dasd.c 75 KB

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