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