2 * SCSI target lib functions
4 * Copyright (C) 2005 Mike Christie <michaelc@cs.wisc.edu>
5 * Copyright (C) 2005 FUJITA Tomonori <tomof@acm.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 #include <linux/blkdev.h>
23 #include <linux/hash.h>
24 #include <linux/module.h>
25 #include <linux/pagemap.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_tgt.h>
31 #include <../drivers/md/dm-bio-list.h>
33 #include "scsi_tgt_priv.h"
35 static struct workqueue_struct *scsi_tgtd;
36 static kmem_cache_t *scsi_tgt_cmd_cache;
39 * TODO: this struct will be killed when the block layer supports large bios
40 * and James's work struct code is in
43 /* TODO replace work with James b's code */
44 struct work_struct work;
45 /* TODO replace the lists with a large bio */
46 struct bio_list xfer_done_list;
47 struct bio_list xfer_list;
49 struct list_head hash_list;
57 #define TGT_HASH_ORDER 4
58 #define cmd_hashfn(tag) hash_long((unsigned long) (tag), TGT_HASH_ORDER)
60 struct scsi_tgt_queuedata {
61 struct Scsi_Host *shost;
62 struct list_head cmd_hash[1 << TGT_HASH_ORDER];
63 spinlock_t cmd_hash_lock;
67 * Function: scsi_host_get_command()
69 * Purpose: Allocate and setup a scsi command block and blk request
71 * Arguments: shost - scsi host
72 * data_dir - dma data dir
73 * gfp_mask- allocator flags
75 * Returns: The allocated scsi command structure.
77 * This should be called by target LLDs to get a command.
79 struct scsi_cmnd *scsi_host_get_command(struct Scsi_Host *shost,
80 enum dma_data_direction data_dir,
83 int write = (data_dir == DMA_TO_DEVICE);
85 struct scsi_cmnd *cmd;
86 struct scsi_tgt_cmd *tcmd;
88 /* Bail if we can't get a reference to the device */
89 if (!get_device(&shost->shost_gendev))
92 tcmd = kmem_cache_alloc(scsi_tgt_cmd_cache, GFP_ATOMIC);
96 rq = blk_get_request(shost->uspace_req_q, write, gfp_mask);
100 cmd = __scsi_get_command(shost, gfp_mask);
104 memset(cmd, 0, sizeof(*cmd));
105 cmd->sc_data_direction = data_dir;
106 cmd->jiffies_at_alloc = jiffies;
110 rq->cmd_type = REQ_TYPE_SPECIAL;
111 rq->cmd_flags |= REQ_TYPE_BLOCK_PC;
112 rq->end_io_data = tcmd;
114 bio_list_init(&tcmd->xfer_list);
115 bio_list_init(&tcmd->xfer_done_list);
123 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
125 put_device(&shost->shost_gendev);
129 EXPORT_SYMBOL_GPL(scsi_host_get_command);
132 * Function: scsi_host_put_command()
134 * Purpose: Free a scsi command block
136 * Arguments: shost - scsi host
137 * cmd - command block to free
141 * Notes: The command must not belong to any lists.
143 void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
145 struct request_queue *q = shost->uspace_req_q;
146 struct request *rq = cmd->request;
147 struct scsi_tgt_cmd *tcmd = rq->end_io_data;
150 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
152 spin_lock_irqsave(q->queue_lock, flags);
153 __blk_put_request(q, rq);
154 spin_unlock_irqrestore(q->queue_lock, flags);
156 __scsi_put_command(shost, cmd, &shost->shost_gendev);
158 EXPORT_SYMBOL_GPL(scsi_host_put_command);
160 static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd)
164 /* must call bio_endio in case bio was bounced */
165 while ((bio = bio_list_pop(&tcmd->xfer_done_list))) {
166 bio_endio(bio, bio->bi_size, 0);
170 while ((bio = bio_list_pop(&tcmd->xfer_list))) {
171 bio_endio(bio, bio->bi_size, 0);
176 static void cmd_hashlist_del(struct scsi_cmnd *cmd)
178 struct request_queue *q = cmd->request->q;
179 struct scsi_tgt_queuedata *qdata = q->queuedata;
181 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
183 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
184 list_del(&tcmd->hash_list);
185 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
188 static void scsi_tgt_cmd_destroy(void *data)
190 struct scsi_cmnd *cmd = data;
191 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
193 dprintk("cmd %p %d %lu\n", cmd, cmd->sc_data_direction,
194 rq_data_dir(cmd->request));
196 * We fix rq->cmd_flags here since when we told bio_map_user
197 * to write vm for WRITE commands, blk_rq_bio_prep set
198 * rq_data_dir the flags to READ.
200 if (cmd->sc_data_direction == DMA_TO_DEVICE)
201 cmd->request->cmd_flags |= REQ_RW;
203 cmd->request->cmd_flags &= ~REQ_RW;
205 scsi_unmap_user_pages(tcmd);
206 scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd);
209 static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd,
212 struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
214 struct list_head *head;
217 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
218 head = &qdata->cmd_hash[cmd_hashfn(tag)];
219 list_add(&tcmd->hash_list, head);
220 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
224 * scsi_tgt_alloc_queue - setup queue used for message passing
227 * This should be called by the LLD after host allocation.
228 * And will be released when the host is released.
230 int scsi_tgt_alloc_queue(struct Scsi_Host *shost)
232 struct scsi_tgt_queuedata *queuedata;
233 struct request_queue *q;
237 * Do we need to send a netlink event or should uspace
238 * just respond to the hotplug event?
240 q = __scsi_alloc_queue(shost, NULL);
244 queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL);
249 queuedata->shost = shost;
250 q->queuedata = queuedata;
253 * this is a silly hack. We should probably just queue as many
254 * command as is recvd to userspace. uspace can then make
255 * sure we do not overload the HBA
257 q->nr_requests = shost->hostt->can_queue;
259 * We currently only support software LLDs so this does
260 * not matter for now. Do we need this for the cards we support?
261 * If so we should make it a host template value.
263 blk_queue_dma_alignment(q, 0);
264 shost->uspace_req_q = q;
266 for (i = 0; i < ARRAY_SIZE(queuedata->cmd_hash); i++)
267 INIT_LIST_HEAD(&queuedata->cmd_hash[i]);
268 spin_lock_init(&queuedata->cmd_hash_lock);
273 blk_cleanup_queue(q);
276 EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue);
278 void scsi_tgt_free_queue(struct Scsi_Host *shost)
282 struct request_queue *q = shost->uspace_req_q;
283 struct scsi_cmnd *cmd;
284 struct scsi_tgt_queuedata *qdata = q->queuedata;
285 struct scsi_tgt_cmd *tcmd, *n;
288 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
290 for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) {
291 list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i],
293 list_del(&tcmd->hash_list);
294 list_add(&tcmd->hash_list, &cmds);
298 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
300 while (!list_empty(&cmds)) {
301 tcmd = list_entry(cmds.next, struct scsi_tgt_cmd, hash_list);
302 list_del(&tcmd->hash_list);
303 cmd = tcmd->rq->special;
305 shost->hostt->eh_abort_handler(cmd);
306 scsi_tgt_cmd_destroy(cmd);
309 EXPORT_SYMBOL_GPL(scsi_tgt_free_queue);
311 struct Scsi_Host *scsi_tgt_cmd_to_host(struct scsi_cmnd *cmd)
313 struct scsi_tgt_queuedata *queue = cmd->request->q->queuedata;
316 EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host);
319 * scsi_tgt_queue_command - queue command for userspace processing
322 * @tag: unique value to identify this command for tmf
324 int scsi_tgt_queue_command(struct scsi_cmnd *cmd, struct scsi_lun *scsilun,
327 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
330 init_scsi_tgt_cmd(cmd->request, tcmd, tag);
331 err = scsi_tgt_uspace_send_cmd(cmd, scsilun, tag);
333 cmd_hashlist_del(cmd);
337 EXPORT_SYMBOL_GPL(scsi_tgt_queue_command);
340 * This is run from a interrpt handler normally and the unmap
341 * needs process context so we must queue
343 static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)
345 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
347 dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
349 scsi_tgt_uspace_send_status(cmd, tcmd->tag);
350 INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy, cmd);
351 queue_work(scsi_tgtd, &tcmd->work);
354 static int __scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
356 struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
359 dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
361 err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done);
363 case SCSI_MLQUEUE_HOST_BUSY:
364 case SCSI_MLQUEUE_DEVICE_BUSY:
371 static void scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
373 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
376 err = __scsi_tgt_transfer_response(cmd);
380 cmd->result = DID_BUS_BUSY << 16;
381 err = scsi_tgt_uspace_send_status(cmd, tcmd->tag);
383 /* the eh will have to pick this up */
384 printk(KERN_ERR "Could not send cmd %p status\n", cmd);
387 static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask)
389 struct request *rq = cmd->request;
390 struct scsi_tgt_cmd *tcmd = rq->end_io_data;
393 cmd->use_sg = rq->nr_phys_segments;
394 cmd->request_buffer = scsi_alloc_sgtable(cmd, gfp_mask);
395 if (!cmd->request_buffer)
398 cmd->request_bufflen = rq->data_len;
400 dprintk("cmd %p addr %p cnt %d %lu\n", cmd, tcmd->buffer, cmd->use_sg,
402 count = blk_rq_map_sg(rq->q, rq, cmd->request_buffer);
403 if (likely(count <= cmd->use_sg)) {
408 eprintk("cmd %p addr %p cnt %d\n", cmd, tcmd->buffer, cmd->use_sg);
409 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
413 /* TODO: test this crap and replace bio_map_user with new interface maybe */
414 static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
417 struct request_queue *q = cmd->request->q;
418 struct request *rq = cmd->request;
419 void *uaddr = tcmd->buffer;
420 unsigned int len = tcmd->bufflen;
425 dprintk("%lx %u\n", (unsigned long) uaddr, len);
426 bio = bio_map_user(q, NULL, (unsigned long) uaddr, len, rw);
429 dprintk("fail to map %lx %u %d %x\n",
430 (unsigned long) uaddr, len, err, cmd->cmnd[0]);
434 uaddr += bio->bi_size;
438 * The first bio is added and merged. We could probably
439 * try to add others using scsi_merge_bio() but for now
440 * we keep it simple. The first bio should be pretty large
441 * (either hitting the 1 MB bio pages limit or a queue limit)
442 * already but for really large IO we may want to try and
446 blk_rq_bio_prep(q, rq, bio);
447 rq->data_len = bio->bi_size;
449 /* put list of bios to transfer in next go around */
450 bio_list_add(&tcmd->xfer_list, bio);
454 err = scsi_tgt_init_cmd(cmd, GFP_KERNEL);
462 bio_unmap_user(rq->bio);
463 while ((bio = bio_list_pop(&tcmd->xfer_list)))
470 static int scsi_tgt_transfer_data(struct scsi_cmnd *);
472 static void scsi_tgt_data_transfer_done(struct scsi_cmnd *cmd)
474 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
478 /* should we free resources here on error ? */
481 err = scsi_tgt_uspace_send_status(cmd, tcmd->tag);
483 /* the tgt uspace eh will have to pick this up */
484 printk(KERN_ERR "Could not send cmd %p status\n", cmd);
488 dprintk("cmd %p request_bufflen %u bufflen %u\n",
489 cmd, cmd->request_bufflen, tcmd->bufflen);
491 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
492 bio_list_add(&tcmd->xfer_done_list, cmd->request->bio);
494 tcmd->buffer += cmd->request_bufflen;
495 cmd->offset += cmd->request_bufflen;
497 if (!tcmd->xfer_list.head) {
498 scsi_tgt_transfer_response(cmd);
502 dprintk("cmd2 %p request_bufflen %u bufflen %u\n",
503 cmd, cmd->request_bufflen, tcmd->bufflen);
505 bio = bio_list_pop(&tcmd->xfer_list);
508 blk_rq_bio_prep(cmd->request->q, cmd->request, bio);
509 cmd->request->data_len = bio->bi_size;
510 err = scsi_tgt_init_cmd(cmd, GFP_ATOMIC);
512 cmd->result = DID_ERROR << 16;
513 goto send_uspace_err;
516 if (scsi_tgt_transfer_data(cmd)) {
517 cmd->result = DID_NO_CONNECT << 16;
518 goto send_uspace_err;
522 static int scsi_tgt_transfer_data(struct scsi_cmnd *cmd)
525 struct Scsi_Host *host = scsi_tgt_cmd_to_host(cmd);
527 err = host->hostt->transfer_data(cmd, scsi_tgt_data_transfer_done);
529 case SCSI_MLQUEUE_HOST_BUSY:
530 case SCSI_MLQUEUE_DEVICE_BUSY:
537 static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
540 char __user *p = (char __user *) uaddr;
542 if (copy_from_user(cmd->sense_buffer, p,
543 min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) {
544 printk(KERN_ERR "Could not copy the sense buffer\n");
550 static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
554 err = shost->hostt->eh_abort_handler(cmd);
556 eprintk("fail to abort %p\n", cmd);
558 scsi_tgt_cmd_destroy(cmd);
562 static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
564 struct scsi_tgt_queuedata *qdata = q->queuedata;
565 struct request *rq = NULL;
566 struct list_head *head;
567 struct scsi_tgt_cmd *tcmd;
570 head = &qdata->cmd_hash[cmd_hashfn(tag)];
571 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
572 list_for_each_entry(tcmd, head, hash_list) {
573 if (tcmd->tag == tag) {
575 list_del(&tcmd->hash_list);
579 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
584 int scsi_tgt_kspace_exec(int host_no, u64 tag, int result, u32 len,
585 unsigned long uaddr, u8 rw)
587 struct Scsi_Host *shost;
588 struct scsi_cmnd *cmd;
590 struct scsi_tgt_cmd *tcmd;
593 dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
594 result, len, uaddr, rw);
596 /* TODO: replace with a O(1) alg */
597 shost = scsi_host_lookup(host_no);
599 printk(KERN_ERR "Could not find host no %d\n", host_no);
603 if (!shost->uspace_req_q) {
604 printk(KERN_ERR "Not target scsi host %d\n", host_no);
608 rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
610 printk(KERN_ERR "Could not find tag %llu\n",
611 (unsigned long long) tag);
617 dprintk("cmd %p result %d len %d bufflen %u %lu %x\n", cmd,
618 result, len, cmd->request_bufflen, rq_data_dir(rq), cmd->cmnd[0]);
620 if (result == TASK_ABORTED) {
621 scsi_tgt_abort_cmd(shost, cmd);
625 * store the userspace values here, the working values are
626 * in the request_* values
628 tcmd = cmd->request->end_io_data;
629 tcmd->buffer = (void *)uaddr;
631 cmd->result = result;
633 if (!tcmd->bufflen || cmd->request_buffer) {
634 err = __scsi_tgt_transfer_response(cmd);
639 * TODO: Do we need to handle case where request does not
642 err = scsi_map_user_pages(rq->end_io_data, cmd, rw);
644 eprintk("%p %d\n", cmd, err);
649 /* userspace failure */
651 if (status_byte(cmd->result) == CHECK_CONDITION)
652 scsi_tgt_copy_sense(cmd, uaddr, len);
653 err = __scsi_tgt_transfer_response(cmd);
656 /* ask the target LLD to transfer the data to the buffer */
657 err = scsi_tgt_transfer_data(cmd);
660 scsi_host_put(shost);
664 int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, int function, u64 tag,
665 struct scsi_lun *scsilun, void *data)
669 /* TODO: need to retry if this fails. */
670 err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, function,
673 eprintk("The task management request lost!\n");
676 EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
678 int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 mid, int result)
680 struct Scsi_Host *shost;
683 dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
685 shost = scsi_host_lookup(host_no);
687 printk(KERN_ERR "Could not find host no %d\n", host_no);
691 if (!shost->uspace_req_q) {
692 printk(KERN_ERR "Not target scsi host %d\n", host_no);
696 err = shost->hostt->tsk_mgmt_response(mid, result);
698 scsi_host_put(shost);
702 static int __init scsi_tgt_init(void)
706 scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd",
707 sizeof(struct scsi_tgt_cmd),
709 if (!scsi_tgt_cmd_cache)
712 scsi_tgtd = create_workqueue("scsi_tgtd");
718 err = scsi_tgt_if_init();
725 destroy_workqueue(scsi_tgtd);
727 kmem_cache_destroy(scsi_tgt_cmd_cache);
731 static void __exit scsi_tgt_exit(void)
733 destroy_workqueue(scsi_tgtd);
735 kmem_cache_destroy(scsi_tgt_cmd_cache);
738 module_init(scsi_tgt_init);
739 module_exit(scsi_tgt_exit);
741 MODULE_DESCRIPTION("SCSI target core");
742 MODULE_LICENSE("GPL");