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