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