admin-cmd.c 15 KB

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