1 // SPDX-License-Identifier: GPL-2.0
3 * Discovery service for the NVMe over Fabrics target.
4 * Copyright (C) 2016 Intel Corporation. All rights reserved.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/slab.h>
8 #include <generated/utsrelease.h>
11 struct nvmet_subsys *nvmet_disc_subsys;
13 static u64 nvmet_genctr;
15 static void __nvmet_disc_changed(struct nvmet_port *port,
16 struct nvmet_ctrl *ctrl)
18 if (ctrl->port != port)
21 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_DISC_CHANGE))
24 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
25 NVME_AER_NOTICE_DISC_CHANGED, NVME_LOG_DISC);
28 void nvmet_port_disc_changed(struct nvmet_port *port,
29 struct nvmet_subsys *subsys)
31 struct nvmet_ctrl *ctrl;
35 list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) {
36 if (subsys && !nvmet_host_allowed(subsys, ctrl->hostnqn))
39 __nvmet_disc_changed(port, ctrl);
43 static void __nvmet_subsys_disc_changed(struct nvmet_port *port,
44 struct nvmet_subsys *subsys,
45 struct nvmet_host *host)
47 struct nvmet_ctrl *ctrl;
49 list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) {
50 if (host && strcmp(nvmet_host_name(host), ctrl->hostnqn))
53 __nvmet_disc_changed(port, ctrl);
57 void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
58 struct nvmet_host *host)
60 struct nvmet_port *port;
61 struct nvmet_subsys_link *s;
65 list_for_each_entry(port, nvmet_ports, global_entry)
66 list_for_each_entry(s, &port->subsystems, entry) {
67 if (s->subsys != subsys)
69 __nvmet_subsys_disc_changed(port, subsys, host);
73 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port)
75 down_write(&nvmet_config_sem);
76 if (list_empty(&port->entry)) {
77 list_add_tail(&port->entry, &parent->referrals);
79 nvmet_port_disc_changed(parent, NULL);
81 up_write(&nvmet_config_sem);
84 void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port)
86 down_write(&nvmet_config_sem);
87 if (!list_empty(&port->entry)) {
88 port->enabled = false;
89 list_del_init(&port->entry);
90 nvmet_port_disc_changed(parent, NULL);
92 up_write(&nvmet_config_sem);
95 static void nvmet_format_discovery_entry(struct nvmf_disc_rsp_page_hdr *hdr,
96 struct nvmet_port *port, char *subsys_nqn, char *traddr,
99 struct nvmf_disc_rsp_page_entry *e = &hdr->entries[numrec];
101 e->trtype = port->disc_addr.trtype;
102 e->adrfam = port->disc_addr.adrfam;
103 e->treq = port->disc_addr.treq;
104 e->portid = port->disc_addr.portid;
105 /* we support only dynamic controllers */
106 e->cntlid = cpu_to_le16(NVME_CNTLID_DYNAMIC);
107 e->asqsz = cpu_to_le16(NVME_AQ_DEPTH);
109 memcpy(e->trsvcid, port->disc_addr.trsvcid, NVMF_TRSVCID_SIZE);
110 memcpy(e->traddr, traddr, NVMF_TRADDR_SIZE);
111 memcpy(e->tsas.common, port->disc_addr.tsas.common, NVMF_TSAS_SIZE);
112 strncpy(e->subnqn, subsys_nqn, NVMF_NQN_SIZE);
116 * nvmet_set_disc_traddr - set a correct discovery log entry traddr
118 * IP based transports (e.g RDMA) can listen on "any" ipv4/ipv6 addresses
119 * (INADDR_ANY or IN6ADDR_ANY_INIT). The discovery log page traddr reply
120 * must not contain that "any" IP address. If the transport implements
121 * .disc_traddr, use it. this callback will set the discovery traddr
122 * from the req->port address in case the port in question listens
125 static void nvmet_set_disc_traddr(struct nvmet_req *req, struct nvmet_port *port,
128 if (req->ops->disc_traddr)
129 req->ops->disc_traddr(req, port, traddr);
131 memcpy(traddr, port->disc_addr.traddr, NVMF_TRADDR_SIZE);
134 static void nvmet_execute_get_disc_log_page(struct nvmet_req *req)
136 const int entry_size = sizeof(struct nvmf_disc_rsp_page_entry);
137 struct nvmet_ctrl *ctrl = req->sq->ctrl;
138 struct nvmf_disc_rsp_page_hdr *hdr;
139 size_t data_len = nvmet_get_log_page_len(req->cmd);
140 size_t alloc_len = max(data_len, sizeof(*hdr));
141 int residual_len = data_len - sizeof(*hdr);
142 struct nvmet_subsys_link *p;
143 struct nvmet_port *r;
148 * Make sure we're passing at least a buffer of response header size.
149 * If host provided data len is less than the header size, only the
150 * number of bytes requested by host will be sent to host.
152 hdr = kzalloc(alloc_len, GFP_KERNEL);
154 status = NVME_SC_INTERNAL;
158 down_read(&nvmet_config_sem);
159 list_for_each_entry(p, &req->port->subsystems, entry) {
160 if (!nvmet_host_allowed(p->subsys, ctrl->hostnqn))
162 if (residual_len >= entry_size) {
163 char traddr[NVMF_TRADDR_SIZE];
165 nvmet_set_disc_traddr(req, req->port, traddr);
166 nvmet_format_discovery_entry(hdr, req->port,
167 p->subsys->subsysnqn, traddr,
168 NVME_NQN_NVME, numrec);
169 residual_len -= entry_size;
174 list_for_each_entry(r, &req->port->referrals, entry) {
175 if (residual_len >= entry_size) {
176 nvmet_format_discovery_entry(hdr, r,
177 NVME_DISC_SUBSYS_NAME,
179 NVME_NQN_DISC, numrec);
180 residual_len -= entry_size;
185 hdr->genctr = cpu_to_le64(nvmet_genctr);
186 hdr->numrec = cpu_to_le64(numrec);
187 hdr->recfmt = cpu_to_le16(0);
189 nvmet_clear_aen_bit(req, NVME_AEN_BIT_DISC_CHANGE);
191 up_read(&nvmet_config_sem);
193 status = nvmet_copy_to_sgl(req, 0, hdr, data_len);
196 nvmet_req_complete(req, status);
199 static void nvmet_execute_identify_disc_ctrl(struct nvmet_req *req)
201 struct nvmet_ctrl *ctrl = req->sq->ctrl;
202 struct nvme_id_ctrl *id;
205 id = kzalloc(sizeof(*id), GFP_KERNEL);
207 status = NVME_SC_INTERNAL;
211 memset(id->fr, ' ', sizeof(id->fr));
212 strncpy((char *)id->fr, UTS_RELEASE, sizeof(id->fr));
214 /* no limit on data transfer sizes for now */
216 id->cntlid = cpu_to_le16(ctrl->cntlid);
217 id->ver = cpu_to_le32(ctrl->subsys->ver);
220 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
221 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
223 id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
224 if (ctrl->ops->has_keyed_sgls)
225 id->sgls |= cpu_to_le32(1 << 2);
226 if (req->port->inline_data_size)
227 id->sgls |= cpu_to_le32(1 << 20);
229 id->oaes = cpu_to_le32(NVMET_DISC_AEN_CFG_OPTIONAL);
231 strlcpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn));
233 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
237 nvmet_req_complete(req, status);
240 static void nvmet_execute_disc_set_features(struct nvmet_req *req)
242 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
245 switch (cdw10 & 0xff) {
247 stat = nvmet_set_feat_kato(req);
249 case NVME_FEAT_ASYNC_EVENT:
250 stat = nvmet_set_feat_async_event(req,
251 NVMET_DISC_AEN_CFG_OPTIONAL);
255 offsetof(struct nvme_common_command, cdw10);
256 stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
260 nvmet_req_complete(req, stat);
263 static void nvmet_execute_disc_get_features(struct nvmet_req *req)
265 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
268 switch (cdw10 & 0xff) {
270 nvmet_get_feat_kato(req);
272 case NVME_FEAT_ASYNC_EVENT:
273 nvmet_get_feat_async_event(req);
277 offsetof(struct nvme_common_command, cdw10);
278 stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
282 nvmet_req_complete(req, stat);
285 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req)
287 struct nvme_command *cmd = req->cmd;
289 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
290 pr_err("got cmd %d while not ready\n",
293 offsetof(struct nvme_common_command, opcode);
294 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
297 switch (cmd->common.opcode) {
298 case nvme_admin_set_features:
299 req->execute = nvmet_execute_disc_set_features;
302 case nvme_admin_get_features:
303 req->execute = nvmet_execute_disc_get_features;
306 case nvme_admin_async_event:
307 req->execute = nvmet_execute_async_event;
310 case nvme_admin_keep_alive:
311 req->execute = nvmet_execute_keep_alive;
314 case nvme_admin_get_log_page:
315 req->data_len = nvmet_get_log_page_len(cmd);
317 switch (cmd->get_log_page.lid) {
319 req->execute = nvmet_execute_get_disc_log_page;
322 pr_err("unsupported get_log_page lid %d\n",
323 cmd->get_log_page.lid);
325 offsetof(struct nvme_get_log_page_command, lid);
326 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
328 case nvme_admin_identify:
329 req->data_len = NVME_IDENTIFY_DATA_SIZE;
330 switch (cmd->identify.cns) {
331 case NVME_ID_CNS_CTRL:
333 nvmet_execute_identify_disc_ctrl;
336 pr_err("unsupported identify cns %d\n",
338 req->error_loc = offsetof(struct nvme_identify, cns);
339 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
342 pr_err("unhandled cmd %d\n", cmd->common.opcode);
343 req->error_loc = offsetof(struct nvme_common_command, opcode);
344 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
349 int __init nvmet_init_discovery(void)
352 nvmet_subsys_alloc(NVME_DISC_SUBSYS_NAME, NVME_NQN_DISC);
353 if (!nvmet_disc_subsys)
358 void nvmet_exit_discovery(void)
360 nvmet_subsys_put(nvmet_disc_subsys);