Path: blob/master/drivers/infiniband/hw/qib/qib_qsfp.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008, 2009 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 "qib.h"38#include "qib_qsfp.h"3940/*41* QSFP support for ib_qib driver, using "Two Wire Serial Interface" driver42* in qib_twsi.c43*/44#define QSFP_MAX_RETRY 44546static int qsfp_read(struct qib_pportdata *ppd, int addr, void *bp, int len)47{48struct qib_devdata *dd = ppd->dd;49u32 out, mask;50int ret, cnt, pass = 0;51int stuck = 0;52u8 *buff = bp;5354ret = mutex_lock_interruptible(&dd->eep_lock);55if (ret)56goto no_unlock;5758if (dd->twsi_eeprom_dev == QIB_TWSI_NO_DEV) {59ret = -ENXIO;60goto bail;61}6263/*64* We presume, if we are called at all, that this board has65* QSFP. This is on the same i2c chain as the legacy parts,66* but only responds if the module is selected via GPIO pins.67* Further, there are very long setup and hold requirements68* on MODSEL.69*/70mask = QSFP_GPIO_MOD_SEL_N | QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;71out = QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;72if (ppd->hw_pidx) {73mask <<= QSFP_GPIO_PORT2_SHIFT;74out <<= QSFP_GPIO_PORT2_SHIFT;75}7677dd->f_gpio_mod(dd, out, mask, mask);7879/*80* Module could take up to 2 Msec to respond to MOD_SEL, and there81* is no way to tell if it is ready, so we must wait.82*/83msleep(2);8485/* Make sure TWSI bus is in sane state. */86ret = qib_twsi_reset(dd);87if (ret) {88qib_dev_porterr(dd, ppd->port,89"QSFP interface Reset for read failed\n");90ret = -EIO;91stuck = 1;92goto deselect;93}9495/* All QSFP modules are at A0 */9697cnt = 0;98while (cnt < len) {99unsigned in_page;100int wlen = len - cnt;101in_page = addr % QSFP_PAGESIZE;102if ((in_page + wlen) > QSFP_PAGESIZE)103wlen = QSFP_PAGESIZE - in_page;104ret = qib_twsi_blk_rd(dd, QSFP_DEV, addr, buff + cnt, wlen);105/* Some QSFP's fail first try. Retry as experiment */106if (ret && cnt == 0 && ++pass < QSFP_MAX_RETRY)107continue;108if (ret) {109/* qib_twsi_blk_rd() 1 for error, else 0 */110ret = -EIO;111goto deselect;112}113addr += wlen;114cnt += wlen;115}116ret = cnt;117118deselect:119/*120* Module could take up to 10 uSec after transfer before121* ready to respond to MOD_SEL negation, and there is no way122* to tell if it is ready, so we must wait.123*/124udelay(10);125/* set QSFP MODSEL, RST. LP all high */126dd->f_gpio_mod(dd, mask, mask, mask);127128/*129* Module could take up to 2 Msec to respond to MOD_SEL130* going away, and there is no way to tell if it is ready.131* so we must wait.132*/133if (stuck)134qib_dev_err(dd, "QSFP interface bus stuck non-idle\n");135136if (pass >= QSFP_MAX_RETRY && ret)137qib_dev_porterr(dd, ppd->port, "QSFP failed even retrying\n");138else if (pass)139qib_dev_porterr(dd, ppd->port, "QSFP retries: %d\n", pass);140141msleep(2);142143bail:144mutex_unlock(&dd->eep_lock);145146no_unlock:147return ret;148}149150/*151* qsfp_write152* We do not ordinarily write the QSFP, but this is needed to select153* the page on non-flat QSFPs, and possibly later unusual cases154*/155static int qib_qsfp_write(struct qib_pportdata *ppd, int addr, void *bp,156int len)157{158struct qib_devdata *dd = ppd->dd;159u32 out, mask;160int ret, cnt;161u8 *buff = bp;162163ret = mutex_lock_interruptible(&dd->eep_lock);164if (ret)165goto no_unlock;166167if (dd->twsi_eeprom_dev == QIB_TWSI_NO_DEV) {168ret = -ENXIO;169goto bail;170}171172/*173* We presume, if we are called at all, that this board has174* QSFP. This is on the same i2c chain as the legacy parts,175* but only responds if the module is selected via GPIO pins.176* Further, there are very long setup and hold requirements177* on MODSEL.178*/179mask = QSFP_GPIO_MOD_SEL_N | QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;180out = QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;181if (ppd->hw_pidx) {182mask <<= QSFP_GPIO_PORT2_SHIFT;183out <<= QSFP_GPIO_PORT2_SHIFT;184}185dd->f_gpio_mod(dd, out, mask, mask);186187/*188* Module could take up to 2 Msec to respond to MOD_SEL,189* and there is no way to tell if it is ready, so we must wait.190*/191msleep(2);192193/* Make sure TWSI bus is in sane state. */194ret = qib_twsi_reset(dd);195if (ret) {196qib_dev_porterr(dd, ppd->port,197"QSFP interface Reset for write failed\n");198ret = -EIO;199goto deselect;200}201202/* All QSFP modules are at A0 */203204cnt = 0;205while (cnt < len) {206unsigned in_page;207int wlen = len - cnt;208in_page = addr % QSFP_PAGESIZE;209if ((in_page + wlen) > QSFP_PAGESIZE)210wlen = QSFP_PAGESIZE - in_page;211ret = qib_twsi_blk_wr(dd, QSFP_DEV, addr, buff + cnt, wlen);212if (ret) {213/* qib_twsi_blk_wr() 1 for error, else 0 */214ret = -EIO;215goto deselect;216}217addr += wlen;218cnt += wlen;219}220ret = cnt;221222deselect:223/*224* Module could take up to 10 uSec after transfer before225* ready to respond to MOD_SEL negation, and there is no way226* to tell if it is ready, so we must wait.227*/228udelay(10);229/* set QSFP MODSEL, RST, LP high */230dd->f_gpio_mod(dd, mask, mask, mask);231/*232* Module could take up to 2 Msec to respond to MOD_SEL233* going away, and there is no way to tell if it is ready.234* so we must wait.235*/236msleep(2);237238bail:239mutex_unlock(&dd->eep_lock);240241no_unlock:242return ret;243}244245/*246* For validation, we want to check the checksums, even of the247* fields we do not otherwise use. This function reads the bytes from248* <first> to <next-1> and returns the 8lsbs of the sum, or <0 for errors249*/250static int qsfp_cks(struct qib_pportdata *ppd, int first, int next)251{252int ret;253u16 cks;254u8 bval;255256cks = 0;257while (first < next) {258ret = qsfp_read(ppd, first, &bval, 1);259if (ret < 0)260goto bail;261cks += bval;262++first;263}264ret = cks & 0xFF;265bail:266return ret;267268}269270int qib_refresh_qsfp_cache(struct qib_pportdata *ppd, struct qib_qsfp_cache *cp)271{272int ret;273int idx;274u16 cks;275u32 mask;276u8 peek[4];277278/* ensure sane contents on invalid reads, for cable swaps */279memset(cp, 0, sizeof(*cp));280281mask = QSFP_GPIO_MOD_PRS_N;282if (ppd->hw_pidx)283mask <<= QSFP_GPIO_PORT2_SHIFT;284285ret = ppd->dd->f_gpio_mod(ppd->dd, 0, 0, 0);286if (ret & mask) {287ret = -ENODEV;288goto bail;289}290291ret = qsfp_read(ppd, 0, peek, 3);292if (ret < 0)293goto bail;294if ((peek[0] & 0xFE) != 0x0C)295qib_dev_porterr(ppd->dd, ppd->port,296"QSFP byte0 is 0x%02X, S/B 0x0C/D\n", peek[0]);297298if ((peek[2] & 2) == 0) {299/*300* If cable is paged, rather than "flat memory", we need to301* set the page to zero, Even if it already appears to be zero.302*/303u8 poke = 0;304ret = qib_qsfp_write(ppd, 127, &poke, 1);305udelay(50);306if (ret != 1) {307qib_dev_porterr(ppd->dd, ppd->port,308"Failed QSFP Page set\n");309goto bail;310}311}312313ret = qsfp_read(ppd, QSFP_MOD_ID_OFFS, &cp->id, 1);314if (ret < 0)315goto bail;316if ((cp->id & 0xFE) != 0x0C)317qib_dev_porterr(ppd->dd, ppd->port,318"QSFP ID byte is 0x%02X, S/B 0x0C/D\n", cp->id);319cks = cp->id;320321ret = qsfp_read(ppd, QSFP_MOD_PWR_OFFS, &cp->pwr, 1);322if (ret < 0)323goto bail;324cks += cp->pwr;325326ret = qsfp_cks(ppd, QSFP_MOD_PWR_OFFS + 1, QSFP_MOD_LEN_OFFS);327if (ret < 0)328goto bail;329cks += ret;330331ret = qsfp_read(ppd, QSFP_MOD_LEN_OFFS, &cp->len, 1);332if (ret < 0)333goto bail;334cks += cp->len;335336ret = qsfp_read(ppd, QSFP_MOD_TECH_OFFS, &cp->tech, 1);337if (ret < 0)338goto bail;339cks += cp->tech;340341ret = qsfp_read(ppd, QSFP_VEND_OFFS, &cp->vendor, QSFP_VEND_LEN);342if (ret < 0)343goto bail;344for (idx = 0; idx < QSFP_VEND_LEN; ++idx)345cks += cp->vendor[idx];346347ret = qsfp_read(ppd, QSFP_IBXCV_OFFS, &cp->xt_xcv, 1);348if (ret < 0)349goto bail;350cks += cp->xt_xcv;351352ret = qsfp_read(ppd, QSFP_VOUI_OFFS, &cp->oui, QSFP_VOUI_LEN);353if (ret < 0)354goto bail;355for (idx = 0; idx < QSFP_VOUI_LEN; ++idx)356cks += cp->oui[idx];357358ret = qsfp_read(ppd, QSFP_PN_OFFS, &cp->partnum, QSFP_PN_LEN);359if (ret < 0)360goto bail;361for (idx = 0; idx < QSFP_PN_LEN; ++idx)362cks += cp->partnum[idx];363364ret = qsfp_read(ppd, QSFP_REV_OFFS, &cp->rev, QSFP_REV_LEN);365if (ret < 0)366goto bail;367for (idx = 0; idx < QSFP_REV_LEN; ++idx)368cks += cp->rev[idx];369370ret = qsfp_read(ppd, QSFP_ATTEN_OFFS, &cp->atten, QSFP_ATTEN_LEN);371if (ret < 0)372goto bail;373for (idx = 0; idx < QSFP_ATTEN_LEN; ++idx)374cks += cp->atten[idx];375376ret = qsfp_cks(ppd, QSFP_ATTEN_OFFS + QSFP_ATTEN_LEN, QSFP_CC_OFFS);377if (ret < 0)378goto bail;379cks += ret;380381cks &= 0xFF;382ret = qsfp_read(ppd, QSFP_CC_OFFS, &cp->cks1, 1);383if (ret < 0)384goto bail;385if (cks != cp->cks1)386qib_dev_porterr(ppd->dd, ppd->port,387"QSFP cks1 is %02X, computed %02X\n", cp->cks1,388cks);389390/* Second checksum covers 192 to (serial, date, lot) */391ret = qsfp_cks(ppd, QSFP_CC_OFFS + 1, QSFP_SN_OFFS);392if (ret < 0)393goto bail;394cks = ret;395396ret = qsfp_read(ppd, QSFP_SN_OFFS, &cp->serial, QSFP_SN_LEN);397if (ret < 0)398goto bail;399for (idx = 0; idx < QSFP_SN_LEN; ++idx)400cks += cp->serial[idx];401402ret = qsfp_read(ppd, QSFP_DATE_OFFS, &cp->date, QSFP_DATE_LEN);403if (ret < 0)404goto bail;405for (idx = 0; idx < QSFP_DATE_LEN; ++idx)406cks += cp->date[idx];407408ret = qsfp_read(ppd, QSFP_LOT_OFFS, &cp->lot, QSFP_LOT_LEN);409if (ret < 0)410goto bail;411for (idx = 0; idx < QSFP_LOT_LEN; ++idx)412cks += cp->lot[idx];413414ret = qsfp_cks(ppd, QSFP_LOT_OFFS + QSFP_LOT_LEN, QSFP_CC_EXT_OFFS);415if (ret < 0)416goto bail;417cks += ret;418419ret = qsfp_read(ppd, QSFP_CC_EXT_OFFS, &cp->cks2, 1);420if (ret < 0)421goto bail;422cks &= 0xFF;423if (cks != cp->cks2)424qib_dev_porterr(ppd->dd, ppd->port,425"QSFP cks2 is %02X, computed %02X\n", cp->cks2,426cks);427return 0;428429bail:430cp->id = 0;431return ret;432}433434const char * const qib_qsfp_devtech[16] = {435"850nm VCSEL", "1310nm VCSEL", "1550nm VCSEL", "1310nm FP",436"1310nm DFB", "1550nm DFB", "1310nm EML", "1550nm EML",437"Cu Misc", "1490nm DFB", "Cu NoEq", "Cu Eq",438"Undef", "Cu Active BothEq", "Cu FarEq", "Cu NearEq"439};440441#define QSFP_DUMP_CHUNK 16 /* Holds longest string */442#define QSFP_DEFAULT_HDR_CNT 224443444static const char *pwr_codes = "1.5W2.0W2.5W3.5W";445446/*447* Initialize structures that control access to QSFP. Called once per port448* on cards that support QSFP.449*/450void qib_qsfp_init(struct qib_qsfp_data *qd,451void (*fevent)(struct work_struct *))452{453u32 mask, highs;454int pins;455456struct qib_devdata *dd = qd->ppd->dd;457458/* Initialize work struct for later QSFP events */459INIT_WORK(&qd->work, fevent);460461/*462* Later, we may want more validation. For now, just set up pins and463* blip reset. If module is present, call qib_refresh_qsfp_cache(),464* to do further init.465*/466mask = QSFP_GPIO_MOD_SEL_N | QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;467highs = mask - QSFP_GPIO_MOD_RST_N;468if (qd->ppd->hw_pidx) {469mask <<= QSFP_GPIO_PORT2_SHIFT;470highs <<= QSFP_GPIO_PORT2_SHIFT;471}472dd->f_gpio_mod(dd, highs, mask, mask);473udelay(20); /* Generous RST dwell */474475dd->f_gpio_mod(dd, mask, mask, mask);476/* Spec says module can take up to two seconds! */477mask = QSFP_GPIO_MOD_PRS_N;478if (qd->ppd->hw_pidx)479mask <<= QSFP_GPIO_PORT2_SHIFT;480481/* Do not try to wait here. Better to let event handle it */482pins = dd->f_gpio_mod(dd, 0, 0, 0);483if (pins & mask)484goto bail;485/* We see a module, but it may be unwise to look yet. Just schedule */486qd->t_insert = get_jiffies_64();487queue_work(ib_wq, &qd->work);488bail:489return;490}491492void qib_qsfp_deinit(struct qib_qsfp_data *qd)493{494/*495* There is nothing to do here for now. our work is scheduled496* with queue_work(), and flush_workqueue() from remove_one497* will block until all work setup with queue_work()498* completes.499*/500}501502int qib_qsfp_dump(struct qib_pportdata *ppd, char *buf, int len)503{504struct qib_qsfp_cache cd;505u8 bin_buff[QSFP_DUMP_CHUNK];506char lenstr[6];507int sofar, ret;508int bidx = 0;509510sofar = 0;511ret = qib_refresh_qsfp_cache(ppd, &cd);512if (ret < 0)513goto bail;514515lenstr[0] = ' ';516lenstr[1] = '\0';517if (QSFP_IS_CU(cd.tech))518sprintf(lenstr, "%dM ", cd.len);519520sofar += scnprintf(buf + sofar, len - sofar, "PWR:%.3sW\n", pwr_codes +521(QSFP_PWR(cd.pwr) * 4));522523sofar += scnprintf(buf + sofar, len - sofar, "TECH:%s%s\n", lenstr,524qib_qsfp_devtech[cd.tech >> 4]);525526sofar += scnprintf(buf + sofar, len - sofar, "Vendor:%.*s\n",527QSFP_VEND_LEN, cd.vendor);528529sofar += scnprintf(buf + sofar, len - sofar, "OUI:%06X\n",530QSFP_OUI(cd.oui));531532sofar += scnprintf(buf + sofar, len - sofar, "Part#:%.*s\n",533QSFP_PN_LEN, cd.partnum);534sofar += scnprintf(buf + sofar, len - sofar, "Rev:%.*s\n",535QSFP_REV_LEN, cd.rev);536if (QSFP_IS_CU(cd.tech))537sofar += scnprintf(buf + sofar, len - sofar, "Atten:%d, %d\n",538QSFP_ATTEN_SDR(cd.atten),539QSFP_ATTEN_DDR(cd.atten));540sofar += scnprintf(buf + sofar, len - sofar, "Serial:%.*s\n",541QSFP_SN_LEN, cd.serial);542sofar += scnprintf(buf + sofar, len - sofar, "Date:%.*s\n",543QSFP_DATE_LEN, cd.date);544sofar += scnprintf(buf + sofar, len - sofar, "Lot:%.*s\n",545QSFP_LOT_LEN, cd.date);546547while (bidx < QSFP_DEFAULT_HDR_CNT) {548int iidx;549ret = qsfp_read(ppd, bidx, bin_buff, QSFP_DUMP_CHUNK);550if (ret < 0)551goto bail;552for (iidx = 0; iidx < ret; ++iidx) {553sofar += scnprintf(buf + sofar, len-sofar, " %02X",554bin_buff[iidx]);555}556sofar += scnprintf(buf + sofar, len - sofar, "\n");557bidx += QSFP_DUMP_CHUNK;558}559ret = sofar;560bail:561return ret;562}563564565