dasd.c 55 KB

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