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