dasd.c 101 KB

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