xref: /linux/drivers/scsi/scsi_lib.c (revision 98366c20a275e957416e9516db5dcb7195b4e101)
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>
11 #include <linux/blkdev.h>
12 #include <linux/completion.h>
13 #include <linux/kernel.h>
14 #include <linux/mempool.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/delay.h>
19 #include <linux/hardirq.h>
20 #include <linux/scatterlist.h>
21 
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_cmnd.h>
24 #include <scsi/scsi_dbg.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_driver.h>
27 #include <scsi/scsi_eh.h>
28 #include <scsi/scsi_host.h>
29 
30 #include "scsi_priv.h"
31 #include "scsi_logging.h"
32 
33 
34 #define SG_MEMPOOL_NR		ARRAY_SIZE(scsi_sg_pools)
35 #define SG_MEMPOOL_SIZE		2
36 
37 /*
38  * The maximum number of SG segments that we will put inside a scatterlist
39  * (unless chaining is used). Should ideally fit inside a single page, to
40  * avoid a higher order allocation.
41  */
42 #define SCSI_MAX_SG_SEGMENTS	128
43 
44 struct scsi_host_sg_pool {
45 	size_t		size;
46 	char		*name;
47 	struct kmem_cache	*slab;
48 	mempool_t	*pool;
49 };
50 
51 #define SP(x) { x, "sgpool-" #x }
52 static struct scsi_host_sg_pool scsi_sg_pools[] = {
53 	SP(8),
54 	SP(16),
55 #if (SCSI_MAX_SG_SEGMENTS > 16)
56 	SP(32),
57 #if (SCSI_MAX_SG_SEGMENTS > 32)
58 	SP(64),
59 #if (SCSI_MAX_SG_SEGMENTS > 64)
60 	SP(128),
61 #endif
62 #endif
63 #endif
64 };
65 #undef SP
66 
67 static void scsi_run_queue(struct request_queue *q);
68 
69 /*
70  * Function:	scsi_unprep_request()
71  *
72  * Purpose:	Remove all preparation done for a request, including its
73  *		associated scsi_cmnd, so that it can be requeued.
74  *
75  * Arguments:	req	- request to unprepare
76  *
77  * Lock status:	Assumed that no locks are held upon entry.
78  *
79  * Returns:	Nothing.
80  */
81 static void scsi_unprep_request(struct request *req)
82 {
83 	struct scsi_cmnd *cmd = req->special;
84 
85 	req->cmd_flags &= ~REQ_DONTPREP;
86 	req->special = NULL;
87 
88 	scsi_put_command(cmd);
89 }
90 
91 /*
92  * Function:    scsi_queue_insert()
93  *
94  * Purpose:     Insert a command in the midlevel queue.
95  *
96  * Arguments:   cmd    - command that we are adding to queue.
97  *              reason - why we are inserting command to queue.
98  *
99  * Lock status: Assumed that lock is not held upon entry.
100  *
101  * Returns:     Nothing.
102  *
103  * Notes:       We do this for one of two cases.  Either the host is busy
104  *              and it cannot accept any more commands for the time being,
105  *              or the device returned QUEUE_FULL and can accept no more
106  *              commands.
107  * Notes:       This could be called either from an interrupt context or a
108  *              normal process context.
109  */
110 int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
111 {
112 	struct Scsi_Host *host = cmd->device->host;
113 	struct scsi_device *device = cmd->device;
114 	struct request_queue *q = device->request_queue;
115 	unsigned long flags;
116 
117 	SCSI_LOG_MLQUEUE(1,
118 		 printk("Inserting command %p into mlqueue\n", cmd));
119 
120 	/*
121 	 * Set the appropriate busy bit for the device/host.
122 	 *
123 	 * If the host/device isn't busy, assume that something actually
124 	 * completed, and that we should be able to queue a command now.
125 	 *
126 	 * Note that the prior mid-layer assumption that any host could
127 	 * always queue at least one command is now broken.  The mid-layer
128 	 * will implement a user specifiable stall (see
129 	 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
130 	 * if a command is requeued with no other commands outstanding
131 	 * either for the device or for the host.
132 	 */
133 	if (reason == SCSI_MLQUEUE_HOST_BUSY)
134 		host->host_blocked = host->max_host_blocked;
135 	else if (reason == SCSI_MLQUEUE_DEVICE_BUSY)
136 		device->device_blocked = device->max_device_blocked;
137 
138 	/*
139 	 * Decrement the counters, since these commands are no longer
140 	 * active on the host/device.
141 	 */
142 	scsi_device_unbusy(device);
143 
144 	/*
145 	 * Requeue this command.  It will go before all other commands
146 	 * that are already in the queue.
147 	 *
148 	 * NOTE: there is magic here about the way the queue is plugged if
149 	 * we have no outstanding commands.
150 	 *
151 	 * Although we *don't* plug the queue, we call the request
152 	 * function.  The SCSI request function detects the blocked condition
153 	 * and plugs the queue appropriately.
154          */
155 	spin_lock_irqsave(q->queue_lock, flags);
156 	blk_requeue_request(q, cmd->request);
157 	spin_unlock_irqrestore(q->queue_lock, flags);
158 
159 	scsi_run_queue(q);
160 
161 	return 0;
162 }
163 
164 /**
165  * scsi_execute - insert request and wait for the result
166  * @sdev:	scsi device
167  * @cmd:	scsi command
168  * @data_direction: data direction
169  * @buffer:	data buffer
170  * @bufflen:	len of buffer
171  * @sense:	optional sense buffer
172  * @timeout:	request timeout in seconds
173  * @retries:	number of times to retry request
174  * @flags:	or into request flags;
175  *
176  * returns the req->errors value which is the scsi_cmnd result
177  * field.
178  **/
179 int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
180 		 int data_direction, void *buffer, unsigned bufflen,
181 		 unsigned char *sense, int timeout, int retries, int flags)
182 {
183 	struct request *req;
184 	int write = (data_direction == DMA_TO_DEVICE);
185 	int ret = DRIVER_ERROR << 24;
186 
187 	req = blk_get_request(sdev->request_queue, write, __GFP_WAIT);
188 
189 	if (bufflen &&	blk_rq_map_kern(sdev->request_queue, req,
190 					buffer, bufflen, __GFP_WAIT))
191 		goto out;
192 
193 	req->cmd_len = COMMAND_SIZE(cmd[0]);
194 	memcpy(req->cmd, cmd, req->cmd_len);
195 	req->sense = sense;
196 	req->sense_len = 0;
197 	req->retries = retries;
198 	req->timeout = timeout;
199 	req->cmd_type = REQ_TYPE_BLOCK_PC;
200 	req->cmd_flags |= flags | REQ_QUIET | REQ_PREEMPT;
201 
202 	/*
203 	 * head injection *required* here otherwise quiesce won't work
204 	 */
205 	blk_execute_rq(req->q, NULL, req, 1);
206 
207 	ret = req->errors;
208  out:
209 	blk_put_request(req);
210 
211 	return ret;
212 }
213 EXPORT_SYMBOL(scsi_execute);
214 
215 
216 int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd,
217 		     int data_direction, void *buffer, unsigned bufflen,
218 		     struct scsi_sense_hdr *sshdr, int timeout, int retries)
219 {
220 	char *sense = NULL;
221 	int result;
222 
223 	if (sshdr) {
224 		sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
225 		if (!sense)
226 			return DRIVER_ERROR << 24;
227 	}
228 	result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
229 			      sense, timeout, retries, 0);
230 	if (sshdr)
231 		scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
232 
233 	kfree(sense);
234 	return result;
235 }
236 EXPORT_SYMBOL(scsi_execute_req);
237 
238 struct scsi_io_context {
239 	void *data;
240 	void (*done)(void *data, char *sense, int result, int resid);
241 	char sense[SCSI_SENSE_BUFFERSIZE];
242 };
243 
244 static struct kmem_cache *scsi_io_context_cache;
245 
246 static void scsi_end_async(struct request *req, int uptodate)
247 {
248 	struct scsi_io_context *sioc = req->end_io_data;
249 
250 	if (sioc->done)
251 		sioc->done(sioc->data, sioc->sense, req->errors, req->data_len);
252 
253 	kmem_cache_free(scsi_io_context_cache, sioc);
254 	__blk_put_request(req->q, req);
255 }
256 
257 static int scsi_merge_bio(struct request *rq, struct bio *bio)
258 {
259 	struct request_queue *q = rq->q;
260 
261 	bio->bi_flags &= ~(1 << BIO_SEG_VALID);
262 	if (rq_data_dir(rq) == WRITE)
263 		bio->bi_rw |= (1 << BIO_RW);
264 	blk_queue_bounce(q, &bio);
265 
266 	return blk_rq_append_bio(q, rq, bio);
267 }
268 
269 static void scsi_bi_endio(struct bio *bio, int error)
270 {
271 	bio_put(bio);
272 }
273 
274 /**
275  * scsi_req_map_sg - map a scatterlist into a request
276  * @rq:		request to fill
277  * @sg:		scatterlist
278  * @nsegs:	number of elements
279  * @bufflen:	len of buffer
280  * @gfp:	memory allocation flags
281  *
282  * scsi_req_map_sg maps a scatterlist into a request so that the
283  * request can be sent to the block layer. We do not trust the scatterlist
284  * sent to use, as some ULDs use that struct to only organize the pages.
285  */
286 static int scsi_req_map_sg(struct request *rq, struct scatterlist *sgl,
287 			   int nsegs, unsigned bufflen, gfp_t gfp)
288 {
289 	struct request_queue *q = rq->q;
290 	int nr_pages = (bufflen + sgl[0].offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
291 	unsigned int data_len = bufflen, len, bytes, off;
292 	struct scatterlist *sg;
293 	struct page *page;
294 	struct bio *bio = NULL;
295 	int i, err, nr_vecs = 0;
296 
297 	for_each_sg(sgl, sg, nsegs, i) {
298 		page = sg_page(sg);
299 		off = sg->offset;
300 		len = sg->length;
301  		data_len += len;
302 
303 		while (len > 0 && data_len > 0) {
304 			/*
305 			 * sg sends a scatterlist that is larger than
306 			 * the data_len it wants transferred for certain
307 			 * IO sizes
308 			 */
309 			bytes = min_t(unsigned int, len, PAGE_SIZE - off);
310 			bytes = min(bytes, data_len);
311 
312 			if (!bio) {
313 				nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
314 				nr_pages -= nr_vecs;
315 
316 				bio = bio_alloc(gfp, nr_vecs);
317 				if (!bio) {
318 					err = -ENOMEM;
319 					goto free_bios;
320 				}
321 				bio->bi_end_io = scsi_bi_endio;
322 			}
323 
324 			if (bio_add_pc_page(q, bio, page, bytes, off) !=
325 			    bytes) {
326 				bio_put(bio);
327 				err = -EINVAL;
328 				goto free_bios;
329 			}
330 
331 			if (bio->bi_vcnt >= nr_vecs) {
332 				err = scsi_merge_bio(rq, bio);
333 				if (err) {
334 					bio_endio(bio, 0);
335 					goto free_bios;
336 				}
337 				bio = NULL;
338 			}
339 
340 			page++;
341 			len -= bytes;
342 			data_len -=bytes;
343 			off = 0;
344 		}
345 	}
346 
347 	rq->buffer = rq->data = NULL;
348 	rq->data_len = bufflen;
349 	return 0;
350 
351 free_bios:
352 	while ((bio = rq->bio) != NULL) {
353 		rq->bio = bio->bi_next;
354 		/*
355 		 * call endio instead of bio_put incase it was bounced
356 		 */
357 		bio_endio(bio, 0);
358 	}
359 
360 	return err;
361 }
362 
363 /**
364  * scsi_execute_async - insert request
365  * @sdev:	scsi device
366  * @cmd:	scsi command
367  * @cmd_len:	length of scsi cdb
368  * @data_direction: data direction
369  * @buffer:	data buffer (this can be a kernel buffer or scatterlist)
370  * @bufflen:	len of buffer
371  * @use_sg:	if buffer is a scatterlist this is the number of elements
372  * @timeout:	request timeout in seconds
373  * @retries:	number of times to retry request
374  * @flags:	or into request flags
375  **/
376 int scsi_execute_async(struct scsi_device *sdev, const unsigned char *cmd,
377 		       int cmd_len, int data_direction, void *buffer, unsigned bufflen,
378 		       int use_sg, int timeout, int retries, void *privdata,
379 		       void (*done)(void *, char *, int, int), gfp_t gfp)
380 {
381 	struct request *req;
382 	struct scsi_io_context *sioc;
383 	int err = 0;
384 	int write = (data_direction == DMA_TO_DEVICE);
385 
386 	sioc = kmem_cache_zalloc(scsi_io_context_cache, gfp);
387 	if (!sioc)
388 		return DRIVER_ERROR << 24;
389 
390 	req = blk_get_request(sdev->request_queue, write, gfp);
391 	if (!req)
392 		goto free_sense;
393 	req->cmd_type = REQ_TYPE_BLOCK_PC;
394 	req->cmd_flags |= REQ_QUIET;
395 
396 	if (use_sg)
397 		err = scsi_req_map_sg(req, buffer, use_sg, bufflen, gfp);
398 	else if (bufflen)
399 		err = blk_rq_map_kern(req->q, req, buffer, bufflen, gfp);
400 
401 	if (err)
402 		goto free_req;
403 
404 	req->cmd_len = cmd_len;
405 	memset(req->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
406 	memcpy(req->cmd, cmd, req->cmd_len);
407 	req->sense = sioc->sense;
408 	req->sense_len = 0;
409 	req->timeout = timeout;
410 	req->retries = retries;
411 	req->end_io_data = sioc;
412 
413 	sioc->data = privdata;
414 	sioc->done = done;
415 
416 	blk_execute_rq_nowait(req->q, NULL, req, 1, scsi_end_async);
417 	return 0;
418 
419 free_req:
420 	blk_put_request(req);
421 free_sense:
422 	kmem_cache_free(scsi_io_context_cache, sioc);
423 	return DRIVER_ERROR << 24;
424 }
425 EXPORT_SYMBOL_GPL(scsi_execute_async);
426 
427 /*
428  * Function:    scsi_init_cmd_errh()
429  *
430  * Purpose:     Initialize cmd fields related to error handling.
431  *
432  * Arguments:   cmd	- command that is ready to be queued.
433  *
434  * Notes:       This function has the job of initializing a number of
435  *              fields related to error handling.   Typically this will
436  *              be called once for each command, as required.
437  */
438 static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
439 {
440 	cmd->serial_number = 0;
441 	cmd->resid = 0;
442 	memset(cmd->sense_buffer, 0, sizeof cmd->sense_buffer);
443 	if (cmd->cmd_len == 0)
444 		cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
445 }
446 
447 void scsi_device_unbusy(struct scsi_device *sdev)
448 {
449 	struct Scsi_Host *shost = sdev->host;
450 	unsigned long flags;
451 
452 	spin_lock_irqsave(shost->host_lock, flags);
453 	shost->host_busy--;
454 	if (unlikely(scsi_host_in_recovery(shost) &&
455 		     (shost->host_failed || shost->host_eh_scheduled)))
456 		scsi_eh_wakeup(shost);
457 	spin_unlock(shost->host_lock);
458 	spin_lock(sdev->request_queue->queue_lock);
459 	sdev->device_busy--;
460 	spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
461 }
462 
463 /*
464  * Called for single_lun devices on IO completion. Clear starget_sdev_user,
465  * and call blk_run_queue for all the scsi_devices on the target -
466  * including current_sdev first.
467  *
468  * Called with *no* scsi locks held.
469  */
470 static void scsi_single_lun_run(struct scsi_device *current_sdev)
471 {
472 	struct Scsi_Host *shost = current_sdev->host;
473 	struct scsi_device *sdev, *tmp;
474 	struct scsi_target *starget = scsi_target(current_sdev);
475 	unsigned long flags;
476 
477 	spin_lock_irqsave(shost->host_lock, flags);
478 	starget->starget_sdev_user = NULL;
479 	spin_unlock_irqrestore(shost->host_lock, flags);
480 
481 	/*
482 	 * Call blk_run_queue for all LUNs on the target, starting with
483 	 * current_sdev. We race with others (to set starget_sdev_user),
484 	 * but in most cases, we will be first. Ideally, each LU on the
485 	 * target would get some limited time or requests on the target.
486 	 */
487 	blk_run_queue(current_sdev->request_queue);
488 
489 	spin_lock_irqsave(shost->host_lock, flags);
490 	if (starget->starget_sdev_user)
491 		goto out;
492 	list_for_each_entry_safe(sdev, tmp, &starget->devices,
493 			same_target_siblings) {
494 		if (sdev == current_sdev)
495 			continue;
496 		if (scsi_device_get(sdev))
497 			continue;
498 
499 		spin_unlock_irqrestore(shost->host_lock, flags);
500 		blk_run_queue(sdev->request_queue);
501 		spin_lock_irqsave(shost->host_lock, flags);
502 
503 		scsi_device_put(sdev);
504 	}
505  out:
506 	spin_unlock_irqrestore(shost->host_lock, flags);
507 }
508 
509 /*
510  * Function:	scsi_run_queue()
511  *
512  * Purpose:	Select a proper request queue to serve next
513  *
514  * Arguments:	q	- last request's queue
515  *
516  * Returns:     Nothing
517  *
518  * Notes:	The previous command was completely finished, start
519  *		a new one if possible.
520  */
521 static void scsi_run_queue(struct request_queue *q)
522 {
523 	struct scsi_device *sdev = q->queuedata;
524 	struct Scsi_Host *shost = sdev->host;
525 	unsigned long flags;
526 
527 	if (sdev->single_lun)
528 		scsi_single_lun_run(sdev);
529 
530 	spin_lock_irqsave(shost->host_lock, flags);
531 	while (!list_empty(&shost->starved_list) &&
532 	       !shost->host_blocked && !shost->host_self_blocked &&
533 		!((shost->can_queue > 0) &&
534 		  (shost->host_busy >= shost->can_queue))) {
535 		/*
536 		 * As long as shost is accepting commands and we have
537 		 * starved queues, call blk_run_queue. scsi_request_fn
538 		 * drops the queue_lock and can add us back to the
539 		 * starved_list.
540 		 *
541 		 * host_lock protects the starved_list and starved_entry.
542 		 * scsi_request_fn must get the host_lock before checking
543 		 * or modifying starved_list or starved_entry.
544 		 */
545 		sdev = list_entry(shost->starved_list.next,
546 					  struct scsi_device, starved_entry);
547 		list_del_init(&sdev->starved_entry);
548 		spin_unlock_irqrestore(shost->host_lock, flags);
549 
550 
551 		if (test_bit(QUEUE_FLAG_REENTER, &q->queue_flags) &&
552 		    !test_and_set_bit(QUEUE_FLAG_REENTER,
553 				      &sdev->request_queue->queue_flags)) {
554 			blk_run_queue(sdev->request_queue);
555 			clear_bit(QUEUE_FLAG_REENTER,
556 				  &sdev->request_queue->queue_flags);
557 		} else
558 			blk_run_queue(sdev->request_queue);
559 
560 		spin_lock_irqsave(shost->host_lock, flags);
561 		if (unlikely(!list_empty(&sdev->starved_entry)))
562 			/*
563 			 * sdev lost a race, and was put back on the
564 			 * starved list. This is unlikely but without this
565 			 * in theory we could loop forever.
566 			 */
567 			break;
568 	}
569 	spin_unlock_irqrestore(shost->host_lock, flags);
570 
571 	blk_run_queue(q);
572 }
573 
574 /*
575  * Function:	scsi_requeue_command()
576  *
577  * Purpose:	Handle post-processing of completed commands.
578  *
579  * Arguments:	q	- queue to operate on
580  *		cmd	- command that may need to be requeued.
581  *
582  * Returns:	Nothing
583  *
584  * Notes:	After command completion, there may be blocks left
585  *		over which weren't finished by the previous command
586  *		this can be for a number of reasons - the main one is
587  *		I/O errors in the middle of the request, in which case
588  *		we need to request the blocks that come after the bad
589  *		sector.
590  * Notes:	Upon return, cmd is a stale pointer.
591  */
592 static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
593 {
594 	struct request *req = cmd->request;
595 	unsigned long flags;
596 
597 	scsi_unprep_request(req);
598 	spin_lock_irqsave(q->queue_lock, flags);
599 	blk_requeue_request(q, req);
600 	spin_unlock_irqrestore(q->queue_lock, flags);
601 
602 	scsi_run_queue(q);
603 }
604 
605 void scsi_next_command(struct scsi_cmnd *cmd)
606 {
607 	struct scsi_device *sdev = cmd->device;
608 	struct request_queue *q = sdev->request_queue;
609 
610 	/* need to hold a reference on the device before we let go of the cmd */
611 	get_device(&sdev->sdev_gendev);
612 
613 	scsi_put_command(cmd);
614 	scsi_run_queue(q);
615 
616 	/* ok to remove device now */
617 	put_device(&sdev->sdev_gendev);
618 }
619 
620 void scsi_run_host_queues(struct Scsi_Host *shost)
621 {
622 	struct scsi_device *sdev;
623 
624 	shost_for_each_device(sdev, shost)
625 		scsi_run_queue(sdev->request_queue);
626 }
627 
628 /*
629  * Function:    scsi_end_request()
630  *
631  * Purpose:     Post-processing of completed commands (usually invoked at end
632  *		of upper level post-processing and scsi_io_completion).
633  *
634  * Arguments:   cmd	 - command that is complete.
635  *              uptodate - 1 if I/O indicates success, <= 0 for I/O error.
636  *              bytes    - number of bytes of completed I/O
637  *		requeue  - indicates whether we should requeue leftovers.
638  *
639  * Lock status: Assumed that lock is not held upon entry.
640  *
641  * Returns:     cmd if requeue required, NULL otherwise.
642  *
643  * Notes:       This is called for block device requests in order to
644  *              mark some number of sectors as complete.
645  *
646  *		We are guaranteeing that the request queue will be goosed
647  *		at some point during this call.
648  * Notes:	If cmd was requeued, upon return it will be a stale pointer.
649  */
650 static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
651 					  int bytes, int requeue)
652 {
653 	struct request_queue *q = cmd->device->request_queue;
654 	struct request *req = cmd->request;
655 	unsigned long flags;
656 
657 	/*
658 	 * If there are blocks left over at the end, set up the command
659 	 * to queue the remainder of them.
660 	 */
661 	if (end_that_request_chunk(req, uptodate, bytes)) {
662 		int leftover = (req->hard_nr_sectors << 9);
663 
664 		if (blk_pc_request(req))
665 			leftover = req->data_len;
666 
667 		/* kill remainder if no retrys */
668 		if (!uptodate && blk_noretry_request(req))
669 			end_that_request_chunk(req, 0, leftover);
670 		else {
671 			if (requeue) {
672 				/*
673 				 * Bleah.  Leftovers again.  Stick the
674 				 * leftovers in the front of the
675 				 * queue, and goose the queue again.
676 				 */
677 				scsi_requeue_command(q, cmd);
678 				cmd = NULL;
679 			}
680 			return cmd;
681 		}
682 	}
683 
684 	add_disk_randomness(req->rq_disk);
685 
686 	spin_lock_irqsave(q->queue_lock, flags);
687 	if (blk_rq_tagged(req))
688 		blk_queue_end_tag(q, req);
689 	end_that_request_last(req, uptodate);
690 	spin_unlock_irqrestore(q->queue_lock, flags);
691 
692 	/*
693 	 * This will goose the queue request function at the end, so we don't
694 	 * need to worry about launching another command.
695 	 */
696 	scsi_next_command(cmd);
697 	return NULL;
698 }
699 
700 /*
701  * Like SCSI_MAX_SG_SEGMENTS, but for archs that have sg chaining. This limit
702  * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
703  */
704 #define SCSI_MAX_SG_CHAIN_SEGMENTS	2048
705 
706 static inline unsigned int scsi_sgtable_index(unsigned short nents)
707 {
708 	unsigned int index;
709 
710 	switch (nents) {
711 	case 1 ... 8:
712 		index = 0;
713 		break;
714 	case 9 ... 16:
715 		index = 1;
716 		break;
717 #if (SCSI_MAX_SG_SEGMENTS > 16)
718 	case 17 ... 32:
719 		index = 2;
720 		break;
721 #if (SCSI_MAX_SG_SEGMENTS > 32)
722 	case 33 ... 64:
723 		index = 3;
724 		break;
725 #if (SCSI_MAX_SG_SEGMENTS > 64)
726 	case 65 ... 128:
727 		index = 4;
728 		break;
729 #endif
730 #endif
731 #endif
732 	default:
733 		printk(KERN_ERR "scsi: bad segment count=%d\n", nents);
734 		BUG();
735 	}
736 
737 	return index;
738 }
739 
740 struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask)
741 {
742 	struct scsi_host_sg_pool *sgp;
743 	struct scatterlist *sgl, *prev, *ret;
744 	unsigned int index;
745 	int this, left;
746 
747 	BUG_ON(!cmd->use_sg);
748 
749 	left = cmd->use_sg;
750 	ret = prev = NULL;
751 	do {
752 		this = left;
753 		if (this > SCSI_MAX_SG_SEGMENTS) {
754 			this = SCSI_MAX_SG_SEGMENTS - 1;
755 			index = SG_MEMPOOL_NR - 1;
756 		} else
757 			index = scsi_sgtable_index(this);
758 
759 		left -= this;
760 
761 		sgp = scsi_sg_pools + index;
762 
763 		sgl = mempool_alloc(sgp->pool, gfp_mask);
764 		if (unlikely(!sgl))
765 			goto enomem;
766 
767 		sg_init_table(sgl, sgp->size);
768 
769 		/*
770 		 * first loop through, set initial index and return value
771 		 */
772 		if (!ret)
773 			ret = sgl;
774 
775 		/*
776 		 * chain previous sglist, if any. we know the previous
777 		 * sglist must be the biggest one, or we would not have
778 		 * ended up doing another loop.
779 		 */
780 		if (prev)
781 			sg_chain(prev, SCSI_MAX_SG_SEGMENTS, sgl);
782 
783 		/*
784 		 * if we have nothing left, mark the last segment as
785 		 * end-of-list
786 		 */
787 		if (!left)
788 			sg_mark_end(&sgl[this - 1]);
789 
790 		/*
791 		 * don't allow subsequent mempool allocs to sleep, it would
792 		 * violate the mempool principle.
793 		 */
794 		gfp_mask &= ~__GFP_WAIT;
795 		gfp_mask |= __GFP_HIGH;
796 		prev = sgl;
797 	} while (left);
798 
799 	/*
800 	 * ->use_sg may get modified after dma mapping has potentially
801 	 * shrunk the number of segments, so keep a copy of it for free.
802 	 */
803 	cmd->__use_sg = cmd->use_sg;
804 	return ret;
805 enomem:
806 	if (ret) {
807 		/*
808 		 * Free entries chained off ret. Since we were trying to
809 		 * allocate another sglist, we know that all entries are of
810 		 * the max size.
811 		 */
812 		sgp = scsi_sg_pools + SG_MEMPOOL_NR - 1;
813 		prev = ret;
814 		ret = &ret[SCSI_MAX_SG_SEGMENTS - 1];
815 
816 		while ((sgl = sg_chain_ptr(ret)) != NULL) {
817 			ret = &sgl[SCSI_MAX_SG_SEGMENTS - 1];
818 			mempool_free(sgl, sgp->pool);
819 		}
820 
821 		mempool_free(prev, sgp->pool);
822 	}
823 	return NULL;
824 }
825 
826 EXPORT_SYMBOL(scsi_alloc_sgtable);
827 
828 void scsi_free_sgtable(struct scsi_cmnd *cmd)
829 {
830 	struct scatterlist *sgl = cmd->request_buffer;
831 	struct scsi_host_sg_pool *sgp;
832 
833 	/*
834 	 * if this is the biggest size sglist, check if we have
835 	 * chained parts we need to free
836 	 */
837 	if (cmd->__use_sg > SCSI_MAX_SG_SEGMENTS) {
838 		unsigned short this, left;
839 		struct scatterlist *next;
840 		unsigned int index;
841 
842 		left = cmd->__use_sg - (SCSI_MAX_SG_SEGMENTS - 1);
843 		next = sg_chain_ptr(&sgl[SCSI_MAX_SG_SEGMENTS - 1]);
844 		while (left && next) {
845 			sgl = next;
846 			this = left;
847 			if (this > SCSI_MAX_SG_SEGMENTS) {
848 				this = SCSI_MAX_SG_SEGMENTS - 1;
849 				index = SG_MEMPOOL_NR - 1;
850 			} else
851 				index = scsi_sgtable_index(this);
852 
853 			left -= this;
854 
855 			sgp = scsi_sg_pools + index;
856 
857 			if (left)
858 				next = sg_chain_ptr(&sgl[sgp->size - 1]);
859 
860 			mempool_free(sgl, sgp->pool);
861 		}
862 
863 		/*
864 		 * Restore original, will be freed below
865 		 */
866 		sgl = cmd->request_buffer;
867 		sgp = scsi_sg_pools + SG_MEMPOOL_NR - 1;
868 	} else
869 		sgp = scsi_sg_pools + scsi_sgtable_index(cmd->__use_sg);
870 
871 	mempool_free(sgl, sgp->pool);
872 }
873 
874 EXPORT_SYMBOL(scsi_free_sgtable);
875 
876 /*
877  * Function:    scsi_release_buffers()
878  *
879  * Purpose:     Completion processing for block device I/O requests.
880  *
881  * Arguments:   cmd	- command that we are bailing.
882  *
883  * Lock status: Assumed that no lock is held upon entry.
884  *
885  * Returns:     Nothing
886  *
887  * Notes:       In the event that an upper level driver rejects a
888  *		command, we must release resources allocated during
889  *		the __init_io() function.  Primarily this would involve
890  *		the scatter-gather table, and potentially any bounce
891  *		buffers.
892  */
893 static void scsi_release_buffers(struct scsi_cmnd *cmd)
894 {
895 	if (cmd->use_sg)
896 		scsi_free_sgtable(cmd);
897 
898 	/*
899 	 * Zero these out.  They now point to freed memory, and it is
900 	 * dangerous to hang onto the pointers.
901 	 */
902 	cmd->request_buffer = NULL;
903 	cmd->request_bufflen = 0;
904 }
905 
906 /*
907  * Function:    scsi_io_completion()
908  *
909  * Purpose:     Completion processing for block device I/O requests.
910  *
911  * Arguments:   cmd   - command that is finished.
912  *
913  * Lock status: Assumed that no lock is held upon entry.
914  *
915  * Returns:     Nothing
916  *
917  * Notes:       This function is matched in terms of capabilities to
918  *              the function that created the scatter-gather list.
919  *              In other words, if there are no bounce buffers
920  *              (the normal case for most drivers), we don't need
921  *              the logic to deal with cleaning up afterwards.
922  *
923  *		We must do one of several things here:
924  *
925  *		a) Call scsi_end_request.  This will finish off the
926  *		   specified number of sectors.  If we are done, the
927  *		   command block will be released, and the queue
928  *		   function will be goosed.  If we are not done, then
929  *		   scsi_end_request will directly goose the queue.
930  *
931  *		b) We can just use scsi_requeue_command() here.  This would
932  *		   be used if we just wanted to retry, for example.
933  */
934 void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
935 {
936 	int result = cmd->result;
937 	int this_count = cmd->request_bufflen;
938 	struct request_queue *q = cmd->device->request_queue;
939 	struct request *req = cmd->request;
940 	int clear_errors = 1;
941 	struct scsi_sense_hdr sshdr;
942 	int sense_valid = 0;
943 	int sense_deferred = 0;
944 
945 	scsi_release_buffers(cmd);
946 
947 	if (result) {
948 		sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
949 		if (sense_valid)
950 			sense_deferred = scsi_sense_is_deferred(&sshdr);
951 	}
952 
953 	if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
954 		req->errors = result;
955 		if (result) {
956 			clear_errors = 0;
957 			if (sense_valid && req->sense) {
958 				/*
959 				 * SG_IO wants current and deferred errors
960 				 */
961 				int len = 8 + cmd->sense_buffer[7];
962 
963 				if (len > SCSI_SENSE_BUFFERSIZE)
964 					len = SCSI_SENSE_BUFFERSIZE;
965 				memcpy(req->sense, cmd->sense_buffer,  len);
966 				req->sense_len = len;
967 			}
968 		}
969 		req->data_len = cmd->resid;
970 	}
971 
972 	/*
973 	 * Next deal with any sectors which we were able to correctly
974 	 * handle.
975 	 */
976 	SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, "
977 				      "%d bytes done.\n",
978 				      req->nr_sectors, good_bytes));
979 	SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n", cmd->use_sg));
980 
981 	if (clear_errors)
982 		req->errors = 0;
983 
984 	/* A number of bytes were successfully read.  If there
985 	 * are leftovers and there is some kind of error
986 	 * (result != 0), retry the rest.
987 	 */
988 	if (scsi_end_request(cmd, 1, good_bytes, result == 0) == NULL)
989 		return;
990 
991 	/* good_bytes = 0, or (inclusive) there were leftovers and
992 	 * result = 0, so scsi_end_request couldn't retry.
993 	 */
994 	if (sense_valid && !sense_deferred) {
995 		switch (sshdr.sense_key) {
996 		case UNIT_ATTENTION:
997 			if (cmd->device->removable) {
998 				/* Detected disc change.  Set a bit
999 				 * and quietly refuse further access.
1000 				 */
1001 				cmd->device->changed = 1;
1002 				scsi_end_request(cmd, 0, this_count, 1);
1003 				return;
1004 			} else {
1005 				/* Must have been a power glitch, or a
1006 				 * bus reset.  Could not have been a
1007 				 * media change, so we just retry the
1008 				 * request and see what happens.
1009 				 */
1010 				scsi_requeue_command(q, cmd);
1011 				return;
1012 			}
1013 			break;
1014 		case ILLEGAL_REQUEST:
1015 			/* If we had an ILLEGAL REQUEST returned, then
1016 			 * we may have performed an unsupported
1017 			 * command.  The only thing this should be
1018 			 * would be a ten byte read where only a six
1019 			 * byte read was supported.  Also, on a system
1020 			 * where READ CAPACITY failed, we may have
1021 			 * read past the end of the disk.
1022 			 */
1023 			if ((cmd->device->use_10_for_rw &&
1024 			    sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
1025 			    (cmd->cmnd[0] == READ_10 ||
1026 			     cmd->cmnd[0] == WRITE_10)) {
1027 				cmd->device->use_10_for_rw = 0;
1028 				/* This will cause a retry with a
1029 				 * 6-byte command.
1030 				 */
1031 				scsi_requeue_command(q, cmd);
1032 				return;
1033 			} else {
1034 				scsi_end_request(cmd, 0, this_count, 1);
1035 				return;
1036 			}
1037 			break;
1038 		case NOT_READY:
1039 			/* If the device is in the process of becoming
1040 			 * ready, or has a temporary blockage, retry.
1041 			 */
1042 			if (sshdr.asc == 0x04) {
1043 				switch (sshdr.ascq) {
1044 				case 0x01: /* becoming ready */
1045 				case 0x04: /* format in progress */
1046 				case 0x05: /* rebuild in progress */
1047 				case 0x06: /* recalculation in progress */
1048 				case 0x07: /* operation in progress */
1049 				case 0x08: /* Long write in progress */
1050 				case 0x09: /* self test in progress */
1051 					scsi_requeue_command(q, cmd);
1052 					return;
1053 				default:
1054 					break;
1055 				}
1056 			}
1057 			if (!(req->cmd_flags & REQ_QUIET))
1058 				scsi_cmd_print_sense_hdr(cmd,
1059 							 "Device not ready",
1060 							 &sshdr);
1061 
1062 			scsi_end_request(cmd, 0, this_count, 1);
1063 			return;
1064 		case VOLUME_OVERFLOW:
1065 			if (!(req->cmd_flags & REQ_QUIET)) {
1066 				scmd_printk(KERN_INFO, cmd,
1067 					    "Volume overflow, CDB: ");
1068 				__scsi_print_command(cmd->cmnd);
1069 				scsi_print_sense("", cmd);
1070 			}
1071 			/* See SSC3rXX or current. */
1072 			scsi_end_request(cmd, 0, this_count, 1);
1073 			return;
1074 		default:
1075 			break;
1076 		}
1077 	}
1078 	if (host_byte(result) == DID_RESET) {
1079 		/* Third party bus reset or reset for error recovery
1080 		 * reasons.  Just retry the request and see what
1081 		 * happens.
1082 		 */
1083 		scsi_requeue_command(q, cmd);
1084 		return;
1085 	}
1086 	if (result) {
1087 		if (!(req->cmd_flags & REQ_QUIET)) {
1088 			scsi_print_result(cmd);
1089 			if (driver_byte(result) & DRIVER_SENSE)
1090 				scsi_print_sense("", cmd);
1091 		}
1092 	}
1093 	scsi_end_request(cmd, 0, this_count, !result);
1094 }
1095 
1096 /*
1097  * Function:    scsi_init_io()
1098  *
1099  * Purpose:     SCSI I/O initialize function.
1100  *
1101  * Arguments:   cmd   - Command descriptor we wish to initialize
1102  *
1103  * Returns:     0 on success
1104  *		BLKPREP_DEFER if the failure is retryable
1105  *		BLKPREP_KILL if the failure is fatal
1106  */
1107 static int scsi_init_io(struct scsi_cmnd *cmd)
1108 {
1109 	struct request     *req = cmd->request;
1110 	int		   count;
1111 
1112 	/*
1113 	 * We used to not use scatter-gather for single segment request,
1114 	 * but now we do (it makes highmem I/O easier to support without
1115 	 * kmapping pages)
1116 	 */
1117 	cmd->use_sg = req->nr_phys_segments;
1118 
1119 	/*
1120 	 * If sg table allocation fails, requeue request later.
1121 	 */
1122 	cmd->request_buffer = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
1123 	if (unlikely(!cmd->request_buffer)) {
1124 		scsi_unprep_request(req);
1125 		return BLKPREP_DEFER;
1126 	}
1127 
1128 	req->buffer = NULL;
1129 	if (blk_pc_request(req))
1130 		cmd->request_bufflen = req->data_len;
1131 	else
1132 		cmd->request_bufflen = req->nr_sectors << 9;
1133 
1134 	/*
1135 	 * Next, walk the list, and fill in the addresses and sizes of
1136 	 * each segment.
1137 	 */
1138 	count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
1139 	if (likely(count <= cmd->use_sg)) {
1140 		cmd->use_sg = count;
1141 		return BLKPREP_OK;
1142 	}
1143 
1144 	printk(KERN_ERR "Incorrect number of segments after building list\n");
1145 	printk(KERN_ERR "counted %d, received %d\n", count, cmd->use_sg);
1146 	printk(KERN_ERR "req nr_sec %lu, cur_nr_sec %u\n", req->nr_sectors,
1147 			req->current_nr_sectors);
1148 
1149 	return BLKPREP_KILL;
1150 }
1151 
1152 static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
1153 		struct request *req)
1154 {
1155 	struct scsi_cmnd *cmd;
1156 
1157 	if (!req->special) {
1158 		cmd = scsi_get_command(sdev, GFP_ATOMIC);
1159 		if (unlikely(!cmd))
1160 			return NULL;
1161 		req->special = cmd;
1162 	} else {
1163 		cmd = req->special;
1164 	}
1165 
1166 	/* pull a tag out of the request if we have one */
1167 	cmd->tag = req->tag;
1168 	cmd->request = req;
1169 
1170 	return cmd;
1171 }
1172 
1173 int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
1174 {
1175 	struct scsi_cmnd *cmd;
1176 	int ret = scsi_prep_state_check(sdev, req);
1177 
1178 	if (ret != BLKPREP_OK)
1179 		return ret;
1180 
1181 	cmd = scsi_get_cmd_from_req(sdev, req);
1182 	if (unlikely(!cmd))
1183 		return BLKPREP_DEFER;
1184 
1185 	/*
1186 	 * BLOCK_PC requests may transfer data, in which case they must
1187 	 * a bio attached to them.  Or they might contain a SCSI command
1188 	 * that does not transfer data, in which case they may optionally
1189 	 * submit a request without an attached bio.
1190 	 */
1191 	if (req->bio) {
1192 		int ret;
1193 
1194 		BUG_ON(!req->nr_phys_segments);
1195 
1196 		ret = scsi_init_io(cmd);
1197 		if (unlikely(ret))
1198 			return ret;
1199 	} else {
1200 		BUG_ON(req->data_len);
1201 		BUG_ON(req->data);
1202 
1203 		cmd->request_bufflen = 0;
1204 		cmd->request_buffer = NULL;
1205 		cmd->use_sg = 0;
1206 		req->buffer = NULL;
1207 	}
1208 
1209 	BUILD_BUG_ON(sizeof(req->cmd) > sizeof(cmd->cmnd));
1210 	memcpy(cmd->cmnd, req->cmd, sizeof(cmd->cmnd));
1211 	cmd->cmd_len = req->cmd_len;
1212 	if (!req->data_len)
1213 		cmd->sc_data_direction = DMA_NONE;
1214 	else if (rq_data_dir(req) == WRITE)
1215 		cmd->sc_data_direction = DMA_TO_DEVICE;
1216 	else
1217 		cmd->sc_data_direction = DMA_FROM_DEVICE;
1218 
1219 	cmd->transfersize = req->data_len;
1220 	cmd->allowed = req->retries;
1221 	cmd->timeout_per_command = req->timeout;
1222 	return BLKPREP_OK;
1223 }
1224 EXPORT_SYMBOL(scsi_setup_blk_pc_cmnd);
1225 
1226 /*
1227  * Setup a REQ_TYPE_FS command.  These are simple read/write request
1228  * from filesystems that still need to be translated to SCSI CDBs from
1229  * the ULD.
1230  */
1231 int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
1232 {
1233 	struct scsi_cmnd *cmd;
1234 	int ret = scsi_prep_state_check(sdev, req);
1235 
1236 	if (ret != BLKPREP_OK)
1237 		return ret;
1238 	/*
1239 	 * Filesystem requests must transfer data.
1240 	 */
1241 	BUG_ON(!req->nr_phys_segments);
1242 
1243 	cmd = scsi_get_cmd_from_req(sdev, req);
1244 	if (unlikely(!cmd))
1245 		return BLKPREP_DEFER;
1246 
1247 	return scsi_init_io(cmd);
1248 }
1249 EXPORT_SYMBOL(scsi_setup_fs_cmnd);
1250 
1251 int scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
1252 {
1253 	int ret = BLKPREP_OK;
1254 
1255 	/*
1256 	 * If the device is not in running state we will reject some
1257 	 * or all commands.
1258 	 */
1259 	if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
1260 		switch (sdev->sdev_state) {
1261 		case SDEV_OFFLINE:
1262 			/*
1263 			 * If the device is offline we refuse to process any
1264 			 * commands.  The device must be brought online
1265 			 * before trying any recovery commands.
1266 			 */
1267 			sdev_printk(KERN_ERR, sdev,
1268 				    "rejecting I/O to offline device\n");
1269 			ret = BLKPREP_KILL;
1270 			break;
1271 		case SDEV_DEL:
1272 			/*
1273 			 * If the device is fully deleted, we refuse to
1274 			 * process any commands as well.
1275 			 */
1276 			sdev_printk(KERN_ERR, sdev,
1277 				    "rejecting I/O to dead device\n");
1278 			ret = BLKPREP_KILL;
1279 			break;
1280 		case SDEV_QUIESCE:
1281 		case SDEV_BLOCK:
1282 			/*
1283 			 * If the devices is blocked we defer normal commands.
1284 			 */
1285 			if (!(req->cmd_flags & REQ_PREEMPT))
1286 				ret = BLKPREP_DEFER;
1287 			break;
1288 		default:
1289 			/*
1290 			 * For any other not fully online state we only allow
1291 			 * special commands.  In particular any user initiated
1292 			 * command is not allowed.
1293 			 */
1294 			if (!(req->cmd_flags & REQ_PREEMPT))
1295 				ret = BLKPREP_KILL;
1296 			break;
1297 		}
1298 	}
1299 	return ret;
1300 }
1301 EXPORT_SYMBOL(scsi_prep_state_check);
1302 
1303 int scsi_prep_return(struct request_queue *q, struct request *req, int ret)
1304 {
1305 	struct scsi_device *sdev = q->queuedata;
1306 
1307 	switch (ret) {
1308 	case BLKPREP_KILL:
1309 		req->errors = DID_NO_CONNECT << 16;
1310 		/* release the command and kill it */
1311 		if (req->special) {
1312 			struct scsi_cmnd *cmd = req->special;
1313 			scsi_release_buffers(cmd);
1314 			scsi_put_command(cmd);
1315 			req->special = NULL;
1316 		}
1317 		break;
1318 	case BLKPREP_DEFER:
1319 		/*
1320 		 * If we defer, the elv_next_request() returns NULL, but the
1321 		 * queue must be restarted, so we plug here if no returning
1322 		 * command will automatically do that.
1323 		 */
1324 		if (sdev->device_busy == 0)
1325 			blk_plug_device(q);
1326 		break;
1327 	default:
1328 		req->cmd_flags |= REQ_DONTPREP;
1329 	}
1330 
1331 	return ret;
1332 }
1333 EXPORT_SYMBOL(scsi_prep_return);
1334 
1335 static int scsi_prep_fn(struct request_queue *q, struct request *req)
1336 {
1337 	struct scsi_device *sdev = q->queuedata;
1338 	int ret = BLKPREP_KILL;
1339 
1340 	if (req->cmd_type == REQ_TYPE_BLOCK_PC)
1341 		ret = scsi_setup_blk_pc_cmnd(sdev, req);
1342 	return scsi_prep_return(q, req, ret);
1343 }
1344 
1345 /*
1346  * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1347  * return 0.
1348  *
1349  * Called with the queue_lock held.
1350  */
1351 static inline int scsi_dev_queue_ready(struct request_queue *q,
1352 				  struct scsi_device *sdev)
1353 {
1354 	if (sdev->device_busy >= sdev->queue_depth)
1355 		return 0;
1356 	if (sdev->device_busy == 0 && sdev->device_blocked) {
1357 		/*
1358 		 * unblock after device_blocked iterates to zero
1359 		 */
1360 		if (--sdev->device_blocked == 0) {
1361 			SCSI_LOG_MLQUEUE(3,
1362 				   sdev_printk(KERN_INFO, sdev,
1363 				   "unblocking device at zero depth\n"));
1364 		} else {
1365 			blk_plug_device(q);
1366 			return 0;
1367 		}
1368 	}
1369 	if (sdev->device_blocked)
1370 		return 0;
1371 
1372 	return 1;
1373 }
1374 
1375 /*
1376  * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1377  * return 0. We must end up running the queue again whenever 0 is
1378  * returned, else IO can hang.
1379  *
1380  * Called with host_lock held.
1381  */
1382 static inline int scsi_host_queue_ready(struct request_queue *q,
1383 				   struct Scsi_Host *shost,
1384 				   struct scsi_device *sdev)
1385 {
1386 	if (scsi_host_in_recovery(shost))
1387 		return 0;
1388 	if (shost->host_busy == 0 && shost->host_blocked) {
1389 		/*
1390 		 * unblock after host_blocked iterates to zero
1391 		 */
1392 		if (--shost->host_blocked == 0) {
1393 			SCSI_LOG_MLQUEUE(3,
1394 				printk("scsi%d unblocking host at zero depth\n",
1395 					shost->host_no));
1396 		} else {
1397 			blk_plug_device(q);
1398 			return 0;
1399 		}
1400 	}
1401 	if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
1402 	    shost->host_blocked || shost->host_self_blocked) {
1403 		if (list_empty(&sdev->starved_entry))
1404 			list_add_tail(&sdev->starved_entry, &shost->starved_list);
1405 		return 0;
1406 	}
1407 
1408 	/* We're OK to process the command, so we can't be starved */
1409 	if (!list_empty(&sdev->starved_entry))
1410 		list_del_init(&sdev->starved_entry);
1411 
1412 	return 1;
1413 }
1414 
1415 /*
1416  * Kill a request for a dead device
1417  */
1418 static void scsi_kill_request(struct request *req, struct request_queue *q)
1419 {
1420 	struct scsi_cmnd *cmd = req->special;
1421 	struct scsi_device *sdev = cmd->device;
1422 	struct Scsi_Host *shost = sdev->host;
1423 
1424 	blkdev_dequeue_request(req);
1425 
1426 	if (unlikely(cmd == NULL)) {
1427 		printk(KERN_CRIT "impossible request in %s.\n",
1428 				 __FUNCTION__);
1429 		BUG();
1430 	}
1431 
1432 	scsi_init_cmd_errh(cmd);
1433 	cmd->result = DID_NO_CONNECT << 16;
1434 	atomic_inc(&cmd->device->iorequest_cnt);
1435 
1436 	/*
1437 	 * SCSI request completion path will do scsi_device_unbusy(),
1438 	 * bump busy counts.  To bump the counters, we need to dance
1439 	 * with the locks as normal issue path does.
1440 	 */
1441 	sdev->device_busy++;
1442 	spin_unlock(sdev->request_queue->queue_lock);
1443 	spin_lock(shost->host_lock);
1444 	shost->host_busy++;
1445 	spin_unlock(shost->host_lock);
1446 	spin_lock(sdev->request_queue->queue_lock);
1447 
1448 	__scsi_done(cmd);
1449 }
1450 
1451 static void scsi_softirq_done(struct request *rq)
1452 {
1453 	struct scsi_cmnd *cmd = rq->completion_data;
1454 	unsigned long wait_for = (cmd->allowed + 1) * cmd->timeout_per_command;
1455 	int disposition;
1456 
1457 	INIT_LIST_HEAD(&cmd->eh_entry);
1458 
1459 	disposition = scsi_decide_disposition(cmd);
1460 	if (disposition != SUCCESS &&
1461 	    time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1462 		sdev_printk(KERN_ERR, cmd->device,
1463 			    "timing out command, waited %lus\n",
1464 			    wait_for/HZ);
1465 		disposition = SUCCESS;
1466 	}
1467 
1468 	scsi_log_completion(cmd, disposition);
1469 
1470 	switch (disposition) {
1471 		case SUCCESS:
1472 			scsi_finish_command(cmd);
1473 			break;
1474 		case NEEDS_RETRY:
1475 			scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
1476 			break;
1477 		case ADD_TO_MLQUEUE:
1478 			scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1479 			break;
1480 		default:
1481 			if (!scsi_eh_scmd_add(cmd, 0))
1482 				scsi_finish_command(cmd);
1483 	}
1484 }
1485 
1486 /*
1487  * Function:    scsi_request_fn()
1488  *
1489  * Purpose:     Main strategy routine for SCSI.
1490  *
1491  * Arguments:   q       - Pointer to actual queue.
1492  *
1493  * Returns:     Nothing
1494  *
1495  * Lock status: IO request lock assumed to be held when called.
1496  */
1497 static void scsi_request_fn(struct request_queue *q)
1498 {
1499 	struct scsi_device *sdev = q->queuedata;
1500 	struct Scsi_Host *shost;
1501 	struct scsi_cmnd *cmd;
1502 	struct request *req;
1503 
1504 	if (!sdev) {
1505 		printk("scsi: killing requests for dead queue\n");
1506 		while ((req = elv_next_request(q)) != NULL)
1507 			scsi_kill_request(req, q);
1508 		return;
1509 	}
1510 
1511 	if(!get_device(&sdev->sdev_gendev))
1512 		/* We must be tearing the block queue down already */
1513 		return;
1514 
1515 	/*
1516 	 * To start with, we keep looping until the queue is empty, or until
1517 	 * the host is no longer able to accept any more requests.
1518 	 */
1519 	shost = sdev->host;
1520 	while (!blk_queue_plugged(q)) {
1521 		int rtn;
1522 		/*
1523 		 * get next queueable request.  We do this early to make sure
1524 		 * that the request is fully prepared even if we cannot
1525 		 * accept it.
1526 		 */
1527 		req = elv_next_request(q);
1528 		if (!req || !scsi_dev_queue_ready(q, sdev))
1529 			break;
1530 
1531 		if (unlikely(!scsi_device_online(sdev))) {
1532 			sdev_printk(KERN_ERR, sdev,
1533 				    "rejecting I/O to offline device\n");
1534 			scsi_kill_request(req, q);
1535 			continue;
1536 		}
1537 
1538 
1539 		/*
1540 		 * Remove the request from the request list.
1541 		 */
1542 		if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
1543 			blkdev_dequeue_request(req);
1544 		sdev->device_busy++;
1545 
1546 		spin_unlock(q->queue_lock);
1547 		cmd = req->special;
1548 		if (unlikely(cmd == NULL)) {
1549 			printk(KERN_CRIT "impossible request in %s.\n"
1550 					 "please mail a stack trace to "
1551 					 "linux-scsi@vger.kernel.org\n",
1552 					 __FUNCTION__);
1553 			blk_dump_rq_flags(req, "foo");
1554 			BUG();
1555 		}
1556 		spin_lock(shost->host_lock);
1557 
1558 		if (!scsi_host_queue_ready(q, shost, sdev))
1559 			goto not_ready;
1560 		if (sdev->single_lun) {
1561 			if (scsi_target(sdev)->starget_sdev_user &&
1562 			    scsi_target(sdev)->starget_sdev_user != sdev)
1563 				goto not_ready;
1564 			scsi_target(sdev)->starget_sdev_user = sdev;
1565 		}
1566 		shost->host_busy++;
1567 
1568 		/*
1569 		 * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
1570 		 *		take the lock again.
1571 		 */
1572 		spin_unlock_irq(shost->host_lock);
1573 
1574 		/*
1575 		 * Finally, initialize any error handling parameters, and set up
1576 		 * the timers for timeouts.
1577 		 */
1578 		scsi_init_cmd_errh(cmd);
1579 
1580 		/*
1581 		 * Dispatch the command to the low-level driver.
1582 		 */
1583 		rtn = scsi_dispatch_cmd(cmd);
1584 		spin_lock_irq(q->queue_lock);
1585 		if(rtn) {
1586 			/* we're refusing the command; because of
1587 			 * the way locks get dropped, we need to
1588 			 * check here if plugging is required */
1589 			if(sdev->device_busy == 0)
1590 				blk_plug_device(q);
1591 
1592 			break;
1593 		}
1594 	}
1595 
1596 	goto out;
1597 
1598  not_ready:
1599 	spin_unlock_irq(shost->host_lock);
1600 
1601 	/*
1602 	 * lock q, handle tag, requeue req, and decrement device_busy. We
1603 	 * must return with queue_lock held.
1604 	 *
1605 	 * Decrementing device_busy without checking it is OK, as all such
1606 	 * cases (host limits or settings) should run the queue at some
1607 	 * later time.
1608 	 */
1609 	spin_lock_irq(q->queue_lock);
1610 	blk_requeue_request(q, req);
1611 	sdev->device_busy--;
1612 	if(sdev->device_busy == 0)
1613 		blk_plug_device(q);
1614  out:
1615 	/* must be careful here...if we trigger the ->remove() function
1616 	 * we cannot be holding the q lock */
1617 	spin_unlock_irq(q->queue_lock);
1618 	put_device(&sdev->sdev_gendev);
1619 	spin_lock_irq(q->queue_lock);
1620 }
1621 
1622 u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
1623 {
1624 	struct device *host_dev;
1625 	u64 bounce_limit = 0xffffffff;
1626 
1627 	if (shost->unchecked_isa_dma)
1628 		return BLK_BOUNCE_ISA;
1629 	/*
1630 	 * Platforms with virtual-DMA translation
1631 	 * hardware have no practical limit.
1632 	 */
1633 	if (!PCI_DMA_BUS_IS_PHYS)
1634 		return BLK_BOUNCE_ANY;
1635 
1636 	host_dev = scsi_get_device(shost);
1637 	if (host_dev && host_dev->dma_mask)
1638 		bounce_limit = *host_dev->dma_mask;
1639 
1640 	return bounce_limit;
1641 }
1642 EXPORT_SYMBOL(scsi_calculate_bounce_limit);
1643 
1644 struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
1645 					 request_fn_proc *request_fn)
1646 {
1647 	struct request_queue *q;
1648 
1649 	q = blk_init_queue(request_fn, NULL);
1650 	if (!q)
1651 		return NULL;
1652 
1653 	/*
1654 	 * this limit is imposed by hardware restrictions
1655 	 */
1656 	blk_queue_max_hw_segments(q, shost->sg_tablesize);
1657 
1658 	/*
1659 	 * In the future, sg chaining support will be mandatory and this
1660 	 * ifdef can then go away. Right now we don't have all archs
1661 	 * converted, so better keep it safe.
1662 	 */
1663 #ifdef ARCH_HAS_SG_CHAIN
1664 	if (shost->use_sg_chaining)
1665 		blk_queue_max_phys_segments(q, SCSI_MAX_SG_CHAIN_SEGMENTS);
1666 	else
1667 		blk_queue_max_phys_segments(q, SCSI_MAX_SG_SEGMENTS);
1668 #else
1669 	blk_queue_max_phys_segments(q, SCSI_MAX_SG_SEGMENTS);
1670 #endif
1671 
1672 	blk_queue_max_sectors(q, shost->max_sectors);
1673 	blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
1674 	blk_queue_segment_boundary(q, shost->dma_boundary);
1675 
1676 	if (!shost->use_clustering)
1677 		clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
1678 	return q;
1679 }
1680 EXPORT_SYMBOL(__scsi_alloc_queue);
1681 
1682 struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
1683 {
1684 	struct request_queue *q;
1685 
1686 	q = __scsi_alloc_queue(sdev->host, scsi_request_fn);
1687 	if (!q)
1688 		return NULL;
1689 
1690 	blk_queue_prep_rq(q, scsi_prep_fn);
1691 	blk_queue_softirq_done(q, scsi_softirq_done);
1692 	return q;
1693 }
1694 
1695 void scsi_free_queue(struct request_queue *q)
1696 {
1697 	blk_cleanup_queue(q);
1698 }
1699 
1700 /*
1701  * Function:    scsi_block_requests()
1702  *
1703  * Purpose:     Utility function used by low-level drivers to prevent further
1704  *		commands from being queued to the device.
1705  *
1706  * Arguments:   shost       - Host in question
1707  *
1708  * Returns:     Nothing
1709  *
1710  * Lock status: No locks are assumed held.
1711  *
1712  * Notes:       There is no timer nor any other means by which the requests
1713  *		get unblocked other than the low-level driver calling
1714  *		scsi_unblock_requests().
1715  */
1716 void scsi_block_requests(struct Scsi_Host *shost)
1717 {
1718 	shost->host_self_blocked = 1;
1719 }
1720 EXPORT_SYMBOL(scsi_block_requests);
1721 
1722 /*
1723  * Function:    scsi_unblock_requests()
1724  *
1725  * Purpose:     Utility function used by low-level drivers to allow further
1726  *		commands from being queued to the device.
1727  *
1728  * Arguments:   shost       - Host in question
1729  *
1730  * Returns:     Nothing
1731  *
1732  * Lock status: No locks are assumed held.
1733  *
1734  * Notes:       There is no timer nor any other means by which the requests
1735  *		get unblocked other than the low-level driver calling
1736  *		scsi_unblock_requests().
1737  *
1738  *		This is done as an API function so that changes to the
1739  *		internals of the scsi mid-layer won't require wholesale
1740  *		changes to drivers that use this feature.
1741  */
1742 void scsi_unblock_requests(struct Scsi_Host *shost)
1743 {
1744 	shost->host_self_blocked = 0;
1745 	scsi_run_host_queues(shost);
1746 }
1747 EXPORT_SYMBOL(scsi_unblock_requests);
1748 
1749 int __init scsi_init_queue(void)
1750 {
1751 	int i;
1752 
1753 	scsi_io_context_cache = kmem_cache_create("scsi_io_context",
1754 					sizeof(struct scsi_io_context),
1755 					0, 0, NULL);
1756 	if (!scsi_io_context_cache) {
1757 		printk(KERN_ERR "SCSI: can't init scsi io context cache\n");
1758 		return -ENOMEM;
1759 	}
1760 
1761 	for (i = 0; i < SG_MEMPOOL_NR; i++) {
1762 		struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1763 		int size = sgp->size * sizeof(struct scatterlist);
1764 
1765 		sgp->slab = kmem_cache_create(sgp->name, size, 0,
1766 				SLAB_HWCACHE_ALIGN, NULL);
1767 		if (!sgp->slab) {
1768 			printk(KERN_ERR "SCSI: can't init sg slab %s\n",
1769 					sgp->name);
1770 		}
1771 
1772 		sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
1773 						     sgp->slab);
1774 		if (!sgp->pool) {
1775 			printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
1776 					sgp->name);
1777 		}
1778 	}
1779 
1780 	return 0;
1781 }
1782 
1783 void scsi_exit_queue(void)
1784 {
1785 	int i;
1786 
1787 	kmem_cache_destroy(scsi_io_context_cache);
1788 
1789 	for (i = 0; i < SG_MEMPOOL_NR; i++) {
1790 		struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1791 		mempool_destroy(sgp->pool);
1792 		kmem_cache_destroy(sgp->slab);
1793 	}
1794 }
1795 
1796 /**
1797  *	scsi_mode_select - issue a mode select
1798  *	@sdev:	SCSI device to be queried
1799  *	@pf:	Page format bit (1 == standard, 0 == vendor specific)
1800  *	@sp:	Save page bit (0 == don't save, 1 == save)
1801  *	@modepage: mode page being requested
1802  *	@buffer: request buffer (may not be smaller than eight bytes)
1803  *	@len:	length of request buffer.
1804  *	@timeout: command timeout
1805  *	@retries: number of retries before failing
1806  *	@data: returns a structure abstracting the mode header data
1807  *	@sense: place to put sense data (or NULL if no sense to be collected).
1808  *		must be SCSI_SENSE_BUFFERSIZE big.
1809  *
1810  *	Returns zero if successful; negative error number or scsi
1811  *	status on error
1812  *
1813  */
1814 int
1815 scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
1816 		 unsigned char *buffer, int len, int timeout, int retries,
1817 		 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
1818 {
1819 	unsigned char cmd[10];
1820 	unsigned char *real_buffer;
1821 	int ret;
1822 
1823 	memset(cmd, 0, sizeof(cmd));
1824 	cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
1825 
1826 	if (sdev->use_10_for_ms) {
1827 		if (len > 65535)
1828 			return -EINVAL;
1829 		real_buffer = kmalloc(8 + len, GFP_KERNEL);
1830 		if (!real_buffer)
1831 			return -ENOMEM;
1832 		memcpy(real_buffer + 8, buffer, len);
1833 		len += 8;
1834 		real_buffer[0] = 0;
1835 		real_buffer[1] = 0;
1836 		real_buffer[2] = data->medium_type;
1837 		real_buffer[3] = data->device_specific;
1838 		real_buffer[4] = data->longlba ? 0x01 : 0;
1839 		real_buffer[5] = 0;
1840 		real_buffer[6] = data->block_descriptor_length >> 8;
1841 		real_buffer[7] = data->block_descriptor_length;
1842 
1843 		cmd[0] = MODE_SELECT_10;
1844 		cmd[7] = len >> 8;
1845 		cmd[8] = len;
1846 	} else {
1847 		if (len > 255 || data->block_descriptor_length > 255 ||
1848 		    data->longlba)
1849 			return -EINVAL;
1850 
1851 		real_buffer = kmalloc(4 + len, GFP_KERNEL);
1852 		if (!real_buffer)
1853 			return -ENOMEM;
1854 		memcpy(real_buffer + 4, buffer, len);
1855 		len += 4;
1856 		real_buffer[0] = 0;
1857 		real_buffer[1] = data->medium_type;
1858 		real_buffer[2] = data->device_specific;
1859 		real_buffer[3] = data->block_descriptor_length;
1860 
1861 
1862 		cmd[0] = MODE_SELECT;
1863 		cmd[4] = len;
1864 	}
1865 
1866 	ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
1867 			       sshdr, timeout, retries);
1868 	kfree(real_buffer);
1869 	return ret;
1870 }
1871 EXPORT_SYMBOL_GPL(scsi_mode_select);
1872 
1873 /**
1874  *	scsi_mode_sense - issue a mode sense, falling back from 10 to
1875  *		six bytes if necessary.
1876  *	@sdev:	SCSI device to be queried
1877  *	@dbd:	set if mode sense will allow block descriptors to be returned
1878  *	@modepage: mode page being requested
1879  *	@buffer: request buffer (may not be smaller than eight bytes)
1880  *	@len:	length of request buffer.
1881  *	@timeout: command timeout
1882  *	@retries: number of retries before failing
1883  *	@data: returns a structure abstracting the mode header data
1884  *	@sense: place to put sense data (or NULL if no sense to be collected).
1885  *		must be SCSI_SENSE_BUFFERSIZE big.
1886  *
1887  *	Returns zero if unsuccessful, or the header offset (either 4
1888  *	or 8 depending on whether a six or ten byte command was
1889  *	issued) if successful.
1890  **/
1891 int
1892 scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
1893 		  unsigned char *buffer, int len, int timeout, int retries,
1894 		  struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
1895 {
1896 	unsigned char cmd[12];
1897 	int use_10_for_ms;
1898 	int header_length;
1899 	int result;
1900 	struct scsi_sense_hdr my_sshdr;
1901 
1902 	memset(data, 0, sizeof(*data));
1903 	memset(&cmd[0], 0, 12);
1904 	cmd[1] = dbd & 0x18;	/* allows DBD and LLBA bits */
1905 	cmd[2] = modepage;
1906 
1907 	/* caller might not be interested in sense, but we need it */
1908 	if (!sshdr)
1909 		sshdr = &my_sshdr;
1910 
1911  retry:
1912 	use_10_for_ms = sdev->use_10_for_ms;
1913 
1914 	if (use_10_for_ms) {
1915 		if (len < 8)
1916 			len = 8;
1917 
1918 		cmd[0] = MODE_SENSE_10;
1919 		cmd[8] = len;
1920 		header_length = 8;
1921 	} else {
1922 		if (len < 4)
1923 			len = 4;
1924 
1925 		cmd[0] = MODE_SENSE;
1926 		cmd[4] = len;
1927 		header_length = 4;
1928 	}
1929 
1930 	memset(buffer, 0, len);
1931 
1932 	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
1933 				  sshdr, timeout, retries);
1934 
1935 	/* This code looks awful: what it's doing is making sure an
1936 	 * ILLEGAL REQUEST sense return identifies the actual command
1937 	 * byte as the problem.  MODE_SENSE commands can return
1938 	 * ILLEGAL REQUEST if the code page isn't supported */
1939 
1940 	if (use_10_for_ms && !scsi_status_is_good(result) &&
1941 	    (driver_byte(result) & DRIVER_SENSE)) {
1942 		if (scsi_sense_valid(sshdr)) {
1943 			if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
1944 			    (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
1945 				/*
1946 				 * Invalid command operation code
1947 				 */
1948 				sdev->use_10_for_ms = 0;
1949 				goto retry;
1950 			}
1951 		}
1952 	}
1953 
1954 	if(scsi_status_is_good(result)) {
1955 		if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
1956 			     (modepage == 6 || modepage == 8))) {
1957 			/* Initio breakage? */
1958 			header_length = 0;
1959 			data->length = 13;
1960 			data->medium_type = 0;
1961 			data->device_specific = 0;
1962 			data->longlba = 0;
1963 			data->block_descriptor_length = 0;
1964 		} else if(use_10_for_ms) {
1965 			data->length = buffer[0]*256 + buffer[1] + 2;
1966 			data->medium_type = buffer[2];
1967 			data->device_specific = buffer[3];
1968 			data->longlba = buffer[4] & 0x01;
1969 			data->block_descriptor_length = buffer[6]*256
1970 				+ buffer[7];
1971 		} else {
1972 			data->length = buffer[0] + 1;
1973 			data->medium_type = buffer[1];
1974 			data->device_specific = buffer[2];
1975 			data->block_descriptor_length = buffer[3];
1976 		}
1977 		data->header_length = header_length;
1978 	}
1979 
1980 	return result;
1981 }
1982 EXPORT_SYMBOL(scsi_mode_sense);
1983 
1984 int
1985 scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
1986 {
1987 	char cmd[] = {
1988 		TEST_UNIT_READY, 0, 0, 0, 0, 0,
1989 	};
1990 	struct scsi_sense_hdr sshdr;
1991 	int result;
1992 
1993 	result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, &sshdr,
1994 				  timeout, retries);
1995 
1996 	if ((driver_byte(result) & DRIVER_SENSE) && sdev->removable) {
1997 
1998 		if ((scsi_sense_valid(&sshdr)) &&
1999 		    ((sshdr.sense_key == UNIT_ATTENTION) ||
2000 		     (sshdr.sense_key == NOT_READY))) {
2001 			sdev->changed = 1;
2002 			result = 0;
2003 		}
2004 	}
2005 	return result;
2006 }
2007 EXPORT_SYMBOL(scsi_test_unit_ready);
2008 
2009 /**
2010  *	scsi_device_set_state - Take the given device through the device
2011  *		state model.
2012  *	@sdev:	scsi device to change the state of.
2013  *	@state:	state to change to.
2014  *
2015  *	Returns zero if unsuccessful or an error if the requested
2016  *	transition is illegal.
2017  **/
2018 int
2019 scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2020 {
2021 	enum scsi_device_state oldstate = sdev->sdev_state;
2022 
2023 	if (state == oldstate)
2024 		return 0;
2025 
2026 	switch (state) {
2027 	case SDEV_CREATED:
2028 		/* There are no legal states that come back to
2029 		 * created.  This is the manually initialised start
2030 		 * state */
2031 		goto illegal;
2032 
2033 	case SDEV_RUNNING:
2034 		switch (oldstate) {
2035 		case SDEV_CREATED:
2036 		case SDEV_OFFLINE:
2037 		case SDEV_QUIESCE:
2038 		case SDEV_BLOCK:
2039 			break;
2040 		default:
2041 			goto illegal;
2042 		}
2043 		break;
2044 
2045 	case SDEV_QUIESCE:
2046 		switch (oldstate) {
2047 		case SDEV_RUNNING:
2048 		case SDEV_OFFLINE:
2049 			break;
2050 		default:
2051 			goto illegal;
2052 		}
2053 		break;
2054 
2055 	case SDEV_OFFLINE:
2056 		switch (oldstate) {
2057 		case SDEV_CREATED:
2058 		case SDEV_RUNNING:
2059 		case SDEV_QUIESCE:
2060 		case SDEV_BLOCK:
2061 			break;
2062 		default:
2063 			goto illegal;
2064 		}
2065 		break;
2066 
2067 	case SDEV_BLOCK:
2068 		switch (oldstate) {
2069 		case SDEV_CREATED:
2070 		case SDEV_RUNNING:
2071 			break;
2072 		default:
2073 			goto illegal;
2074 		}
2075 		break;
2076 
2077 	case SDEV_CANCEL:
2078 		switch (oldstate) {
2079 		case SDEV_CREATED:
2080 		case SDEV_RUNNING:
2081 		case SDEV_QUIESCE:
2082 		case SDEV_OFFLINE:
2083 		case SDEV_BLOCK:
2084 			break;
2085 		default:
2086 			goto illegal;
2087 		}
2088 		break;
2089 
2090 	case SDEV_DEL:
2091 		switch (oldstate) {
2092 		case SDEV_CREATED:
2093 		case SDEV_RUNNING:
2094 		case SDEV_OFFLINE:
2095 		case SDEV_CANCEL:
2096 			break;
2097 		default:
2098 			goto illegal;
2099 		}
2100 		break;
2101 
2102 	}
2103 	sdev->sdev_state = state;
2104 	return 0;
2105 
2106  illegal:
2107 	SCSI_LOG_ERROR_RECOVERY(1,
2108 				sdev_printk(KERN_ERR, sdev,
2109 					    "Illegal state transition %s->%s\n",
2110 					    scsi_device_state_name(oldstate),
2111 					    scsi_device_state_name(state))
2112 				);
2113 	return -EINVAL;
2114 }
2115 EXPORT_SYMBOL(scsi_device_set_state);
2116 
2117 /**
2118  * 	sdev_evt_emit - emit a single SCSI device uevent
2119  *	@sdev: associated SCSI device
2120  *	@evt: event to emit
2121  *
2122  *	Send a single uevent (scsi_event) to the associated scsi_device.
2123  */
2124 static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2125 {
2126 	int idx = 0;
2127 	char *envp[3];
2128 
2129 	switch (evt->evt_type) {
2130 	case SDEV_EVT_MEDIA_CHANGE:
2131 		envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2132 		break;
2133 
2134 	default:
2135 		/* do nothing */
2136 		break;
2137 	}
2138 
2139 	envp[idx++] = NULL;
2140 
2141 	kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2142 }
2143 
2144 /**
2145  * 	sdev_evt_thread - send a uevent for each scsi event
2146  *	@work: work struct for scsi_device
2147  *
2148  *	Dispatch queued events to their associated scsi_device kobjects
2149  *	as uevents.
2150  */
2151 void scsi_evt_thread(struct work_struct *work)
2152 {
2153 	struct scsi_device *sdev;
2154 	LIST_HEAD(event_list);
2155 
2156 	sdev = container_of(work, struct scsi_device, event_work);
2157 
2158 	while (1) {
2159 		struct scsi_event *evt;
2160 		struct list_head *this, *tmp;
2161 		unsigned long flags;
2162 
2163 		spin_lock_irqsave(&sdev->list_lock, flags);
2164 		list_splice_init(&sdev->event_list, &event_list);
2165 		spin_unlock_irqrestore(&sdev->list_lock, flags);
2166 
2167 		if (list_empty(&event_list))
2168 			break;
2169 
2170 		list_for_each_safe(this, tmp, &event_list) {
2171 			evt = list_entry(this, struct scsi_event, node);
2172 			list_del(&evt->node);
2173 			scsi_evt_emit(sdev, evt);
2174 			kfree(evt);
2175 		}
2176 	}
2177 }
2178 
2179 /**
2180  * 	sdev_evt_send - send asserted event to uevent thread
2181  *	@sdev: scsi_device event occurred on
2182  *	@evt: event to send
2183  *
2184  *	Assert scsi device event asynchronously.
2185  */
2186 void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2187 {
2188 	unsigned long flags;
2189 
2190 	if (!test_bit(evt->evt_type, sdev->supported_events)) {
2191 		kfree(evt);
2192 		return;
2193 	}
2194 
2195 	spin_lock_irqsave(&sdev->list_lock, flags);
2196 	list_add_tail(&evt->node, &sdev->event_list);
2197 	schedule_work(&sdev->event_work);
2198 	spin_unlock_irqrestore(&sdev->list_lock, flags);
2199 }
2200 EXPORT_SYMBOL_GPL(sdev_evt_send);
2201 
2202 /**
2203  * 	sdev_evt_alloc - allocate a new scsi event
2204  *	@evt_type: type of event to allocate
2205  *	@gfpflags: GFP flags for allocation
2206  *
2207  *	Allocates and returns a new scsi_event.
2208  */
2209 struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2210 				  gfp_t gfpflags)
2211 {
2212 	struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2213 	if (!evt)
2214 		return NULL;
2215 
2216 	evt->evt_type = evt_type;
2217 	INIT_LIST_HEAD(&evt->node);
2218 
2219 	/* evt_type-specific initialization, if any */
2220 	switch (evt_type) {
2221 	case SDEV_EVT_MEDIA_CHANGE:
2222 	default:
2223 		/* do nothing */
2224 		break;
2225 	}
2226 
2227 	return evt;
2228 }
2229 EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2230 
2231 /**
2232  * 	sdev_evt_send_simple - send asserted event to uevent thread
2233  *	@sdev: scsi_device event occurred on
2234  *	@evt_type: type of event to send
2235  *	@gfpflags: GFP flags for allocation
2236  *
2237  *	Assert scsi device event asynchronously, given an event type.
2238  */
2239 void sdev_evt_send_simple(struct scsi_device *sdev,
2240 			  enum scsi_device_event evt_type, gfp_t gfpflags)
2241 {
2242 	struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2243 	if (!evt) {
2244 		sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2245 			    evt_type);
2246 		return;
2247 	}
2248 
2249 	sdev_evt_send(sdev, evt);
2250 }
2251 EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2252 
2253 /**
2254  *	scsi_device_quiesce - Block user issued commands.
2255  *	@sdev:	scsi device to quiesce.
2256  *
2257  *	This works by trying to transition to the SDEV_QUIESCE state
2258  *	(which must be a legal transition).  When the device is in this
2259  *	state, only special requests will be accepted, all others will
2260  *	be deferred.  Since special requests may also be requeued requests,
2261  *	a successful return doesn't guarantee the device will be
2262  *	totally quiescent.
2263  *
2264  *	Must be called with user context, may sleep.
2265  *
2266  *	Returns zero if unsuccessful or an error if not.
2267  **/
2268 int
2269 scsi_device_quiesce(struct scsi_device *sdev)
2270 {
2271 	int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2272 	if (err)
2273 		return err;
2274 
2275 	scsi_run_queue(sdev->request_queue);
2276 	while (sdev->device_busy) {
2277 		msleep_interruptible(200);
2278 		scsi_run_queue(sdev->request_queue);
2279 	}
2280 	return 0;
2281 }
2282 EXPORT_SYMBOL(scsi_device_quiesce);
2283 
2284 /**
2285  *	scsi_device_resume - Restart user issued commands to a quiesced device.
2286  *	@sdev:	scsi device to resume.
2287  *
2288  *	Moves the device from quiesced back to running and restarts the
2289  *	queues.
2290  *
2291  *	Must be called with user context, may sleep.
2292  **/
2293 void
2294 scsi_device_resume(struct scsi_device *sdev)
2295 {
2296 	if(scsi_device_set_state(sdev, SDEV_RUNNING))
2297 		return;
2298 	scsi_run_queue(sdev->request_queue);
2299 }
2300 EXPORT_SYMBOL(scsi_device_resume);
2301 
2302 static void
2303 device_quiesce_fn(struct scsi_device *sdev, void *data)
2304 {
2305 	scsi_device_quiesce(sdev);
2306 }
2307 
2308 void
2309 scsi_target_quiesce(struct scsi_target *starget)
2310 {
2311 	starget_for_each_device(starget, NULL, device_quiesce_fn);
2312 }
2313 EXPORT_SYMBOL(scsi_target_quiesce);
2314 
2315 static void
2316 device_resume_fn(struct scsi_device *sdev, void *data)
2317 {
2318 	scsi_device_resume(sdev);
2319 }
2320 
2321 void
2322 scsi_target_resume(struct scsi_target *starget)
2323 {
2324 	starget_for_each_device(starget, NULL, device_resume_fn);
2325 }
2326 EXPORT_SYMBOL(scsi_target_resume);
2327 
2328 /**
2329  * scsi_internal_device_block - internal function to put a device
2330  *				temporarily into the SDEV_BLOCK state
2331  * @sdev:	device to block
2332  *
2333  * Block request made by scsi lld's to temporarily stop all
2334  * scsi commands on the specified device.  Called from interrupt
2335  * or normal process context.
2336  *
2337  * Returns zero if successful or error if not
2338  *
2339  * Notes:
2340  *	This routine transitions the device to the SDEV_BLOCK state
2341  *	(which must be a legal transition).  When the device is in this
2342  *	state, all commands are deferred until the scsi lld reenables
2343  *	the device with scsi_device_unblock or device_block_tmo fires.
2344  *	This routine assumes the host_lock is held on entry.
2345  **/
2346 int
2347 scsi_internal_device_block(struct scsi_device *sdev)
2348 {
2349 	struct request_queue *q = sdev->request_queue;
2350 	unsigned long flags;
2351 	int err = 0;
2352 
2353 	err = scsi_device_set_state(sdev, SDEV_BLOCK);
2354 	if (err)
2355 		return err;
2356 
2357 	/*
2358 	 * The device has transitioned to SDEV_BLOCK.  Stop the
2359 	 * block layer from calling the midlayer with this device's
2360 	 * request queue.
2361 	 */
2362 	spin_lock_irqsave(q->queue_lock, flags);
2363 	blk_stop_queue(q);
2364 	spin_unlock_irqrestore(q->queue_lock, flags);
2365 
2366 	return 0;
2367 }
2368 EXPORT_SYMBOL_GPL(scsi_internal_device_block);
2369 
2370 /**
2371  * scsi_internal_device_unblock - resume a device after a block request
2372  * @sdev:	device to resume
2373  *
2374  * Called by scsi lld's or the midlayer to restart the device queue
2375  * for the previously suspended scsi device.  Called from interrupt or
2376  * normal process context.
2377  *
2378  * Returns zero if successful or error if not.
2379  *
2380  * Notes:
2381  *	This routine transitions the device to the SDEV_RUNNING state
2382  *	(which must be a legal transition) allowing the midlayer to
2383  *	goose the queue for this device.  This routine assumes the
2384  *	host_lock is held upon entry.
2385  **/
2386 int
2387 scsi_internal_device_unblock(struct scsi_device *sdev)
2388 {
2389 	struct request_queue *q = sdev->request_queue;
2390 	int err;
2391 	unsigned long flags;
2392 
2393 	/*
2394 	 * Try to transition the scsi device to SDEV_RUNNING
2395 	 * and goose the device queue if successful.
2396 	 */
2397 	err = scsi_device_set_state(sdev, SDEV_RUNNING);
2398 	if (err)
2399 		return err;
2400 
2401 	spin_lock_irqsave(q->queue_lock, flags);
2402 	blk_start_queue(q);
2403 	spin_unlock_irqrestore(q->queue_lock, flags);
2404 
2405 	return 0;
2406 }
2407 EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
2408 
2409 static void
2410 device_block(struct scsi_device *sdev, void *data)
2411 {
2412 	scsi_internal_device_block(sdev);
2413 }
2414 
2415 static int
2416 target_block(struct device *dev, void *data)
2417 {
2418 	if (scsi_is_target_device(dev))
2419 		starget_for_each_device(to_scsi_target(dev), NULL,
2420 					device_block);
2421 	return 0;
2422 }
2423 
2424 void
2425 scsi_target_block(struct device *dev)
2426 {
2427 	if (scsi_is_target_device(dev))
2428 		starget_for_each_device(to_scsi_target(dev), NULL,
2429 					device_block);
2430 	else
2431 		device_for_each_child(dev, NULL, target_block);
2432 }
2433 EXPORT_SYMBOL_GPL(scsi_target_block);
2434 
2435 static void
2436 device_unblock(struct scsi_device *sdev, void *data)
2437 {
2438 	scsi_internal_device_unblock(sdev);
2439 }
2440 
2441 static int
2442 target_unblock(struct device *dev, void *data)
2443 {
2444 	if (scsi_is_target_device(dev))
2445 		starget_for_each_device(to_scsi_target(dev), NULL,
2446 					device_unblock);
2447 	return 0;
2448 }
2449 
2450 void
2451 scsi_target_unblock(struct device *dev)
2452 {
2453 	if (scsi_is_target_device(dev))
2454 		starget_for_each_device(to_scsi_target(dev), NULL,
2455 					device_unblock);
2456 	else
2457 		device_for_each_child(dev, NULL, target_unblock);
2458 }
2459 EXPORT_SYMBOL_GPL(scsi_target_unblock);
2460 
2461 /**
2462  * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
2463  * @sg:		scatter-gather list
2464  * @sg_count:	number of segments in sg
2465  * @offset:	offset in bytes into sg, on return offset into the mapped area
2466  * @len:	bytes to map, on return number of bytes mapped
2467  *
2468  * Returns virtual address of the start of the mapped page
2469  */
2470 void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
2471 			  size_t *offset, size_t *len)
2472 {
2473 	int i;
2474 	size_t sg_len = 0, len_complete = 0;
2475 	struct scatterlist *sg;
2476 	struct page *page;
2477 
2478 	WARN_ON(!irqs_disabled());
2479 
2480 	for_each_sg(sgl, sg, sg_count, i) {
2481 		len_complete = sg_len; /* Complete sg-entries */
2482 		sg_len += sg->length;
2483 		if (sg_len > *offset)
2484 			break;
2485 	}
2486 
2487 	if (unlikely(i == sg_count)) {
2488 		printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
2489 			"elements %d\n",
2490 		       __FUNCTION__, sg_len, *offset, sg_count);
2491 		WARN_ON(1);
2492 		return NULL;
2493 	}
2494 
2495 	/* Offset starting from the beginning of first page in this sg-entry */
2496 	*offset = *offset - len_complete + sg->offset;
2497 
2498 	/* Assumption: contiguous pages can be accessed as "page + i" */
2499 	page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
2500 	*offset &= ~PAGE_MASK;
2501 
2502 	/* Bytes in this sg-entry from *offset to the end of the page */
2503 	sg_len = PAGE_SIZE - *offset;
2504 	if (*len > sg_len)
2505 		*len = sg_len;
2506 
2507 	return kmap_atomic(page, KM_BIO_SRC_IRQ);
2508 }
2509 EXPORT_SYMBOL(scsi_kmap_atomic_sg);
2510 
2511 /**
2512  * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously
2513  *			   mapped with scsi_kmap_atomic_sg
2514  * @virt:	virtual address to be unmapped
2515  */
2516 void scsi_kunmap_atomic_sg(void *virt)
2517 {
2518 	kunmap_atomic(virt, KM_BIO_SRC_IRQ);
2519 }
2520 EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
2521