dasd.c 76 KB

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