18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2012 Intel Corporation. All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
48c2ecf20Sopenharmony_ci * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
78c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
88c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
98c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
108c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
138c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
148c2ecf20Sopenharmony_ci *     conditions are met:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
178c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
188c2ecf20Sopenharmony_ci *        disclaimer.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
218c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
228c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
238c2ecf20Sopenharmony_ci *        provided with the distribution.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
268c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
278c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
288c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
298c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
308c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
318c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
328c2ecf20Sopenharmony_ci * SOFTWARE.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/delay.h>
368c2ecf20Sopenharmony_ci#include <linux/pci.h>
378c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include "qib.h"
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/*
428c2ecf20Sopenharmony_ci * Functions specific to the serial EEPROM on cards handled by ib_qib.
438c2ecf20Sopenharmony_ci * The actual serail interface code is in qib_twsi.c. This file is a client
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/**
478c2ecf20Sopenharmony_ci * qib_eeprom_read - receives bytes from the eeprom via I2C
488c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
498c2ecf20Sopenharmony_ci * @eeprom_offset: address to read from
508c2ecf20Sopenharmony_ci * @buffer: where to store result
518c2ecf20Sopenharmony_ci * @len: number of bytes to receive
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ciint qib_eeprom_read(struct qib_devdata *dd, u8 eeprom_offset,
548c2ecf20Sopenharmony_ci		    void *buff, int len)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	int ret;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	ret = mutex_lock_interruptible(&dd->eep_lock);
598c2ecf20Sopenharmony_ci	if (!ret) {
608c2ecf20Sopenharmony_ci		ret = qib_twsi_reset(dd);
618c2ecf20Sopenharmony_ci		if (ret)
628c2ecf20Sopenharmony_ci			qib_dev_err(dd, "EEPROM Reset for read failed\n");
638c2ecf20Sopenharmony_ci		else
648c2ecf20Sopenharmony_ci			ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev,
658c2ecf20Sopenharmony_ci					      eeprom_offset, buff, len);
668c2ecf20Sopenharmony_ci		mutex_unlock(&dd->eep_lock);
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	return ret;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/*
738c2ecf20Sopenharmony_ci * Actually update the eeprom, first doing write enable if
748c2ecf20Sopenharmony_ci * needed, then restoring write enable state.
758c2ecf20Sopenharmony_ci * Must be called with eep_lock held
768c2ecf20Sopenharmony_ci */
778c2ecf20Sopenharmony_cistatic int eeprom_write_with_enable(struct qib_devdata *dd, u8 offset,
788c2ecf20Sopenharmony_ci		     const void *buf, int len)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	int ret, pwen;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	pwen = dd->f_eeprom_wen(dd, 1);
838c2ecf20Sopenharmony_ci	ret = qib_twsi_reset(dd);
848c2ecf20Sopenharmony_ci	if (ret)
858c2ecf20Sopenharmony_ci		qib_dev_err(dd, "EEPROM Reset for write failed\n");
868c2ecf20Sopenharmony_ci	else
878c2ecf20Sopenharmony_ci		ret = qib_twsi_blk_wr(dd, dd->twsi_eeprom_dev,
888c2ecf20Sopenharmony_ci				      offset, buf, len);
898c2ecf20Sopenharmony_ci	dd->f_eeprom_wen(dd, pwen);
908c2ecf20Sopenharmony_ci	return ret;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/**
948c2ecf20Sopenharmony_ci * qib_eeprom_write - writes data to the eeprom via I2C
958c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
968c2ecf20Sopenharmony_ci * @eeprom_offset: where to place data
978c2ecf20Sopenharmony_ci * @buffer: data to write
988c2ecf20Sopenharmony_ci * @len: number of bytes to write
998c2ecf20Sopenharmony_ci */
1008c2ecf20Sopenharmony_ciint qib_eeprom_write(struct qib_devdata *dd, u8 eeprom_offset,
1018c2ecf20Sopenharmony_ci		     const void *buff, int len)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	int ret;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	ret = mutex_lock_interruptible(&dd->eep_lock);
1068c2ecf20Sopenharmony_ci	if (!ret) {
1078c2ecf20Sopenharmony_ci		ret = eeprom_write_with_enable(dd, eeprom_offset, buff, len);
1088c2ecf20Sopenharmony_ci		mutex_unlock(&dd->eep_lock);
1098c2ecf20Sopenharmony_ci	}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	return ret;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic u8 flash_csum(struct qib_flash *ifp, int adjust)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	u8 *ip = (u8 *) ifp;
1178c2ecf20Sopenharmony_ci	u8 csum = 0, len;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/*
1208c2ecf20Sopenharmony_ci	 * Limit length checksummed to max length of actual data.
1218c2ecf20Sopenharmony_ci	 * Checksum of erased eeprom will still be bad, but we avoid
1228c2ecf20Sopenharmony_ci	 * reading past the end of the buffer we were passed.
1238c2ecf20Sopenharmony_ci	 */
1248c2ecf20Sopenharmony_ci	len = ifp->if_length;
1258c2ecf20Sopenharmony_ci	if (len > sizeof(struct qib_flash))
1268c2ecf20Sopenharmony_ci		len = sizeof(struct qib_flash);
1278c2ecf20Sopenharmony_ci	while (len--)
1288c2ecf20Sopenharmony_ci		csum += *ip++;
1298c2ecf20Sopenharmony_ci	csum -= ifp->if_csum;
1308c2ecf20Sopenharmony_ci	csum = ~csum;
1318c2ecf20Sopenharmony_ci	if (adjust)
1328c2ecf20Sopenharmony_ci		ifp->if_csum = csum;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return csum;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci/**
1388c2ecf20Sopenharmony_ci * qib_get_eeprom_info- get the GUID et al. from the TSWI EEPROM device
1398c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * We have the capability to use the nguid field, and get
1428c2ecf20Sopenharmony_ci * the guid from the first chip's flash, to use for all of them.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_civoid qib_get_eeprom_info(struct qib_devdata *dd)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	void *buf;
1478c2ecf20Sopenharmony_ci	struct qib_flash *ifp;
1488c2ecf20Sopenharmony_ci	__be64 guid;
1498c2ecf20Sopenharmony_ci	int len, eep_stat;
1508c2ecf20Sopenharmony_ci	u8 csum, *bguid;
1518c2ecf20Sopenharmony_ci	int t = dd->unit;
1528c2ecf20Sopenharmony_ci	struct qib_devdata *dd0 = qib_lookup(0);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (t && dd0->nguid > 1 && t <= dd0->nguid) {
1558c2ecf20Sopenharmony_ci		u8 oguid;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci		dd->base_guid = dd0->base_guid;
1588c2ecf20Sopenharmony_ci		bguid = (u8 *) &dd->base_guid;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci		oguid = bguid[7];
1618c2ecf20Sopenharmony_ci		bguid[7] += t;
1628c2ecf20Sopenharmony_ci		if (oguid > bguid[7]) {
1638c2ecf20Sopenharmony_ci			if (bguid[6] == 0xff) {
1648c2ecf20Sopenharmony_ci				if (bguid[5] == 0xff) {
1658c2ecf20Sopenharmony_ci					qib_dev_err(dd,
1668c2ecf20Sopenharmony_ci						    "Can't set GUID from base, wraps to OUI!\n");
1678c2ecf20Sopenharmony_ci					dd->base_guid = 0;
1688c2ecf20Sopenharmony_ci					goto bail;
1698c2ecf20Sopenharmony_ci				}
1708c2ecf20Sopenharmony_ci				bguid[5]++;
1718c2ecf20Sopenharmony_ci			}
1728c2ecf20Sopenharmony_ci			bguid[6]++;
1738c2ecf20Sopenharmony_ci		}
1748c2ecf20Sopenharmony_ci		dd->nguid = 1;
1758c2ecf20Sopenharmony_ci		goto bail;
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	/*
1798c2ecf20Sopenharmony_ci	 * Read full flash, not just currently used part, since it may have
1808c2ecf20Sopenharmony_ci	 * been written with a newer definition.
1818c2ecf20Sopenharmony_ci	 * */
1828c2ecf20Sopenharmony_ci	len = sizeof(struct qib_flash);
1838c2ecf20Sopenharmony_ci	buf = vmalloc(len);
1848c2ecf20Sopenharmony_ci	if (!buf)
1858c2ecf20Sopenharmony_ci		goto bail;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/*
1888c2ecf20Sopenharmony_ci	 * Use "public" eeprom read function, which does locking and
1898c2ecf20Sopenharmony_ci	 * figures out device. This will migrate to chip-specific.
1908c2ecf20Sopenharmony_ci	 */
1918c2ecf20Sopenharmony_ci	eep_stat = qib_eeprom_read(dd, 0, buf, len);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (eep_stat) {
1948c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed reading GUID from eeprom\n");
1958c2ecf20Sopenharmony_ci		goto done;
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci	ifp = (struct qib_flash *)buf;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	csum = flash_csum(ifp, 0);
2008c2ecf20Sopenharmony_ci	if (csum != ifp->if_csum) {
2018c2ecf20Sopenharmony_ci		qib_devinfo(dd->pcidev,
2028c2ecf20Sopenharmony_ci			"Bad I2C flash checksum: 0x%x, not 0x%x\n",
2038c2ecf20Sopenharmony_ci			csum, ifp->if_csum);
2048c2ecf20Sopenharmony_ci		goto done;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci	if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||
2078c2ecf20Sopenharmony_ci	    *(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {
2088c2ecf20Sopenharmony_ci		qib_dev_err(dd,
2098c2ecf20Sopenharmony_ci			"Invalid GUID %llx from flash; ignoring\n",
2108c2ecf20Sopenharmony_ci			*(unsigned long long *) ifp->if_guid);
2118c2ecf20Sopenharmony_ci		/* don't allow GUID if all 0 or all 1's */
2128c2ecf20Sopenharmony_ci		goto done;
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/* complain, but allow it */
2168c2ecf20Sopenharmony_ci	if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
2178c2ecf20Sopenharmony_ci		qib_devinfo(dd->pcidev,
2188c2ecf20Sopenharmony_ci			"Warning, GUID %llx is default, probably not correct!\n",
2198c2ecf20Sopenharmony_ci			*(unsigned long long *) ifp->if_guid);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	bguid = ifp->if_guid;
2228c2ecf20Sopenharmony_ci	if (!bguid[0] && !bguid[1] && !bguid[2]) {
2238c2ecf20Sopenharmony_ci		/*
2248c2ecf20Sopenharmony_ci		 * Original incorrect GUID format in flash; fix in
2258c2ecf20Sopenharmony_ci		 * core copy, by shifting up 2 octets; don't need to
2268c2ecf20Sopenharmony_ci		 * change top octet, since both it and shifted are 0.
2278c2ecf20Sopenharmony_ci		 */
2288c2ecf20Sopenharmony_ci		bguid[1] = bguid[3];
2298c2ecf20Sopenharmony_ci		bguid[2] = bguid[4];
2308c2ecf20Sopenharmony_ci		bguid[3] = 0;
2318c2ecf20Sopenharmony_ci		bguid[4] = 0;
2328c2ecf20Sopenharmony_ci		guid = *(__be64 *) ifp->if_guid;
2338c2ecf20Sopenharmony_ci	} else
2348c2ecf20Sopenharmony_ci		guid = *(__be64 *) ifp->if_guid;
2358c2ecf20Sopenharmony_ci	dd->base_guid = guid;
2368c2ecf20Sopenharmony_ci	dd->nguid = ifp->if_numguid;
2378c2ecf20Sopenharmony_ci	/*
2388c2ecf20Sopenharmony_ci	 * Things are slightly complicated by the desire to transparently
2398c2ecf20Sopenharmony_ci	 * support both the Pathscale 10-digit serial number and the QLogic
2408c2ecf20Sopenharmony_ci	 * 13-character version.
2418c2ecf20Sopenharmony_ci	 */
2428c2ecf20Sopenharmony_ci	if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] &&
2438c2ecf20Sopenharmony_ci	    ((u8 *) ifp->if_sprefix)[0] != 0xFF) {
2448c2ecf20Sopenharmony_ci		char *snp = dd->serial;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci		/*
2478c2ecf20Sopenharmony_ci		 * This board has a Serial-prefix, which is stored
2488c2ecf20Sopenharmony_ci		 * elsewhere for backward-compatibility.
2498c2ecf20Sopenharmony_ci		 */
2508c2ecf20Sopenharmony_ci		memcpy(snp, ifp->if_sprefix, sizeof(ifp->if_sprefix));
2518c2ecf20Sopenharmony_ci		snp[sizeof(ifp->if_sprefix)] = '\0';
2528c2ecf20Sopenharmony_ci		len = strlen(snp);
2538c2ecf20Sopenharmony_ci		snp += len;
2548c2ecf20Sopenharmony_ci		len = sizeof(dd->serial) - len;
2558c2ecf20Sopenharmony_ci		if (len > sizeof(ifp->if_serial))
2568c2ecf20Sopenharmony_ci			len = sizeof(ifp->if_serial);
2578c2ecf20Sopenharmony_ci		memcpy(snp, ifp->if_serial, len);
2588c2ecf20Sopenharmony_ci	} else {
2598c2ecf20Sopenharmony_ci		memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci	if (!strstr(ifp->if_comment, "Tested successfully"))
2628c2ecf20Sopenharmony_ci		qib_dev_err(dd,
2638c2ecf20Sopenharmony_ci			"Board SN %s did not pass functional test: %s\n",
2648c2ecf20Sopenharmony_ci			dd->serial, ifp->if_comment);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cidone:
2678c2ecf20Sopenharmony_ci	vfree(buf);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cibail:;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
272