discovery.c 10.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12
/*
 * Discovery service for the NVMe over Fabrics target.
 * Copyright (C) 2016 Intel Corporation. All rights reserved.
 */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/slab.h>
#include <generated/utsrelease.h>
#include "nvmet.h"

struct nvmet_subsys *nvmet_disc_subsys;

13
static u64 nvmet_genctr;
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
static void __nvmet_disc_changed(struct nvmet_port *port,
				 struct nvmet_ctrl *ctrl)
{
	if (ctrl->port != port)
		return;

	if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_DISC_CHANGE))
		return;

	nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
			      NVME_AER_NOTICE_DISC_CHANGED, NVME_LOG_DISC);
}

void nvmet_port_disc_changed(struct nvmet_port *port,
			     struct nvmet_subsys *subsys)
{
	struct nvmet_ctrl *ctrl;

33
	lockdep_assert_held(&nvmet_config_sem);
34 35
	nvmet_genctr++;

36
	mutex_lock(&nvmet_disc_subsys->lock);
37 38 39 40 41 42
	list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) {
		if (subsys && !nvmet_host_allowed(subsys, ctrl->hostnqn))
			continue;

		__nvmet_disc_changed(port, ctrl);
	}
43
	mutex_unlock(&nvmet_disc_subsys->lock);
44 45 46 47

	/* If transport can signal change, notify transport */
	if (port->tr_ops && port->tr_ops->discovery_chg)
		port->tr_ops->discovery_chg(port);
48 49 50 51 52 53 54 55
}

static void __nvmet_subsys_disc_changed(struct nvmet_port *port,
					struct nvmet_subsys *subsys,
					struct nvmet_host *host)
{
	struct nvmet_ctrl *ctrl;

56
	mutex_lock(&nvmet_disc_subsys->lock);
57 58 59 60 61 62
	list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) {
		if (host && strcmp(nvmet_host_name(host), ctrl->hostnqn))
			continue;

		__nvmet_disc_changed(port, ctrl);
	}
63
	mutex_unlock(&nvmet_disc_subsys->lock);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
			       struct nvmet_host *host)
{
	struct nvmet_port *port;
	struct nvmet_subsys_link *s;

	nvmet_genctr++;

	list_for_each_entry(port, nvmet_ports, global_entry)
		list_for_each_entry(s, &port->subsystems, entry) {
			if (s->subsys != subsys)
				continue;
			__nvmet_subsys_disc_changed(port, subsys, host);
		}
}

82 83 84 85 86 87
void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port)
{
	down_write(&nvmet_config_sem);
	if (list_empty(&port->entry)) {
		list_add_tail(&port->entry, &parent->referrals);
		port->enabled = true;
88
		nvmet_port_disc_changed(parent, NULL);
89 90 91 92
	}
	up_write(&nvmet_config_sem);
}

93
void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port)
94 95 96 97 98
{
	down_write(&nvmet_config_sem);
	if (!list_empty(&port->entry)) {
		port->enabled = false;
		list_del_init(&port->entry);
99
		nvmet_port_disc_changed(parent, NULL);
100 101 102 103 104
	}
	up_write(&nvmet_config_sem);
}

static void nvmet_format_discovery_entry(struct nvmf_disc_rsp_page_hdr *hdr,
105 106
		struct nvmet_port *port, char *subsys_nqn, char *traddr,
		u8 type, u32 numrec)
107 108 109 110 111 112 113 114 115
{
	struct nvmf_disc_rsp_page_entry *e = &hdr->entries[numrec];

	e->trtype = port->disc_addr.trtype;
	e->adrfam = port->disc_addr.adrfam;
	e->treq = port->disc_addr.treq;
	e->portid = port->disc_addr.portid;
	/* we support only dynamic controllers */
	e->cntlid = cpu_to_le16(NVME_CNTLID_DYNAMIC);
116
	e->asqsz = cpu_to_le16(NVME_AQ_DEPTH);
C
Christoph Hellwig 已提交
117
	e->subtype = type;
118
	memcpy(e->trsvcid, port->disc_addr.trsvcid, NVMF_TRSVCID_SIZE);
119
	memcpy(e->traddr, traddr, NVMF_TRADDR_SIZE);
120
	memcpy(e->tsas.common, port->disc_addr.tsas.common, NVMF_TSAS_SIZE);
121
	strncpy(e->subnqn, subsys_nqn, NVMF_NQN_SIZE);
122 123
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/*
 * nvmet_set_disc_traddr - set a correct discovery log entry traddr
 *
 * IP based transports (e.g RDMA) can listen on "any" ipv4/ipv6 addresses
 * (INADDR_ANY or IN6ADDR_ANY_INIT). The discovery log page traddr reply
 * must not contain that "any" IP address. If the transport implements
 * .disc_traddr, use it. this callback will set the discovery traddr
 * from the req->port address in case the port in question listens
 * "any" IP address.
 */
static void nvmet_set_disc_traddr(struct nvmet_req *req, struct nvmet_port *port,
		char *traddr)
{
	if (req->ops->disc_traddr)
		req->ops->disc_traddr(req, port, traddr);
	else
		memcpy(traddr, port->disc_addr.traddr, NVMF_TRADDR_SIZE);
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
static size_t discovery_log_entries(struct nvmet_req *req)
{
	struct nvmet_ctrl *ctrl = req->sq->ctrl;
	struct nvmet_subsys_link *p;
	struct nvmet_port *r;
	size_t entries = 0;

	list_for_each_entry(p, &req->port->subsystems, entry) {
		if (!nvmet_host_allowed(p->subsys, ctrl->hostnqn))
			continue;
		entries++;
	}
	list_for_each_entry(r, &req->port->referrals, entry)
		entries++;
	return entries;
}

160 161 162 163 164
static void nvmet_execute_get_disc_log_page(struct nvmet_req *req)
{
	const int entry_size = sizeof(struct nvmf_disc_rsp_page_entry);
	struct nvmet_ctrl *ctrl = req->sq->ctrl;
	struct nvmf_disc_rsp_page_hdr *hdr;
165
	u64 offset = nvmet_get_log_page_offset(req->cmd);
166
	size_t data_len = nvmet_get_log_page_len(req->cmd);
167
	size_t alloc_len;
168 169 170 171
	struct nvmet_subsys_link *p;
	struct nvmet_port *r;
	u32 numrec = 0;
	u16 status = 0;
172 173 174 175 176 177 178
	void *buffer;

	/* Spec requires dword aligned offsets */
	if (offset & 0x3) {
		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
		goto out;
	}
179 180 181 182 183 184

	/*
	 * Make sure we're passing at least a buffer of response header size.
	 * If host provided data len is less than the header size, only the
	 * number of bytes requested by host will be sent to host.
	 */
185 186 187 188 189
	down_read(&nvmet_config_sem);
	alloc_len = sizeof(*hdr) + entry_size * discovery_log_entries(req);
	buffer = kzalloc(alloc_len, GFP_KERNEL);
	if (!buffer) {
		up_read(&nvmet_config_sem);
190 191 192 193
		status = NVME_SC_INTERNAL;
		goto out;
	}

194
	hdr = buffer;
195
	list_for_each_entry(p, &req->port->subsystems, entry) {
196 197
		char traddr[NVMF_TRADDR_SIZE];

198
		if (!nvmet_host_allowed(p->subsys, ctrl->hostnqn))
199
			continue;
200 201 202 203 204

		nvmet_set_disc_traddr(req, req->port, traddr);
		nvmet_format_discovery_entry(hdr, req->port,
				p->subsys->subsysnqn, traddr,
				NVME_NQN_NVME, numrec);
205 206 207 208
		numrec++;
	}

	list_for_each_entry(r, &req->port->referrals, entry) {
209 210 211 212
		nvmet_format_discovery_entry(hdr, r,
				NVME_DISC_SUBSYS_NAME,
				r->disc_addr.traddr,
				NVME_NQN_DISC, numrec);
213 214 215 216 217 218 219
		numrec++;
	}

	hdr->genctr = cpu_to_le64(nvmet_genctr);
	hdr->numrec = cpu_to_le64(numrec);
	hdr->recfmt = cpu_to_le16(0);

220 221
	nvmet_clear_aen_bit(req, NVME_AEN_BIT_DISC_CHANGE);

222 223
	up_read(&nvmet_config_sem);

224 225
	status = nvmet_copy_to_sgl(req, 0, buffer + offset, data_len);
	kfree(buffer);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
out:
	nvmet_req_complete(req, status);
}

static void nvmet_execute_identify_disc_ctrl(struct nvmet_req *req)
{
	struct nvmet_ctrl *ctrl = req->sq->ctrl;
	struct nvme_id_ctrl *id;
	u16 status = 0;

	id = kzalloc(sizeof(*id), GFP_KERNEL);
	if (!id) {
		status = NVME_SC_INTERNAL;
		goto out;
	}

	memset(id->fr, ' ', sizeof(id->fr));
	strncpy((char *)id->fr, UTS_RELEASE, sizeof(id->fr));

	/* no limit on data transfer sizes for now */
	id->mdts = 0;
	id->cntlid = cpu_to_le16(ctrl->cntlid);
	id->ver = cpu_to_le32(ctrl->subsys->ver);
	id->lpa = (1 << 2);

	/* no enforcement soft-limit for maxcmd - pick arbitrary high value */
	id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);

	id->sgls = cpu_to_le32(1 << 0);	/* we always support SGLs */
	if (ctrl->ops->has_keyed_sgls)
		id->sgls |= cpu_to_le32(1 << 2);
257
	if (req->port->inline_data_size)
258 259
		id->sgls |= cpu_to_le32(1 << 20);

260 261
	id->oaes = cpu_to_le32(NVMET_DISC_AEN_CFG_OPTIONAL);

262
	strlcpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn));
263 264 265 266 267 268 269 270

	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));

	kfree(id);
out:
	nvmet_req_complete(req, status);
}

271 272
static void nvmet_execute_disc_set_features(struct nvmet_req *req)
{
273
	u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
274 275 276 277 278 279 280 281 282 283 284
	u16 stat;

	switch (cdw10 & 0xff) {
	case NVME_FEAT_KATO:
		stat = nvmet_set_feat_kato(req);
		break;
	case NVME_FEAT_ASYNC_EVENT:
		stat = nvmet_set_feat_async_event(req,
						  NVMET_DISC_AEN_CFG_OPTIONAL);
		break;
	default:
285 286
		req->error_loc =
			offsetof(struct nvme_common_command, cdw10);
287 288 289 290 291 292 293 294 295
		stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
		break;
	}

	nvmet_req_complete(req, stat);
}

static void nvmet_execute_disc_get_features(struct nvmet_req *req)
{
296
	u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
297 298 299 300 301 302 303 304 305 306
	u16 stat = 0;

	switch (cdw10 & 0xff) {
	case NVME_FEAT_KATO:
		nvmet_get_feat_kato(req);
		break;
	case NVME_FEAT_ASYNC_EVENT:
		nvmet_get_feat_async_event(req);
		break;
	default:
307 308
		req->error_loc =
			offsetof(struct nvme_common_command, cdw10);
309 310 311 312 313 314 315
		stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
		break;
	}

	nvmet_req_complete(req, stat);
}

316
u16 nvmet_parse_discovery_cmd(struct nvmet_req *req)
317 318 319 320
{
	struct nvme_command *cmd = req->cmd;

	if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
321 322
		pr_err("got cmd %d while not ready\n",
		       cmd->common.opcode);
323 324
		req->error_loc =
			offsetof(struct nvme_common_command, opcode);
325 326 327 328
		return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
	}

	switch (cmd->common.opcode) {
329 330 331 332 333 334 335 336 337 338 339 340
	case nvme_admin_set_features:
		req->execute = nvmet_execute_disc_set_features;
		req->data_len = 0;
		return 0;
	case nvme_admin_get_features:
		req->execute = nvmet_execute_disc_get_features;
		req->data_len = 0;
		return 0;
	case nvme_admin_async_event:
		req->execute = nvmet_execute_async_event;
		req->data_len = 0;
		return 0;
341 342 343 344
	case nvme_admin_keep_alive:
		req->execute = nvmet_execute_keep_alive;
		req->data_len = 0;
		return 0;
345 346 347 348 349 350 351 352
	case nvme_admin_get_log_page:
		req->data_len = nvmet_get_log_page_len(cmd);

		switch (cmd->get_log_page.lid) {
		case NVME_LOG_DISC:
			req->execute = nvmet_execute_get_disc_log_page;
			return 0;
		default:
353 354
			pr_err("unsupported get_log_page lid %d\n",
			       cmd->get_log_page.lid);
355 356
			req->error_loc =
				offsetof(struct nvme_get_log_page_command, lid);
B
Bart Van Assche 已提交
357
			return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
358 359
		}
	case nvme_admin_identify:
360
		req->data_len = NVME_IDENTIFY_DATA_SIZE;
361
		switch (cmd->identify.cns) {
362
		case NVME_ID_CNS_CTRL:
363 364 365 366
			req->execute =
				nvmet_execute_identify_disc_ctrl;
			return 0;
		default:
367 368
			pr_err("unsupported identify cns %d\n",
			       cmd->identify.cns);
369
			req->error_loc = offsetof(struct nvme_identify, cns);
370 371 372
			return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
		}
	default:
373
		pr_err("unhandled cmd %d\n", cmd->common.opcode);
374
		req->error_loc = offsetof(struct nvme_common_command, opcode);
375 376 377 378 379 380 381 382 383
		return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
	}

}

int __init nvmet_init_discovery(void)
{
	nvmet_disc_subsys =
		nvmet_subsys_alloc(NVME_DISC_SUBSYS_NAME, NVME_NQN_DISC);
384
	return PTR_ERR_OR_ZERO(nvmet_disc_subsys);
385 386 387 388 389 390
}

void nvmet_exit_discovery(void)
{
	nvmet_subsys_put(nvmet_disc_subsys);
}