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