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