Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 IT University of Copenhagen. All rights reserved. |
| 3 | * Initial release: Matias Bjorling <m@bjorling.me> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License version |
| 7 | * 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, |
| 17 | * USA. |
| 18 | * |
| 19 | */ |
| 20 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 21 | #include <linux/list.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/sem.h> |
| 24 | #include <linux/bitmap.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/miscdevice.h> |
| 27 | #include <linux/lightnvm.h> |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 28 | #include <linux/sched/sysctl.h> |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 29 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 30 | static LIST_HEAD(nvm_tgt_types); |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 31 | static DECLARE_RWSEM(nvm_tgtt_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 32 | static LIST_HEAD(nvm_mgrs); |
| 33 | static LIST_HEAD(nvm_devices); |
| 34 | static DECLARE_RWSEM(nvm_lock); |
| 35 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 36 | struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock) |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 37 | { |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 38 | struct nvm_tgt_type *tmp, *tt = NULL; |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 39 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 40 | if (lock) |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 41 | down_write(&nvm_tgtt_lock); |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 42 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 43 | list_for_each_entry(tmp, &nvm_tgt_types, list) |
| 44 | if (!strcmp(name, tmp->name)) { |
| 45 | tt = tmp; |
| 46 | break; |
| 47 | } |
| 48 | |
| 49 | if (lock) |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 50 | up_write(&nvm_tgtt_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 51 | return tt; |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 52 | } |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 53 | EXPORT_SYMBOL(nvm_find_target_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 54 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 55 | int nvm_register_tgt_type(struct nvm_tgt_type *tt) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 56 | { |
| 57 | int ret = 0; |
| 58 | |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 59 | down_write(&nvm_tgtt_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 60 | if (nvm_find_target_type(tt->name, 0)) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 61 | ret = -EEXIST; |
| 62 | else |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 63 | list_add(&tt->list, &nvm_tgt_types); |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 64 | up_write(&nvm_tgtt_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 65 | |
| 66 | return ret; |
| 67 | } |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 68 | EXPORT_SYMBOL(nvm_register_tgt_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 69 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 70 | void nvm_unregister_tgt_type(struct nvm_tgt_type *tt) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 71 | { |
| 72 | if (!tt) |
| 73 | return; |
| 74 | |
| 75 | down_write(&nvm_lock); |
| 76 | list_del(&tt->list); |
| 77 | up_write(&nvm_lock); |
| 78 | } |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 79 | EXPORT_SYMBOL(nvm_unregister_tgt_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 80 | |
| 81 | void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags, |
| 82 | dma_addr_t *dma_handler) |
| 83 | { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 84 | return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags, |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 85 | dma_handler); |
| 86 | } |
| 87 | EXPORT_SYMBOL(nvm_dev_dma_alloc); |
| 88 | |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 89 | void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 90 | dma_addr_t dma_handler) |
| 91 | { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 92 | dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 93 | } |
| 94 | EXPORT_SYMBOL(nvm_dev_dma_free); |
| 95 | |
| 96 | static struct nvmm_type *nvm_find_mgr_type(const char *name) |
| 97 | { |
| 98 | struct nvmm_type *mt; |
| 99 | |
| 100 | list_for_each_entry(mt, &nvm_mgrs, list) |
| 101 | if (!strcmp(name, mt->name)) |
| 102 | return mt; |
| 103 | |
| 104 | return NULL; |
| 105 | } |
| 106 | |
Johannes Thumshirn | 58eaaf9 | 2016-07-07 09:54:11 +0200 | [diff] [blame] | 107 | static struct nvmm_type *nvm_init_mgr(struct nvm_dev *dev) |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 108 | { |
| 109 | struct nvmm_type *mt; |
| 110 | int ret; |
| 111 | |
| 112 | lockdep_assert_held(&nvm_lock); |
| 113 | |
| 114 | list_for_each_entry(mt, &nvm_mgrs, list) { |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 115 | if (strncmp(dev->sb.mmtype, mt->name, NVM_MMTYPE_LEN)) |
| 116 | continue; |
| 117 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 118 | ret = mt->register_mgr(dev); |
| 119 | if (ret < 0) { |
| 120 | pr_err("nvm: media mgr failed to init (%d) on dev %s\n", |
| 121 | ret, dev->name); |
| 122 | return NULL; /* initialization failed */ |
| 123 | } else if (ret > 0) |
| 124 | return mt; |
| 125 | } |
| 126 | |
| 127 | return NULL; |
| 128 | } |
| 129 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 130 | int nvm_register_mgr(struct nvmm_type *mt) |
| 131 | { |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 132 | struct nvm_dev *dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 133 | int ret = 0; |
| 134 | |
| 135 | down_write(&nvm_lock); |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 136 | if (nvm_find_mgr_type(mt->name)) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 137 | ret = -EEXIST; |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 138 | goto finish; |
| 139 | } else { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 140 | list_add(&mt->list, &nvm_mgrs); |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* try to register media mgr if any device have none configured */ |
| 144 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 145 | if (dev->mt) |
| 146 | continue; |
| 147 | |
| 148 | dev->mt = nvm_init_mgr(dev); |
| 149 | } |
| 150 | finish: |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 151 | up_write(&nvm_lock); |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | EXPORT_SYMBOL(nvm_register_mgr); |
| 156 | |
| 157 | void nvm_unregister_mgr(struct nvmm_type *mt) |
| 158 | { |
| 159 | if (!mt) |
| 160 | return; |
| 161 | |
| 162 | down_write(&nvm_lock); |
| 163 | list_del(&mt->list); |
| 164 | up_write(&nvm_lock); |
| 165 | } |
| 166 | EXPORT_SYMBOL(nvm_unregister_mgr); |
| 167 | |
| 168 | static struct nvm_dev *nvm_find_nvm_dev(const char *name) |
| 169 | { |
| 170 | struct nvm_dev *dev; |
| 171 | |
| 172 | list_for_each_entry(dev, &nvm_devices, devices) |
| 173 | if (!strcmp(name, dev->name)) |
| 174 | return dev; |
| 175 | |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | struct nvm_block *nvm_get_blk(struct nvm_dev *dev, struct nvm_lun *lun, |
| 180 | unsigned long flags) |
| 181 | { |
| 182 | return dev->mt->get_blk(dev, lun, flags); |
| 183 | } |
| 184 | EXPORT_SYMBOL(nvm_get_blk); |
| 185 | |
| 186 | /* Assumes that all valid pages have already been moved on release to bm */ |
| 187 | void nvm_put_blk(struct nvm_dev *dev, struct nvm_block *blk) |
| 188 | { |
| 189 | return dev->mt->put_blk(dev, blk); |
| 190 | } |
| 191 | EXPORT_SYMBOL(nvm_put_blk); |
| 192 | |
Javier González | 529435e | 2016-07-07 09:54:08 +0200 | [diff] [blame] | 193 | void nvm_mark_blk(struct nvm_dev *dev, struct ppa_addr ppa, int type) |
| 194 | { |
| 195 | return dev->mt->mark_blk(dev, ppa, type); |
| 196 | } |
| 197 | EXPORT_SYMBOL(nvm_mark_blk); |
| 198 | |
Javier González | a24ba46 | 2016-11-28 22:38:56 +0100 | [diff] [blame^] | 199 | int nvm_set_bb_tbl(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas, |
| 200 | int type) |
| 201 | { |
| 202 | struct nvm_rq rqd; |
| 203 | int ret; |
| 204 | |
| 205 | if (nr_ppas > dev->ops->max_phys_sect) { |
| 206 | pr_err("nvm: unable to update all sysblocks atomically\n"); |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
| 210 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 211 | |
| 212 | nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1); |
| 213 | nvm_generic_to_addr_mode(dev, &rqd); |
| 214 | |
| 215 | ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type); |
| 216 | nvm_free_rqd_ppalist(dev, &rqd); |
| 217 | if (ret) { |
| 218 | pr_err("nvm: sysblk failed bb mark\n"); |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | EXPORT_SYMBOL(nvm_set_bb_tbl); |
| 225 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 226 | int nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd) |
| 227 | { |
| 228 | return dev->mt->submit_io(dev, rqd); |
| 229 | } |
| 230 | EXPORT_SYMBOL(nvm_submit_io); |
| 231 | |
Javier González | bb31497 | 2016-11-28 22:38:54 +0100 | [diff] [blame] | 232 | int nvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk, int flags) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 233 | { |
Javier González | bb31497 | 2016-11-28 22:38:54 +0100 | [diff] [blame] | 234 | return dev->mt->erase_blk(dev, blk, flags); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 235 | } |
| 236 | EXPORT_SYMBOL(nvm_erase_blk); |
| 237 | |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 238 | void nvm_addr_to_generic_mode(struct nvm_dev *dev, struct nvm_rq *rqd) |
| 239 | { |
| 240 | int i; |
| 241 | |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 242 | if (rqd->nr_ppas > 1) { |
| 243 | for (i = 0; i < rqd->nr_ppas; i++) |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 244 | rqd->ppa_list[i] = dev_to_generic_addr(dev, |
| 245 | rqd->ppa_list[i]); |
| 246 | } else { |
| 247 | rqd->ppa_addr = dev_to_generic_addr(dev, rqd->ppa_addr); |
| 248 | } |
| 249 | } |
| 250 | EXPORT_SYMBOL(nvm_addr_to_generic_mode); |
| 251 | |
| 252 | void nvm_generic_to_addr_mode(struct nvm_dev *dev, struct nvm_rq *rqd) |
| 253 | { |
| 254 | int i; |
| 255 | |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 256 | if (rqd->nr_ppas > 1) { |
| 257 | for (i = 0; i < rqd->nr_ppas; i++) |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 258 | rqd->ppa_list[i] = generic_to_dev_addr(dev, |
| 259 | rqd->ppa_list[i]); |
| 260 | } else { |
| 261 | rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr); |
| 262 | } |
| 263 | } |
| 264 | EXPORT_SYMBOL(nvm_generic_to_addr_mode); |
| 265 | |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 266 | int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd, |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 267 | const struct ppa_addr *ppas, int nr_ppas, int vblk) |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 268 | { |
| 269 | int i, plane_cnt, pl_idx; |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 270 | struct ppa_addr ppa; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 271 | |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 272 | if ((!vblk || dev->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) { |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 273 | rqd->nr_ppas = nr_ppas; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 274 | rqd->ppa_addr = ppas[0]; |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 279 | rqd->nr_ppas = nr_ppas; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 280 | rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list); |
| 281 | if (!rqd->ppa_list) { |
| 282 | pr_err("nvm: failed to allocate dma memory\n"); |
| 283 | return -ENOMEM; |
| 284 | } |
| 285 | |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 286 | if (!vblk) { |
| 287 | for (i = 0; i < nr_ppas; i++) |
| 288 | rqd->ppa_list[i] = ppas[i]; |
| 289 | } else { |
| 290 | plane_cnt = dev->plane_mode; |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 291 | rqd->nr_ppas *= plane_cnt; |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 292 | |
Matias Bjørling | 556755e | 2016-01-12 07:49:26 +0100 | [diff] [blame] | 293 | for (i = 0; i < nr_ppas; i++) { |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 294 | for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) { |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 295 | ppa = ppas[i]; |
| 296 | ppa.g.pl = pl_idx; |
| 297 | rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa; |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 298 | } |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | EXPORT_SYMBOL(nvm_set_rqd_ppalist); |
| 305 | |
| 306 | void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd) |
| 307 | { |
| 308 | if (!rqd->ppa_list) |
| 309 | return; |
| 310 | |
| 311 | nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list); |
| 312 | } |
| 313 | EXPORT_SYMBOL(nvm_free_rqd_ppalist); |
| 314 | |
Javier González | bb31497 | 2016-11-28 22:38:54 +0100 | [diff] [blame] | 315 | int nvm_erase_ppa(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas, |
| 316 | int flags) |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 317 | { |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 318 | struct nvm_rq rqd; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 319 | int ret; |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 320 | |
| 321 | if (!dev->ops->erase_block) |
| 322 | return 0; |
| 323 | |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 324 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 325 | |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 326 | ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1); |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 327 | if (ret) |
| 328 | return ret; |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 329 | |
| 330 | nvm_generic_to_addr_mode(dev, &rqd); |
| 331 | |
Javier González | bb31497 | 2016-11-28 22:38:54 +0100 | [diff] [blame] | 332 | rqd.flags = flags; |
| 333 | |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 334 | ret = dev->ops->erase_block(dev, &rqd); |
| 335 | |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 336 | nvm_free_rqd_ppalist(dev, &rqd); |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 337 | |
| 338 | return ret; |
| 339 | } |
| 340 | EXPORT_SYMBOL(nvm_erase_ppa); |
| 341 | |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 342 | void nvm_end_io(struct nvm_rq *rqd, int error) |
| 343 | { |
Matias Bjørling | 72d256e | 2016-01-12 07:49:29 +0100 | [diff] [blame] | 344 | rqd->error = error; |
| 345 | rqd->end_io(rqd); |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 346 | } |
| 347 | EXPORT_SYMBOL(nvm_end_io); |
| 348 | |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 349 | static void nvm_end_io_sync(struct nvm_rq *rqd) |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 350 | { |
| 351 | struct completion *waiting = rqd->wait; |
| 352 | |
| 353 | rqd->wait = NULL; |
| 354 | |
| 355 | complete(waiting); |
| 356 | } |
| 357 | |
Matias Bjørling | d9e46d5 | 2016-07-07 09:54:23 +0200 | [diff] [blame] | 358 | static int __nvm_submit_ppa(struct nvm_dev *dev, struct nvm_rq *rqd, int opcode, |
Matias Bjørling | 1145e63 | 2016-05-06 20:02:56 +0200 | [diff] [blame] | 359 | int flags, void *buf, int len) |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 360 | { |
| 361 | DECLARE_COMPLETION_ONSTACK(wait); |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 362 | struct bio *bio; |
| 363 | int ret; |
| 364 | unsigned long hang_check; |
| 365 | |
| 366 | bio = bio_map_kern(dev->q, buf, len, GFP_KERNEL); |
| 367 | if (IS_ERR_OR_NULL(bio)) |
| 368 | return -ENOMEM; |
| 369 | |
Matias Bjørling | 1145e63 | 2016-05-06 20:02:56 +0200 | [diff] [blame] | 370 | nvm_generic_to_addr_mode(dev, rqd); |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 371 | |
Matias Bjørling | 1145e63 | 2016-05-06 20:02:56 +0200 | [diff] [blame] | 372 | rqd->dev = dev; |
| 373 | rqd->opcode = opcode; |
| 374 | rqd->flags = flags; |
| 375 | rqd->bio = bio; |
| 376 | rqd->wait = &wait; |
| 377 | rqd->end_io = nvm_end_io_sync; |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 378 | |
Matias Bjørling | 1145e63 | 2016-05-06 20:02:56 +0200 | [diff] [blame] | 379 | ret = dev->ops->submit_io(dev, rqd); |
Matias Bjørling | ecfb40c | 2016-05-06 20:02:55 +0200 | [diff] [blame] | 380 | if (ret) { |
Matias Bjørling | ecfb40c | 2016-05-06 20:02:55 +0200 | [diff] [blame] | 381 | bio_put(bio); |
| 382 | return ret; |
| 383 | } |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 384 | |
| 385 | /* Prevent hang_check timer from firing at us during very long I/O */ |
| 386 | hang_check = sysctl_hung_task_timeout_secs; |
| 387 | if (hang_check) |
Matias Bjørling | 12624af | 2016-07-07 09:54:13 +0200 | [diff] [blame] | 388 | while (!wait_for_completion_io_timeout(&wait, |
| 389 | hang_check * (HZ/2))) |
| 390 | ; |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 391 | else |
| 392 | wait_for_completion_io(&wait); |
| 393 | |
Matias Bjørling | 1145e63 | 2016-05-06 20:02:56 +0200 | [diff] [blame] | 394 | return rqd->error; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * nvm_submit_ppa_list - submit user-defined ppa list to device. The user must |
| 399 | * take to free ppa list if necessary. |
| 400 | * @dev: device |
| 401 | * @ppa_list: user created ppa_list |
| 402 | * @nr_ppas: length of ppa_list |
| 403 | * @opcode: device opcode |
| 404 | * @flags: device flags |
| 405 | * @buf: data buffer |
| 406 | * @len: data buffer length |
| 407 | */ |
| 408 | int nvm_submit_ppa_list(struct nvm_dev *dev, struct ppa_addr *ppa_list, |
| 409 | int nr_ppas, int opcode, int flags, void *buf, int len) |
| 410 | { |
| 411 | struct nvm_rq rqd; |
| 412 | |
| 413 | if (dev->ops->max_phys_sect < nr_ppas) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 417 | |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 418 | rqd.nr_ppas = nr_ppas; |
Matias Bjørling | 1145e63 | 2016-05-06 20:02:56 +0200 | [diff] [blame] | 419 | if (nr_ppas > 1) |
| 420 | rqd.ppa_list = ppa_list; |
| 421 | else |
| 422 | rqd.ppa_addr = ppa_list[0]; |
| 423 | |
| 424 | return __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len); |
| 425 | } |
| 426 | EXPORT_SYMBOL(nvm_submit_ppa_list); |
| 427 | |
| 428 | /** |
| 429 | * nvm_submit_ppa - submit PPAs to device. PPAs will automatically be unfolded |
| 430 | * as single, dual, quad plane PPAs depending on device type. |
| 431 | * @dev: device |
| 432 | * @ppa: user created ppa_list |
| 433 | * @nr_ppas: length of ppa_list |
| 434 | * @opcode: device opcode |
| 435 | * @flags: device flags |
| 436 | * @buf: data buffer |
| 437 | * @len: data buffer length |
| 438 | */ |
| 439 | int nvm_submit_ppa(struct nvm_dev *dev, struct ppa_addr *ppa, int nr_ppas, |
| 440 | int opcode, int flags, void *buf, int len) |
| 441 | { |
| 442 | struct nvm_rq rqd; |
| 443 | int ret; |
| 444 | |
| 445 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 446 | ret = nvm_set_rqd_ppalist(dev, &rqd, ppa, nr_ppas, 1); |
Matias Bjørling | 1145e63 | 2016-05-06 20:02:56 +0200 | [diff] [blame] | 447 | if (ret) |
| 448 | return ret; |
| 449 | |
| 450 | ret = __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len); |
| 451 | |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 452 | nvm_free_rqd_ppalist(dev, &rqd); |
| 453 | |
Matias Bjørling | 1145e63 | 2016-05-06 20:02:56 +0200 | [diff] [blame] | 454 | return ret; |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 455 | } |
| 456 | EXPORT_SYMBOL(nvm_submit_ppa); |
| 457 | |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 458 | /* |
| 459 | * folds a bad block list from its plane representation to its virtual |
| 460 | * block representation. The fold is done in place and reduced size is |
| 461 | * returned. |
| 462 | * |
| 463 | * If any of the planes status are bad or grown bad block, the virtual block |
| 464 | * is marked bad. If not bad, the first plane state acts as the block state. |
| 465 | */ |
| 466 | int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks) |
| 467 | { |
| 468 | int blk, offset, pl, blktype; |
| 469 | |
| 470 | if (nr_blks != dev->blks_per_lun * dev->plane_mode) |
| 471 | return -EINVAL; |
| 472 | |
| 473 | for (blk = 0; blk < dev->blks_per_lun; blk++) { |
| 474 | offset = blk * dev->plane_mode; |
| 475 | blktype = blks[offset]; |
| 476 | |
| 477 | /* Bad blocks on any planes take precedence over other types */ |
| 478 | for (pl = 0; pl < dev->plane_mode; pl++) { |
| 479 | if (blks[offset + pl] & |
| 480 | (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) { |
| 481 | blktype = blks[offset + pl]; |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | blks[blk] = blktype; |
| 487 | } |
| 488 | |
| 489 | return dev->blks_per_lun; |
| 490 | } |
| 491 | EXPORT_SYMBOL(nvm_bb_tbl_fold); |
| 492 | |
Matias Bjørling | e11903f | 2016-05-06 20:03:05 +0200 | [diff] [blame] | 493 | int nvm_get_bb_tbl(struct nvm_dev *dev, struct ppa_addr ppa, u8 *blks) |
| 494 | { |
| 495 | ppa = generic_to_dev_addr(dev, ppa); |
| 496 | |
| 497 | return dev->ops->get_bb_tbl(dev, ppa, blks); |
| 498 | } |
| 499 | EXPORT_SYMBOL(nvm_get_bb_tbl); |
| 500 | |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 501 | static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp) |
| 502 | { |
| 503 | int i; |
| 504 | |
| 505 | dev->lps_per_blk = dev->pgs_per_blk; |
| 506 | dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL); |
| 507 | if (!dev->lptbl) |
| 508 | return -ENOMEM; |
| 509 | |
| 510 | /* Just a linear array */ |
| 511 | for (i = 0; i < dev->lps_per_blk; i++) |
| 512 | dev->lptbl[i] = i; |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp) |
| 518 | { |
| 519 | int i, p; |
| 520 | struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc; |
| 521 | |
| 522 | if (!mlc->num_pairs) |
| 523 | return 0; |
| 524 | |
| 525 | dev->lps_per_blk = mlc->num_pairs; |
| 526 | dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL); |
| 527 | if (!dev->lptbl) |
| 528 | return -ENOMEM; |
| 529 | |
| 530 | /* The lower page table encoding consists of a list of bytes, where each |
| 531 | * has a lower and an upper half. The first half byte maintains the |
| 532 | * increment value and every value after is an offset added to the |
Matias Bjørling | 12624af | 2016-07-07 09:54:13 +0200 | [diff] [blame] | 533 | * previous incrementation value |
| 534 | */ |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 535 | dev->lptbl[0] = mlc->pairs[0] & 0xF; |
| 536 | for (i = 1; i < dev->lps_per_blk; i++) { |
| 537 | p = mlc->pairs[i >> 1]; |
| 538 | if (i & 0x1) /* upper */ |
| 539 | dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4); |
| 540 | else /* lower */ |
| 541 | dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF); |
| 542 | } |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 547 | static int nvm_core_init(struct nvm_dev *dev) |
| 548 | { |
| 549 | struct nvm_id *id = &dev->identity; |
| 550 | struct nvm_id_group *grp = &id->groups[0]; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 551 | int ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 552 | |
| 553 | /* device values */ |
| 554 | dev->nr_chnls = grp->num_ch; |
| 555 | dev->luns_per_chnl = grp->num_lun; |
| 556 | dev->pgs_per_blk = grp->num_pg; |
| 557 | dev->blks_per_lun = grp->num_blk; |
| 558 | dev->nr_planes = grp->num_pln; |
Matias Bjørling | 4891d12 | 2016-05-06 20:02:57 +0200 | [diff] [blame] | 559 | dev->fpg_size = grp->fpg_sz; |
| 560 | dev->pfpg_size = grp->fpg_sz * grp->num_pln; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 561 | dev->sec_size = grp->csecs; |
| 562 | dev->oob_size = grp->sos; |
| 563 | dev->sec_per_pg = grp->fpg_sz / grp->csecs; |
Matias Bjørling | f9a9995 | 2016-01-12 07:49:34 +0100 | [diff] [blame] | 564 | dev->mccap = grp->mccap; |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 565 | memcpy(&dev->ppaf, &id->ppaf, sizeof(struct nvm_addr_format)); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 566 | |
| 567 | dev->plane_mode = NVM_PLANE_SINGLE; |
| 568 | dev->max_rq_size = dev->ops->max_phys_sect * dev->sec_size; |
| 569 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 570 | if (grp->mpos & 0x020202) |
| 571 | dev->plane_mode = NVM_PLANE_DOUBLE; |
| 572 | if (grp->mpos & 0x040404) |
| 573 | dev->plane_mode = NVM_PLANE_QUAD; |
| 574 | |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 575 | if (grp->mtype != 0) { |
| 576 | pr_err("nvm: memory type not supported\n"); |
| 577 | return -EINVAL; |
| 578 | } |
| 579 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 580 | /* calculated values */ |
| 581 | dev->sec_per_pl = dev->sec_per_pg * dev->nr_planes; |
| 582 | dev->sec_per_blk = dev->sec_per_pl * dev->pgs_per_blk; |
| 583 | dev->sec_per_lun = dev->sec_per_blk * dev->blks_per_lun; |
| 584 | dev->nr_luns = dev->luns_per_chnl * dev->nr_chnls; |
| 585 | |
Matias Bjørling | ed2a92a | 2016-02-20 08:52:42 +0100 | [diff] [blame] | 586 | dev->total_secs = dev->nr_luns * dev->sec_per_lun; |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 587 | dev->lun_map = kcalloc(BITS_TO_LONGS(dev->nr_luns), |
| 588 | sizeof(unsigned long), GFP_KERNEL); |
| 589 | if (!dev->lun_map) |
| 590 | return -ENOMEM; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 591 | |
| 592 | switch (grp->fmtype) { |
| 593 | case NVM_ID_FMTYPE_SLC: |
| 594 | if (nvm_init_slc_tbl(dev, grp)) { |
| 595 | ret = -ENOMEM; |
| 596 | goto err_fmtype; |
| 597 | } |
| 598 | break; |
| 599 | case NVM_ID_FMTYPE_MLC: |
| 600 | if (nvm_init_mlc_tbl(dev, grp)) { |
| 601 | ret = -ENOMEM; |
| 602 | goto err_fmtype; |
| 603 | } |
| 604 | break; |
| 605 | default: |
| 606 | pr_err("nvm: flash type not supported\n"); |
| 607 | ret = -EINVAL; |
| 608 | goto err_fmtype; |
| 609 | } |
| 610 | |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 611 | mutex_init(&dev->mlock); |
Wenwei Tao | 4c9dacb | 2016-03-03 15:06:37 +0100 | [diff] [blame] | 612 | spin_lock_init(&dev->lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 613 | |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 614 | blk_queue_logical_block_size(dev->q, dev->sec_size); |
| 615 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 616 | return 0; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 617 | err_fmtype: |
| 618 | kfree(dev->lun_map); |
| 619 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 620 | } |
| 621 | |
Matias Bjørling | 976bdfc | 2016-05-06 20:03:17 +0200 | [diff] [blame] | 622 | static void nvm_free_mgr(struct nvm_dev *dev) |
| 623 | { |
Matias Bjørling | 976bdfc | 2016-05-06 20:03:17 +0200 | [diff] [blame] | 624 | if (!dev->mt) |
| 625 | return; |
| 626 | |
Matias Bjørling | 976bdfc | 2016-05-06 20:03:17 +0200 | [diff] [blame] | 627 | dev->mt->unregister_mgr(dev); |
| 628 | dev->mt = NULL; |
| 629 | } |
| 630 | |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 631 | void nvm_free(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 632 | { |
| 633 | if (!dev) |
| 634 | return; |
| 635 | |
Matias Bjørling | 976bdfc | 2016-05-06 20:03:17 +0200 | [diff] [blame] | 636 | nvm_free_mgr(dev); |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 637 | |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 638 | if (dev->dma_pool) |
| 639 | dev->ops->destroy_dma_pool(dev->dma_pool); |
| 640 | |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 641 | kfree(dev->lptbl); |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 642 | kfree(dev->lun_map); |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 643 | kfree(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static int nvm_init(struct nvm_dev *dev) |
| 647 | { |
Wenwei Tao | 480fc0d | 2015-11-20 13:47:53 +0100 | [diff] [blame] | 648 | int ret = -EINVAL; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 649 | |
| 650 | if (!dev->q || !dev->ops) |
Wenwei Tao | 480fc0d | 2015-11-20 13:47:53 +0100 | [diff] [blame] | 651 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 652 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 653 | if (dev->ops->identity(dev, &dev->identity)) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 654 | pr_err("nvm: device could not be identified\n"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 655 | goto err; |
| 656 | } |
| 657 | |
| 658 | pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n", |
| 659 | dev->identity.ver_id, dev->identity.vmnt, |
| 660 | dev->identity.cgrps); |
| 661 | |
| 662 | if (dev->identity.ver_id != 1) { |
| 663 | pr_err("nvm: device not supported by kernel."); |
| 664 | goto err; |
| 665 | } |
| 666 | |
| 667 | if (dev->identity.cgrps != 1) { |
| 668 | pr_err("nvm: only one group configuration supported."); |
| 669 | goto err; |
| 670 | } |
| 671 | |
| 672 | ret = nvm_core_init(dev); |
| 673 | if (ret) { |
| 674 | pr_err("nvm: could not initialize core structures.\n"); |
| 675 | goto err; |
| 676 | } |
| 677 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 678 | pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n", |
| 679 | dev->name, dev->sec_per_pg, dev->nr_planes, |
| 680 | dev->pgs_per_blk, dev->blks_per_lun, dev->nr_luns, |
| 681 | dev->nr_chnls); |
| 682 | return 0; |
| 683 | err: |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 684 | pr_err("nvm: failed to initialize nvm\n"); |
| 685 | return ret; |
| 686 | } |
| 687 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 688 | struct nvm_dev *nvm_alloc_dev(int node) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 689 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 690 | return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node); |
| 691 | } |
| 692 | EXPORT_SYMBOL(nvm_alloc_dev); |
| 693 | |
| 694 | int nvm_register(struct nvm_dev *dev) |
| 695 | { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 696 | int ret; |
| 697 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 698 | ret = nvm_init(dev); |
| 699 | if (ret) |
| 700 | goto err_init; |
| 701 | |
Wenwei Tao | d160147 | 2015-11-28 16:49:25 +0100 | [diff] [blame] | 702 | if (dev->ops->max_phys_sect > 256) { |
| 703 | pr_info("nvm: max sectors supported is 256.\n"); |
| 704 | ret = -EINVAL; |
| 705 | goto err_init; |
| 706 | } |
| 707 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 708 | if (dev->ops->max_phys_sect > 1) { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 709 | dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist"); |
| 710 | if (!dev->dma_pool) { |
| 711 | pr_err("nvm: could not create dma pool\n"); |
Matias Bjørling | 93e70c1 | 2015-11-20 13:47:54 +0100 | [diff] [blame] | 712 | ret = -ENOMEM; |
| 713 | goto err_init; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 714 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 717 | if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) { |
| 718 | ret = nvm_get_sysblock(dev, &dev->sb); |
| 719 | if (!ret) |
| 720 | pr_err("nvm: device not initialized.\n"); |
| 721 | else if (ret < 0) |
| 722 | pr_err("nvm: err (%d) on device initialization\n", ret); |
| 723 | } |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 724 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 725 | /* register device with a supported media manager */ |
Matias Bjørling | edad2e6 | 2015-11-16 15:34:42 +0100 | [diff] [blame] | 726 | down_write(&nvm_lock); |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 727 | if (ret > 0) |
| 728 | dev->mt = nvm_init_mgr(dev); |
Matias Bjørling | edad2e6 | 2015-11-16 15:34:42 +0100 | [diff] [blame] | 729 | list_add(&dev->devices, &nvm_devices); |
| 730 | up_write(&nvm_lock); |
| 731 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 732 | return 0; |
| 733 | err_init: |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 734 | kfree(dev->lun_map); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 735 | return ret; |
| 736 | } |
| 737 | EXPORT_SYMBOL(nvm_register); |
| 738 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 739 | void nvm_unregister(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 740 | { |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 741 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 742 | list_del(&dev->devices); |
| 743 | up_write(&nvm_lock); |
Matias Bjørling | c1480ad | 2015-11-16 15:34:43 +0100 | [diff] [blame] | 744 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 745 | nvm_free(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 746 | } |
| 747 | EXPORT_SYMBOL(nvm_unregister); |
| 748 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 749 | static int __nvm_configure_create(struct nvm_ioctl_create *create) |
| 750 | { |
| 751 | struct nvm_dev *dev; |
| 752 | struct nvm_ioctl_create_simple *s; |
| 753 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 754 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 755 | dev = nvm_find_nvm_dev(create->dev); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 756 | up_write(&nvm_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 757 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 758 | if (!dev) { |
| 759 | pr_err("nvm: device not found\n"); |
| 760 | return -EINVAL; |
| 761 | } |
| 762 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 763 | if (!dev->mt) { |
| 764 | pr_info("nvm: device has no media manager registered.\n"); |
| 765 | return -ENODEV; |
| 766 | } |
| 767 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 768 | if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) { |
| 769 | pr_err("nvm: config type not valid\n"); |
| 770 | return -EINVAL; |
| 771 | } |
| 772 | s = &create->conf.s; |
| 773 | |
| 774 | if (s->lun_begin > s->lun_end || s->lun_end > dev->nr_luns) { |
| 775 | pr_err("nvm: lun out of bound (%u:%u > %u)\n", |
| 776 | s->lun_begin, s->lun_end, dev->nr_luns); |
| 777 | return -EINVAL; |
| 778 | } |
| 779 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 780 | return dev->mt->create_tgt(dev, create); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | #ifdef CONFIG_NVM_DEBUG |
| 784 | static int nvm_configure_show(const char *val) |
| 785 | { |
| 786 | struct nvm_dev *dev; |
| 787 | char opcode, devname[DISK_NAME_LEN]; |
| 788 | int ret; |
| 789 | |
| 790 | ret = sscanf(val, "%c %32s", &opcode, devname); |
| 791 | if (ret != 2) { |
| 792 | pr_err("nvm: invalid command. Use \"opcode devicename\".\n"); |
| 793 | return -EINVAL; |
| 794 | } |
| 795 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 796 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 797 | dev = nvm_find_nvm_dev(devname); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 798 | up_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 799 | if (!dev) { |
| 800 | pr_err("nvm: device not found\n"); |
| 801 | return -EINVAL; |
| 802 | } |
| 803 | |
| 804 | if (!dev->mt) |
| 805 | return 0; |
| 806 | |
Javier Gonzalez | 2fde0e4 | 2015-11-20 13:47:57 +0100 | [diff] [blame] | 807 | dev->mt->lun_info_print(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | static int nvm_configure_remove(const char *val) |
| 813 | { |
| 814 | struct nvm_ioctl_remove remove; |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 815 | struct nvm_dev *dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 816 | char opcode; |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 817 | int ret = 0; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 818 | |
| 819 | ret = sscanf(val, "%c %256s", &opcode, remove.tgtname); |
| 820 | if (ret != 2) { |
| 821 | pr_err("nvm: invalid command. Use \"d targetname\".\n"); |
| 822 | return -EINVAL; |
| 823 | } |
| 824 | |
| 825 | remove.flags = 0; |
| 826 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 827 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 828 | ret = dev->mt->remove_tgt(dev, &remove); |
| 829 | if (!ret) |
| 830 | break; |
| 831 | } |
| 832 | |
| 833 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | static int nvm_configure_create(const char *val) |
| 837 | { |
| 838 | struct nvm_ioctl_create create; |
| 839 | char opcode; |
| 840 | int lun_begin, lun_end, ret; |
| 841 | |
| 842 | ret = sscanf(val, "%c %256s %256s %48s %u:%u", &opcode, create.dev, |
| 843 | create.tgtname, create.tgttype, |
| 844 | &lun_begin, &lun_end); |
| 845 | if (ret != 6) { |
| 846 | pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n"); |
| 847 | return -EINVAL; |
| 848 | } |
| 849 | |
| 850 | create.flags = 0; |
| 851 | create.conf.type = NVM_CONFIG_TYPE_SIMPLE; |
| 852 | create.conf.s.lun_begin = lun_begin; |
| 853 | create.conf.s.lun_end = lun_end; |
| 854 | |
| 855 | return __nvm_configure_create(&create); |
| 856 | } |
| 857 | |
| 858 | |
| 859 | /* Exposes administrative interface through /sys/module/lnvm/configure_by_str */ |
| 860 | static int nvm_configure_by_str_event(const char *val, |
| 861 | const struct kernel_param *kp) |
| 862 | { |
| 863 | char opcode; |
| 864 | int ret; |
| 865 | |
| 866 | ret = sscanf(val, "%c", &opcode); |
| 867 | if (ret != 1) { |
| 868 | pr_err("nvm: string must have the format of \"cmd ...\"\n"); |
| 869 | return -EINVAL; |
| 870 | } |
| 871 | |
| 872 | switch (opcode) { |
| 873 | case 'a': |
| 874 | return nvm_configure_create(val); |
| 875 | case 'd': |
| 876 | return nvm_configure_remove(val); |
| 877 | case 's': |
| 878 | return nvm_configure_show(val); |
| 879 | default: |
| 880 | pr_err("nvm: invalid command\n"); |
| 881 | return -EINVAL; |
| 882 | } |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | static int nvm_configure_get(char *buf, const struct kernel_param *kp) |
| 888 | { |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 889 | int sz; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 890 | struct nvm_dev *dev; |
| 891 | |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 892 | sz = sprintf(buf, "available devices:\n"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 893 | down_write(&nvm_lock); |
| 894 | list_for_each_entry(dev, &nvm_devices, devices) { |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 895 | if (sz > 4095 - DISK_NAME_LEN - 2) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 896 | break; |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 897 | sz += sprintf(buf + sz, " %32s\n", dev->name); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 898 | } |
| 899 | up_write(&nvm_lock); |
| 900 | |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 901 | return sz; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static const struct kernel_param_ops nvm_configure_by_str_event_param_ops = { |
| 905 | .set = nvm_configure_by_str_event, |
| 906 | .get = nvm_configure_get, |
| 907 | }; |
| 908 | |
| 909 | #undef MODULE_PARAM_PREFIX |
| 910 | #define MODULE_PARAM_PREFIX "lnvm." |
| 911 | |
| 912 | module_param_cb(configure_debug, &nvm_configure_by_str_event_param_ops, NULL, |
| 913 | 0644); |
| 914 | |
| 915 | #endif /* CONFIG_NVM_DEBUG */ |
| 916 | |
| 917 | static long nvm_ioctl_info(struct file *file, void __user *arg) |
| 918 | { |
| 919 | struct nvm_ioctl_info *info; |
| 920 | struct nvm_tgt_type *tt; |
| 921 | int tgt_iter = 0; |
| 922 | |
| 923 | if (!capable(CAP_SYS_ADMIN)) |
| 924 | return -EPERM; |
| 925 | |
| 926 | info = memdup_user(arg, sizeof(struct nvm_ioctl_info)); |
| 927 | if (IS_ERR(info)) |
| 928 | return -EFAULT; |
| 929 | |
| 930 | info->version[0] = NVM_VERSION_MAJOR; |
| 931 | info->version[1] = NVM_VERSION_MINOR; |
| 932 | info->version[2] = NVM_VERSION_PATCH; |
| 933 | |
| 934 | down_write(&nvm_lock); |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 935 | list_for_each_entry(tt, &nvm_tgt_types, list) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 936 | struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter]; |
| 937 | |
| 938 | tgt->version[0] = tt->version[0]; |
| 939 | tgt->version[1] = tt->version[1]; |
| 940 | tgt->version[2] = tt->version[2]; |
| 941 | strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX); |
| 942 | |
| 943 | tgt_iter++; |
| 944 | } |
| 945 | |
| 946 | info->tgtsize = tgt_iter; |
| 947 | up_write(&nvm_lock); |
| 948 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 949 | if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) { |
| 950 | kfree(info); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 951 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 952 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 953 | |
| 954 | kfree(info); |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | static long nvm_ioctl_get_devices(struct file *file, void __user *arg) |
| 959 | { |
| 960 | struct nvm_ioctl_get_devices *devices; |
| 961 | struct nvm_dev *dev; |
| 962 | int i = 0; |
| 963 | |
| 964 | if (!capable(CAP_SYS_ADMIN)) |
| 965 | return -EPERM; |
| 966 | |
| 967 | devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL); |
| 968 | if (!devices) |
| 969 | return -ENOMEM; |
| 970 | |
| 971 | down_write(&nvm_lock); |
| 972 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 973 | struct nvm_ioctl_device_info *info = &devices->info[i]; |
| 974 | |
| 975 | sprintf(info->devname, "%s", dev->name); |
| 976 | if (dev->mt) { |
| 977 | info->bmversion[0] = dev->mt->version[0]; |
| 978 | info->bmversion[1] = dev->mt->version[1]; |
| 979 | info->bmversion[2] = dev->mt->version[2]; |
| 980 | sprintf(info->bmname, "%s", dev->mt->name); |
| 981 | } else { |
| 982 | sprintf(info->bmname, "none"); |
| 983 | } |
| 984 | |
| 985 | i++; |
| 986 | if (i > 31) { |
| 987 | pr_err("nvm: max 31 devices can be reported.\n"); |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | up_write(&nvm_lock); |
| 992 | |
| 993 | devices->nr_devices = i; |
| 994 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 995 | if (copy_to_user(arg, devices, |
| 996 | sizeof(struct nvm_ioctl_get_devices))) { |
| 997 | kfree(devices); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 998 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 999 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1000 | |
| 1001 | kfree(devices); |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | static long nvm_ioctl_dev_create(struct file *file, void __user *arg) |
| 1006 | { |
| 1007 | struct nvm_ioctl_create create; |
| 1008 | |
| 1009 | if (!capable(CAP_SYS_ADMIN)) |
| 1010 | return -EPERM; |
| 1011 | |
| 1012 | if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create))) |
| 1013 | return -EFAULT; |
| 1014 | |
| 1015 | create.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1016 | create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0'; |
| 1017 | create.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1018 | |
| 1019 | if (create.flags != 0) { |
| 1020 | pr_err("nvm: no flags supported\n"); |
| 1021 | return -EINVAL; |
| 1022 | } |
| 1023 | |
| 1024 | return __nvm_configure_create(&create); |
| 1025 | } |
| 1026 | |
| 1027 | static long nvm_ioctl_dev_remove(struct file *file, void __user *arg) |
| 1028 | { |
| 1029 | struct nvm_ioctl_remove remove; |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1030 | struct nvm_dev *dev; |
| 1031 | int ret = 0; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1032 | |
| 1033 | if (!capable(CAP_SYS_ADMIN)) |
| 1034 | return -EPERM; |
| 1035 | |
| 1036 | if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove))) |
| 1037 | return -EFAULT; |
| 1038 | |
| 1039 | remove.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1040 | |
| 1041 | if (remove.flags != 0) { |
| 1042 | pr_err("nvm: no flags supported\n"); |
| 1043 | return -EINVAL; |
| 1044 | } |
| 1045 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1046 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 1047 | ret = dev->mt->remove_tgt(dev, &remove); |
| 1048 | if (!ret) |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1055 | static void nvm_setup_nvm_sb_info(struct nvm_sb_info *info) |
| 1056 | { |
| 1057 | info->seqnr = 1; |
| 1058 | info->erase_cnt = 0; |
| 1059 | info->version = 1; |
| 1060 | } |
| 1061 | |
| 1062 | static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init *init) |
| 1063 | { |
| 1064 | struct nvm_dev *dev; |
| 1065 | struct nvm_sb_info info; |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 1066 | int ret; |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1067 | |
| 1068 | down_write(&nvm_lock); |
| 1069 | dev = nvm_find_nvm_dev(init->dev); |
| 1070 | up_write(&nvm_lock); |
| 1071 | if (!dev) { |
| 1072 | pr_err("nvm: device not found\n"); |
| 1073 | return -EINVAL; |
| 1074 | } |
| 1075 | |
| 1076 | nvm_setup_nvm_sb_info(&info); |
| 1077 | |
| 1078 | strncpy(info.mmtype, init->mmtype, NVM_MMTYPE_LEN); |
| 1079 | info.fs_ppa.ppa = -1; |
| 1080 | |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1081 | if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) { |
| 1082 | ret = nvm_init_sysblock(dev, &info); |
| 1083 | if (ret) |
| 1084 | return ret; |
| 1085 | } |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 1086 | |
| 1087 | memcpy(&dev->sb, &info, sizeof(struct nvm_sb_info)); |
| 1088 | |
| 1089 | down_write(&nvm_lock); |
| 1090 | dev->mt = nvm_init_mgr(dev); |
| 1091 | up_write(&nvm_lock); |
| 1092 | |
| 1093 | return 0; |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | static long nvm_ioctl_dev_init(struct file *file, void __user *arg) |
| 1097 | { |
| 1098 | struct nvm_ioctl_dev_init init; |
| 1099 | |
| 1100 | if (!capable(CAP_SYS_ADMIN)) |
| 1101 | return -EPERM; |
| 1102 | |
| 1103 | if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init))) |
| 1104 | return -EFAULT; |
| 1105 | |
| 1106 | if (init.flags != 0) { |
| 1107 | pr_err("nvm: no flags supported\n"); |
| 1108 | return -EINVAL; |
| 1109 | } |
| 1110 | |
| 1111 | init.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1112 | |
| 1113 | return __nvm_ioctl_dev_init(&init); |
| 1114 | } |
| 1115 | |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1116 | static long nvm_ioctl_dev_factory(struct file *file, void __user *arg) |
| 1117 | { |
| 1118 | struct nvm_ioctl_dev_factory fact; |
| 1119 | struct nvm_dev *dev; |
| 1120 | |
| 1121 | if (!capable(CAP_SYS_ADMIN)) |
| 1122 | return -EPERM; |
| 1123 | |
| 1124 | if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory))) |
| 1125 | return -EFAULT; |
| 1126 | |
| 1127 | fact.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1128 | |
| 1129 | if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1)) |
| 1130 | return -EINVAL; |
| 1131 | |
| 1132 | down_write(&nvm_lock); |
| 1133 | dev = nvm_find_nvm_dev(fact.dev); |
| 1134 | up_write(&nvm_lock); |
| 1135 | if (!dev) { |
| 1136 | pr_err("nvm: device not found\n"); |
| 1137 | return -EINVAL; |
| 1138 | } |
| 1139 | |
Matias Bjørling | 976bdfc | 2016-05-06 20:03:17 +0200 | [diff] [blame] | 1140 | nvm_free_mgr(dev); |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1141 | |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1142 | if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) |
| 1143 | return nvm_dev_factory(dev, fact.flags); |
| 1144 | |
| 1145 | return 0; |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1148 | static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg) |
| 1149 | { |
| 1150 | void __user *argp = (void __user *)arg; |
| 1151 | |
| 1152 | switch (cmd) { |
| 1153 | case NVM_INFO: |
| 1154 | return nvm_ioctl_info(file, argp); |
| 1155 | case NVM_GET_DEVICES: |
| 1156 | return nvm_ioctl_get_devices(file, argp); |
| 1157 | case NVM_DEV_CREATE: |
| 1158 | return nvm_ioctl_dev_create(file, argp); |
| 1159 | case NVM_DEV_REMOVE: |
| 1160 | return nvm_ioctl_dev_remove(file, argp); |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1161 | case NVM_DEV_INIT: |
| 1162 | return nvm_ioctl_dev_init(file, argp); |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1163 | case NVM_DEV_FACTORY: |
| 1164 | return nvm_ioctl_dev_factory(file, argp); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1165 | } |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | static const struct file_operations _ctl_fops = { |
| 1170 | .open = nonseekable_open, |
| 1171 | .unlocked_ioctl = nvm_ctl_ioctl, |
| 1172 | .owner = THIS_MODULE, |
| 1173 | .llseek = noop_llseek, |
| 1174 | }; |
| 1175 | |
| 1176 | static struct miscdevice _nvm_misc = { |
| 1177 | .minor = MISC_DYNAMIC_MINOR, |
| 1178 | .name = "lightnvm", |
| 1179 | .nodename = "lightnvm/control", |
| 1180 | .fops = &_ctl_fops, |
| 1181 | }; |
PrasannaKumar Muralidharan | d6a38c0 | 2016-08-25 22:30:50 +0530 | [diff] [blame] | 1182 | module_misc_device(_nvm_misc); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1183 | |
| 1184 | MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR); |
| 1185 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1186 | MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>"); |
| 1187 | MODULE_LICENSE("GPL v2"); |
| 1188 | MODULE_VERSION("0.1"); |