dasd.c 109 KB

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