qib_eeprom.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2012 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
  4. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/delay.h>
  35. #include <linux/pci.h>
  36. #include <linux/vmalloc.h>
  37. #include "qib.h"
  38. /*
  39. * Functions specific to the serial EEPROM on cards handled by ib_qib.
  40. * The actual serail interface code is in qib_twsi.c. This file is a client
  41. */
  42. /**
  43. * qib_eeprom_read - receives bytes from the eeprom via I2C
  44. * @dd: the qlogic_ib device
  45. * @eeprom_offset: address to read from
  46. * @buffer: where to store result
  47. * @len: number of bytes to receive
  48. */
  49. int qib_eeprom_read(struct qib_devdata *dd, u8 eeprom_offset,
  50. void *buff, int len)
  51. {
  52. int ret;
  53. ret = mutex_lock_interruptible(&dd->eep_lock);
  54. if (!ret) {
  55. ret = qib_twsi_reset(dd);
  56. if (ret)
  57. qib_dev_err(dd, "EEPROM Reset for read failed\n");
  58. else
  59. ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev,
  60. eeprom_offset, buff, len);
  61. mutex_unlock(&dd->eep_lock);
  62. }
  63. return ret;
  64. }
  65. /*
  66. * Actually update the eeprom, first doing write enable if
  67. * needed, then restoring write enable state.
  68. * Must be called with eep_lock held
  69. */
  70. static int eeprom_write_with_enable(struct qib_devdata *dd, u8 offset,
  71. const void *buf, int len)
  72. {
  73. int ret, pwen;
  74. pwen = dd->f_eeprom_wen(dd, 1);
  75. ret = qib_twsi_reset(dd);
  76. if (ret)
  77. qib_dev_err(dd, "EEPROM Reset for write failed\n");
  78. else
  79. ret = qib_twsi_blk_wr(dd, dd->twsi_eeprom_dev,
  80. offset, buf, len);
  81. dd->f_eeprom_wen(dd, pwen);
  82. return ret;
  83. }
  84. /**
  85. * qib_eeprom_write - writes data to the eeprom via I2C
  86. * @dd: the qlogic_ib device
  87. * @eeprom_offset: where to place data
  88. * @buffer: data to write
  89. * @len: number of bytes to write
  90. */
  91. int qib_eeprom_write(struct qib_devdata *dd, u8 eeprom_offset,
  92. const void *buff, int len)
  93. {
  94. int ret;
  95. ret = mutex_lock_interruptible(&dd->eep_lock);
  96. if (!ret) {
  97. ret = eeprom_write_with_enable(dd, eeprom_offset, buff, len);
  98. mutex_unlock(&dd->eep_lock);
  99. }
  100. return ret;
  101. }
  102. static u8 flash_csum(struct qib_flash *ifp, int adjust)
  103. {
  104. u8 *ip = (u8 *) ifp;
  105. u8 csum = 0, len;
  106. /*
  107. * Limit length checksummed to max length of actual data.
  108. * Checksum of erased eeprom will still be bad, but we avoid
  109. * reading past the end of the buffer we were passed.
  110. */
  111. len = ifp->if_length;
  112. if (len > sizeof(struct qib_flash))
  113. len = sizeof(struct qib_flash);
  114. while (len--)
  115. csum += *ip++;
  116. csum -= ifp->if_csum;
  117. csum = ~csum;
  118. if (adjust)
  119. ifp->if_csum = csum;
  120. return csum;
  121. }
  122. /**
  123. * qib_get_eeprom_info- get the GUID et al. from the TSWI EEPROM device
  124. * @dd: the qlogic_ib device
  125. *
  126. * We have the capability to use the nguid field, and get
  127. * the guid from the first chip's flash, to use for all of them.
  128. */
  129. void qib_get_eeprom_info(struct qib_devdata *dd)
  130. {
  131. void *buf;
  132. struct qib_flash *ifp;
  133. __be64 guid;
  134. int len, eep_stat;
  135. u8 csum, *bguid;
  136. int t = dd->unit;
  137. struct qib_devdata *dd0 = qib_lookup(0);
  138. if (t && dd0->nguid > 1 && t <= dd0->nguid) {
  139. u8 oguid;
  140. dd->base_guid = dd0->base_guid;
  141. bguid = (u8 *) &dd->base_guid;
  142. oguid = bguid[7];
  143. bguid[7] += t;
  144. if (oguid > bguid[7]) {
  145. if (bguid[6] == 0xff) {
  146. if (bguid[5] == 0xff) {
  147. qib_dev_err(dd,
  148. "Can't set %s GUID from base, wraps to OUI!\n",
  149. qib_get_unit_name(t));
  150. dd->base_guid = 0;
  151. goto bail;
  152. }
  153. bguid[5]++;
  154. }
  155. bguid[6]++;
  156. }
  157. dd->nguid = 1;
  158. goto bail;
  159. }
  160. /*
  161. * Read full flash, not just currently used part, since it may have
  162. * been written with a newer definition.
  163. * */
  164. len = sizeof(struct qib_flash);
  165. buf = vmalloc(len);
  166. if (!buf) {
  167. qib_dev_err(dd,
  168. "Couldn't allocate memory to read %u bytes from eeprom for GUID\n",
  169. len);
  170. goto bail;
  171. }
  172. /*
  173. * Use "public" eeprom read function, which does locking and
  174. * figures out device. This will migrate to chip-specific.
  175. */
  176. eep_stat = qib_eeprom_read(dd, 0, buf, len);
  177. if (eep_stat) {
  178. qib_dev_err(dd, "Failed reading GUID from eeprom\n");
  179. goto done;
  180. }
  181. ifp = (struct qib_flash *)buf;
  182. csum = flash_csum(ifp, 0);
  183. if (csum != ifp->if_csum) {
  184. qib_devinfo(dd->pcidev,
  185. "Bad I2C flash checksum: 0x%x, not 0x%x\n",
  186. csum, ifp->if_csum);
  187. goto done;
  188. }
  189. if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||
  190. *(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {
  191. qib_dev_err(dd,
  192. "Invalid GUID %llx from flash; ignoring\n",
  193. *(unsigned long long *) ifp->if_guid);
  194. /* don't allow GUID if all 0 or all 1's */
  195. goto done;
  196. }
  197. /* complain, but allow it */
  198. if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
  199. qib_devinfo(dd->pcidev,
  200. "Warning, GUID %llx is default, probably not correct!\n",
  201. *(unsigned long long *) ifp->if_guid);
  202. bguid = ifp->if_guid;
  203. if (!bguid[0] && !bguid[1] && !bguid[2]) {
  204. /*
  205. * Original incorrect GUID format in flash; fix in
  206. * core copy, by shifting up 2 octets; don't need to
  207. * change top octet, since both it and shifted are 0.
  208. */
  209. bguid[1] = bguid[3];
  210. bguid[2] = bguid[4];
  211. bguid[3] = 0;
  212. bguid[4] = 0;
  213. guid = *(__be64 *) ifp->if_guid;
  214. } else
  215. guid = *(__be64 *) ifp->if_guid;
  216. dd->base_guid = guid;
  217. dd->nguid = ifp->if_numguid;
  218. /*
  219. * Things are slightly complicated by the desire to transparently
  220. * support both the Pathscale 10-digit serial number and the QLogic
  221. * 13-character version.
  222. */
  223. if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] &&
  224. ((u8 *) ifp->if_sprefix)[0] != 0xFF) {
  225. char *snp = dd->serial;
  226. /*
  227. * This board has a Serial-prefix, which is stored
  228. * elsewhere for backward-compatibility.
  229. */
  230. memcpy(snp, ifp->if_sprefix, sizeof(ifp->if_sprefix));
  231. snp[sizeof(ifp->if_sprefix)] = '\0';
  232. len = strlen(snp);
  233. snp += len;
  234. len = sizeof(dd->serial) - len;
  235. if (len > sizeof(ifp->if_serial))
  236. len = sizeof(ifp->if_serial);
  237. memcpy(snp, ifp->if_serial, len);
  238. } else {
  239. memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
  240. }
  241. if (!strstr(ifp->if_comment, "Tested successfully"))
  242. qib_dev_err(dd,
  243. "Board SN %s did not pass functional test: %s\n",
  244. dd->serial, ifp->if_comment);
  245. done:
  246. vfree(buf);
  247. bail:;
  248. }