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 75aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device); 76aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device); 779c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr); 789c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr); 79aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event); 80aef9ec39SRoland Dreier 813236822bSFUJITA Tomonori static struct scsi_transport_template *ib_srp_transport_template; 823236822bSFUJITA Tomonori 83aef9ec39SRoland Dreier static struct ib_client srp_client = { 84aef9ec39SRoland Dreier .name = "srp", 85aef9ec39SRoland Dreier .add = srp_add_one, 86aef9ec39SRoland Dreier .remove = srp_remove_one 87aef9ec39SRoland Dreier }; 88aef9ec39SRoland Dreier 89c1a0b23bSMichael S. Tsirkin static struct ib_sa_client srp_sa_client; 90c1a0b23bSMichael S. Tsirkin 91aef9ec39SRoland Dreier static inline struct srp_target_port *host_to_target(struct Scsi_Host *host) 92aef9ec39SRoland Dreier { 93aef9ec39SRoland Dreier return (struct srp_target_port *) host->hostdata; 94aef9ec39SRoland Dreier } 95aef9ec39SRoland Dreier 96aef9ec39SRoland Dreier static const char *srp_target_info(struct Scsi_Host *host) 97aef9ec39SRoland Dreier { 98aef9ec39SRoland Dreier return host_to_target(host)->target_name; 99aef9ec39SRoland Dreier } 100aef9ec39SRoland Dreier 1015d7cbfd6SRoland Dreier static int srp_target_is_topspin(struct srp_target_port *target) 1025d7cbfd6SRoland Dreier { 1035d7cbfd6SRoland Dreier static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad }; 1043d1ff48dSRaghava Kondapalli static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d }; 1055d7cbfd6SRoland Dreier 1065d7cbfd6SRoland Dreier return topspin_workarounds && 1073d1ff48dSRaghava Kondapalli (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) || 1083d1ff48dSRaghava Kondapalli !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui)); 1095d7cbfd6SRoland Dreier } 1105d7cbfd6SRoland Dreier 111aef9ec39SRoland Dreier static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size, 112aef9ec39SRoland Dreier gfp_t gfp_mask, 113aef9ec39SRoland Dreier enum dma_data_direction direction) 114aef9ec39SRoland Dreier { 115aef9ec39SRoland Dreier struct srp_iu *iu; 116aef9ec39SRoland Dreier 117aef9ec39SRoland Dreier iu = kmalloc(sizeof *iu, gfp_mask); 118aef9ec39SRoland Dreier if (!iu) 119aef9ec39SRoland Dreier goto out; 120aef9ec39SRoland Dreier 121aef9ec39SRoland Dreier iu->buf = kzalloc(size, gfp_mask); 122aef9ec39SRoland Dreier if (!iu->buf) 123aef9ec39SRoland Dreier goto out_free_iu; 124aef9ec39SRoland Dreier 12505321937SGreg Kroah-Hartman iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size, 12605321937SGreg Kroah-Hartman direction); 12705321937SGreg Kroah-Hartman if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma)) 128aef9ec39SRoland Dreier goto out_free_buf; 129aef9ec39SRoland Dreier 130aef9ec39SRoland Dreier iu->size = size; 131aef9ec39SRoland Dreier iu->direction = direction; 132aef9ec39SRoland Dreier 133aef9ec39SRoland Dreier return iu; 134aef9ec39SRoland Dreier 135aef9ec39SRoland Dreier out_free_buf: 136aef9ec39SRoland Dreier kfree(iu->buf); 137aef9ec39SRoland Dreier out_free_iu: 138aef9ec39SRoland Dreier kfree(iu); 139aef9ec39SRoland Dreier out: 140aef9ec39SRoland Dreier return NULL; 141aef9ec39SRoland Dreier } 142aef9ec39SRoland Dreier 143aef9ec39SRoland Dreier static void srp_free_iu(struct srp_host *host, struct srp_iu *iu) 144aef9ec39SRoland Dreier { 145aef9ec39SRoland Dreier if (!iu) 146aef9ec39SRoland Dreier return; 147aef9ec39SRoland Dreier 14805321937SGreg Kroah-Hartman ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size, 14905321937SGreg Kroah-Hartman iu->direction); 150aef9ec39SRoland Dreier kfree(iu->buf); 151aef9ec39SRoland Dreier kfree(iu); 152aef9ec39SRoland Dreier } 153aef9ec39SRoland Dreier 154aef9ec39SRoland Dreier static void srp_qp_event(struct ib_event *event, void *context) 155aef9ec39SRoland Dreier { 156aef9ec39SRoland Dreier printk(KERN_ERR PFX "QP event %d\n", event->event); 157aef9ec39SRoland Dreier } 158aef9ec39SRoland Dreier 159aef9ec39SRoland Dreier static int srp_init_qp(struct srp_target_port *target, 160aef9ec39SRoland Dreier struct ib_qp *qp) 161aef9ec39SRoland Dreier { 162aef9ec39SRoland Dreier struct ib_qp_attr *attr; 163aef9ec39SRoland Dreier int ret; 164aef9ec39SRoland Dreier 165aef9ec39SRoland Dreier attr = kmalloc(sizeof *attr, GFP_KERNEL); 166aef9ec39SRoland Dreier if (!attr) 167aef9ec39SRoland Dreier return -ENOMEM; 168aef9ec39SRoland Dreier 169969a60f9SRoland Dreier ret = ib_find_pkey(target->srp_host->srp_dev->dev, 170aef9ec39SRoland Dreier target->srp_host->port, 171aef9ec39SRoland Dreier be16_to_cpu(target->path.pkey), 172aef9ec39SRoland Dreier &attr->pkey_index); 173aef9ec39SRoland Dreier if (ret) 174aef9ec39SRoland Dreier goto out; 175aef9ec39SRoland Dreier 176aef9ec39SRoland Dreier attr->qp_state = IB_QPS_INIT; 177aef9ec39SRoland Dreier attr->qp_access_flags = (IB_ACCESS_REMOTE_READ | 178aef9ec39SRoland Dreier IB_ACCESS_REMOTE_WRITE); 179aef9ec39SRoland Dreier attr->port_num = target->srp_host->port; 180aef9ec39SRoland Dreier 181aef9ec39SRoland Dreier ret = ib_modify_qp(qp, attr, 182aef9ec39SRoland Dreier IB_QP_STATE | 183aef9ec39SRoland Dreier IB_QP_PKEY_INDEX | 184aef9ec39SRoland Dreier IB_QP_ACCESS_FLAGS | 185aef9ec39SRoland Dreier IB_QP_PORT); 186aef9ec39SRoland Dreier 187aef9ec39SRoland Dreier out: 188aef9ec39SRoland Dreier kfree(attr); 189aef9ec39SRoland Dreier return ret; 190aef9ec39SRoland Dreier } 191aef9ec39SRoland Dreier 1929fe4bcf4SDavid Dillow static int srp_new_cm_id(struct srp_target_port *target) 1939fe4bcf4SDavid Dillow { 1949fe4bcf4SDavid Dillow struct ib_cm_id *new_cm_id; 1959fe4bcf4SDavid Dillow 19605321937SGreg Kroah-Hartman new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev, 1979fe4bcf4SDavid Dillow srp_cm_handler, target); 1989fe4bcf4SDavid Dillow if (IS_ERR(new_cm_id)) 1999fe4bcf4SDavid Dillow return PTR_ERR(new_cm_id); 2009fe4bcf4SDavid Dillow 2019fe4bcf4SDavid Dillow if (target->cm_id) 2029fe4bcf4SDavid Dillow ib_destroy_cm_id(target->cm_id); 2039fe4bcf4SDavid Dillow target->cm_id = new_cm_id; 2049fe4bcf4SDavid Dillow 2059fe4bcf4SDavid Dillow return 0; 2069fe4bcf4SDavid Dillow } 2079fe4bcf4SDavid Dillow 208aef9ec39SRoland Dreier static int srp_create_target_ib(struct srp_target_port *target) 209aef9ec39SRoland Dreier { 210aef9ec39SRoland Dreier struct ib_qp_init_attr *init_attr; 211aef9ec39SRoland Dreier int ret; 212aef9ec39SRoland Dreier 213aef9ec39SRoland Dreier init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL); 214aef9ec39SRoland Dreier if (!init_attr) 215aef9ec39SRoland Dreier return -ENOMEM; 216aef9ec39SRoland Dreier 2179c03dc9fSBart Van Assche target->recv_cq = ib_create_cq(target->srp_host->srp_dev->dev, 2189c03dc9fSBart Van Assche srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0); 2199c03dc9fSBart Van Assche if (IS_ERR(target->recv_cq)) { 2209c03dc9fSBart Van Assche ret = PTR_ERR(target->recv_cq); 221da9d2f07SRoland Dreier goto err; 222aef9ec39SRoland Dreier } 223aef9ec39SRoland Dreier 2249c03dc9fSBart Van Assche target->send_cq = ib_create_cq(target->srp_host->srp_dev->dev, 2259c03dc9fSBart Van Assche srp_send_completion, NULL, target, SRP_SQ_SIZE, 0); 2269c03dc9fSBart Van Assche if (IS_ERR(target->send_cq)) { 2279c03dc9fSBart Van Assche ret = PTR_ERR(target->send_cq); 228da9d2f07SRoland Dreier goto err_recv_cq; 2299c03dc9fSBart Van Assche } 2309c03dc9fSBart Van Assche 2319c03dc9fSBart Van Assche ib_req_notify_cq(target->recv_cq, IB_CQ_NEXT_COMP); 232aef9ec39SRoland Dreier 233aef9ec39SRoland Dreier init_attr->event_handler = srp_qp_event; 234aef9ec39SRoland Dreier init_attr->cap.max_send_wr = SRP_SQ_SIZE; 235aef9ec39SRoland Dreier init_attr->cap.max_recv_wr = SRP_RQ_SIZE; 236aef9ec39SRoland Dreier init_attr->cap.max_recv_sge = 1; 237aef9ec39SRoland Dreier init_attr->cap.max_send_sge = 1; 238aef9ec39SRoland Dreier init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 239aef9ec39SRoland Dreier init_attr->qp_type = IB_QPT_RC; 2409c03dc9fSBart Van Assche init_attr->send_cq = target->send_cq; 2419c03dc9fSBart Van Assche init_attr->recv_cq = target->recv_cq; 242aef9ec39SRoland Dreier 24305321937SGreg Kroah-Hartman target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr); 244aef9ec39SRoland Dreier if (IS_ERR(target->qp)) { 245aef9ec39SRoland Dreier ret = PTR_ERR(target->qp); 246da9d2f07SRoland Dreier goto err_send_cq; 247aef9ec39SRoland Dreier } 248aef9ec39SRoland Dreier 249aef9ec39SRoland Dreier ret = srp_init_qp(target, target->qp); 250da9d2f07SRoland Dreier if (ret) 251da9d2f07SRoland Dreier goto err_qp; 252aef9ec39SRoland Dreier 253da9d2f07SRoland Dreier kfree(init_attr); 254da9d2f07SRoland Dreier return 0; 255da9d2f07SRoland Dreier 256da9d2f07SRoland Dreier err_qp: 257da9d2f07SRoland Dreier ib_destroy_qp(target->qp); 258da9d2f07SRoland Dreier 259da9d2f07SRoland Dreier err_send_cq: 260da9d2f07SRoland Dreier ib_destroy_cq(target->send_cq); 261da9d2f07SRoland Dreier 262da9d2f07SRoland Dreier err_recv_cq: 263da9d2f07SRoland Dreier ib_destroy_cq(target->recv_cq); 264da9d2f07SRoland Dreier 265da9d2f07SRoland Dreier err: 266aef9ec39SRoland Dreier kfree(init_attr); 267aef9ec39SRoland Dreier return ret; 268aef9ec39SRoland Dreier } 269aef9ec39SRoland Dreier 270aef9ec39SRoland Dreier static void srp_free_target_ib(struct srp_target_port *target) 271aef9ec39SRoland Dreier { 272aef9ec39SRoland Dreier int i; 273aef9ec39SRoland Dreier 274aef9ec39SRoland Dreier ib_destroy_qp(target->qp); 2759c03dc9fSBart Van Assche ib_destroy_cq(target->send_cq); 2769c03dc9fSBart Van Assche ib_destroy_cq(target->recv_cq); 277aef9ec39SRoland Dreier 278aef9ec39SRoland Dreier for (i = 0; i < SRP_RQ_SIZE; ++i) 279aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->rx_ring[i]); 280dd5e6e38SBart Van Assche for (i = 0; i < SRP_SQ_SIZE; ++i) 281aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->tx_ring[i]); 282aef9ec39SRoland Dreier } 283aef9ec39SRoland Dreier 284aef9ec39SRoland Dreier static void srp_path_rec_completion(int status, 285aef9ec39SRoland Dreier struct ib_sa_path_rec *pathrec, 286aef9ec39SRoland Dreier void *target_ptr) 287aef9ec39SRoland Dreier { 288aef9ec39SRoland Dreier struct srp_target_port *target = target_ptr; 289aef9ec39SRoland Dreier 290aef9ec39SRoland Dreier target->status = status; 291aef9ec39SRoland Dreier if (status) 2927aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 2937aa54bd7SDavid Dillow PFX "Got failed path rec status %d\n", status); 294aef9ec39SRoland Dreier else 295aef9ec39SRoland Dreier target->path = *pathrec; 296aef9ec39SRoland Dreier complete(&target->done); 297aef9ec39SRoland Dreier } 298aef9ec39SRoland Dreier 299aef9ec39SRoland Dreier static int srp_lookup_path(struct srp_target_port *target) 300aef9ec39SRoland Dreier { 301aef9ec39SRoland Dreier target->path.numb_path = 1; 302aef9ec39SRoland Dreier 303aef9ec39SRoland Dreier init_completion(&target->done); 304aef9ec39SRoland Dreier 305c1a0b23bSMichael S. Tsirkin target->path_query_id = ib_sa_path_rec_get(&srp_sa_client, 30605321937SGreg Kroah-Hartman target->srp_host->srp_dev->dev, 307aef9ec39SRoland Dreier target->srp_host->port, 308aef9ec39SRoland Dreier &target->path, 309247e020eSSean Hefty IB_SA_PATH_REC_SERVICE_ID | 310aef9ec39SRoland Dreier IB_SA_PATH_REC_DGID | 311aef9ec39SRoland Dreier IB_SA_PATH_REC_SGID | 312aef9ec39SRoland Dreier IB_SA_PATH_REC_NUMB_PATH | 313aef9ec39SRoland Dreier IB_SA_PATH_REC_PKEY, 314aef9ec39SRoland Dreier SRP_PATH_REC_TIMEOUT_MS, 315aef9ec39SRoland Dreier GFP_KERNEL, 316aef9ec39SRoland Dreier srp_path_rec_completion, 317aef9ec39SRoland Dreier target, &target->path_query); 318aef9ec39SRoland Dreier if (target->path_query_id < 0) 319aef9ec39SRoland Dreier return target->path_query_id; 320aef9ec39SRoland Dreier 321aef9ec39SRoland Dreier wait_for_completion(&target->done); 322aef9ec39SRoland Dreier 323aef9ec39SRoland Dreier if (target->status < 0) 3247aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 3257aa54bd7SDavid Dillow PFX "Path record query failed\n"); 326aef9ec39SRoland Dreier 327aef9ec39SRoland Dreier return target->status; 328aef9ec39SRoland Dreier } 329aef9ec39SRoland Dreier 330aef9ec39SRoland Dreier static int srp_send_req(struct srp_target_port *target) 331aef9ec39SRoland Dreier { 332aef9ec39SRoland Dreier struct { 333aef9ec39SRoland Dreier struct ib_cm_req_param param; 334aef9ec39SRoland Dreier struct srp_login_req priv; 335aef9ec39SRoland Dreier } *req = NULL; 336aef9ec39SRoland Dreier int status; 337aef9ec39SRoland Dreier 338aef9ec39SRoland Dreier req = kzalloc(sizeof *req, GFP_KERNEL); 339aef9ec39SRoland Dreier if (!req) 340aef9ec39SRoland Dreier return -ENOMEM; 341aef9ec39SRoland Dreier 342aef9ec39SRoland Dreier req->param.primary_path = &target->path; 343aef9ec39SRoland Dreier req->param.alternate_path = NULL; 344aef9ec39SRoland Dreier req->param.service_id = target->service_id; 345aef9ec39SRoland Dreier req->param.qp_num = target->qp->qp_num; 346aef9ec39SRoland Dreier req->param.qp_type = target->qp->qp_type; 347aef9ec39SRoland Dreier req->param.private_data = &req->priv; 348aef9ec39SRoland Dreier req->param.private_data_len = sizeof req->priv; 349aef9ec39SRoland Dreier req->param.flow_control = 1; 350aef9ec39SRoland Dreier 351aef9ec39SRoland Dreier get_random_bytes(&req->param.starting_psn, 4); 352aef9ec39SRoland Dreier req->param.starting_psn &= 0xffffff; 353aef9ec39SRoland Dreier 354aef9ec39SRoland Dreier /* 355aef9ec39SRoland Dreier * Pick some arbitrary defaults here; we could make these 356aef9ec39SRoland Dreier * module parameters if anyone cared about setting them. 357aef9ec39SRoland Dreier */ 358aef9ec39SRoland Dreier req->param.responder_resources = 4; 359aef9ec39SRoland Dreier req->param.remote_cm_response_timeout = 20; 360aef9ec39SRoland Dreier req->param.local_cm_response_timeout = 20; 361aef9ec39SRoland Dreier req->param.retry_count = 7; 362aef9ec39SRoland Dreier req->param.rnr_retry_count = 7; 363aef9ec39SRoland Dreier req->param.max_cm_retries = 15; 364aef9ec39SRoland Dreier 365aef9ec39SRoland Dreier req->priv.opcode = SRP_LOGIN_REQ; 366aef9ec39SRoland Dreier req->priv.tag = 0; 36774b0a15bSVu Pham req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len); 368aef9ec39SRoland Dreier req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | 369aef9ec39SRoland Dreier SRP_BUF_FORMAT_INDIRECT); 3700c0450dbSRamachandra K /* 3710c0450dbSRamachandra K * In the published SRP specification (draft rev. 16a), the 3720c0450dbSRamachandra K * port identifier format is 8 bytes of ID extension followed 3730c0450dbSRamachandra K * by 8 bytes of GUID. Older drafts put the two halves in the 3740c0450dbSRamachandra K * opposite order, so that the GUID comes first. 3750c0450dbSRamachandra K * 3760c0450dbSRamachandra K * Targets conforming to these obsolete drafts can be 3770c0450dbSRamachandra K * recognized by the I/O Class they report. 3780c0450dbSRamachandra K */ 3790c0450dbSRamachandra K if (target->io_class == SRP_REV10_IB_IO_CLASS) { 3800c0450dbSRamachandra K memcpy(req->priv.initiator_port_id, 38101cb9bcbSIshai Rabinovitz &target->path.sgid.global.interface_id, 8); 3820c0450dbSRamachandra K memcpy(req->priv.initiator_port_id + 8, 38301cb9bcbSIshai Rabinovitz &target->initiator_ext, 8); 3840c0450dbSRamachandra K memcpy(req->priv.target_port_id, &target->ioc_guid, 8); 3850c0450dbSRamachandra K memcpy(req->priv.target_port_id + 8, &target->id_ext, 8); 3860c0450dbSRamachandra K } else { 3870c0450dbSRamachandra K memcpy(req->priv.initiator_port_id, 38801cb9bcbSIshai Rabinovitz &target->initiator_ext, 8); 38901cb9bcbSIshai Rabinovitz memcpy(req->priv.initiator_port_id + 8, 39001cb9bcbSIshai Rabinovitz &target->path.sgid.global.interface_id, 8); 3910c0450dbSRamachandra K memcpy(req->priv.target_port_id, &target->id_ext, 8); 3920c0450dbSRamachandra K memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8); 3930c0450dbSRamachandra K } 3940c0450dbSRamachandra K 395aef9ec39SRoland Dreier /* 396aef9ec39SRoland Dreier * Topspin/Cisco SRP targets will reject our login unless we 39701cb9bcbSIshai Rabinovitz * zero out the first 8 bytes of our initiator port ID and set 39801cb9bcbSIshai Rabinovitz * the second 8 bytes to the local node GUID. 399aef9ec39SRoland Dreier */ 4005d7cbfd6SRoland Dreier if (srp_target_is_topspin(target)) { 4017aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, 4027aa54bd7SDavid Dillow PFX "Topspin/Cisco initiator port ID workaround " 403aef9ec39SRoland Dreier "activated for target GUID %016llx\n", 404aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->ioc_guid)); 405aef9ec39SRoland Dreier memset(req->priv.initiator_port_id, 0, 8); 40601cb9bcbSIshai Rabinovitz memcpy(req->priv.initiator_port_id + 8, 40705321937SGreg Kroah-Hartman &target->srp_host->srp_dev->dev->node_guid, 8); 408aef9ec39SRoland Dreier } 409aef9ec39SRoland Dreier 410aef9ec39SRoland Dreier status = ib_send_cm_req(target->cm_id, &req->param); 411aef9ec39SRoland Dreier 412aef9ec39SRoland Dreier kfree(req); 413aef9ec39SRoland Dreier 414aef9ec39SRoland Dreier return status; 415aef9ec39SRoland Dreier } 416aef9ec39SRoland Dreier 417aef9ec39SRoland Dreier static void srp_disconnect_target(struct srp_target_port *target) 418aef9ec39SRoland Dreier { 419aef9ec39SRoland Dreier /* XXX should send SRP_I_LOGOUT request */ 420aef9ec39SRoland Dreier 421aef9ec39SRoland Dreier init_completion(&target->done); 422e6581056SRoland Dreier if (ib_send_cm_dreq(target->cm_id, NULL, 0)) { 4237aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, 4247aa54bd7SDavid Dillow PFX "Sending CM DREQ failed\n"); 425e6581056SRoland Dreier return; 426e6581056SRoland Dreier } 427aef9ec39SRoland Dreier wait_for_completion(&target->done); 428aef9ec39SRoland Dreier } 429aef9ec39SRoland Dreier 4309709f0e0SBart Van Assche static bool srp_change_state(struct srp_target_port *target, 4319709f0e0SBart Van Assche enum srp_target_state old, 4329709f0e0SBart Van Assche enum srp_target_state new) 4339709f0e0SBart Van Assche { 4349709f0e0SBart Van Assche bool changed = false; 4359709f0e0SBart Van Assche 436e9684678SBart Van Assche spin_lock_irq(&target->lock); 4379709f0e0SBart Van Assche if (target->state == old) { 4389709f0e0SBart Van Assche target->state = new; 4399709f0e0SBart Van Assche changed = true; 4409709f0e0SBart Van Assche } 441e9684678SBart Van Assche spin_unlock_irq(&target->lock); 4429709f0e0SBart Van Assche return changed; 4439709f0e0SBart Van Assche } 4449709f0e0SBart Van Assche 445c4028958SDavid Howells static void srp_remove_work(struct work_struct *work) 446aef9ec39SRoland Dreier { 447c4028958SDavid Howells struct srp_target_port *target = 448c4028958SDavid Howells container_of(work, struct srp_target_port, work); 449aef9ec39SRoland Dreier 4509709f0e0SBart Van Assche if (!srp_change_state(target, SRP_TARGET_DEAD, SRP_TARGET_REMOVED)) 451aef9ec39SRoland Dreier return; 452aef9ec39SRoland Dreier 453b3589fd4SMatthew Wilcox spin_lock(&target->srp_host->target_lock); 454aef9ec39SRoland Dreier list_del(&target->list); 455b3589fd4SMatthew Wilcox spin_unlock(&target->srp_host->target_lock); 456aef9ec39SRoland Dreier 4573236822bSFUJITA Tomonori srp_remove_host(target->scsi_host); 458aef9ec39SRoland Dreier scsi_remove_host(target->scsi_host); 459aef9ec39SRoland Dreier ib_destroy_cm_id(target->cm_id); 460aef9ec39SRoland Dreier srp_free_target_ib(target); 461aef9ec39SRoland Dreier scsi_host_put(target->scsi_host); 462aef9ec39SRoland Dreier } 463aef9ec39SRoland Dreier 464aef9ec39SRoland Dreier static int srp_connect_target(struct srp_target_port *target) 465aef9ec39SRoland Dreier { 4669fe4bcf4SDavid Dillow int retries = 3; 467aef9ec39SRoland Dreier int ret; 468aef9ec39SRoland Dreier 469aef9ec39SRoland Dreier ret = srp_lookup_path(target); 470aef9ec39SRoland Dreier if (ret) 471aef9ec39SRoland Dreier return ret; 472aef9ec39SRoland Dreier 473aef9ec39SRoland Dreier while (1) { 474aef9ec39SRoland Dreier init_completion(&target->done); 475aef9ec39SRoland Dreier ret = srp_send_req(target); 476aef9ec39SRoland Dreier if (ret) 477aef9ec39SRoland Dreier return ret; 478aef9ec39SRoland Dreier wait_for_completion(&target->done); 479aef9ec39SRoland Dreier 480aef9ec39SRoland Dreier /* 481aef9ec39SRoland Dreier * The CM event handling code will set status to 482aef9ec39SRoland Dreier * SRP_PORT_REDIRECT if we get a port redirect REJ 483aef9ec39SRoland Dreier * back, or SRP_DLID_REDIRECT if we get a lid/qp 484aef9ec39SRoland Dreier * redirect REJ back. 485aef9ec39SRoland Dreier */ 486aef9ec39SRoland Dreier switch (target->status) { 487aef9ec39SRoland Dreier case 0: 488aef9ec39SRoland Dreier return 0; 489aef9ec39SRoland Dreier 490aef9ec39SRoland Dreier case SRP_PORT_REDIRECT: 491aef9ec39SRoland Dreier ret = srp_lookup_path(target); 492aef9ec39SRoland Dreier if (ret) 493aef9ec39SRoland Dreier return ret; 494aef9ec39SRoland Dreier break; 495aef9ec39SRoland Dreier 496aef9ec39SRoland Dreier case SRP_DLID_REDIRECT: 497aef9ec39SRoland Dreier break; 498aef9ec39SRoland Dreier 4999fe4bcf4SDavid Dillow case SRP_STALE_CONN: 5009fe4bcf4SDavid Dillow /* Our current CM id was stale, and is now in timewait. 5019fe4bcf4SDavid Dillow * Try to reconnect with a new one. 5029fe4bcf4SDavid Dillow */ 5039fe4bcf4SDavid Dillow if (!retries-- || srp_new_cm_id(target)) { 5049fe4bcf4SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 5059fe4bcf4SDavid Dillow "giving up on stale connection\n"); 5069fe4bcf4SDavid Dillow target->status = -ECONNRESET; 5079fe4bcf4SDavid Dillow return target->status; 5089fe4bcf4SDavid Dillow } 5099fe4bcf4SDavid Dillow 5109fe4bcf4SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 5119fe4bcf4SDavid Dillow "retrying stale connection\n"); 5129fe4bcf4SDavid Dillow break; 5139fe4bcf4SDavid Dillow 514aef9ec39SRoland Dreier default: 515aef9ec39SRoland Dreier return target->status; 516aef9ec39SRoland Dreier } 517aef9ec39SRoland Dreier } 518aef9ec39SRoland Dreier } 519aef9ec39SRoland Dreier 520d945e1dfSRoland Dreier static void srp_unmap_data(struct scsi_cmnd *scmnd, 521d945e1dfSRoland Dreier struct srp_target_port *target, 522d945e1dfSRoland Dreier struct srp_request *req) 523d945e1dfSRoland Dreier { 524bb350d1dSFUJITA Tomonori if (!scsi_sglist(scmnd) || 525d945e1dfSRoland Dreier (scmnd->sc_data_direction != DMA_TO_DEVICE && 526d945e1dfSRoland Dreier scmnd->sc_data_direction != DMA_FROM_DEVICE)) 527d945e1dfSRoland Dreier return; 528d945e1dfSRoland Dreier 529f5358a17SRoland Dreier if (req->fmr) { 530f5358a17SRoland Dreier ib_fmr_pool_unmap(req->fmr); 531f5358a17SRoland Dreier req->fmr = NULL; 532f5358a17SRoland Dreier } 533f5358a17SRoland Dreier 53405321937SGreg Kroah-Hartman ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd), 535bb350d1dSFUJITA Tomonori scsi_sg_count(scmnd), scmnd->sc_data_direction); 536d945e1dfSRoland Dreier } 537d945e1dfSRoland Dreier 53894a9174cSBart Van Assche static void srp_remove_req(struct srp_target_port *target, 53994a9174cSBart Van Assche struct srp_request *req, s32 req_lim_delta) 540526b4caaSIshai Rabinovitz { 54194a9174cSBart Van Assche unsigned long flags; 54294a9174cSBart Van Assche 543526b4caaSIshai Rabinovitz srp_unmap_data(req->scmnd, target, req); 544e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 54594a9174cSBart Van Assche target->req_lim += req_lim_delta; 546f8b6e31eSDavid Dillow req->scmnd = NULL; 547536ae14eSBart Van Assche list_add_tail(&req->list, &target->free_reqs); 548e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 549526b4caaSIshai Rabinovitz } 550526b4caaSIshai Rabinovitz 551526b4caaSIshai Rabinovitz static void srp_reset_req(struct srp_target_port *target, struct srp_request *req) 552526b4caaSIshai Rabinovitz { 553526b4caaSIshai Rabinovitz req->scmnd->result = DID_RESET << 16; 554526b4caaSIshai Rabinovitz req->scmnd->scsi_done(req->scmnd); 55594a9174cSBart Van Assche srp_remove_req(target, req, 0); 556526b4caaSIshai Rabinovitz } 557526b4caaSIshai Rabinovitz 558aef9ec39SRoland Dreier static int srp_reconnect_target(struct srp_target_port *target) 559aef9ec39SRoland Dreier { 560aef9ec39SRoland Dreier struct ib_qp_attr qp_attr; 561aef9ec39SRoland Dreier struct ib_wc wc; 562dcb4cb85SBart Van Assche int i, ret; 563aef9ec39SRoland Dreier 5649709f0e0SBart Van Assche if (!srp_change_state(target, SRP_TARGET_LIVE, SRP_TARGET_CONNECTING)) 565aef9ec39SRoland Dreier return -EAGAIN; 566aef9ec39SRoland Dreier 567aef9ec39SRoland Dreier srp_disconnect_target(target); 568aef9ec39SRoland Dreier /* 569aef9ec39SRoland Dreier * Now get a new local CM ID so that we avoid confusing the 570aef9ec39SRoland Dreier * target in case things are really fouled up. 571aef9ec39SRoland Dreier */ 5729fe4bcf4SDavid Dillow ret = srp_new_cm_id(target); 5739fe4bcf4SDavid Dillow if (ret) 574aef9ec39SRoland Dreier goto err; 575aef9ec39SRoland Dreier 576aef9ec39SRoland Dreier qp_attr.qp_state = IB_QPS_RESET; 577aef9ec39SRoland Dreier ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE); 578aef9ec39SRoland Dreier if (ret) 579aef9ec39SRoland Dreier goto err; 580aef9ec39SRoland Dreier 581aef9ec39SRoland Dreier ret = srp_init_qp(target, target->qp); 582aef9ec39SRoland Dreier if (ret) 583aef9ec39SRoland Dreier goto err; 584aef9ec39SRoland Dreier 5859c03dc9fSBart Van Assche while (ib_poll_cq(target->recv_cq, 1, &wc) > 0) 5869c03dc9fSBart Van Assche ; /* nothing */ 5879c03dc9fSBart Van Assche while (ib_poll_cq(target->send_cq, 1, &wc) > 0) 588aef9ec39SRoland Dreier ; /* nothing */ 589aef9ec39SRoland Dreier 590536ae14eSBart Van Assche for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { 591536ae14eSBart Van Assche struct srp_request *req = &target->req_ring[i]; 592536ae14eSBart Van Assche if (req->scmnd) 593526b4caaSIshai Rabinovitz srp_reset_req(target, req); 594536ae14eSBart Van Assche } 595aef9ec39SRoland Dreier 596536ae14eSBart Van Assche INIT_LIST_HEAD(&target->free_tx); 597dcb4cb85SBart Van Assche for (i = 0; i < SRP_SQ_SIZE; ++i) 598536ae14eSBart Van Assche list_add(&target->tx_ring[i]->list, &target->free_tx); 599aef9ec39SRoland Dreier 6001033ff67SIshai Rabinovitz target->qp_in_error = 0; 601aef9ec39SRoland Dreier ret = srp_connect_target(target); 602aef9ec39SRoland Dreier if (ret) 603aef9ec39SRoland Dreier goto err; 604aef9ec39SRoland Dreier 6059709f0e0SBart Van Assche if (!srp_change_state(target, SRP_TARGET_CONNECTING, SRP_TARGET_LIVE)) 606aef9ec39SRoland Dreier ret = -EAGAIN; 607aef9ec39SRoland Dreier 608aef9ec39SRoland Dreier return ret; 609aef9ec39SRoland Dreier 610aef9ec39SRoland Dreier err: 6117aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 6127aa54bd7SDavid Dillow PFX "reconnect failed (%d), removing target port.\n", ret); 613aef9ec39SRoland Dreier 614aef9ec39SRoland Dreier /* 615aef9ec39SRoland Dreier * We couldn't reconnect, so kill our target port off. 6169709f0e0SBart Van Assche * However, we have to defer the real removal because we 6179709f0e0SBart Van Assche * are in the context of the SCSI error handler now, which 6189709f0e0SBart Van Assche * will deadlock if we call scsi_remove_host(). 6199709f0e0SBart Van Assche * 6209709f0e0SBart Van Assche * Schedule our work inside the lock to avoid a race with 6219709f0e0SBart Van Assche * the flush_scheduled_work() in srp_remove_one(). 622aef9ec39SRoland Dreier */ 623e9684678SBart Van Assche spin_lock_irq(&target->lock); 624aef9ec39SRoland Dreier if (target->state == SRP_TARGET_CONNECTING) { 625aef9ec39SRoland Dreier target->state = SRP_TARGET_DEAD; 626c4028958SDavid Howells INIT_WORK(&target->work, srp_remove_work); 627f0626710STejun Heo queue_work(ib_wq, &target->work); 628aef9ec39SRoland Dreier } 629e9684678SBart Van Assche spin_unlock_irq(&target->lock); 630aef9ec39SRoland Dreier 631aef9ec39SRoland Dreier return ret; 632aef9ec39SRoland Dreier } 633aef9ec39SRoland Dreier 634559ce8f1SIshai Rabinovitz static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat, 635f5358a17SRoland Dreier int sg_cnt, struct srp_request *req, 636f5358a17SRoland Dreier struct srp_direct_buf *buf) 637f5358a17SRoland Dreier { 638f5358a17SRoland Dreier u64 io_addr = 0; 639f5358a17SRoland Dreier u64 *dma_pages; 640f5358a17SRoland Dreier u32 len; 641f5358a17SRoland Dreier int page_cnt; 642f5358a17SRoland Dreier int i, j; 643f5358a17SRoland Dreier int ret; 64405321937SGreg Kroah-Hartman struct srp_device *dev = target->srp_host->srp_dev; 64585507bccSRalph Campbell struct ib_device *ibdev = dev->dev; 646bb350d1dSFUJITA Tomonori struct scatterlist *sg; 647f5358a17SRoland Dreier 648f5358a17SRoland Dreier if (!dev->fmr_pool) 649f5358a17SRoland Dreier return -ENODEV; 650f5358a17SRoland Dreier 651*8c4037b5SDavid Dillow if (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask) 652559ce8f1SIshai Rabinovitz return -EINVAL; 653559ce8f1SIshai Rabinovitz 654f5358a17SRoland Dreier len = page_cnt = 0; 655bb350d1dSFUJITA Tomonori scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) { 656bb350d1dSFUJITA Tomonori unsigned int dma_len = ib_sg_dma_len(ibdev, sg); 65785507bccSRalph Campbell 658bb350d1dSFUJITA Tomonori if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) { 659f5358a17SRoland Dreier if (i > 0) 660f5358a17SRoland Dreier return -EINVAL; 661f5358a17SRoland Dreier else 662f5358a17SRoland Dreier ++page_cnt; 663f5358a17SRoland Dreier } 664bb350d1dSFUJITA Tomonori if ((ib_sg_dma_address(ibdev, sg) + dma_len) & 665f5358a17SRoland Dreier ~dev->fmr_page_mask) { 666f5358a17SRoland Dreier if (i < sg_cnt - 1) 667f5358a17SRoland Dreier return -EINVAL; 668f5358a17SRoland Dreier else 669f5358a17SRoland Dreier ++page_cnt; 670f5358a17SRoland Dreier } 671f5358a17SRoland Dreier 67285507bccSRalph Campbell len += dma_len; 673f5358a17SRoland Dreier } 674f5358a17SRoland Dreier 675f5358a17SRoland Dreier page_cnt += len >> dev->fmr_page_shift; 676f5358a17SRoland Dreier if (page_cnt > SRP_FMR_SIZE) 677f5358a17SRoland Dreier return -ENOMEM; 678f5358a17SRoland Dreier 679f5358a17SRoland Dreier dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC); 680f5358a17SRoland Dreier if (!dma_pages) 681f5358a17SRoland Dreier return -ENOMEM; 682f5358a17SRoland Dreier 683f5358a17SRoland Dreier page_cnt = 0; 684bb350d1dSFUJITA Tomonori scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) { 685bb350d1dSFUJITA Tomonori unsigned int dma_len = ib_sg_dma_len(ibdev, sg); 68685507bccSRalph Campbell 68785507bccSRalph Campbell for (j = 0; j < dma_len; j += dev->fmr_page_size) 688f5358a17SRoland Dreier dma_pages[page_cnt++] = 689bb350d1dSFUJITA Tomonori (ib_sg_dma_address(ibdev, sg) & 69085507bccSRalph Campbell dev->fmr_page_mask) + j; 69185507bccSRalph Campbell } 692f5358a17SRoland Dreier 693f5358a17SRoland Dreier req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool, 694adfaa888SMichael S. Tsirkin dma_pages, page_cnt, io_addr); 695f5358a17SRoland Dreier if (IS_ERR(req->fmr)) { 696f5358a17SRoland Dreier ret = PTR_ERR(req->fmr); 6976583eb3dSVu Pham req->fmr = NULL; 698f5358a17SRoland Dreier goto out; 699f5358a17SRoland Dreier } 700f5358a17SRoland Dreier 70185507bccSRalph Campbell buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) & 70285507bccSRalph Campbell ~dev->fmr_page_mask); 703f5358a17SRoland Dreier buf->key = cpu_to_be32(req->fmr->fmr->rkey); 704f5358a17SRoland Dreier buf->len = cpu_to_be32(len); 705f5358a17SRoland Dreier 706f5358a17SRoland Dreier ret = 0; 707f5358a17SRoland Dreier 708f5358a17SRoland Dreier out: 709f5358a17SRoland Dreier kfree(dma_pages); 710f5358a17SRoland Dreier 711f5358a17SRoland Dreier return ret; 712f5358a17SRoland Dreier } 713f5358a17SRoland Dreier 714aef9ec39SRoland Dreier static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target, 715aef9ec39SRoland Dreier struct srp_request *req) 716aef9ec39SRoland Dreier { 717cf368713SRoland Dreier struct scatterlist *scat; 718aef9ec39SRoland Dreier struct srp_cmd *cmd = req->cmd->buf; 719cf368713SRoland Dreier int len, nents, count; 720f5358a17SRoland Dreier u8 fmt = SRP_DATA_DESC_DIRECT; 72185507bccSRalph Campbell struct srp_device *dev; 72285507bccSRalph Campbell struct ib_device *ibdev; 723aef9ec39SRoland Dreier 724bb350d1dSFUJITA Tomonori if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE) 725aef9ec39SRoland Dreier return sizeof (struct srp_cmd); 726aef9ec39SRoland Dreier 727aef9ec39SRoland Dreier if (scmnd->sc_data_direction != DMA_FROM_DEVICE && 728aef9ec39SRoland Dreier scmnd->sc_data_direction != DMA_TO_DEVICE) { 7297aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 7307aa54bd7SDavid Dillow PFX "Unhandled data direction %d\n", 731aef9ec39SRoland Dreier scmnd->sc_data_direction); 732aef9ec39SRoland Dreier return -EINVAL; 733aef9ec39SRoland Dreier } 734aef9ec39SRoland Dreier 735bb350d1dSFUJITA Tomonori nents = scsi_sg_count(scmnd); 736bb350d1dSFUJITA Tomonori scat = scsi_sglist(scmnd); 737aef9ec39SRoland Dreier 73805321937SGreg Kroah-Hartman dev = target->srp_host->srp_dev; 73985507bccSRalph Campbell ibdev = dev->dev; 74085507bccSRalph Campbell 74185507bccSRalph Campbell count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction); 742aef9ec39SRoland Dreier 743aef9ec39SRoland Dreier fmt = SRP_DATA_DESC_DIRECT; 744f5358a17SRoland Dreier len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf); 745f5358a17SRoland Dreier 746f5358a17SRoland Dreier if (count == 1) { 747f5358a17SRoland Dreier /* 748f5358a17SRoland Dreier * The midlayer only generated a single gather/scatter 749f5358a17SRoland Dreier * entry, or DMA mapping coalesced everything to a 750f5358a17SRoland Dreier * single entry. So a direct descriptor along with 751f5358a17SRoland Dreier * the DMA MR suffices. 752f5358a17SRoland Dreier */ 753f5358a17SRoland Dreier struct srp_direct_buf *buf = (void *) cmd->add_data; 754aef9ec39SRoland Dreier 75585507bccSRalph Campbell buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat)); 7569af76271SDavid Dillow buf->key = cpu_to_be32(target->rkey); 75785507bccSRalph Campbell buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat)); 758559ce8f1SIshai Rabinovitz } else if (srp_map_fmr(target, scat, count, req, 759f5358a17SRoland Dreier (void *) cmd->add_data)) { 760f5358a17SRoland Dreier /* 761f5358a17SRoland Dreier * FMR mapping failed, and the scatterlist has more 762f5358a17SRoland Dreier * than one entry. Generate an indirect memory 763f5358a17SRoland Dreier * descriptor. 764f5358a17SRoland Dreier */ 765aef9ec39SRoland Dreier struct srp_indirect_buf *buf = (void *) cmd->add_data; 766bb350d1dSFUJITA Tomonori struct scatterlist *sg; 767aef9ec39SRoland Dreier u32 datalen = 0; 768f5358a17SRoland Dreier int i; 769aef9ec39SRoland Dreier 770aef9ec39SRoland Dreier fmt = SRP_DATA_DESC_INDIRECT; 771f5358a17SRoland Dreier len = sizeof (struct srp_cmd) + 772f5358a17SRoland Dreier sizeof (struct srp_indirect_buf) + 773f5358a17SRoland Dreier count * sizeof (struct srp_direct_buf); 774f5358a17SRoland Dreier 775bb350d1dSFUJITA Tomonori scsi_for_each_sg(scmnd, sg, count, i) { 776bb350d1dSFUJITA Tomonori unsigned int dma_len = ib_sg_dma_len(ibdev, sg); 77785507bccSRalph Campbell 778f5358a17SRoland Dreier buf->desc_list[i].va = 779bb350d1dSFUJITA Tomonori cpu_to_be64(ib_sg_dma_address(ibdev, sg)); 780f5358a17SRoland Dreier buf->desc_list[i].key = 7819af76271SDavid Dillow cpu_to_be32(target->rkey); 78285507bccSRalph Campbell buf->desc_list[i].len = cpu_to_be32(dma_len); 78385507bccSRalph Campbell datalen += dma_len; 784f5358a17SRoland Dreier } 785aef9ec39SRoland Dreier 786aef9ec39SRoland Dreier if (scmnd->sc_data_direction == DMA_TO_DEVICE) 787cf368713SRoland Dreier cmd->data_out_desc_cnt = count; 788aef9ec39SRoland Dreier else 789cf368713SRoland Dreier cmd->data_in_desc_cnt = count; 790aef9ec39SRoland Dreier 791f5358a17SRoland Dreier buf->table_desc.va = 792f5358a17SRoland Dreier cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf); 793aef9ec39SRoland Dreier buf->table_desc.key = 7949af76271SDavid Dillow cpu_to_be32(target->rkey); 795aef9ec39SRoland Dreier buf->table_desc.len = 796cf368713SRoland Dreier cpu_to_be32(count * sizeof (struct srp_direct_buf)); 797aef9ec39SRoland Dreier 798aef9ec39SRoland Dreier buf->len = cpu_to_be32(datalen); 799aef9ec39SRoland Dreier } 800aef9ec39SRoland Dreier 801aef9ec39SRoland Dreier if (scmnd->sc_data_direction == DMA_TO_DEVICE) 802aef9ec39SRoland Dreier cmd->buf_fmt = fmt << 4; 803aef9ec39SRoland Dreier else 804aef9ec39SRoland Dreier cmd->buf_fmt = fmt; 805aef9ec39SRoland Dreier 806aef9ec39SRoland Dreier return len; 807aef9ec39SRoland Dreier } 808aef9ec39SRoland Dreier 80905a1d750SDavid Dillow /* 81076c75b25SBart Van Assche * Return an IU and possible credit to the free pool 81176c75b25SBart Van Assche */ 81276c75b25SBart Van Assche static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu, 81376c75b25SBart Van Assche enum srp_iu_type iu_type) 81476c75b25SBart Van Assche { 81576c75b25SBart Van Assche unsigned long flags; 81676c75b25SBart Van Assche 817e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 81876c75b25SBart Van Assche list_add(&iu->list, &target->free_tx); 81976c75b25SBart Van Assche if (iu_type != SRP_IU_RSP) 82076c75b25SBart Van Assche ++target->req_lim; 821e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 82276c75b25SBart Van Assche } 82376c75b25SBart Van Assche 82476c75b25SBart Van Assche /* 825e9684678SBart Van Assche * Must be called with target->lock held to protect req_lim and free_tx. 826e9684678SBart Van Assche * If IU is not sent, it must be returned using srp_put_tx_iu(). 82705a1d750SDavid Dillow * 82805a1d750SDavid Dillow * Note: 82905a1d750SDavid Dillow * An upper limit for the number of allocated information units for each 83005a1d750SDavid Dillow * request type is: 83105a1d750SDavid Dillow * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues 83205a1d750SDavid Dillow * more than Scsi_Host.can_queue requests. 83305a1d750SDavid Dillow * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE. 83405a1d750SDavid Dillow * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than 83505a1d750SDavid Dillow * one unanswered SRP request to an initiator. 83605a1d750SDavid Dillow */ 83705a1d750SDavid Dillow static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target, 83805a1d750SDavid Dillow enum srp_iu_type iu_type) 83905a1d750SDavid Dillow { 84005a1d750SDavid Dillow s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE; 84105a1d750SDavid Dillow struct srp_iu *iu; 84205a1d750SDavid Dillow 84305a1d750SDavid Dillow srp_send_completion(target->send_cq, target); 84405a1d750SDavid Dillow 845dcb4cb85SBart Van Assche if (list_empty(&target->free_tx)) 84605a1d750SDavid Dillow return NULL; 84705a1d750SDavid Dillow 84805a1d750SDavid Dillow /* Initiator responses to target requests do not consume credits */ 84976c75b25SBart Van Assche if (iu_type != SRP_IU_RSP) { 85076c75b25SBart Van Assche if (target->req_lim <= rsv) { 85105a1d750SDavid Dillow ++target->zero_req_lim; 85205a1d750SDavid Dillow return NULL; 85305a1d750SDavid Dillow } 85405a1d750SDavid Dillow 85576c75b25SBart Van Assche --target->req_lim; 85676c75b25SBart Van Assche } 85776c75b25SBart Van Assche 858dcb4cb85SBart Van Assche iu = list_first_entry(&target->free_tx, struct srp_iu, list); 85976c75b25SBart Van Assche list_del(&iu->list); 86005a1d750SDavid Dillow return iu; 86105a1d750SDavid Dillow } 86205a1d750SDavid Dillow 86376c75b25SBart Van Assche static int srp_post_send(struct srp_target_port *target, 86405a1d750SDavid Dillow struct srp_iu *iu, int len) 86505a1d750SDavid Dillow { 86605a1d750SDavid Dillow struct ib_sge list; 86705a1d750SDavid Dillow struct ib_send_wr wr, *bad_wr; 86805a1d750SDavid Dillow 86905a1d750SDavid Dillow list.addr = iu->dma; 87005a1d750SDavid Dillow list.length = len; 8719af76271SDavid Dillow list.lkey = target->lkey; 87205a1d750SDavid Dillow 87305a1d750SDavid Dillow wr.next = NULL; 874dcb4cb85SBart Van Assche wr.wr_id = (uintptr_t) iu; 87505a1d750SDavid Dillow wr.sg_list = &list; 87605a1d750SDavid Dillow wr.num_sge = 1; 87705a1d750SDavid Dillow wr.opcode = IB_WR_SEND; 87805a1d750SDavid Dillow wr.send_flags = IB_SEND_SIGNALED; 87905a1d750SDavid Dillow 88076c75b25SBart Van Assche return ib_post_send(target->qp, &wr, &bad_wr); 88105a1d750SDavid Dillow } 88205a1d750SDavid Dillow 883dcb4cb85SBart Van Assche static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu) 884c996bb47SBart Van Assche { 885c996bb47SBart Van Assche struct ib_recv_wr wr, *bad_wr; 886dcb4cb85SBart Van Assche struct ib_sge list; 887c996bb47SBart Van Assche 888c996bb47SBart Van Assche list.addr = iu->dma; 889c996bb47SBart Van Assche list.length = iu->size; 8909af76271SDavid Dillow list.lkey = target->lkey; 891c996bb47SBart Van Assche 892c996bb47SBart Van Assche wr.next = NULL; 893dcb4cb85SBart Van Assche wr.wr_id = (uintptr_t) iu; 894c996bb47SBart Van Assche wr.sg_list = &list; 895c996bb47SBart Van Assche wr.num_sge = 1; 896c996bb47SBart Van Assche 897dcb4cb85SBart Van Assche return ib_post_recv(target->qp, &wr, &bad_wr); 898c996bb47SBart Van Assche } 899c996bb47SBart Van Assche 900aef9ec39SRoland Dreier static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp) 901aef9ec39SRoland Dreier { 902aef9ec39SRoland Dreier struct srp_request *req; 903aef9ec39SRoland Dreier struct scsi_cmnd *scmnd; 904aef9ec39SRoland Dreier unsigned long flags; 905aef9ec39SRoland Dreier 906aef9ec39SRoland Dreier if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) { 907e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 90894a9174cSBart Van Assche target->req_lim += be32_to_cpu(rsp->req_lim_delta); 909e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 91094a9174cSBart Van Assche 911f8b6e31eSDavid Dillow target->tsk_mgmt_status = -1; 912f8b6e31eSDavid Dillow if (be32_to_cpu(rsp->resp_data_len) >= 4) 913f8b6e31eSDavid Dillow target->tsk_mgmt_status = rsp->data[3]; 914f8b6e31eSDavid Dillow complete(&target->tsk_mgmt_done); 915aef9ec39SRoland Dreier } else { 916f8b6e31eSDavid Dillow req = &target->req_ring[rsp->tag]; 917aef9ec39SRoland Dreier scmnd = req->scmnd; 918aef9ec39SRoland Dreier if (!scmnd) 9197aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 9207aa54bd7SDavid Dillow "Null scmnd for RSP w/tag %016llx\n", 921aef9ec39SRoland Dreier (unsigned long long) rsp->tag); 922aef9ec39SRoland Dreier scmnd->result = rsp->status; 923aef9ec39SRoland Dreier 924aef9ec39SRoland Dreier if (rsp->flags & SRP_RSP_FLAG_SNSVALID) { 925aef9ec39SRoland Dreier memcpy(scmnd->sense_buffer, rsp->data + 926aef9ec39SRoland Dreier be32_to_cpu(rsp->resp_data_len), 927aef9ec39SRoland Dreier min_t(int, be32_to_cpu(rsp->sense_data_len), 928aef9ec39SRoland Dreier SCSI_SENSE_BUFFERSIZE)); 929aef9ec39SRoland Dreier } 930aef9ec39SRoland Dreier 931aef9ec39SRoland Dreier if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER)) 932bb350d1dSFUJITA Tomonori scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt)); 933aef9ec39SRoland Dreier else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER)) 934bb350d1dSFUJITA Tomonori scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt)); 935aef9ec39SRoland Dreier 93694a9174cSBart Van Assche srp_remove_req(target, req, be32_to_cpu(rsp->req_lim_delta)); 937f8b6e31eSDavid Dillow scmnd->host_scribble = NULL; 938aef9ec39SRoland Dreier scmnd->scsi_done(scmnd); 939aef9ec39SRoland Dreier } 940aef9ec39SRoland Dreier } 941aef9ec39SRoland Dreier 942bb12588aSDavid Dillow static int srp_response_common(struct srp_target_port *target, s32 req_delta, 943bb12588aSDavid Dillow void *rsp, int len) 944bb12588aSDavid Dillow { 94576c75b25SBart Van Assche struct ib_device *dev = target->srp_host->srp_dev->dev; 946bb12588aSDavid Dillow unsigned long flags; 947bb12588aSDavid Dillow struct srp_iu *iu; 94876c75b25SBart Van Assche int err; 949bb12588aSDavid Dillow 950e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 951bb12588aSDavid Dillow target->req_lim += req_delta; 952bb12588aSDavid Dillow iu = __srp_get_tx_iu(target, SRP_IU_RSP); 953e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 95476c75b25SBart Van Assche 955bb12588aSDavid Dillow if (!iu) { 956bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 957bb12588aSDavid Dillow "no IU available to send response\n"); 95876c75b25SBart Van Assche return 1; 959bb12588aSDavid Dillow } 960bb12588aSDavid Dillow 961bb12588aSDavid Dillow ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE); 962bb12588aSDavid Dillow memcpy(iu->buf, rsp, len); 963bb12588aSDavid Dillow ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE); 964bb12588aSDavid Dillow 96576c75b25SBart Van Assche err = srp_post_send(target, iu, len); 96676c75b25SBart Van Assche if (err) { 967bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 968bb12588aSDavid Dillow "unable to post response: %d\n", err); 96976c75b25SBart Van Assche srp_put_tx_iu(target, iu, SRP_IU_RSP); 97076c75b25SBart Van Assche } 971bb12588aSDavid Dillow 972bb12588aSDavid Dillow return err; 973bb12588aSDavid Dillow } 974bb12588aSDavid Dillow 975bb12588aSDavid Dillow static void srp_process_cred_req(struct srp_target_port *target, 976bb12588aSDavid Dillow struct srp_cred_req *req) 977bb12588aSDavid Dillow { 978bb12588aSDavid Dillow struct srp_cred_rsp rsp = { 979bb12588aSDavid Dillow .opcode = SRP_CRED_RSP, 980bb12588aSDavid Dillow .tag = req->tag, 981bb12588aSDavid Dillow }; 982bb12588aSDavid Dillow s32 delta = be32_to_cpu(req->req_lim_delta); 983bb12588aSDavid Dillow 984bb12588aSDavid Dillow if (srp_response_common(target, delta, &rsp, sizeof rsp)) 985bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 986bb12588aSDavid Dillow "problems processing SRP_CRED_REQ\n"); 987bb12588aSDavid Dillow } 988bb12588aSDavid Dillow 989bb12588aSDavid Dillow static void srp_process_aer_req(struct srp_target_port *target, 990bb12588aSDavid Dillow struct srp_aer_req *req) 991bb12588aSDavid Dillow { 992bb12588aSDavid Dillow struct srp_aer_rsp rsp = { 993bb12588aSDavid Dillow .opcode = SRP_AER_RSP, 994bb12588aSDavid Dillow .tag = req->tag, 995bb12588aSDavid Dillow }; 996bb12588aSDavid Dillow s32 delta = be32_to_cpu(req->req_lim_delta); 997bb12588aSDavid Dillow 998bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 999bb12588aSDavid Dillow "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun)); 1000bb12588aSDavid Dillow 1001bb12588aSDavid Dillow if (srp_response_common(target, delta, &rsp, sizeof rsp)) 1002bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 1003bb12588aSDavid Dillow "problems processing SRP_AER_REQ\n"); 1004bb12588aSDavid Dillow } 1005bb12588aSDavid Dillow 1006aef9ec39SRoland Dreier static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc) 1007aef9ec39SRoland Dreier { 1008dcb4cb85SBart Van Assche struct ib_device *dev = target->srp_host->srp_dev->dev; 1009dcb4cb85SBart Van Assche struct srp_iu *iu = (struct srp_iu *) wc->wr_id; 1010c996bb47SBart Van Assche int res; 1011aef9ec39SRoland Dreier u8 opcode; 1012aef9ec39SRoland Dreier 101385507bccSRalph Campbell ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len, 101485507bccSRalph Campbell DMA_FROM_DEVICE); 1015aef9ec39SRoland Dreier 1016aef9ec39SRoland Dreier opcode = *(u8 *) iu->buf; 1017aef9ec39SRoland Dreier 1018aef9ec39SRoland Dreier if (0) { 10197aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 10207aa54bd7SDavid Dillow PFX "recv completion, opcode 0x%02x\n", opcode); 10217a700811SBart Van Assche print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1, 10227a700811SBart Van Assche iu->buf, wc->byte_len, true); 1023aef9ec39SRoland Dreier } 1024aef9ec39SRoland Dreier 1025aef9ec39SRoland Dreier switch (opcode) { 1026aef9ec39SRoland Dreier case SRP_RSP: 1027aef9ec39SRoland Dreier srp_process_rsp(target, iu->buf); 1028aef9ec39SRoland Dreier break; 1029aef9ec39SRoland Dreier 1030bb12588aSDavid Dillow case SRP_CRED_REQ: 1031bb12588aSDavid Dillow srp_process_cred_req(target, iu->buf); 1032bb12588aSDavid Dillow break; 1033bb12588aSDavid Dillow 1034bb12588aSDavid Dillow case SRP_AER_REQ: 1035bb12588aSDavid Dillow srp_process_aer_req(target, iu->buf); 1036bb12588aSDavid Dillow break; 1037bb12588aSDavid Dillow 1038aef9ec39SRoland Dreier case SRP_T_LOGOUT: 1039aef9ec39SRoland Dreier /* XXX Handle target logout */ 10407aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 10417aa54bd7SDavid Dillow PFX "Got target logout request\n"); 1042aef9ec39SRoland Dreier break; 1043aef9ec39SRoland Dreier 1044aef9ec39SRoland Dreier default: 10457aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 10467aa54bd7SDavid Dillow PFX "Unhandled SRP opcode 0x%02x\n", opcode); 1047aef9ec39SRoland Dreier break; 1048aef9ec39SRoland Dreier } 1049aef9ec39SRoland Dreier 105085507bccSRalph Campbell ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len, 105185507bccSRalph Campbell DMA_FROM_DEVICE); 1052c996bb47SBart Van Assche 1053dcb4cb85SBart Van Assche res = srp_post_recv(target, iu); 1054c996bb47SBart Van Assche if (res != 0) 1055c996bb47SBart Van Assche shost_printk(KERN_ERR, target->scsi_host, 1056c996bb47SBart Van Assche PFX "Recv failed with error code %d\n", res); 1057aef9ec39SRoland Dreier } 1058aef9ec39SRoland Dreier 10599c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr) 1060aef9ec39SRoland Dreier { 1061aef9ec39SRoland Dreier struct srp_target_port *target = target_ptr; 1062aef9ec39SRoland Dreier struct ib_wc wc; 1063aef9ec39SRoland Dreier 1064aef9ec39SRoland Dreier ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); 1065aef9ec39SRoland Dreier while (ib_poll_cq(cq, 1, &wc) > 0) { 1066aef9ec39SRoland Dreier if (wc.status) { 10677aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 10689c03dc9fSBart Van Assche PFX "failed receive status %d\n", 1069aef9ec39SRoland Dreier wc.status); 10701033ff67SIshai Rabinovitz target->qp_in_error = 1; 1071aef9ec39SRoland Dreier break; 1072aef9ec39SRoland Dreier } 1073aef9ec39SRoland Dreier 1074aef9ec39SRoland Dreier srp_handle_recv(target, &wc); 10759c03dc9fSBart Van Assche } 10769c03dc9fSBart Van Assche } 10779c03dc9fSBart Van Assche 10789c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr) 10799c03dc9fSBart Van Assche { 10809c03dc9fSBart Van Assche struct srp_target_port *target = target_ptr; 10819c03dc9fSBart Van Assche struct ib_wc wc; 1082dcb4cb85SBart Van Assche struct srp_iu *iu; 10839c03dc9fSBart Van Assche 10849c03dc9fSBart Van Assche while (ib_poll_cq(cq, 1, &wc) > 0) { 10859c03dc9fSBart Van Assche if (wc.status) { 10869c03dc9fSBart Van Assche shost_printk(KERN_ERR, target->scsi_host, 10879c03dc9fSBart Van Assche PFX "failed send status %d\n", 10889c03dc9fSBart Van Assche wc.status); 10899c03dc9fSBart Van Assche target->qp_in_error = 1; 10909c03dc9fSBart Van Assche break; 10919c03dc9fSBart Van Assche } 10929c03dc9fSBart Van Assche 1093dcb4cb85SBart Van Assche iu = (struct srp_iu *) wc.wr_id; 1094dcb4cb85SBart Van Assche list_add(&iu->list, &target->free_tx); 1095aef9ec39SRoland Dreier } 1096aef9ec39SRoland Dreier } 1097aef9ec39SRoland Dreier 109876c75b25SBart Van Assche static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd) 1099aef9ec39SRoland Dreier { 110076c75b25SBart Van Assche struct srp_target_port *target = host_to_target(shost); 1101aef9ec39SRoland Dreier struct srp_request *req; 1102aef9ec39SRoland Dreier struct srp_iu *iu; 1103aef9ec39SRoland Dreier struct srp_cmd *cmd; 110485507bccSRalph Campbell struct ib_device *dev; 110576c75b25SBart Van Assche unsigned long flags; 1106aef9ec39SRoland Dreier int len; 1107aef9ec39SRoland Dreier 1108aef9ec39SRoland Dreier if (target->state == SRP_TARGET_CONNECTING) 1109aef9ec39SRoland Dreier goto err; 1110aef9ec39SRoland Dreier 1111aef9ec39SRoland Dreier if (target->state == SRP_TARGET_DEAD || 1112aef9ec39SRoland Dreier target->state == SRP_TARGET_REMOVED) { 1113aef9ec39SRoland Dreier scmnd->result = DID_BAD_TARGET << 16; 111476c75b25SBart Van Assche scmnd->scsi_done(scmnd); 1115aef9ec39SRoland Dreier return 0; 1116aef9ec39SRoland Dreier } 1117aef9ec39SRoland Dreier 1118e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 1119bb12588aSDavid Dillow iu = __srp_get_tx_iu(target, SRP_IU_CMD); 1120aef9ec39SRoland Dreier if (!iu) 1121695b8349SBart Van Assche goto err_unlock; 1122695b8349SBart Van Assche 1123695b8349SBart Van Assche req = list_first_entry(&target->free_reqs, struct srp_request, list); 1124695b8349SBart Van Assche list_del(&req->list); 1125695b8349SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 1126aef9ec39SRoland Dreier 112705321937SGreg Kroah-Hartman dev = target->srp_host->srp_dev->dev; 112885507bccSRalph Campbell ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len, 112985507bccSRalph Campbell DMA_TO_DEVICE); 1130aef9ec39SRoland Dreier 1131aef9ec39SRoland Dreier scmnd->result = 0; 1132f8b6e31eSDavid Dillow scmnd->host_scribble = (void *) req; 1133aef9ec39SRoland Dreier 1134aef9ec39SRoland Dreier cmd = iu->buf; 1135aef9ec39SRoland Dreier memset(cmd, 0, sizeof *cmd); 1136aef9ec39SRoland Dreier 1137aef9ec39SRoland Dreier cmd->opcode = SRP_CMD; 1138aef9ec39SRoland Dreier cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48); 1139d945e1dfSRoland Dreier cmd->tag = req->index; 1140aef9ec39SRoland Dreier memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len); 1141aef9ec39SRoland Dreier 1142aef9ec39SRoland Dreier req->scmnd = scmnd; 1143aef9ec39SRoland Dreier req->cmd = iu; 1144aef9ec39SRoland Dreier 1145aef9ec39SRoland Dreier len = srp_map_data(scmnd, target, req); 1146aef9ec39SRoland Dreier if (len < 0) { 11477aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 11487aa54bd7SDavid Dillow PFX "Failed to map data\n"); 114976c75b25SBart Van Assche goto err_iu; 1150aef9ec39SRoland Dreier } 1151aef9ec39SRoland Dreier 115285507bccSRalph Campbell ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len, 115385507bccSRalph Campbell DMA_TO_DEVICE); 1154aef9ec39SRoland Dreier 115576c75b25SBart Van Assche if (srp_post_send(target, iu, len)) { 11567aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n"); 1157aef9ec39SRoland Dreier goto err_unmap; 1158aef9ec39SRoland Dreier } 1159aef9ec39SRoland Dreier 1160aef9ec39SRoland Dreier return 0; 1161aef9ec39SRoland Dreier 1162aef9ec39SRoland Dreier err_unmap: 1163aef9ec39SRoland Dreier srp_unmap_data(scmnd, target, req); 1164aef9ec39SRoland Dreier 116576c75b25SBart Van Assche err_iu: 116676c75b25SBart Van Assche srp_put_tx_iu(target, iu, SRP_IU_CMD); 116776c75b25SBart Van Assche 1168e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 116976c75b25SBart Van Assche list_add(&req->list, &target->free_reqs); 1170695b8349SBart Van Assche 1171695b8349SBart Van Assche err_unlock: 1172e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 117376c75b25SBart Van Assche 1174aef9ec39SRoland Dreier err: 1175aef9ec39SRoland Dreier return SCSI_MLQUEUE_HOST_BUSY; 1176aef9ec39SRoland Dreier } 1177aef9ec39SRoland Dreier 1178aef9ec39SRoland Dreier static int srp_alloc_iu_bufs(struct srp_target_port *target) 1179aef9ec39SRoland Dreier { 1180aef9ec39SRoland Dreier int i; 1181aef9ec39SRoland Dreier 1182aef9ec39SRoland Dreier for (i = 0; i < SRP_RQ_SIZE; ++i) { 1183aef9ec39SRoland Dreier target->rx_ring[i] = srp_alloc_iu(target->srp_host, 1184aef9ec39SRoland Dreier target->max_ti_iu_len, 1185aef9ec39SRoland Dreier GFP_KERNEL, DMA_FROM_DEVICE); 1186aef9ec39SRoland Dreier if (!target->rx_ring[i]) 1187aef9ec39SRoland Dreier goto err; 1188aef9ec39SRoland Dreier } 1189aef9ec39SRoland Dreier 1190dd5e6e38SBart Van Assche for (i = 0; i < SRP_SQ_SIZE; ++i) { 1191aef9ec39SRoland Dreier target->tx_ring[i] = srp_alloc_iu(target->srp_host, 119274b0a15bSVu Pham srp_max_iu_len, 1193aef9ec39SRoland Dreier GFP_KERNEL, DMA_TO_DEVICE); 1194aef9ec39SRoland Dreier if (!target->tx_ring[i]) 1195aef9ec39SRoland Dreier goto err; 1196dcb4cb85SBart Van Assche 1197dcb4cb85SBart Van Assche list_add(&target->tx_ring[i]->list, &target->free_tx); 1198aef9ec39SRoland Dreier } 1199aef9ec39SRoland Dreier 1200aef9ec39SRoland Dreier return 0; 1201aef9ec39SRoland Dreier 1202aef9ec39SRoland Dreier err: 1203aef9ec39SRoland Dreier for (i = 0; i < SRP_RQ_SIZE; ++i) { 1204aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->rx_ring[i]); 1205aef9ec39SRoland Dreier target->rx_ring[i] = NULL; 1206aef9ec39SRoland Dreier } 1207aef9ec39SRoland Dreier 1208dd5e6e38SBart Van Assche for (i = 0; i < SRP_SQ_SIZE; ++i) { 1209aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->tx_ring[i]); 1210aef9ec39SRoland Dreier target->tx_ring[i] = NULL; 1211aef9ec39SRoland Dreier } 1212aef9ec39SRoland Dreier 1213aef9ec39SRoland Dreier return -ENOMEM; 1214aef9ec39SRoland Dreier } 1215aef9ec39SRoland Dreier 1216aef9ec39SRoland Dreier static void srp_cm_rej_handler(struct ib_cm_id *cm_id, 1217aef9ec39SRoland Dreier struct ib_cm_event *event, 1218aef9ec39SRoland Dreier struct srp_target_port *target) 1219aef9ec39SRoland Dreier { 12207aa54bd7SDavid Dillow struct Scsi_Host *shost = target->scsi_host; 1221aef9ec39SRoland Dreier struct ib_class_port_info *cpi; 1222aef9ec39SRoland Dreier int opcode; 1223aef9ec39SRoland Dreier 1224aef9ec39SRoland Dreier switch (event->param.rej_rcvd.reason) { 1225aef9ec39SRoland Dreier case IB_CM_REJ_PORT_CM_REDIRECT: 1226aef9ec39SRoland Dreier cpi = event->param.rej_rcvd.ari; 1227aef9ec39SRoland Dreier target->path.dlid = cpi->redirect_lid; 1228aef9ec39SRoland Dreier target->path.pkey = cpi->redirect_pkey; 1229aef9ec39SRoland Dreier cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff; 1230aef9ec39SRoland Dreier memcpy(target->path.dgid.raw, cpi->redirect_gid, 16); 1231aef9ec39SRoland Dreier 1232aef9ec39SRoland Dreier target->status = target->path.dlid ? 1233aef9ec39SRoland Dreier SRP_DLID_REDIRECT : SRP_PORT_REDIRECT; 1234aef9ec39SRoland Dreier break; 1235aef9ec39SRoland Dreier 1236aef9ec39SRoland Dreier case IB_CM_REJ_PORT_REDIRECT: 12375d7cbfd6SRoland Dreier if (srp_target_is_topspin(target)) { 1238aef9ec39SRoland Dreier /* 1239aef9ec39SRoland Dreier * Topspin/Cisco SRP gateways incorrectly send 1240aef9ec39SRoland Dreier * reject reason code 25 when they mean 24 1241aef9ec39SRoland Dreier * (port redirect). 1242aef9ec39SRoland Dreier */ 1243aef9ec39SRoland Dreier memcpy(target->path.dgid.raw, 1244aef9ec39SRoland Dreier event->param.rej_rcvd.ari, 16); 1245aef9ec39SRoland Dreier 12467aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, shost, 12477aa54bd7SDavid Dillow PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n", 1248aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix), 1249aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id)); 1250aef9ec39SRoland Dreier 1251aef9ec39SRoland Dreier target->status = SRP_PORT_REDIRECT; 1252aef9ec39SRoland Dreier } else { 12537aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12547aa54bd7SDavid Dillow " REJ reason: IB_CM_REJ_PORT_REDIRECT\n"); 1255aef9ec39SRoland Dreier target->status = -ECONNRESET; 1256aef9ec39SRoland Dreier } 1257aef9ec39SRoland Dreier break; 1258aef9ec39SRoland Dreier 1259aef9ec39SRoland Dreier case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: 12607aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12617aa54bd7SDavid Dillow " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); 1262aef9ec39SRoland Dreier target->status = -ECONNRESET; 1263aef9ec39SRoland Dreier break; 1264aef9ec39SRoland Dreier 1265aef9ec39SRoland Dreier case IB_CM_REJ_CONSUMER_DEFINED: 1266aef9ec39SRoland Dreier opcode = *(u8 *) event->private_data; 1267aef9ec39SRoland Dreier if (opcode == SRP_LOGIN_REJ) { 1268aef9ec39SRoland Dreier struct srp_login_rej *rej = event->private_data; 1269aef9ec39SRoland Dreier u32 reason = be32_to_cpu(rej->reason); 1270aef9ec39SRoland Dreier 1271aef9ec39SRoland Dreier if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) 12727aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12737aa54bd7SDavid Dillow PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); 1274aef9ec39SRoland Dreier else 12757aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12767aa54bd7SDavid Dillow PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason); 1277aef9ec39SRoland Dreier } else 12787aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 12797aa54bd7SDavid Dillow " REJ reason: IB_CM_REJ_CONSUMER_DEFINED," 1280aef9ec39SRoland Dreier " opcode 0x%02x\n", opcode); 1281aef9ec39SRoland Dreier target->status = -ECONNRESET; 1282aef9ec39SRoland Dreier break; 1283aef9ec39SRoland Dreier 12849fe4bcf4SDavid Dillow case IB_CM_REJ_STALE_CONN: 12859fe4bcf4SDavid Dillow shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n"); 12869fe4bcf4SDavid Dillow target->status = SRP_STALE_CONN; 12879fe4bcf4SDavid Dillow break; 12889fe4bcf4SDavid Dillow 1289aef9ec39SRoland Dreier default: 12907aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n", 1291aef9ec39SRoland Dreier event->param.rej_rcvd.reason); 1292aef9ec39SRoland Dreier target->status = -ECONNRESET; 1293aef9ec39SRoland Dreier } 1294aef9ec39SRoland Dreier } 1295aef9ec39SRoland Dreier 1296aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) 1297aef9ec39SRoland Dreier { 1298aef9ec39SRoland Dreier struct srp_target_port *target = cm_id->context; 1299aef9ec39SRoland Dreier struct ib_qp_attr *qp_attr = NULL; 1300aef9ec39SRoland Dreier int attr_mask = 0; 1301aef9ec39SRoland Dreier int comp = 0; 1302aef9ec39SRoland Dreier int opcode = 0; 1303c996bb47SBart Van Assche int i; 1304aef9ec39SRoland Dreier 1305aef9ec39SRoland Dreier switch (event->event) { 1306aef9ec39SRoland Dreier case IB_CM_REQ_ERROR: 13077aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, 13087aa54bd7SDavid Dillow PFX "Sending CM REQ failed\n"); 1309aef9ec39SRoland Dreier comp = 1; 1310aef9ec39SRoland Dreier target->status = -ECONNRESET; 1311aef9ec39SRoland Dreier break; 1312aef9ec39SRoland Dreier 1313aef9ec39SRoland Dreier case IB_CM_REP_RECEIVED: 1314aef9ec39SRoland Dreier comp = 1; 1315aef9ec39SRoland Dreier opcode = *(u8 *) event->private_data; 1316aef9ec39SRoland Dreier 1317aef9ec39SRoland Dreier if (opcode == SRP_LOGIN_RSP) { 1318aef9ec39SRoland Dreier struct srp_login_rsp *rsp = event->private_data; 1319aef9ec39SRoland Dreier 1320aef9ec39SRoland Dreier target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len); 1321aef9ec39SRoland Dreier target->req_lim = be32_to_cpu(rsp->req_lim_delta); 1322aef9ec39SRoland Dreier 13237ade400aSBart Van Assche /* 13247ade400aSBart Van Assche * Reserve credits for task management so we don't 13257ade400aSBart Van Assche * bounce requests back to the SCSI mid-layer. 13267ade400aSBart Van Assche */ 13277ade400aSBart Van Assche target->scsi_host->can_queue 13287ade400aSBart Van Assche = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE, 1329aef9ec39SRoland Dreier target->scsi_host->can_queue); 1330aef9ec39SRoland Dreier } else { 13317aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 13327aa54bd7SDavid Dillow PFX "Unhandled RSP opcode %#x\n", opcode); 1333aef9ec39SRoland Dreier target->status = -ECONNRESET; 1334aef9ec39SRoland Dreier break; 1335aef9ec39SRoland Dreier } 1336aef9ec39SRoland Dreier 1337d2fcea7dSVu Pham if (!target->rx_ring[0]) { 1338aef9ec39SRoland Dreier target->status = srp_alloc_iu_bufs(target); 1339aef9ec39SRoland Dreier if (target->status) 1340aef9ec39SRoland Dreier break; 1341d2fcea7dSVu Pham } 1342aef9ec39SRoland Dreier 1343aef9ec39SRoland Dreier qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL); 1344aef9ec39SRoland Dreier if (!qp_attr) { 1345aef9ec39SRoland Dreier target->status = -ENOMEM; 1346aef9ec39SRoland Dreier break; 1347aef9ec39SRoland Dreier } 1348aef9ec39SRoland Dreier 1349aef9ec39SRoland Dreier qp_attr->qp_state = IB_QPS_RTR; 1350aef9ec39SRoland Dreier target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); 1351aef9ec39SRoland Dreier if (target->status) 1352aef9ec39SRoland Dreier break; 1353aef9ec39SRoland Dreier 1354aef9ec39SRoland Dreier target->status = ib_modify_qp(target->qp, qp_attr, attr_mask); 1355aef9ec39SRoland Dreier if (target->status) 1356aef9ec39SRoland Dreier break; 1357aef9ec39SRoland Dreier 1358c996bb47SBart Van Assche for (i = 0; i < SRP_RQ_SIZE; i++) { 1359dcb4cb85SBart Van Assche struct srp_iu *iu = target->rx_ring[i]; 1360dcb4cb85SBart Van Assche target->status = srp_post_recv(target, iu); 1361aef9ec39SRoland Dreier if (target->status) 1362aef9ec39SRoland Dreier break; 1363c996bb47SBart Van Assche } 1364c996bb47SBart Van Assche if (target->status) 1365c996bb47SBart Van Assche break; 1366aef9ec39SRoland Dreier 1367aef9ec39SRoland Dreier qp_attr->qp_state = IB_QPS_RTS; 1368aef9ec39SRoland Dreier target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); 1369aef9ec39SRoland Dreier if (target->status) 1370aef9ec39SRoland Dreier break; 1371aef9ec39SRoland Dreier 1372aef9ec39SRoland Dreier target->status = ib_modify_qp(target->qp, qp_attr, attr_mask); 1373aef9ec39SRoland Dreier if (target->status) 1374aef9ec39SRoland Dreier break; 1375aef9ec39SRoland Dreier 1376aef9ec39SRoland Dreier target->status = ib_send_cm_rtu(cm_id, NULL, 0); 1377aef9ec39SRoland Dreier if (target->status) 1378aef9ec39SRoland Dreier break; 1379aef9ec39SRoland Dreier 1380aef9ec39SRoland Dreier break; 1381aef9ec39SRoland Dreier 1382aef9ec39SRoland Dreier case IB_CM_REJ_RECEIVED: 13837aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n"); 1384aef9ec39SRoland Dreier comp = 1; 1385aef9ec39SRoland Dreier 1386aef9ec39SRoland Dreier srp_cm_rej_handler(cm_id, event, target); 1387aef9ec39SRoland Dreier break; 1388aef9ec39SRoland Dreier 1389b7ac4ab4SIshai Rabinovitz case IB_CM_DREQ_RECEIVED: 13907aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 13917aa54bd7SDavid Dillow PFX "DREQ received - connection closed\n"); 1392b7ac4ab4SIshai Rabinovitz if (ib_send_cm_drep(cm_id, NULL, 0)) 13937aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 13947aa54bd7SDavid Dillow PFX "Sending CM DREP failed\n"); 1395aef9ec39SRoland Dreier break; 1396aef9ec39SRoland Dreier 1397aef9ec39SRoland Dreier case IB_CM_TIMEWAIT_EXIT: 13987aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 13997aa54bd7SDavid Dillow PFX "connection closed\n"); 1400aef9ec39SRoland Dreier 1401aef9ec39SRoland Dreier comp = 1; 1402aef9ec39SRoland Dreier target->status = 0; 1403aef9ec39SRoland Dreier break; 1404aef9ec39SRoland Dreier 1405b7ac4ab4SIshai Rabinovitz case IB_CM_MRA_RECEIVED: 1406b7ac4ab4SIshai Rabinovitz case IB_CM_DREQ_ERROR: 1407b7ac4ab4SIshai Rabinovitz case IB_CM_DREP_RECEIVED: 1408b7ac4ab4SIshai Rabinovitz break; 1409b7ac4ab4SIshai Rabinovitz 1410aef9ec39SRoland Dreier default: 14117aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 14127aa54bd7SDavid Dillow PFX "Unhandled CM event %d\n", event->event); 1413aef9ec39SRoland Dreier break; 1414aef9ec39SRoland Dreier } 1415aef9ec39SRoland Dreier 1416aef9ec39SRoland Dreier if (comp) 1417aef9ec39SRoland Dreier complete(&target->done); 1418aef9ec39SRoland Dreier 1419aef9ec39SRoland Dreier kfree(qp_attr); 1420aef9ec39SRoland Dreier 1421aef9ec39SRoland Dreier return 0; 1422aef9ec39SRoland Dreier } 1423aef9ec39SRoland Dreier 1424d945e1dfSRoland Dreier static int srp_send_tsk_mgmt(struct srp_target_port *target, 1425f8b6e31eSDavid Dillow u64 req_tag, unsigned int lun, u8 func) 1426aef9ec39SRoland Dreier { 142719081f31SDavid Dillow struct ib_device *dev = target->srp_host->srp_dev->dev; 1428aef9ec39SRoland Dreier struct srp_iu *iu; 1429aef9ec39SRoland Dreier struct srp_tsk_mgmt *tsk_mgmt; 1430aef9ec39SRoland Dreier 14311285b3a0SRoland Dreier if (target->state == SRP_TARGET_DEAD || 1432f8b6e31eSDavid Dillow target->state == SRP_TARGET_REMOVED) 143376c75b25SBart Van Assche return -1; 14341285b3a0SRoland Dreier 1435f8b6e31eSDavid Dillow init_completion(&target->tsk_mgmt_done); 1436aef9ec39SRoland Dreier 1437e9684678SBart Van Assche spin_lock_irq(&target->lock); 1438bb12588aSDavid Dillow iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT); 1439e9684678SBart Van Assche spin_unlock_irq(&target->lock); 144076c75b25SBart Van Assche 1441aef9ec39SRoland Dreier if (!iu) 144276c75b25SBart Van Assche return -1; 1443aef9ec39SRoland Dreier 144419081f31SDavid Dillow ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt, 144519081f31SDavid Dillow DMA_TO_DEVICE); 1446aef9ec39SRoland Dreier tsk_mgmt = iu->buf; 1447aef9ec39SRoland Dreier memset(tsk_mgmt, 0, sizeof *tsk_mgmt); 1448aef9ec39SRoland Dreier 1449aef9ec39SRoland Dreier tsk_mgmt->opcode = SRP_TSK_MGMT; 1450f8b6e31eSDavid Dillow tsk_mgmt->lun = cpu_to_be64((u64) lun << 48); 1451f8b6e31eSDavid Dillow tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT; 1452aef9ec39SRoland Dreier tsk_mgmt->tsk_mgmt_func = func; 1453f8b6e31eSDavid Dillow tsk_mgmt->task_tag = req_tag; 1454aef9ec39SRoland Dreier 145519081f31SDavid Dillow ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt, 145619081f31SDavid Dillow DMA_TO_DEVICE); 145776c75b25SBart Van Assche if (srp_post_send(target, iu, sizeof *tsk_mgmt)) { 145876c75b25SBart Van Assche srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT); 145976c75b25SBart Van Assche return -1; 146076c75b25SBart Van Assche } 1461d945e1dfSRoland Dreier 1462f8b6e31eSDavid Dillow if (!wait_for_completion_timeout(&target->tsk_mgmt_done, 1463aef9ec39SRoland Dreier msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS))) 1464d945e1dfSRoland Dreier return -1; 1465aef9ec39SRoland Dreier 1466d945e1dfSRoland Dreier return 0; 1467d945e1dfSRoland Dreier } 1468d945e1dfSRoland Dreier 1469aef9ec39SRoland Dreier static int srp_abort(struct scsi_cmnd *scmnd) 1470aef9ec39SRoland Dreier { 1471d945e1dfSRoland Dreier struct srp_target_port *target = host_to_target(scmnd->device->host); 1472f8b6e31eSDavid Dillow struct srp_request *req = (struct srp_request *) scmnd->host_scribble; 1473d945e1dfSRoland Dreier int ret = SUCCESS; 1474d945e1dfSRoland Dreier 14757aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n"); 1476aef9ec39SRoland Dreier 1477f8b6e31eSDavid Dillow if (!req || target->qp_in_error) 14781033ff67SIshai Rabinovitz return FAILED; 1479f8b6e31eSDavid Dillow if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun, 1480f8b6e31eSDavid Dillow SRP_TSK_ABORT_TASK)) 1481d945e1dfSRoland Dreier return FAILED; 1482d945e1dfSRoland Dreier 1483f8b6e31eSDavid Dillow if (req->scmnd) { 1484f8b6e31eSDavid Dillow if (!target->tsk_mgmt_status) { 148594a9174cSBart Van Assche srp_remove_req(target, req, 0); 1486d945e1dfSRoland Dreier scmnd->result = DID_ABORT << 16; 1487d945e1dfSRoland Dreier } else 1488d945e1dfSRoland Dreier ret = FAILED; 1489f8b6e31eSDavid Dillow } 1490d945e1dfSRoland Dreier 1491d945e1dfSRoland Dreier return ret; 1492aef9ec39SRoland Dreier } 1493aef9ec39SRoland Dreier 1494aef9ec39SRoland Dreier static int srp_reset_device(struct scsi_cmnd *scmnd) 1495aef9ec39SRoland Dreier { 1496d945e1dfSRoland Dreier struct srp_target_port *target = host_to_target(scmnd->device->host); 1497536ae14eSBart Van Assche int i; 1498d945e1dfSRoland Dreier 14997aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n"); 1500aef9ec39SRoland Dreier 15011033ff67SIshai Rabinovitz if (target->qp_in_error) 15021033ff67SIshai Rabinovitz return FAILED; 1503f8b6e31eSDavid Dillow if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun, 1504f8b6e31eSDavid Dillow SRP_TSK_LUN_RESET)) 1505d945e1dfSRoland Dreier return FAILED; 1506f8b6e31eSDavid Dillow if (target->tsk_mgmt_status) 1507d945e1dfSRoland Dreier return FAILED; 1508d945e1dfSRoland Dreier 1509536ae14eSBart Van Assche for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { 1510536ae14eSBart Van Assche struct srp_request *req = &target->req_ring[i]; 1511f8b6e31eSDavid Dillow if (req->scmnd && req->scmnd->device == scmnd->device) 1512526b4caaSIshai Rabinovitz srp_reset_req(target, req); 1513536ae14eSBart Van Assche } 1514d945e1dfSRoland Dreier 1515d945e1dfSRoland Dreier return SUCCESS; 1516aef9ec39SRoland Dreier } 1517aef9ec39SRoland Dreier 1518aef9ec39SRoland Dreier static int srp_reset_host(struct scsi_cmnd *scmnd) 1519aef9ec39SRoland Dreier { 1520aef9ec39SRoland Dreier struct srp_target_port *target = host_to_target(scmnd->device->host); 1521aef9ec39SRoland Dreier int ret = FAILED; 1522aef9ec39SRoland Dreier 15237aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n"); 1524aef9ec39SRoland Dreier 1525aef9ec39SRoland Dreier if (!srp_reconnect_target(target)) 1526aef9ec39SRoland Dreier ret = SUCCESS; 1527aef9ec39SRoland Dreier 1528aef9ec39SRoland Dreier return ret; 1529aef9ec39SRoland Dreier } 1530aef9ec39SRoland Dreier 1531ee959b00STony Jones static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr, 1532ee959b00STony Jones char *buf) 15336ecb0c84SRoland Dreier { 1534ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15356ecb0c84SRoland Dreier 15366ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 15376ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 15386ecb0c84SRoland Dreier return -ENODEV; 15396ecb0c84SRoland Dreier 15406ecb0c84SRoland Dreier return sprintf(buf, "0x%016llx\n", 15416ecb0c84SRoland Dreier (unsigned long long) be64_to_cpu(target->id_ext)); 15426ecb0c84SRoland Dreier } 15436ecb0c84SRoland Dreier 1544ee959b00STony Jones static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr, 1545ee959b00STony Jones char *buf) 15466ecb0c84SRoland Dreier { 1547ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15486ecb0c84SRoland Dreier 15496ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 15506ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 15516ecb0c84SRoland Dreier return -ENODEV; 15526ecb0c84SRoland Dreier 15536ecb0c84SRoland Dreier return sprintf(buf, "0x%016llx\n", 15546ecb0c84SRoland Dreier (unsigned long long) be64_to_cpu(target->ioc_guid)); 15556ecb0c84SRoland Dreier } 15566ecb0c84SRoland Dreier 1557ee959b00STony Jones static ssize_t show_service_id(struct device *dev, 1558ee959b00STony Jones struct device_attribute *attr, char *buf) 15596ecb0c84SRoland Dreier { 1560ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15616ecb0c84SRoland Dreier 15626ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 15636ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 15646ecb0c84SRoland Dreier return -ENODEV; 15656ecb0c84SRoland Dreier 15666ecb0c84SRoland Dreier return sprintf(buf, "0x%016llx\n", 15676ecb0c84SRoland Dreier (unsigned long long) be64_to_cpu(target->service_id)); 15686ecb0c84SRoland Dreier } 15696ecb0c84SRoland Dreier 1570ee959b00STony Jones static ssize_t show_pkey(struct device *dev, struct device_attribute *attr, 1571ee959b00STony Jones char *buf) 15726ecb0c84SRoland Dreier { 1573ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15746ecb0c84SRoland Dreier 15756ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 15766ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 15776ecb0c84SRoland Dreier return -ENODEV; 15786ecb0c84SRoland Dreier 15796ecb0c84SRoland Dreier return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey)); 15806ecb0c84SRoland Dreier } 15816ecb0c84SRoland Dreier 1582ee959b00STony Jones static ssize_t show_dgid(struct device *dev, struct device_attribute *attr, 1583ee959b00STony Jones char *buf) 15846ecb0c84SRoland Dreier { 1585ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15866ecb0c84SRoland Dreier 15876ecb0c84SRoland Dreier if (target->state == SRP_TARGET_DEAD || 15886ecb0c84SRoland Dreier target->state == SRP_TARGET_REMOVED) 15896ecb0c84SRoland Dreier return -ENODEV; 15906ecb0c84SRoland Dreier 15915b095d98SHarvey Harrison return sprintf(buf, "%pI6\n", target->path.dgid.raw); 15926ecb0c84SRoland Dreier } 15936ecb0c84SRoland Dreier 1594ee959b00STony Jones static ssize_t show_orig_dgid(struct device *dev, 1595ee959b00STony Jones struct device_attribute *attr, char *buf) 15963633b3d0SIshai Rabinovitz { 1597ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 15983633b3d0SIshai Rabinovitz 15993633b3d0SIshai Rabinovitz if (target->state == SRP_TARGET_DEAD || 16003633b3d0SIshai Rabinovitz target->state == SRP_TARGET_REMOVED) 16013633b3d0SIshai Rabinovitz return -ENODEV; 16023633b3d0SIshai Rabinovitz 16035b095d98SHarvey Harrison return sprintf(buf, "%pI6\n", target->orig_dgid); 16043633b3d0SIshai Rabinovitz } 16053633b3d0SIshai Rabinovitz 160689de7486SBart Van Assche static ssize_t show_req_lim(struct device *dev, 160789de7486SBart Van Assche struct device_attribute *attr, char *buf) 160889de7486SBart Van Assche { 160989de7486SBart Van Assche struct srp_target_port *target = host_to_target(class_to_shost(dev)); 161089de7486SBart Van Assche 161189de7486SBart Van Assche if (target->state == SRP_TARGET_DEAD || 161289de7486SBart Van Assche target->state == SRP_TARGET_REMOVED) 161389de7486SBart Van Assche return -ENODEV; 161489de7486SBart Van Assche 161589de7486SBart Van Assche return sprintf(buf, "%d\n", target->req_lim); 161689de7486SBart Van Assche } 161789de7486SBart Van Assche 1618ee959b00STony Jones static ssize_t show_zero_req_lim(struct device *dev, 1619ee959b00STony Jones struct device_attribute *attr, char *buf) 16206bfa24faSRoland Dreier { 1621ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 16226bfa24faSRoland Dreier 16236bfa24faSRoland Dreier if (target->state == SRP_TARGET_DEAD || 16246bfa24faSRoland Dreier target->state == SRP_TARGET_REMOVED) 16256bfa24faSRoland Dreier return -ENODEV; 16266bfa24faSRoland Dreier 16276bfa24faSRoland Dreier return sprintf(buf, "%d\n", target->zero_req_lim); 16286bfa24faSRoland Dreier } 16296bfa24faSRoland Dreier 1630ee959b00STony Jones static ssize_t show_local_ib_port(struct device *dev, 1631ee959b00STony Jones struct device_attribute *attr, char *buf) 1632ded7f1a1SIshai Rabinovitz { 1633ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 1634ded7f1a1SIshai Rabinovitz 1635ded7f1a1SIshai Rabinovitz return sprintf(buf, "%d\n", target->srp_host->port); 1636ded7f1a1SIshai Rabinovitz } 1637ded7f1a1SIshai Rabinovitz 1638ee959b00STony Jones static ssize_t show_local_ib_device(struct device *dev, 1639ee959b00STony Jones struct device_attribute *attr, char *buf) 1640ded7f1a1SIshai Rabinovitz { 1641ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 1642ded7f1a1SIshai Rabinovitz 164305321937SGreg Kroah-Hartman return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name); 1644ded7f1a1SIshai Rabinovitz } 1645ded7f1a1SIshai Rabinovitz 1646ee959b00STony Jones static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL); 1647ee959b00STony Jones static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL); 1648ee959b00STony Jones static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL); 1649ee959b00STony Jones static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); 1650ee959b00STony Jones static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL); 1651ee959b00STony Jones static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL); 165289de7486SBart Van Assche static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL); 1653ee959b00STony Jones static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL); 1654ee959b00STony Jones static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL); 1655ee959b00STony Jones static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL); 16566ecb0c84SRoland Dreier 1657ee959b00STony Jones static struct device_attribute *srp_host_attrs[] = { 1658ee959b00STony Jones &dev_attr_id_ext, 1659ee959b00STony Jones &dev_attr_ioc_guid, 1660ee959b00STony Jones &dev_attr_service_id, 1661ee959b00STony Jones &dev_attr_pkey, 1662ee959b00STony Jones &dev_attr_dgid, 1663ee959b00STony Jones &dev_attr_orig_dgid, 166489de7486SBart Van Assche &dev_attr_req_lim, 1665ee959b00STony Jones &dev_attr_zero_req_lim, 1666ee959b00STony Jones &dev_attr_local_ib_port, 1667ee959b00STony Jones &dev_attr_local_ib_device, 16686ecb0c84SRoland Dreier NULL 16696ecb0c84SRoland Dreier }; 16706ecb0c84SRoland Dreier 1671aef9ec39SRoland Dreier static struct scsi_host_template srp_template = { 1672aef9ec39SRoland Dreier .module = THIS_MODULE, 1673b7f008fdSRoland Dreier .name = "InfiniBand SRP initiator", 1674b7f008fdSRoland Dreier .proc_name = DRV_NAME, 1675aef9ec39SRoland Dreier .info = srp_target_info, 1676aef9ec39SRoland Dreier .queuecommand = srp_queuecommand, 1677aef9ec39SRoland Dreier .eh_abort_handler = srp_abort, 1678aef9ec39SRoland Dreier .eh_device_reset_handler = srp_reset_device, 1679aef9ec39SRoland Dreier .eh_host_reset_handler = srp_reset_host, 1680dd5e6e38SBart Van Assche .can_queue = SRP_CMD_SQ_SIZE, 1681aef9ec39SRoland Dreier .this_id = -1, 1682dd5e6e38SBart Van Assche .cmd_per_lun = SRP_CMD_SQ_SIZE, 16836ecb0c84SRoland Dreier .use_clustering = ENABLE_CLUSTERING, 16846ecb0c84SRoland Dreier .shost_attrs = srp_host_attrs 1685aef9ec39SRoland Dreier }; 1686aef9ec39SRoland Dreier 1687aef9ec39SRoland Dreier static int srp_add_target(struct srp_host *host, struct srp_target_port *target) 1688aef9ec39SRoland Dreier { 16893236822bSFUJITA Tomonori struct srp_rport_identifiers ids; 16903236822bSFUJITA Tomonori struct srp_rport *rport; 16913236822bSFUJITA Tomonori 1692aef9ec39SRoland Dreier sprintf(target->target_name, "SRP.T10:%016llX", 1693aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->id_ext)); 1694aef9ec39SRoland Dreier 169505321937SGreg Kroah-Hartman if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device)) 1696aef9ec39SRoland Dreier return -ENODEV; 1697aef9ec39SRoland Dreier 16983236822bSFUJITA Tomonori memcpy(ids.port_id, &target->id_ext, 8); 16993236822bSFUJITA Tomonori memcpy(ids.port_id + 8, &target->ioc_guid, 8); 1700aebd5e47SFUJITA Tomonori ids.roles = SRP_RPORT_ROLE_TARGET; 17013236822bSFUJITA Tomonori rport = srp_rport_add(target->scsi_host, &ids); 17023236822bSFUJITA Tomonori if (IS_ERR(rport)) { 17033236822bSFUJITA Tomonori scsi_remove_host(target->scsi_host); 17043236822bSFUJITA Tomonori return PTR_ERR(rport); 17053236822bSFUJITA Tomonori } 17063236822bSFUJITA Tomonori 1707b3589fd4SMatthew Wilcox spin_lock(&host->target_lock); 1708aef9ec39SRoland Dreier list_add_tail(&target->list, &host->target_list); 1709b3589fd4SMatthew Wilcox spin_unlock(&host->target_lock); 1710aef9ec39SRoland Dreier 1711aef9ec39SRoland Dreier target->state = SRP_TARGET_LIVE; 1712aef9ec39SRoland Dreier 1713aef9ec39SRoland Dreier scsi_scan_target(&target->scsi_host->shost_gendev, 17141962a4a1SMatthew Wilcox 0, target->scsi_id, SCAN_WILD_CARD, 0); 1715aef9ec39SRoland Dreier 1716aef9ec39SRoland Dreier return 0; 1717aef9ec39SRoland Dreier } 1718aef9ec39SRoland Dreier 1719ee959b00STony Jones static void srp_release_dev(struct device *dev) 1720aef9ec39SRoland Dreier { 1721aef9ec39SRoland Dreier struct srp_host *host = 1722ee959b00STony Jones container_of(dev, struct srp_host, dev); 1723aef9ec39SRoland Dreier 1724aef9ec39SRoland Dreier complete(&host->released); 1725aef9ec39SRoland Dreier } 1726aef9ec39SRoland Dreier 1727aef9ec39SRoland Dreier static struct class srp_class = { 1728aef9ec39SRoland Dreier .name = "infiniband_srp", 1729ee959b00STony Jones .dev_release = srp_release_dev 1730aef9ec39SRoland Dreier }; 1731aef9ec39SRoland Dreier 1732aef9ec39SRoland Dreier /* 1733aef9ec39SRoland Dreier * Target ports are added by writing 1734aef9ec39SRoland Dreier * 1735aef9ec39SRoland Dreier * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>, 1736aef9ec39SRoland Dreier * pkey=<P_Key>,service_id=<service ID> 1737aef9ec39SRoland Dreier * 1738aef9ec39SRoland Dreier * to the add_target sysfs attribute. 1739aef9ec39SRoland Dreier */ 1740aef9ec39SRoland Dreier enum { 1741aef9ec39SRoland Dreier SRP_OPT_ERR = 0, 1742aef9ec39SRoland Dreier SRP_OPT_ID_EXT = 1 << 0, 1743aef9ec39SRoland Dreier SRP_OPT_IOC_GUID = 1 << 1, 1744aef9ec39SRoland Dreier SRP_OPT_DGID = 1 << 2, 1745aef9ec39SRoland Dreier SRP_OPT_PKEY = 1 << 3, 1746aef9ec39SRoland Dreier SRP_OPT_SERVICE_ID = 1 << 4, 1747aef9ec39SRoland Dreier SRP_OPT_MAX_SECT = 1 << 5, 174852fb2b50SVu Pham SRP_OPT_MAX_CMD_PER_LUN = 1 << 6, 17490c0450dbSRamachandra K SRP_OPT_IO_CLASS = 1 << 7, 175001cb9bcbSIshai Rabinovitz SRP_OPT_INITIATOR_EXT = 1 << 8, 1751aef9ec39SRoland Dreier SRP_OPT_ALL = (SRP_OPT_ID_EXT | 1752aef9ec39SRoland Dreier SRP_OPT_IOC_GUID | 1753aef9ec39SRoland Dreier SRP_OPT_DGID | 1754aef9ec39SRoland Dreier SRP_OPT_PKEY | 1755aef9ec39SRoland Dreier SRP_OPT_SERVICE_ID), 1756aef9ec39SRoland Dreier }; 1757aef9ec39SRoland Dreier 1758a447c093SSteven Whitehouse static const match_table_t srp_opt_tokens = { 1759aef9ec39SRoland Dreier { SRP_OPT_ID_EXT, "id_ext=%s" }, 1760aef9ec39SRoland Dreier { SRP_OPT_IOC_GUID, "ioc_guid=%s" }, 1761aef9ec39SRoland Dreier { SRP_OPT_DGID, "dgid=%s" }, 1762aef9ec39SRoland Dreier { SRP_OPT_PKEY, "pkey=%x" }, 1763aef9ec39SRoland Dreier { SRP_OPT_SERVICE_ID, "service_id=%s" }, 1764aef9ec39SRoland Dreier { SRP_OPT_MAX_SECT, "max_sect=%d" }, 176552fb2b50SVu Pham { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" }, 17660c0450dbSRamachandra K { SRP_OPT_IO_CLASS, "io_class=%x" }, 176701cb9bcbSIshai Rabinovitz { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" }, 1768aef9ec39SRoland Dreier { SRP_OPT_ERR, NULL } 1769aef9ec39SRoland Dreier }; 1770aef9ec39SRoland Dreier 1771aef9ec39SRoland Dreier static int srp_parse_options(const char *buf, struct srp_target_port *target) 1772aef9ec39SRoland Dreier { 1773aef9ec39SRoland Dreier char *options, *sep_opt; 1774aef9ec39SRoland Dreier char *p; 1775aef9ec39SRoland Dreier char dgid[3]; 1776aef9ec39SRoland Dreier substring_t args[MAX_OPT_ARGS]; 1777aef9ec39SRoland Dreier int opt_mask = 0; 1778aef9ec39SRoland Dreier int token; 1779aef9ec39SRoland Dreier int ret = -EINVAL; 1780aef9ec39SRoland Dreier int i; 1781aef9ec39SRoland Dreier 1782aef9ec39SRoland Dreier options = kstrdup(buf, GFP_KERNEL); 1783aef9ec39SRoland Dreier if (!options) 1784aef9ec39SRoland Dreier return -ENOMEM; 1785aef9ec39SRoland Dreier 1786aef9ec39SRoland Dreier sep_opt = options; 1787aef9ec39SRoland Dreier while ((p = strsep(&sep_opt, ",")) != NULL) { 1788aef9ec39SRoland Dreier if (!*p) 1789aef9ec39SRoland Dreier continue; 1790aef9ec39SRoland Dreier 1791aef9ec39SRoland Dreier token = match_token(p, srp_opt_tokens, args); 1792aef9ec39SRoland Dreier opt_mask |= token; 1793aef9ec39SRoland Dreier 1794aef9ec39SRoland Dreier switch (token) { 1795aef9ec39SRoland Dreier case SRP_OPT_ID_EXT: 1796aef9ec39SRoland Dreier p = match_strdup(args); 1797a20f3a6dSIshai Rabinovitz if (!p) { 1798a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1799a20f3a6dSIshai Rabinovitz goto out; 1800a20f3a6dSIshai Rabinovitz } 1801aef9ec39SRoland Dreier target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); 1802aef9ec39SRoland Dreier kfree(p); 1803aef9ec39SRoland Dreier break; 1804aef9ec39SRoland Dreier 1805aef9ec39SRoland Dreier case SRP_OPT_IOC_GUID: 1806aef9ec39SRoland Dreier p = match_strdup(args); 1807a20f3a6dSIshai Rabinovitz if (!p) { 1808a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1809a20f3a6dSIshai Rabinovitz goto out; 1810a20f3a6dSIshai Rabinovitz } 1811aef9ec39SRoland Dreier target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16)); 1812aef9ec39SRoland Dreier kfree(p); 1813aef9ec39SRoland Dreier break; 1814aef9ec39SRoland Dreier 1815aef9ec39SRoland Dreier case SRP_OPT_DGID: 1816aef9ec39SRoland Dreier p = match_strdup(args); 1817a20f3a6dSIshai Rabinovitz if (!p) { 1818a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1819a20f3a6dSIshai Rabinovitz goto out; 1820a20f3a6dSIshai Rabinovitz } 1821aef9ec39SRoland Dreier if (strlen(p) != 32) { 1822aef9ec39SRoland Dreier printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p); 1823ce1823f0SRoland Dreier kfree(p); 1824aef9ec39SRoland Dreier goto out; 1825aef9ec39SRoland Dreier } 1826aef9ec39SRoland Dreier 1827aef9ec39SRoland Dreier for (i = 0; i < 16; ++i) { 1828aef9ec39SRoland Dreier strlcpy(dgid, p + i * 2, 3); 1829aef9ec39SRoland Dreier target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16); 1830aef9ec39SRoland Dreier } 1831bf17c1c7SRoland Dreier kfree(p); 18323633b3d0SIshai Rabinovitz memcpy(target->orig_dgid, target->path.dgid.raw, 16); 1833aef9ec39SRoland Dreier break; 1834aef9ec39SRoland Dreier 1835aef9ec39SRoland Dreier case SRP_OPT_PKEY: 1836aef9ec39SRoland Dreier if (match_hex(args, &token)) { 1837aef9ec39SRoland Dreier printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p); 1838aef9ec39SRoland Dreier goto out; 1839aef9ec39SRoland Dreier } 1840aef9ec39SRoland Dreier target->path.pkey = cpu_to_be16(token); 1841aef9ec39SRoland Dreier break; 1842aef9ec39SRoland Dreier 1843aef9ec39SRoland Dreier case SRP_OPT_SERVICE_ID: 1844aef9ec39SRoland Dreier p = match_strdup(args); 1845a20f3a6dSIshai Rabinovitz if (!p) { 1846a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1847a20f3a6dSIshai Rabinovitz goto out; 1848a20f3a6dSIshai Rabinovitz } 1849aef9ec39SRoland Dreier target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16)); 1850247e020eSSean Hefty target->path.service_id = target->service_id; 1851aef9ec39SRoland Dreier kfree(p); 1852aef9ec39SRoland Dreier break; 1853aef9ec39SRoland Dreier 1854aef9ec39SRoland Dreier case SRP_OPT_MAX_SECT: 1855aef9ec39SRoland Dreier if (match_int(args, &token)) { 1856aef9ec39SRoland Dreier printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p); 1857aef9ec39SRoland Dreier goto out; 1858aef9ec39SRoland Dreier } 1859aef9ec39SRoland Dreier target->scsi_host->max_sectors = token; 1860aef9ec39SRoland Dreier break; 1861aef9ec39SRoland Dreier 186252fb2b50SVu Pham case SRP_OPT_MAX_CMD_PER_LUN: 186352fb2b50SVu Pham if (match_int(args, &token)) { 186452fb2b50SVu Pham printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p); 186552fb2b50SVu Pham goto out; 186652fb2b50SVu Pham } 1867dd5e6e38SBart Van Assche target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE); 186852fb2b50SVu Pham break; 186952fb2b50SVu Pham 18700c0450dbSRamachandra K case SRP_OPT_IO_CLASS: 18710c0450dbSRamachandra K if (match_hex(args, &token)) { 18720c0450dbSRamachandra K printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p); 18730c0450dbSRamachandra K goto out; 18740c0450dbSRamachandra K } 18750c0450dbSRamachandra K if (token != SRP_REV10_IB_IO_CLASS && 18760c0450dbSRamachandra K token != SRP_REV16A_IB_IO_CLASS) { 18770c0450dbSRamachandra K printk(KERN_WARNING PFX "unknown IO class parameter value" 18780c0450dbSRamachandra K " %x specified (use %x or %x).\n", 18790c0450dbSRamachandra K token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS); 18800c0450dbSRamachandra K goto out; 18810c0450dbSRamachandra K } 18820c0450dbSRamachandra K target->io_class = token; 18830c0450dbSRamachandra K break; 18840c0450dbSRamachandra K 188501cb9bcbSIshai Rabinovitz case SRP_OPT_INITIATOR_EXT: 188601cb9bcbSIshai Rabinovitz p = match_strdup(args); 1887a20f3a6dSIshai Rabinovitz if (!p) { 1888a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 1889a20f3a6dSIshai Rabinovitz goto out; 1890a20f3a6dSIshai Rabinovitz } 189101cb9bcbSIshai Rabinovitz target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); 189201cb9bcbSIshai Rabinovitz kfree(p); 189301cb9bcbSIshai Rabinovitz break; 189401cb9bcbSIshai Rabinovitz 1895aef9ec39SRoland Dreier default: 1896aef9ec39SRoland Dreier printk(KERN_WARNING PFX "unknown parameter or missing value " 1897aef9ec39SRoland Dreier "'%s' in target creation request\n", p); 1898aef9ec39SRoland Dreier goto out; 1899aef9ec39SRoland Dreier } 1900aef9ec39SRoland Dreier } 1901aef9ec39SRoland Dreier 1902aef9ec39SRoland Dreier if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL) 1903aef9ec39SRoland Dreier ret = 0; 1904aef9ec39SRoland Dreier else 1905aef9ec39SRoland Dreier for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i) 1906aef9ec39SRoland Dreier if ((srp_opt_tokens[i].token & SRP_OPT_ALL) && 1907aef9ec39SRoland Dreier !(srp_opt_tokens[i].token & opt_mask)) 1908aef9ec39SRoland Dreier printk(KERN_WARNING PFX "target creation request is " 1909aef9ec39SRoland Dreier "missing parameter '%s'\n", 1910aef9ec39SRoland Dreier srp_opt_tokens[i].pattern); 1911aef9ec39SRoland Dreier 1912aef9ec39SRoland Dreier out: 1913aef9ec39SRoland Dreier kfree(options); 1914aef9ec39SRoland Dreier return ret; 1915aef9ec39SRoland Dreier } 1916aef9ec39SRoland Dreier 1917ee959b00STony Jones static ssize_t srp_create_target(struct device *dev, 1918ee959b00STony Jones struct device_attribute *attr, 1919aef9ec39SRoland Dreier const char *buf, size_t count) 1920aef9ec39SRoland Dreier { 1921aef9ec39SRoland Dreier struct srp_host *host = 1922ee959b00STony Jones container_of(dev, struct srp_host, dev); 1923aef9ec39SRoland Dreier struct Scsi_Host *target_host; 1924aef9ec39SRoland Dreier struct srp_target_port *target; 1925aef9ec39SRoland Dreier int ret; 1926aef9ec39SRoland Dreier int i; 1927aef9ec39SRoland Dreier 1928aef9ec39SRoland Dreier target_host = scsi_host_alloc(&srp_template, 1929aef9ec39SRoland Dreier sizeof (struct srp_target_port)); 1930aef9ec39SRoland Dreier if (!target_host) 1931aef9ec39SRoland Dreier return -ENOMEM; 1932aef9ec39SRoland Dreier 19333236822bSFUJITA Tomonori target_host->transportt = ib_srp_transport_template; 19345f068992SRoland Dreier target_host->max_lun = SRP_MAX_LUN; 19353c8edf0eSArne Redlich target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb; 19365f068992SRoland Dreier 1937aef9ec39SRoland Dreier target = host_to_target(target_host); 1938aef9ec39SRoland Dreier 19390c0450dbSRamachandra K target->io_class = SRP_REV16A_IB_IO_CLASS; 1940aef9ec39SRoland Dreier target->scsi_host = target_host; 1941aef9ec39SRoland Dreier target->srp_host = host; 19429af76271SDavid Dillow target->lkey = host->srp_dev->mr->lkey; 19439af76271SDavid Dillow target->rkey = host->srp_dev->mr->rkey; 1944aef9ec39SRoland Dreier 1945e9684678SBart Van Assche spin_lock_init(&target->lock); 1946dcb4cb85SBart Van Assche INIT_LIST_HEAD(&target->free_tx); 1947d945e1dfSRoland Dreier INIT_LIST_HEAD(&target->free_reqs); 1948dd5e6e38SBart Van Assche for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { 1949d945e1dfSRoland Dreier target->req_ring[i].index = i; 1950d945e1dfSRoland Dreier list_add_tail(&target->req_ring[i].list, &target->free_reqs); 1951d945e1dfSRoland Dreier } 1952aef9ec39SRoland Dreier 1953aef9ec39SRoland Dreier ret = srp_parse_options(buf, target); 1954aef9ec39SRoland Dreier if (ret) 1955aef9ec39SRoland Dreier goto err; 1956aef9ec39SRoland Dreier 1957969a60f9SRoland Dreier ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid); 1958aef9ec39SRoland Dreier 19597aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, PFX 19607aa54bd7SDavid Dillow "new target: id_ext %016llx ioc_guid %016llx pkey %04x " 19615b095d98SHarvey Harrison "service_id %016llx dgid %pI6\n", 1962aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->id_ext), 1963aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->ioc_guid), 1964aef9ec39SRoland Dreier be16_to_cpu(target->path.pkey), 1965aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->service_id), 19668867cd7cSHarvey Harrison target->path.dgid.raw); 1967aef9ec39SRoland Dreier 1968aef9ec39SRoland Dreier ret = srp_create_target_ib(target); 1969aef9ec39SRoland Dreier if (ret) 1970aef9ec39SRoland Dreier goto err; 1971aef9ec39SRoland Dreier 19729fe4bcf4SDavid Dillow ret = srp_new_cm_id(target); 19739fe4bcf4SDavid Dillow if (ret) 1974aef9ec39SRoland Dreier goto err_free; 1975aef9ec39SRoland Dreier 19761033ff67SIshai Rabinovitz target->qp_in_error = 0; 1977aef9ec39SRoland Dreier ret = srp_connect_target(target); 1978aef9ec39SRoland Dreier if (ret) { 19797aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 19807aa54bd7SDavid Dillow PFX "Connection failed\n"); 1981aef9ec39SRoland Dreier goto err_cm_id; 1982aef9ec39SRoland Dreier } 1983aef9ec39SRoland Dreier 1984aef9ec39SRoland Dreier ret = srp_add_target(host, target); 1985aef9ec39SRoland Dreier if (ret) 1986aef9ec39SRoland Dreier goto err_disconnect; 1987aef9ec39SRoland Dreier 1988aef9ec39SRoland Dreier return count; 1989aef9ec39SRoland Dreier 1990aef9ec39SRoland Dreier err_disconnect: 1991aef9ec39SRoland Dreier srp_disconnect_target(target); 1992aef9ec39SRoland Dreier 1993aef9ec39SRoland Dreier err_cm_id: 1994aef9ec39SRoland Dreier ib_destroy_cm_id(target->cm_id); 1995aef9ec39SRoland Dreier 1996aef9ec39SRoland Dreier err_free: 1997aef9ec39SRoland Dreier srp_free_target_ib(target); 1998aef9ec39SRoland Dreier 1999aef9ec39SRoland Dreier err: 2000aef9ec39SRoland Dreier scsi_host_put(target_host); 2001aef9ec39SRoland Dreier 2002aef9ec39SRoland Dreier return ret; 2003aef9ec39SRoland Dreier } 2004aef9ec39SRoland Dreier 2005ee959b00STony Jones static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target); 2006aef9ec39SRoland Dreier 2007ee959b00STony Jones static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, 2008ee959b00STony Jones char *buf) 2009aef9ec39SRoland Dreier { 2010ee959b00STony Jones struct srp_host *host = container_of(dev, struct srp_host, dev); 2011aef9ec39SRoland Dreier 201205321937SGreg Kroah-Hartman return sprintf(buf, "%s\n", host->srp_dev->dev->name); 2013aef9ec39SRoland Dreier } 2014aef9ec39SRoland Dreier 2015ee959b00STony Jones static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); 2016aef9ec39SRoland Dreier 2017ee959b00STony Jones static ssize_t show_port(struct device *dev, struct device_attribute *attr, 2018ee959b00STony Jones char *buf) 2019aef9ec39SRoland Dreier { 2020ee959b00STony Jones struct srp_host *host = container_of(dev, struct srp_host, dev); 2021aef9ec39SRoland Dreier 2022aef9ec39SRoland Dreier return sprintf(buf, "%d\n", host->port); 2023aef9ec39SRoland Dreier } 2024aef9ec39SRoland Dreier 2025ee959b00STony Jones static DEVICE_ATTR(port, S_IRUGO, show_port, NULL); 2026aef9ec39SRoland Dreier 2027f5358a17SRoland Dreier static struct srp_host *srp_add_port(struct srp_device *device, u8 port) 2028aef9ec39SRoland Dreier { 2029aef9ec39SRoland Dreier struct srp_host *host; 2030aef9ec39SRoland Dreier 2031aef9ec39SRoland Dreier host = kzalloc(sizeof *host, GFP_KERNEL); 2032aef9ec39SRoland Dreier if (!host) 2033aef9ec39SRoland Dreier return NULL; 2034aef9ec39SRoland Dreier 2035aef9ec39SRoland Dreier INIT_LIST_HEAD(&host->target_list); 2036b3589fd4SMatthew Wilcox spin_lock_init(&host->target_lock); 2037aef9ec39SRoland Dreier init_completion(&host->released); 203805321937SGreg Kroah-Hartman host->srp_dev = device; 2039aef9ec39SRoland Dreier host->port = port; 2040aef9ec39SRoland Dreier 2041ee959b00STony Jones host->dev.class = &srp_class; 2042ee959b00STony Jones host->dev.parent = device->dev->dma_device; 2043d927e38cSKay Sievers dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port); 2044aef9ec39SRoland Dreier 2045ee959b00STony Jones if (device_register(&host->dev)) 2046f5358a17SRoland Dreier goto free_host; 2047ee959b00STony Jones if (device_create_file(&host->dev, &dev_attr_add_target)) 2048aef9ec39SRoland Dreier goto err_class; 2049ee959b00STony Jones if (device_create_file(&host->dev, &dev_attr_ibdev)) 2050aef9ec39SRoland Dreier goto err_class; 2051ee959b00STony Jones if (device_create_file(&host->dev, &dev_attr_port)) 2052aef9ec39SRoland Dreier goto err_class; 2053aef9ec39SRoland Dreier 2054aef9ec39SRoland Dreier return host; 2055aef9ec39SRoland Dreier 2056aef9ec39SRoland Dreier err_class: 2057ee959b00STony Jones device_unregister(&host->dev); 2058aef9ec39SRoland Dreier 2059f5358a17SRoland Dreier free_host: 2060aef9ec39SRoland Dreier kfree(host); 2061aef9ec39SRoland Dreier 2062aef9ec39SRoland Dreier return NULL; 2063aef9ec39SRoland Dreier } 2064aef9ec39SRoland Dreier 2065aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device) 2066aef9ec39SRoland Dreier { 2067f5358a17SRoland Dreier struct srp_device *srp_dev; 2068f5358a17SRoland Dreier struct ib_device_attr *dev_attr; 2069f5358a17SRoland Dreier struct ib_fmr_pool_param fmr_param; 2070aef9ec39SRoland Dreier struct srp_host *host; 2071aef9ec39SRoland Dreier int s, e, p; 2072aef9ec39SRoland Dreier 2073f5358a17SRoland Dreier dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL); 2074f5358a17SRoland Dreier if (!dev_attr) 2075cf311cd4SSean Hefty return; 2076aef9ec39SRoland Dreier 2077f5358a17SRoland Dreier if (ib_query_device(device, dev_attr)) { 2078f5358a17SRoland Dreier printk(KERN_WARNING PFX "Query device failed for %s\n", 2079f5358a17SRoland Dreier device->name); 2080f5358a17SRoland Dreier goto free_attr; 2081f5358a17SRoland Dreier } 2082f5358a17SRoland Dreier 2083f5358a17SRoland Dreier srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL); 2084f5358a17SRoland Dreier if (!srp_dev) 2085f5358a17SRoland Dreier goto free_attr; 2086f5358a17SRoland Dreier 2087f5358a17SRoland Dreier /* 2088f5358a17SRoland Dreier * Use the smallest page size supported by the HCA, down to a 2089f5358a17SRoland Dreier * minimum of 512 bytes (which is the smallest sector that a 2090f5358a17SRoland Dreier * SCSI command will ever carry). 2091f5358a17SRoland Dreier */ 2092f5358a17SRoland Dreier srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1); 2093f5358a17SRoland Dreier srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift; 2094bf628dc2SRoland Dreier srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1); 2095f5358a17SRoland Dreier 2096f5358a17SRoland Dreier INIT_LIST_HEAD(&srp_dev->dev_list); 2097f5358a17SRoland Dreier 2098f5358a17SRoland Dreier srp_dev->dev = device; 2099f5358a17SRoland Dreier srp_dev->pd = ib_alloc_pd(device); 2100f5358a17SRoland Dreier if (IS_ERR(srp_dev->pd)) 2101f5358a17SRoland Dreier goto free_dev; 2102f5358a17SRoland Dreier 2103f5358a17SRoland Dreier srp_dev->mr = ib_get_dma_mr(srp_dev->pd, 2104f5358a17SRoland Dreier IB_ACCESS_LOCAL_WRITE | 2105f5358a17SRoland Dreier IB_ACCESS_REMOTE_READ | 2106f5358a17SRoland Dreier IB_ACCESS_REMOTE_WRITE); 2107f5358a17SRoland Dreier if (IS_ERR(srp_dev->mr)) 2108f5358a17SRoland Dreier goto err_pd; 2109f5358a17SRoland Dreier 2110f5358a17SRoland Dreier memset(&fmr_param, 0, sizeof fmr_param); 2111f5358a17SRoland Dreier fmr_param.pool_size = SRP_FMR_POOL_SIZE; 2112f5358a17SRoland Dreier fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE; 2113f5358a17SRoland Dreier fmr_param.cache = 1; 2114f5358a17SRoland Dreier fmr_param.max_pages_per_fmr = SRP_FMR_SIZE; 2115f5358a17SRoland Dreier fmr_param.page_shift = srp_dev->fmr_page_shift; 2116f5358a17SRoland Dreier fmr_param.access = (IB_ACCESS_LOCAL_WRITE | 2117f5358a17SRoland Dreier IB_ACCESS_REMOTE_WRITE | 2118f5358a17SRoland Dreier IB_ACCESS_REMOTE_READ); 2119f5358a17SRoland Dreier 2120f5358a17SRoland Dreier srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param); 2121f5358a17SRoland Dreier if (IS_ERR(srp_dev->fmr_pool)) 2122f5358a17SRoland Dreier srp_dev->fmr_pool = NULL; 2123aef9ec39SRoland Dreier 212407ebafbaSTom Tucker if (device->node_type == RDMA_NODE_IB_SWITCH) { 2125aef9ec39SRoland Dreier s = 0; 2126aef9ec39SRoland Dreier e = 0; 2127aef9ec39SRoland Dreier } else { 2128aef9ec39SRoland Dreier s = 1; 2129aef9ec39SRoland Dreier e = device->phys_port_cnt; 2130aef9ec39SRoland Dreier } 2131aef9ec39SRoland Dreier 2132aef9ec39SRoland Dreier for (p = s; p <= e; ++p) { 2133f5358a17SRoland Dreier host = srp_add_port(srp_dev, p); 2134aef9ec39SRoland Dreier if (host) 2135f5358a17SRoland Dreier list_add_tail(&host->list, &srp_dev->dev_list); 2136aef9ec39SRoland Dreier } 2137aef9ec39SRoland Dreier 2138f5358a17SRoland Dreier ib_set_client_data(device, &srp_client, srp_dev); 2139f5358a17SRoland Dreier 2140f5358a17SRoland Dreier goto free_attr; 2141f5358a17SRoland Dreier 2142f5358a17SRoland Dreier err_pd: 2143f5358a17SRoland Dreier ib_dealloc_pd(srp_dev->pd); 2144f5358a17SRoland Dreier 2145f5358a17SRoland Dreier free_dev: 2146f5358a17SRoland Dreier kfree(srp_dev); 2147f5358a17SRoland Dreier 2148f5358a17SRoland Dreier free_attr: 2149f5358a17SRoland Dreier kfree(dev_attr); 2150aef9ec39SRoland Dreier } 2151aef9ec39SRoland Dreier 2152aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device) 2153aef9ec39SRoland Dreier { 2154f5358a17SRoland Dreier struct srp_device *srp_dev; 2155aef9ec39SRoland Dreier struct srp_host *host, *tmp_host; 2156aef9ec39SRoland Dreier LIST_HEAD(target_list); 2157aef9ec39SRoland Dreier struct srp_target_port *target, *tmp_target; 2158aef9ec39SRoland Dreier 2159f5358a17SRoland Dreier srp_dev = ib_get_client_data(device, &srp_client); 2160aef9ec39SRoland Dreier 2161f5358a17SRoland Dreier list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) { 2162ee959b00STony Jones device_unregister(&host->dev); 2163aef9ec39SRoland Dreier /* 2164aef9ec39SRoland Dreier * Wait for the sysfs entry to go away, so that no new 2165aef9ec39SRoland Dreier * target ports can be created. 2166aef9ec39SRoland Dreier */ 2167aef9ec39SRoland Dreier wait_for_completion(&host->released); 2168aef9ec39SRoland Dreier 2169aef9ec39SRoland Dreier /* 2170aef9ec39SRoland Dreier * Mark all target ports as removed, so we stop queueing 2171aef9ec39SRoland Dreier * commands and don't try to reconnect. 2172aef9ec39SRoland Dreier */ 2173b3589fd4SMatthew Wilcox spin_lock(&host->target_lock); 2174549c5fc2SMatthew Wilcox list_for_each_entry(target, &host->target_list, list) { 2175e9684678SBart Van Assche spin_lock_irq(&target->lock); 2176aef9ec39SRoland Dreier target->state = SRP_TARGET_REMOVED; 2177e9684678SBart Van Assche spin_unlock_irq(&target->lock); 2178aef9ec39SRoland Dreier } 2179b3589fd4SMatthew Wilcox spin_unlock(&host->target_lock); 2180aef9ec39SRoland Dreier 2181aef9ec39SRoland Dreier /* 2182aef9ec39SRoland Dreier * Wait for any reconnection tasks that may have 2183aef9ec39SRoland Dreier * started before we marked our target ports as 2184aef9ec39SRoland Dreier * removed, and any target port removal tasks. 2185aef9ec39SRoland Dreier */ 2186f0626710STejun Heo flush_workqueue(ib_wq); 2187aef9ec39SRoland Dreier 2188aef9ec39SRoland Dreier list_for_each_entry_safe(target, tmp_target, 2189aef9ec39SRoland Dreier &host->target_list, list) { 2190b0e47c8bSDavid Dillow srp_remove_host(target->scsi_host); 2191ad696989SDave Dillow scsi_remove_host(target->scsi_host); 2192aef9ec39SRoland Dreier srp_disconnect_target(target); 2193aef9ec39SRoland Dreier ib_destroy_cm_id(target->cm_id); 2194aef9ec39SRoland Dreier srp_free_target_ib(target); 2195aef9ec39SRoland Dreier scsi_host_put(target->scsi_host); 2196aef9ec39SRoland Dreier } 2197aef9ec39SRoland Dreier 2198aef9ec39SRoland Dreier kfree(host); 2199aef9ec39SRoland Dreier } 2200aef9ec39SRoland Dreier 2201f5358a17SRoland Dreier if (srp_dev->fmr_pool) 2202f5358a17SRoland Dreier ib_destroy_fmr_pool(srp_dev->fmr_pool); 2203f5358a17SRoland Dreier ib_dereg_mr(srp_dev->mr); 2204f5358a17SRoland Dreier ib_dealloc_pd(srp_dev->pd); 2205f5358a17SRoland Dreier 2206f5358a17SRoland Dreier kfree(srp_dev); 2207aef9ec39SRoland Dreier } 2208aef9ec39SRoland Dreier 22093236822bSFUJITA Tomonori static struct srp_function_template ib_srp_transport_functions = { 22103236822bSFUJITA Tomonori }; 22113236822bSFUJITA Tomonori 2212aef9ec39SRoland Dreier static int __init srp_init_module(void) 2213aef9ec39SRoland Dreier { 2214aef9ec39SRoland Dreier int ret; 2215aef9ec39SRoland Dreier 2216dcb4cb85SBart Van Assche BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *)); 2217dd5e6e38SBart Van Assche 22181e89a194SDavid Dillow if (srp_sg_tablesize > 255) { 22191e89a194SDavid Dillow printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n"); 22201e89a194SDavid Dillow srp_sg_tablesize = 255; 22211e89a194SDavid Dillow } 22221e89a194SDavid Dillow 22233236822bSFUJITA Tomonori ib_srp_transport_template = 22243236822bSFUJITA Tomonori srp_attach_transport(&ib_srp_transport_functions); 22253236822bSFUJITA Tomonori if (!ib_srp_transport_template) 22263236822bSFUJITA Tomonori return -ENOMEM; 22273236822bSFUJITA Tomonori 222874b0a15bSVu Pham srp_template.sg_tablesize = srp_sg_tablesize; 222974b0a15bSVu Pham srp_max_iu_len = (sizeof (struct srp_cmd) + 223074b0a15bSVu Pham sizeof (struct srp_indirect_buf) + 223174b0a15bSVu Pham srp_sg_tablesize * 16); 223274b0a15bSVu Pham 2233aef9ec39SRoland Dreier ret = class_register(&srp_class); 2234aef9ec39SRoland Dreier if (ret) { 2235aef9ec39SRoland Dreier printk(KERN_ERR PFX "couldn't register class infiniband_srp\n"); 22363236822bSFUJITA Tomonori srp_release_transport(ib_srp_transport_template); 2237aef9ec39SRoland Dreier return ret; 2238aef9ec39SRoland Dreier } 2239aef9ec39SRoland Dreier 2240c1a0b23bSMichael S. Tsirkin ib_sa_register_client(&srp_sa_client); 2241c1a0b23bSMichael S. Tsirkin 2242aef9ec39SRoland Dreier ret = ib_register_client(&srp_client); 2243aef9ec39SRoland Dreier if (ret) { 2244aef9ec39SRoland Dreier printk(KERN_ERR PFX "couldn't register IB client\n"); 22453236822bSFUJITA Tomonori srp_release_transport(ib_srp_transport_template); 2246c1a0b23bSMichael S. Tsirkin ib_sa_unregister_client(&srp_sa_client); 2247aef9ec39SRoland Dreier class_unregister(&srp_class); 2248aef9ec39SRoland Dreier return ret; 2249aef9ec39SRoland Dreier } 2250aef9ec39SRoland Dreier 2251aef9ec39SRoland Dreier return 0; 2252aef9ec39SRoland Dreier } 2253aef9ec39SRoland Dreier 2254aef9ec39SRoland Dreier static void __exit srp_cleanup_module(void) 2255aef9ec39SRoland Dreier { 2256aef9ec39SRoland Dreier ib_unregister_client(&srp_client); 2257c1a0b23bSMichael S. Tsirkin ib_sa_unregister_client(&srp_sa_client); 2258aef9ec39SRoland Dreier class_unregister(&srp_class); 22593236822bSFUJITA Tomonori srp_release_transport(ib_srp_transport_template); 2260aef9ec39SRoland Dreier } 2261aef9ec39SRoland Dreier 2262aef9ec39SRoland Dreier module_init(srp_init_module); 2263aef9ec39SRoland Dreier module_exit(srp_cleanup_module); 2264