Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 IT University of Copenhagen. All rights reserved. |
| 3 | * Initial release: Matias Bjorling <m@bjorling.me> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License version |
| 7 | * 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, |
| 17 | * USA. |
| 18 | * |
| 19 | */ |
| 20 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 21 | #include <linux/list.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/sem.h> |
| 24 | #include <linux/bitmap.h> |
Paul Gortmaker | 389b2a1 | 2016-10-29 16:38:41 -0400 | [diff] [blame] | 25 | #include <linux/moduleparam.h> |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 26 | #include <linux/miscdevice.h> |
| 27 | #include <linux/lightnvm.h> |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 28 | #include <linux/sched/sysctl.h> |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 29 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 30 | static LIST_HEAD(nvm_tgt_types); |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 31 | static DECLARE_RWSEM(nvm_tgtt_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 32 | static LIST_HEAD(nvm_devices); |
| 33 | static DECLARE_RWSEM(nvm_lock); |
| 34 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 35 | /* Map between virtual and physical channel and lun */ |
| 36 | struct nvm_ch_map { |
| 37 | int ch_off; |
| 38 | int nr_luns; |
| 39 | int *lun_offs; |
| 40 | }; |
| 41 | |
| 42 | struct nvm_dev_map { |
| 43 | struct nvm_ch_map *chnls; |
| 44 | int nr_chnls; |
| 45 | }; |
| 46 | |
| 47 | struct nvm_area { |
| 48 | struct list_head list; |
| 49 | sector_t begin; |
| 50 | sector_t end; /* end is excluded */ |
| 51 | }; |
| 52 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 53 | static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name) |
| 54 | { |
| 55 | struct nvm_target *tgt; |
| 56 | |
| 57 | list_for_each_entry(tgt, &dev->targets, list) |
| 58 | if (!strcmp(name, tgt->disk->disk_name)) |
| 59 | return tgt; |
| 60 | |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end) |
| 65 | { |
| 66 | int i; |
| 67 | |
| 68 | for (i = lun_begin; i <= lun_end; i++) { |
| 69 | if (test_and_set_bit(i, dev->lun_map)) { |
| 70 | pr_err("nvm: lun %d already allocated\n", i); |
| 71 | goto err; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | err: |
| 77 | while (--i > lun_begin) |
| 78 | clear_bit(i, dev->lun_map); |
| 79 | |
| 80 | return -EBUSY; |
| 81 | } |
| 82 | |
| 83 | static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin, |
| 84 | int lun_end) |
| 85 | { |
| 86 | int i; |
| 87 | |
| 88 | for (i = lun_begin; i <= lun_end; i++) |
| 89 | WARN_ON(!test_and_clear_bit(i, dev->lun_map)); |
| 90 | } |
| 91 | |
| 92 | static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev) |
| 93 | { |
| 94 | struct nvm_dev *dev = tgt_dev->parent; |
| 95 | struct nvm_dev_map *dev_map = tgt_dev->map; |
| 96 | int i, j; |
| 97 | |
| 98 | for (i = 0; i < dev_map->nr_chnls; i++) { |
| 99 | struct nvm_ch_map *ch_map = &dev_map->chnls[i]; |
| 100 | int *lun_offs = ch_map->lun_offs; |
| 101 | int ch = i + ch_map->ch_off; |
| 102 | |
| 103 | for (j = 0; j < ch_map->nr_luns; j++) { |
| 104 | int lun = j + lun_offs[j]; |
| 105 | int lunid = (ch * dev->geo.luns_per_chnl) + lun; |
| 106 | |
| 107 | WARN_ON(!test_and_clear_bit(lunid, dev->lun_map)); |
| 108 | } |
| 109 | |
| 110 | kfree(ch_map->lun_offs); |
| 111 | } |
| 112 | |
| 113 | kfree(dev_map->chnls); |
| 114 | kfree(dev_map); |
| 115 | |
| 116 | kfree(tgt_dev->luns); |
| 117 | kfree(tgt_dev); |
| 118 | } |
| 119 | |
| 120 | static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev, |
| 121 | int lun_begin, int lun_end) |
| 122 | { |
| 123 | struct nvm_tgt_dev *tgt_dev = NULL; |
| 124 | struct nvm_dev_map *dev_rmap = dev->rmap; |
| 125 | struct nvm_dev_map *dev_map; |
| 126 | struct ppa_addr *luns; |
| 127 | int nr_luns = lun_end - lun_begin + 1; |
| 128 | int luns_left = nr_luns; |
| 129 | int nr_chnls = nr_luns / dev->geo.luns_per_chnl; |
| 130 | int nr_chnls_mod = nr_luns % dev->geo.luns_per_chnl; |
| 131 | int bch = lun_begin / dev->geo.luns_per_chnl; |
| 132 | int blun = lun_begin % dev->geo.luns_per_chnl; |
| 133 | int lunid = 0; |
| 134 | int lun_balanced = 1; |
| 135 | int prev_nr_luns; |
| 136 | int i, j; |
| 137 | |
| 138 | nr_chnls = nr_luns / dev->geo.luns_per_chnl; |
| 139 | nr_chnls = (nr_chnls_mod == 0) ? nr_chnls : nr_chnls + 1; |
| 140 | |
| 141 | dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL); |
| 142 | if (!dev_map) |
| 143 | goto err_dev; |
| 144 | |
| 145 | dev_map->chnls = kcalloc(nr_chnls, sizeof(struct nvm_ch_map), |
| 146 | GFP_KERNEL); |
| 147 | if (!dev_map->chnls) |
| 148 | goto err_chnls; |
| 149 | |
| 150 | luns = kcalloc(nr_luns, sizeof(struct ppa_addr), GFP_KERNEL); |
| 151 | if (!luns) |
| 152 | goto err_luns; |
| 153 | |
| 154 | prev_nr_luns = (luns_left > dev->geo.luns_per_chnl) ? |
| 155 | dev->geo.luns_per_chnl : luns_left; |
| 156 | for (i = 0; i < nr_chnls; i++) { |
| 157 | struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch]; |
| 158 | int *lun_roffs = ch_rmap->lun_offs; |
| 159 | struct nvm_ch_map *ch_map = &dev_map->chnls[i]; |
| 160 | int *lun_offs; |
| 161 | int luns_in_chnl = (luns_left > dev->geo.luns_per_chnl) ? |
| 162 | dev->geo.luns_per_chnl : luns_left; |
| 163 | |
| 164 | if (lun_balanced && prev_nr_luns != luns_in_chnl) |
| 165 | lun_balanced = 0; |
| 166 | |
| 167 | ch_map->ch_off = ch_rmap->ch_off = bch; |
| 168 | ch_map->nr_luns = luns_in_chnl; |
| 169 | |
| 170 | lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL); |
| 171 | if (!lun_offs) |
| 172 | goto err_ch; |
| 173 | |
| 174 | for (j = 0; j < luns_in_chnl; j++) { |
| 175 | luns[lunid].ppa = 0; |
| 176 | luns[lunid].g.ch = i; |
| 177 | luns[lunid++].g.lun = j; |
| 178 | |
| 179 | lun_offs[j] = blun; |
| 180 | lun_roffs[j + blun] = blun; |
| 181 | } |
| 182 | |
| 183 | ch_map->lun_offs = lun_offs; |
| 184 | |
| 185 | /* when starting a new channel, lun offset is reset */ |
| 186 | blun = 0; |
| 187 | luns_left -= luns_in_chnl; |
| 188 | } |
| 189 | |
| 190 | dev_map->nr_chnls = nr_chnls; |
| 191 | |
| 192 | tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL); |
| 193 | if (!tgt_dev) |
| 194 | goto err_ch; |
| 195 | |
| 196 | memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo)); |
| 197 | /* Target device only owns a portion of the physical device */ |
| 198 | tgt_dev->geo.nr_chnls = nr_chnls; |
| 199 | tgt_dev->geo.nr_luns = nr_luns; |
| 200 | tgt_dev->geo.luns_per_chnl = (lun_balanced) ? prev_nr_luns : -1; |
| 201 | tgt_dev->total_secs = nr_luns * tgt_dev->geo.sec_per_lun; |
| 202 | tgt_dev->q = dev->q; |
| 203 | tgt_dev->map = dev_map; |
| 204 | tgt_dev->luns = luns; |
| 205 | memcpy(&tgt_dev->identity, &dev->identity, sizeof(struct nvm_id)); |
| 206 | |
| 207 | tgt_dev->parent = dev; |
| 208 | |
| 209 | return tgt_dev; |
| 210 | err_ch: |
| 211 | while (--i > 0) |
| 212 | kfree(dev_map->chnls[i].lun_offs); |
| 213 | kfree(luns); |
| 214 | err_luns: |
| 215 | kfree(dev_map->chnls); |
| 216 | err_chnls: |
| 217 | kfree(dev_map); |
| 218 | err_dev: |
| 219 | return tgt_dev; |
| 220 | } |
| 221 | |
| 222 | static const struct block_device_operations nvm_fops = { |
| 223 | .owner = THIS_MODULE, |
| 224 | }; |
| 225 | |
| 226 | static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create) |
| 227 | { |
| 228 | struct nvm_ioctl_create_simple *s = &create->conf.s; |
| 229 | struct request_queue *tqueue; |
| 230 | struct gendisk *tdisk; |
| 231 | struct nvm_tgt_type *tt; |
| 232 | struct nvm_target *t; |
| 233 | struct nvm_tgt_dev *tgt_dev; |
| 234 | void *targetdata; |
| 235 | |
| 236 | tt = nvm_find_target_type(create->tgttype, 1); |
| 237 | if (!tt) { |
| 238 | pr_err("nvm: target type %s not found\n", create->tgttype); |
| 239 | return -EINVAL; |
| 240 | } |
| 241 | |
| 242 | mutex_lock(&dev->mlock); |
| 243 | t = nvm_find_target(dev, create->tgtname); |
| 244 | if (t) { |
| 245 | pr_err("nvm: target name already exists.\n"); |
| 246 | mutex_unlock(&dev->mlock); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | mutex_unlock(&dev->mlock); |
| 250 | |
| 251 | if (nvm_reserve_luns(dev, s->lun_begin, s->lun_end)) |
| 252 | return -ENOMEM; |
| 253 | |
| 254 | t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL); |
| 255 | if (!t) |
| 256 | goto err_reserve; |
| 257 | |
| 258 | tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end); |
| 259 | if (!tgt_dev) { |
| 260 | pr_err("nvm: could not create target device\n"); |
| 261 | goto err_t; |
| 262 | } |
| 263 | |
| 264 | tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node); |
| 265 | if (!tqueue) |
| 266 | goto err_dev; |
| 267 | blk_queue_make_request(tqueue, tt->make_rq); |
| 268 | |
| 269 | tdisk = alloc_disk(0); |
| 270 | if (!tdisk) |
| 271 | goto err_queue; |
| 272 | |
| 273 | sprintf(tdisk->disk_name, "%s", create->tgtname); |
| 274 | tdisk->flags = GENHD_FL_EXT_DEVT; |
| 275 | tdisk->major = 0; |
| 276 | tdisk->first_minor = 0; |
| 277 | tdisk->fops = &nvm_fops; |
| 278 | tdisk->queue = tqueue; |
| 279 | |
| 280 | targetdata = tt->init(tgt_dev, tdisk); |
| 281 | if (IS_ERR(targetdata)) |
| 282 | goto err_init; |
| 283 | |
| 284 | tdisk->private_data = targetdata; |
| 285 | tqueue->queuedata = targetdata; |
| 286 | |
| 287 | blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect); |
| 288 | |
| 289 | set_capacity(tdisk, tt->capacity(targetdata)); |
| 290 | add_disk(tdisk); |
| 291 | |
Javier González | 9a69b0e | 2017-01-31 13:17:20 +0100 | [diff] [blame] | 292 | if (tt->sysfs_init && tt->sysfs_init(tdisk)) |
| 293 | goto err_sysfs; |
| 294 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 295 | t->type = tt; |
| 296 | t->disk = tdisk; |
| 297 | t->dev = tgt_dev; |
| 298 | |
| 299 | mutex_lock(&dev->mlock); |
| 300 | list_add_tail(&t->list, &dev->targets); |
| 301 | mutex_unlock(&dev->mlock); |
| 302 | |
| 303 | return 0; |
Javier González | 9a69b0e | 2017-01-31 13:17:20 +0100 | [diff] [blame] | 304 | err_sysfs: |
| 305 | if (tt->exit) |
| 306 | tt->exit(targetdata); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 307 | err_init: |
| 308 | put_disk(tdisk); |
| 309 | err_queue: |
| 310 | blk_cleanup_queue(tqueue); |
| 311 | err_dev: |
Javier González | deccf5a | 2017-01-31 13:17:19 +0100 | [diff] [blame] | 312 | nvm_remove_tgt_dev(tgt_dev); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 313 | err_t: |
| 314 | kfree(t); |
| 315 | err_reserve: |
| 316 | nvm_release_luns_err(dev, s->lun_begin, s->lun_end); |
| 317 | return -ENOMEM; |
| 318 | } |
| 319 | |
| 320 | static void __nvm_remove_target(struct nvm_target *t) |
| 321 | { |
| 322 | struct nvm_tgt_type *tt = t->type; |
| 323 | struct gendisk *tdisk = t->disk; |
| 324 | struct request_queue *q = tdisk->queue; |
| 325 | |
| 326 | del_gendisk(tdisk); |
| 327 | blk_cleanup_queue(q); |
| 328 | |
Javier González | 9a69b0e | 2017-01-31 13:17:20 +0100 | [diff] [blame] | 329 | if (tt->sysfs_exit) |
| 330 | tt->sysfs_exit(tdisk); |
| 331 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 332 | if (tt->exit) |
| 333 | tt->exit(tdisk->private_data); |
| 334 | |
| 335 | nvm_remove_tgt_dev(t->dev); |
| 336 | put_disk(tdisk); |
| 337 | |
| 338 | list_del(&t->list); |
| 339 | kfree(t); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * nvm_remove_tgt - Removes a target from the media manager |
| 344 | * @dev: device |
| 345 | * @remove: ioctl structure with target name to remove. |
| 346 | * |
| 347 | * Returns: |
| 348 | * 0: on success |
| 349 | * 1: on not found |
| 350 | * <0: on error |
| 351 | */ |
| 352 | static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove) |
| 353 | { |
| 354 | struct nvm_target *t; |
| 355 | |
| 356 | mutex_lock(&dev->mlock); |
| 357 | t = nvm_find_target(dev, remove->tgtname); |
| 358 | if (!t) { |
| 359 | mutex_unlock(&dev->mlock); |
| 360 | return 1; |
| 361 | } |
| 362 | __nvm_remove_target(t); |
| 363 | mutex_unlock(&dev->mlock); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static int nvm_register_map(struct nvm_dev *dev) |
| 369 | { |
| 370 | struct nvm_dev_map *rmap; |
| 371 | int i, j; |
| 372 | |
| 373 | rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL); |
| 374 | if (!rmap) |
| 375 | goto err_rmap; |
| 376 | |
| 377 | rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map), |
| 378 | GFP_KERNEL); |
| 379 | if (!rmap->chnls) |
| 380 | goto err_chnls; |
| 381 | |
| 382 | for (i = 0; i < dev->geo.nr_chnls; i++) { |
| 383 | struct nvm_ch_map *ch_rmap; |
| 384 | int *lun_roffs; |
| 385 | int luns_in_chnl = dev->geo.luns_per_chnl; |
| 386 | |
| 387 | ch_rmap = &rmap->chnls[i]; |
| 388 | |
| 389 | ch_rmap->ch_off = -1; |
| 390 | ch_rmap->nr_luns = luns_in_chnl; |
| 391 | |
| 392 | lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL); |
| 393 | if (!lun_roffs) |
| 394 | goto err_ch; |
| 395 | |
| 396 | for (j = 0; j < luns_in_chnl; j++) |
| 397 | lun_roffs[j] = -1; |
| 398 | |
| 399 | ch_rmap->lun_offs = lun_roffs; |
| 400 | } |
| 401 | |
| 402 | dev->rmap = rmap; |
| 403 | |
| 404 | return 0; |
| 405 | err_ch: |
| 406 | while (--i >= 0) |
| 407 | kfree(rmap->chnls[i].lun_offs); |
| 408 | err_chnls: |
| 409 | kfree(rmap); |
| 410 | err_rmap: |
| 411 | return -ENOMEM; |
| 412 | } |
| 413 | |
Matias Bjørling | 61a561d | 2017-01-31 13:17:13 +0100 | [diff] [blame] | 414 | static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 415 | { |
| 416 | struct nvm_dev_map *dev_map = tgt_dev->map; |
| 417 | struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch]; |
| 418 | int lun_off = ch_map->lun_offs[p->g.lun]; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 419 | |
| 420 | p->g.ch += ch_map->ch_off; |
| 421 | p->g.lun += lun_off; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Matias Bjørling | 61a561d | 2017-01-31 13:17:13 +0100 | [diff] [blame] | 424 | static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 425 | { |
| 426 | struct nvm_dev *dev = tgt_dev->parent; |
| 427 | struct nvm_dev_map *dev_rmap = dev->rmap; |
| 428 | struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch]; |
| 429 | int lun_roff = ch_rmap->lun_offs[p->g.lun]; |
| 430 | |
| 431 | p->g.ch -= ch_rmap->ch_off; |
| 432 | p->g.lun -= lun_roff; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 435 | static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, |
| 436 | struct ppa_addr *ppa_list, int nr_ppas) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 437 | { |
| 438 | int i; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 439 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 440 | for (i = 0; i < nr_ppas; i++) { |
| 441 | nvm_map_to_dev(tgt_dev, &ppa_list[i]); |
| 442 | ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 443 | } |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 446 | static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, |
| 447 | struct ppa_addr *ppa_list, int nr_ppas) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 448 | { |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 449 | int i; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 450 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 451 | for (i = 0; i < nr_ppas; i++) { |
| 452 | ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]); |
| 453 | nvm_map_to_tgt(tgt_dev, &ppa_list[i]); |
| 454 | } |
| 455 | } |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 456 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 457 | static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd) |
| 458 | { |
| 459 | if (rqd->nr_ppas == 1) { |
| 460 | nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1); |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas); |
| 465 | } |
| 466 | |
| 467 | static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd) |
| 468 | { |
| 469 | if (rqd->nr_ppas == 1) { |
| 470 | nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1); |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries, |
| 478 | int len) |
| 479 | { |
| 480 | struct nvm_geo *geo = &dev->geo; |
| 481 | struct nvm_dev_map *dev_rmap = dev->rmap; |
| 482 | u64 i; |
| 483 | |
| 484 | for (i = 0; i < len; i++) { |
| 485 | struct nvm_ch_map *ch_rmap; |
| 486 | int *lun_roffs; |
| 487 | struct ppa_addr gaddr; |
| 488 | u64 pba = le64_to_cpu(entries[i]); |
| 489 | int off; |
| 490 | u64 diff; |
| 491 | |
| 492 | if (!pba) |
| 493 | continue; |
| 494 | |
| 495 | gaddr = linear_to_generic_addr(geo, pba); |
| 496 | ch_rmap = &dev_rmap->chnls[gaddr.g.ch]; |
| 497 | lun_roffs = ch_rmap->lun_offs; |
| 498 | |
| 499 | off = gaddr.g.ch * geo->luns_per_chnl + gaddr.g.lun; |
| 500 | |
| 501 | diff = ((ch_rmap->ch_off * geo->luns_per_chnl) + |
| 502 | (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun; |
| 503 | |
| 504 | entries[i] -= cpu_to_le64(diff); |
| 505 | } |
| 506 | } |
| 507 | EXPORT_SYMBOL(nvm_part_to_tgt); |
| 508 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 509 | struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock) |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 510 | { |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 511 | struct nvm_tgt_type *tmp, *tt = NULL; |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 512 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 513 | if (lock) |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 514 | down_write(&nvm_tgtt_lock); |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 515 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 516 | list_for_each_entry(tmp, &nvm_tgt_types, list) |
| 517 | if (!strcmp(name, tmp->name)) { |
| 518 | tt = tmp; |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | if (lock) |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 523 | up_write(&nvm_tgtt_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 524 | return tt; |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 525 | } |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 526 | EXPORT_SYMBOL(nvm_find_target_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 527 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 528 | int nvm_register_tgt_type(struct nvm_tgt_type *tt) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 529 | { |
| 530 | int ret = 0; |
| 531 | |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 532 | down_write(&nvm_tgtt_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 533 | if (nvm_find_target_type(tt->name, 0)) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 534 | ret = -EEXIST; |
| 535 | else |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 536 | list_add(&tt->list, &nvm_tgt_types); |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 537 | up_write(&nvm_tgtt_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 538 | |
| 539 | return ret; |
| 540 | } |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 541 | EXPORT_SYMBOL(nvm_register_tgt_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 542 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 543 | void nvm_unregister_tgt_type(struct nvm_tgt_type *tt) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 544 | { |
| 545 | if (!tt) |
| 546 | return; |
| 547 | |
| 548 | down_write(&nvm_lock); |
| 549 | list_del(&tt->list); |
| 550 | up_write(&nvm_lock); |
| 551 | } |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 552 | EXPORT_SYMBOL(nvm_unregister_tgt_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 553 | |
| 554 | void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags, |
| 555 | dma_addr_t *dma_handler) |
| 556 | { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 557 | return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags, |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 558 | dma_handler); |
| 559 | } |
| 560 | EXPORT_SYMBOL(nvm_dev_dma_alloc); |
| 561 | |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 562 | void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 563 | { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 564 | dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 565 | } |
| 566 | EXPORT_SYMBOL(nvm_dev_dma_free); |
| 567 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 568 | static struct nvm_dev *nvm_find_nvm_dev(const char *name) |
| 569 | { |
| 570 | struct nvm_dev *dev; |
| 571 | |
| 572 | list_for_each_entry(dev, &nvm_devices, devices) |
| 573 | if (!strcmp(name, dev->name)) |
| 574 | return dev; |
| 575 | |
| 576 | return NULL; |
| 577 | } |
| 578 | |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 579 | int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas, |
| 580 | int nr_ppas, int type) |
| 581 | { |
| 582 | struct nvm_dev *dev = tgt_dev->parent; |
| 583 | struct nvm_rq rqd; |
| 584 | int ret; |
| 585 | |
| 586 | if (nr_ppas > dev->ops->max_phys_sect) { |
| 587 | pr_err("nvm: unable to update all blocks atomically\n"); |
| 588 | return -EINVAL; |
| 589 | } |
| 590 | |
| 591 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 592 | |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 593 | nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1); |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 594 | nvm_rq_tgt_to_dev(tgt_dev, &rqd); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 595 | |
| 596 | ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type); |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 597 | nvm_free_rqd_ppalist(tgt_dev, &rqd); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 598 | if (ret) { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 599 | pr_err("nvm: failed bb mark\n"); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 600 | return -EINVAL; |
| 601 | } |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | EXPORT_SYMBOL(nvm_set_tgt_bb_tbl); |
| 606 | |
Javier González | a279006 | 2016-11-28 22:39:12 +0100 | [diff] [blame] | 607 | int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev) |
| 608 | { |
| 609 | struct nvm_dev *dev = tgt_dev->parent; |
| 610 | |
| 611 | return dev->ops->max_phys_sect; |
| 612 | } |
| 613 | EXPORT_SYMBOL(nvm_max_phys_sects); |
| 614 | |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 615 | int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 616 | { |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 617 | struct nvm_dev *dev = tgt_dev->parent; |
| 618 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 619 | if (!dev->ops->submit_io) |
| 620 | return -ENODEV; |
| 621 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 622 | nvm_rq_tgt_to_dev(tgt_dev, rqd); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 623 | |
| 624 | rqd->dev = tgt_dev; |
| 625 | return dev->ops->submit_io(dev, rqd); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 626 | } |
| 627 | EXPORT_SYMBOL(nvm_submit_io); |
| 628 | |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 629 | static void nvm_end_io_sync(struct nvm_rq *rqd) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 630 | { |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 631 | struct completion *waiting = rqd->private; |
| 632 | |
| 633 | complete(waiting); |
| 634 | } |
| 635 | |
| 636 | int nvm_erase_sync(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas, |
| 637 | int nr_ppas) |
| 638 | { |
| 639 | struct nvm_geo *geo = &tgt_dev->geo; |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 640 | struct nvm_rq rqd; |
| 641 | int ret; |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 642 | DECLARE_COMPLETION_ONSTACK(wait); |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 643 | |
| 644 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 645 | |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 646 | rqd.opcode = NVM_OP_ERASE; |
| 647 | rqd.end_io = nvm_end_io_sync; |
| 648 | rqd.private = &wait; |
| 649 | rqd.flags = geo->plane_mode >> 1; |
| 650 | |
| 651 | ret = nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1); |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 652 | if (ret) |
| 653 | return ret; |
| 654 | |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 655 | ret = nvm_submit_io(tgt_dev, &rqd); |
| 656 | if (ret) { |
| 657 | pr_err("rrpr: erase I/O submission failed: %d\n", ret); |
| 658 | goto free_ppa_list; |
| 659 | } |
| 660 | wait_for_completion_io(&wait); |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 661 | |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 662 | free_ppa_list: |
| 663 | nvm_free_rqd_ppalist(tgt_dev, &rqd); |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 664 | |
| 665 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 666 | } |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 667 | EXPORT_SYMBOL(nvm_erase_sync); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 668 | |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 669 | int nvm_get_l2p_tbl(struct nvm_tgt_dev *tgt_dev, u64 slba, u32 nlb, |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 670 | nvm_l2p_update_fn *update_l2p, void *priv) |
| 671 | { |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 672 | struct nvm_dev *dev = tgt_dev->parent; |
| 673 | |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 674 | if (!dev->ops->get_l2p_tbl) |
| 675 | return 0; |
| 676 | |
| 677 | return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv); |
| 678 | } |
| 679 | EXPORT_SYMBOL(nvm_get_l2p_tbl); |
| 680 | |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 681 | int nvm_get_area(struct nvm_tgt_dev *tgt_dev, sector_t *lba, sector_t len) |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 682 | { |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 683 | struct nvm_dev *dev = tgt_dev->parent; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 684 | struct nvm_geo *geo = &dev->geo; |
| 685 | struct nvm_area *area, *prev, *next; |
| 686 | sector_t begin = 0; |
| 687 | sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9; |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 688 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 689 | if (len > max_sectors) |
| 690 | return -EINVAL; |
| 691 | |
| 692 | area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL); |
| 693 | if (!area) |
| 694 | return -ENOMEM; |
| 695 | |
| 696 | prev = NULL; |
| 697 | |
| 698 | spin_lock(&dev->lock); |
| 699 | list_for_each_entry(next, &dev->area_list, list) { |
| 700 | if (begin + len > next->begin) { |
| 701 | begin = next->end; |
| 702 | prev = next; |
| 703 | continue; |
| 704 | } |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | if ((begin + len) > max_sectors) { |
| 709 | spin_unlock(&dev->lock); |
| 710 | kfree(area); |
| 711 | return -EINVAL; |
| 712 | } |
| 713 | |
| 714 | area->begin = *lba = begin; |
| 715 | area->end = begin + len; |
| 716 | |
| 717 | if (prev) /* insert into sorted order */ |
| 718 | list_add(&area->list, &prev->list); |
| 719 | else |
| 720 | list_add(&area->list, &dev->area_list); |
| 721 | spin_unlock(&dev->lock); |
| 722 | |
| 723 | return 0; |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 724 | } |
| 725 | EXPORT_SYMBOL(nvm_get_area); |
| 726 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 727 | void nvm_put_area(struct nvm_tgt_dev *tgt_dev, sector_t begin) |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 728 | { |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 729 | struct nvm_dev *dev = tgt_dev->parent; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 730 | struct nvm_area *area; |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 731 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 732 | spin_lock(&dev->lock); |
| 733 | list_for_each_entry(area, &dev->area_list, list) { |
| 734 | if (area->begin != begin) |
| 735 | continue; |
| 736 | |
| 737 | list_del(&area->list); |
| 738 | spin_unlock(&dev->lock); |
| 739 | kfree(area); |
| 740 | return; |
| 741 | } |
| 742 | spin_unlock(&dev->lock); |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 743 | } |
| 744 | EXPORT_SYMBOL(nvm_put_area); |
| 745 | |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 746 | int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd, |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 747 | const struct ppa_addr *ppas, int nr_ppas, int vblk) |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 748 | { |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 749 | struct nvm_dev *dev = tgt_dev->parent; |
| 750 | struct nvm_geo *geo = &tgt_dev->geo; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 751 | int i, plane_cnt, pl_idx; |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 752 | struct ppa_addr ppa; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 753 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 754 | if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) { |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 755 | rqd->nr_ppas = nr_ppas; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 756 | rqd->ppa_addr = ppas[0]; |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 761 | rqd->nr_ppas = nr_ppas; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 762 | rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list); |
| 763 | if (!rqd->ppa_list) { |
| 764 | pr_err("nvm: failed to allocate dma memory\n"); |
| 765 | return -ENOMEM; |
| 766 | } |
| 767 | |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 768 | if (!vblk) { |
| 769 | for (i = 0; i < nr_ppas; i++) |
| 770 | rqd->ppa_list[i] = ppas[i]; |
| 771 | } else { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 772 | plane_cnt = geo->plane_mode; |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 773 | rqd->nr_ppas *= plane_cnt; |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 774 | |
Matias Bjørling | 556755e | 2016-01-12 07:49:26 +0100 | [diff] [blame] | 775 | for (i = 0; i < nr_ppas; i++) { |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 776 | for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) { |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 777 | ppa = ppas[i]; |
| 778 | ppa.g.pl = pl_idx; |
| 779 | rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa; |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 780 | } |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | EXPORT_SYMBOL(nvm_set_rqd_ppalist); |
| 787 | |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 788 | void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd) |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 789 | { |
| 790 | if (!rqd->ppa_list) |
| 791 | return; |
| 792 | |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame^] | 793 | nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list); |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 794 | } |
| 795 | EXPORT_SYMBOL(nvm_free_rqd_ppalist); |
| 796 | |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 797 | void nvm_end_io(struct nvm_rq *rqd) |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 798 | { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 799 | struct nvm_tgt_dev *tgt_dev = rqd->dev; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 800 | |
| 801 | /* Convert address space */ |
| 802 | if (tgt_dev) |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 803 | nvm_rq_dev_to_tgt(tgt_dev, rqd); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 804 | |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 805 | if (rqd->end_io) |
| 806 | rqd->end_io(rqd); |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 807 | } |
| 808 | EXPORT_SYMBOL(nvm_end_io); |
| 809 | |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 810 | /* |
| 811 | * folds a bad block list from its plane representation to its virtual |
| 812 | * block representation. The fold is done in place and reduced size is |
| 813 | * returned. |
| 814 | * |
| 815 | * If any of the planes status are bad or grown bad block, the virtual block |
| 816 | * is marked bad. If not bad, the first plane state acts as the block state. |
| 817 | */ |
| 818 | int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks) |
| 819 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 820 | struct nvm_geo *geo = &dev->geo; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 821 | int blk, offset, pl, blktype; |
| 822 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 823 | if (nr_blks != geo->blks_per_lun * geo->plane_mode) |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 824 | return -EINVAL; |
| 825 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 826 | for (blk = 0; blk < geo->blks_per_lun; blk++) { |
| 827 | offset = blk * geo->plane_mode; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 828 | blktype = blks[offset]; |
| 829 | |
| 830 | /* Bad blocks on any planes take precedence over other types */ |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 831 | for (pl = 0; pl < geo->plane_mode; pl++) { |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 832 | if (blks[offset + pl] & |
| 833 | (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) { |
| 834 | blktype = blks[offset + pl]; |
| 835 | break; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | blks[blk] = blktype; |
| 840 | } |
| 841 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 842 | return geo->blks_per_lun; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 843 | } |
| 844 | EXPORT_SYMBOL(nvm_bb_tbl_fold); |
| 845 | |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 846 | int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa, |
| 847 | u8 *blks) |
| 848 | { |
Matias Bjørling | 8f4fe00 | 2017-01-31 13:17:12 +0100 | [diff] [blame] | 849 | struct nvm_dev *dev = tgt_dev->parent; |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 850 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 851 | nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1); |
Matias Bjørling | 8f4fe00 | 2017-01-31 13:17:12 +0100 | [diff] [blame] | 852 | |
| 853 | return dev->ops->get_bb_tbl(dev, ppa, blks); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 854 | } |
| 855 | EXPORT_SYMBOL(nvm_get_tgt_bb_tbl); |
| 856 | |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 857 | static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp) |
| 858 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 859 | struct nvm_geo *geo = &dev->geo; |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 860 | int i; |
| 861 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 862 | dev->lps_per_blk = geo->pgs_per_blk; |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 863 | dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL); |
| 864 | if (!dev->lptbl) |
| 865 | return -ENOMEM; |
| 866 | |
| 867 | /* Just a linear array */ |
| 868 | for (i = 0; i < dev->lps_per_blk; i++) |
| 869 | dev->lptbl[i] = i; |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp) |
| 875 | { |
| 876 | int i, p; |
| 877 | struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc; |
| 878 | |
| 879 | if (!mlc->num_pairs) |
| 880 | return 0; |
| 881 | |
| 882 | dev->lps_per_blk = mlc->num_pairs; |
| 883 | dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL); |
| 884 | if (!dev->lptbl) |
| 885 | return -ENOMEM; |
| 886 | |
| 887 | /* The lower page table encoding consists of a list of bytes, where each |
| 888 | * has a lower and an upper half. The first half byte maintains the |
| 889 | * increment value and every value after is an offset added to the |
Matias Bjørling | 12624af | 2016-07-07 09:54:13 +0200 | [diff] [blame] | 890 | * previous incrementation value |
| 891 | */ |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 892 | dev->lptbl[0] = mlc->pairs[0] & 0xF; |
| 893 | for (i = 1; i < dev->lps_per_blk; i++) { |
| 894 | p = mlc->pairs[i >> 1]; |
| 895 | if (i & 0x1) /* upper */ |
| 896 | dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4); |
| 897 | else /* lower */ |
| 898 | dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF); |
| 899 | } |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 904 | static int nvm_core_init(struct nvm_dev *dev) |
| 905 | { |
| 906 | struct nvm_id *id = &dev->identity; |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 907 | struct nvm_id_group *grp = &id->grp; |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 908 | struct nvm_geo *geo = &dev->geo; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 909 | int ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 910 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 911 | /* Whole device values */ |
| 912 | geo->nr_chnls = grp->num_ch; |
| 913 | geo->luns_per_chnl = grp->num_lun; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 914 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 915 | /* Generic device values */ |
| 916 | geo->pgs_per_blk = grp->num_pg; |
| 917 | geo->blks_per_lun = grp->num_blk; |
| 918 | geo->nr_planes = grp->num_pln; |
| 919 | geo->fpg_size = grp->fpg_sz; |
| 920 | geo->pfpg_size = grp->fpg_sz * grp->num_pln; |
| 921 | geo->sec_size = grp->csecs; |
| 922 | geo->oob_size = grp->sos; |
| 923 | geo->sec_per_pg = grp->fpg_sz / grp->csecs; |
| 924 | geo->mccap = grp->mccap; |
| 925 | memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format)); |
| 926 | |
| 927 | geo->plane_mode = NVM_PLANE_SINGLE; |
| 928 | geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 929 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 930 | if (grp->mpos & 0x020202) |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 931 | geo->plane_mode = NVM_PLANE_DOUBLE; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 932 | if (grp->mpos & 0x040404) |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 933 | geo->plane_mode = NVM_PLANE_QUAD; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 934 | |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 935 | if (grp->mtype != 0) { |
| 936 | pr_err("nvm: memory type not supported\n"); |
| 937 | return -EINVAL; |
| 938 | } |
| 939 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 940 | /* calculated values */ |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 941 | geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes; |
| 942 | geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk; |
| 943 | geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun; |
| 944 | geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 945 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 946 | dev->total_secs = geo->nr_luns * geo->sec_per_lun; |
| 947 | dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns), |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 948 | sizeof(unsigned long), GFP_KERNEL); |
| 949 | if (!dev->lun_map) |
| 950 | return -ENOMEM; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 951 | |
| 952 | switch (grp->fmtype) { |
| 953 | case NVM_ID_FMTYPE_SLC: |
| 954 | if (nvm_init_slc_tbl(dev, grp)) { |
| 955 | ret = -ENOMEM; |
| 956 | goto err_fmtype; |
| 957 | } |
| 958 | break; |
| 959 | case NVM_ID_FMTYPE_MLC: |
| 960 | if (nvm_init_mlc_tbl(dev, grp)) { |
| 961 | ret = -ENOMEM; |
| 962 | goto err_fmtype; |
| 963 | } |
| 964 | break; |
| 965 | default: |
| 966 | pr_err("nvm: flash type not supported\n"); |
| 967 | ret = -EINVAL; |
| 968 | goto err_fmtype; |
| 969 | } |
| 970 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 971 | INIT_LIST_HEAD(&dev->area_list); |
| 972 | INIT_LIST_HEAD(&dev->targets); |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 973 | mutex_init(&dev->mlock); |
Wenwei Tao | 4c9dacb | 2016-03-03 15:06:37 +0100 | [diff] [blame] | 974 | spin_lock_init(&dev->lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 975 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 976 | ret = nvm_register_map(dev); |
| 977 | if (ret) |
| 978 | goto err_fmtype; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 979 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 980 | blk_queue_logical_block_size(dev->q, geo->sec_size); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 981 | return 0; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 982 | err_fmtype: |
| 983 | kfree(dev->lun_map); |
| 984 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 985 | } |
| 986 | |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 987 | void nvm_free(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 988 | { |
| 989 | if (!dev) |
| 990 | return; |
| 991 | |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 992 | if (dev->dma_pool) |
| 993 | dev->ops->destroy_dma_pool(dev->dma_pool); |
| 994 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 995 | kfree(dev->rmap); |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 996 | kfree(dev->lptbl); |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 997 | kfree(dev->lun_map); |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 998 | kfree(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | static int nvm_init(struct nvm_dev *dev) |
| 1002 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 1003 | struct nvm_geo *geo = &dev->geo; |
Wenwei Tao | 480fc0d | 2015-11-20 13:47:53 +0100 | [diff] [blame] | 1004 | int ret = -EINVAL; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1005 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1006 | if (dev->ops->identity(dev, &dev->identity)) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1007 | pr_err("nvm: device could not be identified\n"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1008 | goto err; |
| 1009 | } |
| 1010 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 1011 | pr_debug("nvm: ver:%x nvm_vendor:%x\n", |
| 1012 | dev->identity.ver_id, dev->identity.vmnt); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1013 | |
| 1014 | if (dev->identity.ver_id != 1) { |
| 1015 | pr_err("nvm: device not supported by kernel."); |
| 1016 | goto err; |
| 1017 | } |
| 1018 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1019 | ret = nvm_core_init(dev); |
| 1020 | if (ret) { |
| 1021 | pr_err("nvm: could not initialize core structures.\n"); |
| 1022 | goto err; |
| 1023 | } |
| 1024 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1025 | pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n", |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 1026 | dev->name, geo->sec_per_pg, geo->nr_planes, |
| 1027 | geo->pgs_per_blk, geo->blks_per_lun, |
| 1028 | geo->nr_luns, geo->nr_chnls); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1029 | return 0; |
| 1030 | err: |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1031 | pr_err("nvm: failed to initialize nvm\n"); |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1035 | struct nvm_dev *nvm_alloc_dev(int node) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1036 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1037 | return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node); |
| 1038 | } |
| 1039 | EXPORT_SYMBOL(nvm_alloc_dev); |
| 1040 | |
| 1041 | int nvm_register(struct nvm_dev *dev) |
| 1042 | { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1043 | int ret; |
| 1044 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1045 | if (!dev->q || !dev->ops) |
| 1046 | return -EINVAL; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1047 | |
Wenwei Tao | d160147 | 2015-11-28 16:49:25 +0100 | [diff] [blame] | 1048 | if (dev->ops->max_phys_sect > 256) { |
| 1049 | pr_info("nvm: max sectors supported is 256.\n"); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1050 | return -EINVAL; |
Wenwei Tao | d160147 | 2015-11-28 16:49:25 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1053 | if (dev->ops->max_phys_sect > 1) { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 1054 | dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist"); |
| 1055 | if (!dev->dma_pool) { |
| 1056 | pr_err("nvm: could not create dma pool\n"); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1057 | return -ENOMEM; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1058 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1061 | ret = nvm_init(dev); |
| 1062 | if (ret) |
| 1063 | goto err_init; |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 1064 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 1065 | /* register device with a supported media manager */ |
Matias Bjørling | edad2e6 | 2015-11-16 15:34:42 +0100 | [diff] [blame] | 1066 | down_write(&nvm_lock); |
| 1067 | list_add(&dev->devices, &nvm_devices); |
| 1068 | up_write(&nvm_lock); |
| 1069 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1070 | return 0; |
| 1071 | err_init: |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1072 | dev->ops->destroy_dma_pool(dev->dma_pool); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1073 | return ret; |
| 1074 | } |
| 1075 | EXPORT_SYMBOL(nvm_register); |
| 1076 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1077 | void nvm_unregister(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1078 | { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1079 | struct nvm_target *t, *tmp; |
| 1080 | |
| 1081 | mutex_lock(&dev->mlock); |
| 1082 | list_for_each_entry_safe(t, tmp, &dev->targets, list) { |
| 1083 | if (t->dev->parent != dev) |
| 1084 | continue; |
| 1085 | __nvm_remove_target(t); |
| 1086 | } |
| 1087 | mutex_unlock(&dev->mlock); |
| 1088 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 1089 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1090 | list_del(&dev->devices); |
| 1091 | up_write(&nvm_lock); |
Matias Bjørling | c1480ad | 2015-11-16 15:34:43 +0100 | [diff] [blame] | 1092 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 1093 | nvm_free(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1094 | } |
| 1095 | EXPORT_SYMBOL(nvm_unregister); |
| 1096 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1097 | static int __nvm_configure_create(struct nvm_ioctl_create *create) |
| 1098 | { |
| 1099 | struct nvm_dev *dev; |
| 1100 | struct nvm_ioctl_create_simple *s; |
| 1101 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 1102 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1103 | dev = nvm_find_nvm_dev(create->dev); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 1104 | up_write(&nvm_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1105 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1106 | if (!dev) { |
| 1107 | pr_err("nvm: device not found\n"); |
| 1108 | return -EINVAL; |
| 1109 | } |
| 1110 | |
| 1111 | if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) { |
| 1112 | pr_err("nvm: config type not valid\n"); |
| 1113 | return -EINVAL; |
| 1114 | } |
| 1115 | s = &create->conf.s; |
| 1116 | |
Matias Bjørling | 6732c74 | 2017-02-15 16:25:33 +0100 | [diff] [blame] | 1117 | if (s->lun_begin == -1 && s->lun_end == -1) { |
| 1118 | s->lun_begin = 0; |
| 1119 | s->lun_end = dev->geo.nr_luns - 1; |
| 1120 | } |
| 1121 | |
Matias Bjørling | 0e5ffd1 | 2017-02-15 16:25:32 +0100 | [diff] [blame] | 1122 | if (s->lun_begin > s->lun_end || s->lun_end >= dev->geo.nr_luns) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1123 | pr_err("nvm: lun out of bound (%u:%u > %u)\n", |
Matias Bjørling | 0e5ffd1 | 2017-02-15 16:25:32 +0100 | [diff] [blame] | 1124 | s->lun_begin, s->lun_end, dev->geo.nr_luns - 1); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1125 | return -EINVAL; |
| 1126 | } |
| 1127 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1128 | return nvm_create_tgt(dev, create); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1131 | static long nvm_ioctl_info(struct file *file, void __user *arg) |
| 1132 | { |
| 1133 | struct nvm_ioctl_info *info; |
| 1134 | struct nvm_tgt_type *tt; |
| 1135 | int tgt_iter = 0; |
| 1136 | |
| 1137 | if (!capable(CAP_SYS_ADMIN)) |
| 1138 | return -EPERM; |
| 1139 | |
| 1140 | info = memdup_user(arg, sizeof(struct nvm_ioctl_info)); |
| 1141 | if (IS_ERR(info)) |
| 1142 | return -EFAULT; |
| 1143 | |
| 1144 | info->version[0] = NVM_VERSION_MAJOR; |
| 1145 | info->version[1] = NVM_VERSION_MINOR; |
| 1146 | info->version[2] = NVM_VERSION_PATCH; |
| 1147 | |
| 1148 | down_write(&nvm_lock); |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 1149 | list_for_each_entry(tt, &nvm_tgt_types, list) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1150 | struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter]; |
| 1151 | |
| 1152 | tgt->version[0] = tt->version[0]; |
| 1153 | tgt->version[1] = tt->version[1]; |
| 1154 | tgt->version[2] = tt->version[2]; |
| 1155 | strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX); |
| 1156 | |
| 1157 | tgt_iter++; |
| 1158 | } |
| 1159 | |
| 1160 | info->tgtsize = tgt_iter; |
| 1161 | up_write(&nvm_lock); |
| 1162 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1163 | if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) { |
| 1164 | kfree(info); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1165 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1166 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1167 | |
| 1168 | kfree(info); |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | static long nvm_ioctl_get_devices(struct file *file, void __user *arg) |
| 1173 | { |
| 1174 | struct nvm_ioctl_get_devices *devices; |
| 1175 | struct nvm_dev *dev; |
| 1176 | int i = 0; |
| 1177 | |
| 1178 | if (!capable(CAP_SYS_ADMIN)) |
| 1179 | return -EPERM; |
| 1180 | |
| 1181 | devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL); |
| 1182 | if (!devices) |
| 1183 | return -ENOMEM; |
| 1184 | |
| 1185 | down_write(&nvm_lock); |
| 1186 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 1187 | struct nvm_ioctl_device_info *info = &devices->info[i]; |
| 1188 | |
| 1189 | sprintf(info->devname, "%s", dev->name); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1190 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1191 | /* kept for compatibility */ |
| 1192 | info->bmversion[0] = 1; |
| 1193 | info->bmversion[1] = 0; |
| 1194 | info->bmversion[2] = 0; |
| 1195 | sprintf(info->bmname, "%s", "gennvm"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1196 | i++; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1197 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1198 | if (i > 31) { |
| 1199 | pr_err("nvm: max 31 devices can be reported.\n"); |
| 1200 | break; |
| 1201 | } |
| 1202 | } |
| 1203 | up_write(&nvm_lock); |
| 1204 | |
| 1205 | devices->nr_devices = i; |
| 1206 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1207 | if (copy_to_user(arg, devices, |
| 1208 | sizeof(struct nvm_ioctl_get_devices))) { |
| 1209 | kfree(devices); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1210 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1211 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1212 | |
| 1213 | kfree(devices); |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | static long nvm_ioctl_dev_create(struct file *file, void __user *arg) |
| 1218 | { |
| 1219 | struct nvm_ioctl_create create; |
| 1220 | |
| 1221 | if (!capable(CAP_SYS_ADMIN)) |
| 1222 | return -EPERM; |
| 1223 | |
| 1224 | if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create))) |
| 1225 | return -EFAULT; |
| 1226 | |
| 1227 | create.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1228 | create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0'; |
| 1229 | create.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1230 | |
| 1231 | if (create.flags != 0) { |
| 1232 | pr_err("nvm: no flags supported\n"); |
| 1233 | return -EINVAL; |
| 1234 | } |
| 1235 | |
| 1236 | return __nvm_configure_create(&create); |
| 1237 | } |
| 1238 | |
| 1239 | static long nvm_ioctl_dev_remove(struct file *file, void __user *arg) |
| 1240 | { |
| 1241 | struct nvm_ioctl_remove remove; |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1242 | struct nvm_dev *dev; |
| 1243 | int ret = 0; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1244 | |
| 1245 | if (!capable(CAP_SYS_ADMIN)) |
| 1246 | return -EPERM; |
| 1247 | |
| 1248 | if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove))) |
| 1249 | return -EFAULT; |
| 1250 | |
| 1251 | remove.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1252 | |
| 1253 | if (remove.flags != 0) { |
| 1254 | pr_err("nvm: no flags supported\n"); |
| 1255 | return -EINVAL; |
| 1256 | } |
| 1257 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1258 | list_for_each_entry(dev, &nvm_devices, devices) { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1259 | ret = nvm_remove_tgt(dev, &remove); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1260 | if (!ret) |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1267 | /* kept for compatibility reasons */ |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1268 | static long nvm_ioctl_dev_init(struct file *file, void __user *arg) |
| 1269 | { |
| 1270 | struct nvm_ioctl_dev_init init; |
| 1271 | |
| 1272 | if (!capable(CAP_SYS_ADMIN)) |
| 1273 | return -EPERM; |
| 1274 | |
| 1275 | if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init))) |
| 1276 | return -EFAULT; |
| 1277 | |
| 1278 | if (init.flags != 0) { |
| 1279 | pr_err("nvm: no flags supported\n"); |
| 1280 | return -EINVAL; |
| 1281 | } |
| 1282 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1283 | return 0; |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1286 | /* Kept for compatibility reasons */ |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1287 | static long nvm_ioctl_dev_factory(struct file *file, void __user *arg) |
| 1288 | { |
| 1289 | struct nvm_ioctl_dev_factory fact; |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1290 | |
| 1291 | if (!capable(CAP_SYS_ADMIN)) |
| 1292 | return -EPERM; |
| 1293 | |
| 1294 | if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory))) |
| 1295 | return -EFAULT; |
| 1296 | |
| 1297 | fact.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1298 | |
| 1299 | if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1)) |
| 1300 | return -EINVAL; |
| 1301 | |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1302 | return 0; |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1303 | } |
| 1304 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1305 | static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg) |
| 1306 | { |
| 1307 | void __user *argp = (void __user *)arg; |
| 1308 | |
| 1309 | switch (cmd) { |
| 1310 | case NVM_INFO: |
| 1311 | return nvm_ioctl_info(file, argp); |
| 1312 | case NVM_GET_DEVICES: |
| 1313 | return nvm_ioctl_get_devices(file, argp); |
| 1314 | case NVM_DEV_CREATE: |
| 1315 | return nvm_ioctl_dev_create(file, argp); |
| 1316 | case NVM_DEV_REMOVE: |
| 1317 | return nvm_ioctl_dev_remove(file, argp); |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1318 | case NVM_DEV_INIT: |
| 1319 | return nvm_ioctl_dev_init(file, argp); |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1320 | case NVM_DEV_FACTORY: |
| 1321 | return nvm_ioctl_dev_factory(file, argp); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1322 | } |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | static const struct file_operations _ctl_fops = { |
| 1327 | .open = nonseekable_open, |
| 1328 | .unlocked_ioctl = nvm_ctl_ioctl, |
| 1329 | .owner = THIS_MODULE, |
| 1330 | .llseek = noop_llseek, |
| 1331 | }; |
| 1332 | |
| 1333 | static struct miscdevice _nvm_misc = { |
| 1334 | .minor = MISC_DYNAMIC_MINOR, |
| 1335 | .name = "lightnvm", |
| 1336 | .nodename = "lightnvm/control", |
| 1337 | .fops = &_ctl_fops, |
| 1338 | }; |
Paul Gortmaker | 389b2a1 | 2016-10-29 16:38:41 -0400 | [diff] [blame] | 1339 | builtin_misc_device(_nvm_misc); |