FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 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> |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 30 | #include <scsi/scsi_transport.h> |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 31 | #include <scsi/scsi_tgt.h> |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 32 | |
| 33 | #include "scsi_tgt_priv.h" |
| 34 | |
| 35 | static struct workqueue_struct *scsi_tgtd; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 36 | static struct kmem_cache *scsi_tgt_cmd_cache; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 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; |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 45 | /* TODO fix limits of some drivers */ |
| 46 | struct bio *bio; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 47 | |
| 48 | struct list_head hash_list; |
| 49 | struct request *rq; |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 50 | u64 itn_id; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 51 | u64 tag; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 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 | |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 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); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 99 | if (!rq) |
| 100 | goto free_tcmd; |
| 101 | |
| 102 | cmd = __scsi_get_command(shost, gfp_mask); |
| 103 | if (!cmd) |
| 104 | goto release_rq; |
| 105 | |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 106 | cmd->sc_data_direction = data_dir; |
| 107 | cmd->jiffies_at_alloc = jiffies; |
| 108 | cmd->request = rq; |
| 109 | |
| 110 | rq->special = cmd; |
| 111 | rq->cmd_type = REQ_TYPE_SPECIAL; |
| 112 | rq->cmd_flags |= REQ_TYPE_BLOCK_PC; |
| 113 | rq->end_io_data = tcmd; |
| 114 | |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 115 | tcmd->rq = rq; |
| 116 | |
| 117 | return cmd; |
| 118 | |
| 119 | release_rq: |
| 120 | blk_put_request(rq); |
| 121 | free_tcmd: |
| 122 | kmem_cache_free(scsi_tgt_cmd_cache, tcmd); |
| 123 | put_dev: |
| 124 | put_device(&shost->shost_gendev); |
| 125 | return NULL; |
| 126 | |
| 127 | } |
| 128 | EXPORT_SYMBOL_GPL(scsi_host_get_command); |
| 129 | |
| 130 | /* |
| 131 | * Function: scsi_host_put_command() |
| 132 | * |
| 133 | * Purpose: Free a scsi command block |
| 134 | * |
| 135 | * Arguments: shost - scsi host |
| 136 | * cmd - command block to free |
| 137 | * |
| 138 | * Returns: Nothing. |
| 139 | * |
| 140 | * Notes: The command must not belong to any lists. |
| 141 | */ |
| 142 | void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd) |
| 143 | { |
| 144 | struct request_queue *q = shost->uspace_req_q; |
| 145 | struct request *rq = cmd->request; |
| 146 | struct scsi_tgt_cmd *tcmd = rq->end_io_data; |
| 147 | unsigned long flags; |
| 148 | |
| 149 | kmem_cache_free(scsi_tgt_cmd_cache, tcmd); |
| 150 | |
| 151 | spin_lock_irqsave(q->queue_lock, flags); |
| 152 | __blk_put_request(q, rq); |
| 153 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 154 | |
| 155 | __scsi_put_command(shost, cmd, &shost->shost_gendev); |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(scsi_host_put_command); |
| 158 | |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 159 | static void cmd_hashlist_del(struct scsi_cmnd *cmd) |
| 160 | { |
| 161 | struct request_queue *q = cmd->request->q; |
| 162 | struct scsi_tgt_queuedata *qdata = q->queuedata; |
| 163 | unsigned long flags; |
| 164 | struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data; |
| 165 | |
| 166 | spin_lock_irqsave(&qdata->cmd_hash_lock, flags); |
| 167 | list_del(&tcmd->hash_list); |
| 168 | spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags); |
| 169 | } |
| 170 | |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 171 | static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd) |
| 172 | { |
| 173 | blk_rq_unmap_user(tcmd->bio); |
| 174 | } |
| 175 | |
David Howells | 06328b4 | 2006-12-06 15:02:26 +0000 | [diff] [blame] | 176 | static void scsi_tgt_cmd_destroy(struct work_struct *work) |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 177 | { |
David Howells | 06328b4 | 2006-12-06 15:02:26 +0000 | [diff] [blame] | 178 | struct scsi_tgt_cmd *tcmd = |
| 179 | container_of(work, struct scsi_tgt_cmd, work); |
| 180 | struct scsi_cmnd *cmd = tcmd->rq->special; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 181 | |
Randy Dunlap | a8aae4d | 2007-11-15 11:53:25 +0200 | [diff] [blame] | 182 | dprintk("cmd %p %d %u\n", cmd, cmd->sc_data_direction, |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 183 | rq_data_dir(cmd->request)); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 184 | scsi_unmap_user_pages(tcmd); |
| 185 | scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd); |
| 186 | } |
| 187 | |
| 188 | static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd, |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 189 | u64 itn_id, u64 tag) |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 190 | { |
| 191 | struct scsi_tgt_queuedata *qdata = rq->q->queuedata; |
| 192 | unsigned long flags; |
| 193 | struct list_head *head; |
| 194 | |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 195 | tcmd->itn_id = itn_id; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 196 | tcmd->tag = tag; |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 197 | tcmd->bio = NULL; |
David Howells | 06328b4 | 2006-12-06 15:02:26 +0000 | [diff] [blame] | 198 | INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 199 | spin_lock_irqsave(&qdata->cmd_hash_lock, flags); |
| 200 | head = &qdata->cmd_hash[cmd_hashfn(tag)]; |
| 201 | list_add(&tcmd->hash_list, head); |
| 202 | spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags); |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * scsi_tgt_alloc_queue - setup queue used for message passing |
| 207 | * shost: scsi host |
| 208 | * |
| 209 | * This should be called by the LLD after host allocation. |
| 210 | * And will be released when the host is released. |
| 211 | */ |
| 212 | int scsi_tgt_alloc_queue(struct Scsi_Host *shost) |
| 213 | { |
| 214 | struct scsi_tgt_queuedata *queuedata; |
| 215 | struct request_queue *q; |
| 216 | int err, i; |
| 217 | |
| 218 | /* |
| 219 | * Do we need to send a netlink event or should uspace |
| 220 | * just respond to the hotplug event? |
| 221 | */ |
| 222 | q = __scsi_alloc_queue(shost, NULL); |
| 223 | if (!q) |
| 224 | return -ENOMEM; |
| 225 | |
| 226 | queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL); |
| 227 | if (!queuedata) { |
| 228 | err = -ENOMEM; |
| 229 | goto cleanup_queue; |
| 230 | } |
| 231 | queuedata->shost = shost; |
| 232 | q->queuedata = queuedata; |
| 233 | |
| 234 | /* |
| 235 | * this is a silly hack. We should probably just queue as many |
| 236 | * command as is recvd to userspace. uspace can then make |
| 237 | * sure we do not overload the HBA |
| 238 | */ |
FUJITA Tomonori | 8184fe9 | 2007-09-01 02:02:16 +0900 | [diff] [blame] | 239 | q->nr_requests = shost->can_queue; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 240 | /* |
| 241 | * We currently only support software LLDs so this does |
| 242 | * not matter for now. Do we need this for the cards we support? |
| 243 | * If so we should make it a host template value. |
| 244 | */ |
| 245 | blk_queue_dma_alignment(q, 0); |
| 246 | shost->uspace_req_q = q; |
| 247 | |
| 248 | for (i = 0; i < ARRAY_SIZE(queuedata->cmd_hash); i++) |
| 249 | INIT_LIST_HEAD(&queuedata->cmd_hash[i]); |
| 250 | spin_lock_init(&queuedata->cmd_hash_lock); |
| 251 | |
| 252 | return 0; |
| 253 | |
| 254 | cleanup_queue: |
| 255 | blk_cleanup_queue(q); |
| 256 | return err; |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue); |
| 259 | |
| 260 | void scsi_tgt_free_queue(struct Scsi_Host *shost) |
| 261 | { |
| 262 | int i; |
| 263 | unsigned long flags; |
| 264 | struct request_queue *q = shost->uspace_req_q; |
| 265 | struct scsi_cmnd *cmd; |
| 266 | struct scsi_tgt_queuedata *qdata = q->queuedata; |
| 267 | struct scsi_tgt_cmd *tcmd, *n; |
| 268 | LIST_HEAD(cmds); |
| 269 | |
| 270 | spin_lock_irqsave(&qdata->cmd_hash_lock, flags); |
| 271 | |
| 272 | for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) { |
| 273 | list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i], |
| 274 | hash_list) { |
| 275 | list_del(&tcmd->hash_list); |
| 276 | list_add(&tcmd->hash_list, &cmds); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags); |
| 281 | |
| 282 | while (!list_empty(&cmds)) { |
| 283 | tcmd = list_entry(cmds.next, struct scsi_tgt_cmd, hash_list); |
| 284 | list_del(&tcmd->hash_list); |
| 285 | cmd = tcmd->rq->special; |
| 286 | |
| 287 | shost->hostt->eh_abort_handler(cmd); |
David Howells | 06328b4 | 2006-12-06 15:02:26 +0000 | [diff] [blame] | 288 | scsi_tgt_cmd_destroy(&tcmd->work); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(scsi_tgt_free_queue); |
| 292 | |
| 293 | struct Scsi_Host *scsi_tgt_cmd_to_host(struct scsi_cmnd *cmd) |
| 294 | { |
| 295 | struct scsi_tgt_queuedata *queue = cmd->request->q->queuedata; |
| 296 | return queue->shost; |
| 297 | } |
| 298 | EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host); |
| 299 | |
| 300 | /* |
| 301 | * scsi_tgt_queue_command - queue command for userspace processing |
| 302 | * @cmd: scsi command |
| 303 | * @scsilun: scsi lun |
| 304 | * @tag: unique value to identify this command for tmf |
| 305 | */ |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 306 | int scsi_tgt_queue_command(struct scsi_cmnd *cmd, u64 itn_id, |
| 307 | struct scsi_lun *scsilun, u64 tag) |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 308 | { |
| 309 | struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data; |
| 310 | int err; |
| 311 | |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 312 | init_scsi_tgt_cmd(cmd->request, tcmd, itn_id, tag); |
| 313 | err = scsi_tgt_uspace_send_cmd(cmd, itn_id, scsilun, tag); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 314 | if (err) |
| 315 | cmd_hashlist_del(cmd); |
| 316 | |
| 317 | return err; |
| 318 | } |
| 319 | EXPORT_SYMBOL_GPL(scsi_tgt_queue_command); |
| 320 | |
| 321 | /* |
Joe Perches | b1c1181 | 2008-02-03 17:28:22 +0200 | [diff] [blame] | 322 | * This is run from a interrupt handler normally and the unmap |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 323 | * needs process context so we must queue |
| 324 | */ |
| 325 | static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd) |
| 326 | { |
| 327 | struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data; |
| 328 | |
Randy Dunlap | a8aae4d | 2007-11-15 11:53:25 +0200 | [diff] [blame] | 329 | dprintk("cmd %p %u\n", cmd, rq_data_dir(cmd->request)); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 330 | |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 331 | scsi_tgt_uspace_send_status(cmd, tcmd->itn_id, tcmd->tag); |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 332 | |
Boaz Harrosh | bb52d82 | 2007-12-13 16:14:27 -0800 | [diff] [blame] | 333 | scsi_release_buffers(cmd); |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 334 | |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 335 | queue_work(scsi_tgtd, &tcmd->work); |
| 336 | } |
| 337 | |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 338 | static int scsi_tgt_transfer_response(struct scsi_cmnd *cmd) |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 339 | { |
| 340 | struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd); |
| 341 | int err; |
| 342 | |
Randy Dunlap | a8aae4d | 2007-11-15 11:53:25 +0200 | [diff] [blame] | 343 | dprintk("cmd %p %u\n", cmd, rq_data_dir(cmd->request)); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 344 | |
| 345 | err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done); |
| 346 | switch (err) { |
| 347 | case SCSI_MLQUEUE_HOST_BUSY: |
| 348 | case SCSI_MLQUEUE_DEVICE_BUSY: |
| 349 | return -EAGAIN; |
| 350 | } |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 354 | /* TODO: test this crap and replace bio_map_user with new interface maybe */ |
| 355 | static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd, |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 356 | unsigned long uaddr, unsigned int len, int rw) |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 357 | { |
| 358 | struct request_queue *q = cmd->request->q; |
| 359 | struct request *rq = cmd->request; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 360 | int err; |
| 361 | |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 362 | dprintk("%lx %u\n", uaddr, len); |
| 363 | err = blk_rq_map_user(q, rq, (void *)uaddr, len); |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 364 | if (err) { |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 365 | /* |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 366 | * TODO: need to fixup sg_tablesize, max_segment_size, |
| 367 | * max_sectors, etc for modern HW and software drivers |
| 368 | * where this value is bogus. |
| 369 | * |
| 370 | * TODO2: we can alloc a reserve buffer of max size |
| 371 | * we can handle and do the slow copy path for really large |
| 372 | * IO. |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 373 | */ |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 374 | eprintk("Could not handle request of size %u.\n", len); |
| 375 | return err; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 376 | } |
| 377 | |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 378 | tcmd->bio = rq->bio; |
Boaz Harrosh | bb52d82 | 2007-12-13 16:14:27 -0800 | [diff] [blame] | 379 | err = scsi_init_io(cmd, GFP_KERNEL); |
| 380 | if (err) { |
| 381 | scsi_release_buffers(cmd); |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 382 | goto unmap_rq; |
Boaz Harrosh | bb52d82 | 2007-12-13 16:14:27 -0800 | [diff] [blame] | 383 | } |
FUJITA Tomonori | cccddc2 | 2008-03-01 15:36:36 +0900 | [diff] [blame] | 384 | /* |
| 385 | * we use REQ_TYPE_BLOCK_PC so scsi_init_io doesn't set the |
| 386 | * length for us. |
| 387 | */ |
| 388 | cmd->sdb.length = rq->data_len; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 389 | |
| 390 | return 0; |
| 391 | |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 392 | unmap_rq: |
| 393 | scsi_unmap_user_pages(tcmd); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 394 | return err; |
| 395 | } |
| 396 | |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 397 | static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr, |
| 398 | unsigned len) |
| 399 | { |
| 400 | char __user *p = (char __user *) uaddr; |
| 401 | |
| 402 | if (copy_from_user(cmd->sense_buffer, p, |
| 403 | min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) { |
| 404 | printk(KERN_ERR "Could not copy the sense buffer\n"); |
| 405 | return -EIO; |
| 406 | } |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd) |
| 411 | { |
David Howells | 06328b4 | 2006-12-06 15:02:26 +0000 | [diff] [blame] | 412 | struct scsi_tgt_cmd *tcmd; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 413 | int err; |
| 414 | |
| 415 | err = shost->hostt->eh_abort_handler(cmd); |
| 416 | if (err) |
| 417 | eprintk("fail to abort %p\n", cmd); |
| 418 | |
David Howells | 06328b4 | 2006-12-06 15:02:26 +0000 | [diff] [blame] | 419 | tcmd = cmd->request->end_io_data; |
| 420 | scsi_tgt_cmd_destroy(&tcmd->work); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 421 | return err; |
| 422 | } |
| 423 | |
| 424 | static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag) |
| 425 | { |
| 426 | struct scsi_tgt_queuedata *qdata = q->queuedata; |
| 427 | struct request *rq = NULL; |
| 428 | struct list_head *head; |
| 429 | struct scsi_tgt_cmd *tcmd; |
| 430 | unsigned long flags; |
| 431 | |
| 432 | head = &qdata->cmd_hash[cmd_hashfn(tag)]; |
| 433 | spin_lock_irqsave(&qdata->cmd_hash_lock, flags); |
| 434 | list_for_each_entry(tcmd, head, hash_list) { |
| 435 | if (tcmd->tag == tag) { |
| 436 | rq = tcmd->rq; |
| 437 | list_del(&tcmd->hash_list); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags); |
| 442 | |
| 443 | return rq; |
| 444 | } |
| 445 | |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 446 | int scsi_tgt_kspace_exec(int host_no, u64 itn_id, int result, u64 tag, |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 447 | unsigned long uaddr, u32 len, unsigned long sense_uaddr, |
| 448 | u32 sense_len, u8 rw) |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 449 | { |
| 450 | struct Scsi_Host *shost; |
| 451 | struct scsi_cmnd *cmd; |
| 452 | struct request *rq; |
| 453 | struct scsi_tgt_cmd *tcmd; |
| 454 | int err = 0; |
| 455 | |
| 456 | dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag, |
| 457 | result, len, uaddr, rw); |
| 458 | |
| 459 | /* TODO: replace with a O(1) alg */ |
| 460 | shost = scsi_host_lookup(host_no); |
| 461 | if (IS_ERR(shost)) { |
| 462 | printk(KERN_ERR "Could not find host no %d\n", host_no); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | if (!shost->uspace_req_q) { |
| 467 | printk(KERN_ERR "Not target scsi host %d\n", host_no); |
| 468 | goto done; |
| 469 | } |
| 470 | |
| 471 | rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag); |
| 472 | if (!rq) { |
| 473 | printk(KERN_ERR "Could not find tag %llu\n", |
| 474 | (unsigned long long) tag); |
| 475 | err = -EINVAL; |
| 476 | goto done; |
| 477 | } |
| 478 | cmd = rq->special; |
| 479 | |
Randy Dunlap | a8aae4d | 2007-11-15 11:53:25 +0200 | [diff] [blame] | 480 | dprintk("cmd %p scb %x result %d len %d bufflen %u %u %x\n", |
| 481 | cmd, cmd->cmnd[0], result, len, scsi_bufflen(cmd), |
Mike Christie | 181011e | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 482 | rq_data_dir(rq), cmd->cmnd[0]); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 483 | |
| 484 | if (result == TASK_ABORTED) { |
| 485 | scsi_tgt_abort_cmd(shost, cmd); |
| 486 | goto done; |
| 487 | } |
| 488 | /* |
| 489 | * store the userspace values here, the working values are |
| 490 | * in the request_* values |
| 491 | */ |
| 492 | tcmd = cmd->request->end_io_data; |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 493 | cmd->result = result; |
| 494 | |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 495 | if (cmd->result == SAM_STAT_CHECK_CONDITION) |
| 496 | scsi_tgt_copy_sense(cmd, sense_uaddr, sense_len); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 497 | |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 498 | if (len) { |
| 499 | err = scsi_map_user_pages(rq->end_io_data, cmd, uaddr, len, rw); |
| 500 | if (err) { |
FUJITA Tomonori | e8f8248 | 2007-03-03 09:55:55 +0900 | [diff] [blame] | 501 | /* |
| 502 | * user-space daemon bugs or OOM |
| 503 | * TODO: we can do better for OOM. |
| 504 | */ |
FUJITA Tomonori | a52deca | 2007-03-13 10:07:15 +0900 | [diff] [blame] | 505 | struct scsi_tgt_queuedata *qdata; |
| 506 | struct list_head *head; |
| 507 | unsigned long flags; |
| 508 | |
FUJITA Tomonori | e8f8248 | 2007-03-03 09:55:55 +0900 | [diff] [blame] | 509 | eprintk("cmd %p ret %d uaddr %lx len %d rw %d\n", |
| 510 | cmd, err, uaddr, len, rw); |
FUJITA Tomonori | a52deca | 2007-03-13 10:07:15 +0900 | [diff] [blame] | 511 | |
| 512 | qdata = shost->uspace_req_q->queuedata; |
| 513 | head = &qdata->cmd_hash[cmd_hashfn(tcmd->tag)]; |
| 514 | |
| 515 | spin_lock_irqsave(&qdata->cmd_hash_lock, flags); |
| 516 | list_add(&tcmd->hash_list, head); |
| 517 | spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags); |
| 518 | |
| 519 | goto done; |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 520 | } |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 521 | } |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 522 | err = scsi_tgt_transfer_response(cmd); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 523 | done: |
| 524 | scsi_host_put(shost); |
| 525 | return err; |
| 526 | } |
| 527 | |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 528 | int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, u64 itn_id, |
| 529 | int function, u64 tag, struct scsi_lun *scsilun, |
| 530 | void *data) |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 531 | { |
| 532 | int err; |
| 533 | |
| 534 | /* TODO: need to retry if this fails. */ |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 535 | err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, itn_id, |
| 536 | function, tag, scsilun, data); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 537 | if (err < 0) |
| 538 | eprintk("The task management request lost!\n"); |
| 539 | return err; |
| 540 | } |
| 541 | EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request); |
| 542 | |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 543 | int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 itn_id, u64 mid, int result) |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 544 | { |
| 545 | struct Scsi_Host *shost; |
| 546 | int err = -EINVAL; |
| 547 | |
| 548 | dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid); |
| 549 | |
| 550 | shost = scsi_host_lookup(host_no); |
| 551 | if (IS_ERR(shost)) { |
| 552 | printk(KERN_ERR "Could not find host no %d\n", host_no); |
| 553 | return err; |
| 554 | } |
| 555 | |
| 556 | if (!shost->uspace_req_q) { |
| 557 | printk(KERN_ERR "Not target scsi host %d\n", host_no); |
| 558 | goto done; |
| 559 | } |
| 560 | |
FUJITA Tomonori | bfb7437 | 2007-07-11 15:08:22 +0900 | [diff] [blame] | 561 | err = shost->transportt->tsk_mgmt_response(shost, itn_id, mid, result); |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 562 | done: |
| 563 | scsi_host_put(shost); |
| 564 | return err; |
| 565 | } |
| 566 | |
| 567 | int scsi_tgt_it_nexus_create(struct Scsi_Host *shost, u64 itn_id, |
| 568 | char *initiator) |
| 569 | { |
| 570 | int err; |
| 571 | |
| 572 | /* TODO: need to retry if this fails. */ |
| 573 | err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no, itn_id, 0, |
| 574 | initiator); |
| 575 | if (err < 0) |
| 576 | eprintk("The i_t_neuxs request lost, %d %llx!\n", |
| 577 | shost->host_no, (unsigned long long)itn_id); |
| 578 | return err; |
| 579 | } |
| 580 | EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_create); |
| 581 | |
| 582 | int scsi_tgt_it_nexus_destroy(struct Scsi_Host *shost, u64 itn_id) |
| 583 | { |
| 584 | int err; |
| 585 | |
| 586 | /* TODO: need to retry if this fails. */ |
| 587 | err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no, |
| 588 | itn_id, 1, NULL); |
| 589 | if (err < 0) |
| 590 | eprintk("The i_t_neuxs request lost, %d %llx!\n", |
| 591 | shost->host_no, (unsigned long long)itn_id); |
| 592 | return err; |
| 593 | } |
| 594 | EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_destroy); |
| 595 | |
| 596 | int scsi_tgt_kspace_it_nexus_rsp(int host_no, u64 itn_id, int result) |
| 597 | { |
| 598 | struct Scsi_Host *shost; |
| 599 | int err = -EINVAL; |
| 600 | |
Randy Dunlap | a8aae4d | 2007-11-15 11:53:25 +0200 | [diff] [blame] | 601 | dprintk("%d %d%llx\n", host_no, result, (unsigned long long)itn_id); |
FUJITA Tomonori | 2c47f9e | 2007-07-11 15:08:17 +0900 | [diff] [blame] | 602 | |
| 603 | shost = scsi_host_lookup(host_no); |
| 604 | if (IS_ERR(shost)) { |
| 605 | printk(KERN_ERR "Could not find host no %d\n", host_no); |
| 606 | return err; |
| 607 | } |
| 608 | |
| 609 | if (!shost->uspace_req_q) { |
| 610 | printk(KERN_ERR "Not target scsi host %d\n", host_no); |
| 611 | goto done; |
| 612 | } |
| 613 | |
| 614 | err = shost->transportt->it_nexus_response(shost, itn_id, result); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 615 | done: |
| 616 | scsi_host_put(shost); |
| 617 | return err; |
| 618 | } |
| 619 | |
| 620 | static int __init scsi_tgt_init(void) |
| 621 | { |
| 622 | int err; |
| 623 | |
FUJITA Tomonori | b312bab | 2008-04-10 23:48:14 +0900 | [diff] [blame^] | 624 | scsi_tgt_cmd_cache = KMEM_CACHE(scsi_tgt_cmd, 0); |
FUJITA Tomonori | 5a55c25 | 2006-11-16 19:24:13 +0900 | [diff] [blame] | 625 | if (!scsi_tgt_cmd_cache) |
| 626 | return -ENOMEM; |
| 627 | |
| 628 | scsi_tgtd = create_workqueue("scsi_tgtd"); |
| 629 | if (!scsi_tgtd) { |
| 630 | err = -ENOMEM; |
| 631 | goto free_kmemcache; |
| 632 | } |
| 633 | |
| 634 | err = scsi_tgt_if_init(); |
| 635 | if (err) |
| 636 | goto destroy_wq; |
| 637 | |
| 638 | return 0; |
| 639 | |
| 640 | destroy_wq: |
| 641 | destroy_workqueue(scsi_tgtd); |
| 642 | free_kmemcache: |
| 643 | kmem_cache_destroy(scsi_tgt_cmd_cache); |
| 644 | return err; |
| 645 | } |
| 646 | |
| 647 | static void __exit scsi_tgt_exit(void) |
| 648 | { |
| 649 | destroy_workqueue(scsi_tgtd); |
| 650 | scsi_tgt_if_exit(); |
| 651 | kmem_cache_destroy(scsi_tgt_cmd_cache); |
| 652 | } |
| 653 | |
| 654 | module_init(scsi_tgt_init); |
| 655 | module_exit(scsi_tgt_exit); |
| 656 | |
| 657 | MODULE_DESCRIPTION("SCSI target core"); |
| 658 | MODULE_LICENSE("GPL"); |