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;
52 #define TGT_HASH_ORDER 4
53 #define cmd_hashfn(tag) hash_long((unsigned long) (tag), TGT_HASH_ORDER)
55 struct scsi_tgt_queuedata {
56 struct Scsi_Host *shost;
57 struct list_head cmd_hash[1 << TGT_HASH_ORDER];
58 spinlock_t cmd_hash_lock;
62 * Function: scsi_host_get_command()
64 * Purpose: Allocate and setup a scsi command block and blk request
66 * Arguments: shost - scsi host
67 * data_dir - dma data dir
68 * gfp_mask- allocator flags
70 * Returns: The allocated scsi command structure.
72 * This should be called by target LLDs to get a command.
74 struct scsi_cmnd *scsi_host_get_command(struct Scsi_Host *shost,
75 enum dma_data_direction data_dir,
78 int write = (data_dir == DMA_TO_DEVICE);
80 struct scsi_cmnd *cmd;
81 struct scsi_tgt_cmd *tcmd;
83 /* Bail if we can't get a reference to the device */
84 if (!get_device(&shost->shost_gendev))
87 tcmd = kmem_cache_alloc(scsi_tgt_cmd_cache, GFP_ATOMIC);
92 * The blk helpers are used to the READ/WRITE requests
93 * transfering data from a initiator point of view. Since
94 * we are in target mode we want the opposite.
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;
121 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
123 put_device(&shost->shost_gendev);
127 EXPORT_SYMBOL_GPL(scsi_host_get_command);
130 * Function: scsi_host_put_command()
132 * Purpose: Free a scsi command block
134 * Arguments: shost - scsi host
135 * cmd - command block to free
139 * Notes: The command must not belong to any lists.
141 void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
143 struct request_queue *q = shost->uspace_req_q;
144 struct request *rq = cmd->request;
145 struct scsi_tgt_cmd *tcmd = rq->end_io_data;
148 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
150 spin_lock_irqsave(q->queue_lock, flags);
151 __blk_put_request(q, rq);
152 spin_unlock_irqrestore(q->queue_lock, flags);
154 __scsi_put_command(shost, cmd, &shost->shost_gendev);
156 EXPORT_SYMBOL_GPL(scsi_host_put_command);
158 static void cmd_hashlist_del(struct scsi_cmnd *cmd)
160 struct request_queue *q = cmd->request->q;
161 struct scsi_tgt_queuedata *qdata = q->queuedata;
163 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
165 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
166 list_del(&tcmd->hash_list);
167 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
170 static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd)
172 blk_rq_unmap_user(tcmd->bio);
175 static void scsi_tgt_cmd_destroy(struct work_struct *work)
177 struct scsi_tgt_cmd *tcmd =
178 container_of(work, struct scsi_tgt_cmd, work);
179 struct scsi_cmnd *cmd = tcmd->rq->special;
181 dprintk("cmd %p %d %lu\n", cmd, cmd->sc_data_direction,
182 rq_data_dir(cmd->request));
183 scsi_unmap_user_pages(tcmd);
184 scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd);
187 static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd,
190 struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
192 struct list_head *head;
196 INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy);
197 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
198 head = &qdata->cmd_hash[cmd_hashfn(tag)];
199 list_add(&tcmd->hash_list, head);
200 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
204 * scsi_tgt_alloc_queue - setup queue used for message passing
207 * This should be called by the LLD after host allocation.
208 * And will be released when the host is released.
210 int scsi_tgt_alloc_queue(struct Scsi_Host *shost)
212 struct scsi_tgt_queuedata *queuedata;
213 struct request_queue *q;
217 * Do we need to send a netlink event or should uspace
218 * just respond to the hotplug event?
220 q = __scsi_alloc_queue(shost, NULL);
224 queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL);
229 queuedata->shost = shost;
230 q->queuedata = queuedata;
233 * this is a silly hack. We should probably just queue as many
234 * command as is recvd to userspace. uspace can then make
235 * sure we do not overload the HBA
237 q->nr_requests = shost->hostt->can_queue;
239 * We currently only support software LLDs so this does
240 * not matter for now. Do we need this for the cards we support?
241 * If so we should make it a host template value.
243 blk_queue_dma_alignment(q, 0);
244 shost->uspace_req_q = q;
246 for (i = 0; i < ARRAY_SIZE(queuedata->cmd_hash); i++)
247 INIT_LIST_HEAD(&queuedata->cmd_hash[i]);
248 spin_lock_init(&queuedata->cmd_hash_lock);
253 blk_cleanup_queue(q);
256 EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue);
258 void scsi_tgt_free_queue(struct Scsi_Host *shost)
262 struct request_queue *q = shost->uspace_req_q;
263 struct scsi_cmnd *cmd;
264 struct scsi_tgt_queuedata *qdata = q->queuedata;
265 struct scsi_tgt_cmd *tcmd, *n;
268 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
270 for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) {
271 list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i],
273 list_del(&tcmd->hash_list);
274 list_add(&tcmd->hash_list, &cmds);
278 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
280 while (!list_empty(&cmds)) {
281 tcmd = list_entry(cmds.next, struct scsi_tgt_cmd, hash_list);
282 list_del(&tcmd->hash_list);
283 cmd = tcmd->rq->special;
285 shost->hostt->eh_abort_handler(cmd);
286 scsi_tgt_cmd_destroy(&tcmd->work);
289 EXPORT_SYMBOL_GPL(scsi_tgt_free_queue);
291 struct Scsi_Host *scsi_tgt_cmd_to_host(struct scsi_cmnd *cmd)
293 struct scsi_tgt_queuedata *queue = cmd->request->q->queuedata;
296 EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host);
299 * scsi_tgt_queue_command - queue command for userspace processing
302 * @tag: unique value to identify this command for tmf
304 int scsi_tgt_queue_command(struct scsi_cmnd *cmd, struct scsi_lun *scsilun,
307 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
310 init_scsi_tgt_cmd(cmd->request, tcmd, tag);
311 err = scsi_tgt_uspace_send_cmd(cmd, scsilun, tag);
313 cmd_hashlist_del(cmd);
317 EXPORT_SYMBOL_GPL(scsi_tgt_queue_command);
320 * This is run from a interrpt handler normally and the unmap
321 * needs process context so we must queue
323 static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)
325 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
327 dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
329 scsi_tgt_uspace_send_status(cmd, tcmd->tag);
331 if (cmd->request_buffer)
332 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
334 queue_work(scsi_tgtd, &tcmd->work);
337 static int scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
339 struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
342 dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
344 err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done);
346 case SCSI_MLQUEUE_HOST_BUSY:
347 case SCSI_MLQUEUE_DEVICE_BUSY:
353 static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask)
355 struct request *rq = cmd->request;
358 cmd->use_sg = rq->nr_phys_segments;
359 cmd->request_buffer = scsi_alloc_sgtable(cmd, gfp_mask);
360 if (!cmd->request_buffer)
363 cmd->request_bufflen = rq->data_len;
365 dprintk("cmd %p cnt %d %lu\n", cmd, cmd->use_sg, rq_data_dir(rq));
366 count = blk_rq_map_sg(rq->q, rq, cmd->request_buffer);
367 if (likely(count <= cmd->use_sg)) {
372 eprintk("cmd %p cnt %d\n", cmd, cmd->use_sg);
373 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
377 /* TODO: test this crap and replace bio_map_user with new interface maybe */
378 static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
379 unsigned long uaddr, unsigned int len, int rw)
381 struct request_queue *q = cmd->request->q;
382 struct request *rq = cmd->request;
385 dprintk("%lx %u\n", uaddr, len);
386 err = blk_rq_map_user(q, rq, (void *)uaddr, len);
389 * TODO: need to fixup sg_tablesize, max_segment_size,
390 * max_sectors, etc for modern HW and software drivers
391 * where this value is bogus.
393 * TODO2: we can alloc a reserve buffer of max size
394 * we can handle and do the slow copy path for really large
397 eprintk("Could not handle request of size %u.\n", len);
402 err = scsi_tgt_init_cmd(cmd, GFP_KERNEL);
409 scsi_unmap_user_pages(tcmd);
413 static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
416 char __user *p = (char __user *) uaddr;
418 if (copy_from_user(cmd->sense_buffer, p,
419 min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) {
420 printk(KERN_ERR "Could not copy the sense buffer\n");
426 static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
428 struct scsi_tgt_cmd *tcmd;
431 err = shost->hostt->eh_abort_handler(cmd);
433 eprintk("fail to abort %p\n", cmd);
435 tcmd = cmd->request->end_io_data;
436 scsi_tgt_cmd_destroy(&tcmd->work);
440 static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
442 struct scsi_tgt_queuedata *qdata = q->queuedata;
443 struct request *rq = NULL;
444 struct list_head *head;
445 struct scsi_tgt_cmd *tcmd;
448 head = &qdata->cmd_hash[cmd_hashfn(tag)];
449 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
450 list_for_each_entry(tcmd, head, hash_list) {
451 if (tcmd->tag == tag) {
453 list_del(&tcmd->hash_list);
457 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
462 int scsi_tgt_kspace_exec(int host_no, int result, u64 tag,
463 unsigned long uaddr, u32 len, unsigned long sense_uaddr,
464 u32 sense_len, u8 rw)
466 struct Scsi_Host *shost;
467 struct scsi_cmnd *cmd;
469 struct scsi_tgt_cmd *tcmd;
472 dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
473 result, len, uaddr, rw);
475 /* TODO: replace with a O(1) alg */
476 shost = scsi_host_lookup(host_no);
478 printk(KERN_ERR "Could not find host no %d\n", host_no);
482 if (!shost->uspace_req_q) {
483 printk(KERN_ERR "Not target scsi host %d\n", host_no);
487 rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
489 printk(KERN_ERR "Could not find tag %llu\n",
490 (unsigned long long) tag);
496 dprintk("cmd %p scb %x result %d len %d bufflen %u %lu %x\n",
497 cmd, cmd->cmnd[0], result, len, cmd->request_bufflen,
498 rq_data_dir(rq), cmd->cmnd[0]);
500 if (result == TASK_ABORTED) {
501 scsi_tgt_abort_cmd(shost, cmd);
505 * store the userspace values here, the working values are
506 * in the request_* values
508 tcmd = cmd->request->end_io_data;
509 cmd->result = result;
511 if (cmd->result == SAM_STAT_CHECK_CONDITION)
512 scsi_tgt_copy_sense(cmd, sense_uaddr, sense_len);
515 err = scsi_map_user_pages(rq->end_io_data, cmd, uaddr, len, rw);
518 * user-space daemon bugs or OOM
519 * TODO: we can do better for OOM.
521 struct scsi_tgt_queuedata *qdata;
522 struct list_head *head;
525 eprintk("cmd %p ret %d uaddr %lx len %d rw %d\n",
526 cmd, err, uaddr, len, rw);
528 qdata = shost->uspace_req_q->queuedata;
529 head = &qdata->cmd_hash[cmd_hashfn(tcmd->tag)];
531 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
532 list_add(&tcmd->hash_list, head);
533 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
538 err = scsi_tgt_transfer_response(cmd);
540 scsi_host_put(shost);
544 int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, int function, u64 tag,
545 struct scsi_lun *scsilun, void *data)
549 /* TODO: need to retry if this fails. */
550 err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, function,
553 eprintk("The task management request lost!\n");
556 EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
558 int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 mid, int result)
560 struct Scsi_Host *shost;
563 dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
565 shost = scsi_host_lookup(host_no);
567 printk(KERN_ERR "Could not find host no %d\n", host_no);
571 if (!shost->uspace_req_q) {
572 printk(KERN_ERR "Not target scsi host %d\n", host_no);
576 err = shost->hostt->tsk_mgmt_response(mid, result);
578 scsi_host_put(shost);
582 static int __init scsi_tgt_init(void)
586 scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd",
587 sizeof(struct scsi_tgt_cmd),
589 if (!scsi_tgt_cmd_cache)
592 scsi_tgtd = create_workqueue("scsi_tgtd");
598 err = scsi_tgt_if_init();
605 destroy_workqueue(scsi_tgtd);
607 kmem_cache_destroy(scsi_tgt_cmd_cache);
611 static void __exit scsi_tgt_exit(void)
613 destroy_workqueue(scsi_tgtd);
615 kmem_cache_destroy(scsi_tgt_cmd_cache);
618 module_init(scsi_tgt_init);
619 module_exit(scsi_tgt_exit);
621 MODULE_DESCRIPTION("SCSI target core");
622 MODULE_LICENSE("GPL");