dasd.c 56 KB

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