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