fabrics-cmd.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_STATUS_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_STATUS_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_STATUS_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_STATUS_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_STATUS_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_STATUS_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_STATUS_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_STATUS_DNR;
  140. }
  141. /* for fabrics, this value applies to only the I/O Submission Queues */
  142. if (qid && sqsize > mqes) {
  143. pr_warn("sqsize %u is larger than MQES supported %u cntlid %d\n",
  144. sqsize, mqes, ctrl->cntlid);
  145. req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
  146. req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
  147. return NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
  148. }
  149. old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
  150. if (old) {
  151. pr_warn("queue already connected!\n");
  152. req->error_loc = offsetof(struct nvmf_connect_command, opcode);
  153. return NVME_SC_CONNECT_CTRL_BUSY | NVME_STATUS_DNR;
  154. }
  155. /* note: convert queue size from 0's-based value to 1's-based value */
  156. nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1);
  157. nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1);
  158. if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
  159. req->sq->sqhd_disabled = true;
  160. req->cqe->sq_head = cpu_to_le16(0xffff);
  161. }
  162. if (ctrl->ops->install_queue) {
  163. ret = ctrl->ops->install_queue(req->sq);
  164. if (ret) {
  165. pr_err("failed to install queue %d cntlid %d ret %x\n",
  166. qid, ctrl->cntlid, ret);
  167. ctrl->sqs[qid] = NULL;
  168. goto err;
  169. }
  170. }
  171. return 0;
  172. err:
  173. req->sq->ctrl = NULL;
  174. return ret;
  175. }
  176. static u32 nvmet_connect_result(struct nvmet_ctrl *ctrl)
  177. {
  178. return (u32)ctrl->cntlid |
  179. (nvmet_has_auth(ctrl) ? NVME_CONNECT_AUTHREQ_ATR : 0);
  180. }
  181. static void nvmet_execute_admin_connect(struct nvmet_req *req)
  182. {
  183. struct nvmf_connect_command *c = &req->cmd->connect;
  184. struct nvmf_connect_data *d;
  185. struct nvmet_ctrl *ctrl = NULL;
  186. u16 status;
  187. u8 dhchap_status;
  188. if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
  189. return;
  190. d = kmalloc(sizeof(*d), GFP_KERNEL);
  191. if (!d) {
  192. status = NVME_SC_INTERNAL;
  193. goto complete;
  194. }
  195. status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
  196. if (status)
  197. goto out;
  198. if (c->recfmt != 0) {
  199. pr_warn("invalid connect version (%d).\n",
  200. le16_to_cpu(c->recfmt));
  201. req->error_loc = offsetof(struct nvmf_connect_command, recfmt);
  202. status = NVME_SC_CONNECT_FORMAT | NVME_STATUS_DNR;
  203. goto out;
  204. }
  205. if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
  206. pr_warn("connect attempt for invalid controller ID %#x\n",
  207. d->cntlid);
  208. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
  209. req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
  210. goto out;
  211. }
  212. d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
  213. d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
  214. status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
  215. le32_to_cpu(c->kato), &ctrl);
  216. if (status)
  217. goto out;
  218. uuid_copy(&ctrl->hostid, &d->hostid);
  219. dhchap_status = nvmet_setup_auth(ctrl);
  220. if (dhchap_status) {
  221. pr_err("Failed to setup authentication, dhchap status %u\n",
  222. dhchap_status);
  223. nvmet_ctrl_put(ctrl);
  224. if (dhchap_status == NVME_AUTH_DHCHAP_FAILURE_FAILED)
  225. status = (NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR);
  226. else
  227. status = NVME_SC_INTERNAL;
  228. goto out;
  229. }
  230. status = nvmet_install_queue(ctrl, req);
  231. if (status) {
  232. nvmet_ctrl_put(ctrl);
  233. goto out;
  234. }
  235. pr_info("creating %s controller %d for subsystem %s for NQN %s%s%s.\n",
  236. nvmet_is_disc_subsys(ctrl->subsys) ? "discovery" : "nvm",
  237. ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn,
  238. ctrl->pi_support ? " T10-PI is enabled" : "",
  239. nvmet_has_auth(ctrl) ? " with DH-HMAC-CHAP" : "");
  240. req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
  241. out:
  242. kfree(d);
  243. complete:
  244. nvmet_req_complete(req, status);
  245. }
  246. static void nvmet_execute_io_connect(struct nvmet_req *req)
  247. {
  248. struct nvmf_connect_command *c = &req->cmd->connect;
  249. struct nvmf_connect_data *d;
  250. struct nvmet_ctrl *ctrl;
  251. u16 qid = le16_to_cpu(c->qid);
  252. u16 status;
  253. if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
  254. return;
  255. d = kmalloc(sizeof(*d), GFP_KERNEL);
  256. if (!d) {
  257. status = NVME_SC_INTERNAL;
  258. goto complete;
  259. }
  260. status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
  261. if (status)
  262. goto out;
  263. if (c->recfmt != 0) {
  264. pr_warn("invalid connect version (%d).\n",
  265. le16_to_cpu(c->recfmt));
  266. status = NVME_SC_CONNECT_FORMAT | NVME_STATUS_DNR;
  267. goto out;
  268. }
  269. d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
  270. d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
  271. ctrl = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
  272. le16_to_cpu(d->cntlid), req);
  273. if (!ctrl) {
  274. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
  275. goto out;
  276. }
  277. if (unlikely(qid > ctrl->subsys->max_qid)) {
  278. pr_warn("invalid queue id (%d)\n", qid);
  279. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
  280. req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
  281. goto out_ctrl_put;
  282. }
  283. status = nvmet_install_queue(ctrl, req);
  284. if (status)
  285. goto out_ctrl_put;
  286. pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
  287. req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
  288. out:
  289. kfree(d);
  290. complete:
  291. nvmet_req_complete(req, status);
  292. return;
  293. out_ctrl_put:
  294. nvmet_ctrl_put(ctrl);
  295. goto out;
  296. }
  297. u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
  298. {
  299. struct nvme_command *cmd = req->cmd;
  300. if (!nvme_is_fabrics(cmd)) {
  301. pr_debug("invalid command 0x%x on unconnected queue.\n",
  302. cmd->fabrics.opcode);
  303. req->error_loc = offsetof(struct nvme_common_command, opcode);
  304. return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
  305. }
  306. if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
  307. pr_debug("invalid capsule type 0x%x on unconnected queue.\n",
  308. cmd->fabrics.fctype);
  309. req->error_loc = offsetof(struct nvmf_common_command, fctype);
  310. return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
  311. }
  312. if (cmd->connect.qid == 0)
  313. req->execute = nvmet_execute_admin_connect;
  314. else
  315. req->execute = nvmet_execute_io_connect;
  316. return 0;
  317. }