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