Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1 | #ifndef NVM_H |
| 2 | #define NVM_H |
| 3 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 4 | #include <linux/blkdev.h> |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 5 | #include <linux/types.h> |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 6 | #include <uapi/linux/lightnvm.h> |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 7 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 8 | enum { |
| 9 | NVM_IO_OK = 0, |
| 10 | NVM_IO_REQUEUE = 1, |
| 11 | NVM_IO_DONE = 2, |
| 12 | NVM_IO_ERR = 3, |
| 13 | |
| 14 | NVM_IOTYPE_NONE = 0, |
| 15 | NVM_IOTYPE_GC = 1, |
| 16 | }; |
| 17 | |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 18 | #define NVM_BLK_BITS (16) |
| 19 | #define NVM_PG_BITS (16) |
| 20 | #define NVM_SEC_BITS (8) |
| 21 | #define NVM_PL_BITS (8) |
| 22 | #define NVM_LUN_BITS (8) |
Matias Bjørling | df414b3 | 2016-05-06 20:03:19 +0200 | [diff] [blame] | 23 | #define NVM_CH_BITS (7) |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 24 | |
| 25 | struct ppa_addr { |
| 26 | /* Generic structure for all addresses */ |
| 27 | union { |
| 28 | struct { |
| 29 | u64 blk : NVM_BLK_BITS; |
| 30 | u64 pg : NVM_PG_BITS; |
| 31 | u64 sec : NVM_SEC_BITS; |
| 32 | u64 pl : NVM_PL_BITS; |
| 33 | u64 lun : NVM_LUN_BITS; |
| 34 | u64 ch : NVM_CH_BITS; |
Matias Bjørling | df414b3 | 2016-05-06 20:03:19 +0200 | [diff] [blame] | 35 | u64 reserved : 1; |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 36 | } g; |
| 37 | |
Matias Bjørling | df414b3 | 2016-05-06 20:03:19 +0200 | [diff] [blame] | 38 | struct { |
| 39 | u64 line : 63; |
| 40 | u64 is_cached : 1; |
| 41 | } c; |
| 42 | |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 43 | u64 ppa; |
| 44 | }; |
| 45 | }; |
| 46 | |
| 47 | struct nvm_rq; |
| 48 | struct nvm_id; |
| 49 | struct nvm_dev; |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 50 | struct nvm_tgt_dev; |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 51 | |
| 52 | typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *); |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 53 | typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *); |
| 54 | typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32, |
| 55 | nvm_l2p_update_fn *, void *); |
Matias Bjørling | e11903f | 2016-05-06 20:03:05 +0200 | [diff] [blame] | 56 | typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, u8 *); |
Matias Bjørling | 00ee6cc | 2016-05-06 20:03:09 +0200 | [diff] [blame] | 57 | typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct ppa_addr *, int, int); |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 58 | typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *); |
| 59 | typedef int (nvm_erase_blk_fn)(struct nvm_dev *, struct nvm_rq *); |
| 60 | typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *); |
| 61 | typedef void (nvm_destroy_dma_pool_fn)(void *); |
| 62 | typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t, |
| 63 | dma_addr_t *); |
| 64 | typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t); |
| 65 | |
| 66 | struct nvm_dev_ops { |
| 67 | nvm_id_fn *identity; |
| 68 | nvm_get_l2p_tbl_fn *get_l2p_tbl; |
| 69 | nvm_op_bb_tbl_fn *get_bb_tbl; |
| 70 | nvm_op_set_bb_fn *set_bb_tbl; |
| 71 | |
| 72 | nvm_submit_io_fn *submit_io; |
| 73 | nvm_erase_blk_fn *erase_block; |
| 74 | |
| 75 | nvm_create_dma_pool_fn *create_dma_pool; |
| 76 | nvm_destroy_dma_pool_fn *destroy_dma_pool; |
| 77 | nvm_dev_dma_alloc_fn *dev_dma_alloc; |
| 78 | nvm_dev_dma_free_fn *dev_dma_free; |
| 79 | |
| 80 | unsigned int max_phys_sect; |
| 81 | }; |
| 82 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 83 | #ifdef CONFIG_NVM |
| 84 | |
| 85 | #include <linux/blkdev.h> |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 86 | #include <linux/file.h> |
| 87 | #include <linux/dmapool.h> |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 88 | #include <uapi/linux/lightnvm.h> |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 89 | |
| 90 | enum { |
| 91 | /* HW Responsibilities */ |
| 92 | NVM_RSP_L2P = 1 << 0, |
| 93 | NVM_RSP_ECC = 1 << 1, |
| 94 | |
| 95 | /* Physical Adressing Mode */ |
| 96 | NVM_ADDRMODE_LINEAR = 0, |
| 97 | NVM_ADDRMODE_CHANNEL = 1, |
| 98 | |
| 99 | /* Plane programming mode for LUN */ |
Matias Bjørling | d5bdec8 | 2016-02-19 13:56:58 +0100 | [diff] [blame] | 100 | NVM_PLANE_SINGLE = 1, |
| 101 | NVM_PLANE_DOUBLE = 2, |
| 102 | NVM_PLANE_QUAD = 4, |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 103 | |
| 104 | /* Status codes */ |
| 105 | NVM_RSP_SUCCESS = 0x0, |
| 106 | NVM_RSP_NOT_CHANGEABLE = 0x1, |
| 107 | NVM_RSP_ERR_FAILWRITE = 0x40ff, |
| 108 | NVM_RSP_ERR_EMPTYPAGE = 0x42ff, |
Javier González | 402ab9a | 2016-11-28 22:38:57 +0100 | [diff] [blame] | 109 | NVM_RSP_ERR_FAILECC = 0x4281, |
Javier González | 38ea2f7 | 2017-01-31 13:17:18 +0100 | [diff] [blame] | 110 | NVM_RSP_ERR_FAILCRC = 0x4004, |
Javier González | 402ab9a | 2016-11-28 22:38:57 +0100 | [diff] [blame] | 111 | NVM_RSP_WARN_HIGHECC = 0x4700, |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 112 | |
| 113 | /* Device opcodes */ |
| 114 | NVM_OP_HBREAD = 0x02, |
| 115 | NVM_OP_HBWRITE = 0x81, |
| 116 | NVM_OP_PWRITE = 0x91, |
| 117 | NVM_OP_PREAD = 0x92, |
| 118 | NVM_OP_ERASE = 0x90, |
| 119 | |
| 120 | /* PPA Command Flags */ |
| 121 | NVM_IO_SNGL_ACCESS = 0x0, |
| 122 | NVM_IO_DUAL_ACCESS = 0x1, |
| 123 | NVM_IO_QUAD_ACCESS = 0x2, |
| 124 | |
Matias Bjørling | 57b4bd0 | 2015-12-06 11:25:47 +0100 | [diff] [blame] | 125 | /* NAND Access Modes */ |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 126 | NVM_IO_SUSPEND = 0x80, |
| 127 | NVM_IO_SLC_MODE = 0x100, |
| 128 | NVM_IO_SCRAMBLE_DISABLE = 0x200, |
Matias Bjørling | 57b4bd0 | 2015-12-06 11:25:47 +0100 | [diff] [blame] | 129 | |
| 130 | /* Block Types */ |
| 131 | NVM_BLK_T_FREE = 0x0, |
| 132 | NVM_BLK_T_BAD = 0x1, |
Matias Bjørling | b5d4acd | 2016-01-12 07:49:32 +0100 | [diff] [blame] | 133 | NVM_BLK_T_GRWN_BAD = 0x2, |
| 134 | NVM_BLK_T_DEV = 0x4, |
| 135 | NVM_BLK_T_HOST = 0x8, |
Matias Bjørling | f9a9995 | 2016-01-12 07:49:34 +0100 | [diff] [blame] | 136 | |
| 137 | /* Memory capabilities */ |
| 138 | NVM_ID_CAP_SLC = 0x1, |
| 139 | NVM_ID_CAP_CMD_SUSPEND = 0x2, |
| 140 | NVM_ID_CAP_SCRAMBLE = 0x4, |
| 141 | NVM_ID_CAP_ENCRYPT = 0x8, |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 142 | |
| 143 | /* Memory types */ |
| 144 | NVM_ID_FMTYPE_SLC = 0, |
| 145 | NVM_ID_FMTYPE_MLC = 1, |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 146 | |
| 147 | /* Device capabilities */ |
| 148 | NVM_ID_DCAP_BBLKMGMT = 0x1, |
| 149 | NVM_UD_DCAP_ECC = 0x2, |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | struct nvm_id_lp_mlc { |
| 153 | u16 num_pairs; |
| 154 | u8 pairs[886]; |
| 155 | }; |
| 156 | |
| 157 | struct nvm_id_lp_tbl { |
| 158 | __u8 id[8]; |
| 159 | struct nvm_id_lp_mlc mlc; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | struct nvm_id_group { |
| 163 | u8 mtype; |
| 164 | u8 fmtype; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 165 | u8 num_ch; |
| 166 | u8 num_lun; |
| 167 | u8 num_pln; |
| 168 | u16 num_blk; |
| 169 | u16 num_pg; |
| 170 | u16 fpg_sz; |
| 171 | u16 csecs; |
| 172 | u16 sos; |
| 173 | u32 trdt; |
| 174 | u32 trdm; |
| 175 | u32 tprt; |
| 176 | u32 tprm; |
| 177 | u32 tbet; |
| 178 | u32 tbem; |
| 179 | u32 mpos; |
Matias Bjørling | 12be5ed | 2015-11-16 15:34:39 +0100 | [diff] [blame] | 180 | u32 mccap; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 181 | u16 cpar; |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 182 | |
| 183 | struct nvm_id_lp_tbl lptbl; |
Matias Bjørling | 73387e7 | 2015-11-16 15:34:40 +0100 | [diff] [blame] | 184 | }; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 185 | |
| 186 | struct nvm_addr_format { |
| 187 | u8 ch_offset; |
| 188 | u8 ch_len; |
| 189 | u8 lun_offset; |
| 190 | u8 lun_len; |
| 191 | u8 pln_offset; |
| 192 | u8 pln_len; |
| 193 | u8 blk_offset; |
| 194 | u8 blk_len; |
| 195 | u8 pg_offset; |
| 196 | u8 pg_len; |
| 197 | u8 sect_offset; |
| 198 | u8 sect_len; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | struct nvm_id { |
| 202 | u8 ver_id; |
| 203 | u8 vmnt; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 204 | u32 cap; |
| 205 | u32 dom; |
| 206 | struct nvm_addr_format ppaf; |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 207 | struct nvm_id_group grp; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 208 | } __packed; |
| 209 | |
| 210 | struct nvm_target { |
| 211 | struct list_head list; |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 212 | struct nvm_tgt_dev *dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 213 | struct nvm_tgt_type *type; |
| 214 | struct gendisk *disk; |
| 215 | }; |
| 216 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 217 | #define ADDR_EMPTY (~0ULL) |
| 218 | |
| 219 | #define NVM_VERSION_MAJOR 1 |
| 220 | #define NVM_VERSION_MINOR 0 |
| 221 | #define NVM_VERSION_PATCH 0 |
| 222 | |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 223 | struct nvm_rq; |
Matias Bjørling | 72d256e | 2016-01-12 07:49:29 +0100 | [diff] [blame] | 224 | typedef void (nvm_end_io_fn)(struct nvm_rq *); |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 225 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 226 | struct nvm_rq { |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 227 | struct nvm_tgt_dev *dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 228 | |
| 229 | struct bio *bio; |
| 230 | |
| 231 | union { |
| 232 | struct ppa_addr ppa_addr; |
| 233 | dma_addr_t dma_ppa_list; |
| 234 | }; |
| 235 | |
| 236 | struct ppa_addr *ppa_list; |
| 237 | |
Javier González | 003fad3 | 2016-05-06 20:03:12 +0200 | [diff] [blame] | 238 | void *meta_list; |
| 239 | dma_addr_t dma_meta_list; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 240 | |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 241 | struct completion *wait; |
| 242 | nvm_end_io_fn *end_io; |
| 243 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 244 | uint8_t opcode; |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 245 | uint16_t nr_ppas; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 246 | uint16_t flags; |
Matias Bjørling | 72d256e | 2016-01-12 07:49:29 +0100 | [diff] [blame] | 247 | |
Matias Bjorling | 9f86726 | 2016-03-03 15:06:39 +0100 | [diff] [blame] | 248 | u64 ppa_status; /* ppa media status */ |
Matias Bjørling | 72d256e | 2016-01-12 07:49:29 +0100 | [diff] [blame] | 249 | int error; |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 250 | |
| 251 | void *private; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu) |
| 255 | { |
| 256 | return pdu - sizeof(struct nvm_rq); |
| 257 | } |
| 258 | |
| 259 | static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata) |
| 260 | { |
| 261 | return rqdata + 1; |
| 262 | } |
| 263 | |
Javier González | ff0e498 | 2016-01-12 07:49:33 +0100 | [diff] [blame] | 264 | enum { |
| 265 | NVM_BLK_ST_FREE = 0x1, /* Free block */ |
Matias Bjørling | 077d238 | 2016-07-07 09:54:14 +0200 | [diff] [blame] | 266 | NVM_BLK_ST_TGT = 0x2, /* Block in use by target */ |
Javier González | ff0e498 | 2016-01-12 07:49:33 +0100 | [diff] [blame] | 267 | NVM_BLK_ST_BAD = 0x8, /* Bad block */ |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 268 | }; |
| 269 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 270 | /* Device generic information */ |
| 271 | struct nvm_geo { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 272 | int nr_chnls; |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 273 | int nr_luns; |
| 274 | int luns_per_chnl; /* -1 if channels are not symmetric */ |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 275 | int nr_planes; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 276 | int sec_per_pg; /* only sectors for a single page */ |
| 277 | int pgs_per_blk; |
| 278 | int blks_per_lun; |
Matias Bjørling | 4891d12 | 2016-05-06 20:02:57 +0200 | [diff] [blame] | 279 | int fpg_size; |
| 280 | int pfpg_size; /* size of buffer if all pages are to be read */ |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 281 | int sec_size; |
| 282 | int oob_size; |
Matias Bjørling | f9a9995 | 2016-01-12 07:49:34 +0100 | [diff] [blame] | 283 | int mccap; |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 284 | struct nvm_addr_format ppaf; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 285 | |
| 286 | /* Calculated/Cached values. These do not reflect the actual usable |
| 287 | * blocks at run-time. |
| 288 | */ |
| 289 | int max_rq_size; |
| 290 | int plane_mode; /* drive device in single, double or quad mode */ |
| 291 | |
| 292 | int sec_per_pl; /* all sectors across planes */ |
| 293 | int sec_per_blk; |
| 294 | int sec_per_lun; |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 295 | }; |
| 296 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 297 | /* sub-device structure */ |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 298 | struct nvm_tgt_dev { |
| 299 | /* Device information */ |
| 300 | struct nvm_geo geo; |
| 301 | |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 302 | /* Base ppas for target LUNs */ |
| 303 | struct ppa_addr *luns; |
| 304 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 305 | sector_t total_secs; |
| 306 | |
| 307 | struct nvm_id identity; |
| 308 | struct request_queue *q; |
| 309 | |
Javier González | 959e911 | 2016-11-28 22:39:11 +0100 | [diff] [blame] | 310 | struct nvm_dev *parent; |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 311 | void *map; |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | struct nvm_dev { |
| 315 | struct nvm_dev_ops *ops; |
| 316 | |
| 317 | struct list_head devices; |
| 318 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 319 | /* Device information */ |
| 320 | struct nvm_geo geo; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 321 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 322 | /* lower page table */ |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 323 | int lps_per_blk; |
| 324 | int *lptbl; |
| 325 | |
Matias Bjørling | 4ece44a | 2016-02-20 08:52:41 +0100 | [diff] [blame] | 326 | unsigned long total_secs; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 327 | |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 328 | unsigned long *lun_map; |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 329 | void *dma_pool; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 330 | |
| 331 | struct nvm_id identity; |
| 332 | |
| 333 | /* Backend device */ |
| 334 | struct request_queue *q; |
| 335 | char name[DISK_NAME_LEN]; |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 336 | void *private_data; |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 337 | |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 338 | void *rmap; |
| 339 | |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 340 | struct mutex mlock; |
Wenwei Tao | 4c9dacb | 2016-03-03 15:06:37 +0100 | [diff] [blame] | 341 | spinlock_t lock; |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 342 | |
| 343 | /* target management */ |
| 344 | struct list_head area_list; |
| 345 | struct list_head targets; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 346 | }; |
| 347 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 348 | static inline struct ppa_addr linear_to_generic_addr(struct nvm_geo *geo, |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 349 | u64 pba) |
Javier González | 0e5c324 | 2016-11-28 22:39:01 +0100 | [diff] [blame] | 350 | { |
| 351 | struct ppa_addr l; |
| 352 | int secs, pgs, blks, luns; |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 353 | sector_t ppa = pba; |
Javier González | 0e5c324 | 2016-11-28 22:39:01 +0100 | [diff] [blame] | 354 | |
| 355 | l.ppa = 0; |
| 356 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 357 | div_u64_rem(ppa, geo->sec_per_pg, &secs); |
Javier González | 0e5c324 | 2016-11-28 22:39:01 +0100 | [diff] [blame] | 358 | l.g.sec = secs; |
| 359 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 360 | sector_div(ppa, geo->sec_per_pg); |
| 361 | div_u64_rem(ppa, geo->pgs_per_blk, &pgs); |
Javier González | 0e5c324 | 2016-11-28 22:39:01 +0100 | [diff] [blame] | 362 | l.g.pg = pgs; |
| 363 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 364 | sector_div(ppa, geo->pgs_per_blk); |
| 365 | div_u64_rem(ppa, geo->blks_per_lun, &blks); |
Javier González | 0e5c324 | 2016-11-28 22:39:01 +0100 | [diff] [blame] | 366 | l.g.blk = blks; |
| 367 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 368 | sector_div(ppa, geo->blks_per_lun); |
| 369 | div_u64_rem(ppa, geo->luns_per_chnl, &luns); |
Javier González | 0e5c324 | 2016-11-28 22:39:01 +0100 | [diff] [blame] | 370 | l.g.lun = luns; |
| 371 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 372 | sector_div(ppa, geo->luns_per_chnl); |
Javier González | 0e5c324 | 2016-11-28 22:39:01 +0100 | [diff] [blame] | 373 | l.g.ch = ppa; |
| 374 | |
| 375 | return l; |
| 376 | } |
| 377 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 378 | static inline struct ppa_addr generic_to_dev_addr(struct nvm_tgt_dev *tgt_dev, |
| 379 | struct ppa_addr r) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 380 | { |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 381 | struct nvm_geo *geo = &tgt_dev->geo; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 382 | struct ppa_addr l; |
| 383 | |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 384 | l.ppa = ((u64)r.g.blk) << geo->ppaf.blk_offset; |
| 385 | l.ppa |= ((u64)r.g.pg) << geo->ppaf.pg_offset; |
| 386 | l.ppa |= ((u64)r.g.sec) << geo->ppaf.sect_offset; |
| 387 | l.ppa |= ((u64)r.g.pl) << geo->ppaf.pln_offset; |
| 388 | l.ppa |= ((u64)r.g.lun) << geo->ppaf.lun_offset; |
| 389 | l.ppa |= ((u64)r.g.ch) << geo->ppaf.ch_offset; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 390 | |
| 391 | return l; |
| 392 | } |
| 393 | |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 394 | static inline struct ppa_addr dev_to_generic_addr(struct nvm_tgt_dev *tgt_dev, |
| 395 | struct ppa_addr r) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 396 | { |
Matias Bjørling | dab8ee9 | 2017-01-31 13:17:14 +0100 | [diff] [blame] | 397 | struct nvm_geo *geo = &tgt_dev->geo; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 398 | struct ppa_addr l; |
| 399 | |
Javier González | 5389a1d | 2016-07-07 09:54:09 +0200 | [diff] [blame] | 400 | l.ppa = 0; |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 401 | /* |
| 402 | * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc. |
| 403 | */ |
Javier González | 8e79b5c | 2016-11-28 22:39:06 +0100 | [diff] [blame] | 404 | l.g.blk = (r.ppa >> geo->ppaf.blk_offset) & |
| 405 | (((1 << geo->ppaf.blk_len) - 1)); |
| 406 | l.g.pg |= (r.ppa >> geo->ppaf.pg_offset) & |
| 407 | (((1 << geo->ppaf.pg_len) - 1)); |
| 408 | l.g.sec |= (r.ppa >> geo->ppaf.sect_offset) & |
| 409 | (((1 << geo->ppaf.sect_len) - 1)); |
| 410 | l.g.pl |= (r.ppa >> geo->ppaf.pln_offset) & |
| 411 | (((1 << geo->ppaf.pln_len) - 1)); |
| 412 | l.g.lun |= (r.ppa >> geo->ppaf.lun_offset) & |
| 413 | (((1 << geo->ppaf.lun_len) - 1)); |
| 414 | l.g.ch |= (r.ppa >> geo->ppaf.ch_offset) & |
| 415 | (((1 << geo->ppaf.ch_len) - 1)); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 416 | |
| 417 | return l; |
| 418 | } |
| 419 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 420 | static inline int ppa_empty(struct ppa_addr ppa_addr) |
| 421 | { |
| 422 | return (ppa_addr.ppa == ADDR_EMPTY); |
| 423 | } |
| 424 | |
| 425 | static inline void ppa_set_empty(struct ppa_addr *ppa_addr) |
| 426 | { |
| 427 | ppa_addr->ppa = ADDR_EMPTY; |
| 428 | } |
| 429 | |
Javier González | a24ba46 | 2016-11-28 22:38:56 +0100 | [diff] [blame] | 430 | static inline int ppa_cmp_blk(struct ppa_addr ppa1, struct ppa_addr ppa2) |
| 431 | { |
| 432 | if (ppa_empty(ppa1) || ppa_empty(ppa2)) |
| 433 | return 0; |
| 434 | |
| 435 | return ((ppa1.g.ch == ppa2.g.ch) && (ppa1.g.lun == ppa2.g.lun) && |
| 436 | (ppa1.g.blk == ppa2.g.blk)); |
| 437 | } |
| 438 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 439 | typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 440 | typedef sector_t (nvm_tgt_capacity_fn)(void *); |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 441 | typedef void *(nvm_tgt_init_fn)(struct nvm_tgt_dev *, struct gendisk *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 442 | typedef void (nvm_tgt_exit_fn)(void *); |
Javier González | 9a69b0e | 2017-01-31 13:17:20 +0100 | [diff] [blame] | 443 | typedef int (nvm_tgt_sysfs_init_fn)(struct gendisk *); |
| 444 | typedef void (nvm_tgt_sysfs_exit_fn)(struct gendisk *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 445 | |
| 446 | struct nvm_tgt_type { |
| 447 | const char *name; |
| 448 | unsigned int version[3]; |
| 449 | |
| 450 | /* target entry points */ |
| 451 | nvm_tgt_make_rq_fn *make_rq; |
| 452 | nvm_tgt_capacity_fn *capacity; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 453 | |
| 454 | /* module-specific init/teardown */ |
| 455 | nvm_tgt_init_fn *init; |
| 456 | nvm_tgt_exit_fn *exit; |
| 457 | |
Javier González | 9a69b0e | 2017-01-31 13:17:20 +0100 | [diff] [blame] | 458 | /* sysfs */ |
| 459 | nvm_tgt_sysfs_init_fn *sysfs_init; |
| 460 | nvm_tgt_sysfs_exit_fn *sysfs_exit; |
| 461 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 462 | /* For internal use */ |
| 463 | struct list_head list; |
| 464 | }; |
| 465 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 466 | extern struct nvm_tgt_type *nvm_find_target_type(const char *, int); |
| 467 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 468 | extern int nvm_register_tgt_type(struct nvm_tgt_type *); |
| 469 | extern void nvm_unregister_tgt_type(struct nvm_tgt_type *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 470 | |
| 471 | extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *); |
| 472 | extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t); |
| 473 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 474 | extern struct nvm_dev *nvm_alloc_dev(int); |
| 475 | extern int nvm_register(struct nvm_dev *); |
| 476 | extern void nvm_unregister(struct nvm_dev *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 477 | |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 478 | extern int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr *, |
| 479 | int, int); |
Javier González | a279006 | 2016-11-28 22:39:12 +0100 | [diff] [blame] | 480 | extern int nvm_max_phys_sects(struct nvm_tgt_dev *); |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 481 | extern int nvm_submit_io(struct nvm_tgt_dev *, struct nvm_rq *); |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 482 | extern int nvm_set_rqd_ppalist(struct nvm_dev *, struct nvm_rq *, |
Matias Bjørling | 8680f16 | 2016-07-07 09:54:22 +0200 | [diff] [blame] | 483 | const struct ppa_addr *, int, int); |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 484 | extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *); |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 485 | extern int nvm_erase_blk(struct nvm_tgt_dev *, struct ppa_addr *, int); |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 486 | extern int nvm_get_l2p_tbl(struct nvm_tgt_dev *, u64, u32, nvm_l2p_update_fn *, |
Javier González | 8e53624 | 2016-11-28 22:39:10 +0100 | [diff] [blame] | 487 | void *); |
Javier González | da2d7cb | 2016-11-28 22:39:13 +0100 | [diff] [blame] | 488 | extern int nvm_get_area(struct nvm_tgt_dev *, sector_t *, sector_t); |
| 489 | extern void nvm_put_area(struct nvm_tgt_dev *, sector_t); |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 490 | extern void nvm_end_io(struct nvm_rq *); |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 491 | extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int); |
Javier González | 333ba05 | 2016-11-28 22:39:14 +0100 | [diff] [blame] | 492 | extern int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr, u8 *); |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 493 | |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 494 | extern int nvm_dev_factory(struct nvm_dev *, int flags); |
Matias Bjørling | 5136061 | 2016-05-06 20:03:04 +0200 | [diff] [blame] | 495 | |
Matias Bjørling | ade69e2 | 2017-01-31 13:17:09 +0100 | [diff] [blame] | 496 | extern void nvm_part_to_tgt(struct nvm_dev *, sector_t *, int); |
Matias Bjørling | 5136061 | 2016-05-06 20:03:04 +0200 | [diff] [blame] | 497 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 498 | #else /* CONFIG_NVM */ |
| 499 | struct nvm_dev_ops; |
| 500 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 501 | static inline struct nvm_dev *nvm_alloc_dev(int node) |
| 502 | { |
| 503 | return ERR_PTR(-EINVAL); |
| 504 | } |
| 505 | static inline int nvm_register(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 506 | { |
| 507 | return -EINVAL; |
| 508 | } |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 509 | static inline void nvm_unregister(struct nvm_dev *dev) {} |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 510 | #endif /* CONFIG_NVM */ |
| 511 | #endif /* LIGHTNVM.H */ |