dasd.c 54 KB

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