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