dasd.c 103 KB

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