dasd.c 94 KB

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