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>
32 #include "scsi_tgt_priv.h"
34 static struct workqueue_struct *scsi_tgtd;
35 static struct kmem_cache *scsi_tgt_cmd_cache;
38 * TODO: this struct will be killed when the block layer supports large bios
39 * and James's work struct code is in
42 /* TODO replace work with James b's code */
43 struct work_struct work;
44 /* TODO fix limits of some drivers */
47 struct list_head hash_list;
55 #define TGT_HASH_ORDER 4
56 #define cmd_hashfn(tag) hash_long((unsigned long) (tag), TGT_HASH_ORDER)
58 struct scsi_tgt_queuedata {
59 struct Scsi_Host *shost;
60 struct list_head cmd_hash[1 << TGT_HASH_ORDER];
61 spinlock_t cmd_hash_lock;
65 * Function: scsi_host_get_command()
67 * Purpose: Allocate and setup a scsi command block and blk request
69 * Arguments: shost - scsi host
70 * data_dir - dma data dir
71 * gfp_mask- allocator flags
73 * Returns: The allocated scsi command structure.
75 * This should be called by target LLDs to get a command.
77 struct scsi_cmnd *scsi_host_get_command(struct Scsi_Host *shost,
78 enum dma_data_direction data_dir,
81 int write = (data_dir == DMA_TO_DEVICE);
83 struct scsi_cmnd *cmd;
84 struct scsi_tgt_cmd *tcmd;
86 /* Bail if we can't get a reference to the device */
87 if (!get_device(&shost->shost_gendev))
90 tcmd = kmem_cache_alloc(scsi_tgt_cmd_cache, GFP_ATOMIC);
95 * The blk helpers are used to the READ/WRITE requests
96 * transfering data from a initiator point of view. Since
97 * we are in target mode we want the opposite.
99 rq = blk_get_request(shost->uspace_req_q, !write, gfp_mask);
103 cmd = __scsi_get_command(shost, gfp_mask);
107 memset(cmd, 0, sizeof(*cmd));
108 cmd->sc_data_direction = data_dir;
109 cmd->jiffies_at_alloc = jiffies;
113 rq->cmd_type = REQ_TYPE_SPECIAL;
114 rq->cmd_flags |= REQ_TYPE_BLOCK_PC;
115 rq->end_io_data = tcmd;
124 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
126 put_device(&shost->shost_gendev);
130 EXPORT_SYMBOL_GPL(scsi_host_get_command);
133 * Function: scsi_host_put_command()
135 * Purpose: Free a scsi command block
137 * Arguments: shost - scsi host
138 * cmd - command block to free
142 * Notes: The command must not belong to any lists.
144 void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
146 struct request_queue *q = shost->uspace_req_q;
147 struct request *rq = cmd->request;
148 struct scsi_tgt_cmd *tcmd = rq->end_io_data;
151 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
153 spin_lock_irqsave(q->queue_lock, flags);
154 __blk_put_request(q, rq);
155 spin_unlock_irqrestore(q->queue_lock, flags);
157 __scsi_put_command(shost, cmd, &shost->shost_gendev);
159 EXPORT_SYMBOL_GPL(scsi_host_put_command);
161 static void cmd_hashlist_del(struct scsi_cmnd *cmd)
163 struct request_queue *q = cmd->request->q;
164 struct scsi_tgt_queuedata *qdata = q->queuedata;
166 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
168 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
169 list_del(&tcmd->hash_list);
170 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
173 static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd)
175 blk_rq_unmap_user(tcmd->bio);
178 static void scsi_tgt_cmd_destroy(struct work_struct *work)
180 struct scsi_tgt_cmd *tcmd =
181 container_of(work, struct scsi_tgt_cmd, work);
182 struct scsi_cmnd *cmd = tcmd->rq->special;
184 dprintk("cmd %p %d %lu\n", cmd, cmd->sc_data_direction,
185 rq_data_dir(cmd->request));
186 scsi_unmap_user_pages(tcmd);
187 scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd);
190 static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd,
193 struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
195 struct list_head *head;
199 INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy);
200 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
201 head = &qdata->cmd_hash[cmd_hashfn(tag)];
202 list_add(&tcmd->hash_list, head);
203 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
207 * scsi_tgt_alloc_queue - setup queue used for message passing
210 * This should be called by the LLD after host allocation.
211 * And will be released when the host is released.
213 int scsi_tgt_alloc_queue(struct Scsi_Host *shost)
215 struct scsi_tgt_queuedata *queuedata;
216 struct request_queue *q;
220 * Do we need to send a netlink event or should uspace
221 * just respond to the hotplug event?
223 q = __scsi_alloc_queue(shost, NULL);
227 queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL);
232 queuedata->shost = shost;
233 q->queuedata = queuedata;
236 * this is a silly hack. We should probably just queue as many
237 * command as is recvd to userspace. uspace can then make
238 * sure we do not overload the HBA
240 q->nr_requests = shost->hostt->can_queue;
242 * We currently only support software LLDs so this does
243 * not matter for now. Do we need this for the cards we support?
244 * If so we should make it a host template value.
246 blk_queue_dma_alignment(q, 0);
247 shost->uspace_req_q = q;
249 for (i = 0; i < ARRAY_SIZE(queuedata->cmd_hash); i++)
250 INIT_LIST_HEAD(&queuedata->cmd_hash[i]);
251 spin_lock_init(&queuedata->cmd_hash_lock);
256 blk_cleanup_queue(q);
259 EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue);
261 void scsi_tgt_free_queue(struct Scsi_Host *shost)
265 struct request_queue *q = shost->uspace_req_q;
266 struct scsi_cmnd *cmd;
267 struct scsi_tgt_queuedata *qdata = q->queuedata;
268 struct scsi_tgt_cmd *tcmd, *n;
271 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
273 for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) {
274 list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i],
276 list_del(&tcmd->hash_list);
277 list_add(&tcmd->hash_list, &cmds);
281 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
283 while (!list_empty(&cmds)) {
284 tcmd = list_entry(cmds.next, struct scsi_tgt_cmd, hash_list);
285 list_del(&tcmd->hash_list);
286 cmd = tcmd->rq->special;
288 shost->hostt->eh_abort_handler(cmd);
289 scsi_tgt_cmd_destroy(&tcmd->work);
292 EXPORT_SYMBOL_GPL(scsi_tgt_free_queue);
294 struct Scsi_Host *scsi_tgt_cmd_to_host(struct scsi_cmnd *cmd)
296 struct scsi_tgt_queuedata *queue = cmd->request->q->queuedata;
299 EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host);
302 * scsi_tgt_queue_command - queue command for userspace processing
305 * @tag: unique value to identify this command for tmf
307 int scsi_tgt_queue_command(struct scsi_cmnd *cmd, struct scsi_lun *scsilun,
310 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
313 init_scsi_tgt_cmd(cmd->request, tcmd, tag);
314 err = scsi_tgt_uspace_send_cmd(cmd, scsilun, tag);
316 cmd_hashlist_del(cmd);
320 EXPORT_SYMBOL_GPL(scsi_tgt_queue_command);
323 * This is run from a interrpt handler normally and the unmap
324 * needs process context so we must queue
326 static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)
328 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
330 dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
332 scsi_tgt_uspace_send_status(cmd, tcmd->tag);
333 queue_work(scsi_tgtd, &tcmd->work);
336 static int __scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
338 struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
341 dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
343 err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done);
345 case SCSI_MLQUEUE_HOST_BUSY:
346 case SCSI_MLQUEUE_DEVICE_BUSY:
353 static void scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
355 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
358 err = __scsi_tgt_transfer_response(cmd);
362 cmd->result = DID_BUS_BUSY << 16;
363 err = scsi_tgt_uspace_send_status(cmd, tcmd->tag);
365 /* the eh will have to pick this up */
366 printk(KERN_ERR "Could not send cmd %p status\n", cmd);
369 static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask)
371 struct request *rq = cmd->request;
372 struct scsi_tgt_cmd *tcmd = rq->end_io_data;
375 cmd->use_sg = rq->nr_phys_segments;
376 cmd->request_buffer = scsi_alloc_sgtable(cmd, gfp_mask);
377 if (!cmd->request_buffer)
380 cmd->request_bufflen = rq->data_len;
382 dprintk("cmd %p addr %p cnt %d %lu\n", cmd, tcmd->buffer, cmd->use_sg,
384 count = blk_rq_map_sg(rq->q, rq, cmd->request_buffer);
385 if (likely(count <= cmd->use_sg)) {
390 eprintk("cmd %p addr %p cnt %d\n", cmd, tcmd->buffer, cmd->use_sg);
391 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
395 /* TODO: test this crap and replace bio_map_user with new interface maybe */
396 static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
399 struct request_queue *q = cmd->request->q;
400 struct request *rq = cmd->request;
401 void *uaddr = tcmd->buffer;
402 unsigned int len = tcmd->bufflen;
405 dprintk("%lx %u\n", (unsigned long) uaddr, len);
406 err = blk_rq_map_user(q, rq, uaddr, len);
409 * TODO: need to fixup sg_tablesize, max_segment_size,
410 * max_sectors, etc for modern HW and software drivers
411 * where this value is bogus.
413 * TODO2: we can alloc a reserve buffer of max size
414 * we can handle and do the slow copy path for really large
417 eprintk("Could not handle request of size %u.\n", len);
422 err = scsi_tgt_init_cmd(cmd, GFP_KERNEL);
429 scsi_unmap_user_pages(tcmd);
433 static int scsi_tgt_transfer_data(struct scsi_cmnd *);
435 static void scsi_tgt_data_transfer_done(struct scsi_cmnd *cmd)
437 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
440 /* should we free resources here on error ? */
442 err = scsi_tgt_uspace_send_status(cmd, tcmd->tag);
444 /* the tgt uspace eh will have to pick this up */
445 printk(KERN_ERR "Could not send cmd %p status\n", cmd);
449 dprintk("cmd %p request_bufflen %u bufflen %u\n",
450 cmd, cmd->request_bufflen, tcmd->bufflen);
452 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
453 tcmd->buffer += cmd->request_bufflen;
454 scsi_tgt_transfer_response(cmd);
457 static int scsi_tgt_transfer_data(struct scsi_cmnd *cmd)
460 struct Scsi_Host *host = scsi_tgt_cmd_to_host(cmd);
462 err = host->hostt->transfer_data(cmd, scsi_tgt_data_transfer_done);
464 case SCSI_MLQUEUE_HOST_BUSY:
465 case SCSI_MLQUEUE_DEVICE_BUSY:
472 static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
475 char __user *p = (char __user *) uaddr;
477 if (copy_from_user(cmd->sense_buffer, p,
478 min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) {
479 printk(KERN_ERR "Could not copy the sense buffer\n");
485 static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
487 struct scsi_tgt_cmd *tcmd;
490 err = shost->hostt->eh_abort_handler(cmd);
492 eprintk("fail to abort %p\n", cmd);
494 tcmd = cmd->request->end_io_data;
495 scsi_tgt_cmd_destroy(&tcmd->work);
499 static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
501 struct scsi_tgt_queuedata *qdata = q->queuedata;
502 struct request *rq = NULL;
503 struct list_head *head;
504 struct scsi_tgt_cmd *tcmd;
507 head = &qdata->cmd_hash[cmd_hashfn(tag)];
508 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
509 list_for_each_entry(tcmd, head, hash_list) {
510 if (tcmd->tag == tag) {
512 list_del(&tcmd->hash_list);
516 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
521 int scsi_tgt_kspace_exec(int host_no, u64 tag, int result, u32 len,
522 unsigned long uaddr, u8 rw)
524 struct Scsi_Host *shost;
525 struct scsi_cmnd *cmd;
527 struct scsi_tgt_cmd *tcmd;
530 dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
531 result, len, uaddr, rw);
533 /* TODO: replace with a O(1) alg */
534 shost = scsi_host_lookup(host_no);
536 printk(KERN_ERR "Could not find host no %d\n", host_no);
540 if (!shost->uspace_req_q) {
541 printk(KERN_ERR "Not target scsi host %d\n", host_no);
545 rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
547 printk(KERN_ERR "Could not find tag %llu\n",
548 (unsigned long long) tag);
554 dprintk("cmd %p scb %x result %d len %d bufflen %u %lu %x\n",
555 cmd, cmd->cmnd[0], result, len, cmd->request_bufflen,
556 rq_data_dir(rq), cmd->cmnd[0]);
558 if (result == TASK_ABORTED) {
559 scsi_tgt_abort_cmd(shost, cmd);
563 * store the userspace values here, the working values are
564 * in the request_* values
566 tcmd = cmd->request->end_io_data;
567 tcmd->buffer = (void *)uaddr;
569 cmd->result = result;
571 if (!tcmd->bufflen || cmd->request_buffer) {
572 err = __scsi_tgt_transfer_response(cmd);
577 * TODO: Do we need to handle case where request does not
580 err = scsi_map_user_pages(rq->end_io_data, cmd, rw);
582 eprintk("%p %d\n", cmd, err);
587 /* userspace failure */
589 if (status_byte(cmd->result) == CHECK_CONDITION)
590 scsi_tgt_copy_sense(cmd, uaddr, len);
591 err = __scsi_tgt_transfer_response(cmd);
594 /* ask the target LLD to transfer the data to the buffer */
595 err = scsi_tgt_transfer_data(cmd);
598 scsi_host_put(shost);
602 int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, int function, u64 tag,
603 struct scsi_lun *scsilun, void *data)
607 /* TODO: need to retry if this fails. */
608 err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, function,
611 eprintk("The task management request lost!\n");
614 EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
616 int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 mid, int result)
618 struct Scsi_Host *shost;
621 dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
623 shost = scsi_host_lookup(host_no);
625 printk(KERN_ERR "Could not find host no %d\n", host_no);
629 if (!shost->uspace_req_q) {
630 printk(KERN_ERR "Not target scsi host %d\n", host_no);
634 err = shost->hostt->tsk_mgmt_response(mid, result);
636 scsi_host_put(shost);
640 static int __init scsi_tgt_init(void)
644 scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd",
645 sizeof(struct scsi_tgt_cmd),
647 if (!scsi_tgt_cmd_cache)
650 scsi_tgtd = create_workqueue("scsi_tgtd");
656 err = scsi_tgt_if_init();
663 destroy_workqueue(scsi_tgtd);
665 kmem_cache_destroy(scsi_tgt_cmd_cache);
669 static void __exit scsi_tgt_exit(void)
671 destroy_workqueue(scsi_tgtd);
673 kmem_cache_destroy(scsi_tgt_cmd_cache);
676 module_init(scsi_tgt_init);
677 module_exit(scsi_tgt_exit);
679 MODULE_DESCRIPTION("SCSI target core");
680 MODULE_LICENSE("GPL");