admin-cmd.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVMe admin command implementation.
  4. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/module.h>
  8. #include <linux/rculist.h>
  9. #include <linux/part_stat.h>
  10. #include <generated/utsrelease.h>
  11. #include <asm/unaligned.h>
  12. #include "nvmet.h"
  13. u32 nvmet_get_log_page_len(struct nvme_command *cmd)
  14. {
  15. u32 len = le16_to_cpu(cmd->get_log_page.numdu);
  16. len <<= 16;
  17. len += le16_to_cpu(cmd->get_log_page.numdl);
  18. /* NUMD is a 0's based value */
  19. len += 1;
  20. len *= sizeof(u32);
  21. return len;
  22. }
  23. static u32 nvmet_feat_data_len(struct nvmet_req *req, u32 cdw10)
  24. {
  25. switch (cdw10 & 0xff) {
  26. case NVME_FEAT_HOST_ID:
  27. return sizeof(req->sq->ctrl->hostid);
  28. default:
  29. return 0;
  30. }
  31. }
  32. u64 nvmet_get_log_page_offset(struct nvme_command *cmd)
  33. {
  34. return le64_to_cpu(cmd->get_log_page.lpo);
  35. }
  36. static void nvmet_execute_get_log_page_noop(struct nvmet_req *req)
  37. {
  38. nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->transfer_len));
  39. }
  40. static void nvmet_execute_get_log_page_error(struct nvmet_req *req)
  41. {
  42. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  43. unsigned long flags;
  44. off_t offset = 0;
  45. u64 slot;
  46. u64 i;
  47. spin_lock_irqsave(&ctrl->error_lock, flags);
  48. slot = ctrl->err_counter % NVMET_ERROR_LOG_SLOTS;
  49. for (i = 0; i < NVMET_ERROR_LOG_SLOTS; i++) {
  50. if (nvmet_copy_to_sgl(req, offset, &ctrl->slots[slot],
  51. sizeof(struct nvme_error_slot)))
  52. break;
  53. if (slot == 0)
  54. slot = NVMET_ERROR_LOG_SLOTS - 1;
  55. else
  56. slot--;
  57. offset += sizeof(struct nvme_error_slot);
  58. }
  59. spin_unlock_irqrestore(&ctrl->error_lock, flags);
  60. nvmet_req_complete(req, 0);
  61. }
  62. static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
  63. struct nvme_smart_log *slog)
  64. {
  65. u64 host_reads, host_writes, data_units_read, data_units_written;
  66. u16 status;
  67. status = nvmet_req_find_ns(req);
  68. if (status)
  69. return status;
  70. /* we don't have the right data for file backed ns */
  71. if (!req->ns->bdev)
  72. return NVME_SC_SUCCESS;
  73. host_reads = part_stat_read(req->ns->bdev, ios[READ]);
  74. data_units_read =
  75. DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000);
  76. host_writes = part_stat_read(req->ns->bdev, ios[WRITE]);
  77. data_units_written =
  78. DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000);
  79. put_unaligned_le64(host_reads, &slog->host_reads[0]);
  80. put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
  81. put_unaligned_le64(host_writes, &slog->host_writes[0]);
  82. put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
  83. return NVME_SC_SUCCESS;
  84. }
  85. static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
  86. struct nvme_smart_log *slog)
  87. {
  88. u64 host_reads = 0, host_writes = 0;
  89. u64 data_units_read = 0, data_units_written = 0;
  90. struct nvmet_ns *ns;
  91. struct nvmet_ctrl *ctrl;
  92. unsigned long idx;
  93. ctrl = req->sq->ctrl;
  94. xa_for_each(&ctrl->subsys->namespaces, idx, ns) {
  95. /* we don't have the right data for file backed ns */
  96. if (!ns->bdev)
  97. continue;
  98. host_reads += part_stat_read(ns->bdev, ios[READ]);
  99. data_units_read += DIV_ROUND_UP(
  100. part_stat_read(ns->bdev, sectors[READ]), 1000);
  101. host_writes += part_stat_read(ns->bdev, ios[WRITE]);
  102. data_units_written += DIV_ROUND_UP(
  103. part_stat_read(ns->bdev, sectors[WRITE]), 1000);
  104. }
  105. put_unaligned_le64(host_reads, &slog->host_reads[0]);
  106. put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
  107. put_unaligned_le64(host_writes, &slog->host_writes[0]);
  108. put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
  109. return NVME_SC_SUCCESS;
  110. }
  111. static void nvmet_execute_get_log_page_smart(struct nvmet_req *req)
  112. {
  113. struct nvme_smart_log *log;
  114. u16 status = NVME_SC_INTERNAL;
  115. unsigned long flags;
  116. if (req->transfer_len != sizeof(*log))
  117. goto out;
  118. log = kzalloc(sizeof(*log), GFP_KERNEL);
  119. if (!log)
  120. goto out;
  121. if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL))
  122. status = nvmet_get_smart_log_all(req, log);
  123. else
  124. status = nvmet_get_smart_log_nsid(req, log);
  125. if (status)
  126. goto out_free_log;
  127. spin_lock_irqsave(&req->sq->ctrl->error_lock, flags);
  128. put_unaligned_le64(req->sq->ctrl->err_counter,
  129. &log->num_err_log_entries);
  130. spin_unlock_irqrestore(&req->sq->ctrl->error_lock, flags);
  131. status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
  132. out_free_log:
  133. kfree(log);
  134. out:
  135. nvmet_req_complete(req, status);
  136. }
  137. static void nvmet_get_cmd_effects_nvm(struct nvme_effects_log *log)
  138. {
  139. log->acs[nvme_admin_get_log_page] =
  140. log->acs[nvme_admin_identify] =
  141. log->acs[nvme_admin_abort_cmd] =
  142. log->acs[nvme_admin_set_features] =
  143. log->acs[nvme_admin_get_features] =
  144. log->acs[nvme_admin_async_event] =
  145. log->acs[nvme_admin_keep_alive] =
  146. cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
  147. log->iocs[nvme_cmd_read] =
  148. log->iocs[nvme_cmd_write] =
  149. log->iocs[nvme_cmd_flush] =
  150. log->iocs[nvme_cmd_dsm] =
  151. log->iocs[nvme_cmd_write_zeroes] =
  152. cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
  153. }
  154. static void nvmet_get_cmd_effects_zns(struct nvme_effects_log *log)
  155. {
  156. log->iocs[nvme_cmd_zone_append] =
  157. log->iocs[nvme_cmd_zone_mgmt_send] =
  158. log->iocs[nvme_cmd_zone_mgmt_recv] =
  159. cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
  160. }
  161. static void nvmet_execute_get_log_cmd_effects_ns(struct nvmet_req *req)
  162. {
  163. struct nvme_effects_log *log;
  164. u16 status = NVME_SC_SUCCESS;
  165. log = kzalloc(sizeof(*log), GFP_KERNEL);
  166. if (!log) {
  167. status = NVME_SC_INTERNAL;
  168. goto out;
  169. }
  170. switch (req->cmd->get_log_page.csi) {
  171. case NVME_CSI_NVM:
  172. nvmet_get_cmd_effects_nvm(log);
  173. break;
  174. case NVME_CSI_ZNS:
  175. if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
  176. status = NVME_SC_INVALID_IO_CMD_SET;
  177. goto free;
  178. }
  179. nvmet_get_cmd_effects_nvm(log);
  180. nvmet_get_cmd_effects_zns(log);
  181. break;
  182. default:
  183. status = NVME_SC_INVALID_LOG_PAGE;
  184. goto free;
  185. }
  186. status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
  187. free:
  188. kfree(log);
  189. out:
  190. nvmet_req_complete(req, status);
  191. }
  192. static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req)
  193. {
  194. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  195. u16 status = NVME_SC_INTERNAL;
  196. size_t len;
  197. if (req->transfer_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32))
  198. goto out;
  199. mutex_lock(&ctrl->lock);
  200. if (ctrl->nr_changed_ns == U32_MAX)
  201. len = sizeof(__le32);
  202. else
  203. len = ctrl->nr_changed_ns * sizeof(__le32);
  204. status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len);
  205. if (!status)
  206. status = nvmet_zero_sgl(req, len, req->transfer_len - len);
  207. ctrl->nr_changed_ns = 0;
  208. nvmet_clear_aen_bit(req, NVME_AEN_BIT_NS_ATTR);
  209. mutex_unlock(&ctrl->lock);
  210. out:
  211. nvmet_req_complete(req, status);
  212. }
  213. static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
  214. struct nvme_ana_group_desc *desc)
  215. {
  216. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  217. struct nvmet_ns *ns;
  218. unsigned long idx;
  219. u32 count = 0;
  220. if (!(req->cmd->get_log_page.lsp & NVME_ANA_LOG_RGO)) {
  221. xa_for_each(&ctrl->subsys->namespaces, idx, ns)
  222. if (ns->anagrpid == grpid)
  223. desc->nsids[count++] = cpu_to_le32(ns->nsid);
  224. }
  225. desc->grpid = cpu_to_le32(grpid);
  226. desc->nnsids = cpu_to_le32(count);
  227. desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
  228. desc->state = req->port->ana_state[grpid];
  229. memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
  230. return struct_size(desc, nsids, count);
  231. }
  232. static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
  233. {
  234. struct nvme_ana_rsp_hdr hdr = { 0, };
  235. struct nvme_ana_group_desc *desc;
  236. size_t offset = sizeof(struct nvme_ana_rsp_hdr); /* start beyond hdr */
  237. size_t len;
  238. u32 grpid;
  239. u16 ngrps = 0;
  240. u16 status;
  241. status = NVME_SC_INTERNAL;
  242. desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
  243. GFP_KERNEL);
  244. if (!desc)
  245. goto out;
  246. down_read(&nvmet_ana_sem);
  247. for (grpid = 1; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
  248. if (!nvmet_ana_group_enabled[grpid])
  249. continue;
  250. len = nvmet_format_ana_group(req, grpid, desc);
  251. status = nvmet_copy_to_sgl(req, offset, desc, len);
  252. if (status)
  253. break;
  254. offset += len;
  255. ngrps++;
  256. }
  257. for ( ; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
  258. if (nvmet_ana_group_enabled[grpid])
  259. ngrps++;
  260. }
  261. hdr.chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
  262. hdr.ngrps = cpu_to_le16(ngrps);
  263. nvmet_clear_aen_bit(req, NVME_AEN_BIT_ANA_CHANGE);
  264. up_read(&nvmet_ana_sem);
  265. kfree(desc);
  266. /* copy the header last once we know the number of groups */
  267. status = nvmet_copy_to_sgl(req, 0, &hdr, sizeof(hdr));
  268. out:
  269. nvmet_req_complete(req, status);
  270. }
  271. static void nvmet_execute_get_log_page(struct nvmet_req *req)
  272. {
  273. if (!nvmet_check_transfer_len(req, nvmet_get_log_page_len(req->cmd)))
  274. return;
  275. switch (req->cmd->get_log_page.lid) {
  276. case NVME_LOG_ERROR:
  277. return nvmet_execute_get_log_page_error(req);
  278. case NVME_LOG_SMART:
  279. return nvmet_execute_get_log_page_smart(req);
  280. case NVME_LOG_FW_SLOT:
  281. /*
  282. * We only support a single firmware slot which always is
  283. * active, so we can zero out the whole firmware slot log and
  284. * still claim to fully implement this mandatory log page.
  285. */
  286. return nvmet_execute_get_log_page_noop(req);
  287. case NVME_LOG_CHANGED_NS:
  288. return nvmet_execute_get_log_changed_ns(req);
  289. case NVME_LOG_CMD_EFFECTS:
  290. return nvmet_execute_get_log_cmd_effects_ns(req);
  291. case NVME_LOG_ANA:
  292. return nvmet_execute_get_log_page_ana(req);
  293. }
  294. pr_debug("unhandled lid %d on qid %d\n",
  295. req->cmd->get_log_page.lid, req->sq->qid);
  296. req->error_loc = offsetof(struct nvme_get_log_page_command, lid);
  297. nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_SC_DNR);
  298. }
  299. static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
  300. {
  301. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  302. struct nvmet_subsys *subsys = ctrl->subsys;
  303. struct nvme_id_ctrl *id;
  304. u32 cmd_capsule_size;
  305. u16 status = 0;
  306. if (!subsys->subsys_discovered) {
  307. mutex_lock(&subsys->lock);
  308. subsys->subsys_discovered = true;
  309. mutex_unlock(&subsys->lock);
  310. }
  311. id = kzalloc(sizeof(*id), GFP_KERNEL);
  312. if (!id) {
  313. status = NVME_SC_INTERNAL;
  314. goto out;
  315. }
  316. /* XXX: figure out how to assign real vendors IDs. */
  317. id->vid = 0;
  318. id->ssvid = 0;
  319. memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE);
  320. memcpy_and_pad(id->mn, sizeof(id->mn), subsys->model_number,
  321. strlen(subsys->model_number), ' ');
  322. memcpy_and_pad(id->fr, sizeof(id->fr),
  323. UTS_RELEASE, strlen(UTS_RELEASE), ' ');
  324. id->rab = 6;
  325. if (nvmet_is_disc_subsys(ctrl->subsys))
  326. id->cntrltype = NVME_CTRL_DISC;
  327. else
  328. id->cntrltype = NVME_CTRL_IO;
  329. /*
  330. * XXX: figure out how we can assign a IEEE OUI, but until then
  331. * the safest is to leave it as zeroes.
  332. */
  333. /* we support multiple ports, multiples hosts and ANA: */
  334. id->cmic = NVME_CTRL_CMIC_MULTI_PORT | NVME_CTRL_CMIC_MULTI_CTRL |
  335. NVME_CTRL_CMIC_ANA;
  336. /* Limit MDTS according to transport capability */
  337. if (ctrl->ops->get_mdts)
  338. id->mdts = ctrl->ops->get_mdts(ctrl);
  339. else
  340. id->mdts = 0;
  341. id->cntlid = cpu_to_le16(ctrl->cntlid);
  342. id->ver = cpu_to_le32(ctrl->subsys->ver);
  343. /* XXX: figure out what to do about RTD3R/RTD3 */
  344. id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL);
  345. id->ctratt = cpu_to_le32(NVME_CTRL_ATTR_HID_128_BIT |
  346. NVME_CTRL_ATTR_TBKAS);
  347. id->oacs = 0;
  348. /*
  349. * We don't really have a practical limit on the number of abort
  350. * comands. But we don't do anything useful for abort either, so
  351. * no point in allowing more abort commands than the spec requires.
  352. */
  353. id->acl = 3;
  354. id->aerl = NVMET_ASYNC_EVENTS - 1;
  355. /* first slot is read-only, only one slot supported */
  356. id->frmw = (1 << 0) | (1 << 1);
  357. id->lpa = (1 << 0) | (1 << 1) | (1 << 2);
  358. id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
  359. id->npss = 0;
  360. /* We support keep-alive timeout in granularity of seconds */
  361. id->kas = cpu_to_le16(NVMET_KAS);
  362. id->sqes = (0x6 << 4) | 0x6;
  363. id->cqes = (0x4 << 4) | 0x4;
  364. /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
  365. id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
  366. id->nn = cpu_to_le32(NVMET_MAX_NAMESPACES);
  367. id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES);
  368. id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
  369. NVME_CTRL_ONCS_WRITE_ZEROES);
  370. /* XXX: don't report vwc if the underlying device is write through */
  371. id->vwc = NVME_CTRL_VWC_PRESENT;
  372. /*
  373. * We can't support atomic writes bigger than a LBA without support
  374. * from the backend device.
  375. */
  376. id->awun = 0;
  377. id->awupf = 0;
  378. id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
  379. if (ctrl->ops->flags & NVMF_KEYED_SGLS)
  380. id->sgls |= cpu_to_le32(1 << 2);
  381. if (req->port->inline_data_size)
  382. id->sgls |= cpu_to_le32(1 << 20);
  383. strscpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn));
  384. /*
  385. * Max command capsule size is sqe + in-capsule data size.
  386. * Disable in-capsule data for Metadata capable controllers.
  387. */
  388. cmd_capsule_size = sizeof(struct nvme_command);
  389. if (!ctrl->pi_support)
  390. cmd_capsule_size += req->port->inline_data_size;
  391. id->ioccsz = cpu_to_le32(cmd_capsule_size / 16);
  392. /* Max response capsule size is cqe */
  393. id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
  394. id->msdbd = ctrl->ops->msdbd;
  395. id->anacap = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
  396. id->anatt = 10; /* random value */
  397. id->anagrpmax = cpu_to_le32(NVMET_MAX_ANAGRPS);
  398. id->nanagrpid = cpu_to_le32(NVMET_MAX_ANAGRPS);
  399. /*
  400. * Meh, we don't really support any power state. Fake up the same
  401. * values that qemu does.
  402. */
  403. id->psd[0].max_power = cpu_to_le16(0x9c4);
  404. id->psd[0].entry_lat = cpu_to_le32(0x10);
  405. id->psd[0].exit_lat = cpu_to_le32(0x4);
  406. id->nwpc = 1 << 0; /* write protect and no write protect */
  407. status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
  408. kfree(id);
  409. out:
  410. nvmet_req_complete(req, status);
  411. }
  412. static void nvmet_execute_identify_ns(struct nvmet_req *req)
  413. {
  414. struct nvme_id_ns *id;
  415. u16 status;
  416. if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
  417. req->error_loc = offsetof(struct nvme_identify, nsid);
  418. status = NVME_SC_INVALID_NS | NVME_SC_DNR;
  419. goto out;
  420. }
  421. id = kzalloc(sizeof(*id), GFP_KERNEL);
  422. if (!id) {
  423. status = NVME_SC_INTERNAL;
  424. goto out;
  425. }
  426. /* return an all zeroed buffer if we can't find an active namespace */
  427. status = nvmet_req_find_ns(req);
  428. if (status) {
  429. status = 0;
  430. goto done;
  431. }
  432. if (nvmet_ns_revalidate(req->ns)) {
  433. mutex_lock(&req->ns->subsys->lock);
  434. nvmet_ns_changed(req->ns->subsys, req->ns->nsid);
  435. mutex_unlock(&req->ns->subsys->lock);
  436. }
  437. /*
  438. * nuse = ncap = nsze isn't always true, but we have no way to find
  439. * that out from the underlying device.
  440. */
  441. id->ncap = id->nsze =
  442. cpu_to_le64(req->ns->size >> req->ns->blksize_shift);
  443. switch (req->port->ana_state[req->ns->anagrpid]) {
  444. case NVME_ANA_INACCESSIBLE:
  445. case NVME_ANA_PERSISTENT_LOSS:
  446. break;
  447. default:
  448. id->nuse = id->nsze;
  449. break;
  450. }
  451. if (req->ns->bdev)
  452. nvmet_bdev_set_limits(req->ns->bdev, id);
  453. /*
  454. * We just provide a single LBA format that matches what the
  455. * underlying device reports.
  456. */
  457. id->nlbaf = 0;
  458. id->flbas = 0;
  459. /*
  460. * Our namespace might always be shared. Not just with other
  461. * controllers, but also with any other user of the block device.
  462. */
  463. id->nmic = NVME_NS_NMIC_SHARED;
  464. id->anagrpid = cpu_to_le32(req->ns->anagrpid);
  465. memcpy(&id->nguid, &req->ns->nguid, sizeof(id->nguid));
  466. id->lbaf[0].ds = req->ns->blksize_shift;
  467. if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns)) {
  468. id->dpc = NVME_NS_DPC_PI_FIRST | NVME_NS_DPC_PI_LAST |
  469. NVME_NS_DPC_PI_TYPE1 | NVME_NS_DPC_PI_TYPE2 |
  470. NVME_NS_DPC_PI_TYPE3;
  471. id->mc = NVME_MC_EXTENDED_LBA;
  472. id->dps = req->ns->pi_type;
  473. id->flbas = NVME_NS_FLBAS_META_EXT;
  474. id->lbaf[0].ms = cpu_to_le16(req->ns->metadata_size);
  475. }
  476. if (req->ns->readonly)
  477. id->nsattr |= (1 << 0);
  478. done:
  479. if (!status)
  480. status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
  481. kfree(id);
  482. out:
  483. nvmet_req_complete(req, status);
  484. }
  485. static void nvmet_execute_identify_nslist(struct nvmet_req *req)
  486. {
  487. static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
  488. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  489. struct nvmet_ns *ns;
  490. unsigned long idx;
  491. u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
  492. __le32 *list;
  493. u16 status = 0;
  494. int i = 0;
  495. list = kzalloc(buf_size, GFP_KERNEL);
  496. if (!list) {
  497. status = NVME_SC_INTERNAL;
  498. goto out;
  499. }
  500. xa_for_each(&ctrl->subsys->namespaces, idx, ns) {
  501. if (ns->nsid <= min_nsid)
  502. continue;
  503. list[i++] = cpu_to_le32(ns->nsid);
  504. if (i == buf_size / sizeof(__le32))
  505. break;
  506. }
  507. status = nvmet_copy_to_sgl(req, 0, list, buf_size);
  508. kfree(list);
  509. out:
  510. nvmet_req_complete(req, status);
  511. }
  512. static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len,
  513. void *id, off_t *off)
  514. {
  515. struct nvme_ns_id_desc desc = {
  516. .nidt = type,
  517. .nidl = len,
  518. };
  519. u16 status;
  520. status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc));
  521. if (status)
  522. return status;
  523. *off += sizeof(desc);
  524. status = nvmet_copy_to_sgl(req, *off, id, len);
  525. if (status)
  526. return status;
  527. *off += len;
  528. return 0;
  529. }
  530. static void nvmet_execute_identify_desclist(struct nvmet_req *req)
  531. {
  532. off_t off = 0;
  533. u16 status;
  534. status = nvmet_req_find_ns(req);
  535. if (status)
  536. goto out;
  537. if (memchr_inv(&req->ns->uuid, 0, sizeof(req->ns->uuid))) {
  538. status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
  539. NVME_NIDT_UUID_LEN,
  540. &req->ns->uuid, &off);
  541. if (status)
  542. goto out;
  543. }
  544. if (memchr_inv(req->ns->nguid, 0, sizeof(req->ns->nguid))) {
  545. status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
  546. NVME_NIDT_NGUID_LEN,
  547. &req->ns->nguid, &off);
  548. if (status)
  549. goto out;
  550. }
  551. status = nvmet_copy_ns_identifier(req, NVME_NIDT_CSI,
  552. NVME_NIDT_CSI_LEN,
  553. &req->ns->csi, &off);
  554. if (status)
  555. goto out;
  556. if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off,
  557. off) != NVME_IDENTIFY_DATA_SIZE - off)
  558. status = NVME_SC_INTERNAL | NVME_SC_DNR;
  559. out:
  560. nvmet_req_complete(req, status);
  561. }
  562. static bool nvmet_handle_identify_desclist(struct nvmet_req *req)
  563. {
  564. switch (req->cmd->identify.csi) {
  565. case NVME_CSI_NVM:
  566. nvmet_execute_identify_desclist(req);
  567. return true;
  568. case NVME_CSI_ZNS:
  569. if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
  570. nvmet_execute_identify_desclist(req);
  571. return true;
  572. }
  573. return false;
  574. default:
  575. return false;
  576. }
  577. }
  578. static void nvmet_execute_identify_ctrl_nvm(struct nvmet_req *req)
  579. {
  580. /* Not supported: return zeroes */
  581. nvmet_req_complete(req,
  582. nvmet_zero_sgl(req, 0, sizeof(struct nvme_id_ctrl_nvm)));
  583. }
  584. static void nvmet_execute_identify(struct nvmet_req *req)
  585. {
  586. if (!nvmet_check_transfer_len(req, NVME_IDENTIFY_DATA_SIZE))
  587. return;
  588. switch (req->cmd->identify.cns) {
  589. case NVME_ID_CNS_NS:
  590. nvmet_execute_identify_ns(req);
  591. return;
  592. case NVME_ID_CNS_CS_NS:
  593. if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
  594. switch (req->cmd->identify.csi) {
  595. case NVME_CSI_ZNS:
  596. return nvmet_execute_identify_cns_cs_ns(req);
  597. default:
  598. break;
  599. }
  600. }
  601. break;
  602. case NVME_ID_CNS_CTRL:
  603. nvmet_execute_identify_ctrl(req);
  604. return;
  605. case NVME_ID_CNS_CS_CTRL:
  606. switch (req->cmd->identify.csi) {
  607. case NVME_CSI_NVM:
  608. nvmet_execute_identify_ctrl_nvm(req);
  609. return;
  610. case NVME_CSI_ZNS:
  611. if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
  612. nvmet_execute_identify_ctrl_zns(req);
  613. return;
  614. }
  615. break;
  616. }
  617. break;
  618. case NVME_ID_CNS_NS_ACTIVE_LIST:
  619. nvmet_execute_identify_nslist(req);
  620. return;
  621. case NVME_ID_CNS_NS_DESC_LIST:
  622. if (nvmet_handle_identify_desclist(req) == true)
  623. return;
  624. break;
  625. }
  626. nvmet_req_cns_error_complete(req);
  627. }
  628. /*
  629. * A "minimum viable" abort implementation: the command is mandatory in the
  630. * spec, but we are not required to do any useful work. We couldn't really
  631. * do a useful abort, so don't bother even with waiting for the command
  632. * to be exectuted and return immediately telling the command to abort
  633. * wasn't found.
  634. */
  635. static void nvmet_execute_abort(struct nvmet_req *req)
  636. {
  637. if (!nvmet_check_transfer_len(req, 0))
  638. return;
  639. nvmet_set_result(req, 1);
  640. nvmet_req_complete(req, 0);
  641. }
  642. static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req)
  643. {
  644. u16 status;
  645. if (req->ns->file)
  646. status = nvmet_file_flush(req);
  647. else
  648. status = nvmet_bdev_flush(req);
  649. if (status)
  650. pr_err("write protect flush failed nsid: %u\n", req->ns->nsid);
  651. return status;
  652. }
  653. static u16 nvmet_set_feat_write_protect(struct nvmet_req *req)
  654. {
  655. u32 write_protect = le32_to_cpu(req->cmd->common.cdw11);
  656. struct nvmet_subsys *subsys = nvmet_req_subsys(req);
  657. u16 status;
  658. status = nvmet_req_find_ns(req);
  659. if (status)
  660. return status;
  661. mutex_lock(&subsys->lock);
  662. switch (write_protect) {
  663. case NVME_NS_WRITE_PROTECT:
  664. req->ns->readonly = true;
  665. status = nvmet_write_protect_flush_sync(req);
  666. if (status)
  667. req->ns->readonly = false;
  668. break;
  669. case NVME_NS_NO_WRITE_PROTECT:
  670. req->ns->readonly = false;
  671. status = 0;
  672. break;
  673. default:
  674. break;
  675. }
  676. if (!status)
  677. nvmet_ns_changed(subsys, req->ns->nsid);
  678. mutex_unlock(&subsys->lock);
  679. return status;
  680. }
  681. u16 nvmet_set_feat_kato(struct nvmet_req *req)
  682. {
  683. u32 val32 = le32_to_cpu(req->cmd->common.cdw11);
  684. nvmet_stop_keep_alive_timer(req->sq->ctrl);
  685. req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
  686. nvmet_start_keep_alive_timer(req->sq->ctrl);
  687. nvmet_set_result(req, req->sq->ctrl->kato);
  688. return 0;
  689. }
  690. u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask)
  691. {
  692. u32 val32 = le32_to_cpu(req->cmd->common.cdw11);
  693. if (val32 & ~mask) {
  694. req->error_loc = offsetof(struct nvme_common_command, cdw11);
  695. return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  696. }
  697. WRITE_ONCE(req->sq->ctrl->aen_enabled, val32);
  698. nvmet_set_result(req, val32);
  699. return 0;
  700. }
  701. void nvmet_execute_set_features(struct nvmet_req *req)
  702. {
  703. struct nvmet_subsys *subsys = nvmet_req_subsys(req);
  704. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
  705. u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
  706. u16 status = 0;
  707. u16 nsqr;
  708. u16 ncqr;
  709. if (!nvmet_check_transfer_len(req, 0))
  710. return;
  711. switch (cdw10 & 0xff) {
  712. case NVME_FEAT_NUM_QUEUES:
  713. ncqr = (cdw11 >> 16) & 0xffff;
  714. nsqr = cdw11 & 0xffff;
  715. if (ncqr == 0xffff || nsqr == 0xffff) {
  716. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  717. break;
  718. }
  719. nvmet_set_result(req,
  720. (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
  721. break;
  722. case NVME_FEAT_KATO:
  723. status = nvmet_set_feat_kato(req);
  724. break;
  725. case NVME_FEAT_ASYNC_EVENT:
  726. status = nvmet_set_feat_async_event(req, NVMET_AEN_CFG_ALL);
  727. break;
  728. case NVME_FEAT_HOST_ID:
  729. status = NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  730. break;
  731. case NVME_FEAT_WRITE_PROTECT:
  732. status = nvmet_set_feat_write_protect(req);
  733. break;
  734. default:
  735. req->error_loc = offsetof(struct nvme_common_command, cdw10);
  736. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  737. break;
  738. }
  739. nvmet_req_complete(req, status);
  740. }
  741. static u16 nvmet_get_feat_write_protect(struct nvmet_req *req)
  742. {
  743. struct nvmet_subsys *subsys = nvmet_req_subsys(req);
  744. u32 result;
  745. result = nvmet_req_find_ns(req);
  746. if (result)
  747. return result;
  748. mutex_lock(&subsys->lock);
  749. if (req->ns->readonly == true)
  750. result = NVME_NS_WRITE_PROTECT;
  751. else
  752. result = NVME_NS_NO_WRITE_PROTECT;
  753. nvmet_set_result(req, result);
  754. mutex_unlock(&subsys->lock);
  755. return 0;
  756. }
  757. void nvmet_get_feat_kato(struct nvmet_req *req)
  758. {
  759. nvmet_set_result(req, req->sq->ctrl->kato * 1000);
  760. }
  761. void nvmet_get_feat_async_event(struct nvmet_req *req)
  762. {
  763. nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled));
  764. }
  765. void nvmet_execute_get_features(struct nvmet_req *req)
  766. {
  767. struct nvmet_subsys *subsys = nvmet_req_subsys(req);
  768. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
  769. u16 status = 0;
  770. if (!nvmet_check_transfer_len(req, nvmet_feat_data_len(req, cdw10)))
  771. return;
  772. switch (cdw10 & 0xff) {
  773. /*
  774. * These features are mandatory in the spec, but we don't
  775. * have a useful way to implement them. We'll eventually
  776. * need to come up with some fake values for these.
  777. */
  778. #if 0
  779. case NVME_FEAT_ARBITRATION:
  780. break;
  781. case NVME_FEAT_POWER_MGMT:
  782. break;
  783. case NVME_FEAT_TEMP_THRESH:
  784. break;
  785. case NVME_FEAT_ERR_RECOVERY:
  786. break;
  787. case NVME_FEAT_IRQ_COALESCE:
  788. break;
  789. case NVME_FEAT_IRQ_CONFIG:
  790. break;
  791. case NVME_FEAT_WRITE_ATOMIC:
  792. break;
  793. #endif
  794. case NVME_FEAT_ASYNC_EVENT:
  795. nvmet_get_feat_async_event(req);
  796. break;
  797. case NVME_FEAT_VOLATILE_WC:
  798. nvmet_set_result(req, 1);
  799. break;
  800. case NVME_FEAT_NUM_QUEUES:
  801. nvmet_set_result(req,
  802. (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
  803. break;
  804. case NVME_FEAT_KATO:
  805. nvmet_get_feat_kato(req);
  806. break;
  807. case NVME_FEAT_HOST_ID:
  808. /* need 128-bit host identifier flag */
  809. if (!(req->cmd->common.cdw11 & cpu_to_le32(1 << 0))) {
  810. req->error_loc =
  811. offsetof(struct nvme_common_command, cdw11);
  812. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  813. break;
  814. }
  815. status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid,
  816. sizeof(req->sq->ctrl->hostid));
  817. break;
  818. case NVME_FEAT_WRITE_PROTECT:
  819. status = nvmet_get_feat_write_protect(req);
  820. break;
  821. default:
  822. req->error_loc =
  823. offsetof(struct nvme_common_command, cdw10);
  824. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  825. break;
  826. }
  827. nvmet_req_complete(req, status);
  828. }
  829. void nvmet_execute_async_event(struct nvmet_req *req)
  830. {
  831. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  832. if (!nvmet_check_transfer_len(req, 0))
  833. return;
  834. mutex_lock(&ctrl->lock);
  835. if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
  836. mutex_unlock(&ctrl->lock);
  837. nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR);
  838. return;
  839. }
  840. ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
  841. mutex_unlock(&ctrl->lock);
  842. queue_work(nvmet_wq, &ctrl->async_event_work);
  843. }
  844. void nvmet_execute_keep_alive(struct nvmet_req *req)
  845. {
  846. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  847. u16 status = 0;
  848. if (!nvmet_check_transfer_len(req, 0))
  849. return;
  850. if (!ctrl->kato) {
  851. status = NVME_SC_KA_TIMEOUT_INVALID;
  852. goto out;
  853. }
  854. pr_debug("ctrl %d update keep-alive timer for %d secs\n",
  855. ctrl->cntlid, ctrl->kato);
  856. mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
  857. out:
  858. nvmet_req_complete(req, status);
  859. }
  860. u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
  861. {
  862. struct nvme_command *cmd = req->cmd;
  863. u16 ret;
  864. if (nvme_is_fabrics(cmd))
  865. return nvmet_parse_fabrics_admin_cmd(req);
  866. if (unlikely(!nvmet_check_auth_status(req)))
  867. return NVME_SC_AUTH_REQUIRED | NVME_SC_DNR;
  868. if (nvmet_is_disc_subsys(nvmet_req_subsys(req)))
  869. return nvmet_parse_discovery_cmd(req);
  870. ret = nvmet_check_ctrl_status(req);
  871. if (unlikely(ret))
  872. return ret;
  873. if (nvmet_is_passthru_req(req))
  874. return nvmet_parse_passthru_admin_cmd(req);
  875. switch (cmd->common.opcode) {
  876. case nvme_admin_get_log_page:
  877. req->execute = nvmet_execute_get_log_page;
  878. return 0;
  879. case nvme_admin_identify:
  880. req->execute = nvmet_execute_identify;
  881. return 0;
  882. case nvme_admin_abort_cmd:
  883. req->execute = nvmet_execute_abort;
  884. return 0;
  885. case nvme_admin_set_features:
  886. req->execute = nvmet_execute_set_features;
  887. return 0;
  888. case nvme_admin_get_features:
  889. req->execute = nvmet_execute_get_features;
  890. return 0;
  891. case nvme_admin_async_event:
  892. req->execute = nvmet_execute_async_event;
  893. return 0;
  894. case nvme_admin_keep_alive:
  895. req->execute = nvmet_execute_keep_alive;
  896. return 0;
  897. default:
  898. return nvmet_report_invalid_opcode(req);
  899. }
  900. }