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