dasd.c 104 KB

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