blob: 6ca868868feed23b559b303067c7460f6cceb6fb [file] [log] [blame]
Javier González02a15202018-10-09 13:12:06 +02001// SPDX-License-Identifier: GPL-2.0
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002/*
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Matias Bjorling <matias@cnexlabs.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * pblk-core.c - pblk's core functionality
17 *
18 */
19
Hans Holmberg4c44abf2018-10-09 13:11:52 +020020#define CREATE_TRACE_POINTS
21
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020022#include "pblk.h"
Hans Holmberg4c44abf2018-10-09 13:11:52 +020023#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020024
Javier González8bd40022017-10-13 14:46:44 +020025static void pblk_line_mark_bb(struct work_struct *work)
26{
27 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
28 ws);
29 struct pblk *pblk = line_ws->pblk;
30 struct nvm_tgt_dev *dev = pblk->dev;
31 struct ppa_addr *ppa = line_ws->priv;
32 int ret;
33
Matias Bjørlingaff3fb12018-10-09 13:11:36 +020034 ret = nvm_set_chunk_meta(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
Javier González8bd40022017-10-13 14:46:44 +020035 if (ret) {
36 struct pblk_line *line;
37 int pos;
38
Javier Gonzálezcb216652018-10-09 13:11:42 +020039 line = pblk_ppa_to_line(pblk, *ppa);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +010040 pos = pblk_ppa_to_pos(&dev->geo, *ppa);
Javier González8bd40022017-10-13 14:46:44 +020041
Matias Bjørling4e495a42018-07-13 10:48:42 +020042 pblk_err(pblk, "failed to mark bb, line:%d, pos:%d\n",
Javier González8bd40022017-10-13 14:46:44 +020043 line->id, pos);
44 }
45
46 kfree(ppa);
Kent Overstreetb906bbb2018-05-20 18:25:50 -040047 mempool_free(line_ws, &pblk->gen_ws_pool);
Javier González8bd40022017-10-13 14:46:44 +020048}
49
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020050static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line,
Javier González32ef9412018-03-30 00:05:20 +020051 struct ppa_addr ppa_addr)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020052{
53 struct nvm_tgt_dev *dev = pblk->dev;
54 struct nvm_geo *geo = &dev->geo;
Javier González32ef9412018-03-30 00:05:20 +020055 struct ppa_addr *ppa;
56 int pos = pblk_ppa_to_pos(geo, ppa_addr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020057
Matias Bjørling4e495a42018-07-13 10:48:42 +020058 pblk_debug(pblk, "erase failed: line:%d, pos:%d\n", line->id, pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059 atomic_long_inc(&pblk->erase_failed);
60
Javier Gonzáleza44f53f2017-04-22 01:32:49 +020061 atomic_dec(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020062 if (test_and_set_bit(pos, line->blk_bitmap))
Matias Bjørling4e495a42018-07-13 10:48:42 +020063 pblk_err(pblk, "attempted to erase bb: line:%d, pos:%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020064 line->id, pos);
65
Javier González32ef9412018-03-30 00:05:20 +020066 /* Not necessary to mark bad blocks on 2.0 spec. */
67 if (geo->version == NVM_OCSSD_SPEC_20)
68 return;
69
70 ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC);
71 if (!ppa)
72 return;
73
74 *ppa = ppa_addr;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020075 pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb,
76 GFP_ATOMIC, pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020077}
78
79static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd)
80{
Javier González32ef9412018-03-30 00:05:20 +020081 struct nvm_tgt_dev *dev = pblk->dev;
82 struct nvm_geo *geo = &dev->geo;
83 struct nvm_chk_meta *chunk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020084 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +020085 int pos;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020086
Javier Gonzálezcb216652018-10-09 13:11:42 +020087 line = pblk_ppa_to_line(pblk, rqd->ppa_addr);
Javier González32ef9412018-03-30 00:05:20 +020088 pos = pblk_ppa_to_pos(geo, rqd->ppa_addr);
89 chunk = &line->chks[pos];
90
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020091 atomic_dec(&line->left_seblks);
92
93 if (rqd->error) {
Hans Holmberg4209c312018-10-09 13:11:55 +020094 trace_pblk_chunk_reset(pblk_disk_name(pblk),
95 &rqd->ppa_addr, PBLK_CHUNK_RESET_FAILED);
96
Javier González32ef9412018-03-30 00:05:20 +020097 chunk->state = NVM_CHK_ST_OFFLINE;
98 pblk_mark_bb(pblk, line, rqd->ppa_addr);
99 } else {
Hans Holmberg4209c312018-10-09 13:11:55 +0200100 trace_pblk_chunk_reset(pblk_disk_name(pblk),
101 &rqd->ppa_addr, PBLK_CHUNK_RESET_DONE);
102
Javier González32ef9412018-03-30 00:05:20 +0200103 chunk->state = NVM_CHK_ST_FREE;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200104 }
Javier González588726d32017-06-26 11:57:29 +0200105
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200106 trace_pblk_chunk_state(pblk_disk_name(pblk), &rqd->ppa_addr,
107 chunk->state);
108
Javier González588726d32017-06-26 11:57:29 +0200109 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200110}
111
112/* Erase completion assumes that only one block is erased at the time */
113static void pblk_end_io_erase(struct nvm_rq *rqd)
114{
115 struct pblk *pblk = rqd->private;
116
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200117 __pblk_end_io_erase(pblk, rqd);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400118 mempool_free(rqd, &pblk->e_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200119}
120
Javier González32ef9412018-03-30 00:05:20 +0200121/*
122 * Get information for all chunks from the device.
123 *
Javier González090ee262018-10-09 13:12:01 +0200124 * The caller is responsible for freeing (vmalloc) the returned structure
Javier González32ef9412018-03-30 00:05:20 +0200125 */
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200126struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk)
Javier González32ef9412018-03-30 00:05:20 +0200127{
128 struct nvm_tgt_dev *dev = pblk->dev;
129 struct nvm_geo *geo = &dev->geo;
130 struct nvm_chk_meta *meta;
131 struct ppa_addr ppa;
132 unsigned long len;
133 int ret;
134
135 ppa.ppa = 0;
136
137 len = geo->all_chunks * sizeof(*meta);
Javier González090ee262018-10-09 13:12:01 +0200138 meta = vzalloc(len);
Javier González32ef9412018-03-30 00:05:20 +0200139 if (!meta)
140 return ERR_PTR(-ENOMEM);
141
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200142 ret = nvm_get_chunk_meta(dev, ppa, geo->all_chunks, meta);
Javier González32ef9412018-03-30 00:05:20 +0200143 if (ret) {
Hans Holmberg6916cf52019-02-11 13:25:03 +0100144 vfree(meta);
Javier González32ef9412018-03-30 00:05:20 +0200145 return ERR_PTR(-EIO);
146 }
147
148 return meta;
149}
150
151struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
152 struct nvm_chk_meta *meta,
153 struct ppa_addr ppa)
154{
155 struct nvm_tgt_dev *dev = pblk->dev;
156 struct nvm_geo *geo = &dev->geo;
157 int ch_off = ppa.m.grp * geo->num_chk * geo->num_lun;
158 int lun_off = ppa.m.pu * geo->num_chk;
159 int chk_off = ppa.m.chk;
160
161 return meta + ch_off + lun_off + chk_off;
162}
163
Javier González0880a9a2017-06-26 11:57:19 +0200164void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
165 u64 paddr)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200166{
167 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
168 struct list_head *move_list = NULL;
169
170 /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P
171 * table is modified with reclaimed sectors, a check is done to endure
172 * that newer updates are not overwritten.
173 */
174 spin_lock(&line->lock);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200175 WARN_ON(line->state == PBLK_LINESTATE_FREE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200176
177 if (test_and_set_bit(paddr, line->invalid_bitmap)) {
178 WARN_ONCE(1, "pblk: double invalidate\n");
179 spin_unlock(&line->lock);
180 return;
181 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200182 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200183
184 if (line->state == PBLK_LINESTATE_CLOSED)
185 move_list = pblk_line_gc_list(pblk, line);
186 spin_unlock(&line->lock);
187
188 if (move_list) {
189 spin_lock(&l_mg->gc_lock);
190 spin_lock(&line->lock);
191 /* Prevent moving a line that has just been chosen for GC */
Javier Gonzálezd3401212017-10-13 14:46:14 +0200192 if (line->state == PBLK_LINESTATE_GC) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200193 spin_unlock(&line->lock);
194 spin_unlock(&l_mg->gc_lock);
195 return;
196 }
197 spin_unlock(&line->lock);
198
199 list_move_tail(&line->list, move_list);
200 spin_unlock(&l_mg->gc_lock);
201 }
202}
203
204void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa)
205{
206 struct pblk_line *line;
207 u64 paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208
Matias Bjørling880eda52018-07-13 10:48:37 +0200209#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200210 /* Callers must ensure that the ppa points to a device address */
211 BUG_ON(pblk_addr_in_cache(ppa));
212 BUG_ON(pblk_ppa_empty(ppa));
213#endif
214
Javier Gonzálezcb216652018-10-09 13:11:42 +0200215 line = pblk_ppa_to_line(pblk, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200216 paddr = pblk_dev_ppa_to_line_addr(pblk, ppa);
217
218 __pblk_map_invalidate(pblk, line, paddr);
219}
220
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221static void pblk_invalidate_range(struct pblk *pblk, sector_t slba,
222 unsigned int nr_secs)
223{
224 sector_t lba;
225
226 spin_lock(&pblk->trans_lock);
227 for (lba = slba; lba < slba + nr_secs; lba++) {
228 struct ppa_addr ppa;
229
230 ppa = pblk_trans_map_get(pblk, lba);
231
232 if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa))
233 pblk_map_invalidate(pblk, ppa);
234
235 pblk_ppa_set_empty(&ppa);
236 pblk_trans_map_set(pblk, lba, ppa);
237 }
238 spin_unlock(&pblk->trans_lock);
239}
240
Javier González45dcf292018-10-09 13:12:02 +0200241int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
242{
243 struct nvm_tgt_dev *dev = pblk->dev;
244
245 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
246 &rqd->dma_meta_list);
247 if (!rqd->meta_list)
248 return -ENOMEM;
249
250 if (rqd->nr_ppas == 1)
251 return 0;
252
Igor Konopko24828d02018-12-11 20:16:24 +0100253 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size(pblk);
254 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size(pblk);
Javier González45dcf292018-10-09 13:12:02 +0200255
256 return 0;
257}
258
259void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
260{
261 struct nvm_tgt_dev *dev = pblk->dev;
262
263 if (rqd->meta_list)
264 nvm_dev_dma_free(dev->parent, rqd->meta_list,
265 rqd->dma_meta_list);
266}
267
Javier González67bf26a2017-10-13 14:46:20 +0200268/* Caller must guarantee that the request is a valid type */
269struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200270{
271 mempool_t *pool;
272 struct nvm_rq *rqd;
273 int rq_size;
274
Javier González67bf26a2017-10-13 14:46:20 +0200275 switch (type) {
276 case PBLK_WRITE:
277 case PBLK_WRITE_INT:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400278 pool = &pblk->w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200279 rq_size = pblk_w_rq_size;
Javier González67bf26a2017-10-13 14:46:20 +0200280 break;
281 case PBLK_READ:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400282 pool = &pblk->r_rq_pool;
Javier González084ec9b2017-06-26 16:27:13 -0600283 rq_size = pblk_g_rq_size;
Javier González67bf26a2017-10-13 14:46:20 +0200284 break;
285 default:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400286 pool = &pblk->e_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200287 rq_size = pblk_g_rq_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200288 }
289
290 rqd = mempool_alloc(pool, GFP_KERNEL);
291 memset(rqd, 0, rq_size);
292
293 return rqd;
294}
295
Javier González67bf26a2017-10-13 14:46:20 +0200296/* Typically used on completion path. Cannot guarantee request consistency */
297void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298{
299 mempool_t *pool;
300
Javier González67bf26a2017-10-13 14:46:20 +0200301 switch (type) {
302 case PBLK_WRITE:
303 kfree(((struct pblk_c_ctx *)nvm_rq_to_pdu(rqd))->lun_bitmap);
Gustavo A. R. Silva884b0312018-07-13 10:48:43 +0200304 /* fall through */
Javier González67bf26a2017-10-13 14:46:20 +0200305 case PBLK_WRITE_INT:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400306 pool = &pblk->w_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200307 break;
308 case PBLK_READ:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400309 pool = &pblk->r_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200310 break;
311 case PBLK_ERASE:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400312 pool = &pblk->e_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200313 break;
314 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200315 pblk_err(pblk, "trying to free unknown rqd type\n");
Javier González67bf26a2017-10-13 14:46:20 +0200316 return;
317 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200318
Javier González45dcf292018-10-09 13:12:02 +0200319 pblk_free_rqd_meta(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200320 mempool_free(rqd, pool);
321}
322
323void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
324 int nr_pages)
325{
326 struct bio_vec bv;
327 int i;
328
329 WARN_ON(off + nr_pages != bio->bi_vcnt);
330
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200331 for (i = off; i < nr_pages + off; i++) {
332 bv = bio->bi_io_vec[i];
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400333 mempool_free(bv.bv_page, &pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200334 }
335}
336
337int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
338 int nr_pages)
339{
340 struct request_queue *q = pblk->dev->q;
341 struct page *page;
342 int i, ret;
343
344 for (i = 0; i < nr_pages; i++) {
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400345 page = mempool_alloc(&pblk->page_bio_pool, flags);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200346
347 ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0);
348 if (ret != PBLK_EXPOSED_PAGE_SIZE) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200349 pblk_err(pblk, "could not add page to bio\n");
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400350 mempool_free(page, &pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200351 goto err;
352 }
353 }
354
355 return 0;
356err:
Igor Konopkof142ac02018-06-01 16:41:08 +0200357 pblk_bio_free_pages(pblk, bio, (bio->bi_vcnt - i), i);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200358 return -1;
359}
360
Hans Holmbergcc9c9a02018-06-01 16:41:13 +0200361void pblk_write_kick(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200362{
363 wake_up_process(pblk->writer_ts);
364 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000));
365}
366
Kees Cook87c1d2d2017-10-17 21:10:19 -0700367void pblk_write_timer_fn(struct timer_list *t)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200368{
Kees Cook87c1d2d2017-10-17 21:10:19 -0700369 struct pblk *pblk = from_timer(pblk, t, wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200370
371 /* kick the write thread every tick to flush outstanding data */
372 pblk_write_kick(pblk);
373}
374
375void pblk_write_should_kick(struct pblk *pblk)
376{
377 unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb);
378
Igor Konopko55d8ec32018-12-11 20:16:26 +0100379 if (secs_avail >= pblk->min_write_pgs_data)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200380 pblk_write_kick(pblk);
381}
382
Javier González8bd40022017-10-13 14:46:44 +0200383static void pblk_wait_for_meta(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200384{
Javier González588726d32017-06-26 11:57:29 +0200385 do {
386 if (!atomic_read(&pblk->inflight_io))
387 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200388
Javier González588726d32017-06-26 11:57:29 +0200389 schedule();
390 } while (1);
391}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200392
Javier González588726d32017-06-26 11:57:29 +0200393static void pblk_flush_writer(struct pblk *pblk)
394{
395 pblk_rb_flush(&pblk->rwb);
396 do {
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200397 if (!pblk_rb_sync_count(&pblk->rwb))
Javier González588726d32017-06-26 11:57:29 +0200398 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200399
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200400 pblk_write_kick(pblk);
Javier González588726d32017-06-26 11:57:29 +0200401 schedule();
402 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200403}
404
405struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line)
406{
407 struct pblk_line_meta *lm = &pblk->lm;
408 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
409 struct list_head *move_list = NULL;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100410 int packed_meta = (le32_to_cpu(*line->vsc) / pblk->min_write_pgs_data)
411 * (pblk->min_write_pgs - pblk->min_write_pgs_data);
412 int vsc = le32_to_cpu(*line->vsc) + packed_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200413
Javier González476118c2017-06-26 11:57:26 +0200414 lockdep_assert_held(&line->lock);
415
Hans Holmberg48b8d202018-06-01 16:41:06 +0200416 if (line->w_err_gc->has_write_err) {
417 if (line->gc_group != PBLK_LINEGC_WERR) {
418 line->gc_group = PBLK_LINEGC_WERR;
419 move_list = &l_mg->gc_werr_list;
420 pblk_rl_werr_line_in(&pblk->rl);
421 }
422 } else if (!vsc) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200423 if (line->gc_group != PBLK_LINEGC_FULL) {
424 line->gc_group = PBLK_LINEGC_FULL;
425 move_list = &l_mg->gc_full_list;
426 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200427 } else if (vsc < lm->high_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200428 if (line->gc_group != PBLK_LINEGC_HIGH) {
429 line->gc_group = PBLK_LINEGC_HIGH;
430 move_list = &l_mg->gc_high_list;
431 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200432 } else if (vsc < lm->mid_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200433 if (line->gc_group != PBLK_LINEGC_MID) {
434 line->gc_group = PBLK_LINEGC_MID;
435 move_list = &l_mg->gc_mid_list;
436 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200437 } else if (vsc < line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438 if (line->gc_group != PBLK_LINEGC_LOW) {
439 line->gc_group = PBLK_LINEGC_LOW;
440 move_list = &l_mg->gc_low_list;
441 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200442 } else if (vsc == line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200443 if (line->gc_group != PBLK_LINEGC_EMPTY) {
444 line->gc_group = PBLK_LINEGC_EMPTY;
445 move_list = &l_mg->gc_empty_list;
446 }
447 } else {
448 line->state = PBLK_LINESTATE_CORRUPT;
Hans Holmbergf2937232018-10-09 13:11:53 +0200449 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
450 line->state);
451
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200452 line->gc_group = PBLK_LINEGC_NONE;
453 move_list = &l_mg->corrupt_list;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200454 pblk_err(pblk, "corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200455 line->id, vsc,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200456 line->sec_in_line,
457 lm->high_thrs, lm->mid_thrs);
458 }
459
460 return move_list;
461}
462
463void pblk_discard(struct pblk *pblk, struct bio *bio)
464{
465 sector_t slba = pblk_get_lba(bio);
466 sector_t nr_secs = pblk_get_secs(bio);
467
468 pblk_invalidate_range(pblk, slba, nr_secs);
469}
470
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200471void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd)
472{
473 atomic_long_inc(&pblk->write_failed);
Matias Bjørling880eda52018-07-13 10:48:37 +0200474#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200475 pblk_print_failed_rqd(pblk, rqd, rqd->error);
476#endif
477}
478
479void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd)
480{
481 /* Empty page read is not necessarily an error (e.g., L2P recovery) */
482 if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
483 atomic_long_inc(&pblk->read_empty);
484 return;
485 }
486
487 switch (rqd->error) {
488 case NVM_RSP_WARN_HIGHECC:
489 atomic_long_inc(&pblk->read_high_ecc);
490 break;
491 case NVM_RSP_ERR_FAILECC:
492 case NVM_RSP_ERR_FAILCRC:
493 atomic_long_inc(&pblk->read_failed);
494 break;
495 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200496 pblk_err(pblk, "unknown read error:%d\n", rqd->error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200497 }
Matias Bjørling880eda52018-07-13 10:48:37 +0200498#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200499 pblk_print_failed_rqd(pblk, rqd, rqd->error);
500#endif
501}
502
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200503void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write)
504{
505 pblk->sec_per_write = sec_per_write;
506}
507
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200508int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd)
509{
510 struct nvm_tgt_dev *dev = pblk->dev;
511
Javier González588726d32017-06-26 11:57:29 +0200512 atomic_inc(&pblk->inflight_io);
513
Matias Bjørling880eda52018-07-13 10:48:37 +0200514#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezb6730dd42018-06-01 15:04:20 +0200515 if (pblk_check_io(pblk, rqd))
516 return NVM_IO_ERR;
517#endif
518
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200519 return nvm_submit_io(dev, rqd);
520}
521
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200522void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd)
523{
524 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
525
526 int i;
527
528 for (i = 0; i < rqd->nr_ppas; i++) {
529 struct ppa_addr *ppa = &ppa_list[i];
530 struct nvm_chk_meta *chunk = pblk_dev_ppa_to_chunk(pblk, *ppa);
531 u64 caddr = pblk_dev_ppa_to_chunk_addr(pblk, *ppa);
532
533 if (caddr == 0)
534 trace_pblk_chunk_state(pblk_disk_name(pblk),
535 ppa, NVM_CHK_ST_OPEN);
Hans Holmberg96076f72018-12-11 20:16:09 +0100536 else if (caddr == (chunk->cnlb - 1))
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200537 trace_pblk_chunk_state(pblk_disk_name(pblk),
538 ppa, NVM_CHK_ST_CLOSED);
539 }
540}
541
Javier González1a94b2d2017-10-13 14:46:47 +0200542int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd)
543{
544 struct nvm_tgt_dev *dev = pblk->dev;
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200545 int ret;
Javier González1a94b2d2017-10-13 14:46:47 +0200546
Javier González1a94b2d2017-10-13 14:46:47 +0200547 atomic_inc(&pblk->inflight_io);
548
Matias Bjørling880eda52018-07-13 10:48:37 +0200549#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezb6730dd42018-06-01 15:04:20 +0200550 if (pblk_check_io(pblk, rqd))
551 return NVM_IO_ERR;
552#endif
553
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200554 ret = nvm_submit_io_sync(dev, rqd);
555
556 if (trace_pblk_chunk_state_enabled() && !ret &&
557 rqd->opcode == NVM_OP_PWRITE)
558 pblk_check_chunk_state_update(pblk, rqd);
559
560 return ret;
Javier González1a94b2d2017-10-13 14:46:47 +0200561}
562
Javier González253babc2018-10-09 13:12:04 +0200563int pblk_submit_io_sync_sem(struct pblk *pblk, struct nvm_rq *rqd)
564{
565 struct ppa_addr *ppa_list;
566 int ret;
567
568 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
569
570 pblk_down_chunk(pblk, ppa_list[0]);
571 ret = pblk_submit_io_sync(pblk, rqd);
572 pblk_up_chunk(pblk, ppa_list[0]);
573
574 return ret;
575}
576
Javier González55e836d2017-10-13 14:46:16 +0200577static void pblk_bio_map_addr_endio(struct bio *bio)
578{
579 bio_put(bio);
580}
581
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
583 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200584 int alloc_type, gfp_t gfp_mask)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200585{
586 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200587 void *kaddr = data;
588 struct page *page;
589 struct bio *bio;
590 int i, ret;
591
Javier Gonzálezde54e702017-06-30 17:56:39 +0200592 if (alloc_type == PBLK_KMALLOC_META)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200593 return bio_map_kern(dev->q, kaddr, len, gfp_mask);
594
595 bio = bio_kmalloc(gfp_mask, nr_secs);
596 if (!bio)
597 return ERR_PTR(-ENOMEM);
598
599 for (i = 0; i < nr_secs; i++) {
600 page = vmalloc_to_page(kaddr);
601 if (!page) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200602 pblk_err(pblk, "could not map vmalloc bio\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200603 bio_put(bio);
604 bio = ERR_PTR(-ENOMEM);
605 goto out;
606 }
607
608 ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0);
609 if (ret != PAGE_SIZE) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200610 pblk_err(pblk, "could not add page to bio\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200611 bio_put(bio);
612 bio = ERR_PTR(-ENOMEM);
613 goto out;
614 }
615
616 kaddr += PAGE_SIZE;
617 }
Javier González55e836d2017-10-13 14:46:16 +0200618
619 bio->bi_end_io = pblk_bio_map_addr_endio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200620out:
621 return bio;
622}
623
624int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
Igor Konopko55d8ec32018-12-11 20:16:26 +0100625 unsigned long secs_to_flush, bool skip_meta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200626{
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200627 int max = pblk->sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200628 int min = pblk->min_write_pgs;
629 int secs_to_sync = 0;
630
Igor Konopko55d8ec32018-12-11 20:16:26 +0100631 if (skip_meta && pblk->min_write_pgs_data != pblk->min_write_pgs)
632 min = max = pblk->min_write_pgs_data;
633
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200634 if (secs_avail >= max)
635 secs_to_sync = max;
636 else if (secs_avail >= min)
637 secs_to_sync = min * (secs_avail / min);
638 else if (secs_to_flush)
639 secs_to_sync = min;
640
641 return secs_to_sync;
642}
643
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200644void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
645{
646 u64 addr;
647 int i;
648
Rakesh Pandite57903f2017-10-13 14:45:56 +0200649 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200650 addr = find_next_zero_bit(line->map_bitmap,
651 pblk->lm.sec_per_line, line->cur_sec);
652 line->cur_sec = addr - nr_secs;
653
654 for (i = 0; i < nr_secs; i++, line->cur_sec--)
655 WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap));
Rakesh Pandite57903f2017-10-13 14:45:56 +0200656 spin_unlock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200657}
658
659u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200660{
661 u64 addr;
662 int i;
663
Javier González476118c2017-06-26 11:57:26 +0200664 lockdep_assert_held(&line->lock);
665
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200666 /* logic error: ppa out-of-bounds. Prevent generating bad address */
667 if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) {
668 WARN(1, "pblk: page allocation out of bounds\n");
669 nr_secs = pblk->lm.sec_per_line - line->cur_sec;
670 }
671
672 line->cur_sec = addr = find_next_zero_bit(line->map_bitmap,
673 pblk->lm.sec_per_line, line->cur_sec);
674 for (i = 0; i < nr_secs; i++, line->cur_sec++)
675 WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap));
676
677 return addr;
678}
679
680u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
681{
682 u64 addr;
683
684 /* Lock needed in case a write fails and a recovery needs to remap
685 * failed write buffer entries
686 */
687 spin_lock(&line->lock);
688 addr = __pblk_alloc_page(pblk, line, nr_secs);
689 line->left_msecs -= nr_secs;
690 WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n");
691 spin_unlock(&line->lock);
692
693 return addr;
694}
695
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200696u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line)
697{
698 u64 paddr;
699
700 spin_lock(&line->lock);
701 paddr = find_next_zero_bit(line->map_bitmap,
702 pblk->lm.sec_per_line, line->cur_sec);
703 spin_unlock(&line->lock);
704
705 return paddr;
706}
707
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200708u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
709{
710 struct nvm_tgt_dev *dev = pblk->dev;
711 struct nvm_geo *geo = &dev->geo;
712 struct pblk_line_meta *lm = &pblk->lm;
713 int bit;
714
715 /* This usually only happens on bad lines */
716 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
717 if (bit >= lm->blk_per_line)
718 return -1;
719
720 return bit * geo->ws_opt;
721}
722
723int pblk_line_smeta_read(struct pblk *pblk, struct pblk_line *line)
724{
725 struct nvm_tgt_dev *dev = pblk->dev;
726 struct pblk_line_meta *lm = &pblk->lm;
727 struct bio *bio;
728 struct nvm_rq rqd;
729 u64 paddr = pblk_line_smeta_start(pblk, line);
730 int i, ret;
731
732 memset(&rqd, 0, sizeof(struct nvm_rq));
733
734 ret = pblk_alloc_rqd_meta(pblk, &rqd);
735 if (ret)
736 return ret;
737
738 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
739 if (IS_ERR(bio)) {
740 ret = PTR_ERR(bio);
741 goto clear_rqd;
742 }
743
744 bio->bi_iter.bi_sector = 0; /* internal bio */
745 bio_set_op_attrs(bio, REQ_OP_READ, 0);
746
747 rqd.bio = bio;
748 rqd.opcode = NVM_OP_PREAD;
749 rqd.nr_ppas = lm->smeta_sec;
750 rqd.is_seq = 1;
751
752 for (i = 0; i < lm->smeta_sec; i++, paddr++)
753 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
754
755 ret = pblk_submit_io_sync(pblk, &rqd);
756 if (ret) {
757 pblk_err(pblk, "smeta I/O submission failed: %d\n", ret);
758 bio_put(bio);
759 goto clear_rqd;
760 }
761
762 atomic_dec(&pblk->inflight_io);
763
764 if (rqd.error)
765 pblk_log_read_err(pblk, &rqd);
766
767clear_rqd:
768 pblk_free_rqd_meta(pblk, &rqd);
769 return ret;
770}
771
772static int pblk_line_smeta_write(struct pblk *pblk, struct pblk_line *line,
773 u64 paddr)
774{
775 struct nvm_tgt_dev *dev = pblk->dev;
776 struct pblk_line_meta *lm = &pblk->lm;
777 struct bio *bio;
778 struct nvm_rq rqd;
779 __le64 *lba_list = emeta_to_lbas(pblk, line->emeta->buf);
780 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
781 int i, ret;
782
783 memset(&rqd, 0, sizeof(struct nvm_rq));
784
785 ret = pblk_alloc_rqd_meta(pblk, &rqd);
786 if (ret)
787 return ret;
788
789 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
790 if (IS_ERR(bio)) {
791 ret = PTR_ERR(bio);
792 goto clear_rqd;
793 }
794
795 bio->bi_iter.bi_sector = 0; /* internal bio */
796 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
797
798 rqd.bio = bio;
799 rqd.opcode = NVM_OP_PWRITE;
800 rqd.nr_ppas = lm->smeta_sec;
801 rqd.is_seq = 1;
802
803 for (i = 0; i < lm->smeta_sec; i++, paddr++) {
Igor Konopkofaa79f22018-12-11 20:16:23 +0100804 struct pblk_sec_meta *meta = pblk_get_meta(pblk,
805 rqd.meta_list, i);
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200806
807 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
Igor Konopkofaa79f22018-12-11 20:16:23 +0100808 meta->lba = lba_list[paddr] = addr_empty;
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200809 }
810
Javier González253babc2018-10-09 13:12:04 +0200811 ret = pblk_submit_io_sync_sem(pblk, &rqd);
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200812 if (ret) {
813 pblk_err(pblk, "smeta I/O submission failed: %d\n", ret);
814 bio_put(bio);
815 goto clear_rqd;
816 }
817
818 atomic_dec(&pblk->inflight_io);
819
820 if (rqd.error) {
821 pblk_log_write_err(pblk, &rqd);
822 ret = -EIO;
823 }
824
825clear_rqd:
826 pblk_free_rqd_meta(pblk, &rqd);
827 return ret;
828}
829
830int pblk_line_emeta_read(struct pblk *pblk, struct pblk_line *line,
831 void *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200832{
833 struct nvm_tgt_dev *dev = pblk->dev;
834 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezde54e702017-06-30 17:56:39 +0200835 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200836 struct pblk_line_meta *lm = &pblk->lm;
Javier González63e38092017-06-26 11:57:24 +0200837 void *ppa_list, *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200838 struct bio *bio;
839 struct nvm_rq rqd;
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200840 u64 paddr = line->emeta_ssec;
Javier González63e38092017-06-26 11:57:24 +0200841 dma_addr_t dma_ppa_list, dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842 int min = pblk->min_write_pgs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200843 int left_ppas = lm->emeta_sec[0];
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200844 int line_id = line->id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200845 int rq_ppas, rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200846 int i, j;
847 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200848
Javier González63e38092017-06-26 11:57:24 +0200849 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
850 &dma_meta_list);
851 if (!meta_list)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200852 return -ENOMEM;
853
Igor Konopko24828d02018-12-11 20:16:24 +0100854 ppa_list = meta_list + pblk_dma_meta_size(pblk);
855 dma_ppa_list = dma_meta_list + pblk_dma_meta_size(pblk);
Javier González63e38092017-06-26 11:57:24 +0200856
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200857next_rq:
858 memset(&rqd, 0, sizeof(struct nvm_rq));
859
Igor Konopko55d8ec32018-12-11 20:16:26 +0100860 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0, false);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200861 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200862
Javier Gonzálezde54e702017-06-30 17:56:39 +0200863 bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len,
864 l_mg->emeta_alloc_type, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200865 if (IS_ERR(bio)) {
866 ret = PTR_ERR(bio);
867 goto free_rqd_dma;
868 }
869
870 bio->bi_iter.bi_sector = 0; /* internal bio */
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200871 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200872
873 rqd.bio = bio;
Javier González63e38092017-06-26 11:57:24 +0200874 rqd.meta_list = meta_list;
875 rqd.ppa_list = ppa_list;
876 rqd.dma_meta_list = dma_meta_list;
877 rqd.dma_ppa_list = dma_ppa_list;
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200878 rqd.opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200879 rqd.nr_ppas = rq_ppas;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200880
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200881 for (i = 0; i < rqd.nr_ppas; ) {
882 struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, line_id);
883 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González63e38092017-06-26 11:57:24 +0200884
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200885 if (pblk_io_aligned(pblk, rq_ppas))
886 rqd.is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200887
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200888 while (test_bit(pos, line->blk_bitmap)) {
889 paddr += min;
890 if (pblk_boundary_paddr_checks(pblk, paddr)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200891 bio_put(bio);
892 ret = -EINTR;
893 goto free_rqd_dma;
894 }
895
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200896 ppa = addr_to_gen_ppa(pblk, paddr, line_id);
897 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200898 }
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200899
900 if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
901 bio_put(bio);
902 ret = -EINTR;
903 goto free_rqd_dma;
904 }
905
906 for (j = 0; j < min; j++, i++, paddr++)
907 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line_id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200908 }
909
Javier González1a94b2d2017-10-13 14:46:47 +0200910 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200911 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200912 pblk_err(pblk, "emeta I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200913 bio_put(bio);
914 goto free_rqd_dma;
915 }
916
Javier González588726d32017-06-26 11:57:29 +0200917 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200918
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200919 if (rqd.error)
920 pblk_log_read_err(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200921
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200922 emeta_buf += rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200923 left_ppas -= rq_ppas;
924 if (left_ppas)
925 goto next_rq;
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200926
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200927free_rqd_dma:
Javier González63e38092017-06-26 11:57:24 +0200928 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200929 return ret;
930}
931
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200932static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
933 struct ppa_addr ppa)
934{
935 rqd->opcode = NVM_OP_ERASE;
936 rqd->ppa_addr = ppa;
937 rqd->nr_ppas = 1;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200938 rqd->is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200939 rqd->bio = NULL;
940}
941
942static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
943{
Matias Bjørling4b5d56e2018-10-09 13:11:33 +0200944 struct nvm_rq rqd = {NULL};
945 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200946
Hans Holmberg4209c312018-10-09 13:11:55 +0200947 trace_pblk_chunk_reset(pblk_disk_name(pblk), &ppa,
948 PBLK_CHUNK_RESET_START);
949
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200950 pblk_setup_e_rq(pblk, &rqd, ppa);
951
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200952 /* The write thread schedules erases so that it minimizes disturbances
953 * with writes. Thus, there is no need to take the LUN semaphore.
954 */
Javier González1a94b2d2017-10-13 14:46:47 +0200955 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200956 rqd.private = pblk;
957 __pblk_end_io_erase(pblk, &rqd);
958
Javier González588726d32017-06-26 11:57:29 +0200959 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200960}
961
962int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
963{
964 struct pblk_line_meta *lm = &pblk->lm;
965 struct ppa_addr ppa;
Javier González588726d32017-06-26 11:57:29 +0200966 int ret, bit = -1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200967
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200968 /* Erase only good blocks, one at a time */
969 do {
970 spin_lock(&line->lock);
971 bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
972 bit + 1);
973 if (bit >= lm->blk_per_line) {
974 spin_unlock(&line->lock);
975 break;
976 }
977
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200978 ppa = pblk->luns[bit].bppa; /* set ch and lun */
Javier González69471512018-03-30 00:05:15 +0200979 ppa.a.blk = line->id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200980
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200981 atomic_dec(&line->left_eblks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200982 WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200983 spin_unlock(&line->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200984
Javier González588726d32017-06-26 11:57:29 +0200985 ret = pblk_blk_erase_sync(pblk, ppa);
986 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200987 pblk_err(pblk, "failed to erase line %d\n", line->id);
Javier González588726d32017-06-26 11:57:29 +0200988 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200989 }
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200990 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200991
992 return 0;
993}
994
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200995static void pblk_line_setup_metadata(struct pblk_line *line,
996 struct pblk_line_mgmt *l_mg,
997 struct pblk_line_meta *lm)
998{
999 int meta_line;
1000
Javier González588726d32017-06-26 11:57:29 +02001001 lockdep_assert_held(&l_mg->free_lock);
1002
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001003retry_meta:
1004 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
1005 if (meta_line == PBLK_DATA_LINES) {
1006 spin_unlock(&l_mg->free_lock);
1007 io_schedule();
1008 spin_lock(&l_mg->free_lock);
1009 goto retry_meta;
1010 }
1011
1012 set_bit(meta_line, &l_mg->meta_bitmap);
1013 line->meta_line = meta_line;
1014
1015 line->smeta = l_mg->sline_meta[meta_line];
1016 line->emeta = l_mg->eline_meta[meta_line];
1017
1018 memset(line->smeta, 0, lm->smeta_len);
1019 memset(line->emeta->buf, 0, lm->emeta_len[0]);
1020
1021 line->emeta->mem = 0;
1022 atomic_set(&line->emeta->sync, 0);
1023}
1024
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001025/* For now lines are always assumed full lines. Thus, smeta former and current
1026 * lun bitmaps are omitted.
1027 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001028static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001029 struct pblk_line *cur)
1030{
1031 struct nvm_tgt_dev *dev = pblk->dev;
1032 struct nvm_geo *geo = &dev->geo;
1033 struct pblk_line_meta *lm = &pblk->lm;
1034 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001035 struct pblk_emeta *emeta = line->emeta;
1036 struct line_emeta *emeta_buf = emeta->buf;
1037 struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001038 int nr_blk_line;
1039
1040 /* After erasing the line, new bad blocks might appear and we risk
1041 * having an invalid line
1042 */
1043 nr_blk_line = lm->blk_per_line -
1044 bitmap_weight(line->blk_bitmap, lm->blk_per_line);
1045 if (nr_blk_line < lm->min_blk_line) {
1046 spin_lock(&l_mg->free_lock);
1047 spin_lock(&line->lock);
1048 line->state = PBLK_LINESTATE_BAD;
Hans Holmbergf2937232018-10-09 13:11:53 +02001049 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1050 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001051 spin_unlock(&line->lock);
1052
1053 list_add_tail(&line->list, &l_mg->bad_list);
1054 spin_unlock(&l_mg->free_lock);
1055
Matias Bjørling4e495a42018-07-13 10:48:42 +02001056 pblk_debug(pblk, "line %d is bad\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001057
1058 return 0;
1059 }
1060
1061 /* Run-time metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001062 line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001063
1064 /* Mark LUNs allocated in this line (all for now) */
1065 bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
1066
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001067 smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
Andy Shevchenko7e0a0842019-02-11 13:25:05 +01001068 guid_copy((guid_t *)&smeta_buf->header.uuid, &pblk->instance_uuid);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001069 smeta_buf->header.id = cpu_to_le32(line->id);
1070 smeta_buf->header.type = cpu_to_le16(line->type);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +02001071 smeta_buf->header.version_major = SMETA_VERSION_MAJOR;
1072 smeta_buf->header.version_minor = SMETA_VERSION_MINOR;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001073
1074 /* Start metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001075 smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001076 smeta_buf->window_wr_lun = cpu_to_le32(geo->all_luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001077
1078 /* Fill metadata among lines */
1079 if (cur) {
1080 memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001081 smeta_buf->prev_id = cpu_to_le32(cur->id);
1082 cur->emeta->buf->next_id = cpu_to_le32(line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001083 } else {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001084 smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001085 }
1086
1087 /* All smeta must be set at this point */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001088 smeta_buf->header.crc = cpu_to_le32(
1089 pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
1090 smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001091
1092 /* End metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001093 memcpy(&emeta_buf->header, &smeta_buf->header,
1094 sizeof(struct line_header));
Hans Holmbergd0ab0b12018-03-30 00:04:51 +02001095
1096 emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
1097 emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
1098 emeta_buf->header.crc = cpu_to_le32(
1099 pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
1100
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001101 emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
1102 emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
1103 emeta_buf->nr_valid_lbas = cpu_to_le64(0);
1104 emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
1105 emeta_buf->crc = cpu_to_le32(0);
1106 emeta_buf->prev_id = smeta_buf->prev_id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001107
1108 return 1;
1109}
1110
Javier González9cfd5a92018-06-01 16:41:14 +02001111static int pblk_line_alloc_bitmaps(struct pblk *pblk, struct pblk_line *line)
1112{
1113 struct pblk_line_meta *lm = &pblk->lm;
Hans Holmberg53d82db2018-10-09 13:11:47 +02001114 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González9cfd5a92018-06-01 16:41:14 +02001115
Hans Holmberg53d82db2018-10-09 13:11:47 +02001116 line->map_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
Javier González9cfd5a92018-06-01 16:41:14 +02001117 if (!line->map_bitmap)
1118 return -ENOMEM;
1119
Hans Holmberg53d82db2018-10-09 13:11:47 +02001120 memset(line->map_bitmap, 0, lm->sec_bitmap_len);
1121
Javier González9cfd5a92018-06-01 16:41:14 +02001122 /* will be initialized using bb info from map_bitmap */
Hans Holmberg53d82db2018-10-09 13:11:47 +02001123 line->invalid_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
Javier González9cfd5a92018-06-01 16:41:14 +02001124 if (!line->invalid_bitmap) {
Hans Holmberg53d82db2018-10-09 13:11:47 +02001125 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier González9cfd5a92018-06-01 16:41:14 +02001126 line->map_bitmap = NULL;
1127 return -ENOMEM;
1128 }
1129
1130 return 0;
1131}
1132
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001133/* For now lines are always assumed full lines. Thus, smeta former and current
1134 * lun bitmaps are omitted.
1135 */
1136static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
1137 int init)
1138{
1139 struct nvm_tgt_dev *dev = pblk->dev;
1140 struct nvm_geo *geo = &dev->geo;
1141 struct pblk_line_meta *lm = &pblk->lm;
1142 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001143 u64 off;
1144 int bit = -1;
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001145 int emeta_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001146
1147 line->sec_in_line = lm->sec_per_line;
1148
1149 /* Capture bad block information on line mapping bitmaps */
1150 while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
1151 bit + 1)) < lm->blk_per_line) {
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001152 off = bit * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001153 bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
1154 lm->sec_per_line);
1155 bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
1156 lm->sec_per_line);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001157 line->sec_in_line -= geo->clba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001158 }
1159
1160 /* Mark smeta metadata sectors as bad sectors */
1161 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001162 off = bit * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001163 bitmap_set(line->map_bitmap, off, lm->smeta_sec);
1164 line->sec_in_line -= lm->smeta_sec;
1165 line->smeta_ssec = off;
1166 line->cur_sec = off + lm->smeta_sec;
1167
Javier Gonzálezaf3fac12018-10-09 13:12:03 +02001168 if (init && pblk_line_smeta_write(pblk, line, off)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001169 pblk_debug(pblk, "line smeta I/O failed. Retry\n");
Hans Holmberg6cf17a22018-06-01 16:41:07 +02001170 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001171 }
1172
1173 bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
1174
1175 /* Mark emeta metadata sectors as bad sectors. We need to consider bad
1176 * blocks to make sure that there are enough sectors to store emeta
1177 */
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001178 emeta_secs = lm->emeta_sec[0];
1179 off = lm->sec_per_line;
1180 while (emeta_secs) {
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001181 off -= geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001182 if (!test_bit(off, line->invalid_bitmap)) {
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001183 bitmap_set(line->invalid_bitmap, off, geo->ws_opt);
1184 emeta_secs -= geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001185 }
1186 }
1187
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001188 line->emeta_ssec = off;
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001189 line->sec_in_line -= lm->emeta_sec[0];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001190 line->nr_valid_lbas = 0;
Javier González0880a9a2017-06-26 11:57:19 +02001191 line->left_msecs = line->sec_in_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001192 *line->vsc = cpu_to_le32(line->sec_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001193
1194 if (lm->sec_per_line - line->sec_in_line !=
1195 bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
1196 spin_lock(&line->lock);
1197 line->state = PBLK_LINESTATE_BAD;
Hans Holmbergf2937232018-10-09 13:11:53 +02001198 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1199 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001200 spin_unlock(&line->lock);
1201
1202 list_add_tail(&line->list, &l_mg->bad_list);
Matias Bjørling4e495a42018-07-13 10:48:42 +02001203 pblk_err(pblk, "unexpected line %d is bad\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001204
1205 return 0;
1206 }
1207
1208 return 1;
1209}
1210
Javier González32ef9412018-03-30 00:05:20 +02001211static int pblk_prepare_new_line(struct pblk *pblk, struct pblk_line *line)
1212{
1213 struct pblk_line_meta *lm = &pblk->lm;
1214 struct nvm_tgt_dev *dev = pblk->dev;
1215 struct nvm_geo *geo = &dev->geo;
1216 int blk_to_erase = atomic_read(&line->blk_in_line);
1217 int i;
1218
1219 for (i = 0; i < lm->blk_per_line; i++) {
1220 struct pblk_lun *rlun = &pblk->luns[i];
1221 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
1222 int state = line->chks[pos].state;
1223
1224 /* Free chunks should not be erased */
1225 if (state & NVM_CHK_ST_FREE) {
1226 set_bit(pblk_ppa_to_pos(geo, rlun->bppa),
1227 line->erase_bitmap);
1228 blk_to_erase--;
1229 }
1230 }
1231
1232 return blk_to_erase;
1233}
1234
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001235static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
1236{
1237 struct pblk_line_meta *lm = &pblk->lm;
Javier González1d8b33e2018-06-01 15:04:16 +02001238 int blk_in_line = atomic_read(&line->blk_in_line);
Javier González9cfd5a92018-06-01 16:41:14 +02001239 int blk_to_erase;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001240
Javier González32ef9412018-03-30 00:05:20 +02001241 /* Bad blocks do not need to be erased */
1242 bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line);
1243
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001244 spin_lock(&line->lock);
Javier González32ef9412018-03-30 00:05:20 +02001245
1246 /* If we have not written to this line, we need to mark up free chunks
1247 * as already erased
1248 */
1249 if (line->state == PBLK_LINESTATE_NEW) {
1250 blk_to_erase = pblk_prepare_new_line(pblk, line);
1251 line->state = PBLK_LINESTATE_FREE;
Hans Holmbergf2937232018-10-09 13:11:53 +02001252 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1253 line->state);
Javier González32ef9412018-03-30 00:05:20 +02001254 } else {
Javier González1d8b33e2018-06-01 15:04:16 +02001255 blk_to_erase = blk_in_line;
1256 }
1257
1258 if (blk_in_line < lm->min_blk_line) {
Javier González9cfd5a92018-06-01 16:41:14 +02001259 spin_unlock(&line->lock);
1260 return -EAGAIN;
Javier González32ef9412018-03-30 00:05:20 +02001261 }
1262
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001263 if (line->state != PBLK_LINESTATE_FREE) {
Javier González588726d32017-06-26 11:57:29 +02001264 WARN(1, "pblk: corrupted line %d, state %d\n",
1265 line->id, line->state);
Javier González9cfd5a92018-06-01 16:41:14 +02001266 spin_unlock(&line->lock);
1267 return -EINTR;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001268 }
Javier González588726d32017-06-26 11:57:29 +02001269
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001270 line->state = PBLK_LINESTATE_OPEN;
Hans Holmbergf2937232018-10-09 13:11:53 +02001271 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1272 line->state);
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001273
Javier González32ef9412018-03-30 00:05:20 +02001274 atomic_set(&line->left_eblks, blk_to_erase);
1275 atomic_set(&line->left_seblks, blk_to_erase);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001276
1277 line->meta_distance = lm->meta_distance;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001278 spin_unlock(&line->lock);
1279
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001280 kref_init(&line->ref);
Heiner Litz05869422019-02-11 13:25:09 +01001281 atomic_set(&line->sec_to_update, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001282
1283 return 0;
1284}
1285
Javier González361d8892018-12-11 20:16:19 +01001286/* Line allocations in the recovery path are always single threaded */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001287int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
1288{
1289 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1290 int ret;
1291
1292 spin_lock(&l_mg->free_lock);
1293 l_mg->data_line = line;
1294 list_del(&line->list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001295
1296 ret = pblk_line_prepare(pblk, line);
1297 if (ret) {
1298 list_add(&line->list, &l_mg->free_list);
Javier González3dc001f2017-04-22 01:32:45 +02001299 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001300 return ret;
1301 }
Javier González3dc001f2017-04-22 01:32:45 +02001302 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001303
Javier González9cfd5a92018-06-01 16:41:14 +02001304 ret = pblk_line_alloc_bitmaps(pblk, line);
1305 if (ret)
Hua Sufde201a2018-12-11 20:16:18 +01001306 goto fail;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001307
1308 if (!pblk_line_init_bb(pblk, line, 0)) {
Hua Sufde201a2018-12-11 20:16:18 +01001309 ret = -EINTR;
1310 goto fail;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001311 }
1312
Javier González9cfd5a92018-06-01 16:41:14 +02001313 pblk_rl_free_lines_dec(&pblk->rl, line, true);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001314 return 0;
Hua Sufde201a2018-12-11 20:16:18 +01001315
1316fail:
1317 spin_lock(&l_mg->free_lock);
1318 list_add(&line->list, &l_mg->free_list);
1319 spin_unlock(&l_mg->free_lock);
1320
1321 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001322}
1323
1324void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
1325{
Hans Holmberg53d82db2018-10-09 13:11:47 +02001326 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1327
1328 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001329 line->map_bitmap = NULL;
1330 line->smeta = NULL;
1331 line->emeta = NULL;
1332}
1333
Javier González9cfd5a92018-06-01 16:41:14 +02001334static void pblk_line_reinit(struct pblk_line *line)
1335{
1336 *line->vsc = cpu_to_le32(EMPTY_ENTRY);
1337
1338 line->map_bitmap = NULL;
1339 line->invalid_bitmap = NULL;
1340 line->smeta = NULL;
1341 line->emeta = NULL;
1342}
1343
1344void pblk_line_free(struct pblk_line *line)
1345{
Hans Holmberg53d82db2018-10-09 13:11:47 +02001346 struct pblk *pblk = line->pblk;
1347 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1348
1349 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
1350 mempool_free(line->invalid_bitmap, l_mg->bitmap_pool);
Javier González9cfd5a92018-06-01 16:41:14 +02001351
1352 pblk_line_reinit(line);
1353}
1354
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001355struct pblk_line *pblk_line_get(struct pblk *pblk)
1356{
1357 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1358 struct pblk_line_meta *lm = &pblk->lm;
Javier González588726d32017-06-26 11:57:29 +02001359 struct pblk_line *line;
1360 int ret, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001361
1362 lockdep_assert_held(&l_mg->free_lock);
1363
Javier González588726d32017-06-26 11:57:29 +02001364retry:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001365 if (list_empty(&l_mg->free_list)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001366 pblk_err(pblk, "no free lines\n");
Javier González588726d32017-06-26 11:57:29 +02001367 return NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001368 }
1369
1370 line = list_first_entry(&l_mg->free_list, struct pblk_line, list);
1371 list_del(&line->list);
1372 l_mg->nr_free_lines--;
1373
1374 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1375 if (unlikely(bit >= lm->blk_per_line)) {
1376 spin_lock(&line->lock);
1377 line->state = PBLK_LINESTATE_BAD;
Hans Holmbergf2937232018-10-09 13:11:53 +02001378 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1379 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001380 spin_unlock(&line->lock);
1381
1382 list_add_tail(&line->list, &l_mg->bad_list);
1383
Matias Bjørling4e495a42018-07-13 10:48:42 +02001384 pblk_debug(pblk, "line %d is bad\n", line->id);
Javier González588726d32017-06-26 11:57:29 +02001385 goto retry;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001386 }
1387
Javier González588726d32017-06-26 11:57:29 +02001388 ret = pblk_line_prepare(pblk, line);
1389 if (ret) {
Javier González1d8b33e2018-06-01 15:04:16 +02001390 switch (ret) {
1391 case -EAGAIN:
1392 list_add(&line->list, &l_mg->bad_list);
1393 goto retry;
1394 case -EINTR:
Javier González588726d32017-06-26 11:57:29 +02001395 list_add(&line->list, &l_mg->corrupt_list);
1396 goto retry;
Javier González1d8b33e2018-06-01 15:04:16 +02001397 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +02001398 pblk_err(pblk, "failed to prepare line %d\n", line->id);
Javier González588726d32017-06-26 11:57:29 +02001399 list_add(&line->list, &l_mg->free_list);
1400 l_mg->nr_free_lines++;
1401 return NULL;
1402 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001403 }
1404
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001405 return line;
1406}
1407
1408static struct pblk_line *pblk_line_retry(struct pblk *pblk,
1409 struct pblk_line *line)
1410{
1411 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1412 struct pblk_line *retry_line;
1413
Javier González588726d32017-06-26 11:57:29 +02001414retry:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001415 spin_lock(&l_mg->free_lock);
1416 retry_line = pblk_line_get(pblk);
1417 if (!retry_line) {
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001418 l_mg->data_line = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001419 spin_unlock(&l_mg->free_lock);
1420 return NULL;
1421 }
1422
Javier González9cfd5a92018-06-01 16:41:14 +02001423 retry_line->map_bitmap = line->map_bitmap;
1424 retry_line->invalid_bitmap = line->invalid_bitmap;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001425 retry_line->smeta = line->smeta;
1426 retry_line->emeta = line->emeta;
1427 retry_line->meta_line = line->meta_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001428
Javier González9cfd5a92018-06-01 16:41:14 +02001429 pblk_line_reinit(line);
1430
Javier González3dc001f2017-04-22 01:32:45 +02001431 l_mg->data_line = retry_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001432 spin_unlock(&l_mg->free_lock);
1433
Javier Gonzáleza7689932018-01-05 14:16:13 +01001434 pblk_rl_free_lines_dec(&pblk->rl, line, false);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001435
Javier González588726d32017-06-26 11:57:29 +02001436 if (pblk_line_erase(pblk, retry_line))
1437 goto retry;
1438
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001439 return retry_line;
1440}
1441
Javier González588726d32017-06-26 11:57:29 +02001442static void pblk_set_space_limit(struct pblk *pblk)
1443{
1444 struct pblk_rl *rl = &pblk->rl;
1445
1446 atomic_set(&rl->rb_space, 0);
1447}
1448
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001449struct pblk_line *pblk_line_get_first_data(struct pblk *pblk)
1450{
1451 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1452 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001453
1454 spin_lock(&l_mg->free_lock);
1455 line = pblk_line_get(pblk);
1456 if (!line) {
1457 spin_unlock(&l_mg->free_lock);
1458 return NULL;
1459 }
1460
1461 line->seq_nr = l_mg->d_seq_nr++;
1462 line->type = PBLK_LINETYPE_DATA;
1463 l_mg->data_line = line;
1464
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001465 pblk_line_setup_metadata(line, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001466
1467 /* Allocate next line for preparation */
1468 l_mg->data_next = pblk_line_get(pblk);
Javier González588726d32017-06-26 11:57:29 +02001469 if (!l_mg->data_next) {
1470 /* If we cannot get a new line, we need to stop the pipeline.
1471 * Only allow as many writes in as we can store safely and then
1472 * fail gracefully
1473 */
1474 pblk_set_space_limit(pblk);
1475
1476 l_mg->data_next = NULL;
1477 } else {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001478 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1479 l_mg->data_next->type = PBLK_LINETYPE_DATA;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001480 }
1481 spin_unlock(&l_mg->free_lock);
1482
Javier González9cfd5a92018-06-01 16:41:14 +02001483 if (pblk_line_alloc_bitmaps(pblk, line))
1484 return NULL;
1485
Javier González588726d32017-06-26 11:57:29 +02001486 if (pblk_line_erase(pblk, line)) {
1487 line = pblk_line_retry(pblk, line);
1488 if (!line)
1489 return NULL;
1490 }
1491
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001492retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001493 if (!pblk_line_init_metadata(pblk, line, NULL)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001494 line = pblk_line_retry(pblk, line);
1495 if (!line)
1496 return NULL;
1497
1498 goto retry_setup;
1499 }
1500
1501 if (!pblk_line_init_bb(pblk, line, 1)) {
1502 line = pblk_line_retry(pblk, line);
1503 if (!line)
1504 return NULL;
1505
1506 goto retry_setup;
1507 }
1508
Javier Gonzáleza7689932018-01-05 14:16:13 +01001509 pblk_rl_free_lines_dec(&pblk->rl, line, true);
1510
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001511 return line;
1512}
1513
Matias Bjørlingae14cc02018-10-09 13:11:40 +02001514void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa)
1515{
1516 struct pblk_line *line;
1517
Javier Gonzálezcb216652018-10-09 13:11:42 +02001518 line = pblk_ppa_to_line(pblk, ppa);
Matias Bjørlingae14cc02018-10-09 13:11:40 +02001519 kref_put(&line->ref, pblk_line_put_wq);
1520}
1521
1522void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd)
1523{
1524 struct ppa_addr *ppa_list;
1525 int i;
1526
1527 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1528
1529 for (i = 0; i < rqd->nr_ppas; i++)
1530 pblk_ppa_to_line_put(pblk, ppa_list[i]);
1531}
1532
Javier González588726d32017-06-26 11:57:29 +02001533static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line)
1534{
1535 lockdep_assert_held(&pblk->l_mg.free_lock);
1536
1537 pblk_set_space_limit(pblk);
1538 pblk->state = PBLK_STATE_STOPPING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001539 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Javier González588726d32017-06-26 11:57:29 +02001540}
1541
Javier González8bd40022017-10-13 14:46:44 +02001542static void pblk_line_close_meta_sync(struct pblk *pblk)
1543{
1544 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1545 struct pblk_line_meta *lm = &pblk->lm;
1546 struct pblk_line *line, *tline;
1547 LIST_HEAD(list);
1548
1549 spin_lock(&l_mg->close_lock);
1550 if (list_empty(&l_mg->emeta_list)) {
1551 spin_unlock(&l_mg->close_lock);
1552 return;
1553 }
1554
1555 list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev);
1556 spin_unlock(&l_mg->close_lock);
1557
1558 list_for_each_entry_safe(line, tline, &list, list) {
1559 struct pblk_emeta *emeta = line->emeta;
1560
1561 while (emeta->mem < lm->emeta_len[0]) {
1562 int ret;
1563
1564 ret = pblk_submit_meta_io(pblk, line);
1565 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001566 pblk_err(pblk, "sync meta line %d failed (%d)\n",
Javier González8bd40022017-10-13 14:46:44 +02001567 line->id, ret);
1568 return;
1569 }
1570 }
1571 }
1572
1573 pblk_wait_for_meta(pblk);
1574 flush_workqueue(pblk->close_wq);
1575}
1576
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001577void __pblk_pipeline_flush(struct pblk *pblk)
Javier González588726d32017-06-26 11:57:29 +02001578{
1579 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1580 int ret;
1581
1582 spin_lock(&l_mg->free_lock);
1583 if (pblk->state == PBLK_STATE_RECOVERING ||
1584 pblk->state == PBLK_STATE_STOPPED) {
1585 spin_unlock(&l_mg->free_lock);
1586 return;
1587 }
1588 pblk->state = PBLK_STATE_RECOVERING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001589 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Javier González588726d32017-06-26 11:57:29 +02001590 spin_unlock(&l_mg->free_lock);
1591
1592 pblk_flush_writer(pblk);
1593 pblk_wait_for_meta(pblk);
1594
1595 ret = pblk_recov_pad(pblk);
1596 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001597 pblk_err(pblk, "could not close data on teardown(%d)\n", ret);
Javier González588726d32017-06-26 11:57:29 +02001598 return;
1599 }
1600
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001601 flush_workqueue(pblk->bb_wq);
Javier González588726d32017-06-26 11:57:29 +02001602 pblk_line_close_meta_sync(pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001603}
1604
1605void __pblk_pipeline_stop(struct pblk *pblk)
1606{
1607 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González588726d32017-06-26 11:57:29 +02001608
1609 spin_lock(&l_mg->free_lock);
1610 pblk->state = PBLK_STATE_STOPPED;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001611 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Javier González588726d32017-06-26 11:57:29 +02001612 l_mg->data_line = NULL;
1613 l_mg->data_next = NULL;
1614 spin_unlock(&l_mg->free_lock);
1615}
1616
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001617void pblk_pipeline_stop(struct pblk *pblk)
1618{
1619 __pblk_pipeline_flush(pblk);
1620 __pblk_pipeline_stop(pblk);
1621}
1622
Javier González21d22872017-10-13 14:46:26 +02001623struct pblk_line *pblk_line_replace_data(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001624{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001625 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González21d22872017-10-13 14:46:26 +02001626 struct pblk_line *cur, *new = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001627 unsigned int left_seblks;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001628
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001629 new = l_mg->data_next;
1630 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001631 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001632
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001633 spin_lock(&l_mg->free_lock);
Javier González44cdbdc2018-10-09 13:12:07 +02001634 cur = l_mg->data_line;
1635 l_mg->data_line = new;
1636
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001637 pblk_line_setup_metadata(new, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001638 spin_unlock(&l_mg->free_lock);
1639
Javier González588726d32017-06-26 11:57:29 +02001640retry_erase:
1641 left_seblks = atomic_read(&new->left_seblks);
1642 if (left_seblks) {
1643 /* If line is not fully erased, erase it */
1644 if (atomic_read(&new->left_eblks)) {
1645 if (pblk_line_erase(pblk, new))
Javier González21d22872017-10-13 14:46:26 +02001646 goto out;
Javier González588726d32017-06-26 11:57:29 +02001647 } else {
1648 io_schedule();
1649 }
1650 goto retry_erase;
1651 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001652
Javier González9cfd5a92018-06-01 16:41:14 +02001653 if (pblk_line_alloc_bitmaps(pblk, new))
1654 return NULL;
1655
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001656retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001657 if (!pblk_line_init_metadata(pblk, new, cur)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001658 new = pblk_line_retry(pblk, new);
Javier Gonzálezf3236cef2017-04-22 01:32:46 +02001659 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001660 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001661
1662 goto retry_setup;
1663 }
1664
1665 if (!pblk_line_init_bb(pblk, new, 1)) {
1666 new = pblk_line_retry(pblk, new);
1667 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001668 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001669
1670 goto retry_setup;
1671 }
1672
Javier Gonzáleza7689932018-01-05 14:16:13 +01001673 pblk_rl_free_lines_dec(&pblk->rl, new, true);
1674
Javier González588726d32017-06-26 11:57:29 +02001675 /* Allocate next line for preparation */
1676 spin_lock(&l_mg->free_lock);
1677 l_mg->data_next = pblk_line_get(pblk);
1678 if (!l_mg->data_next) {
1679 /* If we cannot get a new line, we need to stop the pipeline.
1680 * Only allow as many writes in as we can store safely and then
1681 * fail gracefully
1682 */
1683 pblk_stop_writes(pblk, new);
1684 l_mg->data_next = NULL;
1685 } else {
1686 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1687 l_mg->data_next->type = PBLK_LINETYPE_DATA;
Javier González588726d32017-06-26 11:57:29 +02001688 }
1689 spin_unlock(&l_mg->free_lock);
1690
Javier González21d22872017-10-13 14:46:26 +02001691out:
1692 return new;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001693}
1694
Javier González7bd4d372017-10-13 14:46:23 +02001695static void __pblk_line_put(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001696{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001697 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +02001698 struct pblk_gc *gc = &pblk->gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001699
1700 spin_lock(&line->lock);
1701 WARN_ON(line->state != PBLK_LINESTATE_GC);
1702 line->state = PBLK_LINESTATE_FREE;
Hans Holmbergf2937232018-10-09 13:11:53 +02001703 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1704 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001705 line->gc_group = PBLK_LINEGC_NONE;
Javier González8e55c072018-06-01 15:04:22 +02001706 pblk_line_free(line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001707
Hans Holmberg48b8d202018-06-01 16:41:06 +02001708 if (line->w_err_gc->has_write_err) {
1709 pblk_rl_werr_line_out(&pblk->rl);
1710 line->w_err_gc->has_write_err = 0;
1711 }
1712
1713 spin_unlock(&line->lock);
Hans Holmbergd6b992f2017-10-13 14:46:41 +02001714 atomic_dec(&gc->pipeline_gc);
1715
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001716 spin_lock(&l_mg->free_lock);
1717 list_add_tail(&line->list, &l_mg->free_list);
1718 l_mg->nr_free_lines++;
1719 spin_unlock(&l_mg->free_lock);
1720
1721 pblk_rl_free_lines_inc(&pblk->rl, line);
1722}
1723
Javier González7bd4d372017-10-13 14:46:23 +02001724static void pblk_line_put_ws(struct work_struct *work)
1725{
1726 struct pblk_line_ws *line_put_ws = container_of(work,
1727 struct pblk_line_ws, ws);
1728 struct pblk *pblk = line_put_ws->pblk;
1729 struct pblk_line *line = line_put_ws->line;
1730
1731 __pblk_line_put(pblk, line);
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001732 mempool_free(line_put_ws, &pblk->gen_ws_pool);
Javier González7bd4d372017-10-13 14:46:23 +02001733}
1734
1735void pblk_line_put(struct kref *ref)
1736{
1737 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1738 struct pblk *pblk = line->pblk;
1739
1740 __pblk_line_put(pblk, line);
1741}
1742
1743void pblk_line_put_wq(struct kref *ref)
1744{
1745 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1746 struct pblk *pblk = line->pblk;
1747 struct pblk_line_ws *line_put_ws;
1748
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001749 line_put_ws = mempool_alloc(&pblk->gen_ws_pool, GFP_ATOMIC);
Javier González7bd4d372017-10-13 14:46:23 +02001750 if (!line_put_ws)
1751 return;
1752
1753 line_put_ws->pblk = pblk;
1754 line_put_ws->line = line;
1755 line_put_ws->priv = NULL;
1756
1757 INIT_WORK(&line_put_ws->ws, pblk_line_put_ws);
1758 queue_work(pblk->r_end_wq, &line_put_ws->ws);
1759}
1760
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001761int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
1762{
1763 struct nvm_rq *rqd;
1764 int err;
1765
Javier González67bf26a2017-10-13 14:46:20 +02001766 rqd = pblk_alloc_rqd(pblk, PBLK_ERASE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001767
1768 pblk_setup_e_rq(pblk, rqd, ppa);
1769
1770 rqd->end_io = pblk_end_io_erase;
1771 rqd->private = pblk;
1772
Hans Holmberg4209c312018-10-09 13:11:55 +02001773 trace_pblk_chunk_reset(pblk_disk_name(pblk),
1774 &ppa, PBLK_CHUNK_RESET_START);
1775
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001776 /* The write thread schedules erases so that it minimizes disturbances
1777 * with writes. Thus, there is no need to take the LUN semaphore.
1778 */
1779 err = pblk_submit_io(pblk, rqd);
1780 if (err) {
1781 struct nvm_tgt_dev *dev = pblk->dev;
1782 struct nvm_geo *geo = &dev->geo;
1783
Matias Bjørling4e495a42018-07-13 10:48:42 +02001784 pblk_err(pblk, "could not async erase line:%d,blk:%d\n",
Javier Gonzálezcb216652018-10-09 13:11:42 +02001785 pblk_ppa_to_line_id(ppa),
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001786 pblk_ppa_to_pos(geo, ppa));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001787 }
1788
1789 return err;
1790}
1791
1792struct pblk_line *pblk_line_get_data(struct pblk *pblk)
1793{
1794 return pblk->l_mg.data_line;
1795}
1796
Javier Gonzálezd624f372017-06-26 11:57:15 +02001797/* For now, always erase next line */
1798struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001799{
1800 return pblk->l_mg.data_next;
1801}
1802
1803int pblk_line_is_full(struct pblk_line *line)
1804{
1805 return (line->left_msecs == 0);
1806}
1807
Javier González588726d32017-06-26 11:57:29 +02001808static void pblk_line_should_sync_meta(struct pblk *pblk)
1809{
1810 if (pblk_rl_is_limit(&pblk->rl))
1811 pblk_line_close_meta_sync(pblk);
1812}
1813
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001814void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
1815{
Javier González32ef9412018-03-30 00:05:20 +02001816 struct nvm_tgt_dev *dev = pblk->dev;
1817 struct nvm_geo *geo = &dev->geo;
1818 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001819 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1820 struct list_head *move_list;
Javier González32ef9412018-03-30 00:05:20 +02001821 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001822
Matias Bjørling880eda52018-07-13 10:48:37 +02001823#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001824 WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001825 "pblk: corrupt closed line %d\n", line->id);
Javier Gonzáleza84ebb82017-06-30 17:56:43 +02001826#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001827
1828 spin_lock(&l_mg->free_lock);
1829 WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
1830 spin_unlock(&l_mg->free_lock);
1831
1832 spin_lock(&l_mg->gc_lock);
1833 spin_lock(&line->lock);
1834 WARN_ON(line->state != PBLK_LINESTATE_OPEN);
1835 line->state = PBLK_LINESTATE_CLOSED;
1836 move_list = pblk_line_gc_list(pblk, line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001837 list_add_tail(&line->list, move_list);
1838
Hans Holmberg53d82db2018-10-09 13:11:47 +02001839 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001840 line->map_bitmap = NULL;
1841 line->smeta = NULL;
1842 line->emeta = NULL;
1843
Javier González32ef9412018-03-30 00:05:20 +02001844 for (i = 0; i < lm->blk_per_line; i++) {
1845 struct pblk_lun *rlun = &pblk->luns[i];
1846 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
1847 int state = line->chks[pos].state;
1848
1849 if (!(state & NVM_CHK_ST_OFFLINE))
1850 state = NVM_CHK_ST_CLOSED;
1851 }
1852
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001853 spin_unlock(&line->lock);
1854 spin_unlock(&l_mg->gc_lock);
Hans Holmbergf2937232018-10-09 13:11:53 +02001855
1856 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1857 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001858}
1859
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001860void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
1861{
1862 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1863 struct pblk_line_meta *lm = &pblk->lm;
1864 struct pblk_emeta *emeta = line->emeta;
1865 struct line_emeta *emeta_buf = emeta->buf;
Hans Holmberg76758392018-03-30 00:04:52 +02001866 struct wa_counters *wa = emeta_to_wa(lm, emeta_buf);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001867
Javier González588726d32017-06-26 11:57:29 +02001868 /* No need for exact vsc value; avoid a big line lock and take aprox. */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001869 memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
1870 memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
1871
Hans Holmberg76758392018-03-30 00:04:52 +02001872 wa->user = cpu_to_le64(atomic64_read(&pblk->user_wa));
1873 wa->pad = cpu_to_le64(atomic64_read(&pblk->pad_wa));
1874 wa->gc = cpu_to_le64(atomic64_read(&pblk->gc_wa));
1875
Javier González9cc85bc2018-10-09 13:11:45 +02001876 if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC) {
1877 emeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
Andy Shevchenko7e0a0842019-02-11 13:25:05 +01001878 guid_copy((guid_t *)&emeta_buf->header.uuid,
1879 &pblk->instance_uuid);
Javier González9cc85bc2018-10-09 13:11:45 +02001880 emeta_buf->header.id = cpu_to_le32(line->id);
1881 emeta_buf->header.type = cpu_to_le16(line->type);
1882 emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
1883 emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
1884 emeta_buf->header.crc = cpu_to_le32(
1885 pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
1886 }
1887
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001888 emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
1889 emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
1890
1891 spin_lock(&l_mg->close_lock);
1892 spin_lock(&line->lock);
Hans Holmberg48b8d202018-06-01 16:41:06 +02001893
1894 /* Update the in-memory start address for emeta, in case it has
1895 * shifted due to write errors
1896 */
1897 if (line->emeta_ssec != line->cur_sec)
1898 line->emeta_ssec = line->cur_sec;
1899
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001900 list_add_tail(&line->list, &l_mg->emeta_list);
1901 spin_unlock(&line->lock);
1902 spin_unlock(&l_mg->close_lock);
Javier González588726d32017-06-26 11:57:29 +02001903
1904 pblk_line_should_sync_meta(pblk);
Hans Holmberg48b8d202018-06-01 16:41:06 +02001905}
1906
1907static void pblk_save_lba_list(struct pblk *pblk, struct pblk_line *line)
1908{
1909 struct pblk_line_meta *lm = &pblk->lm;
1910 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1911 unsigned int lba_list_size = lm->emeta_len[2];
1912 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
1913 struct pblk_emeta *emeta = line->emeta;
1914
1915 w_err_gc->lba_list = pblk_malloc(lba_list_size,
1916 l_mg->emeta_alloc_type, GFP_KERNEL);
1917 memcpy(w_err_gc->lba_list, emeta_to_lbas(pblk, emeta->buf),
1918 lba_list_size);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001919}
1920
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001921void pblk_line_close_ws(struct work_struct *work)
1922{
1923 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1924 ws);
1925 struct pblk *pblk = line_ws->pblk;
1926 struct pblk_line *line = line_ws->line;
Hans Holmberg48b8d202018-06-01 16:41:06 +02001927 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
1928
1929 /* Write errors makes the emeta start address stored in smeta invalid,
1930 * so keep a copy of the lba list until we've gc'd the line
1931 */
1932 if (w_err_gc->has_write_err)
1933 pblk_save_lba_list(pblk, line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001934
1935 pblk_line_close(pblk, line);
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001936 mempool_free(line_ws, &pblk->gen_ws_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001937}
1938
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001939void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
1940 void (*work)(struct work_struct *), gfp_t gfp_mask,
Javier Gonzálezef576492017-06-26 11:57:28 +02001941 struct workqueue_struct *wq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001942{
1943 struct pblk_line_ws *line_ws;
1944
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001945 line_ws = mempool_alloc(&pblk->gen_ws_pool, gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001946
1947 line_ws->pblk = pblk;
1948 line_ws->line = line;
1949 line_ws->priv = priv;
1950
1951 INIT_WORK(&line_ws->ws, work);
Javier Gonzálezef576492017-06-26 11:57:28 +02001952 queue_work(wq, &line_ws->ws);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001953}
1954
Matias Bjørling43241cf2018-10-09 13:11:51 +02001955static void __pblk_down_chunk(struct pblk *pblk, int pos)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001956{
Javier González3eaa11e2017-07-07 21:08:52 +02001957 struct pblk_lun *rlun = &pblk->luns[pos];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001958 int ret;
1959
1960 /*
1961 * Only send one inflight I/O per LUN. Since we map at a page
1962 * granurality, all ppas in the I/O will map to the same LUN
1963 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001964
Javier González3eaa11e2017-07-07 21:08:52 +02001965 ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000));
Rakesh Panditc5493842017-10-13 14:45:58 +02001966 if (ret == -ETIME || ret == -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001967 pblk_err(pblk, "taking lun semaphore timed out: err %d\n",
1968 -ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001969}
1970
Matias Bjørling43241cf2018-10-09 13:11:51 +02001971void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa)
Javier González3eaa11e2017-07-07 21:08:52 +02001972{
1973 struct nvm_tgt_dev *dev = pblk->dev;
1974 struct nvm_geo *geo = &dev->geo;
Matias Bjørling43241cf2018-10-09 13:11:51 +02001975 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González3eaa11e2017-07-07 21:08:52 +02001976
Matias Bjørling43241cf2018-10-09 13:11:51 +02001977 __pblk_down_chunk(pblk, pos);
Javier González3eaa11e2017-07-07 21:08:52 +02001978}
1979
Matias Bjørling43241cf2018-10-09 13:11:51 +02001980void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa,
Javier González3eaa11e2017-07-07 21:08:52 +02001981 unsigned long *lun_bitmap)
1982{
1983 struct nvm_tgt_dev *dev = pblk->dev;
1984 struct nvm_geo *geo = &dev->geo;
Matias Bjørling43241cf2018-10-09 13:11:51 +02001985 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González3eaa11e2017-07-07 21:08:52 +02001986
1987 /* If the LUN has been locked for this same request, do no attempt to
1988 * lock it again
1989 */
1990 if (test_and_set_bit(pos, lun_bitmap))
1991 return;
1992
Matias Bjørling43241cf2018-10-09 13:11:51 +02001993 __pblk_down_chunk(pblk, pos);
Javier González3eaa11e2017-07-07 21:08:52 +02001994}
1995
Matias Bjørling43241cf2018-10-09 13:11:51 +02001996void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa)
Javier González3eaa11e2017-07-07 21:08:52 +02001997{
1998 struct nvm_tgt_dev *dev = pblk->dev;
1999 struct nvm_geo *geo = &dev->geo;
2000 struct pblk_lun *rlun;
Matias Bjørling43241cf2018-10-09 13:11:51 +02002001 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González3eaa11e2017-07-07 21:08:52 +02002002
2003 rlun = &pblk->luns[pos];
2004 up(&rlun->wr_sem);
2005}
2006
Hans Holmberge99e8022018-10-09 13:11:48 +02002007void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002008{
2009 struct nvm_tgt_dev *dev = pblk->dev;
2010 struct nvm_geo *geo = &dev->geo;
2011 struct pblk_lun *rlun;
Javier Gonzáleza40afad2018-03-30 00:05:14 +02002012 int num_lun = geo->all_luns;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002013 int bit = -1;
2014
Javier Gonzáleza40afad2018-03-30 00:05:14 +02002015 while ((bit = find_next_bit(lun_bitmap, num_lun, bit + 1)) < num_lun) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002016 rlun = &pblk->luns[bit];
2017 up(&rlun->wr_sem);
2018 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002019}
2020
2021void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
2022{
Javier González9f6cb132017-10-13 14:46:12 +02002023 struct ppa_addr ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002024
2025 /* logic error: lba out-of-bounds. Ignore update */
2026 if (!(lba < pblk->rl.nr_secs)) {
2027 WARN(1, "pblk: corrupted L2P map request\n");
2028 return;
2029 }
2030
2031 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02002032 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002033
Javier González9f6cb132017-10-13 14:46:12 +02002034 if (!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p))
2035 pblk_map_invalidate(pblk, ppa_l2p);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002036
2037 pblk_trans_map_set(pblk, lba, ppa);
2038 spin_unlock(&pblk->trans_lock);
2039}
2040
2041void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
2042{
Javier Gonzálezd3401212017-10-13 14:46:14 +02002043
Matias Bjørling880eda52018-07-13 10:48:37 +02002044#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002045 /* Callers must ensure that the ppa points to a cache address */
2046 BUG_ON(!pblk_addr_in_cache(ppa));
2047 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
2048#endif
2049
2050 pblk_update_map(pblk, lba, ppa);
2051}
2052
Javier González9f6cb132017-10-13 14:46:12 +02002053int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa_new,
Javier Gonzálezd3401212017-10-13 14:46:14 +02002054 struct pblk_line *gc_line, u64 paddr_gc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002055{
Javier Gonzálezd3401212017-10-13 14:46:14 +02002056 struct ppa_addr ppa_l2p, ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002057 int ret = 1;
2058
Matias Bjørling880eda52018-07-13 10:48:37 +02002059#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002060 /* Callers must ensure that the ppa points to a cache address */
Javier González9f6cb132017-10-13 14:46:12 +02002061 BUG_ON(!pblk_addr_in_cache(ppa_new));
2062 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa_new)));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002063#endif
2064
2065 /* logic error: lba out-of-bounds. Ignore update */
2066 if (!(lba < pblk->rl.nr_secs)) {
2067 WARN(1, "pblk: corrupted L2P map request\n");
2068 return 0;
2069 }
2070
2071 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02002072 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzálezd3401212017-10-13 14:46:14 +02002073 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, gc_line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002074
Javier Gonzálezd3401212017-10-13 14:46:14 +02002075 if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) {
2076 spin_lock(&gc_line->lock);
2077 WARN(!test_bit(paddr_gc, gc_line->invalid_bitmap),
2078 "pblk: corrupted GC update");
2079 spin_unlock(&gc_line->lock);
Javier González9f6cb132017-10-13 14:46:12 +02002080
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002081 ret = 0;
2082 goto out;
2083 }
2084
Javier González9f6cb132017-10-13 14:46:12 +02002085 pblk_trans_map_set(pblk, lba, ppa_new);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002086out:
2087 spin_unlock(&pblk->trans_lock);
2088 return ret;
2089}
2090
Javier González9f6cb132017-10-13 14:46:12 +02002091void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
2092 struct ppa_addr ppa_mapped, struct ppa_addr ppa_cache)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002093{
Javier González9f6cb132017-10-13 14:46:12 +02002094 struct ppa_addr ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002095
Matias Bjørling880eda52018-07-13 10:48:37 +02002096#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002097 /* Callers must ensure that the ppa points to a device address */
Javier González9f6cb132017-10-13 14:46:12 +02002098 BUG_ON(pblk_addr_in_cache(ppa_mapped));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002099#endif
2100 /* Invalidate and discard padded entries */
2101 if (lba == ADDR_EMPTY) {
Hans Holmberg76758392018-03-30 00:04:52 +02002102 atomic64_inc(&pblk->pad_wa);
Matias Bjørling880eda52018-07-13 10:48:37 +02002103#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002104 atomic_long_inc(&pblk->padded_wb);
2105#endif
Javier González9f6cb132017-10-13 14:46:12 +02002106 if (!pblk_ppa_empty(ppa_mapped))
2107 pblk_map_invalidate(pblk, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002108 return;
2109 }
2110
2111 /* logic error: lba out-of-bounds. Ignore update */
2112 if (!(lba < pblk->rl.nr_secs)) {
2113 WARN(1, "pblk: corrupted L2P map request\n");
2114 return;
2115 }
2116
2117 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02002118 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002119
2120 /* Do not update L2P if the cacheline has been updated. In this case,
2121 * the mapped ppa must be invalidated
2122 */
Javier González9f6cb132017-10-13 14:46:12 +02002123 if (!pblk_ppa_comp(ppa_l2p, ppa_cache)) {
2124 if (!pblk_ppa_empty(ppa_mapped))
2125 pblk_map_invalidate(pblk, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002126 goto out;
2127 }
2128
Matias Bjørling880eda52018-07-13 10:48:37 +02002129#ifdef CONFIG_NVM_PBLK_DEBUG
Javier González9f6cb132017-10-13 14:46:12 +02002130 WARN_ON(!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002131#endif
2132
Javier González9f6cb132017-10-13 14:46:12 +02002133 pblk_trans_map_set(pblk, lba, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002134out:
2135 spin_unlock(&pblk->trans_lock);
2136}
2137
2138void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
2139 sector_t blba, int nr_secs)
2140{
2141 int i;
2142
2143 spin_lock(&pblk->trans_lock);
Javier González7bd4d372017-10-13 14:46:23 +02002144 for (i = 0; i < nr_secs; i++) {
2145 struct ppa_addr ppa;
2146
2147 ppa = ppas[i] = pblk_trans_map_get(pblk, blba + i);
2148
2149 /* If the L2P entry maps to a line, the reference is valid */
2150 if (!pblk_ppa_empty(ppa) && !pblk_addr_in_cache(ppa)) {
Javier Gonzálezcb216652018-10-09 13:11:42 +02002151 struct pblk_line *line = pblk_ppa_to_line(pblk, ppa);
Javier González7bd4d372017-10-13 14:46:23 +02002152
2153 kref_get(&line->ref);
2154 }
2155 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002156 spin_unlock(&pblk->trans_lock);
2157}
2158
2159void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
2160 u64 *lba_list, int nr_secs)
2161{
Javier Gonzálezd3401212017-10-13 14:46:14 +02002162 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002163 int i;
2164
2165 spin_lock(&pblk->trans_lock);
2166 for (i = 0; i < nr_secs; i++) {
2167 lba = lba_list[i];
Javier Gonzálezd3401212017-10-13 14:46:14 +02002168 if (lba != ADDR_EMPTY) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002169 /* logic error: lba out-of-bounds. Ignore update */
2170 if (!(lba < pblk->rl.nr_secs)) {
2171 WARN(1, "pblk: corrupted L2P map request\n");
2172 continue;
2173 }
2174 ppas[i] = pblk_trans_map_get(pblk, lba);
2175 }
2176 }
2177 spin_unlock(&pblk->trans_lock);
2178}
Igor Konopko55d8ec32018-12-11 20:16:26 +01002179
2180void *pblk_get_meta_for_writes(struct pblk *pblk, struct nvm_rq *rqd)
2181{
2182 void *buffer;
2183
2184 if (pblk_is_oob_meta_supported(pblk)) {
2185 /* Just use OOB metadata buffer as always */
2186 buffer = rqd->meta_list;
2187 } else {
2188 /* We need to reuse last page of request (packed metadata)
2189 * in similar way as traditional oob metadata
2190 */
2191 buffer = page_to_virt(
2192 rqd->bio->bi_io_vec[rqd->bio->bi_vcnt - 1].bv_page);
2193 }
2194
2195 return buffer;
2196}
2197
2198void pblk_get_packed_meta(struct pblk *pblk, struct nvm_rq *rqd)
2199{
2200 void *meta_list = rqd->meta_list;
2201 void *page;
2202 int i = 0;
2203
2204 if (pblk_is_oob_meta_supported(pblk))
2205 return;
2206
2207 page = page_to_virt(rqd->bio->bi_io_vec[rqd->bio->bi_vcnt - 1].bv_page);
2208 /* We need to fill oob meta buffer with data from packed metadata */
2209 for (; i < rqd->nr_ppas; i++)
2210 memcpy(pblk_get_meta(pblk, meta_list, i),
2211 page + (i * sizeof(struct pblk_sec_meta)),
2212 sizeof(struct pblk_sec_meta));
2213}