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