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