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