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