]>
Commit | Line | Data |
---|---|---|
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 | ||
34 | static struct workqueue_struct *scsi_tgtd; | |
e18b890b | 35 | static 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 | */ | |
41 | struct 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; | |
50 | ||
51 | void *buffer; | |
52 | unsigned bufflen; | |
53 | }; | |
54 | ||
55 | #define TGT_HASH_ORDER 4 | |
56 | #define cmd_hashfn(tag) hash_long((unsigned long) (tag), TGT_HASH_ORDER) | |
57 | ||
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; | |
62 | }; | |
63 | ||
64 | /* | |
65 | * Function: scsi_host_get_command() | |
66 | * | |
67 | * Purpose: Allocate and setup a scsi command block and blk request | |
68 | * | |
69 | * Arguments: shost - scsi host | |
70 | * data_dir - dma data dir | |
71 | * gfp_mask- allocator flags | |
72 | * | |
73 | * Returns: The allocated scsi command structure. | |
74 | * | |
75 | * This should be called by target LLDs to get a command. | |
76 | */ | |
77 | struct scsi_cmnd *scsi_host_get_command(struct Scsi_Host *shost, | |
78 | enum dma_data_direction data_dir, | |
79 | gfp_t gfp_mask) | |
80 | { | |
81 | int write = (data_dir == DMA_TO_DEVICE); | |
82 | struct request *rq; | |
83 | struct scsi_cmnd *cmd; | |
84 | struct scsi_tgt_cmd *tcmd; | |
85 | ||
86 | /* Bail if we can't get a reference to the device */ | |
87 | if (!get_device(&shost->shost_gendev)) | |
88 | return NULL; | |
89 | ||
90 | tcmd = kmem_cache_alloc(scsi_tgt_cmd_cache, GFP_ATOMIC); | |
91 | if (!tcmd) | |
92 | goto put_dev; | |
93 | ||
181011e0 MC |
94 | /* |
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. | |
98 | */ | |
99 | rq = blk_get_request(shost->uspace_req_q, !write, gfp_mask); | |
5a55c259 FT |
100 | if (!rq) |
101 | goto free_tcmd; | |
102 | ||
103 | cmd = __scsi_get_command(shost, gfp_mask); | |
104 | if (!cmd) | |
105 | goto release_rq; | |
106 | ||
107 | memset(cmd, 0, sizeof(*cmd)); | |
108 | cmd->sc_data_direction = data_dir; | |
109 | cmd->jiffies_at_alloc = jiffies; | |
110 | cmd->request = rq; | |
111 | ||
112 | rq->special = cmd; | |
113 | rq->cmd_type = REQ_TYPE_SPECIAL; | |
114 | rq->cmd_flags |= REQ_TYPE_BLOCK_PC; | |
115 | rq->end_io_data = tcmd; | |
116 | ||
5a55c259 FT |
117 | tcmd->rq = rq; |
118 | ||
119 | return cmd; | |
120 | ||
121 | release_rq: | |
122 | blk_put_request(rq); | |
123 | free_tcmd: | |
124 | kmem_cache_free(scsi_tgt_cmd_cache, tcmd); | |
125 | put_dev: | |
126 | put_device(&shost->shost_gendev); | |
127 | return NULL; | |
128 | ||
129 | } | |
130 | EXPORT_SYMBOL_GPL(scsi_host_get_command); | |
131 | ||
132 | /* | |
133 | * Function: scsi_host_put_command() | |
134 | * | |
135 | * Purpose: Free a scsi command block | |
136 | * | |
137 | * Arguments: shost - scsi host | |
138 | * cmd - command block to free | |
139 | * | |
140 | * Returns: Nothing. | |
141 | * | |
142 | * Notes: The command must not belong to any lists. | |
143 | */ | |
144 | void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd) | |
145 | { | |
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; | |
149 | unsigned long flags; | |
150 | ||
151 | kmem_cache_free(scsi_tgt_cmd_cache, tcmd); | |
152 | ||
153 | spin_lock_irqsave(q->queue_lock, flags); | |
154 | __blk_put_request(q, rq); | |
155 | spin_unlock_irqrestore(q->queue_lock, flags); | |
156 | ||
157 | __scsi_put_command(shost, cmd, &shost->shost_gendev); | |
158 | } | |
159 | EXPORT_SYMBOL_GPL(scsi_host_put_command); | |
160 | ||
5a55c259 FT |
161 | static void cmd_hashlist_del(struct scsi_cmnd *cmd) |
162 | { | |
163 | struct request_queue *q = cmd->request->q; | |
164 | struct scsi_tgt_queuedata *qdata = q->queuedata; | |
165 | unsigned long flags; | |
166 | struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data; | |
167 | ||
168 | spin_lock_irqsave(&qdata->cmd_hash_lock, flags); | |
169 | list_del(&tcmd->hash_list); | |
170 | spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags); | |
171 | } | |
172 | ||
181011e0 MC |
173 | static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd) |
174 | { | |
175 | blk_rq_unmap_user(tcmd->bio); | |
176 | } | |
177 | ||
06328b4f | 178 | static void scsi_tgt_cmd_destroy(struct work_struct *work) |
5a55c259 | 179 | { |
06328b4f DH |
180 | struct scsi_tgt_cmd *tcmd = |
181 | container_of(work, struct scsi_tgt_cmd, work); | |
182 | struct scsi_cmnd *cmd = tcmd->rq->special; | |
5a55c259 FT |
183 | |
184 | dprintk("cmd %p %d %lu\n", cmd, cmd->sc_data_direction, | |
185 | rq_data_dir(cmd->request)); | |
5a55c259 FT |
186 | scsi_unmap_user_pages(tcmd); |
187 | scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd); | |
188 | } | |
189 | ||
190 | static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd, | |
191 | u64 tag) | |
192 | { | |
193 | struct scsi_tgt_queuedata *qdata = rq->q->queuedata; | |
194 | unsigned long flags; | |
195 | struct list_head *head; | |
196 | ||
197 | tcmd->tag = tag; | |
181011e0 | 198 | tcmd->bio = NULL; |
06328b4f | 199 | INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy); |
5a55c259 FT |
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); | |
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 | */ | |
213 | int 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 | */ | |
240 | q->nr_requests = shost->hostt->can_queue; | |
241 | /* | |
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 | ||
255 | cleanup_queue: | |
256 | blk_cleanup_queue(q); | |
257 | return err; | |
258 | } | |
259 | EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue); | |
260 | ||
261 | void 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); | |
06328b4f | 289 | scsi_tgt_cmd_destroy(&tcmd->work); |
5a55c259 FT |
290 | } |
291 | } | |
292 | EXPORT_SYMBOL_GPL(scsi_tgt_free_queue); | |
293 | ||
294 | struct 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 | } | |
299 | EXPORT_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 | */ | |
307 | int scsi_tgt_queue_command(struct scsi_cmnd *cmd, struct scsi_lun *scsilun, | |
308 | u64 tag) | |
309 | { | |
310 | struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data; | |
311 | int err; | |
312 | ||
313 | init_scsi_tgt_cmd(cmd->request, tcmd, tag); | |
314 | err = scsi_tgt_uspace_send_cmd(cmd, scsilun, tag); | |
315 | if (err) | |
316 | cmd_hashlist_del(cmd); | |
317 | ||
318 | return err; | |
319 | } | |
320 | EXPORT_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 | */ | |
326 | static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd) | |
327 | { | |
328 | struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data; | |
329 | ||
330 | dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request)); | |
331 | ||
332 | scsi_tgt_uspace_send_status(cmd, tcmd->tag); | |
5a55c259 FT |
333 | queue_work(scsi_tgtd, &tcmd->work); |
334 | } | |
335 | ||
336 | static int __scsi_tgt_transfer_response(struct scsi_cmnd *cmd) | |
337 | { | |
338 | struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd); | |
339 | int err; | |
340 | ||
341 | dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request)); | |
342 | ||
343 | err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done); | |
344 | switch (err) { | |
345 | case SCSI_MLQUEUE_HOST_BUSY: | |
346 | case SCSI_MLQUEUE_DEVICE_BUSY: | |
347 | return -EAGAIN; | |
348 | } | |
349 | ||
350 | return 0; | |
351 | } | |
352 | ||
353 | static void scsi_tgt_transfer_response(struct scsi_cmnd *cmd) | |
354 | { | |
355 | struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data; | |
356 | int err; | |
357 | ||
358 | err = __scsi_tgt_transfer_response(cmd); | |
359 | if (!err) | |
360 | return; | |
361 | ||
362 | cmd->result = DID_BUS_BUSY << 16; | |
363 | err = scsi_tgt_uspace_send_status(cmd, tcmd->tag); | |
364 | if (err <= 0) | |
365 | /* the eh will have to pick this up */ | |
366 | printk(KERN_ERR "Could not send cmd %p status\n", cmd); | |
367 | } | |
368 | ||
369 | static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask) | |
370 | { | |
371 | struct request *rq = cmd->request; | |
372 | struct scsi_tgt_cmd *tcmd = rq->end_io_data; | |
373 | int count; | |
374 | ||
375 | cmd->use_sg = rq->nr_phys_segments; | |
376 | cmd->request_buffer = scsi_alloc_sgtable(cmd, gfp_mask); | |
377 | if (!cmd->request_buffer) | |
378 | return -ENOMEM; | |
379 | ||
380 | cmd->request_bufflen = rq->data_len; | |
381 | ||
382 | dprintk("cmd %p addr %p cnt %d %lu\n", cmd, tcmd->buffer, cmd->use_sg, | |
383 | rq_data_dir(rq)); | |
384 | count = blk_rq_map_sg(rq->q, rq, cmd->request_buffer); | |
385 | if (likely(count <= cmd->use_sg)) { | |
386 | cmd->use_sg = count; | |
387 | return 0; | |
388 | } | |
389 | ||
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); | |
392 | return -EINVAL; | |
393 | } | |
394 | ||
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, | |
397 | int rw) | |
398 | { | |
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; | |
5a55c259 FT |
403 | int err; |
404 | ||
181011e0 MC |
405 | dprintk("%lx %u\n", (unsigned long) uaddr, len); |
406 | err = blk_rq_map_user(q, rq, uaddr, len); | |
407 | if (err) { | |
5a55c259 | 408 | /* |
181011e0 MC |
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. | |
412 | * | |
413 | * TODO2: we can alloc a reserve buffer of max size | |
414 | * we can handle and do the slow copy path for really large | |
415 | * IO. | |
5a55c259 | 416 | */ |
181011e0 MC |
417 | eprintk("Could not handle request of size %u.\n", len); |
418 | return err; | |
5a55c259 FT |
419 | } |
420 | ||
181011e0 | 421 | tcmd->bio = rq->bio; |
5a55c259 FT |
422 | err = scsi_tgt_init_cmd(cmd, GFP_KERNEL); |
423 | if (err) | |
181011e0 | 424 | goto unmap_rq; |
5a55c259 FT |
425 | |
426 | return 0; | |
427 | ||
181011e0 MC |
428 | unmap_rq: |
429 | scsi_unmap_user_pages(tcmd); | |
5a55c259 FT |
430 | return err; |
431 | } | |
432 | ||
433 | static int scsi_tgt_transfer_data(struct scsi_cmnd *); | |
434 | ||
435 | static void scsi_tgt_data_transfer_done(struct scsi_cmnd *cmd) | |
436 | { | |
437 | struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data; | |
5a55c259 FT |
438 | int err; |
439 | ||
440 | /* should we free resources here on error ? */ | |
441 | if (cmd->result) { | |
5a55c259 FT |
442 | err = scsi_tgt_uspace_send_status(cmd, tcmd->tag); |
443 | if (err <= 0) | |
444 | /* the tgt uspace eh will have to pick this up */ | |
445 | printk(KERN_ERR "Could not send cmd %p status\n", cmd); | |
446 | return; | |
447 | } | |
448 | ||
449 | dprintk("cmd %p request_bufflen %u bufflen %u\n", | |
450 | cmd, cmd->request_bufflen, tcmd->bufflen); | |
451 | ||
452 | scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len); | |
5a55c259 | 453 | tcmd->buffer += cmd->request_bufflen; |
181011e0 | 454 | scsi_tgt_transfer_response(cmd); |
5a55c259 FT |
455 | } |
456 | ||
457 | static int scsi_tgt_transfer_data(struct scsi_cmnd *cmd) | |
458 | { | |
459 | int err; | |
460 | struct Scsi_Host *host = scsi_tgt_cmd_to_host(cmd); | |
461 | ||
462 | err = host->hostt->transfer_data(cmd, scsi_tgt_data_transfer_done); | |
463 | switch (err) { | |
464 | case SCSI_MLQUEUE_HOST_BUSY: | |
465 | case SCSI_MLQUEUE_DEVICE_BUSY: | |
466 | return -EAGAIN; | |
467 | default: | |
468 | return 0; | |
469 | } | |
470 | } | |
471 | ||
472 | static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr, | |
473 | unsigned len) | |
474 | { | |
475 | char __user *p = (char __user *) uaddr; | |
476 | ||
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"); | |
480 | return -EIO; | |
481 | } | |
482 | return 0; | |
483 | } | |
484 | ||
485 | static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd) | |
486 | { | |
06328b4f | 487 | struct scsi_tgt_cmd *tcmd; |
5a55c259 FT |
488 | int err; |
489 | ||
490 | err = shost->hostt->eh_abort_handler(cmd); | |
491 | if (err) | |
492 | eprintk("fail to abort %p\n", cmd); | |
493 | ||
06328b4f DH |
494 | tcmd = cmd->request->end_io_data; |
495 | scsi_tgt_cmd_destroy(&tcmd->work); | |
5a55c259 FT |
496 | return err; |
497 | } | |
498 | ||
499 | static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag) | |
500 | { | |
501 | struct scsi_tgt_queuedata *qdata = q->queuedata; | |
502 | struct request *rq = NULL; | |
503 | struct list_head *head; | |
504 | struct scsi_tgt_cmd *tcmd; | |
505 | unsigned long flags; | |
506 | ||
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) { | |
511 | rq = tcmd->rq; | |
512 | list_del(&tcmd->hash_list); | |
513 | break; | |
514 | } | |
515 | } | |
516 | spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags); | |
517 | ||
518 | return rq; | |
519 | } | |
520 | ||
521 | int scsi_tgt_kspace_exec(int host_no, u64 tag, int result, u32 len, | |
522 | unsigned long uaddr, u8 rw) | |
523 | { | |
524 | struct Scsi_Host *shost; | |
525 | struct scsi_cmnd *cmd; | |
526 | struct request *rq; | |
527 | struct scsi_tgt_cmd *tcmd; | |
528 | int err = 0; | |
529 | ||
530 | dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag, | |
531 | result, len, uaddr, rw); | |
532 | ||
533 | /* TODO: replace with a O(1) alg */ | |
534 | shost = scsi_host_lookup(host_no); | |
535 | if (IS_ERR(shost)) { | |
536 | printk(KERN_ERR "Could not find host no %d\n", host_no); | |
537 | return -EINVAL; | |
538 | } | |
539 | ||
540 | if (!shost->uspace_req_q) { | |
541 | printk(KERN_ERR "Not target scsi host %d\n", host_no); | |
542 | goto done; | |
543 | } | |
544 | ||
545 | rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag); | |
546 | if (!rq) { | |
547 | printk(KERN_ERR "Could not find tag %llu\n", | |
548 | (unsigned long long) tag); | |
549 | err = -EINVAL; | |
550 | goto done; | |
551 | } | |
552 | cmd = rq->special; | |
553 | ||
181011e0 MC |
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]); | |
5a55c259 FT |
557 | |
558 | if (result == TASK_ABORTED) { | |
559 | scsi_tgt_abort_cmd(shost, cmd); | |
560 | goto done; | |
561 | } | |
562 | /* | |
563 | * store the userspace values here, the working values are | |
564 | * in the request_* values | |
565 | */ | |
566 | tcmd = cmd->request->end_io_data; | |
567 | tcmd->buffer = (void *)uaddr; | |
568 | tcmd->bufflen = len; | |
569 | cmd->result = result; | |
570 | ||
571 | if (!tcmd->bufflen || cmd->request_buffer) { | |
572 | err = __scsi_tgt_transfer_response(cmd); | |
573 | goto done; | |
574 | } | |
575 | ||
576 | /* | |
577 | * TODO: Do we need to handle case where request does not | |
578 | * align with LLD. | |
579 | */ | |
580 | err = scsi_map_user_pages(rq->end_io_data, cmd, rw); | |
581 | if (err) { | |
582 | eprintk("%p %d\n", cmd, err); | |
583 | err = -EAGAIN; | |
584 | goto done; | |
585 | } | |
586 | ||
587 | /* userspace failure */ | |
588 | if (cmd->result) { | |
589 | if (status_byte(cmd->result) == CHECK_CONDITION) | |
590 | scsi_tgt_copy_sense(cmd, uaddr, len); | |
591 | err = __scsi_tgt_transfer_response(cmd); | |
592 | goto done; | |
593 | } | |
594 | /* ask the target LLD to transfer the data to the buffer */ | |
595 | err = scsi_tgt_transfer_data(cmd); | |
596 | ||
597 | done: | |
598 | scsi_host_put(shost); | |
599 | return err; | |
600 | } | |
601 | ||
602 | int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, int function, u64 tag, | |
603 | struct scsi_lun *scsilun, void *data) | |
604 | { | |
605 | int err; | |
606 | ||
607 | /* TODO: need to retry if this fails. */ | |
608 | err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, function, | |
609 | tag, scsilun, data); | |
610 | if (err < 0) | |
611 | eprintk("The task management request lost!\n"); | |
612 | return err; | |
613 | } | |
614 | EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request); | |
615 | ||
616 | int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 mid, int result) | |
617 | { | |
618 | struct Scsi_Host *shost; | |
619 | int err = -EINVAL; | |
620 | ||
621 | dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid); | |
622 | ||
623 | shost = scsi_host_lookup(host_no); | |
624 | if (IS_ERR(shost)) { | |
625 | printk(KERN_ERR "Could not find host no %d\n", host_no); | |
626 | return err; | |
627 | } | |
628 | ||
629 | if (!shost->uspace_req_q) { | |
630 | printk(KERN_ERR "Not target scsi host %d\n", host_no); | |
631 | goto done; | |
632 | } | |
633 | ||
634 | err = shost->hostt->tsk_mgmt_response(mid, result); | |
635 | done: | |
636 | scsi_host_put(shost); | |
637 | return err; | |
638 | } | |
639 | ||
640 | static int __init scsi_tgt_init(void) | |
641 | { | |
642 | int err; | |
643 | ||
644 | scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd", | |
645 | sizeof(struct scsi_tgt_cmd), | |
646 | 0, 0, NULL, NULL); | |
647 | if (!scsi_tgt_cmd_cache) | |
648 | return -ENOMEM; | |
649 | ||
650 | scsi_tgtd = create_workqueue("scsi_tgtd"); | |
651 | if (!scsi_tgtd) { | |
652 | err = -ENOMEM; | |
653 | goto free_kmemcache; | |
654 | } | |
655 | ||
656 | err = scsi_tgt_if_init(); | |
657 | if (err) | |
658 | goto destroy_wq; | |
659 | ||
660 | return 0; | |
661 | ||
662 | destroy_wq: | |
663 | destroy_workqueue(scsi_tgtd); | |
664 | free_kmemcache: | |
665 | kmem_cache_destroy(scsi_tgt_cmd_cache); | |
666 | return err; | |
667 | } | |
668 | ||
669 | static void __exit scsi_tgt_exit(void) | |
670 | { | |
671 | destroy_workqueue(scsi_tgtd); | |
672 | scsi_tgt_if_exit(); | |
673 | kmem_cache_destroy(scsi_tgt_cmd_cache); | |
674 | } | |
675 | ||
676 | module_init(scsi_tgt_init); | |
677 | module_exit(scsi_tgt_exit); | |
678 | ||
679 | MODULE_DESCRIPTION("SCSI target core"); | |
680 | MODULE_LICENSE("GPL"); |