dasd.c 55 KB

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