]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/nvme/target/core.c
nvmet: support fabrics sq flow control
[linux.git] / drivers / nvme / target / core.c
1 /*
2  * Common code for the NVMe target.
3  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/rculist.h>
18 #include <linux/pci-p2pdma.h>
19
20 #include "nvmet.h"
21
22 struct workqueue_struct *buffered_io_wq;
23 static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
24 static DEFINE_IDA(cntlid_ida);
25
26 /*
27  * This read/write semaphore is used to synchronize access to configuration
28  * information on a target system that will result in discovery log page
29  * information change for at least one host.
30  * The full list of resources to protected by this semaphore is:
31  *
32  *  - subsystems list
33  *  - per-subsystem allowed hosts list
34  *  - allow_any_host subsystem attribute
35  *  - nvmet_genctr
36  *  - the nvmet_transports array
37  *
38  * When updating any of those lists/structures write lock should be obtained,
39  * while when reading (popolating discovery log page or checking host-subsystem
40  * link) read lock is obtained to allow concurrent reads.
41  */
42 DECLARE_RWSEM(nvmet_config_sem);
43
44 u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
45 u64 nvmet_ana_chgcnt;
46 DECLARE_RWSEM(nvmet_ana_sem);
47
48 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
49                 const char *subsysnqn);
50
51 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
52                 size_t len)
53 {
54         if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
55                 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
56         return 0;
57 }
58
59 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
60 {
61         if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
62                 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
63         return 0;
64 }
65
66 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len)
67 {
68         if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len)
69                 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
70         return 0;
71 }
72
73 static unsigned int nvmet_max_nsid(struct nvmet_subsys *subsys)
74 {
75         struct nvmet_ns *ns;
76
77         if (list_empty(&subsys->namespaces))
78                 return 0;
79
80         ns = list_last_entry(&subsys->namespaces, struct nvmet_ns, dev_link);
81         return ns->nsid;
82 }
83
84 static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
85 {
86         return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
87 }
88
89 static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
90 {
91         struct nvmet_req *req;
92
93         while (1) {
94                 mutex_lock(&ctrl->lock);
95                 if (!ctrl->nr_async_event_cmds) {
96                         mutex_unlock(&ctrl->lock);
97                         return;
98                 }
99
100                 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
101                 mutex_unlock(&ctrl->lock);
102                 nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_SC_DNR);
103         }
104 }
105
106 static void nvmet_async_event_work(struct work_struct *work)
107 {
108         struct nvmet_ctrl *ctrl =
109                 container_of(work, struct nvmet_ctrl, async_event_work);
110         struct nvmet_async_event *aen;
111         struct nvmet_req *req;
112
113         while (1) {
114                 mutex_lock(&ctrl->lock);
115                 aen = list_first_entry_or_null(&ctrl->async_events,
116                                 struct nvmet_async_event, entry);
117                 if (!aen || !ctrl->nr_async_event_cmds) {
118                         mutex_unlock(&ctrl->lock);
119                         return;
120                 }
121
122                 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
123                 nvmet_set_result(req, nvmet_async_event_result(aen));
124
125                 list_del(&aen->entry);
126                 kfree(aen);
127
128                 mutex_unlock(&ctrl->lock);
129                 nvmet_req_complete(req, 0);
130         }
131 }
132
133 void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
134                 u8 event_info, u8 log_page)
135 {
136         struct nvmet_async_event *aen;
137
138         aen = kmalloc(sizeof(*aen), GFP_KERNEL);
139         if (!aen)
140                 return;
141
142         aen->event_type = event_type;
143         aen->event_info = event_info;
144         aen->log_page = log_page;
145
146         mutex_lock(&ctrl->lock);
147         list_add_tail(&aen->entry, &ctrl->async_events);
148         mutex_unlock(&ctrl->lock);
149
150         schedule_work(&ctrl->async_event_work);
151 }
152
153 static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid)
154 {
155         u32 i;
156
157         mutex_lock(&ctrl->lock);
158         if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES)
159                 goto out_unlock;
160
161         for (i = 0; i < ctrl->nr_changed_ns; i++) {
162                 if (ctrl->changed_ns_list[i] == nsid)
163                         goto out_unlock;
164         }
165
166         if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) {
167                 ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff);
168                 ctrl->nr_changed_ns = U32_MAX;
169                 goto out_unlock;
170         }
171
172         ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid;
173 out_unlock:
174         mutex_unlock(&ctrl->lock);
175 }
176
177 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid)
178 {
179         struct nvmet_ctrl *ctrl;
180
181         list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
182                 nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid));
183                 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_NS_ATTR))
184                         continue;
185                 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
186                                 NVME_AER_NOTICE_NS_CHANGED,
187                                 NVME_LOG_CHANGED_NS);
188         }
189 }
190
191 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
192                 struct nvmet_port *port)
193 {
194         struct nvmet_ctrl *ctrl;
195
196         mutex_lock(&subsys->lock);
197         list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
198                 if (port && ctrl->port != port)
199                         continue;
200                 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_ANA_CHANGE))
201                         continue;
202                 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
203                                 NVME_AER_NOTICE_ANA, NVME_LOG_ANA);
204         }
205         mutex_unlock(&subsys->lock);
206 }
207
208 void nvmet_port_send_ana_event(struct nvmet_port *port)
209 {
210         struct nvmet_subsys_link *p;
211
212         down_read(&nvmet_config_sem);
213         list_for_each_entry(p, &port->subsystems, entry)
214                 nvmet_send_ana_event(p->subsys, port);
215         up_read(&nvmet_config_sem);
216 }
217
218 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops)
219 {
220         int ret = 0;
221
222         down_write(&nvmet_config_sem);
223         if (nvmet_transports[ops->type])
224                 ret = -EINVAL;
225         else
226                 nvmet_transports[ops->type] = ops;
227         up_write(&nvmet_config_sem);
228
229         return ret;
230 }
231 EXPORT_SYMBOL_GPL(nvmet_register_transport);
232
233 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
234 {
235         down_write(&nvmet_config_sem);
236         nvmet_transports[ops->type] = NULL;
237         up_write(&nvmet_config_sem);
238 }
239 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
240
241 int nvmet_enable_port(struct nvmet_port *port)
242 {
243         const struct nvmet_fabrics_ops *ops;
244         int ret;
245
246         lockdep_assert_held(&nvmet_config_sem);
247
248         ops = nvmet_transports[port->disc_addr.trtype];
249         if (!ops) {
250                 up_write(&nvmet_config_sem);
251                 request_module("nvmet-transport-%d", port->disc_addr.trtype);
252                 down_write(&nvmet_config_sem);
253                 ops = nvmet_transports[port->disc_addr.trtype];
254                 if (!ops) {
255                         pr_err("transport type %d not supported\n",
256                                 port->disc_addr.trtype);
257                         return -EINVAL;
258                 }
259         }
260
261         if (!try_module_get(ops->owner))
262                 return -EINVAL;
263
264         ret = ops->add_port(port);
265         if (ret) {
266                 module_put(ops->owner);
267                 return ret;
268         }
269
270         /* If the transport didn't set inline_data_size, then disable it. */
271         if (port->inline_data_size < 0)
272                 port->inline_data_size = 0;
273
274         port->enabled = true;
275         return 0;
276 }
277
278 void nvmet_disable_port(struct nvmet_port *port)
279 {
280         const struct nvmet_fabrics_ops *ops;
281
282         lockdep_assert_held(&nvmet_config_sem);
283
284         port->enabled = false;
285
286         ops = nvmet_transports[port->disc_addr.trtype];
287         ops->remove_port(port);
288         module_put(ops->owner);
289 }
290
291 static void nvmet_keep_alive_timer(struct work_struct *work)
292 {
293         struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
294                         struct nvmet_ctrl, ka_work);
295         bool cmd_seen = ctrl->cmd_seen;
296
297         ctrl->cmd_seen = false;
298         if (cmd_seen) {
299                 pr_debug("ctrl %d reschedule traffic based keep-alive timer\n",
300                         ctrl->cntlid);
301                 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
302                 return;
303         }
304
305         pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
306                 ctrl->cntlid, ctrl->kato);
307
308         nvmet_ctrl_fatal_error(ctrl);
309 }
310
311 static void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
312 {
313         pr_debug("ctrl %d start keep-alive timer for %d secs\n",
314                 ctrl->cntlid, ctrl->kato);
315
316         INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
317         schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
318 }
319
320 static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
321 {
322         pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
323
324         cancel_delayed_work_sync(&ctrl->ka_work);
325 }
326
327 static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
328                 __le32 nsid)
329 {
330         struct nvmet_ns *ns;
331
332         list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
333                 if (ns->nsid == le32_to_cpu(nsid))
334                         return ns;
335         }
336
337         return NULL;
338 }
339
340 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
341 {
342         struct nvmet_ns *ns;
343
344         rcu_read_lock();
345         ns = __nvmet_find_namespace(ctrl, nsid);
346         if (ns)
347                 percpu_ref_get(&ns->ref);
348         rcu_read_unlock();
349
350         return ns;
351 }
352
353 static void nvmet_destroy_namespace(struct percpu_ref *ref)
354 {
355         struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
356
357         complete(&ns->disable_done);
358 }
359
360 void nvmet_put_namespace(struct nvmet_ns *ns)
361 {
362         percpu_ref_put(&ns->ref);
363 }
364
365 static void nvmet_ns_dev_disable(struct nvmet_ns *ns)
366 {
367         nvmet_bdev_ns_disable(ns);
368         nvmet_file_ns_disable(ns);
369 }
370
371 static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns)
372 {
373         int ret;
374         struct pci_dev *p2p_dev;
375
376         if (!ns->use_p2pmem)
377                 return 0;
378
379         if (!ns->bdev) {
380                 pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n");
381                 return -EINVAL;
382         }
383
384         if (!blk_queue_pci_p2pdma(ns->bdev->bd_queue)) {
385                 pr_err("peer-to-peer DMA is not supported by the driver of %s\n",
386                        ns->device_path);
387                 return -EINVAL;
388         }
389
390         if (ns->p2p_dev) {
391                 ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true);
392                 if (ret < 0)
393                         return -EINVAL;
394         } else {
395                 /*
396                  * Right now we just check that there is p2pmem available so
397                  * we can report an error to the user right away if there
398                  * is not. We'll find the actual device to use once we
399                  * setup the controller when the port's device is available.
400                  */
401
402                 p2p_dev = pci_p2pmem_find(nvmet_ns_dev(ns));
403                 if (!p2p_dev) {
404                         pr_err("no peer-to-peer memory is available for %s\n",
405                                ns->device_path);
406                         return -EINVAL;
407                 }
408
409                 pci_dev_put(p2p_dev);
410         }
411
412         return 0;
413 }
414
415 /*
416  * Note: ctrl->subsys->lock should be held when calling this function
417  */
418 static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl,
419                                     struct nvmet_ns *ns)
420 {
421         struct device *clients[2];
422         struct pci_dev *p2p_dev;
423         int ret;
424
425         if (!ctrl->p2p_client || !ns->use_p2pmem)
426                 return;
427
428         if (ns->p2p_dev) {
429                 ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true);
430                 if (ret < 0)
431                         return;
432
433                 p2p_dev = pci_dev_get(ns->p2p_dev);
434         } else {
435                 clients[0] = ctrl->p2p_client;
436                 clients[1] = nvmet_ns_dev(ns);
437
438                 p2p_dev = pci_p2pmem_find_many(clients, ARRAY_SIZE(clients));
439                 if (!p2p_dev) {
440                         pr_err("no peer-to-peer memory is available that's supported by %s and %s\n",
441                                dev_name(ctrl->p2p_client), ns->device_path);
442                         return;
443                 }
444         }
445
446         ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev);
447         if (ret < 0)
448                 pci_dev_put(p2p_dev);
449
450         pr_info("using p2pmem on %s for nsid %d\n", pci_name(p2p_dev),
451                 ns->nsid);
452 }
453
454 int nvmet_ns_enable(struct nvmet_ns *ns)
455 {
456         struct nvmet_subsys *subsys = ns->subsys;
457         struct nvmet_ctrl *ctrl;
458         int ret;
459
460         mutex_lock(&subsys->lock);
461         ret = -EMFILE;
462         if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
463                 goto out_unlock;
464         ret = 0;
465         if (ns->enabled)
466                 goto out_unlock;
467
468         ret = nvmet_bdev_ns_enable(ns);
469         if (ret == -ENOTBLK)
470                 ret = nvmet_file_ns_enable(ns);
471         if (ret)
472                 goto out_unlock;
473
474         ret = nvmet_p2pmem_ns_enable(ns);
475         if (ret)
476                 goto out_unlock;
477
478         list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
479                 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
480
481         ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
482                                 0, GFP_KERNEL);
483         if (ret)
484                 goto out_dev_put;
485
486         if (ns->nsid > subsys->max_nsid)
487                 subsys->max_nsid = ns->nsid;
488
489         /*
490          * The namespaces list needs to be sorted to simplify the implementation
491          * of the Identify Namepace List subcommand.
492          */
493         if (list_empty(&subsys->namespaces)) {
494                 list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
495         } else {
496                 struct nvmet_ns *old;
497
498                 list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
499                         BUG_ON(ns->nsid == old->nsid);
500                         if (ns->nsid < old->nsid)
501                                 break;
502                 }
503
504                 list_add_tail_rcu(&ns->dev_link, &old->dev_link);
505         }
506         subsys->nr_namespaces++;
507
508         nvmet_ns_changed(subsys, ns->nsid);
509         ns->enabled = true;
510         ret = 0;
511 out_unlock:
512         mutex_unlock(&subsys->lock);
513         return ret;
514 out_dev_put:
515         list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
516                 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
517
518         nvmet_ns_dev_disable(ns);
519         goto out_unlock;
520 }
521
522 void nvmet_ns_disable(struct nvmet_ns *ns)
523 {
524         struct nvmet_subsys *subsys = ns->subsys;
525         struct nvmet_ctrl *ctrl;
526
527         mutex_lock(&subsys->lock);
528         if (!ns->enabled)
529                 goto out_unlock;
530
531         ns->enabled = false;
532         list_del_rcu(&ns->dev_link);
533         if (ns->nsid == subsys->max_nsid)
534                 subsys->max_nsid = nvmet_max_nsid(subsys);
535
536         list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
537                 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
538
539         mutex_unlock(&subsys->lock);
540
541         /*
542          * Now that we removed the namespaces from the lookup list, we
543          * can kill the per_cpu ref and wait for any remaining references
544          * to be dropped, as well as a RCU grace period for anyone only
545          * using the namepace under rcu_read_lock().  Note that we can't
546          * use call_rcu here as we need to ensure the namespaces have
547          * been fully destroyed before unloading the module.
548          */
549         percpu_ref_kill(&ns->ref);
550         synchronize_rcu();
551         wait_for_completion(&ns->disable_done);
552         percpu_ref_exit(&ns->ref);
553
554         mutex_lock(&subsys->lock);
555
556         subsys->nr_namespaces--;
557         nvmet_ns_changed(subsys, ns->nsid);
558         nvmet_ns_dev_disable(ns);
559 out_unlock:
560         mutex_unlock(&subsys->lock);
561 }
562
563 void nvmet_ns_free(struct nvmet_ns *ns)
564 {
565         nvmet_ns_disable(ns);
566
567         down_write(&nvmet_ana_sem);
568         nvmet_ana_group_enabled[ns->anagrpid]--;
569         up_write(&nvmet_ana_sem);
570
571         kfree(ns->device_path);
572         kfree(ns);
573 }
574
575 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
576 {
577         struct nvmet_ns *ns;
578
579         ns = kzalloc(sizeof(*ns), GFP_KERNEL);
580         if (!ns)
581                 return NULL;
582
583         INIT_LIST_HEAD(&ns->dev_link);
584         init_completion(&ns->disable_done);
585
586         ns->nsid = nsid;
587         ns->subsys = subsys;
588
589         down_write(&nvmet_ana_sem);
590         ns->anagrpid = NVMET_DEFAULT_ANA_GRPID;
591         nvmet_ana_group_enabled[ns->anagrpid]++;
592         up_write(&nvmet_ana_sem);
593
594         uuid_gen(&ns->uuid);
595         ns->buffered_io = false;
596
597         return ns;
598 }
599
600 static void nvmet_update_sq_head(struct nvmet_req *req)
601 {
602         if (req->sq->size) {
603                 u32 old_sqhd, new_sqhd;
604
605                 do {
606                         old_sqhd = req->sq->sqhd;
607                         new_sqhd = (old_sqhd + 1) % req->sq->size;
608                 } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
609                                         old_sqhd);
610         }
611         req->rsp->sq_head = cpu_to_le16(req->sq->sqhd & 0x0000FFFF);
612 }
613
614 static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
615 {
616         if (!req->sq->sqhd_disabled)
617                 nvmet_update_sq_head(req);
618         req->rsp->sq_id = cpu_to_le16(req->sq->qid);
619         req->rsp->command_id = req->cmd->common.command_id;
620         if (status)
621                 nvmet_set_status(req, status);
622         if (req->ns)
623                 nvmet_put_namespace(req->ns);
624         req->ops->queue_response(req);
625 }
626
627 void nvmet_req_complete(struct nvmet_req *req, u16 status)
628 {
629         __nvmet_req_complete(req, status);
630         percpu_ref_put(&req->sq->ref);
631 }
632 EXPORT_SYMBOL_GPL(nvmet_req_complete);
633
634 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
635                 u16 qid, u16 size)
636 {
637         cq->qid = qid;
638         cq->size = size;
639
640         ctrl->cqs[qid] = cq;
641 }
642
643 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
644                 u16 qid, u16 size)
645 {
646         sq->sqhd = 0;
647         sq->qid = qid;
648         sq->size = size;
649
650         ctrl->sqs[qid] = sq;
651 }
652
653 static void nvmet_confirm_sq(struct percpu_ref *ref)
654 {
655         struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
656
657         complete(&sq->confirm_done);
658 }
659
660 void nvmet_sq_destroy(struct nvmet_sq *sq)
661 {
662         /*
663          * If this is the admin queue, complete all AERs so that our
664          * queue doesn't have outstanding requests on it.
665          */
666         if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
667                 nvmet_async_events_free(sq->ctrl);
668         percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
669         wait_for_completion(&sq->confirm_done);
670         wait_for_completion(&sq->free_done);
671         percpu_ref_exit(&sq->ref);
672
673         if (sq->ctrl) {
674                 nvmet_ctrl_put(sq->ctrl);
675                 sq->ctrl = NULL; /* allows reusing the queue later */
676         }
677 }
678 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
679
680 static void nvmet_sq_free(struct percpu_ref *ref)
681 {
682         struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
683
684         complete(&sq->free_done);
685 }
686
687 int nvmet_sq_init(struct nvmet_sq *sq)
688 {
689         int ret;
690
691         ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
692         if (ret) {
693                 pr_err("percpu_ref init failed!\n");
694                 return ret;
695         }
696         init_completion(&sq->free_done);
697         init_completion(&sq->confirm_done);
698
699         return 0;
700 }
701 EXPORT_SYMBOL_GPL(nvmet_sq_init);
702
703 static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
704                 struct nvmet_ns *ns)
705 {
706         enum nvme_ana_state state = port->ana_state[ns->anagrpid];
707
708         if (unlikely(state == NVME_ANA_INACCESSIBLE))
709                 return NVME_SC_ANA_INACCESSIBLE;
710         if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
711                 return NVME_SC_ANA_PERSISTENT_LOSS;
712         if (unlikely(state == NVME_ANA_CHANGE))
713                 return NVME_SC_ANA_TRANSITION;
714         return 0;
715 }
716
717 static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
718 {
719         if (unlikely(req->ns->readonly)) {
720                 switch (req->cmd->common.opcode) {
721                 case nvme_cmd_read:
722                 case nvme_cmd_flush:
723                         break;
724                 default:
725                         return NVME_SC_NS_WRITE_PROTECTED;
726                 }
727         }
728
729         return 0;
730 }
731
732 static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
733 {
734         struct nvme_command *cmd = req->cmd;
735         u16 ret;
736
737         ret = nvmet_check_ctrl_status(req, cmd);
738         if (unlikely(ret))
739                 return ret;
740
741         req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
742         if (unlikely(!req->ns))
743                 return NVME_SC_INVALID_NS | NVME_SC_DNR;
744         ret = nvmet_check_ana_state(req->port, req->ns);
745         if (unlikely(ret))
746                 return ret;
747         ret = nvmet_io_cmd_check_access(req);
748         if (unlikely(ret))
749                 return ret;
750
751         if (req->ns->file)
752                 return nvmet_file_parse_io_cmd(req);
753         else
754                 return nvmet_bdev_parse_io_cmd(req);
755 }
756
757 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
758                 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
759 {
760         u8 flags = req->cmd->common.flags;
761         u16 status;
762
763         req->cq = cq;
764         req->sq = sq;
765         req->ops = ops;
766         req->sg = NULL;
767         req->sg_cnt = 0;
768         req->transfer_len = 0;
769         req->rsp->status = 0;
770         req->rsp->sq_head = 0;
771         req->ns = NULL;
772
773         /* no support for fused commands yet */
774         if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
775                 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
776                 goto fail;
777         }
778
779         /*
780          * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
781          * contains an address of a single contiguous physical buffer that is
782          * byte aligned.
783          */
784         if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
785                 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
786                 goto fail;
787         }
788
789         if (unlikely(!req->sq->ctrl))
790                 /* will return an error for any Non-connect command: */
791                 status = nvmet_parse_connect_cmd(req);
792         else if (likely(req->sq->qid != 0))
793                 status = nvmet_parse_io_cmd(req);
794         else if (req->cmd->common.opcode == nvme_fabrics_command)
795                 status = nvmet_parse_fabrics_cmd(req);
796         else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
797                 status = nvmet_parse_discovery_cmd(req);
798         else
799                 status = nvmet_parse_admin_cmd(req);
800
801         if (status)
802                 goto fail;
803
804         if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
805                 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
806                 goto fail;
807         }
808
809         if (sq->ctrl)
810                 sq->ctrl->cmd_seen = true;
811
812         return true;
813
814 fail:
815         __nvmet_req_complete(req, status);
816         return false;
817 }
818 EXPORT_SYMBOL_GPL(nvmet_req_init);
819
820 void nvmet_req_uninit(struct nvmet_req *req)
821 {
822         percpu_ref_put(&req->sq->ref);
823         if (req->ns)
824                 nvmet_put_namespace(req->ns);
825 }
826 EXPORT_SYMBOL_GPL(nvmet_req_uninit);
827
828 void nvmet_req_execute(struct nvmet_req *req)
829 {
830         if (unlikely(req->data_len != req->transfer_len))
831                 nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR);
832         else
833                 req->execute(req);
834 }
835 EXPORT_SYMBOL_GPL(nvmet_req_execute);
836
837 int nvmet_req_alloc_sgl(struct nvmet_req *req)
838 {
839         struct pci_dev *p2p_dev = NULL;
840
841         if (IS_ENABLED(CONFIG_PCI_P2PDMA)) {
842                 if (req->sq->ctrl && req->ns)
843                         p2p_dev = radix_tree_lookup(&req->sq->ctrl->p2p_ns_map,
844                                                     req->ns->nsid);
845
846                 req->p2p_dev = NULL;
847                 if (req->sq->qid && p2p_dev) {
848                         req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt,
849                                                        req->transfer_len);
850                         if (req->sg) {
851                                 req->p2p_dev = p2p_dev;
852                                 return 0;
853                         }
854                 }
855
856                 /*
857                  * If no P2P memory was available we fallback to using
858                  * regular memory
859                  */
860         }
861
862         req->sg = sgl_alloc(req->transfer_len, GFP_KERNEL, &req->sg_cnt);
863         if (!req->sg)
864                 return -ENOMEM;
865
866         return 0;
867 }
868 EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgl);
869
870 void nvmet_req_free_sgl(struct nvmet_req *req)
871 {
872         if (req->p2p_dev)
873                 pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
874         else
875                 sgl_free(req->sg);
876
877         req->sg = NULL;
878         req->sg_cnt = 0;
879 }
880 EXPORT_SYMBOL_GPL(nvmet_req_free_sgl);
881
882 static inline bool nvmet_cc_en(u32 cc)
883 {
884         return (cc >> NVME_CC_EN_SHIFT) & 0x1;
885 }
886
887 static inline u8 nvmet_cc_css(u32 cc)
888 {
889         return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
890 }
891
892 static inline u8 nvmet_cc_mps(u32 cc)
893 {
894         return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
895 }
896
897 static inline u8 nvmet_cc_ams(u32 cc)
898 {
899         return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
900 }
901
902 static inline u8 nvmet_cc_shn(u32 cc)
903 {
904         return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
905 }
906
907 static inline u8 nvmet_cc_iosqes(u32 cc)
908 {
909         return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
910 }
911
912 static inline u8 nvmet_cc_iocqes(u32 cc)
913 {
914         return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
915 }
916
917 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
918 {
919         lockdep_assert_held(&ctrl->lock);
920
921         if (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
922             nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES ||
923             nvmet_cc_mps(ctrl->cc) != 0 ||
924             nvmet_cc_ams(ctrl->cc) != 0 ||
925             nvmet_cc_css(ctrl->cc) != 0) {
926                 ctrl->csts = NVME_CSTS_CFS;
927                 return;
928         }
929
930         ctrl->csts = NVME_CSTS_RDY;
931
932         /*
933          * Controllers that are not yet enabled should not really enforce the
934          * keep alive timeout, but we still want to track a timeout and cleanup
935          * in case a host died before it enabled the controller.  Hence, simply
936          * reset the keep alive timer when the controller is enabled.
937          */
938         mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
939 }
940
941 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
942 {
943         lockdep_assert_held(&ctrl->lock);
944
945         /* XXX: tear down queues? */
946         ctrl->csts &= ~NVME_CSTS_RDY;
947         ctrl->cc = 0;
948 }
949
950 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
951 {
952         u32 old;
953
954         mutex_lock(&ctrl->lock);
955         old = ctrl->cc;
956         ctrl->cc = new;
957
958         if (nvmet_cc_en(new) && !nvmet_cc_en(old))
959                 nvmet_start_ctrl(ctrl);
960         if (!nvmet_cc_en(new) && nvmet_cc_en(old))
961                 nvmet_clear_ctrl(ctrl);
962         if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
963                 nvmet_clear_ctrl(ctrl);
964                 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
965         }
966         if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
967                 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
968         mutex_unlock(&ctrl->lock);
969 }
970
971 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
972 {
973         /* command sets supported: NVMe command set: */
974         ctrl->cap = (1ULL << 37);
975         /* CC.EN timeout in 500msec units: */
976         ctrl->cap |= (15ULL << 24);
977         /* maximum queue entries supported: */
978         ctrl->cap |= NVMET_QUEUE_SIZE - 1;
979 }
980
981 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
982                 struct nvmet_req *req, struct nvmet_ctrl **ret)
983 {
984         struct nvmet_subsys *subsys;
985         struct nvmet_ctrl *ctrl;
986         u16 status = 0;
987
988         subsys = nvmet_find_get_subsys(req->port, subsysnqn);
989         if (!subsys) {
990                 pr_warn("connect request for invalid subsystem %s!\n",
991                         subsysnqn);
992                 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
993                 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
994         }
995
996         mutex_lock(&subsys->lock);
997         list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
998                 if (ctrl->cntlid == cntlid) {
999                         if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
1000                                 pr_warn("hostnqn mismatch.\n");
1001                                 continue;
1002                         }
1003                         if (!kref_get_unless_zero(&ctrl->ref))
1004                                 continue;
1005
1006                         *ret = ctrl;
1007                         goto out;
1008                 }
1009         }
1010
1011         pr_warn("could not find controller %d for subsys %s / host %s\n",
1012                 cntlid, subsysnqn, hostnqn);
1013         req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
1014         status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1015
1016 out:
1017         mutex_unlock(&subsys->lock);
1018         nvmet_subsys_put(subsys);
1019         return status;
1020 }
1021
1022 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
1023 {
1024         if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
1025                 pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
1026                        cmd->common.opcode, req->sq->qid);
1027                 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1028         }
1029
1030         if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
1031                 pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
1032                        cmd->common.opcode, req->sq->qid);
1033                 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1034         }
1035         return 0;
1036 }
1037
1038 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
1039 {
1040         struct nvmet_host_link *p;
1041
1042         lockdep_assert_held(&nvmet_config_sem);
1043
1044         if (subsys->allow_any_host)
1045                 return true;
1046
1047         if (subsys->type == NVME_NQN_DISC) /* allow all access to disc subsys */
1048                 return true;
1049
1050         list_for_each_entry(p, &subsys->hosts, entry) {
1051                 if (!strcmp(nvmet_host_name(p->host), hostnqn))
1052                         return true;
1053         }
1054
1055         return false;
1056 }
1057
1058 /*
1059  * Note: ctrl->subsys->lock should be held when calling this function
1060  */
1061 static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
1062                 struct nvmet_req *req)
1063 {
1064         struct nvmet_ns *ns;
1065
1066         if (!req->p2p_client)
1067                 return;
1068
1069         ctrl->p2p_client = get_device(req->p2p_client);
1070
1071         list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link)
1072                 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
1073 }
1074
1075 /*
1076  * Note: ctrl->subsys->lock should be held when calling this function
1077  */
1078 static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
1079 {
1080         struct radix_tree_iter iter;
1081         void __rcu **slot;
1082
1083         radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0)
1084                 pci_dev_put(radix_tree_deref_slot(slot));
1085
1086         put_device(ctrl->p2p_client);
1087 }
1088
1089 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
1090                 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
1091 {
1092         struct nvmet_subsys *subsys;
1093         struct nvmet_ctrl *ctrl;
1094         int ret;
1095         u16 status;
1096
1097         status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1098         subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1099         if (!subsys) {
1100                 pr_warn("connect request for invalid subsystem %s!\n",
1101                         subsysnqn);
1102                 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1103                 goto out;
1104         }
1105
1106         status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1107         down_read(&nvmet_config_sem);
1108         if (!nvmet_host_allowed(subsys, hostnqn)) {
1109                 pr_info("connect by host %s for subsystem %s not allowed\n",
1110                         hostnqn, subsysnqn);
1111                 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
1112                 up_read(&nvmet_config_sem);
1113                 status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
1114                 goto out_put_subsystem;
1115         }
1116         up_read(&nvmet_config_sem);
1117
1118         status = NVME_SC_INTERNAL;
1119         ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1120         if (!ctrl)
1121                 goto out_put_subsystem;
1122         mutex_init(&ctrl->lock);
1123
1124         nvmet_init_cap(ctrl);
1125
1126         ctrl->port = req->port;
1127
1128         INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
1129         INIT_LIST_HEAD(&ctrl->async_events);
1130         INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL);
1131
1132         memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
1133         memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
1134
1135         kref_init(&ctrl->ref);
1136         ctrl->subsys = subsys;
1137         WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
1138
1139         ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
1140                         sizeof(__le32), GFP_KERNEL);
1141         if (!ctrl->changed_ns_list)
1142                 goto out_free_ctrl;
1143
1144         ctrl->cqs = kcalloc(subsys->max_qid + 1,
1145                         sizeof(struct nvmet_cq *),
1146                         GFP_KERNEL);
1147         if (!ctrl->cqs)
1148                 goto out_free_changed_ns_list;
1149
1150         ctrl->sqs = kcalloc(subsys->max_qid + 1,
1151                         sizeof(struct nvmet_sq *),
1152                         GFP_KERNEL);
1153         if (!ctrl->sqs)
1154                 goto out_free_cqs;
1155
1156         ret = ida_simple_get(&cntlid_ida,
1157                              NVME_CNTLID_MIN, NVME_CNTLID_MAX,
1158                              GFP_KERNEL);
1159         if (ret < 0) {
1160                 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
1161                 goto out_free_sqs;
1162         }
1163         ctrl->cntlid = ret;
1164
1165         ctrl->ops = req->ops;
1166
1167         /*
1168          * Discovery controllers may use some arbitrary high value
1169          * in order to cleanup stale discovery sessions
1170          */
1171         if ((ctrl->subsys->type == NVME_NQN_DISC) && !kato)
1172                 kato = NVMET_DISC_KATO_MS;
1173
1174         /* keep-alive timeout in seconds */
1175         ctrl->kato = DIV_ROUND_UP(kato, 1000);
1176
1177         nvmet_start_keep_alive_timer(ctrl);
1178
1179         mutex_lock(&subsys->lock);
1180         list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1181         nvmet_setup_p2p_ns_map(ctrl, req);
1182         mutex_unlock(&subsys->lock);
1183
1184         *ctrlp = ctrl;
1185         return 0;
1186
1187 out_free_sqs:
1188         kfree(ctrl->sqs);
1189 out_free_cqs:
1190         kfree(ctrl->cqs);
1191 out_free_changed_ns_list:
1192         kfree(ctrl->changed_ns_list);
1193 out_free_ctrl:
1194         kfree(ctrl);
1195 out_put_subsystem:
1196         nvmet_subsys_put(subsys);
1197 out:
1198         return status;
1199 }
1200
1201 static void nvmet_ctrl_free(struct kref *ref)
1202 {
1203         struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
1204         struct nvmet_subsys *subsys = ctrl->subsys;
1205
1206         mutex_lock(&subsys->lock);
1207         nvmet_release_p2p_ns_map(ctrl);
1208         list_del(&ctrl->subsys_entry);
1209         mutex_unlock(&subsys->lock);
1210
1211         nvmet_stop_keep_alive_timer(ctrl);
1212
1213         flush_work(&ctrl->async_event_work);
1214         cancel_work_sync(&ctrl->fatal_err_work);
1215
1216         ida_simple_remove(&cntlid_ida, ctrl->cntlid);
1217
1218         kfree(ctrl->sqs);
1219         kfree(ctrl->cqs);
1220         kfree(ctrl->changed_ns_list);
1221         kfree(ctrl);
1222
1223         nvmet_subsys_put(subsys);
1224 }
1225
1226 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
1227 {
1228         kref_put(&ctrl->ref, nvmet_ctrl_free);
1229 }
1230
1231 static void nvmet_fatal_error_handler(struct work_struct *work)
1232 {
1233         struct nvmet_ctrl *ctrl =
1234                         container_of(work, struct nvmet_ctrl, fatal_err_work);
1235
1236         pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
1237         ctrl->ops->delete_ctrl(ctrl);
1238 }
1239
1240 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
1241 {
1242         mutex_lock(&ctrl->lock);
1243         if (!(ctrl->csts & NVME_CSTS_CFS)) {
1244                 ctrl->csts |= NVME_CSTS_CFS;
1245                 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
1246                 schedule_work(&ctrl->fatal_err_work);
1247         }
1248         mutex_unlock(&ctrl->lock);
1249 }
1250 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
1251
1252 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
1253                 const char *subsysnqn)
1254 {
1255         struct nvmet_subsys_link *p;
1256
1257         if (!port)
1258                 return NULL;
1259
1260         if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) {
1261                 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
1262                         return NULL;
1263                 return nvmet_disc_subsys;
1264         }
1265
1266         down_read(&nvmet_config_sem);
1267         list_for_each_entry(p, &port->subsystems, entry) {
1268                 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
1269                                 NVMF_NQN_SIZE)) {
1270                         if (!kref_get_unless_zero(&p->subsys->ref))
1271                                 break;
1272                         up_read(&nvmet_config_sem);
1273                         return p->subsys;
1274                 }
1275         }
1276         up_read(&nvmet_config_sem);
1277         return NULL;
1278 }
1279
1280 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
1281                 enum nvme_subsys_type type)
1282 {
1283         struct nvmet_subsys *subsys;
1284
1285         subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1286         if (!subsys)
1287                 return NULL;
1288
1289         subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
1290         /* generate a random serial number as our controllers are ephemeral: */
1291         get_random_bytes(&subsys->serial, sizeof(subsys->serial));
1292
1293         switch (type) {
1294         case NVME_NQN_NVME:
1295                 subsys->max_qid = NVMET_NR_QUEUES;
1296                 break;
1297         case NVME_NQN_DISC:
1298                 subsys->max_qid = 0;
1299                 break;
1300         default:
1301                 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
1302                 kfree(subsys);
1303                 return NULL;
1304         }
1305         subsys->type = type;
1306         subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
1307                         GFP_KERNEL);
1308         if (!subsys->subsysnqn) {
1309                 kfree(subsys);
1310                 return NULL;
1311         }
1312
1313         kref_init(&subsys->ref);
1314
1315         mutex_init(&subsys->lock);
1316         INIT_LIST_HEAD(&subsys->namespaces);
1317         INIT_LIST_HEAD(&subsys->ctrls);
1318         INIT_LIST_HEAD(&subsys->hosts);
1319
1320         return subsys;
1321 }
1322
1323 static void nvmet_subsys_free(struct kref *ref)
1324 {
1325         struct nvmet_subsys *subsys =
1326                 container_of(ref, struct nvmet_subsys, ref);
1327
1328         WARN_ON_ONCE(!list_empty(&subsys->namespaces));
1329
1330         kfree(subsys->subsysnqn);
1331         kfree(subsys);
1332 }
1333
1334 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
1335 {
1336         struct nvmet_ctrl *ctrl;
1337
1338         mutex_lock(&subsys->lock);
1339         list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1340                 ctrl->ops->delete_ctrl(ctrl);
1341         mutex_unlock(&subsys->lock);
1342 }
1343
1344 void nvmet_subsys_put(struct nvmet_subsys *subsys)
1345 {
1346         kref_put(&subsys->ref, nvmet_subsys_free);
1347 }
1348
1349 static int __init nvmet_init(void)
1350 {
1351         int error;
1352
1353         nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
1354
1355         buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
1356                         WQ_MEM_RECLAIM, 0);
1357         if (!buffered_io_wq) {
1358                 error = -ENOMEM;
1359                 goto out;
1360         }
1361
1362         error = nvmet_init_discovery();
1363         if (error)
1364                 goto out_free_work_queue;
1365
1366         error = nvmet_init_configfs();
1367         if (error)
1368                 goto out_exit_discovery;
1369         return 0;
1370
1371 out_exit_discovery:
1372         nvmet_exit_discovery();
1373 out_free_work_queue:
1374         destroy_workqueue(buffered_io_wq);
1375 out:
1376         return error;
1377 }
1378
1379 static void __exit nvmet_exit(void)
1380 {
1381         nvmet_exit_configfs();
1382         nvmet_exit_discovery();
1383         ida_destroy(&cntlid_ida);
1384         destroy_workqueue(buffered_io_wq);
1385
1386         BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
1387         BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
1388 }
1389
1390 module_init(nvmet_init);
1391 module_exit(nvmet_exit);
1392
1393 MODULE_LICENSE("GPL v2");