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