dasd.c 52 KB

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