dasd.c 107 KB

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