Compiler warning fixed
[mirror/scst/.git] / qla2x00t / qla_attr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/kthread.h>
10 #include <linux/vmalloc.h>
11 #include <linux/delay.h>
12 #include <linux/version.h>
13
14 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
15 #include "qla2x_tgt.h"
16 #include <linux/ctype.h>
17 #endif
18
19 static int qla24xx_vport_disable(struct fc_vport *, bool);
20
21 /* SYSFS attributes --------------------------------------------------------- */
22
23 #if defined(CONFIG_SCSI_QLA2XXX_TARGET)
24
25 static ssize_t
26 qla2x00_show_tgt_enabled(struct device *dev,
27                          struct device_attribute *attr, char *buf)
28 {
29         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
30         ulong max_size = PAGE_SIZE;
31         ulong size;
32
33         size = scnprintf(buf, max_size, "%d\n", ha->flags.enable_target_mode);
34
35         return size;
36 }
37
38 static ssize_t
39 qla2x00_store_tgt_enabled(struct device *dev, struct device_attribute *attr,
40                          const char *buf, size_t size)
41 {
42         struct scsi_qla_host *ha = shost_priv(class_to_shost(dev));
43         int force = 0;
44
45         if (buf == NULL)
46                 return size;
47
48         if (qla_target.tgt_host_action == NULL) {
49                 printk(KERN_INFO "%s: not acting for lack of target driver\n",
50                        __func__);
51                 return size;
52         }
53
54         if ((size > 1) && (buf[1] == 'f')) {
55                 force = 1;
56                 printk(KERN_DEBUG "%s: forcing the matter\n", __func__);
57         }
58
59         switch (buf[0]) {
60         case '0' :
61                 if ((ha->flags.enable_target_mode) || force) {
62                         qla_target.tgt_host_action(ha, DISABLE_TARGET_MODE);
63                         msleep_interruptible(10*1000);
64                 }
65                 break;
66         case '1' :
67                 if ((ha->flags.enable_target_mode == 0) || force) {
68                         qla_target.tgt_host_action(ha, ENABLE_TARGET_MODE);
69                         msleep_interruptible(10*1000);
70                 }
71                 break;
72         default:
73                 printk(KERN_INFO "%s: Requested action not understood: %s\n",
74                        __func__, buf);
75                 break;
76         }
77
78         if ((size > 2) && (buf[2] == 'r'))
79                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
80
81         return size;
82 }
83
84 static DEVICE_ATTR(target_mode_enabled,
85                          S_IRUGO|S_IWUSR,
86                          qla2x00_show_tgt_enabled,
87                          qla2x00_store_tgt_enabled);
88
89 static ssize_t
90 qla2x00_show_resource_counts(struct device *dev,
91                          struct device_attribute *attr, char *buf)
92 {
93         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
94         ulong max_size = PAGE_SIZE;
95         ulong size;
96         mbx_cmd_t mc;
97         int rval;
98
99         mc.mb[0] = MBC_GET_RESOURCE_COUNTS;
100         mc.out_mb = MBX_0;
101         mc.in_mb = MBX_0|MBX_1|MBX_2;
102         mc.tov = 30;
103         mc.flags = 0;
104
105         rval = qla2x00_mailbox_command(ha, &mc);
106
107         if (rval != QLA_SUCCESS) {
108                 size = scnprintf(buf, max_size,
109                                 "Mailbox Command failed %d, mb %#x",
110                                 rval, mc.mb[0]);
111                 goto out;
112         }
113
114         size = scnprintf(buf, max_size,
115                         "immed_notify\t%d\ncommand\t\t%d\n",
116                         mc.mb[2], mc.mb[1]);
117
118 out:
119         return size;
120 }
121
122 static DEVICE_ATTR(resource_counts,
123                          S_IRUGO,
124                          qla2x00_show_resource_counts,
125                          NULL);
126
127 static ssize_t
128 qla2x00_show_port_database(struct device *dev,
129                          struct device_attribute *attr, char *buf)
130 {
131         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
132         ulong max_size = PAGE_SIZE;
133         ulong size = 0;
134         int rval, i;
135         uint16_t entries;
136         mbx_cmd_t mc;
137         dma_addr_t pmap_dma;
138         struct port_data {
139                 uint8_t port_name[WWN_SIZE];
140                 uint16_t loop_id;
141         } *pmap;
142         ulong dma_size = 0x100*sizeof(*pmap);
143
144         pmap = (struct port_data *)dma_alloc_coherent(&ha->pdev->dev, dma_size,
145                                                 &pmap_dma, GFP_KERNEL);
146         if (pmap == NULL) {
147                 size = scnprintf(buf, max_size, "DMA Alloc failed of %ld",
148                                 dma_size);
149                 goto out;
150         }
151
152         mc.mb[0] = MBC_PORT_NODE_NAME_LIST;
153         mc.mb[1] = BIT_1;
154         mc.mb[2] = MSW(pmap_dma);
155         mc.mb[3] = LSW(pmap_dma);
156         mc.mb[6] = MSW(MSD(pmap_dma));
157         mc.mb[7] = LSW(MSD(pmap_dma));
158         mc.out_mb = MBX_0|MBX_1|MBX_2|MBX_3|MBX_6|MBX_7;
159         mc.in_mb = MBX_0|MBX_1;
160         mc.tov = 30;
161         mc.flags = MBX_DMA_IN;
162
163         rval = qla2x00_mailbox_command(ha, &mc);
164
165         if (rval != QLA_SUCCESS) {
166                 size = scnprintf(buf, max_size,
167                                 "Mailbox Command failed %d, mb0 %#x mb1 %#x\n",
168                                 rval, mc.mb[0], mc.mb[1]);
169                 goto out_free;
170         }
171
172         entries = le16_to_cpu(mc.mb[1])/sizeof(*pmap);
173
174         size += scnprintf(buf+size, max_size-size,
175                           "Port Name List (%#04x) returned %d bytes\n"
176                           "L_ID WWPN\n", MBC_PORT_NODE_NAME_LIST,
177                           le16_to_cpu(mc.mb[1]));
178
179         for (i = 0; (i < entries) && (size < max_size); ++i) {
180                 size += scnprintf(buf+size, max_size-size,
181                                  "%04x %02x%02x%02x%02x%02x%02x%02x%02x\n",
182                                  le16_to_cpu(pmap[i].loop_id),
183                                  pmap[i].port_name[7], pmap[i].port_name[6],
184                                  pmap[i].port_name[5], pmap[i].port_name[4],
185                                  pmap[i].port_name[3], pmap[i].port_name[2],
186                                  pmap[i].port_name[1], pmap[i].port_name[0]);
187         }
188
189 out_free:
190         dma_free_coherent(&ha->pdev->dev, dma_size, pmap, pmap_dma);
191
192         if (size < max_size) {
193                 /*id_list_t *pc; */
194                 char *id_iter;
195                 unsigned long flags;
196                 struct gid_list_info *gid;
197
198                 spin_lock_irqsave(&ha->hardware_lock, flags);
199                 /* Get list of logged in devices. */
200                 memset(ha->gid_list, 0, GID_LIST_SIZE);
201                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
202                 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
203                                            &entries);
204                 if (rval != QLA_SUCCESS) {
205                         size += scnprintf(buf+size, max_size-size,
206                                          "qla2x00_get_id_list failed: %d",
207                                         rval);
208                         goto get_id_failed;
209                 }
210
211                 size += scnprintf(buf+size, max_size-size,
212                                  "\nGet ID List (0x007C) returned %d entries\n"
213                                  "L_ID PortID\n",
214                                  entries);
215
216                 id_iter = (char *)ha->gid_list;
217                 for (i = 0; (i < entries) && (size < max_size); ++i) {
218                         gid = (struct gid_list_info *)id_iter;
219                         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
220                                 size += scnprintf(buf+size, max_size-size,
221                                                  " %02x  %02x%02x%02x\n",
222                                                  gid->loop_id_2100,
223                                                  gid->domain,
224                                                  gid->area,
225                                                  gid->al_pa);
226
227                         } else {
228                                 size += scnprintf(buf+size, max_size-size,
229                                                  "%04x %02x%02x%02x %02x\n",
230                                                  le16_to_cpu(gid->loop_id),
231                                                  gid->domain,
232                                                  gid->area,
233                                                  gid->al_pa,
234                                                  gid->loop_id_2100);
235
236                         }
237                         id_iter += ha->gid_list_info_size;
238                 }
239         }
240
241 get_id_failed:
242         if (size < max_size) {
243                 fc_port_t *fcport;
244                 char *state;
245                 char port_type[] = "URSBIT";
246
247                 size += scnprintf(buf+size, max_size-size,
248                                  "\nfc_ports database\n");
249
250                 list_for_each_entry_rcu(fcport, &ha->fcports, list) {
251                         if (size >= max_size)
252                                 goto out;
253                         switch (atomic_read(&fcport->state)) {
254                         case FCS_UNCONFIGURED:
255                                 state = "Unconfigured";
256                                 break;
257                         case FCS_DEVICE_DEAD:
258                                 state = "Dead";
259                                 break;
260                         case FCS_DEVICE_LOST:
261                                 state = "Lost";
262                                 break;
263                         case FCS_ONLINE:
264                                 state = "Online";
265                                 break;
266                         case FCS_NOT_SUPPORTED:
267                                 state = "Not Supported";
268                                 break;
269                         case FCS_FAILOVER:
270                                 state = "Failover";
271                                 break;
272                         case FCS_FAILOVER_FAILED:
273                                 state = "Failover Failed";
274                                 break;
275                         default:
276                                 state = "Unknown";
277                                 break;
278                         }
279
280                         size += scnprintf(buf+size, max_size-size,
281                                  "%04x %02x%02x%02x "
282                                  "%02x%02x%02x%02x%02x%02x%02x%02x "
283                                  "%c %s\n",
284                                  fcport->loop_id,
285                                  fcport->d_id.b.domain,
286                                  fcport->d_id.b.area,
287                                  fcport->d_id.b.al_pa,
288                                  fcport->port_name[0], fcport->port_name[1],
289                                  fcport->port_name[2], fcport->port_name[3],
290                                  fcport->port_name[4], fcport->port_name[5],
291                                  fcport->port_name[6], fcport->port_name[7],
292                                  port_type[fcport->port_type], state);
293                 }
294         }
295 out:
296         return size;
297 }
298
299 static ssize_t
300 qla2x00_update_portdb(struct device *dev, struct device_attribute *attr,
301                       const char *buf, size_t size)
302 {
303         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
304         unsigned char reading = '0';
305
306         switch (reading) {
307         case '2':
308                 qla2x00_configure_loop(ha);
309                 break;
310
311         case 'l':
312         case 'L':
313                 qla2x00_configure_local_loop(ha);
314                 break;
315
316         case 'f':
317         case 'F':
318                 qla2x00_configure_fabric(ha);
319
320         default:
321                 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
322                 break;
323         }
324
325         return size;
326 }
327
328
329 static DEVICE_ATTR(port_database,
330                          S_IRUGO|S_IWUSR,
331                          qla2x00_show_port_database,
332                          qla2x00_update_portdb);
333
334 #endif /* CONFIG_SCSI_QLA2XXX_TARGET */
335
336 static ssize_t
337 qla2x00_sysfs_read_fw_dump(struct kobject *kobj,
338                            struct bin_attribute *bin_attr,
339                            char *buf, loff_t off, size_t count)
340 {
341         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
342             struct device, kobj)));
343         char *rbuf = (char *)ha->fw_dump;
344
345         if (ha->fw_dump_reading == 0)
346                 return 0;
347         if (off > ha->fw_dump_len)
348                 return 0;
349         if (off + count > ha->fw_dump_len)
350                 count = ha->fw_dump_len - off;
351
352         memcpy(buf, &rbuf[off], count);
353
354         return (count);
355 }
356
357 static ssize_t
358 qla2x00_sysfs_write_fw_dump(struct kobject *kobj,
359                             struct bin_attribute *bin_attr,
360                             char *buf, loff_t off, size_t count)
361 {
362         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
363             struct device, kobj)));
364         int reading;
365
366         if (off != 0)
367                 return (0);
368
369         reading = simple_strtol(buf, NULL, 10);
370         switch (reading) {
371         case 0:
372                 if (!ha->fw_dump_reading)
373                         break;
374
375                 qla_printk(KERN_INFO, ha,
376                     "Firmware dump cleared on (%ld).\n", ha->host_no);
377
378                 ha->fw_dump_reading = 0;
379                 ha->fw_dumped = 0;
380                 break;
381         case 1:
382                 if (ha->fw_dumped && !ha->fw_dump_reading) {
383                         ha->fw_dump_reading = 1;
384
385                         qla_printk(KERN_INFO, ha,
386                             "Raw firmware dump ready for read on (%ld).\n",
387                             ha->host_no);
388                 }
389                 break;
390         case 2:
391                 qla2x00_alloc_fw_dump(ha);
392                 break;
393         case 3:
394                 qla2x00_system_error(ha);
395                 break;
396         }
397         return (count);
398 }
399
400 static struct bin_attribute sysfs_fw_dump_attr = {
401         .attr = {
402                 .name = "fw_dump",
403                 .mode = S_IRUSR | S_IWUSR,
404         },
405         .size = 0,
406         .read = qla2x00_sysfs_read_fw_dump,
407         .write = qla2x00_sysfs_write_fw_dump,
408 };
409
410 static ssize_t
411 qla2x00_sysfs_read_nvram(struct kobject *kobj,
412                          struct bin_attribute *bin_attr,
413                          char *buf, loff_t off, size_t count)
414 {
415         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
416             struct device, kobj)));
417         int             size = ha->nvram_size;
418         char            *nvram_cache = ha->nvram;
419
420         if (!capable(CAP_SYS_ADMIN) || off > size || count == 0)
421                 return 0;
422         if (off + count > size) {
423                 size -= off;
424                 count = size;
425         }
426
427         /* Read NVRAM data from cache. */
428         memcpy(buf, &nvram_cache[off], count);
429
430         return count;
431 }
432
433 static ssize_t
434 qla2x00_sysfs_write_nvram(struct kobject *kobj,
435                           struct bin_attribute *bin_attr,
436                           char *buf, loff_t off, size_t count)
437 {
438         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
439             struct device, kobj)));
440         uint16_t        cnt;
441
442         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
443                 return 0;
444
445         /* Checksum NVRAM. */
446         if (IS_FWI2_CAPABLE(ha)) {
447                 uint32_t *iter;
448                 uint32_t chksum;
449
450                 iter = (uint32_t *)buf;
451                 chksum = 0;
452                 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
453                         chksum += le32_to_cpu(*iter++);
454                 chksum = ~chksum + 1;
455                 *iter = cpu_to_le32(chksum);
456         } else {
457                 uint8_t *iter;
458                 uint8_t chksum;
459
460                 iter = (uint8_t *)buf;
461                 chksum = 0;
462                 for (cnt = 0; cnt < count - 1; cnt++)
463                         chksum += *iter++;
464                 chksum = ~chksum + 1;
465                 *iter = chksum;
466         }
467
468         /* Write NVRAM. */
469         ha->isp_ops->write_nvram(ha, (uint8_t *)buf, ha->nvram_base, count);
470         ha->isp_ops->read_nvram(ha, (uint8_t *)ha->nvram, ha->nvram_base,
471             count);
472
473         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
474
475         return (count);
476 }
477
478 static struct bin_attribute sysfs_nvram_attr = {
479         .attr = {
480                 .name = "nvram",
481                 .mode = S_IRUSR | S_IWUSR,
482         },
483         .size = 512,
484         .read = qla2x00_sysfs_read_nvram,
485         .write = qla2x00_sysfs_write_nvram,
486 };
487
488 static ssize_t
489 qla2x00_sysfs_read_optrom(struct kobject *kobj,
490                           struct bin_attribute *bin_attr,
491                           char *buf, loff_t off, size_t count)
492 {
493         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
494             struct device, kobj)));
495
496         if (ha->optrom_state != QLA_SREADING)
497                 return 0;
498         if (off > ha->optrom_region_size)
499                 return 0;
500         if (off + count > ha->optrom_region_size)
501                 count = ha->optrom_region_size - off;
502
503         memcpy(buf, &ha->optrom_buffer[off], count);
504
505         return count;
506 }
507
508 static ssize_t
509 qla2x00_sysfs_write_optrom(struct kobject *kobj,
510                            struct bin_attribute *bin_attr,
511                            char *buf, loff_t off, size_t count)
512 {
513         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
514             struct device, kobj)));
515
516         if (ha->optrom_state != QLA_SWRITING)
517                 return -EINVAL;
518         if (off > ha->optrom_region_size)
519                 return -ERANGE;
520         if (off + count > ha->optrom_region_size)
521                 count = ha->optrom_region_size - off;
522
523         memcpy(&ha->optrom_buffer[off], buf, count);
524
525         return count;
526 }
527
528 static struct bin_attribute sysfs_optrom_attr = {
529         .attr = {
530                 .name = "optrom",
531                 .mode = S_IRUSR | S_IWUSR,
532         },
533         .size = 0,
534         .read = qla2x00_sysfs_read_optrom,
535         .write = qla2x00_sysfs_write_optrom,
536 };
537
538 static ssize_t
539 qla2x00_sysfs_write_optrom_ctl(struct kobject *kobj,
540                                struct bin_attribute *bin_attr,
541                                char *buf, loff_t off, size_t count)
542 {
543         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
544             struct device, kobj)));
545         uint32_t start = 0;
546         uint32_t size = ha->optrom_size;
547         int val, valid;
548
549         if (off)
550                 return 0;
551
552         if (sscanf(buf, "%d:%x:%x", &val, &start, &size) < 1)
553                 return -EINVAL;
554         if (start > ha->optrom_size)
555                 return -EINVAL;
556
557         switch (val) {
558         case 0:
559                 if (ha->optrom_state != QLA_SREADING &&
560                     ha->optrom_state != QLA_SWRITING)
561                         break;
562
563                 ha->optrom_state = QLA_SWAITING;
564
565                 DEBUG2(qla_printk(KERN_INFO, ha,
566                     "Freeing flash region allocation -- 0x%x bytes.\n",
567                     ha->optrom_region_size));
568
569                 vfree(ha->optrom_buffer);
570                 ha->optrom_buffer = NULL;
571                 break;
572         case 1:
573                 if (ha->optrom_state != QLA_SWAITING)
574                         break;
575
576                 if (start & 0xfff) {
577                         qla_printk(KERN_WARNING, ha,
578                             "Invalid start region 0x%x/0x%x.\n", start, size);
579                         return -EINVAL;
580                 }
581
582                 ha->optrom_region_start = start;
583                 ha->optrom_region_size = start + size > ha->optrom_size ?
584                     ha->optrom_size - start : size;
585
586                 ha->optrom_state = QLA_SREADING;
587                 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
588                 if (ha->optrom_buffer == NULL) {
589                         qla_printk(KERN_WARNING, ha,
590                             "Unable to allocate memory for optrom retrieval "
591                             "(%x).\n", ha->optrom_region_size);
592
593                         ha->optrom_state = QLA_SWAITING;
594                         return count;
595                 }
596
597                 DEBUG2(qla_printk(KERN_INFO, ha,
598                     "Reading flash region -- 0x%x/0x%x.\n",
599                     ha->optrom_region_start, ha->optrom_region_size));
600
601                 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
602                 ha->isp_ops->read_optrom(ha, ha->optrom_buffer,
603                     ha->optrom_region_start, ha->optrom_region_size);
604                 break;
605         case 2:
606                 if (ha->optrom_state != QLA_SWAITING)
607                         break;
608
609                 /*
610                  * We need to be more restrictive on which FLASH regions are
611                  * allowed to be updated via user-space.  Regions accessible
612                  * via this method include:
613                  *
614                  * ISP21xx/ISP22xx/ISP23xx type boards:
615                  *
616                  *      0x000000 -> 0x020000 -- Boot code.
617                  *
618                  * ISP2322/ISP24xx type boards:
619                  *
620                  *      0x000000 -> 0x07ffff -- Boot code.
621                  *      0x080000 -> 0x0fffff -- Firmware.
622                  *
623                  * ISP25xx type boards:
624                  *
625                  *      0x000000 -> 0x07ffff -- Boot code.
626                  *      0x080000 -> 0x0fffff -- Firmware.
627                  *      0x120000 -> 0x12ffff -- VPD and HBA parameters.
628                  */
629                 valid = 0;
630                 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
631                         valid = 1;
632                 else if (start == (FA_BOOT_CODE_ADDR*4) ||
633                     start == (FA_RISC_CODE_ADDR*4))
634                         valid = 1;
635                 else if (IS_QLA25XX(ha) && start == (FA_VPD_NVRAM_ADDR*4))
636                     valid = 1;
637                 if (!valid) {
638                         qla_printk(KERN_WARNING, ha,
639                             "Invalid start region 0x%x/0x%x.\n", start, size);
640                         return -EINVAL;
641                 }
642
643                 ha->optrom_region_start = start;
644                 ha->optrom_region_size = start + size > ha->optrom_size ?
645                     ha->optrom_size - start : size;
646
647                 ha->optrom_state = QLA_SWRITING;
648                 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
649                 if (ha->optrom_buffer == NULL) {
650                         qla_printk(KERN_WARNING, ha,
651                             "Unable to allocate memory for optrom update "
652                             "(%x).\n", ha->optrom_region_size);
653
654                         ha->optrom_state = QLA_SWAITING;
655                         return count;
656                 }
657
658                 DEBUG2(qla_printk(KERN_INFO, ha,
659                     "Staging flash region write -- 0x%x/0x%x.\n",
660                     ha->optrom_region_start, ha->optrom_region_size));
661
662                 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
663                 break;
664         case 3:
665                 if (ha->optrom_state != QLA_SWRITING)
666                         break;
667
668                 DEBUG2(qla_printk(KERN_INFO, ha,
669                     "Writing flash region -- 0x%x/0x%x.\n",
670                     ha->optrom_region_start, ha->optrom_region_size));
671
672                 ha->isp_ops->write_optrom(ha, ha->optrom_buffer,
673                     ha->optrom_region_start, ha->optrom_region_size);
674                 break;
675         default:
676                 count = -EINVAL;
677         }
678         return count;
679 }
680
681 static struct bin_attribute sysfs_optrom_ctl_attr = {
682         .attr = {
683                 .name = "optrom_ctl",
684                 .mode = S_IWUSR,
685         },
686         .size = 0,
687         .write = qla2x00_sysfs_write_optrom_ctl,
688 };
689
690 static ssize_t
691 qla2x00_sysfs_read_vpd(struct kobject *kobj,
692                        struct bin_attribute *bin_attr,
693                        char *buf, loff_t off, size_t count)
694 {
695         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
696             struct device, kobj)));
697         int           size = ha->vpd_size;
698         char          *vpd_cache = ha->vpd;
699
700         if (!capable(CAP_SYS_ADMIN) || off > size || count == 0)
701                 return 0;
702         if (off + count > size) {
703                 size -= off;
704                 count = size;
705         }
706
707         /* Read NVRAM data from cache. */
708         memcpy(buf, &vpd_cache[off], count);
709
710         return count;
711 }
712
713 static ssize_t
714 qla2x00_sysfs_write_vpd(struct kobject *kobj,
715                         struct bin_attribute *bin_attr,
716                         char *buf, loff_t off, size_t count)
717 {
718         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
719             struct device, kobj)));
720
721         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size)
722                 return 0;
723
724         /* Write NVRAM. */
725         ha->isp_ops->write_nvram(ha, (uint8_t *)buf, ha->vpd_base, count);
726         ha->isp_ops->read_nvram(ha, (uint8_t *)ha->vpd, ha->vpd_base, count);
727
728         return count;
729 }
730
731 static struct bin_attribute sysfs_vpd_attr = {
732         .attr = {
733                 .name = "vpd",
734                 .mode = S_IRUSR | S_IWUSR,
735         },
736         .size = 0,
737         .read = qla2x00_sysfs_read_vpd,
738         .write = qla2x00_sysfs_write_vpd,
739 };
740
741 static ssize_t
742 qla2x00_sysfs_read_sfp(struct kobject *kobj,
743                        struct bin_attribute *bin_attr,
744                        char *buf, loff_t off, size_t count)
745 {
746         struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
747             struct device, kobj)));
748         uint16_t iter, addr, offset;
749         int rval;
750
751         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != SFP_DEV_SIZE * 2)
752                 return 0;
753
754         if (ha->sfp_data)
755                 goto do_read;
756
757         ha->sfp_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
758             &ha->sfp_data_dma);
759         if (!ha->sfp_data) {
760                 qla_printk(KERN_WARNING, ha,
761                     "Unable to allocate memory for SFP read-data.\n");
762                 return 0;
763         }
764
765 do_read:
766         memset(ha->sfp_data, 0, SFP_BLOCK_SIZE);
767         addr = 0xa0;
768         for (iter = 0, offset = 0; iter < (SFP_DEV_SIZE * 2) / SFP_BLOCK_SIZE;
769             iter++, offset += SFP_BLOCK_SIZE) {
770                 if (iter == 4) {
771                         /* Skip to next device address. */
772                         addr = 0xa2;
773                         offset = 0;
774                 }
775
776                 rval = qla2x00_read_sfp(ha, ha->sfp_data_dma, addr, offset,
777                     SFP_BLOCK_SIZE);
778                 if (rval != QLA_SUCCESS) {
779                         qla_printk(KERN_WARNING, ha,
780                             "Unable to read SFP data (%x/%x/%x).\n", rval,
781                             addr, offset);
782                         count = 0;
783                         break;
784                 }
785                 memcpy(buf, ha->sfp_data, SFP_BLOCK_SIZE);
786                 buf += SFP_BLOCK_SIZE;
787         }
788
789         return count;
790 }
791
792 static struct bin_attribute sysfs_sfp_attr = {
793         .attr = {
794                 .name = "sfp",
795                 .mode = S_IRUSR | S_IWUSR,
796         },
797         .size = SFP_DEV_SIZE * 2,
798         .read = qla2x00_sysfs_read_sfp,
799 };
800
801 static struct sysfs_entry {
802         char *name;
803         struct bin_attribute *attr;
804         int is4GBp_only;
805 } bin_file_entries[] = {
806         { "fw_dump", &sysfs_fw_dump_attr, },
807         { "nvram", &sysfs_nvram_attr, },
808         { "optrom", &sysfs_optrom_attr, },
809         { "optrom_ctl", &sysfs_optrom_ctl_attr, },
810         { "vpd", &sysfs_vpd_attr, 1 },
811         { "sfp", &sysfs_sfp_attr, 1 },
812         { NULL },
813 };
814
815 void
816 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
817 {
818         struct Scsi_Host *host = ha->host;
819         struct sysfs_entry *iter;
820         int ret;
821
822         for (iter = bin_file_entries; iter->name; iter++) {
823                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
824                         continue;
825
826                 ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
827                     iter->attr);
828                 if (ret)
829                         qla_printk(KERN_INFO, ha,
830                             "Unable to create sysfs %s binary attribute "
831                             "(%d).\n", iter->name, ret);
832         }
833 }
834
835 void
836 qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
837 {
838         struct Scsi_Host *host = ha->host;
839         struct sysfs_entry *iter;
840
841         for (iter = bin_file_entries; iter->name; iter++) {
842                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
843                         continue;
844
845                 sysfs_remove_bin_file(&host->shost_gendev.kobj,
846                     iter->attr);
847         }
848
849         if (ha->beacon_blink_led == 1)
850                 ha->isp_ops->beacon_off(ha);
851 }
852
853 /* Scsi_Host attributes. */
854
855 static ssize_t
856 qla2x00_drvr_version_show(struct device *dev,
857                           struct device_attribute *attr, char *buf)
858 {
859         return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
860 }
861
862 static ssize_t
863 qla2x00_fw_version_show(struct device *dev,
864                         struct device_attribute *attr, char *buf)
865 {
866         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
867         char fw_str[30];
868
869         return snprintf(buf, PAGE_SIZE, "%s\n",
870             ha->isp_ops->fw_version_str(ha, fw_str));
871 }
872
873 static ssize_t
874 qla2x00_serial_num_show(struct device *dev, struct device_attribute *attr,
875                         char *buf)
876 {
877         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
878         uint32_t sn;
879
880         if (IS_FWI2_CAPABLE(ha))
881                 return snprintf(buf, PAGE_SIZE, "\n");
882
883         sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
884         return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
885             sn % 100000);
886 }
887
888 static ssize_t
889 qla2x00_isp_name_show(struct device *dev, struct device_attribute *attr,
890                       char *buf)
891 {
892         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
893         return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
894 }
895
896 static ssize_t
897 qla2x00_isp_id_show(struct device *dev, struct device_attribute *attr,
898                     char *buf)
899 {
900         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
901         return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
902             ha->product_id[0], ha->product_id[1], ha->product_id[2],
903             ha->product_id[3]);
904 }
905
906 static ssize_t
907 qla2x00_model_name_show(struct device *dev, struct device_attribute *attr,
908                         char *buf)
909 {
910         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
911         return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
912 }
913
914 static ssize_t
915 qla2x00_model_desc_show(struct device *dev, struct device_attribute *attr,
916                         char *buf)
917 {
918         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
919         return snprintf(buf, PAGE_SIZE, "%s\n",
920             ha->model_desc ? ha->model_desc: "");
921 }
922
923 static ssize_t
924 qla2x00_pci_info_show(struct device *dev, struct device_attribute *attr,
925                       char *buf)
926 {
927         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
928         char pci_info[30];
929
930         return snprintf(buf, PAGE_SIZE, "%s\n",
931             ha->isp_ops->pci_info_str(ha, pci_info));
932 }
933
934 static ssize_t
935 qla2x00_link_state_show(struct device *dev, struct device_attribute *attr,
936                         char *buf)
937 {
938         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
939         int len = 0;
940
941         if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
942             atomic_read(&ha->loop_state) == LOOP_DEAD)
943                 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
944         else if (atomic_read(&ha->loop_state) != LOOP_READY ||
945             test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
946             test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
947                 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
948         else {
949                 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
950
951                 switch (ha->current_topology) {
952                 case ISP_CFG_NL:
953                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
954                         break;
955                 case ISP_CFG_FL:
956                         len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
957                         break;
958                 case ISP_CFG_N:
959                         len += snprintf(buf + len, PAGE_SIZE-len,
960                             "N_Port to N_Port\n");
961                         break;
962                 case ISP_CFG_F:
963                         len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
964                         break;
965                 default:
966                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
967                         break;
968                 }
969         }
970         return len;
971 }
972
973 static ssize_t
974 qla2x00_zio_show(struct device *dev, struct device_attribute *attr,
975                  char *buf)
976 {
977         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
978         int len = 0;
979
980         switch (ha->zio_mode) {
981         case QLA_ZIO_MODE_6:
982                 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
983                 break;
984         case QLA_ZIO_DISABLED:
985                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
986                 break;
987         }
988         return len;
989 }
990
991 static ssize_t
992 qla2x00_zio_store(struct device *dev, struct device_attribute *attr,
993                   const char *buf, size_t count)
994 {
995         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
996         int val = 0;
997         uint16_t zio_mode;
998
999         if (!IS_ZIO_SUPPORTED(ha))
1000                 return -ENOTSUPP;
1001
1002         if (sscanf(buf, "%d", &val) != 1)
1003                 return -EINVAL;
1004
1005         if (val)
1006                 zio_mode = QLA_ZIO_MODE_6;
1007         else
1008                 zio_mode = QLA_ZIO_DISABLED;
1009
1010         /* Update per-hba values and queue a reset. */
1011         if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
1012                 ha->zio_mode = zio_mode;
1013                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1014         }
1015         return strlen(buf);
1016 }
1017
1018 static ssize_t
1019 qla2x00_zio_timer_show(struct device *dev, struct device_attribute *attr,
1020                        char *buf)
1021 {
1022         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
1023
1024         return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
1025 }
1026
1027 static ssize_t
1028 qla2x00_zio_timer_store(struct device *dev, struct device_attribute *attr,
1029                         const char *buf, size_t count)
1030 {
1031         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
1032         int val = 0;
1033         uint16_t zio_timer;
1034
1035         if (sscanf(buf, "%d", &val) != 1)
1036                 return -EINVAL;
1037         if (val > 25500 || val < 100)
1038                 return -ERANGE;
1039
1040         zio_timer = (uint16_t)(val / 100);
1041         ha->zio_timer = zio_timer;
1042
1043         return strlen(buf);
1044 }
1045
1046 static ssize_t
1047 qla2x00_beacon_show(struct device *dev, struct device_attribute *attr,
1048                     char *buf)
1049 {
1050         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
1051         int len = 0;
1052
1053         if (ha->beacon_blink_led)
1054                 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
1055         else
1056                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1057         return len;
1058 }
1059
1060 static ssize_t
1061 qla2x00_beacon_store(struct device *dev, struct device_attribute *attr,
1062                      const char *buf, size_t count)
1063 {
1064         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
1065         int val = 0;
1066         int rval;
1067
1068         if (IS_QLA2100(ha) || IS_QLA2200(ha))
1069                 return -EPERM;
1070
1071         if (test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) {
1072                 qla_printk(KERN_WARNING, ha,
1073                     "Abort ISP active -- ignoring beacon request.\n");
1074                 return -EBUSY;
1075         }
1076
1077         if (sscanf(buf, "%d", &val) != 1)
1078                 return -EINVAL;
1079
1080         if (val)
1081                 rval = ha->isp_ops->beacon_on(ha);
1082         else
1083                 rval = ha->isp_ops->beacon_off(ha);
1084
1085         if (rval != QLA_SUCCESS)
1086                 count = 0;
1087
1088         return count;
1089 }
1090
1091 static ssize_t
1092 qla2x00_optrom_bios_version_show(struct device *dev,
1093                                  struct device_attribute *attr, char *buf)
1094 {
1095         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
1096
1097         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
1098             ha->bios_revision[0]);
1099 }
1100
1101 static ssize_t
1102 qla2x00_optrom_efi_version_show(struct device *dev,
1103                                 struct device_attribute *attr, char *buf)
1104 {
1105         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
1106
1107         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
1108             ha->efi_revision[0]);
1109 }
1110
1111 static ssize_t
1112 qla2x00_optrom_fcode_version_show(struct device *dev,
1113                                   struct device_attribute *attr, char *buf)
1114 {
1115         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
1116
1117         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
1118             ha->fcode_revision[0]);
1119 }
1120
1121 static ssize_t
1122 qla2x00_optrom_fw_version_show(struct device *dev,
1123                                struct device_attribute *attr, char *buf)
1124 {
1125         scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
1126
1127         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
1128             ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
1129             ha->fw_revision[3]);
1130 }
1131
1132 static DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show, NULL);
1133 static DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
1134 static DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
1135 static DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
1136 static DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
1137 static DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
1138 static DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
1139 static DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
1140 static DEVICE_ATTR(link_state, S_IRUGO, qla2x00_link_state_show, NULL);
1141 static DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show, qla2x00_zio_store);
1142 static DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
1143                    qla2x00_zio_timer_store);
1144 static DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
1145                    qla2x00_beacon_store);
1146 static DEVICE_ATTR(optrom_bios_version, S_IRUGO,
1147                    qla2x00_optrom_bios_version_show, NULL);
1148 static DEVICE_ATTR(optrom_efi_version, S_IRUGO,
1149                    qla2x00_optrom_efi_version_show, NULL);
1150 static DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
1151                    qla2x00_optrom_fcode_version_show, NULL);
1152 static DEVICE_ATTR(optrom_fw_version, S_IRUGO, qla2x00_optrom_fw_version_show,
1153                    NULL);
1154
1155 struct device_attribute *qla2x00_host_attrs[] = {
1156         &dev_attr_driver_version,
1157         &dev_attr_fw_version,
1158         &dev_attr_serial_num,
1159         &dev_attr_isp_name,
1160         &dev_attr_isp_id,
1161         &dev_attr_model_name,
1162         &dev_attr_model_desc,
1163         &dev_attr_pci_info,
1164         &dev_attr_link_state,
1165         &dev_attr_zio,
1166         &dev_attr_zio_timer,
1167         &dev_attr_beacon,
1168         &dev_attr_optrom_bios_version,
1169         &dev_attr_optrom_efi_version,
1170         &dev_attr_optrom_fcode_version,
1171         &dev_attr_optrom_fw_version,
1172 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
1173         &dev_attr_target_mode_enabled,
1174         &dev_attr_resource_counts,
1175         &dev_attr_port_database,
1176 #endif
1177         NULL,
1178 };
1179
1180 /* Host attributes. */
1181
1182 static void
1183 qla2x00_get_host_port_id(struct Scsi_Host *shost)
1184 {
1185         scsi_qla_host_t *ha = shost_priv(shost);
1186
1187         fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
1188             ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
1189 }
1190
1191 static void
1192 qla2x00_get_host_speed(struct Scsi_Host *shost)
1193 {
1194         scsi_qla_host_t *ha = to_qla_parent(shost_priv(shost));
1195         u32 speed = FC_PORTSPEED_UNKNOWN;
1196
1197         switch (ha->link_data_rate) {
1198         case PORT_SPEED_1GB:
1199                 speed = FC_PORTSPEED_1GBIT;
1200                 break;
1201         case PORT_SPEED_2GB:
1202                 speed = FC_PORTSPEED_2GBIT;
1203                 break;
1204         case PORT_SPEED_4GB:
1205                 speed = FC_PORTSPEED_4GBIT;
1206                 break;
1207         case PORT_SPEED_8GB:
1208                 speed = FC_PORTSPEED_8GBIT;
1209                 break;
1210         }
1211         fc_host_speed(shost) = speed;
1212 }
1213
1214 static void
1215 qla2x00_get_host_port_type(struct Scsi_Host *shost)
1216 {
1217         scsi_qla_host_t *ha = shost_priv(shost);
1218         uint32_t port_type = FC_PORTTYPE_UNKNOWN;
1219
1220         if (ha->parent) {
1221                 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
1222                 return;
1223         }
1224         switch (ha->current_topology) {
1225         case ISP_CFG_NL:
1226                 port_type = FC_PORTTYPE_LPORT;
1227                 break;
1228         case ISP_CFG_FL:
1229                 port_type = FC_PORTTYPE_NLPORT;
1230                 break;
1231         case ISP_CFG_N:
1232                 port_type = FC_PORTTYPE_PTP;
1233                 break;
1234         case ISP_CFG_F:
1235                 port_type = FC_PORTTYPE_NPORT;
1236                 break;
1237         }
1238         fc_host_port_type(shost) = port_type;
1239 }
1240
1241 static void
1242 qla2x00_get_starget_node_name(struct scsi_target *starget)
1243 {
1244         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1245         scsi_qla_host_t *ha = shost_priv(host);
1246         fc_port_t *fcport;
1247         u64 node_name = 0;
1248
1249         list_for_each_entry_rcu(fcport, &ha->fcports, list) {
1250                 if (fcport->rport &&
1251                     starget->id == fcport->rport->scsi_target_id) {
1252                         node_name = wwn_to_u64(fcport->node_name);
1253                         break;
1254                 }
1255         }
1256
1257         fc_starget_node_name(starget) = node_name;
1258 }
1259
1260 static void
1261 qla2x00_get_starget_port_name(struct scsi_target *starget)
1262 {
1263         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1264         scsi_qla_host_t *ha = shost_priv(host);
1265         fc_port_t *fcport;
1266         u64 port_name = 0;
1267
1268         list_for_each_entry_rcu(fcport, &ha->fcports, list) {
1269                 if (fcport->rport &&
1270                     starget->id == fcport->rport->scsi_target_id) {
1271                         port_name = wwn_to_u64(fcport->port_name);
1272                         break;
1273                 }
1274         }
1275
1276         fc_starget_port_name(starget) = port_name;
1277 }
1278
1279 static void
1280 qla2x00_get_starget_port_id(struct scsi_target *starget)
1281 {
1282         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1283         scsi_qla_host_t *ha = shost_priv(host);
1284         fc_port_t *fcport;
1285         uint32_t port_id = ~0U;
1286
1287         list_for_each_entry_rcu(fcport, &ha->fcports, list) {
1288                 if (fcport->rport &&
1289                     starget->id == fcport->rport->scsi_target_id) {
1290                         port_id = fcport->d_id.b.domain << 16 |
1291                             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
1292                         break;
1293                 }
1294         }
1295
1296         fc_starget_port_id(starget) = port_id;
1297 }
1298
1299 static void
1300 qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
1301 {
1302         struct Scsi_Host *host = rport_to_shost(rport);
1303         scsi_qla_host_t *ha = shost_priv(host);
1304
1305         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
1306 }
1307
1308 static void
1309 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
1310 {
1311         struct Scsi_Host *host = rport_to_shost(rport);
1312         scsi_qla_host_t *ha = shost_priv(host);
1313
1314         if (timeout)
1315                 ha->port_down_retry_count = timeout;
1316         else
1317                 ha->port_down_retry_count = 1;
1318
1319         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
1320 }
1321
1322 static int
1323 qla2x00_issue_lip(struct Scsi_Host *shost)
1324 {
1325         scsi_qla_host_t *ha = shost_priv(shost);
1326
1327         qla2x00_loop_reset(ha);
1328         return 0;
1329 }
1330
1331 static struct fc_host_statistics *
1332 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
1333 {
1334         scsi_qla_host_t *ha = to_qla_parent(shost_priv(shost));
1335         int rval;
1336         struct link_statistics *stats;
1337         dma_addr_t stats_dma;
1338         struct fc_host_statistics *pfc_host_stat;
1339
1340         pfc_host_stat = &ha->fc_host_stat;
1341         memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
1342
1343         stats = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &stats_dma);
1344         if (stats == NULL) {
1345                 DEBUG2_3_11(printk("%s(%ld): Failed to allocate memory.\n",
1346                     __func__, ha->host_no));
1347                 goto done;
1348         }
1349         memset(stats, 0, DMA_POOL_SIZE);
1350
1351         rval = QLA_FUNCTION_FAILED;
1352         if (IS_FWI2_CAPABLE(ha)) {
1353                 rval = qla24xx_get_isp_stats(ha, stats, stats_dma);
1354         } else if (atomic_read(&ha->loop_state) == LOOP_READY &&
1355                     !test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) &&
1356                     !test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) &&
1357                     !ha->dpc_active) {
1358                 /* Must be in a 'READY' state for statistics retrieval. */
1359                 rval = qla2x00_get_link_status(ha, ha->loop_id, stats,
1360                     stats_dma);
1361         }
1362
1363         if (rval != QLA_SUCCESS)
1364                 goto done_free;
1365
1366         pfc_host_stat->link_failure_count = stats->link_fail_cnt;
1367         pfc_host_stat->loss_of_sync_count = stats->loss_sync_cnt;
1368         pfc_host_stat->loss_of_signal_count = stats->loss_sig_cnt;
1369         pfc_host_stat->prim_seq_protocol_err_count = stats->prim_seq_err_cnt;
1370         pfc_host_stat->invalid_tx_word_count = stats->inval_xmit_word_cnt;
1371         pfc_host_stat->invalid_crc_count = stats->inval_crc_cnt;
1372         if (IS_FWI2_CAPABLE(ha)) {
1373                 pfc_host_stat->tx_frames = stats->tx_frames;
1374                 pfc_host_stat->rx_frames = stats->rx_frames;
1375                 pfc_host_stat->dumped_frames = stats->dumped_frames;
1376                 pfc_host_stat->nos_count = stats->nos_rcvd;
1377         }
1378
1379 done_free:
1380         dma_pool_free(ha->s_dma_pool, stats, stats_dma);
1381 done:
1382         return pfc_host_stat;
1383 }
1384
1385 static void
1386 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
1387 {
1388         scsi_qla_host_t *ha = shost_priv(shost);
1389
1390         qla2x00_get_sym_node_name(ha, fc_host_symbolic_name(shost));
1391 }
1392
1393 static void
1394 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
1395 {
1396         scsi_qla_host_t *ha = shost_priv(shost);
1397
1398         set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
1399 }
1400
1401 static void
1402 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
1403 {
1404         scsi_qla_host_t *ha = shost_priv(shost);
1405         u64 node_name;
1406
1407         if (ha->device_flags & SWITCH_FOUND)
1408                 node_name = wwn_to_u64(ha->fabric_node_name);
1409         else
1410                 node_name = wwn_to_u64(ha->node_name);
1411
1412         fc_host_fabric_name(shost) = node_name;
1413 }
1414
1415 static void
1416 qla2x00_get_host_port_state(struct Scsi_Host *shost)
1417 {
1418         scsi_qla_host_t *ha = to_qla_parent(shost_priv(shost));
1419
1420         if (!ha->flags.online)
1421                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1422         else if (atomic_read(&ha->loop_state) == LOOP_TIMEOUT)
1423                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1424         else
1425                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1426 }
1427
1428 static int
1429 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
1430 {
1431         int     ret = 0;
1432         scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
1433         scsi_qla_host_t *vha;
1434
1435         ret = qla24xx_vport_create_req_sanity_check(fc_vport);
1436         if (ret) {
1437                 DEBUG15(printk("qla24xx_vport_create_req_sanity_check failed, "
1438                     "status %x\n", ret));
1439                 return (ret);
1440         }
1441
1442         vha = qla24xx_create_vhost(fc_vport);
1443         if (vha == NULL) {
1444                 DEBUG15(printk ("qla24xx_create_vhost failed, vha = %p\n",
1445                     vha));
1446                 return FC_VPORT_FAILED;
1447         }
1448         if (disable) {
1449                 atomic_set(&vha->vp_state, VP_OFFLINE);
1450                 fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
1451         } else
1452                 atomic_set(&vha->vp_state, VP_FAILED);
1453
1454         /* ready to create vport */
1455         qla_printk(KERN_INFO, vha, "VP entry id %d assigned.\n", vha->vp_idx);
1456
1457         /* initialized vport states */
1458         atomic_set(&vha->loop_state, LOOP_DOWN);
1459         vha->vp_err_state=  VP_ERR_PORTDWN;
1460         vha->vp_prev_err_state=  VP_ERR_UNKWN;
1461         /* Check if physical ha port is Up */
1462         if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
1463             atomic_read(&ha->loop_state) == LOOP_DEAD) {
1464                 /* Don't retry or attempt login of this virtual port */
1465                 DEBUG15(printk ("scsi(%ld): pport loop_state is not UP.\n",
1466                     vha->host_no));
1467                 atomic_set(&vha->loop_state, LOOP_DEAD);
1468                 if (!disable)
1469                         fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
1470         }
1471
1472         if (scsi_add_host(vha->host, &fc_vport->dev)) {
1473                 DEBUG15(printk("scsi(%ld): scsi_add_host failure for VP[%d].\n",
1474                         vha->host_no, vha->vp_idx));
1475                 goto vport_create_failed_2;
1476         }
1477
1478         /* initialize attributes */
1479         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
1480         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
1481         fc_host_supported_classes(vha->host) =
1482                 fc_host_supported_classes(ha->host);
1483         fc_host_supported_speeds(vha->host) =
1484                 fc_host_supported_speeds(ha->host);
1485
1486         qla24xx_vport_disable(fc_vport, disable);
1487
1488         return 0;
1489 vport_create_failed_2:
1490         qla24xx_disable_vp(vha);
1491         qla24xx_deallocate_vp_id(vha);
1492         kfree(vha->port_name);
1493         kfree(vha->node_name);
1494         scsi_host_put(vha->host);
1495         return FC_VPORT_FAILED;
1496 }
1497
1498 static int
1499 qla24xx_vport_delete(struct fc_vport *fc_vport)
1500 {
1501         scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
1502         scsi_qla_host_t *vha = fc_vport->dd_data;
1503
1504         qla24xx_disable_vp(vha);
1505         qla24xx_deallocate_vp_id(vha);
1506
1507         mutex_lock(&ha->vport_lock);
1508         ha->cur_vport_count--;
1509         clear_bit(vha->vp_idx, ha->vp_idx_map);
1510         mutex_unlock(&ha->vport_lock);
1511
1512         kfree(vha->node_name);
1513         kfree(vha->port_name);
1514
1515         if (vha->timer_active) {
1516                 qla2x00_vp_stop_timer(vha);
1517                 DEBUG15(printk ("scsi(%ld): timer for the vport[%d] = %p "
1518                     "has stopped\n",
1519                     vha->host_no, vha->vp_idx, vha));
1520         }
1521
1522         fc_remove_host(vha->host);
1523
1524         scsi_remove_host(vha->host);
1525
1526         scsi_host_put(vha->host);
1527
1528         return 0;
1529 }
1530
1531 static int
1532 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
1533 {
1534         scsi_qla_host_t *vha = fc_vport->dd_data;
1535
1536         if (disable)
1537                 qla24xx_disable_vp(vha);
1538         else
1539                 qla24xx_enable_vp(vha);
1540
1541         return 0;
1542 }
1543
1544 struct fc_function_template qla2xxx_transport_functions = {
1545
1546         .show_host_node_name = 1,
1547         .show_host_port_name = 1,
1548         .show_host_supported_classes = 1,
1549         .show_host_supported_speeds = 1,
1550
1551         .get_host_port_id = qla2x00_get_host_port_id,
1552         .show_host_port_id = 1,
1553         .get_host_speed = qla2x00_get_host_speed,
1554         .show_host_speed = 1,
1555         .get_host_port_type = qla2x00_get_host_port_type,
1556         .show_host_port_type = 1,
1557         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1558         .show_host_symbolic_name = 1,
1559         .set_host_system_hostname = qla2x00_set_host_system_hostname,
1560         .show_host_system_hostname = 1,
1561         .get_host_fabric_name = qla2x00_get_host_fabric_name,
1562         .show_host_fabric_name = 1,
1563         .get_host_port_state = qla2x00_get_host_port_state,
1564         .show_host_port_state = 1,
1565
1566         .dd_fcrport_size = sizeof(struct fc_port *),
1567         .show_rport_supported_classes = 1,
1568
1569         .get_starget_node_name = qla2x00_get_starget_node_name,
1570         .show_starget_node_name = 1,
1571         .get_starget_port_name = qla2x00_get_starget_port_name,
1572         .show_starget_port_name = 1,
1573         .get_starget_port_id  = qla2x00_get_starget_port_id,
1574         .show_starget_port_id = 1,
1575
1576         .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
1577         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1578         .show_rport_dev_loss_tmo = 1,
1579
1580         .issue_fc_host_lip = qla2x00_issue_lip,
1581         .get_fc_host_stats = qla2x00_get_fc_host_stats,
1582
1583         .vport_create = qla24xx_vport_create,
1584         .vport_disable = qla24xx_vport_disable,
1585         .vport_delete = qla24xx_vport_delete,
1586 };
1587
1588 struct fc_function_template qla2xxx_transport_vport_functions = {
1589
1590         .show_host_node_name = 1,
1591         .show_host_port_name = 1,
1592         .show_host_supported_classes = 1,
1593
1594         .get_host_port_id = qla2x00_get_host_port_id,
1595         .show_host_port_id = 1,
1596         .get_host_speed = qla2x00_get_host_speed,
1597         .show_host_speed = 1,
1598         .get_host_port_type = qla2x00_get_host_port_type,
1599         .show_host_port_type = 1,
1600         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1601         .show_host_symbolic_name = 1,
1602         .set_host_system_hostname = qla2x00_set_host_system_hostname,
1603         .show_host_system_hostname = 1,
1604         .get_host_fabric_name = qla2x00_get_host_fabric_name,
1605         .show_host_fabric_name = 1,
1606         .get_host_port_state = qla2x00_get_host_port_state,
1607         .show_host_port_state = 1,
1608
1609         .dd_fcrport_size = sizeof(struct fc_port *),
1610         .show_rport_supported_classes = 1,
1611
1612         .get_starget_node_name = qla2x00_get_starget_node_name,
1613         .show_starget_node_name = 1,
1614         .get_starget_port_name = qla2x00_get_starget_port_name,
1615         .show_starget_port_name = 1,
1616         .get_starget_port_id  = qla2x00_get_starget_port_id,
1617         .show_starget_port_id = 1,
1618
1619         .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
1620         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1621         .show_rport_dev_loss_tmo = 1,
1622
1623         .issue_fc_host_lip = qla2x00_issue_lip,
1624         .get_fc_host_stats = qla2x00_get_fc_host_stats,
1625 };
1626
1627 void
1628 qla2x00_init_host_attr(scsi_qla_host_t *ha)
1629 {
1630         u32 speed = FC_PORTSPEED_UNKNOWN;
1631
1632         fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
1633         fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
1634         fc_host_supported_classes(ha->host) = FC_COS_CLASS3;
1635         fc_host_max_npiv_vports(ha->host) = ha->max_npiv_vports;;
1636         fc_host_npiv_vports_inuse(ha->host) = ha->cur_vport_count;
1637
1638         if (IS_QLA25XX(ha))
1639                 speed = FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT |
1640                     FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
1641         else if (IS_QLA24XX_TYPE(ha))
1642                 speed = FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
1643                     FC_PORTSPEED_1GBIT;
1644         else if (IS_QLA23XX(ha))
1645                 speed = FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
1646         else
1647                 speed = FC_PORTSPEED_1GBIT;
1648         fc_host_supported_speeds(ha->host) = speed;
1649 }