dasd.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  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. /* let user-space know that the device status changed */
  350. kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
  351. }
  352. /*
  353. * Kick starter for devices that did not complete the startup/shutdown
  354. * procedure or were sleeping because of a pending state.
  355. * dasd_kick_device will schedule a call do do_kick_device to the kernel
  356. * event daemon.
  357. */
  358. static void
  359. do_kick_device(struct work_struct *work)
  360. {
  361. struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
  362. dasd_change_state(device);
  363. dasd_schedule_bh(device);
  364. dasd_put_device(device);
  365. }
  366. void
  367. dasd_kick_device(struct dasd_device *device)
  368. {
  369. dasd_get_device(device);
  370. /* queue call to dasd_kick_device to the kernel event daemon. */
  371. schedule_work(&device->kick_work);
  372. }
  373. /*
  374. * Set the target state for a device and starts the state change.
  375. */
  376. void
  377. dasd_set_target_state(struct dasd_device *device, int target)
  378. {
  379. /* If we are in probeonly mode stop at DASD_STATE_READY. */
  380. if (dasd_probeonly && target > DASD_STATE_READY)
  381. target = DASD_STATE_READY;
  382. if (device->target != target) {
  383. if (device->state == target)
  384. wake_up(&dasd_init_waitq);
  385. device->target = target;
  386. }
  387. if (device->state != device->target)
  388. dasd_change_state(device);
  389. }
  390. /*
  391. * Enable devices with device numbers in [from..to].
  392. */
  393. static inline int
  394. _wait_for_device(struct dasd_device *device)
  395. {
  396. return (device->state == device->target);
  397. }
  398. void
  399. dasd_enable_device(struct dasd_device *device)
  400. {
  401. dasd_set_target_state(device, DASD_STATE_ONLINE);
  402. if (device->state <= DASD_STATE_KNOWN)
  403. /* No discipline for device found. */
  404. dasd_set_target_state(device, DASD_STATE_NEW);
  405. /* Now wait for the devices to come up. */
  406. wait_event(dasd_init_waitq, _wait_for_device(device));
  407. }
  408. /*
  409. * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
  410. */
  411. #ifdef CONFIG_DASD_PROFILE
  412. struct dasd_profile_info_t dasd_global_profile;
  413. unsigned int dasd_profile_level = DASD_PROFILE_OFF;
  414. /*
  415. * Increments counter in global and local profiling structures.
  416. */
  417. #define dasd_profile_counter(value, counter, device) \
  418. { \
  419. int index; \
  420. for (index = 0; index < 31 && value >> (2+index); index++); \
  421. dasd_global_profile.counter[index]++; \
  422. device->profile.counter[index]++; \
  423. }
  424. /*
  425. * Add profiling information for cqr before execution.
  426. */
  427. static void
  428. dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
  429. struct request *req)
  430. {
  431. struct list_head *l;
  432. unsigned int counter;
  433. if (dasd_profile_level != DASD_PROFILE_ON)
  434. return;
  435. /* count the length of the chanq for statistics */
  436. counter = 0;
  437. list_for_each(l, &device->ccw_queue)
  438. if (++counter >= 31)
  439. break;
  440. dasd_global_profile.dasd_io_nr_req[counter]++;
  441. device->profile.dasd_io_nr_req[counter]++;
  442. }
  443. /*
  444. * Add profiling information for cqr after execution.
  445. */
  446. static void
  447. dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
  448. struct request *req)
  449. {
  450. long strtime, irqtime, endtime, tottime; /* in microseconds */
  451. long tottimeps, sectors;
  452. if (dasd_profile_level != DASD_PROFILE_ON)
  453. return;
  454. sectors = req->nr_sectors;
  455. if (!cqr->buildclk || !cqr->startclk ||
  456. !cqr->stopclk || !cqr->endclk ||
  457. !sectors)
  458. return;
  459. strtime = ((cqr->startclk - cqr->buildclk) >> 12);
  460. irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
  461. endtime = ((cqr->endclk - cqr->stopclk) >> 12);
  462. tottime = ((cqr->endclk - cqr->buildclk) >> 12);
  463. tottimeps = tottime / sectors;
  464. if (!dasd_global_profile.dasd_io_reqs)
  465. memset(&dasd_global_profile, 0,
  466. sizeof (struct dasd_profile_info_t));
  467. dasd_global_profile.dasd_io_reqs++;
  468. dasd_global_profile.dasd_io_sects += sectors;
  469. if (!device->profile.dasd_io_reqs)
  470. memset(&device->profile, 0,
  471. sizeof (struct dasd_profile_info_t));
  472. device->profile.dasd_io_reqs++;
  473. device->profile.dasd_io_sects += sectors;
  474. dasd_profile_counter(sectors, dasd_io_secs, device);
  475. dasd_profile_counter(tottime, dasd_io_times, device);
  476. dasd_profile_counter(tottimeps, dasd_io_timps, device);
  477. dasd_profile_counter(strtime, dasd_io_time1, device);
  478. dasd_profile_counter(irqtime, dasd_io_time2, device);
  479. dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
  480. dasd_profile_counter(endtime, dasd_io_time3, device);
  481. }
  482. #else
  483. #define dasd_profile_start(device, cqr, req) do {} while (0)
  484. #define dasd_profile_end(device, cqr, req) do {} while (0)
  485. #endif /* CONFIG_DASD_PROFILE */
  486. /*
  487. * Allocate memory for a channel program with 'cplength' channel
  488. * command words and 'datasize' additional space. There are two
  489. * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
  490. * memory and 2) dasd_smalloc_request uses the static ccw memory
  491. * that gets allocated for each device.
  492. */
  493. struct dasd_ccw_req *
  494. dasd_kmalloc_request(char *magic, int cplength, int datasize,
  495. struct dasd_device * device)
  496. {
  497. struct dasd_ccw_req *cqr;
  498. /* Sanity checks */
  499. BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
  500. (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  501. cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
  502. if (cqr == NULL)
  503. return ERR_PTR(-ENOMEM);
  504. cqr->cpaddr = NULL;
  505. if (cplength > 0) {
  506. cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
  507. GFP_ATOMIC | GFP_DMA);
  508. if (cqr->cpaddr == NULL) {
  509. kfree(cqr);
  510. return ERR_PTR(-ENOMEM);
  511. }
  512. }
  513. cqr->data = NULL;
  514. if (datasize > 0) {
  515. cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
  516. if (cqr->data == NULL) {
  517. kfree(cqr->cpaddr);
  518. kfree(cqr);
  519. return ERR_PTR(-ENOMEM);
  520. }
  521. }
  522. strncpy((char *) &cqr->magic, magic, 4);
  523. ASCEBC((char *) &cqr->magic, 4);
  524. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  525. dasd_get_device(device);
  526. return cqr;
  527. }
  528. struct dasd_ccw_req *
  529. dasd_smalloc_request(char *magic, int cplength, int datasize,
  530. struct dasd_device * device)
  531. {
  532. unsigned long flags;
  533. struct dasd_ccw_req *cqr;
  534. char *data;
  535. int size;
  536. /* Sanity checks */
  537. BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
  538. (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  539. size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
  540. if (cplength > 0)
  541. size += cplength * sizeof(struct ccw1);
  542. if (datasize > 0)
  543. size += datasize;
  544. spin_lock_irqsave(&device->mem_lock, flags);
  545. cqr = (struct dasd_ccw_req *)
  546. dasd_alloc_chunk(&device->ccw_chunks, size);
  547. spin_unlock_irqrestore(&device->mem_lock, flags);
  548. if (cqr == NULL)
  549. return ERR_PTR(-ENOMEM);
  550. memset(cqr, 0, sizeof(struct dasd_ccw_req));
  551. data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
  552. cqr->cpaddr = NULL;
  553. if (cplength > 0) {
  554. cqr->cpaddr = (struct ccw1 *) data;
  555. data += cplength*sizeof(struct ccw1);
  556. memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
  557. }
  558. cqr->data = NULL;
  559. if (datasize > 0) {
  560. cqr->data = data;
  561. memset(cqr->data, 0, datasize);
  562. }
  563. strncpy((char *) &cqr->magic, magic, 4);
  564. ASCEBC((char *) &cqr->magic, 4);
  565. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  566. dasd_get_device(device);
  567. return cqr;
  568. }
  569. /*
  570. * Free memory of a channel program. This function needs to free all the
  571. * idal lists that might have been created by dasd_set_cda and the
  572. * struct dasd_ccw_req itself.
  573. */
  574. void
  575. dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
  576. {
  577. #ifdef CONFIG_64BIT
  578. struct ccw1 *ccw;
  579. /* Clear any idals used for the request. */
  580. ccw = cqr->cpaddr;
  581. do {
  582. clear_normalized_cda(ccw);
  583. } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
  584. #endif
  585. kfree(cqr->cpaddr);
  586. kfree(cqr->data);
  587. kfree(cqr);
  588. dasd_put_device(device);
  589. }
  590. void
  591. dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
  592. {
  593. unsigned long flags;
  594. spin_lock_irqsave(&device->mem_lock, flags);
  595. dasd_free_chunk(&device->ccw_chunks, cqr);
  596. spin_unlock_irqrestore(&device->mem_lock, flags);
  597. dasd_put_device(device);
  598. }
  599. /*
  600. * Check discipline magic in cqr.
  601. */
  602. static inline int
  603. dasd_check_cqr(struct dasd_ccw_req *cqr)
  604. {
  605. struct dasd_device *device;
  606. if (cqr == NULL)
  607. return -EINVAL;
  608. device = cqr->device;
  609. if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
  610. DEV_MESSAGE(KERN_WARNING, device,
  611. " dasd_ccw_req 0x%08x magic doesn't match"
  612. " discipline 0x%08x",
  613. cqr->magic,
  614. *(unsigned int *) device->discipline->name);
  615. return -EINVAL;
  616. }
  617. return 0;
  618. }
  619. /*
  620. * Terminate the current i/o and set the request to clear_pending.
  621. * Timer keeps device runnig.
  622. * ccw_device_clear can fail if the i/o subsystem
  623. * is in a bad mood.
  624. */
  625. int
  626. dasd_term_IO(struct dasd_ccw_req * cqr)
  627. {
  628. struct dasd_device *device;
  629. int retries, rc;
  630. /* Check the cqr */
  631. rc = dasd_check_cqr(cqr);
  632. if (rc)
  633. return rc;
  634. retries = 0;
  635. device = (struct dasd_device *) cqr->device;
  636. while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
  637. rc = ccw_device_clear(device->cdev, (long) cqr);
  638. switch (rc) {
  639. case 0: /* termination successful */
  640. cqr->retries--;
  641. cqr->status = DASD_CQR_CLEAR;
  642. cqr->stopclk = get_clock();
  643. cqr->starttime = 0;
  644. DBF_DEV_EVENT(DBF_DEBUG, device,
  645. "terminate cqr %p successful",
  646. cqr);
  647. break;
  648. case -ENODEV:
  649. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  650. "device gone, retry");
  651. break;
  652. case -EIO:
  653. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  654. "I/O error, retry");
  655. break;
  656. case -EINVAL:
  657. case -EBUSY:
  658. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  659. "device busy, retry later");
  660. break;
  661. default:
  662. DEV_MESSAGE(KERN_ERR, device,
  663. "line %d unknown RC=%d, please "
  664. "report to linux390@de.ibm.com",
  665. __LINE__, rc);
  666. BUG();
  667. break;
  668. }
  669. retries++;
  670. }
  671. dasd_schedule_bh(device);
  672. return rc;
  673. }
  674. /*
  675. * Start the i/o. This start_IO can fail if the channel is really busy.
  676. * In that case set up a timer to start the request later.
  677. */
  678. int
  679. dasd_start_IO(struct dasd_ccw_req * cqr)
  680. {
  681. struct dasd_device *device;
  682. int rc;
  683. /* Check the cqr */
  684. rc = dasd_check_cqr(cqr);
  685. if (rc)
  686. return rc;
  687. device = (struct dasd_device *) cqr->device;
  688. if (cqr->retries < 0) {
  689. DEV_MESSAGE(KERN_DEBUG, device,
  690. "start_IO: request %p (%02x/%i) - no retry left.",
  691. cqr, cqr->status, cqr->retries);
  692. cqr->status = DASD_CQR_FAILED;
  693. return -EIO;
  694. }
  695. cqr->startclk = get_clock();
  696. cqr->starttime = jiffies;
  697. cqr->retries--;
  698. rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
  699. cqr->lpm, 0);
  700. switch (rc) {
  701. case 0:
  702. cqr->status = DASD_CQR_IN_IO;
  703. DBF_DEV_EVENT(DBF_DEBUG, device,
  704. "start_IO: request %p started successful",
  705. cqr);
  706. break;
  707. case -EBUSY:
  708. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  709. "start_IO: device busy, retry later");
  710. break;
  711. case -ETIMEDOUT:
  712. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  713. "start_IO: request timeout, retry later");
  714. break;
  715. case -EACCES:
  716. /* -EACCES indicates that the request used only a
  717. * subset of the available pathes and all these
  718. * pathes are gone.
  719. * Do a retry with all available pathes.
  720. */
  721. cqr->lpm = LPM_ANYPATH;
  722. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  723. "start_IO: selected pathes gone,"
  724. " retry on all pathes");
  725. break;
  726. case -ENODEV:
  727. case -EIO:
  728. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  729. "start_IO: device gone, retry");
  730. break;
  731. default:
  732. DEV_MESSAGE(KERN_ERR, device,
  733. "line %d unknown RC=%d, please report"
  734. " to linux390@de.ibm.com", __LINE__, rc);
  735. BUG();
  736. break;
  737. }
  738. return rc;
  739. }
  740. /*
  741. * Timeout function for dasd devices. This is used for different purposes
  742. * 1) missing interrupt handler for normal operation
  743. * 2) delayed start of request where start_IO failed with -EBUSY
  744. * 3) timeout for missing state change interrupts
  745. * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
  746. * DASD_CQR_QUEUED for 2) and 3).
  747. */
  748. static void
  749. dasd_timeout_device(unsigned long ptr)
  750. {
  751. unsigned long flags;
  752. struct dasd_device *device;
  753. device = (struct dasd_device *) ptr;
  754. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  755. /* re-activate request queue */
  756. device->stopped &= ~DASD_STOPPED_PENDING;
  757. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  758. dasd_schedule_bh(device);
  759. }
  760. /*
  761. * Setup timeout for a device in jiffies.
  762. */
  763. void
  764. dasd_set_timer(struct dasd_device *device, int expires)
  765. {
  766. if (expires == 0) {
  767. if (timer_pending(&device->timer))
  768. del_timer(&device->timer);
  769. return;
  770. }
  771. if (timer_pending(&device->timer)) {
  772. if (mod_timer(&device->timer, jiffies + expires))
  773. return;
  774. }
  775. device->timer.function = dasd_timeout_device;
  776. device->timer.data = (unsigned long) device;
  777. device->timer.expires = jiffies + expires;
  778. add_timer(&device->timer);
  779. }
  780. /*
  781. * Clear timeout for a device.
  782. */
  783. void
  784. dasd_clear_timer(struct dasd_device *device)
  785. {
  786. if (timer_pending(&device->timer))
  787. del_timer(&device->timer);
  788. }
  789. static void
  790. dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
  791. {
  792. struct dasd_ccw_req *cqr;
  793. struct dasd_device *device;
  794. cqr = (struct dasd_ccw_req *) intparm;
  795. if (cqr->status != DASD_CQR_IN_IO) {
  796. MESSAGE(KERN_DEBUG,
  797. "invalid status in handle_killed_request: "
  798. "bus_id %s, status %02x",
  799. cdev->dev.bus_id, cqr->status);
  800. return;
  801. }
  802. device = (struct dasd_device *) cqr->device;
  803. if (device == NULL ||
  804. device != dasd_device_from_cdev_locked(cdev) ||
  805. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  806. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  807. cdev->dev.bus_id);
  808. return;
  809. }
  810. /* Schedule request to be retried. */
  811. cqr->status = DASD_CQR_QUEUED;
  812. dasd_clear_timer(device);
  813. dasd_schedule_bh(device);
  814. dasd_put_device(device);
  815. }
  816. static void
  817. dasd_handle_state_change_pending(struct dasd_device *device)
  818. {
  819. struct dasd_ccw_req *cqr;
  820. struct list_head *l, *n;
  821. /* First of all start sense subsystem status request. */
  822. dasd_eer_snss(device);
  823. device->stopped &= ~DASD_STOPPED_PENDING;
  824. /* restart all 'running' IO on queue */
  825. list_for_each_safe(l, n, &device->ccw_queue) {
  826. cqr = list_entry(l, struct dasd_ccw_req, list);
  827. if (cqr->status == DASD_CQR_IN_IO) {
  828. cqr->status = DASD_CQR_QUEUED;
  829. }
  830. }
  831. dasd_clear_timer(device);
  832. dasd_schedule_bh(device);
  833. }
  834. /*
  835. * Interrupt handler for "normal" ssch-io based dasd devices.
  836. */
  837. void
  838. dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
  839. struct irb *irb)
  840. {
  841. struct dasd_ccw_req *cqr, *next;
  842. struct dasd_device *device;
  843. unsigned long long now;
  844. int expires;
  845. dasd_era_t era;
  846. char mask;
  847. if (IS_ERR(irb)) {
  848. switch (PTR_ERR(irb)) {
  849. case -EIO:
  850. dasd_handle_killed_request(cdev, intparm);
  851. break;
  852. case -ETIMEDOUT:
  853. printk(KERN_WARNING"%s(%s): request timed out\n",
  854. __FUNCTION__, cdev->dev.bus_id);
  855. //FIXME - dasd uses own timeout interface...
  856. break;
  857. default:
  858. printk(KERN_WARNING"%s(%s): unknown error %ld\n",
  859. __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
  860. }
  861. return;
  862. }
  863. now = get_clock();
  864. DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
  865. cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
  866. (unsigned int) intparm);
  867. /* first of all check for state change pending interrupt */
  868. mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
  869. if ((irb->scsw.dstat & mask) == mask) {
  870. device = dasd_device_from_cdev_locked(cdev);
  871. if (!IS_ERR(device)) {
  872. dasd_handle_state_change_pending(device);
  873. dasd_put_device(device);
  874. }
  875. return;
  876. }
  877. cqr = (struct dasd_ccw_req *) intparm;
  878. /* check for unsolicited interrupts */
  879. if (cqr == NULL) {
  880. MESSAGE(KERN_DEBUG,
  881. "unsolicited interrupt received: bus_id %s",
  882. cdev->dev.bus_id);
  883. return;
  884. }
  885. device = (struct dasd_device *) cqr->device;
  886. if (device == NULL ||
  887. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  888. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  889. cdev->dev.bus_id);
  890. return;
  891. }
  892. /* Check for clear pending */
  893. if (cqr->status == DASD_CQR_CLEAR &&
  894. irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
  895. cqr->status = DASD_CQR_QUEUED;
  896. dasd_clear_timer(device);
  897. wake_up(&dasd_flush_wq);
  898. dasd_schedule_bh(device);
  899. return;
  900. }
  901. /* check status - the request might have been killed by dyn detach */
  902. if (cqr->status != DASD_CQR_IN_IO) {
  903. MESSAGE(KERN_DEBUG,
  904. "invalid status: bus_id %s, status %02x",
  905. cdev->dev.bus_id, cqr->status);
  906. return;
  907. }
  908. DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
  909. ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
  910. /* Find out the appropriate era_action. */
  911. if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
  912. era = dasd_era_fatal;
  913. else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  914. irb->scsw.cstat == 0 &&
  915. !irb->esw.esw0.erw.cons)
  916. era = dasd_era_none;
  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. if (device->features & DASD_FEATURE_ERPLOG) {
  943. /* dump sense data */
  944. dasd_log_sense(cqr, irb);
  945. }
  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 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. test_bit(DASD_CQR_FLAGS_USE_ERP,
  1016. &cqr->flags)) {
  1017. erp_fn = device->discipline->
  1018. erp_action(cqr);
  1019. erp_fn(cqr);
  1020. } else
  1021. dasd_default_erp_action(cqr);
  1022. }
  1023. goto restart;
  1024. }
  1025. /* First of all call extended error reporting. */
  1026. if (dasd_eer_enabled(device) &&
  1027. cqr->status == DASD_CQR_FAILED) {
  1028. dasd_eer_write(device, cqr, DASD_EER_FATALERROR);
  1029. /* restart request */
  1030. cqr->status = DASD_CQR_QUEUED;
  1031. cqr->retries = 255;
  1032. device->stopped |= DASD_STOPPED_QUIESCE;
  1033. goto restart;
  1034. }
  1035. /* Process finished ERP request. */
  1036. if (cqr->refers) {
  1037. __dasd_process_erp(device, cqr);
  1038. goto restart;
  1039. }
  1040. /* Rechain finished requests to final queue */
  1041. cqr->endclk = get_clock();
  1042. list_move_tail(&cqr->list, final_queue);
  1043. }
  1044. }
  1045. static void
  1046. dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
  1047. {
  1048. struct request *req;
  1049. struct dasd_device *device;
  1050. int status;
  1051. req = (struct request *) data;
  1052. device = cqr->device;
  1053. dasd_profile_end(device, cqr, req);
  1054. status = cqr->device->discipline->free_cp(cqr,req);
  1055. spin_lock_irq(&device->request_queue_lock);
  1056. dasd_end_request(req, status);
  1057. spin_unlock_irq(&device->request_queue_lock);
  1058. }
  1059. /*
  1060. * Fetch requests from the block device queue.
  1061. */
  1062. static void
  1063. __dasd_process_blk_queue(struct dasd_device * device)
  1064. {
  1065. request_queue_t *queue;
  1066. struct request *req;
  1067. struct dasd_ccw_req *cqr;
  1068. int nr_queued;
  1069. queue = device->request_queue;
  1070. /* No queue ? Then there is nothing to do. */
  1071. if (queue == NULL)
  1072. return;
  1073. /*
  1074. * We requeue request from the block device queue to the ccw
  1075. * queue only in two states. In state DASD_STATE_READY the
  1076. * partition detection is done and we need to requeue requests
  1077. * for that. State DASD_STATE_ONLINE is normal block device
  1078. * operation.
  1079. */
  1080. if (device->state != DASD_STATE_READY &&
  1081. device->state != DASD_STATE_ONLINE)
  1082. return;
  1083. nr_queued = 0;
  1084. /* Now we try to fetch requests from the request queue */
  1085. list_for_each_entry(cqr, &device->ccw_queue, list)
  1086. if (cqr->status == DASD_CQR_QUEUED)
  1087. nr_queued++;
  1088. while (!blk_queue_plugged(queue) &&
  1089. elv_next_request(queue) &&
  1090. nr_queued < DASD_CHANQ_MAX_SIZE) {
  1091. req = elv_next_request(queue);
  1092. if (device->features & DASD_FEATURE_READONLY &&
  1093. rq_data_dir(req) == WRITE) {
  1094. DBF_DEV_EVENT(DBF_ERR, device,
  1095. "Rejecting write request %p",
  1096. req);
  1097. blkdev_dequeue_request(req);
  1098. dasd_end_request(req, 0);
  1099. continue;
  1100. }
  1101. if (device->stopped & DASD_STOPPED_DC_EIO) {
  1102. blkdev_dequeue_request(req);
  1103. dasd_end_request(req, 0);
  1104. continue;
  1105. }
  1106. cqr = device->discipline->build_cp(device, req);
  1107. if (IS_ERR(cqr)) {
  1108. if (PTR_ERR(cqr) == -ENOMEM)
  1109. break; /* terminate request queue loop */
  1110. if (PTR_ERR(cqr) == -EAGAIN) {
  1111. /*
  1112. * The current request cannot be build right
  1113. * now, we have to try later. If this request
  1114. * is the head-of-queue we stop the device
  1115. * for 1/2 second.
  1116. */
  1117. if (!list_empty(&device->ccw_queue))
  1118. break;
  1119. device->stopped |= DASD_STOPPED_PENDING;
  1120. dasd_set_timer(device, HZ/2);
  1121. break;
  1122. }
  1123. DBF_DEV_EVENT(DBF_ERR, device,
  1124. "CCW creation failed (rc=%ld) "
  1125. "on request %p",
  1126. PTR_ERR(cqr), req);
  1127. blkdev_dequeue_request(req);
  1128. dasd_end_request(req, 0);
  1129. continue;
  1130. }
  1131. cqr->callback = dasd_end_request_cb;
  1132. cqr->callback_data = (void *) req;
  1133. cqr->status = DASD_CQR_QUEUED;
  1134. blkdev_dequeue_request(req);
  1135. list_add_tail(&cqr->list, &device->ccw_queue);
  1136. dasd_profile_start(device, cqr, req);
  1137. nr_queued++;
  1138. }
  1139. }
  1140. /*
  1141. * Take a look at the first request on the ccw queue and check
  1142. * if it reached its expire time. If so, terminate the IO.
  1143. */
  1144. static void
  1145. __dasd_check_expire(struct dasd_device * device)
  1146. {
  1147. struct dasd_ccw_req *cqr;
  1148. if (list_empty(&device->ccw_queue))
  1149. return;
  1150. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1151. if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
  1152. (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
  1153. if (device->discipline->term_IO(cqr) != 0) {
  1154. /* Hmpf, try again in 5 sec */
  1155. dasd_set_timer(device, 5*HZ);
  1156. DEV_MESSAGE(KERN_ERR, device,
  1157. "internal error - timeout (%is) expired "
  1158. "for cqr %p, termination failed, "
  1159. "retrying in 5s",
  1160. (cqr->expires/HZ), cqr);
  1161. } else {
  1162. DEV_MESSAGE(KERN_ERR, device,
  1163. "internal error - timeout (%is) expired "
  1164. "for cqr %p (%i retries left)",
  1165. (cqr->expires/HZ), cqr, cqr->retries);
  1166. }
  1167. }
  1168. }
  1169. /*
  1170. * Take a look at the first request on the ccw queue and check
  1171. * if it needs to be started.
  1172. */
  1173. static void
  1174. __dasd_start_head(struct dasd_device * device)
  1175. {
  1176. struct dasd_ccw_req *cqr;
  1177. int rc;
  1178. if (list_empty(&device->ccw_queue))
  1179. return;
  1180. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1181. if (cqr->status != DASD_CQR_QUEUED)
  1182. return;
  1183. /* Non-temporary stop condition will trigger fail fast */
  1184. if (device->stopped & ~DASD_STOPPED_PENDING &&
  1185. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1186. (!dasd_eer_enabled(device))) {
  1187. cqr->status = DASD_CQR_FAILED;
  1188. dasd_schedule_bh(device);
  1189. return;
  1190. }
  1191. /* Don't try to start requests if device is stopped */
  1192. if (device->stopped)
  1193. return;
  1194. rc = device->discipline->start_IO(cqr);
  1195. if (rc == 0)
  1196. dasd_set_timer(device, cqr->expires);
  1197. else if (rc == -EACCES) {
  1198. dasd_schedule_bh(device);
  1199. } else
  1200. /* Hmpf, try again in 1/2 sec */
  1201. dasd_set_timer(device, 50);
  1202. }
  1203. static inline int
  1204. _wait_for_clear(struct dasd_ccw_req *cqr)
  1205. {
  1206. return (cqr->status == DASD_CQR_QUEUED);
  1207. }
  1208. /*
  1209. * Remove all requests from the ccw queue (all = '1') or only block device
  1210. * requests in case all = '0'.
  1211. * Take care of the erp-chain (chained via cqr->refers) and remove either
  1212. * the whole erp-chain or none of the erp-requests.
  1213. * If a request is currently running, term_IO is called and the request
  1214. * is re-queued. Prior to removing the terminated request we need to wait
  1215. * for the clear-interrupt.
  1216. * In case termination is not possible we stop processing and just finishing
  1217. * the already moved requests.
  1218. */
  1219. static int
  1220. dasd_flush_ccw_queue(struct dasd_device * device, int all)
  1221. {
  1222. struct dasd_ccw_req *cqr, *orig, *n;
  1223. int rc, i;
  1224. struct list_head flush_queue;
  1225. INIT_LIST_HEAD(&flush_queue);
  1226. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1227. rc = 0;
  1228. restart:
  1229. list_for_each_entry_safe(cqr, n, &device->ccw_queue, list) {
  1230. /* get original request of erp request-chain */
  1231. for (orig = cqr; orig->refers != NULL; orig = orig->refers);
  1232. /* Flush all request or only block device requests? */
  1233. if (all == 0 && cqr->callback != dasd_end_request_cb &&
  1234. orig->callback != dasd_end_request_cb) {
  1235. continue;
  1236. }
  1237. /* Check status and move request to flush_queue */
  1238. switch (cqr->status) {
  1239. case DASD_CQR_IN_IO:
  1240. rc = device->discipline->term_IO(cqr);
  1241. if (rc) {
  1242. /* unable to terminate requeust */
  1243. DEV_MESSAGE(KERN_ERR, device,
  1244. "dasd flush ccw_queue is unable "
  1245. " to terminate request %p",
  1246. cqr);
  1247. /* stop flush processing */
  1248. goto finished;
  1249. }
  1250. break;
  1251. case DASD_CQR_QUEUED:
  1252. case DASD_CQR_ERROR:
  1253. /* set request to FAILED */
  1254. cqr->stopclk = get_clock();
  1255. cqr->status = DASD_CQR_FAILED;
  1256. break;
  1257. default: /* do not touch the others */
  1258. break;
  1259. }
  1260. /* Rechain request (including erp chain) */
  1261. for (i = 0; cqr != NULL; cqr = cqr->refers, i++) {
  1262. cqr->endclk = get_clock();
  1263. list_move_tail(&cqr->list, &flush_queue);
  1264. }
  1265. if (i > 1)
  1266. /* moved more than one request - need to restart */
  1267. goto restart;
  1268. }
  1269. finished:
  1270. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1271. /* Now call the callback function of flushed requests */
  1272. restart_cb:
  1273. list_for_each_entry_safe(cqr, n, &flush_queue, list) {
  1274. if (cqr->status == DASD_CQR_CLEAR) {
  1275. /* wait for clear interrupt! */
  1276. wait_event(dasd_flush_wq, _wait_for_clear(cqr));
  1277. cqr->status = DASD_CQR_FAILED;
  1278. }
  1279. /* Process finished ERP request. */
  1280. if (cqr->refers) {
  1281. __dasd_process_erp(device, cqr);
  1282. /* restart list_for_xx loop since dasd_process_erp
  1283. * might remove multiple elements */
  1284. goto restart_cb;
  1285. }
  1286. /* call the callback function */
  1287. cqr->endclk = get_clock();
  1288. if (cqr->callback != NULL)
  1289. (cqr->callback)(cqr, cqr->callback_data);
  1290. }
  1291. return rc;
  1292. }
  1293. /*
  1294. * Acquire the device lock and process queues for the device.
  1295. */
  1296. static void
  1297. dasd_tasklet(struct dasd_device * device)
  1298. {
  1299. struct list_head final_queue;
  1300. struct list_head *l, *n;
  1301. struct dasd_ccw_req *cqr;
  1302. atomic_set (&device->tasklet_scheduled, 0);
  1303. INIT_LIST_HEAD(&final_queue);
  1304. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1305. /* Check expire time of first request on the ccw queue. */
  1306. __dasd_check_expire(device);
  1307. /* Finish off requests on ccw queue */
  1308. __dasd_process_ccw_queue(device, &final_queue);
  1309. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1310. /* Now call the callback function of requests with final status */
  1311. list_for_each_safe(l, n, &final_queue) {
  1312. cqr = list_entry(l, struct dasd_ccw_req, list);
  1313. list_del_init(&cqr->list);
  1314. if (cqr->callback != NULL)
  1315. (cqr->callback)(cqr, cqr->callback_data);
  1316. }
  1317. spin_lock_irq(&device->request_queue_lock);
  1318. spin_lock(get_ccwdev_lock(device->cdev));
  1319. /* Get new request from the block device request queue */
  1320. __dasd_process_blk_queue(device);
  1321. /* Now check if the head of the ccw queue needs to be started. */
  1322. __dasd_start_head(device);
  1323. spin_unlock(get_ccwdev_lock(device->cdev));
  1324. spin_unlock_irq(&device->request_queue_lock);
  1325. dasd_put_device(device);
  1326. }
  1327. /*
  1328. * Schedules a call to dasd_tasklet over the device tasklet.
  1329. */
  1330. void
  1331. dasd_schedule_bh(struct dasd_device * device)
  1332. {
  1333. /* Protect against rescheduling. */
  1334. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  1335. return;
  1336. dasd_get_device(device);
  1337. tasklet_hi_schedule(&device->tasklet);
  1338. }
  1339. /*
  1340. * Queue a request to the head of the ccw_queue. Start the I/O if
  1341. * possible.
  1342. */
  1343. void
  1344. dasd_add_request_head(struct dasd_ccw_req *req)
  1345. {
  1346. struct dasd_device *device;
  1347. unsigned long flags;
  1348. device = req->device;
  1349. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1350. req->status = DASD_CQR_QUEUED;
  1351. req->device = device;
  1352. list_add(&req->list, &device->ccw_queue);
  1353. /* let the bh start the request to keep them in order */
  1354. dasd_schedule_bh(device);
  1355. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1356. }
  1357. /*
  1358. * Queue a request to the tail of the ccw_queue. Start the I/O if
  1359. * possible.
  1360. */
  1361. void
  1362. dasd_add_request_tail(struct dasd_ccw_req *req)
  1363. {
  1364. struct dasd_device *device;
  1365. unsigned long flags;
  1366. device = req->device;
  1367. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1368. req->status = DASD_CQR_QUEUED;
  1369. req->device = device;
  1370. list_add_tail(&req->list, &device->ccw_queue);
  1371. /* let the bh start the request to keep them in order */
  1372. dasd_schedule_bh(device);
  1373. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1374. }
  1375. /*
  1376. * Wakeup callback.
  1377. */
  1378. static void
  1379. dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  1380. {
  1381. wake_up((wait_queue_head_t *) data);
  1382. }
  1383. static inline int
  1384. _wait_for_wakeup(struct dasd_ccw_req *cqr)
  1385. {
  1386. struct dasd_device *device;
  1387. int rc;
  1388. device = cqr->device;
  1389. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1390. rc = ((cqr->status == DASD_CQR_DONE ||
  1391. cqr->status == DASD_CQR_FAILED) &&
  1392. list_empty(&cqr->list));
  1393. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1394. return rc;
  1395. }
  1396. /*
  1397. * Attempts to start a special ccw queue and waits for its completion.
  1398. */
  1399. int
  1400. dasd_sleep_on(struct dasd_ccw_req * cqr)
  1401. {
  1402. wait_queue_head_t wait_q;
  1403. struct dasd_device *device;
  1404. int rc;
  1405. device = cqr->device;
  1406. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1407. init_waitqueue_head (&wait_q);
  1408. cqr->callback = dasd_wakeup_cb;
  1409. cqr->callback_data = (void *) &wait_q;
  1410. cqr->status = DASD_CQR_QUEUED;
  1411. list_add_tail(&cqr->list, &device->ccw_queue);
  1412. /* let the bh start the request to keep them in order */
  1413. dasd_schedule_bh(device);
  1414. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1415. wait_event(wait_q, _wait_for_wakeup(cqr));
  1416. /* Request status is either done or failed. */
  1417. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1418. return rc;
  1419. }
  1420. /*
  1421. * Attempts to start a special ccw queue and wait interruptible
  1422. * for its completion.
  1423. */
  1424. int
  1425. dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
  1426. {
  1427. wait_queue_head_t wait_q;
  1428. struct dasd_device *device;
  1429. int rc, finished;
  1430. device = cqr->device;
  1431. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1432. init_waitqueue_head (&wait_q);
  1433. cqr->callback = dasd_wakeup_cb;
  1434. cqr->callback_data = (void *) &wait_q;
  1435. cqr->status = DASD_CQR_QUEUED;
  1436. list_add_tail(&cqr->list, &device->ccw_queue);
  1437. /* let the bh start the request to keep them in order */
  1438. dasd_schedule_bh(device);
  1439. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1440. finished = 0;
  1441. while (!finished) {
  1442. rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
  1443. if (rc != -ERESTARTSYS) {
  1444. /* Request is final (done or failed) */
  1445. rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  1446. break;
  1447. }
  1448. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1449. switch (cqr->status) {
  1450. case DASD_CQR_IN_IO:
  1451. /* terminate runnig cqr */
  1452. if (device->discipline->term_IO) {
  1453. cqr->retries = -1;
  1454. device->discipline->term_IO(cqr);
  1455. /* wait (non-interruptible) for final status
  1456. * because signal ist still pending */
  1457. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1458. wait_event(wait_q, _wait_for_wakeup(cqr));
  1459. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1460. rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  1461. finished = 1;
  1462. }
  1463. break;
  1464. case DASD_CQR_QUEUED:
  1465. /* request */
  1466. list_del_init(&cqr->list);
  1467. rc = -EIO;
  1468. finished = 1;
  1469. break;
  1470. default:
  1471. /* cqr with 'non-interruptable' status - just wait */
  1472. break;
  1473. }
  1474. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1475. }
  1476. return rc;
  1477. }
  1478. /*
  1479. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  1480. * for eckd devices) the currently running request has to be terminated
  1481. * and be put back to status queued, before the special request is added
  1482. * to the head of the queue. Then the special request is waited on normally.
  1483. */
  1484. static inline int
  1485. _dasd_term_running_cqr(struct dasd_device *device)
  1486. {
  1487. struct dasd_ccw_req *cqr;
  1488. if (list_empty(&device->ccw_queue))
  1489. return 0;
  1490. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1491. return device->discipline->term_IO(cqr);
  1492. }
  1493. int
  1494. dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
  1495. {
  1496. wait_queue_head_t wait_q;
  1497. struct dasd_device *device;
  1498. int rc;
  1499. device = cqr->device;
  1500. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1501. rc = _dasd_term_running_cqr(device);
  1502. if (rc) {
  1503. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1504. return rc;
  1505. }
  1506. init_waitqueue_head (&wait_q);
  1507. cqr->callback = dasd_wakeup_cb;
  1508. cqr->callback_data = (void *) &wait_q;
  1509. cqr->status = DASD_CQR_QUEUED;
  1510. list_add(&cqr->list, &device->ccw_queue);
  1511. /* let the bh start the request to keep them in order */
  1512. dasd_schedule_bh(device);
  1513. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1514. wait_event(wait_q, _wait_for_wakeup(cqr));
  1515. /* Request status is either done or failed. */
  1516. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1517. return rc;
  1518. }
  1519. /*
  1520. * Cancels a request that was started with dasd_sleep_on_req.
  1521. * This is useful to timeout requests. The request will be
  1522. * terminated if it is currently in i/o.
  1523. * Returns 1 if the request has been terminated.
  1524. */
  1525. int
  1526. dasd_cancel_req(struct dasd_ccw_req *cqr)
  1527. {
  1528. struct dasd_device *device = cqr->device;
  1529. unsigned long flags;
  1530. int rc;
  1531. rc = 0;
  1532. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1533. switch (cqr->status) {
  1534. case DASD_CQR_QUEUED:
  1535. /* request was not started - just set to failed */
  1536. cqr->status = DASD_CQR_FAILED;
  1537. break;
  1538. case DASD_CQR_IN_IO:
  1539. /* request in IO - terminate IO and release again */
  1540. if (device->discipline->term_IO(cqr) != 0)
  1541. /* what to do if unable to terminate ??????
  1542. e.g. not _IN_IO */
  1543. cqr->status = DASD_CQR_FAILED;
  1544. cqr->stopclk = get_clock();
  1545. rc = 1;
  1546. break;
  1547. case DASD_CQR_DONE:
  1548. case DASD_CQR_FAILED:
  1549. /* already finished - do nothing */
  1550. break;
  1551. default:
  1552. DEV_MESSAGE(KERN_ALERT, device,
  1553. "invalid status %02x in request",
  1554. cqr->status);
  1555. BUG();
  1556. }
  1557. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1558. dasd_schedule_bh(device);
  1559. return rc;
  1560. }
  1561. /*
  1562. * SECTION: Block device operations (request queue, partitions, open, release).
  1563. */
  1564. /*
  1565. * Dasd request queue function. Called from ll_rw_blk.c
  1566. */
  1567. static void
  1568. do_dasd_request(request_queue_t * queue)
  1569. {
  1570. struct dasd_device *device;
  1571. device = (struct dasd_device *) queue->queuedata;
  1572. spin_lock(get_ccwdev_lock(device->cdev));
  1573. /* Get new request from the block device request queue */
  1574. __dasd_process_blk_queue(device);
  1575. /* Now check if the head of the ccw queue needs to be started. */
  1576. __dasd_start_head(device);
  1577. spin_unlock(get_ccwdev_lock(device->cdev));
  1578. }
  1579. /*
  1580. * Allocate and initialize request queue and default I/O scheduler.
  1581. */
  1582. static int
  1583. dasd_alloc_queue(struct dasd_device * device)
  1584. {
  1585. int rc;
  1586. device->request_queue = blk_init_queue(do_dasd_request,
  1587. &device->request_queue_lock);
  1588. if (device->request_queue == NULL)
  1589. return -ENOMEM;
  1590. device->request_queue->queuedata = device;
  1591. elevator_exit(device->request_queue->elevator);
  1592. rc = elevator_init(device->request_queue, "deadline");
  1593. if (rc) {
  1594. blk_cleanup_queue(device->request_queue);
  1595. return rc;
  1596. }
  1597. return 0;
  1598. }
  1599. /*
  1600. * Allocate and initialize request queue.
  1601. */
  1602. static void
  1603. dasd_setup_queue(struct dasd_device * device)
  1604. {
  1605. int max;
  1606. blk_queue_hardsect_size(device->request_queue, device->bp_block);
  1607. max = device->discipline->max_blocks << device->s2b_shift;
  1608. blk_queue_max_sectors(device->request_queue, max);
  1609. blk_queue_max_phys_segments(device->request_queue, -1L);
  1610. blk_queue_max_hw_segments(device->request_queue, -1L);
  1611. blk_queue_max_segment_size(device->request_queue, -1L);
  1612. blk_queue_segment_boundary(device->request_queue, -1L);
  1613. blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
  1614. }
  1615. /*
  1616. * Deactivate and free request queue.
  1617. */
  1618. static void
  1619. dasd_free_queue(struct dasd_device * device)
  1620. {
  1621. if (device->request_queue) {
  1622. blk_cleanup_queue(device->request_queue);
  1623. device->request_queue = NULL;
  1624. }
  1625. }
  1626. /*
  1627. * Flush request on the request queue.
  1628. */
  1629. static void
  1630. dasd_flush_request_queue(struct dasd_device * device)
  1631. {
  1632. struct request *req;
  1633. if (!device->request_queue)
  1634. return;
  1635. spin_lock_irq(&device->request_queue_lock);
  1636. while ((req = elv_next_request(device->request_queue))) {
  1637. blkdev_dequeue_request(req);
  1638. dasd_end_request(req, 0);
  1639. }
  1640. spin_unlock_irq(&device->request_queue_lock);
  1641. }
  1642. static int
  1643. dasd_open(struct inode *inp, struct file *filp)
  1644. {
  1645. struct gendisk *disk = inp->i_bdev->bd_disk;
  1646. struct dasd_device *device = disk->private_data;
  1647. int rc;
  1648. atomic_inc(&device->open_count);
  1649. if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1650. rc = -ENODEV;
  1651. goto unlock;
  1652. }
  1653. if (!try_module_get(device->discipline->owner)) {
  1654. rc = -EINVAL;
  1655. goto unlock;
  1656. }
  1657. if (dasd_probeonly) {
  1658. DEV_MESSAGE(KERN_INFO, device, "%s",
  1659. "No access to device due to probeonly mode");
  1660. rc = -EPERM;
  1661. goto out;
  1662. }
  1663. if (device->state <= DASD_STATE_BASIC) {
  1664. DBF_DEV_EVENT(DBF_ERR, device, " %s",
  1665. " Cannot open unrecognized device");
  1666. rc = -ENODEV;
  1667. goto out;
  1668. }
  1669. return 0;
  1670. out:
  1671. module_put(device->discipline->owner);
  1672. unlock:
  1673. atomic_dec(&device->open_count);
  1674. return rc;
  1675. }
  1676. static int
  1677. dasd_release(struct inode *inp, struct file *filp)
  1678. {
  1679. struct gendisk *disk = inp->i_bdev->bd_disk;
  1680. struct dasd_device *device = disk->private_data;
  1681. atomic_dec(&device->open_count);
  1682. module_put(device->discipline->owner);
  1683. return 0;
  1684. }
  1685. /*
  1686. * Return disk geometry.
  1687. */
  1688. static int
  1689. dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1690. {
  1691. struct dasd_device *device;
  1692. device = bdev->bd_disk->private_data;
  1693. if (!device)
  1694. return -ENODEV;
  1695. if (!device->discipline ||
  1696. !device->discipline->fill_geometry)
  1697. return -EINVAL;
  1698. device->discipline->fill_geometry(device, geo);
  1699. geo->start = get_start_sect(bdev) >> device->s2b_shift;
  1700. return 0;
  1701. }
  1702. struct block_device_operations
  1703. dasd_device_operations = {
  1704. .owner = THIS_MODULE,
  1705. .open = dasd_open,
  1706. .release = dasd_release,
  1707. .ioctl = dasd_ioctl,
  1708. .compat_ioctl = dasd_compat_ioctl,
  1709. .getgeo = dasd_getgeo,
  1710. };
  1711. static void
  1712. dasd_exit(void)
  1713. {
  1714. #ifdef CONFIG_PROC_FS
  1715. dasd_proc_exit();
  1716. #endif
  1717. dasd_eer_exit();
  1718. if (dasd_page_cache != NULL) {
  1719. kmem_cache_destroy(dasd_page_cache);
  1720. dasd_page_cache = NULL;
  1721. }
  1722. dasd_gendisk_exit();
  1723. dasd_devmap_exit();
  1724. if (dasd_debug_area != NULL) {
  1725. debug_unregister(dasd_debug_area);
  1726. dasd_debug_area = NULL;
  1727. }
  1728. }
  1729. /*
  1730. * SECTION: common functions for ccw_driver use
  1731. */
  1732. /*
  1733. * Initial attempt at a probe function. this can be simplified once
  1734. * the other detection code is gone.
  1735. */
  1736. int
  1737. dasd_generic_probe (struct ccw_device *cdev,
  1738. struct dasd_discipline *discipline)
  1739. {
  1740. int ret;
  1741. ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
  1742. if (ret) {
  1743. printk(KERN_WARNING
  1744. "dasd_generic_probe: could not set ccw-device options "
  1745. "for %s\n", cdev->dev.bus_id);
  1746. return ret;
  1747. }
  1748. ret = dasd_add_sysfs_files(cdev);
  1749. if (ret) {
  1750. printk(KERN_WARNING
  1751. "dasd_generic_probe: could not add sysfs entries "
  1752. "for %s\n", cdev->dev.bus_id);
  1753. return ret;
  1754. }
  1755. cdev->handler = &dasd_int_handler;
  1756. /*
  1757. * Automatically online either all dasd devices (dasd_autodetect)
  1758. * or all devices specified with dasd= parameters during
  1759. * initial probe.
  1760. */
  1761. if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
  1762. (dasd_autodetect && dasd_busid_known(cdev->dev.bus_id) != 0))
  1763. ret = ccw_device_set_online(cdev);
  1764. if (ret)
  1765. printk(KERN_WARNING
  1766. "dasd_generic_probe: could not initially online "
  1767. "ccw-device %s\n", cdev->dev.bus_id);
  1768. return ret;
  1769. }
  1770. /*
  1771. * This will one day be called from a global not_oper handler.
  1772. * It is also used by driver_unregister during module unload.
  1773. */
  1774. void
  1775. dasd_generic_remove (struct ccw_device *cdev)
  1776. {
  1777. struct dasd_device *device;
  1778. cdev->handler = NULL;
  1779. dasd_remove_sysfs_files(cdev);
  1780. device = dasd_device_from_cdev(cdev);
  1781. if (IS_ERR(device))
  1782. return;
  1783. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1784. /* Already doing offline processing */
  1785. dasd_put_device(device);
  1786. return;
  1787. }
  1788. /*
  1789. * This device is removed unconditionally. Set offline
  1790. * flag to prevent dasd_open from opening it while it is
  1791. * no quite down yet.
  1792. */
  1793. dasd_set_target_state(device, DASD_STATE_NEW);
  1794. /* dasd_delete_device destroys the device reference. */
  1795. dasd_delete_device(device);
  1796. }
  1797. /*
  1798. * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
  1799. * the device is detected for the first time and is supposed to be used
  1800. * or the user has started activation through sysfs.
  1801. */
  1802. int
  1803. dasd_generic_set_online (struct ccw_device *cdev,
  1804. struct dasd_discipline *base_discipline)
  1805. {
  1806. struct dasd_discipline *discipline;
  1807. struct dasd_device *device;
  1808. int rc;
  1809. /* first online clears initial online feature flag */
  1810. dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
  1811. device = dasd_create_device(cdev);
  1812. if (IS_ERR(device))
  1813. return PTR_ERR(device);
  1814. discipline = base_discipline;
  1815. if (device->features & DASD_FEATURE_USEDIAG) {
  1816. if (!dasd_diag_discipline_pointer) {
  1817. printk (KERN_WARNING
  1818. "dasd_generic couldn't online device %s "
  1819. "- discipline DIAG not available\n",
  1820. cdev->dev.bus_id);
  1821. dasd_delete_device(device);
  1822. return -ENODEV;
  1823. }
  1824. discipline = dasd_diag_discipline_pointer;
  1825. }
  1826. if (!try_module_get(base_discipline->owner)) {
  1827. dasd_delete_device(device);
  1828. return -EINVAL;
  1829. }
  1830. if (!try_module_get(discipline->owner)) {
  1831. module_put(base_discipline->owner);
  1832. dasd_delete_device(device);
  1833. return -EINVAL;
  1834. }
  1835. device->base_discipline = base_discipline;
  1836. device->discipline = discipline;
  1837. rc = discipline->check_device(device);
  1838. if (rc) {
  1839. printk (KERN_WARNING
  1840. "dasd_generic couldn't online device %s "
  1841. "with discipline %s rc=%i\n",
  1842. cdev->dev.bus_id, discipline->name, rc);
  1843. module_put(discipline->owner);
  1844. module_put(base_discipline->owner);
  1845. dasd_delete_device(device);
  1846. return rc;
  1847. }
  1848. dasd_set_target_state(device, DASD_STATE_ONLINE);
  1849. if (device->state <= DASD_STATE_KNOWN) {
  1850. printk (KERN_WARNING
  1851. "dasd_generic discipline not found for %s\n",
  1852. cdev->dev.bus_id);
  1853. rc = -ENODEV;
  1854. dasd_set_target_state(device, DASD_STATE_NEW);
  1855. dasd_delete_device(device);
  1856. } else
  1857. pr_debug("dasd_generic device %s found\n",
  1858. cdev->dev.bus_id);
  1859. /* FIXME: we have to wait for the root device but we don't want
  1860. * to wait for each single device but for all at once. */
  1861. wait_event(dasd_init_waitq, _wait_for_device(device));
  1862. dasd_put_device(device);
  1863. return rc;
  1864. }
  1865. int
  1866. dasd_generic_set_offline (struct ccw_device *cdev)
  1867. {
  1868. struct dasd_device *device;
  1869. int max_count, open_count;
  1870. device = dasd_device_from_cdev(cdev);
  1871. if (IS_ERR(device))
  1872. return PTR_ERR(device);
  1873. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1874. /* Already doing offline processing */
  1875. dasd_put_device(device);
  1876. return 0;
  1877. }
  1878. /*
  1879. * We must make sure that this device is currently not in use.
  1880. * The open_count is increased for every opener, that includes
  1881. * the blkdev_get in dasd_scan_partitions. We are only interested
  1882. * in the other openers.
  1883. */
  1884. max_count = device->bdev ? 0 : -1;
  1885. open_count = (int) atomic_read(&device->open_count);
  1886. if (open_count > max_count) {
  1887. if (open_count > 0)
  1888. printk (KERN_WARNING "Can't offline dasd device with "
  1889. "open count = %i.\n",
  1890. open_count);
  1891. else
  1892. printk (KERN_WARNING "%s",
  1893. "Can't offline dasd device due to internal "
  1894. "use\n");
  1895. clear_bit(DASD_FLAG_OFFLINE, &device->flags);
  1896. dasd_put_device(device);
  1897. return -EBUSY;
  1898. }
  1899. dasd_set_target_state(device, DASD_STATE_NEW);
  1900. /* dasd_delete_device destroys the device reference. */
  1901. dasd_delete_device(device);
  1902. return 0;
  1903. }
  1904. int
  1905. dasd_generic_notify(struct ccw_device *cdev, int event)
  1906. {
  1907. struct dasd_device *device;
  1908. struct dasd_ccw_req *cqr;
  1909. unsigned long flags;
  1910. int ret;
  1911. device = dasd_device_from_cdev(cdev);
  1912. if (IS_ERR(device))
  1913. return 0;
  1914. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1915. ret = 0;
  1916. switch (event) {
  1917. case CIO_GONE:
  1918. case CIO_NO_PATH:
  1919. /* First of all call extended error reporting. */
  1920. dasd_eer_write(device, NULL, DASD_EER_NOPATH);
  1921. if (device->state < DASD_STATE_BASIC)
  1922. break;
  1923. /* Device is active. We want to keep it. */
  1924. if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
  1925. list_for_each_entry(cqr, &device->ccw_queue, list)
  1926. if (cqr->status == DASD_CQR_IN_IO)
  1927. cqr->status = DASD_CQR_FAILED;
  1928. device->stopped |= DASD_STOPPED_DC_EIO;
  1929. } else {
  1930. list_for_each_entry(cqr, &device->ccw_queue, list)
  1931. if (cqr->status == DASD_CQR_IN_IO) {
  1932. cqr->status = DASD_CQR_QUEUED;
  1933. cqr->retries++;
  1934. }
  1935. device->stopped |= DASD_STOPPED_DC_WAIT;
  1936. dasd_set_timer(device, 0);
  1937. }
  1938. dasd_schedule_bh(device);
  1939. ret = 1;
  1940. break;
  1941. case CIO_OPER:
  1942. /* FIXME: add a sanity check. */
  1943. device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
  1944. dasd_schedule_bh(device);
  1945. ret = 1;
  1946. break;
  1947. }
  1948. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1949. dasd_put_device(device);
  1950. return ret;
  1951. }
  1952. struct dasd_ccw_req * dasd_generic_build_rdc(struct dasd_device *device,
  1953. void *rdc_buffer,
  1954. int rdc_buffer_size, char *magic)
  1955. {
  1956. struct dasd_ccw_req *cqr;
  1957. struct ccw1 *ccw;
  1958. cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
  1959. if (IS_ERR(cqr)) {
  1960. DEV_MESSAGE(KERN_WARNING, device, "%s",
  1961. "Could not allocate RDC request");
  1962. return cqr;
  1963. }
  1964. ccw = cqr->cpaddr;
  1965. ccw->cmd_code = CCW_CMD_RDC;
  1966. ccw->cda = (__u32)(addr_t)rdc_buffer;
  1967. ccw->count = rdc_buffer_size;
  1968. cqr->device = device;
  1969. cqr->expires = 10*HZ;
  1970. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  1971. cqr->retries = 2;
  1972. cqr->buildclk = get_clock();
  1973. cqr->status = DASD_CQR_FILLED;
  1974. return cqr;
  1975. }
  1976. int dasd_generic_read_dev_chars(struct dasd_device *device, char *magic,
  1977. void **rdc_buffer, int rdc_buffer_size)
  1978. {
  1979. int ret;
  1980. struct dasd_ccw_req *cqr;
  1981. cqr = dasd_generic_build_rdc(device, *rdc_buffer, rdc_buffer_size,
  1982. magic);
  1983. if (IS_ERR(cqr))
  1984. return PTR_ERR(cqr);
  1985. ret = dasd_sleep_on(cqr);
  1986. dasd_sfree_request(cqr, cqr->device);
  1987. return ret;
  1988. }
  1989. static int __init
  1990. dasd_init(void)
  1991. {
  1992. int rc;
  1993. init_waitqueue_head(&dasd_init_waitq);
  1994. init_waitqueue_head(&dasd_flush_wq);
  1995. /* register 'common' DASD debug area, used for all DBF_XXX calls */
  1996. dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
  1997. if (dasd_debug_area == NULL) {
  1998. rc = -ENOMEM;
  1999. goto failed;
  2000. }
  2001. debug_register_view(dasd_debug_area, &debug_sprintf_view);
  2002. debug_set_level(dasd_debug_area, DBF_WARNING);
  2003. DBF_EVENT(DBF_EMERG, "%s", "debug area created");
  2004. dasd_diag_discipline_pointer = NULL;
  2005. rc = dasd_devmap_init();
  2006. if (rc)
  2007. goto failed;
  2008. rc = dasd_gendisk_init();
  2009. if (rc)
  2010. goto failed;
  2011. rc = dasd_parse();
  2012. if (rc)
  2013. goto failed;
  2014. rc = dasd_eer_init();
  2015. if (rc)
  2016. goto failed;
  2017. #ifdef CONFIG_PROC_FS
  2018. rc = dasd_proc_init();
  2019. if (rc)
  2020. goto failed;
  2021. #endif
  2022. return 0;
  2023. failed:
  2024. MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
  2025. dasd_exit();
  2026. return rc;
  2027. }
  2028. module_init(dasd_init);
  2029. module_exit(dasd_exit);
  2030. EXPORT_SYMBOL(dasd_debug_area);
  2031. EXPORT_SYMBOL(dasd_diag_discipline_pointer);
  2032. EXPORT_SYMBOL(dasd_add_request_head);
  2033. EXPORT_SYMBOL(dasd_add_request_tail);
  2034. EXPORT_SYMBOL(dasd_cancel_req);
  2035. EXPORT_SYMBOL(dasd_clear_timer);
  2036. EXPORT_SYMBOL(dasd_enable_device);
  2037. EXPORT_SYMBOL(dasd_int_handler);
  2038. EXPORT_SYMBOL(dasd_kfree_request);
  2039. EXPORT_SYMBOL(dasd_kick_device);
  2040. EXPORT_SYMBOL(dasd_kmalloc_request);
  2041. EXPORT_SYMBOL(dasd_schedule_bh);
  2042. EXPORT_SYMBOL(dasd_set_target_state);
  2043. EXPORT_SYMBOL(dasd_set_timer);
  2044. EXPORT_SYMBOL(dasd_sfree_request);
  2045. EXPORT_SYMBOL(dasd_sleep_on);
  2046. EXPORT_SYMBOL(dasd_sleep_on_immediatly);
  2047. EXPORT_SYMBOL(dasd_sleep_on_interruptible);
  2048. EXPORT_SYMBOL(dasd_smalloc_request);
  2049. EXPORT_SYMBOL(dasd_start_IO);
  2050. EXPORT_SYMBOL(dasd_term_IO);
  2051. EXPORT_SYMBOL_GPL(dasd_generic_probe);
  2052. EXPORT_SYMBOL_GPL(dasd_generic_remove);
  2053. EXPORT_SYMBOL_GPL(dasd_generic_notify);
  2054. EXPORT_SYMBOL_GPL(dasd_generic_set_online);
  2055. EXPORT_SYMBOL_GPL(dasd_generic_set_offline);