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