pblk.h 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /*
  2. * Copyright (C) 2015 IT University of Copenhagen (rrpc.h)
  3. * Copyright (C) 2016 CNEX Labs
  4. * Initial release: Matias Bjorling <matias@cnexlabs.com>
  5. * Write buffering: Javier Gonzalez <javier@cnexlabs.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * Implementation of a Physical Block-device target for Open-channel SSDs.
  17. *
  18. */
  19. #ifndef PBLK_H_
  20. #define PBLK_H_
  21. #include <linux/blkdev.h>
  22. #include <linux/blk-mq.h>
  23. #include <linux/bio.h>
  24. #include <linux/module.h>
  25. #include <linux/kthread.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/crc32.h>
  28. #include <linux/uuid.h>
  29. #include <linux/lightnvm.h>
  30. /* Run only GC if less than 1/X blocks are free */
  31. #define GC_LIMIT_INVERSE 5
  32. #define GC_TIME_MSECS 1000
  33. #define PBLK_SECTOR (512)
  34. #define PBLK_EXPOSED_PAGE_SIZE (4096)
  35. #define PBLK_MAX_REQ_ADDRS (64)
  36. #define PBLK_MAX_REQ_ADDRS_PW (6)
  37. #define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
  38. #define PBLK_COMMAND_TIMEOUT_MS 30000
  39. /* Max 512 LUNs per device */
  40. #define PBLK_MAX_LUNS_BITMAP (4)
  41. #define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
  42. #define pblk_for_each_lun(pblk, rlun, i) \
  43. for ((i) = 0, rlun = &(pblk)->luns[0]; \
  44. (i) < (pblk)->nr_luns; (i)++, rlun = &(pblk)->luns[(i)])
  45. #define ERASE 2 /* READ = 0, WRITE = 1 */
  46. enum {
  47. /* IO Types */
  48. PBLK_IOTYPE_USER = 1 << 0,
  49. PBLK_IOTYPE_GC = 1 << 1,
  50. /* Write buffer flags */
  51. PBLK_FLUSH_ENTRY = 1 << 2,
  52. PBLK_WRITTEN_DATA = 1 << 3,
  53. PBLK_SUBMITTED_ENTRY = 1 << 4,
  54. PBLK_WRITABLE_ENTRY = 1 << 5,
  55. };
  56. enum {
  57. PBLK_BLK_ST_OPEN = 0x1,
  58. PBLK_BLK_ST_CLOSED = 0x2,
  59. };
  60. /* The number of GC lists and the rate-limiter states go together. This way the
  61. * rate-limiter can dictate how much GC is needed based on resource utilization.
  62. */
  63. #define PBLK_NR_GC_LISTS 3
  64. #define PBLK_MAX_GC_JOBS 32
  65. enum {
  66. PBLK_RL_HIGH = 1,
  67. PBLK_RL_MID = 2,
  68. PBLK_RL_LOW = 3,
  69. };
  70. struct pblk_sec_meta {
  71. u64 reserved;
  72. __le64 lba;
  73. };
  74. #define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS)
  75. /* write completion context */
  76. struct pblk_c_ctx {
  77. struct list_head list; /* Head for out-of-order completion */
  78. unsigned long *lun_bitmap; /* Luns used on current request */
  79. unsigned int sentry;
  80. unsigned int nr_valid;
  81. unsigned int nr_padded;
  82. };
  83. /* Read context */
  84. struct pblk_r_ctx {
  85. struct bio *orig_bio;
  86. };
  87. /* Recovery context */
  88. struct pblk_rec_ctx {
  89. struct pblk *pblk;
  90. struct nvm_rq *rqd;
  91. struct list_head failed;
  92. struct work_struct ws_rec;
  93. };
  94. /* Write context */
  95. struct pblk_w_ctx {
  96. struct bio_list bios; /* Original bios - used for completion
  97. * in REQ_FUA, REQ_FLUSH case
  98. */
  99. u64 lba; /* Logic addr. associated with entry */
  100. struct ppa_addr ppa; /* Physic addr. associated with entry */
  101. int flags; /* Write context flags */
  102. };
  103. struct pblk_rb_entry {
  104. struct ppa_addr cacheline; /* Cacheline for this entry */
  105. void *data; /* Pointer to data on this entry */
  106. struct pblk_w_ctx w_ctx; /* Context for this entry */
  107. struct list_head index; /* List head to enable indexes */
  108. };
  109. #define EMPTY_ENTRY (~0U)
  110. struct pblk_rb_pages {
  111. struct page *pages;
  112. int order;
  113. struct list_head list;
  114. };
  115. struct pblk_rb {
  116. struct pblk_rb_entry *entries; /* Ring buffer entries */
  117. unsigned int mem; /* Write offset - points to next
  118. * writable entry in memory
  119. */
  120. unsigned int subm; /* Read offset - points to last entry
  121. * that has been submitted to the media
  122. * to be persisted
  123. */
  124. unsigned int sync; /* Synced - backpointer that signals
  125. * the last submitted entry that has
  126. * been successfully persisted to media
  127. */
  128. unsigned int sync_point; /* Sync point - last entry that must be
  129. * flushed to the media. Used with
  130. * REQ_FLUSH and REQ_FUA
  131. */
  132. unsigned int l2p_update; /* l2p update point - next entry for
  133. * which l2p mapping will be updated to
  134. * contain a device ppa address (instead
  135. * of a cacheline
  136. */
  137. unsigned int nr_entries; /* Number of entries in write buffer -
  138. * must be a power of two
  139. */
  140. unsigned int seg_size; /* Size of the data segments being
  141. * stored on each entry. Typically this
  142. * will be 4KB
  143. */
  144. struct list_head pages; /* List of data pages */
  145. spinlock_t w_lock; /* Write lock */
  146. spinlock_t s_lock; /* Sync lock */
  147. #ifdef CONFIG_NVM_DEBUG
  148. atomic_t inflight_sync_point; /* Not served REQ_FLUSH | REQ_FUA */
  149. #endif
  150. };
  151. #define PBLK_RECOVERY_SECTORS 16
  152. struct pblk_lun {
  153. struct ppa_addr bppa;
  154. u8 *bb_list; /* Bad block list for LUN. Only used on
  155. * bring up. Bad blocks are managed
  156. * within lines on run-time.
  157. */
  158. struct semaphore wr_sem;
  159. };
  160. struct pblk_gc_rq {
  161. struct pblk_line *line;
  162. void *data;
  163. u64 *lba_list;
  164. int nr_secs;
  165. int secs_to_gc;
  166. struct list_head list;
  167. };
  168. struct pblk_gc {
  169. int gc_active;
  170. int gc_enabled;
  171. int gc_forced;
  172. int gc_jobs_active;
  173. atomic_t inflight_gc;
  174. struct task_struct *gc_ts;
  175. struct task_struct *gc_writer_ts;
  176. struct workqueue_struct *gc_reader_wq;
  177. struct timer_list gc_timer;
  178. int w_entries;
  179. struct list_head w_list;
  180. spinlock_t lock;
  181. spinlock_t w_lock;
  182. };
  183. struct pblk_rl {
  184. unsigned int high; /* Upper threshold for rate limiter (free run -
  185. * user I/O rate limiter
  186. */
  187. unsigned int low; /* Lower threshold for rate limiter (user I/O
  188. * rate limiter - stall)
  189. */
  190. unsigned int high_pw; /* High rounded up as a power of 2 */
  191. #define PBLK_USER_HIGH_THRS 2 /* Begin write limit at 50 percent
  192. * available blks
  193. */
  194. #define PBLK_USER_LOW_THRS 20 /* Aggressive GC at 5% available blocks */
  195. int rb_windows_pw; /* Number of rate windows in the write buffer
  196. * given as a power-of-2. This guarantees that
  197. * when user I/O is being rate limited, there
  198. * will be reserved enough space for the GC to
  199. * place its payload. A window is of
  200. * pblk->max_write_pgs size, which in NVMe is
  201. * 64, i.e., 256kb.
  202. */
  203. int rb_budget; /* Total number of entries available for I/O */
  204. int rb_user_max; /* Max buffer entries available for user I/O */
  205. atomic_t rb_user_cnt; /* User I/O buffer counter */
  206. int rb_gc_max; /* Max buffer entries available for GC I/O */
  207. int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
  208. int rb_state; /* Rate-limiter current state */
  209. atomic_t rb_gc_cnt; /* GC I/O buffer counter */
  210. int rb_user_active;
  211. struct timer_list u_timer;
  212. unsigned long long nr_secs;
  213. unsigned long total_blocks;
  214. atomic_t free_blocks;
  215. };
  216. #define PBLK_LINE_NR_LUN_BITMAP 2
  217. #define PBLK_LINE_NR_SEC_BITMAP 2
  218. #define PBLK_LINE_EMPTY (~0U)
  219. enum {
  220. /* Line Types */
  221. PBLK_LINETYPE_FREE = 0,
  222. PBLK_LINETYPE_LOG = 1,
  223. PBLK_LINETYPE_DATA = 2,
  224. /* Line state */
  225. PBLK_LINESTATE_FREE = 10,
  226. PBLK_LINESTATE_OPEN = 11,
  227. PBLK_LINESTATE_CLOSED = 12,
  228. PBLK_LINESTATE_GC = 13,
  229. PBLK_LINESTATE_BAD = 14,
  230. PBLK_LINESTATE_CORRUPT = 15,
  231. /* GC group */
  232. PBLK_LINEGC_NONE = 20,
  233. PBLK_LINEGC_EMPTY = 21,
  234. PBLK_LINEGC_LOW = 22,
  235. PBLK_LINEGC_MID = 23,
  236. PBLK_LINEGC_HIGH = 24,
  237. PBLK_LINEGC_FULL = 25,
  238. };
  239. #define PBLK_MAGIC 0x70626c6b /*pblk*/
  240. struct line_header {
  241. __le32 crc;
  242. __le32 identifier; /* pblk identifier */
  243. __u8 uuid[16]; /* instance uuid */
  244. __le16 type; /* line type */
  245. __le16 version; /* type version */
  246. __le32 id; /* line id for current line */
  247. };
  248. struct line_smeta {
  249. struct line_header header;
  250. __le32 crc; /* Full structure including struct crc */
  251. /* Previous line metadata */
  252. __le32 prev_id; /* Line id for previous line */
  253. /* Current line metadata */
  254. __le64 seq_nr; /* Sequence number for current line */
  255. /* Active writers */
  256. __le32 window_wr_lun; /* Number of parallel LUNs to write */
  257. __le32 rsvd[2];
  258. };
  259. /*
  260. * Metadata Layout:
  261. * 1. struct pblk_emeta
  262. * 2. nr_lbas u64 forming lba list
  263. * 3. nr_lines (all) u32 valid sector count (vsc) (~0U: non-alloc line)
  264. * 4. nr_luns bits (u64 format) forming line bad block bitmap
  265. *
  266. * 3. and 4. will be part of FTL log
  267. */
  268. struct line_emeta {
  269. struct line_header header;
  270. __le32 crc; /* Full structure including struct crc */
  271. /* Previous line metadata */
  272. __le32 prev_id; /* Line id for prev line */
  273. /* Current line metadata */
  274. __le64 seq_nr; /* Sequence number for current line */
  275. /* Active writers */
  276. __le32 window_wr_lun; /* Number of parallel LUNs to write */
  277. /* Bookkeeping for recovery */
  278. __le32 next_id; /* Line id for next line */
  279. __le64 nr_lbas; /* Number of lbas mapped in line */
  280. __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
  281. };
  282. struct pblk_line {
  283. struct pblk *pblk;
  284. unsigned int id; /* Line number corresponds to the
  285. * block line
  286. */
  287. unsigned int seq_nr; /* Unique line sequence number */
  288. int state; /* PBLK_LINESTATE_X */
  289. int type; /* PBLK_LINETYPE_X */
  290. int gc_group; /* PBLK_LINEGC_X */
  291. struct list_head list; /* Free, GC lists */
  292. unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
  293. struct line_smeta *smeta; /* Start metadata */
  294. struct line_emeta *emeta; /* End metadata */
  295. int meta_line; /* Metadata line id */
  296. u64 smeta_ssec; /* Sector where smeta starts */
  297. u64 emeta_ssec; /* Sector where emeta starts */
  298. unsigned int sec_in_line; /* Number of usable secs in line */
  299. atomic_t blk_in_line; /* Number of good blocks in line */
  300. unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
  301. unsigned long *erase_bitmap; /* Bitmap for erased blocks */
  302. unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
  303. unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
  304. atomic_t left_eblks; /* Blocks left for erasing */
  305. atomic_t left_seblks; /* Blocks left for sync erasing */
  306. int left_msecs; /* Sectors left for mapping */
  307. int left_ssecs; /* Sectors left to sync */
  308. unsigned int cur_sec; /* Sector map pointer */
  309. unsigned int vsc; /* Valid sector count in line */
  310. struct kref ref; /* Write buffer L2P references */
  311. spinlock_t lock; /* Necessary for invalid_bitmap only */
  312. };
  313. #define PBLK_DATA_LINES 4
  314. enum{
  315. PBLK_KMALLOC_META = 1,
  316. PBLK_VMALLOC_META = 2,
  317. };
  318. struct pblk_line_metadata {
  319. void *meta;
  320. };
  321. struct pblk_line_mgmt {
  322. int nr_lines; /* Total number of full lines */
  323. int nr_free_lines; /* Number of full lines in free list */
  324. /* Free lists - use free_lock */
  325. struct list_head free_list; /* Full lines ready to use */
  326. struct list_head corrupt_list; /* Full lines corrupted */
  327. struct list_head bad_list; /* Full lines bad */
  328. /* GC lists - use gc_lock */
  329. struct list_head *gc_lists[PBLK_NR_GC_LISTS];
  330. struct list_head gc_high_list; /* Full lines ready to GC, high isc */
  331. struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
  332. struct list_head gc_low_list; /* Full lines ready to GC, low isc */
  333. struct list_head gc_full_list; /* Full lines ready to GC, no valid */
  334. struct list_head gc_empty_list; /* Full lines close, all valid */
  335. struct pblk_line *log_line; /* Current FTL log line */
  336. struct pblk_line *data_line; /* Current data line */
  337. struct pblk_line *log_next; /* Next FTL log line */
  338. struct pblk_line *data_next; /* Next data line */
  339. /* Metadata allocation type: VMALLOC | KMALLOC */
  340. int smeta_alloc_type;
  341. int emeta_alloc_type;
  342. /* Pre-allocated metadata for data lines */
  343. struct pblk_line_metadata sline_meta[PBLK_DATA_LINES];
  344. struct pblk_line_metadata eline_meta[PBLK_DATA_LINES];
  345. unsigned long meta_bitmap;
  346. /* Helpers for fast bitmap calculations */
  347. unsigned long *bb_template;
  348. unsigned long *bb_aux;
  349. unsigned long d_seq_nr; /* Data line unique sequence number */
  350. unsigned long l_seq_nr; /* Log line unique sequence number */
  351. spinlock_t free_lock;
  352. spinlock_t gc_lock;
  353. };
  354. struct pblk_line_meta {
  355. unsigned int smeta_len; /* Total length for smeta */
  356. unsigned int smeta_sec; /* Sectors needed for smeta*/
  357. unsigned int emeta_len; /* Total length for emeta */
  358. unsigned int emeta_sec; /* Sectors needed for emeta*/
  359. unsigned int emeta_bb; /* Boundary for bb that affects emeta */
  360. unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
  361. unsigned int blk_bitmap_len; /* Length for block bitmap in line */
  362. unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
  363. unsigned int blk_per_line; /* Number of blocks in a full line */
  364. unsigned int sec_per_line; /* Number of sectors in a line */
  365. unsigned int min_blk_line; /* Min. number of good blocks in line */
  366. unsigned int mid_thrs; /* Threshold for GC mid list */
  367. unsigned int high_thrs; /* Threshold for GC high list */
  368. };
  369. struct pblk_addr_format {
  370. u64 ch_mask;
  371. u64 lun_mask;
  372. u64 pln_mask;
  373. u64 blk_mask;
  374. u64 pg_mask;
  375. u64 sec_mask;
  376. u8 ch_offset;
  377. u8 lun_offset;
  378. u8 pln_offset;
  379. u8 blk_offset;
  380. u8 pg_offset;
  381. u8 sec_offset;
  382. };
  383. struct pblk {
  384. struct nvm_tgt_dev *dev;
  385. struct gendisk *disk;
  386. struct kobject kobj;
  387. struct pblk_lun *luns;
  388. struct pblk_line *lines; /* Line array */
  389. struct pblk_line_mgmt l_mg; /* Line management */
  390. struct pblk_line_meta lm; /* Line metadata */
  391. int ppaf_bitsize;
  392. struct pblk_addr_format ppaf;
  393. struct pblk_rb rwb;
  394. int min_write_pgs; /* Minimum amount of pages required by controller */
  395. int max_write_pgs; /* Maximum amount of pages supported by controller */
  396. int pgs_in_buffer; /* Number of pages that need to be held in buffer to
  397. * guarantee successful reads.
  398. */
  399. sector_t capacity; /* Device capacity when bad blocks are subtracted */
  400. int over_pct; /* Percentage of device used for over-provisioning */
  401. /* pblk provisioning values. Used by rate limiter */
  402. struct pblk_rl rl;
  403. struct semaphore erase_sem;
  404. unsigned char instance_uuid[16];
  405. #ifdef CONFIG_NVM_DEBUG
  406. /* All debug counters apply to 4kb sector I/Os */
  407. atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
  408. atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
  409. atomic_long_t padded_wb; /* Sectors padded in write buffer */
  410. atomic_long_t nr_flush; /* Number of flush/fua I/O */
  411. atomic_long_t req_writes; /* Sectors stored on write buffer */
  412. atomic_long_t sub_writes; /* Sectors submitted from buffer */
  413. atomic_long_t sync_writes; /* Sectors synced to media */
  414. atomic_long_t compl_writes; /* Sectors completed in write bio */
  415. atomic_long_t inflight_reads; /* Inflight sector read requests */
  416. atomic_long_t sync_reads; /* Completed sector read requests */
  417. atomic_long_t recov_writes; /* Sectors submitted from recovery */
  418. atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
  419. atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
  420. #endif
  421. spinlock_t lock;
  422. atomic_long_t read_failed;
  423. atomic_long_t read_empty;
  424. atomic_long_t read_high_ecc;
  425. atomic_long_t read_failed_gc;
  426. atomic_long_t write_failed;
  427. atomic_long_t erase_failed;
  428. struct task_struct *writer_ts;
  429. /* Simple translation map of logical addresses to physical addresses.
  430. * The logical addresses is known by the host system, while the physical
  431. * addresses are used when writing to the disk block device.
  432. */
  433. unsigned char *trans_map;
  434. spinlock_t trans_lock;
  435. struct list_head compl_list;
  436. mempool_t *page_pool;
  437. mempool_t *line_ws_pool;
  438. mempool_t *rec_pool;
  439. mempool_t *r_rq_pool;
  440. mempool_t *w_rq_pool;
  441. mempool_t *line_meta_pool;
  442. struct workqueue_struct *kw_wq;
  443. struct timer_list wtimer;
  444. struct pblk_gc gc;
  445. };
  446. struct pblk_line_ws {
  447. struct pblk *pblk;
  448. struct pblk_line *line;
  449. void *priv;
  450. struct work_struct ws;
  451. };
  452. #define pblk_r_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_r_ctx))
  453. #define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
  454. /*
  455. * pblk ring buffer operations
  456. */
  457. int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
  458. unsigned int power_size, unsigned int power_seg_sz);
  459. unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
  460. void *pblk_rb_entries_ref(struct pblk_rb *rb);
  461. int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
  462. unsigned int nr_entries, unsigned int *pos);
  463. int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
  464. unsigned int *pos);
  465. void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
  466. struct pblk_w_ctx w_ctx, unsigned int pos);
  467. void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
  468. struct pblk_w_ctx w_ctx, struct pblk_line *gc_line,
  469. unsigned int pos);
  470. struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
  471. void pblk_rb_sync_l2p(struct pblk_rb *rb);
  472. unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct bio *bio,
  473. struct pblk_c_ctx *c_ctx,
  474. unsigned int pos,
  475. unsigned int nr_entries,
  476. unsigned int count);
  477. unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
  478. struct list_head *list,
  479. unsigned int max);
  480. int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
  481. u64 pos, int bio_iter);
  482. unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
  483. unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
  484. unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
  485. struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
  486. struct ppa_addr *ppa);
  487. void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
  488. unsigned int pblk_rb_sync_point_count(struct pblk_rb *rb);
  489. unsigned int pblk_rb_read_count(struct pblk_rb *rb);
  490. unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
  491. int pblk_rb_tear_down_check(struct pblk_rb *rb);
  492. int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
  493. void pblk_rb_data_free(struct pblk_rb *rb);
  494. ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
  495. /*
  496. * pblk core
  497. */
  498. struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int rw);
  499. int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
  500. struct pblk_c_ctx *c_ctx);
  501. void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int rw);
  502. void pblk_flush_writer(struct pblk *pblk);
  503. struct ppa_addr pblk_get_lba_map(struct pblk *pblk, sector_t lba);
  504. void pblk_discard(struct pblk *pblk, struct bio *bio);
  505. void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
  506. void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
  507. int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
  508. struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
  509. unsigned int nr_secs, unsigned int len,
  510. gfp_t gfp_mask);
  511. struct pblk_line *pblk_line_get(struct pblk *pblk);
  512. struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
  513. struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
  514. int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
  515. void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
  516. struct pblk_line *pblk_line_get_data(struct pblk *pblk);
  517. struct pblk_line *pblk_line_get_data_next(struct pblk *pblk);
  518. int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
  519. int pblk_line_is_full(struct pblk_line *line);
  520. void pblk_line_free(struct pblk *pblk, struct pblk_line *line);
  521. void pblk_line_close_ws(struct work_struct *work);
  522. void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
  523. void pblk_line_mark_bb(struct work_struct *work);
  524. void pblk_line_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
  525. void (*work)(struct work_struct *));
  526. u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
  527. int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
  528. int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line);
  529. int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
  530. void pblk_line_put(struct kref *ref);
  531. struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
  532. u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
  533. int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
  534. unsigned long secs_to_flush);
  535. void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
  536. unsigned long *lun_bitmap);
  537. void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
  538. unsigned long *lun_bitmap);
  539. void pblk_end_bio_sync(struct bio *bio);
  540. void pblk_end_io_sync(struct nvm_rq *rqd);
  541. int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
  542. int nr_pages);
  543. void pblk_map_pad_invalidate(struct pblk *pblk, struct pblk_line *line,
  544. u64 paddr);
  545. void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
  546. int nr_pages);
  547. void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
  548. void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
  549. void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
  550. struct ppa_addr ppa);
  551. void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
  552. struct ppa_addr ppa, struct ppa_addr entry_line);
  553. int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
  554. struct pblk_line *gc_line);
  555. void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
  556. u64 *lba_list, int nr_secs);
  557. void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
  558. sector_t blba, int nr_secs);
  559. /*
  560. * pblk user I/O write path
  561. */
  562. int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
  563. unsigned long flags);
  564. int pblk_write_gc_to_cache(struct pblk *pblk, void *data, u64 *lba_list,
  565. unsigned int nr_entries, unsigned int nr_rec_entries,
  566. struct pblk_line *gc_line, unsigned long flags);
  567. /*
  568. * pblk map
  569. */
  570. void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
  571. unsigned int sentry, unsigned long *lun_bitmap,
  572. unsigned int valid_secs, struct ppa_addr *erase_ppa);
  573. void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
  574. unsigned long *lun_bitmap, unsigned int valid_secs,
  575. unsigned int off);
  576. /*
  577. * pblk write thread
  578. */
  579. int pblk_write_ts(void *data);
  580. void pblk_write_timer_fn(unsigned long data);
  581. void pblk_write_should_kick(struct pblk *pblk);
  582. /*
  583. * pblk read path
  584. */
  585. int pblk_submit_read(struct pblk *pblk, struct bio *bio);
  586. int pblk_submit_read_gc(struct pblk *pblk, u64 *lba_list, void *data,
  587. unsigned int nr_secs, unsigned int *secs_to_gc,
  588. struct pblk_line *line);
  589. /*
  590. * pblk recovery
  591. */
  592. void pblk_submit_rec(struct work_struct *work);
  593. struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
  594. void pblk_recov_pad(struct pblk *pblk);
  595. __le64 *pblk_recov_get_lba_list(struct pblk *pblk, struct line_emeta *emeta);
  596. int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
  597. struct pblk_rec_ctx *recovery, u64 *comp_bits,
  598. unsigned int comp);
  599. /*
  600. * pblk gc
  601. */
  602. #define PBLK_GC_TRIES 3
  603. int pblk_gc_init(struct pblk *pblk);
  604. void pblk_gc_exit(struct pblk *pblk);
  605. void pblk_gc_should_start(struct pblk *pblk);
  606. void pblk_gc_should_stop(struct pblk *pblk);
  607. int pblk_gc_status(struct pblk *pblk);
  608. void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
  609. int *gc_active);
  610. void pblk_gc_sysfs_force(struct pblk *pblk, int force);
  611. /*
  612. * pblk rate limiter
  613. */
  614. void pblk_rl_init(struct pblk_rl *rl, int budget);
  615. void pblk_rl_free(struct pblk_rl *rl);
  616. int pblk_rl_gc_thrs(struct pblk_rl *rl);
  617. unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
  618. int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
  619. void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
  620. int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
  621. void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
  622. void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
  623. void pblk_rl_set_gc_rsc(struct pblk_rl *rl, int rsv);
  624. int pblk_rl_sysfs_rate_show(struct pblk_rl *rl);
  625. void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
  626. void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line);
  627. /*
  628. * pblk sysfs
  629. */
  630. int pblk_sysfs_init(struct gendisk *tdisk);
  631. void pblk_sysfs_exit(struct gendisk *tdisk);
  632. static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
  633. {
  634. if (type == PBLK_KMALLOC_META)
  635. return kmalloc(size, flags);
  636. return vmalloc(size);
  637. }
  638. static inline void pblk_mfree(void *ptr, int type)
  639. {
  640. if (type == PBLK_KMALLOC_META)
  641. kfree(ptr);
  642. else
  643. vfree(ptr);
  644. }
  645. static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
  646. {
  647. return c_ctx - sizeof(struct nvm_rq);
  648. }
  649. static inline void *pblk_line_emeta_to_lbas(struct line_emeta *emeta)
  650. {
  651. return (emeta) + 1;
  652. }
  653. #define NVM_MEM_PAGE_WRITE (8)
  654. static inline int pblk_pad_distance(struct pblk *pblk)
  655. {
  656. struct nvm_tgt_dev *dev = pblk->dev;
  657. struct nvm_geo *geo = &dev->geo;
  658. return NVM_MEM_PAGE_WRITE * geo->nr_luns * geo->sec_per_pl;
  659. }
  660. static inline int pblk_dev_ppa_to_line(struct ppa_addr p)
  661. {
  662. return p.g.blk;
  663. }
  664. static inline int pblk_tgt_ppa_to_line(struct ppa_addr p)
  665. {
  666. return p.g.blk;
  667. }
  668. static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
  669. {
  670. return p.g.lun * geo->nr_chnls + p.g.ch;
  671. }
  672. /* A block within a line corresponds to the lun */
  673. static inline int pblk_dev_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
  674. {
  675. return p.g.lun * geo->nr_chnls + p.g.ch;
  676. }
  677. static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
  678. {
  679. struct ppa_addr ppa64;
  680. ppa64.ppa = 0;
  681. if (ppa32 == -1) {
  682. ppa64.ppa = ADDR_EMPTY;
  683. } else if (ppa32 & (1U << 31)) {
  684. ppa64.c.line = ppa32 & ((~0U) >> 1);
  685. ppa64.c.is_cached = 1;
  686. } else {
  687. ppa64.g.blk = (ppa32 & pblk->ppaf.blk_mask) >>
  688. pblk->ppaf.blk_offset;
  689. ppa64.g.pg = (ppa32 & pblk->ppaf.pg_mask) >>
  690. pblk->ppaf.pg_offset;
  691. ppa64.g.lun = (ppa32 & pblk->ppaf.lun_mask) >>
  692. pblk->ppaf.lun_offset;
  693. ppa64.g.ch = (ppa32 & pblk->ppaf.ch_mask) >>
  694. pblk->ppaf.ch_offset;
  695. ppa64.g.pl = (ppa32 & pblk->ppaf.pln_mask) >>
  696. pblk->ppaf.pln_offset;
  697. ppa64.g.sec = (ppa32 & pblk->ppaf.sec_mask) >>
  698. pblk->ppaf.sec_offset;
  699. }
  700. return ppa64;
  701. }
  702. static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
  703. sector_t lba)
  704. {
  705. struct ppa_addr ppa;
  706. if (pblk->ppaf_bitsize < 32) {
  707. u32 *map = (u32 *)pblk->trans_map;
  708. ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
  709. } else {
  710. struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
  711. ppa = map[lba];
  712. }
  713. return ppa;
  714. }
  715. static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
  716. {
  717. u32 ppa32 = 0;
  718. if (ppa64.ppa == ADDR_EMPTY) {
  719. ppa32 = ~0U;
  720. } else if (ppa64.c.is_cached) {
  721. ppa32 |= ppa64.c.line;
  722. ppa32 |= 1U << 31;
  723. } else {
  724. ppa32 |= ppa64.g.blk << pblk->ppaf.blk_offset;
  725. ppa32 |= ppa64.g.pg << pblk->ppaf.pg_offset;
  726. ppa32 |= ppa64.g.lun << pblk->ppaf.lun_offset;
  727. ppa32 |= ppa64.g.ch << pblk->ppaf.ch_offset;
  728. ppa32 |= ppa64.g.pl << pblk->ppaf.pln_offset;
  729. ppa32 |= ppa64.g.sec << pblk->ppaf.sec_offset;
  730. }
  731. return ppa32;
  732. }
  733. static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
  734. struct ppa_addr ppa)
  735. {
  736. if (pblk->ppaf_bitsize < 32) {
  737. u32 *map = (u32 *)pblk->trans_map;
  738. map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
  739. } else {
  740. u64 *map = (u64 *)pblk->trans_map;
  741. map[lba] = ppa.ppa;
  742. }
  743. }
  744. static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
  745. struct ppa_addr p)
  746. {
  747. u64 paddr;
  748. paddr = 0;
  749. paddr |= (u64)p.g.pg << pblk->ppaf.pg_offset;
  750. paddr |= (u64)p.g.lun << pblk->ppaf.lun_offset;
  751. paddr |= (u64)p.g.ch << pblk->ppaf.ch_offset;
  752. paddr |= (u64)p.g.pl << pblk->ppaf.pln_offset;
  753. paddr |= (u64)p.g.sec << pblk->ppaf.sec_offset;
  754. return paddr;
  755. }
  756. static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
  757. {
  758. return (ppa_addr.ppa == ADDR_EMPTY);
  759. }
  760. static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
  761. {
  762. ppa_addr->ppa = ADDR_EMPTY;
  763. }
  764. static inline int pblk_addr_in_cache(struct ppa_addr ppa)
  765. {
  766. return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
  767. }
  768. static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
  769. {
  770. return ppa.c.line;
  771. }
  772. static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
  773. {
  774. struct ppa_addr p;
  775. p.c.line = addr;
  776. p.c.is_cached = 1;
  777. return p;
  778. }
  779. static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
  780. u64 line_id)
  781. {
  782. struct ppa_addr ppa;
  783. ppa.ppa = 0;
  784. ppa.g.blk = line_id;
  785. ppa.g.pg = (paddr & pblk->ppaf.pg_mask) >> pblk->ppaf.pg_offset;
  786. ppa.g.lun = (paddr & pblk->ppaf.lun_mask) >> pblk->ppaf.lun_offset;
  787. ppa.g.ch = (paddr & pblk->ppaf.ch_mask) >> pblk->ppaf.ch_offset;
  788. ppa.g.pl = (paddr & pblk->ppaf.pln_mask) >> pblk->ppaf.pln_offset;
  789. ppa.g.sec = (paddr & pblk->ppaf.sec_mask) >> pblk->ppaf.sec_offset;
  790. return ppa;
  791. }
  792. static inline struct ppa_addr addr_to_pblk_ppa(struct pblk *pblk, u64 paddr,
  793. u64 line_id)
  794. {
  795. struct ppa_addr ppa;
  796. ppa = addr_to_gen_ppa(pblk, paddr, line_id);
  797. return ppa;
  798. }
  799. static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
  800. struct line_smeta *smeta)
  801. {
  802. u32 crc = ~(u32)0;
  803. crc = crc32_le(crc, (unsigned char *)smeta + sizeof(crc),
  804. sizeof(struct line_header) - sizeof(crc));
  805. return crc;
  806. }
  807. static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
  808. struct line_smeta *smeta)
  809. {
  810. struct pblk_line_meta *lm = &pblk->lm;
  811. u32 crc = ~(u32)0;
  812. crc = crc32_le(crc, (unsigned char *)smeta +
  813. sizeof(struct line_header) + sizeof(crc),
  814. lm->smeta_len -
  815. sizeof(struct line_header) - sizeof(crc));
  816. return crc;
  817. }
  818. static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
  819. struct line_emeta *emeta)
  820. {
  821. struct pblk_line_meta *lm = &pblk->lm;
  822. u32 crc = ~(u32)0;
  823. crc = crc32_le(crc, (unsigned char *)emeta +
  824. sizeof(struct line_header) + sizeof(crc),
  825. lm->emeta_len -
  826. sizeof(struct line_header) - sizeof(crc));
  827. return crc;
  828. }
  829. static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
  830. {
  831. struct nvm_tgt_dev *dev = pblk->dev;
  832. struct nvm_geo *geo = &dev->geo;
  833. int flags;
  834. flags = geo->plane_mode >> 1;
  835. if (type == WRITE)
  836. flags |= NVM_IO_SCRAMBLE_ENABLE;
  837. return flags;
  838. }
  839. static inline int pblk_set_read_mode(struct pblk *pblk)
  840. {
  841. return NVM_IO_SNGL_ACCESS | NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
  842. }
  843. #ifdef CONFIG_NVM_DEBUG
  844. static inline void print_ppa(struct ppa_addr *p, char *msg, int error)
  845. {
  846. if (p->c.is_cached) {
  847. pr_err("ppa: (%s: %x) cache line: %llu\n",
  848. msg, error, (u64)p->c.line);
  849. } else {
  850. pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
  851. msg, error,
  852. p->g.ch, p->g.lun, p->g.blk,
  853. p->g.pg, p->g.pl, p->g.sec);
  854. }
  855. }
  856. static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
  857. int error)
  858. {
  859. int bit = -1;
  860. if (rqd->nr_ppas == 1) {
  861. print_ppa(&rqd->ppa_addr, "rqd", error);
  862. return;
  863. }
  864. while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
  865. bit + 1)) < rqd->nr_ppas) {
  866. print_ppa(&rqd->ppa_list[bit], "rqd", error);
  867. }
  868. pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
  869. }
  870. #endif
  871. static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
  872. struct ppa_addr *ppas, int nr_ppas)
  873. {
  874. struct nvm_geo *geo = &tgt_dev->geo;
  875. struct ppa_addr *ppa;
  876. int i;
  877. for (i = 0; i < nr_ppas; i++) {
  878. ppa = &ppas[i];
  879. if (!ppa->c.is_cached &&
  880. ppa->g.ch < geo->nr_chnls &&
  881. ppa->g.lun < geo->luns_per_chnl &&
  882. ppa->g.pl < geo->nr_planes &&
  883. ppa->g.blk < geo->blks_per_lun &&
  884. ppa->g.pg < geo->pgs_per_blk &&
  885. ppa->g.sec < geo->sec_per_pg)
  886. continue;
  887. #ifdef CONFIG_NVM_DEBUG
  888. print_ppa(ppa, "boundary", i);
  889. #endif
  890. return 1;
  891. }
  892. return 0;
  893. }
  894. static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
  895. {
  896. struct pblk_line_meta *lm = &pblk->lm;
  897. if (paddr > lm->sec_per_line)
  898. return 1;
  899. return 0;
  900. }
  901. static inline unsigned int pblk_get_bi_idx(struct bio *bio)
  902. {
  903. return bio->bi_iter.bi_idx;
  904. }
  905. static inline sector_t pblk_get_lba(struct bio *bio)
  906. {
  907. return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
  908. }
  909. static inline unsigned int pblk_get_secs(struct bio *bio)
  910. {
  911. return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
  912. }
  913. static inline sector_t pblk_get_sector(sector_t lba)
  914. {
  915. return lba * NR_PHY_IN_LOG;
  916. }
  917. static inline void pblk_setup_uuid(struct pblk *pblk)
  918. {
  919. uuid_le uuid;
  920. uuid_le_gen(&uuid);
  921. memcpy(pblk->instance_uuid, uuid.b, 16);
  922. }
  923. #endif /* PBLK_H_ */