pr.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015 Intel Corporation
  4. * Keith Busch <kbusch@kernel.org>
  5. */
  6. #include <linux/blkdev.h>
  7. #include <linux/pr.h>
  8. #include <linux/unaligned.h>
  9. #include "nvme.h"
  10. static enum nvme_pr_type nvme_pr_type_from_blk(enum pr_type type)
  11. {
  12. switch (type) {
  13. case PR_WRITE_EXCLUSIVE:
  14. return NVME_PR_WRITE_EXCLUSIVE;
  15. case PR_EXCLUSIVE_ACCESS:
  16. return NVME_PR_EXCLUSIVE_ACCESS;
  17. case PR_WRITE_EXCLUSIVE_REG_ONLY:
  18. return NVME_PR_WRITE_EXCLUSIVE_REG_ONLY;
  19. case PR_EXCLUSIVE_ACCESS_REG_ONLY:
  20. return NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY;
  21. case PR_WRITE_EXCLUSIVE_ALL_REGS:
  22. return NVME_PR_WRITE_EXCLUSIVE_ALL_REGS;
  23. case PR_EXCLUSIVE_ACCESS_ALL_REGS:
  24. return NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS;
  25. }
  26. return 0;
  27. }
  28. static enum pr_type block_pr_type_from_nvme(enum nvme_pr_type type)
  29. {
  30. switch (type) {
  31. case NVME_PR_WRITE_EXCLUSIVE:
  32. return PR_WRITE_EXCLUSIVE;
  33. case NVME_PR_EXCLUSIVE_ACCESS:
  34. return PR_EXCLUSIVE_ACCESS;
  35. case NVME_PR_WRITE_EXCLUSIVE_REG_ONLY:
  36. return PR_WRITE_EXCLUSIVE_REG_ONLY;
  37. case NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY:
  38. return PR_EXCLUSIVE_ACCESS_REG_ONLY;
  39. case NVME_PR_WRITE_EXCLUSIVE_ALL_REGS:
  40. return PR_WRITE_EXCLUSIVE_ALL_REGS;
  41. case NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS:
  42. return PR_EXCLUSIVE_ACCESS_ALL_REGS;
  43. }
  44. return 0;
  45. }
  46. static int nvme_send_ns_head_pr_command(struct block_device *bdev,
  47. struct nvme_command *c, void *data, unsigned int data_len)
  48. {
  49. struct nvme_ns_head *head = bdev->bd_disk->private_data;
  50. int srcu_idx = srcu_read_lock(&head->srcu);
  51. struct nvme_ns *ns = nvme_find_path(head);
  52. int ret = -EWOULDBLOCK;
  53. if (ns) {
  54. c->common.nsid = cpu_to_le32(ns->head->ns_id);
  55. ret = nvme_submit_sync_cmd(ns->queue, c, data, data_len);
  56. }
  57. srcu_read_unlock(&head->srcu, srcu_idx);
  58. return ret;
  59. }
  60. static int nvme_send_ns_pr_command(struct nvme_ns *ns, struct nvme_command *c,
  61. void *data, unsigned int data_len)
  62. {
  63. c->common.nsid = cpu_to_le32(ns->head->ns_id);
  64. return nvme_submit_sync_cmd(ns->queue, c, data, data_len);
  65. }
  66. static int nvme_status_to_pr_err(int status)
  67. {
  68. if (nvme_is_path_error(status))
  69. return PR_STS_PATH_FAILED;
  70. switch (status & NVME_SCT_SC_MASK) {
  71. case NVME_SC_SUCCESS:
  72. return PR_STS_SUCCESS;
  73. case NVME_SC_RESERVATION_CONFLICT:
  74. return PR_STS_RESERVATION_CONFLICT;
  75. case NVME_SC_BAD_ATTRIBUTES:
  76. case NVME_SC_INVALID_OPCODE:
  77. case NVME_SC_INVALID_FIELD:
  78. case NVME_SC_INVALID_NS:
  79. return -EINVAL;
  80. default:
  81. return PR_STS_IOERR;
  82. }
  83. }
  84. static int nvme_send_pr_command(struct block_device *bdev,
  85. struct nvme_command *c, void *data, unsigned int data_len)
  86. {
  87. if (nvme_disk_is_ns_head(bdev->bd_disk))
  88. return nvme_send_ns_head_pr_command(bdev, c, data, data_len);
  89. return nvme_send_ns_pr_command(bdev->bd_disk->private_data, c, data,
  90. data_len);
  91. }
  92. static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
  93. u64 key, u64 sa_key, u8 op)
  94. {
  95. struct nvme_command c = { };
  96. u8 data[16] = { 0, };
  97. int ret;
  98. put_unaligned_le64(key, &data[0]);
  99. put_unaligned_le64(sa_key, &data[8]);
  100. c.common.opcode = op;
  101. c.common.cdw10 = cpu_to_le32(cdw10);
  102. ret = nvme_send_pr_command(bdev, &c, data, sizeof(data));
  103. if (ret < 0)
  104. return ret;
  105. return nvme_status_to_pr_err(ret);
  106. }
  107. static int nvme_pr_register(struct block_device *bdev, u64 old,
  108. u64 new, unsigned flags)
  109. {
  110. u32 cdw10;
  111. if (flags & ~PR_FL_IGNORE_KEY)
  112. return -EOPNOTSUPP;
  113. cdw10 = old ? 2 : 0;
  114. cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
  115. cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
  116. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
  117. }
  118. static int nvme_pr_reserve(struct block_device *bdev, u64 key,
  119. enum pr_type type, unsigned flags)
  120. {
  121. u32 cdw10;
  122. if (flags & ~PR_FL_IGNORE_KEY)
  123. return -EOPNOTSUPP;
  124. cdw10 = nvme_pr_type_from_blk(type) << 8;
  125. cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
  126. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
  127. }
  128. static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
  129. enum pr_type type, bool abort)
  130. {
  131. u32 cdw10 = nvme_pr_type_from_blk(type) << 8 | (abort ? 2 : 1);
  132. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
  133. }
  134. static int nvme_pr_clear(struct block_device *bdev, u64 key)
  135. {
  136. u32 cdw10 = 1 | (key ? 0 : 1 << 3);
  137. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
  138. }
  139. static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  140. {
  141. u32 cdw10 = nvme_pr_type_from_blk(type) << 8 | (key ? 0 : 1 << 3);
  142. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
  143. }
  144. static int nvme_pr_resv_report(struct block_device *bdev, void *data,
  145. u32 data_len, bool *eds)
  146. {
  147. struct nvme_command c = { };
  148. int ret;
  149. c.common.opcode = nvme_cmd_resv_report;
  150. c.common.cdw10 = cpu_to_le32(nvme_bytes_to_numd(data_len));
  151. c.common.cdw11 = cpu_to_le32(NVME_EXTENDED_DATA_STRUCT);
  152. *eds = true;
  153. retry:
  154. ret = nvme_send_pr_command(bdev, &c, data, data_len);
  155. if (ret == NVME_SC_HOST_ID_INCONSIST &&
  156. c.common.cdw11 == cpu_to_le32(NVME_EXTENDED_DATA_STRUCT)) {
  157. c.common.cdw11 = 0;
  158. *eds = false;
  159. goto retry;
  160. }
  161. if (ret < 0)
  162. return ret;
  163. return nvme_status_to_pr_err(ret);
  164. }
  165. static int nvme_pr_read_keys(struct block_device *bdev,
  166. struct pr_keys *keys_info)
  167. {
  168. u32 rse_len, num_keys = keys_info->num_keys;
  169. struct nvme_reservation_status_ext *rse;
  170. int ret, i;
  171. bool eds;
  172. /*
  173. * Assume we are using 128-bit host IDs and allocate a buffer large
  174. * enough to get enough keys to fill the return keys buffer.
  175. */
  176. rse_len = struct_size(rse, regctl_eds, num_keys);
  177. rse = kzalloc(rse_len, GFP_KERNEL);
  178. if (!rse)
  179. return -ENOMEM;
  180. ret = nvme_pr_resv_report(bdev, rse, rse_len, &eds);
  181. if (ret)
  182. goto free_rse;
  183. keys_info->generation = le32_to_cpu(rse->gen);
  184. keys_info->num_keys = get_unaligned_le16(&rse->regctl);
  185. num_keys = min(num_keys, keys_info->num_keys);
  186. for (i = 0; i < num_keys; i++) {
  187. if (eds) {
  188. keys_info->keys[i] =
  189. le64_to_cpu(rse->regctl_eds[i].rkey);
  190. } else {
  191. struct nvme_reservation_status *rs;
  192. rs = (struct nvme_reservation_status *)rse;
  193. keys_info->keys[i] = le64_to_cpu(rs->regctl_ds[i].rkey);
  194. }
  195. }
  196. free_rse:
  197. kfree(rse);
  198. return ret;
  199. }
  200. static int nvme_pr_read_reservation(struct block_device *bdev,
  201. struct pr_held_reservation *resv)
  202. {
  203. struct nvme_reservation_status_ext tmp_rse, *rse;
  204. int ret, i, num_regs;
  205. u32 rse_len;
  206. bool eds;
  207. get_num_regs:
  208. /*
  209. * Get the number of registrations so we know how big to allocate
  210. * the response buffer.
  211. */
  212. ret = nvme_pr_resv_report(bdev, &tmp_rse, sizeof(tmp_rse), &eds);
  213. if (ret)
  214. return ret;
  215. num_regs = get_unaligned_le16(&tmp_rse.regctl);
  216. if (!num_regs) {
  217. resv->generation = le32_to_cpu(tmp_rse.gen);
  218. return 0;
  219. }
  220. rse_len = struct_size(rse, regctl_eds, num_regs);
  221. rse = kzalloc(rse_len, GFP_KERNEL);
  222. if (!rse)
  223. return -ENOMEM;
  224. ret = nvme_pr_resv_report(bdev, rse, rse_len, &eds);
  225. if (ret)
  226. goto free_rse;
  227. if (num_regs != get_unaligned_le16(&rse->regctl)) {
  228. kfree(rse);
  229. goto get_num_regs;
  230. }
  231. resv->generation = le32_to_cpu(rse->gen);
  232. resv->type = block_pr_type_from_nvme(rse->rtype);
  233. for (i = 0; i < num_regs; i++) {
  234. if (eds) {
  235. if (rse->regctl_eds[i].rcsts) {
  236. resv->key = le64_to_cpu(rse->regctl_eds[i].rkey);
  237. break;
  238. }
  239. } else {
  240. struct nvme_reservation_status *rs;
  241. rs = (struct nvme_reservation_status *)rse;
  242. if (rs->regctl_ds[i].rcsts) {
  243. resv->key = le64_to_cpu(rs->regctl_ds[i].rkey);
  244. break;
  245. }
  246. }
  247. }
  248. free_rse:
  249. kfree(rse);
  250. return ret;
  251. }
  252. const struct pr_ops nvme_pr_ops = {
  253. .pr_register = nvme_pr_register,
  254. .pr_reserve = nvme_pr_reserve,
  255. .pr_release = nvme_pr_release,
  256. .pr_preempt = nvme_pr_preempt,
  257. .pr_clear = nvme_pr_clear,
  258. .pr_read_keys = nvme_pr_read_keys,
  259. .pr_read_reservation = nvme_pr_read_reservation,
  260. };