dasd.c 99 KB

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