xref: /linux/drivers/scsi/pm8001/pm8001_ctl.c (revision 6093a688a07da07808f0122f9aa2a3eed250d853)
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 sysfs_emit(buf, "%d\n",
65 			pm8001_ha->main_cfg_tbl.pm8001_tbl.interface_rev);
66 	} else {
67 		return sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%d\n",
195 			pm8001_ha->main_cfg_tbl.pm8001_tbl.max_out_io);
196 	} else {
197 		return sysfs_emit(buf, "%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 sysfs_emit(buf, "%04d\n",
219 			(u16)(pm8001_ha->main_cfg_tbl.pm8001_tbl.max_sgl >> 16));
220 	} else {
221 		return sysfs_emit(buf, "%04d\n",
222 			(u16)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_sgl >> 16));
223 	}
224 }
225 static DEVICE_ATTR(max_devices, S_IRUGO, pm8001_ctl_max_devices_show, NULL);
226 /**
227  * pm8001_ctl_max_sg_list_show - max sg list supported iff not 0.0 for no
228  * hardware limitation
229  * @cdev: pointer to embedded class device
230  * @attr: device attribute (unused)
231  * @buf: the buffer returned
232  *
233  * A sysfs 'read-only' shost attribute.
234  */
235 static ssize_t pm8001_ctl_max_sg_list_show(struct device *cdev,
236 	struct device_attribute *attr, char *buf)
237 {
238 	struct Scsi_Host *shost = class_to_shost(cdev);
239 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
240 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
241 
242 	if (pm8001_ha->chip_id == chip_8001) {
243 		return sysfs_emit(buf, "%04d\n",
244 			pm8001_ha->main_cfg_tbl.pm8001_tbl.max_sgl & 0x0000FFFF);
245 	} else {
246 		return sysfs_emit(buf, "%04d\n",
247 			pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_sgl & 0x0000FFFF);
248 	}
249 }
250 static DEVICE_ATTR(max_sg_list, S_IRUGO, pm8001_ctl_max_sg_list_show, NULL);
251 
252 #define SAS_1_0 0x1
253 #define SAS_1_1 0x2
254 #define SAS_2_0 0x4
255 
256 static ssize_t
257 show_sas_spec_support_status(unsigned int mode, char *buf)
258 {
259 	ssize_t len = 0;
260 
261 	if (mode & SAS_1_1)
262 		len = sprintf(buf, "%s", "SAS1.1");
263 	if (mode & SAS_2_0)
264 		len += sprintf(buf + len, "%s%s", len ? ", " : "", "SAS2.0");
265 	len += sprintf(buf + len, "\n");
266 
267 	return len;
268 }
269 
270 /**
271  * pm8001_ctl_sas_spec_support_show - sas spec supported
272  * @cdev: pointer to embedded class device
273  * @attr: device attribute (unused)
274  * @buf: the buffer returned
275  *
276  * A sysfs 'read-only' shost attribute.
277  */
278 static ssize_t pm8001_ctl_sas_spec_support_show(struct device *cdev,
279 	struct device_attribute *attr, char *buf)
280 {
281 	unsigned int mode;
282 	struct Scsi_Host *shost = class_to_shost(cdev);
283 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
284 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
285 	/* fe000000 means supports SAS2.1 */
286 	if (pm8001_ha->chip_id == chip_8001)
287 		mode = (pm8001_ha->main_cfg_tbl.pm8001_tbl.ctrl_cap_flag &
288 							0xfe000000)>>25;
289 	else
290 		/* fe000000 means supports SAS2.1 */
291 		mode = (pm8001_ha->main_cfg_tbl.pm80xx_tbl.ctrl_cap_flag &
292 							0xfe000000)>>25;
293 	return show_sas_spec_support_status(mode, buf);
294 }
295 static DEVICE_ATTR(sas_spec_support, S_IRUGO,
296 		   pm8001_ctl_sas_spec_support_show, NULL);
297 
298 /**
299  * pm8001_ctl_host_sas_address_show - sas address
300  * @cdev: pointer to embedded class device
301  * @attr: device attribute (unused)
302  * @buf: the buffer returned
303  *
304  * This is the controller sas address
305  *
306  * A sysfs 'read-only' shost attribute.
307  */
308 static ssize_t pm8001_ctl_host_sas_address_show(struct device *cdev,
309 	struct device_attribute *attr, char *buf)
310 {
311 	struct Scsi_Host *shost = class_to_shost(cdev);
312 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
313 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
314 	return sysfs_emit(buf, "0x%016llx\n",
315 			be64_to_cpu(*(__be64 *)pm8001_ha->sas_addr));
316 }
317 static DEVICE_ATTR(host_sas_address, S_IRUGO,
318 		   pm8001_ctl_host_sas_address_show, NULL);
319 
320 /**
321  * pm8001_ctl_logging_level_show - logging level
322  * @cdev: pointer to embedded class device
323  * @attr: device attribute (unused)
324  * @buf: the buffer returned
325  *
326  * A sysfs 'read/write' shost attribute.
327  */
328 static ssize_t pm8001_ctl_logging_level_show(struct device *cdev,
329 	struct device_attribute *attr, char *buf)
330 {
331 	struct Scsi_Host *shost = class_to_shost(cdev);
332 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
333 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
334 
335 	return sysfs_emit(buf, "%08xh\n", pm8001_ha->logging_level);
336 }
337 
338 static ssize_t pm8001_ctl_logging_level_store(struct device *cdev,
339 	struct device_attribute *attr, const char *buf, size_t count)
340 {
341 	struct Scsi_Host *shost = class_to_shost(cdev);
342 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
343 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
344 	int val = 0;
345 
346 	if (sscanf(buf, "%x", &val) != 1)
347 		return -EINVAL;
348 
349 	pm8001_ha->logging_level = val;
350 	return strlen(buf);
351 }
352 
353 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
354 	pm8001_ctl_logging_level_show, pm8001_ctl_logging_level_store);
355 /**
356  * pm8001_ctl_aap_log_show - aap1 event log
357  * @cdev: pointer to embedded class device
358  * @attr: device attribute (unused)
359  * @buf: the buffer returned
360  *
361  * A sysfs 'read-only' shost attribute.
362  */
363 static ssize_t pm8001_ctl_aap_log_show(struct device *cdev,
364 	struct device_attribute *attr, char *buf)
365 {
366 	struct Scsi_Host *shost = class_to_shost(cdev);
367 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
368 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
369 	u8 *ptr = (u8 *)pm8001_ha->memoryMap.region[AAP1].virt_ptr;
370 	int i;
371 
372 	char *str = buf;
373 	int max = 2;
374 	for (i = 0; i < max; i++) {
375 		str += sprintf(str, "0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x"
376 			       "0x%08x 0x%08x\n",
377 			       pm8001_ctl_aap1_memmap(ptr, i, 0),
378 			       pm8001_ctl_aap1_memmap(ptr, i, 4),
379 			       pm8001_ctl_aap1_memmap(ptr, i, 8),
380 			       pm8001_ctl_aap1_memmap(ptr, i, 12),
381 			       pm8001_ctl_aap1_memmap(ptr, i, 16),
382 			       pm8001_ctl_aap1_memmap(ptr, i, 20),
383 			       pm8001_ctl_aap1_memmap(ptr, i, 24),
384 			       pm8001_ctl_aap1_memmap(ptr, i, 28));
385 	}
386 
387 	return str - buf;
388 }
389 static DEVICE_ATTR(aap_log, S_IRUGO, pm8001_ctl_aap_log_show, NULL);
390 /**
391  * pm8001_ctl_ib_queue_log_show - Out bound Queue log
392  * @cdev:pointer to embedded class device
393  * @attr: device attribute (unused)
394  * @buf: the buffer returned
395  *
396  * A sysfs 'read-only' shost attribute.
397  */
398 static ssize_t pm8001_ctl_ib_queue_log_show(struct device *cdev,
399 	struct device_attribute *attr, char *buf)
400 {
401 	struct Scsi_Host *shost = class_to_shost(cdev);
402 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
403 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
404 	int offset;
405 	char *str = buf;
406 	int start = 0;
407 	u32 ib_offset = pm8001_ha->ib_offset;
408 	u32 queue_size = pm8001_ha->max_q_num * PM8001_MPI_QUEUE * 128;
409 #define IB_MEMMAP(c)	\
410 		(*(u32 *)((u8 *)pm8001_ha->	\
411 		memoryMap.region[ib_offset].virt_ptr +	\
412 		pm8001_ha->evtlog_ib_offset + (c)))
413 
414 	for (offset = 0; offset < IB_OB_READ_TIMES; offset++) {
415 		str += sprintf(str, "0x%08x\n", IB_MEMMAP(start));
416 		start = start + 4;
417 	}
418 	pm8001_ha->evtlog_ib_offset += SYSFS_OFFSET;
419 	if (((pm8001_ha->evtlog_ib_offset) % queue_size) == 0)
420 		pm8001_ha->evtlog_ib_offset = 0;
421 
422 	return str - buf;
423 }
424 
425 static DEVICE_ATTR(ib_log, S_IRUGO, pm8001_ctl_ib_queue_log_show, NULL);
426 /**
427  * pm8001_ctl_ob_queue_log_show - Out bound Queue log
428  * @cdev:pointer to embedded class device
429  * @attr: device attribute (unused)
430  * @buf: the buffer returned
431  *
432  * A sysfs 'read-only' shost attribute.
433  */
434 
435 static ssize_t pm8001_ctl_ob_queue_log_show(struct device *cdev,
436 	struct device_attribute *attr, char *buf)
437 {
438 	struct Scsi_Host *shost = class_to_shost(cdev);
439 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
440 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
441 	int offset;
442 	char *str = buf;
443 	int start = 0;
444 	u32 ob_offset = pm8001_ha->ob_offset;
445 	u32 queue_size = pm8001_ha->max_q_num * PM8001_MPI_QUEUE * 128;
446 #define OB_MEMMAP(c)	\
447 		(*(u32 *)((u8 *)pm8001_ha->	\
448 		memoryMap.region[ob_offset].virt_ptr +	\
449 		pm8001_ha->evtlog_ob_offset + (c)))
450 
451 	for (offset = 0; offset < IB_OB_READ_TIMES; offset++) {
452 		str += sprintf(str, "0x%08x\n", OB_MEMMAP(start));
453 		start = start + 4;
454 	}
455 	pm8001_ha->evtlog_ob_offset += SYSFS_OFFSET;
456 	if (((pm8001_ha->evtlog_ob_offset) % queue_size) == 0)
457 		pm8001_ha->evtlog_ob_offset = 0;
458 
459 	return str - buf;
460 }
461 static DEVICE_ATTR(ob_log, S_IRUGO, pm8001_ctl_ob_queue_log_show, NULL);
462 /**
463  * pm8001_ctl_bios_version_show - Bios version Display
464  * @cdev:pointer to embedded class device
465  * @attr: device attribute (unused)
466  * @buf:the buffer returned
467  *
468  * A sysfs 'read-only' shost attribute.
469  */
470 static ssize_t pm8001_ctl_bios_version_show(struct device *cdev,
471 	struct device_attribute *attr, char *buf)
472 {
473 	struct Scsi_Host *shost = class_to_shost(cdev);
474 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
475 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
476 	char *str = buf;
477 	int bios_index;
478 	DECLARE_COMPLETION_ONSTACK(completion);
479 	struct pm8001_ioctl_payload payload;
480 
481 	pm8001_ha->nvmd_completion = &completion;
482 	payload.minor_function = 7;
483 	payload.offset = 0;
484 	payload.rd_length = 4096;
485 	payload.func_specific = kzalloc(4096, GFP_KERNEL);
486 	if (!payload.func_specific)
487 		return -ENOMEM;
488 	if (PM8001_CHIP_DISP->get_nvmd_req(pm8001_ha, &payload)) {
489 		kfree(payload.func_specific);
490 		return -ENOMEM;
491 	}
492 	wait_for_completion(&completion);
493 	for (bios_index = BIOSOFFSET; bios_index < BIOS_OFFSET_LIMIT;
494 		bios_index++)
495 		str += sprintf(str, "%c",
496 			*(payload.func_specific+bios_index));
497 	kfree(payload.func_specific);
498 	return str - buf;
499 }
500 static DEVICE_ATTR(bios_version, S_IRUGO, pm8001_ctl_bios_version_show, NULL);
501 /**
502  * event_log_size_show - event log size
503  * @cdev: pointer to embedded class device
504  * @attr: device attribute (unused)
505  * @buf: the buffer returned
506  *
507  * A sysfs read  shost attribute.
508  */
509 static ssize_t event_log_size_show(struct device *cdev,
510 	struct device_attribute *attr, char *buf)
511 {
512 	struct Scsi_Host *shost = class_to_shost(cdev);
513 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
514 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
515 
516 	return sysfs_emit(buf, "%d\n",
517 		pm8001_ha->main_cfg_tbl.pm80xx_tbl.event_log_size);
518 }
519 static DEVICE_ATTR_RO(event_log_size);
520 /**
521  * pm8001_ctl_iop_log_show - IOP event log
522  * @cdev: pointer to embedded class device
523  * @attr: device attribute (unused)
524  * @buf: the buffer returned
525  *
526  * A sysfs 'read-only' shost attribute.
527  */
528 static ssize_t pm8001_ctl_iop_log_show(struct device *cdev,
529 	struct device_attribute *attr, char *buf)
530 {
531 	struct Scsi_Host *shost = class_to_shost(cdev);
532 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
533 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
534 	char *str = buf;
535 	u32 read_size =
536 		pm8001_ha->main_cfg_tbl.pm80xx_tbl.event_log_size / 1024;
537 	u32 max_read_times = 32;
538 	u32 max_count = (read_size * 1024) / (max_read_times * 4);
539 	u32 *temp = (u32 *)pm8001_ha->memoryMap.region[IOP].virt_ptr;
540 
541 	mutex_lock(&pm8001_ha->iop_log_lock);
542 
543 	if ((pm8001_ha->iop_log_count % max_count) == 0) {
544 		pm8001_ha->iop_log_start = 0;
545 		pm8001_ha->iop_log_end = max_read_times;
546 		pm8001_ha->iop_log_count = 0;
547 	} else {
548 		pm8001_ha->iop_log_start = pm8001_ha->iop_log_end;
549 		pm8001_ha->iop_log_end = pm8001_ha->iop_log_end + max_read_times;
550 	}
551 
552 	for (; pm8001_ha->iop_log_start < pm8001_ha->iop_log_end; pm8001_ha->iop_log_start++)
553 		str += sprintf(str, "%08x ", *(temp+pm8001_ha->iop_log_start));
554 	pm8001_ha->iop_log_count++;
555 	mutex_unlock(&pm8001_ha->iop_log_lock);
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 sysfs_emit(buf, "%08x\n",
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] __nonstring;
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 	int		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 	__be32		partitionSizeTmp;
723 	u32		partitionSize;
724 	u32		loopNumber, loopcount;
725 	struct pm8001_fw_image_header *image_hdr;
726 	u32		sizeRead = 0;
727 	u32		ret = 0;
728 	u32		length = 1024 * 16 + sizeof(*payload) - 1;
729 	u32		fc_len;
730 	u8		*read_buf;
731 
732 	if (pm8001_ha->fw_image->size < 28) {
733 		pm8001_ha->fw_status = FAIL_FILE_SIZE;
734 		return -EFAULT;
735 	}
736 	ioctlbuffer = kzalloc(length, GFP_KERNEL);
737 	if (!ioctlbuffer) {
738 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
739 		return -ENOMEM;
740 	}
741 	image_hdr = (struct pm8001_fw_image_header *)pm8001_ha->fw_image->data;
742 	while (sizeRead < pm8001_ha->fw_image->size) {
743 		partitionSizeTmp =
744 			*(__be32 *)((u8 *)&image_hdr->image_length + sizeRead);
745 		partitionSize = be32_to_cpu(partitionSizeTmp);
746 		loopcount = DIV_ROUND_UP(partitionSize + HEADER_LEN,
747 					IOCTL_BUF_SIZE);
748 		for (loopNumber = 0; loopNumber < loopcount; loopNumber++) {
749 			payload = (struct pm8001_ioctl_payload *)ioctlbuffer;
750 			payload->wr_length = 1024*16;
751 			payload->id = 0;
752 			fwControl =
753 			      (struct fw_control_info *)&payload->func_specific;
754 			fwControl->len = IOCTL_BUF_SIZE;   /* IN */
755 			fwControl->size = partitionSize + HEADER_LEN;/* IN */
756 			fwControl->retcode = 0;/* OUT */
757 			fwControl->offset = loopNumber * IOCTL_BUF_SIZE;/*OUT */
758 
759 			/*
760 			 * for the last chunk of data in case file size is
761 			 * not even with 4k, load only the rest
762 			 */
763 
764 			read_buf  = (u8 *)pm8001_ha->fw_image->data + sizeRead;
765 			fc_len = (partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE;
766 
767 			if (loopcount - loopNumber == 1 && fc_len) {
768 				fwControl->len = fc_len;
769 				memcpy((u8 *)fwControl->buffer, read_buf, fc_len);
770 				sizeRead += fc_len;
771 			} else {
772 				memcpy((u8 *)fwControl->buffer, read_buf, IOCTL_BUF_SIZE);
773 				sizeRead += IOCTL_BUF_SIZE;
774 			}
775 
776 			pm8001_ha->nvmd_completion = &completion;
777 			ret = PM8001_CHIP_DISP->fw_flash_update_req(pm8001_ha, payload);
778 			if (ret) {
779 				pm8001_ha->fw_status = FAIL_OUT_MEMORY;
780 				goto out;
781 			}
782 			wait_for_completion(&completion);
783 			if (fwControl->retcode > FLASH_UPDATE_IN_PROGRESS) {
784 				pm8001_ha->fw_status = fwControl->retcode;
785 				ret = -EFAULT;
786 				goto out;
787 			}
788 		}
789 	}
790 out:
791 	kfree(ioctlbuffer);
792 	return ret;
793 }
794 static ssize_t pm8001_store_update_fw(struct device *cdev,
795 				      struct device_attribute *attr,
796 				      const char *buf, size_t count)
797 {
798 	struct Scsi_Host *shost = class_to_shost(cdev);
799 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
800 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
801 	char *cmd_ptr, *filename_ptr;
802 	int res, i;
803 	int flash_command = FLASH_CMD_NONE;
804 	int ret;
805 
806 	if (!capable(CAP_SYS_ADMIN))
807 		return -EACCES;
808 
809 	/* this test protects us from running two flash processes at once,
810 	 * so we should start with this test */
811 	if (pm8001_ha->fw_status == FLASH_IN_PROGRESS)
812 		return -EINPROGRESS;
813 	pm8001_ha->fw_status = FLASH_IN_PROGRESS;
814 
815 	cmd_ptr = kcalloc(count, 2, GFP_KERNEL);
816 	if (!cmd_ptr) {
817 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
818 		return -ENOMEM;
819 	}
820 
821 	filename_ptr = cmd_ptr + count;
822 	res = sscanf(buf, "%s %s", cmd_ptr, filename_ptr);
823 	if (res != 2) {
824 		pm8001_ha->fw_status = FAIL_PARAMETERS;
825 		ret = -EINVAL;
826 		goto out;
827 	}
828 
829 	for (i = 0; flash_command_table[i].code != FLASH_CMD_NONE; i++) {
830 		if (!memcmp(flash_command_table[i].command,
831 				 cmd_ptr, strlen(cmd_ptr))) {
832 			flash_command = flash_command_table[i].code;
833 			break;
834 		}
835 	}
836 	if (flash_command == FLASH_CMD_NONE) {
837 		pm8001_ha->fw_status = FAIL_PARAMETERS;
838 		ret = -EINVAL;
839 		goto out;
840 	}
841 
842 	ret = request_firmware(&pm8001_ha->fw_image,
843 			       filename_ptr,
844 			       pm8001_ha->dev);
845 
846 	if (ret) {
847 		pm8001_dbg(pm8001_ha, FAIL,
848 			   "Failed to load firmware image file %s, error %d\n",
849 			   filename_ptr, ret);
850 		pm8001_ha->fw_status = FAIL_OPEN_BIOS_FILE;
851 		goto out;
852 	}
853 
854 	if (FLASH_CMD_UPDATE == flash_command)
855 		ret = pm8001_update_flash(pm8001_ha);
856 	else
857 		ret = pm8001_set_nvmd(pm8001_ha);
858 
859 	release_firmware(pm8001_ha->fw_image);
860 out:
861 	kfree(cmd_ptr);
862 
863 	if (ret)
864 		return ret;
865 
866 	pm8001_ha->fw_status = FLASH_OK;
867 	return count;
868 }
869 
870 static ssize_t pm8001_show_update_fw(struct device *cdev,
871 				     struct device_attribute *attr, char *buf)
872 {
873 	int i;
874 	struct Scsi_Host *shost = class_to_shost(cdev);
875 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
876 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
877 
878 	for (i = 0; flash_error_table[i].err_code != 0; i++) {
879 		if (flash_error_table[i].err_code == pm8001_ha->fw_status)
880 			break;
881 	}
882 	if (pm8001_ha->fw_status != FLASH_IN_PROGRESS)
883 		pm8001_ha->fw_status = FLASH_OK;
884 
885 	return sysfs_emit(buf, "status=%x %s\n",
886 			  flash_error_table[i].err_code,
887 			  flash_error_table[i].reason);
888 }
889 static DEVICE_ATTR(update_fw, S_IRUGO|S_IWUSR|S_IWGRP,
890 	pm8001_show_update_fw, pm8001_store_update_fw);
891 
892 static const char *const mpiStateText[] = {
893 	"MPI is not initialized",
894 	"MPI is successfully initialized",
895 	"MPI termination is in progress",
896 	"MPI initialization failed with error in [31:16]"
897 };
898 
899 /**
900  * ctl_mpi_state_show - controller MPI state check
901  * @cdev: pointer to embedded class device
902  * @attr: device attribute (unused)
903  * @buf: the buffer returned
904  *
905  * A sysfs 'read-only' shost attribute.
906  */
907 static ssize_t ctl_mpi_state_show(struct device *cdev,
908 		struct device_attribute *attr, char *buf)
909 {
910 	struct Scsi_Host *shost = class_to_shost(cdev);
911 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
912 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
913 	unsigned int mpidw0;
914 
915 	mpidw0 = pm8001_mr32(pm8001_ha->general_stat_tbl_addr, 0);
916 	return sysfs_emit(buf, "%s\n", mpiStateText[mpidw0 & 0x0003]);
917 }
918 static DEVICE_ATTR_RO(ctl_mpi_state);
919 
920 /**
921  * ctl_hmi_error_show - controller MPI initialization fails
922  * @cdev: pointer to embedded class device
923  * @attr: device attribute (unused)
924  * @buf: the buffer returned
925  *
926  * A sysfs 'read-only' shost attribute.
927  */
928 static ssize_t ctl_hmi_error_show(struct device *cdev,
929 		struct device_attribute *attr, char *buf)
930 {
931 	struct Scsi_Host *shost = class_to_shost(cdev);
932 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
933 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
934 	unsigned int mpidw0;
935 
936 	mpidw0 = pm8001_mr32(pm8001_ha->general_stat_tbl_addr, 0);
937 	return sysfs_emit(buf, "0x%08x\n", (mpidw0 >> 16));
938 }
939 static DEVICE_ATTR_RO(ctl_hmi_error);
940 
941 /**
942  * ctl_raae_count_show - controller raae count check
943  * @cdev: pointer to embedded class device
944  * @attr: device attribute (unused)
945  * @buf: the buffer returned
946  *
947  * A sysfs 'read-only' shost attribute.
948  */
949 static ssize_t ctl_raae_count_show(struct device *cdev,
950 		struct device_attribute *attr, char *buf)
951 {
952 	struct Scsi_Host *shost = class_to_shost(cdev);
953 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
954 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
955 	unsigned int raaecnt;
956 
957 	raaecnt = pm8001_mr32(pm8001_ha->general_stat_tbl_addr, 12);
958 	return sysfs_emit(buf, "0x%08x\n", raaecnt);
959 }
960 static DEVICE_ATTR_RO(ctl_raae_count);
961 
962 /**
963  * ctl_iop0_count_show - controller iop0 count check
964  * @cdev: pointer to embedded class device
965  * @attr: device attribute (unused)
966  * @buf: the buffer returned
967  *
968  * A sysfs 'read-only' shost attribute.
969  */
970 static ssize_t ctl_iop0_count_show(struct device *cdev,
971 		struct device_attribute *attr, char *buf)
972 {
973 	struct Scsi_Host *shost = class_to_shost(cdev);
974 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
975 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
976 	unsigned int iop0cnt;
977 
978 	iop0cnt = pm8001_mr32(pm8001_ha->general_stat_tbl_addr, 16);
979 	return sysfs_emit(buf, "0x%08x\n", iop0cnt);
980 }
981 static DEVICE_ATTR_RO(ctl_iop0_count);
982 
983 /**
984  * ctl_iop1_count_show - controller iop1 count check
985  * @cdev: pointer to embedded class device
986  * @attr: device attribute (unused)
987  * @buf: the buffer returned
988  *
989  * A sysfs 'read-only' shost attribute.
990  */
991 static ssize_t ctl_iop1_count_show(struct device *cdev,
992 		struct device_attribute *attr, char *buf)
993 {
994 	struct Scsi_Host *shost = class_to_shost(cdev);
995 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
996 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
997 	unsigned int iop1cnt;
998 
999 	iop1cnt = pm8001_mr32(pm8001_ha->general_stat_tbl_addr, 20);
1000 	return sysfs_emit(buf, "0x%08x\n", iop1cnt);
1001 
1002 }
1003 static DEVICE_ATTR_RO(ctl_iop1_count);
1004 
1005 static struct attribute *pm8001_host_attrs[] = {
1006 	&dev_attr_interface_rev.attr,
1007 	&dev_attr_controller_fatal_error.attr,
1008 	&dev_attr_fw_version.attr,
1009 	&dev_attr_update_fw.attr,
1010 	&dev_attr_aap_log.attr,
1011 	&dev_attr_iop_log.attr,
1012 	&dev_attr_fatal_log.attr,
1013 	&dev_attr_non_fatal_log.attr,
1014 	&dev_attr_non_fatal_count.attr,
1015 	&dev_attr_gsm_log.attr,
1016 	&dev_attr_max_out_io.attr,
1017 	&dev_attr_max_devices.attr,
1018 	&dev_attr_max_sg_list.attr,
1019 	&dev_attr_sas_spec_support.attr,
1020 	&dev_attr_logging_level.attr,
1021 	&dev_attr_event_log_size.attr,
1022 	&dev_attr_host_sas_address.attr,
1023 	&dev_attr_bios_version.attr,
1024 	&dev_attr_ib_log.attr,
1025 	&dev_attr_ob_log.attr,
1026 	&dev_attr_ila_version.attr,
1027 	&dev_attr_inc_fw_ver.attr,
1028 	&dev_attr_ctl_mpi_state.attr,
1029 	&dev_attr_ctl_hmi_error.attr,
1030 	&dev_attr_ctl_raae_count.attr,
1031 	&dev_attr_ctl_iop0_count.attr,
1032 	&dev_attr_ctl_iop1_count.attr,
1033 	NULL,
1034 };
1035 
1036 static const struct attribute_group pm8001_host_attr_group = {
1037 	.attrs = pm8001_host_attrs
1038 };
1039 
1040 const struct attribute_group *pm8001_host_groups[] = {
1041 	&pm8001_host_attr_group,
1042 	NULL
1043 };
1044 
1045 const struct attribute_group *pm8001_sdev_groups[] = {
1046 	&sas_ata_sdev_attr_group,
1047 	NULL
1048 };
1049