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