Path: blob/master/drivers/infiniband/hw/ipath/ipath_eeprom.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008 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 "ipath_kernel.h"3839/*40* InfiniPath I2C driver for a serial eeprom. This is not a generic41* I2C interface. For a start, the device we're using (Atmel AT24C11)42* doesn't work like a regular I2C device. It looks like one43* electrically, but not logically. Normal I2C devices have a single44* 7-bit or 10-bit I2C address that they respond to. Valid 7-bit45* addresses range from 0x03 to 0x77. Addresses 0x00 to 0x02 and 0x7846* to 0x7F are special reserved addresses (e.g. 0x00 is the "general47* call" address.) The Atmel device, on the other hand, responds to ALL48* 7-bit addresses. It's designed to be the only device on a given I2C49* bus. A 7-bit address corresponds to the memory address within the50* Atmel device itself.51*52* Also, the timing requirements mean more than simple software53* bitbanging, with readbacks from chip to ensure timing (simple udelay54* is not enough).55*56* This all means that accessing the device is specialized enough57* that using the standard kernel I2C bitbanging interface would be58* impossible. For example, the core I2C eeprom driver expects to find59* a device at one or more of a limited set of addresses only. It doesn't60* allow writing to an eeprom. It also doesn't provide any means of61* accessing eeprom contents from within the kernel, only via sysfs.62*/6364/* Added functionality for IBA7220-based cards */65#define IPATH_EEPROM_DEV_V1 0xA066#define IPATH_EEPROM_DEV_V2 0xA267#define IPATH_TEMP_DEV 0x9868#define IPATH_BAD_DEV (IPATH_EEPROM_DEV_V2+2)69#define IPATH_NO_DEV (0xFF)7071/*72* The number of I2C chains is proliferating. Table below brings73* some order to the madness. The basic principle is that the74* table is scanned from the top, and a "probe" is made to the75* device probe_dev. If that succeeds, the chain is considered76* to be of that type, and dd->i2c_chain_type is set to the index+177* of the entry.78* The +1 is so static initialization can mean "unknown, do probe."79*/80static struct i2c_chain_desc {81u8 probe_dev; /* If seen at probe, chain is this type */82u8 eeprom_dev; /* Dev addr (if any) for EEPROM */83u8 temp_dev; /* Dev Addr (if any) for Temp-sense */84} i2c_chains[] = {85{ IPATH_BAD_DEV, IPATH_NO_DEV, IPATH_NO_DEV }, /* pre-iba7220 bds */86{ IPATH_EEPROM_DEV_V1, IPATH_EEPROM_DEV_V1, IPATH_TEMP_DEV}, /* V1 */87{ IPATH_EEPROM_DEV_V2, IPATH_EEPROM_DEV_V2, IPATH_TEMP_DEV}, /* V2 */88{ IPATH_NO_DEV }89};9091enum i2c_type {92i2c_line_scl = 0,93i2c_line_sda94};9596enum i2c_state {97i2c_line_low = 0,98i2c_line_high99};100101#define READ_CMD 1102#define WRITE_CMD 0103104/**105* i2c_gpio_set - set a GPIO line106* @dd: the infinipath device107* @line: the line to set108* @new_line_state: the state to set109*110* Returns 0 if the line was set to the new state successfully, non-zero111* on error.112*/113static int i2c_gpio_set(struct ipath_devdata *dd,114enum i2c_type line,115enum i2c_state new_line_state)116{117u64 out_mask, dir_mask, *gpioval;118unsigned long flags = 0;119120gpioval = &dd->ipath_gpio_out;121122if (line == i2c_line_scl) {123dir_mask = dd->ipath_gpio_scl;124out_mask = (1UL << dd->ipath_gpio_scl_num);125} else {126dir_mask = dd->ipath_gpio_sda;127out_mask = (1UL << dd->ipath_gpio_sda_num);128}129130spin_lock_irqsave(&dd->ipath_gpio_lock, flags);131if (new_line_state == i2c_line_high) {132/* tri-state the output rather than force high */133dd->ipath_extctrl &= ~dir_mask;134} else {135/* config line to be an output */136dd->ipath_extctrl |= dir_mask;137}138ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl);139140/* set output as well (no real verify) */141if (new_line_state == i2c_line_high)142*gpioval |= out_mask;143else144*gpioval &= ~out_mask;145146ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_out, *gpioval);147spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);148149return 0;150}151152/**153* i2c_gpio_get - get a GPIO line state154* @dd: the infinipath device155* @line: the line to get156* @curr_statep: where to put the line state157*158* Returns 0 if the line was set to the new state successfully, non-zero159* on error. curr_state is not set on error.160*/161static int i2c_gpio_get(struct ipath_devdata *dd,162enum i2c_type line,163enum i2c_state *curr_statep)164{165u64 read_val, mask;166int ret;167unsigned long flags = 0;168169/* check args */170if (curr_statep == NULL) {171ret = 1;172goto bail;173}174175/* config line to be an input */176if (line == i2c_line_scl)177mask = dd->ipath_gpio_scl;178else179mask = dd->ipath_gpio_sda;180181spin_lock_irqsave(&dd->ipath_gpio_lock, flags);182dd->ipath_extctrl &= ~mask;183ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl);184/*185* Below is very unlikely to reflect true input state if Output186* Enable actually changed.187*/188read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extstatus);189spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);190191if (read_val & mask)192*curr_statep = i2c_line_high;193else194*curr_statep = i2c_line_low;195196ret = 0;197198bail:199return ret;200}201202/**203* i2c_wait_for_writes - wait for a write204* @dd: the infinipath device205*206* We use this instead of udelay directly, so we can make sure207* that previous register writes have been flushed all the way208* to the chip. Since we are delaying anyway, the cost doesn't209* hurt, and makes the bit twiddling more regular210*/211static void i2c_wait_for_writes(struct ipath_devdata *dd)212{213(void)ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch);214rmb();215}216217static void scl_out(struct ipath_devdata *dd, u8 bit)218{219udelay(1);220i2c_gpio_set(dd, i2c_line_scl, bit ? i2c_line_high : i2c_line_low);221222i2c_wait_for_writes(dd);223}224225static void sda_out(struct ipath_devdata *dd, u8 bit)226{227i2c_gpio_set(dd, i2c_line_sda, bit ? i2c_line_high : i2c_line_low);228229i2c_wait_for_writes(dd);230}231232static u8 sda_in(struct ipath_devdata *dd, int wait)233{234enum i2c_state bit;235236if (i2c_gpio_get(dd, i2c_line_sda, &bit))237ipath_dbg("get bit failed!\n");238239if (wait)240i2c_wait_for_writes(dd);241242return bit == i2c_line_high ? 1U : 0;243}244245/**246* i2c_ackrcv - see if ack following write is true247* @dd: the infinipath device248*/249static int i2c_ackrcv(struct ipath_devdata *dd)250{251u8 ack_received;252253/* AT ENTRY SCL = LOW */254/* change direction, ignore data */255ack_received = sda_in(dd, 1);256scl_out(dd, i2c_line_high);257ack_received = sda_in(dd, 1) == 0;258scl_out(dd, i2c_line_low);259return ack_received;260}261262/**263* rd_byte - read a byte, leaving ACK, STOP, etc up to caller264* @dd: the infinipath device265*266* Returns byte shifted out of device267*/268static int rd_byte(struct ipath_devdata *dd)269{270int bit_cntr, data;271272data = 0;273274for (bit_cntr = 7; bit_cntr >= 0; --bit_cntr) {275data <<= 1;276scl_out(dd, i2c_line_high);277data |= sda_in(dd, 0);278scl_out(dd, i2c_line_low);279}280return data;281}282283/**284* wr_byte - write a byte, one bit at a time285* @dd: the infinipath device286* @data: the byte to write287*288* Returns 0 if we got the following ack, otherwise 1289*/290static int wr_byte(struct ipath_devdata *dd, u8 data)291{292int bit_cntr;293u8 bit;294295for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {296bit = (data >> bit_cntr) & 1;297sda_out(dd, bit);298scl_out(dd, i2c_line_high);299scl_out(dd, i2c_line_low);300}301return (!i2c_ackrcv(dd)) ? 1 : 0;302}303304static void send_ack(struct ipath_devdata *dd)305{306sda_out(dd, i2c_line_low);307scl_out(dd, i2c_line_high);308scl_out(dd, i2c_line_low);309sda_out(dd, i2c_line_high);310}311312/**313* i2c_startcmd - transmit the start condition, followed by address/cmd314* @dd: the infinipath device315* @offset_dir: direction byte316*317* (both clock/data high, clock high, data low while clock is high)318*/319static int i2c_startcmd(struct ipath_devdata *dd, u8 offset_dir)320{321int res;322323/* issue start sequence */324sda_out(dd, i2c_line_high);325scl_out(dd, i2c_line_high);326sda_out(dd, i2c_line_low);327scl_out(dd, i2c_line_low);328329/* issue length and direction byte */330res = wr_byte(dd, offset_dir);331332if (res)333ipath_cdbg(VERBOSE, "No ack to complete start\n");334335return res;336}337338/**339* stop_cmd - transmit the stop condition340* @dd: the infinipath device341*342* (both clock/data low, clock high, data high while clock is high)343*/344static void stop_cmd(struct ipath_devdata *dd)345{346scl_out(dd, i2c_line_low);347sda_out(dd, i2c_line_low);348scl_out(dd, i2c_line_high);349sda_out(dd, i2c_line_high);350udelay(2);351}352353/**354* eeprom_reset - reset I2C communication355* @dd: the infinipath device356*/357358static int eeprom_reset(struct ipath_devdata *dd)359{360int clock_cycles_left = 9;361u64 *gpioval = &dd->ipath_gpio_out;362int ret;363unsigned long flags;364365spin_lock_irqsave(&dd->ipath_gpio_lock, flags);366/* Make sure shadows are consistent */367dd->ipath_extctrl = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);368*gpioval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_out);369spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);370371ipath_cdbg(VERBOSE, "Resetting i2c eeprom; initial gpioout reg "372"is %llx\n", (unsigned long long) *gpioval);373374/*375* This is to get the i2c into a known state, by first going low,376* then tristate sda (and then tristate scl as first thing377* in loop)378*/379scl_out(dd, i2c_line_low);380sda_out(dd, i2c_line_high);381382/* Clock up to 9 cycles looking for SDA hi, then issue START and STOP */383while (clock_cycles_left--) {384scl_out(dd, i2c_line_high);385386/* SDA seen high, issue START by dropping it while SCL high */387if (sda_in(dd, 0)) {388sda_out(dd, i2c_line_low);389scl_out(dd, i2c_line_low);390/* ATMEL spec says must be followed by STOP. */391scl_out(dd, i2c_line_high);392sda_out(dd, i2c_line_high);393ret = 0;394goto bail;395}396397scl_out(dd, i2c_line_low);398}399400ret = 1;401402bail:403return ret;404}405406/*407* Probe for I2C device at specified address. Returns 0 for "success"408* to match rest of this file.409* Leave bus in "reasonable" state for further commands.410*/411static int i2c_probe(struct ipath_devdata *dd, int devaddr)412{413int ret = 0;414415ret = eeprom_reset(dd);416if (ret) {417ipath_dev_err(dd, "Failed reset probing device 0x%02X\n",418devaddr);419return ret;420}421/*422* Reset no longer leaves bus in start condition, so normal423* i2c_startcmd() will do.424*/425ret = i2c_startcmd(dd, devaddr | READ_CMD);426if (ret)427ipath_cdbg(VERBOSE, "Failed startcmd for device 0x%02X\n",428devaddr);429else {430/*431* Device did respond. Complete a single-byte read, because some432* devices apparently cannot handle STOP immediately after they433* ACK the start-cmd.434*/435int data;436data = rd_byte(dd);437stop_cmd(dd);438ipath_cdbg(VERBOSE, "Response from device 0x%02X\n", devaddr);439}440return ret;441}442443/*444* Returns the "i2c type". This is a pointer to a struct that describes445* the I2C chain on this board. To minimize impact on struct ipath_devdata,446* the (small integer) index into the table is actually memoized, rather447* then the pointer.448* Memoization is because the type is determined on the first call per chip.449* An alternative would be to move type determination to early450* init code.451*/452static struct i2c_chain_desc *ipath_i2c_type(struct ipath_devdata *dd)453{454int idx;455456/* Get memoized index, from previous successful probes */457idx = dd->ipath_i2c_chain_type - 1;458if (idx >= 0 && idx < (ARRAY_SIZE(i2c_chains) - 1))459goto done;460461idx = 0;462while (i2c_chains[idx].probe_dev != IPATH_NO_DEV) {463/* if probe succeeds, this is type */464if (!i2c_probe(dd, i2c_chains[idx].probe_dev))465break;466++idx;467}468469/*470* Old EEPROM (first entry) may require a reset after probe,471* rather than being able to "start" after "stop"472*/473if (idx == 0)474eeprom_reset(dd);475476if (i2c_chains[idx].probe_dev == IPATH_NO_DEV)477idx = -1;478else479dd->ipath_i2c_chain_type = idx + 1;480done:481return (idx >= 0) ? i2c_chains + idx : NULL;482}483484static int ipath_eeprom_internal_read(struct ipath_devdata *dd,485u8 eeprom_offset, void *buffer, int len)486{487int ret;488struct i2c_chain_desc *icd;489u8 *bp = buffer;490491ret = 1;492icd = ipath_i2c_type(dd);493if (!icd)494goto bail;495496if (icd->eeprom_dev == IPATH_NO_DEV) {497/* legacy not-really-I2C */498ipath_cdbg(VERBOSE, "Start command only address\n");499eeprom_offset = (eeprom_offset << 1) | READ_CMD;500ret = i2c_startcmd(dd, eeprom_offset);501} else {502/* Actual I2C */503ipath_cdbg(VERBOSE, "Start command uses devaddr\n");504if (i2c_startcmd(dd, icd->eeprom_dev | WRITE_CMD)) {505ipath_dbg("Failed EEPROM startcmd\n");506stop_cmd(dd);507ret = 1;508goto bail;509}510ret = wr_byte(dd, eeprom_offset);511stop_cmd(dd);512if (ret) {513ipath_dev_err(dd, "Failed to write EEPROM address\n");514ret = 1;515goto bail;516}517ret = i2c_startcmd(dd, icd->eeprom_dev | READ_CMD);518}519if (ret) {520ipath_dbg("Failed startcmd for dev %02X\n", icd->eeprom_dev);521stop_cmd(dd);522ret = 1;523goto bail;524}525526/*527* eeprom keeps clocking data out as long as we ack, automatically528* incrementing the address.529*/530while (len-- > 0) {531/* get and store data */532*bp++ = rd_byte(dd);533/* send ack if not the last byte */534if (len)535send_ack(dd);536}537538stop_cmd(dd);539540ret = 0;541542bail:543return ret;544}545546static int ipath_eeprom_internal_write(struct ipath_devdata *dd, u8 eeprom_offset,547const void *buffer, int len)548{549int sub_len;550const u8 *bp = buffer;551int max_wait_time, i;552int ret;553struct i2c_chain_desc *icd;554555ret = 1;556icd = ipath_i2c_type(dd);557if (!icd)558goto bail;559560while (len > 0) {561if (icd->eeprom_dev == IPATH_NO_DEV) {562if (i2c_startcmd(dd,563(eeprom_offset << 1) | WRITE_CMD)) {564ipath_dbg("Failed to start cmd offset %u\n",565eeprom_offset);566goto failed_write;567}568} else {569/* Real I2C */570if (i2c_startcmd(dd, icd->eeprom_dev | WRITE_CMD)) {571ipath_dbg("Failed EEPROM startcmd\n");572goto failed_write;573}574ret = wr_byte(dd, eeprom_offset);575if (ret) {576ipath_dev_err(dd, "Failed to write EEPROM "577"address\n");578goto failed_write;579}580}581582sub_len = min(len, 4);583eeprom_offset += sub_len;584len -= sub_len;585586for (i = 0; i < sub_len; i++) {587if (wr_byte(dd, *bp++)) {588ipath_dbg("no ack after byte %u/%u (%u "589"total remain)\n", i, sub_len,590len + sub_len - i);591goto failed_write;592}593}594595stop_cmd(dd);596597/*598* wait for write complete by waiting for a successful599* read (the chip replies with a zero after the write600* cmd completes, and before it writes to the eeprom.601* The startcmd for the read will fail the ack until602* the writes have completed. We do this inline to avoid603* the debug prints that are in the real read routine604* if the startcmd fails.605* We also use the proper device address, so it doesn't matter606* whether we have real eeprom_dev. legacy likes any address.607*/608max_wait_time = 100;609while (i2c_startcmd(dd, icd->eeprom_dev | READ_CMD)) {610stop_cmd(dd);611if (!--max_wait_time) {612ipath_dbg("Did not get successful read to "613"complete write\n");614goto failed_write;615}616}617/* now read (and ignore) the resulting byte */618rd_byte(dd);619stop_cmd(dd);620}621622ret = 0;623goto bail;624625failed_write:626stop_cmd(dd);627ret = 1;628629bail:630return ret;631}632633/**634* ipath_eeprom_read - receives bytes from the eeprom via I2C635* @dd: the infinipath device636* @eeprom_offset: address to read from637* @buffer: where to store result638* @len: number of bytes to receive639*/640int ipath_eeprom_read(struct ipath_devdata *dd, u8 eeprom_offset,641void *buff, int len)642{643int ret;644645ret = mutex_lock_interruptible(&dd->ipath_eep_lock);646if (!ret) {647ret = ipath_eeprom_internal_read(dd, eeprom_offset, buff, len);648mutex_unlock(&dd->ipath_eep_lock);649}650651return ret;652}653654/**655* ipath_eeprom_write - writes data to the eeprom via I2C656* @dd: the infinipath device657* @eeprom_offset: where to place data658* @buffer: data to write659* @len: number of bytes to write660*/661int ipath_eeprom_write(struct ipath_devdata *dd, u8 eeprom_offset,662const void *buff, int len)663{664int ret;665666ret = mutex_lock_interruptible(&dd->ipath_eep_lock);667if (!ret) {668ret = ipath_eeprom_internal_write(dd, eeprom_offset, buff, len);669mutex_unlock(&dd->ipath_eep_lock);670}671672return ret;673}674675static u8 flash_csum(struct ipath_flash *ifp, int adjust)676{677u8 *ip = (u8 *) ifp;678u8 csum = 0, len;679680/*681* Limit length checksummed to max length of actual data.682* Checksum of erased eeprom will still be bad, but we avoid683* reading past the end of the buffer we were passed.684*/685len = ifp->if_length;686if (len > sizeof(struct ipath_flash))687len = sizeof(struct ipath_flash);688while (len--)689csum += *ip++;690csum -= ifp->if_csum;691csum = ~csum;692if (adjust)693ifp->if_csum = csum;694695return csum;696}697698/**699* ipath_get_guid - get the GUID from the i2c device700* @dd: the infinipath device701*702* We have the capability to use the ipath_nguid field, and get703* the guid from the first chip's flash, to use for all of them.704*/705void ipath_get_eeprom_info(struct ipath_devdata *dd)706{707void *buf;708struct ipath_flash *ifp;709__be64 guid;710int len, eep_stat;711u8 csum, *bguid;712int t = dd->ipath_unit;713struct ipath_devdata *dd0 = ipath_lookup(0);714715if (t && dd0->ipath_nguid > 1 && t <= dd0->ipath_nguid) {716u8 oguid;717dd->ipath_guid = dd0->ipath_guid;718bguid = (u8 *) & dd->ipath_guid;719720oguid = bguid[7];721bguid[7] += t;722if (oguid > bguid[7]) {723if (bguid[6] == 0xff) {724if (bguid[5] == 0xff) {725ipath_dev_err(726dd,727"Can't set %s GUID from "728"base, wraps to OUI!\n",729ipath_get_unit_name(t));730dd->ipath_guid = 0;731goto bail;732}733bguid[5]++;734}735bguid[6]++;736}737dd->ipath_nguid = 1;738739ipath_dbg("nguid %u, so adding %u to device 0 guid, "740"for %llx\n",741dd0->ipath_nguid, t,742(unsigned long long) be64_to_cpu(dd->ipath_guid));743goto bail;744}745746/*747* read full flash, not just currently used part, since it may have748* been written with a newer definition749* */750len = sizeof(struct ipath_flash);751buf = vmalloc(len);752if (!buf) {753ipath_dev_err(dd, "Couldn't allocate memory to read %u "754"bytes from eeprom for GUID\n", len);755goto bail;756}757758mutex_lock(&dd->ipath_eep_lock);759eep_stat = ipath_eeprom_internal_read(dd, 0, buf, len);760mutex_unlock(&dd->ipath_eep_lock);761762if (eep_stat) {763ipath_dev_err(dd, "Failed reading GUID from eeprom\n");764goto done;765}766ifp = (struct ipath_flash *)buf;767768csum = flash_csum(ifp, 0);769if (csum != ifp->if_csum) {770dev_info(&dd->pcidev->dev, "Bad I2C flash checksum: "771"0x%x, not 0x%x\n", csum, ifp->if_csum);772goto done;773}774if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||775*(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {776ipath_dev_err(dd, "Invalid GUID %llx from flash; "777"ignoring\n",778*(unsigned long long *) ifp->if_guid);779/* don't allow GUID if all 0 or all 1's */780goto done;781}782783/* complain, but allow it */784if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)785dev_info(&dd->pcidev->dev, "Warning, GUID %llx is "786"default, probably not correct!\n",787*(unsigned long long *) ifp->if_guid);788789bguid = ifp->if_guid;790if (!bguid[0] && !bguid[1] && !bguid[2]) {791/* original incorrect GUID format in flash; fix in792* core copy, by shifting up 2 octets; don't need to793* change top octet, since both it and shifted are794* 0.. */795bguid[1] = bguid[3];796bguid[2] = bguid[4];797bguid[3] = bguid[4] = 0;798guid = *(__be64 *) ifp->if_guid;799ipath_cdbg(VERBOSE, "Old GUID format in flash, top 3 zero, "800"shifting 2 octets\n");801} else802guid = *(__be64 *) ifp->if_guid;803dd->ipath_guid = guid;804dd->ipath_nguid = ifp->if_numguid;805/*806* Things are slightly complicated by the desire to transparently807* support both the Pathscale 10-digit serial number and the QLogic808* 13-character version.809*/810if ((ifp->if_fversion > 1) && ifp->if_sprefix[0]811&& ((u8 *)ifp->if_sprefix)[0] != 0xFF) {812/* This board has a Serial-prefix, which is stored813* elsewhere for backward-compatibility.814*/815char *snp = dd->ipath_serial;816memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix);817snp[sizeof ifp->if_sprefix] = '\0';818len = strlen(snp);819snp += len;820len = (sizeof dd->ipath_serial) - len;821if (len > sizeof ifp->if_serial) {822len = sizeof ifp->if_serial;823}824memcpy(snp, ifp->if_serial, len);825} else826memcpy(dd->ipath_serial, ifp->if_serial,827sizeof ifp->if_serial);828if (!strstr(ifp->if_comment, "Tested successfully"))829ipath_dev_err(dd, "Board SN %s did not pass functional "830"test: %s\n", dd->ipath_serial,831ifp->if_comment);832833ipath_cdbg(VERBOSE, "Initted GUID to %llx from eeprom\n",834(unsigned long long) be64_to_cpu(dd->ipath_guid));835836memcpy(&dd->ipath_eep_st_errs, &ifp->if_errcntp, IPATH_EEP_LOG_CNT);837/*838* Power-on (actually "active") hours are kept as little-endian value839* in EEPROM, but as seconds in a (possibly as small as 24-bit)840* atomic_t while running.841*/842atomic_set(&dd->ipath_active_time, 0);843dd->ipath_eep_hrs = ifp->if_powerhour[0] | (ifp->if_powerhour[1] << 8);844845done:846vfree(buf);847848bail:;849}850851/**852* ipath_update_eeprom_log - copy active-time and error counters to eeprom853* @dd: the infinipath device854*855* Although the time is kept as seconds in the ipath_devdata struct, it is856* rounded to hours for re-write, as we have only 16 bits in EEPROM.857* First-cut code reads whole (expected) struct ipath_flash, modifies,858* re-writes. Future direction: read/write only what we need, assuming859* that the EEPROM had to have been "good enough" for driver init, and860* if not, we aren't making it worse.861*862*/863864int ipath_update_eeprom_log(struct ipath_devdata *dd)865{866void *buf;867struct ipath_flash *ifp;868int len, hi_water;869uint32_t new_time, new_hrs;870u8 csum;871int ret, idx;872unsigned long flags;873874/* first, check if we actually need to do anything. */875ret = 0;876for (idx = 0; idx < IPATH_EEP_LOG_CNT; ++idx) {877if (dd->ipath_eep_st_new_errs[idx]) {878ret = 1;879break;880}881}882new_time = atomic_read(&dd->ipath_active_time);883884if (ret == 0 && new_time < 3600)885return 0;886887/*888* The quick-check above determined that there is something worthy889* of logging, so get current contents and do a more detailed idea.890* read full flash, not just currently used part, since it may have891* been written with a newer definition892*/893len = sizeof(struct ipath_flash);894buf = vmalloc(len);895ret = 1;896if (!buf) {897ipath_dev_err(dd, "Couldn't allocate memory to read %u "898"bytes from eeprom for logging\n", len);899goto bail;900}901902/* Grab semaphore and read current EEPROM. If we get an903* error, let go, but if not, keep it until we finish write.904*/905ret = mutex_lock_interruptible(&dd->ipath_eep_lock);906if (ret) {907ipath_dev_err(dd, "Unable to acquire EEPROM for logging\n");908goto free_bail;909}910ret = ipath_eeprom_internal_read(dd, 0, buf, len);911if (ret) {912mutex_unlock(&dd->ipath_eep_lock);913ipath_dev_err(dd, "Unable read EEPROM for logging\n");914goto free_bail;915}916ifp = (struct ipath_flash *)buf;917918csum = flash_csum(ifp, 0);919if (csum != ifp->if_csum) {920mutex_unlock(&dd->ipath_eep_lock);921ipath_dev_err(dd, "EEPROM cks err (0x%02X, S/B 0x%02X)\n",922csum, ifp->if_csum);923ret = 1;924goto free_bail;925}926hi_water = 0;927spin_lock_irqsave(&dd->ipath_eep_st_lock, flags);928for (idx = 0; idx < IPATH_EEP_LOG_CNT; ++idx) {929int new_val = dd->ipath_eep_st_new_errs[idx];930if (new_val) {931/*932* If we have seen any errors, add to EEPROM values933* We need to saturate at 0xFF (255) and we also934* would need to adjust the checksum if we were935* trying to minimize EEPROM traffic936* Note that we add to actual current count in EEPROM,937* in case it was altered while we were running.938*/939new_val += ifp->if_errcntp[idx];940if (new_val > 0xFF)941new_val = 0xFF;942if (ifp->if_errcntp[idx] != new_val) {943ifp->if_errcntp[idx] = new_val;944hi_water = offsetof(struct ipath_flash,945if_errcntp) + idx;946}947/*948* update our shadow (used to minimize EEPROM949* traffic), to match what we are about to write.950*/951dd->ipath_eep_st_errs[idx] = new_val;952dd->ipath_eep_st_new_errs[idx] = 0;953}954}955/*956* now update active-time. We would like to round to the nearest hour957* but unless atomic_t are sure to be proper signed ints we cannot,958* because we need to account for what we "transfer" to EEPROM and959* if we log an hour at 31 minutes, then we would need to set960* active_time to -29 to accurately count the _next_ hour.961*/962if (new_time >= 3600) {963new_hrs = new_time / 3600;964atomic_sub((new_hrs * 3600), &dd->ipath_active_time);965new_hrs += dd->ipath_eep_hrs;966if (new_hrs > 0xFFFF)967new_hrs = 0xFFFF;968dd->ipath_eep_hrs = new_hrs;969if ((new_hrs & 0xFF) != ifp->if_powerhour[0]) {970ifp->if_powerhour[0] = new_hrs & 0xFF;971hi_water = offsetof(struct ipath_flash, if_powerhour);972}973if ((new_hrs >> 8) != ifp->if_powerhour[1]) {974ifp->if_powerhour[1] = new_hrs >> 8;975hi_water = offsetof(struct ipath_flash, if_powerhour)976+ 1;977}978}979/*980* There is a tiny possibility that we could somehow fail to write981* the EEPROM after updating our shadows, but problems from holding982* the spinlock too long are a much bigger issue.983*/984spin_unlock_irqrestore(&dd->ipath_eep_st_lock, flags);985if (hi_water) {986/* we made some change to the data, uopdate cksum and write */987csum = flash_csum(ifp, 1);988ret = ipath_eeprom_internal_write(dd, 0, buf, hi_water + 1);989}990mutex_unlock(&dd->ipath_eep_lock);991if (ret)992ipath_dev_err(dd, "Failed updating EEPROM\n");993994free_bail:995vfree(buf);996bail:997return ret;998999}10001001/**1002* ipath_inc_eeprom_err - increment one of the four error counters1003* that are logged to EEPROM.1004* @dd: the infinipath device1005* @eidx: 0..3, the counter to increment1006* @incr: how much to add1007*1008* Each counter is 8-bits, and saturates at 255 (0xFF). They1009* are copied to the EEPROM (aka flash) whenever ipath_update_eeprom_log()1010* is called, but it can only be called in a context that allows sleep.1011* This function can be called even at interrupt level.1012*/10131014void ipath_inc_eeprom_err(struct ipath_devdata *dd, u32 eidx, u32 incr)1015{1016uint new_val;1017unsigned long flags;10181019spin_lock_irqsave(&dd->ipath_eep_st_lock, flags);1020new_val = dd->ipath_eep_st_new_errs[eidx] + incr;1021if (new_val > 255)1022new_val = 255;1023dd->ipath_eep_st_new_errs[eidx] = new_val;1024spin_unlock_irqrestore(&dd->ipath_eep_st_lock, flags);1025return;1026}10271028static int ipath_tempsense_internal_read(struct ipath_devdata *dd, u8 regnum)1029{1030int ret;1031struct i2c_chain_desc *icd;10321033ret = -ENOENT;10341035icd = ipath_i2c_type(dd);1036if (!icd)1037goto bail;10381039if (icd->temp_dev == IPATH_NO_DEV) {1040/* tempsense only exists on new, real-I2C boards */1041ret = -ENXIO;1042goto bail;1043}10441045if (i2c_startcmd(dd, icd->temp_dev | WRITE_CMD)) {1046ipath_dbg("Failed tempsense startcmd\n");1047stop_cmd(dd);1048ret = -ENXIO;1049goto bail;1050}1051ret = wr_byte(dd, regnum);1052stop_cmd(dd);1053if (ret) {1054ipath_dev_err(dd, "Failed tempsense WR command %02X\n",1055regnum);1056ret = -ENXIO;1057goto bail;1058}1059if (i2c_startcmd(dd, icd->temp_dev | READ_CMD)) {1060ipath_dbg("Failed tempsense RD startcmd\n");1061stop_cmd(dd);1062ret = -ENXIO;1063goto bail;1064}1065/*1066* We can only clock out one byte per command, sensibly1067*/1068ret = rd_byte(dd);1069stop_cmd(dd);10701071bail:1072return ret;1073}10741075#define VALID_TS_RD_REG_MASK 0xBF10761077/**1078* ipath_tempsense_read - read register of temp sensor via I2C1079* @dd: the infinipath device1080* @regnum: register to read from1081*1082* returns reg contents (0..255) or < 0 for error1083*/1084int ipath_tempsense_read(struct ipath_devdata *dd, u8 regnum)1085{1086int ret;10871088if (regnum > 7)1089return -EINVAL;10901091/* return a bogus value for (the one) register we do not have */1092if (!((1 << regnum) & VALID_TS_RD_REG_MASK))1093return 0;10941095ret = mutex_lock_interruptible(&dd->ipath_eep_lock);1096if (!ret) {1097ret = ipath_tempsense_internal_read(dd, regnum);1098mutex_unlock(&dd->ipath_eep_lock);1099}11001101/*1102* There are three possibilities here:1103* ret is actual value (0..255)1104* ret is -ENXIO or -EINVAL from code in this file1105* ret is -EINTR from mutex_lock_interruptible.1106*/1107return ret;1108}11091110static int ipath_tempsense_internal_write(struct ipath_devdata *dd,1111u8 regnum, u8 data)1112{1113int ret = -ENOENT;1114struct i2c_chain_desc *icd;11151116icd = ipath_i2c_type(dd);1117if (!icd)1118goto bail;11191120if (icd->temp_dev == IPATH_NO_DEV) {1121/* tempsense only exists on new, real-I2C boards */1122ret = -ENXIO;1123goto bail;1124}1125if (i2c_startcmd(dd, icd->temp_dev | WRITE_CMD)) {1126ipath_dbg("Failed tempsense startcmd\n");1127stop_cmd(dd);1128ret = -ENXIO;1129goto bail;1130}1131ret = wr_byte(dd, regnum);1132if (ret) {1133stop_cmd(dd);1134ipath_dev_err(dd, "Failed to write tempsense command %02X\n",1135regnum);1136ret = -ENXIO;1137goto bail;1138}1139ret = wr_byte(dd, data);1140stop_cmd(dd);1141ret = i2c_startcmd(dd, icd->temp_dev | READ_CMD);1142if (ret) {1143ipath_dev_err(dd, "Failed tempsense data wrt to %02X\n",1144regnum);1145ret = -ENXIO;1146}11471148bail:1149return ret;1150}11511152#define VALID_TS_WR_REG_MASK ((1 << 9) | (1 << 0xB) | (1 << 0xD))11531154/**1155* ipath_tempsense_write - write register of temp sensor via I2C1156* @dd: the infinipath device1157* @regnum: register to write1158* @data: data to write1159*1160* returns 0 for success or < 0 for error1161*/1162int ipath_tempsense_write(struct ipath_devdata *dd, u8 regnum, u8 data)1163{1164int ret;11651166if (regnum > 15 || !((1 << regnum) & VALID_TS_WR_REG_MASK))1167return -EINVAL;11681169ret = mutex_lock_interruptible(&dd->ipath_eep_lock);1170if (!ret) {1171ret = ipath_tempsense_internal_write(dd, regnum, data);1172mutex_unlock(&dd->ipath_eep_lock);1173}11741175/*1176* There are three possibilities here:1177* ret is 0 for success1178* ret is -ENXIO or -EINVAL from code in this file1179* ret is -EINTR from mutex_lock_interruptible.1180*/1181return ret;1182}118311841185