Commit | Line | Data |
---|---|---|
77141dc6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
a07b4970 CH |
2 | /* |
3 | * Discovery service for the NVMe over Fabrics target. | |
4 | * Copyright (C) 2016 Intel Corporation. All rights reserved. | |
a07b4970 CH |
5 | */ |
6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
7 | #include <linux/slab.h> | |
8 | #include <generated/utsrelease.h> | |
9 | #include "nvmet.h" | |
10 | ||
11 | struct nvmet_subsys *nvmet_disc_subsys; | |
12 | ||
03198c4d | 13 | static u64 nvmet_genctr; |
a07b4970 | 14 | |
b662a078 JS |
15 | static void __nvmet_disc_changed(struct nvmet_port *port, |
16 | struct nvmet_ctrl *ctrl) | |
17 | { | |
18 | if (ctrl->port != port) | |
19 | return; | |
20 | ||
21 | if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_DISC_CHANGE)) | |
22 | return; | |
23 | ||
24 | nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE, | |
25 | NVME_AER_NOTICE_DISC_CHANGED, NVME_LOG_DISC); | |
26 | } | |
27 | ||
28 | void nvmet_port_disc_changed(struct nvmet_port *port, | |
29 | struct nvmet_subsys *subsys) | |
30 | { | |
31 | struct nvmet_ctrl *ctrl; | |
32 | ||
6f53e73b | 33 | lockdep_assert_held(&nvmet_config_sem); |
b662a078 JS |
34 | nvmet_genctr++; |
35 | ||
6f53e73b | 36 | mutex_lock(&nvmet_disc_subsys->lock); |
b662a078 JS |
37 | list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) { |
38 | if (subsys && !nvmet_host_allowed(subsys, ctrl->hostnqn)) | |
39 | continue; | |
40 | ||
41 | __nvmet_disc_changed(port, ctrl); | |
42 | } | |
6f53e73b | 43 | mutex_unlock(&nvmet_disc_subsys->lock); |
9d09dd8d JS |
44 | |
45 | /* If transport can signal change, notify transport */ | |
46 | if (port->tr_ops && port->tr_ops->discovery_chg) | |
47 | port->tr_ops->discovery_chg(port); | |
b662a078 JS |
48 | } |
49 | ||
50 | static void __nvmet_subsys_disc_changed(struct nvmet_port *port, | |
51 | struct nvmet_subsys *subsys, | |
52 | struct nvmet_host *host) | |
53 | { | |
54 | struct nvmet_ctrl *ctrl; | |
55 | ||
6f53e73b | 56 | mutex_lock(&nvmet_disc_subsys->lock); |
b662a078 JS |
57 | list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) { |
58 | if (host && strcmp(nvmet_host_name(host), ctrl->hostnqn)) | |
59 | continue; | |
60 | ||
61 | __nvmet_disc_changed(port, ctrl); | |
62 | } | |
6f53e73b | 63 | mutex_unlock(&nvmet_disc_subsys->lock); |
b662a078 JS |
64 | } |
65 | ||
66 | void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys, | |
67 | struct nvmet_host *host) | |
68 | { | |
69 | struct nvmet_port *port; | |
70 | struct nvmet_subsys_link *s; | |
71 | ||
0068a7b0 | 72 | lockdep_assert_held(&nvmet_config_sem); |
b662a078 JS |
73 | nvmet_genctr++; |
74 | ||
75 | list_for_each_entry(port, nvmet_ports, global_entry) | |
76 | list_for_each_entry(s, &port->subsystems, entry) { | |
77 | if (s->subsys != subsys) | |
78 | continue; | |
79 | __nvmet_subsys_disc_changed(port, subsys, host); | |
80 | } | |
81 | } | |
82 | ||
a07b4970 CH |
83 | void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port) |
84 | { | |
85 | down_write(&nvmet_config_sem); | |
86 | if (list_empty(&port->entry)) { | |
87 | list_add_tail(&port->entry, &parent->referrals); | |
88 | port->enabled = true; | |
b662a078 | 89 | nvmet_port_disc_changed(parent, NULL); |
a07b4970 CH |
90 | } |
91 | up_write(&nvmet_config_sem); | |
92 | } | |
93 | ||
b662a078 | 94 | void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port) |
a07b4970 CH |
95 | { |
96 | down_write(&nvmet_config_sem); | |
97 | if (!list_empty(&port->entry)) { | |
98 | port->enabled = false; | |
99 | list_del_init(&port->entry); | |
b662a078 | 100 | nvmet_port_disc_changed(parent, NULL); |
a07b4970 CH |
101 | } |
102 | up_write(&nvmet_config_sem); | |
103 | } | |
104 | ||
105 | static void nvmet_format_discovery_entry(struct nvmf_disc_rsp_page_hdr *hdr, | |
4c652685 SG |
106 | struct nvmet_port *port, char *subsys_nqn, char *traddr, |
107 | u8 type, u32 numrec) | |
a07b4970 CH |
108 | { |
109 | struct nvmf_disc_rsp_page_entry *e = &hdr->entries[numrec]; | |
110 | ||
111 | e->trtype = port->disc_addr.trtype; | |
112 | e->adrfam = port->disc_addr.adrfam; | |
113 | e->treq = port->disc_addr.treq; | |
114 | e->portid = port->disc_addr.portid; | |
115 | /* we support only dynamic controllers */ | |
116 | e->cntlid = cpu_to_le16(NVME_CNTLID_DYNAMIC); | |
7aa1f427 | 117 | e->asqsz = cpu_to_le16(NVME_AQ_DEPTH); |
a446c084 | 118 | e->subtype = type; |
a07b4970 | 119 | memcpy(e->trsvcid, port->disc_addr.trsvcid, NVMF_TRSVCID_SIZE); |
4c652685 | 120 | memcpy(e->traddr, traddr, NVMF_TRADDR_SIZE); |
a07b4970 | 121 | memcpy(e->tsas.common, port->disc_addr.tsas.common, NVMF_TSAS_SIZE); |
6038aa53 | 122 | strncpy(e->subnqn, subsys_nqn, NVMF_NQN_SIZE); |
a07b4970 CH |
123 | } |
124 | ||
4c652685 SG |
125 | /* |
126 | * nvmet_set_disc_traddr - set a correct discovery log entry traddr | |
127 | * | |
128 | * IP based transports (e.g RDMA) can listen on "any" ipv4/ipv6 addresses | |
129 | * (INADDR_ANY or IN6ADDR_ANY_INIT). The discovery log page traddr reply | |
130 | * must not contain that "any" IP address. If the transport implements | |
131 | * .disc_traddr, use it. this callback will set the discovery traddr | |
132 | * from the req->port address in case the port in question listens | |
133 | * "any" IP address. | |
134 | */ | |
135 | static void nvmet_set_disc_traddr(struct nvmet_req *req, struct nvmet_port *port, | |
136 | char *traddr) | |
137 | { | |
138 | if (req->ops->disc_traddr) | |
139 | req->ops->disc_traddr(req, port, traddr); | |
140 | else | |
141 | memcpy(traddr, port->disc_addr.traddr, NVMF_TRADDR_SIZE); | |
142 | } | |
143 | ||
d808b7f7 KB |
144 | static size_t discovery_log_entries(struct nvmet_req *req) |
145 | { | |
146 | struct nvmet_ctrl *ctrl = req->sq->ctrl; | |
147 | struct nvmet_subsys_link *p; | |
148 | struct nvmet_port *r; | |
149 | size_t entries = 0; | |
150 | ||
151 | list_for_each_entry(p, &req->port->subsystems, entry) { | |
152 | if (!nvmet_host_allowed(p->subsys, ctrl->hostnqn)) | |
153 | continue; | |
154 | entries++; | |
155 | } | |
156 | list_for_each_entry(r, &req->port->referrals, entry) | |
157 | entries++; | |
158 | return entries; | |
159 | } | |
160 | ||
6f86f2c9 | 161 | static void nvmet_execute_disc_get_log_page(struct nvmet_req *req) |
a07b4970 CH |
162 | { |
163 | const int entry_size = sizeof(struct nvmf_disc_rsp_page_entry); | |
164 | struct nvmet_ctrl *ctrl = req->sq->ctrl; | |
165 | struct nvmf_disc_rsp_page_hdr *hdr; | |
d808b7f7 | 166 | u64 offset = nvmet_get_log_page_offset(req->cmd); |
a07b4970 | 167 | size_t data_len = nvmet_get_log_page_len(req->cmd); |
d808b7f7 | 168 | size_t alloc_len; |
a07b4970 CH |
169 | struct nvmet_subsys_link *p; |
170 | struct nvmet_port *r; | |
171 | u32 numrec = 0; | |
172 | u16 status = 0; | |
d808b7f7 KB |
173 | void *buffer; |
174 | ||
136cc1ff | 175 | if (!nvmet_check_transfer_len(req, data_len)) |
e9061c39 CH |
176 | return; |
177 | ||
6f86f2c9 CH |
178 | if (req->cmd->get_log_page.lid != NVME_LOG_DISC) { |
179 | req->error_loc = | |
180 | offsetof(struct nvme_get_log_page_command, lid); | |
79695dcd | 181 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
6f86f2c9 CH |
182 | goto out; |
183 | } | |
184 | ||
d808b7f7 KB |
185 | /* Spec requires dword aligned offsets */ |
186 | if (offset & 0x3) { | |
79695dcd HP |
187 | req->error_loc = |
188 | offsetof(struct nvme_get_log_page_command, lpo); | |
d808b7f7 KB |
189 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
190 | goto out; | |
191 | } | |
a07b4970 CH |
192 | |
193 | /* | |
194 | * Make sure we're passing at least a buffer of response header size. | |
195 | * If host provided data len is less than the header size, only the | |
196 | * number of bytes requested by host will be sent to host. | |
197 | */ | |
d808b7f7 KB |
198 | down_read(&nvmet_config_sem); |
199 | alloc_len = sizeof(*hdr) + entry_size * discovery_log_entries(req); | |
200 | buffer = kzalloc(alloc_len, GFP_KERNEL); | |
201 | if (!buffer) { | |
202 | up_read(&nvmet_config_sem); | |
a07b4970 CH |
203 | status = NVME_SC_INTERNAL; |
204 | goto out; | |
205 | } | |
206 | ||
d808b7f7 | 207 | hdr = buffer; |
a07b4970 | 208 | list_for_each_entry(p, &req->port->subsystems, entry) { |
d808b7f7 KB |
209 | char traddr[NVMF_TRADDR_SIZE]; |
210 | ||
253928ee | 211 | if (!nvmet_host_allowed(p->subsys, ctrl->hostnqn)) |
a07b4970 | 212 | continue; |
d808b7f7 KB |
213 | |
214 | nvmet_set_disc_traddr(req, req->port, traddr); | |
215 | nvmet_format_discovery_entry(hdr, req->port, | |
216 | p->subsys->subsysnqn, traddr, | |
217 | NVME_NQN_NVME, numrec); | |
a07b4970 CH |
218 | numrec++; |
219 | } | |
220 | ||
221 | list_for_each_entry(r, &req->port->referrals, entry) { | |
d808b7f7 KB |
222 | nvmet_format_discovery_entry(hdr, r, |
223 | NVME_DISC_SUBSYS_NAME, | |
224 | r->disc_addr.traddr, | |
225 | NVME_NQN_DISC, numrec); | |
a07b4970 CH |
226 | numrec++; |
227 | } | |
228 | ||
229 | hdr->genctr = cpu_to_le64(nvmet_genctr); | |
230 | hdr->numrec = cpu_to_le64(numrec); | |
231 | hdr->recfmt = cpu_to_le16(0); | |
232 | ||
b662a078 JS |
233 | nvmet_clear_aen_bit(req, NVME_AEN_BIT_DISC_CHANGE); |
234 | ||
a07b4970 CH |
235 | up_read(&nvmet_config_sem); |
236 | ||
d808b7f7 KB |
237 | status = nvmet_copy_to_sgl(req, 0, buffer + offset, data_len); |
238 | kfree(buffer); | |
a07b4970 CH |
239 | out: |
240 | nvmet_req_complete(req, status); | |
241 | } | |
242 | ||
6f86f2c9 | 243 | static void nvmet_execute_disc_identify(struct nvmet_req *req) |
a07b4970 CH |
244 | { |
245 | struct nvmet_ctrl *ctrl = req->sq->ctrl; | |
246 | struct nvme_id_ctrl *id; | |
247 | u16 status = 0; | |
248 | ||
136cc1ff | 249 | if (!nvmet_check_transfer_len(req, NVME_IDENTIFY_DATA_SIZE)) |
e9061c39 CH |
250 | return; |
251 | ||
6f86f2c9 CH |
252 | if (req->cmd->identify.cns != NVME_ID_CNS_CTRL) { |
253 | req->error_loc = offsetof(struct nvme_identify, cns); | |
79695dcd | 254 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
6f86f2c9 CH |
255 | goto out; |
256 | } | |
257 | ||
a07b4970 CH |
258 | id = kzalloc(sizeof(*id), GFP_KERNEL); |
259 | if (!id) { | |
260 | status = NVME_SC_INTERNAL; | |
261 | goto out; | |
262 | } | |
263 | ||
e13b0615 | 264 | memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE); |
a07b4970 | 265 | memset(id->fr, ' ', sizeof(id->fr)); |
0d148efd NG |
266 | memcpy_and_pad(id->mn, sizeof(id->mn), ctrl->subsys->model_number, |
267 | strlen(ctrl->subsys->model_number), ' '); | |
d4b3a174 SG |
268 | memcpy_and_pad(id->fr, sizeof(id->fr), |
269 | UTS_RELEASE, strlen(UTS_RELEASE), ' '); | |
a07b4970 CH |
270 | |
271 | /* no limit on data transfer sizes for now */ | |
272 | id->mdts = 0; | |
273 | id->cntlid = cpu_to_le16(ctrl->cntlid); | |
274 | id->ver = cpu_to_le32(ctrl->subsys->ver); | |
275 | id->lpa = (1 << 2); | |
276 | ||
277 | /* no enforcement soft-limit for maxcmd - pick arbitrary high value */ | |
278 | id->maxcmd = cpu_to_le16(NVMET_MAX_CMD); | |
279 | ||
280 | id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */ | |
6fa350f7 | 281 | if (ctrl->ops->flags & NVMF_KEYED_SGLS) |
a07b4970 | 282 | id->sgls |= cpu_to_le32(1 << 2); |
0d5ee2b2 | 283 | if (req->port->inline_data_size) |
a07b4970 CH |
284 | id->sgls |= cpu_to_le32(1 << 20); |
285 | ||
b662a078 JS |
286 | id->oaes = cpu_to_le32(NVMET_DISC_AEN_CFG_OPTIONAL); |
287 | ||
5eadc9cc | 288 | strlcpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn)); |
a07b4970 CH |
289 | |
290 | status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); | |
291 | ||
292 | kfree(id); | |
293 | out: | |
294 | nvmet_req_complete(req, status); | |
295 | } | |
296 | ||
6a8ec0ac JS |
297 | static void nvmet_execute_disc_set_features(struct nvmet_req *req) |
298 | { | |
b7c8f366 | 299 | u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10); |
6a8ec0ac JS |
300 | u16 stat; |
301 | ||
136cc1ff | 302 | if (!nvmet_check_transfer_len(req, 0)) |
e9061c39 CH |
303 | return; |
304 | ||
6a8ec0ac JS |
305 | switch (cdw10 & 0xff) { |
306 | case NVME_FEAT_KATO: | |
307 | stat = nvmet_set_feat_kato(req); | |
308 | break; | |
309 | case NVME_FEAT_ASYNC_EVENT: | |
310 | stat = nvmet_set_feat_async_event(req, | |
311 | NVMET_DISC_AEN_CFG_OPTIONAL); | |
312 | break; | |
313 | default: | |
84faf42b CK |
314 | req->error_loc = |
315 | offsetof(struct nvme_common_command, cdw10); | |
6a8ec0ac JS |
316 | stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
317 | break; | |
318 | } | |
319 | ||
320 | nvmet_req_complete(req, stat); | |
321 | } | |
322 | ||
323 | static void nvmet_execute_disc_get_features(struct nvmet_req *req) | |
324 | { | |
b7c8f366 | 325 | u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10); |
6a8ec0ac JS |
326 | u16 stat = 0; |
327 | ||
136cc1ff | 328 | if (!nvmet_check_transfer_len(req, 0)) |
e9061c39 CH |
329 | return; |
330 | ||
6a8ec0ac JS |
331 | switch (cdw10 & 0xff) { |
332 | case NVME_FEAT_KATO: | |
333 | nvmet_get_feat_kato(req); | |
334 | break; | |
335 | case NVME_FEAT_ASYNC_EVENT: | |
336 | nvmet_get_feat_async_event(req); | |
337 | break; | |
338 | default: | |
84faf42b CK |
339 | req->error_loc = |
340 | offsetof(struct nvme_common_command, cdw10); | |
6a8ec0ac JS |
341 | stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
342 | break; | |
343 | } | |
344 | ||
345 | nvmet_req_complete(req, stat); | |
346 | } | |
347 | ||
64a0ca88 | 348 | u16 nvmet_parse_discovery_cmd(struct nvmet_req *req) |
a07b4970 CH |
349 | { |
350 | struct nvme_command *cmd = req->cmd; | |
351 | ||
a07b4970 | 352 | if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) { |
4151dd9a PP |
353 | pr_err("got cmd %d while not ready\n", |
354 | cmd->common.opcode); | |
84faf42b CK |
355 | req->error_loc = |
356 | offsetof(struct nvme_common_command, opcode); | |
a07b4970 CH |
357 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
358 | } | |
359 | ||
360 | switch (cmd->common.opcode) { | |
6a8ec0ac JS |
361 | case nvme_admin_set_features: |
362 | req->execute = nvmet_execute_disc_set_features; | |
6a8ec0ac JS |
363 | return 0; |
364 | case nvme_admin_get_features: | |
365 | req->execute = nvmet_execute_disc_get_features; | |
6a8ec0ac JS |
366 | return 0; |
367 | case nvme_admin_async_event: | |
368 | req->execute = nvmet_execute_async_event; | |
6a8ec0ac | 369 | return 0; |
f9362ac1 JS |
370 | case nvme_admin_keep_alive: |
371 | req->execute = nvmet_execute_keep_alive; | |
f9362ac1 | 372 | return 0; |
a07b4970 | 373 | case nvme_admin_get_log_page: |
6f86f2c9 CH |
374 | req->execute = nvmet_execute_disc_get_log_page; |
375 | return 0; | |
a07b4970 | 376 | case nvme_admin_identify: |
6f86f2c9 CH |
377 | req->execute = nvmet_execute_disc_identify; |
378 | return 0; | |
a07b4970 | 379 | default: |
3651aaac | 380 | pr_debug("unhandled cmd %d\n", cmd->common.opcode); |
84faf42b | 381 | req->error_loc = offsetof(struct nvme_common_command, opcode); |
a07b4970 CH |
382 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
383 | } | |
384 | ||
a07b4970 CH |
385 | } |
386 | ||
387 | int __init nvmet_init_discovery(void) | |
388 | { | |
389 | nvmet_disc_subsys = | |
390 | nvmet_subsys_alloc(NVME_DISC_SUBSYS_NAME, NVME_NQN_DISC); | |
1179d337 | 391 | return PTR_ERR_OR_ZERO(nvmet_disc_subsys); |
a07b4970 CH |
392 | } |
393 | ||
394 | void nvmet_exit_discovery(void) | |
395 | { | |
396 | nvmet_subsys_put(nvmet_disc_subsys); | |
397 | } |