dasd.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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. void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
  36. MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
  37. MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
  38. " Copyright 2000 IBM Corporation");
  39. MODULE_SUPPORTED_DEVICE("dasd");
  40. MODULE_LICENSE("GPL");
  41. /*
  42. * SECTION: prototypes for static functions of dasd.c
  43. */
  44. static int dasd_alloc_queue(struct dasd_device * device);
  45. static void dasd_setup_queue(struct dasd_device * device);
  46. static void dasd_free_queue(struct dasd_device * device);
  47. static void dasd_flush_request_queue(struct dasd_device *);
  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(struct work_struct *);
  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);
  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_WARNING);
  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. dasd_clear_timer(device);
  180. DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
  181. if (device->debug_area != NULL) {
  182. debug_unregister(device->debug_area);
  183. device->debug_area = NULL;
  184. }
  185. device->state = DASD_STATE_KNOWN;
  186. return 0;
  187. }
  188. /*
  189. * Do the initial analysis. The do_analysis function may return
  190. * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
  191. * until the discipline decides to continue the startup sequence
  192. * by calling the function dasd_change_state. The eckd disciplines
  193. * uses this to start a ccw that detects the format. The completion
  194. * interrupt for this detection ccw uses the kernel event daemon to
  195. * trigger the call to dasd_change_state. All this is done in the
  196. * discipline code, see dasd_eckd.c.
  197. * After the analysis ccw is done (do_analysis returned 0) the block
  198. * device is setup.
  199. * In case the analysis returns an error, the device setup is stopped
  200. * (a fake disk was already added to allow formatting).
  201. */
  202. static int
  203. dasd_state_basic_to_ready(struct dasd_device * device)
  204. {
  205. int rc;
  206. rc = 0;
  207. if (device->discipline->do_analysis != NULL)
  208. rc = device->discipline->do_analysis(device);
  209. if (rc) {
  210. if (rc != -EAGAIN)
  211. device->state = DASD_STATE_UNFMT;
  212. return rc;
  213. }
  214. /* make disk known with correct capacity */
  215. dasd_setup_queue(device);
  216. set_capacity(device->gdp, device->blocks << device->s2b_shift);
  217. device->state = DASD_STATE_READY;
  218. rc = dasd_scan_partitions(device);
  219. if (rc)
  220. device->state = DASD_STATE_BASIC;
  221. return rc;
  222. }
  223. /*
  224. * Remove device from block device layer. Destroy dirty buffers.
  225. * Forget format information. Check if the target level is basic
  226. * and if it is create fake disk for formatting.
  227. */
  228. static int
  229. dasd_state_ready_to_basic(struct dasd_device * device)
  230. {
  231. int rc;
  232. rc = dasd_flush_ccw_queue(device, 0);
  233. if (rc)
  234. return rc;
  235. dasd_destroy_partitions(device);
  236. dasd_flush_request_queue(device);
  237. device->blocks = 0;
  238. device->bp_block = 0;
  239. device->s2b_shift = 0;
  240. device->state = DASD_STATE_BASIC;
  241. return 0;
  242. }
  243. /*
  244. * Back to basic.
  245. */
  246. static int
  247. dasd_state_unfmt_to_basic(struct dasd_device * device)
  248. {
  249. device->state = DASD_STATE_BASIC;
  250. return 0;
  251. }
  252. /*
  253. * Make the device online and schedule the bottom half to start
  254. * the requeueing of requests from the linux request queue to the
  255. * ccw queue.
  256. */
  257. static int
  258. dasd_state_ready_to_online(struct dasd_device * device)
  259. {
  260. device->state = DASD_STATE_ONLINE;
  261. dasd_schedule_bh(device);
  262. return 0;
  263. }
  264. /*
  265. * Stop the requeueing of requests again.
  266. */
  267. static int
  268. dasd_state_online_to_ready(struct dasd_device * device)
  269. {
  270. device->state = DASD_STATE_READY;
  271. return 0;
  272. }
  273. /*
  274. * Device startup state changes.
  275. */
  276. static int
  277. dasd_increase_state(struct dasd_device *device)
  278. {
  279. int rc;
  280. rc = 0;
  281. if (device->state == DASD_STATE_NEW &&
  282. device->target >= DASD_STATE_KNOWN)
  283. rc = dasd_state_new_to_known(device);
  284. if (!rc &&
  285. device->state == DASD_STATE_KNOWN &&
  286. device->target >= DASD_STATE_BASIC)
  287. rc = dasd_state_known_to_basic(device);
  288. if (!rc &&
  289. device->state == DASD_STATE_BASIC &&
  290. device->target >= DASD_STATE_READY)
  291. rc = dasd_state_basic_to_ready(device);
  292. if (!rc &&
  293. device->state == DASD_STATE_UNFMT &&
  294. device->target > DASD_STATE_UNFMT)
  295. rc = -EPERM;
  296. if (!rc &&
  297. device->state == DASD_STATE_READY &&
  298. device->target >= DASD_STATE_ONLINE)
  299. rc = dasd_state_ready_to_online(device);
  300. return rc;
  301. }
  302. /*
  303. * Device shutdown state changes.
  304. */
  305. static int
  306. dasd_decrease_state(struct dasd_device *device)
  307. {
  308. int rc;
  309. rc = 0;
  310. if (device->state == DASD_STATE_ONLINE &&
  311. device->target <= DASD_STATE_READY)
  312. rc = dasd_state_online_to_ready(device);
  313. if (!rc &&
  314. device->state == DASD_STATE_READY &&
  315. device->target <= DASD_STATE_BASIC)
  316. rc = dasd_state_ready_to_basic(device);
  317. if (!rc &&
  318. device->state == DASD_STATE_UNFMT &&
  319. device->target <= DASD_STATE_BASIC)
  320. rc = dasd_state_unfmt_to_basic(device);
  321. if (!rc &&
  322. device->state == DASD_STATE_BASIC &&
  323. device->target <= DASD_STATE_KNOWN)
  324. rc = dasd_state_basic_to_known(device);
  325. if (!rc &&
  326. device->state == DASD_STATE_KNOWN &&
  327. device->target <= DASD_STATE_NEW)
  328. rc = dasd_state_known_to_new(device);
  329. return rc;
  330. }
  331. /*
  332. * This is the main startup/shutdown routine.
  333. */
  334. static void
  335. dasd_change_state(struct dasd_device *device)
  336. {
  337. int rc;
  338. if (device->state == device->target)
  339. /* Already where we want to go today... */
  340. return;
  341. if (device->state < device->target)
  342. rc = dasd_increase_state(device);
  343. else
  344. rc = dasd_decrease_state(device);
  345. if (rc && rc != -EAGAIN)
  346. device->target = device->state;
  347. if (device->state == device->target)
  348. wake_up(&dasd_init_waitq);
  349. }
  350. /*
  351. * Kick starter for devices that did not complete the startup/shutdown
  352. * procedure or were sleeping because of a pending state.
  353. * dasd_kick_device will schedule a call do do_kick_device to the kernel
  354. * event daemon.
  355. */
  356. static void
  357. do_kick_device(struct work_struct *work)
  358. {
  359. struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
  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 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 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_locked(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_locked(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 (irb->esw.esw0.erw.cons)
  916. era = device->discipline->examine_error(cqr, irb);
  917. else
  918. era = dasd_era_recover;
  919. DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
  920. expires = 0;
  921. if (era == dasd_era_none) {
  922. cqr->status = DASD_CQR_DONE;
  923. cqr->stopclk = now;
  924. /* Start first request on queue if possible -> fast_io. */
  925. if (cqr->list.next != &device->ccw_queue) {
  926. next = list_entry(cqr->list.next,
  927. struct dasd_ccw_req, list);
  928. if ((next->status == DASD_CQR_QUEUED) &&
  929. (!device->stopped)) {
  930. if (device->discipline->start_IO(next) == 0)
  931. expires = next->expires;
  932. else
  933. DEV_MESSAGE(KERN_DEBUG, device, "%s",
  934. "Interrupt fastpath "
  935. "failed!");
  936. }
  937. }
  938. } else { /* error */
  939. memcpy(&cqr->irb, irb, sizeof (struct irb));
  940. if (device->features & DASD_FEATURE_ERPLOG) {
  941. /* dump sense data */
  942. dasd_log_sense(cqr, irb);
  943. }
  944. switch (era) {
  945. case dasd_era_fatal:
  946. cqr->status = DASD_CQR_FAILED;
  947. cqr->stopclk = now;
  948. break;
  949. case dasd_era_recover:
  950. cqr->status = DASD_CQR_ERROR;
  951. break;
  952. default:
  953. BUG();
  954. }
  955. }
  956. if (expires != 0)
  957. dasd_set_timer(device, expires);
  958. else
  959. dasd_clear_timer(device);
  960. dasd_schedule_bh(device);
  961. }
  962. /*
  963. * posts the buffer_cache about a finalized request
  964. */
  965. static inline void
  966. dasd_end_request(struct request *req, int uptodate)
  967. {
  968. if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
  969. BUG();
  970. add_disk_randomness(req->rq_disk);
  971. end_that_request_last(req, uptodate);
  972. }
  973. /*
  974. * Process finished error recovery ccw.
  975. */
  976. static inline void
  977. __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
  978. {
  979. dasd_erp_fn_t erp_fn;
  980. if (cqr->status == DASD_CQR_DONE)
  981. DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
  982. else
  983. DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
  984. erp_fn = device->discipline->erp_postaction(cqr);
  985. erp_fn(cqr);
  986. }
  987. /*
  988. * Process ccw request queue.
  989. */
  990. static void
  991. __dasd_process_ccw_queue(struct dasd_device * device,
  992. struct list_head *final_queue)
  993. {
  994. struct list_head *l, *n;
  995. struct dasd_ccw_req *cqr;
  996. dasd_erp_fn_t erp_fn;
  997. restart:
  998. /* Process request with final status. */
  999. list_for_each_safe(l, n, &device->ccw_queue) {
  1000. cqr = list_entry(l, struct dasd_ccw_req, list);
  1001. /* Stop list processing at the first non-final request. */
  1002. if (cqr->status != DASD_CQR_DONE &&
  1003. cqr->status != DASD_CQR_FAILED &&
  1004. cqr->status != DASD_CQR_ERROR)
  1005. break;
  1006. /* Process requests with DASD_CQR_ERROR */
  1007. if (cqr->status == DASD_CQR_ERROR) {
  1008. if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
  1009. cqr->status = DASD_CQR_FAILED;
  1010. cqr->stopclk = get_clock();
  1011. } else {
  1012. if (cqr->irb.esw.esw0.erw.cons &&
  1013. test_bit(DASD_CQR_FLAGS_USE_ERP,
  1014. &cqr->flags)) {
  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 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. if (PTR_ERR(cqr) == -EAGAIN) {
  1109. /*
  1110. * The current request cannot be build right
  1111. * now, we have to try later. If this request
  1112. * is the head-of-queue we stop the device
  1113. * for 1/2 second.
  1114. */
  1115. if (!list_empty(&device->ccw_queue))
  1116. break;
  1117. device->stopped |= DASD_STOPPED_PENDING;
  1118. dasd_set_timer(device, HZ/2);
  1119. break;
  1120. }
  1121. DBF_DEV_EVENT(DBF_ERR, device,
  1122. "CCW creation failed (rc=%ld) "
  1123. "on request %p",
  1124. PTR_ERR(cqr), req);
  1125. blkdev_dequeue_request(req);
  1126. dasd_end_request(req, 0);
  1127. continue;
  1128. }
  1129. cqr->callback = dasd_end_request_cb;
  1130. cqr->callback_data = (void *) req;
  1131. cqr->status = DASD_CQR_QUEUED;
  1132. blkdev_dequeue_request(req);
  1133. list_add_tail(&cqr->list, &device->ccw_queue);
  1134. dasd_profile_start(device, cqr, req);
  1135. nr_queued++;
  1136. }
  1137. }
  1138. /*
  1139. * Take a look at the first request on the ccw queue and check
  1140. * if it reached its expire time. If so, terminate the IO.
  1141. */
  1142. static void
  1143. __dasd_check_expire(struct dasd_device * device)
  1144. {
  1145. struct dasd_ccw_req *cqr;
  1146. if (list_empty(&device->ccw_queue))
  1147. return;
  1148. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1149. if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
  1150. (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
  1151. if (device->discipline->term_IO(cqr) != 0) {
  1152. /* Hmpf, try again in 5 sec */
  1153. dasd_set_timer(device, 5*HZ);
  1154. DEV_MESSAGE(KERN_ERR, device,
  1155. "internal error - timeout (%is) expired "
  1156. "for cqr %p, termination failed, "
  1157. "retrying in 5s",
  1158. (cqr->expires/HZ), cqr);
  1159. } else {
  1160. DEV_MESSAGE(KERN_ERR, device,
  1161. "internal error - timeout (%is) expired "
  1162. "for cqr %p (%i retries left)",
  1163. (cqr->expires/HZ), cqr, cqr->retries);
  1164. }
  1165. }
  1166. }
  1167. /*
  1168. * Take a look at the first request on the ccw queue and check
  1169. * if it needs to be started.
  1170. */
  1171. static void
  1172. __dasd_start_head(struct dasd_device * device)
  1173. {
  1174. struct dasd_ccw_req *cqr;
  1175. int rc;
  1176. if (list_empty(&device->ccw_queue))
  1177. return;
  1178. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1179. if (cqr->status != DASD_CQR_QUEUED)
  1180. return;
  1181. /* Non-temporary stop condition will trigger fail fast */
  1182. if (device->stopped & ~DASD_STOPPED_PENDING &&
  1183. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1184. (!dasd_eer_enabled(device))) {
  1185. cqr->status = DASD_CQR_FAILED;
  1186. dasd_schedule_bh(device);
  1187. return;
  1188. }
  1189. /* Don't try to start requests if device is stopped */
  1190. if (device->stopped)
  1191. return;
  1192. rc = device->discipline->start_IO(cqr);
  1193. if (rc == 0)
  1194. dasd_set_timer(device, cqr->expires);
  1195. else if (rc == -EACCES) {
  1196. dasd_schedule_bh(device);
  1197. } else
  1198. /* Hmpf, try again in 1/2 sec */
  1199. dasd_set_timer(device, 50);
  1200. }
  1201. static inline int
  1202. _wait_for_clear(struct dasd_ccw_req *cqr)
  1203. {
  1204. return (cqr->status == DASD_CQR_QUEUED);
  1205. }
  1206. /*
  1207. * Remove all requests from the ccw queue (all = '1') or only block device
  1208. * requests in case all = '0'.
  1209. * Take care of the erp-chain (chained via cqr->refers) and remove either
  1210. * the whole erp-chain or none of the erp-requests.
  1211. * If a request is currently running, term_IO is called and the request
  1212. * is re-queued. Prior to removing the terminated request we need to wait
  1213. * for the clear-interrupt.
  1214. * In case termination is not possible we stop processing and just finishing
  1215. * the already moved requests.
  1216. */
  1217. static int
  1218. dasd_flush_ccw_queue(struct dasd_device * device, int all)
  1219. {
  1220. struct dasd_ccw_req *cqr, *orig, *n;
  1221. int rc, i;
  1222. struct list_head flush_queue;
  1223. INIT_LIST_HEAD(&flush_queue);
  1224. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1225. rc = 0;
  1226. restart:
  1227. list_for_each_entry_safe(cqr, n, &device->ccw_queue, list) {
  1228. /* get original request of erp request-chain */
  1229. for (orig = cqr; orig->refers != NULL; orig = orig->refers);
  1230. /* Flush all request or only block device requests? */
  1231. if (all == 0 && cqr->callback != dasd_end_request_cb &&
  1232. orig->callback != dasd_end_request_cb) {
  1233. continue;
  1234. }
  1235. /* Check status and move request to flush_queue */
  1236. switch (cqr->status) {
  1237. case DASD_CQR_IN_IO:
  1238. rc = device->discipline->term_IO(cqr);
  1239. if (rc) {
  1240. /* unable to terminate requeust */
  1241. DEV_MESSAGE(KERN_ERR, device,
  1242. "dasd flush ccw_queue is unable "
  1243. " to terminate request %p",
  1244. cqr);
  1245. /* stop flush processing */
  1246. goto finished;
  1247. }
  1248. break;
  1249. case DASD_CQR_QUEUED:
  1250. case DASD_CQR_ERROR:
  1251. /* set request to FAILED */
  1252. cqr->stopclk = get_clock();
  1253. cqr->status = DASD_CQR_FAILED;
  1254. break;
  1255. default: /* do not touch the others */
  1256. break;
  1257. }
  1258. /* Rechain request (including erp chain) */
  1259. for (i = 0; cqr != NULL; cqr = cqr->refers, i++) {
  1260. cqr->endclk = get_clock();
  1261. list_move_tail(&cqr->list, &flush_queue);
  1262. }
  1263. if (i > 1)
  1264. /* moved more than one request - need to restart */
  1265. goto restart;
  1266. }
  1267. finished:
  1268. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1269. /* Now call the callback function of flushed requests */
  1270. restart_cb:
  1271. list_for_each_entry_safe(cqr, n, &flush_queue, list) {
  1272. if (cqr->status == DASD_CQR_CLEAR) {
  1273. /* wait for clear interrupt! */
  1274. wait_event(dasd_flush_wq, _wait_for_clear(cqr));
  1275. cqr->status = DASD_CQR_FAILED;
  1276. }
  1277. /* Process finished ERP request. */
  1278. if (cqr->refers) {
  1279. __dasd_process_erp(device, cqr);
  1280. /* restart list_for_xx loop since dasd_process_erp
  1281. * might remove multiple elements */
  1282. goto restart_cb;
  1283. }
  1284. /* call the callback function */
  1285. cqr->endclk = get_clock();
  1286. if (cqr->callback != NULL)
  1287. (cqr->callback)(cqr, cqr->callback_data);
  1288. }
  1289. return rc;
  1290. }
  1291. /*
  1292. * Acquire the device lock and process queues for the device.
  1293. */
  1294. static void
  1295. dasd_tasklet(struct dasd_device * device)
  1296. {
  1297. struct list_head final_queue;
  1298. struct list_head *l, *n;
  1299. struct dasd_ccw_req *cqr;
  1300. atomic_set (&device->tasklet_scheduled, 0);
  1301. INIT_LIST_HEAD(&final_queue);
  1302. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1303. /* Check expire time of first request on the ccw queue. */
  1304. __dasd_check_expire(device);
  1305. /* Finish off requests on ccw queue */
  1306. __dasd_process_ccw_queue(device, &final_queue);
  1307. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1308. /* Now call the callback function of requests with final status */
  1309. list_for_each_safe(l, n, &final_queue) {
  1310. cqr = list_entry(l, struct dasd_ccw_req, list);
  1311. list_del_init(&cqr->list);
  1312. if (cqr->callback != NULL)
  1313. (cqr->callback)(cqr, cqr->callback_data);
  1314. }
  1315. spin_lock_irq(&device->request_queue_lock);
  1316. spin_lock(get_ccwdev_lock(device->cdev));
  1317. /* Get new request from the block device request queue */
  1318. __dasd_process_blk_queue(device);
  1319. /* Now check if the head of the ccw queue needs to be started. */
  1320. __dasd_start_head(device);
  1321. spin_unlock(get_ccwdev_lock(device->cdev));
  1322. spin_unlock_irq(&device->request_queue_lock);
  1323. dasd_put_device(device);
  1324. }
  1325. /*
  1326. * Schedules a call to dasd_tasklet over the device tasklet.
  1327. */
  1328. void
  1329. dasd_schedule_bh(struct dasd_device * device)
  1330. {
  1331. /* Protect against rescheduling. */
  1332. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  1333. return;
  1334. dasd_get_device(device);
  1335. tasklet_hi_schedule(&device->tasklet);
  1336. }
  1337. /*
  1338. * Queue a request to the head of the ccw_queue. Start the I/O if
  1339. * possible.
  1340. */
  1341. void
  1342. dasd_add_request_head(struct dasd_ccw_req *req)
  1343. {
  1344. struct dasd_device *device;
  1345. unsigned long flags;
  1346. device = req->device;
  1347. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1348. req->status = DASD_CQR_QUEUED;
  1349. req->device = device;
  1350. list_add(&req->list, &device->ccw_queue);
  1351. /* let the bh start the request to keep them in order */
  1352. dasd_schedule_bh(device);
  1353. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1354. }
  1355. /*
  1356. * Queue a request to the tail of the ccw_queue. Start the I/O if
  1357. * possible.
  1358. */
  1359. void
  1360. dasd_add_request_tail(struct dasd_ccw_req *req)
  1361. {
  1362. struct dasd_device *device;
  1363. unsigned long flags;
  1364. device = req->device;
  1365. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1366. req->status = DASD_CQR_QUEUED;
  1367. req->device = device;
  1368. list_add_tail(&req->list, &device->ccw_queue);
  1369. /* let the bh start the request to keep them in order */
  1370. dasd_schedule_bh(device);
  1371. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1372. }
  1373. /*
  1374. * Wakeup callback.
  1375. */
  1376. static void
  1377. dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  1378. {
  1379. wake_up((wait_queue_head_t *) data);
  1380. }
  1381. static inline int
  1382. _wait_for_wakeup(struct dasd_ccw_req *cqr)
  1383. {
  1384. struct dasd_device *device;
  1385. int rc;
  1386. device = cqr->device;
  1387. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1388. rc = ((cqr->status == DASD_CQR_DONE ||
  1389. cqr->status == DASD_CQR_FAILED) &&
  1390. list_empty(&cqr->list));
  1391. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1392. return rc;
  1393. }
  1394. /*
  1395. * Attempts to start a special ccw queue and waits for its completion.
  1396. */
  1397. int
  1398. dasd_sleep_on(struct dasd_ccw_req * cqr)
  1399. {
  1400. wait_queue_head_t wait_q;
  1401. struct dasd_device *device;
  1402. int rc;
  1403. device = cqr->device;
  1404. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1405. init_waitqueue_head (&wait_q);
  1406. cqr->callback = dasd_wakeup_cb;
  1407. cqr->callback_data = (void *) &wait_q;
  1408. cqr->status = DASD_CQR_QUEUED;
  1409. list_add_tail(&cqr->list, &device->ccw_queue);
  1410. /* let the bh start the request to keep them in order */
  1411. dasd_schedule_bh(device);
  1412. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1413. wait_event(wait_q, _wait_for_wakeup(cqr));
  1414. /* Request status is either done or failed. */
  1415. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1416. return rc;
  1417. }
  1418. /*
  1419. * Attempts to start a special ccw queue and wait interruptible
  1420. * for its completion.
  1421. */
  1422. int
  1423. dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
  1424. {
  1425. wait_queue_head_t wait_q;
  1426. struct dasd_device *device;
  1427. int rc, finished;
  1428. device = cqr->device;
  1429. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1430. init_waitqueue_head (&wait_q);
  1431. cqr->callback = dasd_wakeup_cb;
  1432. cqr->callback_data = (void *) &wait_q;
  1433. cqr->status = DASD_CQR_QUEUED;
  1434. list_add_tail(&cqr->list, &device->ccw_queue);
  1435. /* let the bh start the request to keep them in order */
  1436. dasd_schedule_bh(device);
  1437. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1438. finished = 0;
  1439. while (!finished) {
  1440. rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
  1441. if (rc != -ERESTARTSYS) {
  1442. /* Request is final (done or failed) */
  1443. rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  1444. break;
  1445. }
  1446. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1447. switch (cqr->status) {
  1448. case DASD_CQR_IN_IO:
  1449. /* terminate runnig cqr */
  1450. if (device->discipline->term_IO) {
  1451. cqr->retries = -1;
  1452. device->discipline->term_IO(cqr);
  1453. /* wait (non-interruptible) for final status
  1454. * because signal ist still pending */
  1455. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1456. wait_event(wait_q, _wait_for_wakeup(cqr));
  1457. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1458. rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  1459. finished = 1;
  1460. }
  1461. break;
  1462. case DASD_CQR_QUEUED:
  1463. /* request */
  1464. list_del_init(&cqr->list);
  1465. rc = -EIO;
  1466. finished = 1;
  1467. break;
  1468. default:
  1469. /* cqr with 'non-interruptable' status - just wait */
  1470. break;
  1471. }
  1472. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1473. }
  1474. return rc;
  1475. }
  1476. /*
  1477. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  1478. * for eckd devices) the currently running request has to be terminated
  1479. * and be put back to status queued, before the special request is added
  1480. * to the head of the queue. Then the special request is waited on normally.
  1481. */
  1482. static inline int
  1483. _dasd_term_running_cqr(struct dasd_device *device)
  1484. {
  1485. struct dasd_ccw_req *cqr;
  1486. if (list_empty(&device->ccw_queue))
  1487. return 0;
  1488. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1489. return device->discipline->term_IO(cqr);
  1490. }
  1491. int
  1492. dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
  1493. {
  1494. wait_queue_head_t wait_q;
  1495. struct dasd_device *device;
  1496. int rc;
  1497. device = cqr->device;
  1498. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1499. rc = _dasd_term_running_cqr(device);
  1500. if (rc) {
  1501. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1502. return rc;
  1503. }
  1504. init_waitqueue_head (&wait_q);
  1505. cqr->callback = dasd_wakeup_cb;
  1506. cqr->callback_data = (void *) &wait_q;
  1507. cqr->status = DASD_CQR_QUEUED;
  1508. list_add(&cqr->list, &device->ccw_queue);
  1509. /* let the bh start the request to keep them in order */
  1510. dasd_schedule_bh(device);
  1511. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1512. wait_event(wait_q, _wait_for_wakeup(cqr));
  1513. /* Request status is either done or failed. */
  1514. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1515. return rc;
  1516. }
  1517. /*
  1518. * Cancels a request that was started with dasd_sleep_on_req.
  1519. * This is useful to timeout requests. The request will be
  1520. * terminated if it is currently in i/o.
  1521. * Returns 1 if the request has been terminated.
  1522. */
  1523. int
  1524. dasd_cancel_req(struct dasd_ccw_req *cqr)
  1525. {
  1526. struct dasd_device *device = cqr->device;
  1527. unsigned long flags;
  1528. int rc;
  1529. rc = 0;
  1530. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1531. switch (cqr->status) {
  1532. case DASD_CQR_QUEUED:
  1533. /* request was not started - just set to failed */
  1534. cqr->status = DASD_CQR_FAILED;
  1535. break;
  1536. case DASD_CQR_IN_IO:
  1537. /* request in IO - terminate IO and release again */
  1538. if (device->discipline->term_IO(cqr) != 0)
  1539. /* what to do if unable to terminate ??????
  1540. e.g. not _IN_IO */
  1541. cqr->status = DASD_CQR_FAILED;
  1542. cqr->stopclk = get_clock();
  1543. rc = 1;
  1544. break;
  1545. case DASD_CQR_DONE:
  1546. case DASD_CQR_FAILED:
  1547. /* already finished - do nothing */
  1548. break;
  1549. default:
  1550. DEV_MESSAGE(KERN_ALERT, device,
  1551. "invalid status %02x in request",
  1552. cqr->status);
  1553. BUG();
  1554. }
  1555. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1556. dasd_schedule_bh(device);
  1557. return rc;
  1558. }
  1559. /*
  1560. * SECTION: Block device operations (request queue, partitions, open, release).
  1561. */
  1562. /*
  1563. * Dasd request queue function. Called from ll_rw_blk.c
  1564. */
  1565. static void
  1566. do_dasd_request(request_queue_t * queue)
  1567. {
  1568. struct dasd_device *device;
  1569. device = (struct dasd_device *) queue->queuedata;
  1570. spin_lock(get_ccwdev_lock(device->cdev));
  1571. /* Get new request from the block device request queue */
  1572. __dasd_process_blk_queue(device);
  1573. /* Now check if the head of the ccw queue needs to be started. */
  1574. __dasd_start_head(device);
  1575. spin_unlock(get_ccwdev_lock(device->cdev));
  1576. }
  1577. /*
  1578. * Allocate and initialize request queue and default I/O scheduler.
  1579. */
  1580. static int
  1581. dasd_alloc_queue(struct dasd_device * device)
  1582. {
  1583. int rc;
  1584. device->request_queue = blk_init_queue(do_dasd_request,
  1585. &device->request_queue_lock);
  1586. if (device->request_queue == NULL)
  1587. return -ENOMEM;
  1588. device->request_queue->queuedata = device;
  1589. elevator_exit(device->request_queue->elevator);
  1590. rc = elevator_init(device->request_queue, "deadline");
  1591. if (rc) {
  1592. blk_cleanup_queue(device->request_queue);
  1593. return rc;
  1594. }
  1595. return 0;
  1596. }
  1597. /*
  1598. * Allocate and initialize request queue.
  1599. */
  1600. static void
  1601. dasd_setup_queue(struct dasd_device * device)
  1602. {
  1603. int max;
  1604. blk_queue_hardsect_size(device->request_queue, device->bp_block);
  1605. max = device->discipline->max_blocks << device->s2b_shift;
  1606. blk_queue_max_sectors(device->request_queue, max);
  1607. blk_queue_max_phys_segments(device->request_queue, -1L);
  1608. blk_queue_max_hw_segments(device->request_queue, -1L);
  1609. blk_queue_max_segment_size(device->request_queue, -1L);
  1610. blk_queue_segment_boundary(device->request_queue, -1L);
  1611. blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
  1612. }
  1613. /*
  1614. * Deactivate and free request queue.
  1615. */
  1616. static void
  1617. dasd_free_queue(struct dasd_device * device)
  1618. {
  1619. if (device->request_queue) {
  1620. blk_cleanup_queue(device->request_queue);
  1621. device->request_queue = NULL;
  1622. }
  1623. }
  1624. /*
  1625. * Flush request on the request queue.
  1626. */
  1627. static void
  1628. dasd_flush_request_queue(struct dasd_device * device)
  1629. {
  1630. struct request *req;
  1631. if (!device->request_queue)
  1632. return;
  1633. spin_lock_irq(&device->request_queue_lock);
  1634. while ((req = elv_next_request(device->request_queue))) {
  1635. blkdev_dequeue_request(req);
  1636. dasd_end_request(req, 0);
  1637. }
  1638. spin_unlock_irq(&device->request_queue_lock);
  1639. }
  1640. static int
  1641. dasd_open(struct inode *inp, struct file *filp)
  1642. {
  1643. struct gendisk *disk = inp->i_bdev->bd_disk;
  1644. struct dasd_device *device = disk->private_data;
  1645. int rc;
  1646. atomic_inc(&device->open_count);
  1647. if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1648. rc = -ENODEV;
  1649. goto unlock;
  1650. }
  1651. if (!try_module_get(device->discipline->owner)) {
  1652. rc = -EINVAL;
  1653. goto unlock;
  1654. }
  1655. if (dasd_probeonly) {
  1656. DEV_MESSAGE(KERN_INFO, device, "%s",
  1657. "No access to device due to probeonly mode");
  1658. rc = -EPERM;
  1659. goto out;
  1660. }
  1661. if (device->state <= DASD_STATE_BASIC) {
  1662. DBF_DEV_EVENT(DBF_ERR, device, " %s",
  1663. " Cannot open unrecognized device");
  1664. rc = -ENODEV;
  1665. goto out;
  1666. }
  1667. return 0;
  1668. out:
  1669. module_put(device->discipline->owner);
  1670. unlock:
  1671. atomic_dec(&device->open_count);
  1672. return rc;
  1673. }
  1674. static int
  1675. dasd_release(struct inode *inp, struct file *filp)
  1676. {
  1677. struct gendisk *disk = inp->i_bdev->bd_disk;
  1678. struct dasd_device *device = disk->private_data;
  1679. atomic_dec(&device->open_count);
  1680. module_put(device->discipline->owner);
  1681. return 0;
  1682. }
  1683. /*
  1684. * Return disk geometry.
  1685. */
  1686. static int
  1687. dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1688. {
  1689. struct dasd_device *device;
  1690. device = bdev->bd_disk->private_data;
  1691. if (!device)
  1692. return -ENODEV;
  1693. if (!device->discipline ||
  1694. !device->discipline->fill_geometry)
  1695. return -EINVAL;
  1696. device->discipline->fill_geometry(device, geo);
  1697. geo->start = get_start_sect(bdev) >> device->s2b_shift;
  1698. return 0;
  1699. }
  1700. struct block_device_operations
  1701. dasd_device_operations = {
  1702. .owner = THIS_MODULE,
  1703. .open = dasd_open,
  1704. .release = dasd_release,
  1705. .ioctl = dasd_ioctl,
  1706. .compat_ioctl = dasd_compat_ioctl,
  1707. .getgeo = dasd_getgeo,
  1708. };
  1709. static void
  1710. dasd_exit(void)
  1711. {
  1712. #ifdef CONFIG_PROC_FS
  1713. dasd_proc_exit();
  1714. #endif
  1715. dasd_eer_exit();
  1716. if (dasd_page_cache != NULL) {
  1717. kmem_cache_destroy(dasd_page_cache);
  1718. dasd_page_cache = NULL;
  1719. }
  1720. dasd_gendisk_exit();
  1721. dasd_devmap_exit();
  1722. if (dasd_debug_area != NULL) {
  1723. debug_unregister(dasd_debug_area);
  1724. dasd_debug_area = NULL;
  1725. }
  1726. }
  1727. /*
  1728. * SECTION: common functions for ccw_driver use
  1729. */
  1730. /*
  1731. * Initial attempt at a probe function. this can be simplified once
  1732. * the other detection code is gone.
  1733. */
  1734. int
  1735. dasd_generic_probe (struct ccw_device *cdev,
  1736. struct dasd_discipline *discipline)
  1737. {
  1738. int ret;
  1739. ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
  1740. if (ret) {
  1741. printk(KERN_WARNING
  1742. "dasd_generic_probe: could not set ccw-device options "
  1743. "for %s\n", cdev->dev.bus_id);
  1744. return ret;
  1745. }
  1746. ret = dasd_add_sysfs_files(cdev);
  1747. if (ret) {
  1748. printk(KERN_WARNING
  1749. "dasd_generic_probe: could not add sysfs entries "
  1750. "for %s\n", cdev->dev.bus_id);
  1751. return ret;
  1752. }
  1753. cdev->handler = &dasd_int_handler;
  1754. /*
  1755. * Automatically online either all dasd devices (dasd_autodetect)
  1756. * or all devices specified with dasd= parameters during
  1757. * initial probe.
  1758. */
  1759. if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
  1760. (dasd_autodetect && dasd_busid_known(cdev->dev.bus_id) != 0))
  1761. ret = ccw_device_set_online(cdev);
  1762. if (ret)
  1763. printk(KERN_WARNING
  1764. "dasd_generic_probe: could not initially online "
  1765. "ccw-device %s\n", cdev->dev.bus_id);
  1766. return ret;
  1767. }
  1768. /*
  1769. * This will one day be called from a global not_oper handler.
  1770. * It is also used by driver_unregister during module unload.
  1771. */
  1772. void
  1773. dasd_generic_remove (struct ccw_device *cdev)
  1774. {
  1775. struct dasd_device *device;
  1776. cdev->handler = NULL;
  1777. dasd_remove_sysfs_files(cdev);
  1778. device = dasd_device_from_cdev(cdev);
  1779. if (IS_ERR(device))
  1780. return;
  1781. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1782. /* Already doing offline processing */
  1783. dasd_put_device(device);
  1784. return;
  1785. }
  1786. /*
  1787. * This device is removed unconditionally. Set offline
  1788. * flag to prevent dasd_open from opening it while it is
  1789. * no quite down yet.
  1790. */
  1791. dasd_set_target_state(device, DASD_STATE_NEW);
  1792. /* dasd_delete_device destroys the device reference. */
  1793. dasd_delete_device(device);
  1794. }
  1795. /*
  1796. * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
  1797. * the device is detected for the first time and is supposed to be used
  1798. * or the user has started activation through sysfs.
  1799. */
  1800. int
  1801. dasd_generic_set_online (struct ccw_device *cdev,
  1802. struct dasd_discipline *base_discipline)
  1803. {
  1804. struct dasd_discipline *discipline;
  1805. struct dasd_device *device;
  1806. int rc;
  1807. /* first online clears initial online feature flag */
  1808. dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
  1809. device = dasd_create_device(cdev);
  1810. if (IS_ERR(device))
  1811. return PTR_ERR(device);
  1812. discipline = base_discipline;
  1813. if (device->features & DASD_FEATURE_USEDIAG) {
  1814. if (!dasd_diag_discipline_pointer) {
  1815. printk (KERN_WARNING
  1816. "dasd_generic couldn't online device %s "
  1817. "- discipline DIAG not available\n",
  1818. cdev->dev.bus_id);
  1819. dasd_delete_device(device);
  1820. return -ENODEV;
  1821. }
  1822. discipline = dasd_diag_discipline_pointer;
  1823. }
  1824. if (!try_module_get(base_discipline->owner)) {
  1825. dasd_delete_device(device);
  1826. return -EINVAL;
  1827. }
  1828. if (!try_module_get(discipline->owner)) {
  1829. module_put(base_discipline->owner);
  1830. dasd_delete_device(device);
  1831. return -EINVAL;
  1832. }
  1833. device->base_discipline = base_discipline;
  1834. device->discipline = discipline;
  1835. rc = discipline->check_device(device);
  1836. if (rc) {
  1837. printk (KERN_WARNING
  1838. "dasd_generic couldn't online device %s "
  1839. "with discipline %s rc=%i\n",
  1840. cdev->dev.bus_id, discipline->name, rc);
  1841. module_put(discipline->owner);
  1842. module_put(base_discipline->owner);
  1843. dasd_delete_device(device);
  1844. return rc;
  1845. }
  1846. dasd_set_target_state(device, DASD_STATE_ONLINE);
  1847. if (device->state <= DASD_STATE_KNOWN) {
  1848. printk (KERN_WARNING
  1849. "dasd_generic discipline not found for %s\n",
  1850. cdev->dev.bus_id);
  1851. rc = -ENODEV;
  1852. dasd_set_target_state(device, DASD_STATE_NEW);
  1853. dasd_delete_device(device);
  1854. } else
  1855. pr_debug("dasd_generic device %s found\n",
  1856. cdev->dev.bus_id);
  1857. /* FIXME: we have to wait for the root device but we don't want
  1858. * to wait for each single device but for all at once. */
  1859. wait_event(dasd_init_waitq, _wait_for_device(device));
  1860. dasd_put_device(device);
  1861. return rc;
  1862. }
  1863. int
  1864. dasd_generic_set_offline (struct ccw_device *cdev)
  1865. {
  1866. struct dasd_device *device;
  1867. int max_count, open_count;
  1868. device = dasd_device_from_cdev(cdev);
  1869. if (IS_ERR(device))
  1870. return PTR_ERR(device);
  1871. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1872. /* Already doing offline processing */
  1873. dasd_put_device(device);
  1874. return 0;
  1875. }
  1876. /*
  1877. * We must make sure that this device is currently not in use.
  1878. * The open_count is increased for every opener, that includes
  1879. * the blkdev_get in dasd_scan_partitions. We are only interested
  1880. * in the other openers.
  1881. */
  1882. max_count = device->bdev ? 0 : -1;
  1883. open_count = (int) atomic_read(&device->open_count);
  1884. if (open_count > max_count) {
  1885. if (open_count > 0)
  1886. printk (KERN_WARNING "Can't offline dasd device with "
  1887. "open count = %i.\n",
  1888. open_count);
  1889. else
  1890. printk (KERN_WARNING "%s",
  1891. "Can't offline dasd device due to internal "
  1892. "use\n");
  1893. clear_bit(DASD_FLAG_OFFLINE, &device->flags);
  1894. dasd_put_device(device);
  1895. return -EBUSY;
  1896. }
  1897. dasd_set_target_state(device, DASD_STATE_NEW);
  1898. /* dasd_delete_device destroys the device reference. */
  1899. dasd_delete_device(device);
  1900. return 0;
  1901. }
  1902. int
  1903. dasd_generic_notify(struct ccw_device *cdev, int event)
  1904. {
  1905. struct dasd_device *device;
  1906. struct dasd_ccw_req *cqr;
  1907. unsigned long flags;
  1908. int ret;
  1909. device = dasd_device_from_cdev(cdev);
  1910. if (IS_ERR(device))
  1911. return 0;
  1912. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1913. ret = 0;
  1914. switch (event) {
  1915. case CIO_GONE:
  1916. case CIO_NO_PATH:
  1917. /* First of all call extended error reporting. */
  1918. dasd_eer_write(device, NULL, DASD_EER_NOPATH);
  1919. if (device->state < DASD_STATE_BASIC)
  1920. break;
  1921. /* Device is active. We want to keep it. */
  1922. if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
  1923. list_for_each_entry(cqr, &device->ccw_queue, list)
  1924. if (cqr->status == DASD_CQR_IN_IO)
  1925. cqr->status = DASD_CQR_FAILED;
  1926. device->stopped |= DASD_STOPPED_DC_EIO;
  1927. } else {
  1928. list_for_each_entry(cqr, &device->ccw_queue, list)
  1929. if (cqr->status == DASD_CQR_IN_IO) {
  1930. cqr->status = DASD_CQR_QUEUED;
  1931. cqr->retries++;
  1932. }
  1933. device->stopped |= DASD_STOPPED_DC_WAIT;
  1934. dasd_set_timer(device, 0);
  1935. }
  1936. dasd_schedule_bh(device);
  1937. ret = 1;
  1938. break;
  1939. case CIO_OPER:
  1940. /* FIXME: add a sanity check. */
  1941. device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
  1942. dasd_schedule_bh(device);
  1943. ret = 1;
  1944. break;
  1945. }
  1946. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1947. dasd_put_device(device);
  1948. return ret;
  1949. }
  1950. static int __init
  1951. dasd_init(void)
  1952. {
  1953. int rc;
  1954. init_waitqueue_head(&dasd_init_waitq);
  1955. init_waitqueue_head(&dasd_flush_wq);
  1956. /* register 'common' DASD debug area, used for all DBF_XXX calls */
  1957. dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
  1958. if (dasd_debug_area == NULL) {
  1959. rc = -ENOMEM;
  1960. goto failed;
  1961. }
  1962. debug_register_view(dasd_debug_area, &debug_sprintf_view);
  1963. debug_set_level(dasd_debug_area, DBF_WARNING);
  1964. DBF_EVENT(DBF_EMERG, "%s", "debug area created");
  1965. dasd_diag_discipline_pointer = NULL;
  1966. rc = dasd_devmap_init();
  1967. if (rc)
  1968. goto failed;
  1969. rc = dasd_gendisk_init();
  1970. if (rc)
  1971. goto failed;
  1972. rc = dasd_parse();
  1973. if (rc)
  1974. goto failed;
  1975. rc = dasd_eer_init();
  1976. if (rc)
  1977. goto failed;
  1978. #ifdef CONFIG_PROC_FS
  1979. rc = dasd_proc_init();
  1980. if (rc)
  1981. goto failed;
  1982. #endif
  1983. return 0;
  1984. failed:
  1985. MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
  1986. dasd_exit();
  1987. return rc;
  1988. }
  1989. module_init(dasd_init);
  1990. module_exit(dasd_exit);
  1991. EXPORT_SYMBOL(dasd_debug_area);
  1992. EXPORT_SYMBOL(dasd_diag_discipline_pointer);
  1993. EXPORT_SYMBOL(dasd_add_request_head);
  1994. EXPORT_SYMBOL(dasd_add_request_tail);
  1995. EXPORT_SYMBOL(dasd_cancel_req);
  1996. EXPORT_SYMBOL(dasd_clear_timer);
  1997. EXPORT_SYMBOL(dasd_enable_device);
  1998. EXPORT_SYMBOL(dasd_int_handler);
  1999. EXPORT_SYMBOL(dasd_kfree_request);
  2000. EXPORT_SYMBOL(dasd_kick_device);
  2001. EXPORT_SYMBOL(dasd_kmalloc_request);
  2002. EXPORT_SYMBOL(dasd_schedule_bh);
  2003. EXPORT_SYMBOL(dasd_set_target_state);
  2004. EXPORT_SYMBOL(dasd_set_timer);
  2005. EXPORT_SYMBOL(dasd_sfree_request);
  2006. EXPORT_SYMBOL(dasd_sleep_on);
  2007. EXPORT_SYMBOL(dasd_sleep_on_immediatly);
  2008. EXPORT_SYMBOL(dasd_sleep_on_interruptible);
  2009. EXPORT_SYMBOL(dasd_smalloc_request);
  2010. EXPORT_SYMBOL(dasd_start_IO);
  2011. EXPORT_SYMBOL(dasd_term_IO);
  2012. EXPORT_SYMBOL_GPL(dasd_generic_probe);
  2013. EXPORT_SYMBOL_GPL(dasd_generic_remove);
  2014. EXPORT_SYMBOL_GPL(dasd_generic_notify);
  2015. EXPORT_SYMBOL_GPL(dasd_generic_set_online);
  2016. EXPORT_SYMBOL_GPL(dasd_generic_set_offline);