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