dasd.c 75 KB

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