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