Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 1 | /* |
Mike Marciniszyn | 7fac330 | 2012-07-19 13:04:25 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2012 Intel Corporation. All rights reserved. |
| 3 | * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved. |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 4 | * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved. |
| 5 | * |
| 6 | * This software is available to you under a choice of one of two |
| 7 | * licenses. You may choose to be licensed under the terms of the GNU |
| 8 | * General Public License (GPL) Version 2, available from the file |
| 9 | * COPYING in the main directory of this source tree, or the |
| 10 | * OpenIB.org BSD license below: |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or |
| 13 | * without modification, are permitted provided that the following |
| 14 | * conditions are met: |
| 15 | * |
| 16 | * - Redistributions of source code must retain the above |
| 17 | * copyright notice, this list of conditions and the following |
| 18 | * disclaimer. |
| 19 | * |
| 20 | * - Redistributions in binary form must reproduce the above |
| 21 | * copyright notice, this list of conditions and the following |
| 22 | * disclaimer in the documentation and/or other materials |
| 23 | * provided with the distribution. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 29 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 30 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 31 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 | * SOFTWARE. |
| 33 | */ |
| 34 | |
| 35 | #include <linux/delay.h> |
| 36 | #include <linux/pci.h> |
| 37 | #include <linux/vmalloc.h> |
| 38 | |
| 39 | #include "qib.h" |
| 40 | |
| 41 | /* |
| 42 | * Functions specific to the serial EEPROM on cards handled by ib_qib. |
| 43 | * The actual serail interface code is in qib_twsi.c. This file is a client |
| 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * qib_eeprom_read - receives bytes from the eeprom via I2C |
| 48 | * @dd: the qlogic_ib device |
| 49 | * @eeprom_offset: address to read from |
| 50 | * @buffer: where to store result |
| 51 | * @len: number of bytes to receive |
| 52 | */ |
| 53 | int qib_eeprom_read(struct qib_devdata *dd, u8 eeprom_offset, |
| 54 | void *buff, int len) |
| 55 | { |
| 56 | int ret; |
| 57 | |
| 58 | ret = mutex_lock_interruptible(&dd->eep_lock); |
| 59 | if (!ret) { |
| 60 | ret = qib_twsi_reset(dd); |
| 61 | if (ret) |
| 62 | qib_dev_err(dd, "EEPROM Reset for read failed\n"); |
| 63 | else |
| 64 | ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev, |
| 65 | eeprom_offset, buff, len); |
| 66 | mutex_unlock(&dd->eep_lock); |
| 67 | } |
| 68 | |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Actually update the eeprom, first doing write enable if |
| 74 | * needed, then restoring write enable state. |
| 75 | * Must be called with eep_lock held |
| 76 | */ |
| 77 | static int eeprom_write_with_enable(struct qib_devdata *dd, u8 offset, |
| 78 | const void *buf, int len) |
| 79 | { |
| 80 | int ret, pwen; |
| 81 | |
| 82 | pwen = dd->f_eeprom_wen(dd, 1); |
| 83 | ret = qib_twsi_reset(dd); |
| 84 | if (ret) |
| 85 | qib_dev_err(dd, "EEPROM Reset for write failed\n"); |
| 86 | else |
| 87 | ret = qib_twsi_blk_wr(dd, dd->twsi_eeprom_dev, |
| 88 | offset, buf, len); |
| 89 | dd->f_eeprom_wen(dd, pwen); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * qib_eeprom_write - writes data to the eeprom via I2C |
| 95 | * @dd: the qlogic_ib device |
| 96 | * @eeprom_offset: where to place data |
| 97 | * @buffer: data to write |
| 98 | * @len: number of bytes to write |
| 99 | */ |
| 100 | int qib_eeprom_write(struct qib_devdata *dd, u8 eeprom_offset, |
| 101 | const void *buff, int len) |
| 102 | { |
| 103 | int ret; |
| 104 | |
| 105 | ret = mutex_lock_interruptible(&dd->eep_lock); |
| 106 | if (!ret) { |
| 107 | ret = eeprom_write_with_enable(dd, eeprom_offset, buff, len); |
| 108 | mutex_unlock(&dd->eep_lock); |
| 109 | } |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | static u8 flash_csum(struct qib_flash *ifp, int adjust) |
| 115 | { |
| 116 | u8 *ip = (u8 *) ifp; |
| 117 | u8 csum = 0, len; |
| 118 | |
| 119 | /* |
| 120 | * Limit length checksummed to max length of actual data. |
| 121 | * Checksum of erased eeprom will still be bad, but we avoid |
| 122 | * reading past the end of the buffer we were passed. |
| 123 | */ |
| 124 | len = ifp->if_length; |
| 125 | if (len > sizeof(struct qib_flash)) |
| 126 | len = sizeof(struct qib_flash); |
| 127 | while (len--) |
| 128 | csum += *ip++; |
| 129 | csum -= ifp->if_csum; |
| 130 | csum = ~csum; |
| 131 | if (adjust) |
| 132 | ifp->if_csum = csum; |
| 133 | |
| 134 | return csum; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * qib_get_eeprom_info- get the GUID et al. from the TSWI EEPROM device |
| 139 | * @dd: the qlogic_ib device |
| 140 | * |
| 141 | * We have the capability to use the nguid field, and get |
| 142 | * the guid from the first chip's flash, to use for all of them. |
| 143 | */ |
| 144 | void qib_get_eeprom_info(struct qib_devdata *dd) |
| 145 | { |
| 146 | void *buf; |
| 147 | struct qib_flash *ifp; |
| 148 | __be64 guid; |
| 149 | int len, eep_stat; |
| 150 | u8 csum, *bguid; |
| 151 | int t = dd->unit; |
| 152 | struct qib_devdata *dd0 = qib_lookup(0); |
| 153 | |
| 154 | if (t && dd0->nguid > 1 && t <= dd0->nguid) { |
| 155 | u8 oguid; |
| 156 | dd->base_guid = dd0->base_guid; |
| 157 | bguid = (u8 *) &dd->base_guid; |
| 158 | |
| 159 | oguid = bguid[7]; |
| 160 | bguid[7] += t; |
| 161 | if (oguid > bguid[7]) { |
| 162 | if (bguid[6] == 0xff) { |
| 163 | if (bguid[5] == 0xff) { |
Mike Marciniszyn | 7fac330 | 2012-07-19 13:04:25 +0000 | [diff] [blame^] | 164 | qib_dev_err(dd, |
| 165 | "Can't set %s GUID from base, wraps to OUI!\n", |
| 166 | qib_get_unit_name(t)); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 167 | dd->base_guid = 0; |
| 168 | goto bail; |
| 169 | } |
| 170 | bguid[5]++; |
| 171 | } |
| 172 | bguid[6]++; |
| 173 | } |
| 174 | dd->nguid = 1; |
| 175 | goto bail; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Read full flash, not just currently used part, since it may have |
| 180 | * been written with a newer definition. |
| 181 | * */ |
| 182 | len = sizeof(struct qib_flash); |
| 183 | buf = vmalloc(len); |
| 184 | if (!buf) { |
Mike Marciniszyn | 7fac330 | 2012-07-19 13:04:25 +0000 | [diff] [blame^] | 185 | qib_dev_err(dd, |
| 186 | "Couldn't allocate memory to read %u bytes from eeprom for GUID\n", |
| 187 | len); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 188 | goto bail; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Use "public" eeprom read function, which does locking and |
| 193 | * figures out device. This will migrate to chip-specific. |
| 194 | */ |
| 195 | eep_stat = qib_eeprom_read(dd, 0, buf, len); |
| 196 | |
| 197 | if (eep_stat) { |
| 198 | qib_dev_err(dd, "Failed reading GUID from eeprom\n"); |
| 199 | goto done; |
| 200 | } |
| 201 | ifp = (struct qib_flash *)buf; |
| 202 | |
| 203 | csum = flash_csum(ifp, 0); |
| 204 | if (csum != ifp->if_csum) { |
Mike Marciniszyn | 7fac330 | 2012-07-19 13:04:25 +0000 | [diff] [blame^] | 205 | qib_devinfo(dd->pcidev, |
| 206 | "Bad I2C flash checksum: 0x%x, not 0x%x\n", |
| 207 | csum, ifp->if_csum); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 208 | goto done; |
| 209 | } |
| 210 | if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) || |
| 211 | *(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) { |
Mike Marciniszyn | 7fac330 | 2012-07-19 13:04:25 +0000 | [diff] [blame^] | 212 | qib_dev_err(dd, |
| 213 | "Invalid GUID %llx from flash; ignoring\n", |
| 214 | *(unsigned long long *) ifp->if_guid); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 215 | /* don't allow GUID if all 0 or all 1's */ |
| 216 | goto done; |
| 217 | } |
| 218 | |
| 219 | /* complain, but allow it */ |
| 220 | if (*(u64 *) ifp->if_guid == 0x100007511000000ULL) |
Mike Marciniszyn | 7fac330 | 2012-07-19 13:04:25 +0000 | [diff] [blame^] | 221 | qib_devinfo(dd->pcidev, |
| 222 | "Warning, GUID %llx is default, probably not correct!\n", |
| 223 | *(unsigned long long *) ifp->if_guid); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 224 | |
| 225 | bguid = ifp->if_guid; |
| 226 | if (!bguid[0] && !bguid[1] && !bguid[2]) { |
| 227 | /* |
| 228 | * Original incorrect GUID format in flash; fix in |
| 229 | * core copy, by shifting up 2 octets; don't need to |
| 230 | * change top octet, since both it and shifted are 0. |
| 231 | */ |
| 232 | bguid[1] = bguid[3]; |
| 233 | bguid[2] = bguid[4]; |
| 234 | bguid[3] = 0; |
| 235 | bguid[4] = 0; |
| 236 | guid = *(__be64 *) ifp->if_guid; |
| 237 | } else |
| 238 | guid = *(__be64 *) ifp->if_guid; |
| 239 | dd->base_guid = guid; |
| 240 | dd->nguid = ifp->if_numguid; |
| 241 | /* |
| 242 | * Things are slightly complicated by the desire to transparently |
| 243 | * support both the Pathscale 10-digit serial number and the QLogic |
| 244 | * 13-character version. |
| 245 | */ |
| 246 | if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] && |
| 247 | ((u8 *) ifp->if_sprefix)[0] != 0xFF) { |
| 248 | char *snp = dd->serial; |
| 249 | |
| 250 | /* |
| 251 | * This board has a Serial-prefix, which is stored |
| 252 | * elsewhere for backward-compatibility. |
| 253 | */ |
| 254 | memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix); |
| 255 | snp[sizeof ifp->if_sprefix] = '\0'; |
| 256 | len = strlen(snp); |
| 257 | snp += len; |
| 258 | len = (sizeof dd->serial) - len; |
| 259 | if (len > sizeof ifp->if_serial) |
| 260 | len = sizeof ifp->if_serial; |
| 261 | memcpy(snp, ifp->if_serial, len); |
| 262 | } else |
| 263 | memcpy(dd->serial, ifp->if_serial, |
| 264 | sizeof ifp->if_serial); |
| 265 | if (!strstr(ifp->if_comment, "Tested successfully")) |
Mike Marciniszyn | 7fac330 | 2012-07-19 13:04:25 +0000 | [diff] [blame^] | 266 | qib_dev_err(dd, |
| 267 | "Board SN %s did not pass functional test: %s\n", |
| 268 | dd->serial, ifp->if_comment); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 269 | |
| 270 | memcpy(&dd->eep_st_errs, &ifp->if_errcntp, QIB_EEP_LOG_CNT); |
| 271 | /* |
| 272 | * Power-on (actually "active") hours are kept as little-endian value |
| 273 | * in EEPROM, but as seconds in a (possibly as small as 24-bit) |
| 274 | * atomic_t while running. |
| 275 | */ |
| 276 | atomic_set(&dd->active_time, 0); |
| 277 | dd->eep_hrs = ifp->if_powerhour[0] | (ifp->if_powerhour[1] << 8); |
| 278 | |
| 279 | done: |
| 280 | vfree(buf); |
| 281 | |
| 282 | bail:; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * qib_update_eeprom_log - copy active-time and error counters to eeprom |
| 287 | * @dd: the qlogic_ib device |
| 288 | * |
| 289 | * Although the time is kept as seconds in the qib_devdata struct, it is |
| 290 | * rounded to hours for re-write, as we have only 16 bits in EEPROM. |
| 291 | * First-cut code reads whole (expected) struct qib_flash, modifies, |
| 292 | * re-writes. Future direction: read/write only what we need, assuming |
| 293 | * that the EEPROM had to have been "good enough" for driver init, and |
| 294 | * if not, we aren't making it worse. |
| 295 | * |
| 296 | */ |
| 297 | int qib_update_eeprom_log(struct qib_devdata *dd) |
| 298 | { |
| 299 | void *buf; |
| 300 | struct qib_flash *ifp; |
| 301 | int len, hi_water; |
| 302 | uint32_t new_time, new_hrs; |
| 303 | u8 csum; |
| 304 | int ret, idx; |
| 305 | unsigned long flags; |
| 306 | |
| 307 | /* first, check if we actually need to do anything. */ |
| 308 | ret = 0; |
| 309 | for (idx = 0; idx < QIB_EEP_LOG_CNT; ++idx) { |
| 310 | if (dd->eep_st_new_errs[idx]) { |
| 311 | ret = 1; |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | new_time = atomic_read(&dd->active_time); |
| 316 | |
| 317 | if (ret == 0 && new_time < 3600) |
| 318 | goto bail; |
| 319 | |
| 320 | /* |
| 321 | * The quick-check above determined that there is something worthy |
| 322 | * of logging, so get current contents and do a more detailed idea. |
| 323 | * read full flash, not just currently used part, since it may have |
| 324 | * been written with a newer definition |
| 325 | */ |
| 326 | len = sizeof(struct qib_flash); |
| 327 | buf = vmalloc(len); |
| 328 | ret = 1; |
| 329 | if (!buf) { |
Mike Marciniszyn | 7fac330 | 2012-07-19 13:04:25 +0000 | [diff] [blame^] | 330 | qib_dev_err(dd, |
| 331 | "Couldn't allocate memory to read %u bytes from eeprom for logging\n", |
| 332 | len); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 333 | goto bail; |
| 334 | } |
| 335 | |
| 336 | /* Grab semaphore and read current EEPROM. If we get an |
| 337 | * error, let go, but if not, keep it until we finish write. |
| 338 | */ |
| 339 | ret = mutex_lock_interruptible(&dd->eep_lock); |
| 340 | if (ret) { |
| 341 | qib_dev_err(dd, "Unable to acquire EEPROM for logging\n"); |
| 342 | goto free_bail; |
| 343 | } |
| 344 | ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev, 0, buf, len); |
| 345 | if (ret) { |
| 346 | mutex_unlock(&dd->eep_lock); |
| 347 | qib_dev_err(dd, "Unable read EEPROM for logging\n"); |
| 348 | goto free_bail; |
| 349 | } |
| 350 | ifp = (struct qib_flash *)buf; |
| 351 | |
| 352 | csum = flash_csum(ifp, 0); |
| 353 | if (csum != ifp->if_csum) { |
| 354 | mutex_unlock(&dd->eep_lock); |
| 355 | qib_dev_err(dd, "EEPROM cks err (0x%02X, S/B 0x%02X)\n", |
| 356 | csum, ifp->if_csum); |
| 357 | ret = 1; |
| 358 | goto free_bail; |
| 359 | } |
| 360 | hi_water = 0; |
| 361 | spin_lock_irqsave(&dd->eep_st_lock, flags); |
| 362 | for (idx = 0; idx < QIB_EEP_LOG_CNT; ++idx) { |
| 363 | int new_val = dd->eep_st_new_errs[idx]; |
| 364 | if (new_val) { |
| 365 | /* |
| 366 | * If we have seen any errors, add to EEPROM values |
| 367 | * We need to saturate at 0xFF (255) and we also |
| 368 | * would need to adjust the checksum if we were |
| 369 | * trying to minimize EEPROM traffic |
| 370 | * Note that we add to actual current count in EEPROM, |
| 371 | * in case it was altered while we were running. |
| 372 | */ |
| 373 | new_val += ifp->if_errcntp[idx]; |
| 374 | if (new_val > 0xFF) |
| 375 | new_val = 0xFF; |
| 376 | if (ifp->if_errcntp[idx] != new_val) { |
| 377 | ifp->if_errcntp[idx] = new_val; |
| 378 | hi_water = offsetof(struct qib_flash, |
| 379 | if_errcntp) + idx; |
| 380 | } |
| 381 | /* |
| 382 | * update our shadow (used to minimize EEPROM |
| 383 | * traffic), to match what we are about to write. |
| 384 | */ |
| 385 | dd->eep_st_errs[idx] = new_val; |
| 386 | dd->eep_st_new_errs[idx] = 0; |
| 387 | } |
| 388 | } |
| 389 | /* |
| 390 | * Now update active-time. We would like to round to the nearest hour |
| 391 | * but unless atomic_t are sure to be proper signed ints we cannot, |
| 392 | * because we need to account for what we "transfer" to EEPROM and |
| 393 | * if we log an hour at 31 minutes, then we would need to set |
| 394 | * active_time to -29 to accurately count the _next_ hour. |
| 395 | */ |
| 396 | if (new_time >= 3600) { |
| 397 | new_hrs = new_time / 3600; |
| 398 | atomic_sub((new_hrs * 3600), &dd->active_time); |
| 399 | new_hrs += dd->eep_hrs; |
| 400 | if (new_hrs > 0xFFFF) |
| 401 | new_hrs = 0xFFFF; |
| 402 | dd->eep_hrs = new_hrs; |
| 403 | if ((new_hrs & 0xFF) != ifp->if_powerhour[0]) { |
| 404 | ifp->if_powerhour[0] = new_hrs & 0xFF; |
| 405 | hi_water = offsetof(struct qib_flash, if_powerhour); |
| 406 | } |
| 407 | if ((new_hrs >> 8) != ifp->if_powerhour[1]) { |
| 408 | ifp->if_powerhour[1] = new_hrs >> 8; |
| 409 | hi_water = offsetof(struct qib_flash, if_powerhour) + 1; |
| 410 | } |
| 411 | } |
| 412 | /* |
| 413 | * There is a tiny possibility that we could somehow fail to write |
| 414 | * the EEPROM after updating our shadows, but problems from holding |
| 415 | * the spinlock too long are a much bigger issue. |
| 416 | */ |
| 417 | spin_unlock_irqrestore(&dd->eep_st_lock, flags); |
| 418 | if (hi_water) { |
| 419 | /* we made some change to the data, uopdate cksum and write */ |
| 420 | csum = flash_csum(ifp, 1); |
| 421 | ret = eeprom_write_with_enable(dd, 0, buf, hi_water + 1); |
| 422 | } |
| 423 | mutex_unlock(&dd->eep_lock); |
| 424 | if (ret) |
| 425 | qib_dev_err(dd, "Failed updating EEPROM\n"); |
| 426 | |
| 427 | free_bail: |
| 428 | vfree(buf); |
| 429 | bail: |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * qib_inc_eeprom_err - increment one of the four error counters |
| 435 | * that are logged to EEPROM. |
| 436 | * @dd: the qlogic_ib device |
| 437 | * @eidx: 0..3, the counter to increment |
| 438 | * @incr: how much to add |
| 439 | * |
| 440 | * Each counter is 8-bits, and saturates at 255 (0xFF). They |
| 441 | * are copied to the EEPROM (aka flash) whenever qib_update_eeprom_log() |
| 442 | * is called, but it can only be called in a context that allows sleep. |
| 443 | * This function can be called even at interrupt level. |
| 444 | */ |
| 445 | void qib_inc_eeprom_err(struct qib_devdata *dd, u32 eidx, u32 incr) |
| 446 | { |
| 447 | uint new_val; |
| 448 | unsigned long flags; |
| 449 | |
| 450 | spin_lock_irqsave(&dd->eep_st_lock, flags); |
| 451 | new_val = dd->eep_st_new_errs[eidx] + incr; |
| 452 | if (new_val > 255) |
| 453 | new_val = 255; |
| 454 | dd->eep_st_new_errs[eidx] = new_val; |
| 455 | spin_unlock_irqrestore(&dd->eep_st_lock, flags); |
| 456 | } |