[SCSI] qla2xxx: Add ISP25XX support.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / qla2xxx / qla_attr.c
CommitLineData
8482e118 1/*
fa90c54f
AV
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
8482e118 4 *
fa90c54f 5 * See LICENSE.qla2xxx for copyright and licensing details.
8482e118
AV
6 */
7#include "qla_def.h"
8
2c3dfe3f 9#include <linux/kthread.h>
7aaef27b 10#include <linux/vmalloc.h>
8482e118 11
2c3dfe3f
SJ
12int qla24xx_vport_disable(struct fc_vport *, bool);
13
8482e118
AV
14/* SYSFS attributes --------------------------------------------------------- */
15
16static ssize_t
91a69029
ZR
17qla2x00_sysfs_read_fw_dump(struct kobject *kobj,
18 struct bin_attribute *bin_attr,
19 char *buf, loff_t off, size_t count)
8482e118
AV
20{
21 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
22 struct device, kobj)));
a7a167bf 23 char *rbuf = (char *)ha->fw_dump;
8482e118
AV
24
25 if (ha->fw_dump_reading == 0)
26 return 0;
a7a167bf
AV
27 if (off > ha->fw_dump_len)
28 return 0;
29 if (off + count > ha->fw_dump_len)
30 count = ha->fw_dump_len - off;
8482e118 31
a7a167bf 32 memcpy(buf, &rbuf[off], count);
8482e118
AV
33
34 return (count);
35}
36
37static ssize_t
91a69029
ZR
38qla2x00_sysfs_write_fw_dump(struct kobject *kobj,
39 struct bin_attribute *bin_attr,
40 char *buf, loff_t off, size_t count)
8482e118
AV
41{
42 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
43 struct device, kobj)));
44 int reading;
8482e118
AV
45
46 if (off != 0)
47 return (0);
48
49 reading = simple_strtol(buf, NULL, 10);
50 switch (reading) {
51 case 0:
a7a167bf
AV
52 if (!ha->fw_dump_reading)
53 break;
8482e118 54
a7a167bf
AV
55 qla_printk(KERN_INFO, ha,
56 "Firmware dump cleared on (%ld).\n", ha->host_no);
57
58 ha->fw_dump_reading = 0;
59 ha->fw_dumped = 0;
8482e118
AV
60 break;
61 case 1:
d4e3e04d 62 if (ha->fw_dumped && !ha->fw_dump_reading) {
8482e118
AV
63 ha->fw_dump_reading = 1;
64
8482e118 65 qla_printk(KERN_INFO, ha,
a7a167bf 66 "Raw firmware dump ready for read on (%ld).\n",
8482e118 67 ha->host_no);
8482e118
AV
68 }
69 break;
a7a167bf
AV
70 case 2:
71 qla2x00_alloc_fw_dump(ha);
72 break;
8482e118
AV
73 }
74 return (count);
75}
76
77static struct bin_attribute sysfs_fw_dump_attr = {
78 .attr = {
79 .name = "fw_dump",
80 .mode = S_IRUSR | S_IWUSR,
8482e118
AV
81 },
82 .size = 0,
83 .read = qla2x00_sysfs_read_fw_dump,
84 .write = qla2x00_sysfs_write_fw_dump,
85};
86
87static ssize_t
91a69029
ZR
88qla2x00_sysfs_read_nvram(struct kobject *kobj,
89 struct bin_attribute *bin_attr,
90 char *buf, loff_t off, size_t count)
8482e118
AV
91{
92 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
93 struct device, kobj)));
8482e118 94 unsigned long flags;
8482e118 95
1b3f6365 96 if (!capable(CAP_SYS_ADMIN) || off != 0)
8482e118
AV
97 return 0;
98
99 /* Read NVRAM. */
100 spin_lock_irqsave(&ha->hardware_lock, flags);
fd34f556 101 ha->isp_ops->read_nvram(ha, (uint8_t *)buf, ha->nvram_base,
459c5378 102 ha->nvram_size);
8482e118
AV
103 spin_unlock_irqrestore(&ha->hardware_lock, flags);
104
1b3f6365 105 return ha->nvram_size;
8482e118
AV
106}
107
108static ssize_t
91a69029
ZR
109qla2x00_sysfs_write_nvram(struct kobject *kobj,
110 struct bin_attribute *bin_attr,
111 char *buf, loff_t off, size_t count)
8482e118
AV
112{
113 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
114 struct device, kobj)));
8482e118
AV
115 unsigned long flags;
116 uint16_t cnt;
8482e118 117
459c5378 118 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
8482e118
AV
119 return 0;
120
121 /* Checksum NVRAM. */
e428924c 122 if (IS_FWI2_CAPABLE(ha)) {
459c5378
AV
123 uint32_t *iter;
124 uint32_t chksum;
125
126 iter = (uint32_t *)buf;
127 chksum = 0;
128 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
129 chksum += le32_to_cpu(*iter++);
130 chksum = ~chksum + 1;
131 *iter = cpu_to_le32(chksum);
132 } else {
133 uint8_t *iter;
134 uint8_t chksum;
135
136 iter = (uint8_t *)buf;
137 chksum = 0;
138 for (cnt = 0; cnt < count - 1; cnt++)
139 chksum += *iter++;
140 chksum = ~chksum + 1;
141 *iter = chksum;
142 }
8482e118
AV
143
144 /* Write NVRAM. */
145 spin_lock_irqsave(&ha->hardware_lock, flags);
fd34f556 146 ha->isp_ops->write_nvram(ha, (uint8_t *)buf, ha->nvram_base, count);
8482e118
AV
147 spin_unlock_irqrestore(&ha->hardware_lock, flags);
148
26b8d348
AV
149 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
150
8482e118
AV
151 return (count);
152}
153
154static struct bin_attribute sysfs_nvram_attr = {
155 .attr = {
156 .name = "nvram",
157 .mode = S_IRUSR | S_IWUSR,
8482e118 158 },
1b3f6365 159 .size = 512,
8482e118
AV
160 .read = qla2x00_sysfs_read_nvram,
161 .write = qla2x00_sysfs_write_nvram,
162};
163
854165f4 164static ssize_t
91a69029
ZR
165qla2x00_sysfs_read_optrom(struct kobject *kobj,
166 struct bin_attribute *bin_attr,
167 char *buf, loff_t off, size_t count)
854165f4
AV
168{
169 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
170 struct device, kobj)));
171
172 if (ha->optrom_state != QLA_SREADING)
173 return 0;
174 if (off > ha->optrom_size)
175 return 0;
176 if (off + count > ha->optrom_size)
177 count = ha->optrom_size - off;
178
179 memcpy(buf, &ha->optrom_buffer[off], count);
180
181 return count;
182}
183
184static ssize_t
91a69029
ZR
185qla2x00_sysfs_write_optrom(struct kobject *kobj,
186 struct bin_attribute *bin_attr,
187 char *buf, loff_t off, size_t count)
854165f4
AV
188{
189 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
190 struct device, kobj)));
191
192 if (ha->optrom_state != QLA_SWRITING)
193 return -EINVAL;
194 if (off > ha->optrom_size)
195 return -ERANGE;
196 if (off + count > ha->optrom_size)
197 count = ha->optrom_size - off;
198
199 memcpy(&ha->optrom_buffer[off], buf, count);
200
201 return count;
202}
203
204static struct bin_attribute sysfs_optrom_attr = {
205 .attr = {
206 .name = "optrom",
207 .mode = S_IRUSR | S_IWUSR,
854165f4 208 },
c3a2f0df 209 .size = 0,
854165f4
AV
210 .read = qla2x00_sysfs_read_optrom,
211 .write = qla2x00_sysfs_write_optrom,
212};
213
214static ssize_t
91a69029
ZR
215qla2x00_sysfs_write_optrom_ctl(struct kobject *kobj,
216 struct bin_attribute *bin_attr,
217 char *buf, loff_t off, size_t count)
854165f4
AV
218{
219 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
220 struct device, kobj)));
221 int val;
222
223 if (off)
224 return 0;
225
226 if (sscanf(buf, "%d", &val) != 1)
227 return -EINVAL;
228
229 switch (val) {
230 case 0:
231 if (ha->optrom_state != QLA_SREADING &&
232 ha->optrom_state != QLA_SWRITING)
233 break;
234
235 ha->optrom_state = QLA_SWAITING;
236 vfree(ha->optrom_buffer);
237 ha->optrom_buffer = NULL;
238 break;
239 case 1:
240 if (ha->optrom_state != QLA_SWAITING)
241 break;
242
243 ha->optrom_state = QLA_SREADING;
244 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
245 if (ha->optrom_buffer == NULL) {
246 qla_printk(KERN_WARNING, ha,
247 "Unable to allocate memory for optrom retrieval "
248 "(%x).\n", ha->optrom_size);
249
250 ha->optrom_state = QLA_SWAITING;
251 return count;
252 }
253
254 memset(ha->optrom_buffer, 0, ha->optrom_size);
fd34f556 255 ha->isp_ops->read_optrom(ha, ha->optrom_buffer, 0,
854165f4
AV
256 ha->optrom_size);
257 break;
258 case 2:
259 if (ha->optrom_state != QLA_SWAITING)
260 break;
261
262 ha->optrom_state = QLA_SWRITING;
263 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
264 if (ha->optrom_buffer == NULL) {
265 qla_printk(KERN_WARNING, ha,
266 "Unable to allocate memory for optrom update "
267 "(%x).\n", ha->optrom_size);
268
269 ha->optrom_state = QLA_SWAITING;
270 return count;
271 }
272 memset(ha->optrom_buffer, 0, ha->optrom_size);
273 break;
274 case 3:
275 if (ha->optrom_state != QLA_SWRITING)
276 break;
277
fd34f556 278 ha->isp_ops->write_optrom(ha, ha->optrom_buffer, 0,
854165f4
AV
279 ha->optrom_size);
280 break;
281 }
282 return count;
283}
284
285static struct bin_attribute sysfs_optrom_ctl_attr = {
286 .attr = {
287 .name = "optrom_ctl",
288 .mode = S_IWUSR,
854165f4
AV
289 },
290 .size = 0,
291 .write = qla2x00_sysfs_write_optrom_ctl,
292};
293
6f641790 294static ssize_t
91a69029
ZR
295qla2x00_sysfs_read_vpd(struct kobject *kobj,
296 struct bin_attribute *bin_attr,
297 char *buf, loff_t off, size_t count)
6f641790
AV
298{
299 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
300 struct device, kobj)));
301 unsigned long flags;
302
303 if (!capable(CAP_SYS_ADMIN) || off != 0)
304 return 0;
305
6f641790
AV
306 /* Read NVRAM. */
307 spin_lock_irqsave(&ha->hardware_lock, flags);
fd34f556
AV
308 ha->isp_ops->read_nvram(ha, (uint8_t *)buf, ha->vpd_base,
309 ha->vpd_size);
6f641790
AV
310 spin_unlock_irqrestore(&ha->hardware_lock, flags);
311
312 return ha->vpd_size;
313}
314
315static ssize_t
91a69029
ZR
316qla2x00_sysfs_write_vpd(struct kobject *kobj,
317 struct bin_attribute *bin_attr,
318 char *buf, loff_t off, size_t count)
6f641790
AV
319{
320 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
321 struct device, kobj)));
322 unsigned long flags;
323
324 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size)
325 return 0;
326
6f641790
AV
327 /* Write NVRAM. */
328 spin_lock_irqsave(&ha->hardware_lock, flags);
fd34f556 329 ha->isp_ops->write_nvram(ha, (uint8_t *)buf, ha->vpd_base, count);
6f641790
AV
330 spin_unlock_irqrestore(&ha->hardware_lock, flags);
331
332 return count;
333}
334
335static struct bin_attribute sysfs_vpd_attr = {
336 .attr = {
337 .name = "vpd",
338 .mode = S_IRUSR | S_IWUSR,
6f641790
AV
339 },
340 .size = 0,
341 .read = qla2x00_sysfs_read_vpd,
342 .write = qla2x00_sysfs_write_vpd,
343};
344
88729e53 345static ssize_t
91a69029
ZR
346qla2x00_sysfs_read_sfp(struct kobject *kobj,
347 struct bin_attribute *bin_attr,
348 char *buf, loff_t off, size_t count)
88729e53
AV
349{
350 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
351 struct device, kobj)));
352 uint16_t iter, addr, offset;
353 int rval;
354
355 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != SFP_DEV_SIZE * 2)
356 return 0;
357
358 addr = 0xa0;
359 for (iter = 0, offset = 0; iter < (SFP_DEV_SIZE * 2) / SFP_BLOCK_SIZE;
360 iter++, offset += SFP_BLOCK_SIZE) {
361 if (iter == 4) {
362 /* Skip to next device address. */
363 addr = 0xa2;
364 offset = 0;
365 }
366
367 rval = qla2x00_read_sfp(ha, ha->sfp_data_dma, addr, offset,
368 SFP_BLOCK_SIZE);
369 if (rval != QLA_SUCCESS) {
370 qla_printk(KERN_WARNING, ha,
371 "Unable to read SFP data (%x/%x/%x).\n", rval,
372 addr, offset);
373 count = 0;
374 break;
375 }
376 memcpy(buf, ha->sfp_data, SFP_BLOCK_SIZE);
377 buf += SFP_BLOCK_SIZE;
378 }
379
380 return count;
381}
382
383static struct bin_attribute sysfs_sfp_attr = {
384 .attr = {
385 .name = "sfp",
386 .mode = S_IRUSR | S_IWUSR,
88729e53
AV
387 },
388 .size = SFP_DEV_SIZE * 2,
389 .read = qla2x00_sysfs_read_sfp,
390};
391
f1663ad5
AV
392static struct sysfs_entry {
393 char *name;
394 struct bin_attribute *attr;
395 int is4GBp_only;
396} bin_file_entries[] = {
397 { "fw_dump", &sysfs_fw_dump_attr, },
398 { "nvram", &sysfs_nvram_attr, },
399 { "optrom", &sysfs_optrom_attr, },
400 { "optrom_ctl", &sysfs_optrom_ctl_attr, },
401 { "vpd", &sysfs_vpd_attr, 1 },
402 { "sfp", &sysfs_sfp_attr, 1 },
46ddab7b 403 { NULL },
f1663ad5
AV
404};
405
8482e118
AV
406void
407qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
408{
409 struct Scsi_Host *host = ha->host;
f1663ad5
AV
410 struct sysfs_entry *iter;
411 int ret;
8482e118 412
f1663ad5 413 for (iter = bin_file_entries; iter->name; iter++) {
e428924c 414 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
f1663ad5
AV
415 continue;
416
417 ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
418 iter->attr);
419 if (ret)
420 qla_printk(KERN_INFO, ha,
421 "Unable to create sysfs %s binary attribute "
422 "(%d).\n", iter->name, ret);
7914d004 423 }
8482e118
AV
424}
425
426void
427qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
428{
429 struct Scsi_Host *host = ha->host;
f1663ad5
AV
430 struct sysfs_entry *iter;
431
432 for (iter = bin_file_entries; iter->name; iter++) {
e428924c 433 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
f1663ad5 434 continue;
8482e118 435
88729e53 436 sysfs_remove_bin_file(&host->shost_gendev.kobj,
f1663ad5 437 iter->attr);
7914d004 438 }
f6df144c
AV
439
440 if (ha->beacon_blink_led == 1)
fd34f556 441 ha->isp_ops->beacon_off(ha);
8482e118
AV
442}
443
afb046e2
AV
444/* Scsi_Host attributes. */
445
446static ssize_t
447qla2x00_drvr_version_show(struct class_device *cdev, char *buf)
448{
449 return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
450}
451
452static ssize_t
453qla2x00_fw_version_show(struct class_device *cdev, char *buf)
454{
455 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
456 char fw_str[30];
457
458 return snprintf(buf, PAGE_SIZE, "%s\n",
fd34f556 459 ha->isp_ops->fw_version_str(ha, fw_str));
afb046e2
AV
460}
461
462static ssize_t
463qla2x00_serial_num_show(struct class_device *cdev, char *buf)
464{
465 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
466 uint32_t sn;
467
468 sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
469 return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
470 sn % 100000);
471}
472
473static ssize_t
474qla2x00_isp_name_show(struct class_device *cdev, char *buf)
475{
476 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
5433383e 477 return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
afb046e2
AV
478}
479
480static ssize_t
481qla2x00_isp_id_show(struct class_device *cdev, char *buf)
482{
483 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
484 return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
485 ha->product_id[0], ha->product_id[1], ha->product_id[2],
486 ha->product_id[3]);
487}
488
489static ssize_t
490qla2x00_model_name_show(struct class_device *cdev, char *buf)
491{
492 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
493 return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
494}
495
496static ssize_t
497qla2x00_model_desc_show(struct class_device *cdev, char *buf)
498{
499 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
500 return snprintf(buf, PAGE_SIZE, "%s\n",
501 ha->model_desc ? ha->model_desc: "");
502}
503
504static ssize_t
505qla2x00_pci_info_show(struct class_device *cdev, char *buf)
506{
507 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
508 char pci_info[30];
509
510 return snprintf(buf, PAGE_SIZE, "%s\n",
fd34f556 511 ha->isp_ops->pci_info_str(ha, pci_info));
afb046e2
AV
512}
513
514static ssize_t
515qla2x00_state_show(struct class_device *cdev, char *buf)
516{
517 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
518 int len = 0;
519
520 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
521 atomic_read(&ha->loop_state) == LOOP_DEAD)
522 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
523 else if (atomic_read(&ha->loop_state) != LOOP_READY ||
524 test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
525 test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
526 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
527 else {
528 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
529
530 switch (ha->current_topology) {
531 case ISP_CFG_NL:
532 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
533 break;
534 case ISP_CFG_FL:
535 len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
536 break;
537 case ISP_CFG_N:
538 len += snprintf(buf + len, PAGE_SIZE-len,
539 "N_Port to N_Port\n");
540 break;
541 case ISP_CFG_F:
542 len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
543 break;
544 default:
545 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
546 break;
547 }
548 }
549 return len;
550}
551
4fdfefe5
AV
552static ssize_t
553qla2x00_zio_show(struct class_device *cdev, char *buf)
554{
555 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
556 int len = 0;
557
558 switch (ha->zio_mode) {
4fdfefe5
AV
559 case QLA_ZIO_MODE_6:
560 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
561 break;
562 case QLA_ZIO_DISABLED:
563 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
564 break;
565 }
566 return len;
567}
568
569static ssize_t
570qla2x00_zio_store(struct class_device *cdev, const char *buf, size_t count)
571{
572 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
573 int val = 0;
574 uint16_t zio_mode;
575
4a59f71d
AV
576 if (!IS_ZIO_SUPPORTED(ha))
577 return -ENOTSUPP;
578
4fdfefe5
AV
579 if (sscanf(buf, "%d", &val) != 1)
580 return -EINVAL;
581
4a59f71d 582 if (val)
4fdfefe5 583 zio_mode = QLA_ZIO_MODE_6;
4a59f71d 584 else
4fdfefe5 585 zio_mode = QLA_ZIO_DISABLED;
4fdfefe5
AV
586
587 /* Update per-hba values and queue a reset. */
588 if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
589 ha->zio_mode = zio_mode;
590 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
591 }
592 return strlen(buf);
593}
594
595static ssize_t
596qla2x00_zio_timer_show(struct class_device *cdev, char *buf)
597{
598 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
599
600 return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
601}
602
603static ssize_t
604qla2x00_zio_timer_store(struct class_device *cdev, const char *buf,
605 size_t count)
606{
607 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
608 int val = 0;
609 uint16_t zio_timer;
610
611 if (sscanf(buf, "%d", &val) != 1)
612 return -EINVAL;
613 if (val > 25500 || val < 100)
614 return -ERANGE;
615
616 zio_timer = (uint16_t)(val / 100);
617 ha->zio_timer = zio_timer;
618
619 return strlen(buf);
620}
621
f6df144c
AV
622static ssize_t
623qla2x00_beacon_show(struct class_device *cdev, char *buf)
624{
625 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
626 int len = 0;
627
628 if (ha->beacon_blink_led)
629 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
630 else
631 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
632 return len;
633}
634
635static ssize_t
636qla2x00_beacon_store(struct class_device *cdev, const char *buf,
637 size_t count)
638{
639 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
640 int val = 0;
641 int rval;
642
643 if (IS_QLA2100(ha) || IS_QLA2200(ha))
644 return -EPERM;
645
646 if (test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) {
647 qla_printk(KERN_WARNING, ha,
648 "Abort ISP active -- ignoring beacon request.\n");
649 return -EBUSY;
650 }
651
652 if (sscanf(buf, "%d", &val) != 1)
653 return -EINVAL;
654
655 if (val)
fd34f556 656 rval = ha->isp_ops->beacon_on(ha);
f6df144c 657 else
fd34f556 658 rval = ha->isp_ops->beacon_off(ha);
f6df144c
AV
659
660 if (rval != QLA_SUCCESS)
661 count = 0;
662
663 return count;
664}
665
30c47662
AV
666static ssize_t
667qla2x00_optrom_bios_version_show(struct class_device *cdev, char *buf)
668{
669 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
670
671 return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
672 ha->bios_revision[0]);
673}
674
675static ssize_t
676qla2x00_optrom_efi_version_show(struct class_device *cdev, char *buf)
677{
678 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
679
680 return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
681 ha->efi_revision[0]);
682}
683
684static ssize_t
685qla2x00_optrom_fcode_version_show(struct class_device *cdev, char *buf)
686{
687 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
688
689 return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
690 ha->fcode_revision[0]);
691}
692
693static ssize_t
694qla2x00_optrom_fw_version_show(struct class_device *cdev, char *buf)
695{
696 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
697
698 return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
699 ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
700 ha->fw_revision[3]);
701}
702
afb046e2
AV
703static CLASS_DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show,
704 NULL);
705static CLASS_DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
706static CLASS_DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
707static CLASS_DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
708static CLASS_DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
709static CLASS_DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
710static CLASS_DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
711static CLASS_DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
712static CLASS_DEVICE_ATTR(state, S_IRUGO, qla2x00_state_show, NULL);
4fdfefe5
AV
713static CLASS_DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show,
714 qla2x00_zio_store);
715static CLASS_DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
716 qla2x00_zio_timer_store);
f6df144c
AV
717static CLASS_DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
718 qla2x00_beacon_store);
30c47662
AV
719static CLASS_DEVICE_ATTR(optrom_bios_version, S_IRUGO,
720 qla2x00_optrom_bios_version_show, NULL);
721static CLASS_DEVICE_ATTR(optrom_efi_version, S_IRUGO,
722 qla2x00_optrom_efi_version_show, NULL);
723static CLASS_DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
724 qla2x00_optrom_fcode_version_show, NULL);
725static CLASS_DEVICE_ATTR(optrom_fw_version, S_IRUGO,
726 qla2x00_optrom_fw_version_show, NULL);
afb046e2
AV
727
728struct class_device_attribute *qla2x00_host_attrs[] = {
729 &class_device_attr_driver_version,
730 &class_device_attr_fw_version,
731 &class_device_attr_serial_num,
732 &class_device_attr_isp_name,
733 &class_device_attr_isp_id,
734 &class_device_attr_model_name,
735 &class_device_attr_model_desc,
736 &class_device_attr_pci_info,
737 &class_device_attr_state,
4fdfefe5
AV
738 &class_device_attr_zio,
739 &class_device_attr_zio_timer,
f6df144c 740 &class_device_attr_beacon,
30c47662
AV
741 &class_device_attr_optrom_bios_version,
742 &class_device_attr_optrom_efi_version,
743 &class_device_attr_optrom_fcode_version,
744 &class_device_attr_optrom_fw_version,
afb046e2
AV
745 NULL,
746};
747
8482e118
AV
748/* Host attributes. */
749
750static void
751qla2x00_get_host_port_id(struct Scsi_Host *shost)
752{
753 scsi_qla_host_t *ha = to_qla_host(shost);
754
755 fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
756 ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
757}
758
04414013
AV
759static void
760qla2x00_get_host_speed(struct Scsi_Host *shost)
761{
762 scsi_qla_host_t *ha = to_qla_host(shost);
763 uint32_t speed = 0;
764
765 switch (ha->link_data_rate) {
d8b45213 766 case PORT_SPEED_1GB:
04414013
AV
767 speed = 1;
768 break;
d8b45213 769 case PORT_SPEED_2GB:
04414013
AV
770 speed = 2;
771 break;
d8b45213 772 case PORT_SPEED_4GB:
04414013
AV
773 speed = 4;
774 break;
775 }
776 fc_host_speed(shost) = speed;
777}
778
8d067623
AV
779static void
780qla2x00_get_host_port_type(struct Scsi_Host *shost)
781{
782 scsi_qla_host_t *ha = to_qla_host(shost);
783 uint32_t port_type = FC_PORTTYPE_UNKNOWN;
784
785 switch (ha->current_topology) {
786 case ISP_CFG_NL:
787 port_type = FC_PORTTYPE_LPORT;
788 break;
789 case ISP_CFG_FL:
790 port_type = FC_PORTTYPE_NLPORT;
791 break;
792 case ISP_CFG_N:
793 port_type = FC_PORTTYPE_PTP;
794 break;
795 case ISP_CFG_F:
796 port_type = FC_PORTTYPE_NPORT;
797 break;
798 }
799 fc_host_port_type(shost) = port_type;
800}
801
8482e118
AV
802static void
803qla2x00_get_starget_node_name(struct scsi_target *starget)
804{
805 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
806 scsi_qla_host_t *ha = to_qla_host(host);
bdf79621 807 fc_port_t *fcport;
f8b02a85 808 u64 node_name = 0;
8482e118 809
bdf79621
AV
810 list_for_each_entry(fcport, &ha->fcports, list) {
811 if (starget->id == fcport->os_target_id) {
f8b02a85 812 node_name = wwn_to_u64(fcport->node_name);
bdf79621
AV
813 break;
814 }
815 }
816
f8b02a85 817 fc_starget_node_name(starget) = node_name;
8482e118
AV
818}
819
820static void
821qla2x00_get_starget_port_name(struct scsi_target *starget)
822{
823 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
824 scsi_qla_host_t *ha = to_qla_host(host);
bdf79621 825 fc_port_t *fcport;
f8b02a85 826 u64 port_name = 0;
8482e118 827
bdf79621
AV
828 list_for_each_entry(fcport, &ha->fcports, list) {
829 if (starget->id == fcport->os_target_id) {
f8b02a85 830 port_name = wwn_to_u64(fcport->port_name);
bdf79621
AV
831 break;
832 }
833 }
834
f8b02a85 835 fc_starget_port_name(starget) = port_name;
8482e118
AV
836}
837
838static void
839qla2x00_get_starget_port_id(struct scsi_target *starget)
840{
841 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
842 scsi_qla_host_t *ha = to_qla_host(host);
bdf79621
AV
843 fc_port_t *fcport;
844 uint32_t port_id = ~0U;
845
846 list_for_each_entry(fcport, &ha->fcports, list) {
847 if (starget->id == fcport->os_target_id) {
848 port_id = fcport->d_id.b.domain << 16 |
849 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
850 break;
851 }
852 }
8482e118 853
8482e118
AV
854 fc_starget_port_id(starget) = port_id;
855}
856
857static void
858qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
859{
bdf79621
AV
860 struct Scsi_Host *host = rport_to_shost(rport);
861 scsi_qla_host_t *ha = to_qla_host(host);
8482e118
AV
862
863 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
864}
865
866static void
867qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
868{
bdf79621
AV
869 struct Scsi_Host *host = rport_to_shost(rport);
870 scsi_qla_host_t *ha = to_qla_host(host);
8482e118
AV
871
872 if (timeout)
873 ha->port_down_retry_count = timeout;
874 else
875 ha->port_down_retry_count = 1;
876
877 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
878}
879
91ca7b01
AV
880static int
881qla2x00_issue_lip(struct Scsi_Host *shost)
882{
883 scsi_qla_host_t *ha = to_qla_host(shost);
884
885 set_bit(LOOP_RESET_NEEDED, &ha->dpc_flags);
886 return 0;
887}
888
392e2f65
AV
889static struct fc_host_statistics *
890qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
891{
892 scsi_qla_host_t *ha = to_qla_host(shost);
893 int rval;
894 uint16_t mb_stat[1];
895 link_stat_t stat_buf;
896 struct fc_host_statistics *pfc_host_stat;
897
178779a6 898 rval = QLA_FUNCTION_FAILED;
392e2f65
AV
899 pfc_host_stat = &ha->fc_host_stat;
900 memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
901
e428924c 902 if (IS_FWI2_CAPABLE(ha)) {
392e2f65
AV
903 rval = qla24xx_get_isp_stats(ha, (uint32_t *)&stat_buf,
904 sizeof(stat_buf) / 4, mb_stat);
178779a6
AV
905 } else if (atomic_read(&ha->loop_state) == LOOP_READY &&
906 !test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) &&
907 !test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) &&
908 !ha->dpc_active) {
909 /* Must be in a 'READY' state for statistics retrieval. */
392e2f65
AV
910 rval = qla2x00_get_link_status(ha, ha->loop_id, &stat_buf,
911 mb_stat);
912 }
178779a6
AV
913
914 if (rval != QLA_SUCCESS)
915 goto done;
392e2f65
AV
916
917 pfc_host_stat->link_failure_count = stat_buf.link_fail_cnt;
918 pfc_host_stat->loss_of_sync_count = stat_buf.loss_sync_cnt;
919 pfc_host_stat->loss_of_signal_count = stat_buf.loss_sig_cnt;
920 pfc_host_stat->prim_seq_protocol_err_count = stat_buf.prim_seq_err_cnt;
921 pfc_host_stat->invalid_tx_word_count = stat_buf.inval_xmit_word_cnt;
922 pfc_host_stat->invalid_crc_count = stat_buf.inval_crc_cnt;
178779a6 923done:
392e2f65
AV
924 return pfc_host_stat;
925}
926
1620f7c2
AV
927static void
928qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
929{
930 scsi_qla_host_t *ha = to_qla_host(shost);
931
932 qla2x00_get_sym_node_name(ha, fc_host_symbolic_name(shost));
933}
934
a740a3f0
AV
935static void
936qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
937{
938 scsi_qla_host_t *ha = to_qla_host(shost);
939
940 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
941}
942
90991c85
AV
943static void
944qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
945{
946 scsi_qla_host_t *ha = to_qla_host(shost);
947 u64 node_name;
948
949 if (ha->device_flags & SWITCH_FOUND)
950 node_name = wwn_to_u64(ha->fabric_node_name);
951 else
952 node_name = wwn_to_u64(ha->node_name);
953
954 fc_host_fabric_name(shost) = node_name;
955}
956
7047fcdd
AV
957static void
958qla2x00_get_host_port_state(struct Scsi_Host *shost)
959{
960 scsi_qla_host_t *ha = to_qla_host(shost);
961
962 if (!ha->flags.online)
963 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
964 else if (atomic_read(&ha->loop_state) == LOOP_TIMEOUT)
965 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
966 else
967 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
968}
969
2c3dfe3f
SJ
970static int
971qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
972{
973 int ret = 0;
974 scsi_qla_host_t *ha = (scsi_qla_host_t *) fc_vport->shost->hostdata;
975 scsi_qla_host_t *vha;
976
977 ret = qla24xx_vport_create_req_sanity_check(fc_vport);
978 if (ret) {
979 DEBUG15(printk("qla24xx_vport_create_req_sanity_check failed, "
980 "status %x\n", ret));
981 return (ret);
982 }
983
984 vha = qla24xx_create_vhost(fc_vport);
985 if (vha == NULL) {
986 DEBUG15(printk ("qla24xx_create_vhost failed, vha = %p\n",
987 vha));
988 return FC_VPORT_FAILED;
989 }
990 if (disable) {
991 atomic_set(&vha->vp_state, VP_OFFLINE);
992 fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
993 } else
994 atomic_set(&vha->vp_state, VP_FAILED);
995
996 /* ready to create vport */
997 qla_printk(KERN_INFO, vha, "VP entry id %d assigned.\n", vha->vp_idx);
998
999 /* initialized vport states */
1000 atomic_set(&vha->loop_state, LOOP_DOWN);
1001 vha->vp_err_state= VP_ERR_PORTDWN;
1002 vha->vp_prev_err_state= VP_ERR_UNKWN;
1003 /* Check if physical ha port is Up */
1004 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
1005 atomic_read(&ha->loop_state) == LOOP_DEAD) {
1006 /* Don't retry or attempt login of this virtual port */
1007 DEBUG15(printk ("scsi(%ld): pport loop_state is not UP.\n",
1008 vha->host_no));
1009 atomic_set(&vha->loop_state, LOOP_DEAD);
1010 if (!disable)
1011 fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
1012 }
1013
1014 if (scsi_add_host(vha->host, &fc_vport->dev)) {
1015 DEBUG15(printk("scsi(%ld): scsi_add_host failure for VP[%d].\n",
1016 vha->host_no, vha->vp_idx));
1017 goto vport_create_failed_2;
1018 }
1019
1020 /* initialize attributes */
1021 fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
1022 fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
1023 fc_host_supported_classes(vha->host) =
1024 fc_host_supported_classes(ha->host);
1025 fc_host_supported_speeds(vha->host) =
1026 fc_host_supported_speeds(ha->host);
1027
1028 qla24xx_vport_disable(fc_vport, disable);
1029
1030 return 0;
1031vport_create_failed_2:
1032 qla24xx_disable_vp(vha);
1033 qla24xx_deallocate_vp_id(vha);
1034 kfree(vha->port_name);
1035 kfree(vha->node_name);
1036 scsi_host_put(vha->host);
1037 return FC_VPORT_FAILED;
1038}
1039
1040int
1041qla24xx_vport_delete(struct fc_vport *fc_vport)
1042{
1043 scsi_qla_host_t *ha = (scsi_qla_host_t *) fc_vport->shost->hostdata;
1044 scsi_qla_host_t *vha = fc_vport->dd_data;
1045
1046 qla24xx_disable_vp(vha);
1047 qla24xx_deallocate_vp_id(vha);
1048
1049 down(&ha->vport_sem);
1050 ha->cur_vport_count--;
1051 clear_bit(vha->vp_idx, (unsigned long *)ha->vp_idx_map);
1052 up(&ha->vport_sem);
1053
1054 kfree(vha->node_name);
1055 kfree(vha->port_name);
1056
1057 if (vha->timer_active) {
1058 qla2x00_vp_stop_timer(vha);
1059 DEBUG15(printk ("scsi(%ld): timer for the vport[%d] = %p "
1060 "has stopped\n",
1061 vha->host_no, vha->vp_idx, vha));
1062 }
1063
1064 fc_remove_host(vha->host);
1065
1066 scsi_remove_host(vha->host);
1067
1068 scsi_host_put(vha->host);
1069
1070 return 0;
1071}
1072
1073int
1074qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
1075{
1076 scsi_qla_host_t *vha = fc_vport->dd_data;
1077
1078 if (disable)
1079 qla24xx_disable_vp(vha);
1080 else
1081 qla24xx_enable_vp(vha);
1082
1083 return 0;
1084}
1085
1c97a12a 1086struct fc_function_template qla2xxx_transport_functions = {
8482e118
AV
1087
1088 .show_host_node_name = 1,
1089 .show_host_port_name = 1,
ad3e0eda
AV
1090 .show_host_supported_classes = 1,
1091
8482e118
AV
1092 .get_host_port_id = qla2x00_get_host_port_id,
1093 .show_host_port_id = 1,
04414013
AV
1094 .get_host_speed = qla2x00_get_host_speed,
1095 .show_host_speed = 1,
8d067623
AV
1096 .get_host_port_type = qla2x00_get_host_port_type,
1097 .show_host_port_type = 1,
1620f7c2
AV
1098 .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1099 .show_host_symbolic_name = 1,
a740a3f0
AV
1100 .set_host_system_hostname = qla2x00_set_host_system_hostname,
1101 .show_host_system_hostname = 1,
90991c85
AV
1102 .get_host_fabric_name = qla2x00_get_host_fabric_name,
1103 .show_host_fabric_name = 1,
7047fcdd
AV
1104 .get_host_port_state = qla2x00_get_host_port_state,
1105 .show_host_port_state = 1,
8482e118 1106
bdf79621 1107 .dd_fcrport_size = sizeof(struct fc_port *),
ad3e0eda 1108 .show_rport_supported_classes = 1,
8482e118
AV
1109
1110 .get_starget_node_name = qla2x00_get_starget_node_name,
1111 .show_starget_node_name = 1,
1112 .get_starget_port_name = qla2x00_get_starget_port_name,
1113 .show_starget_port_name = 1,
1114 .get_starget_port_id = qla2x00_get_starget_port_id,
1115 .show_starget_port_id = 1,
1116
1117 .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
1118 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1119 .show_rport_dev_loss_tmo = 1,
1120
91ca7b01 1121 .issue_fc_host_lip = qla2x00_issue_lip,
392e2f65 1122 .get_fc_host_stats = qla2x00_get_fc_host_stats,
2c3dfe3f
SJ
1123
1124 .vport_create = qla24xx_vport_create,
1125 .vport_disable = qla24xx_vport_disable,
1126 .vport_delete = qla24xx_vport_delete,
1127};
1128
1129struct fc_function_template qla2xxx_transport_vport_functions = {
1130
1131 .show_host_node_name = 1,
1132 .show_host_port_name = 1,
1133 .show_host_supported_classes = 1,
1134
1135 .get_host_port_id = qla2x00_get_host_port_id,
1136 .show_host_port_id = 1,
1137 .get_host_speed = qla2x00_get_host_speed,
1138 .show_host_speed = 1,
1139 .get_host_port_type = qla2x00_get_host_port_type,
1140 .show_host_port_type = 1,
1141 .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1142 .show_host_symbolic_name = 1,
1143 .set_host_system_hostname = qla2x00_set_host_system_hostname,
1144 .show_host_system_hostname = 1,
1145 .get_host_fabric_name = qla2x00_get_host_fabric_name,
1146 .show_host_fabric_name = 1,
1147 .get_host_port_state = qla2x00_get_host_port_state,
1148 .show_host_port_state = 1,
1149
1150 .dd_fcrport_size = sizeof(struct fc_port *),
1151 .show_rport_supported_classes = 1,
1152
1153 .get_starget_node_name = qla2x00_get_starget_node_name,
1154 .show_starget_node_name = 1,
1155 .get_starget_port_name = qla2x00_get_starget_port_name,
1156 .show_starget_port_name = 1,
1157 .get_starget_port_id = qla2x00_get_starget_port_id,
1158 .show_starget_port_id = 1,
1159
1160 .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
1161 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1162 .show_rport_dev_loss_tmo = 1,
1163
1164 .issue_fc_host_lip = qla2x00_issue_lip,
1165 .get_fc_host_stats = qla2x00_get_fc_host_stats,
8482e118
AV
1166};
1167
8482e118
AV
1168void
1169qla2x00_init_host_attr(scsi_qla_host_t *ha)
1170{
dad9c8c1
AV
1171 fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
1172 fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
ad3e0eda 1173 fc_host_supported_classes(ha->host) = FC_COS_CLASS3;
2c3dfe3f
SJ
1174 fc_host_max_npiv_vports(ha->host) = MAX_NUM_VPORT_FABRIC;
1175 fc_host_npiv_vports_inuse(ha->host) = ha->cur_vport_count;
8482e118 1176}