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 | |
Joe Perches | d236cd0 | 2013-02-01 14:33:58 -0800 | [diff] [blame] | 33 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 34 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 35 | #include <linux/module.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/err.h> |
| 39 | #include <linux/string.h> |
| 40 | #include <linux/parser.h> |
| 41 | #include <linux/random.h> |
Tim Schmielau | de25968 | 2006-01-08 01:02:05 -0800 | [diff] [blame] | 42 | #include <linux/jiffies.h> |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 43 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 44 | #include <linux/atomic.h> |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 45 | |
| 46 | #include <scsi/scsi.h> |
| 47 | #include <scsi/scsi_device.h> |
| 48 | #include <scsi/scsi_dbg.h> |
Jack Wang | 71444b9 | 2013-11-07 11:37:37 +0100 | [diff] [blame] | 49 | #include <scsi/scsi_tcq.h> |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 50 | #include <scsi/srp.h> |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 51 | #include <scsi/scsi_transport_srp.h> |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 52 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 53 | #include "ib_srp.h" |
| 54 | |
| 55 | #define DRV_NAME "ib_srp" |
| 56 | #define PFX DRV_NAME ": " |
Vu Pham | e8ca413 | 2013-06-28 14:59:08 +0200 | [diff] [blame] | 57 | #define DRV_VERSION "1.0" |
| 58 | #define DRV_RELDATE "July 1, 2013" |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 59 | |
| 60 | MODULE_AUTHOR("Roland Dreier"); |
| 61 | MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator " |
| 62 | "v" DRV_VERSION " (" DRV_RELDATE ")"); |
| 63 | MODULE_LICENSE("Dual BSD/GPL"); |
| 64 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 65 | static unsigned int srp_sg_tablesize; |
| 66 | static unsigned int cmd_sg_entries; |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 67 | static unsigned int indirect_sg_entries; |
| 68 | static bool allow_ext_sg; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 69 | static bool prefer_fr; |
Bart Van Assche | b1b8854 | 2014-05-20 15:06:41 +0200 | [diff] [blame] | 70 | static bool register_always; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 71 | static int topspin_workarounds = 1; |
| 72 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 73 | module_param(srp_sg_tablesize, uint, 0444); |
| 74 | MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries"); |
| 75 | |
| 76 | module_param(cmd_sg_entries, uint, 0444); |
| 77 | MODULE_PARM_DESC(cmd_sg_entries, |
| 78 | "Default number of gather/scatter entries in the SRP command (default is 12, max 255)"); |
| 79 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 80 | module_param(indirect_sg_entries, uint, 0444); |
| 81 | MODULE_PARM_DESC(indirect_sg_entries, |
| 82 | "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")"); |
| 83 | |
| 84 | module_param(allow_ext_sg, bool, 0444); |
| 85 | MODULE_PARM_DESC(allow_ext_sg, |
| 86 | "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)"); |
| 87 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 88 | module_param(topspin_workarounds, int, 0444); |
| 89 | MODULE_PARM_DESC(topspin_workarounds, |
| 90 | "Enable workarounds for Topspin/Cisco SRP target bugs if != 0"); |
| 91 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 92 | module_param(prefer_fr, bool, 0444); |
| 93 | MODULE_PARM_DESC(prefer_fr, |
| 94 | "Whether to use fast registration if both FMR and fast registration are supported"); |
| 95 | |
Bart Van Assche | b1b8854 | 2014-05-20 15:06:41 +0200 | [diff] [blame] | 96 | module_param(register_always, bool, 0444); |
| 97 | MODULE_PARM_DESC(register_always, |
| 98 | "Use memory registration even for contiguous memory regions"); |
| 99 | |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 100 | static struct kernel_param_ops srp_tmo_ops; |
| 101 | |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 102 | static int srp_reconnect_delay = 10; |
| 103 | module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay, |
| 104 | S_IRUGO | S_IWUSR); |
| 105 | MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts"); |
| 106 | |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 107 | static int srp_fast_io_fail_tmo = 15; |
| 108 | module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo, |
| 109 | S_IRUGO | S_IWUSR); |
| 110 | MODULE_PARM_DESC(fast_io_fail_tmo, |
| 111 | "Number of seconds between the observation of a transport" |
| 112 | " layer error and failing all I/O. \"off\" means that this" |
| 113 | " functionality is disabled."); |
| 114 | |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 115 | static int srp_dev_loss_tmo = 600; |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 116 | module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo, |
| 117 | S_IRUGO | S_IWUSR); |
| 118 | MODULE_PARM_DESC(dev_loss_tmo, |
| 119 | "Maximum number of seconds that the SRP transport should" |
| 120 | " insulate transport layer errors. After this time has been" |
| 121 | " exceeded the SCSI host is removed. Should be" |
| 122 | " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT) |
| 123 | " if fast_io_fail_tmo has not been set. \"off\" means that" |
| 124 | " this functionality is disabled."); |
| 125 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 126 | static void srp_add_one(struct ib_device *device); |
| 127 | static void srp_remove_one(struct ib_device *device); |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 128 | static void srp_recv_completion(struct ib_cq *cq, void *target_ptr); |
| 129 | static void srp_send_completion(struct ib_cq *cq, void *target_ptr); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 130 | static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event); |
| 131 | |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 132 | static struct scsi_transport_template *ib_srp_transport_template; |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 133 | static struct workqueue_struct *srp_remove_wq; |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 134 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 135 | static struct ib_client srp_client = { |
| 136 | .name = "srp", |
| 137 | .add = srp_add_one, |
| 138 | .remove = srp_remove_one |
| 139 | }; |
| 140 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 141 | static struct ib_sa_client srp_sa_client; |
| 142 | |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 143 | static int srp_tmo_get(char *buffer, const struct kernel_param *kp) |
| 144 | { |
| 145 | int tmo = *(int *)kp->arg; |
| 146 | |
| 147 | if (tmo >= 0) |
| 148 | return sprintf(buffer, "%d", tmo); |
| 149 | else |
| 150 | return sprintf(buffer, "off"); |
| 151 | } |
| 152 | |
| 153 | static int srp_tmo_set(const char *val, const struct kernel_param *kp) |
| 154 | { |
| 155 | int tmo, res; |
| 156 | |
| 157 | if (strncmp(val, "off", 3) != 0) { |
| 158 | res = kstrtoint(val, 0, &tmo); |
| 159 | if (res) |
| 160 | goto out; |
| 161 | } else { |
| 162 | tmo = -1; |
| 163 | } |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 164 | if (kp->arg == &srp_reconnect_delay) |
| 165 | res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo, |
| 166 | srp_dev_loss_tmo); |
| 167 | else if (kp->arg == &srp_fast_io_fail_tmo) |
| 168 | res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo); |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 169 | else |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 170 | res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo, |
| 171 | tmo); |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 172 | if (res) |
| 173 | goto out; |
| 174 | *(int *)kp->arg = tmo; |
| 175 | |
| 176 | out: |
| 177 | return res; |
| 178 | } |
| 179 | |
| 180 | static struct kernel_param_ops srp_tmo_ops = { |
| 181 | .get = srp_tmo_get, |
| 182 | .set = srp_tmo_set, |
| 183 | }; |
| 184 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 185 | static inline struct srp_target_port *host_to_target(struct Scsi_Host *host) |
| 186 | { |
| 187 | return (struct srp_target_port *) host->hostdata; |
| 188 | } |
| 189 | |
| 190 | static const char *srp_target_info(struct Scsi_Host *host) |
| 191 | { |
| 192 | return host_to_target(host)->target_name; |
| 193 | } |
| 194 | |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 195 | static int srp_target_is_topspin(struct srp_target_port *target) |
| 196 | { |
| 197 | static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad }; |
Raghava Kondapalli | 3d1ff48d | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 198 | static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d }; |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 199 | |
| 200 | return topspin_workarounds && |
Raghava Kondapalli | 3d1ff48d | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 201 | (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) || |
| 202 | !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui)); |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 205 | static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size, |
| 206 | gfp_t gfp_mask, |
| 207 | enum dma_data_direction direction) |
| 208 | { |
| 209 | struct srp_iu *iu; |
| 210 | |
| 211 | iu = kmalloc(sizeof *iu, gfp_mask); |
| 212 | if (!iu) |
| 213 | goto out; |
| 214 | |
| 215 | iu->buf = kzalloc(size, gfp_mask); |
| 216 | if (!iu->buf) |
| 217 | goto out_free_iu; |
| 218 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 219 | iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size, |
| 220 | direction); |
| 221 | if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 222 | goto out_free_buf; |
| 223 | |
| 224 | iu->size = size; |
| 225 | iu->direction = direction; |
| 226 | |
| 227 | return iu; |
| 228 | |
| 229 | out_free_buf: |
| 230 | kfree(iu->buf); |
| 231 | out_free_iu: |
| 232 | kfree(iu); |
| 233 | out: |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | static void srp_free_iu(struct srp_host *host, struct srp_iu *iu) |
| 238 | { |
| 239 | if (!iu) |
| 240 | return; |
| 241 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 242 | ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size, |
| 243 | iu->direction); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 244 | kfree(iu->buf); |
| 245 | kfree(iu); |
| 246 | } |
| 247 | |
| 248 | static void srp_qp_event(struct ib_event *event, void *context) |
| 249 | { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 250 | pr_debug("QP event %d\n", event->event); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static int srp_init_qp(struct srp_target_port *target, |
| 254 | struct ib_qp *qp) |
| 255 | { |
| 256 | struct ib_qp_attr *attr; |
| 257 | int ret; |
| 258 | |
| 259 | attr = kmalloc(sizeof *attr, GFP_KERNEL); |
| 260 | if (!attr) |
| 261 | return -ENOMEM; |
| 262 | |
Roland Dreier | 969a60f | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 263 | ret = ib_find_pkey(target->srp_host->srp_dev->dev, |
| 264 | target->srp_host->port, |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 265 | be16_to_cpu(target->pkey), |
Roland Dreier | 969a60f | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 266 | &attr->pkey_index); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 267 | if (ret) |
| 268 | goto out; |
| 269 | |
| 270 | attr->qp_state = IB_QPS_INIT; |
| 271 | attr->qp_access_flags = (IB_ACCESS_REMOTE_READ | |
| 272 | IB_ACCESS_REMOTE_WRITE); |
| 273 | attr->port_num = target->srp_host->port; |
| 274 | |
| 275 | ret = ib_modify_qp(qp, attr, |
| 276 | IB_QP_STATE | |
| 277 | IB_QP_PKEY_INDEX | |
| 278 | IB_QP_ACCESS_FLAGS | |
| 279 | IB_QP_PORT); |
| 280 | |
| 281 | out: |
| 282 | kfree(attr); |
| 283 | return ret; |
| 284 | } |
| 285 | |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 286 | static int srp_new_cm_id(struct srp_target_port *target) |
| 287 | { |
| 288 | struct ib_cm_id *new_cm_id; |
| 289 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 290 | 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] | 291 | srp_cm_handler, target); |
| 292 | if (IS_ERR(new_cm_id)) |
| 293 | return PTR_ERR(new_cm_id); |
| 294 | |
| 295 | if (target->cm_id) |
| 296 | ib_destroy_cm_id(target->cm_id); |
| 297 | target->cm_id = new_cm_id; |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 298 | target->path.sgid = target->sgid; |
| 299 | target->path.dgid = target->orig_dgid; |
| 300 | target->path.pkey = target->pkey; |
| 301 | target->path.service_id = target->service_id; |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 306 | static struct ib_fmr_pool *srp_alloc_fmr_pool(struct srp_target_port *target) |
| 307 | { |
| 308 | struct srp_device *dev = target->srp_host->srp_dev; |
| 309 | struct ib_fmr_pool_param fmr_param; |
| 310 | |
| 311 | memset(&fmr_param, 0, sizeof(fmr_param)); |
| 312 | fmr_param.pool_size = target->scsi_host->can_queue; |
| 313 | fmr_param.dirty_watermark = fmr_param.pool_size / 4; |
| 314 | fmr_param.cache = 1; |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 315 | fmr_param.max_pages_per_fmr = dev->max_pages_per_mr; |
| 316 | fmr_param.page_shift = ilog2(dev->mr_page_size); |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 317 | fmr_param.access = (IB_ACCESS_LOCAL_WRITE | |
| 318 | IB_ACCESS_REMOTE_WRITE | |
| 319 | IB_ACCESS_REMOTE_READ); |
| 320 | |
| 321 | return ib_create_fmr_pool(dev->pd, &fmr_param); |
| 322 | } |
| 323 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 324 | /** |
| 325 | * srp_destroy_fr_pool() - free the resources owned by a pool |
| 326 | * @pool: Fast registration pool to be destroyed. |
| 327 | */ |
| 328 | static void srp_destroy_fr_pool(struct srp_fr_pool *pool) |
| 329 | { |
| 330 | int i; |
| 331 | struct srp_fr_desc *d; |
| 332 | |
| 333 | if (!pool) |
| 334 | return; |
| 335 | |
| 336 | for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) { |
| 337 | if (d->frpl) |
| 338 | ib_free_fast_reg_page_list(d->frpl); |
| 339 | if (d->mr) |
| 340 | ib_dereg_mr(d->mr); |
| 341 | } |
| 342 | kfree(pool); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * srp_create_fr_pool() - allocate and initialize a pool for fast registration |
| 347 | * @device: IB device to allocate fast registration descriptors for. |
| 348 | * @pd: Protection domain associated with the FR descriptors. |
| 349 | * @pool_size: Number of descriptors to allocate. |
| 350 | * @max_page_list_len: Maximum fast registration work request page list length. |
| 351 | */ |
| 352 | static struct srp_fr_pool *srp_create_fr_pool(struct ib_device *device, |
| 353 | struct ib_pd *pd, int pool_size, |
| 354 | int max_page_list_len) |
| 355 | { |
| 356 | struct srp_fr_pool *pool; |
| 357 | struct srp_fr_desc *d; |
| 358 | struct ib_mr *mr; |
| 359 | struct ib_fast_reg_page_list *frpl; |
| 360 | int i, ret = -EINVAL; |
| 361 | |
| 362 | if (pool_size <= 0) |
| 363 | goto err; |
| 364 | ret = -ENOMEM; |
| 365 | pool = kzalloc(sizeof(struct srp_fr_pool) + |
| 366 | pool_size * sizeof(struct srp_fr_desc), GFP_KERNEL); |
| 367 | if (!pool) |
| 368 | goto err; |
| 369 | pool->size = pool_size; |
| 370 | pool->max_page_list_len = max_page_list_len; |
| 371 | spin_lock_init(&pool->lock); |
| 372 | INIT_LIST_HEAD(&pool->free_list); |
| 373 | |
| 374 | for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) { |
| 375 | mr = ib_alloc_fast_reg_mr(pd, max_page_list_len); |
| 376 | if (IS_ERR(mr)) { |
| 377 | ret = PTR_ERR(mr); |
| 378 | goto destroy_pool; |
| 379 | } |
| 380 | d->mr = mr; |
| 381 | frpl = ib_alloc_fast_reg_page_list(device, max_page_list_len); |
| 382 | if (IS_ERR(frpl)) { |
| 383 | ret = PTR_ERR(frpl); |
| 384 | goto destroy_pool; |
| 385 | } |
| 386 | d->frpl = frpl; |
| 387 | list_add_tail(&d->entry, &pool->free_list); |
| 388 | } |
| 389 | |
| 390 | out: |
| 391 | return pool; |
| 392 | |
| 393 | destroy_pool: |
| 394 | srp_destroy_fr_pool(pool); |
| 395 | |
| 396 | err: |
| 397 | pool = ERR_PTR(ret); |
| 398 | goto out; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * srp_fr_pool_get() - obtain a descriptor suitable for fast registration |
| 403 | * @pool: Pool to obtain descriptor from. |
| 404 | */ |
| 405 | static struct srp_fr_desc *srp_fr_pool_get(struct srp_fr_pool *pool) |
| 406 | { |
| 407 | struct srp_fr_desc *d = NULL; |
| 408 | unsigned long flags; |
| 409 | |
| 410 | spin_lock_irqsave(&pool->lock, flags); |
| 411 | if (!list_empty(&pool->free_list)) { |
| 412 | d = list_first_entry(&pool->free_list, typeof(*d), entry); |
| 413 | list_del(&d->entry); |
| 414 | } |
| 415 | spin_unlock_irqrestore(&pool->lock, flags); |
| 416 | |
| 417 | return d; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * srp_fr_pool_put() - put an FR descriptor back in the free list |
| 422 | * @pool: Pool the descriptor was allocated from. |
| 423 | * @desc: Pointer to an array of fast registration descriptor pointers. |
| 424 | * @n: Number of descriptors to put back. |
| 425 | * |
| 426 | * Note: The caller must already have queued an invalidation request for |
| 427 | * desc->mr->rkey before calling this function. |
| 428 | */ |
| 429 | static void srp_fr_pool_put(struct srp_fr_pool *pool, struct srp_fr_desc **desc, |
| 430 | int n) |
| 431 | { |
| 432 | unsigned long flags; |
| 433 | int i; |
| 434 | |
| 435 | spin_lock_irqsave(&pool->lock, flags); |
| 436 | for (i = 0; i < n; i++) |
| 437 | list_add(&desc[i]->entry, &pool->free_list); |
| 438 | spin_unlock_irqrestore(&pool->lock, flags); |
| 439 | } |
| 440 | |
| 441 | static struct srp_fr_pool *srp_alloc_fr_pool(struct srp_target_port *target) |
| 442 | { |
| 443 | struct srp_device *dev = target->srp_host->srp_dev; |
| 444 | |
| 445 | return srp_create_fr_pool(dev->dev, dev->pd, |
| 446 | target->scsi_host->can_queue, |
| 447 | dev->max_pages_per_mr); |
| 448 | } |
| 449 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 450 | static int srp_create_target_ib(struct srp_target_port *target) |
| 451 | { |
Bart Van Assche | 62154b2 | 2014-05-20 15:04:45 +0200 | [diff] [blame] | 452 | struct srp_device *dev = target->srp_host->srp_dev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 453 | struct ib_qp_init_attr *init_attr; |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 454 | struct ib_cq *recv_cq, *send_cq; |
| 455 | struct ib_qp *qp; |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 456 | struct ib_fmr_pool *fmr_pool = NULL; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 457 | struct srp_fr_pool *fr_pool = NULL; |
| 458 | const int m = 1 + dev->use_fast_reg; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 459 | int ret; |
| 460 | |
| 461 | init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL); |
| 462 | if (!init_attr) |
| 463 | return -ENOMEM; |
| 464 | |
Bart Van Assche | 62154b2 | 2014-05-20 15:04:45 +0200 | [diff] [blame] | 465 | recv_cq = ib_create_cq(dev->dev, srp_recv_completion, NULL, target, |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 466 | target->queue_size, target->comp_vector); |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 467 | if (IS_ERR(recv_cq)) { |
| 468 | ret = PTR_ERR(recv_cq); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 469 | goto err; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Bart Van Assche | 62154b2 | 2014-05-20 15:04:45 +0200 | [diff] [blame] | 472 | send_cq = ib_create_cq(dev->dev, srp_send_completion, NULL, target, |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 473 | m * target->queue_size, target->comp_vector); |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 474 | if (IS_ERR(send_cq)) { |
| 475 | ret = PTR_ERR(send_cq); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 476 | goto err_recv_cq; |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 479 | ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 480 | |
| 481 | init_attr->event_handler = srp_qp_event; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 482 | init_attr->cap.max_send_wr = m * target->queue_size; |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 483 | init_attr->cap.max_recv_wr = target->queue_size; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 484 | init_attr->cap.max_recv_sge = 1; |
| 485 | init_attr->cap.max_send_sge = 1; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 486 | init_attr->sq_sig_type = IB_SIGNAL_REQ_WR; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 487 | init_attr->qp_type = IB_QPT_RC; |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 488 | init_attr->send_cq = send_cq; |
| 489 | init_attr->recv_cq = recv_cq; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 490 | |
Bart Van Assche | 62154b2 | 2014-05-20 15:04:45 +0200 | [diff] [blame] | 491 | qp = ib_create_qp(dev->pd, init_attr); |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 492 | if (IS_ERR(qp)) { |
| 493 | ret = PTR_ERR(qp); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 494 | goto err_send_cq; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 497 | ret = srp_init_qp(target, qp); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 498 | if (ret) |
| 499 | goto err_qp; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 500 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 501 | if (dev->use_fast_reg && dev->has_fr) { |
| 502 | fr_pool = srp_alloc_fr_pool(target); |
| 503 | if (IS_ERR(fr_pool)) { |
| 504 | ret = PTR_ERR(fr_pool); |
| 505 | shost_printk(KERN_WARNING, target->scsi_host, PFX |
| 506 | "FR pool allocation failed (%d)\n", ret); |
| 507 | goto err_qp; |
| 508 | } |
| 509 | if (target->fr_pool) |
| 510 | srp_destroy_fr_pool(target->fr_pool); |
| 511 | target->fr_pool = fr_pool; |
| 512 | } else if (!dev->use_fast_reg && dev->has_fmr) { |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 513 | fmr_pool = srp_alloc_fmr_pool(target); |
| 514 | if (IS_ERR(fmr_pool)) { |
| 515 | ret = PTR_ERR(fmr_pool); |
| 516 | shost_printk(KERN_WARNING, target->scsi_host, PFX |
| 517 | "FMR pool allocation failed (%d)\n", ret); |
| 518 | goto err_qp; |
| 519 | } |
| 520 | if (target->fmr_pool) |
| 521 | ib_destroy_fmr_pool(target->fmr_pool); |
| 522 | target->fmr_pool = fmr_pool; |
| 523 | } |
| 524 | |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 525 | if (target->qp) |
| 526 | ib_destroy_qp(target->qp); |
| 527 | if (target->recv_cq) |
| 528 | ib_destroy_cq(target->recv_cq); |
| 529 | if (target->send_cq) |
| 530 | ib_destroy_cq(target->send_cq); |
| 531 | |
| 532 | target->qp = qp; |
| 533 | target->recv_cq = recv_cq; |
| 534 | target->send_cq = send_cq; |
| 535 | |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 536 | kfree(init_attr); |
| 537 | return 0; |
| 538 | |
| 539 | err_qp: |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 540 | ib_destroy_qp(qp); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 541 | |
| 542 | err_send_cq: |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 543 | ib_destroy_cq(send_cq); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 544 | |
| 545 | err_recv_cq: |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 546 | ib_destroy_cq(recv_cq); |
Roland Dreier | da9d2f0 | 2010-02-24 15:07:59 -0800 | [diff] [blame] | 547 | |
| 548 | err: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 549 | kfree(init_attr); |
| 550 | return ret; |
| 551 | } |
| 552 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 553 | /* |
| 554 | * Note: this function may be called without srp_alloc_iu_bufs() having been |
| 555 | * invoked. Hence the target->[rt]x_ring checks. |
| 556 | */ |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 557 | static void srp_free_target_ib(struct srp_target_port *target) |
| 558 | { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 559 | struct srp_device *dev = target->srp_host->srp_dev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 560 | int i; |
| 561 | |
Bart Van Assche | 394c595 | 2014-10-30 14:46:27 +0100 | [diff] [blame] | 562 | if (target->cm_id) { |
| 563 | ib_destroy_cm_id(target->cm_id); |
| 564 | target->cm_id = NULL; |
| 565 | } |
| 566 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 567 | if (dev->use_fast_reg) { |
| 568 | if (target->fr_pool) |
| 569 | srp_destroy_fr_pool(target->fr_pool); |
| 570 | } else { |
| 571 | if (target->fmr_pool) |
| 572 | ib_destroy_fmr_pool(target->fmr_pool); |
| 573 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 574 | ib_destroy_qp(target->qp); |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 575 | ib_destroy_cq(target->send_cq); |
| 576 | ib_destroy_cq(target->recv_cq); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 577 | |
Ishai Rabinovitz | 73aa89e | 2012-11-26 11:44:53 +0100 | [diff] [blame] | 578 | target->qp = NULL; |
| 579 | target->send_cq = target->recv_cq = NULL; |
| 580 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 581 | if (target->rx_ring) { |
| 582 | for (i = 0; i < target->queue_size; ++i) |
| 583 | srp_free_iu(target->srp_host, target->rx_ring[i]); |
| 584 | kfree(target->rx_ring); |
| 585 | target->rx_ring = NULL; |
| 586 | } |
| 587 | if (target->tx_ring) { |
| 588 | for (i = 0; i < target->queue_size; ++i) |
| 589 | srp_free_iu(target->srp_host, target->tx_ring[i]); |
| 590 | kfree(target->tx_ring); |
| 591 | target->tx_ring = NULL; |
| 592 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static void srp_path_rec_completion(int status, |
| 596 | struct ib_sa_path_rec *pathrec, |
| 597 | void *target_ptr) |
| 598 | { |
| 599 | struct srp_target_port *target = target_ptr; |
| 600 | |
| 601 | target->status = status; |
| 602 | if (status) |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 603 | shost_printk(KERN_ERR, target->scsi_host, |
| 604 | PFX "Got failed path rec status %d\n", status); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 605 | else |
| 606 | target->path = *pathrec; |
| 607 | complete(&target->done); |
| 608 | } |
| 609 | |
| 610 | static int srp_lookup_path(struct srp_target_port *target) |
| 611 | { |
Bart Van Assche | a702adc | 2014-03-14 13:53:10 +0100 | [diff] [blame] | 612 | int ret; |
| 613 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 614 | target->path.numb_path = 1; |
| 615 | |
| 616 | init_completion(&target->done); |
| 617 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 618 | 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] | 619 | target->srp_host->srp_dev->dev, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 620 | target->srp_host->port, |
| 621 | &target->path, |
Sean Hefty | 247e020 | 2007-08-08 15:51:18 -0700 | [diff] [blame] | 622 | IB_SA_PATH_REC_SERVICE_ID | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 623 | IB_SA_PATH_REC_DGID | |
| 624 | IB_SA_PATH_REC_SGID | |
| 625 | IB_SA_PATH_REC_NUMB_PATH | |
| 626 | IB_SA_PATH_REC_PKEY, |
| 627 | SRP_PATH_REC_TIMEOUT_MS, |
| 628 | GFP_KERNEL, |
| 629 | srp_path_rec_completion, |
| 630 | target, &target->path_query); |
| 631 | if (target->path_query_id < 0) |
| 632 | return target->path_query_id; |
| 633 | |
Bart Van Assche | a702adc | 2014-03-14 13:53:10 +0100 | [diff] [blame] | 634 | ret = wait_for_completion_interruptible(&target->done); |
| 635 | if (ret < 0) |
| 636 | return ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 637 | |
| 638 | if (target->status < 0) |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 639 | shost_printk(KERN_WARNING, target->scsi_host, |
| 640 | PFX "Path record query failed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 641 | |
| 642 | return target->status; |
| 643 | } |
| 644 | |
| 645 | static int srp_send_req(struct srp_target_port *target) |
| 646 | { |
| 647 | struct { |
| 648 | struct ib_cm_req_param param; |
| 649 | struct srp_login_req priv; |
| 650 | } *req = NULL; |
| 651 | int status; |
| 652 | |
| 653 | req = kzalloc(sizeof *req, GFP_KERNEL); |
| 654 | if (!req) |
| 655 | return -ENOMEM; |
| 656 | |
| 657 | req->param.primary_path = &target->path; |
| 658 | req->param.alternate_path = NULL; |
| 659 | req->param.service_id = target->service_id; |
| 660 | req->param.qp_num = target->qp->qp_num; |
| 661 | req->param.qp_type = target->qp->qp_type; |
| 662 | req->param.private_data = &req->priv; |
| 663 | req->param.private_data_len = sizeof req->priv; |
| 664 | req->param.flow_control = 1; |
| 665 | |
| 666 | get_random_bytes(&req->param.starting_psn, 4); |
| 667 | req->param.starting_psn &= 0xffffff; |
| 668 | |
| 669 | /* |
| 670 | * Pick some arbitrary defaults here; we could make these |
| 671 | * module parameters if anyone cared about setting them. |
| 672 | */ |
| 673 | req->param.responder_resources = 4; |
| 674 | req->param.remote_cm_response_timeout = 20; |
| 675 | req->param.local_cm_response_timeout = 20; |
Vu Pham | 7bb312e | 2013-10-26 14:31:27 +0200 | [diff] [blame] | 676 | req->param.retry_count = target->tl_retry_count; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 677 | req->param.rnr_retry_count = 7; |
| 678 | req->param.max_cm_retries = 15; |
| 679 | |
| 680 | req->priv.opcode = SRP_LOGIN_REQ; |
| 681 | req->priv.tag = 0; |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 682 | 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] | 683 | req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | |
| 684 | SRP_BUF_FORMAT_INDIRECT); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 685 | /* |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 686 | * In the published SRP specification (draft rev. 16a), the |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 687 | * port identifier format is 8 bytes of ID extension followed |
| 688 | * by 8 bytes of GUID. Older drafts put the two halves in the |
| 689 | * opposite order, so that the GUID comes first. |
| 690 | * |
| 691 | * Targets conforming to these obsolete drafts can be |
| 692 | * recognized by the I/O Class they report. |
| 693 | */ |
| 694 | if (target->io_class == SRP_REV10_IB_IO_CLASS) { |
| 695 | memcpy(req->priv.initiator_port_id, |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 696 | &target->sgid.global.interface_id, 8); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 697 | memcpy(req->priv.initiator_port_id + 8, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 698 | &target->initiator_ext, 8); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 699 | memcpy(req->priv.target_port_id, &target->ioc_guid, 8); |
| 700 | memcpy(req->priv.target_port_id + 8, &target->id_ext, 8); |
| 701 | } else { |
| 702 | memcpy(req->priv.initiator_port_id, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 703 | &target->initiator_ext, 8); |
| 704 | memcpy(req->priv.initiator_port_id + 8, |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 705 | &target->sgid.global.interface_id, 8); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 706 | memcpy(req->priv.target_port_id, &target->id_ext, 8); |
| 707 | memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8); |
| 708 | } |
| 709 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 710 | /* |
| 711 | * Topspin/Cisco SRP targets will reject our login unless we |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 712 | * zero out the first 8 bytes of our initiator port ID and set |
| 713 | * the second 8 bytes to the local node GUID. |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 714 | */ |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 715 | if (srp_target_is_topspin(target)) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 716 | shost_printk(KERN_DEBUG, target->scsi_host, |
| 717 | PFX "Topspin/Cisco initiator port ID workaround " |
| 718 | "activated for target GUID %016llx\n", |
| 719 | (unsigned long long) be64_to_cpu(target->ioc_guid)); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 720 | memset(req->priv.initiator_port_id, 0, 8); |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 721 | memcpy(req->priv.initiator_port_id + 8, |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 722 | &target->srp_host->srp_dev->dev->node_guid, 8); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 723 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 724 | |
| 725 | status = ib_send_cm_req(target->cm_id, &req->param); |
| 726 | |
| 727 | kfree(req); |
| 728 | |
| 729 | return status; |
| 730 | } |
| 731 | |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 732 | static bool srp_queue_remove_work(struct srp_target_port *target) |
| 733 | { |
| 734 | bool changed = false; |
| 735 | |
| 736 | spin_lock_irq(&target->lock); |
| 737 | if (target->state != SRP_TARGET_REMOVED) { |
| 738 | target->state = SRP_TARGET_REMOVED; |
| 739 | changed = true; |
| 740 | } |
| 741 | spin_unlock_irq(&target->lock); |
| 742 | |
| 743 | if (changed) |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 744 | queue_work(srp_remove_wq, &target->remove_work); |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 745 | |
| 746 | return changed; |
| 747 | } |
| 748 | |
Bart Van Assche | 294c875 | 2011-12-25 12:18:12 +0000 | [diff] [blame] | 749 | static bool srp_change_conn_state(struct srp_target_port *target, |
| 750 | bool connected) |
| 751 | { |
| 752 | bool changed = false; |
| 753 | |
| 754 | spin_lock_irq(&target->lock); |
| 755 | if (target->connected != connected) { |
| 756 | target->connected = connected; |
| 757 | changed = true; |
| 758 | } |
| 759 | spin_unlock_irq(&target->lock); |
| 760 | |
| 761 | return changed; |
| 762 | } |
| 763 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 764 | static void srp_disconnect_target(struct srp_target_port *target) |
| 765 | { |
Bart Van Assche | 294c875 | 2011-12-25 12:18:12 +0000 | [diff] [blame] | 766 | if (srp_change_conn_state(target, false)) { |
| 767 | /* XXX should send SRP_I_LOGOUT request */ |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 768 | |
Bart Van Assche | 294c875 | 2011-12-25 12:18:12 +0000 | [diff] [blame] | 769 | if (ib_send_cm_dreq(target->cm_id, NULL, 0)) { |
| 770 | shost_printk(KERN_DEBUG, target->scsi_host, |
| 771 | PFX "Sending CM DREQ failed\n"); |
Bart Van Assche | 294c875 | 2011-12-25 12:18:12 +0000 | [diff] [blame] | 772 | } |
Roland Dreier | e658105 | 2006-05-17 09:13:21 -0700 | [diff] [blame] | 773 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 774 | } |
| 775 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 776 | static void srp_free_req_data(struct srp_target_port *target) |
| 777 | { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 778 | struct srp_device *dev = target->srp_host->srp_dev; |
| 779 | struct ib_device *ibdev = dev->dev; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 780 | struct srp_request *req; |
| 781 | int i; |
| 782 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 783 | if (!target->req_ring) |
| 784 | return; |
| 785 | |
| 786 | for (i = 0; i < target->req_ring_size; ++i) { |
| 787 | req = &target->req_ring[i]; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 788 | if (dev->use_fast_reg) |
| 789 | kfree(req->fr_list); |
| 790 | else |
| 791 | kfree(req->fmr_list); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 792 | kfree(req->map_page); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 793 | if (req->indirect_dma_addr) { |
| 794 | ib_dma_unmap_single(ibdev, req->indirect_dma_addr, |
| 795 | target->indirect_size, |
| 796 | DMA_TO_DEVICE); |
| 797 | } |
| 798 | kfree(req->indirect_desc); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 799 | } |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 800 | |
| 801 | kfree(target->req_ring); |
| 802 | target->req_ring = NULL; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 803 | } |
| 804 | |
Bart Van Assche | b81d00b | 2013-10-26 14:38:47 +0200 | [diff] [blame] | 805 | static int srp_alloc_req_data(struct srp_target_port *target) |
| 806 | { |
| 807 | struct srp_device *srp_dev = target->srp_host->srp_dev; |
| 808 | struct ib_device *ibdev = srp_dev->dev; |
| 809 | struct srp_request *req; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 810 | void *mr_list; |
Bart Van Assche | b81d00b | 2013-10-26 14:38:47 +0200 | [diff] [blame] | 811 | dma_addr_t dma_addr; |
| 812 | int i, ret = -ENOMEM; |
| 813 | |
| 814 | INIT_LIST_HEAD(&target->free_reqs); |
| 815 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 816 | target->req_ring = kzalloc(target->req_ring_size * |
| 817 | sizeof(*target->req_ring), GFP_KERNEL); |
| 818 | if (!target->req_ring) |
| 819 | goto out; |
| 820 | |
| 821 | for (i = 0; i < target->req_ring_size; ++i) { |
Bart Van Assche | b81d00b | 2013-10-26 14:38:47 +0200 | [diff] [blame] | 822 | req = &target->req_ring[i]; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 823 | mr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *), |
| 824 | GFP_KERNEL); |
| 825 | if (!mr_list) |
| 826 | goto out; |
| 827 | if (srp_dev->use_fast_reg) |
| 828 | req->fr_list = mr_list; |
| 829 | else |
| 830 | req->fmr_list = mr_list; |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 831 | req->map_page = kmalloc(srp_dev->max_pages_per_mr * |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 832 | sizeof(void *), GFP_KERNEL); |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 833 | if (!req->map_page) |
| 834 | goto out; |
Bart Van Assche | b81d00b | 2013-10-26 14:38:47 +0200 | [diff] [blame] | 835 | req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL); |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 836 | if (!req->indirect_desc) |
Bart Van Assche | b81d00b | 2013-10-26 14:38:47 +0200 | [diff] [blame] | 837 | goto out; |
| 838 | |
| 839 | dma_addr = ib_dma_map_single(ibdev, req->indirect_desc, |
| 840 | target->indirect_size, |
| 841 | DMA_TO_DEVICE); |
| 842 | if (ib_dma_mapping_error(ibdev, dma_addr)) |
| 843 | goto out; |
| 844 | |
| 845 | req->indirect_dma_addr = dma_addr; |
| 846 | req->index = i; |
| 847 | list_add_tail(&req->list, &target->free_reqs); |
| 848 | } |
| 849 | ret = 0; |
| 850 | |
| 851 | out: |
| 852 | return ret; |
| 853 | } |
| 854 | |
Bart Van Assche | 683b159 | 2012-01-14 12:40:44 +0000 | [diff] [blame] | 855 | /** |
| 856 | * srp_del_scsi_host_attr() - Remove attributes defined in the host template. |
| 857 | * @shost: SCSI host whose attributes to remove from sysfs. |
| 858 | * |
| 859 | * Note: Any attributes defined in the host template and that did not exist |
| 860 | * before invocation of this function will be ignored. |
| 861 | */ |
| 862 | static void srp_del_scsi_host_attr(struct Scsi_Host *shost) |
| 863 | { |
| 864 | struct device_attribute **attr; |
| 865 | |
| 866 | for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr) |
| 867 | device_remove_file(&shost->shost_dev, *attr); |
| 868 | } |
| 869 | |
Bart Van Assche | ee12d6a | 2011-12-25 19:41:07 +0000 | [diff] [blame] | 870 | static void srp_remove_target(struct srp_target_port *target) |
| 871 | { |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 872 | WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED); |
| 873 | |
Bart Van Assche | ee12d6a | 2011-12-25 19:41:07 +0000 | [diff] [blame] | 874 | srp_del_scsi_host_attr(target->scsi_host); |
Bart Van Assche | 9dd69a6 | 2013-10-26 14:32:30 +0200 | [diff] [blame] | 875 | srp_rport_get(target->rport); |
Bart Van Assche | ee12d6a | 2011-12-25 19:41:07 +0000 | [diff] [blame] | 876 | srp_remove_host(target->scsi_host); |
| 877 | scsi_remove_host(target->scsi_host); |
Bart Van Assche | 9307916 | 2013-12-11 17:06:14 +0100 | [diff] [blame] | 878 | srp_stop_rport_timers(target->rport); |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 879 | srp_disconnect_target(target); |
Bart Van Assche | ee12d6a | 2011-12-25 19:41:07 +0000 | [diff] [blame] | 880 | srp_free_target_ib(target); |
Bart Van Assche | c1120f8 | 2013-10-26 14:35:08 +0200 | [diff] [blame] | 881 | cancel_work_sync(&target->tl_err_work); |
Bart Van Assche | 9dd69a6 | 2013-10-26 14:32:30 +0200 | [diff] [blame] | 882 | srp_rport_put(target->rport); |
Bart Van Assche | ee12d6a | 2011-12-25 19:41:07 +0000 | [diff] [blame] | 883 | srp_free_req_data(target); |
Vu Pham | 65d7dd2 | 2013-10-10 13:50:29 +0200 | [diff] [blame] | 884 | |
| 885 | spin_lock(&target->srp_host->target_lock); |
| 886 | list_del(&target->list); |
| 887 | spin_unlock(&target->srp_host->target_lock); |
| 888 | |
Bart Van Assche | ee12d6a | 2011-12-25 19:41:07 +0000 | [diff] [blame] | 889 | scsi_host_put(target->scsi_host); |
| 890 | } |
| 891 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 892 | static void srp_remove_work(struct work_struct *work) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 893 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 894 | struct srp_target_port *target = |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 895 | container_of(work, struct srp_target_port, remove_work); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 896 | |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 897 | WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 898 | |
Bart Van Assche | 96fc248 | 2013-06-28 14:51:26 +0200 | [diff] [blame] | 899 | srp_remove_target(target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 900 | } |
| 901 | |
Bart Van Assche | dc1bdbd | 2011-09-16 20:41:13 +0200 | [diff] [blame] | 902 | static void srp_rport_delete(struct srp_rport *rport) |
| 903 | { |
| 904 | struct srp_target_port *target = rport->lld_data; |
| 905 | |
| 906 | srp_queue_remove_work(target); |
| 907 | } |
| 908 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 909 | static int srp_connect_target(struct srp_target_port *target) |
| 910 | { |
| 911 | int ret; |
| 912 | |
Bart Van Assche | 294c875 | 2011-12-25 12:18:12 +0000 | [diff] [blame] | 913 | WARN_ON_ONCE(target->connected); |
| 914 | |
Bart Van Assche | 948d1e8 | 2011-09-03 09:25:42 +0200 | [diff] [blame] | 915 | target->qp_in_error = false; |
| 916 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 917 | ret = srp_lookup_path(target); |
| 918 | if (ret) |
| 919 | return ret; |
| 920 | |
| 921 | while (1) { |
| 922 | init_completion(&target->done); |
| 923 | ret = srp_send_req(target); |
| 924 | if (ret) |
| 925 | return ret; |
Bart Van Assche | a702adc | 2014-03-14 13:53:10 +0100 | [diff] [blame] | 926 | ret = wait_for_completion_interruptible(&target->done); |
| 927 | if (ret < 0) |
| 928 | return ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 929 | |
| 930 | /* |
| 931 | * The CM event handling code will set status to |
| 932 | * SRP_PORT_REDIRECT if we get a port redirect REJ |
| 933 | * back, or SRP_DLID_REDIRECT if we get a lid/qp |
| 934 | * redirect REJ back. |
| 935 | */ |
| 936 | switch (target->status) { |
| 937 | case 0: |
Bart Van Assche | 294c875 | 2011-12-25 12:18:12 +0000 | [diff] [blame] | 938 | srp_change_conn_state(target, true); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 939 | return 0; |
| 940 | |
| 941 | case SRP_PORT_REDIRECT: |
| 942 | ret = srp_lookup_path(target); |
| 943 | if (ret) |
| 944 | return ret; |
| 945 | break; |
| 946 | |
| 947 | case SRP_DLID_REDIRECT: |
| 948 | break; |
| 949 | |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 950 | case SRP_STALE_CONN: |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 951 | shost_printk(KERN_ERR, target->scsi_host, PFX |
Bart Van Assche | 205619f | 2014-10-30 14:46:55 +0100 | [diff] [blame] | 952 | "giving up on stale connection\n"); |
| 953 | target->status = -ECONNRESET; |
| 954 | return target->status; |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 955 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 956 | default: |
| 957 | return target->status; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 962 | static int srp_inv_rkey(struct srp_target_port *target, u32 rkey) |
| 963 | { |
| 964 | struct ib_send_wr *bad_wr; |
| 965 | struct ib_send_wr wr = { |
| 966 | .opcode = IB_WR_LOCAL_INV, |
| 967 | .wr_id = LOCAL_INV_WR_ID_MASK, |
| 968 | .next = NULL, |
| 969 | .num_sge = 0, |
| 970 | .send_flags = 0, |
| 971 | .ex.invalidate_rkey = rkey, |
| 972 | }; |
| 973 | |
| 974 | return ib_post_send(target->qp, &wr, &bad_wr); |
| 975 | } |
| 976 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 977 | static void srp_unmap_data(struct scsi_cmnd *scmnd, |
| 978 | struct srp_target_port *target, |
| 979 | struct srp_request *req) |
| 980 | { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 981 | struct srp_device *dev = target->srp_host->srp_dev; |
| 982 | struct ib_device *ibdev = dev->dev; |
| 983 | int i, res; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 984 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 985 | if (!scsi_sglist(scmnd) || |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 986 | (scmnd->sc_data_direction != DMA_TO_DEVICE && |
| 987 | scmnd->sc_data_direction != DMA_FROM_DEVICE)) |
| 988 | return; |
| 989 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 990 | if (dev->use_fast_reg) { |
| 991 | struct srp_fr_desc **pfr; |
| 992 | |
| 993 | for (i = req->nmdesc, pfr = req->fr_list; i > 0; i--, pfr++) { |
| 994 | res = srp_inv_rkey(target, (*pfr)->mr->rkey); |
| 995 | if (res < 0) { |
| 996 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 997 | "Queueing INV WR for rkey %#x failed (%d)\n", |
| 998 | (*pfr)->mr->rkey, res); |
| 999 | queue_work(system_long_wq, |
| 1000 | &target->tl_err_work); |
| 1001 | } |
| 1002 | } |
| 1003 | if (req->nmdesc) |
| 1004 | srp_fr_pool_put(target->fr_pool, req->fr_list, |
| 1005 | req->nmdesc); |
| 1006 | } else { |
| 1007 | struct ib_pool_fmr **pfmr; |
| 1008 | |
| 1009 | for (i = req->nmdesc, pfmr = req->fmr_list; i > 0; i--, pfmr++) |
| 1010 | ib_fmr_pool_unmap(*pfmr); |
| 1011 | } |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1012 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1013 | ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd), |
| 1014 | scmnd->sc_data_direction); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1017 | /** |
| 1018 | * srp_claim_req - Take ownership of the scmnd associated with a request. |
| 1019 | * @target: SRP target port. |
| 1020 | * @req: SRP request. |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1021 | * @sdev: If not NULL, only take ownership for this SCSI device. |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1022 | * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take |
| 1023 | * ownership of @req->scmnd if it equals @scmnd. |
| 1024 | * |
| 1025 | * Return value: |
| 1026 | * Either NULL or a pointer to the SCSI command the caller became owner of. |
| 1027 | */ |
| 1028 | static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target, |
| 1029 | struct srp_request *req, |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1030 | struct scsi_device *sdev, |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1031 | struct scsi_cmnd *scmnd) |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1032 | { |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 1033 | unsigned long flags; |
| 1034 | |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1035 | spin_lock_irqsave(&target->lock, flags); |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1036 | if (req->scmnd && |
| 1037 | (!sdev || req->scmnd->device == sdev) && |
| 1038 | (!scmnd || req->scmnd == scmnd)) { |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1039 | scmnd = req->scmnd; |
| 1040 | req->scmnd = NULL; |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1041 | } else { |
| 1042 | scmnd = NULL; |
| 1043 | } |
| 1044 | spin_unlock_irqrestore(&target->lock, flags); |
| 1045 | |
| 1046 | return scmnd; |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * srp_free_req() - Unmap data and add request to the free request list. |
Bart Van Assche | af24663 | 2014-05-20 15:04:21 +0200 | [diff] [blame] | 1051 | * @target: SRP target port. |
| 1052 | * @req: Request to be freed. |
| 1053 | * @scmnd: SCSI command associated with @req. |
| 1054 | * @req_lim_delta: Amount to be added to @target->req_lim. |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1055 | */ |
| 1056 | static void srp_free_req(struct srp_target_port *target, |
| 1057 | struct srp_request *req, struct scsi_cmnd *scmnd, |
| 1058 | s32 req_lim_delta) |
| 1059 | { |
| 1060 | unsigned long flags; |
| 1061 | |
| 1062 | srp_unmap_data(scmnd, target, req); |
| 1063 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1064 | spin_lock_irqsave(&target->lock, flags); |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 1065 | target->req_lim += req_lim_delta; |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 1066 | list_add_tail(&req->list, &target->free_reqs); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1067 | spin_unlock_irqrestore(&target->lock, flags); |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 1070 | static void srp_finish_req(struct srp_target_port *target, |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1071 | struct srp_request *req, struct scsi_device *sdev, |
| 1072 | int result) |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1073 | { |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1074 | struct scsi_cmnd *scmnd = srp_claim_req(target, req, sdev, NULL); |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1075 | |
| 1076 | if (scmnd) { |
Bart Van Assche | 9b796d0 | 2012-08-24 10:27:54 +0000 | [diff] [blame] | 1077 | srp_free_req(target, req, scmnd, 0); |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 1078 | scmnd->result = result; |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1079 | scmnd->scsi_done(scmnd); |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1080 | } |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 1083 | static void srp_terminate_io(struct srp_rport *rport) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1084 | { |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 1085 | struct srp_target_port *target = rport->lld_data; |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1086 | struct Scsi_Host *shost = target->scsi_host; |
| 1087 | struct scsi_device *sdev; |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 1088 | int i; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1089 | |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1090 | /* |
| 1091 | * Invoking srp_terminate_io() while srp_queuecommand() is running |
| 1092 | * is not safe. Hence the warning statement below. |
| 1093 | */ |
| 1094 | shost_for_each_device(sdev, shost) |
| 1095 | WARN_ON_ONCE(sdev->request_queue->request_fn_active); |
| 1096 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 1097 | for (i = 0; i < target->req_ring_size; ++i) { |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 1098 | struct srp_request *req = &target->req_ring[i]; |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1099 | srp_finish_req(target, req, NULL, DID_TRANSPORT_FAILFAST << 16); |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * It is up to the caller to ensure that srp_rport_reconnect() calls are |
| 1105 | * serialized and that no concurrent srp_queuecommand(), srp_abort(), |
| 1106 | * srp_reset_device() or srp_reset_host() calls will occur while this function |
| 1107 | * is in progress. One way to realize that is not to call this function |
| 1108 | * directly but to call srp_reconnect_rport() instead since that last function |
| 1109 | * serializes calls of this function via rport->mutex and also blocks |
| 1110 | * srp_queuecommand() calls before invoking this function. |
| 1111 | */ |
| 1112 | static int srp_rport_reconnect(struct srp_rport *rport) |
| 1113 | { |
| 1114 | struct srp_target_port *target = rport->lld_data; |
| 1115 | int i, ret; |
Bart Van Assche | 09be70a | 2012-03-17 17:18:54 +0000 | [diff] [blame] | 1116 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1117 | srp_disconnect_target(target); |
Bart Van Assche | 34aa654 | 2014-10-30 14:47:22 +0100 | [diff] [blame] | 1118 | |
| 1119 | if (target->state == SRP_TARGET_SCANNING) |
| 1120 | return -ENODEV; |
| 1121 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1122 | /* |
Bart Van Assche | c7c4e7f | 2013-02-21 17:19:04 +0000 | [diff] [blame] | 1123 | * Now get a new local CM ID so that we avoid confusing the target in |
| 1124 | * case things are really fouled up. Doing so also ensures that all CM |
| 1125 | * callbacks will have finished before a new QP is allocated. |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1126 | */ |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 1127 | ret = srp_new_cm_id(target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1128 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 1129 | for (i = 0; i < target->req_ring_size; ++i) { |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 1130 | struct srp_request *req = &target->req_ring[i]; |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1131 | srp_finish_req(target, req, NULL, DID_RESET << 16); |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 1132 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1133 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1134 | /* |
| 1135 | * Whether or not creating a new CM ID succeeded, create a new |
| 1136 | * QP. This guarantees that all callback functions for the old QP have |
| 1137 | * finished before any send requests are posted on the new QP. |
| 1138 | */ |
| 1139 | ret += srp_create_target_ib(target); |
| 1140 | |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 1141 | INIT_LIST_HEAD(&target->free_tx); |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 1142 | for (i = 0; i < target->queue_size; ++i) |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 1143 | list_add(&target->tx_ring[i]->list, &target->free_tx); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1144 | |
Bart Van Assche | c7c4e7f | 2013-02-21 17:19:04 +0000 | [diff] [blame] | 1145 | if (ret == 0) |
| 1146 | ret = srp_connect_target(target); |
Bart Van Assche | 09be70a | 2012-03-17 17:18:54 +0000 | [diff] [blame] | 1147 | |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 1148 | if (ret == 0) |
| 1149 | shost_printk(KERN_INFO, target->scsi_host, |
| 1150 | PFX "reconnect succeeded\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1151 | |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1155 | static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr, |
| 1156 | unsigned int dma_len, u32 rkey) |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1157 | { |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1158 | struct srp_direct_buf *desc = state->desc; |
| 1159 | |
| 1160 | desc->va = cpu_to_be64(dma_addr); |
| 1161 | desc->key = cpu_to_be32(rkey); |
| 1162 | desc->len = cpu_to_be32(dma_len); |
| 1163 | |
| 1164 | state->total_len += dma_len; |
| 1165 | state->desc++; |
| 1166 | state->ndesc++; |
| 1167 | } |
| 1168 | |
| 1169 | static int srp_map_finish_fmr(struct srp_map_state *state, |
| 1170 | struct srp_target_port *target) |
| 1171 | { |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1172 | struct ib_pool_fmr *fmr; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1173 | u64 io_addr = 0; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1174 | |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 1175 | fmr = ib_fmr_pool_map_phys(target->fmr_pool, state->pages, |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1176 | state->npages, io_addr); |
| 1177 | if (IS_ERR(fmr)) |
| 1178 | return PTR_ERR(fmr); |
| 1179 | |
| 1180 | *state->next_fmr++ = fmr; |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 1181 | state->nmdesc++; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1182 | |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 1183 | srp_map_desc(state, 0, state->dma_len, fmr->fmr->rkey); |
Bart Van Assche | 539dde6 | 2014-05-20 15:05:46 +0200 | [diff] [blame] | 1184 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1185 | return 0; |
| 1186 | } |
| 1187 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1188 | static int srp_map_finish_fr(struct srp_map_state *state, |
| 1189 | struct srp_target_port *target) |
| 1190 | { |
| 1191 | struct srp_device *dev = target->srp_host->srp_dev; |
| 1192 | struct ib_send_wr *bad_wr; |
| 1193 | struct ib_send_wr wr; |
| 1194 | struct srp_fr_desc *desc; |
| 1195 | u32 rkey; |
| 1196 | |
| 1197 | desc = srp_fr_pool_get(target->fr_pool); |
| 1198 | if (!desc) |
| 1199 | return -ENOMEM; |
| 1200 | |
| 1201 | rkey = ib_inc_rkey(desc->mr->rkey); |
| 1202 | ib_update_fast_reg_key(desc->mr, rkey); |
| 1203 | |
| 1204 | memcpy(desc->frpl->page_list, state->pages, |
| 1205 | sizeof(state->pages[0]) * state->npages); |
| 1206 | |
| 1207 | memset(&wr, 0, sizeof(wr)); |
| 1208 | wr.opcode = IB_WR_FAST_REG_MR; |
| 1209 | wr.wr_id = FAST_REG_WR_ID_MASK; |
| 1210 | wr.wr.fast_reg.iova_start = state->base_dma_addr; |
| 1211 | wr.wr.fast_reg.page_list = desc->frpl; |
| 1212 | wr.wr.fast_reg.page_list_len = state->npages; |
| 1213 | wr.wr.fast_reg.page_shift = ilog2(dev->mr_page_size); |
| 1214 | wr.wr.fast_reg.length = state->dma_len; |
| 1215 | wr.wr.fast_reg.access_flags = (IB_ACCESS_LOCAL_WRITE | |
| 1216 | IB_ACCESS_REMOTE_READ | |
| 1217 | IB_ACCESS_REMOTE_WRITE); |
| 1218 | wr.wr.fast_reg.rkey = desc->mr->lkey; |
| 1219 | |
| 1220 | *state->next_fr++ = desc; |
| 1221 | state->nmdesc++; |
| 1222 | |
| 1223 | srp_map_desc(state, state->base_dma_addr, state->dma_len, |
| 1224 | desc->mr->rkey); |
| 1225 | |
| 1226 | return ib_post_send(target->qp, &wr, &bad_wr); |
| 1227 | } |
| 1228 | |
Bart Van Assche | 539dde6 | 2014-05-20 15:05:46 +0200 | [diff] [blame] | 1229 | static int srp_finish_mapping(struct srp_map_state *state, |
| 1230 | struct srp_target_port *target) |
| 1231 | { |
| 1232 | int ret = 0; |
| 1233 | |
| 1234 | if (state->npages == 0) |
| 1235 | return 0; |
| 1236 | |
Bart Van Assche | b1b8854 | 2014-05-20 15:06:41 +0200 | [diff] [blame] | 1237 | if (state->npages == 1 && !register_always) |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 1238 | srp_map_desc(state, state->base_dma_addr, state->dma_len, |
Bart Van Assche | 539dde6 | 2014-05-20 15:05:46 +0200 | [diff] [blame] | 1239 | target->rkey); |
| 1240 | else |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1241 | ret = target->srp_host->srp_dev->use_fast_reg ? |
| 1242 | srp_map_finish_fr(state, target) : |
| 1243 | srp_map_finish_fmr(state, target); |
Bart Van Assche | 539dde6 | 2014-05-20 15:05:46 +0200 | [diff] [blame] | 1244 | |
| 1245 | if (ret == 0) { |
| 1246 | state->npages = 0; |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 1247 | state->dma_len = 0; |
Bart Van Assche | 539dde6 | 2014-05-20 15:05:46 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | return ret; |
| 1251 | } |
| 1252 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1253 | static void srp_map_update_start(struct srp_map_state *state, |
| 1254 | struct scatterlist *sg, int sg_index, |
| 1255 | dma_addr_t dma_addr) |
| 1256 | { |
| 1257 | state->unmapped_sg = sg; |
| 1258 | state->unmapped_index = sg_index; |
| 1259 | state->unmapped_addr = dma_addr; |
| 1260 | } |
| 1261 | |
| 1262 | static int srp_map_sg_entry(struct srp_map_state *state, |
| 1263 | struct srp_target_port *target, |
| 1264 | struct scatterlist *sg, int sg_index, |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1265 | bool use_mr) |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1266 | { |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 1267 | struct srp_device *dev = target->srp_host->srp_dev; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1268 | struct ib_device *ibdev = dev->dev; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1269 | dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg); |
| 1270 | unsigned int dma_len = ib_sg_dma_len(ibdev, sg); |
| 1271 | unsigned int len; |
| 1272 | int ret; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1273 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1274 | if (!dma_len) |
| 1275 | return 0; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1276 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1277 | if (!use_mr) { |
| 1278 | /* |
| 1279 | * Once we're in direct map mode for a request, we don't |
| 1280 | * go back to FMR or FR mode, so no need to update anything |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1281 | * other than the descriptor. |
| 1282 | */ |
| 1283 | srp_map_desc(state, dma_addr, dma_len, target->rkey); |
| 1284 | return 0; |
| 1285 | } |
Ishai Rabinovitz | 559ce8f | 2006-08-03 10:35:43 -0700 | [diff] [blame] | 1286 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1287 | /* |
| 1288 | * Since not all RDMA HW drivers support non-zero page offsets for |
| 1289 | * FMR, if we start at an offset into a page, don't merge into the |
| 1290 | * current FMR mapping. Finish it out, and use the kernel's MR for |
| 1291 | * this sg entry. |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1292 | */ |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1293 | if ((!dev->use_fast_reg && dma_addr & ~dev->mr_page_mask) || |
| 1294 | dma_len > dev->mr_max_size) { |
Bart Van Assche | 539dde6 | 2014-05-20 15:05:46 +0200 | [diff] [blame] | 1295 | ret = srp_finish_mapping(state, target); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1296 | if (ret) |
| 1297 | return ret; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1298 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1299 | srp_map_desc(state, dma_addr, dma_len, target->rkey); |
| 1300 | srp_map_update_start(state, NULL, 0, 0); |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1304 | /* |
| 1305 | * If this is the first sg that will be mapped via FMR or via FR, save |
| 1306 | * our position. We need to know the first unmapped entry, its index, |
| 1307 | * and the first unmapped address within that entry to be able to |
| 1308 | * restart mapping after an error. |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1309 | */ |
| 1310 | if (!state->unmapped_sg) |
| 1311 | srp_map_update_start(state, sg, sg_index, dma_addr); |
| 1312 | |
| 1313 | while (dma_len) { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1314 | unsigned offset = dma_addr & ~dev->mr_page_mask; |
| 1315 | if (state->npages == dev->max_pages_per_mr || offset != 0) { |
Bart Van Assche | 539dde6 | 2014-05-20 15:05:46 +0200 | [diff] [blame] | 1316 | ret = srp_finish_mapping(state, target); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1317 | if (ret) |
| 1318 | return ret; |
| 1319 | |
| 1320 | srp_map_update_start(state, sg, sg_index, dma_addr); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1323 | len = min_t(unsigned int, dma_len, dev->mr_page_size - offset); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1324 | |
| 1325 | if (!state->npages) |
| 1326 | state->base_dma_addr = dma_addr; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1327 | state->pages[state->npages++] = dma_addr & dev->mr_page_mask; |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 1328 | state->dma_len += len; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1329 | dma_addr += len; |
| 1330 | dma_len -= len; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1333 | /* |
| 1334 | * If the last entry of the MR wasn't a full page, then we need to |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1335 | * close it out and start a new one -- we can only merge at page |
| 1336 | * boundries. |
| 1337 | */ |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1338 | ret = 0; |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 1339 | if (len != dev->mr_page_size) { |
Bart Van Assche | 539dde6 | 2014-05-20 15:05:46 +0200 | [diff] [blame] | 1340 | ret = srp_finish_mapping(state, target); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1341 | if (!ret) |
| 1342 | srp_map_update_start(state, NULL, 0, 0); |
| 1343 | } |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1344 | return ret; |
| 1345 | } |
| 1346 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1347 | static int srp_map_sg(struct srp_map_state *state, |
| 1348 | struct srp_target_port *target, struct srp_request *req, |
| 1349 | struct scatterlist *scat, int count) |
Bart Van Assche | 76bc1e1 | 2014-05-20 15:05:21 +0200 | [diff] [blame] | 1350 | { |
| 1351 | struct srp_device *dev = target->srp_host->srp_dev; |
| 1352 | struct ib_device *ibdev = dev->dev; |
| 1353 | struct scatterlist *sg; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1354 | int i; |
| 1355 | bool use_mr; |
Bart Van Assche | 76bc1e1 | 2014-05-20 15:05:21 +0200 | [diff] [blame] | 1356 | |
| 1357 | state->desc = req->indirect_desc; |
| 1358 | state->pages = req->map_page; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1359 | if (dev->use_fast_reg) { |
| 1360 | state->next_fr = req->fr_list; |
| 1361 | use_mr = !!target->fr_pool; |
| 1362 | } else { |
| 1363 | state->next_fmr = req->fmr_list; |
| 1364 | use_mr = !!target->fmr_pool; |
| 1365 | } |
Bart Van Assche | 76bc1e1 | 2014-05-20 15:05:21 +0200 | [diff] [blame] | 1366 | |
| 1367 | for_each_sg(scat, sg, count, i) { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1368 | if (srp_map_sg_entry(state, target, sg, i, use_mr)) { |
| 1369 | /* |
| 1370 | * Memory registration failed, so backtrack to the |
| 1371 | * first unmapped entry and continue on without using |
| 1372 | * memory registration. |
Bart Van Assche | 76bc1e1 | 2014-05-20 15:05:21 +0200 | [diff] [blame] | 1373 | */ |
| 1374 | dma_addr_t dma_addr; |
| 1375 | unsigned int dma_len; |
| 1376 | |
| 1377 | backtrack: |
| 1378 | sg = state->unmapped_sg; |
| 1379 | i = state->unmapped_index; |
| 1380 | |
| 1381 | dma_addr = ib_sg_dma_address(ibdev, sg); |
| 1382 | dma_len = ib_sg_dma_len(ibdev, sg); |
| 1383 | dma_len -= (state->unmapped_addr - dma_addr); |
| 1384 | dma_addr = state->unmapped_addr; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1385 | use_mr = false; |
Bart Van Assche | 76bc1e1 | 2014-05-20 15:05:21 +0200 | [diff] [blame] | 1386 | srp_map_desc(state, dma_addr, dma_len, target->rkey); |
| 1387 | } |
| 1388 | } |
| 1389 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1390 | if (use_mr && srp_finish_mapping(state, target)) |
Bart Van Assche | 76bc1e1 | 2014-05-20 15:05:21 +0200 | [diff] [blame] | 1391 | goto backtrack; |
| 1392 | |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 1393 | req->nmdesc = state->nmdesc; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1394 | |
| 1395 | return 0; |
Bart Van Assche | 76bc1e1 | 2014-05-20 15:05:21 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1398 | static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target, |
| 1399 | struct srp_request *req) |
| 1400 | { |
Bart Van Assche | 76bc1e1 | 2014-05-20 15:05:21 +0200 | [diff] [blame] | 1401 | struct scatterlist *scat; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1402 | struct srp_cmd *cmd = req->cmd->buf; |
Bart Van Assche | 76bc1e1 | 2014-05-20 15:05:21 +0200 | [diff] [blame] | 1403 | int len, nents, count; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1404 | struct srp_device *dev; |
| 1405 | struct ib_device *ibdev; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1406 | struct srp_map_state state; |
| 1407 | struct srp_indirect_buf *indirect_hdr; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1408 | u32 table_len; |
| 1409 | u8 fmt; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1410 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 1411 | if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1412 | return sizeof (struct srp_cmd); |
| 1413 | |
| 1414 | if (scmnd->sc_data_direction != DMA_FROM_DEVICE && |
| 1415 | scmnd->sc_data_direction != DMA_TO_DEVICE) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1416 | shost_printk(KERN_WARNING, target->scsi_host, |
| 1417 | PFX "Unhandled data direction %d\n", |
| 1418 | scmnd->sc_data_direction); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1419 | return -EINVAL; |
| 1420 | } |
| 1421 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 1422 | nents = scsi_sg_count(scmnd); |
| 1423 | scat = scsi_sglist(scmnd); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 1424 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 1425 | dev = target->srp_host->srp_dev; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1426 | ibdev = dev->dev; |
| 1427 | |
| 1428 | count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1429 | if (unlikely(count == 0)) |
| 1430 | return -EIO; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1431 | |
| 1432 | fmt = SRP_DATA_DESC_DIRECT; |
| 1433 | len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 1434 | |
Bart Van Assche | b1b8854 | 2014-05-20 15:06:41 +0200 | [diff] [blame] | 1435 | if (count == 1 && !register_always) { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1436 | /* |
| 1437 | * The midlayer only generated a single gather/scatter |
| 1438 | * entry, or DMA mapping coalesced everything to a |
| 1439 | * single entry. So a direct descriptor along with |
| 1440 | * the DMA MR suffices. |
| 1441 | */ |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1442 | struct srp_direct_buf *buf = (void *) cmd->add_data; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1443 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1444 | buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat)); |
David Dillow | 9af7627 | 2010-11-26 15:34:46 -0500 | [diff] [blame] | 1445 | buf->key = cpu_to_be32(target->rkey); |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1446 | buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat)); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 1447 | |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 1448 | req->nmdesc = 0; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1449 | goto map_complete; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1450 | } |
| 1451 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1452 | /* |
| 1453 | * We have more than one scatter/gather entry, so build our indirect |
| 1454 | * descriptor table, trying to merge as many entries as we can. |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1455 | */ |
| 1456 | indirect_hdr = (void *) cmd->add_data; |
| 1457 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1458 | ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr, |
| 1459 | target->indirect_size, DMA_TO_DEVICE); |
| 1460 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1461 | memset(&state, 0, sizeof(state)); |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1462 | srp_map_sg(&state, target, req, scat, count); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1463 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1464 | /* We've mapped the request, now pull as much of the indirect |
| 1465 | * descriptor table as we can into the command buffer. If this |
| 1466 | * target is not using an external indirect table, we are |
| 1467 | * guaranteed to fit into the command, as the SCSI layer won't |
| 1468 | * give us more S/G entries than we allow. |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1469 | */ |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1470 | if (state.ndesc == 1) { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1471 | /* |
| 1472 | * Memory registration collapsed the sg-list into one entry, |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1473 | * so use a direct descriptor. |
| 1474 | */ |
| 1475 | struct srp_direct_buf *buf = (void *) cmd->add_data; |
| 1476 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1477 | *buf = req->indirect_desc[0]; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1478 | goto map_complete; |
| 1479 | } |
| 1480 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1481 | if (unlikely(target->cmd_sg_cnt < state.ndesc && |
| 1482 | !target->allow_ext_sg)) { |
| 1483 | shost_printk(KERN_ERR, target->scsi_host, |
| 1484 | "Could not fit S/G list into SRP_CMD\n"); |
| 1485 | return -EIO; |
| 1486 | } |
| 1487 | |
| 1488 | count = min(state.ndesc, target->cmd_sg_cnt); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1489 | table_len = state.ndesc * sizeof (struct srp_direct_buf); |
| 1490 | |
| 1491 | fmt = SRP_DATA_DESC_INDIRECT; |
| 1492 | len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1493 | len += count * sizeof (struct srp_direct_buf); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1494 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1495 | memcpy(indirect_hdr->desc_list, req->indirect_desc, |
| 1496 | count * sizeof (struct srp_direct_buf)); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1497 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1498 | indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1499 | indirect_hdr->table_desc.key = cpu_to_be32(target->rkey); |
| 1500 | indirect_hdr->table_desc.len = cpu_to_be32(table_len); |
| 1501 | indirect_hdr->len = cpu_to_be32(state.total_len); |
| 1502 | |
| 1503 | if (scmnd->sc_data_direction == DMA_TO_DEVICE) |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1504 | cmd->data_out_desc_cnt = count; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1505 | else |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 1506 | cmd->data_in_desc_cnt = count; |
| 1507 | |
| 1508 | ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len, |
| 1509 | DMA_TO_DEVICE); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 1510 | |
| 1511 | map_complete: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1512 | if (scmnd->sc_data_direction == DMA_TO_DEVICE) |
| 1513 | cmd->buf_fmt = fmt << 4; |
| 1514 | else |
| 1515 | cmd->buf_fmt = fmt; |
| 1516 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1517 | return len; |
| 1518 | } |
| 1519 | |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1520 | /* |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1521 | * Return an IU and possible credit to the free pool |
| 1522 | */ |
| 1523 | static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu, |
| 1524 | enum srp_iu_type iu_type) |
| 1525 | { |
| 1526 | unsigned long flags; |
| 1527 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1528 | spin_lock_irqsave(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1529 | list_add(&iu->list, &target->free_tx); |
| 1530 | if (iu_type != SRP_IU_RSP) |
| 1531 | ++target->req_lim; |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1532 | spin_unlock_irqrestore(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | /* |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1536 | * Must be called with target->lock held to protect req_lim and free_tx. |
| 1537 | * 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] | 1538 | * |
| 1539 | * Note: |
| 1540 | * An upper limit for the number of allocated information units for each |
| 1541 | * request type is: |
| 1542 | * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues |
| 1543 | * more than Scsi_Host.can_queue requests. |
| 1544 | * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE. |
| 1545 | * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than |
| 1546 | * one unanswered SRP request to an initiator. |
| 1547 | */ |
| 1548 | static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target, |
| 1549 | enum srp_iu_type iu_type) |
| 1550 | { |
| 1551 | s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE; |
| 1552 | struct srp_iu *iu; |
| 1553 | |
| 1554 | srp_send_completion(target->send_cq, target); |
| 1555 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1556 | if (list_empty(&target->free_tx)) |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1557 | return NULL; |
| 1558 | |
| 1559 | /* Initiator responses to target requests do not consume credits */ |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1560 | if (iu_type != SRP_IU_RSP) { |
| 1561 | if (target->req_lim <= rsv) { |
| 1562 | ++target->zero_req_lim; |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | |
| 1566 | --target->req_lim; |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1567 | } |
| 1568 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1569 | iu = list_first_entry(&target->free_tx, struct srp_iu, list); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1570 | list_del(&iu->list); |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1571 | return iu; |
| 1572 | } |
| 1573 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1574 | static int srp_post_send(struct srp_target_port *target, |
| 1575 | struct srp_iu *iu, int len) |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1576 | { |
| 1577 | struct ib_sge list; |
| 1578 | struct ib_send_wr wr, *bad_wr; |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1579 | |
| 1580 | list.addr = iu->dma; |
| 1581 | list.length = len; |
David Dillow | 9af7627 | 2010-11-26 15:34:46 -0500 | [diff] [blame] | 1582 | list.lkey = target->lkey; |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1583 | |
| 1584 | wr.next = NULL; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1585 | wr.wr_id = (uintptr_t) iu; |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1586 | wr.sg_list = &list; |
| 1587 | wr.num_sge = 1; |
| 1588 | wr.opcode = IB_WR_SEND; |
| 1589 | wr.send_flags = IB_SEND_SIGNALED; |
| 1590 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1591 | return ib_post_send(target->qp, &wr, &bad_wr); |
David Dillow | 05a1d75 | 2010-10-08 14:48:14 -0400 | [diff] [blame] | 1592 | } |
| 1593 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1594 | 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] | 1595 | { |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1596 | struct ib_recv_wr wr, *bad_wr; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1597 | struct ib_sge list; |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1598 | |
| 1599 | list.addr = iu->dma; |
| 1600 | list.length = iu->size; |
David Dillow | 9af7627 | 2010-11-26 15:34:46 -0500 | [diff] [blame] | 1601 | list.lkey = target->lkey; |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1602 | |
| 1603 | wr.next = NULL; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1604 | wr.wr_id = (uintptr_t) iu; |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1605 | wr.sg_list = &list; |
| 1606 | wr.num_sge = 1; |
| 1607 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1608 | return ib_post_recv(target->qp, &wr, &bad_wr); |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1611 | static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp) |
| 1612 | { |
| 1613 | struct srp_request *req; |
| 1614 | struct scsi_cmnd *scmnd; |
| 1615 | unsigned long flags; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1616 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1617 | if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) { |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1618 | spin_lock_irqsave(&target->lock, flags); |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 1619 | target->req_lim += be32_to_cpu(rsp->req_lim_delta); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1620 | spin_unlock_irqrestore(&target->lock, flags); |
Bart Van Assche | 94a9174 | 2010-11-26 14:50:09 -0500 | [diff] [blame] | 1621 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1622 | target->tsk_mgmt_status = -1; |
| 1623 | if (be32_to_cpu(rsp->resp_data_len) >= 4) |
| 1624 | target->tsk_mgmt_status = rsp->data[3]; |
| 1625 | complete(&target->tsk_mgmt_done); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1626 | } else { |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1627 | req = &target->req_ring[rsp->tag]; |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 1628 | scmnd = srp_claim_req(target, req, NULL, NULL); |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1629 | if (!scmnd) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1630 | shost_printk(KERN_ERR, target->scsi_host, |
| 1631 | "Null scmnd for RSP w/tag %016llx\n", |
| 1632 | (unsigned long long) rsp->tag); |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1633 | |
| 1634 | spin_lock_irqsave(&target->lock, flags); |
| 1635 | target->req_lim += be32_to_cpu(rsp->req_lim_delta); |
| 1636 | spin_unlock_irqrestore(&target->lock, flags); |
| 1637 | |
| 1638 | return; |
| 1639 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1640 | scmnd->result = rsp->status; |
| 1641 | |
| 1642 | if (rsp->flags & SRP_RSP_FLAG_SNSVALID) { |
| 1643 | memcpy(scmnd->sense_buffer, rsp->data + |
| 1644 | be32_to_cpu(rsp->resp_data_len), |
| 1645 | min_t(int, be32_to_cpu(rsp->sense_data_len), |
| 1646 | SCSI_SENSE_BUFFERSIZE)); |
| 1647 | } |
| 1648 | |
Bart Van Assche | e714531 | 2014-07-09 15:57:51 +0200 | [diff] [blame] | 1649 | if (unlikely(rsp->flags & SRP_RSP_FLAG_DIUNDER)) |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 1650 | scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt)); |
Bart Van Assche | e714531 | 2014-07-09 15:57:51 +0200 | [diff] [blame] | 1651 | else if (unlikely(rsp->flags & SRP_RSP_FLAG_DIOVER)) |
| 1652 | scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_in_res_cnt)); |
| 1653 | else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOUNDER)) |
| 1654 | scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt)); |
| 1655 | else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOOVER)) |
| 1656 | scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_out_res_cnt)); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1657 | |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 1658 | srp_free_req(target, req, scmnd, |
| 1659 | be32_to_cpu(rsp->req_lim_delta)); |
| 1660 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1661 | scmnd->host_scribble = NULL; |
| 1662 | scmnd->scsi_done(scmnd); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1663 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1666 | static int srp_response_common(struct srp_target_port *target, s32 req_delta, |
| 1667 | void *rsp, int len) |
| 1668 | { |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1669 | struct ib_device *dev = target->srp_host->srp_dev->dev; |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1670 | unsigned long flags; |
| 1671 | struct srp_iu *iu; |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1672 | int err; |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1673 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1674 | spin_lock_irqsave(&target->lock, flags); |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1675 | target->req_lim += req_delta; |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1676 | iu = __srp_get_tx_iu(target, SRP_IU_RSP); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1677 | spin_unlock_irqrestore(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1678 | |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1679 | if (!iu) { |
| 1680 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1681 | "no IU available to send response\n"); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1682 | return 1; |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE); |
| 1686 | memcpy(iu->buf, rsp, len); |
| 1687 | ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE); |
| 1688 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1689 | err = srp_post_send(target, iu, len); |
| 1690 | if (err) { |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1691 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1692 | "unable to post response: %d\n", err); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1693 | srp_put_tx_iu(target, iu, SRP_IU_RSP); |
| 1694 | } |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1695 | |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1696 | return err; |
| 1697 | } |
| 1698 | |
| 1699 | static void srp_process_cred_req(struct srp_target_port *target, |
| 1700 | struct srp_cred_req *req) |
| 1701 | { |
| 1702 | struct srp_cred_rsp rsp = { |
| 1703 | .opcode = SRP_CRED_RSP, |
| 1704 | .tag = req->tag, |
| 1705 | }; |
| 1706 | s32 delta = be32_to_cpu(req->req_lim_delta); |
| 1707 | |
| 1708 | if (srp_response_common(target, delta, &rsp, sizeof rsp)) |
| 1709 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1710 | "problems processing SRP_CRED_REQ\n"); |
| 1711 | } |
| 1712 | |
| 1713 | static void srp_process_aer_req(struct srp_target_port *target, |
| 1714 | struct srp_aer_req *req) |
| 1715 | { |
| 1716 | struct srp_aer_rsp rsp = { |
| 1717 | .opcode = SRP_AER_RSP, |
| 1718 | .tag = req->tag, |
| 1719 | }; |
| 1720 | s32 delta = be32_to_cpu(req->req_lim_delta); |
| 1721 | |
| 1722 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1723 | "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun)); |
| 1724 | |
| 1725 | if (srp_response_common(target, delta, &rsp, sizeof rsp)) |
| 1726 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1727 | "problems processing SRP_AER_REQ\n"); |
| 1728 | } |
| 1729 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1730 | static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc) |
| 1731 | { |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1732 | struct ib_device *dev = target->srp_host->srp_dev->dev; |
Roland Dreier | 737b94e | 2011-05-23 11:30:04 -0700 | [diff] [blame] | 1733 | 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] | 1734 | int res; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1735 | u8 opcode; |
| 1736 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1737 | ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len, |
| 1738 | DMA_FROM_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1739 | |
| 1740 | opcode = *(u8 *) iu->buf; |
| 1741 | |
| 1742 | if (0) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1743 | shost_printk(KERN_ERR, target->scsi_host, |
| 1744 | PFX "recv completion, opcode 0x%02x\n", opcode); |
Bart Van Assche | 7a70081 | 2010-07-29 15:56:37 +0000 | [diff] [blame] | 1745 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1, |
| 1746 | iu->buf, wc->byte_len, true); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | switch (opcode) { |
| 1750 | case SRP_RSP: |
| 1751 | srp_process_rsp(target, iu->buf); |
| 1752 | break; |
| 1753 | |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1754 | case SRP_CRED_REQ: |
| 1755 | srp_process_cred_req(target, iu->buf); |
| 1756 | break; |
| 1757 | |
| 1758 | case SRP_AER_REQ: |
| 1759 | srp_process_aer_req(target, iu->buf); |
| 1760 | break; |
| 1761 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1762 | case SRP_T_LOGOUT: |
| 1763 | /* XXX Handle target logout */ |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1764 | shost_printk(KERN_WARNING, target->scsi_host, |
| 1765 | PFX "Got target logout request\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1766 | break; |
| 1767 | |
| 1768 | default: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1769 | shost_printk(KERN_WARNING, target->scsi_host, |
| 1770 | PFX "Unhandled SRP opcode 0x%02x\n", opcode); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1771 | break; |
| 1772 | } |
| 1773 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1774 | ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len, |
| 1775 | DMA_FROM_DEVICE); |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1776 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1777 | res = srp_post_recv(target, iu); |
Bart Van Assche | c996bb4 | 2010-07-30 10:59:05 +0000 | [diff] [blame] | 1778 | if (res != 0) |
| 1779 | shost_printk(KERN_ERR, target->scsi_host, |
| 1780 | PFX "Recv failed with error code %d\n", res); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1781 | } |
| 1782 | |
Bart Van Assche | c1120f8 | 2013-10-26 14:35:08 +0200 | [diff] [blame] | 1783 | /** |
| 1784 | * srp_tl_err_work() - handle a transport layer error |
Bart Van Assche | af24663 | 2014-05-20 15:04:21 +0200 | [diff] [blame] | 1785 | * @work: Work structure embedded in an SRP target port. |
Bart Van Assche | c1120f8 | 2013-10-26 14:35:08 +0200 | [diff] [blame] | 1786 | * |
| 1787 | * Note: This function may get invoked before the rport has been created, |
| 1788 | * hence the target->rport test. |
| 1789 | */ |
| 1790 | static void srp_tl_err_work(struct work_struct *work) |
| 1791 | { |
| 1792 | struct srp_target_port *target; |
| 1793 | |
| 1794 | target = container_of(work, struct srp_target_port, tl_err_work); |
| 1795 | if (target->rport) |
| 1796 | srp_start_tl_fail_timers(target->rport); |
| 1797 | } |
| 1798 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1799 | static void srp_handle_qp_err(u64 wr_id, enum ib_wc_status wc_status, |
| 1800 | bool send_err, struct srp_target_port *target) |
Bart Van Assche | 948d1e8 | 2011-09-03 09:25:42 +0200 | [diff] [blame] | 1801 | { |
Bart Van Assche | 294c875 | 2011-12-25 12:18:12 +0000 | [diff] [blame] | 1802 | if (target->connected && !target->qp_in_error) { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1803 | if (wr_id & LOCAL_INV_WR_ID_MASK) { |
| 1804 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1805 | "LOCAL_INV failed with status %d\n", |
| 1806 | wc_status); |
| 1807 | } else if (wr_id & FAST_REG_WR_ID_MASK) { |
| 1808 | shost_printk(KERN_ERR, target->scsi_host, PFX |
| 1809 | "FAST_REG_MR failed status %d\n", |
| 1810 | wc_status); |
| 1811 | } else { |
| 1812 | shost_printk(KERN_ERR, target->scsi_host, |
| 1813 | PFX "failed %s status %d for iu %p\n", |
| 1814 | send_err ? "send" : "receive", |
| 1815 | wc_status, (void *)(uintptr_t)wr_id); |
| 1816 | } |
Bart Van Assche | c1120f8 | 2013-10-26 14:35:08 +0200 | [diff] [blame] | 1817 | queue_work(system_long_wq, &target->tl_err_work); |
Bart Van Assche | 4f0af69 | 2012-11-26 11:16:40 +0100 | [diff] [blame] | 1818 | } |
Bart Van Assche | 948d1e8 | 2011-09-03 09:25:42 +0200 | [diff] [blame] | 1819 | target->qp_in_error = true; |
| 1820 | } |
| 1821 | |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 1822 | static void srp_recv_completion(struct ib_cq *cq, void *target_ptr) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1823 | { |
| 1824 | struct srp_target_port *target = target_ptr; |
| 1825 | struct ib_wc wc; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1826 | |
| 1827 | ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); |
| 1828 | while (ib_poll_cq(cq, 1, &wc) > 0) { |
Bart Van Assche | 948d1e8 | 2011-09-03 09:25:42 +0200 | [diff] [blame] | 1829 | if (likely(wc.status == IB_WC_SUCCESS)) { |
| 1830 | srp_handle_recv(target, &wc); |
| 1831 | } else { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1832 | srp_handle_qp_err(wc.wr_id, wc.status, false, target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1833 | } |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | static void srp_send_completion(struct ib_cq *cq, void *target_ptr) |
| 1838 | { |
| 1839 | struct srp_target_port *target = target_ptr; |
| 1840 | struct ib_wc wc; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1841 | struct srp_iu *iu; |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 1842 | |
| 1843 | while (ib_poll_cq(cq, 1, &wc) > 0) { |
Bart Van Assche | 948d1e8 | 2011-09-03 09:25:42 +0200 | [diff] [blame] | 1844 | if (likely(wc.status == IB_WC_SUCCESS)) { |
| 1845 | iu = (struct srp_iu *) (uintptr_t) wc.wr_id; |
| 1846 | list_add(&iu->list, &target->free_tx); |
| 1847 | } else { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 1848 | srp_handle_qp_err(wc.wr_id, wc.status, true, target); |
Bart Van Assche | 9c03dc9 | 2010-02-02 19:23:54 +0000 | [diff] [blame] | 1849 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1853 | static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1854 | { |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1855 | struct srp_target_port *target = host_to_target(shost); |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 1856 | struct srp_rport *rport = target->rport; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1857 | struct srp_request *req; |
| 1858 | struct srp_iu *iu; |
| 1859 | struct srp_cmd *cmd; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1860 | struct ib_device *dev; |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1861 | unsigned long flags; |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 1862 | int len, ret; |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 1863 | const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler; |
| 1864 | |
| 1865 | /* |
| 1866 | * The SCSI EH thread is the only context from which srp_queuecommand() |
| 1867 | * can get invoked for blocked devices (SDEV_BLOCK / |
| 1868 | * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by |
| 1869 | * locking the rport mutex if invoked from inside the SCSI EH. |
| 1870 | */ |
| 1871 | if (in_scsi_eh) |
| 1872 | mutex_lock(&rport->mutex); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1873 | |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 1874 | scmnd->result = srp_chkready(target->rport); |
| 1875 | if (unlikely(scmnd->result)) |
| 1876 | goto err; |
Bart Van Assche | 2ce19e7 | 2013-02-21 17:20:00 +0000 | [diff] [blame] | 1877 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1878 | spin_lock_irqsave(&target->lock, flags); |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 1879 | iu = __srp_get_tx_iu(target, SRP_IU_CMD); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1880 | if (!iu) |
Bart Van Assche | 695b834 | 2011-01-13 19:02:25 +0000 | [diff] [blame] | 1881 | goto err_unlock; |
| 1882 | |
| 1883 | req = list_first_entry(&target->free_reqs, struct srp_request, list); |
| 1884 | list_del(&req->list); |
| 1885 | spin_unlock_irqrestore(&target->lock, flags); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1886 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 1887 | dev = target->srp_host->srp_dev->dev; |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1888 | 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] | 1889 | DMA_TO_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1890 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 1891 | scmnd->host_scribble = (void *) req; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1892 | |
| 1893 | cmd = iu->buf; |
| 1894 | memset(cmd, 0, sizeof *cmd); |
| 1895 | |
| 1896 | cmd->opcode = SRP_CMD; |
| 1897 | cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1898 | cmd->tag = req->index; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1899 | memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len); |
| 1900 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1901 | req->scmnd = scmnd; |
| 1902 | req->cmd = iu; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1903 | |
| 1904 | len = srp_map_data(scmnd, target, req); |
| 1905 | if (len < 0) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1906 | shost_printk(KERN_ERR, target->scsi_host, |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 1907 | PFX "Failed to map data (%d)\n", len); |
| 1908 | /* |
| 1909 | * If we ran out of memory descriptors (-ENOMEM) because an |
| 1910 | * application is queuing many requests with more than |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 1911 | * max_pages_per_mr sg-list elements, tell the SCSI mid-layer |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 1912 | * to reduce queue depth temporarily. |
| 1913 | */ |
| 1914 | scmnd->result = len == -ENOMEM ? |
| 1915 | DID_OK << 16 | QUEUE_FULL << 1 : DID_ERROR << 16; |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1916 | goto err_iu; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1917 | } |
| 1918 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1919 | 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] | 1920 | DMA_TO_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1921 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1922 | if (srp_post_send(target, iu, len)) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 1923 | shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1924 | goto err_unmap; |
| 1925 | } |
| 1926 | |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 1927 | ret = 0; |
| 1928 | |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 1929 | unlock_rport: |
| 1930 | if (in_scsi_eh) |
| 1931 | mutex_unlock(&rport->mutex); |
| 1932 | |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 1933 | return ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1934 | |
| 1935 | err_unmap: |
| 1936 | srp_unmap_data(scmnd, target, req); |
| 1937 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1938 | err_iu: |
| 1939 | srp_put_tx_iu(target, iu, SRP_IU_CMD); |
| 1940 | |
Bart Van Assche | 024ca90 | 2014-05-20 15:03:49 +0200 | [diff] [blame] | 1941 | /* |
| 1942 | * Avoid that the loops that iterate over the request ring can |
| 1943 | * encounter a dangling SCSI command pointer. |
| 1944 | */ |
| 1945 | req->scmnd = NULL; |
| 1946 | |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1947 | spin_lock_irqsave(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1948 | list_add(&req->list, &target->free_reqs); |
Bart Van Assche | 695b834 | 2011-01-13 19:02:25 +0000 | [diff] [blame] | 1949 | |
| 1950 | err_unlock: |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 1951 | spin_unlock_irqrestore(&target->lock, flags); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 1952 | |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 1953 | err: |
| 1954 | if (scmnd->result) { |
| 1955 | scmnd->scsi_done(scmnd); |
| 1956 | ret = 0; |
| 1957 | } else { |
| 1958 | ret = SCSI_MLQUEUE_HOST_BUSY; |
| 1959 | } |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 1960 | |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 1961 | goto unlock_rport; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1962 | } |
| 1963 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 1964 | /* |
| 1965 | * Note: the resources allocated in this function are freed in |
| 1966 | * srp_free_target_ib(). |
| 1967 | */ |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1968 | static int srp_alloc_iu_bufs(struct srp_target_port *target) |
| 1969 | { |
| 1970 | int i; |
| 1971 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 1972 | target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring), |
| 1973 | GFP_KERNEL); |
| 1974 | if (!target->rx_ring) |
| 1975 | goto err_no_ring; |
| 1976 | target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring), |
| 1977 | GFP_KERNEL); |
| 1978 | if (!target->tx_ring) |
| 1979 | goto err_no_ring; |
| 1980 | |
| 1981 | for (i = 0; i < target->queue_size; ++i) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1982 | target->rx_ring[i] = srp_alloc_iu(target->srp_host, |
| 1983 | target->max_ti_iu_len, |
| 1984 | GFP_KERNEL, DMA_FROM_DEVICE); |
| 1985 | if (!target->rx_ring[i]) |
| 1986 | goto err; |
| 1987 | } |
| 1988 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 1989 | for (i = 0; i < target->queue_size; ++i) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1990 | target->tx_ring[i] = srp_alloc_iu(target->srp_host, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 1991 | target->max_iu_len, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1992 | GFP_KERNEL, DMA_TO_DEVICE); |
| 1993 | if (!target->tx_ring[i]) |
| 1994 | goto err; |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 1995 | |
| 1996 | list_add(&target->tx_ring[i]->list, &target->free_tx); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | return 0; |
| 2000 | |
| 2001 | err: |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2002 | for (i = 0; i < target->queue_size; ++i) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2003 | srp_free_iu(target->srp_host, target->rx_ring[i]); |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2004 | srp_free_iu(target->srp_host, target->tx_ring[i]); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2005 | } |
| 2006 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2007 | |
| 2008 | err_no_ring: |
| 2009 | kfree(target->tx_ring); |
| 2010 | target->tx_ring = NULL; |
| 2011 | kfree(target->rx_ring); |
| 2012 | target->rx_ring = NULL; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2013 | |
| 2014 | return -ENOMEM; |
| 2015 | } |
| 2016 | |
Bart Van Assche | c9b03c1 | 2011-09-03 09:34:48 +0200 | [diff] [blame] | 2017 | static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask) |
| 2018 | { |
| 2019 | uint64_t T_tr_ns, max_compl_time_ms; |
| 2020 | uint32_t rq_tmo_jiffies; |
| 2021 | |
| 2022 | /* |
| 2023 | * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair, |
| 2024 | * table 91), both the QP timeout and the retry count have to be set |
| 2025 | * for RC QP's during the RTR to RTS transition. |
| 2026 | */ |
| 2027 | WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) != |
| 2028 | (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)); |
| 2029 | |
| 2030 | /* |
| 2031 | * Set target->rq_tmo_jiffies to one second more than the largest time |
| 2032 | * it can take before an error completion is generated. See also |
| 2033 | * C9-140..142 in the IBTA spec for more information about how to |
| 2034 | * convert the QP Local ACK Timeout value to nanoseconds. |
| 2035 | */ |
| 2036 | T_tr_ns = 4096 * (1ULL << qp_attr->timeout); |
| 2037 | max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns; |
| 2038 | do_div(max_compl_time_ms, NSEC_PER_MSEC); |
| 2039 | rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000); |
| 2040 | |
| 2041 | return rq_tmo_jiffies; |
| 2042 | } |
| 2043 | |
David Dillow | 961e0be | 2011-01-14 17:32:07 -0500 | [diff] [blame] | 2044 | static void srp_cm_rep_handler(struct ib_cm_id *cm_id, |
| 2045 | struct srp_login_rsp *lrsp, |
| 2046 | struct srp_target_port *target) |
| 2047 | { |
| 2048 | struct ib_qp_attr *qp_attr = NULL; |
| 2049 | int attr_mask = 0; |
| 2050 | int ret; |
| 2051 | int i; |
| 2052 | |
| 2053 | if (lrsp->opcode == SRP_LOGIN_RSP) { |
| 2054 | target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len); |
| 2055 | target->req_lim = be32_to_cpu(lrsp->req_lim_delta); |
| 2056 | |
| 2057 | /* |
| 2058 | * Reserve credits for task management so we don't |
| 2059 | * bounce requests back to the SCSI mid-layer. |
| 2060 | */ |
| 2061 | target->scsi_host->can_queue |
| 2062 | = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE, |
| 2063 | target->scsi_host->can_queue); |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2064 | target->scsi_host->cmd_per_lun |
| 2065 | = min_t(int, target->scsi_host->can_queue, |
| 2066 | target->scsi_host->cmd_per_lun); |
David Dillow | 961e0be | 2011-01-14 17:32:07 -0500 | [diff] [blame] | 2067 | } else { |
| 2068 | shost_printk(KERN_WARNING, target->scsi_host, |
| 2069 | PFX "Unhandled RSP opcode %#x\n", lrsp->opcode); |
| 2070 | ret = -ECONNRESET; |
| 2071 | goto error; |
| 2072 | } |
| 2073 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2074 | if (!target->rx_ring) { |
David Dillow | 961e0be | 2011-01-14 17:32:07 -0500 | [diff] [blame] | 2075 | ret = srp_alloc_iu_bufs(target); |
| 2076 | if (ret) |
| 2077 | goto error; |
| 2078 | } |
| 2079 | |
| 2080 | ret = -ENOMEM; |
| 2081 | qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL); |
| 2082 | if (!qp_attr) |
| 2083 | goto error; |
| 2084 | |
| 2085 | qp_attr->qp_state = IB_QPS_RTR; |
| 2086 | ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); |
| 2087 | if (ret) |
| 2088 | goto error_free; |
| 2089 | |
| 2090 | ret = ib_modify_qp(target->qp, qp_attr, attr_mask); |
| 2091 | if (ret) |
| 2092 | goto error_free; |
| 2093 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2094 | for (i = 0; i < target->queue_size; i++) { |
David Dillow | 961e0be | 2011-01-14 17:32:07 -0500 | [diff] [blame] | 2095 | struct srp_iu *iu = target->rx_ring[i]; |
| 2096 | ret = srp_post_recv(target, iu); |
| 2097 | if (ret) |
| 2098 | goto error_free; |
| 2099 | } |
| 2100 | |
| 2101 | qp_attr->qp_state = IB_QPS_RTS; |
| 2102 | ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); |
| 2103 | if (ret) |
| 2104 | goto error_free; |
| 2105 | |
Bart Van Assche | c9b03c1 | 2011-09-03 09:34:48 +0200 | [diff] [blame] | 2106 | target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask); |
| 2107 | |
David Dillow | 961e0be | 2011-01-14 17:32:07 -0500 | [diff] [blame] | 2108 | ret = ib_modify_qp(target->qp, qp_attr, attr_mask); |
| 2109 | if (ret) |
| 2110 | goto error_free; |
| 2111 | |
| 2112 | ret = ib_send_cm_rtu(cm_id, NULL, 0); |
| 2113 | |
| 2114 | error_free: |
| 2115 | kfree(qp_attr); |
| 2116 | |
| 2117 | error: |
| 2118 | target->status = ret; |
| 2119 | } |
| 2120 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2121 | static void srp_cm_rej_handler(struct ib_cm_id *cm_id, |
| 2122 | struct ib_cm_event *event, |
| 2123 | struct srp_target_port *target) |
| 2124 | { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2125 | struct Scsi_Host *shost = target->scsi_host; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2126 | struct ib_class_port_info *cpi; |
| 2127 | int opcode; |
| 2128 | |
| 2129 | switch (event->param.rej_rcvd.reason) { |
| 2130 | case IB_CM_REJ_PORT_CM_REDIRECT: |
| 2131 | cpi = event->param.rej_rcvd.ari; |
| 2132 | target->path.dlid = cpi->redirect_lid; |
| 2133 | target->path.pkey = cpi->redirect_pkey; |
| 2134 | cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff; |
| 2135 | memcpy(target->path.dgid.raw, cpi->redirect_gid, 16); |
| 2136 | |
| 2137 | target->status = target->path.dlid ? |
| 2138 | SRP_DLID_REDIRECT : SRP_PORT_REDIRECT; |
| 2139 | break; |
| 2140 | |
| 2141 | case IB_CM_REJ_PORT_REDIRECT: |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 2142 | if (srp_target_is_topspin(target)) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2143 | /* |
| 2144 | * Topspin/Cisco SRP gateways incorrectly send |
| 2145 | * reject reason code 25 when they mean 24 |
| 2146 | * (port redirect). |
| 2147 | */ |
| 2148 | memcpy(target->path.dgid.raw, |
| 2149 | event->param.rej_rcvd.ari, 16); |
| 2150 | |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2151 | shost_printk(KERN_DEBUG, shost, |
| 2152 | PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n", |
| 2153 | (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix), |
| 2154 | (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id)); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2155 | |
| 2156 | target->status = SRP_PORT_REDIRECT; |
| 2157 | } else { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2158 | shost_printk(KERN_WARNING, shost, |
| 2159 | " REJ reason: IB_CM_REJ_PORT_REDIRECT\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2160 | target->status = -ECONNRESET; |
| 2161 | } |
| 2162 | break; |
| 2163 | |
| 2164 | case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2165 | shost_printk(KERN_WARNING, shost, |
| 2166 | " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2167 | target->status = -ECONNRESET; |
| 2168 | break; |
| 2169 | |
| 2170 | case IB_CM_REJ_CONSUMER_DEFINED: |
| 2171 | opcode = *(u8 *) event->private_data; |
| 2172 | if (opcode == SRP_LOGIN_REJ) { |
| 2173 | struct srp_login_rej *rej = event->private_data; |
| 2174 | u32 reason = be32_to_cpu(rej->reason); |
| 2175 | |
| 2176 | if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2177 | shost_printk(KERN_WARNING, shost, |
| 2178 | PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2179 | else |
Bart Van Assche | e7ffde0 | 2014-03-14 13:52:21 +0100 | [diff] [blame] | 2180 | shost_printk(KERN_WARNING, shost, PFX |
| 2181 | "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n", |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 2182 | target->sgid.raw, |
| 2183 | target->orig_dgid.raw, reason); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2184 | } else |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2185 | shost_printk(KERN_WARNING, shost, |
| 2186 | " REJ reason: IB_CM_REJ_CONSUMER_DEFINED," |
| 2187 | " opcode 0x%02x\n", opcode); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2188 | target->status = -ECONNRESET; |
| 2189 | break; |
| 2190 | |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 2191 | case IB_CM_REJ_STALE_CONN: |
| 2192 | shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n"); |
| 2193 | target->status = SRP_STALE_CONN; |
| 2194 | break; |
| 2195 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2196 | default: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2197 | shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n", |
| 2198 | event->param.rej_rcvd.reason); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2199 | target->status = -ECONNRESET; |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) |
| 2204 | { |
| 2205 | struct srp_target_port *target = cm_id->context; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2206 | int comp = 0; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2207 | |
| 2208 | switch (event->event) { |
| 2209 | case IB_CM_REQ_ERROR: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2210 | shost_printk(KERN_DEBUG, target->scsi_host, |
| 2211 | PFX "Sending CM REQ failed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2212 | comp = 1; |
| 2213 | target->status = -ECONNRESET; |
| 2214 | break; |
| 2215 | |
| 2216 | case IB_CM_REP_RECEIVED: |
| 2217 | comp = 1; |
David Dillow | 961e0be | 2011-01-14 17:32:07 -0500 | [diff] [blame] | 2218 | srp_cm_rep_handler(cm_id, event->private_data, target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2219 | break; |
| 2220 | |
| 2221 | case IB_CM_REJ_RECEIVED: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2222 | shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2223 | comp = 1; |
| 2224 | |
| 2225 | srp_cm_rej_handler(cm_id, event, target); |
| 2226 | break; |
| 2227 | |
Ishai Rabinovitz | b7ac4ab | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 2228 | case IB_CM_DREQ_RECEIVED: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2229 | shost_printk(KERN_WARNING, target->scsi_host, |
| 2230 | PFX "DREQ received - connection closed\n"); |
Bart Van Assche | 294c875 | 2011-12-25 12:18:12 +0000 | [diff] [blame] | 2231 | srp_change_conn_state(target, false); |
Ishai Rabinovitz | b7ac4ab | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 2232 | if (ib_send_cm_drep(cm_id, NULL, 0)) |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2233 | shost_printk(KERN_ERR, target->scsi_host, |
| 2234 | PFX "Sending CM DREP failed\n"); |
Bart Van Assche | c1120f8 | 2013-10-26 14:35:08 +0200 | [diff] [blame] | 2235 | queue_work(system_long_wq, &target->tl_err_work); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2236 | break; |
| 2237 | |
| 2238 | case IB_CM_TIMEWAIT_EXIT: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2239 | shost_printk(KERN_ERR, target->scsi_host, |
| 2240 | PFX "connection closed\n"); |
Bart Van Assche | ac72d76 | 2014-03-14 13:53:40 +0100 | [diff] [blame] | 2241 | comp = 1; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2242 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2243 | target->status = 0; |
| 2244 | break; |
| 2245 | |
Ishai Rabinovitz | b7ac4ab | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 2246 | case IB_CM_MRA_RECEIVED: |
| 2247 | case IB_CM_DREQ_ERROR: |
| 2248 | case IB_CM_DREP_RECEIVED: |
| 2249 | break; |
| 2250 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2251 | default: |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2252 | shost_printk(KERN_WARNING, target->scsi_host, |
| 2253 | PFX "Unhandled CM event %d\n", event->event); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2254 | break; |
| 2255 | } |
| 2256 | |
| 2257 | if (comp) |
| 2258 | complete(&target->done); |
| 2259 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2260 | return 0; |
| 2261 | } |
| 2262 | |
Jack Wang | 71444b9 | 2013-11-07 11:37:37 +0100 | [diff] [blame] | 2263 | /** |
Jack Wang | 71444b9 | 2013-11-07 11:37:37 +0100 | [diff] [blame] | 2264 | * srp_change_queue_depth - setting device queue depth |
| 2265 | * @sdev: scsi device struct |
| 2266 | * @qdepth: requested queue depth |
| 2267 | * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP |
| 2268 | * (see include/scsi/scsi_host.h for definition) |
| 2269 | * |
| 2270 | * Returns queue depth. |
| 2271 | */ |
| 2272 | static int |
| 2273 | srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason) |
| 2274 | { |
| 2275 | struct Scsi_Host *shost = sdev->host; |
| 2276 | int max_depth; |
| 2277 | if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) { |
| 2278 | max_depth = shost->can_queue; |
| 2279 | if (!sdev->tagged_supported) |
| 2280 | max_depth = 1; |
| 2281 | if (qdepth > max_depth) |
| 2282 | qdepth = max_depth; |
Christoph Hellwig | c8b09f6 | 2014-11-03 20:15:14 +0100 | [diff] [blame] | 2283 | scsi_adjust_queue_depth(sdev, qdepth); |
Jack Wang | 71444b9 | 2013-11-07 11:37:37 +0100 | [diff] [blame] | 2284 | } else if (reason == SCSI_QDEPTH_QFULL) |
| 2285 | scsi_track_queue_full(sdev, qdepth); |
| 2286 | else |
| 2287 | return -EOPNOTSUPP; |
| 2288 | |
| 2289 | return sdev->queue_depth; |
| 2290 | } |
| 2291 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2292 | static int srp_send_tsk_mgmt(struct srp_target_port *target, |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 2293 | u64 req_tag, unsigned int lun, u8 func) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2294 | { |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 2295 | struct srp_rport *rport = target->rport; |
David Dillow | 19081f3 | 2010-10-18 08:54:49 -0400 | [diff] [blame] | 2296 | struct ib_device *dev = target->srp_host->srp_dev->dev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2297 | struct srp_iu *iu; |
| 2298 | struct srp_tsk_mgmt *tsk_mgmt; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2299 | |
Bart Van Assche | 3780d1f | 2013-02-21 17:18:00 +0000 | [diff] [blame] | 2300 | if (!target->connected || target->qp_in_error) |
| 2301 | return -1; |
| 2302 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 2303 | init_completion(&target->tsk_mgmt_done); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2304 | |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 2305 | /* |
| 2306 | * Lock the rport mutex to avoid that srp_create_target_ib() is |
| 2307 | * invoked while a task management function is being sent. |
| 2308 | */ |
| 2309 | mutex_lock(&rport->mutex); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 2310 | spin_lock_irq(&target->lock); |
David Dillow | bb12588 | 2010-10-08 14:40:47 -0400 | [diff] [blame] | 2311 | iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT); |
Bart Van Assche | e968467 | 2010-11-26 15:08:38 -0500 | [diff] [blame] | 2312 | spin_unlock_irq(&target->lock); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 2313 | |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 2314 | if (!iu) { |
| 2315 | mutex_unlock(&rport->mutex); |
| 2316 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 2317 | return -1; |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 2318 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2319 | |
David Dillow | 19081f3 | 2010-10-18 08:54:49 -0400 | [diff] [blame] | 2320 | ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt, |
| 2321 | DMA_TO_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2322 | tsk_mgmt = iu->buf; |
| 2323 | memset(tsk_mgmt, 0, sizeof *tsk_mgmt); |
| 2324 | |
| 2325 | tsk_mgmt->opcode = SRP_TSK_MGMT; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 2326 | tsk_mgmt->lun = cpu_to_be64((u64) lun << 48); |
| 2327 | tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2328 | tsk_mgmt->tsk_mgmt_func = func; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 2329 | tsk_mgmt->task_tag = req_tag; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2330 | |
David Dillow | 19081f3 | 2010-10-18 08:54:49 -0400 | [diff] [blame] | 2331 | ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt, |
| 2332 | DMA_TO_DEVICE); |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 2333 | if (srp_post_send(target, iu, sizeof *tsk_mgmt)) { |
| 2334 | srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT); |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 2335 | mutex_unlock(&rport->mutex); |
| 2336 | |
Bart Van Assche | 76c75b2 | 2010-11-26 14:37:47 -0500 | [diff] [blame] | 2337 | return -1; |
| 2338 | } |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 2339 | mutex_unlock(&rport->mutex); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2340 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 2341 | if (!wait_for_completion_timeout(&target->tsk_mgmt_done, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2342 | msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS))) |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2343 | return -1; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2344 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2345 | return 0; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2348 | static int srp_abort(struct scsi_cmnd *scmnd) |
| 2349 | { |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2350 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 2351 | struct srp_request *req = (struct srp_request *) scmnd->host_scribble; |
Bart Van Assche | 086f44f | 2013-06-12 15:23:04 +0200 | [diff] [blame] | 2352 | int ret; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2353 | |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2354 | shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2355 | |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 2356 | if (!req || !srp_claim_req(target, req, NULL, scmnd)) |
Bart Van Assche | 99b6697 | 2013-10-10 13:52:33 +0200 | [diff] [blame] | 2357 | return SUCCESS; |
Bart Van Assche | 086f44f | 2013-06-12 15:23:04 +0200 | [diff] [blame] | 2358 | if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun, |
Bart Van Assche | 80d5e8a | 2013-07-10 17:36:35 +0200 | [diff] [blame] | 2359 | SRP_TSK_ABORT_TASK) == 0) |
Bart Van Assche | 086f44f | 2013-06-12 15:23:04 +0200 | [diff] [blame] | 2360 | ret = SUCCESS; |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 2361 | else if (target->rport->state == SRP_RPORT_LOST) |
Bart Van Assche | 99e1c13 | 2013-06-28 14:49:58 +0200 | [diff] [blame] | 2362 | ret = FAST_IO_FAIL; |
Bart Van Assche | 086f44f | 2013-06-12 15:23:04 +0200 | [diff] [blame] | 2363 | else |
| 2364 | ret = FAILED; |
Bart Van Assche | 2203299 | 2012-08-14 13:18:53 +0000 | [diff] [blame] | 2365 | srp_free_req(target, req, scmnd, 0); |
| 2366 | scmnd->result = DID_ABORT << 16; |
Bart Van Assche | d853667 | 2012-08-24 10:29:11 +0000 | [diff] [blame] | 2367 | scmnd->scsi_done(scmnd); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2368 | |
Bart Van Assche | 086f44f | 2013-06-12 15:23:04 +0200 | [diff] [blame] | 2369 | return ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | static int srp_reset_device(struct scsi_cmnd *scmnd) |
| 2373 | { |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2374 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 2375 | int i; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2376 | |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2377 | shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2378 | |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 2379 | if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun, |
| 2380 | SRP_TSK_LUN_RESET)) |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2381 | return FAILED; |
David Dillow | f8b6e31 | 2010-11-26 13:02:21 -0500 | [diff] [blame] | 2382 | if (target->tsk_mgmt_status) |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2383 | return FAILED; |
| 2384 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2385 | for (i = 0; i < target->req_ring_size; ++i) { |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 2386 | struct srp_request *req = &target->req_ring[i]; |
Bart Van Assche | b3fe628 | 2014-03-14 13:54:11 +0100 | [diff] [blame] | 2387 | srp_finish_req(target, req, scmnd->device, DID_RESET << 16); |
Bart Van Assche | 536ae14 | 2010-11-26 13:58:27 -0500 | [diff] [blame] | 2388 | } |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2389 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 2390 | return SUCCESS; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | static int srp_reset_host(struct scsi_cmnd *scmnd) |
| 2394 | { |
| 2395 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2396 | |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 2397 | 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] | 2398 | |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 2399 | return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2400 | } |
| 2401 | |
Bart Van Assche | c9b03c1 | 2011-09-03 09:34:48 +0200 | [diff] [blame] | 2402 | static int srp_slave_configure(struct scsi_device *sdev) |
| 2403 | { |
| 2404 | struct Scsi_Host *shost = sdev->host; |
| 2405 | struct srp_target_port *target = host_to_target(shost); |
| 2406 | struct request_queue *q = sdev->request_queue; |
| 2407 | unsigned long timeout; |
| 2408 | |
| 2409 | if (sdev->type == TYPE_DISK) { |
| 2410 | timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies); |
| 2411 | blk_queue_rq_timeout(q, timeout); |
| 2412 | } |
| 2413 | |
| 2414 | return 0; |
| 2415 | } |
| 2416 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2417 | static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr, |
| 2418 | char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2419 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2420 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2421 | |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2422 | return sprintf(buf, "0x%016llx\n", |
| 2423 | (unsigned long long) be64_to_cpu(target->id_ext)); |
| 2424 | } |
| 2425 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2426 | static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr, |
| 2427 | char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2428 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2429 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2430 | |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2431 | return sprintf(buf, "0x%016llx\n", |
| 2432 | (unsigned long long) be64_to_cpu(target->ioc_guid)); |
| 2433 | } |
| 2434 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2435 | static ssize_t show_service_id(struct device *dev, |
| 2436 | struct device_attribute *attr, char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2437 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2438 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2439 | |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2440 | return sprintf(buf, "0x%016llx\n", |
| 2441 | (unsigned long long) be64_to_cpu(target->service_id)); |
| 2442 | } |
| 2443 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2444 | static ssize_t show_pkey(struct device *dev, struct device_attribute *attr, |
| 2445 | char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2446 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2447 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2448 | |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 2449 | return sprintf(buf, "0x%04x\n", be16_to_cpu(target->pkey)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2450 | } |
| 2451 | |
Bart Van Assche | 848b308 | 2013-10-26 14:38:12 +0200 | [diff] [blame] | 2452 | static ssize_t show_sgid(struct device *dev, struct device_attribute *attr, |
| 2453 | char *buf) |
| 2454 | { |
| 2455 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
| 2456 | |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 2457 | return sprintf(buf, "%pI6\n", target->sgid.raw); |
Bart Van Assche | 848b308 | 2013-10-26 14:38:12 +0200 | [diff] [blame] | 2458 | } |
| 2459 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2460 | static ssize_t show_dgid(struct device *dev, struct device_attribute *attr, |
| 2461 | char *buf) |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2462 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2463 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2464 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 2465 | return sprintf(buf, "%pI6\n", target->path.dgid.raw); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2466 | } |
| 2467 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2468 | static ssize_t show_orig_dgid(struct device *dev, |
| 2469 | struct device_attribute *attr, char *buf) |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 2470 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2471 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 2472 | |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 2473 | return sprintf(buf, "%pI6\n", target->orig_dgid.raw); |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 2474 | } |
| 2475 | |
Bart Van Assche | 89de748 | 2010-08-03 14:08:45 +0000 | [diff] [blame] | 2476 | static ssize_t show_req_lim(struct device *dev, |
| 2477 | struct device_attribute *attr, char *buf) |
| 2478 | { |
| 2479 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
| 2480 | |
Bart Van Assche | 89de748 | 2010-08-03 14:08:45 +0000 | [diff] [blame] | 2481 | return sprintf(buf, "%d\n", target->req_lim); |
| 2482 | } |
| 2483 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2484 | static ssize_t show_zero_req_lim(struct device *dev, |
| 2485 | struct device_attribute *attr, char *buf) |
Roland Dreier | 6bfa24f | 2006-06-17 20:37:33 -0700 | [diff] [blame] | 2486 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2487 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Roland Dreier | 6bfa24f | 2006-06-17 20:37:33 -0700 | [diff] [blame] | 2488 | |
Roland Dreier | 6bfa24f | 2006-06-17 20:37:33 -0700 | [diff] [blame] | 2489 | return sprintf(buf, "%d\n", target->zero_req_lim); |
| 2490 | } |
| 2491 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2492 | static ssize_t show_local_ib_port(struct device *dev, |
| 2493 | struct device_attribute *attr, char *buf) |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 2494 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2495 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 2496 | |
| 2497 | return sprintf(buf, "%d\n", target->srp_host->port); |
| 2498 | } |
| 2499 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2500 | static ssize_t show_local_ib_device(struct device *dev, |
| 2501 | struct device_attribute *attr, char *buf) |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 2502 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2503 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 2504 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 2505 | return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name); |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 2506 | } |
| 2507 | |
Bart Van Assche | 4b5e5f4 | 2013-06-28 14:57:42 +0200 | [diff] [blame] | 2508 | static ssize_t show_comp_vector(struct device *dev, |
| 2509 | struct device_attribute *attr, char *buf) |
| 2510 | { |
| 2511 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
| 2512 | |
| 2513 | return sprintf(buf, "%d\n", target->comp_vector); |
| 2514 | } |
| 2515 | |
Vu Pham | 7bb312e | 2013-10-26 14:31:27 +0200 | [diff] [blame] | 2516 | static ssize_t show_tl_retry_count(struct device *dev, |
| 2517 | struct device_attribute *attr, char *buf) |
| 2518 | { |
| 2519 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
| 2520 | |
| 2521 | return sprintf(buf, "%d\n", target->tl_retry_count); |
| 2522 | } |
| 2523 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2524 | static ssize_t show_cmd_sg_entries(struct device *dev, |
| 2525 | struct device_attribute *attr, char *buf) |
| 2526 | { |
| 2527 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
| 2528 | |
| 2529 | return sprintf(buf, "%u\n", target->cmd_sg_cnt); |
| 2530 | } |
| 2531 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2532 | static ssize_t show_allow_ext_sg(struct device *dev, |
| 2533 | struct device_attribute *attr, char *buf) |
| 2534 | { |
| 2535 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
| 2536 | |
| 2537 | return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false"); |
| 2538 | } |
| 2539 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2540 | static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL); |
| 2541 | static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL); |
| 2542 | static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL); |
| 2543 | static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); |
Bart Van Assche | 848b308 | 2013-10-26 14:38:12 +0200 | [diff] [blame] | 2544 | static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2545 | static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL); |
| 2546 | static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL); |
Bart Van Assche | 89de748 | 2010-08-03 14:08:45 +0000 | [diff] [blame] | 2547 | static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2548 | static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL); |
| 2549 | static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL); |
| 2550 | static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL); |
Bart Van Assche | 4b5e5f4 | 2013-06-28 14:57:42 +0200 | [diff] [blame] | 2551 | static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL); |
Vu Pham | 7bb312e | 2013-10-26 14:31:27 +0200 | [diff] [blame] | 2552 | static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL); |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2553 | 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] | 2554 | 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] | 2555 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2556 | static struct device_attribute *srp_host_attrs[] = { |
| 2557 | &dev_attr_id_ext, |
| 2558 | &dev_attr_ioc_guid, |
| 2559 | &dev_attr_service_id, |
| 2560 | &dev_attr_pkey, |
Bart Van Assche | 848b308 | 2013-10-26 14:38:12 +0200 | [diff] [blame] | 2561 | &dev_attr_sgid, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2562 | &dev_attr_dgid, |
| 2563 | &dev_attr_orig_dgid, |
Bart Van Assche | 89de748 | 2010-08-03 14:08:45 +0000 | [diff] [blame] | 2564 | &dev_attr_req_lim, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2565 | &dev_attr_zero_req_lim, |
| 2566 | &dev_attr_local_ib_port, |
| 2567 | &dev_attr_local_ib_device, |
Bart Van Assche | 4b5e5f4 | 2013-06-28 14:57:42 +0200 | [diff] [blame] | 2568 | &dev_attr_comp_vector, |
Vu Pham | 7bb312e | 2013-10-26 14:31:27 +0200 | [diff] [blame] | 2569 | &dev_attr_tl_retry_count, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2570 | &dev_attr_cmd_sg_entries, |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2571 | &dev_attr_allow_ext_sg, |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2572 | NULL |
| 2573 | }; |
| 2574 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2575 | static struct scsi_host_template srp_template = { |
| 2576 | .module = THIS_MODULE, |
Roland Dreier | b7f008f | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 2577 | .name = "InfiniBand SRP initiator", |
| 2578 | .proc_name = DRV_NAME, |
Bart Van Assche | c9b03c1 | 2011-09-03 09:34:48 +0200 | [diff] [blame] | 2579 | .slave_configure = srp_slave_configure, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2580 | .info = srp_target_info, |
| 2581 | .queuecommand = srp_queuecommand, |
Jack Wang | 71444b9 | 2013-11-07 11:37:37 +0100 | [diff] [blame] | 2582 | .change_queue_depth = srp_change_queue_depth, |
Christoph Hellwig | a62182f | 2014-10-02 14:39:55 +0200 | [diff] [blame] | 2583 | .change_queue_type = scsi_change_queue_type, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2584 | .eh_abort_handler = srp_abort, |
| 2585 | .eh_device_reset_handler = srp_reset_device, |
| 2586 | .eh_host_reset_handler = srp_reset_host, |
Bart Van Assche | 2742c1d | 2013-06-12 15:24:25 +0200 | [diff] [blame] | 2587 | .skip_settle_delay = true, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2588 | .sg_tablesize = SRP_DEF_SG_TABLESIZE, |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2589 | .can_queue = SRP_DEFAULT_CMD_SQ_SIZE, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2590 | .this_id = -1, |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2591 | .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE, |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 2592 | .use_clustering = ENABLE_CLUSTERING, |
| 2593 | .shost_attrs = srp_host_attrs |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2594 | }; |
| 2595 | |
Bart Van Assche | 34aa654 | 2014-10-30 14:47:22 +0100 | [diff] [blame] | 2596 | static int srp_sdev_count(struct Scsi_Host *host) |
| 2597 | { |
| 2598 | struct scsi_device *sdev; |
| 2599 | int c = 0; |
| 2600 | |
| 2601 | shost_for_each_device(sdev, host) |
| 2602 | c++; |
| 2603 | |
| 2604 | return c; |
| 2605 | } |
| 2606 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2607 | static int srp_add_target(struct srp_host *host, struct srp_target_port *target) |
| 2608 | { |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 2609 | struct srp_rport_identifiers ids; |
| 2610 | struct srp_rport *rport; |
| 2611 | |
Bart Van Assche | 34aa654 | 2014-10-30 14:47:22 +0100 | [diff] [blame] | 2612 | target->state = SRP_TARGET_SCANNING; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2613 | sprintf(target->target_name, "SRP.T10:%016llX", |
| 2614 | (unsigned long long) be64_to_cpu(target->id_ext)); |
| 2615 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 2616 | 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] | 2617 | return -ENODEV; |
| 2618 | |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 2619 | memcpy(ids.port_id, &target->id_ext, 8); |
| 2620 | memcpy(ids.port_id + 8, &target->ioc_guid, 8); |
FUJITA Tomonori | aebd5e4 | 2007-07-11 15:08:15 +0900 | [diff] [blame] | 2621 | ids.roles = SRP_RPORT_ROLE_TARGET; |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 2622 | rport = srp_rport_add(target->scsi_host, &ids); |
| 2623 | if (IS_ERR(rport)) { |
| 2624 | scsi_remove_host(target->scsi_host); |
| 2625 | return PTR_ERR(rport); |
| 2626 | } |
| 2627 | |
Bart Van Assche | dc1bdbd | 2011-09-16 20:41:13 +0200 | [diff] [blame] | 2628 | rport->lld_data = target; |
Bart Van Assche | 9dd69a6 | 2013-10-26 14:32:30 +0200 | [diff] [blame] | 2629 | target->rport = rport; |
Bart Van Assche | dc1bdbd | 2011-09-16 20:41:13 +0200 | [diff] [blame] | 2630 | |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2631 | spin_lock(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2632 | list_add_tail(&target->list, &host->target_list); |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2633 | spin_unlock(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2634 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2635 | scsi_scan_target(&target->scsi_host->shost_gendev, |
Matthew Wilcox | 1962a4a | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2636 | 0, target->scsi_id, SCAN_WILD_CARD, 0); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2637 | |
Bart Van Assche | 34aa654 | 2014-10-30 14:47:22 +0100 | [diff] [blame] | 2638 | if (!target->connected || target->qp_in_error) { |
| 2639 | shost_printk(KERN_INFO, target->scsi_host, |
| 2640 | PFX "SCSI scan failed - removing SCSI host\n"); |
| 2641 | srp_queue_remove_work(target); |
| 2642 | goto out; |
| 2643 | } |
| 2644 | |
| 2645 | pr_debug(PFX "%s: SCSI scan succeeded - detected %d LUNs\n", |
| 2646 | dev_name(&target->scsi_host->shost_gendev), |
| 2647 | srp_sdev_count(target->scsi_host)); |
| 2648 | |
| 2649 | spin_lock_irq(&target->lock); |
| 2650 | if (target->state == SRP_TARGET_SCANNING) |
| 2651 | target->state = SRP_TARGET_LIVE; |
| 2652 | spin_unlock_irq(&target->lock); |
| 2653 | |
| 2654 | out: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2655 | return 0; |
| 2656 | } |
| 2657 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2658 | static void srp_release_dev(struct device *dev) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2659 | { |
| 2660 | struct srp_host *host = |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2661 | container_of(dev, struct srp_host, dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2662 | |
| 2663 | complete(&host->released); |
| 2664 | } |
| 2665 | |
| 2666 | static struct class srp_class = { |
| 2667 | .name = "infiniband_srp", |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2668 | .dev_release = srp_release_dev |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2669 | }; |
| 2670 | |
Bart Van Assche | 96fc248 | 2013-06-28 14:51:26 +0200 | [diff] [blame] | 2671 | /** |
| 2672 | * srp_conn_unique() - check whether the connection to a target is unique |
Bart Van Assche | af24663 | 2014-05-20 15:04:21 +0200 | [diff] [blame] | 2673 | * @host: SRP host. |
| 2674 | * @target: SRP target port. |
Bart Van Assche | 96fc248 | 2013-06-28 14:51:26 +0200 | [diff] [blame] | 2675 | */ |
| 2676 | static bool srp_conn_unique(struct srp_host *host, |
| 2677 | struct srp_target_port *target) |
| 2678 | { |
| 2679 | struct srp_target_port *t; |
| 2680 | bool ret = false; |
| 2681 | |
| 2682 | if (target->state == SRP_TARGET_REMOVED) |
| 2683 | goto out; |
| 2684 | |
| 2685 | ret = true; |
| 2686 | |
| 2687 | spin_lock(&host->target_lock); |
| 2688 | list_for_each_entry(t, &host->target_list, list) { |
| 2689 | if (t != target && |
| 2690 | target->id_ext == t->id_ext && |
| 2691 | target->ioc_guid == t->ioc_guid && |
| 2692 | target->initiator_ext == t->initiator_ext) { |
| 2693 | ret = false; |
| 2694 | break; |
| 2695 | } |
| 2696 | } |
| 2697 | spin_unlock(&host->target_lock); |
| 2698 | |
| 2699 | out: |
| 2700 | return ret; |
| 2701 | } |
| 2702 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2703 | /* |
| 2704 | * Target ports are added by writing |
| 2705 | * |
| 2706 | * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>, |
| 2707 | * pkey=<P_Key>,service_id=<service ID> |
| 2708 | * |
| 2709 | * to the add_target sysfs attribute. |
| 2710 | */ |
| 2711 | enum { |
| 2712 | SRP_OPT_ERR = 0, |
| 2713 | SRP_OPT_ID_EXT = 1 << 0, |
| 2714 | SRP_OPT_IOC_GUID = 1 << 1, |
| 2715 | SRP_OPT_DGID = 1 << 2, |
| 2716 | SRP_OPT_PKEY = 1 << 3, |
| 2717 | SRP_OPT_SERVICE_ID = 1 << 4, |
| 2718 | SRP_OPT_MAX_SECT = 1 << 5, |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2719 | SRP_OPT_MAX_CMD_PER_LUN = 1 << 6, |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 2720 | SRP_OPT_IO_CLASS = 1 << 7, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 2721 | SRP_OPT_INITIATOR_EXT = 1 << 8, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2722 | SRP_OPT_CMD_SG_ENTRIES = 1 << 9, |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2723 | SRP_OPT_ALLOW_EXT_SG = 1 << 10, |
| 2724 | SRP_OPT_SG_TABLESIZE = 1 << 11, |
Bart Van Assche | 4b5e5f4 | 2013-06-28 14:57:42 +0200 | [diff] [blame] | 2725 | SRP_OPT_COMP_VECTOR = 1 << 12, |
Vu Pham | 7bb312e | 2013-10-26 14:31:27 +0200 | [diff] [blame] | 2726 | SRP_OPT_TL_RETRY_COUNT = 1 << 13, |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2727 | SRP_OPT_QUEUE_SIZE = 1 << 14, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2728 | SRP_OPT_ALL = (SRP_OPT_ID_EXT | |
| 2729 | SRP_OPT_IOC_GUID | |
| 2730 | SRP_OPT_DGID | |
| 2731 | SRP_OPT_PKEY | |
| 2732 | SRP_OPT_SERVICE_ID), |
| 2733 | }; |
| 2734 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 2735 | static const match_table_t srp_opt_tokens = { |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2736 | { SRP_OPT_ID_EXT, "id_ext=%s" }, |
| 2737 | { SRP_OPT_IOC_GUID, "ioc_guid=%s" }, |
| 2738 | { SRP_OPT_DGID, "dgid=%s" }, |
| 2739 | { SRP_OPT_PKEY, "pkey=%x" }, |
| 2740 | { SRP_OPT_SERVICE_ID, "service_id=%s" }, |
| 2741 | { SRP_OPT_MAX_SECT, "max_sect=%d" }, |
| 2742 | { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" }, |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 2743 | { SRP_OPT_IO_CLASS, "io_class=%x" }, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 2744 | { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" }, |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2745 | { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" }, |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2746 | { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" }, |
| 2747 | { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" }, |
Bart Van Assche | 4b5e5f4 | 2013-06-28 14:57:42 +0200 | [diff] [blame] | 2748 | { SRP_OPT_COMP_VECTOR, "comp_vector=%u" }, |
Vu Pham | 7bb312e | 2013-10-26 14:31:27 +0200 | [diff] [blame] | 2749 | { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" }, |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2750 | { SRP_OPT_QUEUE_SIZE, "queue_size=%d" }, |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2751 | { SRP_OPT_ERR, NULL } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2752 | }; |
| 2753 | |
| 2754 | static int srp_parse_options(const char *buf, struct srp_target_port *target) |
| 2755 | { |
| 2756 | char *options, *sep_opt; |
| 2757 | char *p; |
| 2758 | char dgid[3]; |
| 2759 | substring_t args[MAX_OPT_ARGS]; |
| 2760 | int opt_mask = 0; |
| 2761 | int token; |
| 2762 | int ret = -EINVAL; |
| 2763 | int i; |
| 2764 | |
| 2765 | options = kstrdup(buf, GFP_KERNEL); |
| 2766 | if (!options) |
| 2767 | return -ENOMEM; |
| 2768 | |
| 2769 | sep_opt = options; |
| 2770 | while ((p = strsep(&sep_opt, ",")) != NULL) { |
| 2771 | if (!*p) |
| 2772 | continue; |
| 2773 | |
| 2774 | token = match_token(p, srp_opt_tokens, args); |
| 2775 | opt_mask |= token; |
| 2776 | |
| 2777 | switch (token) { |
| 2778 | case SRP_OPT_ID_EXT: |
| 2779 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 2780 | if (!p) { |
| 2781 | ret = -ENOMEM; |
| 2782 | goto out; |
| 2783 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2784 | target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 2785 | kfree(p); |
| 2786 | break; |
| 2787 | |
| 2788 | case SRP_OPT_IOC_GUID: |
| 2789 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 2790 | if (!p) { |
| 2791 | ret = -ENOMEM; |
| 2792 | goto out; |
| 2793 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2794 | target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 2795 | kfree(p); |
| 2796 | break; |
| 2797 | |
| 2798 | case SRP_OPT_DGID: |
| 2799 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 2800 | if (!p) { |
| 2801 | ret = -ENOMEM; |
| 2802 | goto out; |
| 2803 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2804 | if (strlen(p) != 32) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2805 | pr_warn("bad dest GID parameter '%s'\n", p); |
Roland Dreier | ce1823f | 2006-04-03 09:31:04 -0700 | [diff] [blame] | 2806 | kfree(p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2807 | goto out; |
| 2808 | } |
| 2809 | |
| 2810 | for (i = 0; i < 16; ++i) { |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 2811 | strlcpy(dgid, p + i * 2, sizeof(dgid)); |
| 2812 | if (sscanf(dgid, "%hhx", |
| 2813 | &target->orig_dgid.raw[i]) < 1) { |
| 2814 | ret = -EINVAL; |
| 2815 | kfree(p); |
| 2816 | goto out; |
| 2817 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2818 | } |
Roland Dreier | bf17c1c | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 2819 | kfree(p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2820 | break; |
| 2821 | |
| 2822 | case SRP_OPT_PKEY: |
| 2823 | if (match_hex(args, &token)) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2824 | pr_warn("bad P_Key parameter '%s'\n", p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2825 | goto out; |
| 2826 | } |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 2827 | target->pkey = cpu_to_be16(token); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2828 | break; |
| 2829 | |
| 2830 | case SRP_OPT_SERVICE_ID: |
| 2831 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 2832 | if (!p) { |
| 2833 | ret = -ENOMEM; |
| 2834 | goto out; |
| 2835 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2836 | target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 2837 | kfree(p); |
| 2838 | break; |
| 2839 | |
| 2840 | case SRP_OPT_MAX_SECT: |
| 2841 | if (match_int(args, &token)) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2842 | pr_warn("bad max sect parameter '%s'\n", p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2843 | goto out; |
| 2844 | } |
| 2845 | target->scsi_host->max_sectors = token; |
| 2846 | break; |
| 2847 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2848 | case SRP_OPT_QUEUE_SIZE: |
| 2849 | if (match_int(args, &token) || token < 1) { |
| 2850 | pr_warn("bad queue_size parameter '%s'\n", p); |
| 2851 | goto out; |
| 2852 | } |
| 2853 | target->scsi_host->can_queue = token; |
| 2854 | target->queue_size = token + SRP_RSP_SQ_SIZE + |
| 2855 | SRP_TSK_MGMT_SQ_SIZE; |
| 2856 | if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN)) |
| 2857 | target->scsi_host->cmd_per_lun = token; |
| 2858 | break; |
| 2859 | |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2860 | case SRP_OPT_MAX_CMD_PER_LUN: |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2861 | if (match_int(args, &token) || token < 1) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2862 | pr_warn("bad max cmd_per_lun parameter '%s'\n", |
| 2863 | p); |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2864 | goto out; |
| 2865 | } |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2866 | target->scsi_host->cmd_per_lun = token; |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2867 | break; |
| 2868 | |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 2869 | case SRP_OPT_IO_CLASS: |
| 2870 | if (match_hex(args, &token)) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2871 | pr_warn("bad IO class parameter '%s'\n", p); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 2872 | goto out; |
| 2873 | } |
| 2874 | if (token != SRP_REV10_IB_IO_CLASS && |
| 2875 | token != SRP_REV16A_IB_IO_CLASS) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2876 | pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n", |
| 2877 | token, SRP_REV10_IB_IO_CLASS, |
| 2878 | SRP_REV16A_IB_IO_CLASS); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 2879 | goto out; |
| 2880 | } |
| 2881 | target->io_class = token; |
| 2882 | break; |
| 2883 | |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 2884 | case SRP_OPT_INITIATOR_EXT: |
| 2885 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 2886 | if (!p) { |
| 2887 | ret = -ENOMEM; |
| 2888 | goto out; |
| 2889 | } |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 2890 | target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 2891 | kfree(p); |
| 2892 | break; |
| 2893 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2894 | case SRP_OPT_CMD_SG_ENTRIES: |
| 2895 | if (match_int(args, &token) || token < 1 || token > 255) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2896 | pr_warn("bad max cmd_sg_entries parameter '%s'\n", |
| 2897 | p); |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2898 | goto out; |
| 2899 | } |
| 2900 | target->cmd_sg_cnt = token; |
| 2901 | break; |
| 2902 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2903 | case SRP_OPT_ALLOW_EXT_SG: |
| 2904 | if (match_int(args, &token)) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2905 | pr_warn("bad allow_ext_sg parameter '%s'\n", p); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2906 | goto out; |
| 2907 | } |
| 2908 | target->allow_ext_sg = !!token; |
| 2909 | break; |
| 2910 | |
| 2911 | case SRP_OPT_SG_TABLESIZE: |
| 2912 | if (match_int(args, &token) || token < 1 || |
| 2913 | token > SCSI_MAX_SG_CHAIN_SEGMENTS) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2914 | pr_warn("bad max sg_tablesize parameter '%s'\n", |
| 2915 | p); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2916 | goto out; |
| 2917 | } |
| 2918 | target->sg_tablesize = token; |
| 2919 | break; |
| 2920 | |
Bart Van Assche | 4b5e5f4 | 2013-06-28 14:57:42 +0200 | [diff] [blame] | 2921 | case SRP_OPT_COMP_VECTOR: |
| 2922 | if (match_int(args, &token) || token < 0) { |
| 2923 | pr_warn("bad comp_vector parameter '%s'\n", p); |
| 2924 | goto out; |
| 2925 | } |
| 2926 | target->comp_vector = token; |
| 2927 | break; |
| 2928 | |
Vu Pham | 7bb312e | 2013-10-26 14:31:27 +0200 | [diff] [blame] | 2929 | case SRP_OPT_TL_RETRY_COUNT: |
| 2930 | if (match_int(args, &token) || token < 2 || token > 7) { |
| 2931 | pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n", |
| 2932 | p); |
| 2933 | goto out; |
| 2934 | } |
| 2935 | target->tl_retry_count = token; |
| 2936 | break; |
| 2937 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2938 | default: |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2939 | pr_warn("unknown parameter or missing value '%s' in target creation request\n", |
| 2940 | p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2941 | goto out; |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL) |
| 2946 | ret = 0; |
| 2947 | else |
| 2948 | for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i) |
| 2949 | if ((srp_opt_tokens[i].token & SRP_OPT_ALL) && |
| 2950 | !(srp_opt_tokens[i].token & opt_mask)) |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 2951 | pr_warn("target creation request is missing parameter '%s'\n", |
| 2952 | srp_opt_tokens[i].pattern); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2953 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2954 | if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue |
| 2955 | && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN)) |
| 2956 | pr_warn("cmd_per_lun = %d > queue_size = %d\n", |
| 2957 | target->scsi_host->cmd_per_lun, |
| 2958 | target->scsi_host->can_queue); |
| 2959 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2960 | out: |
| 2961 | kfree(options); |
| 2962 | return ret; |
| 2963 | } |
| 2964 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2965 | static ssize_t srp_create_target(struct device *dev, |
| 2966 | struct device_attribute *attr, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2967 | const char *buf, size_t count) |
| 2968 | { |
| 2969 | struct srp_host *host = |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2970 | container_of(dev, struct srp_host, dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2971 | struct Scsi_Host *target_host; |
| 2972 | struct srp_target_port *target; |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 2973 | struct srp_device *srp_dev = host->srp_dev; |
| 2974 | struct ib_device *ibdev = srp_dev->dev; |
Bart Van Assche | b81d00b | 2013-10-26 14:38:47 +0200 | [diff] [blame] | 2975 | int ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2976 | |
| 2977 | target_host = scsi_host_alloc(&srp_template, |
| 2978 | sizeof (struct srp_target_port)); |
| 2979 | if (!target_host) |
| 2980 | return -ENOMEM; |
| 2981 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2982 | target_host->transportt = ib_srp_transport_template; |
Bart Van Assche | fd1b6c4 | 2011-07-13 09:19:16 -0700 | [diff] [blame] | 2983 | target_host->max_channel = 0; |
| 2984 | target_host->max_id = 1; |
Arne Redlich | 3c8edf0 | 2006-11-15 12:43:00 +0100 | [diff] [blame] | 2985 | target_host->max_lun = SRP_MAX_LUN; |
| 2986 | target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb; |
Roland Dreier | 5f06899 | 2005-11-11 14:06:01 -0800 | [diff] [blame] | 2987 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2988 | target = host_to_target(target_host); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2989 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 2990 | target->io_class = SRP_REV16A_IB_IO_CLASS; |
| 2991 | target->scsi_host = target_host; |
| 2992 | target->srp_host = host; |
| 2993 | target->lkey = host->srp_dev->mr->lkey; |
| 2994 | target->rkey = host->srp_dev->mr->rkey; |
| 2995 | target->cmd_sg_cnt = cmd_sg_entries; |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 2996 | target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries; |
| 2997 | target->allow_ext_sg = allow_ext_sg; |
Vu Pham | 7bb312e | 2013-10-26 14:31:27 +0200 | [diff] [blame] | 2998 | target->tl_retry_count = 7; |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 2999 | target->queue_size = SRP_DEFAULT_QUEUE_SIZE; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3000 | |
Bart Van Assche | 34aa654 | 2014-10-30 14:47:22 +0100 | [diff] [blame] | 3001 | /* |
| 3002 | * Avoid that the SCSI host can be removed by srp_remove_target() |
| 3003 | * before this function returns. |
| 3004 | */ |
| 3005 | scsi_host_get(target->scsi_host); |
| 3006 | |
Bart Van Assche | 2d7091b | 2014-03-14 13:52:45 +0100 | [diff] [blame] | 3007 | mutex_lock(&host->add_target_mutex); |
| 3008 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3009 | ret = srp_parse_options(buf, target); |
| 3010 | if (ret) |
| 3011 | goto err; |
| 3012 | |
Bart Van Assche | 4d73f95 | 2013-10-26 14:40:37 +0200 | [diff] [blame] | 3013 | target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE; |
| 3014 | |
Bart Van Assche | 96fc248 | 2013-06-28 14:51:26 +0200 | [diff] [blame] | 3015 | if (!srp_conn_unique(target->srp_host, target)) { |
| 3016 | shost_printk(KERN_INFO, target->scsi_host, |
| 3017 | PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n", |
| 3018 | be64_to_cpu(target->id_ext), |
| 3019 | be64_to_cpu(target->ioc_guid), |
| 3020 | be64_to_cpu(target->initiator_ext)); |
| 3021 | ret = -EEXIST; |
| 3022 | goto err; |
| 3023 | } |
| 3024 | |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 3025 | if (!srp_dev->has_fmr && !srp_dev->has_fr && !target->allow_ext_sg && |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 3026 | target->cmd_sg_cnt < target->sg_tablesize) { |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 3027 | pr_warn("No MR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n"); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 3028 | target->sg_tablesize = target->cmd_sg_cnt; |
| 3029 | } |
| 3030 | |
| 3031 | target_host->sg_tablesize = target->sg_tablesize; |
| 3032 | target->indirect_size = target->sg_tablesize * |
| 3033 | sizeof (struct srp_direct_buf); |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 3034 | target->max_iu_len = sizeof (struct srp_cmd) + |
| 3035 | sizeof (struct srp_indirect_buf) + |
| 3036 | target->cmd_sg_cnt * sizeof (struct srp_direct_buf); |
| 3037 | |
Bart Van Assche | c1120f8 | 2013-10-26 14:35:08 +0200 | [diff] [blame] | 3038 | INIT_WORK(&target->tl_err_work, srp_tl_err_work); |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 3039 | INIT_WORK(&target->remove_work, srp_remove_work); |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 3040 | spin_lock_init(&target->lock); |
| 3041 | INIT_LIST_HEAD(&target->free_tx); |
Bart Van Assche | b81d00b | 2013-10-26 14:38:47 +0200 | [diff] [blame] | 3042 | ret = srp_alloc_req_data(target); |
| 3043 | if (ret) |
| 3044 | goto err_free_mem; |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 3045 | |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 3046 | ret = ib_query_gid(ibdev, host->port, 0, &target->sgid); |
Sagi Grimberg | 2088ca6 | 2014-03-14 13:51:58 +0100 | [diff] [blame] | 3047 | if (ret) |
| 3048 | goto err_free_mem; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3049 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3050 | ret = srp_create_target_ib(target); |
| 3051 | if (ret) |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 3052 | goto err_free_mem; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3053 | |
David Dillow | 9fe4bcf | 2008-01-08 17:08:52 -0500 | [diff] [blame] | 3054 | ret = srp_new_cm_id(target); |
| 3055 | if (ret) |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 3056 | goto err_free_ib; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3057 | |
| 3058 | ret = srp_connect_target(target); |
| 3059 | if (ret) { |
David Dillow | 7aa54bd | 2008-01-07 18:23:41 -0500 | [diff] [blame] | 3060 | shost_printk(KERN_ERR, target->scsi_host, |
| 3061 | PFX "Connection failed\n"); |
Bart Van Assche | 394c595 | 2014-10-30 14:46:27 +0100 | [diff] [blame] | 3062 | goto err_free_ib; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | ret = srp_add_target(host, target); |
| 3066 | if (ret) |
| 3067 | goto err_disconnect; |
| 3068 | |
Bart Van Assche | 34aa654 | 2014-10-30 14:47:22 +0100 | [diff] [blame] | 3069 | if (target->state != SRP_TARGET_REMOVED) { |
| 3070 | shost_printk(KERN_DEBUG, target->scsi_host, PFX |
| 3071 | "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n", |
| 3072 | be64_to_cpu(target->id_ext), |
| 3073 | be64_to_cpu(target->ioc_guid), |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 3074 | be16_to_cpu(target->pkey), |
Bart Van Assche | 34aa654 | 2014-10-30 14:47:22 +0100 | [diff] [blame] | 3075 | be64_to_cpu(target->service_id), |
Bart Van Assche | 747fe00 | 2014-10-30 14:48:05 +0100 | [diff] [blame^] | 3076 | target->sgid.raw, target->orig_dgid.raw); |
Bart Van Assche | 34aa654 | 2014-10-30 14:47:22 +0100 | [diff] [blame] | 3077 | } |
Bart Van Assche | e7ffde0 | 2014-03-14 13:52:21 +0100 | [diff] [blame] | 3078 | |
Bart Van Assche | 2d7091b | 2014-03-14 13:52:45 +0100 | [diff] [blame] | 3079 | ret = count; |
| 3080 | |
| 3081 | out: |
| 3082 | mutex_unlock(&host->add_target_mutex); |
Bart Van Assche | 34aa654 | 2014-10-30 14:47:22 +0100 | [diff] [blame] | 3083 | |
| 3084 | scsi_host_put(target->scsi_host); |
| 3085 | |
Bart Van Assche | 2d7091b | 2014-03-14 13:52:45 +0100 | [diff] [blame] | 3086 | return ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3087 | |
| 3088 | err_disconnect: |
| 3089 | srp_disconnect_target(target); |
| 3090 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 3091 | err_free_ib: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3092 | srp_free_target_ib(target); |
| 3093 | |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 3094 | err_free_mem: |
| 3095 | srp_free_req_data(target); |
| 3096 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3097 | err: |
| 3098 | scsi_host_put(target_host); |
Bart Van Assche | 2d7091b | 2014-03-14 13:52:45 +0100 | [diff] [blame] | 3099 | goto out; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3100 | } |
| 3101 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3102 | static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3103 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3104 | static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, |
| 3105 | char *buf) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3106 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3107 | struct srp_host *host = container_of(dev, struct srp_host, dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3108 | |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 3109 | return sprintf(buf, "%s\n", host->srp_dev->dev->name); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3110 | } |
| 3111 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3112 | static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3113 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3114 | static ssize_t show_port(struct device *dev, struct device_attribute *attr, |
| 3115 | char *buf) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3116 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3117 | struct srp_host *host = container_of(dev, struct srp_host, dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3118 | |
| 3119 | return sprintf(buf, "%d\n", host->port); |
| 3120 | } |
| 3121 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3122 | static DEVICE_ATTR(port, S_IRUGO, show_port, NULL); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3123 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3124 | 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] | 3125 | { |
| 3126 | struct srp_host *host; |
| 3127 | |
| 3128 | host = kzalloc(sizeof *host, GFP_KERNEL); |
| 3129 | if (!host) |
| 3130 | return NULL; |
| 3131 | |
| 3132 | INIT_LIST_HEAD(&host->target_list); |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 3133 | spin_lock_init(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3134 | init_completion(&host->released); |
Bart Van Assche | 2d7091b | 2014-03-14 13:52:45 +0100 | [diff] [blame] | 3135 | mutex_init(&host->add_target_mutex); |
Greg Kroah-Hartman | 0532193 | 2008-03-06 00:13:36 +0100 | [diff] [blame] | 3136 | host->srp_dev = device; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3137 | host->port = port; |
| 3138 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3139 | host->dev.class = &srp_class; |
| 3140 | host->dev.parent = device->dev->dma_device; |
Kay Sievers | d927e38 | 2009-01-06 10:44:39 -0800 | [diff] [blame] | 3141 | dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3142 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3143 | if (device_register(&host->dev)) |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3144 | goto free_host; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3145 | if (device_create_file(&host->dev, &dev_attr_add_target)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3146 | goto err_class; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3147 | if (device_create_file(&host->dev, &dev_attr_ibdev)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3148 | goto err_class; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3149 | if (device_create_file(&host->dev, &dev_attr_port)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3150 | goto err_class; |
| 3151 | |
| 3152 | return host; |
| 3153 | |
| 3154 | err_class: |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3155 | device_unregister(&host->dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3156 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3157 | free_host: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3158 | kfree(host); |
| 3159 | |
| 3160 | return NULL; |
| 3161 | } |
| 3162 | |
| 3163 | static void srp_add_one(struct ib_device *device) |
| 3164 | { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3165 | struct srp_device *srp_dev; |
| 3166 | struct ib_device_attr *dev_attr; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3167 | struct srp_host *host; |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 3168 | int mr_page_shift, s, e, p; |
| 3169 | u64 max_pages_per_mr; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3170 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3171 | dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL); |
| 3172 | if (!dev_attr) |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 3173 | return; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3174 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3175 | if (ib_query_device(device, dev_attr)) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 3176 | pr_warn("Query device failed for %s\n", device->name); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3177 | goto free_attr; |
| 3178 | } |
| 3179 | |
| 3180 | srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL); |
| 3181 | if (!srp_dev) |
| 3182 | goto free_attr; |
| 3183 | |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 3184 | srp_dev->has_fmr = (device->alloc_fmr && device->dealloc_fmr && |
| 3185 | device->map_phys_fmr && device->unmap_fmr); |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 3186 | srp_dev->has_fr = (dev_attr->device_cap_flags & |
| 3187 | IB_DEVICE_MEM_MGT_EXTENSIONS); |
| 3188 | if (!srp_dev->has_fmr && !srp_dev->has_fr) |
| 3189 | dev_warn(&device->dev, "neither FMR nor FR is supported\n"); |
| 3190 | |
| 3191 | srp_dev->use_fast_reg = (srp_dev->has_fr && |
| 3192 | (!srp_dev->has_fmr || prefer_fr)); |
Bart Van Assche | d1b4289 | 2014-05-20 15:07:20 +0200 | [diff] [blame] | 3193 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3194 | /* |
| 3195 | * Use the smallest page size supported by the HCA, down to a |
David Dillow | 8f26c9f | 2011-01-14 19:45:50 -0500 | [diff] [blame] | 3196 | * minimum of 4096 bytes. We're unlikely to build large sglists |
| 3197 | * out of smaller entries. |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3198 | */ |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 3199 | mr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1); |
| 3200 | srp_dev->mr_page_size = 1 << mr_page_shift; |
| 3201 | srp_dev->mr_page_mask = ~((u64) srp_dev->mr_page_size - 1); |
| 3202 | max_pages_per_mr = dev_attr->max_mr_size; |
| 3203 | do_div(max_pages_per_mr, srp_dev->mr_page_size); |
| 3204 | srp_dev->max_pages_per_mr = min_t(u64, SRP_MAX_PAGES_PER_MR, |
| 3205 | max_pages_per_mr); |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 3206 | if (srp_dev->use_fast_reg) { |
| 3207 | srp_dev->max_pages_per_mr = |
| 3208 | min_t(u32, srp_dev->max_pages_per_mr, |
| 3209 | dev_attr->max_fast_reg_page_list_len); |
| 3210 | } |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 3211 | srp_dev->mr_max_size = srp_dev->mr_page_size * |
| 3212 | srp_dev->max_pages_per_mr; |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 3213 | pr_debug("%s: mr_page_shift = %d, dev_attr->max_mr_size = %#llx, dev_attr->max_fast_reg_page_list_len = %u, max_pages_per_mr = %d, mr_max_size = %#x\n", |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 3214 | device->name, mr_page_shift, dev_attr->max_mr_size, |
Bart Van Assche | 5cfb178 | 2014-05-20 15:08:34 +0200 | [diff] [blame] | 3215 | dev_attr->max_fast_reg_page_list_len, |
Bart Van Assche | 52ede08 | 2014-05-20 15:07:45 +0200 | [diff] [blame] | 3216 | srp_dev->max_pages_per_mr, srp_dev->mr_max_size); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3217 | |
| 3218 | INIT_LIST_HEAD(&srp_dev->dev_list); |
| 3219 | |
| 3220 | srp_dev->dev = device; |
| 3221 | srp_dev->pd = ib_alloc_pd(device); |
| 3222 | if (IS_ERR(srp_dev->pd)) |
| 3223 | goto free_dev; |
| 3224 | |
| 3225 | srp_dev->mr = ib_get_dma_mr(srp_dev->pd, |
| 3226 | IB_ACCESS_LOCAL_WRITE | |
| 3227 | IB_ACCESS_REMOTE_READ | |
| 3228 | IB_ACCESS_REMOTE_WRITE); |
| 3229 | if (IS_ERR(srp_dev->mr)) |
| 3230 | goto err_pd; |
| 3231 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 3232 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3233 | s = 0; |
| 3234 | e = 0; |
| 3235 | } else { |
| 3236 | s = 1; |
| 3237 | e = device->phys_port_cnt; |
| 3238 | } |
| 3239 | |
| 3240 | for (p = s; p <= e; ++p) { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3241 | host = srp_add_port(srp_dev, p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3242 | if (host) |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3243 | list_add_tail(&host->list, &srp_dev->dev_list); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3244 | } |
| 3245 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3246 | ib_set_client_data(device, &srp_client, srp_dev); |
| 3247 | |
| 3248 | goto free_attr; |
| 3249 | |
| 3250 | err_pd: |
| 3251 | ib_dealloc_pd(srp_dev->pd); |
| 3252 | |
| 3253 | free_dev: |
| 3254 | kfree(srp_dev); |
| 3255 | |
| 3256 | free_attr: |
| 3257 | kfree(dev_attr); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | static void srp_remove_one(struct ib_device *device) |
| 3261 | { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3262 | struct srp_device *srp_dev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3263 | struct srp_host *host, *tmp_host; |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 3264 | struct srp_target_port *target; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3265 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3266 | srp_dev = ib_get_client_data(device, &srp_client); |
Dotan Barak | 1fe0cb8 | 2013-06-12 15:20:36 +0200 | [diff] [blame] | 3267 | if (!srp_dev) |
| 3268 | return; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3269 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3270 | 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] | 3271 | device_unregister(&host->dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3272 | /* |
| 3273 | * Wait for the sysfs entry to go away, so that no new |
| 3274 | * target ports can be created. |
| 3275 | */ |
| 3276 | wait_for_completion(&host->released); |
| 3277 | |
| 3278 | /* |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 3279 | * Remove all target ports. |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3280 | */ |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 3281 | spin_lock(&host->target_lock); |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 3282 | list_for_each_entry(target, &host->target_list, list) |
| 3283 | srp_queue_remove_work(target); |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 3284 | spin_unlock(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3285 | |
| 3286 | /* |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 3287 | * Wait for tl_err and target port removal tasks. |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3288 | */ |
Bart Van Assche | ef6c49d | 2011-12-26 16:49:18 +0000 | [diff] [blame] | 3289 | flush_workqueue(system_long_wq); |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 3290 | flush_workqueue(srp_remove_wq); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3291 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3292 | kfree(host); |
| 3293 | } |
| 3294 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3295 | ib_dereg_mr(srp_dev->mr); |
| 3296 | ib_dealloc_pd(srp_dev->pd); |
| 3297 | |
| 3298 | kfree(srp_dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3299 | } |
| 3300 | |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 3301 | static struct srp_function_template ib_srp_transport_functions = { |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 3302 | .has_rport_state = true, |
| 3303 | .reset_timer_if_blocked = true, |
Bart Van Assche | a95cadb | 2013-10-26 14:37:17 +0200 | [diff] [blame] | 3304 | .reconnect_delay = &srp_reconnect_delay, |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 3305 | .fast_io_fail_tmo = &srp_fast_io_fail_tmo, |
| 3306 | .dev_loss_tmo = &srp_dev_loss_tmo, |
| 3307 | .reconnect = srp_rport_reconnect, |
Bart Van Assche | dc1bdbd | 2011-09-16 20:41:13 +0200 | [diff] [blame] | 3308 | .rport_delete = srp_rport_delete, |
Bart Van Assche | ed9b226 | 2013-10-26 14:34:27 +0200 | [diff] [blame] | 3309 | .terminate_rport_io = srp_terminate_io, |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 3310 | }; |
| 3311 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3312 | static int __init srp_init_module(void) |
| 3313 | { |
| 3314 | int ret; |
| 3315 | |
Bart Van Assche | dcb4cb8 | 2010-11-26 13:22:48 -0500 | [diff] [blame] | 3316 | 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] | 3317 | |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 3318 | if (srp_sg_tablesize) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 3319 | pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n"); |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 3320 | if (!cmd_sg_entries) |
| 3321 | cmd_sg_entries = srp_sg_tablesize; |
| 3322 | } |
| 3323 | |
| 3324 | if (!cmd_sg_entries) |
| 3325 | cmd_sg_entries = SRP_DEF_SG_TABLESIZE; |
| 3326 | |
| 3327 | if (cmd_sg_entries > 255) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 3328 | pr_warn("Clamping cmd_sg_entries to 255\n"); |
David Dillow | 4924864 | 2011-01-14 18:23:24 -0500 | [diff] [blame] | 3329 | cmd_sg_entries = 255; |
David Dillow | 1e89a19 | 2008-04-16 21:01:12 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 3332 | if (!indirect_sg_entries) |
| 3333 | indirect_sg_entries = cmd_sg_entries; |
| 3334 | else if (indirect_sg_entries < cmd_sg_entries) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 3335 | pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n", |
| 3336 | cmd_sg_entries); |
David Dillow | c07d424 | 2011-01-16 13:57:10 -0500 | [diff] [blame] | 3337 | indirect_sg_entries = cmd_sg_entries; |
| 3338 | } |
| 3339 | |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 3340 | srp_remove_wq = create_workqueue("srp_remove"); |
Wei Yongjun | da05be2 | 2014-08-14 08:56:22 +0800 | [diff] [blame] | 3341 | if (!srp_remove_wq) { |
| 3342 | ret = -ENOMEM; |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 3343 | goto out; |
| 3344 | } |
| 3345 | |
| 3346 | ret = -ENOMEM; |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 3347 | ib_srp_transport_template = |
| 3348 | srp_attach_transport(&ib_srp_transport_functions); |
| 3349 | if (!ib_srp_transport_template) |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 3350 | goto destroy_wq; |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 3351 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3352 | ret = class_register(&srp_class); |
| 3353 | if (ret) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 3354 | pr_err("couldn't register class infiniband_srp\n"); |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 3355 | goto release_tr; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3356 | } |
| 3357 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 3358 | ib_sa_register_client(&srp_sa_client); |
| 3359 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3360 | ret = ib_register_client(&srp_client); |
| 3361 | if (ret) { |
Bart Van Assche | e0bda7d | 2012-01-14 12:39:44 +0000 | [diff] [blame] | 3362 | pr_err("couldn't register IB client\n"); |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 3363 | goto unreg_sa; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3364 | } |
| 3365 | |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 3366 | out: |
| 3367 | return ret; |
| 3368 | |
| 3369 | unreg_sa: |
| 3370 | ib_sa_unregister_client(&srp_sa_client); |
| 3371 | class_unregister(&srp_class); |
| 3372 | |
| 3373 | release_tr: |
| 3374 | srp_release_transport(ib_srp_transport_template); |
| 3375 | |
| 3376 | destroy_wq: |
| 3377 | destroy_workqueue(srp_remove_wq); |
| 3378 | goto out; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | static void __exit srp_cleanup_module(void) |
| 3382 | { |
| 3383 | ib_unregister_client(&srp_client); |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 3384 | ib_sa_unregister_client(&srp_sa_client); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3385 | class_unregister(&srp_class); |
FUJITA Tomonori | 3236822 | 2007-06-27 16:33:12 +0900 | [diff] [blame] | 3386 | srp_release_transport(ib_srp_transport_template); |
Bart Van Assche | bcc0591 | 2014-07-09 15:57:26 +0200 | [diff] [blame] | 3387 | destroy_workqueue(srp_remove_wq); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | module_init(srp_init_module); |
| 3391 | module_exit(srp_cleanup_module); |