blob: 93ece8f4e5dea391c07c8a4241df9d180489c869 [file] [log] [blame]
FUJITA Tomonori5a55c252006-11-16 19:24:13 +09001/*
2 * SCSI target lib functions
3 *
4 * Copyright (C) 2005 Mike Christie <michaelc@cs.wisc.edu>
5 * Copyright (C) 2005 FUJITA Tomonori <tomof@acm.org>
6 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA
21 */
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>
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +090030#include <scsi/scsi_transport.h>
FUJITA Tomonori5a55c252006-11-16 19:24:13 +090031#include <scsi/scsi_tgt.h>
FUJITA Tomonori5a55c252006-11-16 19:24:13 +090032
33#include "scsi_tgt_priv.h"
34
35static struct workqueue_struct *scsi_tgtd;
Christoph Lametere18b8902006-12-06 20:33:20 -080036static struct kmem_cache *scsi_tgt_cmd_cache;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +090037
38/*
39 * TODO: this struct will be killed when the block layer supports large bios
40 * and James's work struct code is in
41 */
42struct scsi_tgt_cmd {
43 /* TODO replace work with James b's code */
44 struct work_struct work;
Mike Christie181011e2007-03-03 09:55:54 +090045 /* TODO fix limits of some drivers */
46 struct bio *bio;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +090047
48 struct list_head hash_list;
49 struct request *rq;
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +090050 u64 itn_id;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +090051 u64 tag;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +090052};
53
54#define TGT_HASH_ORDER 4
55#define cmd_hashfn(tag) hash_long((unsigned long) (tag), TGT_HASH_ORDER)
56
57struct scsi_tgt_queuedata {
58 struct Scsi_Host *shost;
59 struct list_head cmd_hash[1 << TGT_HASH_ORDER];
60 spinlock_t cmd_hash_lock;
61};
62
63/*
64 * Function: scsi_host_get_command()
65 *
66 * Purpose: Allocate and setup a scsi command block and blk request
67 *
68 * Arguments: shost - scsi host
69 * data_dir - dma data dir
70 * gfp_mask- allocator flags
71 *
72 * Returns: The allocated scsi command structure.
73 *
74 * This should be called by target LLDs to get a command.
75 */
76struct scsi_cmnd *scsi_host_get_command(struct Scsi_Host *shost,
77 enum dma_data_direction data_dir,
78 gfp_t gfp_mask)
79{
80 int write = (data_dir == DMA_TO_DEVICE);
81 struct request *rq;
82 struct scsi_cmnd *cmd;
83 struct scsi_tgt_cmd *tcmd;
84
85 /* Bail if we can't get a reference to the device */
86 if (!get_device(&shost->shost_gendev))
87 return NULL;
88
89 tcmd = kmem_cache_alloc(scsi_tgt_cmd_cache, GFP_ATOMIC);
90 if (!tcmd)
91 goto put_dev;
92
Mike Christie181011e2007-03-03 09:55:54 +090093 /*
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.
97 */
98 rq = blk_get_request(shost->uspace_req_q, !write, gfp_mask);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +090099 if (!rq)
100 goto free_tcmd;
101
102 cmd = __scsi_get_command(shost, gfp_mask);
103 if (!cmd)
104 goto release_rq;
105
106 memset(cmd, 0, sizeof(*cmd));
107 cmd->sc_data_direction = data_dir;
108 cmd->jiffies_at_alloc = jiffies;
109 cmd->request = rq;
110
111 rq->special = cmd;
112 rq->cmd_type = REQ_TYPE_SPECIAL;
113 rq->cmd_flags |= REQ_TYPE_BLOCK_PC;
114 rq->end_io_data = tcmd;
115
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900116 tcmd->rq = rq;
117
118 return cmd;
119
120release_rq:
121 blk_put_request(rq);
122free_tcmd:
123 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
124put_dev:
125 put_device(&shost->shost_gendev);
126 return NULL;
127
128}
129EXPORT_SYMBOL_GPL(scsi_host_get_command);
130
131/*
132 * Function: scsi_host_put_command()
133 *
134 * Purpose: Free a scsi command block
135 *
136 * Arguments: shost - scsi host
137 * cmd - command block to free
138 *
139 * Returns: Nothing.
140 *
141 * Notes: The command must not belong to any lists.
142 */
143void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
144{
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;
148 unsigned long flags;
149
150 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
151
152 spin_lock_irqsave(q->queue_lock, flags);
153 __blk_put_request(q, rq);
154 spin_unlock_irqrestore(q->queue_lock, flags);
155
156 __scsi_put_command(shost, cmd, &shost->shost_gendev);
157}
158EXPORT_SYMBOL_GPL(scsi_host_put_command);
159
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900160static void cmd_hashlist_del(struct scsi_cmnd *cmd)
161{
162 struct request_queue *q = cmd->request->q;
163 struct scsi_tgt_queuedata *qdata = q->queuedata;
164 unsigned long flags;
165 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
166
167 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
168 list_del(&tcmd->hash_list);
169 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
170}
171
Mike Christie181011e2007-03-03 09:55:54 +0900172static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd)
173{
174 blk_rq_unmap_user(tcmd->bio);
175}
176
David Howells06328b42006-12-06 15:02:26 +0000177static void scsi_tgt_cmd_destroy(struct work_struct *work)
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900178{
David Howells06328b42006-12-06 15:02:26 +0000179 struct scsi_tgt_cmd *tcmd =
180 container_of(work, struct scsi_tgt_cmd, work);
181 struct scsi_cmnd *cmd = tcmd->rq->special;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900182
Randy Dunlapa8aae4d2007-11-15 11:53:25 +0200183 dprintk("cmd %p %d %u\n", cmd, cmd->sc_data_direction,
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900184 rq_data_dir(cmd->request));
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900185 scsi_unmap_user_pages(tcmd);
186 scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd);
187}
188
189static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd,
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900190 u64 itn_id, u64 tag)
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900191{
192 struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
193 unsigned long flags;
194 struct list_head *head;
195
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900196 tcmd->itn_id = itn_id;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900197 tcmd->tag = tag;
Mike Christie181011e2007-03-03 09:55:54 +0900198 tcmd->bio = NULL;
David Howells06328b42006-12-06 15:02:26 +0000199 INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900200 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);
204}
205
206/*
207 * scsi_tgt_alloc_queue - setup queue used for message passing
208 * shost: scsi host
209 *
210 * This should be called by the LLD after host allocation.
211 * And will be released when the host is released.
212 */
213int scsi_tgt_alloc_queue(struct Scsi_Host *shost)
214{
215 struct scsi_tgt_queuedata *queuedata;
216 struct request_queue *q;
217 int err, i;
218
219 /*
220 * Do we need to send a netlink event or should uspace
221 * just respond to the hotplug event?
222 */
223 q = __scsi_alloc_queue(shost, NULL);
224 if (!q)
225 return -ENOMEM;
226
227 queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL);
228 if (!queuedata) {
229 err = -ENOMEM;
230 goto cleanup_queue;
231 }
232 queuedata->shost = shost;
233 q->queuedata = queuedata;
234
235 /*
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
239 */
FUJITA Tomonori8184fe92007-09-01 02:02:16 +0900240 q->nr_requests = shost->can_queue;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900241 /*
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.
245 */
246 blk_queue_dma_alignment(q, 0);
247 shost->uspace_req_q = q;
248
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);
252
253 return 0;
254
255cleanup_queue:
256 blk_cleanup_queue(q);
257 return err;
258}
259EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue);
260
261void scsi_tgt_free_queue(struct Scsi_Host *shost)
262{
263 int i;
264 unsigned long flags;
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;
269 LIST_HEAD(cmds);
270
271 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
272
273 for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) {
274 list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i],
275 hash_list) {
276 list_del(&tcmd->hash_list);
277 list_add(&tcmd->hash_list, &cmds);
278 }
279 }
280
281 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
282
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;
287
288 shost->hostt->eh_abort_handler(cmd);
David Howells06328b42006-12-06 15:02:26 +0000289 scsi_tgt_cmd_destroy(&tcmd->work);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900290 }
291}
292EXPORT_SYMBOL_GPL(scsi_tgt_free_queue);
293
294struct Scsi_Host *scsi_tgt_cmd_to_host(struct scsi_cmnd *cmd)
295{
296 struct scsi_tgt_queuedata *queue = cmd->request->q->queuedata;
297 return queue->shost;
298}
299EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host);
300
301/*
302 * scsi_tgt_queue_command - queue command for userspace processing
303 * @cmd: scsi command
304 * @scsilun: scsi lun
305 * @tag: unique value to identify this command for tmf
306 */
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900307int scsi_tgt_queue_command(struct scsi_cmnd *cmd, u64 itn_id,
308 struct scsi_lun *scsilun, u64 tag)
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900309{
310 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
311 int err;
312
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900313 init_scsi_tgt_cmd(cmd->request, tcmd, itn_id, tag);
314 err = scsi_tgt_uspace_send_cmd(cmd, itn_id, scsilun, tag);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900315 if (err)
316 cmd_hashlist_del(cmd);
317
318 return err;
319}
320EXPORT_SYMBOL_GPL(scsi_tgt_queue_command);
321
322/*
323 * This is run from a interrpt handler normally and the unmap
324 * needs process context so we must queue
325 */
326static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)
327{
328 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
329
Randy Dunlapa8aae4d2007-11-15 11:53:25 +0200330 dprintk("cmd %p %u\n", cmd, rq_data_dir(cmd->request));
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900331
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900332 scsi_tgt_uspace_send_status(cmd, tcmd->itn_id, tcmd->tag);
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900333
FUJITA Tomonorif10ab662007-10-25 01:21:30 +0900334 if (scsi_sglist(cmd))
Jens Axboe0cde8d92007-10-16 11:12:37 +0200335 scsi_free_sgtable(cmd);
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900336
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900337 queue_work(scsi_tgtd, &tcmd->work);
338}
339
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900340static int scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900341{
342 struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
343 int err;
344
Randy Dunlapa8aae4d2007-11-15 11:53:25 +0200345 dprintk("cmd %p %u\n", cmd, rq_data_dir(cmd->request));
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900346
347 err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done);
348 switch (err) {
349 case SCSI_MLQUEUE_HOST_BUSY:
350 case SCSI_MLQUEUE_DEVICE_BUSY:
351 return -EAGAIN;
352 }
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900353 return 0;
354}
355
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900356static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask)
357{
358 struct request *rq = cmd->request;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900359 int count;
360
361 cmd->use_sg = rq->nr_phys_segments;
362 cmd->request_buffer = scsi_alloc_sgtable(cmd, gfp_mask);
363 if (!cmd->request_buffer)
364 return -ENOMEM;
365
366 cmd->request_bufflen = rq->data_len;
367
FUJITA Tomonorif10ab662007-10-25 01:21:30 +0900368 dprintk("cmd %p cnt %d %lu\n", cmd, scsi_sg_count(cmd),
369 rq_data_dir(rq));
370 count = blk_rq_map_sg(rq->q, rq, scsi_sglist(cmd));
Rusty Russell4a03d902007-11-19 11:28:48 +1100371 BUG_ON(count > cmd->use_sg);
372 cmd->use_sg = count;
373 return 0;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900374}
375
376/* TODO: test this crap and replace bio_map_user with new interface maybe */
377static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900378 unsigned long uaddr, unsigned int len, int rw)
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900379{
380 struct request_queue *q = cmd->request->q;
381 struct request *rq = cmd->request;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900382 int err;
383
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900384 dprintk("%lx %u\n", uaddr, len);
385 err = blk_rq_map_user(q, rq, (void *)uaddr, len);
Mike Christie181011e2007-03-03 09:55:54 +0900386 if (err) {
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900387 /*
Mike Christie181011e2007-03-03 09:55:54 +0900388 * TODO: need to fixup sg_tablesize, max_segment_size,
389 * max_sectors, etc for modern HW and software drivers
390 * where this value is bogus.
391 *
392 * TODO2: we can alloc a reserve buffer of max size
393 * we can handle and do the slow copy path for really large
394 * IO.
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900395 */
Mike Christie181011e2007-03-03 09:55:54 +0900396 eprintk("Could not handle request of size %u.\n", len);
397 return err;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900398 }
399
Mike Christie181011e2007-03-03 09:55:54 +0900400 tcmd->bio = rq->bio;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900401 err = scsi_tgt_init_cmd(cmd, GFP_KERNEL);
402 if (err)
Mike Christie181011e2007-03-03 09:55:54 +0900403 goto unmap_rq;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900404
405 return 0;
406
Mike Christie181011e2007-03-03 09:55:54 +0900407unmap_rq:
408 scsi_unmap_user_pages(tcmd);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900409 return err;
410}
411
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900412static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
413 unsigned len)
414{
415 char __user *p = (char __user *) uaddr;
416
417 if (copy_from_user(cmd->sense_buffer, p,
418 min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) {
419 printk(KERN_ERR "Could not copy the sense buffer\n");
420 return -EIO;
421 }
422 return 0;
423}
424
425static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
426{
David Howells06328b42006-12-06 15:02:26 +0000427 struct scsi_tgt_cmd *tcmd;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900428 int err;
429
430 err = shost->hostt->eh_abort_handler(cmd);
431 if (err)
432 eprintk("fail to abort %p\n", cmd);
433
David Howells06328b42006-12-06 15:02:26 +0000434 tcmd = cmd->request->end_io_data;
435 scsi_tgt_cmd_destroy(&tcmd->work);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900436 return err;
437}
438
439static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
440{
441 struct scsi_tgt_queuedata *qdata = q->queuedata;
442 struct request *rq = NULL;
443 struct list_head *head;
444 struct scsi_tgt_cmd *tcmd;
445 unsigned long flags;
446
447 head = &qdata->cmd_hash[cmd_hashfn(tag)];
448 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
449 list_for_each_entry(tcmd, head, hash_list) {
450 if (tcmd->tag == tag) {
451 rq = tcmd->rq;
452 list_del(&tcmd->hash_list);
453 break;
454 }
455 }
456 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
457
458 return rq;
459}
460
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900461int scsi_tgt_kspace_exec(int host_no, u64 itn_id, int result, u64 tag,
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900462 unsigned long uaddr, u32 len, unsigned long sense_uaddr,
463 u32 sense_len, u8 rw)
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900464{
465 struct Scsi_Host *shost;
466 struct scsi_cmnd *cmd;
467 struct request *rq;
468 struct scsi_tgt_cmd *tcmd;
469 int err = 0;
470
471 dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
472 result, len, uaddr, rw);
473
474 /* TODO: replace with a O(1) alg */
475 shost = scsi_host_lookup(host_no);
476 if (IS_ERR(shost)) {
477 printk(KERN_ERR "Could not find host no %d\n", host_no);
478 return -EINVAL;
479 }
480
481 if (!shost->uspace_req_q) {
482 printk(KERN_ERR "Not target scsi host %d\n", host_no);
483 goto done;
484 }
485
486 rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
487 if (!rq) {
488 printk(KERN_ERR "Could not find tag %llu\n",
489 (unsigned long long) tag);
490 err = -EINVAL;
491 goto done;
492 }
493 cmd = rq->special;
494
Randy Dunlapa8aae4d2007-11-15 11:53:25 +0200495 dprintk("cmd %p scb %x result %d len %d bufflen %u %u %x\n",
496 cmd, cmd->cmnd[0], result, len, scsi_bufflen(cmd),
Mike Christie181011e2007-03-03 09:55:54 +0900497 rq_data_dir(rq), cmd->cmnd[0]);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900498
499 if (result == TASK_ABORTED) {
500 scsi_tgt_abort_cmd(shost, cmd);
501 goto done;
502 }
503 /*
504 * store the userspace values here, the working values are
505 * in the request_* values
506 */
507 tcmd = cmd->request->end_io_data;
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900508 cmd->result = result;
509
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900510 if (cmd->result == SAM_STAT_CHECK_CONDITION)
511 scsi_tgt_copy_sense(cmd, sense_uaddr, sense_len);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900512
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900513 if (len) {
514 err = scsi_map_user_pages(rq->end_io_data, cmd, uaddr, len, rw);
515 if (err) {
FUJITA Tomonorie8f82482007-03-03 09:55:55 +0900516 /*
517 * user-space daemon bugs or OOM
518 * TODO: we can do better for OOM.
519 */
FUJITA Tomonoria52deca2007-03-13 10:07:15 +0900520 struct scsi_tgt_queuedata *qdata;
521 struct list_head *head;
522 unsigned long flags;
523
FUJITA Tomonorie8f82482007-03-03 09:55:55 +0900524 eprintk("cmd %p ret %d uaddr %lx len %d rw %d\n",
525 cmd, err, uaddr, len, rw);
FUJITA Tomonoria52deca2007-03-13 10:07:15 +0900526
527 qdata = shost->uspace_req_q->queuedata;
528 head = &qdata->cmd_hash[cmd_hashfn(tcmd->tag)];
529
530 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
531 list_add(&tcmd->hash_list, head);
532 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
533
534 goto done;
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900535 }
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900536 }
FUJITA Tomonoribc7e3802007-03-03 09:55:54 +0900537 err = scsi_tgt_transfer_response(cmd);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900538done:
539 scsi_host_put(shost);
540 return err;
541}
542
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900543int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, u64 itn_id,
544 int function, u64 tag, struct scsi_lun *scsilun,
545 void *data)
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900546{
547 int err;
548
549 /* TODO: need to retry if this fails. */
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900550 err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, itn_id,
551 function, tag, scsilun, data);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900552 if (err < 0)
553 eprintk("The task management request lost!\n");
554 return err;
555}
556EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
557
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900558int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 itn_id, u64 mid, int result)
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900559{
560 struct Scsi_Host *shost;
561 int err = -EINVAL;
562
563 dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
564
565 shost = scsi_host_lookup(host_no);
566 if (IS_ERR(shost)) {
567 printk(KERN_ERR "Could not find host no %d\n", host_no);
568 return err;
569 }
570
571 if (!shost->uspace_req_q) {
572 printk(KERN_ERR "Not target scsi host %d\n", host_no);
573 goto done;
574 }
575
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900576 err = shost->transportt->tsk_mgmt_response(shost, itn_id, mid, result);
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900577done:
578 scsi_host_put(shost);
579 return err;
580}
581
582int scsi_tgt_it_nexus_create(struct Scsi_Host *shost, u64 itn_id,
583 char *initiator)
584{
585 int err;
586
587 /* TODO: need to retry if this fails. */
588 err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no, itn_id, 0,
589 initiator);
590 if (err < 0)
591 eprintk("The i_t_neuxs request lost, %d %llx!\n",
592 shost->host_no, (unsigned long long)itn_id);
593 return err;
594}
595EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_create);
596
597int scsi_tgt_it_nexus_destroy(struct Scsi_Host *shost, u64 itn_id)
598{
599 int err;
600
601 /* TODO: need to retry if this fails. */
602 err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no,
603 itn_id, 1, NULL);
604 if (err < 0)
605 eprintk("The i_t_neuxs request lost, %d %llx!\n",
606 shost->host_no, (unsigned long long)itn_id);
607 return err;
608}
609EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_destroy);
610
611int scsi_tgt_kspace_it_nexus_rsp(int host_no, u64 itn_id, int result)
612{
613 struct Scsi_Host *shost;
614 int err = -EINVAL;
615
Randy Dunlapa8aae4d2007-11-15 11:53:25 +0200616 dprintk("%d %d%llx\n", host_no, result, (unsigned long long)itn_id);
FUJITA Tomonori2c47f9e2007-07-11 15:08:17 +0900617
618 shost = scsi_host_lookup(host_no);
619 if (IS_ERR(shost)) {
620 printk(KERN_ERR "Could not find host no %d\n", host_no);
621 return err;
622 }
623
624 if (!shost->uspace_req_q) {
625 printk(KERN_ERR "Not target scsi host %d\n", host_no);
626 goto done;
627 }
628
629 err = shost->transportt->it_nexus_response(shost, itn_id, result);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900630done:
631 scsi_host_put(shost);
632 return err;
633}
634
635static int __init scsi_tgt_init(void)
636{
637 int err;
638
639 scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd",
640 sizeof(struct scsi_tgt_cmd),
Paul Mundt20c2df82007-07-20 10:11:58 +0900641 0, 0, NULL);
FUJITA Tomonori5a55c252006-11-16 19:24:13 +0900642 if (!scsi_tgt_cmd_cache)
643 return -ENOMEM;
644
645 scsi_tgtd = create_workqueue("scsi_tgtd");
646 if (!scsi_tgtd) {
647 err = -ENOMEM;
648 goto free_kmemcache;
649 }
650
651 err = scsi_tgt_if_init();
652 if (err)
653 goto destroy_wq;
654
655 return 0;
656
657destroy_wq:
658 destroy_workqueue(scsi_tgtd);
659free_kmemcache:
660 kmem_cache_destroy(scsi_tgt_cmd_cache);
661 return err;
662}
663
664static void __exit scsi_tgt_exit(void)
665{
666 destroy_workqueue(scsi_tgtd);
667 scsi_tgt_if_exit();
668 kmem_cache_destroy(scsi_tgt_cmd_cache);
669}
670
671module_init(scsi_tgt_init);
672module_exit(scsi_tgt_exit);
673
674MODULE_DESCRIPTION("SCSI target core");
675MODULE_LICENSE("GPL");