dasd.c 94 KB

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