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