dasd.c 108 KB

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