fabrics-cmd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_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. default:
  86. pr_err("received unknown capsule type 0x%x\n",
  87. cmd->fabrics.fctype);
  88. req->error_loc = offsetof(struct nvmf_common_command, fctype);
  89. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  90. }
  91. return 0;
  92. }
  93. static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
  94. {
  95. struct nvmf_connect_command *c = &req->cmd->connect;
  96. u16 qid = le16_to_cpu(c->qid);
  97. u16 sqsize = le16_to_cpu(c->sqsize);
  98. struct nvmet_ctrl *old;
  99. u16 ret;
  100. old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
  101. if (old) {
  102. pr_warn("queue already connected!\n");
  103. req->error_loc = offsetof(struct nvmf_connect_command, opcode);
  104. return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
  105. }
  106. if (!sqsize) {
  107. pr_warn("queue size zero!\n");
  108. req->error_loc = offsetof(struct nvmf_connect_command, sqsize);
  109. req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(sqsize);
  110. ret = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  111. goto err;
  112. }
  113. /* note: convert queue size from 0's-based value to 1's-based value */
  114. nvmet_cq_setup(ctrl, req->cq, qid, sqsize + 1);
  115. nvmet_sq_setup(ctrl, req->sq, qid, sqsize + 1);
  116. if (c->cattr & NVME_CONNECT_DISABLE_SQFLOW) {
  117. req->sq->sqhd_disabled = true;
  118. req->cqe->sq_head = cpu_to_le16(0xffff);
  119. }
  120. if (ctrl->ops->install_queue) {
  121. ret = ctrl->ops->install_queue(req->sq);
  122. if (ret) {
  123. pr_err("failed to install queue %d cntlid %d ret %x\n",
  124. qid, ctrl->cntlid, ret);
  125. goto err;
  126. }
  127. }
  128. return 0;
  129. err:
  130. req->sq->ctrl = NULL;
  131. return ret;
  132. }
  133. static void nvmet_execute_admin_connect(struct nvmet_req *req)
  134. {
  135. struct nvmf_connect_command *c = &req->cmd->connect;
  136. struct nvmf_connect_data *d;
  137. struct nvmet_ctrl *ctrl = NULL;
  138. u16 status = 0;
  139. if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
  140. return;
  141. d = kmalloc(sizeof(*d), GFP_KERNEL);
  142. if (!d) {
  143. status = NVME_SC_INTERNAL;
  144. goto complete;
  145. }
  146. status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
  147. if (status)
  148. goto out;
  149. /* zero out initial completion result, assign values as needed */
  150. req->cqe->result.u32 = 0;
  151. if (c->recfmt != 0) {
  152. pr_warn("invalid connect version (%d).\n",
  153. le16_to_cpu(c->recfmt));
  154. req->error_loc = offsetof(struct nvmf_connect_command, recfmt);
  155. status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
  156. goto out;
  157. }
  158. if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
  159. pr_warn("connect attempt for invalid controller ID %#x\n",
  160. d->cntlid);
  161. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  162. req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
  163. goto out;
  164. }
  165. status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
  166. le32_to_cpu(c->kato), &ctrl);
  167. if (status) {
  168. if (status == (NVME_SC_INVALID_FIELD | NVME_SC_DNR))
  169. req->error_loc =
  170. offsetof(struct nvme_common_command, opcode);
  171. goto out;
  172. }
  173. ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support;
  174. uuid_copy(&ctrl->hostid, &d->hostid);
  175. status = nvmet_install_queue(ctrl, req);
  176. if (status) {
  177. nvmet_ctrl_put(ctrl);
  178. goto out;
  179. }
  180. pr_info("creating controller %d for subsystem %s for NQN %s%s.\n",
  181. ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn,
  182. ctrl->pi_support ? " T10-PI is enabled" : "");
  183. req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid);
  184. out:
  185. kfree(d);
  186. complete:
  187. nvmet_req_complete(req, status);
  188. }
  189. static void nvmet_execute_io_connect(struct nvmet_req *req)
  190. {
  191. struct nvmf_connect_command *c = &req->cmd->connect;
  192. struct nvmf_connect_data *d;
  193. struct nvmet_ctrl *ctrl = NULL;
  194. u16 qid = le16_to_cpu(c->qid);
  195. u16 status = 0;
  196. if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
  197. return;
  198. d = kmalloc(sizeof(*d), GFP_KERNEL);
  199. if (!d) {
  200. status = NVME_SC_INTERNAL;
  201. goto complete;
  202. }
  203. status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
  204. if (status)
  205. goto out;
  206. /* zero out initial completion result, assign values as needed */
  207. req->cqe->result.u32 = 0;
  208. if (c->recfmt != 0) {
  209. pr_warn("invalid connect version (%d).\n",
  210. le16_to_cpu(c->recfmt));
  211. status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
  212. goto out;
  213. }
  214. status = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
  215. le16_to_cpu(d->cntlid),
  216. req, &ctrl);
  217. if (status)
  218. goto out;
  219. if (unlikely(qid > ctrl->subsys->max_qid)) {
  220. pr_warn("invalid queue id (%d)\n", qid);
  221. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  222. req->cqe->result.u32 = IPO_IATTR_CONNECT_SQE(qid);
  223. goto out_ctrl_put;
  224. }
  225. status = nvmet_install_queue(ctrl, req);
  226. if (status)
  227. goto out_ctrl_put;
  228. /* pass back cntlid for successful completion */
  229. req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid);
  230. pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
  231. out:
  232. kfree(d);
  233. complete:
  234. nvmet_req_complete(req, status);
  235. return;
  236. out_ctrl_put:
  237. nvmet_ctrl_put(ctrl);
  238. goto out;
  239. }
  240. u16 nvmet_parse_connect_cmd(struct nvmet_req *req)
  241. {
  242. struct nvme_command *cmd = req->cmd;
  243. if (!nvme_is_fabrics(cmd)) {
  244. pr_err("invalid command 0x%x on unconnected queue.\n",
  245. cmd->fabrics.opcode);
  246. req->error_loc = offsetof(struct nvme_common_command, opcode);
  247. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  248. }
  249. if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
  250. pr_err("invalid capsule type 0x%x on unconnected queue.\n",
  251. cmd->fabrics.fctype);
  252. req->error_loc = offsetof(struct nvmf_common_command, fctype);
  253. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  254. }
  255. if (cmd->connect.qid == 0)
  256. req->execute = nvmet_execute_admin_connect;
  257. else
  258. req->execute = nvmet_execute_io_connect;
  259. return 0;
  260. }