PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / scsi / pm8001 / pm8001_ctl.c
bloba04b4ff8c7f627193c6829e2386c7c8e167632ee
1 /*
2 * PMC-Sierra 8001/8081/8088/8089 SAS/SATA based host adapters driver
4 * Copyright (c) 2008-2009 USI Co., Ltd.
5 * All rights reserved.
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.
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.
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.
40 #include <linux/firmware.h>
41 #include <linux/slab.h>
42 #include "pm8001_sas.h"
43 #include "pm8001_ctl.h"
45 /* scsi host attributes */
47 /**
48 * pm8001_ctl_mpi_interface_rev_show - MPI interface revision number
49 * @cdev: pointer to embedded class device
50 * @buf: the buffer returned
52 * A sysfs 'read-only' shost attribute.
54 static ssize_t pm8001_ctl_mpi_interface_rev_show(struct device *cdev,
55 struct device_attribute *attr, char *buf)
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;
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);
69 static
70 DEVICE_ATTR(interface_rev, S_IRUGO, pm8001_ctl_mpi_interface_rev_show, NULL);
72 /**
73 * pm8001_ctl_fw_version_show - firmware version
74 * @cdev: pointer to embedded class device
75 * @buf: the buffer returned
77 * A sysfs 'read-only' shost attribute.
79 static ssize_t pm8001_ctl_fw_version_show(struct device *cdev,
80 struct device_attribute *attr, char *buf)
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;
86 if (pm8001_ha->chip_id == chip_8001) {
87 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
88 (u8)(pm8001_ha->main_cfg_tbl.pm8001_tbl.firmware_rev >> 24),
89 (u8)(pm8001_ha->main_cfg_tbl.pm8001_tbl.firmware_rev >> 16),
90 (u8)(pm8001_ha->main_cfg_tbl.pm8001_tbl.firmware_rev >> 8),
91 (u8)(pm8001_ha->main_cfg_tbl.pm8001_tbl.firmware_rev));
92 } else {
93 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
94 (u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev >> 24),
95 (u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev >> 16),
96 (u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev >> 8),
97 (u8)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev));
100 static DEVICE_ATTR(fw_version, S_IRUGO, pm8001_ctl_fw_version_show, NULL);
102 * pm8001_ctl_max_out_io_show - max outstanding io supported
103 * @cdev: pointer to embedded class device
104 * @buf: the buffer returned
106 * A sysfs 'read-only' shost attribute.
108 static ssize_t pm8001_ctl_max_out_io_show(struct device *cdev,
109 struct device_attribute *attr, char *buf)
111 struct Scsi_Host *shost = class_to_shost(cdev);
112 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
113 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
115 if (pm8001_ha->chip_id == chip_8001) {
116 return snprintf(buf, PAGE_SIZE, "%d\n",
117 pm8001_ha->main_cfg_tbl.pm8001_tbl.max_out_io);
118 } else {
119 return snprintf(buf, PAGE_SIZE, "%d\n",
120 pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_out_io);
123 static DEVICE_ATTR(max_out_io, S_IRUGO, pm8001_ctl_max_out_io_show, NULL);
125 * pm8001_ctl_max_devices_show - max devices support
126 * @cdev: pointer to embedded class device
127 * @buf: the buffer returned
129 * A sysfs 'read-only' shost attribute.
131 static ssize_t pm8001_ctl_max_devices_show(struct device *cdev,
132 struct device_attribute *attr, char *buf)
134 struct Scsi_Host *shost = class_to_shost(cdev);
135 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
136 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
138 if (pm8001_ha->chip_id == chip_8001) {
139 return snprintf(buf, PAGE_SIZE, "%04d\n",
140 (u16)(pm8001_ha->main_cfg_tbl.pm8001_tbl.max_sgl >> 16)
142 } else {
143 return snprintf(buf, PAGE_SIZE, "%04d\n",
144 (u16)(pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_sgl >> 16)
148 static DEVICE_ATTR(max_devices, S_IRUGO, pm8001_ctl_max_devices_show, NULL);
150 * pm8001_ctl_max_sg_list_show - max sg list supported iff not 0.0 for no
151 * hardware limitation
152 * @cdev: pointer to embedded class device
153 * @buf: the buffer returned
155 * A sysfs 'read-only' shost attribute.
157 static ssize_t pm8001_ctl_max_sg_list_show(struct device *cdev,
158 struct device_attribute *attr, char *buf)
160 struct Scsi_Host *shost = class_to_shost(cdev);
161 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
162 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
164 if (pm8001_ha->chip_id == chip_8001) {
165 return snprintf(buf, PAGE_SIZE, "%04d\n",
166 pm8001_ha->main_cfg_tbl.pm8001_tbl.max_sgl & 0x0000FFFF
168 } else {
169 return snprintf(buf, PAGE_SIZE, "%04d\n",
170 pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_sgl & 0x0000FFFF
174 static DEVICE_ATTR(max_sg_list, S_IRUGO, pm8001_ctl_max_sg_list_show, NULL);
176 #define SAS_1_0 0x1
177 #define SAS_1_1 0x2
178 #define SAS_2_0 0x4
180 static ssize_t
181 show_sas_spec_support_status(unsigned int mode, char *buf)
183 ssize_t len = 0;
185 if (mode & SAS_1_1)
186 len = sprintf(buf, "%s", "SAS1.1");
187 if (mode & SAS_2_0)
188 len += sprintf(buf + len, "%s%s", len ? ", " : "", "SAS2.0");
189 len += sprintf(buf + len, "\n");
191 return len;
195 * pm8001_ctl_sas_spec_support_show - sas spec supported
196 * @cdev: pointer to embedded class device
197 * @buf: the buffer returned
199 * A sysfs 'read-only' shost attribute.
201 static ssize_t pm8001_ctl_sas_spec_support_show(struct device *cdev,
202 struct device_attribute *attr, char *buf)
204 unsigned int mode;
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 /* fe000000 means supports SAS2.1 */
209 if (pm8001_ha->chip_id == chip_8001)
210 mode = (pm8001_ha->main_cfg_tbl.pm8001_tbl.ctrl_cap_flag &
211 0xfe000000)>>25;
212 else
213 /* fe000000 means supports SAS2.1 */
214 mode = (pm8001_ha->main_cfg_tbl.pm80xx_tbl.ctrl_cap_flag &
215 0xfe000000)>>25;
216 return show_sas_spec_support_status(mode, buf);
218 static DEVICE_ATTR(sas_spec_support, S_IRUGO,
219 pm8001_ctl_sas_spec_support_show, NULL);
222 * pm8001_ctl_sas_address_show - sas address
223 * @cdev: pointer to embedded class device
224 * @buf: the buffer returned
226 * This is the controller sas address
228 * A sysfs 'read-only' shost attribute.
230 static ssize_t pm8001_ctl_host_sas_address_show(struct device *cdev,
231 struct device_attribute *attr, char *buf)
233 struct Scsi_Host *shost = class_to_shost(cdev);
234 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
235 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
236 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
237 be64_to_cpu(*(__be64 *)pm8001_ha->sas_addr));
239 static DEVICE_ATTR(host_sas_address, S_IRUGO,
240 pm8001_ctl_host_sas_address_show, NULL);
243 * pm8001_ctl_logging_level_show - logging level
244 * @cdev: pointer to embedded class device
245 * @buf: the buffer returned
247 * A sysfs 'read/write' shost attribute.
249 static ssize_t pm8001_ctl_logging_level_show(struct device *cdev,
250 struct device_attribute *attr, char *buf)
252 struct Scsi_Host *shost = class_to_shost(cdev);
253 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
254 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
256 return snprintf(buf, PAGE_SIZE, "%08xh\n", pm8001_ha->logging_level);
258 static ssize_t pm8001_ctl_logging_level_store(struct device *cdev,
259 struct device_attribute *attr, const char *buf, size_t count)
261 struct Scsi_Host *shost = class_to_shost(cdev);
262 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
263 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
264 int val = 0;
266 if (sscanf(buf, "%x", &val) != 1)
267 return -EINVAL;
269 pm8001_ha->logging_level = val;
270 return strlen(buf);
273 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
274 pm8001_ctl_logging_level_show, pm8001_ctl_logging_level_store);
276 * pm8001_ctl_aap_log_show - aap1 event log
277 * @cdev: pointer to embedded class device
278 * @buf: the buffer returned
280 * A sysfs 'read-only' shost attribute.
282 static ssize_t pm8001_ctl_aap_log_show(struct device *cdev,
283 struct device_attribute *attr, char *buf)
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 int i;
289 #define AAP1_MEMMAP(r, c) \
290 (*(u32 *)((u8*)pm8001_ha->memoryMap.region[AAP1].virt_ptr + (r) * 32 \
291 + (c)))
293 char *str = buf;
294 int max = 2;
295 for (i = 0; i < max; i++) {
296 str += sprintf(str, "0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x"
297 "0x%08x 0x%08x\n",
298 AAP1_MEMMAP(i, 0),
299 AAP1_MEMMAP(i, 4),
300 AAP1_MEMMAP(i, 8),
301 AAP1_MEMMAP(i, 12),
302 AAP1_MEMMAP(i, 16),
303 AAP1_MEMMAP(i, 20),
304 AAP1_MEMMAP(i, 24),
305 AAP1_MEMMAP(i, 28));
308 return str - buf;
310 static DEVICE_ATTR(aap_log, S_IRUGO, pm8001_ctl_aap_log_show, NULL);
312 * pm8001_ctl_ib_queue_log_show - Out bound Queue log
313 * @cdev:pointer to embedded class device
314 * @buf: the buffer returned
315 * A sysfs 'read-only' shost attribute.
317 static ssize_t pm8001_ctl_ib_queue_log_show(struct device *cdev,
318 struct device_attribute *attr, char *buf)
320 struct Scsi_Host *shost = class_to_shost(cdev);
321 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
322 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
323 int offset;
324 char *str = buf;
325 int start = 0;
326 #define IB_MEMMAP(c) \
327 (*(u32 *)((u8 *)pm8001_ha-> \
328 memoryMap.region[IB].virt_ptr + \
329 pm8001_ha->evtlog_ib_offset + (c)))
331 for (offset = 0; offset < IB_OB_READ_TIMES; offset++) {
332 if (pm8001_ha->chip_id != chip_8001)
333 str += sprintf(str, "0x%08x\n", IB_MEMMAP(start));
334 else
335 str += sprintf(str, "0x%08x\n", IB_MEMMAP(start));
336 start = start + 4;
338 pm8001_ha->evtlog_ib_offset += SYSFS_OFFSET;
339 if ((((pm8001_ha->evtlog_ib_offset) % (PM80XX_IB_OB_QUEUE_SIZE)) == 0)
340 && (pm8001_ha->chip_id != chip_8001))
341 pm8001_ha->evtlog_ib_offset = 0;
342 if ((((pm8001_ha->evtlog_ib_offset) % (PM8001_IB_OB_QUEUE_SIZE)) == 0)
343 && (pm8001_ha->chip_id == chip_8001))
344 pm8001_ha->evtlog_ib_offset = 0;
346 return str - buf;
349 static DEVICE_ATTR(ib_log, S_IRUGO, pm8001_ctl_ib_queue_log_show, NULL);
351 * pm8001_ctl_ob_queue_log_show - Out bound Queue log
352 * @cdev:pointer to embedded class device
353 * @buf: the buffer returned
354 * A sysfs 'read-only' shost attribute.
357 static ssize_t pm8001_ctl_ob_queue_log_show(struct device *cdev,
358 struct device_attribute *attr, char *buf)
360 struct Scsi_Host *shost = class_to_shost(cdev);
361 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
362 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
363 int offset;
364 char *str = buf;
365 int start = 0;
366 #define OB_MEMMAP(c) \
367 (*(u32 *)((u8 *)pm8001_ha-> \
368 memoryMap.region[OB].virt_ptr + \
369 pm8001_ha->evtlog_ob_offset + (c)))
371 for (offset = 0; offset < IB_OB_READ_TIMES; offset++) {
372 if (pm8001_ha->chip_id != chip_8001)
373 str += sprintf(str, "0x%08x\n", OB_MEMMAP(start));
374 else
375 str += sprintf(str, "0x%08x\n", OB_MEMMAP(start));
376 start = start + 4;
378 pm8001_ha->evtlog_ob_offset += SYSFS_OFFSET;
379 if ((((pm8001_ha->evtlog_ob_offset) % (PM80XX_IB_OB_QUEUE_SIZE)) == 0)
380 && (pm8001_ha->chip_id != chip_8001))
381 pm8001_ha->evtlog_ob_offset = 0;
382 if ((((pm8001_ha->evtlog_ob_offset) % (PM8001_IB_OB_QUEUE_SIZE)) == 0)
383 && (pm8001_ha->chip_id == chip_8001))
384 pm8001_ha->evtlog_ob_offset = 0;
386 return str - buf;
388 static DEVICE_ATTR(ob_log, S_IRUGO, pm8001_ctl_ob_queue_log_show, NULL);
390 * pm8001_ctl_bios_version_show - Bios version Display
391 * @cdev:pointer to embedded class device
392 * @buf:the buffer returned
393 * A sysfs 'read-only' shost attribute.
395 static ssize_t pm8001_ctl_bios_version_show(struct device *cdev,
396 struct device_attribute *attr, char *buf)
398 struct Scsi_Host *shost = class_to_shost(cdev);
399 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
400 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
401 char *str = buf;
402 void *virt_addr;
403 int bios_index;
404 DECLARE_COMPLETION_ONSTACK(completion);
405 struct pm8001_ioctl_payload payload;
407 pm8001_ha->nvmd_completion = &completion;
408 payload.minor_function = 7;
409 payload.offset = 0;
410 payload.length = 4096;
411 payload.func_specific = kzalloc(4096, GFP_KERNEL);
412 PM8001_CHIP_DISP->get_nvmd_req(pm8001_ha, &payload);
413 wait_for_completion(&completion);
414 virt_addr = pm8001_ha->memoryMap.region[NVMD].virt_ptr;
415 for (bios_index = BIOSOFFSET; bios_index < BIOS_OFFSET_LIMIT;
416 bios_index++)
417 str += sprintf(str, "%c",
418 *((u8 *)((u8 *)virt_addr+bios_index)));
419 return str - buf;
421 static DEVICE_ATTR(bios_version, S_IRUGO, pm8001_ctl_bios_version_show, NULL);
423 * pm8001_ctl_aap_log_show - IOP event log
424 * @cdev: pointer to embedded class device
425 * @buf: the buffer returned
427 * A sysfs 'read-only' shost attribute.
429 static ssize_t pm8001_ctl_iop_log_show(struct device *cdev,
430 struct device_attribute *attr, char *buf)
432 struct Scsi_Host *shost = class_to_shost(cdev);
433 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
434 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
435 #define IOP_MEMMAP(r, c) \
436 (*(u32 *)((u8*)pm8001_ha->memoryMap.region[IOP].virt_ptr + (r) * 32 \
437 + (c)))
438 int i;
439 char *str = buf;
440 int max = 2;
441 for (i = 0; i < max; i++) {
442 str += sprintf(str, "0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x"
443 "0x%08x 0x%08x\n",
444 IOP_MEMMAP(i, 0),
445 IOP_MEMMAP(i, 4),
446 IOP_MEMMAP(i, 8),
447 IOP_MEMMAP(i, 12),
448 IOP_MEMMAP(i, 16),
449 IOP_MEMMAP(i, 20),
450 IOP_MEMMAP(i, 24),
451 IOP_MEMMAP(i, 28));
454 return str - buf;
456 static DEVICE_ATTR(iop_log, S_IRUGO, pm8001_ctl_iop_log_show, NULL);
459 ** pm8001_ctl_fatal_log_show - fatal error logging
460 ** @cdev:pointer to embedded class device
461 ** @buf: the buffer returned
463 ** A sysfs 'read-only' shost attribute.
466 static ssize_t pm8001_ctl_fatal_log_show(struct device *cdev,
467 struct device_attribute *attr, char *buf)
469 u32 count;
471 count = pm80xx_get_fatal_dump(cdev, attr, buf);
472 return count;
475 static DEVICE_ATTR(fatal_log, S_IRUGO, pm8001_ctl_fatal_log_show, NULL);
479 ** pm8001_ctl_gsm_log_show - gsm dump collection
480 ** @cdev:pointer to embedded class device
481 ** @buf: the buffer returned
482 **A sysfs 'read-only' shost attribute.
484 static ssize_t pm8001_ctl_gsm_log_show(struct device *cdev,
485 struct device_attribute *attr, char *buf)
487 u32 count;
489 count = pm8001_get_gsm_dump(cdev, SYSFS_OFFSET, buf);
490 return count;
493 static DEVICE_ATTR(gsm_log, S_IRUGO, pm8001_ctl_gsm_log_show, NULL);
495 #define FLASH_CMD_NONE 0x00
496 #define FLASH_CMD_UPDATE 0x01
497 #define FLASH_CMD_SET_NVMD 0x02
499 struct flash_command {
500 u8 command[8];
501 int code;
504 static struct flash_command flash_command_table[] =
506 {"set_nvmd", FLASH_CMD_SET_NVMD},
507 {"update", FLASH_CMD_UPDATE},
508 {"", FLASH_CMD_NONE} /* Last entry should be NULL. */
511 struct error_fw {
512 char *reason;
513 int err_code;
516 static struct error_fw flash_error_table[] =
518 {"Failed to open fw image file", FAIL_OPEN_BIOS_FILE},
519 {"image header mismatch", FLASH_UPDATE_HDR_ERR},
520 {"image offset mismatch", FLASH_UPDATE_OFFSET_ERR},
521 {"image CRC Error", FLASH_UPDATE_CRC_ERR},
522 {"image length Error.", FLASH_UPDATE_LENGTH_ERR},
523 {"Failed to program flash chip", FLASH_UPDATE_HW_ERR},
524 {"Flash chip not supported.", FLASH_UPDATE_DNLD_NOT_SUPPORTED},
525 {"Flash update disabled.", FLASH_UPDATE_DISABLED},
526 {"Flash in progress", FLASH_IN_PROGRESS},
527 {"Image file size Error", FAIL_FILE_SIZE},
528 {"Input parameter error", FAIL_PARAMETERS},
529 {"Out of memory", FAIL_OUT_MEMORY},
530 {"OK", 0} /* Last entry err_code = 0. */
533 static int pm8001_set_nvmd(struct pm8001_hba_info *pm8001_ha)
535 struct pm8001_ioctl_payload *payload;
536 DECLARE_COMPLETION_ONSTACK(completion);
537 u8 *ioctlbuffer = NULL;
538 u32 length = 0;
539 u32 ret = 0;
541 length = 1024 * 5 + sizeof(*payload) - 1;
542 ioctlbuffer = kzalloc(length, GFP_KERNEL);
543 if (!ioctlbuffer)
544 return -ENOMEM;
545 if ((pm8001_ha->fw_image->size <= 0) ||
546 (pm8001_ha->fw_image->size > 4096)) {
547 ret = FAIL_FILE_SIZE;
548 goto out;
550 payload = (struct pm8001_ioctl_payload *)ioctlbuffer;
551 memcpy((u8 *)&payload->func_specific, (u8 *)pm8001_ha->fw_image->data,
552 pm8001_ha->fw_image->size);
553 payload->length = pm8001_ha->fw_image->size;
554 payload->id = 0;
555 payload->minor_function = 0x1;
556 pm8001_ha->nvmd_completion = &completion;
557 ret = PM8001_CHIP_DISP->set_nvmd_req(pm8001_ha, payload);
558 wait_for_completion(&completion);
559 out:
560 kfree(ioctlbuffer);
561 return ret;
564 static int pm8001_update_flash(struct pm8001_hba_info *pm8001_ha)
566 struct pm8001_ioctl_payload *payload;
567 DECLARE_COMPLETION_ONSTACK(completion);
568 u8 *ioctlbuffer = NULL;
569 u32 length = 0;
570 struct fw_control_info *fwControl;
571 u32 loopNumber, loopcount = 0;
572 u32 sizeRead = 0;
573 u32 partitionSize, partitionSizeTmp;
574 u32 ret = 0;
575 u32 partitionNumber = 0;
576 struct pm8001_fw_image_header *image_hdr;
578 length = 1024 * 16 + sizeof(*payload) - 1;
579 ioctlbuffer = kzalloc(length, GFP_KERNEL);
580 image_hdr = (struct pm8001_fw_image_header *)pm8001_ha->fw_image->data;
581 if (!ioctlbuffer)
582 return -ENOMEM;
583 if (pm8001_ha->fw_image->size < 28) {
584 ret = FAIL_FILE_SIZE;
585 goto out;
588 while (sizeRead < pm8001_ha->fw_image->size) {
589 partitionSizeTmp =
590 *(u32 *)((u8 *)&image_hdr->image_length + sizeRead);
591 partitionSize = be32_to_cpu(partitionSizeTmp);
592 loopcount = (partitionSize + HEADER_LEN)/IOCTL_BUF_SIZE;
593 if (loopcount % IOCTL_BUF_SIZE)
594 loopcount++;
595 if (loopcount == 0)
596 loopcount++;
597 for (loopNumber = 0; loopNumber < loopcount; loopNumber++) {
598 payload = (struct pm8001_ioctl_payload *)ioctlbuffer;
599 payload->length = 1024*16;
600 payload->id = 0;
601 fwControl =
602 (struct fw_control_info *)&payload->func_specific;
603 fwControl->len = IOCTL_BUF_SIZE; /* IN */
604 fwControl->size = partitionSize + HEADER_LEN;/* IN */
605 fwControl->retcode = 0;/* OUT */
606 fwControl->offset = loopNumber * IOCTL_BUF_SIZE;/*OUT */
608 /* for the last chunk of data in case file size is not even with
609 4k, load only the rest*/
610 if (((loopcount-loopNumber) == 1) &&
611 ((partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE)) {
612 fwControl->len =
613 (partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE;
614 memcpy((u8 *)fwControl->buffer,
615 (u8 *)pm8001_ha->fw_image->data + sizeRead,
616 (partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE);
617 sizeRead +=
618 (partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE;
619 } else {
620 memcpy((u8 *)fwControl->buffer,
621 (u8 *)pm8001_ha->fw_image->data + sizeRead,
622 IOCTL_BUF_SIZE);
623 sizeRead += IOCTL_BUF_SIZE;
626 pm8001_ha->nvmd_completion = &completion;
627 ret = PM8001_CHIP_DISP->fw_flash_update_req(pm8001_ha, payload);
628 wait_for_completion(&completion);
629 if (ret || (fwControl->retcode > FLASH_UPDATE_IN_PROGRESS)) {
630 ret = fwControl->retcode;
631 kfree(ioctlbuffer);
632 ioctlbuffer = NULL;
633 break;
636 if (ret)
637 break;
638 partitionNumber++;
640 out:
641 kfree(ioctlbuffer);
642 return ret;
644 static ssize_t pm8001_store_update_fw(struct device *cdev,
645 struct device_attribute *attr,
646 const char *buf, size_t count)
648 struct Scsi_Host *shost = class_to_shost(cdev);
649 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
650 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
651 char *cmd_ptr, *filename_ptr;
652 int res, i;
653 int flash_command = FLASH_CMD_NONE;
654 int err = 0;
655 if (!capable(CAP_SYS_ADMIN))
656 return -EACCES;
658 cmd_ptr = kzalloc(count*2, GFP_KERNEL);
660 if (!cmd_ptr) {
661 err = FAIL_OUT_MEMORY;
662 goto out;
665 filename_ptr = cmd_ptr + count;
666 res = sscanf(buf, "%s %s", cmd_ptr, filename_ptr);
667 if (res != 2) {
668 err = FAIL_PARAMETERS;
669 goto out1;
672 for (i = 0; flash_command_table[i].code != FLASH_CMD_NONE; i++) {
673 if (!memcmp(flash_command_table[i].command,
674 cmd_ptr, strlen(cmd_ptr))) {
675 flash_command = flash_command_table[i].code;
676 break;
679 if (flash_command == FLASH_CMD_NONE) {
680 err = FAIL_PARAMETERS;
681 goto out1;
684 if (pm8001_ha->fw_status == FLASH_IN_PROGRESS) {
685 err = FLASH_IN_PROGRESS;
686 goto out1;
688 err = request_firmware(&pm8001_ha->fw_image,
689 filename_ptr,
690 pm8001_ha->dev);
692 if (err) {
693 PM8001_FAIL_DBG(pm8001_ha,
694 pm8001_printk("Failed to load firmware image file %s,"
695 " error %d\n", filename_ptr, err));
696 err = FAIL_OPEN_BIOS_FILE;
697 goto out1;
700 switch (flash_command) {
701 case FLASH_CMD_UPDATE:
702 pm8001_ha->fw_status = FLASH_IN_PROGRESS;
703 err = pm8001_update_flash(pm8001_ha);
704 break;
705 case FLASH_CMD_SET_NVMD:
706 pm8001_ha->fw_status = FLASH_IN_PROGRESS;
707 err = pm8001_set_nvmd(pm8001_ha);
708 break;
709 default:
710 pm8001_ha->fw_status = FAIL_PARAMETERS;
711 err = FAIL_PARAMETERS;
712 break;
714 release_firmware(pm8001_ha->fw_image);
715 out1:
716 kfree(cmd_ptr);
717 out:
718 pm8001_ha->fw_status = err;
720 if (!err)
721 return count;
722 else
723 return -err;
726 static ssize_t pm8001_show_update_fw(struct device *cdev,
727 struct device_attribute *attr, char *buf)
729 int i;
730 struct Scsi_Host *shost = class_to_shost(cdev);
731 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
732 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
734 for (i = 0; flash_error_table[i].err_code != 0; i++) {
735 if (flash_error_table[i].err_code == pm8001_ha->fw_status)
736 break;
738 if (pm8001_ha->fw_status != FLASH_IN_PROGRESS)
739 pm8001_ha->fw_status = FLASH_OK;
741 return snprintf(buf, PAGE_SIZE, "status=%x %s\n",
742 flash_error_table[i].err_code,
743 flash_error_table[i].reason);
746 static DEVICE_ATTR(update_fw, S_IRUGO|S_IWUGO,
747 pm8001_show_update_fw, pm8001_store_update_fw);
748 struct device_attribute *pm8001_host_attrs[] = {
749 &dev_attr_interface_rev,
750 &dev_attr_fw_version,
751 &dev_attr_update_fw,
752 &dev_attr_aap_log,
753 &dev_attr_iop_log,
754 &dev_attr_fatal_log,
755 &dev_attr_gsm_log,
756 &dev_attr_max_out_io,
757 &dev_attr_max_devices,
758 &dev_attr_max_sg_list,
759 &dev_attr_sas_spec_support,
760 &dev_attr_logging_level,
761 &dev_attr_host_sas_address,
762 &dev_attr_bios_version,
763 &dev_attr_ib_log,
764 &dev_attr_ob_log,
765 NULL,