discovery.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Discovery service for the NVMe over Fabrics target.
  3. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version
  7. * 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/slab.h>
  16. #include <generated/utsrelease.h>
  17. #include "nvmet.h"
  18. struct nvmet_subsys *nvmet_disc_subsys;
  19. u64 nvmet_genctr;
  20. void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port)
  21. {
  22. down_write(&nvmet_config_sem);
  23. if (list_empty(&port->entry)) {
  24. list_add_tail(&port->entry, &parent->referrals);
  25. port->enabled = true;
  26. nvmet_genctr++;
  27. }
  28. up_write(&nvmet_config_sem);
  29. }
  30. void nvmet_referral_disable(struct nvmet_port *port)
  31. {
  32. down_write(&nvmet_config_sem);
  33. if (!list_empty(&port->entry)) {
  34. port->enabled = false;
  35. list_del_init(&port->entry);
  36. nvmet_genctr++;
  37. }
  38. up_write(&nvmet_config_sem);
  39. }
  40. static void nvmet_format_discovery_entry(struct nvmf_disc_rsp_page_hdr *hdr,
  41. struct nvmet_port *port, char *subsys_nqn, u8 type, u32 numrec)
  42. {
  43. struct nvmf_disc_rsp_page_entry *e = &hdr->entries[numrec];
  44. e->trtype = port->disc_addr.trtype;
  45. e->adrfam = port->disc_addr.adrfam;
  46. e->treq = port->disc_addr.treq;
  47. e->portid = port->disc_addr.portid;
  48. /* we support only dynamic controllers */
  49. e->cntlid = cpu_to_le16(NVME_CNTLID_DYNAMIC);
  50. e->asqsz = cpu_to_le16(NVMF_AQ_DEPTH);
  51. e->subtype = type;
  52. memcpy(e->trsvcid, port->disc_addr.trsvcid, NVMF_TRSVCID_SIZE);
  53. memcpy(e->traddr, port->disc_addr.traddr, NVMF_TRADDR_SIZE);
  54. memcpy(e->tsas.common, port->disc_addr.tsas.common, NVMF_TSAS_SIZE);
  55. memcpy(e->subnqn, subsys_nqn, NVMF_NQN_SIZE);
  56. }
  57. static void nvmet_execute_get_disc_log_page(struct nvmet_req *req)
  58. {
  59. const int entry_size = sizeof(struct nvmf_disc_rsp_page_entry);
  60. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  61. struct nvmf_disc_rsp_page_hdr *hdr;
  62. size_t data_len = nvmet_get_log_page_len(req->cmd);
  63. size_t alloc_len = max(data_len, sizeof(*hdr));
  64. int residual_len = data_len - sizeof(*hdr);
  65. struct nvmet_subsys_link *p;
  66. struct nvmet_port *r;
  67. u32 numrec = 0;
  68. u16 status = 0;
  69. /*
  70. * Make sure we're passing at least a buffer of response header size.
  71. * If host provided data len is less than the header size, only the
  72. * number of bytes requested by host will be sent to host.
  73. */
  74. hdr = kzalloc(alloc_len, GFP_KERNEL);
  75. if (!hdr) {
  76. status = NVME_SC_INTERNAL;
  77. goto out;
  78. }
  79. down_read(&nvmet_config_sem);
  80. list_for_each_entry(p, &req->port->subsystems, entry) {
  81. if (!nvmet_host_allowed(req, p->subsys, ctrl->hostnqn))
  82. continue;
  83. if (residual_len >= entry_size) {
  84. nvmet_format_discovery_entry(hdr, req->port,
  85. p->subsys->subsysnqn,
  86. NVME_NQN_NVME, numrec);
  87. residual_len -= entry_size;
  88. }
  89. numrec++;
  90. }
  91. list_for_each_entry(r, &req->port->referrals, entry) {
  92. if (residual_len >= entry_size) {
  93. nvmet_format_discovery_entry(hdr, r,
  94. NVME_DISC_SUBSYS_NAME,
  95. NVME_NQN_DISC, numrec);
  96. residual_len -= entry_size;
  97. }
  98. numrec++;
  99. }
  100. hdr->genctr = cpu_to_le64(nvmet_genctr);
  101. hdr->numrec = cpu_to_le64(numrec);
  102. hdr->recfmt = cpu_to_le16(0);
  103. up_read(&nvmet_config_sem);
  104. status = nvmet_copy_to_sgl(req, 0, hdr, data_len);
  105. kfree(hdr);
  106. out:
  107. nvmet_req_complete(req, status);
  108. }
  109. static void nvmet_execute_identify_disc_ctrl(struct nvmet_req *req)
  110. {
  111. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  112. struct nvme_id_ctrl *id;
  113. u16 status = 0;
  114. id = kzalloc(sizeof(*id), GFP_KERNEL);
  115. if (!id) {
  116. status = NVME_SC_INTERNAL;
  117. goto out;
  118. }
  119. memset(id->fr, ' ', sizeof(id->fr));
  120. strncpy((char *)id->fr, UTS_RELEASE, sizeof(id->fr));
  121. /* no limit on data transfer sizes for now */
  122. id->mdts = 0;
  123. id->cntlid = cpu_to_le16(ctrl->cntlid);
  124. id->ver = cpu_to_le32(ctrl->subsys->ver);
  125. id->lpa = (1 << 2);
  126. /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
  127. id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
  128. id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
  129. if (ctrl->ops->has_keyed_sgls)
  130. id->sgls |= cpu_to_le32(1 << 2);
  131. if (ctrl->ops->sqe_inline_size)
  132. id->sgls |= cpu_to_le32(1 << 20);
  133. strcpy(id->subnqn, ctrl->subsys->subsysnqn);
  134. status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
  135. kfree(id);
  136. out:
  137. nvmet_req_complete(req, status);
  138. }
  139. int nvmet_parse_discovery_cmd(struct nvmet_req *req)
  140. {
  141. struct nvme_command *cmd = req->cmd;
  142. req->ns = NULL;
  143. if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
  144. pr_err("nvmet: got cmd %d while not ready\n",
  145. cmd->common.opcode);
  146. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  147. }
  148. switch (cmd->common.opcode) {
  149. case nvme_admin_get_log_page:
  150. req->data_len = nvmet_get_log_page_len(cmd);
  151. switch (cmd->get_log_page.lid) {
  152. case NVME_LOG_DISC:
  153. req->execute = nvmet_execute_get_disc_log_page;
  154. return 0;
  155. default:
  156. pr_err("nvmet: unsupported get_log_page lid %d\n",
  157. cmd->get_log_page.lid);
  158. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  159. }
  160. case nvme_admin_identify:
  161. req->data_len = 4096;
  162. switch (le32_to_cpu(cmd->identify.cns)) {
  163. case NVME_ID_CNS_CTRL:
  164. req->execute =
  165. nvmet_execute_identify_disc_ctrl;
  166. return 0;
  167. default:
  168. pr_err("nvmet: unsupported identify cns %d\n",
  169. le32_to_cpu(cmd->identify.cns));
  170. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  171. }
  172. default:
  173. pr_err("nvmet: unsupported cmd %d\n",
  174. cmd->common.opcode);
  175. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  176. }
  177. pr_err("nvmet: unhandled cmd %d\n", cmd->common.opcode);
  178. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  179. }
  180. int __init nvmet_init_discovery(void)
  181. {
  182. nvmet_disc_subsys =
  183. nvmet_subsys_alloc(NVME_DISC_SUBSYS_NAME, NVME_NQN_DISC);
  184. if (!nvmet_disc_subsys)
  185. return -ENOMEM;
  186. return 0;
  187. }
  188. void nvmet_exit_discovery(void)
  189. {
  190. nvmet_subsys_put(nvmet_disc_subsys);
  191. }