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