]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/lpfc/lpfc_attr.c
[SCSI] lpfc: NPIV: split ports
[linux-2.6-omap-h63xx.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-2007 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/delay.h>
24 #include <linux/pci.h>
25 #include <linux/interrupt.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_transport_fc.h>
32
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_disc.h"
36 #include "lpfc_scsi.h"
37 #include "lpfc.h"
38 #include "lpfc_logmsg.h"
39 #include "lpfc_version.h"
40 #include "lpfc_compat.h"
41 #include "lpfc_crtn.h"
42
43 #define LPFC_DEF_DEVLOSS_TMO 30
44 #define LPFC_MIN_DEVLOSS_TMO 1
45 #define LPFC_MAX_DEVLOSS_TMO 255
46
47 static void
48 lpfc_jedec_to_ascii(int incr, char hdw[])
49 {
50         int i, j;
51         for (i = 0; i < 8; i++) {
52                 j = (incr & 0xf);
53                 if (j <= 9)
54                         hdw[7 - i] = 0x30 +  j;
55                  else
56                         hdw[7 - i] = 0x61 + j - 10;
57                 incr = (incr >> 4);
58         }
59         hdw[8] = 0;
60         return;
61 }
62
63 static ssize_t
64 lpfc_drvr_version_show(struct class_device *cdev, char *buf)
65 {
66         return snprintf(buf, PAGE_SIZE, LPFC_MODULE_DESC "\n");
67 }
68
69 static ssize_t
70 management_version_show(struct class_device *cdev, char *buf)
71 {
72         return snprintf(buf, PAGE_SIZE, DFC_API_VERSION "\n");
73 }
74
75 static ssize_t
76 lpfc_info_show(struct class_device *cdev, char *buf)
77 {
78         struct Scsi_Host *host = class_to_shost(cdev);
79
80         return snprintf(buf, PAGE_SIZE, "%s\n",lpfc_info(host));
81 }
82
83 static ssize_t
84 lpfc_serialnum_show(struct class_device *cdev, char *buf)
85 {
86         struct Scsi_Host  *shost = class_to_shost(cdev);
87         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
88         struct lpfc_hba   *phba = vport->phba;
89
90         return snprintf(buf, PAGE_SIZE, "%s\n",phba->SerialNumber);
91 }
92
93 static ssize_t
94 lpfc_modeldesc_show(struct class_device *cdev, char *buf)
95 {
96         struct Scsi_Host  *shost = class_to_shost(cdev);
97         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
98         struct lpfc_hba   *phba = vport->phba;
99
100         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelDesc);
101 }
102
103 static ssize_t
104 lpfc_modelname_show(struct class_device *cdev, char *buf)
105 {
106         struct Scsi_Host  *shost = class_to_shost(cdev);
107         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
108         struct lpfc_hba   *phba = vport->phba;
109
110         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelName);
111 }
112
113 static ssize_t
114 lpfc_programtype_show(struct class_device *cdev, char *buf)
115 {
116         struct Scsi_Host  *shost = class_to_shost(cdev);
117         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
118         struct lpfc_hba   *phba = vport->phba;
119
120         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ProgramType);
121 }
122
123 static ssize_t
124 lpfc_vportnum_show(struct class_device *cdev, char *buf)
125 {
126         struct Scsi_Host  *shost = class_to_shost(cdev);
127         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
128         struct lpfc_hba   *phba = vport->phba;
129
130         return snprintf(buf, PAGE_SIZE, "%s\n",phba->Port);
131 }
132
133 static ssize_t
134 lpfc_fwrev_show(struct class_device *cdev, char *buf)
135 {
136         struct Scsi_Host  *shost = class_to_shost(cdev);
137         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
138         struct lpfc_hba   *phba = vport->phba;
139         char fwrev[32];
140
141         lpfc_decode_firmware_rev(phba, fwrev, 1);
142         return snprintf(buf, PAGE_SIZE, "%s\n",fwrev);
143 }
144
145 static ssize_t
146 lpfc_hdw_show(struct class_device *cdev, char *buf)
147 {
148         char hdw[9];
149         struct Scsi_Host  *shost = class_to_shost(cdev);
150         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
151         struct lpfc_hba   *phba = vport->phba;
152         lpfc_vpd_t *vp = &phba->vpd;
153
154         lpfc_jedec_to_ascii(vp->rev.biuRev, hdw);
155         return snprintf(buf, PAGE_SIZE, "%s\n", hdw);
156 }
157 static ssize_t
158 lpfc_option_rom_version_show(struct class_device *cdev, char *buf)
159 {
160         struct Scsi_Host  *shost = class_to_shost(cdev);
161         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
162         struct lpfc_hba   *phba = vport->phba;
163
164         return snprintf(buf, PAGE_SIZE, "%s\n", phba->OptionROMVersion);
165 }
166 static ssize_t
167 lpfc_state_show(struct class_device *cdev, char *buf)
168 {
169         struct Scsi_Host  *shost = class_to_shost(cdev);
170         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
171         struct lpfc_hba   *phba = vport->phba;
172         int  len = 0;
173
174         switch (phba->link_state) {
175         case LPFC_LINK_UNKNOWN:
176         case LPFC_WARM_START:
177         case LPFC_INIT_START:
178         case LPFC_INIT_MBX_CMDS:
179         case LPFC_LINK_DOWN:
180         case LPFC_HBA_ERROR:
181                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Down");
182                 break;
183         case LPFC_LINK_UP:
184         case LPFC_CLEAR_LA:
185                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Up - \n");
186
187                 switch (vport->port_state) {
188                         len += snprintf(buf + len, PAGE_SIZE-len,
189                                         "initializing\n");
190                         break;
191                 case LPFC_LOCAL_CFG_LINK:
192                         len += snprintf(buf + len, PAGE_SIZE-len,
193                                         "configuring\n");
194                         break;
195                 case LPFC_FLOGI:
196                 case LPFC_FABRIC_CFG_LINK:
197                 case LPFC_NS_REG:
198                 case LPFC_NS_QRY:
199                 case LPFC_BUILD_DISC_LIST:
200                 case LPFC_DISC_AUTH:
201                         len += snprintf(buf + len, PAGE_SIZE - len,
202                                         "Discovery\n");
203                         break;
204                 case LPFC_VPORT_READY:
205                         len += snprintf(buf + len, PAGE_SIZE - len, "Ready\n");
206                         break;
207
208                 case LPFC_STATE_UNKNOWN:
209                         len += snprintf(buf + len, PAGE_SIZE - len,
210                                         "Unknown\n");
211                         break;
212                 }
213
214                 if (phba->fc_topology == TOPOLOGY_LOOP) {
215                         if (vport->fc_flag & FC_PUBLIC_LOOP)
216                                 len += snprintf(buf + len, PAGE_SIZE-len,
217                                                 "   Public Loop\n");
218                         else
219                                 len += snprintf(buf + len, PAGE_SIZE-len,
220                                                 "   Private Loop\n");
221                 } else {
222                         if (vport->fc_flag & FC_FABRIC)
223                                 len += snprintf(buf + len, PAGE_SIZE-len,
224                                                 "   Fabric\n");
225                         else
226                                 len += snprintf(buf + len, PAGE_SIZE-len,
227                                                 "   Point-2-Point\n");
228                 }
229         }
230
231         return len;
232 }
233
234 static ssize_t
235 lpfc_num_discovered_ports_show(struct class_device *cdev, char *buf)
236 {
237         struct Scsi_Host  *shost = class_to_shost(cdev);
238         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
239
240         return snprintf(buf, PAGE_SIZE, "%d\n",
241                         vport->fc_map_cnt + vport->fc_unmap_cnt);
242 }
243
244
245 static int
246 lpfc_issue_lip(struct Scsi_Host *shost)
247 {
248         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
249         struct lpfc_hba   *phba = vport->phba;
250         LPFC_MBOXQ_t *pmboxq;
251         int mbxstatus = MBXERR_ERROR;
252
253         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
254             (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) ||
255             (vport->port_state != LPFC_VPORT_READY))
256                 return -EPERM;
257
258         pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
259
260         if (!pmboxq)
261                 return -ENOMEM;
262
263         memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
264         pmboxq->mb.mbxCommand = MBX_DOWN_LINK;
265         pmboxq->mb.mbxOwner = OWN_HOST;
266
267         mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO * 2);
268
269         if ((mbxstatus == MBX_SUCCESS) && (pmboxq->mb.mbxStatus == 0)) {
270                 memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
271                 lpfc_init_link(phba, pmboxq, phba->cfg_topology,
272                                phba->cfg_link_speed);
273                 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
274                                                      phba->fc_ratov * 2);
275         }
276
277         lpfc_set_loopback_flag(phba);
278         if (mbxstatus == MBX_TIMEOUT)
279                 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
280         else
281                 mempool_free(pmboxq, phba->mbox_mem_pool);
282
283         if (mbxstatus == MBXERR_ERROR)
284                 return -EIO;
285
286         return 0;
287 }
288
289 static int
290 lpfc_do_offline(struct lpfc_hba *phba, uint32_t type)
291 {
292         struct completion online_compl;
293         struct lpfc_sli_ring *pring;
294         struct lpfc_sli *psli;
295         int status = 0;
296         int cnt = 0;
297         int i;
298
299         init_completion(&online_compl);
300         lpfc_workq_post_event(phba, &status, &online_compl,
301                               LPFC_EVT_OFFLINE_PREP);
302         wait_for_completion(&online_compl);
303
304         if (status != 0)
305                 return -EIO;
306
307         psli = &phba->sli;
308
309         for (i = 0; i < psli->num_rings; i++) {
310                 pring = &psli->ring[i];
311                 /* The linkdown event takes 30 seconds to timeout. */
312                 while (pring->txcmplq_cnt) {
313                         msleep(10);
314                         if (cnt++ > 3000) {
315                                 lpfc_printf_log(phba,
316                                         KERN_WARNING, LOG_INIT,
317                                         "%d:0466 Outstanding IO when "
318                                         "bringing Adapter offline\n",
319                                         phba->brd_no);
320                                 break;
321                         }
322                 }
323         }
324
325         init_completion(&online_compl);
326         lpfc_workq_post_event(phba, &status, &online_compl, type);
327         wait_for_completion(&online_compl);
328
329         if (status != 0)
330                 return -EIO;
331
332         return 0;
333 }
334
335 static int
336 lpfc_selective_reset(struct lpfc_hba *phba)
337 {
338         struct completion online_compl;
339         int status = 0;
340
341         status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
342
343         if (status != 0)
344                 return status;
345
346         init_completion(&online_compl);
347         lpfc_workq_post_event(phba, &status, &online_compl,
348                               LPFC_EVT_ONLINE);
349         wait_for_completion(&online_compl);
350
351         if (status != 0)
352                 return -EIO;
353
354         return 0;
355 }
356
357 static ssize_t
358 lpfc_issue_reset(struct class_device *cdev, const char *buf, size_t count)
359 {
360         struct Scsi_Host  *shost = class_to_shost(cdev);
361         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
362         struct lpfc_hba   *phba = vport->phba;
363
364         int status = -EINVAL;
365
366         if (strncmp(buf, "selective", sizeof("selective") - 1) == 0)
367                 status = lpfc_selective_reset(phba);
368
369         if (status == 0)
370                 return strlen(buf);
371         else
372                 return status;
373 }
374
375 static ssize_t
376 lpfc_nport_evt_cnt_show(struct class_device *cdev, char *buf)
377 {
378         struct Scsi_Host  *shost = class_to_shost(cdev);
379         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
380         struct lpfc_hba   *phba = vport->phba;
381
382         return snprintf(buf, PAGE_SIZE, "%d\n", phba->nport_event_cnt);
383 }
384
385 static ssize_t
386 lpfc_board_mode_show(struct class_device *cdev, char *buf)
387 {
388         struct Scsi_Host  *shost = class_to_shost(cdev);
389         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
390         struct lpfc_hba   *phba = vport->phba;
391         char  * state;
392
393         if (phba->link_state == LPFC_HBA_ERROR)
394                 state = "error";
395         else if (phba->link_state == LPFC_WARM_START)
396                 state = "warm start";
397         else if (phba->link_state == LPFC_INIT_START)
398                 state = "offline";
399         else
400                 state = "online";
401
402         return snprintf(buf, PAGE_SIZE, "%s\n", state);
403 }
404
405 static ssize_t
406 lpfc_board_mode_store(struct class_device *cdev, const char *buf, size_t count)
407 {
408         struct Scsi_Host  *shost = class_to_shost(cdev);
409         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
410         struct lpfc_hba   *phba = vport->phba;
411         struct completion online_compl;
412         int status=0;
413
414         init_completion(&online_compl);
415
416         if(strncmp(buf, "online", sizeof("online") - 1) == 0) {
417                 lpfc_workq_post_event(phba, &status, &online_compl,
418                                       LPFC_EVT_ONLINE);
419                 wait_for_completion(&online_compl);
420         } else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
421                 status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
422         else if (strncmp(buf, "warm", sizeof("warm") - 1) == 0)
423                 status = lpfc_do_offline(phba, LPFC_EVT_WARM_START);
424         else if (strncmp(buf, "error", sizeof("error") - 1) == 0)
425                 status = lpfc_do_offline(phba, LPFC_EVT_KILL);
426         else
427                 return -EINVAL;
428
429         if (!status)
430                 return strlen(buf);
431         else
432                 return -EIO;
433 }
434
435 static ssize_t
436 lpfc_poll_show(struct class_device *cdev, char *buf)
437 {
438         struct Scsi_Host  *shost = class_to_shost(cdev);
439         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
440         struct lpfc_hba   *phba = vport->phba;
441
442         return snprintf(buf, PAGE_SIZE, "%#x\n", phba->cfg_poll);
443 }
444
445 static ssize_t
446 lpfc_poll_store(struct class_device *cdev, const char *buf,
447                 size_t count)
448 {
449         struct Scsi_Host  *shost = class_to_shost(cdev);
450         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
451         struct lpfc_hba   *phba = vport->phba;
452         uint32_t creg_val;
453         uint32_t old_val;
454         int val=0;
455
456         if (!isdigit(buf[0]))
457                 return -EINVAL;
458
459         if (sscanf(buf, "%i", &val) != 1)
460                 return -EINVAL;
461
462         if ((val & 0x3) != val)
463                 return -EINVAL;
464
465         spin_lock_irq(&phba->hbalock);
466
467         old_val = phba->cfg_poll;
468
469         if (val & ENABLE_FCP_RING_POLLING) {
470                 if ((val & DISABLE_FCP_RING_INT) &&
471                     !(old_val & DISABLE_FCP_RING_INT)) {
472                         creg_val = readl(phba->HCregaddr);
473                         creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
474                         writel(creg_val, phba->HCregaddr);
475                         readl(phba->HCregaddr); /* flush */
476
477                         lpfc_poll_start_timer(phba);
478                 }
479         } else if (val != 0x0) {
480                 spin_unlock_irq(&phba->hbalock);
481                 return -EINVAL;
482         }
483
484         if (!(val & DISABLE_FCP_RING_INT) &&
485             (old_val & DISABLE_FCP_RING_INT))
486         {
487                 spin_unlock_irq(&phba->hbalock);
488                 del_timer(&phba->fcp_poll_timer);
489                 spin_lock_irq(&phba->hbalock);
490                 creg_val = readl(phba->HCregaddr);
491                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
492                 writel(creg_val, phba->HCregaddr);
493                 readl(phba->HCregaddr); /* flush */
494         }
495
496         phba->cfg_poll = val;
497
498         spin_unlock_irq(&phba->hbalock);
499
500         return strlen(buf);
501 }
502
503 #define lpfc_param_show(attr)   \
504 static ssize_t \
505 lpfc_##attr##_show(struct class_device *cdev, char *buf) \
506 { \
507         struct Scsi_Host  *shost = class_to_shost(cdev);\
508         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
509         struct lpfc_hba   *phba = vport->phba;\
510         int val = 0;\
511         val = phba->cfg_##attr;\
512         return snprintf(buf, PAGE_SIZE, "%d\n",\
513                         phba->cfg_##attr);\
514 }
515
516 #define lpfc_param_hex_show(attr)       \
517 static ssize_t \
518 lpfc_##attr##_show(struct class_device *cdev, char *buf) \
519 { \
520         struct Scsi_Host  *shost = class_to_shost(cdev);\
521         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
522         struct lpfc_hba   *phba = vport->phba;\
523         int val = 0;\
524         val = phba->cfg_##attr;\
525         return snprintf(buf, PAGE_SIZE, "%#x\n",\
526                         phba->cfg_##attr);\
527 }
528
529 #define lpfc_param_init(attr, default, minval, maxval)  \
530 static int \
531 lpfc_##attr##_init(struct lpfc_hba *phba, int val) \
532 { \
533         if (val >= minval && val <= maxval) {\
534                 phba->cfg_##attr = val;\
535                 return 0;\
536         }\
537         lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
538                         "%d:0449 lpfc_"#attr" attribute cannot be set to %d, "\
539                         "allowed range is ["#minval", "#maxval"]\n", \
540                         phba->brd_no, val); \
541         phba->cfg_##attr = default;\
542         return -EINVAL;\
543 }
544
545 #define lpfc_param_set(attr, default, minval, maxval)   \
546 static int \
547 lpfc_##attr##_set(struct lpfc_hba *phba, int val) \
548 { \
549         if (val >= minval && val <= maxval) {\
550                 phba->cfg_##attr = val;\
551                 return 0;\
552         }\
553         lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
554                         "%d:0450 lpfc_"#attr" attribute cannot be set to %d, "\
555                         "allowed range is ["#minval", "#maxval"]\n", \
556                         phba->brd_no, val); \
557         return -EINVAL;\
558 }
559
560 #define lpfc_param_store(attr)  \
561 static ssize_t \
562 lpfc_##attr##_store(struct class_device *cdev, const char *buf, size_t count) \
563 { \
564         struct Scsi_Host  *shost = class_to_shost(cdev);\
565         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
566         struct lpfc_hba   *phba = vport->phba;\
567         int val=0;\
568         if (!isdigit(buf[0]))\
569                 return -EINVAL;\
570         if (sscanf(buf, "%i", &val) != 1)\
571                 return -EINVAL;\
572         if (lpfc_##attr##_set(phba, val) == 0) \
573                 return strlen(buf);\
574         else \
575                 return -EINVAL;\
576 }
577
578 #define LPFC_ATTR(name, defval, minval, maxval, desc) \
579 static int lpfc_##name = defval;\
580 module_param(lpfc_##name, int, 0);\
581 MODULE_PARM_DESC(lpfc_##name, desc);\
582 lpfc_param_init(name, defval, minval, maxval)
583
584 #define LPFC_ATTR_R(name, defval, minval, maxval, desc) \
585 static int lpfc_##name = defval;\
586 module_param(lpfc_##name, int, 0);\
587 MODULE_PARM_DESC(lpfc_##name, desc);\
588 lpfc_param_show(name)\
589 lpfc_param_init(name, defval, minval, maxval)\
590 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
591
592 #define LPFC_ATTR_RW(name, defval, minval, maxval, desc) \
593 static int lpfc_##name = defval;\
594 module_param(lpfc_##name, int, 0);\
595 MODULE_PARM_DESC(lpfc_##name, desc);\
596 lpfc_param_show(name)\
597 lpfc_param_init(name, defval, minval, maxval)\
598 lpfc_param_set(name, defval, minval, maxval)\
599 lpfc_param_store(name)\
600 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
601                          lpfc_##name##_show, lpfc_##name##_store)
602
603 #define LPFC_ATTR_HEX_R(name, defval, minval, maxval, desc) \
604 static int lpfc_##name = defval;\
605 module_param(lpfc_##name, int, 0);\
606 MODULE_PARM_DESC(lpfc_##name, desc);\
607 lpfc_param_hex_show(name)\
608 lpfc_param_init(name, defval, minval, maxval)\
609 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
610
611 #define LPFC_ATTR_HEX_RW(name, defval, minval, maxval, desc) \
612 static int lpfc_##name = defval;\
613 module_param(lpfc_##name, int, 0);\
614 MODULE_PARM_DESC(lpfc_##name, desc);\
615 lpfc_param_hex_show(name)\
616 lpfc_param_init(name, defval, minval, maxval)\
617 lpfc_param_set(name, defval, minval, maxval)\
618 lpfc_param_store(name)\
619 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
620                          lpfc_##name##_show, lpfc_##name##_store)
621
622 static CLASS_DEVICE_ATTR(info, S_IRUGO, lpfc_info_show, NULL);
623 static CLASS_DEVICE_ATTR(serialnum, S_IRUGO, lpfc_serialnum_show, NULL);
624 static CLASS_DEVICE_ATTR(modeldesc, S_IRUGO, lpfc_modeldesc_show, NULL);
625 static CLASS_DEVICE_ATTR(modelname, S_IRUGO, lpfc_modelname_show, NULL);
626 static CLASS_DEVICE_ATTR(programtype, S_IRUGO, lpfc_programtype_show, NULL);
627 static CLASS_DEVICE_ATTR(portnum, S_IRUGO, lpfc_vportnum_show, NULL);
628 static CLASS_DEVICE_ATTR(fwrev, S_IRUGO, lpfc_fwrev_show, NULL);
629 static CLASS_DEVICE_ATTR(hdw, S_IRUGO, lpfc_hdw_show, NULL);
630 static CLASS_DEVICE_ATTR(state, S_IRUGO, lpfc_state_show, NULL);
631 static CLASS_DEVICE_ATTR(option_rom_version, S_IRUGO,
632                                         lpfc_option_rom_version_show, NULL);
633 static CLASS_DEVICE_ATTR(num_discovered_ports, S_IRUGO,
634                                         lpfc_num_discovered_ports_show, NULL);
635 static CLASS_DEVICE_ATTR(nport_evt_cnt, S_IRUGO, lpfc_nport_evt_cnt_show, NULL);
636 static CLASS_DEVICE_ATTR(lpfc_drvr_version, S_IRUGO, lpfc_drvr_version_show,
637                          NULL);
638 static CLASS_DEVICE_ATTR(management_version, S_IRUGO, management_version_show,
639                          NULL);
640 static CLASS_DEVICE_ATTR(board_mode, S_IRUGO | S_IWUSR,
641                          lpfc_board_mode_show, lpfc_board_mode_store);
642 static CLASS_DEVICE_ATTR(issue_reset, S_IWUSR, NULL, lpfc_issue_reset);
643
644
645 static char *lpfc_soft_wwn_key = "C99G71SL8032A";
646
647 static ssize_t
648 lpfc_soft_wwn_enable_store(struct class_device *cdev, const char *buf,
649                                 size_t count)
650 {
651         struct Scsi_Host  *shost = class_to_shost(cdev);
652         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
653         struct lpfc_hba   *phba = vport->phba;
654         unsigned int cnt = count;
655
656         /*
657          * We're doing a simple sanity check for soft_wwpn setting.
658          * We require that the user write a specific key to enable
659          * the soft_wwpn attribute to be settable. Once the attribute
660          * is written, the enable key resets. If further updates are
661          * desired, the key must be written again to re-enable the
662          * attribute.
663          *
664          * The "key" is not secret - it is a hardcoded string shown
665          * here. The intent is to protect against the random user or
666          * application that is just writing attributes.
667          */
668
669         /* count may include a LF at end of string */
670         if (buf[cnt-1] == '\n')
671                 cnt--;
672
673         if ((cnt != strlen(lpfc_soft_wwn_key)) ||
674             (strncmp(buf, lpfc_soft_wwn_key, strlen(lpfc_soft_wwn_key)) != 0))
675                 return -EINVAL;
676
677         phba->soft_wwn_enable = 1;
678         return count;
679 }
680 static CLASS_DEVICE_ATTR(lpfc_soft_wwn_enable, S_IWUSR, NULL,
681                                 lpfc_soft_wwn_enable_store);
682
683 static ssize_t
684 lpfc_soft_wwpn_show(struct class_device *cdev, char *buf)
685 {
686         struct Scsi_Host  *shost = class_to_shost(cdev);
687         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
688         struct lpfc_hba   *phba = vport->phba;
689
690         return snprintf(buf, PAGE_SIZE, "0x%llx\n",
691                         (unsigned long long)phba->cfg_soft_wwpn);
692 }
693
694
695 static ssize_t
696 lpfc_soft_wwpn_store(struct class_device *cdev, const char *buf, size_t count)
697 {
698         struct Scsi_Host  *shost = class_to_shost(cdev);
699         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
700         struct lpfc_hba   *phba = vport->phba;
701         struct completion online_compl;
702         int stat1=0, stat2=0;
703         unsigned int i, j, cnt=count;
704         u8 wwpn[8];
705
706         /* count may include a LF at end of string */
707         if (buf[cnt-1] == '\n')
708                 cnt--;
709
710         if (!phba->soft_wwn_enable || (cnt < 16) || (cnt > 18) ||
711             ((cnt == 17) && (*buf++ != 'x')) ||
712             ((cnt == 18) && ((*buf++ != '0') || (*buf++ != 'x'))))
713                 return -EINVAL;
714
715         phba->soft_wwn_enable = 0;
716
717         memset(wwpn, 0, sizeof(wwpn));
718
719         /* Validate and store the new name */
720         for (i=0, j=0; i < 16; i++) {
721                 if ((*buf >= 'a') && (*buf <= 'f'))
722                         j = ((j << 4) | ((*buf++ -'a') + 10));
723                 else if ((*buf >= 'A') && (*buf <= 'F'))
724                         j = ((j << 4) | ((*buf++ -'A') + 10));
725                 else if ((*buf >= '0') && (*buf <= '9'))
726                         j = ((j << 4) | (*buf++ -'0'));
727                 else
728                         return -EINVAL;
729                 if (i % 2) {
730                         wwpn[i/2] = j & 0xff;
731                         j = 0;
732                 }
733         }
734         phba->cfg_soft_wwpn = wwn_to_u64(wwpn);
735         fc_host_port_name(shost) = phba->cfg_soft_wwpn;
736         if (phba->cfg_soft_wwnn)
737                 fc_host_node_name(shost) = phba->cfg_soft_wwnn;
738
739         dev_printk(KERN_NOTICE, &phba->pcidev->dev,
740                    "lpfc%d: Reinitializing to use soft_wwpn\n", phba->brd_no);
741
742         stat1 = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
743         if (stat1)
744                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
745                         "%d:0463 lpfc_soft_wwpn attribute set failed to reinit "
746                         "adapter - %d\n", phba->brd_no, stat1);
747
748         init_completion(&online_compl);
749         lpfc_workq_post_event(phba, &stat2, &online_compl, LPFC_EVT_ONLINE);
750         wait_for_completion(&online_compl);
751         if (stat2)
752                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
753                         "%d:0464 lpfc_soft_wwpn attribute set failed to reinit "
754                         "adapter - %d\n", phba->brd_no, stat2);
755
756         return (stat1 || stat2) ? -EIO : count;
757 }
758 static CLASS_DEVICE_ATTR(lpfc_soft_wwpn, S_IRUGO | S_IWUSR,\
759                          lpfc_soft_wwpn_show, lpfc_soft_wwpn_store);
760
761 static ssize_t
762 lpfc_soft_wwnn_show(struct class_device *cdev, char *buf)
763 {
764         struct Scsi_Host *host = class_to_shost(cdev);
765         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
766         return snprintf(buf, PAGE_SIZE, "0x%llx\n",
767                         (unsigned long long)phba->cfg_soft_wwnn);
768 }
769
770
771 static ssize_t
772 lpfc_soft_wwnn_store(struct class_device *cdev, const char *buf, size_t count)
773 {
774         struct Scsi_Host *host = class_to_shost(cdev);
775         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
776         unsigned int i, j, cnt=count;
777         u8 wwnn[8];
778
779         /* count may include a LF at end of string */
780         if (buf[cnt-1] == '\n')
781                 cnt--;
782
783         if (!phba->soft_wwn_enable || (cnt < 16) || (cnt > 18) ||
784             ((cnt == 17) && (*buf++ != 'x')) ||
785             ((cnt == 18) && ((*buf++ != '0') || (*buf++ != 'x'))))
786                 return -EINVAL;
787
788         /*
789          * Allow wwnn to be set many times, as long as the enable is set.
790          * However, once the wwpn is set, everything locks.
791          */
792
793         memset(wwnn, 0, sizeof(wwnn));
794
795         /* Validate and store the new name */
796         for (i=0, j=0; i < 16; i++) {
797                 if ((*buf >= 'a') && (*buf <= 'f'))
798                         j = ((j << 4) | ((*buf++ -'a') + 10));
799                 else if ((*buf >= 'A') && (*buf <= 'F'))
800                         j = ((j << 4) | ((*buf++ -'A') + 10));
801                 else if ((*buf >= '0') && (*buf <= '9'))
802                         j = ((j << 4) | (*buf++ -'0'));
803                 else
804                         return -EINVAL;
805                 if (i % 2) {
806                         wwnn[i/2] = j & 0xff;
807                         j = 0;
808                 }
809         }
810         phba->cfg_soft_wwnn = wwn_to_u64(wwnn);
811
812         dev_printk(KERN_NOTICE, &phba->pcidev->dev,
813                    "lpfc%d: soft_wwnn set. Value will take effect upon "
814                    "setting of the soft_wwpn\n", phba->brd_no);
815
816         return count;
817 }
818 static CLASS_DEVICE_ATTR(lpfc_soft_wwnn, S_IRUGO | S_IWUSR,\
819                          lpfc_soft_wwnn_show, lpfc_soft_wwnn_store);
820
821
822 static int lpfc_poll = 0;
823 module_param(lpfc_poll, int, 0);
824 MODULE_PARM_DESC(lpfc_poll, "FCP ring polling mode control:"
825                  " 0 - none,"
826                  " 1 - poll with interrupts enabled"
827                  " 3 - poll and disable FCP ring interrupts");
828
829 static CLASS_DEVICE_ATTR(lpfc_poll, S_IRUGO | S_IWUSR,
830                          lpfc_poll_show, lpfc_poll_store);
831
832 /*
833 # lpfc_nodev_tmo: If set, it will hold all I/O errors on devices that disappear
834 # until the timer expires. Value range is [0,255]. Default value is 30.
835 */
836 static int lpfc_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
837 static int lpfc_devloss_tmo = LPFC_DEF_DEVLOSS_TMO;
838 module_param(lpfc_nodev_tmo, int, 0);
839 MODULE_PARM_DESC(lpfc_nodev_tmo,
840                  "Seconds driver will hold I/O waiting "
841                  "for a device to come back");
842 static ssize_t
843 lpfc_nodev_tmo_show(struct class_device *cdev, char *buf)
844 {
845         struct Scsi_Host  *shost = class_to_shost(cdev);
846         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
847         struct lpfc_hba   *phba = vport->phba;
848         int val = 0;
849         val = phba->cfg_devloss_tmo;
850         return snprintf(buf, PAGE_SIZE, "%d\n",
851                         phba->cfg_devloss_tmo);
852 }
853
854 static int
855 lpfc_nodev_tmo_init(struct lpfc_hba *phba, int val)
856 {
857         static int warned;
858         if (phba->cfg_devloss_tmo !=  LPFC_DEF_DEVLOSS_TMO) {
859                 phba->cfg_nodev_tmo = phba->cfg_devloss_tmo;
860                 if (!warned && val != LPFC_DEF_DEVLOSS_TMO) {
861                         warned = 1;
862                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
863                                         "%d:0402 Ignoring nodev_tmo module "
864                                         "parameter because devloss_tmo is"
865                                         " set.\n",
866                                         phba->brd_no);
867                 }
868                 return 0;
869         }
870
871         if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
872                 phba->cfg_nodev_tmo = val;
873                 phba->cfg_devloss_tmo = val;
874                 return 0;
875         }
876         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
877                         "%d:0400 lpfc_nodev_tmo attribute cannot be set to %d, "
878                         "allowed range is [%d, %d]\n",
879                         phba->brd_no, val,
880                         LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
881         phba->cfg_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
882         return -EINVAL;
883 }
884
885 static int
886 lpfc_nodev_tmo_set(struct lpfc_hba *phba, int val)
887 {
888         if (phba->dev_loss_tmo_changed ||
889                 (lpfc_devloss_tmo != LPFC_DEF_DEVLOSS_TMO)) {
890                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
891                                 "%d:0401 Ignoring change to nodev_tmo "
892                                 "because devloss_tmo is set.\n",
893                                 phba->brd_no);
894                 return 0;
895         }
896
897         if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
898                 phba->cfg_nodev_tmo = val;
899                 phba->cfg_devloss_tmo = val;
900                 return 0;
901         }
902
903         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
904                         "%d:0403 lpfc_nodev_tmo attribute cannot be set to %d, "
905                         "allowed range is [%d, %d]\n",
906                         phba->brd_no, val, LPFC_MIN_DEVLOSS_TMO,
907                         LPFC_MAX_DEVLOSS_TMO);
908         return -EINVAL;
909 }
910
911 lpfc_param_store(nodev_tmo)
912
913 static CLASS_DEVICE_ATTR(lpfc_nodev_tmo, S_IRUGO | S_IWUSR,
914                          lpfc_nodev_tmo_show, lpfc_nodev_tmo_store);
915
916 /*
917 # lpfc_devloss_tmo: If set, it will hold all I/O errors on devices that
918 # disappear until the timer expires. Value range is [0,255]. Default
919 # value is 30.
920 */
921 module_param(lpfc_devloss_tmo, int, 0);
922 MODULE_PARM_DESC(lpfc_devloss_tmo,
923                  "Seconds driver will hold I/O waiting "
924                  "for a device to come back");
925 lpfc_param_init(devloss_tmo, LPFC_DEF_DEVLOSS_TMO,
926                 LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO)
927 lpfc_param_show(devloss_tmo)
928 static int
929 lpfc_devloss_tmo_set(struct lpfc_hba *phba, int val)
930 {
931         if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
932                 phba->cfg_nodev_tmo = val;
933                 phba->cfg_devloss_tmo = val;
934                 phba->dev_loss_tmo_changed = 1;
935                 return 0;
936         }
937
938         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
939                         "%d:0404 lpfc_devloss_tmo attribute cannot be set to"
940                         " %d, allowed range is [%d, %d]\n",
941                         phba->brd_no, val, LPFC_MIN_DEVLOSS_TMO,
942                         LPFC_MAX_DEVLOSS_TMO);
943         return -EINVAL;
944 }
945
946 lpfc_param_store(devloss_tmo)
947 static CLASS_DEVICE_ATTR(lpfc_devloss_tmo, S_IRUGO | S_IWUSR,
948         lpfc_devloss_tmo_show, lpfc_devloss_tmo_store);
949
950 /*
951 # lpfc_log_verbose: Only turn this flag on if you are willing to risk being
952 # deluged with LOTS of information.
953 # You can set a bit mask to record specific types of verbose messages:
954 #
955 # LOG_ELS                       0x1        ELS events
956 # LOG_DISCOVERY                 0x2        Link discovery events
957 # LOG_MBOX                      0x4        Mailbox events
958 # LOG_INIT                      0x8        Initialization events
959 # LOG_LINK_EVENT                0x10       Link events
960 # LOG_FCP                       0x40       FCP traffic history
961 # LOG_NODE                      0x80       Node table events
962 # LOG_MISC                      0x400      Miscellaneous events
963 # LOG_SLI                       0x800      SLI events
964 # LOG_FCP_ERROR                 0x1000     Only log FCP errors
965 # LOG_LIBDFC                    0x2000     LIBDFC events
966 # LOG_ALL_MSG                   0xffff     LOG all messages
967 */
968 LPFC_ATTR_HEX_RW(log_verbose, 0x0, 0x0, 0xffff, "Verbose logging bit-mask");
969
970 /*
971 # lun_queue_depth:  This parameter is used to limit the number of outstanding
972 # commands per FCP LUN. Value range is [1,128]. Default value is 30.
973 */
974 LPFC_ATTR_R(lun_queue_depth, 30, 1, 128,
975             "Max number of FCP commands we can queue to a specific LUN");
976
977 /*
978 # hba_queue_depth:  This parameter is used to limit the number of outstanding
979 # commands per lpfc HBA. Value range is [32,8192]. If this parameter
980 # value is greater than the maximum number of exchanges supported by the HBA,
981 # then maximum number of exchanges supported by the HBA is used to determine
982 # the hba_queue_depth.
983 */
984 LPFC_ATTR_R(hba_queue_depth, 8192, 32, 8192,
985             "Max number of FCP commands we can queue to a lpfc HBA");
986
987 /*
988 # Some disk devices have a "select ID" or "select Target" capability.
989 # From a protocol standpoint "select ID" usually means select the
990 # Fibre channel "ALPA".  In the FC-AL Profile there is an "informative
991 # annex" which contains a table that maps a "select ID" (a number
992 # between 0 and 7F) to an ALPA.  By default, for compatibility with
993 # older drivers, the lpfc driver scans this table from low ALPA to high
994 # ALPA.
995 #
996 # Turning on the scan-down variable (on  = 1, off = 0) will
997 # cause the lpfc driver to use an inverted table, effectively
998 # scanning ALPAs from high to low. Value range is [0,1]. Default value is 1.
999 #
1000 # (Note: This "select ID" functionality is a LOOP ONLY characteristic
1001 # and will not work across a fabric. Also this parameter will take
1002 # effect only in the case when ALPA map is not available.)
1003 */
1004 LPFC_ATTR_R(scan_down, 1, 0, 1,
1005              "Start scanning for devices from highest ALPA to lowest");
1006
1007 /*
1008 # lpfc_topology:  link topology for init link
1009 #            0x0  = attempt loop mode then point-to-point
1010 #            0x01 = internal loopback mode
1011 #            0x02 = attempt point-to-point mode only
1012 #            0x04 = attempt loop mode only
1013 #            0x06 = attempt point-to-point mode then loop
1014 # Set point-to-point mode if you want to run as an N_Port.
1015 # Set loop mode if you want to run as an NL_Port. Value range is [0,0x6].
1016 # Default value is 0.
1017 */
1018 LPFC_ATTR_RW(topology, 0, 0, 6, "Select Fibre Channel topology");
1019
1020 /*
1021 # lpfc_link_speed: Link speed selection for initializing the Fibre Channel
1022 # connection.
1023 #       0  = auto select (default)
1024 #       1  = 1 Gigabaud
1025 #       2  = 2 Gigabaud
1026 #       4  = 4 Gigabaud
1027 #       8  = 8 Gigabaud
1028 # Value range is [0,8]. Default value is 0.
1029 */
1030 LPFC_ATTR_R(link_speed, 0, 0, 8, "Select link speed");
1031
1032 /*
1033 # lpfc_fcp_class:  Determines FC class to use for the FCP protocol.
1034 # Value range is [2,3]. Default value is 3.
1035 */
1036 LPFC_ATTR_R(fcp_class, 3, 2, 3,
1037              "Select Fibre Channel class of service for FCP sequences");
1038
1039 /*
1040 # lpfc_use_adisc: Use ADISC for FCP rediscovery instead of PLOGI. Value range
1041 # is [0,1]. Default value is 0.
1042 */
1043 LPFC_ATTR_RW(use_adisc, 0, 0, 1,
1044              "Use ADISC on rediscovery to authenticate FCP devices");
1045
1046 /*
1047 # lpfc_ack0: Use ACK0, instead of ACK1 for class 2 acknowledgement. Value
1048 # range is [0,1]. Default value is 0.
1049 */
1050 LPFC_ATTR_R(ack0, 0, 0, 1, "Enable ACK0 support");
1051
1052 /*
1053 # lpfc_cr_delay & lpfc_cr_count: Default values for I/O colaesing
1054 # cr_delay (msec) or cr_count outstanding commands. cr_delay can take
1055 # value [0,63]. cr_count can take value [1,255]. Default value of cr_delay
1056 # is 0. Default value of cr_count is 1. The cr_count feature is disabled if
1057 # cr_delay is set to 0.
1058 */
1059 LPFC_ATTR_RW(cr_delay, 0, 0, 63, "A count of milliseconds after which an "
1060                 "interrupt response is generated");
1061
1062 LPFC_ATTR_RW(cr_count, 1, 1, 255, "A count of I/O completions after which an "
1063                 "interrupt response is generated");
1064
1065 /*
1066 # lpfc_multi_ring_support:  Determines how many rings to spread available
1067 # cmd/rsp IOCB entries across.
1068 # Value range is [1,2]. Default value is 1.
1069 */
1070 LPFC_ATTR_R(multi_ring_support, 1, 1, 2, "Determines number of primary "
1071                 "SLI rings to spread IOCB entries across");
1072
1073 /*
1074 # lpfc_multi_ring_rctl:  If lpfc_multi_ring_support is enabled, this
1075 # identifies what rctl value to configure the additional ring for.
1076 # Value range is [1,0xff]. Default value is 4 (Unsolicated Data).
1077 */
1078 LPFC_ATTR_R(multi_ring_rctl, FC_UNSOL_DATA, 1,
1079              255, "Identifies RCTL for additional ring configuration");
1080
1081 /*
1082 # lpfc_multi_ring_type:  If lpfc_multi_ring_support is enabled, this
1083 # identifies what type value to configure the additional ring for.
1084 # Value range is [1,0xff]. Default value is 5 (LLC/SNAP).
1085 */
1086 LPFC_ATTR_R(multi_ring_type, FC_LLC_SNAP, 1,
1087              255, "Identifies TYPE for additional ring configuration");
1088
1089 /*
1090 # lpfc_fdmi_on: controls FDMI support.
1091 #       0 = no FDMI support
1092 #       1 = support FDMI without attribute of hostname
1093 #       2 = support FDMI with attribute of hostname
1094 # Value range [0,2]. Default value is 0.
1095 */
1096 LPFC_ATTR_RW(fdmi_on, 0, 0, 2, "Enable FDMI support");
1097
1098 /*
1099 # Specifies the maximum number of ELS cmds we can have outstanding (for
1100 # discovery). Value range is [1,64]. Default value = 32.
1101 */
1102 LPFC_ATTR(discovery_threads, 32, 1, 64, "Maximum number of ELS commands "
1103                  "during discovery");
1104
1105 /*
1106 # lpfc_max_luns: maximum allowed LUN.
1107 # Value range is [0,65535]. Default value is 255.
1108 # NOTE: The SCSI layer might probe all allowed LUN on some old targets.
1109 */
1110 LPFC_ATTR_R(max_luns, 255, 0, 65535,
1111              "Maximum allowed LUN");
1112
1113 /*
1114 # lpfc_poll_tmo: .Milliseconds driver will wait between polling FCP ring.
1115 # Value range is [1,255], default value is 10.
1116 */
1117 LPFC_ATTR_RW(poll_tmo, 10, 1, 255,
1118              "Milliseconds driver will wait between polling FCP ring");
1119
1120 /*
1121 # lpfc_use_msi: Use MSI (Message Signaled Interrupts) in systems that
1122 #               support this feature
1123 #       0  = MSI disabled (default)
1124 #       1  = MSI enabled
1125 # Value range is [0,1]. Default value is 0.
1126 */
1127 LPFC_ATTR_R(use_msi, 0, 0, 1, "Use Message Signaled Interrupts, if possible");
1128
1129
1130 struct class_device_attribute *lpfc_hba_attrs[] = {
1131         &class_device_attr_info,
1132         &class_device_attr_serialnum,
1133         &class_device_attr_modeldesc,
1134         &class_device_attr_modelname,
1135         &class_device_attr_programtype,
1136         &class_device_attr_portnum,
1137         &class_device_attr_fwrev,
1138         &class_device_attr_hdw,
1139         &class_device_attr_option_rom_version,
1140         &class_device_attr_state,
1141         &class_device_attr_num_discovered_ports,
1142         &class_device_attr_lpfc_drvr_version,
1143         &class_device_attr_lpfc_log_verbose,
1144         &class_device_attr_lpfc_lun_queue_depth,
1145         &class_device_attr_lpfc_hba_queue_depth,
1146         &class_device_attr_lpfc_nodev_tmo,
1147         &class_device_attr_lpfc_devloss_tmo,
1148         &class_device_attr_lpfc_fcp_class,
1149         &class_device_attr_lpfc_use_adisc,
1150         &class_device_attr_lpfc_ack0,
1151         &class_device_attr_lpfc_topology,
1152         &class_device_attr_lpfc_scan_down,
1153         &class_device_attr_lpfc_link_speed,
1154         &class_device_attr_lpfc_cr_delay,
1155         &class_device_attr_lpfc_cr_count,
1156         &class_device_attr_lpfc_multi_ring_support,
1157         &class_device_attr_lpfc_multi_ring_rctl,
1158         &class_device_attr_lpfc_multi_ring_type,
1159         &class_device_attr_lpfc_fdmi_on,
1160         &class_device_attr_lpfc_max_luns,
1161         &class_device_attr_nport_evt_cnt,
1162         &class_device_attr_management_version,
1163         &class_device_attr_board_mode,
1164         &class_device_attr_issue_reset,
1165         &class_device_attr_lpfc_poll,
1166         &class_device_attr_lpfc_poll_tmo,
1167         &class_device_attr_lpfc_use_msi,
1168         &class_device_attr_lpfc_soft_wwnn,
1169         &class_device_attr_lpfc_soft_wwpn,
1170         &class_device_attr_lpfc_soft_wwn_enable,
1171         NULL,
1172 };
1173
1174 static ssize_t
1175 sysfs_ctlreg_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
1176 {
1177         size_t buf_off;
1178         struct class_device *cdev = container_of(kobj, struct class_device,
1179                                                  kobj);
1180         struct Scsi_Host  *shost = class_to_shost(cdev);
1181         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1182         struct lpfc_hba   *phba = vport->phba;
1183
1184         if ((off + count) > FF_REG_AREA_SIZE)
1185                 return -ERANGE;
1186
1187         if (count == 0) return 0;
1188
1189         if (off % 4 || count % 4 || (unsigned long)buf % 4)
1190                 return -EINVAL;
1191
1192         if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
1193                 return -EPERM;
1194         }
1195
1196         spin_lock_irq(&phba->hbalock);
1197         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t))
1198                 writel(*((uint32_t *)(buf + buf_off)),
1199                        phba->ctrl_regs_memmap_p + off + buf_off);
1200
1201         spin_unlock_irq(&phba->hbalock);
1202
1203         return count;
1204 }
1205
1206 static ssize_t
1207 sysfs_ctlreg_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
1208 {
1209         size_t buf_off;
1210         uint32_t * tmp_ptr;
1211         struct class_device *cdev = container_of(kobj, struct class_device,
1212                                                  kobj);
1213         struct Scsi_Host  *shost = class_to_shost(cdev);
1214         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1215         struct lpfc_hba   *phba = vport->phba;
1216
1217         if (off > FF_REG_AREA_SIZE)
1218                 return -ERANGE;
1219
1220         if ((off + count) > FF_REG_AREA_SIZE)
1221                 count = FF_REG_AREA_SIZE - off;
1222
1223         if (count == 0) return 0;
1224
1225         if (off % 4 || count % 4 || (unsigned long)buf % 4)
1226                 return -EINVAL;
1227
1228         spin_lock_irq(&phba->hbalock);
1229
1230         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t)) {
1231                 tmp_ptr = (uint32_t *)(buf + buf_off);
1232                 *tmp_ptr = readl(phba->ctrl_regs_memmap_p + off + buf_off);
1233         }
1234
1235         spin_unlock_irq(&phba->hbalock);
1236
1237         return count;
1238 }
1239
1240 static struct bin_attribute sysfs_ctlreg_attr = {
1241         .attr = {
1242                 .name = "ctlreg",
1243                 .mode = S_IRUSR | S_IWUSR,
1244                 .owner = THIS_MODULE,
1245         },
1246         .size = 256,
1247         .read = sysfs_ctlreg_read,
1248         .write = sysfs_ctlreg_write,
1249 };
1250
1251
1252 static void
1253 sysfs_mbox_idle(struct lpfc_hba *phba)
1254 {
1255         phba->sysfs_mbox.state = SMBOX_IDLE;
1256         phba->sysfs_mbox.offset = 0;
1257
1258         if (phba->sysfs_mbox.mbox) {
1259                 mempool_free(phba->sysfs_mbox.mbox,
1260                              phba->mbox_mem_pool);
1261                 phba->sysfs_mbox.mbox = NULL;
1262         }
1263 }
1264
1265 static ssize_t
1266 sysfs_mbox_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
1267 {
1268         struct class_device *cdev = container_of(kobj, struct class_device,
1269                                                  kobj);
1270         struct Scsi_Host  *shost = class_to_shost(cdev);
1271         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1272         struct lpfc_hba   *phba = vport->phba;
1273         struct lpfcMboxq  *mbox = NULL;
1274
1275         if ((count + off) > MAILBOX_CMD_SIZE)
1276                 return -ERANGE;
1277
1278         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
1279                 return -EINVAL;
1280
1281         if (count == 0)
1282                 return 0;
1283
1284         if (off == 0) {
1285                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1286                 if (!mbox)
1287                         return -ENOMEM;
1288                 memset(mbox, 0, sizeof (LPFC_MBOXQ_t));
1289         }
1290
1291         spin_lock_irq(&phba->hbalock);
1292
1293         if (off == 0) {
1294                 if (phba->sysfs_mbox.mbox)
1295                         mempool_free(mbox, phba->mbox_mem_pool);
1296                 else
1297                         phba->sysfs_mbox.mbox = mbox;
1298                 phba->sysfs_mbox.state = SMBOX_WRITING;
1299         } else {
1300                 if (phba->sysfs_mbox.state  != SMBOX_WRITING ||
1301                     phba->sysfs_mbox.offset != off           ||
1302                     phba->sysfs_mbox.mbox   == NULL ) {
1303                         sysfs_mbox_idle(phba);
1304                         spin_unlock_irq(&phba->hbalock);
1305                         return -EAGAIN;
1306                 }
1307         }
1308
1309         memcpy((uint8_t *) & phba->sysfs_mbox.mbox->mb + off,
1310                buf, count);
1311
1312         phba->sysfs_mbox.offset = off + count;
1313
1314         spin_unlock_irq(&phba->hbalock);
1315
1316         return count;
1317 }
1318
1319 static ssize_t
1320 sysfs_mbox_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
1321 {
1322         struct class_device *cdev = container_of(kobj, struct class_device,
1323                                                  kobj);
1324         struct Scsi_Host  *shost = class_to_shost(cdev);
1325         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1326         struct lpfc_hba   *phba = vport->phba;
1327         int rc;
1328
1329         if (off > MAILBOX_CMD_SIZE)
1330                 return -ERANGE;
1331
1332         if ((count + off) > MAILBOX_CMD_SIZE)
1333                 count = MAILBOX_CMD_SIZE - off;
1334
1335         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
1336                 return -EINVAL;
1337
1338         if (off && count == 0)
1339                 return 0;
1340
1341         spin_lock_irq(&phba->hbalock);
1342
1343         if (off == 0 &&
1344             phba->sysfs_mbox.state  == SMBOX_WRITING &&
1345             phba->sysfs_mbox.offset >= 2 * sizeof(uint32_t)) {
1346
1347                 switch (phba->sysfs_mbox.mbox->mb.mbxCommand) {
1348                         /* Offline only */
1349                 case MBX_WRITE_NV:
1350                 case MBX_INIT_LINK:
1351                 case MBX_DOWN_LINK:
1352                 case MBX_CONFIG_LINK:
1353                 case MBX_CONFIG_RING:
1354                 case MBX_RESET_RING:
1355                 case MBX_UNREG_LOGIN:
1356                 case MBX_CLEAR_LA:
1357                 case MBX_DUMP_CONTEXT:
1358                 case MBX_RUN_DIAGS:
1359                 case MBX_RESTART:
1360                 case MBX_FLASH_WR_ULA:
1361                 case MBX_SET_MASK:
1362                 case MBX_SET_SLIM:
1363                 case MBX_SET_DEBUG:
1364                         if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
1365                                 printk(KERN_WARNING "mbox_read:Command 0x%x "
1366                                        "is illegal in on-line state\n",
1367                                        phba->sysfs_mbox.mbox->mb.mbxCommand);
1368                                 sysfs_mbox_idle(phba);
1369                                 spin_unlock_irq(&phba->hbalock);
1370                                 return -EPERM;
1371                         }
1372                 case MBX_LOAD_SM:
1373                 case MBX_READ_NV:
1374                 case MBX_READ_CONFIG:
1375                 case MBX_READ_RCONFIG:
1376                 case MBX_READ_STATUS:
1377                 case MBX_READ_XRI:
1378                 case MBX_READ_REV:
1379                 case MBX_READ_LNK_STAT:
1380                 case MBX_DUMP_MEMORY:
1381                 case MBX_DOWN_LOAD:
1382                 case MBX_UPDATE_CFG:
1383                 case MBX_KILL_BOARD:
1384                 case MBX_LOAD_AREA:
1385                 case MBX_LOAD_EXP_ROM:
1386                 case MBX_BEACON:
1387                 case MBX_DEL_LD_ENTRY:
1388                         break;
1389                 case MBX_READ_SPARM64:
1390                 case MBX_READ_LA:
1391                 case MBX_READ_LA64:
1392                 case MBX_REG_LOGIN:
1393                 case MBX_REG_LOGIN64:
1394                 case MBX_CONFIG_PORT:
1395                 case MBX_RUN_BIU_DIAG:
1396                         printk(KERN_WARNING "mbox_read: Illegal Command 0x%x\n",
1397                                phba->sysfs_mbox.mbox->mb.mbxCommand);
1398                         sysfs_mbox_idle(phba);
1399                         spin_unlock_irq(&phba->hbalock);
1400                         return -EPERM;
1401                 default:
1402                         printk(KERN_WARNING "mbox_read: Unknown Command 0x%x\n",
1403                                phba->sysfs_mbox.mbox->mb.mbxCommand);
1404                         sysfs_mbox_idle(phba);
1405                         spin_unlock_irq(&phba->hbalock);
1406                         return -EPERM;
1407                 }
1408
1409                 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
1410                         sysfs_mbox_idle(phba);
1411                         spin_unlock_irq(&phba->hbalock);
1412                         return  -EAGAIN;
1413                 }
1414
1415                 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
1416                     (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE))){
1417
1418                         spin_unlock_irq(&phba->hbalock);
1419                         rc = lpfc_sli_issue_mbox (phba,
1420                                                   phba->sysfs_mbox.mbox,
1421                                                   MBX_POLL);
1422                         spin_lock_irq(&phba->hbalock);
1423
1424                 } else {
1425                         spin_unlock_irq(&phba->hbalock);
1426                         rc = lpfc_sli_issue_mbox_wait (phba,
1427                                                        phba->sysfs_mbox.mbox,
1428                                 lpfc_mbox_tmo_val(phba,
1429                                     phba->sysfs_mbox.mbox->mb.mbxCommand) * HZ);
1430                         spin_lock_irq(&phba->hbalock);
1431                 }
1432
1433                 if (rc != MBX_SUCCESS) {
1434                         if (rc == MBX_TIMEOUT) {
1435                                 phba->sysfs_mbox.mbox->mbox_cmpl =
1436                                         lpfc_sli_def_mbox_cmpl;
1437                                 phba->sysfs_mbox.mbox = NULL;
1438                         }
1439                         sysfs_mbox_idle(phba);
1440                         spin_unlock_irq(&phba->hbalock);
1441                         return  (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
1442                 }
1443                 phba->sysfs_mbox.state = SMBOX_READING;
1444         }
1445         else if (phba->sysfs_mbox.offset != off ||
1446                  phba->sysfs_mbox.state  != SMBOX_READING) {
1447                 printk(KERN_WARNING  "mbox_read: Bad State\n");
1448                 sysfs_mbox_idle(phba);
1449                 spin_unlock_irq(&phba->hbalock);
1450                 return -EAGAIN;
1451         }
1452
1453         memcpy(buf, (uint8_t *) & phba->sysfs_mbox.mbox->mb + off, count);
1454
1455         phba->sysfs_mbox.offset = off + count;
1456
1457         if (phba->sysfs_mbox.offset == MAILBOX_CMD_SIZE)
1458                 sysfs_mbox_idle(phba);
1459
1460         spin_unlock_irq(&phba->hbalock);
1461
1462         return count;
1463 }
1464
1465 static struct bin_attribute sysfs_mbox_attr = {
1466         .attr = {
1467                 .name = "mbox",
1468                 .mode = S_IRUSR | S_IWUSR,
1469                 .owner = THIS_MODULE,
1470         },
1471         .size = MAILBOX_CMD_SIZE,
1472         .read = sysfs_mbox_read,
1473         .write = sysfs_mbox_write,
1474 };
1475
1476 int
1477 lpfc_alloc_sysfs_attr(struct lpfc_vport *vport)
1478 {
1479         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1480         int error;
1481
1482         error = sysfs_create_bin_file(&shost->shost_classdev.kobj,
1483                                                         &sysfs_ctlreg_attr);
1484         if (error)
1485                 goto out;
1486
1487         error = sysfs_create_bin_file(&shost->shost_classdev.kobj,
1488                                                         &sysfs_mbox_attr);
1489         if (error)
1490                 goto out_remove_ctlreg_attr;
1491
1492         return 0;
1493 out_remove_ctlreg_attr:
1494         sysfs_remove_bin_file(&shost->shost_classdev.kobj, &sysfs_ctlreg_attr);
1495 out:
1496         return error;
1497 }
1498
1499 void
1500 lpfc_free_sysfs_attr(struct lpfc_vport *vport)
1501 {
1502         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1503
1504         sysfs_remove_bin_file(&shost->shost_classdev.kobj, &sysfs_mbox_attr);
1505         sysfs_remove_bin_file(&shost->shost_classdev.kobj, &sysfs_ctlreg_attr);
1506 }
1507
1508
1509 /*
1510  * Dynamic FC Host Attributes Support
1511  */
1512
1513 static void
1514 lpfc_get_host_port_id(struct Scsi_Host *shost)
1515 {
1516         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1517
1518         /* note: fc_myDID already in cpu endianness */
1519         fc_host_port_id(shost) = vport->fc_myDID;
1520 }
1521
1522 static void
1523 lpfc_get_host_port_type(struct Scsi_Host *shost)
1524 {
1525         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1526         struct lpfc_hba   *phba = vport->phba;
1527
1528         spin_lock_irq(shost->host_lock);
1529
1530         if (lpfc_is_link_up(phba)) {
1531                 if (phba->fc_topology == TOPOLOGY_LOOP) {
1532                         if (vport->fc_flag & FC_PUBLIC_LOOP)
1533                                 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
1534                         else
1535                                 fc_host_port_type(shost) = FC_PORTTYPE_LPORT;
1536                 } else {
1537                         if (vport->fc_flag & FC_FABRIC)
1538                                 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
1539                         else
1540                                 fc_host_port_type(shost) = FC_PORTTYPE_PTP;
1541                 }
1542         } else
1543                 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
1544
1545         spin_unlock_irq(shost->host_lock);
1546 }
1547
1548 static void
1549 lpfc_get_host_port_state(struct Scsi_Host *shost)
1550 {
1551         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1552         struct lpfc_hba   *phba = vport->phba;
1553
1554         spin_lock_irq(shost->host_lock);
1555
1556         if (vport->fc_flag & FC_OFFLINE_MODE)
1557                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1558         else {
1559                 switch (phba->link_state) {
1560                 case LPFC_LINK_UNKNOWN:
1561                 case LPFC_LINK_DOWN:
1562                         fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1563                         break;
1564                 case LPFC_LINK_UP:
1565                 case LPFC_CLEAR_LA:
1566                         /* Links up, beyond this port_type reports state */
1567                         fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1568                         break;
1569                 case LPFC_HBA_ERROR:
1570                         fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
1571                         break;
1572                 default:
1573                         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1574                         break;
1575                 }
1576         }
1577
1578         spin_unlock_irq(shost->host_lock);
1579 }
1580
1581 static void
1582 lpfc_get_host_speed(struct Scsi_Host *shost)
1583 {
1584         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1585         struct lpfc_hba   *phba = vport->phba;
1586
1587         spin_lock_irq(shost->host_lock);
1588
1589         if (lpfc_is_link_up(phba)) {
1590                 switch(phba->fc_linkspeed) {
1591                         case LA_1GHZ_LINK:
1592                                 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1593                         break;
1594                         case LA_2GHZ_LINK:
1595                                 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1596                         break;
1597                         case LA_4GHZ_LINK:
1598                                 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1599                         break;
1600                         case LA_8GHZ_LINK:
1601                                 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
1602                         break;
1603                         default:
1604                                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1605                         break;
1606                 }
1607         }
1608
1609         spin_unlock_irq(shost->host_lock);
1610 }
1611
1612 static void
1613 lpfc_get_host_fabric_name (struct Scsi_Host *shost)
1614 {
1615         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1616         struct lpfc_hba   *phba = vport->phba;
1617         u64 node_name;
1618
1619         spin_lock_irq(shost->host_lock);
1620
1621         if ((vport->fc_flag & FC_FABRIC) ||
1622             ((phba->fc_topology == TOPOLOGY_LOOP) &&
1623              (vport->fc_flag & FC_PUBLIC_LOOP)))
1624                 node_name = wwn_to_u64(phba->fc_fabparam.nodeName.u.wwn);
1625         else
1626                 /* fabric is local port if there is no F/FL_Port */
1627                 node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1628
1629         spin_unlock_irq(shost->host_lock);
1630
1631         fc_host_fabric_name(shost) = node_name;
1632 }
1633
1634 static struct fc_host_statistics *
1635 lpfc_get_stats(struct Scsi_Host *shost)
1636 {
1637         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1638         struct lpfc_hba   *phba = vport->phba;
1639         struct lpfc_sli   *psli = &phba->sli;
1640         struct fc_host_statistics *hs = &phba->link_stats;
1641         struct lpfc_lnk_stat * lso = &psli->lnk_stat_offsets;
1642         LPFC_MBOXQ_t *pmboxq;
1643         MAILBOX_t *pmb;
1644         unsigned long seconds;
1645         int rc = 0;
1646
1647                                 /* prevent udev from issuing mailbox commands
1648                                  * until the port is configured.
1649                                  */
1650         if (phba->link_state < LPFC_LINK_DOWN ||
1651             !phba->mbox_mem_pool ||
1652             (phba->sli.sli_flag & LPFC_SLI2_ACTIVE) == 0)
1653                         return NULL;
1654
1655         if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
1656                 return NULL;
1657
1658         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1659         if (!pmboxq)
1660                 return NULL;
1661         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1662
1663         pmb = &pmboxq->mb;
1664         pmb->mbxCommand = MBX_READ_STATUS;
1665         pmb->mbxOwner = OWN_HOST;
1666         pmboxq->context1 = NULL;
1667
1668         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
1669                 (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1670                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1671         else
1672                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1673
1674         if (rc != MBX_SUCCESS) {
1675                 if (rc == MBX_TIMEOUT)
1676                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1677                 else
1678                         mempool_free(pmboxq, phba->mbox_mem_pool);
1679                 return NULL;
1680         }
1681
1682         memset(hs, 0, sizeof (struct fc_host_statistics));
1683
1684         hs->tx_frames = pmb->un.varRdStatus.xmitFrameCnt;
1685         hs->tx_words = (pmb->un.varRdStatus.xmitByteCnt * 256);
1686         hs->rx_frames = pmb->un.varRdStatus.rcvFrameCnt;
1687         hs->rx_words = (pmb->un.varRdStatus.rcvByteCnt * 256);
1688
1689         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1690         pmb->mbxCommand = MBX_READ_LNK_STAT;
1691         pmb->mbxOwner = OWN_HOST;
1692         pmboxq->context1 = NULL;
1693
1694         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
1695             (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1696                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1697         else
1698                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1699
1700         if (rc != MBX_SUCCESS) {
1701                 if (rc == MBX_TIMEOUT)
1702                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1703                 else
1704                         mempool_free( pmboxq, phba->mbox_mem_pool);
1705                 return NULL;
1706         }
1707
1708         hs->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
1709         hs->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
1710         hs->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
1711         hs->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
1712         hs->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
1713         hs->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
1714         hs->error_frames = pmb->un.varRdLnk.crcCnt;
1715
1716         hs->link_failure_count -= lso->link_failure_count;
1717         hs->loss_of_sync_count -= lso->loss_of_sync_count;
1718         hs->loss_of_signal_count -= lso->loss_of_signal_count;
1719         hs->prim_seq_protocol_err_count -= lso->prim_seq_protocol_err_count;
1720         hs->invalid_tx_word_count -= lso->invalid_tx_word_count;
1721         hs->invalid_crc_count -= lso->invalid_crc_count;
1722         hs->error_frames -= lso->error_frames;
1723
1724         if (phba->fc_topology == TOPOLOGY_LOOP) {
1725                 hs->lip_count = (phba->fc_eventTag >> 1);
1726                 hs->lip_count -= lso->link_events;
1727                 hs->nos_count = -1;
1728         } else {
1729                 hs->lip_count = -1;
1730                 hs->nos_count = (phba->fc_eventTag >> 1);
1731                 hs->nos_count -= lso->link_events;
1732         }
1733
1734         hs->dumped_frames = -1;
1735
1736         seconds = get_seconds();
1737         if (seconds < psli->stats_start)
1738                 hs->seconds_since_last_reset = seconds +
1739                                 ((unsigned long)-1 - psli->stats_start);
1740         else
1741                 hs->seconds_since_last_reset = seconds - psli->stats_start;
1742
1743         mempool_free(pmboxq, phba->mbox_mem_pool);
1744
1745         return hs;
1746 }
1747
1748 static void
1749 lpfc_reset_stats(struct Scsi_Host *shost)
1750 {
1751         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1752         struct lpfc_hba   *phba = vport->phba;
1753         struct lpfc_sli   *psli = &phba->sli;
1754         struct lpfc_lnk_stat *lso = &psli->lnk_stat_offsets;
1755         LPFC_MBOXQ_t *pmboxq;
1756         MAILBOX_t *pmb;
1757         int rc = 0;
1758
1759         if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
1760                 return;
1761
1762         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1763         if (!pmboxq)
1764                 return;
1765         memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1766
1767         pmb = &pmboxq->mb;
1768         pmb->mbxCommand = MBX_READ_STATUS;
1769         pmb->mbxOwner = OWN_HOST;
1770         pmb->un.varWords[0] = 0x1; /* reset request */
1771         pmboxq->context1 = NULL;
1772
1773         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
1774                 (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1775                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1776         else
1777                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1778
1779         if (rc != MBX_SUCCESS) {
1780                 if (rc == MBX_TIMEOUT)
1781                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1782                 else
1783                         mempool_free(pmboxq, phba->mbox_mem_pool);
1784                 return;
1785         }
1786
1787         memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1788         pmb->mbxCommand = MBX_READ_LNK_STAT;
1789         pmb->mbxOwner = OWN_HOST;
1790         pmboxq->context1 = NULL;
1791
1792         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
1793             (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1794                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1795         else
1796                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1797
1798         if (rc != MBX_SUCCESS) {
1799                 if (rc == MBX_TIMEOUT)
1800                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1801                 else
1802                         mempool_free( pmboxq, phba->mbox_mem_pool);
1803                 return;
1804         }
1805
1806         lso->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
1807         lso->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
1808         lso->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
1809         lso->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
1810         lso->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
1811         lso->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
1812         lso->error_frames = pmb->un.varRdLnk.crcCnt;
1813         lso->link_events = (phba->fc_eventTag >> 1);
1814
1815         psli->stats_start = get_seconds();
1816
1817         mempool_free(pmboxq, phba->mbox_mem_pool);
1818
1819         return;
1820 }
1821
1822 /*
1823  * The LPFC driver treats linkdown handling as target loss events so there
1824  * are no sysfs handlers for link_down_tmo.
1825  */
1826
1827 static struct lpfc_nodelist *
1828 lpfc_get_node_by_target(struct scsi_target *starget)
1829 {
1830         struct Scsi_Host  *shost = dev_to_shost(starget->dev.parent);
1831         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1832         struct lpfc_nodelist *ndlp;
1833
1834         spin_lock_irq(shost->host_lock);
1835         /* Search for this, mapped, target ID */
1836         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1837                 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1838                     starget->id == ndlp->nlp_sid) {
1839                         spin_unlock_irq(shost->host_lock);
1840                         return ndlp;
1841                 }
1842         }
1843         spin_unlock_irq(shost->host_lock);
1844         return NULL;
1845 }
1846
1847 static void
1848 lpfc_get_starget_port_id(struct scsi_target *starget)
1849 {
1850         struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
1851
1852         fc_starget_port_id(starget) = ndlp ? ndlp->nlp_DID : -1;
1853 }
1854
1855 static void
1856 lpfc_get_starget_node_name(struct scsi_target *starget)
1857 {
1858         struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
1859
1860         fc_starget_node_name(starget) =
1861                 ndlp ? wwn_to_u64(ndlp->nlp_nodename.u.wwn) : 0;
1862 }
1863
1864 static void
1865 lpfc_get_starget_port_name(struct scsi_target *starget)
1866 {
1867         struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
1868
1869         fc_starget_port_name(starget) =
1870                 ndlp ? wwn_to_u64(ndlp->nlp_portname.u.wwn) : 0;
1871 }
1872
1873 static void
1874 lpfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
1875 {
1876         if (timeout)
1877                 rport->dev_loss_tmo = timeout;
1878         else
1879                 rport->dev_loss_tmo = 1;
1880 }
1881
1882
1883 #define lpfc_rport_show_function(field, format_string, sz, cast)        \
1884 static ssize_t                                                          \
1885 lpfc_show_rport_##field (struct class_device *cdev, char *buf)          \
1886 {                                                                       \
1887         struct fc_rport *rport = transport_class_to_rport(cdev);        \
1888         struct lpfc_rport_data *rdata = rport->hostdata;                \
1889         return snprintf(buf, sz, format_string,                         \
1890                 (rdata->target) ? cast rdata->target->field : 0);       \
1891 }
1892
1893 #define lpfc_rport_rd_attr(field, format_string, sz)                    \
1894         lpfc_rport_show_function(field, format_string, sz, )            \
1895 static FC_RPORT_ATTR(field, S_IRUGO, lpfc_show_rport_##field, NULL)
1896
1897
1898 struct fc_function_template lpfc_transport_functions = {
1899         /* fixed attributes the driver supports */
1900         .show_host_node_name = 1,
1901         .show_host_port_name = 1,
1902         .show_host_supported_classes = 1,
1903         .show_host_supported_fc4s = 1,
1904         .show_host_supported_speeds = 1,
1905         .show_host_maxframe_size = 1,
1906
1907         /* dynamic attributes the driver supports */
1908         .get_host_port_id = lpfc_get_host_port_id,
1909         .show_host_port_id = 1,
1910
1911         .get_host_port_type = lpfc_get_host_port_type,
1912         .show_host_port_type = 1,
1913
1914         .get_host_port_state = lpfc_get_host_port_state,
1915         .show_host_port_state = 1,
1916
1917         /* active_fc4s is shown but doesn't change (thus no get function) */
1918         .show_host_active_fc4s = 1,
1919
1920         .get_host_speed = lpfc_get_host_speed,
1921         .show_host_speed = 1,
1922
1923         .get_host_fabric_name = lpfc_get_host_fabric_name,
1924         .show_host_fabric_name = 1,
1925
1926         /*
1927          * The LPFC driver treats linkdown handling as target loss events
1928          * so there are no sysfs handlers for link_down_tmo.
1929          */
1930
1931         .get_fc_host_stats = lpfc_get_stats,
1932         .reset_fc_host_stats = lpfc_reset_stats,
1933
1934         .dd_fcrport_size = sizeof(struct lpfc_rport_data),
1935         .show_rport_maxframe_size = 1,
1936         .show_rport_supported_classes = 1,
1937
1938         .set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
1939         .show_rport_dev_loss_tmo = 1,
1940
1941         .get_starget_port_id  = lpfc_get_starget_port_id,
1942         .show_starget_port_id = 1,
1943
1944         .get_starget_node_name = lpfc_get_starget_node_name,
1945         .show_starget_node_name = 1,
1946
1947         .get_starget_port_name = lpfc_get_starget_port_name,
1948         .show_starget_port_name = 1,
1949
1950         .issue_fc_host_lip = lpfc_issue_lip,
1951         .dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
1952         .terminate_rport_io = lpfc_terminate_rport_io,
1953 };
1954
1955 void
1956 lpfc_get_cfgparam(struct lpfc_hba *phba)
1957 {
1958         lpfc_log_verbose_init(phba, lpfc_log_verbose);
1959         lpfc_cr_delay_init(phba, lpfc_cr_delay);
1960         lpfc_cr_count_init(phba, lpfc_cr_count);
1961         lpfc_multi_ring_support_init(phba, lpfc_multi_ring_support);
1962         lpfc_multi_ring_rctl_init(phba, lpfc_multi_ring_rctl);
1963         lpfc_multi_ring_type_init(phba, lpfc_multi_ring_type);
1964         lpfc_lun_queue_depth_init(phba, lpfc_lun_queue_depth);
1965         lpfc_fcp_class_init(phba, lpfc_fcp_class);
1966         lpfc_use_adisc_init(phba, lpfc_use_adisc);
1967         lpfc_ack0_init(phba, lpfc_ack0);
1968         lpfc_topology_init(phba, lpfc_topology);
1969         lpfc_scan_down_init(phba, lpfc_scan_down);
1970         lpfc_link_speed_init(phba, lpfc_link_speed);
1971         lpfc_fdmi_on_init(phba, lpfc_fdmi_on);
1972         lpfc_discovery_threads_init(phba, lpfc_discovery_threads);
1973         lpfc_max_luns_init(phba, lpfc_max_luns);
1974         lpfc_poll_tmo_init(phba, lpfc_poll_tmo);
1975         lpfc_use_msi_init(phba, lpfc_use_msi);
1976         lpfc_devloss_tmo_init(phba, lpfc_devloss_tmo);
1977         lpfc_nodev_tmo_init(phba, lpfc_nodev_tmo);
1978         phba->cfg_poll = lpfc_poll;
1979         phba->cfg_soft_wwnn = 0L;
1980         phba->cfg_soft_wwpn = 0L;
1981
1982         /*
1983          * The total number of segments is the configuration value plus 2
1984          * since the IOCB need a command and response bde.
1985          */
1986         phba->cfg_sg_seg_cnt = LPFC_SG_SEG_CNT + 2;
1987
1988         /*
1989          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
1990          * used to create the sg_dma_buf_pool must be dynamically calculated
1991          */
1992         phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
1993                         sizeof(struct fcp_rsp) +
1994                         (phba->cfg_sg_seg_cnt * sizeof(struct ulp_bde64));
1995
1996
1997         lpfc_hba_queue_depth_init(phba, lpfc_hba_queue_depth);
1998
1999         return;
2000 }