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 | |
Matias Bjørling | d5bdec8 | 2016-02-19 13:56:58 +0100 | [diff] [blame] | 253 | plane_cnt = dev->plane_mode; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 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 | |
Matias Bjørling | ed2a92a | 2016-02-20 08:52:42 +0100 | [diff] [blame] | 466 | dev->total_secs = dev->nr_luns * dev->sec_per_lun; |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 467 | dev->lun_map = kcalloc(BITS_TO_LONGS(dev->nr_luns), |
| 468 | sizeof(unsigned long), GFP_KERNEL); |
| 469 | if (!dev->lun_map) |
| 470 | return -ENOMEM; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 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); |
Wenwei Tao | 4c9dacb | 2016-03-03 15:06:37 +0100 | [diff] [blame] | 473 | spin_lock_init(&dev->lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | static void nvm_free(struct nvm_dev *dev) |
| 479 | { |
| 480 | if (!dev) |
| 481 | return; |
| 482 | |
| 483 | if (dev->mt) |
| 484 | dev->mt->unregister_mgr(dev); |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 485 | |
| 486 | kfree(dev->lptbl); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static int nvm_init(struct nvm_dev *dev) |
| 490 | { |
Wenwei Tao | 480fc0d | 2015-11-20 13:47:53 +0100 | [diff] [blame] | 491 | int ret = -EINVAL; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 492 | |
| 493 | if (!dev->q || !dev->ops) |
Wenwei Tao | 480fc0d | 2015-11-20 13:47:53 +0100 | [diff] [blame] | 494 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 495 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 496 | if (dev->ops->identity(dev, &dev->identity)) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 497 | pr_err("nvm: device could not be identified\n"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 498 | goto err; |
| 499 | } |
| 500 | |
| 501 | pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n", |
| 502 | dev->identity.ver_id, dev->identity.vmnt, |
| 503 | dev->identity.cgrps); |
| 504 | |
| 505 | if (dev->identity.ver_id != 1) { |
| 506 | pr_err("nvm: device not supported by kernel."); |
| 507 | goto err; |
| 508 | } |
| 509 | |
| 510 | if (dev->identity.cgrps != 1) { |
| 511 | pr_err("nvm: only one group configuration supported."); |
| 512 | goto err; |
| 513 | } |
| 514 | |
| 515 | ret = nvm_core_init(dev); |
| 516 | if (ret) { |
| 517 | pr_err("nvm: could not initialize core structures.\n"); |
| 518 | goto err; |
| 519 | } |
| 520 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 521 | pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n", |
| 522 | dev->name, dev->sec_per_pg, dev->nr_planes, |
| 523 | dev->pgs_per_blk, dev->blks_per_lun, dev->nr_luns, |
| 524 | dev->nr_chnls); |
| 525 | return 0; |
| 526 | err: |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 527 | pr_err("nvm: failed to initialize nvm\n"); |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | static void nvm_exit(struct nvm_dev *dev) |
| 532 | { |
| 533 | if (dev->ppalist_pool) |
| 534 | dev->ops->destroy_dma_pool(dev->ppalist_pool); |
| 535 | nvm_free(dev); |
| 536 | |
| 537 | pr_info("nvm: successfully unloaded\n"); |
| 538 | } |
| 539 | |
| 540 | int nvm_register(struct request_queue *q, char *disk_name, |
| 541 | struct nvm_dev_ops *ops) |
| 542 | { |
| 543 | struct nvm_dev *dev; |
| 544 | int ret; |
| 545 | |
| 546 | if (!ops->identity) |
| 547 | return -EINVAL; |
| 548 | |
| 549 | dev = kzalloc(sizeof(struct nvm_dev), GFP_KERNEL); |
| 550 | if (!dev) |
| 551 | return -ENOMEM; |
| 552 | |
| 553 | dev->q = q; |
| 554 | dev->ops = ops; |
| 555 | strncpy(dev->name, disk_name, DISK_NAME_LEN); |
| 556 | |
| 557 | ret = nvm_init(dev); |
| 558 | if (ret) |
| 559 | goto err_init; |
| 560 | |
Wenwei Tao | d160147 | 2015-11-28 16:49:25 +0100 | [diff] [blame] | 561 | if (dev->ops->max_phys_sect > 256) { |
| 562 | pr_info("nvm: max sectors supported is 256.\n"); |
| 563 | ret = -EINVAL; |
| 564 | goto err_init; |
| 565 | } |
| 566 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 567 | if (dev->ops->max_phys_sect > 1) { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 568 | dev->ppalist_pool = dev->ops->create_dma_pool(dev, "ppalist"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 569 | if (!dev->ppalist_pool) { |
| 570 | pr_err("nvm: could not create ppa pool\n"); |
Matias Bjørling | 93e70c1 | 2015-11-20 13:47:54 +0100 | [diff] [blame] | 571 | ret = -ENOMEM; |
| 572 | goto err_init; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 573 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 574 | } |
| 575 | |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 576 | if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) { |
| 577 | ret = nvm_get_sysblock(dev, &dev->sb); |
| 578 | if (!ret) |
| 579 | pr_err("nvm: device not initialized.\n"); |
| 580 | else if (ret < 0) |
| 581 | pr_err("nvm: err (%d) on device initialization\n", ret); |
| 582 | } |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 583 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 584 | /* register device with a supported media manager */ |
Matias Bjørling | edad2e6 | 2015-11-16 15:34:42 +0100 | [diff] [blame] | 585 | down_write(&nvm_lock); |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 586 | if (ret > 0) |
| 587 | dev->mt = nvm_init_mgr(dev); |
Matias Bjørling | edad2e6 | 2015-11-16 15:34:42 +0100 | [diff] [blame] | 588 | list_add(&dev->devices, &nvm_devices); |
| 589 | up_write(&nvm_lock); |
| 590 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 591 | return 0; |
| 592 | err_init: |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 593 | kfree(dev->lun_map); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 594 | kfree(dev); |
| 595 | return ret; |
| 596 | } |
| 597 | EXPORT_SYMBOL(nvm_register); |
| 598 | |
| 599 | void nvm_unregister(char *disk_name) |
| 600 | { |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 601 | struct nvm_dev *dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 602 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 603 | down_write(&nvm_lock); |
| 604 | dev = nvm_find_nvm_dev(disk_name); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 605 | if (!dev) { |
| 606 | pr_err("nvm: could not find device %s to unregister\n", |
| 607 | disk_name); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 608 | up_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 609 | return; |
| 610 | } |
| 611 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 612 | list_del(&dev->devices); |
| 613 | up_write(&nvm_lock); |
Matias Bjørling | c1480ad | 2015-11-16 15:34:43 +0100 | [diff] [blame] | 614 | |
| 615 | nvm_exit(dev); |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 616 | kfree(dev->lun_map); |
Matias Bjørling | c1480ad | 2015-11-16 15:34:43 +0100 | [diff] [blame] | 617 | kfree(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 618 | } |
| 619 | EXPORT_SYMBOL(nvm_unregister); |
| 620 | |
| 621 | static const struct block_device_operations nvm_fops = { |
| 622 | .owner = THIS_MODULE, |
| 623 | }; |
| 624 | |
| 625 | static int nvm_create_target(struct nvm_dev *dev, |
| 626 | struct nvm_ioctl_create *create) |
| 627 | { |
| 628 | struct nvm_ioctl_create_simple *s = &create->conf.s; |
| 629 | struct request_queue *tqueue; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 630 | struct gendisk *tdisk; |
| 631 | struct nvm_tgt_type *tt; |
| 632 | struct nvm_target *t; |
| 633 | void *targetdata; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 634 | |
| 635 | if (!dev->mt) { |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 636 | pr_info("nvm: device has no media manager registered.\n"); |
| 637 | return -ENODEV; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 640 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 641 | tt = nvm_find_target_type(create->tgttype); |
| 642 | if (!tt) { |
| 643 | pr_err("nvm: target type %s not found\n", create->tgttype); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 644 | up_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 645 | return -EINVAL; |
| 646 | } |
| 647 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 648 | list_for_each_entry(t, &dev->online_targets, list) { |
| 649 | if (!strcmp(create->tgtname, t->disk->disk_name)) { |
| 650 | pr_err("nvm: target name already exists.\n"); |
| 651 | up_write(&nvm_lock); |
| 652 | return -EINVAL; |
| 653 | } |
| 654 | } |
| 655 | up_write(&nvm_lock); |
| 656 | |
| 657 | t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL); |
| 658 | if (!t) |
| 659 | return -ENOMEM; |
| 660 | |
| 661 | tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node); |
| 662 | if (!tqueue) |
| 663 | goto err_t; |
| 664 | blk_queue_make_request(tqueue, tt->make_rq); |
| 665 | |
| 666 | tdisk = alloc_disk(0); |
| 667 | if (!tdisk) |
| 668 | goto err_queue; |
| 669 | |
| 670 | sprintf(tdisk->disk_name, "%s", create->tgtname); |
| 671 | tdisk->flags = GENHD_FL_EXT_DEVT; |
| 672 | tdisk->major = 0; |
| 673 | tdisk->first_minor = 0; |
| 674 | tdisk->fops = &nvm_fops; |
| 675 | tdisk->queue = tqueue; |
| 676 | |
| 677 | targetdata = tt->init(dev, tdisk, s->lun_begin, s->lun_end); |
| 678 | if (IS_ERR(targetdata)) |
| 679 | goto err_init; |
| 680 | |
| 681 | tdisk->private_data = targetdata; |
| 682 | tqueue->queuedata = targetdata; |
| 683 | |
| 684 | blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect); |
| 685 | |
| 686 | set_capacity(tdisk, tt->capacity(targetdata)); |
| 687 | add_disk(tdisk); |
| 688 | |
| 689 | t->type = tt; |
| 690 | t->disk = tdisk; |
| 691 | |
| 692 | down_write(&nvm_lock); |
| 693 | list_add_tail(&t->list, &dev->online_targets); |
| 694 | up_write(&nvm_lock); |
| 695 | |
| 696 | return 0; |
| 697 | err_init: |
| 698 | put_disk(tdisk); |
| 699 | err_queue: |
| 700 | blk_cleanup_queue(tqueue); |
| 701 | err_t: |
| 702 | kfree(t); |
| 703 | return -ENOMEM; |
| 704 | } |
| 705 | |
| 706 | static void nvm_remove_target(struct nvm_target *t) |
| 707 | { |
| 708 | struct nvm_tgt_type *tt = t->type; |
| 709 | struct gendisk *tdisk = t->disk; |
| 710 | struct request_queue *q = tdisk->queue; |
| 711 | |
| 712 | lockdep_assert_held(&nvm_lock); |
| 713 | |
| 714 | del_gendisk(tdisk); |
Javier González | d09f958 | 2015-11-16 15:34:47 +0100 | [diff] [blame] | 715 | blk_cleanup_queue(q); |
| 716 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 717 | if (tt->exit) |
| 718 | tt->exit(tdisk->private_data); |
| 719 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 720 | put_disk(tdisk); |
| 721 | |
| 722 | list_del(&t->list); |
| 723 | kfree(t); |
| 724 | } |
| 725 | |
| 726 | static int __nvm_configure_create(struct nvm_ioctl_create *create) |
| 727 | { |
| 728 | struct nvm_dev *dev; |
| 729 | struct nvm_ioctl_create_simple *s; |
| 730 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 731 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 732 | dev = nvm_find_nvm_dev(create->dev); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 733 | up_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 734 | if (!dev) { |
| 735 | pr_err("nvm: device not found\n"); |
| 736 | return -EINVAL; |
| 737 | } |
| 738 | |
| 739 | if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) { |
| 740 | pr_err("nvm: config type not valid\n"); |
| 741 | return -EINVAL; |
| 742 | } |
| 743 | s = &create->conf.s; |
| 744 | |
| 745 | if (s->lun_begin > s->lun_end || s->lun_end > dev->nr_luns) { |
| 746 | pr_err("nvm: lun out of bound (%u:%u > %u)\n", |
| 747 | s->lun_begin, s->lun_end, dev->nr_luns); |
| 748 | return -EINVAL; |
| 749 | } |
| 750 | |
| 751 | return nvm_create_target(dev, create); |
| 752 | } |
| 753 | |
| 754 | static int __nvm_configure_remove(struct nvm_ioctl_remove *remove) |
| 755 | { |
| 756 | struct nvm_target *t = NULL; |
| 757 | struct nvm_dev *dev; |
| 758 | int ret = -1; |
| 759 | |
| 760 | down_write(&nvm_lock); |
| 761 | list_for_each_entry(dev, &nvm_devices, devices) |
| 762 | list_for_each_entry(t, &dev->online_targets, list) { |
| 763 | if (!strcmp(remove->tgtname, t->disk->disk_name)) { |
| 764 | nvm_remove_target(t); |
| 765 | ret = 0; |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | up_write(&nvm_lock); |
| 770 | |
| 771 | if (ret) { |
| 772 | pr_err("nvm: target \"%s\" doesn't exist.\n", remove->tgtname); |
| 773 | return -EINVAL; |
| 774 | } |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | #ifdef CONFIG_NVM_DEBUG |
| 780 | static int nvm_configure_show(const char *val) |
| 781 | { |
| 782 | struct nvm_dev *dev; |
| 783 | char opcode, devname[DISK_NAME_LEN]; |
| 784 | int ret; |
| 785 | |
| 786 | ret = sscanf(val, "%c %32s", &opcode, devname); |
| 787 | if (ret != 2) { |
| 788 | pr_err("nvm: invalid command. Use \"opcode devicename\".\n"); |
| 789 | return -EINVAL; |
| 790 | } |
| 791 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 792 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 793 | dev = nvm_find_nvm_dev(devname); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 794 | up_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 795 | if (!dev) { |
| 796 | pr_err("nvm: device not found\n"); |
| 797 | return -EINVAL; |
| 798 | } |
| 799 | |
| 800 | if (!dev->mt) |
| 801 | return 0; |
| 802 | |
Javier Gonzalez | 2fde0e4 | 2015-11-20 13:47:57 +0100 | [diff] [blame] | 803 | dev->mt->lun_info_print(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static int nvm_configure_remove(const char *val) |
| 809 | { |
| 810 | struct nvm_ioctl_remove remove; |
| 811 | char opcode; |
| 812 | int ret; |
| 813 | |
| 814 | ret = sscanf(val, "%c %256s", &opcode, remove.tgtname); |
| 815 | if (ret != 2) { |
| 816 | pr_err("nvm: invalid command. Use \"d targetname\".\n"); |
| 817 | return -EINVAL; |
| 818 | } |
| 819 | |
| 820 | remove.flags = 0; |
| 821 | |
| 822 | return __nvm_configure_remove(&remove); |
| 823 | } |
| 824 | |
| 825 | static int nvm_configure_create(const char *val) |
| 826 | { |
| 827 | struct nvm_ioctl_create create; |
| 828 | char opcode; |
| 829 | int lun_begin, lun_end, ret; |
| 830 | |
| 831 | ret = sscanf(val, "%c %256s %256s %48s %u:%u", &opcode, create.dev, |
| 832 | create.tgtname, create.tgttype, |
| 833 | &lun_begin, &lun_end); |
| 834 | if (ret != 6) { |
| 835 | pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n"); |
| 836 | return -EINVAL; |
| 837 | } |
| 838 | |
| 839 | create.flags = 0; |
| 840 | create.conf.type = NVM_CONFIG_TYPE_SIMPLE; |
| 841 | create.conf.s.lun_begin = lun_begin; |
| 842 | create.conf.s.lun_end = lun_end; |
| 843 | |
| 844 | return __nvm_configure_create(&create); |
| 845 | } |
| 846 | |
| 847 | |
| 848 | /* Exposes administrative interface through /sys/module/lnvm/configure_by_str */ |
| 849 | static int nvm_configure_by_str_event(const char *val, |
| 850 | const struct kernel_param *kp) |
| 851 | { |
| 852 | char opcode; |
| 853 | int ret; |
| 854 | |
| 855 | ret = sscanf(val, "%c", &opcode); |
| 856 | if (ret != 1) { |
| 857 | pr_err("nvm: string must have the format of \"cmd ...\"\n"); |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | |
| 861 | switch (opcode) { |
| 862 | case 'a': |
| 863 | return nvm_configure_create(val); |
| 864 | case 'd': |
| 865 | return nvm_configure_remove(val); |
| 866 | case 's': |
| 867 | return nvm_configure_show(val); |
| 868 | default: |
| 869 | pr_err("nvm: invalid command\n"); |
| 870 | return -EINVAL; |
| 871 | } |
| 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | static int nvm_configure_get(char *buf, const struct kernel_param *kp) |
| 877 | { |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 878 | int sz; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 879 | struct nvm_dev *dev; |
| 880 | |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 881 | sz = sprintf(buf, "available devices:\n"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 882 | down_write(&nvm_lock); |
| 883 | list_for_each_entry(dev, &nvm_devices, devices) { |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 884 | if (sz > 4095 - DISK_NAME_LEN - 2) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 885 | break; |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 886 | sz += sprintf(buf + sz, " %32s\n", dev->name); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 887 | } |
| 888 | up_write(&nvm_lock); |
| 889 | |
Alan | 5e422cf | 2016-02-19 13:56:57 +0100 | [diff] [blame] | 890 | return sz; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static const struct kernel_param_ops nvm_configure_by_str_event_param_ops = { |
| 894 | .set = nvm_configure_by_str_event, |
| 895 | .get = nvm_configure_get, |
| 896 | }; |
| 897 | |
| 898 | #undef MODULE_PARAM_PREFIX |
| 899 | #define MODULE_PARAM_PREFIX "lnvm." |
| 900 | |
| 901 | module_param_cb(configure_debug, &nvm_configure_by_str_event_param_ops, NULL, |
| 902 | 0644); |
| 903 | |
| 904 | #endif /* CONFIG_NVM_DEBUG */ |
| 905 | |
| 906 | static long nvm_ioctl_info(struct file *file, void __user *arg) |
| 907 | { |
| 908 | struct nvm_ioctl_info *info; |
| 909 | struct nvm_tgt_type *tt; |
| 910 | int tgt_iter = 0; |
| 911 | |
| 912 | if (!capable(CAP_SYS_ADMIN)) |
| 913 | return -EPERM; |
| 914 | |
| 915 | info = memdup_user(arg, sizeof(struct nvm_ioctl_info)); |
| 916 | if (IS_ERR(info)) |
| 917 | return -EFAULT; |
| 918 | |
| 919 | info->version[0] = NVM_VERSION_MAJOR; |
| 920 | info->version[1] = NVM_VERSION_MINOR; |
| 921 | info->version[2] = NVM_VERSION_PATCH; |
| 922 | |
| 923 | down_write(&nvm_lock); |
| 924 | list_for_each_entry(tt, &nvm_targets, list) { |
| 925 | struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter]; |
| 926 | |
| 927 | tgt->version[0] = tt->version[0]; |
| 928 | tgt->version[1] = tt->version[1]; |
| 929 | tgt->version[2] = tt->version[2]; |
| 930 | strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX); |
| 931 | |
| 932 | tgt_iter++; |
| 933 | } |
| 934 | |
| 935 | info->tgtsize = tgt_iter; |
| 936 | up_write(&nvm_lock); |
| 937 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 938 | if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) { |
| 939 | kfree(info); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 940 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 941 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 942 | |
| 943 | kfree(info); |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | static long nvm_ioctl_get_devices(struct file *file, void __user *arg) |
| 948 | { |
| 949 | struct nvm_ioctl_get_devices *devices; |
| 950 | struct nvm_dev *dev; |
| 951 | int i = 0; |
| 952 | |
| 953 | if (!capable(CAP_SYS_ADMIN)) |
| 954 | return -EPERM; |
| 955 | |
| 956 | devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL); |
| 957 | if (!devices) |
| 958 | return -ENOMEM; |
| 959 | |
| 960 | down_write(&nvm_lock); |
| 961 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 962 | struct nvm_ioctl_device_info *info = &devices->info[i]; |
| 963 | |
| 964 | sprintf(info->devname, "%s", dev->name); |
| 965 | if (dev->mt) { |
| 966 | info->bmversion[0] = dev->mt->version[0]; |
| 967 | info->bmversion[1] = dev->mt->version[1]; |
| 968 | info->bmversion[2] = dev->mt->version[2]; |
| 969 | sprintf(info->bmname, "%s", dev->mt->name); |
| 970 | } else { |
| 971 | sprintf(info->bmname, "none"); |
| 972 | } |
| 973 | |
| 974 | i++; |
| 975 | if (i > 31) { |
| 976 | pr_err("nvm: max 31 devices can be reported.\n"); |
| 977 | break; |
| 978 | } |
| 979 | } |
| 980 | up_write(&nvm_lock); |
| 981 | |
| 982 | devices->nr_devices = i; |
| 983 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 984 | if (copy_to_user(arg, devices, |
| 985 | sizeof(struct nvm_ioctl_get_devices))) { |
| 986 | kfree(devices); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 987 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 988 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 989 | |
| 990 | kfree(devices); |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | static long nvm_ioctl_dev_create(struct file *file, void __user *arg) |
| 995 | { |
| 996 | struct nvm_ioctl_create create; |
| 997 | |
| 998 | if (!capable(CAP_SYS_ADMIN)) |
| 999 | return -EPERM; |
| 1000 | |
| 1001 | if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create))) |
| 1002 | return -EFAULT; |
| 1003 | |
| 1004 | create.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1005 | create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0'; |
| 1006 | create.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1007 | |
| 1008 | if (create.flags != 0) { |
| 1009 | pr_err("nvm: no flags supported\n"); |
| 1010 | return -EINVAL; |
| 1011 | } |
| 1012 | |
| 1013 | return __nvm_configure_create(&create); |
| 1014 | } |
| 1015 | |
| 1016 | static long nvm_ioctl_dev_remove(struct file *file, void __user *arg) |
| 1017 | { |
| 1018 | struct nvm_ioctl_remove remove; |
| 1019 | |
| 1020 | if (!capable(CAP_SYS_ADMIN)) |
| 1021 | return -EPERM; |
| 1022 | |
| 1023 | if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove))) |
| 1024 | return -EFAULT; |
| 1025 | |
| 1026 | remove.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1027 | |
| 1028 | if (remove.flags != 0) { |
| 1029 | pr_err("nvm: no flags supported\n"); |
| 1030 | return -EINVAL; |
| 1031 | } |
| 1032 | |
| 1033 | return __nvm_configure_remove(&remove); |
| 1034 | } |
| 1035 | |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1036 | static void nvm_setup_nvm_sb_info(struct nvm_sb_info *info) |
| 1037 | { |
| 1038 | info->seqnr = 1; |
| 1039 | info->erase_cnt = 0; |
| 1040 | info->version = 1; |
| 1041 | } |
| 1042 | |
| 1043 | static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init *init) |
| 1044 | { |
| 1045 | struct nvm_dev *dev; |
| 1046 | struct nvm_sb_info info; |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 1047 | int ret; |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1048 | |
| 1049 | down_write(&nvm_lock); |
| 1050 | dev = nvm_find_nvm_dev(init->dev); |
| 1051 | up_write(&nvm_lock); |
| 1052 | if (!dev) { |
| 1053 | pr_err("nvm: device not found\n"); |
| 1054 | return -EINVAL; |
| 1055 | } |
| 1056 | |
| 1057 | nvm_setup_nvm_sb_info(&info); |
| 1058 | |
| 1059 | strncpy(info.mmtype, init->mmtype, NVM_MMTYPE_LEN); |
| 1060 | info.fs_ppa.ppa = -1; |
| 1061 | |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1062 | if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) { |
| 1063 | ret = nvm_init_sysblock(dev, &info); |
| 1064 | if (ret) |
| 1065 | return ret; |
| 1066 | } |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 1067 | |
| 1068 | memcpy(&dev->sb, &info, sizeof(struct nvm_sb_info)); |
| 1069 | |
| 1070 | down_write(&nvm_lock); |
| 1071 | dev->mt = nvm_init_mgr(dev); |
| 1072 | up_write(&nvm_lock); |
| 1073 | |
| 1074 | return 0; |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | static long nvm_ioctl_dev_init(struct file *file, void __user *arg) |
| 1078 | { |
| 1079 | struct nvm_ioctl_dev_init init; |
| 1080 | |
| 1081 | if (!capable(CAP_SYS_ADMIN)) |
| 1082 | return -EPERM; |
| 1083 | |
| 1084 | if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init))) |
| 1085 | return -EFAULT; |
| 1086 | |
| 1087 | if (init.flags != 0) { |
| 1088 | pr_err("nvm: no flags supported\n"); |
| 1089 | return -EINVAL; |
| 1090 | } |
| 1091 | |
| 1092 | init.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1093 | |
| 1094 | return __nvm_ioctl_dev_init(&init); |
| 1095 | } |
| 1096 | |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1097 | static long nvm_ioctl_dev_factory(struct file *file, void __user *arg) |
| 1098 | { |
| 1099 | struct nvm_ioctl_dev_factory fact; |
| 1100 | struct nvm_dev *dev; |
| 1101 | |
| 1102 | if (!capable(CAP_SYS_ADMIN)) |
| 1103 | return -EPERM; |
| 1104 | |
| 1105 | if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory))) |
| 1106 | return -EFAULT; |
| 1107 | |
| 1108 | fact.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1109 | |
| 1110 | if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1)) |
| 1111 | return -EINVAL; |
| 1112 | |
| 1113 | down_write(&nvm_lock); |
| 1114 | dev = nvm_find_nvm_dev(fact.dev); |
| 1115 | up_write(&nvm_lock); |
| 1116 | if (!dev) { |
| 1117 | pr_err("nvm: device not found\n"); |
| 1118 | return -EINVAL; |
| 1119 | } |
| 1120 | |
| 1121 | if (dev->mt) { |
| 1122 | dev->mt->unregister_mgr(dev); |
| 1123 | dev->mt = NULL; |
| 1124 | } |
| 1125 | |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1126 | if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) |
| 1127 | return nvm_dev_factory(dev, fact.flags); |
| 1128 | |
| 1129 | return 0; |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1132 | static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg) |
| 1133 | { |
| 1134 | void __user *argp = (void __user *)arg; |
| 1135 | |
| 1136 | switch (cmd) { |
| 1137 | case NVM_INFO: |
| 1138 | return nvm_ioctl_info(file, argp); |
| 1139 | case NVM_GET_DEVICES: |
| 1140 | return nvm_ioctl_get_devices(file, argp); |
| 1141 | case NVM_DEV_CREATE: |
| 1142 | return nvm_ioctl_dev_create(file, argp); |
| 1143 | case NVM_DEV_REMOVE: |
| 1144 | return nvm_ioctl_dev_remove(file, argp); |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1145 | case NVM_DEV_INIT: |
| 1146 | return nvm_ioctl_dev_init(file, argp); |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1147 | case NVM_DEV_FACTORY: |
| 1148 | return nvm_ioctl_dev_factory(file, argp); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1149 | } |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | static const struct file_operations _ctl_fops = { |
| 1154 | .open = nonseekable_open, |
| 1155 | .unlocked_ioctl = nvm_ctl_ioctl, |
| 1156 | .owner = THIS_MODULE, |
| 1157 | .llseek = noop_llseek, |
| 1158 | }; |
| 1159 | |
| 1160 | static struct miscdevice _nvm_misc = { |
| 1161 | .minor = MISC_DYNAMIC_MINOR, |
| 1162 | .name = "lightnvm", |
| 1163 | .nodename = "lightnvm/control", |
| 1164 | .fops = &_ctl_fops, |
| 1165 | }; |
| 1166 | |
| 1167 | MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR); |
| 1168 | |
| 1169 | static int __init nvm_mod_init(void) |
| 1170 | { |
| 1171 | int ret; |
| 1172 | |
| 1173 | ret = misc_register(&_nvm_misc); |
| 1174 | if (ret) |
| 1175 | pr_err("nvm: misc_register failed for control device"); |
| 1176 | |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
| 1180 | static void __exit nvm_mod_exit(void) |
| 1181 | { |
| 1182 | misc_deregister(&_nvm_misc); |
| 1183 | } |
| 1184 | |
| 1185 | MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>"); |
| 1186 | MODULE_LICENSE("GPL v2"); |
| 1187 | MODULE_VERSION("0.1"); |
| 1188 | module_init(nvm_mod_init); |
| 1189 | module_exit(nvm_mod_exit); |