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