xref: /openbmc/linux/drivers/scsi/pm8001/pm8001_ctl.c (revision 29c37341)
1 /*
2  * PMC-Sierra 8001/8081/8088/8089 SAS/SATA based host adapters driver
3  *
4  * Copyright (c) 2008-2009 USI Co., Ltd.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    substantially similar to the "NO WARRANTY" disclaimer below
15  *    ("Disclaimer") and any redistribution must be conditioned upon
16  *    including a substantially similar Disclaimer requirement for further
17  *    binary redistribution.
18  * 3. Neither the names of the above-listed copyright holders nor the names
19  *    of any contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * Alternatively, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") version 2 as published by the Free
24  * Software Foundation.
25  *
26  * NO WARRANTY
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGES.
38  *
39  */
40 #include <linux/firmware.h>
41 #include <linux/slab.h>
42 #include "pm8001_sas.h"
43 #include "pm8001_ctl.h"
44 
45 /* scsi host attributes */
46 
47 /**
48  * pm8001_ctl_mpi_interface_rev_show - MPI interface revision number
49  * @cdev: pointer to embedded class device
50  * @attr: device attribute (unused)
51  * @buf: the buffer returned
52  *
53  * A sysfs 'read-only' shost attribute.
54  */
55 static ssize_t pm8001_ctl_mpi_interface_rev_show(struct device *cdev,
56 	struct device_attribute *attr, char *buf)
57 {
58 	struct Scsi_Host *shost = class_to_shost(cdev);
59 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
60 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
61 
62 	if (pm8001_ha->chip_id == chip_8001) {
63 		return snprintf(buf, PAGE_SIZE, "%d\n",
64 			pm8001_ha->main_cfg_tbl.pm8001_tbl.interface_rev);
65 	} else {
66 		return snprintf(buf, PAGE_SIZE, "%d\n",
67 			pm8001_ha->main_cfg_tbl.pm80xx_tbl.interface_rev);
68 	}
69 }
70 static
71 DEVICE_ATTR(interface_rev, S_IRUGO, pm8001_ctl_mpi_interface_rev_show, NULL);
72 
73 /**
74  * controller_fatal_error_show - check controller is under fatal err
75  * @cdev: pointer to embedded class device
76  * @attr: device attribute (unused)
77  * @buf: the buffer returned
78  *
79  * A sysfs 'read only' shost attribute.
80  */
81 static ssize_t controller_fatal_error_show(struct device *cdev,
82 		struct device_attribute *attr, char *buf)
83 {
84 	struct Scsi_Host *shost = class_to_shost(cdev);
85 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
86 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
87 
88 	return snprintf(buf, PAGE_SIZE, "%d\n",
89 			pm8001_ha->controller_fatal_error);
90 }
91 static DEVICE_ATTR_RO(controller_fatal_error);
92 
93 /**
94  * pm8001_ctl_fw_version_show - firmware version
95  * @cdev: pointer to embedded class device
96  * @attr: device attribute (unused)
97  * @buf: the buffer returned
98  *
99  * A sysfs 'read-only' shost attribute.
100  */
101 static ssize_t pm8001_ctl_fw_version_show(struct device *cdev,
102 	struct device_attribute *attr, char *buf)
103 {
104 	struct Scsi_Host *shost = class_to_shost(cdev);
105 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
106 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
107 
108 	if (pm8001_ha->chip_id == chip_8001) {
109 		return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
110 		(u8)(pm8001_ha->main_cfg_tbl.pm8001_tbl.firmware_rev >> 24),
111 		(u8)(pm8001_ha->main_cfg_tbl.pm8001_tbl.firmware_rev >> 16),
112 		(u8)(pm8001_ha->main_cfg_tbl.pm8001_tbl.firmware_rev >> 8),
113 		(u8)(pm8001_ha->main_cfg_tbl.pm8001_tbl.firmware_rev));
114 	} else {
115 		return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
116 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev >> 24),
117 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev >> 16),
118 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev >> 8),
119 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev));
120 	}
121 }
122 static DEVICE_ATTR(fw_version, S_IRUGO, pm8001_ctl_fw_version_show, NULL);
123 
124 /**
125  * pm8001_ctl_ila_version_show - ila version
126  * @cdev: pointer to embedded class device
127  * @attr: device attribute (unused)
128  * @buf: the buffer returned
129  *
130  * A sysfs 'read-only' shost attribute.
131  */
132 static ssize_t pm8001_ctl_ila_version_show(struct device *cdev,
133 	struct device_attribute *attr, char *buf)
134 {
135 	struct Scsi_Host *shost = class_to_shost(cdev);
136 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
137 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
138 
139 	if (pm8001_ha->chip_id != chip_8001) {
140 		return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
141 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.ila_version >> 24),
142 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.ila_version >> 16),
143 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.ila_version >> 8),
144 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.ila_version));
145 	}
146 	return 0;
147 }
148 static DEVICE_ATTR(ila_version, 0444, pm8001_ctl_ila_version_show, NULL);
149 
150 /**
151  * pm8001_ctl_inactive_fw_version_show - Inacative firmware version number
152  * @cdev: pointer to embedded class device
153  * @attr: device attribute (unused)
154  * @buf: the buffer returned
155  *
156  * A sysfs 'read-only' shost attribute.
157  */
158 static ssize_t pm8001_ctl_inactive_fw_version_show(struct device *cdev,
159 	struct device_attribute *attr, char *buf)
160 {
161 	struct Scsi_Host *shost = class_to_shost(cdev);
162 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
163 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
164 
165 	if (pm8001_ha->chip_id != chip_8001) {
166 		return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
167 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.inc_fw_version >> 24),
168 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.inc_fw_version >> 16),
169 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.inc_fw_version >> 8),
170 		(u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.inc_fw_version));
171 	}
172 	return 0;
173 }
174 static
175 DEVICE_ATTR(inc_fw_ver, 0444, pm8001_ctl_inactive_fw_version_show, NULL);
176 
177 /**
178  * pm8001_ctl_max_out_io_show - max outstanding io supported
179  * @cdev: pointer to embedded class device
180  * @attr: device attribute (unused)
181  * @buf: the buffer returned
182  *
183  * A sysfs 'read-only' shost attribute.
184  */
185 static ssize_t pm8001_ctl_max_out_io_show(struct device *cdev,
186 	struct device_attribute *attr, char *buf)
187 {
188 	struct Scsi_Host *shost = class_to_shost(cdev);
189 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
190 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
191 
192 	if (pm8001_ha->chip_id == chip_8001) {
193 		return snprintf(buf, PAGE_SIZE, "%d\n",
194 			pm8001_ha->main_cfg_tbl.pm8001_tbl.max_out_io);
195 	} else {
196 		return snprintf(buf, PAGE_SIZE, "%d\n",
197 			pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_out_io);
198 	}
199 }
200 static DEVICE_ATTR(max_out_io, S_IRUGO, pm8001_ctl_max_out_io_show, NULL);
201 /**
202  * pm8001_ctl_max_devices_show - max devices support
203  * @cdev: pointer to embedded class device
204  * @attr: device attribute (unused)
205  * @buf: the buffer returned
206  *
207  * A sysfs 'read-only' shost attribute.
208  */
209 static ssize_t pm8001_ctl_max_devices_show(struct device *cdev,
210 	struct device_attribute *attr, char *buf)
211 {
212 	struct Scsi_Host *shost = class_to_shost(cdev);
213 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
214 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
215 
216 	if (pm8001_ha->chip_id == chip_8001) {
217 		return snprintf(buf, PAGE_SIZE, "%04d\n",
218 			(u16)(pm8001_ha->main_cfg_tbl.pm8001_tbl.max_sgl >> 16)
219 			);
220 	} else {
221 		return snprintf(buf, PAGE_SIZE, "%04d\n",
222 			(u16)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_sgl >> 16)
223 			);
224 	}
225 }
226 static DEVICE_ATTR(max_devices, S_IRUGO, pm8001_ctl_max_devices_show, NULL);
227 /**
228  * pm8001_ctl_max_sg_list_show - max sg list supported iff not 0.0 for no
229  * hardware limitation
230  * @cdev: pointer to embedded class device
231  * @attr: device attribute (unused)
232  * @buf: the buffer returned
233  *
234  * A sysfs 'read-only' shost attribute.
235  */
236 static ssize_t pm8001_ctl_max_sg_list_show(struct device *cdev,
237 	struct device_attribute *attr, char *buf)
238 {
239 	struct Scsi_Host *shost = class_to_shost(cdev);
240 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
241 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
242 
243 	if (pm8001_ha->chip_id == chip_8001) {
244 		return snprintf(buf, PAGE_SIZE, "%04d\n",
245 			pm8001_ha->main_cfg_tbl.pm8001_tbl.max_sgl & 0x0000FFFF
246 			);
247 	} else {
248 		return snprintf(buf, PAGE_SIZE, "%04d\n",
249 			pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_sgl & 0x0000FFFF
250 			);
251 	}
252 }
253 static DEVICE_ATTR(max_sg_list, S_IRUGO, pm8001_ctl_max_sg_list_show, NULL);
254 
255 #define SAS_1_0 0x1
256 #define SAS_1_1 0x2
257 #define SAS_2_0 0x4
258 
259 static ssize_t
260 show_sas_spec_support_status(unsigned int mode, char *buf)
261 {
262 	ssize_t len = 0;
263 
264 	if (mode & SAS_1_1)
265 		len = sprintf(buf, "%s", "SAS1.1");
266 	if (mode & SAS_2_0)
267 		len += sprintf(buf + len, "%s%s", len ? ", " : "", "SAS2.0");
268 	len += sprintf(buf + len, "\n");
269 
270 	return len;
271 }
272 
273 /**
274  * pm8001_ctl_sas_spec_support_show - sas spec supported
275  * @cdev: pointer to embedded class device
276  * @attr: device attribute (unused)
277  * @buf: the buffer returned
278  *
279  * A sysfs 'read-only' shost attribute.
280  */
281 static ssize_t pm8001_ctl_sas_spec_support_show(struct device *cdev,
282 	struct device_attribute *attr, char *buf)
283 {
284 	unsigned int mode;
285 	struct Scsi_Host *shost = class_to_shost(cdev);
286 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
287 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
288 	/* fe000000 means supports SAS2.1 */
289 	if (pm8001_ha->chip_id == chip_8001)
290 		mode = (pm8001_ha->main_cfg_tbl.pm8001_tbl.ctrl_cap_flag &
291 							0xfe000000)>>25;
292 	else
293 		/* fe000000 means supports SAS2.1 */
294 		mode = (pm8001_ha->main_cfg_tbl.pm80xx_tbl.ctrl_cap_flag &
295 							0xfe000000)>>25;
296 	return show_sas_spec_support_status(mode, buf);
297 }
298 static DEVICE_ATTR(sas_spec_support, S_IRUGO,
299 		   pm8001_ctl_sas_spec_support_show, NULL);
300 
301 /**
302  * pm8001_ctl_sas_address_show - sas address
303  * @cdev: pointer to embedded class device
304  * @attr: device attribute (unused)
305  * @buf: the buffer returned
306  *
307  * This is the controller sas address
308  *
309  * A sysfs 'read-only' shost attribute.
310  */
311 static ssize_t pm8001_ctl_host_sas_address_show(struct device *cdev,
312 	struct device_attribute *attr, char *buf)
313 {
314 	struct Scsi_Host *shost = class_to_shost(cdev);
315 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
316 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
317 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
318 			be64_to_cpu(*(__be64 *)pm8001_ha->sas_addr));
319 }
320 static DEVICE_ATTR(host_sas_address, S_IRUGO,
321 		   pm8001_ctl_host_sas_address_show, NULL);
322 
323 /**
324  * pm8001_ctl_logging_level_show - logging level
325  * @cdev: pointer to embedded class device
326  * @attr: device attribute (unused)
327  * @buf: the buffer returned
328  *
329  * A sysfs 'read/write' shost attribute.
330  */
331 static ssize_t pm8001_ctl_logging_level_show(struct device *cdev,
332 	struct device_attribute *attr, char *buf)
333 {
334 	struct Scsi_Host *shost = class_to_shost(cdev);
335 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
336 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
337 
338 	return snprintf(buf, PAGE_SIZE, "%08xh\n", pm8001_ha->logging_level);
339 }
340 
341 static ssize_t pm8001_ctl_logging_level_store(struct device *cdev,
342 	struct device_attribute *attr, const char *buf, size_t count)
343 {
344 	struct Scsi_Host *shost = class_to_shost(cdev);
345 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
346 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
347 	int val = 0;
348 
349 	if (sscanf(buf, "%x", &val) != 1)
350 		return -EINVAL;
351 
352 	pm8001_ha->logging_level = val;
353 	return strlen(buf);
354 }
355 
356 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
357 	pm8001_ctl_logging_level_show, pm8001_ctl_logging_level_store);
358 /**
359  * pm8001_ctl_aap_log_show - aap1 event log
360  * @cdev: pointer to embedded class device
361  * @attr: device attribute (unused)
362  * @buf: the buffer returned
363  *
364  * A sysfs 'read-only' shost attribute.
365  */
366 static ssize_t pm8001_ctl_aap_log_show(struct device *cdev,
367 	struct device_attribute *attr, char *buf)
368 {
369 	struct Scsi_Host *shost = class_to_shost(cdev);
370 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
371 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
372 	int i;
373 #define AAP1_MEMMAP(r, c) \
374 	(*(u32 *)((u8*)pm8001_ha->memoryMap.region[AAP1].virt_ptr + (r) * 32 \
375 	+ (c)))
376 
377 	char *str = buf;
378 	int max = 2;
379 	for (i = 0; i < max; i++) {
380 		str += sprintf(str, "0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x"
381 			       "0x%08x 0x%08x\n",
382 			       AAP1_MEMMAP(i, 0),
383 			       AAP1_MEMMAP(i, 4),
384 			       AAP1_MEMMAP(i, 8),
385 			       AAP1_MEMMAP(i, 12),
386 			       AAP1_MEMMAP(i, 16),
387 			       AAP1_MEMMAP(i, 20),
388 			       AAP1_MEMMAP(i, 24),
389 			       AAP1_MEMMAP(i, 28));
390 	}
391 
392 	return str - buf;
393 }
394 static DEVICE_ATTR(aap_log, S_IRUGO, pm8001_ctl_aap_log_show, NULL);
395 /**
396  * pm8001_ctl_ib_queue_log_show - Out bound Queue log
397  * @cdev:pointer to embedded class device
398  * @attr: device attribute (unused)
399  * @buf: the buffer returned
400  * A sysfs 'read-only' shost attribute.
401  */
402 static ssize_t pm8001_ctl_ib_queue_log_show(struct device *cdev,
403 	struct device_attribute *attr, char *buf)
404 {
405 	struct Scsi_Host *shost = class_to_shost(cdev);
406 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
407 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
408 	int offset;
409 	char *str = buf;
410 	int start = 0;
411 #define IB_MEMMAP(c)	\
412 		(*(u32 *)((u8 *)pm8001_ha->	\
413 		memoryMap.region[IB].virt_ptr +	\
414 		pm8001_ha->evtlog_ib_offset + (c)))
415 
416 	for (offset = 0; offset < IB_OB_READ_TIMES; offset++) {
417 		str += sprintf(str, "0x%08x\n", IB_MEMMAP(start));
418 		start = start + 4;
419 	}
420 	pm8001_ha->evtlog_ib_offset += SYSFS_OFFSET;
421 	if (((pm8001_ha->evtlog_ib_offset) % (PM80XX_IB_OB_QUEUE_SIZE)) == 0)
422 		pm8001_ha->evtlog_ib_offset = 0;
423 
424 	return str - buf;
425 }
426 
427 static DEVICE_ATTR(ib_log, S_IRUGO, pm8001_ctl_ib_queue_log_show, NULL);
428 /**
429  * pm8001_ctl_ob_queue_log_show - Out bound Queue log
430  * @cdev:pointer to embedded class device
431  * @attr: device attribute (unused)
432  * @buf: the buffer returned
433  * A sysfs 'read-only' shost attribute.
434  */
435 
436 static ssize_t pm8001_ctl_ob_queue_log_show(struct device *cdev,
437 	struct device_attribute *attr, char *buf)
438 {
439 	struct Scsi_Host *shost = class_to_shost(cdev);
440 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
441 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
442 	int offset;
443 	char *str = buf;
444 	int start = 0;
445 #define OB_MEMMAP(c)	\
446 		(*(u32 *)((u8 *)pm8001_ha->	\
447 		memoryMap.region[OB].virt_ptr +	\
448 		pm8001_ha->evtlog_ob_offset + (c)))
449 
450 	for (offset = 0; offset < IB_OB_READ_TIMES; offset++) {
451 		str += sprintf(str, "0x%08x\n", OB_MEMMAP(start));
452 		start = start + 4;
453 	}
454 	pm8001_ha->evtlog_ob_offset += SYSFS_OFFSET;
455 	if (((pm8001_ha->evtlog_ob_offset) % (PM80XX_IB_OB_QUEUE_SIZE)) == 0)
456 		pm8001_ha->evtlog_ob_offset = 0;
457 
458 	return str - buf;
459 }
460 static DEVICE_ATTR(ob_log, S_IRUGO, pm8001_ctl_ob_queue_log_show, NULL);
461 /**
462  * pm8001_ctl_bios_version_show - Bios version Display
463  * @cdev:pointer to embedded class device
464  * @attr: device attribute (unused)
465  * @buf:the buffer returned
466  * A sysfs 'read-only' shost attribute.
467  */
468 static ssize_t pm8001_ctl_bios_version_show(struct device *cdev,
469 	struct device_attribute *attr, char *buf)
470 {
471 	struct Scsi_Host *shost = class_to_shost(cdev);
472 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
473 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
474 	char *str = buf;
475 	int bios_index;
476 	DECLARE_COMPLETION_ONSTACK(completion);
477 	struct pm8001_ioctl_payload payload;
478 
479 	pm8001_ha->nvmd_completion = &completion;
480 	payload.minor_function = 7;
481 	payload.offset = 0;
482 	payload.rd_length = 4096;
483 	payload.func_specific = kzalloc(4096, GFP_KERNEL);
484 	if (!payload.func_specific)
485 		return -ENOMEM;
486 	if (PM8001_CHIP_DISP->get_nvmd_req(pm8001_ha, &payload)) {
487 		kfree(payload.func_specific);
488 		return -ENOMEM;
489 	}
490 	wait_for_completion(&completion);
491 	for (bios_index = BIOSOFFSET; bios_index < BIOS_OFFSET_LIMIT;
492 		bios_index++)
493 		str += sprintf(str, "%c",
494 			*(payload.func_specific+bios_index));
495 	kfree(payload.func_specific);
496 	return str - buf;
497 }
498 static DEVICE_ATTR(bios_version, S_IRUGO, pm8001_ctl_bios_version_show, NULL);
499 /**
500  * event_log_size_show - event log size
501  * @cdev: pointer to embedded class device
502  * @attr: device attribute (unused)
503  * @buf: the buffer returned
504  *
505  * A sysfs read  shost attribute.
506  */
507 static ssize_t event_log_size_show(struct device *cdev,
508 	struct device_attribute *attr, char *buf)
509 {
510 	struct Scsi_Host *shost = class_to_shost(cdev);
511 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
512 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
513 
514 	return snprintf(buf, PAGE_SIZE, "%d\n",
515 		pm8001_ha->main_cfg_tbl.pm80xx_tbl.event_log_size);
516 }
517 static DEVICE_ATTR_RO(event_log_size);
518 /**
519  * pm8001_ctl_aap_log_show - IOP event log
520  * @cdev: pointer to embedded class device
521  * @attr: device attribute (unused)
522  * @buf: the buffer returned
523  *
524  * A sysfs 'read-only' shost attribute.
525  */
526 static ssize_t pm8001_ctl_iop_log_show(struct device *cdev,
527 	struct device_attribute *attr, char *buf)
528 {
529 	struct Scsi_Host *shost = class_to_shost(cdev);
530 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
531 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
532 	char *str = buf;
533 	u32 read_size =
534 		pm8001_ha->main_cfg_tbl.pm80xx_tbl.event_log_size / 1024;
535 	static u32 start, end, count;
536 	u32 max_read_times = 32;
537 	u32 max_count = (read_size * 1024) / (max_read_times * 4);
538 	u32 *temp = (u32 *)pm8001_ha->memoryMap.region[IOP].virt_ptr;
539 
540 	if ((count % max_count) == 0) {
541 		start = 0;
542 		end = max_read_times;
543 		count = 0;
544 	} else {
545 		start = end;
546 		end = end + max_read_times;
547 	}
548 
549 	for (; start < end; start++)
550 		str += sprintf(str, "%08x ", *(temp+start));
551 	count++;
552 	return str - buf;
553 }
554 static DEVICE_ATTR(iop_log, S_IRUGO, pm8001_ctl_iop_log_show, NULL);
555 
556 /**
557  ** pm8001_ctl_fatal_log_show - fatal error logging
558  ** @cdev:pointer to embedded class device
559  ** @attr: device attribute
560  ** @buf: the buffer returned
561  **
562  ** A sysfs 'read-only' shost attribute.
563  **/
564 
565 static ssize_t pm8001_ctl_fatal_log_show(struct device *cdev,
566 	struct device_attribute *attr, char *buf)
567 {
568 	ssize_t count;
569 
570 	count = pm80xx_get_fatal_dump(cdev, attr, buf);
571 	return count;
572 }
573 
574 static DEVICE_ATTR(fatal_log, S_IRUGO, pm8001_ctl_fatal_log_show, NULL);
575 
576 /**
577  ** non_fatal_log_show - non fatal error logging
578  ** @cdev:pointer to embedded class device
579  ** @attr: device attribute
580  ** @buf: the buffer returned
581  **
582  ** A sysfs 'read-only' shost attribute.
583  **/
584 static ssize_t non_fatal_log_show(struct device *cdev,
585 	struct device_attribute *attr, char *buf)
586 {
587 	u32 count;
588 
589 	count = pm80xx_get_non_fatal_dump(cdev, attr, buf);
590 	return count;
591 }
592 static DEVICE_ATTR_RO(non_fatal_log);
593 
594 static ssize_t non_fatal_count_show(struct device *cdev,
595 		struct device_attribute *attr, char *buf)
596 {
597 	struct Scsi_Host *shost = class_to_shost(cdev);
598 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
599 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
600 
601 	return snprintf(buf, PAGE_SIZE, "%08x",
602 			pm8001_ha->non_fatal_count);
603 }
604 
605 static ssize_t non_fatal_count_store(struct device *cdev,
606 		struct device_attribute *attr, const char *buf, size_t count)
607 {
608 	struct Scsi_Host *shost = class_to_shost(cdev);
609 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
610 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
611 	int val = 0;
612 
613 	if (kstrtoint(buf, 16, &val) != 0)
614 		return -EINVAL;
615 
616 	pm8001_ha->non_fatal_count = val;
617 	return strlen(buf);
618 }
619 static DEVICE_ATTR_RW(non_fatal_count);
620 
621 /**
622  ** pm8001_ctl_gsm_log_show - gsm dump collection
623  ** @cdev:pointer to embedded class device
624  ** @attr: device attribute (unused)
625  ** @buf: the buffer returned
626  ** A sysfs 'read-only' shost attribute.
627  **/
628 static ssize_t pm8001_ctl_gsm_log_show(struct device *cdev,
629 	struct device_attribute *attr, char *buf)
630 {
631 	ssize_t count;
632 
633 	count = pm8001_get_gsm_dump(cdev, SYSFS_OFFSET, buf);
634 	return count;
635 }
636 
637 static DEVICE_ATTR(gsm_log, S_IRUGO, pm8001_ctl_gsm_log_show, NULL);
638 
639 #define FLASH_CMD_NONE      0x00
640 #define FLASH_CMD_UPDATE    0x01
641 #define FLASH_CMD_SET_NVMD    0x02
642 
643 struct flash_command {
644      u8      command[8];
645      int     code;
646 };
647 
648 static struct flash_command flash_command_table[] =
649 {
650      {"set_nvmd",    FLASH_CMD_SET_NVMD},
651      {"update",      FLASH_CMD_UPDATE},
652      {"",            FLASH_CMD_NONE} /* Last entry should be NULL. */
653 };
654 
655 struct error_fw {
656      char    *reason;
657      int     err_code;
658 };
659 
660 static struct error_fw flash_error_table[] =
661 {
662      {"Failed to open fw image file",	FAIL_OPEN_BIOS_FILE},
663      {"image header mismatch",		FLASH_UPDATE_HDR_ERR},
664      {"image offset mismatch",		FLASH_UPDATE_OFFSET_ERR},
665      {"image CRC Error",		FLASH_UPDATE_CRC_ERR},
666      {"image length Error.",		FLASH_UPDATE_LENGTH_ERR},
667      {"Failed to program flash chip",	FLASH_UPDATE_HW_ERR},
668      {"Flash chip not supported.",	FLASH_UPDATE_DNLD_NOT_SUPPORTED},
669      {"Flash update disabled.",		FLASH_UPDATE_DISABLED},
670      {"Flash in progress",		FLASH_IN_PROGRESS},
671      {"Image file size Error",		FAIL_FILE_SIZE},
672      {"Input parameter error",		FAIL_PARAMETERS},
673      {"Out of memory",			FAIL_OUT_MEMORY},
674      {"OK", 0}	/* Last entry err_code = 0. */
675 };
676 
677 static int pm8001_set_nvmd(struct pm8001_hba_info *pm8001_ha)
678 {
679 	struct pm8001_ioctl_payload	*payload;
680 	DECLARE_COMPLETION_ONSTACK(completion);
681 	u8		*ioctlbuffer;
682 	u32		ret;
683 	u32		length = 1024 * 5 + sizeof(*payload) - 1;
684 
685 	if (pm8001_ha->fw_image->size > 4096) {
686 		pm8001_ha->fw_status = FAIL_FILE_SIZE;
687 		return -EFAULT;
688 	}
689 
690 	ioctlbuffer = kzalloc(length, GFP_KERNEL);
691 	if (!ioctlbuffer) {
692 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
693 		return -ENOMEM;
694 	}
695 	payload = (struct pm8001_ioctl_payload *)ioctlbuffer;
696 	memcpy((u8 *)&payload->func_specific, (u8 *)pm8001_ha->fw_image->data,
697 				pm8001_ha->fw_image->size);
698 	payload->wr_length = pm8001_ha->fw_image->size;
699 	payload->id = 0;
700 	payload->minor_function = 0x1;
701 	pm8001_ha->nvmd_completion = &completion;
702 	ret = PM8001_CHIP_DISP->set_nvmd_req(pm8001_ha, payload);
703 	if (ret) {
704 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
705 		goto out;
706 	}
707 	wait_for_completion(&completion);
708 out:
709 	kfree(ioctlbuffer);
710 	return ret;
711 }
712 
713 static int pm8001_update_flash(struct pm8001_hba_info *pm8001_ha)
714 {
715 	struct pm8001_ioctl_payload	*payload;
716 	DECLARE_COMPLETION_ONSTACK(completion);
717 	u8		*ioctlbuffer;
718 	struct fw_control_info	*fwControl;
719 	u32		partitionSize, partitionSizeTmp;
720 	u32		loopNumber, loopcount;
721 	struct pm8001_fw_image_header *image_hdr;
722 	u32		sizeRead = 0;
723 	u32		ret = 0;
724 	u32		length = 1024 * 16 + sizeof(*payload) - 1;
725 
726 	if (pm8001_ha->fw_image->size < 28) {
727 		pm8001_ha->fw_status = FAIL_FILE_SIZE;
728 		return -EFAULT;
729 	}
730 	ioctlbuffer = kzalloc(length, GFP_KERNEL);
731 	if (!ioctlbuffer) {
732 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
733 		return -ENOMEM;
734 	}
735 	image_hdr = (struct pm8001_fw_image_header *)pm8001_ha->fw_image->data;
736 	while (sizeRead < pm8001_ha->fw_image->size) {
737 		partitionSizeTmp =
738 			*(u32 *)((u8 *)&image_hdr->image_length + sizeRead);
739 		partitionSize = be32_to_cpu(partitionSizeTmp);
740 		loopcount = DIV_ROUND_UP(partitionSize + HEADER_LEN,
741 					IOCTL_BUF_SIZE);
742 		for (loopNumber = 0; loopNumber < loopcount; loopNumber++) {
743 			payload = (struct pm8001_ioctl_payload *)ioctlbuffer;
744 			payload->wr_length = 1024*16;
745 			payload->id = 0;
746 			fwControl =
747 			      (struct fw_control_info *)&payload->func_specific;
748 			fwControl->len = IOCTL_BUF_SIZE;   /* IN */
749 			fwControl->size = partitionSize + HEADER_LEN;/* IN */
750 			fwControl->retcode = 0;/* OUT */
751 			fwControl->offset = loopNumber * IOCTL_BUF_SIZE;/*OUT */
752 
753 		/* for the last chunk of data in case file size is not even with
754 		4k, load only the rest*/
755 		if (((loopcount-loopNumber) == 1) &&
756 			((partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE)) {
757 			fwControl->len =
758 				(partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE;
759 			memcpy((u8 *)fwControl->buffer,
760 				(u8 *)pm8001_ha->fw_image->data + sizeRead,
761 				(partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE);
762 			sizeRead +=
763 				(partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE;
764 		} else {
765 			memcpy((u8 *)fwControl->buffer,
766 				(u8 *)pm8001_ha->fw_image->data + sizeRead,
767 				IOCTL_BUF_SIZE);
768 			sizeRead += IOCTL_BUF_SIZE;
769 		}
770 
771 		pm8001_ha->nvmd_completion = &completion;
772 		ret = PM8001_CHIP_DISP->fw_flash_update_req(pm8001_ha, payload);
773 		if (ret) {
774 			pm8001_ha->fw_status = FAIL_OUT_MEMORY;
775 			goto out;
776 		}
777 		wait_for_completion(&completion);
778 		if (fwControl->retcode > FLASH_UPDATE_IN_PROGRESS) {
779 			pm8001_ha->fw_status = fwControl->retcode;
780 			ret = -EFAULT;
781 			goto out;
782 		}
783 		}
784 	}
785 out:
786 	kfree(ioctlbuffer);
787 	return ret;
788 }
789 static ssize_t pm8001_store_update_fw(struct device *cdev,
790 				      struct device_attribute *attr,
791 				      const char *buf, size_t count)
792 {
793 	struct Scsi_Host *shost = class_to_shost(cdev);
794 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
795 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
796 	char *cmd_ptr, *filename_ptr;
797 	int res, i;
798 	int flash_command = FLASH_CMD_NONE;
799 	int ret;
800 
801 	if (!capable(CAP_SYS_ADMIN))
802 		return -EACCES;
803 
804 	/* this test protects us from running two flash processes at once,
805 	 * so we should start with this test */
806 	if (pm8001_ha->fw_status == FLASH_IN_PROGRESS)
807 		return -EINPROGRESS;
808 	pm8001_ha->fw_status = FLASH_IN_PROGRESS;
809 
810 	cmd_ptr = kcalloc(count, 2, GFP_KERNEL);
811 	if (!cmd_ptr) {
812 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
813 		return -ENOMEM;
814 	}
815 
816 	filename_ptr = cmd_ptr + count;
817 	res = sscanf(buf, "%s %s", cmd_ptr, filename_ptr);
818 	if (res != 2) {
819 		pm8001_ha->fw_status = FAIL_PARAMETERS;
820 		ret = -EINVAL;
821 		goto out;
822 	}
823 
824 	for (i = 0; flash_command_table[i].code != FLASH_CMD_NONE; i++) {
825 		if (!memcmp(flash_command_table[i].command,
826 				 cmd_ptr, strlen(cmd_ptr))) {
827 			flash_command = flash_command_table[i].code;
828 			break;
829 		}
830 	}
831 	if (flash_command == FLASH_CMD_NONE) {
832 		pm8001_ha->fw_status = FAIL_PARAMETERS;
833 		ret = -EINVAL;
834 		goto out;
835 	}
836 
837 	ret = request_firmware(&pm8001_ha->fw_image,
838 			       filename_ptr,
839 			       pm8001_ha->dev);
840 
841 	if (ret) {
842 		PM8001_FAIL_DBG(pm8001_ha,
843 			pm8001_printk(
844 			"Failed to load firmware image file %s,	error %d\n",
845 			filename_ptr, ret));
846 		pm8001_ha->fw_status = FAIL_OPEN_BIOS_FILE;
847 		goto out;
848 	}
849 
850 	if (FLASH_CMD_UPDATE == flash_command)
851 		ret = pm8001_update_flash(pm8001_ha);
852 	else
853 		ret = pm8001_set_nvmd(pm8001_ha);
854 
855 	release_firmware(pm8001_ha->fw_image);
856 out:
857 	kfree(cmd_ptr);
858 
859 	if (ret)
860 		return ret;
861 
862 	pm8001_ha->fw_status = FLASH_OK;
863 	return count;
864 }
865 
866 static ssize_t pm8001_show_update_fw(struct device *cdev,
867 				     struct device_attribute *attr, char *buf)
868 {
869 	int i;
870 	struct Scsi_Host *shost = class_to_shost(cdev);
871 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
872 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
873 
874 	for (i = 0; flash_error_table[i].err_code != 0; i++) {
875 		if (flash_error_table[i].err_code == pm8001_ha->fw_status)
876 			break;
877 	}
878 	if (pm8001_ha->fw_status != FLASH_IN_PROGRESS)
879 		pm8001_ha->fw_status = FLASH_OK;
880 
881 	return snprintf(buf, PAGE_SIZE, "status=%x %s\n",
882 			flash_error_table[i].err_code,
883 			flash_error_table[i].reason);
884 }
885 
886 static DEVICE_ATTR(update_fw, S_IRUGO|S_IWUSR|S_IWGRP,
887 	pm8001_show_update_fw, pm8001_store_update_fw);
888 struct device_attribute *pm8001_host_attrs[] = {
889 	&dev_attr_interface_rev,
890 	&dev_attr_controller_fatal_error,
891 	&dev_attr_fw_version,
892 	&dev_attr_update_fw,
893 	&dev_attr_aap_log,
894 	&dev_attr_iop_log,
895 	&dev_attr_fatal_log,
896 	&dev_attr_non_fatal_log,
897 	&dev_attr_non_fatal_count,
898 	&dev_attr_gsm_log,
899 	&dev_attr_max_out_io,
900 	&dev_attr_max_devices,
901 	&dev_attr_max_sg_list,
902 	&dev_attr_sas_spec_support,
903 	&dev_attr_logging_level,
904 	&dev_attr_event_log_size,
905 	&dev_attr_host_sas_address,
906 	&dev_attr_bios_version,
907 	&dev_attr_ib_log,
908 	&dev_attr_ob_log,
909 	&dev_attr_ila_version,
910 	&dev_attr_inc_fw_ver,
911 	NULL,
912 };
913 
914