admin-cmd.c 22 KB

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