1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/firmware.h>
41#include <linux/slab.h>
42#include "pm8001_sas.h"
43#include "pm8001_ctl.h"
44
45
46
47
48
49
50
51
52
53
54static 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 return snprintf(buf, PAGE_SIZE, "%d\n",
62 pm8001_ha->main_cfg_tbl.interface_rev);
63}
64static
65DEVICE_ATTR(interface_rev, S_IRUGO, pm8001_ctl_mpi_interface_rev_show, NULL);
66
67
68
69
70
71
72
73
74static ssize_t pm8001_ctl_fw_version_show(struct device *cdev,
75 struct device_attribute *attr, char *buf)
76{
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;
80
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));
86}
87static DEVICE_ATTR(fw_version, S_IRUGO, pm8001_ctl_fw_version_show, NULL);
88
89
90
91
92
93
94
95static ssize_t pm8001_ctl_max_out_io_show(struct device *cdev,
96 struct device_attribute *attr, char *buf)
97{
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;
101
102 return snprintf(buf, PAGE_SIZE, "%d\n",
103 pm8001_ha->main_cfg_tbl.max_out_io);
104}
105static DEVICE_ATTR(max_out_io, S_IRUGO, pm8001_ctl_max_out_io_show, NULL);
106
107
108
109
110
111
112
113static ssize_t pm8001_ctl_max_devices_show(struct device *cdev,
114 struct device_attribute *attr, char *buf)
115{
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;
119
120 return snprintf(buf, PAGE_SIZE, "%04d\n",
121 (u16)(pm8001_ha->main_cfg_tbl.max_sgl >> 16));
122}
123static DEVICE_ATTR(max_devices, S_IRUGO, pm8001_ctl_max_devices_show, NULL);
124
125
126
127
128
129
130
131
132static ssize_t pm8001_ctl_max_sg_list_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 return snprintf(buf, PAGE_SIZE, "%04d\n",
140 pm8001_ha->main_cfg_tbl.max_sgl & 0x0000FFFF);
141}
142static DEVICE_ATTR(max_sg_list, S_IRUGO, pm8001_ctl_max_sg_list_show, NULL);
143
144#define SAS_1_0 0x1
145#define SAS_1_1 0x2
146#define SAS_2_0 0x4
147
148static ssize_t
149show_sas_spec_support_status(unsigned int mode, char *buf)
150{
151 ssize_t len = 0;
152
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");
158
159 return len;
160}
161
162
163
164
165
166
167
168
169static ssize_t pm8001_ctl_sas_spec_support_show(struct device *cdev,
170 struct device_attribute *attr, char *buf)
171{
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);
178}
179static DEVICE_ATTR(sas_spec_support, S_IRUGO,
180 pm8001_ctl_sas_spec_support_show, NULL);
181
182
183
184
185
186
187
188
189
190
191static ssize_t pm8001_ctl_host_sas_address_show(struct device *cdev,
192 struct device_attribute *attr, char *buf)
193{
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));
199}
200static DEVICE_ATTR(host_sas_address, S_IRUGO,
201 pm8001_ctl_host_sas_address_show, NULL);
202
203
204
205
206
207
208
209
210static ssize_t pm8001_ctl_logging_level_show(struct device *cdev,
211 struct device_attribute *attr, char *buf)
212{
213 struct Scsi_Host *shost = class_to_shost(cdev);
214 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
215 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
216
217 return snprintf(buf, PAGE_SIZE, "%08xh\n", pm8001_ha->logging_level);
218}
219static ssize_t pm8001_ctl_logging_level_store(struct device *cdev,
220 struct device_attribute *attr, const char *buf, size_t count)
221{
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;
226
227 if (sscanf(buf, "%x", &val) != 1)
228 return -EINVAL;
229
230 pm8001_ha->logging_level = val;
231 return strlen(buf);
232}
233
234static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
235 pm8001_ctl_logging_level_show, pm8001_ctl_logging_level_store);
236
237
238
239
240
241
242
243static ssize_t pm8001_ctl_aap_log_show(struct device *cdev,
244 struct device_attribute *attr, char *buf)
245{
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)))
253
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));
267 }
268
269 return str - buf;
270}
271static DEVICE_ATTR(aap_log, S_IRUGO, pm8001_ctl_aap_log_show, NULL);
272
273
274
275
276
277
278
279static ssize_t pm8001_ctl_iop_log_show(struct device *cdev,
280 struct device_attribute *attr, char *buf)
281{
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));
302 }
303
304 return str - buf;
305}
306static DEVICE_ATTR(iop_log, S_IRUGO, pm8001_ctl_iop_log_show, NULL);
307
308#define FLASH_CMD_NONE 0x00
309#define FLASH_CMD_UPDATE 0x01
310#define FLASH_CMD_SET_NVMD 0x02
311
312struct flash_command {
313 u8 command[8];
314 int code;
315};
316
317static struct flash_command flash_command_table[] =
318{
319 {"set_nvmd", FLASH_CMD_SET_NVMD},
320 {"update", FLASH_CMD_UPDATE},
321 {"", FLASH_CMD_NONE}
322};
323
324struct error_fw {
325 char *reason;
326 int err_code;
327};
328
329static struct error_fw flash_error_table[] =
330{
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}
344};
345
346static int pm8001_set_nvmd(struct pm8001_hba_info *pm8001_ha)
347{
348 struct pm8001_ioctl_payload *payload;
349 DECLARE_COMPLETION_ONSTACK(completion);
350 u8 *ioctlbuffer = NULL;
351 u32 length = 0;
352 u32 ret = 0;
353
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;
362 }
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);
371out:
372 kfree(ioctlbuffer);
373 return ret;
374}
375
376static int pm8001_update_flash(struct pm8001_hba_info *pm8001_ha)
377{
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;
389
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;
398 }
399
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;
416 fwControl->size = partitionSize + HEADER_LEN;
417 fwControl->retcode = 0;
418 fwControl->offset = loopNumber * IOCTL_BUF_SIZE;
419
420
421
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;
436 }
437
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;
446 }
447 }
448 if (ret)
449 break;
450 partitionNumber++;
451}
452out:
453 kfree(ioctlbuffer);
454 return ret;
455}
456static ssize_t pm8001_store_update_fw(struct device *cdev,
457 struct device_attribute *attr,
458 const char *buf, size_t count)
459{
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;
469
470 cmd_ptr = kzalloc(count*2, GFP_KERNEL);
471
472 if (!cmd_ptr) {
473 err = FAIL_OUT_MEMORY;
474 goto out;
475 }
476
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;
482 }
483
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;
489 }
490 }
491 if (flash_command == FLASH_CMD_NONE) {
492 err = FAIL_PARAMETERS;
493 goto out1;
494 }
495
496 if (pm8001_ha->fw_status == FLASH_IN_PROGRESS) {
497 err = FLASH_IN_PROGRESS;
498 goto out1;
499 }
500 err = request_firmware(&pm8001_ha->fw_image,
501 filename_ptr,
502 pm8001_ha->dev);
503
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;
510 }
511
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;
525 }
526 release_firmware(pm8001_ha->fw_image);
527out1:
528 kfree(cmd_ptr);
529out:
530 pm8001_ha->fw_status = err;
531
532 if (!err)
533 return count;
534 else
535 return -err;
536}
537
538static ssize_t pm8001_show_update_fw(struct device *cdev,
539 struct device_attribute *attr, char *buf)
540{
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;
545
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;
549 }
550 if (pm8001_ha->fw_status != FLASH_IN_PROGRESS)
551 pm8001_ha->fw_status = FLASH_OK;
552
553 return snprintf(buf, PAGE_SIZE, "status=%x %s\n",
554 flash_error_table[i].err_code,
555 flash_error_table[i].reason);
556}
557
558static DEVICE_ATTR(update_fw, S_IRUGO|S_IWUGO,
559 pm8001_show_update_fw, pm8001_store_update_fw);
560struct 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,
573};
574
575