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