dasd.c 56 KB

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