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> | |
2c47f9ef | 30 | #include <scsi/scsi_transport.h> |
5a55c259 | 31 | #include <scsi/scsi_tgt.h> |
5a55c259 FT |
32 | |
33 | #include "scsi_tgt_priv.h" | |
34 | ||
35 | static struct workqueue_struct *scsi_tgtd; | |
e18b890b | 36 | static struct kmem_cache *scsi_tgt_cmd_cache; |
5a55c259 FT |
37 | |
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 | */ | |
42 | struct scsi_tgt_cmd { | |
43 | /* TODO replace work with James b's code */ | |
44 | struct work_struct work; | |
181011e0 MC |
45 | /* TODO fix limits of some drivers */ |
46 | struct bio *bio; | |
5a55c259 FT |
47 | |
48 | struct list_head hash_list; | |
49 | struct request *rq; | |
2c47f9ef | 50 | u64 itn_id; |
5a55c259 | 51 | u64 tag; |
5a55c259 FT |
52 | }; |
53 | ||
54 | #define TGT_HASH_ORDER 4 | |
55 | #define cmd_hashfn(tag) hash_long((unsigned long) (tag), TGT_HASH_ORDER) | |
56 | ||
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; | |
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 | */ | |
76 | struct 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 | ||
181011e0 MC |
93 | /* |
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); | |
5a55c259 FT |
99 | 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 | ||
5a55c259 FT |
116 | tcmd->rq = rq; |
117 | ||
118 | return cmd; | |
119 | ||
120 | release_rq: | |
121 | blk_put_request(rq); | |
122 | free_tcmd: | |
123 | kmem_cache_free(scsi_tgt_cmd_cache, tcmd); | |
124 | put_dev: | |
125 | put_device(&shost->shost_gendev); | |
126 | return NULL; | |
127 | ||
128 | } | |
129 | EXPORT_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 | */ | |
143 | void 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 | } | |
158 | EXPORT_SYMBOL_GPL(scsi_host_put_command); | |
159 | ||
5a55c259 FT |
160 | static 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 | ||
181011e0 MC |
172 | static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd) |
173 | { | |
174 | blk_rq_unmap_user(tcmd->bio); | |
175 | } | |
176 | ||
06328b4f | 177 | static void scsi_tgt_cmd_destroy(struct work_struct *work) |
5a55c259 | 178 | { |
06328b4f DH |
179 | struct scsi_tgt_cmd *tcmd = |
180 | container_of(work, struct scsi_tgt_cmd, work); | |
181 | struct scsi_cmnd *cmd = tcmd->rq->special; | |
5a55c259 FT |
182 | |
183 | dprintk("cmd %p %d %lu\n", cmd, cmd->sc_data_direction, | |
184 | rq_data_dir(cmd->request)); | |
5a55c259 FT |
185 | scsi_unmap_user_pages(tcmd); |
186 | scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd); | |
187 | } | |
188 | ||
189 | static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd, | |
2c47f9ef | 190 | u64 itn_id, u64 tag) |
5a55c259 FT |
191 | { |
192 | struct scsi_tgt_queuedata *qdata = rq->q->queuedata; | |
193 | unsigned long flags; | |
194 | struct list_head *head; | |
195 | ||
2c47f9ef | 196 | tcmd->itn_id = itn_id; |
5a55c259 | 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 | */ | |
2c47f9ef FT |
307 | int scsi_tgt_queue_command(struct scsi_cmnd *cmd, u64 itn_id, |
308 | struct scsi_lun *scsilun, u64 tag) | |
5a55c259 FT |
309 | { |
310 | struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data; | |
311 | int err; | |
312 | ||
2c47f9ef FT |
313 | init_scsi_tgt_cmd(cmd->request, tcmd, itn_id, tag); |
314 | err = scsi_tgt_uspace_send_cmd(cmd, itn_id, scsilun, tag); | |
5a55c259 FT |
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 | ||
2c47f9ef | 332 | scsi_tgt_uspace_send_status(cmd, tcmd->itn_id, tcmd->tag); |
bc7e380a FT |
333 | |
334 | if (cmd->request_buffer) | |
335 | scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len); | |
336 | ||
5a55c259 FT |
337 | queue_work(scsi_tgtd, &tcmd->work); |
338 | } | |
339 | ||
bc7e380a | 340 | static int scsi_tgt_transfer_response(struct scsi_cmnd *cmd) |
5a55c259 FT |
341 | { |
342 | struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd); | |
343 | int err; | |
344 | ||
345 | dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request)); | |
346 | ||
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 | } | |
5a55c259 FT |
353 | return 0; |
354 | } | |
355 | ||
5a55c259 FT |
356 | static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask) |
357 | { | |
358 | struct request *rq = cmd->request; | |
5a55c259 FT |
359 | 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 | ||
bc7e380a | 368 | dprintk("cmd %p cnt %d %lu\n", cmd, cmd->use_sg, rq_data_dir(rq)); |
5a55c259 FT |
369 | count = blk_rq_map_sg(rq->q, rq, cmd->request_buffer); |
370 | if (likely(count <= cmd->use_sg)) { | |
371 | cmd->use_sg = count; | |
372 | return 0; | |
373 | } | |
374 | ||
bc7e380a | 375 | eprintk("cmd %p cnt %d\n", cmd, cmd->use_sg); |
5a55c259 FT |
376 | scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len); |
377 | return -EINVAL; | |
378 | } | |
379 | ||
380 | /* TODO: test this crap and replace bio_map_user with new interface maybe */ | |
381 | static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd, | |
bc7e380a | 382 | unsigned long uaddr, unsigned int len, int rw) |
5a55c259 FT |
383 | { |
384 | struct request_queue *q = cmd->request->q; | |
385 | struct request *rq = cmd->request; | |
5a55c259 FT |
386 | int err; |
387 | ||
bc7e380a FT |
388 | dprintk("%lx %u\n", uaddr, len); |
389 | err = blk_rq_map_user(q, rq, (void *)uaddr, len); | |
181011e0 | 390 | if (err) { |
5a55c259 | 391 | /* |
181011e0 MC |
392 | * TODO: need to fixup sg_tablesize, max_segment_size, |
393 | * max_sectors, etc for modern HW and software drivers | |
394 | * where this value is bogus. | |
395 | * | |
396 | * TODO2: we can alloc a reserve buffer of max size | |
397 | * we can handle and do the slow copy path for really large | |
398 | * IO. | |
5a55c259 | 399 | */ |
181011e0 MC |
400 | eprintk("Could not handle request of size %u.\n", len); |
401 | return err; | |
5a55c259 FT |
402 | } |
403 | ||
181011e0 | 404 | tcmd->bio = rq->bio; |
5a55c259 FT |
405 | err = scsi_tgt_init_cmd(cmd, GFP_KERNEL); |
406 | if (err) | |
181011e0 | 407 | goto unmap_rq; |
5a55c259 FT |
408 | |
409 | return 0; | |
410 | ||
181011e0 MC |
411 | unmap_rq: |
412 | scsi_unmap_user_pages(tcmd); | |
5a55c259 FT |
413 | return err; |
414 | } | |
415 | ||
5a55c259 FT |
416 | static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr, |
417 | unsigned len) | |
418 | { | |
419 | char __user *p = (char __user *) uaddr; | |
420 | ||
421 | if (copy_from_user(cmd->sense_buffer, p, | |
422 | min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) { | |
423 | printk(KERN_ERR "Could not copy the sense buffer\n"); | |
424 | return -EIO; | |
425 | } | |
426 | return 0; | |
427 | } | |
428 | ||
429 | static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd) | |
430 | { | |
06328b4f | 431 | struct scsi_tgt_cmd *tcmd; |
5a55c259 FT |
432 | int err; |
433 | ||
434 | err = shost->hostt->eh_abort_handler(cmd); | |
435 | if (err) | |
436 | eprintk("fail to abort %p\n", cmd); | |
437 | ||
06328b4f DH |
438 | tcmd = cmd->request->end_io_data; |
439 | scsi_tgt_cmd_destroy(&tcmd->work); | |
5a55c259 FT |
440 | return err; |
441 | } | |
442 | ||
443 | static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag) | |
444 | { | |
445 | struct scsi_tgt_queuedata *qdata = q->queuedata; | |
446 | struct request *rq = NULL; | |
447 | struct list_head *head; | |
448 | struct scsi_tgt_cmd *tcmd; | |
449 | unsigned long flags; | |
450 | ||
451 | head = &qdata->cmd_hash[cmd_hashfn(tag)]; | |
452 | spin_lock_irqsave(&qdata->cmd_hash_lock, flags); | |
453 | list_for_each_entry(tcmd, head, hash_list) { | |
454 | if (tcmd->tag == tag) { | |
455 | rq = tcmd->rq; | |
456 | list_del(&tcmd->hash_list); | |
457 | break; | |
458 | } | |
459 | } | |
460 | spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags); | |
461 | ||
462 | return rq; | |
463 | } | |
464 | ||
2c47f9ef | 465 | int scsi_tgt_kspace_exec(int host_no, u64 itn_id, int result, u64 tag, |
bc7e380a FT |
466 | unsigned long uaddr, u32 len, unsigned long sense_uaddr, |
467 | u32 sense_len, u8 rw) | |
5a55c259 FT |
468 | { |
469 | struct Scsi_Host *shost; | |
470 | struct scsi_cmnd *cmd; | |
471 | struct request *rq; | |
472 | struct scsi_tgt_cmd *tcmd; | |
473 | int err = 0; | |
474 | ||
475 | dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag, | |
476 | result, len, uaddr, rw); | |
477 | ||
478 | /* TODO: replace with a O(1) alg */ | |
479 | shost = scsi_host_lookup(host_no); | |
480 | if (IS_ERR(shost)) { | |
481 | printk(KERN_ERR "Could not find host no %d\n", host_no); | |
482 | return -EINVAL; | |
483 | } | |
484 | ||
485 | if (!shost->uspace_req_q) { | |
486 | printk(KERN_ERR "Not target scsi host %d\n", host_no); | |
487 | goto done; | |
488 | } | |
489 | ||
490 | rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag); | |
491 | if (!rq) { | |
492 | printk(KERN_ERR "Could not find tag %llu\n", | |
493 | (unsigned long long) tag); | |
494 | err = -EINVAL; | |
495 | goto done; | |
496 | } | |
497 | cmd = rq->special; | |
498 | ||
181011e0 MC |
499 | dprintk("cmd %p scb %x result %d len %d bufflen %u %lu %x\n", |
500 | cmd, cmd->cmnd[0], result, len, cmd->request_bufflen, | |
501 | rq_data_dir(rq), cmd->cmnd[0]); | |
5a55c259 FT |
502 | |
503 | if (result == TASK_ABORTED) { | |
504 | scsi_tgt_abort_cmd(shost, cmd); | |
505 | goto done; | |
506 | } | |
507 | /* | |
508 | * store the userspace values here, the working values are | |
509 | * in the request_* values | |
510 | */ | |
511 | tcmd = cmd->request->end_io_data; | |
5a55c259 FT |
512 | cmd->result = result; |
513 | ||
bc7e380a FT |
514 | if (cmd->result == SAM_STAT_CHECK_CONDITION) |
515 | scsi_tgt_copy_sense(cmd, sense_uaddr, sense_len); | |
5a55c259 | 516 | |
bc7e380a FT |
517 | if (len) { |
518 | err = scsi_map_user_pages(rq->end_io_data, cmd, uaddr, len, rw); | |
519 | if (err) { | |
e8f8248c FT |
520 | /* |
521 | * user-space daemon bugs or OOM | |
522 | * TODO: we can do better for OOM. | |
523 | */ | |
a52decaf FT |
524 | struct scsi_tgt_queuedata *qdata; |
525 | struct list_head *head; | |
526 | unsigned long flags; | |
527 | ||
e8f8248c FT |
528 | eprintk("cmd %p ret %d uaddr %lx len %d rw %d\n", |
529 | cmd, err, uaddr, len, rw); | |
a52decaf FT |
530 | |
531 | qdata = shost->uspace_req_q->queuedata; | |
532 | head = &qdata->cmd_hash[cmd_hashfn(tcmd->tag)]; | |
533 | ||
534 | spin_lock_irqsave(&qdata->cmd_hash_lock, flags); | |
535 | list_add(&tcmd->hash_list, head); | |
536 | spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags); | |
537 | ||
538 | goto done; | |
bc7e380a | 539 | } |
5a55c259 | 540 | } |
bc7e380a | 541 | err = scsi_tgt_transfer_response(cmd); |
5a55c259 FT |
542 | done: |
543 | scsi_host_put(shost); | |
544 | return err; | |
545 | } | |
546 | ||
2c47f9ef FT |
547 | int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, u64 itn_id, |
548 | int function, u64 tag, struct scsi_lun *scsilun, | |
549 | void *data) | |
5a55c259 FT |
550 | { |
551 | int err; | |
552 | ||
553 | /* TODO: need to retry if this fails. */ | |
2c47f9ef FT |
554 | err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, itn_id, |
555 | function, tag, scsilun, data); | |
5a55c259 FT |
556 | if (err < 0) |
557 | eprintk("The task management request lost!\n"); | |
558 | return err; | |
559 | } | |
560 | EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request); | |
561 | ||
2c47f9ef | 562 | int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 itn_id, u64 mid, int result) |
5a55c259 FT |
563 | { |
564 | struct Scsi_Host *shost; | |
565 | int err = -EINVAL; | |
566 | ||
567 | dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid); | |
568 | ||
569 | shost = scsi_host_lookup(host_no); | |
570 | if (IS_ERR(shost)) { | |
571 | printk(KERN_ERR "Could not find host no %d\n", host_no); | |
572 | return err; | |
573 | } | |
574 | ||
575 | if (!shost->uspace_req_q) { | |
576 | printk(KERN_ERR "Not target scsi host %d\n", host_no); | |
577 | goto done; | |
578 | } | |
579 | ||
bfb74374 | 580 | err = shost->transportt->tsk_mgmt_response(shost, itn_id, mid, result); |
2c47f9ef FT |
581 | done: |
582 | scsi_host_put(shost); | |
583 | return err; | |
584 | } | |
585 | ||
586 | int scsi_tgt_it_nexus_create(struct Scsi_Host *shost, u64 itn_id, | |
587 | char *initiator) | |
588 | { | |
589 | int err; | |
590 | ||
591 | /* TODO: need to retry if this fails. */ | |
592 | err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no, itn_id, 0, | |
593 | initiator); | |
594 | if (err < 0) | |
595 | eprintk("The i_t_neuxs request lost, %d %llx!\n", | |
596 | shost->host_no, (unsigned long long)itn_id); | |
597 | return err; | |
598 | } | |
599 | EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_create); | |
600 | ||
601 | int scsi_tgt_it_nexus_destroy(struct Scsi_Host *shost, u64 itn_id) | |
602 | { | |
603 | int err; | |
604 | ||
605 | /* TODO: need to retry if this fails. */ | |
606 | err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no, | |
607 | itn_id, 1, NULL); | |
608 | if (err < 0) | |
609 | eprintk("The i_t_neuxs request lost, %d %llx!\n", | |
610 | shost->host_no, (unsigned long long)itn_id); | |
611 | return err; | |
612 | } | |
613 | EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_destroy); | |
614 | ||
615 | int scsi_tgt_kspace_it_nexus_rsp(int host_no, u64 itn_id, int result) | |
616 | { | |
617 | struct Scsi_Host *shost; | |
618 | int err = -EINVAL; | |
619 | ||
620 | dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid); | |
621 | ||
622 | shost = scsi_host_lookup(host_no); | |
623 | if (IS_ERR(shost)) { | |
624 | printk(KERN_ERR "Could not find host no %d\n", host_no); | |
625 | return err; | |
626 | } | |
627 | ||
628 | if (!shost->uspace_req_q) { | |
629 | printk(KERN_ERR "Not target scsi host %d\n", host_no); | |
630 | goto done; | |
631 | } | |
632 | ||
633 | err = shost->transportt->it_nexus_response(shost, itn_id, result); | |
5a55c259 FT |
634 | done: |
635 | scsi_host_put(shost); | |
636 | return err; | |
637 | } | |
638 | ||
639 | static int __init scsi_tgt_init(void) | |
640 | { | |
641 | int err; | |
642 | ||
643 | scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd", | |
644 | sizeof(struct scsi_tgt_cmd), | |
20c2df83 | 645 | 0, 0, NULL); |
5a55c259 FT |
646 | if (!scsi_tgt_cmd_cache) |
647 | return -ENOMEM; | |
648 | ||
649 | scsi_tgtd = create_workqueue("scsi_tgtd"); | |
650 | if (!scsi_tgtd) { | |
651 | err = -ENOMEM; | |
652 | goto free_kmemcache; | |
653 | } | |
654 | ||
655 | err = scsi_tgt_if_init(); | |
656 | if (err) | |
657 | goto destroy_wq; | |
658 | ||
659 | return 0; | |
660 | ||
661 | destroy_wq: | |
662 | destroy_workqueue(scsi_tgtd); | |
663 | free_kmemcache: | |
664 | kmem_cache_destroy(scsi_tgt_cmd_cache); | |
665 | return err; | |
666 | } | |
667 | ||
668 | static void __exit scsi_tgt_exit(void) | |
669 | { | |
670 | destroy_workqueue(scsi_tgtd); | |
671 | scsi_tgt_if_exit(); | |
672 | kmem_cache_destroy(scsi_tgt_cmd_cache); | |
673 | } | |
674 | ||
675 | module_init(scsi_tgt_init); | |
676 | module_exit(scsi_tgt_exit); | |
677 | ||
678 | MODULE_DESCRIPTION("SCSI target core"); | |
679 | MODULE_LICENSE("GPL"); |