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