dasd.c 68 KB

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