Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 1 | /* |
| 2 | * nvme-lightnvm.c - LightNVM NVMe device |
| 3 | * |
| 4 | * Copyright (C) 2014-2015 IT University of Copenhagen |
| 5 | * Initial release: Matias Bjorling <mb@lightnvm.io> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version |
| 9 | * 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; see the file COPYING. If not, write to |
| 18 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, |
| 19 | * USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "nvme.h" |
| 24 | |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 25 | #include <linux/nvme.h> |
| 26 | #include <linux/bitops.h> |
| 27 | #include <linux/lightnvm.h> |
| 28 | #include <linux/vmalloc.h> |
Matias Bjørling | 84d4add | 2017-01-31 13:17:16 +0100 | [diff] [blame] | 29 | #include <linux/sched/sysctl.h> |
| 30 | #include <uapi/linux/lightnvm.h> |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 31 | |
| 32 | enum nvme_nvm_admin_opcode { |
| 33 | nvme_nvm_admin_identity = 0xe2, |
| 34 | nvme_nvm_admin_get_l2p_tbl = 0xea, |
| 35 | nvme_nvm_admin_get_bb_tbl = 0xf2, |
| 36 | nvme_nvm_admin_set_bb_tbl = 0xf1, |
| 37 | }; |
| 38 | |
| 39 | struct nvme_nvm_hb_rw { |
| 40 | __u8 opcode; |
| 41 | __u8 flags; |
| 42 | __u16 command_id; |
| 43 | __le32 nsid; |
| 44 | __u64 rsvd2; |
| 45 | __le64 metadata; |
| 46 | __le64 prp1; |
| 47 | __le64 prp2; |
| 48 | __le64 spba; |
| 49 | __le16 length; |
| 50 | __le16 control; |
| 51 | __le32 dsmgmt; |
| 52 | __le64 slba; |
| 53 | }; |
| 54 | |
| 55 | struct nvme_nvm_ph_rw { |
| 56 | __u8 opcode; |
| 57 | __u8 flags; |
| 58 | __u16 command_id; |
| 59 | __le32 nsid; |
| 60 | __u64 rsvd2; |
| 61 | __le64 metadata; |
| 62 | __le64 prp1; |
| 63 | __le64 prp2; |
| 64 | __le64 spba; |
| 65 | __le16 length; |
| 66 | __le16 control; |
| 67 | __le32 dsmgmt; |
| 68 | __le64 resv; |
| 69 | }; |
| 70 | |
| 71 | struct nvme_nvm_identity { |
| 72 | __u8 opcode; |
| 73 | __u8 flags; |
| 74 | __u16 command_id; |
| 75 | __le32 nsid; |
| 76 | __u64 rsvd[2]; |
| 77 | __le64 prp1; |
| 78 | __le64 prp2; |
| 79 | __le32 chnl_off; |
| 80 | __u32 rsvd11[5]; |
| 81 | }; |
| 82 | |
| 83 | struct nvme_nvm_l2ptbl { |
| 84 | __u8 opcode; |
| 85 | __u8 flags; |
| 86 | __u16 command_id; |
| 87 | __le32 nsid; |
| 88 | __le32 cdw2[4]; |
| 89 | __le64 prp1; |
| 90 | __le64 prp2; |
| 91 | __le64 slba; |
| 92 | __le32 nlb; |
| 93 | __le16 cdw14[6]; |
| 94 | }; |
| 95 | |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 96 | struct nvme_nvm_getbbtbl { |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 97 | __u8 opcode; |
| 98 | __u8 flags; |
| 99 | __u16 command_id; |
| 100 | __le32 nsid; |
| 101 | __u64 rsvd[2]; |
| 102 | __le64 prp1; |
| 103 | __le64 prp2; |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 104 | __le64 spba; |
| 105 | __u32 rsvd4[4]; |
| 106 | }; |
| 107 | |
| 108 | struct nvme_nvm_setbbtbl { |
| 109 | __u8 opcode; |
| 110 | __u8 flags; |
| 111 | __u16 command_id; |
| 112 | __le32 nsid; |
| 113 | __le64 rsvd[2]; |
| 114 | __le64 prp1; |
| 115 | __le64 prp2; |
| 116 | __le64 spba; |
| 117 | __le16 nlb; |
| 118 | __u8 value; |
| 119 | __u8 rsvd3; |
| 120 | __u32 rsvd4[3]; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | struct nvme_nvm_erase_blk { |
| 124 | __u8 opcode; |
| 125 | __u8 flags; |
| 126 | __u16 command_id; |
| 127 | __le32 nsid; |
| 128 | __u64 rsvd[2]; |
| 129 | __le64 prp1; |
| 130 | __le64 prp2; |
| 131 | __le64 spba; |
| 132 | __le16 length; |
| 133 | __le16 control; |
| 134 | __le32 dsmgmt; |
| 135 | __le64 resv; |
| 136 | }; |
| 137 | |
| 138 | struct nvme_nvm_command { |
| 139 | union { |
| 140 | struct nvme_common_command common; |
| 141 | struct nvme_nvm_identity identity; |
| 142 | struct nvme_nvm_hb_rw hb_rw; |
| 143 | struct nvme_nvm_ph_rw ph_rw; |
| 144 | struct nvme_nvm_l2ptbl l2p; |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 145 | struct nvme_nvm_getbbtbl get_bb; |
| 146 | struct nvme_nvm_setbbtbl set_bb; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 147 | struct nvme_nvm_erase_blk erase; |
| 148 | }; |
| 149 | }; |
| 150 | |
Matias Bjørling | 6dde1d6 | 2016-02-04 15:13:26 +0100 | [diff] [blame] | 151 | #define NVME_NVM_LP_MLC_PAIRS 886 |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 152 | struct nvme_nvm_lp_mlc { |
Johannes Thumshirn | 6f92970 | 2016-07-07 09:54:10 +0200 | [diff] [blame] | 153 | __le16 num_pairs; |
Matias Bjørling | 6dde1d6 | 2016-02-04 15:13:26 +0100 | [diff] [blame] | 154 | __u8 pairs[NVME_NVM_LP_MLC_PAIRS]; |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | struct nvme_nvm_lp_tbl { |
| 158 | __u8 id[8]; |
| 159 | struct nvme_nvm_lp_mlc mlc; |
| 160 | }; |
| 161 | |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 162 | struct nvme_nvm_id_group { |
| 163 | __u8 mtype; |
| 164 | __u8 fmtype; |
| 165 | __le16 res16; |
| 166 | __u8 num_ch; |
| 167 | __u8 num_lun; |
| 168 | __u8 num_pln; |
Matias Bjørling | 36d5dbc | 2015-11-16 15:34:38 +0100 | [diff] [blame] | 169 | __u8 rsvd1; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 170 | __le16 num_blk; |
| 171 | __le16 num_pg; |
| 172 | __le16 fpg_sz; |
| 173 | __le16 csecs; |
| 174 | __le16 sos; |
Matias Bjørling | 36d5dbc | 2015-11-16 15:34:38 +0100 | [diff] [blame] | 175 | __le16 rsvd2; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 176 | __le32 trdt; |
| 177 | __le32 trdm; |
| 178 | __le32 tprt; |
| 179 | __le32 tprm; |
| 180 | __le32 tbet; |
| 181 | __le32 tbem; |
| 182 | __le32 mpos; |
Matias Bjørling | 12be5ed | 2015-11-16 15:34:39 +0100 | [diff] [blame] | 183 | __le32 mccap; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 184 | __le16 cpar; |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 185 | __u8 reserved[10]; |
| 186 | struct nvme_nvm_lp_tbl lptbl; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 187 | } __packed; |
| 188 | |
| 189 | struct nvme_nvm_addr_format { |
| 190 | __u8 ch_offset; |
| 191 | __u8 ch_len; |
| 192 | __u8 lun_offset; |
| 193 | __u8 lun_len; |
| 194 | __u8 pln_offset; |
| 195 | __u8 pln_len; |
| 196 | __u8 blk_offset; |
| 197 | __u8 blk_len; |
| 198 | __u8 pg_offset; |
| 199 | __u8 pg_len; |
| 200 | __u8 sect_offset; |
| 201 | __u8 sect_len; |
| 202 | __u8 res[4]; |
| 203 | } __packed; |
| 204 | |
| 205 | struct nvme_nvm_id { |
| 206 | __u8 ver_id; |
| 207 | __u8 vmnt; |
| 208 | __u8 cgrps; |
Matias Bjørling | dad1b00 | 2015-11-16 15:34:46 +0100 | [diff] [blame] | 209 | __u8 res; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 210 | __le32 cap; |
| 211 | __le32 dom; |
| 212 | struct nvme_nvm_addr_format ppaf; |
Matias Bjørling | dad1b00 | 2015-11-16 15:34:46 +0100 | [diff] [blame] | 213 | __u8 resv[228]; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 214 | struct nvme_nvm_id_group groups[4]; |
| 215 | } __packed; |
| 216 | |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 217 | struct nvme_nvm_bb_tbl { |
| 218 | __u8 tblid[4]; |
| 219 | __le16 verid; |
| 220 | __le16 revid; |
| 221 | __le32 rvsd1; |
| 222 | __le32 tblks; |
| 223 | __le32 tfact; |
| 224 | __le32 tgrown; |
| 225 | __le32 tdresv; |
| 226 | __le32 thresv; |
| 227 | __le32 rsvd2[8]; |
| 228 | __u8 blk[0]; |
| 229 | }; |
| 230 | |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 231 | /* |
| 232 | * Check we didn't inadvertently grow the command struct |
| 233 | */ |
| 234 | static inline void _nvme_nvm_check_size(void) |
| 235 | { |
| 236 | BUILD_BUG_ON(sizeof(struct nvme_nvm_identity) != 64); |
| 237 | BUILD_BUG_ON(sizeof(struct nvme_nvm_hb_rw) != 64); |
| 238 | BUILD_BUG_ON(sizeof(struct nvme_nvm_ph_rw) != 64); |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 239 | BUILD_BUG_ON(sizeof(struct nvme_nvm_getbbtbl) != 64); |
| 240 | BUILD_BUG_ON(sizeof(struct nvme_nvm_setbbtbl) != 64); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 241 | BUILD_BUG_ON(sizeof(struct nvme_nvm_l2ptbl) != 64); |
| 242 | BUILD_BUG_ON(sizeof(struct nvme_nvm_erase_blk) != 64); |
| 243 | BUILD_BUG_ON(sizeof(struct nvme_nvm_id_group) != 960); |
| 244 | BUILD_BUG_ON(sizeof(struct nvme_nvm_addr_format) != 128); |
| 245 | BUILD_BUG_ON(sizeof(struct nvme_nvm_id) != 4096); |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 246 | BUILD_BUG_ON(sizeof(struct nvme_nvm_bb_tbl) != 512); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static int init_grps(struct nvm_id *nvm_id, struct nvme_nvm_id *nvme_nvm_id) |
| 250 | { |
| 251 | struct nvme_nvm_id_group *src; |
| 252 | struct nvm_id_group *dst; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 253 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 254 | if (nvme_nvm_id->cgrps != 1) |
| 255 | return -EINVAL; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 256 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 257 | src = &nvme_nvm_id->groups[0]; |
| 258 | dst = &nvm_id->grp; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 259 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 260 | dst->mtype = src->mtype; |
| 261 | dst->fmtype = src->fmtype; |
| 262 | dst->num_ch = src->num_ch; |
| 263 | dst->num_lun = src->num_lun; |
| 264 | dst->num_pln = src->num_pln; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 265 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 266 | dst->num_pg = le16_to_cpu(src->num_pg); |
| 267 | dst->num_blk = le16_to_cpu(src->num_blk); |
| 268 | dst->fpg_sz = le16_to_cpu(src->fpg_sz); |
| 269 | dst->csecs = le16_to_cpu(src->csecs); |
| 270 | dst->sos = le16_to_cpu(src->sos); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 271 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 272 | dst->trdt = le32_to_cpu(src->trdt); |
| 273 | dst->trdm = le32_to_cpu(src->trdm); |
| 274 | dst->tprt = le32_to_cpu(src->tprt); |
| 275 | dst->tprm = le32_to_cpu(src->tprm); |
| 276 | dst->tbet = le32_to_cpu(src->tbet); |
| 277 | dst->tbem = le32_to_cpu(src->tbem); |
| 278 | dst->mpos = le32_to_cpu(src->mpos); |
| 279 | dst->mccap = le32_to_cpu(src->mccap); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 280 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 281 | dst->cpar = le16_to_cpu(src->cpar); |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 282 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 283 | if (dst->fmtype == NVM_ID_FMTYPE_MLC) { |
| 284 | memcpy(dst->lptbl.id, src->lptbl.id, 8); |
| 285 | dst->lptbl.mlc.num_pairs = |
| 286 | le16_to_cpu(src->lptbl.mlc.num_pairs); |
Matias Bjørling | 6dde1d6 | 2016-02-04 15:13:26 +0100 | [diff] [blame] | 287 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 288 | if (dst->lptbl.mlc.num_pairs > NVME_NVM_LP_MLC_PAIRS) { |
| 289 | pr_err("nvm: number of MLC pairs not supported\n"); |
| 290 | return -EINVAL; |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 291 | } |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 292 | |
| 293 | memcpy(dst->lptbl.mlc.pairs, src->lptbl.mlc.pairs, |
| 294 | dst->lptbl.mlc.num_pairs); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 300 | static int nvme_nvm_identity(struct nvm_dev *nvmdev, struct nvm_id *nvm_id) |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 301 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 302 | struct nvme_ns *ns = nvmdev->q->queuedata; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 303 | struct nvme_nvm_id *nvme_nvm_id; |
| 304 | struct nvme_nvm_command c = {}; |
| 305 | int ret; |
| 306 | |
| 307 | c.identity.opcode = nvme_nvm_admin_identity; |
| 308 | c.identity.nsid = cpu_to_le32(ns->ns_id); |
| 309 | c.identity.chnl_off = 0; |
| 310 | |
| 311 | nvme_nvm_id = kmalloc(sizeof(struct nvme_nvm_id), GFP_KERNEL); |
| 312 | if (!nvme_nvm_id) |
| 313 | return -ENOMEM; |
| 314 | |
Christoph Hellwig | ac02ddd | 2015-12-03 09:52:05 -0700 | [diff] [blame] | 315 | ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, (struct nvme_command *)&c, |
Wenwei Tao | 47b3115 | 2015-11-20 13:47:55 +0100 | [diff] [blame] | 316 | nvme_nvm_id, sizeof(struct nvme_nvm_id)); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 317 | if (ret) { |
| 318 | ret = -EIO; |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | nvm_id->ver_id = nvme_nvm_id->ver_id; |
| 323 | nvm_id->vmnt = nvme_nvm_id->vmnt; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 324 | nvm_id->cap = le32_to_cpu(nvme_nvm_id->cap); |
| 325 | nvm_id->dom = le32_to_cpu(nvme_nvm_id->dom); |
Matias Bjørling | 2393bd3 | 2015-11-16 15:34:45 +0100 | [diff] [blame] | 326 | memcpy(&nvm_id->ppaf, &nvme_nvm_id->ppaf, |
| 327 | sizeof(struct nvme_nvm_addr_format)); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 328 | |
| 329 | ret = init_grps(nvm_id, nvme_nvm_id); |
| 330 | out: |
| 331 | kfree(nvme_nvm_id); |
| 332 | return ret; |
| 333 | } |
| 334 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 335 | static int nvme_nvm_get_l2p_tbl(struct nvm_dev *nvmdev, u64 slba, u32 nlb, |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 336 | nvm_l2p_update_fn *update_l2p, void *priv) |
| 337 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 338 | struct nvme_ns *ns = nvmdev->q->queuedata; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 339 | struct nvme_nvm_command c = {}; |
Christoph Hellwig | ac02ddd | 2015-12-03 09:52:05 -0700 | [diff] [blame] | 340 | u32 len = queue_max_hw_sectors(ns->ctrl->admin_q) << 9; |
Dan Carpenter | 5f436e5 | 2015-11-04 01:37:31 +0300 | [diff] [blame] | 341 | u32 nlb_pr_rq = len / sizeof(u64); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 342 | u64 cmd_slba = slba; |
| 343 | void *entries; |
| 344 | int ret = 0; |
| 345 | |
| 346 | c.l2p.opcode = nvme_nvm_admin_get_l2p_tbl; |
| 347 | c.l2p.nsid = cpu_to_le32(ns->ns_id); |
| 348 | entries = kmalloc(len, GFP_KERNEL); |
| 349 | if (!entries) |
| 350 | return -ENOMEM; |
| 351 | |
| 352 | while (nlb) { |
Dan Carpenter | 5f436e5 | 2015-11-04 01:37:31 +0300 | [diff] [blame] | 353 | u32 cmd_nlb = min(nlb_pr_rq, nlb); |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 354 | u64 elba = slba + cmd_nlb; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 355 | |
| 356 | c.l2p.slba = cpu_to_le64(cmd_slba); |
| 357 | c.l2p.nlb = cpu_to_le32(cmd_nlb); |
| 358 | |
Christoph Hellwig | ac02ddd | 2015-12-03 09:52:05 -0700 | [diff] [blame] | 359 | ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, |
Wenwei Tao | 47b3115 | 2015-11-20 13:47:55 +0100 | [diff] [blame] | 360 | (struct nvme_command *)&c, entries, len); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 361 | if (ret) { |
Sagi Grimberg | b86d8d3 | 2016-05-06 20:03:14 +0200 | [diff] [blame] | 362 | dev_err(ns->ctrl->device, |
| 363 | "L2P table transfer failed (%d)\n", ret); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 364 | ret = -EIO; |
| 365 | goto out; |
| 366 | } |
| 367 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 368 | if (unlikely(elba > nvmdev->total_secs)) { |
| 369 | pr_err("nvm: L2P data from device is out of bounds!\n"); |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 373 | /* Transform physical address to target address space */ |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 374 | nvm_part_to_tgt(nvmdev, entries, cmd_nlb); |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 375 | |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 376 | if (update_l2p(cmd_slba, cmd_nlb, entries, priv)) { |
| 377 | ret = -EINTR; |
| 378 | goto out; |
| 379 | } |
| 380 | |
| 381 | cmd_slba += cmd_nlb; |
| 382 | nlb -= cmd_nlb; |
| 383 | } |
| 384 | |
| 385 | out: |
| 386 | kfree(entries); |
| 387 | return ret; |
| 388 | } |
| 389 | |
Matias Bjørling | 08236c6 | 2015-11-28 16:49:27 +0100 | [diff] [blame] | 390 | static int nvme_nvm_get_bb_tbl(struct nvm_dev *nvmdev, struct ppa_addr ppa, |
Matias Bjørling | e11903f | 2016-05-06 20:03:05 +0200 | [diff] [blame] | 391 | u8 *blks) |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 392 | { |
Matias Bjørling | 08236c6 | 2015-11-28 16:49:27 +0100 | [diff] [blame] | 393 | struct request_queue *q = nvmdev->q; |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 394 | struct nvm_geo *geo = &nvmdev->geo; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 395 | struct nvme_ns *ns = q->queuedata; |
Christoph Hellwig | ac02ddd | 2015-12-03 09:52:05 -0700 | [diff] [blame] | 396 | struct nvme_ctrl *ctrl = ns->ctrl; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 397 | struct nvme_nvm_command c = {}; |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 398 | struct nvme_nvm_bb_tbl *bb_tbl; |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 399 | int nr_blks = geo->blks_per_lun * geo->plane_mode; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 400 | int tblsz = sizeof(struct nvme_nvm_bb_tbl) + nr_blks; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 401 | int ret = 0; |
| 402 | |
| 403 | c.get_bb.opcode = nvme_nvm_admin_get_bb_tbl; |
| 404 | c.get_bb.nsid = cpu_to_le32(ns->ns_id); |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 405 | c.get_bb.spba = cpu_to_le64(ppa.ppa); |
| 406 | |
| 407 | bb_tbl = kzalloc(tblsz, GFP_KERNEL); |
| 408 | if (!bb_tbl) |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 409 | return -ENOMEM; |
| 410 | |
Christoph Hellwig | ac02ddd | 2015-12-03 09:52:05 -0700 | [diff] [blame] | 411 | ret = nvme_submit_sync_cmd(ctrl->admin_q, (struct nvme_command *)&c, |
Wenwei Tao | 47b3115 | 2015-11-20 13:47:55 +0100 | [diff] [blame] | 412 | bb_tbl, tblsz); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 413 | if (ret) { |
Sagi Grimberg | b86d8d3 | 2016-05-06 20:03:14 +0200 | [diff] [blame] | 414 | dev_err(ctrl->device, "get bad block table failed (%d)\n", ret); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 415 | ret = -EIO; |
| 416 | goto out; |
| 417 | } |
| 418 | |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 419 | if (bb_tbl->tblid[0] != 'B' || bb_tbl->tblid[1] != 'B' || |
| 420 | bb_tbl->tblid[2] != 'L' || bb_tbl->tblid[3] != 'T') { |
Sagi Grimberg | b86d8d3 | 2016-05-06 20:03:14 +0200 | [diff] [blame] | 421 | dev_err(ctrl->device, "bbt format mismatch\n"); |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 422 | ret = -EINVAL; |
| 423 | goto out; |
| 424 | } |
| 425 | |
| 426 | if (le16_to_cpu(bb_tbl->verid) != 1) { |
| 427 | ret = -EINVAL; |
Sagi Grimberg | b86d8d3 | 2016-05-06 20:03:14 +0200 | [diff] [blame] | 428 | dev_err(ctrl->device, "bbt version not supported\n"); |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 429 | goto out; |
| 430 | } |
| 431 | |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 432 | if (le32_to_cpu(bb_tbl->tblks) != nr_blks) { |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 433 | ret = -EINVAL; |
Sagi Grimberg | b86d8d3 | 2016-05-06 20:03:14 +0200 | [diff] [blame] | 434 | dev_err(ctrl->device, |
| 435 | "bbt unsuspected blocks returned (%u!=%u)", |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 436 | le32_to_cpu(bb_tbl->tblks), nr_blks); |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 437 | goto out; |
| 438 | } |
| 439 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 440 | memcpy(blks, bb_tbl->blk, geo->blks_per_lun * geo->plane_mode); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 441 | out: |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 442 | kfree(bb_tbl); |
| 443 | return ret; |
| 444 | } |
| 445 | |
Matias Bjørling | 00ee6cc | 2016-05-06 20:03:09 +0200 | [diff] [blame] | 446 | static int nvme_nvm_set_bb_tbl(struct nvm_dev *nvmdev, struct ppa_addr *ppas, |
| 447 | int nr_ppas, int type) |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 448 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 449 | struct nvme_ns *ns = nvmdev->q->queuedata; |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 450 | struct nvme_nvm_command c = {}; |
| 451 | int ret = 0; |
| 452 | |
| 453 | c.set_bb.opcode = nvme_nvm_admin_set_bb_tbl; |
| 454 | c.set_bb.nsid = cpu_to_le32(ns->ns_id); |
Matias Bjørling | 00ee6cc | 2016-05-06 20:03:09 +0200 | [diff] [blame] | 455 | c.set_bb.spba = cpu_to_le64(ppas->ppa); |
| 456 | c.set_bb.nlb = cpu_to_le16(nr_ppas - 1); |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 457 | c.set_bb.value = type; |
| 458 | |
Christoph Hellwig | ac02ddd | 2015-12-03 09:52:05 -0700 | [diff] [blame] | 459 | ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, (struct nvme_command *)&c, |
Wenwei Tao | 47b3115 | 2015-11-20 13:47:55 +0100 | [diff] [blame] | 460 | NULL, 0); |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 461 | if (ret) |
Sagi Grimberg | b86d8d3 | 2016-05-06 20:03:14 +0200 | [diff] [blame] | 462 | dev_err(ns->ctrl->device, "set bad block table failed (%d)\n", |
| 463 | ret); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | static inline void nvme_nvm_rqtocmd(struct request *rq, struct nvm_rq *rqd, |
| 468 | struct nvme_ns *ns, struct nvme_nvm_command *c) |
| 469 | { |
| 470 | c->ph_rw.opcode = rqd->opcode; |
| 471 | c->ph_rw.nsid = cpu_to_le32(ns->ns_id); |
| 472 | c->ph_rw.spba = cpu_to_le64(rqd->ppa_addr.ppa); |
Arnd Bergmann | 45bbd05 | 2016-05-06 20:03:16 +0200 | [diff] [blame] | 473 | c->ph_rw.metadata = cpu_to_le64(rqd->dma_meta_list); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 474 | c->ph_rw.control = cpu_to_le16(rqd->flags); |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 475 | c->ph_rw.length = cpu_to_le16(rqd->nr_ppas - 1); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 476 | |
| 477 | if (rqd->opcode == NVM_OP_HBWRITE || rqd->opcode == NVM_OP_HBREAD) |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 478 | c->hb_rw.slba = cpu_to_le64(nvme_block_nr(ns, |
| 479 | rqd->bio->bi_iter.bi_sector)); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static void nvme_nvm_end_io(struct request *rq, int error) |
| 483 | { |
| 484 | struct nvm_rq *rqd = rq->end_io_data; |
Matias Bjorling | 9f86726 | 2016-03-03 15:06:39 +0100 | [diff] [blame] | 485 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 486 | rqd->ppa_status = nvme_req(rq)->result.u64; |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 487 | rqd->error = error; |
| 488 | nvm_end_io(rqd); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 489 | |
Matias Bjørling | 7498e99 | 2016-11-28 22:38:52 +0100 | [diff] [blame] | 490 | kfree(nvme_req(rq)->cmd); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 491 | blk_mq_free_request(rq); |
| 492 | } |
| 493 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 494 | static int nvme_nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd) |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 495 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 496 | struct request_queue *q = dev->q; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 497 | struct nvme_ns *ns = q->queuedata; |
| 498 | struct request *rq; |
| 499 | struct bio *bio = rqd->bio; |
| 500 | struct nvme_nvm_command *cmd; |
| 501 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 502 | cmd = kzalloc(sizeof(struct nvme_nvm_command), GFP_KERNEL); |
| 503 | if (!cmd) |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 504 | return -ENOMEM; |
| 505 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 506 | rq = nvme_alloc_request(q, (struct nvme_command *)cmd, 0, NVME_QID_ANY); |
| 507 | if (IS_ERR(rq)) { |
| 508 | kfree(cmd); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 509 | return -ENOMEM; |
| 510 | } |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 511 | rq->cmd_flags &= ~REQ_FAILFAST_DRIVER; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 512 | |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 513 | rq->ioprio = bio_prio(bio); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 514 | if (bio_has_data(bio)) |
| 515 | rq->nr_phys_segments = bio_phys_segments(q, bio); |
| 516 | |
| 517 | rq->__data_len = bio->bi_iter.bi_size; |
| 518 | rq->bio = rq->biotail = bio; |
| 519 | |
| 520 | nvme_nvm_rqtocmd(rq, rqd, ns, cmd); |
| 521 | |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 522 | rq->end_io_data = rqd; |
| 523 | |
| 524 | blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_io); |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 529 | static int nvme_nvm_erase_block(struct nvm_dev *dev, struct nvm_rq *rqd) |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 530 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 531 | struct request_queue *q = dev->q; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 532 | struct nvme_ns *ns = q->queuedata; |
| 533 | struct nvme_nvm_command c = {}; |
| 534 | |
| 535 | c.erase.opcode = NVM_OP_ERASE; |
| 536 | c.erase.nsid = cpu_to_le32(ns->ns_id); |
| 537 | c.erase.spba = cpu_to_le64(rqd->ppa_addr.ppa); |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 538 | c.erase.length = cpu_to_le16(rqd->nr_ppas - 1); |
Javier González | bb31497 | 2016-11-28 22:38:54 +0100 | [diff] [blame] | 539 | c.erase.control = cpu_to_le16(rqd->flags); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 540 | |
| 541 | return nvme_submit_sync_cmd(q, (struct nvme_command *)&c, NULL, 0); |
| 542 | } |
| 543 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 544 | static void *nvme_nvm_create_dma_pool(struct nvm_dev *nvmdev, char *name) |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 545 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 546 | struct nvme_ns *ns = nvmdev->q->queuedata; |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 547 | |
Christoph Hellwig | ac02ddd | 2015-12-03 09:52:05 -0700 | [diff] [blame] | 548 | return dma_pool_create(name, ns->ctrl->dev, PAGE_SIZE, PAGE_SIZE, 0); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | static void nvme_nvm_destroy_dma_pool(void *pool) |
| 552 | { |
| 553 | struct dma_pool *dma_pool = pool; |
| 554 | |
| 555 | dma_pool_destroy(dma_pool); |
| 556 | } |
| 557 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 558 | static void *nvme_nvm_dev_dma_alloc(struct nvm_dev *dev, void *pool, |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 559 | gfp_t mem_flags, dma_addr_t *dma_handler) |
| 560 | { |
| 561 | return dma_pool_alloc(pool, mem_flags, dma_handler); |
| 562 | } |
| 563 | |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 564 | static void nvme_nvm_dev_dma_free(void *pool, void *addr, |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 565 | dma_addr_t dma_handler) |
| 566 | { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 567 | dma_pool_free(pool, addr, dma_handler); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static struct nvm_dev_ops nvme_nvm_dev_ops = { |
| 571 | .identity = nvme_nvm_identity, |
| 572 | |
| 573 | .get_l2p_tbl = nvme_nvm_get_l2p_tbl, |
| 574 | |
| 575 | .get_bb_tbl = nvme_nvm_get_bb_tbl, |
Matias Bjørling | 1145046 | 2015-11-16 15:34:37 +0100 | [diff] [blame] | 576 | .set_bb_tbl = nvme_nvm_set_bb_tbl, |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 577 | |
| 578 | .submit_io = nvme_nvm_submit_io, |
| 579 | .erase_block = nvme_nvm_erase_block, |
| 580 | |
| 581 | .create_dma_pool = nvme_nvm_create_dma_pool, |
| 582 | .destroy_dma_pool = nvme_nvm_destroy_dma_pool, |
| 583 | .dev_dma_alloc = nvme_nvm_dev_dma_alloc, |
| 584 | .dev_dma_free = nvme_nvm_dev_dma_free, |
| 585 | |
| 586 | .max_phys_sect = 64, |
| 587 | }; |
| 588 | |
Matias Bjørling | 84d4add | 2017-01-31 13:17:16 +0100 | [diff] [blame] | 589 | static void nvme_nvm_end_user_vio(struct request *rq, int error) |
| 590 | { |
| 591 | struct completion *waiting = rq->end_io_data; |
| 592 | |
| 593 | complete(waiting); |
| 594 | } |
| 595 | |
| 596 | static int nvme_nvm_submit_user_cmd(struct request_queue *q, |
| 597 | struct nvme_ns *ns, |
| 598 | struct nvme_nvm_command *vcmd, |
| 599 | void __user *ubuf, unsigned int bufflen, |
| 600 | void __user *meta_buf, unsigned int meta_len, |
| 601 | void __user *ppa_buf, unsigned int ppa_len, |
| 602 | u32 *result, u64 *status, unsigned int timeout) |
| 603 | { |
| 604 | bool write = nvme_is_write((struct nvme_command *)vcmd); |
| 605 | struct nvm_dev *dev = ns->ndev; |
| 606 | struct gendisk *disk = ns->disk; |
| 607 | struct request *rq; |
| 608 | struct bio *bio = NULL; |
| 609 | __le64 *ppa_list = NULL; |
| 610 | dma_addr_t ppa_dma; |
| 611 | __le64 *metadata = NULL; |
| 612 | dma_addr_t metadata_dma; |
| 613 | DECLARE_COMPLETION_ONSTACK(wait); |
| 614 | int ret; |
| 615 | |
| 616 | rq = nvme_alloc_request(q, (struct nvme_command *)vcmd, 0, |
| 617 | NVME_QID_ANY); |
| 618 | if (IS_ERR(rq)) { |
| 619 | ret = -ENOMEM; |
| 620 | goto err_cmd; |
| 621 | } |
| 622 | |
| 623 | rq->timeout = timeout ? timeout : ADMIN_TIMEOUT; |
| 624 | |
| 625 | rq->cmd_flags &= ~REQ_FAILFAST_DRIVER; |
| 626 | rq->end_io_data = &wait; |
| 627 | |
| 628 | if (ppa_buf && ppa_len) { |
| 629 | ppa_list = dma_pool_alloc(dev->dma_pool, GFP_KERNEL, &ppa_dma); |
| 630 | if (!ppa_list) { |
| 631 | ret = -ENOMEM; |
| 632 | goto err_rq; |
| 633 | } |
| 634 | if (copy_from_user(ppa_list, (void __user *)ppa_buf, |
| 635 | sizeof(u64) * (ppa_len + 1))) { |
| 636 | ret = -EFAULT; |
| 637 | goto err_ppa; |
| 638 | } |
| 639 | vcmd->ph_rw.spba = cpu_to_le64(ppa_dma); |
| 640 | } else { |
| 641 | vcmd->ph_rw.spba = cpu_to_le64((uintptr_t)ppa_buf); |
| 642 | } |
| 643 | |
| 644 | if (ubuf && bufflen) { |
| 645 | ret = blk_rq_map_user(q, rq, NULL, ubuf, bufflen, GFP_KERNEL); |
| 646 | if (ret) |
| 647 | goto err_ppa; |
| 648 | bio = rq->bio; |
| 649 | |
| 650 | if (meta_buf && meta_len) { |
| 651 | metadata = dma_pool_alloc(dev->dma_pool, GFP_KERNEL, |
| 652 | &metadata_dma); |
| 653 | if (!metadata) { |
| 654 | ret = -ENOMEM; |
| 655 | goto err_map; |
| 656 | } |
| 657 | |
| 658 | if (write) { |
| 659 | if (copy_from_user(metadata, |
| 660 | (void __user *)meta_buf, |
| 661 | meta_len)) { |
| 662 | ret = -EFAULT; |
| 663 | goto err_meta; |
| 664 | } |
| 665 | } |
| 666 | vcmd->ph_rw.metadata = cpu_to_le64(metadata_dma); |
| 667 | } |
| 668 | |
| 669 | if (!disk) |
| 670 | goto submit; |
| 671 | |
| 672 | bio->bi_bdev = bdget_disk(disk, 0); |
| 673 | if (!bio->bi_bdev) { |
| 674 | ret = -ENODEV; |
| 675 | goto err_meta; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | submit: |
| 680 | blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_user_vio); |
| 681 | |
| 682 | wait_for_completion_io(&wait); |
| 683 | |
| 684 | ret = nvme_error_status(rq->errors); |
| 685 | if (result) |
| 686 | *result = rq->errors & 0x7ff; |
| 687 | if (status) |
| 688 | *status = le64_to_cpu(nvme_req(rq)->result.u64); |
| 689 | |
| 690 | if (metadata && !ret && !write) { |
| 691 | if (copy_to_user(meta_buf, (void *)metadata, meta_len)) |
| 692 | ret = -EFAULT; |
| 693 | } |
| 694 | err_meta: |
| 695 | if (meta_buf && meta_len) |
| 696 | dma_pool_free(dev->dma_pool, metadata, metadata_dma); |
| 697 | err_map: |
| 698 | if (bio) { |
| 699 | if (disk && bio->bi_bdev) |
| 700 | bdput(bio->bi_bdev); |
| 701 | blk_rq_unmap_user(bio); |
| 702 | } |
| 703 | err_ppa: |
| 704 | if (ppa_buf && ppa_len) |
| 705 | dma_pool_free(dev->dma_pool, ppa_list, ppa_dma); |
| 706 | err_rq: |
| 707 | blk_mq_free_request(rq); |
| 708 | err_cmd: |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | static int nvme_nvm_submit_vio(struct nvme_ns *ns, |
| 713 | struct nvm_user_vio __user *uvio) |
| 714 | { |
| 715 | struct nvm_user_vio vio; |
| 716 | struct nvme_nvm_command c; |
| 717 | unsigned int length; |
| 718 | int ret; |
| 719 | |
| 720 | if (copy_from_user(&vio, uvio, sizeof(vio))) |
| 721 | return -EFAULT; |
| 722 | if (vio.flags) |
| 723 | return -EINVAL; |
| 724 | |
| 725 | memset(&c, 0, sizeof(c)); |
| 726 | c.ph_rw.opcode = vio.opcode; |
| 727 | c.ph_rw.nsid = cpu_to_le32(ns->ns_id); |
| 728 | c.ph_rw.control = cpu_to_le16(vio.control); |
| 729 | c.ph_rw.length = cpu_to_le16(vio.nppas); |
| 730 | |
| 731 | length = (vio.nppas + 1) << ns->lba_shift; |
| 732 | |
| 733 | ret = nvme_nvm_submit_user_cmd(ns->queue, ns, &c, |
| 734 | (void __user *)(uintptr_t)vio.addr, length, |
| 735 | (void __user *)(uintptr_t)vio.metadata, |
| 736 | vio.metadata_len, |
| 737 | (void __user *)(uintptr_t)vio.ppa_list, vio.nppas, |
| 738 | &vio.result, &vio.status, 0); |
| 739 | |
| 740 | if (ret && copy_to_user(uvio, &vio, sizeof(vio))) |
| 741 | return -EFAULT; |
| 742 | |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | static int nvme_nvm_user_vcmd(struct nvme_ns *ns, int admin, |
| 747 | struct nvm_passthru_vio __user *uvcmd) |
| 748 | { |
| 749 | struct nvm_passthru_vio vcmd; |
| 750 | struct nvme_nvm_command c; |
| 751 | struct request_queue *q; |
| 752 | unsigned int timeout = 0; |
| 753 | int ret; |
| 754 | |
| 755 | if (copy_from_user(&vcmd, uvcmd, sizeof(vcmd))) |
| 756 | return -EFAULT; |
| 757 | if ((vcmd.opcode != 0xF2) && (!capable(CAP_SYS_ADMIN))) |
| 758 | return -EACCES; |
| 759 | if (vcmd.flags) |
| 760 | return -EINVAL; |
| 761 | |
| 762 | memset(&c, 0, sizeof(c)); |
| 763 | c.common.opcode = vcmd.opcode; |
| 764 | c.common.nsid = cpu_to_le32(ns->ns_id); |
| 765 | c.common.cdw2[0] = cpu_to_le32(vcmd.cdw2); |
| 766 | c.common.cdw2[1] = cpu_to_le32(vcmd.cdw3); |
| 767 | /* cdw11-12 */ |
| 768 | c.ph_rw.length = cpu_to_le16(vcmd.nppas); |
| 769 | c.ph_rw.control = cpu_to_le32(vcmd.control); |
| 770 | c.common.cdw10[3] = cpu_to_le32(vcmd.cdw13); |
| 771 | c.common.cdw10[4] = cpu_to_le32(vcmd.cdw14); |
| 772 | c.common.cdw10[5] = cpu_to_le32(vcmd.cdw15); |
| 773 | |
| 774 | if (vcmd.timeout_ms) |
| 775 | timeout = msecs_to_jiffies(vcmd.timeout_ms); |
| 776 | |
| 777 | q = admin ? ns->ctrl->admin_q : ns->queue; |
| 778 | |
| 779 | ret = nvme_nvm_submit_user_cmd(q, ns, |
| 780 | (struct nvme_nvm_command *)&c, |
| 781 | (void __user *)(uintptr_t)vcmd.addr, vcmd.data_len, |
| 782 | (void __user *)(uintptr_t)vcmd.metadata, |
| 783 | vcmd.metadata_len, |
| 784 | (void __user *)(uintptr_t)vcmd.ppa_list, vcmd.nppas, |
| 785 | &vcmd.result, &vcmd.status, timeout); |
| 786 | |
| 787 | if (ret && copy_to_user(uvcmd, &vcmd, sizeof(vcmd))) |
| 788 | return -EFAULT; |
| 789 | |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg) |
| 794 | { |
| 795 | switch (cmd) { |
| 796 | case NVME_NVM_IOCTL_ADMIN_VIO: |
| 797 | return nvme_nvm_user_vcmd(ns, 1, (void __user *)arg); |
| 798 | case NVME_NVM_IOCTL_IO_VIO: |
| 799 | return nvme_nvm_user_vcmd(ns, 0, (void __user *)arg); |
| 800 | case NVME_NVM_IOCTL_SUBMIT_VIO: |
| 801 | return nvme_nvm_submit_vio(ns, (void __user *)arg); |
| 802 | default: |
| 803 | return -ENOTTY; |
| 804 | } |
| 805 | } |
| 806 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 807 | int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node) |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 808 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 809 | struct request_queue *q = ns->queue; |
| 810 | struct nvm_dev *dev; |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 811 | |
| 812 | dev = nvm_alloc_dev(node); |
| 813 | if (!dev) |
| 814 | return -ENOMEM; |
| 815 | |
| 816 | dev->q = q; |
| 817 | memcpy(dev->name, disk_name, DISK_NAME_LEN); |
| 818 | dev->ops = &nvme_nvm_dev_ops; |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 819 | dev->private_data = ns; |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 820 | ns->ndev = dev; |
| 821 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 822 | return nvm_register(dev); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 823 | } |
| 824 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 825 | void nvme_nvm_unregister(struct nvme_ns *ns) |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 826 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 827 | nvm_unregister(ns->ndev); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 828 | } |
| 829 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 830 | static ssize_t nvm_dev_attr_show(struct device *dev, |
| 831 | struct device_attribute *dattr, char *page) |
| 832 | { |
| 833 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
| 834 | struct nvm_dev *ndev = ns->ndev; |
| 835 | struct nvm_id *id; |
| 836 | struct nvm_id_group *grp; |
| 837 | struct attribute *attr; |
| 838 | |
| 839 | if (!ndev) |
| 840 | return 0; |
| 841 | |
| 842 | id = &ndev->identity; |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 843 | grp = &id->grp; |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 844 | attr = &dattr->attr; |
| 845 | |
| 846 | if (strcmp(attr->name, "version") == 0) { |
| 847 | return scnprintf(page, PAGE_SIZE, "%u\n", id->ver_id); |
| 848 | } else if (strcmp(attr->name, "vendor_opcode") == 0) { |
| 849 | return scnprintf(page, PAGE_SIZE, "%u\n", id->vmnt); |
| 850 | } else if (strcmp(attr->name, "capabilities") == 0) { |
| 851 | return scnprintf(page, PAGE_SIZE, "%u\n", id->cap); |
| 852 | } else if (strcmp(attr->name, "device_mode") == 0) { |
| 853 | return scnprintf(page, PAGE_SIZE, "%u\n", id->dom); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 854 | /* kept for compatibility */ |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 855 | } else if (strcmp(attr->name, "media_manager") == 0) { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 856 | return scnprintf(page, PAGE_SIZE, "%s\n", "gennvm"); |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 857 | } else if (strcmp(attr->name, "ppa_format") == 0) { |
| 858 | return scnprintf(page, PAGE_SIZE, |
| 859 | "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", |
| 860 | id->ppaf.ch_offset, id->ppaf.ch_len, |
| 861 | id->ppaf.lun_offset, id->ppaf.lun_len, |
| 862 | id->ppaf.pln_offset, id->ppaf.pln_len, |
| 863 | id->ppaf.blk_offset, id->ppaf.blk_len, |
| 864 | id->ppaf.pg_offset, id->ppaf.pg_len, |
| 865 | id->ppaf.sect_offset, id->ppaf.sect_len); |
| 866 | } else if (strcmp(attr->name, "media_type") == 0) { /* u8 */ |
| 867 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->mtype); |
| 868 | } else if (strcmp(attr->name, "flash_media_type") == 0) { |
| 869 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->fmtype); |
| 870 | } else if (strcmp(attr->name, "num_channels") == 0) { |
| 871 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_ch); |
| 872 | } else if (strcmp(attr->name, "num_luns") == 0) { |
| 873 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_lun); |
| 874 | } else if (strcmp(attr->name, "num_planes") == 0) { |
| 875 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pln); |
| 876 | } else if (strcmp(attr->name, "num_blocks") == 0) { /* u16 */ |
| 877 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_blk); |
| 878 | } else if (strcmp(attr->name, "num_pages") == 0) { |
| 879 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pg); |
| 880 | } else if (strcmp(attr->name, "page_size") == 0) { |
| 881 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->fpg_sz); |
| 882 | } else if (strcmp(attr->name, "hw_sector_size") == 0) { |
| 883 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->csecs); |
| 884 | } else if (strcmp(attr->name, "oob_sector_size") == 0) {/* u32 */ |
| 885 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->sos); |
| 886 | } else if (strcmp(attr->name, "read_typ") == 0) { |
| 887 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdt); |
| 888 | } else if (strcmp(attr->name, "read_max") == 0) { |
| 889 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdm); |
| 890 | } else if (strcmp(attr->name, "prog_typ") == 0) { |
| 891 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprt); |
| 892 | } else if (strcmp(attr->name, "prog_max") == 0) { |
| 893 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprm); |
| 894 | } else if (strcmp(attr->name, "erase_typ") == 0) { |
| 895 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbet); |
| 896 | } else if (strcmp(attr->name, "erase_max") == 0) { |
| 897 | return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbem); |
| 898 | } else if (strcmp(attr->name, "multiplane_modes") == 0) { |
| 899 | return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mpos); |
| 900 | } else if (strcmp(attr->name, "media_capabilities") == 0) { |
| 901 | return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mccap); |
| 902 | } else if (strcmp(attr->name, "max_phys_secs") == 0) { |
| 903 | return scnprintf(page, PAGE_SIZE, "%u\n", |
| 904 | ndev->ops->max_phys_sect); |
| 905 | } else { |
| 906 | return scnprintf(page, |
| 907 | PAGE_SIZE, |
| 908 | "Unhandled attr(%s) in `nvm_dev_attr_show`\n", |
| 909 | attr->name); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | #define NVM_DEV_ATTR_RO(_name) \ |
| 914 | DEVICE_ATTR(_name, S_IRUGO, nvm_dev_attr_show, NULL) |
| 915 | |
| 916 | static NVM_DEV_ATTR_RO(version); |
| 917 | static NVM_DEV_ATTR_RO(vendor_opcode); |
| 918 | static NVM_DEV_ATTR_RO(capabilities); |
| 919 | static NVM_DEV_ATTR_RO(device_mode); |
| 920 | static NVM_DEV_ATTR_RO(ppa_format); |
| 921 | static NVM_DEV_ATTR_RO(media_manager); |
| 922 | |
| 923 | static NVM_DEV_ATTR_RO(media_type); |
| 924 | static NVM_DEV_ATTR_RO(flash_media_type); |
| 925 | static NVM_DEV_ATTR_RO(num_channels); |
| 926 | static NVM_DEV_ATTR_RO(num_luns); |
| 927 | static NVM_DEV_ATTR_RO(num_planes); |
| 928 | static NVM_DEV_ATTR_RO(num_blocks); |
| 929 | static NVM_DEV_ATTR_RO(num_pages); |
| 930 | static NVM_DEV_ATTR_RO(page_size); |
| 931 | static NVM_DEV_ATTR_RO(hw_sector_size); |
| 932 | static NVM_DEV_ATTR_RO(oob_sector_size); |
| 933 | static NVM_DEV_ATTR_RO(read_typ); |
| 934 | static NVM_DEV_ATTR_RO(read_max); |
| 935 | static NVM_DEV_ATTR_RO(prog_typ); |
| 936 | static NVM_DEV_ATTR_RO(prog_max); |
| 937 | static NVM_DEV_ATTR_RO(erase_typ); |
| 938 | static NVM_DEV_ATTR_RO(erase_max); |
| 939 | static NVM_DEV_ATTR_RO(multiplane_modes); |
| 940 | static NVM_DEV_ATTR_RO(media_capabilities); |
| 941 | static NVM_DEV_ATTR_RO(max_phys_secs); |
| 942 | |
| 943 | static struct attribute *nvm_dev_attrs[] = { |
| 944 | &dev_attr_version.attr, |
| 945 | &dev_attr_vendor_opcode.attr, |
| 946 | &dev_attr_capabilities.attr, |
| 947 | &dev_attr_device_mode.attr, |
| 948 | &dev_attr_media_manager.attr, |
| 949 | |
| 950 | &dev_attr_ppa_format.attr, |
| 951 | &dev_attr_media_type.attr, |
| 952 | &dev_attr_flash_media_type.attr, |
| 953 | &dev_attr_num_channels.attr, |
| 954 | &dev_attr_num_luns.attr, |
| 955 | &dev_attr_num_planes.attr, |
| 956 | &dev_attr_num_blocks.attr, |
| 957 | &dev_attr_num_pages.attr, |
| 958 | &dev_attr_page_size.attr, |
| 959 | &dev_attr_hw_sector_size.attr, |
| 960 | &dev_attr_oob_sector_size.attr, |
| 961 | &dev_attr_read_typ.attr, |
| 962 | &dev_attr_read_max.attr, |
| 963 | &dev_attr_prog_typ.attr, |
| 964 | &dev_attr_prog_max.attr, |
| 965 | &dev_attr_erase_typ.attr, |
| 966 | &dev_attr_erase_max.attr, |
| 967 | &dev_attr_multiplane_modes.attr, |
| 968 | &dev_attr_media_capabilities.attr, |
| 969 | &dev_attr_max_phys_secs.attr, |
| 970 | NULL, |
| 971 | }; |
| 972 | |
| 973 | static const struct attribute_group nvm_dev_attr_group = { |
| 974 | .name = "lightnvm", |
| 975 | .attrs = nvm_dev_attrs, |
| 976 | }; |
| 977 | |
| 978 | int nvme_nvm_register_sysfs(struct nvme_ns *ns) |
| 979 | { |
| 980 | return sysfs_create_group(&disk_to_dev(ns->disk)->kobj, |
| 981 | &nvm_dev_attr_group); |
| 982 | } |
| 983 | |
| 984 | void nvme_nvm_unregister_sysfs(struct nvme_ns *ns) |
| 985 | { |
| 986 | sysfs_remove_group(&disk_to_dev(ns->disk)->kobj, |
| 987 | &nvm_dev_attr_group); |
| 988 | } |
| 989 | |
Matias Bjørling | 09f2e71 | 2015-11-28 16:49:26 +0100 | [diff] [blame] | 990 | /* move to shared place when used in multiple places. */ |
| 991 | #define PCI_VENDOR_ID_CNEX 0x1d1d |
| 992 | #define PCI_DEVICE_ID_CNEX_WL 0x2807 |
| 993 | #define PCI_DEVICE_ID_CNEX_QEMU 0x1f1f |
| 994 | |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 995 | int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id) |
| 996 | { |
Christoph Hellwig | ac02ddd | 2015-12-03 09:52:05 -0700 | [diff] [blame] | 997 | struct nvme_ctrl *ctrl = ns->ctrl; |
| 998 | /* XXX: this is poking into PCI structures from generic code! */ |
| 999 | struct pci_dev *pdev = to_pci_dev(ctrl->dev); |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 1000 | |
| 1001 | /* QEMU NVMe simulator - PCI ID + Vendor specific bit */ |
Matias Bjørling | 09f2e71 | 2015-11-28 16:49:26 +0100 | [diff] [blame] | 1002 | if (pdev->vendor == PCI_VENDOR_ID_CNEX && |
| 1003 | pdev->device == PCI_DEVICE_ID_CNEX_QEMU && |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 1004 | id->vs[0] == 0x1) |
| 1005 | return 1; |
| 1006 | |
| 1007 | /* CNEX Labs - PCI ID + Vendor specific bit */ |
Matias Bjørling | 09f2e71 | 2015-11-28 16:49:26 +0100 | [diff] [blame] | 1008 | if (pdev->vendor == PCI_VENDOR_ID_CNEX && |
| 1009 | pdev->device == PCI_DEVICE_ID_CNEX_WL && |
Matias Bjørling | ca06408 | 2015-10-29 17:57:29 +0900 | [diff] [blame] | 1010 | id->vs[0] == 0x1) |
| 1011 | return 1; |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |