pblk.h 41 KB

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