nilfs2: unbreak compat ioctl
[zen-stable.git] / drivers / scsi / pm8001 / pm8001_ctl.c
blob45bc197bc22f2a45c46f5176cabefddc69e077f2
1 /*
2 * PMC-Sierra SPC 8001 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 return snprintf(buf, PAGE_SIZE, "%d\n",
62 pm8001_ha->main_cfg_tbl.interface_rev);
64 static
65 DEVICE_ATTR(interface_rev, S_IRUGO, pm8001_ctl_mpi_interface_rev_show, NULL);
67 /**
68 * pm8001_ctl_fw_version_show - firmware version
69 * @cdev: pointer to embedded class device
70 * @buf: the buffer returned
72 * A sysfs 'read-only' shost attribute.
74 static ssize_t pm8001_ctl_fw_version_show(struct device *cdev,
75 struct device_attribute *attr, char *buf)
77 struct Scsi_Host *shost = class_to_shost(cdev);
78 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
79 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
81 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
82 (u8)(pm8001_ha->main_cfg_tbl.firmware_rev >> 24),
83 (u8)(pm8001_ha->main_cfg_tbl.firmware_rev >> 16),
84 (u8)(pm8001_ha->main_cfg_tbl.firmware_rev >> 8),
85 (u8)(pm8001_ha->main_cfg_tbl.firmware_rev));
87 static DEVICE_ATTR(fw_version, S_IRUGO, pm8001_ctl_fw_version_show, NULL);
88 /**
89 * pm8001_ctl_max_out_io_show - max outstanding io supported
90 * @cdev: pointer to embedded class device
91 * @buf: the buffer returned
93 * A sysfs 'read-only' shost attribute.
95 static ssize_t pm8001_ctl_max_out_io_show(struct device *cdev,
96 struct device_attribute *attr, char *buf)
98 struct Scsi_Host *shost = class_to_shost(cdev);
99 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
100 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
102 return snprintf(buf, PAGE_SIZE, "%d\n",
103 pm8001_ha->main_cfg_tbl.max_out_io);
105 static DEVICE_ATTR(max_out_io, S_IRUGO, pm8001_ctl_max_out_io_show, NULL);
107 * pm8001_ctl_max_devices_show - max devices support
108 * @cdev: pointer to embedded class device
109 * @buf: the buffer returned
111 * A sysfs 'read-only' shost attribute.
113 static ssize_t pm8001_ctl_max_devices_show(struct device *cdev,
114 struct device_attribute *attr, char *buf)
116 struct Scsi_Host *shost = class_to_shost(cdev);
117 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
118 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
120 return snprintf(buf, PAGE_SIZE, "%04d\n",
121 (u16)(pm8001_ha->main_cfg_tbl.max_sgl >> 16));
123 static DEVICE_ATTR(max_devices, S_IRUGO, pm8001_ctl_max_devices_show, NULL);
125 * pm8001_ctl_max_sg_list_show - max sg list supported iff not 0.0 for no
126 * hardware limitation
127 * @cdev: pointer to embedded class device
128 * @buf: the buffer returned
130 * A sysfs 'read-only' shost attribute.
132 static ssize_t pm8001_ctl_max_sg_list_show(struct device *cdev,
133 struct device_attribute *attr, char *buf)
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;
139 return snprintf(buf, PAGE_SIZE, "%04d\n",
140 pm8001_ha->main_cfg_tbl.max_sgl & 0x0000FFFF);
142 static DEVICE_ATTR(max_sg_list, S_IRUGO, pm8001_ctl_max_sg_list_show, NULL);
144 #define SAS_1_0 0x1
145 #define SAS_1_1 0x2
146 #define SAS_2_0 0x4
148 static ssize_t
149 show_sas_spec_support_status(unsigned int mode, char *buf)
151 ssize_t len = 0;
153 if (mode & SAS_1_1)
154 len = sprintf(buf, "%s", "SAS1.1");
155 if (mode & SAS_2_0)
156 len += sprintf(buf + len, "%s%s", len ? ", " : "", "SAS2.0");
157 len += sprintf(buf + len, "\n");
159 return len;
163 * pm8001_ctl_sas_spec_support_show - sas spec supported
164 * @cdev: pointer to embedded class device
165 * @buf: the buffer returned
167 * A sysfs 'read-only' shost attribute.
169 static ssize_t pm8001_ctl_sas_spec_support_show(struct device *cdev,
170 struct device_attribute *attr, char *buf)
172 unsigned int mode;
173 struct Scsi_Host *shost = class_to_shost(cdev);
174 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
175 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
176 mode = (pm8001_ha->main_cfg_tbl.ctrl_cap_flag & 0xfe000000)>>25;
177 return show_sas_spec_support_status(mode, buf);
179 static DEVICE_ATTR(sas_spec_support, S_IRUGO,
180 pm8001_ctl_sas_spec_support_show, NULL);
183 * pm8001_ctl_sas_address_show - sas address
184 * @cdev: pointer to embedded class device
185 * @buf: the buffer returned
187 * This is the controller sas address
189 * A sysfs 'read-only' shost attribute.
191 static ssize_t pm8001_ctl_host_sas_address_show(struct device *cdev,
192 struct device_attribute *attr, char *buf)
194 struct Scsi_Host *shost = class_to_shost(cdev);
195 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
196 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
197 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
198 be64_to_cpu(*(__be64 *)pm8001_ha->sas_addr));
200 static DEVICE_ATTR(host_sas_address, S_IRUGO,
201 pm8001_ctl_host_sas_address_show, NULL);
204 * pm8001_ctl_logging_level_show - logging level
205 * @cdev: pointer to embedded class device
206 * @buf: the buffer returned
208 * A sysfs 'read/write' shost attribute.
210 static ssize_t pm8001_ctl_logging_level_show(struct device *cdev,
211 struct device_attribute *attr, char *buf)
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;
217 return snprintf(buf, PAGE_SIZE, "%08xh\n", pm8001_ha->logging_level);
219 static ssize_t pm8001_ctl_logging_level_store(struct device *cdev,
220 struct device_attribute *attr, const char *buf, size_t count)
222 struct Scsi_Host *shost = class_to_shost(cdev);
223 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
224 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
225 int val = 0;
227 if (sscanf(buf, "%x", &val) != 1)
228 return -EINVAL;
230 pm8001_ha->logging_level = val;
231 return strlen(buf);
234 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
235 pm8001_ctl_logging_level_show, pm8001_ctl_logging_level_store);
237 * pm8001_ctl_aap_log_show - aap1 event log
238 * @cdev: pointer to embedded class device
239 * @buf: the buffer returned
241 * A sysfs 'read-only' shost attribute.
243 static ssize_t pm8001_ctl_aap_log_show(struct device *cdev,
244 struct device_attribute *attr, char *buf)
246 struct Scsi_Host *shost = class_to_shost(cdev);
247 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
248 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
249 int i;
250 #define AAP1_MEMMAP(r, c) \
251 (*(u32 *)((u8*)pm8001_ha->memoryMap.region[AAP1].virt_ptr + (r) * 32 \
252 + (c)))
254 char *str = buf;
255 int max = 2;
256 for (i = 0; i < max; i++) {
257 str += sprintf(str, "0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x"
258 "0x%08x 0x%08x\n",
259 AAP1_MEMMAP(i, 0),
260 AAP1_MEMMAP(i, 4),
261 AAP1_MEMMAP(i, 8),
262 AAP1_MEMMAP(i, 12),
263 AAP1_MEMMAP(i, 16),
264 AAP1_MEMMAP(i, 20),
265 AAP1_MEMMAP(i, 24),
266 AAP1_MEMMAP(i, 28));
269 return str - buf;
271 static DEVICE_ATTR(aap_log, S_IRUGO, pm8001_ctl_aap_log_show, NULL);
273 * pm8001_ctl_aap_log_show - IOP event log
274 * @cdev: pointer to embedded class device
275 * @buf: the buffer returned
277 * A sysfs 'read-only' shost attribute.
279 static ssize_t pm8001_ctl_iop_log_show(struct device *cdev,
280 struct device_attribute *attr, char *buf)
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 #define IOP_MEMMAP(r, c) \
286 (*(u32 *)((u8*)pm8001_ha->memoryMap.region[IOP].virt_ptr + (r) * 32 \
287 + (c)))
288 int i;
289 char *str = buf;
290 int max = 2;
291 for (i = 0; i < max; i++) {
292 str += sprintf(str, "0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x"
293 "0x%08x 0x%08x\n",
294 IOP_MEMMAP(i, 0),
295 IOP_MEMMAP(i, 4),
296 IOP_MEMMAP(i, 8),
297 IOP_MEMMAP(i, 12),
298 IOP_MEMMAP(i, 16),
299 IOP_MEMMAP(i, 20),
300 IOP_MEMMAP(i, 24),
301 IOP_MEMMAP(i, 28));
304 return str - buf;
306 static DEVICE_ATTR(iop_log, S_IRUGO, pm8001_ctl_iop_log_show, NULL);
308 #define FLASH_CMD_NONE 0x00
309 #define FLASH_CMD_UPDATE 0x01
310 #define FLASH_CMD_SET_NVMD 0x02
312 struct flash_command {
313 u8 command[8];
314 int code;
317 static struct flash_command flash_command_table[] =
319 {"set_nvmd", FLASH_CMD_SET_NVMD},
320 {"update", FLASH_CMD_UPDATE},
321 {"", FLASH_CMD_NONE} /* Last entry should be NULL. */
324 struct error_fw {
325 char *reason;
326 int err_code;
329 static struct error_fw flash_error_table[] =
331 {"Failed to open fw image file", FAIL_OPEN_BIOS_FILE},
332 {"image header mismatch", FLASH_UPDATE_HDR_ERR},
333 {"image offset mismatch", FLASH_UPDATE_OFFSET_ERR},
334 {"image CRC Error", FLASH_UPDATE_CRC_ERR},
335 {"image length Error.", FLASH_UPDATE_LENGTH_ERR},
336 {"Failed to program flash chip", FLASH_UPDATE_HW_ERR},
337 {"Flash chip not supported.", FLASH_UPDATE_DNLD_NOT_SUPPORTED},
338 {"Flash update disabled.", FLASH_UPDATE_DISABLED},
339 {"Flash in progress", FLASH_IN_PROGRESS},
340 {"Image file size Error", FAIL_FILE_SIZE},
341 {"Input parameter error", FAIL_PARAMETERS},
342 {"Out of memory", FAIL_OUT_MEMORY},
343 {"OK", 0} /* Last entry err_code = 0. */
346 static int pm8001_set_nvmd(struct pm8001_hba_info *pm8001_ha)
348 struct pm8001_ioctl_payload *payload;
349 DECLARE_COMPLETION_ONSTACK(completion);
350 u8 *ioctlbuffer = NULL;
351 u32 length = 0;
352 u32 ret = 0;
354 length = 1024 * 5 + sizeof(*payload) - 1;
355 ioctlbuffer = kzalloc(length, GFP_KERNEL);
356 if (!ioctlbuffer)
357 return -ENOMEM;
358 if ((pm8001_ha->fw_image->size <= 0) ||
359 (pm8001_ha->fw_image->size > 4096)) {
360 ret = FAIL_FILE_SIZE;
361 goto out;
363 payload = (struct pm8001_ioctl_payload *)ioctlbuffer;
364 memcpy((u8 *)payload->func_specific, (u8 *)pm8001_ha->fw_image->data,
365 pm8001_ha->fw_image->size);
366 payload->length = pm8001_ha->fw_image->size;
367 payload->id = 0;
368 pm8001_ha->nvmd_completion = &completion;
369 ret = PM8001_CHIP_DISP->set_nvmd_req(pm8001_ha, payload);
370 wait_for_completion(&completion);
371 out:
372 kfree(ioctlbuffer);
373 return ret;
376 static int pm8001_update_flash(struct pm8001_hba_info *pm8001_ha)
378 struct pm8001_ioctl_payload *payload;
379 DECLARE_COMPLETION_ONSTACK(completion);
380 u8 *ioctlbuffer = NULL;
381 u32 length = 0;
382 struct fw_control_info *fwControl;
383 u32 loopNumber, loopcount = 0;
384 u32 sizeRead = 0;
385 u32 partitionSize, partitionSizeTmp;
386 u32 ret = 0;
387 u32 partitionNumber = 0;
388 struct pm8001_fw_image_header *image_hdr;
390 length = 1024 * 16 + sizeof(*payload) - 1;
391 ioctlbuffer = kzalloc(length, GFP_KERNEL);
392 image_hdr = (struct pm8001_fw_image_header *)pm8001_ha->fw_image->data;
393 if (!ioctlbuffer)
394 return -ENOMEM;
395 if (pm8001_ha->fw_image->size < 28) {
396 ret = FAIL_FILE_SIZE;
397 goto out;
400 while (sizeRead < pm8001_ha->fw_image->size) {
401 partitionSizeTmp =
402 *(u32 *)((u8 *)&image_hdr->image_length + sizeRead);
403 partitionSize = be32_to_cpu(partitionSizeTmp);
404 loopcount = (partitionSize + HEADER_LEN)/IOCTL_BUF_SIZE;
405 if (loopcount % IOCTL_BUF_SIZE)
406 loopcount++;
407 if (loopcount == 0)
408 loopcount++;
409 for (loopNumber = 0; loopNumber < loopcount; loopNumber++) {
410 payload = (struct pm8001_ioctl_payload *)ioctlbuffer;
411 payload->length = 1024*16;
412 payload->id = 0;
413 fwControl =
414 (struct fw_control_info *)payload->func_specific;
415 fwControl->len = IOCTL_BUF_SIZE; /* IN */
416 fwControl->size = partitionSize + HEADER_LEN;/* IN */
417 fwControl->retcode = 0;/* OUT */
418 fwControl->offset = loopNumber * IOCTL_BUF_SIZE;/*OUT */
420 /* for the last chunk of data in case file size is not even with
421 4k, load only the rest*/
422 if (((loopcount-loopNumber) == 1) &&
423 ((partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE)) {
424 fwControl->len =
425 (partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE;
426 memcpy((u8 *)fwControl->buffer,
427 (u8 *)pm8001_ha->fw_image->data + sizeRead,
428 (partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE);
429 sizeRead +=
430 (partitionSize + HEADER_LEN) % IOCTL_BUF_SIZE;
431 } else {
432 memcpy((u8 *)fwControl->buffer,
433 (u8 *)pm8001_ha->fw_image->data + sizeRead,
434 IOCTL_BUF_SIZE);
435 sizeRead += IOCTL_BUF_SIZE;
438 pm8001_ha->nvmd_completion = &completion;
439 ret = PM8001_CHIP_DISP->fw_flash_update_req(pm8001_ha, payload);
440 wait_for_completion(&completion);
441 if (ret || (fwControl->retcode > FLASH_UPDATE_IN_PROGRESS)) {
442 ret = fwControl->retcode;
443 kfree(ioctlbuffer);
444 ioctlbuffer = NULL;
445 break;
448 if (ret)
449 break;
450 partitionNumber++;
452 out:
453 kfree(ioctlbuffer);
454 return ret;
456 static ssize_t pm8001_store_update_fw(struct device *cdev,
457 struct device_attribute *attr,
458 const char *buf, size_t count)
460 struct Scsi_Host *shost = class_to_shost(cdev);
461 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
462 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
463 char *cmd_ptr, *filename_ptr;
464 int res, i;
465 int flash_command = FLASH_CMD_NONE;
466 int err = 0;
467 if (!capable(CAP_SYS_ADMIN))
468 return -EACCES;
470 cmd_ptr = kzalloc(count*2, GFP_KERNEL);
472 if (!cmd_ptr) {
473 err = FAIL_OUT_MEMORY;
474 goto out;
477 filename_ptr = cmd_ptr + count;
478 res = sscanf(buf, "%s %s", cmd_ptr, filename_ptr);
479 if (res != 2) {
480 err = FAIL_PARAMETERS;
481 goto out1;
484 for (i = 0; flash_command_table[i].code != FLASH_CMD_NONE; i++) {
485 if (!memcmp(flash_command_table[i].command,
486 cmd_ptr, strlen(cmd_ptr))) {
487 flash_command = flash_command_table[i].code;
488 break;
491 if (flash_command == FLASH_CMD_NONE) {
492 err = FAIL_PARAMETERS;
493 goto out1;
496 if (pm8001_ha->fw_status == FLASH_IN_PROGRESS) {
497 err = FLASH_IN_PROGRESS;
498 goto out1;
500 err = request_firmware(&pm8001_ha->fw_image,
501 filename_ptr,
502 pm8001_ha->dev);
504 if (err) {
505 PM8001_FAIL_DBG(pm8001_ha,
506 pm8001_printk("Failed to load firmware image file %s,"
507 " error %d\n", filename_ptr, err));
508 err = FAIL_OPEN_BIOS_FILE;
509 goto out1;
512 switch (flash_command) {
513 case FLASH_CMD_UPDATE:
514 pm8001_ha->fw_status = FLASH_IN_PROGRESS;
515 err = pm8001_update_flash(pm8001_ha);
516 break;
517 case FLASH_CMD_SET_NVMD:
518 pm8001_ha->fw_status = FLASH_IN_PROGRESS;
519 err = pm8001_set_nvmd(pm8001_ha);
520 break;
521 default:
522 pm8001_ha->fw_status = FAIL_PARAMETERS;
523 err = FAIL_PARAMETERS;
524 break;
526 release_firmware(pm8001_ha->fw_image);
527 out1:
528 kfree(cmd_ptr);
529 out:
530 pm8001_ha->fw_status = err;
532 if (!err)
533 return count;
534 else
535 return -err;
538 static ssize_t pm8001_show_update_fw(struct device *cdev,
539 struct device_attribute *attr, char *buf)
541 int i;
542 struct Scsi_Host *shost = class_to_shost(cdev);
543 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
544 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
546 for (i = 0; flash_error_table[i].err_code != 0; i++) {
547 if (flash_error_table[i].err_code == pm8001_ha->fw_status)
548 break;
550 if (pm8001_ha->fw_status != FLASH_IN_PROGRESS)
551 pm8001_ha->fw_status = FLASH_OK;
553 return snprintf(buf, PAGE_SIZE, "status=%x %s\n",
554 flash_error_table[i].err_code,
555 flash_error_table[i].reason);
558 static DEVICE_ATTR(update_fw, S_IRUGO|S_IWUGO,
559 pm8001_show_update_fw, pm8001_store_update_fw);
560 struct device_attribute *pm8001_host_attrs[] = {
561 &dev_attr_interface_rev,
562 &dev_attr_fw_version,
563 &dev_attr_update_fw,
564 &dev_attr_aap_log,
565 &dev_attr_iop_log,
566 &dev_attr_max_out_io,
567 &dev_attr_max_devices,
568 &dev_attr_max_sg_list,
569 &dev_attr_sas_spec_support,
570 &dev_attr_logging_level,
571 &dev_attr_host_sas_address,
572 NULL,