]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/nvme/target/core.c
46345c3f9d4a42bb614d00e3ac359f37ad32776f
[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 static 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_req_complete(struct nvmet_req *req, u16 status)
601 {
602         u32 old_sqhd, new_sqhd;
603         u16 sqhd;
604
605         if (status)
606                 nvmet_set_status(req, status);
607
608         if (req->sq->size) {
609                 do {
610                         old_sqhd = req->sq->sqhd;
611                         new_sqhd = (old_sqhd + 1) % req->sq->size;
612                 } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
613                                         old_sqhd);
614         }
615         sqhd = req->sq->sqhd & 0x0000FFFF;
616         req->rsp->sq_head = cpu_to_le16(sqhd);
617         req->rsp->sq_id = cpu_to_le16(req->sq->qid);
618         req->rsp->command_id = req->cmd->common.command_id;
619
620         if (req->ns)
621                 nvmet_put_namespace(req->ns);
622         req->ops->queue_response(req);
623 }
624
625 void nvmet_req_complete(struct nvmet_req *req, u16 status)
626 {
627         __nvmet_req_complete(req, status);
628         percpu_ref_put(&req->sq->ref);
629 }
630 EXPORT_SYMBOL_GPL(nvmet_req_complete);
631
632 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
633                 u16 qid, u16 size)
634 {
635         cq->qid = qid;
636         cq->size = size;
637
638         ctrl->cqs[qid] = cq;
639 }
640
641 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
642                 u16 qid, u16 size)
643 {
644         sq->sqhd = 0;
645         sq->qid = qid;
646         sq->size = size;
647
648         ctrl->sqs[qid] = sq;
649 }
650
651 static void nvmet_confirm_sq(struct percpu_ref *ref)
652 {
653         struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
654
655         complete(&sq->confirm_done);
656 }
657
658 void nvmet_sq_destroy(struct nvmet_sq *sq)
659 {
660         /*
661          * If this is the admin queue, complete all AERs so that our
662          * queue doesn't have outstanding requests on it.
663          */
664         if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
665                 nvmet_async_events_free(sq->ctrl);
666         percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
667         wait_for_completion(&sq->confirm_done);
668         wait_for_completion(&sq->free_done);
669         percpu_ref_exit(&sq->ref);
670
671         if (sq->ctrl) {
672                 nvmet_ctrl_put(sq->ctrl);
673                 sq->ctrl = NULL; /* allows reusing the queue later */
674         }
675 }
676 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
677
678 static void nvmet_sq_free(struct percpu_ref *ref)
679 {
680         struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
681
682         complete(&sq->free_done);
683 }
684
685 int nvmet_sq_init(struct nvmet_sq *sq)
686 {
687         int ret;
688
689         ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
690         if (ret) {
691                 pr_err("percpu_ref init failed!\n");
692                 return ret;
693         }
694         init_completion(&sq->free_done);
695         init_completion(&sq->confirm_done);
696
697         return 0;
698 }
699 EXPORT_SYMBOL_GPL(nvmet_sq_init);
700
701 static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
702                 struct nvmet_ns *ns)
703 {
704         enum nvme_ana_state state = port->ana_state[ns->anagrpid];
705
706         if (unlikely(state == NVME_ANA_INACCESSIBLE))
707                 return NVME_SC_ANA_INACCESSIBLE;
708         if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
709                 return NVME_SC_ANA_PERSISTENT_LOSS;
710         if (unlikely(state == NVME_ANA_CHANGE))
711                 return NVME_SC_ANA_TRANSITION;
712         return 0;
713 }
714
715 static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
716 {
717         if (unlikely(req->ns->readonly)) {
718                 switch (req->cmd->common.opcode) {
719                 case nvme_cmd_read:
720                 case nvme_cmd_flush:
721                         break;
722                 default:
723                         return NVME_SC_NS_WRITE_PROTECTED;
724                 }
725         }
726
727         return 0;
728 }
729
730 static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
731 {
732         struct nvme_command *cmd = req->cmd;
733         u16 ret;
734
735         ret = nvmet_check_ctrl_status(req, cmd);
736         if (unlikely(ret))
737                 return ret;
738
739         req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
740         if (unlikely(!req->ns))
741                 return NVME_SC_INVALID_NS | NVME_SC_DNR;
742         ret = nvmet_check_ana_state(req->port, req->ns);
743         if (unlikely(ret))
744                 return ret;
745         ret = nvmet_io_cmd_check_access(req);
746         if (unlikely(ret))
747                 return ret;
748
749         if (req->ns->file)
750                 return nvmet_file_parse_io_cmd(req);
751         else
752                 return nvmet_bdev_parse_io_cmd(req);
753 }
754
755 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
756                 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
757 {
758         u8 flags = req->cmd->common.flags;
759         u16 status;
760
761         req->cq = cq;
762         req->sq = sq;
763         req->ops = ops;
764         req->sg = NULL;
765         req->sg_cnt = 0;
766         req->transfer_len = 0;
767         req->rsp->status = 0;
768         req->ns = NULL;
769
770         /* no support for fused commands yet */
771         if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
772                 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
773                 goto fail;
774         }
775
776         /*
777          * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
778          * contains an address of a single contiguous physical buffer that is
779          * byte aligned.
780          */
781         if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
782                 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
783                 goto fail;
784         }
785
786         if (unlikely(!req->sq->ctrl))
787                 /* will return an error for any Non-connect command: */
788                 status = nvmet_parse_connect_cmd(req);
789         else if (likely(req->sq->qid != 0))
790                 status = nvmet_parse_io_cmd(req);
791         else if (req->cmd->common.opcode == nvme_fabrics_command)
792                 status = nvmet_parse_fabrics_cmd(req);
793         else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
794                 status = nvmet_parse_discovery_cmd(req);
795         else
796                 status = nvmet_parse_admin_cmd(req);
797
798         if (status)
799                 goto fail;
800
801         if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
802                 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
803                 goto fail;
804         }
805
806         if (sq->ctrl)
807                 sq->ctrl->cmd_seen = true;
808
809         return true;
810
811 fail:
812         __nvmet_req_complete(req, status);
813         return false;
814 }
815 EXPORT_SYMBOL_GPL(nvmet_req_init);
816
817 void nvmet_req_uninit(struct nvmet_req *req)
818 {
819         percpu_ref_put(&req->sq->ref);
820         if (req->ns)
821                 nvmet_put_namespace(req->ns);
822 }
823 EXPORT_SYMBOL_GPL(nvmet_req_uninit);
824
825 void nvmet_req_execute(struct nvmet_req *req)
826 {
827         if (unlikely(req->data_len != req->transfer_len))
828                 nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR);
829         else
830                 req->execute(req);
831 }
832 EXPORT_SYMBOL_GPL(nvmet_req_execute);
833
834 int nvmet_req_alloc_sgl(struct nvmet_req *req)
835 {
836         struct pci_dev *p2p_dev = NULL;
837
838         if (IS_ENABLED(CONFIG_PCI_P2PDMA)) {
839                 if (req->sq->ctrl && req->ns)
840                         p2p_dev = radix_tree_lookup(&req->sq->ctrl->p2p_ns_map,
841                                                     req->ns->nsid);
842
843                 req->p2p_dev = NULL;
844                 if (req->sq->qid && p2p_dev) {
845                         req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt,
846                                                        req->transfer_len);
847                         if (req->sg) {
848                                 req->p2p_dev = p2p_dev;
849                                 return 0;
850                         }
851                 }
852
853                 /*
854                  * If no P2P memory was available we fallback to using
855                  * regular memory
856                  */
857         }
858
859         req->sg = sgl_alloc(req->transfer_len, GFP_KERNEL, &req->sg_cnt);
860         if (!req->sg)
861                 return -ENOMEM;
862
863         return 0;
864 }
865 EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgl);
866
867 void nvmet_req_free_sgl(struct nvmet_req *req)
868 {
869         if (req->p2p_dev)
870                 pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
871         else
872                 sgl_free(req->sg);
873
874         req->sg = NULL;
875         req->sg_cnt = 0;
876 }
877 EXPORT_SYMBOL_GPL(nvmet_req_free_sgl);
878
879 static inline bool nvmet_cc_en(u32 cc)
880 {
881         return (cc >> NVME_CC_EN_SHIFT) & 0x1;
882 }
883
884 static inline u8 nvmet_cc_css(u32 cc)
885 {
886         return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
887 }
888
889 static inline u8 nvmet_cc_mps(u32 cc)
890 {
891         return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
892 }
893
894 static inline u8 nvmet_cc_ams(u32 cc)
895 {
896         return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
897 }
898
899 static inline u8 nvmet_cc_shn(u32 cc)
900 {
901         return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
902 }
903
904 static inline u8 nvmet_cc_iosqes(u32 cc)
905 {
906         return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
907 }
908
909 static inline u8 nvmet_cc_iocqes(u32 cc)
910 {
911         return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
912 }
913
914 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
915 {
916         lockdep_assert_held(&ctrl->lock);
917
918         if (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
919             nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES ||
920             nvmet_cc_mps(ctrl->cc) != 0 ||
921             nvmet_cc_ams(ctrl->cc) != 0 ||
922             nvmet_cc_css(ctrl->cc) != 0) {
923                 ctrl->csts = NVME_CSTS_CFS;
924                 return;
925         }
926
927         ctrl->csts = NVME_CSTS_RDY;
928
929         /*
930          * Controllers that are not yet enabled should not really enforce the
931          * keep alive timeout, but we still want to track a timeout and cleanup
932          * in case a host died before it enabled the controller.  Hence, simply
933          * reset the keep alive timer when the controller is enabled.
934          */
935         mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
936 }
937
938 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
939 {
940         lockdep_assert_held(&ctrl->lock);
941
942         /* XXX: tear down queues? */
943         ctrl->csts &= ~NVME_CSTS_RDY;
944         ctrl->cc = 0;
945 }
946
947 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
948 {
949         u32 old;
950
951         mutex_lock(&ctrl->lock);
952         old = ctrl->cc;
953         ctrl->cc = new;
954
955         if (nvmet_cc_en(new) && !nvmet_cc_en(old))
956                 nvmet_start_ctrl(ctrl);
957         if (!nvmet_cc_en(new) && nvmet_cc_en(old))
958                 nvmet_clear_ctrl(ctrl);
959         if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
960                 nvmet_clear_ctrl(ctrl);
961                 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
962         }
963         if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
964                 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
965         mutex_unlock(&ctrl->lock);
966 }
967
968 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
969 {
970         /* command sets supported: NVMe command set: */
971         ctrl->cap = (1ULL << 37);
972         /* CC.EN timeout in 500msec units: */
973         ctrl->cap |= (15ULL << 24);
974         /* maximum queue entries supported: */
975         ctrl->cap |= NVMET_QUEUE_SIZE - 1;
976 }
977
978 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
979                 struct nvmet_req *req, struct nvmet_ctrl **ret)
980 {
981         struct nvmet_subsys *subsys;
982         struct nvmet_ctrl *ctrl;
983         u16 status = 0;
984
985         subsys = nvmet_find_get_subsys(req->port, subsysnqn);
986         if (!subsys) {
987                 pr_warn("connect request for invalid subsystem %s!\n",
988                         subsysnqn);
989                 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
990                 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
991         }
992
993         mutex_lock(&subsys->lock);
994         list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
995                 if (ctrl->cntlid == cntlid) {
996                         if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
997                                 pr_warn("hostnqn mismatch.\n");
998                                 continue;
999                         }
1000                         if (!kref_get_unless_zero(&ctrl->ref))
1001                                 continue;
1002
1003                         *ret = ctrl;
1004                         goto out;
1005                 }
1006         }
1007
1008         pr_warn("could not find controller %d for subsys %s / host %s\n",
1009                 cntlid, subsysnqn, hostnqn);
1010         req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
1011         status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1012
1013 out:
1014         mutex_unlock(&subsys->lock);
1015         nvmet_subsys_put(subsys);
1016         return status;
1017 }
1018
1019 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
1020 {
1021         if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
1022                 pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
1023                        cmd->common.opcode, req->sq->qid);
1024                 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1025         }
1026
1027         if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
1028                 pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
1029                        cmd->common.opcode, req->sq->qid);
1030                 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
1031         }
1032         return 0;
1033 }
1034
1035 static bool __nvmet_host_allowed(struct nvmet_subsys *subsys,
1036                 const char *hostnqn)
1037 {
1038         struct nvmet_host_link *p;
1039
1040         if (subsys->allow_any_host)
1041                 return true;
1042
1043         list_for_each_entry(p, &subsys->hosts, entry) {
1044                 if (!strcmp(nvmet_host_name(p->host), hostnqn))
1045                         return true;
1046         }
1047
1048         return false;
1049 }
1050
1051 static bool nvmet_host_discovery_allowed(struct nvmet_req *req,
1052                 const char *hostnqn)
1053 {
1054         struct nvmet_subsys_link *s;
1055
1056         list_for_each_entry(s, &req->port->subsystems, entry) {
1057                 if (__nvmet_host_allowed(s->subsys, hostnqn))
1058                         return true;
1059         }
1060
1061         return false;
1062 }
1063
1064 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
1065                 const char *hostnqn)
1066 {
1067         lockdep_assert_held(&nvmet_config_sem);
1068
1069         if (subsys->type == NVME_NQN_DISC)
1070                 return nvmet_host_discovery_allowed(req, hostnqn);
1071         else
1072                 return __nvmet_host_allowed(subsys, hostnqn);
1073 }
1074
1075 /*
1076  * Note: ctrl->subsys->lock should be held when calling this function
1077  */
1078 static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
1079                 struct nvmet_req *req)
1080 {
1081         struct nvmet_ns *ns;
1082
1083         if (!req->p2p_client)
1084                 return;
1085
1086         ctrl->p2p_client = get_device(req->p2p_client);
1087
1088         list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link)
1089                 nvmet_p2pmem_ns_add_p2p(ctrl, ns);
1090 }
1091
1092 /*
1093  * Note: ctrl->subsys->lock should be held when calling this function
1094  */
1095 static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
1096 {
1097         struct radix_tree_iter iter;
1098         void __rcu **slot;
1099
1100         radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0)
1101                 pci_dev_put(radix_tree_deref_slot(slot));
1102
1103         put_device(ctrl->p2p_client);
1104 }
1105
1106 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
1107                 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
1108 {
1109         struct nvmet_subsys *subsys;
1110         struct nvmet_ctrl *ctrl;
1111         int ret;
1112         u16 status;
1113
1114         status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1115         subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1116         if (!subsys) {
1117                 pr_warn("connect request for invalid subsystem %s!\n",
1118                         subsysnqn);
1119                 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1120                 goto out;
1121         }
1122
1123         status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
1124         down_read(&nvmet_config_sem);
1125         if (!nvmet_host_allowed(req, subsys, hostnqn)) {
1126                 pr_info("connect by host %s for subsystem %s not allowed\n",
1127                         hostnqn, subsysnqn);
1128                 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
1129                 up_read(&nvmet_config_sem);
1130                 status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
1131                 goto out_put_subsystem;
1132         }
1133         up_read(&nvmet_config_sem);
1134
1135         status = NVME_SC_INTERNAL;
1136         ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1137         if (!ctrl)
1138                 goto out_put_subsystem;
1139         mutex_init(&ctrl->lock);
1140
1141         nvmet_init_cap(ctrl);
1142
1143         ctrl->port = req->port;
1144
1145         INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
1146         INIT_LIST_HEAD(&ctrl->async_events);
1147         INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL);
1148
1149         memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
1150         memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
1151
1152         kref_init(&ctrl->ref);
1153         ctrl->subsys = subsys;
1154         WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
1155
1156         ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
1157                         sizeof(__le32), GFP_KERNEL);
1158         if (!ctrl->changed_ns_list)
1159                 goto out_free_ctrl;
1160
1161         ctrl->cqs = kcalloc(subsys->max_qid + 1,
1162                         sizeof(struct nvmet_cq *),
1163                         GFP_KERNEL);
1164         if (!ctrl->cqs)
1165                 goto out_free_changed_ns_list;
1166
1167         ctrl->sqs = kcalloc(subsys->max_qid + 1,
1168                         sizeof(struct nvmet_sq *),
1169                         GFP_KERNEL);
1170         if (!ctrl->sqs)
1171                 goto out_free_cqs;
1172
1173         ret = ida_simple_get(&cntlid_ida,
1174                              NVME_CNTLID_MIN, NVME_CNTLID_MAX,
1175                              GFP_KERNEL);
1176         if (ret < 0) {
1177                 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
1178                 goto out_free_sqs;
1179         }
1180         ctrl->cntlid = ret;
1181
1182         ctrl->ops = req->ops;
1183
1184         /*
1185          * Discovery controllers may use some arbitrary high value
1186          * in order to cleanup stale discovery sessions
1187          */
1188         if ((ctrl->subsys->type == NVME_NQN_DISC) && !kato)
1189                 kato = NVMET_DISC_KATO_MS;
1190
1191         /* keep-alive timeout in seconds */
1192         ctrl->kato = DIV_ROUND_UP(kato, 1000);
1193
1194         nvmet_start_keep_alive_timer(ctrl);
1195
1196         mutex_lock(&subsys->lock);
1197         list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1198         nvmet_setup_p2p_ns_map(ctrl, req);
1199         mutex_unlock(&subsys->lock);
1200
1201         *ctrlp = ctrl;
1202         return 0;
1203
1204 out_free_sqs:
1205         kfree(ctrl->sqs);
1206 out_free_cqs:
1207         kfree(ctrl->cqs);
1208 out_free_changed_ns_list:
1209         kfree(ctrl->changed_ns_list);
1210 out_free_ctrl:
1211         kfree(ctrl);
1212 out_put_subsystem:
1213         nvmet_subsys_put(subsys);
1214 out:
1215         return status;
1216 }
1217
1218 static void nvmet_ctrl_free(struct kref *ref)
1219 {
1220         struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
1221         struct nvmet_subsys *subsys = ctrl->subsys;
1222
1223         mutex_lock(&subsys->lock);
1224         nvmet_release_p2p_ns_map(ctrl);
1225         list_del(&ctrl->subsys_entry);
1226         mutex_unlock(&subsys->lock);
1227
1228         nvmet_stop_keep_alive_timer(ctrl);
1229
1230         flush_work(&ctrl->async_event_work);
1231         cancel_work_sync(&ctrl->fatal_err_work);
1232
1233         ida_simple_remove(&cntlid_ida, ctrl->cntlid);
1234
1235         kfree(ctrl->sqs);
1236         kfree(ctrl->cqs);
1237         kfree(ctrl->changed_ns_list);
1238         kfree(ctrl);
1239
1240         nvmet_subsys_put(subsys);
1241 }
1242
1243 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
1244 {
1245         kref_put(&ctrl->ref, nvmet_ctrl_free);
1246 }
1247
1248 static void nvmet_fatal_error_handler(struct work_struct *work)
1249 {
1250         struct nvmet_ctrl *ctrl =
1251                         container_of(work, struct nvmet_ctrl, fatal_err_work);
1252
1253         pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
1254         ctrl->ops->delete_ctrl(ctrl);
1255 }
1256
1257 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
1258 {
1259         mutex_lock(&ctrl->lock);
1260         if (!(ctrl->csts & NVME_CSTS_CFS)) {
1261                 ctrl->csts |= NVME_CSTS_CFS;
1262                 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
1263                 schedule_work(&ctrl->fatal_err_work);
1264         }
1265         mutex_unlock(&ctrl->lock);
1266 }
1267 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
1268
1269 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
1270                 const char *subsysnqn)
1271 {
1272         struct nvmet_subsys_link *p;
1273
1274         if (!port)
1275                 return NULL;
1276
1277         if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) {
1278                 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
1279                         return NULL;
1280                 return nvmet_disc_subsys;
1281         }
1282
1283         down_read(&nvmet_config_sem);
1284         list_for_each_entry(p, &port->subsystems, entry) {
1285                 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
1286                                 NVMF_NQN_SIZE)) {
1287                         if (!kref_get_unless_zero(&p->subsys->ref))
1288                                 break;
1289                         up_read(&nvmet_config_sem);
1290                         return p->subsys;
1291                 }
1292         }
1293         up_read(&nvmet_config_sem);
1294         return NULL;
1295 }
1296
1297 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
1298                 enum nvme_subsys_type type)
1299 {
1300         struct nvmet_subsys *subsys;
1301
1302         subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1303         if (!subsys)
1304                 return NULL;
1305
1306         subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
1307         /* generate a random serial number as our controllers are ephemeral: */
1308         get_random_bytes(&subsys->serial, sizeof(subsys->serial));
1309
1310         switch (type) {
1311         case NVME_NQN_NVME:
1312                 subsys->max_qid = NVMET_NR_QUEUES;
1313                 break;
1314         case NVME_NQN_DISC:
1315                 subsys->max_qid = 0;
1316                 break;
1317         default:
1318                 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
1319                 kfree(subsys);
1320                 return NULL;
1321         }
1322         subsys->type = type;
1323         subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
1324                         GFP_KERNEL);
1325         if (!subsys->subsysnqn) {
1326                 kfree(subsys);
1327                 return NULL;
1328         }
1329
1330         kref_init(&subsys->ref);
1331
1332         mutex_init(&subsys->lock);
1333         INIT_LIST_HEAD(&subsys->namespaces);
1334         INIT_LIST_HEAD(&subsys->ctrls);
1335         INIT_LIST_HEAD(&subsys->hosts);
1336
1337         return subsys;
1338 }
1339
1340 static void nvmet_subsys_free(struct kref *ref)
1341 {
1342         struct nvmet_subsys *subsys =
1343                 container_of(ref, struct nvmet_subsys, ref);
1344
1345         WARN_ON_ONCE(!list_empty(&subsys->namespaces));
1346
1347         kfree(subsys->subsysnqn);
1348         kfree(subsys);
1349 }
1350
1351 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
1352 {
1353         struct nvmet_ctrl *ctrl;
1354
1355         mutex_lock(&subsys->lock);
1356         list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1357                 ctrl->ops->delete_ctrl(ctrl);
1358         mutex_unlock(&subsys->lock);
1359 }
1360
1361 void nvmet_subsys_put(struct nvmet_subsys *subsys)
1362 {
1363         kref_put(&subsys->ref, nvmet_subsys_free);
1364 }
1365
1366 static int __init nvmet_init(void)
1367 {
1368         int error;
1369
1370         nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
1371
1372         buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
1373                         WQ_MEM_RECLAIM, 0);
1374         if (!buffered_io_wq) {
1375                 error = -ENOMEM;
1376                 goto out;
1377         }
1378
1379         error = nvmet_init_discovery();
1380         if (error)
1381                 goto out_free_work_queue;
1382
1383         error = nvmet_init_configfs();
1384         if (error)
1385                 goto out_exit_discovery;
1386         return 0;
1387
1388 out_exit_discovery:
1389         nvmet_exit_discovery();
1390 out_free_work_queue:
1391         destroy_workqueue(buffered_io_wq);
1392 out:
1393         return error;
1394 }
1395
1396 static void __exit nvmet_exit(void)
1397 {
1398         nvmet_exit_configfs();
1399         nvmet_exit_discovery();
1400         ida_destroy(&cntlid_ida);
1401         destroy_workqueue(buffered_io_wq);
1402
1403         BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
1404         BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
1405 }
1406
1407 module_init(nvmet_init);
1408 module_exit(nvmet_exit);
1409
1410 MODULE_LICENSE("GPL v2");