ad7f33a31567cb8687c98ed452792b6ce9ba63f9
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_ct.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2007 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  *                                                                 *
8  * This program is free software; you can redistribute it and/or   *
9  * modify it under the terms of version 2 of the GNU General       *
10  * Public License as published by the Free Software Foundation.    *
11  * This program is distributed in the hope that it will be useful. *
12  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
13  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
14  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
15  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
17  * more details, a copy of which can be found in the file COPYING  *
18  * included with this package.                                     *
19  *******************************************************************/
20
21 /*
22  * Fibre Channel SCSI LAN Device Driver CT support
23  */
24
25 #include <linux/blkdev.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <linux/utsname.h>
29
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport_fc.h>
34
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_version.h"
43 #include "lpfc_vport.h"
44 #include "lpfc_debugfs.h"
45
46 #define HBA_PORTSPEED_UNKNOWN               0   /* Unknown - transceiver
47                                                  * incapable of reporting */
48 #define HBA_PORTSPEED_1GBIT                 1   /* 1 GBit/sec */
49 #define HBA_PORTSPEED_2GBIT                 2   /* 2 GBit/sec */
50 #define HBA_PORTSPEED_4GBIT                 8   /* 4 GBit/sec */
51 #define HBA_PORTSPEED_8GBIT                16   /* 8 GBit/sec */
52 #define HBA_PORTSPEED_10GBIT                4   /* 10 GBit/sec */
53 #define HBA_PORTSPEED_NOT_NEGOTIATED        5   /* Speed not established */
54
55 #define FOURBYTES       4
56
57
58 static char *lpfc_release_version = LPFC_DRIVER_VERSION;
59
60 /*
61  * lpfc_ct_unsol_event
62  */
63 static void
64 lpfc_ct_unsol_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
65                      struct lpfc_dmabuf *mp, uint32_t size)
66 {
67         if (!mp) {
68                 printk(KERN_ERR "%s (%d): Unsolited CT, no buffer, "
69                        "piocbq = %p, status = x%x, mp = %p, size = %d\n",
70                        __FUNCTION__, __LINE__,
71                        piocbq, piocbq->iocb.ulpStatus, mp, size);
72         }
73
74         printk(KERN_ERR "%s (%d): Ignoring unsolicted CT piocbq = %p, "
75                "buffer = %p, size = %d, status = x%x\n",
76                __FUNCTION__, __LINE__,
77                piocbq, mp, size,
78                piocbq->iocb.ulpStatus);
79
80 }
81
82 static void
83 lpfc_ct_ignore_hbq_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
84                           struct lpfc_dmabuf *mp, uint32_t size)
85 {
86         if (!mp) {
87                 printk(KERN_ERR "%s (%d): Unsolited CT, no "
88                        "HBQ buffer, piocbq = %p, status = x%x\n",
89                        __FUNCTION__, __LINE__,
90                        piocbq, piocbq->iocb.ulpStatus);
91         } else {
92                 lpfc_ct_unsol_buffer(phba, piocbq, mp, size);
93                 printk(KERN_ERR "%s (%d): Ignoring unsolicted CT "
94                        "piocbq = %p, buffer = %p, size = %d, "
95                        "status = x%x\n",
96                        __FUNCTION__, __LINE__,
97                        piocbq, mp, size, piocbq->iocb.ulpStatus);
98         }
99 }
100
101 void
102 lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
103                     struct lpfc_iocbq *piocbq)
104 {
105
106         struct lpfc_dmabuf *mp = NULL;
107         IOCB_t *icmd = &piocbq->iocb;
108         int i;
109         struct lpfc_iocbq *iocbq;
110         dma_addr_t paddr;
111         uint32_t size;
112         struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
113         struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
114
115         piocbq->context2 = NULL;
116         piocbq->context3 = NULL;
117
118         if (unlikely(icmd->ulpStatus == IOSTAT_NEED_BUFFER)) {
119                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
120         } else if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
121                 ((icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING)) {
122                 /* Not enough posted buffers; Try posting more buffers */
123                 phba->fc_stat.NoRcvBuf++;
124                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
125                         lpfc_post_buffer(phba, pring, 0, 1);
126                 return;
127         }
128
129         /* If there are no BDEs associated with this IOCB,
130          * there is nothing to do.
131          */
132         if (icmd->ulpBdeCount == 0)
133                 return;
134
135         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
136                 list_for_each_entry(iocbq, &piocbq->list, list) {
137                         icmd = &iocbq->iocb;
138                         if (icmd->ulpBdeCount == 0) {
139                                 printk(KERN_ERR "%s (%d): Unsolited CT, no "
140                                        "BDE, iocbq = %p, status = x%x\n",
141                                        __FUNCTION__, __LINE__,
142                                        iocbq, iocbq->iocb.ulpStatus);
143                                 continue;
144                         }
145
146                         size  = icmd->un.cont64[0].tus.f.bdeSize;
147                         lpfc_ct_ignore_hbq_buffer(phba, piocbq, bdeBuf1, size);
148                         lpfc_in_buf_free(phba, bdeBuf1);
149                         if (icmd->ulpBdeCount == 2) {
150                                 lpfc_ct_ignore_hbq_buffer(phba, piocbq, bdeBuf2,
151                                                           size);
152                                 lpfc_in_buf_free(phba, bdeBuf2);
153                         }
154                 }
155         } else {
156                 struct lpfc_iocbq  *next;
157
158                 list_for_each_entry_safe(iocbq, next, &piocbq->list, list) {
159                         icmd = &iocbq->iocb;
160                         if (icmd->ulpBdeCount == 0) {
161                                 printk(KERN_ERR "%s (%d): Unsolited CT, no "
162                                        "BDE, iocbq = %p, status = x%x\n",
163                                        __FUNCTION__, __LINE__,
164                                        iocbq, iocbq->iocb.ulpStatus);
165                                 continue;
166                         }
167
168                         for (i = 0; i < icmd->ulpBdeCount; i++) {
169                                 paddr = getPaddr(icmd->un.cont64[i].addrHigh,
170                                                  icmd->un.cont64[i].addrLow);
171                                 mp = lpfc_sli_ringpostbuf_get(phba, pring,
172                                                               paddr);
173                                 size = icmd->un.cont64[i].tus.f.bdeSize;
174                                 lpfc_ct_unsol_buffer(phba, piocbq, mp, size);
175                                 lpfc_in_buf_free(phba, mp);
176                         }
177                         list_del(&iocbq->list);
178                         lpfc_sli_release_iocbq(phba, iocbq);
179                 }
180         }
181 }
182
183 static void
184 lpfc_free_ct_rsp(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
185 {
186         struct lpfc_dmabuf *mlast, *next_mlast;
187
188         list_for_each_entry_safe(mlast, next_mlast, &mlist->list, list) {
189                 lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
190                 list_del(&mlast->list);
191                 kfree(mlast);
192         }
193         lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
194         kfree(mlist);
195         return;
196 }
197
198 static struct lpfc_dmabuf *
199 lpfc_alloc_ct_rsp(struct lpfc_hba *phba, int cmdcode, struct ulp_bde64 *bpl,
200                   uint32_t size, int *entries)
201 {
202         struct lpfc_dmabuf *mlist = NULL;
203         struct lpfc_dmabuf *mp;
204         int cnt, i = 0;
205
206         /* We get chucks of FCELSSIZE */
207         cnt = size > FCELSSIZE ? FCELSSIZE: size;
208
209         while (size) {
210                 /* Allocate buffer for rsp payload */
211                 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
212                 if (!mp) {
213                         if (mlist)
214                                 lpfc_free_ct_rsp(phba, mlist);
215                         return NULL;
216                 }
217
218                 INIT_LIST_HEAD(&mp->list);
219
220                 if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT) ||
221                     cmdcode == be16_to_cpu(SLI_CTNS_GFF_ID))
222                         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
223                 else
224                         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
225
226                 if (!mp->virt) {
227                         kfree(mp);
228                         if (mlist)
229                                 lpfc_free_ct_rsp(phba, mlist);
230                         return NULL;
231                 }
232
233                 /* Queue it to a linked list */
234                 if (!mlist)
235                         mlist = mp;
236                 else
237                         list_add_tail(&mp->list, &mlist->list);
238
239                 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
240                 /* build buffer ptr list for IOCB */
241                 bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
242                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
243                 bpl->tus.f.bdeSize = (uint16_t) cnt;
244                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
245                 bpl++;
246
247                 i++;
248                 size -= cnt;
249         }
250
251         *entries = i;
252         return mlist;
253 }
254
255 int
256 lpfc_ct_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocb)
257 {
258         struct lpfc_dmabuf *buf_ptr;
259
260         if (ctiocb->context1) {
261                 buf_ptr = (struct lpfc_dmabuf *) ctiocb->context1;
262                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
263                 kfree(buf_ptr);
264                 ctiocb->context1 = NULL;
265         }
266         if (ctiocb->context2) {
267                 lpfc_free_ct_rsp(phba, (struct lpfc_dmabuf *) ctiocb->context2);
268                 ctiocb->context2 = NULL;
269         }
270
271         if (ctiocb->context3) {
272                 buf_ptr = (struct lpfc_dmabuf *) ctiocb->context3;
273                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
274                 kfree(buf_ptr);
275                 ctiocb->context1 = NULL;
276         }
277         lpfc_sli_release_iocbq(phba, ctiocb);
278         return 0;
279 }
280
281 static int
282 lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
283              struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
284              void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
285                      struct lpfc_iocbq *),
286              struct lpfc_nodelist *ndlp, uint32_t usr_flg, uint32_t num_entry,
287              uint32_t tmo, uint8_t retry)
288 {
289         struct lpfc_hba  *phba = vport->phba;
290         struct lpfc_sli  *psli = &phba->sli;
291         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
292         IOCB_t *icmd;
293         struct lpfc_iocbq *geniocb;
294         int rc;
295
296         /* Allocate buffer for  command iocb */
297         geniocb = lpfc_sli_get_iocbq(phba);
298
299         if (geniocb == NULL)
300                 return 1;
301
302         icmd = &geniocb->iocb;
303         icmd->un.genreq64.bdl.ulpIoTag32 = 0;
304         icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
305         icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
306         icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BDL;
307         icmd->un.genreq64.bdl.bdeSize = (num_entry * sizeof (struct ulp_bde64));
308
309         if (usr_flg)
310                 geniocb->context3 = NULL;
311         else
312                 geniocb->context3 = (uint8_t *) bmp;
313
314         /* Save for completion so we can release these resources */
315         geniocb->context1 = (uint8_t *) inp;
316         geniocb->context2 = (uint8_t *) outp;
317
318         /* Fill in payload, bp points to frame payload */
319         icmd->ulpCommand = CMD_GEN_REQUEST64_CR;
320
321         /* Fill in rest of iocb */
322         icmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
323         icmd->un.genreq64.w5.hcsw.Dfctl = 0;
324         icmd->un.genreq64.w5.hcsw.Rctl = FC_UNSOL_CTL;
325         icmd->un.genreq64.w5.hcsw.Type = FC_COMMON_TRANSPORT_ULP;
326
327         if (!tmo) {
328                  /* FC spec states we need 3 * ratov for CT requests */
329                 tmo = (3 * phba->fc_ratov);
330         }
331         icmd->ulpTimeout = tmo;
332         icmd->ulpBdeCount = 1;
333         icmd->ulpLe = 1;
334         icmd->ulpClass = CLASS3;
335         icmd->ulpContext = ndlp->nlp_rpi;
336
337         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
338                 /* For GEN_REQUEST64_CR, use the RPI */
339                 icmd->ulpCt_h = 0;
340                 icmd->ulpCt_l = 0;
341         }
342
343         /* Issue GEN REQ IOCB for NPORT <did> */
344         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
345                          "0119 Issue GEN REQ IOCB to NPORT x%x "
346                          "Data: x%x x%x\n",
347                          ndlp->nlp_DID, icmd->ulpIoTag,
348                          vport->port_state);
349         geniocb->iocb_cmpl = cmpl;
350         geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
351         geniocb->vport = vport;
352         geniocb->retry = retry;
353         rc = lpfc_sli_issue_iocb(phba, pring, geniocb, 0);
354
355         if (rc == IOCB_ERROR) {
356                 lpfc_sli_release_iocbq(phba, geniocb);
357                 return 1;
358         }
359
360         return 0;
361 }
362
363 static int
364 lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
365             struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
366             void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
367                           struct lpfc_iocbq *),
368             uint32_t rsp_size, uint8_t retry)
369 {
370         struct lpfc_hba  *phba = vport->phba;
371         struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
372         struct lpfc_dmabuf *outmp;
373         int cnt = 0, status;
374         int cmdcode = ((struct lpfc_sli_ct_request *) inmp->virt)->
375                 CommandResponse.bits.CmdRsp;
376
377         bpl++;                  /* Skip past ct request */
378
379         /* Put buffer(s) for ct rsp in bpl */
380         outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
381         if (!outmp)
382                 return -ENOMEM;
383
384         status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp, 0,
385                               cnt+1, 0, retry);
386         if (status) {
387                 lpfc_free_ct_rsp(phba, outmp);
388                 return -ENOMEM;
389         }
390         return 0;
391 }
392
393 struct lpfc_vport *
394 lpfc_find_vport_by_did(struct lpfc_hba *phba, uint32_t did) {
395         struct lpfc_vport *vport_curr;
396         unsigned long flags;
397
398         spin_lock_irqsave(&phba->hbalock, flags);
399         list_for_each_entry(vport_curr, &phba->port_list, listentry) {
400                 if ((vport_curr->fc_myDID) && (vport_curr->fc_myDID == did)) {
401                         spin_unlock_irqrestore(&phba->hbalock, flags);
402                         return vport_curr;
403                 }
404         }
405         spin_unlock_irqrestore(&phba->hbalock, flags);
406         return NULL;
407 }
408
409 static int
410 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint32_t Size)
411 {
412         struct lpfc_hba  *phba = vport->phba;
413         struct lpfc_sli_ct_request *Response =
414                 (struct lpfc_sli_ct_request *) mp->virt;
415         struct lpfc_nodelist *ndlp = NULL;
416         struct lpfc_dmabuf *mlast, *next_mp;
417         uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
418         uint32_t Did, CTentry;
419         int Cnt;
420         struct list_head head;
421
422         lpfc_set_disctmo(vport);
423         vport->num_disc_nodes = 0;
424
425
426         list_add_tail(&head, &mp->list);
427         list_for_each_entry_safe(mp, next_mp, &head, list) {
428                 mlast = mp;
429
430                 Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;
431
432                 Size -= Cnt;
433
434                 if (!ctptr) {
435                         ctptr = (uint32_t *) mlast->virt;
436                 } else
437                         Cnt -= 16;      /* subtract length of CT header */
438
439                 /* Loop through entire NameServer list of DIDs */
440                 while (Cnt >= sizeof (uint32_t)) {
441                         /* Get next DID from NameServer List */
442                         CTentry = *ctptr++;
443                         Did = ((be32_to_cpu(CTentry)) & Mask_DID);
444
445                         ndlp = NULL;
446
447                         /*
448                          * Check for rscn processing or not
449                          * To conserve rpi's, filter out addresses for other
450                          * vports on the same physical HBAs.
451                          */
452                         if ((Did != vport->fc_myDID) &&
453                             ((lpfc_find_vport_by_did(phba, Did) == NULL) ||
454                              vport->cfg_peer_port_login)) {
455                                 if ((vport->port_type != LPFC_NPIV_PORT) ||
456                                     (vport->fc_flag & FC_RFF_NOT_SUPPORTED) ||
457                                     (!vport->cfg_restrict_login)) {
458                                         ndlp = lpfc_setup_disc_node(vport, Did);
459                                         if (ndlp) {
460                                                 lpfc_debugfs_disc_trc(vport,
461                                                 LPFC_DISC_TRC_CT,
462                                                 "Parse GID_FTrsp: "
463                                                 "did:x%x flg:x%x x%x",
464                                                 Did, ndlp->nlp_flag,
465                                                 vport->fc_flag);
466
467                                                 lpfc_printf_vlog(vport,
468                                                         KERN_INFO,
469                                                         LOG_DISCOVERY,
470                                                         "0238 Process "
471                                                         "x%x NameServer Rsp"
472                                                         "Data: x%x x%x x%x\n",
473                                                         Did, ndlp->nlp_flag,
474                                                         vport->fc_flag,
475                                                         vport->fc_rscn_id_cnt);
476                                         } else {
477                                                 lpfc_debugfs_disc_trc(vport,
478                                                 LPFC_DISC_TRC_CT,
479                                                 "Skip1 GID_FTrsp: "
480                                                 "did:x%x flg:x%x cnt:%d",
481                                                 Did, vport->fc_flag,
482                                                 vport->fc_rscn_id_cnt);
483
484                                                 lpfc_printf_vlog(vport,
485                                                         KERN_INFO,
486                                                         LOG_DISCOVERY,
487                                                         "0239 Skip x%x "
488                                                         "NameServer Rsp Data: "
489                                                         "x%x x%x\n",
490                                                         Did, vport->fc_flag,
491                                                         vport->fc_rscn_id_cnt);
492                                         }
493
494                                 } else {
495                                         if (!(vport->fc_flag & FC_RSCN_MODE) ||
496                                         (lpfc_rscn_payload_check(vport, Did))) {
497                                                 lpfc_debugfs_disc_trc(vport,
498                                                 LPFC_DISC_TRC_CT,
499                                                 "Query GID_FTrsp: "
500                                                 "did:x%x flg:x%x cnt:%d",
501                                                 Did, vport->fc_flag,
502                                                 vport->fc_rscn_id_cnt);
503
504                                                 if (lpfc_ns_cmd(vport,
505                                                         SLI_CTNS_GFF_ID,
506                                                         0, Did) == 0)
507                                                         vport->num_disc_nodes++;
508                                         }
509                                         else {
510                                                 lpfc_debugfs_disc_trc(vport,
511                                                 LPFC_DISC_TRC_CT,
512                                                 "Skip2 GID_FTrsp: "
513                                                 "did:x%x flg:x%x cnt:%d",
514                                                 Did, vport->fc_flag,
515                                                 vport->fc_rscn_id_cnt);
516
517                                                 lpfc_printf_vlog(vport,
518                                                         KERN_INFO,
519                                                         LOG_DISCOVERY,
520                                                         "0245 Skip x%x "
521                                                         "NameServer Rsp Data: "
522                                                         "x%x x%x\n",
523                                                         Did, vport->fc_flag,
524                                                         vport->fc_rscn_id_cnt);
525                                         }
526                                 }
527                         }
528                         if (CTentry & (be32_to_cpu(SLI_CT_LAST_ENTRY)))
529                                 goto nsout1;
530                         Cnt -= sizeof (uint32_t);
531                 }
532                 ctptr = NULL;
533
534         }
535
536 nsout1:
537         list_del(&head);
538         return 0;
539 }
540
541 static void
542 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
543                         struct lpfc_iocbq *rspiocb)
544 {
545         struct lpfc_vport *vport = cmdiocb->vport;
546         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
547         IOCB_t *irsp;
548         struct lpfc_dmabuf *bmp;
549         struct lpfc_dmabuf *outp;
550         struct lpfc_sli_ct_request *CTrsp;
551         int rc;
552
553         /* we pass cmdiocb to state machine which needs rspiocb as well */
554         cmdiocb->context_un.rsp_iocb = rspiocb;
555
556         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
557         bmp = (struct lpfc_dmabuf *) cmdiocb->context3;
558         irsp = &rspiocb->iocb;
559
560         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
561                  "GID_FT cmpl:     status:x%x/x%x rtry:%d",
562                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_ns_retry);
563
564         /* Don't bother processing response if vport is being torn down. */
565         if (vport->load_flag & FC_UNLOADING)
566                 goto out;
567
568
569         if (lpfc_els_chk_latt(vport) || lpfc_error_lost_link(irsp)) {
570                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
571                                  "0216 Link event during NS query\n");
572                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
573                 goto out;
574         }
575
576         if (irsp->ulpStatus) {
577                 /* Check for retry */
578                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
579                         if ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
580                                 (irsp->un.ulpWord[4] != IOERR_NO_RESOURCES))
581                                 vport->fc_ns_retry++;
582                         /* CT command is being retried */
583                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
584                                          vport->fc_ns_retry, 0);
585                         if (rc == 0)
586                                 goto out;
587                 }
588                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
589                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
590                                  "0257 GID_FT Query error: 0x%x 0x%x\n",
591                                  irsp->ulpStatus, vport->fc_ns_retry);
592         } else {
593                 /* Good status, continue checking */
594                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
595                 if (CTrsp->CommandResponse.bits.CmdRsp ==
596                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
597                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
598                                          "0208 NameServer Rsp Data: x%x\n",
599                                          vport->fc_flag);
600                         lpfc_ns_rsp(vport, outp,
601                                     (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
602                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
603                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
604                         /* NameServer Rsp Error */
605                         if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
606                             && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
607                                 lpfc_printf_vlog(vport, KERN_INFO,
608                                         LOG_DISCOVERY,
609                                         "0269 No NameServer Entries "
610                                         "Data: x%x x%x x%x x%x\n",
611                                         CTrsp->CommandResponse.bits.CmdRsp,
612                                         (uint32_t) CTrsp->ReasonCode,
613                                         (uint32_t) CTrsp->Explanation,
614                                         vport->fc_flag);
615
616                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
617                                 "GID_FT no entry  cmd:x%x rsn:x%x exp:x%x",
618                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
619                                 (uint32_t) CTrsp->ReasonCode,
620                                 (uint32_t) CTrsp->Explanation);
621                         } else {
622                                 lpfc_printf_vlog(vport, KERN_INFO,
623                                         LOG_DISCOVERY,
624                                         "0240 NameServer Rsp Error "
625                                         "Data: x%x x%x x%x x%x\n",
626                                         CTrsp->CommandResponse.bits.CmdRsp,
627                                         (uint32_t) CTrsp->ReasonCode,
628                                         (uint32_t) CTrsp->Explanation,
629                                         vport->fc_flag);
630
631                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
632                                 "GID_FT rsp err1  cmd:x%x rsn:x%x exp:x%x",
633                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
634                                 (uint32_t) CTrsp->ReasonCode,
635                                 (uint32_t) CTrsp->Explanation);
636                         }
637
638
639                 } else {
640                         /* NameServer Rsp Error */
641                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
642                                         "0241 NameServer Rsp Error "
643                                         "Data: x%x x%x x%x x%x\n",
644                                         CTrsp->CommandResponse.bits.CmdRsp,
645                                         (uint32_t) CTrsp->ReasonCode,
646                                         (uint32_t) CTrsp->Explanation,
647                                         vport->fc_flag);
648
649                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
650                                 "GID_FT rsp err2  cmd:x%x rsn:x%x exp:x%x",
651                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
652                                 (uint32_t) CTrsp->ReasonCode,
653                                 (uint32_t) CTrsp->Explanation);
654                 }
655         }
656         /* Link up / RSCN discovery */
657         if (vport->num_disc_nodes == 0) {
658                 /*
659                  * The driver has cycled through all Nports in the RSCN payload.
660                  * Complete the handling by cleaning up and marking the
661                  * current driver state.
662                  */
663                 if (vport->port_state >= LPFC_DISC_AUTH) {
664                         if (vport->fc_flag & FC_RSCN_MODE) {
665                                 lpfc_els_flush_rscn(vport);
666                                 spin_lock_irq(shost->host_lock);
667                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
668                                 spin_unlock_irq(shost->host_lock);
669                         }
670                         else
671                                 lpfc_els_flush_rscn(vport);
672                 }
673
674                 lpfc_disc_start(vport);
675         }
676 out:
677         lpfc_ct_free_iocb(phba, cmdiocb);
678         return;
679 }
680
681 void
682 lpfc_cmpl_ct_cmd_gff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
683                         struct lpfc_iocbq *rspiocb)
684 {
685         struct lpfc_vport *vport = cmdiocb->vport;
686         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
687         IOCB_t *irsp = &rspiocb->iocb;
688         struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *) cmdiocb->context1;
689         struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *) cmdiocb->context2;
690         struct lpfc_sli_ct_request *CTrsp;
691         int did;
692         uint8_t fbits;
693         struct lpfc_nodelist *ndlp;
694
695         did = ((struct lpfc_sli_ct_request *) inp->virt)->un.gff.PortId;
696         did = be32_to_cpu(did);
697
698         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
699                 "GFF_ID cmpl:     status:x%x/x%x did:x%x",
700                 irsp->ulpStatus, irsp->un.ulpWord[4], did);
701
702         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
703                 /* Good status, continue checking */
704                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
705                 fbits = CTrsp->un.gff_acc.fbits[FCP_TYPE_FEATURE_OFFSET];
706
707                 if (CTrsp->CommandResponse.bits.CmdRsp ==
708                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
709                         if ((fbits & FC4_FEATURE_INIT) &&
710                             !(fbits & FC4_FEATURE_TARGET)) {
711                                 lpfc_printf_vlog(vport, KERN_INFO,
712                                                  LOG_DISCOVERY,
713                                                  "0270 Skip x%x GFF "
714                                                  "NameServer Rsp Data: (init) "
715                                                  "x%x x%x\n", did, fbits,
716                                                  vport->fc_rscn_id_cnt);
717                                 goto out;
718                         }
719                 }
720         }
721         else {
722                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
723                                  "0267 NameServer GFF Rsp "
724                                  "x%x Error (%d %d) Data: x%x x%x\n",
725                                  did, irsp->ulpStatus, irsp->un.ulpWord[4],
726                                  vport->fc_flag, vport->fc_rscn_id_cnt)
727         }
728
729         /* This is a target port, unregistered port, or the GFF_ID failed */
730         ndlp = lpfc_setup_disc_node(vport, did);
731         if (ndlp) {
732                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
733                                  "0242 Process x%x GFF "
734                                  "NameServer Rsp Data: x%x x%x x%x\n",
735                                  did, ndlp->nlp_flag, vport->fc_flag,
736                                  vport->fc_rscn_id_cnt);
737         } else {
738                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
739                                  "0243 Skip x%x GFF "
740                                  "NameServer Rsp Data: x%x x%x\n", did,
741                                  vport->fc_flag, vport->fc_rscn_id_cnt);
742         }
743 out:
744         /* Link up / RSCN discovery */
745         if (vport->num_disc_nodes)
746                 vport->num_disc_nodes--;
747         if (vport->num_disc_nodes == 0) {
748                 /*
749                  * The driver has cycled through all Nports in the RSCN payload.
750                  * Complete the handling by cleaning up and marking the
751                  * current driver state.
752                  */
753                 if (vport->port_state >= LPFC_DISC_AUTH) {
754                         if (vport->fc_flag & FC_RSCN_MODE) {
755                                 lpfc_els_flush_rscn(vport);
756                                 spin_lock_irq(shost->host_lock);
757                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
758                                 spin_unlock_irq(shost->host_lock);
759                         }
760                         else
761                                 lpfc_els_flush_rscn(vport);
762                 }
763                 lpfc_disc_start(vport);
764         }
765         lpfc_ct_free_iocb(phba, cmdiocb);
766         return;
767 }
768
769
770 static void
771 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
772                         struct lpfc_iocbq *rspiocb)
773 {
774         struct lpfc_vport *vport = cmdiocb->vport;
775         struct lpfc_dmabuf *inp;
776         struct lpfc_dmabuf *outp;
777         IOCB_t *irsp;
778         struct lpfc_sli_ct_request *CTrsp;
779         int cmdcode, rc;
780         uint8_t retry;
781         uint32_t latt;
782
783         /* we pass cmdiocb to state machine which needs rspiocb as well */
784         cmdiocb->context_un.rsp_iocb = rspiocb;
785
786         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
787         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
788         irsp = &rspiocb->iocb;
789
790         cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
791                                         CommandResponse.bits.CmdRsp);
792         CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
793
794         latt = lpfc_els_chk_latt(vport);
795
796         /* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
797         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
798                          "0209 RFT request completes, latt %d, "
799                          "ulpStatus x%x CmdRsp x%x, Context x%x, Tag x%x\n",
800                          latt, irsp->ulpStatus,
801                          CTrsp->CommandResponse.bits.CmdRsp,
802                          cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
803
804         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
805                 "CT cmd cmpl:     status:x%x/x%x cmd:x%x",
806                 irsp->ulpStatus, irsp->un.ulpWord[4], cmdcode);
807
808         if (irsp->ulpStatus) {
809                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
810                                  "0268 NS cmd %x Error (%d %d)\n",
811                                  cmdcode, irsp->ulpStatus, irsp->un.ulpWord[4]);
812
813                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
814                         ((irsp->un.ulpWord[4] == IOERR_SLI_DOWN) ||
815                          (irsp->un.ulpWord[4] == IOERR_SLI_ABORTED)))
816                         goto out;
817
818                 retry = cmdiocb->retry;
819                 if (retry >= LPFC_MAX_NS_RETRY)
820                         goto out;
821
822                 retry++;
823                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
824                                  "0216 Retrying NS cmd %x\n", cmdcode);
825                 rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
826                 if (rc == 0)
827                         goto out;
828         }
829
830 out:
831         lpfc_ct_free_iocb(phba, cmdiocb);
832         return;
833 }
834
835 static void
836 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
837                         struct lpfc_iocbq *rspiocb)
838 {
839         lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
840         return;
841 }
842
843 static void
844 lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
845                          struct lpfc_iocbq *rspiocb)
846 {
847         lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
848         return;
849 }
850
851 static void
852 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
853                          struct lpfc_iocbq *rspiocb)
854 {
855         lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
856         return;
857 }
858
859 static void
860 lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
861                         struct lpfc_iocbq *rspiocb)
862 {
863         IOCB_t *irsp = &rspiocb->iocb;
864         struct lpfc_vport *vport = cmdiocb->vport;
865
866         if (irsp->ulpStatus != IOSTAT_SUCCESS)
867             vport->fc_flag |= FC_RFF_NOT_SUPPORTED;
868
869         lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
870         return;
871 }
872
873 int
874 lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
875         size_t size)
876 {
877         int n;
878         uint8_t *wwn = vport->phba->wwpn;
879
880         n = snprintf(symbol, size,
881                      "Emulex PPN-%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
882                      wwn[0], wwn[1], wwn[2], wwn[3],
883                      wwn[4], wwn[5], wwn[6], wwn[7]);
884
885         if (vport->port_type == LPFC_PHYSICAL_PORT)
886                 return n;
887
888         if (n < size)
889                 n += snprintf(symbol + n, size - n, " VPort-%d", vport->vpi);
890
891         if (n < size && vport->vname)
892                 n += snprintf(symbol + n, size - n, " VName-%s", vport->vname);
893         return n;
894 }
895
896 int
897 lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
898         size_t size)
899 {
900         char fwrev[16];
901         int n;
902
903         lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
904
905         n = snprintf(symbol, size, "Emulex %s FV%s DV%s",
906                 vport->phba->ModelName, fwrev, lpfc_release_version);
907         return n;
908 }
909
910 /*
911  * lpfc_ns_cmd
912  * Description:
913  *    Issue Cmd to NameServer
914  *       SLI_CTNS_GID_FT
915  *       LI_CTNS_RFT_ID
916  */
917 int
918 lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
919             uint8_t retry, uint32_t context)
920 {
921         struct lpfc_nodelist * ndlp;
922         struct lpfc_hba *phba = vport->phba;
923         struct lpfc_dmabuf *mp, *bmp;
924         struct lpfc_sli_ct_request *CtReq;
925         struct ulp_bde64 *bpl;
926         void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
927                       struct lpfc_iocbq *) = NULL;
928         uint32_t rsp_size = 1024;
929         size_t   size;
930         int rc = 0;
931
932         ndlp = lpfc_findnode_did(vport, NameServer_DID);
933         if (ndlp == NULL || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) {
934                 rc=1;
935                 goto ns_cmd_exit;
936         }
937
938         /* fill in BDEs for command */
939         /* Allocate buffer for command payload */
940         mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
941         if (!mp) {
942                 rc=2;
943                 goto ns_cmd_exit;
944         }
945
946         INIT_LIST_HEAD(&mp->list);
947         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
948         if (!mp->virt) {
949                 rc=3;
950                 goto ns_cmd_free_mp;
951         }
952
953         /* Allocate buffer for Buffer ptr list */
954         bmp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
955         if (!bmp) {
956                 rc=4;
957                 goto ns_cmd_free_mpvirt;
958         }
959
960         INIT_LIST_HEAD(&bmp->list);
961         bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
962         if (!bmp->virt) {
963                 rc=5;
964                 goto ns_cmd_free_bmp;
965         }
966
967         /* NameServer Req */
968         lpfc_printf_vlog(vport, KERN_INFO ,LOG_DISCOVERY,
969                          "0236 NameServer Req Data: x%x x%x x%x\n",
970                          cmdcode, vport->fc_flag, vport->fc_rscn_id_cnt);
971
972         bpl = (struct ulp_bde64 *) bmp->virt;
973         memset(bpl, 0, sizeof(struct ulp_bde64));
974         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
975         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
976         bpl->tus.f.bdeFlags = 0;
977         if (cmdcode == SLI_CTNS_GID_FT)
978                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
979         else if (cmdcode == SLI_CTNS_GFF_ID)
980                 bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
981         else if (cmdcode == SLI_CTNS_RFT_ID)
982                 bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
983         else if (cmdcode == SLI_CTNS_RNN_ID)
984                 bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
985         else if (cmdcode == SLI_CTNS_RSPN_ID)
986                 bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
987         else if (cmdcode == SLI_CTNS_RSNN_NN)
988                 bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
989         else if (cmdcode == SLI_CTNS_RFF_ID)
990                 bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
991         else
992                 bpl->tus.f.bdeSize = 0;
993         bpl->tus.w = le32_to_cpu(bpl->tus.w);
994
995         CtReq = (struct lpfc_sli_ct_request *) mp->virt;
996         memset(CtReq, 0, sizeof (struct lpfc_sli_ct_request));
997         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
998         CtReq->RevisionId.bits.InId = 0;
999         CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
1000         CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
1001         CtReq->CommandResponse.bits.Size = 0;
1002         switch (cmdcode) {
1003         case SLI_CTNS_GID_FT:
1004                 CtReq->CommandResponse.bits.CmdRsp =
1005                     be16_to_cpu(SLI_CTNS_GID_FT);
1006                 CtReq->un.gid.Fc4Type = SLI_CTPT_FCP;
1007                 if (vport->port_state < LPFC_NS_QRY)
1008                         vport->port_state = LPFC_NS_QRY;
1009                 lpfc_set_disctmo(vport);
1010                 cmpl = lpfc_cmpl_ct_cmd_gid_ft;
1011                 rsp_size = FC_MAX_NS_RSP;
1012                 break;
1013
1014         case SLI_CTNS_GFF_ID:
1015                 CtReq->CommandResponse.bits.CmdRsp =
1016                         be16_to_cpu(SLI_CTNS_GFF_ID);
1017                 CtReq->un.gff.PortId = be32_to_cpu(context);
1018                 cmpl = lpfc_cmpl_ct_cmd_gff_id;
1019                 break;
1020
1021         case SLI_CTNS_RFT_ID:
1022                 CtReq->CommandResponse.bits.CmdRsp =
1023                     be16_to_cpu(SLI_CTNS_RFT_ID);
1024                 CtReq->un.rft.PortId = be32_to_cpu(vport->fc_myDID);
1025                 CtReq->un.rft.fcpReg = 1;
1026                 cmpl = lpfc_cmpl_ct_cmd_rft_id;
1027                 break;
1028
1029         case SLI_CTNS_RNN_ID:
1030                 CtReq->CommandResponse.bits.CmdRsp =
1031                     be16_to_cpu(SLI_CTNS_RNN_ID);
1032                 CtReq->un.rnn.PortId = be32_to_cpu(vport->fc_myDID);
1033                 memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
1034                        sizeof (struct lpfc_name));
1035                 cmpl = lpfc_cmpl_ct_cmd_rnn_id;
1036                 break;
1037
1038         case SLI_CTNS_RSPN_ID:
1039                 CtReq->CommandResponse.bits.CmdRsp =
1040                     be16_to_cpu(SLI_CTNS_RSPN_ID);
1041                 CtReq->un.rspn.PortId = be32_to_cpu(vport->fc_myDID);
1042                 size = sizeof(CtReq->un.rspn.symbname);
1043                 CtReq->un.rspn.len =
1044                         lpfc_vport_symbolic_port_name(vport,
1045                         CtReq->un.rspn.symbname, size);
1046                 cmpl = lpfc_cmpl_ct_cmd_rspn_id;
1047                 break;
1048         case SLI_CTNS_RSNN_NN:
1049                 CtReq->CommandResponse.bits.CmdRsp =
1050                     be16_to_cpu(SLI_CTNS_RSNN_NN);
1051                 memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
1052                        sizeof (struct lpfc_name));
1053                 size = sizeof(CtReq->un.rsnn.symbname);
1054                 CtReq->un.rsnn.len =
1055                         lpfc_vport_symbolic_node_name(vport,
1056                         CtReq->un.rsnn.symbname, size);
1057                 cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
1058                 break;
1059         case SLI_CTNS_RFF_ID:
1060                 vport->fc_flag &= ~FC_RFF_NOT_SUPPORTED;
1061                 CtReq->CommandResponse.bits.CmdRsp =
1062                     be16_to_cpu(SLI_CTNS_RFF_ID);
1063                 CtReq->un.rff.PortId = be32_to_cpu(vport->fc_myDID);;
1064                 CtReq->un.rff.fbits = FC4_FEATURE_INIT;
1065                 CtReq->un.rff.type_code = FC_FCP_DATA;
1066                 cmpl = lpfc_cmpl_ct_cmd_rff_id;
1067                 break;
1068         }
1069
1070         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry)) {
1071                 /* On success, The cmpl function will free the buffers */
1072                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1073                         "Issue CT cmd:    cmd:x%x did:x%x",
1074                         cmdcode, ndlp->nlp_DID, 0);
1075                 return 0;
1076         }
1077
1078         rc=6;
1079         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1080 ns_cmd_free_bmp:
1081         kfree(bmp);
1082 ns_cmd_free_mpvirt:
1083         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1084 ns_cmd_free_mp:
1085         kfree(mp);
1086 ns_cmd_exit:
1087         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1088                          "0266 Issue NameServer Req x%x err %d Data: x%x x%x\n",
1089                          cmdcode, rc, vport->fc_flag, vport->fc_rscn_id_cnt);
1090         return 1;
1091 }
1092
1093 static void
1094 lpfc_cmpl_ct_cmd_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1095                       struct lpfc_iocbq * rspiocb)
1096 {
1097         struct lpfc_dmabuf *inp = cmdiocb->context1;
1098         struct lpfc_dmabuf *outp = cmdiocb->context2;
1099         struct lpfc_sli_ct_request *CTrsp = outp->virt;
1100         struct lpfc_sli_ct_request *CTcmd = inp->virt;
1101         struct lpfc_nodelist *ndlp;
1102         uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
1103         uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
1104         struct lpfc_vport *vport = cmdiocb->vport;
1105         IOCB_t *irsp = &rspiocb->iocb;
1106         uint32_t latt;
1107
1108         latt = lpfc_els_chk_latt(vport);
1109
1110         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1111                 "FDMI cmpl:       status:x%x/x%x latt:%d",
1112                 irsp->ulpStatus, irsp->un.ulpWord[4], latt);
1113
1114         if (latt || irsp->ulpStatus) {
1115                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1116                                  "0229 FDMI cmd %04x failed, latt = %d "
1117                                  "ulpStatus: x%x, rid x%x\n",
1118                                  be16_to_cpu(fdmi_cmd), latt, irsp->ulpStatus,
1119                                  irsp->un.ulpWord[4]);
1120                 lpfc_ct_free_iocb(phba, cmdiocb);
1121                 return;
1122         }
1123
1124         ndlp = lpfc_findnode_did(vport, FDMI_DID);
1125         if (fdmi_rsp == be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
1126                 /* FDMI rsp failed */
1127                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1128                                  "0220 FDMI rsp failed Data: x%x\n",
1129                                  be16_to_cpu(fdmi_cmd));
1130         }
1131
1132         switch (be16_to_cpu(fdmi_cmd)) {
1133         case SLI_MGMT_RHBA:
1134                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA);
1135                 break;
1136
1137         case SLI_MGMT_RPA:
1138                 break;
1139
1140         case SLI_MGMT_DHBA:
1141                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT);
1142                 break;
1143
1144         case SLI_MGMT_DPRT:
1145                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA);
1146                 break;
1147         }
1148         lpfc_ct_free_iocb(phba, cmdiocb);
1149         return;
1150 }
1151
1152 int
1153 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp, int cmdcode)
1154 {
1155         struct lpfc_hba *phba = vport->phba;
1156         struct lpfc_dmabuf *mp, *bmp;
1157         struct lpfc_sli_ct_request *CtReq;
1158         struct ulp_bde64 *bpl;
1159         uint32_t size;
1160         REG_HBA *rh;
1161         PORT_ENTRY *pe;
1162         REG_PORT_ATTRIBUTE *pab;
1163         ATTRIBUTE_BLOCK *ab;
1164         ATTRIBUTE_ENTRY *ae;
1165         void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
1166                       struct lpfc_iocbq *);
1167
1168
1169         /* fill in BDEs for command */
1170         /* Allocate buffer for command payload */
1171         mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1172         if (!mp)
1173                 goto fdmi_cmd_exit;
1174
1175         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
1176         if (!mp->virt)
1177                 goto fdmi_cmd_free_mp;
1178
1179         /* Allocate buffer for Buffer ptr list */
1180         bmp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1181         if (!bmp)
1182                 goto fdmi_cmd_free_mpvirt;
1183
1184         bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
1185         if (!bmp->virt)
1186                 goto fdmi_cmd_free_bmp;
1187
1188         INIT_LIST_HEAD(&mp->list);
1189         INIT_LIST_HEAD(&bmp->list);
1190
1191         /* FDMI request */
1192         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1193                          "0218 FDMI Request Data: x%x x%x x%x\n",
1194                          vport->fc_flag, vport->port_state, cmdcode);
1195         CtReq = (struct lpfc_sli_ct_request *) mp->virt;
1196
1197         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
1198         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
1199         CtReq->RevisionId.bits.InId = 0;
1200
1201         CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
1202         CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
1203         size = 0;
1204
1205         switch (cmdcode) {
1206         case SLI_MGMT_RHBA:
1207                 {
1208                         lpfc_vpd_t *vp = &phba->vpd;
1209                         uint32_t i, j, incr;
1210                         int len;
1211
1212                         CtReq->CommandResponse.bits.CmdRsp =
1213                             be16_to_cpu(SLI_MGMT_RHBA);
1214                         CtReq->CommandResponse.bits.Size = 0;
1215                         rh = (REG_HBA *) & CtReq->un.PortID;
1216                         memcpy(&rh->hi.PortName, &vport->fc_sparam.portName,
1217                                sizeof (struct lpfc_name));
1218                         /* One entry (port) per adapter */
1219                         rh->rpl.EntryCnt = be32_to_cpu(1);
1220                         memcpy(&rh->rpl.pe, &vport->fc_sparam.portName,
1221                                sizeof (struct lpfc_name));
1222
1223                         /* point to the HBA attribute block */
1224                         size = 2 * sizeof (struct lpfc_name) + FOURBYTES;
1225                         ab = (ATTRIBUTE_BLOCK *) ((uint8_t *) rh + size);
1226                         ab->EntryCnt = 0;
1227
1228                         /* Point to the beginning of the first HBA attribute
1229                            entry */
1230                         /* #1 HBA attribute entry */
1231                         size += FOURBYTES;
1232                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1233                         ae->ad.bits.AttrType = be16_to_cpu(NODE_NAME);
1234                         ae->ad.bits.AttrLen =  be16_to_cpu(FOURBYTES
1235                                                 + sizeof (struct lpfc_name));
1236                         memcpy(&ae->un.NodeName, &vport->fc_sparam.nodeName,
1237                                sizeof (struct lpfc_name));
1238                         ab->EntryCnt++;
1239                         size += FOURBYTES + sizeof (struct lpfc_name);
1240
1241                         /* #2 HBA attribute entry */
1242                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1243                         ae->ad.bits.AttrType = be16_to_cpu(MANUFACTURER);
1244                         strcpy(ae->un.Manufacturer, "Emulex Corporation");
1245                         len = strlen(ae->un.Manufacturer);
1246                         len += (len & 3) ? (4 - (len & 3)) : 4;
1247                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1248                         ab->EntryCnt++;
1249                         size += FOURBYTES + len;
1250
1251                         /* #3 HBA attribute entry */
1252                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1253                         ae->ad.bits.AttrType = be16_to_cpu(SERIAL_NUMBER);
1254                         strcpy(ae->un.SerialNumber, phba->SerialNumber);
1255                         len = strlen(ae->un.SerialNumber);
1256                         len += (len & 3) ? (4 - (len & 3)) : 4;
1257                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1258                         ab->EntryCnt++;
1259                         size += FOURBYTES + len;
1260
1261                         /* #4 HBA attribute entry */
1262                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1263                         ae->ad.bits.AttrType = be16_to_cpu(MODEL);
1264                         strcpy(ae->un.Model, phba->ModelName);
1265                         len = strlen(ae->un.Model);
1266                         len += (len & 3) ? (4 - (len & 3)) : 4;
1267                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1268                         ab->EntryCnt++;
1269                         size += FOURBYTES + len;
1270
1271                         /* #5 HBA attribute entry */
1272                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1273                         ae->ad.bits.AttrType = be16_to_cpu(MODEL_DESCRIPTION);
1274                         strcpy(ae->un.ModelDescription, phba->ModelDesc);
1275                         len = strlen(ae->un.ModelDescription);
1276                         len += (len & 3) ? (4 - (len & 3)) : 4;
1277                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1278                         ab->EntryCnt++;
1279                         size += FOURBYTES + len;
1280
1281                         /* #6 HBA attribute entry */
1282                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1283                         ae->ad.bits.AttrType = be16_to_cpu(HARDWARE_VERSION);
1284                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 8);
1285                         /* Convert JEDEC ID to ascii for hardware version */
1286                         incr = vp->rev.biuRev;
1287                         for (i = 0; i < 8; i++) {
1288                                 j = (incr & 0xf);
1289                                 if (j <= 9)
1290                                         ae->un.HardwareVersion[7 - i] =
1291                                             (char)((uint8_t) 0x30 +
1292                                                    (uint8_t) j);
1293                                 else
1294                                         ae->un.HardwareVersion[7 - i] =
1295                                             (char)((uint8_t) 0x61 +
1296                                                    (uint8_t) (j - 10));
1297                                 incr = (incr >> 4);
1298                         }
1299                         ab->EntryCnt++;
1300                         size += FOURBYTES + 8;
1301
1302                         /* #7 HBA attribute entry */
1303                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1304                         ae->ad.bits.AttrType = be16_to_cpu(DRIVER_VERSION);
1305                         strcpy(ae->un.DriverVersion, lpfc_release_version);
1306                         len = strlen(ae->un.DriverVersion);
1307                         len += (len & 3) ? (4 - (len & 3)) : 4;
1308                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1309                         ab->EntryCnt++;
1310                         size += FOURBYTES + len;
1311
1312                         /* #8 HBA attribute entry */
1313                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1314                         ae->ad.bits.AttrType = be16_to_cpu(OPTION_ROM_VERSION);
1315                         strcpy(ae->un.OptionROMVersion, phba->OptionROMVersion);
1316                         len = strlen(ae->un.OptionROMVersion);
1317                         len += (len & 3) ? (4 - (len & 3)) : 4;
1318                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1319                         ab->EntryCnt++;
1320                         size += FOURBYTES + len;
1321
1322                         /* #9 HBA attribute entry */
1323                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1324                         ae->ad.bits.AttrType = be16_to_cpu(FIRMWARE_VERSION);
1325                         lpfc_decode_firmware_rev(phba, ae->un.FirmwareVersion,
1326                                 1);
1327                         len = strlen(ae->un.FirmwareVersion);
1328                         len += (len & 3) ? (4 - (len & 3)) : 4;
1329                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1330                         ab->EntryCnt++;
1331                         size += FOURBYTES + len;
1332
1333                         /* #10 HBA attribute entry */
1334                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1335                         ae->ad.bits.AttrType = be16_to_cpu(OS_NAME_VERSION);
1336                         sprintf(ae->un.OsNameVersion, "%s %s %s",
1337                                 init_utsname()->sysname,
1338                                 init_utsname()->release,
1339                                 init_utsname()->version);
1340                         len = strlen(ae->un.OsNameVersion);
1341                         len += (len & 3) ? (4 - (len & 3)) : 4;
1342                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1343                         ab->EntryCnt++;
1344                         size += FOURBYTES + len;
1345
1346                         /* #11 HBA attribute entry */
1347                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) rh + size);
1348                         ae->ad.bits.AttrType = be16_to_cpu(MAX_CT_PAYLOAD_LEN);
1349                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1350                         ae->un.MaxCTPayloadLen = (65 * 4096);
1351                         ab->EntryCnt++;
1352                         size += FOURBYTES + 4;
1353
1354                         ab->EntryCnt = be32_to_cpu(ab->EntryCnt);
1355                         /* Total size */
1356                         size = GID_REQUEST_SZ - 4 + size;
1357                 }
1358                 break;
1359
1360         case SLI_MGMT_RPA:
1361                 {
1362                         lpfc_vpd_t *vp;
1363                         struct serv_parm *hsp;
1364                         int len;
1365
1366                         vp = &phba->vpd;
1367
1368                         CtReq->CommandResponse.bits.CmdRsp =
1369                             be16_to_cpu(SLI_MGMT_RPA);
1370                         CtReq->CommandResponse.bits.Size = 0;
1371                         pab = (REG_PORT_ATTRIBUTE *) & CtReq->un.PortID;
1372                         size = sizeof (struct lpfc_name) + FOURBYTES;
1373                         memcpy((uint8_t *) & pab->PortName,
1374                                (uint8_t *) & vport->fc_sparam.portName,
1375                                sizeof (struct lpfc_name));
1376                         pab->ab.EntryCnt = 0;
1377
1378                         /* #1 Port attribute entry */
1379                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1380                         ae->ad.bits.AttrType = be16_to_cpu(SUPPORTED_FC4_TYPES);
1381                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 32);
1382                         ae->un.SupportFC4Types[2] = 1;
1383                         ae->un.SupportFC4Types[7] = 1;
1384                         pab->ab.EntryCnt++;
1385                         size += FOURBYTES + 32;
1386
1387                         /* #2 Port attribute entry */
1388                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1389                         ae->ad.bits.AttrType = be16_to_cpu(SUPPORTED_SPEED);
1390                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1391
1392                         ae->un.SupportSpeed = 0;
1393                         if (phba->lmt & LMT_10Gb)
1394                                 ae->un.SupportSpeed = HBA_PORTSPEED_10GBIT;
1395                         if (phba->lmt & LMT_8Gb)
1396                                 ae->un.SupportSpeed |= HBA_PORTSPEED_8GBIT;
1397                         if (phba->lmt & LMT_4Gb)
1398                                 ae->un.SupportSpeed |= HBA_PORTSPEED_4GBIT;
1399                         if (phba->lmt & LMT_2Gb)
1400                                 ae->un.SupportSpeed |= HBA_PORTSPEED_2GBIT;
1401                         if (phba->lmt & LMT_1Gb)
1402                                 ae->un.SupportSpeed |= HBA_PORTSPEED_1GBIT;
1403
1404                         pab->ab.EntryCnt++;
1405                         size += FOURBYTES + 4;
1406
1407                         /* #3 Port attribute entry */
1408                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1409                         ae->ad.bits.AttrType = be16_to_cpu(PORT_SPEED);
1410                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1411                         switch(phba->fc_linkspeed) {
1412                                 case LA_1GHZ_LINK:
1413                                         ae->un.PortSpeed = HBA_PORTSPEED_1GBIT;
1414                                 break;
1415                                 case LA_2GHZ_LINK:
1416                                         ae->un.PortSpeed = HBA_PORTSPEED_2GBIT;
1417                                 break;
1418                                 case LA_4GHZ_LINK:
1419                                         ae->un.PortSpeed = HBA_PORTSPEED_4GBIT;
1420                                 break;
1421                                 case LA_8GHZ_LINK:
1422                                         ae->un.PortSpeed = HBA_PORTSPEED_8GBIT;
1423                                 break;
1424                                 default:
1425                                         ae->un.PortSpeed =
1426                                                 HBA_PORTSPEED_UNKNOWN;
1427                                 break;
1428                         }
1429                         pab->ab.EntryCnt++;
1430                         size += FOURBYTES + 4;
1431
1432                         /* #4 Port attribute entry */
1433                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1434                         ae->ad.bits.AttrType = be16_to_cpu(MAX_FRAME_SIZE);
1435                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + 4);
1436                         hsp = (struct serv_parm *) & vport->fc_sparam;
1437                         ae->un.MaxFrameSize =
1438                             (((uint32_t) hsp->cmn.
1439                               bbRcvSizeMsb) << 8) | (uint32_t) hsp->cmn.
1440                             bbRcvSizeLsb;
1441                         pab->ab.EntryCnt++;
1442                         size += FOURBYTES + 4;
1443
1444                         /* #5 Port attribute entry */
1445                         ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab + size);
1446                         ae->ad.bits.AttrType = be16_to_cpu(OS_DEVICE_NAME);
1447                         strcpy((char *)ae->un.OsDeviceName, LPFC_DRIVER_NAME);
1448                         len = strlen((char *)ae->un.OsDeviceName);
1449                         len += (len & 3) ? (4 - (len & 3)) : 4;
1450                         ae->ad.bits.AttrLen = be16_to_cpu(FOURBYTES + len);
1451                         pab->ab.EntryCnt++;
1452                         size += FOURBYTES + len;
1453
1454                         if (vport->cfg_fdmi_on == 2) {
1455                                 /* #6 Port attribute entry */
1456                                 ae = (ATTRIBUTE_ENTRY *) ((uint8_t *) pab +
1457                                                           size);
1458                                 ae->ad.bits.AttrType = be16_to_cpu(HOST_NAME);
1459                                 sprintf(ae->un.HostName, "%s",
1460                                         init_utsname()->nodename);
1461                                 len = strlen(ae->un.HostName);
1462                                 len += (len & 3) ? (4 - (len & 3)) : 4;
1463                                 ae->ad.bits.AttrLen =
1464                                     be16_to_cpu(FOURBYTES + len);
1465                                 pab->ab.EntryCnt++;
1466                                 size += FOURBYTES + len;
1467                         }
1468
1469                         pab->ab.EntryCnt = be32_to_cpu(pab->ab.EntryCnt);
1470                         /* Total size */
1471                         size = GID_REQUEST_SZ - 4 + size;
1472                 }
1473                 break;
1474
1475         case SLI_MGMT_DHBA:
1476                 CtReq->CommandResponse.bits.CmdRsp = be16_to_cpu(SLI_MGMT_DHBA);
1477                 CtReq->CommandResponse.bits.Size = 0;
1478                 pe = (PORT_ENTRY *) & CtReq->un.PortID;
1479                 memcpy((uint8_t *) & pe->PortName,
1480                        (uint8_t *) & vport->fc_sparam.portName,
1481                        sizeof (struct lpfc_name));
1482                 size = GID_REQUEST_SZ - 4 + sizeof (struct lpfc_name);
1483                 break;
1484
1485         case SLI_MGMT_DPRT:
1486                 CtReq->CommandResponse.bits.CmdRsp = be16_to_cpu(SLI_MGMT_DPRT);
1487                 CtReq->CommandResponse.bits.Size = 0;
1488                 pe = (PORT_ENTRY *) & CtReq->un.PortID;
1489                 memcpy((uint8_t *) & pe->PortName,
1490                        (uint8_t *) & vport->fc_sparam.portName,
1491                        sizeof (struct lpfc_name));
1492                 size = GID_REQUEST_SZ - 4 + sizeof (struct lpfc_name);
1493                 break;
1494         }
1495
1496         bpl = (struct ulp_bde64 *) bmp->virt;
1497         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
1498         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
1499         bpl->tus.f.bdeFlags = 0;
1500         bpl->tus.f.bdeSize = size;
1501         bpl->tus.w = le32_to_cpu(bpl->tus.w);
1502
1503         cmpl = lpfc_cmpl_ct_cmd_fdmi;
1504
1505         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, FC_MAX_NS_RSP, 0))
1506                 return 0;
1507
1508         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1509 fdmi_cmd_free_bmp:
1510         kfree(bmp);
1511 fdmi_cmd_free_mpvirt:
1512         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1513 fdmi_cmd_free_mp:
1514         kfree(mp);
1515 fdmi_cmd_exit:
1516         /* Issue FDMI request failed */
1517         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1518                          "0244 Issue FDMI request failed Data: x%x\n",
1519                          cmdcode);
1520         return 1;
1521 }
1522
1523 void
1524 lpfc_fdmi_tmo(unsigned long ptr)
1525 {
1526         struct lpfc_vport *vport = (struct lpfc_vport *)ptr;
1527         struct lpfc_hba   *phba = vport->phba;
1528         unsigned long iflag;
1529
1530         spin_lock_irqsave(&vport->work_port_lock, iflag);
1531         if (!(vport->work_port_events & WORKER_FDMI_TMO)) {
1532                 vport->work_port_events |= WORKER_FDMI_TMO;
1533                 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
1534
1535                 spin_lock_irqsave(&phba->hbalock, iflag);
1536                 if (phba->work_wait)
1537                         lpfc_worker_wake_up(phba);
1538                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1539         }
1540         else
1541                 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
1542 }
1543
1544 void
1545 lpfc_fdmi_timeout_handler(struct lpfc_vport *vport)
1546 {
1547         struct lpfc_nodelist *ndlp;
1548
1549         ndlp = lpfc_findnode_did(vport, FDMI_DID);
1550         if (ndlp) {
1551                 if (init_utsname()->nodename[0] != '\0')
1552                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
1553                 else
1554                         mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
1555         }
1556         return;
1557 }
1558
1559 void
1560 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
1561 {
1562         struct lpfc_sli *psli = &phba->sli;
1563         lpfc_vpd_t *vp = &phba->vpd;
1564         uint32_t b1, b2, b3, b4, i, rev;
1565         char c;
1566         uint32_t *ptr, str[4];
1567         uint8_t *fwname;
1568
1569         if (vp->rev.rBit) {
1570                 if (psli->sli_flag & LPFC_SLI2_ACTIVE)
1571                         rev = vp->rev.sli2FwRev;
1572                 else
1573                         rev = vp->rev.sli1FwRev;
1574
1575                 b1 = (rev & 0x0000f000) >> 12;
1576                 b2 = (rev & 0x00000f00) >> 8;
1577                 b3 = (rev & 0x000000c0) >> 6;
1578                 b4 = (rev & 0x00000030) >> 4;
1579
1580                 switch (b4) {
1581                 case 0:
1582                         c = 'N';
1583                         break;
1584                 case 1:
1585                         c = 'A';
1586                         break;
1587                 case 2:
1588                         c = 'B';
1589                         break;
1590                 default:
1591                         c = 0;
1592                         break;
1593                 }
1594                 b4 = (rev & 0x0000000f);
1595
1596                 if (psli->sli_flag & LPFC_SLI2_ACTIVE)
1597                         fwname = vp->rev.sli2FwName;
1598                 else
1599                         fwname = vp->rev.sli1FwName;
1600
1601                 for (i = 0; i < 16; i++)
1602                         if (fwname[i] == 0x20)
1603                                 fwname[i] = 0;
1604
1605                 ptr = (uint32_t*)fwname;
1606
1607                 for (i = 0; i < 3; i++)
1608                         str[i] = be32_to_cpu(*ptr++);
1609
1610                 if (c == 0) {
1611                         if (flag)
1612                                 sprintf(fwrevision, "%d.%d%d (%s)",
1613                                         b1, b2, b3, (char *)str);
1614                         else
1615                                 sprintf(fwrevision, "%d.%d%d", b1,
1616                                         b2, b3);
1617                 } else {
1618                         if (flag)
1619                                 sprintf(fwrevision, "%d.%d%d%c%d (%s)",
1620                                         b1, b2, b3, c,
1621                                         b4, (char *)str);
1622                         else
1623                                 sprintf(fwrevision, "%d.%d%d%c%d",
1624                                         b1, b2, b3, c, b4);
1625                 }
1626         } else {
1627                 rev = vp->rev.smFwRev;
1628
1629                 b1 = (rev & 0xff000000) >> 24;
1630                 b2 = (rev & 0x00f00000) >> 20;
1631                 b3 = (rev & 0x000f0000) >> 16;
1632                 c  = (rev & 0x0000ff00) >> 8;
1633                 b4 = (rev & 0x000000ff);
1634
1635                 if (flag)
1636                         sprintf(fwrevision, "%d.%d%d%c%d ", b1,
1637                                 b2, b3, c, b4);
1638                 else
1639                         sprintf(fwrevision, "%d.%d%d%c%d ", b1,
1640                                 b2, b3, c, b4);
1641         }
1642         return;
1643 }