dasd.c 107 KB

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