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 }; 70 71 /** 72 * struct nvmf_ctrl_options - Used to hold the options specified 73 * with the parsing opts enum. 74 * @mask: Used by the fabrics library to parse through sysfs options 75 * on adding a NVMe controller. 76 * @max_reconnects: maximum number of allowed reconnect attempts before removing 77 * the controller, (-1) means reconnect forever, zero means remove 78 * immediately; 79 * @transport: Holds the fabric transport "technology name" (for a lack of 80 * better description) that will be used by an NVMe controller 81 * being added. 82 * @subsysnqn: Hold the fully qualified NQN subystem name (format defined 83 * in the NVMe specification, "NVMe Qualified Names"). 84 * @traddr: The transport-specific TRADDR field for a port on the 85 * subsystem which is adding a controller. 86 * @trsvcid: The transport-specific TRSVCID field for a port on the 87 * subsystem which is adding a controller. 88 * @host_traddr: A transport-specific field identifying the NVME host port 89 * to use for the connection to the controller. 90 * @host_iface: A transport-specific field identifying the NVME host 91 * interface to use for the connection to the controller. 92 * @queue_size: Number of IO queue elements. 93 * @nr_io_queues: Number of controller IO queues that will be established. 94 * @reconnect_delay: Time between two consecutive reconnect attempts. 95 * @discovery_nqn: indicates if the subsysnqn is the well-known discovery NQN. 96 * @kato: Keep-alive timeout. 97 * @host: Virtual NVMe host, contains the NQN and Host ID. 98 * @dhchap_secret: DH-HMAC-CHAP secret 99 * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional 100 * authentication 101 * @keyring: Keyring to use for key lookups 102 * @tls_key: TLS key for encrypted connections (TCP) 103 * @tls: Start TLS encrypted connections (TCP) 104 * @disable_sqflow: disable controller sq flow control 105 * @hdr_digest: generate/verify header digest (TCP) 106 * @data_digest: generate/verify data digest (TCP) 107 * @nr_write_queues: number of queues for write I/O 108 * @nr_poll_queues: number of queues for polling I/O 109 * @tos: type of service 110 * @fast_io_fail_tmo: Fast I/O fail timeout in seconds 111 */ 112 struct nvmf_ctrl_options { 113 unsigned mask; 114 int max_reconnects; 115 char *transport; 116 char *subsysnqn; 117 char *traddr; 118 char *trsvcid; 119 char *host_traddr; 120 char *host_iface; 121 size_t queue_size; 122 unsigned int nr_io_queues; 123 unsigned int reconnect_delay; 124 bool discovery_nqn; 125 bool duplicate_connect; 126 unsigned int kato; 127 struct nvmf_host *host; 128 char *dhchap_secret; 129 char *dhchap_ctrl_secret; 130 struct key *keyring; 131 struct key *tls_key; 132 bool tls; 133 bool disable_sqflow; 134 bool hdr_digest; 135 bool data_digest; 136 unsigned int nr_write_queues; 137 unsigned int nr_poll_queues; 138 int tos; 139 int fast_io_fail_tmo; 140 }; 141 142 /* 143 * struct nvmf_transport_ops - used to register a specific 144 * fabric implementation of NVMe fabrics. 145 * @entry: Used by the fabrics library to add the new 146 * registration entry to its linked-list internal tree. 147 * @module: Transport module reference 148 * @name: Name of the NVMe fabric driver implementation. 149 * @required_opts: sysfs command-line options that must be specified 150 * when adding a new NVMe controller. 151 * @allowed_opts: sysfs command-line options that can be specified 152 * when adding a new NVMe controller. 153 * @create_ctrl(): function pointer that points to a non-NVMe 154 * implementation-specific fabric technology 155 * that would go into starting up that fabric 156 * for the purpose of conneciton to an NVMe controller 157 * using that fabric technology. 158 * 159 * Notes: 160 * 1. At minimum, 'required_opts' and 'allowed_opts' should 161 * be set to the same enum parsing options defined earlier. 162 * 2. create_ctrl() must be defined (even if it does nothing) 163 * 3. struct nvmf_transport_ops must be statically allocated in the 164 * modules .bss section so that a pure module_get on @module 165 * prevents the memory from beeing freed. 166 */ 167 struct nvmf_transport_ops { 168 struct list_head entry; 169 struct module *module; 170 const char *name; 171 int required_opts; 172 int allowed_opts; 173 struct nvme_ctrl *(*create_ctrl)(struct device *dev, 174 struct nvmf_ctrl_options *opts); 175 }; 176 177 static inline bool 178 nvmf_ctlr_matches_baseopts(struct nvme_ctrl *ctrl, 179 struct nvmf_ctrl_options *opts) 180 { 181 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 182 183 if (state == NVME_CTRL_DELETING || 184 state == NVME_CTRL_DELETING_NOIO || 185 state == NVME_CTRL_DEAD || 186 strcmp(opts->subsysnqn, ctrl->opts->subsysnqn) || 187 strcmp(opts->host->nqn, ctrl->opts->host->nqn) || 188 !uuid_equal(&opts->host->id, &ctrl->opts->host->id)) 189 return false; 190 191 return true; 192 } 193 194 static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctrl *ctrl) 195 { 196 if (!ctrl->subsys || 197 !strcmp(ctrl->opts->subsysnqn, NVME_DISC_SUBSYS_NAME)) 198 return ctrl->opts->subsysnqn; 199 return ctrl->subsys->subnqn; 200 } 201 202 static inline void nvmf_complete_timed_out_request(struct request *rq) 203 { 204 if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) { 205 nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD; 206 blk_mq_complete_request(rq); 207 } 208 } 209 210 static inline unsigned int nvmf_nr_io_queues(struct nvmf_ctrl_options *opts) 211 { 212 return min(opts->nr_io_queues, num_online_cpus()) + 213 min(opts->nr_write_queues, num_online_cpus()) + 214 min(opts->nr_poll_queues, num_online_cpus()); 215 } 216 217 int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val); 218 int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val); 219 int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val); 220 int nvmf_subsystem_reset(struct nvme_ctrl *ctrl); 221 int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl); 222 int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid); 223 int nvmf_register_transport(struct nvmf_transport_ops *ops); 224 void nvmf_unregister_transport(struct nvmf_transport_ops *ops); 225 void nvmf_free_options(struct nvmf_ctrl_options *opts); 226 int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size); 227 bool nvmf_should_reconnect(struct nvme_ctrl *ctrl, int status); 228 bool nvmf_ip_options_match(struct nvme_ctrl *ctrl, 229 struct nvmf_ctrl_options *opts); 230 void nvmf_set_io_queues(struct nvmf_ctrl_options *opts, u32 nr_io_queues, 231 u32 io_queues[HCTX_MAX_TYPES]); 232 void nvmf_map_queues(struct blk_mq_tag_set *set, struct nvme_ctrl *ctrl, 233 u32 io_queues[HCTX_MAX_TYPES]); 234 235 #endif /* _NVME_FABRICS_H */ 236