discovery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Discovery service for the NVMe over Fabrics target.
  4. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/slab.h>
  8. #include <generated/utsrelease.h>
  9. #include "nvmet.h"
  10. struct nvmet_subsys *nvmet_disc_subsys;
  11. static u64 nvmet_genctr;
  12. static void __nvmet_disc_changed(struct nvmet_port *port,
  13. struct nvmet_ctrl *ctrl)
  14. {
  15. if (ctrl->port != port)
  16. return;
  17. if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_DISC_CHANGE))
  18. return;
  19. nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
  20. NVME_AER_NOTICE_DISC_CHANGED, NVME_LOG_DISC);
  21. }
  22. void nvmet_port_disc_changed(struct nvmet_port *port,
  23. struct nvmet_subsys *subsys)
  24. {
  25. struct nvmet_ctrl *ctrl;
  26. lockdep_assert_held(&nvmet_config_sem);
  27. nvmet_genctr++;
  28. mutex_lock(&nvmet_disc_subsys->lock);
  29. list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) {
  30. if (subsys && !nvmet_host_allowed(subsys, ctrl->hostnqn))
  31. continue;
  32. __nvmet_disc_changed(port, ctrl);
  33. }
  34. mutex_unlock(&nvmet_disc_subsys->lock);
  35. /* If transport can signal change, notify transport */
  36. if (port->tr_ops && port->tr_ops->discovery_chg)
  37. port->tr_ops->discovery_chg(port);
  38. }
  39. static void __nvmet_subsys_disc_changed(struct nvmet_port *port,
  40. struct nvmet_subsys *subsys,
  41. struct nvmet_host *host)
  42. {
  43. struct nvmet_ctrl *ctrl;
  44. mutex_lock(&nvmet_disc_subsys->lock);
  45. list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) {
  46. if (host && strcmp(nvmet_host_name(host), ctrl->hostnqn))
  47. continue;
  48. __nvmet_disc_changed(port, ctrl);
  49. }
  50. mutex_unlock(&nvmet_disc_subsys->lock);
  51. }
  52. void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
  53. struct nvmet_host *host)
  54. {
  55. struct nvmet_port *port;
  56. struct nvmet_subsys_link *s;
  57. lockdep_assert_held(&nvmet_config_sem);
  58. nvmet_genctr++;
  59. list_for_each_entry(port, nvmet_ports, global_entry)
  60. list_for_each_entry(s, &port->subsystems, entry) {
  61. if (s->subsys != subsys)
  62. continue;
  63. __nvmet_subsys_disc_changed(port, subsys, host);
  64. }
  65. }
  66. void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port)
  67. {
  68. down_write(&nvmet_config_sem);
  69. if (list_empty(&port->entry)) {
  70. list_add_tail(&port->entry, &parent->referrals);
  71. port->enabled = true;
  72. nvmet_port_disc_changed(parent, NULL);
  73. }
  74. up_write(&nvmet_config_sem);
  75. }
  76. void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port)
  77. {
  78. down_write(&nvmet_config_sem);
  79. if (!list_empty(&port->entry)) {
  80. port->enabled = false;
  81. list_del_init(&port->entry);
  82. nvmet_port_disc_changed(parent, NULL);
  83. }
  84. up_write(&nvmet_config_sem);
  85. }
  86. static void nvmet_format_discovery_entry(struct nvmf_disc_rsp_page_hdr *hdr,
  87. struct nvmet_port *port, char *subsys_nqn, char *traddr,
  88. u8 type, u32 numrec)
  89. {
  90. struct nvmf_disc_rsp_page_entry *e = &hdr->entries[numrec];
  91. e->trtype = port->disc_addr.trtype;
  92. e->adrfam = port->disc_addr.adrfam;
  93. e->treq = port->disc_addr.treq;
  94. e->portid = port->disc_addr.portid;
  95. /* we support only dynamic controllers */
  96. e->cntlid = cpu_to_le16(NVME_CNTLID_DYNAMIC);
  97. e->asqsz = cpu_to_le16(NVME_AQ_DEPTH);
  98. e->subtype = type;
  99. memcpy(e->trsvcid, port->disc_addr.trsvcid, NVMF_TRSVCID_SIZE);
  100. memcpy(e->traddr, traddr, NVMF_TRADDR_SIZE);
  101. memcpy(e->tsas.common, port->disc_addr.tsas.common, NVMF_TSAS_SIZE);
  102. strncpy(e->subnqn, subsys_nqn, NVMF_NQN_SIZE);
  103. }
  104. /*
  105. * nvmet_set_disc_traddr - set a correct discovery log entry traddr
  106. *
  107. * IP based transports (e.g RDMA) can listen on "any" ipv4/ipv6 addresses
  108. * (INADDR_ANY or IN6ADDR_ANY_INIT). The discovery log page traddr reply
  109. * must not contain that "any" IP address. If the transport implements
  110. * .disc_traddr, use it. this callback will set the discovery traddr
  111. * from the req->port address in case the port in question listens
  112. * "any" IP address.
  113. */
  114. static void nvmet_set_disc_traddr(struct nvmet_req *req, struct nvmet_port *port,
  115. char *traddr)
  116. {
  117. if (req->ops->disc_traddr)
  118. req->ops->disc_traddr(req, port, traddr);
  119. else
  120. memcpy(traddr, port->disc_addr.traddr, NVMF_TRADDR_SIZE);
  121. }
  122. static size_t discovery_log_entries(struct nvmet_req *req)
  123. {
  124. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  125. struct nvmet_subsys_link *p;
  126. struct nvmet_port *r;
  127. size_t entries = 1;
  128. list_for_each_entry(p, &req->port->subsystems, entry) {
  129. if (!nvmet_host_allowed(p->subsys, ctrl->hostnqn))
  130. continue;
  131. entries++;
  132. }
  133. list_for_each_entry(r, &req->port->referrals, entry)
  134. entries++;
  135. return entries;
  136. }
  137. static void nvmet_execute_disc_get_log_page(struct nvmet_req *req)
  138. {
  139. const int entry_size = sizeof(struct nvmf_disc_rsp_page_entry);
  140. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  141. struct nvmf_disc_rsp_page_hdr *hdr;
  142. u64 offset = nvmet_get_log_page_offset(req->cmd);
  143. size_t data_len = nvmet_get_log_page_len(req->cmd);
  144. size_t alloc_len;
  145. struct nvmet_subsys_link *p;
  146. struct nvmet_port *r;
  147. u32 numrec = 0;
  148. u16 status = 0;
  149. void *buffer;
  150. char traddr[NVMF_TRADDR_SIZE];
  151. if (!nvmet_check_transfer_len(req, data_len))
  152. return;
  153. if (req->cmd->get_log_page.lid != NVME_LOG_DISC) {
  154. req->error_loc =
  155. offsetof(struct nvme_get_log_page_command, lid);
  156. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  157. goto out;
  158. }
  159. /* Spec requires dword aligned offsets */
  160. if (offset & 0x3) {
  161. req->error_loc =
  162. offsetof(struct nvme_get_log_page_command, lpo);
  163. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  164. goto out;
  165. }
  166. /*
  167. * Make sure we're passing at least a buffer of response header size.
  168. * If host provided data len is less than the header size, only the
  169. * number of bytes requested by host will be sent to host.
  170. */
  171. down_read(&nvmet_config_sem);
  172. alloc_len = sizeof(*hdr) + entry_size * discovery_log_entries(req);
  173. buffer = kzalloc(alloc_len, GFP_KERNEL);
  174. if (!buffer) {
  175. up_read(&nvmet_config_sem);
  176. status = NVME_SC_INTERNAL;
  177. goto out;
  178. }
  179. hdr = buffer;
  180. nvmet_set_disc_traddr(req, req->port, traddr);
  181. nvmet_format_discovery_entry(hdr, req->port,
  182. nvmet_disc_subsys->subsysnqn,
  183. traddr, NVME_NQN_CURR, numrec);
  184. numrec++;
  185. list_for_each_entry(p, &req->port->subsystems, entry) {
  186. if (!nvmet_host_allowed(p->subsys, ctrl->hostnqn))
  187. continue;
  188. nvmet_format_discovery_entry(hdr, req->port,
  189. p->subsys->subsysnqn, traddr,
  190. NVME_NQN_NVME, numrec);
  191. numrec++;
  192. }
  193. list_for_each_entry(r, &req->port->referrals, entry) {
  194. nvmet_format_discovery_entry(hdr, r,
  195. NVME_DISC_SUBSYS_NAME,
  196. r->disc_addr.traddr,
  197. NVME_NQN_DISC, numrec);
  198. numrec++;
  199. }
  200. hdr->genctr = cpu_to_le64(nvmet_genctr);
  201. hdr->numrec = cpu_to_le64(numrec);
  202. hdr->recfmt = cpu_to_le16(0);
  203. nvmet_clear_aen_bit(req, NVME_AEN_BIT_DISC_CHANGE);
  204. up_read(&nvmet_config_sem);
  205. status = nvmet_copy_to_sgl(req, 0, buffer + offset, data_len);
  206. kfree(buffer);
  207. out:
  208. nvmet_req_complete(req, status);
  209. }
  210. static void nvmet_execute_disc_identify(struct nvmet_req *req)
  211. {
  212. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  213. struct nvme_id_ctrl *id;
  214. u16 status = 0;
  215. if (!nvmet_check_transfer_len(req, NVME_IDENTIFY_DATA_SIZE))
  216. return;
  217. if (req->cmd->identify.cns != NVME_ID_CNS_CTRL) {
  218. req->error_loc = offsetof(struct nvme_identify, cns);
  219. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  220. goto out;
  221. }
  222. id = kzalloc(sizeof(*id), GFP_KERNEL);
  223. if (!id) {
  224. status = NVME_SC_INTERNAL;
  225. goto out;
  226. }
  227. memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE);
  228. memset(id->fr, ' ', sizeof(id->fr));
  229. memcpy_and_pad(id->mn, sizeof(id->mn), ctrl->subsys->model_number,
  230. strlen(ctrl->subsys->model_number), ' ');
  231. memcpy_and_pad(id->fr, sizeof(id->fr),
  232. UTS_RELEASE, strlen(UTS_RELEASE), ' ');
  233. id->cntrltype = NVME_CTRL_DISC;
  234. /* no limit on data transfer sizes for now */
  235. id->mdts = 0;
  236. id->cntlid = cpu_to_le16(ctrl->cntlid);
  237. id->ver = cpu_to_le32(ctrl->subsys->ver);
  238. id->lpa = (1 << 2);
  239. /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
  240. id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
  241. id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
  242. if (ctrl->ops->flags & NVMF_KEYED_SGLS)
  243. id->sgls |= cpu_to_le32(1 << 2);
  244. if (req->port->inline_data_size)
  245. id->sgls |= cpu_to_le32(1 << 20);
  246. id->oaes = cpu_to_le32(NVMET_DISC_AEN_CFG_OPTIONAL);
  247. strscpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn));
  248. status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
  249. kfree(id);
  250. out:
  251. nvmet_req_complete(req, status);
  252. }
  253. static void nvmet_execute_disc_set_features(struct nvmet_req *req)
  254. {
  255. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
  256. u16 stat;
  257. if (!nvmet_check_transfer_len(req, 0))
  258. return;
  259. switch (cdw10 & 0xff) {
  260. case NVME_FEAT_KATO:
  261. stat = nvmet_set_feat_kato(req);
  262. break;
  263. case NVME_FEAT_ASYNC_EVENT:
  264. stat = nvmet_set_feat_async_event(req,
  265. NVMET_DISC_AEN_CFG_OPTIONAL);
  266. break;
  267. default:
  268. req->error_loc =
  269. offsetof(struct nvme_common_command, cdw10);
  270. stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  271. break;
  272. }
  273. nvmet_req_complete(req, stat);
  274. }
  275. static void nvmet_execute_disc_get_features(struct nvmet_req *req)
  276. {
  277. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
  278. u16 stat = 0;
  279. if (!nvmet_check_transfer_len(req, 0))
  280. return;
  281. switch (cdw10 & 0xff) {
  282. case NVME_FEAT_KATO:
  283. nvmet_get_feat_kato(req);
  284. break;
  285. case NVME_FEAT_ASYNC_EVENT:
  286. nvmet_get_feat_async_event(req);
  287. break;
  288. default:
  289. req->error_loc =
  290. offsetof(struct nvme_common_command, cdw10);
  291. stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  292. break;
  293. }
  294. nvmet_req_complete(req, stat);
  295. }
  296. u16 nvmet_parse_discovery_cmd(struct nvmet_req *req)
  297. {
  298. struct nvme_command *cmd = req->cmd;
  299. if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
  300. pr_err("got cmd %d while not ready\n",
  301. cmd->common.opcode);
  302. req->error_loc =
  303. offsetof(struct nvme_common_command, opcode);
  304. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  305. }
  306. switch (cmd->common.opcode) {
  307. case nvme_admin_set_features:
  308. req->execute = nvmet_execute_disc_set_features;
  309. return 0;
  310. case nvme_admin_get_features:
  311. req->execute = nvmet_execute_disc_get_features;
  312. return 0;
  313. case nvme_admin_async_event:
  314. req->execute = nvmet_execute_async_event;
  315. return 0;
  316. case nvme_admin_keep_alive:
  317. req->execute = nvmet_execute_keep_alive;
  318. return 0;
  319. case nvme_admin_get_log_page:
  320. req->execute = nvmet_execute_disc_get_log_page;
  321. return 0;
  322. case nvme_admin_identify:
  323. req->execute = nvmet_execute_disc_identify;
  324. return 0;
  325. default:
  326. pr_debug("unhandled cmd %d\n", cmd->common.opcode);
  327. req->error_loc = offsetof(struct nvme_common_command, opcode);
  328. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  329. }
  330. }
  331. int __init nvmet_init_discovery(void)
  332. {
  333. nvmet_disc_subsys =
  334. nvmet_subsys_alloc(NVME_DISC_SUBSYS_NAME, NVME_NQN_CURR);
  335. return PTR_ERR_OR_ZERO(nvmet_disc_subsys);
  336. }
  337. void nvmet_exit_discovery(void)
  338. {
  339. nvmet_subsys_put(nvmet_disc_subsys);
  340. }