dasd.c 104 KB

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