dasd.c 56 KB

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