1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2012 Intel Corporation. All rights reserved.
3*4882a593Smuzhiyun * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
4*4882a593Smuzhiyun * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * This software is available to you under a choice of one of two
7*4882a593Smuzhiyun * licenses. You may choose to be licensed under the terms of the GNU
8*4882a593Smuzhiyun * General Public License (GPL) Version 2, available from the file
9*4882a593Smuzhiyun * COPYING in the main directory of this source tree, or the
10*4882a593Smuzhiyun * OpenIB.org BSD license below:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or
13*4882a593Smuzhiyun * without modification, are permitted provided that the following
14*4882a593Smuzhiyun * conditions are met:
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * - Redistributions of source code must retain the above
17*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
18*4882a593Smuzhiyun * disclaimer.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above
21*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
22*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials
23*4882a593Smuzhiyun * provided with the distribution.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28*4882a593Smuzhiyun * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29*4882a593Smuzhiyun * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30*4882a593Smuzhiyun * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31*4882a593Smuzhiyun * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32*4882a593Smuzhiyun * SOFTWARE.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #include <linux/delay.h>
36*4882a593Smuzhiyun #include <linux/pci.h>
37*4882a593Smuzhiyun #include <linux/vmalloc.h>
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #include "qib.h"
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun * Functions specific to the serial EEPROM on cards handled by ib_qib.
43*4882a593Smuzhiyun * The actual serail interface code is in qib_twsi.c. This file is a client
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /**
47*4882a593Smuzhiyun * qib_eeprom_read - receives bytes from the eeprom via I2C
48*4882a593Smuzhiyun * @dd: the qlogic_ib device
49*4882a593Smuzhiyun * @eeprom_offset: address to read from
50*4882a593Smuzhiyun * @buffer: where to store result
51*4882a593Smuzhiyun * @len: number of bytes to receive
52*4882a593Smuzhiyun */
qib_eeprom_read(struct qib_devdata * dd,u8 eeprom_offset,void * buff,int len)53*4882a593Smuzhiyun int qib_eeprom_read(struct qib_devdata *dd, u8 eeprom_offset,
54*4882a593Smuzhiyun void *buff, int len)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun int ret;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun ret = mutex_lock_interruptible(&dd->eep_lock);
59*4882a593Smuzhiyun if (!ret) {
60*4882a593Smuzhiyun ret = qib_twsi_reset(dd);
61*4882a593Smuzhiyun if (ret)
62*4882a593Smuzhiyun qib_dev_err(dd, "EEPROM Reset for read failed\n");
63*4882a593Smuzhiyun else
64*4882a593Smuzhiyun ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev,
65*4882a593Smuzhiyun eeprom_offset, buff, len);
66*4882a593Smuzhiyun mutex_unlock(&dd->eep_lock);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun return ret;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun * Actually update the eeprom, first doing write enable if
74*4882a593Smuzhiyun * needed, then restoring write enable state.
75*4882a593Smuzhiyun * Must be called with eep_lock held
76*4882a593Smuzhiyun */
eeprom_write_with_enable(struct qib_devdata * dd,u8 offset,const void * buf,int len)77*4882a593Smuzhiyun static int eeprom_write_with_enable(struct qib_devdata *dd, u8 offset,
78*4882a593Smuzhiyun const void *buf, int len)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun int ret, pwen;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun pwen = dd->f_eeprom_wen(dd, 1);
83*4882a593Smuzhiyun ret = qib_twsi_reset(dd);
84*4882a593Smuzhiyun if (ret)
85*4882a593Smuzhiyun qib_dev_err(dd, "EEPROM Reset for write failed\n");
86*4882a593Smuzhiyun else
87*4882a593Smuzhiyun ret = qib_twsi_blk_wr(dd, dd->twsi_eeprom_dev,
88*4882a593Smuzhiyun offset, buf, len);
89*4882a593Smuzhiyun dd->f_eeprom_wen(dd, pwen);
90*4882a593Smuzhiyun return ret;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /**
94*4882a593Smuzhiyun * qib_eeprom_write - writes data to the eeprom via I2C
95*4882a593Smuzhiyun * @dd: the qlogic_ib device
96*4882a593Smuzhiyun * @eeprom_offset: where to place data
97*4882a593Smuzhiyun * @buffer: data to write
98*4882a593Smuzhiyun * @len: number of bytes to write
99*4882a593Smuzhiyun */
qib_eeprom_write(struct qib_devdata * dd,u8 eeprom_offset,const void * buff,int len)100*4882a593Smuzhiyun int qib_eeprom_write(struct qib_devdata *dd, u8 eeprom_offset,
101*4882a593Smuzhiyun const void *buff, int len)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun int ret;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun ret = mutex_lock_interruptible(&dd->eep_lock);
106*4882a593Smuzhiyun if (!ret) {
107*4882a593Smuzhiyun ret = eeprom_write_with_enable(dd, eeprom_offset, buff, len);
108*4882a593Smuzhiyun mutex_unlock(&dd->eep_lock);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun return ret;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
flash_csum(struct qib_flash * ifp,int adjust)114*4882a593Smuzhiyun static u8 flash_csum(struct qib_flash *ifp, int adjust)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun u8 *ip = (u8 *) ifp;
117*4882a593Smuzhiyun u8 csum = 0, len;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * Limit length checksummed to max length of actual data.
121*4882a593Smuzhiyun * Checksum of erased eeprom will still be bad, but we avoid
122*4882a593Smuzhiyun * reading past the end of the buffer we were passed.
123*4882a593Smuzhiyun */
124*4882a593Smuzhiyun len = ifp->if_length;
125*4882a593Smuzhiyun if (len > sizeof(struct qib_flash))
126*4882a593Smuzhiyun len = sizeof(struct qib_flash);
127*4882a593Smuzhiyun while (len--)
128*4882a593Smuzhiyun csum += *ip++;
129*4882a593Smuzhiyun csum -= ifp->if_csum;
130*4882a593Smuzhiyun csum = ~csum;
131*4882a593Smuzhiyun if (adjust)
132*4882a593Smuzhiyun ifp->if_csum = csum;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun return csum;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /**
138*4882a593Smuzhiyun * qib_get_eeprom_info- get the GUID et al. from the TSWI EEPROM device
139*4882a593Smuzhiyun * @dd: the qlogic_ib device
140*4882a593Smuzhiyun *
141*4882a593Smuzhiyun * We have the capability to use the nguid field, and get
142*4882a593Smuzhiyun * the guid from the first chip's flash, to use for all of them.
143*4882a593Smuzhiyun */
qib_get_eeprom_info(struct qib_devdata * dd)144*4882a593Smuzhiyun void qib_get_eeprom_info(struct qib_devdata *dd)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun void *buf;
147*4882a593Smuzhiyun struct qib_flash *ifp;
148*4882a593Smuzhiyun __be64 guid;
149*4882a593Smuzhiyun int len, eep_stat;
150*4882a593Smuzhiyun u8 csum, *bguid;
151*4882a593Smuzhiyun int t = dd->unit;
152*4882a593Smuzhiyun struct qib_devdata *dd0 = qib_lookup(0);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun if (t && dd0->nguid > 1 && t <= dd0->nguid) {
155*4882a593Smuzhiyun u8 oguid;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun dd->base_guid = dd0->base_guid;
158*4882a593Smuzhiyun bguid = (u8 *) &dd->base_guid;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun oguid = bguid[7];
161*4882a593Smuzhiyun bguid[7] += t;
162*4882a593Smuzhiyun if (oguid > bguid[7]) {
163*4882a593Smuzhiyun if (bguid[6] == 0xff) {
164*4882a593Smuzhiyun if (bguid[5] == 0xff) {
165*4882a593Smuzhiyun qib_dev_err(dd,
166*4882a593Smuzhiyun "Can't set GUID from base, wraps to OUI!\n");
167*4882a593Smuzhiyun dd->base_guid = 0;
168*4882a593Smuzhiyun goto bail;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun bguid[5]++;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun bguid[6]++;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun dd->nguid = 1;
175*4882a593Smuzhiyun goto bail;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /*
179*4882a593Smuzhiyun * Read full flash, not just currently used part, since it may have
180*4882a593Smuzhiyun * been written with a newer definition.
181*4882a593Smuzhiyun * */
182*4882a593Smuzhiyun len = sizeof(struct qib_flash);
183*4882a593Smuzhiyun buf = vmalloc(len);
184*4882a593Smuzhiyun if (!buf)
185*4882a593Smuzhiyun goto bail;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun * Use "public" eeprom read function, which does locking and
189*4882a593Smuzhiyun * figures out device. This will migrate to chip-specific.
190*4882a593Smuzhiyun */
191*4882a593Smuzhiyun eep_stat = qib_eeprom_read(dd, 0, buf, len);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun if (eep_stat) {
194*4882a593Smuzhiyun qib_dev_err(dd, "Failed reading GUID from eeprom\n");
195*4882a593Smuzhiyun goto done;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun ifp = (struct qib_flash *)buf;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun csum = flash_csum(ifp, 0);
200*4882a593Smuzhiyun if (csum != ifp->if_csum) {
201*4882a593Smuzhiyun qib_devinfo(dd->pcidev,
202*4882a593Smuzhiyun "Bad I2C flash checksum: 0x%x, not 0x%x\n",
203*4882a593Smuzhiyun csum, ifp->if_csum);
204*4882a593Smuzhiyun goto done;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||
207*4882a593Smuzhiyun *(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {
208*4882a593Smuzhiyun qib_dev_err(dd,
209*4882a593Smuzhiyun "Invalid GUID %llx from flash; ignoring\n",
210*4882a593Smuzhiyun *(unsigned long long *) ifp->if_guid);
211*4882a593Smuzhiyun /* don't allow GUID if all 0 or all 1's */
212*4882a593Smuzhiyun goto done;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* complain, but allow it */
216*4882a593Smuzhiyun if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
217*4882a593Smuzhiyun qib_devinfo(dd->pcidev,
218*4882a593Smuzhiyun "Warning, GUID %llx is default, probably not correct!\n",
219*4882a593Smuzhiyun *(unsigned long long *) ifp->if_guid);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun bguid = ifp->if_guid;
222*4882a593Smuzhiyun if (!bguid[0] && !bguid[1] && !bguid[2]) {
223*4882a593Smuzhiyun /*
224*4882a593Smuzhiyun * Original incorrect GUID format in flash; fix in
225*4882a593Smuzhiyun * core copy, by shifting up 2 octets; don't need to
226*4882a593Smuzhiyun * change top octet, since both it and shifted are 0.
227*4882a593Smuzhiyun */
228*4882a593Smuzhiyun bguid[1] = bguid[3];
229*4882a593Smuzhiyun bguid[2] = bguid[4];
230*4882a593Smuzhiyun bguid[3] = 0;
231*4882a593Smuzhiyun bguid[4] = 0;
232*4882a593Smuzhiyun guid = *(__be64 *) ifp->if_guid;
233*4882a593Smuzhiyun } else
234*4882a593Smuzhiyun guid = *(__be64 *) ifp->if_guid;
235*4882a593Smuzhiyun dd->base_guid = guid;
236*4882a593Smuzhiyun dd->nguid = ifp->if_numguid;
237*4882a593Smuzhiyun /*
238*4882a593Smuzhiyun * Things are slightly complicated by the desire to transparently
239*4882a593Smuzhiyun * support both the Pathscale 10-digit serial number and the QLogic
240*4882a593Smuzhiyun * 13-character version.
241*4882a593Smuzhiyun */
242*4882a593Smuzhiyun if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] &&
243*4882a593Smuzhiyun ((u8 *) ifp->if_sprefix)[0] != 0xFF) {
244*4882a593Smuzhiyun char *snp = dd->serial;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /*
247*4882a593Smuzhiyun * This board has a Serial-prefix, which is stored
248*4882a593Smuzhiyun * elsewhere for backward-compatibility.
249*4882a593Smuzhiyun */
250*4882a593Smuzhiyun memcpy(snp, ifp->if_sprefix, sizeof(ifp->if_sprefix));
251*4882a593Smuzhiyun snp[sizeof(ifp->if_sprefix)] = '\0';
252*4882a593Smuzhiyun len = strlen(snp);
253*4882a593Smuzhiyun snp += len;
254*4882a593Smuzhiyun len = sizeof(dd->serial) - len;
255*4882a593Smuzhiyun if (len > sizeof(ifp->if_serial))
256*4882a593Smuzhiyun len = sizeof(ifp->if_serial);
257*4882a593Smuzhiyun memcpy(snp, ifp->if_serial, len);
258*4882a593Smuzhiyun } else {
259*4882a593Smuzhiyun memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun if (!strstr(ifp->if_comment, "Tested successfully"))
262*4882a593Smuzhiyun qib_dev_err(dd,
263*4882a593Smuzhiyun "Board SN %s did not pass functional test: %s\n",
264*4882a593Smuzhiyun dd->serial, ifp->if_comment);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun done:
267*4882a593Smuzhiyun vfree(buf);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun bail:;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272