1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Common code for the NVMe target.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/random.h>
9 #include <linux/rculist.h>
10 #include <linux/pci-p2pdma.h>
11 #include <linux/scatterlist.h>
12
13 #include <generated/utsrelease.h>
14
15 #define CREATE_TRACE_POINTS
16 #include "trace.h"
17
18 #include "nvmet.h"
19 #include "debugfs.h"
20
21 struct kmem_cache *nvmet_bvec_cache;
22 struct workqueue_struct *buffered_io_wq;
23 struct workqueue_struct *zbd_wq;
24 static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
25 static DEFINE_IDA(cntlid_ida);
26
27 struct workqueue_struct *nvmet_wq;
28 EXPORT_SYMBOL_GPL(nvmet_wq);
29
30 /*
31 * This read/write semaphore is used to synchronize access to configuration
32 * information on a target system that will result in discovery log page
33 * information change for at least one host.
34 * The full list of resources to protected by this semaphore is:
35 *
36 * - subsystems list
37 * - per-subsystem allowed hosts list
38 * - allow_any_host subsystem attribute
39 * - nvmet_genctr
40 * - the nvmet_transports array
41 *
42 * When updating any of those lists/structures write lock should be obtained,
43 * while when reading (popolating discovery log page or checking host-subsystem
44 * link) read lock is obtained to allow concurrent reads.
45 */
46 DECLARE_RWSEM(nvmet_config_sem);
47
48 u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
49 u64 nvmet_ana_chgcnt;
50 DECLARE_RWSEM(nvmet_ana_sem);
51
errno_to_nvme_status(struct nvmet_req * req,int errno)52 inline u16 errno_to_nvme_status(struct nvmet_req *req, int errno)
53 {
54 switch (errno) {
55 case 0:
56 return NVME_SC_SUCCESS;
57 case -ENOSPC:
58 req->error_loc = offsetof(struct nvme_rw_command, length);
59 return NVME_SC_CAP_EXCEEDED | NVME_STATUS_DNR;
60 case -EREMOTEIO:
61 req->error_loc = offsetof(struct nvme_rw_command, slba);
62 return NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
63 case -EOPNOTSUPP:
64 req->error_loc = offsetof(struct nvme_common_command, opcode);
65 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
66 case -ENODATA:
67 req->error_loc = offsetof(struct nvme_rw_command, nsid);
68 return NVME_SC_ACCESS_DENIED;
69 case -EIO:
70 fallthrough;
71 default:
72 req->error_loc = offsetof(struct nvme_common_command, opcode);
73 return NVME_SC_INTERNAL | NVME_STATUS_DNR;
74 }
75 }
76
nvmet_report_invalid_opcode(struct nvmet_req * req)77 u16 nvmet_report_invalid_opcode(struct nvmet_req *req)
78 {
79 pr_debug("unhandled cmd %d on qid %d\n", req->cmd->common.opcode,
80 req->sq->qid);
81
82 req->error_loc = offsetof(struct nvme_common_command, opcode);
83 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
84 }
85
86 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
87 const char *subsysnqn);
88
nvmet_copy_to_sgl(struct nvmet_req * req,off_t off,const void * buf,size_t len)89 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
90 size_t len)
91 {
92 if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len) {
93 req->error_loc = offsetof(struct nvme_common_command, dptr);
94 return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
95 }
96 return 0;
97 }
98
nvmet_copy_from_sgl(struct nvmet_req * req,off_t off,void * buf,size_t len)99 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
100 {
101 if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len) {
102 req->error_loc = offsetof(struct nvme_common_command, dptr);
103 return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
104 }
105 return 0;
106 }
107
nvmet_zero_sgl(struct nvmet_req * req,off_t off,size_t len)108 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len)
109 {
110 if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len) {
111 req->error_loc = offsetof(struct nvme_common_command, dptr);
112 return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
113 }
114 return 0;
115 }
116
nvmet_max_nsid(struct nvmet_subsys * subsys)117 static u32 nvmet_max_nsid(struct nvmet_subsys *subsys)
118 {
119 struct nvmet_ns *cur;
120 unsigned long idx;
121 u32 nsid = 0;
122
123 xa_for_each(&subsys->namespaces, idx, cur)
124 nsid = cur->nsid;
125
126 return nsid;
127 }
128
nvmet_async_event_result(struct nvmet_async_event * aen)129 static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
130 {
131 return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
132 }
133
nvmet_async_events_failall(struct nvmet_ctrl * ctrl)134 static void nvmet_async_events_failall(struct nvmet_ctrl *ctrl)
135 {
136 struct nvmet_req *req;
137
138 mutex_lock(&ctrl->lock);
139 while (ctrl->nr_async_event_cmds) {
140 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
141 mutex_unlock(&ctrl->lock);
142 nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_STATUS_DNR);
143 mutex_lock(&ctrl->lock);
144 }
145 mutex_unlock(&ctrl->lock);
146 }
147
nvmet_async_events_process(struct nvmet_ctrl * ctrl)148 static void nvmet_async_events_process(struct nvmet_ctrl *ctrl)
149 {
150 struct nvmet_async_event *aen;
151 struct nvmet_req *req;
152
153 mutex_lock(&ctrl->lock);
154 while (ctrl->nr_async_event_cmds && !list_empty(&ctrl->async_events)) {
155 aen = list_first_entry(&ctrl->async_events,
156 struct nvmet_async_event, entry);
157 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
158 nvmet_set_result(req, nvmet_async_event_result(aen));
159
160 list_del(&aen->entry);
161 kfree(aen);
162
163 mutex_unlock(&ctrl->lock);
164 trace_nvmet_async_event(ctrl, req->cqe->result.u32);
165 nvmet_req_complete(req, 0);
166 mutex_lock(&ctrl->lock);
167 }
168 mutex_unlock(&ctrl->lock);
169 }
170
nvmet_async_events_free(struct nvmet_ctrl * ctrl)171 static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
172 {
173 struct nvmet_async_event *aen, *tmp;
174
175 mutex_lock(&ctrl->lock);
176 list_for_each_entry_safe(aen, tmp, &ctrl->async_events, entry) {
177 list_del(&aen->entry);
178 kfree(aen);
179 }
180 mutex_unlock(&ctrl->lock);
181 }
182
nvmet_async_event_work(struct work_struct * work)183 static void nvmet_async_event_work(struct work_struct *work)
184 {
185 struct nvmet_ctrl *ctrl =
186 container_of(work, struct nvmet_ctrl, async_event_work);
187
188 nvmet_async_events_process(ctrl);
189 }
190
nvmet_add_async_event(struct nvmet_ctrl * ctrl,u8 event_type,u8 event_info,u8 log_page)191 void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
192 u8 event_info, u8 log_page)
193 {
194 struct nvmet_async_event *aen;
195
196 aen = kmalloc(sizeof(*aen), GFP_KERNEL);
197 if (!aen)
198 return;
199
200 aen->event_type = event_type;
201 aen->event_info = event_info;
202 aen->log_page = log_page;
203
204 mutex_lock(&ctrl->lock);
205 list_add_tail(&aen->entry, &ctrl->async_events);
206 mutex_unlock(&ctrl->lock);
207
208 queue_work(nvmet_wq, &ctrl->async_event_work);
209 }
210
nvmet_add_to_changed_ns_log(struct nvmet_ctrl * ctrl,__le32 nsid)211 static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid)
212 {
213 u32 i;
214
215 mutex_lock(&ctrl->lock);
216 if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES)
217 goto out_unlock;
218
219 for (i = 0; i < ctrl->nr_changed_ns; i++) {
220 if (ctrl->changed_ns_list[i] == nsid)
221 goto out_unlock;
222 }
223
224 if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) {
225 ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff);
226 ctrl->nr_changed_ns = U32_MAX;
227 goto out_unlock;
228 }
229
230 ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid;
231 out_unlock:
232 mutex_unlock(&ctrl->lock);
233 }
234
nvmet_ns_changed(struct nvmet_subsys * subsys,u32 nsid)235 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid)
236 {
237 struct nvmet_ctrl *ctrl;
238
239 lockdep_assert_held(&subsys->lock);
240
241 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
242 nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid));
243 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_NS_ATTR))
244 continue;
245 nvmet_add_async_event(ctrl, NVME_AER_NOTICE,
246 NVME_AER_NOTICE_NS_CHANGED,
247 NVME_LOG_CHANGED_NS);
248 }
249 }
250
nvmet_send_ana_event(struct nvmet_subsys * subsys,struct nvmet_port * port)251 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
252 struct nvmet_port *port)
253 {
254 struct nvmet_ctrl *ctrl;
255
256 mutex_lock(&subsys->lock);
257 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
258 if (port && ctrl->port != port)
259 continue;
260 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_ANA_CHANGE))
261 continue;
262 nvmet_add_async_event(ctrl, NVME_AER_NOTICE,
263 NVME_AER_NOTICE_ANA, NVME_LOG_ANA);
264 }
265 mutex_unlock(&subsys->lock);
266 }
267
nvmet_port_send_ana_event(struct nvmet_port * port)268 void nvmet_port_send_ana_event(struct nvmet_port *port)
269 {
270 struct nvmet_subsys_link *p;
271
272 down_read(&nvmet_config_sem);
273 list_for_each_entry(p, &port->subsystems, entry)
274 nvmet_send_ana_event(p->subsys, port);
275 up_read(&nvmet_config_sem);
276 }
277
nvmet_register_transport(const struct nvmet_fabrics_ops * ops)278 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops)
279 {
280 int ret = 0;
281
282 down_write(&nvmet_config_sem);
283 if (nvmet_transports[ops->type])
284 ret = -EINVAL;
285 else
286 nvmet_transports[ops->type] = ops;
287 up_write(&nvmet_config_sem);
288
289 return ret;
290 }
291 EXPORT_SYMBOL_GPL(nvmet_register_transport);
292
nvmet_unregister_transport(const struct nvmet_fabrics_ops * ops)293 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
294 {
295 down_write(&nvmet_config_sem);
296 nvmet_transports[ops->type] = NULL;
297 up_write(&nvmet_config_sem);
298 }
299 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
300
nvmet_port_del_ctrls(struct nvmet_port * port,struct nvmet_subsys * subsys)301 void nvmet_port_del_ctrls(struct nvmet_port *port, struct nvmet_subsys *subsys)
302 {
303 struct nvmet_ctrl *ctrl;
304
305 mutex_lock(&subsys->lock);
306 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
307 if (ctrl->port == port)
308 ctrl->ops->delete_ctrl(ctrl);
309 }
310 mutex_unlock(&subsys->lock);
311 }
312
nvmet_enable_port(struct nvmet_port * port)313 int nvmet_enable_port(struct nvmet_port *port)
314 {
315 const struct nvmet_fabrics_ops *ops;
316 int ret;
317
318 lockdep_assert_held(&nvmet_config_sem);
319
320 ops = nvmet_transports[port->disc_addr.trtype];
321 if (!ops) {
322 up_write(&nvmet_config_sem);
323 request_module("nvmet-transport-%d", port->disc_addr.trtype);
324 down_write(&nvmet_config_sem);
325 ops = nvmet_transports[port->disc_addr.trtype];
326 if (!ops) {
327 pr_err("transport type %d not supported\n",
328 port->disc_addr.trtype);
329 return -EINVAL;
330 }
331 }
332
333 if (!try_module_get(ops->owner))
334 return -EINVAL;
335
336 /*
337 * If the user requested PI support and the transport isn't pi capable,
338 * don't enable the port.
339 */
340 if (port->pi_enable && !(ops->flags & NVMF_METADATA_SUPPORTED)) {
341 pr_err("T10-PI is not supported by transport type %d\n",
342 port->disc_addr.trtype);
343 ret = -EINVAL;
344 goto out_put;
345 }
346
347 ret = ops->add_port(port);
348 if (ret)
349 goto out_put;
350
351 /* If the transport didn't set inline_data_size, then disable it. */
352 if (port->inline_data_size < 0)
353 port->inline_data_size = 0;
354
355 /*
356 * If the transport didn't set the max_queue_size properly, then clamp
357 * it to the target limits. Also set default values in case the
358 * transport didn't set it at all.
359 */
360 if (port->max_queue_size < 0)
361 port->max_queue_size = NVMET_MAX_QUEUE_SIZE;
362 else
363 port->max_queue_size = clamp_t(int, port->max_queue_size,
364 NVMET_MIN_QUEUE_SIZE,
365 NVMET_MAX_QUEUE_SIZE);
366
367 port->enabled = true;
368 port->tr_ops = ops;
369 return 0;
370
371 out_put:
372 module_put(ops->owner);
373 return ret;
374 }
375
nvmet_disable_port(struct nvmet_port * port)376 void nvmet_disable_port(struct nvmet_port *port)
377 {
378 const struct nvmet_fabrics_ops *ops;
379
380 lockdep_assert_held(&nvmet_config_sem);
381
382 port->enabled = false;
383 port->tr_ops = NULL;
384
385 ops = nvmet_transports[port->disc_addr.trtype];
386 ops->remove_port(port);
387 module_put(ops->owner);
388 }
389
nvmet_keep_alive_timer(struct work_struct * work)390 static void nvmet_keep_alive_timer(struct work_struct *work)
391 {
392 struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
393 struct nvmet_ctrl, ka_work);
394 bool reset_tbkas = ctrl->reset_tbkas;
395
396 ctrl->reset_tbkas = false;
397 if (reset_tbkas) {
398 pr_debug("ctrl %d reschedule traffic based keep-alive timer\n",
399 ctrl->cntlid);
400 queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ);
401 return;
402 }
403
404 pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
405 ctrl->cntlid, ctrl->kato);
406
407 nvmet_ctrl_fatal_error(ctrl);
408 }
409
nvmet_start_keep_alive_timer(struct nvmet_ctrl * ctrl)410 void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
411 {
412 if (unlikely(ctrl->kato == 0))
413 return;
414
415 pr_debug("ctrl %d start keep-alive timer for %d secs\n",
416 ctrl->cntlid, ctrl->kato);
417
418 queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ);
419 }
420
nvmet_stop_keep_alive_timer(struct nvmet_ctrl * ctrl)421 void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
422 {
423 if (unlikely(ctrl->kato == 0))
424 return;
425
426 pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
427
428 cancel_delayed_work_sync(&ctrl->ka_work);
429 }
430
nvmet_req_find_ns(struct nvmet_req * req)431 u16 nvmet_req_find_ns(struct nvmet_req *req)
432 {
433 u32 nsid = le32_to_cpu(req->cmd->common.nsid);
434 struct nvmet_subsys *subsys = nvmet_req_subsys(req);
435
436 req->ns = xa_load(&subsys->namespaces, nsid);
437 if (unlikely(!req->ns)) {
438 req->error_loc = offsetof(struct nvme_common_command, nsid);
439 if (nvmet_subsys_nsid_exists(subsys, nsid))
440 return NVME_SC_INTERNAL_PATH_ERROR;
441 return NVME_SC_INVALID_NS | NVME_STATUS_DNR;
442 }
443
444 percpu_ref_get(&req->ns->ref);
445 return NVME_SC_SUCCESS;
446 }
447
nvmet_destroy_namespace(struct percpu_ref * ref)448 static void nvmet_destroy_namespace(struct percpu_ref *ref)
449 {
450 struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
451
452 complete(&ns->disable_done);
453 }
454
nvmet_put_namespace(struct nvmet_ns * ns)455 void nvmet_put_namespace(struct nvmet_ns *ns)
456 {
457 percpu_ref_put(&ns->ref);
458 }
459
nvmet_ns_dev_disable(struct nvmet_ns * ns)460 static void nvmet_ns_dev_disable(struct nvmet_ns *ns)
461 {
462 nvmet_bdev_ns_disable(ns);
463 nvmet_file_ns_disable(ns);
464 }
465
nvmet_p2pmem_ns_enable(struct nvmet_ns * ns)466 static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns)
467 {
468 int ret;
469 struct pci_dev *p2p_dev;
470
471 if (!ns->use_p2pmem)
472 return 0;
473
474 if (!ns->bdev) {
475 pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n");
476 return -EINVAL;
477 }
478
479 if (!blk_queue_pci_p2pdma(ns->bdev->bd_disk->queue)) {
480 pr_err("peer-to-peer DMA is not supported by the driver of %s\n",
481 ns->device_path);
482 return -EINVAL;
483 }
484
485 if (ns->p2p_dev) {
486 ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true);
487 if (ret < 0)
488 return -EINVAL;
489 } else {
490 /*
491 * Right now we just check that there is p2pmem available so
492 * we can report an error to the user right away if there
493 * is not. We'll find the actual device to use once we
494 * setup the controller when the port's device is available.
495 */
496
497 p2p_dev = pci_p2pmem_find(nvmet_ns_dev(ns));
498 if (!p2p_dev) {
499 pr_err("no peer-to-peer memory is available for %s\n",
500 ns->device_path);
501 return -EINVAL;
502 }
503
504 pci_dev_put(p2p_dev);
505 }
506
507 return 0;
508 }
509
510 /*
511 * Note: ctrl->subsys->lock should be held when calling this function
512 */
nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl * ctrl,struct nvmet_ns * ns)513 static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl,
514 struct nvmet_ns *ns)
515 {
516 struct device *clients[2];
517 struct pci_dev *p2p_dev;
518 int ret;
519
520 if (!ctrl->p2p_client || !ns->use_p2pmem)
521 return;
522
523 if (ns->p2p_dev) {
524 ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true);
525 if (ret < 0)
526 return;
527
528 p2p_dev = pci_dev_get(ns->p2p_dev);
529 } else {
530 clients[0] = ctrl->p2p_client;
531 clients[1] = nvmet_ns_dev(ns);
532
533 p2p_dev = pci_p2pmem_find_many(clients, ARRAY_SIZE(clients));
534 if (!p2p_dev) {
535 pr_err("no peer-to-peer memory is available that's supported by %s and %s\n",
536 dev_name(ctrl->p2p_client), ns->device_path);
537 return;
538 }
539 }
540
541 ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev);
542 if (ret < 0)
543 pci_dev_put(p2p_dev);
544
545 pr_info("using p2pmem on %s for nsid %d\n", pci_name(p2p_dev),
546 ns->nsid);
547 }
548
nvmet_ns_revalidate(struct nvmet_ns * ns)549 bool nvmet_ns_revalidate(struct nvmet_ns *ns)
550 {
551 loff_t oldsize = ns->size;
552
553 if (ns->bdev)
554 nvmet_bdev_ns_revalidate(ns);
555 else
556 nvmet_file_ns_revalidate(ns);
557
558 return oldsize != ns->size;
559 }
560
nvmet_ns_enable(struct nvmet_ns * ns)561 int nvmet_ns_enable(struct nvmet_ns *ns)
562 {
563 struct nvmet_subsys *subsys = ns->subsys;
564 struct nvmet_ctrl *ctrl;
565 int ret;
566
567 mutex_lock(&subsys->lock);
568 ret = 0;
569
570 if (nvmet_is_passthru_subsys(subsys)) {
571 pr_info("cannot enable both passthru and regular namespaces for a single subsystem");
572 goto out_unlock;
573 }
574
575 if (ns->enabled)
576 goto out_unlock;
577
578 ret = -EMFILE;
579 if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
580 goto out_unlock;
581
582 ret = nvmet_bdev_ns_enable(ns);
583 if (ret == -ENOTBLK)
584 ret = nvmet_file_ns_enable(ns);
585 if (ret)
586 goto out_unlock;
587
588 ret = nvmet_p2pmem_ns_enable(ns);
589 if (ret)
590 goto out_dev_disable;
591
592 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
593 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
594
595 ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
596 0, GFP_KERNEL);
597 if (ret)
598 goto out_dev_put;
599
600 if (ns->nsid > subsys->max_nsid)
601 subsys->max_nsid = ns->nsid;
602
603 ret = xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL);
604 if (ret)
605 goto out_restore_subsys_maxnsid;
606
607 subsys->nr_namespaces++;
608
609 nvmet_ns_changed(subsys, ns->nsid);
610 ns->enabled = true;
611 ret = 0;
612 out_unlock:
613 mutex_unlock(&subsys->lock);
614 return ret;
615
616 out_restore_subsys_maxnsid:
617 subsys->max_nsid = nvmet_max_nsid(subsys);
618 percpu_ref_exit(&ns->ref);
619 out_dev_put:
620 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
621 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
622 out_dev_disable:
623 nvmet_ns_dev_disable(ns);
624 goto out_unlock;
625 }
626
nvmet_ns_disable(struct nvmet_ns * ns)627 void nvmet_ns_disable(struct nvmet_ns *ns)
628 {
629 struct nvmet_subsys *subsys = ns->subsys;
630 struct nvmet_ctrl *ctrl;
631
632 mutex_lock(&subsys->lock);
633 if (!ns->enabled)
634 goto out_unlock;
635
636 ns->enabled = false;
637 xa_erase(&ns->subsys->namespaces, ns->nsid);
638 if (ns->nsid == subsys->max_nsid)
639 subsys->max_nsid = nvmet_max_nsid(subsys);
640
641 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
642 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
643
644 mutex_unlock(&subsys->lock);
645
646 /*
647 * Now that we removed the namespaces from the lookup list, we
648 * can kill the per_cpu ref and wait for any remaining references
649 * to be dropped, as well as a RCU grace period for anyone only
650 * using the namepace under rcu_read_lock(). Note that we can't
651 * use call_rcu here as we need to ensure the namespaces have
652 * been fully destroyed before unloading the module.
653 */
654 percpu_ref_kill(&ns->ref);
655 synchronize_rcu();
656 wait_for_completion(&ns->disable_done);
657 percpu_ref_exit(&ns->ref);
658
659 mutex_lock(&subsys->lock);
660
661 subsys->nr_namespaces--;
662 nvmet_ns_changed(subsys, ns->nsid);
663 nvmet_ns_dev_disable(ns);
664 out_unlock:
665 mutex_unlock(&subsys->lock);
666 }
667
nvmet_ns_free(struct nvmet_ns * ns)668 void nvmet_ns_free(struct nvmet_ns *ns)
669 {
670 nvmet_ns_disable(ns);
671
672 down_write(&nvmet_ana_sem);
673 nvmet_ana_group_enabled[ns->anagrpid]--;
674 up_write(&nvmet_ana_sem);
675
676 kfree(ns->device_path);
677 kfree(ns);
678 }
679
nvmet_ns_alloc(struct nvmet_subsys * subsys,u32 nsid)680 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
681 {
682 struct nvmet_ns *ns;
683
684 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
685 if (!ns)
686 return NULL;
687
688 init_completion(&ns->disable_done);
689
690 ns->nsid = nsid;
691 ns->subsys = subsys;
692
693 down_write(&nvmet_ana_sem);
694 ns->anagrpid = NVMET_DEFAULT_ANA_GRPID;
695 nvmet_ana_group_enabled[ns->anagrpid]++;
696 up_write(&nvmet_ana_sem);
697
698 uuid_gen(&ns->uuid);
699 ns->buffered_io = false;
700 ns->csi = NVME_CSI_NVM;
701
702 return ns;
703 }
704
nvmet_update_sq_head(struct nvmet_req * req)705 static void nvmet_update_sq_head(struct nvmet_req *req)
706 {
707 if (req->sq->size) {
708 u32 old_sqhd, new_sqhd;
709
710 old_sqhd = READ_ONCE(req->sq->sqhd);
711 do {
712 new_sqhd = (old_sqhd + 1) % req->sq->size;
713 } while (!try_cmpxchg(&req->sq->sqhd, &old_sqhd, new_sqhd));
714 }
715 req->cqe->sq_head = cpu_to_le16(req->sq->sqhd & 0x0000FFFF);
716 }
717
nvmet_set_error(struct nvmet_req * req,u16 status)718 static void nvmet_set_error(struct nvmet_req *req, u16 status)
719 {
720 struct nvmet_ctrl *ctrl = req->sq->ctrl;
721 struct nvme_error_slot *new_error_slot;
722 unsigned long flags;
723
724 req->cqe->status = cpu_to_le16(status << 1);
725
726 if (!ctrl || req->error_loc == NVMET_NO_ERROR_LOC)
727 return;
728
729 spin_lock_irqsave(&ctrl->error_lock, flags);
730 ctrl->err_counter++;
731 new_error_slot =
732 &ctrl->slots[ctrl->err_counter % NVMET_ERROR_LOG_SLOTS];
733
734 new_error_slot->error_count = cpu_to_le64(ctrl->err_counter);
735 new_error_slot->sqid = cpu_to_le16(req->sq->qid);
736 new_error_slot->cmdid = cpu_to_le16(req->cmd->common.command_id);
737 new_error_slot->status_field = cpu_to_le16(status << 1);
738 new_error_slot->param_error_location = cpu_to_le16(req->error_loc);
739 new_error_slot->lba = cpu_to_le64(req->error_slba);
740 new_error_slot->nsid = req->cmd->common.nsid;
741 spin_unlock_irqrestore(&ctrl->error_lock, flags);
742
743 /* set the more bit for this request */
744 req->cqe->status |= cpu_to_le16(1 << 14);
745 }
746
__nvmet_req_complete(struct nvmet_req * req,u16 status)747 static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
748 {
749 struct nvmet_ns *ns = req->ns;
750
751 if (!req->sq->sqhd_disabled)
752 nvmet_update_sq_head(req);
753 req->cqe->sq_id = cpu_to_le16(req->sq->qid);
754 req->cqe->command_id = req->cmd->common.command_id;
755
756 if (unlikely(status))
757 nvmet_set_error(req, status);
758
759 trace_nvmet_req_complete(req);
760
761 req->ops->queue_response(req);
762 if (ns)
763 nvmet_put_namespace(ns);
764 }
765
nvmet_req_complete(struct nvmet_req * req,u16 status)766 void nvmet_req_complete(struct nvmet_req *req, u16 status)
767 {
768 struct nvmet_sq *sq = req->sq;
769
770 __nvmet_req_complete(req, status);
771 percpu_ref_put(&sq->ref);
772 }
773 EXPORT_SYMBOL_GPL(nvmet_req_complete);
774
nvmet_cq_setup(struct nvmet_ctrl * ctrl,struct nvmet_cq * cq,u16 qid,u16 size)775 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
776 u16 qid, u16 size)
777 {
778 cq->qid = qid;
779 cq->size = size;
780 }
781
nvmet_sq_setup(struct nvmet_ctrl * ctrl,struct nvmet_sq * sq,u16 qid,u16 size)782 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
783 u16 qid, u16 size)
784 {
785 sq->sqhd = 0;
786 sq->qid = qid;
787 sq->size = size;
788
789 ctrl->sqs[qid] = sq;
790 }
791
nvmet_confirm_sq(struct percpu_ref * ref)792 static void nvmet_confirm_sq(struct percpu_ref *ref)
793 {
794 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
795
796 complete(&sq->confirm_done);
797 }
798
nvmet_sq_destroy(struct nvmet_sq * sq)799 void nvmet_sq_destroy(struct nvmet_sq *sq)
800 {
801 struct nvmet_ctrl *ctrl = sq->ctrl;
802
803 /*
804 * If this is the admin queue, complete all AERs so that our
805 * queue doesn't have outstanding requests on it.
806 */
807 if (ctrl && ctrl->sqs && ctrl->sqs[0] == sq)
808 nvmet_async_events_failall(ctrl);
809 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
810 wait_for_completion(&sq->confirm_done);
811 wait_for_completion(&sq->free_done);
812 percpu_ref_exit(&sq->ref);
813 nvmet_auth_sq_free(sq);
814
815 /*
816 * we must reference the ctrl again after waiting for inflight IO
817 * to complete. Because admin connect may have sneaked in after we
818 * store sq->ctrl locally, but before we killed the percpu_ref. the
819 * admin connect allocates and assigns sq->ctrl, which now needs a
820 * final ref put, as this ctrl is going away.
821 */
822 ctrl = sq->ctrl;
823
824 if (ctrl) {
825 /*
826 * The teardown flow may take some time, and the host may not
827 * send us keep-alive during this period, hence reset the
828 * traffic based keep-alive timer so we don't trigger a
829 * controller teardown as a result of a keep-alive expiration.
830 */
831 ctrl->reset_tbkas = true;
832 sq->ctrl->sqs[sq->qid] = NULL;
833 nvmet_ctrl_put(ctrl);
834 sq->ctrl = NULL; /* allows reusing the queue later */
835 }
836 }
837 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
838
nvmet_sq_free(struct percpu_ref * ref)839 static void nvmet_sq_free(struct percpu_ref *ref)
840 {
841 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
842
843 complete(&sq->free_done);
844 }
845
nvmet_sq_init(struct nvmet_sq * sq)846 int nvmet_sq_init(struct nvmet_sq *sq)
847 {
848 int ret;
849
850 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
851 if (ret) {
852 pr_err("percpu_ref init failed!\n");
853 return ret;
854 }
855 init_completion(&sq->free_done);
856 init_completion(&sq->confirm_done);
857 nvmet_auth_sq_init(sq);
858
859 return 0;
860 }
861 EXPORT_SYMBOL_GPL(nvmet_sq_init);
862
nvmet_check_ana_state(struct nvmet_port * port,struct nvmet_ns * ns)863 static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
864 struct nvmet_ns *ns)
865 {
866 enum nvme_ana_state state = port->ana_state[ns->anagrpid];
867
868 if (unlikely(state == NVME_ANA_INACCESSIBLE))
869 return NVME_SC_ANA_INACCESSIBLE;
870 if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
871 return NVME_SC_ANA_PERSISTENT_LOSS;
872 if (unlikely(state == NVME_ANA_CHANGE))
873 return NVME_SC_ANA_TRANSITION;
874 return 0;
875 }
876
nvmet_io_cmd_check_access(struct nvmet_req * req)877 static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
878 {
879 if (unlikely(req->ns->readonly)) {
880 switch (req->cmd->common.opcode) {
881 case nvme_cmd_read:
882 case nvme_cmd_flush:
883 break;
884 default:
885 return NVME_SC_NS_WRITE_PROTECTED;
886 }
887 }
888
889 return 0;
890 }
891
nvmet_parse_io_cmd(struct nvmet_req * req)892 static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
893 {
894 struct nvme_command *cmd = req->cmd;
895 u16 ret;
896
897 if (nvme_is_fabrics(cmd))
898 return nvmet_parse_fabrics_io_cmd(req);
899
900 if (unlikely(!nvmet_check_auth_status(req)))
901 return NVME_SC_AUTH_REQUIRED | NVME_STATUS_DNR;
902
903 ret = nvmet_check_ctrl_status(req);
904 if (unlikely(ret))
905 return ret;
906
907 if (nvmet_is_passthru_req(req))
908 return nvmet_parse_passthru_io_cmd(req);
909
910 ret = nvmet_req_find_ns(req);
911 if (unlikely(ret))
912 return ret;
913
914 ret = nvmet_check_ana_state(req->port, req->ns);
915 if (unlikely(ret)) {
916 req->error_loc = offsetof(struct nvme_common_command, nsid);
917 return ret;
918 }
919 ret = nvmet_io_cmd_check_access(req);
920 if (unlikely(ret)) {
921 req->error_loc = offsetof(struct nvme_common_command, nsid);
922 return ret;
923 }
924
925 switch (req->ns->csi) {
926 case NVME_CSI_NVM:
927 if (req->ns->file)
928 return nvmet_file_parse_io_cmd(req);
929 return nvmet_bdev_parse_io_cmd(req);
930 case NVME_CSI_ZNS:
931 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED))
932 return nvmet_bdev_zns_parse_io_cmd(req);
933 return NVME_SC_INVALID_IO_CMD_SET;
934 default:
935 return NVME_SC_INVALID_IO_CMD_SET;
936 }
937 }
938
nvmet_req_init(struct nvmet_req * req,struct nvmet_cq * cq,struct nvmet_sq * sq,const struct nvmet_fabrics_ops * ops)939 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
940 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
941 {
942 u8 flags = req->cmd->common.flags;
943 u16 status;
944
945 req->cq = cq;
946 req->sq = sq;
947 req->ops = ops;
948 req->sg = NULL;
949 req->metadata_sg = NULL;
950 req->sg_cnt = 0;
951 req->metadata_sg_cnt = 0;
952 req->transfer_len = 0;
953 req->metadata_len = 0;
954 req->cqe->result.u64 = 0;
955 req->cqe->status = 0;
956 req->cqe->sq_head = 0;
957 req->ns = NULL;
958 req->error_loc = NVMET_NO_ERROR_LOC;
959 req->error_slba = 0;
960
961 /* no support for fused commands yet */
962 if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
963 req->error_loc = offsetof(struct nvme_common_command, flags);
964 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
965 goto fail;
966 }
967
968 /*
969 * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
970 * contains an address of a single contiguous physical buffer that is
971 * byte aligned.
972 */
973 if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
974 req->error_loc = offsetof(struct nvme_common_command, flags);
975 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
976 goto fail;
977 }
978
979 if (unlikely(!req->sq->ctrl))
980 /* will return an error for any non-connect command: */
981 status = nvmet_parse_connect_cmd(req);
982 else if (likely(req->sq->qid != 0))
983 status = nvmet_parse_io_cmd(req);
984 else
985 status = nvmet_parse_admin_cmd(req);
986
987 if (status)
988 goto fail;
989
990 trace_nvmet_req_init(req, req->cmd);
991
992 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
993 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
994 goto fail;
995 }
996
997 if (sq->ctrl)
998 sq->ctrl->reset_tbkas = true;
999
1000 return true;
1001
1002 fail:
1003 __nvmet_req_complete(req, status);
1004 return false;
1005 }
1006 EXPORT_SYMBOL_GPL(nvmet_req_init);
1007
nvmet_req_uninit(struct nvmet_req * req)1008 void nvmet_req_uninit(struct nvmet_req *req)
1009 {
1010 percpu_ref_put(&req->sq->ref);
1011 if (req->ns)
1012 nvmet_put_namespace(req->ns);
1013 }
1014 EXPORT_SYMBOL_GPL(nvmet_req_uninit);
1015
nvmet_check_transfer_len(struct nvmet_req * req,size_t len)1016 bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len)
1017 {
1018 if (unlikely(len != req->transfer_len)) {
1019 req->error_loc = offsetof(struct nvme_common_command, dptr);
1020 nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR);
1021 return false;
1022 }
1023
1024 return true;
1025 }
1026 EXPORT_SYMBOL_GPL(nvmet_check_transfer_len);
1027
nvmet_check_data_len_lte(struct nvmet_req * req,size_t data_len)1028 bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len)
1029 {
1030 if (unlikely(data_len > req->transfer_len)) {
1031 req->error_loc = offsetof(struct nvme_common_command, dptr);
1032 nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR);
1033 return false;
1034 }
1035
1036 return true;
1037 }
1038
nvmet_data_transfer_len(struct nvmet_req * req)1039 static unsigned int nvmet_data_transfer_len(struct nvmet_req *req)
1040 {
1041 return req->transfer_len - req->metadata_len;
1042 }
1043
nvmet_req_alloc_p2pmem_sgls(struct pci_dev * p2p_dev,struct nvmet_req * req)1044 static int nvmet_req_alloc_p2pmem_sgls(struct pci_dev *p2p_dev,
1045 struct nvmet_req *req)
1046 {
1047 req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt,
1048 nvmet_data_transfer_len(req));
1049 if (!req->sg)
1050 goto out_err;
1051
1052 if (req->metadata_len) {
1053 req->metadata_sg = pci_p2pmem_alloc_sgl(p2p_dev,
1054 &req->metadata_sg_cnt, req->metadata_len);
1055 if (!req->metadata_sg)
1056 goto out_free_sg;
1057 }
1058
1059 req->p2p_dev = p2p_dev;
1060
1061 return 0;
1062 out_free_sg:
1063 pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
1064 out_err:
1065 return -ENOMEM;
1066 }
1067
nvmet_req_find_p2p_dev(struct nvmet_req * req)1068 static struct pci_dev *nvmet_req_find_p2p_dev(struct nvmet_req *req)
1069 {
1070 if (!IS_ENABLED(CONFIG_PCI_P2PDMA) ||
1071 !req->sq->ctrl || !req->sq->qid || !req->ns)
1072 return NULL;
1073 return radix_tree_lookup(&req->sq->ctrl->p2p_ns_map, req->ns->nsid);
1074 }
1075
nvmet_req_alloc_sgls(struct nvmet_req * req)1076 int nvmet_req_alloc_sgls(struct nvmet_req *req)
1077 {
1078 struct pci_dev *p2p_dev = nvmet_req_find_p2p_dev(req);
1079
1080 if (p2p_dev && !nvmet_req_alloc_p2pmem_sgls(p2p_dev, req))
1081 return 0;
1082
1083 req->sg = sgl_alloc(nvmet_data_transfer_len(req), GFP_KERNEL,
1084 &req->sg_cnt);
1085 if (unlikely(!req->sg))
1086 goto out;
1087
1088 if (req->metadata_len) {
1089 req->metadata_sg = sgl_alloc(req->metadata_len, GFP_KERNEL,
1090 &req->metadata_sg_cnt);
1091 if (unlikely(!req->metadata_sg))
1092 goto out_free;
1093 }
1094
1095 return 0;
1096 out_free:
1097 sgl_free(req->sg);
1098 out:
1099 return -ENOMEM;
1100 }
1101 EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgls);
1102
nvmet_req_free_sgls(struct nvmet_req * req)1103 void nvmet_req_free_sgls(struct nvmet_req *req)
1104 {
1105 if (req->p2p_dev) {
1106 pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
1107 if (req->metadata_sg)
1108 pci_p2pmem_free_sgl(req->p2p_dev, req->metadata_sg);
1109 req->p2p_dev = NULL;
1110 } else {
1111 sgl_free(req->sg);
1112 if (req->metadata_sg)
1113 sgl_free(req->metadata_sg);
1114 }
1115
1116 req->sg = NULL;
1117 req->metadata_sg = NULL;
1118 req->sg_cnt = 0;
1119 req->metadata_sg_cnt = 0;
1120 }
1121 EXPORT_SYMBOL_GPL(nvmet_req_free_sgls);
1122
nvmet_cc_en(u32 cc)1123 static inline bool nvmet_cc_en(u32 cc)
1124 {
1125 return (cc >> NVME_CC_EN_SHIFT) & 0x1;
1126 }
1127
nvmet_cc_css(u32 cc)1128 static inline u8 nvmet_cc_css(u32 cc)
1129 {
1130 return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
1131 }
1132
nvmet_cc_mps(u32 cc)1133 static inline u8 nvmet_cc_mps(u32 cc)
1134 {
1135 return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
1136 }
1137
nvmet_cc_ams(u32 cc)1138 static inline u8 nvmet_cc_ams(u32 cc)
1139 {
1140 return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
1141 }
1142
nvmet_cc_shn(u32 cc)1143 static inline u8 nvmet_cc_shn(u32 cc)
1144 {
1145 return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
1146 }
1147
nvmet_cc_iosqes(u32 cc)1148 static inline u8 nvmet_cc_iosqes(u32 cc)
1149 {
1150 return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
1151 }
1152
nvmet_cc_iocqes(u32 cc)1153 static inline u8 nvmet_cc_iocqes(u32 cc)
1154 {
1155 return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
1156 }
1157
nvmet_css_supported(u8 cc_css)1158 static inline bool nvmet_css_supported(u8 cc_css)
1159 {
1160 switch (cc_css << NVME_CC_CSS_SHIFT) {
1161 case NVME_CC_CSS_NVM:
1162 case NVME_CC_CSS_CSI:
1163 return true;
1164 default:
1165 return false;
1166 }
1167 }
1168
nvmet_start_ctrl(struct nvmet_ctrl * ctrl)1169 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
1170 {
1171 lockdep_assert_held(&ctrl->lock);
1172
1173 /*
1174 * Only I/O controllers should verify iosqes,iocqes.
1175 * Strictly speaking, the spec says a discovery controller
1176 * should verify iosqes,iocqes are zeroed, however that
1177 * would break backwards compatibility, so don't enforce it.
1178 */
1179 if (!nvmet_is_disc_subsys(ctrl->subsys) &&
1180 (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
1181 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES)) {
1182 ctrl->csts = NVME_CSTS_CFS;
1183 return;
1184 }
1185
1186 if (nvmet_cc_mps(ctrl->cc) != 0 ||
1187 nvmet_cc_ams(ctrl->cc) != 0 ||
1188 !nvmet_css_supported(nvmet_cc_css(ctrl->cc))) {
1189 ctrl->csts = NVME_CSTS_CFS;
1190 return;
1191 }
1192
1193 ctrl->csts = NVME_CSTS_RDY;
1194
1195 /*
1196 * Controllers that are not yet enabled should not really enforce the
1197 * keep alive timeout, but we still want to track a timeout and cleanup
1198 * in case a host died before it enabled the controller. Hence, simply
1199 * reset the keep alive timer when the controller is enabled.
1200 */
1201 if (ctrl->kato)
1202 mod_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ);
1203 }
1204
nvmet_clear_ctrl(struct nvmet_ctrl * ctrl)1205 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
1206 {
1207 lockdep_assert_held(&ctrl->lock);
1208
1209 /* XXX: tear down queues? */
1210 ctrl->csts &= ~NVME_CSTS_RDY;
1211 ctrl->cc = 0;
1212 }
1213
nvmet_update_cc(struct nvmet_ctrl * ctrl,u32 new)1214 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
1215 {
1216 u32 old;
1217
1218 mutex_lock(&ctrl->lock);
1219 old = ctrl->cc;
1220 ctrl->cc = new;
1221
1222 if (nvmet_cc_en(new) && !nvmet_cc_en(old))
1223 nvmet_start_ctrl(ctrl);
1224 if (!nvmet_cc_en(new) && nvmet_cc_en(old))
1225 nvmet_clear_ctrl(ctrl);
1226 if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
1227 nvmet_clear_ctrl(ctrl);
1228 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
1229 }
1230 if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
1231 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
1232 mutex_unlock(&ctrl->lock);
1233 }
1234
nvmet_init_cap(struct nvmet_ctrl * ctrl)1235 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
1236 {
1237 /* command sets supported: NVMe command set: */
1238 ctrl->cap = (1ULL << 37);
1239 /* Controller supports one or more I/O Command Sets */
1240 ctrl->cap |= (1ULL << 43);
1241 /* CC.EN timeout in 500msec units: */
1242 ctrl->cap |= (15ULL << 24);
1243 /* maximum queue entries supported: */
1244 if (ctrl->ops->get_max_queue_size)
1245 ctrl->cap |= min_t(u16, ctrl->ops->get_max_queue_size(ctrl),
1246 ctrl->port->max_queue_size) - 1;
1247 else
1248 ctrl->cap |= ctrl->port->max_queue_size - 1;
1249
1250 if (nvmet_is_passthru_subsys(ctrl->subsys))
1251 nvmet_passthrough_override_cap(ctrl);
1252 }
1253
nvmet_ctrl_find_get(const char * subsysnqn,const char * hostnqn,u16 cntlid,struct nvmet_req * req)1254 struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
1255 const char *hostnqn, u16 cntlid,
1256 struct nvmet_req *req)
1257 {
1258 struct nvmet_ctrl *ctrl = NULL;
1259 struct nvmet_subsys *subsys;
1260
1261 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1262 if (!subsys) {
1263 pr_warn("connect request for invalid subsystem %s!\n",
1264 subsysnqn);
1265 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1266 goto out;
1267 }
1268
1269 mutex_lock(&subsys->lock);
1270 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
1271 if (ctrl->cntlid == cntlid) {
1272 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
1273 pr_warn("hostnqn mismatch.\n");
1274 continue;
1275 }
1276 if (!kref_get_unless_zero(&ctrl->ref))
1277 continue;
1278
1279 /* ctrl found */
1280 goto found;
1281 }
1282 }
1283
1284 ctrl = NULL; /* ctrl not found */
1285 pr_warn("could not find controller %d for subsys %s / host %s\n",
1286 cntlid, subsysnqn, hostnqn);
1287 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
1288
1289 found:
1290 mutex_unlock(&subsys->lock);
1291 nvmet_subsys_put(subsys);
1292 out:
1293 return ctrl;
1294 }
1295
nvmet_check_ctrl_status(struct nvmet_req * req)1296 u16 nvmet_check_ctrl_status(struct nvmet_req *req)
1297 {
1298 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
1299 pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
1300 req->cmd->common.opcode, req->sq->qid);
1301 return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR;
1302 }
1303
1304 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
1305 pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
1306 req->cmd->common.opcode, req->sq->qid);
1307 return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR;
1308 }
1309
1310 if (unlikely(!nvmet_check_auth_status(req))) {
1311 pr_warn("qid %d not authenticated\n", req->sq->qid);
1312 return NVME_SC_AUTH_REQUIRED | NVME_STATUS_DNR;
1313 }
1314 return 0;
1315 }
1316
nvmet_host_allowed(struct nvmet_subsys * subsys,const char * hostnqn)1317 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
1318 {
1319 struct nvmet_host_link *p;
1320
1321 lockdep_assert_held(&nvmet_config_sem);
1322
1323 if (subsys->allow_any_host)
1324 return true;
1325
1326 if (nvmet_is_disc_subsys(subsys)) /* allow all access to disc subsys */
1327 return true;
1328
1329 list_for_each_entry(p, &subsys->hosts, entry) {
1330 if (!strcmp(nvmet_host_name(p->host), hostnqn))
1331 return true;
1332 }
1333
1334 return false;
1335 }
1336
1337 /*
1338 * Note: ctrl->subsys->lock should be held when calling this function
1339 */
nvmet_setup_p2p_ns_map(struct nvmet_ctrl * ctrl,struct nvmet_req * req)1340 static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
1341 struct nvmet_req *req)
1342 {
1343 struct nvmet_ns *ns;
1344 unsigned long idx;
1345
1346 if (!req->p2p_client)
1347 return;
1348
1349 ctrl->p2p_client = get_device(req->p2p_client);
1350
1351 xa_for_each(&ctrl->subsys->namespaces, idx, ns)
1352 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
1353 }
1354
1355 /*
1356 * Note: ctrl->subsys->lock should be held when calling this function
1357 */
nvmet_release_p2p_ns_map(struct nvmet_ctrl * ctrl)1358 static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
1359 {
1360 struct radix_tree_iter iter;
1361 void __rcu **slot;
1362
1363 radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0)
1364 pci_dev_put(radix_tree_deref_slot(slot));
1365
1366 put_device(ctrl->p2p_client);
1367 }
1368
nvmet_fatal_error_handler(struct work_struct * work)1369 static void nvmet_fatal_error_handler(struct work_struct *work)
1370 {
1371 struct nvmet_ctrl *ctrl =
1372 container_of(work, struct nvmet_ctrl, fatal_err_work);
1373
1374 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
1375 ctrl->ops->delete_ctrl(ctrl);
1376 }
1377
nvmet_alloc_ctrl(const char * subsysnqn,const char * hostnqn,struct nvmet_req * req,u32 kato,struct nvmet_ctrl ** ctrlp)1378 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
1379 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
1380 {
1381 struct nvmet_subsys *subsys;
1382 struct nvmet_ctrl *ctrl;
1383 int ret;
1384 u16 status;
1385
1386 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
1387 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1388 if (!subsys) {
1389 pr_warn("connect request for invalid subsystem %s!\n",
1390 subsysnqn);
1391 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1392 req->error_loc = offsetof(struct nvme_common_command, dptr);
1393 goto out;
1394 }
1395
1396 down_read(&nvmet_config_sem);
1397 if (!nvmet_host_allowed(subsys, hostnqn)) {
1398 pr_info("connect by host %s for subsystem %s not allowed\n",
1399 hostnqn, subsysnqn);
1400 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
1401 up_read(&nvmet_config_sem);
1402 status = NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR;
1403 req->error_loc = offsetof(struct nvme_common_command, dptr);
1404 goto out_put_subsystem;
1405 }
1406 up_read(&nvmet_config_sem);
1407
1408 status = NVME_SC_INTERNAL;
1409 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1410 if (!ctrl)
1411 goto out_put_subsystem;
1412 mutex_init(&ctrl->lock);
1413
1414 ctrl->port = req->port;
1415 ctrl->ops = req->ops;
1416
1417 #ifdef CONFIG_NVME_TARGET_PASSTHRU
1418 /* By default, set loop targets to clear IDS by default */
1419 if (ctrl->port->disc_addr.trtype == NVMF_TRTYPE_LOOP)
1420 subsys->clear_ids = 1;
1421 #endif
1422
1423 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
1424 INIT_LIST_HEAD(&ctrl->async_events);
1425 INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL);
1426 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
1427 INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
1428
1429 memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
1430 memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
1431
1432 kref_init(&ctrl->ref);
1433 ctrl->subsys = subsys;
1434 ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support;
1435 nvmet_init_cap(ctrl);
1436 WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
1437
1438 ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
1439 sizeof(__le32), GFP_KERNEL);
1440 if (!ctrl->changed_ns_list)
1441 goto out_free_ctrl;
1442
1443 ctrl->sqs = kcalloc(subsys->max_qid + 1,
1444 sizeof(struct nvmet_sq *),
1445 GFP_KERNEL);
1446 if (!ctrl->sqs)
1447 goto out_free_changed_ns_list;
1448
1449 ret = ida_alloc_range(&cntlid_ida,
1450 subsys->cntlid_min, subsys->cntlid_max,
1451 GFP_KERNEL);
1452 if (ret < 0) {
1453 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_STATUS_DNR;
1454 goto out_free_sqs;
1455 }
1456 ctrl->cntlid = ret;
1457
1458 /*
1459 * Discovery controllers may use some arbitrary high value
1460 * in order to cleanup stale discovery sessions
1461 */
1462 if (nvmet_is_disc_subsys(ctrl->subsys) && !kato)
1463 kato = NVMET_DISC_KATO_MS;
1464
1465 /* keep-alive timeout in seconds */
1466 ctrl->kato = DIV_ROUND_UP(kato, 1000);
1467
1468 ctrl->err_counter = 0;
1469 spin_lock_init(&ctrl->error_lock);
1470
1471 nvmet_start_keep_alive_timer(ctrl);
1472
1473 mutex_lock(&subsys->lock);
1474 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1475 nvmet_setup_p2p_ns_map(ctrl, req);
1476 nvmet_debugfs_ctrl_setup(ctrl);
1477 mutex_unlock(&subsys->lock);
1478
1479 *ctrlp = ctrl;
1480 return 0;
1481
1482 out_free_sqs:
1483 kfree(ctrl->sqs);
1484 out_free_changed_ns_list:
1485 kfree(ctrl->changed_ns_list);
1486 out_free_ctrl:
1487 kfree(ctrl);
1488 out_put_subsystem:
1489 nvmet_subsys_put(subsys);
1490 out:
1491 return status;
1492 }
1493
nvmet_ctrl_free(struct kref * ref)1494 static void nvmet_ctrl_free(struct kref *ref)
1495 {
1496 struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
1497 struct nvmet_subsys *subsys = ctrl->subsys;
1498
1499 mutex_lock(&subsys->lock);
1500 nvmet_release_p2p_ns_map(ctrl);
1501 list_del(&ctrl->subsys_entry);
1502 mutex_unlock(&subsys->lock);
1503
1504 nvmet_stop_keep_alive_timer(ctrl);
1505
1506 flush_work(&ctrl->async_event_work);
1507 cancel_work_sync(&ctrl->fatal_err_work);
1508
1509 nvmet_destroy_auth(ctrl);
1510
1511 nvmet_debugfs_ctrl_free(ctrl);
1512
1513 ida_free(&cntlid_ida, ctrl->cntlid);
1514
1515 nvmet_async_events_free(ctrl);
1516 kfree(ctrl->sqs);
1517 kfree(ctrl->changed_ns_list);
1518 kfree(ctrl);
1519
1520 nvmet_subsys_put(subsys);
1521 }
1522
nvmet_ctrl_put(struct nvmet_ctrl * ctrl)1523 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
1524 {
1525 kref_put(&ctrl->ref, nvmet_ctrl_free);
1526 }
1527
nvmet_ctrl_fatal_error(struct nvmet_ctrl * ctrl)1528 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
1529 {
1530 mutex_lock(&ctrl->lock);
1531 if (!(ctrl->csts & NVME_CSTS_CFS)) {
1532 ctrl->csts |= NVME_CSTS_CFS;
1533 queue_work(nvmet_wq, &ctrl->fatal_err_work);
1534 }
1535 mutex_unlock(&ctrl->lock);
1536 }
1537 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
1538
nvmet_ctrl_host_traddr(struct nvmet_ctrl * ctrl,char * traddr,size_t traddr_len)1539 ssize_t nvmet_ctrl_host_traddr(struct nvmet_ctrl *ctrl,
1540 char *traddr, size_t traddr_len)
1541 {
1542 if (!ctrl->ops->host_traddr)
1543 return -EOPNOTSUPP;
1544 return ctrl->ops->host_traddr(ctrl, traddr, traddr_len);
1545 }
1546
nvmet_find_get_subsys(struct nvmet_port * port,const char * subsysnqn)1547 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
1548 const char *subsysnqn)
1549 {
1550 struct nvmet_subsys_link *p;
1551
1552 if (!port)
1553 return NULL;
1554
1555 if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) {
1556 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
1557 return NULL;
1558 return nvmet_disc_subsys;
1559 }
1560
1561 down_read(&nvmet_config_sem);
1562 if (!strncmp(nvmet_disc_subsys->subsysnqn, subsysnqn,
1563 NVMF_NQN_SIZE)) {
1564 if (kref_get_unless_zero(&nvmet_disc_subsys->ref)) {
1565 up_read(&nvmet_config_sem);
1566 return nvmet_disc_subsys;
1567 }
1568 }
1569 list_for_each_entry(p, &port->subsystems, entry) {
1570 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
1571 NVMF_NQN_SIZE)) {
1572 if (!kref_get_unless_zero(&p->subsys->ref))
1573 break;
1574 up_read(&nvmet_config_sem);
1575 return p->subsys;
1576 }
1577 }
1578 up_read(&nvmet_config_sem);
1579 return NULL;
1580 }
1581
nvmet_subsys_alloc(const char * subsysnqn,enum nvme_subsys_type type)1582 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
1583 enum nvme_subsys_type type)
1584 {
1585 struct nvmet_subsys *subsys;
1586 char serial[NVMET_SN_MAX_SIZE / 2];
1587 int ret;
1588
1589 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1590 if (!subsys)
1591 return ERR_PTR(-ENOMEM);
1592
1593 subsys->ver = NVMET_DEFAULT_VS;
1594 /* generate a random serial number as our controllers are ephemeral: */
1595 get_random_bytes(&serial, sizeof(serial));
1596 bin2hex(subsys->serial, &serial, sizeof(serial));
1597
1598 subsys->model_number = kstrdup(NVMET_DEFAULT_CTRL_MODEL, GFP_KERNEL);
1599 if (!subsys->model_number) {
1600 ret = -ENOMEM;
1601 goto free_subsys;
1602 }
1603
1604 subsys->ieee_oui = 0;
1605
1606 subsys->firmware_rev = kstrndup(UTS_RELEASE, NVMET_FR_MAX_SIZE, GFP_KERNEL);
1607 if (!subsys->firmware_rev) {
1608 ret = -ENOMEM;
1609 goto free_mn;
1610 }
1611
1612 switch (type) {
1613 case NVME_NQN_NVME:
1614 subsys->max_qid = NVMET_NR_QUEUES;
1615 break;
1616 case NVME_NQN_DISC:
1617 case NVME_NQN_CURR:
1618 subsys->max_qid = 0;
1619 break;
1620 default:
1621 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
1622 ret = -EINVAL;
1623 goto free_fr;
1624 }
1625 subsys->type = type;
1626 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
1627 GFP_KERNEL);
1628 if (!subsys->subsysnqn) {
1629 ret = -ENOMEM;
1630 goto free_fr;
1631 }
1632 subsys->cntlid_min = NVME_CNTLID_MIN;
1633 subsys->cntlid_max = NVME_CNTLID_MAX;
1634 kref_init(&subsys->ref);
1635
1636 mutex_init(&subsys->lock);
1637 xa_init(&subsys->namespaces);
1638 INIT_LIST_HEAD(&subsys->ctrls);
1639 INIT_LIST_HEAD(&subsys->hosts);
1640
1641 ret = nvmet_debugfs_subsys_setup(subsys);
1642 if (ret)
1643 goto free_subsysnqn;
1644
1645 return subsys;
1646
1647 free_subsysnqn:
1648 kfree(subsys->subsysnqn);
1649 free_fr:
1650 kfree(subsys->firmware_rev);
1651 free_mn:
1652 kfree(subsys->model_number);
1653 free_subsys:
1654 kfree(subsys);
1655 return ERR_PTR(ret);
1656 }
1657
nvmet_subsys_free(struct kref * ref)1658 static void nvmet_subsys_free(struct kref *ref)
1659 {
1660 struct nvmet_subsys *subsys =
1661 container_of(ref, struct nvmet_subsys, ref);
1662
1663 WARN_ON_ONCE(!xa_empty(&subsys->namespaces));
1664
1665 nvmet_debugfs_subsys_free(subsys);
1666
1667 xa_destroy(&subsys->namespaces);
1668 nvmet_passthru_subsys_free(subsys);
1669
1670 kfree(subsys->subsysnqn);
1671 kfree(subsys->model_number);
1672 kfree(subsys->firmware_rev);
1673 kfree(subsys);
1674 }
1675
nvmet_subsys_del_ctrls(struct nvmet_subsys * subsys)1676 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
1677 {
1678 struct nvmet_ctrl *ctrl;
1679
1680 mutex_lock(&subsys->lock);
1681 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1682 ctrl->ops->delete_ctrl(ctrl);
1683 mutex_unlock(&subsys->lock);
1684 }
1685
nvmet_subsys_put(struct nvmet_subsys * subsys)1686 void nvmet_subsys_put(struct nvmet_subsys *subsys)
1687 {
1688 kref_put(&subsys->ref, nvmet_subsys_free);
1689 }
1690
nvmet_init(void)1691 static int __init nvmet_init(void)
1692 {
1693 int error = -ENOMEM;
1694
1695 nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
1696
1697 nvmet_bvec_cache = kmem_cache_create("nvmet-bvec",
1698 NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec), 0,
1699 SLAB_HWCACHE_ALIGN, NULL);
1700 if (!nvmet_bvec_cache)
1701 return -ENOMEM;
1702
1703 zbd_wq = alloc_workqueue("nvmet-zbd-wq", WQ_MEM_RECLAIM, 0);
1704 if (!zbd_wq)
1705 goto out_destroy_bvec_cache;
1706
1707 buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
1708 WQ_MEM_RECLAIM, 0);
1709 if (!buffered_io_wq)
1710 goto out_free_zbd_work_queue;
1711
1712 nvmet_wq = alloc_workqueue("nvmet-wq",
1713 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
1714 if (!nvmet_wq)
1715 goto out_free_buffered_work_queue;
1716
1717 error = nvmet_init_debugfs();
1718 if (error)
1719 goto out_free_nvmet_work_queue;
1720
1721 error = nvmet_init_discovery();
1722 if (error)
1723 goto out_exit_debugfs;
1724
1725 error = nvmet_init_configfs();
1726 if (error)
1727 goto out_exit_discovery;
1728
1729 return 0;
1730
1731 out_exit_discovery:
1732 nvmet_exit_discovery();
1733 out_exit_debugfs:
1734 nvmet_exit_debugfs();
1735 out_free_nvmet_work_queue:
1736 destroy_workqueue(nvmet_wq);
1737 out_free_buffered_work_queue:
1738 destroy_workqueue(buffered_io_wq);
1739 out_free_zbd_work_queue:
1740 destroy_workqueue(zbd_wq);
1741 out_destroy_bvec_cache:
1742 kmem_cache_destroy(nvmet_bvec_cache);
1743 return error;
1744 }
1745
nvmet_exit(void)1746 static void __exit nvmet_exit(void)
1747 {
1748 nvmet_exit_configfs();
1749 nvmet_exit_discovery();
1750 nvmet_exit_debugfs();
1751 ida_destroy(&cntlid_ida);
1752 destroy_workqueue(nvmet_wq);
1753 destroy_workqueue(buffered_io_wq);
1754 destroy_workqueue(zbd_wq);
1755 kmem_cache_destroy(nvmet_bvec_cache);
1756
1757 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
1758 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
1759 }
1760
1761 module_init(nvmet_init);
1762 module_exit(nvmet_exit);
1763
1764 MODULE_DESCRIPTION("NVMe target core framework");
1765 MODULE_LICENSE("GPL v2");
1766