1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * hosts.c Copyright (C) 1992 Drew Eckhardt
4 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
5 * Copyright (C) 2002-2003 Christoph Hellwig
6 *
7 * mid to lowlevel SCSI driver interface
8 * Initial versions: Drew Eckhardt
9 * Subsequent revisions: Eric Youngdale
10 *
11 * <drew@colorado.edu>
12 *
13 * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
14 * Added QLOGIC QLA1280 SCSI controller kernel host support.
15 * August 4, 1999 Fred Lewis, Intel DuPont
16 *
17 * Updated to reflect the new initialization scheme for the higher
18 * level of scsi drivers (sd/sr/st)
19 * September 17, 2000 Torben Mathiasen <tmm@image.dk>
20 *
21 * Restructured scsi_host lists and associated functions.
22 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
23 */
24
25 #include <linux/module.h>
26 #include <linux/blkdev.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/kthread.h>
30 #include <linux/string.h>
31 #include <linux/mm.h>
32 #include <linux/init.h>
33 #include <linux/completion.h>
34 #include <linux/transport_class.h>
35 #include <linux/platform_device.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/idr.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_transport.h>
41 #include <scsi/scsi_cmnd.h>
42
43 #include "scsi_priv.h"
44 #include "scsi_logging.h"
45
46
47 static int shost_eh_deadline = -1;
48
49 module_param_named(eh_deadline, shost_eh_deadline, int, S_IRUGO|S_IWUSR);
50 MODULE_PARM_DESC(eh_deadline,
51 "SCSI EH timeout in seconds (should be between 0 and 2^31-1)");
52
53 static DEFINE_IDA(host_index_ida);
54
55
scsi_host_cls_release(struct device * dev)56 static void scsi_host_cls_release(struct device *dev)
57 {
58 put_device(&class_to_shost(dev)->shost_gendev);
59 }
60
61 static struct class shost_class = {
62 .name = "scsi_host",
63 .dev_release = scsi_host_cls_release,
64 .dev_groups = scsi_shost_groups,
65 };
66
67 /**
68 * scsi_host_set_state - Take the given host through the host state model.
69 * @shost: scsi host to change the state of.
70 * @state: state to change to.
71 *
72 * Returns zero if unsuccessful or an error if the requested
73 * transition is illegal.
74 **/
scsi_host_set_state(struct Scsi_Host * shost,enum scsi_host_state state)75 int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
76 __must_hold(shost->host_lock)
77 {
78 enum scsi_host_state oldstate = READ_ONCE(shost->shost_state);
79
80 if (state == oldstate)
81 return 0;
82
83 switch (state) {
84 case SHOST_CREATED:
85 /* There are no legal states that come back to
86 * created. This is the manually initialised start
87 * state */
88 goto illegal;
89
90 case SHOST_RUNNING:
91 switch (oldstate) {
92 case SHOST_CREATED:
93 case SHOST_RECOVERY:
94 break;
95 default:
96 goto illegal;
97 }
98 break;
99
100 case SHOST_RECOVERY:
101 switch (oldstate) {
102 case SHOST_RUNNING:
103 break;
104 default:
105 goto illegal;
106 }
107 break;
108
109 case SHOST_CANCEL:
110 switch (oldstate) {
111 case SHOST_CREATED:
112 case SHOST_RUNNING:
113 case SHOST_CANCEL_RECOVERY:
114 break;
115 default:
116 goto illegal;
117 }
118 break;
119
120 case SHOST_DEL:
121 switch (oldstate) {
122 case SHOST_CANCEL:
123 case SHOST_DEL_RECOVERY:
124 break;
125 default:
126 goto illegal;
127 }
128 break;
129
130 case SHOST_CANCEL_RECOVERY:
131 switch (oldstate) {
132 case SHOST_CANCEL:
133 case SHOST_RECOVERY:
134 break;
135 default:
136 goto illegal;
137 }
138 break;
139
140 case SHOST_DEL_RECOVERY:
141 switch (oldstate) {
142 case SHOST_CANCEL_RECOVERY:
143 break;
144 default:
145 goto illegal;
146 }
147 break;
148 }
149 WRITE_ONCE(shost->shost_state, state);
150 return 0;
151
152 illegal:
153 SCSI_LOG_ERROR_RECOVERY(1,
154 shost_printk(KERN_ERR, shost,
155 "Illegal host state transition"
156 "%s->%s\n",
157 scsi_host_state_name(oldstate),
158 scsi_host_state_name(state)));
159 return -EINVAL;
160 }
161
162 /**
163 * scsi_remove_host - remove a scsi host
164 * @shost: a pointer to a scsi host to remove
165 **/
scsi_remove_host(struct Scsi_Host * shost)166 void scsi_remove_host(struct Scsi_Host *shost)
167 {
168 unsigned long flags;
169
170 mutex_lock(&shost->scan_mutex);
171 spin_lock_irqsave(shost->host_lock, flags);
172 if (scsi_host_set_state(shost, SHOST_CANCEL))
173 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) {
174 spin_unlock_irqrestore(shost->host_lock, flags);
175 mutex_unlock(&shost->scan_mutex);
176 return;
177 }
178 spin_unlock_irqrestore(shost->host_lock, flags);
179
180 scsi_autopm_get_host(shost);
181 flush_workqueue(shost->tmf_work_q);
182 scsi_forget_host(shost);
183 mutex_unlock(&shost->scan_mutex);
184 scsi_proc_host_rm(shost);
185 scsi_proc_hostdir_rm(shost->hostt);
186
187 /*
188 * New SCSI devices cannot be attached anymore because of the SCSI host
189 * state so drop the tag set refcnt. Wait until the tag set refcnt drops
190 * to zero because .exit_cmd_priv implementations may need the host
191 * pointer.
192 */
193 kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
194 wait_for_completion(&shost->tagset_freed);
195
196 spin_lock_irqsave(shost->host_lock, flags);
197 if (scsi_host_set_state(shost, SHOST_DEL))
198 BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
199 spin_unlock_irqrestore(shost->host_lock, flags);
200
201 transport_unregister_device(&shost->shost_gendev);
202 device_unregister(&shost->shost_dev);
203 device_del(&shost->shost_gendev);
204 }
205 EXPORT_SYMBOL(scsi_remove_host);
206
207 /**
208 * scsi_add_host_with_dma - add a scsi host with dma device
209 * @shost: scsi host pointer to add
210 * @dev: a struct device of type scsi class
211 * @dma_dev: dma device for the host
212 *
213 * Note: You rarely need to worry about this unless you're in a
214 * virtualised host environments, so use the simpler scsi_add_host()
215 * function instead.
216 *
217 * Return value:
218 * 0 on success / != 0 for error
219 **/
scsi_add_host_with_dma(struct Scsi_Host * shost,struct device * dev,struct device * dma_dev)220 int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
221 struct device *dma_dev)
222 {
223 const struct scsi_host_template *sht = shost->hostt;
224 int error = -EINVAL;
225
226 shost_printk(KERN_INFO, shost, "%s\n",
227 sht->info ? sht->info(shost) : sht->name);
228
229 if (!shost->can_queue) {
230 shost_printk(KERN_ERR, shost,
231 "can_queue = 0 no longer supported\n");
232 goto fail;
233 }
234
235 if (shost->nr_reserved_cmds && !sht->queue_reserved_command) {
236 shost_printk(KERN_ERR, shost,
237 "nr_reserved_cmds set but no method to queue\n");
238 goto fail;
239 }
240
241 /* Use min_t(int, ...) in case shost->can_queue exceeds SHRT_MAX */
242 shost->cmd_per_lun = min_t(int, shost->cmd_per_lun,
243 shost->can_queue);
244
245 error = scsi_init_sense_cache(shost);
246 if (error)
247 goto fail;
248
249 if (!shost->shost_gendev.parent)
250 shost->shost_gendev.parent = dev ? dev : &platform_bus;
251 if (!dma_dev)
252 dma_dev = shost->shost_gendev.parent;
253
254 shost->dma_dev = dma_dev;
255
256 shost->max_sectors = min_not_zero(shost->max_sectors,
257 dma_max_mapping_size(dma_dev) >> SECTOR_SHIFT);
258
259 error = scsi_mq_setup_tags(shost);
260 if (error)
261 goto fail;
262
263 kref_init(&shost->tagset_refcnt);
264 init_completion(&shost->tagset_freed);
265
266 /*
267 * Increase usage count temporarily here so that calling
268 * scsi_autopm_put_host() will trigger runtime idle if there is
269 * nothing else preventing suspending the device.
270 */
271 pm_runtime_get_noresume(&shost->shost_gendev);
272 pm_runtime_set_active(&shost->shost_gendev);
273 pm_runtime_enable(&shost->shost_gendev);
274 device_enable_async_suspend(&shost->shost_gendev);
275
276 error = device_add(&shost->shost_gendev);
277 if (error)
278 goto out_disable_runtime_pm;
279
280 scoped_guard(spinlock_irq, shost->host_lock)
281 scsi_host_set_state(shost, SHOST_RUNNING);
282 get_device(shost->shost_gendev.parent);
283
284 device_enable_async_suspend(&shost->shost_dev);
285
286 get_device(&shost->shost_gendev);
287 error = device_add(&shost->shost_dev);
288 if (error)
289 goto out_del_gendev;
290
291 if (shost->transportt->host_size) {
292 shost->shost_data = kzalloc(shost->transportt->host_size,
293 GFP_KERNEL);
294 if (shost->shost_data == NULL) {
295 error = -ENOMEM;
296 goto out_del_dev;
297 }
298 }
299
300 if (shost->transportt->create_work_queue) {
301 shost->work_q = alloc_workqueue(
302 "scsi_wq_%d",
303 WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
304 shost->host_no);
305
306 if (!shost->work_q) {
307 error = -EINVAL;
308 goto out_del_dev;
309 }
310 }
311
312 error = scsi_sysfs_add_host(shost);
313 if (error)
314 goto out_del_dev;
315
316 if (shost->nr_reserved_cmds) {
317 shost->pseudo_sdev = scsi_get_pseudo_sdev(shost);
318 if (!shost->pseudo_sdev) {
319 error = -ENOMEM;
320 goto out_del_dev;
321 }
322 }
323
324 scsi_proc_host_add(shost);
325 scsi_autopm_put_host(shost);
326 return error;
327
328 /*
329 * Any host allocation in this function will be freed in
330 * scsi_host_dev_release().
331 */
332 out_del_dev:
333 device_del(&shost->shost_dev);
334 out_del_gendev:
335 /*
336 * Host state is SHOST_RUNNING so we have to explicitly release
337 * ->shost_dev.
338 */
339 put_device(&shost->shost_dev);
340 device_del(&shost->shost_gendev);
341 out_disable_runtime_pm:
342 device_disable_async_suspend(&shost->shost_gendev);
343 pm_runtime_disable(&shost->shost_gendev);
344 pm_runtime_set_suspended(&shost->shost_gendev);
345 pm_runtime_put_noidle(&shost->shost_gendev);
346 kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
347 fail:
348 return error;
349 }
350 EXPORT_SYMBOL(scsi_add_host_with_dma);
351
scsi_host_dev_release(struct device * dev)352 static void scsi_host_dev_release(struct device *dev)
353 {
354 struct Scsi_Host *shost = dev_to_shost(dev);
355 enum scsi_host_state state = scsi_get_host_state(shost);
356 struct device *parent = dev->parent;
357
358 /* Wait for functions invoked through call_rcu(&scmd->rcu, ...) */
359 rcu_barrier();
360
361 cancel_work_sync(&shost->eh_work);
362 if (shost->tmf_work_q)
363 destroy_workqueue(shost->tmf_work_q);
364 if (shost->ehandler)
365 kthread_stop(shost->ehandler);
366 if (shost->work_q)
367 destroy_workqueue(shost->work_q);
368
369 if (state == SHOST_CREATED) {
370 /*
371 * Free the shost_dev device name and remove the proc host dir
372 * here if scsi_host_{alloc,put}() have been called but neither
373 * scsi_host_add() nor scsi_remove_host() has been called.
374 * This avoids that the memory allocated for the shost_dev
375 * name as well as the proc dir structure are leaked.
376 */
377 scsi_proc_hostdir_rm(shost->hostt);
378 kfree(dev_name(&shost->shost_dev));
379 }
380
381 kfree(shost->shost_data);
382
383 ida_free(&host_index_ida, shost->host_no);
384
385 if (state != SHOST_CREATED)
386 put_device(parent);
387 kfree(shost);
388 }
389
390 static const struct device_type scsi_host_type = {
391 .name = "scsi_host",
392 .release = scsi_host_dev_release,
393 };
394
395 /**
396 * scsi_host_alloc - register a scsi host adapter instance.
397 * @sht: pointer to scsi host template
398 * @privsize: extra bytes to allocate for driver
399 *
400 * Note:
401 * Allocate a new Scsi_Host and perform basic initialization.
402 * The host is not published to the scsi midlayer until scsi_add_host
403 * is called.
404 *
405 * Return value:
406 * Pointer to a new Scsi_Host
407 **/
scsi_host_alloc(const struct scsi_host_template * sht,int privsize)408 struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int privsize)
409 {
410 struct Scsi_Host *shost;
411 int index;
412
413 shost = kzalloc(sizeof(struct Scsi_Host) + privsize, GFP_KERNEL);
414 if (!shost)
415 return NULL;
416
417 shost->host_lock = &shost->default_lock;
418 scoped_guard(spinlock_init, shost->host_lock)
419 shost->shost_state = SHOST_CREATED;
420 INIT_LIST_HEAD(&shost->__devices);
421 INIT_LIST_HEAD(&shost->__targets);
422 INIT_LIST_HEAD(&shost->eh_abort_list);
423 INIT_LIST_HEAD(&shost->eh_cmd_q);
424 INIT_LIST_HEAD(&shost->starved_list);
425 init_waitqueue_head(&shost->host_wait);
426 mutex_init(&shost->scan_mutex);
427 INIT_WORK(&shost->eh_work, scsi_rcu_eh_wakeup);
428
429 index = ida_alloc(&host_index_ida, GFP_KERNEL);
430 if (index < 0) {
431 kfree(shost);
432 return NULL;
433 }
434 shost->host_no = index;
435
436 shost->dma_channel = 0xff;
437
438 /* These three are default values which can be overridden */
439 shost->max_channel = 0;
440 shost->max_id = 8;
441 shost->max_lun = 8;
442
443 /* Give each shost a default transportt */
444 shost->transportt = &blank_transport_template;
445
446 /*
447 * All drivers right now should be able to handle 12 byte
448 * commands. Every so often there are requests for 16 byte
449 * commands, but individual low-level drivers need to certify that
450 * they actually do something sensible with such commands.
451 */
452 shost->max_cmd_len = 12;
453 shost->hostt = sht;
454 shost->this_id = sht->this_id;
455 shost->can_queue = sht->can_queue;
456 shost->nr_reserved_cmds = sht->nr_reserved_cmds;
457 shost->sg_tablesize = sht->sg_tablesize;
458 shost->sg_prot_tablesize = sht->sg_prot_tablesize;
459 shost->cmd_per_lun = sht->cmd_per_lun;
460 shost->no_write_same = sht->no_write_same;
461 shost->host_tagset = sht->host_tagset;
462 shost->queuecommand_may_block = sht->queuecommand_may_block;
463
464 if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler)
465 shost->eh_deadline = -1;
466 else if ((ulong) shost_eh_deadline * HZ > INT_MAX) {
467 shost_printk(KERN_WARNING, shost,
468 "eh_deadline %u too large, setting to %u\n",
469 shost_eh_deadline, INT_MAX / HZ);
470 shost->eh_deadline = INT_MAX;
471 } else
472 shost->eh_deadline = shost_eh_deadline * HZ;
473
474 if (sht->supported_mode == MODE_UNKNOWN)
475 /* means we didn't set it ... default to INITIATOR */
476 shost->active_mode = MODE_INITIATOR;
477 else
478 shost->active_mode = sht->supported_mode;
479
480 if (sht->max_host_blocked)
481 shost->max_host_blocked = sht->max_host_blocked;
482 else
483 shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
484
485 /*
486 * If the driver imposes no hard sector transfer limit, start at
487 * machine infinity initially.
488 */
489 if (sht->max_sectors)
490 shost->max_sectors = sht->max_sectors;
491 else
492 shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
493
494 shost->virt_boundary_mask = sht->virt_boundary_mask;
495 if (shost->virt_boundary_mask) {
496 WARN_ON_ONCE(sht->max_segment_size &&
497 sht->max_segment_size != UINT_MAX);
498 shost->max_segment_size = UINT_MAX;
499 } else {
500 if (sht->max_segment_size)
501 shost->max_segment_size = sht->max_segment_size;
502 else
503 shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
504 }
505
506 /* 32-byte (dword) is a common minimum for HBAs. */
507 if (sht->dma_alignment)
508 shost->dma_alignment = sht->dma_alignment;
509 else
510 shost->dma_alignment = 3;
511
512 /*
513 * assume a 4GB boundary, if not set
514 */
515 if (sht->dma_boundary)
516 shost->dma_boundary = sht->dma_boundary;
517 else
518 shost->dma_boundary = 0xffffffff;
519
520 device_initialize(&shost->shost_gendev);
521 dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
522 shost->shost_gendev.bus = &scsi_bus_type;
523 shost->shost_gendev.type = &scsi_host_type;
524 scsi_enable_async_suspend(&shost->shost_gendev);
525
526 device_initialize(&shost->shost_dev);
527 shost->shost_dev.parent = &shost->shost_gendev;
528 shost->shost_dev.class = &shost_class;
529 dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
530 shost->shost_dev.groups = sht->shost_groups;
531
532 shost->ehandler = kthread_run(scsi_error_handler, shost,
533 "scsi_eh_%d", shost->host_no);
534 if (IS_ERR(shost->ehandler)) {
535 shost_printk(KERN_WARNING, shost,
536 "error handler thread failed to spawn, error = %ld\n",
537 PTR_ERR(shost->ehandler));
538 shost->ehandler = NULL;
539 goto fail;
540 }
541
542 shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d",
543 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS,
544 1, shost->host_no);
545 if (!shost->tmf_work_q) {
546 shost_printk(KERN_WARNING, shost,
547 "failed to create tmf workq\n");
548 goto fail;
549 }
550 if (scsi_proc_hostdir_add(shost->hostt) < 0)
551 goto fail;
552 return shost;
553 fail:
554 /*
555 * Host state is still SHOST_CREATED and that is enough to release
556 * ->shost_gendev. scsi_host_dev_release() will free
557 * dev_name(&shost->shost_dev).
558 */
559 put_device(&shost->shost_gendev);
560
561 return NULL;
562 }
563 EXPORT_SYMBOL(scsi_host_alloc);
564
__scsi_host_match(struct device * dev,const void * data)565 static int __scsi_host_match(struct device *dev, const void *data)
566 {
567 struct Scsi_Host *p;
568 const unsigned int *hostnum = data;
569
570 p = class_to_shost(dev);
571 return p->host_no == *hostnum;
572 }
573
574 /**
575 * scsi_host_lookup - get a reference to a Scsi_Host by host no
576 * @hostnum: host number to locate
577 *
578 * Return value:
579 * A pointer to located Scsi_Host or NULL.
580 *
581 * The caller must do a scsi_host_put() to drop the reference
582 * that scsi_host_get() took. The put_device() below dropped
583 * the reference from class_find_device().
584 **/
scsi_host_lookup(unsigned int hostnum)585 struct Scsi_Host *scsi_host_lookup(unsigned int hostnum)
586 {
587 struct device *cdev;
588 struct Scsi_Host *shost = NULL;
589
590 cdev = class_find_device(&shost_class, NULL, &hostnum,
591 __scsi_host_match);
592 if (cdev) {
593 shost = scsi_host_get(class_to_shost(cdev));
594 put_device(cdev);
595 }
596 return shost;
597 }
598 EXPORT_SYMBOL(scsi_host_lookup);
599
600 /**
601 * scsi_host_get - inc a Scsi_Host ref count
602 * @shost: Pointer to Scsi_Host to inc.
603 **/
scsi_host_get(struct Scsi_Host * shost)604 struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
605 {
606 if (scsi_get_host_state(shost) == SHOST_DEL ||
607 !get_device(&shost->shost_gendev))
608 return NULL;
609 return shost;
610 }
611 EXPORT_SYMBOL(scsi_host_get);
612
scsi_host_check_in_flight(struct request * rq,void * data)613 static bool scsi_host_check_in_flight(struct request *rq, void *data)
614 {
615 int *count = data;
616 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
617
618 if (test_bit(SCMD_STATE_INFLIGHT, &cmd->state))
619 (*count)++;
620
621 return true;
622 }
623
624 /**
625 * scsi_host_busy - Return the count of in-flight commands
626 * @shost: Pointer to Scsi_Host
627 **/
scsi_host_busy(struct Scsi_Host * shost)628 int scsi_host_busy(struct Scsi_Host *shost)
629 {
630 int cnt = 0;
631
632 blk_mq_tagset_busy_iter(&shost->tag_set,
633 scsi_host_check_in_flight, &cnt);
634 return cnt;
635 }
636 EXPORT_SYMBOL(scsi_host_busy);
637
638 /**
639 * scsi_host_put - dec a Scsi_Host ref count
640 * @shost: Pointer to Scsi_Host to dec.
641 **/
scsi_host_put(struct Scsi_Host * shost)642 void scsi_host_put(struct Scsi_Host *shost)
643 {
644 put_device(&shost->shost_gendev);
645 }
646 EXPORT_SYMBOL(scsi_host_put);
647
scsi_init_hosts(void)648 int scsi_init_hosts(void)
649 {
650 return class_register(&shost_class);
651 }
652
scsi_exit_hosts(void)653 void scsi_exit_hosts(void)
654 {
655 class_unregister(&shost_class);
656 ida_destroy(&host_index_ida);
657 }
658
scsi_is_host_device(const struct device * dev)659 int scsi_is_host_device(const struct device *dev)
660 {
661 return dev->type == &scsi_host_type;
662 }
663 EXPORT_SYMBOL(scsi_is_host_device);
664
665 /**
666 * scsi_queue_work - Queue work to the Scsi_Host workqueue.
667 * @shost: Pointer to Scsi_Host.
668 * @work: Work to queue for execution.
669 *
670 * Return value:
671 * 1 - work queued for execution
672 * 0 - work is already queued
673 * -EINVAL - work queue doesn't exist
674 **/
scsi_queue_work(struct Scsi_Host * shost,struct work_struct * work)675 int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
676 {
677 if (unlikely(!shost->work_q)) {
678 shost_printk(KERN_ERR, shost,
679 "ERROR: Scsi host '%s' attempted to queue scsi-work, "
680 "when no workqueue created.\n", shost->hostt->name);
681 dump_stack();
682
683 return -EINVAL;
684 }
685
686 return queue_work(shost->work_q, work);
687 }
688 EXPORT_SYMBOL_GPL(scsi_queue_work);
689
690 /**
691 * scsi_flush_work - Flush a Scsi_Host's workqueue.
692 * @shost: Pointer to Scsi_Host.
693 **/
scsi_flush_work(struct Scsi_Host * shost)694 void scsi_flush_work(struct Scsi_Host *shost)
695 {
696 if (!shost->work_q) {
697 shost_printk(KERN_ERR, shost,
698 "ERROR: Scsi host '%s' attempted to flush scsi-work, "
699 "when no workqueue created.\n", shost->hostt->name);
700 dump_stack();
701 return;
702 }
703
704 flush_workqueue(shost->work_q);
705 }
706 EXPORT_SYMBOL_GPL(scsi_flush_work);
707
complete_all_cmds_iter(struct request * rq,void * data)708 static bool complete_all_cmds_iter(struct request *rq, void *data)
709 {
710 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
711 enum scsi_host_status status = *(enum scsi_host_status *)data;
712
713 scsi_dma_unmap(scmd);
714 scmd->result = 0;
715 set_host_byte(scmd, status);
716 scsi_done(scmd);
717 return true;
718 }
719
720 /**
721 * scsi_host_complete_all_commands - Terminate all running commands
722 * @shost: Scsi Host on which commands should be terminated
723 * @status: Status to be set for the terminated commands
724 *
725 * There is no protection against modification of the number
726 * of outstanding commands. It is the responsibility of the
727 * caller to ensure that concurrent I/O submission and/or
728 * completion is stopped when calling this function.
729 */
scsi_host_complete_all_commands(struct Scsi_Host * shost,enum scsi_host_status status)730 void scsi_host_complete_all_commands(struct Scsi_Host *shost,
731 enum scsi_host_status status)
732 {
733 blk_mq_tagset_busy_iter(&shost->tag_set, complete_all_cmds_iter,
734 &status);
735 }
736 EXPORT_SYMBOL_GPL(scsi_host_complete_all_commands);
737
738 struct scsi_host_busy_iter_data {
739 bool (*fn)(struct scsi_cmnd *, void *);
740 void *priv;
741 };
742
__scsi_host_busy_iter_fn(struct request * req,void * priv)743 static bool __scsi_host_busy_iter_fn(struct request *req, void *priv)
744 {
745 struct scsi_host_busy_iter_data *iter_data = priv;
746 struct scsi_cmnd *sc = blk_mq_rq_to_pdu(req);
747
748 return iter_data->fn(sc, iter_data->priv);
749 }
750
751 /**
752 * scsi_host_busy_iter - Iterate over all busy commands
753 * @shost: Pointer to Scsi_Host.
754 * @fn: Function to call on each busy command
755 * @priv: Data pointer passed to @fn
756 *
757 * If locking against concurrent command completions is required
758 * ithas to be provided by the caller
759 **/
scsi_host_busy_iter(struct Scsi_Host * shost,bool (* fn)(struct scsi_cmnd *,void *),void * priv)760 void scsi_host_busy_iter(struct Scsi_Host *shost,
761 bool (*fn)(struct scsi_cmnd *, void *),
762 void *priv)
763 {
764 struct scsi_host_busy_iter_data iter_data = {
765 .fn = fn,
766 .priv = priv,
767 };
768
769 blk_mq_tagset_busy_iter(&shost->tag_set, __scsi_host_busy_iter_fn,
770 &iter_data);
771 }
772 EXPORT_SYMBOL_GPL(scsi_host_busy_iter);
773