dasd.c 75 KB

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