admin-cmd.c 14 KB

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