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