Linux Audio

Check our new training course

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