dasd.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904
  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. (scsw_stctl(&irb->scsw) == (SCSW_STCTL_STATUS_PEND |
  991. SCSW_STCTL_ALERT_STATUS))))) {
  992. if (cqr && cqr->status == DASD_CQR_IN_IO)
  993. cqr->status = DASD_CQR_QUEUED;
  994. if (cqr)
  995. memcpy(&cqr->irb, irb, sizeof(*irb));
  996. device = dasd_device_from_cdev_locked(cdev);
  997. if (IS_ERR(device))
  998. return;
  999. /* ignore unsolicited interrupts for DIAG discipline */
  1000. if (device->discipline == dasd_diag_discipline_pointer) {
  1001. dasd_put_device(device);
  1002. return;
  1003. }
  1004. device->discipline->dump_sense_dbf(device, irb,
  1005. "unsolicited");
  1006. if ((device->features & DASD_FEATURE_ERPLOG))
  1007. device->discipline->dump_sense(device, cqr,
  1008. irb);
  1009. dasd_device_clear_timer(device);
  1010. device->discipline->handle_unsolicited_interrupt(device,
  1011. irb);
  1012. dasd_put_device(device);
  1013. return;
  1014. }
  1015. device = (struct dasd_device *) cqr->startdev;
  1016. if (!device ||
  1017. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  1018. DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
  1019. "invalid device in request");
  1020. return;
  1021. }
  1022. /* Check for clear pending */
  1023. if (cqr->status == DASD_CQR_CLEAR_PENDING &&
  1024. scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
  1025. cqr->status = DASD_CQR_CLEARED;
  1026. dasd_device_clear_timer(device);
  1027. wake_up(&dasd_flush_wq);
  1028. dasd_schedule_device_bh(device);
  1029. return;
  1030. }
  1031. /* check status - the request might have been killed by dyn detach */
  1032. if (cqr->status != DASD_CQR_IN_IO) {
  1033. DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
  1034. "status %02x", dev_name(&cdev->dev), cqr->status);
  1035. return;
  1036. }
  1037. next = NULL;
  1038. expires = 0;
  1039. if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  1040. scsw_cstat(&irb->scsw) == 0) {
  1041. /* request was completed successfully */
  1042. cqr->status = DASD_CQR_SUCCESS;
  1043. cqr->stopclk = now;
  1044. /* Start first request on queue if possible -> fast_io. */
  1045. if (cqr->devlist.next != &device->ccw_queue) {
  1046. next = list_entry(cqr->devlist.next,
  1047. struct dasd_ccw_req, devlist);
  1048. }
  1049. } else { /* error */
  1050. memcpy(&cqr->irb, irb, sizeof(struct irb));
  1051. /* log sense for every failed I/O to s390 debugfeature */
  1052. dasd_log_sense_dbf(cqr, irb);
  1053. if (device->features & DASD_FEATURE_ERPLOG) {
  1054. dasd_log_sense(cqr, irb);
  1055. }
  1056. /*
  1057. * If we don't want complex ERP for this request, then just
  1058. * reset this and retry it in the fastpath
  1059. */
  1060. if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
  1061. cqr->retries > 0) {
  1062. if (cqr->lpm == LPM_ANYPATH)
  1063. DBF_DEV_EVENT(DBF_DEBUG, device,
  1064. "default ERP in fastpath "
  1065. "(%i retries left)",
  1066. cqr->retries);
  1067. cqr->lpm = LPM_ANYPATH;
  1068. cqr->status = DASD_CQR_QUEUED;
  1069. next = cqr;
  1070. } else
  1071. cqr->status = DASD_CQR_ERROR;
  1072. }
  1073. if (next && (next->status == DASD_CQR_QUEUED) &&
  1074. (!device->stopped)) {
  1075. if (device->discipline->start_IO(next) == 0)
  1076. expires = next->expires;
  1077. }
  1078. if (expires != 0)
  1079. dasd_device_set_timer(device, expires);
  1080. else
  1081. dasd_device_clear_timer(device);
  1082. dasd_schedule_device_bh(device);
  1083. }
  1084. enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
  1085. {
  1086. struct dasd_device *device;
  1087. device = dasd_device_from_cdev_locked(cdev);
  1088. if (IS_ERR(device))
  1089. goto out;
  1090. if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
  1091. device->state != device->target ||
  1092. !device->discipline->handle_unsolicited_interrupt){
  1093. dasd_put_device(device);
  1094. goto out;
  1095. }
  1096. dasd_device_clear_timer(device);
  1097. device->discipline->handle_unsolicited_interrupt(device, irb);
  1098. dasd_put_device(device);
  1099. out:
  1100. return UC_TODO_RETRY;
  1101. }
  1102. EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
  1103. /*
  1104. * If we have an error on a dasd_block layer request then we cancel
  1105. * and return all further requests from the same dasd_block as well.
  1106. */
  1107. static void __dasd_device_recovery(struct dasd_device *device,
  1108. struct dasd_ccw_req *ref_cqr)
  1109. {
  1110. struct list_head *l, *n;
  1111. struct dasd_ccw_req *cqr;
  1112. /*
  1113. * only requeue request that came from the dasd_block layer
  1114. */
  1115. if (!ref_cqr->block)
  1116. return;
  1117. list_for_each_safe(l, n, &device->ccw_queue) {
  1118. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1119. if (cqr->status == DASD_CQR_QUEUED &&
  1120. ref_cqr->block == cqr->block) {
  1121. cqr->status = DASD_CQR_CLEARED;
  1122. }
  1123. }
  1124. };
  1125. /*
  1126. * Remove those ccw requests from the queue that need to be returned
  1127. * to the upper layer.
  1128. */
  1129. static void __dasd_device_process_ccw_queue(struct dasd_device *device,
  1130. struct list_head *final_queue)
  1131. {
  1132. struct list_head *l, *n;
  1133. struct dasd_ccw_req *cqr;
  1134. /* Process request with final status. */
  1135. list_for_each_safe(l, n, &device->ccw_queue) {
  1136. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1137. /* Stop list processing at the first non-final request. */
  1138. if (cqr->status == DASD_CQR_QUEUED ||
  1139. cqr->status == DASD_CQR_IN_IO ||
  1140. cqr->status == DASD_CQR_CLEAR_PENDING)
  1141. break;
  1142. if (cqr->status == DASD_CQR_ERROR) {
  1143. __dasd_device_recovery(device, cqr);
  1144. }
  1145. /* Rechain finished requests to final queue */
  1146. list_move_tail(&cqr->devlist, final_queue);
  1147. }
  1148. }
  1149. /*
  1150. * the cqrs from the final queue are returned to the upper layer
  1151. * by setting a dasd_block state and calling the callback function
  1152. */
  1153. static void __dasd_device_process_final_queue(struct dasd_device *device,
  1154. struct list_head *final_queue)
  1155. {
  1156. struct list_head *l, *n;
  1157. struct dasd_ccw_req *cqr;
  1158. struct dasd_block *block;
  1159. void (*callback)(struct dasd_ccw_req *, void *data);
  1160. void *callback_data;
  1161. char errorstring[ERRORLENGTH];
  1162. list_for_each_safe(l, n, final_queue) {
  1163. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1164. list_del_init(&cqr->devlist);
  1165. block = cqr->block;
  1166. callback = cqr->callback;
  1167. callback_data = cqr->callback_data;
  1168. if (block)
  1169. spin_lock_bh(&block->queue_lock);
  1170. switch (cqr->status) {
  1171. case DASD_CQR_SUCCESS:
  1172. cqr->status = DASD_CQR_DONE;
  1173. break;
  1174. case DASD_CQR_ERROR:
  1175. cqr->status = DASD_CQR_NEED_ERP;
  1176. break;
  1177. case DASD_CQR_CLEARED:
  1178. cqr->status = DASD_CQR_TERMINATED;
  1179. break;
  1180. default:
  1181. /* internal error 12 - wrong cqr status*/
  1182. snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
  1183. dev_err(&device->cdev->dev,
  1184. "An error occurred in the DASD device driver, "
  1185. "reason=%s\n", errorstring);
  1186. BUG();
  1187. }
  1188. if (cqr->callback != NULL)
  1189. (callback)(cqr, callback_data);
  1190. if (block)
  1191. spin_unlock_bh(&block->queue_lock);
  1192. }
  1193. }
  1194. /*
  1195. * Take a look at the first request on the ccw queue and check
  1196. * if it reached its expire time. If so, terminate the IO.
  1197. */
  1198. static void __dasd_device_check_expire(struct dasd_device *device)
  1199. {
  1200. struct dasd_ccw_req *cqr;
  1201. if (list_empty(&device->ccw_queue))
  1202. return;
  1203. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1204. if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
  1205. (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
  1206. if (device->discipline->term_IO(cqr) != 0) {
  1207. /* Hmpf, try again in 5 sec */
  1208. dev_err(&device->cdev->dev,
  1209. "cqr %p timed out (%lus) but cannot be "
  1210. "ended, retrying in 5 s\n",
  1211. cqr, (cqr->expires/HZ));
  1212. cqr->expires += 5*HZ;
  1213. dasd_device_set_timer(device, 5*HZ);
  1214. } else {
  1215. dev_err(&device->cdev->dev,
  1216. "cqr %p timed out (%lus), %i retries "
  1217. "remaining\n", cqr, (cqr->expires/HZ),
  1218. cqr->retries);
  1219. }
  1220. }
  1221. }
  1222. /*
  1223. * Take a look at the first request on the ccw queue and check
  1224. * if it needs to be started.
  1225. */
  1226. static void __dasd_device_start_head(struct dasd_device *device)
  1227. {
  1228. struct dasd_ccw_req *cqr;
  1229. int rc;
  1230. if (list_empty(&device->ccw_queue))
  1231. return;
  1232. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1233. if (cqr->status != DASD_CQR_QUEUED)
  1234. return;
  1235. /* when device is stopped, return request to previous layer */
  1236. if (device->stopped) {
  1237. cqr->status = DASD_CQR_CLEARED;
  1238. dasd_schedule_device_bh(device);
  1239. return;
  1240. }
  1241. rc = device->discipline->start_IO(cqr);
  1242. if (rc == 0)
  1243. dasd_device_set_timer(device, cqr->expires);
  1244. else if (rc == -EACCES) {
  1245. dasd_schedule_device_bh(device);
  1246. } else
  1247. /* Hmpf, try again in 1/2 sec */
  1248. dasd_device_set_timer(device, 50);
  1249. }
  1250. /*
  1251. * Go through all request on the dasd_device request queue,
  1252. * terminate them on the cdev if necessary, and return them to the
  1253. * submitting layer via callback.
  1254. * Note:
  1255. * Make sure that all 'submitting layers' still exist when
  1256. * this function is called!. In other words, when 'device' is a base
  1257. * device then all block layer requests must have been removed before
  1258. * via dasd_flush_block_queue.
  1259. */
  1260. int dasd_flush_device_queue(struct dasd_device *device)
  1261. {
  1262. struct dasd_ccw_req *cqr, *n;
  1263. int rc;
  1264. struct list_head flush_queue;
  1265. INIT_LIST_HEAD(&flush_queue);
  1266. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1267. rc = 0;
  1268. list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
  1269. /* Check status and move request to flush_queue */
  1270. switch (cqr->status) {
  1271. case DASD_CQR_IN_IO:
  1272. rc = device->discipline->term_IO(cqr);
  1273. if (rc) {
  1274. /* unable to terminate requeust */
  1275. dev_err(&device->cdev->dev,
  1276. "Flushing the DASD request queue "
  1277. "failed for request %p\n", cqr);
  1278. /* stop flush processing */
  1279. goto finished;
  1280. }
  1281. break;
  1282. case DASD_CQR_QUEUED:
  1283. cqr->stopclk = get_clock();
  1284. cqr->status = DASD_CQR_CLEARED;
  1285. break;
  1286. default: /* no need to modify the others */
  1287. break;
  1288. }
  1289. list_move_tail(&cqr->devlist, &flush_queue);
  1290. }
  1291. finished:
  1292. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1293. /*
  1294. * After this point all requests must be in state CLEAR_PENDING,
  1295. * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
  1296. * one of the others.
  1297. */
  1298. list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
  1299. wait_event(dasd_flush_wq,
  1300. (cqr->status != DASD_CQR_CLEAR_PENDING));
  1301. /*
  1302. * Now set each request back to TERMINATED, DONE or NEED_ERP
  1303. * and call the callback function of flushed requests
  1304. */
  1305. __dasd_device_process_final_queue(device, &flush_queue);
  1306. return rc;
  1307. }
  1308. /*
  1309. * Acquire the device lock and process queues for the device.
  1310. */
  1311. static void dasd_device_tasklet(struct dasd_device *device)
  1312. {
  1313. struct list_head final_queue;
  1314. atomic_set (&device->tasklet_scheduled, 0);
  1315. INIT_LIST_HEAD(&final_queue);
  1316. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1317. /* Check expire time of first request on the ccw queue. */
  1318. __dasd_device_check_expire(device);
  1319. /* find final requests on ccw queue */
  1320. __dasd_device_process_ccw_queue(device, &final_queue);
  1321. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1322. /* Now call the callback function of requests with final status */
  1323. __dasd_device_process_final_queue(device, &final_queue);
  1324. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1325. /* Now check if the head of the ccw queue needs to be started. */
  1326. __dasd_device_start_head(device);
  1327. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1328. dasd_put_device(device);
  1329. }
  1330. /*
  1331. * Schedules a call to dasd_tasklet over the device tasklet.
  1332. */
  1333. void dasd_schedule_device_bh(struct dasd_device *device)
  1334. {
  1335. /* Protect against rescheduling. */
  1336. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  1337. return;
  1338. dasd_get_device(device);
  1339. tasklet_hi_schedule(&device->tasklet);
  1340. }
  1341. void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
  1342. {
  1343. device->stopped |= bits;
  1344. }
  1345. EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
  1346. void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
  1347. {
  1348. device->stopped &= ~bits;
  1349. if (!device->stopped)
  1350. wake_up(&generic_waitq);
  1351. }
  1352. EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
  1353. /*
  1354. * Queue a request to the head of the device ccw_queue.
  1355. * Start the I/O if possible.
  1356. */
  1357. void dasd_add_request_head(struct dasd_ccw_req *cqr)
  1358. {
  1359. struct dasd_device *device;
  1360. unsigned long flags;
  1361. device = cqr->startdev;
  1362. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1363. cqr->status = DASD_CQR_QUEUED;
  1364. list_add(&cqr->devlist, &device->ccw_queue);
  1365. /* let the bh start the request to keep them in order */
  1366. dasd_schedule_device_bh(device);
  1367. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1368. }
  1369. /*
  1370. * Queue a request to the tail of the device ccw_queue.
  1371. * Start the I/O if possible.
  1372. */
  1373. void dasd_add_request_tail(struct dasd_ccw_req *cqr)
  1374. {
  1375. struct dasd_device *device;
  1376. unsigned long flags;
  1377. device = cqr->startdev;
  1378. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1379. cqr->status = DASD_CQR_QUEUED;
  1380. list_add_tail(&cqr->devlist, &device->ccw_queue);
  1381. /* let the bh start the request to keep them in order */
  1382. dasd_schedule_device_bh(device);
  1383. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1384. }
  1385. /*
  1386. * Wakeup helper for the 'sleep_on' functions.
  1387. */
  1388. static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  1389. {
  1390. spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
  1391. cqr->callback_data = DASD_SLEEPON_END_TAG;
  1392. spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
  1393. wake_up(&generic_waitq);
  1394. }
  1395. static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
  1396. {
  1397. struct dasd_device *device;
  1398. int rc;
  1399. device = cqr->startdev;
  1400. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1401. rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
  1402. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1403. return rc;
  1404. }
  1405. /*
  1406. * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
  1407. */
  1408. static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
  1409. {
  1410. struct dasd_device *device;
  1411. dasd_erp_fn_t erp_fn;
  1412. if (cqr->status == DASD_CQR_FILLED)
  1413. return 0;
  1414. device = cqr->startdev;
  1415. if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
  1416. if (cqr->status == DASD_CQR_TERMINATED) {
  1417. device->discipline->handle_terminated_request(cqr);
  1418. return 1;
  1419. }
  1420. if (cqr->status == DASD_CQR_NEED_ERP) {
  1421. erp_fn = device->discipline->erp_action(cqr);
  1422. erp_fn(cqr);
  1423. return 1;
  1424. }
  1425. if (cqr->status == DASD_CQR_FAILED)
  1426. dasd_log_sense(cqr, &cqr->irb);
  1427. if (cqr->refers) {
  1428. __dasd_process_erp(device, cqr);
  1429. return 1;
  1430. }
  1431. }
  1432. return 0;
  1433. }
  1434. static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
  1435. {
  1436. if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
  1437. if (cqr->refers) /* erp is not done yet */
  1438. return 1;
  1439. return ((cqr->status != DASD_CQR_DONE) &&
  1440. (cqr->status != DASD_CQR_FAILED));
  1441. } else
  1442. return (cqr->status == DASD_CQR_FILLED);
  1443. }
  1444. static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
  1445. {
  1446. struct dasd_device *device;
  1447. int rc;
  1448. struct list_head ccw_queue;
  1449. struct dasd_ccw_req *cqr;
  1450. INIT_LIST_HEAD(&ccw_queue);
  1451. maincqr->status = DASD_CQR_FILLED;
  1452. device = maincqr->startdev;
  1453. list_add(&maincqr->blocklist, &ccw_queue);
  1454. for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
  1455. cqr = list_first_entry(&ccw_queue,
  1456. struct dasd_ccw_req, blocklist)) {
  1457. if (__dasd_sleep_on_erp(cqr))
  1458. continue;
  1459. if (cqr->status != DASD_CQR_FILLED) /* could be failed */
  1460. continue;
  1461. /* Non-temporary stop condition will trigger fail fast */
  1462. if (device->stopped & ~DASD_STOPPED_PENDING &&
  1463. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1464. (!dasd_eer_enabled(device))) {
  1465. cqr->status = DASD_CQR_FAILED;
  1466. continue;
  1467. }
  1468. /* Don't try to start requests if device is stopped */
  1469. if (interruptible) {
  1470. rc = wait_event_interruptible(
  1471. generic_waitq, !(device->stopped));
  1472. if (rc == -ERESTARTSYS) {
  1473. cqr->status = DASD_CQR_FAILED;
  1474. maincqr->intrc = rc;
  1475. continue;
  1476. }
  1477. } else
  1478. wait_event(generic_waitq, !(device->stopped));
  1479. cqr->callback = dasd_wakeup_cb;
  1480. cqr->callback_data = DASD_SLEEPON_START_TAG;
  1481. dasd_add_request_tail(cqr);
  1482. if (interruptible) {
  1483. rc = wait_event_interruptible(
  1484. generic_waitq, _wait_for_wakeup(cqr));
  1485. if (rc == -ERESTARTSYS) {
  1486. dasd_cancel_req(cqr);
  1487. /* wait (non-interruptible) for final status */
  1488. wait_event(generic_waitq,
  1489. _wait_for_wakeup(cqr));
  1490. cqr->status = DASD_CQR_FAILED;
  1491. maincqr->intrc = rc;
  1492. continue;
  1493. }
  1494. } else
  1495. wait_event(generic_waitq, _wait_for_wakeup(cqr));
  1496. }
  1497. maincqr->endclk = get_clock();
  1498. if ((maincqr->status != DASD_CQR_DONE) &&
  1499. (maincqr->intrc != -ERESTARTSYS))
  1500. dasd_log_sense(maincqr, &maincqr->irb);
  1501. if (maincqr->status == DASD_CQR_DONE)
  1502. rc = 0;
  1503. else if (maincqr->intrc)
  1504. rc = maincqr->intrc;
  1505. else
  1506. rc = -EIO;
  1507. return rc;
  1508. }
  1509. /*
  1510. * Queue a request to the tail of the device ccw_queue and wait for
  1511. * it's completion.
  1512. */
  1513. int dasd_sleep_on(struct dasd_ccw_req *cqr)
  1514. {
  1515. return _dasd_sleep_on(cqr, 0);
  1516. }
  1517. /*
  1518. * Queue a request to the tail of the device ccw_queue and wait
  1519. * interruptible for it's completion.
  1520. */
  1521. int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
  1522. {
  1523. return _dasd_sleep_on(cqr, 1);
  1524. }
  1525. /*
  1526. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  1527. * for eckd devices) the currently running request has to be terminated
  1528. * and be put back to status queued, before the special request is added
  1529. * to the head of the queue. Then the special request is waited on normally.
  1530. */
  1531. static inline int _dasd_term_running_cqr(struct dasd_device *device)
  1532. {
  1533. struct dasd_ccw_req *cqr;
  1534. if (list_empty(&device->ccw_queue))
  1535. return 0;
  1536. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1537. return device->discipline->term_IO(cqr);
  1538. }
  1539. int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
  1540. {
  1541. struct dasd_device *device;
  1542. int rc;
  1543. device = cqr->startdev;
  1544. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1545. rc = _dasd_term_running_cqr(device);
  1546. if (rc) {
  1547. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1548. return rc;
  1549. }
  1550. cqr->callback = dasd_wakeup_cb;
  1551. cqr->callback_data = DASD_SLEEPON_START_TAG;
  1552. cqr->status = DASD_CQR_QUEUED;
  1553. list_add(&cqr->devlist, &device->ccw_queue);
  1554. /* let the bh start the request to keep them in order */
  1555. dasd_schedule_device_bh(device);
  1556. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1557. wait_event(generic_waitq, _wait_for_wakeup(cqr));
  1558. if (cqr->status == DASD_CQR_DONE)
  1559. rc = 0;
  1560. else if (cqr->intrc)
  1561. rc = cqr->intrc;
  1562. else
  1563. rc = -EIO;
  1564. return rc;
  1565. }
  1566. /*
  1567. * Cancels a request that was started with dasd_sleep_on_req.
  1568. * This is useful to timeout requests. The request will be
  1569. * terminated if it is currently in i/o.
  1570. * Returns 1 if the request has been terminated.
  1571. * 0 if there was no need to terminate the request (not started yet)
  1572. * negative error code if termination failed
  1573. * Cancellation of a request is an asynchronous operation! The calling
  1574. * function has to wait until the request is properly returned via callback.
  1575. */
  1576. int dasd_cancel_req(struct dasd_ccw_req *cqr)
  1577. {
  1578. struct dasd_device *device = cqr->startdev;
  1579. unsigned long flags;
  1580. int rc;
  1581. rc = 0;
  1582. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1583. switch (cqr->status) {
  1584. case DASD_CQR_QUEUED:
  1585. /* request was not started - just set to cleared */
  1586. cqr->status = DASD_CQR_CLEARED;
  1587. break;
  1588. case DASD_CQR_IN_IO:
  1589. /* request in IO - terminate IO and release again */
  1590. rc = device->discipline->term_IO(cqr);
  1591. if (rc) {
  1592. dev_err(&device->cdev->dev,
  1593. "Cancelling request %p failed with rc=%d\n",
  1594. cqr, rc);
  1595. } else {
  1596. cqr->stopclk = get_clock();
  1597. }
  1598. break;
  1599. default: /* already finished or clear pending - do nothing */
  1600. break;
  1601. }
  1602. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1603. dasd_schedule_device_bh(device);
  1604. return rc;
  1605. }
  1606. /*
  1607. * SECTION: Operations of the dasd_block layer.
  1608. */
  1609. /*
  1610. * Timeout function for dasd_block. This is used when the block layer
  1611. * is waiting for something that may not come reliably, (e.g. a state
  1612. * change interrupt)
  1613. */
  1614. static void dasd_block_timeout(unsigned long ptr)
  1615. {
  1616. unsigned long flags;
  1617. struct dasd_block *block;
  1618. block = (struct dasd_block *) ptr;
  1619. spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
  1620. /* re-activate request queue */
  1621. dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
  1622. spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
  1623. dasd_schedule_block_bh(block);
  1624. }
  1625. /*
  1626. * Setup timeout for a dasd_block in jiffies.
  1627. */
  1628. void dasd_block_set_timer(struct dasd_block *block, int expires)
  1629. {
  1630. if (expires == 0)
  1631. del_timer(&block->timer);
  1632. else
  1633. mod_timer(&block->timer, jiffies + expires);
  1634. }
  1635. /*
  1636. * Clear timeout for a dasd_block.
  1637. */
  1638. void dasd_block_clear_timer(struct dasd_block *block)
  1639. {
  1640. del_timer(&block->timer);
  1641. }
  1642. /*
  1643. * Process finished error recovery ccw.
  1644. */
  1645. static void __dasd_process_erp(struct dasd_device *device,
  1646. struct dasd_ccw_req *cqr)
  1647. {
  1648. dasd_erp_fn_t erp_fn;
  1649. if (cqr->status == DASD_CQR_DONE)
  1650. DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
  1651. else
  1652. dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
  1653. erp_fn = device->discipline->erp_postaction(cqr);
  1654. erp_fn(cqr);
  1655. }
  1656. /*
  1657. * Fetch requests from the block device queue.
  1658. */
  1659. static void __dasd_process_request_queue(struct dasd_block *block)
  1660. {
  1661. struct request_queue *queue;
  1662. struct request *req;
  1663. struct dasd_ccw_req *cqr;
  1664. struct dasd_device *basedev;
  1665. unsigned long flags;
  1666. queue = block->request_queue;
  1667. basedev = block->base;
  1668. /* No queue ? Then there is nothing to do. */
  1669. if (queue == NULL)
  1670. return;
  1671. /*
  1672. * We requeue request from the block device queue to the ccw
  1673. * queue only in two states. In state DASD_STATE_READY the
  1674. * partition detection is done and we need to requeue requests
  1675. * for that. State DASD_STATE_ONLINE is normal block device
  1676. * operation.
  1677. */
  1678. if (basedev->state < DASD_STATE_READY) {
  1679. while ((req = blk_fetch_request(block->request_queue)))
  1680. __blk_end_request_all(req, -EIO);
  1681. return;
  1682. }
  1683. /* Now we try to fetch requests from the request queue */
  1684. while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
  1685. if (basedev->features & DASD_FEATURE_READONLY &&
  1686. rq_data_dir(req) == WRITE) {
  1687. DBF_DEV_EVENT(DBF_ERR, basedev,
  1688. "Rejecting write request %p",
  1689. req);
  1690. blk_start_request(req);
  1691. __blk_end_request_all(req, -EIO);
  1692. continue;
  1693. }
  1694. cqr = basedev->discipline->build_cp(basedev, block, req);
  1695. if (IS_ERR(cqr)) {
  1696. if (PTR_ERR(cqr) == -EBUSY)
  1697. break; /* normal end condition */
  1698. if (PTR_ERR(cqr) == -ENOMEM)
  1699. break; /* terminate request queue loop */
  1700. if (PTR_ERR(cqr) == -EAGAIN) {
  1701. /*
  1702. * The current request cannot be build right
  1703. * now, we have to try later. If this request
  1704. * is the head-of-queue we stop the device
  1705. * for 1/2 second.
  1706. */
  1707. if (!list_empty(&block->ccw_queue))
  1708. break;
  1709. spin_lock_irqsave(
  1710. get_ccwdev_lock(basedev->cdev), flags);
  1711. dasd_device_set_stop_bits(basedev,
  1712. DASD_STOPPED_PENDING);
  1713. spin_unlock_irqrestore(
  1714. get_ccwdev_lock(basedev->cdev), flags);
  1715. dasd_block_set_timer(block, HZ/2);
  1716. break;
  1717. }
  1718. DBF_DEV_EVENT(DBF_ERR, basedev,
  1719. "CCW creation failed (rc=%ld) "
  1720. "on request %p",
  1721. PTR_ERR(cqr), req);
  1722. blk_start_request(req);
  1723. __blk_end_request_all(req, -EIO);
  1724. continue;
  1725. }
  1726. /*
  1727. * Note: callback is set to dasd_return_cqr_cb in
  1728. * __dasd_block_start_head to cover erp requests as well
  1729. */
  1730. cqr->callback_data = (void *) req;
  1731. cqr->status = DASD_CQR_FILLED;
  1732. blk_start_request(req);
  1733. list_add_tail(&cqr->blocklist, &block->ccw_queue);
  1734. dasd_profile_start(block, cqr, req);
  1735. }
  1736. }
  1737. static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
  1738. {
  1739. struct request *req;
  1740. int status;
  1741. int error = 0;
  1742. req = (struct request *) cqr->callback_data;
  1743. dasd_profile_end(cqr->block, cqr, req);
  1744. status = cqr->block->base->discipline->free_cp(cqr, req);
  1745. if (status <= 0)
  1746. error = status ? status : -EIO;
  1747. __blk_end_request_all(req, error);
  1748. }
  1749. /*
  1750. * Process ccw request queue.
  1751. */
  1752. static void __dasd_process_block_ccw_queue(struct dasd_block *block,
  1753. struct list_head *final_queue)
  1754. {
  1755. struct list_head *l, *n;
  1756. struct dasd_ccw_req *cqr;
  1757. dasd_erp_fn_t erp_fn;
  1758. unsigned long flags;
  1759. struct dasd_device *base = block->base;
  1760. restart:
  1761. /* Process request with final status. */
  1762. list_for_each_safe(l, n, &block->ccw_queue) {
  1763. cqr = list_entry(l, struct dasd_ccw_req, blocklist);
  1764. if (cqr->status != DASD_CQR_DONE &&
  1765. cqr->status != DASD_CQR_FAILED &&
  1766. cqr->status != DASD_CQR_NEED_ERP &&
  1767. cqr->status != DASD_CQR_TERMINATED)
  1768. continue;
  1769. if (cqr->status == DASD_CQR_TERMINATED) {
  1770. base->discipline->handle_terminated_request(cqr);
  1771. goto restart;
  1772. }
  1773. /* Process requests that may be recovered */
  1774. if (cqr->status == DASD_CQR_NEED_ERP) {
  1775. erp_fn = base->discipline->erp_action(cqr);
  1776. if (IS_ERR(erp_fn(cqr)))
  1777. continue;
  1778. goto restart;
  1779. }
  1780. /* log sense for fatal error */
  1781. if (cqr->status == DASD_CQR_FAILED) {
  1782. dasd_log_sense(cqr, &cqr->irb);
  1783. }
  1784. /* First of all call extended error reporting. */
  1785. if (dasd_eer_enabled(base) &&
  1786. cqr->status == DASD_CQR_FAILED) {
  1787. dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
  1788. /* restart request */
  1789. cqr->status = DASD_CQR_FILLED;
  1790. cqr->retries = 255;
  1791. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  1792. dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
  1793. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
  1794. flags);
  1795. goto restart;
  1796. }
  1797. /* Process finished ERP request. */
  1798. if (cqr->refers) {
  1799. __dasd_process_erp(base, cqr);
  1800. goto restart;
  1801. }
  1802. /* Rechain finished requests to final queue */
  1803. cqr->endclk = get_clock();
  1804. list_move_tail(&cqr->blocklist, final_queue);
  1805. }
  1806. }
  1807. static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
  1808. {
  1809. dasd_schedule_block_bh(cqr->block);
  1810. }
  1811. static void __dasd_block_start_head(struct dasd_block *block)
  1812. {
  1813. struct dasd_ccw_req *cqr;
  1814. if (list_empty(&block->ccw_queue))
  1815. return;
  1816. /* We allways begin with the first requests on the queue, as some
  1817. * of previously started requests have to be enqueued on a
  1818. * dasd_device again for error recovery.
  1819. */
  1820. list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
  1821. if (cqr->status != DASD_CQR_FILLED)
  1822. continue;
  1823. /* Non-temporary stop condition will trigger fail fast */
  1824. if (block->base->stopped & ~DASD_STOPPED_PENDING &&
  1825. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1826. (!dasd_eer_enabled(block->base))) {
  1827. cqr->status = DASD_CQR_FAILED;
  1828. dasd_schedule_block_bh(block);
  1829. continue;
  1830. }
  1831. /* Don't try to start requests if device is stopped */
  1832. if (block->base->stopped)
  1833. return;
  1834. /* just a fail safe check, should not happen */
  1835. if (!cqr->startdev)
  1836. cqr->startdev = block->base;
  1837. /* make sure that the requests we submit find their way back */
  1838. cqr->callback = dasd_return_cqr_cb;
  1839. dasd_add_request_tail(cqr);
  1840. }
  1841. }
  1842. /*
  1843. * Central dasd_block layer routine. Takes requests from the generic
  1844. * block layer request queue, creates ccw requests, enqueues them on
  1845. * a dasd_device and processes ccw requests that have been returned.
  1846. */
  1847. static void dasd_block_tasklet(struct dasd_block *block)
  1848. {
  1849. struct list_head final_queue;
  1850. struct list_head *l, *n;
  1851. struct dasd_ccw_req *cqr;
  1852. atomic_set(&block->tasklet_scheduled, 0);
  1853. INIT_LIST_HEAD(&final_queue);
  1854. spin_lock(&block->queue_lock);
  1855. /* Finish off requests on ccw queue */
  1856. __dasd_process_block_ccw_queue(block, &final_queue);
  1857. spin_unlock(&block->queue_lock);
  1858. /* Now call the callback function of requests with final status */
  1859. spin_lock_irq(&block->request_queue_lock);
  1860. list_for_each_safe(l, n, &final_queue) {
  1861. cqr = list_entry(l, struct dasd_ccw_req, blocklist);
  1862. list_del_init(&cqr->blocklist);
  1863. __dasd_cleanup_cqr(cqr);
  1864. }
  1865. spin_lock(&block->queue_lock);
  1866. /* Get new request from the block device request queue */
  1867. __dasd_process_request_queue(block);
  1868. /* Now check if the head of the ccw queue needs to be started. */
  1869. __dasd_block_start_head(block);
  1870. spin_unlock(&block->queue_lock);
  1871. spin_unlock_irq(&block->request_queue_lock);
  1872. dasd_put_device(block->base);
  1873. }
  1874. static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
  1875. {
  1876. wake_up(&dasd_flush_wq);
  1877. }
  1878. /*
  1879. * Go through all request on the dasd_block request queue, cancel them
  1880. * on the respective dasd_device, and return them to the generic
  1881. * block layer.
  1882. */
  1883. static int dasd_flush_block_queue(struct dasd_block *block)
  1884. {
  1885. struct dasd_ccw_req *cqr, *n;
  1886. int rc, i;
  1887. struct list_head flush_queue;
  1888. INIT_LIST_HEAD(&flush_queue);
  1889. spin_lock_bh(&block->queue_lock);
  1890. rc = 0;
  1891. restart:
  1892. list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
  1893. /* if this request currently owned by a dasd_device cancel it */
  1894. if (cqr->status >= DASD_CQR_QUEUED)
  1895. rc = dasd_cancel_req(cqr);
  1896. if (rc < 0)
  1897. break;
  1898. /* Rechain request (including erp chain) so it won't be
  1899. * touched by the dasd_block_tasklet anymore.
  1900. * Replace the callback so we notice when the request
  1901. * is returned from the dasd_device layer.
  1902. */
  1903. cqr->callback = _dasd_wake_block_flush_cb;
  1904. for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
  1905. list_move_tail(&cqr->blocklist, &flush_queue);
  1906. if (i > 1)
  1907. /* moved more than one request - need to restart */
  1908. goto restart;
  1909. }
  1910. spin_unlock_bh(&block->queue_lock);
  1911. /* Now call the callback function of flushed requests */
  1912. restart_cb:
  1913. list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
  1914. wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
  1915. /* Process finished ERP request. */
  1916. if (cqr->refers) {
  1917. spin_lock_bh(&block->queue_lock);
  1918. __dasd_process_erp(block->base, cqr);
  1919. spin_unlock_bh(&block->queue_lock);
  1920. /* restart list_for_xx loop since dasd_process_erp
  1921. * might remove multiple elements */
  1922. goto restart_cb;
  1923. }
  1924. /* call the callback function */
  1925. spin_lock_irq(&block->request_queue_lock);
  1926. cqr->endclk = get_clock();
  1927. list_del_init(&cqr->blocklist);
  1928. __dasd_cleanup_cqr(cqr);
  1929. spin_unlock_irq(&block->request_queue_lock);
  1930. }
  1931. return rc;
  1932. }
  1933. /*
  1934. * Schedules a call to dasd_tasklet over the device tasklet.
  1935. */
  1936. void dasd_schedule_block_bh(struct dasd_block *block)
  1937. {
  1938. /* Protect against rescheduling. */
  1939. if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
  1940. return;
  1941. /* life cycle of block is bound to it's base device */
  1942. dasd_get_device(block->base);
  1943. tasklet_hi_schedule(&block->tasklet);
  1944. }
  1945. /*
  1946. * SECTION: external block device operations
  1947. * (request queue handling, open, release, etc.)
  1948. */
  1949. /*
  1950. * Dasd request queue function. Called from ll_rw_blk.c
  1951. */
  1952. static void do_dasd_request(struct request_queue *queue)
  1953. {
  1954. struct dasd_block *block;
  1955. block = queue->queuedata;
  1956. spin_lock(&block->queue_lock);
  1957. /* Get new request from the block device request queue */
  1958. __dasd_process_request_queue(block);
  1959. /* Now check if the head of the ccw queue needs to be started. */
  1960. __dasd_block_start_head(block);
  1961. spin_unlock(&block->queue_lock);
  1962. }
  1963. /*
  1964. * Allocate and initialize request queue and default I/O scheduler.
  1965. */
  1966. static int dasd_alloc_queue(struct dasd_block *block)
  1967. {
  1968. int rc;
  1969. block->request_queue = blk_init_queue(do_dasd_request,
  1970. &block->request_queue_lock);
  1971. if (block->request_queue == NULL)
  1972. return -ENOMEM;
  1973. block->request_queue->queuedata = block;
  1974. elevator_exit(block->request_queue->elevator);
  1975. block->request_queue->elevator = NULL;
  1976. rc = elevator_init(block->request_queue, "deadline");
  1977. if (rc) {
  1978. blk_cleanup_queue(block->request_queue);
  1979. return rc;
  1980. }
  1981. return 0;
  1982. }
  1983. /*
  1984. * Allocate and initialize request queue.
  1985. */
  1986. static void dasd_setup_queue(struct dasd_block *block)
  1987. {
  1988. int max;
  1989. blk_queue_logical_block_size(block->request_queue, block->bp_block);
  1990. max = block->base->discipline->max_blocks << block->s2b_shift;
  1991. blk_queue_max_hw_sectors(block->request_queue, max);
  1992. blk_queue_max_segments(block->request_queue, -1L);
  1993. /* with page sized segments we can translate each segement into
  1994. * one idaw/tidaw
  1995. */
  1996. blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
  1997. blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
  1998. }
  1999. /*
  2000. * Deactivate and free request queue.
  2001. */
  2002. static void dasd_free_queue(struct dasd_block *block)
  2003. {
  2004. if (block->request_queue) {
  2005. blk_cleanup_queue(block->request_queue);
  2006. block->request_queue = NULL;
  2007. }
  2008. }
  2009. /*
  2010. * Flush request on the request queue.
  2011. */
  2012. static void dasd_flush_request_queue(struct dasd_block *block)
  2013. {
  2014. struct request *req;
  2015. if (!block->request_queue)
  2016. return;
  2017. spin_lock_irq(&block->request_queue_lock);
  2018. while ((req = blk_fetch_request(block->request_queue)))
  2019. __blk_end_request_all(req, -EIO);
  2020. spin_unlock_irq(&block->request_queue_lock);
  2021. }
  2022. static int dasd_open(struct block_device *bdev, fmode_t mode)
  2023. {
  2024. struct dasd_block *block = bdev->bd_disk->private_data;
  2025. struct dasd_device *base;
  2026. int rc;
  2027. if (!block)
  2028. return -ENODEV;
  2029. base = block->base;
  2030. atomic_inc(&block->open_count);
  2031. if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
  2032. rc = -ENODEV;
  2033. goto unlock;
  2034. }
  2035. if (!try_module_get(base->discipline->owner)) {
  2036. rc = -EINVAL;
  2037. goto unlock;
  2038. }
  2039. if (dasd_probeonly) {
  2040. dev_info(&base->cdev->dev,
  2041. "Accessing the DASD failed because it is in "
  2042. "probeonly mode\n");
  2043. rc = -EPERM;
  2044. goto out;
  2045. }
  2046. if (base->state <= DASD_STATE_BASIC) {
  2047. DBF_DEV_EVENT(DBF_ERR, base, " %s",
  2048. " Cannot open unrecognized device");
  2049. rc = -ENODEV;
  2050. goto out;
  2051. }
  2052. if ((mode & FMODE_WRITE) &&
  2053. (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
  2054. (base->features & DASD_FEATURE_READONLY))) {
  2055. rc = -EROFS;
  2056. goto out;
  2057. }
  2058. return 0;
  2059. out:
  2060. module_put(base->discipline->owner);
  2061. unlock:
  2062. atomic_dec(&block->open_count);
  2063. return rc;
  2064. }
  2065. static int dasd_release(struct gendisk *disk, fmode_t mode)
  2066. {
  2067. struct dasd_block *block = disk->private_data;
  2068. atomic_dec(&block->open_count);
  2069. module_put(block->base->discipline->owner);
  2070. return 0;
  2071. }
  2072. /*
  2073. * Return disk geometry.
  2074. */
  2075. static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  2076. {
  2077. struct dasd_block *block;
  2078. struct dasd_device *base;
  2079. block = bdev->bd_disk->private_data;
  2080. if (!block)
  2081. return -ENODEV;
  2082. base = block->base;
  2083. if (!base->discipline ||
  2084. !base->discipline->fill_geometry)
  2085. return -EINVAL;
  2086. base->discipline->fill_geometry(block, geo);
  2087. geo->start = get_start_sect(bdev) >> block->s2b_shift;
  2088. return 0;
  2089. }
  2090. const struct block_device_operations
  2091. dasd_device_operations = {
  2092. .owner = THIS_MODULE,
  2093. .open = dasd_open,
  2094. .release = dasd_release,
  2095. .ioctl = dasd_ioctl,
  2096. .compat_ioctl = dasd_ioctl,
  2097. .getgeo = dasd_getgeo,
  2098. };
  2099. /*******************************************************************************
  2100. * end of block device operations
  2101. */
  2102. static void
  2103. dasd_exit(void)
  2104. {
  2105. #ifdef CONFIG_PROC_FS
  2106. dasd_proc_exit();
  2107. #endif
  2108. dasd_eer_exit();
  2109. if (dasd_page_cache != NULL) {
  2110. kmem_cache_destroy(dasd_page_cache);
  2111. dasd_page_cache = NULL;
  2112. }
  2113. dasd_gendisk_exit();
  2114. dasd_devmap_exit();
  2115. if (dasd_debug_area != NULL) {
  2116. debug_unregister(dasd_debug_area);
  2117. dasd_debug_area = NULL;
  2118. }
  2119. }
  2120. /*
  2121. * SECTION: common functions for ccw_driver use
  2122. */
  2123. /*
  2124. * Is the device read-only?
  2125. * Note that this function does not report the setting of the
  2126. * readonly device attribute, but how it is configured in z/VM.
  2127. */
  2128. int dasd_device_is_ro(struct dasd_device *device)
  2129. {
  2130. struct ccw_dev_id dev_id;
  2131. struct diag210 diag_data;
  2132. int rc;
  2133. if (!MACHINE_IS_VM)
  2134. return 0;
  2135. ccw_device_get_id(device->cdev, &dev_id);
  2136. memset(&diag_data, 0, sizeof(diag_data));
  2137. diag_data.vrdcdvno = dev_id.devno;
  2138. diag_data.vrdclen = sizeof(diag_data);
  2139. rc = diag210(&diag_data);
  2140. if (rc == 0 || rc == 2) {
  2141. return diag_data.vrdcvfla & 0x80;
  2142. } else {
  2143. DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
  2144. dev_id.devno, rc);
  2145. return 0;
  2146. }
  2147. }
  2148. EXPORT_SYMBOL_GPL(dasd_device_is_ro);
  2149. static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
  2150. {
  2151. struct ccw_device *cdev = data;
  2152. int ret;
  2153. ret = ccw_device_set_online(cdev);
  2154. if (ret)
  2155. pr_warning("%s: Setting the DASD online failed with rc=%d\n",
  2156. dev_name(&cdev->dev), ret);
  2157. }
  2158. /*
  2159. * Initial attempt at a probe function. this can be simplified once
  2160. * the other detection code is gone.
  2161. */
  2162. int dasd_generic_probe(struct ccw_device *cdev,
  2163. struct dasd_discipline *discipline)
  2164. {
  2165. int ret;
  2166. ret = dasd_add_sysfs_files(cdev);
  2167. if (ret) {
  2168. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
  2169. "dasd_generic_probe: could not add "
  2170. "sysfs entries");
  2171. return ret;
  2172. }
  2173. cdev->handler = &dasd_int_handler;
  2174. /*
  2175. * Automatically online either all dasd devices (dasd_autodetect)
  2176. * or all devices specified with dasd= parameters during
  2177. * initial probe.
  2178. */
  2179. if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
  2180. (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
  2181. async_schedule(dasd_generic_auto_online, cdev);
  2182. return 0;
  2183. }
  2184. /*
  2185. * This will one day be called from a global not_oper handler.
  2186. * It is also used by driver_unregister during module unload.
  2187. */
  2188. void dasd_generic_remove(struct ccw_device *cdev)
  2189. {
  2190. struct dasd_device *device;
  2191. struct dasd_block *block;
  2192. cdev->handler = NULL;
  2193. dasd_remove_sysfs_files(cdev);
  2194. device = dasd_device_from_cdev(cdev);
  2195. if (IS_ERR(device))
  2196. return;
  2197. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  2198. /* Already doing offline processing */
  2199. dasd_put_device(device);
  2200. return;
  2201. }
  2202. /*
  2203. * This device is removed unconditionally. Set offline
  2204. * flag to prevent dasd_open from opening it while it is
  2205. * no quite down yet.
  2206. */
  2207. dasd_set_target_state(device, DASD_STATE_NEW);
  2208. /* dasd_delete_device destroys the device reference. */
  2209. block = device->block;
  2210. device->block = NULL;
  2211. dasd_delete_device(device);
  2212. /*
  2213. * life cycle of block is bound to device, so delete it after
  2214. * device was safely removed
  2215. */
  2216. if (block)
  2217. dasd_free_block(block);
  2218. }
  2219. /*
  2220. * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
  2221. * the device is detected for the first time and is supposed to be used
  2222. * or the user has started activation through sysfs.
  2223. */
  2224. int dasd_generic_set_online(struct ccw_device *cdev,
  2225. struct dasd_discipline *base_discipline)
  2226. {
  2227. struct dasd_discipline *discipline;
  2228. struct dasd_device *device;
  2229. int rc;
  2230. /* first online clears initial online feature flag */
  2231. dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
  2232. device = dasd_create_device(cdev);
  2233. if (IS_ERR(device))
  2234. return PTR_ERR(device);
  2235. discipline = base_discipline;
  2236. if (device->features & DASD_FEATURE_USEDIAG) {
  2237. if (!dasd_diag_discipline_pointer) {
  2238. pr_warning("%s Setting the DASD online failed because "
  2239. "of missing DIAG discipline\n",
  2240. dev_name(&cdev->dev));
  2241. dasd_delete_device(device);
  2242. return -ENODEV;
  2243. }
  2244. discipline = dasd_diag_discipline_pointer;
  2245. }
  2246. if (!try_module_get(base_discipline->owner)) {
  2247. dasd_delete_device(device);
  2248. return -EINVAL;
  2249. }
  2250. if (!try_module_get(discipline->owner)) {
  2251. module_put(base_discipline->owner);
  2252. dasd_delete_device(device);
  2253. return -EINVAL;
  2254. }
  2255. device->base_discipline = base_discipline;
  2256. device->discipline = discipline;
  2257. /* check_device will allocate block device if necessary */
  2258. rc = discipline->check_device(device);
  2259. if (rc) {
  2260. pr_warning("%s Setting the DASD online with discipline %s "
  2261. "failed with rc=%i\n",
  2262. dev_name(&cdev->dev), discipline->name, rc);
  2263. module_put(discipline->owner);
  2264. module_put(base_discipline->owner);
  2265. dasd_delete_device(device);
  2266. return rc;
  2267. }
  2268. dasd_set_target_state(device, DASD_STATE_ONLINE);
  2269. if (device->state <= DASD_STATE_KNOWN) {
  2270. pr_warning("%s Setting the DASD online failed because of a "
  2271. "missing discipline\n", dev_name(&cdev->dev));
  2272. rc = -ENODEV;
  2273. dasd_set_target_state(device, DASD_STATE_NEW);
  2274. if (device->block)
  2275. dasd_free_block(device->block);
  2276. dasd_delete_device(device);
  2277. } else
  2278. pr_debug("dasd_generic device %s found\n",
  2279. dev_name(&cdev->dev));
  2280. wait_event(dasd_init_waitq, _wait_for_device(device));
  2281. dasd_put_device(device);
  2282. return rc;
  2283. }
  2284. int dasd_generic_set_offline(struct ccw_device *cdev)
  2285. {
  2286. struct dasd_device *device;
  2287. struct dasd_block *block;
  2288. int max_count, open_count;
  2289. device = dasd_device_from_cdev(cdev);
  2290. if (IS_ERR(device))
  2291. return PTR_ERR(device);
  2292. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  2293. /* Already doing offline processing */
  2294. dasd_put_device(device);
  2295. return 0;
  2296. }
  2297. /*
  2298. * We must make sure that this device is currently not in use.
  2299. * The open_count is increased for every opener, that includes
  2300. * the blkdev_get in dasd_scan_partitions. We are only interested
  2301. * in the other openers.
  2302. */
  2303. if (device->block) {
  2304. max_count = device->block->bdev ? 0 : -1;
  2305. open_count = atomic_read(&device->block->open_count);
  2306. if (open_count > max_count) {
  2307. if (open_count > 0)
  2308. pr_warning("%s: The DASD cannot be set offline "
  2309. "with open count %i\n",
  2310. dev_name(&cdev->dev), open_count);
  2311. else
  2312. pr_warning("%s: The DASD cannot be set offline "
  2313. "while it is in use\n",
  2314. dev_name(&cdev->dev));
  2315. clear_bit(DASD_FLAG_OFFLINE, &device->flags);
  2316. dasd_put_device(device);
  2317. return -EBUSY;
  2318. }
  2319. }
  2320. dasd_set_target_state(device, DASD_STATE_NEW);
  2321. /* dasd_delete_device destroys the device reference. */
  2322. block = device->block;
  2323. device->block = NULL;
  2324. dasd_delete_device(device);
  2325. /*
  2326. * life cycle of block is bound to device, so delete it after
  2327. * device was safely removed
  2328. */
  2329. if (block)
  2330. dasd_free_block(block);
  2331. return 0;
  2332. }
  2333. int dasd_generic_notify(struct ccw_device *cdev, int event)
  2334. {
  2335. struct dasd_device *device;
  2336. struct dasd_ccw_req *cqr;
  2337. int ret;
  2338. device = dasd_device_from_cdev_locked(cdev);
  2339. if (IS_ERR(device))
  2340. return 0;
  2341. ret = 0;
  2342. switch (event) {
  2343. case CIO_GONE:
  2344. case CIO_BOXED:
  2345. case CIO_NO_PATH:
  2346. /* First of all call extended error reporting. */
  2347. dasd_eer_write(device, NULL, DASD_EER_NOPATH);
  2348. if (device->state < DASD_STATE_BASIC)
  2349. break;
  2350. /* Device is active. We want to keep it. */
  2351. list_for_each_entry(cqr, &device->ccw_queue, devlist)
  2352. if (cqr->status == DASD_CQR_IN_IO) {
  2353. cqr->status = DASD_CQR_QUEUED;
  2354. cqr->retries++;
  2355. }
  2356. dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
  2357. dasd_device_clear_timer(device);
  2358. dasd_schedule_device_bh(device);
  2359. ret = 1;
  2360. break;
  2361. case CIO_OPER:
  2362. /* FIXME: add a sanity check. */
  2363. dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
  2364. if (device->stopped & DASD_UNRESUMED_PM) {
  2365. dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
  2366. dasd_restore_device(device);
  2367. ret = 1;
  2368. break;
  2369. }
  2370. dasd_schedule_device_bh(device);
  2371. if (device->block)
  2372. dasd_schedule_block_bh(device->block);
  2373. ret = 1;
  2374. break;
  2375. }
  2376. dasd_put_device(device);
  2377. return ret;
  2378. }
  2379. int dasd_generic_pm_freeze(struct ccw_device *cdev)
  2380. {
  2381. struct dasd_ccw_req *cqr, *n;
  2382. int rc;
  2383. struct list_head freeze_queue;
  2384. struct dasd_device *device = dasd_device_from_cdev(cdev);
  2385. if (IS_ERR(device))
  2386. return PTR_ERR(device);
  2387. /* disallow new I/O */
  2388. dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
  2389. /* clear active requests */
  2390. INIT_LIST_HEAD(&freeze_queue);
  2391. spin_lock_irq(get_ccwdev_lock(cdev));
  2392. rc = 0;
  2393. list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
  2394. /* Check status and move request to flush_queue */
  2395. if (cqr->status == DASD_CQR_IN_IO) {
  2396. rc = device->discipline->term_IO(cqr);
  2397. if (rc) {
  2398. /* unable to terminate requeust */
  2399. dev_err(&device->cdev->dev,
  2400. "Unable to terminate request %p "
  2401. "on suspend\n", cqr);
  2402. spin_unlock_irq(get_ccwdev_lock(cdev));
  2403. dasd_put_device(device);
  2404. return rc;
  2405. }
  2406. }
  2407. list_move_tail(&cqr->devlist, &freeze_queue);
  2408. }
  2409. spin_unlock_irq(get_ccwdev_lock(cdev));
  2410. list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
  2411. wait_event(dasd_flush_wq,
  2412. (cqr->status != DASD_CQR_CLEAR_PENDING));
  2413. if (cqr->status == DASD_CQR_CLEARED)
  2414. cqr->status = DASD_CQR_QUEUED;
  2415. }
  2416. /* move freeze_queue to start of the ccw_queue */
  2417. spin_lock_irq(get_ccwdev_lock(cdev));
  2418. list_splice_tail(&freeze_queue, &device->ccw_queue);
  2419. spin_unlock_irq(get_ccwdev_lock(cdev));
  2420. if (device->discipline->freeze)
  2421. rc = device->discipline->freeze(device);
  2422. dasd_put_device(device);
  2423. return rc;
  2424. }
  2425. EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
  2426. int dasd_generic_restore_device(struct ccw_device *cdev)
  2427. {
  2428. struct dasd_device *device = dasd_device_from_cdev(cdev);
  2429. int rc = 0;
  2430. if (IS_ERR(device))
  2431. return PTR_ERR(device);
  2432. /* allow new IO again */
  2433. dasd_device_remove_stop_bits(device,
  2434. (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
  2435. dasd_schedule_device_bh(device);
  2436. /*
  2437. * call discipline restore function
  2438. * if device is stopped do nothing e.g. for disconnected devices
  2439. */
  2440. if (device->discipline->restore && !(device->stopped))
  2441. rc = device->discipline->restore(device);
  2442. if (rc || device->stopped)
  2443. /*
  2444. * if the resume failed for the DASD we put it in
  2445. * an UNRESUMED stop state
  2446. */
  2447. device->stopped |= DASD_UNRESUMED_PM;
  2448. if (device->block)
  2449. dasd_schedule_block_bh(device->block);
  2450. dasd_put_device(device);
  2451. return 0;
  2452. }
  2453. EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
  2454. static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
  2455. void *rdc_buffer,
  2456. int rdc_buffer_size,
  2457. int magic)
  2458. {
  2459. struct dasd_ccw_req *cqr;
  2460. struct ccw1 *ccw;
  2461. unsigned long *idaw;
  2462. cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
  2463. if (IS_ERR(cqr)) {
  2464. /* internal error 13 - Allocating the RDC request failed*/
  2465. dev_err(&device->cdev->dev,
  2466. "An error occurred in the DASD device driver, "
  2467. "reason=%s\n", "13");
  2468. return cqr;
  2469. }
  2470. ccw = cqr->cpaddr;
  2471. ccw->cmd_code = CCW_CMD_RDC;
  2472. if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
  2473. idaw = (unsigned long *) (cqr->data);
  2474. ccw->cda = (__u32)(addr_t) idaw;
  2475. ccw->flags = CCW_FLAG_IDA;
  2476. idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
  2477. } else {
  2478. ccw->cda = (__u32)(addr_t) rdc_buffer;
  2479. ccw->flags = 0;
  2480. }
  2481. ccw->count = rdc_buffer_size;
  2482. cqr->startdev = device;
  2483. cqr->memdev = device;
  2484. cqr->expires = 10*HZ;
  2485. cqr->retries = 256;
  2486. cqr->buildclk = get_clock();
  2487. cqr->status = DASD_CQR_FILLED;
  2488. return cqr;
  2489. }
  2490. int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
  2491. void *rdc_buffer, int rdc_buffer_size)
  2492. {
  2493. int ret;
  2494. struct dasd_ccw_req *cqr;
  2495. cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
  2496. magic);
  2497. if (IS_ERR(cqr))
  2498. return PTR_ERR(cqr);
  2499. ret = dasd_sleep_on(cqr);
  2500. dasd_sfree_request(cqr, cqr->memdev);
  2501. return ret;
  2502. }
  2503. EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
  2504. /*
  2505. * In command mode and transport mode we need to look for sense
  2506. * data in different places. The sense data itself is allways
  2507. * an array of 32 bytes, so we can unify the sense data access
  2508. * for both modes.
  2509. */
  2510. char *dasd_get_sense(struct irb *irb)
  2511. {
  2512. struct tsb *tsb = NULL;
  2513. char *sense = NULL;
  2514. if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
  2515. if (irb->scsw.tm.tcw)
  2516. tsb = tcw_get_tsb((struct tcw *)(unsigned long)
  2517. irb->scsw.tm.tcw);
  2518. if (tsb && tsb->length == 64 && tsb->flags)
  2519. switch (tsb->flags & 0x07) {
  2520. case 1: /* tsa_iostat */
  2521. sense = tsb->tsa.iostat.sense;
  2522. break;
  2523. case 2: /* tsa_ddpc */
  2524. sense = tsb->tsa.ddpc.sense;
  2525. break;
  2526. default:
  2527. /* currently we don't use interrogate data */
  2528. break;
  2529. }
  2530. } else if (irb->esw.esw0.erw.cons) {
  2531. sense = irb->ecw;
  2532. }
  2533. return sense;
  2534. }
  2535. EXPORT_SYMBOL_GPL(dasd_get_sense);
  2536. static int __init dasd_init(void)
  2537. {
  2538. int rc;
  2539. init_waitqueue_head(&dasd_init_waitq);
  2540. init_waitqueue_head(&dasd_flush_wq);
  2541. init_waitqueue_head(&generic_waitq);
  2542. /* register 'common' DASD debug area, used for all DBF_XXX calls */
  2543. dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
  2544. if (dasd_debug_area == NULL) {
  2545. rc = -ENOMEM;
  2546. goto failed;
  2547. }
  2548. debug_register_view(dasd_debug_area, &debug_sprintf_view);
  2549. debug_set_level(dasd_debug_area, DBF_WARNING);
  2550. DBF_EVENT(DBF_EMERG, "%s", "debug area created");
  2551. dasd_diag_discipline_pointer = NULL;
  2552. rc = dasd_devmap_init();
  2553. if (rc)
  2554. goto failed;
  2555. rc = dasd_gendisk_init();
  2556. if (rc)
  2557. goto failed;
  2558. rc = dasd_parse();
  2559. if (rc)
  2560. goto failed;
  2561. rc = dasd_eer_init();
  2562. if (rc)
  2563. goto failed;
  2564. #ifdef CONFIG_PROC_FS
  2565. rc = dasd_proc_init();
  2566. if (rc)
  2567. goto failed;
  2568. #endif
  2569. return 0;
  2570. failed:
  2571. pr_info("The DASD device driver could not be initialized\n");
  2572. dasd_exit();
  2573. return rc;
  2574. }
  2575. module_init(dasd_init);
  2576. module_exit(dasd_exit);
  2577. EXPORT_SYMBOL(dasd_debug_area);
  2578. EXPORT_SYMBOL(dasd_diag_discipline_pointer);
  2579. EXPORT_SYMBOL(dasd_add_request_head);
  2580. EXPORT_SYMBOL(dasd_add_request_tail);
  2581. EXPORT_SYMBOL(dasd_cancel_req);
  2582. EXPORT_SYMBOL(dasd_device_clear_timer);
  2583. EXPORT_SYMBOL(dasd_block_clear_timer);
  2584. EXPORT_SYMBOL(dasd_enable_device);
  2585. EXPORT_SYMBOL(dasd_int_handler);
  2586. EXPORT_SYMBOL(dasd_kfree_request);
  2587. EXPORT_SYMBOL(dasd_kick_device);
  2588. EXPORT_SYMBOL(dasd_kmalloc_request);
  2589. EXPORT_SYMBOL(dasd_schedule_device_bh);
  2590. EXPORT_SYMBOL(dasd_schedule_block_bh);
  2591. EXPORT_SYMBOL(dasd_set_target_state);
  2592. EXPORT_SYMBOL(dasd_device_set_timer);
  2593. EXPORT_SYMBOL(dasd_block_set_timer);
  2594. EXPORT_SYMBOL(dasd_sfree_request);
  2595. EXPORT_SYMBOL(dasd_sleep_on);
  2596. EXPORT_SYMBOL(dasd_sleep_on_immediatly);
  2597. EXPORT_SYMBOL(dasd_sleep_on_interruptible);
  2598. EXPORT_SYMBOL(dasd_smalloc_request);
  2599. EXPORT_SYMBOL(dasd_start_IO);
  2600. EXPORT_SYMBOL(dasd_term_IO);
  2601. EXPORT_SYMBOL_GPL(dasd_generic_probe);
  2602. EXPORT_SYMBOL_GPL(dasd_generic_remove);
  2603. EXPORT_SYMBOL_GPL(dasd_generic_notify);
  2604. EXPORT_SYMBOL_GPL(dasd_generic_set_online);
  2605. EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
  2606. EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
  2607. EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
  2608. EXPORT_SYMBOL_GPL(dasd_alloc_block);
  2609. EXPORT_SYMBOL_GPL(dasd_free_block);