qib_qsfp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/delay.h>
  34. #include <linux/pci.h>
  35. #include <linux/vmalloc.h>
  36. #include "qib.h"
  37. #include "qib_qsfp.h"
  38. /*
  39. * QSFP support for ib_qib driver, using "Two Wire Serial Interface" driver
  40. * in qib_twsi.c
  41. */
  42. #define QSFP_MAX_RETRY 4
  43. static int qsfp_read(struct qib_pportdata *ppd, int addr, void *bp, int len)
  44. {
  45. struct qib_devdata *dd = ppd->dd;
  46. u32 out, mask;
  47. int ret, cnt, pass = 0;
  48. int stuck = 0;
  49. u8 *buff = bp;
  50. ret = mutex_lock_interruptible(&dd->eep_lock);
  51. if (ret)
  52. goto no_unlock;
  53. if (dd->twsi_eeprom_dev == QIB_TWSI_NO_DEV) {
  54. ret = -ENXIO;
  55. goto bail;
  56. }
  57. /*
  58. * We presume, if we are called at all, that this board has
  59. * QSFP. This is on the same i2c chain as the legacy parts,
  60. * but only responds if the module is selected via GPIO pins.
  61. * Further, there are very long setup and hold requirements
  62. * on MODSEL.
  63. */
  64. mask = QSFP_GPIO_MOD_SEL_N | QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;
  65. out = QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;
  66. if (ppd->hw_pidx) {
  67. mask <<= QSFP_GPIO_PORT2_SHIFT;
  68. out <<= QSFP_GPIO_PORT2_SHIFT;
  69. }
  70. dd->f_gpio_mod(dd, out, mask, mask);
  71. /*
  72. * Module could take up to 2 Msec to respond to MOD_SEL, and there
  73. * is no way to tell if it is ready, so we must wait.
  74. */
  75. msleep(20);
  76. /* Make sure TWSI bus is in sane state. */
  77. ret = qib_twsi_reset(dd);
  78. if (ret) {
  79. qib_dev_porterr(dd, ppd->port,
  80. "QSFP interface Reset for read failed\n");
  81. ret = -EIO;
  82. stuck = 1;
  83. goto deselect;
  84. }
  85. /* All QSFP modules are at A0 */
  86. cnt = 0;
  87. while (cnt < len) {
  88. unsigned in_page;
  89. int wlen = len - cnt;
  90. in_page = addr % QSFP_PAGESIZE;
  91. if ((in_page + wlen) > QSFP_PAGESIZE)
  92. wlen = QSFP_PAGESIZE - in_page;
  93. ret = qib_twsi_blk_rd(dd, QSFP_DEV, addr, buff + cnt, wlen);
  94. /* Some QSFP's fail first try. Retry as experiment */
  95. if (ret && cnt == 0 && ++pass < QSFP_MAX_RETRY)
  96. continue;
  97. if (ret) {
  98. /* qib_twsi_blk_rd() 1 for error, else 0 */
  99. ret = -EIO;
  100. goto deselect;
  101. }
  102. addr += wlen;
  103. cnt += wlen;
  104. }
  105. ret = cnt;
  106. deselect:
  107. /*
  108. * Module could take up to 10 uSec after transfer before
  109. * ready to respond to MOD_SEL negation, and there is no way
  110. * to tell if it is ready, so we must wait.
  111. */
  112. udelay(10);
  113. /* set QSFP MODSEL, RST. LP all high */
  114. dd->f_gpio_mod(dd, mask, mask, mask);
  115. /*
  116. * Module could take up to 2 Msec to respond to MOD_SEL
  117. * going away, and there is no way to tell if it is ready.
  118. * so we must wait.
  119. */
  120. if (stuck)
  121. qib_dev_err(dd, "QSFP interface bus stuck non-idle\n");
  122. if (pass >= QSFP_MAX_RETRY && ret)
  123. qib_dev_porterr(dd, ppd->port, "QSFP failed even retrying\n");
  124. else if (pass)
  125. qib_dev_porterr(dd, ppd->port, "QSFP retries: %d\n", pass);
  126. msleep(20);
  127. bail:
  128. mutex_unlock(&dd->eep_lock);
  129. no_unlock:
  130. return ret;
  131. }
  132. /*
  133. * qsfp_write
  134. * We do not ordinarily write the QSFP, but this is needed to select
  135. * the page on non-flat QSFPs, and possibly later unusual cases
  136. */
  137. static int qib_qsfp_write(struct qib_pportdata *ppd, int addr, void *bp,
  138. int len)
  139. {
  140. struct qib_devdata *dd = ppd->dd;
  141. u32 out, mask;
  142. int ret, cnt;
  143. u8 *buff = bp;
  144. ret = mutex_lock_interruptible(&dd->eep_lock);
  145. if (ret)
  146. goto no_unlock;
  147. if (dd->twsi_eeprom_dev == QIB_TWSI_NO_DEV) {
  148. ret = -ENXIO;
  149. goto bail;
  150. }
  151. /*
  152. * We presume, if we are called at all, that this board has
  153. * QSFP. This is on the same i2c chain as the legacy parts,
  154. * but only responds if the module is selected via GPIO pins.
  155. * Further, there are very long setup and hold requirements
  156. * on MODSEL.
  157. */
  158. mask = QSFP_GPIO_MOD_SEL_N | QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;
  159. out = QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;
  160. if (ppd->hw_pidx) {
  161. mask <<= QSFP_GPIO_PORT2_SHIFT;
  162. out <<= QSFP_GPIO_PORT2_SHIFT;
  163. }
  164. dd->f_gpio_mod(dd, out, mask, mask);
  165. /*
  166. * Module could take up to 2 Msec to respond to MOD_SEL,
  167. * and there is no way to tell if it is ready, so we must wait.
  168. */
  169. msleep(20);
  170. /* Make sure TWSI bus is in sane state. */
  171. ret = qib_twsi_reset(dd);
  172. if (ret) {
  173. qib_dev_porterr(dd, ppd->port,
  174. "QSFP interface Reset for write failed\n");
  175. ret = -EIO;
  176. goto deselect;
  177. }
  178. /* All QSFP modules are at A0 */
  179. cnt = 0;
  180. while (cnt < len) {
  181. unsigned in_page;
  182. int wlen = len - cnt;
  183. in_page = addr % QSFP_PAGESIZE;
  184. if ((in_page + wlen) > QSFP_PAGESIZE)
  185. wlen = QSFP_PAGESIZE - in_page;
  186. ret = qib_twsi_blk_wr(dd, QSFP_DEV, addr, buff + cnt, wlen);
  187. if (ret) {
  188. /* qib_twsi_blk_wr() 1 for error, else 0 */
  189. ret = -EIO;
  190. goto deselect;
  191. }
  192. addr += wlen;
  193. cnt += wlen;
  194. }
  195. ret = cnt;
  196. deselect:
  197. /*
  198. * Module could take up to 10 uSec after transfer before
  199. * ready to respond to MOD_SEL negation, and there is no way
  200. * to tell if it is ready, so we must wait.
  201. */
  202. udelay(10);
  203. /* set QSFP MODSEL, RST, LP high */
  204. dd->f_gpio_mod(dd, mask, mask, mask);
  205. /*
  206. * Module could take up to 2 Msec to respond to MOD_SEL
  207. * going away, and there is no way to tell if it is ready.
  208. * so we must wait.
  209. */
  210. msleep(20);
  211. bail:
  212. mutex_unlock(&dd->eep_lock);
  213. no_unlock:
  214. return ret;
  215. }
  216. /*
  217. * For validation, we want to check the checksums, even of the
  218. * fields we do not otherwise use. This function reads the bytes from
  219. * <first> to <next-1> and returns the 8lsbs of the sum, or <0 for errors
  220. */
  221. static int qsfp_cks(struct qib_pportdata *ppd, int first, int next)
  222. {
  223. int ret;
  224. u16 cks;
  225. u8 bval;
  226. cks = 0;
  227. while (first < next) {
  228. ret = qsfp_read(ppd, first, &bval, 1);
  229. if (ret < 0)
  230. goto bail;
  231. cks += bval;
  232. ++first;
  233. }
  234. ret = cks & 0xFF;
  235. bail:
  236. return ret;
  237. }
  238. int qib_refresh_qsfp_cache(struct qib_pportdata *ppd, struct qib_qsfp_cache *cp)
  239. {
  240. int ret;
  241. int idx;
  242. u16 cks;
  243. u8 peek[4];
  244. /* ensure sane contents on invalid reads, for cable swaps */
  245. memset(cp, 0, sizeof(*cp));
  246. if (!qib_qsfp_mod_present(ppd)) {
  247. ret = -ENODEV;
  248. goto bail;
  249. }
  250. ret = qsfp_read(ppd, 0, peek, 3);
  251. if (ret < 0)
  252. goto bail;
  253. if ((peek[0] & 0xFE) != 0x0C)
  254. qib_dev_porterr(ppd->dd, ppd->port,
  255. "QSFP byte0 is 0x%02X, S/B 0x0C/D\n", peek[0]);
  256. if ((peek[2] & 4) == 0) {
  257. /*
  258. * If cable is paged, rather than "flat memory", we need to
  259. * set the page to zero, Even if it already appears to be zero.
  260. */
  261. u8 poke = 0;
  262. ret = qib_qsfp_write(ppd, 127, &poke, 1);
  263. udelay(50);
  264. if (ret != 1) {
  265. qib_dev_porterr(ppd->dd, ppd->port,
  266. "Failed QSFP Page set\n");
  267. goto bail;
  268. }
  269. }
  270. ret = qsfp_read(ppd, QSFP_MOD_ID_OFFS, &cp->id, 1);
  271. if (ret < 0)
  272. goto bail;
  273. if ((cp->id & 0xFE) != 0x0C)
  274. qib_dev_porterr(ppd->dd, ppd->port,
  275. "QSFP ID byte is 0x%02X, S/B 0x0C/D\n", cp->id);
  276. cks = cp->id;
  277. ret = qsfp_read(ppd, QSFP_MOD_PWR_OFFS, &cp->pwr, 1);
  278. if (ret < 0)
  279. goto bail;
  280. cks += cp->pwr;
  281. ret = qsfp_cks(ppd, QSFP_MOD_PWR_OFFS + 1, QSFP_MOD_LEN_OFFS);
  282. if (ret < 0)
  283. goto bail;
  284. cks += ret;
  285. ret = qsfp_read(ppd, QSFP_MOD_LEN_OFFS, &cp->len, 1);
  286. if (ret < 0)
  287. goto bail;
  288. cks += cp->len;
  289. ret = qsfp_read(ppd, QSFP_MOD_TECH_OFFS, &cp->tech, 1);
  290. if (ret < 0)
  291. goto bail;
  292. cks += cp->tech;
  293. ret = qsfp_read(ppd, QSFP_VEND_OFFS, &cp->vendor, QSFP_VEND_LEN);
  294. if (ret < 0)
  295. goto bail;
  296. for (idx = 0; idx < QSFP_VEND_LEN; ++idx)
  297. cks += cp->vendor[idx];
  298. ret = qsfp_read(ppd, QSFP_IBXCV_OFFS, &cp->xt_xcv, 1);
  299. if (ret < 0)
  300. goto bail;
  301. cks += cp->xt_xcv;
  302. ret = qsfp_read(ppd, QSFP_VOUI_OFFS, &cp->oui, QSFP_VOUI_LEN);
  303. if (ret < 0)
  304. goto bail;
  305. for (idx = 0; idx < QSFP_VOUI_LEN; ++idx)
  306. cks += cp->oui[idx];
  307. ret = qsfp_read(ppd, QSFP_PN_OFFS, &cp->partnum, QSFP_PN_LEN);
  308. if (ret < 0)
  309. goto bail;
  310. for (idx = 0; idx < QSFP_PN_LEN; ++idx)
  311. cks += cp->partnum[idx];
  312. ret = qsfp_read(ppd, QSFP_REV_OFFS, &cp->rev, QSFP_REV_LEN);
  313. if (ret < 0)
  314. goto bail;
  315. for (idx = 0; idx < QSFP_REV_LEN; ++idx)
  316. cks += cp->rev[idx];
  317. ret = qsfp_read(ppd, QSFP_ATTEN_OFFS, &cp->atten, QSFP_ATTEN_LEN);
  318. if (ret < 0)
  319. goto bail;
  320. for (idx = 0; idx < QSFP_ATTEN_LEN; ++idx)
  321. cks += cp->atten[idx];
  322. ret = qsfp_cks(ppd, QSFP_ATTEN_OFFS + QSFP_ATTEN_LEN, QSFP_CC_OFFS);
  323. if (ret < 0)
  324. goto bail;
  325. cks += ret;
  326. cks &= 0xFF;
  327. ret = qsfp_read(ppd, QSFP_CC_OFFS, &cp->cks1, 1);
  328. if (ret < 0)
  329. goto bail;
  330. if (cks != cp->cks1)
  331. qib_dev_porterr(ppd->dd, ppd->port,
  332. "QSFP cks1 is %02X, computed %02X\n", cp->cks1,
  333. cks);
  334. /* Second checksum covers 192 to (serial, date, lot) */
  335. ret = qsfp_cks(ppd, QSFP_CC_OFFS + 1, QSFP_SN_OFFS);
  336. if (ret < 0)
  337. goto bail;
  338. cks = ret;
  339. ret = qsfp_read(ppd, QSFP_SN_OFFS, &cp->serial, QSFP_SN_LEN);
  340. if (ret < 0)
  341. goto bail;
  342. for (idx = 0; idx < QSFP_SN_LEN; ++idx)
  343. cks += cp->serial[idx];
  344. ret = qsfp_read(ppd, QSFP_DATE_OFFS, &cp->date, QSFP_DATE_LEN);
  345. if (ret < 0)
  346. goto bail;
  347. for (idx = 0; idx < QSFP_DATE_LEN; ++idx)
  348. cks += cp->date[idx];
  349. ret = qsfp_read(ppd, QSFP_LOT_OFFS, &cp->lot, QSFP_LOT_LEN);
  350. if (ret < 0)
  351. goto bail;
  352. for (idx = 0; idx < QSFP_LOT_LEN; ++idx)
  353. cks += cp->lot[idx];
  354. ret = qsfp_cks(ppd, QSFP_LOT_OFFS + QSFP_LOT_LEN, QSFP_CC_EXT_OFFS);
  355. if (ret < 0)
  356. goto bail;
  357. cks += ret;
  358. ret = qsfp_read(ppd, QSFP_CC_EXT_OFFS, &cp->cks2, 1);
  359. if (ret < 0)
  360. goto bail;
  361. cks &= 0xFF;
  362. if (cks != cp->cks2)
  363. qib_dev_porterr(ppd->dd, ppd->port,
  364. "QSFP cks2 is %02X, computed %02X\n", cp->cks2,
  365. cks);
  366. return 0;
  367. bail:
  368. cp->id = 0;
  369. return ret;
  370. }
  371. const char * const qib_qsfp_devtech[16] = {
  372. "850nm VCSEL", "1310nm VCSEL", "1550nm VCSEL", "1310nm FP",
  373. "1310nm DFB", "1550nm DFB", "1310nm EML", "1550nm EML",
  374. "Cu Misc", "1490nm DFB", "Cu NoEq", "Cu Eq",
  375. "Undef", "Cu Active BothEq", "Cu FarEq", "Cu NearEq"
  376. };
  377. #define QSFP_DUMP_CHUNK 16 /* Holds longest string */
  378. #define QSFP_DEFAULT_HDR_CNT 224
  379. static const char *pwr_codes = "1.5W2.0W2.5W3.5W";
  380. int qib_qsfp_mod_present(struct qib_pportdata *ppd)
  381. {
  382. u32 mask;
  383. int ret;
  384. mask = QSFP_GPIO_MOD_PRS_N <<
  385. (ppd->hw_pidx * QSFP_GPIO_PORT2_SHIFT);
  386. ret = ppd->dd->f_gpio_mod(ppd->dd, 0, 0, 0);
  387. return !((ret & mask) >>
  388. ((ppd->hw_pidx * QSFP_GPIO_PORT2_SHIFT) + 3));
  389. }
  390. /*
  391. * Initialize structures that control access to QSFP. Called once per port
  392. * on cards that support QSFP.
  393. */
  394. void qib_qsfp_init(struct qib_qsfp_data *qd,
  395. void (*fevent)(struct work_struct *))
  396. {
  397. u32 mask, highs;
  398. struct qib_devdata *dd = qd->ppd->dd;
  399. /* Initialize work struct for later QSFP events */
  400. INIT_WORK(&qd->work, fevent);
  401. /*
  402. * Later, we may want more validation. For now, just set up pins and
  403. * blip reset. If module is present, call qib_refresh_qsfp_cache(),
  404. * to do further init.
  405. */
  406. mask = QSFP_GPIO_MOD_SEL_N | QSFP_GPIO_MOD_RST_N | QSFP_GPIO_LP_MODE;
  407. highs = mask - QSFP_GPIO_MOD_RST_N;
  408. if (qd->ppd->hw_pidx) {
  409. mask <<= QSFP_GPIO_PORT2_SHIFT;
  410. highs <<= QSFP_GPIO_PORT2_SHIFT;
  411. }
  412. dd->f_gpio_mod(dd, highs, mask, mask);
  413. udelay(20); /* Generous RST dwell */
  414. dd->f_gpio_mod(dd, mask, mask, mask);
  415. }
  416. int qib_qsfp_dump(struct qib_pportdata *ppd, char *buf, int len)
  417. {
  418. struct qib_qsfp_cache cd;
  419. u8 bin_buff[QSFP_DUMP_CHUNK];
  420. char lenstr[6];
  421. int sofar, ret;
  422. int bidx = 0;
  423. sofar = 0;
  424. ret = qib_refresh_qsfp_cache(ppd, &cd);
  425. if (ret < 0)
  426. goto bail;
  427. lenstr[0] = ' ';
  428. lenstr[1] = '\0';
  429. if (QSFP_IS_CU(cd.tech))
  430. sprintf(lenstr, "%dM ", cd.len);
  431. sofar += scnprintf(buf + sofar, len - sofar, "PWR:%.3sW\n", pwr_codes +
  432. (QSFP_PWR(cd.pwr) * 4));
  433. sofar += scnprintf(buf + sofar, len - sofar, "TECH:%s%s\n", lenstr,
  434. qib_qsfp_devtech[cd.tech >> 4]);
  435. sofar += scnprintf(buf + sofar, len - sofar, "Vendor:%.*s\n",
  436. QSFP_VEND_LEN, cd.vendor);
  437. sofar += scnprintf(buf + sofar, len - sofar, "OUI:%06X\n",
  438. QSFP_OUI(cd.oui));
  439. sofar += scnprintf(buf + sofar, len - sofar, "Part#:%.*s\n",
  440. QSFP_PN_LEN, cd.partnum);
  441. sofar += scnprintf(buf + sofar, len - sofar, "Rev:%.*s\n",
  442. QSFP_REV_LEN, cd.rev);
  443. if (QSFP_IS_CU(cd.tech))
  444. sofar += scnprintf(buf + sofar, len - sofar, "Atten:%d, %d\n",
  445. QSFP_ATTEN_SDR(cd.atten),
  446. QSFP_ATTEN_DDR(cd.atten));
  447. sofar += scnprintf(buf + sofar, len - sofar, "Serial:%.*s\n",
  448. QSFP_SN_LEN, cd.serial);
  449. sofar += scnprintf(buf + sofar, len - sofar, "Date:%.*s\n",
  450. QSFP_DATE_LEN, cd.date);
  451. sofar += scnprintf(buf + sofar, len - sofar, "Lot:%.*s\n",
  452. QSFP_LOT_LEN, cd.lot);
  453. while (bidx < QSFP_DEFAULT_HDR_CNT) {
  454. int iidx;
  455. ret = qsfp_read(ppd, bidx, bin_buff, QSFP_DUMP_CHUNK);
  456. if (ret < 0)
  457. goto bail;
  458. for (iidx = 0; iidx < ret; ++iidx) {
  459. sofar += scnprintf(buf + sofar, len-sofar, " %02X",
  460. bin_buff[iidx]);
  461. }
  462. sofar += scnprintf(buf + sofar, len - sofar, "\n");
  463. bidx += QSFP_DUMP_CHUNK;
  464. }
  465. ret = sofar;
  466. bail:
  467. return ret;
  468. }