pblk.h 36 KB

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