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