dasd.c 104 KB

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