]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/scsi_tgt_lib.c
[SCSI] fc_transport: update potential link speeds
[net-next-2.6.git] / drivers / scsi / scsi_tgt_lib.c
CommitLineData
5a55c259
FT
1/*
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>
30#include <scsi/scsi_tgt.h>
5a55c259
FT
31
32#include "scsi_tgt_priv.h"
33
34static struct workqueue_struct *scsi_tgtd;
e18b890b 35static struct kmem_cache *scsi_tgt_cmd_cache;
5a55c259
FT
36
37/*
38 * TODO: this struct will be killed when the block layer supports large bios
39 * and James's work struct code is in
40 */
41struct scsi_tgt_cmd {
42 /* TODO replace work with James b's code */
43 struct work_struct work;
181011e0
MC
44 /* TODO fix limits of some drivers */
45 struct bio *bio;
5a55c259
FT
46
47 struct list_head hash_list;
48 struct request *rq;
49 u64 tag;
5a55c259
FT
50};
51
52#define TGT_HASH_ORDER 4
53#define cmd_hashfn(tag) hash_long((unsigned long) (tag), TGT_HASH_ORDER)
54
55struct scsi_tgt_queuedata {
56 struct Scsi_Host *shost;
57 struct list_head cmd_hash[1 << TGT_HASH_ORDER];
58 spinlock_t cmd_hash_lock;
59};
60
61/*
62 * Function: scsi_host_get_command()
63 *
64 * Purpose: Allocate and setup a scsi command block and blk request
65 *
66 * Arguments: shost - scsi host
67 * data_dir - dma data dir
68 * gfp_mask- allocator flags
69 *
70 * Returns: The allocated scsi command structure.
71 *
72 * This should be called by target LLDs to get a command.
73 */
74struct scsi_cmnd *scsi_host_get_command(struct Scsi_Host *shost,
75 enum dma_data_direction data_dir,
76 gfp_t gfp_mask)
77{
78 int write = (data_dir == DMA_TO_DEVICE);
79 struct request *rq;
80 struct scsi_cmnd *cmd;
81 struct scsi_tgt_cmd *tcmd;
82
83 /* Bail if we can't get a reference to the device */
84 if (!get_device(&shost->shost_gendev))
85 return NULL;
86
87 tcmd = kmem_cache_alloc(scsi_tgt_cmd_cache, GFP_ATOMIC);
88 if (!tcmd)
89 goto put_dev;
90
181011e0
MC
91 /*
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.
95 */
96 rq = blk_get_request(shost->uspace_req_q, !write, gfp_mask);
5a55c259
FT
97 if (!rq)
98 goto free_tcmd;
99
100 cmd = __scsi_get_command(shost, gfp_mask);
101 if (!cmd)
102 goto release_rq;
103
104 memset(cmd, 0, sizeof(*cmd));
105 cmd->sc_data_direction = data_dir;
106 cmd->jiffies_at_alloc = jiffies;
107 cmd->request = rq;
108
109 rq->special = cmd;
110 rq->cmd_type = REQ_TYPE_SPECIAL;
111 rq->cmd_flags |= REQ_TYPE_BLOCK_PC;
112 rq->end_io_data = tcmd;
113
5a55c259
FT
114 tcmd->rq = rq;
115
116 return cmd;
117
118release_rq:
119 blk_put_request(rq);
120free_tcmd:
121 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
122put_dev:
123 put_device(&shost->shost_gendev);
124 return NULL;
125
126}
127EXPORT_SYMBOL_GPL(scsi_host_get_command);
128
129/*
130 * Function: scsi_host_put_command()
131 *
132 * Purpose: Free a scsi command block
133 *
134 * Arguments: shost - scsi host
135 * cmd - command block to free
136 *
137 * Returns: Nothing.
138 *
139 * Notes: The command must not belong to any lists.
140 */
141void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
142{
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;
146 unsigned long flags;
147
148 kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
149
150 spin_lock_irqsave(q->queue_lock, flags);
151 __blk_put_request(q, rq);
152 spin_unlock_irqrestore(q->queue_lock, flags);
153
154 __scsi_put_command(shost, cmd, &shost->shost_gendev);
155}
156EXPORT_SYMBOL_GPL(scsi_host_put_command);
157
5a55c259
FT
158static void cmd_hashlist_del(struct scsi_cmnd *cmd)
159{
160 struct request_queue *q = cmd->request->q;
161 struct scsi_tgt_queuedata *qdata = q->queuedata;
162 unsigned long flags;
163 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
164
165 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
166 list_del(&tcmd->hash_list);
167 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
168}
169
181011e0
MC
170static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd)
171{
172 blk_rq_unmap_user(tcmd->bio);
173}
174
06328b4f 175static void scsi_tgt_cmd_destroy(struct work_struct *work)
5a55c259 176{
06328b4f
DH
177 struct scsi_tgt_cmd *tcmd =
178 container_of(work, struct scsi_tgt_cmd, work);
179 struct scsi_cmnd *cmd = tcmd->rq->special;
5a55c259
FT
180
181 dprintk("cmd %p %d %lu\n", cmd, cmd->sc_data_direction,
182 rq_data_dir(cmd->request));
5a55c259
FT
183 scsi_unmap_user_pages(tcmd);
184 scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd);
185}
186
187static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd,
188 u64 tag)
189{
190 struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
191 unsigned long flags;
192 struct list_head *head;
193
194 tcmd->tag = tag;
181011e0 195 tcmd->bio = NULL;
06328b4f 196 INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy);
5a55c259
FT
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);
201}
202
203/*
204 * scsi_tgt_alloc_queue - setup queue used for message passing
205 * shost: scsi host
206 *
207 * This should be called by the LLD after host allocation.
208 * And will be released when the host is released.
209 */
210int scsi_tgt_alloc_queue(struct Scsi_Host *shost)
211{
212 struct scsi_tgt_queuedata *queuedata;
213 struct request_queue *q;
214 int err, i;
215
216 /*
217 * Do we need to send a netlink event or should uspace
218 * just respond to the hotplug event?
219 */
220 q = __scsi_alloc_queue(shost, NULL);
221 if (!q)
222 return -ENOMEM;
223
224 queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL);
225 if (!queuedata) {
226 err = -ENOMEM;
227 goto cleanup_queue;
228 }
229 queuedata->shost = shost;
230 q->queuedata = queuedata;
231
232 /*
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
236 */
237 q->nr_requests = shost->hostt->can_queue;
238 /*
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.
242 */
243 blk_queue_dma_alignment(q, 0);
244 shost->uspace_req_q = q;
245
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);
249
250 return 0;
251
252cleanup_queue:
253 blk_cleanup_queue(q);
254 return err;
255}
256EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue);
257
258void scsi_tgt_free_queue(struct Scsi_Host *shost)
259{
260 int i;
261 unsigned long flags;
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;
266 LIST_HEAD(cmds);
267
268 spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
269
270 for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) {
271 list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i],
272 hash_list) {
273 list_del(&tcmd->hash_list);
274 list_add(&tcmd->hash_list, &cmds);
275 }
276 }
277
278 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
279
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;
284
285 shost->hostt->eh_abort_handler(cmd);
06328b4f 286 scsi_tgt_cmd_destroy(&tcmd->work);
5a55c259
FT
287 }
288}
289EXPORT_SYMBOL_GPL(scsi_tgt_free_queue);
290
291struct Scsi_Host *scsi_tgt_cmd_to_host(struct scsi_cmnd *cmd)
292{
293 struct scsi_tgt_queuedata *queue = cmd->request->q->queuedata;
294 return queue->shost;
295}
296EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host);
297
298/*
299 * scsi_tgt_queue_command - queue command for userspace processing
300 * @cmd: scsi command
301 * @scsilun: scsi lun
302 * @tag: unique value to identify this command for tmf
303 */
304int scsi_tgt_queue_command(struct scsi_cmnd *cmd, struct scsi_lun *scsilun,
305 u64 tag)
306{
307 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
308 int err;
309
310 init_scsi_tgt_cmd(cmd->request, tcmd, tag);
311 err = scsi_tgt_uspace_send_cmd(cmd, scsilun, tag);
312 if (err)
313 cmd_hashlist_del(cmd);
314
315 return err;
316}
317EXPORT_SYMBOL_GPL(scsi_tgt_queue_command);
318
319/*
320 * This is run from a interrpt handler normally and the unmap
321 * needs process context so we must queue
322 */
323static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)
324{
325 struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
326
327 dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
328
329 scsi_tgt_uspace_send_status(cmd, tcmd->tag);
bc7e380a
FT
330
331 if (cmd->request_buffer)
332 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
333
5a55c259
FT
334 queue_work(scsi_tgtd, &tcmd->work);
335}
336
bc7e380a 337static int scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
5a55c259
FT
338{
339 struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
340 int err;
341
342 dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
343
344 err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done);
345 switch (err) {
346 case SCSI_MLQUEUE_HOST_BUSY:
347 case SCSI_MLQUEUE_DEVICE_BUSY:
348 return -EAGAIN;
349 }
5a55c259
FT
350 return 0;
351}
352
5a55c259
FT
353static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask)
354{
355 struct request *rq = cmd->request;
5a55c259
FT
356 int count;
357
358 cmd->use_sg = rq->nr_phys_segments;
359 cmd->request_buffer = scsi_alloc_sgtable(cmd, gfp_mask);
360 if (!cmd->request_buffer)
361 return -ENOMEM;
362
363 cmd->request_bufflen = rq->data_len;
364
bc7e380a 365 dprintk("cmd %p cnt %d %lu\n", cmd, cmd->use_sg, rq_data_dir(rq));
5a55c259
FT
366 count = blk_rq_map_sg(rq->q, rq, cmd->request_buffer);
367 if (likely(count <= cmd->use_sg)) {
368 cmd->use_sg = count;
369 return 0;
370 }
371
bc7e380a 372 eprintk("cmd %p cnt %d\n", cmd, cmd->use_sg);
5a55c259
FT
373 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
374 return -EINVAL;
375}
376
377/* TODO: test this crap and replace bio_map_user with new interface maybe */
378static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
bc7e380a 379 unsigned long uaddr, unsigned int len, int rw)
5a55c259
FT
380{
381 struct request_queue *q = cmd->request->q;
382 struct request *rq = cmd->request;
5a55c259
FT
383 int err;
384
bc7e380a
FT
385 dprintk("%lx %u\n", uaddr, len);
386 err = blk_rq_map_user(q, rq, (void *)uaddr, len);
181011e0 387 if (err) {
5a55c259 388 /*
181011e0
MC
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.
392 *
393 * TODO2: we can alloc a reserve buffer of max size
394 * we can handle and do the slow copy path for really large
395 * IO.
5a55c259 396 */
181011e0
MC
397 eprintk("Could not handle request of size %u.\n", len);
398 return err;
5a55c259
FT
399 }
400
181011e0 401 tcmd->bio = rq->bio;
5a55c259
FT
402 err = scsi_tgt_init_cmd(cmd, GFP_KERNEL);
403 if (err)
181011e0 404 goto unmap_rq;
5a55c259
FT
405
406 return 0;
407
181011e0
MC
408unmap_rq:
409 scsi_unmap_user_pages(tcmd);
5a55c259
FT
410 return err;
411}
412
5a55c259
FT
413static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
414 unsigned len)
415{
416 char __user *p = (char __user *) uaddr;
417
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");
421 return -EIO;
422 }
423 return 0;
424}
425
426static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
427{
06328b4f 428 struct scsi_tgt_cmd *tcmd;
5a55c259
FT
429 int err;
430
431 err = shost->hostt->eh_abort_handler(cmd);
432 if (err)
433 eprintk("fail to abort %p\n", cmd);
434
06328b4f
DH
435 tcmd = cmd->request->end_io_data;
436 scsi_tgt_cmd_destroy(&tcmd->work);
5a55c259
FT
437 return err;
438}
439
440static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
441{
442 struct scsi_tgt_queuedata *qdata = q->queuedata;
443 struct request *rq = NULL;
444 struct list_head *head;
445 struct scsi_tgt_cmd *tcmd;
446 unsigned long flags;
447
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) {
452 rq = tcmd->rq;
453 list_del(&tcmd->hash_list);
454 break;
455 }
456 }
457 spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
458
459 return rq;
460}
461
e8f8248c
FT
462static void scsi_tgt_build_sense(unsigned char *sense_buffer, unsigned char key,
463 unsigned char asc, unsigned char asq)
464{
465 sense_buffer[0] = 0x70;
466 sense_buffer[2] = key;
467 sense_buffer[7] = 0xa;
468 sense_buffer[12] = asc;
469 sense_buffer[13] = asq;
470}
471
bc7e380a
FT
472int scsi_tgt_kspace_exec(int host_no, int result, u64 tag,
473 unsigned long uaddr, u32 len, unsigned long sense_uaddr,
474 u32 sense_len, u8 rw)
5a55c259
FT
475{
476 struct Scsi_Host *shost;
477 struct scsi_cmnd *cmd;
478 struct request *rq;
479 struct scsi_tgt_cmd *tcmd;
480 int err = 0;
481
482 dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
483 result, len, uaddr, rw);
484
485 /* TODO: replace with a O(1) alg */
486 shost = scsi_host_lookup(host_no);
487 if (IS_ERR(shost)) {
488 printk(KERN_ERR "Could not find host no %d\n", host_no);
489 return -EINVAL;
490 }
491
492 if (!shost->uspace_req_q) {
493 printk(KERN_ERR "Not target scsi host %d\n", host_no);
494 goto done;
495 }
496
497 rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
498 if (!rq) {
499 printk(KERN_ERR "Could not find tag %llu\n",
500 (unsigned long long) tag);
501 err = -EINVAL;
502 goto done;
503 }
504 cmd = rq->special;
505
181011e0
MC
506 dprintk("cmd %p scb %x result %d len %d bufflen %u %lu %x\n",
507 cmd, cmd->cmnd[0], result, len, cmd->request_bufflen,
508 rq_data_dir(rq), cmd->cmnd[0]);
5a55c259
FT
509
510 if (result == TASK_ABORTED) {
511 scsi_tgt_abort_cmd(shost, cmd);
512 goto done;
513 }
514 /*
515 * store the userspace values here, the working values are
516 * in the request_* values
517 */
518 tcmd = cmd->request->end_io_data;
5a55c259
FT
519 cmd->result = result;
520
bc7e380a
FT
521 if (cmd->result == SAM_STAT_CHECK_CONDITION)
522 scsi_tgt_copy_sense(cmd, sense_uaddr, sense_len);
5a55c259 523
bc7e380a
FT
524 if (len) {
525 err = scsi_map_user_pages(rq->end_io_data, cmd, uaddr, len, rw);
526 if (err) {
e8f8248c
FT
527 /*
528 * user-space daemon bugs or OOM
529 * TODO: we can do better for OOM.
530 */
531 eprintk("cmd %p ret %d uaddr %lx len %d rw %d\n",
532 cmd, err, uaddr, len, rw);
533 cmd->result = SAM_STAT_CHECK_CONDITION;
534 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
535 scsi_tgt_build_sense(cmd->sense_buffer,
536 HARDWARE_ERROR, 0, 0);
bc7e380a 537 }
5a55c259 538 }
bc7e380a 539 err = scsi_tgt_transfer_response(cmd);
5a55c259
FT
540done:
541 scsi_host_put(shost);
542 return err;
543}
544
545int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, int function, u64 tag,
546 struct scsi_lun *scsilun, void *data)
547{
548 int err;
549
550 /* TODO: need to retry if this fails. */
551 err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, function,
552 tag, scsilun, data);
553 if (err < 0)
554 eprintk("The task management request lost!\n");
555 return err;
556}
557EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
558
559int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 mid, int result)
560{
561 struct Scsi_Host *shost;
562 int err = -EINVAL;
563
564 dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
565
566 shost = scsi_host_lookup(host_no);
567 if (IS_ERR(shost)) {
568 printk(KERN_ERR "Could not find host no %d\n", host_no);
569 return err;
570 }
571
572 if (!shost->uspace_req_q) {
573 printk(KERN_ERR "Not target scsi host %d\n", host_no);
574 goto done;
575 }
576
577 err = shost->hostt->tsk_mgmt_response(mid, result);
578done:
579 scsi_host_put(shost);
580 return err;
581}
582
583static int __init scsi_tgt_init(void)
584{
585 int err;
586
587 scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd",
588 sizeof(struct scsi_tgt_cmd),
589 0, 0, NULL, NULL);
590 if (!scsi_tgt_cmd_cache)
591 return -ENOMEM;
592
593 scsi_tgtd = create_workqueue("scsi_tgtd");
594 if (!scsi_tgtd) {
595 err = -ENOMEM;
596 goto free_kmemcache;
597 }
598
599 err = scsi_tgt_if_init();
600 if (err)
601 goto destroy_wq;
602
603 return 0;
604
605destroy_wq:
606 destroy_workqueue(scsi_tgtd);
607free_kmemcache:
608 kmem_cache_destroy(scsi_tgt_cmd_cache);
609 return err;
610}
611
612static void __exit scsi_tgt_exit(void)
613{
614 destroy_workqueue(scsi_tgtd);
615 scsi_tgt_if_exit();
616 kmem_cache_destroy(scsi_tgt_cmd_cache);
617}
618
619module_init(scsi_tgt_init);
620module_exit(scsi_tgt_exit);
621
622MODULE_DESCRIPTION("SCSI target core");
623MODULE_LICENSE("GPL");