Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Driver for Solarflare Solarstorm network controllers and boards |
| 3 | * Copyright 2005-2006 Fen Systems Ltd. |
Ben Hutchings | 0a6f40c | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4 | * Copyright 2006-2010 Solarflare Communications Inc. |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation, incorporated herein by reference. |
| 9 | */ |
| 10 | |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 12 | #include <linux/module.h> |
Ben Hutchings | 5f3f9d6 | 2011-11-04 22:29:14 +0000 | [diff] [blame^] | 13 | #undef DEBUG /* <linux/mtd/mtd.h> has its own use for DEBUG */ |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 14 | #include <linux/mtd/mtd.h> |
| 15 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 17 | #include <linux/rtnetlink.h> |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 18 | |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 19 | #include "net_driver.h" |
| 20 | #include "spi.h" |
Hannes Eder | ff2ef90 | 2009-02-18 17:49:50 -0800 | [diff] [blame] | 21 | #include "efx.h" |
Ben Hutchings | 744093c | 2009-11-29 15:12:08 +0000 | [diff] [blame] | 22 | #include "nic.h" |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 23 | #include "mcdi.h" |
| 24 | #include "mcdi_pcol.h" |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 25 | |
| 26 | #define EFX_SPI_VERIFY_BUF_LEN 16 |
| 27 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 28 | struct efx_mtd_partition { |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 29 | struct mtd_info mtd; |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 30 | union { |
| 31 | struct { |
| 32 | bool updating; |
| 33 | u8 nvram_type; |
| 34 | u16 fw_subtype; |
| 35 | } mcdi; |
| 36 | size_t offset; |
| 37 | }; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 38 | const char *type_name; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 39 | char name[IFNAMSIZ + 20]; |
| 40 | }; |
| 41 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 42 | struct efx_mtd_ops { |
| 43 | int (*read)(struct mtd_info *mtd, loff_t start, size_t len, |
| 44 | size_t *retlen, u8 *buffer); |
| 45 | int (*erase)(struct mtd_info *mtd, loff_t start, size_t len); |
| 46 | int (*write)(struct mtd_info *mtd, loff_t start, size_t len, |
| 47 | size_t *retlen, const u8 *buffer); |
| 48 | int (*sync)(struct mtd_info *mtd); |
| 49 | }; |
| 50 | |
| 51 | struct efx_mtd { |
| 52 | struct list_head node; |
| 53 | struct efx_nic *efx; |
| 54 | const struct efx_spi_device *spi; |
| 55 | const char *name; |
| 56 | const struct efx_mtd_ops *ops; |
| 57 | size_t n_parts; |
| 58 | struct efx_mtd_partition part[0]; |
| 59 | }; |
| 60 | |
| 61 | #define efx_for_each_partition(part, efx_mtd) \ |
| 62 | for ((part) = &(efx_mtd)->part[0]; \ |
| 63 | (part) != &(efx_mtd)->part[(efx_mtd)->n_parts]; \ |
| 64 | (part)++) |
| 65 | |
| 66 | #define to_efx_mtd_partition(mtd) \ |
| 67 | container_of(mtd, struct efx_mtd_partition, mtd) |
| 68 | |
| 69 | static int falcon_mtd_probe(struct efx_nic *efx); |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 70 | static int siena_mtd_probe(struct efx_nic *efx); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 71 | |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 72 | /* SPI utilities */ |
| 73 | |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 74 | static int |
| 75 | efx_spi_slow_wait(struct efx_mtd_partition *part, bool uninterruptible) |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 76 | { |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 77 | struct efx_mtd *efx_mtd = part->mtd.priv; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 78 | const struct efx_spi_device *spi = efx_mtd->spi; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 79 | struct efx_nic *efx = efx_mtd->efx; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 80 | u8 status; |
| 81 | int rc, i; |
| 82 | |
| 83 | /* Wait up to 4s for flash/EEPROM to finish a slow operation. */ |
| 84 | for (i = 0; i < 40; i++) { |
| 85 | __set_current_state(uninterruptible ? |
| 86 | TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE); |
| 87 | schedule_timeout(HZ / 10); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 88 | rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL, |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 89 | &status, sizeof(status)); |
| 90 | if (rc) |
| 91 | return rc; |
| 92 | if (!(status & SPI_STATUS_NRDY)) |
| 93 | return 0; |
| 94 | if (signal_pending(current)) |
| 95 | return -EINTR; |
| 96 | } |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 97 | pr_err("%s: timed out waiting for %s\n", part->name, efx_mtd->name); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 98 | return -ETIMEDOUT; |
| 99 | } |
| 100 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 101 | static int |
| 102 | efx_spi_unlock(struct efx_nic *efx, const struct efx_spi_device *spi) |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 103 | { |
| 104 | const u8 unlock_mask = (SPI_STATUS_BP2 | SPI_STATUS_BP1 | |
| 105 | SPI_STATUS_BP0); |
| 106 | u8 status; |
| 107 | int rc; |
| 108 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 109 | rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL, |
| 110 | &status, sizeof(status)); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 111 | if (rc) |
| 112 | return rc; |
| 113 | |
| 114 | if (!(status & unlock_mask)) |
| 115 | return 0; /* already unlocked */ |
| 116 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 117 | rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 118 | if (rc) |
| 119 | return rc; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 120 | rc = falcon_spi_cmd(efx, spi, SPI_SST_EWSR, -1, NULL, NULL, 0); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 121 | if (rc) |
| 122 | return rc; |
| 123 | |
| 124 | status &= ~unlock_mask; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 125 | rc = falcon_spi_cmd(efx, spi, SPI_WRSR, -1, &status, |
| 126 | NULL, sizeof(status)); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 127 | if (rc) |
| 128 | return rc; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 129 | rc = falcon_spi_wait_write(efx, spi); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 130 | if (rc) |
| 131 | return rc; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 136 | static int |
| 137 | efx_spi_erase(struct efx_mtd_partition *part, loff_t start, size_t len) |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 138 | { |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 139 | struct efx_mtd *efx_mtd = part->mtd.priv; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 140 | const struct efx_spi_device *spi = efx_mtd->spi; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 141 | struct efx_nic *efx = efx_mtd->efx; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 142 | unsigned pos, block_len; |
| 143 | u8 empty[EFX_SPI_VERIFY_BUF_LEN]; |
| 144 | u8 buffer[EFX_SPI_VERIFY_BUF_LEN]; |
| 145 | int rc; |
| 146 | |
| 147 | if (len != spi->erase_size) |
| 148 | return -EINVAL; |
| 149 | |
| 150 | if (spi->erase_command == 0) |
| 151 | return -EOPNOTSUPP; |
| 152 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 153 | rc = efx_spi_unlock(efx, spi); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 154 | if (rc) |
| 155 | return rc; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 156 | rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 157 | if (rc) |
| 158 | return rc; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 159 | rc = falcon_spi_cmd(efx, spi, spi->erase_command, start, NULL, |
| 160 | NULL, 0); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 161 | if (rc) |
| 162 | return rc; |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 163 | rc = efx_spi_slow_wait(part, false); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 164 | |
| 165 | /* Verify the entire region has been wiped */ |
| 166 | memset(empty, 0xff, sizeof(empty)); |
| 167 | for (pos = 0; pos < len; pos += block_len) { |
| 168 | block_len = min(len - pos, sizeof(buffer)); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 169 | rc = falcon_spi_read(efx, spi, start + pos, block_len, |
| 170 | NULL, buffer); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 171 | if (rc) |
| 172 | return rc; |
| 173 | if (memcmp(empty, buffer, block_len)) |
| 174 | return -EIO; |
| 175 | |
| 176 | /* Avoid locking up the system */ |
| 177 | cond_resched(); |
| 178 | if (signal_pending(current)) |
| 179 | return -EINTR; |
| 180 | } |
| 181 | |
| 182 | return rc; |
| 183 | } |
| 184 | |
| 185 | /* MTD interface */ |
| 186 | |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 187 | static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase) |
| 188 | { |
| 189 | struct efx_mtd *efx_mtd = mtd->priv; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 190 | int rc; |
| 191 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 192 | rc = efx_mtd->ops->erase(mtd, erase->addr, erase->len); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 193 | if (rc == 0) { |
| 194 | erase->state = MTD_ERASE_DONE; |
| 195 | } else { |
| 196 | erase->state = MTD_ERASE_FAILED; |
| 197 | erase->fail_addr = 0xffffffff; |
| 198 | } |
| 199 | mtd_erase_callback(erase); |
| 200 | return rc; |
| 201 | } |
| 202 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 203 | static void efx_mtd_sync(struct mtd_info *mtd) |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 204 | { |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 205 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 206 | struct efx_mtd *efx_mtd = mtd->priv; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 207 | int rc; |
| 208 | |
| 209 | rc = efx_mtd->ops->sync(mtd); |
| 210 | if (rc) |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 211 | pr_err("%s: %s sync failed (%d)\n", |
| 212 | part->name, efx_mtd->name, rc); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void efx_mtd_remove_partition(struct efx_mtd_partition *part) |
| 216 | { |
| 217 | int rc; |
| 218 | |
| 219 | for (;;) { |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 220 | rc = mtd_device_unregister(&part->mtd); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 221 | if (rc != -EBUSY) |
| 222 | break; |
| 223 | ssleep(1); |
| 224 | } |
| 225 | WARN_ON(rc); |
| 226 | } |
| 227 | |
| 228 | static void efx_mtd_remove_device(struct efx_mtd *efx_mtd) |
| 229 | { |
| 230 | struct efx_mtd_partition *part; |
| 231 | |
| 232 | efx_for_each_partition(part, efx_mtd) |
| 233 | efx_mtd_remove_partition(part); |
| 234 | list_del(&efx_mtd->node); |
| 235 | kfree(efx_mtd); |
| 236 | } |
| 237 | |
| 238 | static void efx_mtd_rename_device(struct efx_mtd *efx_mtd) |
| 239 | { |
| 240 | struct efx_mtd_partition *part; |
| 241 | |
| 242 | efx_for_each_partition(part, efx_mtd) |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 243 | if (efx_nic_rev(efx_mtd->efx) >= EFX_REV_SIENA_A0) |
| 244 | snprintf(part->name, sizeof(part->name), |
| 245 | "%s %s:%02x", efx_mtd->efx->name, |
| 246 | part->type_name, part->mcdi.fw_subtype); |
| 247 | else |
| 248 | snprintf(part->name, sizeof(part->name), |
| 249 | "%s %s", efx_mtd->efx->name, |
| 250 | part->type_name); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static int efx_mtd_probe_device(struct efx_nic *efx, struct efx_mtd *efx_mtd) |
| 254 | { |
| 255 | struct efx_mtd_partition *part; |
| 256 | |
| 257 | efx_mtd->efx = efx; |
| 258 | |
| 259 | efx_mtd_rename_device(efx_mtd); |
| 260 | |
| 261 | efx_for_each_partition(part, efx_mtd) { |
| 262 | part->mtd.writesize = 1; |
| 263 | |
| 264 | part->mtd.owner = THIS_MODULE; |
| 265 | part->mtd.priv = efx_mtd; |
| 266 | part->mtd.name = part->name; |
| 267 | part->mtd.erase = efx_mtd_erase; |
| 268 | part->mtd.read = efx_mtd->ops->read; |
| 269 | part->mtd.write = efx_mtd->ops->write; |
| 270 | part->mtd.sync = efx_mtd_sync; |
| 271 | |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 272 | if (mtd_device_register(&part->mtd, NULL, 0)) |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 273 | goto fail; |
| 274 | } |
| 275 | |
| 276 | list_add(&efx_mtd->node, &efx->mtd_list); |
| 277 | return 0; |
| 278 | |
| 279 | fail: |
| 280 | while (part != &efx_mtd->part[0]) { |
| 281 | --part; |
| 282 | efx_mtd_remove_partition(part); |
| 283 | } |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 284 | /* mtd_device_register() returns 1 if the MTD table is full */ |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 285 | return -ENOMEM; |
| 286 | } |
| 287 | |
| 288 | void efx_mtd_remove(struct efx_nic *efx) |
| 289 | { |
| 290 | struct efx_mtd *efx_mtd, *next; |
| 291 | |
| 292 | WARN_ON(efx_dev_registered(efx)); |
| 293 | |
| 294 | list_for_each_entry_safe(efx_mtd, next, &efx->mtd_list, node) |
| 295 | efx_mtd_remove_device(efx_mtd); |
| 296 | } |
| 297 | |
| 298 | void efx_mtd_rename(struct efx_nic *efx) |
| 299 | { |
| 300 | struct efx_mtd *efx_mtd; |
| 301 | |
| 302 | ASSERT_RTNL(); |
| 303 | |
| 304 | list_for_each_entry(efx_mtd, &efx->mtd_list, node) |
| 305 | efx_mtd_rename_device(efx_mtd); |
| 306 | } |
| 307 | |
| 308 | int efx_mtd_probe(struct efx_nic *efx) |
| 309 | { |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 310 | if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) |
| 311 | return siena_mtd_probe(efx); |
| 312 | else |
| 313 | return falcon_mtd_probe(efx); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* Implementation of MTD operations for Falcon */ |
| 317 | |
| 318 | static int falcon_mtd_read(struct mtd_info *mtd, loff_t start, |
| 319 | size_t len, size_t *retlen, u8 *buffer) |
| 320 | { |
| 321 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
| 322 | struct efx_mtd *efx_mtd = mtd->priv; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 323 | const struct efx_spi_device *spi = efx_mtd->spi; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 324 | struct efx_nic *efx = efx_mtd->efx; |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 325 | struct falcon_nic_data *nic_data = efx->nic_data; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 326 | int rc; |
| 327 | |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 328 | rc = mutex_lock_interruptible(&nic_data->spi_lock); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 329 | if (rc) |
| 330 | return rc; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 331 | rc = falcon_spi_read(efx, spi, part->offset + start, len, |
| 332 | retlen, buffer); |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 333 | mutex_unlock(&nic_data->spi_lock); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 334 | return rc; |
| 335 | } |
| 336 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 337 | static int falcon_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len) |
| 338 | { |
| 339 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
| 340 | struct efx_mtd *efx_mtd = mtd->priv; |
| 341 | struct efx_nic *efx = efx_mtd->efx; |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 342 | struct falcon_nic_data *nic_data = efx->nic_data; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 343 | int rc; |
| 344 | |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 345 | rc = mutex_lock_interruptible(&nic_data->spi_lock); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 346 | if (rc) |
| 347 | return rc; |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 348 | rc = efx_spi_erase(part, part->offset + start, len); |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 349 | mutex_unlock(&nic_data->spi_lock); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 350 | return rc; |
| 351 | } |
| 352 | |
| 353 | static int falcon_mtd_write(struct mtd_info *mtd, loff_t start, |
| 354 | size_t len, size_t *retlen, const u8 *buffer) |
| 355 | { |
| 356 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
| 357 | struct efx_mtd *efx_mtd = mtd->priv; |
| 358 | const struct efx_spi_device *spi = efx_mtd->spi; |
| 359 | struct efx_nic *efx = efx_mtd->efx; |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 360 | struct falcon_nic_data *nic_data = efx->nic_data; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 361 | int rc; |
| 362 | |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 363 | rc = mutex_lock_interruptible(&nic_data->spi_lock); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 364 | if (rc) |
| 365 | return rc; |
| 366 | rc = falcon_spi_write(efx, spi, part->offset + start, len, |
| 367 | retlen, buffer); |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 368 | mutex_unlock(&nic_data->spi_lock); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 369 | return rc; |
| 370 | } |
| 371 | |
| 372 | static int falcon_mtd_sync(struct mtd_info *mtd) |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 373 | { |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 374 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 375 | struct efx_mtd *efx_mtd = mtd->priv; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 376 | struct efx_nic *efx = efx_mtd->efx; |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 377 | struct falcon_nic_data *nic_data = efx->nic_data; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 378 | int rc; |
| 379 | |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 380 | mutex_lock(&nic_data->spi_lock); |
Ben Hutchings | 0c605a2 | 2010-06-23 11:29:24 +0000 | [diff] [blame] | 381 | rc = efx_spi_slow_wait(part, true); |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 382 | mutex_unlock(&nic_data->spi_lock); |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 383 | return rc; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Ben Hutchings | 18e83e4 | 2012-01-05 19:05:20 +0000 | [diff] [blame] | 386 | static const struct efx_mtd_ops falcon_mtd_ops = { |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 387 | .read = falcon_mtd_read, |
| 388 | .erase = falcon_mtd_erase, |
| 389 | .write = falcon_mtd_write, |
| 390 | .sync = falcon_mtd_sync, |
| 391 | }; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 392 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 393 | static int falcon_mtd_probe(struct efx_nic *efx) |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 394 | { |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 395 | struct falcon_nic_data *nic_data = efx->nic_data; |
Ben Hutchings | 6a8872c | 2010-12-02 13:47:10 +0000 | [diff] [blame] | 396 | struct efx_spi_device *spi; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 397 | struct efx_mtd *efx_mtd; |
Ben Hutchings | 6a8872c | 2010-12-02 13:47:10 +0000 | [diff] [blame] | 398 | int rc = -ENODEV; |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 399 | |
| 400 | ASSERT_RTNL(); |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 401 | |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 402 | spi = &nic_data->spi_flash; |
| 403 | if (efx_spi_present(spi) && spi->size > FALCON_FLASH_BOOTCODE_START) { |
Ben Hutchings | 6a8872c | 2010-12-02 13:47:10 +0000 | [diff] [blame] | 404 | efx_mtd = kzalloc(sizeof(*efx_mtd) + sizeof(efx_mtd->part[0]), |
| 405 | GFP_KERNEL); |
| 406 | if (!efx_mtd) |
| 407 | return -ENOMEM; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 408 | |
Ben Hutchings | 6a8872c | 2010-12-02 13:47:10 +0000 | [diff] [blame] | 409 | efx_mtd->spi = spi; |
| 410 | efx_mtd->name = "flash"; |
| 411 | efx_mtd->ops = &falcon_mtd_ops; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 412 | |
Ben Hutchings | 6a8872c | 2010-12-02 13:47:10 +0000 | [diff] [blame] | 413 | efx_mtd->n_parts = 1; |
| 414 | efx_mtd->part[0].mtd.type = MTD_NORFLASH; |
| 415 | efx_mtd->part[0].mtd.flags = MTD_CAP_NORFLASH; |
| 416 | efx_mtd->part[0].mtd.size = spi->size - FALCON_FLASH_BOOTCODE_START; |
| 417 | efx_mtd->part[0].mtd.erasesize = spi->erase_size; |
| 418 | efx_mtd->part[0].offset = FALCON_FLASH_BOOTCODE_START; |
| 419 | efx_mtd->part[0].type_name = "sfc_flash_bootrom"; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 420 | |
Ben Hutchings | 6a8872c | 2010-12-02 13:47:10 +0000 | [diff] [blame] | 421 | rc = efx_mtd_probe_device(efx, efx_mtd); |
| 422 | if (rc) { |
| 423 | kfree(efx_mtd); |
| 424 | return rc; |
| 425 | } |
| 426 | } |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 427 | |
Ben Hutchings | 4de9218 | 2010-12-02 13:47:29 +0000 | [diff] [blame] | 428 | spi = &nic_data->spi_eeprom; |
| 429 | if (efx_spi_present(spi) && spi->size > EFX_EEPROM_BOOTCONFIG_START) { |
Ben Hutchings | 6a8872c | 2010-12-02 13:47:10 +0000 | [diff] [blame] | 430 | efx_mtd = kzalloc(sizeof(*efx_mtd) + sizeof(efx_mtd->part[0]), |
| 431 | GFP_KERNEL); |
| 432 | if (!efx_mtd) |
| 433 | return -ENOMEM; |
| 434 | |
| 435 | efx_mtd->spi = spi; |
| 436 | efx_mtd->name = "EEPROM"; |
| 437 | efx_mtd->ops = &falcon_mtd_ops; |
| 438 | |
| 439 | efx_mtd->n_parts = 1; |
| 440 | efx_mtd->part[0].mtd.type = MTD_RAM; |
| 441 | efx_mtd->part[0].mtd.flags = MTD_CAP_RAM; |
| 442 | efx_mtd->part[0].mtd.size = |
| 443 | min(spi->size, EFX_EEPROM_BOOTCONFIG_END) - |
| 444 | EFX_EEPROM_BOOTCONFIG_START; |
| 445 | efx_mtd->part[0].mtd.erasesize = spi->erase_size; |
| 446 | efx_mtd->part[0].offset = EFX_EEPROM_BOOTCONFIG_START; |
| 447 | efx_mtd->part[0].type_name = "sfc_bootconfig"; |
| 448 | |
| 449 | rc = efx_mtd_probe_device(efx, efx_mtd); |
| 450 | if (rc) { |
| 451 | kfree(efx_mtd); |
| 452 | return rc; |
| 453 | } |
| 454 | } |
| 455 | |
Ben Hutchings | 7688483 | 2009-11-29 15:10:44 +0000 | [diff] [blame] | 456 | return rc; |
Ben Hutchings | f415072 | 2008-11-04 20:34:28 +0000 | [diff] [blame] | 457 | } |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 458 | |
| 459 | /* Implementation of MTD operations for Siena */ |
| 460 | |
| 461 | static int siena_mtd_read(struct mtd_info *mtd, loff_t start, |
| 462 | size_t len, size_t *retlen, u8 *buffer) |
| 463 | { |
| 464 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
| 465 | struct efx_mtd *efx_mtd = mtd->priv; |
| 466 | struct efx_nic *efx = efx_mtd->efx; |
| 467 | loff_t offset = start; |
| 468 | loff_t end = min_t(loff_t, start + len, mtd->size); |
| 469 | size_t chunk; |
| 470 | int rc = 0; |
| 471 | |
| 472 | while (offset < end) { |
Ben Hutchings | 5a27e86 | 2010-01-25 15:49:59 -0800 | [diff] [blame] | 473 | chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX); |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 474 | rc = efx_mcdi_nvram_read(efx, part->mcdi.nvram_type, offset, |
| 475 | buffer, chunk); |
| 476 | if (rc) |
| 477 | goto out; |
| 478 | offset += chunk; |
| 479 | buffer += chunk; |
| 480 | } |
| 481 | out: |
| 482 | *retlen = offset - start; |
| 483 | return rc; |
| 484 | } |
| 485 | |
| 486 | static int siena_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len) |
| 487 | { |
| 488 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
| 489 | struct efx_mtd *efx_mtd = mtd->priv; |
| 490 | struct efx_nic *efx = efx_mtd->efx; |
| 491 | loff_t offset = start & ~((loff_t)(mtd->erasesize - 1)); |
| 492 | loff_t end = min_t(loff_t, start + len, mtd->size); |
| 493 | size_t chunk = part->mtd.erasesize; |
| 494 | int rc = 0; |
| 495 | |
| 496 | if (!part->mcdi.updating) { |
| 497 | rc = efx_mcdi_nvram_update_start(efx, part->mcdi.nvram_type); |
| 498 | if (rc) |
| 499 | goto out; |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 500 | part->mcdi.updating = true; |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* The MCDI interface can in fact do multiple erase blocks at once; |
| 504 | * but erasing may be slow, so we make multiple calls here to avoid |
| 505 | * tripping the MCDI RPC timeout. */ |
| 506 | while (offset < end) { |
| 507 | rc = efx_mcdi_nvram_erase(efx, part->mcdi.nvram_type, offset, |
| 508 | chunk); |
| 509 | if (rc) |
| 510 | goto out; |
| 511 | offset += chunk; |
| 512 | } |
| 513 | out: |
| 514 | return rc; |
| 515 | } |
| 516 | |
| 517 | static int siena_mtd_write(struct mtd_info *mtd, loff_t start, |
| 518 | size_t len, size_t *retlen, const u8 *buffer) |
| 519 | { |
| 520 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
| 521 | struct efx_mtd *efx_mtd = mtd->priv; |
| 522 | struct efx_nic *efx = efx_mtd->efx; |
| 523 | loff_t offset = start; |
| 524 | loff_t end = min_t(loff_t, start + len, mtd->size); |
| 525 | size_t chunk; |
| 526 | int rc = 0; |
| 527 | |
| 528 | if (!part->mcdi.updating) { |
| 529 | rc = efx_mcdi_nvram_update_start(efx, part->mcdi.nvram_type); |
| 530 | if (rc) |
| 531 | goto out; |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 532 | part->mcdi.updating = true; |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | while (offset < end) { |
Ben Hutchings | 5a27e86 | 2010-01-25 15:49:59 -0800 | [diff] [blame] | 536 | chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX); |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 537 | rc = efx_mcdi_nvram_write(efx, part->mcdi.nvram_type, offset, |
| 538 | buffer, chunk); |
| 539 | if (rc) |
| 540 | goto out; |
| 541 | offset += chunk; |
| 542 | buffer += chunk; |
| 543 | } |
| 544 | out: |
| 545 | *retlen = offset - start; |
| 546 | return rc; |
| 547 | } |
| 548 | |
| 549 | static int siena_mtd_sync(struct mtd_info *mtd) |
| 550 | { |
| 551 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
| 552 | struct efx_mtd *efx_mtd = mtd->priv; |
| 553 | struct efx_nic *efx = efx_mtd->efx; |
| 554 | int rc = 0; |
| 555 | |
| 556 | if (part->mcdi.updating) { |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 557 | part->mcdi.updating = false; |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 558 | rc = efx_mcdi_nvram_update_finish(efx, part->mcdi.nvram_type); |
| 559 | } |
| 560 | |
| 561 | return rc; |
| 562 | } |
| 563 | |
Ben Hutchings | 18e83e4 | 2012-01-05 19:05:20 +0000 | [diff] [blame] | 564 | static const struct efx_mtd_ops siena_mtd_ops = { |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 565 | .read = siena_mtd_read, |
| 566 | .erase = siena_mtd_erase, |
| 567 | .write = siena_mtd_write, |
| 568 | .sync = siena_mtd_sync, |
| 569 | }; |
| 570 | |
| 571 | struct siena_nvram_type_info { |
| 572 | int port; |
| 573 | const char *name; |
| 574 | }; |
| 575 | |
Ben Hutchings | 18e83e4 | 2012-01-05 19:05:20 +0000 | [diff] [blame] | 576 | static const struct siena_nvram_type_info siena_nvram_types[] = { |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 577 | [MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO] = { 0, "sfc_dummy_phy" }, |
| 578 | [MC_CMD_NVRAM_TYPE_MC_FW] = { 0, "sfc_mcfw" }, |
| 579 | [MC_CMD_NVRAM_TYPE_MC_FW_BACKUP] = { 0, "sfc_mcfw_backup" }, |
| 580 | [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0] = { 0, "sfc_static_cfg" }, |
| 581 | [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1] = { 1, "sfc_static_cfg" }, |
| 582 | [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0] = { 0, "sfc_dynamic_cfg" }, |
| 583 | [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1] = { 1, "sfc_dynamic_cfg" }, |
| 584 | [MC_CMD_NVRAM_TYPE_EXP_ROM] = { 0, "sfc_exp_rom" }, |
| 585 | [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT0] = { 0, "sfc_exp_rom_cfg" }, |
| 586 | [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT1] = { 1, "sfc_exp_rom_cfg" }, |
| 587 | [MC_CMD_NVRAM_TYPE_PHY_PORT0] = { 0, "sfc_phy_fw" }, |
| 588 | [MC_CMD_NVRAM_TYPE_PHY_PORT1] = { 1, "sfc_phy_fw" }, |
| 589 | }; |
| 590 | |
| 591 | static int siena_mtd_probe_partition(struct efx_nic *efx, |
| 592 | struct efx_mtd *efx_mtd, |
| 593 | unsigned int part_id, |
| 594 | unsigned int type) |
| 595 | { |
| 596 | struct efx_mtd_partition *part = &efx_mtd->part[part_id]; |
Ben Hutchings | 18e83e4 | 2012-01-05 19:05:20 +0000 | [diff] [blame] | 597 | const struct siena_nvram_type_info *info; |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 598 | size_t size, erase_size; |
| 599 | bool protected; |
| 600 | int rc; |
| 601 | |
| 602 | if (type >= ARRAY_SIZE(siena_nvram_types)) |
| 603 | return -ENODEV; |
| 604 | |
| 605 | info = &siena_nvram_types[type]; |
| 606 | |
| 607 | if (info->port != efx_port_num(efx)) |
| 608 | return -ENODEV; |
| 609 | |
| 610 | rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected); |
| 611 | if (rc) |
| 612 | return rc; |
| 613 | if (protected) |
| 614 | return -ENODEV; /* hide it */ |
| 615 | |
| 616 | part->mcdi.nvram_type = type; |
| 617 | part->type_name = info->name; |
| 618 | |
| 619 | part->mtd.type = MTD_NORFLASH; |
| 620 | part->mtd.flags = MTD_CAP_NORFLASH; |
| 621 | part->mtd.size = size; |
| 622 | part->mtd.erasesize = erase_size; |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static int siena_mtd_get_fw_subtypes(struct efx_nic *efx, |
| 628 | struct efx_mtd *efx_mtd) |
| 629 | { |
| 630 | struct efx_mtd_partition *part; |
Ben Hutchings | 05a9320 | 2011-12-20 00:44:06 +0000 | [diff] [blame] | 631 | uint16_t fw_subtype_list[MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MINNUM]; |
Ben Hutchings | 8880f4e | 2009-11-29 15:15:41 +0000 | [diff] [blame] | 632 | int rc; |
| 633 | |
| 634 | rc = efx_mcdi_get_board_cfg(efx, NULL, fw_subtype_list); |
| 635 | if (rc) |
| 636 | return rc; |
| 637 | |
| 638 | efx_for_each_partition(part, efx_mtd) |
| 639 | part->mcdi.fw_subtype = fw_subtype_list[part->mcdi.nvram_type]; |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int siena_mtd_probe(struct efx_nic *efx) |
| 645 | { |
| 646 | struct efx_mtd *efx_mtd; |
| 647 | int rc = -ENODEV; |
| 648 | u32 nvram_types; |
| 649 | unsigned int type; |
| 650 | |
| 651 | ASSERT_RTNL(); |
| 652 | |
| 653 | rc = efx_mcdi_nvram_types(efx, &nvram_types); |
| 654 | if (rc) |
| 655 | return rc; |
| 656 | |
| 657 | efx_mtd = kzalloc(sizeof(*efx_mtd) + |
| 658 | hweight32(nvram_types) * sizeof(efx_mtd->part[0]), |
| 659 | GFP_KERNEL); |
| 660 | if (!efx_mtd) |
| 661 | return -ENOMEM; |
| 662 | |
| 663 | efx_mtd->name = "Siena NVRAM manager"; |
| 664 | |
| 665 | efx_mtd->ops = &siena_mtd_ops; |
| 666 | |
| 667 | type = 0; |
| 668 | efx_mtd->n_parts = 0; |
| 669 | |
| 670 | while (nvram_types != 0) { |
| 671 | if (nvram_types & 1) { |
| 672 | rc = siena_mtd_probe_partition(efx, efx_mtd, |
| 673 | efx_mtd->n_parts, type); |
| 674 | if (rc == 0) |
| 675 | efx_mtd->n_parts++; |
| 676 | else if (rc != -ENODEV) |
| 677 | goto fail; |
| 678 | } |
| 679 | type++; |
| 680 | nvram_types >>= 1; |
| 681 | } |
| 682 | |
| 683 | rc = siena_mtd_get_fw_subtypes(efx, efx_mtd); |
| 684 | if (rc) |
| 685 | goto fail; |
| 686 | |
| 687 | rc = efx_mtd_probe_device(efx, efx_mtd); |
| 688 | fail: |
| 689 | if (rc) |
| 690 | kfree(efx_mtd); |
| 691 | return rc; |
| 692 | } |
| 693 | |