scsi: only maintain target_blocked if the driver has a target queue limit
[deliverable/linux.git] / drivers / scsi / scsi_lib.c
CommitLineData
1da177e4
LT
1/*
2 * scsi_lib.c Copyright (C) 1999 Eric Youngdale
3 *
4 * SCSI queueing library.
5 * Initial versions: Eric Youngdale (eric@andante.org).
6 * Based upon conversations with large numbers
7 * of people at Linux Expo.
8 */
9
10#include <linux/bio.h>
d3f46f39 11#include <linux/bitops.h>
1da177e4
LT
12#include <linux/blkdev.h>
13#include <linux/completion.h>
14#include <linux/kernel.h>
09703660 15#include <linux/export.h>
1da177e4
LT
16#include <linux/mempool.h>
17#include <linux/slab.h>
18#include <linux/init.h>
19#include <linux/pci.h>
20#include <linux/delay.h>
faead26d 21#include <linux/hardirq.h>
c6132da1 22#include <linux/scatterlist.h>
1da177e4
LT
23
24#include <scsi/scsi.h>
beb40487 25#include <scsi/scsi_cmnd.h>
1da177e4
LT
26#include <scsi/scsi_dbg.h>
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_driver.h>
29#include <scsi/scsi_eh.h>
30#include <scsi/scsi_host.h>
1da177e4 31
3b5382c4
CH
32#include <trace/events/scsi.h>
33
1da177e4
LT
34#include "scsi_priv.h"
35#include "scsi_logging.h"
36
37
6391a113 38#define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools)
5972511b 39#define SG_MEMPOOL_SIZE 2
1da177e4
LT
40
41struct scsi_host_sg_pool {
42 size_t size;
a8474ce2 43 char *name;
e18b890b 44 struct kmem_cache *slab;
1da177e4
LT
45 mempool_t *pool;
46};
47
d3f46f39
JB
48#define SP(x) { x, "sgpool-" __stringify(x) }
49#if (SCSI_MAX_SG_SEGMENTS < 32)
50#error SCSI_MAX_SG_SEGMENTS is too small (must be 32 or greater)
51#endif
52c1da39 52static struct scsi_host_sg_pool scsi_sg_pools[] = {
1da177e4
LT
53 SP(8),
54 SP(16),
fd820f40 55#if (SCSI_MAX_SG_SEGMENTS > 32)
d3f46f39 56 SP(32),
fd820f40 57#if (SCSI_MAX_SG_SEGMENTS > 64)
d3f46f39
JB
58 SP(64),
59#if (SCSI_MAX_SG_SEGMENTS > 128)
1da177e4 60 SP(128),
d3f46f39
JB
61#if (SCSI_MAX_SG_SEGMENTS > 256)
62#error SCSI_MAX_SG_SEGMENTS is too large (256 MAX)
fd820f40
FT
63#endif
64#endif
65#endif
d3f46f39
JB
66#endif
67 SP(SCSI_MAX_SG_SEGMENTS)
a8474ce2 68};
1da177e4
LT
69#undef SP
70
7027ad72 71struct kmem_cache *scsi_sdb_cache;
6f9a35e2 72
a488e749
JA
73/*
74 * When to reinvoke queueing after a resource shortage. It's 3 msecs to
75 * not change behaviour from the previous unplug mechanism, experimentation
76 * may prove this needs changing.
77 */
78#define SCSI_QUEUE_DELAY 3
79
de3e8bf3
CH
80static void
81scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
1da177e4
LT
82{
83 struct Scsi_Host *host = cmd->device->host;
84 struct scsi_device *device = cmd->device;
f0c0a376 85 struct scsi_target *starget = scsi_target(device);
1da177e4
LT
86
87 /*
d8c37e7b 88 * Set the appropriate busy bit for the device/host.
1da177e4
LT
89 *
90 * If the host/device isn't busy, assume that something actually
91 * completed, and that we should be able to queue a command now.
92 *
93 * Note that the prior mid-layer assumption that any host could
94 * always queue at least one command is now broken. The mid-layer
95 * will implement a user specifiable stall (see
96 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
97 * if a command is requeued with no other commands outstanding
98 * either for the device or for the host.
99 */
f0c0a376
MC
100 switch (reason) {
101 case SCSI_MLQUEUE_HOST_BUSY:
cd9070c9 102 atomic_set(&host->host_blocked, host->max_host_blocked);
f0c0a376
MC
103 break;
104 case SCSI_MLQUEUE_DEVICE_BUSY:
573e5913 105 case SCSI_MLQUEUE_EH_RETRY:
cd9070c9
CH
106 atomic_set(&device->device_blocked,
107 device->max_device_blocked);
f0c0a376
MC
108 break;
109 case SCSI_MLQUEUE_TARGET_BUSY:
cd9070c9
CH
110 atomic_set(&starget->target_blocked,
111 starget->max_target_blocked);
f0c0a376
MC
112 break;
113 }
de3e8bf3
CH
114}
115
116/**
117 * __scsi_queue_insert - private queue insertion
118 * @cmd: The SCSI command being requeued
119 * @reason: The reason for the requeue
120 * @unbusy: Whether the queue should be unbusied
121 *
122 * This is a private queue insertion. The public interface
123 * scsi_queue_insert() always assumes the queue should be unbusied
124 * because it's always called before the completion. This function is
125 * for a requeue after completion, which should only occur in this
126 * file.
127 */
128static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
129{
130 struct scsi_device *device = cmd->device;
131 struct request_queue *q = device->request_queue;
132 unsigned long flags;
133
134 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
135 "Inserting command %p into mlqueue\n", cmd));
136
137 scsi_set_blocked(cmd, reason);
1da177e4 138
1da177e4
LT
139 /*
140 * Decrement the counters, since these commands are no longer
141 * active on the host/device.
142 */
4f5299ac
JB
143 if (unbusy)
144 scsi_device_unbusy(device);
1da177e4
LT
145
146 /*
a1bf9d1d 147 * Requeue this command. It will go before all other commands
b485462a
BVA
148 * that are already in the queue. Schedule requeue work under
149 * lock such that the kblockd_schedule_work() call happens
150 * before blk_cleanup_queue() finishes.
a488e749 151 */
644373a4 152 cmd->result = 0;
a1bf9d1d 153 spin_lock_irqsave(q->queue_lock, flags);
59897dad 154 blk_requeue_request(q, cmd->request);
59c3d45e 155 kblockd_schedule_work(&device->requeue_work);
b485462a 156 spin_unlock_irqrestore(q->queue_lock, flags);
1da177e4
LT
157}
158
4f5299ac
JB
159/*
160 * Function: scsi_queue_insert()
161 *
162 * Purpose: Insert a command in the midlevel queue.
163 *
164 * Arguments: cmd - command that we are adding to queue.
165 * reason - why we are inserting command to queue.
166 *
167 * Lock status: Assumed that lock is not held upon entry.
168 *
169 * Returns: Nothing.
170 *
171 * Notes: We do this for one of two cases. Either the host is busy
172 * and it cannot accept any more commands for the time being,
173 * or the device returned QUEUE_FULL and can accept no more
174 * commands.
175 * Notes: This could be called either from an interrupt context or a
176 * normal process context.
177 */
84feb166 178void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
4f5299ac 179{
84feb166 180 __scsi_queue_insert(cmd, reason, 1);
4f5299ac 181}
39216033 182/**
33aa687d 183 * scsi_execute - insert request and wait for the result
39216033
JB
184 * @sdev: scsi device
185 * @cmd: scsi command
186 * @data_direction: data direction
187 * @buffer: data buffer
188 * @bufflen: len of buffer
189 * @sense: optional sense buffer
190 * @timeout: request timeout in seconds
191 * @retries: number of times to retry request
33aa687d 192 * @flags: or into request flags;
f4f4e47e 193 * @resid: optional residual length
39216033 194 *
59c51591 195 * returns the req->errors value which is the scsi_cmnd result
ea73a9f2 196 * field.
eb44820c 197 */
33aa687d
JB
198int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
199 int data_direction, void *buffer, unsigned bufflen,
2bfad21e 200 unsigned char *sense, int timeout, int retries, u64 flags,
f4f4e47e 201 int *resid)
39216033
JB
202{
203 struct request *req;
204 int write = (data_direction == DMA_TO_DEVICE);
205 int ret = DRIVER_ERROR << 24;
206
207 req = blk_get_request(sdev->request_queue, write, __GFP_WAIT);
bfe159a5
JB
208 if (!req)
209 return ret;
f27b087b 210 blk_rq_set_block_pc(req);
39216033
JB
211
212 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
213 buffer, bufflen, __GFP_WAIT))
214 goto out;
215
216 req->cmd_len = COMMAND_SIZE(cmd[0]);
217 memcpy(req->cmd, cmd, req->cmd_len);
218 req->sense = sense;
219 req->sense_len = 0;
17e01f21 220 req->retries = retries;
39216033 221 req->timeout = timeout;
4aff5e23 222 req->cmd_flags |= flags | REQ_QUIET | REQ_PREEMPT;
39216033
JB
223
224 /*
225 * head injection *required* here otherwise quiesce won't work
226 */
227 blk_execute_rq(req->q, NULL, req, 1);
228
bdb2b8ca
AS
229 /*
230 * Some devices (USB mass-storage in particular) may transfer
231 * garbage data together with a residue indicating that the data
232 * is invalid. Prevent the garbage from being misinterpreted
233 * and prevent security leaks by zeroing out the excess data.
234 */
c3a4d78c
TH
235 if (unlikely(req->resid_len > 0 && req->resid_len <= bufflen))
236 memset(buffer + (bufflen - req->resid_len), 0, req->resid_len);
bdb2b8ca 237
f4f4e47e 238 if (resid)
c3a4d78c 239 *resid = req->resid_len;
39216033
JB
240 ret = req->errors;
241 out:
242 blk_put_request(req);
243
244 return ret;
245}
33aa687d 246EXPORT_SYMBOL(scsi_execute);
39216033 247
9b21493c 248int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd,
ea73a9f2 249 int data_direction, void *buffer, unsigned bufflen,
f4f4e47e 250 struct scsi_sense_hdr *sshdr, int timeout, int retries,
2bfad21e 251 int *resid, u64 flags)
ea73a9f2
JB
252{
253 char *sense = NULL;
1ccb48bb 254 int result;
255
ea73a9f2 256 if (sshdr) {
24669f75 257 sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
ea73a9f2
JB
258 if (!sense)
259 return DRIVER_ERROR << 24;
ea73a9f2 260 }
1ccb48bb 261 result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
9b21493c 262 sense, timeout, retries, flags, resid);
ea73a9f2 263 if (sshdr)
e514385b 264 scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
ea73a9f2
JB
265
266 kfree(sense);
267 return result;
268}
9b21493c 269EXPORT_SYMBOL(scsi_execute_req_flags);
ea73a9f2 270
1da177e4
LT
271/*
272 * Function: scsi_init_cmd_errh()
273 *
274 * Purpose: Initialize cmd fields related to error handling.
275 *
276 * Arguments: cmd - command that is ready to be queued.
277 *
1da177e4
LT
278 * Notes: This function has the job of initializing a number of
279 * fields related to error handling. Typically this will
280 * be called once for each command, as required.
281 */
631c228c 282static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
1da177e4 283{
1da177e4 284 cmd->serial_number = 0;
30b0c37b 285 scsi_set_resid(cmd, 0);
b80ca4f7 286 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1da177e4 287 if (cmd->cmd_len == 0)
db4742dd 288 cmd->cmd_len = scsi_command_size(cmd->cmnd);
1da177e4
LT
289}
290
291void scsi_device_unbusy(struct scsi_device *sdev)
292{
293 struct Scsi_Host *shost = sdev->host;
f0c0a376 294 struct scsi_target *starget = scsi_target(sdev);
1da177e4
LT
295 unsigned long flags;
296
74665016 297 atomic_dec(&shost->host_busy);
2ccbb008
CH
298 if (starget->can_queue > 0)
299 atomic_dec(&starget->target_busy);
74665016 300
939647ee 301 if (unlikely(scsi_host_in_recovery(shost) &&
74665016
CH
302 (shost->host_failed || shost->host_eh_scheduled))) {
303 spin_lock_irqsave(shost->host_lock, flags);
1da177e4 304 scsi_eh_wakeup(shost);
74665016
CH
305 spin_unlock_irqrestore(shost->host_lock, flags);
306 }
307
71e75c97 308 atomic_dec(&sdev->device_busy);
1da177e4
LT
309}
310
311/*
312 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
313 * and call blk_run_queue for all the scsi_devices on the target -
314 * including current_sdev first.
315 *
316 * Called with *no* scsi locks held.
317 */
318static void scsi_single_lun_run(struct scsi_device *current_sdev)
319{
320 struct Scsi_Host *shost = current_sdev->host;
321 struct scsi_device *sdev, *tmp;
322 struct scsi_target *starget = scsi_target(current_sdev);
323 unsigned long flags;
324
325 spin_lock_irqsave(shost->host_lock, flags);
326 starget->starget_sdev_user = NULL;
327 spin_unlock_irqrestore(shost->host_lock, flags);
328
329 /*
330 * Call blk_run_queue for all LUNs on the target, starting with
331 * current_sdev. We race with others (to set starget_sdev_user),
332 * but in most cases, we will be first. Ideally, each LU on the
333 * target would get some limited time or requests on the target.
334 */
335 blk_run_queue(current_sdev->request_queue);
336
337 spin_lock_irqsave(shost->host_lock, flags);
338 if (starget->starget_sdev_user)
339 goto out;
340 list_for_each_entry_safe(sdev, tmp, &starget->devices,
341 same_target_siblings) {
342 if (sdev == current_sdev)
343 continue;
344 if (scsi_device_get(sdev))
345 continue;
346
347 spin_unlock_irqrestore(shost->host_lock, flags);
348 blk_run_queue(sdev->request_queue);
349 spin_lock_irqsave(shost->host_lock, flags);
350
351 scsi_device_put(sdev);
352 }
353 out:
354 spin_unlock_irqrestore(shost->host_lock, flags);
355}
356
cd9070c9 357static inline bool scsi_device_is_busy(struct scsi_device *sdev)
9d112517 358{
cd9070c9
CH
359 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
360 return true;
361 if (atomic_read(&sdev->device_blocked) > 0)
362 return true;
363 return false;
9d112517
KU
364}
365
cd9070c9 366static inline bool scsi_target_is_busy(struct scsi_target *starget)
f0c0a376 367{
2ccbb008
CH
368 if (starget->can_queue > 0) {
369 if (atomic_read(&starget->target_busy) >= starget->can_queue)
370 return true;
371 if (atomic_read(&starget->target_blocked) > 0)
372 return true;
373 }
cd9070c9 374 return false;
f0c0a376
MC
375}
376
cd9070c9 377static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
9d112517 378{
cd9070c9
CH
379 if (shost->can_queue > 0 &&
380 atomic_read(&shost->host_busy) >= shost->can_queue)
381 return true;
382 if (atomic_read(&shost->host_blocked) > 0)
383 return true;
384 if (shost->host_self_blocked)
385 return true;
386 return false;
9d112517
KU
387}
388
21a05df5 389static void scsi_starved_list_run(struct Scsi_Host *shost)
1da177e4 390{
2a3a59e5 391 LIST_HEAD(starved_list);
21a05df5 392 struct scsi_device *sdev;
1da177e4
LT
393 unsigned long flags;
394
1da177e4 395 spin_lock_irqsave(shost->host_lock, flags);
2a3a59e5
MC
396 list_splice_init(&shost->starved_list, &starved_list);
397
398 while (!list_empty(&starved_list)) {
e2eb7244
JB
399 struct request_queue *slq;
400
1da177e4
LT
401 /*
402 * As long as shost is accepting commands and we have
403 * starved queues, call blk_run_queue. scsi_request_fn
404 * drops the queue_lock and can add us back to the
405 * starved_list.
406 *
407 * host_lock protects the starved_list and starved_entry.
408 * scsi_request_fn must get the host_lock before checking
409 * or modifying starved_list or starved_entry.
410 */
2a3a59e5 411 if (scsi_host_is_busy(shost))
f0c0a376 412 break;
f0c0a376 413
2a3a59e5
MC
414 sdev = list_entry(starved_list.next,
415 struct scsi_device, starved_entry);
416 list_del_init(&sdev->starved_entry);
f0c0a376
MC
417 if (scsi_target_is_busy(scsi_target(sdev))) {
418 list_move_tail(&sdev->starved_entry,
419 &shost->starved_list);
420 continue;
421 }
422
e2eb7244
JB
423 /*
424 * Once we drop the host lock, a racing scsi_remove_device()
425 * call may remove the sdev from the starved list and destroy
426 * it and the queue. Mitigate by taking a reference to the
427 * queue and never touching the sdev again after we drop the
428 * host lock. Note: if __scsi_remove_device() invokes
429 * blk_cleanup_queue() before the queue is run from this
430 * function then blk_run_queue() will return immediately since
431 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
432 */
433 slq = sdev->request_queue;
434 if (!blk_get_queue(slq))
435 continue;
436 spin_unlock_irqrestore(shost->host_lock, flags);
437
438 blk_run_queue(slq);
439 blk_put_queue(slq);
440
441 spin_lock_irqsave(shost->host_lock, flags);
1da177e4 442 }
2a3a59e5
MC
443 /* put any unprocessed entries back */
444 list_splice(&starved_list, &shost->starved_list);
1da177e4 445 spin_unlock_irqrestore(shost->host_lock, flags);
21a05df5
CH
446}
447
448/*
449 * Function: scsi_run_queue()
450 *
451 * Purpose: Select a proper request queue to serve next
452 *
453 * Arguments: q - last request's queue
454 *
455 * Returns: Nothing
456 *
457 * Notes: The previous command was completely finished, start
458 * a new one if possible.
459 */
460static void scsi_run_queue(struct request_queue *q)
461{
462 struct scsi_device *sdev = q->queuedata;
463
464 if (scsi_target(sdev)->single_lun)
465 scsi_single_lun_run(sdev);
466 if (!list_empty(&sdev->host->starved_list))
467 scsi_starved_list_run(sdev->host);
1da177e4
LT
468
469 blk_run_queue(q);
470}
471
9937a5e2
JA
472void scsi_requeue_run_queue(struct work_struct *work)
473{
474 struct scsi_device *sdev;
475 struct request_queue *q;
476
477 sdev = container_of(work, struct scsi_device, requeue_work);
478 q = sdev->request_queue;
479 scsi_run_queue(q);
480}
481
1da177e4
LT
482/*
483 * Function: scsi_requeue_command()
484 *
485 * Purpose: Handle post-processing of completed commands.
486 *
487 * Arguments: q - queue to operate on
488 * cmd - command that may need to be requeued.
489 *
490 * Returns: Nothing
491 *
492 * Notes: After command completion, there may be blocks left
493 * over which weren't finished by the previous command
494 * this can be for a number of reasons - the main one is
495 * I/O errors in the middle of the request, in which case
496 * we need to request the blocks that come after the bad
497 * sector.
e91442b6 498 * Notes: Upon return, cmd is a stale pointer.
1da177e4
LT
499 */
500static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
501{
940f5d47 502 struct scsi_device *sdev = cmd->device;
e91442b6 503 struct request *req = cmd->request;
283369cc
TH
504 unsigned long flags;
505
283369cc 506 spin_lock_irqsave(q->queue_lock, flags);
134997a0
CH
507 blk_unprep_request(req);
508 req->special = NULL;
509 scsi_put_command(cmd);
e91442b6 510 blk_requeue_request(q, req);
283369cc 511 spin_unlock_irqrestore(q->queue_lock, flags);
1da177e4
LT
512
513 scsi_run_queue(q);
940f5d47
BVA
514
515 put_device(&sdev->sdev_gendev);
1da177e4
LT
516}
517
518void scsi_next_command(struct scsi_cmnd *cmd)
519{
49d7bc64
LT
520 struct scsi_device *sdev = cmd->device;
521 struct request_queue *q = sdev->request_queue;
522
1da177e4
LT
523 scsi_put_command(cmd);
524 scsi_run_queue(q);
49d7bc64 525
49d7bc64 526 put_device(&sdev->sdev_gendev);
1da177e4
LT
527}
528
529void scsi_run_host_queues(struct Scsi_Host *shost)
530{
531 struct scsi_device *sdev;
532
533 shost_for_each_device(sdev, shost)
534 scsi_run_queue(sdev->request_queue);
535}
536
a8474ce2
JA
537static inline unsigned int scsi_sgtable_index(unsigned short nents)
538{
539 unsigned int index;
540
d3f46f39
JB
541 BUG_ON(nents > SCSI_MAX_SG_SEGMENTS);
542
543 if (nents <= 8)
a8474ce2 544 index = 0;
d3f46f39
JB
545 else
546 index = get_count_order(nents) - 3;
1da177e4 547
a8474ce2
JA
548 return index;
549}
550
5ed7959e 551static void scsi_sg_free(struct scatterlist *sgl, unsigned int nents)
a8474ce2
JA
552{
553 struct scsi_host_sg_pool *sgp;
a8474ce2 554
5ed7959e
JA
555 sgp = scsi_sg_pools + scsi_sgtable_index(nents);
556 mempool_free(sgl, sgp->pool);
557}
a8474ce2 558
5ed7959e
JA
559static struct scatterlist *scsi_sg_alloc(unsigned int nents, gfp_t gfp_mask)
560{
561 struct scsi_host_sg_pool *sgp;
a8474ce2 562
5ed7959e
JA
563 sgp = scsi_sg_pools + scsi_sgtable_index(nents);
564 return mempool_alloc(sgp->pool, gfp_mask);
565}
a3bec5c5 566
30b0c37b
BH
567static int scsi_alloc_sgtable(struct scsi_data_buffer *sdb, int nents,
568 gfp_t gfp_mask)
5ed7959e
JA
569{
570 int ret;
a8474ce2 571
30b0c37b 572 BUG_ON(!nents);
a8474ce2 573
30b0c37b
BH
574 ret = __sg_alloc_table(&sdb->table, nents, SCSI_MAX_SG_SEGMENTS,
575 gfp_mask, scsi_sg_alloc);
5ed7959e 576 if (unlikely(ret))
30b0c37b 577 __sg_free_table(&sdb->table, SCSI_MAX_SG_SEGMENTS,
7cedb1f1 578 scsi_sg_free);
45711f1a 579
a8474ce2 580 return ret;
1da177e4
LT
581}
582
30b0c37b 583static void scsi_free_sgtable(struct scsi_data_buffer *sdb)
1da177e4 584{
30b0c37b 585 __sg_free_table(&sdb->table, SCSI_MAX_SG_SEGMENTS, scsi_sg_free);
1da177e4
LT
586}
587
588/*
589 * Function: scsi_release_buffers()
590 *
c682adf3 591 * Purpose: Free resources allocate for a scsi_command.
1da177e4
LT
592 *
593 * Arguments: cmd - command that we are bailing.
594 *
595 * Lock status: Assumed that no lock is held upon entry.
596 *
597 * Returns: Nothing
598 *
599 * Notes: In the event that an upper level driver rejects a
600 * command, we must release resources allocated during
601 * the __init_io() function. Primarily this would involve
c682adf3 602 * the scatter-gather table.
1da177e4 603 */
f1bea55d 604static void scsi_release_buffers(struct scsi_cmnd *cmd)
1da177e4 605{
c682adf3
CH
606 if (cmd->sdb.table.nents)
607 scsi_free_sgtable(&cmd->sdb);
608
609 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
610
611 if (scsi_prot_sg_count(cmd))
612 scsi_free_sgtable(cmd->prot_sdb);
1da177e4
LT
613}
614
c682adf3
CH
615static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
616{
617 struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
618
619 scsi_free_sgtable(bidi_sdb);
620 kmem_cache_free(scsi_sdb_cache, bidi_sdb);
621 cmd->request->next_rq->special = NULL;
622}
623
0f7f6234
HR
624/**
625 * __scsi_error_from_host_byte - translate SCSI error code into errno
626 * @cmd: SCSI command (unused)
627 * @result: scsi error code
628 *
629 * Translate SCSI error code into standard UNIX errno.
630 * Return values:
631 * -ENOLINK temporary transport failure
632 * -EREMOTEIO permanent target failure, do not retry
633 * -EBADE permanent nexus failure, retry on other path
a9d6ceb8 634 * -ENOSPC No write space available
7e782af5 635 * -ENODATA Medium error
0f7f6234
HR
636 * -EIO unspecified I/O error
637 */
63583cca
HR
638static int __scsi_error_from_host_byte(struct scsi_cmnd *cmd, int result)
639{
640 int error = 0;
641
642 switch(host_byte(result)) {
643 case DID_TRANSPORT_FAILFAST:
644 error = -ENOLINK;
645 break;
646 case DID_TARGET_FAILURE:
2082ebc4 647 set_host_byte(cmd, DID_OK);
63583cca
HR
648 error = -EREMOTEIO;
649 break;
650 case DID_NEXUS_FAILURE:
2082ebc4 651 set_host_byte(cmd, DID_OK);
63583cca
HR
652 error = -EBADE;
653 break;
a9d6ceb8
HR
654 case DID_ALLOC_FAILURE:
655 set_host_byte(cmd, DID_OK);
656 error = -ENOSPC;
657 break;
7e782af5
HR
658 case DID_MEDIUM_ERROR:
659 set_host_byte(cmd, DID_OK);
660 error = -ENODATA;
661 break;
63583cca
HR
662 default:
663 error = -EIO;
664 break;
665 }
666
667 return error;
668}
669
1da177e4
LT
670/*
671 * Function: scsi_io_completion()
672 *
673 * Purpose: Completion processing for block device I/O requests.
674 *
675 * Arguments: cmd - command that is finished.
676 *
677 * Lock status: Assumed that no lock is held upon entry.
678 *
679 * Returns: Nothing
680 *
bc85dc50
CH
681 * Notes: We will finish off the specified number of sectors. If we
682 * are done, the command block will be released and the queue
683 * function will be goosed. If we are not done then we have to
b60af5b0 684 * figure out what to do next:
1da177e4 685 *
b60af5b0
AS
686 * a) We can call scsi_requeue_command(). The request
687 * will be unprepared and put back on the queue. Then
688 * a new command will be created for it. This should
689 * be used if we made forward progress, or if we want
690 * to switch from READ(10) to READ(6) for example.
1da177e4 691 *
bc85dc50 692 * b) We can call __scsi_queue_insert(). The request will
b60af5b0
AS
693 * be put back on the queue and retried using the same
694 * command as before, possibly after a delay.
695 *
696 * c) We can call blk_end_request() with -EIO to fail
697 * the remainder of the request.
1da177e4 698 */
03aba2f7 699void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
1da177e4
LT
700{
701 int result = cmd->result;
165125e1 702 struct request_queue *q = cmd->device->request_queue;
1da177e4 703 struct request *req = cmd->request;
fa8e36c3 704 int error = 0;
1da177e4
LT
705 struct scsi_sense_hdr sshdr;
706 int sense_valid = 0;
707 int sense_deferred = 0;
b60af5b0
AS
708 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
709 ACTION_DELAYED_RETRY} action;
ee60b2c5 710 unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
1da177e4 711
1da177e4
LT
712 if (result) {
713 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
714 if (sense_valid)
715 sense_deferred = scsi_sense_is_deferred(&sshdr);
716 }
631c228c 717
33659ebb 718 if (req->cmd_type == REQ_TYPE_BLOCK_PC) { /* SG_IO ioctl from block level */
1da177e4 719 if (result) {
1da177e4
LT
720 if (sense_valid && req->sense) {
721 /*
722 * SG_IO wants current and deferred errors
723 */
724 int len = 8 + cmd->sense_buffer[7];
725
726 if (len > SCSI_SENSE_BUFFERSIZE)
727 len = SCSI_SENSE_BUFFERSIZE;
728 memcpy(req->sense, cmd->sense_buffer, len);
729 req->sense_len = len;
730 }
fa8e36c3 731 if (!sense_deferred)
63583cca 732 error = __scsi_error_from_host_byte(cmd, result);
b22f687d 733 }
27c41973
MS
734 /*
735 * __scsi_error_from_host_byte may have reset the host_byte
736 */
737 req->errors = cmd->result;
e6bb7a96
FT
738
739 req->resid_len = scsi_get_resid(cmd);
740
6f9a35e2 741 if (scsi_bidi_cmnd(cmd)) {
e6bb7a96
FT
742 /*
743 * Bidi commands Must be complete as a whole,
744 * both sides at once.
745 */
746 req->next_rq->resid_len = scsi_in(cmd)->resid;
747
63c43b0e 748 scsi_release_buffers(cmd);
c682adf3 749 scsi_release_bidi_buffers(cmd);
bc85dc50 750
e6bb7a96
FT
751 blk_end_request_all(req, 0);
752
e6bb7a96 753 scsi_next_command(cmd);
6f9a35e2
BH
754 return;
755 }
89fb4cd1
JB
756 } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
757 /*
758 * Certain non BLOCK_PC requests are commands that don't
759 * actually transfer anything (FLUSH), so cannot use
760 * good_bytes != blk_rq_bytes(req) as the signal for an error.
761 * This sets the error explicitly for the problem case.
762 */
763 error = __scsi_error_from_host_byte(cmd, result);
1da177e4
LT
764 }
765
33659ebb
CH
766 /* no bidi support for !REQ_TYPE_BLOCK_PC yet */
767 BUG_ON(blk_bidi_rq(req));
30b0c37b 768
1da177e4
LT
769 /*
770 * Next deal with any sectors which we were able to correctly
771 * handle.
772 */
91921e01
HR
773 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
774 "%u sectors total, %d bytes done.\n",
775 blk_rq_sectors(req), good_bytes));
d6b0c537 776
a9bddd74
JB
777 /*
778 * Recovered errors need reporting, but they're always treated
779 * as success, so fiddle the result code here. For BLOCK_PC
780 * we already took a copy of the original into rq->errors which
781 * is what gets returned to the user
782 */
e7efe593
DG
783 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
784 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
785 * print since caller wants ATA registers. Only occurs on
786 * SCSI ATA PASS_THROUGH commands when CK_COND=1
787 */
788 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
789 ;
790 else if (!(req->cmd_flags & REQ_QUIET))
a9bddd74
JB
791 scsi_print_sense("", cmd);
792 result = 0;
793 /* BLOCK_PC may have set error */
794 error = 0;
795 }
796
797 /*
bc85dc50 798 * If we finished all bytes in the request we are done now.
d6b0c537 799 */
bc85dc50
CH
800 if (!blk_end_request(req, error, good_bytes))
801 goto next_command;
802
803 /*
804 * Kill remainder if no retrys.
805 */
806 if (error && scsi_noretry_cmd(cmd)) {
807 blk_end_request_all(req, error);
808 goto next_command;
809 }
810
811 /*
812 * If there had been no error, but we have leftover bytes in the
813 * requeues just queue the command up again.
d6b0c537 814 */
bc85dc50
CH
815 if (result == 0)
816 goto requeue;
03aba2f7 817
63583cca 818 error = __scsi_error_from_host_byte(cmd, result);
3e695f89 819
b60af5b0
AS
820 if (host_byte(result) == DID_RESET) {
821 /* Third party bus reset or reset for error recovery
822 * reasons. Just retry the command and see what
823 * happens.
824 */
825 action = ACTION_RETRY;
826 } else if (sense_valid && !sense_deferred) {
1da177e4
LT
827 switch (sshdr.sense_key) {
828 case UNIT_ATTENTION:
829 if (cmd->device->removable) {
03aba2f7 830 /* Detected disc change. Set a bit
1da177e4
LT
831 * and quietly refuse further access.
832 */
833 cmd->device->changed = 1;
b60af5b0 834 action = ACTION_FAIL;
1da177e4 835 } else {
03aba2f7
LT
836 /* Must have been a power glitch, or a
837 * bus reset. Could not have been a
838 * media change, so we just retry the
b60af5b0 839 * command and see what happens.
03aba2f7 840 */
b60af5b0 841 action = ACTION_RETRY;
1da177e4
LT
842 }
843 break;
844 case ILLEGAL_REQUEST:
03aba2f7
LT
845 /* If we had an ILLEGAL REQUEST returned, then
846 * we may have performed an unsupported
847 * command. The only thing this should be
848 * would be a ten byte read where only a six
849 * byte read was supported. Also, on a system
850 * where READ CAPACITY failed, we may have
851 * read past the end of the disk.
852 */
26a68019
JA
853 if ((cmd->device->use_10_for_rw &&
854 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
1da177e4
LT
855 (cmd->cmnd[0] == READ_10 ||
856 cmd->cmnd[0] == WRITE_10)) {
b60af5b0 857 /* This will issue a new 6-byte command. */
1da177e4 858 cmd->device->use_10_for_rw = 0;
b60af5b0 859 action = ACTION_REPREP;
3e695f89 860 } else if (sshdr.asc == 0x10) /* DIX */ {
3e695f89
MP
861 action = ACTION_FAIL;
862 error = -EILSEQ;
c98a0eb0 863 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
5db44863 864 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
c98a0eb0 865 action = ACTION_FAIL;
66a651aa 866 error = -EREMOTEIO;
b60af5b0
AS
867 } else
868 action = ACTION_FAIL;
869 break;
511e44f4 870 case ABORTED_COMMAND:
126c0982 871 action = ACTION_FAIL;
e6c11dbb 872 if (sshdr.asc == 0x10) /* DIF */
3e695f89 873 error = -EILSEQ;
1da177e4
LT
874 break;
875 case NOT_READY:
03aba2f7 876 /* If the device is in the process of becoming
f3e93f73 877 * ready, or has a temporary blockage, retry.
1da177e4 878 */
f3e93f73
JB
879 if (sshdr.asc == 0x04) {
880 switch (sshdr.ascq) {
881 case 0x01: /* becoming ready */
882 case 0x04: /* format in progress */
883 case 0x05: /* rebuild in progress */
884 case 0x06: /* recalculation in progress */
885 case 0x07: /* operation in progress */
886 case 0x08: /* Long write in progress */
887 case 0x09: /* self test in progress */
d8705f11 888 case 0x14: /* space allocation in progress */
b60af5b0 889 action = ACTION_DELAYED_RETRY;
f3e93f73 890 break;
3dbf6a54 891 default:
3dbf6a54
AS
892 action = ACTION_FAIL;
893 break;
f3e93f73 894 }
e6c11dbb 895 } else
b60af5b0 896 action = ACTION_FAIL;
b60af5b0 897 break;
1da177e4 898 case VOLUME_OVERFLOW:
03aba2f7 899 /* See SSC3rXX or current. */
b60af5b0
AS
900 action = ACTION_FAIL;
901 break;
1da177e4 902 default:
b60af5b0 903 action = ACTION_FAIL;
1da177e4
LT
904 break;
905 }
e6c11dbb 906 } else
b60af5b0 907 action = ACTION_FAIL;
b60af5b0 908
ee60b2c5 909 if (action != ACTION_FAIL &&
e6c11dbb 910 time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
ee60b2c5 911 action = ACTION_FAIL;
ee60b2c5 912
b60af5b0
AS
913 switch (action) {
914 case ACTION_FAIL:
915 /* Give up and fail the remainder of the request */
4aff5e23 916 if (!(req->cmd_flags & REQ_QUIET)) {
a4d04a4c 917 scsi_print_result(cmd);
3173d8c3
JB
918 if (driver_byte(result) & DRIVER_SENSE)
919 scsi_print_sense("", cmd);
002b1eb2 920 scsi_print_command(cmd);
3173d8c3 921 }
bc85dc50
CH
922 if (!blk_end_request_err(req, error))
923 goto next_command;
924 /*FALLTHRU*/
b60af5b0 925 case ACTION_REPREP:
bc85dc50 926 requeue:
b60af5b0
AS
927 /* Unprep the request and put it back at the head of the queue.
928 * A new command will be prepared and issued.
929 */
79ed2429 930 scsi_release_buffers(cmd);
b60af5b0
AS
931 scsi_requeue_command(q, cmd);
932 break;
933 case ACTION_RETRY:
934 /* Retry the same command immediately */
4f5299ac 935 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
b60af5b0
AS
936 break;
937 case ACTION_DELAYED_RETRY:
938 /* Retry the same command after a delay */
4f5299ac 939 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
b60af5b0 940 break;
1da177e4 941 }
bc85dc50
CH
942 return;
943
944next_command:
945 scsi_release_buffers(cmd);
946 scsi_next_command(cmd);
1da177e4 947}
1da177e4 948
6f9a35e2
BH
949static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb,
950 gfp_t gfp_mask)
1da177e4 951{
6f9a35e2 952 int count;
1da177e4
LT
953
954 /*
3b003157 955 * If sg table allocation fails, requeue request later.
1da177e4 956 */
30b0c37b
BH
957 if (unlikely(scsi_alloc_sgtable(sdb, req->nr_phys_segments,
958 gfp_mask))) {
1da177e4 959 return BLKPREP_DEFER;
7c72ce81 960 }
1da177e4 961
1da177e4
LT
962 /*
963 * Next, walk the list, and fill in the addresses and sizes of
964 * each segment.
965 */
30b0c37b
BH
966 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
967 BUG_ON(count > sdb->table.nents);
968 sdb->table.nents = count;
1011c1b9 969 sdb->length = blk_rq_bytes(req);
4a03d90e 970 return BLKPREP_OK;
1da177e4 971}
6f9a35e2
BH
972
973/*
974 * Function: scsi_init_io()
975 *
976 * Purpose: SCSI I/O initialize function.
977 *
978 * Arguments: cmd - Command descriptor we wish to initialize
979 *
980 * Returns: 0 on success
981 * BLKPREP_DEFER if the failure is retryable
982 * BLKPREP_KILL if the failure is fatal
983 */
984int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
985{
5e012aad 986 struct scsi_device *sdev = cmd->device;
13f05c8d 987 struct request *rq = cmd->request;
635d98b1 988 int error;
13f05c8d 989
635d98b1
CH
990 BUG_ON(!rq->nr_phys_segments);
991
992 error = scsi_init_sgtable(rq, &cmd->sdb, gfp_mask);
6f9a35e2
BH
993 if (error)
994 goto err_exit;
995
13f05c8d 996 if (blk_bidi_rq(rq)) {
6f9a35e2 997 struct scsi_data_buffer *bidi_sdb = kmem_cache_zalloc(
6362abd3 998 scsi_sdb_cache, GFP_ATOMIC);
6f9a35e2
BH
999 if (!bidi_sdb) {
1000 error = BLKPREP_DEFER;
1001 goto err_exit;
1002 }
1003
13f05c8d
MP
1004 rq->next_rq->special = bidi_sdb;
1005 error = scsi_init_sgtable(rq->next_rq, bidi_sdb, GFP_ATOMIC);
6f9a35e2
BH
1006 if (error)
1007 goto err_exit;
1008 }
1009
13f05c8d 1010 if (blk_integrity_rq(rq)) {
7027ad72
MP
1011 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1012 int ivecs, count;
1013
1014 BUG_ON(prot_sdb == NULL);
13f05c8d 1015 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
7027ad72
MP
1016
1017 if (scsi_alloc_sgtable(prot_sdb, ivecs, gfp_mask)) {
1018 error = BLKPREP_DEFER;
1019 goto err_exit;
1020 }
1021
13f05c8d 1022 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
7027ad72
MP
1023 prot_sdb->table.sgl);
1024 BUG_ON(unlikely(count > ivecs));
13f05c8d 1025 BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
7027ad72
MP
1026
1027 cmd->prot_sdb = prot_sdb;
1028 cmd->prot_sdb->table.nents = count;
1029 }
1030
6f9a35e2
BH
1031 return BLKPREP_OK ;
1032
1033err_exit:
1034 scsi_release_buffers(cmd);
610a6349 1035 cmd->request->special = NULL;
3a5c19c2 1036 scsi_put_command(cmd);
5e012aad 1037 put_device(&sdev->sdev_gendev);
6f9a35e2
BH
1038 return error;
1039}
bb52d82f 1040EXPORT_SYMBOL(scsi_init_io);
1da177e4 1041
3b003157
CH
1042static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
1043 struct request *req)
1044{
1045 struct scsi_cmnd *cmd;
1046
1047 if (!req->special) {
04796336
CH
1048 /* Bail if we can't get a reference to the device */
1049 if (!get_device(&sdev->sdev_gendev))
1050 return NULL;
1051
3b003157 1052 cmd = scsi_get_command(sdev, GFP_ATOMIC);
04796336
CH
1053 if (unlikely(!cmd)) {
1054 put_device(&sdev->sdev_gendev);
3b003157 1055 return NULL;
04796336 1056 }
3b003157
CH
1057 req->special = cmd;
1058 } else {
1059 cmd = req->special;
1060 }
1061
1062 /* pull a tag out of the request if we have one */
1063 cmd->tag = req->tag;
1064 cmd->request = req;
1065
64a87b24 1066 cmd->cmnd = req->cmd;
72f7d322 1067 cmd->prot_op = SCSI_PROT_NORMAL;
64a87b24 1068
3b003157
CH
1069 return cmd;
1070}
1071
4f1e5765 1072static int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
7b16318d 1073{
a1b73fc1 1074 struct scsi_cmnd *cmd = req->special;
3b003157
CH
1075
1076 /*
1077 * BLOCK_PC requests may transfer data, in which case they must
1078 * a bio attached to them. Or they might contain a SCSI command
1079 * that does not transfer data, in which case they may optionally
1080 * submit a request without an attached bio.
1081 */
1082 if (req->bio) {
635d98b1 1083 int ret = scsi_init_io(cmd, GFP_ATOMIC);
3b003157
CH
1084 if (unlikely(ret))
1085 return ret;
1086 } else {
b0790410 1087 BUG_ON(blk_rq_bytes(req));
3b003157 1088
30b0c37b 1089 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
3b003157 1090 }
7b16318d 1091
7b16318d 1092 cmd->cmd_len = req->cmd_len;
b0790410 1093 cmd->transfersize = blk_rq_bytes(req);
7b16318d 1094 cmd->allowed = req->retries;
3b003157 1095 return BLKPREP_OK;
7b16318d 1096}
7b16318d 1097
3b003157 1098/*
3868cf8e
CH
1099 * Setup a REQ_TYPE_FS command. These are simple request from filesystems
1100 * that still need to be translated to SCSI CDBs from the ULD.
3b003157 1101 */
3868cf8e 1102static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
1da177e4 1103{
a1b73fc1 1104 struct scsi_cmnd *cmd = req->special;
a6a8d9f8
CS
1105
1106 if (unlikely(sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh
1107 && sdev->scsi_dh_data->scsi_dh->prep_fn)) {
a1b73fc1 1108 int ret = sdev->scsi_dh_data->scsi_dh->prep_fn(sdev, req);
a6a8d9f8
CS
1109 if (ret != BLKPREP_OK)
1110 return ret;
1111 }
1112
64a87b24 1113 memset(cmd->cmnd, 0, BLK_MAX_CDB);
3868cf8e 1114 return scsi_cmd_to_driver(cmd)->init_command(cmd);
3b003157
CH
1115}
1116
6af7a4ff
CH
1117static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
1118{
1119 struct scsi_cmnd *cmd = req->special;
1120
1121 if (!blk_rq_bytes(req))
1122 cmd->sc_data_direction = DMA_NONE;
1123 else if (rq_data_dir(req) == WRITE)
1124 cmd->sc_data_direction = DMA_TO_DEVICE;
1125 else
1126 cmd->sc_data_direction = DMA_FROM_DEVICE;
1127
1128 switch (req->cmd_type) {
1129 case REQ_TYPE_FS:
1130 return scsi_setup_fs_cmnd(sdev, req);
1131 case REQ_TYPE_BLOCK_PC:
1132 return scsi_setup_blk_pc_cmnd(sdev, req);
1133 default:
1134 return BLKPREP_KILL;
1135 }
1136}
1137
a1b73fc1
CH
1138static int
1139scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
3b003157 1140{
3b003157
CH
1141 int ret = BLKPREP_OK;
1142
1da177e4 1143 /*
3b003157
CH
1144 * If the device is not in running state we will reject some
1145 * or all commands.
1da177e4 1146 */
3b003157
CH
1147 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
1148 switch (sdev->sdev_state) {
1149 case SDEV_OFFLINE:
1b8d2620 1150 case SDEV_TRANSPORT_OFFLINE:
3b003157
CH
1151 /*
1152 * If the device is offline we refuse to process any
1153 * commands. The device must be brought online
1154 * before trying any recovery commands.
1155 */
1156 sdev_printk(KERN_ERR, sdev,
1157 "rejecting I/O to offline device\n");
1158 ret = BLKPREP_KILL;
1159 break;
1160 case SDEV_DEL:
1161 /*
1162 * If the device is fully deleted, we refuse to
1163 * process any commands as well.
1164 */
9ccfc756 1165 sdev_printk(KERN_ERR, sdev,
3b003157
CH
1166 "rejecting I/O to dead device\n");
1167 ret = BLKPREP_KILL;
1168 break;
1169 case SDEV_QUIESCE:
1170 case SDEV_BLOCK:
6f4267e3 1171 case SDEV_CREATED_BLOCK:
3b003157
CH
1172 /*
1173 * If the devices is blocked we defer normal commands.
1174 */
1175 if (!(req->cmd_flags & REQ_PREEMPT))
1176 ret = BLKPREP_DEFER;
1177 break;
1178 default:
1179 /*
1180 * For any other not fully online state we only allow
1181 * special commands. In particular any user initiated
1182 * command is not allowed.
1183 */
1184 if (!(req->cmd_flags & REQ_PREEMPT))
1185 ret = BLKPREP_KILL;
1186 break;
1da177e4 1187 }
1da177e4 1188 }
7f9a6bc4
JB
1189 return ret;
1190}
1da177e4 1191
a1b73fc1
CH
1192static int
1193scsi_prep_return(struct request_queue *q, struct request *req, int ret)
7f9a6bc4
JB
1194{
1195 struct scsi_device *sdev = q->queuedata;
1da177e4 1196
3b003157
CH
1197 switch (ret) {
1198 case BLKPREP_KILL:
1199 req->errors = DID_NO_CONNECT << 16;
7f9a6bc4
JB
1200 /* release the command and kill it */
1201 if (req->special) {
1202 struct scsi_cmnd *cmd = req->special;
1203 scsi_release_buffers(cmd);
1204 scsi_put_command(cmd);
68c03d91 1205 put_device(&sdev->sdev_gendev);
7f9a6bc4
JB
1206 req->special = NULL;
1207 }
3b003157
CH
1208 break;
1209 case BLKPREP_DEFER:
1da177e4 1210 /*
9934c8c0 1211 * If we defer, the blk_peek_request() returns NULL, but the
a488e749
JA
1212 * queue must be restarted, so we schedule a callback to happen
1213 * shortly.
1da177e4 1214 */
71e75c97 1215 if (atomic_read(&sdev->device_busy) == 0)
a488e749 1216 blk_delay_queue(q, SCSI_QUEUE_DELAY);
3b003157
CH
1217 break;
1218 default:
1219 req->cmd_flags |= REQ_DONTPREP;
1da177e4
LT
1220 }
1221
3b003157 1222 return ret;
1da177e4 1223}
7f9a6bc4 1224
a1b73fc1 1225static int scsi_prep_fn(struct request_queue *q, struct request *req)
7f9a6bc4
JB
1226{
1227 struct scsi_device *sdev = q->queuedata;
a1b73fc1
CH
1228 struct scsi_cmnd *cmd;
1229 int ret;
1230
1231 ret = scsi_prep_state_check(sdev, req);
1232 if (ret != BLKPREP_OK)
1233 goto out;
1234
1235 cmd = scsi_get_cmd_from_req(sdev, req);
1236 if (unlikely(!cmd)) {
1237 ret = BLKPREP_DEFER;
1238 goto out;
1239 }
7f9a6bc4 1240
6af7a4ff 1241 ret = scsi_setup_cmnd(sdev, req);
a1b73fc1 1242out:
7f9a6bc4
JB
1243 return scsi_prep_return(q, req, ret);
1244}
a1b73fc1
CH
1245
1246static void scsi_unprep_fn(struct request_queue *q, struct request *req)
1247{
1248 if (req->cmd_type == REQ_TYPE_FS) {
1249 struct scsi_cmnd *cmd = req->special;
1250 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
1251
1252 if (drv->uninit_command)
1253 drv->uninit_command(cmd);
1254 }
1255}
1da177e4
LT
1256
1257/*
1258 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1259 * return 0.
1260 *
1261 * Called with the queue_lock held.
1262 */
1263static inline int scsi_dev_queue_ready(struct request_queue *q,
1264 struct scsi_device *sdev)
1265{
71e75c97
CH
1266 unsigned int busy;
1267
1268 busy = atomic_inc_return(&sdev->device_busy) - 1;
cd9070c9 1269 if (atomic_read(&sdev->device_blocked)) {
71e75c97
CH
1270 if (busy)
1271 goto out_dec;
1272
1da177e4
LT
1273 /*
1274 * unblock after device_blocked iterates to zero
1275 */
cd9070c9 1276 if (atomic_dec_return(&sdev->device_blocked) > 0) {
a488e749 1277 blk_delay_queue(q, SCSI_QUEUE_DELAY);
71e75c97 1278 goto out_dec;
1da177e4 1279 }
71e75c97
CH
1280 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1281 "unblocking device at zero depth\n"));
1da177e4 1282 }
71e75c97
CH
1283
1284 if (busy >= sdev->queue_depth)
1285 goto out_dec;
1da177e4
LT
1286
1287 return 1;
71e75c97
CH
1288out_dec:
1289 atomic_dec(&sdev->device_busy);
1290 return 0;
1da177e4
LT
1291}
1292
f0c0a376
MC
1293/*
1294 * scsi_target_queue_ready: checks if there we can send commands to target
1295 * @sdev: scsi device on starget to check.
f0c0a376
MC
1296 */
1297static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1298 struct scsi_device *sdev)
1299{
1300 struct scsi_target *starget = scsi_target(sdev);
7ae65c0f 1301 unsigned int busy;
f0c0a376
MC
1302
1303 if (starget->single_lun) {
7ae65c0f 1304 spin_lock_irq(shost->host_lock);
f0c0a376 1305 if (starget->starget_sdev_user &&
7ae65c0f
CH
1306 starget->starget_sdev_user != sdev) {
1307 spin_unlock_irq(shost->host_lock);
1308 return 0;
1309 }
f0c0a376 1310 starget->starget_sdev_user = sdev;
7ae65c0f 1311 spin_unlock_irq(shost->host_lock);
f0c0a376
MC
1312 }
1313
2ccbb008
CH
1314 if (starget->can_queue <= 0)
1315 return 1;
1316
7ae65c0f 1317 busy = atomic_inc_return(&starget->target_busy) - 1;
cd9070c9 1318 if (atomic_read(&starget->target_blocked) > 0) {
7ae65c0f
CH
1319 if (busy)
1320 goto starved;
1321
f0c0a376
MC
1322 /*
1323 * unblock after target_blocked iterates to zero
1324 */
cd9070c9 1325 if (atomic_dec_return(&starget->target_blocked) > 0)
7ae65c0f 1326 goto out_dec;
cf68d334
CH
1327
1328 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1329 "unblocking target at zero depth\n"));
f0c0a376
MC
1330 }
1331
2ccbb008 1332 if (busy >= starget->can_queue)
7ae65c0f 1333 goto starved;
f0c0a376 1334
7ae65c0f
CH
1335 return 1;
1336
1337starved:
1338 spin_lock_irq(shost->host_lock);
1339 list_move_tail(&sdev->starved_entry, &shost->starved_list);
cf68d334 1340 spin_unlock_irq(shost->host_lock);
7ae65c0f 1341out_dec:
2ccbb008
CH
1342 if (starget->can_queue > 0)
1343 atomic_dec(&starget->target_busy);
7ae65c0f 1344 return 0;
f0c0a376
MC
1345}
1346
1da177e4
LT
1347/*
1348 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1349 * return 0. We must end up running the queue again whenever 0 is
1350 * returned, else IO can hang.
1da177e4
LT
1351 */
1352static inline int scsi_host_queue_ready(struct request_queue *q,
1353 struct Scsi_Host *shost,
1354 struct scsi_device *sdev)
1355{
74665016 1356 unsigned int busy;
cf68d334 1357
939647ee 1358 if (scsi_host_in_recovery(shost))
74665016
CH
1359 return 0;
1360
1361 busy = atomic_inc_return(&shost->host_busy) - 1;
cd9070c9 1362 if (atomic_read(&shost->host_blocked) > 0) {
74665016
CH
1363 if (busy)
1364 goto starved;
1365
1da177e4
LT
1366 /*
1367 * unblock after host_blocked iterates to zero
1368 */
cd9070c9 1369 if (atomic_dec_return(&shost->host_blocked) > 0)
74665016 1370 goto out_dec;
cf68d334
CH
1371
1372 SCSI_LOG_MLQUEUE(3,
1373 shost_printk(KERN_INFO, shost,
1374 "unblocking host at zero depth\n"));
1da177e4 1375 }
74665016
CH
1376
1377 if (shost->can_queue > 0 && busy >= shost->can_queue)
1378 goto starved;
1379 if (shost->host_self_blocked)
1380 goto starved;
1da177e4
LT
1381
1382 /* We're OK to process the command, so we can't be starved */
74665016
CH
1383 if (!list_empty(&sdev->starved_entry)) {
1384 spin_lock_irq(shost->host_lock);
1385 if (!list_empty(&sdev->starved_entry))
1386 list_del_init(&sdev->starved_entry);
1387 spin_unlock_irq(shost->host_lock);
1388 }
1da177e4 1389
74665016
CH
1390 return 1;
1391
1392starved:
1393 spin_lock_irq(shost->host_lock);
1394 if (list_empty(&sdev->starved_entry))
1395 list_add_tail(&sdev->starved_entry, &shost->starved_list);
cf68d334 1396 spin_unlock_irq(shost->host_lock);
74665016
CH
1397out_dec:
1398 atomic_dec(&shost->host_busy);
1399 return 0;
1da177e4
LT
1400}
1401
6c5121b7
KU
1402/*
1403 * Busy state exporting function for request stacking drivers.
1404 *
1405 * For efficiency, no lock is taken to check the busy state of
1406 * shost/starget/sdev, since the returned value is not guaranteed and
1407 * may be changed after request stacking drivers call the function,
1408 * regardless of taking lock or not.
1409 *
67bd9413
BVA
1410 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1411 * needs to return 'not busy'. Otherwise, request stacking drivers
1412 * may hold requests forever.
6c5121b7
KU
1413 */
1414static int scsi_lld_busy(struct request_queue *q)
1415{
1416 struct scsi_device *sdev = q->queuedata;
1417 struct Scsi_Host *shost;
6c5121b7 1418
3f3299d5 1419 if (blk_queue_dying(q))
6c5121b7
KU
1420 return 0;
1421
1422 shost = sdev->host;
6c5121b7 1423
b7e94a16
JN
1424 /*
1425 * Ignore host/starget busy state.
1426 * Since block layer does not have a concept of fairness across
1427 * multiple queues, congestion of host/starget needs to be handled
1428 * in SCSI layer.
1429 */
1430 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
6c5121b7
KU
1431 return 1;
1432
1433 return 0;
1434}
1435
1da177e4 1436/*
e91442b6 1437 * Kill a request for a dead device
1da177e4 1438 */
165125e1 1439static void scsi_kill_request(struct request *req, struct request_queue *q)
1da177e4 1440{
e91442b6 1441 struct scsi_cmnd *cmd = req->special;
03b14708
JS
1442 struct scsi_device *sdev;
1443 struct scsi_target *starget;
1444 struct Scsi_Host *shost;
1da177e4 1445
9934c8c0 1446 blk_start_request(req);
788ce43a 1447
74571813
HR
1448 scmd_printk(KERN_INFO, cmd, "killing request\n");
1449
03b14708
JS
1450 sdev = cmd->device;
1451 starget = scsi_target(sdev);
1452 shost = sdev->host;
e91442b6
JB
1453 scsi_init_cmd_errh(cmd);
1454 cmd->result = DID_NO_CONNECT << 16;
1455 atomic_inc(&cmd->device->iorequest_cnt);
e36e0c80
TH
1456
1457 /*
1458 * SCSI request completion path will do scsi_device_unbusy(),
1459 * bump busy counts. To bump the counters, we need to dance
1460 * with the locks as normal issue path does.
1461 */
71e75c97 1462 atomic_inc(&sdev->device_busy);
74665016 1463 atomic_inc(&shost->host_busy);
2ccbb008
CH
1464 if (starget->can_queue > 0)
1465 atomic_inc(&starget->target_busy);
e36e0c80 1466
242f9dcb 1467 blk_complete_request(req);
1da177e4
LT
1468}
1469
1aea6434
JA
1470static void scsi_softirq_done(struct request *rq)
1471{
242f9dcb
JA
1472 struct scsi_cmnd *cmd = rq->special;
1473 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
1aea6434
JA
1474 int disposition;
1475
1476 INIT_LIST_HEAD(&cmd->eh_entry);
1477
242f9dcb
JA
1478 atomic_inc(&cmd->device->iodone_cnt);
1479 if (cmd->result)
1480 atomic_inc(&cmd->device->ioerr_cnt);
1481
1aea6434
JA
1482 disposition = scsi_decide_disposition(cmd);
1483 if (disposition != SUCCESS &&
1484 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1485 sdev_printk(KERN_ERR, cmd->device,
1486 "timing out command, waited %lus\n",
1487 wait_for/HZ);
1488 disposition = SUCCESS;
1489 }
91921e01 1490
1aea6434
JA
1491 scsi_log_completion(cmd, disposition);
1492
1493 switch (disposition) {
1494 case SUCCESS:
1495 scsi_finish_command(cmd);
1496 break;
1497 case NEEDS_RETRY:
596f482a 1498 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
1aea6434
JA
1499 break;
1500 case ADD_TO_MLQUEUE:
1501 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1502 break;
1503 default:
1504 if (!scsi_eh_scmd_add(cmd, 0))
1505 scsi_finish_command(cmd);
1506 }
1507}
1508
3b5382c4
CH
1509/**
1510 * scsi_done - Invoke completion on finished SCSI command.
1511 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
1512 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
1513 *
1514 * Description: This function is the mid-level's (SCSI Core) interrupt routine,
1515 * which regains ownership of the SCSI command (de facto) from a LLDD, and
1516 * calls blk_complete_request() for further processing.
1517 *
1518 * This function is interrupt context safe.
1519 */
1520static void scsi_done(struct scsi_cmnd *cmd)
1521{
1522 trace_scsi_dispatch_cmd_done(cmd);
1523 blk_complete_request(cmd->request);
1524}
1525
1da177e4
LT
1526/*
1527 * Function: scsi_request_fn()
1528 *
1529 * Purpose: Main strategy routine for SCSI.
1530 *
1531 * Arguments: q - Pointer to actual queue.
1532 *
1533 * Returns: Nothing
1534 *
1535 * Lock status: IO request lock assumed to be held when called.
1536 */
1537static void scsi_request_fn(struct request_queue *q)
613be1f6
BVA
1538 __releases(q->queue_lock)
1539 __acquires(q->queue_lock)
1da177e4
LT
1540{
1541 struct scsi_device *sdev = q->queuedata;
1542 struct Scsi_Host *shost;
1543 struct scsi_cmnd *cmd;
1544 struct request *req;
1545
1da177e4
LT
1546 /*
1547 * To start with, we keep looping until the queue is empty, or until
1548 * the host is no longer able to accept any more requests.
1549 */
1550 shost = sdev->host;
a488e749 1551 for (;;) {
1da177e4
LT
1552 int rtn;
1553 /*
1554 * get next queueable request. We do this early to make sure
91921e01 1555 * that the request is fully prepared even if we cannot
1da177e4
LT
1556 * accept it.
1557 */
9934c8c0 1558 req = blk_peek_request(q);
71e75c97 1559 if (!req)
1da177e4
LT
1560 break;
1561
1562 if (unlikely(!scsi_device_online(sdev))) {
9ccfc756
JB
1563 sdev_printk(KERN_ERR, sdev,
1564 "rejecting I/O to offline device\n");
e91442b6 1565 scsi_kill_request(req, q);
1da177e4
LT
1566 continue;
1567 }
1568
71e75c97
CH
1569 if (!scsi_dev_queue_ready(q, sdev))
1570 break;
1da177e4
LT
1571
1572 /*
1573 * Remove the request from the request list.
1574 */
1575 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
9934c8c0 1576 blk_start_request(req);
1da177e4 1577
cf68d334 1578 spin_unlock_irq(q->queue_lock);
e91442b6
JB
1579 cmd = req->special;
1580 if (unlikely(cmd == NULL)) {
1581 printk(KERN_CRIT "impossible request in %s.\n"
1582 "please mail a stack trace to "
4aff5e23 1583 "linux-scsi@vger.kernel.org\n",
cadbd4a5 1584 __func__);
4aff5e23 1585 blk_dump_rq_flags(req, "foo");
e91442b6
JB
1586 BUG();
1587 }
1da177e4 1588
ecefe8a9
MC
1589 /*
1590 * We hit this when the driver is using a host wide
1591 * tag map. For device level tag maps the queue_depth check
1592 * in the device ready fn would prevent us from trying
1593 * to allocate a tag. Since the map is a shared host resource
1594 * we add the dev to the starved list so it eventually gets
1595 * a run when a tag is freed.
1596 */
6bd522f6 1597 if (blk_queue_tagged(q) && !blk_rq_tagged(req)) {
cf68d334 1598 spin_lock_irq(shost->host_lock);
ecefe8a9
MC
1599 if (list_empty(&sdev->starved_entry))
1600 list_add_tail(&sdev->starved_entry,
1601 &shost->starved_list);
cf68d334 1602 spin_unlock_irq(shost->host_lock);
ecefe8a9
MC
1603 goto not_ready;
1604 }
1605
f0c0a376
MC
1606 if (!scsi_target_queue_ready(shost, sdev))
1607 goto not_ready;
1608
1da177e4 1609 if (!scsi_host_queue_ready(q, shost, sdev))
cf68d334 1610 goto host_not_ready;
1da177e4 1611
1da177e4
LT
1612 /*
1613 * Finally, initialize any error handling parameters, and set up
1614 * the timers for timeouts.
1615 */
1616 scsi_init_cmd_errh(cmd);
1617
1618 /*
1619 * Dispatch the command to the low-level driver.
1620 */
3b5382c4 1621 cmd->scsi_done = scsi_done;
1da177e4 1622 rtn = scsi_dispatch_cmd(cmd);
d0d3bbf9
CH
1623 if (rtn) {
1624 scsi_queue_insert(cmd, rtn);
1625 spin_lock_irq(q->queue_lock);
a488e749 1626 goto out_delay;
d0d3bbf9
CH
1627 }
1628 spin_lock_irq(q->queue_lock);
1da177e4
LT
1629 }
1630
613be1f6 1631 return;
1da177e4 1632
cf68d334 1633 host_not_ready:
2ccbb008
CH
1634 if (scsi_target(sdev)->can_queue > 0)
1635 atomic_dec(&scsi_target(sdev)->target_busy);
cf68d334 1636 not_ready:
1da177e4
LT
1637 /*
1638 * lock q, handle tag, requeue req, and decrement device_busy. We
1639 * must return with queue_lock held.
1640 *
1641 * Decrementing device_busy without checking it is OK, as all such
1642 * cases (host limits or settings) should run the queue at some
1643 * later time.
1644 */
1645 spin_lock_irq(q->queue_lock);
1646 blk_requeue_request(q, req);
71e75c97 1647 atomic_dec(&sdev->device_busy);
a488e749 1648out_delay:
71e75c97 1649 if (atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
a488e749 1650 blk_delay_queue(q, SCSI_QUEUE_DELAY);
1da177e4
LT
1651}
1652
f1bea55d 1653static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
1da177e4
LT
1654{
1655 struct device *host_dev;
1656 u64 bounce_limit = 0xffffffff;
1657
1658 if (shost->unchecked_isa_dma)
1659 return BLK_BOUNCE_ISA;
1660 /*
1661 * Platforms with virtual-DMA translation
1662 * hardware have no practical limit.
1663 */
1664 if (!PCI_DMA_BUS_IS_PHYS)
1665 return BLK_BOUNCE_ANY;
1666
1667 host_dev = scsi_get_device(shost);
1668 if (host_dev && host_dev->dma_mask)
e83b3664 1669 bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
1da177e4
LT
1670
1671 return bounce_limit;
1672}
1da177e4 1673
b58d9154
FT
1674struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
1675 request_fn_proc *request_fn)
1da177e4 1676{
1da177e4 1677 struct request_queue *q;
6f381fa3 1678 struct device *dev = shost->dma_dev;
1da177e4 1679
b58d9154 1680 q = blk_init_queue(request_fn, NULL);
1da177e4
LT
1681 if (!q)
1682 return NULL;
1683
a8474ce2
JA
1684 /*
1685 * this limit is imposed by hardware restrictions
1686 */
8a78362c
MP
1687 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
1688 SCSI_MAX_SG_CHAIN_SEGMENTS));
a8474ce2 1689
13f05c8d
MP
1690 if (scsi_host_prot_dma(shost)) {
1691 shost->sg_prot_tablesize =
1692 min_not_zero(shost->sg_prot_tablesize,
1693 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
1694 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
1695 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
1696 }
1697
086fa5ff 1698 blk_queue_max_hw_sectors(q, shost->max_sectors);
1da177e4
LT
1699 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
1700 blk_queue_segment_boundary(q, shost->dma_boundary);
99c84dbd 1701 dma_set_seg_boundary(dev, shost->dma_boundary);
1da177e4 1702
860ac568
FT
1703 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
1704
1da177e4 1705 if (!shost->use_clustering)
e692cb66 1706 q->limits.cluster = 0;
465ff318
JB
1707
1708 /*
1709 * set a reasonable default alignment on word boundaries: the
1710 * host and device may alter it using
1711 * blk_queue_update_dma_alignment() later.
1712 */
1713 blk_queue_dma_alignment(q, 0x03);
1714
1da177e4
LT
1715 return q;
1716}
b58d9154
FT
1717EXPORT_SYMBOL(__scsi_alloc_queue);
1718
1719struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
1720{
1721 struct request_queue *q;
1722
1723 q = __scsi_alloc_queue(sdev->host, scsi_request_fn);
1724 if (!q)
1725 return NULL;
1726
1727 blk_queue_prep_rq(q, scsi_prep_fn);
a1b73fc1 1728 blk_queue_unprep_rq(q, scsi_unprep_fn);
b58d9154 1729 blk_queue_softirq_done(q, scsi_softirq_done);
242f9dcb 1730 blk_queue_rq_timed_out(q, scsi_times_out);
6c5121b7 1731 blk_queue_lld_busy(q, scsi_lld_busy);
b58d9154
FT
1732 return q;
1733}
1da177e4 1734
1da177e4
LT
1735/*
1736 * Function: scsi_block_requests()
1737 *
1738 * Purpose: Utility function used by low-level drivers to prevent further
1739 * commands from being queued to the device.
1740 *
1741 * Arguments: shost - Host in question
1742 *
1743 * Returns: Nothing
1744 *
1745 * Lock status: No locks are assumed held.
1746 *
1747 * Notes: There is no timer nor any other means by which the requests
1748 * get unblocked other than the low-level driver calling
1749 * scsi_unblock_requests().
1750 */
1751void scsi_block_requests(struct Scsi_Host *shost)
1752{
1753 shost->host_self_blocked = 1;
1754}
1755EXPORT_SYMBOL(scsi_block_requests);
1756
1757/*
1758 * Function: scsi_unblock_requests()
1759 *
1760 * Purpose: Utility function used by low-level drivers to allow further
1761 * commands from being queued to the device.
1762 *
1763 * Arguments: shost - Host in question
1764 *
1765 * Returns: Nothing
1766 *
1767 * Lock status: No locks are assumed held.
1768 *
1769 * Notes: There is no timer nor any other means by which the requests
1770 * get unblocked other than the low-level driver calling
1771 * scsi_unblock_requests().
1772 *
1773 * This is done as an API function so that changes to the
1774 * internals of the scsi mid-layer won't require wholesale
1775 * changes to drivers that use this feature.
1776 */
1777void scsi_unblock_requests(struct Scsi_Host *shost)
1778{
1779 shost->host_self_blocked = 0;
1780 scsi_run_host_queues(shost);
1781}
1782EXPORT_SYMBOL(scsi_unblock_requests);
1783
1784int __init scsi_init_queue(void)
1785{
1786 int i;
1787
6362abd3
MP
1788 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
1789 sizeof(struct scsi_data_buffer),
1790 0, 0, NULL);
1791 if (!scsi_sdb_cache) {
1792 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
f078727b 1793 return -ENOMEM;
6f9a35e2
BH
1794 }
1795
1da177e4
LT
1796 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1797 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1798 int size = sgp->size * sizeof(struct scatterlist);
1799
1800 sgp->slab = kmem_cache_create(sgp->name, size, 0,
20c2df83 1801 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
1802 if (!sgp->slab) {
1803 printk(KERN_ERR "SCSI: can't init sg slab %s\n",
1804 sgp->name);
6362abd3 1805 goto cleanup_sdb;
1da177e4
LT
1806 }
1807
93d2341c
MD
1808 sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
1809 sgp->slab);
1da177e4
LT
1810 if (!sgp->pool) {
1811 printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
1812 sgp->name);
6362abd3 1813 goto cleanup_sdb;
1da177e4
LT
1814 }
1815 }
1816
1817 return 0;
3d9dd6ee 1818
6362abd3 1819cleanup_sdb:
3d9dd6ee
FT
1820 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1821 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1822 if (sgp->pool)
1823 mempool_destroy(sgp->pool);
1824 if (sgp->slab)
1825 kmem_cache_destroy(sgp->slab);
1826 }
6362abd3 1827 kmem_cache_destroy(scsi_sdb_cache);
3d9dd6ee
FT
1828
1829 return -ENOMEM;
1da177e4
LT
1830}
1831
1832void scsi_exit_queue(void)
1833{
1834 int i;
1835
6362abd3 1836 kmem_cache_destroy(scsi_sdb_cache);
aa7b5cd7 1837
1da177e4
LT
1838 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1839 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1840 mempool_destroy(sgp->pool);
1841 kmem_cache_destroy(sgp->slab);
1842 }
1843}
5baba830
JB
1844
1845/**
1846 * scsi_mode_select - issue a mode select
1847 * @sdev: SCSI device to be queried
1848 * @pf: Page format bit (1 == standard, 0 == vendor specific)
1849 * @sp: Save page bit (0 == don't save, 1 == save)
1850 * @modepage: mode page being requested
1851 * @buffer: request buffer (may not be smaller than eight bytes)
1852 * @len: length of request buffer.
1853 * @timeout: command timeout
1854 * @retries: number of retries before failing
1855 * @data: returns a structure abstracting the mode header data
eb44820c 1856 * @sshdr: place to put sense data (or NULL if no sense to be collected).
5baba830
JB
1857 * must be SCSI_SENSE_BUFFERSIZE big.
1858 *
1859 * Returns zero if successful; negative error number or scsi
1860 * status on error
1861 *
1862 */
1863int
1864scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
1865 unsigned char *buffer, int len, int timeout, int retries,
1866 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
1867{
1868 unsigned char cmd[10];
1869 unsigned char *real_buffer;
1870 int ret;
1871
1872 memset(cmd, 0, sizeof(cmd));
1873 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
1874
1875 if (sdev->use_10_for_ms) {
1876 if (len > 65535)
1877 return -EINVAL;
1878 real_buffer = kmalloc(8 + len, GFP_KERNEL);
1879 if (!real_buffer)
1880 return -ENOMEM;
1881 memcpy(real_buffer + 8, buffer, len);
1882 len += 8;
1883 real_buffer[0] = 0;
1884 real_buffer[1] = 0;
1885 real_buffer[2] = data->medium_type;
1886 real_buffer[3] = data->device_specific;
1887 real_buffer[4] = data->longlba ? 0x01 : 0;
1888 real_buffer[5] = 0;
1889 real_buffer[6] = data->block_descriptor_length >> 8;
1890 real_buffer[7] = data->block_descriptor_length;
1891
1892 cmd[0] = MODE_SELECT_10;
1893 cmd[7] = len >> 8;
1894 cmd[8] = len;
1895 } else {
1896 if (len > 255 || data->block_descriptor_length > 255 ||
1897 data->longlba)
1898 return -EINVAL;
1899
1900 real_buffer = kmalloc(4 + len, GFP_KERNEL);
1901 if (!real_buffer)
1902 return -ENOMEM;
1903 memcpy(real_buffer + 4, buffer, len);
1904 len += 4;
1905 real_buffer[0] = 0;
1906 real_buffer[1] = data->medium_type;
1907 real_buffer[2] = data->device_specific;
1908 real_buffer[3] = data->block_descriptor_length;
1909
1910
1911 cmd[0] = MODE_SELECT;
1912 cmd[4] = len;
1913 }
1914
1915 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
f4f4e47e 1916 sshdr, timeout, retries, NULL);
5baba830
JB
1917 kfree(real_buffer);
1918 return ret;
1919}
1920EXPORT_SYMBOL_GPL(scsi_mode_select);
1921
1da177e4 1922/**
eb44820c 1923 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
1cf72699 1924 * @sdev: SCSI device to be queried
1da177e4
LT
1925 * @dbd: set if mode sense will allow block descriptors to be returned
1926 * @modepage: mode page being requested
1927 * @buffer: request buffer (may not be smaller than eight bytes)
1928 * @len: length of request buffer.
1929 * @timeout: command timeout
1930 * @retries: number of retries before failing
1931 * @data: returns a structure abstracting the mode header data
eb44820c 1932 * @sshdr: place to put sense data (or NULL if no sense to be collected).
1cf72699 1933 * must be SCSI_SENSE_BUFFERSIZE big.
1da177e4
LT
1934 *
1935 * Returns zero if unsuccessful, or the header offset (either 4
1936 * or 8 depending on whether a six or ten byte command was
1937 * issued) if successful.
eb44820c 1938 */
1da177e4 1939int
1cf72699 1940scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
1da177e4 1941 unsigned char *buffer, int len, int timeout, int retries,
5baba830
JB
1942 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
1943{
1da177e4
LT
1944 unsigned char cmd[12];
1945 int use_10_for_ms;
1946 int header_length;
1cf72699 1947 int result;
ea73a9f2 1948 struct scsi_sense_hdr my_sshdr;
1da177e4
LT
1949
1950 memset(data, 0, sizeof(*data));
1951 memset(&cmd[0], 0, 12);
1952 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
1953 cmd[2] = modepage;
1954
ea73a9f2
JB
1955 /* caller might not be interested in sense, but we need it */
1956 if (!sshdr)
1957 sshdr = &my_sshdr;
1958
1da177e4 1959 retry:
1cf72699 1960 use_10_for_ms = sdev->use_10_for_ms;
1da177e4
LT
1961
1962 if (use_10_for_ms) {
1963 if (len < 8)
1964 len = 8;
1965
1966 cmd[0] = MODE_SENSE_10;
1967 cmd[8] = len;
1968 header_length = 8;
1969 } else {
1970 if (len < 4)
1971 len = 4;
1972
1973 cmd[0] = MODE_SENSE;
1974 cmd[4] = len;
1975 header_length = 4;
1976 }
1977
1da177e4
LT
1978 memset(buffer, 0, len);
1979
1cf72699 1980 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
f4f4e47e 1981 sshdr, timeout, retries, NULL);
1da177e4
LT
1982
1983 /* This code looks awful: what it's doing is making sure an
1984 * ILLEGAL REQUEST sense return identifies the actual command
1985 * byte as the problem. MODE_SENSE commands can return
1986 * ILLEGAL REQUEST if the code page isn't supported */
1987
1cf72699
JB
1988 if (use_10_for_ms && !scsi_status_is_good(result) &&
1989 (driver_byte(result) & DRIVER_SENSE)) {
ea73a9f2
JB
1990 if (scsi_sense_valid(sshdr)) {
1991 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
1992 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
1da177e4
LT
1993 /*
1994 * Invalid command operation code
1995 */
1cf72699 1996 sdev->use_10_for_ms = 0;
1da177e4
LT
1997 goto retry;
1998 }
1999 }
2000 }
2001
1cf72699 2002 if(scsi_status_is_good(result)) {
6d73c851
AV
2003 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2004 (modepage == 6 || modepage == 8))) {
2005 /* Initio breakage? */
2006 header_length = 0;
2007 data->length = 13;
2008 data->medium_type = 0;
2009 data->device_specific = 0;
2010 data->longlba = 0;
2011 data->block_descriptor_length = 0;
2012 } else if(use_10_for_ms) {
1da177e4
LT
2013 data->length = buffer[0]*256 + buffer[1] + 2;
2014 data->medium_type = buffer[2];
2015 data->device_specific = buffer[3];
2016 data->longlba = buffer[4] & 0x01;
2017 data->block_descriptor_length = buffer[6]*256
2018 + buffer[7];
2019 } else {
2020 data->length = buffer[0] + 1;
2021 data->medium_type = buffer[1];
2022 data->device_specific = buffer[2];
2023 data->block_descriptor_length = buffer[3];
2024 }
6d73c851 2025 data->header_length = header_length;
1da177e4
LT
2026 }
2027
1cf72699 2028 return result;
1da177e4
LT
2029}
2030EXPORT_SYMBOL(scsi_mode_sense);
2031
001aac25
JB
2032/**
2033 * scsi_test_unit_ready - test if unit is ready
2034 * @sdev: scsi device to change the state of.
2035 * @timeout: command timeout
2036 * @retries: number of retries before failing
2037 * @sshdr_external: Optional pointer to struct scsi_sense_hdr for
2038 * returning sense. Make sure that this is cleared before passing
2039 * in.
2040 *
2041 * Returns zero if unsuccessful or an error if TUR failed. For
9f8a2c23 2042 * removable media, UNIT_ATTENTION sets ->changed flag.
001aac25 2043 **/
1da177e4 2044int
001aac25
JB
2045scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
2046 struct scsi_sense_hdr *sshdr_external)
1da177e4 2047{
1da177e4
LT
2048 char cmd[] = {
2049 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2050 };
001aac25 2051 struct scsi_sense_hdr *sshdr;
1da177e4 2052 int result;
001aac25
JB
2053
2054 if (!sshdr_external)
2055 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
2056 else
2057 sshdr = sshdr_external;
2058
2059 /* try to eat the UNIT_ATTENTION if there are enough retries */
2060 do {
2061 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
f4f4e47e 2062 timeout, retries, NULL);
32c356d7
JB
2063 if (sdev->removable && scsi_sense_valid(sshdr) &&
2064 sshdr->sense_key == UNIT_ATTENTION)
2065 sdev->changed = 1;
2066 } while (scsi_sense_valid(sshdr) &&
2067 sshdr->sense_key == UNIT_ATTENTION && --retries);
001aac25 2068
001aac25
JB
2069 if (!sshdr_external)
2070 kfree(sshdr);
1da177e4
LT
2071 return result;
2072}
2073EXPORT_SYMBOL(scsi_test_unit_ready);
2074
2075/**
eb44820c 2076 * scsi_device_set_state - Take the given device through the device state model.
1da177e4
LT
2077 * @sdev: scsi device to change the state of.
2078 * @state: state to change to.
2079 *
2080 * Returns zero if unsuccessful or an error if the requested
2081 * transition is illegal.
eb44820c 2082 */
1da177e4
LT
2083int
2084scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2085{
2086 enum scsi_device_state oldstate = sdev->sdev_state;
2087
2088 if (state == oldstate)
2089 return 0;
2090
2091 switch (state) {
2092 case SDEV_CREATED:
6f4267e3
JB
2093 switch (oldstate) {
2094 case SDEV_CREATED_BLOCK:
2095 break;
2096 default:
2097 goto illegal;
2098 }
2099 break;
1da177e4
LT
2100
2101 case SDEV_RUNNING:
2102 switch (oldstate) {
2103 case SDEV_CREATED:
2104 case SDEV_OFFLINE:
1b8d2620 2105 case SDEV_TRANSPORT_OFFLINE:
1da177e4
LT
2106 case SDEV_QUIESCE:
2107 case SDEV_BLOCK:
2108 break;
2109 default:
2110 goto illegal;
2111 }
2112 break;
2113
2114 case SDEV_QUIESCE:
2115 switch (oldstate) {
2116 case SDEV_RUNNING:
2117 case SDEV_OFFLINE:
1b8d2620 2118 case SDEV_TRANSPORT_OFFLINE:
1da177e4
LT
2119 break;
2120 default:
2121 goto illegal;
2122 }
2123 break;
2124
2125 case SDEV_OFFLINE:
1b8d2620 2126 case SDEV_TRANSPORT_OFFLINE:
1da177e4
LT
2127 switch (oldstate) {
2128 case SDEV_CREATED:
2129 case SDEV_RUNNING:
2130 case SDEV_QUIESCE:
2131 case SDEV_BLOCK:
2132 break;
2133 default:
2134 goto illegal;
2135 }
2136 break;
2137
2138 case SDEV_BLOCK:
2139 switch (oldstate) {
1da177e4 2140 case SDEV_RUNNING:
6f4267e3
JB
2141 case SDEV_CREATED_BLOCK:
2142 break;
2143 default:
2144 goto illegal;
2145 }
2146 break;
2147
2148 case SDEV_CREATED_BLOCK:
2149 switch (oldstate) {
2150 case SDEV_CREATED:
1da177e4
LT
2151 break;
2152 default:
2153 goto illegal;
2154 }
2155 break;
2156
2157 case SDEV_CANCEL:
2158 switch (oldstate) {
2159 case SDEV_CREATED:
2160 case SDEV_RUNNING:
9ea72909 2161 case SDEV_QUIESCE:
1da177e4 2162 case SDEV_OFFLINE:
1b8d2620 2163 case SDEV_TRANSPORT_OFFLINE:
1da177e4
LT
2164 case SDEV_BLOCK:
2165 break;
2166 default:
2167 goto illegal;
2168 }
2169 break;
2170
2171 case SDEV_DEL:
2172 switch (oldstate) {
309bd271
BK
2173 case SDEV_CREATED:
2174 case SDEV_RUNNING:
2175 case SDEV_OFFLINE:
1b8d2620 2176 case SDEV_TRANSPORT_OFFLINE:
1da177e4 2177 case SDEV_CANCEL:
0516c08d 2178 case SDEV_CREATED_BLOCK:
1da177e4
LT
2179 break;
2180 default:
2181 goto illegal;
2182 }
2183 break;
2184
2185 }
2186 sdev->sdev_state = state;
2187 return 0;
2188
2189 illegal:
91921e01 2190 SCSI_LOG_ERROR_RECOVERY(1,
9ccfc756 2191 sdev_printk(KERN_ERR, sdev,
91921e01 2192 "Illegal state transition %s->%s",
9ccfc756
JB
2193 scsi_device_state_name(oldstate),
2194 scsi_device_state_name(state))
1da177e4
LT
2195 );
2196 return -EINVAL;
2197}
2198EXPORT_SYMBOL(scsi_device_set_state);
2199
a341cd0f
JG
2200/**
2201 * sdev_evt_emit - emit a single SCSI device uevent
2202 * @sdev: associated SCSI device
2203 * @evt: event to emit
2204 *
2205 * Send a single uevent (scsi_event) to the associated scsi_device.
2206 */
2207static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2208{
2209 int idx = 0;
2210 char *envp[3];
2211
2212 switch (evt->evt_type) {
2213 case SDEV_EVT_MEDIA_CHANGE:
2214 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2215 break;
279afdfe
EM
2216 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2217 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2218 break;
2219 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2220 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2221 break;
2222 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2223 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2224 break;
2225 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2226 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2227 break;
2228 case SDEV_EVT_LUN_CHANGE_REPORTED:
2229 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2230 break;
a341cd0f
JG
2231 default:
2232 /* do nothing */
2233 break;
2234 }
2235
2236 envp[idx++] = NULL;
2237
2238 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2239}
2240
2241/**
2242 * sdev_evt_thread - send a uevent for each scsi event
2243 * @work: work struct for scsi_device
2244 *
2245 * Dispatch queued events to their associated scsi_device kobjects
2246 * as uevents.
2247 */
2248void scsi_evt_thread(struct work_struct *work)
2249{
2250 struct scsi_device *sdev;
279afdfe 2251 enum scsi_device_event evt_type;
a341cd0f
JG
2252 LIST_HEAD(event_list);
2253
2254 sdev = container_of(work, struct scsi_device, event_work);
2255
279afdfe
EM
2256 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2257 if (test_and_clear_bit(evt_type, sdev->pending_events))
2258 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2259
a341cd0f
JG
2260 while (1) {
2261 struct scsi_event *evt;
2262 struct list_head *this, *tmp;
2263 unsigned long flags;
2264
2265 spin_lock_irqsave(&sdev->list_lock, flags);
2266 list_splice_init(&sdev->event_list, &event_list);
2267 spin_unlock_irqrestore(&sdev->list_lock, flags);
2268
2269 if (list_empty(&event_list))
2270 break;
2271
2272 list_for_each_safe(this, tmp, &event_list) {
2273 evt = list_entry(this, struct scsi_event, node);
2274 list_del(&evt->node);
2275 scsi_evt_emit(sdev, evt);
2276 kfree(evt);
2277 }
2278 }
2279}
2280
2281/**
2282 * sdev_evt_send - send asserted event to uevent thread
2283 * @sdev: scsi_device event occurred on
2284 * @evt: event to send
2285 *
2286 * Assert scsi device event asynchronously.
2287 */
2288void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2289{
2290 unsigned long flags;
2291
4d1566ed
KS
2292#if 0
2293 /* FIXME: currently this check eliminates all media change events
2294 * for polled devices. Need to update to discriminate between AN
2295 * and polled events */
a341cd0f
JG
2296 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2297 kfree(evt);
2298 return;
2299 }
4d1566ed 2300#endif
a341cd0f
JG
2301
2302 spin_lock_irqsave(&sdev->list_lock, flags);
2303 list_add_tail(&evt->node, &sdev->event_list);
2304 schedule_work(&sdev->event_work);
2305 spin_unlock_irqrestore(&sdev->list_lock, flags);
2306}
2307EXPORT_SYMBOL_GPL(sdev_evt_send);
2308
2309/**
2310 * sdev_evt_alloc - allocate a new scsi event
2311 * @evt_type: type of event to allocate
2312 * @gfpflags: GFP flags for allocation
2313 *
2314 * Allocates and returns a new scsi_event.
2315 */
2316struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2317 gfp_t gfpflags)
2318{
2319 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2320 if (!evt)
2321 return NULL;
2322
2323 evt->evt_type = evt_type;
2324 INIT_LIST_HEAD(&evt->node);
2325
2326 /* evt_type-specific initialization, if any */
2327 switch (evt_type) {
2328 case SDEV_EVT_MEDIA_CHANGE:
279afdfe
EM
2329 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2330 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2331 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2332 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2333 case SDEV_EVT_LUN_CHANGE_REPORTED:
a341cd0f
JG
2334 default:
2335 /* do nothing */
2336 break;
2337 }
2338
2339 return evt;
2340}
2341EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2342
2343/**
2344 * sdev_evt_send_simple - send asserted event to uevent thread
2345 * @sdev: scsi_device event occurred on
2346 * @evt_type: type of event to send
2347 * @gfpflags: GFP flags for allocation
2348 *
2349 * Assert scsi device event asynchronously, given an event type.
2350 */
2351void sdev_evt_send_simple(struct scsi_device *sdev,
2352 enum scsi_device_event evt_type, gfp_t gfpflags)
2353{
2354 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2355 if (!evt) {
2356 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2357 evt_type);
2358 return;
2359 }
2360
2361 sdev_evt_send(sdev, evt);
2362}
2363EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2364
1da177e4
LT
2365/**
2366 * scsi_device_quiesce - Block user issued commands.
2367 * @sdev: scsi device to quiesce.
2368 *
2369 * This works by trying to transition to the SDEV_QUIESCE state
2370 * (which must be a legal transition). When the device is in this
2371 * state, only special requests will be accepted, all others will
2372 * be deferred. Since special requests may also be requeued requests,
2373 * a successful return doesn't guarantee the device will be
2374 * totally quiescent.
2375 *
2376 * Must be called with user context, may sleep.
2377 *
2378 * Returns zero if unsuccessful or an error if not.
eb44820c 2379 */
1da177e4
LT
2380int
2381scsi_device_quiesce(struct scsi_device *sdev)
2382{
2383 int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2384 if (err)
2385 return err;
2386
2387 scsi_run_queue(sdev->request_queue);
71e75c97 2388 while (atomic_read(&sdev->device_busy)) {
1da177e4
LT
2389 msleep_interruptible(200);
2390 scsi_run_queue(sdev->request_queue);
2391 }
2392 return 0;
2393}
2394EXPORT_SYMBOL(scsi_device_quiesce);
2395
2396/**
2397 * scsi_device_resume - Restart user issued commands to a quiesced device.
2398 * @sdev: scsi device to resume.
2399 *
2400 * Moves the device from quiesced back to running and restarts the
2401 * queues.
2402 *
2403 * Must be called with user context, may sleep.
eb44820c 2404 */
a7a20d10 2405void scsi_device_resume(struct scsi_device *sdev)
1da177e4 2406{
a7a20d10
DW
2407 /* check if the device state was mutated prior to resume, and if
2408 * so assume the state is being managed elsewhere (for example
2409 * device deleted during suspend)
2410 */
2411 if (sdev->sdev_state != SDEV_QUIESCE ||
2412 scsi_device_set_state(sdev, SDEV_RUNNING))
1da177e4
LT
2413 return;
2414 scsi_run_queue(sdev->request_queue);
2415}
2416EXPORT_SYMBOL(scsi_device_resume);
2417
2418static void
2419device_quiesce_fn(struct scsi_device *sdev, void *data)
2420{
2421 scsi_device_quiesce(sdev);
2422}
2423
2424void
2425scsi_target_quiesce(struct scsi_target *starget)
2426{
2427 starget_for_each_device(starget, NULL, device_quiesce_fn);
2428}
2429EXPORT_SYMBOL(scsi_target_quiesce);
2430
2431static void
2432device_resume_fn(struct scsi_device *sdev, void *data)
2433{
2434 scsi_device_resume(sdev);
2435}
2436
2437void
2438scsi_target_resume(struct scsi_target *starget)
2439{
2440 starget_for_each_device(starget, NULL, device_resume_fn);
2441}
2442EXPORT_SYMBOL(scsi_target_resume);
2443
2444/**
eb44820c 2445 * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
1da177e4
LT
2446 * @sdev: device to block
2447 *
2448 * Block request made by scsi lld's to temporarily stop all
2449 * scsi commands on the specified device. Called from interrupt
2450 * or normal process context.
2451 *
2452 * Returns zero if successful or error if not
2453 *
2454 * Notes:
2455 * This routine transitions the device to the SDEV_BLOCK state
2456 * (which must be a legal transition). When the device is in this
2457 * state, all commands are deferred until the scsi lld reenables
2458 * the device with scsi_device_unblock or device_block_tmo fires.
eb44820c 2459 */
1da177e4
LT
2460int
2461scsi_internal_device_block(struct scsi_device *sdev)
2462{
165125e1 2463 struct request_queue *q = sdev->request_queue;
1da177e4
LT
2464 unsigned long flags;
2465 int err = 0;
2466
2467 err = scsi_device_set_state(sdev, SDEV_BLOCK);
6f4267e3
JB
2468 if (err) {
2469 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2470
2471 if (err)
2472 return err;
2473 }
1da177e4
LT
2474
2475 /*
2476 * The device has transitioned to SDEV_BLOCK. Stop the
2477 * block layer from calling the midlayer with this device's
2478 * request queue.
2479 */
2480 spin_lock_irqsave(q->queue_lock, flags);
2481 blk_stop_queue(q);
2482 spin_unlock_irqrestore(q->queue_lock, flags);
2483
2484 return 0;
2485}
2486EXPORT_SYMBOL_GPL(scsi_internal_device_block);
2487
2488/**
2489 * scsi_internal_device_unblock - resume a device after a block request
2490 * @sdev: device to resume
5d9fb5cc 2491 * @new_state: state to set devices to after unblocking
1da177e4
LT
2492 *
2493 * Called by scsi lld's or the midlayer to restart the device queue
2494 * for the previously suspended scsi device. Called from interrupt or
2495 * normal process context.
2496 *
2497 * Returns zero if successful or error if not.
2498 *
2499 * Notes:
2500 * This routine transitions the device to the SDEV_RUNNING state
5d9fb5cc 2501 * or to one of the offline states (which must be a legal transition)
d075498c 2502 * allowing the midlayer to goose the queue for this device.
eb44820c 2503 */
1da177e4 2504int
5d9fb5cc
MC
2505scsi_internal_device_unblock(struct scsi_device *sdev,
2506 enum scsi_device_state new_state)
1da177e4 2507{
165125e1 2508 struct request_queue *q = sdev->request_queue;
1da177e4 2509 unsigned long flags;
5d9fb5cc
MC
2510
2511 /*
2512 * Try to transition the scsi device to SDEV_RUNNING or one of the
2513 * offlined states and goose the device queue if successful.
1da177e4 2514 */
0e58076b
VC
2515 if ((sdev->sdev_state == SDEV_BLOCK) ||
2516 (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE))
5d9fb5cc
MC
2517 sdev->sdev_state = new_state;
2518 else if (sdev->sdev_state == SDEV_CREATED_BLOCK) {
2519 if (new_state == SDEV_TRANSPORT_OFFLINE ||
2520 new_state == SDEV_OFFLINE)
2521 sdev->sdev_state = new_state;
2522 else
2523 sdev->sdev_state = SDEV_CREATED;
2524 } else if (sdev->sdev_state != SDEV_CANCEL &&
986fe6c7 2525 sdev->sdev_state != SDEV_OFFLINE)
5c10e63c 2526 return -EINVAL;
1da177e4
LT
2527
2528 spin_lock_irqsave(q->queue_lock, flags);
2529 blk_start_queue(q);
2530 spin_unlock_irqrestore(q->queue_lock, flags);
2531
2532 return 0;
2533}
2534EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
2535
2536static void
2537device_block(struct scsi_device *sdev, void *data)
2538{
2539 scsi_internal_device_block(sdev);
2540}
2541
2542static int
2543target_block(struct device *dev, void *data)
2544{
2545 if (scsi_is_target_device(dev))
2546 starget_for_each_device(to_scsi_target(dev), NULL,
2547 device_block);
2548 return 0;
2549}
2550
2551void
2552scsi_target_block(struct device *dev)
2553{
2554 if (scsi_is_target_device(dev))
2555 starget_for_each_device(to_scsi_target(dev), NULL,
2556 device_block);
2557 else
2558 device_for_each_child(dev, NULL, target_block);
2559}
2560EXPORT_SYMBOL_GPL(scsi_target_block);
2561
2562static void
2563device_unblock(struct scsi_device *sdev, void *data)
2564{
5d9fb5cc 2565 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
1da177e4
LT
2566}
2567
2568static int
2569target_unblock(struct device *dev, void *data)
2570{
2571 if (scsi_is_target_device(dev))
5d9fb5cc 2572 starget_for_each_device(to_scsi_target(dev), data,
1da177e4
LT
2573 device_unblock);
2574 return 0;
2575}
2576
2577void
5d9fb5cc 2578scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
1da177e4
LT
2579{
2580 if (scsi_is_target_device(dev))
5d9fb5cc 2581 starget_for_each_device(to_scsi_target(dev), &new_state,
1da177e4
LT
2582 device_unblock);
2583 else
5d9fb5cc 2584 device_for_each_child(dev, &new_state, target_unblock);
1da177e4
LT
2585}
2586EXPORT_SYMBOL_GPL(scsi_target_unblock);
cdb8c2a6
GL
2587
2588/**
2589 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
eb44820c 2590 * @sgl: scatter-gather list
cdb8c2a6
GL
2591 * @sg_count: number of segments in sg
2592 * @offset: offset in bytes into sg, on return offset into the mapped area
2593 * @len: bytes to map, on return number of bytes mapped
2594 *
2595 * Returns virtual address of the start of the mapped page
2596 */
c6132da1 2597void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
cdb8c2a6
GL
2598 size_t *offset, size_t *len)
2599{
2600 int i;
2601 size_t sg_len = 0, len_complete = 0;
c6132da1 2602 struct scatterlist *sg;
cdb8c2a6
GL
2603 struct page *page;
2604
22cfefb5
AM
2605 WARN_ON(!irqs_disabled());
2606
c6132da1 2607 for_each_sg(sgl, sg, sg_count, i) {
cdb8c2a6 2608 len_complete = sg_len; /* Complete sg-entries */
c6132da1 2609 sg_len += sg->length;
cdb8c2a6
GL
2610 if (sg_len > *offset)
2611 break;
2612 }
2613
2614 if (unlikely(i == sg_count)) {
169e1a2a
AM
2615 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
2616 "elements %d\n",
cadbd4a5 2617 __func__, sg_len, *offset, sg_count);
cdb8c2a6
GL
2618 WARN_ON(1);
2619 return NULL;
2620 }
2621
2622 /* Offset starting from the beginning of first page in this sg-entry */
c6132da1 2623 *offset = *offset - len_complete + sg->offset;
cdb8c2a6
GL
2624
2625 /* Assumption: contiguous pages can be accessed as "page + i" */
45711f1a 2626 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
cdb8c2a6
GL
2627 *offset &= ~PAGE_MASK;
2628
2629 /* Bytes in this sg-entry from *offset to the end of the page */
2630 sg_len = PAGE_SIZE - *offset;
2631 if (*len > sg_len)
2632 *len = sg_len;
2633
77dfce07 2634 return kmap_atomic(page);
cdb8c2a6
GL
2635}
2636EXPORT_SYMBOL(scsi_kmap_atomic_sg);
2637
2638/**
eb44820c 2639 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
cdb8c2a6
GL
2640 * @virt: virtual address to be unmapped
2641 */
2642void scsi_kunmap_atomic_sg(void *virt)
2643{
77dfce07 2644 kunmap_atomic(virt);
cdb8c2a6
GL
2645}
2646EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
6f4c827e
AL
2647
2648void sdev_disable_disk_events(struct scsi_device *sdev)
2649{
2650 atomic_inc(&sdev->disk_events_disable_depth);
2651}
2652EXPORT_SYMBOL(sdev_disable_disk_events);
2653
2654void sdev_enable_disk_events(struct scsi_device *sdev)
2655{
2656 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
2657 return;
2658 atomic_dec(&sdev->disk_events_disable_depth);
2659}
2660EXPORT_SYMBOL(sdev_enable_disk_events);
This page took 1.107543 seconds and 5 git commands to generate.