dasd.c 53 KB

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