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