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; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 39 | int num_lun; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 40 | int *lun_offs; |
| 41 | }; |
| 42 | |
| 43 | struct nvm_dev_map { |
| 44 | struct nvm_ch_map *chnls; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 45 | int num_ch; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 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 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 117 | for (i = 0; i < dev_map->num_ch; i++) { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 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) { |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 123 | for (j = 0; j < ch_map->num_lun; j++) { |
Javier González | edee1bd | 2017-04-15 20:55:42 +0200 | [diff] [blame] | 124 | int lun = j + lun_offs[j]; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 125 | int lunid = (ch * dev->geo.num_lun) + 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; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 150 | int num_lun = lun_end - lun_begin + 1; |
| 151 | int luns_left = num_lun; |
| 152 | int num_ch = num_lun / dev->geo.num_lun; |
| 153 | int num_ch_mod = num_lun % dev->geo.num_lun; |
| 154 | int bch = lun_begin / dev->geo.num_lun; |
| 155 | int blun = lun_begin % dev->geo.num_lun; |
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 | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 158 | int sec_per_lun, prev_num_lun; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 159 | int i, j; |
| 160 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 161 | num_ch = (num_ch_mod == 0) ? num_ch : num_ch + 1; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 162 | |
| 163 | dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL); |
| 164 | if (!dev_map) |
| 165 | goto err_dev; |
| 166 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 167 | dev_map->chnls = kcalloc(num_ch, sizeof(struct nvm_ch_map), GFP_KERNEL); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 168 | if (!dev_map->chnls) |
| 169 | goto err_chnls; |
| 170 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 171 | luns = kcalloc(num_lun, sizeof(struct ppa_addr), GFP_KERNEL); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 172 | if (!luns) |
| 173 | goto err_luns; |
| 174 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 175 | prev_num_lun = (luns_left > dev->geo.num_lun) ? |
| 176 | dev->geo.num_lun : luns_left; |
| 177 | for (i = 0; i < num_ch; i++) { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 178 | struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch]; |
| 179 | int *lun_roffs = ch_rmap->lun_offs; |
| 180 | struct nvm_ch_map *ch_map = &dev_map->chnls[i]; |
| 181 | int *lun_offs; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 182 | int luns_in_chnl = (luns_left > dev->geo.num_lun) ? |
| 183 | dev->geo.num_lun : luns_left; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 184 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 185 | if (lun_balanced && prev_num_lun != luns_in_chnl) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 186 | lun_balanced = 0; |
| 187 | |
| 188 | ch_map->ch_off = ch_rmap->ch_off = bch; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 189 | ch_map->num_lun = luns_in_chnl; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 190 | |
| 191 | lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL); |
| 192 | if (!lun_offs) |
| 193 | goto err_ch; |
| 194 | |
| 195 | for (j = 0; j < luns_in_chnl; j++) { |
| 196 | luns[lunid].ppa = 0; |
Javier González | 6947151 | 2018-03-30 00:05:15 +0200 | [diff] [blame] | 197 | luns[lunid].a.ch = i; |
| 198 | luns[lunid++].a.lun = j; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 199 | |
| 200 | lun_offs[j] = blun; |
| 201 | lun_roffs[j + blun] = blun; |
| 202 | } |
| 203 | |
| 204 | ch_map->lun_offs = lun_offs; |
| 205 | |
| 206 | /* when starting a new channel, lun offset is reset */ |
| 207 | blun = 0; |
| 208 | luns_left -= luns_in_chnl; |
| 209 | } |
| 210 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 211 | dev_map->num_ch = num_ch; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 212 | |
| 213 | tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL); |
| 214 | if (!tgt_dev) |
| 215 | goto err_ch; |
| 216 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 217 | /* Inherit device geometry from parent */ |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 218 | memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo)); |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 219 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 220 | /* Target device only owns a portion of the physical device */ |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 221 | tgt_dev->geo.num_ch = num_ch; |
| 222 | tgt_dev->geo.num_lun = (lun_balanced) ? prev_num_lun : -1; |
| 223 | tgt_dev->geo.all_luns = num_lun; |
| 224 | tgt_dev->geo.all_chunks = num_lun * dev->geo.num_chk; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 225 | |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 226 | tgt_dev->geo.op = op; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 227 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 228 | sec_per_lun = dev->geo.clba * dev->geo.num_chk; |
| 229 | tgt_dev->geo.total_secs = num_lun * sec_per_lun; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 230 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 231 | tgt_dev->q = dev->q; |
| 232 | tgt_dev->map = dev_map; |
| 233 | tgt_dev->luns = luns; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 234 | tgt_dev->parent = dev; |
| 235 | |
| 236 | return tgt_dev; |
| 237 | err_ch: |
Javier González | 507f7d6 | 2017-05-03 11:19:05 +0200 | [diff] [blame] | 238 | while (--i >= 0) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 239 | kfree(dev_map->chnls[i].lun_offs); |
| 240 | kfree(luns); |
| 241 | err_luns: |
| 242 | kfree(dev_map->chnls); |
| 243 | err_chnls: |
| 244 | kfree(dev_map); |
| 245 | err_dev: |
| 246 | return tgt_dev; |
| 247 | } |
| 248 | |
| 249 | static const struct block_device_operations nvm_fops = { |
| 250 | .owner = THIS_MODULE, |
| 251 | }; |
| 252 | |
Javier González | e29c80e | 2018-01-05 14:16:04 +0100 | [diff] [blame] | 253 | static struct nvm_tgt_type *__nvm_find_target_type(const char *name) |
Rakesh Pandit | eb6f168 | 2017-10-13 14:46:31 +0200 | [diff] [blame] | 254 | { |
Javier González | e29c80e | 2018-01-05 14:16:04 +0100 | [diff] [blame] | 255 | struct nvm_tgt_type *tt; |
Rakesh Pandit | eb6f168 | 2017-10-13 14:46:31 +0200 | [diff] [blame] | 256 | |
Javier González | e29c80e | 2018-01-05 14:16:04 +0100 | [diff] [blame] | 257 | list_for_each_entry(tt, &nvm_tgt_types, list) |
| 258 | if (!strcmp(name, tt->name)) |
| 259 | return tt; |
Rakesh Pandit | eb6f168 | 2017-10-13 14:46:31 +0200 | [diff] [blame] | 260 | |
Javier González | e29c80e | 2018-01-05 14:16:04 +0100 | [diff] [blame] | 261 | return NULL; |
| 262 | } |
Rakesh Pandit | eb6f168 | 2017-10-13 14:46:31 +0200 | [diff] [blame] | 263 | |
Javier González | e29c80e | 2018-01-05 14:16:04 +0100 | [diff] [blame] | 264 | static struct nvm_tgt_type *nvm_find_target_type(const char *name) |
| 265 | { |
| 266 | struct nvm_tgt_type *tt; |
| 267 | |
| 268 | down_write(&nvm_tgtt_lock); |
| 269 | tt = __nvm_find_target_type(name); |
| 270 | up_write(&nvm_tgtt_lock); |
| 271 | |
Rakesh Pandit | eb6f168 | 2017-10-13 14:46:31 +0200 | [diff] [blame] | 272 | return tt; |
| 273 | } |
| 274 | |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 275 | static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin, |
| 276 | int lun_end) |
| 277 | { |
| 278 | if (lun_begin > lun_end || lun_end >= geo->all_luns) { |
| 279 | pr_err("nvm: lun out of bound (%u:%u > %u)\n", |
| 280 | lun_begin, lun_end, geo->all_luns - 1); |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int __nvm_config_simple(struct nvm_dev *dev, |
| 288 | struct nvm_ioctl_create_simple *s) |
| 289 | { |
| 290 | struct nvm_geo *geo = &dev->geo; |
| 291 | |
| 292 | if (s->lun_begin == -1 && s->lun_end == -1) { |
| 293 | s->lun_begin = 0; |
| 294 | s->lun_end = geo->all_luns - 1; |
| 295 | } |
| 296 | |
| 297 | return nvm_config_check_luns(geo, s->lun_begin, s->lun_end); |
| 298 | } |
| 299 | |
| 300 | static int __nvm_config_extended(struct nvm_dev *dev, |
| 301 | struct nvm_ioctl_create_extended *e) |
| 302 | { |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 303 | if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) { |
| 304 | e->lun_begin = 0; |
| 305 | e->lun_end = dev->geo.all_luns - 1; |
| 306 | } |
| 307 | |
| 308 | /* op not set falls into target's default */ |
Heiner Litz | 9d7aa4a | 2018-03-30 00:05:08 +0200 | [diff] [blame] | 309 | if (e->op == 0xFFFF) { |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 310 | e->op = NVM_TARGET_DEFAULT_OP; |
Heiner Litz | 9d7aa4a | 2018-03-30 00:05:08 +0200 | [diff] [blame] | 311 | } 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] | 312 | pr_err("nvm: invalid over provisioning value\n"); |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 316 | 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] | 317 | } |
| 318 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 319 | static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create) |
| 320 | { |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 321 | struct nvm_ioctl_create_extended e; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 322 | struct request_queue *tqueue; |
| 323 | struct gendisk *tdisk; |
| 324 | struct nvm_tgt_type *tt; |
| 325 | struct nvm_target *t; |
| 326 | struct nvm_tgt_dev *tgt_dev; |
| 327 | void *targetdata; |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 328 | int ret; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 329 | |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 330 | switch (create->conf.type) { |
| 331 | case NVM_CONFIG_TYPE_SIMPLE: |
| 332 | ret = __nvm_config_simple(dev, &create->conf.s); |
| 333 | if (ret) |
| 334 | return ret; |
| 335 | |
| 336 | e.lun_begin = create->conf.s.lun_begin; |
| 337 | e.lun_end = create->conf.s.lun_end; |
| 338 | e.op = NVM_TARGET_DEFAULT_OP; |
| 339 | break; |
| 340 | case NVM_CONFIG_TYPE_EXTENDED: |
| 341 | ret = __nvm_config_extended(dev, &create->conf.e); |
| 342 | if (ret) |
| 343 | return ret; |
| 344 | |
| 345 | e = create->conf.e; |
| 346 | break; |
| 347 | default: |
| 348 | pr_err("nvm: config type not valid\n"); |
| 349 | return -EINVAL; |
| 350 | } |
| 351 | |
Javier González | e29c80e | 2018-01-05 14:16:04 +0100 | [diff] [blame] | 352 | tt = nvm_find_target_type(create->tgttype); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 353 | if (!tt) { |
| 354 | pr_err("nvm: target type %s not found\n", create->tgttype); |
| 355 | return -EINVAL; |
| 356 | } |
| 357 | |
Matias Bjørling | 656e33c | 2018-10-09 13:11:34 +0200 | [diff] [blame] | 358 | if ((tt->flags & NVM_TGT_F_HOST_L2P) != (dev->geo.dom & NVM_RSP_L2P)) { |
| 359 | pr_err("nvm: device is incompatible with target L2P type.\n"); |
| 360 | return -EINVAL; |
| 361 | } |
| 362 | |
Javier González | bd77b23 | 2018-01-05 14:16:05 +0100 | [diff] [blame] | 363 | if (nvm_target_exists(create->tgtname)) { |
| 364 | pr_err("nvm: target name already exists (%s)\n", |
| 365 | create->tgtname); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 366 | return -EINVAL; |
| 367 | } |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 368 | |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 369 | ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end); |
Rakesh Pandit | 12e9a6d6 | 2017-06-27 14:55:33 +0300 | [diff] [blame] | 370 | if (ret) |
| 371 | return ret; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 372 | |
| 373 | t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL); |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 374 | if (!t) { |
| 375 | ret = -ENOMEM; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 376 | goto err_reserve; |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 377 | } |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 378 | |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 379 | 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] | 380 | if (!tgt_dev) { |
| 381 | pr_err("nvm: could not create target device\n"); |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 382 | ret = -ENOMEM; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 383 | goto err_t; |
| 384 | } |
| 385 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 386 | tdisk = alloc_disk(0); |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 387 | if (!tdisk) { |
| 388 | ret = -ENOMEM; |
Javier González | 7d1ef2f | 2017-04-15 20:55:43 +0200 | [diff] [blame] | 389 | goto err_dev; |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 390 | } |
Javier González | 7d1ef2f | 2017-04-15 20:55:43 +0200 | [diff] [blame] | 391 | |
Christoph Hellwig | 6d46964 | 2018-11-14 17:02:18 +0100 | [diff] [blame] | 392 | tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node); |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 393 | if (!tqueue) { |
| 394 | ret = -ENOMEM; |
Javier González | 7d1ef2f | 2017-04-15 20:55:43 +0200 | [diff] [blame] | 395 | goto err_disk; |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 396 | } |
Javier González | 7d1ef2f | 2017-04-15 20:55:43 +0200 | [diff] [blame] | 397 | blk_queue_make_request(tqueue, tt->make_rq); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 398 | |
Javier González | 6eb0824 | 2017-04-15 20:55:49 +0200 | [diff] [blame] | 399 | strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name)); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 400 | tdisk->flags = GENHD_FL_EXT_DEVT; |
| 401 | tdisk->major = 0; |
| 402 | tdisk->first_minor = 0; |
| 403 | tdisk->fops = &nvm_fops; |
| 404 | tdisk->queue = tqueue; |
| 405 | |
Javier González | 4af3f75 | 2017-04-15 20:55:45 +0200 | [diff] [blame] | 406 | targetdata = tt->init(tgt_dev, tdisk, create->flags); |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 407 | if (IS_ERR(targetdata)) { |
| 408 | ret = PTR_ERR(targetdata); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 409 | goto err_init; |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 410 | } |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 411 | |
| 412 | tdisk->private_data = targetdata; |
| 413 | tqueue->queuedata = targetdata; |
| 414 | |
Matias Bjørling | 89a09c5 | 2018-03-30 00:05:04 +0200 | [diff] [blame] | 415 | blk_queue_max_hw_sectors(tqueue, |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 416 | (dev->geo.csecs >> 9) * NVM_MAX_VLBA); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 417 | |
| 418 | set_capacity(tdisk, tt->capacity(targetdata)); |
| 419 | add_disk(tdisk); |
| 420 | |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 421 | if (tt->sysfs_init && tt->sysfs_init(tdisk)) { |
| 422 | ret = -ENOMEM; |
Javier González | 9a69b0e | 2017-01-31 13:17:20 +0100 | [diff] [blame] | 423 | goto err_sysfs; |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 424 | } |
Javier González | 9a69b0e | 2017-01-31 13:17:20 +0100 | [diff] [blame] | 425 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 426 | t->type = tt; |
| 427 | t->disk = tdisk; |
| 428 | t->dev = tgt_dev; |
| 429 | |
| 430 | mutex_lock(&dev->mlock); |
| 431 | list_add_tail(&t->list, &dev->targets); |
| 432 | mutex_unlock(&dev->mlock); |
| 433 | |
Rakesh Pandit | 9001482 | 2017-10-13 14:45:50 +0200 | [diff] [blame] | 434 | __module_get(tt->owner); |
| 435 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 436 | return 0; |
Javier González | 9a69b0e | 2017-01-31 13:17:20 +0100 | [diff] [blame] | 437 | err_sysfs: |
| 438 | if (tt->exit) |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 439 | tt->exit(targetdata, true); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 440 | err_init: |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 441 | blk_cleanup_queue(tqueue); |
Rakesh Pandit | 75ba4ad | 2017-04-20 08:17:47 -0600 | [diff] [blame] | 442 | tdisk->queue = NULL; |
Javier González | 7d1ef2f | 2017-04-15 20:55:43 +0200 | [diff] [blame] | 443 | err_disk: |
| 444 | put_disk(tdisk); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 445 | err_dev: |
Javier González | edee1bd | 2017-04-15 20:55:42 +0200 | [diff] [blame] | 446 | nvm_remove_tgt_dev(tgt_dev, 0); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 447 | err_t: |
| 448 | kfree(t); |
| 449 | err_reserve: |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 450 | nvm_release_luns_err(dev, e.lun_begin, e.lun_end); |
Rakesh Pandit | 8d77bb8 | 2017-04-20 21:23:56 +0300 | [diff] [blame] | 451 | return ret; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 454 | static void __nvm_remove_target(struct nvm_target *t, bool graceful) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 455 | { |
| 456 | struct nvm_tgt_type *tt = t->type; |
| 457 | struct gendisk *tdisk = t->disk; |
| 458 | struct request_queue *q = tdisk->queue; |
| 459 | |
| 460 | del_gendisk(tdisk); |
| 461 | blk_cleanup_queue(q); |
| 462 | |
Javier González | 9a69b0e | 2017-01-31 13:17:20 +0100 | [diff] [blame] | 463 | if (tt->sysfs_exit) |
| 464 | tt->sysfs_exit(tdisk); |
| 465 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 466 | if (tt->exit) |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 467 | tt->exit(tdisk->private_data, graceful); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 468 | |
Javier González | edee1bd | 2017-04-15 20:55:42 +0200 | [diff] [blame] | 469 | nvm_remove_tgt_dev(t->dev, 1); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 470 | put_disk(tdisk); |
Rakesh Pandit | 9001482 | 2017-10-13 14:45:50 +0200 | [diff] [blame] | 471 | module_put(t->type->owner); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 472 | |
| 473 | list_del(&t->list); |
| 474 | kfree(t); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * nvm_remove_tgt - Removes a target from the media manager |
| 479 | * @dev: device |
| 480 | * @remove: ioctl structure with target name to remove. |
| 481 | * |
| 482 | * Returns: |
| 483 | * 0: on success |
| 484 | * 1: on not found |
| 485 | * <0: on error |
| 486 | */ |
| 487 | static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove) |
| 488 | { |
| 489 | struct nvm_target *t; |
| 490 | |
| 491 | mutex_lock(&dev->mlock); |
| 492 | t = nvm_find_target(dev, remove->tgtname); |
| 493 | if (!t) { |
| 494 | mutex_unlock(&dev->mlock); |
| 495 | return 1; |
| 496 | } |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 497 | __nvm_remove_target(t, true); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 498 | mutex_unlock(&dev->mlock); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int nvm_register_map(struct nvm_dev *dev) |
| 504 | { |
| 505 | struct nvm_dev_map *rmap; |
| 506 | int i, j; |
| 507 | |
| 508 | rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL); |
| 509 | if (!rmap) |
| 510 | goto err_rmap; |
| 511 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 512 | rmap->chnls = kcalloc(dev->geo.num_ch, sizeof(struct nvm_ch_map), |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 513 | GFP_KERNEL); |
| 514 | if (!rmap->chnls) |
| 515 | goto err_chnls; |
| 516 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 517 | for (i = 0; i < dev->geo.num_ch; i++) { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 518 | struct nvm_ch_map *ch_rmap; |
| 519 | int *lun_roffs; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 520 | int luns_in_chnl = dev->geo.num_lun; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 521 | |
| 522 | ch_rmap = &rmap->chnls[i]; |
| 523 | |
| 524 | ch_rmap->ch_off = -1; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 525 | ch_rmap->num_lun = luns_in_chnl; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 526 | |
| 527 | lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL); |
| 528 | if (!lun_roffs) |
| 529 | goto err_ch; |
| 530 | |
| 531 | for (j = 0; j < luns_in_chnl; j++) |
| 532 | lun_roffs[j] = -1; |
| 533 | |
| 534 | ch_rmap->lun_offs = lun_roffs; |
| 535 | } |
| 536 | |
| 537 | dev->rmap = rmap; |
| 538 | |
| 539 | return 0; |
| 540 | err_ch: |
| 541 | while (--i >= 0) |
| 542 | kfree(rmap->chnls[i].lun_offs); |
| 543 | err_chnls: |
| 544 | kfree(rmap); |
| 545 | err_rmap: |
| 546 | return -ENOMEM; |
| 547 | } |
| 548 | |
Javier González | 7a3de2b | 2017-04-15 20:55:39 +0200 | [diff] [blame] | 549 | static void nvm_unregister_map(struct nvm_dev *dev) |
| 550 | { |
| 551 | struct nvm_dev_map *rmap = dev->rmap; |
| 552 | int i; |
| 553 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 554 | for (i = 0; i < dev->geo.num_ch; i++) |
Javier González | 7a3de2b | 2017-04-15 20:55:39 +0200 | [diff] [blame] | 555 | kfree(rmap->chnls[i].lun_offs); |
| 556 | |
| 557 | kfree(rmap->chnls); |
| 558 | kfree(rmap); |
| 559 | } |
| 560 | |
Matias Bjørling | 61a561d | 2017-01-31 13:17:13 +0100 | [diff] [blame] | 561 | 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] | 562 | { |
| 563 | struct nvm_dev_map *dev_map = tgt_dev->map; |
Javier González | 6947151 | 2018-03-30 00:05:15 +0200 | [diff] [blame] | 564 | struct nvm_ch_map *ch_map = &dev_map->chnls[p->a.ch]; |
| 565 | int lun_off = ch_map->lun_offs[p->a.lun]; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 566 | |
Javier González | 6947151 | 2018-03-30 00:05:15 +0200 | [diff] [blame] | 567 | p->a.ch += ch_map->ch_off; |
| 568 | p->a.lun += lun_off; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Matias Bjørling | 61a561d | 2017-01-31 13:17:13 +0100 | [diff] [blame] | 571 | 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] | 572 | { |
| 573 | struct nvm_dev *dev = tgt_dev->parent; |
| 574 | struct nvm_dev_map *dev_rmap = dev->rmap; |
Javier González | 6947151 | 2018-03-30 00:05:15 +0200 | [diff] [blame] | 575 | struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->a.ch]; |
| 576 | int lun_roff = ch_rmap->lun_offs[p->a.lun]; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 577 | |
Javier González | 6947151 | 2018-03-30 00:05:15 +0200 | [diff] [blame] | 578 | p->a.ch -= ch_rmap->ch_off; |
| 579 | p->a.lun -= lun_roff; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 582 | static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, |
| 583 | struct ppa_addr *ppa_list, int nr_ppas) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 584 | { |
| 585 | int i; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 586 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 587 | for (i = 0; i < nr_ppas; i++) { |
| 588 | nvm_map_to_dev(tgt_dev, &ppa_list[i]); |
Javier González | 7100d50 | 2018-03-30 00:05:16 +0200 | [diff] [blame] | 589 | ppa_list[i] = generic_to_dev_addr(tgt_dev->parent, ppa_list[i]); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 590 | } |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 593 | static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, |
| 594 | struct ppa_addr *ppa_list, int nr_ppas) |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 595 | { |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 596 | int i; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 597 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 598 | for (i = 0; i < nr_ppas; i++) { |
Javier González | 7100d50 | 2018-03-30 00:05:16 +0200 | [diff] [blame] | 599 | ppa_list[i] = dev_to_generic_addr(tgt_dev->parent, ppa_list[i]); |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 600 | nvm_map_to_tgt(tgt_dev, &ppa_list[i]); |
| 601 | } |
| 602 | } |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 603 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 604 | static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd) |
| 605 | { |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 606 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 607 | |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 608 | nvm_ppa_tgt_to_dev(tgt_dev, ppa_list, rqd->nr_ppas); |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd) |
| 612 | { |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 613 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 614 | |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 615 | nvm_ppa_dev_to_tgt(tgt_dev, ppa_list, rqd->nr_ppas); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 618 | int nvm_register_tgt_type(struct nvm_tgt_type *tt) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 619 | { |
| 620 | int ret = 0; |
| 621 | |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 622 | down_write(&nvm_tgtt_lock); |
Javier González | e29c80e | 2018-01-05 14:16:04 +0100 | [diff] [blame] | 623 | if (__nvm_find_target_type(tt->name)) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 624 | ret = -EEXIST; |
| 625 | else |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 626 | list_add(&tt->list, &nvm_tgt_types); |
Matias Bjørling | 5cd9078 | 2016-07-07 09:54:17 +0200 | [diff] [blame] | 627 | up_write(&nvm_tgtt_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 628 | |
| 629 | return ret; |
| 630 | } |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 631 | EXPORT_SYMBOL(nvm_register_tgt_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 632 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 633 | void nvm_unregister_tgt_type(struct nvm_tgt_type *tt) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 634 | { |
| 635 | if (!tt) |
| 636 | return; |
| 637 | |
Rakesh Pandit | 88d31ea | 2017-10-13 14:45:52 +0200 | [diff] [blame] | 638 | down_write(&nvm_tgtt_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 639 | list_del(&tt->list); |
Rakesh Pandit | 88d31ea | 2017-10-13 14:45:52 +0200 | [diff] [blame] | 640 | up_write(&nvm_tgtt_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 641 | } |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 642 | EXPORT_SYMBOL(nvm_unregister_tgt_type); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 643 | |
| 644 | void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags, |
| 645 | dma_addr_t *dma_handler) |
| 646 | { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 647 | 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] | 648 | dma_handler); |
| 649 | } |
| 650 | EXPORT_SYMBOL(nvm_dev_dma_alloc); |
| 651 | |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 652 | 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] | 653 | { |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 654 | dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 655 | } |
| 656 | EXPORT_SYMBOL(nvm_dev_dma_free); |
| 657 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 658 | static struct nvm_dev *nvm_find_nvm_dev(const char *name) |
| 659 | { |
| 660 | struct nvm_dev *dev; |
| 661 | |
| 662 | list_for_each_entry(dev, &nvm_devices, devices) |
| 663 | if (!strcmp(name, dev->name)) |
| 664 | return dev; |
| 665 | |
| 666 | return NULL; |
| 667 | } |
| 668 | |
Rakesh Pandit | eb6f168 | 2017-10-13 14:46:31 +0200 | [diff] [blame] | 669 | static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd, |
| 670 | const struct ppa_addr *ppas, int nr_ppas) |
| 671 | { |
| 672 | struct nvm_dev *dev = tgt_dev->parent; |
| 673 | struct nvm_geo *geo = &tgt_dev->geo; |
| 674 | int i, plane_cnt, pl_idx; |
| 675 | struct ppa_addr ppa; |
| 676 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 677 | if (geo->pln_mode == NVM_PLANE_SINGLE && nr_ppas == 1) { |
Rakesh Pandit | eb6f168 | 2017-10-13 14:46:31 +0200 | [diff] [blame] | 678 | rqd->nr_ppas = nr_ppas; |
| 679 | rqd->ppa_addr = ppas[0]; |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | rqd->nr_ppas = nr_ppas; |
| 685 | rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list); |
| 686 | if (!rqd->ppa_list) { |
| 687 | pr_err("nvm: failed to allocate dma memory\n"); |
| 688 | return -ENOMEM; |
| 689 | } |
| 690 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 691 | plane_cnt = geo->pln_mode; |
Rakesh Pandit | eb6f168 | 2017-10-13 14:46:31 +0200 | [diff] [blame] | 692 | rqd->nr_ppas *= plane_cnt; |
| 693 | |
| 694 | for (i = 0; i < nr_ppas; i++) { |
| 695 | for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) { |
| 696 | ppa = ppas[i]; |
| 697 | ppa.g.pl = pl_idx; |
| 698 | rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, |
| 706 | struct nvm_rq *rqd) |
| 707 | { |
| 708 | if (!rqd->ppa_list) |
| 709 | return; |
| 710 | |
| 711 | nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list); |
| 712 | } |
| 713 | |
Matias Bjørling | d7b6801 | 2018-10-09 13:11:32 +0200 | [diff] [blame] | 714 | static int nvm_set_flags(struct nvm_geo *geo, struct nvm_rq *rqd) |
| 715 | { |
| 716 | int flags = 0; |
| 717 | |
| 718 | if (geo->version == NVM_OCSSD_SPEC_20) |
| 719 | return 0; |
| 720 | |
| 721 | if (rqd->is_seq) |
| 722 | flags |= geo->pln_mode >> 1; |
| 723 | |
| 724 | if (rqd->opcode == NVM_OP_PREAD) |
| 725 | flags |= (NVM_IO_SCRAMBLE_ENABLE | NVM_IO_SUSPEND); |
| 726 | else if (rqd->opcode == NVM_OP_PWRITE) |
| 727 | flags |= NVM_IO_SCRAMBLE_ENABLE; |
| 728 | |
| 729 | return flags; |
| 730 | } |
| 731 | |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 732 | 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] | 733 | { |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 734 | struct nvm_dev *dev = tgt_dev->parent; |
Javier González | 3e505af | 2017-06-26 11:57:10 +0200 | [diff] [blame] | 735 | int ret; |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 736 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 737 | if (!dev->ops->submit_io) |
| 738 | return -ENODEV; |
| 739 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 740 | nvm_rq_tgt_to_dev(tgt_dev, rqd); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 741 | |
| 742 | rqd->dev = tgt_dev; |
Matias Bjørling | d7b6801 | 2018-10-09 13:11:32 +0200 | [diff] [blame] | 743 | rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd); |
Javier González | 3e505af | 2017-06-26 11:57:10 +0200 | [diff] [blame] | 744 | |
| 745 | /* In case of error, fail with right address format */ |
| 746 | ret = dev->ops->submit_io(dev, rqd); |
| 747 | if (ret) |
| 748 | nvm_rq_dev_to_tgt(tgt_dev, rqd); |
| 749 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 750 | } |
| 751 | EXPORT_SYMBOL(nvm_submit_io); |
| 752 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 753 | 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] | 754 | { |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 755 | struct nvm_dev *dev = tgt_dev->parent; |
| 756 | int ret; |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame] | 757 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 758 | if (!dev->ops->submit_io_sync) |
| 759 | return -ENODEV; |
| 760 | |
| 761 | nvm_rq_tgt_to_dev(tgt_dev, rqd); |
| 762 | |
| 763 | rqd->dev = tgt_dev; |
Matias Bjørling | d7b6801 | 2018-10-09 13:11:32 +0200 | [diff] [blame] | 764 | rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd); |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 765 | |
| 766 | /* In case of error, fail with right address format */ |
| 767 | ret = dev->ops->submit_io_sync(dev, rqd); |
| 768 | nvm_rq_dev_to_tgt(tgt_dev, rqd); |
| 769 | |
| 770 | return ret; |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame] | 771 | } |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 772 | EXPORT_SYMBOL(nvm_submit_io_sync); |
Javier González | 17912c4 | 2017-04-15 20:55:37 +0200 | [diff] [blame] | 773 | |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 774 | void nvm_end_io(struct nvm_rq *rqd) |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 775 | { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 776 | struct nvm_tgt_dev *tgt_dev = rqd->dev; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 777 | |
| 778 | /* Convert address space */ |
| 779 | if (tgt_dev) |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 780 | nvm_rq_dev_to_tgt(tgt_dev, rqd); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 781 | |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 782 | if (rqd->end_io) |
| 783 | rqd->end_io(rqd); |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 784 | } |
| 785 | EXPORT_SYMBOL(nvm_end_io); |
| 786 | |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 787 | static int nvm_submit_io_sync_raw(struct nvm_dev *dev, struct nvm_rq *rqd) |
| 788 | { |
| 789 | if (!dev->ops->submit_io_sync) |
| 790 | return -ENODEV; |
| 791 | |
| 792 | rqd->flags = nvm_set_flags(&dev->geo, rqd); |
| 793 | |
| 794 | return dev->ops->submit_io_sync(dev, rqd); |
| 795 | } |
| 796 | |
| 797 | static int nvm_bb_chunk_sense(struct nvm_dev *dev, struct ppa_addr ppa) |
| 798 | { |
| 799 | struct nvm_rq rqd = { NULL }; |
| 800 | struct bio bio; |
| 801 | struct bio_vec bio_vec; |
| 802 | struct page *page; |
| 803 | int ret; |
| 804 | |
| 805 | page = alloc_page(GFP_KERNEL); |
| 806 | if (!page) |
| 807 | return -ENOMEM; |
| 808 | |
| 809 | bio_init(&bio, &bio_vec, 1); |
| 810 | bio_add_page(&bio, page, PAGE_SIZE, 0); |
| 811 | bio_set_op_attrs(&bio, REQ_OP_READ, 0); |
| 812 | |
| 813 | rqd.bio = &bio; |
| 814 | rqd.opcode = NVM_OP_PREAD; |
| 815 | rqd.is_seq = 1; |
| 816 | rqd.nr_ppas = 1; |
| 817 | rqd.ppa_addr = generic_to_dev_addr(dev, ppa); |
| 818 | |
| 819 | ret = nvm_submit_io_sync_raw(dev, &rqd); |
| 820 | if (ret) |
| 821 | return ret; |
| 822 | |
| 823 | __free_page(page); |
| 824 | |
| 825 | return rqd.error; |
| 826 | } |
| 827 | |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 828 | /* |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 829 | * Scans a 1.2 chunk first and last page to determine if its state. |
| 830 | * If the chunk is found to be open, also scan it to update the write |
| 831 | * pointer. |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 832 | */ |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 833 | static int nvm_bb_chunk_scan(struct nvm_dev *dev, struct ppa_addr ppa, |
| 834 | struct nvm_chk_meta *meta) |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 835 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 836 | struct nvm_geo *geo = &dev->geo; |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 837 | int ret, pg, pl; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 838 | |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 839 | /* sense first page */ |
| 840 | ret = nvm_bb_chunk_sense(dev, ppa); |
| 841 | if (ret < 0) /* io error */ |
| 842 | return ret; |
| 843 | else if (ret == 0) /* valid data */ |
| 844 | meta->state = NVM_CHK_ST_OPEN; |
| 845 | else if (ret > 0) { |
| 846 | /* |
| 847 | * If empty page, the chunk is free, else it is an |
| 848 | * actual io error. In that case, mark it offline. |
| 849 | */ |
| 850 | switch (ret) { |
| 851 | case NVM_RSP_ERR_EMPTYPAGE: |
| 852 | meta->state = NVM_CHK_ST_FREE; |
| 853 | return 0; |
| 854 | case NVM_RSP_ERR_FAILCRC: |
| 855 | case NVM_RSP_ERR_FAILECC: |
| 856 | case NVM_RSP_WARN_HIGHECC: |
| 857 | meta->state = NVM_CHK_ST_OPEN; |
| 858 | goto scan; |
| 859 | default: |
| 860 | return -ret; /* other io error */ |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | /* sense last page */ |
| 865 | ppa.g.pg = geo->num_pg - 1; |
| 866 | ppa.g.pl = geo->num_pln - 1; |
| 867 | |
| 868 | ret = nvm_bb_chunk_sense(dev, ppa); |
| 869 | if (ret < 0) /* io error */ |
| 870 | return ret; |
| 871 | else if (ret == 0) { /* Chunk fully written */ |
| 872 | meta->state = NVM_CHK_ST_CLOSED; |
| 873 | meta->wp = geo->clba; |
| 874 | return 0; |
| 875 | } else if (ret > 0) { |
| 876 | switch (ret) { |
| 877 | case NVM_RSP_ERR_EMPTYPAGE: |
| 878 | case NVM_RSP_ERR_FAILCRC: |
| 879 | case NVM_RSP_ERR_FAILECC: |
| 880 | case NVM_RSP_WARN_HIGHECC: |
| 881 | meta->state = NVM_CHK_ST_OPEN; |
| 882 | break; |
| 883 | default: |
| 884 | return -ret; /* other io error */ |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | scan: |
| 889 | /* |
| 890 | * chunk is open, we scan sequentially to update the write pointer. |
| 891 | * We make the assumption that targets write data across all planes |
| 892 | * before moving to the next page. |
| 893 | */ |
| 894 | for (pg = 0; pg < geo->num_pg; pg++) { |
| 895 | for (pl = 0; pl < geo->num_pln; pl++) { |
| 896 | ppa.g.pg = pg; |
| 897 | ppa.g.pl = pl; |
| 898 | |
| 899 | ret = nvm_bb_chunk_sense(dev, ppa); |
| 900 | if (ret < 0) /* io error */ |
| 901 | return ret; |
| 902 | else if (ret == 0) { |
| 903 | meta->wp += geo->ws_min; |
| 904 | } else if (ret > 0) { |
| 905 | switch (ret) { |
| 906 | case NVM_RSP_ERR_EMPTYPAGE: |
| 907 | return 0; |
| 908 | case NVM_RSP_ERR_FAILCRC: |
| 909 | case NVM_RSP_ERR_FAILECC: |
| 910 | case NVM_RSP_WARN_HIGHECC: |
| 911 | meta->wp += geo->ws_min; |
| 912 | break; |
| 913 | default: |
| 914 | return -ret; /* other io error */ |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * folds a bad block list from its plane representation to its |
| 925 | * chunk representation. |
| 926 | * |
| 927 | * If any of the planes status are bad or grown bad, the chunk is marked |
| 928 | * offline. If not bad, the first plane state acts as the chunk state. |
| 929 | */ |
| 930 | static int nvm_bb_to_chunk(struct nvm_dev *dev, struct ppa_addr ppa, |
| 931 | u8 *blks, int nr_blks, struct nvm_chk_meta *meta) |
| 932 | { |
| 933 | struct nvm_geo *geo = &dev->geo; |
| 934 | int ret, blk, pl, offset, blktype; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 935 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 936 | for (blk = 0; blk < geo->num_chk; blk++) { |
| 937 | offset = blk * geo->pln_mode; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 938 | blktype = blks[offset]; |
| 939 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 940 | for (pl = 0; pl < geo->pln_mode; pl++) { |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 941 | if (blks[offset + pl] & |
| 942 | (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) { |
| 943 | blktype = blks[offset + pl]; |
| 944 | break; |
| 945 | } |
| 946 | } |
| 947 | |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 948 | ppa.g.blk = blk; |
| 949 | |
| 950 | meta->wp = 0; |
| 951 | meta->type = NVM_CHK_TP_W_SEQ; |
| 952 | meta->wi = 0; |
| 953 | meta->slba = generic_to_dev_addr(dev, ppa).ppa; |
| 954 | meta->cnlb = dev->geo.clba; |
| 955 | |
| 956 | if (blktype == NVM_BLK_T_FREE) { |
| 957 | ret = nvm_bb_chunk_scan(dev, ppa, meta); |
| 958 | if (ret) |
| 959 | return ret; |
| 960 | } else { |
| 961 | meta->state = NVM_CHK_ST_OFFLINE; |
| 962 | } |
| 963 | |
| 964 | meta++; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 965 | } |
| 966 | |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 967 | return 0; |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 968 | } |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 969 | |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 970 | static int nvm_get_bb_meta(struct nvm_dev *dev, sector_t slba, |
| 971 | int nchks, struct nvm_chk_meta *meta) |
| 972 | { |
| 973 | struct nvm_geo *geo = &dev->geo; |
| 974 | struct ppa_addr ppa; |
| 975 | u8 *blks; |
| 976 | int ch, lun, nr_blks; |
Geert Uytterhoeven | 55e58c5 | 2018-12-11 20:16:08 +0100 | [diff] [blame^] | 977 | int ret = 0; |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 978 | |
| 979 | ppa.ppa = slba; |
| 980 | ppa = dev_to_generic_addr(dev, ppa); |
| 981 | |
| 982 | if (ppa.g.blk != 0) |
| 983 | return -EINVAL; |
| 984 | |
| 985 | if ((nchks % geo->num_chk) != 0) |
| 986 | return -EINVAL; |
| 987 | |
| 988 | nr_blks = geo->num_chk * geo->pln_mode; |
| 989 | |
| 990 | blks = kmalloc(nr_blks, GFP_KERNEL); |
| 991 | if (!blks) |
| 992 | return -ENOMEM; |
| 993 | |
| 994 | for (ch = ppa.g.ch; ch < geo->num_ch; ch++) { |
| 995 | for (lun = ppa.g.lun; lun < geo->num_lun; lun++) { |
| 996 | struct ppa_addr ppa_gen, ppa_dev; |
| 997 | |
| 998 | if (!nchks) |
| 999 | goto done; |
| 1000 | |
| 1001 | ppa_gen.ppa = 0; |
| 1002 | ppa_gen.g.ch = ch; |
| 1003 | ppa_gen.g.lun = lun; |
| 1004 | ppa_dev = generic_to_dev_addr(dev, ppa_gen); |
| 1005 | |
| 1006 | ret = dev->ops->get_bb_tbl(dev, ppa_dev, blks); |
| 1007 | if (ret) |
| 1008 | goto done; |
| 1009 | |
| 1010 | ret = nvm_bb_to_chunk(dev, ppa_gen, blks, nr_blks, |
| 1011 | meta); |
| 1012 | if (ret) |
| 1013 | goto done; |
| 1014 | |
| 1015 | meta += geo->num_chk; |
| 1016 | nchks -= geo->num_chk; |
| 1017 | } |
| 1018 | } |
| 1019 | done: |
| 1020 | kfree(blks); |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa, |
| 1025 | int nchks, struct nvm_chk_meta *meta) |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 1026 | { |
Matias Bjørling | 8f4fe00 | 2017-01-31 13:17:12 +0100 | [diff] [blame] | 1027 | struct nvm_dev *dev = tgt_dev->parent; |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 1028 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 1029 | nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1); |
Matias Bjørling | 8f4fe00 | 2017-01-31 13:17:12 +0100 | [diff] [blame] | 1030 | |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 1031 | if (dev->geo.version == NVM_OCSSD_SPEC_12) |
| 1032 | return nvm_get_bb_meta(dev, (sector_t)ppa.ppa, nchks, meta); |
| 1033 | |
| 1034 | return dev->ops->get_chk_meta(dev, (sector_t)ppa.ppa, nchks, meta); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 1035 | } |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 1036 | EXPORT_SYMBOL_GPL(nvm_get_chunk_meta); |
| 1037 | |
| 1038 | int nvm_set_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas, |
| 1039 | int nr_ppas, int type) |
| 1040 | { |
| 1041 | struct nvm_dev *dev = tgt_dev->parent; |
| 1042 | struct nvm_rq rqd; |
| 1043 | int ret; |
| 1044 | |
| 1045 | if (dev->geo.version == NVM_OCSSD_SPEC_20) |
| 1046 | return 0; |
| 1047 | |
| 1048 | if (nr_ppas > NVM_MAX_VLBA) { |
| 1049 | pr_err("nvm: unable to update all blocks atomically\n"); |
| 1050 | return -EINVAL; |
| 1051 | } |
| 1052 | |
| 1053 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 1054 | |
| 1055 | nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas); |
| 1056 | nvm_rq_tgt_to_dev(tgt_dev, &rqd); |
| 1057 | |
| 1058 | ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type); |
| 1059 | nvm_free_rqd_ppalist(tgt_dev, &rqd); |
| 1060 | if (ret) |
| 1061 | return -EINVAL; |
| 1062 | |
| 1063 | return 0; |
| 1064 | } |
| 1065 | EXPORT_SYMBOL_GPL(nvm_set_chunk_meta); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 1066 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1067 | static int nvm_core_init(struct nvm_dev *dev) |
| 1068 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 1069 | struct nvm_geo *geo = &dev->geo; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 1070 | int ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1071 | |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 1072 | dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns), |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 1073 | sizeof(unsigned long), GFP_KERNEL); |
| 1074 | if (!dev->lun_map) |
| 1075 | return -ENOMEM; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 1076 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1077 | INIT_LIST_HEAD(&dev->area_list); |
| 1078 | INIT_LIST_HEAD(&dev->targets); |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 1079 | mutex_init(&dev->mlock); |
Wenwei Tao | 4c9dacb | 2016-03-03 15:06:37 +0100 | [diff] [blame] | 1080 | spin_lock_init(&dev->lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1081 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1082 | ret = nvm_register_map(dev); |
| 1083 | if (ret) |
| 1084 | goto err_fmtype; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1085 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1086 | return 0; |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 1087 | err_fmtype: |
| 1088 | kfree(dev->lun_map); |
| 1089 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
Javier González | 46b160c | 2017-04-15 20:55:46 +0200 | [diff] [blame] | 1092 | static void nvm_free(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1093 | { |
| 1094 | if (!dev) |
| 1095 | return; |
| 1096 | |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 1097 | if (dev->dma_pool) |
| 1098 | dev->ops->destroy_dma_pool(dev->dma_pool); |
| 1099 | |
Javier González | 7a3de2b | 2017-04-15 20:55:39 +0200 | [diff] [blame] | 1100 | nvm_unregister_map(dev); |
Matias Bjørling | 7f7c5d0 | 2016-05-06 20:02:59 +0200 | [diff] [blame] | 1101 | kfree(dev->lun_map); |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 1102 | kfree(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static int nvm_init(struct nvm_dev *dev) |
| 1106 | { |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 1107 | struct nvm_geo *geo = &dev->geo; |
Wenwei Tao | 480fc0d | 2015-11-20 13:47:53 +0100 | [diff] [blame] | 1108 | int ret = -EINVAL; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1109 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 1110 | if (dev->ops->identity(dev)) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1111 | pr_err("nvm: device could not be identified\n"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1112 | goto err; |
| 1113 | } |
| 1114 | |
Javier González | 3cb98f8 | 2018-03-30 00:05:11 +0200 | [diff] [blame] | 1115 | pr_debug("nvm: ver:%u.%u nvm_vendor:%x\n", |
| 1116 | geo->major_ver_id, geo->minor_ver_id, |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 1117 | geo->vmnt); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1118 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1119 | ret = nvm_core_init(dev); |
| 1120 | if (ret) { |
| 1121 | pr_err("nvm: could not initialize core structures.\n"); |
| 1122 | goto err; |
| 1123 | } |
| 1124 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 1125 | pr_info("nvm: registered %s [%u/%u/%u/%u/%u]\n", |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 1126 | dev->name, dev->geo.ws_min, dev->geo.ws_opt, |
| 1127 | dev->geo.num_chk, dev->geo.all_luns, |
| 1128 | dev->geo.num_ch); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1129 | return 0; |
| 1130 | err: |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1131 | pr_err("nvm: failed to initialize nvm\n"); |
| 1132 | return ret; |
| 1133 | } |
| 1134 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1135 | struct nvm_dev *nvm_alloc_dev(int node) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1136 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1137 | return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node); |
| 1138 | } |
| 1139 | EXPORT_SYMBOL(nvm_alloc_dev); |
| 1140 | |
| 1141 | int nvm_register(struct nvm_dev *dev) |
| 1142 | { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1143 | int ret; |
| 1144 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1145 | if (!dev->q || !dev->ops) |
| 1146 | return -EINVAL; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1147 | |
Matias Bjørling | 89a09c5 | 2018-03-30 00:05:04 +0200 | [diff] [blame] | 1148 | dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist"); |
| 1149 | if (!dev->dma_pool) { |
| 1150 | pr_err("nvm: could not create dma pool\n"); |
| 1151 | return -ENOMEM; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1152 | } |
| 1153 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1154 | ret = nvm_init(dev); |
| 1155 | if (ret) |
| 1156 | goto err_init; |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 1157 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 1158 | /* register device with a supported media manager */ |
Matias Bjørling | edad2e6 | 2015-11-16 15:34:42 +0100 | [diff] [blame] | 1159 | down_write(&nvm_lock); |
| 1160 | list_add(&dev->devices, &nvm_devices); |
| 1161 | up_write(&nvm_lock); |
| 1162 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1163 | return 0; |
| 1164 | err_init: |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1165 | dev->ops->destroy_dma_pool(dev->dma_pool); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1166 | return ret; |
| 1167 | } |
| 1168 | EXPORT_SYMBOL(nvm_register); |
| 1169 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1170 | void nvm_unregister(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1171 | { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1172 | struct nvm_target *t, *tmp; |
| 1173 | |
| 1174 | mutex_lock(&dev->mlock); |
| 1175 | list_for_each_entry_safe(t, tmp, &dev->targets, list) { |
| 1176 | if (t->dev->parent != dev) |
| 1177 | continue; |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 1178 | __nvm_remove_target(t, false); |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1179 | } |
| 1180 | mutex_unlock(&dev->mlock); |
| 1181 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 1182 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1183 | list_del(&dev->devices); |
| 1184 | up_write(&nvm_lock); |
Matias Bjørling | c1480ad | 2015-11-16 15:34:43 +0100 | [diff] [blame] | 1185 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 1186 | nvm_free(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1187 | } |
| 1188 | EXPORT_SYMBOL(nvm_unregister); |
| 1189 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1190 | static int __nvm_configure_create(struct nvm_ioctl_create *create) |
| 1191 | { |
| 1192 | struct nvm_dev *dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1193 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 1194 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1195 | dev = nvm_find_nvm_dev(create->dev); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 1196 | up_write(&nvm_lock); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1197 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1198 | if (!dev) { |
| 1199 | pr_err("nvm: device not found\n"); |
| 1200 | return -EINVAL; |
| 1201 | } |
| 1202 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1203 | return nvm_create_tgt(dev, create); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1206 | static long nvm_ioctl_info(struct file *file, void __user *arg) |
| 1207 | { |
| 1208 | struct nvm_ioctl_info *info; |
| 1209 | struct nvm_tgt_type *tt; |
| 1210 | int tgt_iter = 0; |
| 1211 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1212 | info = memdup_user(arg, sizeof(struct nvm_ioctl_info)); |
| 1213 | if (IS_ERR(info)) |
| 1214 | return -EFAULT; |
| 1215 | |
| 1216 | info->version[0] = NVM_VERSION_MAJOR; |
| 1217 | info->version[1] = NVM_VERSION_MINOR; |
| 1218 | info->version[2] = NVM_VERSION_PATCH; |
| 1219 | |
Rakesh Pandit | 88d31ea | 2017-10-13 14:45:52 +0200 | [diff] [blame] | 1220 | down_write(&nvm_tgtt_lock); |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 1221 | list_for_each_entry(tt, &nvm_tgt_types, list) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1222 | struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter]; |
| 1223 | |
| 1224 | tgt->version[0] = tt->version[0]; |
| 1225 | tgt->version[1] = tt->version[1]; |
| 1226 | tgt->version[2] = tt->version[2]; |
| 1227 | strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX); |
| 1228 | |
| 1229 | tgt_iter++; |
| 1230 | } |
| 1231 | |
| 1232 | info->tgtsize = tgt_iter; |
Rakesh Pandit | 88d31ea | 2017-10-13 14:45:52 +0200 | [diff] [blame] | 1233 | up_write(&nvm_tgtt_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1234 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1235 | if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) { |
| 1236 | kfree(info); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1237 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1238 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1239 | |
| 1240 | kfree(info); |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | static long nvm_ioctl_get_devices(struct file *file, void __user *arg) |
| 1245 | { |
| 1246 | struct nvm_ioctl_get_devices *devices; |
| 1247 | struct nvm_dev *dev; |
| 1248 | int i = 0; |
| 1249 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1250 | devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL); |
| 1251 | if (!devices) |
| 1252 | return -ENOMEM; |
| 1253 | |
| 1254 | down_write(&nvm_lock); |
| 1255 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 1256 | struct nvm_ioctl_device_info *info = &devices->info[i]; |
| 1257 | |
Javier González | 6eb0824 | 2017-04-15 20:55:49 +0200 | [diff] [blame] | 1258 | strlcpy(info->devname, dev->name, sizeof(info->devname)); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1259 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1260 | /* kept for compatibility */ |
| 1261 | info->bmversion[0] = 1; |
| 1262 | info->bmversion[1] = 0; |
| 1263 | info->bmversion[2] = 0; |
Javier González | 6eb0824 | 2017-04-15 20:55:49 +0200 | [diff] [blame] | 1264 | strlcpy(info->bmname, "gennvm", sizeof(info->bmname)); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1265 | i++; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1266 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1267 | if (i > 31) { |
| 1268 | pr_err("nvm: max 31 devices can be reported.\n"); |
| 1269 | break; |
| 1270 | } |
| 1271 | } |
| 1272 | up_write(&nvm_lock); |
| 1273 | |
| 1274 | devices->nr_devices = i; |
| 1275 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1276 | if (copy_to_user(arg, devices, |
| 1277 | sizeof(struct nvm_ioctl_get_devices))) { |
| 1278 | kfree(devices); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1279 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 1280 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1281 | |
| 1282 | kfree(devices); |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | static long nvm_ioctl_dev_create(struct file *file, void __user *arg) |
| 1287 | { |
| 1288 | struct nvm_ioctl_create create; |
| 1289 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1290 | if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create))) |
| 1291 | return -EFAULT; |
| 1292 | |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 1293 | if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED && |
| 1294 | create.conf.e.rsv != 0) { |
| 1295 | pr_err("nvm: reserved config field in use\n"); |
| 1296 | return -EINVAL; |
| 1297 | } |
| 1298 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1299 | create.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1300 | create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0'; |
| 1301 | create.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1302 | |
| 1303 | if (create.flags != 0) { |
Javier González | 4af3f75 | 2017-04-15 20:55:45 +0200 | [diff] [blame] | 1304 | __u32 flags = create.flags; |
| 1305 | |
| 1306 | /* Check for valid flags */ |
| 1307 | if (flags & NVM_TARGET_FACTORY) |
| 1308 | flags &= ~NVM_TARGET_FACTORY; |
| 1309 | |
| 1310 | if (flags) { |
| 1311 | pr_err("nvm: flag not supported\n"); |
| 1312 | return -EINVAL; |
| 1313 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | return __nvm_configure_create(&create); |
| 1317 | } |
| 1318 | |
| 1319 | static long nvm_ioctl_dev_remove(struct file *file, void __user *arg) |
| 1320 | { |
| 1321 | struct nvm_ioctl_remove remove; |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1322 | struct nvm_dev *dev; |
| 1323 | int ret = 0; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1324 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1325 | if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove))) |
| 1326 | return -EFAULT; |
| 1327 | |
| 1328 | remove.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 1329 | |
| 1330 | if (remove.flags != 0) { |
| 1331 | pr_err("nvm: no flags supported\n"); |
| 1332 | return -EINVAL; |
| 1333 | } |
| 1334 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1335 | list_for_each_entry(dev, &nvm_devices, devices) { |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1336 | ret = nvm_remove_tgt(dev, &remove); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 1337 | if (!ret) |
| 1338 | break; |
| 1339 | } |
| 1340 | |
| 1341 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1342 | } |
| 1343 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1344 | /* kept for compatibility reasons */ |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1345 | static long nvm_ioctl_dev_init(struct file *file, void __user *arg) |
| 1346 | { |
| 1347 | struct nvm_ioctl_dev_init init; |
| 1348 | |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1349 | if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init))) |
| 1350 | return -EFAULT; |
| 1351 | |
| 1352 | if (init.flags != 0) { |
| 1353 | pr_err("nvm: no flags supported\n"); |
| 1354 | return -EINVAL; |
| 1355 | } |
| 1356 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1357 | return 0; |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1358 | } |
| 1359 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 1360 | /* Kept for compatibility reasons */ |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1361 | static long nvm_ioctl_dev_factory(struct file *file, void __user *arg) |
| 1362 | { |
| 1363 | struct nvm_ioctl_dev_factory fact; |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1364 | |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1365 | if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory))) |
| 1366 | return -EFAULT; |
| 1367 | |
| 1368 | fact.dev[DISK_NAME_LEN - 1] = '\0'; |
| 1369 | |
| 1370 | if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1)) |
| 1371 | return -EINVAL; |
| 1372 | |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1373 | return 0; |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1374 | } |
| 1375 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1376 | static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg) |
| 1377 | { |
| 1378 | void __user *argp = (void __user *)arg; |
| 1379 | |
Johannes Thumshirn | 40f962d | 2018-03-30 00:05:07 +0200 | [diff] [blame] | 1380 | if (!capable(CAP_SYS_ADMIN)) |
| 1381 | return -EPERM; |
| 1382 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1383 | switch (cmd) { |
| 1384 | case NVM_INFO: |
| 1385 | return nvm_ioctl_info(file, argp); |
| 1386 | case NVM_GET_DEVICES: |
| 1387 | return nvm_ioctl_get_devices(file, argp); |
| 1388 | case NVM_DEV_CREATE: |
| 1389 | return nvm_ioctl_dev_create(file, argp); |
| 1390 | case NVM_DEV_REMOVE: |
| 1391 | return nvm_ioctl_dev_remove(file, argp); |
Matias Bjørling | 5569615 | 2016-01-12 07:49:37 +0100 | [diff] [blame] | 1392 | case NVM_DEV_INIT: |
| 1393 | return nvm_ioctl_dev_init(file, argp); |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 1394 | case NVM_DEV_FACTORY: |
| 1395 | return nvm_ioctl_dev_factory(file, argp); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1396 | } |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | static const struct file_operations _ctl_fops = { |
| 1401 | .open = nonseekable_open, |
| 1402 | .unlocked_ioctl = nvm_ctl_ioctl, |
| 1403 | .owner = THIS_MODULE, |
| 1404 | .llseek = noop_llseek, |
| 1405 | }; |
| 1406 | |
| 1407 | static struct miscdevice _nvm_misc = { |
| 1408 | .minor = MISC_DYNAMIC_MINOR, |
| 1409 | .name = "lightnvm", |
| 1410 | .nodename = "lightnvm/control", |
| 1411 | .fops = &_ctl_fops, |
| 1412 | }; |
Paul Gortmaker | 389b2a1 | 2016-10-29 16:38:41 -0400 | [diff] [blame] | 1413 | builtin_misc_device(_nvm_misc); |