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