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