dasd.c 76 KB

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