zns.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Western Digital Corporation or its affiliates.
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/vmalloc.h>
  7. #include "nvme.h"
  8. int nvme_revalidate_zones(struct nvme_ns *ns)
  9. {
  10. struct request_queue *q = ns->queue;
  11. int ret;
  12. ret = blk_revalidate_disk_zones(ns->disk, NULL);
  13. if (!ret)
  14. blk_queue_max_zone_append_sectors(q, ns->ctrl->max_zone_append);
  15. return ret;
  16. }
  17. static int nvme_set_max_append(struct nvme_ctrl *ctrl)
  18. {
  19. struct nvme_command c = { };
  20. struct nvme_id_ctrl_zns *id;
  21. int status;
  22. id = kzalloc(sizeof(*id), GFP_KERNEL);
  23. if (!id)
  24. return -ENOMEM;
  25. c.identify.opcode = nvme_admin_identify;
  26. c.identify.cns = NVME_ID_CNS_CS_CTRL;
  27. c.identify.csi = NVME_CSI_ZNS;
  28. status = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
  29. if (status) {
  30. kfree(id);
  31. return status;
  32. }
  33. if (id->zasl)
  34. ctrl->max_zone_append = 1 << (id->zasl + 3);
  35. else
  36. ctrl->max_zone_append = ctrl->max_hw_sectors;
  37. kfree(id);
  38. return 0;
  39. }
  40. int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
  41. {
  42. struct nvme_effects_log *log = ns->head->effects;
  43. struct request_queue *q = ns->queue;
  44. struct nvme_command c = { };
  45. struct nvme_id_ns_zns *id;
  46. int status;
  47. /* Driver requires zone append support */
  48. if ((le32_to_cpu(log->iocs[nvme_cmd_zone_append]) &
  49. NVME_CMD_EFFECTS_CSUPP)) {
  50. if (test_and_clear_bit(NVME_NS_FORCE_RO, &ns->flags))
  51. dev_warn(ns->ctrl->device,
  52. "Zone Append supported for zoned namespace:%d. Remove read-only mode\n",
  53. ns->head->ns_id);
  54. } else {
  55. set_bit(NVME_NS_FORCE_RO, &ns->flags);
  56. dev_warn(ns->ctrl->device,
  57. "Zone Append not supported for zoned namespace:%d. Forcing to read-only mode\n",
  58. ns->head->ns_id);
  59. }
  60. /* Lazily query controller append limit for the first zoned namespace */
  61. if (!ns->ctrl->max_zone_append) {
  62. status = nvme_set_max_append(ns->ctrl);
  63. if (status)
  64. return status;
  65. }
  66. id = kzalloc(sizeof(*id), GFP_KERNEL);
  67. if (!id)
  68. return -ENOMEM;
  69. c.identify.opcode = nvme_admin_identify;
  70. c.identify.nsid = cpu_to_le32(ns->head->ns_id);
  71. c.identify.cns = NVME_ID_CNS_CS_NS;
  72. c.identify.csi = NVME_CSI_ZNS;
  73. status = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, id, sizeof(*id));
  74. if (status)
  75. goto free_data;
  76. /*
  77. * We currently do not handle devices requiring any of the zoned
  78. * operation characteristics.
  79. */
  80. if (id->zoc) {
  81. dev_warn(ns->ctrl->device,
  82. "zone operations:%x not supported for namespace:%u\n",
  83. le16_to_cpu(id->zoc), ns->head->ns_id);
  84. status = -ENODEV;
  85. goto free_data;
  86. }
  87. ns->zsze = nvme_lba_to_sect(ns, le64_to_cpu(id->lbafe[lbaf].zsze));
  88. if (!is_power_of_2(ns->zsze)) {
  89. dev_warn(ns->ctrl->device,
  90. "invalid zone size:%llu for namespace:%u\n",
  91. ns->zsze, ns->head->ns_id);
  92. status = -ENODEV;
  93. goto free_data;
  94. }
  95. disk_set_zoned(ns->disk, BLK_ZONED_HM);
  96. blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
  97. disk_set_max_open_zones(ns->disk, le32_to_cpu(id->mor) + 1);
  98. disk_set_max_active_zones(ns->disk, le32_to_cpu(id->mar) + 1);
  99. free_data:
  100. kfree(id);
  101. return status;
  102. }
  103. static void *nvme_zns_alloc_report_buffer(struct nvme_ns *ns,
  104. unsigned int nr_zones, size_t *buflen)
  105. {
  106. struct request_queue *q = ns->disk->queue;
  107. size_t bufsize;
  108. void *buf;
  109. const size_t min_bufsize = sizeof(struct nvme_zone_report) +
  110. sizeof(struct nvme_zone_descriptor);
  111. nr_zones = min_t(unsigned int, nr_zones,
  112. get_capacity(ns->disk) >> ilog2(ns->zsze));
  113. bufsize = sizeof(struct nvme_zone_report) +
  114. nr_zones * sizeof(struct nvme_zone_descriptor);
  115. bufsize = min_t(size_t, bufsize,
  116. queue_max_hw_sectors(q) << SECTOR_SHIFT);
  117. bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
  118. while (bufsize >= min_bufsize) {
  119. buf = __vmalloc(bufsize, GFP_KERNEL | __GFP_NORETRY);
  120. if (buf) {
  121. *buflen = bufsize;
  122. return buf;
  123. }
  124. bufsize >>= 1;
  125. }
  126. return NULL;
  127. }
  128. static int nvme_zone_parse_entry(struct nvme_ns *ns,
  129. struct nvme_zone_descriptor *entry,
  130. unsigned int idx, report_zones_cb cb,
  131. void *data)
  132. {
  133. struct blk_zone zone = { };
  134. if ((entry->zt & 0xf) != NVME_ZONE_TYPE_SEQWRITE_REQ) {
  135. dev_err(ns->ctrl->device, "invalid zone type %#x\n",
  136. entry->zt);
  137. return -EINVAL;
  138. }
  139. zone.type = BLK_ZONE_TYPE_SEQWRITE_REQ;
  140. zone.cond = entry->zs >> 4;
  141. zone.len = ns->zsze;
  142. zone.capacity = nvme_lba_to_sect(ns, le64_to_cpu(entry->zcap));
  143. zone.start = nvme_lba_to_sect(ns, le64_to_cpu(entry->zslba));
  144. if (zone.cond == BLK_ZONE_COND_FULL)
  145. zone.wp = zone.start + zone.len;
  146. else
  147. zone.wp = nvme_lba_to_sect(ns, le64_to_cpu(entry->wp));
  148. return cb(&zone, idx, data);
  149. }
  150. int nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
  151. unsigned int nr_zones, report_zones_cb cb, void *data)
  152. {
  153. struct nvme_zone_report *report;
  154. struct nvme_command c = { };
  155. int ret, zone_idx = 0;
  156. unsigned int nz, i;
  157. size_t buflen;
  158. if (ns->head->ids.csi != NVME_CSI_ZNS)
  159. return -EINVAL;
  160. report = nvme_zns_alloc_report_buffer(ns, nr_zones, &buflen);
  161. if (!report)
  162. return -ENOMEM;
  163. c.zmr.opcode = nvme_cmd_zone_mgmt_recv;
  164. c.zmr.nsid = cpu_to_le32(ns->head->ns_id);
  165. c.zmr.numd = cpu_to_le32(nvme_bytes_to_numd(buflen));
  166. c.zmr.zra = NVME_ZRA_ZONE_REPORT;
  167. c.zmr.zrasf = NVME_ZRASF_ZONE_REPORT_ALL;
  168. c.zmr.pr = NVME_REPORT_ZONE_PARTIAL;
  169. sector &= ~(ns->zsze - 1);
  170. while (zone_idx < nr_zones && sector < get_capacity(ns->disk)) {
  171. memset(report, 0, buflen);
  172. c.zmr.slba = cpu_to_le64(nvme_sect_to_lba(ns, sector));
  173. ret = nvme_submit_sync_cmd(ns->queue, &c, report, buflen);
  174. if (ret) {
  175. if (ret > 0)
  176. ret = -EIO;
  177. goto out_free;
  178. }
  179. nz = min((unsigned int)le64_to_cpu(report->nr_zones), nr_zones);
  180. if (!nz)
  181. break;
  182. for (i = 0; i < nz && zone_idx < nr_zones; i++) {
  183. ret = nvme_zone_parse_entry(ns, &report->entries[i],
  184. zone_idx, cb, data);
  185. if (ret)
  186. goto out_free;
  187. zone_idx++;
  188. }
  189. sector += ns->zsze * nz;
  190. }
  191. if (zone_idx > 0)
  192. ret = zone_idx;
  193. else
  194. ret = -EINVAL;
  195. out_free:
  196. kvfree(report);
  197. return ret;
  198. }
  199. blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns, struct request *req,
  200. struct nvme_command *c, enum nvme_zone_mgmt_action action)
  201. {
  202. memset(c, 0, sizeof(*c));
  203. c->zms.opcode = nvme_cmd_zone_mgmt_send;
  204. c->zms.nsid = cpu_to_le32(ns->head->ns_id);
  205. c->zms.slba = cpu_to_le64(nvme_sect_to_lba(ns, blk_rq_pos(req)));
  206. c->zms.zsa = action;
  207. if (req_op(req) == REQ_OP_ZONE_RESET_ALL)
  208. c->zms.select_all = 1;
  209. return BLK_STS_OK;
  210. }