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