admin-cmd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 == cpu_to_le32(NVME_NSID_ALL))
  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 NVME_LOG_ERROR:
  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 NVME_LOG_SMART:
  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 NVME_LOG_FW_SLOT:
  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. const char model[] = "Linux";
  155. id = kzalloc(sizeof(*id), GFP_KERNEL);
  156. if (!id) {
  157. status = NVME_SC_INTERNAL;
  158. goto out;
  159. }
  160. /* XXX: figure out how to assign real vendors IDs. */
  161. id->vid = 0;
  162. id->ssvid = 0;
  163. bin2hex(id->sn, &ctrl->subsys->serial,
  164. min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
  165. memcpy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1, ' ');
  166. memcpy_and_pad(id->fr, sizeof(id->fr),
  167. UTS_RELEASE, strlen(UTS_RELEASE), ' ');
  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 = NVME_IDENTIFY_DATA_SIZE;
  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. static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len,
  306. void *id, off_t *off)
  307. {
  308. struct nvme_ns_id_desc desc = {
  309. .nidt = type,
  310. .nidl = len,
  311. };
  312. u16 status;
  313. status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc));
  314. if (status)
  315. return status;
  316. *off += sizeof(desc);
  317. status = nvmet_copy_to_sgl(req, *off, id, len);
  318. if (status)
  319. return status;
  320. *off += len;
  321. return 0;
  322. }
  323. static void nvmet_execute_identify_desclist(struct nvmet_req *req)
  324. {
  325. struct nvmet_ns *ns;
  326. u16 status = 0;
  327. off_t off = 0;
  328. ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
  329. if (!ns) {
  330. status = NVME_SC_INVALID_NS | NVME_SC_DNR;
  331. goto out;
  332. }
  333. if (memchr_inv(&ns->uuid, 0, sizeof(ns->uuid))) {
  334. status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
  335. NVME_NIDT_UUID_LEN,
  336. &ns->uuid, &off);
  337. if (status)
  338. goto out_put_ns;
  339. }
  340. if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid))) {
  341. status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
  342. NVME_NIDT_NGUID_LEN,
  343. &ns->nguid, &off);
  344. if (status)
  345. goto out_put_ns;
  346. }
  347. if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off,
  348. off) != NVME_IDENTIFY_DATA_SIZE - off)
  349. status = NVME_SC_INTERNAL | NVME_SC_DNR;
  350. out_put_ns:
  351. nvmet_put_namespace(ns);
  352. out:
  353. nvmet_req_complete(req, status);
  354. }
  355. /*
  356. * A "mimimum viable" abort implementation: the command is mandatory in the
  357. * spec, but we are not required to do any useful work. We couldn't really
  358. * do a useful abort, so don't bother even with waiting for the command
  359. * to be exectuted and return immediately telling the command to abort
  360. * wasn't found.
  361. */
  362. static void nvmet_execute_abort(struct nvmet_req *req)
  363. {
  364. nvmet_set_result(req, 1);
  365. nvmet_req_complete(req, 0);
  366. }
  367. static void nvmet_execute_set_features(struct nvmet_req *req)
  368. {
  369. struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
  370. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
  371. u32 val32;
  372. u16 status = 0;
  373. switch (cdw10 & 0xff) {
  374. case NVME_FEAT_NUM_QUEUES:
  375. nvmet_set_result(req,
  376. (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
  377. break;
  378. case NVME_FEAT_KATO:
  379. val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
  380. req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
  381. nvmet_set_result(req, req->sq->ctrl->kato);
  382. break;
  383. case NVME_FEAT_HOST_ID:
  384. status = NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  385. break;
  386. default:
  387. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  388. break;
  389. }
  390. nvmet_req_complete(req, status);
  391. }
  392. static void nvmet_execute_get_features(struct nvmet_req *req)
  393. {
  394. struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
  395. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
  396. u16 status = 0;
  397. switch (cdw10 & 0xff) {
  398. /*
  399. * These features are mandatory in the spec, but we don't
  400. * have a useful way to implement them. We'll eventually
  401. * need to come up with some fake values for these.
  402. */
  403. #if 0
  404. case NVME_FEAT_ARBITRATION:
  405. break;
  406. case NVME_FEAT_POWER_MGMT:
  407. break;
  408. case NVME_FEAT_TEMP_THRESH:
  409. break;
  410. case NVME_FEAT_ERR_RECOVERY:
  411. break;
  412. case NVME_FEAT_IRQ_COALESCE:
  413. break;
  414. case NVME_FEAT_IRQ_CONFIG:
  415. break;
  416. case NVME_FEAT_WRITE_ATOMIC:
  417. break;
  418. case NVME_FEAT_ASYNC_EVENT:
  419. break;
  420. #endif
  421. case NVME_FEAT_VOLATILE_WC:
  422. nvmet_set_result(req, 1);
  423. break;
  424. case NVME_FEAT_NUM_QUEUES:
  425. nvmet_set_result(req,
  426. (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
  427. break;
  428. case NVME_FEAT_KATO:
  429. nvmet_set_result(req, req->sq->ctrl->kato * 1000);
  430. break;
  431. case NVME_FEAT_HOST_ID:
  432. /* need 128-bit host identifier flag */
  433. if (!(req->cmd->common.cdw10[1] & cpu_to_le32(1 << 0))) {
  434. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  435. break;
  436. }
  437. status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid,
  438. sizeof(req->sq->ctrl->hostid));
  439. break;
  440. default:
  441. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  442. break;
  443. }
  444. nvmet_req_complete(req, status);
  445. }
  446. static void nvmet_execute_async_event(struct nvmet_req *req)
  447. {
  448. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  449. mutex_lock(&ctrl->lock);
  450. if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
  451. mutex_unlock(&ctrl->lock);
  452. nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR);
  453. return;
  454. }
  455. ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
  456. mutex_unlock(&ctrl->lock);
  457. schedule_work(&ctrl->async_event_work);
  458. }
  459. static void nvmet_execute_keep_alive(struct nvmet_req *req)
  460. {
  461. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  462. pr_debug("ctrl %d update keep-alive timer for %d secs\n",
  463. ctrl->cntlid, ctrl->kato);
  464. mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
  465. nvmet_req_complete(req, 0);
  466. }
  467. u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
  468. {
  469. struct nvme_command *cmd = req->cmd;
  470. u16 ret;
  471. req->ns = NULL;
  472. ret = nvmet_check_ctrl_status(req, cmd);
  473. if (unlikely(ret))
  474. return ret;
  475. switch (cmd->common.opcode) {
  476. case nvme_admin_get_log_page:
  477. req->data_len = nvmet_get_log_page_len(cmd);
  478. switch (cmd->get_log_page.lid) {
  479. case NVME_LOG_ERROR:
  480. case NVME_LOG_SMART:
  481. case NVME_LOG_FW_SLOT:
  482. req->execute = nvmet_execute_get_log_page;
  483. return 0;
  484. }
  485. break;
  486. case nvme_admin_identify:
  487. req->data_len = NVME_IDENTIFY_DATA_SIZE;
  488. switch (cmd->identify.cns) {
  489. case NVME_ID_CNS_NS:
  490. req->execute = nvmet_execute_identify_ns;
  491. return 0;
  492. case NVME_ID_CNS_CTRL:
  493. req->execute = nvmet_execute_identify_ctrl;
  494. return 0;
  495. case NVME_ID_CNS_NS_ACTIVE_LIST:
  496. req->execute = nvmet_execute_identify_nslist;
  497. return 0;
  498. case NVME_ID_CNS_NS_DESC_LIST:
  499. req->execute = nvmet_execute_identify_desclist;
  500. return 0;
  501. }
  502. break;
  503. case nvme_admin_abort_cmd:
  504. req->execute = nvmet_execute_abort;
  505. req->data_len = 0;
  506. return 0;
  507. case nvme_admin_set_features:
  508. req->execute = nvmet_execute_set_features;
  509. req->data_len = 0;
  510. return 0;
  511. case nvme_admin_get_features:
  512. req->execute = nvmet_execute_get_features;
  513. req->data_len = 0;
  514. return 0;
  515. case nvme_admin_async_event:
  516. req->execute = nvmet_execute_async_event;
  517. req->data_len = 0;
  518. return 0;
  519. case nvme_admin_keep_alive:
  520. req->execute = nvmet_execute_keep_alive;
  521. req->data_len = 0;
  522. return 0;
  523. }
  524. pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
  525. req->sq->qid);
  526. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  527. }