]> err.no Git - linux-2.6/blob - drivers/scsi/lpfc/lpfc_ct.c
[SCSI] lpfc: NPIV: add SLI-3 interface
[linux-2.6] / drivers / scsi / lpfc / lpfc_ct.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  *                                                                 *
8  * This program is free software; you can redistribute it and/or   *
9  * modify it under the terms of version 2 of the GNU General       *
10  * Public License as published by the Free Software Foundation.    *
11  * This program is distributed in the hope that it will be useful. *
12  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
13  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
14  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
15  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
17  * more details, a copy of which can be found in the file COPYING  *
18  * included with this package.                                     *
19  *******************************************************************/
20
21 /*
22  * Fibre Channel SCSI LAN Device Driver CT support
23  */
24
25 #include <linux/blkdev.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <linux/utsname.h>
29
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport_fc.h>
34
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_version.h"
43
44 #define HBA_PORTSPEED_UNKNOWN               0   /* Unknown - transceiver
45                                                  * incapable of reporting */
46 #define HBA_PORTSPEED_1GBIT                 1   /* 1 GBit/sec */
47 #define HBA_PORTSPEED_2GBIT                 2   /* 2 GBit/sec */
48 #define HBA_PORTSPEED_4GBIT                 8   /* 4 GBit/sec */
49 #define HBA_PORTSPEED_8GBIT                16   /* 8 GBit/sec */
50 #define HBA_PORTSPEED_10GBIT                4   /* 10 GBit/sec */
51 #define HBA_PORTSPEED_NOT_NEGOTIATED        5   /* Speed not established */
52
53 #define FOURBYTES       4
54
55
56 static char *lpfc_release_version = LPFC_DRIVER_VERSION;
57
58 /*
59  * lpfc_ct_unsol_event
60  */
61 static void
62 lpfc_ct_unsol_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
63                      struct lpfc_dmabuf *mp, uint32_t size)
64 {
65         if (!mp) {
66                 printk(KERN_ERR "%s (%d): Unsolited CT, no buffer, "
67                        "piocbq = %p, status = x%x, mp = %p, size = %d\n",
68                        __FUNCTION__, __LINE__,
69                        piocbq, piocbq->iocb.ulpStatus, mp, size);
70         }
71
72         printk(KERN_ERR "%s (%d): Ignoring unsolicted CT piocbq = %p, "
73                "buffer = %p, size = %d, status = x%x\n",
74                __FUNCTION__, __LINE__,
75                piocbq, mp, size,
76                piocbq->iocb.ulpStatus);
77 }
78
79 static void
80 lpfc_ct_ignore_hbq_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
81                           struct hbq_dmabuf *sp, uint32_t size)
82 {
83         struct lpfc_dmabuf *mp = NULL;
84
85         mp = sp ? &sp->dbuf : NULL;
86         if (!mp) {
87                 printk(KERN_ERR "%s (%d): Unsolited CT, no "
88                        "HBQ buffer, piocbq = %p, status = x%x\n",
89                        __FUNCTION__, __LINE__,
90                        piocbq, piocbq->iocb.ulpStatus);
91         } else {
92                 lpfc_ct_unsol_buffer(phba, piocbq, mp, size);
93                 printk(KERN_ERR "%s (%d): Ignoring unsolicted CT "
94                        "piocbq = %p, buffer = %p, size = %d, "
95                        "status = x%x\n",
96                        __FUNCTION__, __LINE__,
97                        piocbq, mp, size, piocbq->iocb.ulpStatus);
98         }
99 }
100
101 void
102 lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
103                     struct lpfc_iocbq *piocbq)
104 {
105         struct lpfc_dmabuf *mp = NULL;
106         struct hbq_dmabuf  *sp = NULL;
107         IOCB_t *icmd = &piocbq->iocb;
108         int i;
109         struct lpfc_iocbq *iocbq;
110         dma_addr_t paddr;
111         uint32_t size;
112
113         if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
114             ((icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING)) {
115                 /* Not enough posted buffers; Try posting more buffers */
116                 phba->fc_stat.NoRcvBuf++;
117                 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
118                         lpfc_sli_hbqbuf_fill_hbq(phba);
119                 else
120                         lpfc_post_buffer(phba, pring, 0, 1);
121                 return;
122         }
123
124         /* If there are no BDEs associated with this IOCB,
125          * there is nothing to do.
126          */
127         if (icmd->ulpBdeCount == 0)
128                 return;
129
130         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
131                 list_for_each_entry(iocbq, &piocbq->list, list) {
132                         icmd = &iocbq->iocb;
133                         if (icmd->ulpBdeCount == 0) {
134                                 printk(KERN_ERR "%s (%d): Unsolited CT, no "
135                                        "BDE, iocbq = %p, status = x%x\n",
136                                        __FUNCTION__, __LINE__,
137                                        iocbq, iocbq->iocb.ulpStatus);
138                                 continue;
139                         }
140
141                         size  = icmd->un.cont64[0].tus.f.bdeSize;
142                         sp = lpfc_sli_hbqbuf_find(phba, icmd->un.ulpWord[3]);
143                         if (sp)
144                                 phba->hbq_buff_count--;
145                         lpfc_ct_ignore_hbq_buffer(phba, iocbq, sp, size);
146                         lpfc_sli_free_hbq(phba, sp);
147                         if (icmd->ulpBdeCount == 2) {
148                                 sp = lpfc_sli_hbqbuf_find(phba,
149                                                           icmd->un.ulpWord[15]);
150                                 if (sp)
151                                         phba->hbq_buff_count--;
152                                 lpfc_ct_ignore_hbq_buffer(phba, iocbq, sp,
153                                                           size);
154                                 lpfc_sli_free_hbq(phba, sp);
155                         }
156
157                 }
158                 lpfc_sli_hbqbuf_fill_hbq(phba);
159         } else {
160                 struct lpfc_iocbq  *next;
161
162                 list_for_each_entry_safe(iocbq, next, &piocbq->list, list) {
163                         icmd = &iocbq->iocb;
164                         if (icmd->ulpBdeCount == 0) {
165                                 printk(KERN_ERR "%s (%d): Unsolited CT, no "
166                                        "BDE, iocbq = %p, status = x%x\n",
167                                        __FUNCTION__, __LINE__,
168                                        iocbq, iocbq->iocb.ulpStatus);
169                                 continue;
170                         }
171
172                         for (i = 0; i < icmd->ulpBdeCount; i++) {
173                                 paddr = getPaddr(icmd->un.cont64[i].addrHigh,
174                                                  icmd->un.cont64[i].addrLow);
175                                 mp = lpfc_sli_ringpostbuf_get(phba, pring,
176                                                               paddr);
177                                 size = icmd->un.cont64[i].tus.f.bdeSize;
178                                 lpfc_ct_unsol_buffer(phba, piocbq, mp, size);
179                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
180                                 kfree(mp);
181                         }
182                         list_del(&iocbq->list);
183                         lpfc_sli_release_iocbq(phba, iocbq);
184                 }
185         }
186 }
187
188 static void
189 lpfc_free_ct_rsp(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
190 {
191         struct lpfc_dmabuf *mlast, *next_mlast;
192
193         list_for_each_entry_safe(mlast, next_mlast, &mlist->list, list) {
194                 lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
195                 list_del(&mlast->list);
196                 kfree(mlast);
197         }
198         lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
199         kfree(mlist);
200         return;
201 }
202
203 static struct lpfc_dmabuf *
204 lpfc_alloc_ct_rsp(struct lpfc_hba *phba, int cmdcode, struct ulp_bde64 *bpl,
205                   uint32_t size, int *entries)
206 {
207         struct lpfc_dmabuf *mlist = NULL;
208         struct lpfc_dmabuf *mp;
209         int cnt, i = 0;
210
211         /* We get chucks of FCELSSIZE */
212         cnt = size > FCELSSIZE ? FCELSSIZE: size;
213
214         while (size) {
215                 /* Allocate buffer for rsp payload */
216                 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
217                 if (!mp) {
218                         if (mlist)
219                                 lpfc_free_ct_rsp(phba, mlist);
220                         return NULL;
221                 }
222
223                 INIT_LIST_HEAD(&mp->list);
224
225                 if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT))
226                         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
227                 else
228                         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
229
230                 if (!mp->virt) {
231                         kfree(mp);
232                         if (mlist)
233                                 lpfc_free_ct_rsp(phba, mlist);
234                         return NULL;
235                 }
236
237                 /* Queue it to a linked list */
238                 if (!mlist)
239                         mlist = mp;
240                 else
241                         list_add_tail(&mp->list, &mlist->list);
242
243                 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
244                 /* build buffer ptr list for IOCB */
245                 bpl->addrLow = le32_to_cpu( putPaddrLow(mp->phys) );
246                 bpl->addrHigh = le32_to_cpu( putPaddrHigh(mp->phys) );
247                 bpl->tus.f.bdeSize = (uint16_t) cnt;
248                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
249                 bpl++;
250
251                 i++;
252                 size -= cnt;
253         }
254
255         *entries = i;
256         return mlist;
257 }
258
259 static int
260 lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
261              struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
262              void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
263                      struct lpfc_iocbq *),
264              struct lpfc_nodelist *ndlp, uint32_t usr_flg, uint32_t num_entry,
265              uint32_t tmo)
266 {
267         struct lpfc_hba  *phba = vport->phba;
268         struct lpfc_sli  *psli = &phba->sli;
269         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
270         IOCB_t *icmd;
271         struct lpfc_iocbq *geniocb;
272
273         /* Allocate buffer for  command iocb */
274         geniocb = lpfc_sli_get_iocbq(phba);
275
276         if (geniocb == NULL)
277                 return 1;
278
279         icmd = &geniocb->iocb;
280         icmd->un.genreq64.bdl.ulpIoTag32 = 0;
281         icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
282         icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
283         icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BDL;
284         icmd->un.genreq64.bdl.bdeSize = (num_entry * sizeof (struct ulp_bde64));
285
286         if (usr_flg)
287                 geniocb->context3 = NULL;
288         else
289                 geniocb->context3 = (uint8_t *) bmp;
290
291         /* Save for completion so we can release these resources */
292         geniocb->context1 = (uint8_t *) inp;
293         geniocb->context2 = (uint8_t *) outp;
294
295         /* Fill in payload, bp points to frame payload */
296         icmd->ulpCommand = CMD_GEN_REQUEST64_CR;
297
298         /* Fill in rest of iocb */
299         icmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
300         icmd->un.genreq64.w5.hcsw.Dfctl = 0;
301         icmd->un.genreq64.w5.hcsw.Rctl = FC_UNSOL_CTL;
302         icmd->un.genreq64.w5.hcsw.Type = FC_COMMON_TRANSPORT_ULP;
303
304         if (!tmo) {
305                  /* FC spec states we need 3 * ratov for CT requests */
306                 tmo = (3 * phba->fc_ratov);
307         }
308         icmd->ulpTimeout = tmo;
309         icmd->ulpBdeCount = 1;
310         icmd->ulpLe = 1;
311         icmd->ulpClass = CLASS3;
312         icmd->ulpContext = ndlp->nlp_rpi;
313
314         /* Issue GEN REQ IOCB for NPORT <did> */
315         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
316                         "%d:0119 Issue GEN REQ IOCB for NPORT x%x "
317                         "Data: x%x x%x\n", phba->brd_no, icmd->un.ulpWord[5],
318                         icmd->ulpIoTag, vport->port_state);
319         geniocb->iocb_cmpl = cmpl;
320         geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
321         geniocb->vport = vport;
322         if (lpfc_sli_issue_iocb(phba, pring, geniocb, 0) == IOCB_ERROR) {
323                 lpfc_sli_release_iocbq(phba, geniocb);
324                 return 1;
325         }
326
327         return 0;
328 }
329
330 static int
331 lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
332             struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
333             void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
334                           struct lpfc_iocbq *),
335             uint32_t rsp_size)
336 {
337         struct lpfc_hba  *phba = vport->phba;
338         struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
339         struct lpfc_dmabuf *outmp;
340         int cnt = 0, status;
341         int cmdcode = ((struct lpfc_sli_ct_request *) inmp->virt)->
342                 CommandResponse.bits.CmdRsp;
343
344         bpl++;                  /* Skip past ct request */
345
346         /* Put buffer(s) for ct rsp in bpl */
347         outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
348         if (!outmp)
349                 return -ENOMEM;
350
351         status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp, 0,
352                               cnt+1, 0);
353         if (status) {
354                 lpfc_free_ct_rsp(phba, outmp);
355                 return -ENOMEM;
356         }
357         return 0;
358 }
359
360 static int
361 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint32_t Size)
362 {
363         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
364         struct lpfc_hba  *phba = vport->phba;
365         struct lpfc_sli_ct_request *Response =
366                 (struct lpfc_sli_ct_request *) mp->virt;
367         struct lpfc_nodelist *ndlp = NULL;
368         struct lpfc_dmabuf *mlast, *next_mp;
369         uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
370         uint32_t Did, CTentry;
371         int Cnt;
372         struct list_head head;
373
374         lpfc_set_disctmo(vport);
375
376
377         list_add_tail(&head, &mp->list);
378         list_for_each_entry_safe(mp, next_mp, &head, list) {
379                 mlast = mp;
380
381                 Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;
382
383                 Size -= Cnt;
384
385                 if (!ctptr) {
386                         ctptr = (uint32_t *) mlast->virt;
387                 } else
388                         Cnt -= 16;      /* subtract length of CT header */
389
390                 /* Loop through entire NameServer list of DIDs */
391                 while (Cnt >= sizeof (uint32_t)) {
392                         /* Get next DID from NameServer List */
393                         CTentry = *ctptr++;
394                         Did = ((be32_to_cpu(CTentry)) & Mask_DID);
395                         ndlp = NULL;
396                         /* Check for rscn processing or not */
397                         if (Did != vport->fc_myDID)
398                                 ndlp = lpfc_setup_disc_node(vport, Did);
399                         if (ndlp) {
400                                 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
401                                                 "%d:0238 Process x%x NameServer"
402                                                 " Rsp Data: x%x x%x x%x\n",
403                                                 phba->brd_no,
404                                                 Did, ndlp->nlp_flag,
405                                                 vport->fc_flag,
406                                                 vport->fc_rscn_id_cnt);
407                         } else {
408                                 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
409                                                 "%d:0239 Skip x%x NameServer "
410                                                 "Rsp Data: x%x x%x x%x\n",
411                                                 phba->brd_no,
412                                                 Did, Size, vport->fc_flag,
413                                                 vport->fc_rscn_id_cnt);
414                         }
415                         if (CTentry & (be32_to_cpu(SLI_CT_LAST_ENTRY)))
416                                 goto nsout1;
417                         Cnt -= sizeof (uint32_t);
418                 }
419                 ctptr = NULL;
420
421         }
422
423 nsout1:
424         list_del(&head);
425
426         /*
427          * The driver has cycled through all Nports in the RSCN payload.
428          * Complete the handling by cleaning up and marking the
429          * current driver state.
430          */
431         if (vport->port_state == LPFC_VPORT_READY) {
432                 lpfc_els_flush_rscn(vport);
433                 spin_lock_irq(shost->host_lock);
434                 vport->fc_flag |= FC_RSCN_MODE; /* we are still in RSCN mode */
435                 spin_unlock_irq(shost->host_lock);
436         }
437         return 0;
438 }
439
440
441
442
443 static void
444 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
445                         struct lpfc_iocbq *rspiocb)
446 {
447         struct lpfc_vport *vport = cmdiocb->vport;
448         IOCB_t *irsp;
449         struct lpfc_dmabuf *bmp;
450         struct lpfc_dmabuf *inp;
451         struct lpfc_dmabuf *outp;
452         struct lpfc_nodelist *ndlp;
453         struct lpfc_sli_ct_request *CTrsp;
454         int rc;
455
456         /* we pass cmdiocb to state machine which needs rspiocb as well */
457         cmdiocb->context_un.rsp_iocb = rspiocb;
458
459         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
460         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
461         bmp = (struct lpfc_dmabuf *) cmdiocb->context3;
462
463         irsp = &rspiocb->iocb;
464         if (irsp->ulpStatus) {
465                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
466                         ((irsp->un.ulpWord[4] == IOERR_SLI_DOWN) ||
467                          (irsp->un.ulpWord[4] == IOERR_SLI_ABORTED)))
468                         goto out;
469
470                 /* Check for retry */
471                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
472                         vport->fc_ns_retry++;
473                         /* CT command is being retried */
474                         ndlp = lpfc_findnode_did(vport, NameServer_DID);
475                         if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
476                                 rc = lpfc_ns_cmd(vport, ndlp, SLI_CTNS_GID_FT);
477                                 if (rc == 0)
478                                         goto out;
479                                 }
480                         }
481         } else {
482                 /* Good status, continue checking */
483                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
484                 if (CTrsp->CommandResponse.bits.CmdRsp ==
485                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
486                         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
487                                         "%d:0208 NameServer Rsp "
488                                         "Data: x%x\n",
489                                         phba->brd_no,
490                                         vport->fc_flag);
491                         lpfc_ns_rsp(vport, outp,
492                                     (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
493                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
494                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
495                         /* NameServer Rsp Error */
496                         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
497                                         "%d:0240 NameServer Rsp Error "
498                                         "Data: x%x x%x x%x x%x\n",
499                                         phba->brd_no,
500                                         CTrsp->CommandResponse.bits.CmdRsp,
501                                         (uint32_t) CTrsp->ReasonCode,
502                                         (uint32_t) CTrsp->Explanation,
503                                         vport->fc_flag);
504                 } else {
505                         /* NameServer Rsp Error */
506                         lpfc_printf_log(phba,
507                                         KERN_INFO,
508                                         LOG_DISCOVERY,
509                                         "%d:0241 NameServer Rsp Error "
510                                         "Data: x%x x%x x%x x%x\n",
511                                         phba->brd_no,
512                                         CTrsp->CommandResponse.bits.CmdRsp,
513                                         (uint32_t) CTrsp->ReasonCode,
514                                         (uint32_t) CTrsp->Explanation,
515                                         vport->fc_flag);
516                 }
517         }
518         /* Link up / RSCN discovery */
519         lpfc_disc_start(vport);
520 out:
521         lpfc_free_ct_rsp(phba, outp);
522         lpfc_mbuf_free(phba, inp->virt, inp->phys);
523         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
524         kfree(inp);
525         kfree(bmp);
526         lpfc_sli_release_iocbq(phba, cmdiocb);
527         return;
528 }
529
530 static void
531 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
532                         struct lpfc_iocbq *rspiocb)
533 {
534         struct lpfc_dmabuf *bmp;
535         struct lpfc_dmabuf *inp;
536         struct lpfc_dmabuf *outp;
537         IOCB_t *irsp;
538         struct lpfc_sli_ct_request *CTrsp;
539
540         /* we pass cmdiocb to state machine which needs rspiocb as well */
541         cmdiocb->context_un.rsp_iocb = rspiocb;
542
543         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
544         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
545         bmp = (struct lpfc_dmabuf *) cmdiocb->context3;
546         irsp = &rspiocb->iocb;
547
548         CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
549
550         /* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
551         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
552                         "%d:0209 RFT request completes ulpStatus x%x "
553                         "CmdRsp x%x, Context x%x, Tag x%x\n",
554                         phba->brd_no, irsp->ulpStatus,
555                         CTrsp->CommandResponse.bits.CmdRsp,
556                         cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
557
558         lpfc_free_ct_rsp(phba, outp);
559         lpfc_mbuf_free(phba, inp->virt, inp->phys);
560         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
561         kfree(inp);
562         kfree(bmp);
563         lpfc_sli_release_iocbq(phba, cmdiocb);
564         return;
565 }
566
567 static void
568 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
569                         struct lpfc_iocbq *rspiocb)
570 {
571         lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
572         return;
573 }
574
575 static void
576 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
577                          struct lpfc_iocbq *rspiocb)
578 {
579         lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
580         return;
581 }
582
583 static void
584 lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
585                          struct lpfc_iocbq * rspiocb)
586 {
587         lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
588         return;
589 }
590
591 void
592 lpfc_get_hba_sym_node_name(struct lpfc_hba *phba, uint8_t *symbp)
593 {
594         char fwrev[16];
595
596         lpfc_decode_firmware_rev(phba, fwrev, 0);
597
598         sprintf(symbp, "Emulex %s FV%s DV%s", phba->ModelName,
599                 fwrev, lpfc_release_version);
600         return;
601 }
602
603 /*
604  * lpfc_ns_cmd
605  * Description:
606  *    Issue Cmd to NameServer
607  *       SLI_CTNS_GID_FT
608  *       LI_CTNS_RFT_ID
609  */
610 int
611 lpfc_ns_cmd(struct lpfc_vport *vport, struct lpfc_nodelist * ndlp, int cmdcode)
612 {
613         struct lpfc_hba *phba = vport->phba;
614         struct lpfc_dmabuf *mp, *bmp;
615         struct lpfc_sli_ct_request *CtReq;
616         struct ulp_bde64 *bpl;
617         void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
618                       struct lpfc_iocbq *) = NULL;
619         uint32_t rsp_size = 1024;
620
621         /* fill in BDEs for command */
622         /* Allocate buffer for command payload */
623         mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
624         if (!mp)
625                 goto ns_cmd_exit;
626
627         INIT_LIST_HEAD(&mp->list);
628         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
629         if (!mp->virt)
630                 goto ns_cmd_free_mp;
631
632         /* Allocate buffer for Buffer ptr list */
633         bmp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
634         if (!bmp)
635                 goto ns_cmd_free_mpvirt;
636
637         INIT_LIST_HEAD(&bmp->list);
638         bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
639         if (!bmp->virt)
640                 goto ns_cmd_free_bmp;
641
642         /* NameServer Req */
643         lpfc_printf_log(phba,
644                         KERN_INFO,
645                         LOG_DISCOVERY,
646                         "%d:0236 NameServer Req Data: x%x x%x x%x\n",
647                         phba->brd_no, cmdcode, vport->fc_flag,
648                         vport->fc_rscn_id_cnt);
649
650         bpl = (struct ulp_bde64 *) bmp->virt;
651         memset(bpl, 0, sizeof(struct ulp_bde64));
652         bpl->addrHigh = le32_to_cpu( putPaddrHigh(mp->phys) );
653         bpl->addrLow = le32_to_cpu( putPaddrLow(mp->phys) );
654         bpl->tus.f.bdeFlags = 0;
655         if (cmdcode == SLI_CTNS_GID_FT)
656                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
657         else if (cmdcode == SLI_CTNS_RFT_ID)
658                 bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
659         else if (cmdcode == SLI_CTNS_RNN_ID)
660                 bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
661         else if (cmdcode == SLI_CTNS_RSNN_NN)
662                 bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
663         else if (cmdcode == SLI_CTNS_RFF_ID)
664                 bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
665         else
666                 bpl->tus.f.bdeSize = 0;
667         bpl->tus.w = le32_to_cpu(bpl->tus.w);
668
669         CtReq = (struct lpfc_sli_ct_request *) mp->virt;
670         memset(CtReq, 0, sizeof (struct lpfc_sli_ct_request));
671         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
672         CtReq->RevisionId.bits.InId = 0;
673         CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
674         CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
675         CtReq->CommandResponse.bits.Size = 0;
676         switch (cmdcode) {
677         case SLI_CTNS_GID_FT:
678                 CtReq->CommandResponse.bits.CmdRsp =
679                     be16_to_cpu(SLI_CTNS_GID_FT);
680                 CtReq->un.gid.Fc4Type = SLI_CTPT_FCP;
681                 if (vport->port_state < LPFC_VPORT_READY)
682                         vport->port_state = LPFC_NS_QRY;
683                 lpfc_set_disctmo(vport);
684                 cmpl = lpfc_cmpl_ct_cmd_gid_ft;
685                 rsp_size = FC_MAX_NS_RSP;
686                 break;
687
688         case SLI_CTNS_RFT_ID:
689                 CtReq->CommandResponse.bits.CmdRsp =
690                     be16_to_cpu(SLI_CTNS_RFT_ID);
691                 CtReq->un.rft.PortId = be32_to_cpu(vport->fc_myDID);
692                 CtReq->un.rft.fcpReg = 1;
693                 cmpl = lpfc_cmpl_ct_cmd_rft_id;
694                 break;
695
696         case SLI_CTNS_RFF_ID:
697                 CtReq->CommandResponse.bits.CmdRsp =
698                         be16_to_cpu(SLI_CTNS_RFF_ID);
699                 CtReq->un.rff.PortId = be32_to_cpu(vport->fc_myDID);
700                 CtReq->un.rff.feature_res = 0;
701                 CtReq->un.rff.feature_tgt = 0;
702                 CtReq->un.rff.type_code = FC_FCP_DATA;
703                 CtReq->un.rff.feature_init = 1;
704                 cmpl = lpfc_cmpl_ct_cmd_rff_id;
705                 break;
706
707         case SLI_CTNS_RNN_ID:
708                 CtReq->CommandResponse.bits.CmdRsp =
709                     be16_to_cpu(SLI_CTNS_RNN_ID);
710                 CtReq->un.rnn.PortId = be32_to_cpu(vport->fc_myDID);
711                 memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
712                        sizeof (struct lpfc_name));
713                 cmpl = lpfc_cmpl_ct_cmd_rnn_id;
714                 break;
715
716         case SLI_CTNS_RSNN_NN:
717                 CtReq->CommandResponse.bits.CmdRsp =
718                     be16_to_cpu(SLI_CTNS_RSNN_NN);
719                 memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
720                        sizeof (struct lpfc_name));
721                 lpfc_get_hba_sym_node_name(phba, CtReq->un.rsnn.symbname);
722                 CtReq->un.rsnn.len = strlen(CtReq->un.rsnn.symbname);
723                 cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
724                 break;
725         }
726
727         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size))
728                 /* On success, The cmpl function will free the buffers */
729                 return 0;
730
731         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
732 ns_cmd_free_bmp:
733         kfree(bmp);
734 ns_cmd_free_mpvirt:
735         lpfc_mbuf_free(phba, mp->virt, mp->phys);
736 ns_cmd_free_mp:
737         kfree(mp);
738 ns_cmd_exit:
739         return 1;
740 }
741
742 static void
743 lpfc_cmpl_ct_cmd_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
744                       struct lpfc_iocbq * rspiocb)
745 {
746         struct lpfc_dmabuf *bmp = cmdiocb->context3;
747         struct lpfc_dmabuf *inp = cmdiocb->context1;
748         struct lpfc_dmabuf *outp = cmdiocb->context2;
749         struct lpfc_sli_ct_request *CTrsp = outp->virt;
750         struct lpfc_sli_ct_request *CTcmd = inp->virt;
751         struct lpfc_nodelist *ndlp;
752         uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
753         uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
754         struct lpfc_vport *vport = cmdiocb->vport;
755
756         ndlp = lpfc_findnode_did(vport, FDMI_DID);
757         if (fdmi_rsp == be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
758                 /* FDMI rsp failed */
759                 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
760                                 "%d:0220 FDMI rsp failed Data: x%x\n",
761                                 phba->brd_no, be16_to_cpu(fdmi_cmd));
762         }
763
764         switch (be16_to_cpu(fdmi_cmd)) {
765         case SLI_MGMT_RHBA:
766                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA);
767                 break;
768
769         case SLI_MGMT_RPA:
770                 break;
771
772         case SLI_MGMT_DHBA:
773                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT);
774                 break;
775
776         case SLI_MGMT_DPRT:
777                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA);
778                 break;
779         }
780
781         lpfc_free_ct_rsp(phba, outp);
782         lpfc_mbuf_free(phba, inp->virt, inp->phys);
783         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
784         kfree(inp);
785         kfree(bmp);
786         lpfc_sli_release_iocbq(phba, cmdiocb);
787         return;
788 }
789
790 int
791 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp, int cmdcode)
792 {
793         struct lpfc_hba *phba = vport->phba;
794         struct lpfc_dmabuf *mp, *bmp;
795         struct lpfc_sli_ct_request *CtReq;
796         struct ulp_bde64 *bpl;
797         uint32_t size;
798         REG_HBA *rh;
799         PORT_ENTRY *pe;
800         REG_PORT_ATTRIBUTE *pab;
801         ATTRIBUTE_BLOCK *ab;
802         ATTRIBUTE_ENTRY *ae;
803         void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
804                       struct lpfc_iocbq *);
805
806
807         /* fill in BDEs for command */
808         /* Allocate buffer for command payload */
809         mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
810         if (!mp)
811                 goto fdmi_cmd_exit;
812
813         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
814         if (!mp->virt)
815                 goto fdmi_cmd_free_mp;
816
817         /* Allocate buffer for Buffer ptr list */
818         bmp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
819         if (!bmp)
820                 goto fdmi_cmd_free_mpvirt;
821
822         bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
823         if (!bmp->virt)
824                 goto fdmi_cmd_free_bmp;
825
826         INIT_LIST_HEAD(&mp->list);
827         INIT_LIST_HEAD(&bmp->list);
828
829         /* FDMI request */
830         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
831                         "%d:0218 FDMI Request Data: x%x x%x x%x\n",
832                         phba->brd_no,
833                         vport->fc_flag, vport->port_state, cmdcode);
834
835         CtReq = (struct lpfc_sli_ct_request *) mp->virt;
836
837         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
838         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
839         CtReq->RevisionId.bits.InId = 0;
840
841         CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
842         CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
843         size = 0;
844
845         switch (cmdcode) {
846         case SLI_MGMT_RHBA:
847                 {
848                         lpfc_vpd_t *vp = &phba->vpd;
849                         uint32_t i, j, incr;
850                         int len;
851
852                         CtReq->CommandResponse.bits.CmdRsp =
853                             be16_to_cpu(SLI_MGMT_RHBA);
854                         CtReq->CommandResponse.bits.Size = 0;
855                         rh = (REG_HBA *) & CtReq->un.PortID;
856                         memcpy(&rh->hi.PortName, &vport->fc_sparam.portName,
857                                sizeof (struct lpfc_name));
858                         /* One entry (port) per adapter */
859                         rh->rpl.EntryCnt = be32_to_cpu(1);
860                         memcpy(&rh->rpl.pe, &vport->fc_sparam.portName,
861                                sizeof (struct lpfc_name));
862
863                         /* point to the HBA attribute block */
864                         size = 2 * sizeof (struct lpfc_name) + FOURBYTES;
865                         ab = (ATTRIBUTE_BLOCK *) ((uint8_t *) rh + size);
866                         ab->EntryCnt = 0;
867
868                         /* Point to the beginning of the first HBA attribute
869                            entry */
870                         /* #1 HBA attribute entry */
871                         size += FOURBYTES;
872                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
873                         ae->ad.bits.AttrType = be16_to_cpu(NODE_NAME);
874                         ae->ad.bits.AttrLen =  be16_to_cpu(FOURBYTES
875                                                 + sizeof (struct lpfc_name));
876                         memcpy(&ae->un.NodeName, &vport->fc_sparam.nodeName,
877                                sizeof (struct lpfc_name));
878                         ab->EntryCnt++;
879                         size += FOURBYTES + sizeof (struct lpfc_name);
880
881                         /* #2 HBA attribute entry */
882                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
883                         ae->ad.bits.AttrType = be16_to_cpu(MANUFACTURER);
884                         strcpy(ae->un.Manufacturer, "Emulex Corporation");
885                         len = strlen(ae->un.Manufacturer);
886                         len += (len & 3) ? (4 - (len & 3)) : 4;
887                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
888                         ab->EntryCnt++;
889                         size += FOURBYTES + len;
890
891                         /* #3 HBA attribute entry */
892                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
893                         ae->ad.bits.AttrType = be16_to_cpu(SERIAL_NUMBER);
894                         strcpy(ae->un.SerialNumber, phba->SerialNumber);
895                         len = strlen(ae->un.SerialNumber);
896                         len += (len & 3) ? (4 - (len & 3)) : 4;
897                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
898                         ab->EntryCnt++;
899                         size += FOURBYTES + len;
900
901                         /* #4 HBA attribute entry */
902                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
903                         ae->ad.bits.AttrType = be16_to_cpu(MODEL);
904                         strcpy(ae->un.Model, phba->ModelName);
905                         len = strlen(ae->un.Model);
906                         len += (len & 3) ? (4 - (len & 3)) : 4;
907                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
908                         ab->EntryCnt++;
909                         size += FOURBYTES + len;
910
911                         /* #5 HBA attribute entry */
912                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
913                         ae->ad.bits.AttrType = be16_to_cpu(MODEL_DESCRIPTION);
914                         strcpy(ae->un.ModelDescription, phba->ModelDesc);
915                         len = strlen(ae->un.ModelDescription);
916                         len += (len & 3) ? (4 - (len & 3)) : 4;
917                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
918                         ab->EntryCnt++;
919                         size += FOURBYTES + len;
920
921                         /* #6 HBA attribute entry */
922                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
923                         ae->ad.bits.AttrType = be16_to_cpu(HARDWARE_VERSION);
924                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 8);
925                         /* Convert JEDEC ID to ascii for hardware version */
926                         incr = vp->rev.biuRev;
927                         for (i = 0; i < 8; i++) {
928                                 j = (incr & 0xf);
929                                 if (j <= 9)
930                                         ae->un.HardwareVersion[7 - i] =
931                                             (char)((uint8_t) 0x30 +
932                                                    (uint8_t) j);
933                                 else
934                                         ae->un.HardwareVersion[7 - i] =
935                                             (char)((uint8_t) 0x61 +
936                                                    (uint8_t) (j - 10));
937                                 incr = (incr >> 4);
938                         }
939                         ab->EntryCnt++;
940                         size += FOURBYTES + 8;
941
942                         /* #7 HBA attribute entry */
943                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
944                         ae->ad.bits.AttrType = be16_to_cpu(DRIVER_VERSION);
945                         strcpy(ae->un.DriverVersion, lpfc_release_version);
946                         len = strlen(ae->un.DriverVersion);
947                         len += (len & 3) ? (4 - (len & 3)) : 4;
948                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
949                         ab->EntryCnt++;
950                         size += FOURBYTES + len;
951
952                         /* #8 HBA attribute entry */
953                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
954                         ae->ad.bits.AttrType = be16_to_cpu(OPTION_ROM_VERSION);
955                         strcpy(ae->un.OptionROMVersion, phba->OptionROMVersion);
956                         len = strlen(ae->un.OptionROMVersion);
957                         len += (len & 3) ? (4 - (len & 3)) : 4;
958                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
959                         ab->EntryCnt++;
960                         size += FOURBYTES + len;
961
962                         /* #9 HBA attribute entry */
963                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
964                         ae->ad.bits.AttrType = be16_to_cpu(FIRMWARE_VERSION);
965                         lpfc_decode_firmware_rev(phba, ae->un.FirmwareVersion,
966                                 1);
967                         len = strlen(ae->un.FirmwareVersion);
968                         len += (len & 3) ? (4 - (len & 3)) : 4;
969                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
970                         ab->EntryCnt++;
971                         size += FOURBYTES + len;
972
973                         /* #10 HBA attribute entry */
974                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
975                         ae->ad.bits.AttrType = be16_to_cpu(OS_NAME_VERSION);
976                         sprintf(ae->un.OsNameVersion, "%s %s %s",
977                                 init_utsname()->sysname,
978                                 init_utsname()->release,
979                                 init_utsname()->version);
980                         len = strlen(ae->un.OsNameVersion);
981                         len += (len & 3) ? (4 - (len & 3)) : 4;
982                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
983                         ab->EntryCnt++;
984                         size += FOURBYTES + len;
985
986                         /* #11 HBA attribute entry */
987                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
988                         ae->ad.bits.AttrType = be16_to_cpu(MAX_CT_PAYLOAD_LEN);
989                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
990                         ae->un.MaxCTPayloadLen = (65 * 4096);
991                         ab->EntryCnt++;
992                         size += FOURBYTES + 4;
993
994                         ab->EntryCnt = be32_to_cpu(ab->EntryCnt);
995                         /* Total size */
996                         size = GID_REQUEST_SZ - 4 + size;
997                 }
998                 break;
999
1000         case SLI_MGMT_RPA:
1001                 {
1002                         lpfc_vpd_t *vp;
1003                         struct serv_parm *hsp;
1004                         int len;
1005
1006                         vp = &phba->vpd;
1007
1008                         CtReq->CommandResponse.bits.CmdRsp =
1009                             be16_to_cpu(SLI_MGMT_RPA);
1010                         CtReq->CommandResponse.bits.Size = 0;
1011                         pab = (REG_PORT_ATTRIBUTE *) & CtReq->un.PortID;
1012                         size = sizeof (struct lpfc_name) + FOURBYTES;
1013                         memcpy((uint8_t *) & pab->PortName,
1014                                (uint8_t *) & vport->fc_sparam.portName,
1015                                sizeof (struct lpfc_name));
1016                         pab->ab.EntryCnt = 0;
1017
1018                         /* #1 Port attribute entry */
1019                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1020                         ae->ad.bits.AttrType = be16_to_cpu(SUPPORTED_FC4_TYPES);
1021                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 32);
1022                         ae->un.SupportFC4Types[2] = 1;
1023                         ae->un.SupportFC4Types[7] = 1;
1024                         pab->ab.EntryCnt++;
1025                         size += FOURBYTES + 32;
1026
1027                         /* #2 Port attribute entry */
1028                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1029                         ae->ad.bits.AttrType = be16_to_cpu(SUPPORTED_SPEED);
1030                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1031
1032                         ae->un.SupportSpeed = 0;
1033                         if (phba->lmt & LMT_10Gb)
1034                                 ae->un.SupportSpeed = HBA_PORTSPEED_10GBIT;
1035                         if (phba->lmt & LMT_8Gb)
1036                                 ae->un.SupportSpeed |= HBA_PORTSPEED_8GBIT;
1037                         if (phba->lmt & LMT_4Gb)
1038                                 ae->un.SupportSpeed |= HBA_PORTSPEED_4GBIT;
1039                         if (phba->lmt & LMT_2Gb)
1040                                 ae->un.SupportSpeed |= HBA_PORTSPEED_2GBIT;
1041                         if (phba->lmt & LMT_1Gb)
1042                                 ae->un.SupportSpeed |= HBA_PORTSPEED_1GBIT;
1043
1044                         pab->ab.EntryCnt++;
1045                         size += FOURBYTES + 4;
1046
1047                         /* #3 Port attribute entry */
1048                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1049                         ae->ad.bits.AttrType = be16_to_cpu(PORT_SPEED);
1050                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1051                         switch(phba->fc_linkspeed) {
1052                                 case LA_1GHZ_LINK:
1053                                         ae->un.PortSpeed = HBA_PORTSPEED_1GBIT;
1054                                 break;
1055                                 case LA_2GHZ_LINK:
1056                                         ae->un.PortSpeed = HBA_PORTSPEED_2GBIT;
1057                                 break;
1058                                 case LA_4GHZ_LINK:
1059                                         ae->un.PortSpeed = HBA_PORTSPEED_4GBIT;
1060                                 break;
1061                                 case LA_8GHZ_LINK:
1062                                         ae->un.PortSpeed = HBA_PORTSPEED_8GBIT;
1063                                 break;
1064                                 default:
1065                                         ae->un.PortSpeed =
1066                                                 HBA_PORTSPEED_UNKNOWN;
1067                                 break;
1068                         }
1069                         pab->ab.EntryCnt++;
1070                         size += FOURBYTES + 4;
1071
1072                         /* #4 Port attribute entry */
1073                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1074                         ae->ad.bits.AttrType = be16_to_cpu(MAX_FRAME_SIZE);
1075                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1076                         hsp = (struct serv_parm *) & vport->fc_sparam;
1077                         ae->un.MaxFrameSize =
1078                             (((uint32_t) hsp->cmn.
1079                               bbRcvSizeMsb) << 8) | (uint32_t) hsp->cmn.
1080                             bbRcvSizeLsb;
1081                         pab->ab.EntryCnt++;
1082                         size += FOURBYTES + 4;
1083
1084                         /* #5 Port attribute entry */
1085                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1086                         ae->ad.bits.AttrType = be16_to_cpu(OS_DEVICE_NAME);
1087                         strcpy((char *)ae->un.OsDeviceName, LPFC_DRIVER_NAME);
1088                         len = strlen((char *)ae->un.OsDeviceName);
1089                         len += (len & 3) ? (4 - (len & 3)) : 4;
1090                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1091                         pab->ab.EntryCnt++;
1092                         size += FOURBYTES + len;
1093
1094                         if (phba->cfg_fdmi_on == 2) {
1095                                 /* #6 Port attribute entry */
1096                                 ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab +
1097                                                           size);
1098                                 ae->ad.bits.AttrType = be16_to_cpu(HOST_NAME);
1099                                 sprintf(ae->un.HostName, "%s",
1100                                         init_utsname()->nodename);
1101                                 len = strlen(ae->un.HostName);
1102                                 len += (len & 3) ? (4 - (len & 3)) : 4;
1103                                 ae->ad.bits.AttrLen =
1104                                     be16_to_cpu(FOURBYTES + len);
1105                                 pab->ab.EntryCnt++;
1106                                 size += FOURBYTES + len;
1107                         }
1108
1109                         pab->ab.EntryCnt = be32_to_cpu(pab->ab.EntryCnt);
1110                         /* Total size */
1111                         size = GID_REQUEST_SZ - 4 + size;
1112                 }
1113                 break;
1114
1115         case SLI_MGMT_DHBA:
1116                 CtReq->CommandResponse.bits.CmdRsp = be16_to_cpu(SLI_MGMT_DHBA);
1117                 CtReq->CommandResponse.bits.Size = 0;
1118                 pe = (PORT_ENTRY *) & CtReq->un.PortID;
1119                 memcpy((uint8_t *) & pe->PortName,
1120                        (uint8_t *) & vport->fc_sparam.portName,
1121                        sizeof (struct lpfc_name));
1122                 size = GID_REQUEST_SZ - 4 + sizeof (struct lpfc_name);
1123                 break;
1124
1125         case SLI_MGMT_DPRT:
1126                 CtReq->CommandResponse.bits.CmdRsp = be16_to_cpu(SLI_MGMT_DPRT);
1127                 CtReq->CommandResponse.bits.Size = 0;
1128                 pe = (PORT_ENTRY *) & CtReq->un.PortID;
1129                 memcpy((uint8_t *) & pe->PortName,
1130                        (uint8_t *) & vport->fc_sparam.portName,
1131                        sizeof (struct lpfc_name));
1132                 size = GID_REQUEST_SZ - 4 + sizeof (struct lpfc_name);
1133                 break;
1134         }
1135
1136         bpl = (struct ulp_bde64 *) bmp->virt;
1137         bpl->addrHigh = le32_to_cpu( putPaddrHigh(mp->phys) );
1138         bpl->addrLow = le32_to_cpu( putPaddrLow(mp->phys) );
1139         bpl->tus.f.bdeFlags = 0;
1140         bpl->tus.f.bdeSize = size;
1141         bpl->tus.w = le32_to_cpu(bpl->tus.w);
1142
1143         cmpl = lpfc_cmpl_ct_cmd_fdmi;
1144
1145         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, FC_MAX_NS_RSP))
1146                 return 0;
1147
1148         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1149 fdmi_cmd_free_bmp:
1150         kfree(bmp);
1151 fdmi_cmd_free_mpvirt:
1152         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1153 fdmi_cmd_free_mp:
1154         kfree(mp);
1155 fdmi_cmd_exit:
1156         /* Issue FDMI request failed */
1157         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1158                         "%d:0244 Issue FDMI request failed Data: x%x\n",
1159                         phba->brd_no, cmdcode);
1160         return 1;
1161 }
1162
1163 void
1164 lpfc_fdmi_tmo(unsigned long ptr)
1165 {
1166         struct lpfc_vport *vport = (struct lpfc_vport *)ptr;
1167         struct lpfc_hba   *phba = vport->phba;
1168         unsigned long iflag;
1169
1170         spin_lock_irqsave(&vport->work_port_lock, iflag);
1171         if (!(vport->work_port_events & WORKER_FDMI_TMO)) {
1172                 vport->work_port_events |= WORKER_FDMI_TMO;
1173                 if (phba->work_wait)
1174                         wake_up(phba->work_wait);
1175         }
1176         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
1177 }
1178
1179 void
1180 lpfc_fdmi_timeout_handler(struct lpfc_vport *vport)
1181 {
1182         struct lpfc_nodelist *ndlp;
1183
1184         ndlp = lpfc_findnode_did(vport, FDMI_DID);
1185         if (ndlp) {
1186                 if (init_utsname()->nodename[0] != '\0')
1187                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
1188                 else
1189                         mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
1190         }
1191         return;
1192 }
1193
1194 void
1195 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
1196 {
1197         struct lpfc_sli *psli = &phba->sli;
1198         lpfc_vpd_t *vp = &phba->vpd;
1199         uint32_t b1, b2, b3, b4, i, rev;
1200         char c;
1201         uint32_t *ptr, str[4];
1202         uint8_t *fwname;
1203
1204         if (vp->rev.rBit) {
1205                 if (psli->sli_flag & LPFC_SLI2_ACTIVE)
1206                         rev = vp->rev.sli2FwRev;
1207                 else
1208                         rev = vp->rev.sli1FwRev;
1209
1210                 b1 = (rev & 0x0000f000) >> 12;
1211                 b2 = (rev & 0x00000f00) >> 8;
1212                 b3 = (rev & 0x000000c0) >> 6;
1213                 b4 = (rev & 0x00000030) >> 4;
1214
1215                 switch (b4) {
1216                 case 0:
1217                         c = 'N';
1218                         break;
1219                 case 1:
1220                         c = 'A';
1221                         break;
1222                 case 2:
1223                         c = 'B';
1224                         break;
1225                 default:
1226                         c = 0;
1227                         break;
1228                 }
1229                 b4 = (rev & 0x0000000f);
1230
1231                 if (psli->sli_flag & LPFC_SLI2_ACTIVE)
1232                         fwname = vp->rev.sli2FwName;
1233                 else
1234                         fwname = vp->rev.sli1FwName;
1235
1236                 for (i = 0; i < 16; i++)
1237                         if (fwname[i] == 0x20)
1238                                 fwname[i] = 0;
1239
1240                 ptr = (uint32_t*)fwname;
1241
1242                 for (i = 0; i < 3; i++)
1243                         str[i] = be32_to_cpu(*ptr++);
1244
1245                 if (c == 0) {
1246                         if (flag)
1247                                 sprintf(fwrevision, "%d.%d%d (%s)",
1248                                         b1, b2, b3, (char *)str);
1249                         else
1250                                 sprintf(fwrevision, "%d.%d%d", b1,
1251                                         b2, b3);
1252                 } else {
1253                         if (flag)
1254                                 sprintf(fwrevision, "%d.%d%d%c%d (%s)",
1255                                         b1, b2, b3, c,
1256                                         b4, (char *)str);
1257                         else
1258                                 sprintf(fwrevision, "%d.%d%d%c%d",
1259                                         b1, b2, b3, c, b4);
1260                 }
1261         } else {
1262                 rev = vp->rev.smFwRev;
1263
1264                 b1 = (rev & 0xff000000) >> 24;
1265                 b2 = (rev & 0x00f00000) >> 20;
1266                 b3 = (rev & 0x000f0000) >> 16;
1267                 c  = (rev & 0x0000ff00) >> 8;
1268                 b4 = (rev & 0x000000ff);
1269
1270                 if (flag)
1271                         sprintf(fwrevision, "%d.%d%d%c%d ", b1,
1272                                 b2, b3, c, b4);
1273                 else
1274                         sprintf(fwrevision, "%d.%d%d%c%d ", b1,
1275                                 b2, b3, c, b4);
1276         }
1277         return;
1278 }