xref: /openbmc/linux/drivers/scsi/pm8001/pm8001_ctl.c (revision 7b73a9c8)
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.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 /**
559  ** pm8001_ctl_gsm_log_show - gsm dump collection
560  ** @cdev:pointer to embedded class device
561  ** @buf: the buffer returned
562  **A sysfs 'read-only' shost attribute.
563  **/
564 static ssize_t pm8001_ctl_gsm_log_show(struct device *cdev,
565 	struct device_attribute *attr, char *buf)
566 {
567 	ssize_t count;
568 
569 	count = pm8001_get_gsm_dump(cdev, SYSFS_OFFSET, buf);
570 	return count;
571 }
572 
573 static DEVICE_ATTR(gsm_log, S_IRUGO, pm8001_ctl_gsm_log_show, NULL);
574 
575 #define FLASH_CMD_NONE      0x00
576 #define FLASH_CMD_UPDATE    0x01
577 #define FLASH_CMD_SET_NVMD    0x02
578 
579 struct flash_command {
580      u8      command[8];
581      int     code;
582 };
583 
584 static struct flash_command flash_command_table[] =
585 {
586      {"set_nvmd",    FLASH_CMD_SET_NVMD},
587      {"update",      FLASH_CMD_UPDATE},
588      {"",            FLASH_CMD_NONE} /* Last entry should be NULL. */
589 };
590 
591 struct error_fw {
592      char    *reason;
593      int     err_code;
594 };
595 
596 static struct error_fw flash_error_table[] =
597 {
598      {"Failed to open fw image file",	FAIL_OPEN_BIOS_FILE},
599      {"image header mismatch",		FLASH_UPDATE_HDR_ERR},
600      {"image offset mismatch",		FLASH_UPDATE_OFFSET_ERR},
601      {"image CRC Error",		FLASH_UPDATE_CRC_ERR},
602      {"image length Error.",		FLASH_UPDATE_LENGTH_ERR},
603      {"Failed to program flash chip",	FLASH_UPDATE_HW_ERR},
604      {"Flash chip not supported.",	FLASH_UPDATE_DNLD_NOT_SUPPORTED},
605      {"Flash update disabled.",		FLASH_UPDATE_DISABLED},
606      {"Flash in progress",		FLASH_IN_PROGRESS},
607      {"Image file size Error",		FAIL_FILE_SIZE},
608      {"Input parameter error",		FAIL_PARAMETERS},
609      {"Out of memory",			FAIL_OUT_MEMORY},
610      {"OK", 0}	/* Last entry err_code = 0. */
611 };
612 
613 static int pm8001_set_nvmd(struct pm8001_hba_info *pm8001_ha)
614 {
615 	struct pm8001_ioctl_payload	*payload;
616 	DECLARE_COMPLETION_ONSTACK(completion);
617 	u8		*ioctlbuffer;
618 	u32		ret;
619 	u32		length = 1024 * 5 + sizeof(*payload) - 1;
620 
621 	if (pm8001_ha->fw_image->size > 4096) {
622 		pm8001_ha->fw_status = FAIL_FILE_SIZE;
623 		return -EFAULT;
624 	}
625 
626 	ioctlbuffer = kzalloc(length, GFP_KERNEL);
627 	if (!ioctlbuffer) {
628 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
629 		return -ENOMEM;
630 	}
631 	payload = (struct pm8001_ioctl_payload *)ioctlbuffer;
632 	memcpy((u8 *)&payload->func_specific, (u8 *)pm8001_ha->fw_image->data,
633 				pm8001_ha->fw_image->size);
634 	payload->length = pm8001_ha->fw_image->size;
635 	payload->id = 0;
636 	payload->minor_function = 0x1;
637 	pm8001_ha->nvmd_completion = &completion;
638 	ret = PM8001_CHIP_DISP->set_nvmd_req(pm8001_ha, payload);
639 	if (ret) {
640 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
641 		goto out;
642 	}
643 	wait_for_completion(&completion);
644 out:
645 	kfree(ioctlbuffer);
646 	return ret;
647 }
648 
649 static int pm8001_update_flash(struct pm8001_hba_info *pm8001_ha)
650 {
651 	struct pm8001_ioctl_payload	*payload;
652 	DECLARE_COMPLETION_ONSTACK(completion);
653 	u8		*ioctlbuffer;
654 	struct fw_control_info	*fwControl;
655 	u32		partitionSize, partitionSizeTmp;
656 	u32		loopNumber, loopcount;
657 	struct pm8001_fw_image_header *image_hdr;
658 	u32		sizeRead = 0;
659 	u32		ret = 0;
660 	u32		length = 1024 * 16 + sizeof(*payload) - 1;
661 
662 	if (pm8001_ha->fw_image->size < 28) {
663 		pm8001_ha->fw_status = FAIL_FILE_SIZE;
664 		return -EFAULT;
665 	}
666 	ioctlbuffer = kzalloc(length, GFP_KERNEL);
667 	if (!ioctlbuffer) {
668 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
669 		return -ENOMEM;
670 	}
671 	image_hdr = (struct pm8001_fw_image_header *)pm8001_ha->fw_image->data;
672 	while (sizeRead < pm8001_ha->fw_image->size) {
673 		partitionSizeTmp =
674 			*(u32 *)((u8 *)&image_hdr->image_length + sizeRead);
675 		partitionSize = be32_to_cpu(partitionSizeTmp);
676 		loopcount = DIV_ROUND_UP(partitionSize + HEADER_LEN,
677 					IOCTL_BUF_SIZE);
678 		for (loopNumber = 0; loopNumber < loopcount; loopNumber++) {
679 			payload = (struct pm8001_ioctl_payload *)ioctlbuffer;
680 			payload->length = 1024*16;
681 			payload->id = 0;
682 			fwControl =
683 			      (struct fw_control_info *)&payload->func_specific;
684 			fwControl->len = IOCTL_BUF_SIZE;   /* IN */
685 			fwControl->size = partitionSize + HEADER_LEN;/* IN */
686 			fwControl->retcode = 0;/* OUT */
687 			fwControl->offset = loopNumber * IOCTL_BUF_SIZE;/*OUT */
688 
689 		/* for the last chunk of data in case file size is not even with
690 		4k, load only the rest*/
691 		if (((loopcount-loopNumber) == 1) &&
692 			((partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE)) {
693 			fwControl->len =
694 				(partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE;
695 			memcpy((u8 *)fwControl->buffer,
696 				(u8 *)pm8001_ha->fw_image->data + sizeRead,
697 				(partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE);
698 			sizeRead +=
699 				(partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE;
700 		} else {
701 			memcpy((u8 *)fwControl->buffer,
702 				(u8 *)pm8001_ha->fw_image->data + sizeRead,
703 				IOCTL_BUF_SIZE);
704 			sizeRead += IOCTL_BUF_SIZE;
705 		}
706 
707 		pm8001_ha->nvmd_completion = &completion;
708 		ret = PM8001_CHIP_DISP->fw_flash_update_req(pm8001_ha, payload);
709 		if (ret) {
710 			pm8001_ha->fw_status = FAIL_OUT_MEMORY;
711 			goto out;
712 		}
713 		wait_for_completion(&completion);
714 		if (fwControl->retcode > FLASH_UPDATE_IN_PROGRESS) {
715 			pm8001_ha->fw_status = fwControl->retcode;
716 			ret = -EFAULT;
717 			goto out;
718 		}
719 		}
720 	}
721 out:
722 	kfree(ioctlbuffer);
723 	return ret;
724 }
725 static ssize_t pm8001_store_update_fw(struct device *cdev,
726 				      struct device_attribute *attr,
727 				      const char *buf, size_t count)
728 {
729 	struct Scsi_Host *shost = class_to_shost(cdev);
730 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
731 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
732 	char *cmd_ptr, *filename_ptr;
733 	int res, i;
734 	int flash_command = FLASH_CMD_NONE;
735 	int ret;
736 
737 	if (!capable(CAP_SYS_ADMIN))
738 		return -EACCES;
739 
740 	/* this test protects us from running two flash processes at once,
741 	 * so we should start with this test */
742 	if (pm8001_ha->fw_status == FLASH_IN_PROGRESS)
743 		return -EINPROGRESS;
744 	pm8001_ha->fw_status = FLASH_IN_PROGRESS;
745 
746 	cmd_ptr = kcalloc(count, 2, GFP_KERNEL);
747 	if (!cmd_ptr) {
748 		pm8001_ha->fw_status = FAIL_OUT_MEMORY;
749 		return -ENOMEM;
750 	}
751 
752 	filename_ptr = cmd_ptr + count;
753 	res = sscanf(buf, "%s %s", cmd_ptr, filename_ptr);
754 	if (res != 2) {
755 		pm8001_ha->fw_status = FAIL_PARAMETERS;
756 		ret = -EINVAL;
757 		goto out;
758 	}
759 
760 	for (i = 0; flash_command_table[i].code != FLASH_CMD_NONE; i++) {
761 		if (!memcmp(flash_command_table[i].command,
762 				 cmd_ptr, strlen(cmd_ptr))) {
763 			flash_command = flash_command_table[i].code;
764 			break;
765 		}
766 	}
767 	if (flash_command == FLASH_CMD_NONE) {
768 		pm8001_ha->fw_status = FAIL_PARAMETERS;
769 		ret = -EINVAL;
770 		goto out;
771 	}
772 
773 	ret = request_firmware(&pm8001_ha->fw_image,
774 			       filename_ptr,
775 			       pm8001_ha->dev);
776 
777 	if (ret) {
778 		PM8001_FAIL_DBG(pm8001_ha,
779 			pm8001_printk(
780 			"Failed to load firmware image file %s,	error %d\n",
781 			filename_ptr, ret));
782 		pm8001_ha->fw_status = FAIL_OPEN_BIOS_FILE;
783 		goto out;
784 	}
785 
786 	if (FLASH_CMD_UPDATE == flash_command)
787 		ret = pm8001_update_flash(pm8001_ha);
788 	else
789 		ret = pm8001_set_nvmd(pm8001_ha);
790 
791 	release_firmware(pm8001_ha->fw_image);
792 out:
793 	kfree(cmd_ptr);
794 
795 	if (ret)
796 		return ret;
797 
798 	pm8001_ha->fw_status = FLASH_OK;
799 	return count;
800 }
801 
802 static ssize_t pm8001_show_update_fw(struct device *cdev,
803 				     struct device_attribute *attr, char *buf)
804 {
805 	int i;
806 	struct Scsi_Host *shost = class_to_shost(cdev);
807 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
808 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
809 
810 	for (i = 0; flash_error_table[i].err_code != 0; i++) {
811 		if (flash_error_table[i].err_code == pm8001_ha->fw_status)
812 			break;
813 	}
814 	if (pm8001_ha->fw_status != FLASH_IN_PROGRESS)
815 		pm8001_ha->fw_status = FLASH_OK;
816 
817 	return snprintf(buf, PAGE_SIZE, "status=%x %s\n",
818 			flash_error_table[i].err_code,
819 			flash_error_table[i].reason);
820 }
821 
822 static DEVICE_ATTR(update_fw, S_IRUGO|S_IWUSR|S_IWGRP,
823 	pm8001_show_update_fw, pm8001_store_update_fw);
824 struct device_attribute *pm8001_host_attrs[] = {
825 	&dev_attr_interface_rev,
826 	&dev_attr_controller_fatal_error,
827 	&dev_attr_fw_version,
828 	&dev_attr_update_fw,
829 	&dev_attr_aap_log,
830 	&dev_attr_iop_log,
831 	&dev_attr_fatal_log,
832 	&dev_attr_gsm_log,
833 	&dev_attr_max_out_io,
834 	&dev_attr_max_devices,
835 	&dev_attr_max_sg_list,
836 	&dev_attr_sas_spec_support,
837 	&dev_attr_logging_level,
838 	&dev_attr_event_log_size,
839 	&dev_attr_host_sas_address,
840 	&dev_attr_bios_version,
841 	&dev_attr_ib_log,
842 	&dev_attr_ob_log,
843 	&dev_attr_ila_version,
844 	&dev_attr_inc_fw_ver,
845 	NULL,
846 };
847 
848