Vishal Verma | 9e0e252 | 2015-12-24 19:20:32 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_BADBLOCKS_H |
| 2 | #define _LINUX_BADBLOCKS_H |
| 3 | |
| 4 | #include <linux/seqlock.h> |
Dan Williams | 16263ff | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 5 | #include <linux/device.h> |
Vishal Verma | 9e0e252 | 2015-12-24 19:20:32 -0700 | [diff] [blame] | 6 | #include <linux/kernel.h> |
| 7 | #include <linux/stddef.h> |
| 8 | #include <linux/types.h> |
| 9 | |
| 10 | #define BB_LEN_MASK (0x00000000000001FFULL) |
| 11 | #define BB_OFFSET_MASK (0x7FFFFFFFFFFFFE00ULL) |
| 12 | #define BB_ACK_MASK (0x8000000000000000ULL) |
| 13 | #define BB_MAX_LEN 512 |
| 14 | #define BB_OFFSET(x) (((x) & BB_OFFSET_MASK) >> 9) |
| 15 | #define BB_LEN(x) (((x) & BB_LEN_MASK) + 1) |
| 16 | #define BB_ACK(x) (!!((x) & BB_ACK_MASK)) |
| 17 | #define BB_MAKE(a, l, ack) (((a)<<9) | ((l)-1) | ((u64)(!!(ack)) << 63)) |
| 18 | |
| 19 | /* Bad block numbers are stored sorted in a single page. |
| 20 | * 64bits is used for each block or extent. |
| 21 | * 54 bits are sector number, 9 bits are extent size, |
| 22 | * 1 bit is an 'acknowledged' flag. |
| 23 | */ |
| 24 | #define MAX_BADBLOCKS (PAGE_SIZE/8) |
| 25 | |
| 26 | struct badblocks { |
Dan Williams | 16263ff | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 27 | struct device *dev; /* set by devm_init_badblocks */ |
Vishal Verma | 9e0e252 | 2015-12-24 19:20:32 -0700 | [diff] [blame] | 28 | int count; /* count of bad blocks */ |
| 29 | int unacked_exist; /* there probably are unacknowledged |
| 30 | * bad blocks. This is only cleared |
| 31 | * when a read discovers none |
| 32 | */ |
| 33 | int shift; /* shift from sectors to block size |
| 34 | * a -ve shift means badblocks are |
| 35 | * disabled.*/ |
| 36 | u64 *page; /* badblock list */ |
| 37 | int changed; |
| 38 | seqlock_t lock; |
| 39 | sector_t sector; |
| 40 | sector_t size; /* in sectors */ |
| 41 | }; |
| 42 | |
| 43 | int badblocks_check(struct badblocks *bb, sector_t s, int sectors, |
| 44 | sector_t *first_bad, int *bad_sectors); |
| 45 | int badblocks_set(struct badblocks *bb, sector_t s, int sectors, |
| 46 | int acknowledged); |
| 47 | int badblocks_clear(struct badblocks *bb, sector_t s, int sectors); |
| 48 | void ack_all_badblocks(struct badblocks *bb); |
| 49 | ssize_t badblocks_show(struct badblocks *bb, char *page, int unack); |
| 50 | ssize_t badblocks_store(struct badblocks *bb, const char *page, size_t len, |
| 51 | int unack); |
| 52 | int badblocks_init(struct badblocks *bb, int enable); |
Dan Williams | d3b407f | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 53 | void badblocks_exit(struct badblocks *bb); |
Dan Williams | 16263ff | 2016-01-04 23:50:23 -0800 | [diff] [blame] | 54 | struct device; |
| 55 | int devm_init_badblocks(struct device *dev, struct badblocks *bb); |
| 56 | static inline void devm_exit_badblocks(struct device *dev, struct badblocks *bb) |
| 57 | { |
| 58 | if (bb->dev != dev) { |
| 59 | dev_WARN_ONCE(dev, 1, "%s: badblocks instance not associated\n", |
| 60 | __func__); |
| 61 | return; |
| 62 | } |
| 63 | badblocks_exit(bb); |
| 64 | } |
Vishal Verma | 9e0e252 | 2015-12-24 19:20:32 -0700 | [diff] [blame] | 65 | #endif |