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_transport.h>
31 #include <scsi/scsi_tgt.h>
33 #include "scsi_tgt_priv.h"
35 static struct workqueue_struct
*scsi_tgtd
;
36 static struct kmem_cache
*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 fix limits of some drivers */
48 struct list_head hash_list
;
54 #define TGT_HASH_ORDER 4
55 #define cmd_hashfn(tag) hash_long((unsigned long) (tag), TGT_HASH_ORDER)
57 struct scsi_tgt_queuedata
{
58 struct Scsi_Host
*shost
;
59 struct list_head cmd_hash
[1 << TGT_HASH_ORDER
];
60 spinlock_t cmd_hash_lock
;
64 * Function: scsi_host_get_command()
66 * Purpose: Allocate and setup a scsi command block and blk request
68 * Arguments: shost - scsi host
69 * data_dir - dma data dir
70 * gfp_mask- allocator flags
72 * Returns: The allocated scsi command structure.
74 * This should be called by target LLDs to get a command.
76 struct scsi_cmnd
*scsi_host_get_command(struct Scsi_Host
*shost
,
77 enum dma_data_direction data_dir
,
80 int write
= (data_dir
== DMA_TO_DEVICE
);
82 struct scsi_cmnd
*cmd
;
83 struct scsi_tgt_cmd
*tcmd
;
85 /* Bail if we can't get a reference to the device */
86 if (!get_device(&shost
->shost_gendev
))
89 tcmd
= kmem_cache_alloc(scsi_tgt_cmd_cache
, GFP_ATOMIC
);
94 * The blk helpers are used to the READ/WRITE requests
95 * transfering data from a initiator point of view. Since
96 * we are in target mode we want the opposite.
98 rq
= blk_get_request(shost
->uspace_req_q
, !write
, gfp_mask
);
102 cmd
= __scsi_get_command(shost
, gfp_mask
);
106 cmd
->sc_data_direction
= data_dir
;
107 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 %u\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
;
197 tcmd
->itn_id
= itn_id
;
200 INIT_WORK(&tcmd
->work
, scsi_tgt_cmd_destroy
);
201 spin_lock_irqsave(&qdata
->cmd_hash_lock
, flags
);
202 head
= &qdata
->cmd_hash
[cmd_hashfn(tag
)];
203 list_add(&tcmd
->hash_list
, head
);
204 spin_unlock_irqrestore(&qdata
->cmd_hash_lock
, flags
);
208 * scsi_tgt_alloc_queue - setup queue used for message passing
211 * This should be called by the LLD after host allocation.
212 * And will be released when the host is released.
214 int scsi_tgt_alloc_queue(struct Scsi_Host
*shost
)
216 struct scsi_tgt_queuedata
*queuedata
;
217 struct request_queue
*q
;
221 * Do we need to send a netlink event or should uspace
222 * just respond to the hotplug event?
224 q
= __scsi_alloc_queue(shost
, NULL
);
228 queuedata
= kzalloc(sizeof(*queuedata
), GFP_KERNEL
);
233 queuedata
->shost
= shost
;
234 q
->queuedata
= queuedata
;
237 * this is a silly hack. We should probably just queue as many
238 * command as is recvd to userspace. uspace can then make
239 * sure we do not overload the HBA
241 q
->nr_requests
= shost
->can_queue
;
243 * We currently only support software LLDs so this does
244 * not matter for now. Do we need this for the cards we support?
245 * If so we should make it a host template value.
247 blk_queue_dma_alignment(q
, 0);
248 shost
->uspace_req_q
= q
;
250 for (i
= 0; i
< ARRAY_SIZE(queuedata
->cmd_hash
); i
++)
251 INIT_LIST_HEAD(&queuedata
->cmd_hash
[i
]);
252 spin_lock_init(&queuedata
->cmd_hash_lock
);
257 blk_cleanup_queue(q
);
260 EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue
);
262 void scsi_tgt_free_queue(struct Scsi_Host
*shost
)
266 struct request_queue
*q
= shost
->uspace_req_q
;
267 struct scsi_cmnd
*cmd
;
268 struct scsi_tgt_queuedata
*qdata
= q
->queuedata
;
269 struct scsi_tgt_cmd
*tcmd
, *n
;
272 spin_lock_irqsave(&qdata
->cmd_hash_lock
, flags
);
274 for (i
= 0; i
< ARRAY_SIZE(qdata
->cmd_hash
); i
++) {
275 list_for_each_entry_safe(tcmd
, n
, &qdata
->cmd_hash
[i
],
277 list_del(&tcmd
->hash_list
);
278 list_add(&tcmd
->hash_list
, &cmds
);
282 spin_unlock_irqrestore(&qdata
->cmd_hash_lock
, flags
);
284 while (!list_empty(&cmds
)) {
285 tcmd
= list_entry(cmds
.next
, struct scsi_tgt_cmd
, hash_list
);
286 list_del(&tcmd
->hash_list
);
287 cmd
= tcmd
->rq
->special
;
289 shost
->hostt
->eh_abort_handler(cmd
);
290 scsi_tgt_cmd_destroy(&tcmd
->work
);
293 EXPORT_SYMBOL_GPL(scsi_tgt_free_queue
);
295 struct Scsi_Host
*scsi_tgt_cmd_to_host(struct scsi_cmnd
*cmd
)
297 struct scsi_tgt_queuedata
*queue
= cmd
->request
->q
->queuedata
;
300 EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host
);
303 * scsi_tgt_queue_command - queue command for userspace processing
306 * @tag: unique value to identify this command for tmf
308 int scsi_tgt_queue_command(struct scsi_cmnd
*cmd
, u64 itn_id
,
309 struct scsi_lun
*scsilun
, u64 tag
)
311 struct scsi_tgt_cmd
*tcmd
= cmd
->request
->end_io_data
;
314 init_scsi_tgt_cmd(cmd
->request
, tcmd
, itn_id
, tag
);
315 err
= scsi_tgt_uspace_send_cmd(cmd
, itn_id
, scsilun
, tag
);
317 cmd_hashlist_del(cmd
);
321 EXPORT_SYMBOL_GPL(scsi_tgt_queue_command
);
324 * This is run from a interrupt handler normally and the unmap
325 * needs process context so we must queue
327 static void scsi_tgt_cmd_done(struct scsi_cmnd
*cmd
)
329 struct scsi_tgt_cmd
*tcmd
= cmd
->request
->end_io_data
;
331 dprintk("cmd %p %u\n", cmd
, rq_data_dir(cmd
->request
));
333 scsi_tgt_uspace_send_status(cmd
, tcmd
->itn_id
, tcmd
->tag
);
335 scsi_release_buffers(cmd
);
337 queue_work(scsi_tgtd
, &tcmd
->work
);
340 static int scsi_tgt_transfer_response(struct scsi_cmnd
*cmd
)
342 struct Scsi_Host
*shost
= scsi_tgt_cmd_to_host(cmd
);
345 dprintk("cmd %p %u\n", cmd
, rq_data_dir(cmd
->request
));
347 err
= shost
->hostt
->transfer_response(cmd
, scsi_tgt_cmd_done
);
349 case SCSI_MLQUEUE_HOST_BUSY
:
350 case SCSI_MLQUEUE_DEVICE_BUSY
:
356 /* TODO: test this crap and replace bio_map_user with new interface maybe */
357 static int scsi_map_user_pages(struct scsi_tgt_cmd
*tcmd
, struct scsi_cmnd
*cmd
,
358 unsigned long uaddr
, unsigned int len
, int rw
)
360 struct request_queue
*q
= cmd
->request
->q
;
361 struct request
*rq
= cmd
->request
;
364 dprintk("%lx %u\n", uaddr
, len
);
365 err
= blk_rq_map_user(q
, rq
, NULL
, (void *)uaddr
, len
, GFP_KERNEL
);
368 * TODO: need to fixup sg_tablesize, max_segment_size,
369 * max_sectors, etc for modern HW and software drivers
370 * where this value is bogus.
372 * TODO2: we can alloc a reserve buffer of max size
373 * we can handle and do the slow copy path for really large
376 eprintk("Could not handle request of size %u.\n", len
);
381 err
= scsi_init_io(cmd
, GFP_KERNEL
);
383 scsi_release_buffers(cmd
);
387 * we use REQ_TYPE_BLOCK_PC so scsi_init_io doesn't set the
390 cmd
->sdb
.length
= blk_rq_bytes(rq
);
395 scsi_unmap_user_pages(tcmd
);
399 static int scsi_tgt_copy_sense(struct scsi_cmnd
*cmd
, unsigned long uaddr
,
402 char __user
*p
= (char __user
*) uaddr
;
404 if (copy_from_user(cmd
->sense_buffer
, p
,
405 min_t(unsigned, SCSI_SENSE_BUFFERSIZE
, len
))) {
406 printk(KERN_ERR
"Could not copy the sense buffer\n");
412 static int scsi_tgt_abort_cmd(struct Scsi_Host
*shost
, struct scsi_cmnd
*cmd
)
414 struct scsi_tgt_cmd
*tcmd
;
417 err
= shost
->hostt
->eh_abort_handler(cmd
);
419 eprintk("fail to abort %p\n", cmd
);
421 tcmd
= cmd
->request
->end_io_data
;
422 scsi_tgt_cmd_destroy(&tcmd
->work
);
426 static struct request
*tgt_cmd_hash_lookup(struct request_queue
*q
, u64 tag
)
428 struct scsi_tgt_queuedata
*qdata
= q
->queuedata
;
429 struct request
*rq
= NULL
;
430 struct list_head
*head
;
431 struct scsi_tgt_cmd
*tcmd
;
434 head
= &qdata
->cmd_hash
[cmd_hashfn(tag
)];
435 spin_lock_irqsave(&qdata
->cmd_hash_lock
, flags
);
436 list_for_each_entry(tcmd
, head
, hash_list
) {
437 if (tcmd
->tag
== tag
) {
439 list_del(&tcmd
->hash_list
);
443 spin_unlock_irqrestore(&qdata
->cmd_hash_lock
, flags
);
448 int scsi_tgt_kspace_exec(int host_no
, u64 itn_id
, int result
, u64 tag
,
449 unsigned long uaddr
, u32 len
, unsigned long sense_uaddr
,
450 u32 sense_len
, u8 rw
)
452 struct Scsi_Host
*shost
;
453 struct scsi_cmnd
*cmd
;
455 struct scsi_tgt_cmd
*tcmd
;
458 dprintk("%d %llu %d %u %lx %u\n", host_no
, (unsigned long long) tag
,
459 result
, len
, uaddr
, rw
);
461 /* TODO: replace with a O(1) alg */
462 shost
= scsi_host_lookup(host_no
);
464 printk(KERN_ERR
"Could not find host no %d\n", host_no
);
468 if (!shost
->uspace_req_q
) {
469 printk(KERN_ERR
"Not target scsi host %d\n", host_no
);
473 rq
= tgt_cmd_hash_lookup(shost
->uspace_req_q
, tag
);
475 printk(KERN_ERR
"Could not find tag %llu\n",
476 (unsigned long long) tag
);
482 dprintk("cmd %p scb %x result %d len %d bufflen %u %u %x\n",
483 cmd
, cmd
->cmnd
[0], result
, len
, scsi_bufflen(cmd
),
484 rq_data_dir(rq
), cmd
->cmnd
[0]);
486 if (result
== TASK_ABORTED
) {
487 scsi_tgt_abort_cmd(shost
, cmd
);
491 * store the userspace values here, the working values are
492 * in the request_* values
494 tcmd
= cmd
->request
->end_io_data
;
495 cmd
->result
= result
;
497 if (cmd
->result
== SAM_STAT_CHECK_CONDITION
)
498 scsi_tgt_copy_sense(cmd
, sense_uaddr
, sense_len
);
501 err
= scsi_map_user_pages(rq
->end_io_data
, cmd
, uaddr
, len
, rw
);
504 * user-space daemon bugs or OOM
505 * TODO: we can do better for OOM.
507 struct scsi_tgt_queuedata
*qdata
;
508 struct list_head
*head
;
511 eprintk("cmd %p ret %d uaddr %lx len %d rw %d\n",
512 cmd
, err
, uaddr
, len
, rw
);
514 qdata
= shost
->uspace_req_q
->queuedata
;
515 head
= &qdata
->cmd_hash
[cmd_hashfn(tcmd
->tag
)];
517 spin_lock_irqsave(&qdata
->cmd_hash_lock
, flags
);
518 list_add(&tcmd
->hash_list
, head
);
519 spin_unlock_irqrestore(&qdata
->cmd_hash_lock
, flags
);
524 err
= scsi_tgt_transfer_response(cmd
);
526 scsi_host_put(shost
);
530 int scsi_tgt_tsk_mgmt_request(struct Scsi_Host
*shost
, u64 itn_id
,
531 int function
, u64 tag
, struct scsi_lun
*scsilun
,
536 /* TODO: need to retry if this fails. */
537 err
= scsi_tgt_uspace_send_tsk_mgmt(shost
->host_no
, itn_id
,
538 function
, tag
, scsilun
, data
);
540 eprintk("The task management request lost!\n");
543 EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request
);
545 int scsi_tgt_kspace_tsk_mgmt(int host_no
, u64 itn_id
, u64 mid
, int result
)
547 struct Scsi_Host
*shost
;
550 dprintk("%d %d %llx\n", host_no
, result
, (unsigned long long) mid
);
552 shost
= scsi_host_lookup(host_no
);
554 printk(KERN_ERR
"Could not find host no %d\n", host_no
);
558 if (!shost
->uspace_req_q
) {
559 printk(KERN_ERR
"Not target scsi host %d\n", host_no
);
563 err
= shost
->transportt
->tsk_mgmt_response(shost
, itn_id
, mid
, result
);
565 scsi_host_put(shost
);
569 int scsi_tgt_it_nexus_create(struct Scsi_Host
*shost
, u64 itn_id
,
574 /* TODO: need to retry if this fails. */
575 err
= scsi_tgt_uspace_send_it_nexus_request(shost
->host_no
, itn_id
, 0,
578 eprintk("The i_t_neuxs request lost, %d %llx!\n",
579 shost
->host_no
, (unsigned long long)itn_id
);
582 EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_create
);
584 int scsi_tgt_it_nexus_destroy(struct Scsi_Host
*shost
, u64 itn_id
)
588 /* TODO: need to retry if this fails. */
589 err
= scsi_tgt_uspace_send_it_nexus_request(shost
->host_no
,
592 eprintk("The i_t_neuxs request lost, %d %llx!\n",
593 shost
->host_no
, (unsigned long long)itn_id
);
596 EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_destroy
);
598 int scsi_tgt_kspace_it_nexus_rsp(int host_no
, u64 itn_id
, int result
)
600 struct Scsi_Host
*shost
;
603 dprintk("%d %d%llx\n", host_no
, result
, (unsigned long long)itn_id
);
605 shost
= scsi_host_lookup(host_no
);
607 printk(KERN_ERR
"Could not find host no %d\n", host_no
);
611 if (!shost
->uspace_req_q
) {
612 printk(KERN_ERR
"Not target scsi host %d\n", host_no
);
616 err
= shost
->transportt
->it_nexus_response(shost
, itn_id
, result
);
618 scsi_host_put(shost
);
622 static int __init
scsi_tgt_init(void)
626 scsi_tgt_cmd_cache
= KMEM_CACHE(scsi_tgt_cmd
, 0);
627 if (!scsi_tgt_cmd_cache
)
630 scsi_tgtd
= create_workqueue("scsi_tgtd");
636 err
= scsi_tgt_if_init();
643 destroy_workqueue(scsi_tgtd
);
645 kmem_cache_destroy(scsi_tgt_cmd_cache
);
649 static void __exit
scsi_tgt_exit(void)
651 destroy_workqueue(scsi_tgtd
);
653 kmem_cache_destroy(scsi_tgt_cmd_cache
);
656 module_init(scsi_tgt_init
);
657 module_exit(scsi_tgt_exit
);
659 MODULE_DESCRIPTION("SCSI target core");
660 MODULE_LICENSE("GPL");