dasd.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  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. * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
  9. *
  10. */
  11. #include <linux/config.h>
  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 <asm/ccwdev.h>
  21. #include <asm/ebcdic.h>
  22. #include <asm/idals.h>
  23. #include <asm/todclk.h>
  24. /* This is ugly... */
  25. #define PRINTK_HEADER "dasd:"
  26. #include "dasd_int.h"
  27. /*
  28. * SECTION: Constant definitions to be used within this file
  29. */
  30. #define DASD_CHANQ_MAX_SIZE 4
  31. /*
  32. * SECTION: exported variables of dasd.c
  33. */
  34. debug_info_t *dasd_debug_area;
  35. struct dasd_discipline *dasd_diag_discipline_pointer;
  36. MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
  37. MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
  38. " Copyright 2000 IBM Corporation");
  39. MODULE_SUPPORTED_DEVICE("dasd");
  40. MODULE_LICENSE("GPL");
  41. /*
  42. * SECTION: prototypes for static functions of dasd.c
  43. */
  44. static int dasd_alloc_queue(struct dasd_device * device);
  45. static void dasd_setup_queue(struct dasd_device * device);
  46. static void dasd_free_queue(struct dasd_device * device);
  47. static void dasd_flush_request_queue(struct dasd_device *);
  48. static void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
  49. static void dasd_flush_ccw_queue(struct dasd_device *, int);
  50. static void dasd_tasklet(struct dasd_device *);
  51. static void do_kick_device(void *data);
  52. /*
  53. * SECTION: Operations on the device structure.
  54. */
  55. static wait_queue_head_t dasd_init_waitq;
  56. /*
  57. * Allocate memory for a new device structure.
  58. */
  59. struct dasd_device *
  60. dasd_alloc_device(void)
  61. {
  62. struct dasd_device *device;
  63. device = kzalloc(sizeof (struct dasd_device), GFP_ATOMIC);
  64. if (device == NULL)
  65. return ERR_PTR(-ENOMEM);
  66. /* open_count = 0 means device online but not in use */
  67. atomic_set(&device->open_count, -1);
  68. /* Get two pages for normal block device operations. */
  69. device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
  70. if (device->ccw_mem == NULL) {
  71. kfree(device);
  72. return ERR_PTR(-ENOMEM);
  73. }
  74. /* Get one page for error recovery. */
  75. device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
  76. if (device->erp_mem == NULL) {
  77. free_pages((unsigned long) device->ccw_mem, 1);
  78. kfree(device);
  79. return ERR_PTR(-ENOMEM);
  80. }
  81. dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
  82. dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
  83. spin_lock_init(&device->mem_lock);
  84. spin_lock_init(&device->request_queue_lock);
  85. atomic_set (&device->tasklet_scheduled, 0);
  86. tasklet_init(&device->tasklet,
  87. (void (*)(unsigned long)) dasd_tasklet,
  88. (unsigned long) device);
  89. INIT_LIST_HEAD(&device->ccw_queue);
  90. init_timer(&device->timer);
  91. INIT_WORK(&device->kick_work, do_kick_device, device);
  92. device->state = DASD_STATE_NEW;
  93. device->target = DASD_STATE_NEW;
  94. return device;
  95. }
  96. /*
  97. * Free memory of a device structure.
  98. */
  99. void
  100. dasd_free_device(struct dasd_device *device)
  101. {
  102. kfree(device->private);
  103. free_page((unsigned long) device->erp_mem);
  104. free_pages((unsigned long) device->ccw_mem, 1);
  105. kfree(device);
  106. }
  107. /*
  108. * Make a new device known to the system.
  109. */
  110. static inline int
  111. dasd_state_new_to_known(struct dasd_device *device)
  112. {
  113. int rc;
  114. /*
  115. * As long as the device is not in state DASD_STATE_NEW we want to
  116. * keep the reference count > 0.
  117. */
  118. dasd_get_device(device);
  119. rc = dasd_alloc_queue(device);
  120. if (rc) {
  121. dasd_put_device(device);
  122. return rc;
  123. }
  124. device->state = DASD_STATE_KNOWN;
  125. return 0;
  126. }
  127. /*
  128. * Let the system forget about a device.
  129. */
  130. static inline void
  131. dasd_state_known_to_new(struct dasd_device * device)
  132. {
  133. /* Disable extended error reporting for this device. */
  134. dasd_eer_disable(device);
  135. /* Forget the discipline information. */
  136. if (device->discipline)
  137. module_put(device->discipline->owner);
  138. device->discipline = NULL;
  139. if (device->base_discipline)
  140. module_put(device->base_discipline->owner);
  141. device->base_discipline = NULL;
  142. device->state = DASD_STATE_NEW;
  143. dasd_free_queue(device);
  144. /* Give up reference we took in dasd_state_new_to_known. */
  145. dasd_put_device(device);
  146. }
  147. /*
  148. * Request the irq line for the device.
  149. */
  150. static inline int
  151. dasd_state_known_to_basic(struct dasd_device * device)
  152. {
  153. int rc;
  154. /* Allocate and register gendisk structure. */
  155. rc = dasd_gendisk_alloc(device);
  156. if (rc)
  157. return rc;
  158. /* register 'device' debug area, used for all DBF_DEV_XXX calls */
  159. device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
  160. 8 * sizeof (long));
  161. debug_register_view(device->debug_area, &debug_sprintf_view);
  162. debug_set_level(device->debug_area, DBF_EMERG);
  163. DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
  164. device->state = DASD_STATE_BASIC;
  165. return 0;
  166. }
  167. /*
  168. * Release the irq line for the device. Terminate any running i/o.
  169. */
  170. static inline void
  171. dasd_state_basic_to_known(struct dasd_device * device)
  172. {
  173. dasd_gendisk_free(device);
  174. dasd_flush_ccw_queue(device, 1);
  175. DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
  176. if (device->debug_area != NULL) {
  177. debug_unregister(device->debug_area);
  178. device->debug_area = NULL;
  179. }
  180. device->state = DASD_STATE_KNOWN;
  181. }
  182. /*
  183. * Do the initial analysis. The do_analysis function may return
  184. * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
  185. * until the discipline decides to continue the startup sequence
  186. * by calling the function dasd_change_state. The eckd disciplines
  187. * uses this to start a ccw that detects the format. The completion
  188. * interrupt for this detection ccw uses the kernel event daemon to
  189. * trigger the call to dasd_change_state. All this is done in the
  190. * discipline code, see dasd_eckd.c.
  191. * After the analysis ccw is done (do_analysis returned 0) the block
  192. * device is setup.
  193. * In case the analysis returns an error, the device setup is stopped
  194. * (a fake disk was already added to allow formatting).
  195. */
  196. static inline int
  197. dasd_state_basic_to_ready(struct dasd_device * device)
  198. {
  199. int rc;
  200. rc = 0;
  201. if (device->discipline->do_analysis != NULL)
  202. rc = device->discipline->do_analysis(device);
  203. if (rc) {
  204. if (rc != -EAGAIN)
  205. device->state = DASD_STATE_UNFMT;
  206. return rc;
  207. }
  208. /* make disk known with correct capacity */
  209. dasd_setup_queue(device);
  210. set_capacity(device->gdp, device->blocks << device->s2b_shift);
  211. device->state = DASD_STATE_READY;
  212. rc = dasd_scan_partitions(device);
  213. if (rc)
  214. device->state = DASD_STATE_BASIC;
  215. return rc;
  216. }
  217. /*
  218. * Remove device from block device layer. Destroy dirty buffers.
  219. * Forget format information. Check if the target level is basic
  220. * and if it is create fake disk for formatting.
  221. */
  222. static inline void
  223. dasd_state_ready_to_basic(struct dasd_device * device)
  224. {
  225. dasd_flush_ccw_queue(device, 0);
  226. dasd_destroy_partitions(device);
  227. dasd_flush_request_queue(device);
  228. device->blocks = 0;
  229. device->bp_block = 0;
  230. device->s2b_shift = 0;
  231. device->state = DASD_STATE_BASIC;
  232. }
  233. /*
  234. * Back to basic.
  235. */
  236. static inline void
  237. dasd_state_unfmt_to_basic(struct dasd_device * device)
  238. {
  239. device->state = DASD_STATE_BASIC;
  240. }
  241. /*
  242. * Make the device online and schedule the bottom half to start
  243. * the requeueing of requests from the linux request queue to the
  244. * ccw queue.
  245. */
  246. static inline int
  247. dasd_state_ready_to_online(struct dasd_device * device)
  248. {
  249. device->state = DASD_STATE_ONLINE;
  250. dasd_schedule_bh(device);
  251. return 0;
  252. }
  253. /*
  254. * Stop the requeueing of requests again.
  255. */
  256. static inline void
  257. dasd_state_online_to_ready(struct dasd_device * device)
  258. {
  259. device->state = DASD_STATE_READY;
  260. }
  261. /*
  262. * Device startup state changes.
  263. */
  264. static inline int
  265. dasd_increase_state(struct dasd_device *device)
  266. {
  267. int rc;
  268. rc = 0;
  269. if (device->state == DASD_STATE_NEW &&
  270. device->target >= DASD_STATE_KNOWN)
  271. rc = dasd_state_new_to_known(device);
  272. if (!rc &&
  273. device->state == DASD_STATE_KNOWN &&
  274. device->target >= DASD_STATE_BASIC)
  275. rc = dasd_state_known_to_basic(device);
  276. if (!rc &&
  277. device->state == DASD_STATE_BASIC &&
  278. device->target >= DASD_STATE_READY)
  279. rc = dasd_state_basic_to_ready(device);
  280. if (!rc &&
  281. device->state == DASD_STATE_READY &&
  282. device->target >= DASD_STATE_ONLINE)
  283. rc = dasd_state_ready_to_online(device);
  284. return rc;
  285. }
  286. /*
  287. * Device shutdown state changes.
  288. */
  289. static inline int
  290. dasd_decrease_state(struct dasd_device *device)
  291. {
  292. if (device->state == DASD_STATE_ONLINE &&
  293. device->target <= DASD_STATE_READY)
  294. dasd_state_online_to_ready(device);
  295. if (device->state == DASD_STATE_READY &&
  296. device->target <= DASD_STATE_BASIC)
  297. dasd_state_ready_to_basic(device);
  298. if (device->state == DASD_STATE_UNFMT &&
  299. device->target <= DASD_STATE_BASIC)
  300. dasd_state_unfmt_to_basic(device);
  301. if (device->state == DASD_STATE_BASIC &&
  302. device->target <= DASD_STATE_KNOWN)
  303. dasd_state_basic_to_known(device);
  304. if (device->state == DASD_STATE_KNOWN &&
  305. device->target <= DASD_STATE_NEW)
  306. dasd_state_known_to_new(device);
  307. return 0;
  308. }
  309. /*
  310. * This is the main startup/shutdown routine.
  311. */
  312. static void
  313. dasd_change_state(struct dasd_device *device)
  314. {
  315. int rc;
  316. if (device->state == device->target)
  317. /* Already where we want to go today... */
  318. return;
  319. if (device->state < device->target)
  320. rc = dasd_increase_state(device);
  321. else
  322. rc = dasd_decrease_state(device);
  323. if (rc && rc != -EAGAIN)
  324. device->target = device->state;
  325. if (device->state == device->target)
  326. wake_up(&dasd_init_waitq);
  327. }
  328. /*
  329. * Kick starter for devices that did not complete the startup/shutdown
  330. * procedure or were sleeping because of a pending state.
  331. * dasd_kick_device will schedule a call do do_kick_device to the kernel
  332. * event daemon.
  333. */
  334. static void
  335. do_kick_device(void *data)
  336. {
  337. struct dasd_device *device;
  338. device = (struct dasd_device *) data;
  339. dasd_change_state(device);
  340. dasd_schedule_bh(device);
  341. dasd_put_device(device);
  342. }
  343. void
  344. dasd_kick_device(struct dasd_device *device)
  345. {
  346. dasd_get_device(device);
  347. /* queue call to dasd_kick_device to the kernel event daemon. */
  348. schedule_work(&device->kick_work);
  349. }
  350. /*
  351. * Set the target state for a device and starts the state change.
  352. */
  353. void
  354. dasd_set_target_state(struct dasd_device *device, int target)
  355. {
  356. /* If we are in probeonly mode stop at DASD_STATE_READY. */
  357. if (dasd_probeonly && target > DASD_STATE_READY)
  358. target = DASD_STATE_READY;
  359. if (device->target != target) {
  360. if (device->state == target)
  361. wake_up(&dasd_init_waitq);
  362. device->target = target;
  363. }
  364. if (device->state != device->target)
  365. dasd_change_state(device);
  366. }
  367. /*
  368. * Enable devices with device numbers in [from..to].
  369. */
  370. static inline int
  371. _wait_for_device(struct dasd_device *device)
  372. {
  373. return (device->state == device->target);
  374. }
  375. void
  376. dasd_enable_device(struct dasd_device *device)
  377. {
  378. dasd_set_target_state(device, DASD_STATE_ONLINE);
  379. if (device->state <= DASD_STATE_KNOWN)
  380. /* No discipline for device found. */
  381. dasd_set_target_state(device, DASD_STATE_NEW);
  382. /* Now wait for the devices to come up. */
  383. wait_event(dasd_init_waitq, _wait_for_device(device));
  384. }
  385. /*
  386. * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
  387. */
  388. #ifdef CONFIG_DASD_PROFILE
  389. struct dasd_profile_info_t dasd_global_profile;
  390. unsigned int dasd_profile_level = DASD_PROFILE_OFF;
  391. /*
  392. * Increments counter in global and local profiling structures.
  393. */
  394. #define dasd_profile_counter(value, counter, device) \
  395. { \
  396. int index; \
  397. for (index = 0; index < 31 && value >> (2+index); index++); \
  398. dasd_global_profile.counter[index]++; \
  399. device->profile.counter[index]++; \
  400. }
  401. /*
  402. * Add profiling information for cqr before execution.
  403. */
  404. static inline void
  405. dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
  406. struct request *req)
  407. {
  408. struct list_head *l;
  409. unsigned int counter;
  410. if (dasd_profile_level != DASD_PROFILE_ON)
  411. return;
  412. /* count the length of the chanq for statistics */
  413. counter = 0;
  414. list_for_each(l, &device->ccw_queue)
  415. if (++counter >= 31)
  416. break;
  417. dasd_global_profile.dasd_io_nr_req[counter]++;
  418. device->profile.dasd_io_nr_req[counter]++;
  419. }
  420. /*
  421. * Add profiling information for cqr after execution.
  422. */
  423. static inline void
  424. dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
  425. struct request *req)
  426. {
  427. long strtime, irqtime, endtime, tottime; /* in microseconds */
  428. long tottimeps, sectors;
  429. if (dasd_profile_level != DASD_PROFILE_ON)
  430. return;
  431. sectors = req->nr_sectors;
  432. if (!cqr->buildclk || !cqr->startclk ||
  433. !cqr->stopclk || !cqr->endclk ||
  434. !sectors)
  435. return;
  436. strtime = ((cqr->startclk - cqr->buildclk) >> 12);
  437. irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
  438. endtime = ((cqr->endclk - cqr->stopclk) >> 12);
  439. tottime = ((cqr->endclk - cqr->buildclk) >> 12);
  440. tottimeps = tottime / sectors;
  441. if (!dasd_global_profile.dasd_io_reqs)
  442. memset(&dasd_global_profile, 0,
  443. sizeof (struct dasd_profile_info_t));
  444. dasd_global_profile.dasd_io_reqs++;
  445. dasd_global_profile.dasd_io_sects += sectors;
  446. if (!device->profile.dasd_io_reqs)
  447. memset(&device->profile, 0,
  448. sizeof (struct dasd_profile_info_t));
  449. device->profile.dasd_io_reqs++;
  450. device->profile.dasd_io_sects += sectors;
  451. dasd_profile_counter(sectors, dasd_io_secs, device);
  452. dasd_profile_counter(tottime, dasd_io_times, device);
  453. dasd_profile_counter(tottimeps, dasd_io_timps, device);
  454. dasd_profile_counter(strtime, dasd_io_time1, device);
  455. dasd_profile_counter(irqtime, dasd_io_time2, device);
  456. dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
  457. dasd_profile_counter(endtime, dasd_io_time3, device);
  458. }
  459. #else
  460. #define dasd_profile_start(device, cqr, req) do {} while (0)
  461. #define dasd_profile_end(device, cqr, req) do {} while (0)
  462. #endif /* CONFIG_DASD_PROFILE */
  463. /*
  464. * Allocate memory for a channel program with 'cplength' channel
  465. * command words and 'datasize' additional space. There are two
  466. * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
  467. * memory and 2) dasd_smalloc_request uses the static ccw memory
  468. * that gets allocated for each device.
  469. */
  470. struct dasd_ccw_req *
  471. dasd_kmalloc_request(char *magic, int cplength, int datasize,
  472. struct dasd_device * device)
  473. {
  474. struct dasd_ccw_req *cqr;
  475. /* Sanity checks */
  476. BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
  477. (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  478. cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
  479. if (cqr == NULL)
  480. return ERR_PTR(-ENOMEM);
  481. cqr->cpaddr = NULL;
  482. if (cplength > 0) {
  483. cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
  484. GFP_ATOMIC | GFP_DMA);
  485. if (cqr->cpaddr == NULL) {
  486. kfree(cqr);
  487. return ERR_PTR(-ENOMEM);
  488. }
  489. }
  490. cqr->data = NULL;
  491. if (datasize > 0) {
  492. cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
  493. if (cqr->data == NULL) {
  494. kfree(cqr->cpaddr);
  495. kfree(cqr);
  496. return ERR_PTR(-ENOMEM);
  497. }
  498. }
  499. strncpy((char *) &cqr->magic, magic, 4);
  500. ASCEBC((char *) &cqr->magic, 4);
  501. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  502. dasd_get_device(device);
  503. return cqr;
  504. }
  505. struct dasd_ccw_req *
  506. dasd_smalloc_request(char *magic, int cplength, int datasize,
  507. struct dasd_device * device)
  508. {
  509. unsigned long flags;
  510. struct dasd_ccw_req *cqr;
  511. char *data;
  512. int size;
  513. /* Sanity checks */
  514. BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
  515. (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  516. size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
  517. if (cplength > 0)
  518. size += cplength * sizeof(struct ccw1);
  519. if (datasize > 0)
  520. size += datasize;
  521. spin_lock_irqsave(&device->mem_lock, flags);
  522. cqr = (struct dasd_ccw_req *)
  523. dasd_alloc_chunk(&device->ccw_chunks, size);
  524. spin_unlock_irqrestore(&device->mem_lock, flags);
  525. if (cqr == NULL)
  526. return ERR_PTR(-ENOMEM);
  527. memset(cqr, 0, sizeof(struct dasd_ccw_req));
  528. data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
  529. cqr->cpaddr = NULL;
  530. if (cplength > 0) {
  531. cqr->cpaddr = (struct ccw1 *) data;
  532. data += cplength*sizeof(struct ccw1);
  533. memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
  534. }
  535. cqr->data = NULL;
  536. if (datasize > 0) {
  537. cqr->data = data;
  538. memset(cqr->data, 0, datasize);
  539. }
  540. strncpy((char *) &cqr->magic, magic, 4);
  541. ASCEBC((char *) &cqr->magic, 4);
  542. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  543. dasd_get_device(device);
  544. return cqr;
  545. }
  546. /*
  547. * Free memory of a channel program. This function needs to free all the
  548. * idal lists that might have been created by dasd_set_cda and the
  549. * struct dasd_ccw_req itself.
  550. */
  551. void
  552. dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
  553. {
  554. #ifdef CONFIG_64BIT
  555. struct ccw1 *ccw;
  556. /* Clear any idals used for the request. */
  557. ccw = cqr->cpaddr;
  558. do {
  559. clear_normalized_cda(ccw);
  560. } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
  561. #endif
  562. kfree(cqr->cpaddr);
  563. kfree(cqr->data);
  564. kfree(cqr);
  565. dasd_put_device(device);
  566. }
  567. void
  568. dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
  569. {
  570. unsigned long flags;
  571. spin_lock_irqsave(&device->mem_lock, flags);
  572. dasd_free_chunk(&device->ccw_chunks, cqr);
  573. spin_unlock_irqrestore(&device->mem_lock, flags);
  574. dasd_put_device(device);
  575. }
  576. /*
  577. * Check discipline magic in cqr.
  578. */
  579. static inline int
  580. dasd_check_cqr(struct dasd_ccw_req *cqr)
  581. {
  582. struct dasd_device *device;
  583. if (cqr == NULL)
  584. return -EINVAL;
  585. device = cqr->device;
  586. if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
  587. DEV_MESSAGE(KERN_WARNING, device,
  588. " dasd_ccw_req 0x%08x magic doesn't match"
  589. " discipline 0x%08x",
  590. cqr->magic,
  591. *(unsigned int *) device->discipline->name);
  592. return -EINVAL;
  593. }
  594. return 0;
  595. }
  596. /*
  597. * Terminate the current i/o and set the request to clear_pending.
  598. * Timer keeps device runnig.
  599. * ccw_device_clear can fail if the i/o subsystem
  600. * is in a bad mood.
  601. */
  602. int
  603. dasd_term_IO(struct dasd_ccw_req * cqr)
  604. {
  605. struct dasd_device *device;
  606. int retries, rc;
  607. /* Check the cqr */
  608. rc = dasd_check_cqr(cqr);
  609. if (rc)
  610. return rc;
  611. retries = 0;
  612. device = (struct dasd_device *) cqr->device;
  613. while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
  614. rc = ccw_device_clear(device->cdev, (long) cqr);
  615. switch (rc) {
  616. case 0: /* termination successful */
  617. cqr->retries--;
  618. cqr->status = DASD_CQR_CLEAR;
  619. cqr->stopclk = get_clock();
  620. DBF_DEV_EVENT(DBF_DEBUG, device,
  621. "terminate cqr %p successful",
  622. cqr);
  623. break;
  624. case -ENODEV:
  625. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  626. "device gone, retry");
  627. break;
  628. case -EIO:
  629. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  630. "I/O error, retry");
  631. break;
  632. case -EINVAL:
  633. case -EBUSY:
  634. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  635. "device busy, retry later");
  636. break;
  637. default:
  638. DEV_MESSAGE(KERN_ERR, device,
  639. "line %d unknown RC=%d, please "
  640. "report to linux390@de.ibm.com",
  641. __LINE__, rc);
  642. BUG();
  643. break;
  644. }
  645. retries++;
  646. }
  647. dasd_schedule_bh(device);
  648. return rc;
  649. }
  650. /*
  651. * Start the i/o. This start_IO can fail if the channel is really busy.
  652. * In that case set up a timer to start the request later.
  653. */
  654. int
  655. dasd_start_IO(struct dasd_ccw_req * cqr)
  656. {
  657. struct dasd_device *device;
  658. int rc;
  659. /* Check the cqr */
  660. rc = dasd_check_cqr(cqr);
  661. if (rc)
  662. return rc;
  663. device = (struct dasd_device *) cqr->device;
  664. if (cqr->retries < 0) {
  665. DEV_MESSAGE(KERN_DEBUG, device,
  666. "start_IO: request %p (%02x/%i) - no retry left.",
  667. cqr, cqr->status, cqr->retries);
  668. cqr->status = DASD_CQR_FAILED;
  669. return -EIO;
  670. }
  671. cqr->startclk = get_clock();
  672. cqr->starttime = jiffies;
  673. cqr->retries--;
  674. rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
  675. cqr->lpm, 0);
  676. switch (rc) {
  677. case 0:
  678. cqr->status = DASD_CQR_IN_IO;
  679. DBF_DEV_EVENT(DBF_DEBUG, device,
  680. "start_IO: request %p started successful",
  681. cqr);
  682. break;
  683. case -EBUSY:
  684. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  685. "start_IO: device busy, retry later");
  686. break;
  687. case -ETIMEDOUT:
  688. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  689. "start_IO: request timeout, retry later");
  690. break;
  691. case -EACCES:
  692. /* -EACCES indicates that the request used only a
  693. * subset of the available pathes and all these
  694. * pathes are gone.
  695. * Do a retry with all available pathes.
  696. */
  697. cqr->lpm = LPM_ANYPATH;
  698. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  699. "start_IO: selected pathes gone,"
  700. " retry on all pathes");
  701. break;
  702. case -ENODEV:
  703. case -EIO:
  704. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  705. "start_IO: device gone, retry");
  706. break;
  707. default:
  708. DEV_MESSAGE(KERN_ERR, device,
  709. "line %d unknown RC=%d, please report"
  710. " to linux390@de.ibm.com", __LINE__, rc);
  711. BUG();
  712. break;
  713. }
  714. return rc;
  715. }
  716. /*
  717. * Timeout function for dasd devices. This is used for different purposes
  718. * 1) missing interrupt handler for normal operation
  719. * 2) delayed start of request where start_IO failed with -EBUSY
  720. * 3) timeout for missing state change interrupts
  721. * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
  722. * DASD_CQR_QUEUED for 2) and 3).
  723. */
  724. static void
  725. dasd_timeout_device(unsigned long ptr)
  726. {
  727. unsigned long flags;
  728. struct dasd_device *device;
  729. device = (struct dasd_device *) ptr;
  730. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  731. /* re-activate request queue */
  732. device->stopped &= ~DASD_STOPPED_PENDING;
  733. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  734. dasd_schedule_bh(device);
  735. }
  736. /*
  737. * Setup timeout for a device in jiffies.
  738. */
  739. void
  740. dasd_set_timer(struct dasd_device *device, int expires)
  741. {
  742. if (expires == 0) {
  743. if (timer_pending(&device->timer))
  744. del_timer(&device->timer);
  745. return;
  746. }
  747. if (timer_pending(&device->timer)) {
  748. if (mod_timer(&device->timer, jiffies + expires))
  749. return;
  750. }
  751. device->timer.function = dasd_timeout_device;
  752. device->timer.data = (unsigned long) device;
  753. device->timer.expires = jiffies + expires;
  754. add_timer(&device->timer);
  755. }
  756. /*
  757. * Clear timeout for a device.
  758. */
  759. void
  760. dasd_clear_timer(struct dasd_device *device)
  761. {
  762. if (timer_pending(&device->timer))
  763. del_timer(&device->timer);
  764. }
  765. static void
  766. dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
  767. {
  768. struct dasd_ccw_req *cqr;
  769. struct dasd_device *device;
  770. cqr = (struct dasd_ccw_req *) intparm;
  771. if (cqr->status != DASD_CQR_IN_IO) {
  772. MESSAGE(KERN_DEBUG,
  773. "invalid status in handle_killed_request: "
  774. "bus_id %s, status %02x",
  775. cdev->dev.bus_id, cqr->status);
  776. return;
  777. }
  778. device = (struct dasd_device *) cqr->device;
  779. if (device == NULL ||
  780. device != dasd_device_from_cdev(cdev) ||
  781. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  782. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  783. cdev->dev.bus_id);
  784. return;
  785. }
  786. /* Schedule request to be retried. */
  787. cqr->status = DASD_CQR_QUEUED;
  788. dasd_clear_timer(device);
  789. dasd_schedule_bh(device);
  790. dasd_put_device(device);
  791. }
  792. static void
  793. dasd_handle_state_change_pending(struct dasd_device *device)
  794. {
  795. struct dasd_ccw_req *cqr;
  796. struct list_head *l, *n;
  797. /* First of all start sense subsystem status request. */
  798. dasd_eer_snss(device);
  799. device->stopped &= ~DASD_STOPPED_PENDING;
  800. /* restart all 'running' IO on queue */
  801. list_for_each_safe(l, n, &device->ccw_queue) {
  802. cqr = list_entry(l, struct dasd_ccw_req, list);
  803. if (cqr->status == DASD_CQR_IN_IO) {
  804. cqr->status = DASD_CQR_QUEUED;
  805. }
  806. }
  807. dasd_clear_timer(device);
  808. dasd_schedule_bh(device);
  809. }
  810. /*
  811. * Interrupt handler for "normal" ssch-io based dasd devices.
  812. */
  813. void
  814. dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
  815. struct irb *irb)
  816. {
  817. struct dasd_ccw_req *cqr, *next;
  818. struct dasd_device *device;
  819. unsigned long long now;
  820. int expires;
  821. dasd_era_t era;
  822. char mask;
  823. if (IS_ERR(irb)) {
  824. switch (PTR_ERR(irb)) {
  825. case -EIO:
  826. dasd_handle_killed_request(cdev, intparm);
  827. break;
  828. case -ETIMEDOUT:
  829. printk(KERN_WARNING"%s(%s): request timed out\n",
  830. __FUNCTION__, cdev->dev.bus_id);
  831. //FIXME - dasd uses own timeout interface...
  832. break;
  833. default:
  834. printk(KERN_WARNING"%s(%s): unknown error %ld\n",
  835. __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
  836. }
  837. return;
  838. }
  839. now = get_clock();
  840. DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
  841. cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
  842. (unsigned int) intparm);
  843. /* first of all check for state change pending interrupt */
  844. mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
  845. if ((irb->scsw.dstat & mask) == mask) {
  846. device = dasd_device_from_cdev(cdev);
  847. if (!IS_ERR(device)) {
  848. dasd_handle_state_change_pending(device);
  849. dasd_put_device(device);
  850. }
  851. return;
  852. }
  853. cqr = (struct dasd_ccw_req *) intparm;
  854. /* check for unsolicited interrupts */
  855. if (cqr == NULL) {
  856. MESSAGE(KERN_DEBUG,
  857. "unsolicited interrupt received: bus_id %s",
  858. cdev->dev.bus_id);
  859. return;
  860. }
  861. device = (struct dasd_device *) cqr->device;
  862. if (device == NULL ||
  863. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  864. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  865. cdev->dev.bus_id);
  866. return;
  867. }
  868. /* Check for clear pending */
  869. if (cqr->status == DASD_CQR_CLEAR &&
  870. irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
  871. cqr->status = DASD_CQR_QUEUED;
  872. dasd_clear_timer(device);
  873. dasd_schedule_bh(device);
  874. return;
  875. }
  876. /* check status - the request might have been killed by dyn detach */
  877. if (cqr->status != DASD_CQR_IN_IO) {
  878. MESSAGE(KERN_DEBUG,
  879. "invalid status: bus_id %s, status %02x",
  880. cdev->dev.bus_id, cqr->status);
  881. return;
  882. }
  883. DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
  884. ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
  885. /* Find out the appropriate era_action. */
  886. if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
  887. era = dasd_era_fatal;
  888. else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  889. irb->scsw.cstat == 0 &&
  890. !irb->esw.esw0.erw.cons)
  891. era = dasd_era_none;
  892. else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
  893. era = dasd_era_fatal; /* don't recover this request */
  894. else if (irb->esw.esw0.erw.cons)
  895. era = device->discipline->examine_error(cqr, irb);
  896. else
  897. era = dasd_era_recover;
  898. DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
  899. expires = 0;
  900. if (era == dasd_era_none) {
  901. cqr->status = DASD_CQR_DONE;
  902. cqr->stopclk = now;
  903. /* Start first request on queue if possible -> fast_io. */
  904. if (cqr->list.next != &device->ccw_queue) {
  905. next = list_entry(cqr->list.next,
  906. struct dasd_ccw_req, list);
  907. if ((next->status == DASD_CQR_QUEUED) &&
  908. (!device->stopped)) {
  909. if (device->discipline->start_IO(next) == 0)
  910. expires = next->expires;
  911. else
  912. DEV_MESSAGE(KERN_DEBUG, device, "%s",
  913. "Interrupt fastpath "
  914. "failed!");
  915. }
  916. }
  917. } else { /* error */
  918. memcpy(&cqr->irb, irb, sizeof (struct irb));
  919. #ifdef ERP_DEBUG
  920. /* dump sense data */
  921. dasd_log_sense(cqr, irb);
  922. #endif
  923. switch (era) {
  924. case dasd_era_fatal:
  925. cqr->status = DASD_CQR_FAILED;
  926. cqr->stopclk = now;
  927. break;
  928. case dasd_era_recover:
  929. cqr->status = DASD_CQR_ERROR;
  930. break;
  931. default:
  932. BUG();
  933. }
  934. }
  935. if (expires != 0)
  936. dasd_set_timer(device, expires);
  937. else
  938. dasd_clear_timer(device);
  939. dasd_schedule_bh(device);
  940. }
  941. /*
  942. * posts the buffer_cache about a finalized request
  943. */
  944. static inline void
  945. dasd_end_request(struct request *req, int uptodate)
  946. {
  947. if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
  948. BUG();
  949. add_disk_randomness(req->rq_disk);
  950. end_that_request_last(req, uptodate);
  951. }
  952. /*
  953. * Process finished error recovery ccw.
  954. */
  955. static inline void
  956. __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
  957. {
  958. dasd_erp_fn_t erp_fn;
  959. if (cqr->status == DASD_CQR_DONE)
  960. DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
  961. else
  962. DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
  963. erp_fn = device->discipline->erp_postaction(cqr);
  964. erp_fn(cqr);
  965. }
  966. /*
  967. * Process ccw request queue.
  968. */
  969. static inline void
  970. __dasd_process_ccw_queue(struct dasd_device * device,
  971. struct list_head *final_queue)
  972. {
  973. struct list_head *l, *n;
  974. struct dasd_ccw_req *cqr;
  975. dasd_erp_fn_t erp_fn;
  976. restart:
  977. /* Process request with final status. */
  978. list_for_each_safe(l, n, &device->ccw_queue) {
  979. cqr = list_entry(l, struct dasd_ccw_req, list);
  980. /* Stop list processing at the first non-final request. */
  981. if (cqr->status != DASD_CQR_DONE &&
  982. cqr->status != DASD_CQR_FAILED &&
  983. cqr->status != DASD_CQR_ERROR)
  984. break;
  985. /* Process requests with DASD_CQR_ERROR */
  986. if (cqr->status == DASD_CQR_ERROR) {
  987. if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
  988. cqr->status = DASD_CQR_FAILED;
  989. cqr->stopclk = get_clock();
  990. } else {
  991. if (cqr->irb.esw.esw0.erw.cons) {
  992. erp_fn = device->discipline->
  993. erp_action(cqr);
  994. erp_fn(cqr);
  995. } else
  996. dasd_default_erp_action(cqr);
  997. }
  998. goto restart;
  999. }
  1000. /* First of all call extended error reporting. */
  1001. if (dasd_eer_enabled(device) &&
  1002. cqr->status == DASD_CQR_FAILED) {
  1003. dasd_eer_write(device, cqr, DASD_EER_FATALERROR);
  1004. /* restart request */
  1005. cqr->status = DASD_CQR_QUEUED;
  1006. cqr->retries = 255;
  1007. device->stopped |= DASD_STOPPED_QUIESCE;
  1008. goto restart;
  1009. }
  1010. /* Process finished ERP request. */
  1011. if (cqr->refers) {
  1012. __dasd_process_erp(device, cqr);
  1013. goto restart;
  1014. }
  1015. /* Rechain finished requests to final queue */
  1016. cqr->endclk = get_clock();
  1017. list_move_tail(&cqr->list, final_queue);
  1018. }
  1019. }
  1020. static void
  1021. dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
  1022. {
  1023. struct request *req;
  1024. struct dasd_device *device;
  1025. int status;
  1026. req = (struct request *) data;
  1027. device = cqr->device;
  1028. dasd_profile_end(device, cqr, req);
  1029. status = cqr->device->discipline->free_cp(cqr,req);
  1030. spin_lock_irq(&device->request_queue_lock);
  1031. dasd_end_request(req, status);
  1032. spin_unlock_irq(&device->request_queue_lock);
  1033. }
  1034. /*
  1035. * Fetch requests from the block device queue.
  1036. */
  1037. static inline void
  1038. __dasd_process_blk_queue(struct dasd_device * device)
  1039. {
  1040. request_queue_t *queue;
  1041. struct request *req;
  1042. struct dasd_ccw_req *cqr;
  1043. int nr_queued;
  1044. queue = device->request_queue;
  1045. /* No queue ? Then there is nothing to do. */
  1046. if (queue == NULL)
  1047. return;
  1048. /*
  1049. * We requeue request from the block device queue to the ccw
  1050. * queue only in two states. In state DASD_STATE_READY the
  1051. * partition detection is done and we need to requeue requests
  1052. * for that. State DASD_STATE_ONLINE is normal block device
  1053. * operation.
  1054. */
  1055. if (device->state != DASD_STATE_READY &&
  1056. device->state != DASD_STATE_ONLINE)
  1057. return;
  1058. nr_queued = 0;
  1059. /* Now we try to fetch requests from the request queue */
  1060. list_for_each_entry(cqr, &device->ccw_queue, list)
  1061. if (cqr->status == DASD_CQR_QUEUED)
  1062. nr_queued++;
  1063. while (!blk_queue_plugged(queue) &&
  1064. elv_next_request(queue) &&
  1065. nr_queued < DASD_CHANQ_MAX_SIZE) {
  1066. req = elv_next_request(queue);
  1067. if (device->features & DASD_FEATURE_READONLY &&
  1068. rq_data_dir(req) == WRITE) {
  1069. DBF_DEV_EVENT(DBF_ERR, device,
  1070. "Rejecting write request %p",
  1071. req);
  1072. blkdev_dequeue_request(req);
  1073. dasd_end_request(req, 0);
  1074. continue;
  1075. }
  1076. if (device->stopped & DASD_STOPPED_DC_EIO) {
  1077. blkdev_dequeue_request(req);
  1078. dasd_end_request(req, 0);
  1079. continue;
  1080. }
  1081. cqr = device->discipline->build_cp(device, req);
  1082. if (IS_ERR(cqr)) {
  1083. if (PTR_ERR(cqr) == -ENOMEM)
  1084. break; /* terminate request queue loop */
  1085. DBF_DEV_EVENT(DBF_ERR, device,
  1086. "CCW creation failed (rc=%ld) "
  1087. "on request %p",
  1088. PTR_ERR(cqr), req);
  1089. blkdev_dequeue_request(req);
  1090. dasd_end_request(req, 0);
  1091. continue;
  1092. }
  1093. cqr->callback = dasd_end_request_cb;
  1094. cqr->callback_data = (void *) req;
  1095. cqr->status = DASD_CQR_QUEUED;
  1096. blkdev_dequeue_request(req);
  1097. list_add_tail(&cqr->list, &device->ccw_queue);
  1098. dasd_profile_start(device, cqr, req);
  1099. nr_queued++;
  1100. }
  1101. }
  1102. /*
  1103. * Take a look at the first request on the ccw queue and check
  1104. * if it reached its expire time. If so, terminate the IO.
  1105. */
  1106. static inline void
  1107. __dasd_check_expire(struct dasd_device * device)
  1108. {
  1109. struct dasd_ccw_req *cqr;
  1110. if (list_empty(&device->ccw_queue))
  1111. return;
  1112. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1113. if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
  1114. if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
  1115. if (device->discipline->term_IO(cqr) != 0)
  1116. /* Hmpf, try again in 1/10 sec */
  1117. dasd_set_timer(device, 10);
  1118. }
  1119. }
  1120. }
  1121. /*
  1122. * Take a look at the first request on the ccw queue and check
  1123. * if it needs to be started.
  1124. */
  1125. static inline void
  1126. __dasd_start_head(struct dasd_device * device)
  1127. {
  1128. struct dasd_ccw_req *cqr;
  1129. int rc;
  1130. if (list_empty(&device->ccw_queue))
  1131. return;
  1132. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1133. if (cqr->status != DASD_CQR_QUEUED)
  1134. return;
  1135. /* Non-temporary stop condition will trigger fail fast */
  1136. if (device->stopped & ~DASD_STOPPED_PENDING &&
  1137. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1138. (!dasd_eer_enabled(device))) {
  1139. cqr->status = DASD_CQR_FAILED;
  1140. dasd_schedule_bh(device);
  1141. return;
  1142. }
  1143. /* Don't try to start requests if device is stopped */
  1144. if (device->stopped)
  1145. return;
  1146. rc = device->discipline->start_IO(cqr);
  1147. if (rc == 0)
  1148. dasd_set_timer(device, cqr->expires);
  1149. else if (rc == -EACCES) {
  1150. dasd_schedule_bh(device);
  1151. } else
  1152. /* Hmpf, try again in 1/2 sec */
  1153. dasd_set_timer(device, 50);
  1154. }
  1155. /*
  1156. * Remove requests from the ccw queue.
  1157. */
  1158. static void
  1159. dasd_flush_ccw_queue(struct dasd_device * device, int all)
  1160. {
  1161. struct list_head flush_queue;
  1162. struct list_head *l, *n;
  1163. struct dasd_ccw_req *cqr;
  1164. INIT_LIST_HEAD(&flush_queue);
  1165. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1166. list_for_each_safe(l, n, &device->ccw_queue) {
  1167. cqr = list_entry(l, struct dasd_ccw_req, list);
  1168. /* Flush all request or only block device requests? */
  1169. if (all == 0 && cqr->callback == dasd_end_request_cb)
  1170. continue;
  1171. if (cqr->status == DASD_CQR_IN_IO)
  1172. device->discipline->term_IO(cqr);
  1173. if (cqr->status != DASD_CQR_DONE ||
  1174. cqr->status != DASD_CQR_FAILED) {
  1175. cqr->status = DASD_CQR_FAILED;
  1176. cqr->stopclk = get_clock();
  1177. }
  1178. /* Process finished ERP request. */
  1179. if (cqr->refers) {
  1180. __dasd_process_erp(device, cqr);
  1181. continue;
  1182. }
  1183. /* Rechain request on device request queue */
  1184. cqr->endclk = get_clock();
  1185. list_move_tail(&cqr->list, &flush_queue);
  1186. }
  1187. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1188. /* Now call the callback function of flushed requests */
  1189. list_for_each_safe(l, n, &flush_queue) {
  1190. cqr = list_entry(l, struct dasd_ccw_req, list);
  1191. if (cqr->callback != NULL)
  1192. (cqr->callback)(cqr, cqr->callback_data);
  1193. }
  1194. }
  1195. /*
  1196. * Acquire the device lock and process queues for the device.
  1197. */
  1198. static void
  1199. dasd_tasklet(struct dasd_device * device)
  1200. {
  1201. struct list_head final_queue;
  1202. struct list_head *l, *n;
  1203. struct dasd_ccw_req *cqr;
  1204. atomic_set (&device->tasklet_scheduled, 0);
  1205. INIT_LIST_HEAD(&final_queue);
  1206. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1207. /* Check expire time of first request on the ccw queue. */
  1208. __dasd_check_expire(device);
  1209. /* Finish off requests on ccw queue */
  1210. __dasd_process_ccw_queue(device, &final_queue);
  1211. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1212. /* Now call the callback function of requests with final status */
  1213. list_for_each_safe(l, n, &final_queue) {
  1214. cqr = list_entry(l, struct dasd_ccw_req, list);
  1215. list_del_init(&cqr->list);
  1216. if (cqr->callback != NULL)
  1217. (cqr->callback)(cqr, cqr->callback_data);
  1218. }
  1219. spin_lock_irq(&device->request_queue_lock);
  1220. spin_lock(get_ccwdev_lock(device->cdev));
  1221. /* Get new request from the block device request queue */
  1222. __dasd_process_blk_queue(device);
  1223. /* Now check if the head of the ccw queue needs to be started. */
  1224. __dasd_start_head(device);
  1225. spin_unlock(get_ccwdev_lock(device->cdev));
  1226. spin_unlock_irq(&device->request_queue_lock);
  1227. dasd_put_device(device);
  1228. }
  1229. /*
  1230. * Schedules a call to dasd_tasklet over the device tasklet.
  1231. */
  1232. void
  1233. dasd_schedule_bh(struct dasd_device * device)
  1234. {
  1235. /* Protect against rescheduling. */
  1236. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  1237. return;
  1238. dasd_get_device(device);
  1239. tasklet_hi_schedule(&device->tasklet);
  1240. }
  1241. /*
  1242. * Queue a request to the head of the ccw_queue. Start the I/O if
  1243. * possible.
  1244. */
  1245. void
  1246. dasd_add_request_head(struct dasd_ccw_req *req)
  1247. {
  1248. struct dasd_device *device;
  1249. unsigned long flags;
  1250. device = req->device;
  1251. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1252. req->status = DASD_CQR_QUEUED;
  1253. req->device = device;
  1254. list_add(&req->list, &device->ccw_queue);
  1255. /* let the bh start the request to keep them in order */
  1256. dasd_schedule_bh(device);
  1257. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1258. }
  1259. /*
  1260. * Queue a request to the tail of the ccw_queue. Start the I/O if
  1261. * possible.
  1262. */
  1263. void
  1264. dasd_add_request_tail(struct dasd_ccw_req *req)
  1265. {
  1266. struct dasd_device *device;
  1267. unsigned long flags;
  1268. device = req->device;
  1269. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1270. req->status = DASD_CQR_QUEUED;
  1271. req->device = device;
  1272. list_add_tail(&req->list, &device->ccw_queue);
  1273. /* let the bh start the request to keep them in order */
  1274. dasd_schedule_bh(device);
  1275. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1276. }
  1277. /*
  1278. * Wakeup callback.
  1279. */
  1280. static void
  1281. dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  1282. {
  1283. wake_up((wait_queue_head_t *) data);
  1284. }
  1285. static inline int
  1286. _wait_for_wakeup(struct dasd_ccw_req *cqr)
  1287. {
  1288. struct dasd_device *device;
  1289. int rc;
  1290. device = cqr->device;
  1291. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1292. rc = ((cqr->status == DASD_CQR_DONE ||
  1293. cqr->status == DASD_CQR_FAILED) &&
  1294. list_empty(&cqr->list));
  1295. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1296. return rc;
  1297. }
  1298. /*
  1299. * Attempts to start a special ccw queue and waits for its completion.
  1300. */
  1301. int
  1302. dasd_sleep_on(struct dasd_ccw_req * cqr)
  1303. {
  1304. wait_queue_head_t wait_q;
  1305. struct dasd_device *device;
  1306. int rc;
  1307. device = cqr->device;
  1308. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1309. init_waitqueue_head (&wait_q);
  1310. cqr->callback = dasd_wakeup_cb;
  1311. cqr->callback_data = (void *) &wait_q;
  1312. cqr->status = DASD_CQR_QUEUED;
  1313. list_add_tail(&cqr->list, &device->ccw_queue);
  1314. /* let the bh start the request to keep them in order */
  1315. dasd_schedule_bh(device);
  1316. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1317. wait_event(wait_q, _wait_for_wakeup(cqr));
  1318. /* Request status is either done or failed. */
  1319. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1320. return rc;
  1321. }
  1322. /*
  1323. * Attempts to start a special ccw queue and wait interruptible
  1324. * for its completion.
  1325. */
  1326. int
  1327. dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
  1328. {
  1329. wait_queue_head_t wait_q;
  1330. struct dasd_device *device;
  1331. int rc, finished;
  1332. device = cqr->device;
  1333. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1334. init_waitqueue_head (&wait_q);
  1335. cqr->callback = dasd_wakeup_cb;
  1336. cqr->callback_data = (void *) &wait_q;
  1337. cqr->status = DASD_CQR_QUEUED;
  1338. list_add_tail(&cqr->list, &device->ccw_queue);
  1339. /* let the bh start the request to keep them in order */
  1340. dasd_schedule_bh(device);
  1341. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1342. finished = 0;
  1343. while (!finished) {
  1344. rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
  1345. if (rc != -ERESTARTSYS) {
  1346. /* Request is final (done or failed) */
  1347. rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  1348. break;
  1349. }
  1350. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1351. switch (cqr->status) {
  1352. case DASD_CQR_IN_IO:
  1353. /* terminate runnig cqr */
  1354. if (device->discipline->term_IO) {
  1355. cqr->retries = -1;
  1356. device->discipline->term_IO(cqr);
  1357. /*nished =
  1358. * wait (non-interruptible) for final status
  1359. * because signal ist still pending
  1360. */
  1361. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1362. wait_event(wait_q, _wait_for_wakeup(cqr));
  1363. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1364. rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  1365. finished = 1;
  1366. }
  1367. break;
  1368. case DASD_CQR_QUEUED:
  1369. /* request */
  1370. list_del_init(&cqr->list);
  1371. rc = -EIO;
  1372. finished = 1;
  1373. break;
  1374. default:
  1375. /* cqr with 'non-interruptable' status - just wait */
  1376. break;
  1377. }
  1378. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1379. }
  1380. return rc;
  1381. }
  1382. /*
  1383. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  1384. * for eckd devices) the currently running request has to be terminated
  1385. * and be put back to status queued, before the special request is added
  1386. * to the head of the queue. Then the special request is waited on normally.
  1387. */
  1388. static inline int
  1389. _dasd_term_running_cqr(struct dasd_device *device)
  1390. {
  1391. struct dasd_ccw_req *cqr;
  1392. int rc;
  1393. if (list_empty(&device->ccw_queue))
  1394. return 0;
  1395. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1396. rc = device->discipline->term_IO(cqr);
  1397. if (rc == 0) {
  1398. /* termination successful */
  1399. cqr->status = DASD_CQR_QUEUED;
  1400. cqr->startclk = cqr->stopclk = 0;
  1401. cqr->starttime = 0;
  1402. }
  1403. return rc;
  1404. }
  1405. int
  1406. dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
  1407. {
  1408. wait_queue_head_t wait_q;
  1409. struct dasd_device *device;
  1410. int rc;
  1411. device = cqr->device;
  1412. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1413. rc = _dasd_term_running_cqr(device);
  1414. if (rc) {
  1415. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1416. return rc;
  1417. }
  1418. init_waitqueue_head (&wait_q);
  1419. cqr->callback = dasd_wakeup_cb;
  1420. cqr->callback_data = (void *) &wait_q;
  1421. cqr->status = DASD_CQR_QUEUED;
  1422. list_add(&cqr->list, &device->ccw_queue);
  1423. /* let the bh start the request to keep them in order */
  1424. dasd_schedule_bh(device);
  1425. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1426. wait_event(wait_q, _wait_for_wakeup(cqr));
  1427. /* Request status is either done or failed. */
  1428. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1429. return rc;
  1430. }
  1431. /*
  1432. * Cancels a request that was started with dasd_sleep_on_req.
  1433. * This is useful to timeout requests. The request will be
  1434. * terminated if it is currently in i/o.
  1435. * Returns 1 if the request has been terminated.
  1436. */
  1437. int
  1438. dasd_cancel_req(struct dasd_ccw_req *cqr)
  1439. {
  1440. struct dasd_device *device = cqr->device;
  1441. unsigned long flags;
  1442. int rc;
  1443. rc = 0;
  1444. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1445. switch (cqr->status) {
  1446. case DASD_CQR_QUEUED:
  1447. /* request was not started - just set to failed */
  1448. cqr->status = DASD_CQR_FAILED;
  1449. break;
  1450. case DASD_CQR_IN_IO:
  1451. /* request in IO - terminate IO and release again */
  1452. if (device->discipline->term_IO(cqr) != 0)
  1453. /* what to do if unable to terminate ??????
  1454. e.g. not _IN_IO */
  1455. cqr->status = DASD_CQR_FAILED;
  1456. cqr->stopclk = get_clock();
  1457. rc = 1;
  1458. break;
  1459. case DASD_CQR_DONE:
  1460. case DASD_CQR_FAILED:
  1461. /* already finished - do nothing */
  1462. break;
  1463. default:
  1464. DEV_MESSAGE(KERN_ALERT, device,
  1465. "invalid status %02x in request",
  1466. cqr->status);
  1467. BUG();
  1468. }
  1469. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1470. dasd_schedule_bh(device);
  1471. return rc;
  1472. }
  1473. /*
  1474. * SECTION: Block device operations (request queue, partitions, open, release).
  1475. */
  1476. /*
  1477. * Dasd request queue function. Called from ll_rw_blk.c
  1478. */
  1479. static void
  1480. do_dasd_request(request_queue_t * queue)
  1481. {
  1482. struct dasd_device *device;
  1483. device = (struct dasd_device *) queue->queuedata;
  1484. spin_lock(get_ccwdev_lock(device->cdev));
  1485. /* Get new request from the block device request queue */
  1486. __dasd_process_blk_queue(device);
  1487. /* Now check if the head of the ccw queue needs to be started. */
  1488. __dasd_start_head(device);
  1489. spin_unlock(get_ccwdev_lock(device->cdev));
  1490. }
  1491. /*
  1492. * Allocate and initialize request queue and default I/O scheduler.
  1493. */
  1494. static int
  1495. dasd_alloc_queue(struct dasd_device * device)
  1496. {
  1497. int rc;
  1498. device->request_queue = blk_init_queue(do_dasd_request,
  1499. &device->request_queue_lock);
  1500. if (device->request_queue == NULL)
  1501. return -ENOMEM;
  1502. device->request_queue->queuedata = device;
  1503. elevator_exit(device->request_queue->elevator);
  1504. rc = elevator_init(device->request_queue, "deadline");
  1505. if (rc) {
  1506. blk_cleanup_queue(device->request_queue);
  1507. return rc;
  1508. }
  1509. return 0;
  1510. }
  1511. /*
  1512. * Allocate and initialize request queue.
  1513. */
  1514. static void
  1515. dasd_setup_queue(struct dasd_device * device)
  1516. {
  1517. int max;
  1518. blk_queue_hardsect_size(device->request_queue, device->bp_block);
  1519. max = device->discipline->max_blocks << device->s2b_shift;
  1520. blk_queue_max_sectors(device->request_queue, max);
  1521. blk_queue_max_phys_segments(device->request_queue, -1L);
  1522. blk_queue_max_hw_segments(device->request_queue, -1L);
  1523. blk_queue_max_segment_size(device->request_queue, -1L);
  1524. blk_queue_segment_boundary(device->request_queue, -1L);
  1525. blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
  1526. }
  1527. /*
  1528. * Deactivate and free request queue.
  1529. */
  1530. static void
  1531. dasd_free_queue(struct dasd_device * device)
  1532. {
  1533. if (device->request_queue) {
  1534. blk_cleanup_queue(device->request_queue);
  1535. device->request_queue = NULL;
  1536. }
  1537. }
  1538. /*
  1539. * Flush request on the request queue.
  1540. */
  1541. static void
  1542. dasd_flush_request_queue(struct dasd_device * device)
  1543. {
  1544. struct request *req;
  1545. if (!device->request_queue)
  1546. return;
  1547. spin_lock_irq(&device->request_queue_lock);
  1548. while (!list_empty(&device->request_queue->queue_head)) {
  1549. req = elv_next_request(device->request_queue);
  1550. if (req == NULL)
  1551. break;
  1552. dasd_end_request(req, 0);
  1553. blkdev_dequeue_request(req);
  1554. }
  1555. spin_unlock_irq(&device->request_queue_lock);
  1556. }
  1557. static int
  1558. dasd_open(struct inode *inp, struct file *filp)
  1559. {
  1560. struct gendisk *disk = inp->i_bdev->bd_disk;
  1561. struct dasd_device *device = disk->private_data;
  1562. int rc;
  1563. atomic_inc(&device->open_count);
  1564. if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1565. rc = -ENODEV;
  1566. goto unlock;
  1567. }
  1568. if (!try_module_get(device->discipline->owner)) {
  1569. rc = -EINVAL;
  1570. goto unlock;
  1571. }
  1572. if (dasd_probeonly) {
  1573. DEV_MESSAGE(KERN_INFO, device, "%s",
  1574. "No access to device due to probeonly mode");
  1575. rc = -EPERM;
  1576. goto out;
  1577. }
  1578. if (device->state <= DASD_STATE_BASIC) {
  1579. DBF_DEV_EVENT(DBF_ERR, device, " %s",
  1580. " Cannot open unrecognized device");
  1581. rc = -ENODEV;
  1582. goto out;
  1583. }
  1584. return 0;
  1585. out:
  1586. module_put(device->discipline->owner);
  1587. unlock:
  1588. atomic_dec(&device->open_count);
  1589. return rc;
  1590. }
  1591. static int
  1592. dasd_release(struct inode *inp, struct file *filp)
  1593. {
  1594. struct gendisk *disk = inp->i_bdev->bd_disk;
  1595. struct dasd_device *device = disk->private_data;
  1596. atomic_dec(&device->open_count);
  1597. module_put(device->discipline->owner);
  1598. return 0;
  1599. }
  1600. /*
  1601. * Return disk geometry.
  1602. */
  1603. static int
  1604. dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1605. {
  1606. struct dasd_device *device;
  1607. device = bdev->bd_disk->private_data;
  1608. if (!device)
  1609. return -ENODEV;
  1610. if (!device->discipline ||
  1611. !device->discipline->fill_geometry)
  1612. return -EINVAL;
  1613. device->discipline->fill_geometry(device, geo);
  1614. geo->start = get_start_sect(bdev) >> device->s2b_shift;
  1615. return 0;
  1616. }
  1617. struct block_device_operations
  1618. dasd_device_operations = {
  1619. .owner = THIS_MODULE,
  1620. .open = dasd_open,
  1621. .release = dasd_release,
  1622. .ioctl = dasd_ioctl,
  1623. .compat_ioctl = dasd_compat_ioctl,
  1624. .getgeo = dasd_getgeo,
  1625. };
  1626. static void
  1627. dasd_exit(void)
  1628. {
  1629. #ifdef CONFIG_PROC_FS
  1630. dasd_proc_exit();
  1631. #endif
  1632. dasd_eer_exit();
  1633. if (dasd_page_cache != NULL) {
  1634. kmem_cache_destroy(dasd_page_cache);
  1635. dasd_page_cache = NULL;
  1636. }
  1637. dasd_gendisk_exit();
  1638. dasd_devmap_exit();
  1639. devfs_remove("dasd");
  1640. if (dasd_debug_area != NULL) {
  1641. debug_unregister(dasd_debug_area);
  1642. dasd_debug_area = NULL;
  1643. }
  1644. }
  1645. /*
  1646. * SECTION: common functions for ccw_driver use
  1647. */
  1648. /*
  1649. * Initial attempt at a probe function. this can be simplified once
  1650. * the other detection code is gone.
  1651. */
  1652. int
  1653. dasd_generic_probe (struct ccw_device *cdev,
  1654. struct dasd_discipline *discipline)
  1655. {
  1656. int ret;
  1657. ret = dasd_add_sysfs_files(cdev);
  1658. if (ret) {
  1659. printk(KERN_WARNING
  1660. "dasd_generic_probe: could not add sysfs entries "
  1661. "for %s\n", cdev->dev.bus_id);
  1662. } else {
  1663. cdev->handler = &dasd_int_handler;
  1664. }
  1665. return ret;
  1666. }
  1667. /*
  1668. * This will one day be called from a global not_oper handler.
  1669. * It is also used by driver_unregister during module unload.
  1670. */
  1671. void
  1672. dasd_generic_remove (struct ccw_device *cdev)
  1673. {
  1674. struct dasd_device *device;
  1675. cdev->handler = NULL;
  1676. dasd_remove_sysfs_files(cdev);
  1677. device = dasd_device_from_cdev(cdev);
  1678. if (IS_ERR(device))
  1679. return;
  1680. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1681. /* Already doing offline processing */
  1682. dasd_put_device(device);
  1683. return;
  1684. }
  1685. /*
  1686. * This device is removed unconditionally. Set offline
  1687. * flag to prevent dasd_open from opening it while it is
  1688. * no quite down yet.
  1689. */
  1690. dasd_set_target_state(device, DASD_STATE_NEW);
  1691. /* dasd_delete_device destroys the device reference. */
  1692. dasd_delete_device(device);
  1693. }
  1694. /*
  1695. * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
  1696. * the device is detected for the first time and is supposed to be used
  1697. * or the user has started activation through sysfs.
  1698. */
  1699. int
  1700. dasd_generic_set_online (struct ccw_device *cdev,
  1701. struct dasd_discipline *base_discipline)
  1702. {
  1703. struct dasd_discipline *discipline;
  1704. struct dasd_device *device;
  1705. int rc;
  1706. device = dasd_create_device(cdev);
  1707. if (IS_ERR(device))
  1708. return PTR_ERR(device);
  1709. discipline = base_discipline;
  1710. if (device->features & DASD_FEATURE_USEDIAG) {
  1711. if (!dasd_diag_discipline_pointer) {
  1712. printk (KERN_WARNING
  1713. "dasd_generic couldn't online device %s "
  1714. "- discipline DIAG not available\n",
  1715. cdev->dev.bus_id);
  1716. dasd_delete_device(device);
  1717. return -ENODEV;
  1718. }
  1719. discipline = dasd_diag_discipline_pointer;
  1720. }
  1721. if (!try_module_get(base_discipline->owner)) {
  1722. dasd_delete_device(device);
  1723. return -EINVAL;
  1724. }
  1725. if (!try_module_get(discipline->owner)) {
  1726. module_put(base_discipline->owner);
  1727. dasd_delete_device(device);
  1728. return -EINVAL;
  1729. }
  1730. device->base_discipline = base_discipline;
  1731. device->discipline = discipline;
  1732. rc = discipline->check_device(device);
  1733. if (rc) {
  1734. printk (KERN_WARNING
  1735. "dasd_generic couldn't online device %s "
  1736. "with discipline %s rc=%i\n",
  1737. cdev->dev.bus_id, discipline->name, rc);
  1738. module_put(discipline->owner);
  1739. module_put(base_discipline->owner);
  1740. dasd_delete_device(device);
  1741. return rc;
  1742. }
  1743. dasd_set_target_state(device, DASD_STATE_ONLINE);
  1744. if (device->state <= DASD_STATE_KNOWN) {
  1745. printk (KERN_WARNING
  1746. "dasd_generic discipline not found for %s\n",
  1747. cdev->dev.bus_id);
  1748. rc = -ENODEV;
  1749. dasd_set_target_state(device, DASD_STATE_NEW);
  1750. dasd_delete_device(device);
  1751. } else
  1752. pr_debug("dasd_generic device %s found\n",
  1753. cdev->dev.bus_id);
  1754. /* FIXME: we have to wait for the root device but we don't want
  1755. * to wait for each single device but for all at once. */
  1756. wait_event(dasd_init_waitq, _wait_for_device(device));
  1757. dasd_put_device(device);
  1758. return rc;
  1759. }
  1760. int
  1761. dasd_generic_set_offline (struct ccw_device *cdev)
  1762. {
  1763. struct dasd_device *device;
  1764. int max_count, open_count;
  1765. device = dasd_device_from_cdev(cdev);
  1766. if (IS_ERR(device))
  1767. return PTR_ERR(device);
  1768. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1769. /* Already doing offline processing */
  1770. dasd_put_device(device);
  1771. return 0;
  1772. }
  1773. /*
  1774. * We must make sure that this device is currently not in use.
  1775. * The open_count is increased for every opener, that includes
  1776. * the blkdev_get in dasd_scan_partitions. We are only interested
  1777. * in the other openers.
  1778. */
  1779. max_count = device->bdev ? 0 : -1;
  1780. open_count = (int) atomic_read(&device->open_count);
  1781. if (open_count > max_count) {
  1782. if (open_count > 0)
  1783. printk (KERN_WARNING "Can't offline dasd device with "
  1784. "open count = %i.\n",
  1785. open_count);
  1786. else
  1787. printk (KERN_WARNING "%s",
  1788. "Can't offline dasd device due to internal "
  1789. "use\n");
  1790. clear_bit(DASD_FLAG_OFFLINE, &device->flags);
  1791. dasd_put_device(device);
  1792. return -EBUSY;
  1793. }
  1794. dasd_set_target_state(device, DASD_STATE_NEW);
  1795. /* dasd_delete_device destroys the device reference. */
  1796. dasd_delete_device(device);
  1797. return 0;
  1798. }
  1799. int
  1800. dasd_generic_notify(struct ccw_device *cdev, int event)
  1801. {
  1802. struct dasd_device *device;
  1803. struct dasd_ccw_req *cqr;
  1804. unsigned long flags;
  1805. int ret;
  1806. device = dasd_device_from_cdev(cdev);
  1807. if (IS_ERR(device))
  1808. return 0;
  1809. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1810. ret = 0;
  1811. switch (event) {
  1812. case CIO_GONE:
  1813. case CIO_NO_PATH:
  1814. /* First of all call extended error reporting. */
  1815. dasd_eer_write(device, NULL, DASD_EER_NOPATH);
  1816. if (device->state < DASD_STATE_BASIC)
  1817. break;
  1818. /* Device is active. We want to keep it. */
  1819. if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
  1820. list_for_each_entry(cqr, &device->ccw_queue, list)
  1821. if (cqr->status == DASD_CQR_IN_IO)
  1822. cqr->status = DASD_CQR_FAILED;
  1823. device->stopped |= DASD_STOPPED_DC_EIO;
  1824. } else {
  1825. list_for_each_entry(cqr, &device->ccw_queue, list)
  1826. if (cqr->status == DASD_CQR_IN_IO) {
  1827. cqr->status = DASD_CQR_QUEUED;
  1828. cqr->retries++;
  1829. }
  1830. device->stopped |= DASD_STOPPED_DC_WAIT;
  1831. dasd_set_timer(device, 0);
  1832. }
  1833. dasd_schedule_bh(device);
  1834. ret = 1;
  1835. break;
  1836. case CIO_OPER:
  1837. /* FIXME: add a sanity check. */
  1838. device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
  1839. dasd_schedule_bh(device);
  1840. ret = 1;
  1841. break;
  1842. }
  1843. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1844. dasd_put_device(device);
  1845. return ret;
  1846. }
  1847. /*
  1848. * Automatically online either all dasd devices (dasd_autodetect) or
  1849. * all devices specified with dasd= parameters.
  1850. */
  1851. static int
  1852. __dasd_auto_online(struct device *dev, void *data)
  1853. {
  1854. struct ccw_device *cdev;
  1855. cdev = to_ccwdev(dev);
  1856. if (dasd_autodetect || dasd_busid_known(cdev->dev.bus_id) == 0)
  1857. ccw_device_set_online(cdev);
  1858. return 0;
  1859. }
  1860. void
  1861. dasd_generic_auto_online (struct ccw_driver *dasd_discipline_driver)
  1862. {
  1863. struct device_driver *drv;
  1864. drv = get_driver(&dasd_discipline_driver->driver);
  1865. driver_for_each_device(drv, NULL, NULL, __dasd_auto_online);
  1866. put_driver(drv);
  1867. }
  1868. static int __init
  1869. dasd_init(void)
  1870. {
  1871. int rc;
  1872. init_waitqueue_head(&dasd_init_waitq);
  1873. /* register 'common' DASD debug area, used for all DBF_XXX calls */
  1874. dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
  1875. if (dasd_debug_area == NULL) {
  1876. rc = -ENOMEM;
  1877. goto failed;
  1878. }
  1879. debug_register_view(dasd_debug_area, &debug_sprintf_view);
  1880. debug_set_level(dasd_debug_area, DBF_EMERG);
  1881. DBF_EVENT(DBF_EMERG, "%s", "debug area created");
  1882. dasd_diag_discipline_pointer = NULL;
  1883. rc = devfs_mk_dir("dasd");
  1884. if (rc)
  1885. goto failed;
  1886. rc = dasd_devmap_init();
  1887. if (rc)
  1888. goto failed;
  1889. rc = dasd_gendisk_init();
  1890. if (rc)
  1891. goto failed;
  1892. rc = dasd_parse();
  1893. if (rc)
  1894. goto failed;
  1895. rc = dasd_eer_init();
  1896. if (rc)
  1897. goto failed;
  1898. #ifdef CONFIG_PROC_FS
  1899. rc = dasd_proc_init();
  1900. if (rc)
  1901. goto failed;
  1902. #endif
  1903. return 0;
  1904. failed:
  1905. MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
  1906. dasd_exit();
  1907. return rc;
  1908. }
  1909. module_init(dasd_init);
  1910. module_exit(dasd_exit);
  1911. EXPORT_SYMBOL(dasd_debug_area);
  1912. EXPORT_SYMBOL(dasd_diag_discipline_pointer);
  1913. EXPORT_SYMBOL(dasd_add_request_head);
  1914. EXPORT_SYMBOL(dasd_add_request_tail);
  1915. EXPORT_SYMBOL(dasd_cancel_req);
  1916. EXPORT_SYMBOL(dasd_clear_timer);
  1917. EXPORT_SYMBOL(dasd_enable_device);
  1918. EXPORT_SYMBOL(dasd_int_handler);
  1919. EXPORT_SYMBOL(dasd_kfree_request);
  1920. EXPORT_SYMBOL(dasd_kick_device);
  1921. EXPORT_SYMBOL(dasd_kmalloc_request);
  1922. EXPORT_SYMBOL(dasd_schedule_bh);
  1923. EXPORT_SYMBOL(dasd_set_target_state);
  1924. EXPORT_SYMBOL(dasd_set_timer);
  1925. EXPORT_SYMBOL(dasd_sfree_request);
  1926. EXPORT_SYMBOL(dasd_sleep_on);
  1927. EXPORT_SYMBOL(dasd_sleep_on_immediatly);
  1928. EXPORT_SYMBOL(dasd_sleep_on_interruptible);
  1929. EXPORT_SYMBOL(dasd_smalloc_request);
  1930. EXPORT_SYMBOL(dasd_start_IO);
  1931. EXPORT_SYMBOL(dasd_term_IO);
  1932. EXPORT_SYMBOL_GPL(dasd_generic_probe);
  1933. EXPORT_SYMBOL_GPL(dasd_generic_remove);
  1934. EXPORT_SYMBOL_GPL(dasd_generic_notify);
  1935. EXPORT_SYMBOL_GPL(dasd_generic_set_online);
  1936. EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
  1937. EXPORT_SYMBOL_GPL(dasd_generic_auto_online);
  1938. /*
  1939. * Overrides for Emacs so that we follow Linus's tabbing style.
  1940. * Emacs will notice this stuff at the end of the file and automatically
  1941. * adjust the settings for this buffer only. This must remain at the end
  1942. * of the file.
  1943. * ---------------------------------------------------------------------------
  1944. * Local variables:
  1945. * c-indent-level: 4
  1946. * c-brace-imaginary-offset: 0
  1947. * c-brace-offset: -4
  1948. * c-argdecl-indent: 4
  1949. * c-label-offset: -4
  1950. * c-continued-statement-offset: 4
  1951. * c-continued-brace-offset: 0
  1952. * indent-tabs-mode: 1
  1953. * tab-width: 8
  1954. * End:
  1955. */