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. |
| 31 | * |
| 32 | * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $ |
| 33 | */ |
| 34 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 35 | #include <linux/module.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/err.h> |
| 39 | #include <linux/string.h> |
| 40 | #include <linux/parser.h> |
| 41 | #include <linux/random.h> |
Tim Schmielau | de25968 | 2006-01-08 01:02:05 -0800 | [diff] [blame] | 42 | #include <linux/jiffies.h> |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 43 | |
| 44 | #include <asm/atomic.h> |
| 45 | |
| 46 | #include <scsi/scsi.h> |
| 47 | #include <scsi/scsi_device.h> |
| 48 | #include <scsi/scsi_dbg.h> |
| 49 | #include <scsi/srp.h> |
| 50 | |
| 51 | #include <rdma/ib_cache.h> |
| 52 | |
| 53 | #include "ib_srp.h" |
| 54 | |
| 55 | #define DRV_NAME "ib_srp" |
| 56 | #define PFX DRV_NAME ": " |
| 57 | #define DRV_VERSION "0.2" |
| 58 | #define DRV_RELDATE "November 1, 2005" |
| 59 | |
| 60 | MODULE_AUTHOR("Roland Dreier"); |
| 61 | MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator " |
| 62 | "v" DRV_VERSION " (" DRV_RELDATE ")"); |
| 63 | MODULE_LICENSE("Dual BSD/GPL"); |
| 64 | |
Vu Pham | 74b0a15 | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 65 | static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE; |
| 66 | static int srp_max_iu_len; |
| 67 | |
| 68 | module_param(srp_sg_tablesize, int, 0444); |
| 69 | MODULE_PARM_DESC(srp_sg_tablesize, |
| 70 | "Max number of gather/scatter entries per I/O (default is 12)"); |
| 71 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 72 | static int topspin_workarounds = 1; |
| 73 | |
| 74 | module_param(topspin_workarounds, int, 0444); |
| 75 | MODULE_PARM_DESC(topspin_workarounds, |
| 76 | "Enable workarounds for Topspin/Cisco SRP target bugs if != 0"); |
| 77 | |
Ishai Rabinovitz | 559ce8f | 2006-08-03 10:35:43 -0700 | [diff] [blame] | 78 | static int mellanox_workarounds = 1; |
| 79 | |
| 80 | module_param(mellanox_workarounds, int, 0444); |
| 81 | MODULE_PARM_DESC(mellanox_workarounds, |
| 82 | "Enable workarounds for Mellanox SRP target bugs if != 0"); |
| 83 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 84 | static void srp_add_one(struct ib_device *device); |
| 85 | static void srp_remove_one(struct ib_device *device); |
| 86 | static void srp_completion(struct ib_cq *cq, void *target_ptr); |
| 87 | static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event); |
| 88 | |
| 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 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 139 | iu->dma = ib_dma_map_single(host->dev->dev, iu->buf, size, direction); |
| 140 | if (ib_dma_mapping_error(host->dev->dev, iu->dma)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 141 | goto out_free_buf; |
| 142 | |
| 143 | iu->size = size; |
| 144 | iu->direction = direction; |
| 145 | |
| 146 | return iu; |
| 147 | |
| 148 | out_free_buf: |
| 149 | kfree(iu->buf); |
| 150 | out_free_iu: |
| 151 | kfree(iu); |
| 152 | out: |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | static void srp_free_iu(struct srp_host *host, struct srp_iu *iu) |
| 157 | { |
| 158 | if (!iu) |
| 159 | return; |
| 160 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 161 | ib_dma_unmap_single(host->dev->dev, iu->dma, iu->size, iu->direction); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 162 | kfree(iu->buf); |
| 163 | kfree(iu); |
| 164 | } |
| 165 | |
| 166 | static void srp_qp_event(struct ib_event *event, void *context) |
| 167 | { |
| 168 | printk(KERN_ERR PFX "QP event %d\n", event->event); |
| 169 | } |
| 170 | |
| 171 | static int srp_init_qp(struct srp_target_port *target, |
| 172 | struct ib_qp *qp) |
| 173 | { |
| 174 | struct ib_qp_attr *attr; |
| 175 | int ret; |
| 176 | |
| 177 | attr = kmalloc(sizeof *attr, GFP_KERNEL); |
| 178 | if (!attr) |
| 179 | return -ENOMEM; |
| 180 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 181 | ret = ib_find_cached_pkey(target->srp_host->dev->dev, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 182 | target->srp_host->port, |
| 183 | be16_to_cpu(target->path.pkey), |
| 184 | &attr->pkey_index); |
| 185 | if (ret) |
| 186 | goto out; |
| 187 | |
| 188 | attr->qp_state = IB_QPS_INIT; |
| 189 | attr->qp_access_flags = (IB_ACCESS_REMOTE_READ | |
| 190 | IB_ACCESS_REMOTE_WRITE); |
| 191 | attr->port_num = target->srp_host->port; |
| 192 | |
| 193 | ret = ib_modify_qp(qp, attr, |
| 194 | IB_QP_STATE | |
| 195 | IB_QP_PKEY_INDEX | |
| 196 | IB_QP_ACCESS_FLAGS | |
| 197 | IB_QP_PORT); |
| 198 | |
| 199 | out: |
| 200 | kfree(attr); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static int srp_create_target_ib(struct srp_target_port *target) |
| 205 | { |
| 206 | struct ib_qp_init_attr *init_attr; |
| 207 | int ret; |
| 208 | |
| 209 | init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL); |
| 210 | if (!init_attr) |
| 211 | return -ENOMEM; |
| 212 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 213 | target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion, |
Michael S. Tsirkin | f4fd0b2 | 2007-05-03 13:48:47 +0300 | [diff] [blame] | 214 | NULL, target, SRP_CQ_SIZE, 0); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 215 | if (IS_ERR(target->cq)) { |
| 216 | ret = PTR_ERR(target->cq); |
| 217 | goto out; |
| 218 | } |
| 219 | |
| 220 | ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP); |
| 221 | |
| 222 | init_attr->event_handler = srp_qp_event; |
| 223 | init_attr->cap.max_send_wr = SRP_SQ_SIZE; |
| 224 | init_attr->cap.max_recv_wr = SRP_RQ_SIZE; |
| 225 | init_attr->cap.max_recv_sge = 1; |
| 226 | init_attr->cap.max_send_sge = 1; |
| 227 | init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; |
| 228 | init_attr->qp_type = IB_QPT_RC; |
| 229 | init_attr->send_cq = target->cq; |
| 230 | init_attr->recv_cq = target->cq; |
| 231 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 232 | target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 233 | if (IS_ERR(target->qp)) { |
| 234 | ret = PTR_ERR(target->qp); |
| 235 | ib_destroy_cq(target->cq); |
| 236 | goto out; |
| 237 | } |
| 238 | |
| 239 | ret = srp_init_qp(target, target->qp); |
| 240 | if (ret) { |
| 241 | ib_destroy_qp(target->qp); |
| 242 | ib_destroy_cq(target->cq); |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | out: |
| 247 | kfree(init_attr); |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static void srp_free_target_ib(struct srp_target_port *target) |
| 252 | { |
| 253 | int i; |
| 254 | |
| 255 | ib_destroy_qp(target->qp); |
| 256 | ib_destroy_cq(target->cq); |
| 257 | |
| 258 | for (i = 0; i < SRP_RQ_SIZE; ++i) |
| 259 | srp_free_iu(target->srp_host, target->rx_ring[i]); |
| 260 | for (i = 0; i < SRP_SQ_SIZE + 1; ++i) |
| 261 | srp_free_iu(target->srp_host, target->tx_ring[i]); |
| 262 | } |
| 263 | |
| 264 | static void srp_path_rec_completion(int status, |
| 265 | struct ib_sa_path_rec *pathrec, |
| 266 | void *target_ptr) |
| 267 | { |
| 268 | struct srp_target_port *target = target_ptr; |
| 269 | |
| 270 | target->status = status; |
| 271 | if (status) |
| 272 | printk(KERN_ERR PFX "Got failed path rec status %d\n", status); |
| 273 | else |
| 274 | target->path = *pathrec; |
| 275 | complete(&target->done); |
| 276 | } |
| 277 | |
| 278 | static int srp_lookup_path(struct srp_target_port *target) |
| 279 | { |
| 280 | target->path.numb_path = 1; |
| 281 | |
| 282 | init_completion(&target->done); |
| 283 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 284 | target->path_query_id = ib_sa_path_rec_get(&srp_sa_client, |
| 285 | target->srp_host->dev->dev, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 286 | target->srp_host->port, |
| 287 | &target->path, |
| 288 | IB_SA_PATH_REC_DGID | |
| 289 | IB_SA_PATH_REC_SGID | |
| 290 | IB_SA_PATH_REC_NUMB_PATH | |
| 291 | IB_SA_PATH_REC_PKEY, |
| 292 | SRP_PATH_REC_TIMEOUT_MS, |
| 293 | GFP_KERNEL, |
| 294 | srp_path_rec_completion, |
| 295 | target, &target->path_query); |
| 296 | if (target->path_query_id < 0) |
| 297 | return target->path_query_id; |
| 298 | |
| 299 | wait_for_completion(&target->done); |
| 300 | |
| 301 | if (target->status < 0) |
| 302 | printk(KERN_WARNING PFX "Path record query failed\n"); |
| 303 | |
| 304 | return target->status; |
| 305 | } |
| 306 | |
| 307 | static int srp_send_req(struct srp_target_port *target) |
| 308 | { |
| 309 | struct { |
| 310 | struct ib_cm_req_param param; |
| 311 | struct srp_login_req priv; |
| 312 | } *req = NULL; |
| 313 | int status; |
| 314 | |
| 315 | req = kzalloc(sizeof *req, GFP_KERNEL); |
| 316 | if (!req) |
| 317 | return -ENOMEM; |
| 318 | |
| 319 | req->param.primary_path = &target->path; |
| 320 | req->param.alternate_path = NULL; |
| 321 | req->param.service_id = target->service_id; |
| 322 | req->param.qp_num = target->qp->qp_num; |
| 323 | req->param.qp_type = target->qp->qp_type; |
| 324 | req->param.private_data = &req->priv; |
| 325 | req->param.private_data_len = sizeof req->priv; |
| 326 | req->param.flow_control = 1; |
| 327 | |
| 328 | get_random_bytes(&req->param.starting_psn, 4); |
| 329 | req->param.starting_psn &= 0xffffff; |
| 330 | |
| 331 | /* |
| 332 | * Pick some arbitrary defaults here; we could make these |
| 333 | * module parameters if anyone cared about setting them. |
| 334 | */ |
| 335 | req->param.responder_resources = 4; |
| 336 | req->param.remote_cm_response_timeout = 20; |
| 337 | req->param.local_cm_response_timeout = 20; |
| 338 | req->param.retry_count = 7; |
| 339 | req->param.rnr_retry_count = 7; |
| 340 | req->param.max_cm_retries = 15; |
| 341 | |
| 342 | req->priv.opcode = SRP_LOGIN_REQ; |
| 343 | req->priv.tag = 0; |
Vu Pham | 74b0a15 | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 344 | 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] | 345 | req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | |
| 346 | SRP_BUF_FORMAT_INDIRECT); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 347 | /* |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 348 | * In the published SRP specification (draft rev. 16a), the |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 349 | * port identifier format is 8 bytes of ID extension followed |
| 350 | * by 8 bytes of GUID. Older drafts put the two halves in the |
| 351 | * opposite order, so that the GUID comes first. |
| 352 | * |
| 353 | * Targets conforming to these obsolete drafts can be |
| 354 | * recognized by the I/O Class they report. |
| 355 | */ |
| 356 | if (target->io_class == SRP_REV10_IB_IO_CLASS) { |
| 357 | memcpy(req->priv.initiator_port_id, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 358 | &target->path.sgid.global.interface_id, 8); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 359 | memcpy(req->priv.initiator_port_id + 8, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 360 | &target->initiator_ext, 8); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 361 | memcpy(req->priv.target_port_id, &target->ioc_guid, 8); |
| 362 | memcpy(req->priv.target_port_id + 8, &target->id_ext, 8); |
| 363 | } else { |
| 364 | memcpy(req->priv.initiator_port_id, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 365 | &target->initiator_ext, 8); |
| 366 | memcpy(req->priv.initiator_port_id + 8, |
| 367 | &target->path.sgid.global.interface_id, 8); |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 368 | memcpy(req->priv.target_port_id, &target->id_ext, 8); |
| 369 | memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8); |
| 370 | } |
| 371 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 372 | /* |
| 373 | * Topspin/Cisco SRP targets will reject our login unless we |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 374 | * zero out the first 8 bytes of our initiator port ID and set |
| 375 | * the second 8 bytes to the local node GUID. |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 376 | */ |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 377 | if (srp_target_is_topspin(target)) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 378 | printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround " |
| 379 | "activated for target GUID %016llx\n", |
| 380 | (unsigned long long) be64_to_cpu(target->ioc_guid)); |
| 381 | memset(req->priv.initiator_port_id, 0, 8); |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 382 | memcpy(req->priv.initiator_port_id + 8, |
| 383 | &target->srp_host->dev->dev->node_guid, 8); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 384 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 385 | |
| 386 | status = ib_send_cm_req(target->cm_id, &req->param); |
| 387 | |
| 388 | kfree(req); |
| 389 | |
| 390 | return status; |
| 391 | } |
| 392 | |
| 393 | static void srp_disconnect_target(struct srp_target_port *target) |
| 394 | { |
| 395 | /* XXX should send SRP_I_LOGOUT request */ |
| 396 | |
| 397 | init_completion(&target->done); |
Roland Dreier | e658105 | 2006-05-17 09:13:21 -0700 | [diff] [blame] | 398 | if (ib_send_cm_dreq(target->cm_id, NULL, 0)) { |
| 399 | printk(KERN_DEBUG PFX "Sending CM DREQ failed\n"); |
| 400 | return; |
| 401 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 402 | wait_for_completion(&target->done); |
| 403 | } |
| 404 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 405 | static void srp_remove_work(struct work_struct *work) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 406 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 407 | struct srp_target_port *target = |
| 408 | container_of(work, struct srp_target_port, work); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 409 | |
| 410 | spin_lock_irq(target->scsi_host->host_lock); |
| 411 | if (target->state != SRP_TARGET_DEAD) { |
| 412 | spin_unlock_irq(target->scsi_host->host_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | target->state = SRP_TARGET_REMOVED; |
| 416 | spin_unlock_irq(target->scsi_host->host_lock); |
| 417 | |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 418 | spin_lock(&target->srp_host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 419 | list_del(&target->list); |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 420 | spin_unlock(&target->srp_host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 421 | |
| 422 | scsi_remove_host(target->scsi_host); |
| 423 | ib_destroy_cm_id(target->cm_id); |
| 424 | srp_free_target_ib(target); |
| 425 | scsi_host_put(target->scsi_host); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static int srp_connect_target(struct srp_target_port *target) |
| 429 | { |
| 430 | int ret; |
| 431 | |
| 432 | ret = srp_lookup_path(target); |
| 433 | if (ret) |
| 434 | return ret; |
| 435 | |
| 436 | while (1) { |
| 437 | init_completion(&target->done); |
| 438 | ret = srp_send_req(target); |
| 439 | if (ret) |
| 440 | return ret; |
| 441 | wait_for_completion(&target->done); |
| 442 | |
| 443 | /* |
| 444 | * The CM event handling code will set status to |
| 445 | * SRP_PORT_REDIRECT if we get a port redirect REJ |
| 446 | * back, or SRP_DLID_REDIRECT if we get a lid/qp |
| 447 | * redirect REJ back. |
| 448 | */ |
| 449 | switch (target->status) { |
| 450 | case 0: |
| 451 | return 0; |
| 452 | |
| 453 | case SRP_PORT_REDIRECT: |
| 454 | ret = srp_lookup_path(target); |
| 455 | if (ret) |
| 456 | return ret; |
| 457 | break; |
| 458 | |
| 459 | case SRP_DLID_REDIRECT: |
| 460 | break; |
| 461 | |
| 462 | default: |
| 463 | return target->status; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 468 | static void srp_unmap_data(struct scsi_cmnd *scmnd, |
| 469 | struct srp_target_port *target, |
| 470 | struct srp_request *req) |
| 471 | { |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 472 | if (!scsi_sglist(scmnd) || |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 473 | (scmnd->sc_data_direction != DMA_TO_DEVICE && |
| 474 | scmnd->sc_data_direction != DMA_FROM_DEVICE)) |
| 475 | return; |
| 476 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 477 | if (req->fmr) { |
| 478 | ib_fmr_pool_unmap(req->fmr); |
| 479 | req->fmr = NULL; |
| 480 | } |
| 481 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 482 | ib_dma_unmap_sg(target->srp_host->dev->dev, scsi_sglist(scmnd), |
| 483 | scsi_sg_count(scmnd), scmnd->sc_data_direction); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 486 | static void srp_remove_req(struct srp_target_port *target, struct srp_request *req) |
| 487 | { |
| 488 | srp_unmap_data(req->scmnd, target, req); |
| 489 | list_move_tail(&req->list, &target->free_reqs); |
| 490 | } |
| 491 | |
| 492 | static void srp_reset_req(struct srp_target_port *target, struct srp_request *req) |
| 493 | { |
| 494 | req->scmnd->result = DID_RESET << 16; |
| 495 | req->scmnd->scsi_done(req->scmnd); |
| 496 | srp_remove_req(target, req); |
| 497 | } |
| 498 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 499 | static int srp_reconnect_target(struct srp_target_port *target) |
| 500 | { |
| 501 | struct ib_cm_id *new_cm_id; |
| 502 | struct ib_qp_attr qp_attr; |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 503 | struct srp_request *req, *tmp; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 504 | struct ib_wc wc; |
| 505 | int ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 506 | |
| 507 | spin_lock_irq(target->scsi_host->host_lock); |
| 508 | if (target->state != SRP_TARGET_LIVE) { |
| 509 | spin_unlock_irq(target->scsi_host->host_lock); |
| 510 | return -EAGAIN; |
| 511 | } |
| 512 | target->state = SRP_TARGET_CONNECTING; |
| 513 | spin_unlock_irq(target->scsi_host->host_lock); |
| 514 | |
| 515 | srp_disconnect_target(target); |
| 516 | /* |
| 517 | * Now get a new local CM ID so that we avoid confusing the |
| 518 | * target in case things are really fouled up. |
| 519 | */ |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 520 | new_cm_id = ib_create_cm_id(target->srp_host->dev->dev, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 521 | srp_cm_handler, target); |
| 522 | if (IS_ERR(new_cm_id)) { |
| 523 | ret = PTR_ERR(new_cm_id); |
| 524 | goto err; |
| 525 | } |
| 526 | ib_destroy_cm_id(target->cm_id); |
| 527 | target->cm_id = new_cm_id; |
| 528 | |
| 529 | qp_attr.qp_state = IB_QPS_RESET; |
| 530 | ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE); |
| 531 | if (ret) |
| 532 | goto err; |
| 533 | |
| 534 | ret = srp_init_qp(target, target->qp); |
| 535 | if (ret) |
| 536 | goto err; |
| 537 | |
| 538 | while (ib_poll_cq(target->cq, 1, &wc) > 0) |
| 539 | ; /* nothing */ |
| 540 | |
Ishai Rabinovitz | d916a8f | 2006-07-25 19:54:09 +0300 | [diff] [blame] | 541 | spin_lock_irq(target->scsi_host->host_lock); |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 542 | list_for_each_entry_safe(req, tmp, &target->req_queue, list) |
| 543 | srp_reset_req(target, req); |
Ishai Rabinovitz | d916a8f | 2006-07-25 19:54:09 +0300 | [diff] [blame] | 544 | spin_unlock_irq(target->scsi_host->host_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 545 | |
| 546 | target->rx_head = 0; |
| 547 | target->tx_head = 0; |
| 548 | target->tx_tail = 0; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 549 | |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 550 | target->qp_in_error = 0; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 551 | ret = srp_connect_target(target); |
| 552 | if (ret) |
| 553 | goto err; |
| 554 | |
| 555 | spin_lock_irq(target->scsi_host->host_lock); |
| 556 | if (target->state == SRP_TARGET_CONNECTING) { |
| 557 | ret = 0; |
| 558 | target->state = SRP_TARGET_LIVE; |
| 559 | } else |
| 560 | ret = -EAGAIN; |
| 561 | spin_unlock_irq(target->scsi_host->host_lock); |
| 562 | |
| 563 | return ret; |
| 564 | |
| 565 | err: |
| 566 | printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret); |
| 567 | |
| 568 | /* |
| 569 | * We couldn't reconnect, so kill our target port off. |
| 570 | * However, we have to defer the real removal because we might |
| 571 | * be in the context of the SCSI error handler now, which |
| 572 | * would deadlock if we call scsi_remove_host(). |
| 573 | */ |
| 574 | spin_lock_irq(target->scsi_host->host_lock); |
| 575 | if (target->state == SRP_TARGET_CONNECTING) { |
| 576 | target->state = SRP_TARGET_DEAD; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 577 | INIT_WORK(&target->work, srp_remove_work); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 578 | schedule_work(&target->work); |
| 579 | } |
| 580 | spin_unlock_irq(target->scsi_host->host_lock); |
| 581 | |
| 582 | return ret; |
| 583 | } |
| 584 | |
Ishai Rabinovitz | 559ce8f | 2006-08-03 10:35:43 -0700 | [diff] [blame] | 585 | 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] | 586 | int sg_cnt, struct srp_request *req, |
| 587 | struct srp_direct_buf *buf) |
| 588 | { |
| 589 | u64 io_addr = 0; |
| 590 | u64 *dma_pages; |
| 591 | u32 len; |
| 592 | int page_cnt; |
| 593 | int i, j; |
| 594 | int ret; |
Ishai Rabinovitz | 559ce8f | 2006-08-03 10:35:43 -0700 | [diff] [blame] | 595 | struct srp_device *dev = target->srp_host->dev; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 596 | struct ib_device *ibdev = dev->dev; |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 597 | struct scatterlist *sg; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 598 | |
| 599 | if (!dev->fmr_pool) |
| 600 | return -ENODEV; |
| 601 | |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 602 | if (srp_target_is_mellanox(target) && |
| 603 | (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask)) |
Ishai Rabinovitz | 559ce8f | 2006-08-03 10:35:43 -0700 | [diff] [blame] | 604 | return -EINVAL; |
| 605 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 606 | len = page_cnt = 0; |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 607 | scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) { |
| 608 | unsigned int dma_len = ib_sg_dma_len(ibdev, sg); |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 609 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 610 | if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 611 | if (i > 0) |
| 612 | return -EINVAL; |
| 613 | else |
| 614 | ++page_cnt; |
| 615 | } |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 616 | if ((ib_sg_dma_address(ibdev, sg) + dma_len) & |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 617 | ~dev->fmr_page_mask) { |
| 618 | if (i < sg_cnt - 1) |
| 619 | return -EINVAL; |
| 620 | else |
| 621 | ++page_cnt; |
| 622 | } |
| 623 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 624 | len += dma_len; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | page_cnt += len >> dev->fmr_page_shift; |
| 628 | if (page_cnt > SRP_FMR_SIZE) |
| 629 | return -ENOMEM; |
| 630 | |
| 631 | dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC); |
| 632 | if (!dma_pages) |
| 633 | return -ENOMEM; |
| 634 | |
| 635 | page_cnt = 0; |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 636 | scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) { |
| 637 | unsigned int dma_len = ib_sg_dma_len(ibdev, sg); |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 638 | |
| 639 | for (j = 0; j < dma_len; j += dev->fmr_page_size) |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 640 | dma_pages[page_cnt++] = |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 641 | (ib_sg_dma_address(ibdev, sg) & |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 642 | dev->fmr_page_mask) + j; |
| 643 | } |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 644 | |
| 645 | req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool, |
Michael S. Tsirkin | adfaa88 | 2006-07-14 00:23:55 -0700 | [diff] [blame] | 646 | dma_pages, page_cnt, io_addr); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 647 | if (IS_ERR(req->fmr)) { |
| 648 | ret = PTR_ERR(req->fmr); |
Vu Pham | 6583eb3 | 2006-07-14 00:23:53 -0700 | [diff] [blame] | 649 | req->fmr = NULL; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 650 | goto out; |
| 651 | } |
| 652 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 653 | buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) & |
| 654 | ~dev->fmr_page_mask); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 655 | buf->key = cpu_to_be32(req->fmr->fmr->rkey); |
| 656 | buf->len = cpu_to_be32(len); |
| 657 | |
| 658 | ret = 0; |
| 659 | |
| 660 | out: |
| 661 | kfree(dma_pages); |
| 662 | |
| 663 | return ret; |
| 664 | } |
| 665 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 666 | static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target, |
| 667 | struct srp_request *req) |
| 668 | { |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 669 | struct scatterlist *scat; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 670 | struct srp_cmd *cmd = req->cmd->buf; |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 671 | int len, nents, count; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 672 | u8 fmt = SRP_DATA_DESC_DIRECT; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 673 | struct srp_device *dev; |
| 674 | struct ib_device *ibdev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 675 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 676 | if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 677 | return sizeof (struct srp_cmd); |
| 678 | |
| 679 | if (scmnd->sc_data_direction != DMA_FROM_DEVICE && |
| 680 | scmnd->sc_data_direction != DMA_TO_DEVICE) { |
| 681 | printk(KERN_WARNING PFX "Unhandled data direction %d\n", |
| 682 | scmnd->sc_data_direction); |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 686 | nents = scsi_sg_count(scmnd); |
| 687 | scat = scsi_sglist(scmnd); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 688 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 689 | dev = target->srp_host->dev; |
| 690 | ibdev = dev->dev; |
| 691 | |
| 692 | count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 693 | |
| 694 | fmt = SRP_DATA_DESC_DIRECT; |
| 695 | len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 696 | |
| 697 | if (count == 1) { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 698 | /* |
| 699 | * The midlayer only generated a single gather/scatter |
| 700 | * entry, or DMA mapping coalesced everything to a |
| 701 | * single entry. So a direct descriptor along with |
| 702 | * the DMA MR suffices. |
| 703 | */ |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 704 | struct srp_direct_buf *buf = (void *) cmd->add_data; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 705 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 706 | buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat)); |
| 707 | buf->key = cpu_to_be32(dev->mr->rkey); |
| 708 | buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat)); |
Ishai Rabinovitz | 559ce8f | 2006-08-03 10:35:43 -0700 | [diff] [blame] | 709 | } else if (srp_map_fmr(target, scat, count, req, |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 710 | (void *) cmd->add_data)) { |
| 711 | /* |
| 712 | * FMR mapping failed, and the scatterlist has more |
| 713 | * than one entry. Generate an indirect memory |
| 714 | * descriptor. |
| 715 | */ |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 716 | struct srp_indirect_buf *buf = (void *) cmd->add_data; |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 717 | struct scatterlist *sg; |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 718 | u32 datalen = 0; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 719 | int i; |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 720 | |
| 721 | fmt = SRP_DATA_DESC_INDIRECT; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 722 | len = sizeof (struct srp_cmd) + |
| 723 | sizeof (struct srp_indirect_buf) + |
| 724 | count * sizeof (struct srp_direct_buf); |
| 725 | |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 726 | scsi_for_each_sg(scmnd, sg, count, i) { |
| 727 | unsigned int dma_len = ib_sg_dma_len(ibdev, sg); |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 728 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 729 | buf->desc_list[i].va = |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 730 | cpu_to_be64(ib_sg_dma_address(ibdev, sg)); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 731 | buf->desc_list[i].key = |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 732 | cpu_to_be32(dev->mr->rkey); |
| 733 | buf->desc_list[i].len = cpu_to_be32(dma_len); |
| 734 | datalen += dma_len; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 735 | } |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 736 | |
| 737 | if (scmnd->sc_data_direction == DMA_TO_DEVICE) |
| 738 | cmd->data_out_desc_cnt = count; |
| 739 | else |
| 740 | cmd->data_in_desc_cnt = count; |
| 741 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 742 | buf->table_desc.va = |
| 743 | cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 744 | buf->table_desc.key = |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 745 | cpu_to_be32(target->srp_host->dev->mr->rkey); |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 746 | buf->table_desc.len = |
| 747 | cpu_to_be32(count * sizeof (struct srp_direct_buf)); |
| 748 | |
Roland Dreier | cf36871 | 2006-03-24 15:47:26 -0800 | [diff] [blame] | 749 | buf->len = cpu_to_be32(datalen); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | if (scmnd->sc_data_direction == DMA_TO_DEVICE) |
| 753 | cmd->buf_fmt = fmt << 4; |
| 754 | else |
| 755 | cmd->buf_fmt = fmt; |
| 756 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 757 | return len; |
| 758 | } |
| 759 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 760 | static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp) |
| 761 | { |
| 762 | struct srp_request *req; |
| 763 | struct scsi_cmnd *scmnd; |
| 764 | unsigned long flags; |
| 765 | s32 delta; |
| 766 | |
| 767 | delta = (s32) be32_to_cpu(rsp->req_lim_delta); |
| 768 | |
| 769 | spin_lock_irqsave(target->scsi_host->host_lock, flags); |
| 770 | |
| 771 | target->req_lim += delta; |
| 772 | |
| 773 | req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT]; |
| 774 | |
| 775 | if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) { |
| 776 | if (be32_to_cpu(rsp->resp_data_len) < 4) |
| 777 | req->tsk_status = -1; |
| 778 | else |
| 779 | req->tsk_status = rsp->data[3]; |
| 780 | complete(&req->done); |
| 781 | } else { |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 782 | scmnd = req->scmnd; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 783 | if (!scmnd) |
| 784 | printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n", |
| 785 | (unsigned long long) rsp->tag); |
| 786 | scmnd->result = rsp->status; |
| 787 | |
| 788 | if (rsp->flags & SRP_RSP_FLAG_SNSVALID) { |
| 789 | memcpy(scmnd->sense_buffer, rsp->data + |
| 790 | be32_to_cpu(rsp->resp_data_len), |
| 791 | min_t(int, be32_to_cpu(rsp->sense_data_len), |
| 792 | SCSI_SENSE_BUFFERSIZE)); |
| 793 | } |
| 794 | |
| 795 | if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER)) |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 796 | scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt)); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 797 | else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER)) |
FUJITA Tomonori | bb350d1 | 2007-05-26 02:28:25 +0900 | [diff] [blame] | 798 | scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt)); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 799 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 800 | if (!req->tsk_mgmt) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 801 | scmnd->host_scribble = (void *) -1L; |
| 802 | scmnd->scsi_done(scmnd); |
| 803 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 804 | srp_remove_req(target, req); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 805 | } else |
| 806 | req->cmd_done = 1; |
| 807 | } |
| 808 | |
| 809 | spin_unlock_irqrestore(target->scsi_host->host_lock, flags); |
| 810 | } |
| 811 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 812 | static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc) |
| 813 | { |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 814 | struct ib_device *dev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 815 | struct srp_iu *iu; |
| 816 | u8 opcode; |
| 817 | |
| 818 | iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV]; |
| 819 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 820 | dev = target->srp_host->dev->dev; |
| 821 | ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len, |
| 822 | DMA_FROM_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 823 | |
| 824 | opcode = *(u8 *) iu->buf; |
| 825 | |
| 826 | if (0) { |
| 827 | int i; |
| 828 | |
| 829 | printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode); |
| 830 | |
| 831 | for (i = 0; i < wc->byte_len; ++i) { |
| 832 | if (i % 8 == 0) |
| 833 | printk(KERN_ERR " [%02x] ", i); |
| 834 | printk(" %02x", ((u8 *) iu->buf)[i]); |
| 835 | if ((i + 1) % 8 == 0) |
| 836 | printk("\n"); |
| 837 | } |
| 838 | |
| 839 | if (wc->byte_len % 8) |
| 840 | printk("\n"); |
| 841 | } |
| 842 | |
| 843 | switch (opcode) { |
| 844 | case SRP_RSP: |
| 845 | srp_process_rsp(target, iu->buf); |
| 846 | break; |
| 847 | |
| 848 | case SRP_T_LOGOUT: |
| 849 | /* XXX Handle target logout */ |
| 850 | printk(KERN_WARNING PFX "Got target logout request\n"); |
| 851 | break; |
| 852 | |
| 853 | default: |
| 854 | printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode); |
| 855 | break; |
| 856 | } |
| 857 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 858 | ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len, |
| 859 | DMA_FROM_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | static void srp_completion(struct ib_cq *cq, void *target_ptr) |
| 863 | { |
| 864 | struct srp_target_port *target = target_ptr; |
| 865 | struct ib_wc wc; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 866 | |
| 867 | ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); |
| 868 | while (ib_poll_cq(cq, 1, &wc) > 0) { |
| 869 | if (wc.status) { |
| 870 | printk(KERN_ERR PFX "failed %s status %d\n", |
| 871 | wc.wr_id & SRP_OP_RECV ? "receive" : "send", |
| 872 | wc.status); |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 873 | target->qp_in_error = 1; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 874 | break; |
| 875 | } |
| 876 | |
| 877 | if (wc.wr_id & SRP_OP_RECV) |
| 878 | srp_handle_recv(target, &wc); |
| 879 | else |
| 880 | ++target->tx_tail; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | static int __srp_post_recv(struct srp_target_port *target) |
| 885 | { |
| 886 | struct srp_iu *iu; |
| 887 | struct ib_sge list; |
| 888 | struct ib_recv_wr wr, *bad_wr; |
| 889 | unsigned int next; |
| 890 | int ret; |
| 891 | |
| 892 | next = target->rx_head & (SRP_RQ_SIZE - 1); |
| 893 | wr.wr_id = next | SRP_OP_RECV; |
| 894 | iu = target->rx_ring[next]; |
| 895 | |
| 896 | list.addr = iu->dma; |
| 897 | list.length = iu->size; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 898 | list.lkey = target->srp_host->dev->mr->lkey; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 899 | |
| 900 | wr.next = NULL; |
| 901 | wr.sg_list = &list; |
| 902 | wr.num_sge = 1; |
| 903 | |
| 904 | ret = ib_post_recv(target->qp, &wr, &bad_wr); |
| 905 | if (!ret) |
| 906 | ++target->rx_head; |
| 907 | |
| 908 | return ret; |
| 909 | } |
| 910 | |
| 911 | static int srp_post_recv(struct srp_target_port *target) |
| 912 | { |
| 913 | unsigned long flags; |
| 914 | int ret; |
| 915 | |
| 916 | spin_lock_irqsave(target->scsi_host->host_lock, flags); |
| 917 | ret = __srp_post_recv(target); |
| 918 | spin_unlock_irqrestore(target->scsi_host->host_lock, flags); |
| 919 | |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Must be called with target->scsi_host->host_lock held to protect |
Roland Dreier | 47f2bce | 2005-11-15 00:19:21 -0800 | [diff] [blame] | 925 | * req_lim and tx_head. Lock cannot be dropped between call here and |
| 926 | * call to __srp_post_send(). |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 927 | */ |
| 928 | static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target) |
| 929 | { |
| 930 | if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE) |
| 931 | return NULL; |
| 932 | |
Roland Dreier | 6bfa24f | 2006-06-17 20:37:33 -0700 | [diff] [blame] | 933 | if (unlikely(target->req_lim < 1)) |
| 934 | ++target->zero_req_lim; |
Roland Dreier | 47f2bce | 2005-11-15 00:19:21 -0800 | [diff] [blame] | 935 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 936 | return target->tx_ring[target->tx_head & SRP_SQ_SIZE]; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Must be called with target->scsi_host->host_lock held to protect |
| 941 | * req_lim and tx_head. |
| 942 | */ |
| 943 | static int __srp_post_send(struct srp_target_port *target, |
| 944 | struct srp_iu *iu, int len) |
| 945 | { |
| 946 | struct ib_sge list; |
| 947 | struct ib_send_wr wr, *bad_wr; |
| 948 | int ret = 0; |
| 949 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 950 | list.addr = iu->dma; |
| 951 | list.length = len; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 952 | list.lkey = target->srp_host->dev->mr->lkey; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 953 | |
| 954 | wr.next = NULL; |
| 955 | wr.wr_id = target->tx_head & SRP_SQ_SIZE; |
| 956 | wr.sg_list = &list; |
| 957 | wr.num_sge = 1; |
| 958 | wr.opcode = IB_WR_SEND; |
| 959 | wr.send_flags = IB_SEND_SIGNALED; |
| 960 | |
| 961 | ret = ib_post_send(target->qp, &wr, &bad_wr); |
| 962 | |
| 963 | if (!ret) { |
| 964 | ++target->tx_head; |
| 965 | --target->req_lim; |
| 966 | } |
| 967 | |
| 968 | return ret; |
| 969 | } |
| 970 | |
| 971 | static int srp_queuecommand(struct scsi_cmnd *scmnd, |
| 972 | void (*done)(struct scsi_cmnd *)) |
| 973 | { |
| 974 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
| 975 | struct srp_request *req; |
| 976 | struct srp_iu *iu; |
| 977 | struct srp_cmd *cmd; |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 978 | struct ib_device *dev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 979 | int len; |
| 980 | |
| 981 | if (target->state == SRP_TARGET_CONNECTING) |
| 982 | goto err; |
| 983 | |
| 984 | if (target->state == SRP_TARGET_DEAD || |
| 985 | target->state == SRP_TARGET_REMOVED) { |
| 986 | scmnd->result = DID_BAD_TARGET << 16; |
| 987 | done(scmnd); |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | iu = __srp_get_tx_iu(target); |
| 992 | if (!iu) |
| 993 | goto err; |
| 994 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 995 | dev = target->srp_host->dev->dev; |
| 996 | ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len, |
| 997 | DMA_TO_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 998 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 999 | req = list_entry(target->free_reqs.next, struct srp_request, list); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1000 | |
| 1001 | scmnd->scsi_done = done; |
| 1002 | scmnd->result = 0; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1003 | scmnd->host_scribble = (void *) (long) req->index; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1004 | |
| 1005 | cmd = iu->buf; |
| 1006 | memset(cmd, 0, sizeof *cmd); |
| 1007 | |
| 1008 | cmd->opcode = SRP_CMD; |
| 1009 | cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1010 | cmd->tag = req->index; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1011 | memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len); |
| 1012 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1013 | req->scmnd = scmnd; |
| 1014 | req->cmd = iu; |
| 1015 | req->cmd_done = 0; |
| 1016 | req->tsk_mgmt = NULL; |
| 1017 | |
| 1018 | len = srp_map_data(scmnd, target, req); |
| 1019 | if (len < 0) { |
| 1020 | printk(KERN_ERR PFX "Failed to map data\n"); |
| 1021 | goto err; |
| 1022 | } |
| 1023 | |
| 1024 | if (__srp_post_recv(target)) { |
| 1025 | printk(KERN_ERR PFX "Recv failed\n"); |
| 1026 | goto err_unmap; |
| 1027 | } |
| 1028 | |
Ralph Campbell | 85507bc | 2006-12-12 14:30:55 -0800 | [diff] [blame] | 1029 | ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len, |
| 1030 | DMA_TO_DEVICE); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1031 | |
| 1032 | if (__srp_post_send(target, iu, len)) { |
| 1033 | printk(KERN_ERR PFX "Send failed\n"); |
| 1034 | goto err_unmap; |
| 1035 | } |
| 1036 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1037 | list_move_tail(&req->list, &target->req_queue); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1038 | |
| 1039 | return 0; |
| 1040 | |
| 1041 | err_unmap: |
| 1042 | srp_unmap_data(scmnd, target, req); |
| 1043 | |
| 1044 | err: |
| 1045 | return SCSI_MLQUEUE_HOST_BUSY; |
| 1046 | } |
| 1047 | |
| 1048 | static int srp_alloc_iu_bufs(struct srp_target_port *target) |
| 1049 | { |
| 1050 | int i; |
| 1051 | |
| 1052 | for (i = 0; i < SRP_RQ_SIZE; ++i) { |
| 1053 | target->rx_ring[i] = srp_alloc_iu(target->srp_host, |
| 1054 | target->max_ti_iu_len, |
| 1055 | GFP_KERNEL, DMA_FROM_DEVICE); |
| 1056 | if (!target->rx_ring[i]) |
| 1057 | goto err; |
| 1058 | } |
| 1059 | |
| 1060 | for (i = 0; i < SRP_SQ_SIZE + 1; ++i) { |
| 1061 | target->tx_ring[i] = srp_alloc_iu(target->srp_host, |
Vu Pham | 74b0a15 | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 1062 | srp_max_iu_len, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1063 | GFP_KERNEL, DMA_TO_DEVICE); |
| 1064 | if (!target->tx_ring[i]) |
| 1065 | goto err; |
| 1066 | } |
| 1067 | |
| 1068 | return 0; |
| 1069 | |
| 1070 | err: |
| 1071 | for (i = 0; i < SRP_RQ_SIZE; ++i) { |
| 1072 | srp_free_iu(target->srp_host, target->rx_ring[i]); |
| 1073 | target->rx_ring[i] = NULL; |
| 1074 | } |
| 1075 | |
| 1076 | for (i = 0; i < SRP_SQ_SIZE + 1; ++i) { |
| 1077 | srp_free_iu(target->srp_host, target->tx_ring[i]); |
| 1078 | target->tx_ring[i] = NULL; |
| 1079 | } |
| 1080 | |
| 1081 | return -ENOMEM; |
| 1082 | } |
| 1083 | |
| 1084 | static void srp_cm_rej_handler(struct ib_cm_id *cm_id, |
| 1085 | struct ib_cm_event *event, |
| 1086 | struct srp_target_port *target) |
| 1087 | { |
| 1088 | struct ib_class_port_info *cpi; |
| 1089 | int opcode; |
| 1090 | |
| 1091 | switch (event->param.rej_rcvd.reason) { |
| 1092 | case IB_CM_REJ_PORT_CM_REDIRECT: |
| 1093 | cpi = event->param.rej_rcvd.ari; |
| 1094 | target->path.dlid = cpi->redirect_lid; |
| 1095 | target->path.pkey = cpi->redirect_pkey; |
| 1096 | cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff; |
| 1097 | memcpy(target->path.dgid.raw, cpi->redirect_gid, 16); |
| 1098 | |
| 1099 | target->status = target->path.dlid ? |
| 1100 | SRP_DLID_REDIRECT : SRP_PORT_REDIRECT; |
| 1101 | break; |
| 1102 | |
| 1103 | case IB_CM_REJ_PORT_REDIRECT: |
Roland Dreier | 5d7cbfd | 2007-08-03 10:45:18 -0700 | [diff] [blame] | 1104 | if (srp_target_is_topspin(target)) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1105 | /* |
| 1106 | * Topspin/Cisco SRP gateways incorrectly send |
| 1107 | * reject reason code 25 when they mean 24 |
| 1108 | * (port redirect). |
| 1109 | */ |
| 1110 | memcpy(target->path.dgid.raw, |
| 1111 | event->param.rej_rcvd.ari, 16); |
| 1112 | |
| 1113 | printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n", |
| 1114 | (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix), |
| 1115 | (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id)); |
| 1116 | |
| 1117 | target->status = SRP_PORT_REDIRECT; |
| 1118 | } else { |
| 1119 | printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n"); |
| 1120 | target->status = -ECONNRESET; |
| 1121 | } |
| 1122 | break; |
| 1123 | |
| 1124 | case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: |
| 1125 | printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); |
| 1126 | target->status = -ECONNRESET; |
| 1127 | break; |
| 1128 | |
| 1129 | case IB_CM_REJ_CONSUMER_DEFINED: |
| 1130 | opcode = *(u8 *) event->private_data; |
| 1131 | if (opcode == SRP_LOGIN_REJ) { |
| 1132 | struct srp_login_rej *rej = event->private_data; |
| 1133 | u32 reason = be32_to_cpu(rej->reason); |
| 1134 | |
| 1135 | if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) |
| 1136 | printk(KERN_WARNING PFX |
| 1137 | "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); |
| 1138 | else |
| 1139 | printk(KERN_WARNING PFX |
| 1140 | "SRP LOGIN REJECTED, reason 0x%08x\n", reason); |
| 1141 | } else |
| 1142 | printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED," |
| 1143 | " opcode 0x%02x\n", opcode); |
| 1144 | target->status = -ECONNRESET; |
| 1145 | break; |
| 1146 | |
| 1147 | default: |
| 1148 | printk(KERN_WARNING " REJ reason 0x%x\n", |
| 1149 | event->param.rej_rcvd.reason); |
| 1150 | target->status = -ECONNRESET; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) |
| 1155 | { |
| 1156 | struct srp_target_port *target = cm_id->context; |
| 1157 | struct ib_qp_attr *qp_attr = NULL; |
| 1158 | int attr_mask = 0; |
| 1159 | int comp = 0; |
| 1160 | int opcode = 0; |
| 1161 | |
| 1162 | switch (event->event) { |
| 1163 | case IB_CM_REQ_ERROR: |
| 1164 | printk(KERN_DEBUG PFX "Sending CM REQ failed\n"); |
| 1165 | comp = 1; |
| 1166 | target->status = -ECONNRESET; |
| 1167 | break; |
| 1168 | |
| 1169 | case IB_CM_REP_RECEIVED: |
| 1170 | comp = 1; |
| 1171 | opcode = *(u8 *) event->private_data; |
| 1172 | |
| 1173 | if (opcode == SRP_LOGIN_RSP) { |
| 1174 | struct srp_login_rsp *rsp = event->private_data; |
| 1175 | |
| 1176 | target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len); |
| 1177 | target->req_lim = be32_to_cpu(rsp->req_lim_delta); |
| 1178 | |
| 1179 | target->scsi_host->can_queue = min(target->req_lim, |
| 1180 | target->scsi_host->can_queue); |
| 1181 | } else { |
| 1182 | printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode); |
| 1183 | target->status = -ECONNRESET; |
| 1184 | break; |
| 1185 | } |
| 1186 | |
Vu Pham | d2fcea7 | 2006-11-21 14:14:10 -0800 | [diff] [blame] | 1187 | if (!target->rx_ring[0]) { |
| 1188 | target->status = srp_alloc_iu_bufs(target); |
| 1189 | if (target->status) |
| 1190 | break; |
| 1191 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1192 | |
| 1193 | qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL); |
| 1194 | if (!qp_attr) { |
| 1195 | target->status = -ENOMEM; |
| 1196 | break; |
| 1197 | } |
| 1198 | |
| 1199 | qp_attr->qp_state = IB_QPS_RTR; |
| 1200 | target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); |
| 1201 | if (target->status) |
| 1202 | break; |
| 1203 | |
| 1204 | target->status = ib_modify_qp(target->qp, qp_attr, attr_mask); |
| 1205 | if (target->status) |
| 1206 | break; |
| 1207 | |
| 1208 | target->status = srp_post_recv(target); |
| 1209 | if (target->status) |
| 1210 | break; |
| 1211 | |
| 1212 | qp_attr->qp_state = IB_QPS_RTS; |
| 1213 | target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); |
| 1214 | if (target->status) |
| 1215 | break; |
| 1216 | |
| 1217 | target->status = ib_modify_qp(target->qp, qp_attr, attr_mask); |
| 1218 | if (target->status) |
| 1219 | break; |
| 1220 | |
| 1221 | target->status = ib_send_cm_rtu(cm_id, NULL, 0); |
| 1222 | if (target->status) |
| 1223 | break; |
| 1224 | |
| 1225 | break; |
| 1226 | |
| 1227 | case IB_CM_REJ_RECEIVED: |
| 1228 | printk(KERN_DEBUG PFX "REJ received\n"); |
| 1229 | comp = 1; |
| 1230 | |
| 1231 | srp_cm_rej_handler(cm_id, event, target); |
| 1232 | break; |
| 1233 | |
Ishai Rabinovitz | b7ac4ab | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 1234 | case IB_CM_DREQ_RECEIVED: |
| 1235 | printk(KERN_WARNING PFX "DREQ received - connection closed\n"); |
| 1236 | if (ib_send_cm_drep(cm_id, NULL, 0)) |
| 1237 | printk(KERN_ERR PFX "Sending CM DREP failed\n"); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1238 | break; |
| 1239 | |
| 1240 | case IB_CM_TIMEWAIT_EXIT: |
| 1241 | printk(KERN_ERR PFX "connection closed\n"); |
| 1242 | |
| 1243 | comp = 1; |
| 1244 | target->status = 0; |
| 1245 | break; |
| 1246 | |
Ishai Rabinovitz | b7ac4ab | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 1247 | case IB_CM_MRA_RECEIVED: |
| 1248 | case IB_CM_DREQ_ERROR: |
| 1249 | case IB_CM_DREP_RECEIVED: |
| 1250 | break; |
| 1251 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1252 | default: |
| 1253 | printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event); |
| 1254 | break; |
| 1255 | } |
| 1256 | |
| 1257 | if (comp) |
| 1258 | complete(&target->done); |
| 1259 | |
| 1260 | kfree(qp_attr); |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1265 | static int srp_send_tsk_mgmt(struct srp_target_port *target, |
| 1266 | struct srp_request *req, u8 func) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1267 | { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1268 | struct srp_iu *iu; |
| 1269 | struct srp_tsk_mgmt *tsk_mgmt; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1270 | |
| 1271 | spin_lock_irq(target->scsi_host->host_lock); |
| 1272 | |
Roland Dreier | 1285b3a | 2006-03-03 15:47:25 -0800 | [diff] [blame] | 1273 | if (target->state == SRP_TARGET_DEAD || |
| 1274 | target->state == SRP_TARGET_REMOVED) { |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1275 | req->scmnd->result = DID_BAD_TARGET << 16; |
Roland Dreier | 1285b3a | 2006-03-03 15:47:25 -0800 | [diff] [blame] | 1276 | goto out; |
| 1277 | } |
| 1278 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1279 | init_completion(&req->done); |
| 1280 | |
| 1281 | iu = __srp_get_tx_iu(target); |
| 1282 | if (!iu) |
| 1283 | goto out; |
| 1284 | |
| 1285 | tsk_mgmt = iu->buf; |
| 1286 | memset(tsk_mgmt, 0, sizeof *tsk_mgmt); |
| 1287 | |
| 1288 | tsk_mgmt->opcode = SRP_TSK_MGMT; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1289 | tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48); |
| 1290 | tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1291 | tsk_mgmt->tsk_mgmt_func = func; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1292 | tsk_mgmt->task_tag = req->index; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1293 | |
| 1294 | if (__srp_post_send(target, iu, sizeof *tsk_mgmt)) |
| 1295 | goto out; |
| 1296 | |
| 1297 | req->tsk_mgmt = iu; |
| 1298 | |
| 1299 | spin_unlock_irq(target->scsi_host->host_lock); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1300 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1301 | if (!wait_for_completion_timeout(&req->done, |
| 1302 | msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS))) |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1303 | return -1; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1304 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1305 | return 0; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1306 | |
| 1307 | out: |
| 1308 | spin_unlock_irq(target->scsi_host->host_lock); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1309 | return -1; |
| 1310 | } |
| 1311 | |
| 1312 | static int srp_find_req(struct srp_target_port *target, |
| 1313 | struct scsi_cmnd *scmnd, |
| 1314 | struct srp_request **req) |
| 1315 | { |
| 1316 | if (scmnd->host_scribble == (void *) -1L) |
| 1317 | return -1; |
| 1318 | |
| 1319 | *req = &target->req_ring[(long) scmnd->host_scribble]; |
| 1320 | |
| 1321 | return 0; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | static int srp_abort(struct scsi_cmnd *scmnd) |
| 1325 | { |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1326 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
| 1327 | struct srp_request *req; |
| 1328 | int ret = SUCCESS; |
| 1329 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1330 | printk(KERN_ERR "SRP abort called\n"); |
| 1331 | |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 1332 | if (target->qp_in_error) |
| 1333 | return FAILED; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1334 | if (srp_find_req(target, scmnd, &req)) |
| 1335 | return FAILED; |
| 1336 | if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK)) |
| 1337 | return FAILED; |
| 1338 | |
| 1339 | spin_lock_irq(target->scsi_host->host_lock); |
| 1340 | |
| 1341 | if (req->cmd_done) { |
| 1342 | srp_remove_req(target, req); |
| 1343 | scmnd->scsi_done(scmnd); |
| 1344 | } else if (!req->tsk_status) { |
| 1345 | srp_remove_req(target, req); |
| 1346 | scmnd->result = DID_ABORT << 16; |
| 1347 | } else |
| 1348 | ret = FAILED; |
| 1349 | |
| 1350 | spin_unlock_irq(target->scsi_host->host_lock); |
| 1351 | |
| 1352 | return ret; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | static int srp_reset_device(struct scsi_cmnd *scmnd) |
| 1356 | { |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1357 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
| 1358 | struct srp_request *req, *tmp; |
| 1359 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1360 | printk(KERN_ERR "SRP reset_device called\n"); |
| 1361 | |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 1362 | if (target->qp_in_error) |
| 1363 | return FAILED; |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1364 | if (srp_find_req(target, scmnd, &req)) |
| 1365 | return FAILED; |
| 1366 | if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET)) |
| 1367 | return FAILED; |
| 1368 | if (req->tsk_status) |
| 1369 | return FAILED; |
| 1370 | |
| 1371 | spin_lock_irq(target->scsi_host->host_lock); |
| 1372 | |
| 1373 | list_for_each_entry_safe(req, tmp, &target->req_queue, list) |
Ishai Rabinovitz | 526b4ca | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1374 | if (req->scmnd->device == scmnd->device) |
| 1375 | srp_reset_req(target, req); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1376 | |
| 1377 | spin_unlock_irq(target->scsi_host->host_lock); |
| 1378 | |
| 1379 | return SUCCESS; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | static int srp_reset_host(struct scsi_cmnd *scmnd) |
| 1383 | { |
| 1384 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
| 1385 | int ret = FAILED; |
| 1386 | |
| 1387 | printk(KERN_ERR PFX "SRP reset_host called\n"); |
| 1388 | |
| 1389 | if (!srp_reconnect_target(target)) |
| 1390 | ret = SUCCESS; |
| 1391 | |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1395 | static ssize_t show_id_ext(struct class_device *cdev, char *buf) |
| 1396 | { |
| 1397 | struct srp_target_port *target = host_to_target(class_to_shost(cdev)); |
| 1398 | |
| 1399 | if (target->state == SRP_TARGET_DEAD || |
| 1400 | target->state == SRP_TARGET_REMOVED) |
| 1401 | return -ENODEV; |
| 1402 | |
| 1403 | return sprintf(buf, "0x%016llx\n", |
| 1404 | (unsigned long long) be64_to_cpu(target->id_ext)); |
| 1405 | } |
| 1406 | |
| 1407 | static ssize_t show_ioc_guid(struct class_device *cdev, char *buf) |
| 1408 | { |
| 1409 | struct srp_target_port *target = host_to_target(class_to_shost(cdev)); |
| 1410 | |
| 1411 | if (target->state == SRP_TARGET_DEAD || |
| 1412 | target->state == SRP_TARGET_REMOVED) |
| 1413 | return -ENODEV; |
| 1414 | |
| 1415 | return sprintf(buf, "0x%016llx\n", |
| 1416 | (unsigned long long) be64_to_cpu(target->ioc_guid)); |
| 1417 | } |
| 1418 | |
| 1419 | static ssize_t show_service_id(struct class_device *cdev, char *buf) |
| 1420 | { |
| 1421 | struct srp_target_port *target = host_to_target(class_to_shost(cdev)); |
| 1422 | |
| 1423 | if (target->state == SRP_TARGET_DEAD || |
| 1424 | target->state == SRP_TARGET_REMOVED) |
| 1425 | return -ENODEV; |
| 1426 | |
| 1427 | return sprintf(buf, "0x%016llx\n", |
| 1428 | (unsigned long long) be64_to_cpu(target->service_id)); |
| 1429 | } |
| 1430 | |
| 1431 | static ssize_t show_pkey(struct class_device *cdev, char *buf) |
| 1432 | { |
| 1433 | struct srp_target_port *target = host_to_target(class_to_shost(cdev)); |
| 1434 | |
| 1435 | if (target->state == SRP_TARGET_DEAD || |
| 1436 | target->state == SRP_TARGET_REMOVED) |
| 1437 | return -ENODEV; |
| 1438 | |
| 1439 | return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey)); |
| 1440 | } |
| 1441 | |
| 1442 | static ssize_t show_dgid(struct class_device *cdev, char *buf) |
| 1443 | { |
| 1444 | struct srp_target_port *target = host_to_target(class_to_shost(cdev)); |
| 1445 | |
| 1446 | if (target->state == SRP_TARGET_DEAD || |
| 1447 | target->state == SRP_TARGET_REMOVED) |
| 1448 | return -ENODEV; |
| 1449 | |
| 1450 | return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", |
| 1451 | be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]), |
| 1452 | be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]), |
| 1453 | be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]), |
| 1454 | be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]), |
| 1455 | be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]), |
| 1456 | be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]), |
| 1457 | be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]), |
| 1458 | be16_to_cpu(((__be16 *) target->path.dgid.raw)[7])); |
| 1459 | } |
| 1460 | |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 1461 | static ssize_t show_orig_dgid(struct class_device *cdev, char *buf) |
| 1462 | { |
| 1463 | struct srp_target_port *target = host_to_target(class_to_shost(cdev)); |
| 1464 | |
| 1465 | if (target->state == SRP_TARGET_DEAD || |
| 1466 | target->state == SRP_TARGET_REMOVED) |
| 1467 | return -ENODEV; |
| 1468 | |
| 1469 | return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", |
| 1470 | be16_to_cpu(target->orig_dgid[0]), |
| 1471 | be16_to_cpu(target->orig_dgid[1]), |
| 1472 | be16_to_cpu(target->orig_dgid[2]), |
| 1473 | be16_to_cpu(target->orig_dgid[3]), |
| 1474 | be16_to_cpu(target->orig_dgid[4]), |
| 1475 | be16_to_cpu(target->orig_dgid[5]), |
| 1476 | be16_to_cpu(target->orig_dgid[6]), |
| 1477 | be16_to_cpu(target->orig_dgid[7])); |
| 1478 | } |
| 1479 | |
Roland Dreier | 6bfa24f | 2006-06-17 20:37:33 -0700 | [diff] [blame] | 1480 | static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf) |
| 1481 | { |
| 1482 | struct srp_target_port *target = host_to_target(class_to_shost(cdev)); |
| 1483 | |
| 1484 | if (target->state == SRP_TARGET_DEAD || |
| 1485 | target->state == SRP_TARGET_REMOVED) |
| 1486 | return -ENODEV; |
| 1487 | |
| 1488 | return sprintf(buf, "%d\n", target->zero_req_lim); |
| 1489 | } |
| 1490 | |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 1491 | static ssize_t show_local_ib_port(struct class_device *cdev, char *buf) |
| 1492 | { |
| 1493 | struct srp_target_port *target = host_to_target(class_to_shost(cdev)); |
| 1494 | |
| 1495 | return sprintf(buf, "%d\n", target->srp_host->port); |
| 1496 | } |
| 1497 | |
| 1498 | static ssize_t show_local_ib_device(struct class_device *cdev, char *buf) |
| 1499 | { |
| 1500 | struct srp_target_port *target = host_to_target(class_to_shost(cdev)); |
| 1501 | |
| 1502 | return sprintf(buf, "%s\n", target->srp_host->dev->dev->name); |
| 1503 | } |
| 1504 | |
| 1505 | static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL); |
| 1506 | static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL); |
| 1507 | static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL); |
| 1508 | static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); |
| 1509 | static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL); |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 1510 | static CLASS_DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL); |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 1511 | static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL); |
| 1512 | static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL); |
| 1513 | static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL); |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1514 | |
| 1515 | static struct class_device_attribute *srp_host_attrs[] = { |
| 1516 | &class_device_attr_id_ext, |
| 1517 | &class_device_attr_ioc_guid, |
| 1518 | &class_device_attr_service_id, |
| 1519 | &class_device_attr_pkey, |
| 1520 | &class_device_attr_dgid, |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 1521 | &class_device_attr_orig_dgid, |
Roland Dreier | 6bfa24f | 2006-06-17 20:37:33 -0700 | [diff] [blame] | 1522 | &class_device_attr_zero_req_lim, |
Ishai Rabinovitz | ded7f1a | 2006-08-15 17:34:52 +0300 | [diff] [blame] | 1523 | &class_device_attr_local_ib_port, |
| 1524 | &class_device_attr_local_ib_device, |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1525 | NULL |
| 1526 | }; |
| 1527 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1528 | static struct scsi_host_template srp_template = { |
| 1529 | .module = THIS_MODULE, |
Roland Dreier | b7f008f | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 1530 | .name = "InfiniBand SRP initiator", |
| 1531 | .proc_name = DRV_NAME, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1532 | .info = srp_target_info, |
| 1533 | .queuecommand = srp_queuecommand, |
| 1534 | .eh_abort_handler = srp_abort, |
| 1535 | .eh_device_reset_handler = srp_reset_device, |
| 1536 | .eh_host_reset_handler = srp_reset_host, |
| 1537 | .can_queue = SRP_SQ_SIZE, |
| 1538 | .this_id = -1, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1539 | .cmd_per_lun = SRP_SQ_SIZE, |
Roland Dreier | 6ecb0c8 | 2006-03-20 10:08:23 -0800 | [diff] [blame] | 1540 | .use_clustering = ENABLE_CLUSTERING, |
| 1541 | .shost_attrs = srp_host_attrs |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1542 | }; |
| 1543 | |
| 1544 | static int srp_add_target(struct srp_host *host, struct srp_target_port *target) |
| 1545 | { |
| 1546 | sprintf(target->target_name, "SRP.T10:%016llX", |
| 1547 | (unsigned long long) be64_to_cpu(target->id_ext)); |
| 1548 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1549 | if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device)) |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1550 | return -ENODEV; |
| 1551 | |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 1552 | spin_lock(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1553 | list_add_tail(&target->list, &host->target_list); |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 1554 | spin_unlock(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1555 | |
| 1556 | target->state = SRP_TARGET_LIVE; |
| 1557 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1558 | scsi_scan_target(&target->scsi_host->shost_gendev, |
Matthew Wilcox | 1962a4a | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 1559 | 0, target->scsi_id, SCAN_WILD_CARD, 0); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1560 | |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | static void srp_release_class_dev(struct class_device *class_dev) |
| 1565 | { |
| 1566 | struct srp_host *host = |
| 1567 | container_of(class_dev, struct srp_host, class_dev); |
| 1568 | |
| 1569 | complete(&host->released); |
| 1570 | } |
| 1571 | |
| 1572 | static struct class srp_class = { |
| 1573 | .name = "infiniband_srp", |
| 1574 | .release = srp_release_class_dev |
| 1575 | }; |
| 1576 | |
| 1577 | /* |
| 1578 | * Target ports are added by writing |
| 1579 | * |
| 1580 | * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>, |
| 1581 | * pkey=<P_Key>,service_id=<service ID> |
| 1582 | * |
| 1583 | * to the add_target sysfs attribute. |
| 1584 | */ |
| 1585 | enum { |
| 1586 | SRP_OPT_ERR = 0, |
| 1587 | SRP_OPT_ID_EXT = 1 << 0, |
| 1588 | SRP_OPT_IOC_GUID = 1 << 1, |
| 1589 | SRP_OPT_DGID = 1 << 2, |
| 1590 | SRP_OPT_PKEY = 1 << 3, |
| 1591 | SRP_OPT_SERVICE_ID = 1 << 4, |
| 1592 | SRP_OPT_MAX_SECT = 1 << 5, |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 1593 | SRP_OPT_MAX_CMD_PER_LUN = 1 << 6, |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1594 | SRP_OPT_IO_CLASS = 1 << 7, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 1595 | SRP_OPT_INITIATOR_EXT = 1 << 8, |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1596 | SRP_OPT_ALL = (SRP_OPT_ID_EXT | |
| 1597 | SRP_OPT_IOC_GUID | |
| 1598 | SRP_OPT_DGID | |
| 1599 | SRP_OPT_PKEY | |
| 1600 | SRP_OPT_SERVICE_ID), |
| 1601 | }; |
| 1602 | |
| 1603 | static match_table_t srp_opt_tokens = { |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 1604 | { SRP_OPT_ID_EXT, "id_ext=%s" }, |
| 1605 | { SRP_OPT_IOC_GUID, "ioc_guid=%s" }, |
| 1606 | { SRP_OPT_DGID, "dgid=%s" }, |
| 1607 | { SRP_OPT_PKEY, "pkey=%x" }, |
| 1608 | { SRP_OPT_SERVICE_ID, "service_id=%s" }, |
| 1609 | { SRP_OPT_MAX_SECT, "max_sect=%d" }, |
| 1610 | { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" }, |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1611 | { SRP_OPT_IO_CLASS, "io_class=%x" }, |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 1612 | { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" }, |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 1613 | { SRP_OPT_ERR, NULL } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1614 | }; |
| 1615 | |
| 1616 | static int srp_parse_options(const char *buf, struct srp_target_port *target) |
| 1617 | { |
| 1618 | char *options, *sep_opt; |
| 1619 | char *p; |
| 1620 | char dgid[3]; |
| 1621 | substring_t args[MAX_OPT_ARGS]; |
| 1622 | int opt_mask = 0; |
| 1623 | int token; |
| 1624 | int ret = -EINVAL; |
| 1625 | int i; |
| 1626 | |
| 1627 | options = kstrdup(buf, GFP_KERNEL); |
| 1628 | if (!options) |
| 1629 | return -ENOMEM; |
| 1630 | |
| 1631 | sep_opt = options; |
| 1632 | while ((p = strsep(&sep_opt, ",")) != NULL) { |
| 1633 | if (!*p) |
| 1634 | continue; |
| 1635 | |
| 1636 | token = match_token(p, srp_opt_tokens, args); |
| 1637 | opt_mask |= token; |
| 1638 | |
| 1639 | switch (token) { |
| 1640 | case SRP_OPT_ID_EXT: |
| 1641 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 1642 | if (!p) { |
| 1643 | ret = -ENOMEM; |
| 1644 | goto out; |
| 1645 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1646 | target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 1647 | kfree(p); |
| 1648 | break; |
| 1649 | |
| 1650 | case SRP_OPT_IOC_GUID: |
| 1651 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 1652 | if (!p) { |
| 1653 | ret = -ENOMEM; |
| 1654 | goto out; |
| 1655 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1656 | target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 1657 | kfree(p); |
| 1658 | break; |
| 1659 | |
| 1660 | case SRP_OPT_DGID: |
| 1661 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 1662 | if (!p) { |
| 1663 | ret = -ENOMEM; |
| 1664 | goto out; |
| 1665 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1666 | if (strlen(p) != 32) { |
| 1667 | printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p); |
Roland Dreier | ce1823f | 2006-04-03 09:31:04 -0700 | [diff] [blame] | 1668 | kfree(p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1669 | goto out; |
| 1670 | } |
| 1671 | |
| 1672 | for (i = 0; i < 16; ++i) { |
| 1673 | strlcpy(dgid, p + i * 2, 3); |
| 1674 | target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16); |
| 1675 | } |
Roland Dreier | bf17c1c | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 1676 | kfree(p); |
Ishai Rabinovitz | 3633b3d | 2007-05-06 21:18:11 -0700 | [diff] [blame] | 1677 | memcpy(target->orig_dgid, target->path.dgid.raw, 16); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1678 | break; |
| 1679 | |
| 1680 | case SRP_OPT_PKEY: |
| 1681 | if (match_hex(args, &token)) { |
| 1682 | printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p); |
| 1683 | goto out; |
| 1684 | } |
| 1685 | target->path.pkey = cpu_to_be16(token); |
| 1686 | break; |
| 1687 | |
| 1688 | case SRP_OPT_SERVICE_ID: |
| 1689 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 1690 | if (!p) { |
| 1691 | ret = -ENOMEM; |
| 1692 | goto out; |
| 1693 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1694 | target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 1695 | kfree(p); |
| 1696 | break; |
| 1697 | |
| 1698 | case SRP_OPT_MAX_SECT: |
| 1699 | if (match_int(args, &token)) { |
| 1700 | printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p); |
| 1701 | goto out; |
| 1702 | } |
| 1703 | target->scsi_host->max_sectors = token; |
| 1704 | break; |
| 1705 | |
Vu Pham | 52fb2b50 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 1706 | case SRP_OPT_MAX_CMD_PER_LUN: |
| 1707 | if (match_int(args, &token)) { |
| 1708 | printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p); |
| 1709 | goto out; |
| 1710 | } |
| 1711 | target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE); |
| 1712 | break; |
| 1713 | |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1714 | case SRP_OPT_IO_CLASS: |
| 1715 | if (match_hex(args, &token)) { |
| 1716 | printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p); |
| 1717 | goto out; |
| 1718 | } |
| 1719 | if (token != SRP_REV10_IB_IO_CLASS && |
| 1720 | token != SRP_REV16A_IB_IO_CLASS) { |
| 1721 | printk(KERN_WARNING PFX "unknown IO class parameter value" |
| 1722 | " %x specified (use %x or %x).\n", |
| 1723 | token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS); |
| 1724 | goto out; |
| 1725 | } |
| 1726 | target->io_class = token; |
| 1727 | break; |
| 1728 | |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 1729 | case SRP_OPT_INITIATOR_EXT: |
| 1730 | p = match_strdup(args); |
Ishai Rabinovitz | a20f3a6 | 2007-01-16 17:20:25 +0200 | [diff] [blame] | 1731 | if (!p) { |
| 1732 | ret = -ENOMEM; |
| 1733 | goto out; |
| 1734 | } |
Ishai Rabinovitz | 01cb9bc | 2006-10-04 15:28:56 +0200 | [diff] [blame] | 1735 | target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
| 1736 | kfree(p); |
| 1737 | break; |
| 1738 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1739 | default: |
| 1740 | printk(KERN_WARNING PFX "unknown parameter or missing value " |
| 1741 | "'%s' in target creation request\n", p); |
| 1742 | goto out; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL) |
| 1747 | ret = 0; |
| 1748 | else |
| 1749 | for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i) |
| 1750 | if ((srp_opt_tokens[i].token & SRP_OPT_ALL) && |
| 1751 | !(srp_opt_tokens[i].token & opt_mask)) |
| 1752 | printk(KERN_WARNING PFX "target creation request is " |
| 1753 | "missing parameter '%s'\n", |
| 1754 | srp_opt_tokens[i].pattern); |
| 1755 | |
| 1756 | out: |
| 1757 | kfree(options); |
| 1758 | return ret; |
| 1759 | } |
| 1760 | |
| 1761 | static ssize_t srp_create_target(struct class_device *class_dev, |
| 1762 | const char *buf, size_t count) |
| 1763 | { |
| 1764 | struct srp_host *host = |
| 1765 | container_of(class_dev, struct srp_host, class_dev); |
| 1766 | struct Scsi_Host *target_host; |
| 1767 | struct srp_target_port *target; |
| 1768 | int ret; |
| 1769 | int i; |
| 1770 | |
| 1771 | target_host = scsi_host_alloc(&srp_template, |
| 1772 | sizeof (struct srp_target_port)); |
| 1773 | if (!target_host) |
| 1774 | return -ENOMEM; |
| 1775 | |
Arne Redlich | 3c8edf0 | 2006-11-15 12:43:00 +0100 | [diff] [blame] | 1776 | target_host->max_lun = SRP_MAX_LUN; |
| 1777 | target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb; |
Roland Dreier | 5f06899 | 2005-11-11 14:06:01 -0800 | [diff] [blame] | 1778 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1779 | target = host_to_target(target_host); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1780 | |
Ramachandra K | 0c0450db | 2006-06-17 20:37:38 -0700 | [diff] [blame] | 1781 | target->io_class = SRP_REV16A_IB_IO_CLASS; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1782 | target->scsi_host = target_host; |
| 1783 | target->srp_host = host; |
| 1784 | |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1785 | INIT_LIST_HEAD(&target->free_reqs); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1786 | INIT_LIST_HEAD(&target->req_queue); |
Roland Dreier | d945e1d | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 1787 | for (i = 0; i < SRP_SQ_SIZE; ++i) { |
| 1788 | target->req_ring[i].index = i; |
| 1789 | list_add_tail(&target->req_ring[i].list, &target->free_reqs); |
| 1790 | } |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1791 | |
| 1792 | ret = srp_parse_options(buf, target); |
| 1793 | if (ret) |
| 1794 | goto err; |
| 1795 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1796 | ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1797 | |
| 1798 | printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x " |
| 1799 | "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", |
| 1800 | (unsigned long long) be64_to_cpu(target->id_ext), |
| 1801 | (unsigned long long) be64_to_cpu(target->ioc_guid), |
| 1802 | be16_to_cpu(target->path.pkey), |
| 1803 | (unsigned long long) be64_to_cpu(target->service_id), |
| 1804 | (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]), |
| 1805 | (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]), |
| 1806 | (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]), |
| 1807 | (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]), |
| 1808 | (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]), |
| 1809 | (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]), |
| 1810 | (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]), |
| 1811 | (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14])); |
| 1812 | |
| 1813 | ret = srp_create_target_ib(target); |
| 1814 | if (ret) |
| 1815 | goto err; |
| 1816 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1817 | target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1818 | if (IS_ERR(target->cm_id)) { |
| 1819 | ret = PTR_ERR(target->cm_id); |
| 1820 | goto err_free; |
| 1821 | } |
| 1822 | |
Ishai Rabinovitz | 1033ff6 | 2007-01-16 17:26:22 +0200 | [diff] [blame] | 1823 | target->qp_in_error = 0; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1824 | ret = srp_connect_target(target); |
| 1825 | if (ret) { |
| 1826 | printk(KERN_ERR PFX "Connection failed\n"); |
| 1827 | goto err_cm_id; |
| 1828 | } |
| 1829 | |
| 1830 | ret = srp_add_target(host, target); |
| 1831 | if (ret) |
| 1832 | goto err_disconnect; |
| 1833 | |
| 1834 | return count; |
| 1835 | |
| 1836 | err_disconnect: |
| 1837 | srp_disconnect_target(target); |
| 1838 | |
| 1839 | err_cm_id: |
| 1840 | ib_destroy_cm_id(target->cm_id); |
| 1841 | |
| 1842 | err_free: |
| 1843 | srp_free_target_ib(target); |
| 1844 | |
| 1845 | err: |
| 1846 | scsi_host_put(target_host); |
| 1847 | |
| 1848 | return ret; |
| 1849 | } |
| 1850 | |
| 1851 | static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target); |
| 1852 | |
| 1853 | static ssize_t show_ibdev(struct class_device *class_dev, char *buf) |
| 1854 | { |
| 1855 | struct srp_host *host = |
| 1856 | container_of(class_dev, struct srp_host, class_dev); |
| 1857 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1858 | return sprintf(buf, "%s\n", host->dev->dev->name); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
| 1862 | |
| 1863 | static ssize_t show_port(struct class_device *class_dev, char *buf) |
| 1864 | { |
| 1865 | struct srp_host *host = |
| 1866 | container_of(class_dev, struct srp_host, class_dev); |
| 1867 | |
| 1868 | return sprintf(buf, "%d\n", host->port); |
| 1869 | } |
| 1870 | |
| 1871 | static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL); |
| 1872 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1873 | 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] | 1874 | { |
| 1875 | struct srp_host *host; |
| 1876 | |
| 1877 | host = kzalloc(sizeof *host, GFP_KERNEL); |
| 1878 | if (!host) |
| 1879 | return NULL; |
| 1880 | |
| 1881 | INIT_LIST_HEAD(&host->target_list); |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 1882 | spin_lock_init(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1883 | init_completion(&host->released); |
| 1884 | host->dev = device; |
| 1885 | host->port = port; |
| 1886 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1887 | host->class_dev.class = &srp_class; |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1888 | host->class_dev.dev = device->dev->dma_device; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1889 | snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d", |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1890 | device->dev->name, port); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1891 | |
| 1892 | if (class_device_register(&host->class_dev)) |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1893 | goto free_host; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1894 | if (class_device_create_file(&host->class_dev, &class_device_attr_add_target)) |
| 1895 | goto err_class; |
| 1896 | if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev)) |
| 1897 | goto err_class; |
| 1898 | if (class_device_create_file(&host->class_dev, &class_device_attr_port)) |
| 1899 | goto err_class; |
| 1900 | |
| 1901 | return host; |
| 1902 | |
| 1903 | err_class: |
| 1904 | class_device_unregister(&host->class_dev); |
| 1905 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1906 | free_host: |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1907 | kfree(host); |
| 1908 | |
| 1909 | return NULL; |
| 1910 | } |
| 1911 | |
| 1912 | static void srp_add_one(struct ib_device *device) |
| 1913 | { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1914 | struct srp_device *srp_dev; |
| 1915 | struct ib_device_attr *dev_attr; |
| 1916 | struct ib_fmr_pool_param fmr_param; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1917 | struct srp_host *host; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1918 | int s, e, p; |
| 1919 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1920 | dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL); |
| 1921 | if (!dev_attr) |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 1922 | return; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1923 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1924 | if (ib_query_device(device, dev_attr)) { |
| 1925 | printk(KERN_WARNING PFX "Query device failed for %s\n", |
| 1926 | device->name); |
| 1927 | goto free_attr; |
| 1928 | } |
| 1929 | |
| 1930 | srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL); |
| 1931 | if (!srp_dev) |
| 1932 | goto free_attr; |
| 1933 | |
| 1934 | /* |
| 1935 | * Use the smallest page size supported by the HCA, down to a |
| 1936 | * minimum of 512 bytes (which is the smallest sector that a |
| 1937 | * SCSI command will ever carry). |
| 1938 | */ |
| 1939 | srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1); |
| 1940 | srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift; |
Roland Dreier | bf628dc | 2006-12-15 14:01:49 -0800 | [diff] [blame] | 1941 | srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1); |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1942 | |
| 1943 | INIT_LIST_HEAD(&srp_dev->dev_list); |
| 1944 | |
| 1945 | srp_dev->dev = device; |
| 1946 | srp_dev->pd = ib_alloc_pd(device); |
| 1947 | if (IS_ERR(srp_dev->pd)) |
| 1948 | goto free_dev; |
| 1949 | |
| 1950 | srp_dev->mr = ib_get_dma_mr(srp_dev->pd, |
| 1951 | IB_ACCESS_LOCAL_WRITE | |
| 1952 | IB_ACCESS_REMOTE_READ | |
| 1953 | IB_ACCESS_REMOTE_WRITE); |
| 1954 | if (IS_ERR(srp_dev->mr)) |
| 1955 | goto err_pd; |
| 1956 | |
| 1957 | memset(&fmr_param, 0, sizeof fmr_param); |
| 1958 | fmr_param.pool_size = SRP_FMR_POOL_SIZE; |
| 1959 | fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE; |
| 1960 | fmr_param.cache = 1; |
| 1961 | fmr_param.max_pages_per_fmr = SRP_FMR_SIZE; |
| 1962 | fmr_param.page_shift = srp_dev->fmr_page_shift; |
| 1963 | fmr_param.access = (IB_ACCESS_LOCAL_WRITE | |
| 1964 | IB_ACCESS_REMOTE_WRITE | |
| 1965 | IB_ACCESS_REMOTE_READ); |
| 1966 | |
| 1967 | srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param); |
| 1968 | if (IS_ERR(srp_dev->fmr_pool)) |
| 1969 | srp_dev->fmr_pool = NULL; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1970 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1971 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1972 | s = 0; |
| 1973 | e = 0; |
| 1974 | } else { |
| 1975 | s = 1; |
| 1976 | e = device->phys_port_cnt; |
| 1977 | } |
| 1978 | |
| 1979 | for (p = s; p <= e; ++p) { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1980 | host = srp_add_port(srp_dev, p); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1981 | if (host) |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1982 | list_add_tail(&host->list, &srp_dev->dev_list); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1983 | } |
| 1984 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1985 | ib_set_client_data(device, &srp_client, srp_dev); |
| 1986 | |
| 1987 | goto free_attr; |
| 1988 | |
| 1989 | err_pd: |
| 1990 | ib_dealloc_pd(srp_dev->pd); |
| 1991 | |
| 1992 | free_dev: |
| 1993 | kfree(srp_dev); |
| 1994 | |
| 1995 | free_attr: |
| 1996 | kfree(dev_attr); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | static void srp_remove_one(struct ib_device *device) |
| 2000 | { |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2001 | struct srp_device *srp_dev; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2002 | struct srp_host *host, *tmp_host; |
| 2003 | LIST_HEAD(target_list); |
| 2004 | struct srp_target_port *target, *tmp_target; |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2005 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2006 | srp_dev = ib_get_client_data(device, &srp_client); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2007 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2008 | list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) { |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2009 | class_device_unregister(&host->class_dev); |
| 2010 | /* |
| 2011 | * Wait for the sysfs entry to go away, so that no new |
| 2012 | * target ports can be created. |
| 2013 | */ |
| 2014 | wait_for_completion(&host->released); |
| 2015 | |
| 2016 | /* |
| 2017 | * Mark all target ports as removed, so we stop queueing |
| 2018 | * commands and don't try to reconnect. |
| 2019 | */ |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2020 | spin_lock(&host->target_lock); |
Matthew Wilcox | 549c5fc2 | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2021 | list_for_each_entry(target, &host->target_list, list) { |
Ishai Rabinovitz | 0c5b395 | 2006-06-17 20:37:31 -0700 | [diff] [blame] | 2022 | spin_lock_irq(target->scsi_host->host_lock); |
| 2023 | target->state = SRP_TARGET_REMOVED; |
| 2024 | spin_unlock_irq(target->scsi_host->host_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2025 | } |
Matthew Wilcox | b3589fd | 2006-06-17 20:37:30 -0700 | [diff] [blame] | 2026 | spin_unlock(&host->target_lock); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2027 | |
| 2028 | /* |
| 2029 | * Wait for any reconnection tasks that may have |
| 2030 | * started before we marked our target ports as |
| 2031 | * removed, and any target port removal tasks. |
| 2032 | */ |
| 2033 | flush_scheduled_work(); |
| 2034 | |
| 2035 | list_for_each_entry_safe(target, tmp_target, |
| 2036 | &host->target_list, list) { |
| 2037 | scsi_remove_host(target->scsi_host); |
| 2038 | srp_disconnect_target(target); |
| 2039 | ib_destroy_cm_id(target->cm_id); |
| 2040 | srp_free_target_ib(target); |
| 2041 | scsi_host_put(target->scsi_host); |
| 2042 | } |
| 2043 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2044 | kfree(host); |
| 2045 | } |
| 2046 | |
Roland Dreier | f5358a1 | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2047 | if (srp_dev->fmr_pool) |
| 2048 | ib_destroy_fmr_pool(srp_dev->fmr_pool); |
| 2049 | ib_dereg_mr(srp_dev->mr); |
| 2050 | ib_dealloc_pd(srp_dev->pd); |
| 2051 | |
| 2052 | kfree(srp_dev); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | static int __init srp_init_module(void) |
| 2056 | { |
| 2057 | int ret; |
| 2058 | |
Vu Pham | 74b0a15 | 2006-06-17 20:37:32 -0700 | [diff] [blame] | 2059 | srp_template.sg_tablesize = srp_sg_tablesize; |
| 2060 | srp_max_iu_len = (sizeof (struct srp_cmd) + |
| 2061 | sizeof (struct srp_indirect_buf) + |
| 2062 | srp_sg_tablesize * 16); |
| 2063 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2064 | ret = class_register(&srp_class); |
| 2065 | if (ret) { |
| 2066 | printk(KERN_ERR PFX "couldn't register class infiniband_srp\n"); |
| 2067 | return ret; |
| 2068 | } |
| 2069 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 2070 | ib_sa_register_client(&srp_sa_client); |
| 2071 | |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2072 | ret = ib_register_client(&srp_client); |
| 2073 | if (ret) { |
| 2074 | printk(KERN_ERR PFX "couldn't register IB client\n"); |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 2075 | ib_sa_unregister_client(&srp_sa_client); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2076 | class_unregister(&srp_class); |
| 2077 | return ret; |
| 2078 | } |
| 2079 | |
| 2080 | return 0; |
| 2081 | } |
| 2082 | |
| 2083 | static void __exit srp_cleanup_module(void) |
| 2084 | { |
| 2085 | ib_unregister_client(&srp_client); |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 2086 | ib_sa_unregister_client(&srp_sa_client); |
Roland Dreier | aef9ec3 | 2005-11-02 14:07:13 -0800 | [diff] [blame] | 2087 | class_unregister(&srp_class); |
| 2088 | } |
| 2089 | |
| 2090 | module_init(srp_init_module); |
| 2091 | module_exit(srp_cleanup_module); |