dasd.c 109 KB

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