fabrics-cmd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * NVMe Fabrics command implementation.
  3. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  4. *
  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.
  8. *
  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
  12. * more details.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/blkdev.h>
  16. #include "nvmet.h"
  17. static void nvmet_execute_prop_set(struct nvmet_req *req)
  18. {
  19. u16 status = 0;
  20. if (!(req->cmd->prop_set.attrib & 1)) {
  21. u64 val = le64_to_cpu(req->cmd->prop_set.value);
  22. switch (le32_to_cpu(req->cmd->prop_set.offset)) {
  23. case NVME_REG_CC:
  24. nvmet_update_cc(req->sq->ctrl, val);
  25. break;
  26. default:
  27. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  28. break;
  29. }
  30. } else {
  31. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  32. }
  33. nvmet_req_complete(req, status);
  34. }
  35. static void nvmet_execute_prop_get(struct nvmet_req *req)
  36. {
  37. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  38. u16 status = 0;
  39. u64 val = 0;
  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. req->rsp->result64 = cpu_to_le64(val);
  66. nvmet_req_complete(req, status);
  67. }
  68. int nvmet_parse_fabrics_cmd(struct nvmet_req *req)
  69. {
  70. struct nvme_command *cmd = req->cmd;
  71. req->ns = NULL;
  72. switch (cmd->fabrics.fctype) {
  73. case nvme_fabrics_type_property_set:
  74. req->data_len = 0;
  75. req->execute = nvmet_execute_prop_set;
  76. break;
  77. case nvme_fabrics_type_property_get:
  78. req->data_len = 0;
  79. req->execute = nvmet_execute_prop_get;
  80. break;
  81. default:
  82. pr_err("received unknown capsule type 0x%x\n",
  83. cmd->fabrics.fctype);
  84. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  85. }
  86. return 0;
  87. }
  88. static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)
  89. {
  90. struct nvmf_connect_command *c = &req->cmd->connect;
  91. u16 qid = le16_to_cpu(c->qid);
  92. u16 sqsize = le16_to_cpu(c->sqsize);
  93. struct nvmet_ctrl *old;
  94. old = cmpxchg(&req->sq->ctrl, NULL, ctrl);
  95. if (old) {
  96. pr_warn("queue already connected!\n");
  97. return NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
  98. }
  99. nvmet_cq_setup(ctrl, req->cq, qid, sqsize);
  100. nvmet_sq_setup(ctrl, req->sq, qid, sqsize);
  101. return 0;
  102. }
  103. static void nvmet_execute_admin_connect(struct nvmet_req *req)
  104. {
  105. struct nvmf_connect_command *c = &req->cmd->connect;
  106. struct nvmf_connect_data *d;
  107. struct nvmet_ctrl *ctrl = NULL;
  108. u16 status = 0;
  109. d = kmap(sg_page(req->sg)) + req->sg->offset;
  110. /* zero out initial completion result, assign values as needed */
  111. req->rsp->result = 0;
  112. if (c->recfmt != 0) {
  113. pr_warn("invalid connect version (%d).\n",
  114. le16_to_cpu(c->recfmt));
  115. status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
  116. goto out;
  117. }
  118. if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
  119. pr_warn("connect attempt for invalid controller ID %#x\n",
  120. d->cntlid);
  121. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  122. req->rsp->result = IPO_IATTR_CONNECT_DATA(cntlid);
  123. goto out;
  124. }
  125. status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
  126. le32_to_cpu(c->kato), &ctrl);
  127. if (status)
  128. goto out;
  129. status = nvmet_install_queue(ctrl, req);
  130. if (status) {
  131. nvmet_ctrl_put(ctrl);
  132. goto out;
  133. }
  134. pr_info("creating controller %d for NQN %s.\n",
  135. ctrl->cntlid, ctrl->hostnqn);
  136. req->rsp->result16 = cpu_to_le16(ctrl->cntlid);
  137. out:
  138. kunmap(sg_page(req->sg));
  139. nvmet_req_complete(req, status);
  140. }
  141. static void nvmet_execute_io_connect(struct nvmet_req *req)
  142. {
  143. struct nvmf_connect_command *c = &req->cmd->connect;
  144. struct nvmf_connect_data *d;
  145. struct nvmet_ctrl *ctrl = NULL;
  146. u16 qid = le16_to_cpu(c->qid);
  147. u16 status = 0;
  148. d = kmap(sg_page(req->sg)) + req->sg->offset;
  149. /* zero out initial completion result, assign values as needed */
  150. req->rsp->result = 0;
  151. if (c->recfmt != 0) {
  152. pr_warn("invalid connect version (%d).\n",
  153. le16_to_cpu(c->recfmt));
  154. status = NVME_SC_CONNECT_FORMAT | NVME_SC_DNR;
  155. goto out;
  156. }
  157. status = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
  158. le16_to_cpu(d->cntlid),
  159. req, &ctrl);
  160. if (status)
  161. goto out;
  162. if (unlikely(qid > ctrl->subsys->max_qid)) {
  163. pr_warn("invalid queue id (%d)\n", qid);
  164. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  165. req->rsp->result = IPO_IATTR_CONNECT_SQE(qid);
  166. goto out_ctrl_put;
  167. }
  168. status = nvmet_install_queue(ctrl, req);
  169. if (status) {
  170. /* pass back cntlid that had the issue of installing queue */
  171. req->rsp->result16 = cpu_to_le16(ctrl->cntlid);
  172. goto out_ctrl_put;
  173. }
  174. pr_info("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
  175. out:
  176. kunmap(sg_page(req->sg));
  177. nvmet_req_complete(req, status);
  178. return;
  179. out_ctrl_put:
  180. nvmet_ctrl_put(ctrl);
  181. goto out;
  182. }
  183. int nvmet_parse_connect_cmd(struct nvmet_req *req)
  184. {
  185. struct nvme_command *cmd = req->cmd;
  186. req->ns = NULL;
  187. if (req->cmd->common.opcode != nvme_fabrics_command) {
  188. pr_err("invalid command 0x%x on unconnected queue.\n",
  189. cmd->fabrics.opcode);
  190. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  191. }
  192. if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {
  193. pr_err("invalid capsule type 0x%x on unconnected queue.\n",
  194. cmd->fabrics.fctype);
  195. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  196. }
  197. req->data_len = sizeof(struct nvmf_connect_data);
  198. if (cmd->connect.qid == 0)
  199. req->execute = nvmet_execute_admin_connect;
  200. else
  201. req->execute = nvmet_execute_io_connect;
  202. return 0;
  203. }