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