[SCSI] qla2xxx: Rework firmware-trace facilities.
[safe/jmp/linux-2.6] / drivers / scsi / qla2xxx / qla_attr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2005 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/vmalloc.h>
10
11 /* SYSFS attributes --------------------------------------------------------- */
12
13 static ssize_t
14 qla2x00_sysfs_read_fw_dump(struct kobject *kobj, char *buf, loff_t off,
15     size_t count)
16 {
17         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
18             struct device, kobj)));
19         char *rbuf = (char *)ha->fw_dump;
20
21         if (ha->fw_dump_reading == 0)
22                 return 0;
23         if (off > ha->fw_dump_len)
24                 return 0;
25         if (off + count > ha->fw_dump_len)
26                 count = ha->fw_dump_len - off;
27
28         memcpy(buf, &rbuf[off], count);
29
30         return (count);
31 }
32
33 static ssize_t
34 qla2x00_sysfs_write_fw_dump(struct kobject *kobj, char *buf, loff_t off,
35     size_t count)
36 {
37         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
38             struct device, kobj)));
39         int reading;
40
41         if (off != 0)
42                 return (0);
43
44         reading = simple_strtol(buf, NULL, 10);
45         switch (reading) {
46         case 0:
47                 if (!ha->fw_dump_reading)
48                         break;
49
50                 qla_printk(KERN_INFO, ha,
51                     "Firmware dump cleared on (%ld).\n", ha->host_no);
52
53                 ha->fw_dump_reading = 0;
54                 ha->fw_dumped = 0;
55                 break;
56         case 1:
57                 if (ha->fw_dumped && !ha->fw_dump_reading) {
58                         ha->fw_dump_reading = 1;
59
60                         qla_printk(KERN_INFO, ha,
61                             "Raw firmware dump ready for read on (%ld).\n",
62                             ha->host_no);
63                 }
64                 break;
65         case 2:
66                 qla2x00_alloc_fw_dump(ha);
67                 break;
68         }
69         return (count);
70 }
71
72 static struct bin_attribute sysfs_fw_dump_attr = {
73         .attr = {
74                 .name = "fw_dump",
75                 .mode = S_IRUSR | S_IWUSR,
76                 .owner = THIS_MODULE,
77         },
78         .size = 0,
79         .read = qla2x00_sysfs_read_fw_dump,
80         .write = qla2x00_sysfs_write_fw_dump,
81 };
82
83 static ssize_t
84 qla2x00_sysfs_read_nvram(struct kobject *kobj, char *buf, loff_t off,
85     size_t count)
86 {
87         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
88             struct device, kobj)));
89         unsigned long   flags;
90
91         if (!capable(CAP_SYS_ADMIN) || off != 0)
92                 return 0;
93
94         /* Read NVRAM. */
95         spin_lock_irqsave(&ha->hardware_lock, flags);
96         ha->isp_ops.read_nvram(ha, (uint8_t *)buf, ha->nvram_base,
97             ha->nvram_size);
98         spin_unlock_irqrestore(&ha->hardware_lock, flags);
99
100         return ha->nvram_size;
101 }
102
103 static ssize_t
104 qla2x00_sysfs_write_nvram(struct kobject *kobj, char *buf, loff_t off,
105     size_t count)
106 {
107         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
108             struct device, kobj)));
109         unsigned long   flags;
110         uint16_t        cnt;
111
112         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
113                 return 0;
114
115         /* Checksum NVRAM. */
116         if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
117                 uint32_t *iter;
118                 uint32_t chksum;
119
120                 iter = (uint32_t *)buf;
121                 chksum = 0;
122                 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
123                         chksum += le32_to_cpu(*iter++);
124                 chksum = ~chksum + 1;
125                 *iter = cpu_to_le32(chksum);
126         } else {
127                 uint8_t *iter;
128                 uint8_t chksum;
129
130                 iter = (uint8_t *)buf;
131                 chksum = 0;
132                 for (cnt = 0; cnt < count - 1; cnt++)
133                         chksum += *iter++;
134                 chksum = ~chksum + 1;
135                 *iter = chksum;
136         }
137
138         /* Write NVRAM. */
139         spin_lock_irqsave(&ha->hardware_lock, flags);
140         ha->isp_ops.write_nvram(ha, (uint8_t *)buf, ha->nvram_base, count);
141         spin_unlock_irqrestore(&ha->hardware_lock, flags);
142
143         return (count);
144 }
145
146 static struct bin_attribute sysfs_nvram_attr = {
147         .attr = {
148                 .name = "nvram",
149                 .mode = S_IRUSR | S_IWUSR,
150                 .owner = THIS_MODULE,
151         },
152         .size = 512,
153         .read = qla2x00_sysfs_read_nvram,
154         .write = qla2x00_sysfs_write_nvram,
155 };
156
157 static ssize_t
158 qla2x00_sysfs_read_optrom(struct kobject *kobj, char *buf, loff_t off,
159     size_t count)
160 {
161         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
162             struct device, kobj)));
163
164         if (ha->optrom_state != QLA_SREADING)
165                 return 0;
166         if (off > ha->optrom_size)
167                 return 0;
168         if (off + count > ha->optrom_size)
169                 count = ha->optrom_size - off;
170
171         memcpy(buf, &ha->optrom_buffer[off], count);
172
173         return count;
174 }
175
176 static ssize_t
177 qla2x00_sysfs_write_optrom(struct kobject *kobj, char *buf, loff_t off,
178     size_t count)
179 {
180         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
181             struct device, kobj)));
182
183         if (ha->optrom_state != QLA_SWRITING)
184                 return -EINVAL;
185         if (off > ha->optrom_size)
186                 return -ERANGE;
187         if (off + count > ha->optrom_size)
188                 count = ha->optrom_size - off;
189
190         memcpy(&ha->optrom_buffer[off], buf, count);
191
192         return count;
193 }
194
195 static struct bin_attribute sysfs_optrom_attr = {
196         .attr = {
197                 .name = "optrom",
198                 .mode = S_IRUSR | S_IWUSR,
199                 .owner = THIS_MODULE,
200         },
201         .size = OPTROM_SIZE_24XX,
202         .read = qla2x00_sysfs_read_optrom,
203         .write = qla2x00_sysfs_write_optrom,
204 };
205
206 static ssize_t
207 qla2x00_sysfs_write_optrom_ctl(struct kobject *kobj, char *buf, loff_t off,
208     size_t count)
209 {
210         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
211             struct device, kobj)));
212         int val;
213
214         if (off)
215                 return 0;
216
217         if (sscanf(buf, "%d", &val) != 1)
218                 return -EINVAL;
219
220         switch (val) {
221         case 0:
222                 if (ha->optrom_state != QLA_SREADING &&
223                     ha->optrom_state != QLA_SWRITING)
224                         break;
225
226                 ha->optrom_state = QLA_SWAITING;
227                 vfree(ha->optrom_buffer);
228                 ha->optrom_buffer = NULL;
229                 break;
230         case 1:
231                 if (ha->optrom_state != QLA_SWAITING)
232                         break;
233
234                 ha->optrom_state = QLA_SREADING;
235                 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
236                 if (ha->optrom_buffer == NULL) {
237                         qla_printk(KERN_WARNING, ha,
238                             "Unable to allocate memory for optrom retrieval "
239                             "(%x).\n", ha->optrom_size);
240
241                         ha->optrom_state = QLA_SWAITING;
242                         return count;
243                 }
244
245                 memset(ha->optrom_buffer, 0, ha->optrom_size);
246                 ha->isp_ops.read_optrom(ha, ha->optrom_buffer, 0,
247                     ha->optrom_size);
248                 break;
249         case 2:
250                 if (ha->optrom_state != QLA_SWAITING)
251                         break;
252
253                 ha->optrom_state = QLA_SWRITING;
254                 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
255                 if (ha->optrom_buffer == NULL) {
256                         qla_printk(KERN_WARNING, ha,
257                             "Unable to allocate memory for optrom update "
258                             "(%x).\n", ha->optrom_size);
259
260                         ha->optrom_state = QLA_SWAITING;
261                         return count;
262                 }
263                 memset(ha->optrom_buffer, 0, ha->optrom_size);
264                 break;
265         case 3:
266                 if (ha->optrom_state != QLA_SWRITING)
267                         break;
268
269                 ha->isp_ops.write_optrom(ha, ha->optrom_buffer, 0,
270                     ha->optrom_size);
271                 break;
272         }
273         return count;
274 }
275
276 static struct bin_attribute sysfs_optrom_ctl_attr = {
277         .attr = {
278                 .name = "optrom_ctl",
279                 .mode = S_IWUSR,
280                 .owner = THIS_MODULE,
281         },
282         .size = 0,
283         .write = qla2x00_sysfs_write_optrom_ctl,
284 };
285
286 static ssize_t
287 qla2x00_sysfs_read_vpd(struct kobject *kobj, char *buf, loff_t off,
288     size_t count)
289 {
290         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
291             struct device, kobj)));
292         unsigned long flags;
293
294         if (!capable(CAP_SYS_ADMIN) || off != 0)
295                 return 0;
296
297         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
298                 return -ENOTSUPP;
299
300         /* Read NVRAM. */
301         spin_lock_irqsave(&ha->hardware_lock, flags);
302         ha->isp_ops.read_nvram(ha, (uint8_t *)buf, ha->vpd_base, ha->vpd_size);
303         spin_unlock_irqrestore(&ha->hardware_lock, flags);
304
305         return ha->vpd_size;
306 }
307
308 static ssize_t
309 qla2x00_sysfs_write_vpd(struct kobject *kobj, char *buf, loff_t off,
310     size_t count)
311 {
312         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
313             struct device, kobj)));
314         unsigned long flags;
315
316         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size)
317                 return 0;
318
319         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
320                 return -ENOTSUPP;
321
322         /* Write NVRAM. */
323         spin_lock_irqsave(&ha->hardware_lock, flags);
324         ha->isp_ops.write_nvram(ha, (uint8_t *)buf, ha->vpd_base, count);
325         spin_unlock_irqrestore(&ha->hardware_lock, flags);
326
327         return count;
328 }
329
330 static struct bin_attribute sysfs_vpd_attr = {
331         .attr = {
332                 .name = "vpd",
333                 .mode = S_IRUSR | S_IWUSR,
334                 .owner = THIS_MODULE,
335         },
336         .size = 0,
337         .read = qla2x00_sysfs_read_vpd,
338         .write = qla2x00_sysfs_write_vpd,
339 };
340
341 void
342 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
343 {
344         struct Scsi_Host *host = ha->host;
345
346         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
347         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
348         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_optrom_attr);
349         sysfs_create_bin_file(&host->shost_gendev.kobj,
350             &sysfs_optrom_ctl_attr);
351         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_vpd_attr);
352 }
353
354 void
355 qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
356 {
357         struct Scsi_Host *host = ha->host;
358
359         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
360         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
361         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_optrom_attr);
362         sysfs_remove_bin_file(&host->shost_gendev.kobj,
363             &sysfs_optrom_ctl_attr);
364         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_vpd_attr);
365
366         if (ha->beacon_blink_led == 1)
367                 ha->isp_ops.beacon_off(ha);
368 }
369
370 /* Scsi_Host attributes. */
371
372 static ssize_t
373 qla2x00_drvr_version_show(struct class_device *cdev, char *buf)
374 {
375         return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
376 }
377
378 static ssize_t
379 qla2x00_fw_version_show(struct class_device *cdev, char *buf)
380 {
381         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
382         char fw_str[30];
383
384         return snprintf(buf, PAGE_SIZE, "%s\n",
385             ha->isp_ops.fw_version_str(ha, fw_str));
386 }
387
388 static ssize_t
389 qla2x00_serial_num_show(struct class_device *cdev, char *buf)
390 {
391         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
392         uint32_t sn;
393
394         sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
395         return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
396             sn % 100000);
397 }
398
399 static ssize_t
400 qla2x00_isp_name_show(struct class_device *cdev, char *buf)
401 {
402         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
403         return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
404 }
405
406 static ssize_t
407 qla2x00_isp_id_show(struct class_device *cdev, char *buf)
408 {
409         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
410         return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
411             ha->product_id[0], ha->product_id[1], ha->product_id[2],
412             ha->product_id[3]);
413 }
414
415 static ssize_t
416 qla2x00_model_name_show(struct class_device *cdev, char *buf)
417 {
418         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
419         return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
420 }
421
422 static ssize_t
423 qla2x00_model_desc_show(struct class_device *cdev, char *buf)
424 {
425         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
426         return snprintf(buf, PAGE_SIZE, "%s\n",
427             ha->model_desc ? ha->model_desc: "");
428 }
429
430 static ssize_t
431 qla2x00_pci_info_show(struct class_device *cdev, char *buf)
432 {
433         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
434         char pci_info[30];
435
436         return snprintf(buf, PAGE_SIZE, "%s\n",
437             ha->isp_ops.pci_info_str(ha, pci_info));
438 }
439
440 static ssize_t
441 qla2x00_state_show(struct class_device *cdev, char *buf)
442 {
443         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
444         int len = 0;
445
446         if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
447             atomic_read(&ha->loop_state) == LOOP_DEAD)
448                 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
449         else if (atomic_read(&ha->loop_state) != LOOP_READY ||
450             test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
451             test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
452                 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
453         else {
454                 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
455
456                 switch (ha->current_topology) {
457                 case ISP_CFG_NL:
458                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
459                         break;
460                 case ISP_CFG_FL:
461                         len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
462                         break;
463                 case ISP_CFG_N:
464                         len += snprintf(buf + len, PAGE_SIZE-len,
465                             "N_Port to N_Port\n");
466                         break;
467                 case ISP_CFG_F:
468                         len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
469                         break;
470                 default:
471                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
472                         break;
473                 }
474         }
475         return len;
476 }
477
478 static ssize_t
479 qla2x00_zio_show(struct class_device *cdev, char *buf)
480 {
481         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
482         int len = 0;
483
484         switch (ha->zio_mode) {
485         case QLA_ZIO_MODE_6:
486                 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
487                 break;
488         case QLA_ZIO_DISABLED:
489                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
490                 break;
491         }
492         return len;
493 }
494
495 static ssize_t
496 qla2x00_zio_store(struct class_device *cdev, const char *buf, size_t count)
497 {
498         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
499         int val = 0;
500         uint16_t zio_mode;
501
502         if (!IS_ZIO_SUPPORTED(ha))
503                 return -ENOTSUPP;
504
505         if (sscanf(buf, "%d", &val) != 1)
506                 return -EINVAL;
507
508         if (val)
509                 zio_mode = QLA_ZIO_MODE_6;
510         else
511                 zio_mode = QLA_ZIO_DISABLED;
512
513         /* Update per-hba values and queue a reset. */
514         if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
515                 ha->zio_mode = zio_mode;
516                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
517         }
518         return strlen(buf);
519 }
520
521 static ssize_t
522 qla2x00_zio_timer_show(struct class_device *cdev, char *buf)
523 {
524         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
525
526         return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
527 }
528
529 static ssize_t
530 qla2x00_zio_timer_store(struct class_device *cdev, const char *buf,
531     size_t count)
532 {
533         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
534         int val = 0;
535         uint16_t zio_timer;
536
537         if (sscanf(buf, "%d", &val) != 1)
538                 return -EINVAL;
539         if (val > 25500 || val < 100)
540                 return -ERANGE;
541
542         zio_timer = (uint16_t)(val / 100);
543         ha->zio_timer = zio_timer;
544
545         return strlen(buf);
546 }
547
548 static ssize_t
549 qla2x00_beacon_show(struct class_device *cdev, char *buf)
550 {
551         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
552         int len = 0;
553
554         if (ha->beacon_blink_led)
555                 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
556         else
557                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
558         return len;
559 }
560
561 static ssize_t
562 qla2x00_beacon_store(struct class_device *cdev, const char *buf,
563     size_t count)
564 {
565         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
566         int val = 0;
567         int rval;
568
569         if (IS_QLA2100(ha) || IS_QLA2200(ha))
570                 return -EPERM;
571
572         if (test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) {
573                 qla_printk(KERN_WARNING, ha,
574                     "Abort ISP active -- ignoring beacon request.\n");
575                 return -EBUSY;
576         }
577
578         if (sscanf(buf, "%d", &val) != 1)
579                 return -EINVAL;
580
581         if (val)
582                 rval = ha->isp_ops.beacon_on(ha);
583         else
584                 rval = ha->isp_ops.beacon_off(ha);
585
586         if (rval != QLA_SUCCESS)
587                 count = 0;
588
589         return count;
590 }
591
592 static CLASS_DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show,
593         NULL);
594 static CLASS_DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
595 static CLASS_DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
596 static CLASS_DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
597 static CLASS_DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
598 static CLASS_DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
599 static CLASS_DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
600 static CLASS_DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
601 static CLASS_DEVICE_ATTR(state, S_IRUGO, qla2x00_state_show, NULL);
602 static CLASS_DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show,
603     qla2x00_zio_store);
604 static CLASS_DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
605     qla2x00_zio_timer_store);
606 static CLASS_DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
607     qla2x00_beacon_store);
608
609 struct class_device_attribute *qla2x00_host_attrs[] = {
610         &class_device_attr_driver_version,
611         &class_device_attr_fw_version,
612         &class_device_attr_serial_num,
613         &class_device_attr_isp_name,
614         &class_device_attr_isp_id,
615         &class_device_attr_model_name,
616         &class_device_attr_model_desc,
617         &class_device_attr_pci_info,
618         &class_device_attr_state,
619         &class_device_attr_zio,
620         &class_device_attr_zio_timer,
621         &class_device_attr_beacon,
622         NULL,
623 };
624
625 /* Host attributes. */
626
627 static void
628 qla2x00_get_host_port_id(struct Scsi_Host *shost)
629 {
630         scsi_qla_host_t *ha = to_qla_host(shost);
631
632         fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
633             ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
634 }
635
636 static void
637 qla2x00_get_host_speed(struct Scsi_Host *shost)
638 {
639         scsi_qla_host_t *ha = to_qla_host(shost);
640         uint32_t speed = 0;
641
642         switch (ha->link_data_rate) {
643         case LDR_1GB:
644                 speed = 1;
645                 break;
646         case LDR_2GB:
647                 speed = 2;
648                 break;
649         case LDR_4GB:
650                 speed = 4;
651                 break;
652         }
653         fc_host_speed(shost) = speed;
654 }
655
656 static void
657 qla2x00_get_host_port_type(struct Scsi_Host *shost)
658 {
659         scsi_qla_host_t *ha = to_qla_host(shost);
660         uint32_t port_type = FC_PORTTYPE_UNKNOWN;
661
662         switch (ha->current_topology) {
663         case ISP_CFG_NL:
664                 port_type = FC_PORTTYPE_LPORT;
665                 break;
666         case ISP_CFG_FL:
667                 port_type = FC_PORTTYPE_NLPORT;
668                 break;
669         case ISP_CFG_N:
670                 port_type = FC_PORTTYPE_PTP;
671                 break;
672         case ISP_CFG_F:
673                 port_type = FC_PORTTYPE_NPORT;
674                 break;
675         }
676         fc_host_port_type(shost) = port_type;
677 }
678
679 static void
680 qla2x00_get_starget_node_name(struct scsi_target *starget)
681 {
682         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
683         scsi_qla_host_t *ha = to_qla_host(host);
684         fc_port_t *fcport;
685         u64 node_name = 0;
686
687         list_for_each_entry(fcport, &ha->fcports, list) {
688                 if (starget->id == fcport->os_target_id) {
689                         node_name = wwn_to_u64(fcport->node_name);
690                         break;
691                 }
692         }
693
694         fc_starget_node_name(starget) = node_name;
695 }
696
697 static void
698 qla2x00_get_starget_port_name(struct scsi_target *starget)
699 {
700         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
701         scsi_qla_host_t *ha = to_qla_host(host);
702         fc_port_t *fcport;
703         u64 port_name = 0;
704
705         list_for_each_entry(fcport, &ha->fcports, list) {
706                 if (starget->id == fcport->os_target_id) {
707                         port_name = wwn_to_u64(fcport->port_name);
708                         break;
709                 }
710         }
711
712         fc_starget_port_name(starget) = port_name;
713 }
714
715 static void
716 qla2x00_get_starget_port_id(struct scsi_target *starget)
717 {
718         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
719         scsi_qla_host_t *ha = to_qla_host(host);
720         fc_port_t *fcport;
721         uint32_t port_id = ~0U;
722
723         list_for_each_entry(fcport, &ha->fcports, list) {
724                 if (starget->id == fcport->os_target_id) {
725                         port_id = fcport->d_id.b.domain << 16 |
726                             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
727                         break;
728                 }
729         }
730
731         fc_starget_port_id(starget) = port_id;
732 }
733
734 static void
735 qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
736 {
737         struct Scsi_Host *host = rport_to_shost(rport);
738         scsi_qla_host_t *ha = to_qla_host(host);
739
740         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
741 }
742
743 static void
744 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
745 {
746         struct Scsi_Host *host = rport_to_shost(rport);
747         scsi_qla_host_t *ha = to_qla_host(host);
748
749         if (timeout)
750                 ha->port_down_retry_count = timeout;
751         else
752                 ha->port_down_retry_count = 1;
753
754         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
755 }
756
757 static int
758 qla2x00_issue_lip(struct Scsi_Host *shost)
759 {
760         scsi_qla_host_t *ha = to_qla_host(shost);
761
762         set_bit(LOOP_RESET_NEEDED, &ha->dpc_flags);
763         return 0;
764 }
765
766 static struct fc_host_statistics *
767 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
768 {
769         scsi_qla_host_t *ha = to_qla_host(shost);
770         int rval;
771         uint16_t mb_stat[1];
772         link_stat_t stat_buf;
773         struct fc_host_statistics *pfc_host_stat;
774
775         pfc_host_stat = &ha->fc_host_stat;
776         memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
777
778         if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
779                 rval = qla24xx_get_isp_stats(ha, (uint32_t *)&stat_buf,
780                     sizeof(stat_buf) / 4, mb_stat);
781         } else {
782                 rval = qla2x00_get_link_status(ha, ha->loop_id, &stat_buf,
783                     mb_stat);
784         }
785         if (rval != 0) {
786                 qla_printk(KERN_WARNING, ha,
787                     "Unable to retrieve host statistics (%d).\n", mb_stat[0]);
788                 return pfc_host_stat;
789         }
790
791         pfc_host_stat->link_failure_count = stat_buf.link_fail_cnt;
792         pfc_host_stat->loss_of_sync_count = stat_buf.loss_sync_cnt;
793         pfc_host_stat->loss_of_signal_count = stat_buf.loss_sig_cnt;
794         pfc_host_stat->prim_seq_protocol_err_count = stat_buf.prim_seq_err_cnt;
795         pfc_host_stat->invalid_tx_word_count = stat_buf.inval_xmit_word_cnt;
796         pfc_host_stat->invalid_crc_count = stat_buf.inval_crc_cnt;
797
798         return pfc_host_stat;
799 }
800
801 struct fc_function_template qla2xxx_transport_functions = {
802
803         .show_host_node_name = 1,
804         .show_host_port_name = 1,
805         .show_host_supported_classes = 1,
806
807         .get_host_port_id = qla2x00_get_host_port_id,
808         .show_host_port_id = 1,
809         .get_host_speed = qla2x00_get_host_speed,
810         .show_host_speed = 1,
811         .get_host_port_type = qla2x00_get_host_port_type,
812         .show_host_port_type = 1,
813
814         .dd_fcrport_size = sizeof(struct fc_port *),
815         .show_rport_supported_classes = 1,
816
817         .get_starget_node_name = qla2x00_get_starget_node_name,
818         .show_starget_node_name = 1,
819         .get_starget_port_name = qla2x00_get_starget_port_name,
820         .show_starget_port_name = 1,
821         .get_starget_port_id  = qla2x00_get_starget_port_id,
822         .show_starget_port_id = 1,
823
824         .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
825         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
826         .show_rport_dev_loss_tmo = 1,
827
828         .issue_fc_host_lip = qla2x00_issue_lip,
829         .get_fc_host_stats = qla2x00_get_fc_host_stats,
830 };
831
832 void
833 qla2x00_init_host_attr(scsi_qla_host_t *ha)
834 {
835         fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
836         fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
837         fc_host_supported_classes(ha->host) = FC_COS_CLASS3;
838 }