162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2012 Intel Corporation. All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
462306a36Sopenharmony_ci * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This software is available to you under a choice of one of two
762306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
862306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
962306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
1062306a36Sopenharmony_ci * OpenIB.org BSD license below:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1362306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1462306a36Sopenharmony_ci *     conditions are met:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1762306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1862306a36Sopenharmony_ci *        disclaimer.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2162306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2262306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2362306a36Sopenharmony_ci *        provided with the distribution.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2662306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2762306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2862306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2962306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3062306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3162306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3262306a36Sopenharmony_ci * SOFTWARE.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include <linux/delay.h>
3662306a36Sopenharmony_ci#include <linux/pci.h>
3762306a36Sopenharmony_ci#include <linux/vmalloc.h>
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#include "qib.h"
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/*
4262306a36Sopenharmony_ci * Functions specific to the serial EEPROM on cards handled by ib_qib.
4362306a36Sopenharmony_ci * The actual serail interface code is in qib_twsi.c. This file is a client
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/**
4762306a36Sopenharmony_ci * qib_eeprom_read - receives bytes from the eeprom via I2C
4862306a36Sopenharmony_ci * @dd: the qlogic_ib device
4962306a36Sopenharmony_ci * @eeprom_offset: address to read from
5062306a36Sopenharmony_ci * @buff: where to store result
5162306a36Sopenharmony_ci * @len: number of bytes to receive
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_ciint qib_eeprom_read(struct qib_devdata *dd, u8 eeprom_offset,
5462306a36Sopenharmony_ci		    void *buff, int len)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	int ret;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	ret = mutex_lock_interruptible(&dd->eep_lock);
5962306a36Sopenharmony_ci	if (!ret) {
6062306a36Sopenharmony_ci		ret = qib_twsi_reset(dd);
6162306a36Sopenharmony_ci		if (ret)
6262306a36Sopenharmony_ci			qib_dev_err(dd, "EEPROM Reset for read failed\n");
6362306a36Sopenharmony_ci		else
6462306a36Sopenharmony_ci			ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev,
6562306a36Sopenharmony_ci					      eeprom_offset, buff, len);
6662306a36Sopenharmony_ci		mutex_unlock(&dd->eep_lock);
6762306a36Sopenharmony_ci	}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	return ret;
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/*
7362306a36Sopenharmony_ci * Actually update the eeprom, first doing write enable if
7462306a36Sopenharmony_ci * needed, then restoring write enable state.
7562306a36Sopenharmony_ci * Must be called with eep_lock held
7662306a36Sopenharmony_ci */
7762306a36Sopenharmony_cistatic int eeprom_write_with_enable(struct qib_devdata *dd, u8 offset,
7862306a36Sopenharmony_ci		     const void *buf, int len)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	int ret, pwen;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	pwen = dd->f_eeprom_wen(dd, 1);
8362306a36Sopenharmony_ci	ret = qib_twsi_reset(dd);
8462306a36Sopenharmony_ci	if (ret)
8562306a36Sopenharmony_ci		qib_dev_err(dd, "EEPROM Reset for write failed\n");
8662306a36Sopenharmony_ci	else
8762306a36Sopenharmony_ci		ret = qib_twsi_blk_wr(dd, dd->twsi_eeprom_dev,
8862306a36Sopenharmony_ci				      offset, buf, len);
8962306a36Sopenharmony_ci	dd->f_eeprom_wen(dd, pwen);
9062306a36Sopenharmony_ci	return ret;
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci/**
9462306a36Sopenharmony_ci * qib_eeprom_write - writes data to the eeprom via I2C
9562306a36Sopenharmony_ci * @dd: the qlogic_ib device
9662306a36Sopenharmony_ci * @eeprom_offset: where to place data
9762306a36Sopenharmony_ci * @buff: data to write
9862306a36Sopenharmony_ci * @len: number of bytes to write
9962306a36Sopenharmony_ci */
10062306a36Sopenharmony_ciint qib_eeprom_write(struct qib_devdata *dd, u8 eeprom_offset,
10162306a36Sopenharmony_ci		     const void *buff, int len)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	int ret;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	ret = mutex_lock_interruptible(&dd->eep_lock);
10662306a36Sopenharmony_ci	if (!ret) {
10762306a36Sopenharmony_ci		ret = eeprom_write_with_enable(dd, eeprom_offset, buff, len);
10862306a36Sopenharmony_ci		mutex_unlock(&dd->eep_lock);
10962306a36Sopenharmony_ci	}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	return ret;
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic u8 flash_csum(struct qib_flash *ifp, int adjust)
11562306a36Sopenharmony_ci{
11662306a36Sopenharmony_ci	u8 *ip = (u8 *) ifp;
11762306a36Sopenharmony_ci	u8 csum = 0, len;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	/*
12062306a36Sopenharmony_ci	 * Limit length checksummed to max length of actual data.
12162306a36Sopenharmony_ci	 * Checksum of erased eeprom will still be bad, but we avoid
12262306a36Sopenharmony_ci	 * reading past the end of the buffer we were passed.
12362306a36Sopenharmony_ci	 */
12462306a36Sopenharmony_ci	len = ifp->if_length;
12562306a36Sopenharmony_ci	if (len > sizeof(struct qib_flash))
12662306a36Sopenharmony_ci		len = sizeof(struct qib_flash);
12762306a36Sopenharmony_ci	while (len--)
12862306a36Sopenharmony_ci		csum += *ip++;
12962306a36Sopenharmony_ci	csum -= ifp->if_csum;
13062306a36Sopenharmony_ci	csum = ~csum;
13162306a36Sopenharmony_ci	if (adjust)
13262306a36Sopenharmony_ci		ifp->if_csum = csum;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	return csum;
13562306a36Sopenharmony_ci}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci/**
13862306a36Sopenharmony_ci * qib_get_eeprom_info- get the GUID et al. from the TSWI EEPROM device
13962306a36Sopenharmony_ci * @dd: the qlogic_ib device
14062306a36Sopenharmony_ci *
14162306a36Sopenharmony_ci * We have the capability to use the nguid field, and get
14262306a36Sopenharmony_ci * the guid from the first chip's flash, to use for all of them.
14362306a36Sopenharmony_ci */
14462306a36Sopenharmony_civoid qib_get_eeprom_info(struct qib_devdata *dd)
14562306a36Sopenharmony_ci{
14662306a36Sopenharmony_ci	void *buf;
14762306a36Sopenharmony_ci	struct qib_flash *ifp;
14862306a36Sopenharmony_ci	__be64 guid;
14962306a36Sopenharmony_ci	int len, eep_stat;
15062306a36Sopenharmony_ci	u8 csum, *bguid;
15162306a36Sopenharmony_ci	int t = dd->unit;
15262306a36Sopenharmony_ci	struct qib_devdata *dd0 = qib_lookup(0);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	if (t && dd0->nguid > 1 && t <= dd0->nguid) {
15562306a36Sopenharmony_ci		u8 oguid;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci		dd->base_guid = dd0->base_guid;
15862306a36Sopenharmony_ci		bguid = (u8 *) &dd->base_guid;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci		oguid = bguid[7];
16162306a36Sopenharmony_ci		bguid[7] += t;
16262306a36Sopenharmony_ci		if (oguid > bguid[7]) {
16362306a36Sopenharmony_ci			if (bguid[6] == 0xff) {
16462306a36Sopenharmony_ci				if (bguid[5] == 0xff) {
16562306a36Sopenharmony_ci					qib_dev_err(dd,
16662306a36Sopenharmony_ci						    "Can't set GUID from base, wraps to OUI!\n");
16762306a36Sopenharmony_ci					dd->base_guid = 0;
16862306a36Sopenharmony_ci					goto bail;
16962306a36Sopenharmony_ci				}
17062306a36Sopenharmony_ci				bguid[5]++;
17162306a36Sopenharmony_ci			}
17262306a36Sopenharmony_ci			bguid[6]++;
17362306a36Sopenharmony_ci		}
17462306a36Sopenharmony_ci		dd->nguid = 1;
17562306a36Sopenharmony_ci		goto bail;
17662306a36Sopenharmony_ci	}
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	/*
17962306a36Sopenharmony_ci	 * Read full flash, not just currently used part, since it may have
18062306a36Sopenharmony_ci	 * been written with a newer definition.
18162306a36Sopenharmony_ci	 * */
18262306a36Sopenharmony_ci	len = sizeof(struct qib_flash);
18362306a36Sopenharmony_ci	buf = vmalloc(len);
18462306a36Sopenharmony_ci	if (!buf)
18562306a36Sopenharmony_ci		goto bail;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	/*
18862306a36Sopenharmony_ci	 * Use "public" eeprom read function, which does locking and
18962306a36Sopenharmony_ci	 * figures out device. This will migrate to chip-specific.
19062306a36Sopenharmony_ci	 */
19162306a36Sopenharmony_ci	eep_stat = qib_eeprom_read(dd, 0, buf, len);
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	if (eep_stat) {
19462306a36Sopenharmony_ci		qib_dev_err(dd, "Failed reading GUID from eeprom\n");
19562306a36Sopenharmony_ci		goto done;
19662306a36Sopenharmony_ci	}
19762306a36Sopenharmony_ci	ifp = (struct qib_flash *)buf;
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	csum = flash_csum(ifp, 0);
20062306a36Sopenharmony_ci	if (csum != ifp->if_csum) {
20162306a36Sopenharmony_ci		qib_devinfo(dd->pcidev,
20262306a36Sopenharmony_ci			"Bad I2C flash checksum: 0x%x, not 0x%x\n",
20362306a36Sopenharmony_ci			csum, ifp->if_csum);
20462306a36Sopenharmony_ci		goto done;
20562306a36Sopenharmony_ci	}
20662306a36Sopenharmony_ci	if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||
20762306a36Sopenharmony_ci	    *(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {
20862306a36Sopenharmony_ci		qib_dev_err(dd,
20962306a36Sopenharmony_ci			"Invalid GUID %llx from flash; ignoring\n",
21062306a36Sopenharmony_ci			*(unsigned long long *) ifp->if_guid);
21162306a36Sopenharmony_ci		/* don't allow GUID if all 0 or all 1's */
21262306a36Sopenharmony_ci		goto done;
21362306a36Sopenharmony_ci	}
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	/* complain, but allow it */
21662306a36Sopenharmony_ci	if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
21762306a36Sopenharmony_ci		qib_devinfo(dd->pcidev,
21862306a36Sopenharmony_ci			"Warning, GUID %llx is default, probably not correct!\n",
21962306a36Sopenharmony_ci			*(unsigned long long *) ifp->if_guid);
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	bguid = ifp->if_guid;
22262306a36Sopenharmony_ci	if (!bguid[0] && !bguid[1] && !bguid[2]) {
22362306a36Sopenharmony_ci		/*
22462306a36Sopenharmony_ci		 * Original incorrect GUID format in flash; fix in
22562306a36Sopenharmony_ci		 * core copy, by shifting up 2 octets; don't need to
22662306a36Sopenharmony_ci		 * change top octet, since both it and shifted are 0.
22762306a36Sopenharmony_ci		 */
22862306a36Sopenharmony_ci		bguid[1] = bguid[3];
22962306a36Sopenharmony_ci		bguid[2] = bguid[4];
23062306a36Sopenharmony_ci		bguid[3] = 0;
23162306a36Sopenharmony_ci		bguid[4] = 0;
23262306a36Sopenharmony_ci		guid = *(__be64 *) ifp->if_guid;
23362306a36Sopenharmony_ci	} else
23462306a36Sopenharmony_ci		guid = *(__be64 *) ifp->if_guid;
23562306a36Sopenharmony_ci	dd->base_guid = guid;
23662306a36Sopenharmony_ci	dd->nguid = ifp->if_numguid;
23762306a36Sopenharmony_ci	/*
23862306a36Sopenharmony_ci	 * Things are slightly complicated by the desire to transparently
23962306a36Sopenharmony_ci	 * support both the Pathscale 10-digit serial number and the QLogic
24062306a36Sopenharmony_ci	 * 13-character version.
24162306a36Sopenharmony_ci	 */
24262306a36Sopenharmony_ci	if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] &&
24362306a36Sopenharmony_ci	    ((u8 *) ifp->if_sprefix)[0] != 0xFF) {
24462306a36Sopenharmony_ci		char *snp = dd->serial;
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci		/*
24762306a36Sopenharmony_ci		 * This board has a Serial-prefix, which is stored
24862306a36Sopenharmony_ci		 * elsewhere for backward-compatibility.
24962306a36Sopenharmony_ci		 */
25062306a36Sopenharmony_ci		memcpy(snp, ifp->if_sprefix, sizeof(ifp->if_sprefix));
25162306a36Sopenharmony_ci		snp[sizeof(ifp->if_sprefix)] = '\0';
25262306a36Sopenharmony_ci		len = strlen(snp);
25362306a36Sopenharmony_ci		snp += len;
25462306a36Sopenharmony_ci		len = sizeof(dd->serial) - len;
25562306a36Sopenharmony_ci		if (len > sizeof(ifp->if_serial))
25662306a36Sopenharmony_ci			len = sizeof(ifp->if_serial);
25762306a36Sopenharmony_ci		memcpy(snp, ifp->if_serial, len);
25862306a36Sopenharmony_ci	} else {
25962306a36Sopenharmony_ci		memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
26062306a36Sopenharmony_ci	}
26162306a36Sopenharmony_ci	if (!strstr(ifp->if_comment, "Tested successfully"))
26262306a36Sopenharmony_ci		qib_dev_err(dd,
26362306a36Sopenharmony_ci			"Board SN %s did not pass functional test: %s\n",
26462306a36Sopenharmony_ci			dd->serial, ifp->if_comment);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_cidone:
26762306a36Sopenharmony_ci	vfree(buf);
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_cibail:;
27062306a36Sopenharmony_ci}
27162306a36Sopenharmony_ci
272