admin-cmd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. /*
  21. * This helper allows us to clear the AEN based on the RAE bit,
  22. * Please use this helper when processing the log pages which are
  23. * associated with the AEN.
  24. */
  25. static inline void nvmet_clear_aen(struct nvmet_req *req, u32 aen_bit)
  26. {
  27. int rae = le32_to_cpu(req->cmd->common.cdw10[0]) & 1 << 15;
  28. if (!rae)
  29. clear_bit(aen_bit, &req->sq->ctrl->aen_masked);
  30. }
  31. u32 nvmet_get_log_page_len(struct nvme_command *cmd)
  32. {
  33. u32 len = le16_to_cpu(cmd->get_log_page.numdu);
  34. len <<= 16;
  35. len += le16_to_cpu(cmd->get_log_page.numdl);
  36. /* NUMD is a 0's based value */
  37. len += 1;
  38. len *= sizeof(u32);
  39. return len;
  40. }
  41. static void nvmet_execute_get_log_page_noop(struct nvmet_req *req)
  42. {
  43. nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->data_len));
  44. }
  45. static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
  46. struct nvme_smart_log *slog)
  47. {
  48. struct nvmet_ns *ns;
  49. u64 host_reads, host_writes, data_units_read, data_units_written;
  50. ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->get_log_page.nsid);
  51. if (!ns) {
  52. pr_err("nvmet : Could not find namespace id : %d\n",
  53. le32_to_cpu(req->cmd->get_log_page.nsid));
  54. return NVME_SC_INVALID_NS;
  55. }
  56. /* we don't have the right data for file backed ns */
  57. if (!ns->bdev)
  58. goto out;
  59. host_reads = part_stat_read(ns->bdev->bd_part, ios[READ]);
  60. data_units_read = DIV_ROUND_UP(part_stat_read(ns->bdev->bd_part,
  61. sectors[READ]), 1000);
  62. host_writes = part_stat_read(ns->bdev->bd_part, ios[WRITE]);
  63. data_units_written = DIV_ROUND_UP(part_stat_read(ns->bdev->bd_part,
  64. sectors[WRITE]), 1000);
  65. put_unaligned_le64(host_reads, &slog->host_reads[0]);
  66. put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
  67. put_unaligned_le64(host_writes, &slog->host_writes[0]);
  68. put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
  69. out:
  70. nvmet_put_namespace(ns);
  71. return NVME_SC_SUCCESS;
  72. }
  73. static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
  74. struct nvme_smart_log *slog)
  75. {
  76. u64 host_reads = 0, host_writes = 0;
  77. u64 data_units_read = 0, data_units_written = 0;
  78. struct nvmet_ns *ns;
  79. struct nvmet_ctrl *ctrl;
  80. ctrl = req->sq->ctrl;
  81. rcu_read_lock();
  82. list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
  83. /* we don't have the right data for file backed ns */
  84. if (!ns->bdev)
  85. continue;
  86. host_reads += part_stat_read(ns->bdev->bd_part, ios[READ]);
  87. data_units_read += DIV_ROUND_UP(
  88. part_stat_read(ns->bdev->bd_part, sectors[READ]), 1000);
  89. host_writes += part_stat_read(ns->bdev->bd_part, ios[WRITE]);
  90. data_units_written += DIV_ROUND_UP(
  91. part_stat_read(ns->bdev->bd_part, sectors[WRITE]), 1000);
  92. }
  93. rcu_read_unlock();
  94. put_unaligned_le64(host_reads, &slog->host_reads[0]);
  95. put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
  96. put_unaligned_le64(host_writes, &slog->host_writes[0]);
  97. put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
  98. return NVME_SC_SUCCESS;
  99. }
  100. static void nvmet_execute_get_log_page_smart(struct nvmet_req *req)
  101. {
  102. struct nvme_smart_log *log;
  103. u16 status = NVME_SC_INTERNAL;
  104. if (req->data_len != sizeof(*log))
  105. goto out;
  106. log = kzalloc(sizeof(*log), GFP_KERNEL);
  107. if (!log)
  108. goto out;
  109. if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL))
  110. status = nvmet_get_smart_log_all(req, log);
  111. else
  112. status = nvmet_get_smart_log_nsid(req, log);
  113. if (status)
  114. goto out_free_log;
  115. status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
  116. out_free_log:
  117. kfree(log);
  118. out:
  119. nvmet_req_complete(req, status);
  120. }
  121. static void nvmet_execute_get_log_cmd_effects_ns(struct nvmet_req *req)
  122. {
  123. u16 status = NVME_SC_INTERNAL;
  124. struct nvme_effects_log *log;
  125. log = kzalloc(sizeof(*log), GFP_KERNEL);
  126. if (!log)
  127. goto out;
  128. log->acs[nvme_admin_get_log_page] = cpu_to_le32(1 << 0);
  129. log->acs[nvme_admin_identify] = cpu_to_le32(1 << 0);
  130. log->acs[nvme_admin_abort_cmd] = cpu_to_le32(1 << 0);
  131. log->acs[nvme_admin_set_features] = cpu_to_le32(1 << 0);
  132. log->acs[nvme_admin_get_features] = cpu_to_le32(1 << 0);
  133. log->acs[nvme_admin_async_event] = cpu_to_le32(1 << 0);
  134. log->acs[nvme_admin_keep_alive] = cpu_to_le32(1 << 0);
  135. log->iocs[nvme_cmd_read] = cpu_to_le32(1 << 0);
  136. log->iocs[nvme_cmd_write] = cpu_to_le32(1 << 0);
  137. log->iocs[nvme_cmd_flush] = cpu_to_le32(1 << 0);
  138. log->iocs[nvme_cmd_dsm] = cpu_to_le32(1 << 0);
  139. log->iocs[nvme_cmd_write_zeroes] = cpu_to_le32(1 << 0);
  140. status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
  141. kfree(log);
  142. out:
  143. nvmet_req_complete(req, status);
  144. }
  145. static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req)
  146. {
  147. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  148. u16 status = NVME_SC_INTERNAL;
  149. size_t len;
  150. if (req->data_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32))
  151. goto out;
  152. mutex_lock(&ctrl->lock);
  153. if (ctrl->nr_changed_ns == U32_MAX)
  154. len = sizeof(__le32);
  155. else
  156. len = ctrl->nr_changed_ns * sizeof(__le32);
  157. status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len);
  158. if (!status)
  159. status = nvmet_zero_sgl(req, len, req->data_len - len);
  160. ctrl->nr_changed_ns = 0;
  161. nvmet_clear_aen(req, NVME_AEN_CFG_NS_ATTR);
  162. mutex_unlock(&ctrl->lock);
  163. out:
  164. nvmet_req_complete(req, status);
  165. }
  166. static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
  167. struct nvme_ana_group_desc *desc)
  168. {
  169. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  170. struct nvmet_ns *ns;
  171. u32 count = 0;
  172. if (!(req->cmd->get_log_page.lsp & NVME_ANA_LOG_RGO)) {
  173. rcu_read_lock();
  174. list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link)
  175. if (ns->anagrpid == grpid)
  176. desc->nsids[count++] = cpu_to_le32(ns->nsid);
  177. rcu_read_unlock();
  178. }
  179. desc->grpid = cpu_to_le32(grpid);
  180. desc->nnsids = cpu_to_le32(count);
  181. desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
  182. desc->state = req->port->ana_state[grpid];
  183. memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
  184. return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32);
  185. }
  186. static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
  187. {
  188. struct nvme_ana_rsp_hdr hdr = { 0, };
  189. struct nvme_ana_group_desc *desc;
  190. size_t offset = sizeof(struct nvme_ana_rsp_hdr); /* start beyond hdr */
  191. size_t len;
  192. u32 grpid;
  193. u16 ngrps = 0;
  194. u16 status;
  195. status = NVME_SC_INTERNAL;
  196. desc = kmalloc(sizeof(struct nvme_ana_group_desc) +
  197. NVMET_MAX_NAMESPACES * sizeof(__le32), GFP_KERNEL);
  198. if (!desc)
  199. goto out;
  200. down_read(&nvmet_ana_sem);
  201. for (grpid = 1; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
  202. if (!nvmet_ana_group_enabled[grpid])
  203. continue;
  204. len = nvmet_format_ana_group(req, grpid, desc);
  205. status = nvmet_copy_to_sgl(req, offset, desc, len);
  206. if (status)
  207. break;
  208. offset += len;
  209. ngrps++;
  210. }
  211. for ( ; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
  212. if (nvmet_ana_group_enabled[grpid])
  213. ngrps++;
  214. }
  215. hdr.chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
  216. hdr.ngrps = cpu_to_le16(ngrps);
  217. nvmet_clear_aen(req, NVME_AEN_CFG_ANA_CHANGE);
  218. up_read(&nvmet_ana_sem);
  219. kfree(desc);
  220. /* copy the header last once we know the number of groups */
  221. status = nvmet_copy_to_sgl(req, 0, &hdr, sizeof(hdr));
  222. out:
  223. nvmet_req_complete(req, status);
  224. }
  225. static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
  226. {
  227. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  228. struct nvme_id_ctrl *id;
  229. u16 status = 0;
  230. const char model[] = "Linux";
  231. id = kzalloc(sizeof(*id), GFP_KERNEL);
  232. if (!id) {
  233. status = NVME_SC_INTERNAL;
  234. goto out;
  235. }
  236. /* XXX: figure out how to assign real vendors IDs. */
  237. id->vid = 0;
  238. id->ssvid = 0;
  239. memset(id->sn, ' ', sizeof(id->sn));
  240. bin2hex(id->sn, &ctrl->subsys->serial,
  241. min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
  242. memcpy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1, ' ');
  243. memcpy_and_pad(id->fr, sizeof(id->fr),
  244. UTS_RELEASE, strlen(UTS_RELEASE), ' ');
  245. id->rab = 6;
  246. /*
  247. * XXX: figure out how we can assign a IEEE OUI, but until then
  248. * the safest is to leave it as zeroes.
  249. */
  250. /* we support multiple ports, multiples hosts and ANA: */
  251. id->cmic = (1 << 0) | (1 << 1) | (1 << 3);
  252. /* no limit on data transfer sizes for now */
  253. id->mdts = 0;
  254. id->cntlid = cpu_to_le16(ctrl->cntlid);
  255. id->ver = cpu_to_le32(ctrl->subsys->ver);
  256. /* XXX: figure out what to do about RTD3R/RTD3 */
  257. id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL);
  258. id->ctratt = cpu_to_le32(1 << 0);
  259. id->oacs = 0;
  260. /*
  261. * We don't really have a practical limit on the number of abort
  262. * comands. But we don't do anything useful for abort either, so
  263. * no point in allowing more abort commands than the spec requires.
  264. */
  265. id->acl = 3;
  266. id->aerl = NVMET_ASYNC_EVENTS - 1;
  267. /* first slot is read-only, only one slot supported */
  268. id->frmw = (1 << 0) | (1 << 1);
  269. id->lpa = (1 << 0) | (1 << 1) | (1 << 2);
  270. id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
  271. id->npss = 0;
  272. /* We support keep-alive timeout in granularity of seconds */
  273. id->kas = cpu_to_le16(NVMET_KAS);
  274. id->sqes = (0x6 << 4) | 0x6;
  275. id->cqes = (0x4 << 4) | 0x4;
  276. /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
  277. id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
  278. id->nn = cpu_to_le32(ctrl->subsys->max_nsid);
  279. id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES);
  280. id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
  281. NVME_CTRL_ONCS_WRITE_ZEROES);
  282. /* XXX: don't report vwc if the underlying device is write through */
  283. id->vwc = NVME_CTRL_VWC_PRESENT;
  284. /*
  285. * We can't support atomic writes bigger than a LBA without support
  286. * from the backend device.
  287. */
  288. id->awun = 0;
  289. id->awupf = 0;
  290. id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
  291. if (ctrl->ops->has_keyed_sgls)
  292. id->sgls |= cpu_to_le32(1 << 2);
  293. if (req->port->inline_data_size)
  294. id->sgls |= cpu_to_le32(1 << 20);
  295. strcpy(id->subnqn, ctrl->subsys->subsysnqn);
  296. /* Max command capsule size is sqe + single page of in-capsule data */
  297. id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
  298. req->port->inline_data_size) / 16);
  299. /* Max response capsule size is cqe */
  300. id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
  301. id->msdbd = ctrl->ops->msdbd;
  302. id->anacap = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
  303. id->anatt = 10; /* random value */
  304. id->anagrpmax = cpu_to_le32(NVMET_MAX_ANAGRPS);
  305. id->nanagrpid = cpu_to_le32(NVMET_MAX_ANAGRPS);
  306. /*
  307. * Meh, we don't really support any power state. Fake up the same
  308. * values that qemu does.
  309. */
  310. id->psd[0].max_power = cpu_to_le16(0x9c4);
  311. id->psd[0].entry_lat = cpu_to_le32(0x10);
  312. id->psd[0].exit_lat = cpu_to_le32(0x4);
  313. id->nwpc = 1 << 0; /* write protect and no write protect */
  314. status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
  315. kfree(id);
  316. out:
  317. nvmet_req_complete(req, status);
  318. }
  319. static void nvmet_execute_identify_ns(struct nvmet_req *req)
  320. {
  321. struct nvmet_ns *ns;
  322. struct nvme_id_ns *id;
  323. u16 status = 0;
  324. if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
  325. status = NVME_SC_INVALID_NS | NVME_SC_DNR;
  326. goto out;
  327. }
  328. id = kzalloc(sizeof(*id), GFP_KERNEL);
  329. if (!id) {
  330. status = NVME_SC_INTERNAL;
  331. goto out;
  332. }
  333. /* return an all zeroed buffer if we can't find an active namespace */
  334. ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
  335. if (!ns)
  336. goto done;
  337. /*
  338. * nuse = ncap = nsze isn't always true, but we have no way to find
  339. * that out from the underlying device.
  340. */
  341. id->ncap = id->nsze = cpu_to_le64(ns->size >> ns->blksize_shift);
  342. switch (req->port->ana_state[ns->anagrpid]) {
  343. case NVME_ANA_INACCESSIBLE:
  344. case NVME_ANA_PERSISTENT_LOSS:
  345. break;
  346. default:
  347. id->nuse = id->nsze;
  348. break;
  349. }
  350. /*
  351. * We just provide a single LBA format that matches what the
  352. * underlying device reports.
  353. */
  354. id->nlbaf = 0;
  355. id->flbas = 0;
  356. /*
  357. * Our namespace might always be shared. Not just with other
  358. * controllers, but also with any other user of the block device.
  359. */
  360. id->nmic = (1 << 0);
  361. id->anagrpid = cpu_to_le32(ns->anagrpid);
  362. memcpy(&id->nguid, &ns->nguid, sizeof(id->nguid));
  363. id->lbaf[0].ds = ns->blksize_shift;
  364. if (ns->readonly)
  365. id->nsattr |= (1 << 0);
  366. nvmet_put_namespace(ns);
  367. done:
  368. status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
  369. kfree(id);
  370. out:
  371. nvmet_req_complete(req, status);
  372. }
  373. static void nvmet_execute_identify_nslist(struct nvmet_req *req)
  374. {
  375. static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
  376. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  377. struct nvmet_ns *ns;
  378. u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
  379. __le32 *list;
  380. u16 status = 0;
  381. int i = 0;
  382. list = kzalloc(buf_size, GFP_KERNEL);
  383. if (!list) {
  384. status = NVME_SC_INTERNAL;
  385. goto out;
  386. }
  387. rcu_read_lock();
  388. list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
  389. if (ns->nsid <= min_nsid)
  390. continue;
  391. list[i++] = cpu_to_le32(ns->nsid);
  392. if (i == buf_size / sizeof(__le32))
  393. break;
  394. }
  395. rcu_read_unlock();
  396. status = nvmet_copy_to_sgl(req, 0, list, buf_size);
  397. kfree(list);
  398. out:
  399. nvmet_req_complete(req, status);
  400. }
  401. static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len,
  402. void *id, off_t *off)
  403. {
  404. struct nvme_ns_id_desc desc = {
  405. .nidt = type,
  406. .nidl = len,
  407. };
  408. u16 status;
  409. status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc));
  410. if (status)
  411. return status;
  412. *off += sizeof(desc);
  413. status = nvmet_copy_to_sgl(req, *off, id, len);
  414. if (status)
  415. return status;
  416. *off += len;
  417. return 0;
  418. }
  419. static void nvmet_execute_identify_desclist(struct nvmet_req *req)
  420. {
  421. struct nvmet_ns *ns;
  422. u16 status = 0;
  423. off_t off = 0;
  424. ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
  425. if (!ns) {
  426. status = NVME_SC_INVALID_NS | NVME_SC_DNR;
  427. goto out;
  428. }
  429. if (memchr_inv(&ns->uuid, 0, sizeof(ns->uuid))) {
  430. status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
  431. NVME_NIDT_UUID_LEN,
  432. &ns->uuid, &off);
  433. if (status)
  434. goto out_put_ns;
  435. }
  436. if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid))) {
  437. status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
  438. NVME_NIDT_NGUID_LEN,
  439. &ns->nguid, &off);
  440. if (status)
  441. goto out_put_ns;
  442. }
  443. if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off,
  444. off) != NVME_IDENTIFY_DATA_SIZE - off)
  445. status = NVME_SC_INTERNAL | NVME_SC_DNR;
  446. out_put_ns:
  447. nvmet_put_namespace(ns);
  448. out:
  449. nvmet_req_complete(req, status);
  450. }
  451. /*
  452. * A "minimum viable" abort implementation: the command is mandatory in the
  453. * spec, but we are not required to do any useful work. We couldn't really
  454. * do a useful abort, so don't bother even with waiting for the command
  455. * to be exectuted and return immediately telling the command to abort
  456. * wasn't found.
  457. */
  458. static void nvmet_execute_abort(struct nvmet_req *req)
  459. {
  460. nvmet_set_result(req, 1);
  461. nvmet_req_complete(req, 0);
  462. }
  463. static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req)
  464. {
  465. u16 status;
  466. if (req->ns->file)
  467. status = nvmet_file_flush(req);
  468. else
  469. status = nvmet_bdev_flush(req);
  470. if (status)
  471. pr_err("write protect flush failed nsid: %u\n", req->ns->nsid);
  472. return status;
  473. }
  474. static u16 nvmet_set_feat_write_protect(struct nvmet_req *req)
  475. {
  476. u32 write_protect = le32_to_cpu(req->cmd->common.cdw10[1]);
  477. struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
  478. u16 status = NVME_SC_FEATURE_NOT_CHANGEABLE;
  479. req->ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->rw.nsid);
  480. if (unlikely(!req->ns))
  481. return status;
  482. mutex_lock(&subsys->lock);
  483. switch (write_protect) {
  484. case NVME_NS_WRITE_PROTECT:
  485. req->ns->readonly = true;
  486. status = nvmet_write_protect_flush_sync(req);
  487. if (status)
  488. req->ns->readonly = false;
  489. break;
  490. case NVME_NS_NO_WRITE_PROTECT:
  491. req->ns->readonly = false;
  492. status = 0;
  493. break;
  494. default:
  495. break;
  496. }
  497. if (!status)
  498. nvmet_ns_changed(subsys, req->ns->nsid);
  499. mutex_unlock(&subsys->lock);
  500. return status;
  501. }
  502. static void nvmet_execute_set_features(struct nvmet_req *req)
  503. {
  504. struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
  505. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
  506. u32 val32;
  507. u16 status = 0;
  508. switch (cdw10 & 0xff) {
  509. case NVME_FEAT_NUM_QUEUES:
  510. nvmet_set_result(req,
  511. (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
  512. break;
  513. case NVME_FEAT_KATO:
  514. val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
  515. req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
  516. nvmet_set_result(req, req->sq->ctrl->kato);
  517. break;
  518. case NVME_FEAT_ASYNC_EVENT:
  519. val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
  520. if (val32 & ~NVMET_AEN_CFG_ALL) {
  521. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  522. break;
  523. }
  524. WRITE_ONCE(req->sq->ctrl->aen_enabled, val32);
  525. nvmet_set_result(req, val32);
  526. break;
  527. case NVME_FEAT_HOST_ID:
  528. status = NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  529. break;
  530. case NVME_FEAT_WRITE_PROTECT:
  531. status = nvmet_set_feat_write_protect(req);
  532. break;
  533. default:
  534. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  535. break;
  536. }
  537. nvmet_req_complete(req, status);
  538. }
  539. static u16 nvmet_get_feat_write_protect(struct nvmet_req *req)
  540. {
  541. struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
  542. u32 result;
  543. req->ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->common.nsid);
  544. if (!req->ns)
  545. return NVME_SC_INVALID_NS | NVME_SC_DNR;
  546. mutex_lock(&subsys->lock);
  547. if (req->ns->readonly == true)
  548. result = NVME_NS_WRITE_PROTECT;
  549. else
  550. result = NVME_NS_NO_WRITE_PROTECT;
  551. nvmet_set_result(req, result);
  552. mutex_unlock(&subsys->lock);
  553. return 0;
  554. }
  555. static void nvmet_execute_get_features(struct nvmet_req *req)
  556. {
  557. struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
  558. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
  559. u16 status = 0;
  560. switch (cdw10 & 0xff) {
  561. /*
  562. * These features are mandatory in the spec, but we don't
  563. * have a useful way to implement them. We'll eventually
  564. * need to come up with some fake values for these.
  565. */
  566. #if 0
  567. case NVME_FEAT_ARBITRATION:
  568. break;
  569. case NVME_FEAT_POWER_MGMT:
  570. break;
  571. case NVME_FEAT_TEMP_THRESH:
  572. break;
  573. case NVME_FEAT_ERR_RECOVERY:
  574. break;
  575. case NVME_FEAT_IRQ_COALESCE:
  576. break;
  577. case NVME_FEAT_IRQ_CONFIG:
  578. break;
  579. case NVME_FEAT_WRITE_ATOMIC:
  580. break;
  581. #endif
  582. case NVME_FEAT_ASYNC_EVENT:
  583. nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled));
  584. break;
  585. case NVME_FEAT_VOLATILE_WC:
  586. nvmet_set_result(req, 1);
  587. break;
  588. case NVME_FEAT_NUM_QUEUES:
  589. nvmet_set_result(req,
  590. (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
  591. break;
  592. case NVME_FEAT_KATO:
  593. nvmet_set_result(req, req->sq->ctrl->kato * 1000);
  594. break;
  595. case NVME_FEAT_HOST_ID:
  596. /* need 128-bit host identifier flag */
  597. if (!(req->cmd->common.cdw10[1] & cpu_to_le32(1 << 0))) {
  598. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  599. break;
  600. }
  601. status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid,
  602. sizeof(req->sq->ctrl->hostid));
  603. break;
  604. case NVME_FEAT_WRITE_PROTECT:
  605. status = nvmet_get_feat_write_protect(req);
  606. break;
  607. default:
  608. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  609. break;
  610. }
  611. nvmet_req_complete(req, status);
  612. }
  613. static void nvmet_execute_async_event(struct nvmet_req *req)
  614. {
  615. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  616. mutex_lock(&ctrl->lock);
  617. if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
  618. mutex_unlock(&ctrl->lock);
  619. nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR);
  620. return;
  621. }
  622. ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
  623. mutex_unlock(&ctrl->lock);
  624. schedule_work(&ctrl->async_event_work);
  625. }
  626. static void nvmet_execute_keep_alive(struct nvmet_req *req)
  627. {
  628. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  629. pr_debug("ctrl %d update keep-alive timer for %d secs\n",
  630. ctrl->cntlid, ctrl->kato);
  631. mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
  632. nvmet_req_complete(req, 0);
  633. }
  634. u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
  635. {
  636. struct nvme_command *cmd = req->cmd;
  637. u16 ret;
  638. ret = nvmet_check_ctrl_status(req, cmd);
  639. if (unlikely(ret))
  640. return ret;
  641. switch (cmd->common.opcode) {
  642. case nvme_admin_get_log_page:
  643. req->data_len = nvmet_get_log_page_len(cmd);
  644. switch (cmd->get_log_page.lid) {
  645. case NVME_LOG_ERROR:
  646. /*
  647. * We currently never set the More bit in the status
  648. * field, so all error log entries are invalid and can
  649. * be zeroed out. This is called a minum viable
  650. * implementation (TM) of this mandatory log page.
  651. */
  652. req->execute = nvmet_execute_get_log_page_noop;
  653. return 0;
  654. case NVME_LOG_SMART:
  655. req->execute = nvmet_execute_get_log_page_smart;
  656. return 0;
  657. case NVME_LOG_FW_SLOT:
  658. /*
  659. * We only support a single firmware slot which always
  660. * is active, so we can zero out the whole firmware slot
  661. * log and still claim to fully implement this mandatory
  662. * log page.
  663. */
  664. req->execute = nvmet_execute_get_log_page_noop;
  665. return 0;
  666. case NVME_LOG_CHANGED_NS:
  667. req->execute = nvmet_execute_get_log_changed_ns;
  668. return 0;
  669. case NVME_LOG_CMD_EFFECTS:
  670. req->execute = nvmet_execute_get_log_cmd_effects_ns;
  671. return 0;
  672. case NVME_LOG_ANA:
  673. req->execute = nvmet_execute_get_log_page_ana;
  674. return 0;
  675. }
  676. break;
  677. case nvme_admin_identify:
  678. req->data_len = NVME_IDENTIFY_DATA_SIZE;
  679. switch (cmd->identify.cns) {
  680. case NVME_ID_CNS_NS:
  681. req->execute = nvmet_execute_identify_ns;
  682. return 0;
  683. case NVME_ID_CNS_CTRL:
  684. req->execute = nvmet_execute_identify_ctrl;
  685. return 0;
  686. case NVME_ID_CNS_NS_ACTIVE_LIST:
  687. req->execute = nvmet_execute_identify_nslist;
  688. return 0;
  689. case NVME_ID_CNS_NS_DESC_LIST:
  690. req->execute = nvmet_execute_identify_desclist;
  691. return 0;
  692. }
  693. break;
  694. case nvme_admin_abort_cmd:
  695. req->execute = nvmet_execute_abort;
  696. req->data_len = 0;
  697. return 0;
  698. case nvme_admin_set_features:
  699. req->execute = nvmet_execute_set_features;
  700. req->data_len = 0;
  701. return 0;
  702. case nvme_admin_get_features:
  703. req->execute = nvmet_execute_get_features;
  704. req->data_len = 0;
  705. return 0;
  706. case nvme_admin_async_event:
  707. req->execute = nvmet_execute_async_event;
  708. req->data_len = 0;
  709. return 0;
  710. case nvme_admin_keep_alive:
  711. req->execute = nvmet_execute_keep_alive;
  712. req->data_len = 0;
  713. return 0;
  714. }
  715. pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
  716. req->sq->qid);
  717. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  718. }