dasd.c 57 KB

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