pblk.h 36 KB

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