kernel: update 4.0 to 4.0.5
[openwrt.git] / target / linux / brcm47xx / patches-4.0 / 031-09-MIPS-BCM47xx-Add-helper-variable-for-storing-NVRAM-l.patch
1 From f229d75f1472c4cd30f464e4a0f94f410046bd80 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
3 Date: Sat, 6 Jun 2015 23:16:23 +0200
4 Subject: [PATCH] MIPS: BCM47xx: Add helper variable for storing NVRAM length
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This simplifies code just a bit (also maybe makes it a bit more
10 intuitive?) and will allow us to stop storing header. Right now we copy
11 whole NVRAM including its header to the internal buffer. It is not
12 needed to store a header as we don't access all these details like CRC,
13 flags, etc. The next improvement that should follow is copying only the
14 real contents.
15
16 Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
17 Acked-by: Hauke Mehrtens <hauke@hauke-m.de>
18 Cc: linux-mips@linux-mips.org
19 Cc: Arend van Spriel <arend@broadcom.com>
20 Cc: Hante Meuleman <meuleman@broadcom.com>
21 Patchwork: https://patchwork.linux-mips.org/patch/10535/
22 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
23 ---
24  arch/mips/bcm47xx/nvram.c | 37 ++++++++++++++++---------------------
25  1 file changed, 16 insertions(+), 21 deletions(-)
26
27 --- a/arch/mips/bcm47xx/nvram.c
28 +++ b/arch/mips/bcm47xx/nvram.c
29 @@ -35,6 +35,7 @@ struct nvram_header {
30  };
31  
32  static char nvram_buf[NVRAM_SPACE];
33 +static size_t nvram_len;
34  static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
35  
36  static u32 find_nvram_size(void __iomem *end)
37 @@ -60,7 +61,7 @@ static int nvram_find_and_copy(void __io
38         u32 *src, *dst;
39         u32 size;
40  
41 -       if (nvram_buf[0]) {
42 +       if (nvram_len) {
43                 pr_warn("nvram already initialized\n");
44                 return -EEXIST;
45         }
46 @@ -99,17 +100,18 @@ found:
47         for (i = 0; i < sizeof(struct nvram_header); i += 4)
48                 *dst++ = __raw_readl(src++);
49         header = (struct nvram_header *)nvram_buf;
50 -       if (header->len > size) {
51 +       nvram_len = header->len;
52 +       if (nvram_len > size) {
53                 pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
54 -               header->len = size;
55 +               nvram_len = size;
56         }
57 -       if (header->len >= NVRAM_SPACE) {
58 +       if (nvram_len >= NVRAM_SPACE) {
59                 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
60                        header->len, NVRAM_SPACE - 1);
61 -               header->len = NVRAM_SPACE - 1;
62 +               nvram_len = NVRAM_SPACE - 1;
63         }
64         /* proceed reading data after header */
65 -       for (; i < header->len; i += 4)
66 +       for (; i < nvram_len; i += 4)
67                 *dst++ = readl(src++);
68         nvram_buf[NVRAM_SPACE - 1] = '\0';
69  
70 @@ -144,7 +146,6 @@ static int nvram_init(void)
71  #ifdef CONFIG_MTD
72         struct mtd_info *mtd;
73         struct nvram_header header;
74 -       struct nvram_header *pheader;
75         size_t bytes_read;
76         int err;
77  
78 @@ -155,20 +156,16 @@ static int nvram_init(void)
79         err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
80         if (!err && header.magic == NVRAM_MAGIC &&
81             header.len > sizeof(header)) {
82 -               if (header.len >= NVRAM_SPACE) {
83 +               nvram_len = header.len;
84 +               if (nvram_len >= NVRAM_SPACE) {
85                         pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
86                                 header.len, NVRAM_SPACE);
87 -                       header.len = NVRAM_SPACE - 1;
88 +                       nvram_len = NVRAM_SPACE - 1;
89                 }
90  
91 -               err = mtd_read(mtd, 0, header.len, &bytes_read,
92 +               err = mtd_read(mtd, 0, nvram_len, &nvram_len,
93                                (u8 *)nvram_buf);
94 -               if (err)
95 -                       return err;
96 -
97 -               pheader = (struct nvram_header *)nvram_buf;
98 -               pheader->len = header.len;
99 -               return 0;
100 +               return err;
101         }
102  #endif
103  
104 @@ -183,7 +180,7 @@ int bcm47xx_nvram_getenv(const char *nam
105         if (!name)
106                 return -EINVAL;
107  
108 -       if (!nvram_buf[0]) {
109 +       if (!nvram_len) {
110                 err = nvram_init();
111                 if (err)
112                         return err;
113 @@ -231,16 +228,14 @@ char *bcm47xx_nvram_get_contents(size_t
114  {
115         int err;
116         char *nvram;
117 -       struct nvram_header *header;
118  
119 -       if (!nvram_buf[0]) {
120 +       if (!nvram_len) {
121                 err = nvram_init();
122                 if (err)
123                         return NULL;
124         }
125  
126 -       header = (struct nvram_header *)nvram_buf;
127 -       *nvram_size = header->len - sizeof(struct nvram_header);
128 +       *nvram_size = nvram_len - sizeof(struct nvram_header);
129         nvram = vmalloc(*nvram_size);
130         if (!nvram)
131                 return NULL;