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