Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2012 Intel Corporation. All rights reserved.
0003  * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
0004  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
0005  *
0006  * This software is available to you under a choice of one of two
0007  * licenses.  You may choose to be licensed under the terms of the GNU
0008  * General Public License (GPL) Version 2, available from the file
0009  * COPYING in the main directory of this source tree, or the
0010  * OpenIB.org BSD license below:
0011  *
0012  *     Redistribution and use in source and binary forms, with or
0013  *     without modification, are permitted provided that the following
0014  *     conditions are met:
0015  *
0016  *      - Redistributions of source code must retain the above
0017  *        copyright notice, this list of conditions and the following
0018  *        disclaimer.
0019  *
0020  *      - Redistributions in binary form must reproduce the above
0021  *        copyright notice, this list of conditions and the following
0022  *        disclaimer in the documentation and/or other materials
0023  *        provided with the distribution.
0024  *
0025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0026  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0027  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0028  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0029  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0030  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0031  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0032  * SOFTWARE.
0033  */
0034 
0035 #include <linux/delay.h>
0036 #include <linux/pci.h>
0037 #include <linux/vmalloc.h>
0038 
0039 #include "qib.h"
0040 
0041 /*
0042  * Functions specific to the serial EEPROM on cards handled by ib_qib.
0043  * The actual serail interface code is in qib_twsi.c. This file is a client
0044  */
0045 
0046 /**
0047  * qib_eeprom_read - receives bytes from the eeprom via I2C
0048  * @dd: the qlogic_ib device
0049  * @eeprom_offset: address to read from
0050  * @buff: where to store result
0051  * @len: number of bytes to receive
0052  */
0053 int qib_eeprom_read(struct qib_devdata *dd, u8 eeprom_offset,
0054             void *buff, int len)
0055 {
0056     int ret;
0057 
0058     ret = mutex_lock_interruptible(&dd->eep_lock);
0059     if (!ret) {
0060         ret = qib_twsi_reset(dd);
0061         if (ret)
0062             qib_dev_err(dd, "EEPROM Reset for read failed\n");
0063         else
0064             ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev,
0065                           eeprom_offset, buff, len);
0066         mutex_unlock(&dd->eep_lock);
0067     }
0068 
0069     return ret;
0070 }
0071 
0072 /*
0073  * Actually update the eeprom, first doing write enable if
0074  * needed, then restoring write enable state.
0075  * Must be called with eep_lock held
0076  */
0077 static int eeprom_write_with_enable(struct qib_devdata *dd, u8 offset,
0078              const void *buf, int len)
0079 {
0080     int ret, pwen;
0081 
0082     pwen = dd->f_eeprom_wen(dd, 1);
0083     ret = qib_twsi_reset(dd);
0084     if (ret)
0085         qib_dev_err(dd, "EEPROM Reset for write failed\n");
0086     else
0087         ret = qib_twsi_blk_wr(dd, dd->twsi_eeprom_dev,
0088                       offset, buf, len);
0089     dd->f_eeprom_wen(dd, pwen);
0090     return ret;
0091 }
0092 
0093 /**
0094  * qib_eeprom_write - writes data to the eeprom via I2C
0095  * @dd: the qlogic_ib device
0096  * @eeprom_offset: where to place data
0097  * @buff: data to write
0098  * @len: number of bytes to write
0099  */
0100 int qib_eeprom_write(struct qib_devdata *dd, u8 eeprom_offset,
0101              const void *buff, int len)
0102 {
0103     int ret;
0104 
0105     ret = mutex_lock_interruptible(&dd->eep_lock);
0106     if (!ret) {
0107         ret = eeprom_write_with_enable(dd, eeprom_offset, buff, len);
0108         mutex_unlock(&dd->eep_lock);
0109     }
0110 
0111     return ret;
0112 }
0113 
0114 static u8 flash_csum(struct qib_flash *ifp, int adjust)
0115 {
0116     u8 *ip = (u8 *) ifp;
0117     u8 csum = 0, len;
0118 
0119     /*
0120      * Limit length checksummed to max length of actual data.
0121      * Checksum of erased eeprom will still be bad, but we avoid
0122      * reading past the end of the buffer we were passed.
0123      */
0124     len = ifp->if_length;
0125     if (len > sizeof(struct qib_flash))
0126         len = sizeof(struct qib_flash);
0127     while (len--)
0128         csum += *ip++;
0129     csum -= ifp->if_csum;
0130     csum = ~csum;
0131     if (adjust)
0132         ifp->if_csum = csum;
0133 
0134     return csum;
0135 }
0136 
0137 /**
0138  * qib_get_eeprom_info- get the GUID et al. from the TSWI EEPROM device
0139  * @dd: the qlogic_ib device
0140  *
0141  * We have the capability to use the nguid field, and get
0142  * the guid from the first chip's flash, to use for all of them.
0143  */
0144 void qib_get_eeprom_info(struct qib_devdata *dd)
0145 {
0146     void *buf;
0147     struct qib_flash *ifp;
0148     __be64 guid;
0149     int len, eep_stat;
0150     u8 csum, *bguid;
0151     int t = dd->unit;
0152     struct qib_devdata *dd0 = qib_lookup(0);
0153 
0154     if (t && dd0->nguid > 1 && t <= dd0->nguid) {
0155         u8 oguid;
0156 
0157         dd->base_guid = dd0->base_guid;
0158         bguid = (u8 *) &dd->base_guid;
0159 
0160         oguid = bguid[7];
0161         bguid[7] += t;
0162         if (oguid > bguid[7]) {
0163             if (bguid[6] == 0xff) {
0164                 if (bguid[5] == 0xff) {
0165                     qib_dev_err(dd,
0166                             "Can't set GUID from base, wraps to OUI!\n");
0167                     dd->base_guid = 0;
0168                     goto bail;
0169                 }
0170                 bguid[5]++;
0171             }
0172             bguid[6]++;
0173         }
0174         dd->nguid = 1;
0175         goto bail;
0176     }
0177 
0178     /*
0179      * Read full flash, not just currently used part, since it may have
0180      * been written with a newer definition.
0181      * */
0182     len = sizeof(struct qib_flash);
0183     buf = vmalloc(len);
0184     if (!buf)
0185         goto bail;
0186 
0187     /*
0188      * Use "public" eeprom read function, which does locking and
0189      * figures out device. This will migrate to chip-specific.
0190      */
0191     eep_stat = qib_eeprom_read(dd, 0, buf, len);
0192 
0193     if (eep_stat) {
0194         qib_dev_err(dd, "Failed reading GUID from eeprom\n");
0195         goto done;
0196     }
0197     ifp = (struct qib_flash *)buf;
0198 
0199     csum = flash_csum(ifp, 0);
0200     if (csum != ifp->if_csum) {
0201         qib_devinfo(dd->pcidev,
0202             "Bad I2C flash checksum: 0x%x, not 0x%x\n",
0203             csum, ifp->if_csum);
0204         goto done;
0205     }
0206     if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||
0207         *(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {
0208         qib_dev_err(dd,
0209             "Invalid GUID %llx from flash; ignoring\n",
0210             *(unsigned long long *) ifp->if_guid);
0211         /* don't allow GUID if all 0 or all 1's */
0212         goto done;
0213     }
0214 
0215     /* complain, but allow it */
0216     if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
0217         qib_devinfo(dd->pcidev,
0218             "Warning, GUID %llx is default, probably not correct!\n",
0219             *(unsigned long long *) ifp->if_guid);
0220 
0221     bguid = ifp->if_guid;
0222     if (!bguid[0] && !bguid[1] && !bguid[2]) {
0223         /*
0224          * Original incorrect GUID format in flash; fix in
0225          * core copy, by shifting up 2 octets; don't need to
0226          * change top octet, since both it and shifted are 0.
0227          */
0228         bguid[1] = bguid[3];
0229         bguid[2] = bguid[4];
0230         bguid[3] = 0;
0231         bguid[4] = 0;
0232         guid = *(__be64 *) ifp->if_guid;
0233     } else
0234         guid = *(__be64 *) ifp->if_guid;
0235     dd->base_guid = guid;
0236     dd->nguid = ifp->if_numguid;
0237     /*
0238      * Things are slightly complicated by the desire to transparently
0239      * support both the Pathscale 10-digit serial number and the QLogic
0240      * 13-character version.
0241      */
0242     if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] &&
0243         ((u8 *) ifp->if_sprefix)[0] != 0xFF) {
0244         char *snp = dd->serial;
0245 
0246         /*
0247          * This board has a Serial-prefix, which is stored
0248          * elsewhere for backward-compatibility.
0249          */
0250         memcpy(snp, ifp->if_sprefix, sizeof(ifp->if_sprefix));
0251         snp[sizeof(ifp->if_sprefix)] = '\0';
0252         len = strlen(snp);
0253         snp += len;
0254         len = sizeof(dd->serial) - len;
0255         if (len > sizeof(ifp->if_serial))
0256             len = sizeof(ifp->if_serial);
0257         memcpy(snp, ifp->if_serial, len);
0258     } else {
0259         memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
0260     }
0261     if (!strstr(ifp->if_comment, "Tested successfully"))
0262         qib_dev_err(dd,
0263             "Board SN %s did not pass functional test: %s\n",
0264             dd->serial, ifp->if_comment);
0265 
0266 done:
0267     vfree(buf);
0268 
0269 bail:;
0270 }
0271