1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * NVMe over Fabrics common host code. 4 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 5 */ 6 #ifndef _NVME_FABRICS_H 7 #define _NVME_FABRICS_H 1 8 9 #include <linux/in.h> 10 #include <linux/inet.h> 11 12 #define NVMF_MIN_QUEUE_SIZE 16 13 #define NVMF_MAX_QUEUE_SIZE 1024 14 #define NVMF_DEF_QUEUE_SIZE 128 15 #define NVMF_DEF_RECONNECT_DELAY 10 16 /* default to 600 seconds of reconnect attempts before giving up */ 17 #define NVMF_DEF_CTRL_LOSS_TMO 600 18 /* default is -1: the fail fast mechanism is disabled */ 19 #define NVMF_DEF_FAIL_FAST_TMO -1 20 21 /* 22 * Define a host as seen by the target. We allocate one at boot, but also 23 * allow the override it when creating controllers. This is both to provide 24 * persistence of the Host NQN over multiple boots, and to allow using 25 * multiple ones, for example in a container scenario. Because we must not 26 * use different Host NQNs with the same Host ID we generate a Host ID and 27 * use this structure to keep track of the relation between the two. 28 */ 29 struct nvmf_host { 30 struct kref ref; 31 struct list_head list; 32 char nqn[NVMF_NQN_SIZE]; 33 uuid_t id; 34 }; 35 36 /** 37 * enum nvmf_parsing_opts - used to define the sysfs parsing options used. 38 */ 39 enum { 40 NVMF_OPT_ERR = 0, 41 NVMF_OPT_TRANSPORT = 1 << 0, 42 NVMF_OPT_NQN = 1 << 1, 43 NVMF_OPT_TRADDR = 1 << 2, 44 NVMF_OPT_TRSVCID = 1 << 3, 45 NVMF_OPT_QUEUE_SIZE = 1 << 4, 46 NVMF_OPT_NR_IO_QUEUES = 1 << 5, 47 NVMF_OPT_TL_RETRY_COUNT = 1 << 6, 48 NVMF_OPT_KATO = 1 << 7, 49 NVMF_OPT_HOSTNQN = 1 << 8, 50 NVMF_OPT_RECONNECT_DELAY = 1 << 9, 51 NVMF_OPT_HOST_TRADDR = 1 << 10, 52 NVMF_OPT_CTRL_LOSS_TMO = 1 << 11, 53 NVMF_OPT_HOST_ID = 1 << 12, 54 NVMF_OPT_DUP_CONNECT = 1 << 13, 55 NVMF_OPT_DISABLE_SQFLOW = 1 << 14, 56 NVMF_OPT_HDR_DIGEST = 1 << 15, 57 NVMF_OPT_DATA_DIGEST = 1 << 16, 58 NVMF_OPT_NR_WRITE_QUEUES = 1 << 17, 59 NVMF_OPT_NR_POLL_QUEUES = 1 << 18, 60 NVMF_OPT_TOS = 1 << 19, 61 NVMF_OPT_FAIL_FAST_TMO = 1 << 20, 62 NVMF_OPT_HOST_IFACE = 1 << 21, 63 NVMF_OPT_DISCOVERY = 1 << 22, 64 NVMF_OPT_DHCHAP_SECRET = 1 << 23, 65 NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24, 66 NVMF_OPT_TLS = 1 << 25, 67 NVMF_OPT_KEYRING = 1 << 26, 68 NVMF_OPT_TLS_KEY = 1 << 27, 69 NVMF_OPT_CONCAT = 1 << 28, 70 }; 71 72 /** 73 * struct nvmf_ctrl_options - Used to hold the options specified 74 * with the parsing opts enum. 75 * @mask: Used by the fabrics library to parse through sysfs options 76 * on adding a NVMe controller. 77 * @max_reconnects: maximum number of allowed reconnect attempts before removing 78 * the controller, (-1) means reconnect forever, zero means remove 79 * immediately; 80 * @transport: Holds the fabric transport "technology name" (for a lack of 81 * better description) that will be used by an NVMe controller 82 * being added. 83 * @subsysnqn: Hold the fully qualified NQN subsystem name (format defined 84 * in the NVMe specification, "NVMe Qualified Names"). 85 * @traddr: The transport-specific TRADDR field for a port on the 86 * subsystem which is adding a controller. 87 * @trsvcid: The transport-specific TRSVCID field for a port on the 88 * subsystem which is adding a controller. 89 * @host_traddr: A transport-specific field identifying the NVME host port 90 * to use for the connection to the controller. 91 * @host_iface: A transport-specific field identifying the NVME host 92 * interface to use for the connection to the controller. 93 * @queue_size: Number of IO queue elements. 94 * @nr_io_queues: Number of controller IO queues that will be established. 95 * @reconnect_delay: Time between two consecutive reconnect attempts. 96 * @discovery_nqn: indicates if the subsysnqn is the well-known discovery NQN. 97 * @kato: Keep-alive timeout. 98 * @host: Virtual NVMe host, contains the NQN and Host ID. 99 * @dhchap_secret: DH-HMAC-CHAP secret 100 * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional 101 * authentication 102 * @keyring: Keyring to use for key lookups 103 * @tls_key: TLS key for encrypted connections (TCP) 104 * @tls: Start TLS encrypted connections (TCP) 105 * @concat: Enabled Secure channel concatenation (TCP) 106 * @disable_sqflow: disable controller sq flow control 107 * @hdr_digest: generate/verify header digest (TCP) 108 * @data_digest: generate/verify data digest (TCP) 109 * @nr_write_queues: number of queues for write I/O 110 * @nr_poll_queues: number of queues for polling I/O 111 * @tos: type of service 112 * @fast_io_fail_tmo: Fast I/O fail timeout in seconds 113 */ 114 struct nvmf_ctrl_options { 115 unsigned mask; 116 int max_reconnects; 117 char *transport; 118 char *subsysnqn; 119 char *traddr; 120 char *trsvcid; 121 char *host_traddr; 122 char *host_iface; 123 size_t queue_size; 124 unsigned int nr_io_queues; 125 unsigned int reconnect_delay; 126 bool discovery_nqn; 127 bool duplicate_connect; 128 unsigned int kato; 129 struct nvmf_host *host; 130 char *dhchap_secret; 131 char *dhchap_ctrl_secret; 132 struct key *keyring; 133 struct key *tls_key; 134 bool tls; 135 bool concat; 136 bool disable_sqflow; 137 bool hdr_digest; 138 bool data_digest; 139 unsigned int nr_write_queues; 140 unsigned int nr_poll_queues; 141 int tos; 142 int fast_io_fail_tmo; 143 }; 144 145 /* 146 * struct nvmf_transport_ops - used to register a specific 147 * fabric implementation of NVMe fabrics. 148 * @entry: Used by the fabrics library to add the new 149 * registration entry to its linked-list internal tree. 150 * @module: Transport module reference 151 * @name: Name of the NVMe fabric driver implementation. 152 * @required_opts: sysfs command-line options that must be specified 153 * when adding a new NVMe controller. 154 * @allowed_opts: sysfs command-line options that can be specified 155 * when adding a new NVMe controller. 156 * @create_ctrl(): function pointer that points to a non-NVMe 157 * implementation-specific fabric technology 158 * that would go into starting up that fabric 159 * for the purpose of connection to an NVMe controller 160 * using that fabric technology. 161 * 162 * Notes: 163 * 1. At minimum, 'required_opts' and 'allowed_opts' should 164 * be set to the same enum parsing options defined earlier. 165 * 2. create_ctrl() must be defined (even if it does nothing) 166 * 3. struct nvmf_transport_ops must be statically allocated in the 167 * modules .bss section so that a pure module_get on @module 168 * prevents the memory from being freed. 169 */ 170 struct nvmf_transport_ops { 171 struct list_head entry; 172 struct module *module; 173 const char *name; 174 int required_opts; 175 int allowed_opts; 176 struct nvme_ctrl *(*create_ctrl)(struct device *dev, 177 struct nvmf_ctrl_options *opts); 178 }; 179 180 static inline bool 181 nvmf_ctlr_matches_baseopts(struct nvme_ctrl *ctrl, 182 struct nvmf_ctrl_options *opts) 183 { 184 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 185 186 if (state == NVME_CTRL_DELETING || 187 state == NVME_CTRL_DELETING_NOIO || 188 state == NVME_CTRL_DEAD || 189 strcmp(opts->subsysnqn, ctrl->opts->subsysnqn) || 190 strcmp(opts->host->nqn, ctrl->opts->host->nqn) || 191 !uuid_equal(&opts->host->id, &ctrl->opts->host->id)) 192 return false; 193 194 return true; 195 } 196 197 static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctrl *ctrl) 198 { 199 if (!ctrl->subsys || 200 !strcmp(ctrl->opts->subsysnqn, NVME_DISC_SUBSYS_NAME)) 201 return ctrl->opts->subsysnqn; 202 return ctrl->subsys->subnqn; 203 } 204 205 static inline void nvmf_complete_timed_out_request(struct request *rq) 206 { 207 if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) { 208 nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD; 209 blk_mq_complete_request(rq); 210 } 211 } 212 213 static inline unsigned int nvmf_nr_io_queues(struct nvmf_ctrl_options *opts) 214 { 215 return min(opts->nr_io_queues, num_online_cpus()) + 216 min(opts->nr_write_queues, num_online_cpus()) + 217 min(opts->nr_poll_queues, num_online_cpus()); 218 } 219 220 int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val); 221 int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val); 222 int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val); 223 int nvmf_subsystem_reset(struct nvme_ctrl *ctrl); 224 int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl); 225 int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid); 226 int nvmf_register_transport(struct nvmf_transport_ops *ops); 227 void nvmf_unregister_transport(struct nvmf_transport_ops *ops); 228 void nvmf_free_options(struct nvmf_ctrl_options *opts); 229 int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size); 230 bool nvmf_should_reconnect(struct nvme_ctrl *ctrl, int status); 231 bool nvmf_ip_options_match(struct nvme_ctrl *ctrl, 232 struct nvmf_ctrl_options *opts); 233 void nvmf_set_io_queues(struct nvmf_ctrl_options *opts, u32 nr_io_queues, 234 u32 io_queues[HCTX_MAX_TYPES]); 235 void nvmf_map_queues(struct blk_mq_tag_set *set, struct nvme_ctrl *ctrl, 236 u32 io_queues[HCTX_MAX_TYPES]); 237 238 #endif /* _NVME_FABRICS_H */ 239