Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005 Cisco Systems. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 31 | */ |
| 32 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 33 | #include <linux/module.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/err.h> |
| 37 | #include <linux/string.h> |
| 38 | #include <linux/parser.h> |
| 39 | #include <linux/random.h> |
Tim Schmielau | de25968 | 2006-01-08 01:02:05 -0800 | [diff] [blame] | 40 | #include <linux/jiffies.h> |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 41 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame^] | 42 | #include <linux/atomic.h> |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 43 | |
| 44 | #include <scsi/scsi.h> |
| 45 | #include <scsi/scsi_device.h> |
| 46 | #include <scsi/scsi_dbg.h> |
| 47 | #include <scsi/srp.h> |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 48 | #include <scsi/scsi_transport_srp.h> |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 49 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 50 | #include "ib_srp.h" |
| 51 | |
| 52 | #define DRV_NAME "ib_srp" |
| 53 | #define PFX DRV_NAME ": " |
| 54 | #define DRV_VERSION "0.2" |
| 55 | #define DRV_RELDATE "November 1, 2005" |
| 56 | |
| 57 | MODULE_AUTHOR("Roland Dreier"); |
| 58 | MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator " |
| 59 | "v" DRV_VERSION " (" DRV_RELDATE ")"); |
| 60 | MODULE_LICENSE("Dual BSD/GPL"); |
| 61 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 62 | static unsigned int srp_sg_tablesize; |
| 63 | static unsigned int cmd_sg_entries; |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 64 | static unsigned int indirect_sg_entries; |
| 65 | static bool allow_ext_sg; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 66 | static int topspin_workarounds = 1; |
| 67 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 68 | module_param(srp_sg_tablesize, uint, 0444); |
| 69 | MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries"); |
| 70 | |
| 71 | module_param(cmd_sg_entries, uint, 0444); |
| 72 | MODULE_PARM_DESC(cmd_sg_entries, |
| 73 | "Default number of gather/scatter entries in the SRP command (default is 12, max 255)"); |
| 74 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 75 | module_param(indirect_sg_entries, uint, 0444); |
| 76 | MODULE_PARM_DESC(indirect_sg_entries, |
| 77 | "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")"); |
| 78 | |
| 79 | module_param(allow_ext_sg, bool, 0444); |
| 80 | MODULE_PARM_DESC(allow_ext_sg, |
| 81 | "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)"); |
| 82 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 83 | module_param(topspin_workarounds, int, 0444); |
| 84 | MODULE_PARM_DESC(topspin_workarounds, |
| 85 | "Enable workarounds for Topspin/Cisco SRP target bugs if != 0"); |
| 86 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 87 | static void srp_add_one(struct ib_device *device); |
| 88 | static void srp_remove_one(struct ib_device *device); |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 89 | static void srp_recv_completion(struct ib_cq *cq, void *target_ptr); |
| 90 | static void srp_send_completion(struct ib_cq *cq, void *target_ptr); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 91 | static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event); |
| 92 | |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 93 | static struct scsi_transport_template *ib_srp_transport_template; |
| 94 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 95 | static struct ib_client srp_client = { |
| 96 | .name = "srp", |
| 97 | .add = srp_add_one, |
| 98 | .remove = srp_remove_one |
| 99 | }; |
| 100 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 101 | static struct ib_sa_client srp_sa_client; |
| 102 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 103 | static inline struct srp_target_port *host_to_target(struct Scsi_Host *host) |
| 104 | { |
| 105 | return (struct srp_target_port *) host->hostdata; |
| 106 | } |
| 107 | |
| 108 | static const char *srp_target_info(struct Scsi_Host *host) |
| 109 | { |
| 110 | return host_to_target(host)->target_name; |
| 111 | } |
| 112 | |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 113 | static int srp_target_is_topspin(struct srp_target_port *target) |
| 114 | { |
| 115 | static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad }; |
Raghava Kondapalli | 3d1ff48d | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 116 | static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d }; |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 117 | |
| 118 | return topspin_workarounds && |
Raghava Kondapalli | 3d1ff48d | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 119 | (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) || |
| 120 | !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui)); |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 123 | static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size, |
| 124 | gfp_t gfp_mask, |
| 125 | enum dma_data_direction direction) |
| 126 | { |
| 127 | struct srp_iu *iu; |
| 128 | |
| 129 | iu = kmalloc(sizeof *iu, gfp_mask); |
| 130 | if (!iu) |
| 131 | goto out; |
| 132 | |
| 133 | iu->buf = kzalloc(size, gfp_mask); |
| 134 | if (!iu->buf) |
| 135 | goto out_free_iu; |
| 136 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 137 | iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size, |
| 138 | direction); |
| 139 | if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 140 | goto out_free_buf; |
| 141 | |
| 142 | iu->size = size; |
| 143 | iu->direction = direction; |
| 144 | |
| 145 | return iu; |
| 146 | |
| 147 | out_free_buf: |
| 148 | kfree(iu->buf); |
| 149 | out_free_iu: |
| 150 | kfree(iu); |
| 151 | out: |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | static void srp_free_iu(struct srp_host *host, struct srp_iu *iu) |
| 156 | { |
| 157 | if (!iu) |
| 158 | return; |
| 159 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 160 | ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size, |
| 161 | iu->direction); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 162 | kfree(iu->buf); |
| 163 | kfree(iu); |
| 164 | } |
| 165 | |
| 166 | static void srp_qp_event(struct ib_event *event, void *context) |
| 167 | { |
| 168 | printk(KERN_ERR PFX "QP event %d\n", event->event); |
| 169 | } |
| 170 | |
| 171 | static int srp_init_qp(struct srp_target_port *target, |
| 172 | struct ib_qp *qp) |
| 173 | { |
| 174 | struct ib_qp_attr *attr; |
| 175 | int ret; |
| 176 | |
| 177 | attr = kmalloc(sizeof *attr, GFP_KERNEL); |
| 178 | if (!attr) |
| 179 | return -ENOMEM; |
| 180 | |
Roland Dreier | 969a60f | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 181 | ret = ib_find_pkey(target->srp_host->srp_dev->dev, |
| 182 | target->srp_host->port, |
| 183 | be16_to_cpu(target->path.pkey), |
| 184 | &attr->pkey_index); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 185 | if (ret) |
| 186 | goto out; |
| 187 | |
| 188 | attr->qp_state = IB_QPS_INIT; |
| 189 | attr->qp_access_flags = (IB_ACCESS_REMOTE_READ | |
| 190 | IB_ACCESS_REMOTE_WRITE); |
| 191 | attr->port_num = target->srp_host->port; |
| 192 | |
| 193 | ret = ib_modify_qp(qp, attr, |
| 194 | IB_QP_STATE | |
| 195 | IB_QP_PKEY_INDEX | |
| 196 | IB_QP_ACCESS_FLAGS | |
| 197 | IB_QP_PORT); |
| 198 | |
| 199 | out: |
| 200 | kfree(attr); |
| 201 | return ret; |
| 202 | } |
| 203 | |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 204 | static int srp_new_cm_id(struct srp_target_port *target) |
| 205 | { |
| 206 | struct ib_cm_id *new_cm_id; |
| 207 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 208 | new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev, |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 209 | srp_cm_handler, target); |
| 210 | if (IS_ERR(new_cm_id)) |
| 211 | return PTR_ERR(new_cm_id); |
| 212 | |
| 213 | if (target->cm_id) |
| 214 | ib_destroy_cm_id(target->cm_id); |
| 215 | target->cm_id = new_cm_id; |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 220 | static int srp_create_target_ib(struct srp_target_port *target) |
| 221 | { |
| 222 | struct ib_qp_init_attr *init_attr; |
| 223 | int ret; |
| 224 | |
| 225 | init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL); |
| 226 | if (!init_attr) |
| 227 | return -ENOMEM; |
| 228 | |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 229 | target->recv_cq = ib_create_cq(target->srp_host->srp_dev->dev, |
| 230 | srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0); |
| 231 | if (IS_ERR(target->recv_cq)) { |
| 232 | ret = PTR_ERR(target->recv_cq); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 233 | goto err; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 236 | target->send_cq = ib_create_cq(target->srp_host->srp_dev->dev, |
| 237 | srp_send_completion, NULL, target, SRP_SQ_SIZE, 0); |
| 238 | if (IS_ERR(target->send_cq)) { |
| 239 | ret = PTR_ERR(target->send_cq); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 240 | goto err_recv_cq; |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | ib_req_notify_cq(target->recv_cq, IB_CQ_NEXT_COMP); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 244 | |
| 245 | init_attr->event_handler = srp_qp_event; |
| 246 | init_attr->cap.max_send_wr = SRP_SQ_SIZE; |
| 247 | init_attr->cap.max_recv_wr = SRP_RQ_SIZE; |
| 248 | init_attr->cap.max_recv_sge = 1; |
| 249 | init_attr->cap.max_send_sge = 1; |
| 250 | init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; |
| 251 | init_attr->qp_type = IB_QPT_RC; |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 252 | init_attr->send_cq = target->send_cq; |
| 253 | init_attr->recv_cq = target->recv_cq; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 254 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 255 | target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 256 | if (IS_ERR(target->qp)) { |
| 257 | ret = PTR_ERR(target->qp); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 258 | goto err_send_cq; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | ret = srp_init_qp(target, target->qp); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 262 | if (ret) |
| 263 | goto err_qp; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 264 | |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 265 | kfree(init_attr); |
| 266 | return 0; |
| 267 | |
| 268 | err_qp: |
| 269 | ib_destroy_qp(target->qp); |
| 270 | |
| 271 | err_send_cq: |
| 272 | ib_destroy_cq(target->send_cq); |
| 273 | |
| 274 | err_recv_cq: |
| 275 | ib_destroy_cq(target->recv_cq); |
| 276 | |
| 277 | err: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 278 | kfree(init_attr); |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | static void srp_free_target_ib(struct srp_target_port *target) |
| 283 | { |
| 284 | int i; |
| 285 | |
| 286 | ib_destroy_qp(target->qp); |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 287 | ib_destroy_cq(target->send_cq); |
| 288 | ib_destroy_cq(target->recv_cq); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 289 | |
| 290 | for (i = 0; i < SRP_RQ_SIZE; ++i) |
| 291 | srp_free_iu(target->srp_host, target->rx_ring[i]); |
Bart Van Assche | dd5e6e3 | 2010-08-30 19:27:20 +0000 | [diff] [blame] | 292 | for (i = 0; i < SRP_SQ_SIZE; ++i) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 293 | srp_free_iu(target->srp_host, target->tx_ring[i]); |
| 294 | } |
| 295 | |
| 296 | static void srp_path_rec_completion(int status, |
| 297 | struct ib_sa_path_rec *pathrec, |
| 298 | void *target_ptr) |
| 299 | { |
| 300 | struct srp_target_port *target = target_ptr; |
| 301 | |
| 302 | target->status = status; |
| 303 | if (status) |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 304 | shost_printk(KERN_ERR, target->scsi_host, |
| 305 | PFX "Got failed path rec status %d\n", status); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 306 | else |
| 307 | target->path = *pathrec; |
| 308 | complete(&target->done); |
| 309 | } |
| 310 | |
| 311 | static int srp_lookup_path(struct srp_target_port *target) |
| 312 | { |
| 313 | target->path.numb_path = 1; |
| 314 | |
| 315 | init_completion(&target->done); |
| 316 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 317 | target->path_query_id = ib_sa_path_rec_get(&srp_sa_client, |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 318 | target->srp_host->srp_dev->dev, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 319 | target->srp_host->port, |
| 320 | &target->path, |
Sean Hefty | 247e020 | 2007-08-08 15:51:18 -0700 | [diff] [blame] | 321 | IB_SA_PATH_REC_SERVICE_ID | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 322 | IB_SA_PATH_REC_DGID | |
| 323 | IB_SA_PATH_REC_SGID | |
| 324 | IB_SA_PATH_REC_NUMB_PATH | |
| 325 | IB_SA_PATH_REC_PKEY, |
| 326 | SRP_PATH_REC_TIMEOUT_MS, |
| 327 | GFP_KERNEL, |
| 328 | srp_path_rec_completion, |
| 329 | target, &target->path_query); |
| 330 | if (target->path_query_id < 0) |
| 331 | return target->path_query_id; |
| 332 | |
| 333 | wait_for_completion(&target->done); |
| 334 | |
| 335 | if (target->status < 0) |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 336 | shost_printk(KERN_WARNING, target->scsi_host, |
| 337 | PFX "Path record query failed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 338 | |
| 339 | return target->status; |
| 340 | } |
| 341 | |
| 342 | static int srp_send_req(struct srp_target_port *target) |
| 343 | { |
| 344 | struct { |
| 345 | struct ib_cm_req_param param; |
| 346 | struct srp_login_req priv; |
| 347 | } *req = NULL; |
| 348 | int status; |
| 349 | |
| 350 | req = kzalloc(sizeof *req, GFP_KERNEL); |
| 351 | if (!req) |
| 352 | return -ENOMEM; |
| 353 | |
| 354 | req->param.primary_path = &target->path; |
| 355 | req->param.alternate_path = NULL; |
| 356 | req->param.service_id = target->service_id; |
| 357 | req->param.qp_num = target->qp->qp_num; |
| 358 | req->param.qp_type = target->qp->qp_type; |
| 359 | req->param.private_data = &req->priv; |
| 360 | req->param.private_data_len = sizeof req->priv; |
| 361 | req->param.flow_control = 1; |
| 362 | |
| 363 | get_random_bytes(&req->param.starting_psn, 4); |
| 364 | req->param.starting_psn &= 0xffffff; |
| 365 | |
| 366 | /* |
| 367 | * Pick some arbitrary defaults here; we could make these |
| 368 | * module parameters if anyone cared about setting them. |
| 369 | */ |
| 370 | req->param.responder_resources = 4; |
| 371 | req->param.remote_cm_response_timeout = 20; |
| 372 | req->param.local_cm_response_timeout = 20; |
| 373 | req->param.retry_count = 7; |
| 374 | req->param.rnr_retry_count = 7; |
| 375 | req->param.max_cm_retries = 15; |
| 376 | |
| 377 | req->priv.opcode = SRP_LOGIN_REQ; |
| 378 | req->priv.tag = 0; |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 379 | req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 380 | req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | |
| 381 | SRP_BUF_FORMAT_INDIRECT); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 382 | /* |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 383 | * In the published SRP specification (draft rev. 16a), the |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 384 | * port identifier format is 8 bytes of ID extension followed |
| 385 | * by 8 bytes of GUID. Older drafts put the two halves in the |
| 386 | * opposite order, so that the GUID comes first. |
| 387 | * |
| 388 | * Targets conforming to these obsolete drafts can be |
| 389 | * recognized by the I/O Class they report. |
| 390 | */ |
| 391 | if (target->io_class == SRP_REV10_IB_IO_CLASS) { |
| 392 | memcpy(req->priv.initiator_port_id, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 393 | &target->path.sgid.global.interface_id, 8); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 394 | memcpy(req->priv.initiator_port_id + 8, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 395 | &target->initiator_ext, 8); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 396 | memcpy(req->priv.target_port_id, &target->ioc_guid, 8); |
| 397 | memcpy(req->priv.target_port_id + 8, &target->id_ext, 8); |
| 398 | } else { |
| 399 | memcpy(req->priv.initiator_port_id, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 400 | &target->initiator_ext, 8); |
| 401 | memcpy(req->priv.initiator_port_id + 8, |
| 402 | &target->path.sgid.global.interface_id, 8); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 403 | memcpy(req->priv.target_port_id, &target->id_ext, 8); |
| 404 | memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8); |
| 405 | } |
| 406 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 407 | /* |
| 408 | * Topspin/Cisco SRP targets will reject our login unless we |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 409 | * zero out the first 8 bytes of our initiator port ID and set |
| 410 | * the second 8 bytes to the local node GUID. |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 411 | */ |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 412 | if (srp_target_is_topspin(target)) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 413 | shost_printk(KERN_DEBUG, target->scsi_host, |
| 414 | PFX "Topspin/Cisco initiator port ID workaround " |
| 415 | "activated for target GUID %016llx\n", |
| 416 | (unsigned long long) be64_to_cpu(target->ioc_guid)); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 417 | memset(req->priv.initiator_port_id, 0, 8); |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 418 | memcpy(req->priv.initiator_port_id + 8, |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 419 | &target->srp_host->srp_dev->dev->node_guid, 8); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 420 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 421 | |
| 422 | status = ib_send_cm_req(target->cm_id, &req->param); |
| 423 | |
| 424 | kfree(req); |
| 425 | |
| 426 | return status; |
| 427 | } |
| 428 | |
| 429 | static void srp_disconnect_target(struct srp_target_port *target) |
| 430 | { |
| 431 | /* XXX should send SRP_I_LOGOUT request */ |
| 432 | |
| 433 | init_completion(&target->done); |
Roland Dreier | e658105 | 2006-05-17 09:13:21 -0700 | [diff] [blame] | 434 | if (ib_send_cm_dreq(target->cm_id, NULL, 0)) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 435 | shost_printk(KERN_DEBUG, target->scsi_host, |
| 436 | PFX "Sending CM DREQ failed\n"); |
Roland Dreier | e658105 | 2006-05-17 09:13:21 -0700 | [diff] [blame] | 437 | return; |
| 438 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 439 | wait_for_completion(&target->done); |
| 440 | } |
| 441 | |
Bart Van Assche | 9709f0e | 2010-11-26 13:13:06 -0500 | [diff] [blame] | 442 | static bool srp_change_state(struct srp_target_port *target, |
| 443 | enum srp_target_state old, |
| 444 | enum srp_target_state new) |
| 445 | { |
| 446 | bool changed = false; |
| 447 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 448 | spin_lock_irq(&target->lock); |
Bart Van Assche | 9709f0e | 2010-11-26 13:13:06 -0500 | [diff] [blame] | 449 | if (target->state == old) { |
| 450 | target->state = new; |
| 451 | changed = true; |
| 452 | } |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 453 | spin_unlock_irq(&target->lock); |
Bart Van Assche | 9709f0e | 2010-11-26 13:13:06 -0500 | [diff] [blame] | 454 | return changed; |
| 455 | } |
| 456 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 457 | static void srp_free_req_data(struct srp_target_port *target) |
| 458 | { |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 459 | struct ib_device *ibdev = target->srp_host->srp_dev->dev; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 460 | struct srp_request *req; |
| 461 | int i; |
| 462 | |
| 463 | for (i = 0, req = target->req_ring; i < SRP_CMD_SQ_SIZE; ++i, ++req) { |
| 464 | kfree(req->fmr_list); |
| 465 | kfree(req->map_page); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 466 | if (req->indirect_dma_addr) { |
| 467 | ib_dma_unmap_single(ibdev, req->indirect_dma_addr, |
| 468 | target->indirect_size, |
| 469 | DMA_TO_DEVICE); |
| 470 | } |
| 471 | kfree(req->indirect_desc); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 475 | static void srp_remove_work(struct work_struct *work) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 476 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 477 | struct srp_target_port *target = |
| 478 | container_of(work, struct srp_target_port, work); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 479 | |
Bart Van Assche | 9709f0e | 2010-11-26 13:13:06 -0500 | [diff] [blame] | 480 | if (!srp_change_state(target, SRP_TARGET_DEAD, SRP_TARGET_REMOVED)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 481 | return; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 482 | |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 483 | spin_lock(&target->srp_host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 484 | list_del(&target->list); |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 485 | spin_unlock(&target->srp_host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 486 | |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 487 | srp_remove_host(target->scsi_host); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 488 | scsi_remove_host(target->scsi_host); |
| 489 | ib_destroy_cm_id(target->cm_id); |
| 490 | srp_free_target_ib(target); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 491 | srp_free_req_data(target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 492 | scsi_host_put(target->scsi_host); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static int srp_connect_target(struct srp_target_port *target) |
| 496 | { |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 497 | int retries = 3; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 498 | int ret; |
| 499 | |
| 500 | ret = srp_lookup_path(target); |
| 501 | if (ret) |
| 502 | return ret; |
| 503 | |
| 504 | while (1) { |
| 505 | init_completion(&target->done); |
| 506 | ret = srp_send_req(target); |
| 507 | if (ret) |
| 508 | return ret; |
| 509 | wait_for_completion(&target->done); |
| 510 | |
| 511 | /* |
| 512 | * The CM event handling code will set status to |
| 513 | * SRP_PORT_REDIRECT if we get a port redirect REJ |
| 514 | * back, or SRP_DLID_REDIRECT if we get a lid/qp |
| 515 | * redirect REJ back. |
| 516 | */ |
| 517 | switch (target->status) { |
| 518 | case 0: |
| 519 | return 0; |
| 520 | |
| 521 | case SRP_PORT_REDIRECT: |
| 522 | ret = srp_lookup_path(target); |
| 523 | if (ret) |
| 524 | return ret; |
| 525 | break; |
| 526 | |
| 527 | case SRP_DLID_REDIRECT: |
| 528 | break; |
| 529 | |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 530 | case SRP_STALE_CONN: |
| 531 | /* Our current CM id was stale, and is now in timewait. |
| 532 | * Try to reconnect with a new one. |
| 533 | */ |
| 534 | if (!retries-- || srp_new_cm_id(target)) { |
| 535 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 536 | "giving up on stale connection\n"); |
| 537 | target->status = -ECONNRESET; |
| 538 | return target->status; |
| 539 | } |
| 540 | |
| 541 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 542 | "retrying stale connection\n"); |
| 543 | break; |
| 544 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 545 | default: |
| 546 | return target->status; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 551 | static void srp_unmap_data(struct scsi_cmnd *scmnd, |
| 552 | struct srp_target_port *target, |
| 553 | struct srp_request *req) |
| 554 | { |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 555 | struct ib_device *ibdev = target->srp_host->srp_dev->dev; |
| 556 | struct ib_pool_fmr **pfmr; |
| 557 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 558 | if (!scsi_sglist(scmnd) || |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 559 | (scmnd->sc_data_direction != DMA_TO_DEVICE && |
| 560 | scmnd->sc_data_direction != DMA_FROM_DEVICE)) |
| 561 | return; |
| 562 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 563 | pfmr = req->fmr_list; |
| 564 | while (req->nfmr--) |
| 565 | ib_fmr_pool_unmap(*pfmr++); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 566 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 567 | ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd), |
| 568 | scmnd->sc_data_direction); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 571 | static void srp_remove_req(struct srp_target_port *target, |
| 572 | struct srp_request *req, s32 req_lim_delta) |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 573 | { |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 574 | unsigned long flags; |
| 575 | |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 576 | srp_unmap_data(req->scmnd, target, req); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 577 | spin_lock_irqsave(&target->lock, flags); |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 578 | target->req_lim += req_lim_delta; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 579 | req->scmnd = NULL; |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 580 | list_add_tail(&req->list, &target->free_reqs); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 581 | spin_unlock_irqrestore(&target->lock, flags); |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static void srp_reset_req(struct srp_target_port *target, struct srp_request *req) |
| 585 | { |
| 586 | req->scmnd->result = DID_RESET << 16; |
| 587 | req->scmnd->scsi_done(req->scmnd); |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 588 | srp_remove_req(target, req, 0); |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 591 | static int srp_reconnect_target(struct srp_target_port *target) |
| 592 | { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 593 | struct ib_qp_attr qp_attr; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 594 | struct ib_wc wc; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 595 | int i, ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 596 | |
Bart Van Assche | 9709f0e | 2010-11-26 13:13:06 -0500 | [diff] [blame] | 597 | if (!srp_change_state(target, SRP_TARGET_LIVE, SRP_TARGET_CONNECTING)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 598 | return -EAGAIN; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 599 | |
| 600 | srp_disconnect_target(target); |
| 601 | /* |
| 602 | * Now get a new local CM ID so that we avoid confusing the |
| 603 | * target in case things are really fouled up. |
| 604 | */ |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 605 | ret = srp_new_cm_id(target); |
| 606 | if (ret) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 607 | goto err; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 608 | |
| 609 | qp_attr.qp_state = IB_QPS_RESET; |
| 610 | ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE); |
| 611 | if (ret) |
| 612 | goto err; |
| 613 | |
| 614 | ret = srp_init_qp(target, target->qp); |
| 615 | if (ret) |
| 616 | goto err; |
| 617 | |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 618 | while (ib_poll_cq(target->recv_cq, 1, &wc) > 0) |
| 619 | ; /* nothing */ |
| 620 | while (ib_poll_cq(target->send_cq, 1, &wc) > 0) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 621 | ; /* nothing */ |
| 622 | |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 623 | for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { |
| 624 | struct srp_request *req = &target->req_ring[i]; |
| 625 | if (req->scmnd) |
| 626 | srp_reset_req(target, req); |
| 627 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 628 | |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 629 | INIT_LIST_HEAD(&target->free_tx); |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 630 | for (i = 0; i < SRP_SQ_SIZE; ++i) |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 631 | list_add(&target->tx_ring[i]->list, &target->free_tx); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 632 | |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 633 | target->qp_in_error = 0; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 634 | ret = srp_connect_target(target); |
| 635 | if (ret) |
| 636 | goto err; |
| 637 | |
Bart Van Assche | 9709f0e | 2010-11-26 13:13:06 -0500 | [diff] [blame] | 638 | if (!srp_change_state(target, SRP_TARGET_CONNECTING, SRP_TARGET_LIVE)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 639 | ret = -EAGAIN; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 640 | |
| 641 | return ret; |
| 642 | |
| 643 | err: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 644 | shost_printk(KERN_ERR, target->scsi_host, |
| 645 | PFX "reconnect failed (%d), removing target port.\n", ret); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 646 | |
| 647 | /* |
| 648 | * We couldn't reconnect, so kill our target port off. |
Bart Van Assche | 9709f0e | 2010-11-26 13:13:06 -0500 | [diff] [blame] | 649 | * However, we have to defer the real removal because we |
| 650 | * are in the context of the SCSI error handler now, which |
| 651 | * will deadlock if we call scsi_remove_host(). |
| 652 | * |
| 653 | * Schedule our work inside the lock to avoid a race with |
| 654 | * the flush_scheduled_work() in srp_remove_one(). |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 655 | */ |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 656 | spin_lock_irq(&target->lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 657 | if (target->state == SRP_TARGET_CONNECTING) { |
| 658 | target->state = SRP_TARGET_DEAD; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 659 | INIT_WORK(&target->work, srp_remove_work); |
Tejun Heo | f062671 | 2010-10-19 15:24:36 +0000 | [diff] [blame] | 660 | queue_work(ib_wq, &target->work); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 661 | } |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 662 | spin_unlock_irq(&target->lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 663 | |
| 664 | return ret; |
| 665 | } |
| 666 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 667 | static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr, |
| 668 | unsigned int dma_len, u32 rkey) |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 669 | { |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 670 | struct srp_direct_buf *desc = state->desc; |
| 671 | |
| 672 | desc->va = cpu_to_be64(dma_addr); |
| 673 | desc->key = cpu_to_be32(rkey); |
| 674 | desc->len = cpu_to_be32(dma_len); |
| 675 | |
| 676 | state->total_len += dma_len; |
| 677 | state->desc++; |
| 678 | state->ndesc++; |
| 679 | } |
| 680 | |
| 681 | static int srp_map_finish_fmr(struct srp_map_state *state, |
| 682 | struct srp_target_port *target) |
| 683 | { |
| 684 | struct srp_device *dev = target->srp_host->srp_dev; |
| 685 | struct ib_pool_fmr *fmr; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 686 | u64 io_addr = 0; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 687 | |
| 688 | if (!state->npages) |
| 689 | return 0; |
| 690 | |
| 691 | if (state->npages == 1) { |
| 692 | srp_map_desc(state, state->base_dma_addr, state->fmr_len, |
| 693 | target->rkey); |
| 694 | state->npages = state->fmr_len = 0; |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages, |
| 699 | state->npages, io_addr); |
| 700 | if (IS_ERR(fmr)) |
| 701 | return PTR_ERR(fmr); |
| 702 | |
| 703 | *state->next_fmr++ = fmr; |
| 704 | state->nfmr++; |
| 705 | |
| 706 | srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey); |
| 707 | state->npages = state->fmr_len = 0; |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | static void srp_map_update_start(struct srp_map_state *state, |
| 712 | struct scatterlist *sg, int sg_index, |
| 713 | dma_addr_t dma_addr) |
| 714 | { |
| 715 | state->unmapped_sg = sg; |
| 716 | state->unmapped_index = sg_index; |
| 717 | state->unmapped_addr = dma_addr; |
| 718 | } |
| 719 | |
| 720 | static int srp_map_sg_entry(struct srp_map_state *state, |
| 721 | struct srp_target_port *target, |
| 722 | struct scatterlist *sg, int sg_index, |
| 723 | int use_fmr) |
| 724 | { |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 725 | struct srp_device *dev = target->srp_host->srp_dev; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 726 | struct ib_device *ibdev = dev->dev; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 727 | dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg); |
| 728 | unsigned int dma_len = ib_sg_dma_len(ibdev, sg); |
| 729 | unsigned int len; |
| 730 | int ret; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 731 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 732 | if (!dma_len) |
| 733 | return 0; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 734 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 735 | if (use_fmr == SRP_MAP_NO_FMR) { |
| 736 | /* Once we're in direct map mode for a request, we don't |
| 737 | * go back to FMR mode, so no need to update anything |
| 738 | * other than the descriptor. |
| 739 | */ |
| 740 | srp_map_desc(state, dma_addr, dma_len, target->rkey); |
| 741 | return 0; |
| 742 | } |
Ishai Rabinovitz | 559ce8f | 2006-08-03 10:35:43 -0700 | [diff] [blame] | 743 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 744 | /* If we start at an offset into the FMR page, don't merge into |
| 745 | * the current FMR. Finish it out, and use the kernel's MR for this |
| 746 | * sg entry. This is to avoid potential bugs on some SRP targets |
| 747 | * that were never quite defined, but went away when the initiator |
| 748 | * avoided using FMR on such page fragments. |
| 749 | */ |
| 750 | if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) { |
| 751 | ret = srp_map_finish_fmr(state, target); |
| 752 | if (ret) |
| 753 | return ret; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 754 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 755 | srp_map_desc(state, dma_addr, dma_len, target->rkey); |
| 756 | srp_map_update_start(state, NULL, 0, 0); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | /* If this is the first sg to go into the FMR, save our position. |
| 761 | * We need to know the first unmapped entry, its index, and the |
| 762 | * first unmapped address within that entry to be able to restart |
| 763 | * mapping after an error. |
| 764 | */ |
| 765 | if (!state->unmapped_sg) |
| 766 | srp_map_update_start(state, sg, sg_index, dma_addr); |
| 767 | |
| 768 | while (dma_len) { |
| 769 | if (state->npages == SRP_FMR_SIZE) { |
| 770 | ret = srp_map_finish_fmr(state, target); |
| 771 | if (ret) |
| 772 | return ret; |
| 773 | |
| 774 | srp_map_update_start(state, sg, sg_index, dma_addr); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 775 | } |
| 776 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 777 | len = min_t(unsigned int, dma_len, dev->fmr_page_size); |
| 778 | |
| 779 | if (!state->npages) |
| 780 | state->base_dma_addr = dma_addr; |
| 781 | state->pages[state->npages++] = dma_addr; |
| 782 | state->fmr_len += len; |
| 783 | dma_addr += len; |
| 784 | dma_len -= len; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 785 | } |
| 786 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 787 | /* If the last entry of the FMR wasn't a full page, then we need to |
| 788 | * close it out and start a new one -- we can only merge at page |
| 789 | * boundries. |
| 790 | */ |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 791 | ret = 0; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 792 | if (len != dev->fmr_page_size) { |
| 793 | ret = srp_map_finish_fmr(state, target); |
| 794 | if (!ret) |
| 795 | srp_map_update_start(state, NULL, 0, 0); |
| 796 | } |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 797 | return ret; |
| 798 | } |
| 799 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 800 | static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target, |
| 801 | struct srp_request *req) |
| 802 | { |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 803 | struct scatterlist *scat, *sg; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 804 | struct srp_cmd *cmd = req->cmd->buf; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 805 | int i, len, nents, count, use_fmr; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 806 | struct srp_device *dev; |
| 807 | struct ib_device *ibdev; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 808 | struct srp_map_state state; |
| 809 | struct srp_indirect_buf *indirect_hdr; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 810 | u32 table_len; |
| 811 | u8 fmt; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 812 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 813 | if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 814 | return sizeof (struct srp_cmd); |
| 815 | |
| 816 | if (scmnd->sc_data_direction != DMA_FROM_DEVICE && |
| 817 | scmnd->sc_data_direction != DMA_TO_DEVICE) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 818 | shost_printk(KERN_WARNING, target->scsi_host, |
| 819 | PFX "Unhandled data direction %d\n", |
| 820 | scmnd->sc_data_direction); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 821 | return -EINVAL; |
| 822 | } |
| 823 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 824 | nents = scsi_sg_count(scmnd); |
| 825 | scat = scsi_sglist(scmnd); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 826 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 827 | dev = target->srp_host->srp_dev; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 828 | ibdev = dev->dev; |
| 829 | |
| 830 | count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 831 | if (unlikely(count == 0)) |
| 832 | return -EIO; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 833 | |
| 834 | fmt = SRP_DATA_DESC_DIRECT; |
| 835 | len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 836 | |
| 837 | if (count == 1) { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 838 | /* |
| 839 | * The midlayer only generated a single gather/scatter |
| 840 | * entry, or DMA mapping coalesced everything to a |
| 841 | * single entry. So a direct descriptor along with |
| 842 | * the DMA MR suffices. |
| 843 | */ |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 844 | struct srp_direct_buf *buf = (void *) cmd->add_data; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 845 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 846 | buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat)); |
David Dillow | 9af7627 | 2010-11-26 15:34:46 -0500 | [diff] [blame] | 847 | buf->key = cpu_to_be32(target->rkey); |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 848 | buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat)); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 849 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 850 | req->nfmr = 0; |
| 851 | goto map_complete; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 852 | } |
| 853 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 854 | /* We have more than one scatter/gather entry, so build our indirect |
| 855 | * descriptor table, trying to merge as many entries with FMR as we |
| 856 | * can. |
| 857 | */ |
| 858 | indirect_hdr = (void *) cmd->add_data; |
| 859 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 860 | ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr, |
| 861 | target->indirect_size, DMA_TO_DEVICE); |
| 862 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 863 | memset(&state, 0, sizeof(state)); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 864 | state.desc = req->indirect_desc; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 865 | state.pages = req->map_page; |
| 866 | state.next_fmr = req->fmr_list; |
| 867 | |
| 868 | use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR; |
| 869 | |
| 870 | for_each_sg(scat, sg, count, i) { |
| 871 | if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) { |
| 872 | /* FMR mapping failed, so backtrack to the first |
| 873 | * unmapped entry and continue on without using FMR. |
| 874 | */ |
| 875 | dma_addr_t dma_addr; |
| 876 | unsigned int dma_len; |
| 877 | |
| 878 | backtrack: |
| 879 | sg = state.unmapped_sg; |
| 880 | i = state.unmapped_index; |
| 881 | |
| 882 | dma_addr = ib_sg_dma_address(ibdev, sg); |
| 883 | dma_len = ib_sg_dma_len(ibdev, sg); |
| 884 | dma_len -= (state.unmapped_addr - dma_addr); |
| 885 | dma_addr = state.unmapped_addr; |
| 886 | use_fmr = SRP_MAP_NO_FMR; |
| 887 | srp_map_desc(&state, dma_addr, dma_len, target->rkey); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target)) |
| 892 | goto backtrack; |
| 893 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 894 | /* We've mapped the request, now pull as much of the indirect |
| 895 | * descriptor table as we can into the command buffer. If this |
| 896 | * target is not using an external indirect table, we are |
| 897 | * guaranteed to fit into the command, as the SCSI layer won't |
| 898 | * give us more S/G entries than we allow. |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 899 | */ |
| 900 | req->nfmr = state.nfmr; |
| 901 | if (state.ndesc == 1) { |
| 902 | /* FMR mapping was able to collapse this to one entry, |
| 903 | * so use a direct descriptor. |
| 904 | */ |
| 905 | struct srp_direct_buf *buf = (void *) cmd->add_data; |
| 906 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 907 | *buf = req->indirect_desc[0]; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 908 | goto map_complete; |
| 909 | } |
| 910 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 911 | if (unlikely(target->cmd_sg_cnt < state.ndesc && |
| 912 | !target->allow_ext_sg)) { |
| 913 | shost_printk(KERN_ERR, target->scsi_host, |
| 914 | "Could not fit S/G list into SRP_CMD\n"); |
| 915 | return -EIO; |
| 916 | } |
| 917 | |
| 918 | count = min(state.ndesc, target->cmd_sg_cnt); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 919 | table_len = state.ndesc * sizeof (struct srp_direct_buf); |
| 920 | |
| 921 | fmt = SRP_DATA_DESC_INDIRECT; |
| 922 | len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 923 | len += count * sizeof (struct srp_direct_buf); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 924 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 925 | memcpy(indirect_hdr->desc_list, req->indirect_desc, |
| 926 | count * sizeof (struct srp_direct_buf)); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 927 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 928 | indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 929 | indirect_hdr->table_desc.key = cpu_to_be32(target->rkey); |
| 930 | indirect_hdr->table_desc.len = cpu_to_be32(table_len); |
| 931 | indirect_hdr->len = cpu_to_be32(state.total_len); |
| 932 | |
| 933 | if (scmnd->sc_data_direction == DMA_TO_DEVICE) |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 934 | cmd->data_out_desc_cnt = count; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 935 | else |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 936 | cmd->data_in_desc_cnt = count; |
| 937 | |
| 938 | ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len, |
| 939 | DMA_TO_DEVICE); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 940 | |
| 941 | map_complete: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 942 | if (scmnd->sc_data_direction == DMA_TO_DEVICE) |
| 943 | cmd->buf_fmt = fmt << 4; |
| 944 | else |
| 945 | cmd->buf_fmt = fmt; |
| 946 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 947 | return len; |
| 948 | } |
| 949 | |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 950 | /* |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 951 | * Return an IU and possible credit to the free pool |
| 952 | */ |
| 953 | static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu, |
| 954 | enum srp_iu_type iu_type) |
| 955 | { |
| 956 | unsigned long flags; |
| 957 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 958 | spin_lock_irqsave(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 959 | list_add(&iu->list, &target->free_tx); |
| 960 | if (iu_type != SRP_IU_RSP) |
| 961 | ++target->req_lim; |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 962 | spin_unlock_irqrestore(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | /* |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 966 | * Must be called with target->lock held to protect req_lim and free_tx. |
| 967 | * If IU is not sent, it must be returned using srp_put_tx_iu(). |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 968 | * |
| 969 | * Note: |
| 970 | * An upper limit for the number of allocated information units for each |
| 971 | * request type is: |
| 972 | * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues |
| 973 | * more than Scsi_Host.can_queue requests. |
| 974 | * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE. |
| 975 | * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than |
| 976 | * one unanswered SRP request to an initiator. |
| 977 | */ |
| 978 | static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target, |
| 979 | enum srp_iu_type iu_type) |
| 980 | { |
| 981 | s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE; |
| 982 | struct srp_iu *iu; |
| 983 | |
| 984 | srp_send_completion(target->send_cq, target); |
| 985 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 986 | if (list_empty(&target->free_tx)) |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 987 | return NULL; |
| 988 | |
| 989 | /* Initiator responses to target requests do not consume credits */ |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 990 | if (iu_type != SRP_IU_RSP) { |
| 991 | if (target->req_lim <= rsv) { |
| 992 | ++target->zero_req_lim; |
| 993 | return NULL; |
| 994 | } |
| 995 | |
| 996 | --target->req_lim; |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 997 | } |
| 998 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 999 | iu = list_first_entry(&target->free_tx, struct srp_iu, list); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1000 | list_del(&iu->list); |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1001 | return iu; |
| 1002 | } |
| 1003 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1004 | static int srp_post_send(struct srp_target_port *target, |
| 1005 | struct srp_iu *iu, int len) |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1006 | { |
| 1007 | struct ib_sge list; |
| 1008 | struct ib_send_wr wr, *bad_wr; |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1009 | |
| 1010 | list.addr = iu->dma; |
| 1011 | list.length = len; |
David Dillow | 9af7627 | 2010-11-26 15:34:46 -0500 | [diff] [blame] | 1012 | list.lkey = target->lkey; |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1013 | |
| 1014 | wr.next = NULL; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1015 | wr.wr_id = (uintptr_t) iu; |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1016 | wr.sg_list = &list; |
| 1017 | wr.num_sge = 1; |
| 1018 | wr.opcode = IB_WR_SEND; |
| 1019 | wr.send_flags = IB_SEND_SIGNALED; |
| 1020 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1021 | return ib_post_send(target->qp, &wr, &bad_wr); |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1022 | } |
| 1023 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1024 | static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu) |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1025 | { |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1026 | struct ib_recv_wr wr, *bad_wr; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1027 | struct ib_sge list; |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1028 | |
| 1029 | list.addr = iu->dma; |
| 1030 | list.length = iu->size; |
David Dillow | 9af7627 | 2010-11-26 15:34:46 -0500 | [diff] [blame] | 1031 | list.lkey = target->lkey; |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1032 | |
| 1033 | wr.next = NULL; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1034 | wr.wr_id = (uintptr_t) iu; |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1035 | wr.sg_list = &list; |
| 1036 | wr.num_sge = 1; |
| 1037 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1038 | return ib_post_recv(target->qp, &wr, &bad_wr); |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1041 | static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp) |
| 1042 | { |
| 1043 | struct srp_request *req; |
| 1044 | struct scsi_cmnd *scmnd; |
| 1045 | unsigned long flags; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1046 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1047 | if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) { |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1048 | spin_lock_irqsave(&target->lock, flags); |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 1049 | target->req_lim += be32_to_cpu(rsp->req_lim_delta); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1050 | spin_unlock_irqrestore(&target->lock, flags); |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 1051 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1052 | target->tsk_mgmt_status = -1; |
| 1053 | if (be32_to_cpu(rsp->resp_data_len) >= 4) |
| 1054 | target->tsk_mgmt_status = rsp->data[3]; |
| 1055 | complete(&target->tsk_mgmt_done); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1056 | } else { |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1057 | req = &target->req_ring[rsp->tag]; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1058 | scmnd = req->scmnd; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1059 | if (!scmnd) |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1060 | shost_printk(KERN_ERR, target->scsi_host, |
| 1061 | "Null scmnd for RSP w/tag %016llx\n", |
| 1062 | (unsigned long long) rsp->tag); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1063 | scmnd->result = rsp->status; |
| 1064 | |
| 1065 | if (rsp->flags & SRP_RSP_FLAG_SNSVALID) { |
| 1066 | memcpy(scmnd->sense_buffer, rsp->data + |
| 1067 | be32_to_cpu(rsp->resp_data_len), |
| 1068 | min_t(int, be32_to_cpu(rsp->sense_data_len), |
| 1069 | SCSI_SENSE_BUFFERSIZE)); |
| 1070 | } |
| 1071 | |
| 1072 | if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER)) |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 1073 | scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt)); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1074 | else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER)) |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 1075 | scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt)); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1076 | |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 1077 | srp_remove_req(target, req, be32_to_cpu(rsp->req_lim_delta)); |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1078 | scmnd->host_scribble = NULL; |
| 1079 | scmnd->scsi_done(scmnd); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1080 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1083 | static int srp_response_common(struct srp_target_port *target, s32 req_delta, |
| 1084 | void *rsp, int len) |
| 1085 | { |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1086 | struct ib_device *dev = target->srp_host->srp_dev->dev; |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1087 | unsigned long flags; |
| 1088 | struct srp_iu *iu; |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1089 | int err; |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1090 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1091 | spin_lock_irqsave(&target->lock, flags); |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1092 | target->req_lim += req_delta; |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1093 | iu = __srp_get_tx_iu(target, SRP_IU_RSP); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1094 | spin_unlock_irqrestore(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1095 | |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1096 | if (!iu) { |
| 1097 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1098 | "no IU available to send response\n"); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1099 | return 1; |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE); |
| 1103 | memcpy(iu->buf, rsp, len); |
| 1104 | ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE); |
| 1105 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1106 | err = srp_post_send(target, iu, len); |
| 1107 | if (err) { |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1108 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1109 | "unable to post response: %d\n", err); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1110 | srp_put_tx_iu(target, iu, SRP_IU_RSP); |
| 1111 | } |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1112 | |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1113 | return err; |
| 1114 | } |
| 1115 | |
| 1116 | static void srp_process_cred_req(struct srp_target_port *target, |
| 1117 | struct srp_cred_req *req) |
| 1118 | { |
| 1119 | struct srp_cred_rsp rsp = { |
| 1120 | .opcode = SRP_CRED_RSP, |
| 1121 | .tag = req->tag, |
| 1122 | }; |
| 1123 | s32 delta = be32_to_cpu(req->req_lim_delta); |
| 1124 | |
| 1125 | if (srp_response_common(target, delta, &rsp, sizeof rsp)) |
| 1126 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1127 | "problems processing SRP_CRED_REQ\n"); |
| 1128 | } |
| 1129 | |
| 1130 | static void srp_process_aer_req(struct srp_target_port *target, |
| 1131 | struct srp_aer_req *req) |
| 1132 | { |
| 1133 | struct srp_aer_rsp rsp = { |
| 1134 | .opcode = SRP_AER_RSP, |
| 1135 | .tag = req->tag, |
| 1136 | }; |
| 1137 | s32 delta = be32_to_cpu(req->req_lim_delta); |
| 1138 | |
| 1139 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1140 | "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun)); |
| 1141 | |
| 1142 | if (srp_response_common(target, delta, &rsp, sizeof rsp)) |
| 1143 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1144 | "problems processing SRP_AER_REQ\n"); |
| 1145 | } |
| 1146 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1147 | static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc) |
| 1148 | { |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1149 | struct ib_device *dev = target->srp_host->srp_dev->dev; |
Roland Dreier | 737b94e | 2011-05-23 11:30:04 -0700 | [diff] [blame] | 1150 | struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id; |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1151 | int res; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1152 | u8 opcode; |
| 1153 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1154 | ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len, |
| 1155 | DMA_FROM_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1156 | |
| 1157 | opcode = *(u8 *) iu->buf; |
| 1158 | |
| 1159 | if (0) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1160 | shost_printk(KERN_ERR, target->scsi_host, |
| 1161 | PFX "recv completion, opcode 0x%02x\n", opcode); |
Bart Van Assche | 7a70081 | 2010-07-29 15:56:37 +0000 | [diff] [blame] | 1162 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1, |
| 1163 | iu->buf, wc->byte_len, true); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | switch (opcode) { |
| 1167 | case SRP_RSP: |
| 1168 | srp_process_rsp(target, iu->buf); |
| 1169 | break; |
| 1170 | |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1171 | case SRP_CRED_REQ: |
| 1172 | srp_process_cred_req(target, iu->buf); |
| 1173 | break; |
| 1174 | |
| 1175 | case SRP_AER_REQ: |
| 1176 | srp_process_aer_req(target, iu->buf); |
| 1177 | break; |
| 1178 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1179 | case SRP_T_LOGOUT: |
| 1180 | /* XXX Handle target logout */ |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1181 | shost_printk(KERN_WARNING, target->scsi_host, |
| 1182 | PFX "Got target logout request\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1183 | break; |
| 1184 | |
| 1185 | default: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1186 | shost_printk(KERN_WARNING, target->scsi_host, |
| 1187 | PFX "Unhandled SRP opcode 0x%02x\n", opcode); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1188 | break; |
| 1189 | } |
| 1190 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1191 | ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len, |
| 1192 | DMA_FROM_DEVICE); |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1193 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1194 | res = srp_post_recv(target, iu); |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1195 | if (res != 0) |
| 1196 | shost_printk(KERN_ERR, target->scsi_host, |
| 1197 | PFX "Recv failed with error code %d\n", res); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 1200 | static void srp_recv_completion(struct ib_cq *cq, void *target_ptr) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1201 | { |
| 1202 | struct srp_target_port *target = target_ptr; |
| 1203 | struct ib_wc wc; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1204 | |
| 1205 | ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); |
| 1206 | while (ib_poll_cq(cq, 1, &wc) > 0) { |
| 1207 | if (wc.status) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1208 | shost_printk(KERN_ERR, target->scsi_host, |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 1209 | PFX "failed receive status %d\n", |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1210 | wc.status); |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 1211 | target->qp_in_error = 1; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1212 | break; |
| 1213 | } |
| 1214 | |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 1215 | srp_handle_recv(target, &wc); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | static void srp_send_completion(struct ib_cq *cq, void *target_ptr) |
| 1220 | { |
| 1221 | struct srp_target_port *target = target_ptr; |
| 1222 | struct ib_wc wc; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1223 | struct srp_iu *iu; |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 1224 | |
| 1225 | while (ib_poll_cq(cq, 1, &wc) > 0) { |
| 1226 | if (wc.status) { |
| 1227 | shost_printk(KERN_ERR, target->scsi_host, |
| 1228 | PFX "failed send status %d\n", |
| 1229 | wc.status); |
| 1230 | target->qp_in_error = 1; |
| 1231 | break; |
| 1232 | } |
| 1233 | |
Roland Dreier | 737b94e | 2011-05-23 11:30:04 -0700 | [diff] [blame] | 1234 | iu = (struct srp_iu *) (uintptr_t) wc.wr_id; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1235 | list_add(&iu->list, &target->free_tx); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1239 | static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1240 | { |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1241 | struct srp_target_port *target = host_to_target(shost); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1242 | struct srp_request *req; |
| 1243 | struct srp_iu *iu; |
| 1244 | struct srp_cmd *cmd; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1245 | struct ib_device *dev; |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1246 | unsigned long flags; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1247 | int len; |
| 1248 | |
| 1249 | if (target->state == SRP_TARGET_CONNECTING) |
| 1250 | goto err; |
| 1251 | |
| 1252 | if (target->state == SRP_TARGET_DEAD || |
| 1253 | target->state == SRP_TARGET_REMOVED) { |
| 1254 | scmnd->result = DID_BAD_TARGET << 16; |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1255 | scmnd->scsi_done(scmnd); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1256 | return 0; |
| 1257 | } |
| 1258 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1259 | spin_lock_irqsave(&target->lock, flags); |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1260 | iu = __srp_get_tx_iu(target, SRP_IU_CMD); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1261 | if (!iu) |
Bart Van Assche | 695b834 | 2011-01-13 19:02:25 +0000 | [diff] [blame] | 1262 | goto err_unlock; |
| 1263 | |
| 1264 | req = list_first_entry(&target->free_reqs, struct srp_request, list); |
| 1265 | list_del(&req->list); |
| 1266 | spin_unlock_irqrestore(&target->lock, flags); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1267 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 1268 | dev = target->srp_host->srp_dev->dev; |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1269 | ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len, |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1270 | DMA_TO_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1271 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1272 | scmnd->result = 0; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1273 | scmnd->host_scribble = (void *) req; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1274 | |
| 1275 | cmd = iu->buf; |
| 1276 | memset(cmd, 0, sizeof *cmd); |
| 1277 | |
| 1278 | cmd->opcode = SRP_CMD; |
| 1279 | cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1280 | cmd->tag = req->index; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1281 | memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len); |
| 1282 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1283 | req->scmnd = scmnd; |
| 1284 | req->cmd = iu; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1285 | |
| 1286 | len = srp_map_data(scmnd, target, req); |
| 1287 | if (len < 0) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1288 | shost_printk(KERN_ERR, target->scsi_host, |
| 1289 | PFX "Failed to map data\n"); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1290 | goto err_iu; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1293 | ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len, |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1294 | DMA_TO_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1295 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1296 | if (srp_post_send(target, iu, len)) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1297 | shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1298 | goto err_unmap; |
| 1299 | } |
| 1300 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1301 | return 0; |
| 1302 | |
| 1303 | err_unmap: |
| 1304 | srp_unmap_data(scmnd, target, req); |
| 1305 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1306 | err_iu: |
| 1307 | srp_put_tx_iu(target, iu, SRP_IU_CMD); |
| 1308 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1309 | spin_lock_irqsave(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1310 | list_add(&req->list, &target->free_reqs); |
Bart Van Assche | 695b834 | 2011-01-13 19:02:25 +0000 | [diff] [blame] | 1311 | |
| 1312 | err_unlock: |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1313 | spin_unlock_irqrestore(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1314 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1315 | err: |
| 1316 | return SCSI_MLQUEUE_HOST_BUSY; |
| 1317 | } |
| 1318 | |
| 1319 | static int srp_alloc_iu_bufs(struct srp_target_port *target) |
| 1320 | { |
| 1321 | int i; |
| 1322 | |
| 1323 | for (i = 0; i < SRP_RQ_SIZE; ++i) { |
| 1324 | target->rx_ring[i] = srp_alloc_iu(target->srp_host, |
| 1325 | target->max_ti_iu_len, |
| 1326 | GFP_KERNEL, DMA_FROM_DEVICE); |
| 1327 | if (!target->rx_ring[i]) |
| 1328 | goto err; |
| 1329 | } |
| 1330 | |
Bart Van Assche | dd5e6e3 | 2010-08-30 19:27:20 +0000 | [diff] [blame] | 1331 | for (i = 0; i < SRP_SQ_SIZE; ++i) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1332 | target->tx_ring[i] = srp_alloc_iu(target->srp_host, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1333 | target->max_iu_len, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1334 | GFP_KERNEL, DMA_TO_DEVICE); |
| 1335 | if (!target->tx_ring[i]) |
| 1336 | goto err; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1337 | |
| 1338 | list_add(&target->tx_ring[i]->list, &target->free_tx); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | return 0; |
| 1342 | |
| 1343 | err: |
| 1344 | for (i = 0; i < SRP_RQ_SIZE; ++i) { |
| 1345 | srp_free_iu(target->srp_host, target->rx_ring[i]); |
| 1346 | target->rx_ring[i] = NULL; |
| 1347 | } |
| 1348 | |
Bart Van Assche | dd5e6e3 | 2010-08-30 19:27:20 +0000 | [diff] [blame] | 1349 | for (i = 0; i < SRP_SQ_SIZE; ++i) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1350 | srp_free_iu(target->srp_host, target->tx_ring[i]); |
| 1351 | target->tx_ring[i] = NULL; |
| 1352 | } |
| 1353 | |
| 1354 | return -ENOMEM; |
| 1355 | } |
| 1356 | |
David Dillow | 961e0be | 2011-01-14 17:32:07 -0500 | [diff] [blame] | 1357 | static void srp_cm_rep_handler(struct ib_cm_id *cm_id, |
| 1358 | struct srp_login_rsp *lrsp, |
| 1359 | struct srp_target_port *target) |
| 1360 | { |
| 1361 | struct ib_qp_attr *qp_attr = NULL; |
| 1362 | int attr_mask = 0; |
| 1363 | int ret; |
| 1364 | int i; |
| 1365 | |
| 1366 | if (lrsp->opcode == SRP_LOGIN_RSP) { |
| 1367 | target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len); |
| 1368 | target->req_lim = be32_to_cpu(lrsp->req_lim_delta); |
| 1369 | |
| 1370 | /* |
| 1371 | * Reserve credits for task management so we don't |
| 1372 | * bounce requests back to the SCSI mid-layer. |
| 1373 | */ |
| 1374 | target->scsi_host->can_queue |
| 1375 | = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE, |
| 1376 | target->scsi_host->can_queue); |
| 1377 | } else { |
| 1378 | shost_printk(KERN_WARNING, target->scsi_host, |
| 1379 | PFX "Unhandled RSP opcode %#x\n", lrsp->opcode); |
| 1380 | ret = -ECONNRESET; |
| 1381 | goto error; |
| 1382 | } |
| 1383 | |
| 1384 | if (!target->rx_ring[0]) { |
| 1385 | ret = srp_alloc_iu_bufs(target); |
| 1386 | if (ret) |
| 1387 | goto error; |
| 1388 | } |
| 1389 | |
| 1390 | ret = -ENOMEM; |
| 1391 | qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL); |
| 1392 | if (!qp_attr) |
| 1393 | goto error; |
| 1394 | |
| 1395 | qp_attr->qp_state = IB_QPS_RTR; |
| 1396 | ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); |
| 1397 | if (ret) |
| 1398 | goto error_free; |
| 1399 | |
| 1400 | ret = ib_modify_qp(target->qp, qp_attr, attr_mask); |
| 1401 | if (ret) |
| 1402 | goto error_free; |
| 1403 | |
| 1404 | for (i = 0; i < SRP_RQ_SIZE; i++) { |
| 1405 | struct srp_iu *iu = target->rx_ring[i]; |
| 1406 | ret = srp_post_recv(target, iu); |
| 1407 | if (ret) |
| 1408 | goto error_free; |
| 1409 | } |
| 1410 | |
| 1411 | qp_attr->qp_state = IB_QPS_RTS; |
| 1412 | ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); |
| 1413 | if (ret) |
| 1414 | goto error_free; |
| 1415 | |
| 1416 | ret = ib_modify_qp(target->qp, qp_attr, attr_mask); |
| 1417 | if (ret) |
| 1418 | goto error_free; |
| 1419 | |
| 1420 | ret = ib_send_cm_rtu(cm_id, NULL, 0); |
| 1421 | |
| 1422 | error_free: |
| 1423 | kfree(qp_attr); |
| 1424 | |
| 1425 | error: |
| 1426 | target->status = ret; |
| 1427 | } |
| 1428 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1429 | static void srp_cm_rej_handler(struct ib_cm_id *cm_id, |
| 1430 | struct ib_cm_event *event, |
| 1431 | struct srp_target_port *target) |
| 1432 | { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1433 | struct Scsi_Host *shost = target->scsi_host; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1434 | struct ib_class_port_info *cpi; |
| 1435 | int opcode; |
| 1436 | |
| 1437 | switch (event->param.rej_rcvd.reason) { |
| 1438 | case IB_CM_REJ_PORT_CM_REDIRECT: |
| 1439 | cpi = event->param.rej_rcvd.ari; |
| 1440 | target->path.dlid = cpi->redirect_lid; |
| 1441 | target->path.pkey = cpi->redirect_pkey; |
| 1442 | cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff; |
| 1443 | memcpy(target->path.dgid.raw, cpi->redirect_gid, 16); |
| 1444 | |
| 1445 | target->status = target->path.dlid ? |
| 1446 | SRP_DLID_REDIRECT : SRP_PORT_REDIRECT; |
| 1447 | break; |
| 1448 | |
| 1449 | case IB_CM_REJ_PORT_REDIRECT: |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 1450 | if (srp_target_is_topspin(target)) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1451 | /* |
| 1452 | * Topspin/Cisco SRP gateways incorrectly send |
| 1453 | * reject reason code 25 when they mean 24 |
| 1454 | * (port redirect). |
| 1455 | */ |
| 1456 | memcpy(target->path.dgid.raw, |
| 1457 | event->param.rej_rcvd.ari, 16); |
| 1458 | |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1459 | shost_printk(KERN_DEBUG, shost, |
| 1460 | PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n", |
| 1461 | (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix), |
| 1462 | (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id)); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1463 | |
| 1464 | target->status = SRP_PORT_REDIRECT; |
| 1465 | } else { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1466 | shost_printk(KERN_WARNING, shost, |
| 1467 | " REJ reason: IB_CM_REJ_PORT_REDIRECT\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1468 | target->status = -ECONNRESET; |
| 1469 | } |
| 1470 | break; |
| 1471 | |
| 1472 | case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1473 | shost_printk(KERN_WARNING, shost, |
| 1474 | " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1475 | target->status = -ECONNRESET; |
| 1476 | break; |
| 1477 | |
| 1478 | case IB_CM_REJ_CONSUMER_DEFINED: |
| 1479 | opcode = *(u8 *) event->private_data; |
| 1480 | if (opcode == SRP_LOGIN_REJ) { |
| 1481 | struct srp_login_rej *rej = event->private_data; |
| 1482 | u32 reason = be32_to_cpu(rej->reason); |
| 1483 | |
| 1484 | if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1485 | shost_printk(KERN_WARNING, shost, |
| 1486 | PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1487 | else |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1488 | shost_printk(KERN_WARNING, shost, |
| 1489 | PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1490 | } else |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1491 | shost_printk(KERN_WARNING, shost, |
| 1492 | " REJ reason: IB_CM_REJ_CONSUMER_DEFINED," |
| 1493 | " opcode 0x%02x\n", opcode); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1494 | target->status = -ECONNRESET; |
| 1495 | break; |
| 1496 | |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 1497 | case IB_CM_REJ_STALE_CONN: |
| 1498 | shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n"); |
| 1499 | target->status = SRP_STALE_CONN; |
| 1500 | break; |
| 1501 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1502 | default: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1503 | shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n", |
| 1504 | event->param.rej_rcvd.reason); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1505 | target->status = -ECONNRESET; |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) |
| 1510 | { |
| 1511 | struct srp_target_port *target = cm_id->context; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1512 | int comp = 0; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1513 | |
| 1514 | switch (event->event) { |
| 1515 | case IB_CM_REQ_ERROR: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1516 | shost_printk(KERN_DEBUG, target->scsi_host, |
| 1517 | PFX "Sending CM REQ failed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1518 | comp = 1; |
| 1519 | target->status = -ECONNRESET; |
| 1520 | break; |
| 1521 | |
| 1522 | case IB_CM_REP_RECEIVED: |
| 1523 | comp = 1; |
David Dillow | 961e0be | 2011-01-14 17:32:07 -0500 | [diff] [blame] | 1524 | srp_cm_rep_handler(cm_id, event->private_data, target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1525 | break; |
| 1526 | |
| 1527 | case IB_CM_REJ_RECEIVED: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1528 | shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1529 | comp = 1; |
| 1530 | |
| 1531 | srp_cm_rej_handler(cm_id, event, target); |
| 1532 | break; |
| 1533 | |
Ishai Rabinovitz | b7ac4ab | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 1534 | case IB_CM_DREQ_RECEIVED: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1535 | shost_printk(KERN_WARNING, target->scsi_host, |
| 1536 | PFX "DREQ received - connection closed\n"); |
Ishai Rabinovitz | b7ac4ab | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 1537 | if (ib_send_cm_drep(cm_id, NULL, 0)) |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1538 | shost_printk(KERN_ERR, target->scsi_host, |
| 1539 | PFX "Sending CM DREP failed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1540 | break; |
| 1541 | |
| 1542 | case IB_CM_TIMEWAIT_EXIT: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1543 | shost_printk(KERN_ERR, target->scsi_host, |
| 1544 | PFX "connection closed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1545 | |
| 1546 | comp = 1; |
| 1547 | target->status = 0; |
| 1548 | break; |
| 1549 | |
Ishai Rabinovitz | b7ac4ab | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 1550 | case IB_CM_MRA_RECEIVED: |
| 1551 | case IB_CM_DREQ_ERROR: |
| 1552 | case IB_CM_DREP_RECEIVED: |
| 1553 | break; |
| 1554 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1555 | default: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1556 | shost_printk(KERN_WARNING, target->scsi_host, |
| 1557 | PFX "Unhandled CM event %d\n", event->event); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1558 | break; |
| 1559 | } |
| 1560 | |
| 1561 | if (comp) |
| 1562 | complete(&target->done); |
| 1563 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1564 | return 0; |
| 1565 | } |
| 1566 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1567 | static int srp_send_tsk_mgmt(struct srp_target_port *target, |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1568 | u64 req_tag, unsigned int lun, u8 func) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1569 | { |
David Dillow | 19081f3 | 2010-10-18 08:54:49 -0400 | [diff] [blame] | 1570 | struct ib_device *dev = target->srp_host->srp_dev->dev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1571 | struct srp_iu *iu; |
| 1572 | struct srp_tsk_mgmt *tsk_mgmt; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1573 | |
Roland Dreier | 1285b3a | 2006-03-03 15:47:25 -0800 | [diff] [blame] | 1574 | if (target->state == SRP_TARGET_DEAD || |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1575 | target->state == SRP_TARGET_REMOVED) |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1576 | return -1; |
Roland Dreier | 1285b3a | 2006-03-03 15:47:25 -0800 | [diff] [blame] | 1577 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1578 | init_completion(&target->tsk_mgmt_done); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1579 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1580 | spin_lock_irq(&target->lock); |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1581 | iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1582 | spin_unlock_irq(&target->lock); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1583 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1584 | if (!iu) |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1585 | return -1; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1586 | |
David Dillow | 19081f3 | 2010-10-18 08:54:49 -0400 | [diff] [blame] | 1587 | ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt, |
| 1588 | DMA_TO_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1589 | tsk_mgmt = iu->buf; |
| 1590 | memset(tsk_mgmt, 0, sizeof *tsk_mgmt); |
| 1591 | |
| 1592 | tsk_mgmt->opcode = SRP_TSK_MGMT; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1593 | tsk_mgmt->lun = cpu_to_be64((u64) lun << 48); |
| 1594 | tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1595 | tsk_mgmt->tsk_mgmt_func = func; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1596 | tsk_mgmt->task_tag = req_tag; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1597 | |
David Dillow | 19081f3 | 2010-10-18 08:54:49 -0400 | [diff] [blame] | 1598 | ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt, |
| 1599 | DMA_TO_DEVICE); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1600 | if (srp_post_send(target, iu, sizeof *tsk_mgmt)) { |
| 1601 | srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT); |
| 1602 | return -1; |
| 1603 | } |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1604 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1605 | if (!wait_for_completion_timeout(&target->tsk_mgmt_done, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1606 | msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS))) |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1607 | return -1; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1608 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1609 | return 0; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1612 | static int srp_abort(struct scsi_cmnd *scmnd) |
| 1613 | { |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1614 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1615 | struct srp_request *req = (struct srp_request *) scmnd->host_scribble; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1616 | int ret = SUCCESS; |
| 1617 | |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1618 | shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1619 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1620 | if (!req || target->qp_in_error) |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 1621 | return FAILED; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1622 | if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun, |
| 1623 | SRP_TSK_ABORT_TASK)) |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1624 | return FAILED; |
| 1625 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1626 | if (req->scmnd) { |
| 1627 | if (!target->tsk_mgmt_status) { |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 1628 | srp_remove_req(target, req, 0); |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1629 | scmnd->result = DID_ABORT << 16; |
| 1630 | } else |
| 1631 | ret = FAILED; |
| 1632 | } |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1633 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1634 | return ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | static int srp_reset_device(struct scsi_cmnd *scmnd) |
| 1638 | { |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1639 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 1640 | int i; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1641 | |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1642 | shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1643 | |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 1644 | if (target->qp_in_error) |
| 1645 | return FAILED; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1646 | if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun, |
| 1647 | SRP_TSK_LUN_RESET)) |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1648 | return FAILED; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1649 | if (target->tsk_mgmt_status) |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1650 | return FAILED; |
| 1651 | |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 1652 | for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { |
| 1653 | struct srp_request *req = &target->req_ring[i]; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1654 | if (req->scmnd && req->scmnd->device == scmnd->device) |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1655 | srp_reset_req(target, req); |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 1656 | } |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1657 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1658 | return SUCCESS; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | static int srp_reset_host(struct scsi_cmnd *scmnd) |
| 1662 | { |
| 1663 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
| 1664 | int ret = FAILED; |
| 1665 | |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1666 | shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1667 | |
| 1668 | if (!srp_reconnect_target(target)) |
| 1669 | ret = SUCCESS; |
| 1670 | |
| 1671 | return ret; |
| 1672 | } |
| 1673 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1674 | static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr, |
| 1675 | char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1676 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1677 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1678 | |
| 1679 | if (target->state == SRP_TARGET_DEAD || |
| 1680 | target->state == SRP_TARGET_REMOVED) |
| 1681 | return -ENODEV; |
| 1682 | |
| 1683 | return sprintf(buf, "0x%016llx\n", |
| 1684 | (unsigned long long) be64_to_cpu(target->id_ext)); |
| 1685 | } |
| 1686 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1687 | static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr, |
| 1688 | char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1689 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1690 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1691 | |
| 1692 | if (target->state == SRP_TARGET_DEAD || |
| 1693 | target->state == SRP_TARGET_REMOVED) |
| 1694 | return -ENODEV; |
| 1695 | |
| 1696 | return sprintf(buf, "0x%016llx\n", |
| 1697 | (unsigned long long) be64_to_cpu(target->ioc_guid)); |
| 1698 | } |
| 1699 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1700 | static ssize_t show_service_id(struct device *dev, |
| 1701 | struct device_attribute *attr, char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1702 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1703 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1704 | |
| 1705 | if (target->state == SRP_TARGET_DEAD || |
| 1706 | target->state == SRP_TARGET_REMOVED) |
| 1707 | return -ENODEV; |
| 1708 | |
| 1709 | return sprintf(buf, "0x%016llx\n", |
| 1710 | (unsigned long long) be64_to_cpu(target->service_id)); |
| 1711 | } |
| 1712 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1713 | static ssize_t show_pkey(struct device *dev, struct device_attribute *attr, |
| 1714 | char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1715 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1716 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1717 | |
| 1718 | if (target->state == SRP_TARGET_DEAD || |
| 1719 | target->state == SRP_TARGET_REMOVED) |
| 1720 | return -ENODEV; |
| 1721 | |
| 1722 | return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey)); |
| 1723 | } |
| 1724 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1725 | static ssize_t show_dgid(struct device *dev, struct device_attribute *attr, |
| 1726 | char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1727 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1728 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1729 | |
| 1730 | if (target->state == SRP_TARGET_DEAD || |
| 1731 | target->state == SRP_TARGET_REMOVED) |
| 1732 | return -ENODEV; |
| 1733 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 1734 | return sprintf(buf, "%pI6\n", target->path.dgid.raw); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1735 | } |
| 1736 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1737 | static ssize_t show_orig_dgid(struct device *dev, |
| 1738 | struct device_attribute *attr, char *buf) |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 1739 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1740 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 1741 | |
| 1742 | if (target->state == SRP_TARGET_DEAD || |
| 1743 | target->state == SRP_TARGET_REMOVED) |
| 1744 | return -ENODEV; |
| 1745 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 1746 | return sprintf(buf, "%pI6\n", target->orig_dgid); |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Bart Van Assche | 89de748 | 2010-08-03 14:08:45 +0000 | [diff] [blame] | 1749 | static ssize_t show_req_lim(struct device *dev, |
| 1750 | struct device_attribute *attr, char *buf) |
| 1751 | { |
| 1752 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
| 1753 | |
| 1754 | if (target->state == SRP_TARGET_DEAD || |
| 1755 | target->state == SRP_TARGET_REMOVED) |
| 1756 | return -ENODEV; |
| 1757 | |
| 1758 | return sprintf(buf, "%d\n", target->req_lim); |
| 1759 | } |
| 1760 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1761 | static ssize_t show_zero_req_lim(struct device *dev, |
| 1762 | struct device_attribute *attr, char *buf) |
Roland Dreier | 6bfa24f | 2006-06-17 20:37:33 -0700 | [diff] [blame] | 1763 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1764 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6bfa24f | 2006-06-17 20:37:33 -0700 | [diff] [blame] | 1765 | |
| 1766 | if (target->state == SRP_TARGET_DEAD || |
| 1767 | target->state == SRP_TARGET_REMOVED) |
| 1768 | return -ENODEV; |
| 1769 | |
| 1770 | return sprintf(buf, "%d\n", target->zero_req_lim); |
| 1771 | } |
| 1772 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1773 | static ssize_t show_local_ib_port(struct device *dev, |
| 1774 | struct device_attribute *attr, char *buf) |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 1775 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1776 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 1777 | |
| 1778 | return sprintf(buf, "%d\n", target->srp_host->port); |
| 1779 | } |
| 1780 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1781 | static ssize_t show_local_ib_device(struct device *dev, |
| 1782 | struct device_attribute *attr, char *buf) |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 1783 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1784 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 1785 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 1786 | return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name); |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 1787 | } |
| 1788 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1789 | static ssize_t show_cmd_sg_entries(struct device *dev, |
| 1790 | struct device_attribute *attr, char *buf) |
| 1791 | { |
| 1792 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
| 1793 | |
| 1794 | return sprintf(buf, "%u\n", target->cmd_sg_cnt); |
| 1795 | } |
| 1796 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1797 | static ssize_t show_allow_ext_sg(struct device *dev, |
| 1798 | struct device_attribute *attr, char *buf) |
| 1799 | { |
| 1800 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
| 1801 | |
| 1802 | return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false"); |
| 1803 | } |
| 1804 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1805 | static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL); |
| 1806 | static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL); |
| 1807 | static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL); |
| 1808 | static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); |
| 1809 | static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL); |
| 1810 | static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL); |
Bart Van Assche | 89de748 | 2010-08-03 14:08:45 +0000 | [diff] [blame] | 1811 | static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1812 | static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL); |
| 1813 | static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL); |
| 1814 | static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL); |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1815 | static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1816 | static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1817 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1818 | static struct device_attribute *srp_host_attrs[] = { |
| 1819 | &dev_attr_id_ext, |
| 1820 | &dev_attr_ioc_guid, |
| 1821 | &dev_attr_service_id, |
| 1822 | &dev_attr_pkey, |
| 1823 | &dev_attr_dgid, |
| 1824 | &dev_attr_orig_dgid, |
Bart Van Assche | 89de748 | 2010-08-03 14:08:45 +0000 | [diff] [blame] | 1825 | &dev_attr_req_lim, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1826 | &dev_attr_zero_req_lim, |
| 1827 | &dev_attr_local_ib_port, |
| 1828 | &dev_attr_local_ib_device, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1829 | &dev_attr_cmd_sg_entries, |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1830 | &dev_attr_allow_ext_sg, |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1831 | NULL |
| 1832 | }; |
| 1833 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1834 | static struct scsi_host_template srp_template = { |
| 1835 | .module = THIS_MODULE, |
Roland Dreier | b7f008f | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 1836 | .name = "InfiniBand SRP initiator", |
| 1837 | .proc_name = DRV_NAME, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1838 | .info = srp_target_info, |
| 1839 | .queuecommand = srp_queuecommand, |
| 1840 | .eh_abort_handler = srp_abort, |
| 1841 | .eh_device_reset_handler = srp_reset_device, |
| 1842 | .eh_host_reset_handler = srp_reset_host, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1843 | .sg_tablesize = SRP_DEF_SG_TABLESIZE, |
Bart Van Assche | dd5e6e3 | 2010-08-30 19:27:20 +0000 | [diff] [blame] | 1844 | .can_queue = SRP_CMD_SQ_SIZE, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1845 | .this_id = -1, |
Bart Van Assche | dd5e6e3 | 2010-08-30 19:27:20 +0000 | [diff] [blame] | 1846 | .cmd_per_lun = SRP_CMD_SQ_SIZE, |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1847 | .use_clustering = ENABLE_CLUSTERING, |
| 1848 | .shost_attrs = srp_host_attrs |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1849 | }; |
| 1850 | |
| 1851 | static int srp_add_target(struct srp_host *host, struct srp_target_port *target) |
| 1852 | { |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 1853 | struct srp_rport_identifiers ids; |
| 1854 | struct srp_rport *rport; |
| 1855 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1856 | sprintf(target->target_name, "SRP.T10:%016llX", |
| 1857 | (unsigned long long) be64_to_cpu(target->id_ext)); |
| 1858 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 1859 | if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1860 | return -ENODEV; |
| 1861 | |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 1862 | memcpy(ids.port_id, &target->id_ext, 8); |
| 1863 | memcpy(ids.port_id + 8, &target->ioc_guid, 8); |
FUJITA Tomonori | aebd5e4 | 2007-07-11 15:08:15 +0900 | [diff] [blame] | 1864 | ids.roles = SRP_RPORT_ROLE_TARGET; |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 1865 | rport = srp_rport_add(target->scsi_host, &ids); |
| 1866 | if (IS_ERR(rport)) { |
| 1867 | scsi_remove_host(target->scsi_host); |
| 1868 | return PTR_ERR(rport); |
| 1869 | } |
| 1870 | |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 1871 | spin_lock(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1872 | list_add_tail(&target->list, &host->target_list); |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 1873 | spin_unlock(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1874 | |
| 1875 | target->state = SRP_TARGET_LIVE; |
| 1876 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1877 | scsi_scan_target(&target->scsi_host->shost_gendev, |
Matthew Wilcox | 1962a4a | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 1878 | 0, target->scsi_id, SCAN_WILD_CARD, 0); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1879 | |
| 1880 | return 0; |
| 1881 | } |
| 1882 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1883 | static void srp_release_dev(struct device *dev) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1884 | { |
| 1885 | struct srp_host *host = |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1886 | container_of(dev, struct srp_host, dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1887 | |
| 1888 | complete(&host->released); |
| 1889 | } |
| 1890 | |
| 1891 | static struct class srp_class = { |
| 1892 | .name = "infiniband_srp", |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1893 | .dev_release = srp_release_dev |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1894 | }; |
| 1895 | |
| 1896 | /* |
| 1897 | * Target ports are added by writing |
| 1898 | * |
| 1899 | * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>, |
| 1900 | * pkey=<P_Key>,service_id=<service ID> |
| 1901 | * |
| 1902 | * to the add_target sysfs attribute. |
| 1903 | */ |
| 1904 | enum { |
| 1905 | SRP_OPT_ERR = 0, |
| 1906 | SRP_OPT_ID_EXT = 1 << 0, |
| 1907 | SRP_OPT_IOC_GUID = 1 << 1, |
| 1908 | SRP_OPT_DGID = 1 << 2, |
| 1909 | SRP_OPT_PKEY = 1 << 3, |
| 1910 | SRP_OPT_SERVICE_ID = 1 << 4, |
| 1911 | SRP_OPT_MAX_SECT = 1 << 5, |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 1912 | SRP_OPT_MAX_CMD_PER_LUN = 1 << 6, |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1913 | SRP_OPT_IO_CLASS = 1 << 7, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 1914 | SRP_OPT_INITIATOR_EXT = 1 << 8, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1915 | SRP_OPT_CMD_SG_ENTRIES = 1 << 9, |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1916 | SRP_OPT_ALLOW_EXT_SG = 1 << 10, |
| 1917 | SRP_OPT_SG_TABLESIZE = 1 << 11, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1918 | SRP_OPT_ALL = (SRP_OPT_ID_EXT | |
| 1919 | SRP_OPT_IOC_GUID | |
| 1920 | SRP_OPT_DGID | |
| 1921 | SRP_OPT_PKEY | |
| 1922 | SRP_OPT_SERVICE_ID), |
| 1923 | }; |
| 1924 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 1925 | static const match_table_t srp_opt_tokens = { |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 1926 | { SRP_OPT_ID_EXT, "id_ext=%s" }, |
| 1927 | { SRP_OPT_IOC_GUID, "ioc_guid=%s" }, |
| 1928 | { SRP_OPT_DGID, "dgid=%s" }, |
| 1929 | { SRP_OPT_PKEY, "pkey=%x" }, |
| 1930 | { SRP_OPT_SERVICE_ID, "service_id=%s" }, |
| 1931 | { SRP_OPT_MAX_SECT, "max_sect=%d" }, |
| 1932 | { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" }, |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1933 | { SRP_OPT_IO_CLASS, "io_class=%x" }, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 1934 | { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" }, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1935 | { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" }, |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1936 | { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" }, |
| 1937 | { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" }, |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 1938 | { SRP_OPT_ERR, NULL } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1939 | }; |
| 1940 | |
| 1941 | static int srp_parse_options(const char *buf, struct srp_target_port *target) |
| 1942 | { |
| 1943 | char *options, *sep_opt; |
| 1944 | char *p; |
| 1945 | char dgid[3]; |
| 1946 | substring_t args[MAX_OPT_ARGS]; |
| 1947 | int opt_mask = 0; |
| 1948 | int token; |
| 1949 | int ret = -EINVAL; |
| 1950 | int i; |
| 1951 | |
| 1952 | options = kstrdup(buf, GFP_KERNEL); |
| 1953 | if (!options) |
| 1954 | return -ENOMEM; |
| 1955 | |
| 1956 | sep_opt = options; |
| 1957 | while ((p = strsep(&sep_opt, ",")) != NULL) { |
| 1958 | if (!*p) |
| 1959 | continue; |
| 1960 | |
| 1961 | token = match_token(p, srp_opt_tokens, args); |
| 1962 | opt_mask |= token; |
| 1963 | |
| 1964 | switch (token) { |
| 1965 | case SRP_OPT_ID_EXT: |
| 1966 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 1967 | if (!p) { |
| 1968 | ret = -ENOMEM; |
| 1969 | goto out; |
| 1970 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1971 | target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 1972 | kfree(p); |
| 1973 | break; |
| 1974 | |
| 1975 | case SRP_OPT_IOC_GUID: |
| 1976 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 1977 | if (!p) { |
| 1978 | ret = -ENOMEM; |
| 1979 | goto out; |
| 1980 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1981 | target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 1982 | kfree(p); |
| 1983 | break; |
| 1984 | |
| 1985 | case SRP_OPT_DGID: |
| 1986 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 1987 | if (!p) { |
| 1988 | ret = -ENOMEM; |
| 1989 | goto out; |
| 1990 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1991 | if (strlen(p) != 32) { |
| 1992 | printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p); |
Roland Dreier | ce1823f | 2006-04-03 09:31:04 -0700 | [diff] [blame] | 1993 | kfree(p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1994 | goto out; |
| 1995 | } |
| 1996 | |
| 1997 | for (i = 0; i < 16; ++i) { |
| 1998 | strlcpy(dgid, p + i * 2, 3); |
| 1999 | target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16); |
| 2000 | } |
Roland Dreier | bf17c1c | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 2001 | kfree(p); |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 2002 | memcpy(target->orig_dgid, target->path.dgid.raw, 16); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2003 | break; |
| 2004 | |
| 2005 | case SRP_OPT_PKEY: |
| 2006 | if (match_hex(args, &token)) { |
| 2007 | printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p); |
| 2008 | goto out; |
| 2009 | } |
| 2010 | target->path.pkey = cpu_to_be16(token); |
| 2011 | break; |
| 2012 | |
| 2013 | case SRP_OPT_SERVICE_ID: |
| 2014 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 2015 | if (!p) { |
| 2016 | ret = -ENOMEM; |
| 2017 | goto out; |
| 2018 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2019 | target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
Sean Hefty | 247e020 | 2007-08-08 15:51:18 -0700 | [diff] [blame] | 2020 | target->path.service_id = target->service_id; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2021 | kfree(p); |
| 2022 | break; |
| 2023 | |
| 2024 | case SRP_OPT_MAX_SECT: |
| 2025 | if (match_int(args, &token)) { |
| 2026 | printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p); |
| 2027 | goto out; |
| 2028 | } |
| 2029 | target->scsi_host->max_sectors = token; |
| 2030 | break; |
| 2031 | |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2032 | case SRP_OPT_MAX_CMD_PER_LUN: |
| 2033 | if (match_int(args, &token)) { |
| 2034 | printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p); |
| 2035 | goto out; |
| 2036 | } |
Bart Van Assche | dd5e6e3 | 2010-08-30 19:27:20 +0000 | [diff] [blame] | 2037 | target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE); |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2038 | break; |
| 2039 | |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 2040 | case SRP_OPT_IO_CLASS: |
| 2041 | if (match_hex(args, &token)) { |
| 2042 | printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p); |
| 2043 | goto out; |
| 2044 | } |
| 2045 | if (token != SRP_REV10_IB_IO_CLASS && |
| 2046 | token != SRP_REV16A_IB_IO_CLASS) { |
| 2047 | printk(KERN_WARNING PFX "unknown IO class parameter value" |
| 2048 | " %x specified (use %x or %x).\n", |
| 2049 | token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS); |
| 2050 | goto out; |
| 2051 | } |
| 2052 | target->io_class = token; |
| 2053 | break; |
| 2054 | |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 2055 | case SRP_OPT_INITIATOR_EXT: |
| 2056 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 2057 | if (!p) { |
| 2058 | ret = -ENOMEM; |
| 2059 | goto out; |
| 2060 | } |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 2061 | target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 2062 | kfree(p); |
| 2063 | break; |
| 2064 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2065 | case SRP_OPT_CMD_SG_ENTRIES: |
| 2066 | if (match_int(args, &token) || token < 1 || token > 255) { |
| 2067 | printk(KERN_WARNING PFX "bad max cmd_sg_entries parameter '%s'\n", p); |
| 2068 | goto out; |
| 2069 | } |
| 2070 | target->cmd_sg_cnt = token; |
| 2071 | break; |
| 2072 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2073 | case SRP_OPT_ALLOW_EXT_SG: |
| 2074 | if (match_int(args, &token)) { |
| 2075 | printk(KERN_WARNING PFX "bad allow_ext_sg parameter '%s'\n", p); |
| 2076 | goto out; |
| 2077 | } |
| 2078 | target->allow_ext_sg = !!token; |
| 2079 | break; |
| 2080 | |
| 2081 | case SRP_OPT_SG_TABLESIZE: |
| 2082 | if (match_int(args, &token) || token < 1 || |
| 2083 | token > SCSI_MAX_SG_CHAIN_SEGMENTS) { |
| 2084 | printk(KERN_WARNING PFX "bad max sg_tablesize parameter '%s'\n", p); |
| 2085 | goto out; |
| 2086 | } |
| 2087 | target->sg_tablesize = token; |
| 2088 | break; |
| 2089 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2090 | default: |
| 2091 | printk(KERN_WARNING PFX "unknown parameter or missing value " |
| 2092 | "'%s' in target creation request\n", p); |
| 2093 | goto out; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL) |
| 2098 | ret = 0; |
| 2099 | else |
| 2100 | for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i) |
| 2101 | if ((srp_opt_tokens[i].token & SRP_OPT_ALL) && |
| 2102 | !(srp_opt_tokens[i].token & opt_mask)) |
| 2103 | printk(KERN_WARNING PFX "target creation request is " |
| 2104 | "missing parameter '%s'\n", |
| 2105 | srp_opt_tokens[i].pattern); |
| 2106 | |
| 2107 | out: |
| 2108 | kfree(options); |
| 2109 | return ret; |
| 2110 | } |
| 2111 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2112 | static ssize_t srp_create_target(struct device *dev, |
| 2113 | struct device_attribute *attr, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2114 | const char *buf, size_t count) |
| 2115 | { |
| 2116 | struct srp_host *host = |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2117 | container_of(dev, struct srp_host, dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2118 | struct Scsi_Host *target_host; |
| 2119 | struct srp_target_port *target; |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2120 | struct ib_device *ibdev = host->srp_dev->dev; |
| 2121 | dma_addr_t dma_addr; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2122 | int i, ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2123 | |
| 2124 | target_host = scsi_host_alloc(&srp_template, |
| 2125 | sizeof (struct srp_target_port)); |
| 2126 | if (!target_host) |
| 2127 | return -ENOMEM; |
| 2128 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2129 | target_host->transportt = ib_srp_transport_template; |
Bart Van Assche | fd1b6c4 | 2011-07-13 09:19:16 -0700 | [diff] [blame] | 2130 | target_host->max_channel = 0; |
| 2131 | target_host->max_id = 1; |
Arne Redlich | 3c8edf0 | 2006-11-15 12:43:00 +0100 | [diff] [blame] | 2132 | target_host->max_lun = SRP_MAX_LUN; |
| 2133 | target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb; |
Roland Dreier | 5f06899 | 2005-11-11 14:06:01 -0800 | [diff] [blame] | 2134 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2135 | target = host_to_target(target_host); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2136 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2137 | target->io_class = SRP_REV16A_IB_IO_CLASS; |
| 2138 | target->scsi_host = target_host; |
| 2139 | target->srp_host = host; |
| 2140 | target->lkey = host->srp_dev->mr->lkey; |
| 2141 | target->rkey = host->srp_dev->mr->rkey; |
| 2142 | target->cmd_sg_cnt = cmd_sg_entries; |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2143 | target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries; |
| 2144 | target->allow_ext_sg = allow_ext_sg; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2145 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2146 | ret = srp_parse_options(buf, target); |
| 2147 | if (ret) |
| 2148 | goto err; |
| 2149 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2150 | if (!host->srp_dev->fmr_pool && !target->allow_ext_sg && |
| 2151 | target->cmd_sg_cnt < target->sg_tablesize) { |
| 2152 | printk(KERN_WARNING PFX "No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n"); |
| 2153 | target->sg_tablesize = target->cmd_sg_cnt; |
| 2154 | } |
| 2155 | |
| 2156 | target_host->sg_tablesize = target->sg_tablesize; |
| 2157 | target->indirect_size = target->sg_tablesize * |
| 2158 | sizeof (struct srp_direct_buf); |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2159 | target->max_iu_len = sizeof (struct srp_cmd) + |
| 2160 | sizeof (struct srp_indirect_buf) + |
| 2161 | target->cmd_sg_cnt * sizeof (struct srp_direct_buf); |
| 2162 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2163 | spin_lock_init(&target->lock); |
| 2164 | INIT_LIST_HEAD(&target->free_tx); |
| 2165 | INIT_LIST_HEAD(&target->free_reqs); |
| 2166 | for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { |
| 2167 | struct srp_request *req = &target->req_ring[i]; |
| 2168 | |
| 2169 | req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof (void *), |
| 2170 | GFP_KERNEL); |
| 2171 | req->map_page = kmalloc(SRP_FMR_SIZE * sizeof (void *), |
| 2172 | GFP_KERNEL); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2173 | req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL); |
| 2174 | if (!req->fmr_list || !req->map_page || !req->indirect_desc) |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2175 | goto err_free_mem; |
| 2176 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2177 | dma_addr = ib_dma_map_single(ibdev, req->indirect_desc, |
| 2178 | target->indirect_size, |
| 2179 | DMA_TO_DEVICE); |
| 2180 | if (ib_dma_mapping_error(ibdev, dma_addr)) |
| 2181 | goto err_free_mem; |
| 2182 | |
| 2183 | req->indirect_dma_addr = dma_addr; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2184 | req->index = i; |
| 2185 | list_add_tail(&req->list, &target->free_reqs); |
| 2186 | } |
| 2187 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2188 | ib_query_gid(ibdev, host->port, 0, &target->path.sgid); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2189 | |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2190 | shost_printk(KERN_DEBUG, target->scsi_host, PFX |
| 2191 | "new target: id_ext %016llx ioc_guid %016llx pkey %04x " |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 2192 | "service_id %016llx dgid %pI6\n", |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2193 | (unsigned long long) be64_to_cpu(target->id_ext), |
| 2194 | (unsigned long long) be64_to_cpu(target->ioc_guid), |
| 2195 | be16_to_cpu(target->path.pkey), |
| 2196 | (unsigned long long) be64_to_cpu(target->service_id), |
Harvey Harrison | 8867cd7 | 2008-10-28 22:36:33 -0700 | [diff] [blame] | 2197 | target->path.dgid.raw); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2198 | |
| 2199 | ret = srp_create_target_ib(target); |
| 2200 | if (ret) |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2201 | goto err_free_mem; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2202 | |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 2203 | ret = srp_new_cm_id(target); |
| 2204 | if (ret) |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2205 | goto err_free_ib; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2206 | |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 2207 | target->qp_in_error = 0; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2208 | ret = srp_connect_target(target); |
| 2209 | if (ret) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2210 | shost_printk(KERN_ERR, target->scsi_host, |
| 2211 | PFX "Connection failed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2212 | goto err_cm_id; |
| 2213 | } |
| 2214 | |
| 2215 | ret = srp_add_target(host, target); |
| 2216 | if (ret) |
| 2217 | goto err_disconnect; |
| 2218 | |
| 2219 | return count; |
| 2220 | |
| 2221 | err_disconnect: |
| 2222 | srp_disconnect_target(target); |
| 2223 | |
| 2224 | err_cm_id: |
| 2225 | ib_destroy_cm_id(target->cm_id); |
| 2226 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2227 | err_free_ib: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2228 | srp_free_target_ib(target); |
| 2229 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2230 | err_free_mem: |
| 2231 | srp_free_req_data(target); |
| 2232 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2233 | err: |
| 2234 | scsi_host_put(target_host); |
| 2235 | |
| 2236 | return ret; |
| 2237 | } |
| 2238 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2239 | static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2240 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2241 | static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, |
| 2242 | char *buf) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2243 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2244 | struct srp_host *host = container_of(dev, struct srp_host, dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2245 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 2246 | return sprintf(buf, "%s\n", host->srp_dev->dev->name); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2247 | } |
| 2248 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2249 | static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2250 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2251 | static ssize_t show_port(struct device *dev, struct device_attribute *attr, |
| 2252 | char *buf) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2253 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2254 | struct srp_host *host = container_of(dev, struct srp_host, dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2255 | |
| 2256 | return sprintf(buf, "%d\n", host->port); |
| 2257 | } |
| 2258 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2259 | static DEVICE_ATTR(port, S_IRUGO, show_port, NULL); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2260 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2261 | static struct srp_host *srp_add_port(struct srp_device *device, u8 port) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2262 | { |
| 2263 | struct srp_host *host; |
| 2264 | |
| 2265 | host = kzalloc(sizeof *host, GFP_KERNEL); |
| 2266 | if (!host) |
| 2267 | return NULL; |
| 2268 | |
| 2269 | INIT_LIST_HEAD(&host->target_list); |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2270 | spin_lock_init(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2271 | init_completion(&host->released); |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 2272 | host->srp_dev = device; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2273 | host->port = port; |
| 2274 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2275 | host->dev.class = &srp_class; |
| 2276 | host->dev.parent = device->dev->dma_device; |
Kay Sievers | d927e38 | 2009-01-06 10:44:39 -0800 | [diff] [blame] | 2277 | dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2278 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2279 | if (device_register(&host->dev)) |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2280 | goto free_host; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2281 | if (device_create_file(&host->dev, &dev_attr_add_target)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2282 | goto err_class; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2283 | if (device_create_file(&host->dev, &dev_attr_ibdev)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2284 | goto err_class; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2285 | if (device_create_file(&host->dev, &dev_attr_port)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2286 | goto err_class; |
| 2287 | |
| 2288 | return host; |
| 2289 | |
| 2290 | err_class: |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2291 | device_unregister(&host->dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2292 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2293 | free_host: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2294 | kfree(host); |
| 2295 | |
| 2296 | return NULL; |
| 2297 | } |
| 2298 | |
| 2299 | static void srp_add_one(struct ib_device *device) |
| 2300 | { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2301 | struct srp_device *srp_dev; |
| 2302 | struct ib_device_attr *dev_attr; |
| 2303 | struct ib_fmr_pool_param fmr_param; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2304 | struct srp_host *host; |
David Dillow | be8b981 | 2011-01-18 21:58:09 -0500 | [diff] [blame] | 2305 | int max_pages_per_fmr, fmr_page_shift, s, e, p; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2306 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2307 | dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL); |
| 2308 | if (!dev_attr) |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 2309 | return; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2310 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2311 | if (ib_query_device(device, dev_attr)) { |
| 2312 | printk(KERN_WARNING PFX "Query device failed for %s\n", |
| 2313 | device->name); |
| 2314 | goto free_attr; |
| 2315 | } |
| 2316 | |
| 2317 | srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL); |
| 2318 | if (!srp_dev) |
| 2319 | goto free_attr; |
| 2320 | |
| 2321 | /* |
| 2322 | * Use the smallest page size supported by the HCA, down to a |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2323 | * minimum of 4096 bytes. We're unlikely to build large sglists |
| 2324 | * out of smaller entries. |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2325 | */ |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2326 | fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1); |
| 2327 | srp_dev->fmr_page_size = 1 << fmr_page_shift; |
| 2328 | srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1); |
| 2329 | srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2330 | |
| 2331 | INIT_LIST_HEAD(&srp_dev->dev_list); |
| 2332 | |
| 2333 | srp_dev->dev = device; |
| 2334 | srp_dev->pd = ib_alloc_pd(device); |
| 2335 | if (IS_ERR(srp_dev->pd)) |
| 2336 | goto free_dev; |
| 2337 | |
| 2338 | srp_dev->mr = ib_get_dma_mr(srp_dev->pd, |
| 2339 | IB_ACCESS_LOCAL_WRITE | |
| 2340 | IB_ACCESS_REMOTE_READ | |
| 2341 | IB_ACCESS_REMOTE_WRITE); |
| 2342 | if (IS_ERR(srp_dev->mr)) |
| 2343 | goto err_pd; |
| 2344 | |
David Dillow | be8b981 | 2011-01-18 21:58:09 -0500 | [diff] [blame] | 2345 | for (max_pages_per_fmr = SRP_FMR_SIZE; |
| 2346 | max_pages_per_fmr >= SRP_FMR_MIN_SIZE; |
| 2347 | max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) { |
| 2348 | memset(&fmr_param, 0, sizeof fmr_param); |
| 2349 | fmr_param.pool_size = SRP_FMR_POOL_SIZE; |
| 2350 | fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE; |
| 2351 | fmr_param.cache = 1; |
| 2352 | fmr_param.max_pages_per_fmr = max_pages_per_fmr; |
| 2353 | fmr_param.page_shift = fmr_page_shift; |
| 2354 | fmr_param.access = (IB_ACCESS_LOCAL_WRITE | |
| 2355 | IB_ACCESS_REMOTE_WRITE | |
| 2356 | IB_ACCESS_REMOTE_READ); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2357 | |
David Dillow | be8b981 | 2011-01-18 21:58:09 -0500 | [diff] [blame] | 2358 | srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param); |
| 2359 | if (!IS_ERR(srp_dev->fmr_pool)) |
| 2360 | break; |
| 2361 | } |
| 2362 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2363 | if (IS_ERR(srp_dev->fmr_pool)) |
| 2364 | srp_dev->fmr_pool = NULL; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2365 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2366 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2367 | s = 0; |
| 2368 | e = 0; |
| 2369 | } else { |
| 2370 | s = 1; |
| 2371 | e = device->phys_port_cnt; |
| 2372 | } |
| 2373 | |
| 2374 | for (p = s; p <= e; ++p) { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2375 | host = srp_add_port(srp_dev, p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2376 | if (host) |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2377 | list_add_tail(&host->list, &srp_dev->dev_list); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2378 | } |
| 2379 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2380 | ib_set_client_data(device, &srp_client, srp_dev); |
| 2381 | |
| 2382 | goto free_attr; |
| 2383 | |
| 2384 | err_pd: |
| 2385 | ib_dealloc_pd(srp_dev->pd); |
| 2386 | |
| 2387 | free_dev: |
| 2388 | kfree(srp_dev); |
| 2389 | |
| 2390 | free_attr: |
| 2391 | kfree(dev_attr); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | static void srp_remove_one(struct ib_device *device) |
| 2395 | { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2396 | struct srp_device *srp_dev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2397 | struct srp_host *host, *tmp_host; |
| 2398 | LIST_HEAD(target_list); |
| 2399 | struct srp_target_port *target, *tmp_target; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2400 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2401 | srp_dev = ib_get_client_data(device, &srp_client); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2402 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2403 | list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2404 | device_unregister(&host->dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2405 | /* |
| 2406 | * Wait for the sysfs entry to go away, so that no new |
| 2407 | * target ports can be created. |
| 2408 | */ |
| 2409 | wait_for_completion(&host->released); |
| 2410 | |
| 2411 | /* |
| 2412 | * Mark all target ports as removed, so we stop queueing |
| 2413 | * commands and don't try to reconnect. |
| 2414 | */ |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2415 | spin_lock(&host->target_lock); |
Matthew Wilcox | 549c5fc2 | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2416 | list_for_each_entry(target, &host->target_list, list) { |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 2417 | spin_lock_irq(&target->lock); |
Ishai Rabinovitz | 0c5b395 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2418 | target->state = SRP_TARGET_REMOVED; |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 2419 | spin_unlock_irq(&target->lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2420 | } |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2421 | spin_unlock(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2422 | |
| 2423 | /* |
| 2424 | * Wait for any reconnection tasks that may have |
| 2425 | * started before we marked our target ports as |
| 2426 | * removed, and any target port removal tasks. |
| 2427 | */ |
Tejun Heo | f062671 | 2010-10-19 15:24:36 +0000 | [diff] [blame] | 2428 | flush_workqueue(ib_wq); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2429 | |
| 2430 | list_for_each_entry_safe(target, tmp_target, |
| 2431 | &host->target_list, list) { |
David Dillow | b0e47c8 | 2008-01-03 10:25:27 -0800 | [diff] [blame] | 2432 | srp_remove_host(target->scsi_host); |
Dave Dillow | ad69698 | 2008-01-03 22:35:41 -0500 | [diff] [blame] | 2433 | scsi_remove_host(target->scsi_host); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2434 | srp_disconnect_target(target); |
| 2435 | ib_destroy_cm_id(target->cm_id); |
| 2436 | srp_free_target_ib(target); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 2437 | srp_free_req_data(target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2438 | scsi_host_put(target->scsi_host); |
| 2439 | } |
| 2440 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2441 | kfree(host); |
| 2442 | } |
| 2443 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2444 | if (srp_dev->fmr_pool) |
| 2445 | ib_destroy_fmr_pool(srp_dev->fmr_pool); |
| 2446 | ib_dereg_mr(srp_dev->mr); |
| 2447 | ib_dealloc_pd(srp_dev->pd); |
| 2448 | |
| 2449 | kfree(srp_dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2450 | } |
| 2451 | |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 2452 | static struct srp_function_template ib_srp_transport_functions = { |
| 2453 | }; |
| 2454 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2455 | static int __init srp_init_module(void) |
| 2456 | { |
| 2457 | int ret; |
| 2458 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 2459 | BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *)); |
Bart Van Assche | dd5e6e3 | 2010-08-30 19:27:20 +0000 | [diff] [blame] | 2460 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2461 | if (srp_sg_tablesize) { |
| 2462 | printk(KERN_WARNING PFX "srp_sg_tablesize is deprecated, please use cmd_sg_entries\n"); |
| 2463 | if (!cmd_sg_entries) |
| 2464 | cmd_sg_entries = srp_sg_tablesize; |
| 2465 | } |
| 2466 | |
| 2467 | if (!cmd_sg_entries) |
| 2468 | cmd_sg_entries = SRP_DEF_SG_TABLESIZE; |
| 2469 | |
| 2470 | if (cmd_sg_entries > 255) { |
| 2471 | printk(KERN_WARNING PFX "Clamping cmd_sg_entries to 255\n"); |
| 2472 | cmd_sg_entries = 255; |
David Dillow | 1e89a19 | 2008-04-16 21:01:12 -0700 | [diff] [blame] | 2473 | } |
| 2474 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2475 | if (!indirect_sg_entries) |
| 2476 | indirect_sg_entries = cmd_sg_entries; |
| 2477 | else if (indirect_sg_entries < cmd_sg_entries) { |
| 2478 | printk(KERN_WARNING PFX "Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n", cmd_sg_entries); |
| 2479 | indirect_sg_entries = cmd_sg_entries; |
| 2480 | } |
| 2481 | |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 2482 | ib_srp_transport_template = |
| 2483 | srp_attach_transport(&ib_srp_transport_functions); |
| 2484 | if (!ib_srp_transport_template) |
| 2485 | return -ENOMEM; |
| 2486 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2487 | ret = class_register(&srp_class); |
| 2488 | if (ret) { |
| 2489 | printk(KERN_ERR PFX "couldn't register class infiniband_srp\n"); |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 2490 | srp_release_transport(ib_srp_transport_template); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2491 | return ret; |
| 2492 | } |
| 2493 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 2494 | ib_sa_register_client(&srp_sa_client); |
| 2495 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2496 | ret = ib_register_client(&srp_client); |
| 2497 | if (ret) { |
| 2498 | printk(KERN_ERR PFX "couldn't register IB client\n"); |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 2499 | srp_release_transport(ib_srp_transport_template); |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 2500 | ib_sa_unregister_client(&srp_sa_client); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2501 | class_unregister(&srp_class); |
| 2502 | return ret; |
| 2503 | } |
| 2504 | |
| 2505 | return 0; |
| 2506 | } |
| 2507 | |
| 2508 | static void __exit srp_cleanup_module(void) |
| 2509 | { |
| 2510 | ib_unregister_client(&srp_client); |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 2511 | ib_sa_unregister_client(&srp_sa_client); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2512 | class_unregister(&srp_class); |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 2513 | srp_release_transport(ib_srp_transport_template); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | module_init(srp_init_module); |
| 2517 | module_exit(srp_cleanup_module); |