2 * Copyright(c) 2015, 2016 Intel Corporation.
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <linux/delay.h>
49 #include <linux/pci.h>
50 #include <linux/vmalloc.h>
56 * QSFP support for hfi driver, using "Two Wire Serial Interface" driver
59 #define I2C_MAX_RETRY 4
62 * Raw i2c write. No set-up or lock checking.
64 static int __i2c_write(struct hfi1_pportdata *ppd, u32 target, int i2c_addr,
65 int offset, void *bp, int len)
67 struct hfi1_devdata *dd = ppd->dd;
75 ret = hfi1_twsi_blk_wr(dd, target, i2c_addr, offset,
78 /* hfi1_twsi_blk_wr() 1 for error, else 0 */
85 /* Must wait min 20us between qsfp i2c transactions */
92 * Caller must hold the i2c chain resource.
94 int i2c_write(struct hfi1_pportdata *ppd, u32 target, int i2c_addr, int offset,
99 if (!check_chip_resource(ppd->dd, qsfp_resource(ppd->dd), __func__))
102 /* make sure the TWSI bus is in a sane state */
103 ret = hfi1_twsi_reset(ppd->dd, target);
105 hfi1_dev_porterr(ppd->dd, ppd->port,
106 "I2C chain %d write interface reset failed\n",
111 return __i2c_write(ppd, target, i2c_addr, offset, bp, len);
115 * Raw i2c read. No set-up or lock checking.
117 static int __i2c_read(struct hfi1_pportdata *ppd, u32 target, int i2c_addr,
118 int offset, void *bp, int len)
120 struct hfi1_devdata *dd = ppd->dd;
121 int ret, cnt, pass = 0;
122 int orig_offset = offset;
126 int rlen = len - cnt;
128 ret = hfi1_twsi_blk_rd(dd, target, i2c_addr, offset,
130 /* Some QSFP's fail first try. Retry as experiment */
131 if (ret && cnt == 0 && ++pass < I2C_MAX_RETRY)
134 /* hfi1_twsi_blk_rd() 1 for error, else 0 */
146 hfi1_dev_porterr(dd, ppd->port,
147 "I2C chain %d read failed, addr 0x%x, offset 0x%x, len %d\n",
148 target, i2c_addr, orig_offset, len);
151 /* Must wait min 20us between qsfp i2c transactions */
158 * Caller must hold the i2c chain resource.
160 int i2c_read(struct hfi1_pportdata *ppd, u32 target, int i2c_addr, int offset,
165 if (!check_chip_resource(ppd->dd, qsfp_resource(ppd->dd), __func__))
168 /* make sure the TWSI bus is in a sane state */
169 ret = hfi1_twsi_reset(ppd->dd, target);
171 hfi1_dev_porterr(ppd->dd, ppd->port,
172 "I2C chain %d read interface reset failed\n",
177 return __i2c_read(ppd, target, i2c_addr, offset, bp, len);
181 * Write page n, offset m of QSFP memory as defined by SFF 8636
182 * by writing @addr = ((256 * n) + m)
184 * Caller must hold the i2c chain resource.
186 int qsfp_write(struct hfi1_pportdata *ppd, u32 target, int addr, void *bp,
195 if (!check_chip_resource(ppd->dd, qsfp_resource(ppd->dd), __func__))
198 /* make sure the TWSI bus is in a sane state */
199 ret = hfi1_twsi_reset(ppd->dd, target);
201 hfi1_dev_porterr(ppd->dd, ppd->port,
202 "QSFP chain %d write interface reset failed\n",
207 while (count < len) {
209 * Set the qsfp page based on a zero-based address
210 * and a page size of QSFP_PAGESIZE bytes.
212 page = (u8)(addr / QSFP_PAGESIZE);
214 ret = __i2c_write(ppd, target, QSFP_DEV | QSFP_OFFSET_SIZE,
215 QSFP_PAGE_SELECT_BYTE_OFFS, &page, 1);
217 hfi1_dev_porterr(ppd->dd, ppd->port,
218 "QSFP chain %d can't write QSFP_PAGE_SELECT_BYTE: %d\n",
224 offset = addr % QSFP_PAGESIZE;
225 nwrite = len - count;
226 /* truncate write to boundary if crossing boundary */
227 if (((addr % QSFP_RW_BOUNDARY) + nwrite) > QSFP_RW_BOUNDARY)
228 nwrite = QSFP_RW_BOUNDARY - (addr % QSFP_RW_BOUNDARY);
230 ret = __i2c_write(ppd, target, QSFP_DEV | QSFP_OFFSET_SIZE,
231 offset, bp + count, nwrite);
232 if (ret <= 0) /* stop on error or nothing written */
245 * Perform a stand-alone single QSFP write. Acquire the resource, do the
246 * read, then release the resource.
248 int one_qsfp_write(struct hfi1_pportdata *ppd, u32 target, int addr, void *bp,
251 struct hfi1_devdata *dd = ppd->dd;
252 u32 resource = qsfp_resource(dd);
255 ret = acquire_chip_resource(dd, resource, QSFP_WAIT);
258 ret = qsfp_write(ppd, target, addr, bp, len);
259 release_chip_resource(dd, resource);
265 * Access page n, offset m of QSFP memory as defined by SFF 8636
266 * by reading @addr = ((256 * n) + m)
268 * Caller must hold the i2c chain resource.
270 int qsfp_read(struct hfi1_pportdata *ppd, u32 target, int addr, void *bp,
279 if (!check_chip_resource(ppd->dd, qsfp_resource(ppd->dd), __func__))
282 /* make sure the TWSI bus is in a sane state */
283 ret = hfi1_twsi_reset(ppd->dd, target);
285 hfi1_dev_porterr(ppd->dd, ppd->port,
286 "QSFP chain %d read interface reset failed\n",
291 while (count < len) {
293 * Set the qsfp page based on a zero-based address
294 * and a page size of QSFP_PAGESIZE bytes.
296 page = (u8)(addr / QSFP_PAGESIZE);
297 ret = __i2c_write(ppd, target, QSFP_DEV | QSFP_OFFSET_SIZE,
298 QSFP_PAGE_SELECT_BYTE_OFFS, &page, 1);
300 hfi1_dev_porterr(ppd->dd, ppd->port,
301 "QSFP chain %d can't write QSFP_PAGE_SELECT_BYTE: %d\n",
307 offset = addr % QSFP_PAGESIZE;
309 /* truncate read to boundary if crossing boundary */
310 if (((addr % QSFP_RW_BOUNDARY) + nread) > QSFP_RW_BOUNDARY)
311 nread = QSFP_RW_BOUNDARY - (addr % QSFP_RW_BOUNDARY);
313 /* QSFPs require a 5-10msec delay after write operations */
315 ret = __i2c_read(ppd, target, QSFP_DEV | QSFP_OFFSET_SIZE,
316 offset, bp + count, nread);
317 if (ret <= 0) /* stop on error or nothing read */
330 * Perform a stand-alone single QSFP read. Acquire the resource, do the
331 * read, then release the resource.
333 int one_qsfp_read(struct hfi1_pportdata *ppd, u32 target, int addr, void *bp,
336 struct hfi1_devdata *dd = ppd->dd;
337 u32 resource = qsfp_resource(dd);
340 ret = acquire_chip_resource(dd, resource, QSFP_WAIT);
343 ret = qsfp_read(ppd, target, addr, bp, len);
344 release_chip_resource(dd, resource);
350 * This function caches the QSFP memory range in 128 byte chunks.
351 * As an example, the next byte after address 255 is byte 128 from
352 * upper page 01H (if existing) rather than byte 0 from lower page 00H.
353 * Access page n, offset m of QSFP memory as defined by SFF 8636
354 * in the cache by reading byte ((128 * n) + m)
355 * The calls to qsfp_{read,write} in this function correctly handle the
356 * address map difference between this mapping and the mapping implemented
359 int refresh_qsfp_cache(struct hfi1_pportdata *ppd, struct qsfp_data *cp)
361 u32 target = ppd->dd->hfi1_id;
364 u8 *cache = &cp->cache[0];
366 /* ensure sane contents on invalid reads, for cable swaps */
367 memset(cache, 0, (QSFP_MAX_NUM_PAGES * 128));
368 spin_lock_irqsave(&ppd->qsfp_info.qsfp_lock, flags);
369 ppd->qsfp_info.cache_valid = 0;
370 spin_unlock_irqrestore(&ppd->qsfp_info.qsfp_lock, flags);
372 if (!qsfp_mod_present(ppd)) {
374 goto bail_no_release;
377 ret = acquire_chip_resource(ppd->dd, qsfp_resource(ppd->dd), QSFP_WAIT);
379 goto bail_no_release;
381 ret = qsfp_read(ppd, target, 0, cache, QSFP_PAGESIZE);
382 if (ret != QSFP_PAGESIZE) {
384 "%s: Page 0 read failed, expected %d, got %d\n",
385 __func__, QSFP_PAGESIZE, ret);
389 /* Is paging enabled? */
390 if (!(cache[2] & 4)) {
391 /* Paging enabled, page 03 required */
392 if ((cache[195] & 0xC0) == 0xC0) {
394 ret = qsfp_read(ppd, target, 384, cache + 256, 128);
395 if (ret <= 0 || ret != 128) {
396 dd_dev_info(ppd->dd, "%s failed\n", __func__);
399 ret = qsfp_read(ppd, target, 640, cache + 384, 128);
400 if (ret <= 0 || ret != 128) {
401 dd_dev_info(ppd->dd, "%s failed\n", __func__);
404 ret = qsfp_read(ppd, target, 896, cache + 512, 128);
405 if (ret <= 0 || ret != 128) {
406 dd_dev_info(ppd->dd, "%s failed\n", __func__);
409 } else if ((cache[195] & 0x80) == 0x80) {
410 /* only page 2 and 3 */
411 ret = qsfp_read(ppd, target, 640, cache + 384, 128);
412 if (ret <= 0 || ret != 128) {
413 dd_dev_info(ppd->dd, "%s failed\n", __func__);
416 ret = qsfp_read(ppd, target, 896, cache + 512, 128);
417 if (ret <= 0 || ret != 128) {
418 dd_dev_info(ppd->dd, "%s failed\n", __func__);
421 } else if ((cache[195] & 0x40) == 0x40) {
422 /* only page 1 and 3 */
423 ret = qsfp_read(ppd, target, 384, cache + 256, 128);
424 if (ret <= 0 || ret != 128) {
425 dd_dev_info(ppd->dd, "%s failed\n", __func__);
428 ret = qsfp_read(ppd, target, 896, cache + 512, 128);
429 if (ret <= 0 || ret != 128) {
430 dd_dev_info(ppd->dd, "%s failed\n", __func__);
435 ret = qsfp_read(ppd, target, 896, cache + 512, 128);
436 if (ret <= 0 || ret != 128) {
437 dd_dev_info(ppd->dd, "%s failed\n", __func__);
443 release_chip_resource(ppd->dd, qsfp_resource(ppd->dd));
445 spin_lock_irqsave(&ppd->qsfp_info.qsfp_lock, flags);
446 ppd->qsfp_info.cache_valid = 1;
447 ppd->qsfp_info.cache_refresh_required = 0;
448 spin_unlock_irqrestore(&ppd->qsfp_info.qsfp_lock, flags);
453 release_chip_resource(ppd->dd, qsfp_resource(ppd->dd));
455 memset(cache, 0, (QSFP_MAX_NUM_PAGES * 128));
459 const char * const hfi1_qsfp_devtech[16] = {
460 "850nm VCSEL", "1310nm VCSEL", "1550nm VCSEL", "1310nm FP",
461 "1310nm DFB", "1550nm DFB", "1310nm EML", "1550nm EML",
462 "Cu Misc", "1490nm DFB", "Cu NoEq", "Cu Eq",
463 "Undef", "Cu Active BothEq", "Cu FarEq", "Cu NearEq"
466 #define QSFP_DUMP_CHUNK 16 /* Holds longest string */
467 #define QSFP_DEFAULT_HDR_CNT 224
469 static const char *pwr_codes = "1.5W2.0W2.5W3.5W";
471 int qsfp_mod_present(struct hfi1_pportdata *ppd)
473 struct hfi1_devdata *dd = ppd->dd;
476 reg = read_csr(dd, dd->hfi1_id ? ASIC_QSFP2_IN : ASIC_QSFP1_IN);
477 return !(reg & QSFP_HFI0_MODPRST_N);
481 * This function maps QSFP memory addresses in 128 byte chunks in the following
482 * fashion per the CableInfo SMA query definition in the IBA 1.3 spec/OPA Gen 1
484 * For addr 000-127, lower page 00h
485 * For addr 128-255, upper page 00h
486 * For addr 256-383, upper page 01h
487 * For addr 384-511, upper page 02h
488 * For addr 512-639, upper page 03h
490 * For addresses beyond this range, it returns the invalid range of data buffer
492 * For upper pages that are optional, if they are not valid, returns the
493 * particular range of bytes in the data buffer set to 0.
495 int get_cable_info(struct hfi1_devdata *dd, u32 port_num, u32 addr, u32 len,
498 struct hfi1_pportdata *ppd;
502 if (port_num > dd->num_pports || port_num < 1) {
503 dd_dev_info(dd, "%s: Invalid port number %d\n",
509 ppd = dd->pport + (port_num - 1);
510 if (!qsfp_mod_present(ppd)) {
515 if (!ppd->qsfp_info.cache_valid) {
520 if (addr >= (QSFP_MAX_NUM_PAGES * 128)) {
525 if ((addr + len) > (QSFP_MAX_NUM_PAGES * 128)) {
526 excess_len = (addr + len) - (QSFP_MAX_NUM_PAGES * 128);
527 memcpy(data, &ppd->qsfp_info.cache[addr], (len - excess_len));
528 data += (len - excess_len);
532 memcpy(data, &ppd->qsfp_info.cache[addr], len);
536 memset(data, 0, excess_len);
540 int qsfp_dump(struct hfi1_pportdata *ppd, char *buf, int len)
542 u8 *cache = &ppd->qsfp_info.cache[0];
543 u8 bin_buff[QSFP_DUMP_CHUNK];
547 u8 *atten = &cache[QSFP_ATTEN_OFFS];
548 u8 *vendor_oui = &cache[QSFP_VOUI_OFFS];
554 if (ppd->qsfp_info.cache_valid) {
555 if (QSFP_IS_CU(cache[QSFP_MOD_TECH_OFFS]))
556 sprintf(lenstr, "%dM ", cache[QSFP_MOD_LEN_OFFS]);
558 sofar += scnprintf(buf + sofar, len - sofar, "PWR:%.3sW\n",
560 (QSFP_PWR(cache[QSFP_MOD_PWR_OFFS]) * 4));
562 sofar += scnprintf(buf + sofar, len - sofar, "TECH:%s%s\n",
564 hfi1_qsfp_devtech[(cache[QSFP_MOD_TECH_OFFS]) >> 4]);
566 sofar += scnprintf(buf + sofar, len - sofar, "Vendor:%.*s\n",
567 QSFP_VEND_LEN, &cache[QSFP_VEND_OFFS]);
569 sofar += scnprintf(buf + sofar, len - sofar, "OUI:%06X\n",
570 QSFP_OUI(vendor_oui));
572 sofar += scnprintf(buf + sofar, len - sofar, "Part#:%.*s\n",
573 QSFP_PN_LEN, &cache[QSFP_PN_OFFS]);
575 sofar += scnprintf(buf + sofar, len - sofar, "Rev:%.*s\n",
576 QSFP_REV_LEN, &cache[QSFP_REV_OFFS]);
578 if (QSFP_IS_CU(cache[QSFP_MOD_TECH_OFFS]))
579 sofar += scnprintf(buf + sofar, len - sofar,
581 QSFP_ATTEN_SDR(atten),
582 QSFP_ATTEN_DDR(atten));
584 sofar += scnprintf(buf + sofar, len - sofar, "Serial:%.*s\n",
585 QSFP_SN_LEN, &cache[QSFP_SN_OFFS]);
587 sofar += scnprintf(buf + sofar, len - sofar, "Date:%.*s\n",
588 QSFP_DATE_LEN, &cache[QSFP_DATE_OFFS]);
590 sofar += scnprintf(buf + sofar, len - sofar, "Lot:%.*s\n",
591 QSFP_LOT_LEN, &cache[QSFP_LOT_OFFS]);
593 while (bidx < QSFP_DEFAULT_HDR_CNT) {
596 memcpy(bin_buff, &cache[bidx], QSFP_DUMP_CHUNK);
597 for (iidx = 0; iidx < QSFP_DUMP_CHUNK; ++iidx) {
598 sofar += scnprintf(buf + sofar, len - sofar,
599 " %02X", bin_buff[iidx]);
601 sofar += scnprintf(buf + sofar, len - sofar, "\n");
602 bidx += QSFP_DUMP_CHUNK;