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 33e0bda7d8SBart Van Assche #define pr_fmt(fmt) PFX fmt 34e0bda7d8SBart Van Assche 35aef9ec39SRoland Dreier #include <linux/module.h> 36aef9ec39SRoland Dreier #include <linux/init.h> 37aef9ec39SRoland Dreier #include <linux/slab.h> 38aef9ec39SRoland Dreier #include <linux/err.h> 39aef9ec39SRoland Dreier #include <linux/string.h> 40aef9ec39SRoland Dreier #include <linux/parser.h> 41aef9ec39SRoland Dreier #include <linux/random.h> 42de25968cSTim Schmielau #include <linux/jiffies.h> 43aef9ec39SRoland Dreier 4460063497SArun Sharma #include <linux/atomic.h> 45aef9ec39SRoland Dreier 46aef9ec39SRoland Dreier #include <scsi/scsi.h> 47aef9ec39SRoland Dreier #include <scsi/scsi_device.h> 48aef9ec39SRoland Dreier #include <scsi/scsi_dbg.h> 4971444b97SJack Wang #include <scsi/scsi_tcq.h> 50aef9ec39SRoland Dreier #include <scsi/srp.h> 513236822bSFUJITA Tomonori #include <scsi/scsi_transport_srp.h> 52aef9ec39SRoland Dreier 53aef9ec39SRoland Dreier #include "ib_srp.h" 54aef9ec39SRoland Dreier 55aef9ec39SRoland Dreier #define DRV_NAME "ib_srp" 56aef9ec39SRoland Dreier #define PFX DRV_NAME ": " 57e8ca4135SVu Pham #define DRV_VERSION "1.0" 58e8ca4135SVu Pham #define DRV_RELDATE "July 1, 2013" 59aef9ec39SRoland Dreier 60aef9ec39SRoland Dreier MODULE_AUTHOR("Roland Dreier"); 61aef9ec39SRoland Dreier MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator " 62aef9ec39SRoland Dreier "v" DRV_VERSION " (" DRV_RELDATE ")"); 63aef9ec39SRoland Dreier MODULE_LICENSE("Dual BSD/GPL"); 64aef9ec39SRoland Dreier 6549248644SDavid Dillow static unsigned int srp_sg_tablesize; 6649248644SDavid Dillow static unsigned int cmd_sg_entries; 67c07d424dSDavid Dillow static unsigned int indirect_sg_entries; 68c07d424dSDavid Dillow static bool allow_ext_sg; 69aef9ec39SRoland Dreier static int topspin_workarounds = 1; 70aef9ec39SRoland Dreier 7149248644SDavid Dillow module_param(srp_sg_tablesize, uint, 0444); 7249248644SDavid Dillow MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries"); 7349248644SDavid Dillow 7449248644SDavid Dillow module_param(cmd_sg_entries, uint, 0444); 7549248644SDavid Dillow MODULE_PARM_DESC(cmd_sg_entries, 7649248644SDavid Dillow "Default number of gather/scatter entries in the SRP command (default is 12, max 255)"); 7749248644SDavid Dillow 78c07d424dSDavid Dillow module_param(indirect_sg_entries, uint, 0444); 79c07d424dSDavid Dillow MODULE_PARM_DESC(indirect_sg_entries, 80c07d424dSDavid Dillow "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")"); 81c07d424dSDavid Dillow 82c07d424dSDavid Dillow module_param(allow_ext_sg, bool, 0444); 83c07d424dSDavid Dillow MODULE_PARM_DESC(allow_ext_sg, 84c07d424dSDavid Dillow "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)"); 85c07d424dSDavid Dillow 86aef9ec39SRoland Dreier module_param(topspin_workarounds, int, 0444); 87aef9ec39SRoland Dreier MODULE_PARM_DESC(topspin_workarounds, 88aef9ec39SRoland Dreier "Enable workarounds for Topspin/Cisco SRP target bugs if != 0"); 89aef9ec39SRoland Dreier 90ed9b2264SBart Van Assche static struct kernel_param_ops srp_tmo_ops; 91ed9b2264SBart Van Assche 92a95cadb9SBart Van Assche static int srp_reconnect_delay = 10; 93a95cadb9SBart Van Assche module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay, 94a95cadb9SBart Van Assche S_IRUGO | S_IWUSR); 95a95cadb9SBart Van Assche MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts"); 96a95cadb9SBart Van Assche 97ed9b2264SBart Van Assche static int srp_fast_io_fail_tmo = 15; 98ed9b2264SBart Van Assche module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo, 99ed9b2264SBart Van Assche S_IRUGO | S_IWUSR); 100ed9b2264SBart Van Assche MODULE_PARM_DESC(fast_io_fail_tmo, 101ed9b2264SBart Van Assche "Number of seconds between the observation of a transport" 102ed9b2264SBart Van Assche " layer error and failing all I/O. \"off\" means that this" 103ed9b2264SBart Van Assche " functionality is disabled."); 104ed9b2264SBart Van Assche 105a95cadb9SBart Van Assche static int srp_dev_loss_tmo = 600; 106ed9b2264SBart Van Assche module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo, 107ed9b2264SBart Van Assche S_IRUGO | S_IWUSR); 108ed9b2264SBart Van Assche MODULE_PARM_DESC(dev_loss_tmo, 109ed9b2264SBart Van Assche "Maximum number of seconds that the SRP transport should" 110ed9b2264SBart Van Assche " insulate transport layer errors. After this time has been" 111ed9b2264SBart Van Assche " exceeded the SCSI host is removed. Should be" 112ed9b2264SBart Van Assche " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT) 113ed9b2264SBart Van Assche " if fast_io_fail_tmo has not been set. \"off\" means that" 114ed9b2264SBart Van Assche " this functionality is disabled."); 115ed9b2264SBart Van Assche 116aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device); 117aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device); 1189c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr); 1199c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr); 120aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event); 121aef9ec39SRoland Dreier 1223236822bSFUJITA Tomonori static struct scsi_transport_template *ib_srp_transport_template; 1233236822bSFUJITA Tomonori 124aef9ec39SRoland Dreier static struct ib_client srp_client = { 125aef9ec39SRoland Dreier .name = "srp", 126aef9ec39SRoland Dreier .add = srp_add_one, 127aef9ec39SRoland Dreier .remove = srp_remove_one 128aef9ec39SRoland Dreier }; 129aef9ec39SRoland Dreier 130c1a0b23bSMichael S. Tsirkin static struct ib_sa_client srp_sa_client; 131c1a0b23bSMichael S. Tsirkin 132ed9b2264SBart Van Assche static int srp_tmo_get(char *buffer, const struct kernel_param *kp) 133ed9b2264SBart Van Assche { 134ed9b2264SBart Van Assche int tmo = *(int *)kp->arg; 135ed9b2264SBart Van Assche 136ed9b2264SBart Van Assche if (tmo >= 0) 137ed9b2264SBart Van Assche return sprintf(buffer, "%d", tmo); 138ed9b2264SBart Van Assche else 139ed9b2264SBart Van Assche return sprintf(buffer, "off"); 140ed9b2264SBart Van Assche } 141ed9b2264SBart Van Assche 142ed9b2264SBart Van Assche static int srp_tmo_set(const char *val, const struct kernel_param *kp) 143ed9b2264SBart Van Assche { 144ed9b2264SBart Van Assche int tmo, res; 145ed9b2264SBart Van Assche 146ed9b2264SBart Van Assche if (strncmp(val, "off", 3) != 0) { 147ed9b2264SBart Van Assche res = kstrtoint(val, 0, &tmo); 148ed9b2264SBart Van Assche if (res) 149ed9b2264SBart Van Assche goto out; 150ed9b2264SBart Van Assche } else { 151ed9b2264SBart Van Assche tmo = -1; 152ed9b2264SBart Van Assche } 153a95cadb9SBart Van Assche if (kp->arg == &srp_reconnect_delay) 154a95cadb9SBart Van Assche res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo, 155a95cadb9SBart Van Assche srp_dev_loss_tmo); 156a95cadb9SBart Van Assche else if (kp->arg == &srp_fast_io_fail_tmo) 157a95cadb9SBart Van Assche res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo); 158ed9b2264SBart Van Assche else 159a95cadb9SBart Van Assche res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo, 160a95cadb9SBart Van Assche tmo); 161ed9b2264SBart Van Assche if (res) 162ed9b2264SBart Van Assche goto out; 163ed9b2264SBart Van Assche *(int *)kp->arg = tmo; 164ed9b2264SBart Van Assche 165ed9b2264SBart Van Assche out: 166ed9b2264SBart Van Assche return res; 167ed9b2264SBart Van Assche } 168ed9b2264SBart Van Assche 169ed9b2264SBart Van Assche static struct kernel_param_ops srp_tmo_ops = { 170ed9b2264SBart Van Assche .get = srp_tmo_get, 171ed9b2264SBart Van Assche .set = srp_tmo_set, 172ed9b2264SBart Van Assche }; 173ed9b2264SBart Van Assche 174aef9ec39SRoland Dreier static inline struct srp_target_port *host_to_target(struct Scsi_Host *host) 175aef9ec39SRoland Dreier { 176aef9ec39SRoland Dreier return (struct srp_target_port *) host->hostdata; 177aef9ec39SRoland Dreier } 178aef9ec39SRoland Dreier 179aef9ec39SRoland Dreier static const char *srp_target_info(struct Scsi_Host *host) 180aef9ec39SRoland Dreier { 181aef9ec39SRoland Dreier return host_to_target(host)->target_name; 182aef9ec39SRoland Dreier } 183aef9ec39SRoland Dreier 1845d7cbfd6SRoland Dreier static int srp_target_is_topspin(struct srp_target_port *target) 1855d7cbfd6SRoland Dreier { 1865d7cbfd6SRoland Dreier static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad }; 1873d1ff48dSRaghava Kondapalli static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d }; 1885d7cbfd6SRoland Dreier 1895d7cbfd6SRoland Dreier return topspin_workarounds && 1903d1ff48dSRaghava Kondapalli (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) || 1913d1ff48dSRaghava Kondapalli !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui)); 1925d7cbfd6SRoland Dreier } 1935d7cbfd6SRoland Dreier 194aef9ec39SRoland Dreier static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size, 195aef9ec39SRoland Dreier gfp_t gfp_mask, 196aef9ec39SRoland Dreier enum dma_data_direction direction) 197aef9ec39SRoland Dreier { 198aef9ec39SRoland Dreier struct srp_iu *iu; 199aef9ec39SRoland Dreier 200aef9ec39SRoland Dreier iu = kmalloc(sizeof *iu, gfp_mask); 201aef9ec39SRoland Dreier if (!iu) 202aef9ec39SRoland Dreier goto out; 203aef9ec39SRoland Dreier 204aef9ec39SRoland Dreier iu->buf = kzalloc(size, gfp_mask); 205aef9ec39SRoland Dreier if (!iu->buf) 206aef9ec39SRoland Dreier goto out_free_iu; 207aef9ec39SRoland Dreier 20805321937SGreg Kroah-Hartman iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size, 20905321937SGreg Kroah-Hartman direction); 21005321937SGreg Kroah-Hartman if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma)) 211aef9ec39SRoland Dreier goto out_free_buf; 212aef9ec39SRoland Dreier 213aef9ec39SRoland Dreier iu->size = size; 214aef9ec39SRoland Dreier iu->direction = direction; 215aef9ec39SRoland Dreier 216aef9ec39SRoland Dreier return iu; 217aef9ec39SRoland Dreier 218aef9ec39SRoland Dreier out_free_buf: 219aef9ec39SRoland Dreier kfree(iu->buf); 220aef9ec39SRoland Dreier out_free_iu: 221aef9ec39SRoland Dreier kfree(iu); 222aef9ec39SRoland Dreier out: 223aef9ec39SRoland Dreier return NULL; 224aef9ec39SRoland Dreier } 225aef9ec39SRoland Dreier 226aef9ec39SRoland Dreier static void srp_free_iu(struct srp_host *host, struct srp_iu *iu) 227aef9ec39SRoland Dreier { 228aef9ec39SRoland Dreier if (!iu) 229aef9ec39SRoland Dreier return; 230aef9ec39SRoland Dreier 23105321937SGreg Kroah-Hartman ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size, 23205321937SGreg Kroah-Hartman iu->direction); 233aef9ec39SRoland Dreier kfree(iu->buf); 234aef9ec39SRoland Dreier kfree(iu); 235aef9ec39SRoland Dreier } 236aef9ec39SRoland Dreier 237aef9ec39SRoland Dreier static void srp_qp_event(struct ib_event *event, void *context) 238aef9ec39SRoland Dreier { 239e0bda7d8SBart Van Assche pr_debug("QP event %d\n", event->event); 240aef9ec39SRoland Dreier } 241aef9ec39SRoland Dreier 242aef9ec39SRoland Dreier static int srp_init_qp(struct srp_target_port *target, 243aef9ec39SRoland Dreier struct ib_qp *qp) 244aef9ec39SRoland Dreier { 245aef9ec39SRoland Dreier struct ib_qp_attr *attr; 246aef9ec39SRoland Dreier int ret; 247aef9ec39SRoland Dreier 248aef9ec39SRoland Dreier attr = kmalloc(sizeof *attr, GFP_KERNEL); 249aef9ec39SRoland Dreier if (!attr) 250aef9ec39SRoland Dreier return -ENOMEM; 251aef9ec39SRoland Dreier 252969a60f9SRoland Dreier ret = ib_find_pkey(target->srp_host->srp_dev->dev, 253aef9ec39SRoland Dreier target->srp_host->port, 254aef9ec39SRoland Dreier be16_to_cpu(target->path.pkey), 255aef9ec39SRoland Dreier &attr->pkey_index); 256aef9ec39SRoland Dreier if (ret) 257aef9ec39SRoland Dreier goto out; 258aef9ec39SRoland Dreier 259aef9ec39SRoland Dreier attr->qp_state = IB_QPS_INIT; 260aef9ec39SRoland Dreier attr->qp_access_flags = (IB_ACCESS_REMOTE_READ | 261aef9ec39SRoland Dreier IB_ACCESS_REMOTE_WRITE); 262aef9ec39SRoland Dreier attr->port_num = target->srp_host->port; 263aef9ec39SRoland Dreier 264aef9ec39SRoland Dreier ret = ib_modify_qp(qp, attr, 265aef9ec39SRoland Dreier IB_QP_STATE | 266aef9ec39SRoland Dreier IB_QP_PKEY_INDEX | 267aef9ec39SRoland Dreier IB_QP_ACCESS_FLAGS | 268aef9ec39SRoland Dreier IB_QP_PORT); 269aef9ec39SRoland Dreier 270aef9ec39SRoland Dreier out: 271aef9ec39SRoland Dreier kfree(attr); 272aef9ec39SRoland Dreier return ret; 273aef9ec39SRoland Dreier } 274aef9ec39SRoland Dreier 2759fe4bcf4SDavid Dillow static int srp_new_cm_id(struct srp_target_port *target) 2769fe4bcf4SDavid Dillow { 2779fe4bcf4SDavid Dillow struct ib_cm_id *new_cm_id; 2789fe4bcf4SDavid Dillow 27905321937SGreg Kroah-Hartman new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev, 2809fe4bcf4SDavid Dillow srp_cm_handler, target); 2819fe4bcf4SDavid Dillow if (IS_ERR(new_cm_id)) 2829fe4bcf4SDavid Dillow return PTR_ERR(new_cm_id); 2839fe4bcf4SDavid Dillow 2849fe4bcf4SDavid Dillow if (target->cm_id) 2859fe4bcf4SDavid Dillow ib_destroy_cm_id(target->cm_id); 2869fe4bcf4SDavid Dillow target->cm_id = new_cm_id; 2879fe4bcf4SDavid Dillow 2889fe4bcf4SDavid Dillow return 0; 2899fe4bcf4SDavid Dillow } 2909fe4bcf4SDavid Dillow 291aef9ec39SRoland Dreier static int srp_create_target_ib(struct srp_target_port *target) 292aef9ec39SRoland Dreier { 293aef9ec39SRoland Dreier struct ib_qp_init_attr *init_attr; 29473aa89edSIshai Rabinovitz struct ib_cq *recv_cq, *send_cq; 29573aa89edSIshai Rabinovitz struct ib_qp *qp; 296aef9ec39SRoland Dreier int ret; 297aef9ec39SRoland Dreier 298aef9ec39SRoland Dreier init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL); 299aef9ec39SRoland Dreier if (!init_attr) 300aef9ec39SRoland Dreier return -ENOMEM; 301aef9ec39SRoland Dreier 30273aa89edSIshai Rabinovitz recv_cq = ib_create_cq(target->srp_host->srp_dev->dev, 3034d73f95fSBart Van Assche srp_recv_completion, NULL, target, 3044d73f95fSBart Van Assche target->queue_size, target->comp_vector); 30573aa89edSIshai Rabinovitz if (IS_ERR(recv_cq)) { 30673aa89edSIshai Rabinovitz ret = PTR_ERR(recv_cq); 307da9d2f07SRoland Dreier goto err; 308aef9ec39SRoland Dreier } 309aef9ec39SRoland Dreier 31073aa89edSIshai Rabinovitz send_cq = ib_create_cq(target->srp_host->srp_dev->dev, 3114d73f95fSBart Van Assche srp_send_completion, NULL, target, 3124d73f95fSBart Van Assche target->queue_size, target->comp_vector); 31373aa89edSIshai Rabinovitz if (IS_ERR(send_cq)) { 31473aa89edSIshai Rabinovitz ret = PTR_ERR(send_cq); 315da9d2f07SRoland Dreier goto err_recv_cq; 3169c03dc9fSBart Van Assche } 3179c03dc9fSBart Van Assche 31873aa89edSIshai Rabinovitz ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP); 319aef9ec39SRoland Dreier 320aef9ec39SRoland Dreier init_attr->event_handler = srp_qp_event; 3214d73f95fSBart Van Assche init_attr->cap.max_send_wr = target->queue_size; 3224d73f95fSBart Van Assche init_attr->cap.max_recv_wr = target->queue_size; 323aef9ec39SRoland Dreier init_attr->cap.max_recv_sge = 1; 324aef9ec39SRoland Dreier init_attr->cap.max_send_sge = 1; 325aef9ec39SRoland Dreier init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 326aef9ec39SRoland Dreier init_attr->qp_type = IB_QPT_RC; 32773aa89edSIshai Rabinovitz init_attr->send_cq = send_cq; 32873aa89edSIshai Rabinovitz init_attr->recv_cq = recv_cq; 329aef9ec39SRoland Dreier 33073aa89edSIshai Rabinovitz qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr); 33173aa89edSIshai Rabinovitz if (IS_ERR(qp)) { 33273aa89edSIshai Rabinovitz ret = PTR_ERR(qp); 333da9d2f07SRoland Dreier goto err_send_cq; 334aef9ec39SRoland Dreier } 335aef9ec39SRoland Dreier 33673aa89edSIshai Rabinovitz ret = srp_init_qp(target, qp); 337da9d2f07SRoland Dreier if (ret) 338da9d2f07SRoland Dreier goto err_qp; 339aef9ec39SRoland Dreier 34073aa89edSIshai Rabinovitz if (target->qp) 34173aa89edSIshai Rabinovitz ib_destroy_qp(target->qp); 34273aa89edSIshai Rabinovitz if (target->recv_cq) 34373aa89edSIshai Rabinovitz ib_destroy_cq(target->recv_cq); 34473aa89edSIshai Rabinovitz if (target->send_cq) 34573aa89edSIshai Rabinovitz ib_destroy_cq(target->send_cq); 34673aa89edSIshai Rabinovitz 34773aa89edSIshai Rabinovitz target->qp = qp; 34873aa89edSIshai Rabinovitz target->recv_cq = recv_cq; 34973aa89edSIshai Rabinovitz target->send_cq = send_cq; 35073aa89edSIshai Rabinovitz 351da9d2f07SRoland Dreier kfree(init_attr); 352da9d2f07SRoland Dreier return 0; 353da9d2f07SRoland Dreier 354da9d2f07SRoland Dreier err_qp: 35573aa89edSIshai Rabinovitz ib_destroy_qp(qp); 356da9d2f07SRoland Dreier 357da9d2f07SRoland Dreier err_send_cq: 35873aa89edSIshai Rabinovitz ib_destroy_cq(send_cq); 359da9d2f07SRoland Dreier 360da9d2f07SRoland Dreier err_recv_cq: 36173aa89edSIshai Rabinovitz ib_destroy_cq(recv_cq); 362da9d2f07SRoland Dreier 363da9d2f07SRoland Dreier err: 364aef9ec39SRoland Dreier kfree(init_attr); 365aef9ec39SRoland Dreier return ret; 366aef9ec39SRoland Dreier } 367aef9ec39SRoland Dreier 3684d73f95fSBart Van Assche /* 3694d73f95fSBart Van Assche * Note: this function may be called without srp_alloc_iu_bufs() having been 3704d73f95fSBart Van Assche * invoked. Hence the target->[rt]x_ring checks. 3714d73f95fSBart Van Assche */ 372aef9ec39SRoland Dreier static void srp_free_target_ib(struct srp_target_port *target) 373aef9ec39SRoland Dreier { 374aef9ec39SRoland Dreier int i; 375aef9ec39SRoland Dreier 376aef9ec39SRoland Dreier ib_destroy_qp(target->qp); 3779c03dc9fSBart Van Assche ib_destroy_cq(target->send_cq); 3789c03dc9fSBart Van Assche ib_destroy_cq(target->recv_cq); 379aef9ec39SRoland Dreier 38073aa89edSIshai Rabinovitz target->qp = NULL; 38173aa89edSIshai Rabinovitz target->send_cq = target->recv_cq = NULL; 38273aa89edSIshai Rabinovitz 3834d73f95fSBart Van Assche if (target->rx_ring) { 3844d73f95fSBart Van Assche for (i = 0; i < target->queue_size; ++i) 385aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->rx_ring[i]); 3864d73f95fSBart Van Assche kfree(target->rx_ring); 3874d73f95fSBart Van Assche target->rx_ring = NULL; 3884d73f95fSBart Van Assche } 3894d73f95fSBart Van Assche if (target->tx_ring) { 3904d73f95fSBart Van Assche for (i = 0; i < target->queue_size; ++i) 391aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->tx_ring[i]); 3924d73f95fSBart Van Assche kfree(target->tx_ring); 3934d73f95fSBart Van Assche target->tx_ring = NULL; 3944d73f95fSBart Van Assche } 395aef9ec39SRoland Dreier } 396aef9ec39SRoland Dreier 397aef9ec39SRoland Dreier static void srp_path_rec_completion(int status, 398aef9ec39SRoland Dreier struct ib_sa_path_rec *pathrec, 399aef9ec39SRoland Dreier void *target_ptr) 400aef9ec39SRoland Dreier { 401aef9ec39SRoland Dreier struct srp_target_port *target = target_ptr; 402aef9ec39SRoland Dreier 403aef9ec39SRoland Dreier target->status = status; 404aef9ec39SRoland Dreier if (status) 4057aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 4067aa54bd7SDavid Dillow PFX "Got failed path rec status %d\n", status); 407aef9ec39SRoland Dreier else 408aef9ec39SRoland Dreier target->path = *pathrec; 409aef9ec39SRoland Dreier complete(&target->done); 410aef9ec39SRoland Dreier } 411aef9ec39SRoland Dreier 412aef9ec39SRoland Dreier static int srp_lookup_path(struct srp_target_port *target) 413aef9ec39SRoland Dreier { 414aef9ec39SRoland Dreier target->path.numb_path = 1; 415aef9ec39SRoland Dreier 416aef9ec39SRoland Dreier init_completion(&target->done); 417aef9ec39SRoland Dreier 418c1a0b23bSMichael S. Tsirkin target->path_query_id = ib_sa_path_rec_get(&srp_sa_client, 41905321937SGreg Kroah-Hartman target->srp_host->srp_dev->dev, 420aef9ec39SRoland Dreier target->srp_host->port, 421aef9ec39SRoland Dreier &target->path, 422247e020eSSean Hefty IB_SA_PATH_REC_SERVICE_ID | 423aef9ec39SRoland Dreier IB_SA_PATH_REC_DGID | 424aef9ec39SRoland Dreier IB_SA_PATH_REC_SGID | 425aef9ec39SRoland Dreier IB_SA_PATH_REC_NUMB_PATH | 426aef9ec39SRoland Dreier IB_SA_PATH_REC_PKEY, 427aef9ec39SRoland Dreier SRP_PATH_REC_TIMEOUT_MS, 428aef9ec39SRoland Dreier GFP_KERNEL, 429aef9ec39SRoland Dreier srp_path_rec_completion, 430aef9ec39SRoland Dreier target, &target->path_query); 431aef9ec39SRoland Dreier if (target->path_query_id < 0) 432aef9ec39SRoland Dreier return target->path_query_id; 433aef9ec39SRoland Dreier 434aef9ec39SRoland Dreier wait_for_completion(&target->done); 435aef9ec39SRoland Dreier 436aef9ec39SRoland Dreier if (target->status < 0) 4377aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 4387aa54bd7SDavid Dillow PFX "Path record query failed\n"); 439aef9ec39SRoland Dreier 440aef9ec39SRoland Dreier return target->status; 441aef9ec39SRoland Dreier } 442aef9ec39SRoland Dreier 443aef9ec39SRoland Dreier static int srp_send_req(struct srp_target_port *target) 444aef9ec39SRoland Dreier { 445aef9ec39SRoland Dreier struct { 446aef9ec39SRoland Dreier struct ib_cm_req_param param; 447aef9ec39SRoland Dreier struct srp_login_req priv; 448aef9ec39SRoland Dreier } *req = NULL; 449aef9ec39SRoland Dreier int status; 450aef9ec39SRoland Dreier 451aef9ec39SRoland Dreier req = kzalloc(sizeof *req, GFP_KERNEL); 452aef9ec39SRoland Dreier if (!req) 453aef9ec39SRoland Dreier return -ENOMEM; 454aef9ec39SRoland Dreier 455aef9ec39SRoland Dreier req->param.primary_path = &target->path; 456aef9ec39SRoland Dreier req->param.alternate_path = NULL; 457aef9ec39SRoland Dreier req->param.service_id = target->service_id; 458aef9ec39SRoland Dreier req->param.qp_num = target->qp->qp_num; 459aef9ec39SRoland Dreier req->param.qp_type = target->qp->qp_type; 460aef9ec39SRoland Dreier req->param.private_data = &req->priv; 461aef9ec39SRoland Dreier req->param.private_data_len = sizeof req->priv; 462aef9ec39SRoland Dreier req->param.flow_control = 1; 463aef9ec39SRoland Dreier 464aef9ec39SRoland Dreier get_random_bytes(&req->param.starting_psn, 4); 465aef9ec39SRoland Dreier req->param.starting_psn &= 0xffffff; 466aef9ec39SRoland Dreier 467aef9ec39SRoland Dreier /* 468aef9ec39SRoland Dreier * Pick some arbitrary defaults here; we could make these 469aef9ec39SRoland Dreier * module parameters if anyone cared about setting them. 470aef9ec39SRoland Dreier */ 471aef9ec39SRoland Dreier req->param.responder_resources = 4; 472aef9ec39SRoland Dreier req->param.remote_cm_response_timeout = 20; 473aef9ec39SRoland Dreier req->param.local_cm_response_timeout = 20; 4747bb312e4SVu Pham req->param.retry_count = target->tl_retry_count; 475aef9ec39SRoland Dreier req->param.rnr_retry_count = 7; 476aef9ec39SRoland Dreier req->param.max_cm_retries = 15; 477aef9ec39SRoland Dreier 478aef9ec39SRoland Dreier req->priv.opcode = SRP_LOGIN_REQ; 479aef9ec39SRoland Dreier req->priv.tag = 0; 48049248644SDavid Dillow req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len); 481aef9ec39SRoland Dreier req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | 482aef9ec39SRoland Dreier SRP_BUF_FORMAT_INDIRECT); 4830c0450dbSRamachandra K /* 4840c0450dbSRamachandra K * In the published SRP specification (draft rev. 16a), the 4850c0450dbSRamachandra K * port identifier format is 8 bytes of ID extension followed 4860c0450dbSRamachandra K * by 8 bytes of GUID. Older drafts put the two halves in the 4870c0450dbSRamachandra K * opposite order, so that the GUID comes first. 4880c0450dbSRamachandra K * 4890c0450dbSRamachandra K * Targets conforming to these obsolete drafts can be 4900c0450dbSRamachandra K * recognized by the I/O Class they report. 4910c0450dbSRamachandra K */ 4920c0450dbSRamachandra K if (target->io_class == SRP_REV10_IB_IO_CLASS) { 4930c0450dbSRamachandra K memcpy(req->priv.initiator_port_id, 49401cb9bcbSIshai Rabinovitz &target->path.sgid.global.interface_id, 8); 4950c0450dbSRamachandra K memcpy(req->priv.initiator_port_id + 8, 49601cb9bcbSIshai Rabinovitz &target->initiator_ext, 8); 4970c0450dbSRamachandra K memcpy(req->priv.target_port_id, &target->ioc_guid, 8); 4980c0450dbSRamachandra K memcpy(req->priv.target_port_id + 8, &target->id_ext, 8); 4990c0450dbSRamachandra K } else { 5000c0450dbSRamachandra K memcpy(req->priv.initiator_port_id, 50101cb9bcbSIshai Rabinovitz &target->initiator_ext, 8); 50201cb9bcbSIshai Rabinovitz memcpy(req->priv.initiator_port_id + 8, 50301cb9bcbSIshai Rabinovitz &target->path.sgid.global.interface_id, 8); 5040c0450dbSRamachandra K memcpy(req->priv.target_port_id, &target->id_ext, 8); 5050c0450dbSRamachandra K memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8); 5060c0450dbSRamachandra K } 5070c0450dbSRamachandra K 508aef9ec39SRoland Dreier /* 509aef9ec39SRoland Dreier * Topspin/Cisco SRP targets will reject our login unless we 51001cb9bcbSIshai Rabinovitz * zero out the first 8 bytes of our initiator port ID and set 51101cb9bcbSIshai Rabinovitz * the second 8 bytes to the local node GUID. 512aef9ec39SRoland Dreier */ 5135d7cbfd6SRoland Dreier if (srp_target_is_topspin(target)) { 5147aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, 5157aa54bd7SDavid Dillow PFX "Topspin/Cisco initiator port ID workaround " 516aef9ec39SRoland Dreier "activated for target GUID %016llx\n", 517aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->ioc_guid)); 518aef9ec39SRoland Dreier memset(req->priv.initiator_port_id, 0, 8); 51901cb9bcbSIshai Rabinovitz memcpy(req->priv.initiator_port_id + 8, 52005321937SGreg Kroah-Hartman &target->srp_host->srp_dev->dev->node_guid, 8); 521aef9ec39SRoland Dreier } 522aef9ec39SRoland Dreier 523aef9ec39SRoland Dreier status = ib_send_cm_req(target->cm_id, &req->param); 524aef9ec39SRoland Dreier 525aef9ec39SRoland Dreier kfree(req); 526aef9ec39SRoland Dreier 527aef9ec39SRoland Dreier return status; 528aef9ec39SRoland Dreier } 529aef9ec39SRoland Dreier 530ef6c49d8SBart Van Assche static bool srp_queue_remove_work(struct srp_target_port *target) 531ef6c49d8SBart Van Assche { 532ef6c49d8SBart Van Assche bool changed = false; 533ef6c49d8SBart Van Assche 534ef6c49d8SBart Van Assche spin_lock_irq(&target->lock); 535ef6c49d8SBart Van Assche if (target->state != SRP_TARGET_REMOVED) { 536ef6c49d8SBart Van Assche target->state = SRP_TARGET_REMOVED; 537ef6c49d8SBart Van Assche changed = true; 538ef6c49d8SBart Van Assche } 539ef6c49d8SBart Van Assche spin_unlock_irq(&target->lock); 540ef6c49d8SBart Van Assche 541ef6c49d8SBart Van Assche if (changed) 542ef6c49d8SBart Van Assche queue_work(system_long_wq, &target->remove_work); 543ef6c49d8SBart Van Assche 544ef6c49d8SBart Van Assche return changed; 545ef6c49d8SBart Van Assche } 546ef6c49d8SBart Van Assche 547294c875aSBart Van Assche static bool srp_change_conn_state(struct srp_target_port *target, 548294c875aSBart Van Assche bool connected) 549294c875aSBart Van Assche { 550294c875aSBart Van Assche bool changed = false; 551294c875aSBart Van Assche 552294c875aSBart Van Assche spin_lock_irq(&target->lock); 553294c875aSBart Van Assche if (target->connected != connected) { 554294c875aSBart Van Assche target->connected = connected; 555294c875aSBart Van Assche changed = true; 556294c875aSBart Van Assche } 557294c875aSBart Van Assche spin_unlock_irq(&target->lock); 558294c875aSBart Van Assche 559294c875aSBart Van Assche return changed; 560294c875aSBart Van Assche } 561294c875aSBart Van Assche 562aef9ec39SRoland Dreier static void srp_disconnect_target(struct srp_target_port *target) 563aef9ec39SRoland Dreier { 564294c875aSBart Van Assche if (srp_change_conn_state(target, false)) { 565aef9ec39SRoland Dreier /* XXX should send SRP_I_LOGOUT request */ 566aef9ec39SRoland Dreier 567e6581056SRoland Dreier if (ib_send_cm_dreq(target->cm_id, NULL, 0)) { 5687aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, 5697aa54bd7SDavid Dillow PFX "Sending CM DREQ failed\n"); 570aef9ec39SRoland Dreier } 571294c875aSBart Van Assche } 572294c875aSBart Van Assche } 573aef9ec39SRoland Dreier 5748f26c9ffSDavid Dillow static void srp_free_req_data(struct srp_target_port *target) 5758f26c9ffSDavid Dillow { 576c07d424dSDavid Dillow struct ib_device *ibdev = target->srp_host->srp_dev->dev; 5778f26c9ffSDavid Dillow struct srp_request *req; 5788f26c9ffSDavid Dillow int i; 5798f26c9ffSDavid Dillow 5804d73f95fSBart Van Assche if (!target->req_ring) 5814d73f95fSBart Van Assche return; 5824d73f95fSBart Van Assche 5834d73f95fSBart Van Assche for (i = 0; i < target->req_ring_size; ++i) { 5844d73f95fSBart Van Assche req = &target->req_ring[i]; 5858f26c9ffSDavid Dillow kfree(req->fmr_list); 5868f26c9ffSDavid Dillow kfree(req->map_page); 587c07d424dSDavid Dillow if (req->indirect_dma_addr) { 588c07d424dSDavid Dillow ib_dma_unmap_single(ibdev, req->indirect_dma_addr, 589c07d424dSDavid Dillow target->indirect_size, 590c07d424dSDavid Dillow DMA_TO_DEVICE); 591c07d424dSDavid Dillow } 592c07d424dSDavid Dillow kfree(req->indirect_desc); 5938f26c9ffSDavid Dillow } 5944d73f95fSBart Van Assche 5954d73f95fSBart Van Assche kfree(target->req_ring); 5964d73f95fSBart Van Assche target->req_ring = NULL; 5978f26c9ffSDavid Dillow } 5988f26c9ffSDavid Dillow 599b81d00bdSBart Van Assche static int srp_alloc_req_data(struct srp_target_port *target) 600b81d00bdSBart Van Assche { 601b81d00bdSBart Van Assche struct srp_device *srp_dev = target->srp_host->srp_dev; 602b81d00bdSBart Van Assche struct ib_device *ibdev = srp_dev->dev; 603b81d00bdSBart Van Assche struct srp_request *req; 604b81d00bdSBart Van Assche dma_addr_t dma_addr; 605b81d00bdSBart Van Assche int i, ret = -ENOMEM; 606b81d00bdSBart Van Assche 607b81d00bdSBart Van Assche INIT_LIST_HEAD(&target->free_reqs); 608b81d00bdSBart Van Assche 6094d73f95fSBart Van Assche target->req_ring = kzalloc(target->req_ring_size * 6104d73f95fSBart Van Assche sizeof(*target->req_ring), GFP_KERNEL); 6114d73f95fSBart Van Assche if (!target->req_ring) 6124d73f95fSBart Van Assche goto out; 6134d73f95fSBart Van Assche 6144d73f95fSBart Van Assche for (i = 0; i < target->req_ring_size; ++i) { 615b81d00bdSBart Van Assche req = &target->req_ring[i]; 616b81d00bdSBart Van Assche req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *), 617b81d00bdSBart Van Assche GFP_KERNEL); 618b81d00bdSBart Van Assche req->map_page = kmalloc(SRP_FMR_SIZE * sizeof(void *), 619b81d00bdSBart Van Assche GFP_KERNEL); 620b81d00bdSBart Van Assche req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL); 621b81d00bdSBart Van Assche if (!req->fmr_list || !req->map_page || !req->indirect_desc) 622b81d00bdSBart Van Assche goto out; 623b81d00bdSBart Van Assche 624b81d00bdSBart Van Assche dma_addr = ib_dma_map_single(ibdev, req->indirect_desc, 625b81d00bdSBart Van Assche target->indirect_size, 626b81d00bdSBart Van Assche DMA_TO_DEVICE); 627b81d00bdSBart Van Assche if (ib_dma_mapping_error(ibdev, dma_addr)) 628b81d00bdSBart Van Assche goto out; 629b81d00bdSBart Van Assche 630b81d00bdSBart Van Assche req->indirect_dma_addr = dma_addr; 631b81d00bdSBart Van Assche req->index = i; 632b81d00bdSBart Van Assche list_add_tail(&req->list, &target->free_reqs); 633b81d00bdSBart Van Assche } 634b81d00bdSBart Van Assche ret = 0; 635b81d00bdSBart Van Assche 636b81d00bdSBart Van Assche out: 637b81d00bdSBart Van Assche return ret; 638b81d00bdSBart Van Assche } 639b81d00bdSBart Van Assche 640683b159aSBart Van Assche /** 641683b159aSBart Van Assche * srp_del_scsi_host_attr() - Remove attributes defined in the host template. 642683b159aSBart Van Assche * @shost: SCSI host whose attributes to remove from sysfs. 643683b159aSBart Van Assche * 644683b159aSBart Van Assche * Note: Any attributes defined in the host template and that did not exist 645683b159aSBart Van Assche * before invocation of this function will be ignored. 646683b159aSBart Van Assche */ 647683b159aSBart Van Assche static void srp_del_scsi_host_attr(struct Scsi_Host *shost) 648683b159aSBart Van Assche { 649683b159aSBart Van Assche struct device_attribute **attr; 650683b159aSBart Van Assche 651683b159aSBart Van Assche for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr) 652683b159aSBart Van Assche device_remove_file(&shost->shost_dev, *attr); 653683b159aSBart Van Assche } 654683b159aSBart Van Assche 655ee12d6a8SBart Van Assche static void srp_remove_target(struct srp_target_port *target) 656ee12d6a8SBart Van Assche { 657ef6c49d8SBart Van Assche WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED); 658ef6c49d8SBart Van Assche 659ee12d6a8SBart Van Assche srp_del_scsi_host_attr(target->scsi_host); 6609dd69a60SBart Van Assche srp_rport_get(target->rport); 661ee12d6a8SBart Van Assche srp_remove_host(target->scsi_host); 662ee12d6a8SBart Van Assche scsi_remove_host(target->scsi_host); 66393079162SBart Van Assche srp_stop_rport_timers(target->rport); 664ef6c49d8SBart Van Assche srp_disconnect_target(target); 665ee12d6a8SBart Van Assche ib_destroy_cm_id(target->cm_id); 666ee12d6a8SBart Van Assche srp_free_target_ib(target); 667c1120f89SBart Van Assche cancel_work_sync(&target->tl_err_work); 6689dd69a60SBart Van Assche srp_rport_put(target->rport); 669ee12d6a8SBart Van Assche srp_free_req_data(target); 67065d7dd2fSVu Pham 67165d7dd2fSVu Pham spin_lock(&target->srp_host->target_lock); 67265d7dd2fSVu Pham list_del(&target->list); 67365d7dd2fSVu Pham spin_unlock(&target->srp_host->target_lock); 67465d7dd2fSVu Pham 675ee12d6a8SBart Van Assche scsi_host_put(target->scsi_host); 676ee12d6a8SBart Van Assche } 677ee12d6a8SBart Van Assche 678c4028958SDavid Howells static void srp_remove_work(struct work_struct *work) 679aef9ec39SRoland Dreier { 680c4028958SDavid Howells struct srp_target_port *target = 681ef6c49d8SBart Van Assche container_of(work, struct srp_target_port, remove_work); 682aef9ec39SRoland Dreier 683ef6c49d8SBart Van Assche WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED); 684aef9ec39SRoland Dreier 68596fc248aSBart Van Assche srp_remove_target(target); 686aef9ec39SRoland Dreier } 687aef9ec39SRoland Dreier 688dc1bdbd9SBart Van Assche static void srp_rport_delete(struct srp_rport *rport) 689dc1bdbd9SBart Van Assche { 690dc1bdbd9SBart Van Assche struct srp_target_port *target = rport->lld_data; 691dc1bdbd9SBart Van Assche 692dc1bdbd9SBart Van Assche srp_queue_remove_work(target); 693dc1bdbd9SBart Van Assche } 694dc1bdbd9SBart Van Assche 695aef9ec39SRoland Dreier static int srp_connect_target(struct srp_target_port *target) 696aef9ec39SRoland Dreier { 6979fe4bcf4SDavid Dillow int retries = 3; 698aef9ec39SRoland Dreier int ret; 699aef9ec39SRoland Dreier 700294c875aSBart Van Assche WARN_ON_ONCE(target->connected); 701294c875aSBart Van Assche 702948d1e88SBart Van Assche target->qp_in_error = false; 703948d1e88SBart Van Assche 704aef9ec39SRoland Dreier ret = srp_lookup_path(target); 705aef9ec39SRoland Dreier if (ret) 706aef9ec39SRoland Dreier return ret; 707aef9ec39SRoland Dreier 708aef9ec39SRoland Dreier while (1) { 709aef9ec39SRoland Dreier init_completion(&target->done); 710aef9ec39SRoland Dreier ret = srp_send_req(target); 711aef9ec39SRoland Dreier if (ret) 712aef9ec39SRoland Dreier return ret; 713aef9ec39SRoland Dreier wait_for_completion(&target->done); 714aef9ec39SRoland Dreier 715aef9ec39SRoland Dreier /* 716aef9ec39SRoland Dreier * The CM event handling code will set status to 717aef9ec39SRoland Dreier * SRP_PORT_REDIRECT if we get a port redirect REJ 718aef9ec39SRoland Dreier * back, or SRP_DLID_REDIRECT if we get a lid/qp 719aef9ec39SRoland Dreier * redirect REJ back. 720aef9ec39SRoland Dreier */ 721aef9ec39SRoland Dreier switch (target->status) { 722aef9ec39SRoland Dreier case 0: 723294c875aSBart Van Assche srp_change_conn_state(target, true); 724aef9ec39SRoland Dreier return 0; 725aef9ec39SRoland Dreier 726aef9ec39SRoland Dreier case SRP_PORT_REDIRECT: 727aef9ec39SRoland Dreier ret = srp_lookup_path(target); 728aef9ec39SRoland Dreier if (ret) 729aef9ec39SRoland Dreier return ret; 730aef9ec39SRoland Dreier break; 731aef9ec39SRoland Dreier 732aef9ec39SRoland Dreier case SRP_DLID_REDIRECT: 733aef9ec39SRoland Dreier break; 734aef9ec39SRoland Dreier 7359fe4bcf4SDavid Dillow case SRP_STALE_CONN: 7369fe4bcf4SDavid Dillow /* Our current CM id was stale, and is now in timewait. 7379fe4bcf4SDavid Dillow * Try to reconnect with a new one. 7389fe4bcf4SDavid Dillow */ 7399fe4bcf4SDavid Dillow if (!retries-- || srp_new_cm_id(target)) { 7409fe4bcf4SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 7419fe4bcf4SDavid Dillow "giving up on stale connection\n"); 7429fe4bcf4SDavid Dillow target->status = -ECONNRESET; 7439fe4bcf4SDavid Dillow return target->status; 7449fe4bcf4SDavid Dillow } 7459fe4bcf4SDavid Dillow 7469fe4bcf4SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 7479fe4bcf4SDavid Dillow "retrying stale connection\n"); 7489fe4bcf4SDavid Dillow break; 7499fe4bcf4SDavid Dillow 750aef9ec39SRoland Dreier default: 751aef9ec39SRoland Dreier return target->status; 752aef9ec39SRoland Dreier } 753aef9ec39SRoland Dreier } 754aef9ec39SRoland Dreier } 755aef9ec39SRoland Dreier 756d945e1dfSRoland Dreier static void srp_unmap_data(struct scsi_cmnd *scmnd, 757d945e1dfSRoland Dreier struct srp_target_port *target, 758d945e1dfSRoland Dreier struct srp_request *req) 759d945e1dfSRoland Dreier { 7608f26c9ffSDavid Dillow struct ib_device *ibdev = target->srp_host->srp_dev->dev; 7618f26c9ffSDavid Dillow struct ib_pool_fmr **pfmr; 7628f26c9ffSDavid Dillow 763bb350d1dSFUJITA Tomonori if (!scsi_sglist(scmnd) || 764d945e1dfSRoland Dreier (scmnd->sc_data_direction != DMA_TO_DEVICE && 765d945e1dfSRoland Dreier scmnd->sc_data_direction != DMA_FROM_DEVICE)) 766d945e1dfSRoland Dreier return; 767d945e1dfSRoland Dreier 7688f26c9ffSDavid Dillow pfmr = req->fmr_list; 7698f26c9ffSDavid Dillow while (req->nfmr--) 7708f26c9ffSDavid Dillow ib_fmr_pool_unmap(*pfmr++); 771f5358a17SRoland Dreier 7728f26c9ffSDavid Dillow ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd), 7738f26c9ffSDavid Dillow scmnd->sc_data_direction); 774d945e1dfSRoland Dreier } 775d945e1dfSRoland Dreier 77622032991SBart Van Assche /** 77722032991SBart Van Assche * srp_claim_req - Take ownership of the scmnd associated with a request. 77822032991SBart Van Assche * @target: SRP target port. 77922032991SBart Van Assche * @req: SRP request. 78022032991SBart Van Assche * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take 78122032991SBart Van Assche * ownership of @req->scmnd if it equals @scmnd. 78222032991SBart Van Assche * 78322032991SBart Van Assche * Return value: 78422032991SBart Van Assche * Either NULL or a pointer to the SCSI command the caller became owner of. 78522032991SBart Van Assche */ 78622032991SBart Van Assche static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target, 78722032991SBart Van Assche struct srp_request *req, 78822032991SBart Van Assche struct scsi_cmnd *scmnd) 789526b4caaSIshai Rabinovitz { 79094a9174cSBart Van Assche unsigned long flags; 79194a9174cSBart Van Assche 79222032991SBart Van Assche spin_lock_irqsave(&target->lock, flags); 79322032991SBart Van Assche if (!scmnd) { 79422032991SBart Van Assche scmnd = req->scmnd; 79522032991SBart Van Assche req->scmnd = NULL; 79622032991SBart Van Assche } else if (req->scmnd == scmnd) { 79722032991SBart Van Assche req->scmnd = NULL; 79822032991SBart Van Assche } else { 79922032991SBart Van Assche scmnd = NULL; 80022032991SBart Van Assche } 80122032991SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 80222032991SBart Van Assche 80322032991SBart Van Assche return scmnd; 80422032991SBart Van Assche } 80522032991SBart Van Assche 80622032991SBart Van Assche /** 80722032991SBart Van Assche * srp_free_req() - Unmap data and add request to the free request list. 80822032991SBart Van Assche */ 80922032991SBart Van Assche static void srp_free_req(struct srp_target_port *target, 81022032991SBart Van Assche struct srp_request *req, struct scsi_cmnd *scmnd, 81122032991SBart Van Assche s32 req_lim_delta) 81222032991SBart Van Assche { 81322032991SBart Van Assche unsigned long flags; 81422032991SBart Van Assche 81522032991SBart Van Assche srp_unmap_data(scmnd, target, req); 81622032991SBart Van Assche 817e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 81894a9174cSBart Van Assche target->req_lim += req_lim_delta; 819536ae14eSBart Van Assche list_add_tail(&req->list, &target->free_reqs); 820e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 821526b4caaSIshai Rabinovitz } 822526b4caaSIshai Rabinovitz 823ed9b2264SBart Van Assche static void srp_finish_req(struct srp_target_port *target, 824ed9b2264SBart Van Assche struct srp_request *req, int result) 825526b4caaSIshai Rabinovitz { 82622032991SBart Van Assche struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL); 82722032991SBart Van Assche 82822032991SBart Van Assche if (scmnd) { 8299b796d06SBart Van Assche srp_free_req(target, req, scmnd, 0); 830ed9b2264SBart Van Assche scmnd->result = result; 83122032991SBart Van Assche scmnd->scsi_done(scmnd); 83222032991SBart Van Assche } 833526b4caaSIshai Rabinovitz } 834526b4caaSIshai Rabinovitz 835ed9b2264SBart Van Assche static void srp_terminate_io(struct srp_rport *rport) 836aef9ec39SRoland Dreier { 837ed9b2264SBart Van Assche struct srp_target_port *target = rport->lld_data; 838ed9b2264SBart Van Assche int i; 839aef9ec39SRoland Dreier 8404d73f95fSBart Van Assche for (i = 0; i < target->req_ring_size; ++i) { 841ed9b2264SBart Van Assche struct srp_request *req = &target->req_ring[i]; 842ed9b2264SBart Van Assche srp_finish_req(target, req, DID_TRANSPORT_FAILFAST << 16); 843ed9b2264SBart Van Assche } 844ed9b2264SBart Van Assche } 845ed9b2264SBart Van Assche 846ed9b2264SBart Van Assche /* 847ed9b2264SBart Van Assche * It is up to the caller to ensure that srp_rport_reconnect() calls are 848ed9b2264SBart Van Assche * serialized and that no concurrent srp_queuecommand(), srp_abort(), 849ed9b2264SBart Van Assche * srp_reset_device() or srp_reset_host() calls will occur while this function 850ed9b2264SBart Van Assche * is in progress. One way to realize that is not to call this function 851ed9b2264SBart Van Assche * directly but to call srp_reconnect_rport() instead since that last function 852ed9b2264SBart Van Assche * serializes calls of this function via rport->mutex and also blocks 853ed9b2264SBart Van Assche * srp_queuecommand() calls before invoking this function. 854ed9b2264SBart Van Assche */ 855ed9b2264SBart Van Assche static int srp_rport_reconnect(struct srp_rport *rport) 856ed9b2264SBart Van Assche { 857ed9b2264SBart Van Assche struct srp_target_port *target = rport->lld_data; 858ed9b2264SBart Van Assche int i, ret; 85909be70a2SBart Van Assche 860aef9ec39SRoland Dreier srp_disconnect_target(target); 861aef9ec39SRoland Dreier /* 862c7c4e7ffSBart Van Assche * Now get a new local CM ID so that we avoid confusing the target in 863c7c4e7ffSBart Van Assche * case things are really fouled up. Doing so also ensures that all CM 864c7c4e7ffSBart Van Assche * callbacks will have finished before a new QP is allocated. 865aef9ec39SRoland Dreier */ 8669fe4bcf4SDavid Dillow ret = srp_new_cm_id(target); 867c7c4e7ffSBart Van Assche /* 868c7c4e7ffSBart Van Assche * Whether or not creating a new CM ID succeeded, create a new 869c7c4e7ffSBart Van Assche * QP. This guarantees that all completion callback function 870c7c4e7ffSBart Van Assche * invocations have finished before request resetting starts. 871c7c4e7ffSBart Van Assche */ 872c7c4e7ffSBart Van Assche if (ret == 0) 87373aa89edSIshai Rabinovitz ret = srp_create_target_ib(target); 874c7c4e7ffSBart Van Assche else 875c7c4e7ffSBart Van Assche srp_create_target_ib(target); 876aef9ec39SRoland Dreier 8774d73f95fSBart Van Assche for (i = 0; i < target->req_ring_size; ++i) { 878536ae14eSBart Van Assche struct srp_request *req = &target->req_ring[i]; 879ed9b2264SBart Van Assche srp_finish_req(target, req, DID_RESET << 16); 880536ae14eSBart Van Assche } 881aef9ec39SRoland Dreier 882536ae14eSBart Van Assche INIT_LIST_HEAD(&target->free_tx); 8834d73f95fSBart Van Assche for (i = 0; i < target->queue_size; ++i) 884536ae14eSBart Van Assche list_add(&target->tx_ring[i]->list, &target->free_tx); 885aef9ec39SRoland Dreier 886c7c4e7ffSBart Van Assche if (ret == 0) 887aef9ec39SRoland Dreier ret = srp_connect_target(target); 88809be70a2SBart Van Assche 889ed9b2264SBart Van Assche if (ret == 0) 890ed9b2264SBart Van Assche shost_printk(KERN_INFO, target->scsi_host, 891ed9b2264SBart Van Assche PFX "reconnect succeeded\n"); 892aef9ec39SRoland Dreier 893aef9ec39SRoland Dreier return ret; 894aef9ec39SRoland Dreier } 895aef9ec39SRoland Dreier 8968f26c9ffSDavid Dillow static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr, 8978f26c9ffSDavid Dillow unsigned int dma_len, u32 rkey) 898f5358a17SRoland Dreier { 8998f26c9ffSDavid Dillow struct srp_direct_buf *desc = state->desc; 9008f26c9ffSDavid Dillow 9018f26c9ffSDavid Dillow desc->va = cpu_to_be64(dma_addr); 9028f26c9ffSDavid Dillow desc->key = cpu_to_be32(rkey); 9038f26c9ffSDavid Dillow desc->len = cpu_to_be32(dma_len); 9048f26c9ffSDavid Dillow 9058f26c9ffSDavid Dillow state->total_len += dma_len; 9068f26c9ffSDavid Dillow state->desc++; 9078f26c9ffSDavid Dillow state->ndesc++; 9088f26c9ffSDavid Dillow } 9098f26c9ffSDavid Dillow 9108f26c9ffSDavid Dillow static int srp_map_finish_fmr(struct srp_map_state *state, 9118f26c9ffSDavid Dillow struct srp_target_port *target) 9128f26c9ffSDavid Dillow { 9138f26c9ffSDavid Dillow struct srp_device *dev = target->srp_host->srp_dev; 9148f26c9ffSDavid Dillow struct ib_pool_fmr *fmr; 915f5358a17SRoland Dreier u64 io_addr = 0; 9168f26c9ffSDavid Dillow 9178f26c9ffSDavid Dillow if (!state->npages) 9188f26c9ffSDavid Dillow return 0; 9198f26c9ffSDavid Dillow 9208f26c9ffSDavid Dillow if (state->npages == 1) { 9218f26c9ffSDavid Dillow srp_map_desc(state, state->base_dma_addr, state->fmr_len, 9228f26c9ffSDavid Dillow target->rkey); 9238f26c9ffSDavid Dillow state->npages = state->fmr_len = 0; 9248f26c9ffSDavid Dillow return 0; 9258f26c9ffSDavid Dillow } 9268f26c9ffSDavid Dillow 9278f26c9ffSDavid Dillow fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages, 9288f26c9ffSDavid Dillow state->npages, io_addr); 9298f26c9ffSDavid Dillow if (IS_ERR(fmr)) 9308f26c9ffSDavid Dillow return PTR_ERR(fmr); 9318f26c9ffSDavid Dillow 9328f26c9ffSDavid Dillow *state->next_fmr++ = fmr; 9338f26c9ffSDavid Dillow state->nfmr++; 9348f26c9ffSDavid Dillow 9358f26c9ffSDavid Dillow srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey); 9368f26c9ffSDavid Dillow state->npages = state->fmr_len = 0; 9378f26c9ffSDavid Dillow return 0; 9388f26c9ffSDavid Dillow } 9398f26c9ffSDavid Dillow 9408f26c9ffSDavid Dillow static void srp_map_update_start(struct srp_map_state *state, 9418f26c9ffSDavid Dillow struct scatterlist *sg, int sg_index, 9428f26c9ffSDavid Dillow dma_addr_t dma_addr) 9438f26c9ffSDavid Dillow { 9448f26c9ffSDavid Dillow state->unmapped_sg = sg; 9458f26c9ffSDavid Dillow state->unmapped_index = sg_index; 9468f26c9ffSDavid Dillow state->unmapped_addr = dma_addr; 9478f26c9ffSDavid Dillow } 9488f26c9ffSDavid Dillow 9498f26c9ffSDavid Dillow static int srp_map_sg_entry(struct srp_map_state *state, 9508f26c9ffSDavid Dillow struct srp_target_port *target, 9518f26c9ffSDavid Dillow struct scatterlist *sg, int sg_index, 9528f26c9ffSDavid Dillow int use_fmr) 9538f26c9ffSDavid Dillow { 95405321937SGreg Kroah-Hartman struct srp_device *dev = target->srp_host->srp_dev; 95585507bccSRalph Campbell struct ib_device *ibdev = dev->dev; 9568f26c9ffSDavid Dillow dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg); 957bb350d1dSFUJITA Tomonori unsigned int dma_len = ib_sg_dma_len(ibdev, sg); 9588f26c9ffSDavid Dillow unsigned int len; 9598f26c9ffSDavid Dillow int ret; 96085507bccSRalph Campbell 9618f26c9ffSDavid Dillow if (!dma_len) 9628f26c9ffSDavid Dillow return 0; 9638f26c9ffSDavid Dillow 9648f26c9ffSDavid Dillow if (use_fmr == SRP_MAP_NO_FMR) { 9658f26c9ffSDavid Dillow /* Once we're in direct map mode for a request, we don't 9668f26c9ffSDavid Dillow * go back to FMR mode, so no need to update anything 9678f26c9ffSDavid Dillow * other than the descriptor. 9688f26c9ffSDavid Dillow */ 9698f26c9ffSDavid Dillow srp_map_desc(state, dma_addr, dma_len, target->rkey); 9708f26c9ffSDavid Dillow return 0; 971f5358a17SRoland Dreier } 972f5358a17SRoland Dreier 9738f26c9ffSDavid Dillow /* If we start at an offset into the FMR page, don't merge into 9748f26c9ffSDavid Dillow * the current FMR. Finish it out, and use the kernel's MR for this 9758f26c9ffSDavid Dillow * sg entry. This is to avoid potential bugs on some SRP targets 9768f26c9ffSDavid Dillow * that were never quite defined, but went away when the initiator 9778f26c9ffSDavid Dillow * avoided using FMR on such page fragments. 9788f26c9ffSDavid Dillow */ 9798f26c9ffSDavid Dillow if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) { 9808f26c9ffSDavid Dillow ret = srp_map_finish_fmr(state, target); 9818f26c9ffSDavid Dillow if (ret) 9828f26c9ffSDavid Dillow return ret; 9838f26c9ffSDavid Dillow 9848f26c9ffSDavid Dillow srp_map_desc(state, dma_addr, dma_len, target->rkey); 9858f26c9ffSDavid Dillow srp_map_update_start(state, NULL, 0, 0); 9868f26c9ffSDavid Dillow return 0; 987f5358a17SRoland Dreier } 988f5358a17SRoland Dreier 9898f26c9ffSDavid Dillow /* If this is the first sg to go into the FMR, save our position. 9908f26c9ffSDavid Dillow * We need to know the first unmapped entry, its index, and the 9918f26c9ffSDavid Dillow * first unmapped address within that entry to be able to restart 9928f26c9ffSDavid Dillow * mapping after an error. 9938f26c9ffSDavid Dillow */ 9948f26c9ffSDavid Dillow if (!state->unmapped_sg) 9958f26c9ffSDavid Dillow srp_map_update_start(state, sg, sg_index, dma_addr); 996f5358a17SRoland Dreier 9978f26c9ffSDavid Dillow while (dma_len) { 9988f26c9ffSDavid Dillow if (state->npages == SRP_FMR_SIZE) { 9998f26c9ffSDavid Dillow ret = srp_map_finish_fmr(state, target); 10008f26c9ffSDavid Dillow if (ret) 10018f26c9ffSDavid Dillow return ret; 1002f5358a17SRoland Dreier 10038f26c9ffSDavid Dillow srp_map_update_start(state, sg, sg_index, dma_addr); 100485507bccSRalph Campbell } 1005f5358a17SRoland Dreier 10068f26c9ffSDavid Dillow len = min_t(unsigned int, dma_len, dev->fmr_page_size); 10078f26c9ffSDavid Dillow 10088f26c9ffSDavid Dillow if (!state->npages) 10098f26c9ffSDavid Dillow state->base_dma_addr = dma_addr; 10108f26c9ffSDavid Dillow state->pages[state->npages++] = dma_addr; 10118f26c9ffSDavid Dillow state->fmr_len += len; 10128f26c9ffSDavid Dillow dma_addr += len; 10138f26c9ffSDavid Dillow dma_len -= len; 1014f5358a17SRoland Dreier } 1015f5358a17SRoland Dreier 10168f26c9ffSDavid Dillow /* If the last entry of the FMR wasn't a full page, then we need to 10178f26c9ffSDavid Dillow * close it out and start a new one -- we can only merge at page 10188f26c9ffSDavid Dillow * boundries. 10198f26c9ffSDavid Dillow */ 1020f5358a17SRoland Dreier ret = 0; 10218f26c9ffSDavid Dillow if (len != dev->fmr_page_size) { 10228f26c9ffSDavid Dillow ret = srp_map_finish_fmr(state, target); 10238f26c9ffSDavid Dillow if (!ret) 10248f26c9ffSDavid Dillow srp_map_update_start(state, NULL, 0, 0); 10258f26c9ffSDavid Dillow } 1026f5358a17SRoland Dreier return ret; 1027f5358a17SRoland Dreier } 1028f5358a17SRoland Dreier 1029aef9ec39SRoland Dreier static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target, 1030aef9ec39SRoland Dreier struct srp_request *req) 1031aef9ec39SRoland Dreier { 10328f26c9ffSDavid Dillow struct scatterlist *scat, *sg; 1033aef9ec39SRoland Dreier struct srp_cmd *cmd = req->cmd->buf; 10348f26c9ffSDavid Dillow int i, len, nents, count, use_fmr; 103585507bccSRalph Campbell struct srp_device *dev; 103685507bccSRalph Campbell struct ib_device *ibdev; 10378f26c9ffSDavid Dillow struct srp_map_state state; 10388f26c9ffSDavid Dillow struct srp_indirect_buf *indirect_hdr; 10398f26c9ffSDavid Dillow u32 table_len; 10408f26c9ffSDavid Dillow u8 fmt; 1041aef9ec39SRoland Dreier 1042bb350d1dSFUJITA Tomonori if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE) 1043aef9ec39SRoland Dreier return sizeof (struct srp_cmd); 1044aef9ec39SRoland Dreier 1045aef9ec39SRoland Dreier if (scmnd->sc_data_direction != DMA_FROM_DEVICE && 1046aef9ec39SRoland Dreier scmnd->sc_data_direction != DMA_TO_DEVICE) { 10477aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 10487aa54bd7SDavid Dillow PFX "Unhandled data direction %d\n", 1049aef9ec39SRoland Dreier scmnd->sc_data_direction); 1050aef9ec39SRoland Dreier return -EINVAL; 1051aef9ec39SRoland Dreier } 1052aef9ec39SRoland Dreier 1053bb350d1dSFUJITA Tomonori nents = scsi_sg_count(scmnd); 1054bb350d1dSFUJITA Tomonori scat = scsi_sglist(scmnd); 1055aef9ec39SRoland Dreier 105605321937SGreg Kroah-Hartman dev = target->srp_host->srp_dev; 105785507bccSRalph Campbell ibdev = dev->dev; 105885507bccSRalph Campbell 105985507bccSRalph Campbell count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction); 10608f26c9ffSDavid Dillow if (unlikely(count == 0)) 10618f26c9ffSDavid Dillow return -EIO; 1062aef9ec39SRoland Dreier 1063aef9ec39SRoland Dreier fmt = SRP_DATA_DESC_DIRECT; 1064f5358a17SRoland Dreier len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf); 1065f5358a17SRoland Dreier 1066f5358a17SRoland Dreier if (count == 1) { 1067f5358a17SRoland Dreier /* 1068f5358a17SRoland Dreier * The midlayer only generated a single gather/scatter 1069f5358a17SRoland Dreier * entry, or DMA mapping coalesced everything to a 1070f5358a17SRoland Dreier * single entry. So a direct descriptor along with 1071f5358a17SRoland Dreier * the DMA MR suffices. 1072f5358a17SRoland Dreier */ 1073f5358a17SRoland Dreier struct srp_direct_buf *buf = (void *) cmd->add_data; 1074aef9ec39SRoland Dreier 107585507bccSRalph Campbell buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat)); 10769af76271SDavid Dillow buf->key = cpu_to_be32(target->rkey); 107785507bccSRalph Campbell buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat)); 10788f26c9ffSDavid Dillow 10798f26c9ffSDavid Dillow req->nfmr = 0; 10808f26c9ffSDavid Dillow goto map_complete; 10818f26c9ffSDavid Dillow } 10828f26c9ffSDavid Dillow 10838f26c9ffSDavid Dillow /* We have more than one scatter/gather entry, so build our indirect 10848f26c9ffSDavid Dillow * descriptor table, trying to merge as many entries with FMR as we 10858f26c9ffSDavid Dillow * can. 1086f5358a17SRoland Dreier */ 10878f26c9ffSDavid Dillow indirect_hdr = (void *) cmd->add_data; 10888f26c9ffSDavid Dillow 1089c07d424dSDavid Dillow ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr, 1090c07d424dSDavid Dillow target->indirect_size, DMA_TO_DEVICE); 1091c07d424dSDavid Dillow 10928f26c9ffSDavid Dillow memset(&state, 0, sizeof(state)); 1093c07d424dSDavid Dillow state.desc = req->indirect_desc; 10948f26c9ffSDavid Dillow state.pages = req->map_page; 10958f26c9ffSDavid Dillow state.next_fmr = req->fmr_list; 10968f26c9ffSDavid Dillow 10978f26c9ffSDavid Dillow use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR; 10988f26c9ffSDavid Dillow 10998f26c9ffSDavid Dillow for_each_sg(scat, sg, count, i) { 11008f26c9ffSDavid Dillow if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) { 11018f26c9ffSDavid Dillow /* FMR mapping failed, so backtrack to the first 11028f26c9ffSDavid Dillow * unmapped entry and continue on without using FMR. 11038f26c9ffSDavid Dillow */ 11048f26c9ffSDavid Dillow dma_addr_t dma_addr; 11058f26c9ffSDavid Dillow unsigned int dma_len; 11068f26c9ffSDavid Dillow 11078f26c9ffSDavid Dillow backtrack: 11088f26c9ffSDavid Dillow sg = state.unmapped_sg; 11098f26c9ffSDavid Dillow i = state.unmapped_index; 11108f26c9ffSDavid Dillow 11118f26c9ffSDavid Dillow dma_addr = ib_sg_dma_address(ibdev, sg); 11128f26c9ffSDavid Dillow dma_len = ib_sg_dma_len(ibdev, sg); 11138f26c9ffSDavid Dillow dma_len -= (state.unmapped_addr - dma_addr); 11148f26c9ffSDavid Dillow dma_addr = state.unmapped_addr; 11158f26c9ffSDavid Dillow use_fmr = SRP_MAP_NO_FMR; 11168f26c9ffSDavid Dillow srp_map_desc(&state, dma_addr, dma_len, target->rkey); 11178f26c9ffSDavid Dillow } 11188f26c9ffSDavid Dillow } 11198f26c9ffSDavid Dillow 11208f26c9ffSDavid Dillow if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target)) 11218f26c9ffSDavid Dillow goto backtrack; 11228f26c9ffSDavid Dillow 1123c07d424dSDavid Dillow /* We've mapped the request, now pull as much of the indirect 1124c07d424dSDavid Dillow * descriptor table as we can into the command buffer. If this 1125c07d424dSDavid Dillow * target is not using an external indirect table, we are 1126c07d424dSDavid Dillow * guaranteed to fit into the command, as the SCSI layer won't 1127c07d424dSDavid Dillow * give us more S/G entries than we allow. 11288f26c9ffSDavid Dillow */ 11298f26c9ffSDavid Dillow req->nfmr = state.nfmr; 11308f26c9ffSDavid Dillow if (state.ndesc == 1) { 11318f26c9ffSDavid Dillow /* FMR mapping was able to collapse this to one entry, 11328f26c9ffSDavid Dillow * so use a direct descriptor. 11338f26c9ffSDavid Dillow */ 11348f26c9ffSDavid Dillow struct srp_direct_buf *buf = (void *) cmd->add_data; 11358f26c9ffSDavid Dillow 1136c07d424dSDavid Dillow *buf = req->indirect_desc[0]; 11378f26c9ffSDavid Dillow goto map_complete; 11388f26c9ffSDavid Dillow } 11398f26c9ffSDavid Dillow 1140c07d424dSDavid Dillow if (unlikely(target->cmd_sg_cnt < state.ndesc && 1141c07d424dSDavid Dillow !target->allow_ext_sg)) { 1142c07d424dSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 1143c07d424dSDavid Dillow "Could not fit S/G list into SRP_CMD\n"); 1144c07d424dSDavid Dillow return -EIO; 1145c07d424dSDavid Dillow } 1146c07d424dSDavid Dillow 1147c07d424dSDavid Dillow count = min(state.ndesc, target->cmd_sg_cnt); 11488f26c9ffSDavid Dillow table_len = state.ndesc * sizeof (struct srp_direct_buf); 1149aef9ec39SRoland Dreier 1150aef9ec39SRoland Dreier fmt = SRP_DATA_DESC_INDIRECT; 11518f26c9ffSDavid Dillow len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf); 1152c07d424dSDavid Dillow len += count * sizeof (struct srp_direct_buf); 1153f5358a17SRoland Dreier 1154c07d424dSDavid Dillow memcpy(indirect_hdr->desc_list, req->indirect_desc, 1155c07d424dSDavid Dillow count * sizeof (struct srp_direct_buf)); 115685507bccSRalph Campbell 1157c07d424dSDavid Dillow indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr); 11588f26c9ffSDavid Dillow indirect_hdr->table_desc.key = cpu_to_be32(target->rkey); 11598f26c9ffSDavid Dillow indirect_hdr->table_desc.len = cpu_to_be32(table_len); 11608f26c9ffSDavid Dillow indirect_hdr->len = cpu_to_be32(state.total_len); 1161aef9ec39SRoland Dreier 1162aef9ec39SRoland Dreier if (scmnd->sc_data_direction == DMA_TO_DEVICE) 1163c07d424dSDavid Dillow cmd->data_out_desc_cnt = count; 1164aef9ec39SRoland Dreier else 1165c07d424dSDavid Dillow cmd->data_in_desc_cnt = count; 1166c07d424dSDavid Dillow 1167c07d424dSDavid Dillow ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len, 1168c07d424dSDavid Dillow DMA_TO_DEVICE); 1169aef9ec39SRoland Dreier 11708f26c9ffSDavid Dillow map_complete: 1171aef9ec39SRoland Dreier if (scmnd->sc_data_direction == DMA_TO_DEVICE) 1172aef9ec39SRoland Dreier cmd->buf_fmt = fmt << 4; 1173aef9ec39SRoland Dreier else 1174aef9ec39SRoland Dreier cmd->buf_fmt = fmt; 1175aef9ec39SRoland Dreier 1176aef9ec39SRoland Dreier return len; 1177aef9ec39SRoland Dreier } 1178aef9ec39SRoland Dreier 117905a1d750SDavid Dillow /* 118076c75b25SBart Van Assche * Return an IU and possible credit to the free pool 118176c75b25SBart Van Assche */ 118276c75b25SBart Van Assche static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu, 118376c75b25SBart Van Assche enum srp_iu_type iu_type) 118476c75b25SBart Van Assche { 118576c75b25SBart Van Assche unsigned long flags; 118676c75b25SBart Van Assche 1187e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 118876c75b25SBart Van Assche list_add(&iu->list, &target->free_tx); 118976c75b25SBart Van Assche if (iu_type != SRP_IU_RSP) 119076c75b25SBart Van Assche ++target->req_lim; 1191e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 119276c75b25SBart Van Assche } 119376c75b25SBart Van Assche 119476c75b25SBart Van Assche /* 1195e9684678SBart Van Assche * Must be called with target->lock held to protect req_lim and free_tx. 1196e9684678SBart Van Assche * If IU is not sent, it must be returned using srp_put_tx_iu(). 119705a1d750SDavid Dillow * 119805a1d750SDavid Dillow * Note: 119905a1d750SDavid Dillow * An upper limit for the number of allocated information units for each 120005a1d750SDavid Dillow * request type is: 120105a1d750SDavid Dillow * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues 120205a1d750SDavid Dillow * more than Scsi_Host.can_queue requests. 120305a1d750SDavid Dillow * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE. 120405a1d750SDavid Dillow * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than 120505a1d750SDavid Dillow * one unanswered SRP request to an initiator. 120605a1d750SDavid Dillow */ 120705a1d750SDavid Dillow static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target, 120805a1d750SDavid Dillow enum srp_iu_type iu_type) 120905a1d750SDavid Dillow { 121005a1d750SDavid Dillow s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE; 121105a1d750SDavid Dillow struct srp_iu *iu; 121205a1d750SDavid Dillow 121305a1d750SDavid Dillow srp_send_completion(target->send_cq, target); 121405a1d750SDavid Dillow 1215dcb4cb85SBart Van Assche if (list_empty(&target->free_tx)) 121605a1d750SDavid Dillow return NULL; 121705a1d750SDavid Dillow 121805a1d750SDavid Dillow /* Initiator responses to target requests do not consume credits */ 121976c75b25SBart Van Assche if (iu_type != SRP_IU_RSP) { 122076c75b25SBart Van Assche if (target->req_lim <= rsv) { 122105a1d750SDavid Dillow ++target->zero_req_lim; 122205a1d750SDavid Dillow return NULL; 122305a1d750SDavid Dillow } 122405a1d750SDavid Dillow 122576c75b25SBart Van Assche --target->req_lim; 122676c75b25SBart Van Assche } 122776c75b25SBart Van Assche 1228dcb4cb85SBart Van Assche iu = list_first_entry(&target->free_tx, struct srp_iu, list); 122976c75b25SBart Van Assche list_del(&iu->list); 123005a1d750SDavid Dillow return iu; 123105a1d750SDavid Dillow } 123205a1d750SDavid Dillow 123376c75b25SBart Van Assche static int srp_post_send(struct srp_target_port *target, 123405a1d750SDavid Dillow struct srp_iu *iu, int len) 123505a1d750SDavid Dillow { 123605a1d750SDavid Dillow struct ib_sge list; 123705a1d750SDavid Dillow struct ib_send_wr wr, *bad_wr; 123805a1d750SDavid Dillow 123905a1d750SDavid Dillow list.addr = iu->dma; 124005a1d750SDavid Dillow list.length = len; 12419af76271SDavid Dillow list.lkey = target->lkey; 124205a1d750SDavid Dillow 124305a1d750SDavid Dillow wr.next = NULL; 1244dcb4cb85SBart Van Assche wr.wr_id = (uintptr_t) iu; 124505a1d750SDavid Dillow wr.sg_list = &list; 124605a1d750SDavid Dillow wr.num_sge = 1; 124705a1d750SDavid Dillow wr.opcode = IB_WR_SEND; 124805a1d750SDavid Dillow wr.send_flags = IB_SEND_SIGNALED; 124905a1d750SDavid Dillow 125076c75b25SBart Van Assche return ib_post_send(target->qp, &wr, &bad_wr); 125105a1d750SDavid Dillow } 125205a1d750SDavid Dillow 1253dcb4cb85SBart Van Assche static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu) 1254c996bb47SBart Van Assche { 1255c996bb47SBart Van Assche struct ib_recv_wr wr, *bad_wr; 1256dcb4cb85SBart Van Assche struct ib_sge list; 1257c996bb47SBart Van Assche 1258c996bb47SBart Van Assche list.addr = iu->dma; 1259c996bb47SBart Van Assche list.length = iu->size; 12609af76271SDavid Dillow list.lkey = target->lkey; 1261c996bb47SBart Van Assche 1262c996bb47SBart Van Assche wr.next = NULL; 1263dcb4cb85SBart Van Assche wr.wr_id = (uintptr_t) iu; 1264c996bb47SBart Van Assche wr.sg_list = &list; 1265c996bb47SBart Van Assche wr.num_sge = 1; 1266c996bb47SBart Van Assche 1267dcb4cb85SBart Van Assche return ib_post_recv(target->qp, &wr, &bad_wr); 1268c996bb47SBart Van Assche } 1269c996bb47SBart Van Assche 1270aef9ec39SRoland Dreier static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp) 1271aef9ec39SRoland Dreier { 1272aef9ec39SRoland Dreier struct srp_request *req; 1273aef9ec39SRoland Dreier struct scsi_cmnd *scmnd; 1274aef9ec39SRoland Dreier unsigned long flags; 1275aef9ec39SRoland Dreier 1276aef9ec39SRoland Dreier if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) { 1277e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 127894a9174cSBart Van Assche target->req_lim += be32_to_cpu(rsp->req_lim_delta); 1279e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 128094a9174cSBart Van Assche 1281f8b6e31eSDavid Dillow target->tsk_mgmt_status = -1; 1282f8b6e31eSDavid Dillow if (be32_to_cpu(rsp->resp_data_len) >= 4) 1283f8b6e31eSDavid Dillow target->tsk_mgmt_status = rsp->data[3]; 1284f8b6e31eSDavid Dillow complete(&target->tsk_mgmt_done); 1285aef9ec39SRoland Dreier } else { 1286f8b6e31eSDavid Dillow req = &target->req_ring[rsp->tag]; 128722032991SBart Van Assche scmnd = srp_claim_req(target, req, NULL); 128822032991SBart Van Assche if (!scmnd) { 12897aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 12907aa54bd7SDavid Dillow "Null scmnd for RSP w/tag %016llx\n", 1291aef9ec39SRoland Dreier (unsigned long long) rsp->tag); 129222032991SBart Van Assche 129322032991SBart Van Assche spin_lock_irqsave(&target->lock, flags); 129422032991SBart Van Assche target->req_lim += be32_to_cpu(rsp->req_lim_delta); 129522032991SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 129622032991SBart Van Assche 129722032991SBart Van Assche return; 129822032991SBart Van Assche } 1299aef9ec39SRoland Dreier scmnd->result = rsp->status; 1300aef9ec39SRoland Dreier 1301aef9ec39SRoland Dreier if (rsp->flags & SRP_RSP_FLAG_SNSVALID) { 1302aef9ec39SRoland Dreier memcpy(scmnd->sense_buffer, rsp->data + 1303aef9ec39SRoland Dreier be32_to_cpu(rsp->resp_data_len), 1304aef9ec39SRoland Dreier min_t(int, be32_to_cpu(rsp->sense_data_len), 1305aef9ec39SRoland Dreier SCSI_SENSE_BUFFERSIZE)); 1306aef9ec39SRoland Dreier } 1307aef9ec39SRoland Dreier 1308aef9ec39SRoland Dreier if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER)) 1309bb350d1dSFUJITA Tomonori scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt)); 1310aef9ec39SRoland Dreier else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER)) 1311bb350d1dSFUJITA Tomonori scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt)); 1312aef9ec39SRoland Dreier 131322032991SBart Van Assche srp_free_req(target, req, scmnd, 131422032991SBart Van Assche be32_to_cpu(rsp->req_lim_delta)); 131522032991SBart Van Assche 1316f8b6e31eSDavid Dillow scmnd->host_scribble = NULL; 1317aef9ec39SRoland Dreier scmnd->scsi_done(scmnd); 1318aef9ec39SRoland Dreier } 1319aef9ec39SRoland Dreier } 1320aef9ec39SRoland Dreier 1321bb12588aSDavid Dillow static int srp_response_common(struct srp_target_port *target, s32 req_delta, 1322bb12588aSDavid Dillow void *rsp, int len) 1323bb12588aSDavid Dillow { 132476c75b25SBart Van Assche struct ib_device *dev = target->srp_host->srp_dev->dev; 1325bb12588aSDavid Dillow unsigned long flags; 1326bb12588aSDavid Dillow struct srp_iu *iu; 132776c75b25SBart Van Assche int err; 1328bb12588aSDavid Dillow 1329e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 1330bb12588aSDavid Dillow target->req_lim += req_delta; 1331bb12588aSDavid Dillow iu = __srp_get_tx_iu(target, SRP_IU_RSP); 1332e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 133376c75b25SBart Van Assche 1334bb12588aSDavid Dillow if (!iu) { 1335bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 1336bb12588aSDavid Dillow "no IU available to send response\n"); 133776c75b25SBart Van Assche return 1; 1338bb12588aSDavid Dillow } 1339bb12588aSDavid Dillow 1340bb12588aSDavid Dillow ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE); 1341bb12588aSDavid Dillow memcpy(iu->buf, rsp, len); 1342bb12588aSDavid Dillow ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE); 1343bb12588aSDavid Dillow 134476c75b25SBart Van Assche err = srp_post_send(target, iu, len); 134576c75b25SBart Van Assche if (err) { 1346bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 1347bb12588aSDavid Dillow "unable to post response: %d\n", err); 134876c75b25SBart Van Assche srp_put_tx_iu(target, iu, SRP_IU_RSP); 134976c75b25SBart Van Assche } 1350bb12588aSDavid Dillow 1351bb12588aSDavid Dillow return err; 1352bb12588aSDavid Dillow } 1353bb12588aSDavid Dillow 1354bb12588aSDavid Dillow static void srp_process_cred_req(struct srp_target_port *target, 1355bb12588aSDavid Dillow struct srp_cred_req *req) 1356bb12588aSDavid Dillow { 1357bb12588aSDavid Dillow struct srp_cred_rsp rsp = { 1358bb12588aSDavid Dillow .opcode = SRP_CRED_RSP, 1359bb12588aSDavid Dillow .tag = req->tag, 1360bb12588aSDavid Dillow }; 1361bb12588aSDavid Dillow s32 delta = be32_to_cpu(req->req_lim_delta); 1362bb12588aSDavid Dillow 1363bb12588aSDavid Dillow if (srp_response_common(target, delta, &rsp, sizeof rsp)) 1364bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 1365bb12588aSDavid Dillow "problems processing SRP_CRED_REQ\n"); 1366bb12588aSDavid Dillow } 1367bb12588aSDavid Dillow 1368bb12588aSDavid Dillow static void srp_process_aer_req(struct srp_target_port *target, 1369bb12588aSDavid Dillow struct srp_aer_req *req) 1370bb12588aSDavid Dillow { 1371bb12588aSDavid Dillow struct srp_aer_rsp rsp = { 1372bb12588aSDavid Dillow .opcode = SRP_AER_RSP, 1373bb12588aSDavid Dillow .tag = req->tag, 1374bb12588aSDavid Dillow }; 1375bb12588aSDavid Dillow s32 delta = be32_to_cpu(req->req_lim_delta); 1376bb12588aSDavid Dillow 1377bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 1378bb12588aSDavid Dillow "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun)); 1379bb12588aSDavid Dillow 1380bb12588aSDavid Dillow if (srp_response_common(target, delta, &rsp, sizeof rsp)) 1381bb12588aSDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX 1382bb12588aSDavid Dillow "problems processing SRP_AER_REQ\n"); 1383bb12588aSDavid Dillow } 1384bb12588aSDavid Dillow 1385aef9ec39SRoland Dreier static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc) 1386aef9ec39SRoland Dreier { 1387dcb4cb85SBart Van Assche struct ib_device *dev = target->srp_host->srp_dev->dev; 1388737b94ebSRoland Dreier struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id; 1389c996bb47SBart Van Assche int res; 1390aef9ec39SRoland Dreier u8 opcode; 1391aef9ec39SRoland Dreier 139285507bccSRalph Campbell ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len, 139385507bccSRalph Campbell DMA_FROM_DEVICE); 1394aef9ec39SRoland Dreier 1395aef9ec39SRoland Dreier opcode = *(u8 *) iu->buf; 1396aef9ec39SRoland Dreier 1397aef9ec39SRoland Dreier if (0) { 13987aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 13997aa54bd7SDavid Dillow PFX "recv completion, opcode 0x%02x\n", opcode); 14007a700811SBart Van Assche print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1, 14017a700811SBart Van Assche iu->buf, wc->byte_len, true); 1402aef9ec39SRoland Dreier } 1403aef9ec39SRoland Dreier 1404aef9ec39SRoland Dreier switch (opcode) { 1405aef9ec39SRoland Dreier case SRP_RSP: 1406aef9ec39SRoland Dreier srp_process_rsp(target, iu->buf); 1407aef9ec39SRoland Dreier break; 1408aef9ec39SRoland Dreier 1409bb12588aSDavid Dillow case SRP_CRED_REQ: 1410bb12588aSDavid Dillow srp_process_cred_req(target, iu->buf); 1411bb12588aSDavid Dillow break; 1412bb12588aSDavid Dillow 1413bb12588aSDavid Dillow case SRP_AER_REQ: 1414bb12588aSDavid Dillow srp_process_aer_req(target, iu->buf); 1415bb12588aSDavid Dillow break; 1416bb12588aSDavid Dillow 1417aef9ec39SRoland Dreier case SRP_T_LOGOUT: 1418aef9ec39SRoland Dreier /* XXX Handle target logout */ 14197aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 14207aa54bd7SDavid Dillow PFX "Got target logout request\n"); 1421aef9ec39SRoland Dreier break; 1422aef9ec39SRoland Dreier 1423aef9ec39SRoland Dreier default: 14247aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 14257aa54bd7SDavid Dillow PFX "Unhandled SRP opcode 0x%02x\n", opcode); 1426aef9ec39SRoland Dreier break; 1427aef9ec39SRoland Dreier } 1428aef9ec39SRoland Dreier 142985507bccSRalph Campbell ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len, 143085507bccSRalph Campbell DMA_FROM_DEVICE); 1431c996bb47SBart Van Assche 1432dcb4cb85SBart Van Assche res = srp_post_recv(target, iu); 1433c996bb47SBart Van Assche if (res != 0) 1434c996bb47SBart Van Assche shost_printk(KERN_ERR, target->scsi_host, 1435c996bb47SBart Van Assche PFX "Recv failed with error code %d\n", res); 1436aef9ec39SRoland Dreier } 1437aef9ec39SRoland Dreier 1438c1120f89SBart Van Assche /** 1439c1120f89SBart Van Assche * srp_tl_err_work() - handle a transport layer error 1440c1120f89SBart Van Assche * 1441c1120f89SBart Van Assche * Note: This function may get invoked before the rport has been created, 1442c1120f89SBart Van Assche * hence the target->rport test. 1443c1120f89SBart Van Assche */ 1444c1120f89SBart Van Assche static void srp_tl_err_work(struct work_struct *work) 1445c1120f89SBart Van Assche { 1446c1120f89SBart Van Assche struct srp_target_port *target; 1447c1120f89SBart Van Assche 1448c1120f89SBart Van Assche target = container_of(work, struct srp_target_port, tl_err_work); 1449c1120f89SBart Van Assche if (target->rport) 1450c1120f89SBart Van Assche srp_start_tl_fail_timers(target->rport); 1451c1120f89SBart Van Assche } 1452c1120f89SBart Van Assche 1453cd4e3854SBart Van Assche static void srp_handle_qp_err(enum ib_wc_status wc_status, bool send_err, 1454948d1e88SBart Van Assche struct srp_target_port *target) 1455948d1e88SBart Van Assche { 1456294c875aSBart Van Assche if (target->connected && !target->qp_in_error) { 14574f0af697SBart Van Assche shost_printk(KERN_ERR, target->scsi_host, 14584f0af697SBart Van Assche PFX "failed %s status %d\n", 1459cd4e3854SBart Van Assche send_err ? "send" : "receive", 14604f0af697SBart Van Assche wc_status); 1461c1120f89SBart Van Assche queue_work(system_long_wq, &target->tl_err_work); 14624f0af697SBart Van Assche } 1463948d1e88SBart Van Assche target->qp_in_error = true; 1464948d1e88SBart Van Assche } 1465948d1e88SBart Van Assche 14669c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr) 1467aef9ec39SRoland Dreier { 1468aef9ec39SRoland Dreier struct srp_target_port *target = target_ptr; 1469aef9ec39SRoland Dreier struct ib_wc wc; 1470aef9ec39SRoland Dreier 1471aef9ec39SRoland Dreier ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); 1472aef9ec39SRoland Dreier while (ib_poll_cq(cq, 1, &wc) > 0) { 1473948d1e88SBart Van Assche if (likely(wc.status == IB_WC_SUCCESS)) { 1474948d1e88SBart Van Assche srp_handle_recv(target, &wc); 1475948d1e88SBart Van Assche } else { 1476cd4e3854SBart Van Assche srp_handle_qp_err(wc.status, false, target); 1477aef9ec39SRoland Dreier } 14789c03dc9fSBart Van Assche } 14799c03dc9fSBart Van Assche } 14809c03dc9fSBart Van Assche 14819c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr) 14829c03dc9fSBart Van Assche { 14839c03dc9fSBart Van Assche struct srp_target_port *target = target_ptr; 14849c03dc9fSBart Van Assche struct ib_wc wc; 1485dcb4cb85SBart Van Assche struct srp_iu *iu; 14869c03dc9fSBart Van Assche 14879c03dc9fSBart Van Assche while (ib_poll_cq(cq, 1, &wc) > 0) { 1488948d1e88SBart Van Assche if (likely(wc.status == IB_WC_SUCCESS)) { 1489737b94ebSRoland Dreier iu = (struct srp_iu *) (uintptr_t) wc.wr_id; 1490dcb4cb85SBart Van Assche list_add(&iu->list, &target->free_tx); 1491948d1e88SBart Van Assche } else { 1492cd4e3854SBart Van Assche srp_handle_qp_err(wc.status, true, target); 1493948d1e88SBart Van Assche } 1494aef9ec39SRoland Dreier } 1495aef9ec39SRoland Dreier } 1496aef9ec39SRoland Dreier 149776c75b25SBart Van Assche static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd) 1498aef9ec39SRoland Dreier { 149976c75b25SBart Van Assche struct srp_target_port *target = host_to_target(shost); 1500a95cadb9SBart Van Assche struct srp_rport *rport = target->rport; 1501aef9ec39SRoland Dreier struct srp_request *req; 1502aef9ec39SRoland Dreier struct srp_iu *iu; 1503aef9ec39SRoland Dreier struct srp_cmd *cmd; 150485507bccSRalph Campbell struct ib_device *dev; 150576c75b25SBart Van Assche unsigned long flags; 1506ed9b2264SBart Van Assche int len, result; 1507a95cadb9SBart Van Assche const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler; 1508a95cadb9SBart Van Assche 1509a95cadb9SBart Van Assche /* 1510a95cadb9SBart Van Assche * The SCSI EH thread is the only context from which srp_queuecommand() 1511a95cadb9SBart Van Assche * can get invoked for blocked devices (SDEV_BLOCK / 1512a95cadb9SBart Van Assche * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by 1513a95cadb9SBart Van Assche * locking the rport mutex if invoked from inside the SCSI EH. 1514a95cadb9SBart Van Assche */ 1515a95cadb9SBart Van Assche if (in_scsi_eh) 1516a95cadb9SBart Van Assche mutex_lock(&rport->mutex); 1517aef9ec39SRoland Dreier 1518ed9b2264SBart Van Assche result = srp_chkready(target->rport); 1519ed9b2264SBart Van Assche if (unlikely(result)) { 1520ed9b2264SBart Van Assche scmnd->result = result; 15212ce19e72SBart Van Assche scmnd->scsi_done(scmnd); 1522a95cadb9SBart Van Assche goto unlock_rport; 15232ce19e72SBart Van Assche } 15242ce19e72SBart Van Assche 1525e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 1526bb12588aSDavid Dillow iu = __srp_get_tx_iu(target, SRP_IU_CMD); 1527aef9ec39SRoland Dreier if (!iu) 1528695b8349SBart Van Assche goto err_unlock; 1529695b8349SBart Van Assche 1530695b8349SBart Van Assche req = list_first_entry(&target->free_reqs, struct srp_request, list); 1531695b8349SBart Van Assche list_del(&req->list); 1532695b8349SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 1533aef9ec39SRoland Dreier 153405321937SGreg Kroah-Hartman dev = target->srp_host->srp_dev->dev; 153549248644SDavid Dillow ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len, 153685507bccSRalph Campbell DMA_TO_DEVICE); 1537aef9ec39SRoland Dreier 1538aef9ec39SRoland Dreier scmnd->result = 0; 1539f8b6e31eSDavid Dillow scmnd->host_scribble = (void *) req; 1540aef9ec39SRoland Dreier 1541aef9ec39SRoland Dreier cmd = iu->buf; 1542aef9ec39SRoland Dreier memset(cmd, 0, sizeof *cmd); 1543aef9ec39SRoland Dreier 1544aef9ec39SRoland Dreier cmd->opcode = SRP_CMD; 1545aef9ec39SRoland Dreier cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48); 1546d945e1dfSRoland Dreier cmd->tag = req->index; 1547aef9ec39SRoland Dreier memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len); 1548aef9ec39SRoland Dreier 1549aef9ec39SRoland Dreier req->scmnd = scmnd; 1550aef9ec39SRoland Dreier req->cmd = iu; 1551aef9ec39SRoland Dreier 1552aef9ec39SRoland Dreier len = srp_map_data(scmnd, target, req); 1553aef9ec39SRoland Dreier if (len < 0) { 15547aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 15557aa54bd7SDavid Dillow PFX "Failed to map data\n"); 155676c75b25SBart Van Assche goto err_iu; 1557aef9ec39SRoland Dreier } 1558aef9ec39SRoland Dreier 155949248644SDavid Dillow ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len, 156085507bccSRalph Campbell DMA_TO_DEVICE); 1561aef9ec39SRoland Dreier 156276c75b25SBart Van Assche if (srp_post_send(target, iu, len)) { 15637aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n"); 1564aef9ec39SRoland Dreier goto err_unmap; 1565aef9ec39SRoland Dreier } 1566aef9ec39SRoland Dreier 1567a95cadb9SBart Van Assche unlock_rport: 1568a95cadb9SBart Van Assche if (in_scsi_eh) 1569a95cadb9SBart Van Assche mutex_unlock(&rport->mutex); 1570a95cadb9SBart Van Assche 1571aef9ec39SRoland Dreier return 0; 1572aef9ec39SRoland Dreier 1573aef9ec39SRoland Dreier err_unmap: 1574aef9ec39SRoland Dreier srp_unmap_data(scmnd, target, req); 1575aef9ec39SRoland Dreier 157676c75b25SBart Van Assche err_iu: 157776c75b25SBart Van Assche srp_put_tx_iu(target, iu, SRP_IU_CMD); 157876c75b25SBart Van Assche 1579e9684678SBart Van Assche spin_lock_irqsave(&target->lock, flags); 158076c75b25SBart Van Assche list_add(&req->list, &target->free_reqs); 1581695b8349SBart Van Assche 1582695b8349SBart Van Assche err_unlock: 1583e9684678SBart Van Assche spin_unlock_irqrestore(&target->lock, flags); 158476c75b25SBart Van Assche 1585a95cadb9SBart Van Assche if (in_scsi_eh) 1586a95cadb9SBart Van Assche mutex_unlock(&rport->mutex); 1587a95cadb9SBart Van Assche 1588aef9ec39SRoland Dreier return SCSI_MLQUEUE_HOST_BUSY; 1589aef9ec39SRoland Dreier } 1590aef9ec39SRoland Dreier 15914d73f95fSBart Van Assche /* 15924d73f95fSBart Van Assche * Note: the resources allocated in this function are freed in 15934d73f95fSBart Van Assche * srp_free_target_ib(). 15944d73f95fSBart Van Assche */ 1595aef9ec39SRoland Dreier static int srp_alloc_iu_bufs(struct srp_target_port *target) 1596aef9ec39SRoland Dreier { 1597aef9ec39SRoland Dreier int i; 1598aef9ec39SRoland Dreier 15994d73f95fSBart Van Assche target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring), 16004d73f95fSBart Van Assche GFP_KERNEL); 16014d73f95fSBart Van Assche if (!target->rx_ring) 16024d73f95fSBart Van Assche goto err_no_ring; 16034d73f95fSBart Van Assche target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring), 16044d73f95fSBart Van Assche GFP_KERNEL); 16054d73f95fSBart Van Assche if (!target->tx_ring) 16064d73f95fSBart Van Assche goto err_no_ring; 16074d73f95fSBart Van Assche 16084d73f95fSBart Van Assche for (i = 0; i < target->queue_size; ++i) { 1609aef9ec39SRoland Dreier target->rx_ring[i] = srp_alloc_iu(target->srp_host, 1610aef9ec39SRoland Dreier target->max_ti_iu_len, 1611aef9ec39SRoland Dreier GFP_KERNEL, DMA_FROM_DEVICE); 1612aef9ec39SRoland Dreier if (!target->rx_ring[i]) 1613aef9ec39SRoland Dreier goto err; 1614aef9ec39SRoland Dreier } 1615aef9ec39SRoland Dreier 16164d73f95fSBart Van Assche for (i = 0; i < target->queue_size; ++i) { 1617aef9ec39SRoland Dreier target->tx_ring[i] = srp_alloc_iu(target->srp_host, 161849248644SDavid Dillow target->max_iu_len, 1619aef9ec39SRoland Dreier GFP_KERNEL, DMA_TO_DEVICE); 1620aef9ec39SRoland Dreier if (!target->tx_ring[i]) 1621aef9ec39SRoland Dreier goto err; 1622dcb4cb85SBart Van Assche 1623dcb4cb85SBart Van Assche list_add(&target->tx_ring[i]->list, &target->free_tx); 1624aef9ec39SRoland Dreier } 1625aef9ec39SRoland Dreier 1626aef9ec39SRoland Dreier return 0; 1627aef9ec39SRoland Dreier 1628aef9ec39SRoland Dreier err: 16294d73f95fSBart Van Assche for (i = 0; i < target->queue_size; ++i) { 1630aef9ec39SRoland Dreier srp_free_iu(target->srp_host, target->rx_ring[i]); 16314d73f95fSBart Van Assche srp_free_iu(target->srp_host, target->tx_ring[i]); 1632aef9ec39SRoland Dreier } 1633aef9ec39SRoland Dreier 16344d73f95fSBart Van Assche 16354d73f95fSBart Van Assche err_no_ring: 16364d73f95fSBart Van Assche kfree(target->tx_ring); 16374d73f95fSBart Van Assche target->tx_ring = NULL; 16384d73f95fSBart Van Assche kfree(target->rx_ring); 16394d73f95fSBart Van Assche target->rx_ring = NULL; 1640aef9ec39SRoland Dreier 1641aef9ec39SRoland Dreier return -ENOMEM; 1642aef9ec39SRoland Dreier } 1643aef9ec39SRoland Dreier 1644c9b03c1aSBart Van Assche static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask) 1645c9b03c1aSBart Van Assche { 1646c9b03c1aSBart Van Assche uint64_t T_tr_ns, max_compl_time_ms; 1647c9b03c1aSBart Van Assche uint32_t rq_tmo_jiffies; 1648c9b03c1aSBart Van Assche 1649c9b03c1aSBart Van Assche /* 1650c9b03c1aSBart Van Assche * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair, 1651c9b03c1aSBart Van Assche * table 91), both the QP timeout and the retry count have to be set 1652c9b03c1aSBart Van Assche * for RC QP's during the RTR to RTS transition. 1653c9b03c1aSBart Van Assche */ 1654c9b03c1aSBart Van Assche WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) != 1655c9b03c1aSBart Van Assche (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)); 1656c9b03c1aSBart Van Assche 1657c9b03c1aSBart Van Assche /* 1658c9b03c1aSBart Van Assche * Set target->rq_tmo_jiffies to one second more than the largest time 1659c9b03c1aSBart Van Assche * it can take before an error completion is generated. See also 1660c9b03c1aSBart Van Assche * C9-140..142 in the IBTA spec for more information about how to 1661c9b03c1aSBart Van Assche * convert the QP Local ACK Timeout value to nanoseconds. 1662c9b03c1aSBart Van Assche */ 1663c9b03c1aSBart Van Assche T_tr_ns = 4096 * (1ULL << qp_attr->timeout); 1664c9b03c1aSBart Van Assche max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns; 1665c9b03c1aSBart Van Assche do_div(max_compl_time_ms, NSEC_PER_MSEC); 1666c9b03c1aSBart Van Assche rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000); 1667c9b03c1aSBart Van Assche 1668c9b03c1aSBart Van Assche return rq_tmo_jiffies; 1669c9b03c1aSBart Van Assche } 1670c9b03c1aSBart Van Assche 1671961e0be8SDavid Dillow static void srp_cm_rep_handler(struct ib_cm_id *cm_id, 1672961e0be8SDavid Dillow struct srp_login_rsp *lrsp, 1673961e0be8SDavid Dillow struct srp_target_port *target) 1674961e0be8SDavid Dillow { 1675961e0be8SDavid Dillow struct ib_qp_attr *qp_attr = NULL; 1676961e0be8SDavid Dillow int attr_mask = 0; 1677961e0be8SDavid Dillow int ret; 1678961e0be8SDavid Dillow int i; 1679961e0be8SDavid Dillow 1680961e0be8SDavid Dillow if (lrsp->opcode == SRP_LOGIN_RSP) { 1681961e0be8SDavid Dillow target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len); 1682961e0be8SDavid Dillow target->req_lim = be32_to_cpu(lrsp->req_lim_delta); 1683961e0be8SDavid Dillow 1684961e0be8SDavid Dillow /* 1685961e0be8SDavid Dillow * Reserve credits for task management so we don't 1686961e0be8SDavid Dillow * bounce requests back to the SCSI mid-layer. 1687961e0be8SDavid Dillow */ 1688961e0be8SDavid Dillow target->scsi_host->can_queue 1689961e0be8SDavid Dillow = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE, 1690961e0be8SDavid Dillow target->scsi_host->can_queue); 16914d73f95fSBart Van Assche target->scsi_host->cmd_per_lun 16924d73f95fSBart Van Assche = min_t(int, target->scsi_host->can_queue, 16934d73f95fSBart Van Assche target->scsi_host->cmd_per_lun); 1694961e0be8SDavid Dillow } else { 1695961e0be8SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 1696961e0be8SDavid Dillow PFX "Unhandled RSP opcode %#x\n", lrsp->opcode); 1697961e0be8SDavid Dillow ret = -ECONNRESET; 1698961e0be8SDavid Dillow goto error; 1699961e0be8SDavid Dillow } 1700961e0be8SDavid Dillow 17014d73f95fSBart Van Assche if (!target->rx_ring) { 1702961e0be8SDavid Dillow ret = srp_alloc_iu_bufs(target); 1703961e0be8SDavid Dillow if (ret) 1704961e0be8SDavid Dillow goto error; 1705961e0be8SDavid Dillow } 1706961e0be8SDavid Dillow 1707961e0be8SDavid Dillow ret = -ENOMEM; 1708961e0be8SDavid Dillow qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL); 1709961e0be8SDavid Dillow if (!qp_attr) 1710961e0be8SDavid Dillow goto error; 1711961e0be8SDavid Dillow 1712961e0be8SDavid Dillow qp_attr->qp_state = IB_QPS_RTR; 1713961e0be8SDavid Dillow ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); 1714961e0be8SDavid Dillow if (ret) 1715961e0be8SDavid Dillow goto error_free; 1716961e0be8SDavid Dillow 1717961e0be8SDavid Dillow ret = ib_modify_qp(target->qp, qp_attr, attr_mask); 1718961e0be8SDavid Dillow if (ret) 1719961e0be8SDavid Dillow goto error_free; 1720961e0be8SDavid Dillow 17214d73f95fSBart Van Assche for (i = 0; i < target->queue_size; i++) { 1722961e0be8SDavid Dillow struct srp_iu *iu = target->rx_ring[i]; 1723961e0be8SDavid Dillow ret = srp_post_recv(target, iu); 1724961e0be8SDavid Dillow if (ret) 1725961e0be8SDavid Dillow goto error_free; 1726961e0be8SDavid Dillow } 1727961e0be8SDavid Dillow 1728961e0be8SDavid Dillow qp_attr->qp_state = IB_QPS_RTS; 1729961e0be8SDavid Dillow ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); 1730961e0be8SDavid Dillow if (ret) 1731961e0be8SDavid Dillow goto error_free; 1732961e0be8SDavid Dillow 1733c9b03c1aSBart Van Assche target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask); 1734c9b03c1aSBart Van Assche 1735961e0be8SDavid Dillow ret = ib_modify_qp(target->qp, qp_attr, attr_mask); 1736961e0be8SDavid Dillow if (ret) 1737961e0be8SDavid Dillow goto error_free; 1738961e0be8SDavid Dillow 1739961e0be8SDavid Dillow ret = ib_send_cm_rtu(cm_id, NULL, 0); 1740961e0be8SDavid Dillow 1741961e0be8SDavid Dillow error_free: 1742961e0be8SDavid Dillow kfree(qp_attr); 1743961e0be8SDavid Dillow 1744961e0be8SDavid Dillow error: 1745961e0be8SDavid Dillow target->status = ret; 1746961e0be8SDavid Dillow } 1747961e0be8SDavid Dillow 1748aef9ec39SRoland Dreier static void srp_cm_rej_handler(struct ib_cm_id *cm_id, 1749aef9ec39SRoland Dreier struct ib_cm_event *event, 1750aef9ec39SRoland Dreier struct srp_target_port *target) 1751aef9ec39SRoland Dreier { 17527aa54bd7SDavid Dillow struct Scsi_Host *shost = target->scsi_host; 1753aef9ec39SRoland Dreier struct ib_class_port_info *cpi; 1754aef9ec39SRoland Dreier int opcode; 1755aef9ec39SRoland Dreier 1756aef9ec39SRoland Dreier switch (event->param.rej_rcvd.reason) { 1757aef9ec39SRoland Dreier case IB_CM_REJ_PORT_CM_REDIRECT: 1758aef9ec39SRoland Dreier cpi = event->param.rej_rcvd.ari; 1759aef9ec39SRoland Dreier target->path.dlid = cpi->redirect_lid; 1760aef9ec39SRoland Dreier target->path.pkey = cpi->redirect_pkey; 1761aef9ec39SRoland Dreier cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff; 1762aef9ec39SRoland Dreier memcpy(target->path.dgid.raw, cpi->redirect_gid, 16); 1763aef9ec39SRoland Dreier 1764aef9ec39SRoland Dreier target->status = target->path.dlid ? 1765aef9ec39SRoland Dreier SRP_DLID_REDIRECT : SRP_PORT_REDIRECT; 1766aef9ec39SRoland Dreier break; 1767aef9ec39SRoland Dreier 1768aef9ec39SRoland Dreier case IB_CM_REJ_PORT_REDIRECT: 17695d7cbfd6SRoland Dreier if (srp_target_is_topspin(target)) { 1770aef9ec39SRoland Dreier /* 1771aef9ec39SRoland Dreier * Topspin/Cisco SRP gateways incorrectly send 1772aef9ec39SRoland Dreier * reject reason code 25 when they mean 24 1773aef9ec39SRoland Dreier * (port redirect). 1774aef9ec39SRoland Dreier */ 1775aef9ec39SRoland Dreier memcpy(target->path.dgid.raw, 1776aef9ec39SRoland Dreier event->param.rej_rcvd.ari, 16); 1777aef9ec39SRoland Dreier 17787aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, shost, 17797aa54bd7SDavid Dillow PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n", 1780aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix), 1781aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id)); 1782aef9ec39SRoland Dreier 1783aef9ec39SRoland Dreier target->status = SRP_PORT_REDIRECT; 1784aef9ec39SRoland Dreier } else { 17857aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 17867aa54bd7SDavid Dillow " REJ reason: IB_CM_REJ_PORT_REDIRECT\n"); 1787aef9ec39SRoland Dreier target->status = -ECONNRESET; 1788aef9ec39SRoland Dreier } 1789aef9ec39SRoland Dreier break; 1790aef9ec39SRoland Dreier 1791aef9ec39SRoland Dreier case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: 17927aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 17937aa54bd7SDavid Dillow " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); 1794aef9ec39SRoland Dreier target->status = -ECONNRESET; 1795aef9ec39SRoland Dreier break; 1796aef9ec39SRoland Dreier 1797aef9ec39SRoland Dreier case IB_CM_REJ_CONSUMER_DEFINED: 1798aef9ec39SRoland Dreier opcode = *(u8 *) event->private_data; 1799aef9ec39SRoland Dreier if (opcode == SRP_LOGIN_REJ) { 1800aef9ec39SRoland Dreier struct srp_login_rej *rej = event->private_data; 1801aef9ec39SRoland Dreier u32 reason = be32_to_cpu(rej->reason); 1802aef9ec39SRoland Dreier 1803aef9ec39SRoland Dreier if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) 18047aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 18057aa54bd7SDavid Dillow PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); 1806aef9ec39SRoland Dreier else 1807*e7ffde01SBart Van Assche shost_printk(KERN_WARNING, shost, PFX 1808*e7ffde01SBart Van Assche "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n", 1809*e7ffde01SBart Van Assche target->path.sgid.raw, 1810*e7ffde01SBart Van Assche target->orig_dgid, reason); 1811aef9ec39SRoland Dreier } else 18127aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, 18137aa54bd7SDavid Dillow " REJ reason: IB_CM_REJ_CONSUMER_DEFINED," 1814aef9ec39SRoland Dreier " opcode 0x%02x\n", opcode); 1815aef9ec39SRoland Dreier target->status = -ECONNRESET; 1816aef9ec39SRoland Dreier break; 1817aef9ec39SRoland Dreier 18189fe4bcf4SDavid Dillow case IB_CM_REJ_STALE_CONN: 18199fe4bcf4SDavid Dillow shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n"); 18209fe4bcf4SDavid Dillow target->status = SRP_STALE_CONN; 18219fe4bcf4SDavid Dillow break; 18229fe4bcf4SDavid Dillow 1823aef9ec39SRoland Dreier default: 18247aa54bd7SDavid Dillow shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n", 1825aef9ec39SRoland Dreier event->param.rej_rcvd.reason); 1826aef9ec39SRoland Dreier target->status = -ECONNRESET; 1827aef9ec39SRoland Dreier } 1828aef9ec39SRoland Dreier } 1829aef9ec39SRoland Dreier 1830aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) 1831aef9ec39SRoland Dreier { 1832aef9ec39SRoland Dreier struct srp_target_port *target = cm_id->context; 1833aef9ec39SRoland Dreier int comp = 0; 1834aef9ec39SRoland Dreier 1835aef9ec39SRoland Dreier switch (event->event) { 1836aef9ec39SRoland Dreier case IB_CM_REQ_ERROR: 18377aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, 18387aa54bd7SDavid Dillow PFX "Sending CM REQ failed\n"); 1839aef9ec39SRoland Dreier comp = 1; 1840aef9ec39SRoland Dreier target->status = -ECONNRESET; 1841aef9ec39SRoland Dreier break; 1842aef9ec39SRoland Dreier 1843aef9ec39SRoland Dreier case IB_CM_REP_RECEIVED: 1844aef9ec39SRoland Dreier comp = 1; 1845961e0be8SDavid Dillow srp_cm_rep_handler(cm_id, event->private_data, target); 1846aef9ec39SRoland Dreier break; 1847aef9ec39SRoland Dreier 1848aef9ec39SRoland Dreier case IB_CM_REJ_RECEIVED: 18497aa54bd7SDavid Dillow shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n"); 1850aef9ec39SRoland Dreier comp = 1; 1851aef9ec39SRoland Dreier 1852aef9ec39SRoland Dreier srp_cm_rej_handler(cm_id, event, target); 1853aef9ec39SRoland Dreier break; 1854aef9ec39SRoland Dreier 1855b7ac4ab4SIshai Rabinovitz case IB_CM_DREQ_RECEIVED: 18567aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 18577aa54bd7SDavid Dillow PFX "DREQ received - connection closed\n"); 1858294c875aSBart Van Assche srp_change_conn_state(target, false); 1859b7ac4ab4SIshai Rabinovitz if (ib_send_cm_drep(cm_id, NULL, 0)) 18607aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 18617aa54bd7SDavid Dillow PFX "Sending CM DREP failed\n"); 1862c1120f89SBart Van Assche queue_work(system_long_wq, &target->tl_err_work); 1863aef9ec39SRoland Dreier break; 1864aef9ec39SRoland Dreier 1865aef9ec39SRoland Dreier case IB_CM_TIMEWAIT_EXIT: 18667aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 18677aa54bd7SDavid Dillow PFX "connection closed\n"); 1868aef9ec39SRoland Dreier 1869aef9ec39SRoland Dreier target->status = 0; 1870aef9ec39SRoland Dreier break; 1871aef9ec39SRoland Dreier 1872b7ac4ab4SIshai Rabinovitz case IB_CM_MRA_RECEIVED: 1873b7ac4ab4SIshai Rabinovitz case IB_CM_DREQ_ERROR: 1874b7ac4ab4SIshai Rabinovitz case IB_CM_DREP_RECEIVED: 1875b7ac4ab4SIshai Rabinovitz break; 1876b7ac4ab4SIshai Rabinovitz 1877aef9ec39SRoland Dreier default: 18787aa54bd7SDavid Dillow shost_printk(KERN_WARNING, target->scsi_host, 18797aa54bd7SDavid Dillow PFX "Unhandled CM event %d\n", event->event); 1880aef9ec39SRoland Dreier break; 1881aef9ec39SRoland Dreier } 1882aef9ec39SRoland Dreier 1883aef9ec39SRoland Dreier if (comp) 1884aef9ec39SRoland Dreier complete(&target->done); 1885aef9ec39SRoland Dreier 1886aef9ec39SRoland Dreier return 0; 1887aef9ec39SRoland Dreier } 1888aef9ec39SRoland Dreier 188971444b97SJack Wang /** 189071444b97SJack Wang * srp_change_queue_type - changing device queue tag type 189171444b97SJack Wang * @sdev: scsi device struct 189271444b97SJack Wang * @tag_type: requested tag type 189371444b97SJack Wang * 189471444b97SJack Wang * Returns queue tag type. 189571444b97SJack Wang */ 189671444b97SJack Wang static int 189771444b97SJack Wang srp_change_queue_type(struct scsi_device *sdev, int tag_type) 189871444b97SJack Wang { 189971444b97SJack Wang if (sdev->tagged_supported) { 190071444b97SJack Wang scsi_set_tag_type(sdev, tag_type); 190171444b97SJack Wang if (tag_type) 190271444b97SJack Wang scsi_activate_tcq(sdev, sdev->queue_depth); 190371444b97SJack Wang else 190471444b97SJack Wang scsi_deactivate_tcq(sdev, sdev->queue_depth); 190571444b97SJack Wang } else 190671444b97SJack Wang tag_type = 0; 190771444b97SJack Wang 190871444b97SJack Wang return tag_type; 190971444b97SJack Wang } 191071444b97SJack Wang 191171444b97SJack Wang /** 191271444b97SJack Wang * srp_change_queue_depth - setting device queue depth 191371444b97SJack Wang * @sdev: scsi device struct 191471444b97SJack Wang * @qdepth: requested queue depth 191571444b97SJack Wang * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP 191671444b97SJack Wang * (see include/scsi/scsi_host.h for definition) 191771444b97SJack Wang * 191871444b97SJack Wang * Returns queue depth. 191971444b97SJack Wang */ 192071444b97SJack Wang static int 192171444b97SJack Wang srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason) 192271444b97SJack Wang { 192371444b97SJack Wang struct Scsi_Host *shost = sdev->host; 192471444b97SJack Wang int max_depth; 192571444b97SJack Wang if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) { 192671444b97SJack Wang max_depth = shost->can_queue; 192771444b97SJack Wang if (!sdev->tagged_supported) 192871444b97SJack Wang max_depth = 1; 192971444b97SJack Wang if (qdepth > max_depth) 193071444b97SJack Wang qdepth = max_depth; 193171444b97SJack Wang scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth); 193271444b97SJack Wang } else if (reason == SCSI_QDEPTH_QFULL) 193371444b97SJack Wang scsi_track_queue_full(sdev, qdepth); 193471444b97SJack Wang else 193571444b97SJack Wang return -EOPNOTSUPP; 193671444b97SJack Wang 193771444b97SJack Wang return sdev->queue_depth; 193871444b97SJack Wang } 193971444b97SJack Wang 1940d945e1dfSRoland Dreier static int srp_send_tsk_mgmt(struct srp_target_port *target, 1941f8b6e31eSDavid Dillow u64 req_tag, unsigned int lun, u8 func) 1942aef9ec39SRoland Dreier { 1943a95cadb9SBart Van Assche struct srp_rport *rport = target->rport; 194419081f31SDavid Dillow struct ib_device *dev = target->srp_host->srp_dev->dev; 1945aef9ec39SRoland Dreier struct srp_iu *iu; 1946aef9ec39SRoland Dreier struct srp_tsk_mgmt *tsk_mgmt; 1947aef9ec39SRoland Dreier 19483780d1f0SBart Van Assche if (!target->connected || target->qp_in_error) 19493780d1f0SBart Van Assche return -1; 19503780d1f0SBart Van Assche 1951f8b6e31eSDavid Dillow init_completion(&target->tsk_mgmt_done); 1952aef9ec39SRoland Dreier 1953a95cadb9SBart Van Assche /* 1954a95cadb9SBart Van Assche * Lock the rport mutex to avoid that srp_create_target_ib() is 1955a95cadb9SBart Van Assche * invoked while a task management function is being sent. 1956a95cadb9SBart Van Assche */ 1957a95cadb9SBart Van Assche mutex_lock(&rport->mutex); 1958e9684678SBart Van Assche spin_lock_irq(&target->lock); 1959bb12588aSDavid Dillow iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT); 1960e9684678SBart Van Assche spin_unlock_irq(&target->lock); 196176c75b25SBart Van Assche 1962a95cadb9SBart Van Assche if (!iu) { 1963a95cadb9SBart Van Assche mutex_unlock(&rport->mutex); 1964a95cadb9SBart Van Assche 196576c75b25SBart Van Assche return -1; 1966a95cadb9SBart Van Assche } 1967aef9ec39SRoland Dreier 196819081f31SDavid Dillow ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt, 196919081f31SDavid Dillow DMA_TO_DEVICE); 1970aef9ec39SRoland Dreier tsk_mgmt = iu->buf; 1971aef9ec39SRoland Dreier memset(tsk_mgmt, 0, sizeof *tsk_mgmt); 1972aef9ec39SRoland Dreier 1973aef9ec39SRoland Dreier tsk_mgmt->opcode = SRP_TSK_MGMT; 1974f8b6e31eSDavid Dillow tsk_mgmt->lun = cpu_to_be64((u64) lun << 48); 1975f8b6e31eSDavid Dillow tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT; 1976aef9ec39SRoland Dreier tsk_mgmt->tsk_mgmt_func = func; 1977f8b6e31eSDavid Dillow tsk_mgmt->task_tag = req_tag; 1978aef9ec39SRoland Dreier 197919081f31SDavid Dillow ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt, 198019081f31SDavid Dillow DMA_TO_DEVICE); 198176c75b25SBart Van Assche if (srp_post_send(target, iu, sizeof *tsk_mgmt)) { 198276c75b25SBart Van Assche srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT); 1983a95cadb9SBart Van Assche mutex_unlock(&rport->mutex); 1984a95cadb9SBart Van Assche 198576c75b25SBart Van Assche return -1; 198676c75b25SBart Van Assche } 1987a95cadb9SBart Van Assche mutex_unlock(&rport->mutex); 1988d945e1dfSRoland Dreier 1989f8b6e31eSDavid Dillow if (!wait_for_completion_timeout(&target->tsk_mgmt_done, 1990aef9ec39SRoland Dreier msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS))) 1991d945e1dfSRoland Dreier return -1; 1992aef9ec39SRoland Dreier 1993d945e1dfSRoland Dreier return 0; 1994d945e1dfSRoland Dreier } 1995d945e1dfSRoland Dreier 1996aef9ec39SRoland Dreier static int srp_abort(struct scsi_cmnd *scmnd) 1997aef9ec39SRoland Dreier { 1998d945e1dfSRoland Dreier struct srp_target_port *target = host_to_target(scmnd->device->host); 1999f8b6e31eSDavid Dillow struct srp_request *req = (struct srp_request *) scmnd->host_scribble; 2000086f44f5SBart Van Assche int ret; 2001d945e1dfSRoland Dreier 20027aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n"); 2003aef9ec39SRoland Dreier 2004c7c4e7ffSBart Van Assche if (!req || !srp_claim_req(target, req, scmnd)) 200599b6697aSBart Van Assche return SUCCESS; 2006086f44f5SBart Van Assche if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun, 200780d5e8a2SBart Van Assche SRP_TSK_ABORT_TASK) == 0) 2008086f44f5SBart Van Assche ret = SUCCESS; 2009ed9b2264SBart Van Assche else if (target->rport->state == SRP_RPORT_LOST) 201099e1c139SBart Van Assche ret = FAST_IO_FAIL; 2011086f44f5SBart Van Assche else 2012086f44f5SBart Van Assche ret = FAILED; 201322032991SBart Van Assche srp_free_req(target, req, scmnd, 0); 2014d945e1dfSRoland Dreier scmnd->result = DID_ABORT << 16; 2015d8536670SBart Van Assche scmnd->scsi_done(scmnd); 2016d945e1dfSRoland Dreier 2017086f44f5SBart Van Assche return ret; 2018aef9ec39SRoland Dreier } 2019aef9ec39SRoland Dreier 2020aef9ec39SRoland Dreier static int srp_reset_device(struct scsi_cmnd *scmnd) 2021aef9ec39SRoland Dreier { 2022d945e1dfSRoland Dreier struct srp_target_port *target = host_to_target(scmnd->device->host); 2023536ae14eSBart Van Assche int i; 2024d945e1dfSRoland Dreier 20257aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n"); 2026aef9ec39SRoland Dreier 2027f8b6e31eSDavid Dillow if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun, 2028f8b6e31eSDavid Dillow SRP_TSK_LUN_RESET)) 2029d945e1dfSRoland Dreier return FAILED; 2030f8b6e31eSDavid Dillow if (target->tsk_mgmt_status) 2031d945e1dfSRoland Dreier return FAILED; 2032d945e1dfSRoland Dreier 20334d73f95fSBart Van Assche for (i = 0; i < target->req_ring_size; ++i) { 2034536ae14eSBart Van Assche struct srp_request *req = &target->req_ring[i]; 2035f8b6e31eSDavid Dillow if (req->scmnd && req->scmnd->device == scmnd->device) 2036ed9b2264SBart Van Assche srp_finish_req(target, req, DID_RESET << 16); 2037536ae14eSBart Van Assche } 2038d945e1dfSRoland Dreier 2039d945e1dfSRoland Dreier return SUCCESS; 2040aef9ec39SRoland Dreier } 2041aef9ec39SRoland Dreier 2042aef9ec39SRoland Dreier static int srp_reset_host(struct scsi_cmnd *scmnd) 2043aef9ec39SRoland Dreier { 2044aef9ec39SRoland Dreier struct srp_target_port *target = host_to_target(scmnd->device->host); 2045aef9ec39SRoland Dreier 20467aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n"); 2047aef9ec39SRoland Dreier 2048ed9b2264SBart Van Assche return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED; 2049aef9ec39SRoland Dreier } 2050aef9ec39SRoland Dreier 2051c9b03c1aSBart Van Assche static int srp_slave_configure(struct scsi_device *sdev) 2052c9b03c1aSBart Van Assche { 2053c9b03c1aSBart Van Assche struct Scsi_Host *shost = sdev->host; 2054c9b03c1aSBart Van Assche struct srp_target_port *target = host_to_target(shost); 2055c9b03c1aSBart Van Assche struct request_queue *q = sdev->request_queue; 2056c9b03c1aSBart Van Assche unsigned long timeout; 2057c9b03c1aSBart Van Assche 2058c9b03c1aSBart Van Assche if (sdev->type == TYPE_DISK) { 2059c9b03c1aSBart Van Assche timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies); 2060c9b03c1aSBart Van Assche blk_queue_rq_timeout(q, timeout); 2061c9b03c1aSBart Van Assche } 2062c9b03c1aSBart Van Assche 2063c9b03c1aSBart Van Assche return 0; 2064c9b03c1aSBart Van Assche } 2065c9b03c1aSBart Van Assche 2066ee959b00STony Jones static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr, 2067ee959b00STony Jones char *buf) 20686ecb0c84SRoland Dreier { 2069ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 20706ecb0c84SRoland Dreier 20716ecb0c84SRoland Dreier return sprintf(buf, "0x%016llx\n", 20726ecb0c84SRoland Dreier (unsigned long long) be64_to_cpu(target->id_ext)); 20736ecb0c84SRoland Dreier } 20746ecb0c84SRoland Dreier 2075ee959b00STony Jones static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr, 2076ee959b00STony Jones char *buf) 20776ecb0c84SRoland Dreier { 2078ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 20796ecb0c84SRoland Dreier 20806ecb0c84SRoland Dreier return sprintf(buf, "0x%016llx\n", 20816ecb0c84SRoland Dreier (unsigned long long) be64_to_cpu(target->ioc_guid)); 20826ecb0c84SRoland Dreier } 20836ecb0c84SRoland Dreier 2084ee959b00STony Jones static ssize_t show_service_id(struct device *dev, 2085ee959b00STony Jones struct device_attribute *attr, char *buf) 20866ecb0c84SRoland Dreier { 2087ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 20886ecb0c84SRoland Dreier 20896ecb0c84SRoland Dreier return sprintf(buf, "0x%016llx\n", 20906ecb0c84SRoland Dreier (unsigned long long) be64_to_cpu(target->service_id)); 20916ecb0c84SRoland Dreier } 20926ecb0c84SRoland Dreier 2093ee959b00STony Jones static ssize_t show_pkey(struct device *dev, struct device_attribute *attr, 2094ee959b00STony Jones char *buf) 20956ecb0c84SRoland Dreier { 2096ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 20976ecb0c84SRoland Dreier 20986ecb0c84SRoland Dreier return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey)); 20996ecb0c84SRoland Dreier } 21006ecb0c84SRoland Dreier 2101848b3082SBart Van Assche static ssize_t show_sgid(struct device *dev, struct device_attribute *attr, 2102848b3082SBart Van Assche char *buf) 2103848b3082SBart Van Assche { 2104848b3082SBart Van Assche struct srp_target_port *target = host_to_target(class_to_shost(dev)); 2105848b3082SBart Van Assche 2106848b3082SBart Van Assche return sprintf(buf, "%pI6\n", target->path.sgid.raw); 2107848b3082SBart Van Assche } 2108848b3082SBart Van Assche 2109ee959b00STony Jones static ssize_t show_dgid(struct device *dev, struct device_attribute *attr, 2110ee959b00STony Jones char *buf) 21116ecb0c84SRoland Dreier { 2112ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 21136ecb0c84SRoland Dreier 21145b095d98SHarvey Harrison return sprintf(buf, "%pI6\n", target->path.dgid.raw); 21156ecb0c84SRoland Dreier } 21166ecb0c84SRoland Dreier 2117ee959b00STony Jones static ssize_t show_orig_dgid(struct device *dev, 2118ee959b00STony Jones struct device_attribute *attr, char *buf) 21193633b3d0SIshai Rabinovitz { 2120ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 21213633b3d0SIshai Rabinovitz 21225b095d98SHarvey Harrison return sprintf(buf, "%pI6\n", target->orig_dgid); 21233633b3d0SIshai Rabinovitz } 21243633b3d0SIshai Rabinovitz 212589de7486SBart Van Assche static ssize_t show_req_lim(struct device *dev, 212689de7486SBart Van Assche struct device_attribute *attr, char *buf) 212789de7486SBart Van Assche { 212889de7486SBart Van Assche struct srp_target_port *target = host_to_target(class_to_shost(dev)); 212989de7486SBart Van Assche 213089de7486SBart Van Assche return sprintf(buf, "%d\n", target->req_lim); 213189de7486SBart Van Assche } 213289de7486SBart Van Assche 2133ee959b00STony Jones static ssize_t show_zero_req_lim(struct device *dev, 2134ee959b00STony Jones struct device_attribute *attr, char *buf) 21356bfa24faSRoland Dreier { 2136ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 21376bfa24faSRoland Dreier 21386bfa24faSRoland Dreier return sprintf(buf, "%d\n", target->zero_req_lim); 21396bfa24faSRoland Dreier } 21406bfa24faSRoland Dreier 2141ee959b00STony Jones static ssize_t show_local_ib_port(struct device *dev, 2142ee959b00STony Jones struct device_attribute *attr, char *buf) 2143ded7f1a1SIshai Rabinovitz { 2144ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 2145ded7f1a1SIshai Rabinovitz 2146ded7f1a1SIshai Rabinovitz return sprintf(buf, "%d\n", target->srp_host->port); 2147ded7f1a1SIshai Rabinovitz } 2148ded7f1a1SIshai Rabinovitz 2149ee959b00STony Jones static ssize_t show_local_ib_device(struct device *dev, 2150ee959b00STony Jones struct device_attribute *attr, char *buf) 2151ded7f1a1SIshai Rabinovitz { 2152ee959b00STony Jones struct srp_target_port *target = host_to_target(class_to_shost(dev)); 2153ded7f1a1SIshai Rabinovitz 215405321937SGreg Kroah-Hartman return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name); 2155ded7f1a1SIshai Rabinovitz } 2156ded7f1a1SIshai Rabinovitz 21574b5e5f41SBart Van Assche static ssize_t show_comp_vector(struct device *dev, 21584b5e5f41SBart Van Assche struct device_attribute *attr, char *buf) 21594b5e5f41SBart Van Assche { 21604b5e5f41SBart Van Assche struct srp_target_port *target = host_to_target(class_to_shost(dev)); 21614b5e5f41SBart Van Assche 21624b5e5f41SBart Van Assche return sprintf(buf, "%d\n", target->comp_vector); 21634b5e5f41SBart Van Assche } 21644b5e5f41SBart Van Assche 21657bb312e4SVu Pham static ssize_t show_tl_retry_count(struct device *dev, 21667bb312e4SVu Pham struct device_attribute *attr, char *buf) 21677bb312e4SVu Pham { 21687bb312e4SVu Pham struct srp_target_port *target = host_to_target(class_to_shost(dev)); 21697bb312e4SVu Pham 21707bb312e4SVu Pham return sprintf(buf, "%d\n", target->tl_retry_count); 21717bb312e4SVu Pham } 21727bb312e4SVu Pham 217349248644SDavid Dillow static ssize_t show_cmd_sg_entries(struct device *dev, 217449248644SDavid Dillow struct device_attribute *attr, char *buf) 217549248644SDavid Dillow { 217649248644SDavid Dillow struct srp_target_port *target = host_to_target(class_to_shost(dev)); 217749248644SDavid Dillow 217849248644SDavid Dillow return sprintf(buf, "%u\n", target->cmd_sg_cnt); 217949248644SDavid Dillow } 218049248644SDavid Dillow 2181c07d424dSDavid Dillow static ssize_t show_allow_ext_sg(struct device *dev, 2182c07d424dSDavid Dillow struct device_attribute *attr, char *buf) 2183c07d424dSDavid Dillow { 2184c07d424dSDavid Dillow struct srp_target_port *target = host_to_target(class_to_shost(dev)); 2185c07d424dSDavid Dillow 2186c07d424dSDavid Dillow return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false"); 2187c07d424dSDavid Dillow } 2188c07d424dSDavid Dillow 2189ee959b00STony Jones static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL); 2190ee959b00STony Jones static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL); 2191ee959b00STony Jones static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL); 2192ee959b00STony Jones static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); 2193848b3082SBart Van Assche static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL); 2194ee959b00STony Jones static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL); 2195ee959b00STony Jones static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL); 219689de7486SBart Van Assche static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL); 2197ee959b00STony Jones static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL); 2198ee959b00STony Jones static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL); 2199ee959b00STony Jones static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL); 22004b5e5f41SBart Van Assche static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL); 22017bb312e4SVu Pham static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL); 220249248644SDavid Dillow static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL); 2203c07d424dSDavid Dillow static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL); 22046ecb0c84SRoland Dreier 2205ee959b00STony Jones static struct device_attribute *srp_host_attrs[] = { 2206ee959b00STony Jones &dev_attr_id_ext, 2207ee959b00STony Jones &dev_attr_ioc_guid, 2208ee959b00STony Jones &dev_attr_service_id, 2209ee959b00STony Jones &dev_attr_pkey, 2210848b3082SBart Van Assche &dev_attr_sgid, 2211ee959b00STony Jones &dev_attr_dgid, 2212ee959b00STony Jones &dev_attr_orig_dgid, 221389de7486SBart Van Assche &dev_attr_req_lim, 2214ee959b00STony Jones &dev_attr_zero_req_lim, 2215ee959b00STony Jones &dev_attr_local_ib_port, 2216ee959b00STony Jones &dev_attr_local_ib_device, 22174b5e5f41SBart Van Assche &dev_attr_comp_vector, 22187bb312e4SVu Pham &dev_attr_tl_retry_count, 221949248644SDavid Dillow &dev_attr_cmd_sg_entries, 2220c07d424dSDavid Dillow &dev_attr_allow_ext_sg, 22216ecb0c84SRoland Dreier NULL 22226ecb0c84SRoland Dreier }; 22236ecb0c84SRoland Dreier 2224aef9ec39SRoland Dreier static struct scsi_host_template srp_template = { 2225aef9ec39SRoland Dreier .module = THIS_MODULE, 2226b7f008fdSRoland Dreier .name = "InfiniBand SRP initiator", 2227b7f008fdSRoland Dreier .proc_name = DRV_NAME, 2228c9b03c1aSBart Van Assche .slave_configure = srp_slave_configure, 2229aef9ec39SRoland Dreier .info = srp_target_info, 2230aef9ec39SRoland Dreier .queuecommand = srp_queuecommand, 223171444b97SJack Wang .change_queue_depth = srp_change_queue_depth, 223271444b97SJack Wang .change_queue_type = srp_change_queue_type, 2233aef9ec39SRoland Dreier .eh_abort_handler = srp_abort, 2234aef9ec39SRoland Dreier .eh_device_reset_handler = srp_reset_device, 2235aef9ec39SRoland Dreier .eh_host_reset_handler = srp_reset_host, 22362742c1daSBart Van Assche .skip_settle_delay = true, 223749248644SDavid Dillow .sg_tablesize = SRP_DEF_SG_TABLESIZE, 22384d73f95fSBart Van Assche .can_queue = SRP_DEFAULT_CMD_SQ_SIZE, 2239aef9ec39SRoland Dreier .this_id = -1, 22404d73f95fSBart Van Assche .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE, 22416ecb0c84SRoland Dreier .use_clustering = ENABLE_CLUSTERING, 22426ecb0c84SRoland Dreier .shost_attrs = srp_host_attrs 2243aef9ec39SRoland Dreier }; 2244aef9ec39SRoland Dreier 2245aef9ec39SRoland Dreier static int srp_add_target(struct srp_host *host, struct srp_target_port *target) 2246aef9ec39SRoland Dreier { 22473236822bSFUJITA Tomonori struct srp_rport_identifiers ids; 22483236822bSFUJITA Tomonori struct srp_rport *rport; 22493236822bSFUJITA Tomonori 2250aef9ec39SRoland Dreier sprintf(target->target_name, "SRP.T10:%016llX", 2251aef9ec39SRoland Dreier (unsigned long long) be64_to_cpu(target->id_ext)); 2252aef9ec39SRoland Dreier 225305321937SGreg Kroah-Hartman if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device)) 2254aef9ec39SRoland Dreier return -ENODEV; 2255aef9ec39SRoland Dreier 22563236822bSFUJITA Tomonori memcpy(ids.port_id, &target->id_ext, 8); 22573236822bSFUJITA Tomonori memcpy(ids.port_id + 8, &target->ioc_guid, 8); 2258aebd5e47SFUJITA Tomonori ids.roles = SRP_RPORT_ROLE_TARGET; 22593236822bSFUJITA Tomonori rport = srp_rport_add(target->scsi_host, &ids); 22603236822bSFUJITA Tomonori if (IS_ERR(rport)) { 22613236822bSFUJITA Tomonori scsi_remove_host(target->scsi_host); 22623236822bSFUJITA Tomonori return PTR_ERR(rport); 22633236822bSFUJITA Tomonori } 22643236822bSFUJITA Tomonori 2265dc1bdbd9SBart Van Assche rport->lld_data = target; 22669dd69a60SBart Van Assche target->rport = rport; 2267dc1bdbd9SBart Van Assche 2268b3589fd4SMatthew Wilcox spin_lock(&host->target_lock); 2269aef9ec39SRoland Dreier list_add_tail(&target->list, &host->target_list); 2270b3589fd4SMatthew Wilcox spin_unlock(&host->target_lock); 2271aef9ec39SRoland Dreier 2272aef9ec39SRoland Dreier target->state = SRP_TARGET_LIVE; 2273aef9ec39SRoland Dreier 2274aef9ec39SRoland Dreier scsi_scan_target(&target->scsi_host->shost_gendev, 22751962a4a1SMatthew Wilcox 0, target->scsi_id, SCAN_WILD_CARD, 0); 2276aef9ec39SRoland Dreier 2277aef9ec39SRoland Dreier return 0; 2278aef9ec39SRoland Dreier } 2279aef9ec39SRoland Dreier 2280ee959b00STony Jones static void srp_release_dev(struct device *dev) 2281aef9ec39SRoland Dreier { 2282aef9ec39SRoland Dreier struct srp_host *host = 2283ee959b00STony Jones container_of(dev, struct srp_host, dev); 2284aef9ec39SRoland Dreier 2285aef9ec39SRoland Dreier complete(&host->released); 2286aef9ec39SRoland Dreier } 2287aef9ec39SRoland Dreier 2288aef9ec39SRoland Dreier static struct class srp_class = { 2289aef9ec39SRoland Dreier .name = "infiniband_srp", 2290ee959b00STony Jones .dev_release = srp_release_dev 2291aef9ec39SRoland Dreier }; 2292aef9ec39SRoland Dreier 229396fc248aSBart Van Assche /** 229496fc248aSBart Van Assche * srp_conn_unique() - check whether the connection to a target is unique 229596fc248aSBart Van Assche */ 229696fc248aSBart Van Assche static bool srp_conn_unique(struct srp_host *host, 229796fc248aSBart Van Assche struct srp_target_port *target) 229896fc248aSBart Van Assche { 229996fc248aSBart Van Assche struct srp_target_port *t; 230096fc248aSBart Van Assche bool ret = false; 230196fc248aSBart Van Assche 230296fc248aSBart Van Assche if (target->state == SRP_TARGET_REMOVED) 230396fc248aSBart Van Assche goto out; 230496fc248aSBart Van Assche 230596fc248aSBart Van Assche ret = true; 230696fc248aSBart Van Assche 230796fc248aSBart Van Assche spin_lock(&host->target_lock); 230896fc248aSBart Van Assche list_for_each_entry(t, &host->target_list, list) { 230996fc248aSBart Van Assche if (t != target && 231096fc248aSBart Van Assche target->id_ext == t->id_ext && 231196fc248aSBart Van Assche target->ioc_guid == t->ioc_guid && 231296fc248aSBart Van Assche target->initiator_ext == t->initiator_ext) { 231396fc248aSBart Van Assche ret = false; 231496fc248aSBart Van Assche break; 231596fc248aSBart Van Assche } 231696fc248aSBart Van Assche } 231796fc248aSBart Van Assche spin_unlock(&host->target_lock); 231896fc248aSBart Van Assche 231996fc248aSBart Van Assche out: 232096fc248aSBart Van Assche return ret; 232196fc248aSBart Van Assche } 232296fc248aSBart Van Assche 2323aef9ec39SRoland Dreier /* 2324aef9ec39SRoland Dreier * Target ports are added by writing 2325aef9ec39SRoland Dreier * 2326aef9ec39SRoland Dreier * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>, 2327aef9ec39SRoland Dreier * pkey=<P_Key>,service_id=<service ID> 2328aef9ec39SRoland Dreier * 2329aef9ec39SRoland Dreier * to the add_target sysfs attribute. 2330aef9ec39SRoland Dreier */ 2331aef9ec39SRoland Dreier enum { 2332aef9ec39SRoland Dreier SRP_OPT_ERR = 0, 2333aef9ec39SRoland Dreier SRP_OPT_ID_EXT = 1 << 0, 2334aef9ec39SRoland Dreier SRP_OPT_IOC_GUID = 1 << 1, 2335aef9ec39SRoland Dreier SRP_OPT_DGID = 1 << 2, 2336aef9ec39SRoland Dreier SRP_OPT_PKEY = 1 << 3, 2337aef9ec39SRoland Dreier SRP_OPT_SERVICE_ID = 1 << 4, 2338aef9ec39SRoland Dreier SRP_OPT_MAX_SECT = 1 << 5, 233952fb2b50SVu Pham SRP_OPT_MAX_CMD_PER_LUN = 1 << 6, 23400c0450dbSRamachandra K SRP_OPT_IO_CLASS = 1 << 7, 234101cb9bcbSIshai Rabinovitz SRP_OPT_INITIATOR_EXT = 1 << 8, 234249248644SDavid Dillow SRP_OPT_CMD_SG_ENTRIES = 1 << 9, 2343c07d424dSDavid Dillow SRP_OPT_ALLOW_EXT_SG = 1 << 10, 2344c07d424dSDavid Dillow SRP_OPT_SG_TABLESIZE = 1 << 11, 23454b5e5f41SBart Van Assche SRP_OPT_COMP_VECTOR = 1 << 12, 23467bb312e4SVu Pham SRP_OPT_TL_RETRY_COUNT = 1 << 13, 23474d73f95fSBart Van Assche SRP_OPT_QUEUE_SIZE = 1 << 14, 2348aef9ec39SRoland Dreier SRP_OPT_ALL = (SRP_OPT_ID_EXT | 2349aef9ec39SRoland Dreier SRP_OPT_IOC_GUID | 2350aef9ec39SRoland Dreier SRP_OPT_DGID | 2351aef9ec39SRoland Dreier SRP_OPT_PKEY | 2352aef9ec39SRoland Dreier SRP_OPT_SERVICE_ID), 2353aef9ec39SRoland Dreier }; 2354aef9ec39SRoland Dreier 2355a447c093SSteven Whitehouse static const match_table_t srp_opt_tokens = { 2356aef9ec39SRoland Dreier { SRP_OPT_ID_EXT, "id_ext=%s" }, 2357aef9ec39SRoland Dreier { SRP_OPT_IOC_GUID, "ioc_guid=%s" }, 2358aef9ec39SRoland Dreier { SRP_OPT_DGID, "dgid=%s" }, 2359aef9ec39SRoland Dreier { SRP_OPT_PKEY, "pkey=%x" }, 2360aef9ec39SRoland Dreier { SRP_OPT_SERVICE_ID, "service_id=%s" }, 2361aef9ec39SRoland Dreier { SRP_OPT_MAX_SECT, "max_sect=%d" }, 236252fb2b50SVu Pham { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" }, 23630c0450dbSRamachandra K { SRP_OPT_IO_CLASS, "io_class=%x" }, 236401cb9bcbSIshai Rabinovitz { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" }, 236549248644SDavid Dillow { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" }, 2366c07d424dSDavid Dillow { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" }, 2367c07d424dSDavid Dillow { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" }, 23684b5e5f41SBart Van Assche { SRP_OPT_COMP_VECTOR, "comp_vector=%u" }, 23697bb312e4SVu Pham { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" }, 23704d73f95fSBart Van Assche { SRP_OPT_QUEUE_SIZE, "queue_size=%d" }, 2371aef9ec39SRoland Dreier { SRP_OPT_ERR, NULL } 2372aef9ec39SRoland Dreier }; 2373aef9ec39SRoland Dreier 2374aef9ec39SRoland Dreier static int srp_parse_options(const char *buf, struct srp_target_port *target) 2375aef9ec39SRoland Dreier { 2376aef9ec39SRoland Dreier char *options, *sep_opt; 2377aef9ec39SRoland Dreier char *p; 2378aef9ec39SRoland Dreier char dgid[3]; 2379aef9ec39SRoland Dreier substring_t args[MAX_OPT_ARGS]; 2380aef9ec39SRoland Dreier int opt_mask = 0; 2381aef9ec39SRoland Dreier int token; 2382aef9ec39SRoland Dreier int ret = -EINVAL; 2383aef9ec39SRoland Dreier int i; 2384aef9ec39SRoland Dreier 2385aef9ec39SRoland Dreier options = kstrdup(buf, GFP_KERNEL); 2386aef9ec39SRoland Dreier if (!options) 2387aef9ec39SRoland Dreier return -ENOMEM; 2388aef9ec39SRoland Dreier 2389aef9ec39SRoland Dreier sep_opt = options; 2390aef9ec39SRoland Dreier while ((p = strsep(&sep_opt, ",")) != NULL) { 2391aef9ec39SRoland Dreier if (!*p) 2392aef9ec39SRoland Dreier continue; 2393aef9ec39SRoland Dreier 2394aef9ec39SRoland Dreier token = match_token(p, srp_opt_tokens, args); 2395aef9ec39SRoland Dreier opt_mask |= token; 2396aef9ec39SRoland Dreier 2397aef9ec39SRoland Dreier switch (token) { 2398aef9ec39SRoland Dreier case SRP_OPT_ID_EXT: 2399aef9ec39SRoland Dreier p = match_strdup(args); 2400a20f3a6dSIshai Rabinovitz if (!p) { 2401a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 2402a20f3a6dSIshai Rabinovitz goto out; 2403a20f3a6dSIshai Rabinovitz } 2404aef9ec39SRoland Dreier target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); 2405aef9ec39SRoland Dreier kfree(p); 2406aef9ec39SRoland Dreier break; 2407aef9ec39SRoland Dreier 2408aef9ec39SRoland Dreier case SRP_OPT_IOC_GUID: 2409aef9ec39SRoland Dreier p = match_strdup(args); 2410a20f3a6dSIshai Rabinovitz if (!p) { 2411a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 2412a20f3a6dSIshai Rabinovitz goto out; 2413a20f3a6dSIshai Rabinovitz } 2414aef9ec39SRoland Dreier target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16)); 2415aef9ec39SRoland Dreier kfree(p); 2416aef9ec39SRoland Dreier break; 2417aef9ec39SRoland Dreier 2418aef9ec39SRoland Dreier case SRP_OPT_DGID: 2419aef9ec39SRoland Dreier p = match_strdup(args); 2420a20f3a6dSIshai Rabinovitz if (!p) { 2421a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 2422a20f3a6dSIshai Rabinovitz goto out; 2423a20f3a6dSIshai Rabinovitz } 2424aef9ec39SRoland Dreier if (strlen(p) != 32) { 2425e0bda7d8SBart Van Assche pr_warn("bad dest GID parameter '%s'\n", p); 2426ce1823f0SRoland Dreier kfree(p); 2427aef9ec39SRoland Dreier goto out; 2428aef9ec39SRoland Dreier } 2429aef9ec39SRoland Dreier 2430aef9ec39SRoland Dreier for (i = 0; i < 16; ++i) { 2431aef9ec39SRoland Dreier strlcpy(dgid, p + i * 2, 3); 2432aef9ec39SRoland Dreier target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16); 2433aef9ec39SRoland Dreier } 2434bf17c1c7SRoland Dreier kfree(p); 24353633b3d0SIshai Rabinovitz memcpy(target->orig_dgid, target->path.dgid.raw, 16); 2436aef9ec39SRoland Dreier break; 2437aef9ec39SRoland Dreier 2438aef9ec39SRoland Dreier case SRP_OPT_PKEY: 2439aef9ec39SRoland Dreier if (match_hex(args, &token)) { 2440e0bda7d8SBart Van Assche pr_warn("bad P_Key parameter '%s'\n", p); 2441aef9ec39SRoland Dreier goto out; 2442aef9ec39SRoland Dreier } 2443aef9ec39SRoland Dreier target->path.pkey = cpu_to_be16(token); 2444aef9ec39SRoland Dreier break; 2445aef9ec39SRoland Dreier 2446aef9ec39SRoland Dreier case SRP_OPT_SERVICE_ID: 2447aef9ec39SRoland Dreier p = match_strdup(args); 2448a20f3a6dSIshai Rabinovitz if (!p) { 2449a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 2450a20f3a6dSIshai Rabinovitz goto out; 2451a20f3a6dSIshai Rabinovitz } 2452aef9ec39SRoland Dreier target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16)); 2453247e020eSSean Hefty target->path.service_id = target->service_id; 2454aef9ec39SRoland Dreier kfree(p); 2455aef9ec39SRoland Dreier break; 2456aef9ec39SRoland Dreier 2457aef9ec39SRoland Dreier case SRP_OPT_MAX_SECT: 2458aef9ec39SRoland Dreier if (match_int(args, &token)) { 2459e0bda7d8SBart Van Assche pr_warn("bad max sect parameter '%s'\n", p); 2460aef9ec39SRoland Dreier goto out; 2461aef9ec39SRoland Dreier } 2462aef9ec39SRoland Dreier target->scsi_host->max_sectors = token; 2463aef9ec39SRoland Dreier break; 2464aef9ec39SRoland Dreier 24654d73f95fSBart Van Assche case SRP_OPT_QUEUE_SIZE: 24664d73f95fSBart Van Assche if (match_int(args, &token) || token < 1) { 24674d73f95fSBart Van Assche pr_warn("bad queue_size parameter '%s'\n", p); 24684d73f95fSBart Van Assche goto out; 24694d73f95fSBart Van Assche } 24704d73f95fSBart Van Assche target->scsi_host->can_queue = token; 24714d73f95fSBart Van Assche target->queue_size = token + SRP_RSP_SQ_SIZE + 24724d73f95fSBart Van Assche SRP_TSK_MGMT_SQ_SIZE; 24734d73f95fSBart Van Assche if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN)) 24744d73f95fSBart Van Assche target->scsi_host->cmd_per_lun = token; 24754d73f95fSBart Van Assche break; 24764d73f95fSBart Van Assche 247752fb2b50SVu Pham case SRP_OPT_MAX_CMD_PER_LUN: 24784d73f95fSBart Van Assche if (match_int(args, &token) || token < 1) { 2479e0bda7d8SBart Van Assche pr_warn("bad max cmd_per_lun parameter '%s'\n", 2480e0bda7d8SBart Van Assche p); 248152fb2b50SVu Pham goto out; 248252fb2b50SVu Pham } 24834d73f95fSBart Van Assche target->scsi_host->cmd_per_lun = token; 248452fb2b50SVu Pham break; 248552fb2b50SVu Pham 24860c0450dbSRamachandra K case SRP_OPT_IO_CLASS: 24870c0450dbSRamachandra K if (match_hex(args, &token)) { 2488e0bda7d8SBart Van Assche pr_warn("bad IO class parameter '%s'\n", p); 24890c0450dbSRamachandra K goto out; 24900c0450dbSRamachandra K } 24910c0450dbSRamachandra K if (token != SRP_REV10_IB_IO_CLASS && 24920c0450dbSRamachandra K token != SRP_REV16A_IB_IO_CLASS) { 2493e0bda7d8SBart Van Assche pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n", 2494e0bda7d8SBart Van Assche token, SRP_REV10_IB_IO_CLASS, 2495e0bda7d8SBart Van Assche SRP_REV16A_IB_IO_CLASS); 24960c0450dbSRamachandra K goto out; 24970c0450dbSRamachandra K } 24980c0450dbSRamachandra K target->io_class = token; 24990c0450dbSRamachandra K break; 25000c0450dbSRamachandra K 250101cb9bcbSIshai Rabinovitz case SRP_OPT_INITIATOR_EXT: 250201cb9bcbSIshai Rabinovitz p = match_strdup(args); 2503a20f3a6dSIshai Rabinovitz if (!p) { 2504a20f3a6dSIshai Rabinovitz ret = -ENOMEM; 2505a20f3a6dSIshai Rabinovitz goto out; 2506a20f3a6dSIshai Rabinovitz } 250701cb9bcbSIshai Rabinovitz target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); 250801cb9bcbSIshai Rabinovitz kfree(p); 250901cb9bcbSIshai Rabinovitz break; 251001cb9bcbSIshai Rabinovitz 251149248644SDavid Dillow case SRP_OPT_CMD_SG_ENTRIES: 251249248644SDavid Dillow if (match_int(args, &token) || token < 1 || token > 255) { 2513e0bda7d8SBart Van Assche pr_warn("bad max cmd_sg_entries parameter '%s'\n", 2514e0bda7d8SBart Van Assche p); 251549248644SDavid Dillow goto out; 251649248644SDavid Dillow } 251749248644SDavid Dillow target->cmd_sg_cnt = token; 251849248644SDavid Dillow break; 251949248644SDavid Dillow 2520c07d424dSDavid Dillow case SRP_OPT_ALLOW_EXT_SG: 2521c07d424dSDavid Dillow if (match_int(args, &token)) { 2522e0bda7d8SBart Van Assche pr_warn("bad allow_ext_sg parameter '%s'\n", p); 2523c07d424dSDavid Dillow goto out; 2524c07d424dSDavid Dillow } 2525c07d424dSDavid Dillow target->allow_ext_sg = !!token; 2526c07d424dSDavid Dillow break; 2527c07d424dSDavid Dillow 2528c07d424dSDavid Dillow case SRP_OPT_SG_TABLESIZE: 2529c07d424dSDavid Dillow if (match_int(args, &token) || token < 1 || 2530c07d424dSDavid Dillow token > SCSI_MAX_SG_CHAIN_SEGMENTS) { 2531e0bda7d8SBart Van Assche pr_warn("bad max sg_tablesize parameter '%s'\n", 2532e0bda7d8SBart Van Assche p); 2533c07d424dSDavid Dillow goto out; 2534c07d424dSDavid Dillow } 2535c07d424dSDavid Dillow target->sg_tablesize = token; 2536c07d424dSDavid Dillow break; 2537c07d424dSDavid Dillow 25384b5e5f41SBart Van Assche case SRP_OPT_COMP_VECTOR: 25394b5e5f41SBart Van Assche if (match_int(args, &token) || token < 0) { 25404b5e5f41SBart Van Assche pr_warn("bad comp_vector parameter '%s'\n", p); 25414b5e5f41SBart Van Assche goto out; 25424b5e5f41SBart Van Assche } 25434b5e5f41SBart Van Assche target->comp_vector = token; 25444b5e5f41SBart Van Assche break; 25454b5e5f41SBart Van Assche 25467bb312e4SVu Pham case SRP_OPT_TL_RETRY_COUNT: 25477bb312e4SVu Pham if (match_int(args, &token) || token < 2 || token > 7) { 25487bb312e4SVu Pham pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n", 25497bb312e4SVu Pham p); 25507bb312e4SVu Pham goto out; 25517bb312e4SVu Pham } 25527bb312e4SVu Pham target->tl_retry_count = token; 25537bb312e4SVu Pham break; 25547bb312e4SVu Pham 2555aef9ec39SRoland Dreier default: 2556e0bda7d8SBart Van Assche pr_warn("unknown parameter or missing value '%s' in target creation request\n", 2557e0bda7d8SBart Van Assche p); 2558aef9ec39SRoland Dreier goto out; 2559aef9ec39SRoland Dreier } 2560aef9ec39SRoland Dreier } 2561aef9ec39SRoland Dreier 2562aef9ec39SRoland Dreier if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL) 2563aef9ec39SRoland Dreier ret = 0; 2564aef9ec39SRoland Dreier else 2565aef9ec39SRoland Dreier for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i) 2566aef9ec39SRoland Dreier if ((srp_opt_tokens[i].token & SRP_OPT_ALL) && 2567aef9ec39SRoland Dreier !(srp_opt_tokens[i].token & opt_mask)) 2568e0bda7d8SBart Van Assche pr_warn("target creation request is missing parameter '%s'\n", 2569aef9ec39SRoland Dreier srp_opt_tokens[i].pattern); 2570aef9ec39SRoland Dreier 25714d73f95fSBart Van Assche if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue 25724d73f95fSBart Van Assche && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN)) 25734d73f95fSBart Van Assche pr_warn("cmd_per_lun = %d > queue_size = %d\n", 25744d73f95fSBart Van Assche target->scsi_host->cmd_per_lun, 25754d73f95fSBart Van Assche target->scsi_host->can_queue); 25764d73f95fSBart Van Assche 2577aef9ec39SRoland Dreier out: 2578aef9ec39SRoland Dreier kfree(options); 2579aef9ec39SRoland Dreier return ret; 2580aef9ec39SRoland Dreier } 2581aef9ec39SRoland Dreier 2582ee959b00STony Jones static ssize_t srp_create_target(struct device *dev, 2583ee959b00STony Jones struct device_attribute *attr, 2584aef9ec39SRoland Dreier const char *buf, size_t count) 2585aef9ec39SRoland Dreier { 2586aef9ec39SRoland Dreier struct srp_host *host = 2587ee959b00STony Jones container_of(dev, struct srp_host, dev); 2588aef9ec39SRoland Dreier struct Scsi_Host *target_host; 2589aef9ec39SRoland Dreier struct srp_target_port *target; 2590c07d424dSDavid Dillow struct ib_device *ibdev = host->srp_dev->dev; 2591b81d00bdSBart Van Assche int ret; 2592aef9ec39SRoland Dreier 2593aef9ec39SRoland Dreier target_host = scsi_host_alloc(&srp_template, 2594aef9ec39SRoland Dreier sizeof (struct srp_target_port)); 2595aef9ec39SRoland Dreier if (!target_host) 2596aef9ec39SRoland Dreier return -ENOMEM; 2597aef9ec39SRoland Dreier 25983236822bSFUJITA Tomonori target_host->transportt = ib_srp_transport_template; 2599fd1b6c4aSBart Van Assche target_host->max_channel = 0; 2600fd1b6c4aSBart Van Assche target_host->max_id = 1; 26015f068992SRoland Dreier target_host->max_lun = SRP_MAX_LUN; 26023c8edf0eSArne Redlich target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb; 26035f068992SRoland Dreier 2604aef9ec39SRoland Dreier target = host_to_target(target_host); 2605aef9ec39SRoland Dreier 26060c0450dbSRamachandra K target->io_class = SRP_REV16A_IB_IO_CLASS; 2607aef9ec39SRoland Dreier target->scsi_host = target_host; 2608aef9ec39SRoland Dreier target->srp_host = host; 26099af76271SDavid Dillow target->lkey = host->srp_dev->mr->lkey; 26109af76271SDavid Dillow target->rkey = host->srp_dev->mr->rkey; 261149248644SDavid Dillow target->cmd_sg_cnt = cmd_sg_entries; 2612c07d424dSDavid Dillow target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries; 2613c07d424dSDavid Dillow target->allow_ext_sg = allow_ext_sg; 26147bb312e4SVu Pham target->tl_retry_count = 7; 26154d73f95fSBart Van Assche target->queue_size = SRP_DEFAULT_QUEUE_SIZE; 2616aef9ec39SRoland Dreier 2617aef9ec39SRoland Dreier ret = srp_parse_options(buf, target); 2618aef9ec39SRoland Dreier if (ret) 2619aef9ec39SRoland Dreier goto err; 2620aef9ec39SRoland Dreier 26214d73f95fSBart Van Assche target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE; 26224d73f95fSBart Van Assche 262396fc248aSBart Van Assche if (!srp_conn_unique(target->srp_host, target)) { 262496fc248aSBart Van Assche shost_printk(KERN_INFO, target->scsi_host, 262596fc248aSBart Van Assche PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n", 262696fc248aSBart Van Assche be64_to_cpu(target->id_ext), 262796fc248aSBart Van Assche be64_to_cpu(target->ioc_guid), 262896fc248aSBart Van Assche be64_to_cpu(target->initiator_ext)); 262996fc248aSBart Van Assche ret = -EEXIST; 263096fc248aSBart Van Assche goto err; 263196fc248aSBart Van Assche } 263296fc248aSBart Van Assche 2633c07d424dSDavid Dillow if (!host->srp_dev->fmr_pool && !target->allow_ext_sg && 2634c07d424dSDavid Dillow target->cmd_sg_cnt < target->sg_tablesize) { 2635e0bda7d8SBart Van Assche pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n"); 2636c07d424dSDavid Dillow target->sg_tablesize = target->cmd_sg_cnt; 2637c07d424dSDavid Dillow } 2638c07d424dSDavid Dillow 2639c07d424dSDavid Dillow target_host->sg_tablesize = target->sg_tablesize; 2640c07d424dSDavid Dillow target->indirect_size = target->sg_tablesize * 2641c07d424dSDavid Dillow sizeof (struct srp_direct_buf); 264249248644SDavid Dillow target->max_iu_len = sizeof (struct srp_cmd) + 264349248644SDavid Dillow sizeof (struct srp_indirect_buf) + 264449248644SDavid Dillow target->cmd_sg_cnt * sizeof (struct srp_direct_buf); 264549248644SDavid Dillow 2646c1120f89SBart Van Assche INIT_WORK(&target->tl_err_work, srp_tl_err_work); 2647ef6c49d8SBart Van Assche INIT_WORK(&target->remove_work, srp_remove_work); 26488f26c9ffSDavid Dillow spin_lock_init(&target->lock); 26498f26c9ffSDavid Dillow INIT_LIST_HEAD(&target->free_tx); 2650b81d00bdSBart Van Assche ret = srp_alloc_req_data(target); 2651b81d00bdSBart Van Assche if (ret) 26528f26c9ffSDavid Dillow goto err_free_mem; 26538f26c9ffSDavid Dillow 26542088ca66SSagi Grimberg ret = ib_query_gid(ibdev, host->port, 0, &target->path.sgid); 26552088ca66SSagi Grimberg if (ret) 26562088ca66SSagi Grimberg goto err_free_mem; 2657aef9ec39SRoland Dreier 2658aef9ec39SRoland Dreier ret = srp_create_target_ib(target); 2659aef9ec39SRoland Dreier if (ret) 26608f26c9ffSDavid Dillow goto err_free_mem; 2661aef9ec39SRoland Dreier 26629fe4bcf4SDavid Dillow ret = srp_new_cm_id(target); 26639fe4bcf4SDavid Dillow if (ret) 26648f26c9ffSDavid Dillow goto err_free_ib; 2665aef9ec39SRoland Dreier 2666aef9ec39SRoland Dreier ret = srp_connect_target(target); 2667aef9ec39SRoland Dreier if (ret) { 26687aa54bd7SDavid Dillow shost_printk(KERN_ERR, target->scsi_host, 26697aa54bd7SDavid Dillow PFX "Connection failed\n"); 2670aef9ec39SRoland Dreier goto err_cm_id; 2671aef9ec39SRoland Dreier } 2672aef9ec39SRoland Dreier 2673aef9ec39SRoland Dreier ret = srp_add_target(host, target); 2674aef9ec39SRoland Dreier if (ret) 2675aef9ec39SRoland Dreier goto err_disconnect; 2676aef9ec39SRoland Dreier 2677*e7ffde01SBart Van Assche shost_printk(KERN_DEBUG, target->scsi_host, PFX 2678*e7ffde01SBart Van Assche "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n", 2679*e7ffde01SBart Van Assche be64_to_cpu(target->id_ext), 2680*e7ffde01SBart Van Assche be64_to_cpu(target->ioc_guid), 2681*e7ffde01SBart Van Assche be16_to_cpu(target->path.pkey), 2682*e7ffde01SBart Van Assche be64_to_cpu(target->service_id), 2683*e7ffde01SBart Van Assche target->path.sgid.raw, target->path.dgid.raw); 2684*e7ffde01SBart Van Assche 2685aef9ec39SRoland Dreier return count; 2686aef9ec39SRoland Dreier 2687aef9ec39SRoland Dreier err_disconnect: 2688aef9ec39SRoland Dreier srp_disconnect_target(target); 2689aef9ec39SRoland Dreier 2690aef9ec39SRoland Dreier err_cm_id: 2691aef9ec39SRoland Dreier ib_destroy_cm_id(target->cm_id); 2692aef9ec39SRoland Dreier 26938f26c9ffSDavid Dillow err_free_ib: 2694aef9ec39SRoland Dreier srp_free_target_ib(target); 2695aef9ec39SRoland Dreier 26968f26c9ffSDavid Dillow err_free_mem: 26978f26c9ffSDavid Dillow srp_free_req_data(target); 26988f26c9ffSDavid Dillow 2699aef9ec39SRoland Dreier err: 2700aef9ec39SRoland Dreier scsi_host_put(target_host); 2701aef9ec39SRoland Dreier 2702aef9ec39SRoland Dreier return ret; 2703aef9ec39SRoland Dreier } 2704aef9ec39SRoland Dreier 2705ee959b00STony Jones static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target); 2706aef9ec39SRoland Dreier 2707ee959b00STony Jones static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, 2708ee959b00STony Jones char *buf) 2709aef9ec39SRoland Dreier { 2710ee959b00STony Jones struct srp_host *host = container_of(dev, struct srp_host, dev); 2711aef9ec39SRoland Dreier 271205321937SGreg Kroah-Hartman return sprintf(buf, "%s\n", host->srp_dev->dev->name); 2713aef9ec39SRoland Dreier } 2714aef9ec39SRoland Dreier 2715ee959b00STony Jones static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); 2716aef9ec39SRoland Dreier 2717ee959b00STony Jones static ssize_t show_port(struct device *dev, struct device_attribute *attr, 2718ee959b00STony Jones char *buf) 2719aef9ec39SRoland Dreier { 2720ee959b00STony Jones struct srp_host *host = container_of(dev, struct srp_host, dev); 2721aef9ec39SRoland Dreier 2722aef9ec39SRoland Dreier return sprintf(buf, "%d\n", host->port); 2723aef9ec39SRoland Dreier } 2724aef9ec39SRoland Dreier 2725ee959b00STony Jones static DEVICE_ATTR(port, S_IRUGO, show_port, NULL); 2726aef9ec39SRoland Dreier 2727f5358a17SRoland Dreier static struct srp_host *srp_add_port(struct srp_device *device, u8 port) 2728aef9ec39SRoland Dreier { 2729aef9ec39SRoland Dreier struct srp_host *host; 2730aef9ec39SRoland Dreier 2731aef9ec39SRoland Dreier host = kzalloc(sizeof *host, GFP_KERNEL); 2732aef9ec39SRoland Dreier if (!host) 2733aef9ec39SRoland Dreier return NULL; 2734aef9ec39SRoland Dreier 2735aef9ec39SRoland Dreier INIT_LIST_HEAD(&host->target_list); 2736b3589fd4SMatthew Wilcox spin_lock_init(&host->target_lock); 2737aef9ec39SRoland Dreier init_completion(&host->released); 273805321937SGreg Kroah-Hartman host->srp_dev = device; 2739aef9ec39SRoland Dreier host->port = port; 2740aef9ec39SRoland Dreier 2741ee959b00STony Jones host->dev.class = &srp_class; 2742ee959b00STony Jones host->dev.parent = device->dev->dma_device; 2743d927e38cSKay Sievers dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port); 2744aef9ec39SRoland Dreier 2745ee959b00STony Jones if (device_register(&host->dev)) 2746f5358a17SRoland Dreier goto free_host; 2747ee959b00STony Jones if (device_create_file(&host->dev, &dev_attr_add_target)) 2748aef9ec39SRoland Dreier goto err_class; 2749ee959b00STony Jones if (device_create_file(&host->dev, &dev_attr_ibdev)) 2750aef9ec39SRoland Dreier goto err_class; 2751ee959b00STony Jones if (device_create_file(&host->dev, &dev_attr_port)) 2752aef9ec39SRoland Dreier goto err_class; 2753aef9ec39SRoland Dreier 2754aef9ec39SRoland Dreier return host; 2755aef9ec39SRoland Dreier 2756aef9ec39SRoland Dreier err_class: 2757ee959b00STony Jones device_unregister(&host->dev); 2758aef9ec39SRoland Dreier 2759f5358a17SRoland Dreier free_host: 2760aef9ec39SRoland Dreier kfree(host); 2761aef9ec39SRoland Dreier 2762aef9ec39SRoland Dreier return NULL; 2763aef9ec39SRoland Dreier } 2764aef9ec39SRoland Dreier 2765aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device) 2766aef9ec39SRoland Dreier { 2767f5358a17SRoland Dreier struct srp_device *srp_dev; 2768f5358a17SRoland Dreier struct ib_device_attr *dev_attr; 2769f5358a17SRoland Dreier struct ib_fmr_pool_param fmr_param; 2770aef9ec39SRoland Dreier struct srp_host *host; 2771be8b9814SDavid Dillow int max_pages_per_fmr, fmr_page_shift, s, e, p; 2772aef9ec39SRoland Dreier 2773f5358a17SRoland Dreier dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL); 2774f5358a17SRoland Dreier if (!dev_attr) 2775cf311cd4SSean Hefty return; 2776aef9ec39SRoland Dreier 2777f5358a17SRoland Dreier if (ib_query_device(device, dev_attr)) { 2778e0bda7d8SBart Van Assche pr_warn("Query device failed for %s\n", device->name); 2779f5358a17SRoland Dreier goto free_attr; 2780f5358a17SRoland Dreier } 2781f5358a17SRoland Dreier 2782f5358a17SRoland Dreier srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL); 2783f5358a17SRoland Dreier if (!srp_dev) 2784f5358a17SRoland Dreier goto free_attr; 2785f5358a17SRoland Dreier 2786f5358a17SRoland Dreier /* 2787f5358a17SRoland Dreier * Use the smallest page size supported by the HCA, down to a 27888f26c9ffSDavid Dillow * minimum of 4096 bytes. We're unlikely to build large sglists 27898f26c9ffSDavid Dillow * out of smaller entries. 2790f5358a17SRoland Dreier */ 27918f26c9ffSDavid Dillow fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1); 27928f26c9ffSDavid Dillow srp_dev->fmr_page_size = 1 << fmr_page_shift; 2793bf628dc2SRoland Dreier srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1); 27948f26c9ffSDavid Dillow srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE; 2795f5358a17SRoland Dreier 2796f5358a17SRoland Dreier INIT_LIST_HEAD(&srp_dev->dev_list); 2797f5358a17SRoland Dreier 2798f5358a17SRoland Dreier srp_dev->dev = device; 2799f5358a17SRoland Dreier srp_dev->pd = ib_alloc_pd(device); 2800f5358a17SRoland Dreier if (IS_ERR(srp_dev->pd)) 2801f5358a17SRoland Dreier goto free_dev; 2802f5358a17SRoland Dreier 2803f5358a17SRoland Dreier srp_dev->mr = ib_get_dma_mr(srp_dev->pd, 2804f5358a17SRoland Dreier IB_ACCESS_LOCAL_WRITE | 2805f5358a17SRoland Dreier IB_ACCESS_REMOTE_READ | 2806f5358a17SRoland Dreier IB_ACCESS_REMOTE_WRITE); 2807f5358a17SRoland Dreier if (IS_ERR(srp_dev->mr)) 2808f5358a17SRoland Dreier goto err_pd; 2809f5358a17SRoland Dreier 2810be8b9814SDavid Dillow for (max_pages_per_fmr = SRP_FMR_SIZE; 2811be8b9814SDavid Dillow max_pages_per_fmr >= SRP_FMR_MIN_SIZE; 2812be8b9814SDavid Dillow max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) { 2813f5358a17SRoland Dreier memset(&fmr_param, 0, sizeof fmr_param); 2814f5358a17SRoland Dreier fmr_param.pool_size = SRP_FMR_POOL_SIZE; 2815f5358a17SRoland Dreier fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE; 2816f5358a17SRoland Dreier fmr_param.cache = 1; 2817be8b9814SDavid Dillow fmr_param.max_pages_per_fmr = max_pages_per_fmr; 28188f26c9ffSDavid Dillow fmr_param.page_shift = fmr_page_shift; 2819f5358a17SRoland Dreier fmr_param.access = (IB_ACCESS_LOCAL_WRITE | 2820f5358a17SRoland Dreier IB_ACCESS_REMOTE_WRITE | 2821f5358a17SRoland Dreier IB_ACCESS_REMOTE_READ); 2822f5358a17SRoland Dreier 2823f5358a17SRoland Dreier srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param); 2824be8b9814SDavid Dillow if (!IS_ERR(srp_dev->fmr_pool)) 2825be8b9814SDavid Dillow break; 2826be8b9814SDavid Dillow } 2827be8b9814SDavid Dillow 2828f5358a17SRoland Dreier if (IS_ERR(srp_dev->fmr_pool)) 2829f5358a17SRoland Dreier srp_dev->fmr_pool = NULL; 2830aef9ec39SRoland Dreier 283107ebafbaSTom Tucker if (device->node_type == RDMA_NODE_IB_SWITCH) { 2832aef9ec39SRoland Dreier s = 0; 2833aef9ec39SRoland Dreier e = 0; 2834aef9ec39SRoland Dreier } else { 2835aef9ec39SRoland Dreier s = 1; 2836aef9ec39SRoland Dreier e = device->phys_port_cnt; 2837aef9ec39SRoland Dreier } 2838aef9ec39SRoland Dreier 2839aef9ec39SRoland Dreier for (p = s; p <= e; ++p) { 2840f5358a17SRoland Dreier host = srp_add_port(srp_dev, p); 2841aef9ec39SRoland Dreier if (host) 2842f5358a17SRoland Dreier list_add_tail(&host->list, &srp_dev->dev_list); 2843aef9ec39SRoland Dreier } 2844aef9ec39SRoland Dreier 2845f5358a17SRoland Dreier ib_set_client_data(device, &srp_client, srp_dev); 2846f5358a17SRoland Dreier 2847f5358a17SRoland Dreier goto free_attr; 2848f5358a17SRoland Dreier 2849f5358a17SRoland Dreier err_pd: 2850f5358a17SRoland Dreier ib_dealloc_pd(srp_dev->pd); 2851f5358a17SRoland Dreier 2852f5358a17SRoland Dreier free_dev: 2853f5358a17SRoland Dreier kfree(srp_dev); 2854f5358a17SRoland Dreier 2855f5358a17SRoland Dreier free_attr: 2856f5358a17SRoland Dreier kfree(dev_attr); 2857aef9ec39SRoland Dreier } 2858aef9ec39SRoland Dreier 2859aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device) 2860aef9ec39SRoland Dreier { 2861f5358a17SRoland Dreier struct srp_device *srp_dev; 2862aef9ec39SRoland Dreier struct srp_host *host, *tmp_host; 2863ef6c49d8SBart Van Assche struct srp_target_port *target; 2864aef9ec39SRoland Dreier 2865f5358a17SRoland Dreier srp_dev = ib_get_client_data(device, &srp_client); 28661fe0cb84SDotan Barak if (!srp_dev) 28671fe0cb84SDotan Barak return; 2868aef9ec39SRoland Dreier 2869f5358a17SRoland Dreier list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) { 2870ee959b00STony Jones device_unregister(&host->dev); 2871aef9ec39SRoland Dreier /* 2872aef9ec39SRoland Dreier * Wait for the sysfs entry to go away, so that no new 2873aef9ec39SRoland Dreier * target ports can be created. 2874aef9ec39SRoland Dreier */ 2875aef9ec39SRoland Dreier wait_for_completion(&host->released); 2876aef9ec39SRoland Dreier 2877aef9ec39SRoland Dreier /* 2878ef6c49d8SBart Van Assche * Remove all target ports. 2879aef9ec39SRoland Dreier */ 2880b3589fd4SMatthew Wilcox spin_lock(&host->target_lock); 2881ef6c49d8SBart Van Assche list_for_each_entry(target, &host->target_list, list) 2882ef6c49d8SBart Van Assche srp_queue_remove_work(target); 2883b3589fd4SMatthew Wilcox spin_unlock(&host->target_lock); 2884aef9ec39SRoland Dreier 2885aef9ec39SRoland Dreier /* 2886ef6c49d8SBart Van Assche * Wait for target port removal tasks. 2887aef9ec39SRoland Dreier */ 2888ef6c49d8SBart Van Assche flush_workqueue(system_long_wq); 2889aef9ec39SRoland Dreier 2890aef9ec39SRoland Dreier kfree(host); 2891aef9ec39SRoland Dreier } 2892aef9ec39SRoland Dreier 2893f5358a17SRoland Dreier if (srp_dev->fmr_pool) 2894f5358a17SRoland Dreier ib_destroy_fmr_pool(srp_dev->fmr_pool); 2895f5358a17SRoland Dreier ib_dereg_mr(srp_dev->mr); 2896f5358a17SRoland Dreier ib_dealloc_pd(srp_dev->pd); 2897f5358a17SRoland Dreier 2898f5358a17SRoland Dreier kfree(srp_dev); 2899aef9ec39SRoland Dreier } 2900aef9ec39SRoland Dreier 29013236822bSFUJITA Tomonori static struct srp_function_template ib_srp_transport_functions = { 2902ed9b2264SBart Van Assche .has_rport_state = true, 2903ed9b2264SBart Van Assche .reset_timer_if_blocked = true, 2904a95cadb9SBart Van Assche .reconnect_delay = &srp_reconnect_delay, 2905ed9b2264SBart Van Assche .fast_io_fail_tmo = &srp_fast_io_fail_tmo, 2906ed9b2264SBart Van Assche .dev_loss_tmo = &srp_dev_loss_tmo, 2907ed9b2264SBart Van Assche .reconnect = srp_rport_reconnect, 2908dc1bdbd9SBart Van Assche .rport_delete = srp_rport_delete, 2909ed9b2264SBart Van Assche .terminate_rport_io = srp_terminate_io, 29103236822bSFUJITA Tomonori }; 29113236822bSFUJITA Tomonori 2912aef9ec39SRoland Dreier static int __init srp_init_module(void) 2913aef9ec39SRoland Dreier { 2914aef9ec39SRoland Dreier int ret; 2915aef9ec39SRoland Dreier 2916dcb4cb85SBart Van Assche BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *)); 2917dd5e6e38SBart Van Assche 291849248644SDavid Dillow if (srp_sg_tablesize) { 2919e0bda7d8SBart Van Assche pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n"); 292049248644SDavid Dillow if (!cmd_sg_entries) 292149248644SDavid Dillow cmd_sg_entries = srp_sg_tablesize; 292249248644SDavid Dillow } 292349248644SDavid Dillow 292449248644SDavid Dillow if (!cmd_sg_entries) 292549248644SDavid Dillow cmd_sg_entries = SRP_DEF_SG_TABLESIZE; 292649248644SDavid Dillow 292749248644SDavid Dillow if (cmd_sg_entries > 255) { 2928e0bda7d8SBart Van Assche pr_warn("Clamping cmd_sg_entries to 255\n"); 292949248644SDavid Dillow cmd_sg_entries = 255; 29301e89a194SDavid Dillow } 29311e89a194SDavid Dillow 2932c07d424dSDavid Dillow if (!indirect_sg_entries) 2933c07d424dSDavid Dillow indirect_sg_entries = cmd_sg_entries; 2934c07d424dSDavid Dillow else if (indirect_sg_entries < cmd_sg_entries) { 2935e0bda7d8SBart Van Assche pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n", 2936e0bda7d8SBart Van Assche cmd_sg_entries); 2937c07d424dSDavid Dillow indirect_sg_entries = cmd_sg_entries; 2938c07d424dSDavid Dillow } 2939c07d424dSDavid Dillow 29403236822bSFUJITA Tomonori ib_srp_transport_template = 29413236822bSFUJITA Tomonori srp_attach_transport(&ib_srp_transport_functions); 29423236822bSFUJITA Tomonori if (!ib_srp_transport_template) 29433236822bSFUJITA Tomonori return -ENOMEM; 29443236822bSFUJITA Tomonori 2945aef9ec39SRoland Dreier ret = class_register(&srp_class); 2946aef9ec39SRoland Dreier if (ret) { 2947e0bda7d8SBart Van Assche pr_err("couldn't register class infiniband_srp\n"); 29483236822bSFUJITA Tomonori srp_release_transport(ib_srp_transport_template); 2949aef9ec39SRoland Dreier return ret; 2950aef9ec39SRoland Dreier } 2951aef9ec39SRoland Dreier 2952c1a0b23bSMichael S. Tsirkin ib_sa_register_client(&srp_sa_client); 2953c1a0b23bSMichael S. Tsirkin 2954aef9ec39SRoland Dreier ret = ib_register_client(&srp_client); 2955aef9ec39SRoland Dreier if (ret) { 2956e0bda7d8SBart Van Assche pr_err("couldn't register IB client\n"); 29573236822bSFUJITA Tomonori srp_release_transport(ib_srp_transport_template); 2958c1a0b23bSMichael S. Tsirkin ib_sa_unregister_client(&srp_sa_client); 2959aef9ec39SRoland Dreier class_unregister(&srp_class); 2960aef9ec39SRoland Dreier return ret; 2961aef9ec39SRoland Dreier } 2962aef9ec39SRoland Dreier 2963aef9ec39SRoland Dreier return 0; 2964aef9ec39SRoland Dreier } 2965aef9ec39SRoland Dreier 2966aef9ec39SRoland Dreier static void __exit srp_cleanup_module(void) 2967aef9ec39SRoland Dreier { 2968aef9ec39SRoland Dreier ib_unregister_client(&srp_client); 2969c1a0b23bSMichael S. Tsirkin ib_sa_unregister_client(&srp_sa_client); 2970aef9ec39SRoland Dreier class_unregister(&srp_class); 29713236822bSFUJITA Tomonori srp_release_transport(ib_srp_transport_template); 2972aef9ec39SRoland Dreier } 2973aef9ec39SRoland Dreier 2974aef9ec39SRoland Dreier module_init(srp_init_module); 2975aef9ec39SRoland Dreier module_exit(srp_cleanup_module); 2976