qib_eeprom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. /*
  38. * Functions specific to the serial EEPROM on cards handled by ib_qib.
  39. * The actual serail interface code is in qib_twsi.c. This file is a client
  40. */
  41. /**
  42. * qib_eeprom_read - receives bytes from the eeprom via I2C
  43. * @dd: the qlogic_ib device
  44. * @eeprom_offset: address to read from
  45. * @buffer: where to store result
  46. * @len: number of bytes to receive
  47. */
  48. int qib_eeprom_read(struct qib_devdata *dd, u8 eeprom_offset,
  49. void *buff, int len)
  50. {
  51. int ret;
  52. ret = mutex_lock_interruptible(&dd->eep_lock);
  53. if (!ret) {
  54. ret = qib_twsi_reset(dd);
  55. if (ret)
  56. qib_dev_err(dd, "EEPROM Reset for read failed\n");
  57. else
  58. ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev,
  59. eeprom_offset, buff, len);
  60. mutex_unlock(&dd->eep_lock);
  61. }
  62. return ret;
  63. }
  64. /*
  65. * Actually update the eeprom, first doing write enable if
  66. * needed, then restoring write enable state.
  67. * Must be called with eep_lock held
  68. */
  69. static int eeprom_write_with_enable(struct qib_devdata *dd, u8 offset,
  70. const void *buf, int len)
  71. {
  72. int ret, pwen;
  73. pwen = dd->f_eeprom_wen(dd, 1);
  74. ret = qib_twsi_reset(dd);
  75. if (ret)
  76. qib_dev_err(dd, "EEPROM Reset for write failed\n");
  77. else
  78. ret = qib_twsi_blk_wr(dd, dd->twsi_eeprom_dev,
  79. offset, buf, len);
  80. dd->f_eeprom_wen(dd, pwen);
  81. return ret;
  82. }
  83. /**
  84. * qib_eeprom_write - writes data to the eeprom via I2C
  85. * @dd: the qlogic_ib device
  86. * @eeprom_offset: where to place data
  87. * @buffer: data to write
  88. * @len: number of bytes to write
  89. */
  90. int qib_eeprom_write(struct qib_devdata *dd, u8 eeprom_offset,
  91. const void *buff, int len)
  92. {
  93. int ret;
  94. ret = mutex_lock_interruptible(&dd->eep_lock);
  95. if (!ret) {
  96. ret = eeprom_write_with_enable(dd, eeprom_offset, buff, len);
  97. mutex_unlock(&dd->eep_lock);
  98. }
  99. return ret;
  100. }
  101. static u8 flash_csum(struct qib_flash *ifp, int adjust)
  102. {
  103. u8 *ip = (u8 *) ifp;
  104. u8 csum = 0, len;
  105. /*
  106. * Limit length checksummed to max length of actual data.
  107. * Checksum of erased eeprom will still be bad, but we avoid
  108. * reading past the end of the buffer we were passed.
  109. */
  110. len = ifp->if_length;
  111. if (len > sizeof(struct qib_flash))
  112. len = sizeof(struct qib_flash);
  113. while (len--)
  114. csum += *ip++;
  115. csum -= ifp->if_csum;
  116. csum = ~csum;
  117. if (adjust)
  118. ifp->if_csum = csum;
  119. return csum;
  120. }
  121. /**
  122. * qib_get_eeprom_info- get the GUID et al. from the TSWI EEPROM device
  123. * @dd: the qlogic_ib device
  124. *
  125. * We have the capability to use the nguid field, and get
  126. * the guid from the first chip's flash, to use for all of them.
  127. */
  128. void qib_get_eeprom_info(struct qib_devdata *dd)
  129. {
  130. void *buf;
  131. struct qib_flash *ifp;
  132. __be64 guid;
  133. int len, eep_stat;
  134. u8 csum, *bguid;
  135. int t = dd->unit;
  136. struct qib_devdata *dd0 = qib_lookup(0);
  137. if (t && dd0->nguid > 1 && t <= dd0->nguid) {
  138. u8 oguid;
  139. dd->base_guid = dd0->base_guid;
  140. bguid = (u8 *) &dd->base_guid;
  141. oguid = bguid[7];
  142. bguid[7] += t;
  143. if (oguid > bguid[7]) {
  144. if (bguid[6] == 0xff) {
  145. if (bguid[5] == 0xff) {
  146. qib_dev_err(dd, "Can't set %s GUID"
  147. " from base, wraps to"
  148. " 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, "Couldn't allocate memory to read %u "
  168. "bytes from eeprom for GUID\n", len);
  169. goto bail;
  170. }
  171. /*
  172. * Use "public" eeprom read function, which does locking and
  173. * figures out device. This will migrate to chip-specific.
  174. */
  175. eep_stat = qib_eeprom_read(dd, 0, buf, len);
  176. if (eep_stat) {
  177. qib_dev_err(dd, "Failed reading GUID from eeprom\n");
  178. goto done;
  179. }
  180. ifp = (struct qib_flash *)buf;
  181. csum = flash_csum(ifp, 0);
  182. if (csum != ifp->if_csum) {
  183. qib_devinfo(dd->pcidev, "Bad I2C flash checksum: "
  184. "0x%x, not 0x%x\n", csum, ifp->if_csum);
  185. goto done;
  186. }
  187. if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||
  188. *(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {
  189. qib_dev_err(dd, "Invalid GUID %llx from flash; ignoring\n",
  190. *(unsigned long long *) ifp->if_guid);
  191. /* don't allow GUID if all 0 or all 1's */
  192. goto done;
  193. }
  194. /* complain, but allow it */
  195. if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
  196. qib_devinfo(dd->pcidev, "Warning, GUID %llx is "
  197. "default, probably not correct!\n",
  198. *(unsigned long long *) ifp->if_guid);
  199. bguid = ifp->if_guid;
  200. if (!bguid[0] && !bguid[1] && !bguid[2]) {
  201. /*
  202. * Original incorrect GUID format in flash; fix in
  203. * core copy, by shifting up 2 octets; don't need to
  204. * change top octet, since both it and shifted are 0.
  205. */
  206. bguid[1] = bguid[3];
  207. bguid[2] = bguid[4];
  208. bguid[3] = 0;
  209. bguid[4] = 0;
  210. guid = *(__be64 *) ifp->if_guid;
  211. } else
  212. guid = *(__be64 *) ifp->if_guid;
  213. dd->base_guid = guid;
  214. dd->nguid = ifp->if_numguid;
  215. /*
  216. * Things are slightly complicated by the desire to transparently
  217. * support both the Pathscale 10-digit serial number and the QLogic
  218. * 13-character version.
  219. */
  220. if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] &&
  221. ((u8 *) ifp->if_sprefix)[0] != 0xFF) {
  222. char *snp = dd->serial;
  223. /*
  224. * This board has a Serial-prefix, which is stored
  225. * elsewhere for backward-compatibility.
  226. */
  227. memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix);
  228. snp[sizeof ifp->if_sprefix] = '\0';
  229. len = strlen(snp);
  230. snp += len;
  231. len = (sizeof dd->serial) - len;
  232. if (len > sizeof ifp->if_serial)
  233. len = sizeof ifp->if_serial;
  234. memcpy(snp, ifp->if_serial, len);
  235. } else
  236. memcpy(dd->serial, ifp->if_serial,
  237. sizeof ifp->if_serial);
  238. if (!strstr(ifp->if_comment, "Tested successfully"))
  239. qib_dev_err(dd, "Board SN %s did not pass functional "
  240. "test: %s\n", dd->serial, ifp->if_comment);
  241. memcpy(&dd->eep_st_errs, &ifp->if_errcntp, QIB_EEP_LOG_CNT);
  242. /*
  243. * Power-on (actually "active") hours are kept as little-endian value
  244. * in EEPROM, but as seconds in a (possibly as small as 24-bit)
  245. * atomic_t while running.
  246. */
  247. atomic_set(&dd->active_time, 0);
  248. dd->eep_hrs = ifp->if_powerhour[0] | (ifp->if_powerhour[1] << 8);
  249. done:
  250. vfree(buf);
  251. bail:;
  252. }
  253. /**
  254. * qib_update_eeprom_log - copy active-time and error counters to eeprom
  255. * @dd: the qlogic_ib device
  256. *
  257. * Although the time is kept as seconds in the qib_devdata struct, it is
  258. * rounded to hours for re-write, as we have only 16 bits in EEPROM.
  259. * First-cut code reads whole (expected) struct qib_flash, modifies,
  260. * re-writes. Future direction: read/write only what we need, assuming
  261. * that the EEPROM had to have been "good enough" for driver init, and
  262. * if not, we aren't making it worse.
  263. *
  264. */
  265. int qib_update_eeprom_log(struct qib_devdata *dd)
  266. {
  267. void *buf;
  268. struct qib_flash *ifp;
  269. int len, hi_water;
  270. uint32_t new_time, new_hrs;
  271. u8 csum;
  272. int ret, idx;
  273. unsigned long flags;
  274. /* first, check if we actually need to do anything. */
  275. ret = 0;
  276. for (idx = 0; idx < QIB_EEP_LOG_CNT; ++idx) {
  277. if (dd->eep_st_new_errs[idx]) {
  278. ret = 1;
  279. break;
  280. }
  281. }
  282. new_time = atomic_read(&dd->active_time);
  283. if (ret == 0 && new_time < 3600)
  284. goto bail;
  285. /*
  286. * The quick-check above determined that there is something worthy
  287. * of logging, so get current contents and do a more detailed idea.
  288. * read full flash, not just currently used part, since it may have
  289. * been written with a newer definition
  290. */
  291. len = sizeof(struct qib_flash);
  292. buf = vmalloc(len);
  293. ret = 1;
  294. if (!buf) {
  295. qib_dev_err(dd, "Couldn't allocate memory to read %u "
  296. "bytes from eeprom for logging\n", len);
  297. goto bail;
  298. }
  299. /* Grab semaphore and read current EEPROM. If we get an
  300. * error, let go, but if not, keep it until we finish write.
  301. */
  302. ret = mutex_lock_interruptible(&dd->eep_lock);
  303. if (ret) {
  304. qib_dev_err(dd, "Unable to acquire EEPROM for logging\n");
  305. goto free_bail;
  306. }
  307. ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev, 0, buf, len);
  308. if (ret) {
  309. mutex_unlock(&dd->eep_lock);
  310. qib_dev_err(dd, "Unable read EEPROM for logging\n");
  311. goto free_bail;
  312. }
  313. ifp = (struct qib_flash *)buf;
  314. csum = flash_csum(ifp, 0);
  315. if (csum != ifp->if_csum) {
  316. mutex_unlock(&dd->eep_lock);
  317. qib_dev_err(dd, "EEPROM cks err (0x%02X, S/B 0x%02X)\n",
  318. csum, ifp->if_csum);
  319. ret = 1;
  320. goto free_bail;
  321. }
  322. hi_water = 0;
  323. spin_lock_irqsave(&dd->eep_st_lock, flags);
  324. for (idx = 0; idx < QIB_EEP_LOG_CNT; ++idx) {
  325. int new_val = dd->eep_st_new_errs[idx];
  326. if (new_val) {
  327. /*
  328. * If we have seen any errors, add to EEPROM values
  329. * We need to saturate at 0xFF (255) and we also
  330. * would need to adjust the checksum if we were
  331. * trying to minimize EEPROM traffic
  332. * Note that we add to actual current count in EEPROM,
  333. * in case it was altered while we were running.
  334. */
  335. new_val += ifp->if_errcntp[idx];
  336. if (new_val > 0xFF)
  337. new_val = 0xFF;
  338. if (ifp->if_errcntp[idx] != new_val) {
  339. ifp->if_errcntp[idx] = new_val;
  340. hi_water = offsetof(struct qib_flash,
  341. if_errcntp) + idx;
  342. }
  343. /*
  344. * update our shadow (used to minimize EEPROM
  345. * traffic), to match what we are about to write.
  346. */
  347. dd->eep_st_errs[idx] = new_val;
  348. dd->eep_st_new_errs[idx] = 0;
  349. }
  350. }
  351. /*
  352. * Now update active-time. We would like to round to the nearest hour
  353. * but unless atomic_t are sure to be proper signed ints we cannot,
  354. * because we need to account for what we "transfer" to EEPROM and
  355. * if we log an hour at 31 minutes, then we would need to set
  356. * active_time to -29 to accurately count the _next_ hour.
  357. */
  358. if (new_time >= 3600) {
  359. new_hrs = new_time / 3600;
  360. atomic_sub((new_hrs * 3600), &dd->active_time);
  361. new_hrs += dd->eep_hrs;
  362. if (new_hrs > 0xFFFF)
  363. new_hrs = 0xFFFF;
  364. dd->eep_hrs = new_hrs;
  365. if ((new_hrs & 0xFF) != ifp->if_powerhour[0]) {
  366. ifp->if_powerhour[0] = new_hrs & 0xFF;
  367. hi_water = offsetof(struct qib_flash, if_powerhour);
  368. }
  369. if ((new_hrs >> 8) != ifp->if_powerhour[1]) {
  370. ifp->if_powerhour[1] = new_hrs >> 8;
  371. hi_water = offsetof(struct qib_flash, if_powerhour) + 1;
  372. }
  373. }
  374. /*
  375. * There is a tiny possibility that we could somehow fail to write
  376. * the EEPROM after updating our shadows, but problems from holding
  377. * the spinlock too long are a much bigger issue.
  378. */
  379. spin_unlock_irqrestore(&dd->eep_st_lock, flags);
  380. if (hi_water) {
  381. /* we made some change to the data, uopdate cksum and write */
  382. csum = flash_csum(ifp, 1);
  383. ret = eeprom_write_with_enable(dd, 0, buf, hi_water + 1);
  384. }
  385. mutex_unlock(&dd->eep_lock);
  386. if (ret)
  387. qib_dev_err(dd, "Failed updating EEPROM\n");
  388. free_bail:
  389. vfree(buf);
  390. bail:
  391. return ret;
  392. }
  393. /**
  394. * qib_inc_eeprom_err - increment one of the four error counters
  395. * that are logged to EEPROM.
  396. * @dd: the qlogic_ib device
  397. * @eidx: 0..3, the counter to increment
  398. * @incr: how much to add
  399. *
  400. * Each counter is 8-bits, and saturates at 255 (0xFF). They
  401. * are copied to the EEPROM (aka flash) whenever qib_update_eeprom_log()
  402. * is called, but it can only be called in a context that allows sleep.
  403. * This function can be called even at interrupt level.
  404. */
  405. void qib_inc_eeprom_err(struct qib_devdata *dd, u32 eidx, u32 incr)
  406. {
  407. uint new_val;
  408. unsigned long flags;
  409. spin_lock_irqsave(&dd->eep_st_lock, flags);
  410. new_val = dd->eep_st_new_errs[eidx] + incr;
  411. if (new_val > 255)
  412. new_val = 255;
  413. dd->eep_st_new_errs[eidx] = new_val;
  414. spin_unlock_irqrestore(&dd->eep_st_lock, flags);
  415. }