xref: /openbmc/linux/drivers/scsi/hosts.c (revision dc6a81c3)
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 
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 };
65 
66 /**
67  *	scsi_host_set_state - Take the given host through the host state model.
68  *	@shost:	scsi host to change the state of.
69  *	@state:	state to change to.
70  *
71  *	Returns zero if unsuccessful or an error if the requested
72  *	transition is illegal.
73  **/
74 int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
75 {
76 	enum scsi_host_state oldstate = shost->shost_state;
77 
78 	if (state == oldstate)
79 		return 0;
80 
81 	switch (state) {
82 	case SHOST_CREATED:
83 		/* There are no legal states that come back to
84 		 * created.  This is the manually initialised start
85 		 * state */
86 		goto illegal;
87 
88 	case SHOST_RUNNING:
89 		switch (oldstate) {
90 		case SHOST_CREATED:
91 		case SHOST_RECOVERY:
92 			break;
93 		default:
94 			goto illegal;
95 		}
96 		break;
97 
98 	case SHOST_RECOVERY:
99 		switch (oldstate) {
100 		case SHOST_RUNNING:
101 			break;
102 		default:
103 			goto illegal;
104 		}
105 		break;
106 
107 	case SHOST_CANCEL:
108 		switch (oldstate) {
109 		case SHOST_CREATED:
110 		case SHOST_RUNNING:
111 		case SHOST_CANCEL_RECOVERY:
112 			break;
113 		default:
114 			goto illegal;
115 		}
116 		break;
117 
118 	case SHOST_DEL:
119 		switch (oldstate) {
120 		case SHOST_CANCEL:
121 		case SHOST_DEL_RECOVERY:
122 			break;
123 		default:
124 			goto illegal;
125 		}
126 		break;
127 
128 	case SHOST_CANCEL_RECOVERY:
129 		switch (oldstate) {
130 		case SHOST_CANCEL:
131 		case SHOST_RECOVERY:
132 			break;
133 		default:
134 			goto illegal;
135 		}
136 		break;
137 
138 	case SHOST_DEL_RECOVERY:
139 		switch (oldstate) {
140 		case SHOST_CANCEL_RECOVERY:
141 			break;
142 		default:
143 			goto illegal;
144 		}
145 		break;
146 	}
147 	shost->shost_state = state;
148 	return 0;
149 
150  illegal:
151 	SCSI_LOG_ERROR_RECOVERY(1,
152 				shost_printk(KERN_ERR, shost,
153 					     "Illegal host state transition"
154 					     "%s->%s\n",
155 					     scsi_host_state_name(oldstate),
156 					     scsi_host_state_name(state)));
157 	return -EINVAL;
158 }
159 
160 /**
161  * scsi_remove_host - remove a scsi host
162  * @shost:	a pointer to a scsi host to remove
163  **/
164 void scsi_remove_host(struct Scsi_Host *shost)
165 {
166 	unsigned long flags;
167 
168 	mutex_lock(&shost->scan_mutex);
169 	spin_lock_irqsave(shost->host_lock, flags);
170 	if (scsi_host_set_state(shost, SHOST_CANCEL))
171 		if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) {
172 			spin_unlock_irqrestore(shost->host_lock, flags);
173 			mutex_unlock(&shost->scan_mutex);
174 			return;
175 		}
176 	spin_unlock_irqrestore(shost->host_lock, flags);
177 
178 	scsi_autopm_get_host(shost);
179 	flush_workqueue(shost->tmf_work_q);
180 	scsi_forget_host(shost);
181 	mutex_unlock(&shost->scan_mutex);
182 	scsi_proc_host_rm(shost);
183 
184 	spin_lock_irqsave(shost->host_lock, flags);
185 	if (scsi_host_set_state(shost, SHOST_DEL))
186 		BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
187 	spin_unlock_irqrestore(shost->host_lock, flags);
188 
189 	transport_unregister_device(&shost->shost_gendev);
190 	device_unregister(&shost->shost_dev);
191 	device_del(&shost->shost_gendev);
192 }
193 EXPORT_SYMBOL(scsi_remove_host);
194 
195 /**
196  * scsi_add_host_with_dma - add a scsi host with dma device
197  * @shost:	scsi host pointer to add
198  * @dev:	a struct device of type scsi class
199  * @dma_dev:	dma device for the host
200  *
201  * Note: You rarely need to worry about this unless you're in a
202  * virtualised host environments, so use the simpler scsi_add_host()
203  * function instead.
204  *
205  * Return value:
206  * 	0 on success / != 0 for error
207  **/
208 int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
209 			   struct device *dma_dev)
210 {
211 	struct scsi_host_template *sht = shost->hostt;
212 	int error = -EINVAL;
213 
214 	shost_printk(KERN_INFO, shost, "%s\n",
215 			sht->info ? sht->info(shost) : sht->name);
216 
217 	if (!shost->can_queue) {
218 		shost_printk(KERN_ERR, shost,
219 			     "can_queue = 0 no longer supported\n");
220 		goto fail;
221 	}
222 
223 	error = scsi_init_sense_cache(shost);
224 	if (error)
225 		goto fail;
226 
227 	error = scsi_mq_setup_tags(shost);
228 	if (error)
229 		goto fail;
230 
231 	if (!shost->shost_gendev.parent)
232 		shost->shost_gendev.parent = dev ? dev : &platform_bus;
233 	if (!dma_dev)
234 		dma_dev = shost->shost_gendev.parent;
235 
236 	shost->dma_dev = dma_dev;
237 
238 	/*
239 	 * Increase usage count temporarily here so that calling
240 	 * scsi_autopm_put_host() will trigger runtime idle if there is
241 	 * nothing else preventing suspending the device.
242 	 */
243 	pm_runtime_get_noresume(&shost->shost_gendev);
244 	pm_runtime_set_active(&shost->shost_gendev);
245 	pm_runtime_enable(&shost->shost_gendev);
246 	device_enable_async_suspend(&shost->shost_gendev);
247 
248 	error = device_add(&shost->shost_gendev);
249 	if (error)
250 		goto out_disable_runtime_pm;
251 
252 	scsi_host_set_state(shost, SHOST_RUNNING);
253 	get_device(shost->shost_gendev.parent);
254 
255 	device_enable_async_suspend(&shost->shost_dev);
256 
257 	error = device_add(&shost->shost_dev);
258 	if (error)
259 		goto out_del_gendev;
260 
261 	get_device(&shost->shost_gendev);
262 
263 	if (shost->transportt->host_size) {
264 		shost->shost_data = kzalloc(shost->transportt->host_size,
265 					 GFP_KERNEL);
266 		if (shost->shost_data == NULL) {
267 			error = -ENOMEM;
268 			goto out_del_dev;
269 		}
270 	}
271 
272 	if (shost->transportt->create_work_queue) {
273 		snprintf(shost->work_q_name, sizeof(shost->work_q_name),
274 			 "scsi_wq_%d", shost->host_no);
275 		shost->work_q = create_singlethread_workqueue(
276 					shost->work_q_name);
277 		if (!shost->work_q) {
278 			error = -EINVAL;
279 			goto out_free_shost_data;
280 		}
281 	}
282 
283 	error = scsi_sysfs_add_host(shost);
284 	if (error)
285 		goto out_destroy_host;
286 
287 	scsi_proc_host_add(shost);
288 	scsi_autopm_put_host(shost);
289 	return error;
290 
291  out_destroy_host:
292 	if (shost->work_q)
293 		destroy_workqueue(shost->work_q);
294  out_free_shost_data:
295 	kfree(shost->shost_data);
296  out_del_dev:
297 	device_del(&shost->shost_dev);
298  out_del_gendev:
299 	device_del(&shost->shost_gendev);
300  out_disable_runtime_pm:
301 	device_disable_async_suspend(&shost->shost_gendev);
302 	pm_runtime_disable(&shost->shost_gendev);
303 	pm_runtime_set_suspended(&shost->shost_gendev);
304 	pm_runtime_put_noidle(&shost->shost_gendev);
305 	scsi_mq_destroy_tags(shost);
306  fail:
307 	return error;
308 }
309 EXPORT_SYMBOL(scsi_add_host_with_dma);
310 
311 static void scsi_host_dev_release(struct device *dev)
312 {
313 	struct Scsi_Host *shost = dev_to_shost(dev);
314 	struct device *parent = dev->parent;
315 
316 	scsi_proc_hostdir_rm(shost->hostt);
317 
318 	/* Wait for functions invoked through call_rcu(&shost->rcu, ...) */
319 	rcu_barrier();
320 
321 	if (shost->tmf_work_q)
322 		destroy_workqueue(shost->tmf_work_q);
323 	if (shost->ehandler)
324 		kthread_stop(shost->ehandler);
325 	if (shost->work_q)
326 		destroy_workqueue(shost->work_q);
327 
328 	if (shost->shost_state == SHOST_CREATED) {
329 		/*
330 		 * Free the shost_dev device name here if scsi_host_alloc()
331 		 * and scsi_host_put() have been called but neither
332 		 * scsi_host_add() nor scsi_host_remove() has been called.
333 		 * This avoids that the memory allocated for the shost_dev
334 		 * name is leaked.
335 		 */
336 		kfree(dev_name(&shost->shost_dev));
337 	}
338 
339 	if (shost->tag_set.tags)
340 		scsi_mq_destroy_tags(shost);
341 
342 	kfree(shost->shost_data);
343 
344 	ida_simple_remove(&host_index_ida, shost->host_no);
345 
346 	if (parent)
347 		put_device(parent);
348 	kfree(shost);
349 }
350 
351 static struct device_type scsi_host_type = {
352 	.name =		"scsi_host",
353 	.release =	scsi_host_dev_release,
354 };
355 
356 /**
357  * scsi_host_alloc - register a scsi host adapter instance.
358  * @sht:	pointer to scsi host template
359  * @privsize:	extra bytes to allocate for driver
360  *
361  * Note:
362  * 	Allocate a new Scsi_Host and perform basic initialization.
363  * 	The host is not published to the scsi midlayer until scsi_add_host
364  * 	is called.
365  *
366  * Return value:
367  * 	Pointer to a new Scsi_Host
368  **/
369 struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
370 {
371 	struct Scsi_Host *shost;
372 	gfp_t gfp_mask = GFP_KERNEL;
373 	int index;
374 
375 	if (sht->unchecked_isa_dma && privsize)
376 		gfp_mask |= __GFP_DMA;
377 
378 	shost = kzalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
379 	if (!shost)
380 		return NULL;
381 
382 	shost->host_lock = &shost->default_lock;
383 	spin_lock_init(shost->host_lock);
384 	shost->shost_state = SHOST_CREATED;
385 	INIT_LIST_HEAD(&shost->__devices);
386 	INIT_LIST_HEAD(&shost->__targets);
387 	INIT_LIST_HEAD(&shost->eh_cmd_q);
388 	INIT_LIST_HEAD(&shost->starved_list);
389 	init_waitqueue_head(&shost->host_wait);
390 	mutex_init(&shost->scan_mutex);
391 
392 	index = ida_simple_get(&host_index_ida, 0, 0, GFP_KERNEL);
393 	if (index < 0)
394 		goto fail_kfree;
395 	shost->host_no = index;
396 
397 	shost->dma_channel = 0xff;
398 
399 	/* These three are default values which can be overridden */
400 	shost->max_channel = 0;
401 	shost->max_id = 8;
402 	shost->max_lun = 8;
403 
404 	/* Give each shost a default transportt */
405 	shost->transportt = &blank_transport_template;
406 
407 	/*
408 	 * All drivers right now should be able to handle 12 byte
409 	 * commands.  Every so often there are requests for 16 byte
410 	 * commands, but individual low-level drivers need to certify that
411 	 * they actually do something sensible with such commands.
412 	 */
413 	shost->max_cmd_len = 12;
414 	shost->hostt = sht;
415 	shost->this_id = sht->this_id;
416 	shost->can_queue = sht->can_queue;
417 	shost->sg_tablesize = sht->sg_tablesize;
418 	shost->sg_prot_tablesize = sht->sg_prot_tablesize;
419 	shost->cmd_per_lun = sht->cmd_per_lun;
420 	shost->unchecked_isa_dma = sht->unchecked_isa_dma;
421 	shost->no_write_same = sht->no_write_same;
422 
423 	if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler)
424 		shost->eh_deadline = -1;
425 	else if ((ulong) shost_eh_deadline * HZ > INT_MAX) {
426 		shost_printk(KERN_WARNING, shost,
427 			     "eh_deadline %u too large, setting to %u\n",
428 			     shost_eh_deadline, INT_MAX / HZ);
429 		shost->eh_deadline = INT_MAX;
430 	} else
431 		shost->eh_deadline = shost_eh_deadline * HZ;
432 
433 	if (sht->supported_mode == MODE_UNKNOWN)
434 		/* means we didn't set it ... default to INITIATOR */
435 		shost->active_mode = MODE_INITIATOR;
436 	else
437 		shost->active_mode = sht->supported_mode;
438 
439 	if (sht->max_host_blocked)
440 		shost->max_host_blocked = sht->max_host_blocked;
441 	else
442 		shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
443 
444 	/*
445 	 * If the driver imposes no hard sector transfer limit, start at
446 	 * machine infinity initially.
447 	 */
448 	if (sht->max_sectors)
449 		shost->max_sectors = sht->max_sectors;
450 	else
451 		shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
452 
453 	if (sht->max_segment_size)
454 		shost->max_segment_size = sht->max_segment_size;
455 	else
456 		shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
457 
458 	/*
459 	 * assume a 4GB boundary, if not set
460 	 */
461 	if (sht->dma_boundary)
462 		shost->dma_boundary = sht->dma_boundary;
463 	else
464 		shost->dma_boundary = 0xffffffff;
465 
466 	if (sht->virt_boundary_mask)
467 		shost->virt_boundary_mask = sht->virt_boundary_mask;
468 
469 	device_initialize(&shost->shost_gendev);
470 	dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
471 	shost->shost_gendev.bus = &scsi_bus_type;
472 	shost->shost_gendev.type = &scsi_host_type;
473 
474 	device_initialize(&shost->shost_dev);
475 	shost->shost_dev.parent = &shost->shost_gendev;
476 	shost->shost_dev.class = &shost_class;
477 	dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
478 	shost->shost_dev.groups = scsi_sysfs_shost_attr_groups;
479 
480 	shost->ehandler = kthread_run(scsi_error_handler, shost,
481 			"scsi_eh_%d", shost->host_no);
482 	if (IS_ERR(shost->ehandler)) {
483 		shost_printk(KERN_WARNING, shost,
484 			"error handler thread failed to spawn, error = %ld\n",
485 			PTR_ERR(shost->ehandler));
486 		goto fail_index_remove;
487 	}
488 
489 	shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d",
490 					    WQ_UNBOUND | WQ_MEM_RECLAIM,
491 					   1, shost->host_no);
492 	if (!shost->tmf_work_q) {
493 		shost_printk(KERN_WARNING, shost,
494 			     "failed to create tmf workq\n");
495 		goto fail_kthread;
496 	}
497 	scsi_proc_hostdir_add(shost->hostt);
498 	return shost;
499 
500  fail_kthread:
501 	kthread_stop(shost->ehandler);
502  fail_index_remove:
503 	ida_simple_remove(&host_index_ida, shost->host_no);
504  fail_kfree:
505 	kfree(shost);
506 	return NULL;
507 }
508 EXPORT_SYMBOL(scsi_host_alloc);
509 
510 static int __scsi_host_match(struct device *dev, const void *data)
511 {
512 	struct Scsi_Host *p;
513 	const unsigned short *hostnum = data;
514 
515 	p = class_to_shost(dev);
516 	return p->host_no == *hostnum;
517 }
518 
519 /**
520  * scsi_host_lookup - get a reference to a Scsi_Host by host no
521  * @hostnum:	host number to locate
522  *
523  * Return value:
524  *	A pointer to located Scsi_Host or NULL.
525  *
526  *	The caller must do a scsi_host_put() to drop the reference
527  *	that scsi_host_get() took. The put_device() below dropped
528  *	the reference from class_find_device().
529  **/
530 struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
531 {
532 	struct device *cdev;
533 	struct Scsi_Host *shost = NULL;
534 
535 	cdev = class_find_device(&shost_class, NULL, &hostnum,
536 				 __scsi_host_match);
537 	if (cdev) {
538 		shost = scsi_host_get(class_to_shost(cdev));
539 		put_device(cdev);
540 	}
541 	return shost;
542 }
543 EXPORT_SYMBOL(scsi_host_lookup);
544 
545 /**
546  * scsi_host_get - inc a Scsi_Host ref count
547  * @shost:	Pointer to Scsi_Host to inc.
548  **/
549 struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
550 {
551 	if ((shost->shost_state == SHOST_DEL) ||
552 		!get_device(&shost->shost_gendev))
553 		return NULL;
554 	return shost;
555 }
556 EXPORT_SYMBOL(scsi_host_get);
557 
558 static bool scsi_host_check_in_flight(struct request *rq, void *data,
559 				      bool reserved)
560 {
561 	int *count = data;
562 	struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
563 
564 	if (test_bit(SCMD_STATE_INFLIGHT, &cmd->state))
565 		(*count)++;
566 
567 	return true;
568 }
569 
570 /**
571  * scsi_host_busy - Return the host busy counter
572  * @shost:	Pointer to Scsi_Host to inc.
573  **/
574 int scsi_host_busy(struct Scsi_Host *shost)
575 {
576 	int cnt = 0;
577 
578 	blk_mq_tagset_busy_iter(&shost->tag_set,
579 				scsi_host_check_in_flight, &cnt);
580 	return cnt;
581 }
582 EXPORT_SYMBOL(scsi_host_busy);
583 
584 /**
585  * scsi_host_put - dec a Scsi_Host ref count
586  * @shost:	Pointer to Scsi_Host to dec.
587  **/
588 void scsi_host_put(struct Scsi_Host *shost)
589 {
590 	put_device(&shost->shost_gendev);
591 }
592 EXPORT_SYMBOL(scsi_host_put);
593 
594 int scsi_init_hosts(void)
595 {
596 	return class_register(&shost_class);
597 }
598 
599 void scsi_exit_hosts(void)
600 {
601 	class_unregister(&shost_class);
602 	ida_destroy(&host_index_ida);
603 }
604 
605 int scsi_is_host_device(const struct device *dev)
606 {
607 	return dev->type == &scsi_host_type;
608 }
609 EXPORT_SYMBOL(scsi_is_host_device);
610 
611 /**
612  * scsi_queue_work - Queue work to the Scsi_Host workqueue.
613  * @shost:	Pointer to Scsi_Host.
614  * @work:	Work to queue for execution.
615  *
616  * Return value:
617  * 	1 - work queued for execution
618  *	0 - work is already queued
619  *	-EINVAL - work queue doesn't exist
620  **/
621 int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
622 {
623 	if (unlikely(!shost->work_q)) {
624 		shost_printk(KERN_ERR, shost,
625 			"ERROR: Scsi host '%s' attempted to queue scsi-work, "
626 			"when no workqueue created.\n", shost->hostt->name);
627 		dump_stack();
628 
629 		return -EINVAL;
630 	}
631 
632 	return queue_work(shost->work_q, work);
633 }
634 EXPORT_SYMBOL_GPL(scsi_queue_work);
635 
636 /**
637  * scsi_flush_work - Flush a Scsi_Host's workqueue.
638  * @shost:	Pointer to Scsi_Host.
639  **/
640 void scsi_flush_work(struct Scsi_Host *shost)
641 {
642 	if (!shost->work_q) {
643 		shost_printk(KERN_ERR, shost,
644 			"ERROR: Scsi host '%s' attempted to flush scsi-work, "
645 			"when no workqueue created.\n", shost->hostt->name);
646 		dump_stack();
647 		return;
648 	}
649 
650 	flush_workqueue(shost->work_q);
651 }
652 EXPORT_SYMBOL_GPL(scsi_flush_work);
653