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