pblk.h 40 KB

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