qsfp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  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.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  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.
  12. *
  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.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  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
  29. * distribution.
  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.
  33. *
  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.
  45. *
  46. */
  47. #include <linux/delay.h>
  48. #include <linux/pci.h>
  49. #include <linux/vmalloc.h>
  50. #include "hfi.h"
  51. /* for the given bus number, return the CSR for reading an i2c line */
  52. static inline u32 i2c_in_csr(u32 bus_num)
  53. {
  54. return bus_num ? ASIC_QSFP2_IN : ASIC_QSFP1_IN;
  55. }
  56. /* for the given bus number, return the CSR for writing an i2c line */
  57. static inline u32 i2c_oe_csr(u32 bus_num)
  58. {
  59. return bus_num ? ASIC_QSFP2_OE : ASIC_QSFP1_OE;
  60. }
  61. static void hfi1_setsda(void *data, int state)
  62. {
  63. struct hfi1_i2c_bus *bus = (struct hfi1_i2c_bus *)data;
  64. struct hfi1_devdata *dd = bus->controlling_dd;
  65. u64 reg;
  66. u32 target_oe;
  67. target_oe = i2c_oe_csr(bus->num);
  68. reg = read_csr(dd, target_oe);
  69. /*
  70. * The OE bit value is inverted and connected to the pin. When
  71. * OE is 0 the pin is left to be pulled up, when the OE is 1
  72. * the pin is driven low. This matches the "open drain" or "open
  73. * collector" convention.
  74. */
  75. if (state)
  76. reg &= ~QSFP_HFI0_I2CDAT;
  77. else
  78. reg |= QSFP_HFI0_I2CDAT;
  79. write_csr(dd, target_oe, reg);
  80. /* do a read to force the write into the chip */
  81. (void)read_csr(dd, target_oe);
  82. }
  83. static void hfi1_setscl(void *data, int state)
  84. {
  85. struct hfi1_i2c_bus *bus = (struct hfi1_i2c_bus *)data;
  86. struct hfi1_devdata *dd = bus->controlling_dd;
  87. u64 reg;
  88. u32 target_oe;
  89. target_oe = i2c_oe_csr(bus->num);
  90. reg = read_csr(dd, target_oe);
  91. /*
  92. * The OE bit value is inverted and connected to the pin. When
  93. * OE is 0 the pin is left to be pulled up, when the OE is 1
  94. * the pin is driven low. This matches the "open drain" or "open
  95. * collector" convention.
  96. */
  97. if (state)
  98. reg &= ~QSFP_HFI0_I2CCLK;
  99. else
  100. reg |= QSFP_HFI0_I2CCLK;
  101. write_csr(dd, target_oe, reg);
  102. /* do a read to force the write into the chip */
  103. (void)read_csr(dd, target_oe);
  104. }
  105. static int hfi1_getsda(void *data)
  106. {
  107. struct hfi1_i2c_bus *bus = (struct hfi1_i2c_bus *)data;
  108. u64 reg;
  109. u32 target_in;
  110. hfi1_setsda(data, 1); /* clear OE so we do not pull line down */
  111. udelay(2); /* 1us pull up + 250ns hold */
  112. target_in = i2c_in_csr(bus->num);
  113. reg = read_csr(bus->controlling_dd, target_in);
  114. return !!(reg & QSFP_HFI0_I2CDAT);
  115. }
  116. static int hfi1_getscl(void *data)
  117. {
  118. struct hfi1_i2c_bus *bus = (struct hfi1_i2c_bus *)data;
  119. u64 reg;
  120. u32 target_in;
  121. hfi1_setscl(data, 1); /* clear OE so we do not pull line down */
  122. udelay(2); /* 1us pull up + 250ns hold */
  123. target_in = i2c_in_csr(bus->num);
  124. reg = read_csr(bus->controlling_dd, target_in);
  125. return !!(reg & QSFP_HFI0_I2CCLK);
  126. }
  127. /*
  128. * Allocate and initialize the given i2c bus number.
  129. * Returns NULL on failure.
  130. */
  131. static struct hfi1_i2c_bus *init_i2c_bus(struct hfi1_devdata *dd,
  132. struct hfi1_asic_data *ad, int num)
  133. {
  134. struct hfi1_i2c_bus *bus;
  135. int ret;
  136. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  137. if (!bus)
  138. return NULL;
  139. bus->controlling_dd = dd;
  140. bus->num = num; /* our bus number */
  141. bus->algo.setsda = hfi1_setsda;
  142. bus->algo.setscl = hfi1_setscl;
  143. bus->algo.getsda = hfi1_getsda;
  144. bus->algo.getscl = hfi1_getscl;
  145. bus->algo.udelay = 5;
  146. bus->algo.timeout = usecs_to_jiffies(100000);
  147. bus->algo.data = bus;
  148. bus->adapter.owner = THIS_MODULE;
  149. bus->adapter.algo_data = &bus->algo;
  150. bus->adapter.dev.parent = &dd->pcidev->dev;
  151. snprintf(bus->adapter.name, sizeof(bus->adapter.name),
  152. "hfi1_i2c%d", num);
  153. ret = i2c_bit_add_bus(&bus->adapter);
  154. if (ret) {
  155. dd_dev_info(dd, "%s: unable to add i2c bus %d, err %d\n",
  156. __func__, num, ret);
  157. kfree(bus);
  158. return NULL;
  159. }
  160. return bus;
  161. }
  162. /*
  163. * Initialize i2c buses.
  164. * Return 0 on success, -errno on error.
  165. */
  166. int set_up_i2c(struct hfi1_devdata *dd, struct hfi1_asic_data *ad)
  167. {
  168. ad->i2c_bus0 = init_i2c_bus(dd, ad, 0);
  169. ad->i2c_bus1 = init_i2c_bus(dd, ad, 1);
  170. if (!ad->i2c_bus0 || !ad->i2c_bus1)
  171. return -ENOMEM;
  172. return 0;
  173. };
  174. static void clean_i2c_bus(struct hfi1_i2c_bus *bus)
  175. {
  176. if (bus) {
  177. i2c_del_adapter(&bus->adapter);
  178. kfree(bus);
  179. }
  180. }
  181. void clean_up_i2c(struct hfi1_devdata *dd, struct hfi1_asic_data *ad)
  182. {
  183. if (!ad)
  184. return;
  185. clean_i2c_bus(ad->i2c_bus0);
  186. ad->i2c_bus0 = NULL;
  187. clean_i2c_bus(ad->i2c_bus1);
  188. ad->i2c_bus1 = NULL;
  189. }
  190. static int i2c_bus_write(struct hfi1_devdata *dd, struct hfi1_i2c_bus *i2c,
  191. u8 slave_addr, int offset, int offset_size,
  192. u8 *data, u16 len)
  193. {
  194. int ret;
  195. int num_msgs;
  196. u8 offset_bytes[2];
  197. struct i2c_msg msgs[2];
  198. switch (offset_size) {
  199. case 0:
  200. num_msgs = 1;
  201. msgs[0].addr = slave_addr;
  202. msgs[0].flags = 0;
  203. msgs[0].len = len;
  204. msgs[0].buf = data;
  205. break;
  206. case 2:
  207. offset_bytes[1] = (offset >> 8) & 0xff;
  208. /* fall through */
  209. case 1:
  210. num_msgs = 2;
  211. offset_bytes[0] = offset & 0xff;
  212. msgs[0].addr = slave_addr;
  213. msgs[0].flags = 0;
  214. msgs[0].len = offset_size;
  215. msgs[0].buf = offset_bytes;
  216. msgs[1].addr = slave_addr;
  217. msgs[1].flags = I2C_M_NOSTART,
  218. msgs[1].len = len;
  219. msgs[1].buf = data;
  220. break;
  221. default:
  222. return -EINVAL;
  223. }
  224. i2c->controlling_dd = dd;
  225. ret = i2c_transfer(&i2c->adapter, msgs, num_msgs);
  226. if (ret != num_msgs) {
  227. dd_dev_err(dd, "%s: bus %d, i2c slave 0x%x, offset 0x%x, len 0x%x; write failed, ret %d\n",
  228. __func__, i2c->num, slave_addr, offset, len, ret);
  229. return ret < 0 ? ret : -EIO;
  230. }
  231. return 0;
  232. }
  233. static int i2c_bus_read(struct hfi1_devdata *dd, struct hfi1_i2c_bus *bus,
  234. u8 slave_addr, int offset, int offset_size,
  235. u8 *data, u16 len)
  236. {
  237. int ret;
  238. int num_msgs;
  239. u8 offset_bytes[2];
  240. struct i2c_msg msgs[2];
  241. switch (offset_size) {
  242. case 0:
  243. num_msgs = 1;
  244. msgs[0].addr = slave_addr;
  245. msgs[0].flags = I2C_M_RD;
  246. msgs[0].len = len;
  247. msgs[0].buf = data;
  248. break;
  249. case 2:
  250. offset_bytes[1] = (offset >> 8) & 0xff;
  251. /* fall through */
  252. case 1:
  253. num_msgs = 2;
  254. offset_bytes[0] = offset & 0xff;
  255. msgs[0].addr = slave_addr;
  256. msgs[0].flags = 0;
  257. msgs[0].len = offset_size;
  258. msgs[0].buf = offset_bytes;
  259. msgs[1].addr = slave_addr;
  260. msgs[1].flags = I2C_M_RD,
  261. msgs[1].len = len;
  262. msgs[1].buf = data;
  263. break;
  264. default:
  265. return -EINVAL;
  266. }
  267. bus->controlling_dd = dd;
  268. ret = i2c_transfer(&bus->adapter, msgs, num_msgs);
  269. if (ret != num_msgs) {
  270. dd_dev_err(dd, "%s: bus %d, i2c slave 0x%x, offset 0x%x, len 0x%x; read failed, ret %d\n",
  271. __func__, bus->num, slave_addr, offset, len, ret);
  272. return ret < 0 ? ret : -EIO;
  273. }
  274. return 0;
  275. }
  276. /*
  277. * Raw i2c write. No set-up or lock checking.
  278. *
  279. * Return 0 on success, -errno on error.
  280. */
  281. static int __i2c_write(struct hfi1_pportdata *ppd, u32 target, int i2c_addr,
  282. int offset, void *bp, int len)
  283. {
  284. struct hfi1_devdata *dd = ppd->dd;
  285. struct hfi1_i2c_bus *bus;
  286. u8 slave_addr;
  287. int offset_size;
  288. bus = target ? dd->asic_data->i2c_bus1 : dd->asic_data->i2c_bus0;
  289. slave_addr = (i2c_addr & 0xff) >> 1; /* convert to 7-bit addr */
  290. offset_size = (i2c_addr >> 8) & 0x3;
  291. return i2c_bus_write(dd, bus, slave_addr, offset, offset_size, bp, len);
  292. }
  293. /*
  294. * Caller must hold the i2c chain resource.
  295. *
  296. * Return number of bytes written, or -errno.
  297. */
  298. int i2c_write(struct hfi1_pportdata *ppd, u32 target, int i2c_addr, int offset,
  299. void *bp, int len)
  300. {
  301. int ret;
  302. if (!check_chip_resource(ppd->dd, i2c_target(target), __func__))
  303. return -EACCES;
  304. ret = __i2c_write(ppd, target, i2c_addr, offset, bp, len);
  305. if (ret)
  306. return ret;
  307. return len;
  308. }
  309. /*
  310. * Raw i2c read. No set-up or lock checking.
  311. *
  312. * Return 0 on success, -errno on error.
  313. */
  314. static int __i2c_read(struct hfi1_pportdata *ppd, u32 target, int i2c_addr,
  315. int offset, void *bp, int len)
  316. {
  317. struct hfi1_devdata *dd = ppd->dd;
  318. struct hfi1_i2c_bus *bus;
  319. u8 slave_addr;
  320. int offset_size;
  321. bus = target ? dd->asic_data->i2c_bus1 : dd->asic_data->i2c_bus0;
  322. slave_addr = (i2c_addr & 0xff) >> 1; /* convert to 7-bit addr */
  323. offset_size = (i2c_addr >> 8) & 0x3;
  324. return i2c_bus_read(dd, bus, slave_addr, offset, offset_size, bp, len);
  325. }
  326. /*
  327. * Caller must hold the i2c chain resource.
  328. *
  329. * Return number of bytes read, or -errno.
  330. */
  331. int i2c_read(struct hfi1_pportdata *ppd, u32 target, int i2c_addr, int offset,
  332. void *bp, int len)
  333. {
  334. int ret;
  335. if (!check_chip_resource(ppd->dd, i2c_target(target), __func__))
  336. return -EACCES;
  337. ret = __i2c_read(ppd, target, i2c_addr, offset, bp, len);
  338. if (ret)
  339. return ret;
  340. return len;
  341. }
  342. /*
  343. * Write page n, offset m of QSFP memory as defined by SFF 8636
  344. * by writing @addr = ((256 * n) + m)
  345. *
  346. * Caller must hold the i2c chain resource.
  347. *
  348. * Return number of bytes written or -errno.
  349. */
  350. int qsfp_write(struct hfi1_pportdata *ppd, u32 target, int addr, void *bp,
  351. int len)
  352. {
  353. int count = 0;
  354. int offset;
  355. int nwrite;
  356. int ret = 0;
  357. u8 page;
  358. if (!check_chip_resource(ppd->dd, i2c_target(target), __func__))
  359. return -EACCES;
  360. while (count < len) {
  361. /*
  362. * Set the qsfp page based on a zero-based address
  363. * and a page size of QSFP_PAGESIZE bytes.
  364. */
  365. page = (u8)(addr / QSFP_PAGESIZE);
  366. ret = __i2c_write(ppd, target, QSFP_DEV | QSFP_OFFSET_SIZE,
  367. QSFP_PAGE_SELECT_BYTE_OFFS, &page, 1);
  368. /* QSFPs require a 5-10msec delay after write operations */
  369. mdelay(5);
  370. if (ret) {
  371. hfi1_dev_porterr(ppd->dd, ppd->port,
  372. "QSFP chain %d can't write QSFP_PAGE_SELECT_BYTE: %d\n",
  373. target, ret);
  374. break;
  375. }
  376. offset = addr % QSFP_PAGESIZE;
  377. nwrite = len - count;
  378. /* truncate write to boundary if crossing boundary */
  379. if (((addr % QSFP_RW_BOUNDARY) + nwrite) > QSFP_RW_BOUNDARY)
  380. nwrite = QSFP_RW_BOUNDARY - (addr % QSFP_RW_BOUNDARY);
  381. ret = __i2c_write(ppd, target, QSFP_DEV | QSFP_OFFSET_SIZE,
  382. offset, bp + count, nwrite);
  383. /* QSFPs require a 5-10msec delay after write operations */
  384. mdelay(5);
  385. if (ret) /* stop on error */
  386. break;
  387. count += nwrite;
  388. addr += nwrite;
  389. }
  390. if (ret < 0)
  391. return ret;
  392. return count;
  393. }
  394. /*
  395. * Perform a stand-alone single QSFP write. Acquire the resource, do the
  396. * write, then release the resource.
  397. */
  398. int one_qsfp_write(struct hfi1_pportdata *ppd, u32 target, int addr, void *bp,
  399. int len)
  400. {
  401. struct hfi1_devdata *dd = ppd->dd;
  402. u32 resource = qsfp_resource(dd);
  403. int ret;
  404. ret = acquire_chip_resource(dd, resource, QSFP_WAIT);
  405. if (ret)
  406. return ret;
  407. ret = qsfp_write(ppd, target, addr, bp, len);
  408. release_chip_resource(dd, resource);
  409. return ret;
  410. }
  411. /*
  412. * Access page n, offset m of QSFP memory as defined by SFF 8636
  413. * by reading @addr = ((256 * n) + m)
  414. *
  415. * Caller must hold the i2c chain resource.
  416. *
  417. * Return the number of bytes read or -errno.
  418. */
  419. int qsfp_read(struct hfi1_pportdata *ppd, u32 target, int addr, void *bp,
  420. int len)
  421. {
  422. int count = 0;
  423. int offset;
  424. int nread;
  425. int ret = 0;
  426. u8 page;
  427. if (!check_chip_resource(ppd->dd, i2c_target(target), __func__))
  428. return -EACCES;
  429. while (count < len) {
  430. /*
  431. * Set the qsfp page based on a zero-based address
  432. * and a page size of QSFP_PAGESIZE bytes.
  433. */
  434. page = (u8)(addr / QSFP_PAGESIZE);
  435. ret = __i2c_write(ppd, target, QSFP_DEV | QSFP_OFFSET_SIZE,
  436. QSFP_PAGE_SELECT_BYTE_OFFS, &page, 1);
  437. /* QSFPs require a 5-10msec delay after write operations */
  438. mdelay(5);
  439. if (ret) {
  440. hfi1_dev_porterr(ppd->dd, ppd->port,
  441. "QSFP chain %d can't write QSFP_PAGE_SELECT_BYTE: %d\n",
  442. target, ret);
  443. break;
  444. }
  445. offset = addr % QSFP_PAGESIZE;
  446. nread = len - count;
  447. /* truncate read to boundary if crossing boundary */
  448. if (((addr % QSFP_RW_BOUNDARY) + nread) > QSFP_RW_BOUNDARY)
  449. nread = QSFP_RW_BOUNDARY - (addr % QSFP_RW_BOUNDARY);
  450. ret = __i2c_read(ppd, target, QSFP_DEV | QSFP_OFFSET_SIZE,
  451. offset, bp + count, nread);
  452. if (ret) /* stop on error */
  453. break;
  454. count += nread;
  455. addr += nread;
  456. }
  457. if (ret < 0)
  458. return ret;
  459. return count;
  460. }
  461. /*
  462. * Perform a stand-alone single QSFP read. Acquire the resource, do the
  463. * read, then release the resource.
  464. */
  465. int one_qsfp_read(struct hfi1_pportdata *ppd, u32 target, int addr, void *bp,
  466. int len)
  467. {
  468. struct hfi1_devdata *dd = ppd->dd;
  469. u32 resource = qsfp_resource(dd);
  470. int ret;
  471. ret = acquire_chip_resource(dd, resource, QSFP_WAIT);
  472. if (ret)
  473. return ret;
  474. ret = qsfp_read(ppd, target, addr, bp, len);
  475. release_chip_resource(dd, resource);
  476. return ret;
  477. }
  478. /*
  479. * This function caches the QSFP memory range in 128 byte chunks.
  480. * As an example, the next byte after address 255 is byte 128 from
  481. * upper page 01H (if existing) rather than byte 0 from lower page 00H.
  482. * Access page n, offset m of QSFP memory as defined by SFF 8636
  483. * in the cache by reading byte ((128 * n) + m)
  484. * The calls to qsfp_{read,write} in this function correctly handle the
  485. * address map difference between this mapping and the mapping implemented
  486. * by those functions
  487. *
  488. * The caller must be holding the QSFP i2c chain resource.
  489. */
  490. int refresh_qsfp_cache(struct hfi1_pportdata *ppd, struct qsfp_data *cp)
  491. {
  492. u32 target = ppd->dd->hfi1_id;
  493. int ret;
  494. unsigned long flags;
  495. u8 *cache = &cp->cache[0];
  496. /* ensure sane contents on invalid reads, for cable swaps */
  497. memset(cache, 0, (QSFP_MAX_NUM_PAGES * 128));
  498. spin_lock_irqsave(&ppd->qsfp_info.qsfp_lock, flags);
  499. ppd->qsfp_info.cache_valid = 0;
  500. spin_unlock_irqrestore(&ppd->qsfp_info.qsfp_lock, flags);
  501. if (!qsfp_mod_present(ppd)) {
  502. ret = -ENODEV;
  503. goto bail;
  504. }
  505. ret = qsfp_read(ppd, target, 0, cache, QSFP_PAGESIZE);
  506. if (ret != QSFP_PAGESIZE) {
  507. dd_dev_info(ppd->dd,
  508. "%s: Page 0 read failed, expected %d, got %d\n",
  509. __func__, QSFP_PAGESIZE, ret);
  510. goto bail;
  511. }
  512. /* Is paging enabled? */
  513. if (!(cache[2] & 4)) {
  514. /* Paging enabled, page 03 required */
  515. if ((cache[195] & 0xC0) == 0xC0) {
  516. /* all */
  517. ret = qsfp_read(ppd, target, 384, cache + 256, 128);
  518. if (ret <= 0 || ret != 128) {
  519. dd_dev_info(ppd->dd, "%s failed\n", __func__);
  520. goto bail;
  521. }
  522. ret = qsfp_read(ppd, target, 640, cache + 384, 128);
  523. if (ret <= 0 || ret != 128) {
  524. dd_dev_info(ppd->dd, "%s failed\n", __func__);
  525. goto bail;
  526. }
  527. ret = qsfp_read(ppd, target, 896, cache + 512, 128);
  528. if (ret <= 0 || ret != 128) {
  529. dd_dev_info(ppd->dd, "%s failed\n", __func__);
  530. goto bail;
  531. }
  532. } else if ((cache[195] & 0x80) == 0x80) {
  533. /* only page 2 and 3 */
  534. ret = qsfp_read(ppd, target, 640, cache + 384, 128);
  535. if (ret <= 0 || ret != 128) {
  536. dd_dev_info(ppd->dd, "%s failed\n", __func__);
  537. goto bail;
  538. }
  539. ret = qsfp_read(ppd, target, 896, cache + 512, 128);
  540. if (ret <= 0 || ret != 128) {
  541. dd_dev_info(ppd->dd, "%s failed\n", __func__);
  542. goto bail;
  543. }
  544. } else if ((cache[195] & 0x40) == 0x40) {
  545. /* only page 1 and 3 */
  546. ret = qsfp_read(ppd, target, 384, cache + 256, 128);
  547. if (ret <= 0 || ret != 128) {
  548. dd_dev_info(ppd->dd, "%s failed\n", __func__);
  549. goto bail;
  550. }
  551. ret = qsfp_read(ppd, target, 896, cache + 512, 128);
  552. if (ret <= 0 || ret != 128) {
  553. dd_dev_info(ppd->dd, "%s failed\n", __func__);
  554. goto bail;
  555. }
  556. } else {
  557. /* only page 3 */
  558. ret = qsfp_read(ppd, target, 896, cache + 512, 128);
  559. if (ret <= 0 || ret != 128) {
  560. dd_dev_info(ppd->dd, "%s failed\n", __func__);
  561. goto bail;
  562. }
  563. }
  564. }
  565. spin_lock_irqsave(&ppd->qsfp_info.qsfp_lock, flags);
  566. ppd->qsfp_info.cache_valid = 1;
  567. ppd->qsfp_info.cache_refresh_required = 0;
  568. spin_unlock_irqrestore(&ppd->qsfp_info.qsfp_lock, flags);
  569. return 0;
  570. bail:
  571. memset(cache, 0, (QSFP_MAX_NUM_PAGES * 128));
  572. return ret;
  573. }
  574. const char * const hfi1_qsfp_devtech[16] = {
  575. "850nm VCSEL", "1310nm VCSEL", "1550nm VCSEL", "1310nm FP",
  576. "1310nm DFB", "1550nm DFB", "1310nm EML", "1550nm EML",
  577. "Cu Misc", "1490nm DFB", "Cu NoEq", "Cu Eq",
  578. "Undef", "Cu Active BothEq", "Cu FarEq", "Cu NearEq"
  579. };
  580. #define QSFP_DUMP_CHUNK 16 /* Holds longest string */
  581. #define QSFP_DEFAULT_HDR_CNT 224
  582. #define QSFP_PWR(pbyte) (((pbyte) >> 6) & 3)
  583. #define QSFP_HIGH_PWR(pbyte) ((pbyte) & 3)
  584. /* For use with QSFP_HIGH_PWR macro */
  585. #define QSFP_HIGH_PWR_UNUSED 0 /* Bits [1:0] = 00 implies low power module */
  586. /*
  587. * Takes power class byte [Page 00 Byte 129] in SFF 8636
  588. * Returns power class as integer (1 through 7, per SFF 8636 rev 2.4)
  589. */
  590. int get_qsfp_power_class(u8 power_byte)
  591. {
  592. if (QSFP_HIGH_PWR(power_byte) == QSFP_HIGH_PWR_UNUSED)
  593. /* power classes count from 1, their bit encodings from 0 */
  594. return (QSFP_PWR(power_byte) + 1);
  595. /*
  596. * 00 in the high power classes stands for unused, bringing
  597. * balance to the off-by-1 offset above, we add 4 here to
  598. * account for the difference between the low and high power
  599. * groups
  600. */
  601. return (QSFP_HIGH_PWR(power_byte) + 4);
  602. }
  603. int qsfp_mod_present(struct hfi1_pportdata *ppd)
  604. {
  605. struct hfi1_devdata *dd = ppd->dd;
  606. u64 reg;
  607. reg = read_csr(dd, dd->hfi1_id ? ASIC_QSFP2_IN : ASIC_QSFP1_IN);
  608. return !(reg & QSFP_HFI0_MODPRST_N);
  609. }
  610. /*
  611. * This function maps QSFP memory addresses in 128 byte chunks in the following
  612. * fashion per the CableInfo SMA query definition in the IBA 1.3 spec/OPA Gen 1
  613. * spec
  614. * For addr 000-127, lower page 00h
  615. * For addr 128-255, upper page 00h
  616. * For addr 256-383, upper page 01h
  617. * For addr 384-511, upper page 02h
  618. * For addr 512-639, upper page 03h
  619. *
  620. * For addresses beyond this range, it returns the invalid range of data buffer
  621. * set to 0.
  622. * For upper pages that are optional, if they are not valid, returns the
  623. * particular range of bytes in the data buffer set to 0.
  624. */
  625. int get_cable_info(struct hfi1_devdata *dd, u32 port_num, u32 addr, u32 len,
  626. u8 *data)
  627. {
  628. struct hfi1_pportdata *ppd;
  629. u32 excess_len = len;
  630. int ret = 0, offset = 0;
  631. if (port_num > dd->num_pports || port_num < 1) {
  632. dd_dev_info(dd, "%s: Invalid port number %d\n",
  633. __func__, port_num);
  634. ret = -EINVAL;
  635. goto set_zeroes;
  636. }
  637. ppd = dd->pport + (port_num - 1);
  638. if (!qsfp_mod_present(ppd)) {
  639. ret = -ENODEV;
  640. goto set_zeroes;
  641. }
  642. if (!ppd->qsfp_info.cache_valid) {
  643. ret = -EINVAL;
  644. goto set_zeroes;
  645. }
  646. if (addr >= (QSFP_MAX_NUM_PAGES * 128)) {
  647. ret = -ERANGE;
  648. goto set_zeroes;
  649. }
  650. if ((addr + len) > (QSFP_MAX_NUM_PAGES * 128)) {
  651. excess_len = (addr + len) - (QSFP_MAX_NUM_PAGES * 128);
  652. memcpy(data, &ppd->qsfp_info.cache[addr], (len - excess_len));
  653. data += (len - excess_len);
  654. goto set_zeroes;
  655. }
  656. memcpy(data, &ppd->qsfp_info.cache[addr], len);
  657. if (addr <= QSFP_MONITOR_VAL_END &&
  658. (addr + len) >= QSFP_MONITOR_VAL_START) {
  659. /* Overlap with the dynamic channel monitor range */
  660. if (addr < QSFP_MONITOR_VAL_START) {
  661. if (addr + len <= QSFP_MONITOR_VAL_END)
  662. len = addr + len - QSFP_MONITOR_VAL_START;
  663. else
  664. len = QSFP_MONITOR_RANGE;
  665. offset = QSFP_MONITOR_VAL_START - addr;
  666. addr = QSFP_MONITOR_VAL_START;
  667. } else if (addr == QSFP_MONITOR_VAL_START) {
  668. offset = 0;
  669. if (addr + len > QSFP_MONITOR_VAL_END)
  670. len = QSFP_MONITOR_RANGE;
  671. } else {
  672. offset = 0;
  673. if (addr + len > QSFP_MONITOR_VAL_END)
  674. len = QSFP_MONITOR_VAL_END - addr + 1;
  675. }
  676. /* Refresh the values of the dynamic monitors from the cable */
  677. ret = one_qsfp_read(ppd, dd->hfi1_id, addr, data + offset, len);
  678. if (ret != len) {
  679. ret = -EAGAIN;
  680. goto set_zeroes;
  681. }
  682. }
  683. return 0;
  684. set_zeroes:
  685. memset(data, 0, excess_len);
  686. return ret;
  687. }
  688. static const char *pwr_codes[8] = {"N/AW",
  689. "1.5W",
  690. "2.0W",
  691. "2.5W",
  692. "3.5W",
  693. "4.0W",
  694. "4.5W",
  695. "5.0W"
  696. };
  697. int qsfp_dump(struct hfi1_pportdata *ppd, char *buf, int len)
  698. {
  699. u8 *cache = &ppd->qsfp_info.cache[0];
  700. u8 bin_buff[QSFP_DUMP_CHUNK];
  701. char lenstr[6];
  702. int sofar;
  703. int bidx = 0;
  704. u8 *atten = &cache[QSFP_ATTEN_OFFS];
  705. u8 *vendor_oui = &cache[QSFP_VOUI_OFFS];
  706. u8 power_byte = 0;
  707. sofar = 0;
  708. lenstr[0] = ' ';
  709. lenstr[1] = '\0';
  710. if (ppd->qsfp_info.cache_valid) {
  711. if (QSFP_IS_CU(cache[QSFP_MOD_TECH_OFFS]))
  712. snprintf(lenstr, sizeof(lenstr), "%dM ",
  713. cache[QSFP_MOD_LEN_OFFS]);
  714. power_byte = cache[QSFP_MOD_PWR_OFFS];
  715. sofar += scnprintf(buf + sofar, len - sofar, "PWR:%.3sW\n",
  716. pwr_codes[get_qsfp_power_class(power_byte)]);
  717. sofar += scnprintf(buf + sofar, len - sofar, "TECH:%s%s\n",
  718. lenstr,
  719. hfi1_qsfp_devtech[(cache[QSFP_MOD_TECH_OFFS]) >> 4]);
  720. sofar += scnprintf(buf + sofar, len - sofar, "Vendor:%.*s\n",
  721. QSFP_VEND_LEN, &cache[QSFP_VEND_OFFS]);
  722. sofar += scnprintf(buf + sofar, len - sofar, "OUI:%06X\n",
  723. QSFP_OUI(vendor_oui));
  724. sofar += scnprintf(buf + sofar, len - sofar, "Part#:%.*s\n",
  725. QSFP_PN_LEN, &cache[QSFP_PN_OFFS]);
  726. sofar += scnprintf(buf + sofar, len - sofar, "Rev:%.*s\n",
  727. QSFP_REV_LEN, &cache[QSFP_REV_OFFS]);
  728. if (QSFP_IS_CU(cache[QSFP_MOD_TECH_OFFS]))
  729. sofar += scnprintf(buf + sofar, len - sofar,
  730. "Atten:%d, %d\n",
  731. QSFP_ATTEN_SDR(atten),
  732. QSFP_ATTEN_DDR(atten));
  733. sofar += scnprintf(buf + sofar, len - sofar, "Serial:%.*s\n",
  734. QSFP_SN_LEN, &cache[QSFP_SN_OFFS]);
  735. sofar += scnprintf(buf + sofar, len - sofar, "Date:%.*s\n",
  736. QSFP_DATE_LEN, &cache[QSFP_DATE_OFFS]);
  737. sofar += scnprintf(buf + sofar, len - sofar, "Lot:%.*s\n",
  738. QSFP_LOT_LEN, &cache[QSFP_LOT_OFFS]);
  739. while (bidx < QSFP_DEFAULT_HDR_CNT) {
  740. int iidx;
  741. memcpy(bin_buff, &cache[bidx], QSFP_DUMP_CHUNK);
  742. for (iidx = 0; iidx < QSFP_DUMP_CHUNK; ++iidx) {
  743. sofar += scnprintf(buf + sofar, len - sofar,
  744. " %02X", bin_buff[iidx]);
  745. }
  746. sofar += scnprintf(buf + sofar, len - sofar, "\n");
  747. bidx += QSFP_DUMP_CHUNK;
  748. }
  749. }
  750. return sofar;
  751. }