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