dasd.c 71 KB

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