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. * $Revision: 1.172 $
  11. */
  12. #include <linux/config.h>
  13. #include <linux/kmod.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ctype.h>
  17. #include <linux/major.h>
  18. #include <linux/slab.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/hdreg.h>
  21. #include <asm/ccwdev.h>
  22. #include <asm/ebcdic.h>
  23. #include <asm/idals.h>
  24. #include <asm/todclk.h>
  25. /* This is ugly... */
  26. #define PRINTK_HEADER "dasd:"
  27. #include "dasd_int.h"
  28. /*
  29. * SECTION: Constant definitions to be used within this file
  30. */
  31. #define DASD_CHANQ_MAX_SIZE 4
  32. /*
  33. * SECTION: exported variables of dasd.c
  34. */
  35. debug_info_t *dasd_debug_area;
  36. struct dasd_discipline *dasd_diag_discipline_pointer;
  37. MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
  38. MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
  39. " Copyright 2000 IBM Corporation");
  40. MODULE_SUPPORTED_DEVICE("dasd");
  41. MODULE_PARM(dasd, "1-" __MODULE_STRING(256) "s");
  42. MODULE_LICENSE("GPL");
  43. /*
  44. * SECTION: prototypes for static functions of dasd.c
  45. */
  46. static int dasd_alloc_queue(struct dasd_device * device);
  47. static void dasd_setup_queue(struct dasd_device * device);
  48. static void dasd_free_queue(struct dasd_device * device);
  49. static void dasd_flush_request_queue(struct dasd_device *);
  50. static void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
  51. static void dasd_flush_ccw_queue(struct dasd_device *, int);
  52. static void dasd_tasklet(struct dasd_device *);
  53. static void do_kick_device(void *data);
  54. /*
  55. * SECTION: Operations on the device structure.
  56. */
  57. static wait_queue_head_t dasd_init_waitq;
  58. /*
  59. * Allocate memory for a new device structure.
  60. */
  61. struct dasd_device *
  62. dasd_alloc_device(void)
  63. {
  64. struct dasd_device *device;
  65. device = kmalloc(sizeof (struct dasd_device), GFP_ATOMIC);
  66. if (device == NULL)
  67. return ERR_PTR(-ENOMEM);
  68. memset(device, 0, sizeof (struct dasd_device));
  69. /* open_count = 0 means device online but not in use */
  70. atomic_set(&device->open_count, -1);
  71. /* Get two pages for normal block device operations. */
  72. device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
  73. if (device->ccw_mem == NULL) {
  74. kfree(device);
  75. return ERR_PTR(-ENOMEM);
  76. }
  77. /* Get one page for error recovery. */
  78. device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
  79. if (device->erp_mem == NULL) {
  80. free_pages((unsigned long) device->ccw_mem, 1);
  81. kfree(device);
  82. return ERR_PTR(-ENOMEM);
  83. }
  84. dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
  85. dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
  86. spin_lock_init(&device->mem_lock);
  87. spin_lock_init(&device->request_queue_lock);
  88. atomic_set (&device->tasklet_scheduled, 0);
  89. tasklet_init(&device->tasklet,
  90. (void (*)(unsigned long)) dasd_tasklet,
  91. (unsigned long) device);
  92. INIT_LIST_HEAD(&device->ccw_queue);
  93. init_timer(&device->timer);
  94. INIT_WORK(&device->kick_work, do_kick_device, device);
  95. device->state = DASD_STATE_NEW;
  96. device->target = DASD_STATE_NEW;
  97. return device;
  98. }
  99. /*
  100. * Free memory of a device structure.
  101. */
  102. void
  103. dasd_free_device(struct dasd_device *device)
  104. {
  105. kfree(device->private);
  106. free_page((unsigned long) device->erp_mem);
  107. free_pages((unsigned long) device->ccw_mem, 1);
  108. kfree(device);
  109. }
  110. /*
  111. * Make a new device known to the system.
  112. */
  113. static inline int
  114. dasd_state_new_to_known(struct dasd_device *device)
  115. {
  116. int rc;
  117. /*
  118. * As long as the device is not in state DASD_STATE_NEW we want to
  119. * keep the reference count > 0.
  120. */
  121. dasd_get_device(device);
  122. rc = dasd_alloc_queue(device);
  123. if (rc) {
  124. dasd_put_device(device);
  125. return rc;
  126. }
  127. device->state = DASD_STATE_KNOWN;
  128. return 0;
  129. }
  130. /*
  131. * Let the system forget about a device.
  132. */
  133. static inline void
  134. dasd_state_known_to_new(struct dasd_device * device)
  135. {
  136. /* Forget the discipline information. */
  137. device->discipline = NULL;
  138. device->state = DASD_STATE_NEW;
  139. dasd_free_queue(device);
  140. /* Give up reference we took in dasd_state_new_to_known. */
  141. dasd_put_device(device);
  142. }
  143. /*
  144. * Request the irq line for the device.
  145. */
  146. static inline int
  147. dasd_state_known_to_basic(struct dasd_device * device)
  148. {
  149. int rc;
  150. /* Allocate and register gendisk structure. */
  151. rc = dasd_gendisk_alloc(device);
  152. if (rc)
  153. return rc;
  154. /* register 'device' debug area, used for all DBF_DEV_XXX calls */
  155. device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
  156. 8 * sizeof (long));
  157. debug_register_view(device->debug_area, &debug_sprintf_view);
  158. debug_set_level(device->debug_area, DBF_EMERG);
  159. DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
  160. device->state = DASD_STATE_BASIC;
  161. return 0;
  162. }
  163. /*
  164. * Release the irq line for the device. Terminate any running i/o.
  165. */
  166. static inline void
  167. dasd_state_basic_to_known(struct dasd_device * device)
  168. {
  169. dasd_gendisk_free(device);
  170. dasd_flush_ccw_queue(device, 1);
  171. DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
  172. if (device->debug_area != NULL) {
  173. debug_unregister(device->debug_area);
  174. device->debug_area = NULL;
  175. }
  176. device->state = DASD_STATE_KNOWN;
  177. }
  178. /*
  179. * Do the initial analysis. The do_analysis function may return
  180. * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
  181. * until the discipline decides to continue the startup sequence
  182. * by calling the function dasd_change_state. The eckd disciplines
  183. * uses this to start a ccw that detects the format. The completion
  184. * interrupt for this detection ccw uses the kernel event daemon to
  185. * trigger the call to dasd_change_state. All this is done in the
  186. * discipline code, see dasd_eckd.c.
  187. * After the analysis ccw is done (do_analysis returned 0 or error)
  188. * the block device is setup. Either a fake disk is added to allow
  189. * formatting or a proper device request queue is created.
  190. */
  191. static inline int
  192. dasd_state_basic_to_ready(struct dasd_device * device)
  193. {
  194. int rc;
  195. rc = 0;
  196. if (device->discipline->do_analysis != NULL)
  197. rc = device->discipline->do_analysis(device);
  198. if (rc)
  199. return rc;
  200. dasd_setup_queue(device);
  201. device->state = DASD_STATE_READY;
  202. if (dasd_scan_partitions(device) != 0)
  203. device->state = DASD_STATE_BASIC;
  204. return 0;
  205. }
  206. /*
  207. * Remove device from block device layer. Destroy dirty buffers.
  208. * Forget format information. Check if the target level is basic
  209. * and if it is create fake disk for formatting.
  210. */
  211. static inline void
  212. dasd_state_ready_to_basic(struct dasd_device * device)
  213. {
  214. dasd_flush_ccw_queue(device, 0);
  215. dasd_destroy_partitions(device);
  216. dasd_flush_request_queue(device);
  217. device->blocks = 0;
  218. device->bp_block = 0;
  219. device->s2b_shift = 0;
  220. device->state = DASD_STATE_BASIC;
  221. }
  222. /*
  223. * Make the device online and schedule the bottom half to start
  224. * the requeueing of requests from the linux request queue to the
  225. * ccw queue.
  226. */
  227. static inline int
  228. dasd_state_ready_to_online(struct dasd_device * device)
  229. {
  230. device->state = DASD_STATE_ONLINE;
  231. dasd_schedule_bh(device);
  232. return 0;
  233. }
  234. /*
  235. * Stop the requeueing of requests again.
  236. */
  237. static inline void
  238. dasd_state_online_to_ready(struct dasd_device * device)
  239. {
  240. device->state = DASD_STATE_READY;
  241. }
  242. /*
  243. * Device startup state changes.
  244. */
  245. static inline int
  246. dasd_increase_state(struct dasd_device *device)
  247. {
  248. int rc;
  249. rc = 0;
  250. if (device->state == DASD_STATE_NEW &&
  251. device->target >= DASD_STATE_KNOWN)
  252. rc = dasd_state_new_to_known(device);
  253. if (!rc &&
  254. device->state == DASD_STATE_KNOWN &&
  255. device->target >= DASD_STATE_BASIC)
  256. rc = dasd_state_known_to_basic(device);
  257. if (!rc &&
  258. device->state == DASD_STATE_BASIC &&
  259. device->target >= DASD_STATE_READY)
  260. rc = dasd_state_basic_to_ready(device);
  261. if (!rc &&
  262. device->state == DASD_STATE_READY &&
  263. device->target >= DASD_STATE_ONLINE)
  264. rc = dasd_state_ready_to_online(device);
  265. return rc;
  266. }
  267. /*
  268. * Device shutdown state changes.
  269. */
  270. static inline int
  271. dasd_decrease_state(struct dasd_device *device)
  272. {
  273. if (device->state == DASD_STATE_ONLINE &&
  274. device->target <= DASD_STATE_READY)
  275. dasd_state_online_to_ready(device);
  276. if (device->state == DASD_STATE_READY &&
  277. device->target <= DASD_STATE_BASIC)
  278. dasd_state_ready_to_basic(device);
  279. if (device->state == DASD_STATE_BASIC &&
  280. device->target <= DASD_STATE_KNOWN)
  281. dasd_state_basic_to_known(device);
  282. if (device->state == DASD_STATE_KNOWN &&
  283. device->target <= DASD_STATE_NEW)
  284. dasd_state_known_to_new(device);
  285. return 0;
  286. }
  287. /*
  288. * This is the main startup/shutdown routine.
  289. */
  290. static void
  291. dasd_change_state(struct dasd_device *device)
  292. {
  293. int rc;
  294. if (device->state == device->target)
  295. /* Already where we want to go today... */
  296. return;
  297. if (device->state < device->target)
  298. rc = dasd_increase_state(device);
  299. else
  300. rc = dasd_decrease_state(device);
  301. if (rc && rc != -EAGAIN)
  302. device->target = device->state;
  303. if (device->state == device->target)
  304. wake_up(&dasd_init_waitq);
  305. }
  306. /*
  307. * Kick starter for devices that did not complete the startup/shutdown
  308. * procedure or were sleeping because of a pending state.
  309. * dasd_kick_device will schedule a call do do_kick_device to the kernel
  310. * event daemon.
  311. */
  312. static void
  313. do_kick_device(void *data)
  314. {
  315. struct dasd_device *device;
  316. device = (struct dasd_device *) data;
  317. dasd_change_state(device);
  318. dasd_schedule_bh(device);
  319. dasd_put_device(device);
  320. }
  321. void
  322. dasd_kick_device(struct dasd_device *device)
  323. {
  324. dasd_get_device(device);
  325. /* queue call to dasd_kick_device to the kernel event daemon. */
  326. schedule_work(&device->kick_work);
  327. }
  328. /*
  329. * Set the target state for a device and starts the state change.
  330. */
  331. void
  332. dasd_set_target_state(struct dasd_device *device, int target)
  333. {
  334. /* If we are in probeonly mode stop at DASD_STATE_READY. */
  335. if (dasd_probeonly && target > DASD_STATE_READY)
  336. target = DASD_STATE_READY;
  337. if (device->target != target) {
  338. if (device->state == target)
  339. wake_up(&dasd_init_waitq);
  340. device->target = target;
  341. }
  342. if (device->state != device->target)
  343. dasd_change_state(device);
  344. }
  345. /*
  346. * Enable devices with device numbers in [from..to].
  347. */
  348. static inline int
  349. _wait_for_device(struct dasd_device *device)
  350. {
  351. return (device->state == device->target);
  352. }
  353. void
  354. dasd_enable_device(struct dasd_device *device)
  355. {
  356. dasd_set_target_state(device, DASD_STATE_ONLINE);
  357. if (device->state <= DASD_STATE_KNOWN)
  358. /* No discipline for device found. */
  359. dasd_set_target_state(device, DASD_STATE_NEW);
  360. /* Now wait for the devices to come up. */
  361. wait_event(dasd_init_waitq, _wait_for_device(device));
  362. }
  363. /*
  364. * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
  365. */
  366. #ifdef CONFIG_DASD_PROFILE
  367. struct dasd_profile_info_t dasd_global_profile;
  368. unsigned int dasd_profile_level = DASD_PROFILE_OFF;
  369. /*
  370. * Increments counter in global and local profiling structures.
  371. */
  372. #define dasd_profile_counter(value, counter, device) \
  373. { \
  374. int index; \
  375. for (index = 0; index < 31 && value >> (2+index); index++); \
  376. dasd_global_profile.counter[index]++; \
  377. device->profile.counter[index]++; \
  378. }
  379. /*
  380. * Add profiling information for cqr before execution.
  381. */
  382. static inline void
  383. dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
  384. struct request *req)
  385. {
  386. struct list_head *l;
  387. unsigned int counter;
  388. if (dasd_profile_level != DASD_PROFILE_ON)
  389. return;
  390. /* count the length of the chanq for statistics */
  391. counter = 0;
  392. list_for_each(l, &device->ccw_queue)
  393. if (++counter >= 31)
  394. break;
  395. dasd_global_profile.dasd_io_nr_req[counter]++;
  396. device->profile.dasd_io_nr_req[counter]++;
  397. }
  398. /*
  399. * Add profiling information for cqr after execution.
  400. */
  401. static inline void
  402. dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
  403. struct request *req)
  404. {
  405. long strtime, irqtime, endtime, tottime; /* in microseconds */
  406. long tottimeps, sectors;
  407. if (dasd_profile_level != DASD_PROFILE_ON)
  408. return;
  409. sectors = req->nr_sectors;
  410. if (!cqr->buildclk || !cqr->startclk ||
  411. !cqr->stopclk || !cqr->endclk ||
  412. !sectors)
  413. return;
  414. strtime = ((cqr->startclk - cqr->buildclk) >> 12);
  415. irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
  416. endtime = ((cqr->endclk - cqr->stopclk) >> 12);
  417. tottime = ((cqr->endclk - cqr->buildclk) >> 12);
  418. tottimeps = tottime / sectors;
  419. if (!dasd_global_profile.dasd_io_reqs)
  420. memset(&dasd_global_profile, 0,
  421. sizeof (struct dasd_profile_info_t));
  422. dasd_global_profile.dasd_io_reqs++;
  423. dasd_global_profile.dasd_io_sects += sectors;
  424. if (!device->profile.dasd_io_reqs)
  425. memset(&device->profile, 0,
  426. sizeof (struct dasd_profile_info_t));
  427. device->profile.dasd_io_reqs++;
  428. device->profile.dasd_io_sects += sectors;
  429. dasd_profile_counter(sectors, dasd_io_secs, device);
  430. dasd_profile_counter(tottime, dasd_io_times, device);
  431. dasd_profile_counter(tottimeps, dasd_io_timps, device);
  432. dasd_profile_counter(strtime, dasd_io_time1, device);
  433. dasd_profile_counter(irqtime, dasd_io_time2, device);
  434. dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
  435. dasd_profile_counter(endtime, dasd_io_time3, device);
  436. }
  437. #else
  438. #define dasd_profile_start(device, cqr, req) do {} while (0)
  439. #define dasd_profile_end(device, cqr, req) do {} while (0)
  440. #endif /* CONFIG_DASD_PROFILE */
  441. /*
  442. * Allocate memory for a channel program with 'cplength' channel
  443. * command words and 'datasize' additional space. There are two
  444. * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
  445. * memory and 2) dasd_smalloc_request uses the static ccw memory
  446. * that gets allocated for each device.
  447. */
  448. struct dasd_ccw_req *
  449. dasd_kmalloc_request(char *magic, int cplength, int datasize,
  450. struct dasd_device * device)
  451. {
  452. struct dasd_ccw_req *cqr;
  453. /* Sanity checks */
  454. if ( magic == NULL || datasize > PAGE_SIZE ||
  455. (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
  456. BUG();
  457. cqr = kmalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
  458. if (cqr == NULL)
  459. return ERR_PTR(-ENOMEM);
  460. memset(cqr, 0, sizeof(struct dasd_ccw_req));
  461. cqr->cpaddr = NULL;
  462. if (cplength > 0) {
  463. cqr->cpaddr = kmalloc(cplength*sizeof(struct ccw1),
  464. GFP_ATOMIC | GFP_DMA);
  465. if (cqr->cpaddr == NULL) {
  466. kfree(cqr);
  467. return ERR_PTR(-ENOMEM);
  468. }
  469. memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
  470. }
  471. cqr->data = NULL;
  472. if (datasize > 0) {
  473. cqr->data = kmalloc(datasize, GFP_ATOMIC | GFP_DMA);
  474. if (cqr->data == NULL) {
  475. kfree(cqr->cpaddr);
  476. kfree(cqr);
  477. return ERR_PTR(-ENOMEM);
  478. }
  479. memset(cqr->data, 0, datasize);
  480. }
  481. strncpy((char *) &cqr->magic, magic, 4);
  482. ASCEBC((char *) &cqr->magic, 4);
  483. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  484. dasd_get_device(device);
  485. return cqr;
  486. }
  487. struct dasd_ccw_req *
  488. dasd_smalloc_request(char *magic, int cplength, int datasize,
  489. struct dasd_device * device)
  490. {
  491. unsigned long flags;
  492. struct dasd_ccw_req *cqr;
  493. char *data;
  494. int size;
  495. /* Sanity checks */
  496. if ( magic == NULL || datasize > PAGE_SIZE ||
  497. (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
  498. BUG();
  499. size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
  500. if (cplength > 0)
  501. size += cplength * sizeof(struct ccw1);
  502. if (datasize > 0)
  503. size += datasize;
  504. spin_lock_irqsave(&device->mem_lock, flags);
  505. cqr = (struct dasd_ccw_req *)
  506. dasd_alloc_chunk(&device->ccw_chunks, size);
  507. spin_unlock_irqrestore(&device->mem_lock, flags);
  508. if (cqr == NULL)
  509. return ERR_PTR(-ENOMEM);
  510. memset(cqr, 0, sizeof(struct dasd_ccw_req));
  511. data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
  512. cqr->cpaddr = NULL;
  513. if (cplength > 0) {
  514. cqr->cpaddr = (struct ccw1 *) data;
  515. data += cplength*sizeof(struct ccw1);
  516. memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
  517. }
  518. cqr->data = NULL;
  519. if (datasize > 0) {
  520. cqr->data = data;
  521. memset(cqr->data, 0, datasize);
  522. }
  523. strncpy((char *) &cqr->magic, magic, 4);
  524. ASCEBC((char *) &cqr->magic, 4);
  525. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  526. dasd_get_device(device);
  527. return cqr;
  528. }
  529. /*
  530. * Free memory of a channel program. This function needs to free all the
  531. * idal lists that might have been created by dasd_set_cda and the
  532. * struct dasd_ccw_req itself.
  533. */
  534. void
  535. dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
  536. {
  537. #ifdef CONFIG_64BIT
  538. struct ccw1 *ccw;
  539. /* Clear any idals used for the request. */
  540. ccw = cqr->cpaddr;
  541. do {
  542. clear_normalized_cda(ccw);
  543. } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
  544. #endif
  545. kfree(cqr->cpaddr);
  546. kfree(cqr->data);
  547. kfree(cqr);
  548. dasd_put_device(device);
  549. }
  550. void
  551. dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
  552. {
  553. unsigned long flags;
  554. spin_lock_irqsave(&device->mem_lock, flags);
  555. dasd_free_chunk(&device->ccw_chunks, cqr);
  556. spin_unlock_irqrestore(&device->mem_lock, flags);
  557. dasd_put_device(device);
  558. }
  559. /*
  560. * Check discipline magic in cqr.
  561. */
  562. static inline int
  563. dasd_check_cqr(struct dasd_ccw_req *cqr)
  564. {
  565. struct dasd_device *device;
  566. if (cqr == NULL)
  567. return -EINVAL;
  568. device = cqr->device;
  569. if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
  570. DEV_MESSAGE(KERN_WARNING, device,
  571. " dasd_ccw_req 0x%08x magic doesn't match"
  572. " discipline 0x%08x",
  573. cqr->magic,
  574. *(unsigned int *) device->discipline->name);
  575. return -EINVAL;
  576. }
  577. return 0;
  578. }
  579. /*
  580. * Terminate the current i/o and set the request to clear_pending.
  581. * Timer keeps device runnig.
  582. * ccw_device_clear can fail if the i/o subsystem
  583. * is in a bad mood.
  584. */
  585. int
  586. dasd_term_IO(struct dasd_ccw_req * cqr)
  587. {
  588. struct dasd_device *device;
  589. int retries, rc;
  590. /* Check the cqr */
  591. rc = dasd_check_cqr(cqr);
  592. if (rc)
  593. return rc;
  594. retries = 0;
  595. device = (struct dasd_device *) cqr->device;
  596. while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
  597. rc = ccw_device_clear(device->cdev, (long) cqr);
  598. switch (rc) {
  599. case 0: /* termination successful */
  600. if (cqr->retries > 0) {
  601. cqr->retries--;
  602. cqr->status = DASD_CQR_CLEAR;
  603. } else
  604. cqr->status = DASD_CQR_FAILED;
  605. cqr->stopclk = get_clock();
  606. DBF_DEV_EVENT(DBF_DEBUG, device,
  607. "terminate cqr %p successful",
  608. cqr);
  609. break;
  610. case -ENODEV:
  611. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  612. "device gone, retry");
  613. break;
  614. case -EIO:
  615. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  616. "I/O error, retry");
  617. break;
  618. case -EINVAL:
  619. case -EBUSY:
  620. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  621. "device busy, retry later");
  622. break;
  623. default:
  624. DEV_MESSAGE(KERN_ERR, device,
  625. "line %d unknown RC=%d, please "
  626. "report to linux390@de.ibm.com",
  627. __LINE__, rc);
  628. BUG();
  629. break;
  630. }
  631. retries++;
  632. }
  633. dasd_schedule_bh(device);
  634. return rc;
  635. }
  636. /*
  637. * Start the i/o. This start_IO can fail if the channel is really busy.
  638. * In that case set up a timer to start the request later.
  639. */
  640. int
  641. dasd_start_IO(struct dasd_ccw_req * cqr)
  642. {
  643. struct dasd_device *device;
  644. int rc;
  645. /* Check the cqr */
  646. rc = dasd_check_cqr(cqr);
  647. if (rc)
  648. return rc;
  649. device = (struct dasd_device *) cqr->device;
  650. if (cqr->retries < 0) {
  651. DEV_MESSAGE(KERN_DEBUG, device,
  652. "start_IO: request %p (%02x/%i) - no retry left.",
  653. cqr, cqr->status, cqr->retries);
  654. cqr->status = DASD_CQR_FAILED;
  655. return -EIO;
  656. }
  657. cqr->startclk = get_clock();
  658. cqr->starttime = jiffies;
  659. cqr->retries--;
  660. rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
  661. cqr->lpm, 0);
  662. switch (rc) {
  663. case 0:
  664. cqr->status = DASD_CQR_IN_IO;
  665. DBF_DEV_EVENT(DBF_DEBUG, device,
  666. "start_IO: request %p started successful",
  667. cqr);
  668. break;
  669. case -EBUSY:
  670. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  671. "start_IO: device busy, retry later");
  672. break;
  673. case -ETIMEDOUT:
  674. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  675. "start_IO: request timeout, retry later");
  676. break;
  677. case -EACCES:
  678. /* -EACCES indicates that the request used only a
  679. * subset of the available pathes and all these
  680. * pathes are gone.
  681. * Do a retry with all available pathes.
  682. */
  683. cqr->lpm = LPM_ANYPATH;
  684. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  685. "start_IO: selected pathes gone,"
  686. " retry on all pathes");
  687. break;
  688. case -ENODEV:
  689. case -EIO:
  690. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  691. "start_IO: device gone, retry");
  692. break;
  693. default:
  694. DEV_MESSAGE(KERN_ERR, device,
  695. "line %d unknown RC=%d, please report"
  696. " to linux390@de.ibm.com", __LINE__, rc);
  697. BUG();
  698. break;
  699. }
  700. return rc;
  701. }
  702. /*
  703. * Timeout function for dasd devices. This is used for different purposes
  704. * 1) missing interrupt handler for normal operation
  705. * 2) delayed start of request where start_IO failed with -EBUSY
  706. * 3) timeout for missing state change interrupts
  707. * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
  708. * DASD_CQR_QUEUED for 2) and 3).
  709. */
  710. static void
  711. dasd_timeout_device(unsigned long ptr)
  712. {
  713. unsigned long flags;
  714. struct dasd_device *device;
  715. device = (struct dasd_device *) ptr;
  716. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  717. /* re-activate request queue */
  718. device->stopped &= ~DASD_STOPPED_PENDING;
  719. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  720. dasd_schedule_bh(device);
  721. }
  722. /*
  723. * Setup timeout for a device in jiffies.
  724. */
  725. void
  726. dasd_set_timer(struct dasd_device *device, int expires)
  727. {
  728. if (expires == 0) {
  729. if (timer_pending(&device->timer))
  730. del_timer(&device->timer);
  731. return;
  732. }
  733. if (timer_pending(&device->timer)) {
  734. if (mod_timer(&device->timer, jiffies + expires))
  735. return;
  736. }
  737. device->timer.function = dasd_timeout_device;
  738. device->timer.data = (unsigned long) device;
  739. device->timer.expires = jiffies + expires;
  740. add_timer(&device->timer);
  741. }
  742. /*
  743. * Clear timeout for a device.
  744. */
  745. void
  746. dasd_clear_timer(struct dasd_device *device)
  747. {
  748. if (timer_pending(&device->timer))
  749. del_timer(&device->timer);
  750. }
  751. static void
  752. dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
  753. {
  754. struct dasd_ccw_req *cqr;
  755. struct dasd_device *device;
  756. cqr = (struct dasd_ccw_req *) intparm;
  757. if (cqr->status != DASD_CQR_IN_IO) {
  758. MESSAGE(KERN_DEBUG,
  759. "invalid status in handle_killed_request: "
  760. "bus_id %s, status %02x",
  761. cdev->dev.bus_id, cqr->status);
  762. return;
  763. }
  764. device = (struct dasd_device *) cqr->device;
  765. if (device == NULL ||
  766. device != dasd_device_from_cdev(cdev) ||
  767. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  768. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  769. cdev->dev.bus_id);
  770. return;
  771. }
  772. /* Schedule request to be retried. */
  773. cqr->status = DASD_CQR_QUEUED;
  774. dasd_clear_timer(device);
  775. dasd_schedule_bh(device);
  776. dasd_put_device(device);
  777. }
  778. static void
  779. dasd_handle_state_change_pending(struct dasd_device *device)
  780. {
  781. struct dasd_ccw_req *cqr;
  782. struct list_head *l, *n;
  783. device->stopped &= ~DASD_STOPPED_PENDING;
  784. /* restart all 'running' IO on queue */
  785. list_for_each_safe(l, n, &device->ccw_queue) {
  786. cqr = list_entry(l, struct dasd_ccw_req, list);
  787. if (cqr->status == DASD_CQR_IN_IO) {
  788. cqr->status = DASD_CQR_QUEUED;
  789. }
  790. }
  791. dasd_clear_timer(device);
  792. dasd_schedule_bh(device);
  793. }
  794. /*
  795. * Interrupt handler for "normal" ssch-io based dasd devices.
  796. */
  797. void
  798. dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
  799. struct irb *irb)
  800. {
  801. struct dasd_ccw_req *cqr, *next;
  802. struct dasd_device *device;
  803. unsigned long long now;
  804. int expires;
  805. dasd_era_t era;
  806. char mask;
  807. if (IS_ERR(irb)) {
  808. switch (PTR_ERR(irb)) {
  809. case -EIO:
  810. dasd_handle_killed_request(cdev, intparm);
  811. break;
  812. case -ETIMEDOUT:
  813. printk(KERN_WARNING"%s(%s): request timed out\n",
  814. __FUNCTION__, cdev->dev.bus_id);
  815. //FIXME - dasd uses own timeout interface...
  816. break;
  817. default:
  818. printk(KERN_WARNING"%s(%s): unknown error %ld\n",
  819. __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
  820. }
  821. return;
  822. }
  823. now = get_clock();
  824. DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
  825. cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
  826. (unsigned int) intparm);
  827. /* first of all check for state change pending interrupt */
  828. mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
  829. if ((irb->scsw.dstat & mask) == mask) {
  830. device = dasd_device_from_cdev(cdev);
  831. if (!IS_ERR(device)) {
  832. dasd_handle_state_change_pending(device);
  833. dasd_put_device(device);
  834. }
  835. return;
  836. }
  837. cqr = (struct dasd_ccw_req *) intparm;
  838. /* check for unsolicited interrupts */
  839. if (cqr == NULL) {
  840. MESSAGE(KERN_DEBUG,
  841. "unsolicited interrupt received: bus_id %s",
  842. cdev->dev.bus_id);
  843. return;
  844. }
  845. device = (struct dasd_device *) cqr->device;
  846. if (device == NULL ||
  847. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  848. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  849. cdev->dev.bus_id);
  850. return;
  851. }
  852. /* Check for clear pending */
  853. if (cqr->status == DASD_CQR_CLEAR &&
  854. irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
  855. cqr->status = DASD_CQR_QUEUED;
  856. dasd_clear_timer(device);
  857. dasd_schedule_bh(device);
  858. return;
  859. }
  860. /* check status - the request might have been killed by dyn detach */
  861. if (cqr->status != DASD_CQR_IN_IO) {
  862. MESSAGE(KERN_DEBUG,
  863. "invalid status: bus_id %s, status %02x",
  864. cdev->dev.bus_id, cqr->status);
  865. return;
  866. }
  867. DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
  868. ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
  869. /* Find out the appropriate era_action. */
  870. if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
  871. era = dasd_era_fatal;
  872. else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  873. irb->scsw.cstat == 0 &&
  874. !irb->esw.esw0.erw.cons)
  875. era = dasd_era_none;
  876. else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
  877. era = dasd_era_fatal; /* don't recover this request */
  878. else if (irb->esw.esw0.erw.cons)
  879. era = device->discipline->examine_error(cqr, irb);
  880. else
  881. era = dasd_era_recover;
  882. DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
  883. expires = 0;
  884. if (era == dasd_era_none) {
  885. cqr->status = DASD_CQR_DONE;
  886. cqr->stopclk = now;
  887. /* Start first request on queue if possible -> fast_io. */
  888. if (cqr->list.next != &device->ccw_queue) {
  889. next = list_entry(cqr->list.next,
  890. struct dasd_ccw_req, list);
  891. if ((next->status == DASD_CQR_QUEUED) &&
  892. (!device->stopped)) {
  893. if (device->discipline->start_IO(next) == 0)
  894. expires = next->expires;
  895. else
  896. DEV_MESSAGE(KERN_DEBUG, device, "%s",
  897. "Interrupt fastpath "
  898. "failed!");
  899. }
  900. }
  901. } else { /* error */
  902. memcpy(&cqr->irb, irb, sizeof (struct irb));
  903. #ifdef ERP_DEBUG
  904. /* dump sense data */
  905. dasd_log_sense(cqr, irb);
  906. #endif
  907. switch (era) {
  908. case dasd_era_fatal:
  909. cqr->status = DASD_CQR_FAILED;
  910. cqr->stopclk = now;
  911. break;
  912. case dasd_era_recover:
  913. cqr->status = DASD_CQR_ERROR;
  914. break;
  915. default:
  916. BUG();
  917. }
  918. }
  919. if (expires != 0)
  920. dasd_set_timer(device, expires);
  921. else
  922. dasd_clear_timer(device);
  923. dasd_schedule_bh(device);
  924. }
  925. /*
  926. * posts the buffer_cache about a finalized request
  927. */
  928. static inline void
  929. dasd_end_request(struct request *req, int uptodate)
  930. {
  931. if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
  932. BUG();
  933. add_disk_randomness(req->rq_disk);
  934. end_that_request_last(req, uptodate);
  935. }
  936. /*
  937. * Process finished error recovery ccw.
  938. */
  939. static inline void
  940. __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
  941. {
  942. dasd_erp_fn_t erp_fn;
  943. if (cqr->status == DASD_CQR_DONE)
  944. DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
  945. else
  946. DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
  947. erp_fn = device->discipline->erp_postaction(cqr);
  948. erp_fn(cqr);
  949. }
  950. /*
  951. * Process ccw request queue.
  952. */
  953. static inline void
  954. __dasd_process_ccw_queue(struct dasd_device * device,
  955. struct list_head *final_queue)
  956. {
  957. struct list_head *l, *n;
  958. struct dasd_ccw_req *cqr;
  959. dasd_erp_fn_t erp_fn;
  960. restart:
  961. /* Process request with final status. */
  962. list_for_each_safe(l, n, &device->ccw_queue) {
  963. cqr = list_entry(l, struct dasd_ccw_req, list);
  964. /* Stop list processing at the first non-final request. */
  965. if (cqr->status != DASD_CQR_DONE &&
  966. cqr->status != DASD_CQR_FAILED &&
  967. cqr->status != DASD_CQR_ERROR)
  968. break;
  969. /* Process requests with DASD_CQR_ERROR */
  970. if (cqr->status == DASD_CQR_ERROR) {
  971. if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
  972. cqr->status = DASD_CQR_FAILED;
  973. cqr->stopclk = get_clock();
  974. } else {
  975. if (cqr->irb.esw.esw0.erw.cons) {
  976. erp_fn = device->discipline->
  977. erp_action(cqr);
  978. erp_fn(cqr);
  979. } else
  980. dasd_default_erp_action(cqr);
  981. }
  982. goto restart;
  983. }
  984. /* Process finished ERP request. */
  985. if (cqr->refers) {
  986. __dasd_process_erp(device, cqr);
  987. goto restart;
  988. }
  989. /* Rechain finished requests to final queue */
  990. cqr->endclk = get_clock();
  991. list_move_tail(&cqr->list, final_queue);
  992. }
  993. }
  994. static void
  995. dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
  996. {
  997. struct request *req;
  998. struct dasd_device *device;
  999. int status;
  1000. req = (struct request *) data;
  1001. device = cqr->device;
  1002. dasd_profile_end(device, cqr, req);
  1003. status = cqr->device->discipline->free_cp(cqr,req);
  1004. spin_lock_irq(&device->request_queue_lock);
  1005. dasd_end_request(req, status);
  1006. spin_unlock_irq(&device->request_queue_lock);
  1007. }
  1008. /*
  1009. * Fetch requests from the block device queue.
  1010. */
  1011. static inline void
  1012. __dasd_process_blk_queue(struct dasd_device * device)
  1013. {
  1014. request_queue_t *queue;
  1015. struct request *req;
  1016. struct dasd_ccw_req *cqr;
  1017. int nr_queued;
  1018. queue = device->request_queue;
  1019. /* No queue ? Then there is nothing to do. */
  1020. if (queue == NULL)
  1021. return;
  1022. /*
  1023. * We requeue request from the block device queue to the ccw
  1024. * queue only in two states. In state DASD_STATE_READY the
  1025. * partition detection is done and we need to requeue requests
  1026. * for that. State DASD_STATE_ONLINE is normal block device
  1027. * operation.
  1028. */
  1029. if (device->state != DASD_STATE_READY &&
  1030. device->state != DASD_STATE_ONLINE)
  1031. return;
  1032. nr_queued = 0;
  1033. /* Now we try to fetch requests from the request queue */
  1034. list_for_each_entry(cqr, &device->ccw_queue, list)
  1035. if (cqr->status == DASD_CQR_QUEUED)
  1036. nr_queued++;
  1037. while (!blk_queue_plugged(queue) &&
  1038. elv_next_request(queue) &&
  1039. nr_queued < DASD_CHANQ_MAX_SIZE) {
  1040. req = elv_next_request(queue);
  1041. if (device->features & DASD_FEATURE_READONLY &&
  1042. rq_data_dir(req) == WRITE) {
  1043. DBF_DEV_EVENT(DBF_ERR, device,
  1044. "Rejecting write request %p",
  1045. req);
  1046. blkdev_dequeue_request(req);
  1047. dasd_end_request(req, 0);
  1048. continue;
  1049. }
  1050. if (device->stopped & DASD_STOPPED_DC_EIO) {
  1051. blkdev_dequeue_request(req);
  1052. dasd_end_request(req, 0);
  1053. continue;
  1054. }
  1055. cqr = device->discipline->build_cp(device, req);
  1056. if (IS_ERR(cqr)) {
  1057. if (PTR_ERR(cqr) == -ENOMEM)
  1058. break; /* terminate request queue loop */
  1059. DBF_DEV_EVENT(DBF_ERR, device,
  1060. "CCW creation failed (rc=%ld) "
  1061. "on request %p",
  1062. PTR_ERR(cqr), req);
  1063. blkdev_dequeue_request(req);
  1064. dasd_end_request(req, 0);
  1065. continue;
  1066. }
  1067. cqr->callback = dasd_end_request_cb;
  1068. cqr->callback_data = (void *) req;
  1069. cqr->status = DASD_CQR_QUEUED;
  1070. blkdev_dequeue_request(req);
  1071. list_add_tail(&cqr->list, &device->ccw_queue);
  1072. dasd_profile_start(device, cqr, req);
  1073. nr_queued++;
  1074. }
  1075. }
  1076. /*
  1077. * Take a look at the first request on the ccw queue and check
  1078. * if it reached its expire time. If so, terminate the IO.
  1079. */
  1080. static inline void
  1081. __dasd_check_expire(struct dasd_device * device)
  1082. {
  1083. struct dasd_ccw_req *cqr;
  1084. if (list_empty(&device->ccw_queue))
  1085. return;
  1086. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1087. if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
  1088. if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
  1089. if (device->discipline->term_IO(cqr) != 0)
  1090. /* Hmpf, try again in 1/10 sec */
  1091. dasd_set_timer(device, 10);
  1092. }
  1093. }
  1094. }
  1095. /*
  1096. * Take a look at the first request on the ccw queue and check
  1097. * if it needs to be started.
  1098. */
  1099. static inline void
  1100. __dasd_start_head(struct dasd_device * device)
  1101. {
  1102. struct dasd_ccw_req *cqr;
  1103. int rc;
  1104. if (list_empty(&device->ccw_queue))
  1105. return;
  1106. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1107. /* check FAILFAST */
  1108. if (device->stopped & ~DASD_STOPPED_PENDING &&
  1109. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags)) {
  1110. cqr->status = DASD_CQR_FAILED;
  1111. dasd_schedule_bh(device);
  1112. }
  1113. if ((cqr->status == DASD_CQR_QUEUED) &&
  1114. (!device->stopped)) {
  1115. /* try to start the first I/O that can be started */
  1116. rc = device->discipline->start_IO(cqr);
  1117. if (rc == 0)
  1118. dasd_set_timer(device, cqr->expires);
  1119. else if (rc == -EACCES) {
  1120. dasd_schedule_bh(device);
  1121. } else
  1122. /* Hmpf, try again in 1/2 sec */
  1123. dasd_set_timer(device, 50);
  1124. }
  1125. }
  1126. /*
  1127. * Remove requests from the ccw queue.
  1128. */
  1129. static void
  1130. dasd_flush_ccw_queue(struct dasd_device * device, int all)
  1131. {
  1132. struct list_head flush_queue;
  1133. struct list_head *l, *n;
  1134. struct dasd_ccw_req *cqr;
  1135. INIT_LIST_HEAD(&flush_queue);
  1136. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1137. list_for_each_safe(l, n, &device->ccw_queue) {
  1138. cqr = list_entry(l, struct dasd_ccw_req, list);
  1139. /* Flush all request or only block device requests? */
  1140. if (all == 0 && cqr->callback == dasd_end_request_cb)
  1141. continue;
  1142. if (cqr->status == DASD_CQR_IN_IO)
  1143. device->discipline->term_IO(cqr);
  1144. if (cqr->status != DASD_CQR_DONE ||
  1145. cqr->status != DASD_CQR_FAILED) {
  1146. cqr->status = DASD_CQR_FAILED;
  1147. cqr->stopclk = get_clock();
  1148. }
  1149. /* Process finished ERP request. */
  1150. if (cqr->refers) {
  1151. __dasd_process_erp(device, cqr);
  1152. continue;
  1153. }
  1154. /* Rechain request on device request queue */
  1155. cqr->endclk = get_clock();
  1156. list_move_tail(&cqr->list, &flush_queue);
  1157. }
  1158. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1159. /* Now call the callback function of flushed requests */
  1160. list_for_each_safe(l, n, &flush_queue) {
  1161. cqr = list_entry(l, struct dasd_ccw_req, list);
  1162. if (cqr->callback != NULL)
  1163. (cqr->callback)(cqr, cqr->callback_data);
  1164. }
  1165. }
  1166. /*
  1167. * Acquire the device lock and process queues for the device.
  1168. */
  1169. static void
  1170. dasd_tasklet(struct dasd_device * device)
  1171. {
  1172. struct list_head final_queue;
  1173. struct list_head *l, *n;
  1174. struct dasd_ccw_req *cqr;
  1175. atomic_set (&device->tasklet_scheduled, 0);
  1176. INIT_LIST_HEAD(&final_queue);
  1177. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1178. /* Check expire time of first request on the ccw queue. */
  1179. __dasd_check_expire(device);
  1180. /* Finish off requests on ccw queue */
  1181. __dasd_process_ccw_queue(device, &final_queue);
  1182. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1183. /* Now call the callback function of requests with final status */
  1184. list_for_each_safe(l, n, &final_queue) {
  1185. cqr = list_entry(l, struct dasd_ccw_req, list);
  1186. list_del(&cqr->list);
  1187. if (cqr->callback != NULL)
  1188. (cqr->callback)(cqr, cqr->callback_data);
  1189. }
  1190. spin_lock_irq(&device->request_queue_lock);
  1191. spin_lock(get_ccwdev_lock(device->cdev));
  1192. /* Get new request from the block device request queue */
  1193. __dasd_process_blk_queue(device);
  1194. /* Now check if the head of the ccw queue needs to be started. */
  1195. __dasd_start_head(device);
  1196. spin_unlock(get_ccwdev_lock(device->cdev));
  1197. spin_unlock_irq(&device->request_queue_lock);
  1198. dasd_put_device(device);
  1199. }
  1200. /*
  1201. * Schedules a call to dasd_tasklet over the device tasklet.
  1202. */
  1203. void
  1204. dasd_schedule_bh(struct dasd_device * device)
  1205. {
  1206. /* Protect against rescheduling. */
  1207. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  1208. return;
  1209. dasd_get_device(device);
  1210. tasklet_hi_schedule(&device->tasklet);
  1211. }
  1212. /*
  1213. * Queue a request to the head of the ccw_queue. Start the I/O if
  1214. * possible.
  1215. */
  1216. void
  1217. dasd_add_request_head(struct dasd_ccw_req *req)
  1218. {
  1219. struct dasd_device *device;
  1220. unsigned long flags;
  1221. device = req->device;
  1222. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1223. req->status = DASD_CQR_QUEUED;
  1224. req->device = device;
  1225. list_add(&req->list, &device->ccw_queue);
  1226. /* let the bh start the request to keep them in order */
  1227. dasd_schedule_bh(device);
  1228. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1229. }
  1230. /*
  1231. * Queue a request to the tail of the ccw_queue. Start the I/O if
  1232. * possible.
  1233. */
  1234. void
  1235. dasd_add_request_tail(struct dasd_ccw_req *req)
  1236. {
  1237. struct dasd_device *device;
  1238. unsigned long flags;
  1239. device = req->device;
  1240. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1241. req->status = DASD_CQR_QUEUED;
  1242. req->device = device;
  1243. list_add_tail(&req->list, &device->ccw_queue);
  1244. /* let the bh start the request to keep them in order */
  1245. dasd_schedule_bh(device);
  1246. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1247. }
  1248. /*
  1249. * Wakeup callback.
  1250. */
  1251. static void
  1252. dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  1253. {
  1254. wake_up((wait_queue_head_t *) data);
  1255. }
  1256. static inline int
  1257. _wait_for_wakeup(struct dasd_ccw_req *cqr)
  1258. {
  1259. struct dasd_device *device;
  1260. int rc;
  1261. device = cqr->device;
  1262. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1263. rc = cqr->status == DASD_CQR_DONE || cqr->status == DASD_CQR_FAILED;
  1264. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1265. return rc;
  1266. }
  1267. /*
  1268. * Attempts to start a special ccw queue and waits for its completion.
  1269. */
  1270. int
  1271. dasd_sleep_on(struct dasd_ccw_req * cqr)
  1272. {
  1273. wait_queue_head_t wait_q;
  1274. struct dasd_device *device;
  1275. int rc;
  1276. device = cqr->device;
  1277. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1278. init_waitqueue_head (&wait_q);
  1279. cqr->callback = dasd_wakeup_cb;
  1280. cqr->callback_data = (void *) &wait_q;
  1281. cqr->status = DASD_CQR_QUEUED;
  1282. list_add_tail(&cqr->list, &device->ccw_queue);
  1283. /* let the bh start the request to keep them in order */
  1284. dasd_schedule_bh(device);
  1285. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1286. wait_event(wait_q, _wait_for_wakeup(cqr));
  1287. /* Request status is either done or failed. */
  1288. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1289. return rc;
  1290. }
  1291. /*
  1292. * Attempts to start a special ccw queue and wait interruptible
  1293. * for its completion.
  1294. */
  1295. int
  1296. dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
  1297. {
  1298. wait_queue_head_t wait_q;
  1299. struct dasd_device *device;
  1300. int rc, finished;
  1301. device = cqr->device;
  1302. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1303. init_waitqueue_head (&wait_q);
  1304. cqr->callback = dasd_wakeup_cb;
  1305. cqr->callback_data = (void *) &wait_q;
  1306. cqr->status = DASD_CQR_QUEUED;
  1307. list_add_tail(&cqr->list, &device->ccw_queue);
  1308. /* let the bh start the request to keep them in order */
  1309. dasd_schedule_bh(device);
  1310. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1311. finished = 0;
  1312. while (!finished) {
  1313. rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
  1314. if (rc != -ERESTARTSYS) {
  1315. /* Request status is either done or failed. */
  1316. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1317. break;
  1318. }
  1319. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1320. if (cqr->status == DASD_CQR_IN_IO &&
  1321. device->discipline->term_IO(cqr) == 0) {
  1322. list_del(&cqr->list);
  1323. finished = 1;
  1324. }
  1325. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1326. }
  1327. return rc;
  1328. }
  1329. /*
  1330. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  1331. * for eckd devices) the currently running request has to be terminated
  1332. * and be put back to status queued, before the special request is added
  1333. * to the head of the queue. Then the special request is waited on normally.
  1334. */
  1335. static inline int
  1336. _dasd_term_running_cqr(struct dasd_device *device)
  1337. {
  1338. struct dasd_ccw_req *cqr;
  1339. int rc;
  1340. if (list_empty(&device->ccw_queue))
  1341. return 0;
  1342. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1343. rc = device->discipline->term_IO(cqr);
  1344. if (rc == 0) {
  1345. /* termination successful */
  1346. cqr->status = DASD_CQR_QUEUED;
  1347. cqr->startclk = cqr->stopclk = 0;
  1348. cqr->starttime = 0;
  1349. }
  1350. return rc;
  1351. }
  1352. int
  1353. dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
  1354. {
  1355. wait_queue_head_t wait_q;
  1356. struct dasd_device *device;
  1357. int rc;
  1358. device = cqr->device;
  1359. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1360. rc = _dasd_term_running_cqr(device);
  1361. if (rc) {
  1362. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1363. return rc;
  1364. }
  1365. init_waitqueue_head (&wait_q);
  1366. cqr->callback = dasd_wakeup_cb;
  1367. cqr->callback_data = (void *) &wait_q;
  1368. cqr->status = DASD_CQR_QUEUED;
  1369. list_add(&cqr->list, &device->ccw_queue);
  1370. /* let the bh start the request to keep them in order */
  1371. dasd_schedule_bh(device);
  1372. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1373. wait_event(wait_q, _wait_for_wakeup(cqr));
  1374. /* Request status is either done or failed. */
  1375. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1376. return rc;
  1377. }
  1378. /*
  1379. * Cancels a request that was started with dasd_sleep_on_req.
  1380. * This is useful to timeout requests. The request will be
  1381. * terminated if it is currently in i/o.
  1382. * Returns 1 if the request has been terminated.
  1383. */
  1384. int
  1385. dasd_cancel_req(struct dasd_ccw_req *cqr)
  1386. {
  1387. struct dasd_device *device = cqr->device;
  1388. unsigned long flags;
  1389. int rc;
  1390. rc = 0;
  1391. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1392. switch (cqr->status) {
  1393. case DASD_CQR_QUEUED:
  1394. /* request was not started - just set to failed */
  1395. cqr->status = DASD_CQR_FAILED;
  1396. break;
  1397. case DASD_CQR_IN_IO:
  1398. /* request in IO - terminate IO and release again */
  1399. if (device->discipline->term_IO(cqr) != 0)
  1400. /* what to do if unable to terminate ??????
  1401. e.g. not _IN_IO */
  1402. cqr->status = DASD_CQR_FAILED;
  1403. cqr->stopclk = get_clock();
  1404. rc = 1;
  1405. break;
  1406. case DASD_CQR_DONE:
  1407. case DASD_CQR_FAILED:
  1408. /* already finished - do nothing */
  1409. break;
  1410. default:
  1411. DEV_MESSAGE(KERN_ALERT, device,
  1412. "invalid status %02x in request",
  1413. cqr->status);
  1414. BUG();
  1415. }
  1416. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1417. dasd_schedule_bh(device);
  1418. return rc;
  1419. }
  1420. /*
  1421. * SECTION: Block device operations (request queue, partitions, open, release).
  1422. */
  1423. /*
  1424. * Dasd request queue function. Called from ll_rw_blk.c
  1425. */
  1426. static void
  1427. do_dasd_request(request_queue_t * queue)
  1428. {
  1429. struct dasd_device *device;
  1430. device = (struct dasd_device *) queue->queuedata;
  1431. spin_lock(get_ccwdev_lock(device->cdev));
  1432. /* Get new request from the block device request queue */
  1433. __dasd_process_blk_queue(device);
  1434. /* Now check if the head of the ccw queue needs to be started. */
  1435. __dasd_start_head(device);
  1436. spin_unlock(get_ccwdev_lock(device->cdev));
  1437. }
  1438. /*
  1439. * Allocate and initialize request queue and default I/O scheduler.
  1440. */
  1441. static int
  1442. dasd_alloc_queue(struct dasd_device * device)
  1443. {
  1444. int rc;
  1445. device->request_queue = blk_init_queue(do_dasd_request,
  1446. &device->request_queue_lock);
  1447. if (device->request_queue == NULL)
  1448. return -ENOMEM;
  1449. device->request_queue->queuedata = device;
  1450. elevator_exit(device->request_queue->elevator);
  1451. rc = elevator_init(device->request_queue, "deadline");
  1452. if (rc) {
  1453. blk_cleanup_queue(device->request_queue);
  1454. return rc;
  1455. }
  1456. return 0;
  1457. }
  1458. /*
  1459. * Allocate and initialize request queue.
  1460. */
  1461. static void
  1462. dasd_setup_queue(struct dasd_device * device)
  1463. {
  1464. int max;
  1465. blk_queue_hardsect_size(device->request_queue, device->bp_block);
  1466. max = device->discipline->max_blocks << device->s2b_shift;
  1467. blk_queue_max_sectors(device->request_queue, max);
  1468. blk_queue_max_phys_segments(device->request_queue, -1L);
  1469. blk_queue_max_hw_segments(device->request_queue, -1L);
  1470. blk_queue_max_segment_size(device->request_queue, -1L);
  1471. blk_queue_segment_boundary(device->request_queue, -1L);
  1472. blk_queue_ordered(device->request_queue, 1);
  1473. }
  1474. /*
  1475. * Deactivate and free request queue.
  1476. */
  1477. static void
  1478. dasd_free_queue(struct dasd_device * device)
  1479. {
  1480. if (device->request_queue) {
  1481. blk_cleanup_queue(device->request_queue);
  1482. device->request_queue = NULL;
  1483. }
  1484. }
  1485. /*
  1486. * Flush request on the request queue.
  1487. */
  1488. static void
  1489. dasd_flush_request_queue(struct dasd_device * device)
  1490. {
  1491. struct request *req;
  1492. if (!device->request_queue)
  1493. return;
  1494. spin_lock_irq(&device->request_queue_lock);
  1495. while (!list_empty(&device->request_queue->queue_head)) {
  1496. req = elv_next_request(device->request_queue);
  1497. if (req == NULL)
  1498. break;
  1499. dasd_end_request(req, 0);
  1500. blkdev_dequeue_request(req);
  1501. }
  1502. spin_unlock_irq(&device->request_queue_lock);
  1503. }
  1504. static int
  1505. dasd_open(struct inode *inp, struct file *filp)
  1506. {
  1507. struct gendisk *disk = inp->i_bdev->bd_disk;
  1508. struct dasd_device *device = disk->private_data;
  1509. int rc;
  1510. atomic_inc(&device->open_count);
  1511. if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1512. rc = -ENODEV;
  1513. goto unlock;
  1514. }
  1515. if (!try_module_get(device->discipline->owner)) {
  1516. rc = -EINVAL;
  1517. goto unlock;
  1518. }
  1519. if (dasd_probeonly) {
  1520. DEV_MESSAGE(KERN_INFO, device, "%s",
  1521. "No access to device due to probeonly mode");
  1522. rc = -EPERM;
  1523. goto out;
  1524. }
  1525. if (device->state < DASD_STATE_BASIC) {
  1526. DBF_DEV_EVENT(DBF_ERR, device, " %s",
  1527. " Cannot open unrecognized device");
  1528. rc = -ENODEV;
  1529. goto out;
  1530. }
  1531. return 0;
  1532. out:
  1533. module_put(device->discipline->owner);
  1534. unlock:
  1535. atomic_dec(&device->open_count);
  1536. return rc;
  1537. }
  1538. static int
  1539. dasd_release(struct inode *inp, struct file *filp)
  1540. {
  1541. struct gendisk *disk = inp->i_bdev->bd_disk;
  1542. struct dasd_device *device = disk->private_data;
  1543. atomic_dec(&device->open_count);
  1544. module_put(device->discipline->owner);
  1545. return 0;
  1546. }
  1547. /*
  1548. * Return disk geometry.
  1549. */
  1550. static int
  1551. dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1552. {
  1553. struct dasd_device *device;
  1554. device = bdev->bd_disk->private_data;
  1555. if (!device)
  1556. return -ENODEV;
  1557. if (!device->discipline ||
  1558. !device->discipline->fill_geometry)
  1559. return -EINVAL;
  1560. device->discipline->fill_geometry(device, geo);
  1561. geo->start = get_start_sect(bdev) >> device->s2b_shift;
  1562. return 0;
  1563. }
  1564. struct block_device_operations
  1565. dasd_device_operations = {
  1566. .owner = THIS_MODULE,
  1567. .open = dasd_open,
  1568. .release = dasd_release,
  1569. .ioctl = dasd_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. */