dasd.c 76 KB

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