Manual merge with Linus
[pandora-kernel.git] / drivers / scsi / lpfc / lpfc_attr.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2005 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/ctype.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tcq.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_hw.h"
33 #include "lpfc_sli.h"
34 #include "lpfc_disc.h"
35 #include "lpfc_scsi.h"
36 #include "lpfc.h"
37 #include "lpfc_logmsg.h"
38 #include "lpfc_version.h"
39 #include "lpfc_compat.h"
40 #include "lpfc_crtn.h"
41
42
43 static void
44 lpfc_jedec_to_ascii(int incr, char hdw[])
45 {
46         int i, j;
47         for (i = 0; i < 8; i++) {
48                 j = (incr & 0xf);
49                 if (j <= 9)
50                         hdw[7 - i] = 0x30 +  j;
51                  else
52                         hdw[7 - i] = 0x61 + j - 10;
53                 incr = (incr >> 4);
54         }
55         hdw[8] = 0;
56         return;
57 }
58
59 static ssize_t
60 lpfc_drvr_version_show(struct class_device *cdev, char *buf)
61 {
62         return snprintf(buf, PAGE_SIZE, LPFC_MODULE_DESC "\n");
63 }
64
65 static ssize_t
66 management_version_show(struct class_device *cdev, char *buf)
67 {
68         return snprintf(buf, PAGE_SIZE, DFC_API_VERSION "\n");
69 }
70
71 static ssize_t
72 lpfc_info_show(struct class_device *cdev, char *buf)
73 {
74         struct Scsi_Host *host = class_to_shost(cdev);
75         return snprintf(buf, PAGE_SIZE, "%s\n",lpfc_info(host));
76 }
77
78 static ssize_t
79 lpfc_serialnum_show(struct class_device *cdev, char *buf)
80 {
81         struct Scsi_Host *host = class_to_shost(cdev);
82         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
83         return snprintf(buf, PAGE_SIZE, "%s\n",phba->SerialNumber);
84 }
85
86 static ssize_t
87 lpfc_modeldesc_show(struct class_device *cdev, char *buf)
88 {
89         struct Scsi_Host *host = class_to_shost(cdev);
90         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
91         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelDesc);
92 }
93
94 static ssize_t
95 lpfc_modelname_show(struct class_device *cdev, char *buf)
96 {
97         struct Scsi_Host *host = class_to_shost(cdev);
98         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
99         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelName);
100 }
101
102 static ssize_t
103 lpfc_programtype_show(struct class_device *cdev, char *buf)
104 {
105         struct Scsi_Host *host = class_to_shost(cdev);
106         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
107         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ProgramType);
108 }
109
110 static ssize_t
111 lpfc_portnum_show(struct class_device *cdev, char *buf)
112 {
113         struct Scsi_Host *host = class_to_shost(cdev);
114         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
115         return snprintf(buf, PAGE_SIZE, "%s\n",phba->Port);
116 }
117
118 static ssize_t
119 lpfc_fwrev_show(struct class_device *cdev, char *buf)
120 {
121         struct Scsi_Host *host = class_to_shost(cdev);
122         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
123         char fwrev[32];
124         lpfc_decode_firmware_rev(phba, fwrev, 1);
125         return snprintf(buf, PAGE_SIZE, "%s\n",fwrev);
126 }
127
128 static ssize_t
129 lpfc_hdw_show(struct class_device *cdev, char *buf)
130 {
131         char hdw[9];
132         struct Scsi_Host *host = class_to_shost(cdev);
133         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
134         lpfc_vpd_t *vp = &phba->vpd;
135         lpfc_jedec_to_ascii(vp->rev.biuRev, hdw);
136         return snprintf(buf, PAGE_SIZE, "%s\n", hdw);
137 }
138 static ssize_t
139 lpfc_option_rom_version_show(struct class_device *cdev, char *buf)
140 {
141         struct Scsi_Host *host = class_to_shost(cdev);
142         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
143         return snprintf(buf, PAGE_SIZE, "%s\n", phba->OptionROMVersion);
144 }
145 static ssize_t
146 lpfc_state_show(struct class_device *cdev, char *buf)
147 {
148         struct Scsi_Host *host = class_to_shost(cdev);
149         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
150         int len = 0;
151         switch (phba->hba_state) {
152         case LPFC_INIT_START:
153         case LPFC_INIT_MBX_CMDS:
154         case LPFC_LINK_DOWN:
155                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Down\n");
156                 break;
157         case LPFC_LINK_UP:
158         case LPFC_LOCAL_CFG_LINK:
159                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Up\n");
160                 break;
161         case LPFC_FLOGI:
162         case LPFC_FABRIC_CFG_LINK:
163         case LPFC_NS_REG:
164         case LPFC_NS_QRY:
165         case LPFC_BUILD_DISC_LIST:
166         case LPFC_DISC_AUTH:
167         case LPFC_CLEAR_LA:
168                 len += snprintf(buf + len, PAGE_SIZE-len,
169                                 "Link Up - Discovery\n");
170                 break;
171         case LPFC_HBA_READY:
172                 len += snprintf(buf + len, PAGE_SIZE-len,
173                                 "Link Up - Ready:\n");
174                 if (phba->fc_topology == TOPOLOGY_LOOP) {
175                         if (phba->fc_flag & FC_PUBLIC_LOOP)
176                                 len += snprintf(buf + len, PAGE_SIZE-len,
177                                                 "   Public Loop\n");
178                         else
179                                 len += snprintf(buf + len, PAGE_SIZE-len,
180                                                 "   Private Loop\n");
181                 } else {
182                         if (phba->fc_flag & FC_FABRIC)
183                                 len += snprintf(buf + len, PAGE_SIZE-len,
184                                                 "   Fabric\n");
185                         else
186                                 len += snprintf(buf + len, PAGE_SIZE-len,
187                                                 "   Point-2-Point\n");
188                 }
189         }
190         return len;
191 }
192
193 static ssize_t
194 lpfc_num_discovered_ports_show(struct class_device *cdev, char *buf)
195 {
196         struct Scsi_Host *host = class_to_shost(cdev);
197         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
198         return snprintf(buf, PAGE_SIZE, "%d\n", phba->fc_map_cnt +
199                                                         phba->fc_unmap_cnt);
200 }
201
202
203 static ssize_t
204 lpfc_issue_lip (struct class_device *cdev, const char *buf, size_t count)
205 {
206         struct Scsi_Host *host = class_to_shost(cdev);
207         struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata[0];
208         int val = 0;
209         LPFC_MBOXQ_t *pmboxq;
210         int mbxstatus = MBXERR_ERROR;
211
212         if ((sscanf(buf, "%d", &val) != 1) ||
213             (val != 1))
214                 return -EINVAL;
215
216         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
217             (phba->hba_state != LPFC_HBA_READY))
218                 return -EPERM;
219
220         pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
221
222         if (!pmboxq)
223                 return -ENOMEM;
224
225         memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
226         lpfc_init_link(phba, pmboxq, phba->cfg_topology, phba->cfg_link_speed);
227         mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
228
229         if (mbxstatus == MBX_TIMEOUT)
230                 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
231         else
232                 mempool_free( pmboxq, phba->mbox_mem_pool);
233
234         if (mbxstatus == MBXERR_ERROR)
235                 return -EIO;
236
237         return strlen(buf);
238 }
239
240 static ssize_t
241 lpfc_nport_evt_cnt_show(struct class_device *cdev, char *buf)
242 {
243         struct Scsi_Host *host = class_to_shost(cdev);
244         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
245         return snprintf(buf, PAGE_SIZE, "%d\n", phba->nport_event_cnt);
246 }
247
248 static ssize_t
249 lpfc_board_online_show(struct class_device *cdev, char *buf)
250 {
251         struct Scsi_Host *host = class_to_shost(cdev);
252         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
253
254         if (!phba) return 0;
255
256         if (phba->fc_flag & FC_OFFLINE_MODE)
257                 return snprintf(buf, PAGE_SIZE, "0\n");
258         else
259                 return snprintf(buf, PAGE_SIZE, "1\n");
260 }
261
262 static ssize_t
263 lpfc_board_online_store(struct class_device *cdev, const char *buf,
264                                                                 size_t count)
265 {
266         struct Scsi_Host *host = class_to_shost(cdev);
267         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
268         struct completion online_compl;
269         int val=0, status=0;
270
271         if (sscanf(buf, "%d", &val) != 1)
272                 return 0;
273
274         init_completion(&online_compl);
275
276         if (val)
277                 lpfc_workq_post_event(phba, &status, &online_compl,
278                                                         LPFC_EVT_ONLINE);
279         else
280                 lpfc_workq_post_event(phba, &status, &online_compl,
281                                                         LPFC_EVT_OFFLINE);
282         wait_for_completion(&online_compl);
283         if (!status)
284                 return strlen(buf);
285         else
286                 return 0;
287 }
288
289
290 #define lpfc_param_show(attr)   \
291 static ssize_t \
292 lpfc_##attr##_show(struct class_device *cdev, char *buf) \
293 { \
294         struct Scsi_Host *host = class_to_shost(cdev);\
295         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];\
296         int val = 0;\
297         if (phba){\
298                 val = phba->cfg_##attr;\
299                 return snprintf(buf, PAGE_SIZE, "%d\n",\
300                                 phba->cfg_##attr);\
301         }\
302         return 0;\
303 }
304
305 #define lpfc_param_store(attr, minval, maxval)  \
306 static ssize_t \
307 lpfc_##attr##_store(struct class_device *cdev, const char *buf, size_t count) \
308 { \
309         struct Scsi_Host *host = class_to_shost(cdev);\
310         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];\
311         int val = 0;\
312         if (!isdigit(buf[0]))\
313                 return -EINVAL;\
314         if (sscanf(buf, "0x%x", &val) != 1)\
315                 if (sscanf(buf, "%d", &val) != 1)\
316                         return -EINVAL;\
317         if (phba){\
318                 if (val >= minval && val <= maxval) {\
319                         phba->cfg_##attr = val;\
320                         return strlen(buf);\
321                 }\
322         }\
323         return 0;\
324 }
325
326 #define LPFC_ATTR_R_NOINIT(name, desc) \
327 extern int lpfc_##name;\
328 module_param(lpfc_##name, int, 0);\
329 MODULE_PARM_DESC(lpfc_##name, desc);\
330 lpfc_param_show(name)\
331 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
332
333 #define LPFC_ATTR_R(name, defval, minval, maxval, desc) \
334 static int lpfc_##name = defval;\
335 module_param(lpfc_##name, int, 0);\
336 MODULE_PARM_DESC(lpfc_##name, desc);\
337 lpfc_param_show(name)\
338 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
339
340 #define LPFC_ATTR_RW(name, defval, minval, maxval, desc) \
341 static int lpfc_##name = defval;\
342 module_param(lpfc_##name, int, 0);\
343 MODULE_PARM_DESC(lpfc_##name, desc);\
344 lpfc_param_show(name)\
345 lpfc_param_store(name, minval, maxval)\
346 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
347                          lpfc_##name##_show, lpfc_##name##_store)
348
349 static CLASS_DEVICE_ATTR(info, S_IRUGO, lpfc_info_show, NULL);
350 static CLASS_DEVICE_ATTR(serialnum, S_IRUGO, lpfc_serialnum_show, NULL);
351 static CLASS_DEVICE_ATTR(modeldesc, S_IRUGO, lpfc_modeldesc_show, NULL);
352 static CLASS_DEVICE_ATTR(modelname, S_IRUGO, lpfc_modelname_show, NULL);
353 static CLASS_DEVICE_ATTR(programtype, S_IRUGO, lpfc_programtype_show, NULL);
354 static CLASS_DEVICE_ATTR(portnum, S_IRUGO, lpfc_portnum_show, NULL);
355 static CLASS_DEVICE_ATTR(fwrev, S_IRUGO, lpfc_fwrev_show, NULL);
356 static CLASS_DEVICE_ATTR(hdw, S_IRUGO, lpfc_hdw_show, NULL);
357 static CLASS_DEVICE_ATTR(state, S_IRUGO, lpfc_state_show, NULL);
358 static CLASS_DEVICE_ATTR(option_rom_version, S_IRUGO,
359                                         lpfc_option_rom_version_show, NULL);
360 static CLASS_DEVICE_ATTR(num_discovered_ports, S_IRUGO,
361                                         lpfc_num_discovered_ports_show, NULL);
362 static CLASS_DEVICE_ATTR(nport_evt_cnt, S_IRUGO, lpfc_nport_evt_cnt_show, NULL);
363 static CLASS_DEVICE_ATTR(lpfc_drvr_version, S_IRUGO, lpfc_drvr_version_show,
364                          NULL);
365 static CLASS_DEVICE_ATTR(management_version, S_IRUGO, management_version_show,
366                          NULL);
367 static CLASS_DEVICE_ATTR(issue_lip, S_IWUSR, NULL, lpfc_issue_lip);
368 static CLASS_DEVICE_ATTR(board_online, S_IRUGO | S_IWUSR,
369                          lpfc_board_online_show, lpfc_board_online_store);
370
371
372 /*
373 # lpfc_log_verbose: Only turn this flag on if you are willing to risk being
374 # deluged with LOTS of information.
375 # You can set a bit mask to record specific types of verbose messages:
376 #
377 # LOG_ELS                       0x1        ELS events
378 # LOG_DISCOVERY                 0x2        Link discovery events
379 # LOG_MBOX                      0x4        Mailbox events
380 # LOG_INIT                      0x8        Initialization events
381 # LOG_LINK_EVENT                0x10       Link events
382 # LOG_IP                        0x20       IP traffic history
383 # LOG_FCP                       0x40       FCP traffic history
384 # LOG_NODE                      0x80       Node table events
385 # LOG_MISC                      0x400      Miscellaneous events
386 # LOG_SLI                       0x800      SLI events
387 # LOG_CHK_COND                  0x1000     FCP Check condition flag
388 # LOG_LIBDFC                    0x2000     LIBDFC events
389 # LOG_ALL_MSG                   0xffff     LOG all messages
390 */
391 LPFC_ATTR_RW(log_verbose, 0x0, 0x0, 0xffff, "Verbose logging bit-mask");
392
393 /*
394 # lun_queue_depth:  This parameter is used to limit the number of outstanding
395 # commands per FCP LUN. Value range is [1,128]. Default value is 30.
396 */
397 LPFC_ATTR_R(lun_queue_depth, 30, 1, 128,
398             "Max number of FCP commands we can queue to a specific LUN");
399
400 /*
401 # Some disk devices have a "select ID" or "select Target" capability.
402 # From a protocol standpoint "select ID" usually means select the
403 # Fibre channel "ALPA".  In the FC-AL Profile there is an "informative
404 # annex" which contains a table that maps a "select ID" (a number
405 # between 0 and 7F) to an ALPA.  By default, for compatibility with
406 # older drivers, the lpfc driver scans this table from low ALPA to high
407 # ALPA.
408 #
409 # Turning on the scan-down variable (on  = 1, off = 0) will
410 # cause the lpfc driver to use an inverted table, effectively
411 # scanning ALPAs from high to low. Value range is [0,1]. Default value is 1.
412 #
413 # (Note: This "select ID" functionality is a LOOP ONLY characteristic
414 # and will not work across a fabric. Also this parameter will take
415 # effect only in the case when ALPA map is not available.)
416 */
417 LPFC_ATTR_R(scan_down, 1, 0, 1,
418              "Start scanning for devices from highest ALPA to lowest");
419
420 /*
421 # lpfc_nodev_tmo: If set, it will hold all I/O errors on devices that disappear
422 # until the timer expires. Value range is [0,255]. Default value is 20.
423 # NOTE: this MUST be less then the SCSI Layer command timeout - 1.
424 */
425 LPFC_ATTR_RW(nodev_tmo, 30, 0, 255,
426              "Seconds driver will hold I/O waiting for a device to come back");
427
428 /*
429 # lpfc_topology:  link topology for init link
430 #            0x0  = attempt loop mode then point-to-point
431 #            0x02 = attempt point-to-point mode only
432 #            0x04 = attempt loop mode only
433 #            0x06 = attempt point-to-point mode then loop
434 # Set point-to-point mode if you want to run as an N_Port.
435 # Set loop mode if you want to run as an NL_Port. Value range is [0,0x6].
436 # Default value is 0.
437 */
438 LPFC_ATTR_R(topology, 0, 0, 6, "Select Fibre Channel topology");
439
440 /*
441 # lpfc_link_speed: Link speed selection for initializing the Fibre Channel
442 # connection.
443 #       0  = auto select (default)
444 #       1  = 1 Gigabaud
445 #       2  = 2 Gigabaud
446 #       4  = 4 Gigabaud
447 # Value range is [0,4]. Default value is 0.
448 */
449 LPFC_ATTR_R(link_speed, 0, 0, 4, "Select link speed");
450
451 /*
452 # lpfc_fcp_class:  Determines FC class to use for the FCP protocol.
453 # Value range is [2,3]. Default value is 3.
454 */
455 LPFC_ATTR_R(fcp_class, 3, 2, 3,
456              "Select Fibre Channel class of service for FCP sequences");
457
458 /*
459 # lpfc_use_adisc: Use ADISC for FCP rediscovery instead of PLOGI. Value range
460 # is [0,1]. Default value is 0.
461 */
462 LPFC_ATTR_RW(use_adisc, 0, 0, 1,
463              "Use ADISC on rediscovery to authenticate FCP devices");
464
465 /*
466 # lpfc_ack0: Use ACK0, instead of ACK1 for class 2 acknowledgement. Value
467 # range is [0,1]. Default value is 0.
468 */
469 LPFC_ATTR_R(ack0, 0, 0, 1, "Enable ACK0 support");
470
471 /*
472 # lpfc_cr_delay & lpfc_cr_count: Default values for I/O colaesing
473 # cr_delay (msec) or cr_count outstanding commands. cr_delay can take
474 # value [0,63]. cr_count can take value [0,255]. Default value of cr_delay
475 # is 0. Default value of cr_count is 1. The cr_count feature is disabled if
476 # cr_delay is set to 0.
477 */
478 static int lpfc_cr_delay = 0;
479 module_param(lpfc_cr_delay, int , 0);
480 MODULE_PARM_DESC(lpfc_cr_delay, "A count of milliseconds after which an "
481                 "interrupt response is generated");
482
483 static int lpfc_cr_count = 1;
484 module_param(lpfc_cr_count, int, 0);
485 MODULE_PARM_DESC(lpfc_cr_count, "A count of I/O completions after which an "
486                 "interrupt response is generated");
487
488 /*
489 # lpfc_fdmi_on: controls FDMI support.
490 #       0 = no FDMI support
491 #       1 = support FDMI without attribute of hostname
492 #       2 = support FDMI with attribute of hostname
493 # Value range [0,2]. Default value is 0.
494 */
495 LPFC_ATTR_RW(fdmi_on, 0, 0, 2, "Enable FDMI support");
496
497 /*
498 # Specifies the maximum number of ELS cmds we can have outstanding (for
499 # discovery). Value range is [1,64]. Default value = 32.
500 */
501 static int lpfc_discovery_threads = 32;
502 module_param(lpfc_discovery_threads, int, 0);
503 MODULE_PARM_DESC(lpfc_discovery_threads, "Maximum number of ELS commands "
504                  "during discovery");
505
506 /*
507 # lpfc_max_luns: maximum number of LUNs per target driver will support
508 # Value range is [1,32768]. Default value is 256.
509 # NOTE: The SCSI layer will scan each target for this many luns
510 */
511 LPFC_ATTR_R(max_luns, 256, 1, 32768,
512              "Maximum number of LUNs per target driver will support");
513
514 struct class_device_attribute *lpfc_host_attrs[] = {
515         &class_device_attr_info,
516         &class_device_attr_serialnum,
517         &class_device_attr_modeldesc,
518         &class_device_attr_modelname,
519         &class_device_attr_programtype,
520         &class_device_attr_portnum,
521         &class_device_attr_fwrev,
522         &class_device_attr_hdw,
523         &class_device_attr_option_rom_version,
524         &class_device_attr_state,
525         &class_device_attr_num_discovered_ports,
526         &class_device_attr_lpfc_drvr_version,
527         &class_device_attr_lpfc_log_verbose,
528         &class_device_attr_lpfc_lun_queue_depth,
529         &class_device_attr_lpfc_nodev_tmo,
530         &class_device_attr_lpfc_fcp_class,
531         &class_device_attr_lpfc_use_adisc,
532         &class_device_attr_lpfc_ack0,
533         &class_device_attr_lpfc_topology,
534         &class_device_attr_lpfc_scan_down,
535         &class_device_attr_lpfc_link_speed,
536         &class_device_attr_lpfc_fdmi_on,
537         &class_device_attr_lpfc_max_luns,
538         &class_device_attr_nport_evt_cnt,
539         &class_device_attr_management_version,
540         &class_device_attr_issue_lip,
541         &class_device_attr_board_online,
542         NULL,
543 };
544
545 static ssize_t
546 sysfs_ctlreg_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
547 {
548         size_t buf_off;
549         struct Scsi_Host *host = class_to_shost(container_of(kobj,
550                                              struct class_device, kobj));
551         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
552
553         if ((off + count) > FF_REG_AREA_SIZE)
554                 return -ERANGE;
555
556         if (count == 0) return 0;
557
558         if (off % 4 || count % 4 || (unsigned long)buf % 4)
559                 return -EINVAL;
560
561         spin_lock_irq(phba->host->host_lock);
562
563         if (!(phba->fc_flag & FC_OFFLINE_MODE)) {
564                 spin_unlock_irq(phba->host->host_lock);
565                 return -EPERM;
566         }
567
568         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t))
569                 writel(*((uint32_t *)(buf + buf_off)),
570                        phba->ctrl_regs_memmap_p + off + buf_off);
571
572         spin_unlock_irq(phba->host->host_lock);
573
574         return count;
575 }
576
577 static ssize_t
578 sysfs_ctlreg_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
579 {
580         size_t buf_off;
581         uint32_t * tmp_ptr;
582         struct Scsi_Host *host = class_to_shost(container_of(kobj,
583                                              struct class_device, kobj));
584         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
585
586         if (off > FF_REG_AREA_SIZE)
587                 return -ERANGE;
588
589         if ((off + count) > FF_REG_AREA_SIZE)
590                 count = FF_REG_AREA_SIZE - off;
591
592         if (count == 0) return 0;
593
594         if (off % 4 || count % 4 || (unsigned long)buf % 4)
595                 return -EINVAL;
596
597         spin_lock_irq(phba->host->host_lock);
598
599         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t)) {
600                 tmp_ptr = (uint32_t *)(buf + buf_off);
601                 *tmp_ptr = readl(phba->ctrl_regs_memmap_p + off + buf_off);
602         }
603
604         spin_unlock_irq(phba->host->host_lock);
605
606         return count;
607 }
608
609 static struct bin_attribute sysfs_ctlreg_attr = {
610         .attr = {
611                 .name = "ctlreg",
612                 .mode = S_IRUSR | S_IWUSR,
613                 .owner = THIS_MODULE,
614         },
615         .size = 256,
616         .read = sysfs_ctlreg_read,
617         .write = sysfs_ctlreg_write,
618 };
619
620
621 static void
622 sysfs_mbox_idle (struct lpfc_hba * phba)
623 {
624         phba->sysfs_mbox.state = SMBOX_IDLE;
625         phba->sysfs_mbox.offset = 0;
626
627         if (phba->sysfs_mbox.mbox) {
628                 mempool_free(phba->sysfs_mbox.mbox,
629                              phba->mbox_mem_pool);
630                 phba->sysfs_mbox.mbox = NULL;
631         }
632 }
633
634 static ssize_t
635 sysfs_mbox_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
636 {
637         struct Scsi_Host * host =
638                 class_to_shost(container_of(kobj, struct class_device, kobj));
639         struct lpfc_hba * phba = (struct lpfc_hba*)host->hostdata[0];
640         struct lpfcMboxq * mbox = NULL;
641
642         if ((count + off) > MAILBOX_CMD_SIZE)
643                 return -ERANGE;
644
645         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
646                 return -EINVAL;
647
648         if (count == 0)
649                 return 0;
650
651         if (off == 0) {
652                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
653                 if (!mbox)
654                         return -ENOMEM;
655
656         }
657
658         spin_lock_irq(host->host_lock);
659
660         if (off == 0) {
661                 if (phba->sysfs_mbox.mbox)
662                         mempool_free(mbox, phba->mbox_mem_pool);
663                 else
664                         phba->sysfs_mbox.mbox = mbox;
665                 phba->sysfs_mbox.state = SMBOX_WRITING;
666         } else {
667                 if (phba->sysfs_mbox.state  != SMBOX_WRITING ||
668                     phba->sysfs_mbox.offset != off           ||
669                     phba->sysfs_mbox.mbox   == NULL ) {
670                         sysfs_mbox_idle(phba);
671                         spin_unlock_irq(host->host_lock);
672                         return -EINVAL;
673                 }
674         }
675
676         memcpy((uint8_t *) & phba->sysfs_mbox.mbox->mb + off,
677                buf, count);
678
679         phba->sysfs_mbox.offset = off + count;
680
681         spin_unlock_irq(host->host_lock);
682
683         return count;
684 }
685
686 static ssize_t
687 sysfs_mbox_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
688 {
689         struct Scsi_Host *host =
690                 class_to_shost(container_of(kobj, struct class_device,
691                                             kobj));
692         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata[0];
693         int rc;
694
695         if (off > sizeof(MAILBOX_t))
696                 return -ERANGE;
697
698         if ((count + off) > sizeof(MAILBOX_t))
699                 count = sizeof(MAILBOX_t) - off;
700
701         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
702                 return -EINVAL;
703
704         if (off && count == 0)
705                 return 0;
706
707         spin_lock_irq(phba->host->host_lock);
708
709         if (off == 0 &&
710             phba->sysfs_mbox.state  == SMBOX_WRITING &&
711             phba->sysfs_mbox.offset >= 2 * sizeof(uint32_t)) {
712
713                 switch (phba->sysfs_mbox.mbox->mb.mbxCommand) {
714                         /* Offline only */
715                 case MBX_WRITE_NV:
716                 case MBX_INIT_LINK:
717                 case MBX_DOWN_LINK:
718                 case MBX_CONFIG_LINK:
719                 case MBX_CONFIG_RING:
720                 case MBX_RESET_RING:
721                 case MBX_UNREG_LOGIN:
722                 case MBX_CLEAR_LA:
723                 case MBX_DUMP_CONTEXT:
724                 case MBX_RUN_DIAGS:
725                 case MBX_RESTART:
726                 case MBX_FLASH_WR_ULA:
727                 case MBX_SET_MASK:
728                 case MBX_SET_SLIM:
729                 case MBX_SET_DEBUG:
730                         if (!(phba->fc_flag & FC_OFFLINE_MODE)) {
731                                 printk(KERN_WARNING "mbox_read:Command 0x%x "
732                                        "is illegal in on-line state\n",
733                                        phba->sysfs_mbox.mbox->mb.mbxCommand);
734                                 sysfs_mbox_idle(phba);
735                                 spin_unlock_irq(phba->host->host_lock);
736                                 return -EPERM;
737                         }
738                 case MBX_LOAD_SM:
739                 case MBX_READ_NV:
740                 case MBX_READ_CONFIG:
741                 case MBX_READ_RCONFIG:
742                 case MBX_READ_STATUS:
743                 case MBX_READ_XRI:
744                 case MBX_READ_REV:
745                 case MBX_READ_LNK_STAT:
746                 case MBX_DUMP_MEMORY:
747                 case MBX_DOWN_LOAD:
748                 case MBX_UPDATE_CFG:
749                 case MBX_LOAD_AREA:
750                 case MBX_LOAD_EXP_ROM:
751                         break;
752                 case MBX_READ_SPARM64:
753                 case MBX_READ_LA:
754                 case MBX_READ_LA64:
755                 case MBX_REG_LOGIN:
756                 case MBX_REG_LOGIN64:
757                 case MBX_CONFIG_PORT:
758                 case MBX_RUN_BIU_DIAG:
759                         printk(KERN_WARNING "mbox_read: Illegal Command 0x%x\n",
760                                phba->sysfs_mbox.mbox->mb.mbxCommand);
761                         sysfs_mbox_idle(phba);
762                         spin_unlock_irq(phba->host->host_lock);
763                         return -EPERM;
764                 default:
765                         printk(KERN_WARNING "mbox_read: Unknown Command 0x%x\n",
766                                phba->sysfs_mbox.mbox->mb.mbxCommand);
767                         sysfs_mbox_idle(phba);
768                         spin_unlock_irq(phba->host->host_lock);
769                         return -EPERM;
770                 }
771
772                 if ((phba->fc_flag & FC_OFFLINE_MODE) ||
773                     (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE))){
774
775                         spin_unlock_irq(phba->host->host_lock);
776                         rc = lpfc_sli_issue_mbox (phba,
777                                                   phba->sysfs_mbox.mbox,
778                                                   MBX_POLL);
779                         spin_lock_irq(phba->host->host_lock);
780
781                 } else {
782                         spin_unlock_irq(phba->host->host_lock);
783                         rc = lpfc_sli_issue_mbox_wait (phba,
784                                                        phba->sysfs_mbox.mbox,
785                                                        phba->fc_ratov * 2);
786                         spin_lock_irq(phba->host->host_lock);
787                 }
788
789                 if (rc != MBX_SUCCESS) {
790                         sysfs_mbox_idle(phba);
791                         spin_unlock_irq(host->host_lock);
792                         return -ENODEV;
793                 }
794                 phba->sysfs_mbox.state = SMBOX_READING;
795         }
796         else if (phba->sysfs_mbox.offset != off ||
797                  phba->sysfs_mbox.state  != SMBOX_READING) {
798                 printk(KERN_WARNING  "mbox_read: Bad State\n");
799                 sysfs_mbox_idle(phba);
800                 spin_unlock_irq(host->host_lock);
801                 return -EINVAL;
802         }
803
804         memcpy(buf, (uint8_t *) & phba->sysfs_mbox.mbox->mb + off, count);
805
806         phba->sysfs_mbox.offset = off + count;
807
808         if (phba->sysfs_mbox.offset == sizeof(MAILBOX_t))
809                 sysfs_mbox_idle(phba);
810
811         spin_unlock_irq(phba->host->host_lock);
812
813         return count;
814 }
815
816 static struct bin_attribute sysfs_mbox_attr = {
817         .attr = {
818                 .name = "mbox",
819                 .mode = S_IRUSR | S_IWUSR,
820                 .owner = THIS_MODULE,
821         },
822         .size = sizeof(MAILBOX_t),
823         .read = sysfs_mbox_read,
824         .write = sysfs_mbox_write,
825 };
826
827 int
828 lpfc_alloc_sysfs_attr(struct lpfc_hba *phba)
829 {
830         struct Scsi_Host *host = phba->host;
831         int error;
832
833         error = sysfs_create_bin_file(&host->shost_classdev.kobj,
834                                                         &sysfs_ctlreg_attr);
835         if (error)
836                 goto out;
837
838         error = sysfs_create_bin_file(&host->shost_classdev.kobj,
839                                                         &sysfs_mbox_attr);
840         if (error)
841                 goto out_remove_ctlreg_attr;
842
843         return 0;
844 out_remove_ctlreg_attr:
845         sysfs_remove_bin_file(&host->shost_classdev.kobj, &sysfs_ctlreg_attr);
846 out:
847         return error;
848 }
849
850 void
851 lpfc_free_sysfs_attr(struct lpfc_hba *phba)
852 {
853         struct Scsi_Host *host = phba->host;
854
855         sysfs_remove_bin_file(&host->shost_classdev.kobj, &sysfs_mbox_attr);
856         sysfs_remove_bin_file(&host->shost_classdev.kobj, &sysfs_ctlreg_attr);
857 }
858
859
860 /*
861  * Dynamic FC Host Attributes Support
862  */
863
864 static void
865 lpfc_get_host_port_id(struct Scsi_Host *shost)
866 {
867         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata[0];
868         /* note: fc_myDID already in cpu endianness */
869         fc_host_port_id(shost) = phba->fc_myDID;
870 }
871
872 static void
873 lpfc_get_host_port_type(struct Scsi_Host *shost)
874 {
875         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata[0];
876
877         spin_lock_irq(shost->host_lock);
878
879         if (phba->hba_state == LPFC_HBA_READY) {
880                 if (phba->fc_topology == TOPOLOGY_LOOP) {
881                         if (phba->fc_flag & FC_PUBLIC_LOOP)
882                                 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
883                         else
884                                 fc_host_port_type(shost) = FC_PORTTYPE_LPORT;
885                 } else {
886                         if (phba->fc_flag & FC_FABRIC)
887                                 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
888                         else
889                                 fc_host_port_type(shost) = FC_PORTTYPE_PTP;
890                 }
891         } else
892                 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
893
894         spin_unlock_irq(shost->host_lock);
895 }
896
897 static void
898 lpfc_get_host_port_state(struct Scsi_Host *shost)
899 {
900         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata[0];
901
902         spin_lock_irq(shost->host_lock);
903
904         if (phba->fc_flag & FC_OFFLINE_MODE)
905                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
906         else {
907                 switch (phba->hba_state) {
908                 case LPFC_INIT_START:
909                 case LPFC_INIT_MBX_CMDS:
910                 case LPFC_LINK_DOWN:
911                         fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
912                         break;
913                 case LPFC_LINK_UP:
914                 case LPFC_LOCAL_CFG_LINK:
915                 case LPFC_FLOGI:
916                 case LPFC_FABRIC_CFG_LINK:
917                 case LPFC_NS_REG:
918                 case LPFC_NS_QRY:
919                 case LPFC_BUILD_DISC_LIST:
920                 case LPFC_DISC_AUTH:
921                 case LPFC_CLEAR_LA:
922                 case LPFC_HBA_READY:
923                         /* Links up, beyond this port_type reports state */
924                         fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
925                         break;
926                 case LPFC_HBA_ERROR:
927                         fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
928                         break;
929                 default:
930                         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
931                         break;
932                 }
933         }
934
935         spin_unlock_irq(shost->host_lock);
936 }
937
938 static void
939 lpfc_get_host_speed(struct Scsi_Host *shost)
940 {
941         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata[0];
942
943         spin_lock_irq(shost->host_lock);
944
945         if (phba->hba_state == LPFC_HBA_READY) {
946                 switch(phba->fc_linkspeed) {
947                         case LA_1GHZ_LINK:
948                                 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
949                         break;
950                         case LA_2GHZ_LINK:
951                                 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
952                         break;
953                         case LA_4GHZ_LINK:
954                                 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
955                         break;
956                         default:
957                                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
958                         break;
959                 }
960         }
961
962         spin_unlock_irq(shost->host_lock);
963 }
964
965 static void
966 lpfc_get_host_fabric_name (struct Scsi_Host *shost)
967 {
968         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata[0];
969         u64 nodename;
970
971         spin_lock_irq(shost->host_lock);
972
973         if ((phba->fc_flag & FC_FABRIC) ||
974             ((phba->fc_topology == TOPOLOGY_LOOP) &&
975              (phba->fc_flag & FC_PUBLIC_LOOP)))
976                 memcpy(&nodename, &phba->fc_fabparam.nodeName, sizeof(u64));
977         else
978                 /* fabric is local port if there is no F/FL_Port */
979                 memcpy(&nodename, &phba->fc_nodename, sizeof(u64));
980
981         spin_unlock_irq(shost->host_lock);
982
983         fc_host_fabric_name(shost) = be64_to_cpu(nodename);
984 }
985
986
987 static struct fc_host_statistics *
988 lpfc_get_stats(struct Scsi_Host *shost)
989 {
990         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
991         struct lpfc_sli *psli = &phba->sli;
992         struct fc_host_statistics *hs = &phba->link_stats;
993         LPFC_MBOXQ_t *pmboxq;
994         MAILBOX_t *pmb;
995         int rc=0;
996
997         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
998         if (!pmboxq)
999                 return NULL;
1000         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1001
1002         pmb = &pmboxq->mb;
1003         pmb->mbxCommand = MBX_READ_STATUS;
1004         pmb->mbxOwner = OWN_HOST;
1005         pmboxq->context1 = NULL;
1006
1007         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1008             (!(psli->sli_flag & LPFC_SLI2_ACTIVE))){
1009                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1010         } else
1011                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1012
1013         if (rc != MBX_SUCCESS) {
1014                 if (pmboxq) {
1015                         if (rc == MBX_TIMEOUT)
1016                                 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1017                         else
1018                                 mempool_free( pmboxq, phba->mbox_mem_pool);
1019                 }
1020                 return NULL;
1021         }
1022
1023         memset(hs, 0, sizeof (struct fc_host_statistics));
1024
1025         hs->tx_frames = pmb->un.varRdStatus.xmitFrameCnt;
1026         hs->tx_words = (pmb->un.varRdStatus.xmitByteCnt * 256);
1027         hs->rx_frames = pmb->un.varRdStatus.rcvFrameCnt;
1028         hs->rx_words = (pmb->un.varRdStatus.rcvByteCnt * 256);
1029
1030         memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1031         pmb->mbxCommand = MBX_READ_LNK_STAT;
1032         pmb->mbxOwner = OWN_HOST;
1033         pmboxq->context1 = NULL;
1034
1035         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1036             (!(psli->sli_flag & LPFC_SLI2_ACTIVE))) {
1037                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1038         } else
1039                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1040
1041         if (rc != MBX_SUCCESS) {
1042                 if (pmboxq) {
1043                         if (rc == MBX_TIMEOUT)
1044                                 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1045                         else
1046                                 mempool_free( pmboxq, phba->mbox_mem_pool);
1047                 }
1048                 return NULL;
1049         }
1050
1051         hs->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
1052         hs->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
1053         hs->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
1054         hs->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
1055         hs->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
1056         hs->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
1057         hs->error_frames = pmb->un.varRdLnk.crcCnt;
1058
1059         if (phba->fc_topology == TOPOLOGY_LOOP) {
1060                 hs->lip_count = (phba->fc_eventTag >> 1);
1061                 hs->nos_count = -1;
1062         } else {
1063                 hs->lip_count = -1;
1064                 hs->nos_count = (phba->fc_eventTag >> 1);
1065         }
1066
1067         hs->dumped_frames = -1;
1068
1069 /* FIX ME */
1070         /*hs->SecondsSinceLastReset = (jiffies - lpfc_loadtime) / HZ;*/
1071
1072         return hs;
1073 }
1074
1075
1076 /*
1077  * The LPFC driver treats linkdown handling as target loss events so there
1078  * are no sysfs handlers for link_down_tmo.
1079  */
1080 static void
1081 lpfc_get_starget_port_id(struct scsi_target *starget)
1082 {
1083         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1084         struct lpfc_hba *phba = (struct lpfc_hba *) shost->hostdata[0];
1085         uint32_t did = -1;
1086         struct lpfc_nodelist *ndlp = NULL;
1087
1088         spin_lock_irq(shost->host_lock);
1089         /* Search the mapped list for this target ID */
1090         list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1091                 if (starget->id == ndlp->nlp_sid) {
1092                         did = ndlp->nlp_DID;
1093                         break;
1094                 }
1095         }
1096         spin_unlock_irq(shost->host_lock);
1097
1098         fc_starget_port_id(starget) = did;
1099 }
1100
1101 static void
1102 lpfc_get_starget_node_name(struct scsi_target *starget)
1103 {
1104         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1105         struct lpfc_hba *phba = (struct lpfc_hba *) shost->hostdata[0];
1106         uint64_t node_name = 0;
1107         struct lpfc_nodelist *ndlp = NULL;
1108
1109         spin_lock_irq(shost->host_lock);
1110         /* Search the mapped list for this target ID */
1111         list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1112                 if (starget->id == ndlp->nlp_sid) {
1113                         memcpy(&node_name, &ndlp->nlp_nodename,
1114                                                 sizeof(struct lpfc_name));
1115                         break;
1116                 }
1117         }
1118         spin_unlock_irq(shost->host_lock);
1119
1120         fc_starget_node_name(starget) = be64_to_cpu(node_name);
1121 }
1122
1123 static void
1124 lpfc_get_starget_port_name(struct scsi_target *starget)
1125 {
1126         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1127         struct lpfc_hba *phba = (struct lpfc_hba *) shost->hostdata[0];
1128         uint64_t port_name = 0;
1129         struct lpfc_nodelist *ndlp = NULL;
1130
1131         spin_lock_irq(shost->host_lock);
1132         /* Search the mapped list for this target ID */
1133         list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1134                 if (starget->id == ndlp->nlp_sid) {
1135                         memcpy(&port_name, &ndlp->nlp_portname,
1136                                                 sizeof(struct lpfc_name));
1137                         break;
1138                 }
1139         }
1140         spin_unlock_irq(shost->host_lock);
1141
1142         fc_starget_port_name(starget) = be64_to_cpu(port_name);
1143 }
1144
1145 static void
1146 lpfc_get_rport_loss_tmo(struct fc_rport *rport)
1147 {
1148         /*
1149          * Return the driver's global value for device loss timeout plus
1150          * five seconds to allow the driver's nodev timer to run.
1151          */
1152         rport->dev_loss_tmo = lpfc_nodev_tmo + 5;
1153 }
1154
1155 static void
1156 lpfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
1157 {
1158         /*
1159          * The driver doesn't have a per-target timeout setting.  Set
1160          * this value globally. lpfc_nodev_tmo should be greater then 0.
1161          */
1162         if (timeout)
1163                 lpfc_nodev_tmo = timeout;
1164         else
1165                 lpfc_nodev_tmo = 1;
1166         rport->dev_loss_tmo = lpfc_nodev_tmo + 5;
1167 }
1168
1169
1170 #define lpfc_rport_show_function(field, format_string, sz, cast)        \
1171 static ssize_t                                                          \
1172 lpfc_show_rport_##field (struct class_device *cdev, char *buf)          \
1173 {                                                                       \
1174         struct fc_rport *rport = transport_class_to_rport(cdev);        \
1175         struct lpfc_rport_data *rdata = rport->hostdata;                \
1176         return snprintf(buf, sz, format_string,                         \
1177                 (rdata->target) ? cast rdata->target->field : 0);       \
1178 }
1179
1180 #define lpfc_rport_rd_attr(field, format_string, sz)                    \
1181         lpfc_rport_show_function(field, format_string, sz, )            \
1182 static FC_RPORT_ATTR(field, S_IRUGO, lpfc_show_rport_##field, NULL)
1183
1184
1185 struct fc_function_template lpfc_transport_functions = {
1186         /* fixed attributes the driver supports */
1187         .show_host_node_name = 1,
1188         .show_host_port_name = 1,
1189         .show_host_supported_classes = 1,
1190         .show_host_supported_fc4s = 1,
1191         .show_host_symbolic_name = 1,
1192         .show_host_supported_speeds = 1,
1193         .show_host_maxframe_size = 1,
1194
1195         /* dynamic attributes the driver supports */
1196         .get_host_port_id = lpfc_get_host_port_id,
1197         .show_host_port_id = 1,
1198
1199         .get_host_port_type = lpfc_get_host_port_type,
1200         .show_host_port_type = 1,
1201
1202         .get_host_port_state = lpfc_get_host_port_state,
1203         .show_host_port_state = 1,
1204
1205         /* active_fc4s is shown but doesn't change (thus no get function) */
1206         .show_host_active_fc4s = 1,
1207
1208         .get_host_speed = lpfc_get_host_speed,
1209         .show_host_speed = 1,
1210
1211         .get_host_fabric_name = lpfc_get_host_fabric_name,
1212         .show_host_fabric_name = 1,
1213
1214         /*
1215          * The LPFC driver treats linkdown handling as target loss events
1216          * so there are no sysfs handlers for link_down_tmo.
1217          */
1218
1219         .get_fc_host_stats = lpfc_get_stats,
1220
1221         /* the LPFC driver doesn't support resetting stats yet */
1222
1223         .dd_fcrport_size = sizeof(struct lpfc_rport_data),
1224         .show_rport_maxframe_size = 1,
1225         .show_rport_supported_classes = 1,
1226
1227         .get_rport_dev_loss_tmo = lpfc_get_rport_loss_tmo,
1228         .set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
1229         .show_rport_dev_loss_tmo = 1,
1230
1231         .get_starget_port_id  = lpfc_get_starget_port_id,
1232         .show_starget_port_id = 1,
1233
1234         .get_starget_node_name = lpfc_get_starget_node_name,
1235         .show_starget_node_name = 1,
1236
1237         .get_starget_port_name = lpfc_get_starget_port_name,
1238         .show_starget_port_name = 1,
1239 };
1240
1241 void
1242 lpfc_get_cfgparam(struct lpfc_hba *phba)
1243 {
1244         phba->cfg_log_verbose = lpfc_log_verbose;
1245         phba->cfg_cr_delay = lpfc_cr_delay;
1246         phba->cfg_cr_count = lpfc_cr_count;
1247         phba->cfg_lun_queue_depth = lpfc_lun_queue_depth;
1248         phba->cfg_fcp_class = lpfc_fcp_class;
1249         phba->cfg_use_adisc = lpfc_use_adisc;
1250         phba->cfg_ack0 = lpfc_ack0;
1251         phba->cfg_topology = lpfc_topology;
1252         phba->cfg_scan_down = lpfc_scan_down;
1253         phba->cfg_nodev_tmo = lpfc_nodev_tmo;
1254         phba->cfg_link_speed = lpfc_link_speed;
1255         phba->cfg_fdmi_on = lpfc_fdmi_on;
1256         phba->cfg_discovery_threads = lpfc_discovery_threads;
1257         phba->cfg_max_luns = lpfc_max_luns;
1258
1259         /*
1260          * The total number of segments is the configuration value plus 2
1261          * since the IOCB need a command and response bde.
1262          */
1263         phba->cfg_sg_seg_cnt = LPFC_SG_SEG_CNT + 2;
1264
1265         /*
1266          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
1267          * used to create the sg_dma_buf_pool must be dynamically calculated
1268          */
1269         phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
1270                         sizeof(struct fcp_rsp) +
1271                         (phba->cfg_sg_seg_cnt * sizeof(struct ulp_bde64));
1272
1273         switch (phba->pcidev->device) {
1274         case PCI_DEVICE_ID_LP101:
1275         case PCI_DEVICE_ID_BSMB:
1276         case PCI_DEVICE_ID_ZSMB:
1277                 phba->cfg_hba_queue_depth = LPFC_LP101_HBA_Q_DEPTH;
1278                 break;
1279         case PCI_DEVICE_ID_RFLY:
1280         case PCI_DEVICE_ID_PFLY:
1281         case PCI_DEVICE_ID_BMID:
1282         case PCI_DEVICE_ID_ZMID:
1283         case PCI_DEVICE_ID_TFLY:
1284                 phba->cfg_hba_queue_depth = LPFC_LC_HBA_Q_DEPTH;
1285                 break;
1286         default:
1287                 phba->cfg_hba_queue_depth = LPFC_DFT_HBA_Q_DEPTH;
1288         }
1289         return;
1290 }