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