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