dasd.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  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. * Copyright IBM Corp. 1999, 2009
  9. */
  10. #define KMSG_COMPONENT "dasd"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12. #include <linux/kmod.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ctype.h>
  16. #include <linux/major.h>
  17. #include <linux/slab.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/hdreg.h>
  20. #include <linux/async.h>
  21. #include <asm/ccwdev.h>
  22. #include <asm/ebcdic.h>
  23. #include <asm/idals.h>
  24. #include <asm/itcw.h>
  25. /* This is ugly... */
  26. #define PRINTK_HEADER "dasd:"
  27. #include "dasd_int.h"
  28. /*
  29. * SECTION: Constant definitions to be used within this file
  30. */
  31. #define DASD_CHANQ_MAX_SIZE 4
  32. /*
  33. * SECTION: exported variables of dasd.c
  34. */
  35. debug_info_t *dasd_debug_area;
  36. struct dasd_discipline *dasd_diag_discipline_pointer;
  37. void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
  38. MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
  39. MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
  40. " Copyright 2000 IBM Corporation");
  41. MODULE_SUPPORTED_DEVICE("dasd");
  42. MODULE_LICENSE("GPL");
  43. /*
  44. * SECTION: prototypes for static functions of dasd.c
  45. */
  46. static int dasd_alloc_queue(struct dasd_block *);
  47. static void dasd_setup_queue(struct dasd_block *);
  48. static void dasd_free_queue(struct dasd_block *);
  49. static void dasd_flush_request_queue(struct dasd_block *);
  50. static int dasd_flush_block_queue(struct dasd_block *);
  51. static void dasd_device_tasklet(struct dasd_device *);
  52. static void dasd_block_tasklet(struct dasd_block *);
  53. static void do_kick_device(struct work_struct *);
  54. static void do_restore_device(struct work_struct *);
  55. static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
  56. static void dasd_device_timeout(unsigned long);
  57. static void dasd_block_timeout(unsigned long);
  58. static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
  59. /*
  60. * SECTION: Operations on the device structure.
  61. */
  62. static wait_queue_head_t dasd_init_waitq;
  63. static wait_queue_head_t dasd_flush_wq;
  64. static wait_queue_head_t generic_waitq;
  65. /*
  66. * Allocate memory for a new device structure.
  67. */
  68. struct dasd_device *dasd_alloc_device(void)
  69. {
  70. struct dasd_device *device;
  71. device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
  72. if (!device)
  73. return ERR_PTR(-ENOMEM);
  74. /* Get two pages for normal block device operations. */
  75. device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
  76. if (!device->ccw_mem) {
  77. kfree(device);
  78. return ERR_PTR(-ENOMEM);
  79. }
  80. /* Get one page for error recovery. */
  81. device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
  82. if (!device->erp_mem) {
  83. free_pages((unsigned long) device->ccw_mem, 1);
  84. kfree(device);
  85. return ERR_PTR(-ENOMEM);
  86. }
  87. dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
  88. dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
  89. spin_lock_init(&device->mem_lock);
  90. atomic_set(&device->tasklet_scheduled, 0);
  91. tasklet_init(&device->tasklet,
  92. (void (*)(unsigned long)) dasd_device_tasklet,
  93. (unsigned long) device);
  94. INIT_LIST_HEAD(&device->ccw_queue);
  95. init_timer(&device->timer);
  96. device->timer.function = dasd_device_timeout;
  97. device->timer.data = (unsigned long) device;
  98. INIT_WORK(&device->kick_work, do_kick_device);
  99. INIT_WORK(&device->restore_device, do_restore_device);
  100. device->state = DASD_STATE_NEW;
  101. device->target = DASD_STATE_NEW;
  102. return device;
  103. }
  104. /*
  105. * Free memory of a device structure.
  106. */
  107. void dasd_free_device(struct dasd_device *device)
  108. {
  109. kfree(device->private);
  110. free_page((unsigned long) device->erp_mem);
  111. free_pages((unsigned long) device->ccw_mem, 1);
  112. kfree(device);
  113. }
  114. /*
  115. * Allocate memory for a new device structure.
  116. */
  117. struct dasd_block *dasd_alloc_block(void)
  118. {
  119. struct dasd_block *block;
  120. block = kzalloc(sizeof(*block), GFP_ATOMIC);
  121. if (!block)
  122. return ERR_PTR(-ENOMEM);
  123. /* open_count = 0 means device online but not in use */
  124. atomic_set(&block->open_count, -1);
  125. spin_lock_init(&block->request_queue_lock);
  126. atomic_set(&block->tasklet_scheduled, 0);
  127. tasklet_init(&block->tasklet,
  128. (void (*)(unsigned long)) dasd_block_tasklet,
  129. (unsigned long) block);
  130. INIT_LIST_HEAD(&block->ccw_queue);
  131. spin_lock_init(&block->queue_lock);
  132. init_timer(&block->timer);
  133. block->timer.function = dasd_block_timeout;
  134. block->timer.data = (unsigned long) block;
  135. return block;
  136. }
  137. /*
  138. * Free memory of a device structure.
  139. */
  140. void dasd_free_block(struct dasd_block *block)
  141. {
  142. kfree(block);
  143. }
  144. /*
  145. * Make a new device known to the system.
  146. */
  147. static int dasd_state_new_to_known(struct dasd_device *device)
  148. {
  149. int rc;
  150. /*
  151. * As long as the device is not in state DASD_STATE_NEW we want to
  152. * keep the reference count > 0.
  153. */
  154. dasd_get_device(device);
  155. if (device->block) {
  156. rc = dasd_alloc_queue(device->block);
  157. if (rc) {
  158. dasd_put_device(device);
  159. return rc;
  160. }
  161. }
  162. device->state = DASD_STATE_KNOWN;
  163. return 0;
  164. }
  165. /*
  166. * Let the system forget about a device.
  167. */
  168. static int dasd_state_known_to_new(struct dasd_device *device)
  169. {
  170. /* Disable extended error reporting for this device. */
  171. dasd_eer_disable(device);
  172. /* Forget the discipline information. */
  173. if (device->discipline) {
  174. if (device->discipline->uncheck_device)
  175. device->discipline->uncheck_device(device);
  176. module_put(device->discipline->owner);
  177. }
  178. device->discipline = NULL;
  179. if (device->base_discipline)
  180. module_put(device->base_discipline->owner);
  181. device->base_discipline = NULL;
  182. device->state = DASD_STATE_NEW;
  183. if (device->block)
  184. dasd_free_queue(device->block);
  185. /* Give up reference we took in dasd_state_new_to_known. */
  186. dasd_put_device(device);
  187. return 0;
  188. }
  189. /*
  190. * Request the irq line for the device.
  191. */
  192. static int dasd_state_known_to_basic(struct dasd_device *device)
  193. {
  194. int rc;
  195. /* Allocate and register gendisk structure. */
  196. if (device->block) {
  197. rc = dasd_gendisk_alloc(device->block);
  198. if (rc)
  199. return rc;
  200. }
  201. /* register 'device' debug area, used for all DBF_DEV_XXX calls */
  202. device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
  203. 8 * sizeof(long));
  204. debug_register_view(device->debug_area, &debug_sprintf_view);
  205. debug_set_level(device->debug_area, DBF_WARNING);
  206. DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
  207. device->state = DASD_STATE_BASIC;
  208. return 0;
  209. }
  210. /*
  211. * Release the irq line for the device. Terminate any running i/o.
  212. */
  213. static int dasd_state_basic_to_known(struct dasd_device *device)
  214. {
  215. int rc;
  216. if (device->block) {
  217. dasd_gendisk_free(device->block);
  218. dasd_block_clear_timer(device->block);
  219. }
  220. rc = dasd_flush_device_queue(device);
  221. if (rc)
  222. return rc;
  223. dasd_device_clear_timer(device);
  224. DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
  225. if (device->debug_area != NULL) {
  226. debug_unregister(device->debug_area);
  227. device->debug_area = NULL;
  228. }
  229. device->state = DASD_STATE_KNOWN;
  230. return 0;
  231. }
  232. /*
  233. * Do the initial analysis. The do_analysis function may return
  234. * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
  235. * until the discipline decides to continue the startup sequence
  236. * by calling the function dasd_change_state. The eckd disciplines
  237. * uses this to start a ccw that detects the format. The completion
  238. * interrupt for this detection ccw uses the kernel event daemon to
  239. * trigger the call to dasd_change_state. All this is done in the
  240. * discipline code, see dasd_eckd.c.
  241. * After the analysis ccw is done (do_analysis returned 0) the block
  242. * device is setup.
  243. * In case the analysis returns an error, the device setup is stopped
  244. * (a fake disk was already added to allow formatting).
  245. */
  246. static int dasd_state_basic_to_ready(struct dasd_device *device)
  247. {
  248. int rc;
  249. struct dasd_block *block;
  250. rc = 0;
  251. block = device->block;
  252. /* make disk known with correct capacity */
  253. if (block) {
  254. if (block->base->discipline->do_analysis != NULL)
  255. rc = block->base->discipline->do_analysis(block);
  256. if (rc) {
  257. if (rc != -EAGAIN)
  258. device->state = DASD_STATE_UNFMT;
  259. return rc;
  260. }
  261. dasd_setup_queue(block);
  262. set_capacity(block->gdp,
  263. block->blocks << block->s2b_shift);
  264. device->state = DASD_STATE_READY;
  265. rc = dasd_scan_partitions(block);
  266. if (rc)
  267. device->state = DASD_STATE_BASIC;
  268. } else {
  269. device->state = DASD_STATE_READY;
  270. }
  271. return rc;
  272. }
  273. /*
  274. * Remove device from block device layer. Destroy dirty buffers.
  275. * Forget format information. Check if the target level is basic
  276. * and if it is create fake disk for formatting.
  277. */
  278. static int dasd_state_ready_to_basic(struct dasd_device *device)
  279. {
  280. int rc;
  281. device->state = DASD_STATE_BASIC;
  282. if (device->block) {
  283. struct dasd_block *block = device->block;
  284. rc = dasd_flush_block_queue(block);
  285. if (rc) {
  286. device->state = DASD_STATE_READY;
  287. return rc;
  288. }
  289. dasd_destroy_partitions(block);
  290. dasd_flush_request_queue(block);
  291. block->blocks = 0;
  292. block->bp_block = 0;
  293. block->s2b_shift = 0;
  294. }
  295. return 0;
  296. }
  297. /*
  298. * Back to basic.
  299. */
  300. static int dasd_state_unfmt_to_basic(struct dasd_device *device)
  301. {
  302. device->state = DASD_STATE_BASIC;
  303. return 0;
  304. }
  305. /*
  306. * Make the device online and schedule the bottom half to start
  307. * the requeueing of requests from the linux request queue to the
  308. * ccw queue.
  309. */
  310. static int
  311. dasd_state_ready_to_online(struct dasd_device * device)
  312. {
  313. int rc;
  314. struct gendisk *disk;
  315. struct disk_part_iter piter;
  316. struct hd_struct *part;
  317. if (device->discipline->ready_to_online) {
  318. rc = device->discipline->ready_to_online(device);
  319. if (rc)
  320. return rc;
  321. }
  322. device->state = DASD_STATE_ONLINE;
  323. if (device->block) {
  324. dasd_schedule_block_bh(device->block);
  325. disk = device->block->bdev->bd_disk;
  326. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  327. while ((part = disk_part_iter_next(&piter)))
  328. kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
  329. disk_part_iter_exit(&piter);
  330. }
  331. return 0;
  332. }
  333. /*
  334. * Stop the requeueing of requests again.
  335. */
  336. static int dasd_state_online_to_ready(struct dasd_device *device)
  337. {
  338. int rc;
  339. struct gendisk *disk;
  340. struct disk_part_iter piter;
  341. struct hd_struct *part;
  342. if (device->discipline->online_to_ready) {
  343. rc = device->discipline->online_to_ready(device);
  344. if (rc)
  345. return rc;
  346. }
  347. device->state = DASD_STATE_READY;
  348. if (device->block) {
  349. disk = device->block->bdev->bd_disk;
  350. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  351. while ((part = disk_part_iter_next(&piter)))
  352. kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
  353. disk_part_iter_exit(&piter);
  354. }
  355. return 0;
  356. }
  357. /*
  358. * Device startup state changes.
  359. */
  360. static int dasd_increase_state(struct dasd_device *device)
  361. {
  362. int rc;
  363. rc = 0;
  364. if (device->state == DASD_STATE_NEW &&
  365. device->target >= DASD_STATE_KNOWN)
  366. rc = dasd_state_new_to_known(device);
  367. if (!rc &&
  368. device->state == DASD_STATE_KNOWN &&
  369. device->target >= DASD_STATE_BASIC)
  370. rc = dasd_state_known_to_basic(device);
  371. if (!rc &&
  372. device->state == DASD_STATE_BASIC &&
  373. device->target >= DASD_STATE_READY)
  374. rc = dasd_state_basic_to_ready(device);
  375. if (!rc &&
  376. device->state == DASD_STATE_UNFMT &&
  377. device->target > DASD_STATE_UNFMT)
  378. rc = -EPERM;
  379. if (!rc &&
  380. device->state == DASD_STATE_READY &&
  381. device->target >= DASD_STATE_ONLINE)
  382. rc = dasd_state_ready_to_online(device);
  383. return rc;
  384. }
  385. /*
  386. * Device shutdown state changes.
  387. */
  388. static int dasd_decrease_state(struct dasd_device *device)
  389. {
  390. int rc;
  391. rc = 0;
  392. if (device->state == DASD_STATE_ONLINE &&
  393. device->target <= DASD_STATE_READY)
  394. rc = dasd_state_online_to_ready(device);
  395. if (!rc &&
  396. device->state == DASD_STATE_READY &&
  397. device->target <= DASD_STATE_BASIC)
  398. rc = dasd_state_ready_to_basic(device);
  399. if (!rc &&
  400. device->state == DASD_STATE_UNFMT &&
  401. device->target <= DASD_STATE_BASIC)
  402. rc = dasd_state_unfmt_to_basic(device);
  403. if (!rc &&
  404. device->state == DASD_STATE_BASIC &&
  405. device->target <= DASD_STATE_KNOWN)
  406. rc = dasd_state_basic_to_known(device);
  407. if (!rc &&
  408. device->state == DASD_STATE_KNOWN &&
  409. device->target <= DASD_STATE_NEW)
  410. rc = dasd_state_known_to_new(device);
  411. return rc;
  412. }
  413. /*
  414. * This is the main startup/shutdown routine.
  415. */
  416. static void dasd_change_state(struct dasd_device *device)
  417. {
  418. int rc;
  419. if (device->state == device->target)
  420. /* Already where we want to go today... */
  421. return;
  422. if (device->state < device->target)
  423. rc = dasd_increase_state(device);
  424. else
  425. rc = dasd_decrease_state(device);
  426. if (rc == -EAGAIN)
  427. return;
  428. if (rc)
  429. device->target = device->state;
  430. if (device->state == device->target) {
  431. wake_up(&dasd_init_waitq);
  432. dasd_put_device(device);
  433. }
  434. /* let user-space know that the device status changed */
  435. kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
  436. }
  437. /*
  438. * Kick starter for devices that did not complete the startup/shutdown
  439. * procedure or were sleeping because of a pending state.
  440. * dasd_kick_device will schedule a call do do_kick_device to the kernel
  441. * event daemon.
  442. */
  443. static void do_kick_device(struct work_struct *work)
  444. {
  445. struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
  446. dasd_change_state(device);
  447. dasd_schedule_device_bh(device);
  448. dasd_put_device(device);
  449. }
  450. void dasd_kick_device(struct dasd_device *device)
  451. {
  452. dasd_get_device(device);
  453. /* queue call to dasd_kick_device to the kernel event daemon. */
  454. schedule_work(&device->kick_work);
  455. }
  456. /*
  457. * dasd_restore_device will schedule a call do do_restore_device to the kernel
  458. * event daemon.
  459. */
  460. static void do_restore_device(struct work_struct *work)
  461. {
  462. struct dasd_device *device = container_of(work, struct dasd_device,
  463. restore_device);
  464. device->cdev->drv->restore(device->cdev);
  465. dasd_put_device(device);
  466. }
  467. void dasd_restore_device(struct dasd_device *device)
  468. {
  469. dasd_get_device(device);
  470. /* queue call to dasd_restore_device to the kernel event daemon. */
  471. schedule_work(&device->restore_device);
  472. }
  473. /*
  474. * Set the target state for a device and starts the state change.
  475. */
  476. void dasd_set_target_state(struct dasd_device *device, int target)
  477. {
  478. dasd_get_device(device);
  479. /* If we are in probeonly mode stop at DASD_STATE_READY. */
  480. if (dasd_probeonly && target > DASD_STATE_READY)
  481. target = DASD_STATE_READY;
  482. if (device->target != target) {
  483. if (device->state == target) {
  484. wake_up(&dasd_init_waitq);
  485. dasd_put_device(device);
  486. }
  487. device->target = target;
  488. }
  489. if (device->state != device->target)
  490. dasd_change_state(device);
  491. }
  492. /*
  493. * Enable devices with device numbers in [from..to].
  494. */
  495. static inline int _wait_for_device(struct dasd_device *device)
  496. {
  497. return (device->state == device->target);
  498. }
  499. void dasd_enable_device(struct dasd_device *device)
  500. {
  501. dasd_set_target_state(device, DASD_STATE_ONLINE);
  502. if (device->state <= DASD_STATE_KNOWN)
  503. /* No discipline for device found. */
  504. dasd_set_target_state(device, DASD_STATE_NEW);
  505. /* Now wait for the devices to come up. */
  506. wait_event(dasd_init_waitq, _wait_for_device(device));
  507. }
  508. /*
  509. * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
  510. */
  511. #ifdef CONFIG_DASD_PROFILE
  512. struct dasd_profile_info_t dasd_global_profile;
  513. unsigned int dasd_profile_level = DASD_PROFILE_OFF;
  514. /*
  515. * Increments counter in global and local profiling structures.
  516. */
  517. #define dasd_profile_counter(value, counter, block) \
  518. { \
  519. int index; \
  520. for (index = 0; index < 31 && value >> (2+index); index++); \
  521. dasd_global_profile.counter[index]++; \
  522. block->profile.counter[index]++; \
  523. }
  524. /*
  525. * Add profiling information for cqr before execution.
  526. */
  527. static void dasd_profile_start(struct dasd_block *block,
  528. struct dasd_ccw_req *cqr,
  529. struct request *req)
  530. {
  531. struct list_head *l;
  532. unsigned int counter;
  533. if (dasd_profile_level != DASD_PROFILE_ON)
  534. return;
  535. /* count the length of the chanq for statistics */
  536. counter = 0;
  537. list_for_each(l, &block->ccw_queue)
  538. if (++counter >= 31)
  539. break;
  540. dasd_global_profile.dasd_io_nr_req[counter]++;
  541. block->profile.dasd_io_nr_req[counter]++;
  542. }
  543. /*
  544. * Add profiling information for cqr after execution.
  545. */
  546. static void dasd_profile_end(struct dasd_block *block,
  547. struct dasd_ccw_req *cqr,
  548. struct request *req)
  549. {
  550. long strtime, irqtime, endtime, tottime; /* in microseconds */
  551. long tottimeps, sectors;
  552. if (dasd_profile_level != DASD_PROFILE_ON)
  553. return;
  554. sectors = blk_rq_sectors(req);
  555. if (!cqr->buildclk || !cqr->startclk ||
  556. !cqr->stopclk || !cqr->endclk ||
  557. !sectors)
  558. return;
  559. strtime = ((cqr->startclk - cqr->buildclk) >> 12);
  560. irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
  561. endtime = ((cqr->endclk - cqr->stopclk) >> 12);
  562. tottime = ((cqr->endclk - cqr->buildclk) >> 12);
  563. tottimeps = tottime / sectors;
  564. if (!dasd_global_profile.dasd_io_reqs)
  565. memset(&dasd_global_profile, 0,
  566. sizeof(struct dasd_profile_info_t));
  567. dasd_global_profile.dasd_io_reqs++;
  568. dasd_global_profile.dasd_io_sects += sectors;
  569. if (!block->profile.dasd_io_reqs)
  570. memset(&block->profile, 0,
  571. sizeof(struct dasd_profile_info_t));
  572. block->profile.dasd_io_reqs++;
  573. block->profile.dasd_io_sects += sectors;
  574. dasd_profile_counter(sectors, dasd_io_secs, block);
  575. dasd_profile_counter(tottime, dasd_io_times, block);
  576. dasd_profile_counter(tottimeps, dasd_io_timps, block);
  577. dasd_profile_counter(strtime, dasd_io_time1, block);
  578. dasd_profile_counter(irqtime, dasd_io_time2, block);
  579. dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
  580. dasd_profile_counter(endtime, dasd_io_time3, block);
  581. }
  582. #else
  583. #define dasd_profile_start(block, cqr, req) do {} while (0)
  584. #define dasd_profile_end(block, cqr, req) do {} while (0)
  585. #endif /* CONFIG_DASD_PROFILE */
  586. /*
  587. * Allocate memory for a channel program with 'cplength' channel
  588. * command words and 'datasize' additional space. There are two
  589. * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
  590. * memory and 2) dasd_smalloc_request uses the static ccw memory
  591. * that gets allocated for each device.
  592. */
  593. struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
  594. int datasize,
  595. struct dasd_device *device)
  596. {
  597. struct dasd_ccw_req *cqr;
  598. /* Sanity checks */
  599. BUG_ON(datasize > PAGE_SIZE ||
  600. (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  601. cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
  602. if (cqr == NULL)
  603. return ERR_PTR(-ENOMEM);
  604. cqr->cpaddr = NULL;
  605. if (cplength > 0) {
  606. cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
  607. GFP_ATOMIC | GFP_DMA);
  608. if (cqr->cpaddr == NULL) {
  609. kfree(cqr);
  610. return ERR_PTR(-ENOMEM);
  611. }
  612. }
  613. cqr->data = NULL;
  614. if (datasize > 0) {
  615. cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
  616. if (cqr->data == NULL) {
  617. kfree(cqr->cpaddr);
  618. kfree(cqr);
  619. return ERR_PTR(-ENOMEM);
  620. }
  621. }
  622. cqr->magic = magic;
  623. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  624. dasd_get_device(device);
  625. return cqr;
  626. }
  627. struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
  628. int datasize,
  629. struct dasd_device *device)
  630. {
  631. unsigned long flags;
  632. struct dasd_ccw_req *cqr;
  633. char *data;
  634. int size;
  635. /* Sanity checks */
  636. BUG_ON(datasize > PAGE_SIZE ||
  637. (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  638. size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
  639. if (cplength > 0)
  640. size += cplength * sizeof(struct ccw1);
  641. if (datasize > 0)
  642. size += datasize;
  643. spin_lock_irqsave(&device->mem_lock, flags);
  644. cqr = (struct dasd_ccw_req *)
  645. dasd_alloc_chunk(&device->ccw_chunks, size);
  646. spin_unlock_irqrestore(&device->mem_lock, flags);
  647. if (cqr == NULL)
  648. return ERR_PTR(-ENOMEM);
  649. memset(cqr, 0, sizeof(struct dasd_ccw_req));
  650. data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
  651. cqr->cpaddr = NULL;
  652. if (cplength > 0) {
  653. cqr->cpaddr = (struct ccw1 *) data;
  654. data += cplength*sizeof(struct ccw1);
  655. memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
  656. }
  657. cqr->data = NULL;
  658. if (datasize > 0) {
  659. cqr->data = data;
  660. memset(cqr->data, 0, datasize);
  661. }
  662. cqr->magic = magic;
  663. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  664. dasd_get_device(device);
  665. return cqr;
  666. }
  667. /*
  668. * Free memory of a channel program. This function needs to free all the
  669. * idal lists that might have been created by dasd_set_cda and the
  670. * struct dasd_ccw_req itself.
  671. */
  672. void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
  673. {
  674. #ifdef CONFIG_64BIT
  675. struct ccw1 *ccw;
  676. /* Clear any idals used for the request. */
  677. ccw = cqr->cpaddr;
  678. do {
  679. clear_normalized_cda(ccw);
  680. } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
  681. #endif
  682. kfree(cqr->cpaddr);
  683. kfree(cqr->data);
  684. kfree(cqr);
  685. dasd_put_device(device);
  686. }
  687. void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
  688. {
  689. unsigned long flags;
  690. spin_lock_irqsave(&device->mem_lock, flags);
  691. dasd_free_chunk(&device->ccw_chunks, cqr);
  692. spin_unlock_irqrestore(&device->mem_lock, flags);
  693. dasd_put_device(device);
  694. }
  695. /*
  696. * Check discipline magic in cqr.
  697. */
  698. static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
  699. {
  700. struct dasd_device *device;
  701. if (cqr == NULL)
  702. return -EINVAL;
  703. device = cqr->startdev;
  704. if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
  705. DBF_DEV_EVENT(DBF_WARNING, device,
  706. " dasd_ccw_req 0x%08x magic doesn't match"
  707. " discipline 0x%08x",
  708. cqr->magic,
  709. *(unsigned int *) device->discipline->name);
  710. return -EINVAL;
  711. }
  712. return 0;
  713. }
  714. /*
  715. * Terminate the current i/o and set the request to clear_pending.
  716. * Timer keeps device runnig.
  717. * ccw_device_clear can fail if the i/o subsystem
  718. * is in a bad mood.
  719. */
  720. int dasd_term_IO(struct dasd_ccw_req *cqr)
  721. {
  722. struct dasd_device *device;
  723. int retries, rc;
  724. char errorstring[ERRORLENGTH];
  725. /* Check the cqr */
  726. rc = dasd_check_cqr(cqr);
  727. if (rc)
  728. return rc;
  729. retries = 0;
  730. device = (struct dasd_device *) cqr->startdev;
  731. while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
  732. rc = ccw_device_clear(device->cdev, (long) cqr);
  733. switch (rc) {
  734. case 0: /* termination successful */
  735. cqr->retries--;
  736. cqr->status = DASD_CQR_CLEAR_PENDING;
  737. cqr->stopclk = get_clock();
  738. cqr->starttime = 0;
  739. DBF_DEV_EVENT(DBF_DEBUG, device,
  740. "terminate cqr %p successful",
  741. cqr);
  742. break;
  743. case -ENODEV:
  744. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  745. "device gone, retry");
  746. break;
  747. case -EIO:
  748. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  749. "I/O error, retry");
  750. break;
  751. case -EINVAL:
  752. case -EBUSY:
  753. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  754. "device busy, retry later");
  755. break;
  756. default:
  757. /* internal error 10 - unknown rc*/
  758. snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
  759. dev_err(&device->cdev->dev, "An error occurred in the "
  760. "DASD device driver, reason=%s\n", errorstring);
  761. BUG();
  762. break;
  763. }
  764. retries++;
  765. }
  766. dasd_schedule_device_bh(device);
  767. return rc;
  768. }
  769. /*
  770. * Start the i/o. This start_IO can fail if the channel is really busy.
  771. * In that case set up a timer to start the request later.
  772. */
  773. int dasd_start_IO(struct dasd_ccw_req *cqr)
  774. {
  775. struct dasd_device *device;
  776. int rc;
  777. char errorstring[ERRORLENGTH];
  778. /* Check the cqr */
  779. rc = dasd_check_cqr(cqr);
  780. if (rc) {
  781. cqr->intrc = rc;
  782. return rc;
  783. }
  784. device = (struct dasd_device *) cqr->startdev;
  785. if (cqr->retries < 0) {
  786. /* internal error 14 - start_IO run out of retries */
  787. sprintf(errorstring, "14 %p", cqr);
  788. dev_err(&device->cdev->dev, "An error occurred in the DASD "
  789. "device driver, reason=%s\n", errorstring);
  790. cqr->status = DASD_CQR_ERROR;
  791. return -EIO;
  792. }
  793. cqr->startclk = get_clock();
  794. cqr->starttime = jiffies;
  795. cqr->retries--;
  796. if (cqr->cpmode == 1) {
  797. rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
  798. (long) cqr, cqr->lpm);
  799. } else {
  800. rc = ccw_device_start(device->cdev, cqr->cpaddr,
  801. (long) cqr, cqr->lpm, 0);
  802. }
  803. switch (rc) {
  804. case 0:
  805. cqr->status = DASD_CQR_IN_IO;
  806. break;
  807. case -EBUSY:
  808. DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
  809. "start_IO: device busy, retry later");
  810. break;
  811. case -ETIMEDOUT:
  812. DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
  813. "start_IO: request timeout, retry later");
  814. break;
  815. case -EACCES:
  816. /* -EACCES indicates that the request used only a
  817. * subset of the available pathes and all these
  818. * pathes are gone.
  819. * Do a retry with all available pathes.
  820. */
  821. cqr->lpm = LPM_ANYPATH;
  822. DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
  823. "start_IO: selected pathes gone,"
  824. " retry on all pathes");
  825. break;
  826. case -ENODEV:
  827. DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
  828. "start_IO: -ENODEV device gone, retry");
  829. break;
  830. case -EIO:
  831. DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
  832. "start_IO: -EIO device gone, retry");
  833. break;
  834. case -EINVAL:
  835. /* most likely caused in power management context */
  836. DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
  837. "start_IO: -EINVAL device currently "
  838. "not accessible");
  839. break;
  840. default:
  841. /* internal error 11 - unknown rc */
  842. snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
  843. dev_err(&device->cdev->dev,
  844. "An error occurred in the DASD device driver, "
  845. "reason=%s\n", errorstring);
  846. BUG();
  847. break;
  848. }
  849. cqr->intrc = rc;
  850. return rc;
  851. }
  852. /*
  853. * Timeout function for dasd devices. This is used for different purposes
  854. * 1) missing interrupt handler for normal operation
  855. * 2) delayed start of request where start_IO failed with -EBUSY
  856. * 3) timeout for missing state change interrupts
  857. * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
  858. * DASD_CQR_QUEUED for 2) and 3).
  859. */
  860. static void dasd_device_timeout(unsigned long ptr)
  861. {
  862. unsigned long flags;
  863. struct dasd_device *device;
  864. device = (struct dasd_device *) ptr;
  865. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  866. /* re-activate request queue */
  867. dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
  868. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  869. dasd_schedule_device_bh(device);
  870. }
  871. /*
  872. * Setup timeout for a device in jiffies.
  873. */
  874. void dasd_device_set_timer(struct dasd_device *device, int expires)
  875. {
  876. if (expires == 0)
  877. del_timer(&device->timer);
  878. else
  879. mod_timer(&device->timer, jiffies + expires);
  880. }
  881. /*
  882. * Clear timeout for a device.
  883. */
  884. void dasd_device_clear_timer(struct dasd_device *device)
  885. {
  886. del_timer(&device->timer);
  887. }
  888. static void dasd_handle_killed_request(struct ccw_device *cdev,
  889. unsigned long intparm)
  890. {
  891. struct dasd_ccw_req *cqr;
  892. struct dasd_device *device;
  893. if (!intparm)
  894. return;
  895. cqr = (struct dasd_ccw_req *) intparm;
  896. if (cqr->status != DASD_CQR_IN_IO) {
  897. DBF_EVENT_DEVID(DBF_DEBUG, cdev,
  898. "invalid status in handle_killed_request: "
  899. "%02x", cqr->status);
  900. return;
  901. }
  902. device = (struct dasd_device *) cqr->startdev;
  903. if (device == NULL ||
  904. device != dasd_device_from_cdev_locked(cdev) ||
  905. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  906. DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
  907. "invalid device in request");
  908. return;
  909. }
  910. /* Schedule request to be retried. */
  911. cqr->status = DASD_CQR_QUEUED;
  912. dasd_device_clear_timer(device);
  913. dasd_schedule_device_bh(device);
  914. dasd_put_device(device);
  915. }
  916. void dasd_generic_handle_state_change(struct dasd_device *device)
  917. {
  918. /* First of all start sense subsystem status request. */
  919. dasd_eer_snss(device);
  920. dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
  921. dasd_schedule_device_bh(device);
  922. if (device->block)
  923. dasd_schedule_block_bh(device->block);
  924. }
  925. /*
  926. * Interrupt handler for "normal" ssch-io based dasd devices.
  927. */
  928. void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
  929. struct irb *irb)
  930. {
  931. struct dasd_ccw_req *cqr, *next;
  932. struct dasd_device *device;
  933. unsigned long long now;
  934. int expires;
  935. if (IS_ERR(irb)) {
  936. switch (PTR_ERR(irb)) {
  937. case -EIO:
  938. break;
  939. case -ETIMEDOUT:
  940. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
  941. "request timed out\n", __func__);
  942. break;
  943. default:
  944. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
  945. "unknown error %ld\n", __func__,
  946. PTR_ERR(irb));
  947. }
  948. dasd_handle_killed_request(cdev, intparm);
  949. return;
  950. }
  951. now = get_clock();
  952. /* check for unsolicited interrupts */
  953. cqr = (struct dasd_ccw_req *) intparm;
  954. if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
  955. (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
  956. (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
  957. if (cqr && cqr->status == DASD_CQR_IN_IO)
  958. cqr->status = DASD_CQR_QUEUED;
  959. device = dasd_device_from_cdev_locked(cdev);
  960. if (!IS_ERR(device)) {
  961. dasd_device_clear_timer(device);
  962. device->discipline->handle_unsolicited_interrupt(device,
  963. irb);
  964. dasd_put_device(device);
  965. }
  966. return;
  967. }
  968. device = (struct dasd_device *) cqr->startdev;
  969. if (!device ||
  970. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  971. DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
  972. "invalid device in request");
  973. return;
  974. }
  975. /* Check for clear pending */
  976. if (cqr->status == DASD_CQR_CLEAR_PENDING &&
  977. scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
  978. cqr->status = DASD_CQR_CLEARED;
  979. dasd_device_clear_timer(device);
  980. wake_up(&dasd_flush_wq);
  981. dasd_schedule_device_bh(device);
  982. return;
  983. }
  984. /* check status - the request might have been killed by dyn detach */
  985. if (cqr->status != DASD_CQR_IN_IO) {
  986. DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
  987. "status %02x", dev_name(&cdev->dev), cqr->status);
  988. return;
  989. }
  990. next = NULL;
  991. expires = 0;
  992. if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  993. scsw_cstat(&irb->scsw) == 0) {
  994. /* request was completed successfully */
  995. cqr->status = DASD_CQR_SUCCESS;
  996. cqr->stopclk = now;
  997. /* Start first request on queue if possible -> fast_io. */
  998. if (cqr->devlist.next != &device->ccw_queue) {
  999. next = list_entry(cqr->devlist.next,
  1000. struct dasd_ccw_req, devlist);
  1001. }
  1002. } else { /* error */
  1003. memcpy(&cqr->irb, irb, sizeof(struct irb));
  1004. /* log sense for every failed I/O to s390 debugfeature */
  1005. dasd_log_sense_dbf(cqr, irb);
  1006. if (device->features & DASD_FEATURE_ERPLOG) {
  1007. dasd_log_sense(cqr, irb);
  1008. }
  1009. /*
  1010. * If we don't want complex ERP for this request, then just
  1011. * reset this and retry it in the fastpath
  1012. */
  1013. if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
  1014. cqr->retries > 0) {
  1015. if (cqr->lpm == LPM_ANYPATH)
  1016. DBF_DEV_EVENT(DBF_DEBUG, device,
  1017. "default ERP in fastpath "
  1018. "(%i retries left)",
  1019. cqr->retries);
  1020. cqr->lpm = LPM_ANYPATH;
  1021. cqr->status = DASD_CQR_QUEUED;
  1022. next = cqr;
  1023. } else
  1024. cqr->status = DASD_CQR_ERROR;
  1025. }
  1026. if (next && (next->status == DASD_CQR_QUEUED) &&
  1027. (!device->stopped)) {
  1028. if (device->discipline->start_IO(next) == 0)
  1029. expires = next->expires;
  1030. }
  1031. if (expires != 0)
  1032. dasd_device_set_timer(device, expires);
  1033. else
  1034. dasd_device_clear_timer(device);
  1035. dasd_schedule_device_bh(device);
  1036. }
  1037. /*
  1038. * If we have an error on a dasd_block layer request then we cancel
  1039. * and return all further requests from the same dasd_block as well.
  1040. */
  1041. static void __dasd_device_recovery(struct dasd_device *device,
  1042. struct dasd_ccw_req *ref_cqr)
  1043. {
  1044. struct list_head *l, *n;
  1045. struct dasd_ccw_req *cqr;
  1046. /*
  1047. * only requeue request that came from the dasd_block layer
  1048. */
  1049. if (!ref_cqr->block)
  1050. return;
  1051. list_for_each_safe(l, n, &device->ccw_queue) {
  1052. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1053. if (cqr->status == DASD_CQR_QUEUED &&
  1054. ref_cqr->block == cqr->block) {
  1055. cqr->status = DASD_CQR_CLEARED;
  1056. }
  1057. }
  1058. };
  1059. /*
  1060. * Remove those ccw requests from the queue that need to be returned
  1061. * to the upper layer.
  1062. */
  1063. static void __dasd_device_process_ccw_queue(struct dasd_device *device,
  1064. struct list_head *final_queue)
  1065. {
  1066. struct list_head *l, *n;
  1067. struct dasd_ccw_req *cqr;
  1068. /* Process request with final status. */
  1069. list_for_each_safe(l, n, &device->ccw_queue) {
  1070. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1071. /* Stop list processing at the first non-final request. */
  1072. if (cqr->status == DASD_CQR_QUEUED ||
  1073. cqr->status == DASD_CQR_IN_IO ||
  1074. cqr->status == DASD_CQR_CLEAR_PENDING)
  1075. break;
  1076. if (cqr->status == DASD_CQR_ERROR) {
  1077. __dasd_device_recovery(device, cqr);
  1078. }
  1079. /* Rechain finished requests to final queue */
  1080. list_move_tail(&cqr->devlist, final_queue);
  1081. }
  1082. }
  1083. /*
  1084. * the cqrs from the final queue are returned to the upper layer
  1085. * by setting a dasd_block state and calling the callback function
  1086. */
  1087. static void __dasd_device_process_final_queue(struct dasd_device *device,
  1088. struct list_head *final_queue)
  1089. {
  1090. struct list_head *l, *n;
  1091. struct dasd_ccw_req *cqr;
  1092. struct dasd_block *block;
  1093. void (*callback)(struct dasd_ccw_req *, void *data);
  1094. void *callback_data;
  1095. char errorstring[ERRORLENGTH];
  1096. list_for_each_safe(l, n, final_queue) {
  1097. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1098. list_del_init(&cqr->devlist);
  1099. block = cqr->block;
  1100. callback = cqr->callback;
  1101. callback_data = cqr->callback_data;
  1102. if (block)
  1103. spin_lock_bh(&block->queue_lock);
  1104. switch (cqr->status) {
  1105. case DASD_CQR_SUCCESS:
  1106. cqr->status = DASD_CQR_DONE;
  1107. break;
  1108. case DASD_CQR_ERROR:
  1109. cqr->status = DASD_CQR_NEED_ERP;
  1110. break;
  1111. case DASD_CQR_CLEARED:
  1112. cqr->status = DASD_CQR_TERMINATED;
  1113. break;
  1114. default:
  1115. /* internal error 12 - wrong cqr status*/
  1116. snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
  1117. dev_err(&device->cdev->dev,
  1118. "An error occurred in the DASD device driver, "
  1119. "reason=%s\n", errorstring);
  1120. BUG();
  1121. }
  1122. if (cqr->callback != NULL)
  1123. (callback)(cqr, callback_data);
  1124. if (block)
  1125. spin_unlock_bh(&block->queue_lock);
  1126. }
  1127. }
  1128. /*
  1129. * Take a look at the first request on the ccw queue and check
  1130. * if it reached its expire time. If so, terminate the IO.
  1131. */
  1132. static void __dasd_device_check_expire(struct dasd_device *device)
  1133. {
  1134. struct dasd_ccw_req *cqr;
  1135. if (list_empty(&device->ccw_queue))
  1136. return;
  1137. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1138. if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
  1139. (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
  1140. if (device->discipline->term_IO(cqr) != 0) {
  1141. /* Hmpf, try again in 5 sec */
  1142. dev_err(&device->cdev->dev,
  1143. "cqr %p timed out (%is) but cannot be "
  1144. "ended, retrying in 5 s\n",
  1145. cqr, (cqr->expires/HZ));
  1146. cqr->expires += 5*HZ;
  1147. dasd_device_set_timer(device, 5*HZ);
  1148. } else {
  1149. dev_err(&device->cdev->dev,
  1150. "cqr %p timed out (%is), %i retries "
  1151. "remaining\n", cqr, (cqr->expires/HZ),
  1152. cqr->retries);
  1153. }
  1154. }
  1155. }
  1156. /*
  1157. * Take a look at the first request on the ccw queue and check
  1158. * if it needs to be started.
  1159. */
  1160. static void __dasd_device_start_head(struct dasd_device *device)
  1161. {
  1162. struct dasd_ccw_req *cqr;
  1163. int rc;
  1164. if (list_empty(&device->ccw_queue))
  1165. return;
  1166. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1167. if (cqr->status != DASD_CQR_QUEUED)
  1168. return;
  1169. /* when device is stopped, return request to previous layer */
  1170. if (device->stopped) {
  1171. cqr->status = DASD_CQR_CLEARED;
  1172. dasd_schedule_device_bh(device);
  1173. return;
  1174. }
  1175. rc = device->discipline->start_IO(cqr);
  1176. if (rc == 0)
  1177. dasd_device_set_timer(device, cqr->expires);
  1178. else if (rc == -EACCES) {
  1179. dasd_schedule_device_bh(device);
  1180. } else
  1181. /* Hmpf, try again in 1/2 sec */
  1182. dasd_device_set_timer(device, 50);
  1183. }
  1184. /*
  1185. * Go through all request on the dasd_device request queue,
  1186. * terminate them on the cdev if necessary, and return them to the
  1187. * submitting layer via callback.
  1188. * Note:
  1189. * Make sure that all 'submitting layers' still exist when
  1190. * this function is called!. In other words, when 'device' is a base
  1191. * device then all block layer requests must have been removed before
  1192. * via dasd_flush_block_queue.
  1193. */
  1194. int dasd_flush_device_queue(struct dasd_device *device)
  1195. {
  1196. struct dasd_ccw_req *cqr, *n;
  1197. int rc;
  1198. struct list_head flush_queue;
  1199. INIT_LIST_HEAD(&flush_queue);
  1200. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1201. rc = 0;
  1202. list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
  1203. /* Check status and move request to flush_queue */
  1204. switch (cqr->status) {
  1205. case DASD_CQR_IN_IO:
  1206. rc = device->discipline->term_IO(cqr);
  1207. if (rc) {
  1208. /* unable to terminate requeust */
  1209. dev_err(&device->cdev->dev,
  1210. "Flushing the DASD request queue "
  1211. "failed for request %p\n", cqr);
  1212. /* stop flush processing */
  1213. goto finished;
  1214. }
  1215. break;
  1216. case DASD_CQR_QUEUED:
  1217. cqr->stopclk = get_clock();
  1218. cqr->status = DASD_CQR_CLEARED;
  1219. break;
  1220. default: /* no need to modify the others */
  1221. break;
  1222. }
  1223. list_move_tail(&cqr->devlist, &flush_queue);
  1224. }
  1225. finished:
  1226. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1227. /*
  1228. * After this point all requests must be in state CLEAR_PENDING,
  1229. * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
  1230. * one of the others.
  1231. */
  1232. list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
  1233. wait_event(dasd_flush_wq,
  1234. (cqr->status != DASD_CQR_CLEAR_PENDING));
  1235. /*
  1236. * Now set each request back to TERMINATED, DONE or NEED_ERP
  1237. * and call the callback function of flushed requests
  1238. */
  1239. __dasd_device_process_final_queue(device, &flush_queue);
  1240. return rc;
  1241. }
  1242. /*
  1243. * Acquire the device lock and process queues for the device.
  1244. */
  1245. static void dasd_device_tasklet(struct dasd_device *device)
  1246. {
  1247. struct list_head final_queue;
  1248. atomic_set (&device->tasklet_scheduled, 0);
  1249. INIT_LIST_HEAD(&final_queue);
  1250. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1251. /* Check expire time of first request on the ccw queue. */
  1252. __dasd_device_check_expire(device);
  1253. /* find final requests on ccw queue */
  1254. __dasd_device_process_ccw_queue(device, &final_queue);
  1255. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1256. /* Now call the callback function of requests with final status */
  1257. __dasd_device_process_final_queue(device, &final_queue);
  1258. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1259. /* Now check if the head of the ccw queue needs to be started. */
  1260. __dasd_device_start_head(device);
  1261. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1262. dasd_put_device(device);
  1263. }
  1264. /*
  1265. * Schedules a call to dasd_tasklet over the device tasklet.
  1266. */
  1267. void dasd_schedule_device_bh(struct dasd_device *device)
  1268. {
  1269. /* Protect against rescheduling. */
  1270. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  1271. return;
  1272. dasd_get_device(device);
  1273. tasklet_hi_schedule(&device->tasklet);
  1274. }
  1275. void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
  1276. {
  1277. device->stopped |= bits;
  1278. }
  1279. EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
  1280. void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
  1281. {
  1282. device->stopped &= ~bits;
  1283. if (!device->stopped)
  1284. wake_up(&generic_waitq);
  1285. }
  1286. EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
  1287. /*
  1288. * Queue a request to the head of the device ccw_queue.
  1289. * Start the I/O if possible.
  1290. */
  1291. void dasd_add_request_head(struct dasd_ccw_req *cqr)
  1292. {
  1293. struct dasd_device *device;
  1294. unsigned long flags;
  1295. device = cqr->startdev;
  1296. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1297. cqr->status = DASD_CQR_QUEUED;
  1298. list_add(&cqr->devlist, &device->ccw_queue);
  1299. /* let the bh start the request to keep them in order */
  1300. dasd_schedule_device_bh(device);
  1301. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1302. }
  1303. /*
  1304. * Queue a request to the tail of the device ccw_queue.
  1305. * Start the I/O if possible.
  1306. */
  1307. void dasd_add_request_tail(struct dasd_ccw_req *cqr)
  1308. {
  1309. struct dasd_device *device;
  1310. unsigned long flags;
  1311. device = cqr->startdev;
  1312. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1313. cqr->status = DASD_CQR_QUEUED;
  1314. list_add_tail(&cqr->devlist, &device->ccw_queue);
  1315. /* let the bh start the request to keep them in order */
  1316. dasd_schedule_device_bh(device);
  1317. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1318. }
  1319. /*
  1320. * Wakeup helper for the 'sleep_on' functions.
  1321. */
  1322. static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  1323. {
  1324. wake_up((wait_queue_head_t *) data);
  1325. }
  1326. static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
  1327. {
  1328. struct dasd_device *device;
  1329. int rc;
  1330. device = cqr->startdev;
  1331. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1332. rc = ((cqr->status == DASD_CQR_DONE ||
  1333. cqr->status == DASD_CQR_NEED_ERP ||
  1334. cqr->status == DASD_CQR_TERMINATED) &&
  1335. list_empty(&cqr->devlist));
  1336. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1337. return rc;
  1338. }
  1339. /*
  1340. * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
  1341. */
  1342. static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
  1343. {
  1344. struct dasd_device *device;
  1345. dasd_erp_fn_t erp_fn;
  1346. if (cqr->status == DASD_CQR_FILLED)
  1347. return 0;
  1348. device = cqr->startdev;
  1349. if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
  1350. if (cqr->status == DASD_CQR_TERMINATED) {
  1351. device->discipline->handle_terminated_request(cqr);
  1352. return 1;
  1353. }
  1354. if (cqr->status == DASD_CQR_NEED_ERP) {
  1355. erp_fn = device->discipline->erp_action(cqr);
  1356. erp_fn(cqr);
  1357. return 1;
  1358. }
  1359. if (cqr->status == DASD_CQR_FAILED)
  1360. dasd_log_sense(cqr, &cqr->irb);
  1361. if (cqr->refers) {
  1362. __dasd_process_erp(device, cqr);
  1363. return 1;
  1364. }
  1365. }
  1366. return 0;
  1367. }
  1368. static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
  1369. {
  1370. if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
  1371. if (cqr->refers) /* erp is not done yet */
  1372. return 1;
  1373. return ((cqr->status != DASD_CQR_DONE) &&
  1374. (cqr->status != DASD_CQR_FAILED));
  1375. } else
  1376. return (cqr->status == DASD_CQR_FILLED);
  1377. }
  1378. static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
  1379. {
  1380. struct dasd_device *device;
  1381. int rc;
  1382. struct list_head ccw_queue;
  1383. struct dasd_ccw_req *cqr;
  1384. INIT_LIST_HEAD(&ccw_queue);
  1385. maincqr->status = DASD_CQR_FILLED;
  1386. device = maincqr->startdev;
  1387. list_add(&maincqr->blocklist, &ccw_queue);
  1388. for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
  1389. cqr = list_first_entry(&ccw_queue,
  1390. struct dasd_ccw_req, blocklist)) {
  1391. if (__dasd_sleep_on_erp(cqr))
  1392. continue;
  1393. if (cqr->status != DASD_CQR_FILLED) /* could be failed */
  1394. continue;
  1395. /* Non-temporary stop condition will trigger fail fast */
  1396. if (device->stopped & ~DASD_STOPPED_PENDING &&
  1397. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1398. (!dasd_eer_enabled(device))) {
  1399. cqr->status = DASD_CQR_FAILED;
  1400. continue;
  1401. }
  1402. /* Don't try to start requests if device is stopped */
  1403. if (interruptible) {
  1404. rc = wait_event_interruptible(
  1405. generic_waitq, !(device->stopped));
  1406. if (rc == -ERESTARTSYS) {
  1407. cqr->status = DASD_CQR_FAILED;
  1408. maincqr->intrc = rc;
  1409. continue;
  1410. }
  1411. } else
  1412. wait_event(generic_waitq, !(device->stopped));
  1413. cqr->callback = dasd_wakeup_cb;
  1414. cqr->callback_data = (void *) &generic_waitq;
  1415. dasd_add_request_tail(cqr);
  1416. if (interruptible) {
  1417. rc = wait_event_interruptible(
  1418. generic_waitq, _wait_for_wakeup(cqr));
  1419. if (rc == -ERESTARTSYS) {
  1420. dasd_cancel_req(cqr);
  1421. /* wait (non-interruptible) for final status */
  1422. wait_event(generic_waitq,
  1423. _wait_for_wakeup(cqr));
  1424. cqr->status = DASD_CQR_FAILED;
  1425. maincqr->intrc = rc;
  1426. continue;
  1427. }
  1428. } else
  1429. wait_event(generic_waitq, _wait_for_wakeup(cqr));
  1430. }
  1431. maincqr->endclk = get_clock();
  1432. if ((maincqr->status != DASD_CQR_DONE) &&
  1433. (maincqr->intrc != -ERESTARTSYS))
  1434. dasd_log_sense(maincqr, &maincqr->irb);
  1435. if (maincqr->status == DASD_CQR_DONE)
  1436. rc = 0;
  1437. else if (maincqr->intrc)
  1438. rc = maincqr->intrc;
  1439. else
  1440. rc = -EIO;
  1441. return rc;
  1442. }
  1443. /*
  1444. * Queue a request to the tail of the device ccw_queue and wait for
  1445. * it's completion.
  1446. */
  1447. int dasd_sleep_on(struct dasd_ccw_req *cqr)
  1448. {
  1449. return _dasd_sleep_on(cqr, 0);
  1450. }
  1451. /*
  1452. * Queue a request to the tail of the device ccw_queue and wait
  1453. * interruptible for it's completion.
  1454. */
  1455. int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
  1456. {
  1457. return _dasd_sleep_on(cqr, 1);
  1458. }
  1459. /*
  1460. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  1461. * for eckd devices) the currently running request has to be terminated
  1462. * and be put back to status queued, before the special request is added
  1463. * to the head of the queue. Then the special request is waited on normally.
  1464. */
  1465. static inline int _dasd_term_running_cqr(struct dasd_device *device)
  1466. {
  1467. struct dasd_ccw_req *cqr;
  1468. if (list_empty(&device->ccw_queue))
  1469. return 0;
  1470. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1471. return device->discipline->term_IO(cqr);
  1472. }
  1473. int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
  1474. {
  1475. struct dasd_device *device;
  1476. int rc;
  1477. device = cqr->startdev;
  1478. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1479. rc = _dasd_term_running_cqr(device);
  1480. if (rc) {
  1481. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1482. return rc;
  1483. }
  1484. cqr->callback = dasd_wakeup_cb;
  1485. cqr->callback_data = (void *) &generic_waitq;
  1486. cqr->status = DASD_CQR_QUEUED;
  1487. list_add(&cqr->devlist, &device->ccw_queue);
  1488. /* let the bh start the request to keep them in order */
  1489. dasd_schedule_device_bh(device);
  1490. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1491. wait_event(generic_waitq, _wait_for_wakeup(cqr));
  1492. if (cqr->status == DASD_CQR_DONE)
  1493. rc = 0;
  1494. else if (cqr->intrc)
  1495. rc = cqr->intrc;
  1496. else
  1497. rc = -EIO;
  1498. return rc;
  1499. }
  1500. /*
  1501. * Cancels a request that was started with dasd_sleep_on_req.
  1502. * This is useful to timeout requests. The request will be
  1503. * terminated if it is currently in i/o.
  1504. * Returns 1 if the request has been terminated.
  1505. * 0 if there was no need to terminate the request (not started yet)
  1506. * negative error code if termination failed
  1507. * Cancellation of a request is an asynchronous operation! The calling
  1508. * function has to wait until the request is properly returned via callback.
  1509. */
  1510. int dasd_cancel_req(struct dasd_ccw_req *cqr)
  1511. {
  1512. struct dasd_device *device = cqr->startdev;
  1513. unsigned long flags;
  1514. int rc;
  1515. rc = 0;
  1516. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1517. switch (cqr->status) {
  1518. case DASD_CQR_QUEUED:
  1519. /* request was not started - just set to cleared */
  1520. cqr->status = DASD_CQR_CLEARED;
  1521. break;
  1522. case DASD_CQR_IN_IO:
  1523. /* request in IO - terminate IO and release again */
  1524. rc = device->discipline->term_IO(cqr);
  1525. if (rc) {
  1526. dev_err(&device->cdev->dev,
  1527. "Cancelling request %p failed with rc=%d\n",
  1528. cqr, rc);
  1529. } else {
  1530. cqr->stopclk = get_clock();
  1531. rc = 1;
  1532. }
  1533. break;
  1534. default: /* already finished or clear pending - do nothing */
  1535. break;
  1536. }
  1537. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1538. dasd_schedule_device_bh(device);
  1539. return rc;
  1540. }
  1541. /*
  1542. * SECTION: Operations of the dasd_block layer.
  1543. */
  1544. /*
  1545. * Timeout function for dasd_block. This is used when the block layer
  1546. * is waiting for something that may not come reliably, (e.g. a state
  1547. * change interrupt)
  1548. */
  1549. static void dasd_block_timeout(unsigned long ptr)
  1550. {
  1551. unsigned long flags;
  1552. struct dasd_block *block;
  1553. block = (struct dasd_block *) ptr;
  1554. spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
  1555. /* re-activate request queue */
  1556. dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
  1557. spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
  1558. dasd_schedule_block_bh(block);
  1559. }
  1560. /*
  1561. * Setup timeout for a dasd_block in jiffies.
  1562. */
  1563. void dasd_block_set_timer(struct dasd_block *block, int expires)
  1564. {
  1565. if (expires == 0)
  1566. del_timer(&block->timer);
  1567. else
  1568. mod_timer(&block->timer, jiffies + expires);
  1569. }
  1570. /*
  1571. * Clear timeout for a dasd_block.
  1572. */
  1573. void dasd_block_clear_timer(struct dasd_block *block)
  1574. {
  1575. del_timer(&block->timer);
  1576. }
  1577. /*
  1578. * Process finished error recovery ccw.
  1579. */
  1580. static void __dasd_process_erp(struct dasd_device *device,
  1581. struct dasd_ccw_req *cqr)
  1582. {
  1583. dasd_erp_fn_t erp_fn;
  1584. if (cqr->status == DASD_CQR_DONE)
  1585. DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
  1586. else
  1587. dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
  1588. erp_fn = device->discipline->erp_postaction(cqr);
  1589. erp_fn(cqr);
  1590. }
  1591. /*
  1592. * Fetch requests from the block device queue.
  1593. */
  1594. static void __dasd_process_request_queue(struct dasd_block *block)
  1595. {
  1596. struct request_queue *queue;
  1597. struct request *req;
  1598. struct dasd_ccw_req *cqr;
  1599. struct dasd_device *basedev;
  1600. unsigned long flags;
  1601. queue = block->request_queue;
  1602. basedev = block->base;
  1603. /* No queue ? Then there is nothing to do. */
  1604. if (queue == NULL)
  1605. return;
  1606. /*
  1607. * We requeue request from the block device queue to the ccw
  1608. * queue only in two states. In state DASD_STATE_READY the
  1609. * partition detection is done and we need to requeue requests
  1610. * for that. State DASD_STATE_ONLINE is normal block device
  1611. * operation.
  1612. */
  1613. if (basedev->state < DASD_STATE_READY) {
  1614. while ((req = blk_fetch_request(block->request_queue)))
  1615. __blk_end_request_all(req, -EIO);
  1616. return;
  1617. }
  1618. /* Now we try to fetch requests from the request queue */
  1619. while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
  1620. if (basedev->features & DASD_FEATURE_READONLY &&
  1621. rq_data_dir(req) == WRITE) {
  1622. DBF_DEV_EVENT(DBF_ERR, basedev,
  1623. "Rejecting write request %p",
  1624. req);
  1625. blk_start_request(req);
  1626. __blk_end_request_all(req, -EIO);
  1627. continue;
  1628. }
  1629. cqr = basedev->discipline->build_cp(basedev, block, req);
  1630. if (IS_ERR(cqr)) {
  1631. if (PTR_ERR(cqr) == -EBUSY)
  1632. break; /* normal end condition */
  1633. if (PTR_ERR(cqr) == -ENOMEM)
  1634. break; /* terminate request queue loop */
  1635. if (PTR_ERR(cqr) == -EAGAIN) {
  1636. /*
  1637. * The current request cannot be build right
  1638. * now, we have to try later. If this request
  1639. * is the head-of-queue we stop the device
  1640. * for 1/2 second.
  1641. */
  1642. if (!list_empty(&block->ccw_queue))
  1643. break;
  1644. spin_lock_irqsave(
  1645. get_ccwdev_lock(basedev->cdev), flags);
  1646. dasd_device_set_stop_bits(basedev,
  1647. DASD_STOPPED_PENDING);
  1648. spin_unlock_irqrestore(
  1649. get_ccwdev_lock(basedev->cdev), flags);
  1650. dasd_block_set_timer(block, HZ/2);
  1651. break;
  1652. }
  1653. DBF_DEV_EVENT(DBF_ERR, basedev,
  1654. "CCW creation failed (rc=%ld) "
  1655. "on request %p",
  1656. PTR_ERR(cqr), req);
  1657. blk_start_request(req);
  1658. __blk_end_request_all(req, -EIO);
  1659. continue;
  1660. }
  1661. /*
  1662. * Note: callback is set to dasd_return_cqr_cb in
  1663. * __dasd_block_start_head to cover erp requests as well
  1664. */
  1665. cqr->callback_data = (void *) req;
  1666. cqr->status = DASD_CQR_FILLED;
  1667. blk_start_request(req);
  1668. list_add_tail(&cqr->blocklist, &block->ccw_queue);
  1669. dasd_profile_start(block, cqr, req);
  1670. }
  1671. }
  1672. static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
  1673. {
  1674. struct request *req;
  1675. int status;
  1676. int error = 0;
  1677. req = (struct request *) cqr->callback_data;
  1678. dasd_profile_end(cqr->block, cqr, req);
  1679. status = cqr->block->base->discipline->free_cp(cqr, req);
  1680. if (status <= 0)
  1681. error = status ? status : -EIO;
  1682. __blk_end_request_all(req, error);
  1683. }
  1684. /*
  1685. * Process ccw request queue.
  1686. */
  1687. static void __dasd_process_block_ccw_queue(struct dasd_block *block,
  1688. struct list_head *final_queue)
  1689. {
  1690. struct list_head *l, *n;
  1691. struct dasd_ccw_req *cqr;
  1692. dasd_erp_fn_t erp_fn;
  1693. unsigned long flags;
  1694. struct dasd_device *base = block->base;
  1695. restart:
  1696. /* Process request with final status. */
  1697. list_for_each_safe(l, n, &block->ccw_queue) {
  1698. cqr = list_entry(l, struct dasd_ccw_req, blocklist);
  1699. if (cqr->status != DASD_CQR_DONE &&
  1700. cqr->status != DASD_CQR_FAILED &&
  1701. cqr->status != DASD_CQR_NEED_ERP &&
  1702. cqr->status != DASD_CQR_TERMINATED)
  1703. continue;
  1704. if (cqr->status == DASD_CQR_TERMINATED) {
  1705. base->discipline->handle_terminated_request(cqr);
  1706. goto restart;
  1707. }
  1708. /* Process requests that may be recovered */
  1709. if (cqr->status == DASD_CQR_NEED_ERP) {
  1710. erp_fn = base->discipline->erp_action(cqr);
  1711. erp_fn(cqr);
  1712. goto restart;
  1713. }
  1714. /* log sense for fatal error */
  1715. if (cqr->status == DASD_CQR_FAILED) {
  1716. dasd_log_sense(cqr, &cqr->irb);
  1717. }
  1718. /* First of all call extended error reporting. */
  1719. if (dasd_eer_enabled(base) &&
  1720. cqr->status == DASD_CQR_FAILED) {
  1721. dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
  1722. /* restart request */
  1723. cqr->status = DASD_CQR_FILLED;
  1724. cqr->retries = 255;
  1725. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  1726. dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
  1727. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
  1728. flags);
  1729. goto restart;
  1730. }
  1731. /* Process finished ERP request. */
  1732. if (cqr->refers) {
  1733. __dasd_process_erp(base, cqr);
  1734. goto restart;
  1735. }
  1736. /* Rechain finished requests to final queue */
  1737. cqr->endclk = get_clock();
  1738. list_move_tail(&cqr->blocklist, final_queue);
  1739. }
  1740. }
  1741. static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
  1742. {
  1743. dasd_schedule_block_bh(cqr->block);
  1744. }
  1745. static void __dasd_block_start_head(struct dasd_block *block)
  1746. {
  1747. struct dasd_ccw_req *cqr;
  1748. if (list_empty(&block->ccw_queue))
  1749. return;
  1750. /* We allways begin with the first requests on the queue, as some
  1751. * of previously started requests have to be enqueued on a
  1752. * dasd_device again for error recovery.
  1753. */
  1754. list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
  1755. if (cqr->status != DASD_CQR_FILLED)
  1756. continue;
  1757. /* Non-temporary stop condition will trigger fail fast */
  1758. if (block->base->stopped & ~DASD_STOPPED_PENDING &&
  1759. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1760. (!dasd_eer_enabled(block->base))) {
  1761. cqr->status = DASD_CQR_FAILED;
  1762. dasd_schedule_block_bh(block);
  1763. continue;
  1764. }
  1765. /* Don't try to start requests if device is stopped */
  1766. if (block->base->stopped)
  1767. return;
  1768. /* just a fail safe check, should not happen */
  1769. if (!cqr->startdev)
  1770. cqr->startdev = block->base;
  1771. /* make sure that the requests we submit find their way back */
  1772. cqr->callback = dasd_return_cqr_cb;
  1773. dasd_add_request_tail(cqr);
  1774. }
  1775. }
  1776. /*
  1777. * Central dasd_block layer routine. Takes requests from the generic
  1778. * block layer request queue, creates ccw requests, enqueues them on
  1779. * a dasd_device and processes ccw requests that have been returned.
  1780. */
  1781. static void dasd_block_tasklet(struct dasd_block *block)
  1782. {
  1783. struct list_head final_queue;
  1784. struct list_head *l, *n;
  1785. struct dasd_ccw_req *cqr;
  1786. atomic_set(&block->tasklet_scheduled, 0);
  1787. INIT_LIST_HEAD(&final_queue);
  1788. spin_lock(&block->queue_lock);
  1789. /* Finish off requests on ccw queue */
  1790. __dasd_process_block_ccw_queue(block, &final_queue);
  1791. spin_unlock(&block->queue_lock);
  1792. /* Now call the callback function of requests with final status */
  1793. spin_lock_irq(&block->request_queue_lock);
  1794. list_for_each_safe(l, n, &final_queue) {
  1795. cqr = list_entry(l, struct dasd_ccw_req, blocklist);
  1796. list_del_init(&cqr->blocklist);
  1797. __dasd_cleanup_cqr(cqr);
  1798. }
  1799. spin_lock(&block->queue_lock);
  1800. /* Get new request from the block device request queue */
  1801. __dasd_process_request_queue(block);
  1802. /* Now check if the head of the ccw queue needs to be started. */
  1803. __dasd_block_start_head(block);
  1804. spin_unlock(&block->queue_lock);
  1805. spin_unlock_irq(&block->request_queue_lock);
  1806. dasd_put_device(block->base);
  1807. }
  1808. static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
  1809. {
  1810. wake_up(&dasd_flush_wq);
  1811. }
  1812. /*
  1813. * Go through all request on the dasd_block request queue, cancel them
  1814. * on the respective dasd_device, and return them to the generic
  1815. * block layer.
  1816. */
  1817. static int dasd_flush_block_queue(struct dasd_block *block)
  1818. {
  1819. struct dasd_ccw_req *cqr, *n;
  1820. int rc, i;
  1821. struct list_head flush_queue;
  1822. INIT_LIST_HEAD(&flush_queue);
  1823. spin_lock_bh(&block->queue_lock);
  1824. rc = 0;
  1825. restart:
  1826. list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
  1827. /* if this request currently owned by a dasd_device cancel it */
  1828. if (cqr->status >= DASD_CQR_QUEUED)
  1829. rc = dasd_cancel_req(cqr);
  1830. if (rc < 0)
  1831. break;
  1832. /* Rechain request (including erp chain) so it won't be
  1833. * touched by the dasd_block_tasklet anymore.
  1834. * Replace the callback so we notice when the request
  1835. * is returned from the dasd_device layer.
  1836. */
  1837. cqr->callback = _dasd_wake_block_flush_cb;
  1838. for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
  1839. list_move_tail(&cqr->blocklist, &flush_queue);
  1840. if (i > 1)
  1841. /* moved more than one request - need to restart */
  1842. goto restart;
  1843. }
  1844. spin_unlock_bh(&block->queue_lock);
  1845. /* Now call the callback function of flushed requests */
  1846. restart_cb:
  1847. list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
  1848. wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
  1849. /* Process finished ERP request. */
  1850. if (cqr->refers) {
  1851. spin_lock_bh(&block->queue_lock);
  1852. __dasd_process_erp(block->base, cqr);
  1853. spin_unlock_bh(&block->queue_lock);
  1854. /* restart list_for_xx loop since dasd_process_erp
  1855. * might remove multiple elements */
  1856. goto restart_cb;
  1857. }
  1858. /* call the callback function */
  1859. spin_lock_irq(&block->request_queue_lock);
  1860. cqr->endclk = get_clock();
  1861. list_del_init(&cqr->blocklist);
  1862. __dasd_cleanup_cqr(cqr);
  1863. spin_unlock_irq(&block->request_queue_lock);
  1864. }
  1865. return rc;
  1866. }
  1867. /*
  1868. * Schedules a call to dasd_tasklet over the device tasklet.
  1869. */
  1870. void dasd_schedule_block_bh(struct dasd_block *block)
  1871. {
  1872. /* Protect against rescheduling. */
  1873. if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
  1874. return;
  1875. /* life cycle of block is bound to it's base device */
  1876. dasd_get_device(block->base);
  1877. tasklet_hi_schedule(&block->tasklet);
  1878. }
  1879. /*
  1880. * SECTION: external block device operations
  1881. * (request queue handling, open, release, etc.)
  1882. */
  1883. /*
  1884. * Dasd request queue function. Called from ll_rw_blk.c
  1885. */
  1886. static void do_dasd_request(struct request_queue *queue)
  1887. {
  1888. struct dasd_block *block;
  1889. block = queue->queuedata;
  1890. spin_lock(&block->queue_lock);
  1891. /* Get new request from the block device request queue */
  1892. __dasd_process_request_queue(block);
  1893. /* Now check if the head of the ccw queue needs to be started. */
  1894. __dasd_block_start_head(block);
  1895. spin_unlock(&block->queue_lock);
  1896. }
  1897. /*
  1898. * Allocate and initialize request queue and default I/O scheduler.
  1899. */
  1900. static int dasd_alloc_queue(struct dasd_block *block)
  1901. {
  1902. int rc;
  1903. block->request_queue = blk_init_queue(do_dasd_request,
  1904. &block->request_queue_lock);
  1905. if (block->request_queue == NULL)
  1906. return -ENOMEM;
  1907. block->request_queue->queuedata = block;
  1908. elevator_exit(block->request_queue->elevator);
  1909. block->request_queue->elevator = NULL;
  1910. rc = elevator_init(block->request_queue, "deadline");
  1911. if (rc) {
  1912. blk_cleanup_queue(block->request_queue);
  1913. return rc;
  1914. }
  1915. return 0;
  1916. }
  1917. /*
  1918. * Allocate and initialize request queue.
  1919. */
  1920. static void dasd_setup_queue(struct dasd_block *block)
  1921. {
  1922. int max;
  1923. blk_queue_logical_block_size(block->request_queue, block->bp_block);
  1924. max = block->base->discipline->max_blocks << block->s2b_shift;
  1925. blk_queue_max_hw_sectors(block->request_queue, max);
  1926. blk_queue_max_phys_segments(block->request_queue, -1L);
  1927. blk_queue_max_hw_segments(block->request_queue, -1L);
  1928. /* with page sized segments we can translate each segement into
  1929. * one idaw/tidaw
  1930. */
  1931. blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
  1932. blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
  1933. blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
  1934. }
  1935. /*
  1936. * Deactivate and free request queue.
  1937. */
  1938. static void dasd_free_queue(struct dasd_block *block)
  1939. {
  1940. if (block->request_queue) {
  1941. blk_cleanup_queue(block->request_queue);
  1942. block->request_queue = NULL;
  1943. }
  1944. }
  1945. /*
  1946. * Flush request on the request queue.
  1947. */
  1948. static void dasd_flush_request_queue(struct dasd_block *block)
  1949. {
  1950. struct request *req;
  1951. if (!block->request_queue)
  1952. return;
  1953. spin_lock_irq(&block->request_queue_lock);
  1954. while ((req = blk_fetch_request(block->request_queue)))
  1955. __blk_end_request_all(req, -EIO);
  1956. spin_unlock_irq(&block->request_queue_lock);
  1957. }
  1958. static int dasd_open(struct block_device *bdev, fmode_t mode)
  1959. {
  1960. struct dasd_block *block = bdev->bd_disk->private_data;
  1961. struct dasd_device *base = block->base;
  1962. int rc;
  1963. atomic_inc(&block->open_count);
  1964. if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
  1965. rc = -ENODEV;
  1966. goto unlock;
  1967. }
  1968. if (!try_module_get(base->discipline->owner)) {
  1969. rc = -EINVAL;
  1970. goto unlock;
  1971. }
  1972. if (dasd_probeonly) {
  1973. dev_info(&base->cdev->dev,
  1974. "Accessing the DASD failed because it is in "
  1975. "probeonly mode\n");
  1976. rc = -EPERM;
  1977. goto out;
  1978. }
  1979. if (base->state <= DASD_STATE_BASIC) {
  1980. DBF_DEV_EVENT(DBF_ERR, base, " %s",
  1981. " Cannot open unrecognized device");
  1982. rc = -ENODEV;
  1983. goto out;
  1984. }
  1985. return 0;
  1986. out:
  1987. module_put(base->discipline->owner);
  1988. unlock:
  1989. atomic_dec(&block->open_count);
  1990. return rc;
  1991. }
  1992. static int dasd_release(struct gendisk *disk, fmode_t mode)
  1993. {
  1994. struct dasd_block *block = disk->private_data;
  1995. atomic_dec(&block->open_count);
  1996. module_put(block->base->discipline->owner);
  1997. return 0;
  1998. }
  1999. /*
  2000. * Return disk geometry.
  2001. */
  2002. static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  2003. {
  2004. struct dasd_block *block;
  2005. struct dasd_device *base;
  2006. block = bdev->bd_disk->private_data;
  2007. if (!block)
  2008. return -ENODEV;
  2009. base = block->base;
  2010. if (!base->discipline ||
  2011. !base->discipline->fill_geometry)
  2012. return -EINVAL;
  2013. base->discipline->fill_geometry(block, geo);
  2014. geo->start = get_start_sect(bdev) >> block->s2b_shift;
  2015. return 0;
  2016. }
  2017. const struct block_device_operations
  2018. dasd_device_operations = {
  2019. .owner = THIS_MODULE,
  2020. .open = dasd_open,
  2021. .release = dasd_release,
  2022. .ioctl = dasd_ioctl,
  2023. .compat_ioctl = dasd_ioctl,
  2024. .getgeo = dasd_getgeo,
  2025. };
  2026. /*******************************************************************************
  2027. * end of block device operations
  2028. */
  2029. static void
  2030. dasd_exit(void)
  2031. {
  2032. #ifdef CONFIG_PROC_FS
  2033. dasd_proc_exit();
  2034. #endif
  2035. dasd_eer_exit();
  2036. if (dasd_page_cache != NULL) {
  2037. kmem_cache_destroy(dasd_page_cache);
  2038. dasd_page_cache = NULL;
  2039. }
  2040. dasd_gendisk_exit();
  2041. dasd_devmap_exit();
  2042. if (dasd_debug_area != NULL) {
  2043. debug_unregister(dasd_debug_area);
  2044. dasd_debug_area = NULL;
  2045. }
  2046. }
  2047. /*
  2048. * SECTION: common functions for ccw_driver use
  2049. */
  2050. static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
  2051. {
  2052. struct ccw_device *cdev = data;
  2053. int ret;
  2054. ret = ccw_device_set_online(cdev);
  2055. if (ret)
  2056. pr_warning("%s: Setting the DASD online failed with rc=%d\n",
  2057. dev_name(&cdev->dev), ret);
  2058. else {
  2059. struct dasd_device *device = dasd_device_from_cdev(cdev);
  2060. wait_event(dasd_init_waitq, _wait_for_device(device));
  2061. dasd_put_device(device);
  2062. }
  2063. }
  2064. /*
  2065. * Initial attempt at a probe function. this can be simplified once
  2066. * the other detection code is gone.
  2067. */
  2068. int dasd_generic_probe(struct ccw_device *cdev,
  2069. struct dasd_discipline *discipline)
  2070. {
  2071. int ret;
  2072. ret = dasd_add_sysfs_files(cdev);
  2073. if (ret) {
  2074. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
  2075. "dasd_generic_probe: could not add "
  2076. "sysfs entries");
  2077. return ret;
  2078. }
  2079. cdev->handler = &dasd_int_handler;
  2080. /*
  2081. * Automatically online either all dasd devices (dasd_autodetect)
  2082. * or all devices specified with dasd= parameters during
  2083. * initial probe.
  2084. */
  2085. if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
  2086. (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
  2087. async_schedule(dasd_generic_auto_online, cdev);
  2088. return 0;
  2089. }
  2090. /*
  2091. * This will one day be called from a global not_oper handler.
  2092. * It is also used by driver_unregister during module unload.
  2093. */
  2094. void dasd_generic_remove(struct ccw_device *cdev)
  2095. {
  2096. struct dasd_device *device;
  2097. struct dasd_block *block;
  2098. cdev->handler = NULL;
  2099. dasd_remove_sysfs_files(cdev);
  2100. device = dasd_device_from_cdev(cdev);
  2101. if (IS_ERR(device))
  2102. return;
  2103. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  2104. /* Already doing offline processing */
  2105. dasd_put_device(device);
  2106. return;
  2107. }
  2108. /*
  2109. * This device is removed unconditionally. Set offline
  2110. * flag to prevent dasd_open from opening it while it is
  2111. * no quite down yet.
  2112. */
  2113. dasd_set_target_state(device, DASD_STATE_NEW);
  2114. /* dasd_delete_device destroys the device reference. */
  2115. block = device->block;
  2116. device->block = NULL;
  2117. dasd_delete_device(device);
  2118. /*
  2119. * life cycle of block is bound to device, so delete it after
  2120. * device was safely removed
  2121. */
  2122. if (block)
  2123. dasd_free_block(block);
  2124. }
  2125. /*
  2126. * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
  2127. * the device is detected for the first time and is supposed to be used
  2128. * or the user has started activation through sysfs.
  2129. */
  2130. int dasd_generic_set_online(struct ccw_device *cdev,
  2131. struct dasd_discipline *base_discipline)
  2132. {
  2133. struct dasd_discipline *discipline;
  2134. struct dasd_device *device;
  2135. int rc;
  2136. /* first online clears initial online feature flag */
  2137. dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
  2138. device = dasd_create_device(cdev);
  2139. if (IS_ERR(device))
  2140. return PTR_ERR(device);
  2141. discipline = base_discipline;
  2142. if (device->features & DASD_FEATURE_USEDIAG) {
  2143. if (!dasd_diag_discipline_pointer) {
  2144. pr_warning("%s Setting the DASD online failed because "
  2145. "of missing DIAG discipline\n",
  2146. dev_name(&cdev->dev));
  2147. dasd_delete_device(device);
  2148. return -ENODEV;
  2149. }
  2150. discipline = dasd_diag_discipline_pointer;
  2151. }
  2152. if (!try_module_get(base_discipline->owner)) {
  2153. dasd_delete_device(device);
  2154. return -EINVAL;
  2155. }
  2156. if (!try_module_get(discipline->owner)) {
  2157. module_put(base_discipline->owner);
  2158. dasd_delete_device(device);
  2159. return -EINVAL;
  2160. }
  2161. device->base_discipline = base_discipline;
  2162. device->discipline = discipline;
  2163. /* check_device will allocate block device if necessary */
  2164. rc = discipline->check_device(device);
  2165. if (rc) {
  2166. pr_warning("%s Setting the DASD online with discipline %s "
  2167. "failed with rc=%i\n",
  2168. dev_name(&cdev->dev), discipline->name, rc);
  2169. module_put(discipline->owner);
  2170. module_put(base_discipline->owner);
  2171. dasd_delete_device(device);
  2172. return rc;
  2173. }
  2174. dasd_set_target_state(device, DASD_STATE_ONLINE);
  2175. if (device->state <= DASD_STATE_KNOWN) {
  2176. pr_warning("%s Setting the DASD online failed because of a "
  2177. "missing discipline\n", dev_name(&cdev->dev));
  2178. rc = -ENODEV;
  2179. dasd_set_target_state(device, DASD_STATE_NEW);
  2180. if (device->block)
  2181. dasd_free_block(device->block);
  2182. dasd_delete_device(device);
  2183. } else
  2184. pr_debug("dasd_generic device %s found\n",
  2185. dev_name(&cdev->dev));
  2186. dasd_put_device(device);
  2187. return rc;
  2188. }
  2189. int dasd_generic_set_offline(struct ccw_device *cdev)
  2190. {
  2191. struct dasd_device *device;
  2192. struct dasd_block *block;
  2193. int max_count, open_count;
  2194. device = dasd_device_from_cdev(cdev);
  2195. if (IS_ERR(device))
  2196. return PTR_ERR(device);
  2197. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  2198. /* Already doing offline processing */
  2199. dasd_put_device(device);
  2200. return 0;
  2201. }
  2202. /*
  2203. * We must make sure that this device is currently not in use.
  2204. * The open_count is increased for every opener, that includes
  2205. * the blkdev_get in dasd_scan_partitions. We are only interested
  2206. * in the other openers.
  2207. */
  2208. if (device->block) {
  2209. max_count = device->block->bdev ? 0 : -1;
  2210. open_count = atomic_read(&device->block->open_count);
  2211. if (open_count > max_count) {
  2212. if (open_count > 0)
  2213. pr_warning("%s: The DASD cannot be set offline "
  2214. "with open count %i\n",
  2215. dev_name(&cdev->dev), open_count);
  2216. else
  2217. pr_warning("%s: The DASD cannot be set offline "
  2218. "while it is in use\n",
  2219. dev_name(&cdev->dev));
  2220. clear_bit(DASD_FLAG_OFFLINE, &device->flags);
  2221. dasd_put_device(device);
  2222. return -EBUSY;
  2223. }
  2224. }
  2225. dasd_set_target_state(device, DASD_STATE_NEW);
  2226. /* dasd_delete_device destroys the device reference. */
  2227. block = device->block;
  2228. device->block = NULL;
  2229. dasd_delete_device(device);
  2230. /*
  2231. * life cycle of block is bound to device, so delete it after
  2232. * device was safely removed
  2233. */
  2234. if (block)
  2235. dasd_free_block(block);
  2236. return 0;
  2237. }
  2238. int dasd_generic_notify(struct ccw_device *cdev, int event)
  2239. {
  2240. struct dasd_device *device;
  2241. struct dasd_ccw_req *cqr;
  2242. int ret;
  2243. device = dasd_device_from_cdev_locked(cdev);
  2244. if (IS_ERR(device))
  2245. return 0;
  2246. ret = 0;
  2247. switch (event) {
  2248. case CIO_GONE:
  2249. case CIO_BOXED:
  2250. case CIO_NO_PATH:
  2251. /* First of all call extended error reporting. */
  2252. dasd_eer_write(device, NULL, DASD_EER_NOPATH);
  2253. if (device->state < DASD_STATE_BASIC)
  2254. break;
  2255. /* Device is active. We want to keep it. */
  2256. list_for_each_entry(cqr, &device->ccw_queue, devlist)
  2257. if (cqr->status == DASD_CQR_IN_IO) {
  2258. cqr->status = DASD_CQR_QUEUED;
  2259. cqr->retries++;
  2260. }
  2261. dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
  2262. dasd_device_clear_timer(device);
  2263. dasd_schedule_device_bh(device);
  2264. ret = 1;
  2265. break;
  2266. case CIO_OPER:
  2267. /* FIXME: add a sanity check. */
  2268. dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
  2269. if (device->stopped & DASD_UNRESUMED_PM) {
  2270. dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
  2271. dasd_restore_device(device);
  2272. ret = 1;
  2273. break;
  2274. }
  2275. dasd_schedule_device_bh(device);
  2276. if (device->block)
  2277. dasd_schedule_block_bh(device->block);
  2278. ret = 1;
  2279. break;
  2280. }
  2281. dasd_put_device(device);
  2282. return ret;
  2283. }
  2284. int dasd_generic_pm_freeze(struct ccw_device *cdev)
  2285. {
  2286. struct dasd_ccw_req *cqr, *n;
  2287. int rc;
  2288. struct list_head freeze_queue;
  2289. struct dasd_device *device = dasd_device_from_cdev(cdev);
  2290. if (IS_ERR(device))
  2291. return PTR_ERR(device);
  2292. /* disallow new I/O */
  2293. dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
  2294. /* clear active requests */
  2295. INIT_LIST_HEAD(&freeze_queue);
  2296. spin_lock_irq(get_ccwdev_lock(cdev));
  2297. rc = 0;
  2298. list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
  2299. /* Check status and move request to flush_queue */
  2300. if (cqr->status == DASD_CQR_IN_IO) {
  2301. rc = device->discipline->term_IO(cqr);
  2302. if (rc) {
  2303. /* unable to terminate requeust */
  2304. dev_err(&device->cdev->dev,
  2305. "Unable to terminate request %p "
  2306. "on suspend\n", cqr);
  2307. spin_unlock_irq(get_ccwdev_lock(cdev));
  2308. dasd_put_device(device);
  2309. return rc;
  2310. }
  2311. }
  2312. list_move_tail(&cqr->devlist, &freeze_queue);
  2313. }
  2314. spin_unlock_irq(get_ccwdev_lock(cdev));
  2315. list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
  2316. wait_event(dasd_flush_wq,
  2317. (cqr->status != DASD_CQR_CLEAR_PENDING));
  2318. if (cqr->status == DASD_CQR_CLEARED)
  2319. cqr->status = DASD_CQR_QUEUED;
  2320. }
  2321. /* move freeze_queue to start of the ccw_queue */
  2322. spin_lock_irq(get_ccwdev_lock(cdev));
  2323. list_splice_tail(&freeze_queue, &device->ccw_queue);
  2324. spin_unlock_irq(get_ccwdev_lock(cdev));
  2325. if (device->discipline->freeze)
  2326. rc = device->discipline->freeze(device);
  2327. dasd_put_device(device);
  2328. return rc;
  2329. }
  2330. EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
  2331. int dasd_generic_restore_device(struct ccw_device *cdev)
  2332. {
  2333. struct dasd_device *device = dasd_device_from_cdev(cdev);
  2334. int rc = 0;
  2335. if (IS_ERR(device))
  2336. return PTR_ERR(device);
  2337. /* allow new IO again */
  2338. dasd_device_remove_stop_bits(device,
  2339. (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
  2340. dasd_schedule_device_bh(device);
  2341. /*
  2342. * call discipline restore function
  2343. * if device is stopped do nothing e.g. for disconnected devices
  2344. */
  2345. if (device->discipline->restore && !(device->stopped))
  2346. rc = device->discipline->restore(device);
  2347. if (rc || device->stopped)
  2348. /*
  2349. * if the resume failed for the DASD we put it in
  2350. * an UNRESUMED stop state
  2351. */
  2352. device->stopped |= DASD_UNRESUMED_PM;
  2353. if (device->block)
  2354. dasd_schedule_block_bh(device->block);
  2355. dasd_put_device(device);
  2356. return 0;
  2357. }
  2358. EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
  2359. static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
  2360. void *rdc_buffer,
  2361. int rdc_buffer_size,
  2362. int magic)
  2363. {
  2364. struct dasd_ccw_req *cqr;
  2365. struct ccw1 *ccw;
  2366. unsigned long *idaw;
  2367. cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
  2368. if (IS_ERR(cqr)) {
  2369. /* internal error 13 - Allocating the RDC request failed*/
  2370. dev_err(&device->cdev->dev,
  2371. "An error occurred in the DASD device driver, "
  2372. "reason=%s\n", "13");
  2373. return cqr;
  2374. }
  2375. ccw = cqr->cpaddr;
  2376. ccw->cmd_code = CCW_CMD_RDC;
  2377. if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
  2378. idaw = (unsigned long *) (cqr->data);
  2379. ccw->cda = (__u32)(addr_t) idaw;
  2380. ccw->flags = CCW_FLAG_IDA;
  2381. idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
  2382. } else {
  2383. ccw->cda = (__u32)(addr_t) rdc_buffer;
  2384. ccw->flags = 0;
  2385. }
  2386. ccw->count = rdc_buffer_size;
  2387. cqr->startdev = device;
  2388. cqr->memdev = device;
  2389. cqr->expires = 10*HZ;
  2390. cqr->retries = 256;
  2391. cqr->buildclk = get_clock();
  2392. cqr->status = DASD_CQR_FILLED;
  2393. return cqr;
  2394. }
  2395. int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
  2396. void *rdc_buffer, int rdc_buffer_size)
  2397. {
  2398. int ret;
  2399. struct dasd_ccw_req *cqr;
  2400. cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
  2401. magic);
  2402. if (IS_ERR(cqr))
  2403. return PTR_ERR(cqr);
  2404. ret = dasd_sleep_on(cqr);
  2405. dasd_sfree_request(cqr, cqr->memdev);
  2406. return ret;
  2407. }
  2408. EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
  2409. /*
  2410. * In command mode and transport mode we need to look for sense
  2411. * data in different places. The sense data itself is allways
  2412. * an array of 32 bytes, so we can unify the sense data access
  2413. * for both modes.
  2414. */
  2415. char *dasd_get_sense(struct irb *irb)
  2416. {
  2417. struct tsb *tsb = NULL;
  2418. char *sense = NULL;
  2419. if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
  2420. if (irb->scsw.tm.tcw)
  2421. tsb = tcw_get_tsb((struct tcw *)(unsigned long)
  2422. irb->scsw.tm.tcw);
  2423. if (tsb && tsb->length == 64 && tsb->flags)
  2424. switch (tsb->flags & 0x07) {
  2425. case 1: /* tsa_iostat */
  2426. sense = tsb->tsa.iostat.sense;
  2427. break;
  2428. case 2: /* tsa_ddpc */
  2429. sense = tsb->tsa.ddpc.sense;
  2430. break;
  2431. default:
  2432. /* currently we don't use interrogate data */
  2433. break;
  2434. }
  2435. } else if (irb->esw.esw0.erw.cons) {
  2436. sense = irb->ecw;
  2437. }
  2438. return sense;
  2439. }
  2440. EXPORT_SYMBOL_GPL(dasd_get_sense);
  2441. static int __init dasd_init(void)
  2442. {
  2443. int rc;
  2444. init_waitqueue_head(&dasd_init_waitq);
  2445. init_waitqueue_head(&dasd_flush_wq);
  2446. init_waitqueue_head(&generic_waitq);
  2447. /* register 'common' DASD debug area, used for all DBF_XXX calls */
  2448. dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
  2449. if (dasd_debug_area == NULL) {
  2450. rc = -ENOMEM;
  2451. goto failed;
  2452. }
  2453. debug_register_view(dasd_debug_area, &debug_sprintf_view);
  2454. debug_set_level(dasd_debug_area, DBF_WARNING);
  2455. DBF_EVENT(DBF_EMERG, "%s", "debug area created");
  2456. dasd_diag_discipline_pointer = NULL;
  2457. rc = dasd_devmap_init();
  2458. if (rc)
  2459. goto failed;
  2460. rc = dasd_gendisk_init();
  2461. if (rc)
  2462. goto failed;
  2463. rc = dasd_parse();
  2464. if (rc)
  2465. goto failed;
  2466. rc = dasd_eer_init();
  2467. if (rc)
  2468. goto failed;
  2469. #ifdef CONFIG_PROC_FS
  2470. rc = dasd_proc_init();
  2471. if (rc)
  2472. goto failed;
  2473. #endif
  2474. return 0;
  2475. failed:
  2476. pr_info("The DASD device driver could not be initialized\n");
  2477. dasd_exit();
  2478. return rc;
  2479. }
  2480. module_init(dasd_init);
  2481. module_exit(dasd_exit);
  2482. EXPORT_SYMBOL(dasd_debug_area);
  2483. EXPORT_SYMBOL(dasd_diag_discipline_pointer);
  2484. EXPORT_SYMBOL(dasd_add_request_head);
  2485. EXPORT_SYMBOL(dasd_add_request_tail);
  2486. EXPORT_SYMBOL(dasd_cancel_req);
  2487. EXPORT_SYMBOL(dasd_device_clear_timer);
  2488. EXPORT_SYMBOL(dasd_block_clear_timer);
  2489. EXPORT_SYMBOL(dasd_enable_device);
  2490. EXPORT_SYMBOL(dasd_int_handler);
  2491. EXPORT_SYMBOL(dasd_kfree_request);
  2492. EXPORT_SYMBOL(dasd_kick_device);
  2493. EXPORT_SYMBOL(dasd_kmalloc_request);
  2494. EXPORT_SYMBOL(dasd_schedule_device_bh);
  2495. EXPORT_SYMBOL(dasd_schedule_block_bh);
  2496. EXPORT_SYMBOL(dasd_set_target_state);
  2497. EXPORT_SYMBOL(dasd_device_set_timer);
  2498. EXPORT_SYMBOL(dasd_block_set_timer);
  2499. EXPORT_SYMBOL(dasd_sfree_request);
  2500. EXPORT_SYMBOL(dasd_sleep_on);
  2501. EXPORT_SYMBOL(dasd_sleep_on_immediatly);
  2502. EXPORT_SYMBOL(dasd_sleep_on_interruptible);
  2503. EXPORT_SYMBOL(dasd_smalloc_request);
  2504. EXPORT_SYMBOL(dasd_start_IO);
  2505. EXPORT_SYMBOL(dasd_term_IO);
  2506. EXPORT_SYMBOL_GPL(dasd_generic_probe);
  2507. EXPORT_SYMBOL_GPL(dasd_generic_remove);
  2508. EXPORT_SYMBOL_GPL(dasd_generic_notify);
  2509. EXPORT_SYMBOL_GPL(dasd_generic_set_online);
  2510. EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
  2511. EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
  2512. EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
  2513. EXPORT_SYMBOL_GPL(dasd_alloc_block);
  2514. EXPORT_SYMBOL_GPL(dasd_free_block);