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