dasd.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  4. * Horst Hummel <Horst.Hummel@de.ibm.com>
  5. * Carsten Otte <Cotte@de.ibm.com>
  6. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * Bugreports.to..: <Linux390@de.ibm.com>
  8. * Copyright IBM Corp. 1999, 2009
  9. */
  10. #define KMSG_COMPONENT "dasd"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12. #include <linux/kmod.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ctype.h>
  16. #include <linux/major.h>
  17. #include <linux/slab.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/async.h>
  20. #include <linux/mutex.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/vmalloc.h>
  24. #include <asm/ccwdev.h>
  25. #include <asm/ebcdic.h>
  26. #include <asm/idals.h>
  27. #include <asm/itcw.h>
  28. #include <asm/diag.h>
  29. /* This is ugly... */
  30. #define PRINTK_HEADER "dasd:"
  31. #include "dasd_int.h"
  32. /*
  33. * SECTION: Constant definitions to be used within this file
  34. */
  35. #define DASD_CHANQ_MAX_SIZE 4
  36. #define DASD_DIAG_MOD "dasd_diag_mod"
  37. static unsigned int queue_depth = 32;
  38. static unsigned int nr_hw_queues = 4;
  39. module_param(queue_depth, uint, 0444);
  40. MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
  41. module_param(nr_hw_queues, uint, 0444);
  42. MODULE_PARM_DESC(nr_hw_queues, "Default number of hardware queues for new DASD devices");
  43. /*
  44. * SECTION: exported variables of dasd.c
  45. */
  46. debug_info_t *dasd_debug_area;
  47. EXPORT_SYMBOL(dasd_debug_area);
  48. static struct dentry *dasd_debugfs_root_entry;
  49. struct dasd_discipline *dasd_diag_discipline_pointer;
  50. EXPORT_SYMBOL(dasd_diag_discipline_pointer);
  51. void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
  52. MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
  53. MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
  54. " Copyright IBM Corp. 2000");
  55. MODULE_SUPPORTED_DEVICE("dasd");
  56. MODULE_LICENSE("GPL");
  57. /*
  58. * SECTION: prototypes for static functions of dasd.c
  59. */
  60. static int dasd_alloc_queue(struct dasd_block *);
  61. static void dasd_free_queue(struct dasd_block *);
  62. static int dasd_flush_block_queue(struct dasd_block *);
  63. static void dasd_device_tasklet(unsigned long);
  64. static void dasd_block_tasklet(unsigned long);
  65. static void do_kick_device(struct work_struct *);
  66. static void do_restore_device(struct work_struct *);
  67. static void do_reload_device(struct work_struct *);
  68. static void do_requeue_requests(struct work_struct *);
  69. static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
  70. static void dasd_device_timeout(struct timer_list *);
  71. static void dasd_block_timeout(struct timer_list *);
  72. static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
  73. static void dasd_profile_init(struct dasd_profile *, struct dentry *);
  74. static void dasd_profile_exit(struct dasd_profile *);
  75. static void dasd_hosts_init(struct dentry *, struct dasd_device *);
  76. static void dasd_hosts_exit(struct dasd_device *);
  77. /*
  78. * SECTION: Operations on the device structure.
  79. */
  80. static wait_queue_head_t dasd_init_waitq;
  81. static wait_queue_head_t dasd_flush_wq;
  82. static wait_queue_head_t generic_waitq;
  83. static wait_queue_head_t shutdown_waitq;
  84. /*
  85. * Allocate memory for a new device structure.
  86. */
  87. struct dasd_device *dasd_alloc_device(void)
  88. {
  89. struct dasd_device *device;
  90. device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
  91. if (!device)
  92. return ERR_PTR(-ENOMEM);
  93. /* Get two pages for normal block device operations. */
  94. device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
  95. if (!device->ccw_mem) {
  96. kfree(device);
  97. return ERR_PTR(-ENOMEM);
  98. }
  99. /* Get one page for error recovery. */
  100. device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
  101. if (!device->erp_mem) {
  102. free_pages((unsigned long) device->ccw_mem, 1);
  103. kfree(device);
  104. return ERR_PTR(-ENOMEM);
  105. }
  106. /* Get two pages for ese format. */
  107. device->ese_mem = (void *)__get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
  108. if (!device->ese_mem) {
  109. free_page((unsigned long) device->erp_mem);
  110. free_pages((unsigned long) device->ccw_mem, 1);
  111. kfree(device);
  112. return ERR_PTR(-ENOMEM);
  113. }
  114. dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
  115. dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
  116. dasd_init_chunklist(&device->ese_chunks, device->ese_mem, PAGE_SIZE * 2);
  117. spin_lock_init(&device->mem_lock);
  118. atomic_set(&device->tasklet_scheduled, 0);
  119. tasklet_init(&device->tasklet, dasd_device_tasklet,
  120. (unsigned long) device);
  121. INIT_LIST_HEAD(&device->ccw_queue);
  122. timer_setup(&device->timer, dasd_device_timeout, 0);
  123. INIT_WORK(&device->kick_work, do_kick_device);
  124. INIT_WORK(&device->restore_device, do_restore_device);
  125. INIT_WORK(&device->reload_device, do_reload_device);
  126. INIT_WORK(&device->requeue_requests, do_requeue_requests);
  127. device->state = DASD_STATE_NEW;
  128. device->target = DASD_STATE_NEW;
  129. mutex_init(&device->state_mutex);
  130. spin_lock_init(&device->profile.lock);
  131. return device;
  132. }
  133. /*
  134. * Free memory of a device structure.
  135. */
  136. void dasd_free_device(struct dasd_device *device)
  137. {
  138. kfree(device->private);
  139. free_pages((unsigned long) device->ese_mem, 1);
  140. free_page((unsigned long) device->erp_mem);
  141. free_pages((unsigned long) device->ccw_mem, 1);
  142. kfree(device);
  143. }
  144. /*
  145. * Allocate memory for a new device structure.
  146. */
  147. struct dasd_block *dasd_alloc_block(void)
  148. {
  149. struct dasd_block *block;
  150. block = kzalloc(sizeof(*block), GFP_ATOMIC);
  151. if (!block)
  152. return ERR_PTR(-ENOMEM);
  153. /* open_count = 0 means device online but not in use */
  154. atomic_set(&block->open_count, -1);
  155. atomic_set(&block->tasklet_scheduled, 0);
  156. tasklet_init(&block->tasklet, dasd_block_tasklet,
  157. (unsigned long) block);
  158. INIT_LIST_HEAD(&block->ccw_queue);
  159. spin_lock_init(&block->queue_lock);
  160. INIT_LIST_HEAD(&block->format_list);
  161. spin_lock_init(&block->format_lock);
  162. timer_setup(&block->timer, dasd_block_timeout, 0);
  163. spin_lock_init(&block->profile.lock);
  164. return block;
  165. }
  166. EXPORT_SYMBOL_GPL(dasd_alloc_block);
  167. /*
  168. * Free memory of a device structure.
  169. */
  170. void dasd_free_block(struct dasd_block *block)
  171. {
  172. kfree(block);
  173. }
  174. EXPORT_SYMBOL_GPL(dasd_free_block);
  175. /*
  176. * Make a new device known to the system.
  177. */
  178. static int dasd_state_new_to_known(struct dasd_device *device)
  179. {
  180. int rc;
  181. /*
  182. * As long as the device is not in state DASD_STATE_NEW we want to
  183. * keep the reference count > 0.
  184. */
  185. dasd_get_device(device);
  186. if (device->block) {
  187. rc = dasd_alloc_queue(device->block);
  188. if (rc) {
  189. dasd_put_device(device);
  190. return rc;
  191. }
  192. }
  193. device->state = DASD_STATE_KNOWN;
  194. return 0;
  195. }
  196. /*
  197. * Let the system forget about a device.
  198. */
  199. static int dasd_state_known_to_new(struct dasd_device *device)
  200. {
  201. /* Disable extended error reporting for this device. */
  202. dasd_eer_disable(device);
  203. device->state = DASD_STATE_NEW;
  204. if (device->block)
  205. dasd_free_queue(device->block);
  206. /* Give up reference we took in dasd_state_new_to_known. */
  207. dasd_put_device(device);
  208. return 0;
  209. }
  210. static struct dentry *dasd_debugfs_setup(const char *name,
  211. struct dentry *base_dentry)
  212. {
  213. struct dentry *pde;
  214. if (!base_dentry)
  215. return NULL;
  216. pde = debugfs_create_dir(name, base_dentry);
  217. if (!pde || IS_ERR(pde))
  218. return NULL;
  219. return pde;
  220. }
  221. /*
  222. * Request the irq line for the device.
  223. */
  224. static int dasd_state_known_to_basic(struct dasd_device *device)
  225. {
  226. struct dasd_block *block = device->block;
  227. int rc = 0;
  228. /* Allocate and register gendisk structure. */
  229. if (block) {
  230. rc = dasd_gendisk_alloc(block);
  231. if (rc)
  232. return rc;
  233. block->debugfs_dentry =
  234. dasd_debugfs_setup(block->gdp->disk_name,
  235. dasd_debugfs_root_entry);
  236. dasd_profile_init(&block->profile, block->debugfs_dentry);
  237. if (dasd_global_profile_level == DASD_PROFILE_ON)
  238. dasd_profile_on(&device->block->profile);
  239. }
  240. device->debugfs_dentry =
  241. dasd_debugfs_setup(dev_name(&device->cdev->dev),
  242. dasd_debugfs_root_entry);
  243. dasd_profile_init(&device->profile, device->debugfs_dentry);
  244. dasd_hosts_init(device->debugfs_dentry, device);
  245. /* register 'device' debug area, used for all DBF_DEV_XXX calls */
  246. device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
  247. 8 * sizeof(long));
  248. debug_register_view(device->debug_area, &debug_sprintf_view);
  249. debug_set_level(device->debug_area, DBF_WARNING);
  250. DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
  251. device->state = DASD_STATE_BASIC;
  252. return rc;
  253. }
  254. /*
  255. * Release the irq line for the device. Terminate any running i/o.
  256. */
  257. static int dasd_state_basic_to_known(struct dasd_device *device)
  258. {
  259. int rc;
  260. if (device->discipline->basic_to_known) {
  261. rc = device->discipline->basic_to_known(device);
  262. if (rc)
  263. return rc;
  264. }
  265. if (device->block) {
  266. dasd_profile_exit(&device->block->profile);
  267. debugfs_remove(device->block->debugfs_dentry);
  268. dasd_gendisk_free(device->block);
  269. dasd_block_clear_timer(device->block);
  270. }
  271. rc = dasd_flush_device_queue(device);
  272. if (rc)
  273. return rc;
  274. dasd_device_clear_timer(device);
  275. dasd_profile_exit(&device->profile);
  276. dasd_hosts_exit(device);
  277. debugfs_remove(device->debugfs_dentry);
  278. DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
  279. if (device->debug_area != NULL) {
  280. debug_unregister(device->debug_area);
  281. device->debug_area = NULL;
  282. }
  283. device->state = DASD_STATE_KNOWN;
  284. return 0;
  285. }
  286. /*
  287. * Do the initial analysis. The do_analysis function may return
  288. * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
  289. * until the discipline decides to continue the startup sequence
  290. * by calling the function dasd_change_state. The eckd disciplines
  291. * uses this to start a ccw that detects the format. The completion
  292. * interrupt for this detection ccw uses the kernel event daemon to
  293. * trigger the call to dasd_change_state. All this is done in the
  294. * discipline code, see dasd_eckd.c.
  295. * After the analysis ccw is done (do_analysis returned 0) the block
  296. * device is setup.
  297. * In case the analysis returns an error, the device setup is stopped
  298. * (a fake disk was already added to allow formatting).
  299. */
  300. static int dasd_state_basic_to_ready(struct dasd_device *device)
  301. {
  302. int rc;
  303. struct dasd_block *block;
  304. struct gendisk *disk;
  305. rc = 0;
  306. block = device->block;
  307. /* make disk known with correct capacity */
  308. if (block) {
  309. if (block->base->discipline->do_analysis != NULL)
  310. rc = block->base->discipline->do_analysis(block);
  311. if (rc) {
  312. if (rc != -EAGAIN) {
  313. device->state = DASD_STATE_UNFMT;
  314. disk = device->block->gdp;
  315. kobject_uevent(&disk_to_dev(disk)->kobj,
  316. KOBJ_CHANGE);
  317. goto out;
  318. }
  319. return rc;
  320. }
  321. if (device->discipline->setup_blk_queue)
  322. device->discipline->setup_blk_queue(block);
  323. set_capacity(block->gdp,
  324. block->blocks << block->s2b_shift);
  325. device->state = DASD_STATE_READY;
  326. rc = dasd_scan_partitions(block);
  327. if (rc) {
  328. device->state = DASD_STATE_BASIC;
  329. return rc;
  330. }
  331. } else {
  332. device->state = DASD_STATE_READY;
  333. }
  334. out:
  335. if (device->discipline->basic_to_ready)
  336. rc = device->discipline->basic_to_ready(device);
  337. return rc;
  338. }
  339. static inline
  340. int _wait_for_empty_queues(struct dasd_device *device)
  341. {
  342. if (device->block)
  343. return list_empty(&device->ccw_queue) &&
  344. list_empty(&device->block->ccw_queue);
  345. else
  346. return list_empty(&device->ccw_queue);
  347. }
  348. /*
  349. * Remove device from block device layer. Destroy dirty buffers.
  350. * Forget format information. Check if the target level is basic
  351. * and if it is create fake disk for formatting.
  352. */
  353. static int dasd_state_ready_to_basic(struct dasd_device *device)
  354. {
  355. int rc;
  356. device->state = DASD_STATE_BASIC;
  357. if (device->block) {
  358. struct dasd_block *block = device->block;
  359. rc = dasd_flush_block_queue(block);
  360. if (rc) {
  361. device->state = DASD_STATE_READY;
  362. return rc;
  363. }
  364. dasd_destroy_partitions(block);
  365. block->blocks = 0;
  366. block->bp_block = 0;
  367. block->s2b_shift = 0;
  368. }
  369. return 0;
  370. }
  371. /*
  372. * Back to basic.
  373. */
  374. static int dasd_state_unfmt_to_basic(struct dasd_device *device)
  375. {
  376. device->state = DASD_STATE_BASIC;
  377. return 0;
  378. }
  379. /*
  380. * Make the device online and schedule the bottom half to start
  381. * the requeueing of requests from the linux request queue to the
  382. * ccw queue.
  383. */
  384. static int
  385. dasd_state_ready_to_online(struct dasd_device * device)
  386. {
  387. struct gendisk *disk;
  388. struct disk_part_iter piter;
  389. struct hd_struct *part;
  390. device->state = DASD_STATE_ONLINE;
  391. if (device->block) {
  392. dasd_schedule_block_bh(device->block);
  393. if ((device->features & DASD_FEATURE_USERAW)) {
  394. disk = device->block->gdp;
  395. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  396. return 0;
  397. }
  398. disk = device->block->bdev->bd_disk;
  399. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  400. while ((part = disk_part_iter_next(&piter)))
  401. kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
  402. disk_part_iter_exit(&piter);
  403. }
  404. return 0;
  405. }
  406. /*
  407. * Stop the requeueing of requests again.
  408. */
  409. static int dasd_state_online_to_ready(struct dasd_device *device)
  410. {
  411. int rc;
  412. struct gendisk *disk;
  413. struct disk_part_iter piter;
  414. struct hd_struct *part;
  415. if (device->discipline->online_to_ready) {
  416. rc = device->discipline->online_to_ready(device);
  417. if (rc)
  418. return rc;
  419. }
  420. device->state = DASD_STATE_READY;
  421. if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
  422. disk = device->block->bdev->bd_disk;
  423. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  424. while ((part = disk_part_iter_next(&piter)))
  425. kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
  426. disk_part_iter_exit(&piter);
  427. }
  428. return 0;
  429. }
  430. /*
  431. * Device startup state changes.
  432. */
  433. static int dasd_increase_state(struct dasd_device *device)
  434. {
  435. int rc;
  436. rc = 0;
  437. if (device->state == DASD_STATE_NEW &&
  438. device->target >= DASD_STATE_KNOWN)
  439. rc = dasd_state_new_to_known(device);
  440. if (!rc &&
  441. device->state == DASD_STATE_KNOWN &&
  442. device->target >= DASD_STATE_BASIC)
  443. rc = dasd_state_known_to_basic(device);
  444. if (!rc &&
  445. device->state == DASD_STATE_BASIC &&
  446. device->target >= DASD_STATE_READY)
  447. rc = dasd_state_basic_to_ready(device);
  448. if (!rc &&
  449. device->state == DASD_STATE_UNFMT &&
  450. device->target > DASD_STATE_UNFMT)
  451. rc = -EPERM;
  452. if (!rc &&
  453. device->state == DASD_STATE_READY &&
  454. device->target >= DASD_STATE_ONLINE)
  455. rc = dasd_state_ready_to_online(device);
  456. return rc;
  457. }
  458. /*
  459. * Device shutdown state changes.
  460. */
  461. static int dasd_decrease_state(struct dasd_device *device)
  462. {
  463. int rc;
  464. rc = 0;
  465. if (device->state == DASD_STATE_ONLINE &&
  466. device->target <= DASD_STATE_READY)
  467. rc = dasd_state_online_to_ready(device);
  468. if (!rc &&
  469. device->state == DASD_STATE_READY &&
  470. device->target <= DASD_STATE_BASIC)
  471. rc = dasd_state_ready_to_basic(device);
  472. if (!rc &&
  473. device->state == DASD_STATE_UNFMT &&
  474. device->target <= DASD_STATE_BASIC)
  475. rc = dasd_state_unfmt_to_basic(device);
  476. if (!rc &&
  477. device->state == DASD_STATE_BASIC &&
  478. device->target <= DASD_STATE_KNOWN)
  479. rc = dasd_state_basic_to_known(device);
  480. if (!rc &&
  481. device->state == DASD_STATE_KNOWN &&
  482. device->target <= DASD_STATE_NEW)
  483. rc = dasd_state_known_to_new(device);
  484. return rc;
  485. }
  486. /*
  487. * This is the main startup/shutdown routine.
  488. */
  489. static void dasd_change_state(struct dasd_device *device)
  490. {
  491. int rc;
  492. if (device->state == device->target)
  493. /* Already where we want to go today... */
  494. return;
  495. if (device->state < device->target)
  496. rc = dasd_increase_state(device);
  497. else
  498. rc = dasd_decrease_state(device);
  499. if (rc == -EAGAIN)
  500. return;
  501. if (rc)
  502. device->target = device->state;
  503. /* let user-space know that the device status changed */
  504. kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
  505. if (device->state == device->target)
  506. wake_up(&dasd_init_waitq);
  507. }
  508. /*
  509. * Kick starter for devices that did not complete the startup/shutdown
  510. * procedure or were sleeping because of a pending state.
  511. * dasd_kick_device will schedule a call do do_kick_device to the kernel
  512. * event daemon.
  513. */
  514. static void do_kick_device(struct work_struct *work)
  515. {
  516. struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
  517. mutex_lock(&device->state_mutex);
  518. dasd_change_state(device);
  519. mutex_unlock(&device->state_mutex);
  520. dasd_schedule_device_bh(device);
  521. dasd_put_device(device);
  522. }
  523. void dasd_kick_device(struct dasd_device *device)
  524. {
  525. dasd_get_device(device);
  526. /* queue call to dasd_kick_device to the kernel event daemon. */
  527. if (!schedule_work(&device->kick_work))
  528. dasd_put_device(device);
  529. }
  530. EXPORT_SYMBOL(dasd_kick_device);
  531. /*
  532. * dasd_reload_device will schedule a call do do_reload_device to the kernel
  533. * event daemon.
  534. */
  535. static void do_reload_device(struct work_struct *work)
  536. {
  537. struct dasd_device *device = container_of(work, struct dasd_device,
  538. reload_device);
  539. device->discipline->reload(device);
  540. dasd_put_device(device);
  541. }
  542. void dasd_reload_device(struct dasd_device *device)
  543. {
  544. dasd_get_device(device);
  545. /* queue call to dasd_reload_device to the kernel event daemon. */
  546. if (!schedule_work(&device->reload_device))
  547. dasd_put_device(device);
  548. }
  549. EXPORT_SYMBOL(dasd_reload_device);
  550. /*
  551. * dasd_restore_device will schedule a call do do_restore_device to the kernel
  552. * event daemon.
  553. */
  554. static void do_restore_device(struct work_struct *work)
  555. {
  556. struct dasd_device *device = container_of(work, struct dasd_device,
  557. restore_device);
  558. device->cdev->drv->restore(device->cdev);
  559. dasd_put_device(device);
  560. }
  561. void dasd_restore_device(struct dasd_device *device)
  562. {
  563. dasd_get_device(device);
  564. /* queue call to dasd_restore_device to the kernel event daemon. */
  565. if (!schedule_work(&device->restore_device))
  566. dasd_put_device(device);
  567. }
  568. /*
  569. * Set the target state for a device and starts the state change.
  570. */
  571. void dasd_set_target_state(struct dasd_device *device, int target)
  572. {
  573. dasd_get_device(device);
  574. mutex_lock(&device->state_mutex);
  575. /* If we are in probeonly mode stop at DASD_STATE_READY. */
  576. if (dasd_probeonly && target > DASD_STATE_READY)
  577. target = DASD_STATE_READY;
  578. if (device->target != target) {
  579. if (device->state == target)
  580. wake_up(&dasd_init_waitq);
  581. device->target = target;
  582. }
  583. if (device->state != device->target)
  584. dasd_change_state(device);
  585. mutex_unlock(&device->state_mutex);
  586. dasd_put_device(device);
  587. }
  588. EXPORT_SYMBOL(dasd_set_target_state);
  589. /*
  590. * Enable devices with device numbers in [from..to].
  591. */
  592. static inline int _wait_for_device(struct dasd_device *device)
  593. {
  594. return (device->state == device->target);
  595. }
  596. void dasd_enable_device(struct dasd_device *device)
  597. {
  598. dasd_set_target_state(device, DASD_STATE_ONLINE);
  599. if (device->state <= DASD_STATE_KNOWN)
  600. /* No discipline for device found. */
  601. dasd_set_target_state(device, DASD_STATE_NEW);
  602. /* Now wait for the devices to come up. */
  603. wait_event(dasd_init_waitq, _wait_for_device(device));
  604. dasd_reload_device(device);
  605. if (device->discipline->kick_validate)
  606. device->discipline->kick_validate(device);
  607. }
  608. EXPORT_SYMBOL(dasd_enable_device);
  609. /*
  610. * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
  611. */
  612. unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
  613. #ifdef CONFIG_DASD_PROFILE
  614. struct dasd_profile dasd_global_profile = {
  615. .lock = __SPIN_LOCK_UNLOCKED(dasd_global_profile.lock),
  616. };
  617. static struct dentry *dasd_debugfs_global_entry;
  618. /*
  619. * Add profiling information for cqr before execution.
  620. */
  621. static void dasd_profile_start(struct dasd_block *block,
  622. struct dasd_ccw_req *cqr,
  623. struct request *req)
  624. {
  625. struct list_head *l;
  626. unsigned int counter;
  627. struct dasd_device *device;
  628. /* count the length of the chanq for statistics */
  629. counter = 0;
  630. if (dasd_global_profile_level || block->profile.data)
  631. list_for_each(l, &block->ccw_queue)
  632. if (++counter >= 31)
  633. break;
  634. spin_lock(&dasd_global_profile.lock);
  635. if (dasd_global_profile.data) {
  636. dasd_global_profile.data->dasd_io_nr_req[counter]++;
  637. if (rq_data_dir(req) == READ)
  638. dasd_global_profile.data->dasd_read_nr_req[counter]++;
  639. }
  640. spin_unlock(&dasd_global_profile.lock);
  641. spin_lock(&block->profile.lock);
  642. if (block->profile.data) {
  643. block->profile.data->dasd_io_nr_req[counter]++;
  644. if (rq_data_dir(req) == READ)
  645. block->profile.data->dasd_read_nr_req[counter]++;
  646. }
  647. spin_unlock(&block->profile.lock);
  648. /*
  649. * We count the request for the start device, even though it may run on
  650. * some other device due to error recovery. This way we make sure that
  651. * we count each request only once.
  652. */
  653. device = cqr->startdev;
  654. if (device->profile.data) {
  655. counter = 1; /* request is not yet queued on the start device */
  656. list_for_each(l, &device->ccw_queue)
  657. if (++counter >= 31)
  658. break;
  659. }
  660. spin_lock(&device->profile.lock);
  661. if (device->profile.data) {
  662. device->profile.data->dasd_io_nr_req[counter]++;
  663. if (rq_data_dir(req) == READ)
  664. device->profile.data->dasd_read_nr_req[counter]++;
  665. }
  666. spin_unlock(&device->profile.lock);
  667. }
  668. /*
  669. * Add profiling information for cqr after execution.
  670. */
  671. #define dasd_profile_counter(value, index) \
  672. { \
  673. for (index = 0; index < 31 && value >> (2+index); index++) \
  674. ; \
  675. }
  676. static void dasd_profile_end_add_data(struct dasd_profile_info *data,
  677. int is_alias,
  678. int is_tpm,
  679. int is_read,
  680. long sectors,
  681. int sectors_ind,
  682. int tottime_ind,
  683. int tottimeps_ind,
  684. int strtime_ind,
  685. int irqtime_ind,
  686. int irqtimeps_ind,
  687. int endtime_ind)
  688. {
  689. /* in case of an overflow, reset the whole profile */
  690. if (data->dasd_io_reqs == UINT_MAX) {
  691. memset(data, 0, sizeof(*data));
  692. ktime_get_real_ts64(&data->starttod);
  693. }
  694. data->dasd_io_reqs++;
  695. data->dasd_io_sects += sectors;
  696. if (is_alias)
  697. data->dasd_io_alias++;
  698. if (is_tpm)
  699. data->dasd_io_tpm++;
  700. data->dasd_io_secs[sectors_ind]++;
  701. data->dasd_io_times[tottime_ind]++;
  702. data->dasd_io_timps[tottimeps_ind]++;
  703. data->dasd_io_time1[strtime_ind]++;
  704. data->dasd_io_time2[irqtime_ind]++;
  705. data->dasd_io_time2ps[irqtimeps_ind]++;
  706. data->dasd_io_time3[endtime_ind]++;
  707. if (is_read) {
  708. data->dasd_read_reqs++;
  709. data->dasd_read_sects += sectors;
  710. if (is_alias)
  711. data->dasd_read_alias++;
  712. if (is_tpm)
  713. data->dasd_read_tpm++;
  714. data->dasd_read_secs[sectors_ind]++;
  715. data->dasd_read_times[tottime_ind]++;
  716. data->dasd_read_time1[strtime_ind]++;
  717. data->dasd_read_time2[irqtime_ind]++;
  718. data->dasd_read_time3[endtime_ind]++;
  719. }
  720. }
  721. static void dasd_profile_end(struct dasd_block *block,
  722. struct dasd_ccw_req *cqr,
  723. struct request *req)
  724. {
  725. unsigned long strtime, irqtime, endtime, tottime;
  726. unsigned long tottimeps, sectors;
  727. struct dasd_device *device;
  728. int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
  729. int irqtime_ind, irqtimeps_ind, endtime_ind;
  730. struct dasd_profile_info *data;
  731. device = cqr->startdev;
  732. if (!(dasd_global_profile_level ||
  733. block->profile.data ||
  734. device->profile.data))
  735. return;
  736. sectors = blk_rq_sectors(req);
  737. if (!cqr->buildclk || !cqr->startclk ||
  738. !cqr->stopclk || !cqr->endclk ||
  739. !sectors)
  740. return;
  741. strtime = ((cqr->startclk - cqr->buildclk) >> 12);
  742. irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
  743. endtime = ((cqr->endclk - cqr->stopclk) >> 12);
  744. tottime = ((cqr->endclk - cqr->buildclk) >> 12);
  745. tottimeps = tottime / sectors;
  746. dasd_profile_counter(sectors, sectors_ind);
  747. dasd_profile_counter(tottime, tottime_ind);
  748. dasd_profile_counter(tottimeps, tottimeps_ind);
  749. dasd_profile_counter(strtime, strtime_ind);
  750. dasd_profile_counter(irqtime, irqtime_ind);
  751. dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
  752. dasd_profile_counter(endtime, endtime_ind);
  753. spin_lock(&dasd_global_profile.lock);
  754. if (dasd_global_profile.data) {
  755. data = dasd_global_profile.data;
  756. data->dasd_sum_times += tottime;
  757. data->dasd_sum_time_str += strtime;
  758. data->dasd_sum_time_irq += irqtime;
  759. data->dasd_sum_time_end += endtime;
  760. dasd_profile_end_add_data(dasd_global_profile.data,
  761. cqr->startdev != block->base,
  762. cqr->cpmode == 1,
  763. rq_data_dir(req) == READ,
  764. sectors, sectors_ind, tottime_ind,
  765. tottimeps_ind, strtime_ind,
  766. irqtime_ind, irqtimeps_ind,
  767. endtime_ind);
  768. }
  769. spin_unlock(&dasd_global_profile.lock);
  770. spin_lock(&block->profile.lock);
  771. if (block->profile.data) {
  772. data = block->profile.data;
  773. data->dasd_sum_times += tottime;
  774. data->dasd_sum_time_str += strtime;
  775. data->dasd_sum_time_irq += irqtime;
  776. data->dasd_sum_time_end += endtime;
  777. dasd_profile_end_add_data(block->profile.data,
  778. cqr->startdev != block->base,
  779. cqr->cpmode == 1,
  780. rq_data_dir(req) == READ,
  781. sectors, sectors_ind, tottime_ind,
  782. tottimeps_ind, strtime_ind,
  783. irqtime_ind, irqtimeps_ind,
  784. endtime_ind);
  785. }
  786. spin_unlock(&block->profile.lock);
  787. spin_lock(&device->profile.lock);
  788. if (device->profile.data) {
  789. data = device->profile.data;
  790. data->dasd_sum_times += tottime;
  791. data->dasd_sum_time_str += strtime;
  792. data->dasd_sum_time_irq += irqtime;
  793. data->dasd_sum_time_end += endtime;
  794. dasd_profile_end_add_data(device->profile.data,
  795. cqr->startdev != block->base,
  796. cqr->cpmode == 1,
  797. rq_data_dir(req) == READ,
  798. sectors, sectors_ind, tottime_ind,
  799. tottimeps_ind, strtime_ind,
  800. irqtime_ind, irqtimeps_ind,
  801. endtime_ind);
  802. }
  803. spin_unlock(&device->profile.lock);
  804. }
  805. void dasd_profile_reset(struct dasd_profile *profile)
  806. {
  807. struct dasd_profile_info *data;
  808. spin_lock_bh(&profile->lock);
  809. data = profile->data;
  810. if (!data) {
  811. spin_unlock_bh(&profile->lock);
  812. return;
  813. }
  814. memset(data, 0, sizeof(*data));
  815. ktime_get_real_ts64(&data->starttod);
  816. spin_unlock_bh(&profile->lock);
  817. }
  818. int dasd_profile_on(struct dasd_profile *profile)
  819. {
  820. struct dasd_profile_info *data;
  821. data = kzalloc(sizeof(*data), GFP_KERNEL);
  822. if (!data)
  823. return -ENOMEM;
  824. spin_lock_bh(&profile->lock);
  825. if (profile->data) {
  826. spin_unlock_bh(&profile->lock);
  827. kfree(data);
  828. return 0;
  829. }
  830. ktime_get_real_ts64(&data->starttod);
  831. profile->data = data;
  832. spin_unlock_bh(&profile->lock);
  833. return 0;
  834. }
  835. void dasd_profile_off(struct dasd_profile *profile)
  836. {
  837. spin_lock_bh(&profile->lock);
  838. kfree(profile->data);
  839. profile->data = NULL;
  840. spin_unlock_bh(&profile->lock);
  841. }
  842. char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
  843. {
  844. char *buffer;
  845. buffer = vmalloc(user_len + 1);
  846. if (buffer == NULL)
  847. return ERR_PTR(-ENOMEM);
  848. if (copy_from_user(buffer, user_buf, user_len) != 0) {
  849. vfree(buffer);
  850. return ERR_PTR(-EFAULT);
  851. }
  852. /* got the string, now strip linefeed. */
  853. if (buffer[user_len - 1] == '\n')
  854. buffer[user_len - 1] = 0;
  855. else
  856. buffer[user_len] = 0;
  857. return buffer;
  858. }
  859. static ssize_t dasd_stats_write(struct file *file,
  860. const char __user *user_buf,
  861. size_t user_len, loff_t *pos)
  862. {
  863. char *buffer, *str;
  864. int rc;
  865. struct seq_file *m = (struct seq_file *)file->private_data;
  866. struct dasd_profile *prof = m->private;
  867. if (user_len > 65536)
  868. user_len = 65536;
  869. buffer = dasd_get_user_string(user_buf, user_len);
  870. if (IS_ERR(buffer))
  871. return PTR_ERR(buffer);
  872. str = skip_spaces(buffer);
  873. rc = user_len;
  874. if (strncmp(str, "reset", 5) == 0) {
  875. dasd_profile_reset(prof);
  876. } else if (strncmp(str, "on", 2) == 0) {
  877. rc = dasd_profile_on(prof);
  878. if (rc)
  879. goto out;
  880. rc = user_len;
  881. if (prof == &dasd_global_profile) {
  882. dasd_profile_reset(prof);
  883. dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
  884. }
  885. } else if (strncmp(str, "off", 3) == 0) {
  886. if (prof == &dasd_global_profile)
  887. dasd_global_profile_level = DASD_PROFILE_OFF;
  888. dasd_profile_off(prof);
  889. } else
  890. rc = -EINVAL;
  891. out:
  892. vfree(buffer);
  893. return rc;
  894. }
  895. static void dasd_stats_array(struct seq_file *m, unsigned int *array)
  896. {
  897. int i;
  898. for (i = 0; i < 32; i++)
  899. seq_printf(m, "%u ", array[i]);
  900. seq_putc(m, '\n');
  901. }
  902. static void dasd_stats_seq_print(struct seq_file *m,
  903. struct dasd_profile_info *data)
  904. {
  905. seq_printf(m, "start_time %lld.%09ld\n",
  906. (s64)data->starttod.tv_sec, data->starttod.tv_nsec);
  907. seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
  908. seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
  909. seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
  910. seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
  911. seq_printf(m, "avg_total %lu\n", data->dasd_io_reqs ?
  912. data->dasd_sum_times / data->dasd_io_reqs : 0UL);
  913. seq_printf(m, "avg_build_to_ssch %lu\n", data->dasd_io_reqs ?
  914. data->dasd_sum_time_str / data->dasd_io_reqs : 0UL);
  915. seq_printf(m, "avg_ssch_to_irq %lu\n", data->dasd_io_reqs ?
  916. data->dasd_sum_time_irq / data->dasd_io_reqs : 0UL);
  917. seq_printf(m, "avg_irq_to_end %lu\n", data->dasd_io_reqs ?
  918. data->dasd_sum_time_end / data->dasd_io_reqs : 0UL);
  919. seq_puts(m, "histogram_sectors ");
  920. dasd_stats_array(m, data->dasd_io_secs);
  921. seq_puts(m, "histogram_io_times ");
  922. dasd_stats_array(m, data->dasd_io_times);
  923. seq_puts(m, "histogram_io_times_weighted ");
  924. dasd_stats_array(m, data->dasd_io_timps);
  925. seq_puts(m, "histogram_time_build_to_ssch ");
  926. dasd_stats_array(m, data->dasd_io_time1);
  927. seq_puts(m, "histogram_time_ssch_to_irq ");
  928. dasd_stats_array(m, data->dasd_io_time2);
  929. seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
  930. dasd_stats_array(m, data->dasd_io_time2ps);
  931. seq_puts(m, "histogram_time_irq_to_end ");
  932. dasd_stats_array(m, data->dasd_io_time3);
  933. seq_puts(m, "histogram_ccw_queue_length ");
  934. dasd_stats_array(m, data->dasd_io_nr_req);
  935. seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
  936. seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
  937. seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
  938. seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
  939. seq_puts(m, "histogram_read_sectors ");
  940. dasd_stats_array(m, data->dasd_read_secs);
  941. seq_puts(m, "histogram_read_times ");
  942. dasd_stats_array(m, data->dasd_read_times);
  943. seq_puts(m, "histogram_read_time_build_to_ssch ");
  944. dasd_stats_array(m, data->dasd_read_time1);
  945. seq_puts(m, "histogram_read_time_ssch_to_irq ");
  946. dasd_stats_array(m, data->dasd_read_time2);
  947. seq_puts(m, "histogram_read_time_irq_to_end ");
  948. dasd_stats_array(m, data->dasd_read_time3);
  949. seq_puts(m, "histogram_read_ccw_queue_length ");
  950. dasd_stats_array(m, data->dasd_read_nr_req);
  951. }
  952. static int dasd_stats_show(struct seq_file *m, void *v)
  953. {
  954. struct dasd_profile *profile;
  955. struct dasd_profile_info *data;
  956. profile = m->private;
  957. spin_lock_bh(&profile->lock);
  958. data = profile->data;
  959. if (!data) {
  960. spin_unlock_bh(&profile->lock);
  961. seq_puts(m, "disabled\n");
  962. return 0;
  963. }
  964. dasd_stats_seq_print(m, data);
  965. spin_unlock_bh(&profile->lock);
  966. return 0;
  967. }
  968. static int dasd_stats_open(struct inode *inode, struct file *file)
  969. {
  970. struct dasd_profile *profile = inode->i_private;
  971. return single_open(file, dasd_stats_show, profile);
  972. }
  973. static const struct file_operations dasd_stats_raw_fops = {
  974. .owner = THIS_MODULE,
  975. .open = dasd_stats_open,
  976. .read = seq_read,
  977. .llseek = seq_lseek,
  978. .release = single_release,
  979. .write = dasd_stats_write,
  980. };
  981. static void dasd_profile_init(struct dasd_profile *profile,
  982. struct dentry *base_dentry)
  983. {
  984. umode_t mode;
  985. struct dentry *pde;
  986. if (!base_dentry)
  987. return;
  988. profile->dentry = NULL;
  989. profile->data = NULL;
  990. mode = (S_IRUSR | S_IWUSR | S_IFREG);
  991. pde = debugfs_create_file("statistics", mode, base_dentry,
  992. profile, &dasd_stats_raw_fops);
  993. if (pde && !IS_ERR(pde))
  994. profile->dentry = pde;
  995. return;
  996. }
  997. static void dasd_profile_exit(struct dasd_profile *profile)
  998. {
  999. dasd_profile_off(profile);
  1000. debugfs_remove(profile->dentry);
  1001. profile->dentry = NULL;
  1002. }
  1003. static void dasd_statistics_removeroot(void)
  1004. {
  1005. dasd_global_profile_level = DASD_PROFILE_OFF;
  1006. dasd_profile_exit(&dasd_global_profile);
  1007. debugfs_remove(dasd_debugfs_global_entry);
  1008. debugfs_remove(dasd_debugfs_root_entry);
  1009. }
  1010. static void dasd_statistics_createroot(void)
  1011. {
  1012. struct dentry *pde;
  1013. dasd_debugfs_root_entry = NULL;
  1014. pde = debugfs_create_dir("dasd", NULL);
  1015. if (!pde || IS_ERR(pde))
  1016. goto error;
  1017. dasd_debugfs_root_entry = pde;
  1018. pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
  1019. if (!pde || IS_ERR(pde))
  1020. goto error;
  1021. dasd_debugfs_global_entry = pde;
  1022. dasd_profile_init(&dasd_global_profile, dasd_debugfs_global_entry);
  1023. return;
  1024. error:
  1025. DBF_EVENT(DBF_ERR, "%s",
  1026. "Creation of the dasd debugfs interface failed");
  1027. dasd_statistics_removeroot();
  1028. return;
  1029. }
  1030. #else
  1031. #define dasd_profile_start(block, cqr, req) do {} while (0)
  1032. #define dasd_profile_end(block, cqr, req) do {} while (0)
  1033. static void dasd_statistics_createroot(void)
  1034. {
  1035. return;
  1036. }
  1037. static void dasd_statistics_removeroot(void)
  1038. {
  1039. return;
  1040. }
  1041. int dasd_stats_generic_show(struct seq_file *m, void *v)
  1042. {
  1043. seq_puts(m, "Statistics are not activated in this kernel\n");
  1044. return 0;
  1045. }
  1046. static void dasd_profile_init(struct dasd_profile *profile,
  1047. struct dentry *base_dentry)
  1048. {
  1049. return;
  1050. }
  1051. static void dasd_profile_exit(struct dasd_profile *profile)
  1052. {
  1053. return;
  1054. }
  1055. int dasd_profile_on(struct dasd_profile *profile)
  1056. {
  1057. return 0;
  1058. }
  1059. #endif /* CONFIG_DASD_PROFILE */
  1060. static int dasd_hosts_show(struct seq_file *m, void *v)
  1061. {
  1062. struct dasd_device *device;
  1063. int rc = -EOPNOTSUPP;
  1064. device = m->private;
  1065. dasd_get_device(device);
  1066. if (device->discipline->hosts_print)
  1067. rc = device->discipline->hosts_print(device, m);
  1068. dasd_put_device(device);
  1069. return rc;
  1070. }
  1071. DEFINE_SHOW_ATTRIBUTE(dasd_hosts);
  1072. static void dasd_hosts_exit(struct dasd_device *device)
  1073. {
  1074. debugfs_remove(device->hosts_dentry);
  1075. device->hosts_dentry = NULL;
  1076. }
  1077. static void dasd_hosts_init(struct dentry *base_dentry,
  1078. struct dasd_device *device)
  1079. {
  1080. struct dentry *pde;
  1081. umode_t mode;
  1082. if (!base_dentry)
  1083. return;
  1084. mode = S_IRUSR | S_IFREG;
  1085. pde = debugfs_create_file("host_access_list", mode, base_dentry,
  1086. device, &dasd_hosts_fops);
  1087. if (pde && !IS_ERR(pde))
  1088. device->hosts_dentry = pde;
  1089. }
  1090. struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength, int datasize,
  1091. struct dasd_device *device,
  1092. struct dasd_ccw_req *cqr)
  1093. {
  1094. unsigned long flags;
  1095. char *data, *chunk;
  1096. int size = 0;
  1097. if (cplength > 0)
  1098. size += cplength * sizeof(struct ccw1);
  1099. if (datasize > 0)
  1100. size += datasize;
  1101. if (!cqr)
  1102. size += (sizeof(*cqr) + 7L) & -8L;
  1103. spin_lock_irqsave(&device->mem_lock, flags);
  1104. data = chunk = dasd_alloc_chunk(&device->ccw_chunks, size);
  1105. spin_unlock_irqrestore(&device->mem_lock, flags);
  1106. if (!chunk)
  1107. return ERR_PTR(-ENOMEM);
  1108. if (!cqr) {
  1109. cqr = (void *) data;
  1110. data += (sizeof(*cqr) + 7L) & -8L;
  1111. }
  1112. memset(cqr, 0, sizeof(*cqr));
  1113. cqr->mem_chunk = chunk;
  1114. if (cplength > 0) {
  1115. cqr->cpaddr = data;
  1116. data += cplength * sizeof(struct ccw1);
  1117. memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
  1118. }
  1119. if (datasize > 0) {
  1120. cqr->data = data;
  1121. memset(cqr->data, 0, datasize);
  1122. }
  1123. cqr->magic = magic;
  1124. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  1125. dasd_get_device(device);
  1126. return cqr;
  1127. }
  1128. EXPORT_SYMBOL(dasd_smalloc_request);
  1129. struct dasd_ccw_req *dasd_fmalloc_request(int magic, int cplength,
  1130. int datasize,
  1131. struct dasd_device *device)
  1132. {
  1133. struct dasd_ccw_req *cqr;
  1134. unsigned long flags;
  1135. int size, cqr_size;
  1136. char *data;
  1137. cqr_size = (sizeof(*cqr) + 7L) & -8L;
  1138. size = cqr_size;
  1139. if (cplength > 0)
  1140. size += cplength * sizeof(struct ccw1);
  1141. if (datasize > 0)
  1142. size += datasize;
  1143. spin_lock_irqsave(&device->mem_lock, flags);
  1144. cqr = dasd_alloc_chunk(&device->ese_chunks, size);
  1145. spin_unlock_irqrestore(&device->mem_lock, flags);
  1146. if (!cqr)
  1147. return ERR_PTR(-ENOMEM);
  1148. memset(cqr, 0, sizeof(*cqr));
  1149. data = (char *)cqr + cqr_size;
  1150. cqr->cpaddr = NULL;
  1151. if (cplength > 0) {
  1152. cqr->cpaddr = data;
  1153. data += cplength * sizeof(struct ccw1);
  1154. memset(cqr->cpaddr, 0, cplength * sizeof(struct ccw1));
  1155. }
  1156. cqr->data = NULL;
  1157. if (datasize > 0) {
  1158. cqr->data = data;
  1159. memset(cqr->data, 0, datasize);
  1160. }
  1161. cqr->magic = magic;
  1162. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  1163. dasd_get_device(device);
  1164. return cqr;
  1165. }
  1166. EXPORT_SYMBOL(dasd_fmalloc_request);
  1167. void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
  1168. {
  1169. unsigned long flags;
  1170. spin_lock_irqsave(&device->mem_lock, flags);
  1171. dasd_free_chunk(&device->ccw_chunks, cqr->mem_chunk);
  1172. spin_unlock_irqrestore(&device->mem_lock, flags);
  1173. dasd_put_device(device);
  1174. }
  1175. EXPORT_SYMBOL(dasd_sfree_request);
  1176. void dasd_ffree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
  1177. {
  1178. unsigned long flags;
  1179. spin_lock_irqsave(&device->mem_lock, flags);
  1180. dasd_free_chunk(&device->ese_chunks, cqr);
  1181. spin_unlock_irqrestore(&device->mem_lock, flags);
  1182. dasd_put_device(device);
  1183. }
  1184. EXPORT_SYMBOL(dasd_ffree_request);
  1185. /*
  1186. * Check discipline magic in cqr.
  1187. */
  1188. static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
  1189. {
  1190. struct dasd_device *device;
  1191. if (cqr == NULL)
  1192. return -EINVAL;
  1193. device = cqr->startdev;
  1194. if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
  1195. DBF_DEV_EVENT(DBF_WARNING, device,
  1196. " dasd_ccw_req 0x%08x magic doesn't match"
  1197. " discipline 0x%08x",
  1198. cqr->magic,
  1199. *(unsigned int *) device->discipline->name);
  1200. return -EINVAL;
  1201. }
  1202. return 0;
  1203. }
  1204. /*
  1205. * Terminate the current i/o and set the request to clear_pending.
  1206. * Timer keeps device runnig.
  1207. * ccw_device_clear can fail if the i/o subsystem
  1208. * is in a bad mood.
  1209. */
  1210. int dasd_term_IO(struct dasd_ccw_req *cqr)
  1211. {
  1212. struct dasd_device *device;
  1213. int retries, rc;
  1214. char errorstring[ERRORLENGTH];
  1215. /* Check the cqr */
  1216. rc = dasd_check_cqr(cqr);
  1217. if (rc)
  1218. return rc;
  1219. retries = 0;
  1220. device = (struct dasd_device *) cqr->startdev;
  1221. while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
  1222. rc = ccw_device_clear(device->cdev, (long) cqr);
  1223. switch (rc) {
  1224. case 0: /* termination successful */
  1225. cqr->status = DASD_CQR_CLEAR_PENDING;
  1226. cqr->stopclk = get_tod_clock();
  1227. cqr->starttime = 0;
  1228. DBF_DEV_EVENT(DBF_DEBUG, device,
  1229. "terminate cqr %p successful",
  1230. cqr);
  1231. break;
  1232. case -ENODEV:
  1233. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  1234. "device gone, retry");
  1235. break;
  1236. case -EINVAL:
  1237. /*
  1238. * device not valid so no I/O could be running
  1239. * handle CQR as termination successful
  1240. */
  1241. cqr->status = DASD_CQR_CLEARED;
  1242. cqr->stopclk = get_tod_clock();
  1243. cqr->starttime = 0;
  1244. /* no retries for invalid devices */
  1245. cqr->retries = -1;
  1246. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  1247. "EINVAL, handle as terminated");
  1248. /* fake rc to success */
  1249. rc = 0;
  1250. break;
  1251. default:
  1252. /* internal error 10 - unknown rc*/
  1253. snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
  1254. dev_err(&device->cdev->dev, "An error occurred in the "
  1255. "DASD device driver, reason=%s\n", errorstring);
  1256. BUG();
  1257. break;
  1258. }
  1259. retries++;
  1260. }
  1261. dasd_schedule_device_bh(device);
  1262. return rc;
  1263. }
  1264. EXPORT_SYMBOL(dasd_term_IO);
  1265. /*
  1266. * Start the i/o. This start_IO can fail if the channel is really busy.
  1267. * In that case set up a timer to start the request later.
  1268. */
  1269. int dasd_start_IO(struct dasd_ccw_req *cqr)
  1270. {
  1271. struct dasd_device *device;
  1272. int rc;
  1273. char errorstring[ERRORLENGTH];
  1274. /* Check the cqr */
  1275. rc = dasd_check_cqr(cqr);
  1276. if (rc) {
  1277. cqr->intrc = rc;
  1278. return rc;
  1279. }
  1280. device = (struct dasd_device *) cqr->startdev;
  1281. if (((cqr->block &&
  1282. test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
  1283. test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
  1284. !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
  1285. DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
  1286. "because of stolen lock", cqr);
  1287. cqr->status = DASD_CQR_ERROR;
  1288. cqr->intrc = -EPERM;
  1289. return -EPERM;
  1290. }
  1291. if (cqr->retries < 0) {
  1292. /* internal error 14 - start_IO run out of retries */
  1293. sprintf(errorstring, "14 %p", cqr);
  1294. dev_err(&device->cdev->dev, "An error occurred in the DASD "
  1295. "device driver, reason=%s\n", errorstring);
  1296. cqr->status = DASD_CQR_ERROR;
  1297. return -EIO;
  1298. }
  1299. cqr->startclk = get_tod_clock();
  1300. cqr->starttime = jiffies;
  1301. cqr->retries--;
  1302. if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
  1303. cqr->lpm &= dasd_path_get_opm(device);
  1304. if (!cqr->lpm)
  1305. cqr->lpm = dasd_path_get_opm(device);
  1306. }
  1307. if (cqr->cpmode == 1) {
  1308. rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
  1309. (long) cqr, cqr->lpm);
  1310. } else {
  1311. rc = ccw_device_start(device->cdev, cqr->cpaddr,
  1312. (long) cqr, cqr->lpm, 0);
  1313. }
  1314. switch (rc) {
  1315. case 0:
  1316. cqr->status = DASD_CQR_IN_IO;
  1317. break;
  1318. case -EBUSY:
  1319. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  1320. "start_IO: device busy, retry later");
  1321. break;
  1322. case -EACCES:
  1323. /* -EACCES indicates that the request used only a subset of the
  1324. * available paths and all these paths are gone. If the lpm of
  1325. * this request was only a subset of the opm (e.g. the ppm) then
  1326. * we just do a retry with all available paths.
  1327. * If we already use the full opm, something is amiss, and we
  1328. * need a full path verification.
  1329. */
  1330. if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
  1331. DBF_DEV_EVENT(DBF_WARNING, device,
  1332. "start_IO: selected paths gone (%x)",
  1333. cqr->lpm);
  1334. } else if (cqr->lpm != dasd_path_get_opm(device)) {
  1335. cqr->lpm = dasd_path_get_opm(device);
  1336. DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
  1337. "start_IO: selected paths gone,"
  1338. " retry on all paths");
  1339. } else {
  1340. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  1341. "start_IO: all paths in opm gone,"
  1342. " do path verification");
  1343. dasd_generic_last_path_gone(device);
  1344. dasd_path_no_path(device);
  1345. dasd_path_set_tbvpm(device,
  1346. ccw_device_get_path_mask(
  1347. device->cdev));
  1348. }
  1349. break;
  1350. case -ENODEV:
  1351. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  1352. "start_IO: -ENODEV device gone, retry");
  1353. break;
  1354. case -EIO:
  1355. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  1356. "start_IO: -EIO device gone, retry");
  1357. break;
  1358. case -EINVAL:
  1359. /* most likely caused in power management context */
  1360. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  1361. "start_IO: -EINVAL device currently "
  1362. "not accessible");
  1363. break;
  1364. default:
  1365. /* internal error 11 - unknown rc */
  1366. snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
  1367. dev_err(&device->cdev->dev,
  1368. "An error occurred in the DASD device driver, "
  1369. "reason=%s\n", errorstring);
  1370. BUG();
  1371. break;
  1372. }
  1373. cqr->intrc = rc;
  1374. return rc;
  1375. }
  1376. EXPORT_SYMBOL(dasd_start_IO);
  1377. /*
  1378. * Timeout function for dasd devices. This is used for different purposes
  1379. * 1) missing interrupt handler for normal operation
  1380. * 2) delayed start of request where start_IO failed with -EBUSY
  1381. * 3) timeout for missing state change interrupts
  1382. * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
  1383. * DASD_CQR_QUEUED for 2) and 3).
  1384. */
  1385. static void dasd_device_timeout(struct timer_list *t)
  1386. {
  1387. unsigned long flags;
  1388. struct dasd_device *device;
  1389. device = from_timer(device, t, timer);
  1390. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1391. /* re-activate request queue */
  1392. dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
  1393. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1394. dasd_schedule_device_bh(device);
  1395. }
  1396. /*
  1397. * Setup timeout for a device in jiffies.
  1398. */
  1399. void dasd_device_set_timer(struct dasd_device *device, int expires)
  1400. {
  1401. if (expires == 0)
  1402. del_timer(&device->timer);
  1403. else
  1404. mod_timer(&device->timer, jiffies + expires);
  1405. }
  1406. EXPORT_SYMBOL(dasd_device_set_timer);
  1407. /*
  1408. * Clear timeout for a device.
  1409. */
  1410. void dasd_device_clear_timer(struct dasd_device *device)
  1411. {
  1412. del_timer(&device->timer);
  1413. }
  1414. EXPORT_SYMBOL(dasd_device_clear_timer);
  1415. static void dasd_handle_killed_request(struct ccw_device *cdev,
  1416. unsigned long intparm)
  1417. {
  1418. struct dasd_ccw_req *cqr;
  1419. struct dasd_device *device;
  1420. if (!intparm)
  1421. return;
  1422. cqr = (struct dasd_ccw_req *) intparm;
  1423. if (cqr->status != DASD_CQR_IN_IO) {
  1424. DBF_EVENT_DEVID(DBF_DEBUG, cdev,
  1425. "invalid status in handle_killed_request: "
  1426. "%02x", cqr->status);
  1427. return;
  1428. }
  1429. device = dasd_device_from_cdev_locked(cdev);
  1430. if (IS_ERR(device)) {
  1431. DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
  1432. "unable to get device from cdev");
  1433. return;
  1434. }
  1435. if (!cqr->startdev ||
  1436. device != cqr->startdev ||
  1437. strncmp(cqr->startdev->discipline->ebcname,
  1438. (char *) &cqr->magic, 4)) {
  1439. DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
  1440. "invalid device in request");
  1441. dasd_put_device(device);
  1442. return;
  1443. }
  1444. /* Schedule request to be retried. */
  1445. cqr->status = DASD_CQR_QUEUED;
  1446. dasd_device_clear_timer(device);
  1447. dasd_schedule_device_bh(device);
  1448. dasd_put_device(device);
  1449. }
  1450. void dasd_generic_handle_state_change(struct dasd_device *device)
  1451. {
  1452. /* First of all start sense subsystem status request. */
  1453. dasd_eer_snss(device);
  1454. dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
  1455. dasd_schedule_device_bh(device);
  1456. if (device->block) {
  1457. dasd_schedule_block_bh(device->block);
  1458. if (device->block->request_queue)
  1459. blk_mq_run_hw_queues(device->block->request_queue,
  1460. true);
  1461. }
  1462. }
  1463. EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
  1464. static int dasd_check_hpf_error(struct irb *irb)
  1465. {
  1466. return (scsw_tm_is_valid_schxs(&irb->scsw) &&
  1467. (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
  1468. irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
  1469. }
  1470. static int dasd_ese_needs_format(struct dasd_block *block, struct irb *irb)
  1471. {
  1472. struct dasd_device *device = NULL;
  1473. u8 *sense = NULL;
  1474. if (!block)
  1475. return 0;
  1476. device = block->base;
  1477. if (!device || !device->discipline->is_ese)
  1478. return 0;
  1479. if (!device->discipline->is_ese(device))
  1480. return 0;
  1481. sense = dasd_get_sense(irb);
  1482. if (!sense)
  1483. return 0;
  1484. return !!(sense[1] & SNS1_NO_REC_FOUND) ||
  1485. !!(sense[1] & SNS1_FILE_PROTECTED) ||
  1486. scsw_cstat(&irb->scsw) == SCHN_STAT_INCORR_LEN;
  1487. }
  1488. static int dasd_ese_oos_cond(u8 *sense)
  1489. {
  1490. return sense[0] & SNS0_EQUIPMENT_CHECK &&
  1491. sense[1] & SNS1_PERM_ERR &&
  1492. sense[1] & SNS1_WRITE_INHIBITED &&
  1493. sense[25] == 0x01;
  1494. }
  1495. /*
  1496. * Interrupt handler for "normal" ssch-io based dasd devices.
  1497. */
  1498. void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
  1499. struct irb *irb)
  1500. {
  1501. struct dasd_ccw_req *cqr, *next, *fcqr;
  1502. struct dasd_device *device;
  1503. unsigned long now;
  1504. int nrf_suppressed = 0;
  1505. int fp_suppressed = 0;
  1506. u8 *sense = NULL;
  1507. int expires;
  1508. cqr = (struct dasd_ccw_req *) intparm;
  1509. if (IS_ERR(irb)) {
  1510. switch (PTR_ERR(irb)) {
  1511. case -EIO:
  1512. if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
  1513. device = cqr->startdev;
  1514. cqr->status = DASD_CQR_CLEARED;
  1515. dasd_device_clear_timer(device);
  1516. wake_up(&dasd_flush_wq);
  1517. dasd_schedule_device_bh(device);
  1518. return;
  1519. }
  1520. break;
  1521. case -ETIMEDOUT:
  1522. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
  1523. "request timed out\n", __func__);
  1524. break;
  1525. default:
  1526. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
  1527. "unknown error %ld\n", __func__,
  1528. PTR_ERR(irb));
  1529. }
  1530. dasd_handle_killed_request(cdev, intparm);
  1531. return;
  1532. }
  1533. now = get_tod_clock();
  1534. /* check for conditions that should be handled immediately */
  1535. if (!cqr ||
  1536. !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  1537. scsw_cstat(&irb->scsw) == 0)) {
  1538. if (cqr)
  1539. memcpy(&cqr->irb, irb, sizeof(*irb));
  1540. device = dasd_device_from_cdev_locked(cdev);
  1541. if (IS_ERR(device))
  1542. return;
  1543. /* ignore unsolicited interrupts for DIAG discipline */
  1544. if (device->discipline == dasd_diag_discipline_pointer) {
  1545. dasd_put_device(device);
  1546. return;
  1547. }
  1548. /*
  1549. * In some cases 'File Protected' or 'No Record Found' errors
  1550. * might be expected and debug log messages for the
  1551. * corresponding interrupts shouldn't be written then.
  1552. * Check if either of the according suppress bits is set.
  1553. */
  1554. sense = dasd_get_sense(irb);
  1555. if (sense) {
  1556. fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
  1557. test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
  1558. nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
  1559. test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
  1560. /*
  1561. * Extent pool probably out-of-space.
  1562. * Stop device and check exhaust level.
  1563. */
  1564. if (dasd_ese_oos_cond(sense)) {
  1565. dasd_generic_space_exhaust(device, cqr);
  1566. device->discipline->ext_pool_exhaust(device, cqr);
  1567. dasd_put_device(device);
  1568. return;
  1569. }
  1570. }
  1571. if (!(fp_suppressed || nrf_suppressed))
  1572. device->discipline->dump_sense_dbf(device, irb, "int");
  1573. if (device->features & DASD_FEATURE_ERPLOG)
  1574. device->discipline->dump_sense(device, cqr, irb);
  1575. device->discipline->check_for_device_change(device, cqr, irb);
  1576. dasd_put_device(device);
  1577. }
  1578. /* check for for attention message */
  1579. if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
  1580. device = dasd_device_from_cdev_locked(cdev);
  1581. if (!IS_ERR(device)) {
  1582. device->discipline->check_attention(device,
  1583. irb->esw.esw1.lpum);
  1584. dasd_put_device(device);
  1585. }
  1586. }
  1587. if (!cqr)
  1588. return;
  1589. device = (struct dasd_device *) cqr->startdev;
  1590. if (!device ||
  1591. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  1592. DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
  1593. "invalid device in request");
  1594. return;
  1595. }
  1596. if (dasd_ese_needs_format(cqr->block, irb)) {
  1597. if (rq_data_dir((struct request *)cqr->callback_data) == READ) {
  1598. device->discipline->ese_read(cqr, irb);
  1599. cqr->status = DASD_CQR_SUCCESS;
  1600. cqr->stopclk = now;
  1601. dasd_device_clear_timer(device);
  1602. dasd_schedule_device_bh(device);
  1603. return;
  1604. }
  1605. fcqr = device->discipline->ese_format(device, cqr, irb);
  1606. if (IS_ERR(fcqr)) {
  1607. if (PTR_ERR(fcqr) == -EINVAL) {
  1608. cqr->status = DASD_CQR_ERROR;
  1609. return;
  1610. }
  1611. /*
  1612. * If we can't format now, let the request go
  1613. * one extra round. Maybe we can format later.
  1614. */
  1615. cqr->status = DASD_CQR_QUEUED;
  1616. dasd_schedule_device_bh(device);
  1617. return;
  1618. } else {
  1619. fcqr->status = DASD_CQR_QUEUED;
  1620. cqr->status = DASD_CQR_QUEUED;
  1621. list_add(&fcqr->devlist, &device->ccw_queue);
  1622. dasd_schedule_device_bh(device);
  1623. return;
  1624. }
  1625. }
  1626. /* Check for clear pending */
  1627. if (cqr->status == DASD_CQR_CLEAR_PENDING &&
  1628. scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
  1629. cqr->status = DASD_CQR_CLEARED;
  1630. dasd_device_clear_timer(device);
  1631. wake_up(&dasd_flush_wq);
  1632. dasd_schedule_device_bh(device);
  1633. return;
  1634. }
  1635. /* check status - the request might have been killed by dyn detach */
  1636. if (cqr->status != DASD_CQR_IN_IO) {
  1637. DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
  1638. "status %02x", dev_name(&cdev->dev), cqr->status);
  1639. return;
  1640. }
  1641. next = NULL;
  1642. expires = 0;
  1643. if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  1644. scsw_cstat(&irb->scsw) == 0) {
  1645. /* request was completed successfully */
  1646. cqr->status = DASD_CQR_SUCCESS;
  1647. cqr->stopclk = now;
  1648. /* Start first request on queue if possible -> fast_io. */
  1649. if (cqr->devlist.next != &device->ccw_queue) {
  1650. next = list_entry(cqr->devlist.next,
  1651. struct dasd_ccw_req, devlist);
  1652. }
  1653. } else { /* error */
  1654. /* check for HPF error
  1655. * call discipline function to requeue all requests
  1656. * and disable HPF accordingly
  1657. */
  1658. if (cqr->cpmode && dasd_check_hpf_error(irb) &&
  1659. device->discipline->handle_hpf_error)
  1660. device->discipline->handle_hpf_error(device, irb);
  1661. /*
  1662. * If we don't want complex ERP for this request, then just
  1663. * reset this and retry it in the fastpath
  1664. */
  1665. if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
  1666. cqr->retries > 0) {
  1667. if (cqr->lpm == dasd_path_get_opm(device))
  1668. DBF_DEV_EVENT(DBF_DEBUG, device,
  1669. "default ERP in fastpath "
  1670. "(%i retries left)",
  1671. cqr->retries);
  1672. if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
  1673. cqr->lpm = dasd_path_get_opm(device);
  1674. cqr->status = DASD_CQR_QUEUED;
  1675. next = cqr;
  1676. } else
  1677. cqr->status = DASD_CQR_ERROR;
  1678. }
  1679. if (next && (next->status == DASD_CQR_QUEUED) &&
  1680. (!device->stopped)) {
  1681. if (device->discipline->start_IO(next) == 0)
  1682. expires = next->expires;
  1683. }
  1684. if (expires != 0)
  1685. dasd_device_set_timer(device, expires);
  1686. else
  1687. dasd_device_clear_timer(device);
  1688. dasd_schedule_device_bh(device);
  1689. }
  1690. EXPORT_SYMBOL(dasd_int_handler);
  1691. enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
  1692. {
  1693. struct dasd_device *device;
  1694. device = dasd_device_from_cdev_locked(cdev);
  1695. if (IS_ERR(device))
  1696. goto out;
  1697. if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
  1698. device->state != device->target ||
  1699. !device->discipline->check_for_device_change){
  1700. dasd_put_device(device);
  1701. goto out;
  1702. }
  1703. if (device->discipline->dump_sense_dbf)
  1704. device->discipline->dump_sense_dbf(device, irb, "uc");
  1705. device->discipline->check_for_device_change(device, NULL, irb);
  1706. dasd_put_device(device);
  1707. out:
  1708. return UC_TODO_RETRY;
  1709. }
  1710. EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
  1711. /*
  1712. * If we have an error on a dasd_block layer request then we cancel
  1713. * and return all further requests from the same dasd_block as well.
  1714. */
  1715. static void __dasd_device_recovery(struct dasd_device *device,
  1716. struct dasd_ccw_req *ref_cqr)
  1717. {
  1718. struct list_head *l, *n;
  1719. struct dasd_ccw_req *cqr;
  1720. /*
  1721. * only requeue request that came from the dasd_block layer
  1722. */
  1723. if (!ref_cqr->block)
  1724. return;
  1725. list_for_each_safe(l, n, &device->ccw_queue) {
  1726. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1727. if (cqr->status == DASD_CQR_QUEUED &&
  1728. ref_cqr->block == cqr->block) {
  1729. cqr->status = DASD_CQR_CLEARED;
  1730. }
  1731. }
  1732. };
  1733. /*
  1734. * Remove those ccw requests from the queue that need to be returned
  1735. * to the upper layer.
  1736. */
  1737. static void __dasd_device_process_ccw_queue(struct dasd_device *device,
  1738. struct list_head *final_queue)
  1739. {
  1740. struct list_head *l, *n;
  1741. struct dasd_ccw_req *cqr;
  1742. /* Process request with final status. */
  1743. list_for_each_safe(l, n, &device->ccw_queue) {
  1744. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1745. /* Skip any non-final request. */
  1746. if (cqr->status == DASD_CQR_QUEUED ||
  1747. cqr->status == DASD_CQR_IN_IO ||
  1748. cqr->status == DASD_CQR_CLEAR_PENDING)
  1749. continue;
  1750. if (cqr->status == DASD_CQR_ERROR) {
  1751. __dasd_device_recovery(device, cqr);
  1752. }
  1753. /* Rechain finished requests to final queue */
  1754. list_move_tail(&cqr->devlist, final_queue);
  1755. }
  1756. }
  1757. static void __dasd_process_cqr(struct dasd_device *device,
  1758. struct dasd_ccw_req *cqr)
  1759. {
  1760. char errorstring[ERRORLENGTH];
  1761. switch (cqr->status) {
  1762. case DASD_CQR_SUCCESS:
  1763. cqr->status = DASD_CQR_DONE;
  1764. break;
  1765. case DASD_CQR_ERROR:
  1766. cqr->status = DASD_CQR_NEED_ERP;
  1767. break;
  1768. case DASD_CQR_CLEARED:
  1769. cqr->status = DASD_CQR_TERMINATED;
  1770. break;
  1771. default:
  1772. /* internal error 12 - wrong cqr status*/
  1773. snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
  1774. dev_err(&device->cdev->dev,
  1775. "An error occurred in the DASD device driver, "
  1776. "reason=%s\n", errorstring);
  1777. BUG();
  1778. }
  1779. if (cqr->callback)
  1780. cqr->callback(cqr, cqr->callback_data);
  1781. }
  1782. /*
  1783. * the cqrs from the final queue are returned to the upper layer
  1784. * by setting a dasd_block state and calling the callback function
  1785. */
  1786. static void __dasd_device_process_final_queue(struct dasd_device *device,
  1787. struct list_head *final_queue)
  1788. {
  1789. struct list_head *l, *n;
  1790. struct dasd_ccw_req *cqr;
  1791. struct dasd_block *block;
  1792. list_for_each_safe(l, n, final_queue) {
  1793. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1794. list_del_init(&cqr->devlist);
  1795. block = cqr->block;
  1796. if (!block) {
  1797. __dasd_process_cqr(device, cqr);
  1798. } else {
  1799. spin_lock_bh(&block->queue_lock);
  1800. __dasd_process_cqr(device, cqr);
  1801. spin_unlock_bh(&block->queue_lock);
  1802. }
  1803. }
  1804. }
  1805. /*
  1806. * Take a look at the first request on the ccw queue and check
  1807. * if it reached its expire time. If so, terminate the IO.
  1808. */
  1809. static void __dasd_device_check_expire(struct dasd_device *device)
  1810. {
  1811. struct dasd_ccw_req *cqr;
  1812. if (list_empty(&device->ccw_queue))
  1813. return;
  1814. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1815. if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
  1816. (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
  1817. if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
  1818. /*
  1819. * IO in safe offline processing should not
  1820. * run out of retries
  1821. */
  1822. cqr->retries++;
  1823. }
  1824. if (device->discipline->term_IO(cqr) != 0) {
  1825. /* Hmpf, try again in 5 sec */
  1826. dev_err(&device->cdev->dev,
  1827. "cqr %p timed out (%lus) but cannot be "
  1828. "ended, retrying in 5 s\n",
  1829. cqr, (cqr->expires/HZ));
  1830. cqr->expires += 5*HZ;
  1831. dasd_device_set_timer(device, 5*HZ);
  1832. } else {
  1833. dev_err(&device->cdev->dev,
  1834. "cqr %p timed out (%lus), %i retries "
  1835. "remaining\n", cqr, (cqr->expires/HZ),
  1836. cqr->retries);
  1837. }
  1838. }
  1839. }
  1840. /*
  1841. * return 1 when device is not eligible for IO
  1842. */
  1843. static int __dasd_device_is_unusable(struct dasd_device *device,
  1844. struct dasd_ccw_req *cqr)
  1845. {
  1846. int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM | DASD_STOPPED_NOSPC);
  1847. if (test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
  1848. !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
  1849. /*
  1850. * dasd is being set offline
  1851. * but it is no safe offline where we have to allow I/O
  1852. */
  1853. return 1;
  1854. }
  1855. if (device->stopped) {
  1856. if (device->stopped & mask) {
  1857. /* stopped and CQR will not change that. */
  1858. return 1;
  1859. }
  1860. if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
  1861. /* CQR is not able to change device to
  1862. * operational. */
  1863. return 1;
  1864. }
  1865. /* CQR required to get device operational. */
  1866. }
  1867. return 0;
  1868. }
  1869. /*
  1870. * Take a look at the first request on the ccw queue and check
  1871. * if it needs to be started.
  1872. */
  1873. static void __dasd_device_start_head(struct dasd_device *device)
  1874. {
  1875. struct dasd_ccw_req *cqr;
  1876. int rc;
  1877. if (list_empty(&device->ccw_queue))
  1878. return;
  1879. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1880. if (cqr->status != DASD_CQR_QUEUED)
  1881. return;
  1882. /* if device is not usable return request to upper layer */
  1883. if (__dasd_device_is_unusable(device, cqr)) {
  1884. cqr->intrc = -EAGAIN;
  1885. cqr->status = DASD_CQR_CLEARED;
  1886. dasd_schedule_device_bh(device);
  1887. return;
  1888. }
  1889. rc = device->discipline->start_IO(cqr);
  1890. if (rc == 0)
  1891. dasd_device_set_timer(device, cqr->expires);
  1892. else if (rc == -EACCES) {
  1893. dasd_schedule_device_bh(device);
  1894. } else
  1895. /* Hmpf, try again in 1/2 sec */
  1896. dasd_device_set_timer(device, 50);
  1897. }
  1898. static void __dasd_device_check_path_events(struct dasd_device *device)
  1899. {
  1900. int rc;
  1901. if (!dasd_path_get_tbvpm(device))
  1902. return;
  1903. if (device->stopped &
  1904. ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
  1905. return;
  1906. rc = device->discipline->verify_path(device,
  1907. dasd_path_get_tbvpm(device));
  1908. if (rc)
  1909. dasd_device_set_timer(device, 50);
  1910. else
  1911. dasd_path_clear_all_verify(device);
  1912. };
  1913. /*
  1914. * Go through all request on the dasd_device request queue,
  1915. * terminate them on the cdev if necessary, and return them to the
  1916. * submitting layer via callback.
  1917. * Note:
  1918. * Make sure that all 'submitting layers' still exist when
  1919. * this function is called!. In other words, when 'device' is a base
  1920. * device then all block layer requests must have been removed before
  1921. * via dasd_flush_block_queue.
  1922. */
  1923. int dasd_flush_device_queue(struct dasd_device *device)
  1924. {
  1925. struct dasd_ccw_req *cqr, *n;
  1926. int rc;
  1927. struct list_head flush_queue;
  1928. INIT_LIST_HEAD(&flush_queue);
  1929. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1930. rc = 0;
  1931. list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
  1932. /* Check status and move request to flush_queue */
  1933. switch (cqr->status) {
  1934. case DASD_CQR_IN_IO:
  1935. rc = device->discipline->term_IO(cqr);
  1936. if (rc) {
  1937. /* unable to terminate requeust */
  1938. dev_err(&device->cdev->dev,
  1939. "Flushing the DASD request queue "
  1940. "failed for request %p\n", cqr);
  1941. /* stop flush processing */
  1942. goto finished;
  1943. }
  1944. break;
  1945. case DASD_CQR_QUEUED:
  1946. cqr->stopclk = get_tod_clock();
  1947. cqr->status = DASD_CQR_CLEARED;
  1948. break;
  1949. default: /* no need to modify the others */
  1950. break;
  1951. }
  1952. list_move_tail(&cqr->devlist, &flush_queue);
  1953. }
  1954. finished:
  1955. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1956. /*
  1957. * After this point all requests must be in state CLEAR_PENDING,
  1958. * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
  1959. * one of the others.
  1960. */
  1961. list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
  1962. wait_event(dasd_flush_wq,
  1963. (cqr->status != DASD_CQR_CLEAR_PENDING));
  1964. /*
  1965. * Now set each request back to TERMINATED, DONE or NEED_ERP
  1966. * and call the callback function of flushed requests
  1967. */
  1968. __dasd_device_process_final_queue(device, &flush_queue);
  1969. return rc;
  1970. }
  1971. EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
  1972. /*
  1973. * Acquire the device lock and process queues for the device.
  1974. */
  1975. static void dasd_device_tasklet(unsigned long data)
  1976. {
  1977. struct dasd_device *device = (struct dasd_device *) data;
  1978. struct list_head final_queue;
  1979. atomic_set (&device->tasklet_scheduled, 0);
  1980. INIT_LIST_HEAD(&final_queue);
  1981. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1982. /* Check expire time of first request on the ccw queue. */
  1983. __dasd_device_check_expire(device);
  1984. /* find final requests on ccw queue */
  1985. __dasd_device_process_ccw_queue(device, &final_queue);
  1986. __dasd_device_check_path_events(device);
  1987. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1988. /* Now call the callback function of requests with final status */
  1989. __dasd_device_process_final_queue(device, &final_queue);
  1990. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1991. /* Now check if the head of the ccw queue needs to be started. */
  1992. __dasd_device_start_head(device);
  1993. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1994. if (waitqueue_active(&shutdown_waitq))
  1995. wake_up(&shutdown_waitq);
  1996. dasd_put_device(device);
  1997. }
  1998. /*
  1999. * Schedules a call to dasd_tasklet over the device tasklet.
  2000. */
  2001. void dasd_schedule_device_bh(struct dasd_device *device)
  2002. {
  2003. /* Protect against rescheduling. */
  2004. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  2005. return;
  2006. dasd_get_device(device);
  2007. tasklet_hi_schedule(&device->tasklet);
  2008. }
  2009. EXPORT_SYMBOL(dasd_schedule_device_bh);
  2010. void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
  2011. {
  2012. device->stopped |= bits;
  2013. }
  2014. EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
  2015. void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
  2016. {
  2017. device->stopped &= ~bits;
  2018. if (!device->stopped)
  2019. wake_up(&generic_waitq);
  2020. }
  2021. EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
  2022. /*
  2023. * Queue a request to the head of the device ccw_queue.
  2024. * Start the I/O if possible.
  2025. */
  2026. void dasd_add_request_head(struct dasd_ccw_req *cqr)
  2027. {
  2028. struct dasd_device *device;
  2029. unsigned long flags;
  2030. device = cqr->startdev;
  2031. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  2032. cqr->status = DASD_CQR_QUEUED;
  2033. list_add(&cqr->devlist, &device->ccw_queue);
  2034. /* let the bh start the request to keep them in order */
  2035. dasd_schedule_device_bh(device);
  2036. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  2037. }
  2038. EXPORT_SYMBOL(dasd_add_request_head);
  2039. /*
  2040. * Queue a request to the tail of the device ccw_queue.
  2041. * Start the I/O if possible.
  2042. */
  2043. void dasd_add_request_tail(struct dasd_ccw_req *cqr)
  2044. {
  2045. struct dasd_device *device;
  2046. unsigned long flags;
  2047. device = cqr->startdev;
  2048. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  2049. cqr->status = DASD_CQR_QUEUED;
  2050. list_add_tail(&cqr->devlist, &device->ccw_queue);
  2051. /* let the bh start the request to keep them in order */
  2052. dasd_schedule_device_bh(device);
  2053. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  2054. }
  2055. EXPORT_SYMBOL(dasd_add_request_tail);
  2056. /*
  2057. * Wakeup helper for the 'sleep_on' functions.
  2058. */
  2059. void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  2060. {
  2061. spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
  2062. cqr->callback_data = DASD_SLEEPON_END_TAG;
  2063. spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
  2064. wake_up(&generic_waitq);
  2065. }
  2066. EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
  2067. static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
  2068. {
  2069. struct dasd_device *device;
  2070. int rc;
  2071. device = cqr->startdev;
  2072. spin_lock_irq(get_ccwdev_lock(device->cdev));
  2073. rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
  2074. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  2075. return rc;
  2076. }
  2077. /*
  2078. * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
  2079. */
  2080. static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
  2081. {
  2082. struct dasd_device *device;
  2083. dasd_erp_fn_t erp_fn;
  2084. if (cqr->status == DASD_CQR_FILLED)
  2085. return 0;
  2086. device = cqr->startdev;
  2087. if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
  2088. if (cqr->status == DASD_CQR_TERMINATED) {
  2089. device->discipline->handle_terminated_request(cqr);
  2090. return 1;
  2091. }
  2092. if (cqr->status == DASD_CQR_NEED_ERP) {
  2093. erp_fn = device->discipline->erp_action(cqr);
  2094. erp_fn(cqr);
  2095. return 1;
  2096. }
  2097. if (cqr->status == DASD_CQR_FAILED)
  2098. dasd_log_sense(cqr, &cqr->irb);
  2099. if (cqr->refers) {
  2100. __dasd_process_erp(device, cqr);
  2101. return 1;
  2102. }
  2103. }
  2104. return 0;
  2105. }
  2106. static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
  2107. {
  2108. if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
  2109. if (cqr->refers) /* erp is not done yet */
  2110. return 1;
  2111. return ((cqr->status != DASD_CQR_DONE) &&
  2112. (cqr->status != DASD_CQR_FAILED));
  2113. } else
  2114. return (cqr->status == DASD_CQR_FILLED);
  2115. }
  2116. static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
  2117. {
  2118. struct dasd_device *device;
  2119. int rc;
  2120. struct list_head ccw_queue;
  2121. struct dasd_ccw_req *cqr;
  2122. INIT_LIST_HEAD(&ccw_queue);
  2123. maincqr->status = DASD_CQR_FILLED;
  2124. device = maincqr->startdev;
  2125. list_add(&maincqr->blocklist, &ccw_queue);
  2126. for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
  2127. cqr = list_first_entry(&ccw_queue,
  2128. struct dasd_ccw_req, blocklist)) {
  2129. if (__dasd_sleep_on_erp(cqr))
  2130. continue;
  2131. if (cqr->status != DASD_CQR_FILLED) /* could be failed */
  2132. continue;
  2133. if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
  2134. !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
  2135. cqr->status = DASD_CQR_FAILED;
  2136. cqr->intrc = -EPERM;
  2137. continue;
  2138. }
  2139. /* Non-temporary stop condition will trigger fail fast */
  2140. if (device->stopped & ~DASD_STOPPED_PENDING &&
  2141. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  2142. (!dasd_eer_enabled(device))) {
  2143. cqr->status = DASD_CQR_FAILED;
  2144. cqr->intrc = -ENOLINK;
  2145. continue;
  2146. }
  2147. /*
  2148. * Don't try to start requests if device is in
  2149. * offline processing, it might wait forever
  2150. */
  2151. if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  2152. cqr->status = DASD_CQR_FAILED;
  2153. cqr->intrc = -ENODEV;
  2154. continue;
  2155. }
  2156. /*
  2157. * Don't try to start requests if device is stopped
  2158. * except path verification requests
  2159. */
  2160. if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
  2161. if (interruptible) {
  2162. rc = wait_event_interruptible(
  2163. generic_waitq, !(device->stopped));
  2164. if (rc == -ERESTARTSYS) {
  2165. cqr->status = DASD_CQR_FAILED;
  2166. maincqr->intrc = rc;
  2167. continue;
  2168. }
  2169. } else
  2170. wait_event(generic_waitq, !(device->stopped));
  2171. }
  2172. if (!cqr->callback)
  2173. cqr->callback = dasd_wakeup_cb;
  2174. cqr->callback_data = DASD_SLEEPON_START_TAG;
  2175. dasd_add_request_tail(cqr);
  2176. if (interruptible) {
  2177. rc = wait_event_interruptible(
  2178. generic_waitq, _wait_for_wakeup(cqr));
  2179. if (rc == -ERESTARTSYS) {
  2180. dasd_cancel_req(cqr);
  2181. /* wait (non-interruptible) for final status */
  2182. wait_event(generic_waitq,
  2183. _wait_for_wakeup(cqr));
  2184. cqr->status = DASD_CQR_FAILED;
  2185. maincqr->intrc = rc;
  2186. continue;
  2187. }
  2188. } else
  2189. wait_event(generic_waitq, _wait_for_wakeup(cqr));
  2190. }
  2191. maincqr->endclk = get_tod_clock();
  2192. if ((maincqr->status != DASD_CQR_DONE) &&
  2193. (maincqr->intrc != -ERESTARTSYS))
  2194. dasd_log_sense(maincqr, &maincqr->irb);
  2195. if (maincqr->status == DASD_CQR_DONE)
  2196. rc = 0;
  2197. else if (maincqr->intrc)
  2198. rc = maincqr->intrc;
  2199. else
  2200. rc = -EIO;
  2201. return rc;
  2202. }
  2203. static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
  2204. {
  2205. struct dasd_ccw_req *cqr;
  2206. list_for_each_entry(cqr, ccw_queue, blocklist) {
  2207. if (cqr->callback_data != DASD_SLEEPON_END_TAG)
  2208. return 0;
  2209. }
  2210. return 1;
  2211. }
  2212. static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
  2213. {
  2214. struct dasd_device *device;
  2215. struct dasd_ccw_req *cqr, *n;
  2216. u8 *sense = NULL;
  2217. int rc;
  2218. retry:
  2219. list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
  2220. device = cqr->startdev;
  2221. if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
  2222. continue;
  2223. if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
  2224. !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
  2225. cqr->status = DASD_CQR_FAILED;
  2226. cqr->intrc = -EPERM;
  2227. continue;
  2228. }
  2229. /*Non-temporary stop condition will trigger fail fast*/
  2230. if (device->stopped & ~DASD_STOPPED_PENDING &&
  2231. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  2232. !dasd_eer_enabled(device)) {
  2233. cqr->status = DASD_CQR_FAILED;
  2234. cqr->intrc = -EAGAIN;
  2235. continue;
  2236. }
  2237. /*Don't try to start requests if device is stopped*/
  2238. if (interruptible) {
  2239. rc = wait_event_interruptible(
  2240. generic_waitq, !device->stopped);
  2241. if (rc == -ERESTARTSYS) {
  2242. cqr->status = DASD_CQR_FAILED;
  2243. cqr->intrc = rc;
  2244. continue;
  2245. }
  2246. } else
  2247. wait_event(generic_waitq, !(device->stopped));
  2248. if (!cqr->callback)
  2249. cqr->callback = dasd_wakeup_cb;
  2250. cqr->callback_data = DASD_SLEEPON_START_TAG;
  2251. dasd_add_request_tail(cqr);
  2252. }
  2253. wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
  2254. rc = 0;
  2255. list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
  2256. /*
  2257. * In some cases the 'File Protected' or 'Incorrect Length'
  2258. * error might be expected and error recovery would be
  2259. * unnecessary in these cases. Check if the according suppress
  2260. * bit is set.
  2261. */
  2262. sense = dasd_get_sense(&cqr->irb);
  2263. if (sense && sense[1] & SNS1_FILE_PROTECTED &&
  2264. test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
  2265. continue;
  2266. if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
  2267. test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
  2268. continue;
  2269. /*
  2270. * for alias devices simplify error recovery and
  2271. * return to upper layer
  2272. * do not skip ERP requests
  2273. */
  2274. if (cqr->startdev != cqr->basedev && !cqr->refers &&
  2275. (cqr->status == DASD_CQR_TERMINATED ||
  2276. cqr->status == DASD_CQR_NEED_ERP))
  2277. return -EAGAIN;
  2278. /* normal recovery for basedev IO */
  2279. if (__dasd_sleep_on_erp(cqr))
  2280. /* handle erp first */
  2281. goto retry;
  2282. }
  2283. return 0;
  2284. }
  2285. /*
  2286. * Queue a request to the tail of the device ccw_queue and wait for
  2287. * it's completion.
  2288. */
  2289. int dasd_sleep_on(struct dasd_ccw_req *cqr)
  2290. {
  2291. return _dasd_sleep_on(cqr, 0);
  2292. }
  2293. EXPORT_SYMBOL(dasd_sleep_on);
  2294. /*
  2295. * Start requests from a ccw_queue and wait for their completion.
  2296. */
  2297. int dasd_sleep_on_queue(struct list_head *ccw_queue)
  2298. {
  2299. return _dasd_sleep_on_queue(ccw_queue, 0);
  2300. }
  2301. EXPORT_SYMBOL(dasd_sleep_on_queue);
  2302. /*
  2303. * Start requests from a ccw_queue and wait interruptible for their completion.
  2304. */
  2305. int dasd_sleep_on_queue_interruptible(struct list_head *ccw_queue)
  2306. {
  2307. return _dasd_sleep_on_queue(ccw_queue, 1);
  2308. }
  2309. EXPORT_SYMBOL(dasd_sleep_on_queue_interruptible);
  2310. /*
  2311. * Queue a request to the tail of the device ccw_queue and wait
  2312. * interruptible for it's completion.
  2313. */
  2314. int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
  2315. {
  2316. return _dasd_sleep_on(cqr, 1);
  2317. }
  2318. EXPORT_SYMBOL(dasd_sleep_on_interruptible);
  2319. /*
  2320. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  2321. * for eckd devices) the currently running request has to be terminated
  2322. * and be put back to status queued, before the special request is added
  2323. * to the head of the queue. Then the special request is waited on normally.
  2324. */
  2325. static inline int _dasd_term_running_cqr(struct dasd_device *device)
  2326. {
  2327. struct dasd_ccw_req *cqr;
  2328. int rc;
  2329. if (list_empty(&device->ccw_queue))
  2330. return 0;
  2331. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  2332. rc = device->discipline->term_IO(cqr);
  2333. if (!rc)
  2334. /*
  2335. * CQR terminated because a more important request is pending.
  2336. * Undo decreasing of retry counter because this is
  2337. * not an error case.
  2338. */
  2339. cqr->retries++;
  2340. return rc;
  2341. }
  2342. int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
  2343. {
  2344. struct dasd_device *device;
  2345. int rc;
  2346. device = cqr->startdev;
  2347. if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
  2348. !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
  2349. cqr->status = DASD_CQR_FAILED;
  2350. cqr->intrc = -EPERM;
  2351. return -EIO;
  2352. }
  2353. spin_lock_irq(get_ccwdev_lock(device->cdev));
  2354. rc = _dasd_term_running_cqr(device);
  2355. if (rc) {
  2356. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  2357. return rc;
  2358. }
  2359. cqr->callback = dasd_wakeup_cb;
  2360. cqr->callback_data = DASD_SLEEPON_START_TAG;
  2361. cqr->status = DASD_CQR_QUEUED;
  2362. /*
  2363. * add new request as second
  2364. * first the terminated cqr needs to be finished
  2365. */
  2366. list_add(&cqr->devlist, device->ccw_queue.next);
  2367. /* let the bh start the request to keep them in order */
  2368. dasd_schedule_device_bh(device);
  2369. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  2370. wait_event(generic_waitq, _wait_for_wakeup(cqr));
  2371. if (cqr->status == DASD_CQR_DONE)
  2372. rc = 0;
  2373. else if (cqr->intrc)
  2374. rc = cqr->intrc;
  2375. else
  2376. rc = -EIO;
  2377. /* kick tasklets */
  2378. dasd_schedule_device_bh(device);
  2379. if (device->block)
  2380. dasd_schedule_block_bh(device->block);
  2381. return rc;
  2382. }
  2383. EXPORT_SYMBOL(dasd_sleep_on_immediatly);
  2384. /*
  2385. * Cancels a request that was started with dasd_sleep_on_req.
  2386. * This is useful to timeout requests. The request will be
  2387. * terminated if it is currently in i/o.
  2388. * Returns 0 if request termination was successful
  2389. * negative error code if termination failed
  2390. * Cancellation of a request is an asynchronous operation! The calling
  2391. * function has to wait until the request is properly returned via callback.
  2392. */
  2393. static int __dasd_cancel_req(struct dasd_ccw_req *cqr)
  2394. {
  2395. struct dasd_device *device = cqr->startdev;
  2396. int rc = 0;
  2397. switch (cqr->status) {
  2398. case DASD_CQR_QUEUED:
  2399. /* request was not started - just set to cleared */
  2400. cqr->status = DASD_CQR_CLEARED;
  2401. break;
  2402. case DASD_CQR_IN_IO:
  2403. /* request in IO - terminate IO and release again */
  2404. rc = device->discipline->term_IO(cqr);
  2405. if (rc) {
  2406. dev_err(&device->cdev->dev,
  2407. "Cancelling request %p failed with rc=%d\n",
  2408. cqr, rc);
  2409. } else {
  2410. cqr->stopclk = get_tod_clock();
  2411. }
  2412. break;
  2413. default: /* already finished or clear pending - do nothing */
  2414. break;
  2415. }
  2416. dasd_schedule_device_bh(device);
  2417. return rc;
  2418. }
  2419. int dasd_cancel_req(struct dasd_ccw_req *cqr)
  2420. {
  2421. struct dasd_device *device = cqr->startdev;
  2422. unsigned long flags;
  2423. int rc;
  2424. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  2425. rc = __dasd_cancel_req(cqr);
  2426. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  2427. return rc;
  2428. }
  2429. /*
  2430. * SECTION: Operations of the dasd_block layer.
  2431. */
  2432. /*
  2433. * Timeout function for dasd_block. This is used when the block layer
  2434. * is waiting for something that may not come reliably, (e.g. a state
  2435. * change interrupt)
  2436. */
  2437. static void dasd_block_timeout(struct timer_list *t)
  2438. {
  2439. unsigned long flags;
  2440. struct dasd_block *block;
  2441. block = from_timer(block, t, timer);
  2442. spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
  2443. /* re-activate request queue */
  2444. dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
  2445. spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
  2446. dasd_schedule_block_bh(block);
  2447. blk_mq_run_hw_queues(block->request_queue, true);
  2448. }
  2449. /*
  2450. * Setup timeout for a dasd_block in jiffies.
  2451. */
  2452. void dasd_block_set_timer(struct dasd_block *block, int expires)
  2453. {
  2454. if (expires == 0)
  2455. del_timer(&block->timer);
  2456. else
  2457. mod_timer(&block->timer, jiffies + expires);
  2458. }
  2459. EXPORT_SYMBOL(dasd_block_set_timer);
  2460. /*
  2461. * Clear timeout for a dasd_block.
  2462. */
  2463. void dasd_block_clear_timer(struct dasd_block *block)
  2464. {
  2465. del_timer(&block->timer);
  2466. }
  2467. EXPORT_SYMBOL(dasd_block_clear_timer);
  2468. /*
  2469. * Process finished error recovery ccw.
  2470. */
  2471. static void __dasd_process_erp(struct dasd_device *device,
  2472. struct dasd_ccw_req *cqr)
  2473. {
  2474. dasd_erp_fn_t erp_fn;
  2475. if (cqr->status == DASD_CQR_DONE)
  2476. DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
  2477. else
  2478. dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
  2479. erp_fn = device->discipline->erp_postaction(cqr);
  2480. erp_fn(cqr);
  2481. }
  2482. static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
  2483. {
  2484. struct request *req;
  2485. blk_status_t error = BLK_STS_OK;
  2486. unsigned int proc_bytes;
  2487. int status;
  2488. req = (struct request *) cqr->callback_data;
  2489. dasd_profile_end(cqr->block, cqr, req);
  2490. proc_bytes = cqr->proc_bytes;
  2491. status = cqr->block->base->discipline->free_cp(cqr, req);
  2492. if (status < 0)
  2493. error = errno_to_blk_status(status);
  2494. else if (status == 0) {
  2495. switch (cqr->intrc) {
  2496. case -EPERM:
  2497. error = BLK_STS_NEXUS;
  2498. break;
  2499. case -ENOLINK:
  2500. error = BLK_STS_TRANSPORT;
  2501. break;
  2502. case -ETIMEDOUT:
  2503. error = BLK_STS_TIMEOUT;
  2504. break;
  2505. default:
  2506. error = BLK_STS_IOERR;
  2507. break;
  2508. }
  2509. }
  2510. /*
  2511. * We need to take care for ETIMEDOUT errors here since the
  2512. * complete callback does not get called in this case.
  2513. * Take care of all errors here and avoid additional code to
  2514. * transfer the error value to the complete callback.
  2515. */
  2516. if (error) {
  2517. blk_mq_end_request(req, error);
  2518. blk_mq_run_hw_queues(req->q, true);
  2519. } else {
  2520. /*
  2521. * Partial completed requests can happen with ESE devices.
  2522. * During read we might have gotten a NRF error and have to
  2523. * complete a request partially.
  2524. */
  2525. if (proc_bytes) {
  2526. blk_update_request(req, BLK_STS_OK,
  2527. blk_rq_bytes(req) - proc_bytes);
  2528. blk_mq_requeue_request(req, true);
  2529. } else if (likely(!blk_should_fake_timeout(req->q))) {
  2530. blk_mq_complete_request(req);
  2531. }
  2532. }
  2533. }
  2534. /*
  2535. * Process ccw request queue.
  2536. */
  2537. static void __dasd_process_block_ccw_queue(struct dasd_block *block,
  2538. struct list_head *final_queue)
  2539. {
  2540. struct list_head *l, *n;
  2541. struct dasd_ccw_req *cqr;
  2542. dasd_erp_fn_t erp_fn;
  2543. unsigned long flags;
  2544. struct dasd_device *base = block->base;
  2545. restart:
  2546. /* Process request with final status. */
  2547. list_for_each_safe(l, n, &block->ccw_queue) {
  2548. cqr = list_entry(l, struct dasd_ccw_req, blocklist);
  2549. if (cqr->status != DASD_CQR_DONE &&
  2550. cqr->status != DASD_CQR_FAILED &&
  2551. cqr->status != DASD_CQR_NEED_ERP &&
  2552. cqr->status != DASD_CQR_TERMINATED)
  2553. continue;
  2554. if (cqr->status == DASD_CQR_TERMINATED) {
  2555. base->discipline->handle_terminated_request(cqr);
  2556. goto restart;
  2557. }
  2558. /* Process requests that may be recovered */
  2559. if (cqr->status == DASD_CQR_NEED_ERP) {
  2560. erp_fn = base->discipline->erp_action(cqr);
  2561. if (IS_ERR(erp_fn(cqr)))
  2562. continue;
  2563. goto restart;
  2564. }
  2565. /* log sense for fatal error */
  2566. if (cqr->status == DASD_CQR_FAILED) {
  2567. dasd_log_sense(cqr, &cqr->irb);
  2568. }
  2569. /* First of all call extended error reporting. */
  2570. if (dasd_eer_enabled(base) &&
  2571. cqr->status == DASD_CQR_FAILED) {
  2572. dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
  2573. /* restart request */
  2574. cqr->status = DASD_CQR_FILLED;
  2575. cqr->retries = 255;
  2576. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  2577. dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
  2578. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
  2579. flags);
  2580. goto restart;
  2581. }
  2582. /* Process finished ERP request. */
  2583. if (cqr->refers) {
  2584. __dasd_process_erp(base, cqr);
  2585. goto restart;
  2586. }
  2587. /* Rechain finished requests to final queue */
  2588. cqr->endclk = get_tod_clock();
  2589. list_move_tail(&cqr->blocklist, final_queue);
  2590. }
  2591. }
  2592. static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
  2593. {
  2594. dasd_schedule_block_bh(cqr->block);
  2595. }
  2596. static void __dasd_block_start_head(struct dasd_block *block)
  2597. {
  2598. struct dasd_ccw_req *cqr;
  2599. if (list_empty(&block->ccw_queue))
  2600. return;
  2601. /* We allways begin with the first requests on the queue, as some
  2602. * of previously started requests have to be enqueued on a
  2603. * dasd_device again for error recovery.
  2604. */
  2605. list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
  2606. if (cqr->status != DASD_CQR_FILLED)
  2607. continue;
  2608. if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
  2609. !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
  2610. cqr->status = DASD_CQR_FAILED;
  2611. cqr->intrc = -EPERM;
  2612. dasd_schedule_block_bh(block);
  2613. continue;
  2614. }
  2615. /* Non-temporary stop condition will trigger fail fast */
  2616. if (block->base->stopped & ~DASD_STOPPED_PENDING &&
  2617. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  2618. (!dasd_eer_enabled(block->base))) {
  2619. cqr->status = DASD_CQR_FAILED;
  2620. cqr->intrc = -ENOLINK;
  2621. dasd_schedule_block_bh(block);
  2622. continue;
  2623. }
  2624. /* Don't try to start requests if device is stopped */
  2625. if (block->base->stopped)
  2626. return;
  2627. /* just a fail safe check, should not happen */
  2628. if (!cqr->startdev)
  2629. cqr->startdev = block->base;
  2630. /* make sure that the requests we submit find their way back */
  2631. cqr->callback = dasd_return_cqr_cb;
  2632. dasd_add_request_tail(cqr);
  2633. }
  2634. }
  2635. /*
  2636. * Central dasd_block layer routine. Takes requests from the generic
  2637. * block layer request queue, creates ccw requests, enqueues them on
  2638. * a dasd_device and processes ccw requests that have been returned.
  2639. */
  2640. static void dasd_block_tasklet(unsigned long data)
  2641. {
  2642. struct dasd_block *block = (struct dasd_block *) data;
  2643. struct list_head final_queue;
  2644. struct list_head *l, *n;
  2645. struct dasd_ccw_req *cqr;
  2646. struct dasd_queue *dq;
  2647. atomic_set(&block->tasklet_scheduled, 0);
  2648. INIT_LIST_HEAD(&final_queue);
  2649. spin_lock_irq(&block->queue_lock);
  2650. /* Finish off requests on ccw queue */
  2651. __dasd_process_block_ccw_queue(block, &final_queue);
  2652. spin_unlock_irq(&block->queue_lock);
  2653. /* Now call the callback function of requests with final status */
  2654. list_for_each_safe(l, n, &final_queue) {
  2655. cqr = list_entry(l, struct dasd_ccw_req, blocklist);
  2656. dq = cqr->dq;
  2657. spin_lock_irq(&dq->lock);
  2658. list_del_init(&cqr->blocklist);
  2659. __dasd_cleanup_cqr(cqr);
  2660. spin_unlock_irq(&dq->lock);
  2661. }
  2662. spin_lock_irq(&block->queue_lock);
  2663. /* Now check if the head of the ccw queue needs to be started. */
  2664. __dasd_block_start_head(block);
  2665. spin_unlock_irq(&block->queue_lock);
  2666. if (waitqueue_active(&shutdown_waitq))
  2667. wake_up(&shutdown_waitq);
  2668. dasd_put_device(block->base);
  2669. }
  2670. static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
  2671. {
  2672. wake_up(&dasd_flush_wq);
  2673. }
  2674. /*
  2675. * Requeue a request back to the block request queue
  2676. * only works for block requests
  2677. */
  2678. static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
  2679. {
  2680. struct dasd_block *block = cqr->block;
  2681. struct request *req;
  2682. if (!block)
  2683. return -EINVAL;
  2684. /*
  2685. * If the request is an ERP request there is nothing to requeue.
  2686. * This will be done with the remaining original request.
  2687. */
  2688. if (cqr->refers)
  2689. return 0;
  2690. spin_lock_irq(&cqr->dq->lock);
  2691. req = (struct request *) cqr->callback_data;
  2692. blk_mq_requeue_request(req, false);
  2693. spin_unlock_irq(&cqr->dq->lock);
  2694. return 0;
  2695. }
  2696. /*
  2697. * Go through all request on the dasd_block request queue, cancel them
  2698. * on the respective dasd_device, and return them to the generic
  2699. * block layer.
  2700. */
  2701. static int dasd_flush_block_queue(struct dasd_block *block)
  2702. {
  2703. struct dasd_ccw_req *cqr, *n;
  2704. int rc, i;
  2705. struct list_head flush_queue;
  2706. unsigned long flags;
  2707. INIT_LIST_HEAD(&flush_queue);
  2708. spin_lock_bh(&block->queue_lock);
  2709. rc = 0;
  2710. restart:
  2711. list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
  2712. /* if this request currently owned by a dasd_device cancel it */
  2713. if (cqr->status >= DASD_CQR_QUEUED)
  2714. rc = dasd_cancel_req(cqr);
  2715. if (rc < 0)
  2716. break;
  2717. /* Rechain request (including erp chain) so it won't be
  2718. * touched by the dasd_block_tasklet anymore.
  2719. * Replace the callback so we notice when the request
  2720. * is returned from the dasd_device layer.
  2721. */
  2722. cqr->callback = _dasd_wake_block_flush_cb;
  2723. for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
  2724. list_move_tail(&cqr->blocklist, &flush_queue);
  2725. if (i > 1)
  2726. /* moved more than one request - need to restart */
  2727. goto restart;
  2728. }
  2729. spin_unlock_bh(&block->queue_lock);
  2730. /* Now call the callback function of flushed requests */
  2731. restart_cb:
  2732. list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
  2733. wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
  2734. /* Process finished ERP request. */
  2735. if (cqr->refers) {
  2736. spin_lock_bh(&block->queue_lock);
  2737. __dasd_process_erp(block->base, cqr);
  2738. spin_unlock_bh(&block->queue_lock);
  2739. /* restart list_for_xx loop since dasd_process_erp
  2740. * might remove multiple elements */
  2741. goto restart_cb;
  2742. }
  2743. /* call the callback function */
  2744. spin_lock_irqsave(&cqr->dq->lock, flags);
  2745. cqr->endclk = get_tod_clock();
  2746. list_del_init(&cqr->blocklist);
  2747. __dasd_cleanup_cqr(cqr);
  2748. spin_unlock_irqrestore(&cqr->dq->lock, flags);
  2749. }
  2750. return rc;
  2751. }
  2752. /*
  2753. * Schedules a call to dasd_tasklet over the device tasklet.
  2754. */
  2755. void dasd_schedule_block_bh(struct dasd_block *block)
  2756. {
  2757. /* Protect against rescheduling. */
  2758. if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
  2759. return;
  2760. /* life cycle of block is bound to it's base device */
  2761. dasd_get_device(block->base);
  2762. tasklet_hi_schedule(&block->tasklet);
  2763. }
  2764. EXPORT_SYMBOL(dasd_schedule_block_bh);
  2765. /*
  2766. * SECTION: external block device operations
  2767. * (request queue handling, open, release, etc.)
  2768. */
  2769. /*
  2770. * Dasd request queue function. Called from ll_rw_blk.c
  2771. */
  2772. static blk_status_t do_dasd_request(struct blk_mq_hw_ctx *hctx,
  2773. const struct blk_mq_queue_data *qd)
  2774. {
  2775. struct dasd_block *block = hctx->queue->queuedata;
  2776. struct dasd_queue *dq = hctx->driver_data;
  2777. struct request *req = qd->rq;
  2778. struct dasd_device *basedev;
  2779. struct dasd_ccw_req *cqr;
  2780. blk_status_t rc = BLK_STS_OK;
  2781. basedev = block->base;
  2782. spin_lock_irq(&dq->lock);
  2783. if (basedev->state < DASD_STATE_READY ||
  2784. test_bit(DASD_FLAG_OFFLINE, &basedev->flags)) {
  2785. DBF_DEV_EVENT(DBF_ERR, basedev,
  2786. "device not ready for request %p", req);
  2787. rc = BLK_STS_IOERR;
  2788. goto out;
  2789. }
  2790. /*
  2791. * if device is stopped do not fetch new requests
  2792. * except failfast is active which will let requests fail
  2793. * immediately in __dasd_block_start_head()
  2794. */
  2795. if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST)) {
  2796. DBF_DEV_EVENT(DBF_ERR, basedev,
  2797. "device stopped request %p", req);
  2798. rc = BLK_STS_RESOURCE;
  2799. goto out;
  2800. }
  2801. if (basedev->features & DASD_FEATURE_READONLY &&
  2802. rq_data_dir(req) == WRITE) {
  2803. DBF_DEV_EVENT(DBF_ERR, basedev,
  2804. "Rejecting write request %p", req);
  2805. rc = BLK_STS_IOERR;
  2806. goto out;
  2807. }
  2808. if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
  2809. (basedev->features & DASD_FEATURE_FAILFAST ||
  2810. blk_noretry_request(req))) {
  2811. DBF_DEV_EVENT(DBF_ERR, basedev,
  2812. "Rejecting failfast request %p", req);
  2813. rc = BLK_STS_IOERR;
  2814. goto out;
  2815. }
  2816. cqr = basedev->discipline->build_cp(basedev, block, req);
  2817. if (IS_ERR(cqr)) {
  2818. if (PTR_ERR(cqr) == -EBUSY ||
  2819. PTR_ERR(cqr) == -ENOMEM ||
  2820. PTR_ERR(cqr) == -EAGAIN) {
  2821. rc = BLK_STS_RESOURCE;
  2822. goto out;
  2823. }
  2824. DBF_DEV_EVENT(DBF_ERR, basedev,
  2825. "CCW creation failed (rc=%ld) on request %p",
  2826. PTR_ERR(cqr), req);
  2827. rc = BLK_STS_IOERR;
  2828. goto out;
  2829. }
  2830. /*
  2831. * Note: callback is set to dasd_return_cqr_cb in
  2832. * __dasd_block_start_head to cover erp requests as well
  2833. */
  2834. cqr->callback_data = req;
  2835. cqr->status = DASD_CQR_FILLED;
  2836. cqr->dq = dq;
  2837. blk_mq_start_request(req);
  2838. spin_lock(&block->queue_lock);
  2839. list_add_tail(&cqr->blocklist, &block->ccw_queue);
  2840. INIT_LIST_HEAD(&cqr->devlist);
  2841. dasd_profile_start(block, cqr, req);
  2842. dasd_schedule_block_bh(block);
  2843. spin_unlock(&block->queue_lock);
  2844. out:
  2845. spin_unlock_irq(&dq->lock);
  2846. return rc;
  2847. }
  2848. /*
  2849. * Block timeout callback, called from the block layer
  2850. *
  2851. * Return values:
  2852. * BLK_EH_RESET_TIMER if the request should be left running
  2853. * BLK_EH_DONE if the request is handled or terminated
  2854. * by the driver.
  2855. */
  2856. enum blk_eh_timer_return dasd_times_out(struct request *req, bool reserved)
  2857. {
  2858. struct dasd_block *block = req->q->queuedata;
  2859. struct dasd_device *device;
  2860. struct dasd_ccw_req *cqr;
  2861. unsigned long flags;
  2862. int rc = 0;
  2863. cqr = blk_mq_rq_to_pdu(req);
  2864. if (!cqr)
  2865. return BLK_EH_DONE;
  2866. spin_lock_irqsave(&cqr->dq->lock, flags);
  2867. device = cqr->startdev ? cqr->startdev : block->base;
  2868. if (!device->blk_timeout) {
  2869. spin_unlock_irqrestore(&cqr->dq->lock, flags);
  2870. return BLK_EH_RESET_TIMER;
  2871. }
  2872. DBF_DEV_EVENT(DBF_WARNING, device,
  2873. " dasd_times_out cqr %p status %x",
  2874. cqr, cqr->status);
  2875. spin_lock(&block->queue_lock);
  2876. spin_lock(get_ccwdev_lock(device->cdev));
  2877. cqr->retries = -1;
  2878. cqr->intrc = -ETIMEDOUT;
  2879. if (cqr->status >= DASD_CQR_QUEUED) {
  2880. rc = __dasd_cancel_req(cqr);
  2881. } else if (cqr->status == DASD_CQR_FILLED ||
  2882. cqr->status == DASD_CQR_NEED_ERP) {
  2883. cqr->status = DASD_CQR_TERMINATED;
  2884. } else if (cqr->status == DASD_CQR_IN_ERP) {
  2885. struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
  2886. list_for_each_entry_safe(searchcqr, nextcqr,
  2887. &block->ccw_queue, blocklist) {
  2888. tmpcqr = searchcqr;
  2889. while (tmpcqr->refers)
  2890. tmpcqr = tmpcqr->refers;
  2891. if (tmpcqr != cqr)
  2892. continue;
  2893. /* searchcqr is an ERP request for cqr */
  2894. searchcqr->retries = -1;
  2895. searchcqr->intrc = -ETIMEDOUT;
  2896. if (searchcqr->status >= DASD_CQR_QUEUED) {
  2897. rc = __dasd_cancel_req(searchcqr);
  2898. } else if ((searchcqr->status == DASD_CQR_FILLED) ||
  2899. (searchcqr->status == DASD_CQR_NEED_ERP)) {
  2900. searchcqr->status = DASD_CQR_TERMINATED;
  2901. rc = 0;
  2902. } else if (searchcqr->status == DASD_CQR_IN_ERP) {
  2903. /*
  2904. * Shouldn't happen; most recent ERP
  2905. * request is at the front of queue
  2906. */
  2907. continue;
  2908. }
  2909. break;
  2910. }
  2911. }
  2912. spin_unlock(get_ccwdev_lock(device->cdev));
  2913. dasd_schedule_block_bh(block);
  2914. spin_unlock(&block->queue_lock);
  2915. spin_unlock_irqrestore(&cqr->dq->lock, flags);
  2916. return rc ? BLK_EH_RESET_TIMER : BLK_EH_DONE;
  2917. }
  2918. static int dasd_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  2919. unsigned int idx)
  2920. {
  2921. struct dasd_queue *dq = kzalloc(sizeof(*dq), GFP_KERNEL);
  2922. if (!dq)
  2923. return -ENOMEM;
  2924. spin_lock_init(&dq->lock);
  2925. hctx->driver_data = dq;
  2926. return 0;
  2927. }
  2928. static void dasd_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int idx)
  2929. {
  2930. kfree(hctx->driver_data);
  2931. hctx->driver_data = NULL;
  2932. }
  2933. static void dasd_request_done(struct request *req)
  2934. {
  2935. blk_mq_end_request(req, 0);
  2936. blk_mq_run_hw_queues(req->q, true);
  2937. }
  2938. static struct blk_mq_ops dasd_mq_ops = {
  2939. .queue_rq = do_dasd_request,
  2940. .complete = dasd_request_done,
  2941. .timeout = dasd_times_out,
  2942. .init_hctx = dasd_init_hctx,
  2943. .exit_hctx = dasd_exit_hctx,
  2944. };
  2945. /*
  2946. * Allocate and initialize request queue and default I/O scheduler.
  2947. */
  2948. static int dasd_alloc_queue(struct dasd_block *block)
  2949. {
  2950. int rc;
  2951. block->tag_set.ops = &dasd_mq_ops;
  2952. block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
  2953. block->tag_set.nr_hw_queues = nr_hw_queues;
  2954. block->tag_set.queue_depth = queue_depth;
  2955. block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  2956. block->tag_set.numa_node = NUMA_NO_NODE;
  2957. rc = blk_mq_alloc_tag_set(&block->tag_set);
  2958. if (rc)
  2959. return rc;
  2960. block->request_queue = blk_mq_init_queue(&block->tag_set);
  2961. if (IS_ERR(block->request_queue))
  2962. return PTR_ERR(block->request_queue);
  2963. block->request_queue->queuedata = block;
  2964. return 0;
  2965. }
  2966. /*
  2967. * Deactivate and free request queue.
  2968. */
  2969. static void dasd_free_queue(struct dasd_block *block)
  2970. {
  2971. if (block->request_queue) {
  2972. blk_cleanup_queue(block->request_queue);
  2973. blk_mq_free_tag_set(&block->tag_set);
  2974. block->request_queue = NULL;
  2975. }
  2976. }
  2977. static int dasd_open(struct block_device *bdev, fmode_t mode)
  2978. {
  2979. struct dasd_device *base;
  2980. int rc;
  2981. base = dasd_device_from_gendisk(bdev->bd_disk);
  2982. if (!base)
  2983. return -ENODEV;
  2984. atomic_inc(&base->block->open_count);
  2985. if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
  2986. rc = -ENODEV;
  2987. goto unlock;
  2988. }
  2989. if (!try_module_get(base->discipline->owner)) {
  2990. rc = -EINVAL;
  2991. goto unlock;
  2992. }
  2993. if (dasd_probeonly) {
  2994. dev_info(&base->cdev->dev,
  2995. "Accessing the DASD failed because it is in "
  2996. "probeonly mode\n");
  2997. rc = -EPERM;
  2998. goto out;
  2999. }
  3000. if (base->state <= DASD_STATE_BASIC) {
  3001. DBF_DEV_EVENT(DBF_ERR, base, " %s",
  3002. " Cannot open unrecognized device");
  3003. rc = -ENODEV;
  3004. goto out;
  3005. }
  3006. if ((mode & FMODE_WRITE) &&
  3007. (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
  3008. (base->features & DASD_FEATURE_READONLY))) {
  3009. rc = -EROFS;
  3010. goto out;
  3011. }
  3012. dasd_put_device(base);
  3013. return 0;
  3014. out:
  3015. module_put(base->discipline->owner);
  3016. unlock:
  3017. atomic_dec(&base->block->open_count);
  3018. dasd_put_device(base);
  3019. return rc;
  3020. }
  3021. static void dasd_release(struct gendisk *disk, fmode_t mode)
  3022. {
  3023. struct dasd_device *base = dasd_device_from_gendisk(disk);
  3024. if (base) {
  3025. atomic_dec(&base->block->open_count);
  3026. module_put(base->discipline->owner);
  3027. dasd_put_device(base);
  3028. }
  3029. }
  3030. /*
  3031. * Return disk geometry.
  3032. */
  3033. static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  3034. {
  3035. struct dasd_device *base;
  3036. base = dasd_device_from_gendisk(bdev->bd_disk);
  3037. if (!base)
  3038. return -ENODEV;
  3039. if (!base->discipline ||
  3040. !base->discipline->fill_geometry) {
  3041. dasd_put_device(base);
  3042. return -EINVAL;
  3043. }
  3044. base->discipline->fill_geometry(base->block, geo);
  3045. geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
  3046. dasd_put_device(base);
  3047. return 0;
  3048. }
  3049. const struct block_device_operations
  3050. dasd_device_operations = {
  3051. .owner = THIS_MODULE,
  3052. .open = dasd_open,
  3053. .release = dasd_release,
  3054. .ioctl = dasd_ioctl,
  3055. .compat_ioctl = dasd_ioctl,
  3056. .getgeo = dasd_getgeo,
  3057. };
  3058. /*******************************************************************************
  3059. * end of block device operations
  3060. */
  3061. static void
  3062. dasd_exit(void)
  3063. {
  3064. #ifdef CONFIG_PROC_FS
  3065. dasd_proc_exit();
  3066. #endif
  3067. dasd_eer_exit();
  3068. kmem_cache_destroy(dasd_page_cache);
  3069. dasd_page_cache = NULL;
  3070. dasd_gendisk_exit();
  3071. dasd_devmap_exit();
  3072. if (dasd_debug_area != NULL) {
  3073. debug_unregister(dasd_debug_area);
  3074. dasd_debug_area = NULL;
  3075. }
  3076. dasd_statistics_removeroot();
  3077. }
  3078. /*
  3079. * SECTION: common functions for ccw_driver use
  3080. */
  3081. /*
  3082. * Is the device read-only?
  3083. * Note that this function does not report the setting of the
  3084. * readonly device attribute, but how it is configured in z/VM.
  3085. */
  3086. int dasd_device_is_ro(struct dasd_device *device)
  3087. {
  3088. struct ccw_dev_id dev_id;
  3089. struct diag210 diag_data;
  3090. int rc;
  3091. if (!MACHINE_IS_VM)
  3092. return 0;
  3093. ccw_device_get_id(device->cdev, &dev_id);
  3094. memset(&diag_data, 0, sizeof(diag_data));
  3095. diag_data.vrdcdvno = dev_id.devno;
  3096. diag_data.vrdclen = sizeof(diag_data);
  3097. rc = diag210(&diag_data);
  3098. if (rc == 0 || rc == 2) {
  3099. return diag_data.vrdcvfla & 0x80;
  3100. } else {
  3101. DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
  3102. dev_id.devno, rc);
  3103. return 0;
  3104. }
  3105. }
  3106. EXPORT_SYMBOL_GPL(dasd_device_is_ro);
  3107. static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
  3108. {
  3109. struct ccw_device *cdev = data;
  3110. int ret;
  3111. ret = ccw_device_set_online(cdev);
  3112. if (ret)
  3113. pr_warn("%s: Setting the DASD online failed with rc=%d\n",
  3114. dev_name(&cdev->dev), ret);
  3115. }
  3116. /*
  3117. * Initial attempt at a probe function. this can be simplified once
  3118. * the other detection code is gone.
  3119. */
  3120. int dasd_generic_probe(struct ccw_device *cdev,
  3121. struct dasd_discipline *discipline)
  3122. {
  3123. int ret;
  3124. ret = dasd_add_sysfs_files(cdev);
  3125. if (ret) {
  3126. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
  3127. "dasd_generic_probe: could not add "
  3128. "sysfs entries");
  3129. return ret;
  3130. }
  3131. cdev->handler = &dasd_int_handler;
  3132. /*
  3133. * Automatically online either all dasd devices (dasd_autodetect)
  3134. * or all devices specified with dasd= parameters during
  3135. * initial probe.
  3136. */
  3137. if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
  3138. (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
  3139. async_schedule(dasd_generic_auto_online, cdev);
  3140. return 0;
  3141. }
  3142. EXPORT_SYMBOL_GPL(dasd_generic_probe);
  3143. void dasd_generic_free_discipline(struct dasd_device *device)
  3144. {
  3145. /* Forget the discipline information. */
  3146. if (device->discipline) {
  3147. if (device->discipline->uncheck_device)
  3148. device->discipline->uncheck_device(device);
  3149. module_put(device->discipline->owner);
  3150. device->discipline = NULL;
  3151. }
  3152. if (device->base_discipline) {
  3153. module_put(device->base_discipline->owner);
  3154. device->base_discipline = NULL;
  3155. }
  3156. }
  3157. EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
  3158. /*
  3159. * This will one day be called from a global not_oper handler.
  3160. * It is also used by driver_unregister during module unload.
  3161. */
  3162. void dasd_generic_remove(struct ccw_device *cdev)
  3163. {
  3164. struct dasd_device *device;
  3165. struct dasd_block *block;
  3166. device = dasd_device_from_cdev(cdev);
  3167. if (IS_ERR(device)) {
  3168. dasd_remove_sysfs_files(cdev);
  3169. return;
  3170. }
  3171. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
  3172. !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
  3173. /* Already doing offline processing */
  3174. dasd_put_device(device);
  3175. dasd_remove_sysfs_files(cdev);
  3176. return;
  3177. }
  3178. /*
  3179. * This device is removed unconditionally. Set offline
  3180. * flag to prevent dasd_open from opening it while it is
  3181. * no quite down yet.
  3182. */
  3183. dasd_set_target_state(device, DASD_STATE_NEW);
  3184. cdev->handler = NULL;
  3185. /* dasd_delete_device destroys the device reference. */
  3186. block = device->block;
  3187. dasd_delete_device(device);
  3188. /*
  3189. * life cycle of block is bound to device, so delete it after
  3190. * device was safely removed
  3191. */
  3192. if (block)
  3193. dasd_free_block(block);
  3194. dasd_remove_sysfs_files(cdev);
  3195. }
  3196. EXPORT_SYMBOL_GPL(dasd_generic_remove);
  3197. /*
  3198. * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
  3199. * the device is detected for the first time and is supposed to be used
  3200. * or the user has started activation through sysfs.
  3201. */
  3202. int dasd_generic_set_online(struct ccw_device *cdev,
  3203. struct dasd_discipline *base_discipline)
  3204. {
  3205. struct dasd_discipline *discipline;
  3206. struct dasd_device *device;
  3207. int rc;
  3208. /* first online clears initial online feature flag */
  3209. dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
  3210. device = dasd_create_device(cdev);
  3211. if (IS_ERR(device))
  3212. return PTR_ERR(device);
  3213. discipline = base_discipline;
  3214. if (device->features & DASD_FEATURE_USEDIAG) {
  3215. if (!dasd_diag_discipline_pointer) {
  3216. /* Try to load the required module. */
  3217. rc = request_module(DASD_DIAG_MOD);
  3218. if (rc) {
  3219. pr_warn("%s Setting the DASD online failed "
  3220. "because the required module %s "
  3221. "could not be loaded (rc=%d)\n",
  3222. dev_name(&cdev->dev), DASD_DIAG_MOD,
  3223. rc);
  3224. dasd_delete_device(device);
  3225. return -ENODEV;
  3226. }
  3227. }
  3228. /* Module init could have failed, so check again here after
  3229. * request_module(). */
  3230. if (!dasd_diag_discipline_pointer) {
  3231. pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
  3232. dev_name(&cdev->dev));
  3233. dasd_delete_device(device);
  3234. return -ENODEV;
  3235. }
  3236. discipline = dasd_diag_discipline_pointer;
  3237. }
  3238. if (!try_module_get(base_discipline->owner)) {
  3239. dasd_delete_device(device);
  3240. return -EINVAL;
  3241. }
  3242. if (!try_module_get(discipline->owner)) {
  3243. module_put(base_discipline->owner);
  3244. dasd_delete_device(device);
  3245. return -EINVAL;
  3246. }
  3247. device->base_discipline = base_discipline;
  3248. device->discipline = discipline;
  3249. /* check_device will allocate block device if necessary */
  3250. rc = discipline->check_device(device);
  3251. if (rc) {
  3252. pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
  3253. dev_name(&cdev->dev), discipline->name, rc);
  3254. module_put(discipline->owner);
  3255. module_put(base_discipline->owner);
  3256. dasd_delete_device(device);
  3257. return rc;
  3258. }
  3259. dasd_set_target_state(device, DASD_STATE_ONLINE);
  3260. if (device->state <= DASD_STATE_KNOWN) {
  3261. pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
  3262. dev_name(&cdev->dev));
  3263. rc = -ENODEV;
  3264. dasd_set_target_state(device, DASD_STATE_NEW);
  3265. if (device->block)
  3266. dasd_free_block(device->block);
  3267. dasd_delete_device(device);
  3268. } else
  3269. pr_debug("dasd_generic device %s found\n",
  3270. dev_name(&cdev->dev));
  3271. wait_event(dasd_init_waitq, _wait_for_device(device));
  3272. dasd_put_device(device);
  3273. return rc;
  3274. }
  3275. EXPORT_SYMBOL_GPL(dasd_generic_set_online);
  3276. int dasd_generic_set_offline(struct ccw_device *cdev)
  3277. {
  3278. struct dasd_device *device;
  3279. struct dasd_block *block;
  3280. int max_count, open_count, rc;
  3281. unsigned long flags;
  3282. rc = 0;
  3283. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  3284. device = dasd_device_from_cdev_locked(cdev);
  3285. if (IS_ERR(device)) {
  3286. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  3287. return PTR_ERR(device);
  3288. }
  3289. /*
  3290. * We must make sure that this device is currently not in use.
  3291. * The open_count is increased for every opener, that includes
  3292. * the blkdev_get in dasd_scan_partitions. We are only interested
  3293. * in the other openers.
  3294. */
  3295. if (device->block) {
  3296. max_count = device->block->bdev ? 0 : -1;
  3297. open_count = atomic_read(&device->block->open_count);
  3298. if (open_count > max_count) {
  3299. if (open_count > 0)
  3300. pr_warn("%s: The DASD cannot be set offline with open count %i\n",
  3301. dev_name(&cdev->dev), open_count);
  3302. else
  3303. pr_warn("%s: The DASD cannot be set offline while it is in use\n",
  3304. dev_name(&cdev->dev));
  3305. rc = -EBUSY;
  3306. goto out_err;
  3307. }
  3308. }
  3309. /*
  3310. * Test if the offline processing is already running and exit if so.
  3311. * If a safe offline is being processed this could only be a normal
  3312. * offline that should be able to overtake the safe offline and
  3313. * cancel any I/O we do not want to wait for any longer
  3314. */
  3315. if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  3316. if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
  3317. clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING,
  3318. &device->flags);
  3319. } else {
  3320. rc = -EBUSY;
  3321. goto out_err;
  3322. }
  3323. }
  3324. set_bit(DASD_FLAG_OFFLINE, &device->flags);
  3325. /*
  3326. * if safe_offline is called set safe_offline_running flag and
  3327. * clear safe_offline so that a call to normal offline
  3328. * can overrun safe_offline processing
  3329. */
  3330. if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
  3331. !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
  3332. /* need to unlock here to wait for outstanding I/O */
  3333. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  3334. /*
  3335. * If we want to set the device safe offline all IO operations
  3336. * should be finished before continuing the offline process
  3337. * so sync bdev first and then wait for our queues to become
  3338. * empty
  3339. */
  3340. if (device->block) {
  3341. rc = fsync_bdev(device->block->bdev);
  3342. if (rc != 0)
  3343. goto interrupted;
  3344. }
  3345. dasd_schedule_device_bh(device);
  3346. rc = wait_event_interruptible(shutdown_waitq,
  3347. _wait_for_empty_queues(device));
  3348. if (rc != 0)
  3349. goto interrupted;
  3350. /*
  3351. * check if a normal offline process overtook the offline
  3352. * processing in this case simply do nothing beside returning
  3353. * that we got interrupted
  3354. * otherwise mark safe offline as not running any longer and
  3355. * continue with normal offline
  3356. */
  3357. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  3358. if (!test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
  3359. rc = -ERESTARTSYS;
  3360. goto out_err;
  3361. }
  3362. clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
  3363. }
  3364. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  3365. dasd_set_target_state(device, DASD_STATE_NEW);
  3366. /* dasd_delete_device destroys the device reference. */
  3367. block = device->block;
  3368. dasd_delete_device(device);
  3369. /*
  3370. * life cycle of block is bound to device, so delete it after
  3371. * device was safely removed
  3372. */
  3373. if (block)
  3374. dasd_free_block(block);
  3375. return 0;
  3376. interrupted:
  3377. /* interrupted by signal */
  3378. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  3379. clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
  3380. clear_bit(DASD_FLAG_OFFLINE, &device->flags);
  3381. out_err:
  3382. dasd_put_device(device);
  3383. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  3384. return rc;
  3385. }
  3386. EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
  3387. int dasd_generic_last_path_gone(struct dasd_device *device)
  3388. {
  3389. struct dasd_ccw_req *cqr;
  3390. dev_warn(&device->cdev->dev, "No operational channel path is left "
  3391. "for the device\n");
  3392. DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
  3393. /* First of all call extended error reporting. */
  3394. dasd_eer_write(device, NULL, DASD_EER_NOPATH);
  3395. if (device->state < DASD_STATE_BASIC)
  3396. return 0;
  3397. /* Device is active. We want to keep it. */
  3398. list_for_each_entry(cqr, &device->ccw_queue, devlist)
  3399. if ((cqr->status == DASD_CQR_IN_IO) ||
  3400. (cqr->status == DASD_CQR_CLEAR_PENDING)) {
  3401. cqr->status = DASD_CQR_QUEUED;
  3402. cqr->retries++;
  3403. }
  3404. dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
  3405. dasd_device_clear_timer(device);
  3406. dasd_schedule_device_bh(device);
  3407. return 1;
  3408. }
  3409. EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
  3410. int dasd_generic_path_operational(struct dasd_device *device)
  3411. {
  3412. dev_info(&device->cdev->dev, "A channel path to the device has become "
  3413. "operational\n");
  3414. DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
  3415. dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
  3416. if (device->stopped & DASD_UNRESUMED_PM) {
  3417. dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
  3418. dasd_restore_device(device);
  3419. return 1;
  3420. }
  3421. dasd_schedule_device_bh(device);
  3422. if (device->block) {
  3423. dasd_schedule_block_bh(device->block);
  3424. if (device->block->request_queue)
  3425. blk_mq_run_hw_queues(device->block->request_queue,
  3426. true);
  3427. }
  3428. if (!device->stopped)
  3429. wake_up(&generic_waitq);
  3430. return 1;
  3431. }
  3432. EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
  3433. int dasd_generic_notify(struct ccw_device *cdev, int event)
  3434. {
  3435. struct dasd_device *device;
  3436. int ret;
  3437. device = dasd_device_from_cdev_locked(cdev);
  3438. if (IS_ERR(device))
  3439. return 0;
  3440. ret = 0;
  3441. switch (event) {
  3442. case CIO_GONE:
  3443. case CIO_BOXED:
  3444. case CIO_NO_PATH:
  3445. dasd_path_no_path(device);
  3446. ret = dasd_generic_last_path_gone(device);
  3447. break;
  3448. case CIO_OPER:
  3449. ret = 1;
  3450. if (dasd_path_get_opm(device))
  3451. ret = dasd_generic_path_operational(device);
  3452. break;
  3453. }
  3454. dasd_put_device(device);
  3455. return ret;
  3456. }
  3457. EXPORT_SYMBOL_GPL(dasd_generic_notify);
  3458. void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
  3459. {
  3460. struct dasd_device *device;
  3461. int chp, oldopm, hpfpm, ifccpm;
  3462. device = dasd_device_from_cdev_locked(cdev);
  3463. if (IS_ERR(device))
  3464. return;
  3465. oldopm = dasd_path_get_opm(device);
  3466. for (chp = 0; chp < 8; chp++) {
  3467. if (path_event[chp] & PE_PATH_GONE) {
  3468. dasd_path_notoper(device, chp);
  3469. }
  3470. if (path_event[chp] & PE_PATH_AVAILABLE) {
  3471. dasd_path_available(device, chp);
  3472. dasd_schedule_device_bh(device);
  3473. }
  3474. if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
  3475. if (!dasd_path_is_operational(device, chp) &&
  3476. !dasd_path_need_verify(device, chp)) {
  3477. /*
  3478. * we can not establish a pathgroup on an
  3479. * unavailable path, so trigger a path
  3480. * verification first
  3481. */
  3482. dasd_path_available(device, chp);
  3483. dasd_schedule_device_bh(device);
  3484. }
  3485. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  3486. "Pathgroup re-established\n");
  3487. if (device->discipline->kick_validate)
  3488. device->discipline->kick_validate(device);
  3489. }
  3490. }
  3491. hpfpm = dasd_path_get_hpfpm(device);
  3492. ifccpm = dasd_path_get_ifccpm(device);
  3493. if (!dasd_path_get_opm(device) && hpfpm) {
  3494. /*
  3495. * device has no operational paths but at least one path is
  3496. * disabled due to HPF errors
  3497. * disable HPF at all and use the path(s) again
  3498. */
  3499. if (device->discipline->disable_hpf)
  3500. device->discipline->disable_hpf(device);
  3501. dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
  3502. dasd_path_set_tbvpm(device, hpfpm);
  3503. dasd_schedule_device_bh(device);
  3504. dasd_schedule_requeue(device);
  3505. } else if (!dasd_path_get_opm(device) && ifccpm) {
  3506. /*
  3507. * device has no operational paths but at least one path is
  3508. * disabled due to IFCC errors
  3509. * trigger path verification on paths with IFCC errors
  3510. */
  3511. dasd_path_set_tbvpm(device, ifccpm);
  3512. dasd_schedule_device_bh(device);
  3513. }
  3514. if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
  3515. dev_warn(&device->cdev->dev,
  3516. "No verified channel paths remain for the device\n");
  3517. DBF_DEV_EVENT(DBF_WARNING, device,
  3518. "%s", "last verified path gone");
  3519. dasd_eer_write(device, NULL, DASD_EER_NOPATH);
  3520. dasd_device_set_stop_bits(device,
  3521. DASD_STOPPED_DC_WAIT);
  3522. }
  3523. dasd_put_device(device);
  3524. }
  3525. EXPORT_SYMBOL_GPL(dasd_generic_path_event);
  3526. int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
  3527. {
  3528. if (!dasd_path_get_opm(device) && lpm) {
  3529. dasd_path_set_opm(device, lpm);
  3530. dasd_generic_path_operational(device);
  3531. } else
  3532. dasd_path_add_opm(device, lpm);
  3533. return 0;
  3534. }
  3535. EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
  3536. void dasd_generic_space_exhaust(struct dasd_device *device,
  3537. struct dasd_ccw_req *cqr)
  3538. {
  3539. dasd_eer_write(device, NULL, DASD_EER_NOSPC);
  3540. if (device->state < DASD_STATE_BASIC)
  3541. return;
  3542. if (cqr->status == DASD_CQR_IN_IO ||
  3543. cqr->status == DASD_CQR_CLEAR_PENDING) {
  3544. cqr->status = DASD_CQR_QUEUED;
  3545. cqr->retries++;
  3546. }
  3547. dasd_device_set_stop_bits(device, DASD_STOPPED_NOSPC);
  3548. dasd_device_clear_timer(device);
  3549. dasd_schedule_device_bh(device);
  3550. }
  3551. EXPORT_SYMBOL_GPL(dasd_generic_space_exhaust);
  3552. void dasd_generic_space_avail(struct dasd_device *device)
  3553. {
  3554. dev_info(&device->cdev->dev, "Extent pool space is available\n");
  3555. DBF_DEV_EVENT(DBF_WARNING, device, "%s", "space available");
  3556. dasd_device_remove_stop_bits(device, DASD_STOPPED_NOSPC);
  3557. dasd_schedule_device_bh(device);
  3558. if (device->block) {
  3559. dasd_schedule_block_bh(device->block);
  3560. if (device->block->request_queue)
  3561. blk_mq_run_hw_queues(device->block->request_queue, true);
  3562. }
  3563. if (!device->stopped)
  3564. wake_up(&generic_waitq);
  3565. }
  3566. EXPORT_SYMBOL_GPL(dasd_generic_space_avail);
  3567. /*
  3568. * clear active requests and requeue them to block layer if possible
  3569. */
  3570. static int dasd_generic_requeue_all_requests(struct dasd_device *device)
  3571. {
  3572. struct list_head requeue_queue;
  3573. struct dasd_ccw_req *cqr, *n;
  3574. struct dasd_ccw_req *refers;
  3575. int rc;
  3576. INIT_LIST_HEAD(&requeue_queue);
  3577. spin_lock_irq(get_ccwdev_lock(device->cdev));
  3578. rc = 0;
  3579. list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
  3580. /* Check status and move request to flush_queue */
  3581. if (cqr->status == DASD_CQR_IN_IO) {
  3582. rc = device->discipline->term_IO(cqr);
  3583. if (rc) {
  3584. /* unable to terminate requeust */
  3585. dev_err(&device->cdev->dev,
  3586. "Unable to terminate request %p "
  3587. "on suspend\n", cqr);
  3588. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  3589. dasd_put_device(device);
  3590. return rc;
  3591. }
  3592. }
  3593. list_move_tail(&cqr->devlist, &requeue_queue);
  3594. }
  3595. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  3596. list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
  3597. wait_event(dasd_flush_wq,
  3598. (cqr->status != DASD_CQR_CLEAR_PENDING));
  3599. /*
  3600. * requeue requests to blocklayer will only work
  3601. * for block device requests
  3602. */
  3603. if (_dasd_requeue_request(cqr))
  3604. continue;
  3605. /* remove requests from device and block queue */
  3606. list_del_init(&cqr->devlist);
  3607. while (cqr->refers != NULL) {
  3608. refers = cqr->refers;
  3609. /* remove the request from the block queue */
  3610. list_del(&cqr->blocklist);
  3611. /* free the finished erp request */
  3612. dasd_free_erp_request(cqr, cqr->memdev);
  3613. cqr = refers;
  3614. }
  3615. /*
  3616. * _dasd_requeue_request already checked for a valid
  3617. * blockdevice, no need to check again
  3618. * all erp requests (cqr->refers) have a cqr->block
  3619. * pointer copy from the original cqr
  3620. */
  3621. list_del_init(&cqr->blocklist);
  3622. cqr->block->base->discipline->free_cp(
  3623. cqr, (struct request *) cqr->callback_data);
  3624. }
  3625. /*
  3626. * if requests remain then they are internal request
  3627. * and go back to the device queue
  3628. */
  3629. if (!list_empty(&requeue_queue)) {
  3630. /* move freeze_queue to start of the ccw_queue */
  3631. spin_lock_irq(get_ccwdev_lock(device->cdev));
  3632. list_splice_tail(&requeue_queue, &device->ccw_queue);
  3633. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  3634. }
  3635. dasd_schedule_device_bh(device);
  3636. return rc;
  3637. }
  3638. static void do_requeue_requests(struct work_struct *work)
  3639. {
  3640. struct dasd_device *device = container_of(work, struct dasd_device,
  3641. requeue_requests);
  3642. dasd_generic_requeue_all_requests(device);
  3643. dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
  3644. if (device->block)
  3645. dasd_schedule_block_bh(device->block);
  3646. dasd_put_device(device);
  3647. }
  3648. void dasd_schedule_requeue(struct dasd_device *device)
  3649. {
  3650. dasd_get_device(device);
  3651. /* queue call to dasd_reload_device to the kernel event daemon. */
  3652. if (!schedule_work(&device->requeue_requests))
  3653. dasd_put_device(device);
  3654. }
  3655. EXPORT_SYMBOL(dasd_schedule_requeue);
  3656. int dasd_generic_pm_freeze(struct ccw_device *cdev)
  3657. {
  3658. struct dasd_device *device = dasd_device_from_cdev(cdev);
  3659. if (IS_ERR(device))
  3660. return PTR_ERR(device);
  3661. /* mark device as suspended */
  3662. set_bit(DASD_FLAG_SUSPENDED, &device->flags);
  3663. if (device->discipline->freeze)
  3664. device->discipline->freeze(device);
  3665. /* disallow new I/O */
  3666. dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
  3667. return dasd_generic_requeue_all_requests(device);
  3668. }
  3669. EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
  3670. int dasd_generic_restore_device(struct ccw_device *cdev)
  3671. {
  3672. struct dasd_device *device = dasd_device_from_cdev(cdev);
  3673. int rc = 0;
  3674. if (IS_ERR(device))
  3675. return PTR_ERR(device);
  3676. /* allow new IO again */
  3677. dasd_device_remove_stop_bits(device,
  3678. (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
  3679. dasd_schedule_device_bh(device);
  3680. /*
  3681. * call discipline restore function
  3682. * if device is stopped do nothing e.g. for disconnected devices
  3683. */
  3684. if (device->discipline->restore && !(device->stopped))
  3685. rc = device->discipline->restore(device);
  3686. if (rc || device->stopped)
  3687. /*
  3688. * if the resume failed for the DASD we put it in
  3689. * an UNRESUMED stop state
  3690. */
  3691. device->stopped |= DASD_UNRESUMED_PM;
  3692. if (device->block) {
  3693. dasd_schedule_block_bh(device->block);
  3694. if (device->block->request_queue)
  3695. blk_mq_run_hw_queues(device->block->request_queue,
  3696. true);
  3697. }
  3698. clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
  3699. dasd_put_device(device);
  3700. return 0;
  3701. }
  3702. EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
  3703. static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
  3704. int rdc_buffer_size,
  3705. int magic)
  3706. {
  3707. struct dasd_ccw_req *cqr;
  3708. struct ccw1 *ccw;
  3709. cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device,
  3710. NULL);
  3711. if (IS_ERR(cqr)) {
  3712. /* internal error 13 - Allocating the RDC request failed*/
  3713. dev_err(&device->cdev->dev,
  3714. "An error occurred in the DASD device driver, "
  3715. "reason=%s\n", "13");
  3716. return cqr;
  3717. }
  3718. ccw = cqr->cpaddr;
  3719. ccw->cmd_code = CCW_CMD_RDC;
  3720. ccw->cda = (__u32)(addr_t) cqr->data;
  3721. ccw->flags = 0;
  3722. ccw->count = rdc_buffer_size;
  3723. cqr->startdev = device;
  3724. cqr->memdev = device;
  3725. cqr->expires = 10*HZ;
  3726. cqr->retries = 256;
  3727. cqr->buildclk = get_tod_clock();
  3728. cqr->status = DASD_CQR_FILLED;
  3729. return cqr;
  3730. }
  3731. int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
  3732. void *rdc_buffer, int rdc_buffer_size)
  3733. {
  3734. int ret;
  3735. struct dasd_ccw_req *cqr;
  3736. cqr = dasd_generic_build_rdc(device, rdc_buffer_size, magic);
  3737. if (IS_ERR(cqr))
  3738. return PTR_ERR(cqr);
  3739. ret = dasd_sleep_on(cqr);
  3740. if (ret == 0)
  3741. memcpy(rdc_buffer, cqr->data, rdc_buffer_size);
  3742. dasd_sfree_request(cqr, cqr->memdev);
  3743. return ret;
  3744. }
  3745. EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
  3746. /*
  3747. * In command mode and transport mode we need to look for sense
  3748. * data in different places. The sense data itself is allways
  3749. * an array of 32 bytes, so we can unify the sense data access
  3750. * for both modes.
  3751. */
  3752. char *dasd_get_sense(struct irb *irb)
  3753. {
  3754. struct tsb *tsb = NULL;
  3755. char *sense = NULL;
  3756. if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
  3757. if (irb->scsw.tm.tcw)
  3758. tsb = tcw_get_tsb((struct tcw *)(unsigned long)
  3759. irb->scsw.tm.tcw);
  3760. if (tsb && tsb->length == 64 && tsb->flags)
  3761. switch (tsb->flags & 0x07) {
  3762. case 1: /* tsa_iostat */
  3763. sense = tsb->tsa.iostat.sense;
  3764. break;
  3765. case 2: /* tsa_ddpc */
  3766. sense = tsb->tsa.ddpc.sense;
  3767. break;
  3768. default:
  3769. /* currently we don't use interrogate data */
  3770. break;
  3771. }
  3772. } else if (irb->esw.esw0.erw.cons) {
  3773. sense = irb->ecw;
  3774. }
  3775. return sense;
  3776. }
  3777. EXPORT_SYMBOL_GPL(dasd_get_sense);
  3778. void dasd_generic_shutdown(struct ccw_device *cdev)
  3779. {
  3780. struct dasd_device *device;
  3781. device = dasd_device_from_cdev(cdev);
  3782. if (IS_ERR(device))
  3783. return;
  3784. if (device->block)
  3785. dasd_schedule_block_bh(device->block);
  3786. dasd_schedule_device_bh(device);
  3787. wait_event(shutdown_waitq, _wait_for_empty_queues(device));
  3788. }
  3789. EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
  3790. static int __init dasd_init(void)
  3791. {
  3792. int rc;
  3793. init_waitqueue_head(&dasd_init_waitq);
  3794. init_waitqueue_head(&dasd_flush_wq);
  3795. init_waitqueue_head(&generic_waitq);
  3796. init_waitqueue_head(&shutdown_waitq);
  3797. /* register 'common' DASD debug area, used for all DBF_XXX calls */
  3798. dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
  3799. if (dasd_debug_area == NULL) {
  3800. rc = -ENOMEM;
  3801. goto failed;
  3802. }
  3803. debug_register_view(dasd_debug_area, &debug_sprintf_view);
  3804. debug_set_level(dasd_debug_area, DBF_WARNING);
  3805. DBF_EVENT(DBF_EMERG, "%s", "debug area created");
  3806. dasd_diag_discipline_pointer = NULL;
  3807. dasd_statistics_createroot();
  3808. rc = dasd_devmap_init();
  3809. if (rc)
  3810. goto failed;
  3811. rc = dasd_gendisk_init();
  3812. if (rc)
  3813. goto failed;
  3814. rc = dasd_parse();
  3815. if (rc)
  3816. goto failed;
  3817. rc = dasd_eer_init();
  3818. if (rc)
  3819. goto failed;
  3820. #ifdef CONFIG_PROC_FS
  3821. rc = dasd_proc_init();
  3822. if (rc)
  3823. goto failed;
  3824. #endif
  3825. return 0;
  3826. failed:
  3827. pr_info("The DASD device driver could not be initialized\n");
  3828. dasd_exit();
  3829. return rc;
  3830. }
  3831. module_init(dasd_init);
  3832. module_exit(dasd_exit);