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