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