hosts.c 16.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *  hosts.c Copyright (C) 1992 Drew Eckhardt
 *          Copyright (C) 1993, 1994, 1995 Eric Youngdale
 *          Copyright (C) 2002-2003 Christoph Hellwig
 *
 *  mid to lowlevel SCSI driver interface
 *      Initial versions: Drew Eckhardt
 *      Subsequent revisions: Eric Youngdale
 *
 *  <drew@colorado.edu>
 *
 *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
 *  Added QLOGIC QLA1280 SCSI controller kernel host support. 
 *     August 4, 1999 Fred Lewis, Intel DuPont
 *
 *  Updated to reflect the new initialization scheme for the higher 
 *  level of scsi drivers (sd/sr/st)
 *  September 17, 2000 Torben Mathiasen <tmm@image.dk>
 *
 *  Restructured scsi_host lists and associated functions.
 *  September 04, 2002 Mike Anderson (andmike@us.ibm.com)
 */

#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/kernel.h>
27
#include <linux/slab.h>
28
#include <linux/kthread.h>
L
Linus Torvalds 已提交
29 30 31 32 33
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/completion.h>
#include <linux/transport_class.h>
34
#include <linux/platform_device.h>
35
#include <linux/pm_runtime.h>
36
#include <linux/idr.h>
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_transport.h>

#include "scsi_priv.h"
#include "scsi_logging.h"


45
static DEFINE_IDA(host_index_ida);
L
Linus Torvalds 已提交
46 47


48
static void scsi_host_cls_release(struct device *dev)
L
Linus Torvalds 已提交
49
{
50
	put_device(&class_to_shost(dev)->shost_gendev);
L
Linus Torvalds 已提交
51 52 53 54
}

static struct class shost_class = {
	.name		= "scsi_host",
55
	.dev_release	= scsi_host_cls_release,
L
Linus Torvalds 已提交
56 57
};

58
/**
59
 *	scsi_host_set_state - Take the given host through the host state model.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
 *	@shost:	scsi host to change the state of.
 *	@state:	state to change to.
 *
 *	Returns zero if unsuccessful or an error if the requested
 *	transition is illegal.
 **/
int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
{
	enum scsi_host_state oldstate = shost->shost_state;

	if (state == oldstate)
		return 0;

	switch (state) {
	case SHOST_CREATED:
		/* There are no legal states that come back to
		 * created.  This is the manually initialised start
		 * state */
		goto illegal;

	case SHOST_RUNNING:
		switch (oldstate) {
		case SHOST_CREATED:
		case SHOST_RECOVERY:
			break;
		default:
			goto illegal;
		}
		break;

	case SHOST_RECOVERY:
		switch (oldstate) {
		case SHOST_RUNNING:
			break;
		default:
			goto illegal;
		}
		break;

	case SHOST_CANCEL:
		switch (oldstate) {
		case SHOST_CREATED:
		case SHOST_RUNNING:
103
		case SHOST_CANCEL_RECOVERY:
104 105 106 107 108 109 110 111 112
			break;
		default:
			goto illegal;
		}
		break;

	case SHOST_DEL:
		switch (oldstate) {
		case SHOST_CANCEL:
113
		case SHOST_DEL_RECOVERY:
114 115 116 117 118 119
			break;
		default:
			goto illegal;
		}
		break;

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	case SHOST_CANCEL_RECOVERY:
		switch (oldstate) {
		case SHOST_CANCEL:
		case SHOST_RECOVERY:
			break;
		default:
			goto illegal;
		}
		break;

	case SHOST_DEL_RECOVERY:
		switch (oldstate) {
		case SHOST_CANCEL_RECOVERY:
			break;
		default:
			goto illegal;
		}
		break;
138 139 140 141 142 143
	}
	shost->shost_state = state;
	return 0;

 illegal:
	SCSI_LOG_ERROR_RECOVERY(1,
144 145 146 147 148
				shost_printk(KERN_ERR, shost,
					     "Illegal host state transition"
					     "%s->%s\n",
					     scsi_host_state_name(oldstate),
					     scsi_host_state_name(state)));
149 150 151 152
	return -EINVAL;
}
EXPORT_SYMBOL(scsi_host_set_state);

L
Linus Torvalds 已提交
153 154 155 156 157 158
/**
 * scsi_remove_host - remove a scsi host
 * @shost:	a pointer to a scsi host to remove
 **/
void scsi_remove_host(struct Scsi_Host *shost)
{
159
	unsigned long flags;
160

161
	mutex_lock(&shost->scan_mutex);
162 163 164 165
	spin_lock_irqsave(shost->host_lock, flags);
	if (scsi_host_set_state(shost, SHOST_CANCEL))
		if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) {
			spin_unlock_irqrestore(shost->host_lock, flags);
166
			mutex_unlock(&shost->scan_mutex);
167 168 169
			return;
		}
	spin_unlock_irqrestore(shost->host_lock, flags);
170 171

	scsi_autopm_get_host(shost);
172
	flush_workqueue(shost->tmf_work_q);
L
Linus Torvalds 已提交
173
	scsi_forget_host(shost);
174
	mutex_unlock(&shost->scan_mutex);
L
Linus Torvalds 已提交
175 176
	scsi_proc_host_rm(shost);

177 178 179 180
	spin_lock_irqsave(shost->host_lock, flags);
	if (scsi_host_set_state(shost, SHOST_DEL))
		BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
	spin_unlock_irqrestore(shost->host_lock, flags);
L
Linus Torvalds 已提交
181 182

	transport_unregister_device(&shost->shost_gendev);
183
	device_unregister(&shost->shost_dev);
L
Linus Torvalds 已提交
184 185 186 187 188
	device_del(&shost->shost_gendev);
}
EXPORT_SYMBOL(scsi_remove_host);

/**
189
 * scsi_add_host_with_dma - add a scsi host with dma device
L
Linus Torvalds 已提交
190 191
 * @shost:	scsi host pointer to add
 * @dev:	a struct device of type scsi class
192 193 194 195 196
 * @dma_dev:	dma device for the host
 *
 * Note: You rarely need to worry about this unless you're in a
 * virtualised host environments, so use the simpler scsi_add_host()
 * function instead.
L
Linus Torvalds 已提交
197 198 199 200
 *
 * Return value: 
 * 	0 on success / != 0 for error
 **/
201 202
int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
			   struct device *dma_dev)
L
Linus Torvalds 已提交
203 204 205 206
{
	struct scsi_host_template *sht = shost->hostt;
	int error = -EINVAL;

207
	shost_printk(KERN_INFO, shost, "%s\n",
L
Linus Torvalds 已提交
208 209 210
			sht->info ? sht->info(shost) : sht->name);

	if (!shost->can_queue) {
211 212
		shost_printk(KERN_ERR, shost,
			     "can_queue = 0 no longer supported\n");
J
James Bottomley 已提交
213
		goto fail;
L
Linus Torvalds 已提交
214 215
	}

216 217 218 219
	error = scsi_init_sense_cache(shost);
	if (error)
		goto fail;

220 221 222 223
	if (shost_use_blk_mq(shost)) {
		error = scsi_mq_setup_tags(shost);
		if (error)
			goto fail;
224 225 226 227 228 229 230
	} else {
		shost->bqt = blk_init_tags(shost->can_queue,
				shost->hostt->tag_alloc_policy);
		if (!shost->bqt) {
			error = -ENOMEM;
			goto fail;
		}
231 232 233 234 235 236 237 238 239 240
	}

	/*
	 * Note that we allocate the freelist even for the MQ case for now,
	 * as we need a command set aside for scsi_reset_provider.  Having
	 * the full host freelist and one command available for that is a
	 * little heavy-handed, but avoids introducing a special allocator
	 * just for this.  Eventually the structure of scsi_reset_provider
	 * will need a major overhaul.
	 */
J
James Bottomley 已提交
241 242
	error = scsi_setup_command_freelist(shost);
	if (error)
243 244
		goto out_destroy_tags;

J
James Bottomley 已提交
245

L
Linus Torvalds 已提交
246 247
	if (!shost->shost_gendev.parent)
		shost->shost_gendev.parent = dev ? dev : &platform_bus;
248 249 250
	if (!dma_dev)
		dma_dev = shost->shost_gendev.parent;

251
	shost->dma_dev = dma_dev;
L
Linus Torvalds 已提交
252

253 254 255 256 257 258
	/*
	 * Increase usage count temporarily here so that calling
	 * scsi_autopm_put_host() will trigger runtime idle if there is
	 * nothing else preventing suspending the device.
	 */
	pm_runtime_get_noresume(&shost->shost_gendev);
259 260 261 262
	pm_runtime_set_active(&shost->shost_gendev);
	pm_runtime_enable(&shost->shost_gendev);
	device_enable_async_suspend(&shost->shost_gendev);

263 264 265 266
	error = device_add(&shost->shost_gendev);
	if (error)
		goto out_destroy_freelist;

267
	scsi_host_set_state(shost, SHOST_RUNNING);
L
Linus Torvalds 已提交
268 269
	get_device(shost->shost_gendev.parent);

270 271
	device_enable_async_suspend(&shost->shost_dev);

272
	error = device_add(&shost->shost_dev);
L
Linus Torvalds 已提交
273 274 275 276 277
	if (error)
		goto out_del_gendev;

	get_device(&shost->shost_gendev);

278 279 280 281 282
	if (shost->transportt->host_size) {
		shost->shost_data = kzalloc(shost->transportt->host_size,
					 GFP_KERNEL);
		if (shost->shost_data == NULL) {
			error = -ENOMEM;
283
			goto out_del_dev;
284 285
		}
	}
L
Linus Torvalds 已提交
286 287

	if (shost->transportt->create_work_queue) {
288 289
		snprintf(shost->work_q_name, sizeof(shost->work_q_name),
			 "scsi_wq_%d", shost->host_no);
L
Linus Torvalds 已提交
290 291
		shost->work_q = create_singlethread_workqueue(
					shost->work_q_name);
292 293
		if (!shost->work_q) {
			error = -EINVAL;
L
Linus Torvalds 已提交
294
			goto out_free_shost_data;
295
		}
L
Linus Torvalds 已提交
296 297 298 299 300 301 302
	}

	error = scsi_sysfs_add_host(shost);
	if (error)
		goto out_destroy_host;

	scsi_proc_host_add(shost);
303
	scsi_autopm_put_host(shost);
L
Linus Torvalds 已提交
304 305 306 307 308 309 310
	return error;

 out_destroy_host:
	if (shost->work_q)
		destroy_workqueue(shost->work_q);
 out_free_shost_data:
	kfree(shost->shost_data);
311 312
 out_del_dev:
	device_del(&shost->shost_dev);
L
Linus Torvalds 已提交
313 314
 out_del_gendev:
	device_del(&shost->shost_gendev);
315
 out_destroy_freelist:
316 317 318 319
	device_disable_async_suspend(&shost->shost_gendev);
	pm_runtime_disable(&shost->shost_gendev);
	pm_runtime_set_suspended(&shost->shost_gendev);
	pm_runtime_put_noidle(&shost->shost_gendev);
J
James Bottomley 已提交
320
	scsi_destroy_command_freelist(shost);
321 322 323
 out_destroy_tags:
	if (shost_use_blk_mq(shost))
		scsi_mq_destroy_tags(shost);
J
James Bottomley 已提交
324
 fail:
L
Linus Torvalds 已提交
325 326
	return error;
}
327
EXPORT_SYMBOL(scsi_add_host_with_dma);
L
Linus Torvalds 已提交
328 329 330 331 332

static void scsi_host_dev_release(struct device *dev)
{
	struct Scsi_Host *shost = dev_to_shost(dev);
	struct device *parent = dev->parent;
333
	struct request_queue *q;
334
	void *queuedata;
L
Linus Torvalds 已提交
335

336 337
	scsi_proc_hostdir_rm(shost->hostt);

338 339
	if (shost->tmf_work_q)
		destroy_workqueue(shost->tmf_work_q);
340 341
	if (shost->ehandler)
		kthread_stop(shost->ehandler);
L
Linus Torvalds 已提交
342 343
	if (shost->work_q)
		destroy_workqueue(shost->work_q);
344 345
	q = shost->uspace_req_q;
	if (q) {
346 347 348
		queuedata = q->queuedata;
		blk_cleanup_queue(q);
		kfree(queuedata);
349
	}
L
Linus Torvalds 已提交
350

351 352 353 354 355 356 357 358 359 360 361
	if (shost->shost_state == SHOST_CREATED) {
		/*
		 * Free the shost_dev device name here if scsi_host_alloc()
		 * and scsi_host_put() have been called but neither
		 * scsi_host_add() nor scsi_host_remove() has been called.
		 * This avoids that the memory allocated for the shost_dev
		 * name is leaked.
		 */
		kfree(dev_name(&shost->shost_dev));
	}

L
Linus Torvalds 已提交
362
	scsi_destroy_command_freelist(shost);
363 364 365 366 367 368 369
	if (shost_use_blk_mq(shost)) {
		if (shost->tag_set.tags)
			scsi_mq_destroy_tags(shost);
	} else {
		if (shost->bqt)
			blk_free_tags(shost->bqt);
	}
370

L
Linus Torvalds 已提交
371 372
	kfree(shost->shost_data);

373 374
	ida_simple_remove(&host_index_ida, shost->host_no);

L
Linus Torvalds 已提交
375 376 377 378 379
	if (parent)
		put_device(parent);
	kfree(shost);
}

380
static int shost_eh_deadline = -1;
381

382
module_param_named(eh_deadline, shost_eh_deadline, int, S_IRUGO|S_IWUSR);
383
MODULE_PARM_DESC(eh_deadline,
384
		 "SCSI EH timeout in seconds (should be between 0 and 2^31-1)");
385

386
static struct device_type scsi_host_type = {
387 388 389 390
	.name =		"scsi_host",
	.release =	scsi_host_dev_release,
};

L
Linus Torvalds 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/**
 * scsi_host_alloc - register a scsi host adapter instance.
 * @sht:	pointer to scsi host template
 * @privsize:	extra bytes to allocate for driver
 *
 * Note:
 * 	Allocate a new Scsi_Host and perform basic initialization.
 * 	The host is not published to the scsi midlayer until scsi_add_host
 * 	is called.
 *
 * Return value:
 * 	Pointer to a new Scsi_Host
 **/
struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
{
	struct Scsi_Host *shost;
A
Al Viro 已提交
407
	gfp_t gfp_mask = GFP_KERNEL;
408
	int index;
L
Linus Torvalds 已提交
409 410 411 412

	if (sht->unchecked_isa_dma && privsize)
		gfp_mask |= __GFP_DMA;

J
Jes Sorensen 已提交
413
	shost = kzalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
L
Linus Torvalds 已提交
414 415 416
	if (!shost)
		return NULL;

C
Christoph Hellwig 已提交
417 418
	shost->host_lock = &shost->default_lock;
	spin_lock_init(shost->host_lock);
419
	shost->shost_state = SHOST_CREATED;
L
Linus Torvalds 已提交
420 421 422 423 424
	INIT_LIST_HEAD(&shost->__devices);
	INIT_LIST_HEAD(&shost->__targets);
	INIT_LIST_HEAD(&shost->eh_cmd_q);
	INIT_LIST_HEAD(&shost->starved_list);
	init_waitqueue_head(&shost->host_wait);
425
	mutex_init(&shost->scan_mutex);
L
Linus Torvalds 已提交
426

427 428 429 430 431
	index = ida_simple_get(&host_index_ida, 0, 0, GFP_KERNEL);
	if (index < 0)
		goto fail_kfree;
	shost->host_no = index;

L
Linus Torvalds 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	shost->dma_channel = 0xff;

	/* These three are default values which can be overridden */
	shost->max_channel = 0;
	shost->max_id = 8;
	shost->max_lun = 8;

	/* Give each shost a default transportt */
	shost->transportt = &blank_transport_template;

	/*
	 * All drivers right now should be able to handle 12 byte
	 * commands.  Every so often there are requests for 16 byte
	 * commands, but individual low-level drivers need to certify that
	 * they actually do something sensible with such commands.
	 */
	shost->max_cmd_len = 12;
	shost->hostt = sht;
	shost->this_id = sht->this_id;
	shost->can_queue = sht->can_queue;
	shost->sg_tablesize = sht->sg_tablesize;
453
	shost->sg_prot_tablesize = sht->sg_prot_tablesize;
L
Linus Torvalds 已提交
454 455 456
	shost->cmd_per_lun = sht->cmd_per_lun;
	shost->unchecked_isa_dma = sht->unchecked_isa_dma;
	shost->use_clustering = sht->use_clustering;
457
	shost->no_write_same = sht->no_write_same;
L
Linus Torvalds 已提交
458

459
	if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler)
460 461 462 463 464 465 466 467 468
		shost->eh_deadline = -1;
	else if ((ulong) shost_eh_deadline * HZ > INT_MAX) {
		shost_printk(KERN_WARNING, shost,
			     "eh_deadline %u too large, setting to %u\n",
			     shost_eh_deadline, INT_MAX / HZ);
		shost->eh_deadline = INT_MAX;
	} else
		shost->eh_deadline = shost_eh_deadline * HZ;

469 470 471 472 473 474
	if (sht->supported_mode == MODE_UNKNOWN)
		/* means we didn't set it ... default to INITIATOR */
		shost->active_mode = MODE_INITIATOR;
	else
		shost->active_mode = sht->supported_mode;

L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	if (sht->max_host_blocked)
		shost->max_host_blocked = sht->max_host_blocked;
	else
		shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;

	/*
	 * If the driver imposes no hard sector transfer limit, start at
	 * machine infinity initially.
	 */
	if (sht->max_sectors)
		shost->max_sectors = sht->max_sectors;
	else
		shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;

	/*
	 * assume a 4GB boundary, if not set
	 */
	if (sht->dma_boundary)
		shost->dma_boundary = sht->dma_boundary;
	else
		shost->dma_boundary = 0xffffffff;

497 498
	shost->use_blk_mq = scsi_use_blk_mq;

L
Linus Torvalds 已提交
499
	device_initialize(&shost->shost_gendev);
500
	dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
501 502
	shost->shost_gendev.bus = &scsi_bus_type;
	shost->shost_gendev.type = &scsi_host_type;
L
Linus Torvalds 已提交
503

504 505 506
	device_initialize(&shost->shost_dev);
	shost->shost_dev.parent = &shost->shost_gendev;
	shost->shost_dev.class = &shost_class;
507
	dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
508
	shost->shost_dev.groups = scsi_sysfs_shost_attr_groups;
L
Linus Torvalds 已提交
509

510 511 512
	shost->ehandler = kthread_run(scsi_error_handler, shost,
			"scsi_eh_%d", shost->host_no);
	if (IS_ERR(shost->ehandler)) {
513 514 515
		shost_printk(KERN_WARNING, shost,
			"error handler thread failed to spawn, error = %ld\n",
			PTR_ERR(shost->ehandler));
516
		goto fail_index_remove;
517
	}
L
Linus Torvalds 已提交
518

519 520 521 522
	shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d",
					    WQ_UNBOUND | WQ_MEM_RECLAIM,
					   1, shost->host_no);
	if (!shost->tmf_work_q) {
523 524
		shost_printk(KERN_WARNING, shost,
			     "failed to create tmf workq\n");
525 526
		goto fail_kthread;
	}
L
Linus Torvalds 已提交
527 528 529
	scsi_proc_hostdir_add(shost->hostt);
	return shost;

530 531
 fail_kthread:
	kthread_stop(shost->ehandler);
532 533
 fail_index_remove:
	ida_simple_remove(&host_index_ida, shost->host_no);
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
 fail_kfree:
	kfree(shost);
	return NULL;
}
EXPORT_SYMBOL(scsi_host_alloc);

struct Scsi_Host *scsi_register(struct scsi_host_template *sht, int privsize)
{
	struct Scsi_Host *shost = scsi_host_alloc(sht, privsize);

	if (!sht->detect) {
		printk(KERN_WARNING "scsi_register() called on new-style "
				    "template for driver %s\n", sht->name);
		dump_stack();
	}

	if (shost)
		list_add_tail(&shost->sht_legacy_list, &sht->legacy_hosts);
	return shost;
}
EXPORT_SYMBOL(scsi_register);

void scsi_unregister(struct Scsi_Host *shost)
{
	list_del(&shost->sht_legacy_list);
	scsi_host_put(shost);
}
EXPORT_SYMBOL(scsi_unregister);

563
static int __scsi_host_match(struct device *dev, const void *data)
D
Dave Young 已提交
564 565
{
	struct Scsi_Host *p;
566
	const unsigned short *hostnum = data;
D
Dave Young 已提交
567

568
	p = class_to_shost(dev);
D
Dave Young 已提交
569 570 571
	return p->host_no == *hostnum;
}

L
Linus Torvalds 已提交
572 573 574 575 576 577
/**
 * scsi_host_lookup - get a reference to a Scsi_Host by host no
 * @hostnum:	host number to locate
 *
 * Return value:
 *	A pointer to located Scsi_Host or NULL.
578 579 580 581
 *
 *	The caller must do a scsi_host_put() to drop the reference
 *	that scsi_host_get() took. The put_device() below dropped
 *	the reference from class_find_device().
L
Linus Torvalds 已提交
582 583 584
 **/
struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
{
585
	struct device *cdev;
586
	struct Scsi_Host *shost = NULL;
L
Linus Torvalds 已提交
587

588 589
	cdev = class_find_device(&shost_class, NULL, &hostnum,
				 __scsi_host_match);
590
	if (cdev) {
D
Dave Young 已提交
591
		shost = scsi_host_get(class_to_shost(cdev));
592 593
		put_device(cdev);
	}
L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601 602 603
	return shost;
}
EXPORT_SYMBOL(scsi_host_lookup);

/**
 * scsi_host_get - inc a Scsi_Host ref count
 * @shost:	Pointer to Scsi_Host to inc.
 **/
struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
{
604
	if ((shost->shost_state == SHOST_DEL) ||
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
		!get_device(&shost->shost_gendev))
		return NULL;
	return shost;
}
EXPORT_SYMBOL(scsi_host_get);

/**
 * scsi_host_put - dec a Scsi_Host ref count
 * @shost:	Pointer to Scsi_Host to dec.
 **/
void scsi_host_put(struct Scsi_Host *shost)
{
	put_device(&shost->shost_gendev);
}
EXPORT_SYMBOL(scsi_host_put);

int scsi_init_hosts(void)
{
	return class_register(&shost_class);
}

void scsi_exit_hosts(void)
{
	class_unregister(&shost_class);
629
	ida_destroy(&host_index_ida);
L
Linus Torvalds 已提交
630 631 632 633
}

int scsi_is_host_device(const struct device *dev)
{
634
	return dev->type == &scsi_host_type;
L
Linus Torvalds 已提交
635 636 637 638 639 640 641 642 643
}
EXPORT_SYMBOL(scsi_is_host_device);

/**
 * scsi_queue_work - Queue work to the Scsi_Host workqueue.
 * @shost:	Pointer to Scsi_Host.
 * @work:	Work to queue for execution.
 *
 * Return value:
644 645 646
 * 	1 - work queued for execution
 *	0 - work is already queued
 *	-EINVAL - work queue doesn't exist
L
Linus Torvalds 已提交
647 648 649 650
 **/
int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
{
	if (unlikely(!shost->work_q)) {
651
		shost_printk(KERN_ERR, shost,
L
Linus Torvalds 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
			"ERROR: Scsi host '%s' attempted to queue scsi-work, "
			"when no workqueue created.\n", shost->hostt->name);
		dump_stack();

		return -EINVAL;
	}

	return queue_work(shost->work_q, work);
}
EXPORT_SYMBOL_GPL(scsi_queue_work);

/**
 * scsi_flush_work - Flush a Scsi_Host's workqueue.
 * @shost:	Pointer to Scsi_Host.
 **/
void scsi_flush_work(struct Scsi_Host *shost)
{
	if (!shost->work_q) {
670
		shost_printk(KERN_ERR, shost,
L
Linus Torvalds 已提交
671 672 673 674 675 676 677 678 679
			"ERROR: Scsi host '%s' attempted to flush scsi-work, "
			"when no workqueue created.\n", shost->hostt->name);
		dump_stack();
		return;
	}

	flush_workqueue(shost->work_q);
}
EXPORT_SYMBOL_GPL(scsi_flush_work);