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 "nvmet.h"
18
19 static struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
20
21 /*
22 * This read/write semaphore is used to synchronize access to configuration
23 * information on a target system that will result in discovery log page
24 * information change for at least one host.
25 * The full list of resources to protected by this semaphore is:
26 *
27 * - subsystems list
28 * - per-subsystem allowed hosts list
29 * - allow_any_host subsystem attribute
30 * - nvmet_genctr
31 * - the nvmet_transports array
32 *
33 * When updating any of those lists/structures write lock should be obtained,
34 * while when reading (popolating discovery log page or checking host-subsystem
35 * link) read lock is obtained to allow concurrent reads.
36 */
37 DECLARE_RWSEM(nvmet_config_sem);
38
39 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
40 const char *subsysnqn);
41
nvmet_copy_to_sgl(struct nvmet_req * req,off_t off,const void * buf,size_t len)42 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
43 size_t len)
44 {
45 if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
46 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
47 return 0;
48 }
49
nvmet_copy_from_sgl(struct nvmet_req * req,off_t off,void * buf,size_t len)50 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
51 {
52 if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
53 return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
54 return 0;
55 }
56
nvmet_async_event_result(struct nvmet_async_event * aen)57 static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
58 {
59 return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
60 }
61
nvmet_async_events_free(struct nvmet_ctrl * ctrl)62 static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
63 {
64 struct nvmet_req *req;
65
66 while (1) {
67 mutex_lock(&ctrl->lock);
68 if (!ctrl->nr_async_event_cmds) {
69 mutex_unlock(&ctrl->lock);
70 return;
71 }
72
73 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
74 mutex_unlock(&ctrl->lock);
75 nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_SC_DNR);
76 }
77 }
78
nvmet_async_event_work(struct work_struct * work)79 static void nvmet_async_event_work(struct work_struct *work)
80 {
81 struct nvmet_ctrl *ctrl =
82 container_of(work, struct nvmet_ctrl, async_event_work);
83 struct nvmet_async_event *aen;
84 struct nvmet_req *req;
85
86 while (1) {
87 mutex_lock(&ctrl->lock);
88 aen = list_first_entry_or_null(&ctrl->async_events,
89 struct nvmet_async_event, entry);
90 if (!aen || !ctrl->nr_async_event_cmds) {
91 mutex_unlock(&ctrl->lock);
92 return;
93 }
94
95 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
96 nvmet_set_result(req, nvmet_async_event_result(aen));
97
98 list_del(&aen->entry);
99 kfree(aen);
100
101 mutex_unlock(&ctrl->lock);
102 nvmet_req_complete(req, 0);
103 }
104 }
105
nvmet_add_async_event(struct nvmet_ctrl * ctrl,u8 event_type,u8 event_info,u8 log_page)106 static void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
107 u8 event_info, u8 log_page)
108 {
109 struct nvmet_async_event *aen;
110
111 aen = kmalloc(sizeof(*aen), GFP_KERNEL);
112 if (!aen)
113 return;
114
115 aen->event_type = event_type;
116 aen->event_info = event_info;
117 aen->log_page = log_page;
118
119 mutex_lock(&ctrl->lock);
120 list_add_tail(&aen->entry, &ctrl->async_events);
121 mutex_unlock(&ctrl->lock);
122
123 schedule_work(&ctrl->async_event_work);
124 }
125
nvmet_register_transport(struct nvmet_fabrics_ops * ops)126 int nvmet_register_transport(struct nvmet_fabrics_ops *ops)
127 {
128 int ret = 0;
129
130 down_write(&nvmet_config_sem);
131 if (nvmet_transports[ops->type])
132 ret = -EINVAL;
133 else
134 nvmet_transports[ops->type] = ops;
135 up_write(&nvmet_config_sem);
136
137 return ret;
138 }
139 EXPORT_SYMBOL_GPL(nvmet_register_transport);
140
nvmet_unregister_transport(struct nvmet_fabrics_ops * ops)141 void nvmet_unregister_transport(struct nvmet_fabrics_ops *ops)
142 {
143 down_write(&nvmet_config_sem);
144 nvmet_transports[ops->type] = NULL;
145 up_write(&nvmet_config_sem);
146 }
147 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
148
nvmet_enable_port(struct nvmet_port * port)149 int nvmet_enable_port(struct nvmet_port *port)
150 {
151 struct nvmet_fabrics_ops *ops;
152 int ret;
153
154 lockdep_assert_held(&nvmet_config_sem);
155
156 ops = nvmet_transports[port->disc_addr.trtype];
157 if (!ops) {
158 up_write(&nvmet_config_sem);
159 request_module("nvmet-transport-%d", port->disc_addr.trtype);
160 down_write(&nvmet_config_sem);
161 ops = nvmet_transports[port->disc_addr.trtype];
162 if (!ops) {
163 pr_err("transport type %d not supported\n",
164 port->disc_addr.trtype);
165 return -EINVAL;
166 }
167 }
168
169 if (!try_module_get(ops->owner))
170 return -EINVAL;
171
172 ret = ops->add_port(port);
173 if (ret) {
174 module_put(ops->owner);
175 return ret;
176 }
177
178 port->enabled = true;
179 return 0;
180 }
181
nvmet_disable_port(struct nvmet_port * port)182 void nvmet_disable_port(struct nvmet_port *port)
183 {
184 struct nvmet_fabrics_ops *ops;
185
186 lockdep_assert_held(&nvmet_config_sem);
187
188 port->enabled = false;
189
190 ops = nvmet_transports[port->disc_addr.trtype];
191 ops->remove_port(port);
192 module_put(ops->owner);
193 }
194
nvmet_keep_alive_timer(struct work_struct * work)195 static void nvmet_keep_alive_timer(struct work_struct *work)
196 {
197 struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
198 struct nvmet_ctrl, ka_work);
199
200 pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
201 ctrl->cntlid, ctrl->kato);
202
203 ctrl->ops->delete_ctrl(ctrl);
204 }
205
nvmet_start_keep_alive_timer(struct nvmet_ctrl * ctrl)206 static void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
207 {
208 pr_debug("ctrl %d start keep-alive timer for %d secs\n",
209 ctrl->cntlid, ctrl->kato);
210
211 INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
212 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
213 }
214
nvmet_stop_keep_alive_timer(struct nvmet_ctrl * ctrl)215 static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
216 {
217 pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
218
219 cancel_delayed_work_sync(&ctrl->ka_work);
220 }
221
__nvmet_find_namespace(struct nvmet_ctrl * ctrl,__le32 nsid)222 static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
223 __le32 nsid)
224 {
225 struct nvmet_ns *ns;
226
227 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
228 if (ns->nsid == le32_to_cpu(nsid))
229 return ns;
230 }
231
232 return NULL;
233 }
234
nvmet_find_namespace(struct nvmet_ctrl * ctrl,__le32 nsid)235 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
236 {
237 struct nvmet_ns *ns;
238
239 rcu_read_lock();
240 ns = __nvmet_find_namespace(ctrl, nsid);
241 if (ns)
242 percpu_ref_get(&ns->ref);
243 rcu_read_unlock();
244
245 return ns;
246 }
247
nvmet_destroy_namespace(struct percpu_ref * ref)248 static void nvmet_destroy_namespace(struct percpu_ref *ref)
249 {
250 struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
251
252 complete(&ns->disable_done);
253 }
254
nvmet_put_namespace(struct nvmet_ns * ns)255 void nvmet_put_namespace(struct nvmet_ns *ns)
256 {
257 percpu_ref_put(&ns->ref);
258 }
259
nvmet_ns_enable(struct nvmet_ns * ns)260 int nvmet_ns_enable(struct nvmet_ns *ns)
261 {
262 struct nvmet_subsys *subsys = ns->subsys;
263 struct nvmet_ctrl *ctrl;
264 int ret = 0;
265
266 mutex_lock(&subsys->lock);
267 if (ns->enabled)
268 goto out_unlock;
269
270 ns->bdev = blkdev_get_by_path(ns->device_path, FMODE_READ | FMODE_WRITE,
271 NULL);
272 if (IS_ERR(ns->bdev)) {
273 pr_err("nvmet: failed to open block device %s: (%ld)\n",
274 ns->device_path, PTR_ERR(ns->bdev));
275 ret = PTR_ERR(ns->bdev);
276 ns->bdev = NULL;
277 goto out_unlock;
278 }
279
280 ns->size = i_size_read(ns->bdev->bd_inode);
281 ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
282
283 ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
284 0, GFP_KERNEL);
285 if (ret)
286 goto out_blkdev_put;
287
288 if (ns->nsid > subsys->max_nsid)
289 subsys->max_nsid = ns->nsid;
290
291 /*
292 * The namespaces list needs to be sorted to simplify the implementation
293 * of the Identify Namepace List subcommand.
294 */
295 if (list_empty(&subsys->namespaces)) {
296 list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
297 } else {
298 struct nvmet_ns *old;
299
300 list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
301 BUG_ON(ns->nsid == old->nsid);
302 if (ns->nsid < old->nsid)
303 break;
304 }
305
306 list_add_tail_rcu(&ns->dev_link, &old->dev_link);
307 }
308
309 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
310 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE, 0, 0);
311
312 ns->enabled = true;
313 ret = 0;
314 out_unlock:
315 mutex_unlock(&subsys->lock);
316 return ret;
317 out_blkdev_put:
318 blkdev_put(ns->bdev, FMODE_WRITE|FMODE_READ);
319 ns->bdev = NULL;
320 goto out_unlock;
321 }
322
nvmet_ns_disable(struct nvmet_ns * ns)323 void nvmet_ns_disable(struct nvmet_ns *ns)
324 {
325 struct nvmet_subsys *subsys = ns->subsys;
326 struct nvmet_ctrl *ctrl;
327
328 mutex_lock(&subsys->lock);
329 if (!ns->enabled)
330 goto out_unlock;
331
332 ns->enabled = false;
333 list_del_rcu(&ns->dev_link);
334 mutex_unlock(&subsys->lock);
335
336 /*
337 * Now that we removed the namespaces from the lookup list, we
338 * can kill the per_cpu ref and wait for any remaining references
339 * to be dropped, as well as a RCU grace period for anyone only
340 * using the namepace under rcu_read_lock(). Note that we can't
341 * use call_rcu here as we need to ensure the namespaces have
342 * been fully destroyed before unloading the module.
343 */
344 percpu_ref_kill(&ns->ref);
345 synchronize_rcu();
346 wait_for_completion(&ns->disable_done);
347 percpu_ref_exit(&ns->ref);
348
349 mutex_lock(&subsys->lock);
350 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
351 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE, 0, 0);
352
353 if (ns->bdev)
354 blkdev_put(ns->bdev, FMODE_WRITE|FMODE_READ);
355 out_unlock:
356 mutex_unlock(&subsys->lock);
357 }
358
nvmet_ns_free(struct nvmet_ns * ns)359 void nvmet_ns_free(struct nvmet_ns *ns)
360 {
361 nvmet_ns_disable(ns);
362
363 kfree(ns->device_path);
364 kfree(ns);
365 }
366
nvmet_ns_alloc(struct nvmet_subsys * subsys,u32 nsid)367 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
368 {
369 struct nvmet_ns *ns;
370
371 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
372 if (!ns)
373 return NULL;
374
375 INIT_LIST_HEAD(&ns->dev_link);
376 init_completion(&ns->disable_done);
377
378 ns->nsid = nsid;
379 ns->subsys = subsys;
380
381 return ns;
382 }
383
__nvmet_req_complete(struct nvmet_req * req,u16 status)384 static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
385 {
386 if (status)
387 nvmet_set_status(req, status);
388
389 /* XXX: need to fill in something useful for sq_head */
390 req->rsp->sq_head = 0;
391 if (likely(req->sq)) /* may happen during early failure */
392 req->rsp->sq_id = cpu_to_le16(req->sq->qid);
393 req->rsp->command_id = req->cmd->common.command_id;
394
395 if (req->ns)
396 nvmet_put_namespace(req->ns);
397 req->ops->queue_response(req);
398 }
399
nvmet_req_complete(struct nvmet_req * req,u16 status)400 void nvmet_req_complete(struct nvmet_req *req, u16 status)
401 {
402 __nvmet_req_complete(req, status);
403 percpu_ref_put(&req->sq->ref);
404 }
405 EXPORT_SYMBOL_GPL(nvmet_req_complete);
406
nvmet_cq_setup(struct nvmet_ctrl * ctrl,struct nvmet_cq * cq,u16 qid,u16 size)407 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
408 u16 qid, u16 size)
409 {
410 cq->qid = qid;
411 cq->size = size;
412
413 ctrl->cqs[qid] = cq;
414 }
415
nvmet_sq_setup(struct nvmet_ctrl * ctrl,struct nvmet_sq * sq,u16 qid,u16 size)416 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
417 u16 qid, u16 size)
418 {
419 sq->qid = qid;
420 sq->size = size;
421
422 ctrl->sqs[qid] = sq;
423 }
424
nvmet_confirm_sq(struct percpu_ref * ref)425 static void nvmet_confirm_sq(struct percpu_ref *ref)
426 {
427 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
428
429 complete(&sq->confirm_done);
430 }
431
nvmet_sq_destroy(struct nvmet_sq * sq)432 void nvmet_sq_destroy(struct nvmet_sq *sq)
433 {
434 /*
435 * If this is the admin queue, complete all AERs so that our
436 * queue doesn't have outstanding requests on it.
437 */
438 if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
439 nvmet_async_events_free(sq->ctrl);
440 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
441 wait_for_completion(&sq->confirm_done);
442 wait_for_completion(&sq->free_done);
443 percpu_ref_exit(&sq->ref);
444
445 if (sq->ctrl) {
446 nvmet_ctrl_put(sq->ctrl);
447 sq->ctrl = NULL; /* allows reusing the queue later */
448 }
449 }
450 EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
451
nvmet_sq_free(struct percpu_ref * ref)452 static void nvmet_sq_free(struct percpu_ref *ref)
453 {
454 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
455
456 complete(&sq->free_done);
457 }
458
nvmet_sq_init(struct nvmet_sq * sq)459 int nvmet_sq_init(struct nvmet_sq *sq)
460 {
461 int ret;
462
463 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
464 if (ret) {
465 pr_err("percpu_ref init failed!\n");
466 return ret;
467 }
468 init_completion(&sq->free_done);
469 init_completion(&sq->confirm_done);
470
471 return 0;
472 }
473 EXPORT_SYMBOL_GPL(nvmet_sq_init);
474
nvmet_req_init(struct nvmet_req * req,struct nvmet_cq * cq,struct nvmet_sq * sq,struct nvmet_fabrics_ops * ops)475 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
476 struct nvmet_sq *sq, struct nvmet_fabrics_ops *ops)
477 {
478 u8 flags = req->cmd->common.flags;
479 u16 status;
480
481 req->cq = cq;
482 req->sq = sq;
483 req->ops = ops;
484 req->sg = NULL;
485 req->sg_cnt = 0;
486 req->rsp->status = 0;
487
488 /* no support for fused commands yet */
489 if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
490 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
491 goto fail;
492 }
493
494 /* either variant of SGLs is fine, as we don't support metadata */
495 if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF &&
496 (flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METASEG)) {
497 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
498 goto fail;
499 }
500
501 if (unlikely(!req->sq->ctrl))
502 /* will return an error for any Non-connect command: */
503 status = nvmet_parse_connect_cmd(req);
504 else if (likely(req->sq->qid != 0))
505 status = nvmet_parse_io_cmd(req);
506 else if (req->cmd->common.opcode == nvme_fabrics_command)
507 status = nvmet_parse_fabrics_cmd(req);
508 else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
509 status = nvmet_parse_discovery_cmd(req);
510 else
511 status = nvmet_parse_admin_cmd(req);
512
513 if (status)
514 goto fail;
515
516 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
517 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
518 goto fail;
519 }
520
521 return true;
522
523 fail:
524 __nvmet_req_complete(req, status);
525 return false;
526 }
527 EXPORT_SYMBOL_GPL(nvmet_req_init);
528
nvmet_cc_en(u32 cc)529 static inline bool nvmet_cc_en(u32 cc)
530 {
531 return cc & 0x1;
532 }
533
nvmet_cc_css(u32 cc)534 static inline u8 nvmet_cc_css(u32 cc)
535 {
536 return (cc >> 4) & 0x7;
537 }
538
nvmet_cc_mps(u32 cc)539 static inline u8 nvmet_cc_mps(u32 cc)
540 {
541 return (cc >> 7) & 0xf;
542 }
543
nvmet_cc_ams(u32 cc)544 static inline u8 nvmet_cc_ams(u32 cc)
545 {
546 return (cc >> 11) & 0x7;
547 }
548
nvmet_cc_shn(u32 cc)549 static inline u8 nvmet_cc_shn(u32 cc)
550 {
551 return (cc >> 14) & 0x3;
552 }
553
nvmet_cc_iosqes(u32 cc)554 static inline u8 nvmet_cc_iosqes(u32 cc)
555 {
556 return (cc >> 16) & 0xf;
557 }
558
nvmet_cc_iocqes(u32 cc)559 static inline u8 nvmet_cc_iocqes(u32 cc)
560 {
561 return (cc >> 20) & 0xf;
562 }
563
nvmet_start_ctrl(struct nvmet_ctrl * ctrl)564 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
565 {
566 lockdep_assert_held(&ctrl->lock);
567
568 if (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
569 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES ||
570 nvmet_cc_mps(ctrl->cc) != 0 ||
571 nvmet_cc_ams(ctrl->cc) != 0 ||
572 nvmet_cc_css(ctrl->cc) != 0) {
573 ctrl->csts = NVME_CSTS_CFS;
574 return;
575 }
576
577 ctrl->csts = NVME_CSTS_RDY;
578 }
579
nvmet_clear_ctrl(struct nvmet_ctrl * ctrl)580 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
581 {
582 lockdep_assert_held(&ctrl->lock);
583
584 /* XXX: tear down queues? */
585 ctrl->csts &= ~NVME_CSTS_RDY;
586 ctrl->cc = 0;
587 }
588
nvmet_update_cc(struct nvmet_ctrl * ctrl,u32 new)589 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
590 {
591 u32 old;
592
593 mutex_lock(&ctrl->lock);
594 old = ctrl->cc;
595 ctrl->cc = new;
596
597 if (nvmet_cc_en(new) && !nvmet_cc_en(old))
598 nvmet_start_ctrl(ctrl);
599 if (!nvmet_cc_en(new) && nvmet_cc_en(old))
600 nvmet_clear_ctrl(ctrl);
601 if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
602 nvmet_clear_ctrl(ctrl);
603 ctrl->csts |= NVME_CSTS_SHST_CMPLT;
604 }
605 if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
606 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
607 mutex_unlock(&ctrl->lock);
608 }
609
nvmet_init_cap(struct nvmet_ctrl * ctrl)610 static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
611 {
612 /* command sets supported: NVMe command set: */
613 ctrl->cap = (1ULL << 37);
614 /* CC.EN timeout in 500msec units: */
615 ctrl->cap |= (15ULL << 24);
616 /* maximum queue entries supported: */
617 ctrl->cap |= NVMET_QUEUE_SIZE - 1;
618 }
619
nvmet_ctrl_find_get(const char * subsysnqn,const char * hostnqn,u16 cntlid,struct nvmet_req * req,struct nvmet_ctrl ** ret)620 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
621 struct nvmet_req *req, struct nvmet_ctrl **ret)
622 {
623 struct nvmet_subsys *subsys;
624 struct nvmet_ctrl *ctrl;
625 u16 status = 0;
626
627 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
628 if (!subsys) {
629 pr_warn("connect request for invalid subsystem %s!\n",
630 subsysnqn);
631 req->rsp->result = IPO_IATTR_CONNECT_DATA(subsysnqn);
632 return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
633 }
634
635 mutex_lock(&subsys->lock);
636 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
637 if (ctrl->cntlid == cntlid) {
638 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
639 pr_warn("hostnqn mismatch.\n");
640 continue;
641 }
642 if (!kref_get_unless_zero(&ctrl->ref))
643 continue;
644
645 *ret = ctrl;
646 goto out;
647 }
648 }
649
650 pr_warn("could not find controller %d for subsys %s / host %s\n",
651 cntlid, subsysnqn, hostnqn);
652 req->rsp->result = IPO_IATTR_CONNECT_DATA(cntlid);
653 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
654
655 out:
656 mutex_unlock(&subsys->lock);
657 nvmet_subsys_put(subsys);
658 return status;
659 }
660
__nvmet_host_allowed(struct nvmet_subsys * subsys,const char * hostnqn)661 static bool __nvmet_host_allowed(struct nvmet_subsys *subsys,
662 const char *hostnqn)
663 {
664 struct nvmet_host_link *p;
665
666 if (subsys->allow_any_host)
667 return true;
668
669 list_for_each_entry(p, &subsys->hosts, entry) {
670 if (!strcmp(nvmet_host_name(p->host), hostnqn))
671 return true;
672 }
673
674 return false;
675 }
676
nvmet_host_discovery_allowed(struct nvmet_req * req,const char * hostnqn)677 static bool nvmet_host_discovery_allowed(struct nvmet_req *req,
678 const char *hostnqn)
679 {
680 struct nvmet_subsys_link *s;
681
682 list_for_each_entry(s, &req->port->subsystems, entry) {
683 if (__nvmet_host_allowed(s->subsys, hostnqn))
684 return true;
685 }
686
687 return false;
688 }
689
nvmet_host_allowed(struct nvmet_req * req,struct nvmet_subsys * subsys,const char * hostnqn)690 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
691 const char *hostnqn)
692 {
693 lockdep_assert_held(&nvmet_config_sem);
694
695 if (subsys->type == NVME_NQN_DISC)
696 return nvmet_host_discovery_allowed(req, hostnqn);
697 else
698 return __nvmet_host_allowed(subsys, hostnqn);
699 }
700
nvmet_alloc_ctrl(const char * subsysnqn,const char * hostnqn,struct nvmet_req * req,u32 kato,struct nvmet_ctrl ** ctrlp)701 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
702 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
703 {
704 struct nvmet_subsys *subsys;
705 struct nvmet_ctrl *ctrl;
706 int ret;
707 u16 status;
708
709 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
710 subsys = nvmet_find_get_subsys(req->port, subsysnqn);
711 if (!subsys) {
712 pr_warn("connect request for invalid subsystem %s!\n",
713 subsysnqn);
714 req->rsp->result = IPO_IATTR_CONNECT_DATA(subsysnqn);
715 goto out;
716 }
717
718 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
719 down_read(&nvmet_config_sem);
720 if (!nvmet_host_allowed(req, subsys, hostnqn)) {
721 pr_info("connect by host %s for subsystem %s not allowed\n",
722 hostnqn, subsysnqn);
723 req->rsp->result = IPO_IATTR_CONNECT_DATA(hostnqn);
724 up_read(&nvmet_config_sem);
725 goto out_put_subsystem;
726 }
727 up_read(&nvmet_config_sem);
728
729 status = NVME_SC_INTERNAL;
730 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
731 if (!ctrl)
732 goto out_put_subsystem;
733 mutex_init(&ctrl->lock);
734
735 nvmet_init_cap(ctrl);
736
737 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
738 INIT_LIST_HEAD(&ctrl->async_events);
739
740 memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
741 memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
742
743 /* generate a random serial number as our controllers are ephemeral: */
744 get_random_bytes(&ctrl->serial, sizeof(ctrl->serial));
745
746 kref_init(&ctrl->ref);
747 ctrl->subsys = subsys;
748
749 ctrl->cqs = kcalloc(subsys->max_qid + 1,
750 sizeof(struct nvmet_cq *),
751 GFP_KERNEL);
752 if (!ctrl->cqs)
753 goto out_free_ctrl;
754
755 ctrl->sqs = kcalloc(subsys->max_qid + 1,
756 sizeof(struct nvmet_sq *),
757 GFP_KERNEL);
758 if (!ctrl->sqs)
759 goto out_free_cqs;
760
761 ret = ida_simple_get(&subsys->cntlid_ida,
762 NVME_CNTLID_MIN, NVME_CNTLID_MAX,
763 GFP_KERNEL);
764 if (ret < 0) {
765 status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
766 goto out_free_sqs;
767 }
768 ctrl->cntlid = ret;
769
770 ctrl->ops = req->ops;
771 if (ctrl->subsys->type == NVME_NQN_DISC) {
772 /* Don't accept keep-alive timeout for discovery controllers */
773 if (kato) {
774 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
775 goto out_free_sqs;
776 }
777
778 /*
779 * Discovery controllers use some arbitrary high value in order
780 * to cleanup stale discovery sessions
781 *
782 * From the latest base diff RC:
783 * "The Keep Alive command is not supported by
784 * Discovery controllers. A transport may specify a
785 * fixed Discovery controller activity timeout value
786 * (e.g., 2 minutes). If no commands are received
787 * by a Discovery controller within that time
788 * period, the controller may perform the
789 * actions for Keep Alive Timer expiration".
790 */
791 ctrl->kato = NVMET_DISC_KATO;
792 } else {
793 /* keep-alive timeout in seconds */
794 ctrl->kato = DIV_ROUND_UP(kato, 1000);
795 }
796 nvmet_start_keep_alive_timer(ctrl);
797
798 mutex_lock(&subsys->lock);
799 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
800 mutex_unlock(&subsys->lock);
801
802 *ctrlp = ctrl;
803 return 0;
804
805 out_free_sqs:
806 kfree(ctrl->sqs);
807 out_free_cqs:
808 kfree(ctrl->cqs);
809 out_free_ctrl:
810 kfree(ctrl);
811 out_put_subsystem:
812 nvmet_subsys_put(subsys);
813 out:
814 return status;
815 }
816
nvmet_ctrl_free(struct kref * ref)817 static void nvmet_ctrl_free(struct kref *ref)
818 {
819 struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
820 struct nvmet_subsys *subsys = ctrl->subsys;
821
822 nvmet_stop_keep_alive_timer(ctrl);
823
824 mutex_lock(&subsys->lock);
825 list_del(&ctrl->subsys_entry);
826 mutex_unlock(&subsys->lock);
827
828 flush_work(&ctrl->async_event_work);
829 cancel_work_sync(&ctrl->fatal_err_work);
830
831 ida_simple_remove(&subsys->cntlid_ida, ctrl->cntlid);
832 nvmet_subsys_put(subsys);
833
834 kfree(ctrl->sqs);
835 kfree(ctrl->cqs);
836 kfree(ctrl);
837 }
838
nvmet_ctrl_put(struct nvmet_ctrl * ctrl)839 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
840 {
841 kref_put(&ctrl->ref, nvmet_ctrl_free);
842 }
843
nvmet_fatal_error_handler(struct work_struct * work)844 static void nvmet_fatal_error_handler(struct work_struct *work)
845 {
846 struct nvmet_ctrl *ctrl =
847 container_of(work, struct nvmet_ctrl, fatal_err_work);
848
849 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
850 ctrl->ops->delete_ctrl(ctrl);
851 }
852
nvmet_ctrl_fatal_error(struct nvmet_ctrl * ctrl)853 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
854 {
855 mutex_lock(&ctrl->lock);
856 if (!(ctrl->csts & NVME_CSTS_CFS)) {
857 ctrl->csts |= NVME_CSTS_CFS;
858 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
859 schedule_work(&ctrl->fatal_err_work);
860 }
861 mutex_unlock(&ctrl->lock);
862 }
863 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
864
nvmet_find_get_subsys(struct nvmet_port * port,const char * subsysnqn)865 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
866 const char *subsysnqn)
867 {
868 struct nvmet_subsys_link *p;
869
870 if (!port)
871 return NULL;
872
873 if (!strncmp(NVME_DISC_SUBSYS_NAME, subsysnqn,
874 NVMF_NQN_SIZE)) {
875 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
876 return NULL;
877 return nvmet_disc_subsys;
878 }
879
880 down_read(&nvmet_config_sem);
881 list_for_each_entry(p, &port->subsystems, entry) {
882 if (!strncmp(p->subsys->subsysnqn, subsysnqn,
883 NVMF_NQN_SIZE)) {
884 if (!kref_get_unless_zero(&p->subsys->ref))
885 break;
886 up_read(&nvmet_config_sem);
887 return p->subsys;
888 }
889 }
890 up_read(&nvmet_config_sem);
891 return NULL;
892 }
893
nvmet_subsys_alloc(const char * subsysnqn,enum nvme_subsys_type type)894 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
895 enum nvme_subsys_type type)
896 {
897 struct nvmet_subsys *subsys;
898
899 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
900 if (!subsys)
901 return NULL;
902
903 subsys->ver = NVME_VS(1, 2, 1); /* NVMe 1.2.1 */
904
905 switch (type) {
906 case NVME_NQN_NVME:
907 subsys->max_qid = NVMET_NR_QUEUES;
908 break;
909 case NVME_NQN_DISC:
910 subsys->max_qid = 0;
911 break;
912 default:
913 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
914 kfree(subsys);
915 return NULL;
916 }
917 subsys->type = type;
918 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
919 GFP_KERNEL);
920 if (!subsys->subsysnqn) {
921 kfree(subsys);
922 return NULL;
923 }
924
925 kref_init(&subsys->ref);
926
927 mutex_init(&subsys->lock);
928 INIT_LIST_HEAD(&subsys->namespaces);
929 INIT_LIST_HEAD(&subsys->ctrls);
930
931 ida_init(&subsys->cntlid_ida);
932
933 INIT_LIST_HEAD(&subsys->hosts);
934
935 return subsys;
936 }
937
nvmet_subsys_free(struct kref * ref)938 static void nvmet_subsys_free(struct kref *ref)
939 {
940 struct nvmet_subsys *subsys =
941 container_of(ref, struct nvmet_subsys, ref);
942
943 WARN_ON_ONCE(!list_empty(&subsys->namespaces));
944
945 ida_destroy(&subsys->cntlid_ida);
946 kfree(subsys->subsysnqn);
947 kfree(subsys);
948 }
949
nvmet_subsys_put(struct nvmet_subsys * subsys)950 void nvmet_subsys_put(struct nvmet_subsys *subsys)
951 {
952 kref_put(&subsys->ref, nvmet_subsys_free);
953 }
954
nvmet_init(void)955 static int __init nvmet_init(void)
956 {
957 int error;
958
959 error = nvmet_init_discovery();
960 if (error)
961 goto out;
962
963 error = nvmet_init_configfs();
964 if (error)
965 goto out_exit_discovery;
966 return 0;
967
968 out_exit_discovery:
969 nvmet_exit_discovery();
970 out:
971 return error;
972 }
973
nvmet_exit(void)974 static void __exit nvmet_exit(void)
975 {
976 nvmet_exit_configfs();
977 nvmet_exit_discovery();
978
979 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
980 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
981 }
982
983 module_init(nvmet_init);
984 module_exit(nvmet_exit);
985
986 MODULE_LICENSE("GPL v2");
987