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