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