dasd.c 53 KB

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