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