dasd.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  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. if (cqr->retries > 0) {
  600. cqr->retries--;
  601. cqr->status = DASD_CQR_CLEAR;
  602. } else
  603. cqr->status = DASD_CQR_FAILED;
  604. cqr->stopclk = get_clock();
  605. DBF_DEV_EVENT(DBF_DEBUG, device,
  606. "terminate cqr %p successful",
  607. cqr);
  608. break;
  609. case -ENODEV:
  610. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  611. "device gone, retry");
  612. break;
  613. case -EIO:
  614. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  615. "I/O error, retry");
  616. break;
  617. case -EINVAL:
  618. case -EBUSY:
  619. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  620. "device busy, retry later");
  621. break;
  622. default:
  623. DEV_MESSAGE(KERN_ERR, device,
  624. "line %d unknown RC=%d, please "
  625. "report to linux390@de.ibm.com",
  626. __LINE__, rc);
  627. BUG();
  628. break;
  629. }
  630. retries++;
  631. }
  632. dasd_schedule_bh(device);
  633. return rc;
  634. }
  635. /*
  636. * Start the i/o. This start_IO can fail if the channel is really busy.
  637. * In that case set up a timer to start the request later.
  638. */
  639. int
  640. dasd_start_IO(struct dasd_ccw_req * cqr)
  641. {
  642. struct dasd_device *device;
  643. int rc;
  644. /* Check the cqr */
  645. rc = dasd_check_cqr(cqr);
  646. if (rc)
  647. return rc;
  648. device = (struct dasd_device *) cqr->device;
  649. if (cqr->retries < 0) {
  650. DEV_MESSAGE(KERN_DEBUG, device,
  651. "start_IO: request %p (%02x/%i) - no retry left.",
  652. cqr, cqr->status, cqr->retries);
  653. cqr->status = DASD_CQR_FAILED;
  654. return -EIO;
  655. }
  656. cqr->startclk = get_clock();
  657. cqr->starttime = jiffies;
  658. cqr->retries--;
  659. rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
  660. cqr->lpm, 0);
  661. switch (rc) {
  662. case 0:
  663. cqr->status = DASD_CQR_IN_IO;
  664. DBF_DEV_EVENT(DBF_DEBUG, device,
  665. "start_IO: request %p started successful",
  666. cqr);
  667. break;
  668. case -EBUSY:
  669. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  670. "start_IO: device busy, retry later");
  671. break;
  672. case -ETIMEDOUT:
  673. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  674. "start_IO: request timeout, retry later");
  675. break;
  676. case -EACCES:
  677. /* -EACCES indicates that the request used only a
  678. * subset of the available pathes and all these
  679. * pathes are gone.
  680. * Do a retry with all available pathes.
  681. */
  682. cqr->lpm = LPM_ANYPATH;
  683. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  684. "start_IO: selected pathes gone,"
  685. " retry on all pathes");
  686. break;
  687. case -ENODEV:
  688. case -EIO:
  689. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  690. "start_IO: device gone, retry");
  691. break;
  692. default:
  693. DEV_MESSAGE(KERN_ERR, device,
  694. "line %d unknown RC=%d, please report"
  695. " to linux390@de.ibm.com", __LINE__, rc);
  696. BUG();
  697. break;
  698. }
  699. return rc;
  700. }
  701. /*
  702. * Timeout function for dasd devices. This is used for different purposes
  703. * 1) missing interrupt handler for normal operation
  704. * 2) delayed start of request where start_IO failed with -EBUSY
  705. * 3) timeout for missing state change interrupts
  706. * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
  707. * DASD_CQR_QUEUED for 2) and 3).
  708. */
  709. static void
  710. dasd_timeout_device(unsigned long ptr)
  711. {
  712. unsigned long flags;
  713. struct dasd_device *device;
  714. device = (struct dasd_device *) ptr;
  715. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  716. /* re-activate request queue */
  717. device->stopped &= ~DASD_STOPPED_PENDING;
  718. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  719. dasd_schedule_bh(device);
  720. }
  721. /*
  722. * Setup timeout for a device in jiffies.
  723. */
  724. void
  725. dasd_set_timer(struct dasd_device *device, int expires)
  726. {
  727. if (expires == 0) {
  728. if (timer_pending(&device->timer))
  729. del_timer(&device->timer);
  730. return;
  731. }
  732. if (timer_pending(&device->timer)) {
  733. if (mod_timer(&device->timer, jiffies + expires))
  734. return;
  735. }
  736. device->timer.function = dasd_timeout_device;
  737. device->timer.data = (unsigned long) device;
  738. device->timer.expires = jiffies + expires;
  739. add_timer(&device->timer);
  740. }
  741. /*
  742. * Clear timeout for a device.
  743. */
  744. void
  745. dasd_clear_timer(struct dasd_device *device)
  746. {
  747. if (timer_pending(&device->timer))
  748. del_timer(&device->timer);
  749. }
  750. static void
  751. dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
  752. {
  753. struct dasd_ccw_req *cqr;
  754. struct dasd_device *device;
  755. cqr = (struct dasd_ccw_req *) intparm;
  756. if (cqr->status != DASD_CQR_IN_IO) {
  757. MESSAGE(KERN_DEBUG,
  758. "invalid status in handle_killed_request: "
  759. "bus_id %s, status %02x",
  760. cdev->dev.bus_id, cqr->status);
  761. return;
  762. }
  763. device = (struct dasd_device *) cqr->device;
  764. if (device == NULL ||
  765. device != dasd_device_from_cdev(cdev) ||
  766. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  767. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  768. cdev->dev.bus_id);
  769. return;
  770. }
  771. /* Schedule request to be retried. */
  772. cqr->status = DASD_CQR_QUEUED;
  773. dasd_clear_timer(device);
  774. dasd_schedule_bh(device);
  775. dasd_put_device(device);
  776. }
  777. static void
  778. dasd_handle_state_change_pending(struct dasd_device *device)
  779. {
  780. struct dasd_ccw_req *cqr;
  781. struct list_head *l, *n;
  782. device->stopped &= ~DASD_STOPPED_PENDING;
  783. /* restart all 'running' IO on queue */
  784. list_for_each_safe(l, n, &device->ccw_queue) {
  785. cqr = list_entry(l, struct dasd_ccw_req, list);
  786. if (cqr->status == DASD_CQR_IN_IO) {
  787. cqr->status = DASD_CQR_QUEUED;
  788. }
  789. }
  790. dasd_clear_timer(device);
  791. dasd_schedule_bh(device);
  792. }
  793. /*
  794. * Interrupt handler for "normal" ssch-io based dasd devices.
  795. */
  796. void
  797. dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
  798. struct irb *irb)
  799. {
  800. struct dasd_ccw_req *cqr, *next;
  801. struct dasd_device *device;
  802. unsigned long long now;
  803. int expires;
  804. dasd_era_t era;
  805. char mask;
  806. if (IS_ERR(irb)) {
  807. switch (PTR_ERR(irb)) {
  808. case -EIO:
  809. dasd_handle_killed_request(cdev, intparm);
  810. break;
  811. case -ETIMEDOUT:
  812. printk(KERN_WARNING"%s(%s): request timed out\n",
  813. __FUNCTION__, cdev->dev.bus_id);
  814. //FIXME - dasd uses own timeout interface...
  815. break;
  816. default:
  817. printk(KERN_WARNING"%s(%s): unknown error %ld\n",
  818. __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
  819. }
  820. return;
  821. }
  822. now = get_clock();
  823. DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
  824. cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
  825. (unsigned int) intparm);
  826. /* first of all check for state change pending interrupt */
  827. mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
  828. if ((irb->scsw.dstat & mask) == mask) {
  829. device = dasd_device_from_cdev(cdev);
  830. if (!IS_ERR(device)) {
  831. dasd_handle_state_change_pending(device);
  832. dasd_put_device(device);
  833. }
  834. return;
  835. }
  836. cqr = (struct dasd_ccw_req *) intparm;
  837. /* check for unsolicited interrupts */
  838. if (cqr == NULL) {
  839. MESSAGE(KERN_DEBUG,
  840. "unsolicited interrupt received: bus_id %s",
  841. cdev->dev.bus_id);
  842. return;
  843. }
  844. device = (struct dasd_device *) cqr->device;
  845. if (device == NULL ||
  846. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  847. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  848. cdev->dev.bus_id);
  849. return;
  850. }
  851. /* Check for clear pending */
  852. if (cqr->status == DASD_CQR_CLEAR &&
  853. irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
  854. cqr->status = DASD_CQR_QUEUED;
  855. dasd_clear_timer(device);
  856. dasd_schedule_bh(device);
  857. return;
  858. }
  859. /* check status - the request might have been killed by dyn detach */
  860. if (cqr->status != DASD_CQR_IN_IO) {
  861. MESSAGE(KERN_DEBUG,
  862. "invalid status: bus_id %s, status %02x",
  863. cdev->dev.bus_id, cqr->status);
  864. return;
  865. }
  866. DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
  867. ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
  868. /* Find out the appropriate era_action. */
  869. if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
  870. era = dasd_era_fatal;
  871. else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  872. irb->scsw.cstat == 0 &&
  873. !irb->esw.esw0.erw.cons)
  874. era = dasd_era_none;
  875. else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
  876. era = dasd_era_fatal; /* don't recover this request */
  877. else if (irb->esw.esw0.erw.cons)
  878. era = device->discipline->examine_error(cqr, irb);
  879. else
  880. era = dasd_era_recover;
  881. DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
  882. expires = 0;
  883. if (era == dasd_era_none) {
  884. cqr->status = DASD_CQR_DONE;
  885. cqr->stopclk = now;
  886. /* Start first request on queue if possible -> fast_io. */
  887. if (cqr->list.next != &device->ccw_queue) {
  888. next = list_entry(cqr->list.next,
  889. struct dasd_ccw_req, list);
  890. if ((next->status == DASD_CQR_QUEUED) &&
  891. (!device->stopped)) {
  892. if (device->discipline->start_IO(next) == 0)
  893. expires = next->expires;
  894. else
  895. DEV_MESSAGE(KERN_DEBUG, device, "%s",
  896. "Interrupt fastpath "
  897. "failed!");
  898. }
  899. }
  900. } else { /* error */
  901. memcpy(&cqr->irb, irb, sizeof (struct irb));
  902. #ifdef ERP_DEBUG
  903. /* dump sense data */
  904. dasd_log_sense(cqr, irb);
  905. #endif
  906. switch (era) {
  907. case dasd_era_fatal:
  908. cqr->status = DASD_CQR_FAILED;
  909. cqr->stopclk = now;
  910. break;
  911. case dasd_era_recover:
  912. cqr->status = DASD_CQR_ERROR;
  913. break;
  914. default:
  915. BUG();
  916. }
  917. }
  918. if (expires != 0)
  919. dasd_set_timer(device, expires);
  920. else
  921. dasd_clear_timer(device);
  922. dasd_schedule_bh(device);
  923. }
  924. /*
  925. * posts the buffer_cache about a finalized request
  926. */
  927. static inline void
  928. dasd_end_request(struct request *req, int uptodate)
  929. {
  930. if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
  931. BUG();
  932. add_disk_randomness(req->rq_disk);
  933. end_that_request_last(req, uptodate);
  934. }
  935. /*
  936. * Process finished error recovery ccw.
  937. */
  938. static inline void
  939. __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
  940. {
  941. dasd_erp_fn_t erp_fn;
  942. if (cqr->status == DASD_CQR_DONE)
  943. DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
  944. else
  945. DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
  946. erp_fn = device->discipline->erp_postaction(cqr);
  947. erp_fn(cqr);
  948. }
  949. /*
  950. * Process ccw request queue.
  951. */
  952. static inline void
  953. __dasd_process_ccw_queue(struct dasd_device * device,
  954. struct list_head *final_queue)
  955. {
  956. struct list_head *l, *n;
  957. struct dasd_ccw_req *cqr;
  958. dasd_erp_fn_t erp_fn;
  959. restart:
  960. /* Process request with final status. */
  961. list_for_each_safe(l, n, &device->ccw_queue) {
  962. cqr = list_entry(l, struct dasd_ccw_req, list);
  963. /* Stop list processing at the first non-final request. */
  964. if (cqr->status != DASD_CQR_DONE &&
  965. cqr->status != DASD_CQR_FAILED &&
  966. cqr->status != DASD_CQR_ERROR)
  967. break;
  968. /* Process requests with DASD_CQR_ERROR */
  969. if (cqr->status == DASD_CQR_ERROR) {
  970. if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
  971. cqr->status = DASD_CQR_FAILED;
  972. cqr->stopclk = get_clock();
  973. } else {
  974. if (cqr->irb.esw.esw0.erw.cons) {
  975. erp_fn = device->discipline->
  976. erp_action(cqr);
  977. erp_fn(cqr);
  978. } else
  979. dasd_default_erp_action(cqr);
  980. }
  981. goto restart;
  982. }
  983. /* Process finished ERP request. */
  984. if (cqr->refers) {
  985. __dasd_process_erp(device, cqr);
  986. goto restart;
  987. }
  988. /* Rechain finished requests to final queue */
  989. cqr->endclk = get_clock();
  990. list_move_tail(&cqr->list, final_queue);
  991. }
  992. }
  993. static void
  994. dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
  995. {
  996. struct request *req;
  997. struct dasd_device *device;
  998. int status;
  999. req = (struct request *) data;
  1000. device = cqr->device;
  1001. dasd_profile_end(device, cqr, req);
  1002. status = cqr->device->discipline->free_cp(cqr,req);
  1003. spin_lock_irq(&device->request_queue_lock);
  1004. dasd_end_request(req, status);
  1005. spin_unlock_irq(&device->request_queue_lock);
  1006. }
  1007. /*
  1008. * Fetch requests from the block device queue.
  1009. */
  1010. static inline void
  1011. __dasd_process_blk_queue(struct dasd_device * device)
  1012. {
  1013. request_queue_t *queue;
  1014. struct request *req;
  1015. struct dasd_ccw_req *cqr;
  1016. int nr_queued;
  1017. queue = device->request_queue;
  1018. /* No queue ? Then there is nothing to do. */
  1019. if (queue == NULL)
  1020. return;
  1021. /*
  1022. * We requeue request from the block device queue to the ccw
  1023. * queue only in two states. In state DASD_STATE_READY the
  1024. * partition detection is done and we need to requeue requests
  1025. * for that. State DASD_STATE_ONLINE is normal block device
  1026. * operation.
  1027. */
  1028. if (device->state != DASD_STATE_READY &&
  1029. device->state != DASD_STATE_ONLINE)
  1030. return;
  1031. nr_queued = 0;
  1032. /* Now we try to fetch requests from the request queue */
  1033. list_for_each_entry(cqr, &device->ccw_queue, list)
  1034. if (cqr->status == DASD_CQR_QUEUED)
  1035. nr_queued++;
  1036. while (!blk_queue_plugged(queue) &&
  1037. elv_next_request(queue) &&
  1038. nr_queued < DASD_CHANQ_MAX_SIZE) {
  1039. req = elv_next_request(queue);
  1040. if (device->features & DASD_FEATURE_READONLY &&
  1041. rq_data_dir(req) == WRITE) {
  1042. DBF_DEV_EVENT(DBF_ERR, device,
  1043. "Rejecting write request %p",
  1044. req);
  1045. blkdev_dequeue_request(req);
  1046. dasd_end_request(req, 0);
  1047. continue;
  1048. }
  1049. if (device->stopped & DASD_STOPPED_DC_EIO) {
  1050. blkdev_dequeue_request(req);
  1051. dasd_end_request(req, 0);
  1052. continue;
  1053. }
  1054. cqr = device->discipline->build_cp(device, req);
  1055. if (IS_ERR(cqr)) {
  1056. if (PTR_ERR(cqr) == -ENOMEM)
  1057. break; /* terminate request queue loop */
  1058. DBF_DEV_EVENT(DBF_ERR, device,
  1059. "CCW creation failed (rc=%ld) "
  1060. "on request %p",
  1061. PTR_ERR(cqr), req);
  1062. blkdev_dequeue_request(req);
  1063. dasd_end_request(req, 0);
  1064. continue;
  1065. }
  1066. cqr->callback = dasd_end_request_cb;
  1067. cqr->callback_data = (void *) req;
  1068. cqr->status = DASD_CQR_QUEUED;
  1069. blkdev_dequeue_request(req);
  1070. list_add_tail(&cqr->list, &device->ccw_queue);
  1071. dasd_profile_start(device, cqr, req);
  1072. nr_queued++;
  1073. }
  1074. }
  1075. /*
  1076. * Take a look at the first request on the ccw queue and check
  1077. * if it reached its expire time. If so, terminate the IO.
  1078. */
  1079. static inline void
  1080. __dasd_check_expire(struct dasd_device * device)
  1081. {
  1082. struct dasd_ccw_req *cqr;
  1083. if (list_empty(&device->ccw_queue))
  1084. return;
  1085. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1086. if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
  1087. if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
  1088. if (device->discipline->term_IO(cqr) != 0)
  1089. /* Hmpf, try again in 1/10 sec */
  1090. dasd_set_timer(device, 10);
  1091. }
  1092. }
  1093. }
  1094. /*
  1095. * Take a look at the first request on the ccw queue and check
  1096. * if it needs to be started.
  1097. */
  1098. static inline void
  1099. __dasd_start_head(struct dasd_device * device)
  1100. {
  1101. struct dasd_ccw_req *cqr;
  1102. int rc;
  1103. if (list_empty(&device->ccw_queue))
  1104. return;
  1105. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1106. /* check FAILFAST */
  1107. if (device->stopped & ~DASD_STOPPED_PENDING &&
  1108. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags)) {
  1109. cqr->status = DASD_CQR_FAILED;
  1110. dasd_schedule_bh(device);
  1111. }
  1112. if ((cqr->status == DASD_CQR_QUEUED) &&
  1113. (!device->stopped)) {
  1114. /* try to start the first I/O that can be started */
  1115. rc = device->discipline->start_IO(cqr);
  1116. if (rc == 0)
  1117. dasd_set_timer(device, cqr->expires);
  1118. else if (rc == -EACCES) {
  1119. dasd_schedule_bh(device);
  1120. } else
  1121. /* Hmpf, try again in 1/2 sec */
  1122. dasd_set_timer(device, 50);
  1123. }
  1124. }
  1125. /*
  1126. * Remove requests from the ccw queue.
  1127. */
  1128. static void
  1129. dasd_flush_ccw_queue(struct dasd_device * device, int all)
  1130. {
  1131. struct list_head flush_queue;
  1132. struct list_head *l, *n;
  1133. struct dasd_ccw_req *cqr;
  1134. INIT_LIST_HEAD(&flush_queue);
  1135. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1136. list_for_each_safe(l, n, &device->ccw_queue) {
  1137. cqr = list_entry(l, struct dasd_ccw_req, list);
  1138. /* Flush all request or only block device requests? */
  1139. if (all == 0 && cqr->callback == dasd_end_request_cb)
  1140. continue;
  1141. if (cqr->status == DASD_CQR_IN_IO)
  1142. device->discipline->term_IO(cqr);
  1143. if (cqr->status != DASD_CQR_DONE ||
  1144. cqr->status != DASD_CQR_FAILED) {
  1145. cqr->status = DASD_CQR_FAILED;
  1146. cqr->stopclk = get_clock();
  1147. }
  1148. /* Process finished ERP request. */
  1149. if (cqr->refers) {
  1150. __dasd_process_erp(device, cqr);
  1151. continue;
  1152. }
  1153. /* Rechain request on device request queue */
  1154. cqr->endclk = get_clock();
  1155. list_move_tail(&cqr->list, &flush_queue);
  1156. }
  1157. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1158. /* Now call the callback function of flushed requests */
  1159. list_for_each_safe(l, n, &flush_queue) {
  1160. cqr = list_entry(l, struct dasd_ccw_req, list);
  1161. if (cqr->callback != NULL)
  1162. (cqr->callback)(cqr, cqr->callback_data);
  1163. }
  1164. }
  1165. /*
  1166. * Acquire the device lock and process queues for the device.
  1167. */
  1168. static void
  1169. dasd_tasklet(struct dasd_device * device)
  1170. {
  1171. struct list_head final_queue;
  1172. struct list_head *l, *n;
  1173. struct dasd_ccw_req *cqr;
  1174. atomic_set (&device->tasklet_scheduled, 0);
  1175. INIT_LIST_HEAD(&final_queue);
  1176. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1177. /* Check expire time of first request on the ccw queue. */
  1178. __dasd_check_expire(device);
  1179. /* Finish off requests on ccw queue */
  1180. __dasd_process_ccw_queue(device, &final_queue);
  1181. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1182. /* Now call the callback function of requests with final status */
  1183. list_for_each_safe(l, n, &final_queue) {
  1184. cqr = list_entry(l, struct dasd_ccw_req, list);
  1185. list_del(&cqr->list);
  1186. if (cqr->callback != NULL)
  1187. (cqr->callback)(cqr, cqr->callback_data);
  1188. }
  1189. spin_lock_irq(&device->request_queue_lock);
  1190. spin_lock(get_ccwdev_lock(device->cdev));
  1191. /* Get new request from the block device request queue */
  1192. __dasd_process_blk_queue(device);
  1193. /* Now check if the head of the ccw queue needs to be started. */
  1194. __dasd_start_head(device);
  1195. spin_unlock(get_ccwdev_lock(device->cdev));
  1196. spin_unlock_irq(&device->request_queue_lock);
  1197. dasd_put_device(device);
  1198. }
  1199. /*
  1200. * Schedules a call to dasd_tasklet over the device tasklet.
  1201. */
  1202. void
  1203. dasd_schedule_bh(struct dasd_device * device)
  1204. {
  1205. /* Protect against rescheduling. */
  1206. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  1207. return;
  1208. dasd_get_device(device);
  1209. tasklet_hi_schedule(&device->tasklet);
  1210. }
  1211. /*
  1212. * Queue a request to the head of the ccw_queue. Start the I/O if
  1213. * possible.
  1214. */
  1215. void
  1216. dasd_add_request_head(struct dasd_ccw_req *req)
  1217. {
  1218. struct dasd_device *device;
  1219. unsigned long flags;
  1220. device = req->device;
  1221. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1222. req->status = DASD_CQR_QUEUED;
  1223. req->device = device;
  1224. list_add(&req->list, &device->ccw_queue);
  1225. /* let the bh start the request to keep them in order */
  1226. dasd_schedule_bh(device);
  1227. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1228. }
  1229. /*
  1230. * Queue a request to the tail of the ccw_queue. Start the I/O if
  1231. * possible.
  1232. */
  1233. void
  1234. dasd_add_request_tail(struct dasd_ccw_req *req)
  1235. {
  1236. struct dasd_device *device;
  1237. unsigned long flags;
  1238. device = req->device;
  1239. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1240. req->status = DASD_CQR_QUEUED;
  1241. req->device = device;
  1242. list_add_tail(&req->list, &device->ccw_queue);
  1243. /* let the bh start the request to keep them in order */
  1244. dasd_schedule_bh(device);
  1245. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1246. }
  1247. /*
  1248. * Wakeup callback.
  1249. */
  1250. static void
  1251. dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  1252. {
  1253. wake_up((wait_queue_head_t *) data);
  1254. }
  1255. static inline int
  1256. _wait_for_wakeup(struct dasd_ccw_req *cqr)
  1257. {
  1258. struct dasd_device *device;
  1259. int rc;
  1260. device = cqr->device;
  1261. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1262. rc = cqr->status == DASD_CQR_DONE || cqr->status == DASD_CQR_FAILED;
  1263. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1264. return rc;
  1265. }
  1266. /*
  1267. * Attempts to start a special ccw queue and waits for its completion.
  1268. */
  1269. int
  1270. dasd_sleep_on(struct dasd_ccw_req * cqr)
  1271. {
  1272. wait_queue_head_t wait_q;
  1273. struct dasd_device *device;
  1274. int rc;
  1275. device = cqr->device;
  1276. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1277. init_waitqueue_head (&wait_q);
  1278. cqr->callback = dasd_wakeup_cb;
  1279. cqr->callback_data = (void *) &wait_q;
  1280. cqr->status = DASD_CQR_QUEUED;
  1281. list_add_tail(&cqr->list, &device->ccw_queue);
  1282. /* let the bh start the request to keep them in order */
  1283. dasd_schedule_bh(device);
  1284. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1285. wait_event(wait_q, _wait_for_wakeup(cqr));
  1286. /* Request status is either done or failed. */
  1287. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1288. return rc;
  1289. }
  1290. /*
  1291. * Attempts to start a special ccw queue and wait interruptible
  1292. * for its completion.
  1293. */
  1294. int
  1295. dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
  1296. {
  1297. wait_queue_head_t wait_q;
  1298. struct dasd_device *device;
  1299. int rc, finished;
  1300. device = cqr->device;
  1301. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1302. init_waitqueue_head (&wait_q);
  1303. cqr->callback = dasd_wakeup_cb;
  1304. cqr->callback_data = (void *) &wait_q;
  1305. cqr->status = DASD_CQR_QUEUED;
  1306. list_add_tail(&cqr->list, &device->ccw_queue);
  1307. /* let the bh start the request to keep them in order */
  1308. dasd_schedule_bh(device);
  1309. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1310. finished = 0;
  1311. while (!finished) {
  1312. rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
  1313. if (rc != -ERESTARTSYS) {
  1314. /* Request status is either done or failed. */
  1315. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1316. break;
  1317. }
  1318. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1319. if (cqr->status == DASD_CQR_IN_IO &&
  1320. device->discipline->term_IO(cqr) == 0) {
  1321. list_del(&cqr->list);
  1322. finished = 1;
  1323. }
  1324. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1325. }
  1326. return rc;
  1327. }
  1328. /*
  1329. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  1330. * for eckd devices) the currently running request has to be terminated
  1331. * and be put back to status queued, before the special request is added
  1332. * to the head of the queue. Then the special request is waited on normally.
  1333. */
  1334. static inline int
  1335. _dasd_term_running_cqr(struct dasd_device *device)
  1336. {
  1337. struct dasd_ccw_req *cqr;
  1338. int rc;
  1339. if (list_empty(&device->ccw_queue))
  1340. return 0;
  1341. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1342. rc = device->discipline->term_IO(cqr);
  1343. if (rc == 0) {
  1344. /* termination successful */
  1345. cqr->status = DASD_CQR_QUEUED;
  1346. cqr->startclk = cqr->stopclk = 0;
  1347. cqr->starttime = 0;
  1348. }
  1349. return rc;
  1350. }
  1351. int
  1352. dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
  1353. {
  1354. wait_queue_head_t wait_q;
  1355. struct dasd_device *device;
  1356. int rc;
  1357. device = cqr->device;
  1358. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1359. rc = _dasd_term_running_cqr(device);
  1360. if (rc) {
  1361. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1362. return rc;
  1363. }
  1364. init_waitqueue_head (&wait_q);
  1365. cqr->callback = dasd_wakeup_cb;
  1366. cqr->callback_data = (void *) &wait_q;
  1367. cqr->status = DASD_CQR_QUEUED;
  1368. list_add(&cqr->list, &device->ccw_queue);
  1369. /* let the bh start the request to keep them in order */
  1370. dasd_schedule_bh(device);
  1371. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1372. wait_event(wait_q, _wait_for_wakeup(cqr));
  1373. /* Request status is either done or failed. */
  1374. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1375. return rc;
  1376. }
  1377. /*
  1378. * Cancels a request that was started with dasd_sleep_on_req.
  1379. * This is useful to timeout requests. The request will be
  1380. * terminated if it is currently in i/o.
  1381. * Returns 1 if the request has been terminated.
  1382. */
  1383. int
  1384. dasd_cancel_req(struct dasd_ccw_req *cqr)
  1385. {
  1386. struct dasd_device *device = cqr->device;
  1387. unsigned long flags;
  1388. int rc;
  1389. rc = 0;
  1390. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1391. switch (cqr->status) {
  1392. case DASD_CQR_QUEUED:
  1393. /* request was not started - just set to failed */
  1394. cqr->status = DASD_CQR_FAILED;
  1395. break;
  1396. case DASD_CQR_IN_IO:
  1397. /* request in IO - terminate IO and release again */
  1398. if (device->discipline->term_IO(cqr) != 0)
  1399. /* what to do if unable to terminate ??????
  1400. e.g. not _IN_IO */
  1401. cqr->status = DASD_CQR_FAILED;
  1402. cqr->stopclk = get_clock();
  1403. rc = 1;
  1404. break;
  1405. case DASD_CQR_DONE:
  1406. case DASD_CQR_FAILED:
  1407. /* already finished - do nothing */
  1408. break;
  1409. default:
  1410. DEV_MESSAGE(KERN_ALERT, device,
  1411. "invalid status %02x in request",
  1412. cqr->status);
  1413. BUG();
  1414. }
  1415. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1416. dasd_schedule_bh(device);
  1417. return rc;
  1418. }
  1419. /*
  1420. * SECTION: Block device operations (request queue, partitions, open, release).
  1421. */
  1422. /*
  1423. * Dasd request queue function. Called from ll_rw_blk.c
  1424. */
  1425. static void
  1426. do_dasd_request(request_queue_t * queue)
  1427. {
  1428. struct dasd_device *device;
  1429. device = (struct dasd_device *) queue->queuedata;
  1430. spin_lock(get_ccwdev_lock(device->cdev));
  1431. /* Get new request from the block device request queue */
  1432. __dasd_process_blk_queue(device);
  1433. /* Now check if the head of the ccw queue needs to be started. */
  1434. __dasd_start_head(device);
  1435. spin_unlock(get_ccwdev_lock(device->cdev));
  1436. }
  1437. /*
  1438. * Allocate and initialize request queue and default I/O scheduler.
  1439. */
  1440. static int
  1441. dasd_alloc_queue(struct dasd_device * device)
  1442. {
  1443. int rc;
  1444. device->request_queue = blk_init_queue(do_dasd_request,
  1445. &device->request_queue_lock);
  1446. if (device->request_queue == NULL)
  1447. return -ENOMEM;
  1448. device->request_queue->queuedata = device;
  1449. elevator_exit(device->request_queue->elevator);
  1450. rc = elevator_init(device->request_queue, "deadline");
  1451. if (rc) {
  1452. blk_cleanup_queue(device->request_queue);
  1453. return rc;
  1454. }
  1455. return 0;
  1456. }
  1457. /*
  1458. * Allocate and initialize request queue.
  1459. */
  1460. static void
  1461. dasd_setup_queue(struct dasd_device * device)
  1462. {
  1463. int max;
  1464. blk_queue_hardsect_size(device->request_queue, device->bp_block);
  1465. max = device->discipline->max_blocks << device->s2b_shift;
  1466. blk_queue_max_sectors(device->request_queue, max);
  1467. blk_queue_max_phys_segments(device->request_queue, -1L);
  1468. blk_queue_max_hw_segments(device->request_queue, -1L);
  1469. blk_queue_max_segment_size(device->request_queue, -1L);
  1470. blk_queue_segment_boundary(device->request_queue, -1L);
  1471. blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
  1472. }
  1473. /*
  1474. * Deactivate and free request queue.
  1475. */
  1476. static void
  1477. dasd_free_queue(struct dasd_device * device)
  1478. {
  1479. if (device->request_queue) {
  1480. blk_cleanup_queue(device->request_queue);
  1481. device->request_queue = NULL;
  1482. }
  1483. }
  1484. /*
  1485. * Flush request on the request queue.
  1486. */
  1487. static void
  1488. dasd_flush_request_queue(struct dasd_device * device)
  1489. {
  1490. struct request *req;
  1491. if (!device->request_queue)
  1492. return;
  1493. spin_lock_irq(&device->request_queue_lock);
  1494. while (!list_empty(&device->request_queue->queue_head)) {
  1495. req = elv_next_request(device->request_queue);
  1496. if (req == NULL)
  1497. break;
  1498. dasd_end_request(req, 0);
  1499. blkdev_dequeue_request(req);
  1500. }
  1501. spin_unlock_irq(&device->request_queue_lock);
  1502. }
  1503. static int
  1504. dasd_open(struct inode *inp, struct file *filp)
  1505. {
  1506. struct gendisk *disk = inp->i_bdev->bd_disk;
  1507. struct dasd_device *device = disk->private_data;
  1508. int rc;
  1509. atomic_inc(&device->open_count);
  1510. if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1511. rc = -ENODEV;
  1512. goto unlock;
  1513. }
  1514. if (!try_module_get(device->discipline->owner)) {
  1515. rc = -EINVAL;
  1516. goto unlock;
  1517. }
  1518. if (dasd_probeonly) {
  1519. DEV_MESSAGE(KERN_INFO, device, "%s",
  1520. "No access to device due to probeonly mode");
  1521. rc = -EPERM;
  1522. goto out;
  1523. }
  1524. if (device->state < DASD_STATE_BASIC) {
  1525. DBF_DEV_EVENT(DBF_ERR, device, " %s",
  1526. " Cannot open unrecognized device");
  1527. rc = -ENODEV;
  1528. goto out;
  1529. }
  1530. return 0;
  1531. out:
  1532. module_put(device->discipline->owner);
  1533. unlock:
  1534. atomic_dec(&device->open_count);
  1535. return rc;
  1536. }
  1537. static int
  1538. dasd_release(struct inode *inp, struct file *filp)
  1539. {
  1540. struct gendisk *disk = inp->i_bdev->bd_disk;
  1541. struct dasd_device *device = disk->private_data;
  1542. atomic_dec(&device->open_count);
  1543. module_put(device->discipline->owner);
  1544. return 0;
  1545. }
  1546. /*
  1547. * Return disk geometry.
  1548. */
  1549. static int
  1550. dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1551. {
  1552. struct dasd_device *device;
  1553. device = bdev->bd_disk->private_data;
  1554. if (!device)
  1555. return -ENODEV;
  1556. if (!device->discipline ||
  1557. !device->discipline->fill_geometry)
  1558. return -EINVAL;
  1559. device->discipline->fill_geometry(device, geo);
  1560. geo->start = get_start_sect(bdev) >> device->s2b_shift;
  1561. return 0;
  1562. }
  1563. struct block_device_operations
  1564. dasd_device_operations = {
  1565. .owner = THIS_MODULE,
  1566. .open = dasd_open,
  1567. .release = dasd_release,
  1568. .ioctl = dasd_ioctl,
  1569. .compat_ioctl = dasd_compat_ioctl,
  1570. .getgeo = dasd_getgeo,
  1571. };
  1572. static void
  1573. dasd_exit(void)
  1574. {
  1575. #ifdef CONFIG_PROC_FS
  1576. dasd_proc_exit();
  1577. #endif
  1578. dasd_ioctl_exit();
  1579. if (dasd_page_cache != NULL) {
  1580. kmem_cache_destroy(dasd_page_cache);
  1581. dasd_page_cache = NULL;
  1582. }
  1583. dasd_gendisk_exit();
  1584. dasd_devmap_exit();
  1585. devfs_remove("dasd");
  1586. if (dasd_debug_area != NULL) {
  1587. debug_unregister(dasd_debug_area);
  1588. dasd_debug_area = NULL;
  1589. }
  1590. }
  1591. /*
  1592. * SECTION: common functions for ccw_driver use
  1593. */
  1594. /*
  1595. * Initial attempt at a probe function. this can be simplified once
  1596. * the other detection code is gone.
  1597. */
  1598. int
  1599. dasd_generic_probe (struct ccw_device *cdev,
  1600. struct dasd_discipline *discipline)
  1601. {
  1602. int ret;
  1603. ret = dasd_add_sysfs_files(cdev);
  1604. if (ret) {
  1605. printk(KERN_WARNING
  1606. "dasd_generic_probe: could not add sysfs entries "
  1607. "for %s\n", cdev->dev.bus_id);
  1608. } else {
  1609. cdev->handler = &dasd_int_handler;
  1610. }
  1611. return ret;
  1612. }
  1613. /*
  1614. * This will one day be called from a global not_oper handler.
  1615. * It is also used by driver_unregister during module unload.
  1616. */
  1617. void
  1618. dasd_generic_remove (struct ccw_device *cdev)
  1619. {
  1620. struct dasd_device *device;
  1621. cdev->handler = NULL;
  1622. dasd_remove_sysfs_files(cdev);
  1623. device = dasd_device_from_cdev(cdev);
  1624. if (IS_ERR(device))
  1625. return;
  1626. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1627. /* Already doing offline processing */
  1628. dasd_put_device(device);
  1629. return;
  1630. }
  1631. /*
  1632. * This device is removed unconditionally. Set offline
  1633. * flag to prevent dasd_open from opening it while it is
  1634. * no quite down yet.
  1635. */
  1636. dasd_set_target_state(device, DASD_STATE_NEW);
  1637. /* dasd_delete_device destroys the device reference. */
  1638. dasd_delete_device(device);
  1639. }
  1640. /*
  1641. * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
  1642. * the device is detected for the first time and is supposed to be used
  1643. * or the user has started activation through sysfs.
  1644. */
  1645. int
  1646. dasd_generic_set_online (struct ccw_device *cdev,
  1647. struct dasd_discipline *discipline)
  1648. {
  1649. struct dasd_device *device;
  1650. int rc;
  1651. device = dasd_create_device(cdev);
  1652. if (IS_ERR(device))
  1653. return PTR_ERR(device);
  1654. if (device->features & DASD_FEATURE_USEDIAG) {
  1655. if (!dasd_diag_discipline_pointer) {
  1656. printk (KERN_WARNING
  1657. "dasd_generic couldn't online device %s "
  1658. "- discipline DIAG not available\n",
  1659. cdev->dev.bus_id);
  1660. dasd_delete_device(device);
  1661. return -ENODEV;
  1662. }
  1663. discipline = dasd_diag_discipline_pointer;
  1664. }
  1665. device->discipline = discipline;
  1666. rc = discipline->check_device(device);
  1667. if (rc) {
  1668. printk (KERN_WARNING
  1669. "dasd_generic couldn't online device %s "
  1670. "with discipline %s rc=%i\n",
  1671. cdev->dev.bus_id, discipline->name, rc);
  1672. dasd_delete_device(device);
  1673. return rc;
  1674. }
  1675. dasd_set_target_state(device, DASD_STATE_ONLINE);
  1676. if (device->state <= DASD_STATE_KNOWN) {
  1677. printk (KERN_WARNING
  1678. "dasd_generic discipline not found for %s\n",
  1679. cdev->dev.bus_id);
  1680. rc = -ENODEV;
  1681. dasd_set_target_state(device, DASD_STATE_NEW);
  1682. dasd_delete_device(device);
  1683. } else
  1684. pr_debug("dasd_generic device %s found\n",
  1685. cdev->dev.bus_id);
  1686. /* FIXME: we have to wait for the root device but we don't want
  1687. * to wait for each single device but for all at once. */
  1688. wait_event(dasd_init_waitq, _wait_for_device(device));
  1689. dasd_put_device(device);
  1690. return rc;
  1691. }
  1692. int
  1693. dasd_generic_set_offline (struct ccw_device *cdev)
  1694. {
  1695. struct dasd_device *device;
  1696. int max_count;
  1697. device = dasd_device_from_cdev(cdev);
  1698. if (IS_ERR(device))
  1699. return PTR_ERR(device);
  1700. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1701. /* Already doing offline processing */
  1702. dasd_put_device(device);
  1703. return 0;
  1704. }
  1705. /*
  1706. * We must make sure that this device is currently not in use.
  1707. * The open_count is increased for every opener, that includes
  1708. * the blkdev_get in dasd_scan_partitions. We are only interested
  1709. * in the other openers.
  1710. */
  1711. max_count = device->bdev ? 0 : -1;
  1712. if (atomic_read(&device->open_count) > max_count) {
  1713. printk (KERN_WARNING "Can't offline dasd device with open"
  1714. " count = %i.\n",
  1715. atomic_read(&device->open_count));
  1716. clear_bit(DASD_FLAG_OFFLINE, &device->flags);
  1717. dasd_put_device(device);
  1718. return -EBUSY;
  1719. }
  1720. dasd_set_target_state(device, DASD_STATE_NEW);
  1721. /* dasd_delete_device destroys the device reference. */
  1722. dasd_delete_device(device);
  1723. return 0;
  1724. }
  1725. int
  1726. dasd_generic_notify(struct ccw_device *cdev, int event)
  1727. {
  1728. struct dasd_device *device;
  1729. struct dasd_ccw_req *cqr;
  1730. unsigned long flags;
  1731. int ret;
  1732. device = dasd_device_from_cdev(cdev);
  1733. if (IS_ERR(device))
  1734. return 0;
  1735. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1736. ret = 0;
  1737. switch (event) {
  1738. case CIO_GONE:
  1739. case CIO_NO_PATH:
  1740. if (device->state < DASD_STATE_BASIC)
  1741. break;
  1742. /* Device is active. We want to keep it. */
  1743. if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
  1744. list_for_each_entry(cqr, &device->ccw_queue, list)
  1745. if (cqr->status == DASD_CQR_IN_IO)
  1746. cqr->status = DASD_CQR_FAILED;
  1747. device->stopped |= DASD_STOPPED_DC_EIO;
  1748. } else {
  1749. list_for_each_entry(cqr, &device->ccw_queue, list)
  1750. if (cqr->status == DASD_CQR_IN_IO) {
  1751. cqr->status = DASD_CQR_QUEUED;
  1752. cqr->retries++;
  1753. }
  1754. device->stopped |= DASD_STOPPED_DC_WAIT;
  1755. dasd_set_timer(device, 0);
  1756. }
  1757. dasd_schedule_bh(device);
  1758. ret = 1;
  1759. break;
  1760. case CIO_OPER:
  1761. /* FIXME: add a sanity check. */
  1762. device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
  1763. dasd_schedule_bh(device);
  1764. ret = 1;
  1765. break;
  1766. }
  1767. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1768. dasd_put_device(device);
  1769. return ret;
  1770. }
  1771. /*
  1772. * Automatically online either all dasd devices (dasd_autodetect) or
  1773. * all devices specified with dasd= parameters.
  1774. */
  1775. static int
  1776. __dasd_auto_online(struct device *dev, void *data)
  1777. {
  1778. struct ccw_device *cdev;
  1779. cdev = to_ccwdev(dev);
  1780. if (dasd_autodetect || dasd_busid_known(cdev->dev.bus_id) == 0)
  1781. ccw_device_set_online(cdev);
  1782. return 0;
  1783. }
  1784. void
  1785. dasd_generic_auto_online (struct ccw_driver *dasd_discipline_driver)
  1786. {
  1787. struct device_driver *drv;
  1788. drv = get_driver(&dasd_discipline_driver->driver);
  1789. driver_for_each_device(drv, NULL, NULL, __dasd_auto_online);
  1790. put_driver(drv);
  1791. }
  1792. static int __init
  1793. dasd_init(void)
  1794. {
  1795. int rc;
  1796. init_waitqueue_head(&dasd_init_waitq);
  1797. /* register 'common' DASD debug area, used for all DBF_XXX calls */
  1798. dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
  1799. if (dasd_debug_area == NULL) {
  1800. rc = -ENOMEM;
  1801. goto failed;
  1802. }
  1803. debug_register_view(dasd_debug_area, &debug_sprintf_view);
  1804. debug_set_level(dasd_debug_area, DBF_EMERG);
  1805. DBF_EVENT(DBF_EMERG, "%s", "debug area created");
  1806. dasd_diag_discipline_pointer = NULL;
  1807. rc = devfs_mk_dir("dasd");
  1808. if (rc)
  1809. goto failed;
  1810. rc = dasd_devmap_init();
  1811. if (rc)
  1812. goto failed;
  1813. rc = dasd_gendisk_init();
  1814. if (rc)
  1815. goto failed;
  1816. rc = dasd_parse();
  1817. if (rc)
  1818. goto failed;
  1819. rc = dasd_ioctl_init();
  1820. if (rc)
  1821. goto failed;
  1822. #ifdef CONFIG_PROC_FS
  1823. rc = dasd_proc_init();
  1824. if (rc)
  1825. goto failed;
  1826. #endif
  1827. return 0;
  1828. failed:
  1829. MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
  1830. dasd_exit();
  1831. return rc;
  1832. }
  1833. module_init(dasd_init);
  1834. module_exit(dasd_exit);
  1835. EXPORT_SYMBOL(dasd_debug_area);
  1836. EXPORT_SYMBOL(dasd_diag_discipline_pointer);
  1837. EXPORT_SYMBOL(dasd_add_request_head);
  1838. EXPORT_SYMBOL(dasd_add_request_tail);
  1839. EXPORT_SYMBOL(dasd_cancel_req);
  1840. EXPORT_SYMBOL(dasd_clear_timer);
  1841. EXPORT_SYMBOL(dasd_enable_device);
  1842. EXPORT_SYMBOL(dasd_int_handler);
  1843. EXPORT_SYMBOL(dasd_kfree_request);
  1844. EXPORT_SYMBOL(dasd_kick_device);
  1845. EXPORT_SYMBOL(dasd_kmalloc_request);
  1846. EXPORT_SYMBOL(dasd_schedule_bh);
  1847. EXPORT_SYMBOL(dasd_set_target_state);
  1848. EXPORT_SYMBOL(dasd_set_timer);
  1849. EXPORT_SYMBOL(dasd_sfree_request);
  1850. EXPORT_SYMBOL(dasd_sleep_on);
  1851. EXPORT_SYMBOL(dasd_sleep_on_immediatly);
  1852. EXPORT_SYMBOL(dasd_sleep_on_interruptible);
  1853. EXPORT_SYMBOL(dasd_smalloc_request);
  1854. EXPORT_SYMBOL(dasd_start_IO);
  1855. EXPORT_SYMBOL(dasd_term_IO);
  1856. EXPORT_SYMBOL_GPL(dasd_generic_probe);
  1857. EXPORT_SYMBOL_GPL(dasd_generic_remove);
  1858. EXPORT_SYMBOL_GPL(dasd_generic_notify);
  1859. EXPORT_SYMBOL_GPL(dasd_generic_set_online);
  1860. EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
  1861. EXPORT_SYMBOL_GPL(dasd_generic_auto_online);
  1862. /*
  1863. * Overrides for Emacs so that we follow Linus's tabbing style.
  1864. * Emacs will notice this stuff at the end of the file and automatically
  1865. * adjust the settings for this buffer only. This must remain at the end
  1866. * of the file.
  1867. * ---------------------------------------------------------------------------
  1868. * Local variables:
  1869. * c-indent-level: 4
  1870. * c-brace-imaginary-offset: 0
  1871. * c-brace-offset: -4
  1872. * c-argdecl-indent: 4
  1873. * c-label-offset: -4
  1874. * c-continued-statement-offset: 4
  1875. * c-continued-brace-offset: 0
  1876. * indent-tabs-mode: 1
  1877. * tab-width: 8
  1878. * End:
  1879. */