dasd.c 55 KB

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