dasd.c 58 KB

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