fabrics-cmd.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVMe Fabrics command implementation.
  4. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/blkdev.h>
  8. #include "nvmet.h"
  9. static void nvmet_execute_prop_set(struct nvmet_req *req)
  10. {
  11. u64 val = le64_to_cpu(req->cmd->prop_set.value);
  12. u16 status = 0;
  13. if (!nvmet_check_transfer_len(req, 0))
  14. return;
  15. if (req->cmd->prop_set.attrib & 1) {
  16. req->error_loc =
  17. offsetof(struct nvmf_property_set_command, attrib);
  18. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  19. goto out;
  20. }
  21. switch (le32_to_cpu(req->cmd->prop_set.offset)) {
  22. case NVME_REG_CC:
  23. nvmet_update_cc(req->sq->ctrl, val);
  24. break;
  25. default:
  26. req->error_loc =
  27. offsetof(struct nvmf_property_set_command, offset);
  28. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  29. }
  30. out:
  31. nvmet_req_complete(req, status);
  32. }
  33. static void nvmet_execute_prop_get(struct nvmet_req *req)
  34. {
  35. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  36. u16 status = 0;
  37. u64 val = 0;
  38. if (!nvmet_check_transfer_len(req, 0))
  39. return;
  40. if (req->cmd->prop_get.attrib & 1) {
  41. switch (le32_to_cpu(req->cmd->prop_get.offset)) {
  42. case NVME_REG_CAP:
  43. val = ctrl->cap;
  44. break;
  45. default:
  46. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  47. break;
  48. }
  49. } else {
  50. switch (le32_to_cpu(req->cmd->prop_get.offset)) {
  51. case NVME_REG_VS:
  52. val = ctrl->subsys->ver;
  53. break;
  54. case NVME_REG_CC:
  55. val = ctrl->cc;
  56. break;
  57. case NVME_REG_CSTS:
  58. val = ctrl->csts;
  59. break;
  60. default:
  61. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  62. break;
  63. }
  64. }
  65. if (status && req->cmd->prop_get.attrib & 1) {
  66. req->error_loc =
  67. offsetof(struct nvmf_property_get_command, offset);
  68. } else {
  69. req->error_loc =
  70. offsetof(struct nvmf_property_get_command, attrib);
  71. }
  72. req->cqe->result.u64 = cpu_to_le64(val);
  73. nvmet_req_complete(req, status);
  74. }
  75. u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req)
  76. {
  77. struct nvme_command *cmd = req->cmd;
  78. switch (cmd->fabrics.fctype) {
  79. case nvme_fabrics_type_property_set:
  80. req->execute = nvmet_execute_prop_set;
  81. break;
  82. case nvme_fabrics_type_property_get:
  83. req->execute = nvmet_execute_prop_get;
  84. break;
  85. #ifdef CONFIG_NVME_TARGET_AUTH
  86. case nvme_fabrics_type_auth_send:
  87. req->execute = nvmet_execute_auth_send;
  88. break;
  89. case nvme_fabrics_type_auth_receive:
  90. req->execute = nvmet_execute_auth_receive;
  91. break;
  92. #endif
  93. default:
  94. pr_debug("received unknown capsule type 0x%x\n",
  95. cmd->fabrics.fctype);
  96. req->error_loc = offsetof(struct nvmf_common_command, fctype);
  97. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  98. }
  99. return 0;
  100. }
  101. u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req)
  102. {
  103. struct nvme_command *cmd = req->cmd;
  104. switch (cmd->fabrics.fctype) {
  105. #ifdef CONFIG_NVME_TARGET_AUTH
  106. case nvme_fabrics_type_auth_send:
  107. req->execute = nvmet_execute_auth_send;
  108. break;
  109. case nvme_fabrics_type_auth_receive:
  110. req->execute = nvmet_execute_auth_receive;
  111. break;
  112. #endif
  113. default:
  114. pr_debug("received unknown capsule type 0x%x\n",
  115. cmd->fabrics.fctype);
  116. req->error_loc = offsetof(struct nvmf_common_command, fctype);
  117. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  118. }
  119. return 0;
  120. }
  121. static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
  122. {
  123. struct nvmf_connect_command *c = &req->cmd->connect;
  124. u16 qid = le16_to_cpu(c->qid);
  125. u16 sqsize = le16_to_cpu(c->sqsize);
  126. struct nvmet_ctrl *old;
  127. u16 mqes = NVME_CAP_MQES(ctrl->cap);
  128. u16 ret;
  129. if (!sqsize) {
  130. pr_warn("queue size zero!\n");
  131. req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
  132. req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
  133. ret = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  134. goto err;
  135. }
  136. if (ctrl->sqs[qid] != NULL) {
  137. pr_warn("qid %u has already been created\n", qid);
  138. req->error_loc = offsetof(struct nvmf_connect_command, qid);
  139. return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  140. }
  141. if (sqsize > mqes) {
  142. pr_warn("sqsize %u is larger than MQES supported %u cntlid %d\n",
  143. sqsize, mqes, ctrl->cntlid);
  144. req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
  145. req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
  146. return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  147. }
  148. old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
  149. if (old) {
  150. pr_warn("queue already connected!\n");
  151. req->error_loc = offsetof(struct nvmf_connect_command, opcode);
  152. return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
  153. }
  154. /* note: convert queue size from 0's-based value to 1's-based value */
  155. nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1);
  156. nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1);
  157. if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
  158. req->sq->sqhd_disabled = true;
  159. req->cqe->sq_head = cpu_to_le16(0xffff);
  160. }
  161. if (ctrl->ops->install_queue) {
  162. ret = ctrl->ops->install_queue(req->sq);
  163. if (ret) {
  164. pr_err("failed to install queue %d cntlid %d ret %x\n",
  165. qid, ctrl->cntlid, ret);
  166. ctrl->sqs[qid] = NULL;
  167. goto err;
  168. }
  169. }
  170. return 0;
  171. err:
  172. req->sq->ctrl = NULL;
  173. return ret;
  174. }
  175. static u32 nvmet_connect_result(struct nvmet_ctrl *ctrl)
  176. {
  177. return (u32)ctrl->cntlid |
  178. (nvmet_has_auth(ctrl) ? NVME_CONNECT_AUTHREQ_ATR : 0);
  179. }
  180. static void nvmet_execute_admin_connect(struct nvmet_req *req)
  181. {
  182. struct nvmf_connect_command *c = &req->cmd->connect;
  183. struct nvmf_connect_data *d;
  184. struct nvmet_ctrl *ctrl = NULL;
  185. u16 status = 0;
  186. int ret;
  187. if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
  188. return;
  189. d = kmalloc(sizeof(*d), GFP_KERNEL);
  190. if (!d) {
  191. status = NVME_SC_INTERNAL;
  192. goto complete;
  193. }
  194. status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
  195. if (status)
  196. goto out;
  197. /* zero out initial completion result, assign values as needed */
  198. req->cqe->result.u32 = 0;
  199. if (c->recfmt != 0) {
  200. pr_warn("invalid connect version (%d).\n",
  201. le16_to_cpu(c->recfmt));
  202. req->error_loc = offsetof(struct nvmf_connect_command, recfmt);
  203. status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
  204. goto out;
  205. }
  206. if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
  207. pr_warn("connect attempt for invalid controller ID %#x\n",
  208. d->cntlid);
  209. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  210. req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
  211. goto out;
  212. }
  213. d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
  214. d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
  215. status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
  216. le32_to_cpu(c->kato), &ctrl);
  217. if (status)
  218. goto out;
  219. ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support;
  220. uuid_copy(&ctrl->hostid, &d->hostid);
  221. ret = nvmet_setup_auth(ctrl);
  222. if (ret < 0) {
  223. pr_err("Failed to setup authentication, error %d\n", ret);
  224. nvmet_ctrl_put(ctrl);
  225. if (ret == -EPERM)
  226. status = (NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR);
  227. else
  228. status = NVME_SC_INTERNAL;
  229. goto out;
  230. }
  231. status = nvmet_install_queue(ctrl, req);
  232. if (status) {
  233. nvmet_ctrl_put(ctrl);
  234. goto out;
  235. }
  236. pr_info("creating %s controller %d for subsystem %s for NQN %s%s%s.\n",
  237. nvmet_is_disc_subsys(ctrl->subsys) ? "discovery" : "nvm",
  238. ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn,
  239. ctrl->pi_support ? " T10-PI is enabled" : "",
  240. nvmet_has_auth(ctrl) ? " with DH-HMAC-CHAP" : "");
  241. req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
  242. out:
  243. kfree(d);
  244. complete:
  245. nvmet_req_complete(req, status);
  246. }
  247. static void nvmet_execute_io_connect(struct nvmet_req *req)
  248. {
  249. struct nvmf_connect_command *c = &req->cmd->connect;
  250. struct nvmf_connect_data *d;
  251. struct nvmet_ctrl *ctrl;
  252. u16 qid = le16_to_cpu(c->qid);
  253. u16 status = 0;
  254. if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
  255. return;
  256. d = kmalloc(sizeof(*d), GFP_KERNEL);
  257. if (!d) {
  258. status = NVME_SC_INTERNAL;
  259. goto complete;
  260. }
  261. status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
  262. if (status)
  263. goto out;
  264. /* zero out initial completion result, assign values as needed */
  265. req->cqe->result.u32 = 0;
  266. if (c->recfmt != 0) {
  267. pr_warn("invalid connect version (%d).\n",
  268. le16_to_cpu(c->recfmt));
  269. status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
  270. goto out;
  271. }
  272. d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
  273. d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
  274. ctrl = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
  275. le16_to_cpu(d->cntlid), req);
  276. if (!ctrl) {
  277. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  278. goto out;
  279. }
  280. if (unlikely(qid > ctrl->subsys->max_qid)) {
  281. pr_warn("invalid queue id (%d)\n", qid);
  282. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  283. req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
  284. goto out_ctrl_put;
  285. }
  286. status = nvmet_install_queue(ctrl, req);
  287. if (status)
  288. goto out_ctrl_put;
  289. pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
  290. req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
  291. out:
  292. kfree(d);
  293. complete:
  294. nvmet_req_complete(req, status);
  295. return;
  296. out_ctrl_put:
  297. nvmet_ctrl_put(ctrl);
  298. goto out;
  299. }
  300. u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
  301. {
  302. struct nvme_command *cmd = req->cmd;
  303. if (!nvme_is_fabrics(cmd)) {
  304. pr_debug("invalid command 0x%x on unconnected queue.\n",
  305. cmd->fabrics.opcode);
  306. req->error_loc = offsetof(struct nvme_common_command, opcode);
  307. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  308. }
  309. if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
  310. pr_debug("invalid capsule type 0x%x on unconnected queue.\n",
  311. cmd->fabrics.fctype);
  312. req->error_loc = offsetof(struct nvmf_common_command, fctype);
  313. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  314. }
  315. if (cmd->connect.qid == 0)
  316. req->execute = nvmet_execute_admin_connect;
  317. else
  318. req->execute = nvmet_execute_io_connect;
  319. return 0;
  320. }