dasd.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897
  1. /*
  2. * File...........: linux/drivers/s390/block/dasd.c
  3. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  4. * Horst Hummel <Horst.Hummel@de.ibm.com>
  5. * Carsten Otte <Cotte@de.ibm.com>
  6. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * Bugreports.to..: <Linux390@de.ibm.com>
  8. * Copyright IBM Corp. 1999, 2009
  9. */
  10. #define KMSG_COMPONENT "dasd"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12. #include <linux/kmod.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ctype.h>
  16. #include <linux/major.h>
  17. #include <linux/slab.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/hdreg.h>
  20. #include <linux/async.h>
  21. #include <linux/mutex.h>
  22. #include <linux/smp_lock.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. if (IS_ERR(irb)) {
  970. switch (PTR_ERR(irb)) {
  971. case -EIO:
  972. break;
  973. case -ETIMEDOUT:
  974. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
  975. "request timed out\n", __func__);
  976. break;
  977. default:
  978. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
  979. "unknown error %ld\n", __func__,
  980. PTR_ERR(irb));
  981. }
  982. dasd_handle_killed_request(cdev, intparm);
  983. return;
  984. }
  985. now = get_clock();
  986. /* check for unsolicited interrupts */
  987. cqr = (struct dasd_ccw_req *) intparm;
  988. if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
  989. (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
  990. (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
  991. if (cqr && cqr->status == DASD_CQR_IN_IO)
  992. cqr->status = DASD_CQR_QUEUED;
  993. device = dasd_device_from_cdev_locked(cdev);
  994. if (!IS_ERR(device)) {
  995. dasd_device_clear_timer(device);
  996. device->discipline->handle_unsolicited_interrupt(device,
  997. irb);
  998. dasd_put_device(device);
  999. }
  1000. return;
  1001. }
  1002. device = (struct dasd_device *) cqr->startdev;
  1003. if (!device ||
  1004. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  1005. DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
  1006. "invalid device in request");
  1007. return;
  1008. }
  1009. /* Check for clear pending */
  1010. if (cqr->status == DASD_CQR_CLEAR_PENDING &&
  1011. scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
  1012. cqr->status = DASD_CQR_CLEARED;
  1013. dasd_device_clear_timer(device);
  1014. wake_up(&dasd_flush_wq);
  1015. dasd_schedule_device_bh(device);
  1016. return;
  1017. }
  1018. /* check status - the request might have been killed by dyn detach */
  1019. if (cqr->status != DASD_CQR_IN_IO) {
  1020. DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
  1021. "status %02x", dev_name(&cdev->dev), cqr->status);
  1022. return;
  1023. }
  1024. next = NULL;
  1025. expires = 0;
  1026. if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  1027. scsw_cstat(&irb->scsw) == 0) {
  1028. /* request was completed successfully */
  1029. cqr->status = DASD_CQR_SUCCESS;
  1030. cqr->stopclk = now;
  1031. /* Start first request on queue if possible -> fast_io. */
  1032. if (cqr->devlist.next != &device->ccw_queue) {
  1033. next = list_entry(cqr->devlist.next,
  1034. struct dasd_ccw_req, devlist);
  1035. }
  1036. } else { /* error */
  1037. memcpy(&cqr->irb, irb, sizeof(struct irb));
  1038. /* log sense for every failed I/O to s390 debugfeature */
  1039. dasd_log_sense_dbf(cqr, irb);
  1040. if (device->features & DASD_FEATURE_ERPLOG) {
  1041. dasd_log_sense(cqr, irb);
  1042. }
  1043. /*
  1044. * If we don't want complex ERP for this request, then just
  1045. * reset this and retry it in the fastpath
  1046. */
  1047. if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
  1048. cqr->retries > 0) {
  1049. if (cqr->lpm == LPM_ANYPATH)
  1050. DBF_DEV_EVENT(DBF_DEBUG, device,
  1051. "default ERP in fastpath "
  1052. "(%i retries left)",
  1053. cqr->retries);
  1054. cqr->lpm = LPM_ANYPATH;
  1055. cqr->status = DASD_CQR_QUEUED;
  1056. next = cqr;
  1057. } else
  1058. cqr->status = DASD_CQR_ERROR;
  1059. }
  1060. if (next && (next->status == DASD_CQR_QUEUED) &&
  1061. (!device->stopped)) {
  1062. if (device->discipline->start_IO(next) == 0)
  1063. expires = next->expires;
  1064. }
  1065. if (expires != 0)
  1066. dasd_device_set_timer(device, expires);
  1067. else
  1068. dasd_device_clear_timer(device);
  1069. dasd_schedule_device_bh(device);
  1070. }
  1071. enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
  1072. {
  1073. struct dasd_device *device;
  1074. device = dasd_device_from_cdev_locked(cdev);
  1075. if (IS_ERR(device))
  1076. goto out;
  1077. if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
  1078. device->state != device->target ||
  1079. !device->discipline->handle_unsolicited_interrupt){
  1080. dasd_put_device(device);
  1081. goto out;
  1082. }
  1083. dasd_device_clear_timer(device);
  1084. device->discipline->handle_unsolicited_interrupt(device, irb);
  1085. dasd_put_device(device);
  1086. out:
  1087. return UC_TODO_RETRY;
  1088. }
  1089. EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
  1090. /*
  1091. * If we have an error on a dasd_block layer request then we cancel
  1092. * and return all further requests from the same dasd_block as well.
  1093. */
  1094. static void __dasd_device_recovery(struct dasd_device *device,
  1095. struct dasd_ccw_req *ref_cqr)
  1096. {
  1097. struct list_head *l, *n;
  1098. struct dasd_ccw_req *cqr;
  1099. /*
  1100. * only requeue request that came from the dasd_block layer
  1101. */
  1102. if (!ref_cqr->block)
  1103. return;
  1104. list_for_each_safe(l, n, &device->ccw_queue) {
  1105. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1106. if (cqr->status == DASD_CQR_QUEUED &&
  1107. ref_cqr->block == cqr->block) {
  1108. cqr->status = DASD_CQR_CLEARED;
  1109. }
  1110. }
  1111. };
  1112. /*
  1113. * Remove those ccw requests from the queue that need to be returned
  1114. * to the upper layer.
  1115. */
  1116. static void __dasd_device_process_ccw_queue(struct dasd_device *device,
  1117. struct list_head *final_queue)
  1118. {
  1119. struct list_head *l, *n;
  1120. struct dasd_ccw_req *cqr;
  1121. /* Process request with final status. */
  1122. list_for_each_safe(l, n, &device->ccw_queue) {
  1123. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1124. /* Stop list processing at the first non-final request. */
  1125. if (cqr->status == DASD_CQR_QUEUED ||
  1126. cqr->status == DASD_CQR_IN_IO ||
  1127. cqr->status == DASD_CQR_CLEAR_PENDING)
  1128. break;
  1129. if (cqr->status == DASD_CQR_ERROR) {
  1130. __dasd_device_recovery(device, cqr);
  1131. }
  1132. /* Rechain finished requests to final queue */
  1133. list_move_tail(&cqr->devlist, final_queue);
  1134. }
  1135. }
  1136. /*
  1137. * the cqrs from the final queue are returned to the upper layer
  1138. * by setting a dasd_block state and calling the callback function
  1139. */
  1140. static void __dasd_device_process_final_queue(struct dasd_device *device,
  1141. struct list_head *final_queue)
  1142. {
  1143. struct list_head *l, *n;
  1144. struct dasd_ccw_req *cqr;
  1145. struct dasd_block *block;
  1146. void (*callback)(struct dasd_ccw_req *, void *data);
  1147. void *callback_data;
  1148. char errorstring[ERRORLENGTH];
  1149. list_for_each_safe(l, n, final_queue) {
  1150. cqr = list_entry(l, struct dasd_ccw_req, devlist);
  1151. list_del_init(&cqr->devlist);
  1152. block = cqr->block;
  1153. callback = cqr->callback;
  1154. callback_data = cqr->callback_data;
  1155. if (block)
  1156. spin_lock_bh(&block->queue_lock);
  1157. switch (cqr->status) {
  1158. case DASD_CQR_SUCCESS:
  1159. cqr->status = DASD_CQR_DONE;
  1160. break;
  1161. case DASD_CQR_ERROR:
  1162. cqr->status = DASD_CQR_NEED_ERP;
  1163. break;
  1164. case DASD_CQR_CLEARED:
  1165. cqr->status = DASD_CQR_TERMINATED;
  1166. break;
  1167. default:
  1168. /* internal error 12 - wrong cqr status*/
  1169. snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
  1170. dev_err(&device->cdev->dev,
  1171. "An error occurred in the DASD device driver, "
  1172. "reason=%s\n", errorstring);
  1173. BUG();
  1174. }
  1175. if (cqr->callback != NULL)
  1176. (callback)(cqr, callback_data);
  1177. if (block)
  1178. spin_unlock_bh(&block->queue_lock);
  1179. }
  1180. }
  1181. /*
  1182. * Take a look at the first request on the ccw queue and check
  1183. * if it reached its expire time. If so, terminate the IO.
  1184. */
  1185. static void __dasd_device_check_expire(struct dasd_device *device)
  1186. {
  1187. struct dasd_ccw_req *cqr;
  1188. if (list_empty(&device->ccw_queue))
  1189. return;
  1190. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1191. if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
  1192. (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
  1193. if (device->discipline->term_IO(cqr) != 0) {
  1194. /* Hmpf, try again in 5 sec */
  1195. dev_err(&device->cdev->dev,
  1196. "cqr %p timed out (%lus) but cannot be "
  1197. "ended, retrying in 5 s\n",
  1198. cqr, (cqr->expires/HZ));
  1199. cqr->expires += 5*HZ;
  1200. dasd_device_set_timer(device, 5*HZ);
  1201. } else {
  1202. dev_err(&device->cdev->dev,
  1203. "cqr %p timed out (%lus), %i retries "
  1204. "remaining\n", cqr, (cqr->expires/HZ),
  1205. cqr->retries);
  1206. }
  1207. }
  1208. }
  1209. /*
  1210. * Take a look at the first request on the ccw queue and check
  1211. * if it needs to be started.
  1212. */
  1213. static void __dasd_device_start_head(struct dasd_device *device)
  1214. {
  1215. struct dasd_ccw_req *cqr;
  1216. int rc;
  1217. if (list_empty(&device->ccw_queue))
  1218. return;
  1219. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1220. if (cqr->status != DASD_CQR_QUEUED)
  1221. return;
  1222. /* when device is stopped, return request to previous layer */
  1223. if (device->stopped) {
  1224. cqr->status = DASD_CQR_CLEARED;
  1225. dasd_schedule_device_bh(device);
  1226. return;
  1227. }
  1228. rc = device->discipline->start_IO(cqr);
  1229. if (rc == 0)
  1230. dasd_device_set_timer(device, cqr->expires);
  1231. else if (rc == -EACCES) {
  1232. dasd_schedule_device_bh(device);
  1233. } else
  1234. /* Hmpf, try again in 1/2 sec */
  1235. dasd_device_set_timer(device, 50);
  1236. }
  1237. /*
  1238. * Go through all request on the dasd_device request queue,
  1239. * terminate them on the cdev if necessary, and return them to the
  1240. * submitting layer via callback.
  1241. * Note:
  1242. * Make sure that all 'submitting layers' still exist when
  1243. * this function is called!. In other words, when 'device' is a base
  1244. * device then all block layer requests must have been removed before
  1245. * via dasd_flush_block_queue.
  1246. */
  1247. int dasd_flush_device_queue(struct dasd_device *device)
  1248. {
  1249. struct dasd_ccw_req *cqr, *n;
  1250. int rc;
  1251. struct list_head flush_queue;
  1252. INIT_LIST_HEAD(&flush_queue);
  1253. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1254. rc = 0;
  1255. list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
  1256. /* Check status and move request to flush_queue */
  1257. switch (cqr->status) {
  1258. case DASD_CQR_IN_IO:
  1259. rc = device->discipline->term_IO(cqr);
  1260. if (rc) {
  1261. /* unable to terminate requeust */
  1262. dev_err(&device->cdev->dev,
  1263. "Flushing the DASD request queue "
  1264. "failed for request %p\n", cqr);
  1265. /* stop flush processing */
  1266. goto finished;
  1267. }
  1268. break;
  1269. case DASD_CQR_QUEUED:
  1270. cqr->stopclk = get_clock();
  1271. cqr->status = DASD_CQR_CLEARED;
  1272. break;
  1273. default: /* no need to modify the others */
  1274. break;
  1275. }
  1276. list_move_tail(&cqr->devlist, &flush_queue);
  1277. }
  1278. finished:
  1279. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1280. /*
  1281. * After this point all requests must be in state CLEAR_PENDING,
  1282. * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
  1283. * one of the others.
  1284. */
  1285. list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
  1286. wait_event(dasd_flush_wq,
  1287. (cqr->status != DASD_CQR_CLEAR_PENDING));
  1288. /*
  1289. * Now set each request back to TERMINATED, DONE or NEED_ERP
  1290. * and call the callback function of flushed requests
  1291. */
  1292. __dasd_device_process_final_queue(device, &flush_queue);
  1293. return rc;
  1294. }
  1295. /*
  1296. * Acquire the device lock and process queues for the device.
  1297. */
  1298. static void dasd_device_tasklet(struct dasd_device *device)
  1299. {
  1300. struct list_head final_queue;
  1301. atomic_set (&device->tasklet_scheduled, 0);
  1302. INIT_LIST_HEAD(&final_queue);
  1303. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1304. /* Check expire time of first request on the ccw queue. */
  1305. __dasd_device_check_expire(device);
  1306. /* find final requests on ccw queue */
  1307. __dasd_device_process_ccw_queue(device, &final_queue);
  1308. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1309. /* Now call the callback function of requests with final status */
  1310. __dasd_device_process_final_queue(device, &final_queue);
  1311. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1312. /* Now check if the head of the ccw queue needs to be started. */
  1313. __dasd_device_start_head(device);
  1314. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1315. dasd_put_device(device);
  1316. }
  1317. /*
  1318. * Schedules a call to dasd_tasklet over the device tasklet.
  1319. */
  1320. void dasd_schedule_device_bh(struct dasd_device *device)
  1321. {
  1322. /* Protect against rescheduling. */
  1323. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  1324. return;
  1325. dasd_get_device(device);
  1326. tasklet_hi_schedule(&device->tasklet);
  1327. }
  1328. void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
  1329. {
  1330. device->stopped |= bits;
  1331. }
  1332. EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
  1333. void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
  1334. {
  1335. device->stopped &= ~bits;
  1336. if (!device->stopped)
  1337. wake_up(&generic_waitq);
  1338. }
  1339. EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
  1340. /*
  1341. * Queue a request to the head of the device ccw_queue.
  1342. * Start the I/O if possible.
  1343. */
  1344. void dasd_add_request_head(struct dasd_ccw_req *cqr)
  1345. {
  1346. struct dasd_device *device;
  1347. unsigned long flags;
  1348. device = cqr->startdev;
  1349. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1350. cqr->status = DASD_CQR_QUEUED;
  1351. list_add(&cqr->devlist, &device->ccw_queue);
  1352. /* let the bh start the request to keep them in order */
  1353. dasd_schedule_device_bh(device);
  1354. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1355. }
  1356. /*
  1357. * Queue a request to the tail of the device ccw_queue.
  1358. * Start the I/O if possible.
  1359. */
  1360. void dasd_add_request_tail(struct dasd_ccw_req *cqr)
  1361. {
  1362. struct dasd_device *device;
  1363. unsigned long flags;
  1364. device = cqr->startdev;
  1365. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1366. cqr->status = DASD_CQR_QUEUED;
  1367. list_add_tail(&cqr->devlist, &device->ccw_queue);
  1368. /* let the bh start the request to keep them in order */
  1369. dasd_schedule_device_bh(device);
  1370. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1371. }
  1372. /*
  1373. * Wakeup helper for the 'sleep_on' functions.
  1374. */
  1375. static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  1376. {
  1377. spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
  1378. cqr->callback_data = DASD_SLEEPON_END_TAG;
  1379. spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
  1380. wake_up(&generic_waitq);
  1381. }
  1382. static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
  1383. {
  1384. struct dasd_device *device;
  1385. int rc;
  1386. device = cqr->startdev;
  1387. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1388. rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
  1389. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1390. return rc;
  1391. }
  1392. /*
  1393. * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
  1394. */
  1395. static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
  1396. {
  1397. struct dasd_device *device;
  1398. dasd_erp_fn_t erp_fn;
  1399. if (cqr->status == DASD_CQR_FILLED)
  1400. return 0;
  1401. device = cqr->startdev;
  1402. if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
  1403. if (cqr->status == DASD_CQR_TERMINATED) {
  1404. device->discipline->handle_terminated_request(cqr);
  1405. return 1;
  1406. }
  1407. if (cqr->status == DASD_CQR_NEED_ERP) {
  1408. erp_fn = device->discipline->erp_action(cqr);
  1409. erp_fn(cqr);
  1410. return 1;
  1411. }
  1412. if (cqr->status == DASD_CQR_FAILED)
  1413. dasd_log_sense(cqr, &cqr->irb);
  1414. if (cqr->refers) {
  1415. __dasd_process_erp(device, cqr);
  1416. return 1;
  1417. }
  1418. }
  1419. return 0;
  1420. }
  1421. static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
  1422. {
  1423. if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
  1424. if (cqr->refers) /* erp is not done yet */
  1425. return 1;
  1426. return ((cqr->status != DASD_CQR_DONE) &&
  1427. (cqr->status != DASD_CQR_FAILED));
  1428. } else
  1429. return (cqr->status == DASD_CQR_FILLED);
  1430. }
  1431. static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
  1432. {
  1433. struct dasd_device *device;
  1434. int rc;
  1435. struct list_head ccw_queue;
  1436. struct dasd_ccw_req *cqr;
  1437. INIT_LIST_HEAD(&ccw_queue);
  1438. maincqr->status = DASD_CQR_FILLED;
  1439. device = maincqr->startdev;
  1440. list_add(&maincqr->blocklist, &ccw_queue);
  1441. for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
  1442. cqr = list_first_entry(&ccw_queue,
  1443. struct dasd_ccw_req, blocklist)) {
  1444. if (__dasd_sleep_on_erp(cqr))
  1445. continue;
  1446. if (cqr->status != DASD_CQR_FILLED) /* could be failed */
  1447. continue;
  1448. /* Non-temporary stop condition will trigger fail fast */
  1449. if (device->stopped & ~DASD_STOPPED_PENDING &&
  1450. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1451. (!dasd_eer_enabled(device))) {
  1452. cqr->status = DASD_CQR_FAILED;
  1453. continue;
  1454. }
  1455. /* Don't try to start requests if device is stopped */
  1456. if (interruptible) {
  1457. rc = wait_event_interruptible(
  1458. generic_waitq, !(device->stopped));
  1459. if (rc == -ERESTARTSYS) {
  1460. cqr->status = DASD_CQR_FAILED;
  1461. maincqr->intrc = rc;
  1462. continue;
  1463. }
  1464. } else
  1465. wait_event(generic_waitq, !(device->stopped));
  1466. cqr->callback = dasd_wakeup_cb;
  1467. cqr->callback_data = DASD_SLEEPON_START_TAG;
  1468. dasd_add_request_tail(cqr);
  1469. if (interruptible) {
  1470. rc = wait_event_interruptible(
  1471. generic_waitq, _wait_for_wakeup(cqr));
  1472. if (rc == -ERESTARTSYS) {
  1473. dasd_cancel_req(cqr);
  1474. /* wait (non-interruptible) for final status */
  1475. wait_event(generic_waitq,
  1476. _wait_for_wakeup(cqr));
  1477. cqr->status = DASD_CQR_FAILED;
  1478. maincqr->intrc = rc;
  1479. continue;
  1480. }
  1481. } else
  1482. wait_event(generic_waitq, _wait_for_wakeup(cqr));
  1483. }
  1484. maincqr->endclk = get_clock();
  1485. if ((maincqr->status != DASD_CQR_DONE) &&
  1486. (maincqr->intrc != -ERESTARTSYS))
  1487. dasd_log_sense(maincqr, &maincqr->irb);
  1488. if (maincqr->status == DASD_CQR_DONE)
  1489. rc = 0;
  1490. else if (maincqr->intrc)
  1491. rc = maincqr->intrc;
  1492. else
  1493. rc = -EIO;
  1494. return rc;
  1495. }
  1496. /*
  1497. * Queue a request to the tail of the device ccw_queue and wait for
  1498. * it's completion.
  1499. */
  1500. int dasd_sleep_on(struct dasd_ccw_req *cqr)
  1501. {
  1502. return _dasd_sleep_on(cqr, 0);
  1503. }
  1504. /*
  1505. * Queue a request to the tail of the device ccw_queue and wait
  1506. * interruptible for it's completion.
  1507. */
  1508. int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
  1509. {
  1510. return _dasd_sleep_on(cqr, 1);
  1511. }
  1512. /*
  1513. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  1514. * for eckd devices) the currently running request has to be terminated
  1515. * and be put back to status queued, before the special request is added
  1516. * to the head of the queue. Then the special request is waited on normally.
  1517. */
  1518. static inline int _dasd_term_running_cqr(struct dasd_device *device)
  1519. {
  1520. struct dasd_ccw_req *cqr;
  1521. if (list_empty(&device->ccw_queue))
  1522. return 0;
  1523. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
  1524. return device->discipline->term_IO(cqr);
  1525. }
  1526. int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
  1527. {
  1528. struct dasd_device *device;
  1529. int rc;
  1530. device = cqr->startdev;
  1531. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1532. rc = _dasd_term_running_cqr(device);
  1533. if (rc) {
  1534. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1535. return rc;
  1536. }
  1537. cqr->callback = dasd_wakeup_cb;
  1538. cqr->callback_data = DASD_SLEEPON_START_TAG;
  1539. cqr->status = DASD_CQR_QUEUED;
  1540. list_add(&cqr->devlist, &device->ccw_queue);
  1541. /* let the bh start the request to keep them in order */
  1542. dasd_schedule_device_bh(device);
  1543. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1544. wait_event(generic_waitq, _wait_for_wakeup(cqr));
  1545. if (cqr->status == DASD_CQR_DONE)
  1546. rc = 0;
  1547. else if (cqr->intrc)
  1548. rc = cqr->intrc;
  1549. else
  1550. rc = -EIO;
  1551. return rc;
  1552. }
  1553. /*
  1554. * Cancels a request that was started with dasd_sleep_on_req.
  1555. * This is useful to timeout requests. The request will be
  1556. * terminated if it is currently in i/o.
  1557. * Returns 1 if the request has been terminated.
  1558. * 0 if there was no need to terminate the request (not started yet)
  1559. * negative error code if termination failed
  1560. * Cancellation of a request is an asynchronous operation! The calling
  1561. * function has to wait until the request is properly returned via callback.
  1562. */
  1563. int dasd_cancel_req(struct dasd_ccw_req *cqr)
  1564. {
  1565. struct dasd_device *device = cqr->startdev;
  1566. unsigned long flags;
  1567. int rc;
  1568. rc = 0;
  1569. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1570. switch (cqr->status) {
  1571. case DASD_CQR_QUEUED:
  1572. /* request was not started - just set to cleared */
  1573. cqr->status = DASD_CQR_CLEARED;
  1574. break;
  1575. case DASD_CQR_IN_IO:
  1576. /* request in IO - terminate IO and release again */
  1577. rc = device->discipline->term_IO(cqr);
  1578. if (rc) {
  1579. dev_err(&device->cdev->dev,
  1580. "Cancelling request %p failed with rc=%d\n",
  1581. cqr, rc);
  1582. } else {
  1583. cqr->stopclk = get_clock();
  1584. }
  1585. break;
  1586. default: /* already finished or clear pending - do nothing */
  1587. break;
  1588. }
  1589. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1590. dasd_schedule_device_bh(device);
  1591. return rc;
  1592. }
  1593. /*
  1594. * SECTION: Operations of the dasd_block layer.
  1595. */
  1596. /*
  1597. * Timeout function for dasd_block. This is used when the block layer
  1598. * is waiting for something that may not come reliably, (e.g. a state
  1599. * change interrupt)
  1600. */
  1601. static void dasd_block_timeout(unsigned long ptr)
  1602. {
  1603. unsigned long flags;
  1604. struct dasd_block *block;
  1605. block = (struct dasd_block *) ptr;
  1606. spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
  1607. /* re-activate request queue */
  1608. dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
  1609. spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
  1610. dasd_schedule_block_bh(block);
  1611. }
  1612. /*
  1613. * Setup timeout for a dasd_block in jiffies.
  1614. */
  1615. void dasd_block_set_timer(struct dasd_block *block, int expires)
  1616. {
  1617. if (expires == 0)
  1618. del_timer(&block->timer);
  1619. else
  1620. mod_timer(&block->timer, jiffies + expires);
  1621. }
  1622. /*
  1623. * Clear timeout for a dasd_block.
  1624. */
  1625. void dasd_block_clear_timer(struct dasd_block *block)
  1626. {
  1627. del_timer(&block->timer);
  1628. }
  1629. /*
  1630. * Process finished error recovery ccw.
  1631. */
  1632. static void __dasd_process_erp(struct dasd_device *device,
  1633. struct dasd_ccw_req *cqr)
  1634. {
  1635. dasd_erp_fn_t erp_fn;
  1636. if (cqr->status == DASD_CQR_DONE)
  1637. DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
  1638. else
  1639. dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
  1640. erp_fn = device->discipline->erp_postaction(cqr);
  1641. erp_fn(cqr);
  1642. }
  1643. /*
  1644. * Fetch requests from the block device queue.
  1645. */
  1646. static void __dasd_process_request_queue(struct dasd_block *block)
  1647. {
  1648. struct request_queue *queue;
  1649. struct request *req;
  1650. struct dasd_ccw_req *cqr;
  1651. struct dasd_device *basedev;
  1652. unsigned long flags;
  1653. queue = block->request_queue;
  1654. basedev = block->base;
  1655. /* No queue ? Then there is nothing to do. */
  1656. if (queue == NULL)
  1657. return;
  1658. /*
  1659. * We requeue request from the block device queue to the ccw
  1660. * queue only in two states. In state DASD_STATE_READY the
  1661. * partition detection is done and we need to requeue requests
  1662. * for that. State DASD_STATE_ONLINE is normal block device
  1663. * operation.
  1664. */
  1665. if (basedev->state < DASD_STATE_READY) {
  1666. while ((req = blk_fetch_request(block->request_queue)))
  1667. __blk_end_request_all(req, -EIO);
  1668. return;
  1669. }
  1670. /* Now we try to fetch requests from the request queue */
  1671. while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
  1672. if (basedev->features & DASD_FEATURE_READONLY &&
  1673. rq_data_dir(req) == WRITE) {
  1674. DBF_DEV_EVENT(DBF_ERR, basedev,
  1675. "Rejecting write request %p",
  1676. req);
  1677. blk_start_request(req);
  1678. __blk_end_request_all(req, -EIO);
  1679. continue;
  1680. }
  1681. cqr = basedev->discipline->build_cp(basedev, block, req);
  1682. if (IS_ERR(cqr)) {
  1683. if (PTR_ERR(cqr) == -EBUSY)
  1684. break; /* normal end condition */
  1685. if (PTR_ERR(cqr) == -ENOMEM)
  1686. break; /* terminate request queue loop */
  1687. if (PTR_ERR(cqr) == -EAGAIN) {
  1688. /*
  1689. * The current request cannot be build right
  1690. * now, we have to try later. If this request
  1691. * is the head-of-queue we stop the device
  1692. * for 1/2 second.
  1693. */
  1694. if (!list_empty(&block->ccw_queue))
  1695. break;
  1696. spin_lock_irqsave(
  1697. get_ccwdev_lock(basedev->cdev), flags);
  1698. dasd_device_set_stop_bits(basedev,
  1699. DASD_STOPPED_PENDING);
  1700. spin_unlock_irqrestore(
  1701. get_ccwdev_lock(basedev->cdev), flags);
  1702. dasd_block_set_timer(block, HZ/2);
  1703. break;
  1704. }
  1705. DBF_DEV_EVENT(DBF_ERR, basedev,
  1706. "CCW creation failed (rc=%ld) "
  1707. "on request %p",
  1708. PTR_ERR(cqr), req);
  1709. blk_start_request(req);
  1710. __blk_end_request_all(req, -EIO);
  1711. continue;
  1712. }
  1713. /*
  1714. * Note: callback is set to dasd_return_cqr_cb in
  1715. * __dasd_block_start_head to cover erp requests as well
  1716. */
  1717. cqr->callback_data = (void *) req;
  1718. cqr->status = DASD_CQR_FILLED;
  1719. blk_start_request(req);
  1720. list_add_tail(&cqr->blocklist, &block->ccw_queue);
  1721. dasd_profile_start(block, cqr, req);
  1722. }
  1723. }
  1724. static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
  1725. {
  1726. struct request *req;
  1727. int status;
  1728. int error = 0;
  1729. req = (struct request *) cqr->callback_data;
  1730. dasd_profile_end(cqr->block, cqr, req);
  1731. status = cqr->block->base->discipline->free_cp(cqr, req);
  1732. if (status <= 0)
  1733. error = status ? status : -EIO;
  1734. __blk_end_request_all(req, error);
  1735. }
  1736. /*
  1737. * Process ccw request queue.
  1738. */
  1739. static void __dasd_process_block_ccw_queue(struct dasd_block *block,
  1740. struct list_head *final_queue)
  1741. {
  1742. struct list_head *l, *n;
  1743. struct dasd_ccw_req *cqr;
  1744. dasd_erp_fn_t erp_fn;
  1745. unsigned long flags;
  1746. struct dasd_device *base = block->base;
  1747. restart:
  1748. /* Process request with final status. */
  1749. list_for_each_safe(l, n, &block->ccw_queue) {
  1750. cqr = list_entry(l, struct dasd_ccw_req, blocklist);
  1751. if (cqr->status != DASD_CQR_DONE &&
  1752. cqr->status != DASD_CQR_FAILED &&
  1753. cqr->status != DASD_CQR_NEED_ERP &&
  1754. cqr->status != DASD_CQR_TERMINATED)
  1755. continue;
  1756. if (cqr->status == DASD_CQR_TERMINATED) {
  1757. base->discipline->handle_terminated_request(cqr);
  1758. goto restart;
  1759. }
  1760. /* Process requests that may be recovered */
  1761. if (cqr->status == DASD_CQR_NEED_ERP) {
  1762. erp_fn = base->discipline->erp_action(cqr);
  1763. if (IS_ERR(erp_fn(cqr)))
  1764. continue;
  1765. goto restart;
  1766. }
  1767. /* log sense for fatal error */
  1768. if (cqr->status == DASD_CQR_FAILED) {
  1769. dasd_log_sense(cqr, &cqr->irb);
  1770. }
  1771. /* First of all call extended error reporting. */
  1772. if (dasd_eer_enabled(base) &&
  1773. cqr->status == DASD_CQR_FAILED) {
  1774. dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
  1775. /* restart request */
  1776. cqr->status = DASD_CQR_FILLED;
  1777. cqr->retries = 255;
  1778. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  1779. dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
  1780. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
  1781. flags);
  1782. goto restart;
  1783. }
  1784. /* Process finished ERP request. */
  1785. if (cqr->refers) {
  1786. __dasd_process_erp(base, cqr);
  1787. goto restart;
  1788. }
  1789. /* Rechain finished requests to final queue */
  1790. cqr->endclk = get_clock();
  1791. list_move_tail(&cqr->blocklist, final_queue);
  1792. }
  1793. }
  1794. static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
  1795. {
  1796. dasd_schedule_block_bh(cqr->block);
  1797. }
  1798. static void __dasd_block_start_head(struct dasd_block *block)
  1799. {
  1800. struct dasd_ccw_req *cqr;
  1801. if (list_empty(&block->ccw_queue))
  1802. return;
  1803. /* We allways begin with the first requests on the queue, as some
  1804. * of previously started requests have to be enqueued on a
  1805. * dasd_device again for error recovery.
  1806. */
  1807. list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
  1808. if (cqr->status != DASD_CQR_FILLED)
  1809. continue;
  1810. /* Non-temporary stop condition will trigger fail fast */
  1811. if (block->base->stopped & ~DASD_STOPPED_PENDING &&
  1812. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1813. (!dasd_eer_enabled(block->base))) {
  1814. cqr->status = DASD_CQR_FAILED;
  1815. dasd_schedule_block_bh(block);
  1816. continue;
  1817. }
  1818. /* Don't try to start requests if device is stopped */
  1819. if (block->base->stopped)
  1820. return;
  1821. /* just a fail safe check, should not happen */
  1822. if (!cqr->startdev)
  1823. cqr->startdev = block->base;
  1824. /* make sure that the requests we submit find their way back */
  1825. cqr->callback = dasd_return_cqr_cb;
  1826. dasd_add_request_tail(cqr);
  1827. }
  1828. }
  1829. /*
  1830. * Central dasd_block layer routine. Takes requests from the generic
  1831. * block layer request queue, creates ccw requests, enqueues them on
  1832. * a dasd_device and processes ccw requests that have been returned.
  1833. */
  1834. static void dasd_block_tasklet(struct dasd_block *block)
  1835. {
  1836. struct list_head final_queue;
  1837. struct list_head *l, *n;
  1838. struct dasd_ccw_req *cqr;
  1839. atomic_set(&block->tasklet_scheduled, 0);
  1840. INIT_LIST_HEAD(&final_queue);
  1841. spin_lock(&block->queue_lock);
  1842. /* Finish off requests on ccw queue */
  1843. __dasd_process_block_ccw_queue(block, &final_queue);
  1844. spin_unlock(&block->queue_lock);
  1845. /* Now call the callback function of requests with final status */
  1846. spin_lock_irq(&block->request_queue_lock);
  1847. list_for_each_safe(l, n, &final_queue) {
  1848. cqr = list_entry(l, struct dasd_ccw_req, blocklist);
  1849. list_del_init(&cqr->blocklist);
  1850. __dasd_cleanup_cqr(cqr);
  1851. }
  1852. spin_lock(&block->queue_lock);
  1853. /* Get new request from the block device request queue */
  1854. __dasd_process_request_queue(block);
  1855. /* Now check if the head of the ccw queue needs to be started. */
  1856. __dasd_block_start_head(block);
  1857. spin_unlock(&block->queue_lock);
  1858. spin_unlock_irq(&block->request_queue_lock);
  1859. dasd_put_device(block->base);
  1860. }
  1861. static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
  1862. {
  1863. wake_up(&dasd_flush_wq);
  1864. }
  1865. /*
  1866. * Go through all request on the dasd_block request queue, cancel them
  1867. * on the respective dasd_device, and return them to the generic
  1868. * block layer.
  1869. */
  1870. static int dasd_flush_block_queue(struct dasd_block *block)
  1871. {
  1872. struct dasd_ccw_req *cqr, *n;
  1873. int rc, i;
  1874. struct list_head flush_queue;
  1875. INIT_LIST_HEAD(&flush_queue);
  1876. spin_lock_bh(&block->queue_lock);
  1877. rc = 0;
  1878. restart:
  1879. list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
  1880. /* if this request currently owned by a dasd_device cancel it */
  1881. if (cqr->status >= DASD_CQR_QUEUED)
  1882. rc = dasd_cancel_req(cqr);
  1883. if (rc < 0)
  1884. break;
  1885. /* Rechain request (including erp chain) so it won't be
  1886. * touched by the dasd_block_tasklet anymore.
  1887. * Replace the callback so we notice when the request
  1888. * is returned from the dasd_device layer.
  1889. */
  1890. cqr->callback = _dasd_wake_block_flush_cb;
  1891. for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
  1892. list_move_tail(&cqr->blocklist, &flush_queue);
  1893. if (i > 1)
  1894. /* moved more than one request - need to restart */
  1895. goto restart;
  1896. }
  1897. spin_unlock_bh(&block->queue_lock);
  1898. /* Now call the callback function of flushed requests */
  1899. restart_cb:
  1900. list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
  1901. wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
  1902. /* Process finished ERP request. */
  1903. if (cqr->refers) {
  1904. spin_lock_bh(&block->queue_lock);
  1905. __dasd_process_erp(block->base, cqr);
  1906. spin_unlock_bh(&block->queue_lock);
  1907. /* restart list_for_xx loop since dasd_process_erp
  1908. * might remove multiple elements */
  1909. goto restart_cb;
  1910. }
  1911. /* call the callback function */
  1912. spin_lock_irq(&block->request_queue_lock);
  1913. cqr->endclk = get_clock();
  1914. list_del_init(&cqr->blocklist);
  1915. __dasd_cleanup_cqr(cqr);
  1916. spin_unlock_irq(&block->request_queue_lock);
  1917. }
  1918. return rc;
  1919. }
  1920. /*
  1921. * Schedules a call to dasd_tasklet over the device tasklet.
  1922. */
  1923. void dasd_schedule_block_bh(struct dasd_block *block)
  1924. {
  1925. /* Protect against rescheduling. */
  1926. if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
  1927. return;
  1928. /* life cycle of block is bound to it's base device */
  1929. dasd_get_device(block->base);
  1930. tasklet_hi_schedule(&block->tasklet);
  1931. }
  1932. /*
  1933. * SECTION: external block device operations
  1934. * (request queue handling, open, release, etc.)
  1935. */
  1936. /*
  1937. * Dasd request queue function. Called from ll_rw_blk.c
  1938. */
  1939. static void do_dasd_request(struct request_queue *queue)
  1940. {
  1941. struct dasd_block *block;
  1942. block = queue->queuedata;
  1943. spin_lock(&block->queue_lock);
  1944. /* Get new request from the block device request queue */
  1945. __dasd_process_request_queue(block);
  1946. /* Now check if the head of the ccw queue needs to be started. */
  1947. __dasd_block_start_head(block);
  1948. spin_unlock(&block->queue_lock);
  1949. }
  1950. /*
  1951. * Allocate and initialize request queue and default I/O scheduler.
  1952. */
  1953. static int dasd_alloc_queue(struct dasd_block *block)
  1954. {
  1955. int rc;
  1956. block->request_queue = blk_init_queue(do_dasd_request,
  1957. &block->request_queue_lock);
  1958. if (block->request_queue == NULL)
  1959. return -ENOMEM;
  1960. block->request_queue->queuedata = block;
  1961. elevator_exit(block->request_queue->elevator);
  1962. block->request_queue->elevator = NULL;
  1963. rc = elevator_init(block->request_queue, "deadline");
  1964. if (rc) {
  1965. blk_cleanup_queue(block->request_queue);
  1966. return rc;
  1967. }
  1968. return 0;
  1969. }
  1970. /*
  1971. * Allocate and initialize request queue.
  1972. */
  1973. static void dasd_setup_queue(struct dasd_block *block)
  1974. {
  1975. int max;
  1976. blk_queue_logical_block_size(block->request_queue, block->bp_block);
  1977. max = block->base->discipline->max_blocks << block->s2b_shift;
  1978. blk_queue_max_hw_sectors(block->request_queue, max);
  1979. blk_queue_max_segments(block->request_queue, -1L);
  1980. /* with page sized segments we can translate each segement into
  1981. * one idaw/tidaw
  1982. */
  1983. blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
  1984. blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
  1985. blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN);
  1986. }
  1987. /*
  1988. * Deactivate and free request queue.
  1989. */
  1990. static void dasd_free_queue(struct dasd_block *block)
  1991. {
  1992. if (block->request_queue) {
  1993. blk_cleanup_queue(block->request_queue);
  1994. block->request_queue = NULL;
  1995. }
  1996. }
  1997. /*
  1998. * Flush request on the request queue.
  1999. */
  2000. static void dasd_flush_request_queue(struct dasd_block *block)
  2001. {
  2002. struct request *req;
  2003. if (!block->request_queue)
  2004. return;
  2005. spin_lock_irq(&block->request_queue_lock);
  2006. while ((req = blk_fetch_request(block->request_queue)))
  2007. __blk_end_request_all(req, -EIO);
  2008. spin_unlock_irq(&block->request_queue_lock);
  2009. }
  2010. static int dasd_open(struct block_device *bdev, fmode_t mode)
  2011. {
  2012. struct dasd_block *block = bdev->bd_disk->private_data;
  2013. struct dasd_device *base;
  2014. int rc;
  2015. if (!block)
  2016. return -ENODEV;
  2017. lock_kernel();
  2018. base = block->base;
  2019. atomic_inc(&block->open_count);
  2020. if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
  2021. rc = -ENODEV;
  2022. goto unlock;
  2023. }
  2024. if (!try_module_get(base->discipline->owner)) {
  2025. rc = -EINVAL;
  2026. goto unlock;
  2027. }
  2028. if (dasd_probeonly) {
  2029. dev_info(&base->cdev->dev,
  2030. "Accessing the DASD failed because it is in "
  2031. "probeonly mode\n");
  2032. rc = -EPERM;
  2033. goto out;
  2034. }
  2035. if (base->state <= DASD_STATE_BASIC) {
  2036. DBF_DEV_EVENT(DBF_ERR, base, " %s",
  2037. " Cannot open unrecognized device");
  2038. rc = -ENODEV;
  2039. goto out;
  2040. }
  2041. if ((mode & FMODE_WRITE) &&
  2042. (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
  2043. (base->features & DASD_FEATURE_READONLY))) {
  2044. rc = -EROFS;
  2045. goto out;
  2046. }
  2047. unlock_kernel();
  2048. return 0;
  2049. out:
  2050. module_put(base->discipline->owner);
  2051. unlock:
  2052. atomic_dec(&block->open_count);
  2053. unlock_kernel();
  2054. return rc;
  2055. }
  2056. static int dasd_release(struct gendisk *disk, fmode_t mode)
  2057. {
  2058. struct dasd_block *block = disk->private_data;
  2059. lock_kernel();
  2060. atomic_dec(&block->open_count);
  2061. module_put(block->base->discipline->owner);
  2062. unlock_kernel();
  2063. return 0;
  2064. }
  2065. /*
  2066. * Return disk geometry.
  2067. */
  2068. static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  2069. {
  2070. struct dasd_block *block;
  2071. struct dasd_device *base;
  2072. block = bdev->bd_disk->private_data;
  2073. if (!block)
  2074. return -ENODEV;
  2075. base = block->base;
  2076. if (!base->discipline ||
  2077. !base->discipline->fill_geometry)
  2078. return -EINVAL;
  2079. base->discipline->fill_geometry(block, geo);
  2080. geo->start = get_start_sect(bdev) >> block->s2b_shift;
  2081. return 0;
  2082. }
  2083. const struct block_device_operations
  2084. dasd_device_operations = {
  2085. .owner = THIS_MODULE,
  2086. .open = dasd_open,
  2087. .release = dasd_release,
  2088. .ioctl = dasd_ioctl,
  2089. .compat_ioctl = dasd_ioctl,
  2090. .getgeo = dasd_getgeo,
  2091. };
  2092. /*******************************************************************************
  2093. * end of block device operations
  2094. */
  2095. static void
  2096. dasd_exit(void)
  2097. {
  2098. #ifdef CONFIG_PROC_FS
  2099. dasd_proc_exit();
  2100. #endif
  2101. dasd_eer_exit();
  2102. if (dasd_page_cache != NULL) {
  2103. kmem_cache_destroy(dasd_page_cache);
  2104. dasd_page_cache = NULL;
  2105. }
  2106. dasd_gendisk_exit();
  2107. dasd_devmap_exit();
  2108. if (dasd_debug_area != NULL) {
  2109. debug_unregister(dasd_debug_area);
  2110. dasd_debug_area = NULL;
  2111. }
  2112. }
  2113. /*
  2114. * SECTION: common functions for ccw_driver use
  2115. */
  2116. /*
  2117. * Is the device read-only?
  2118. * Note that this function does not report the setting of the
  2119. * readonly device attribute, but how it is configured in z/VM.
  2120. */
  2121. int dasd_device_is_ro(struct dasd_device *device)
  2122. {
  2123. struct ccw_dev_id dev_id;
  2124. struct diag210 diag_data;
  2125. int rc;
  2126. if (!MACHINE_IS_VM)
  2127. return 0;
  2128. ccw_device_get_id(device->cdev, &dev_id);
  2129. memset(&diag_data, 0, sizeof(diag_data));
  2130. diag_data.vrdcdvno = dev_id.devno;
  2131. diag_data.vrdclen = sizeof(diag_data);
  2132. rc = diag210(&diag_data);
  2133. if (rc == 0 || rc == 2) {
  2134. return diag_data.vrdcvfla & 0x80;
  2135. } else {
  2136. DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
  2137. dev_id.devno, rc);
  2138. return 0;
  2139. }
  2140. }
  2141. EXPORT_SYMBOL_GPL(dasd_device_is_ro);
  2142. static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
  2143. {
  2144. struct ccw_device *cdev = data;
  2145. int ret;
  2146. ret = ccw_device_set_online(cdev);
  2147. if (ret)
  2148. pr_warning("%s: Setting the DASD online failed with rc=%d\n",
  2149. dev_name(&cdev->dev), ret);
  2150. }
  2151. /*
  2152. * Initial attempt at a probe function. this can be simplified once
  2153. * the other detection code is gone.
  2154. */
  2155. int dasd_generic_probe(struct ccw_device *cdev,
  2156. struct dasd_discipline *discipline)
  2157. {
  2158. int ret;
  2159. ret = dasd_add_sysfs_files(cdev);
  2160. if (ret) {
  2161. DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
  2162. "dasd_generic_probe: could not add "
  2163. "sysfs entries");
  2164. return ret;
  2165. }
  2166. cdev->handler = &dasd_int_handler;
  2167. /*
  2168. * Automatically online either all dasd devices (dasd_autodetect)
  2169. * or all devices specified with dasd= parameters during
  2170. * initial probe.
  2171. */
  2172. if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
  2173. (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
  2174. async_schedule(dasd_generic_auto_online, cdev);
  2175. return 0;
  2176. }
  2177. /*
  2178. * This will one day be called from a global not_oper handler.
  2179. * It is also used by driver_unregister during module unload.
  2180. */
  2181. void dasd_generic_remove(struct ccw_device *cdev)
  2182. {
  2183. struct dasd_device *device;
  2184. struct dasd_block *block;
  2185. cdev->handler = NULL;
  2186. dasd_remove_sysfs_files(cdev);
  2187. device = dasd_device_from_cdev(cdev);
  2188. if (IS_ERR(device))
  2189. return;
  2190. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  2191. /* Already doing offline processing */
  2192. dasd_put_device(device);
  2193. return;
  2194. }
  2195. /*
  2196. * This device is removed unconditionally. Set offline
  2197. * flag to prevent dasd_open from opening it while it is
  2198. * no quite down yet.
  2199. */
  2200. dasd_set_target_state(device, DASD_STATE_NEW);
  2201. /* dasd_delete_device destroys the device reference. */
  2202. block = device->block;
  2203. device->block = NULL;
  2204. dasd_delete_device(device);
  2205. /*
  2206. * life cycle of block is bound to device, so delete it after
  2207. * device was safely removed
  2208. */
  2209. if (block)
  2210. dasd_free_block(block);
  2211. }
  2212. /*
  2213. * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
  2214. * the device is detected for the first time and is supposed to be used
  2215. * or the user has started activation through sysfs.
  2216. */
  2217. int dasd_generic_set_online(struct ccw_device *cdev,
  2218. struct dasd_discipline *base_discipline)
  2219. {
  2220. struct dasd_discipline *discipline;
  2221. struct dasd_device *device;
  2222. int rc;
  2223. /* first online clears initial online feature flag */
  2224. dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
  2225. device = dasd_create_device(cdev);
  2226. if (IS_ERR(device))
  2227. return PTR_ERR(device);
  2228. discipline = base_discipline;
  2229. if (device->features & DASD_FEATURE_USEDIAG) {
  2230. if (!dasd_diag_discipline_pointer) {
  2231. pr_warning("%s Setting the DASD online failed because "
  2232. "of missing DIAG discipline\n",
  2233. dev_name(&cdev->dev));
  2234. dasd_delete_device(device);
  2235. return -ENODEV;
  2236. }
  2237. discipline = dasd_diag_discipline_pointer;
  2238. }
  2239. if (!try_module_get(base_discipline->owner)) {
  2240. dasd_delete_device(device);
  2241. return -EINVAL;
  2242. }
  2243. if (!try_module_get(discipline->owner)) {
  2244. module_put(base_discipline->owner);
  2245. dasd_delete_device(device);
  2246. return -EINVAL;
  2247. }
  2248. device->base_discipline = base_discipline;
  2249. device->discipline = discipline;
  2250. /* check_device will allocate block device if necessary */
  2251. rc = discipline->check_device(device);
  2252. if (rc) {
  2253. pr_warning("%s Setting the DASD online with discipline %s "
  2254. "failed with rc=%i\n",
  2255. dev_name(&cdev->dev), discipline->name, rc);
  2256. module_put(discipline->owner);
  2257. module_put(base_discipline->owner);
  2258. dasd_delete_device(device);
  2259. return rc;
  2260. }
  2261. dasd_set_target_state(device, DASD_STATE_ONLINE);
  2262. if (device->state <= DASD_STATE_KNOWN) {
  2263. pr_warning("%s Setting the DASD online failed because of a "
  2264. "missing discipline\n", dev_name(&cdev->dev));
  2265. rc = -ENODEV;
  2266. dasd_set_target_state(device, DASD_STATE_NEW);
  2267. if (device->block)
  2268. dasd_free_block(device->block);
  2269. dasd_delete_device(device);
  2270. } else
  2271. pr_debug("dasd_generic device %s found\n",
  2272. dev_name(&cdev->dev));
  2273. wait_event(dasd_init_waitq, _wait_for_device(device));
  2274. dasd_put_device(device);
  2275. return rc;
  2276. }
  2277. int dasd_generic_set_offline(struct ccw_device *cdev)
  2278. {
  2279. struct dasd_device *device;
  2280. struct dasd_block *block;
  2281. int max_count, open_count;
  2282. device = dasd_device_from_cdev(cdev);
  2283. if (IS_ERR(device))
  2284. return PTR_ERR(device);
  2285. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  2286. /* Already doing offline processing */
  2287. dasd_put_device(device);
  2288. return 0;
  2289. }
  2290. /*
  2291. * We must make sure that this device is currently not in use.
  2292. * The open_count is increased for every opener, that includes
  2293. * the blkdev_get in dasd_scan_partitions. We are only interested
  2294. * in the other openers.
  2295. */
  2296. if (device->block) {
  2297. max_count = device->block->bdev ? 0 : -1;
  2298. open_count = atomic_read(&device->block->open_count);
  2299. if (open_count > max_count) {
  2300. if (open_count > 0)
  2301. pr_warning("%s: The DASD cannot be set offline "
  2302. "with open count %i\n",
  2303. dev_name(&cdev->dev), open_count);
  2304. else
  2305. pr_warning("%s: The DASD cannot be set offline "
  2306. "while it is in use\n",
  2307. dev_name(&cdev->dev));
  2308. clear_bit(DASD_FLAG_OFFLINE, &device->flags);
  2309. dasd_put_device(device);
  2310. return -EBUSY;
  2311. }
  2312. }
  2313. dasd_set_target_state(device, DASD_STATE_NEW);
  2314. /* dasd_delete_device destroys the device reference. */
  2315. block = device->block;
  2316. device->block = NULL;
  2317. dasd_delete_device(device);
  2318. /*
  2319. * life cycle of block is bound to device, so delete it after
  2320. * device was safely removed
  2321. */
  2322. if (block)
  2323. dasd_free_block(block);
  2324. return 0;
  2325. }
  2326. int dasd_generic_notify(struct ccw_device *cdev, int event)
  2327. {
  2328. struct dasd_device *device;
  2329. struct dasd_ccw_req *cqr;
  2330. int ret;
  2331. device = dasd_device_from_cdev_locked(cdev);
  2332. if (IS_ERR(device))
  2333. return 0;
  2334. ret = 0;
  2335. switch (event) {
  2336. case CIO_GONE:
  2337. case CIO_BOXED:
  2338. case CIO_NO_PATH:
  2339. /* First of all call extended error reporting. */
  2340. dasd_eer_write(device, NULL, DASD_EER_NOPATH);
  2341. if (device->state < DASD_STATE_BASIC)
  2342. break;
  2343. /* Device is active. We want to keep it. */
  2344. list_for_each_entry(cqr, &device->ccw_queue, devlist)
  2345. if (cqr->status == DASD_CQR_IN_IO) {
  2346. cqr->status = DASD_CQR_QUEUED;
  2347. cqr->retries++;
  2348. }
  2349. dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
  2350. dasd_device_clear_timer(device);
  2351. dasd_schedule_device_bh(device);
  2352. ret = 1;
  2353. break;
  2354. case CIO_OPER:
  2355. /* FIXME: add a sanity check. */
  2356. dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
  2357. if (device->stopped & DASD_UNRESUMED_PM) {
  2358. dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
  2359. dasd_restore_device(device);
  2360. ret = 1;
  2361. break;
  2362. }
  2363. dasd_schedule_device_bh(device);
  2364. if (device->block)
  2365. dasd_schedule_block_bh(device->block);
  2366. ret = 1;
  2367. break;
  2368. }
  2369. dasd_put_device(device);
  2370. return ret;
  2371. }
  2372. int dasd_generic_pm_freeze(struct ccw_device *cdev)
  2373. {
  2374. struct dasd_ccw_req *cqr, *n;
  2375. int rc;
  2376. struct list_head freeze_queue;
  2377. struct dasd_device *device = dasd_device_from_cdev(cdev);
  2378. if (IS_ERR(device))
  2379. return PTR_ERR(device);
  2380. /* disallow new I/O */
  2381. dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
  2382. /* clear active requests */
  2383. INIT_LIST_HEAD(&freeze_queue);
  2384. spin_lock_irq(get_ccwdev_lock(cdev));
  2385. rc = 0;
  2386. list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
  2387. /* Check status and move request to flush_queue */
  2388. if (cqr->status == DASD_CQR_IN_IO) {
  2389. rc = device->discipline->term_IO(cqr);
  2390. if (rc) {
  2391. /* unable to terminate requeust */
  2392. dev_err(&device->cdev->dev,
  2393. "Unable to terminate request %p "
  2394. "on suspend\n", cqr);
  2395. spin_unlock_irq(get_ccwdev_lock(cdev));
  2396. dasd_put_device(device);
  2397. return rc;
  2398. }
  2399. }
  2400. list_move_tail(&cqr->devlist, &freeze_queue);
  2401. }
  2402. spin_unlock_irq(get_ccwdev_lock(cdev));
  2403. list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
  2404. wait_event(dasd_flush_wq,
  2405. (cqr->status != DASD_CQR_CLEAR_PENDING));
  2406. if (cqr->status == DASD_CQR_CLEARED)
  2407. cqr->status = DASD_CQR_QUEUED;
  2408. }
  2409. /* move freeze_queue to start of the ccw_queue */
  2410. spin_lock_irq(get_ccwdev_lock(cdev));
  2411. list_splice_tail(&freeze_queue, &device->ccw_queue);
  2412. spin_unlock_irq(get_ccwdev_lock(cdev));
  2413. if (device->discipline->freeze)
  2414. rc = device->discipline->freeze(device);
  2415. dasd_put_device(device);
  2416. return rc;
  2417. }
  2418. EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
  2419. int dasd_generic_restore_device(struct ccw_device *cdev)
  2420. {
  2421. struct dasd_device *device = dasd_device_from_cdev(cdev);
  2422. int rc = 0;
  2423. if (IS_ERR(device))
  2424. return PTR_ERR(device);
  2425. /* allow new IO again */
  2426. dasd_device_remove_stop_bits(device,
  2427. (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
  2428. dasd_schedule_device_bh(device);
  2429. /*
  2430. * call discipline restore function
  2431. * if device is stopped do nothing e.g. for disconnected devices
  2432. */
  2433. if (device->discipline->restore && !(device->stopped))
  2434. rc = device->discipline->restore(device);
  2435. if (rc || device->stopped)
  2436. /*
  2437. * if the resume failed for the DASD we put it in
  2438. * an UNRESUMED stop state
  2439. */
  2440. device->stopped |= DASD_UNRESUMED_PM;
  2441. if (device->block)
  2442. dasd_schedule_block_bh(device->block);
  2443. dasd_put_device(device);
  2444. return 0;
  2445. }
  2446. EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
  2447. static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
  2448. void *rdc_buffer,
  2449. int rdc_buffer_size,
  2450. int magic)
  2451. {
  2452. struct dasd_ccw_req *cqr;
  2453. struct ccw1 *ccw;
  2454. unsigned long *idaw;
  2455. cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
  2456. if (IS_ERR(cqr)) {
  2457. /* internal error 13 - Allocating the RDC request failed*/
  2458. dev_err(&device->cdev->dev,
  2459. "An error occurred in the DASD device driver, "
  2460. "reason=%s\n", "13");
  2461. return cqr;
  2462. }
  2463. ccw = cqr->cpaddr;
  2464. ccw->cmd_code = CCW_CMD_RDC;
  2465. if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
  2466. idaw = (unsigned long *) (cqr->data);
  2467. ccw->cda = (__u32)(addr_t) idaw;
  2468. ccw->flags = CCW_FLAG_IDA;
  2469. idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
  2470. } else {
  2471. ccw->cda = (__u32)(addr_t) rdc_buffer;
  2472. ccw->flags = 0;
  2473. }
  2474. ccw->count = rdc_buffer_size;
  2475. cqr->startdev = device;
  2476. cqr->memdev = device;
  2477. cqr->expires = 10*HZ;
  2478. cqr->retries = 256;
  2479. cqr->buildclk = get_clock();
  2480. cqr->status = DASD_CQR_FILLED;
  2481. return cqr;
  2482. }
  2483. int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
  2484. void *rdc_buffer, int rdc_buffer_size)
  2485. {
  2486. int ret;
  2487. struct dasd_ccw_req *cqr;
  2488. cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
  2489. magic);
  2490. if (IS_ERR(cqr))
  2491. return PTR_ERR(cqr);
  2492. ret = dasd_sleep_on(cqr);
  2493. dasd_sfree_request(cqr, cqr->memdev);
  2494. return ret;
  2495. }
  2496. EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
  2497. /*
  2498. * In command mode and transport mode we need to look for sense
  2499. * data in different places. The sense data itself is allways
  2500. * an array of 32 bytes, so we can unify the sense data access
  2501. * for both modes.
  2502. */
  2503. char *dasd_get_sense(struct irb *irb)
  2504. {
  2505. struct tsb *tsb = NULL;
  2506. char *sense = NULL;
  2507. if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
  2508. if (irb->scsw.tm.tcw)
  2509. tsb = tcw_get_tsb((struct tcw *)(unsigned long)
  2510. irb->scsw.tm.tcw);
  2511. if (tsb && tsb->length == 64 && tsb->flags)
  2512. switch (tsb->flags & 0x07) {
  2513. case 1: /* tsa_iostat */
  2514. sense = tsb->tsa.iostat.sense;
  2515. break;
  2516. case 2: /* tsa_ddpc */
  2517. sense = tsb->tsa.ddpc.sense;
  2518. break;
  2519. default:
  2520. /* currently we don't use interrogate data */
  2521. break;
  2522. }
  2523. } else if (irb->esw.esw0.erw.cons) {
  2524. sense = irb->ecw;
  2525. }
  2526. return sense;
  2527. }
  2528. EXPORT_SYMBOL_GPL(dasd_get_sense);
  2529. static int __init dasd_init(void)
  2530. {
  2531. int rc;
  2532. init_waitqueue_head(&dasd_init_waitq);
  2533. init_waitqueue_head(&dasd_flush_wq);
  2534. init_waitqueue_head(&generic_waitq);
  2535. /* register 'common' DASD debug area, used for all DBF_XXX calls */
  2536. dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
  2537. if (dasd_debug_area == NULL) {
  2538. rc = -ENOMEM;
  2539. goto failed;
  2540. }
  2541. debug_register_view(dasd_debug_area, &debug_sprintf_view);
  2542. debug_set_level(dasd_debug_area, DBF_WARNING);
  2543. DBF_EVENT(DBF_EMERG, "%s", "debug area created");
  2544. dasd_diag_discipline_pointer = NULL;
  2545. rc = dasd_devmap_init();
  2546. if (rc)
  2547. goto failed;
  2548. rc = dasd_gendisk_init();
  2549. if (rc)
  2550. goto failed;
  2551. rc = dasd_parse();
  2552. if (rc)
  2553. goto failed;
  2554. rc = dasd_eer_init();
  2555. if (rc)
  2556. goto failed;
  2557. #ifdef CONFIG_PROC_FS
  2558. rc = dasd_proc_init();
  2559. if (rc)
  2560. goto failed;
  2561. #endif
  2562. return 0;
  2563. failed:
  2564. pr_info("The DASD device driver could not be initialized\n");
  2565. dasd_exit();
  2566. return rc;
  2567. }
  2568. module_init(dasd_init);
  2569. module_exit(dasd_exit);
  2570. EXPORT_SYMBOL(dasd_debug_area);
  2571. EXPORT_SYMBOL(dasd_diag_discipline_pointer);
  2572. EXPORT_SYMBOL(dasd_add_request_head);
  2573. EXPORT_SYMBOL(dasd_add_request_tail);
  2574. EXPORT_SYMBOL(dasd_cancel_req);
  2575. EXPORT_SYMBOL(dasd_device_clear_timer);
  2576. EXPORT_SYMBOL(dasd_block_clear_timer);
  2577. EXPORT_SYMBOL(dasd_enable_device);
  2578. EXPORT_SYMBOL(dasd_int_handler);
  2579. EXPORT_SYMBOL(dasd_kfree_request);
  2580. EXPORT_SYMBOL(dasd_kick_device);
  2581. EXPORT_SYMBOL(dasd_kmalloc_request);
  2582. EXPORT_SYMBOL(dasd_schedule_device_bh);
  2583. EXPORT_SYMBOL(dasd_schedule_block_bh);
  2584. EXPORT_SYMBOL(dasd_set_target_state);
  2585. EXPORT_SYMBOL(dasd_device_set_timer);
  2586. EXPORT_SYMBOL(dasd_block_set_timer);
  2587. EXPORT_SYMBOL(dasd_sfree_request);
  2588. EXPORT_SYMBOL(dasd_sleep_on);
  2589. EXPORT_SYMBOL(dasd_sleep_on_immediatly);
  2590. EXPORT_SYMBOL(dasd_sleep_on_interruptible);
  2591. EXPORT_SYMBOL(dasd_smalloc_request);
  2592. EXPORT_SYMBOL(dasd_start_IO);
  2593. EXPORT_SYMBOL(dasd_term_IO);
  2594. EXPORT_SYMBOL_GPL(dasd_generic_probe);
  2595. EXPORT_SYMBOL_GPL(dasd_generic_remove);
  2596. EXPORT_SYMBOL_GPL(dasd_generic_notify);
  2597. EXPORT_SYMBOL_GPL(dasd_generic_set_online);
  2598. EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
  2599. EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
  2600. EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
  2601. EXPORT_SYMBOL_GPL(dasd_alloc_block);
  2602. EXPORT_SYMBOL_GPL(dasd_free_block);