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; |
| 50 | |
| 51 | typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *); |
Jens Axboe | a7fd9a4 | 2016-01-13 13:04:11 -0700 | [diff] [blame] | 52 | typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *); |
| 53 | typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32, |
| 54 | nvm_l2p_update_fn *, void *); |
Matias Bjørling | e11903f | 2016-05-06 20:03:05 +0200 | [diff] [blame] | 55 | 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] | 56 | 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] | 57 | typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *); |
| 58 | typedef int (nvm_erase_blk_fn)(struct nvm_dev *, struct nvm_rq *); |
| 59 | typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *); |
| 60 | typedef void (nvm_destroy_dma_pool_fn)(void *); |
| 61 | typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t, |
| 62 | dma_addr_t *); |
| 63 | typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t); |
| 64 | |
| 65 | struct nvm_dev_ops { |
| 66 | nvm_id_fn *identity; |
| 67 | nvm_get_l2p_tbl_fn *get_l2p_tbl; |
| 68 | nvm_op_bb_tbl_fn *get_bb_tbl; |
| 69 | nvm_op_set_bb_fn *set_bb_tbl; |
| 70 | |
| 71 | nvm_submit_io_fn *submit_io; |
| 72 | nvm_erase_blk_fn *erase_block; |
| 73 | |
| 74 | nvm_create_dma_pool_fn *create_dma_pool; |
| 75 | nvm_destroy_dma_pool_fn *destroy_dma_pool; |
| 76 | nvm_dev_dma_alloc_fn *dev_dma_alloc; |
| 77 | nvm_dev_dma_free_fn *dev_dma_free; |
| 78 | |
| 79 | unsigned int max_phys_sect; |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 84 | #ifdef CONFIG_NVM |
| 85 | |
| 86 | #include <linux/blkdev.h> |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 87 | #include <linux/file.h> |
| 88 | #include <linux/dmapool.h> |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 89 | #include <uapi/linux/lightnvm.h> |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 90 | |
| 91 | enum { |
| 92 | /* HW Responsibilities */ |
| 93 | NVM_RSP_L2P = 1 << 0, |
| 94 | NVM_RSP_ECC = 1 << 1, |
| 95 | |
| 96 | /* Physical Adressing Mode */ |
| 97 | NVM_ADDRMODE_LINEAR = 0, |
| 98 | NVM_ADDRMODE_CHANNEL = 1, |
| 99 | |
| 100 | /* Plane programming mode for LUN */ |
Matias Bjørling | d5bdec8 | 2016-02-19 13:56:58 +0100 | [diff] [blame] | 101 | NVM_PLANE_SINGLE = 1, |
| 102 | NVM_PLANE_DOUBLE = 2, |
| 103 | NVM_PLANE_QUAD = 4, |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 104 | |
| 105 | /* Status codes */ |
| 106 | NVM_RSP_SUCCESS = 0x0, |
| 107 | NVM_RSP_NOT_CHANGEABLE = 0x1, |
| 108 | NVM_RSP_ERR_FAILWRITE = 0x40ff, |
| 109 | NVM_RSP_ERR_EMPTYPAGE = 0x42ff, |
| 110 | |
| 111 | /* Device opcodes */ |
| 112 | NVM_OP_HBREAD = 0x02, |
| 113 | NVM_OP_HBWRITE = 0x81, |
| 114 | NVM_OP_PWRITE = 0x91, |
| 115 | NVM_OP_PREAD = 0x92, |
| 116 | NVM_OP_ERASE = 0x90, |
| 117 | |
| 118 | /* PPA Command Flags */ |
| 119 | NVM_IO_SNGL_ACCESS = 0x0, |
| 120 | NVM_IO_DUAL_ACCESS = 0x1, |
| 121 | NVM_IO_QUAD_ACCESS = 0x2, |
| 122 | |
Matias Bjørling | 57b4bd0 | 2015-12-06 11:25:47 +0100 | [diff] [blame] | 123 | /* NAND Access Modes */ |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 124 | NVM_IO_SUSPEND = 0x80, |
| 125 | NVM_IO_SLC_MODE = 0x100, |
| 126 | NVM_IO_SCRAMBLE_DISABLE = 0x200, |
Matias Bjørling | 57b4bd0 | 2015-12-06 11:25:47 +0100 | [diff] [blame] | 127 | |
| 128 | /* Block Types */ |
| 129 | NVM_BLK_T_FREE = 0x0, |
| 130 | NVM_BLK_T_BAD = 0x1, |
Matias Bjørling | b5d4acd | 2016-01-12 07:49:32 +0100 | [diff] [blame] | 131 | NVM_BLK_T_GRWN_BAD = 0x2, |
| 132 | NVM_BLK_T_DEV = 0x4, |
| 133 | NVM_BLK_T_HOST = 0x8, |
Matias Bjørling | f9a9995 | 2016-01-12 07:49:34 +0100 | [diff] [blame] | 134 | |
| 135 | /* Memory capabilities */ |
| 136 | NVM_ID_CAP_SLC = 0x1, |
| 137 | NVM_ID_CAP_CMD_SUSPEND = 0x2, |
| 138 | NVM_ID_CAP_SCRAMBLE = 0x4, |
| 139 | NVM_ID_CAP_ENCRYPT = 0x8, |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 140 | |
| 141 | /* Memory types */ |
| 142 | NVM_ID_FMTYPE_SLC = 0, |
| 143 | NVM_ID_FMTYPE_MLC = 1, |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 144 | |
| 145 | /* Device capabilities */ |
| 146 | NVM_ID_DCAP_BBLKMGMT = 0x1, |
| 147 | NVM_UD_DCAP_ECC = 0x2, |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | struct nvm_id_lp_mlc { |
| 151 | u16 num_pairs; |
| 152 | u8 pairs[886]; |
| 153 | }; |
| 154 | |
| 155 | struct nvm_id_lp_tbl { |
| 156 | __u8 id[8]; |
| 157 | struct nvm_id_lp_mlc mlc; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | struct nvm_id_group { |
| 161 | u8 mtype; |
| 162 | u8 fmtype; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 163 | u8 num_ch; |
| 164 | u8 num_lun; |
| 165 | u8 num_pln; |
| 166 | u16 num_blk; |
| 167 | u16 num_pg; |
| 168 | u16 fpg_sz; |
| 169 | u16 csecs; |
| 170 | u16 sos; |
| 171 | u32 trdt; |
| 172 | u32 trdm; |
| 173 | u32 tprt; |
| 174 | u32 tprm; |
| 175 | u32 tbet; |
| 176 | u32 tbem; |
| 177 | u32 mpos; |
Matias Bjørling | 12be5ed | 2015-11-16 15:34:39 +0100 | [diff] [blame] | 178 | u32 mccap; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 179 | u16 cpar; |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 180 | |
| 181 | struct nvm_id_lp_tbl lptbl; |
Matias Bjørling | 73387e7 | 2015-11-16 15:34:40 +0100 | [diff] [blame] | 182 | }; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 183 | |
| 184 | struct nvm_addr_format { |
| 185 | u8 ch_offset; |
| 186 | u8 ch_len; |
| 187 | u8 lun_offset; |
| 188 | u8 lun_len; |
| 189 | u8 pln_offset; |
| 190 | u8 pln_len; |
| 191 | u8 blk_offset; |
| 192 | u8 blk_len; |
| 193 | u8 pg_offset; |
| 194 | u8 pg_len; |
| 195 | u8 sect_offset; |
| 196 | u8 sect_len; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | struct nvm_id { |
| 200 | u8 ver_id; |
| 201 | u8 vmnt; |
| 202 | u8 cgrps; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 203 | u32 cap; |
| 204 | u32 dom; |
| 205 | struct nvm_addr_format ppaf; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 206 | struct nvm_id_group groups[4]; |
| 207 | } __packed; |
| 208 | |
| 209 | struct nvm_target { |
| 210 | struct list_head list; |
Matias Bjørling | 976bdfc | 2016-05-06 20:03:17 +0200 | [diff] [blame] | 211 | struct nvm_dev *dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 212 | struct nvm_tgt_type *type; |
| 213 | struct gendisk *disk; |
| 214 | }; |
| 215 | |
| 216 | struct nvm_tgt_instance { |
| 217 | struct nvm_tgt_type *tt; |
| 218 | }; |
| 219 | |
| 220 | #define ADDR_EMPTY (~0ULL) |
| 221 | |
| 222 | #define NVM_VERSION_MAJOR 1 |
| 223 | #define NVM_VERSION_MINOR 0 |
| 224 | #define NVM_VERSION_PATCH 0 |
| 225 | |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 226 | struct nvm_rq; |
Matias Bjørling | 72d256e | 2016-01-12 07:49:29 +0100 | [diff] [blame] | 227 | typedef void (nvm_end_io_fn)(struct nvm_rq *); |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 228 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 229 | struct nvm_rq { |
| 230 | struct nvm_tgt_instance *ins; |
| 231 | struct nvm_dev *dev; |
| 232 | |
| 233 | struct bio *bio; |
| 234 | |
| 235 | union { |
| 236 | struct ppa_addr ppa_addr; |
| 237 | dma_addr_t dma_ppa_list; |
| 238 | }; |
| 239 | |
| 240 | struct ppa_addr *ppa_list; |
| 241 | |
Javier González | 003fad3 | 2016-05-06 20:03:12 +0200 | [diff] [blame] | 242 | void *meta_list; |
| 243 | dma_addr_t dma_meta_list; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 244 | |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 245 | struct completion *wait; |
| 246 | nvm_end_io_fn *end_io; |
| 247 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 248 | uint8_t opcode; |
Javier González | 6d5be95 | 2016-05-06 20:03:20 +0200 | [diff] [blame] | 249 | uint16_t nr_ppas; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 250 | uint16_t flags; |
Matias Bjørling | 72d256e | 2016-01-12 07:49:29 +0100 | [diff] [blame] | 251 | |
Matias Bjorling | 9f86726 | 2016-03-03 15:06:39 +0100 | [diff] [blame] | 252 | u64 ppa_status; /* ppa media status */ |
Matias Bjørling | 72d256e | 2016-01-12 07:49:29 +0100 | [diff] [blame] | 253 | int error; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu) |
| 257 | { |
| 258 | return pdu - sizeof(struct nvm_rq); |
| 259 | } |
| 260 | |
| 261 | static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata) |
| 262 | { |
| 263 | return rqdata + 1; |
| 264 | } |
| 265 | |
| 266 | struct nvm_block; |
| 267 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 268 | struct nvm_lun { |
| 269 | int id; |
| 270 | |
| 271 | int lun_id; |
| 272 | int chnl_id; |
| 273 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 274 | spinlock_t lock; |
Javier González | ff0e498 | 2016-01-12 07:49:33 +0100 | [diff] [blame] | 275 | |
Matias Bjørling | 077d238 | 2016-07-07 09:54:14 +0200 | [diff] [blame] | 276 | unsigned int nr_free_blocks; /* Number of unused blocks */ |
Javier González | ff0e498 | 2016-01-12 07:49:33 +0100 | [diff] [blame] | 277 | struct nvm_block *blocks; |
| 278 | }; |
| 279 | |
| 280 | enum { |
| 281 | NVM_BLK_ST_FREE = 0x1, /* Free block */ |
Matias Bjørling | 077d238 | 2016-07-07 09:54:14 +0200 | [diff] [blame] | 282 | NVM_BLK_ST_TGT = 0x2, /* Block in use by target */ |
Javier González | ff0e498 | 2016-01-12 07:49:33 +0100 | [diff] [blame] | 283 | NVM_BLK_ST_BAD = 0x8, /* Bad block */ |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | struct nvm_block { |
| 287 | struct list_head list; |
| 288 | struct nvm_lun *lun; |
| 289 | unsigned long id; |
| 290 | |
| 291 | void *priv; |
Javier González | ff0e498 | 2016-01-12 07:49:33 +0100 | [diff] [blame] | 292 | int state; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 293 | }; |
| 294 | |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 295 | /* system block cpu representation */ |
| 296 | struct nvm_sb_info { |
| 297 | unsigned long seqnr; |
| 298 | unsigned long erase_cnt; |
| 299 | unsigned int version; |
| 300 | char mmtype[NVM_MMTYPE_LEN]; |
| 301 | struct ppa_addr fs_ppa; |
| 302 | }; |
| 303 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 304 | struct nvm_dev { |
| 305 | struct nvm_dev_ops *ops; |
| 306 | |
| 307 | struct list_head devices; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 308 | |
| 309 | /* Media manager */ |
| 310 | struct nvmm_type *mt; |
| 311 | void *mp; |
| 312 | |
Matias Bjørling | b769207 | 2016-01-12 07:49:38 +0100 | [diff] [blame] | 313 | /* System blocks */ |
| 314 | struct nvm_sb_info sb; |
| 315 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 316 | /* Device information */ |
| 317 | int nr_chnls; |
| 318 | int nr_planes; |
| 319 | int luns_per_chnl; |
| 320 | int sec_per_pg; /* only sectors for a single page */ |
| 321 | int pgs_per_blk; |
| 322 | int blks_per_lun; |
Matias Bjørling | 4891d12 | 2016-05-06 20:02:57 +0200 | [diff] [blame] | 323 | int fpg_size; |
| 324 | 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] | 325 | int sec_size; |
| 326 | int oob_size; |
Matias Bjørling | f9a9995 | 2016-01-12 07:49:34 +0100 | [diff] [blame] | 327 | int mccap; |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 328 | struct nvm_addr_format ppaf; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 329 | |
| 330 | /* Calculated/Cached values. These do not reflect the actual usable |
| 331 | * blocks at run-time. |
| 332 | */ |
| 333 | int max_rq_size; |
| 334 | int plane_mode; /* drive device in single, double or quad mode */ |
| 335 | |
| 336 | int sec_per_pl; /* all sectors across planes */ |
| 337 | int sec_per_blk; |
| 338 | int sec_per_lun; |
| 339 | |
Matias Bjørling | ca5927e | 2016-01-12 07:49:35 +0100 | [diff] [blame] | 340 | /* lower page table */ |
| 341 | int lps_per_blk; |
| 342 | int *lptbl; |
| 343 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 344 | unsigned long total_blocks; |
Matias Bjørling | 4ece44a | 2016-02-20 08:52:41 +0100 | [diff] [blame] | 345 | unsigned long total_secs; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 346 | int nr_luns; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 347 | |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 348 | unsigned long *lun_map; |
Javier González | 75b8564 | 2016-05-06 20:03:13 +0200 | [diff] [blame] | 349 | void *dma_pool; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 350 | |
| 351 | struct nvm_id identity; |
| 352 | |
| 353 | /* Backend device */ |
| 354 | struct request_queue *q; |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 355 | struct device dev; |
| 356 | struct device *parent_dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 357 | char name[DISK_NAME_LEN]; |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 358 | void *private_data; |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 359 | |
| 360 | struct mutex mlock; |
Wenwei Tao | 4c9dacb | 2016-03-03 15:06:37 +0100 | [diff] [blame] | 361 | spinlock_t lock; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 362 | }; |
| 363 | |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 364 | static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev, |
| 365 | struct ppa_addr r) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 366 | { |
| 367 | struct ppa_addr l; |
| 368 | |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 369 | l.ppa = ((u64)r.g.blk) << dev->ppaf.blk_offset; |
| 370 | l.ppa |= ((u64)r.g.pg) << dev->ppaf.pg_offset; |
| 371 | l.ppa |= ((u64)r.g.sec) << dev->ppaf.sect_offset; |
| 372 | l.ppa |= ((u64)r.g.pl) << dev->ppaf.pln_offset; |
| 373 | l.ppa |= ((u64)r.g.lun) << dev->ppaf.lun_offset; |
| 374 | l.ppa |= ((u64)r.g.ch) << dev->ppaf.ch_offset; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 375 | |
| 376 | return l; |
| 377 | } |
| 378 | |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 379 | static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev, |
| 380 | struct ppa_addr r) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 381 | { |
| 382 | struct ppa_addr l; |
| 383 | |
Javier González | 5389a1d | 2016-07-07 09:54:09 +0200 | [diff] [blame] | 384 | l.ppa = 0; |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 385 | /* |
| 386 | * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc. |
| 387 | */ |
| 388 | l.g.blk = (r.ppa >> dev->ppaf.blk_offset) & |
| 389 | (((1 << dev->ppaf.blk_len) - 1)); |
| 390 | l.g.pg |= (r.ppa >> dev->ppaf.pg_offset) & |
| 391 | (((1 << dev->ppaf.pg_len) - 1)); |
| 392 | l.g.sec |= (r.ppa >> dev->ppaf.sect_offset) & |
| 393 | (((1 << dev->ppaf.sect_len) - 1)); |
| 394 | l.g.pl |= (r.ppa >> dev->ppaf.pln_offset) & |
| 395 | (((1 << dev->ppaf.pln_len) - 1)); |
| 396 | l.g.lun |= (r.ppa >> dev->ppaf.lun_offset) & |
| 397 | (((1 << dev->ppaf.lun_len) - 1)); |
| 398 | l.g.ch |= (r.ppa >> dev->ppaf.ch_offset) & |
| 399 | (((1 << dev->ppaf.ch_len) - 1)); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 400 | |
| 401 | return l; |
| 402 | } |
| 403 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 404 | static inline int ppa_empty(struct ppa_addr ppa_addr) |
| 405 | { |
| 406 | return (ppa_addr.ppa == ADDR_EMPTY); |
| 407 | } |
| 408 | |
| 409 | static inline void ppa_set_empty(struct ppa_addr *ppa_addr) |
| 410 | { |
| 411 | ppa_addr->ppa = ADDR_EMPTY; |
| 412 | } |
| 413 | |
| 414 | static inline struct ppa_addr block_to_ppa(struct nvm_dev *dev, |
| 415 | struct nvm_block *blk) |
| 416 | { |
| 417 | struct ppa_addr ppa; |
| 418 | struct nvm_lun *lun = blk->lun; |
| 419 | |
| 420 | ppa.ppa = 0; |
| 421 | ppa.g.blk = blk->id % dev->blks_per_lun; |
| 422 | ppa.g.lun = lun->lun_id; |
| 423 | ppa.g.ch = lun->chnl_id; |
| 424 | |
| 425 | return ppa; |
| 426 | } |
| 427 | |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 428 | static inline int ppa_to_slc(struct nvm_dev *dev, int slc_pg) |
| 429 | { |
| 430 | return dev->lptbl[slc_pg]; |
| 431 | } |
| 432 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 433 | 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] | 434 | typedef sector_t (nvm_tgt_capacity_fn)(void *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 435 | typedef void *(nvm_tgt_init_fn)(struct nvm_dev *, struct gendisk *, int, int); |
| 436 | typedef void (nvm_tgt_exit_fn)(void *); |
| 437 | |
| 438 | struct nvm_tgt_type { |
| 439 | const char *name; |
| 440 | unsigned int version[3]; |
| 441 | |
| 442 | /* target entry points */ |
| 443 | nvm_tgt_make_rq_fn *make_rq; |
| 444 | nvm_tgt_capacity_fn *capacity; |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 445 | nvm_end_io_fn *end_io; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 446 | |
| 447 | /* module-specific init/teardown */ |
| 448 | nvm_tgt_init_fn *init; |
| 449 | nvm_tgt_exit_fn *exit; |
| 450 | |
| 451 | /* For internal use */ |
| 452 | struct list_head list; |
| 453 | }; |
| 454 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 455 | extern struct nvm_tgt_type *nvm_find_target_type(const char *, int); |
| 456 | |
Simon A. F. Lund | 6063fe3 | 2016-05-06 20:03:02 +0200 | [diff] [blame] | 457 | extern int nvm_register_tgt_type(struct nvm_tgt_type *); |
| 458 | extern void nvm_unregister_tgt_type(struct nvm_tgt_type *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 459 | |
| 460 | extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *); |
| 461 | extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t); |
| 462 | |
| 463 | typedef int (nvmm_register_fn)(struct nvm_dev *); |
| 464 | typedef void (nvmm_unregister_fn)(struct nvm_dev *); |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 465 | |
| 466 | typedef int (nvmm_create_tgt_fn)(struct nvm_dev *, struct nvm_ioctl_create *); |
| 467 | typedef int (nvmm_remove_tgt_fn)(struct nvm_dev *, struct nvm_ioctl_remove *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 468 | typedef struct nvm_block *(nvmm_get_blk_fn)(struct nvm_dev *, |
| 469 | struct nvm_lun *, unsigned long); |
| 470 | typedef void (nvmm_put_blk_fn)(struct nvm_dev *, struct nvm_block *); |
| 471 | typedef int (nvmm_open_blk_fn)(struct nvm_dev *, struct nvm_block *); |
| 472 | typedef int (nvmm_close_blk_fn)(struct nvm_dev *, struct nvm_block *); |
| 473 | typedef void (nvmm_flush_blk_fn)(struct nvm_dev *, struct nvm_block *); |
| 474 | typedef int (nvmm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 475 | typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct nvm_block *, |
| 476 | unsigned long); |
Matias Bjørling | 04a8aa1 | 2016-05-06 20:03:18 +0200 | [diff] [blame] | 477 | typedef void (nvmm_mark_blk_fn)(struct nvm_dev *, struct ppa_addr, int); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 478 | typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int); |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 479 | typedef int (nvmm_reserve_lun)(struct nvm_dev *, int); |
| 480 | typedef void (nvmm_release_lun)(struct nvm_dev *, int); |
Javier Gonzalez | 2fde0e4 | 2015-11-20 13:47:57 +0100 | [diff] [blame] | 481 | typedef void (nvmm_lun_info_print_fn)(struct nvm_dev *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 482 | |
Wenwei Tao | 4c9dacb | 2016-03-03 15:06:37 +0100 | [diff] [blame] | 483 | typedef int (nvmm_get_area_fn)(struct nvm_dev *, sector_t *, sector_t); |
| 484 | typedef void (nvmm_put_area_fn)(struct nvm_dev *, sector_t); |
| 485 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 486 | struct nvmm_type { |
| 487 | const char *name; |
| 488 | unsigned int version[3]; |
| 489 | |
| 490 | nvmm_register_fn *register_mgr; |
| 491 | nvmm_unregister_fn *unregister_mgr; |
| 492 | |
Matias Bjørling | b76eb20b | 2016-07-07 09:54:16 +0200 | [diff] [blame] | 493 | nvmm_create_tgt_fn *create_tgt; |
| 494 | nvmm_remove_tgt_fn *remove_tgt; |
| 495 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 496 | /* Block administration callbacks */ |
| 497 | nvmm_get_blk_fn *get_blk; |
| 498 | nvmm_put_blk_fn *put_blk; |
| 499 | nvmm_open_blk_fn *open_blk; |
| 500 | nvmm_close_blk_fn *close_blk; |
| 501 | nvmm_flush_blk_fn *flush_blk; |
| 502 | |
| 503 | nvmm_submit_io_fn *submit_io; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 504 | nvmm_erase_blk_fn *erase_blk; |
| 505 | |
Matias Bjørling | 04a8aa1 | 2016-05-06 20:03:18 +0200 | [diff] [blame] | 506 | /* Bad block mgmt */ |
| 507 | nvmm_mark_blk_fn *mark_blk; |
| 508 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 509 | /* Configuration management */ |
| 510 | nvmm_get_lun_fn *get_lun; |
Wenwei Tao | da1e284 | 2016-03-03 15:06:38 +0100 | [diff] [blame] | 511 | nvmm_reserve_lun *reserve_lun; |
| 512 | nvmm_release_lun *release_lun; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 513 | |
| 514 | /* Statistics */ |
Javier Gonzalez | 2fde0e4 | 2015-11-20 13:47:57 +0100 | [diff] [blame] | 515 | nvmm_lun_info_print_fn *lun_info_print; |
Wenwei Tao | 4c9dacb | 2016-03-03 15:06:37 +0100 | [diff] [blame] | 516 | |
| 517 | nvmm_get_area_fn *get_area; |
| 518 | nvmm_put_area_fn *put_area; |
| 519 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 520 | struct list_head list; |
| 521 | }; |
| 522 | |
| 523 | extern int nvm_register_mgr(struct nvmm_type *); |
| 524 | extern void nvm_unregister_mgr(struct nvmm_type *); |
| 525 | |
| 526 | extern struct nvm_block *nvm_get_blk(struct nvm_dev *, struct nvm_lun *, |
| 527 | unsigned long); |
| 528 | extern void nvm_put_blk(struct nvm_dev *, struct nvm_block *); |
| 529 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 530 | extern struct nvm_dev *nvm_alloc_dev(int); |
| 531 | extern int nvm_register(struct nvm_dev *); |
| 532 | extern void nvm_unregister(struct nvm_dev *); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 533 | |
Javier González | 529435e81 | 2016-07-07 09:54:08 +0200 | [diff] [blame] | 534 | void nvm_mark_blk(struct nvm_dev *dev, struct ppa_addr ppa, int type); |
| 535 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 536 | extern int nvm_submit_io(struct nvm_dev *, struct nvm_rq *); |
Matias Bjørling | 069368e | 2016-01-12 07:49:19 +0100 | [diff] [blame] | 537 | extern void nvm_generic_to_addr_mode(struct nvm_dev *, struct nvm_rq *); |
| 538 | extern void nvm_addr_to_generic_mode(struct nvm_dev *, struct nvm_rq *); |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 539 | 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] | 540 | const struct ppa_addr *, int, int); |
Matias Bjørling | abd805e | 2016-01-12 07:49:20 +0100 | [diff] [blame] | 541 | extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *); |
Matias Bjørling | 81e681d | 2016-01-12 07:49:28 +0100 | [diff] [blame] | 542 | extern int nvm_erase_ppa(struct nvm_dev *, struct ppa_addr *, int); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 543 | extern int nvm_erase_blk(struct nvm_dev *, struct nvm_block *); |
Matias Bjørling | 91276162 | 2016-01-12 07:49:21 +0100 | [diff] [blame] | 544 | extern void nvm_end_io(struct nvm_rq *, int); |
Matias Bjørling | 09719b6 | 2016-01-12 07:49:30 +0100 | [diff] [blame] | 545 | extern int nvm_submit_ppa(struct nvm_dev *, struct ppa_addr *, int, int, int, |
| 546 | void *, int); |
Matias Bjørling | 1145e63 | 2016-05-06 20:02:56 +0200 | [diff] [blame] | 547 | extern int nvm_submit_ppa_list(struct nvm_dev *, struct ppa_addr *, int, int, |
| 548 | int, void *, int); |
Matias Bjørling | 22e8c97 | 2016-05-06 20:02:58 +0200 | [diff] [blame] | 549 | extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int); |
Matias Bjørling | e11903f | 2016-05-06 20:03:05 +0200 | [diff] [blame] | 550 | extern int nvm_get_bb_tbl(struct nvm_dev *, struct ppa_addr, u8 *); |
Matias Bjørling | e3eb379 | 2016-01-12 07:49:36 +0100 | [diff] [blame] | 551 | |
| 552 | /* sysblk.c */ |
| 553 | #define NVM_SYSBLK_MAGIC 0x4E564D53 /* "NVMS" */ |
| 554 | |
| 555 | /* system block on disk representation */ |
| 556 | struct nvm_system_block { |
| 557 | __be32 magic; /* magic signature */ |
| 558 | __be32 seqnr; /* sequence number */ |
| 559 | __be32 erase_cnt; /* erase count */ |
| 560 | __be16 version; /* version number */ |
| 561 | u8 mmtype[NVM_MMTYPE_LEN]; /* media manager name */ |
| 562 | __be64 fs_ppa; /* PPA for media manager |
| 563 | * superblock */ |
| 564 | }; |
| 565 | |
| 566 | extern int nvm_get_sysblock(struct nvm_dev *, struct nvm_sb_info *); |
| 567 | extern int nvm_update_sysblock(struct nvm_dev *, struct nvm_sb_info *); |
| 568 | extern int nvm_init_sysblock(struct nvm_dev *, struct nvm_sb_info *); |
Matias Bjørling | 8b4970c | 2016-01-12 07:49:39 +0100 | [diff] [blame] | 569 | |
| 570 | extern int nvm_dev_factory(struct nvm_dev *, int flags); |
Matias Bjørling | 5136061 | 2016-05-06 20:03:04 +0200 | [diff] [blame] | 571 | |
| 572 | #define nvm_for_each_lun_ppa(dev, ppa, chid, lunid) \ |
| 573 | for ((chid) = 0, (ppa).ppa = 0; (chid) < (dev)->nr_chnls; \ |
| 574 | (chid)++, (ppa).g.ch = (chid)) \ |
| 575 | for ((lunid) = 0; (lunid) < (dev)->luns_per_chnl; \ |
| 576 | (lunid)++, (ppa).g.lun = (lunid)) |
| 577 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 578 | #else /* CONFIG_NVM */ |
| 579 | struct nvm_dev_ops; |
| 580 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 581 | static inline struct nvm_dev *nvm_alloc_dev(int node) |
| 582 | { |
| 583 | return ERR_PTR(-EINVAL); |
| 584 | } |
| 585 | static inline int nvm_register(struct nvm_dev *dev) |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 586 | { |
| 587 | return -EINVAL; |
| 588 | } |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 589 | static inline void nvm_unregister(struct nvm_dev *dev) {} |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 590 | #endif /* CONFIG_NVM */ |
| 591 | #endif /* LIGHTNVM.H */ |