ipath_eeprom.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * Copyright (c) 2006, 2007 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 "ipath_kernel.h"
  37. /*
  38. * InfiniPath I2C driver for a serial eeprom. This is not a generic
  39. * I2C interface. For a start, the device we're using (Atmel AT24C11)
  40. * doesn't work like a regular I2C device. It looks like one
  41. * electrically, but not logically. Normal I2C devices have a single
  42. * 7-bit or 10-bit I2C address that they respond to. Valid 7-bit
  43. * addresses range from 0x03 to 0x77. Addresses 0x00 to 0x02 and 0x78
  44. * to 0x7F are special reserved addresses (e.g. 0x00 is the "general
  45. * call" address.) The Atmel device, on the other hand, responds to ALL
  46. * 7-bit addresses. It's designed to be the only device on a given I2C
  47. * bus. A 7-bit address corresponds to the memory address within the
  48. * Atmel device itself.
  49. *
  50. * Also, the timing requirements mean more than simple software
  51. * bitbanging, with readbacks from chip to ensure timing (simple udelay
  52. * is not enough).
  53. *
  54. * This all means that accessing the device is specialized enough
  55. * that using the standard kernel I2C bitbanging interface would be
  56. * impossible. For example, the core I2C eeprom driver expects to find
  57. * a device at one or more of a limited set of addresses only. It doesn't
  58. * allow writing to an eeprom. It also doesn't provide any means of
  59. * accessing eeprom contents from within the kernel, only via sysfs.
  60. */
  61. enum i2c_type {
  62. i2c_line_scl = 0,
  63. i2c_line_sda
  64. };
  65. enum i2c_state {
  66. i2c_line_low = 0,
  67. i2c_line_high
  68. };
  69. #define READ_CMD 1
  70. #define WRITE_CMD 0
  71. static int eeprom_init;
  72. /*
  73. * The gpioval manipulation really should be protected by spinlocks
  74. * or be converted to use atomic operations.
  75. */
  76. /**
  77. * i2c_gpio_set - set a GPIO line
  78. * @dd: the infinipath device
  79. * @line: the line to set
  80. * @new_line_state: the state to set
  81. *
  82. * Returns 0 if the line was set to the new state successfully, non-zero
  83. * on error.
  84. */
  85. static int i2c_gpio_set(struct ipath_devdata *dd,
  86. enum i2c_type line,
  87. enum i2c_state new_line_state)
  88. {
  89. u64 out_mask, dir_mask, *gpioval;
  90. unsigned long flags = 0;
  91. gpioval = &dd->ipath_gpio_out;
  92. if (line == i2c_line_scl) {
  93. dir_mask = dd->ipath_gpio_scl;
  94. out_mask = (1UL << dd->ipath_gpio_scl_num);
  95. } else {
  96. dir_mask = dd->ipath_gpio_sda;
  97. out_mask = (1UL << dd->ipath_gpio_sda_num);
  98. }
  99. spin_lock_irqsave(&dd->ipath_gpio_lock, flags);
  100. if (new_line_state == i2c_line_high) {
  101. /* tri-state the output rather than force high */
  102. dd->ipath_extctrl &= ~dir_mask;
  103. } else {
  104. /* config line to be an output */
  105. dd->ipath_extctrl |= dir_mask;
  106. }
  107. ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl);
  108. /* set output as well (no real verify) */
  109. if (new_line_state == i2c_line_high)
  110. *gpioval |= out_mask;
  111. else
  112. *gpioval &= ~out_mask;
  113. ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_out, *gpioval);
  114. spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);
  115. return 0;
  116. }
  117. /**
  118. * i2c_gpio_get - get a GPIO line state
  119. * @dd: the infinipath device
  120. * @line: the line to get
  121. * @curr_statep: where to put the line state
  122. *
  123. * Returns 0 if the line was set to the new state successfully, non-zero
  124. * on error. curr_state is not set on error.
  125. */
  126. static int i2c_gpio_get(struct ipath_devdata *dd,
  127. enum i2c_type line,
  128. enum i2c_state *curr_statep)
  129. {
  130. u64 read_val, mask;
  131. int ret;
  132. unsigned long flags = 0;
  133. /* check args */
  134. if (curr_statep == NULL) {
  135. ret = 1;
  136. goto bail;
  137. }
  138. /* config line to be an input */
  139. if (line == i2c_line_scl)
  140. mask = dd->ipath_gpio_scl;
  141. else
  142. mask = dd->ipath_gpio_sda;
  143. spin_lock_irqsave(&dd->ipath_gpio_lock, flags);
  144. dd->ipath_extctrl &= ~mask;
  145. ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl);
  146. /*
  147. * Below is very unlikely to reflect true input state if Output
  148. * Enable actually changed.
  149. */
  150. read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extstatus);
  151. spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);
  152. if (read_val & mask)
  153. *curr_statep = i2c_line_high;
  154. else
  155. *curr_statep = i2c_line_low;
  156. ret = 0;
  157. bail:
  158. return ret;
  159. }
  160. /**
  161. * i2c_wait_for_writes - wait for a write
  162. * @dd: the infinipath device
  163. *
  164. * We use this instead of udelay directly, so we can make sure
  165. * that previous register writes have been flushed all the way
  166. * to the chip. Since we are delaying anyway, the cost doesn't
  167. * hurt, and makes the bit twiddling more regular
  168. */
  169. static void i2c_wait_for_writes(struct ipath_devdata *dd)
  170. {
  171. (void)ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch);
  172. rmb();
  173. }
  174. static void scl_out(struct ipath_devdata *dd, u8 bit)
  175. {
  176. udelay(1);
  177. i2c_gpio_set(dd, i2c_line_scl, bit ? i2c_line_high : i2c_line_low);
  178. i2c_wait_for_writes(dd);
  179. }
  180. static void sda_out(struct ipath_devdata *dd, u8 bit)
  181. {
  182. i2c_gpio_set(dd, i2c_line_sda, bit ? i2c_line_high : i2c_line_low);
  183. i2c_wait_for_writes(dd);
  184. }
  185. static u8 sda_in(struct ipath_devdata *dd, int wait)
  186. {
  187. enum i2c_state bit;
  188. if (i2c_gpio_get(dd, i2c_line_sda, &bit))
  189. ipath_dbg("get bit failed!\n");
  190. if (wait)
  191. i2c_wait_for_writes(dd);
  192. return bit == i2c_line_high ? 1U : 0;
  193. }
  194. /**
  195. * i2c_ackrcv - see if ack following write is true
  196. * @dd: the infinipath device
  197. */
  198. static int i2c_ackrcv(struct ipath_devdata *dd)
  199. {
  200. u8 ack_received;
  201. /* AT ENTRY SCL = LOW */
  202. /* change direction, ignore data */
  203. ack_received = sda_in(dd, 1);
  204. scl_out(dd, i2c_line_high);
  205. ack_received = sda_in(dd, 1) == 0;
  206. scl_out(dd, i2c_line_low);
  207. return ack_received;
  208. }
  209. /**
  210. * wr_byte - write a byte, one bit at a time
  211. * @dd: the infinipath device
  212. * @data: the byte to write
  213. *
  214. * Returns 0 if we got the following ack, otherwise 1
  215. */
  216. static int wr_byte(struct ipath_devdata *dd, u8 data)
  217. {
  218. int bit_cntr;
  219. u8 bit;
  220. for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
  221. bit = (data >> bit_cntr) & 1;
  222. sda_out(dd, bit);
  223. scl_out(dd, i2c_line_high);
  224. scl_out(dd, i2c_line_low);
  225. }
  226. return (!i2c_ackrcv(dd)) ? 1 : 0;
  227. }
  228. static void send_ack(struct ipath_devdata *dd)
  229. {
  230. sda_out(dd, i2c_line_low);
  231. scl_out(dd, i2c_line_high);
  232. scl_out(dd, i2c_line_low);
  233. sda_out(dd, i2c_line_high);
  234. }
  235. /**
  236. * i2c_startcmd - transmit the start condition, followed by address/cmd
  237. * @dd: the infinipath device
  238. * @offset_dir: direction byte
  239. *
  240. * (both clock/data high, clock high, data low while clock is high)
  241. */
  242. static int i2c_startcmd(struct ipath_devdata *dd, u8 offset_dir)
  243. {
  244. int res;
  245. /* issue start sequence */
  246. sda_out(dd, i2c_line_high);
  247. scl_out(dd, i2c_line_high);
  248. sda_out(dd, i2c_line_low);
  249. scl_out(dd, i2c_line_low);
  250. /* issue length and direction byte */
  251. res = wr_byte(dd, offset_dir);
  252. if (res)
  253. ipath_cdbg(VERBOSE, "No ack to complete start\n");
  254. return res;
  255. }
  256. /**
  257. * stop_cmd - transmit the stop condition
  258. * @dd: the infinipath device
  259. *
  260. * (both clock/data low, clock high, data high while clock is high)
  261. */
  262. static void stop_cmd(struct ipath_devdata *dd)
  263. {
  264. scl_out(dd, i2c_line_low);
  265. sda_out(dd, i2c_line_low);
  266. scl_out(dd, i2c_line_high);
  267. sda_out(dd, i2c_line_high);
  268. udelay(2);
  269. }
  270. /**
  271. * eeprom_reset - reset I2C communication
  272. * @dd: the infinipath device
  273. */
  274. static int eeprom_reset(struct ipath_devdata *dd)
  275. {
  276. int clock_cycles_left = 9;
  277. u64 *gpioval = &dd->ipath_gpio_out;
  278. int ret;
  279. unsigned long flags;
  280. spin_lock_irqsave(&dd->ipath_gpio_lock, flags);
  281. /* Make sure shadows are consistent */
  282. dd->ipath_extctrl = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);
  283. *gpioval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_out);
  284. spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);
  285. ipath_cdbg(VERBOSE, "Resetting i2c eeprom; initial gpioout reg "
  286. "is %llx\n", (unsigned long long) *gpioval);
  287. eeprom_init = 1;
  288. /*
  289. * This is to get the i2c into a known state, by first going low,
  290. * then tristate sda (and then tristate scl as first thing
  291. * in loop)
  292. */
  293. scl_out(dd, i2c_line_low);
  294. sda_out(dd, i2c_line_high);
  295. while (clock_cycles_left--) {
  296. scl_out(dd, i2c_line_high);
  297. if (sda_in(dd, 0)) {
  298. sda_out(dd, i2c_line_low);
  299. scl_out(dd, i2c_line_low);
  300. ret = 0;
  301. goto bail;
  302. }
  303. scl_out(dd, i2c_line_low);
  304. }
  305. ret = 1;
  306. bail:
  307. return ret;
  308. }
  309. /**
  310. * ipath_eeprom_read - receives bytes from the eeprom via I2C
  311. * @dd: the infinipath device
  312. * @eeprom_offset: address to read from
  313. * @buffer: where to store result
  314. * @len: number of bytes to receive
  315. */
  316. static int ipath_eeprom_internal_read(struct ipath_devdata *dd,
  317. u8 eeprom_offset, void *buffer, int len)
  318. {
  319. /* compiler complains unless initialized */
  320. u8 single_byte = 0;
  321. int bit_cntr;
  322. int ret;
  323. if (!eeprom_init)
  324. eeprom_reset(dd);
  325. eeprom_offset = (eeprom_offset << 1) | READ_CMD;
  326. if (i2c_startcmd(dd, eeprom_offset)) {
  327. ipath_dbg("Failed startcmd\n");
  328. stop_cmd(dd);
  329. ret = 1;
  330. goto bail;
  331. }
  332. /*
  333. * eeprom keeps clocking data out as long as we ack, automatically
  334. * incrementing the address.
  335. */
  336. while (len-- > 0) {
  337. /* get data */
  338. single_byte = 0;
  339. for (bit_cntr = 8; bit_cntr; bit_cntr--) {
  340. u8 bit;
  341. scl_out(dd, i2c_line_high);
  342. bit = sda_in(dd, 0);
  343. single_byte |= bit << (bit_cntr - 1);
  344. scl_out(dd, i2c_line_low);
  345. }
  346. /* send ack if not the last byte */
  347. if (len)
  348. send_ack(dd);
  349. *((u8 *) buffer) = single_byte;
  350. buffer++;
  351. }
  352. stop_cmd(dd);
  353. ret = 0;
  354. bail:
  355. return ret;
  356. }
  357. /**
  358. * ipath_eeprom_write - writes data to the eeprom via I2C
  359. * @dd: the infinipath device
  360. * @eeprom_offset: where to place data
  361. * @buffer: data to write
  362. * @len: number of bytes to write
  363. */
  364. static int ipath_eeprom_internal_write(struct ipath_devdata *dd, u8 eeprom_offset,
  365. const void *buffer, int len)
  366. {
  367. u8 single_byte;
  368. int sub_len;
  369. const u8 *bp = buffer;
  370. int max_wait_time, i;
  371. int ret;
  372. if (!eeprom_init)
  373. eeprom_reset(dd);
  374. while (len > 0) {
  375. if (i2c_startcmd(dd, (eeprom_offset << 1) | WRITE_CMD)) {
  376. ipath_dbg("Failed to start cmd offset %u\n",
  377. eeprom_offset);
  378. goto failed_write;
  379. }
  380. sub_len = min(len, 4);
  381. eeprom_offset += sub_len;
  382. len -= sub_len;
  383. for (i = 0; i < sub_len; i++) {
  384. if (wr_byte(dd, *bp++)) {
  385. ipath_dbg("no ack after byte %u/%u (%u "
  386. "total remain)\n", i, sub_len,
  387. len + sub_len - i);
  388. goto failed_write;
  389. }
  390. }
  391. stop_cmd(dd);
  392. /*
  393. * wait for write complete by waiting for a successful
  394. * read (the chip replies with a zero after the write
  395. * cmd completes, and before it writes to the eeprom.
  396. * The startcmd for the read will fail the ack until
  397. * the writes have completed. We do this inline to avoid
  398. * the debug prints that are in the real read routine
  399. * if the startcmd fails.
  400. */
  401. max_wait_time = 100;
  402. while (i2c_startcmd(dd, READ_CMD)) {
  403. stop_cmd(dd);
  404. if (!--max_wait_time) {
  405. ipath_dbg("Did not get successful read to "
  406. "complete write\n");
  407. goto failed_write;
  408. }
  409. }
  410. /* now read the zero byte */
  411. for (i = single_byte = 0; i < 8; i++) {
  412. u8 bit;
  413. scl_out(dd, i2c_line_high);
  414. bit = sda_in(dd, 0);
  415. scl_out(dd, i2c_line_low);
  416. single_byte <<= 1;
  417. single_byte |= bit;
  418. }
  419. stop_cmd(dd);
  420. }
  421. ret = 0;
  422. goto bail;
  423. failed_write:
  424. stop_cmd(dd);
  425. ret = 1;
  426. bail:
  427. return ret;
  428. }
  429. /*
  430. * The public entry-points ipath_eeprom_read() and ipath_eeprom_write()
  431. * are now just wrappers around the internal functions.
  432. */
  433. int ipath_eeprom_read(struct ipath_devdata *dd, u8 eeprom_offset,
  434. void *buff, int len)
  435. {
  436. int ret;
  437. ret = down_interruptible(&dd->ipath_eep_sem);
  438. if (!ret) {
  439. ret = ipath_eeprom_internal_read(dd, eeprom_offset, buff, len);
  440. up(&dd->ipath_eep_sem);
  441. }
  442. return ret;
  443. }
  444. int ipath_eeprom_write(struct ipath_devdata *dd, u8 eeprom_offset,
  445. const void *buff, int len)
  446. {
  447. int ret;
  448. ret = down_interruptible(&dd->ipath_eep_sem);
  449. if (!ret) {
  450. ret = ipath_eeprom_internal_write(dd, eeprom_offset, buff, len);
  451. up(&dd->ipath_eep_sem);
  452. }
  453. return ret;
  454. }
  455. static u8 flash_csum(struct ipath_flash *ifp, int adjust)
  456. {
  457. u8 *ip = (u8 *) ifp;
  458. u8 csum = 0, len;
  459. for (len = 0; len < ifp->if_length; len++)
  460. csum += *ip++;
  461. csum -= ifp->if_csum;
  462. csum = ~csum;
  463. if (adjust)
  464. ifp->if_csum = csum;
  465. return csum;
  466. }
  467. /**
  468. * ipath_get_guid - get the GUID from the i2c device
  469. * @dd: the infinipath device
  470. *
  471. * We have the capability to use the ipath_nguid field, and get
  472. * the guid from the first chip's flash, to use for all of them.
  473. */
  474. void ipath_get_eeprom_info(struct ipath_devdata *dd)
  475. {
  476. void *buf;
  477. struct ipath_flash *ifp;
  478. __be64 guid;
  479. int len, eep_stat;
  480. u8 csum, *bguid;
  481. int t = dd->ipath_unit;
  482. struct ipath_devdata *dd0 = ipath_lookup(0);
  483. if (t && dd0->ipath_nguid > 1 && t <= dd0->ipath_nguid) {
  484. u8 *bguid, oguid;
  485. dd->ipath_guid = dd0->ipath_guid;
  486. bguid = (u8 *) & dd->ipath_guid;
  487. oguid = bguid[7];
  488. bguid[7] += t;
  489. if (oguid > bguid[7]) {
  490. if (bguid[6] == 0xff) {
  491. if (bguid[5] == 0xff) {
  492. ipath_dev_err(
  493. dd,
  494. "Can't set %s GUID from "
  495. "base, wraps to OUI!\n",
  496. ipath_get_unit_name(t));
  497. dd->ipath_guid = 0;
  498. goto bail;
  499. }
  500. bguid[5]++;
  501. }
  502. bguid[6]++;
  503. }
  504. dd->ipath_nguid = 1;
  505. ipath_dbg("nguid %u, so adding %u to device 0 guid, "
  506. "for %llx\n",
  507. dd0->ipath_nguid, t,
  508. (unsigned long long) be64_to_cpu(dd->ipath_guid));
  509. goto bail;
  510. }
  511. /*
  512. * read full flash, not just currently used part, since it may have
  513. * been written with a newer definition
  514. * */
  515. len = sizeof(struct ipath_flash);
  516. buf = vmalloc(len);
  517. if (!buf) {
  518. ipath_dev_err(dd, "Couldn't allocate memory to read %u "
  519. "bytes from eeprom for GUID\n", len);
  520. goto bail;
  521. }
  522. down(&dd->ipath_eep_sem);
  523. eep_stat = ipath_eeprom_internal_read(dd, 0, buf, len);
  524. up(&dd->ipath_eep_sem);
  525. if (eep_stat) {
  526. ipath_dev_err(dd, "Failed reading GUID from eeprom\n");
  527. goto done;
  528. }
  529. ifp = (struct ipath_flash *)buf;
  530. csum = flash_csum(ifp, 0);
  531. if (csum != ifp->if_csum) {
  532. dev_info(&dd->pcidev->dev, "Bad I2C flash checksum: "
  533. "0x%x, not 0x%x\n", csum, ifp->if_csum);
  534. goto done;
  535. }
  536. if (*(__be64 *) ifp->if_guid == 0ULL ||
  537. *(__be64 *) ifp->if_guid == __constant_cpu_to_be64(-1LL)) {
  538. ipath_dev_err(dd, "Invalid GUID %llx from flash; "
  539. "ignoring\n",
  540. *(unsigned long long *) ifp->if_guid);
  541. /* don't allow GUID if all 0 or all 1's */
  542. goto done;
  543. }
  544. /* complain, but allow it */
  545. if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
  546. dev_info(&dd->pcidev->dev, "Warning, GUID %llx is "
  547. "default, probably not correct!\n",
  548. *(unsigned long long *) ifp->if_guid);
  549. bguid = ifp->if_guid;
  550. if (!bguid[0] && !bguid[1] && !bguid[2]) {
  551. /* original incorrect GUID format in flash; fix in
  552. * core copy, by shifting up 2 octets; don't need to
  553. * change top octet, since both it and shifted are
  554. * 0.. */
  555. bguid[1] = bguid[3];
  556. bguid[2] = bguid[4];
  557. bguid[3] = bguid[4] = 0;
  558. guid = *(__be64 *) ifp->if_guid;
  559. ipath_cdbg(VERBOSE, "Old GUID format in flash, top 3 zero, "
  560. "shifting 2 octets\n");
  561. } else
  562. guid = *(__be64 *) ifp->if_guid;
  563. dd->ipath_guid = guid;
  564. dd->ipath_nguid = ifp->if_numguid;
  565. /*
  566. * Things are slightly complicated by the desire to transparently
  567. * support both the Pathscale 10-digit serial number and the QLogic
  568. * 13-character version.
  569. */
  570. if ((ifp->if_fversion > 1) && ifp->if_sprefix[0]
  571. && ((u8 *)ifp->if_sprefix)[0] != 0xFF) {
  572. /* This board has a Serial-prefix, which is stored
  573. * elsewhere for backward-compatibility.
  574. */
  575. char *snp = dd->ipath_serial;
  576. int len;
  577. memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix);
  578. snp[sizeof ifp->if_sprefix] = '\0';
  579. len = strlen(snp);
  580. snp += len;
  581. len = (sizeof dd->ipath_serial) - len;
  582. if (len > sizeof ifp->if_serial) {
  583. len = sizeof ifp->if_serial;
  584. }
  585. memcpy(snp, ifp->if_serial, len);
  586. } else
  587. memcpy(dd->ipath_serial, ifp->if_serial,
  588. sizeof ifp->if_serial);
  589. if (!strstr(ifp->if_comment, "Tested successfully"))
  590. ipath_dev_err(dd, "Board SN %s did not pass functional "
  591. "test: %s\n", dd->ipath_serial,
  592. ifp->if_comment);
  593. ipath_cdbg(VERBOSE, "Initted GUID to %llx from eeprom\n",
  594. (unsigned long long) be64_to_cpu(dd->ipath_guid));
  595. memcpy(&dd->ipath_eep_st_errs, &ifp->if_errcntp, IPATH_EEP_LOG_CNT);
  596. /*
  597. * Power-on (actually "active") hours are kept as little-endian value
  598. * in EEPROM, but as seconds in a (possibly as small as 24-bit)
  599. * atomic_t while running.
  600. */
  601. atomic_set(&dd->ipath_active_time, 0);
  602. dd->ipath_eep_hrs = ifp->if_powerhour[0] | (ifp->if_powerhour[1] << 8);
  603. done:
  604. vfree(buf);
  605. bail:;
  606. }
  607. /**
  608. * ipath_update_eeprom_log - copy active-time and error counters to eeprom
  609. * @dd: the infinipath device
  610. *
  611. * Although the time is kept as seconds in the ipath_devdata struct, it is
  612. * rounded to hours for re-write, as we have only 16 bits in EEPROM.
  613. * First-cut code reads whole (expected) struct ipath_flash, modifies,
  614. * re-writes. Future direction: read/write only what we need, assuming
  615. * that the EEPROM had to have been "good enough" for driver init, and
  616. * if not, we aren't making it worse.
  617. *
  618. */
  619. int ipath_update_eeprom_log(struct ipath_devdata *dd)
  620. {
  621. void *buf;
  622. struct ipath_flash *ifp;
  623. int len, hi_water;
  624. uint32_t new_time, new_hrs;
  625. u8 csum;
  626. int ret, idx;
  627. unsigned long flags;
  628. /* first, check if we actually need to do anything. */
  629. ret = 0;
  630. for (idx = 0; idx < IPATH_EEP_LOG_CNT; ++idx) {
  631. if (dd->ipath_eep_st_new_errs[idx]) {
  632. ret = 1;
  633. break;
  634. }
  635. }
  636. new_time = atomic_read(&dd->ipath_active_time);
  637. if (ret == 0 && new_time < 3600)
  638. return 0;
  639. /*
  640. * The quick-check above determined that there is something worthy
  641. * of logging, so get current contents and do a more detailed idea.
  642. * read full flash, not just currently used part, since it may have
  643. * been written with a newer definition
  644. */
  645. len = sizeof(struct ipath_flash);
  646. buf = vmalloc(len);
  647. ret = 1;
  648. if (!buf) {
  649. ipath_dev_err(dd, "Couldn't allocate memory to read %u "
  650. "bytes from eeprom for logging\n", len);
  651. goto bail;
  652. }
  653. /* Grab semaphore and read current EEPROM. If we get an
  654. * error, let go, but if not, keep it until we finish write.
  655. */
  656. ret = down_interruptible(&dd->ipath_eep_sem);
  657. if (ret) {
  658. ipath_dev_err(dd, "Unable to acquire EEPROM for logging\n");
  659. goto free_bail;
  660. }
  661. ret = ipath_eeprom_internal_read(dd, 0, buf, len);
  662. if (ret) {
  663. up(&dd->ipath_eep_sem);
  664. ipath_dev_err(dd, "Unable read EEPROM for logging\n");
  665. goto free_bail;
  666. }
  667. ifp = (struct ipath_flash *)buf;
  668. csum = flash_csum(ifp, 0);
  669. if (csum != ifp->if_csum) {
  670. up(&dd->ipath_eep_sem);
  671. ipath_dev_err(dd, "EEPROM cks err (0x%02X, S/B 0x%02X)\n",
  672. csum, ifp->if_csum);
  673. ret = 1;
  674. goto free_bail;
  675. }
  676. hi_water = 0;
  677. spin_lock_irqsave(&dd->ipath_eep_st_lock, flags);
  678. for (idx = 0; idx < IPATH_EEP_LOG_CNT; ++idx) {
  679. int new_val = dd->ipath_eep_st_new_errs[idx];
  680. if (new_val) {
  681. /*
  682. * If we have seen any errors, add to EEPROM values
  683. * We need to saturate at 0xFF (255) and we also
  684. * would need to adjust the checksum if we were
  685. * trying to minimize EEPROM traffic
  686. * Note that we add to actual current count in EEPROM,
  687. * in case it was altered while we were running.
  688. */
  689. new_val += ifp->if_errcntp[idx];
  690. if (new_val > 0xFF)
  691. new_val = 0xFF;
  692. if (ifp->if_errcntp[idx] != new_val) {
  693. ifp->if_errcntp[idx] = new_val;
  694. hi_water = offsetof(struct ipath_flash,
  695. if_errcntp) + idx;
  696. }
  697. /*
  698. * update our shadow (used to minimize EEPROM
  699. * traffic), to match what we are about to write.
  700. */
  701. dd->ipath_eep_st_errs[idx] = new_val;
  702. dd->ipath_eep_st_new_errs[idx] = 0;
  703. }
  704. }
  705. /*
  706. * now update active-time. We would like to round to the nearest hour
  707. * but unless atomic_t are sure to be proper signed ints we cannot,
  708. * because we need to account for what we "transfer" to EEPROM and
  709. * if we log an hour at 31 minutes, then we would need to set
  710. * active_time to -29 to accurately count the _next_ hour.
  711. */
  712. if (new_time > 3600) {
  713. new_hrs = new_time / 3600;
  714. atomic_sub((new_hrs * 3600), &dd->ipath_active_time);
  715. new_hrs += dd->ipath_eep_hrs;
  716. if (new_hrs > 0xFFFF)
  717. new_hrs = 0xFFFF;
  718. dd->ipath_eep_hrs = new_hrs;
  719. if ((new_hrs & 0xFF) != ifp->if_powerhour[0]) {
  720. ifp->if_powerhour[0] = new_hrs & 0xFF;
  721. hi_water = offsetof(struct ipath_flash, if_powerhour);
  722. }
  723. if ((new_hrs >> 8) != ifp->if_powerhour[1]) {
  724. ifp->if_powerhour[1] = new_hrs >> 8;
  725. hi_water = offsetof(struct ipath_flash, if_powerhour)
  726. + 1;
  727. }
  728. }
  729. /*
  730. * There is a tiny possibility that we could somehow fail to write
  731. * the EEPROM after updating our shadows, but problems from holding
  732. * the spinlock too long are a much bigger issue.
  733. */
  734. spin_unlock_irqrestore(&dd->ipath_eep_st_lock, flags);
  735. if (hi_water) {
  736. /* we made some change to the data, uopdate cksum and write */
  737. csum = flash_csum(ifp, 1);
  738. ret = ipath_eeprom_internal_write(dd, 0, buf, hi_water + 1);
  739. }
  740. up(&dd->ipath_eep_sem);
  741. if (ret)
  742. ipath_dev_err(dd, "Failed updating EEPROM\n");
  743. free_bail:
  744. vfree(buf);
  745. bail:
  746. return ret;
  747. }
  748. /**
  749. * ipath_inc_eeprom_err - increment one of the four error counters
  750. * that are logged to EEPROM.
  751. * @dd: the infinipath device
  752. * @eidx: 0..3, the counter to increment
  753. * @incr: how much to add
  754. *
  755. * Each counter is 8-bits, and saturates at 255 (0xFF). They
  756. * are copied to the EEPROM (aka flash) whenever ipath_update_eeprom_log()
  757. * is called, but it can only be called in a context that allows sleep.
  758. * This function can be called even at interrupt level.
  759. */
  760. void ipath_inc_eeprom_err(struct ipath_devdata *dd, u32 eidx, u32 incr)
  761. {
  762. uint new_val;
  763. unsigned long flags;
  764. spin_lock_irqsave(&dd->ipath_eep_st_lock, flags);
  765. new_val = dd->ipath_eep_st_new_errs[eidx] + incr;
  766. if (new_val > 255)
  767. new_val = 255;
  768. dd->ipath_eep_st_new_errs[eidx] = new_val;
  769. spin_unlock_irqrestore(&dd->ipath_eep_st_lock, flags);
  770. return;
  771. }