dasd.c 94 KB

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