dasd.c 71 KB

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