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