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