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