dasd.c 94 KB

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