admin-cmd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * NVMe admin 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/module.h>
  16. #include <generated/utsrelease.h>
  17. #include "nvmet.h"
  18. u32 nvmet_get_log_page_len(struct nvme_command *cmd)
  19. {
  20. u32 len = le16_to_cpu(cmd->get_log_page.numdu);
  21. len <<= 16;
  22. len += le16_to_cpu(cmd->get_log_page.numdl);
  23. /* NUMD is a 0's based value */
  24. len += 1;
  25. len *= sizeof(u32);
  26. return len;
  27. }
  28. static void nvmet_execute_get_log_page(struct nvmet_req *req)
  29. {
  30. size_t data_len = nvmet_get_log_page_len(req->cmd);
  31. void *buf;
  32. u16 status = 0;
  33. buf = kzalloc(data_len, GFP_KERNEL);
  34. if (!buf) {
  35. status = NVME_SC_INTERNAL;
  36. goto out;
  37. }
  38. switch (req->cmd->get_log_page.lid) {
  39. case 0x01:
  40. /*
  41. * We currently never set the More bit in the status field,
  42. * so all error log entries are invalid and can be zeroed out.
  43. * This is called a minum viable implementation (TM) of this
  44. * mandatory log page.
  45. */
  46. break;
  47. case 0x02:
  48. /*
  49. * XXX: fill out actual smart log
  50. *
  51. * We might have a hard time coming up with useful values for
  52. * many of the fields, and even when we have useful data
  53. * available (e.g. units or commands read/written) those aren't
  54. * persistent over power loss.
  55. */
  56. break;
  57. case 0x03:
  58. /*
  59. * We only support a single firmware slot which always is
  60. * active, so we can zero out the whole firmware slot log and
  61. * still claim to fully implement this mandatory log page.
  62. */
  63. break;
  64. default:
  65. BUG();
  66. }
  67. status = nvmet_copy_to_sgl(req, 0, buf, data_len);
  68. kfree(buf);
  69. out:
  70. nvmet_req_complete(req, status);
  71. }
  72. static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
  73. {
  74. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  75. struct nvme_id_ctrl *id;
  76. u16 status = 0;
  77. id = kzalloc(sizeof(*id), GFP_KERNEL);
  78. if (!id) {
  79. status = NVME_SC_INTERNAL;
  80. goto out;
  81. }
  82. /* XXX: figure out how to assign real vendors IDs. */
  83. id->vid = 0;
  84. id->ssvid = 0;
  85. memset(id->sn, ' ', sizeof(id->sn));
  86. snprintf(id->sn, sizeof(id->sn), "%llx", ctrl->serial);
  87. memset(id->mn, ' ', sizeof(id->mn));
  88. strncpy((char *)id->mn, "Linux", sizeof(id->mn));
  89. memset(id->fr, ' ', sizeof(id->fr));
  90. strncpy((char *)id->fr, UTS_RELEASE, sizeof(id->fr));
  91. id->rab = 6;
  92. /*
  93. * XXX: figure out how we can assign a IEEE OUI, but until then
  94. * the safest is to leave it as zeroes.
  95. */
  96. /* we support multiple ports and multiples hosts: */
  97. id->mic = (1 << 0) | (1 << 1);
  98. /* no limit on data transfer sizes for now */
  99. id->mdts = 0;
  100. id->cntlid = cpu_to_le16(ctrl->cntlid);
  101. id->ver = cpu_to_le32(ctrl->subsys->ver);
  102. /* XXX: figure out what to do about RTD3R/RTD3 */
  103. id->oaes = cpu_to_le32(1 << 8);
  104. id->ctratt = cpu_to_le32(1 << 0);
  105. id->oacs = 0;
  106. /*
  107. * We don't really have a practical limit on the number of abort
  108. * comands. But we don't do anything useful for abort either, so
  109. * no point in allowing more abort commands than the spec requires.
  110. */
  111. id->acl = 3;
  112. id->aerl = NVMET_ASYNC_EVENTS - 1;
  113. /* first slot is read-only, only one slot supported */
  114. id->frmw = (1 << 0) | (1 << 1);
  115. id->lpa = (1 << 0) | (1 << 2);
  116. id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
  117. id->npss = 0;
  118. /* We support keep-alive timeout in granularity of seconds */
  119. id->kas = cpu_to_le16(NVMET_KAS);
  120. id->sqes = (0x6 << 4) | 0x6;
  121. id->cqes = (0x4 << 4) | 0x4;
  122. /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
  123. id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
  124. id->nn = cpu_to_le32(ctrl->subsys->max_nsid);
  125. id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM);
  126. /* XXX: don't report vwc if the underlying device is write through */
  127. id->vwc = NVME_CTRL_VWC_PRESENT;
  128. /*
  129. * We can't support atomic writes bigger than a LBA without support
  130. * from the backend device.
  131. */
  132. id->awun = 0;
  133. id->awupf = 0;
  134. id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
  135. if (ctrl->ops->has_keyed_sgls)
  136. id->sgls |= cpu_to_le32(1 << 2);
  137. if (ctrl->ops->sqe_inline_size)
  138. id->sgls |= cpu_to_le32(1 << 20);
  139. strcpy(id->subnqn, ctrl->subsys->subsysnqn);
  140. /* Max command capsule size is sqe + single page of in-capsule data */
  141. id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
  142. ctrl->ops->sqe_inline_size) / 16);
  143. /* Max response capsule size is cqe */
  144. id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
  145. id->msdbd = ctrl->ops->msdbd;
  146. /*
  147. * Meh, we don't really support any power state. Fake up the same
  148. * values that qemu does.
  149. */
  150. id->psd[0].max_power = cpu_to_le16(0x9c4);
  151. id->psd[0].entry_lat = cpu_to_le32(0x10);
  152. id->psd[0].exit_lat = cpu_to_le32(0x4);
  153. status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
  154. kfree(id);
  155. out:
  156. nvmet_req_complete(req, status);
  157. }
  158. static void nvmet_execute_identify_ns(struct nvmet_req *req)
  159. {
  160. struct nvmet_ns *ns;
  161. struct nvme_id_ns *id;
  162. u16 status = 0;
  163. ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
  164. if (!ns) {
  165. status = NVME_SC_INVALID_NS | NVME_SC_DNR;
  166. goto out;
  167. }
  168. id = kzalloc(sizeof(*id), GFP_KERNEL);
  169. if (!id) {
  170. status = NVME_SC_INTERNAL;
  171. goto out_put_ns;
  172. }
  173. /*
  174. * nuse = ncap = nsze isn't aways true, but we have no way to find
  175. * that out from the underlying device.
  176. */
  177. id->ncap = id->nuse = id->nsze =
  178. cpu_to_le64(ns->size >> ns->blksize_shift);
  179. /*
  180. * We just provide a single LBA format that matches what the
  181. * underlying device reports.
  182. */
  183. id->nlbaf = 0;
  184. id->flbas = 0;
  185. /*
  186. * Our namespace might always be shared. Not just with other
  187. * controllers, but also with any other user of the block device.
  188. */
  189. id->nmic = (1 << 0);
  190. memcpy(&id->nguid, &ns->nguid, sizeof(uuid_le));
  191. id->lbaf[0].ds = ns->blksize_shift;
  192. status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
  193. kfree(id);
  194. out_put_ns:
  195. nvmet_put_namespace(ns);
  196. out:
  197. nvmet_req_complete(req, status);
  198. }
  199. static void nvmet_execute_identify_nslist(struct nvmet_req *req)
  200. {
  201. static const int buf_size = 4096;
  202. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  203. struct nvmet_ns *ns;
  204. u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
  205. __le32 *list;
  206. u16 status = 0;
  207. int i = 0;
  208. list = kzalloc(buf_size, GFP_KERNEL);
  209. if (!list) {
  210. status = NVME_SC_INTERNAL;
  211. goto out;
  212. }
  213. rcu_read_lock();
  214. list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
  215. if (ns->nsid <= min_nsid)
  216. continue;
  217. list[i++] = cpu_to_le32(ns->nsid);
  218. if (i == buf_size / sizeof(__le32))
  219. break;
  220. }
  221. rcu_read_unlock();
  222. status = nvmet_copy_to_sgl(req, 0, list, buf_size);
  223. kfree(list);
  224. out:
  225. nvmet_req_complete(req, status);
  226. }
  227. /*
  228. * A "mimimum viable" abort implementation: the command is mandatory in the
  229. * spec, but we are not required to do any useful work. We couldn't really
  230. * do a useful abort, so don't bother even with waiting for the command
  231. * to be exectuted and return immediately telling the command to abort
  232. * wasn't found.
  233. */
  234. static void nvmet_execute_abort(struct nvmet_req *req)
  235. {
  236. nvmet_set_result(req, 1);
  237. nvmet_req_complete(req, 0);
  238. }
  239. static void nvmet_execute_set_features(struct nvmet_req *req)
  240. {
  241. struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
  242. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
  243. u64 val;
  244. u32 val32;
  245. u16 status = 0;
  246. switch (cdw10 & 0xf) {
  247. case NVME_FEAT_NUM_QUEUES:
  248. nvmet_set_result(req,
  249. (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
  250. break;
  251. case NVME_FEAT_KATO:
  252. val = le64_to_cpu(req->cmd->prop_set.value);
  253. val32 = val & 0xffff;
  254. req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
  255. nvmet_set_result(req, req->sq->ctrl->kato);
  256. break;
  257. default:
  258. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  259. break;
  260. }
  261. nvmet_req_complete(req, status);
  262. }
  263. static void nvmet_execute_get_features(struct nvmet_req *req)
  264. {
  265. struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
  266. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
  267. u16 status = 0;
  268. switch (cdw10 & 0xf) {
  269. /*
  270. * These features are mandatory in the spec, but we don't
  271. * have a useful way to implement them. We'll eventually
  272. * need to come up with some fake values for these.
  273. */
  274. #if 0
  275. case NVME_FEAT_ARBITRATION:
  276. break;
  277. case NVME_FEAT_POWER_MGMT:
  278. break;
  279. case NVME_FEAT_TEMP_THRESH:
  280. break;
  281. case NVME_FEAT_ERR_RECOVERY:
  282. break;
  283. case NVME_FEAT_IRQ_COALESCE:
  284. break;
  285. case NVME_FEAT_IRQ_CONFIG:
  286. break;
  287. case NVME_FEAT_WRITE_ATOMIC:
  288. break;
  289. case NVME_FEAT_ASYNC_EVENT:
  290. break;
  291. #endif
  292. case NVME_FEAT_VOLATILE_WC:
  293. nvmet_set_result(req, 1);
  294. break;
  295. case NVME_FEAT_NUM_QUEUES:
  296. nvmet_set_result(req,
  297. (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
  298. break;
  299. case NVME_FEAT_KATO:
  300. nvmet_set_result(req, req->sq->ctrl->kato * 1000);
  301. break;
  302. default:
  303. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  304. break;
  305. }
  306. nvmet_req_complete(req, status);
  307. }
  308. static void nvmet_execute_async_event(struct nvmet_req *req)
  309. {
  310. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  311. mutex_lock(&ctrl->lock);
  312. if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
  313. mutex_unlock(&ctrl->lock);
  314. nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR);
  315. return;
  316. }
  317. ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
  318. mutex_unlock(&ctrl->lock);
  319. schedule_work(&ctrl->async_event_work);
  320. }
  321. static void nvmet_execute_keep_alive(struct nvmet_req *req)
  322. {
  323. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  324. pr_debug("ctrl %d update keep-alive timer for %d secs\n",
  325. ctrl->cntlid, ctrl->kato);
  326. mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
  327. nvmet_req_complete(req, 0);
  328. }
  329. int nvmet_parse_admin_cmd(struct nvmet_req *req)
  330. {
  331. struct nvme_command *cmd = req->cmd;
  332. req->ns = NULL;
  333. if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
  334. pr_err("nvmet: got admin cmd %d while CC.EN == 0\n",
  335. cmd->common.opcode);
  336. return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  337. }
  338. if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
  339. pr_err("nvmet: got admin cmd %d while CSTS.RDY == 0\n",
  340. cmd->common.opcode);
  341. return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  342. }
  343. switch (cmd->common.opcode) {
  344. case nvme_admin_get_log_page:
  345. req->data_len = nvmet_get_log_page_len(cmd);
  346. switch (cmd->get_log_page.lid) {
  347. case 0x01:
  348. case 0x02:
  349. case 0x03:
  350. req->execute = nvmet_execute_get_log_page;
  351. return 0;
  352. }
  353. break;
  354. case nvme_admin_identify:
  355. req->data_len = 4096;
  356. switch (le32_to_cpu(cmd->identify.cns)) {
  357. case 0x00:
  358. req->execute = nvmet_execute_identify_ns;
  359. return 0;
  360. case 0x01:
  361. req->execute = nvmet_execute_identify_ctrl;
  362. return 0;
  363. case 0x02:
  364. req->execute = nvmet_execute_identify_nslist;
  365. return 0;
  366. }
  367. break;
  368. case nvme_admin_abort_cmd:
  369. req->execute = nvmet_execute_abort;
  370. req->data_len = 0;
  371. return 0;
  372. case nvme_admin_set_features:
  373. req->execute = nvmet_execute_set_features;
  374. req->data_len = 0;
  375. return 0;
  376. case nvme_admin_get_features:
  377. req->execute = nvmet_execute_get_features;
  378. req->data_len = 0;
  379. return 0;
  380. case nvme_admin_async_event:
  381. req->execute = nvmet_execute_async_event;
  382. req->data_len = 0;
  383. return 0;
  384. case nvme_admin_keep_alive:
  385. req->execute = nvmet_execute_keep_alive;
  386. req->data_len = 0;
  387. return 0;
  388. }
  389. pr_err("nvmet: unhandled cmd %d\n", cmd->common.opcode);
  390. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  391. }