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 33aef9ec39SRoland Dreier #include <linux/module.h> 34aef9ec39SRoland Dreier #include <linux/init.h> 35aef9ec39SRoland Dreier #include <linux/slab.h> 36aef9ec39SRoland Dreier #include <linux/err.h> 37aef9ec39SRoland Dreier #include <linux/string.h> 38aef9ec39SRoland Dreier #include <linux/parser.h> 39aef9ec39SRoland Dreier #include <linux/random.h> 40de25968cSTim Schmielau #include <linux/jiffies.h> 41aef9ec39SRoland Dreier 42aef9ec39SRoland Dreier #include <asm/atomic.h> 43aef9ec39SRoland Dreier 44aef9ec39SRoland Dreier #include <scsi/scsi.h> 45aef9ec39SRoland Dreier #include <scsi/scsi_device.h> 46aef9ec39SRoland Dreier #include <scsi/scsi_dbg.h> 47aef9ec39SRoland Dreier #include <scsi/srp.h> 483236822bSFUJITA Tomonori #include <scsi/scsi_transport_srp.h> 49aef9ec39SRoland Dreier 50aef9ec39SRoland Dreier #include "ib_srp.h" 51aef9ec39SRoland Dreier 52aef9ec39SRoland Dreier #define DRV_NAME "ib_srp" 53aef9ec39SRoland Dreier #define PFX DRV_NAME ": " 54aef9ec39SRoland Dreier #define DRV_VERSION "0.2" 55aef9ec39SRoland Dreier #define DRV_RELDATE "November 1, 2005" 56aef9ec39SRoland Dreier 57aef9ec39SRoland Dreier MODULE_AUTHOR("Roland Dreier"); 58aef9ec39SRoland Dreier MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator " 59aef9ec39SRoland Dreier "v" DRV_VERSION " (" DRV_RELDATE ")"); 60aef9ec39SRoland Dreier MODULE_LICENSE("Dual BSD/GPL"); 61aef9ec39SRoland Dreier 6274b0a15bSVu Pham static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE; 6374b0a15bSVu Pham static int srp_max_iu_len; 6474b0a15bSVu Pham 6574b0a15bSVu Pham module_param(srp_sg_tablesize, int, 0444); 6674b0a15bSVu Pham MODULE_PARM_DESC(srp_sg_tablesize, 671e89a194SDavid Dillow "Max number of gather/scatter entries per I/O (default is 12, max 255)"); 6874b0a15bSVu Pham 69aef9ec39SRoland Dreier static int topspin_workarounds = 1; 70aef9ec39SRoland Dreier 71aef9ec39SRoland Dreier module_param(topspin_workarounds, int, 0444); 72aef9ec39SRoland Dreier MODULE_PARM_DESC(topspin_workarounds, 73aef9ec39SRoland Dreier "Enable workarounds for Topspin/Cisco SRP target bugs if != 0"); 74aef9ec39SRoland Dreier 75559ce8f1SIshai Rabinovitz static int mellanox_workarounds = 1; 76559ce8f1SIshai Rabinovitz 77559ce8f1SIshai Rabinovitz module_param(mellanox_workarounds, int, 0444); 78559ce8f1SIshai Rabinovitz MODULE_PARM_DESC(mellanox_workarounds, 79559ce8f1SIshai Rabinovitz "Enable workarounds for Mellanox SRP target bugs if != 0"); 80559ce8f1SIshai Rabinovitz 81aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device); 82aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device); 839c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr); 849c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr); 85aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event); 86aef9ec39SRoland Dreier 873236822bSFUJITA Tomonori static struct scsi_transport_template *ib_srp_transport_template; 883236822bSFUJITA Tomonori 89aef9ec39SRoland Dreier static struct ib_client srp_client = { 90aef9ec39SRoland Dreier .name = "srp", 91aef9ec39SRoland Dreier .add = srp_add_one, 92aef9ec39SRoland Dreier .remove = srp_remove_one 93aef9ec39SRoland Dreier }; 94aef9ec39SRoland Dreier 95c1a0b23bSMichael S. Tsirkin static struct ib_sa_client srp_sa_client; 96c1a0b23bSMichael S. Tsirkin 97aef9ec39SRoland Dreier static inline struct srp_target_port *host_to_target(struct Scsi_Host *host) 98aef9ec39SRoland Dreier { 99aef9ec39SRoland Dreier return (struct srp_target_port *) host->hostdata; 100aef9ec39SRoland Dreier } 101aef9ec39SRoland Dreier 102aef9ec39SRoland Dreier static const char *srp_target_info(struct Scsi_Host *host) 103aef9ec39SRoland Dreier { 104aef9ec39SRoland Dreier return host_to_target(host)->target_name; 105aef9ec39SRoland Dreier } 106aef9ec39SRoland Dreier 1075d7cbfd6SRoland Dreier static int srp_target_is_topspin(struct srp_target_port *target) 1085d7cbfd6SRoland Dreier { 1095d7cbfd6SRoland Dreier static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad }; 1103d1ff48dSRaghava Kondapalli static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d }; 1115d7cbfd6SRoland Dreier 1125d7cbfd6SRoland Dreier return topspin_workarounds && 1133d1ff48dSRaghava Kondapalli (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) || 1143d1ff48dSRaghava Kondapalli !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui)); 1155d7cbfd6SRoland Dreier } 1165d7cbfd6SRoland Dreier 1175d7cbfd6SRoland Dreier static int srp_target_is_mellanox(struct srp_target_port *target) 1185d7cbfd6SRoland Dreier { 1195d7cbfd6SRoland Dreier static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 }; 1205d7cbfd6SRoland Dreier 1215d7cbfd6SRoland Dreier return mellanox_workarounds && 1225d7cbfd6SRoland Dreier !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui); 1235d7cbfd6SRoland Dreier } 1245d7cbfd6SRoland Dreier 125aef9ec39SRoland Dreier static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size, 126aef9ec39SRoland Dreier gfp_t gfp_mask, 127aef9ec39SRoland Dreier enum dma_data_direction direction) 128aef9ec39SRoland Dreier { 129aef9ec39SRoland Dreier struct srp_iu *iu; 130aef9ec39SRoland Dreier 131aef9ec39SRoland Dreier iu = kmalloc(sizeof *iu, gfp_mask); 132aef9ec39SRoland Dreier if (!iu) 133aef9ec39SRoland Dreier goto out; 134aef9ec39SRoland Dreier 135aef9ec39SRoland Dreier iu->buf = kzalloc(size, gfp_mask); 136aef9ec39SRoland Dreier if (!iu->buf) 137aef9ec39SRoland Dreier goto out_free_iu; 138aef9ec39SRoland Dreier 13905321937SGreg Kroah-Hartman iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size, 14005321937SGreg Kroah-Hartman direction); 14105321937SGreg Kroah-Hartman if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma)) 142aef9ec39SRoland Dreier goto out_free_buf; 143aef9ec39SRoland Dreier 144aef9ec39SRoland Dreier iu->size = size; 145aef9ec39SRoland Dreier iu->direction = direction; 146aef9ec39SRoland Dreier 147aef9ec39SRoland Dreier return iu; 148aef9ec39SRoland Dreier 149aef9ec39SRoland Dreier out_free_buf: 150aef9ec39SRoland Dreier kfree(iu->buf); 151aef9ec39SRoland Dreier out_free_iu: 152aef9ec39SRoland Dreier kfree(iu); 153aef9ec39SRoland Dreier out: 154aef9ec39SRoland Dreier return NULL; 155aef9ec39SRoland Dreier } 156aef9ec39SRoland Dreier 157aef9ec39SRoland Dreier static void srp_free_iu(struct srp_host *host, struct srp_iu *iu) 158aef9ec39SRoland Dreier { 159aef9ec39SRoland Dreier if (!iu) 160aef9ec39SRoland Dreier return; 161aef9ec39SRoland Dreier 16205321937SGreg Kroah-Hartman ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size, 16305321937SGreg Kroah-Hartman iu->direction); 164aef9ec39SRoland Dreier kfree(iu->buf); 165aef9ec39SRoland Dreier kfree(iu); 166aef9ec39SRoland Dreier } 167aef9ec39SRoland Dreier 168aef9ec39SRoland Dreier static void srp_qp_event(struct ib_event *event, void *context) 169aef9ec39SRoland Dreier { 170aef9ec39SRoland Dreier printk(KERN_ERR PFX "QP event %d\n", event->event); 171aef9ec39SRoland Dreier } 172aef9ec39SRoland Dreier 173aef9ec39SRoland Dreier static int srp_init_qp(struct srp_target_port *target, 174aef9ec39SRoland Dreier struct ib_qp *qp) 175aef9ec39SRoland Dreier { 176aef9ec39SRoland Dreier struct ib_qp_attr *attr; 177aef9ec39SRoland Dreier int ret; 178aef9ec39SRoland Dreier 179aef9ec39SRoland Dreier attr = kmalloc(sizeof *attr, GFP_KERNEL); 180aef9ec39SRoland Dreier if (!attr) 181aef9ec39SRoland Dreier return -ENOMEM; 182aef9ec39SRoland Dreier 183969a60f9SRoland Dreier ret = ib_find_pkey(target->srp_host->srp_dev->dev, 184aef9ec39SRoland Dreier target->srp_host->port, 185aef9ec39SRoland Dreier be16_to_cpu(target->path.pkey), 186aef9ec39SRoland Dreier &attr->pkey_index); 187aef9ec39SRoland Dreier if (ret) 188aef9ec39SRoland Dreier goto out; 189aef9ec39SRoland Dreier 190aef9ec39SRoland Dreier attr->qp_state = IB_QPS_INIT; 191aef9ec39SRoland Dreier attr->qp_access_flags = (IB_ACCESS_REMOTE_READ | 192aef9ec39SRoland Dreier IB_ACCESS_REMOTE_WRITE); 193aef9ec39SRoland Dreier attr->port_num = target->srp_host->port; 194aef9ec39SRoland Dreier 195aef9ec39SRoland Dreier ret = ib_modify_qp(qp, attr, 196aef9ec39SRoland Dreier IB_QP_STATE | 197aef9ec39SRoland Dreier IB_QP_PKEY_INDEX | 198aef9ec39SRoland Dreier IB_QP_ACCESS_FLAGS | 199aef9ec39SRoland Dreier IB_QP_PORT); 200aef9ec39SRoland Dreier 201aef9ec39SRoland Dreier out: 202aef9ec39SRoland Dreier kfree(attr); 203aef9ec39SRoland Dreier return ret; 204aef9ec39SRoland Dreier } 205aef9ec39SRoland Dreier 2069fe4bcf4SDavid Dillow static int srp_new_cm_id(struct srp_target_port *target) 2079fe4bcf4SDavid Dillow { 2089fe4bcf4SDavid Dillow struct ib_cm_id *new_cm_id; 2099fe4bcf4SDavid Dillow 21005321937SGreg Kroah-Hartman new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev, 2119fe4bcf4SDavid Dillow srp_cm_handler, target); 2129fe4bcf4SDavid Dillow if (IS_ERR(new_cm_id)) 2139fe4bcf4SDavid Dillow return PTR_ERR(new_cm_id); 2149fe4bcf4SDavid Dillow 2159fe4bcf4SDavid Dillow if (target->cm_id) 2169fe4bcf4SDavid Dillow ib_destroy_cm_id(target->cm_id); 2179fe4bcf4SDavid Dillow target->cm_id = new_cm_id; 2189fe4bcf4SDavid Dillow 2199fe4bcf4SDavid Dillow return 0; 2209fe4bcf4SDavid Dillow } 2219fe4bcf4SDavid Dillow 222aef9ec39SRoland Dreier static int srp_create_target_ib(struct srp_target_port *target) 223aef9ec39SRoland Dreier { 224aef9ec39SRoland Dreier struct ib_qp_init_attr *init_attr; 225aef9ec39SRoland Dreier int ret; 226aef9ec39SRoland Dreier 227aef9ec39SRoland Dreier init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL); 228aef9ec39SRoland Dreier if (!init_attr) 229aef9ec39SRoland Dreier return -ENOMEM; 230aef9ec39SRoland Dreier 2319c03dc9fSBart Van Assche target->recv_cq = ib_create_cq(target->srp_host->srp_dev->dev, 2329c03dc9fSBart Van Assche srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0); 2339c03dc9fSBart Van Assche if (IS_ERR(target->recv_cq)) { 2349c03dc9fSBart Van Assche ret = PTR_ERR(target->recv_cq); 235da9d2f07SRoland Dreier goto err; 236aef9ec39SRoland Dreier } 237aef9ec39SRoland Dreier 2389c03dc9fSBart Van Assche target->send_cq = ib_create_cq(target->srp_host->srp_dev->dev, 2399c03dc9fSBart Van Assche srp_send_completion, NULL, target, SRP_SQ_SIZE, 0); 2409c03dc9fSBart Van Assche if (IS_ERR(target->send_cq)) { 2419c03dc9fSBart Van Assche ret = PTR_ERR(target->send_cq); 242da9d2f07SRoland Dreier goto err_recv_cq; 2439c03dc9fSBart Van Assche } 2449c03dc9fSBart Van Assche 2459c03dc9fSBart Van Assche ib_req_notify_cq(target->recv_cq, IB_CQ_NEXT_COMP); 246aef9ec39SRoland Dreier 247aef9ec39SRoland Dreier init_attr->event_handler = srp_qp_event; 248aef9ec39SRoland Dreier init_attr->cap.max_send_wr = SRP_SQ_SIZE; 249aef9ec39SRoland Dreier init_attr->cap.max_recv_wr = SRP_RQ_SIZE; 250aef9ec39SRoland Dreier init_attr->cap.max_recv_sge = 1; 251aef9ec39SRoland Dreier init_attr->cap.max_send_sge = 1; 252aef9ec39SRoland Dreier init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 253aef9ec39SRoland Dreier init_attr->qp_type = IB_QPT_RC; 2549c03dc9fSBart Van Assche init_attr->send_cq = target->send_cq; 2559c03dc9fSBart Van Assche init_attr->recv_cq = target->recv_cq; 256aef9ec39SRoland Dreier 25705321937SGreg Kroah-Hartman target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr); 258aef9ec39SRoland Dreier if (IS_ERR(target->qp)) { 259aef9ec39SRoland Dreier ret = PTR_ERR(target->qp); 260da9d2f07SRoland Dreier goto err_send_cq; 261aef9ec39SRoland Dreier } 262aef9ec39SRoland Dreier 263aef9ec39SRoland Dreier ret = srp_init_qp(target, target->qp); 264da9d2f07SRoland Dreier if (ret) 265da9d2f07SRoland Dreier goto err_qp; 266aef9ec39SRoland Dreier 267da9d2f07SRoland Dreier kfree(init_attr); 268da9d2f07SRoland Dreier return 0; 269da9d2f07SRoland Dreier 270da9d2f07SRoland Dreier err_qp: 271da9d2f07SRoland Dreier ib_destroy_qp(target->qp); 272da9d2f07SRoland Dreier 273da9d2f07SRoland Dreier err_send_cq: 274da9d2f07SRoland Dreier ib_destroy_cq(target->send_cq); 275da9d2f07SRoland Dreier 276da9d2f07SRoland Dreier err_recv_cq: 277da9d2f07SRoland Dreier ib_destroy_cq(target->recv_cq); 278da9d2f07SRoland Dreier 279da9d2f07SRoland Dreier err: 280aef9ec39SRoland Dreier kfree(init_attr); 281aef9ec39SRoland Dreier return ret; 282aef9ec39SRoland Dreier } 283aef9ec39SRoland Dreier 284aef9ec39SRoland Dreier static void srp_free_target_ib(struct srp_target_port *target) 285aef9ec39SRoland Dreier { 286aef9ec39SRoland Dreier int i; 287aef9ec39SRoland Dreier 288aef9ec39SRoland Dreier ib_destroy_qp(target->qp); 2899c03dc9fSBart Van Assche ib_destroy_cq(target->send_cq); 2909c03dc9fSBart Van Assche ib_destroy_cq(target->recv_cq); 291aef9ec39SRoland Dreier 292aef9ec39SRoland Dreier for (i = 0; i < SRP_RQ_SIZE; ++i) 293aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->rx_ring[i]); 294dd5e6e38SBart Van Assche for (i = 0; i < SRP_SQ_SIZE; ++i) 295aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->tx_ring[i]); 296aef9ec39SRoland Dreier } 297aef9ec39SRoland Dreier 298aef9ec39SRoland Dreier static void srp_path_rec_completion(int status, 299aef9ec39SRoland Dreier struct ib_sa_path_rec *pathrec, 300aef9ec39SRoland Dreier void *target_ptr) 301aef9ec39SRoland Dreier { 302aef9ec39SRoland Dreier struct srp_target_port *target = target_ptr; 303aef9ec39SRoland Dreier 304aef9ec39SRoland Dreier target->status = status; 305aef9ec39SRoland Dreier if (status) 3067aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 3077aa54bd7SDavid Dillow PFX "Got failed path rec status %d\n", status); 308aef9ec39SRoland Dreier else 309aef9ec39SRoland Dreier target->path = *pathrec; 310aef9ec39SRoland Dreier complete(&target->done); 311aef9ec39SRoland Dreier } 312aef9ec39SRoland Dreier 313aef9ec39SRoland Dreier static int srp_lookup_path(struct srp_target_port *target) 314aef9ec39SRoland Dreier { 315aef9ec39SRoland Dreier target->path.numb_path = 1; 316aef9ec39SRoland Dreier 317aef9ec39SRoland Dreier init_completion(&target->done); 318aef9ec39SRoland Dreier 319c1a0b23bSMichael S. Tsirkin target->path_query_id = ib_sa_path_rec_get(&srp_sa_client, 32005321937SGreg Kroah-Hartman target->srp_host->srp_dev->dev, 321aef9ec39SRoland Dreier target->srp_host->port, 322aef9ec39SRoland Dreier &target->path, 323247e020eSSean Hefty IB_SA_PATH_REC_SERVICE_ID | 324aef9ec39SRoland Dreier IB_SA_PATH_REC_DGID | 325aef9ec39SRoland Dreier IB_SA_PATH_REC_SGID | 326aef9ec39SRoland Dreier IB_SA_PATH_REC_NUMB_PATH | 327aef9ec39SRoland Dreier IB_SA_PATH_REC_PKEY, 328aef9ec39SRoland Dreier SRP_PATH_REC_TIMEOUT_MS, 329aef9ec39SRoland Dreier GFP_KERNEL, 330aef9ec39SRoland Dreier srp_path_rec_completion, 331aef9ec39SRoland Dreier target, &target->path_query); 332aef9ec39SRoland Dreier if (target->path_query_id < 0) 333aef9ec39SRoland Dreier return target->path_query_id; 334aef9ec39SRoland Dreier 335aef9ec39SRoland Dreier wait_for_completion(&target->done); 336aef9ec39SRoland Dreier 337aef9ec39SRoland Dreier if (target->status < 0) 3387aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 3397aa54bd7SDavid Dillow PFX "Path record query failed\n"); 340aef9ec39SRoland Dreier 341aef9ec39SRoland Dreier return target->status; 342aef9ec39SRoland Dreier } 343aef9ec39SRoland Dreier 344aef9ec39SRoland Dreier static int srp_send_req(struct srp_target_port *target) 345aef9ec39SRoland Dreier { 346aef9ec39SRoland Dreier struct { 347aef9ec39SRoland Dreier struct ib_cm_req_param param; 348aef9ec39SRoland Dreier struct srp_login_req priv; 349aef9ec39SRoland Dreier } *req = NULL; 350aef9ec39SRoland Dreier int status; 351aef9ec39SRoland Dreier 352aef9ec39SRoland Dreier req = kzalloc(sizeof *req, GFP_KERNEL); 353aef9ec39SRoland Dreier if (!req) 354aef9ec39SRoland Dreier return -ENOMEM; 355aef9ec39SRoland Dreier 356aef9ec39SRoland Dreier req->param.primary_path = &target->path; 357aef9ec39SRoland Dreier req->param.alternate_path = NULL; 358aef9ec39SRoland Dreier req->param.service_id = target->service_id; 359aef9ec39SRoland Dreier req->param.qp_num = target->qp->qp_num; 360aef9ec39SRoland Dreier req->param.qp_type = target->qp->qp_type; 361aef9ec39SRoland Dreier req->param.private_data = &req->priv; 362aef9ec39SRoland Dreier req->param.private_data_len = sizeof req->priv; 363aef9ec39SRoland Dreier req->param.flow_control = 1; 364aef9ec39SRoland Dreier 365aef9ec39SRoland Dreier get_random_bytes(&req->param.starting_psn, 4); 366aef9ec39SRoland Dreier req->param.starting_psn &= 0xffffff; 367aef9ec39SRoland Dreier 368aef9ec39SRoland Dreier /* 369aef9ec39SRoland Dreier * Pick some arbitrary defaults here; we could make these 370aef9ec39SRoland Dreier * module parameters if anyone cared about setting them. 371aef9ec39SRoland Dreier */ 372aef9ec39SRoland Dreier req->param.responder_resources = 4; 373aef9ec39SRoland Dreier req->param.remote_cm_response_timeout = 20; 374aef9ec39SRoland Dreier req->param.local_cm_response_timeout = 20; 375aef9ec39SRoland Dreier req->param.retry_count = 7; 376aef9ec39SRoland Dreier req->param.rnr_retry_count = 7; 377aef9ec39SRoland Dreier req->param.max_cm_retries = 15; 378aef9ec39SRoland Dreier 379aef9ec39SRoland Dreier req->priv.opcode = SRP_LOGIN_REQ; 380aef9ec39SRoland Dreier req->priv.tag = 0; 38174b0a15bSVu Pham req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len); 382aef9ec39SRoland Dreier req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | 383aef9ec39SRoland Dreier SRP_BUF_FORMAT_INDIRECT); 3840c0450dbSRamachandra K /* 3850c0450dbSRamachandra K * In the published SRP specification (draft rev. 16a), the 3860c0450dbSRamachandra K * port identifier format is 8 bytes of ID extension followed 3870c0450dbSRamachandra K * by 8 bytes of GUID. Older drafts put the two halves in the 3880c0450dbSRamachandra K * opposite order, so that the GUID comes first. 3890c0450dbSRamachandra K * 3900c0450dbSRamachandra K * Targets conforming to these obsolete drafts can be 3910c0450dbSRamachandra K * recognized by the I/O Class they report. 3920c0450dbSRamachandra K */ 3930c0450dbSRamachandra K if (target->io_class == SRP_REV10_IB_IO_CLASS) { 3940c0450dbSRamachandra K memcpy(req->priv.initiator_port_id, 39501cb9bcbSIshai Rabinovitz &target->path.sgid.global.interface_id, 8); 3960c0450dbSRamachandra K memcpy(req->priv.initiator_port_id + 8, 39701cb9bcbSIshai Rabinovitz &target->initiator_ext, 8); 3980c0450dbSRamachandra K memcpy(req->priv.target_port_id, &target->ioc_guid, 8); 3990c0450dbSRamachandra K memcpy(req->priv.target_port_id + 8, &target->id_ext, 8); 4000c0450dbSRamachandra K } else { 4010c0450dbSRamachandra K memcpy(req->priv.initiator_port_id, 40201cb9bcbSIshai Rabinovitz &target->initiator_ext, 8); 40301cb9bcbSIshai Rabinovitz memcpy(req->priv.initiator_port_id + 8, 40401cb9bcbSIshai Rabinovitz &target->path.sgid.global.interface_id, 8); 4050c0450dbSRamachandra K memcpy(req->priv.target_port_id, &target->id_ext, 8); 4060c0450dbSRamachandra K memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8); 4070c0450dbSRamachandra K } 4080c0450dbSRamachandra K 409aef9ec39SRoland Dreier /* 410aef9ec39SRoland Dreier * Topspin/Cisco SRP targets will reject our login unless we 41101cb9bcbSIshai Rabinovitz * zero out the first 8 bytes of our initiator port ID and set 41201cb9bcbSIshai Rabinovitz * the second 8 bytes to the local node GUID. 413aef9ec39SRoland Dreier */ 4145d7cbfd6SRoland Dreier if (srp_target_is_topspin(target)) { 4157aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, 4167aa54bd7SDavid Dillow PFX "Topspin/Cisco initiator port ID workaround " 417aef9ec39SRoland Dreier "activated for target GUID %016llx\n", 418aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->ioc_guid)); 419aef9ec39SRoland Dreier memset(req->priv.initiator_port_id, 0, 8); 42001cb9bcbSIshai Rabinovitz memcpy(req->priv.initiator_port_id + 8, 42105321937SGreg Kroah-Hartman &target->srp_host->srp_dev->dev->node_guid, 8); 422aef9ec39SRoland Dreier } 423aef9ec39SRoland Dreier 424aef9ec39SRoland Dreier status = ib_send_cm_req(target->cm_id, &req->param); 425aef9ec39SRoland Dreier 426aef9ec39SRoland Dreier kfree(req); 427aef9ec39SRoland Dreier 428aef9ec39SRoland Dreier return status; 429aef9ec39SRoland Dreier } 430aef9ec39SRoland Dreier 431aef9ec39SRoland Dreier static void srp_disconnect_target(struct srp_target_port *target) 432aef9ec39SRoland Dreier { 433aef9ec39SRoland Dreier /* XXX should send SRP_I_LOGOUT request */ 434aef9ec39SRoland Dreier 435aef9ec39SRoland Dreier init_completion(&target->done); 436e6581056SRoland Dreier if (ib_send_cm_dreq(target->cm_id, NULL, 0)) { 4377aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, 4387aa54bd7SDavid Dillow PFX "Sending CM DREQ failed\n"); 439e6581056SRoland Dreier return; 440e6581056SRoland Dreier } 441aef9ec39SRoland Dreier wait_for_completion(&target->done); 442aef9ec39SRoland Dreier } 443aef9ec39SRoland Dreier 4449709f0e0SBart Van Assche static bool srp_change_state(struct srp_target_port *target, 4459709f0e0SBart Van Assche enum srp_target_state old, 4469709f0e0SBart Van Assche enum srp_target_state new) 4479709f0e0SBart Van Assche { 4489709f0e0SBart Van Assche bool changed = false; 4499709f0e0SBart Van Assche 450e9684678SBart Van Assche spin_lock_irq(&target->lock); 4519709f0e0SBart Van Assche if (target->state == old) { 4529709f0e0SBart Van Assche target->state = new; 4539709f0e0SBart Van Assche changed = true; 4549709f0e0SBart Van Assche } 455e9684678SBart Van Assche spin_unlock_irq(&target->lock); 4569709f0e0SBart Van Assche return changed; 4579709f0e0SBart Van Assche } 4589709f0e0SBart Van Assche 459c4028958SDavid Howells static void srp_remove_work(struct work_struct *work) 460aef9ec39SRoland Dreier { 461c4028958SDavid Howells struct srp_target_port *target = 462c4028958SDavid Howells container_of(work, struct srp_target_port, work); 463aef9ec39SRoland Dreier 4649709f0e0SBart Van Assche if (!srp_change_state(target, SRP_TARGET_DEAD, SRP_TARGET_REMOVED)) 465aef9ec39SRoland Dreier return; 466aef9ec39SRoland Dreier 467b3589fd4SMatthew Wilcox spin_lock(&target->srp_host->target_lock); 468aef9ec39SRoland Dreier list_del(&target->list); 469b3589fd4SMatthew Wilcox spin_unlock(&target->srp_host->target_lock); 470aef9ec39SRoland Dreier 4713236822bSFUJITA Tomonori srp_remove_host(target->scsi_host); 472aef9ec39SRoland Dreier scsi_remove_host(target->scsi_host); 473aef9ec39SRoland Dreier ib_destroy_cm_id(target->cm_id); 474aef9ec39SRoland Dreier srp_free_target_ib(target); 475aef9ec39SRoland Dreier scsi_host_put(target->scsi_host); 476aef9ec39SRoland Dreier } 477aef9ec39SRoland Dreier 478aef9ec39SRoland Dreier static int srp_connect_target(struct srp_target_port *target) 479aef9ec39SRoland Dreier { 4809fe4bcf4SDavid Dillow int retries = 3; 481aef9ec39SRoland Dreier int ret; 482aef9ec39SRoland Dreier 483aef9ec39SRoland Dreier ret = srp_lookup_path(target); 484aef9ec39SRoland Dreier if (ret) 485aef9ec39SRoland Dreier return ret; 486aef9ec39SRoland Dreier 487aef9ec39SRoland Dreier while (1) { 488aef9ec39SRoland Dreier init_completion(&target->done); 489aef9ec39SRoland Dreier ret = srp_send_req(target); 490aef9ec39SRoland Dreier if (ret) 491aef9ec39SRoland Dreier return ret; 492aef9ec39SRoland Dreier wait_for_completion(&target->done); 493aef9ec39SRoland Dreier 494aef9ec39SRoland Dreier /* 495aef9ec39SRoland Dreier * The CM event handling code will set status to 496aef9ec39SRoland Dreier * SRP_PORT_REDIRECT if we get a port redirect REJ 497aef9ec39SRoland Dreier * back, or SRP_DLID_REDIRECT if we get a lid/qp 498aef9ec39SRoland Dreier * redirect REJ back. 499aef9ec39SRoland Dreier */ 500aef9ec39SRoland Dreier switch (target->status) { 501aef9ec39SRoland Dreier case 0: 502aef9ec39SRoland Dreier return 0; 503aef9ec39SRoland Dreier 504aef9ec39SRoland Dreier case SRP_PORT_REDIRECT: 505aef9ec39SRoland Dreier ret = srp_lookup_path(target); 506aef9ec39SRoland Dreier if (ret) 507aef9ec39SRoland Dreier return ret; 508aef9ec39SRoland Dreier break; 509aef9ec39SRoland Dreier 510aef9ec39SRoland Dreier case SRP_DLID_REDIRECT: 511aef9ec39SRoland Dreier break; 512aef9ec39SRoland Dreier 5139fe4bcf4SDavid Dillow case SRP_STALE_CONN: 5149fe4bcf4SDavid Dillow /* Our current CM id was stale, and is now in timewait. 5159fe4bcf4SDavid Dillow * Try to reconnect with a new one. 5169fe4bcf4SDavid Dillow */ 5179fe4bcf4SDavid Dillow if (!retries-- || srp_new_cm_id(target)) { 5189fe4bcf4SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 5199fe4bcf4SDavid Dillow "giving up on stale connection\n"); 5209fe4bcf4SDavid Dillow target->status = -ECONNRESET; 5219fe4bcf4SDavid Dillow return target->status; 5229fe4bcf4SDavid Dillow } 5239fe4bcf4SDavid Dillow 5249fe4bcf4SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 5259fe4bcf4SDavid Dillow "retrying stale connection\n"); 5269fe4bcf4SDavid Dillow break; 5279fe4bcf4SDavid Dillow 528aef9ec39SRoland Dreier default: 529aef9ec39SRoland Dreier return target->status; 530aef9ec39SRoland Dreier } 531aef9ec39SRoland Dreier } 532aef9ec39SRoland Dreier } 533aef9ec39SRoland Dreier 534d945e1dfSRoland Dreier static void srp_unmap_data(struct scsi_cmnd *scmnd, 535d945e1dfSRoland Dreier struct srp_target_port *target, 536d945e1dfSRoland Dreier struct srp_request *req) 537d945e1dfSRoland Dreier { 538bb350d1dSFUJITA Tomonori if (!scsi_sglist(scmnd) || 539d945e1dfSRoland Dreier (scmnd->sc_data_direction != DMA_TO_DEVICE && 540d945e1dfSRoland Dreier scmnd->sc_data_direction != DMA_FROM_DEVICE)) 541d945e1dfSRoland Dreier return; 542d945e1dfSRoland Dreier 543f5358a17SRoland Dreier if (req->fmr) { 544f5358a17SRoland Dreier ib_fmr_pool_unmap(req->fmr); 545f5358a17SRoland Dreier req->fmr = NULL; 546f5358a17SRoland Dreier } 547f5358a17SRoland Dreier 54805321937SGreg Kroah-Hartman ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd), 549bb350d1dSFUJITA Tomonori scsi_sg_count(scmnd), scmnd->sc_data_direction); 550d945e1dfSRoland Dreier } 551d945e1dfSRoland Dreier 55294a9174cSBart Van Assche static void srp_remove_req(struct srp_target_port *target, 55394a9174cSBart Van Assche struct srp_request *req, s32 req_lim_delta) 554526b4caaSIshai Rabinovitz { 55594a9174cSBart Van Assche unsigned long flags; 55694a9174cSBart Van Assche 557526b4caaSIshai Rabinovitz srp_unmap_data(req->scmnd, target, req); 558e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 55994a9174cSBart Van Assche target->req_lim += req_lim_delta; 560f8b6e31eSDavid Dillow req->scmnd = NULL; 561536ae14eSBart Van Assche list_add_tail(&req->list, &target->free_reqs); 562e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 563526b4caaSIshai Rabinovitz } 564526b4caaSIshai Rabinovitz 565526b4caaSIshai Rabinovitz static void srp_reset_req(struct srp_target_port *target, struct srp_request *req) 566526b4caaSIshai Rabinovitz { 567526b4caaSIshai Rabinovitz req->scmnd->result = DID_RESET << 16; 568526b4caaSIshai Rabinovitz req->scmnd->scsi_done(req->scmnd); 56994a9174cSBart Van Assche srp_remove_req(target, req, 0); 570526b4caaSIshai Rabinovitz } 571526b4caaSIshai Rabinovitz 572aef9ec39SRoland Dreier static int srp_reconnect_target(struct srp_target_port *target) 573aef9ec39SRoland Dreier { 574aef9ec39SRoland Dreier struct ib_qp_attr qp_attr; 575aef9ec39SRoland Dreier struct ib_wc wc; 576dcb4cb85SBart Van Assche int i, ret; 577aef9ec39SRoland Dreier 5789709f0e0SBart Van Assche if (!srp_change_state(target, SRP_TARGET_LIVE, SRP_TARGET_CONNECTING)) 579aef9ec39SRoland Dreier return -EAGAIN; 580aef9ec39SRoland Dreier 581aef9ec39SRoland Dreier srp_disconnect_target(target); 582aef9ec39SRoland Dreier /* 583aef9ec39SRoland Dreier * Now get a new local CM ID so that we avoid confusing the 584aef9ec39SRoland Dreier * target in case things are really fouled up. 585aef9ec39SRoland Dreier */ 5869fe4bcf4SDavid Dillow ret = srp_new_cm_id(target); 5879fe4bcf4SDavid Dillow if (ret) 588aef9ec39SRoland Dreier goto err; 589aef9ec39SRoland Dreier 590aef9ec39SRoland Dreier qp_attr.qp_state = IB_QPS_RESET; 591aef9ec39SRoland Dreier ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE); 592aef9ec39SRoland Dreier if (ret) 593aef9ec39SRoland Dreier goto err; 594aef9ec39SRoland Dreier 595aef9ec39SRoland Dreier ret = srp_init_qp(target, target->qp); 596aef9ec39SRoland Dreier if (ret) 597aef9ec39SRoland Dreier goto err; 598aef9ec39SRoland Dreier 5999c03dc9fSBart Van Assche while (ib_poll_cq(target->recv_cq, 1, &wc) > 0) 6009c03dc9fSBart Van Assche ; /* nothing */ 6019c03dc9fSBart Van Assche while (ib_poll_cq(target->send_cq, 1, &wc) > 0) 602aef9ec39SRoland Dreier ; /* nothing */ 603aef9ec39SRoland Dreier 604536ae14eSBart Van Assche for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { 605536ae14eSBart Van Assche struct srp_request *req = &target->req_ring[i]; 606536ae14eSBart Van Assche if (req->scmnd) 607526b4caaSIshai Rabinovitz srp_reset_req(target, req); 608536ae14eSBart Van Assche } 609aef9ec39SRoland Dreier 610536ae14eSBart Van Assche INIT_LIST_HEAD(&target->free_tx); 611dcb4cb85SBart Van Assche for (i = 0; i < SRP_SQ_SIZE; ++i) 612536ae14eSBart Van Assche list_add(&target->tx_ring[i]->list, &target->free_tx); 613aef9ec39SRoland Dreier 6141033ff67SIshai Rabinovitz target->qp_in_error = 0; 615aef9ec39SRoland Dreier ret = srp_connect_target(target); 616aef9ec39SRoland Dreier if (ret) 617aef9ec39SRoland Dreier goto err; 618aef9ec39SRoland Dreier 6199709f0e0SBart Van Assche if (!srp_change_state(target, SRP_TARGET_CONNECTING, SRP_TARGET_LIVE)) 620aef9ec39SRoland Dreier ret = -EAGAIN; 621aef9ec39SRoland Dreier 622aef9ec39SRoland Dreier return ret; 623aef9ec39SRoland Dreier 624aef9ec39SRoland Dreier err: 6257aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 6267aa54bd7SDavid Dillow PFX "reconnect failed (%d), removing target port.\n", ret); 627aef9ec39SRoland Dreier 628aef9ec39SRoland Dreier /* 629aef9ec39SRoland Dreier * We couldn't reconnect, so kill our target port off. 6309709f0e0SBart Van Assche * However, we have to defer the real removal because we 6319709f0e0SBart Van Assche * are in the context of the SCSI error handler now, which 6329709f0e0SBart Van Assche * will deadlock if we call scsi_remove_host(). 6339709f0e0SBart Van Assche * 6349709f0e0SBart Van Assche * Schedule our work inside the lock to avoid a race with 6359709f0e0SBart Van Assche * the flush_scheduled_work() in srp_remove_one(). 636aef9ec39SRoland Dreier */ 637e9684678SBart Van Assche spin_lock_irq(&target->lock); 638aef9ec39SRoland Dreier if (target->state == SRP_TARGET_CONNECTING) { 639aef9ec39SRoland Dreier target->state = SRP_TARGET_DEAD; 640c4028958SDavid Howells INIT_WORK(&target->work, srp_remove_work); 641*f0626710STejun Heo queue_work(ib_wq, &target->work); 642aef9ec39SRoland Dreier } 643e9684678SBart Van Assche spin_unlock_irq(&target->lock); 644aef9ec39SRoland Dreier 645aef9ec39SRoland Dreier return ret; 646aef9ec39SRoland Dreier } 647aef9ec39SRoland Dreier 648559ce8f1SIshai Rabinovitz static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat, 649f5358a17SRoland Dreier int sg_cnt, struct srp_request *req, 650f5358a17SRoland Dreier struct srp_direct_buf *buf) 651f5358a17SRoland Dreier { 652f5358a17SRoland Dreier u64 io_addr = 0; 653f5358a17SRoland Dreier u64 *dma_pages; 654f5358a17SRoland Dreier u32 len; 655f5358a17SRoland Dreier int page_cnt; 656f5358a17SRoland Dreier int i, j; 657f5358a17SRoland Dreier int ret; 65805321937SGreg Kroah-Hartman struct srp_device *dev = target->srp_host->srp_dev; 65985507bccSRalph Campbell struct ib_device *ibdev = dev->dev; 660bb350d1dSFUJITA Tomonori struct scatterlist *sg; 661f5358a17SRoland Dreier 662f5358a17SRoland Dreier if (!dev->fmr_pool) 663f5358a17SRoland Dreier return -ENODEV; 664f5358a17SRoland Dreier 6655d7cbfd6SRoland Dreier if (srp_target_is_mellanox(target) && 6665d7cbfd6SRoland Dreier (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask)) 667559ce8f1SIshai Rabinovitz return -EINVAL; 668559ce8f1SIshai Rabinovitz 669f5358a17SRoland Dreier len = page_cnt = 0; 670bb350d1dSFUJITA Tomonori scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) { 671bb350d1dSFUJITA Tomonori unsigned int dma_len = ib_sg_dma_len(ibdev, sg); 67285507bccSRalph Campbell 673bb350d1dSFUJITA Tomonori if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) { 674f5358a17SRoland Dreier if (i > 0) 675f5358a17SRoland Dreier return -EINVAL; 676f5358a17SRoland Dreier else 677f5358a17SRoland Dreier ++page_cnt; 678f5358a17SRoland Dreier } 679bb350d1dSFUJITA Tomonori if ((ib_sg_dma_address(ibdev, sg) + dma_len) & 680f5358a17SRoland Dreier ~dev->fmr_page_mask) { 681f5358a17SRoland Dreier if (i < sg_cnt - 1) 682f5358a17SRoland Dreier return -EINVAL; 683f5358a17SRoland Dreier else 684f5358a17SRoland Dreier ++page_cnt; 685f5358a17SRoland Dreier } 686f5358a17SRoland Dreier 68785507bccSRalph Campbell len += dma_len; 688f5358a17SRoland Dreier } 689f5358a17SRoland Dreier 690f5358a17SRoland Dreier page_cnt += len >> dev->fmr_page_shift; 691f5358a17SRoland Dreier if (page_cnt > SRP_FMR_SIZE) 692f5358a17SRoland Dreier return -ENOMEM; 693f5358a17SRoland Dreier 694f5358a17SRoland Dreier dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC); 695f5358a17SRoland Dreier if (!dma_pages) 696f5358a17SRoland Dreier return -ENOMEM; 697f5358a17SRoland Dreier 698f5358a17SRoland Dreier page_cnt = 0; 699bb350d1dSFUJITA Tomonori scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) { 700bb350d1dSFUJITA Tomonori unsigned int dma_len = ib_sg_dma_len(ibdev, sg); 70185507bccSRalph Campbell 70285507bccSRalph Campbell for (j = 0; j < dma_len; j += dev->fmr_page_size) 703f5358a17SRoland Dreier dma_pages[page_cnt++] = 704bb350d1dSFUJITA Tomonori (ib_sg_dma_address(ibdev, sg) & 70585507bccSRalph Campbell dev->fmr_page_mask) + j; 70685507bccSRalph Campbell } 707f5358a17SRoland Dreier 708f5358a17SRoland Dreier req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool, 709adfaa888SMichael S. Tsirkin dma_pages, page_cnt, io_addr); 710f5358a17SRoland Dreier if (IS_ERR(req->fmr)) { 711f5358a17SRoland Dreier ret = PTR_ERR(req->fmr); 7126583eb3dSVu Pham req->fmr = NULL; 713f5358a17SRoland Dreier goto out; 714f5358a17SRoland Dreier } 715f5358a17SRoland Dreier 71685507bccSRalph Campbell buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) & 71785507bccSRalph Campbell ~dev->fmr_page_mask); 718f5358a17SRoland Dreier buf->key = cpu_to_be32(req->fmr->fmr->rkey); 719f5358a17SRoland Dreier buf->len = cpu_to_be32(len); 720f5358a17SRoland Dreier 721f5358a17SRoland Dreier ret = 0; 722f5358a17SRoland Dreier 723f5358a17SRoland Dreier out: 724f5358a17SRoland Dreier kfree(dma_pages); 725f5358a17SRoland Dreier 726f5358a17SRoland Dreier return ret; 727f5358a17SRoland Dreier } 728f5358a17SRoland Dreier 729aef9ec39SRoland Dreier static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target, 730aef9ec39SRoland Dreier struct srp_request *req) 731aef9ec39SRoland Dreier { 732cf368713SRoland Dreier struct scatterlist *scat; 733aef9ec39SRoland Dreier struct srp_cmd *cmd = req->cmd->buf; 734cf368713SRoland Dreier int len, nents, count; 735f5358a17SRoland Dreier u8 fmt = SRP_DATA_DESC_DIRECT; 73685507bccSRalph Campbell struct srp_device *dev; 73785507bccSRalph Campbell struct ib_device *ibdev; 738aef9ec39SRoland Dreier 739bb350d1dSFUJITA Tomonori if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE) 740aef9ec39SRoland Dreier return sizeof (struct srp_cmd); 741aef9ec39SRoland Dreier 742aef9ec39SRoland Dreier if (scmnd->sc_data_direction != DMA_FROM_DEVICE && 743aef9ec39SRoland Dreier scmnd->sc_data_direction != DMA_TO_DEVICE) { 7447aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 7457aa54bd7SDavid Dillow PFX "Unhandled data direction %d\n", 746aef9ec39SRoland Dreier scmnd->sc_data_direction); 747aef9ec39SRoland Dreier return -EINVAL; 748aef9ec39SRoland Dreier } 749aef9ec39SRoland Dreier 750bb350d1dSFUJITA Tomonori nents = scsi_sg_count(scmnd); 751bb350d1dSFUJITA Tomonori scat = scsi_sglist(scmnd); 752aef9ec39SRoland Dreier 75305321937SGreg Kroah-Hartman dev = target->srp_host->srp_dev; 75485507bccSRalph Campbell ibdev = dev->dev; 75585507bccSRalph Campbell 75685507bccSRalph Campbell count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction); 757aef9ec39SRoland Dreier 758aef9ec39SRoland Dreier fmt = SRP_DATA_DESC_DIRECT; 759f5358a17SRoland Dreier len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf); 760f5358a17SRoland Dreier 761f5358a17SRoland Dreier if (count == 1) { 762f5358a17SRoland Dreier /* 763f5358a17SRoland Dreier * The midlayer only generated a single gather/scatter 764f5358a17SRoland Dreier * entry, or DMA mapping coalesced everything to a 765f5358a17SRoland Dreier * single entry. So a direct descriptor along with 766f5358a17SRoland Dreier * the DMA MR suffices. 767f5358a17SRoland Dreier */ 768f5358a17SRoland Dreier struct srp_direct_buf *buf = (void *) cmd->add_data; 769aef9ec39SRoland Dreier 77085507bccSRalph Campbell buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat)); 7719af76271SDavid Dillow buf->key = cpu_to_be32(target->rkey); 77285507bccSRalph Campbell buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat)); 773559ce8f1SIshai Rabinovitz } else if (srp_map_fmr(target, scat, count, req, 774f5358a17SRoland Dreier (void *) cmd->add_data)) { 775f5358a17SRoland Dreier /* 776f5358a17SRoland Dreier * FMR mapping failed, and the scatterlist has more 777f5358a17SRoland Dreier * than one entry. Generate an indirect memory 778f5358a17SRoland Dreier * descriptor. 779f5358a17SRoland Dreier */ 780aef9ec39SRoland Dreier struct srp_indirect_buf *buf = (void *) cmd->add_data; 781bb350d1dSFUJITA Tomonori struct scatterlist *sg; 782aef9ec39SRoland Dreier u32 datalen = 0; 783f5358a17SRoland Dreier int i; 784aef9ec39SRoland Dreier 785aef9ec39SRoland Dreier fmt = SRP_DATA_DESC_INDIRECT; 786f5358a17SRoland Dreier len = sizeof (struct srp_cmd) + 787f5358a17SRoland Dreier sizeof (struct srp_indirect_buf) + 788f5358a17SRoland Dreier count * sizeof (struct srp_direct_buf); 789f5358a17SRoland Dreier 790bb350d1dSFUJITA Tomonori scsi_for_each_sg(scmnd, sg, count, i) { 791bb350d1dSFUJITA Tomonori unsigned int dma_len = ib_sg_dma_len(ibdev, sg); 79285507bccSRalph Campbell 793f5358a17SRoland Dreier buf->desc_list[i].va = 794bb350d1dSFUJITA Tomonori cpu_to_be64(ib_sg_dma_address(ibdev, sg)); 795f5358a17SRoland Dreier buf->desc_list[i].key = 7969af76271SDavid Dillow cpu_to_be32(target->rkey); 79785507bccSRalph Campbell buf->desc_list[i].len = cpu_to_be32(dma_len); 79885507bccSRalph Campbell datalen += dma_len; 799f5358a17SRoland Dreier } 800aef9ec39SRoland Dreier 801aef9ec39SRoland Dreier if (scmnd->sc_data_direction == DMA_TO_DEVICE) 802cf368713SRoland Dreier cmd->data_out_desc_cnt = count; 803aef9ec39SRoland Dreier else 804cf368713SRoland Dreier cmd->data_in_desc_cnt = count; 805aef9ec39SRoland Dreier 806f5358a17SRoland Dreier buf->table_desc.va = 807f5358a17SRoland Dreier cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf); 808aef9ec39SRoland Dreier buf->table_desc.key = 8099af76271SDavid Dillow cpu_to_be32(target->rkey); 810aef9ec39SRoland Dreier buf->table_desc.len = 811cf368713SRoland Dreier cpu_to_be32(count * sizeof (struct srp_direct_buf)); 812aef9ec39SRoland Dreier 813aef9ec39SRoland Dreier buf->len = cpu_to_be32(datalen); 814aef9ec39SRoland Dreier } 815aef9ec39SRoland Dreier 816aef9ec39SRoland Dreier if (scmnd->sc_data_direction == DMA_TO_DEVICE) 817aef9ec39SRoland Dreier cmd->buf_fmt = fmt << 4; 818aef9ec39SRoland Dreier else 819aef9ec39SRoland Dreier cmd->buf_fmt = fmt; 820aef9ec39SRoland Dreier 821aef9ec39SRoland Dreier return len; 822aef9ec39SRoland Dreier } 823aef9ec39SRoland Dreier 82405a1d750SDavid Dillow /* 82576c75b25SBart Van Assche * Return an IU and possible credit to the free pool 82676c75b25SBart Van Assche */ 82776c75b25SBart Van Assche static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu, 82876c75b25SBart Van Assche enum srp_iu_type iu_type) 82976c75b25SBart Van Assche { 83076c75b25SBart Van Assche unsigned long flags; 83176c75b25SBart Van Assche 832e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 83376c75b25SBart Van Assche list_add(&iu->list, &target->free_tx); 83476c75b25SBart Van Assche if (iu_type != SRP_IU_RSP) 83576c75b25SBart Van Assche ++target->req_lim; 836e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 83776c75b25SBart Van Assche } 83876c75b25SBart Van Assche 83976c75b25SBart Van Assche /* 840e9684678SBart Van Assche * Must be called with target->lock held to protect req_lim and free_tx. 841e9684678SBart Van Assche * If IU is not sent, it must be returned using srp_put_tx_iu(). 84205a1d750SDavid Dillow * 84305a1d750SDavid Dillow * Note: 84405a1d750SDavid Dillow * An upper limit for the number of allocated information units for each 84505a1d750SDavid Dillow * request type is: 84605a1d750SDavid Dillow * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues 84705a1d750SDavid Dillow * more than Scsi_Host.can_queue requests. 84805a1d750SDavid Dillow * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE. 84905a1d750SDavid Dillow * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than 85005a1d750SDavid Dillow * one unanswered SRP request to an initiator. 85105a1d750SDavid Dillow */ 85205a1d750SDavid Dillow static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target, 85305a1d750SDavid Dillow enum srp_iu_type iu_type) 85405a1d750SDavid Dillow { 85505a1d750SDavid Dillow s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE; 85605a1d750SDavid Dillow struct srp_iu *iu; 85705a1d750SDavid Dillow 85805a1d750SDavid Dillow srp_send_completion(target->send_cq, target); 85905a1d750SDavid Dillow 860dcb4cb85SBart Van Assche if (list_empty(&target->free_tx)) 86105a1d750SDavid Dillow return NULL; 86205a1d750SDavid Dillow 86305a1d750SDavid Dillow /* Initiator responses to target requests do not consume credits */ 86476c75b25SBart Van Assche if (iu_type != SRP_IU_RSP) { 86576c75b25SBart Van Assche if (target->req_lim <= rsv) { 86605a1d750SDavid Dillow ++target->zero_req_lim; 86705a1d750SDavid Dillow return NULL; 86805a1d750SDavid Dillow } 86905a1d750SDavid Dillow 87076c75b25SBart Van Assche --target->req_lim; 87176c75b25SBart Van Assche } 87276c75b25SBart Van Assche 873dcb4cb85SBart Van Assche iu = list_first_entry(&target->free_tx, struct srp_iu, list); 87476c75b25SBart Van Assche list_del(&iu->list); 87505a1d750SDavid Dillow return iu; 87605a1d750SDavid Dillow } 87705a1d750SDavid Dillow 87876c75b25SBart Van Assche static int srp_post_send(struct srp_target_port *target, 87905a1d750SDavid Dillow struct srp_iu *iu, int len) 88005a1d750SDavid Dillow { 88105a1d750SDavid Dillow struct ib_sge list; 88205a1d750SDavid Dillow struct ib_send_wr wr, *bad_wr; 88305a1d750SDavid Dillow 88405a1d750SDavid Dillow list.addr = iu->dma; 88505a1d750SDavid Dillow list.length = len; 8869af76271SDavid Dillow list.lkey = target->lkey; 88705a1d750SDavid Dillow 88805a1d750SDavid Dillow wr.next = NULL; 889dcb4cb85SBart Van Assche wr.wr_id = (uintptr_t) iu; 89005a1d750SDavid Dillow wr.sg_list = &list; 89105a1d750SDavid Dillow wr.num_sge = 1; 89205a1d750SDavid Dillow wr.opcode = IB_WR_SEND; 89305a1d750SDavid Dillow wr.send_flags = IB_SEND_SIGNALED; 89405a1d750SDavid Dillow 89576c75b25SBart Van Assche return ib_post_send(target->qp, &wr, &bad_wr); 89605a1d750SDavid Dillow } 89705a1d750SDavid Dillow 898dcb4cb85SBart Van Assche static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu) 899c996bb47SBart Van Assche { 900c996bb47SBart Van Assche struct ib_recv_wr wr, *bad_wr; 901dcb4cb85SBart Van Assche struct ib_sge list; 902c996bb47SBart Van Assche 903c996bb47SBart Van Assche list.addr = iu->dma; 904c996bb47SBart Van Assche list.length = iu->size; 9059af76271SDavid Dillow list.lkey = target->lkey; 906c996bb47SBart Van Assche 907c996bb47SBart Van Assche wr.next = NULL; 908dcb4cb85SBart Van Assche wr.wr_id = (uintptr_t) iu; 909c996bb47SBart Van Assche wr.sg_list = &list; 910c996bb47SBart Van Assche wr.num_sge = 1; 911c996bb47SBart Van Assche 912dcb4cb85SBart Van Assche return ib_post_recv(target->qp, &wr, &bad_wr); 913c996bb47SBart Van Assche } 914c996bb47SBart Van Assche 915aef9ec39SRoland Dreier static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp) 916aef9ec39SRoland Dreier { 917aef9ec39SRoland Dreier struct srp_request *req; 918aef9ec39SRoland Dreier struct scsi_cmnd *scmnd; 919aef9ec39SRoland Dreier unsigned long flags; 920aef9ec39SRoland Dreier 921aef9ec39SRoland Dreier if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) { 922e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 92394a9174cSBart Van Assche target->req_lim += be32_to_cpu(rsp->req_lim_delta); 924e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 92594a9174cSBart Van Assche 926f8b6e31eSDavid Dillow target->tsk_mgmt_status = -1; 927f8b6e31eSDavid Dillow if (be32_to_cpu(rsp->resp_data_len) >= 4) 928f8b6e31eSDavid Dillow target->tsk_mgmt_status = rsp->data[3]; 929f8b6e31eSDavid Dillow complete(&target->tsk_mgmt_done); 930aef9ec39SRoland Dreier } else { 931f8b6e31eSDavid Dillow req = &target->req_ring[rsp->tag]; 932aef9ec39SRoland Dreier scmnd = req->scmnd; 933aef9ec39SRoland Dreier if (!scmnd) 9347aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 9357aa54bd7SDavid Dillow "Null scmnd for RSP w/tag %016llx\n", 936aef9ec39SRoland Dreier (unsigned long long) rsp->tag); 937aef9ec39SRoland Dreier scmnd->result = rsp->status; 938aef9ec39SRoland Dreier 939aef9ec39SRoland Dreier if (rsp->flags & SRP_RSP_FLAG_SNSVALID) { 940aef9ec39SRoland Dreier memcpy(scmnd->sense_buffer, rsp->data + 941aef9ec39SRoland Dreier be32_to_cpu(rsp->resp_data_len), 942aef9ec39SRoland Dreier min_t(int, be32_to_cpu(rsp->sense_data_len), 943aef9ec39SRoland Dreier SCSI_SENSE_BUFFERSIZE)); 944aef9ec39SRoland Dreier } 945aef9ec39SRoland Dreier 946aef9ec39SRoland Dreier if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER)) 947bb350d1dSFUJITA Tomonori scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt)); 948aef9ec39SRoland Dreier else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER)) 949bb350d1dSFUJITA Tomonori scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt)); 950aef9ec39SRoland Dreier 95194a9174cSBart Van Assche srp_remove_req(target, req, be32_to_cpu(rsp->req_lim_delta)); 952f8b6e31eSDavid Dillow scmnd->host_scribble = NULL; 953aef9ec39SRoland Dreier scmnd->scsi_done(scmnd); 954aef9ec39SRoland Dreier } 955aef9ec39SRoland Dreier } 956aef9ec39SRoland Dreier 957bb12588aSDavid Dillow static int srp_response_common(struct srp_target_port *target, s32 req_delta, 958bb12588aSDavid Dillow void *rsp, int len) 959bb12588aSDavid Dillow { 96076c75b25SBart Van Assche struct ib_device *dev = target->srp_host->srp_dev->dev; 961bb12588aSDavid Dillow unsigned long flags; 962bb12588aSDavid Dillow struct srp_iu *iu; 96376c75b25SBart Van Assche int err; 964bb12588aSDavid Dillow 965e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 966bb12588aSDavid Dillow target->req_lim += req_delta; 967bb12588aSDavid Dillow iu = __srp_get_tx_iu(target, SRP_IU_RSP); 968e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 96976c75b25SBart Van Assche 970bb12588aSDavid Dillow if (!iu) { 971bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 972bb12588aSDavid Dillow "no IU available to send response\n"); 97376c75b25SBart Van Assche return 1; 974bb12588aSDavid Dillow } 975bb12588aSDavid Dillow 976bb12588aSDavid Dillow ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE); 977bb12588aSDavid Dillow memcpy(iu->buf, rsp, len); 978bb12588aSDavid Dillow ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE); 979bb12588aSDavid Dillow 98076c75b25SBart Van Assche err = srp_post_send(target, iu, len); 98176c75b25SBart Van Assche if (err) { 982bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 983bb12588aSDavid Dillow "unable to post response: %d\n", err); 98476c75b25SBart Van Assche srp_put_tx_iu(target, iu, SRP_IU_RSP); 98576c75b25SBart Van Assche } 986bb12588aSDavid Dillow 987bb12588aSDavid Dillow return err; 988bb12588aSDavid Dillow } 989bb12588aSDavid Dillow 990bb12588aSDavid Dillow static void srp_process_cred_req(struct srp_target_port *target, 991bb12588aSDavid Dillow struct srp_cred_req *req) 992bb12588aSDavid Dillow { 993bb12588aSDavid Dillow struct srp_cred_rsp rsp = { 994bb12588aSDavid Dillow .opcode = SRP_CRED_RSP, 995bb12588aSDavid Dillow .tag = req->tag, 996bb12588aSDavid Dillow }; 997bb12588aSDavid Dillow s32 delta = be32_to_cpu(req->req_lim_delta); 998bb12588aSDavid Dillow 999bb12588aSDavid Dillow if (srp_response_common(target, delta, &rsp, sizeof rsp)) 1000bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 1001bb12588aSDavid Dillow "problems processing SRP_CRED_REQ\n"); 1002bb12588aSDavid Dillow } 1003bb12588aSDavid Dillow 1004bb12588aSDavid Dillow static void srp_process_aer_req(struct srp_target_port *target, 1005bb12588aSDavid Dillow struct srp_aer_req *req) 1006bb12588aSDavid Dillow { 1007bb12588aSDavid Dillow struct srp_aer_rsp rsp = { 1008bb12588aSDavid Dillow .opcode = SRP_AER_RSP, 1009bb12588aSDavid Dillow .tag = req->tag, 1010bb12588aSDavid Dillow }; 1011bb12588aSDavid Dillow s32 delta = be32_to_cpu(req->req_lim_delta); 1012bb12588aSDavid Dillow 1013bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 1014bb12588aSDavid Dillow "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun)); 1015bb12588aSDavid Dillow 1016bb12588aSDavid Dillow if (srp_response_common(target, delta, &rsp, sizeof rsp)) 1017bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 1018bb12588aSDavid Dillow "problems processing SRP_AER_REQ\n"); 1019bb12588aSDavid Dillow } 1020bb12588aSDavid Dillow 1021aef9ec39SRoland Dreier static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc) 1022aef9ec39SRoland Dreier { 1023dcb4cb85SBart Van Assche struct ib_device *dev = target->srp_host->srp_dev->dev; 1024dcb4cb85SBart Van Assche struct srp_iu *iu = (struct srp_iu *) wc->wr_id; 1025c996bb47SBart Van Assche int res; 1026aef9ec39SRoland Dreier u8 opcode; 1027aef9ec39SRoland Dreier 102885507bccSRalph Campbell ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len, 102985507bccSRalph Campbell DMA_FROM_DEVICE); 1030aef9ec39SRoland Dreier 1031aef9ec39SRoland Dreier opcode = *(u8 *) iu->buf; 1032aef9ec39SRoland Dreier 1033aef9ec39SRoland Dreier if (0) { 10347aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 10357aa54bd7SDavid Dillow PFX "recv completion, opcode 0x%02x\n", opcode); 10367a700811SBart Van Assche print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1, 10377a700811SBart Van Assche iu->buf, wc->byte_len, true); 1038aef9ec39SRoland Dreier } 1039aef9ec39SRoland Dreier 1040aef9ec39SRoland Dreier switch (opcode) { 1041aef9ec39SRoland Dreier case SRP_RSP: 1042aef9ec39SRoland Dreier srp_process_rsp(target, iu->buf); 1043aef9ec39SRoland Dreier break; 1044aef9ec39SRoland Dreier 1045bb12588aSDavid Dillow case SRP_CRED_REQ: 1046bb12588aSDavid Dillow srp_process_cred_req(target, iu->buf); 1047bb12588aSDavid Dillow break; 1048bb12588aSDavid Dillow 1049bb12588aSDavid Dillow case SRP_AER_REQ: 1050bb12588aSDavid Dillow srp_process_aer_req(target, iu->buf); 1051bb12588aSDavid Dillow break; 1052bb12588aSDavid Dillow 1053aef9ec39SRoland Dreier case SRP_T_LOGOUT: 1054aef9ec39SRoland Dreier /* XXX Handle target logout */ 10557aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 10567aa54bd7SDavid Dillow PFX "Got target logout request\n"); 1057aef9ec39SRoland Dreier break; 1058aef9ec39SRoland Dreier 1059aef9ec39SRoland Dreier default: 10607aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 10617aa54bd7SDavid Dillow PFX "Unhandled SRP opcode 0x%02x\n", opcode); 1062aef9ec39SRoland Dreier break; 1063aef9ec39SRoland Dreier } 1064aef9ec39SRoland Dreier 106585507bccSRalph Campbell ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len, 106685507bccSRalph Campbell DMA_FROM_DEVICE); 1067c996bb47SBart Van Assche 1068dcb4cb85SBart Van Assche res = srp_post_recv(target, iu); 1069c996bb47SBart Van Assche if (res != 0) 1070c996bb47SBart Van Assche shost_printk(KERN_ERR, target->scsi_host, 1071c996bb47SBart Van Assche PFX "Recv failed with error code %d\n", res); 1072aef9ec39SRoland Dreier } 1073aef9ec39SRoland Dreier 10749c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr) 1075aef9ec39SRoland Dreier { 1076aef9ec39SRoland Dreier struct srp_target_port *target = target_ptr; 1077aef9ec39SRoland Dreier struct ib_wc wc; 1078aef9ec39SRoland Dreier 1079aef9ec39SRoland Dreier ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); 1080aef9ec39SRoland Dreier while (ib_poll_cq(cq, 1, &wc) > 0) { 1081aef9ec39SRoland Dreier if (wc.status) { 10827aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 10839c03dc9fSBart Van Assche PFX "failed receive status %d\n", 1084aef9ec39SRoland Dreier wc.status); 10851033ff67SIshai Rabinovitz target->qp_in_error = 1; 1086aef9ec39SRoland Dreier break; 1087aef9ec39SRoland Dreier } 1088aef9ec39SRoland Dreier 1089aef9ec39SRoland Dreier srp_handle_recv(target, &wc); 10909c03dc9fSBart Van Assche } 10919c03dc9fSBart Van Assche } 10929c03dc9fSBart Van Assche 10939c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr) 10949c03dc9fSBart Van Assche { 10959c03dc9fSBart Van Assche struct srp_target_port *target = target_ptr; 10969c03dc9fSBart Van Assche struct ib_wc wc; 1097dcb4cb85SBart Van Assche struct srp_iu *iu; 10989c03dc9fSBart Van Assche 10999c03dc9fSBart Van Assche while (ib_poll_cq(cq, 1, &wc) > 0) { 11009c03dc9fSBart Van Assche if (wc.status) { 11019c03dc9fSBart Van Assche shost_printk(KERN_ERR, target->scsi_host, 11029c03dc9fSBart Van Assche PFX "failed send status %d\n", 11039c03dc9fSBart Van Assche wc.status); 11049c03dc9fSBart Van Assche target->qp_in_error = 1; 11059c03dc9fSBart Van Assche break; 11069c03dc9fSBart Van Assche } 11079c03dc9fSBart Van Assche 1108dcb4cb85SBart Van Assche iu = (struct srp_iu *) wc.wr_id; 1109dcb4cb85SBart Van Assche list_add(&iu->list, &target->free_tx); 1110aef9ec39SRoland Dreier } 1111aef9ec39SRoland Dreier } 1112aef9ec39SRoland Dreier 111376c75b25SBart Van Assche static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd) 1114aef9ec39SRoland Dreier { 111576c75b25SBart Van Assche struct srp_target_port *target = host_to_target(shost); 1116aef9ec39SRoland Dreier struct srp_request *req; 1117aef9ec39SRoland Dreier struct srp_iu *iu; 1118aef9ec39SRoland Dreier struct srp_cmd *cmd; 111985507bccSRalph Campbell struct ib_device *dev; 112076c75b25SBart Van Assche unsigned long flags; 1121aef9ec39SRoland Dreier int len; 1122aef9ec39SRoland Dreier 1123aef9ec39SRoland Dreier if (target->state == SRP_TARGET_CONNECTING) 1124aef9ec39SRoland Dreier goto err; 1125aef9ec39SRoland Dreier 1126aef9ec39SRoland Dreier if (target->state == SRP_TARGET_DEAD || 1127aef9ec39SRoland Dreier target->state == SRP_TARGET_REMOVED) { 1128aef9ec39SRoland Dreier scmnd->result = DID_BAD_TARGET << 16; 112976c75b25SBart Van Assche scmnd->scsi_done(scmnd); 1130aef9ec39SRoland Dreier return 0; 1131aef9ec39SRoland Dreier } 1132aef9ec39SRoland Dreier 1133e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 1134bb12588aSDavid Dillow iu = __srp_get_tx_iu(target, SRP_IU_CMD); 113576c75b25SBart Van Assche if (iu) { 113676c75b25SBart Van Assche req = list_first_entry(&target->free_reqs, struct srp_request, 113776c75b25SBart Van Assche list); 113876c75b25SBart Van Assche list_del(&req->list); 113976c75b25SBart Van Assche } 1140e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 114176c75b25SBart Van Assche 1142aef9ec39SRoland Dreier if (!iu) 1143aef9ec39SRoland Dreier goto err; 1144aef9ec39SRoland Dreier 114505321937SGreg Kroah-Hartman dev = target->srp_host->srp_dev->dev; 114685507bccSRalph Campbell ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len, 114785507bccSRalph Campbell DMA_TO_DEVICE); 1148aef9ec39SRoland Dreier 1149aef9ec39SRoland Dreier scmnd->result = 0; 1150f8b6e31eSDavid Dillow scmnd->host_scribble = (void *) req; 1151aef9ec39SRoland Dreier 1152aef9ec39SRoland Dreier cmd = iu->buf; 1153aef9ec39SRoland Dreier memset(cmd, 0, sizeof *cmd); 1154aef9ec39SRoland Dreier 1155aef9ec39SRoland Dreier cmd->opcode = SRP_CMD; 1156aef9ec39SRoland Dreier cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48); 1157d945e1dfSRoland Dreier cmd->tag = req->index; 1158aef9ec39SRoland Dreier memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len); 1159aef9ec39SRoland Dreier 1160aef9ec39SRoland Dreier req->scmnd = scmnd; 1161aef9ec39SRoland Dreier req->cmd = iu; 1162aef9ec39SRoland Dreier 1163aef9ec39SRoland Dreier len = srp_map_data(scmnd, target, req); 1164aef9ec39SRoland Dreier if (len < 0) { 11657aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 11667aa54bd7SDavid Dillow PFX "Failed to map data\n"); 116776c75b25SBart Van Assche goto err_iu; 1168aef9ec39SRoland Dreier } 1169aef9ec39SRoland Dreier 117085507bccSRalph Campbell ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len, 117185507bccSRalph Campbell DMA_TO_DEVICE); 1172aef9ec39SRoland Dreier 117376c75b25SBart Van Assche if (srp_post_send(target, iu, len)) { 11747aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n"); 1175aef9ec39SRoland Dreier goto err_unmap; 1176aef9ec39SRoland Dreier } 1177aef9ec39SRoland Dreier 1178aef9ec39SRoland Dreier return 0; 1179aef9ec39SRoland Dreier 1180aef9ec39SRoland Dreier err_unmap: 1181aef9ec39SRoland Dreier srp_unmap_data(scmnd, target, req); 1182aef9ec39SRoland Dreier 118376c75b25SBart Van Assche err_iu: 118476c75b25SBart Van Assche srp_put_tx_iu(target, iu, SRP_IU_CMD); 118576c75b25SBart Van Assche 1186e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 118776c75b25SBart Van Assche list_add(&req->list, &target->free_reqs); 1188e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 118976c75b25SBart Van Assche 1190aef9ec39SRoland Dreier err: 1191aef9ec39SRoland Dreier return SCSI_MLQUEUE_HOST_BUSY; 1192aef9ec39SRoland Dreier } 1193aef9ec39SRoland Dreier 1194aef9ec39SRoland Dreier static int srp_alloc_iu_bufs(struct srp_target_port *target) 1195aef9ec39SRoland Dreier { 1196aef9ec39SRoland Dreier int i; 1197aef9ec39SRoland Dreier 1198aef9ec39SRoland Dreier for (i = 0; i < SRP_RQ_SIZE; ++i) { 1199aef9ec39SRoland Dreier target->rx_ring[i] = srp_alloc_iu(target->srp_host, 1200aef9ec39SRoland Dreier target->max_ti_iu_len, 1201aef9ec39SRoland Dreier GFP_KERNEL, DMA_FROM_DEVICE); 1202aef9ec39SRoland Dreier if (!target->rx_ring[i]) 1203aef9ec39SRoland Dreier goto err; 1204aef9ec39SRoland Dreier } 1205aef9ec39SRoland Dreier 1206dd5e6e38SBart Van Assche for (i = 0; i < SRP_SQ_SIZE; ++i) { 1207aef9ec39SRoland Dreier target->tx_ring[i] = srp_alloc_iu(target->srp_host, 120874b0a15bSVu Pham srp_max_iu_len, 1209aef9ec39SRoland Dreier GFP_KERNEL, DMA_TO_DEVICE); 1210aef9ec39SRoland Dreier if (!target->tx_ring[i]) 1211aef9ec39SRoland Dreier goto err; 1212dcb4cb85SBart Van Assche 1213dcb4cb85SBart Van Assche list_add(&target->tx_ring[i]->list, &target->free_tx); 1214aef9ec39SRoland Dreier } 1215aef9ec39SRoland Dreier 1216aef9ec39SRoland Dreier return 0; 1217aef9ec39SRoland Dreier 1218aef9ec39SRoland Dreier err: 1219aef9ec39SRoland Dreier for (i = 0; i < SRP_RQ_SIZE; ++i) { 1220aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->rx_ring[i]); 1221aef9ec39SRoland Dreier target->rx_ring[i] = NULL; 1222aef9ec39SRoland Dreier } 1223aef9ec39SRoland Dreier 1224dd5e6e38SBart Van Assche for (i = 0; i < SRP_SQ_SIZE; ++i) { 1225aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->tx_ring[i]); 1226aef9ec39SRoland Dreier target->tx_ring[i] = NULL; 1227aef9ec39SRoland Dreier } 1228aef9ec39SRoland Dreier 1229aef9ec39SRoland Dreier return -ENOMEM; 1230aef9ec39SRoland Dreier } 1231aef9ec39SRoland Dreier 1232aef9ec39SRoland Dreier static void srp_cm_rej_handler(struct ib_cm_id *cm_id, 1233aef9ec39SRoland Dreier struct ib_cm_event *event, 1234aef9ec39SRoland Dreier struct srp_target_port *target) 1235aef9ec39SRoland Dreier { 12367aa54bd7SDavid Dillow struct Scsi_Host *shost = target->scsi_host; 1237aef9ec39SRoland Dreier struct ib_class_port_info *cpi; 1238aef9ec39SRoland Dreier int opcode; 1239aef9ec39SRoland Dreier 1240aef9ec39SRoland Dreier switch (event->param.rej_rcvd.reason) { 1241aef9ec39SRoland Dreier case IB_CM_REJ_PORT_CM_REDIRECT: 1242aef9ec39SRoland Dreier cpi = event->param.rej_rcvd.ari; 1243aef9ec39SRoland Dreier target->path.dlid = cpi->redirect_lid; 1244aef9ec39SRoland Dreier target->path.pkey = cpi->redirect_pkey; 1245aef9ec39SRoland Dreier cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff; 1246aef9ec39SRoland Dreier memcpy(target->path.dgid.raw, cpi->redirect_gid, 16); 1247aef9ec39SRoland Dreier 1248aef9ec39SRoland Dreier target->status = target->path.dlid ? 1249aef9ec39SRoland Dreier SRP_DLID_REDIRECT : SRP_PORT_REDIRECT; 1250aef9ec39SRoland Dreier break; 1251aef9ec39SRoland Dreier 1252aef9ec39SRoland Dreier case IB_CM_REJ_PORT_REDIRECT: 12535d7cbfd6SRoland Dreier if (srp_target_is_topspin(target)) { 1254aef9ec39SRoland Dreier /* 1255aef9ec39SRoland Dreier * Topspin/Cisco SRP gateways incorrectly send 1256aef9ec39SRoland Dreier * reject reason code 25 when they mean 24 1257aef9ec39SRoland Dreier * (port redirect). 1258aef9ec39SRoland Dreier */ 1259aef9ec39SRoland Dreier memcpy(target->path.dgid.raw, 1260aef9ec39SRoland Dreier event->param.rej_rcvd.ari, 16); 1261aef9ec39SRoland Dreier 12627aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, shost, 12637aa54bd7SDavid Dillow PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n", 1264aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix), 1265aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id)); 1266aef9ec39SRoland Dreier 1267aef9ec39SRoland Dreier target->status = SRP_PORT_REDIRECT; 1268aef9ec39SRoland Dreier } else { 12697aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12707aa54bd7SDavid Dillow " REJ reason: IB_CM_REJ_PORT_REDIRECT\n"); 1271aef9ec39SRoland Dreier target->status = -ECONNRESET; 1272aef9ec39SRoland Dreier } 1273aef9ec39SRoland Dreier break; 1274aef9ec39SRoland Dreier 1275aef9ec39SRoland Dreier case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: 12767aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12777aa54bd7SDavid Dillow " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); 1278aef9ec39SRoland Dreier target->status = -ECONNRESET; 1279aef9ec39SRoland Dreier break; 1280aef9ec39SRoland Dreier 1281aef9ec39SRoland Dreier case IB_CM_REJ_CONSUMER_DEFINED: 1282aef9ec39SRoland Dreier opcode = *(u8 *) event->private_data; 1283aef9ec39SRoland Dreier if (opcode == SRP_LOGIN_REJ) { 1284aef9ec39SRoland Dreier struct srp_login_rej *rej = event->private_data; 1285aef9ec39SRoland Dreier u32 reason = be32_to_cpu(rej->reason); 1286aef9ec39SRoland Dreier 1287aef9ec39SRoland Dreier if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) 12887aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12897aa54bd7SDavid Dillow PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); 1290aef9ec39SRoland Dreier else 12917aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12927aa54bd7SDavid Dillow PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason); 1293aef9ec39SRoland Dreier } else 12947aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12957aa54bd7SDavid Dillow " REJ reason: IB_CM_REJ_CONSUMER_DEFINED," 1296aef9ec39SRoland Dreier " opcode 0x%02x\n", opcode); 1297aef9ec39SRoland Dreier target->status = -ECONNRESET; 1298aef9ec39SRoland Dreier break; 1299aef9ec39SRoland Dreier 13009fe4bcf4SDavid Dillow case IB_CM_REJ_STALE_CONN: 13019fe4bcf4SDavid Dillow shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n"); 13029fe4bcf4SDavid Dillow target->status = SRP_STALE_CONN; 13039fe4bcf4SDavid Dillow break; 13049fe4bcf4SDavid Dillow 1305aef9ec39SRoland Dreier default: 13067aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n", 1307aef9ec39SRoland Dreier event->param.rej_rcvd.reason); 1308aef9ec39SRoland Dreier target->status = -ECONNRESET; 1309aef9ec39SRoland Dreier } 1310aef9ec39SRoland Dreier } 1311aef9ec39SRoland Dreier 1312aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) 1313aef9ec39SRoland Dreier { 1314aef9ec39SRoland Dreier struct srp_target_port *target = cm_id->context; 1315aef9ec39SRoland Dreier struct ib_qp_attr *qp_attr = NULL; 1316aef9ec39SRoland Dreier int attr_mask = 0; 1317aef9ec39SRoland Dreier int comp = 0; 1318aef9ec39SRoland Dreier int opcode = 0; 1319c996bb47SBart Van Assche int i; 1320aef9ec39SRoland Dreier 1321aef9ec39SRoland Dreier switch (event->event) { 1322aef9ec39SRoland Dreier case IB_CM_REQ_ERROR: 13237aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, 13247aa54bd7SDavid Dillow PFX "Sending CM REQ failed\n"); 1325aef9ec39SRoland Dreier comp = 1; 1326aef9ec39SRoland Dreier target->status = -ECONNRESET; 1327aef9ec39SRoland Dreier break; 1328aef9ec39SRoland Dreier 1329aef9ec39SRoland Dreier case IB_CM_REP_RECEIVED: 1330aef9ec39SRoland Dreier comp = 1; 1331aef9ec39SRoland Dreier opcode = *(u8 *) event->private_data; 1332aef9ec39SRoland Dreier 1333aef9ec39SRoland Dreier if (opcode == SRP_LOGIN_RSP) { 1334aef9ec39SRoland Dreier struct srp_login_rsp *rsp = event->private_data; 1335aef9ec39SRoland Dreier 1336aef9ec39SRoland Dreier target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len); 1337aef9ec39SRoland Dreier target->req_lim = be32_to_cpu(rsp->req_lim_delta); 1338aef9ec39SRoland Dreier 13397ade400aSBart Van Assche /* 13407ade400aSBart Van Assche * Reserve credits for task management so we don't 13417ade400aSBart Van Assche * bounce requests back to the SCSI mid-layer. 13427ade400aSBart Van Assche */ 13437ade400aSBart Van Assche target->scsi_host->can_queue 13447ade400aSBart Van Assche = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE, 1345aef9ec39SRoland Dreier target->scsi_host->can_queue); 1346aef9ec39SRoland Dreier } else { 13477aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 13487aa54bd7SDavid Dillow PFX "Unhandled RSP opcode %#x\n", opcode); 1349aef9ec39SRoland Dreier target->status = -ECONNRESET; 1350aef9ec39SRoland Dreier break; 1351aef9ec39SRoland Dreier } 1352aef9ec39SRoland Dreier 1353d2fcea7dSVu Pham if (!target->rx_ring[0]) { 1354aef9ec39SRoland Dreier target->status = srp_alloc_iu_bufs(target); 1355aef9ec39SRoland Dreier if (target->status) 1356aef9ec39SRoland Dreier break; 1357d2fcea7dSVu Pham } 1358aef9ec39SRoland Dreier 1359aef9ec39SRoland Dreier qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL); 1360aef9ec39SRoland Dreier if (!qp_attr) { 1361aef9ec39SRoland Dreier target->status = -ENOMEM; 1362aef9ec39SRoland Dreier break; 1363aef9ec39SRoland Dreier } 1364aef9ec39SRoland Dreier 1365aef9ec39SRoland Dreier qp_attr->qp_state = IB_QPS_RTR; 1366aef9ec39SRoland Dreier target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); 1367aef9ec39SRoland Dreier if (target->status) 1368aef9ec39SRoland Dreier break; 1369aef9ec39SRoland Dreier 1370aef9ec39SRoland Dreier target->status = ib_modify_qp(target->qp, qp_attr, attr_mask); 1371aef9ec39SRoland Dreier if (target->status) 1372aef9ec39SRoland Dreier break; 1373aef9ec39SRoland Dreier 1374c996bb47SBart Van Assche for (i = 0; i < SRP_RQ_SIZE; i++) { 1375dcb4cb85SBart Van Assche struct srp_iu *iu = target->rx_ring[i]; 1376dcb4cb85SBart Van Assche target->status = srp_post_recv(target, iu); 1377aef9ec39SRoland Dreier if (target->status) 1378aef9ec39SRoland Dreier break; 1379c996bb47SBart Van Assche } 1380c996bb47SBart Van Assche if (target->status) 1381c996bb47SBart Van Assche break; 1382aef9ec39SRoland Dreier 1383aef9ec39SRoland Dreier qp_attr->qp_state = IB_QPS_RTS; 1384aef9ec39SRoland Dreier target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); 1385aef9ec39SRoland Dreier if (target->status) 1386aef9ec39SRoland Dreier break; 1387aef9ec39SRoland Dreier 1388aef9ec39SRoland Dreier target->status = ib_modify_qp(target->qp, qp_attr, attr_mask); 1389aef9ec39SRoland Dreier if (target->status) 1390aef9ec39SRoland Dreier break; 1391aef9ec39SRoland Dreier 1392aef9ec39SRoland Dreier target->status = ib_send_cm_rtu(cm_id, NULL, 0); 1393aef9ec39SRoland Dreier if (target->status) 1394aef9ec39SRoland Dreier break; 1395aef9ec39SRoland Dreier 1396aef9ec39SRoland Dreier break; 1397aef9ec39SRoland Dreier 1398aef9ec39SRoland Dreier case IB_CM_REJ_RECEIVED: 13997aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n"); 1400aef9ec39SRoland Dreier comp = 1; 1401aef9ec39SRoland Dreier 1402aef9ec39SRoland Dreier srp_cm_rej_handler(cm_id, event, target); 1403aef9ec39SRoland Dreier break; 1404aef9ec39SRoland Dreier 1405b7ac4ab4SIshai Rabinovitz case IB_CM_DREQ_RECEIVED: 14067aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 14077aa54bd7SDavid Dillow PFX "DREQ received - connection closed\n"); 1408b7ac4ab4SIshai Rabinovitz if (ib_send_cm_drep(cm_id, NULL, 0)) 14097aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 14107aa54bd7SDavid Dillow PFX "Sending CM DREP failed\n"); 1411aef9ec39SRoland Dreier break; 1412aef9ec39SRoland Dreier 1413aef9ec39SRoland Dreier case IB_CM_TIMEWAIT_EXIT: 14147aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 14157aa54bd7SDavid Dillow PFX "connection closed\n"); 1416aef9ec39SRoland Dreier 1417aef9ec39SRoland Dreier comp = 1; 1418aef9ec39SRoland Dreier target->status = 0; 1419aef9ec39SRoland Dreier break; 1420aef9ec39SRoland Dreier 1421b7ac4ab4SIshai Rabinovitz case IB_CM_MRA_RECEIVED: 1422b7ac4ab4SIshai Rabinovitz case IB_CM_DREQ_ERROR: 1423b7ac4ab4SIshai Rabinovitz case IB_CM_DREP_RECEIVED: 1424b7ac4ab4SIshai Rabinovitz break; 1425b7ac4ab4SIshai Rabinovitz 1426aef9ec39SRoland Dreier default: 14277aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 14287aa54bd7SDavid Dillow PFX "Unhandled CM event %d\n", event->event); 1429aef9ec39SRoland Dreier break; 1430aef9ec39SRoland Dreier } 1431aef9ec39SRoland Dreier 1432aef9ec39SRoland Dreier if (comp) 1433aef9ec39SRoland Dreier complete(&target->done); 1434aef9ec39SRoland Dreier 1435aef9ec39SRoland Dreier kfree(qp_attr); 1436aef9ec39SRoland Dreier 1437aef9ec39SRoland Dreier return 0; 1438aef9ec39SRoland Dreier } 1439aef9ec39SRoland Dreier 1440d945e1dfSRoland Dreier static int srp_send_tsk_mgmt(struct srp_target_port *target, 1441f8b6e31eSDavid Dillow u64 req_tag, unsigned int lun, u8 func) 1442aef9ec39SRoland Dreier { 144319081f31SDavid Dillow struct ib_device *dev = target->srp_host->srp_dev->dev; 1444aef9ec39SRoland Dreier struct srp_iu *iu; 1445aef9ec39SRoland Dreier struct srp_tsk_mgmt *tsk_mgmt; 1446aef9ec39SRoland Dreier 14471285b3a0SRoland Dreier if (target->state == SRP_TARGET_DEAD || 1448f8b6e31eSDavid Dillow target->state == SRP_TARGET_REMOVED) 144976c75b25SBart Van Assche return -1; 14501285b3a0SRoland Dreier 1451f8b6e31eSDavid Dillow init_completion(&target->tsk_mgmt_done); 1452aef9ec39SRoland Dreier 1453e9684678SBart Van Assche spin_lock_irq(&target->lock); 1454bb12588aSDavid Dillow iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT); 1455e9684678SBart Van Assche spin_unlock_irq(&target->lock); 145676c75b25SBart Van Assche 1457aef9ec39SRoland Dreier if (!iu) 145876c75b25SBart Van Assche return -1; 1459aef9ec39SRoland Dreier 146019081f31SDavid Dillow ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt, 146119081f31SDavid Dillow DMA_TO_DEVICE); 1462aef9ec39SRoland Dreier tsk_mgmt = iu->buf; 1463aef9ec39SRoland Dreier memset(tsk_mgmt, 0, sizeof *tsk_mgmt); 1464aef9ec39SRoland Dreier 1465aef9ec39SRoland Dreier tsk_mgmt->opcode = SRP_TSK_MGMT; 1466f8b6e31eSDavid Dillow tsk_mgmt->lun = cpu_to_be64((u64) lun << 48); 1467f8b6e31eSDavid Dillow tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT; 1468aef9ec39SRoland Dreier tsk_mgmt->tsk_mgmt_func = func; 1469f8b6e31eSDavid Dillow tsk_mgmt->task_tag = req_tag; 1470aef9ec39SRoland Dreier 147119081f31SDavid Dillow ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt, 147219081f31SDavid Dillow DMA_TO_DEVICE); 147376c75b25SBart Van Assche if (srp_post_send(target, iu, sizeof *tsk_mgmt)) { 147476c75b25SBart Van Assche srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT); 147576c75b25SBart Van Assche return -1; 147676c75b25SBart Van Assche } 1477d945e1dfSRoland Dreier 1478f8b6e31eSDavid Dillow if (!wait_for_completion_timeout(&target->tsk_mgmt_done, 1479aef9ec39SRoland Dreier msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS))) 1480d945e1dfSRoland Dreier return -1; 1481aef9ec39SRoland Dreier 1482d945e1dfSRoland Dreier return 0; 1483d945e1dfSRoland Dreier } 1484d945e1dfSRoland Dreier 1485aef9ec39SRoland Dreier static int srp_abort(struct scsi_cmnd *scmnd) 1486aef9ec39SRoland Dreier { 1487d945e1dfSRoland Dreier struct srp_target_port *target = host_to_target(scmnd->device->host); 1488f8b6e31eSDavid Dillow struct srp_request *req = (struct srp_request *) scmnd->host_scribble; 1489d945e1dfSRoland Dreier int ret = SUCCESS; 1490d945e1dfSRoland Dreier 14917aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n"); 1492aef9ec39SRoland Dreier 1493f8b6e31eSDavid Dillow if (!req || target->qp_in_error) 14941033ff67SIshai Rabinovitz return FAILED; 1495f8b6e31eSDavid Dillow if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun, 1496f8b6e31eSDavid Dillow SRP_TSK_ABORT_TASK)) 1497d945e1dfSRoland Dreier return FAILED; 1498d945e1dfSRoland Dreier 1499f8b6e31eSDavid Dillow if (req->scmnd) { 1500f8b6e31eSDavid Dillow if (!target->tsk_mgmt_status) { 150194a9174cSBart Van Assche srp_remove_req(target, req, 0); 1502d945e1dfSRoland Dreier scmnd->result = DID_ABORT << 16; 1503d945e1dfSRoland Dreier } else 1504d945e1dfSRoland Dreier ret = FAILED; 1505f8b6e31eSDavid Dillow } 1506d945e1dfSRoland Dreier 1507d945e1dfSRoland Dreier return ret; 1508aef9ec39SRoland Dreier } 1509aef9ec39SRoland Dreier 1510aef9ec39SRoland Dreier static int srp_reset_device(struct scsi_cmnd *scmnd) 1511aef9ec39SRoland Dreier { 1512d945e1dfSRoland Dreier struct srp_target_port *target = host_to_target(scmnd->device->host); 1513536ae14eSBart Van Assche int i; 1514d945e1dfSRoland Dreier 15157aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n"); 1516aef9ec39SRoland Dreier 15171033ff67SIshai Rabinovitz if (target->qp_in_error) 15181033ff67SIshai Rabinovitz return FAILED; 1519f8b6e31eSDavid Dillow if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun, 1520f8b6e31eSDavid Dillow SRP_TSK_LUN_RESET)) 1521d945e1dfSRoland Dreier return FAILED; 1522f8b6e31eSDavid Dillow if (target->tsk_mgmt_status) 1523d945e1dfSRoland Dreier return FAILED; 1524d945e1dfSRoland Dreier 1525536ae14eSBart Van Assche for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { 1526536ae14eSBart Van Assche struct srp_request *req = &target->req_ring[i]; 1527f8b6e31eSDavid Dillow if (req->scmnd && req->scmnd->device == scmnd->device) 1528526b4caaSIshai Rabinovitz srp_reset_req(target, req); 1529536ae14eSBart Van Assche } 1530d945e1dfSRoland Dreier 1531d945e1dfSRoland Dreier return SUCCESS; 1532aef9ec39SRoland Dreier } 1533aef9ec39SRoland Dreier 1534aef9ec39SRoland Dreier static int srp_reset_host(struct scsi_cmnd *scmnd) 1535aef9ec39SRoland Dreier { 1536aef9ec39SRoland Dreier struct srp_target_port *target = host_to_target(scmnd->device->host); 1537aef9ec39SRoland Dreier int ret = FAILED; 1538aef9ec39SRoland Dreier 15397aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n"); 1540aef9ec39SRoland Dreier 1541aef9ec39SRoland Dreier if (!srp_reconnect_target(target)) 1542aef9ec39SRoland Dreier ret = SUCCESS; 1543aef9ec39SRoland Dreier 1544aef9ec39SRoland Dreier return ret; 1545aef9ec39SRoland Dreier } 1546aef9ec39SRoland Dreier 1547ee959b00STony Jones static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr, 1548ee959b00STony Jones char *buf) 15496ecb0c84SRoland Dreier { 1550ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15516ecb0c84SRoland Dreier 15526ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 15536ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 15546ecb0c84SRoland Dreier return -ENODEV; 15556ecb0c84SRoland Dreier 15566ecb0c84SRoland Dreier return sprintf(buf, "0x%016llx\n", 15576ecb0c84SRoland Dreier (unsigned long long) be64_to_cpu(target->id_ext)); 15586ecb0c84SRoland Dreier } 15596ecb0c84SRoland Dreier 1560ee959b00STony Jones static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr, 1561ee959b00STony Jones char *buf) 15626ecb0c84SRoland Dreier { 1563ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15646ecb0c84SRoland Dreier 15656ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 15666ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 15676ecb0c84SRoland Dreier return -ENODEV; 15686ecb0c84SRoland Dreier 15696ecb0c84SRoland Dreier return sprintf(buf, "0x%016llx\n", 15706ecb0c84SRoland Dreier (unsigned long long) be64_to_cpu(target->ioc_guid)); 15716ecb0c84SRoland Dreier } 15726ecb0c84SRoland Dreier 1573ee959b00STony Jones static ssize_t show_service_id(struct device *dev, 1574ee959b00STony Jones struct device_attribute *attr, char *buf) 15756ecb0c84SRoland Dreier { 1576ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15776ecb0c84SRoland Dreier 15786ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 15796ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 15806ecb0c84SRoland Dreier return -ENODEV; 15816ecb0c84SRoland Dreier 15826ecb0c84SRoland Dreier return sprintf(buf, "0x%016llx\n", 15836ecb0c84SRoland Dreier (unsigned long long) be64_to_cpu(target->service_id)); 15846ecb0c84SRoland Dreier } 15856ecb0c84SRoland Dreier 1586ee959b00STony Jones static ssize_t show_pkey(struct device *dev, struct device_attribute *attr, 1587ee959b00STony Jones char *buf) 15886ecb0c84SRoland Dreier { 1589ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15906ecb0c84SRoland Dreier 15916ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 15926ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 15936ecb0c84SRoland Dreier return -ENODEV; 15946ecb0c84SRoland Dreier 15956ecb0c84SRoland Dreier return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey)); 15966ecb0c84SRoland Dreier } 15976ecb0c84SRoland Dreier 1598ee959b00STony Jones static ssize_t show_dgid(struct device *dev, struct device_attribute *attr, 1599ee959b00STony Jones char *buf) 16006ecb0c84SRoland Dreier { 1601ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 16026ecb0c84SRoland Dreier 16036ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 16046ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 16056ecb0c84SRoland Dreier return -ENODEV; 16066ecb0c84SRoland Dreier 16075b095d98SHarvey Harrison return sprintf(buf, "%pI6\n", target->path.dgid.raw); 16086ecb0c84SRoland Dreier } 16096ecb0c84SRoland Dreier 1610ee959b00STony Jones static ssize_t show_orig_dgid(struct device *dev, 1611ee959b00STony Jones struct device_attribute *attr, char *buf) 16123633b3d0SIshai Rabinovitz { 1613ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 16143633b3d0SIshai Rabinovitz 16153633b3d0SIshai Rabinovitz if (target->state == SRP_TARGET_DEAD || 16163633b3d0SIshai Rabinovitz target->state == SRP_TARGET_REMOVED) 16173633b3d0SIshai Rabinovitz return -ENODEV; 16183633b3d0SIshai Rabinovitz 16195b095d98SHarvey Harrison return sprintf(buf, "%pI6\n", target->orig_dgid); 16203633b3d0SIshai Rabinovitz } 16213633b3d0SIshai Rabinovitz 162289de7486SBart Van Assche static ssize_t show_req_lim(struct device *dev, 162389de7486SBart Van Assche struct device_attribute *attr, char *buf) 162489de7486SBart Van Assche { 162589de7486SBart Van Assche struct srp_target_port *target = host_to_target(class_to_shost(dev)); 162689de7486SBart Van Assche 162789de7486SBart Van Assche if (target->state == SRP_TARGET_DEAD || 162889de7486SBart Van Assche target->state == SRP_TARGET_REMOVED) 162989de7486SBart Van Assche return -ENODEV; 163089de7486SBart Van Assche 163189de7486SBart Van Assche return sprintf(buf, "%d\n", target->req_lim); 163289de7486SBart Van Assche } 163389de7486SBart Van Assche 1634ee959b00STony Jones static ssize_t show_zero_req_lim(struct device *dev, 1635ee959b00STony Jones struct device_attribute *attr, char *buf) 16366bfa24faSRoland Dreier { 1637ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 16386bfa24faSRoland Dreier 16396bfa24faSRoland Dreier if (target->state == SRP_TARGET_DEAD || 16406bfa24faSRoland Dreier target->state == SRP_TARGET_REMOVED) 16416bfa24faSRoland Dreier return -ENODEV; 16426bfa24faSRoland Dreier 16436bfa24faSRoland Dreier return sprintf(buf, "%d\n", target->zero_req_lim); 16446bfa24faSRoland Dreier } 16456bfa24faSRoland Dreier 1646ee959b00STony Jones static ssize_t show_local_ib_port(struct device *dev, 1647ee959b00STony Jones struct device_attribute *attr, char *buf) 1648ded7f1a1SIshai Rabinovitz { 1649ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 1650ded7f1a1SIshai Rabinovitz 1651ded7f1a1SIshai Rabinovitz return sprintf(buf, "%d\n", target->srp_host->port); 1652ded7f1a1SIshai Rabinovitz } 1653ded7f1a1SIshai Rabinovitz 1654ee959b00STony Jones static ssize_t show_local_ib_device(struct device *dev, 1655ee959b00STony Jones struct device_attribute *attr, char *buf) 1656ded7f1a1SIshai Rabinovitz { 1657ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 1658ded7f1a1SIshai Rabinovitz 165905321937SGreg Kroah-Hartman return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name); 1660ded7f1a1SIshai Rabinovitz } 1661ded7f1a1SIshai Rabinovitz 1662ee959b00STony Jones static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL); 1663ee959b00STony Jones static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL); 1664ee959b00STony Jones static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL); 1665ee959b00STony Jones static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); 1666ee959b00STony Jones static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL); 1667ee959b00STony Jones static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL); 166889de7486SBart Van Assche static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL); 1669ee959b00STony Jones static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL); 1670ee959b00STony Jones static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL); 1671ee959b00STony Jones static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL); 16726ecb0c84SRoland Dreier 1673ee959b00STony Jones static struct device_attribute *srp_host_attrs[] = { 1674ee959b00STony Jones &dev_attr_id_ext, 1675ee959b00STony Jones &dev_attr_ioc_guid, 1676ee959b00STony Jones &dev_attr_service_id, 1677ee959b00STony Jones &dev_attr_pkey, 1678ee959b00STony Jones &dev_attr_dgid, 1679ee959b00STony Jones &dev_attr_orig_dgid, 168089de7486SBart Van Assche &dev_attr_req_lim, 1681ee959b00STony Jones &dev_attr_zero_req_lim, 1682ee959b00STony Jones &dev_attr_local_ib_port, 1683ee959b00STony Jones &dev_attr_local_ib_device, 16846ecb0c84SRoland Dreier NULL 16856ecb0c84SRoland Dreier }; 16866ecb0c84SRoland Dreier 1687aef9ec39SRoland Dreier static struct scsi_host_template srp_template = { 1688aef9ec39SRoland Dreier .module = THIS_MODULE, 1689b7f008fdSRoland Dreier .name = "InfiniBand SRP initiator", 1690b7f008fdSRoland Dreier .proc_name = DRV_NAME, 1691aef9ec39SRoland Dreier .info = srp_target_info, 1692aef9ec39SRoland Dreier .queuecommand = srp_queuecommand, 1693aef9ec39SRoland Dreier .eh_abort_handler = srp_abort, 1694aef9ec39SRoland Dreier .eh_device_reset_handler = srp_reset_device, 1695aef9ec39SRoland Dreier .eh_host_reset_handler = srp_reset_host, 1696dd5e6e38SBart Van Assche .can_queue = SRP_CMD_SQ_SIZE, 1697aef9ec39SRoland Dreier .this_id = -1, 1698dd5e6e38SBart Van Assche .cmd_per_lun = SRP_CMD_SQ_SIZE, 16996ecb0c84SRoland Dreier .use_clustering = ENABLE_CLUSTERING, 17006ecb0c84SRoland Dreier .shost_attrs = srp_host_attrs 1701aef9ec39SRoland Dreier }; 1702aef9ec39SRoland Dreier 1703aef9ec39SRoland Dreier static int srp_add_target(struct srp_host *host, struct srp_target_port *target) 1704aef9ec39SRoland Dreier { 17053236822bSFUJITA Tomonori struct srp_rport_identifiers ids; 17063236822bSFUJITA Tomonori struct srp_rport *rport; 17073236822bSFUJITA Tomonori 1708aef9ec39SRoland Dreier sprintf(target->target_name, "SRP.T10:%016llX", 1709aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->id_ext)); 1710aef9ec39SRoland Dreier 171105321937SGreg Kroah-Hartman if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device)) 1712aef9ec39SRoland Dreier return -ENODEV; 1713aef9ec39SRoland Dreier 17143236822bSFUJITA Tomonori memcpy(ids.port_id, &target->id_ext, 8); 17153236822bSFUJITA Tomonori memcpy(ids.port_id + 8, &target->ioc_guid, 8); 1716aebd5e47SFUJITA Tomonori ids.roles = SRP_RPORT_ROLE_TARGET; 17173236822bSFUJITA Tomonori rport = srp_rport_add(target->scsi_host, &ids); 17183236822bSFUJITA Tomonori if (IS_ERR(rport)) { 17193236822bSFUJITA Tomonori scsi_remove_host(target->scsi_host); 17203236822bSFUJITA Tomonori return PTR_ERR(rport); 17213236822bSFUJITA Tomonori } 17223236822bSFUJITA Tomonori 1723b3589fd4SMatthew Wilcox spin_lock(&host->target_lock); 1724aef9ec39SRoland Dreier list_add_tail(&target->list, &host->target_list); 1725b3589fd4SMatthew Wilcox spin_unlock(&host->target_lock); 1726aef9ec39SRoland Dreier 1727aef9ec39SRoland Dreier target->state = SRP_TARGET_LIVE; 1728aef9ec39SRoland Dreier 1729aef9ec39SRoland Dreier scsi_scan_target(&target->scsi_host->shost_gendev, 17301962a4a1SMatthew Wilcox 0, target->scsi_id, SCAN_WILD_CARD, 0); 1731aef9ec39SRoland Dreier 1732aef9ec39SRoland Dreier return 0; 1733aef9ec39SRoland Dreier } 1734aef9ec39SRoland Dreier 1735ee959b00STony Jones static void srp_release_dev(struct device *dev) 1736aef9ec39SRoland Dreier { 1737aef9ec39SRoland Dreier struct srp_host *host = 1738ee959b00STony Jones container_of(dev, struct srp_host, dev); 1739aef9ec39SRoland Dreier 1740aef9ec39SRoland Dreier complete(&host->released); 1741aef9ec39SRoland Dreier } 1742aef9ec39SRoland Dreier 1743aef9ec39SRoland Dreier static struct class srp_class = { 1744aef9ec39SRoland Dreier .name = "infiniband_srp", 1745ee959b00STony Jones .dev_release = srp_release_dev 1746aef9ec39SRoland Dreier }; 1747aef9ec39SRoland Dreier 1748aef9ec39SRoland Dreier /* 1749aef9ec39SRoland Dreier * Target ports are added by writing 1750aef9ec39SRoland Dreier * 1751aef9ec39SRoland Dreier * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>, 1752aef9ec39SRoland Dreier * pkey=<P_Key>,service_id=<service ID> 1753aef9ec39SRoland Dreier * 1754aef9ec39SRoland Dreier * to the add_target sysfs attribute. 1755aef9ec39SRoland Dreier */ 1756aef9ec39SRoland Dreier enum { 1757aef9ec39SRoland Dreier SRP_OPT_ERR = 0, 1758aef9ec39SRoland Dreier SRP_OPT_ID_EXT = 1 << 0, 1759aef9ec39SRoland Dreier SRP_OPT_IOC_GUID = 1 << 1, 1760aef9ec39SRoland Dreier SRP_OPT_DGID = 1 << 2, 1761aef9ec39SRoland Dreier SRP_OPT_PKEY = 1 << 3, 1762aef9ec39SRoland Dreier SRP_OPT_SERVICE_ID = 1 << 4, 1763aef9ec39SRoland Dreier SRP_OPT_MAX_SECT = 1 << 5, 176452fb2b50SVu Pham SRP_OPT_MAX_CMD_PER_LUN = 1 << 6, 17650c0450dbSRamachandra K SRP_OPT_IO_CLASS = 1 << 7, 176601cb9bcbSIshai Rabinovitz SRP_OPT_INITIATOR_EXT = 1 << 8, 1767aef9ec39SRoland Dreier SRP_OPT_ALL = (SRP_OPT_ID_EXT | 1768aef9ec39SRoland Dreier SRP_OPT_IOC_GUID | 1769aef9ec39SRoland Dreier SRP_OPT_DGID | 1770aef9ec39SRoland Dreier SRP_OPT_PKEY | 1771aef9ec39SRoland Dreier SRP_OPT_SERVICE_ID), 1772aef9ec39SRoland Dreier }; 1773aef9ec39SRoland Dreier 1774a447c093SSteven Whitehouse static const match_table_t srp_opt_tokens = { 1775aef9ec39SRoland Dreier { SRP_OPT_ID_EXT, "id_ext=%s" }, 1776aef9ec39SRoland Dreier { SRP_OPT_IOC_GUID, "ioc_guid=%s" }, 1777aef9ec39SRoland Dreier { SRP_OPT_DGID, "dgid=%s" }, 1778aef9ec39SRoland Dreier { SRP_OPT_PKEY, "pkey=%x" }, 1779aef9ec39SRoland Dreier { SRP_OPT_SERVICE_ID, "service_id=%s" }, 1780aef9ec39SRoland Dreier { SRP_OPT_MAX_SECT, "max_sect=%d" }, 178152fb2b50SVu Pham { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" }, 17820c0450dbSRamachandra K { SRP_OPT_IO_CLASS, "io_class=%x" }, 178301cb9bcbSIshai Rabinovitz { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" }, 1784aef9ec39SRoland Dreier { SRP_OPT_ERR, NULL } 1785aef9ec39SRoland Dreier }; 1786aef9ec39SRoland Dreier 1787aef9ec39SRoland Dreier static int srp_parse_options(const char *buf, struct srp_target_port *target) 1788aef9ec39SRoland Dreier { 1789aef9ec39SRoland Dreier char *options, *sep_opt; 1790aef9ec39SRoland Dreier char *p; 1791aef9ec39SRoland Dreier char dgid[3]; 1792aef9ec39SRoland Dreier substring_t args[MAX_OPT_ARGS]; 1793aef9ec39SRoland Dreier int opt_mask = 0; 1794aef9ec39SRoland Dreier int token; 1795aef9ec39SRoland Dreier int ret = -EINVAL; 1796aef9ec39SRoland Dreier int i; 1797aef9ec39SRoland Dreier 1798aef9ec39SRoland Dreier options = kstrdup(buf, GFP_KERNEL); 1799aef9ec39SRoland Dreier if (!options) 1800aef9ec39SRoland Dreier return -ENOMEM; 1801aef9ec39SRoland Dreier 1802aef9ec39SRoland Dreier sep_opt = options; 1803aef9ec39SRoland Dreier while ((p = strsep(&sep_opt, ",")) != NULL) { 1804aef9ec39SRoland Dreier if (!*p) 1805aef9ec39SRoland Dreier continue; 1806aef9ec39SRoland Dreier 1807aef9ec39SRoland Dreier token = match_token(p, srp_opt_tokens, args); 1808aef9ec39SRoland Dreier opt_mask |= token; 1809aef9ec39SRoland Dreier 1810aef9ec39SRoland Dreier switch (token) { 1811aef9ec39SRoland Dreier case SRP_OPT_ID_EXT: 1812aef9ec39SRoland Dreier p = match_strdup(args); 1813a20f3a6dSIshai Rabinovitz if (!p) { 1814a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1815a20f3a6dSIshai Rabinovitz goto out; 1816a20f3a6dSIshai Rabinovitz } 1817aef9ec39SRoland Dreier target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); 1818aef9ec39SRoland Dreier kfree(p); 1819aef9ec39SRoland Dreier break; 1820aef9ec39SRoland Dreier 1821aef9ec39SRoland Dreier case SRP_OPT_IOC_GUID: 1822aef9ec39SRoland Dreier p = match_strdup(args); 1823a20f3a6dSIshai Rabinovitz if (!p) { 1824a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1825a20f3a6dSIshai Rabinovitz goto out; 1826a20f3a6dSIshai Rabinovitz } 1827aef9ec39SRoland Dreier target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16)); 1828aef9ec39SRoland Dreier kfree(p); 1829aef9ec39SRoland Dreier break; 1830aef9ec39SRoland Dreier 1831aef9ec39SRoland Dreier case SRP_OPT_DGID: 1832aef9ec39SRoland Dreier p = match_strdup(args); 1833a20f3a6dSIshai Rabinovitz if (!p) { 1834a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1835a20f3a6dSIshai Rabinovitz goto out; 1836a20f3a6dSIshai Rabinovitz } 1837aef9ec39SRoland Dreier if (strlen(p) != 32) { 1838aef9ec39SRoland Dreier printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p); 1839ce1823f0SRoland Dreier kfree(p); 1840aef9ec39SRoland Dreier goto out; 1841aef9ec39SRoland Dreier } 1842aef9ec39SRoland Dreier 1843aef9ec39SRoland Dreier for (i = 0; i < 16; ++i) { 1844aef9ec39SRoland Dreier strlcpy(dgid, p + i * 2, 3); 1845aef9ec39SRoland Dreier target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16); 1846aef9ec39SRoland Dreier } 1847bf17c1c7SRoland Dreier kfree(p); 18483633b3d0SIshai Rabinovitz memcpy(target->orig_dgid, target->path.dgid.raw, 16); 1849aef9ec39SRoland Dreier break; 1850aef9ec39SRoland Dreier 1851aef9ec39SRoland Dreier case SRP_OPT_PKEY: 1852aef9ec39SRoland Dreier if (match_hex(args, &token)) { 1853aef9ec39SRoland Dreier printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p); 1854aef9ec39SRoland Dreier goto out; 1855aef9ec39SRoland Dreier } 1856aef9ec39SRoland Dreier target->path.pkey = cpu_to_be16(token); 1857aef9ec39SRoland Dreier break; 1858aef9ec39SRoland Dreier 1859aef9ec39SRoland Dreier case SRP_OPT_SERVICE_ID: 1860aef9ec39SRoland Dreier p = match_strdup(args); 1861a20f3a6dSIshai Rabinovitz if (!p) { 1862a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1863a20f3a6dSIshai Rabinovitz goto out; 1864a20f3a6dSIshai Rabinovitz } 1865aef9ec39SRoland Dreier target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16)); 1866247e020eSSean Hefty target->path.service_id = target->service_id; 1867aef9ec39SRoland Dreier kfree(p); 1868aef9ec39SRoland Dreier break; 1869aef9ec39SRoland Dreier 1870aef9ec39SRoland Dreier case SRP_OPT_MAX_SECT: 1871aef9ec39SRoland Dreier if (match_int(args, &token)) { 1872aef9ec39SRoland Dreier printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p); 1873aef9ec39SRoland Dreier goto out; 1874aef9ec39SRoland Dreier } 1875aef9ec39SRoland Dreier target->scsi_host->max_sectors = token; 1876aef9ec39SRoland Dreier break; 1877aef9ec39SRoland Dreier 187852fb2b50SVu Pham case SRP_OPT_MAX_CMD_PER_LUN: 187952fb2b50SVu Pham if (match_int(args, &token)) { 188052fb2b50SVu Pham printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p); 188152fb2b50SVu Pham goto out; 188252fb2b50SVu Pham } 1883dd5e6e38SBart Van Assche target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE); 188452fb2b50SVu Pham break; 188552fb2b50SVu Pham 18860c0450dbSRamachandra K case SRP_OPT_IO_CLASS: 18870c0450dbSRamachandra K if (match_hex(args, &token)) { 18880c0450dbSRamachandra K printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p); 18890c0450dbSRamachandra K goto out; 18900c0450dbSRamachandra K } 18910c0450dbSRamachandra K if (token != SRP_REV10_IB_IO_CLASS && 18920c0450dbSRamachandra K token != SRP_REV16A_IB_IO_CLASS) { 18930c0450dbSRamachandra K printk(KERN_WARNING PFX "unknown IO class parameter value" 18940c0450dbSRamachandra K " %x specified (use %x or %x).\n", 18950c0450dbSRamachandra K token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS); 18960c0450dbSRamachandra K goto out; 18970c0450dbSRamachandra K } 18980c0450dbSRamachandra K target->io_class = token; 18990c0450dbSRamachandra K break; 19000c0450dbSRamachandra K 190101cb9bcbSIshai Rabinovitz case SRP_OPT_INITIATOR_EXT: 190201cb9bcbSIshai Rabinovitz p = match_strdup(args); 1903a20f3a6dSIshai Rabinovitz if (!p) { 1904a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1905a20f3a6dSIshai Rabinovitz goto out; 1906a20f3a6dSIshai Rabinovitz } 190701cb9bcbSIshai Rabinovitz target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); 190801cb9bcbSIshai Rabinovitz kfree(p); 190901cb9bcbSIshai Rabinovitz break; 191001cb9bcbSIshai Rabinovitz 1911aef9ec39SRoland Dreier default: 1912aef9ec39SRoland Dreier printk(KERN_WARNING PFX "unknown parameter or missing value " 1913aef9ec39SRoland Dreier "'%s' in target creation request\n", p); 1914aef9ec39SRoland Dreier goto out; 1915aef9ec39SRoland Dreier } 1916aef9ec39SRoland Dreier } 1917aef9ec39SRoland Dreier 1918aef9ec39SRoland Dreier if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL) 1919aef9ec39SRoland Dreier ret = 0; 1920aef9ec39SRoland Dreier else 1921aef9ec39SRoland Dreier for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i) 1922aef9ec39SRoland Dreier if ((srp_opt_tokens[i].token & SRP_OPT_ALL) && 1923aef9ec39SRoland Dreier !(srp_opt_tokens[i].token & opt_mask)) 1924aef9ec39SRoland Dreier printk(KERN_WARNING PFX "target creation request is " 1925aef9ec39SRoland Dreier "missing parameter '%s'\n", 1926aef9ec39SRoland Dreier srp_opt_tokens[i].pattern); 1927aef9ec39SRoland Dreier 1928aef9ec39SRoland Dreier out: 1929aef9ec39SRoland Dreier kfree(options); 1930aef9ec39SRoland Dreier return ret; 1931aef9ec39SRoland Dreier } 1932aef9ec39SRoland Dreier 1933ee959b00STony Jones static ssize_t srp_create_target(struct device *dev, 1934ee959b00STony Jones struct device_attribute *attr, 1935aef9ec39SRoland Dreier const char *buf, size_t count) 1936aef9ec39SRoland Dreier { 1937aef9ec39SRoland Dreier struct srp_host *host = 1938ee959b00STony Jones container_of(dev, struct srp_host, dev); 1939aef9ec39SRoland Dreier struct Scsi_Host *target_host; 1940aef9ec39SRoland Dreier struct srp_target_port *target; 1941aef9ec39SRoland Dreier int ret; 1942aef9ec39SRoland Dreier int i; 1943aef9ec39SRoland Dreier 1944aef9ec39SRoland Dreier target_host = scsi_host_alloc(&srp_template, 1945aef9ec39SRoland Dreier sizeof (struct srp_target_port)); 1946aef9ec39SRoland Dreier if (!target_host) 1947aef9ec39SRoland Dreier return -ENOMEM; 1948aef9ec39SRoland Dreier 19493236822bSFUJITA Tomonori target_host->transportt = ib_srp_transport_template; 19505f068992SRoland Dreier target_host->max_lun = SRP_MAX_LUN; 19513c8edf0eSArne Redlich target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb; 19525f068992SRoland Dreier 1953aef9ec39SRoland Dreier target = host_to_target(target_host); 1954aef9ec39SRoland Dreier 19550c0450dbSRamachandra K target->io_class = SRP_REV16A_IB_IO_CLASS; 1956aef9ec39SRoland Dreier target->scsi_host = target_host; 1957aef9ec39SRoland Dreier target->srp_host = host; 19589af76271SDavid Dillow target->lkey = host->srp_dev->mr->lkey; 19599af76271SDavid Dillow target->rkey = host->srp_dev->mr->rkey; 1960aef9ec39SRoland Dreier 1961e9684678SBart Van Assche spin_lock_init(&target->lock); 1962dcb4cb85SBart Van Assche INIT_LIST_HEAD(&target->free_tx); 1963d945e1dfSRoland Dreier INIT_LIST_HEAD(&target->free_reqs); 1964dd5e6e38SBart Van Assche for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { 1965d945e1dfSRoland Dreier target->req_ring[i].index = i; 1966d945e1dfSRoland Dreier list_add_tail(&target->req_ring[i].list, &target->free_reqs); 1967d945e1dfSRoland Dreier } 1968aef9ec39SRoland Dreier 1969aef9ec39SRoland Dreier ret = srp_parse_options(buf, target); 1970aef9ec39SRoland Dreier if (ret) 1971aef9ec39SRoland Dreier goto err; 1972aef9ec39SRoland Dreier 1973969a60f9SRoland Dreier ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid); 1974aef9ec39SRoland Dreier 19757aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, PFX 19767aa54bd7SDavid Dillow "new target: id_ext %016llx ioc_guid %016llx pkey %04x " 19775b095d98SHarvey Harrison "service_id %016llx dgid %pI6\n", 1978aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->id_ext), 1979aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->ioc_guid), 1980aef9ec39SRoland Dreier be16_to_cpu(target->path.pkey), 1981aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->service_id), 19828867cd7cSHarvey Harrison target->path.dgid.raw); 1983aef9ec39SRoland Dreier 1984aef9ec39SRoland Dreier ret = srp_create_target_ib(target); 1985aef9ec39SRoland Dreier if (ret) 1986aef9ec39SRoland Dreier goto err; 1987aef9ec39SRoland Dreier 19889fe4bcf4SDavid Dillow ret = srp_new_cm_id(target); 19899fe4bcf4SDavid Dillow if (ret) 1990aef9ec39SRoland Dreier goto err_free; 1991aef9ec39SRoland Dreier 19921033ff67SIshai Rabinovitz target->qp_in_error = 0; 1993aef9ec39SRoland Dreier ret = srp_connect_target(target); 1994aef9ec39SRoland Dreier if (ret) { 19957aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 19967aa54bd7SDavid Dillow PFX "Connection failed\n"); 1997aef9ec39SRoland Dreier goto err_cm_id; 1998aef9ec39SRoland Dreier } 1999aef9ec39SRoland Dreier 2000aef9ec39SRoland Dreier ret = srp_add_target(host, target); 2001aef9ec39SRoland Dreier if (ret) 2002aef9ec39SRoland Dreier goto err_disconnect; 2003aef9ec39SRoland Dreier 2004aef9ec39SRoland Dreier return count; 2005aef9ec39SRoland Dreier 2006aef9ec39SRoland Dreier err_disconnect: 2007aef9ec39SRoland Dreier srp_disconnect_target(target); 2008aef9ec39SRoland Dreier 2009aef9ec39SRoland Dreier err_cm_id: 2010aef9ec39SRoland Dreier ib_destroy_cm_id(target->cm_id); 2011aef9ec39SRoland Dreier 2012aef9ec39SRoland Dreier err_free: 2013aef9ec39SRoland Dreier srp_free_target_ib(target); 2014aef9ec39SRoland Dreier 2015aef9ec39SRoland Dreier err: 2016aef9ec39SRoland Dreier scsi_host_put(target_host); 2017aef9ec39SRoland Dreier 2018aef9ec39SRoland Dreier return ret; 2019aef9ec39SRoland Dreier } 2020aef9ec39SRoland Dreier 2021ee959b00STony Jones static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target); 2022aef9ec39SRoland Dreier 2023ee959b00STony Jones static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, 2024ee959b00STony Jones char *buf) 2025aef9ec39SRoland Dreier { 2026ee959b00STony Jones struct srp_host *host = container_of(dev, struct srp_host, dev); 2027aef9ec39SRoland Dreier 202805321937SGreg Kroah-Hartman return sprintf(buf, "%s\n", host->srp_dev->dev->name); 2029aef9ec39SRoland Dreier } 2030aef9ec39SRoland Dreier 2031ee959b00STony Jones static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); 2032aef9ec39SRoland Dreier 2033ee959b00STony Jones static ssize_t show_port(struct device *dev, struct device_attribute *attr, 2034ee959b00STony Jones char *buf) 2035aef9ec39SRoland Dreier { 2036ee959b00STony Jones struct srp_host *host = container_of(dev, struct srp_host, dev); 2037aef9ec39SRoland Dreier 2038aef9ec39SRoland Dreier return sprintf(buf, "%d\n", host->port); 2039aef9ec39SRoland Dreier } 2040aef9ec39SRoland Dreier 2041ee959b00STony Jones static DEVICE_ATTR(port, S_IRUGO, show_port, NULL); 2042aef9ec39SRoland Dreier 2043f5358a17SRoland Dreier static struct srp_host *srp_add_port(struct srp_device *device, u8 port) 2044aef9ec39SRoland Dreier { 2045aef9ec39SRoland Dreier struct srp_host *host; 2046aef9ec39SRoland Dreier 2047aef9ec39SRoland Dreier host = kzalloc(sizeof *host, GFP_KERNEL); 2048aef9ec39SRoland Dreier if (!host) 2049aef9ec39SRoland Dreier return NULL; 2050aef9ec39SRoland Dreier 2051aef9ec39SRoland Dreier INIT_LIST_HEAD(&host->target_list); 2052b3589fd4SMatthew Wilcox spin_lock_init(&host->target_lock); 2053aef9ec39SRoland Dreier init_completion(&host->released); 205405321937SGreg Kroah-Hartman host->srp_dev = device; 2055aef9ec39SRoland Dreier host->port = port; 2056aef9ec39SRoland Dreier 2057ee959b00STony Jones host->dev.class = &srp_class; 2058ee959b00STony Jones host->dev.parent = device->dev->dma_device; 2059d927e38cSKay Sievers dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port); 2060aef9ec39SRoland Dreier 2061ee959b00STony Jones if (device_register(&host->dev)) 2062f5358a17SRoland Dreier goto free_host; 2063ee959b00STony Jones if (device_create_file(&host->dev, &dev_attr_add_target)) 2064aef9ec39SRoland Dreier goto err_class; 2065ee959b00STony Jones if (device_create_file(&host->dev, &dev_attr_ibdev)) 2066aef9ec39SRoland Dreier goto err_class; 2067ee959b00STony Jones if (device_create_file(&host->dev, &dev_attr_port)) 2068aef9ec39SRoland Dreier goto err_class; 2069aef9ec39SRoland Dreier 2070aef9ec39SRoland Dreier return host; 2071aef9ec39SRoland Dreier 2072aef9ec39SRoland Dreier err_class: 2073ee959b00STony Jones device_unregister(&host->dev); 2074aef9ec39SRoland Dreier 2075f5358a17SRoland Dreier free_host: 2076aef9ec39SRoland Dreier kfree(host); 2077aef9ec39SRoland Dreier 2078aef9ec39SRoland Dreier return NULL; 2079aef9ec39SRoland Dreier } 2080aef9ec39SRoland Dreier 2081aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device) 2082aef9ec39SRoland Dreier { 2083f5358a17SRoland Dreier struct srp_device *srp_dev; 2084f5358a17SRoland Dreier struct ib_device_attr *dev_attr; 2085f5358a17SRoland Dreier struct ib_fmr_pool_param fmr_param; 2086aef9ec39SRoland Dreier struct srp_host *host; 2087aef9ec39SRoland Dreier int s, e, p; 2088aef9ec39SRoland Dreier 2089f5358a17SRoland Dreier dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL); 2090f5358a17SRoland Dreier if (!dev_attr) 2091cf311cd4SSean Hefty return; 2092aef9ec39SRoland Dreier 2093f5358a17SRoland Dreier if (ib_query_device(device, dev_attr)) { 2094f5358a17SRoland Dreier printk(KERN_WARNING PFX "Query device failed for %s\n", 2095f5358a17SRoland Dreier device->name); 2096f5358a17SRoland Dreier goto free_attr; 2097f5358a17SRoland Dreier } 2098f5358a17SRoland Dreier 2099f5358a17SRoland Dreier srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL); 2100f5358a17SRoland Dreier if (!srp_dev) 2101f5358a17SRoland Dreier goto free_attr; 2102f5358a17SRoland Dreier 2103f5358a17SRoland Dreier /* 2104f5358a17SRoland Dreier * Use the smallest page size supported by the HCA, down to a 2105f5358a17SRoland Dreier * minimum of 512 bytes (which is the smallest sector that a 2106f5358a17SRoland Dreier * SCSI command will ever carry). 2107f5358a17SRoland Dreier */ 2108f5358a17SRoland Dreier srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1); 2109f5358a17SRoland Dreier srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift; 2110bf628dc2SRoland Dreier srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1); 2111f5358a17SRoland Dreier 2112f5358a17SRoland Dreier INIT_LIST_HEAD(&srp_dev->dev_list); 2113f5358a17SRoland Dreier 2114f5358a17SRoland Dreier srp_dev->dev = device; 2115f5358a17SRoland Dreier srp_dev->pd = ib_alloc_pd(device); 2116f5358a17SRoland Dreier if (IS_ERR(srp_dev->pd)) 2117f5358a17SRoland Dreier goto free_dev; 2118f5358a17SRoland Dreier 2119f5358a17SRoland Dreier srp_dev->mr = ib_get_dma_mr(srp_dev->pd, 2120f5358a17SRoland Dreier IB_ACCESS_LOCAL_WRITE | 2121f5358a17SRoland Dreier IB_ACCESS_REMOTE_READ | 2122f5358a17SRoland Dreier IB_ACCESS_REMOTE_WRITE); 2123f5358a17SRoland Dreier if (IS_ERR(srp_dev->mr)) 2124f5358a17SRoland Dreier goto err_pd; 2125f5358a17SRoland Dreier 2126f5358a17SRoland Dreier memset(&fmr_param, 0, sizeof fmr_param); 2127f5358a17SRoland Dreier fmr_param.pool_size = SRP_FMR_POOL_SIZE; 2128f5358a17SRoland Dreier fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE; 2129f5358a17SRoland Dreier fmr_param.cache = 1; 2130f5358a17SRoland Dreier fmr_param.max_pages_per_fmr = SRP_FMR_SIZE; 2131f5358a17SRoland Dreier fmr_param.page_shift = srp_dev->fmr_page_shift; 2132f5358a17SRoland Dreier fmr_param.access = (IB_ACCESS_LOCAL_WRITE | 2133f5358a17SRoland Dreier IB_ACCESS_REMOTE_WRITE | 2134f5358a17SRoland Dreier IB_ACCESS_REMOTE_READ); 2135f5358a17SRoland Dreier 2136f5358a17SRoland Dreier srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param); 2137f5358a17SRoland Dreier if (IS_ERR(srp_dev->fmr_pool)) 2138f5358a17SRoland Dreier srp_dev->fmr_pool = NULL; 2139aef9ec39SRoland Dreier 214007ebafbaSTom Tucker if (device->node_type == RDMA_NODE_IB_SWITCH) { 2141aef9ec39SRoland Dreier s = 0; 2142aef9ec39SRoland Dreier e = 0; 2143aef9ec39SRoland Dreier } else { 2144aef9ec39SRoland Dreier s = 1; 2145aef9ec39SRoland Dreier e = device->phys_port_cnt; 2146aef9ec39SRoland Dreier } 2147aef9ec39SRoland Dreier 2148aef9ec39SRoland Dreier for (p = s; p <= e; ++p) { 2149f5358a17SRoland Dreier host = srp_add_port(srp_dev, p); 2150aef9ec39SRoland Dreier if (host) 2151f5358a17SRoland Dreier list_add_tail(&host->list, &srp_dev->dev_list); 2152aef9ec39SRoland Dreier } 2153aef9ec39SRoland Dreier 2154f5358a17SRoland Dreier ib_set_client_data(device, &srp_client, srp_dev); 2155f5358a17SRoland Dreier 2156f5358a17SRoland Dreier goto free_attr; 2157f5358a17SRoland Dreier 2158f5358a17SRoland Dreier err_pd: 2159f5358a17SRoland Dreier ib_dealloc_pd(srp_dev->pd); 2160f5358a17SRoland Dreier 2161f5358a17SRoland Dreier free_dev: 2162f5358a17SRoland Dreier kfree(srp_dev); 2163f5358a17SRoland Dreier 2164f5358a17SRoland Dreier free_attr: 2165f5358a17SRoland Dreier kfree(dev_attr); 2166aef9ec39SRoland Dreier } 2167aef9ec39SRoland Dreier 2168aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device) 2169aef9ec39SRoland Dreier { 2170f5358a17SRoland Dreier struct srp_device *srp_dev; 2171aef9ec39SRoland Dreier struct srp_host *host, *tmp_host; 2172aef9ec39SRoland Dreier LIST_HEAD(target_list); 2173aef9ec39SRoland Dreier struct srp_target_port *target, *tmp_target; 2174aef9ec39SRoland Dreier 2175f5358a17SRoland Dreier srp_dev = ib_get_client_data(device, &srp_client); 2176aef9ec39SRoland Dreier 2177f5358a17SRoland Dreier list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) { 2178ee959b00STony Jones device_unregister(&host->dev); 2179aef9ec39SRoland Dreier /* 2180aef9ec39SRoland Dreier * Wait for the sysfs entry to go away, so that no new 2181aef9ec39SRoland Dreier * target ports can be created. 2182aef9ec39SRoland Dreier */ 2183aef9ec39SRoland Dreier wait_for_completion(&host->released); 2184aef9ec39SRoland Dreier 2185aef9ec39SRoland Dreier /* 2186aef9ec39SRoland Dreier * Mark all target ports as removed, so we stop queueing 2187aef9ec39SRoland Dreier * commands and don't try to reconnect. 2188aef9ec39SRoland Dreier */ 2189b3589fd4SMatthew Wilcox spin_lock(&host->target_lock); 2190549c5fc2SMatthew Wilcox list_for_each_entry(target, &host->target_list, list) { 2191e9684678SBart Van Assche spin_lock_irq(&target->lock); 2192aef9ec39SRoland Dreier target->state = SRP_TARGET_REMOVED; 2193e9684678SBart Van Assche spin_unlock_irq(&target->lock); 2194aef9ec39SRoland Dreier } 2195b3589fd4SMatthew Wilcox spin_unlock(&host->target_lock); 2196aef9ec39SRoland Dreier 2197aef9ec39SRoland Dreier /* 2198aef9ec39SRoland Dreier * Wait for any reconnection tasks that may have 2199aef9ec39SRoland Dreier * started before we marked our target ports as 2200aef9ec39SRoland Dreier * removed, and any target port removal tasks. 2201aef9ec39SRoland Dreier */ 2202*f0626710STejun Heo flush_workqueue(ib_wq); 2203aef9ec39SRoland Dreier 2204aef9ec39SRoland Dreier list_for_each_entry_safe(target, tmp_target, 2205aef9ec39SRoland Dreier &host->target_list, list) { 2206b0e47c8bSDavid Dillow srp_remove_host(target->scsi_host); 2207ad696989SDave Dillow scsi_remove_host(target->scsi_host); 2208aef9ec39SRoland Dreier srp_disconnect_target(target); 2209aef9ec39SRoland Dreier ib_destroy_cm_id(target->cm_id); 2210aef9ec39SRoland Dreier srp_free_target_ib(target); 2211aef9ec39SRoland Dreier scsi_host_put(target->scsi_host); 2212aef9ec39SRoland Dreier } 2213aef9ec39SRoland Dreier 2214aef9ec39SRoland Dreier kfree(host); 2215aef9ec39SRoland Dreier } 2216aef9ec39SRoland Dreier 2217f5358a17SRoland Dreier if (srp_dev->fmr_pool) 2218f5358a17SRoland Dreier ib_destroy_fmr_pool(srp_dev->fmr_pool); 2219f5358a17SRoland Dreier ib_dereg_mr(srp_dev->mr); 2220f5358a17SRoland Dreier ib_dealloc_pd(srp_dev->pd); 2221f5358a17SRoland Dreier 2222f5358a17SRoland Dreier kfree(srp_dev); 2223aef9ec39SRoland Dreier } 2224aef9ec39SRoland Dreier 22253236822bSFUJITA Tomonori static struct srp_function_template ib_srp_transport_functions = { 22263236822bSFUJITA Tomonori }; 22273236822bSFUJITA Tomonori 2228aef9ec39SRoland Dreier static int __init srp_init_module(void) 2229aef9ec39SRoland Dreier { 2230aef9ec39SRoland Dreier int ret; 2231aef9ec39SRoland Dreier 2232dcb4cb85SBart Van Assche BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *)); 2233dd5e6e38SBart Van Assche 22341e89a194SDavid Dillow if (srp_sg_tablesize > 255) { 22351e89a194SDavid Dillow printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n"); 22361e89a194SDavid Dillow srp_sg_tablesize = 255; 22371e89a194SDavid Dillow } 22381e89a194SDavid Dillow 22393236822bSFUJITA Tomonori ib_srp_transport_template = 22403236822bSFUJITA Tomonori srp_attach_transport(&ib_srp_transport_functions); 22413236822bSFUJITA Tomonori if (!ib_srp_transport_template) 22423236822bSFUJITA Tomonori return -ENOMEM; 22433236822bSFUJITA Tomonori 224474b0a15bSVu Pham srp_template.sg_tablesize = srp_sg_tablesize; 224574b0a15bSVu Pham srp_max_iu_len = (sizeof (struct srp_cmd) + 224674b0a15bSVu Pham sizeof (struct srp_indirect_buf) + 224774b0a15bSVu Pham srp_sg_tablesize * 16); 224874b0a15bSVu Pham 2249aef9ec39SRoland Dreier ret = class_register(&srp_class); 2250aef9ec39SRoland Dreier if (ret) { 2251aef9ec39SRoland Dreier printk(KERN_ERR PFX "couldn't register class infiniband_srp\n"); 22523236822bSFUJITA Tomonori srp_release_transport(ib_srp_transport_template); 2253aef9ec39SRoland Dreier return ret; 2254aef9ec39SRoland Dreier } 2255aef9ec39SRoland Dreier 2256c1a0b23bSMichael S. Tsirkin ib_sa_register_client(&srp_sa_client); 2257c1a0b23bSMichael S. Tsirkin 2258aef9ec39SRoland Dreier ret = ib_register_client(&srp_client); 2259aef9ec39SRoland Dreier if (ret) { 2260aef9ec39SRoland Dreier printk(KERN_ERR PFX "couldn't register IB client\n"); 22613236822bSFUJITA Tomonori srp_release_transport(ib_srp_transport_template); 2262c1a0b23bSMichael S. Tsirkin ib_sa_unregister_client(&srp_sa_client); 2263aef9ec39SRoland Dreier class_unregister(&srp_class); 2264aef9ec39SRoland Dreier return ret; 2265aef9ec39SRoland Dreier } 2266aef9ec39SRoland Dreier 2267aef9ec39SRoland Dreier return 0; 2268aef9ec39SRoland Dreier } 2269aef9ec39SRoland Dreier 2270aef9ec39SRoland Dreier static void __exit srp_cleanup_module(void) 2271aef9ec39SRoland Dreier { 2272aef9ec39SRoland Dreier ib_unregister_client(&srp_client); 2273c1a0b23bSMichael S. Tsirkin ib_sa_unregister_client(&srp_sa_client); 2274aef9ec39SRoland Dreier class_unregister(&srp_class); 22753236822bSFUJITA Tomonori srp_release_transport(ib_srp_transport_template); 2276aef9ec39SRoland Dreier } 2277aef9ec39SRoland Dreier 2278aef9ec39SRoland Dreier module_init(srp_init_module); 2279aef9ec39SRoland Dreier module_exit(srp_cleanup_module); 2280