dasd.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192
  1. /*
  2. * File...........: linux/drivers/s390/block/dasd.c
  3. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  4. * Horst Hummel <Horst.Hummel@de.ibm.com>
  5. * Carsten Otte <Cotte@de.ibm.com>
  6. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * Bugreports.to..: <Linux390@de.ibm.com>
  8. * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
  9. *
  10. */
  11. #include <linux/config.h>
  12. #include <linux/kmod.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ctype.h>
  16. #include <linux/major.h>
  17. #include <linux/slab.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/hdreg.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/ebcdic.h>
  22. #include <asm/idals.h>
  23. #include <asm/todclk.h>
  24. /* This is ugly... */
  25. #define PRINTK_HEADER "dasd:"
  26. #include "dasd_int.h"
  27. /*
  28. * SECTION: Constant definitions to be used within this file
  29. */
  30. #define DASD_CHANQ_MAX_SIZE 4
  31. /*
  32. * SECTION: exported variables of dasd.c
  33. */
  34. debug_info_t *dasd_debug_area;
  35. struct dasd_discipline *dasd_diag_discipline_pointer;
  36. MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
  37. MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
  38. " Copyright 2000 IBM Corporation");
  39. MODULE_SUPPORTED_DEVICE("dasd");
  40. MODULE_LICENSE("GPL");
  41. /*
  42. * SECTION: prototypes for static functions of dasd.c
  43. */
  44. static int dasd_alloc_queue(struct dasd_device * device);
  45. static void dasd_setup_queue(struct dasd_device * device);
  46. static void dasd_free_queue(struct dasd_device * device);
  47. static void dasd_flush_request_queue(struct dasd_device *);
  48. static void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
  49. static void dasd_flush_ccw_queue(struct dasd_device *, int);
  50. static void dasd_tasklet(struct dasd_device *);
  51. static void do_kick_device(void *data);
  52. /*
  53. * SECTION: Operations on the device structure.
  54. */
  55. static wait_queue_head_t dasd_init_waitq;
  56. /*
  57. * Allocate memory for a new device structure.
  58. */
  59. struct dasd_device *
  60. dasd_alloc_device(void)
  61. {
  62. struct dasd_device *device;
  63. device = kzalloc(sizeof (struct dasd_device), GFP_ATOMIC);
  64. if (device == NULL)
  65. return ERR_PTR(-ENOMEM);
  66. /* open_count = 0 means device online but not in use */
  67. atomic_set(&device->open_count, -1);
  68. /* Get two pages for normal block device operations. */
  69. device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
  70. if (device->ccw_mem == NULL) {
  71. kfree(device);
  72. return ERR_PTR(-ENOMEM);
  73. }
  74. /* Get one page for error recovery. */
  75. device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
  76. if (device->erp_mem == NULL) {
  77. free_pages((unsigned long) device->ccw_mem, 1);
  78. kfree(device);
  79. return ERR_PTR(-ENOMEM);
  80. }
  81. dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
  82. dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
  83. spin_lock_init(&device->mem_lock);
  84. spin_lock_init(&device->request_queue_lock);
  85. atomic_set (&device->tasklet_scheduled, 0);
  86. tasklet_init(&device->tasklet,
  87. (void (*)(unsigned long)) dasd_tasklet,
  88. (unsigned long) device);
  89. INIT_LIST_HEAD(&device->ccw_queue);
  90. init_timer(&device->timer);
  91. INIT_WORK(&device->kick_work, do_kick_device, device);
  92. device->state = DASD_STATE_NEW;
  93. device->target = DASD_STATE_NEW;
  94. return device;
  95. }
  96. /*
  97. * Free memory of a device structure.
  98. */
  99. void
  100. dasd_free_device(struct dasd_device *device)
  101. {
  102. kfree(device->private);
  103. free_page((unsigned long) device->erp_mem);
  104. free_pages((unsigned long) device->ccw_mem, 1);
  105. kfree(device);
  106. }
  107. /*
  108. * Make a new device known to the system.
  109. */
  110. static inline int
  111. dasd_state_new_to_known(struct dasd_device *device)
  112. {
  113. int rc;
  114. /*
  115. * As long as the device is not in state DASD_STATE_NEW we want to
  116. * keep the reference count > 0.
  117. */
  118. dasd_get_device(device);
  119. rc = dasd_alloc_queue(device);
  120. if (rc) {
  121. dasd_put_device(device);
  122. return rc;
  123. }
  124. device->state = DASD_STATE_KNOWN;
  125. return 0;
  126. }
  127. /*
  128. * Let the system forget about a device.
  129. */
  130. static inline void
  131. dasd_state_known_to_new(struct dasd_device * device)
  132. {
  133. /* Disable extended error reporting for this device. */
  134. dasd_eer_disable(device);
  135. /* Forget the discipline information. */
  136. if (device->discipline)
  137. module_put(device->discipline->owner);
  138. device->discipline = NULL;
  139. if (device->base_discipline)
  140. module_put(device->base_discipline->owner);
  141. device->base_discipline = NULL;
  142. device->state = DASD_STATE_NEW;
  143. dasd_free_queue(device);
  144. /* Give up reference we took in dasd_state_new_to_known. */
  145. dasd_put_device(device);
  146. }
  147. /*
  148. * Request the irq line for the device.
  149. */
  150. static inline int
  151. dasd_state_known_to_basic(struct dasd_device * device)
  152. {
  153. int rc;
  154. /* Allocate and register gendisk structure. */
  155. rc = dasd_gendisk_alloc(device);
  156. if (rc)
  157. return rc;
  158. /* register 'device' debug area, used for all DBF_DEV_XXX calls */
  159. device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
  160. 8 * sizeof (long));
  161. debug_register_view(device->debug_area, &debug_sprintf_view);
  162. debug_set_level(device->debug_area, DBF_EMERG);
  163. DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
  164. device->state = DASD_STATE_BASIC;
  165. return 0;
  166. }
  167. /*
  168. * Release the irq line for the device. Terminate any running i/o.
  169. */
  170. static inline void
  171. dasd_state_basic_to_known(struct dasd_device * device)
  172. {
  173. dasd_gendisk_free(device);
  174. dasd_flush_ccw_queue(device, 1);
  175. DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
  176. if (device->debug_area != NULL) {
  177. debug_unregister(device->debug_area);
  178. device->debug_area = NULL;
  179. }
  180. device->state = DASD_STATE_KNOWN;
  181. }
  182. /*
  183. * Do the initial analysis. The do_analysis function may return
  184. * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
  185. * until the discipline decides to continue the startup sequence
  186. * by calling the function dasd_change_state. The eckd disciplines
  187. * uses this to start a ccw that detects the format. The completion
  188. * interrupt for this detection ccw uses the kernel event daemon to
  189. * trigger the call to dasd_change_state. All this is done in the
  190. * discipline code, see dasd_eckd.c.
  191. * After the analysis ccw is done (do_analysis returned 0) the block
  192. * device is setup.
  193. * In case the analysis returns an error, the device setup is stopped
  194. * (a fake disk was already added to allow formatting).
  195. */
  196. static inline int
  197. dasd_state_basic_to_ready(struct dasd_device * device)
  198. {
  199. int rc;
  200. rc = 0;
  201. if (device->discipline->do_analysis != NULL)
  202. rc = device->discipline->do_analysis(device);
  203. if (rc) {
  204. if (rc != -EAGAIN)
  205. device->state = DASD_STATE_UNFMT;
  206. return rc;
  207. }
  208. /* make disk known with correct capacity */
  209. dasd_setup_queue(device);
  210. set_capacity(device->gdp, device->blocks << device->s2b_shift);
  211. device->state = DASD_STATE_READY;
  212. rc = dasd_scan_partitions(device);
  213. if (rc)
  214. device->state = DASD_STATE_BASIC;
  215. return rc;
  216. }
  217. /*
  218. * Remove device from block device layer. Destroy dirty buffers.
  219. * Forget format information. Check if the target level is basic
  220. * and if it is create fake disk for formatting.
  221. */
  222. static inline void
  223. dasd_state_ready_to_basic(struct dasd_device * device)
  224. {
  225. dasd_flush_ccw_queue(device, 0);
  226. dasd_destroy_partitions(device);
  227. dasd_flush_request_queue(device);
  228. device->blocks = 0;
  229. device->bp_block = 0;
  230. device->s2b_shift = 0;
  231. device->state = DASD_STATE_BASIC;
  232. }
  233. /*
  234. * Back to basic.
  235. */
  236. static inline void
  237. dasd_state_unfmt_to_basic(struct dasd_device * device)
  238. {
  239. device->state = DASD_STATE_BASIC;
  240. }
  241. /*
  242. * Make the device online and schedule the bottom half to start
  243. * the requeueing of requests from the linux request queue to the
  244. * ccw queue.
  245. */
  246. static inline int
  247. dasd_state_ready_to_online(struct dasd_device * device)
  248. {
  249. device->state = DASD_STATE_ONLINE;
  250. dasd_schedule_bh(device);
  251. return 0;
  252. }
  253. /*
  254. * Stop the requeueing of requests again.
  255. */
  256. static inline void
  257. dasd_state_online_to_ready(struct dasd_device * device)
  258. {
  259. device->state = DASD_STATE_READY;
  260. }
  261. /*
  262. * Device startup state changes.
  263. */
  264. static inline int
  265. dasd_increase_state(struct dasd_device *device)
  266. {
  267. int rc;
  268. rc = 0;
  269. if (device->state == DASD_STATE_NEW &&
  270. device->target >= DASD_STATE_KNOWN)
  271. rc = dasd_state_new_to_known(device);
  272. if (!rc &&
  273. device->state == DASD_STATE_KNOWN &&
  274. device->target >= DASD_STATE_BASIC)
  275. rc = dasd_state_known_to_basic(device);
  276. if (!rc &&
  277. device->state == DASD_STATE_BASIC &&
  278. device->target >= DASD_STATE_READY)
  279. rc = dasd_state_basic_to_ready(device);
  280. if (!rc &&
  281. device->state == DASD_STATE_UNFMT &&
  282. device->target > DASD_STATE_UNFMT)
  283. rc = -EPERM;
  284. if (!rc &&
  285. device->state == DASD_STATE_READY &&
  286. device->target >= DASD_STATE_ONLINE)
  287. rc = dasd_state_ready_to_online(device);
  288. return rc;
  289. }
  290. /*
  291. * Device shutdown state changes.
  292. */
  293. static inline int
  294. dasd_decrease_state(struct dasd_device *device)
  295. {
  296. if (device->state == DASD_STATE_ONLINE &&
  297. device->target <= DASD_STATE_READY)
  298. dasd_state_online_to_ready(device);
  299. if (device->state == DASD_STATE_READY &&
  300. device->target <= DASD_STATE_BASIC)
  301. dasd_state_ready_to_basic(device);
  302. if (device->state == DASD_STATE_UNFMT &&
  303. device->target <= DASD_STATE_BASIC)
  304. dasd_state_unfmt_to_basic(device);
  305. if (device->state == DASD_STATE_BASIC &&
  306. device->target <= DASD_STATE_KNOWN)
  307. dasd_state_basic_to_known(device);
  308. if (device->state == DASD_STATE_KNOWN &&
  309. device->target <= DASD_STATE_NEW)
  310. dasd_state_known_to_new(device);
  311. return 0;
  312. }
  313. /*
  314. * This is the main startup/shutdown routine.
  315. */
  316. static void
  317. dasd_change_state(struct dasd_device *device)
  318. {
  319. int rc;
  320. if (device->state == device->target)
  321. /* Already where we want to go today... */
  322. return;
  323. if (device->state < device->target)
  324. rc = dasd_increase_state(device);
  325. else
  326. rc = dasd_decrease_state(device);
  327. if (rc && rc != -EAGAIN)
  328. device->target = device->state;
  329. if (device->state == device->target)
  330. wake_up(&dasd_init_waitq);
  331. }
  332. /*
  333. * Kick starter for devices that did not complete the startup/shutdown
  334. * procedure or were sleeping because of a pending state.
  335. * dasd_kick_device will schedule a call do do_kick_device to the kernel
  336. * event daemon.
  337. */
  338. static void
  339. do_kick_device(void *data)
  340. {
  341. struct dasd_device *device;
  342. device = (struct dasd_device *) data;
  343. dasd_change_state(device);
  344. dasd_schedule_bh(device);
  345. dasd_put_device(device);
  346. }
  347. void
  348. dasd_kick_device(struct dasd_device *device)
  349. {
  350. dasd_get_device(device);
  351. /* queue call to dasd_kick_device to the kernel event daemon. */
  352. schedule_work(&device->kick_work);
  353. }
  354. /*
  355. * Set the target state for a device and starts the state change.
  356. */
  357. void
  358. dasd_set_target_state(struct dasd_device *device, int target)
  359. {
  360. /* If we are in probeonly mode stop at DASD_STATE_READY. */
  361. if (dasd_probeonly && target > DASD_STATE_READY)
  362. target = DASD_STATE_READY;
  363. if (device->target != target) {
  364. if (device->state == target)
  365. wake_up(&dasd_init_waitq);
  366. device->target = target;
  367. }
  368. if (device->state != device->target)
  369. dasd_change_state(device);
  370. }
  371. /*
  372. * Enable devices with device numbers in [from..to].
  373. */
  374. static inline int
  375. _wait_for_device(struct dasd_device *device)
  376. {
  377. return (device->state == device->target);
  378. }
  379. void
  380. dasd_enable_device(struct dasd_device *device)
  381. {
  382. dasd_set_target_state(device, DASD_STATE_ONLINE);
  383. if (device->state <= DASD_STATE_KNOWN)
  384. /* No discipline for device found. */
  385. dasd_set_target_state(device, DASD_STATE_NEW);
  386. /* Now wait for the devices to come up. */
  387. wait_event(dasd_init_waitq, _wait_for_device(device));
  388. }
  389. /*
  390. * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
  391. */
  392. #ifdef CONFIG_DASD_PROFILE
  393. struct dasd_profile_info_t dasd_global_profile;
  394. unsigned int dasd_profile_level = DASD_PROFILE_OFF;
  395. /*
  396. * Increments counter in global and local profiling structures.
  397. */
  398. #define dasd_profile_counter(value, counter, device) \
  399. { \
  400. int index; \
  401. for (index = 0; index < 31 && value >> (2+index); index++); \
  402. dasd_global_profile.counter[index]++; \
  403. device->profile.counter[index]++; \
  404. }
  405. /*
  406. * Add profiling information for cqr before execution.
  407. */
  408. static inline void
  409. dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
  410. struct request *req)
  411. {
  412. struct list_head *l;
  413. unsigned int counter;
  414. if (dasd_profile_level != DASD_PROFILE_ON)
  415. return;
  416. /* count the length of the chanq for statistics */
  417. counter = 0;
  418. list_for_each(l, &device->ccw_queue)
  419. if (++counter >= 31)
  420. break;
  421. dasd_global_profile.dasd_io_nr_req[counter]++;
  422. device->profile.dasd_io_nr_req[counter]++;
  423. }
  424. /*
  425. * Add profiling information for cqr after execution.
  426. */
  427. static inline void
  428. dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
  429. struct request *req)
  430. {
  431. long strtime, irqtime, endtime, tottime; /* in microseconds */
  432. long tottimeps, sectors;
  433. if (dasd_profile_level != DASD_PROFILE_ON)
  434. return;
  435. sectors = req->nr_sectors;
  436. if (!cqr->buildclk || !cqr->startclk ||
  437. !cqr->stopclk || !cqr->endclk ||
  438. !sectors)
  439. return;
  440. strtime = ((cqr->startclk - cqr->buildclk) >> 12);
  441. irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
  442. endtime = ((cqr->endclk - cqr->stopclk) >> 12);
  443. tottime = ((cqr->endclk - cqr->buildclk) >> 12);
  444. tottimeps = tottime / sectors;
  445. if (!dasd_global_profile.dasd_io_reqs)
  446. memset(&dasd_global_profile, 0,
  447. sizeof (struct dasd_profile_info_t));
  448. dasd_global_profile.dasd_io_reqs++;
  449. dasd_global_profile.dasd_io_sects += sectors;
  450. if (!device->profile.dasd_io_reqs)
  451. memset(&device->profile, 0,
  452. sizeof (struct dasd_profile_info_t));
  453. device->profile.dasd_io_reqs++;
  454. device->profile.dasd_io_sects += sectors;
  455. dasd_profile_counter(sectors, dasd_io_secs, device);
  456. dasd_profile_counter(tottime, dasd_io_times, device);
  457. dasd_profile_counter(tottimeps, dasd_io_timps, device);
  458. dasd_profile_counter(strtime, dasd_io_time1, device);
  459. dasd_profile_counter(irqtime, dasd_io_time2, device);
  460. dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
  461. dasd_profile_counter(endtime, dasd_io_time3, device);
  462. }
  463. #else
  464. #define dasd_profile_start(device, cqr, req) do {} while (0)
  465. #define dasd_profile_end(device, cqr, req) do {} while (0)
  466. #endif /* CONFIG_DASD_PROFILE */
  467. /*
  468. * Allocate memory for a channel program with 'cplength' channel
  469. * command words and 'datasize' additional space. There are two
  470. * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
  471. * memory and 2) dasd_smalloc_request uses the static ccw memory
  472. * that gets allocated for each device.
  473. */
  474. struct dasd_ccw_req *
  475. dasd_kmalloc_request(char *magic, int cplength, int datasize,
  476. struct dasd_device * device)
  477. {
  478. struct dasd_ccw_req *cqr;
  479. /* Sanity checks */
  480. BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
  481. (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  482. cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
  483. if (cqr == NULL)
  484. return ERR_PTR(-ENOMEM);
  485. cqr->cpaddr = NULL;
  486. if (cplength > 0) {
  487. cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
  488. GFP_ATOMIC | GFP_DMA);
  489. if (cqr->cpaddr == NULL) {
  490. kfree(cqr);
  491. return ERR_PTR(-ENOMEM);
  492. }
  493. }
  494. cqr->data = NULL;
  495. if (datasize > 0) {
  496. cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
  497. if (cqr->data == NULL) {
  498. kfree(cqr->cpaddr);
  499. kfree(cqr);
  500. return ERR_PTR(-ENOMEM);
  501. }
  502. }
  503. strncpy((char *) &cqr->magic, magic, 4);
  504. ASCEBC((char *) &cqr->magic, 4);
  505. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  506. dasd_get_device(device);
  507. return cqr;
  508. }
  509. struct dasd_ccw_req *
  510. dasd_smalloc_request(char *magic, int cplength, int datasize,
  511. struct dasd_device * device)
  512. {
  513. unsigned long flags;
  514. struct dasd_ccw_req *cqr;
  515. char *data;
  516. int size;
  517. /* Sanity checks */
  518. BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
  519. (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  520. size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
  521. if (cplength > 0)
  522. size += cplength * sizeof(struct ccw1);
  523. if (datasize > 0)
  524. size += datasize;
  525. spin_lock_irqsave(&device->mem_lock, flags);
  526. cqr = (struct dasd_ccw_req *)
  527. dasd_alloc_chunk(&device->ccw_chunks, size);
  528. spin_unlock_irqrestore(&device->mem_lock, flags);
  529. if (cqr == NULL)
  530. return ERR_PTR(-ENOMEM);
  531. memset(cqr, 0, sizeof(struct dasd_ccw_req));
  532. data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
  533. cqr->cpaddr = NULL;
  534. if (cplength > 0) {
  535. cqr->cpaddr = (struct ccw1 *) data;
  536. data += cplength*sizeof(struct ccw1);
  537. memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
  538. }
  539. cqr->data = NULL;
  540. if (datasize > 0) {
  541. cqr->data = data;
  542. memset(cqr->data, 0, datasize);
  543. }
  544. strncpy((char *) &cqr->magic, magic, 4);
  545. ASCEBC((char *) &cqr->magic, 4);
  546. set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  547. dasd_get_device(device);
  548. return cqr;
  549. }
  550. /*
  551. * Free memory of a channel program. This function needs to free all the
  552. * idal lists that might have been created by dasd_set_cda and the
  553. * struct dasd_ccw_req itself.
  554. */
  555. void
  556. dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
  557. {
  558. #ifdef CONFIG_64BIT
  559. struct ccw1 *ccw;
  560. /* Clear any idals used for the request. */
  561. ccw = cqr->cpaddr;
  562. do {
  563. clear_normalized_cda(ccw);
  564. } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
  565. #endif
  566. kfree(cqr->cpaddr);
  567. kfree(cqr->data);
  568. kfree(cqr);
  569. dasd_put_device(device);
  570. }
  571. void
  572. dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
  573. {
  574. unsigned long flags;
  575. spin_lock_irqsave(&device->mem_lock, flags);
  576. dasd_free_chunk(&device->ccw_chunks, cqr);
  577. spin_unlock_irqrestore(&device->mem_lock, flags);
  578. dasd_put_device(device);
  579. }
  580. /*
  581. * Check discipline magic in cqr.
  582. */
  583. static inline int
  584. dasd_check_cqr(struct dasd_ccw_req *cqr)
  585. {
  586. struct dasd_device *device;
  587. if (cqr == NULL)
  588. return -EINVAL;
  589. device = cqr->device;
  590. if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
  591. DEV_MESSAGE(KERN_WARNING, device,
  592. " dasd_ccw_req 0x%08x magic doesn't match"
  593. " discipline 0x%08x",
  594. cqr->magic,
  595. *(unsigned int *) device->discipline->name);
  596. return -EINVAL;
  597. }
  598. return 0;
  599. }
  600. /*
  601. * Terminate the current i/o and set the request to clear_pending.
  602. * Timer keeps device runnig.
  603. * ccw_device_clear can fail if the i/o subsystem
  604. * is in a bad mood.
  605. */
  606. int
  607. dasd_term_IO(struct dasd_ccw_req * cqr)
  608. {
  609. struct dasd_device *device;
  610. int retries, rc;
  611. /* Check the cqr */
  612. rc = dasd_check_cqr(cqr);
  613. if (rc)
  614. return rc;
  615. retries = 0;
  616. device = (struct dasd_device *) cqr->device;
  617. while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
  618. rc = ccw_device_clear(device->cdev, (long) cqr);
  619. switch (rc) {
  620. case 0: /* termination successful */
  621. cqr->retries--;
  622. cqr->status = DASD_CQR_CLEAR;
  623. cqr->stopclk = get_clock();
  624. DBF_DEV_EVENT(DBF_DEBUG, device,
  625. "terminate cqr %p successful",
  626. cqr);
  627. break;
  628. case -ENODEV:
  629. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  630. "device gone, retry");
  631. break;
  632. case -EIO:
  633. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  634. "I/O error, retry");
  635. break;
  636. case -EINVAL:
  637. case -EBUSY:
  638. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  639. "device busy, retry later");
  640. break;
  641. default:
  642. DEV_MESSAGE(KERN_ERR, device,
  643. "line %d unknown RC=%d, please "
  644. "report to linux390@de.ibm.com",
  645. __LINE__, rc);
  646. BUG();
  647. break;
  648. }
  649. retries++;
  650. }
  651. dasd_schedule_bh(device);
  652. return rc;
  653. }
  654. /*
  655. * Start the i/o. This start_IO can fail if the channel is really busy.
  656. * In that case set up a timer to start the request later.
  657. */
  658. int
  659. dasd_start_IO(struct dasd_ccw_req * cqr)
  660. {
  661. struct dasd_device *device;
  662. int rc;
  663. /* Check the cqr */
  664. rc = dasd_check_cqr(cqr);
  665. if (rc)
  666. return rc;
  667. device = (struct dasd_device *) cqr->device;
  668. if (cqr->retries < 0) {
  669. DEV_MESSAGE(KERN_DEBUG, device,
  670. "start_IO: request %p (%02x/%i) - no retry left.",
  671. cqr, cqr->status, cqr->retries);
  672. cqr->status = DASD_CQR_FAILED;
  673. return -EIO;
  674. }
  675. cqr->startclk = get_clock();
  676. cqr->starttime = jiffies;
  677. cqr->retries--;
  678. rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
  679. cqr->lpm, 0);
  680. switch (rc) {
  681. case 0:
  682. cqr->status = DASD_CQR_IN_IO;
  683. DBF_DEV_EVENT(DBF_DEBUG, device,
  684. "start_IO: request %p started successful",
  685. cqr);
  686. break;
  687. case -EBUSY:
  688. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  689. "start_IO: device busy, retry later");
  690. break;
  691. case -ETIMEDOUT:
  692. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  693. "start_IO: request timeout, retry later");
  694. break;
  695. case -EACCES:
  696. /* -EACCES indicates that the request used only a
  697. * subset of the available pathes and all these
  698. * pathes are gone.
  699. * Do a retry with all available pathes.
  700. */
  701. cqr->lpm = LPM_ANYPATH;
  702. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  703. "start_IO: selected pathes gone,"
  704. " retry on all pathes");
  705. break;
  706. case -ENODEV:
  707. case -EIO:
  708. DBF_DEV_EVENT(DBF_ERR, device, "%s",
  709. "start_IO: device gone, retry");
  710. break;
  711. default:
  712. DEV_MESSAGE(KERN_ERR, device,
  713. "line %d unknown RC=%d, please report"
  714. " to linux390@de.ibm.com", __LINE__, rc);
  715. BUG();
  716. break;
  717. }
  718. return rc;
  719. }
  720. /*
  721. * Timeout function for dasd devices. This is used for different purposes
  722. * 1) missing interrupt handler for normal operation
  723. * 2) delayed start of request where start_IO failed with -EBUSY
  724. * 3) timeout for missing state change interrupts
  725. * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
  726. * DASD_CQR_QUEUED for 2) and 3).
  727. */
  728. static void
  729. dasd_timeout_device(unsigned long ptr)
  730. {
  731. unsigned long flags;
  732. struct dasd_device *device;
  733. device = (struct dasd_device *) ptr;
  734. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  735. /* re-activate request queue */
  736. device->stopped &= ~DASD_STOPPED_PENDING;
  737. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  738. dasd_schedule_bh(device);
  739. }
  740. /*
  741. * Setup timeout for a device in jiffies.
  742. */
  743. void
  744. dasd_set_timer(struct dasd_device *device, int expires)
  745. {
  746. if (expires == 0) {
  747. if (timer_pending(&device->timer))
  748. del_timer(&device->timer);
  749. return;
  750. }
  751. if (timer_pending(&device->timer)) {
  752. if (mod_timer(&device->timer, jiffies + expires))
  753. return;
  754. }
  755. device->timer.function = dasd_timeout_device;
  756. device->timer.data = (unsigned long) device;
  757. device->timer.expires = jiffies + expires;
  758. add_timer(&device->timer);
  759. }
  760. /*
  761. * Clear timeout for a device.
  762. */
  763. void
  764. dasd_clear_timer(struct dasd_device *device)
  765. {
  766. if (timer_pending(&device->timer))
  767. del_timer(&device->timer);
  768. }
  769. static void
  770. dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
  771. {
  772. struct dasd_ccw_req *cqr;
  773. struct dasd_device *device;
  774. cqr = (struct dasd_ccw_req *) intparm;
  775. if (cqr->status != DASD_CQR_IN_IO) {
  776. MESSAGE(KERN_DEBUG,
  777. "invalid status in handle_killed_request: "
  778. "bus_id %s, status %02x",
  779. cdev->dev.bus_id, cqr->status);
  780. return;
  781. }
  782. device = (struct dasd_device *) cqr->device;
  783. if (device == NULL ||
  784. device != dasd_device_from_cdev(cdev) ||
  785. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  786. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  787. cdev->dev.bus_id);
  788. return;
  789. }
  790. /* Schedule request to be retried. */
  791. cqr->status = DASD_CQR_QUEUED;
  792. dasd_clear_timer(device);
  793. dasd_schedule_bh(device);
  794. dasd_put_device(device);
  795. }
  796. static void
  797. dasd_handle_state_change_pending(struct dasd_device *device)
  798. {
  799. struct dasd_ccw_req *cqr;
  800. struct list_head *l, *n;
  801. /* First of all start sense subsystem status request. */
  802. dasd_eer_snss(device);
  803. device->stopped &= ~DASD_STOPPED_PENDING;
  804. /* restart all 'running' IO on queue */
  805. list_for_each_safe(l, n, &device->ccw_queue) {
  806. cqr = list_entry(l, struct dasd_ccw_req, list);
  807. if (cqr->status == DASD_CQR_IN_IO) {
  808. cqr->status = DASD_CQR_QUEUED;
  809. }
  810. }
  811. dasd_clear_timer(device);
  812. dasd_schedule_bh(device);
  813. }
  814. /*
  815. * Interrupt handler for "normal" ssch-io based dasd devices.
  816. */
  817. void
  818. dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
  819. struct irb *irb)
  820. {
  821. struct dasd_ccw_req *cqr, *next;
  822. struct dasd_device *device;
  823. unsigned long long now;
  824. int expires;
  825. dasd_era_t era;
  826. char mask;
  827. if (IS_ERR(irb)) {
  828. switch (PTR_ERR(irb)) {
  829. case -EIO:
  830. dasd_handle_killed_request(cdev, intparm);
  831. break;
  832. case -ETIMEDOUT:
  833. printk(KERN_WARNING"%s(%s): request timed out\n",
  834. __FUNCTION__, cdev->dev.bus_id);
  835. //FIXME - dasd uses own timeout interface...
  836. break;
  837. default:
  838. printk(KERN_WARNING"%s(%s): unknown error %ld\n",
  839. __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
  840. }
  841. return;
  842. }
  843. now = get_clock();
  844. DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
  845. cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
  846. (unsigned int) intparm);
  847. /* first of all check for state change pending interrupt */
  848. mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
  849. if ((irb->scsw.dstat & mask) == mask) {
  850. device = dasd_device_from_cdev(cdev);
  851. if (!IS_ERR(device)) {
  852. dasd_handle_state_change_pending(device);
  853. dasd_put_device(device);
  854. }
  855. return;
  856. }
  857. cqr = (struct dasd_ccw_req *) intparm;
  858. /* check for unsolicited interrupts */
  859. if (cqr == NULL) {
  860. MESSAGE(KERN_DEBUG,
  861. "unsolicited interrupt received: bus_id %s",
  862. cdev->dev.bus_id);
  863. return;
  864. }
  865. device = (struct dasd_device *) cqr->device;
  866. if (device == NULL ||
  867. strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
  868. MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
  869. cdev->dev.bus_id);
  870. return;
  871. }
  872. /* Check for clear pending */
  873. if (cqr->status == DASD_CQR_CLEAR &&
  874. irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
  875. cqr->status = DASD_CQR_QUEUED;
  876. dasd_clear_timer(device);
  877. dasd_schedule_bh(device);
  878. return;
  879. }
  880. /* check status - the request might have been killed by dyn detach */
  881. if (cqr->status != DASD_CQR_IN_IO) {
  882. MESSAGE(KERN_DEBUG,
  883. "invalid status: bus_id %s, status %02x",
  884. cdev->dev.bus_id, cqr->status);
  885. return;
  886. }
  887. DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
  888. ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
  889. /* Find out the appropriate era_action. */
  890. if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
  891. era = dasd_era_fatal;
  892. else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
  893. irb->scsw.cstat == 0 &&
  894. !irb->esw.esw0.erw.cons)
  895. era = dasd_era_none;
  896. else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
  897. era = dasd_era_fatal; /* don't recover this request */
  898. else if (irb->esw.esw0.erw.cons)
  899. era = device->discipline->examine_error(cqr, irb);
  900. else
  901. era = dasd_era_recover;
  902. DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
  903. expires = 0;
  904. if (era == dasd_era_none) {
  905. cqr->status = DASD_CQR_DONE;
  906. cqr->stopclk = now;
  907. /* Start first request on queue if possible -> fast_io. */
  908. if (cqr->list.next != &device->ccw_queue) {
  909. next = list_entry(cqr->list.next,
  910. struct dasd_ccw_req, list);
  911. if ((next->status == DASD_CQR_QUEUED) &&
  912. (!device->stopped)) {
  913. if (device->discipline->start_IO(next) == 0)
  914. expires = next->expires;
  915. else
  916. DEV_MESSAGE(KERN_DEBUG, device, "%s",
  917. "Interrupt fastpath "
  918. "failed!");
  919. }
  920. }
  921. } else { /* error */
  922. memcpy(&cqr->irb, irb, sizeof (struct irb));
  923. #ifdef ERP_DEBUG
  924. /* dump sense data */
  925. dasd_log_sense(cqr, irb);
  926. #endif
  927. switch (era) {
  928. case dasd_era_fatal:
  929. cqr->status = DASD_CQR_FAILED;
  930. cqr->stopclk = now;
  931. break;
  932. case dasd_era_recover:
  933. cqr->status = DASD_CQR_ERROR;
  934. break;
  935. default:
  936. BUG();
  937. }
  938. }
  939. if (expires != 0)
  940. dasd_set_timer(device, expires);
  941. else
  942. dasd_clear_timer(device);
  943. dasd_schedule_bh(device);
  944. }
  945. /*
  946. * posts the buffer_cache about a finalized request
  947. */
  948. static inline void
  949. dasd_end_request(struct request *req, int uptodate)
  950. {
  951. if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
  952. BUG();
  953. add_disk_randomness(req->rq_disk);
  954. end_that_request_last(req, uptodate);
  955. }
  956. /*
  957. * Process finished error recovery ccw.
  958. */
  959. static inline void
  960. __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
  961. {
  962. dasd_erp_fn_t erp_fn;
  963. if (cqr->status == DASD_CQR_DONE)
  964. DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
  965. else
  966. DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
  967. erp_fn = device->discipline->erp_postaction(cqr);
  968. erp_fn(cqr);
  969. }
  970. /*
  971. * Process ccw request queue.
  972. */
  973. static inline void
  974. __dasd_process_ccw_queue(struct dasd_device * device,
  975. struct list_head *final_queue)
  976. {
  977. struct list_head *l, *n;
  978. struct dasd_ccw_req *cqr;
  979. dasd_erp_fn_t erp_fn;
  980. restart:
  981. /* Process request with final status. */
  982. list_for_each_safe(l, n, &device->ccw_queue) {
  983. cqr = list_entry(l, struct dasd_ccw_req, list);
  984. /* Stop list processing at the first non-final request. */
  985. if (cqr->status != DASD_CQR_DONE &&
  986. cqr->status != DASD_CQR_FAILED &&
  987. cqr->status != DASD_CQR_ERROR)
  988. break;
  989. /* Process requests with DASD_CQR_ERROR */
  990. if (cqr->status == DASD_CQR_ERROR) {
  991. if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
  992. cqr->status = DASD_CQR_FAILED;
  993. cqr->stopclk = get_clock();
  994. } else {
  995. if (cqr->irb.esw.esw0.erw.cons) {
  996. erp_fn = device->discipline->
  997. erp_action(cqr);
  998. erp_fn(cqr);
  999. } else
  1000. dasd_default_erp_action(cqr);
  1001. }
  1002. goto restart;
  1003. }
  1004. /* First of all call extended error reporting. */
  1005. if (dasd_eer_enabled(device) &&
  1006. cqr->status == DASD_CQR_FAILED) {
  1007. dasd_eer_write(device, cqr, DASD_EER_FATALERROR);
  1008. /* restart request */
  1009. cqr->status = DASD_CQR_QUEUED;
  1010. cqr->retries = 255;
  1011. device->stopped |= DASD_STOPPED_QUIESCE;
  1012. goto restart;
  1013. }
  1014. /* Process finished ERP request. */
  1015. if (cqr->refers) {
  1016. __dasd_process_erp(device, cqr);
  1017. goto restart;
  1018. }
  1019. /* Rechain finished requests to final queue */
  1020. cqr->endclk = get_clock();
  1021. list_move_tail(&cqr->list, final_queue);
  1022. }
  1023. }
  1024. static void
  1025. dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
  1026. {
  1027. struct request *req;
  1028. struct dasd_device *device;
  1029. int status;
  1030. req = (struct request *) data;
  1031. device = cqr->device;
  1032. dasd_profile_end(device, cqr, req);
  1033. status = cqr->device->discipline->free_cp(cqr,req);
  1034. spin_lock_irq(&device->request_queue_lock);
  1035. dasd_end_request(req, status);
  1036. spin_unlock_irq(&device->request_queue_lock);
  1037. }
  1038. /*
  1039. * Fetch requests from the block device queue.
  1040. */
  1041. static inline void
  1042. __dasd_process_blk_queue(struct dasd_device * device)
  1043. {
  1044. request_queue_t *queue;
  1045. struct request *req;
  1046. struct dasd_ccw_req *cqr;
  1047. int nr_queued;
  1048. queue = device->request_queue;
  1049. /* No queue ? Then there is nothing to do. */
  1050. if (queue == NULL)
  1051. return;
  1052. /*
  1053. * We requeue request from the block device queue to the ccw
  1054. * queue only in two states. In state DASD_STATE_READY the
  1055. * partition detection is done and we need to requeue requests
  1056. * for that. State DASD_STATE_ONLINE is normal block device
  1057. * operation.
  1058. */
  1059. if (device->state != DASD_STATE_READY &&
  1060. device->state != DASD_STATE_ONLINE)
  1061. return;
  1062. nr_queued = 0;
  1063. /* Now we try to fetch requests from the request queue */
  1064. list_for_each_entry(cqr, &device->ccw_queue, list)
  1065. if (cqr->status == DASD_CQR_QUEUED)
  1066. nr_queued++;
  1067. while (!blk_queue_plugged(queue) &&
  1068. elv_next_request(queue) &&
  1069. nr_queued < DASD_CHANQ_MAX_SIZE) {
  1070. req = elv_next_request(queue);
  1071. if (device->features & DASD_FEATURE_READONLY &&
  1072. rq_data_dir(req) == WRITE) {
  1073. DBF_DEV_EVENT(DBF_ERR, device,
  1074. "Rejecting write request %p",
  1075. req);
  1076. blkdev_dequeue_request(req);
  1077. dasd_end_request(req, 0);
  1078. continue;
  1079. }
  1080. if (device->stopped & DASD_STOPPED_DC_EIO) {
  1081. blkdev_dequeue_request(req);
  1082. dasd_end_request(req, 0);
  1083. continue;
  1084. }
  1085. cqr = device->discipline->build_cp(device, req);
  1086. if (IS_ERR(cqr)) {
  1087. if (PTR_ERR(cqr) == -ENOMEM)
  1088. break; /* terminate request queue loop */
  1089. DBF_DEV_EVENT(DBF_ERR, device,
  1090. "CCW creation failed (rc=%ld) "
  1091. "on request %p",
  1092. PTR_ERR(cqr), req);
  1093. blkdev_dequeue_request(req);
  1094. dasd_end_request(req, 0);
  1095. continue;
  1096. }
  1097. cqr->callback = dasd_end_request_cb;
  1098. cqr->callback_data = (void *) req;
  1099. cqr->status = DASD_CQR_QUEUED;
  1100. blkdev_dequeue_request(req);
  1101. list_add_tail(&cqr->list, &device->ccw_queue);
  1102. dasd_profile_start(device, cqr, req);
  1103. nr_queued++;
  1104. }
  1105. }
  1106. /*
  1107. * Take a look at the first request on the ccw queue and check
  1108. * if it reached its expire time. If so, terminate the IO.
  1109. */
  1110. static inline void
  1111. __dasd_check_expire(struct dasd_device * device)
  1112. {
  1113. struct dasd_ccw_req *cqr;
  1114. if (list_empty(&device->ccw_queue))
  1115. return;
  1116. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1117. if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
  1118. if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
  1119. if (device->discipline->term_IO(cqr) != 0)
  1120. /* Hmpf, try again in 1/10 sec */
  1121. dasd_set_timer(device, 10);
  1122. }
  1123. }
  1124. }
  1125. /*
  1126. * Take a look at the first request on the ccw queue and check
  1127. * if it needs to be started.
  1128. */
  1129. static inline void
  1130. __dasd_start_head(struct dasd_device * device)
  1131. {
  1132. struct dasd_ccw_req *cqr;
  1133. int rc;
  1134. if (list_empty(&device->ccw_queue))
  1135. return;
  1136. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1137. if (cqr->status != DASD_CQR_QUEUED)
  1138. return;
  1139. /* Non-temporary stop condition will trigger fail fast */
  1140. if (device->stopped & ~DASD_STOPPED_PENDING &&
  1141. test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
  1142. (!dasd_eer_enabled(device))) {
  1143. cqr->status = DASD_CQR_FAILED;
  1144. dasd_schedule_bh(device);
  1145. return;
  1146. }
  1147. /* Don't try to start requests if device is stopped */
  1148. if (device->stopped)
  1149. return;
  1150. rc = device->discipline->start_IO(cqr);
  1151. if (rc == 0)
  1152. dasd_set_timer(device, cqr->expires);
  1153. else if (rc == -EACCES) {
  1154. dasd_schedule_bh(device);
  1155. } else
  1156. /* Hmpf, try again in 1/2 sec */
  1157. dasd_set_timer(device, 50);
  1158. }
  1159. /*
  1160. * Remove requests from the ccw queue.
  1161. */
  1162. static void
  1163. dasd_flush_ccw_queue(struct dasd_device * device, int all)
  1164. {
  1165. struct list_head flush_queue;
  1166. struct list_head *l, *n;
  1167. struct dasd_ccw_req *cqr;
  1168. INIT_LIST_HEAD(&flush_queue);
  1169. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1170. list_for_each_safe(l, n, &device->ccw_queue) {
  1171. cqr = list_entry(l, struct dasd_ccw_req, list);
  1172. /* Flush all request or only block device requests? */
  1173. if (all == 0 && cqr->callback == dasd_end_request_cb)
  1174. continue;
  1175. if (cqr->status == DASD_CQR_IN_IO)
  1176. device->discipline->term_IO(cqr);
  1177. if (cqr->status != DASD_CQR_DONE ||
  1178. cqr->status != DASD_CQR_FAILED) {
  1179. cqr->status = DASD_CQR_FAILED;
  1180. cqr->stopclk = get_clock();
  1181. }
  1182. /* Process finished ERP request. */
  1183. if (cqr->refers) {
  1184. __dasd_process_erp(device, cqr);
  1185. continue;
  1186. }
  1187. /* Rechain request on device request queue */
  1188. cqr->endclk = get_clock();
  1189. list_move_tail(&cqr->list, &flush_queue);
  1190. }
  1191. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1192. /* Now call the callback function of flushed requests */
  1193. list_for_each_safe(l, n, &flush_queue) {
  1194. cqr = list_entry(l, struct dasd_ccw_req, list);
  1195. if (cqr->callback != NULL)
  1196. (cqr->callback)(cqr, cqr->callback_data);
  1197. }
  1198. }
  1199. /*
  1200. * Acquire the device lock and process queues for the device.
  1201. */
  1202. static void
  1203. dasd_tasklet(struct dasd_device * device)
  1204. {
  1205. struct list_head final_queue;
  1206. struct list_head *l, *n;
  1207. struct dasd_ccw_req *cqr;
  1208. atomic_set (&device->tasklet_scheduled, 0);
  1209. INIT_LIST_HEAD(&final_queue);
  1210. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1211. /* Check expire time of first request on the ccw queue. */
  1212. __dasd_check_expire(device);
  1213. /* Finish off requests on ccw queue */
  1214. __dasd_process_ccw_queue(device, &final_queue);
  1215. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1216. /* Now call the callback function of requests with final status */
  1217. list_for_each_safe(l, n, &final_queue) {
  1218. cqr = list_entry(l, struct dasd_ccw_req, list);
  1219. list_del_init(&cqr->list);
  1220. if (cqr->callback != NULL)
  1221. (cqr->callback)(cqr, cqr->callback_data);
  1222. }
  1223. spin_lock_irq(&device->request_queue_lock);
  1224. spin_lock(get_ccwdev_lock(device->cdev));
  1225. /* Get new request from the block device request queue */
  1226. __dasd_process_blk_queue(device);
  1227. /* Now check if the head of the ccw queue needs to be started. */
  1228. __dasd_start_head(device);
  1229. spin_unlock(get_ccwdev_lock(device->cdev));
  1230. spin_unlock_irq(&device->request_queue_lock);
  1231. dasd_put_device(device);
  1232. }
  1233. /*
  1234. * Schedules a call to dasd_tasklet over the device tasklet.
  1235. */
  1236. void
  1237. dasd_schedule_bh(struct dasd_device * device)
  1238. {
  1239. /* Protect against rescheduling. */
  1240. if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
  1241. return;
  1242. dasd_get_device(device);
  1243. tasklet_hi_schedule(&device->tasklet);
  1244. }
  1245. /*
  1246. * Queue a request to the head of the ccw_queue. Start the I/O if
  1247. * possible.
  1248. */
  1249. void
  1250. dasd_add_request_head(struct dasd_ccw_req *req)
  1251. {
  1252. struct dasd_device *device;
  1253. unsigned long flags;
  1254. device = req->device;
  1255. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1256. req->status = DASD_CQR_QUEUED;
  1257. req->device = device;
  1258. list_add(&req->list, &device->ccw_queue);
  1259. /* let the bh start the request to keep them in order */
  1260. dasd_schedule_bh(device);
  1261. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1262. }
  1263. /*
  1264. * Queue a request to the tail of the ccw_queue. Start the I/O if
  1265. * possible.
  1266. */
  1267. void
  1268. dasd_add_request_tail(struct dasd_ccw_req *req)
  1269. {
  1270. struct dasd_device *device;
  1271. unsigned long flags;
  1272. device = req->device;
  1273. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1274. req->status = DASD_CQR_QUEUED;
  1275. req->device = device;
  1276. list_add_tail(&req->list, &device->ccw_queue);
  1277. /* let the bh start the request to keep them in order */
  1278. dasd_schedule_bh(device);
  1279. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1280. }
  1281. /*
  1282. * Wakeup callback.
  1283. */
  1284. static void
  1285. dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
  1286. {
  1287. wake_up((wait_queue_head_t *) data);
  1288. }
  1289. static inline int
  1290. _wait_for_wakeup(struct dasd_ccw_req *cqr)
  1291. {
  1292. struct dasd_device *device;
  1293. int rc;
  1294. device = cqr->device;
  1295. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1296. rc = ((cqr->status == DASD_CQR_DONE ||
  1297. cqr->status == DASD_CQR_FAILED) &&
  1298. list_empty(&cqr->list));
  1299. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1300. return rc;
  1301. }
  1302. /*
  1303. * Attempts to start a special ccw queue and waits for its completion.
  1304. */
  1305. int
  1306. dasd_sleep_on(struct dasd_ccw_req * cqr)
  1307. {
  1308. wait_queue_head_t wait_q;
  1309. struct dasd_device *device;
  1310. int rc;
  1311. device = cqr->device;
  1312. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1313. init_waitqueue_head (&wait_q);
  1314. cqr->callback = dasd_wakeup_cb;
  1315. cqr->callback_data = (void *) &wait_q;
  1316. cqr->status = DASD_CQR_QUEUED;
  1317. list_add_tail(&cqr->list, &device->ccw_queue);
  1318. /* let the bh start the request to keep them in order */
  1319. dasd_schedule_bh(device);
  1320. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1321. wait_event(wait_q, _wait_for_wakeup(cqr));
  1322. /* Request status is either done or failed. */
  1323. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1324. return rc;
  1325. }
  1326. /*
  1327. * Attempts to start a special ccw queue and wait interruptible
  1328. * for its completion.
  1329. */
  1330. int
  1331. dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
  1332. {
  1333. wait_queue_head_t wait_q;
  1334. struct dasd_device *device;
  1335. int rc, finished;
  1336. device = cqr->device;
  1337. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1338. init_waitqueue_head (&wait_q);
  1339. cqr->callback = dasd_wakeup_cb;
  1340. cqr->callback_data = (void *) &wait_q;
  1341. cqr->status = DASD_CQR_QUEUED;
  1342. list_add_tail(&cqr->list, &device->ccw_queue);
  1343. /* let the bh start the request to keep them in order */
  1344. dasd_schedule_bh(device);
  1345. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1346. finished = 0;
  1347. while (!finished) {
  1348. rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
  1349. if (rc != -ERESTARTSYS) {
  1350. /* Request is final (done or failed) */
  1351. rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  1352. break;
  1353. }
  1354. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1355. switch (cqr->status) {
  1356. case DASD_CQR_IN_IO:
  1357. /* terminate runnig cqr */
  1358. if (device->discipline->term_IO) {
  1359. cqr->retries = -1;
  1360. device->discipline->term_IO(cqr);
  1361. /*nished =
  1362. * wait (non-interruptible) for final status
  1363. * because signal ist still pending
  1364. */
  1365. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1366. wait_event(wait_q, _wait_for_wakeup(cqr));
  1367. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1368. rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
  1369. finished = 1;
  1370. }
  1371. break;
  1372. case DASD_CQR_QUEUED:
  1373. /* request */
  1374. list_del_init(&cqr->list);
  1375. rc = -EIO;
  1376. finished = 1;
  1377. break;
  1378. default:
  1379. /* cqr with 'non-interruptable' status - just wait */
  1380. break;
  1381. }
  1382. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1383. }
  1384. return rc;
  1385. }
  1386. /*
  1387. * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
  1388. * for eckd devices) the currently running request has to be terminated
  1389. * and be put back to status queued, before the special request is added
  1390. * to the head of the queue. Then the special request is waited on normally.
  1391. */
  1392. static inline int
  1393. _dasd_term_running_cqr(struct dasd_device *device)
  1394. {
  1395. struct dasd_ccw_req *cqr;
  1396. int rc;
  1397. if (list_empty(&device->ccw_queue))
  1398. return 0;
  1399. cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
  1400. rc = device->discipline->term_IO(cqr);
  1401. if (rc == 0) {
  1402. /* termination successful */
  1403. cqr->status = DASD_CQR_QUEUED;
  1404. cqr->startclk = cqr->stopclk = 0;
  1405. cqr->starttime = 0;
  1406. }
  1407. return rc;
  1408. }
  1409. int
  1410. dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
  1411. {
  1412. wait_queue_head_t wait_q;
  1413. struct dasd_device *device;
  1414. int rc;
  1415. device = cqr->device;
  1416. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1417. rc = _dasd_term_running_cqr(device);
  1418. if (rc) {
  1419. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1420. return rc;
  1421. }
  1422. init_waitqueue_head (&wait_q);
  1423. cqr->callback = dasd_wakeup_cb;
  1424. cqr->callback_data = (void *) &wait_q;
  1425. cqr->status = DASD_CQR_QUEUED;
  1426. list_add(&cqr->list, &device->ccw_queue);
  1427. /* let the bh start the request to keep them in order */
  1428. dasd_schedule_bh(device);
  1429. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1430. wait_event(wait_q, _wait_for_wakeup(cqr));
  1431. /* Request status is either done or failed. */
  1432. rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
  1433. return rc;
  1434. }
  1435. /*
  1436. * Cancels a request that was started with dasd_sleep_on_req.
  1437. * This is useful to timeout requests. The request will be
  1438. * terminated if it is currently in i/o.
  1439. * Returns 1 if the request has been terminated.
  1440. */
  1441. int
  1442. dasd_cancel_req(struct dasd_ccw_req *cqr)
  1443. {
  1444. struct dasd_device *device = cqr->device;
  1445. unsigned long flags;
  1446. int rc;
  1447. rc = 0;
  1448. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  1449. switch (cqr->status) {
  1450. case DASD_CQR_QUEUED:
  1451. /* request was not started - just set to failed */
  1452. cqr->status = DASD_CQR_FAILED;
  1453. break;
  1454. case DASD_CQR_IN_IO:
  1455. /* request in IO - terminate IO and release again */
  1456. if (device->discipline->term_IO(cqr) != 0)
  1457. /* what to do if unable to terminate ??????
  1458. e.g. not _IN_IO */
  1459. cqr->status = DASD_CQR_FAILED;
  1460. cqr->stopclk = get_clock();
  1461. rc = 1;
  1462. break;
  1463. case DASD_CQR_DONE:
  1464. case DASD_CQR_FAILED:
  1465. /* already finished - do nothing */
  1466. break;
  1467. default:
  1468. DEV_MESSAGE(KERN_ALERT, device,
  1469. "invalid status %02x in request",
  1470. cqr->status);
  1471. BUG();
  1472. }
  1473. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  1474. dasd_schedule_bh(device);
  1475. return rc;
  1476. }
  1477. /*
  1478. * SECTION: Block device operations (request queue, partitions, open, release).
  1479. */
  1480. /*
  1481. * Dasd request queue function. Called from ll_rw_blk.c
  1482. */
  1483. static void
  1484. do_dasd_request(request_queue_t * queue)
  1485. {
  1486. struct dasd_device *device;
  1487. device = (struct dasd_device *) queue->queuedata;
  1488. spin_lock(get_ccwdev_lock(device->cdev));
  1489. /* Get new request from the block device request queue */
  1490. __dasd_process_blk_queue(device);
  1491. /* Now check if the head of the ccw queue needs to be started. */
  1492. __dasd_start_head(device);
  1493. spin_unlock(get_ccwdev_lock(device->cdev));
  1494. }
  1495. /*
  1496. * Allocate and initialize request queue and default I/O scheduler.
  1497. */
  1498. static int
  1499. dasd_alloc_queue(struct dasd_device * device)
  1500. {
  1501. int rc;
  1502. device->request_queue = blk_init_queue(do_dasd_request,
  1503. &device->request_queue_lock);
  1504. if (device->request_queue == NULL)
  1505. return -ENOMEM;
  1506. device->request_queue->queuedata = device;
  1507. elevator_exit(device->request_queue->elevator);
  1508. rc = elevator_init(device->request_queue, "deadline");
  1509. if (rc) {
  1510. blk_cleanup_queue(device->request_queue);
  1511. return rc;
  1512. }
  1513. return 0;
  1514. }
  1515. /*
  1516. * Allocate and initialize request queue.
  1517. */
  1518. static void
  1519. dasd_setup_queue(struct dasd_device * device)
  1520. {
  1521. int max;
  1522. blk_queue_hardsect_size(device->request_queue, device->bp_block);
  1523. max = device->discipline->max_blocks << device->s2b_shift;
  1524. blk_queue_max_sectors(device->request_queue, max);
  1525. blk_queue_max_phys_segments(device->request_queue, -1L);
  1526. blk_queue_max_hw_segments(device->request_queue, -1L);
  1527. blk_queue_max_segment_size(device->request_queue, -1L);
  1528. blk_queue_segment_boundary(device->request_queue, -1L);
  1529. blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
  1530. }
  1531. /*
  1532. * Deactivate and free request queue.
  1533. */
  1534. static void
  1535. dasd_free_queue(struct dasd_device * device)
  1536. {
  1537. if (device->request_queue) {
  1538. blk_cleanup_queue(device->request_queue);
  1539. device->request_queue = NULL;
  1540. }
  1541. }
  1542. /*
  1543. * Flush request on the request queue.
  1544. */
  1545. static void
  1546. dasd_flush_request_queue(struct dasd_device * device)
  1547. {
  1548. struct request *req;
  1549. if (!device->request_queue)
  1550. return;
  1551. spin_lock_irq(&device->request_queue_lock);
  1552. while (!list_empty(&device->request_queue->queue_head)) {
  1553. req = elv_next_request(device->request_queue);
  1554. if (req == NULL)
  1555. break;
  1556. dasd_end_request(req, 0);
  1557. blkdev_dequeue_request(req);
  1558. }
  1559. spin_unlock_irq(&device->request_queue_lock);
  1560. }
  1561. static int
  1562. dasd_open(struct inode *inp, struct file *filp)
  1563. {
  1564. struct gendisk *disk = inp->i_bdev->bd_disk;
  1565. struct dasd_device *device = disk->private_data;
  1566. int rc;
  1567. atomic_inc(&device->open_count);
  1568. if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1569. rc = -ENODEV;
  1570. goto unlock;
  1571. }
  1572. if (!try_module_get(device->discipline->owner)) {
  1573. rc = -EINVAL;
  1574. goto unlock;
  1575. }
  1576. if (dasd_probeonly) {
  1577. DEV_MESSAGE(KERN_INFO, device, "%s",
  1578. "No access to device due to probeonly mode");
  1579. rc = -EPERM;
  1580. goto out;
  1581. }
  1582. if (device->state <= DASD_STATE_BASIC) {
  1583. DBF_DEV_EVENT(DBF_ERR, device, " %s",
  1584. " Cannot open unrecognized device");
  1585. rc = -ENODEV;
  1586. goto out;
  1587. }
  1588. return 0;
  1589. out:
  1590. module_put(device->discipline->owner);
  1591. unlock:
  1592. atomic_dec(&device->open_count);
  1593. return rc;
  1594. }
  1595. static int
  1596. dasd_release(struct inode *inp, struct file *filp)
  1597. {
  1598. struct gendisk *disk = inp->i_bdev->bd_disk;
  1599. struct dasd_device *device = disk->private_data;
  1600. atomic_dec(&device->open_count);
  1601. module_put(device->discipline->owner);
  1602. return 0;
  1603. }
  1604. /*
  1605. * Return disk geometry.
  1606. */
  1607. static int
  1608. dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1609. {
  1610. struct dasd_device *device;
  1611. device = bdev->bd_disk->private_data;
  1612. if (!device)
  1613. return -ENODEV;
  1614. if (!device->discipline ||
  1615. !device->discipline->fill_geometry)
  1616. return -EINVAL;
  1617. device->discipline->fill_geometry(device, geo);
  1618. geo->start = get_start_sect(bdev) >> device->s2b_shift;
  1619. return 0;
  1620. }
  1621. struct block_device_operations
  1622. dasd_device_operations = {
  1623. .owner = THIS_MODULE,
  1624. .open = dasd_open,
  1625. .release = dasd_release,
  1626. .ioctl = dasd_ioctl,
  1627. .compat_ioctl = dasd_compat_ioctl,
  1628. .getgeo = dasd_getgeo,
  1629. };
  1630. static void
  1631. dasd_exit(void)
  1632. {
  1633. #ifdef CONFIG_PROC_FS
  1634. dasd_proc_exit();
  1635. #endif
  1636. dasd_eer_exit();
  1637. if (dasd_page_cache != NULL) {
  1638. kmem_cache_destroy(dasd_page_cache);
  1639. dasd_page_cache = NULL;
  1640. }
  1641. dasd_gendisk_exit();
  1642. dasd_devmap_exit();
  1643. devfs_remove("dasd");
  1644. if (dasd_debug_area != NULL) {
  1645. debug_unregister(dasd_debug_area);
  1646. dasd_debug_area = NULL;
  1647. }
  1648. }
  1649. /*
  1650. * SECTION: common functions for ccw_driver use
  1651. */
  1652. /*
  1653. * Initial attempt at a probe function. this can be simplified once
  1654. * the other detection code is gone.
  1655. */
  1656. int
  1657. dasd_generic_probe (struct ccw_device *cdev,
  1658. struct dasd_discipline *discipline)
  1659. {
  1660. int ret;
  1661. ret = dasd_add_sysfs_files(cdev);
  1662. if (ret) {
  1663. printk(KERN_WARNING
  1664. "dasd_generic_probe: could not add sysfs entries "
  1665. "for %s\n", cdev->dev.bus_id);
  1666. } else {
  1667. cdev->handler = &dasd_int_handler;
  1668. }
  1669. return ret;
  1670. }
  1671. /*
  1672. * This will one day be called from a global not_oper handler.
  1673. * It is also used by driver_unregister during module unload.
  1674. */
  1675. void
  1676. dasd_generic_remove (struct ccw_device *cdev)
  1677. {
  1678. struct dasd_device *device;
  1679. cdev->handler = NULL;
  1680. dasd_remove_sysfs_files(cdev);
  1681. device = dasd_device_from_cdev(cdev);
  1682. if (IS_ERR(device))
  1683. return;
  1684. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1685. /* Already doing offline processing */
  1686. dasd_put_device(device);
  1687. return;
  1688. }
  1689. /*
  1690. * This device is removed unconditionally. Set offline
  1691. * flag to prevent dasd_open from opening it while it is
  1692. * no quite down yet.
  1693. */
  1694. dasd_set_target_state(device, DASD_STATE_NEW);
  1695. /* dasd_delete_device destroys the device reference. */
  1696. dasd_delete_device(device);
  1697. }
  1698. /*
  1699. * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
  1700. * the device is detected for the first time and is supposed to be used
  1701. * or the user has started activation through sysfs.
  1702. */
  1703. int
  1704. dasd_generic_set_online (struct ccw_device *cdev,
  1705. struct dasd_discipline *base_discipline)
  1706. {
  1707. struct dasd_discipline *discipline;
  1708. struct dasd_device *device;
  1709. int rc;
  1710. device = dasd_create_device(cdev);
  1711. if (IS_ERR(device))
  1712. return PTR_ERR(device);
  1713. discipline = base_discipline;
  1714. if (device->features & DASD_FEATURE_USEDIAG) {
  1715. if (!dasd_diag_discipline_pointer) {
  1716. printk (KERN_WARNING
  1717. "dasd_generic couldn't online device %s "
  1718. "- discipline DIAG not available\n",
  1719. cdev->dev.bus_id);
  1720. dasd_delete_device(device);
  1721. return -ENODEV;
  1722. }
  1723. discipline = dasd_diag_discipline_pointer;
  1724. }
  1725. if (!try_module_get(base_discipline->owner)) {
  1726. dasd_delete_device(device);
  1727. return -EINVAL;
  1728. }
  1729. if (!try_module_get(discipline->owner)) {
  1730. module_put(base_discipline->owner);
  1731. dasd_delete_device(device);
  1732. return -EINVAL;
  1733. }
  1734. device->base_discipline = base_discipline;
  1735. device->discipline = discipline;
  1736. rc = discipline->check_device(device);
  1737. if (rc) {
  1738. printk (KERN_WARNING
  1739. "dasd_generic couldn't online device %s "
  1740. "with discipline %s rc=%i\n",
  1741. cdev->dev.bus_id, discipline->name, rc);
  1742. module_put(discipline->owner);
  1743. module_put(base_discipline->owner);
  1744. dasd_delete_device(device);
  1745. return rc;
  1746. }
  1747. dasd_set_target_state(device, DASD_STATE_ONLINE);
  1748. if (device->state <= DASD_STATE_KNOWN) {
  1749. printk (KERN_WARNING
  1750. "dasd_generic discipline not found for %s\n",
  1751. cdev->dev.bus_id);
  1752. rc = -ENODEV;
  1753. dasd_set_target_state(device, DASD_STATE_NEW);
  1754. dasd_delete_device(device);
  1755. } else
  1756. pr_debug("dasd_generic device %s found\n",
  1757. cdev->dev.bus_id);
  1758. /* FIXME: we have to wait for the root device but we don't want
  1759. * to wait for each single device but for all at once. */
  1760. wait_event(dasd_init_waitq, _wait_for_device(device));
  1761. dasd_put_device(device);
  1762. return rc;
  1763. }
  1764. int
  1765. dasd_generic_set_offline (struct ccw_device *cdev)
  1766. {
  1767. struct dasd_device *device;
  1768. int max_count, open_count;
  1769. device = dasd_device_from_cdev(cdev);
  1770. if (IS_ERR(device))
  1771. return PTR_ERR(device);
  1772. if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
  1773. /* Already doing offline processing */
  1774. dasd_put_device(device);
  1775. return 0;
  1776. }
  1777. /*
  1778. * We must make sure that this device is currently not in use.
  1779. * The open_count is increased for every opener, that includes
  1780. * the blkdev_get in dasd_scan_partitions. We are only interested
  1781. * in the other openers.
  1782. */
  1783. max_count = device->bdev ? 0 : -1;
  1784. open_count = (int) atomic_read(&device->open_count);
  1785. if (open_count > max_count) {
  1786. if (open_count > 0)
  1787. printk (KERN_WARNING "Can't offline dasd device with "
  1788. "open count = %i.\n",
  1789. open_count);
  1790. else
  1791. printk (KERN_WARNING "%s",
  1792. "Can't offline dasd device due to internal "
  1793. "use\n");
  1794. clear_bit(DASD_FLAG_OFFLINE, &device->flags);
  1795. dasd_put_device(device);
  1796. return -EBUSY;
  1797. }
  1798. dasd_set_target_state(device, DASD_STATE_NEW);
  1799. /* dasd_delete_device destroys the device reference. */
  1800. dasd_delete_device(device);
  1801. return 0;
  1802. }
  1803. int
  1804. dasd_generic_notify(struct ccw_device *cdev, int event)
  1805. {
  1806. struct dasd_device *device;
  1807. struct dasd_ccw_req *cqr;
  1808. unsigned long flags;
  1809. int ret;
  1810. device = dasd_device_from_cdev(cdev);
  1811. if (IS_ERR(device))
  1812. return 0;
  1813. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1814. ret = 0;
  1815. switch (event) {
  1816. case CIO_GONE:
  1817. case CIO_NO_PATH:
  1818. /* First of all call extended error reporting. */
  1819. dasd_eer_write(device, NULL, DASD_EER_NOPATH);
  1820. if (device->state < DASD_STATE_BASIC)
  1821. break;
  1822. /* Device is active. We want to keep it. */
  1823. if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
  1824. list_for_each_entry(cqr, &device->ccw_queue, list)
  1825. if (cqr->status == DASD_CQR_IN_IO)
  1826. cqr->status = DASD_CQR_FAILED;
  1827. device->stopped |= DASD_STOPPED_DC_EIO;
  1828. } else {
  1829. list_for_each_entry(cqr, &device->ccw_queue, list)
  1830. if (cqr->status == DASD_CQR_IN_IO) {
  1831. cqr->status = DASD_CQR_QUEUED;
  1832. cqr->retries++;
  1833. }
  1834. device->stopped |= DASD_STOPPED_DC_WAIT;
  1835. dasd_set_timer(device, 0);
  1836. }
  1837. dasd_schedule_bh(device);
  1838. ret = 1;
  1839. break;
  1840. case CIO_OPER:
  1841. /* FIXME: add a sanity check. */
  1842. device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
  1843. dasd_schedule_bh(device);
  1844. ret = 1;
  1845. break;
  1846. }
  1847. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1848. dasd_put_device(device);
  1849. return ret;
  1850. }
  1851. /*
  1852. * Automatically online either all dasd devices (dasd_autodetect) or
  1853. * all devices specified with dasd= parameters.
  1854. */
  1855. static int
  1856. __dasd_auto_online(struct device *dev, void *data)
  1857. {
  1858. struct ccw_device *cdev;
  1859. cdev = to_ccwdev(dev);
  1860. if (dasd_autodetect || dasd_busid_known(cdev->dev.bus_id) == 0)
  1861. ccw_device_set_online(cdev);
  1862. return 0;
  1863. }
  1864. void
  1865. dasd_generic_auto_online (struct ccw_driver *dasd_discipline_driver)
  1866. {
  1867. struct device_driver *drv;
  1868. drv = get_driver(&dasd_discipline_driver->driver);
  1869. driver_for_each_device(drv, NULL, NULL, __dasd_auto_online);
  1870. put_driver(drv);
  1871. }
  1872. static int __init
  1873. dasd_init(void)
  1874. {
  1875. int rc;
  1876. init_waitqueue_head(&dasd_init_waitq);
  1877. /* register 'common' DASD debug area, used for all DBF_XXX calls */
  1878. dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
  1879. if (dasd_debug_area == NULL) {
  1880. rc = -ENOMEM;
  1881. goto failed;
  1882. }
  1883. debug_register_view(dasd_debug_area, &debug_sprintf_view);
  1884. debug_set_level(dasd_debug_area, DBF_EMERG);
  1885. DBF_EVENT(DBF_EMERG, "%s", "debug area created");
  1886. dasd_diag_discipline_pointer = NULL;
  1887. rc = devfs_mk_dir("dasd");
  1888. if (rc)
  1889. goto failed;
  1890. rc = dasd_devmap_init();
  1891. if (rc)
  1892. goto failed;
  1893. rc = dasd_gendisk_init();
  1894. if (rc)
  1895. goto failed;
  1896. rc = dasd_parse();
  1897. if (rc)
  1898. goto failed;
  1899. rc = dasd_eer_init();
  1900. if (rc)
  1901. goto failed;
  1902. #ifdef CONFIG_PROC_FS
  1903. rc = dasd_proc_init();
  1904. if (rc)
  1905. goto failed;
  1906. #endif
  1907. return 0;
  1908. failed:
  1909. MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
  1910. dasd_exit();
  1911. return rc;
  1912. }
  1913. module_init(dasd_init);
  1914. module_exit(dasd_exit);
  1915. EXPORT_SYMBOL(dasd_debug_area);
  1916. EXPORT_SYMBOL(dasd_diag_discipline_pointer);
  1917. EXPORT_SYMBOL(dasd_add_request_head);
  1918. EXPORT_SYMBOL(dasd_add_request_tail);
  1919. EXPORT_SYMBOL(dasd_cancel_req);
  1920. EXPORT_SYMBOL(dasd_clear_timer);
  1921. EXPORT_SYMBOL(dasd_enable_device);
  1922. EXPORT_SYMBOL(dasd_int_handler);
  1923. EXPORT_SYMBOL(dasd_kfree_request);
  1924. EXPORT_SYMBOL(dasd_kick_device);
  1925. EXPORT_SYMBOL(dasd_kmalloc_request);
  1926. EXPORT_SYMBOL(dasd_schedule_bh);
  1927. EXPORT_SYMBOL(dasd_set_target_state);
  1928. EXPORT_SYMBOL(dasd_set_timer);
  1929. EXPORT_SYMBOL(dasd_sfree_request);
  1930. EXPORT_SYMBOL(dasd_sleep_on);
  1931. EXPORT_SYMBOL(dasd_sleep_on_immediatly);
  1932. EXPORT_SYMBOL(dasd_sleep_on_interruptible);
  1933. EXPORT_SYMBOL(dasd_smalloc_request);
  1934. EXPORT_SYMBOL(dasd_start_IO);
  1935. EXPORT_SYMBOL(dasd_term_IO);
  1936. EXPORT_SYMBOL_GPL(dasd_generic_probe);
  1937. EXPORT_SYMBOL_GPL(dasd_generic_remove);
  1938. EXPORT_SYMBOL_GPL(dasd_generic_notify);
  1939. EXPORT_SYMBOL_GPL(dasd_generic_set_online);
  1940. EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
  1941. EXPORT_SYMBOL_GPL(dasd_generic_auto_online);
  1942. /*
  1943. * Overrides for Emacs so that we follow Linus's tabbing style.
  1944. * Emacs will notice this stuff at the end of the file and automatically
  1945. * adjust the settings for this buffer only. This must remain at the end
  1946. * of the file.
  1947. * ---------------------------------------------------------------------------
  1948. * Local variables:
  1949. * c-indent-level: 4
  1950. * c-brace-imaginary-offset: 0
  1951. * c-brace-offset: -4
  1952. * c-argdecl-indent: 4
  1953. * c-label-offset: -4
  1954. * c-continued-statement-offset: 4
  1955. * c-continued-brace-offset: 0
  1956. * indent-tabs-mode: 1
  1957. * tab-width: 8
  1958. * End:
  1959. */