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