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