Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * scsi.c Copyright (C) 1992 Drew Eckhardt |
| 3 | * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale |
| 4 | * Copyright (C) 2002, 2003 Christoph Hellwig |
| 5 | * |
| 6 | * generic mid-level SCSI driver |
| 7 | * Initial versions: Drew Eckhardt |
| 8 | * Subsequent revisions: Eric Youngdale |
| 9 | * |
| 10 | * <drew@colorado.edu> |
| 11 | * |
| 12 | * Bug correction thanks go to : |
| 13 | * Rik Faith <faith@cs.unc.edu> |
| 14 | * Tommy Thorn <tthorn> |
| 15 | * Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de> |
| 16 | * |
| 17 | * Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to |
| 18 | * add scatter-gather, multiple outstanding request, and other |
| 19 | * enhancements. |
| 20 | * |
| 21 | * Native multichannel, wide scsi, /proc/scsi and hot plugging |
| 22 | * support added by Michael Neuffer <mike@i-connect.net> |
| 23 | * |
| 24 | * Added request_module("scsi_hostadapter") for kerneld: |
| 25 | * (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modprobe.conf) |
| 26 | * Bjorn Ekwall <bj0rn@blox.se> |
| 27 | * (changed to kmod) |
| 28 | * |
| 29 | * Major improvements to the timeout, abort, and reset processing, |
| 30 | * as well as performance modifications for large queue depths by |
| 31 | * Leonard N. Zubkoff <lnz@dandelion.com> |
| 32 | * |
| 33 | * Converted cli() code to spinlocks, Ingo Molnar |
| 34 | * |
| 35 | * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli |
| 36 | * |
| 37 | * out_of_space hacks, D. Gilbert (dpg) 990608 |
| 38 | */ |
| 39 | |
| 40 | #include <linux/module.h> |
| 41 | #include <linux/moduleparam.h> |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/sched.h> |
| 44 | #include <linux/timer.h> |
| 45 | #include <linux/string.h> |
| 46 | #include <linux/slab.h> |
| 47 | #include <linux/blkdev.h> |
| 48 | #include <linux/delay.h> |
| 49 | #include <linux/init.h> |
| 50 | #include <linux/completion.h> |
| 51 | #include <linux/devfs_fs_kernel.h> |
| 52 | #include <linux/unistd.h> |
| 53 | #include <linux/spinlock.h> |
| 54 | #include <linux/kmod.h> |
| 55 | #include <linux/interrupt.h> |
| 56 | #include <linux/notifier.h> |
| 57 | #include <linux/cpu.h> |
| 58 | |
| 59 | #include <scsi/scsi.h> |
| 60 | #include <scsi/scsi_cmnd.h> |
| 61 | #include <scsi/scsi_dbg.h> |
| 62 | #include <scsi/scsi_device.h> |
| 63 | #include <scsi/scsi_eh.h> |
| 64 | #include <scsi/scsi_host.h> |
| 65 | #include <scsi/scsi_tcq.h> |
| 66 | #include <scsi/scsi_request.h> |
| 67 | |
| 68 | #include "scsi_priv.h" |
| 69 | #include "scsi_logging.h" |
| 70 | |
Adrian Bunk | 52c1da3 | 2005-06-23 22:05:33 -0700 | [diff] [blame^] | 71 | static void scsi_done(struct scsi_cmnd *cmd); |
| 72 | static int scsi_retry_command(struct scsi_cmnd *cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Definitions and constants. |
| 76 | */ |
| 77 | |
| 78 | #define MIN_RESET_DELAY (2*HZ) |
| 79 | |
| 80 | /* Do not call reset on error if we just did a reset within 15 sec. */ |
| 81 | #define MIN_RESET_PERIOD (15*HZ) |
| 82 | |
| 83 | /* |
| 84 | * Macro to determine the size of SCSI command. This macro takes vendor |
| 85 | * unique commands into account. SCSI commands in groups 6 and 7 are |
| 86 | * vendor unique and we will depend upon the command length being |
| 87 | * supplied correctly in cmd_len. |
| 88 | */ |
| 89 | #define CDB_SIZE(cmd) (((((cmd)->cmnd[0] >> 5) & 7) < 6) ? \ |
| 90 | COMMAND_SIZE((cmd)->cmnd[0]) : (cmd)->cmd_len) |
| 91 | |
| 92 | /* |
| 93 | * Note - the initial logging level can be set here to log events at boot time. |
| 94 | * After the system is up, you may enable logging via the /proc interface. |
| 95 | */ |
| 96 | unsigned int scsi_logging_level; |
| 97 | #if defined(CONFIG_SCSI_LOGGING) |
| 98 | EXPORT_SYMBOL(scsi_logging_level); |
| 99 | #endif |
| 100 | |
| 101 | const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] = { |
| 102 | "Direct-Access ", |
| 103 | "Sequential-Access", |
| 104 | "Printer ", |
| 105 | "Processor ", |
| 106 | "WORM ", |
| 107 | "CD-ROM ", |
| 108 | "Scanner ", |
| 109 | "Optical Device ", |
| 110 | "Medium Changer ", |
| 111 | "Communications ", |
| 112 | "Unknown ", |
| 113 | "Unknown ", |
| 114 | "RAID ", |
| 115 | "Enclosure ", |
| 116 | }; |
| 117 | EXPORT_SYMBOL(scsi_device_types); |
| 118 | |
| 119 | /* |
| 120 | * Function: scsi_allocate_request |
| 121 | * |
| 122 | * Purpose: Allocate a request descriptor. |
| 123 | * |
| 124 | * Arguments: device - device for which we want a request |
| 125 | * gfp_mask - allocation flags passed to kmalloc |
| 126 | * |
| 127 | * Lock status: No locks assumed to be held. This function is SMP-safe. |
| 128 | * |
| 129 | * Returns: Pointer to request block. |
| 130 | */ |
| 131 | struct scsi_request *scsi_allocate_request(struct scsi_device *sdev, |
| 132 | int gfp_mask) |
| 133 | { |
| 134 | const int offset = ALIGN(sizeof(struct scsi_request), 4); |
| 135 | const int size = offset + sizeof(struct request); |
| 136 | struct scsi_request *sreq; |
| 137 | |
| 138 | sreq = kmalloc(size, gfp_mask); |
| 139 | if (likely(sreq != NULL)) { |
| 140 | memset(sreq, 0, size); |
| 141 | sreq->sr_request = (struct request *)(((char *)sreq) + offset); |
| 142 | sreq->sr_device = sdev; |
| 143 | sreq->sr_host = sdev->host; |
| 144 | sreq->sr_magic = SCSI_REQ_MAGIC; |
| 145 | sreq->sr_data_direction = DMA_BIDIRECTIONAL; |
| 146 | } |
| 147 | |
| 148 | return sreq; |
| 149 | } |
| 150 | EXPORT_SYMBOL(scsi_allocate_request); |
| 151 | |
| 152 | void __scsi_release_request(struct scsi_request *sreq) |
| 153 | { |
| 154 | struct request *req = sreq->sr_request; |
| 155 | |
| 156 | /* unlikely because the tag was usually ended earlier by the |
| 157 | * mid-layer. However, for layering reasons ULD's don't end |
| 158 | * the tag of commands they generate. */ |
| 159 | if (unlikely(blk_rq_tagged(req))) { |
| 160 | unsigned long flags; |
| 161 | struct request_queue *q = req->q; |
| 162 | |
| 163 | spin_lock_irqsave(q->queue_lock, flags); |
| 164 | blk_queue_end_tag(q, req); |
| 165 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | if (likely(sreq->sr_command != NULL)) { |
| 170 | struct scsi_cmnd *cmd = sreq->sr_command; |
| 171 | |
| 172 | sreq->sr_command = NULL; |
| 173 | scsi_next_command(cmd); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Function: scsi_release_request |
| 179 | * |
| 180 | * Purpose: Release a request descriptor. |
| 181 | * |
| 182 | * Arguments: sreq - request to release |
| 183 | * |
| 184 | * Lock status: No locks assumed to be held. This function is SMP-safe. |
| 185 | */ |
| 186 | void scsi_release_request(struct scsi_request *sreq) |
| 187 | { |
| 188 | __scsi_release_request(sreq); |
| 189 | kfree(sreq); |
| 190 | } |
| 191 | EXPORT_SYMBOL(scsi_release_request); |
| 192 | |
| 193 | struct scsi_host_cmd_pool { |
| 194 | kmem_cache_t *slab; |
| 195 | unsigned int users; |
| 196 | char *name; |
| 197 | unsigned int slab_flags; |
| 198 | unsigned int gfp_mask; |
| 199 | }; |
| 200 | |
| 201 | static struct scsi_host_cmd_pool scsi_cmd_pool = { |
| 202 | .name = "scsi_cmd_cache", |
| 203 | .slab_flags = SLAB_HWCACHE_ALIGN, |
| 204 | }; |
| 205 | |
| 206 | static struct scsi_host_cmd_pool scsi_cmd_dma_pool = { |
| 207 | .name = "scsi_cmd_cache(DMA)", |
| 208 | .slab_flags = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA, |
| 209 | .gfp_mask = __GFP_DMA, |
| 210 | }; |
| 211 | |
| 212 | static DECLARE_MUTEX(host_cmd_pool_mutex); |
| 213 | |
| 214 | static struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost, |
| 215 | int gfp_mask) |
| 216 | { |
| 217 | struct scsi_cmnd *cmd; |
| 218 | |
| 219 | cmd = kmem_cache_alloc(shost->cmd_pool->slab, |
| 220 | gfp_mask | shost->cmd_pool->gfp_mask); |
| 221 | |
| 222 | if (unlikely(!cmd)) { |
| 223 | unsigned long flags; |
| 224 | |
| 225 | spin_lock_irqsave(&shost->free_list_lock, flags); |
| 226 | if (likely(!list_empty(&shost->free_list))) { |
| 227 | cmd = list_entry(shost->free_list.next, |
| 228 | struct scsi_cmnd, list); |
| 229 | list_del_init(&cmd->list); |
| 230 | } |
| 231 | spin_unlock_irqrestore(&shost->free_list_lock, flags); |
| 232 | } |
| 233 | |
| 234 | return cmd; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Function: scsi_get_command() |
| 239 | * |
| 240 | * Purpose: Allocate and setup a scsi command block |
| 241 | * |
| 242 | * Arguments: dev - parent scsi device |
| 243 | * gfp_mask- allocator flags |
| 244 | * |
| 245 | * Returns: The allocated scsi command structure. |
| 246 | */ |
| 247 | struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, int gfp_mask) |
| 248 | { |
| 249 | struct scsi_cmnd *cmd; |
| 250 | |
| 251 | /* Bail if we can't get a reference to the device */ |
| 252 | if (!get_device(&dev->sdev_gendev)) |
| 253 | return NULL; |
| 254 | |
| 255 | cmd = __scsi_get_command(dev->host, gfp_mask); |
| 256 | |
| 257 | if (likely(cmd != NULL)) { |
| 258 | unsigned long flags; |
| 259 | |
| 260 | memset(cmd, 0, sizeof(*cmd)); |
| 261 | cmd->device = dev; |
| 262 | cmd->state = SCSI_STATE_UNUSED; |
| 263 | cmd->owner = SCSI_OWNER_NOBODY; |
| 264 | init_timer(&cmd->eh_timeout); |
| 265 | INIT_LIST_HEAD(&cmd->list); |
| 266 | spin_lock_irqsave(&dev->list_lock, flags); |
| 267 | list_add_tail(&cmd->list, &dev->cmd_list); |
| 268 | spin_unlock_irqrestore(&dev->list_lock, flags); |
| 269 | } else |
| 270 | put_device(&dev->sdev_gendev); |
| 271 | |
| 272 | return cmd; |
| 273 | } |
| 274 | EXPORT_SYMBOL(scsi_get_command); |
| 275 | |
| 276 | /* |
| 277 | * Function: scsi_put_command() |
| 278 | * |
| 279 | * Purpose: Free a scsi command block |
| 280 | * |
| 281 | * Arguments: cmd - command block to free |
| 282 | * |
| 283 | * Returns: Nothing. |
| 284 | * |
| 285 | * Notes: The command must not belong to any lists. |
| 286 | */ |
| 287 | void scsi_put_command(struct scsi_cmnd *cmd) |
| 288 | { |
| 289 | struct scsi_device *sdev = cmd->device; |
| 290 | struct Scsi_Host *shost = sdev->host; |
| 291 | unsigned long flags; |
| 292 | |
| 293 | /* serious error if the command hasn't come from a device list */ |
| 294 | spin_lock_irqsave(&cmd->device->list_lock, flags); |
| 295 | BUG_ON(list_empty(&cmd->list)); |
| 296 | list_del_init(&cmd->list); |
| 297 | spin_unlock(&cmd->device->list_lock); |
| 298 | /* changing locks here, don't need to restore the irq state */ |
| 299 | spin_lock(&shost->free_list_lock); |
| 300 | if (unlikely(list_empty(&shost->free_list))) { |
| 301 | list_add(&cmd->list, &shost->free_list); |
| 302 | cmd = NULL; |
| 303 | } |
| 304 | spin_unlock_irqrestore(&shost->free_list_lock, flags); |
| 305 | |
| 306 | if (likely(cmd != NULL)) |
| 307 | kmem_cache_free(shost->cmd_pool->slab, cmd); |
| 308 | |
| 309 | put_device(&sdev->sdev_gendev); |
| 310 | } |
| 311 | EXPORT_SYMBOL(scsi_put_command); |
| 312 | |
| 313 | /* |
| 314 | * Function: scsi_setup_command_freelist() |
| 315 | * |
| 316 | * Purpose: Setup the command freelist for a scsi host. |
| 317 | * |
| 318 | * Arguments: shost - host to allocate the freelist for. |
| 319 | * |
| 320 | * Returns: Nothing. |
| 321 | */ |
| 322 | int scsi_setup_command_freelist(struct Scsi_Host *shost) |
| 323 | { |
| 324 | struct scsi_host_cmd_pool *pool; |
| 325 | struct scsi_cmnd *cmd; |
| 326 | |
| 327 | spin_lock_init(&shost->free_list_lock); |
| 328 | INIT_LIST_HEAD(&shost->free_list); |
| 329 | |
| 330 | /* |
| 331 | * Select a command slab for this host and create it if not |
| 332 | * yet existant. |
| 333 | */ |
| 334 | down(&host_cmd_pool_mutex); |
| 335 | pool = (shost->unchecked_isa_dma ? &scsi_cmd_dma_pool : &scsi_cmd_pool); |
| 336 | if (!pool->users) { |
| 337 | pool->slab = kmem_cache_create(pool->name, |
| 338 | sizeof(struct scsi_cmnd), 0, |
| 339 | pool->slab_flags, NULL, NULL); |
| 340 | if (!pool->slab) |
| 341 | goto fail; |
| 342 | } |
| 343 | |
| 344 | pool->users++; |
| 345 | shost->cmd_pool = pool; |
| 346 | up(&host_cmd_pool_mutex); |
| 347 | |
| 348 | /* |
| 349 | * Get one backup command for this host. |
| 350 | */ |
| 351 | cmd = kmem_cache_alloc(shost->cmd_pool->slab, |
| 352 | GFP_KERNEL | shost->cmd_pool->gfp_mask); |
| 353 | if (!cmd) |
| 354 | goto fail2; |
| 355 | list_add(&cmd->list, &shost->free_list); |
| 356 | return 0; |
| 357 | |
| 358 | fail2: |
| 359 | if (!--pool->users) |
| 360 | kmem_cache_destroy(pool->slab); |
| 361 | return -ENOMEM; |
| 362 | fail: |
| 363 | up(&host_cmd_pool_mutex); |
| 364 | return -ENOMEM; |
| 365 | |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Function: scsi_destroy_command_freelist() |
| 370 | * |
| 371 | * Purpose: Release the command freelist for a scsi host. |
| 372 | * |
| 373 | * Arguments: shost - host that's freelist is going to be destroyed |
| 374 | */ |
| 375 | void scsi_destroy_command_freelist(struct Scsi_Host *shost) |
| 376 | { |
| 377 | while (!list_empty(&shost->free_list)) { |
| 378 | struct scsi_cmnd *cmd; |
| 379 | |
| 380 | cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list); |
| 381 | list_del_init(&cmd->list); |
| 382 | kmem_cache_free(shost->cmd_pool->slab, cmd); |
| 383 | } |
| 384 | |
| 385 | down(&host_cmd_pool_mutex); |
| 386 | if (!--shost->cmd_pool->users) |
| 387 | kmem_cache_destroy(shost->cmd_pool->slab); |
| 388 | up(&host_cmd_pool_mutex); |
| 389 | } |
| 390 | |
| 391 | #ifdef CONFIG_SCSI_LOGGING |
| 392 | void scsi_log_send(struct scsi_cmnd *cmd) |
| 393 | { |
| 394 | unsigned int level; |
| 395 | struct scsi_device *sdev; |
| 396 | |
| 397 | /* |
| 398 | * If ML QUEUE log level is greater than or equal to: |
| 399 | * |
| 400 | * 1: nothing (match completion) |
| 401 | * |
| 402 | * 2: log opcode + command of all commands |
| 403 | * |
| 404 | * 3: same as 2 plus dump cmd address |
| 405 | * |
| 406 | * 4: same as 3 plus dump extra junk |
| 407 | */ |
| 408 | if (unlikely(scsi_logging_level)) { |
| 409 | level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT, |
| 410 | SCSI_LOG_MLQUEUE_BITS); |
| 411 | if (level > 1) { |
| 412 | sdev = cmd->device; |
| 413 | printk(KERN_INFO "scsi <%d:%d:%d:%d> send ", |
| 414 | sdev->host->host_no, sdev->channel, sdev->id, |
| 415 | sdev->lun); |
| 416 | if (level > 2) |
| 417 | printk("0x%p ", cmd); |
| 418 | /* |
| 419 | * spaces to match disposition and cmd->result |
| 420 | * output in scsi_log_completion. |
| 421 | */ |
| 422 | printk(" "); |
| 423 | scsi_print_command(cmd); |
| 424 | if (level > 3) { |
| 425 | printk(KERN_INFO "buffer = 0x%p, bufflen = %d," |
| 426 | " done = 0x%p, queuecommand 0x%p\n", |
| 427 | cmd->buffer, cmd->bufflen, |
| 428 | cmd->done, |
| 429 | sdev->host->hostt->queuecommand); |
| 430 | |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | void scsi_log_completion(struct scsi_cmnd *cmd, int disposition) |
| 437 | { |
| 438 | unsigned int level; |
| 439 | struct scsi_device *sdev; |
| 440 | |
| 441 | /* |
| 442 | * If ML COMPLETE log level is greater than or equal to: |
| 443 | * |
| 444 | * 1: log disposition, result, opcode + command, and conditionally |
| 445 | * sense data for failures or non SUCCESS dispositions. |
| 446 | * |
| 447 | * 2: same as 1 but for all command completions. |
| 448 | * |
| 449 | * 3: same as 2 plus dump cmd address |
| 450 | * |
| 451 | * 4: same as 3 plus dump extra junk |
| 452 | */ |
| 453 | if (unlikely(scsi_logging_level)) { |
| 454 | level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT, |
| 455 | SCSI_LOG_MLCOMPLETE_BITS); |
| 456 | if (((level > 0) && (cmd->result || disposition != SUCCESS)) || |
| 457 | (level > 1)) { |
| 458 | sdev = cmd->device; |
| 459 | printk(KERN_INFO "scsi <%d:%d:%d:%d> done ", |
| 460 | sdev->host->host_no, sdev->channel, sdev->id, |
| 461 | sdev->lun); |
| 462 | if (level > 2) |
| 463 | printk("0x%p ", cmd); |
| 464 | /* |
| 465 | * Dump truncated values, so we usually fit within |
| 466 | * 80 chars. |
| 467 | */ |
| 468 | switch (disposition) { |
| 469 | case SUCCESS: |
| 470 | printk("SUCCESS"); |
| 471 | break; |
| 472 | case NEEDS_RETRY: |
| 473 | printk("RETRY "); |
| 474 | break; |
| 475 | case ADD_TO_MLQUEUE: |
| 476 | printk("MLQUEUE"); |
| 477 | break; |
| 478 | case FAILED: |
| 479 | printk("FAILED "); |
| 480 | break; |
| 481 | case TIMEOUT_ERROR: |
| 482 | /* |
| 483 | * If called via scsi_times_out. |
| 484 | */ |
| 485 | printk("TIMEOUT"); |
| 486 | break; |
| 487 | default: |
| 488 | printk("UNKNOWN"); |
| 489 | } |
| 490 | printk(" %8x ", cmd->result); |
| 491 | scsi_print_command(cmd); |
| 492 | if (status_byte(cmd->result) & CHECK_CONDITION) { |
| 493 | /* |
| db9dff3 | 2005-04-03 14:53:59 -0500 | [diff] [blame] | 494 | * XXX The scsi_print_sense formatting/prefix |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | * doesn't match this function. |
| 496 | */ |
| 497 | scsi_print_sense("", cmd); |
| 498 | } |
| 499 | if (level > 3) { |
| 500 | printk(KERN_INFO "scsi host busy %d failed %d\n", |
| 501 | sdev->host->host_busy, |
| 502 | sdev->host->host_failed); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | #endif |
| 508 | |
| 509 | /* |
| 510 | * Assign a serial number and pid to the request for error recovery |
| 511 | * and debugging purposes. Protected by the Host_Lock of host. |
| 512 | */ |
| 513 | static inline void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 514 | { |
| 515 | cmd->serial_number = host->cmd_serial_number++; |
| 516 | if (cmd->serial_number == 0) |
| 517 | cmd->serial_number = host->cmd_serial_number++; |
| 518 | |
| 519 | cmd->pid = host->cmd_pid++; |
| 520 | if (cmd->pid == 0) |
| 521 | cmd->pid = host->cmd_pid++; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Function: scsi_dispatch_command |
| 526 | * |
| 527 | * Purpose: Dispatch a command to the low-level driver. |
| 528 | * |
| 529 | * Arguments: cmd - command block we are dispatching. |
| 530 | * |
| 531 | * Notes: |
| 532 | */ |
| 533 | int scsi_dispatch_cmd(struct scsi_cmnd *cmd) |
| 534 | { |
| 535 | struct Scsi_Host *host = cmd->device->host; |
| 536 | unsigned long flags = 0; |
| 537 | unsigned long timeout; |
| 538 | int rtn = 0; |
| 539 | |
| 540 | /* check if the device is still usable */ |
| 541 | if (unlikely(cmd->device->sdev_state == SDEV_DEL)) { |
| 542 | /* in SDEV_DEL we error all commands. DID_NO_CONNECT |
| 543 | * returns an immediate error upwards, and signals |
| 544 | * that the device is no longer present */ |
| 545 | cmd->result = DID_NO_CONNECT << 16; |
| 546 | atomic_inc(&cmd->device->iorequest_cnt); |
Mike Christie | 69b5289 | 2005-05-01 14:47:15 -0500 | [diff] [blame] | 547 | __scsi_done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | /* return 0 (because the command has been processed) */ |
| 549 | goto out; |
| 550 | } |
| 551 | |
| 552 | /* Check to see if the scsi lld put this device into state SDEV_BLOCK. */ |
| 553 | if (unlikely(cmd->device->sdev_state == SDEV_BLOCK)) { |
| 554 | /* |
| 555 | * in SDEV_BLOCK, the command is just put back on the device |
| 556 | * queue. The suspend state has already blocked the queue so |
| 557 | * future requests should not occur until the device |
| 558 | * transitions out of the suspend state. |
| 559 | */ |
| 560 | scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY); |
| 561 | |
| 562 | SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n")); |
| 563 | |
| 564 | /* |
| 565 | * NOTE: rtn is still zero here because we don't need the |
| 566 | * queue to be plugged on return (it's already stopped) |
| 567 | */ |
| 568 | goto out; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * If SCSI-2 or lower, store the LUN value in cmnd. |
| 573 | */ |
| 574 | if (cmd->device->scsi_level <= SCSI_2) { |
| 575 | cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) | |
| 576 | (cmd->device->lun << 5 & 0xe0); |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * We will wait MIN_RESET_DELAY clock ticks after the last reset so |
| 581 | * we can avoid the drive not being ready. |
| 582 | */ |
| 583 | timeout = host->last_reset + MIN_RESET_DELAY; |
| 584 | |
| 585 | if (host->resetting && time_before(jiffies, timeout)) { |
| 586 | int ticks_remaining = timeout - jiffies; |
| 587 | /* |
| 588 | * NOTE: This may be executed from within an interrupt |
| 589 | * handler! This is bad, but for now, it'll do. The irq |
| 590 | * level of the interrupt handler has been masked out by the |
| 591 | * platform dependent interrupt handling code already, so the |
| 592 | * sti() here will not cause another call to the SCSI host's |
| 593 | * interrupt handler (assuming there is one irq-level per |
| 594 | * host). |
| 595 | */ |
| 596 | while (--ticks_remaining >= 0) |
| 597 | mdelay(1 + 999 / HZ); |
| 598 | host->resetting = 0; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * AK: unlikely race here: for some reason the timer could |
| 603 | * expire before the serial number is set up below. |
| 604 | */ |
| 605 | scsi_add_timer(cmd, cmd->timeout_per_command, scsi_times_out); |
| 606 | |
| 607 | scsi_log_send(cmd); |
| 608 | |
| 609 | /* |
| 610 | * We will use a queued command if possible, otherwise we will |
| 611 | * emulate the queuing and calling of completion function ourselves. |
| 612 | */ |
| 613 | |
| 614 | cmd->state = SCSI_STATE_QUEUED; |
| 615 | cmd->owner = SCSI_OWNER_LOWLEVEL; |
| 616 | |
| 617 | atomic_inc(&cmd->device->iorequest_cnt); |
| 618 | |
| 619 | /* |
| 620 | * Before we queue this command, check if the command |
| 621 | * length exceeds what the host adapter can handle. |
| 622 | */ |
| 623 | if (CDB_SIZE(cmd) > cmd->device->host->max_cmd_len) { |
| 624 | SCSI_LOG_MLQUEUE(3, |
| 625 | printk("queuecommand : command too long.\n")); |
| 626 | cmd->result = (DID_ABORT << 16); |
| 627 | |
| 628 | scsi_done(cmd); |
| 629 | goto out; |
| 630 | } |
| 631 | |
| 632 | spin_lock_irqsave(host->host_lock, flags); |
| 633 | scsi_cmd_get_serial(host, cmd); |
| 634 | |
| 635 | if (unlikely(test_bit(SHOST_CANCEL, &host->shost_state))) { |
| 636 | cmd->result = (DID_NO_CONNECT << 16); |
| 637 | scsi_done(cmd); |
| 638 | } else { |
| 639 | rtn = host->hostt->queuecommand(cmd, scsi_done); |
| 640 | } |
| 641 | spin_unlock_irqrestore(host->host_lock, flags); |
| 642 | if (rtn) { |
Tejun Heo | d8c37e7 | 2005-05-14 00:46:08 +0900 | [diff] [blame] | 643 | if (scsi_delete_timer(cmd)) { |
| 644 | atomic_inc(&cmd->device->iodone_cnt); |
| 645 | scsi_queue_insert(cmd, |
| 646 | (rtn == SCSI_MLQUEUE_DEVICE_BUSY) ? |
| 647 | rtn : SCSI_MLQUEUE_HOST_BUSY); |
| 648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | SCSI_LOG_MLQUEUE(3, |
| 650 | printk("queuecommand : request rejected\n")); |
| 651 | } |
| 652 | |
| 653 | out: |
| 654 | SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n")); |
| 655 | return rtn; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Function: scsi_init_cmd_from_req |
| 660 | * |
| 661 | * Purpose: Queue a SCSI command |
| 662 | * Purpose: Initialize a struct scsi_cmnd from a struct scsi_request |
| 663 | * |
| 664 | * Arguments: cmd - command descriptor. |
| 665 | * sreq - Request from the queue. |
| 666 | * |
| 667 | * Lock status: None needed. |
| 668 | * |
| 669 | * Returns: Nothing. |
| 670 | * |
| 671 | * Notes: Mainly transfer data from the request structure to the |
| 672 | * command structure. The request structure is allocated |
| 673 | * using the normal memory allocator, and requests can pile |
| 674 | * up to more or less any depth. The command structure represents |
| 675 | * a consumable resource, as these are allocated into a pool |
| 676 | * when the SCSI subsystem initializes. The preallocation is |
| 677 | * required so that in low-memory situations a disk I/O request |
| 678 | * won't cause the memory manager to try and write out a page. |
| 679 | * The request structure is generally used by ioctls and character |
| 680 | * devices. |
| 681 | */ |
| 682 | void scsi_init_cmd_from_req(struct scsi_cmnd *cmd, struct scsi_request *sreq) |
| 683 | { |
| 684 | sreq->sr_command = cmd; |
| 685 | |
| 686 | cmd->owner = SCSI_OWNER_MIDLEVEL; |
| 687 | cmd->cmd_len = sreq->sr_cmd_len; |
| 688 | cmd->use_sg = sreq->sr_use_sg; |
| 689 | |
| 690 | cmd->request = sreq->sr_request; |
| 691 | memcpy(cmd->data_cmnd, sreq->sr_cmnd, sizeof(cmd->data_cmnd)); |
| 692 | cmd->serial_number = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | cmd->bufflen = sreq->sr_bufflen; |
| 694 | cmd->buffer = sreq->sr_buffer; |
| 695 | cmd->retries = 0; |
| 696 | cmd->allowed = sreq->sr_allowed; |
| 697 | cmd->done = sreq->sr_done; |
| 698 | cmd->timeout_per_command = sreq->sr_timeout_per_command; |
| 699 | cmd->sc_data_direction = sreq->sr_data_direction; |
| 700 | cmd->sglist_len = sreq->sr_sglist_len; |
| 701 | cmd->underflow = sreq->sr_underflow; |
| 702 | cmd->sc_request = sreq; |
| 703 | memcpy(cmd->cmnd, sreq->sr_cmnd, sizeof(sreq->sr_cmnd)); |
| 704 | |
| 705 | /* |
| 706 | * Zero the sense buffer. Some host adapters automatically request |
| 707 | * sense on error. 0 is not a valid sense code. |
| 708 | */ |
| 709 | memset(cmd->sense_buffer, 0, sizeof(sreq->sr_sense_buffer)); |
| 710 | cmd->request_buffer = sreq->sr_buffer; |
| 711 | cmd->request_bufflen = sreq->sr_bufflen; |
| 712 | cmd->old_use_sg = cmd->use_sg; |
| 713 | if (cmd->cmd_len == 0) |
| 714 | cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]); |
| 715 | cmd->old_cmd_len = cmd->cmd_len; |
| 716 | cmd->sc_old_data_direction = cmd->sc_data_direction; |
| 717 | cmd->old_underflow = cmd->underflow; |
| 718 | |
| 719 | /* |
| 720 | * Start the timer ticking. |
| 721 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | cmd->abort_reason = 0; |
| 723 | cmd->result = 0; |
| 724 | |
| 725 | SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_init_cmd_from_req()\n")); |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * Per-CPU I/O completion queue. |
| 730 | */ |
| 731 | static DEFINE_PER_CPU(struct list_head, scsi_done_q); |
| 732 | |
| 733 | /** |
| 734 | * scsi_done - Enqueue the finished SCSI command into the done queue. |
| 735 | * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives |
| 736 | * ownership back to SCSI Core -- i.e. the LLDD has finished with it. |
| 737 | * |
| 738 | * This function is the mid-level's (SCSI Core) interrupt routine, which |
| 739 | * regains ownership of the SCSI command (de facto) from a LLDD, and enqueues |
| 740 | * the command to the done queue for further processing. |
| 741 | * |
| 742 | * This is the producer of the done queue who enqueues at the tail. |
| 743 | * |
| 744 | * This function is interrupt context safe. |
| 745 | */ |
Adrian Bunk | 52c1da3 | 2005-06-23 22:05:33 -0700 | [diff] [blame^] | 746 | static void scsi_done(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
| 748 | /* |
| 749 | * We don't have to worry about this one timing out any more. |
| 750 | * If we are unable to remove the timer, then the command |
| 751 | * has already timed out. In which case, we have no choice but to |
| 752 | * let the timeout function run, as we have no idea where in fact |
| 753 | * that function could really be. It might be on another processor, |
| 754 | * etc, etc. |
| 755 | */ |
| 756 | if (!scsi_delete_timer(cmd)) |
| 757 | return; |
| 758 | __scsi_done(cmd); |
| 759 | } |
| 760 | |
| 761 | /* Private entry to scsi_done() to complete a command when the timer |
| 762 | * isn't running --- used by scsi_times_out */ |
| 763 | void __scsi_done(struct scsi_cmnd *cmd) |
| 764 | { |
| 765 | unsigned long flags; |
| 766 | |
| 767 | /* |
| 768 | * Set the serial numbers back to zero |
| 769 | */ |
| 770 | cmd->serial_number = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | cmd->state = SCSI_STATE_BHQUEUE; |
| 772 | cmd->owner = SCSI_OWNER_BH_HANDLER; |
| 773 | |
| 774 | atomic_inc(&cmd->device->iodone_cnt); |
| 775 | if (cmd->result) |
| 776 | atomic_inc(&cmd->device->ioerr_cnt); |
| 777 | |
| 778 | /* |
| 779 | * Next, enqueue the command into the done queue. |
| 780 | * It is a per-CPU queue, so we just disable local interrupts |
| 781 | * and need no spinlock. |
| 782 | */ |
| 783 | local_irq_save(flags); |
| 784 | list_add_tail(&cmd->eh_entry, &__get_cpu_var(scsi_done_q)); |
| 785 | raise_softirq_irqoff(SCSI_SOFTIRQ); |
| 786 | local_irq_restore(flags); |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * scsi_softirq - Perform post-interrupt processing of finished SCSI commands. |
| 791 | * |
| 792 | * This is the consumer of the done queue. |
| 793 | * |
| 794 | * This is called with all interrupts enabled. This should reduce |
| 795 | * interrupt latency, stack depth, and reentrancy of the low-level |
| 796 | * drivers. |
| 797 | */ |
| 798 | static void scsi_softirq(struct softirq_action *h) |
| 799 | { |
| 800 | int disposition; |
| 801 | LIST_HEAD(local_q); |
| 802 | |
| 803 | local_irq_disable(); |
| 804 | list_splice_init(&__get_cpu_var(scsi_done_q), &local_q); |
| 805 | local_irq_enable(); |
| 806 | |
| 807 | while (!list_empty(&local_q)) { |
| 808 | struct scsi_cmnd *cmd = list_entry(local_q.next, |
| 809 | struct scsi_cmnd, eh_entry); |
| 810 | list_del_init(&cmd->eh_entry); |
| 811 | |
| 812 | disposition = scsi_decide_disposition(cmd); |
| 813 | scsi_log_completion(cmd, disposition); |
| 814 | switch (disposition) { |
| 815 | case SUCCESS: |
| 816 | scsi_finish_command(cmd); |
| 817 | break; |
| 818 | case NEEDS_RETRY: |
| 819 | scsi_retry_command(cmd); |
| 820 | break; |
| 821 | case ADD_TO_MLQUEUE: |
| 822 | scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY); |
| 823 | break; |
| 824 | default: |
| 825 | if (!scsi_eh_scmd_add(cmd, 0)) |
| 826 | scsi_finish_command(cmd); |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Function: scsi_retry_command |
| 833 | * |
| 834 | * Purpose: Send a command back to the low level to be retried. |
| 835 | * |
| 836 | * Notes: This command is always executed in the context of the |
| 837 | * bottom half handler, or the error handler thread. Low |
| 838 | * level drivers should not become re-entrant as a result of |
| 839 | * this. |
| 840 | */ |
Adrian Bunk | 52c1da3 | 2005-06-23 22:05:33 -0700 | [diff] [blame^] | 841 | static int scsi_retry_command(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | { |
| 843 | /* |
| 844 | * Restore the SCSI command state. |
| 845 | */ |
| 846 | scsi_setup_cmd_retry(cmd); |
| 847 | |
| 848 | /* |
| 849 | * Zero the sense information from the last time we tried |
| 850 | * this command. |
| 851 | */ |
| 852 | memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer)); |
| 853 | |
| 854 | return scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY); |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Function: scsi_finish_command |
| 859 | * |
| 860 | * Purpose: Pass command off to upper layer for finishing of I/O |
| 861 | * request, waking processes that are waiting on results, |
| 862 | * etc. |
| 863 | */ |
| 864 | void scsi_finish_command(struct scsi_cmnd *cmd) |
| 865 | { |
| 866 | struct scsi_device *sdev = cmd->device; |
| 867 | struct Scsi_Host *shost = sdev->host; |
| 868 | struct scsi_request *sreq; |
| 869 | |
| 870 | scsi_device_unbusy(sdev); |
| 871 | |
| 872 | /* |
| 873 | * Clear the flags which say that the device/host is no longer |
| 874 | * capable of accepting new commands. These are set in scsi_queue.c |
| 875 | * for both the queue full condition on a device, and for a |
| 876 | * host full condition on the host. |
| 877 | * |
| 878 | * XXX(hch): What about locking? |
| 879 | */ |
| 880 | shost->host_blocked = 0; |
| 881 | sdev->device_blocked = 0; |
| 882 | |
| 883 | /* |
| 884 | * If we have valid sense information, then some kind of recovery |
| 885 | * must have taken place. Make a note of this. |
| 886 | */ |
| 887 | if (SCSI_SENSE_VALID(cmd)) |
| 888 | cmd->result |= (DRIVER_SENSE << 24); |
| 889 | |
| 890 | SCSI_LOG_MLCOMPLETE(4, printk("Notifying upper driver of completion " |
| 891 | "for device %d %x\n", sdev->id, cmd->result)); |
| 892 | |
| 893 | cmd->owner = SCSI_OWNER_HIGHLEVEL; |
| 894 | cmd->state = SCSI_STATE_FINISHED; |
| 895 | |
| 896 | /* |
| 897 | * We can get here with use_sg=0, causing a panic in the upper level |
| 898 | */ |
| 899 | cmd->use_sg = cmd->old_use_sg; |
| 900 | |
| 901 | /* |
| 902 | * If there is an associated request structure, copy the data over |
| 903 | * before we call the completion function. |
| 904 | */ |
| 905 | sreq = cmd->sc_request; |
| 906 | if (sreq) { |
| 907 | sreq->sr_result = sreq->sr_command->result; |
| 908 | if (sreq->sr_result) { |
| 909 | memcpy(sreq->sr_sense_buffer, |
| 910 | sreq->sr_command->sense_buffer, |
| 911 | sizeof(sreq->sr_sense_buffer)); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | cmd->done(cmd); |
| 916 | } |
| 917 | EXPORT_SYMBOL(scsi_finish_command); |
| 918 | |
| 919 | /* |
| 920 | * Function: scsi_adjust_queue_depth() |
| 921 | * |
| 922 | * Purpose: Allow low level drivers to tell us to change the queue depth |
| 923 | * on a specific SCSI device |
| 924 | * |
| 925 | * Arguments: sdev - SCSI Device in question |
| 926 | * tagged - Do we use tagged queueing (non-0) or do we treat |
| 927 | * this device as an untagged device (0) |
| 928 | * tags - Number of tags allowed if tagged queueing enabled, |
| 929 | * or number of commands the low level driver can |
| 930 | * queue up in non-tagged mode (as per cmd_per_lun). |
| 931 | * |
| 932 | * Returns: Nothing |
| 933 | * |
| 934 | * Lock Status: None held on entry |
| 935 | * |
| 936 | * Notes: Low level drivers may call this at any time and we will do |
| 937 | * the right thing depending on whether or not the device is |
| 938 | * currently active and whether or not it even has the |
| 939 | * command blocks built yet. |
| 940 | */ |
| 941 | void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags) |
| 942 | { |
| 943 | unsigned long flags; |
| 944 | |
| 945 | /* |
| 946 | * refuse to set tagged depth to an unworkable size |
| 947 | */ |
| 948 | if (tags <= 0) |
| 949 | return; |
| 950 | |
| 951 | spin_lock_irqsave(sdev->request_queue->queue_lock, flags); |
| 952 | |
| 953 | /* Check to see if the queue is managed by the block layer |
| 954 | * if it is, and we fail to adjust the depth, exit */ |
| 955 | if (blk_queue_tagged(sdev->request_queue) && |
| 956 | blk_queue_resize_tags(sdev->request_queue, tags) != 0) |
| 957 | goto out; |
| 958 | |
| 959 | sdev->queue_depth = tags; |
| 960 | switch (tagged) { |
| 961 | case MSG_ORDERED_TAG: |
| 962 | sdev->ordered_tags = 1; |
| 963 | sdev->simple_tags = 1; |
| 964 | break; |
| 965 | case MSG_SIMPLE_TAG: |
| 966 | sdev->ordered_tags = 0; |
| 967 | sdev->simple_tags = 1; |
| 968 | break; |
| 969 | default: |
| 970 | printk(KERN_WARNING "(scsi%d:%d:%d:%d) " |
| 971 | "scsi_adjust_queue_depth, bad queue type, " |
| 972 | "disabled\n", sdev->host->host_no, |
| 973 | sdev->channel, sdev->id, sdev->lun); |
| 974 | case 0: |
| 975 | sdev->ordered_tags = sdev->simple_tags = 0; |
| 976 | sdev->queue_depth = tags; |
| 977 | break; |
| 978 | } |
| 979 | out: |
| 980 | spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags); |
| 981 | } |
| 982 | EXPORT_SYMBOL(scsi_adjust_queue_depth); |
| 983 | |
| 984 | /* |
| 985 | * Function: scsi_track_queue_full() |
| 986 | * |
| 987 | * Purpose: This function will track successive QUEUE_FULL events on a |
| 988 | * specific SCSI device to determine if and when there is a |
| 989 | * need to adjust the queue depth on the device. |
| 990 | * |
| 991 | * Arguments: sdev - SCSI Device in question |
| 992 | * depth - Current number of outstanding SCSI commands on |
| 993 | * this device, not counting the one returned as |
| 994 | * QUEUE_FULL. |
| 995 | * |
| 996 | * Returns: 0 - No change needed |
| 997 | * >0 - Adjust queue depth to this new depth |
| 998 | * -1 - Drop back to untagged operation using host->cmd_per_lun |
| 999 | * as the untagged command depth |
| 1000 | * |
| 1001 | * Lock Status: None held on entry |
| 1002 | * |
| 1003 | * Notes: Low level drivers may call this at any time and we will do |
| 1004 | * "The Right Thing." We are interrupt context safe. |
| 1005 | */ |
| 1006 | int scsi_track_queue_full(struct scsi_device *sdev, int depth) |
| 1007 | { |
| 1008 | if ((jiffies >> 4) == sdev->last_queue_full_time) |
| 1009 | return 0; |
| 1010 | |
| 1011 | sdev->last_queue_full_time = (jiffies >> 4); |
| 1012 | if (sdev->last_queue_full_depth != depth) { |
| 1013 | sdev->last_queue_full_count = 1; |
| 1014 | sdev->last_queue_full_depth = depth; |
| 1015 | } else { |
| 1016 | sdev->last_queue_full_count++; |
| 1017 | } |
| 1018 | |
| 1019 | if (sdev->last_queue_full_count <= 10) |
| 1020 | return 0; |
| 1021 | if (sdev->last_queue_full_depth < 8) { |
| 1022 | /* Drop back to untagged */ |
| 1023 | scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | |
| 1027 | if (sdev->ordered_tags) |
| 1028 | scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth); |
| 1029 | else |
| 1030 | scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth); |
| 1031 | return depth; |
| 1032 | } |
| 1033 | EXPORT_SYMBOL(scsi_track_queue_full); |
| 1034 | |
| 1035 | /** |
| 1036 | * scsi_device_get - get an addition reference to a scsi_device |
| 1037 | * @sdev: device to get a reference to |
| 1038 | * |
| 1039 | * Gets a reference to the scsi_device and increments the use count |
| 1040 | * of the underlying LLDD module. You must hold host_lock of the |
| 1041 | * parent Scsi_Host or already have a reference when calling this. |
| 1042 | */ |
| 1043 | int scsi_device_get(struct scsi_device *sdev) |
| 1044 | { |
| 1045 | if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL) |
| 1046 | return -ENXIO; |
| 1047 | if (!get_device(&sdev->sdev_gendev)) |
| 1048 | return -ENXIO; |
| 1049 | if (!try_module_get(sdev->host->hostt->module)) { |
| 1050 | put_device(&sdev->sdev_gendev); |
| 1051 | return -ENXIO; |
| 1052 | } |
| 1053 | return 0; |
| 1054 | } |
| 1055 | EXPORT_SYMBOL(scsi_device_get); |
| 1056 | |
| 1057 | /** |
| 1058 | * scsi_device_put - release a reference to a scsi_device |
| 1059 | * @sdev: device to release a reference on. |
| 1060 | * |
| 1061 | * Release a reference to the scsi_device and decrements the use count |
| 1062 | * of the underlying LLDD module. The device is freed once the last |
| 1063 | * user vanishes. |
| 1064 | */ |
| 1065 | void scsi_device_put(struct scsi_device *sdev) |
| 1066 | { |
| 1067 | module_put(sdev->host->hostt->module); |
| 1068 | put_device(&sdev->sdev_gendev); |
| 1069 | } |
| 1070 | EXPORT_SYMBOL(scsi_device_put); |
| 1071 | |
| 1072 | /* helper for shost_for_each_device, thus not documented */ |
| 1073 | struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost, |
| 1074 | struct scsi_device *prev) |
| 1075 | { |
| 1076 | struct list_head *list = (prev ? &prev->siblings : &shost->__devices); |
| 1077 | struct scsi_device *next = NULL; |
| 1078 | unsigned long flags; |
| 1079 | |
| 1080 | spin_lock_irqsave(shost->host_lock, flags); |
| 1081 | while (list->next != &shost->__devices) { |
| 1082 | next = list_entry(list->next, struct scsi_device, siblings); |
| 1083 | /* skip devices that we can't get a reference to */ |
| 1084 | if (!scsi_device_get(next)) |
| 1085 | break; |
| 1086 | next = NULL; |
| 1087 | list = list->next; |
| 1088 | } |
| 1089 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1090 | |
| 1091 | if (prev) |
| 1092 | scsi_device_put(prev); |
| 1093 | return next; |
| 1094 | } |
| 1095 | EXPORT_SYMBOL(__scsi_iterate_devices); |
| 1096 | |
| 1097 | /** |
| 1098 | * starget_for_each_device - helper to walk all devices of a target |
| 1099 | * @starget: target whose devices we want to iterate over. |
| 1100 | * |
| 1101 | * This traverses over each devices of @shost. The devices have |
| 1102 | * a reference that must be released by scsi_host_put when breaking |
| 1103 | * out of the loop. |
| 1104 | */ |
| 1105 | void starget_for_each_device(struct scsi_target *starget, void * data, |
| 1106 | void (*fn)(struct scsi_device *, void *)) |
| 1107 | { |
| 1108 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 1109 | struct scsi_device *sdev; |
| 1110 | |
| 1111 | shost_for_each_device(sdev, shost) { |
| 1112 | if ((sdev->channel == starget->channel) && |
| 1113 | (sdev->id == starget->id)) |
| 1114 | fn(sdev, data); |
| 1115 | } |
| 1116 | } |
| 1117 | EXPORT_SYMBOL(starget_for_each_device); |
| 1118 | |
| 1119 | /** |
| 1120 | * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED) |
| 1121 | * @starget: SCSI target pointer |
| 1122 | * @lun: SCSI Logical Unit Number |
| 1123 | * |
| 1124 | * Looks up the scsi_device with the specified @lun for a give |
| 1125 | * @starget. The returned scsi_device does not have an additional |
| 1126 | * reference. You must hold the host's host_lock over this call and |
| 1127 | * any access to the returned scsi_device. |
| 1128 | * |
| 1129 | * Note: The only reason why drivers would want to use this is because |
| 1130 | * they're need to access the device list in irq context. Otherwise you |
| 1131 | * really want to use scsi_device_lookup_by_target instead. |
| 1132 | **/ |
| 1133 | struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget, |
| 1134 | uint lun) |
| 1135 | { |
| 1136 | struct scsi_device *sdev; |
| 1137 | |
| 1138 | list_for_each_entry(sdev, &starget->devices, same_target_siblings) { |
| 1139 | if (sdev->lun ==lun) |
| 1140 | return sdev; |
| 1141 | } |
| 1142 | |
| 1143 | return NULL; |
| 1144 | } |
| 1145 | EXPORT_SYMBOL(__scsi_device_lookup_by_target); |
| 1146 | |
| 1147 | /** |
| 1148 | * scsi_device_lookup_by_target - find a device given the target |
| 1149 | * @starget: SCSI target pointer |
| 1150 | * @lun: SCSI Logical Unit Number |
| 1151 | * |
| 1152 | * Looks up the scsi_device with the specified @channel, @id, @lun for a |
| 1153 | * give host. The returned scsi_device has an additional reference that |
| 1154 | * needs to be release with scsi_host_put once you're done with it. |
| 1155 | **/ |
| 1156 | struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget, |
| 1157 | uint lun) |
| 1158 | { |
| 1159 | struct scsi_device *sdev; |
| 1160 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 1161 | unsigned long flags; |
| 1162 | |
| 1163 | spin_lock_irqsave(shost->host_lock, flags); |
| 1164 | sdev = __scsi_device_lookup_by_target(starget, lun); |
| 1165 | if (sdev && scsi_device_get(sdev)) |
| 1166 | sdev = NULL; |
| 1167 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1168 | |
| 1169 | return sdev; |
| 1170 | } |
| 1171 | EXPORT_SYMBOL(scsi_device_lookup_by_target); |
| 1172 | |
| 1173 | /** |
| 1174 | * scsi_device_lookup - find a device given the host (UNLOCKED) |
| 1175 | * @shost: SCSI host pointer |
| 1176 | * @channel: SCSI channel (zero if only one channel) |
| 1177 | * @pun: SCSI target number (physical unit number) |
| 1178 | * @lun: SCSI Logical Unit Number |
| 1179 | * |
| 1180 | * Looks up the scsi_device with the specified @channel, @id, @lun for a |
| 1181 | * give host. The returned scsi_device does not have an additional reference. |
| 1182 | * You must hold the host's host_lock over this call and any access to the |
| 1183 | * returned scsi_device. |
| 1184 | * |
| 1185 | * Note: The only reason why drivers would want to use this is because |
| 1186 | * they're need to access the device list in irq context. Otherwise you |
| 1187 | * really want to use scsi_device_lookup instead. |
| 1188 | **/ |
| 1189 | struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost, |
| 1190 | uint channel, uint id, uint lun) |
| 1191 | { |
| 1192 | struct scsi_device *sdev; |
| 1193 | |
| 1194 | list_for_each_entry(sdev, &shost->__devices, siblings) { |
| 1195 | if (sdev->channel == channel && sdev->id == id && |
| 1196 | sdev->lun ==lun) |
| 1197 | return sdev; |
| 1198 | } |
| 1199 | |
| 1200 | return NULL; |
| 1201 | } |
| 1202 | EXPORT_SYMBOL(__scsi_device_lookup); |
| 1203 | |
| 1204 | /** |
| 1205 | * scsi_device_lookup - find a device given the host |
| 1206 | * @shost: SCSI host pointer |
| 1207 | * @channel: SCSI channel (zero if only one channel) |
| 1208 | * @id: SCSI target number (physical unit number) |
| 1209 | * @lun: SCSI Logical Unit Number |
| 1210 | * |
| 1211 | * Looks up the scsi_device with the specified @channel, @id, @lun for a |
| 1212 | * give host. The returned scsi_device has an additional reference that |
| 1213 | * needs to be release with scsi_host_put once you're done with it. |
| 1214 | **/ |
| 1215 | struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost, |
| 1216 | uint channel, uint id, uint lun) |
| 1217 | { |
| 1218 | struct scsi_device *sdev; |
| 1219 | unsigned long flags; |
| 1220 | |
| 1221 | spin_lock_irqsave(shost->host_lock, flags); |
| 1222 | sdev = __scsi_device_lookup(shost, channel, id, lun); |
| 1223 | if (sdev && scsi_device_get(sdev)) |
| 1224 | sdev = NULL; |
| 1225 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1226 | |
| 1227 | return sdev; |
| 1228 | } |
| 1229 | EXPORT_SYMBOL(scsi_device_lookup); |
| 1230 | |
| 1231 | /** |
| 1232 | * scsi_device_cancel - cancel outstanding IO to this device |
| 1233 | * @sdev: Pointer to struct scsi_device |
| 1234 | * @recovery: Boolean instructing function to recover device or not. |
| 1235 | * |
| 1236 | **/ |
| 1237 | int scsi_device_cancel(struct scsi_device *sdev, int recovery) |
| 1238 | { |
| 1239 | struct scsi_cmnd *scmd; |
| 1240 | LIST_HEAD(active_list); |
| 1241 | struct list_head *lh, *lh_sf; |
| 1242 | unsigned long flags; |
| 1243 | |
| 1244 | scsi_device_set_state(sdev, SDEV_CANCEL); |
| 1245 | |
| 1246 | spin_lock_irqsave(&sdev->list_lock, flags); |
| 1247 | list_for_each_entry(scmd, &sdev->cmd_list, list) { |
| 1248 | if (scmd->request && scmd->request->rq_status != RQ_INACTIVE) { |
| 1249 | /* |
| 1250 | * If we are unable to remove the timer, it means |
| 1251 | * that the command has already timed out or |
| 1252 | * finished. |
| 1253 | */ |
| 1254 | if (!scsi_delete_timer(scmd)) |
| 1255 | continue; |
| 1256 | list_add_tail(&scmd->eh_entry, &active_list); |
| 1257 | } |
| 1258 | } |
| 1259 | spin_unlock_irqrestore(&sdev->list_lock, flags); |
| 1260 | |
| 1261 | if (!list_empty(&active_list)) { |
| 1262 | list_for_each_safe(lh, lh_sf, &active_list) { |
| 1263 | scmd = list_entry(lh, struct scsi_cmnd, eh_entry); |
| 1264 | list_del_init(lh); |
| 1265 | if (recovery) { |
| 1266 | scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD); |
| 1267 | } else { |
| 1268 | scmd->result = (DID_ABORT << 16); |
| 1269 | scsi_finish_command(scmd); |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | EXPORT_SYMBOL(scsi_device_cancel); |
| 1277 | |
| 1278 | #ifdef CONFIG_HOTPLUG_CPU |
| 1279 | static int scsi_cpu_notify(struct notifier_block *self, |
| 1280 | unsigned long action, void *hcpu) |
| 1281 | { |
| 1282 | int cpu = (unsigned long)hcpu; |
| 1283 | |
| 1284 | switch(action) { |
| 1285 | case CPU_DEAD: |
| 1286 | /* Drain scsi_done_q. */ |
| 1287 | local_irq_disable(); |
| 1288 | list_splice_init(&per_cpu(scsi_done_q, cpu), |
| 1289 | &__get_cpu_var(scsi_done_q)); |
| 1290 | raise_softirq_irqoff(SCSI_SOFTIRQ); |
| 1291 | local_irq_enable(); |
| 1292 | break; |
| 1293 | default: |
| 1294 | break; |
| 1295 | } |
| 1296 | return NOTIFY_OK; |
| 1297 | } |
| 1298 | |
| 1299 | static struct notifier_block __devinitdata scsi_cpu_nb = { |
| 1300 | .notifier_call = scsi_cpu_notify, |
| 1301 | }; |
| 1302 | |
| 1303 | #define register_scsi_cpu() register_cpu_notifier(&scsi_cpu_nb) |
| 1304 | #define unregister_scsi_cpu() unregister_cpu_notifier(&scsi_cpu_nb) |
| 1305 | #else |
| 1306 | #define register_scsi_cpu() |
| 1307 | #define unregister_scsi_cpu() |
| 1308 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 1309 | |
| 1310 | MODULE_DESCRIPTION("SCSI core"); |
| 1311 | MODULE_LICENSE("GPL"); |
| 1312 | |
| 1313 | module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR); |
| 1314 | MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels"); |
| 1315 | |
| 1316 | static int __init init_scsi(void) |
| 1317 | { |
| 1318 | int error, i; |
| 1319 | |
| 1320 | error = scsi_init_queue(); |
| 1321 | if (error) |
| 1322 | return error; |
| 1323 | error = scsi_init_procfs(); |
| 1324 | if (error) |
| 1325 | goto cleanup_queue; |
| 1326 | error = scsi_init_devinfo(); |
| 1327 | if (error) |
| 1328 | goto cleanup_procfs; |
| 1329 | error = scsi_init_hosts(); |
| 1330 | if (error) |
| 1331 | goto cleanup_devlist; |
| 1332 | error = scsi_init_sysctl(); |
| 1333 | if (error) |
| 1334 | goto cleanup_hosts; |
| 1335 | error = scsi_sysfs_register(); |
| 1336 | if (error) |
| 1337 | goto cleanup_sysctl; |
| 1338 | |
| 1339 | for (i = 0; i < NR_CPUS; i++) |
| 1340 | INIT_LIST_HEAD(&per_cpu(scsi_done_q, i)); |
| 1341 | |
| 1342 | devfs_mk_dir("scsi"); |
| 1343 | open_softirq(SCSI_SOFTIRQ, scsi_softirq, NULL); |
| 1344 | register_scsi_cpu(); |
| 1345 | printk(KERN_NOTICE "SCSI subsystem initialized\n"); |
| 1346 | return 0; |
| 1347 | |
| 1348 | cleanup_sysctl: |
| 1349 | scsi_exit_sysctl(); |
| 1350 | cleanup_hosts: |
| 1351 | scsi_exit_hosts(); |
| 1352 | cleanup_devlist: |
| 1353 | scsi_exit_devinfo(); |
| 1354 | cleanup_procfs: |
| 1355 | scsi_exit_procfs(); |
| 1356 | cleanup_queue: |
| 1357 | scsi_exit_queue(); |
| 1358 | printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n", |
| 1359 | -error); |
| 1360 | return error; |
| 1361 | } |
| 1362 | |
| 1363 | static void __exit exit_scsi(void) |
| 1364 | { |
| 1365 | scsi_sysfs_unregister(); |
| 1366 | scsi_exit_sysctl(); |
| 1367 | scsi_exit_hosts(); |
| 1368 | scsi_exit_devinfo(); |
| 1369 | devfs_remove("scsi"); |
| 1370 | scsi_exit_procfs(); |
| 1371 | scsi_exit_queue(); |
| 1372 | unregister_scsi_cpu(); |
| 1373 | } |
| 1374 | |
| 1375 | subsys_initcall(init_scsi); |
| 1376 | module_exit(exit_scsi); |