dasd.c 75 KB

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