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