Path: blob/master/drivers/infiniband/hw/qib/qib_eeprom.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.2* Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/delay.h>34#include <linux/pci.h>35#include <linux/vmalloc.h>3637#include "qib.h"3839/*40* Functions specific to the serial EEPROM on cards handled by ib_qib.41* The actual serail interface code is in qib_twsi.c. This file is a client42*/4344/**45* qib_eeprom_read - receives bytes from the eeprom via I2C46* @dd: the qlogic_ib device47* @eeprom_offset: address to read from48* @buffer: where to store result49* @len: number of bytes to receive50*/51int qib_eeprom_read(struct qib_devdata *dd, u8 eeprom_offset,52void *buff, int len)53{54int ret;5556ret = mutex_lock_interruptible(&dd->eep_lock);57if (!ret) {58ret = qib_twsi_reset(dd);59if (ret)60qib_dev_err(dd, "EEPROM Reset for read failed\n");61else62ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev,63eeprom_offset, buff, len);64mutex_unlock(&dd->eep_lock);65}6667return ret;68}6970/*71* Actually update the eeprom, first doing write enable if72* needed, then restoring write enable state.73* Must be called with eep_lock held74*/75static int eeprom_write_with_enable(struct qib_devdata *dd, u8 offset,76const void *buf, int len)77{78int ret, pwen;7980pwen = dd->f_eeprom_wen(dd, 1);81ret = qib_twsi_reset(dd);82if (ret)83qib_dev_err(dd, "EEPROM Reset for write failed\n");84else85ret = qib_twsi_blk_wr(dd, dd->twsi_eeprom_dev,86offset, buf, len);87dd->f_eeprom_wen(dd, pwen);88return ret;89}9091/**92* qib_eeprom_write - writes data to the eeprom via I2C93* @dd: the qlogic_ib device94* @eeprom_offset: where to place data95* @buffer: data to write96* @len: number of bytes to write97*/98int qib_eeprom_write(struct qib_devdata *dd, u8 eeprom_offset,99const void *buff, int len)100{101int ret;102103ret = mutex_lock_interruptible(&dd->eep_lock);104if (!ret) {105ret = eeprom_write_with_enable(dd, eeprom_offset, buff, len);106mutex_unlock(&dd->eep_lock);107}108109return ret;110}111112static u8 flash_csum(struct qib_flash *ifp, int adjust)113{114u8 *ip = (u8 *) ifp;115u8 csum = 0, len;116117/*118* Limit length checksummed to max length of actual data.119* Checksum of erased eeprom will still be bad, but we avoid120* reading past the end of the buffer we were passed.121*/122len = ifp->if_length;123if (len > sizeof(struct qib_flash))124len = sizeof(struct qib_flash);125while (len--)126csum += *ip++;127csum -= ifp->if_csum;128csum = ~csum;129if (adjust)130ifp->if_csum = csum;131132return csum;133}134135/**136* qib_get_eeprom_info- get the GUID et al. from the TSWI EEPROM device137* @dd: the qlogic_ib device138*139* We have the capability to use the nguid field, and get140* the guid from the first chip's flash, to use for all of them.141*/142void qib_get_eeprom_info(struct qib_devdata *dd)143{144void *buf;145struct qib_flash *ifp;146__be64 guid;147int len, eep_stat;148u8 csum, *bguid;149int t = dd->unit;150struct qib_devdata *dd0 = qib_lookup(0);151152if (t && dd0->nguid > 1 && t <= dd0->nguid) {153u8 oguid;154dd->base_guid = dd0->base_guid;155bguid = (u8 *) &dd->base_guid;156157oguid = bguid[7];158bguid[7] += t;159if (oguid > bguid[7]) {160if (bguid[6] == 0xff) {161if (bguid[5] == 0xff) {162qib_dev_err(dd, "Can't set %s GUID"163" from base, wraps to"164" OUI!\n",165qib_get_unit_name(t));166dd->base_guid = 0;167goto bail;168}169bguid[5]++;170}171bguid[6]++;172}173dd->nguid = 1;174goto bail;175}176177/*178* Read full flash, not just currently used part, since it may have179* been written with a newer definition.180* */181len = sizeof(struct qib_flash);182buf = vmalloc(len);183if (!buf) {184qib_dev_err(dd, "Couldn't allocate memory to read %u "185"bytes from eeprom for GUID\n", len);186goto bail;187}188189/*190* Use "public" eeprom read function, which does locking and191* figures out device. This will migrate to chip-specific.192*/193eep_stat = qib_eeprom_read(dd, 0, buf, len);194195if (eep_stat) {196qib_dev_err(dd, "Failed reading GUID from eeprom\n");197goto done;198}199ifp = (struct qib_flash *)buf;200201csum = flash_csum(ifp, 0);202if (csum != ifp->if_csum) {203qib_devinfo(dd->pcidev, "Bad I2C flash checksum: "204"0x%x, not 0x%x\n", csum, ifp->if_csum);205goto done;206}207if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||208*(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {209qib_dev_err(dd, "Invalid GUID %llx from flash; ignoring\n",210*(unsigned long long *) ifp->if_guid);211/* don't allow GUID if all 0 or all 1's */212goto done;213}214215/* complain, but allow it */216if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)217qib_devinfo(dd->pcidev, "Warning, GUID %llx is "218"default, probably not correct!\n",219*(unsigned long long *) ifp->if_guid);220221bguid = ifp->if_guid;222if (!bguid[0] && !bguid[1] && !bguid[2]) {223/*224* Original incorrect GUID format in flash; fix in225* core copy, by shifting up 2 octets; don't need to226* change top octet, since both it and shifted are 0.227*/228bguid[1] = bguid[3];229bguid[2] = bguid[4];230bguid[3] = 0;231bguid[4] = 0;232guid = *(__be64 *) ifp->if_guid;233} else234guid = *(__be64 *) ifp->if_guid;235dd->base_guid = guid;236dd->nguid = ifp->if_numguid;237/*238* Things are slightly complicated by the desire to transparently239* support both the Pathscale 10-digit serial number and the QLogic240* 13-character version.241*/242if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] &&243((u8 *) ifp->if_sprefix)[0] != 0xFF) {244char *snp = dd->serial;245246/*247* This board has a Serial-prefix, which is stored248* elsewhere for backward-compatibility.249*/250memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix);251snp[sizeof ifp->if_sprefix] = '\0';252len = strlen(snp);253snp += len;254len = (sizeof dd->serial) - len;255if (len > sizeof ifp->if_serial)256len = sizeof ifp->if_serial;257memcpy(snp, ifp->if_serial, len);258} else259memcpy(dd->serial, ifp->if_serial,260sizeof ifp->if_serial);261if (!strstr(ifp->if_comment, "Tested successfully"))262qib_dev_err(dd, "Board SN %s did not pass functional "263"test: %s\n", dd->serial, ifp->if_comment);264265memcpy(&dd->eep_st_errs, &ifp->if_errcntp, QIB_EEP_LOG_CNT);266/*267* Power-on (actually "active") hours are kept as little-endian value268* in EEPROM, but as seconds in a (possibly as small as 24-bit)269* atomic_t while running.270*/271atomic_set(&dd->active_time, 0);272dd->eep_hrs = ifp->if_powerhour[0] | (ifp->if_powerhour[1] << 8);273274done:275vfree(buf);276277bail:;278}279280/**281* qib_update_eeprom_log - copy active-time and error counters to eeprom282* @dd: the qlogic_ib device283*284* Although the time is kept as seconds in the qib_devdata struct, it is285* rounded to hours for re-write, as we have only 16 bits in EEPROM.286* First-cut code reads whole (expected) struct qib_flash, modifies,287* re-writes. Future direction: read/write only what we need, assuming288* that the EEPROM had to have been "good enough" for driver init, and289* if not, we aren't making it worse.290*291*/292int qib_update_eeprom_log(struct qib_devdata *dd)293{294void *buf;295struct qib_flash *ifp;296int len, hi_water;297uint32_t new_time, new_hrs;298u8 csum;299int ret, idx;300unsigned long flags;301302/* first, check if we actually need to do anything. */303ret = 0;304for (idx = 0; idx < QIB_EEP_LOG_CNT; ++idx) {305if (dd->eep_st_new_errs[idx]) {306ret = 1;307break;308}309}310new_time = atomic_read(&dd->active_time);311312if (ret == 0 && new_time < 3600)313goto bail;314315/*316* The quick-check above determined that there is something worthy317* of logging, so get current contents and do a more detailed idea.318* read full flash, not just currently used part, since it may have319* been written with a newer definition320*/321len = sizeof(struct qib_flash);322buf = vmalloc(len);323ret = 1;324if (!buf) {325qib_dev_err(dd, "Couldn't allocate memory to read %u "326"bytes from eeprom for logging\n", len);327goto bail;328}329330/* Grab semaphore and read current EEPROM. If we get an331* error, let go, but if not, keep it until we finish write.332*/333ret = mutex_lock_interruptible(&dd->eep_lock);334if (ret) {335qib_dev_err(dd, "Unable to acquire EEPROM for logging\n");336goto free_bail;337}338ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev, 0, buf, len);339if (ret) {340mutex_unlock(&dd->eep_lock);341qib_dev_err(dd, "Unable read EEPROM for logging\n");342goto free_bail;343}344ifp = (struct qib_flash *)buf;345346csum = flash_csum(ifp, 0);347if (csum != ifp->if_csum) {348mutex_unlock(&dd->eep_lock);349qib_dev_err(dd, "EEPROM cks err (0x%02X, S/B 0x%02X)\n",350csum, ifp->if_csum);351ret = 1;352goto free_bail;353}354hi_water = 0;355spin_lock_irqsave(&dd->eep_st_lock, flags);356for (idx = 0; idx < QIB_EEP_LOG_CNT; ++idx) {357int new_val = dd->eep_st_new_errs[idx];358if (new_val) {359/*360* If we have seen any errors, add to EEPROM values361* We need to saturate at 0xFF (255) and we also362* would need to adjust the checksum if we were363* trying to minimize EEPROM traffic364* Note that we add to actual current count in EEPROM,365* in case it was altered while we were running.366*/367new_val += ifp->if_errcntp[idx];368if (new_val > 0xFF)369new_val = 0xFF;370if (ifp->if_errcntp[idx] != new_val) {371ifp->if_errcntp[idx] = new_val;372hi_water = offsetof(struct qib_flash,373if_errcntp) + idx;374}375/*376* update our shadow (used to minimize EEPROM377* traffic), to match what we are about to write.378*/379dd->eep_st_errs[idx] = new_val;380dd->eep_st_new_errs[idx] = 0;381}382}383/*384* Now update active-time. We would like to round to the nearest hour385* but unless atomic_t are sure to be proper signed ints we cannot,386* because we need to account for what we "transfer" to EEPROM and387* if we log an hour at 31 minutes, then we would need to set388* active_time to -29 to accurately count the _next_ hour.389*/390if (new_time >= 3600) {391new_hrs = new_time / 3600;392atomic_sub((new_hrs * 3600), &dd->active_time);393new_hrs += dd->eep_hrs;394if (new_hrs > 0xFFFF)395new_hrs = 0xFFFF;396dd->eep_hrs = new_hrs;397if ((new_hrs & 0xFF) != ifp->if_powerhour[0]) {398ifp->if_powerhour[0] = new_hrs & 0xFF;399hi_water = offsetof(struct qib_flash, if_powerhour);400}401if ((new_hrs >> 8) != ifp->if_powerhour[1]) {402ifp->if_powerhour[1] = new_hrs >> 8;403hi_water = offsetof(struct qib_flash, if_powerhour) + 1;404}405}406/*407* There is a tiny possibility that we could somehow fail to write408* the EEPROM after updating our shadows, but problems from holding409* the spinlock too long are a much bigger issue.410*/411spin_unlock_irqrestore(&dd->eep_st_lock, flags);412if (hi_water) {413/* we made some change to the data, uopdate cksum and write */414csum = flash_csum(ifp, 1);415ret = eeprom_write_with_enable(dd, 0, buf, hi_water + 1);416}417mutex_unlock(&dd->eep_lock);418if (ret)419qib_dev_err(dd, "Failed updating EEPROM\n");420421free_bail:422vfree(buf);423bail:424return ret;425}426427/**428* qib_inc_eeprom_err - increment one of the four error counters429* that are logged to EEPROM.430* @dd: the qlogic_ib device431* @eidx: 0..3, the counter to increment432* @incr: how much to add433*434* Each counter is 8-bits, and saturates at 255 (0xFF). They435* are copied to the EEPROM (aka flash) whenever qib_update_eeprom_log()436* is called, but it can only be called in a context that allows sleep.437* This function can be called even at interrupt level.438*/439void qib_inc_eeprom_err(struct qib_devdata *dd, u32 eidx, u32 incr)440{441uint new_val;442unsigned long flags;443444spin_lock_irqsave(&dd->eep_st_lock, flags);445new_val = dd->eep_st_new_errs[eidx] + incr;446if (new_val > 255)447new_val = 255;448dd->eep_st_new_errs[eidx] = new_val;449spin_unlock_irqrestore(&dd->eep_st_lock, flags);450}451452453