2 * NVMe Fabrics command implementation.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
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.
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
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/blkdev.h>
18 static void nvmet_execute_prop_set(struct nvmet_req *req)
22 if (!(req->cmd->prop_set.attrib & 1)) {
23 u64 val = le64_to_cpu(req->cmd->prop_set.value);
25 switch (le32_to_cpu(req->cmd->prop_set.offset)) {
27 nvmet_update_cc(req->sq->ctrl, val);
30 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
34 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
37 nvmet_req_complete(req, status);
40 static void nvmet_execute_prop_get(struct nvmet_req *req)
42 struct nvmet_ctrl *ctrl = req->sq->ctrl;
46 if (req->cmd->prop_get.attrib & 1) {
47 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
52 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
56 switch (le32_to_cpu(req->cmd->prop_get.offset)) {
58 val = ctrl->subsys->ver;
67 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
72 req->rsp->result.u64 = cpu_to_le64(val);
73 nvmet_req_complete(req, status);
76 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req)
78 struct nvme_command *cmd = req->cmd;
82 switch (cmd->fabrics.fctype) {
83 case nvme_fabrics_type_property_set:
85 req->execute = nvmet_execute_prop_set;
87 case nvme_fabrics_type_property_get:
89 req->execute = nvmet_execute_prop_get;
92 pr_err("received unknown capsule type 0x%x\n",
94 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
100 static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
102 struct nvmf_connect_command *c = &req->cmd->connect;
103 u16 qid = le16_to_cpu(c->qid);
104 u16 sqsize = le16_to_cpu(c->sqsize);
105 struct nvmet_ctrl *old;
107 old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
109 pr_warn("queue already connected!\n");
110 return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
113 nvmet_cq_setup(ctrl, req->cq, qid, sqsize);
114 nvmet_sq_setup(ctrl, req->sq, qid, sqsize);
118 static void nvmet_execute_admin_connect(struct nvmet_req *req)
120 struct nvmf_connect_command *c = &req->cmd->connect;
121 struct nvmf_connect_data *d;
122 struct nvmet_ctrl *ctrl = NULL;
125 d = kmalloc(sizeof(*d), GFP_KERNEL);
127 status = NVME_SC_INTERNAL;
131 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
135 /* zero out initial completion result, assign values as needed */
136 req->rsp->result.u32 = 0;
138 if (c->recfmt != 0) {
139 pr_warn("invalid connect version (%d).\n",
140 le16_to_cpu(c->recfmt));
141 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
145 if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
146 pr_warn("connect attempt for invalid controller ID %#x\n",
148 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
149 req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
153 status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
154 le32_to_cpu(c->kato), &ctrl);
157 uuid_copy(&ctrl->hostid, &d->hostid);
159 status = nvmet_install_queue(ctrl, req);
161 nvmet_ctrl_put(ctrl);
165 pr_info("creating controller %d for subsystem %s for NQN %s.\n",
166 ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn);
167 req->rsp->result.u16 = cpu_to_le16(ctrl->cntlid);
172 nvmet_req_complete(req, status);
175 static void nvmet_execute_io_connect(struct nvmet_req *req)
177 struct nvmf_connect_command *c = &req->cmd->connect;
178 struct nvmf_connect_data *d;
179 struct nvmet_ctrl *ctrl = NULL;
180 u16 qid = le16_to_cpu(c->qid);
183 d = kmalloc(sizeof(*d), GFP_KERNEL);
185 status = NVME_SC_INTERNAL;
189 status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
193 /* zero out initial completion result, assign values as needed */
194 req->rsp->result.u32 = 0;
196 if (c->recfmt != 0) {
197 pr_warn("invalid connect version (%d).\n",
198 le16_to_cpu(c->recfmt));
199 status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
203 status = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
204 le16_to_cpu(d->cntlid),
209 if (unlikely(qid > ctrl->subsys->max_qid)) {
210 pr_warn("invalid queue id (%d)\n", qid);
211 status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
212 req->rsp->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
216 status = nvmet_install_queue(ctrl, req);
218 /* pass back cntlid that had the issue of installing queue */
219 req->rsp->result.u16 = cpu_to_le16(ctrl->cntlid);
223 pr_info("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
228 nvmet_req_complete(req, status);
232 nvmet_ctrl_put(ctrl);
236 u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
238 struct nvme_command *cmd = req->cmd;
242 if (cmd->common.opcode != nvme_fabrics_command) {
243 pr_err("invalid command 0x%x on unconnected queue.\n",
244 cmd->fabrics.opcode);
245 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
247 if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
248 pr_err("invalid capsule type 0x%x on unconnected queue.\n",
249 cmd->fabrics.fctype);
250 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
253 req->data_len = sizeof(struct nvmf_connect_data);
254 if (cmd->connect.qid == 0)
255 req->execute = nvmet_execute_admin_connect;
257 req->execute = nvmet_execute_io_connect;