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