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 | |
| 292 | t->type = tt; |
| 293 | t->disk = tdisk; |
| 294 | t->dev = tgt_dev; |
| 295 | |
| 296 | mutex_lock(&dev->mlock); |
| 297 | list_add_tail(&t->list, &dev->targets); |
| 298 | mutex_unlock(&dev->mlock); |
| 299 | |
| 300 | return 0; |
| 301 | err_init: |
| 302 | put_disk(tdisk); |
| 303 | err_queue: |
| 304 | blk_cleanup_queue(tqueue); |
| 305 | err_dev: |
| 306 | kfree(tgt_dev); |
| 307 | err_t: |
| 308 | kfree(t); |
| 309 | err_reserve: |
| 310 | nvm_release_luns_err(dev, s->lun_begin, s->lun_end); |
| 311 | return -ENOMEM; |
| 312 | } |
| 313 | |
| 314 | static void __nvm_remove_target(struct nvm_target *t) |
| 315 | { |
| 316 | struct nvm_tgt_type *tt = t->type; |
| 317 | struct gendisk *tdisk = t->disk; |
| 318 | struct request_queue *q = tdisk->queue; |
| 319 | |
| 320 | del_gendisk(tdisk); |
| 321 | blk_cleanup_queue(q); |
| 322 | |
| 323 | if (tt->exit) |
| 324 | tt->exit(tdisk->private_data); |
| 325 | |
| 326 | nvm_remove_tgt_dev(t->dev); |
| 327 | put_disk(tdisk); |
| 328 | |
| 329 | list_del(&t->list); |
| 330 | kfree(t); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * nvm_remove_tgt - Removes a target from the media manager |
| 335 | * @dev: device |
| 336 | * @remove: ioctl structure with target name to remove. |
| 337 | * |
| 338 | * Returns: |
| 339 | * 0: on success |
| 340 | * 1: on not found |
| 341 | * <0: on error |
| 342 | */ |
| 343 | static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove) |
| 344 | { |
| 345 | struct nvm_target *t; |
| 346 | |
| 347 | mutex_lock(&dev->mlock); |
| 348 | t = nvm_find_target(dev, remove->tgtname); |
| 349 | if (!t) { |
| 350 | mutex_unlock(&dev->mlock); |
| 351 | return 1; |
| 352 | } |
| 353 | __nvm_remove_target(t); |
| 354 | mutex_unlock(&dev->mlock); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static int nvm_register_map(struct nvm_dev *dev) |
| 360 | { |
| 361 | struct nvm_dev_map *rmap; |
| 362 | int i, j; |
| 363 | |
| 364 | rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL); |
| 365 | if (!rmap) |
| 366 | goto err_rmap; |
| 367 | |
| 368 | rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map), |
| 369 | GFP_KERNEL); |
| 370 | if (!rmap->chnls) |
| 371 | goto err_chnls; |
| 372 | |
| 373 | for (i = 0; i < dev->geo.nr_chnls; i++) { |
| 374 | struct nvm_ch_map *ch_rmap; |
| 375 | int *lun_roffs; |
| 376 | int luns_in_chnl = dev->geo.luns_per_chnl; |
| 377 | |
| 378 | ch_rmap = &rmap->chnls[i]; |
| 379 | |
| 380 | ch_rmap->ch_off = -1; |
| 381 | ch_rmap->nr_luns = luns_in_chnl; |
| 382 | |
| 383 | lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL); |
| 384 | if (!lun_roffs) |
| 385 | goto err_ch; |
| 386 | |
| 387 | for (j = 0; j < luns_in_chnl; j++) |
| 388 | lun_roffs[j] = -1; |
| 389 | |
| 390 | ch_rmap->lun_offs = lun_roffs; |
| 391 | } |
| 392 | |
| 393 | dev->rmap = rmap; |
| 394 | |
| 395 | return 0; |
| 396 | err_ch: |
| 397 | while (--i >= 0) |
| 398 | kfree(rmap->chnls[i].lun_offs); |
| 399 | err_chnls: |
| 400 | kfree(rmap); |
| 401 | err_rmap: |
| 402 | return -ENOMEM; |
| 403 | } |
| 404 | |
Matias Bjørling | 61a561d | 2017-01-31 13:17:13 +0100 | [diff] [blame] | 405 | 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] | 406 | { |
| 407 | struct nvm_dev_map *dev_map = tgt_dev->map; |
| 408 | struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch]; |
| 409 | int lun_off = ch_map->lun_offs[p->g.lun]; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 410 | |
| 411 | p->g.ch += ch_map->ch_off; |
| 412 | p->g.lun += lun_off; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Matias Bjørling | 61a561d | 2017-01-31 13:17:13 +0100 | [diff] [blame] | 415 | 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] | 416 | { |
| 417 | struct nvm_dev *dev = tgt_dev->parent; |
| 418 | struct nvm_dev_map *dev_rmap = dev->rmap; |
| 419 | struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch]; |
| 420 | int lun_roff = ch_rmap->lun_offs[p->g.lun]; |
| 421 | |
| 422 | p->g.ch -= ch_rmap->ch_off; |
| 423 | p->g.lun -= lun_roff; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 426 | static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, |
| 427 | struct ppa_addr *ppa_list, int nr_ppas) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 428 | { |
| 429 | int i; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 430 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 431 | for (i = 0; i < nr_ppas; i++) { |
| 432 | nvm_map_to_dev(tgt_dev, &ppa_list[i]); |
| 433 | 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] | 434 | } |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 437 | static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, |
| 438 | struct ppa_addr *ppa_list, int nr_ppas) |
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 | int i; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 441 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 442 | for (i = 0; i < nr_ppas; i++) { |
| 443 | ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]); |
| 444 | nvm_map_to_tgt(tgt_dev, &ppa_list[i]); |
| 445 | } |
| 446 | } |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 447 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 448 | static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd) |
| 449 | { |
| 450 | if (rqd->nr_ppas == 1) { |
| 451 | nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas); |
| 456 | } |
| 457 | |
| 458 | static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd) |
| 459 | { |
| 460 | if (rqd->nr_ppas == 1) { |
| 461 | nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | 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] | 466 | } |
| 467 | |
| 468 | void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries, |
| 469 | int len) |
| 470 | { |
| 471 | struct nvm_geo *geo = &dev->geo; |
| 472 | struct nvm_dev_map *dev_rmap = dev->rmap; |
| 473 | u64 i; |
| 474 | |
| 475 | for (i = 0; i < len; i++) { |
| 476 | struct nvm_ch_map *ch_rmap; |
| 477 | int *lun_roffs; |
| 478 | struct ppa_addr gaddr; |
| 479 | u64 pba = le64_to_cpu(entries[i]); |
| 480 | int off; |
| 481 | u64 diff; |
| 482 | |
| 483 | if (!pba) |
| 484 | continue; |
| 485 | |
| 486 | gaddr = linear_to_generic_addr(geo, pba); |
| 487 | ch_rmap = &dev_rmap->chnls[gaddr.g.ch]; |
| 488 | lun_roffs = ch_rmap->lun_offs; |
| 489 | |
| 490 | off = gaddr.g.ch * geo->luns_per_chnl + gaddr.g.lun; |
| 491 | |
| 492 | diff = ((ch_rmap->ch_off * geo->luns_per_chnl) + |
| 493 | (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun; |
| 494 | |
| 495 | entries[i] -= cpu_to_le64(diff); |
| 496 | } |
| 497 | } |
| 498 | EXPORT_SYMBOL(nvm_part_to_tgt); |
| 499 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 500 | 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] | 501 | { |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 502 | struct nvm_tgt_type *tmp, *tt = NULL; |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 503 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 504 | if (lock) |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 505 | down_write(&nvm_tgtt_lock); |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 506 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 507 | list_for_each_entry(tmp, &nvm_tgt_types, list) |
| 508 | if (!strcmp(name, tmp->name)) { |
| 509 | tt = tmp; |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | if (lock) |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 514 | up_write(&nvm_tgtt_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 515 | return tt; |
Simon A. F. Lund | 6f8645c | 2016-05-06 20:03:03 +0200 | [diff] [blame] | 516 | } |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 517 | EXPORT_SYMBOL(nvm_find_target_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 518 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 519 | int nvm_register_tgt_type(struct nvm_tgt_type *tt) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 520 | { |
| 521 | int ret = 0; |
| 522 | |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 523 | down_write(&nvm_tgtt_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 524 | if (nvm_find_target_type(tt->name, 0)) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 525 | ret = -EEXIST; |
| 526 | else |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 527 | list_add(&tt->list, &nvm_tgt_types); |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 528 | up_write(&nvm_tgtt_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 529 | |
| 530 | return ret; |
| 531 | } |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 532 | EXPORT_SYMBOL(nvm_register_tgt_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 533 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 534 | void nvm_unregister_tgt_type(struct nvm_tgt_type *tt) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 535 | { |
| 536 | if (!tt) |
| 537 | return; |
| 538 | |
| 539 | down_write(&nvm_lock); |
| 540 | list_del(&tt->list); |
| 541 | up_write(&nvm_lock); |
| 542 | } |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 543 | EXPORT_SYMBOL(nvm_unregister_tgt_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 544 | |
| 545 | void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags, |
| 546 | dma_addr_t *dma_handler) |
| 547 | { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 548 | 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] | 549 | dma_handler); |
| 550 | } |
| 551 | EXPORT_SYMBOL(nvm_dev_dma_alloc); |
| 552 | |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 553 | 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] | 554 | { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 555 | dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 556 | } |
| 557 | EXPORT_SYMBOL(nvm_dev_dma_free); |
| 558 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 559 | static struct nvm_dev *nvm_find_nvm_dev(const char *name) |
| 560 | { |
| 561 | struct nvm_dev *dev; |
| 562 | |
| 563 | list_for_each_entry(dev, &nvm_devices, devices) |
| 564 | if (!strcmp(name, dev->name)) |
| 565 | return dev; |
| 566 | |
| 567 | return NULL; |
| 568 | } |
| 569 | |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 570 | int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas, |
| 571 | int nr_ppas, int type) |
| 572 | { |
| 573 | struct nvm_dev *dev = tgt_dev->parent; |
| 574 | struct nvm_rq rqd; |
| 575 | int ret; |
| 576 | |
| 577 | if (nr_ppas > dev->ops->max_phys_sect) { |
| 578 | pr_err("nvm: unable to update all blocks atomically\n"); |
| 579 | return -EINVAL; |
| 580 | } |
| 581 | |
| 582 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 583 | |
| 584 | nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1); |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 585 | nvm_rq_tgt_to_dev(tgt_dev, &rqd); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 586 | |
| 587 | ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type); |
| 588 | nvm_free_rqd_ppalist(dev, &rqd); |
| 589 | if (ret) { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 590 | pr_err("nvm: failed bb mark\n"); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 591 | return -EINVAL; |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | EXPORT_SYMBOL(nvm_set_tgt_bb_tbl); |
| 597 | |
Javier González | a279006 | 2016-11-28 22:39:12 +0100 | [diff] [blame] | 598 | int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev) |
| 599 | { |
| 600 | struct nvm_dev *dev = tgt_dev->parent; |
| 601 | |
| 602 | return dev->ops->max_phys_sect; |
| 603 | } |
| 604 | EXPORT_SYMBOL(nvm_max_phys_sects); |
| 605 | |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 606 | 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] | 607 | { |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 608 | struct nvm_dev *dev = tgt_dev->parent; |
| 609 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 610 | if (!dev->ops->submit_io) |
| 611 | return -ENODEV; |
| 612 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 613 | nvm_rq_tgt_to_dev(tgt_dev, rqd); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 614 | |
| 615 | rqd->dev = tgt_dev; |
| 616 | return dev->ops->submit_io(dev, rqd); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 617 | } |
| 618 | EXPORT_SYMBOL(nvm_submit_io); |
| 619 | |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 620 | int nvm_erase_blk(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas, int flags) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 621 | { |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 622 | struct nvm_dev *dev = tgt_dev->parent; |
| 623 | struct nvm_rq rqd; |
| 624 | int ret; |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 625 | |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 626 | if (!dev->ops->erase_block) |
| 627 | return 0; |
| 628 | |
Matias Bjørling | 61a561d | 2017-01-31 13:17:13 +0100 | [diff] [blame] | 629 | nvm_map_to_dev(tgt_dev, ppas); |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 630 | |
| 631 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 632 | |
| 633 | ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, 1, 1); |
| 634 | if (ret) |
| 635 | return ret; |
| 636 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 637 | nvm_rq_tgt_to_dev(tgt_dev, &rqd); |
Matias Bjørling | 10995c3 | 2017-01-31 13:17:10 +0100 | [diff] [blame] | 638 | |
| 639 | rqd.flags = flags; |
| 640 | |
| 641 | ret = dev->ops->erase_block(dev, &rqd); |
| 642 | |
| 643 | nvm_free_rqd_ppalist(dev, &rqd); |
| 644 | |
| 645 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 646 | } |
| 647 | EXPORT_SYMBOL(nvm_erase_blk); |
| 648 | |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 649 | 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] | 650 | nvm_l2p_update_fn *update_l2p, void *priv) |
| 651 | { |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 652 | struct nvm_dev *dev = tgt_dev->parent; |
| 653 | |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 654 | if (!dev->ops->get_l2p_tbl) |
| 655 | return 0; |
| 656 | |
| 657 | return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv); |
| 658 | } |
| 659 | EXPORT_SYMBOL(nvm_get_l2p_tbl); |
| 660 | |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 661 | 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] | 662 | { |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 663 | struct nvm_dev *dev = tgt_dev->parent; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 664 | struct nvm_geo *geo = &dev->geo; |
| 665 | struct nvm_area *area, *prev, *next; |
| 666 | sector_t begin = 0; |
| 667 | sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9; |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 668 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 669 | if (len > max_sectors) |
| 670 | return -EINVAL; |
| 671 | |
| 672 | area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL); |
| 673 | if (!area) |
| 674 | return -ENOMEM; |
| 675 | |
| 676 | prev = NULL; |
| 677 | |
| 678 | spin_lock(&dev->lock); |
| 679 | list_for_each_entry(next, &dev->area_list, list) { |
| 680 | if (begin + len > next->begin) { |
| 681 | begin = next->end; |
| 682 | prev = next; |
| 683 | continue; |
| 684 | } |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | if ((begin + len) > max_sectors) { |
| 689 | spin_unlock(&dev->lock); |
| 690 | kfree(area); |
| 691 | return -EINVAL; |
| 692 | } |
| 693 | |
| 694 | area->begin = *lba = begin; |
| 695 | area->end = begin + len; |
| 696 | |
| 697 | if (prev) /* insert into sorted order */ |
| 698 | list_add(&area->list, &prev->list); |
| 699 | else |
| 700 | list_add(&area->list, &dev->area_list); |
| 701 | spin_unlock(&dev->lock); |
| 702 | |
| 703 | return 0; |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 704 | } |
| 705 | EXPORT_SYMBOL(nvm_get_area); |
| 706 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 707 | 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] | 708 | { |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 709 | struct nvm_dev *dev = tgt_dev->parent; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 710 | struct nvm_area *area; |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 711 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 712 | spin_lock(&dev->lock); |
| 713 | list_for_each_entry(area, &dev->area_list, list) { |
| 714 | if (area->begin != begin) |
| 715 | continue; |
| 716 | |
| 717 | list_del(&area->list); |
| 718 | spin_unlock(&dev->lock); |
| 719 | kfree(area); |
| 720 | return; |
| 721 | } |
| 722 | spin_unlock(&dev->lock); |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 723 | } |
| 724 | EXPORT_SYMBOL(nvm_put_area); |
| 725 | |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 726 | int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd, |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 727 | const struct ppa_addr *ppas, int nr_ppas, int vblk) |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 728 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 729 | struct nvm_geo *geo = &dev->geo; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 730 | int i, plane_cnt, pl_idx; |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 731 | struct ppa_addr ppa; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 732 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 733 | if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) { |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 734 | rqd->nr_ppas = nr_ppas; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 735 | rqd->ppa_addr = ppas[0]; |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 740 | rqd->nr_ppas = nr_ppas; |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 741 | rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list); |
| 742 | if (!rqd->ppa_list) { |
| 743 | pr_err("nvm: failed to allocate dma memory\n"); |
| 744 | return -ENOMEM; |
| 745 | } |
| 746 | |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 747 | if (!vblk) { |
| 748 | for (i = 0; i < nr_ppas; i++) |
| 749 | rqd->ppa_list[i] = ppas[i]; |
| 750 | } else { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 751 | plane_cnt = geo->plane_mode; |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 752 | rqd->nr_ppas *= plane_cnt; |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 753 | |
Matias Bjørling | 556755e | 2016-01-12 07:49:26 +0100 | [diff] [blame] | 754 | for (i = 0; i < nr_ppas; i++) { |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 755 | for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) { |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 756 | ppa = ppas[i]; |
| 757 | ppa.g.pl = pl_idx; |
| 758 | rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa; |
Matias Bjørling | 5ebc7d9 | 2016-05-06 20:03:07 +0200 | [diff] [blame] | 759 | } |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | EXPORT_SYMBOL(nvm_set_rqd_ppalist); |
| 766 | |
| 767 | void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd) |
| 768 | { |
| 769 | if (!rqd->ppa_list) |
| 770 | return; |
| 771 | |
| 772 | nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list); |
| 773 | } |
| 774 | EXPORT_SYMBOL(nvm_free_rqd_ppalist); |
| 775 | |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 776 | void nvm_end_io(struct nvm_rq *rqd, int error) |
| 777 | { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 778 | struct nvm_tgt_dev *tgt_dev = rqd->dev; |
| 779 | struct nvm_tgt_instance *ins = rqd->ins; |
| 780 | |
| 781 | /* Convert address space */ |
| 782 | if (tgt_dev) |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 783 | nvm_rq_dev_to_tgt(tgt_dev, rqd); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 784 | |
Matias Bjørling | 72d256e | 2016-01-12 07:49:29 +0100 | [diff] [blame] | 785 | rqd->error = error; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 786 | ins->tt->end_io(rqd); |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 787 | } |
| 788 | EXPORT_SYMBOL(nvm_end_io); |
| 789 | |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 790 | /* |
| 791 | * folds a bad block list from its plane representation to its virtual |
| 792 | * block representation. The fold is done in place and reduced size is |
| 793 | * returned. |
| 794 | * |
| 795 | * If any of the planes status are bad or grown bad block, the virtual block |
| 796 | * is marked bad. If not bad, the first plane state acts as the block state. |
| 797 | */ |
| 798 | int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks) |
| 799 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 800 | struct nvm_geo *geo = &dev->geo; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 801 | int blk, offset, pl, blktype; |
| 802 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 803 | if (nr_blks != geo->blks_per_lun * geo->plane_mode) |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 804 | return -EINVAL; |
| 805 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 806 | for (blk = 0; blk < geo->blks_per_lun; blk++) { |
| 807 | offset = blk * geo->plane_mode; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 808 | blktype = blks[offset]; |
| 809 | |
| 810 | /* Bad blocks on any planes take precedence over other types */ |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 811 | for (pl = 0; pl < geo->plane_mode; pl++) { |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 812 | if (blks[offset + pl] & |
| 813 | (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) { |
| 814 | blktype = blks[offset + pl]; |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | blks[blk] = blktype; |
| 820 | } |
| 821 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 822 | return geo->blks_per_lun; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 823 | } |
| 824 | EXPORT_SYMBOL(nvm_bb_tbl_fold); |
| 825 | |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 826 | int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa, |
| 827 | u8 *blks) |
| 828 | { |
Matias Bjørling | 8f4fe00 | 2017-01-31 13:17:12 +0100 | [diff] [blame] | 829 | struct nvm_dev *dev = tgt_dev->parent; |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 830 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 831 | nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1); |
Matias Bjørling | 8f4fe00 | 2017-01-31 13:17:12 +0100 | [diff] [blame] | 832 | |
| 833 | return dev->ops->get_bb_tbl(dev, ppa, blks); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 834 | } |
| 835 | EXPORT_SYMBOL(nvm_get_tgt_bb_tbl); |
| 836 | |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 837 | static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp) |
| 838 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 839 | struct nvm_geo *geo = &dev->geo; |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 840 | int i; |
| 841 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 842 | dev->lps_per_blk = geo->pgs_per_blk; |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 843 | dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL); |
| 844 | if (!dev->lptbl) |
| 845 | return -ENOMEM; |
| 846 | |
| 847 | /* Just a linear array */ |
| 848 | for (i = 0; i < dev->lps_per_blk; i++) |
| 849 | dev->lptbl[i] = i; |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp) |
| 855 | { |
| 856 | int i, p; |
| 857 | struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc; |
| 858 | |
| 859 | if (!mlc->num_pairs) |
| 860 | return 0; |
| 861 | |
| 862 | dev->lps_per_blk = mlc->num_pairs; |
| 863 | dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL); |
| 864 | if (!dev->lptbl) |
| 865 | return -ENOMEM; |
| 866 | |
| 867 | /* The lower page table encoding consists of a list of bytes, where each |
| 868 | * has a lower and an upper half. The first half byte maintains the |
| 869 | * 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] | 870 | * previous incrementation value |
| 871 | */ |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 872 | dev->lptbl[0] = mlc->pairs[0] & 0xF; |
| 873 | for (i = 1; i < dev->lps_per_blk; i++) { |
| 874 | p = mlc->pairs[i >> 1]; |
| 875 | if (i & 0x1) /* upper */ |
| 876 | dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4); |
| 877 | else /* lower */ |
| 878 | dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF); |
| 879 | } |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 884 | static int nvm_core_init(struct nvm_dev *dev) |
| 885 | { |
| 886 | struct nvm_id *id = &dev->identity; |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame^] | 887 | struct nvm_id_group *grp = &id->grp; |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 888 | struct nvm_geo *geo = &dev->geo; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 889 | int ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 890 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 891 | /* Whole device values */ |
| 892 | geo->nr_chnls = grp->num_ch; |
| 893 | geo->luns_per_chnl = grp->num_lun; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 894 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 895 | /* Generic device values */ |
| 896 | geo->pgs_per_blk = grp->num_pg; |
| 897 | geo->blks_per_lun = grp->num_blk; |
| 898 | geo->nr_planes = grp->num_pln; |
| 899 | geo->fpg_size = grp->fpg_sz; |
| 900 | geo->pfpg_size = grp->fpg_sz * grp->num_pln; |
| 901 | geo->sec_size = grp->csecs; |
| 902 | geo->oob_size = grp->sos; |
| 903 | geo->sec_per_pg = grp->fpg_sz / grp->csecs; |
| 904 | geo->mccap = grp->mccap; |
| 905 | memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format)); |
| 906 | |
| 907 | geo->plane_mode = NVM_PLANE_SINGLE; |
| 908 | 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] | 909 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 910 | if (grp->mpos & 0x020202) |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 911 | geo->plane_mode = NVM_PLANE_DOUBLE; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 912 | if (grp->mpos & 0x040404) |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 913 | geo->plane_mode = NVM_PLANE_QUAD; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 914 | |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 915 | if (grp->mtype != 0) { |
| 916 | pr_err("nvm: memory type not supported\n"); |
| 917 | return -EINVAL; |
| 918 | } |
| 919 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 920 | /* calculated values */ |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 921 | geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes; |
| 922 | geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk; |
| 923 | geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun; |
| 924 | geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 925 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 926 | dev->total_secs = geo->nr_luns * geo->sec_per_lun; |
| 927 | dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns), |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 928 | sizeof(unsigned long), GFP_KERNEL); |
| 929 | if (!dev->lun_map) |
| 930 | return -ENOMEM; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 931 | |
| 932 | switch (grp->fmtype) { |
| 933 | case NVM_ID_FMTYPE_SLC: |
| 934 | if (nvm_init_slc_tbl(dev, grp)) { |
| 935 | ret = -ENOMEM; |
| 936 | goto err_fmtype; |
| 937 | } |
| 938 | break; |
| 939 | case NVM_ID_FMTYPE_MLC: |
| 940 | if (nvm_init_mlc_tbl(dev, grp)) { |
| 941 | ret = -ENOMEM; |
| 942 | goto err_fmtype; |
| 943 | } |
| 944 | break; |
| 945 | default: |
| 946 | pr_err("nvm: flash type not supported\n"); |
| 947 | ret = -EINVAL; |
| 948 | goto err_fmtype; |
| 949 | } |
| 950 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 951 | INIT_LIST_HEAD(&dev->area_list); |
| 952 | INIT_LIST_HEAD(&dev->targets); |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 953 | mutex_init(&dev->mlock); |
Wenwei Tao | 4c9dacb | 2016-03-03 15:06:37 +0100 | [diff] [blame] | 954 | spin_lock_init(&dev->lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 955 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 956 | ret = nvm_register_map(dev); |
| 957 | if (ret) |
| 958 | goto err_fmtype; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 959 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 960 | blk_queue_logical_block_size(dev->q, geo->sec_size); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 961 | return 0; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 962 | err_fmtype: |
| 963 | kfree(dev->lun_map); |
| 964 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 965 | } |
| 966 | |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 967 | void nvm_free(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 968 | { |
| 969 | if (!dev) |
| 970 | return; |
| 971 | |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 972 | if (dev->dma_pool) |
| 973 | dev->ops->destroy_dma_pool(dev->dma_pool); |
| 974 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 975 | kfree(dev->rmap); |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 976 | kfree(dev->lptbl); |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 977 | kfree(dev->lun_map); |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 978 | kfree(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static int nvm_init(struct nvm_dev *dev) |
| 982 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 983 | struct nvm_geo *geo = &dev->geo; |
Wenwei Tao | 480fc0d | 2015-11-20 13:47:53 +0100 | [diff] [blame] | 984 | int ret = -EINVAL; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 985 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 986 | if (dev->ops->identity(dev, &dev->identity)) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 987 | pr_err("nvm: device could not be identified\n"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 988 | goto err; |
| 989 | } |
| 990 | |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame^] | 991 | pr_debug("nvm: ver:%x nvm_vendor:%x\n", |
| 992 | dev->identity.ver_id, dev->identity.vmnt); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 993 | |
| 994 | if (dev->identity.ver_id != 1) { |
| 995 | pr_err("nvm: device not supported by kernel."); |
| 996 | goto err; |
| 997 | } |
| 998 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 999 | ret = nvm_core_init(dev); |
| 1000 | if (ret) { |
| 1001 | pr_err("nvm: could not initialize core structures.\n"); |
| 1002 | goto err; |
| 1003 | } |
| 1004 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1005 | 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] | 1006 | dev->name, geo->sec_per_pg, geo->nr_planes, |
| 1007 | geo->pgs_per_blk, geo->blks_per_lun, |
| 1008 | geo->nr_luns, geo->nr_chnls); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1009 | return 0; |
| 1010 | err: |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1011 | pr_err("nvm: failed to initialize nvm\n"); |
| 1012 | return ret; |
| 1013 | } |
| 1014 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1015 | struct nvm_dev *nvm_alloc_dev(int node) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1016 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1017 | return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node); |
| 1018 | } |
| 1019 | EXPORT_SYMBOL(nvm_alloc_dev); |
| 1020 | |
| 1021 | int nvm_register(struct nvm_dev *dev) |
| 1022 | { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1023 | int ret; |
| 1024 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1025 | if (!dev->q || !dev->ops) |
| 1026 | return -EINVAL; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1027 | |
Wenwei Tao | d160147 | 2015-11-28 16:49:25 +0100 | [diff] [blame] | 1028 | if (dev->ops->max_phys_sect > 256) { |
| 1029 | pr_info("nvm: max sectors supported is 256.\n"); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1030 | return -EINVAL; |
Wenwei Tao | d160147 | 2015-11-28 16:49:25 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1033 | if (dev->ops->max_phys_sect > 1) { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 1034 | dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist"); |
| 1035 | if (!dev->dma_pool) { |
| 1036 | pr_err("nvm: could not create dma pool\n"); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1037 | return -ENOMEM; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1038 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1039 | } |
| 1040 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1041 | ret = nvm_init(dev); |
| 1042 | if (ret) |
| 1043 | goto err_init; |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 1044 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 1045 | /* register device with a supported media manager */ |
Matias Bjørling | edad2e6 | 2015-11-16 15:34:42 +0100 | [diff] [blame] | 1046 | down_write(&nvm_lock); |
| 1047 | list_add(&dev->devices, &nvm_devices); |
| 1048 | up_write(&nvm_lock); |
| 1049 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1050 | return 0; |
| 1051 | err_init: |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1052 | dev->ops->destroy_dma_pool(dev->dma_pool); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1053 | return ret; |
| 1054 | } |
| 1055 | EXPORT_SYMBOL(nvm_register); |
| 1056 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1057 | void nvm_unregister(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1058 | { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1059 | struct nvm_target *t, *tmp; |
| 1060 | |
| 1061 | mutex_lock(&dev->mlock); |
| 1062 | list_for_each_entry_safe(t, tmp, &dev->targets, list) { |
| 1063 | if (t->dev->parent != dev) |
| 1064 | continue; |
| 1065 | __nvm_remove_target(t); |
| 1066 | } |
| 1067 | mutex_unlock(&dev->mlock); |
| 1068 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 1069 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1070 | list_del(&dev->devices); |
| 1071 | up_write(&nvm_lock); |
Matias Bjørling | c1480ad | 2015-11-16 15:34:43 +0100 | [diff] [blame] | 1072 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 1073 | nvm_free(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1074 | } |
| 1075 | EXPORT_SYMBOL(nvm_unregister); |
| 1076 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1077 | static int __nvm_configure_create(struct nvm_ioctl_create *create) |
| 1078 | { |
| 1079 | struct nvm_dev *dev; |
| 1080 | struct nvm_ioctl_create_simple *s; |
| 1081 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 1082 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1083 | dev = nvm_find_nvm_dev(create->dev); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 1084 | up_write(&nvm_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1085 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1086 | if (!dev) { |
| 1087 | pr_err("nvm: device not found\n"); |
| 1088 | return -EINVAL; |
| 1089 | } |
| 1090 | |
| 1091 | if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) { |
| 1092 | pr_err("nvm: config type not valid\n"); |
| 1093 | return -EINVAL; |
| 1094 | } |
| 1095 | s = &create->conf.s; |
| 1096 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 1097 | 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] | 1098 | pr_err("nvm: lun out of bound (%u:%u > %u)\n", |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 1099 | s->lun_begin, s->lun_end, dev->geo.nr_luns); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1100 | return -EINVAL; |
| 1101 | } |
| 1102 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1103 | return nvm_create_tgt(dev, create); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1106 | static long nvm_ioctl_info(struct file *file, void __user *arg) |
| 1107 | { |
| 1108 | struct nvm_ioctl_info *info; |
| 1109 | struct nvm_tgt_type *tt; |
| 1110 | int tgt_iter = 0; |
| 1111 | |
| 1112 | if (!capable(CAP_SYS_ADMIN)) |
| 1113 | return -EPERM; |
| 1114 | |
| 1115 | info = memdup_user(arg, sizeof(struct nvm_ioctl_info)); |
| 1116 | if (IS_ERR(info)) |
| 1117 | return -EFAULT; |
| 1118 | |
| 1119 | info->version[0] = NVM_VERSION_MAJOR; |
| 1120 | info->version[1] = NVM_VERSION_MINOR; |
| 1121 | info->version[2] = NVM_VERSION_PATCH; |
| 1122 | |
| 1123 | down_write(&nvm_lock); |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 1124 | list_for_each_entry(tt, &nvm_tgt_types, list) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1125 | struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter]; |
| 1126 | |
| 1127 | tgt->version[0] = tt->version[0]; |
| 1128 | tgt->version[1] = tt->version[1]; |
| 1129 | tgt->version[2] = tt->version[2]; |
| 1130 | strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX); |
| 1131 | |
| 1132 | tgt_iter++; |
| 1133 | } |
| 1134 | |
| 1135 | info->tgtsize = tgt_iter; |
| 1136 | up_write(&nvm_lock); |
| 1137 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1138 | if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) { |
| 1139 | kfree(info); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1140 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1141 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1142 | |
| 1143 | kfree(info); |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | static long nvm_ioctl_get_devices(struct file *file, void __user *arg) |
| 1148 | { |
| 1149 | struct nvm_ioctl_get_devices *devices; |
| 1150 | struct nvm_dev *dev; |
| 1151 | int i = 0; |
| 1152 | |
| 1153 | if (!capable(CAP_SYS_ADMIN)) |
| 1154 | return -EPERM; |
| 1155 | |
| 1156 | devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL); |
| 1157 | if (!devices) |
| 1158 | return -ENOMEM; |
| 1159 | |
| 1160 | down_write(&nvm_lock); |
| 1161 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 1162 | struct nvm_ioctl_device_info *info = &devices->info[i]; |
| 1163 | |
| 1164 | sprintf(info->devname, "%s", dev->name); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1165 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1166 | /* kept for compatibility */ |
| 1167 | info->bmversion[0] = 1; |
| 1168 | info->bmversion[1] = 0; |
| 1169 | info->bmversion[2] = 0; |
| 1170 | sprintf(info->bmname, "%s", "gennvm"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1171 | i++; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1172 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1173 | if (i > 31) { |
| 1174 | pr_err("nvm: max 31 devices can be reported.\n"); |
| 1175 | break; |
| 1176 | } |
| 1177 | } |
| 1178 | up_write(&nvm_lock); |
| 1179 | |
| 1180 | devices->nr_devices = i; |
| 1181 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1182 | if (copy_to_user(arg, devices, |
| 1183 | sizeof(struct nvm_ioctl_get_devices))) { |
| 1184 | kfree(devices); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1185 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1186 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1187 | |
| 1188 | kfree(devices); |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | static long nvm_ioctl_dev_create(struct file *file, void __user *arg) |
| 1193 | { |
| 1194 | struct nvm_ioctl_create create; |
| 1195 | |
| 1196 | if (!capable(CAP_SYS_ADMIN)) |
| 1197 | return -EPERM; |
| 1198 | |
| 1199 | if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create))) |
| 1200 | return -EFAULT; |
| 1201 | |
| 1202 | create.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1203 | create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0'; |
| 1204 | create.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1205 | |
| 1206 | if (create.flags != 0) { |
| 1207 | pr_err("nvm: no flags supported\n"); |
| 1208 | return -EINVAL; |
| 1209 | } |
| 1210 | |
| 1211 | return __nvm_configure_create(&create); |
| 1212 | } |
| 1213 | |
| 1214 | static long nvm_ioctl_dev_remove(struct file *file, void __user *arg) |
| 1215 | { |
| 1216 | struct nvm_ioctl_remove remove; |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1217 | struct nvm_dev *dev; |
| 1218 | int ret = 0; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1219 | |
| 1220 | if (!capable(CAP_SYS_ADMIN)) |
| 1221 | return -EPERM; |
| 1222 | |
| 1223 | if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove))) |
| 1224 | return -EFAULT; |
| 1225 | |
| 1226 | remove.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1227 | |
| 1228 | if (remove.flags != 0) { |
| 1229 | pr_err("nvm: no flags supported\n"); |
| 1230 | return -EINVAL; |
| 1231 | } |
| 1232 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1233 | list_for_each_entry(dev, &nvm_devices, devices) { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1234 | ret = nvm_remove_tgt(dev, &remove); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1235 | if (!ret) |
| 1236 | break; |
| 1237 | } |
| 1238 | |
| 1239 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1242 | /* kept for compatibility reasons */ |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1243 | static long nvm_ioctl_dev_init(struct file *file, void __user *arg) |
| 1244 | { |
| 1245 | struct nvm_ioctl_dev_init init; |
| 1246 | |
| 1247 | if (!capable(CAP_SYS_ADMIN)) |
| 1248 | return -EPERM; |
| 1249 | |
| 1250 | if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init))) |
| 1251 | return -EFAULT; |
| 1252 | |
| 1253 | if (init.flags != 0) { |
| 1254 | pr_err("nvm: no flags supported\n"); |
| 1255 | return -EINVAL; |
| 1256 | } |
| 1257 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1258 | return 0; |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1259 | } |
| 1260 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1261 | /* Kept for compatibility reasons */ |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1262 | static long nvm_ioctl_dev_factory(struct file *file, void __user *arg) |
| 1263 | { |
| 1264 | struct nvm_ioctl_dev_factory fact; |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1265 | |
| 1266 | if (!capable(CAP_SYS_ADMIN)) |
| 1267 | return -EPERM; |
| 1268 | |
| 1269 | if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory))) |
| 1270 | return -EFAULT; |
| 1271 | |
| 1272 | fact.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1273 | |
| 1274 | if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1)) |
| 1275 | return -EINVAL; |
| 1276 | |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1277 | return 0; |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1278 | } |
| 1279 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1280 | static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg) |
| 1281 | { |
| 1282 | void __user *argp = (void __user *)arg; |
| 1283 | |
| 1284 | switch (cmd) { |
| 1285 | case NVM_INFO: |
| 1286 | return nvm_ioctl_info(file, argp); |
| 1287 | case NVM_GET_DEVICES: |
| 1288 | return nvm_ioctl_get_devices(file, argp); |
| 1289 | case NVM_DEV_CREATE: |
| 1290 | return nvm_ioctl_dev_create(file, argp); |
| 1291 | case NVM_DEV_REMOVE: |
| 1292 | return nvm_ioctl_dev_remove(file, argp); |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1293 | case NVM_DEV_INIT: |
| 1294 | return nvm_ioctl_dev_init(file, argp); |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1295 | case NVM_DEV_FACTORY: |
| 1296 | return nvm_ioctl_dev_factory(file, argp); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1297 | } |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | static const struct file_operations _ctl_fops = { |
| 1302 | .open = nonseekable_open, |
| 1303 | .unlocked_ioctl = nvm_ctl_ioctl, |
| 1304 | .owner = THIS_MODULE, |
| 1305 | .llseek = noop_llseek, |
| 1306 | }; |
| 1307 | |
| 1308 | static struct miscdevice _nvm_misc = { |
| 1309 | .minor = MISC_DYNAMIC_MINOR, |
| 1310 | .name = "lightnvm", |
| 1311 | .nodename = "lightnvm/control", |
| 1312 | .fops = &_ctl_fops, |
| 1313 | }; |
Paul Gortmaker | 389b2a1 | 2016-10-29 16:38:41 -0400 | [diff] [blame] | 1314 | builtin_misc_device(_nvm_misc); |