pblk.h 39 KB

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