dasd.c 76 KB

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