pblk.h 39 KB

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