]> Pileus Git - ~andy/linux/blob - drivers/scsi/lpfc/lpfc_els.c
[SCSI] lpfc 8.3.5: fix VPI registration, error clean up and add support for vlink...
[~andy/linux] / drivers / scsi / lpfc / lpfc_els.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21 /* See Fibre Channel protocol T11 FC-LS for details */
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_transport_fc.h>
30
31 #include "lpfc_hw4.h"
32 #include "lpfc_hw.h"
33 #include "lpfc_sli.h"
34 #include "lpfc_sli4.h"
35 #include "lpfc_nl.h"
36 #include "lpfc_disc.h"
37 #include "lpfc_scsi.h"
38 #include "lpfc.h"
39 #include "lpfc_logmsg.h"
40 #include "lpfc_crtn.h"
41 #include "lpfc_vport.h"
42 #include "lpfc_debugfs.h"
43
44 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
45                           struct lpfc_iocbq *);
46 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
47                         struct lpfc_iocbq *);
48 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
49 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
50                                 struct lpfc_nodelist *ndlp, uint8_t retry);
51 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
52                                   struct lpfc_iocbq *iocb);
53 static void lpfc_register_new_vport(struct lpfc_hba *phba,
54                                     struct lpfc_vport *vport,
55                                     struct lpfc_nodelist *ndlp);
56
57 static int lpfc_max_els_tries = 3;
58
59 /**
60  * lpfc_els_chk_latt - Check host link attention event for a vport
61  * @vport: pointer to a host virtual N_Port data structure.
62  *
63  * This routine checks whether there is an outstanding host link
64  * attention event during the discovery process with the @vport. It is done
65  * by reading the HBA's Host Attention (HA) register. If there is any host
66  * link attention events during this @vport's discovery process, the @vport
67  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
68  * be issued if the link state is not already in host link cleared state,
69  * and a return code shall indicate whether the host link attention event
70  * had happened.
71  *
72  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
73  * state in LPFC_VPORT_READY, the request for checking host link attention
74  * event will be ignored and a return code shall indicate no host link
75  * attention event had happened.
76  *
77  * Return codes
78  *   0 - no host link attention event happened
79  *   1 - host link attention event happened
80  **/
81 int
82 lpfc_els_chk_latt(struct lpfc_vport *vport)
83 {
84         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
85         struct lpfc_hba  *phba = vport->phba;
86         uint32_t ha_copy;
87
88         if (vport->port_state >= LPFC_VPORT_READY ||
89             phba->link_state == LPFC_LINK_DOWN ||
90             phba->sli_rev > LPFC_SLI_REV3)
91                 return 0;
92
93         /* Read the HBA Host Attention Register */
94         ha_copy = readl(phba->HAregaddr);
95
96         if (!(ha_copy & HA_LATT))
97                 return 0;
98
99         /* Pending Link Event during Discovery */
100         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
101                          "0237 Pending Link Event during "
102                          "Discovery: State x%x\n",
103                          phba->pport->port_state);
104
105         /* CLEAR_LA should re-enable link attention events and
106          * we should then imediately take a LATT event. The
107          * LATT processing should call lpfc_linkdown() which
108          * will cleanup any left over in-progress discovery
109          * events.
110          */
111         spin_lock_irq(shost->host_lock);
112         vport->fc_flag |= FC_ABORT_DISCOVERY;
113         spin_unlock_irq(shost->host_lock);
114
115         if (phba->link_state != LPFC_CLEAR_LA)
116                 lpfc_issue_clear_la(phba, vport);
117
118         return 1;
119 }
120
121 /**
122  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
123  * @vport: pointer to a host virtual N_Port data structure.
124  * @expectRsp: flag indicating whether response is expected.
125  * @cmdSize: size of the ELS command.
126  * @retry: number of retries to the command IOCB when it fails.
127  * @ndlp: pointer to a node-list data structure.
128  * @did: destination identifier.
129  * @elscmd: the ELS command code.
130  *
131  * This routine is used for allocating a lpfc-IOCB data structure from
132  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
133  * passed into the routine for discovery state machine to issue an Extended
134  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
135  * and preparation routine that is used by all the discovery state machine
136  * routines and the ELS command-specific fields will be later set up by
137  * the individual discovery machine routines after calling this routine
138  * allocating and preparing a generic IOCB data structure. It fills in the
139  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
140  * payload and response payload (if expected). The reference count on the
141  * ndlp is incremented by 1 and the reference to the ndlp is put into
142  * context1 of the IOCB data structure for this IOCB to hold the ndlp
143  * reference for the command's callback function to access later.
144  *
145  * Return code
146  *   Pointer to the newly allocated/prepared els iocb data structure
147  *   NULL - when els iocb data structure allocation/preparation failed
148  **/
149 struct lpfc_iocbq *
150 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
151                    uint16_t cmdSize, uint8_t retry,
152                    struct lpfc_nodelist *ndlp, uint32_t did,
153                    uint32_t elscmd)
154 {
155         struct lpfc_hba  *phba = vport->phba;
156         struct lpfc_iocbq *elsiocb;
157         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
158         struct ulp_bde64 *bpl;
159         IOCB_t *icmd;
160
161
162         if (!lpfc_is_link_up(phba))
163                 return NULL;
164
165         /* Allocate buffer for  command iocb */
166         elsiocb = lpfc_sli_get_iocbq(phba);
167
168         if (elsiocb == NULL)
169                 return NULL;
170
171         /*
172          * If this command is for fabric controller and HBA running
173          * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
174          */
175         if ((did == Fabric_DID) &&
176                 bf_get(lpfc_fip_flag, &phba->sli4_hba.sli4_flags) &&
177                 ((elscmd == ELS_CMD_FLOGI) ||
178                  (elscmd == ELS_CMD_FDISC) ||
179                  (elscmd == ELS_CMD_LOGO)))
180                 elsiocb->iocb_flag |= LPFC_FIP_ELS;
181         else
182                 elsiocb->iocb_flag &= ~LPFC_FIP_ELS;
183
184         icmd = &elsiocb->iocb;
185
186         /* fill in BDEs for command */
187         /* Allocate buffer for command payload */
188         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
189         if (pcmd)
190                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
191         if (!pcmd || !pcmd->virt)
192                 goto els_iocb_free_pcmb_exit;
193
194         INIT_LIST_HEAD(&pcmd->list);
195
196         /* Allocate buffer for response payload */
197         if (expectRsp) {
198                 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
199                 if (prsp)
200                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
201                                                      &prsp->phys);
202                 if (!prsp || !prsp->virt)
203                         goto els_iocb_free_prsp_exit;
204                 INIT_LIST_HEAD(&prsp->list);
205         } else
206                 prsp = NULL;
207
208         /* Allocate buffer for Buffer ptr list */
209         pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
210         if (pbuflist)
211                 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
212                                                  &pbuflist->phys);
213         if (!pbuflist || !pbuflist->virt)
214                 goto els_iocb_free_pbuf_exit;
215
216         INIT_LIST_HEAD(&pbuflist->list);
217
218         icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
219         icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
220         icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
221         icmd->un.elsreq64.remoteID = did;       /* DID */
222         if (expectRsp) {
223                 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
224                 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
225                 icmd->ulpTimeout = phba->fc_ratov * 2;
226         } else {
227                 icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
228                 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
229         }
230         icmd->ulpBdeCount = 1;
231         icmd->ulpLe = 1;
232         icmd->ulpClass = CLASS3;
233
234         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
235                 icmd->un.elsreq64.myID = vport->fc_myDID;
236
237                 /* For ELS_REQUEST64_CR, use the VPI by default */
238                 icmd->ulpContext = vport->vpi + phba->vpi_base;
239                 icmd->ulpCt_h = 0;
240                 /* The CT field must be 0=INVALID_RPI for the ECHO cmd */
241                 if (elscmd == ELS_CMD_ECHO)
242                         icmd->ulpCt_l = 0; /* context = invalid RPI */
243                 else
244                         icmd->ulpCt_l = 1; /* context = VPI */
245         }
246
247         bpl = (struct ulp_bde64 *) pbuflist->virt;
248         bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
249         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
250         bpl->tus.f.bdeSize = cmdSize;
251         bpl->tus.f.bdeFlags = 0;
252         bpl->tus.w = le32_to_cpu(bpl->tus.w);
253
254         if (expectRsp) {
255                 bpl++;
256                 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
257                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
258                 bpl->tus.f.bdeSize = FCELSSIZE;
259                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
260                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
261         }
262
263         /* prevent preparing iocb with NULL ndlp reference */
264         elsiocb->context1 = lpfc_nlp_get(ndlp);
265         if (!elsiocb->context1)
266                 goto els_iocb_free_pbuf_exit;
267         elsiocb->context2 = pcmd;
268         elsiocb->context3 = pbuflist;
269         elsiocb->retry = retry;
270         elsiocb->vport = vport;
271         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
272
273         if (prsp) {
274                 list_add(&prsp->list, &pcmd->list);
275         }
276         if (expectRsp) {
277                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
278                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
279                                  "0116 Xmit ELS command x%x to remote "
280                                  "NPORT x%x I/O tag: x%x, port state: x%x\n",
281                                  elscmd, did, elsiocb->iotag,
282                                  vport->port_state);
283         } else {
284                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
285                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
286                                  "0117 Xmit ELS response x%x to remote "
287                                  "NPORT x%x I/O tag: x%x, size: x%x\n",
288                                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
289                                  cmdSize);
290         }
291         return elsiocb;
292
293 els_iocb_free_pbuf_exit:
294         if (expectRsp)
295                 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
296         kfree(pbuflist);
297
298 els_iocb_free_prsp_exit:
299         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
300         kfree(prsp);
301
302 els_iocb_free_pcmb_exit:
303         kfree(pcmd);
304         lpfc_sli_release_iocbq(phba, elsiocb);
305         return NULL;
306 }
307
308 /**
309  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
310  * @vport: pointer to a host virtual N_Port data structure.
311  *
312  * This routine issues a fabric registration login for a @vport. An
313  * active ndlp node with Fabric_DID must already exist for this @vport.
314  * The routine invokes two mailbox commands to carry out fabric registration
315  * login through the HBA firmware: the first mailbox command requests the
316  * HBA to perform link configuration for the @vport; and the second mailbox
317  * command requests the HBA to perform the actual fabric registration login
318  * with the @vport.
319  *
320  * Return code
321  *   0 - successfully issued fabric registration login for @vport
322  *   -ENXIO -- failed to issue fabric registration login for @vport
323  **/
324 int
325 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
326 {
327         struct lpfc_hba  *phba = vport->phba;
328         LPFC_MBOXQ_t *mbox;
329         struct lpfc_dmabuf *mp;
330         struct lpfc_nodelist *ndlp;
331         struct serv_parm *sp;
332         int rc;
333         int err = 0;
334
335         sp = &phba->fc_fabparam;
336         ndlp = lpfc_findnode_did(vport, Fabric_DID);
337         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
338                 err = 1;
339                 goto fail;
340         }
341
342         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
343         if (!mbox) {
344                 err = 2;
345                 goto fail;
346         }
347
348         vport->port_state = LPFC_FABRIC_CFG_LINK;
349         lpfc_config_link(phba, mbox);
350         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
351         mbox->vport = vport;
352
353         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
354         if (rc == MBX_NOT_FINISHED) {
355                 err = 3;
356                 goto fail_free_mbox;
357         }
358
359         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
360         if (!mbox) {
361                 err = 4;
362                 goto fail;
363         }
364         rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox, 0);
365         if (rc) {
366                 err = 5;
367                 goto fail_free_mbox;
368         }
369
370         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
371         mbox->vport = vport;
372         /* increment the reference count on ndlp to hold reference
373          * for the callback routine.
374          */
375         mbox->context2 = lpfc_nlp_get(ndlp);
376
377         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
378         if (rc == MBX_NOT_FINISHED) {
379                 err = 6;
380                 goto fail_issue_reg_login;
381         }
382
383         return 0;
384
385 fail_issue_reg_login:
386         /* decrement the reference count on ndlp just incremented
387          * for the failed mbox command.
388          */
389         lpfc_nlp_put(ndlp);
390         mp = (struct lpfc_dmabuf *) mbox->context1;
391         lpfc_mbuf_free(phba, mp->virt, mp->phys);
392         kfree(mp);
393 fail_free_mbox:
394         mempool_free(mbox, phba->mbox_mem_pool);
395
396 fail:
397         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
398         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
399                 "0249 Cannot issue Register Fabric login: Err %d\n", err);
400         return -ENXIO;
401 }
402
403 /**
404  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
405  * @vport: pointer to a host virtual N_Port data structure.
406  *
407  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
408  * the @vport. This mailbox command is necessary for FCoE only.
409  *
410  * Return code
411  *   0 - successfully issued REG_VFI for @vport
412  *   A failure code otherwise.
413  **/
414 static int
415 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
416 {
417         struct lpfc_hba  *phba = vport->phba;
418         LPFC_MBOXQ_t *mboxq;
419         struct lpfc_nodelist *ndlp;
420         struct serv_parm *sp;
421         struct lpfc_dmabuf *dmabuf;
422         int rc = 0;
423
424         sp = &phba->fc_fabparam;
425         ndlp = lpfc_findnode_did(vport, Fabric_DID);
426         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
427                 rc = -ENODEV;
428                 goto fail;
429         }
430
431         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
432         if (!dmabuf) {
433                 rc = -ENOMEM;
434                 goto fail;
435         }
436         dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
437         if (!dmabuf->virt) {
438                 rc = -ENOMEM;
439                 goto fail_free_dmabuf;
440         }
441         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
442         if (!mboxq) {
443                 rc = -ENOMEM;
444                 goto fail_free_coherent;
445         }
446         vport->port_state = LPFC_FABRIC_CFG_LINK;
447         memcpy(dmabuf->virt, &phba->fc_fabparam, sizeof(vport->fc_sparam));
448         lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
449         mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
450         mboxq->vport = vport;
451         mboxq->context1 = dmabuf;
452         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
453         if (rc == MBX_NOT_FINISHED) {
454                 rc = -ENXIO;
455                 goto fail_free_mbox;
456         }
457         return 0;
458
459 fail_free_mbox:
460         mempool_free(mboxq, phba->mbox_mem_pool);
461 fail_free_coherent:
462         lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
463 fail_free_dmabuf:
464         kfree(dmabuf);
465 fail:
466         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
467         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
468                 "0289 Issue Register VFI failed: Err %d\n", rc);
469         return rc;
470 }
471
472 /**
473  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
474  * @vport: pointer to a host virtual N_Port data structure.
475  * @ndlp: pointer to a node-list data structure.
476  * @sp: pointer to service parameter data structure.
477  * @irsp: pointer to the IOCB within the lpfc response IOCB.
478  *
479  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
480  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
481  * port in a fabric topology. It properly sets up the parameters to the @ndlp
482  * from the IOCB response. It also check the newly assigned N_Port ID to the
483  * @vport against the previously assigned N_Port ID. If it is different from
484  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
485  * is invoked on all the remaining nodes with the @vport to unregister the
486  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
487  * is invoked to register login to the fabric.
488  *
489  * Return code
490  *   0 - Success (currently, always return 0)
491  **/
492 static int
493 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
494                            struct serv_parm *sp, IOCB_t *irsp)
495 {
496         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
497         struct lpfc_hba  *phba = vport->phba;
498         struct lpfc_nodelist *np;
499         struct lpfc_nodelist *next_np;
500
501         spin_lock_irq(shost->host_lock);
502         vport->fc_flag |= FC_FABRIC;
503         spin_unlock_irq(shost->host_lock);
504
505         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
506         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
507                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
508
509         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
510
511         if (phba->fc_topology == TOPOLOGY_LOOP) {
512                 spin_lock_irq(shost->host_lock);
513                 vport->fc_flag |= FC_PUBLIC_LOOP;
514                 spin_unlock_irq(shost->host_lock);
515         } else {
516                 /*
517                  * If we are a N-port connected to a Fabric, fixup sparam's so
518                  * logins to devices on remote loops work.
519                  */
520                 vport->fc_sparam.cmn.altBbCredit = 1;
521         }
522
523         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
524         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
525         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
526         ndlp->nlp_class_sup = 0;
527         if (sp->cls1.classValid)
528                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
529         if (sp->cls2.classValid)
530                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
531         if (sp->cls3.classValid)
532                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
533         if (sp->cls4.classValid)
534                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
535         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
536                                 sp->cmn.bbRcvSizeLsb;
537         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
538
539         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
540                 if (sp->cmn.response_multiple_NPort) {
541                         lpfc_printf_vlog(vport, KERN_WARNING,
542                                          LOG_ELS | LOG_VPORT,
543                                          "1816 FLOGI NPIV supported, "
544                                          "response data 0x%x\n",
545                                          sp->cmn.response_multiple_NPort);
546                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
547                 } else {
548                         /* Because we asked f/w for NPIV it still expects us
549                         to call reg_vnpid atleast for the physcial host */
550                         lpfc_printf_vlog(vport, KERN_WARNING,
551                                          LOG_ELS | LOG_VPORT,
552                                          "1817 Fabric does not support NPIV "
553                                          "- configuring single port mode.\n");
554                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
555                 }
556         }
557
558         if ((vport->fc_prevDID != vport->fc_myDID) &&
559                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
560
561                 /* If our NportID changed, we need to ensure all
562                  * remaining NPORTs get unreg_login'ed.
563                  */
564                 list_for_each_entry_safe(np, next_np,
565                                         &vport->fc_nodes, nlp_listp) {
566                         if (!NLP_CHK_NODE_ACT(np))
567                                 continue;
568                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
569                                    !(np->nlp_flag & NLP_NPR_ADISC))
570                                 continue;
571                         spin_lock_irq(shost->host_lock);
572                         np->nlp_flag &= ~NLP_NPR_ADISC;
573                         spin_unlock_irq(shost->host_lock);
574                         lpfc_unreg_rpi(vport, np);
575                 }
576                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
577                         lpfc_mbx_unreg_vpi(vport);
578                         spin_lock_irq(shost->host_lock);
579                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
580                         spin_unlock_irq(shost->host_lock);
581                 }
582         }
583
584         if (phba->sli_rev < LPFC_SLI_REV4) {
585                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
586                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
587                     vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
588                         lpfc_register_new_vport(phba, vport, ndlp);
589                 else
590                         lpfc_issue_fabric_reglogin(vport);
591         } else {
592                 ndlp->nlp_type |= NLP_FABRIC;
593                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
594                 if (vport->vfi_state & LPFC_VFI_REGISTERED) {
595                         lpfc_start_fdiscs(phba);
596                         lpfc_do_scr_ns_plogi(phba, vport);
597                 } else
598                         lpfc_issue_reg_vfi(vport);
599         }
600         return 0;
601 }
602 /**
603  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
604  * @vport: pointer to a host virtual N_Port data structure.
605  * @ndlp: pointer to a node-list data structure.
606  * @sp: pointer to service parameter data structure.
607  *
608  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
609  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
610  * in a point-to-point topology. First, the @vport's N_Port Name is compared
611  * with the received N_Port Name: if the @vport's N_Port Name is greater than
612  * the received N_Port Name lexicographically, this node shall assign local
613  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
614  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
615  * this node shall just wait for the remote node to issue PLOGI and assign
616  * N_Port IDs.
617  *
618  * Return code
619  *   0 - Success
620  *   -ENXIO - Fail
621  **/
622 static int
623 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
624                           struct serv_parm *sp)
625 {
626         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
627         struct lpfc_hba  *phba = vport->phba;
628         LPFC_MBOXQ_t *mbox;
629         int rc;
630
631         spin_lock_irq(shost->host_lock);
632         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
633         spin_unlock_irq(shost->host_lock);
634
635         phba->fc_edtov = FF_DEF_EDTOV;
636         phba->fc_ratov = FF_DEF_RATOV;
637         rc = memcmp(&vport->fc_portname, &sp->portName,
638                     sizeof(vport->fc_portname));
639         if (rc >= 0) {
640                 /* This side will initiate the PLOGI */
641                 spin_lock_irq(shost->host_lock);
642                 vport->fc_flag |= FC_PT2PT_PLOGI;
643                 spin_unlock_irq(shost->host_lock);
644
645                 /*
646                  * N_Port ID cannot be 0, set our to LocalID the other
647                  * side will be RemoteID.
648                  */
649
650                 /* not equal */
651                 if (rc)
652                         vport->fc_myDID = PT2PT_LocalID;
653
654                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
655                 if (!mbox)
656                         goto fail;
657
658                 lpfc_config_link(phba, mbox);
659
660                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
661                 mbox->vport = vport;
662                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
663                 if (rc == MBX_NOT_FINISHED) {
664                         mempool_free(mbox, phba->mbox_mem_pool);
665                         goto fail;
666                 }
667                 /* Decrement ndlp reference count indicating that ndlp can be
668                  * safely released when other references to it are done.
669                  */
670                 lpfc_nlp_put(ndlp);
671
672                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
673                 if (!ndlp) {
674                         /*
675                          * Cannot find existing Fabric ndlp, so allocate a
676                          * new one
677                          */
678                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
679                         if (!ndlp)
680                                 goto fail;
681                         lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
682                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
683                         ndlp = lpfc_enable_node(vport, ndlp,
684                                                 NLP_STE_UNUSED_NODE);
685                         if(!ndlp)
686                                 goto fail;
687                 }
688
689                 memcpy(&ndlp->nlp_portname, &sp->portName,
690                        sizeof(struct lpfc_name));
691                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
692                        sizeof(struct lpfc_name));
693                 /* Set state will put ndlp onto node list if not already done */
694                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
695                 spin_lock_irq(shost->host_lock);
696                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
697                 spin_unlock_irq(shost->host_lock);
698         } else
699                 /* This side will wait for the PLOGI, decrement ndlp reference
700                  * count indicating that ndlp can be released when other
701                  * references to it are done.
702                  */
703                 lpfc_nlp_put(ndlp);
704
705         /* If we are pt2pt with another NPort, force NPIV off! */
706         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
707
708         spin_lock_irq(shost->host_lock);
709         vport->fc_flag |= FC_PT2PT;
710         spin_unlock_irq(shost->host_lock);
711
712         /* Start discovery - this should just do CLEAR_LA */
713         lpfc_disc_start(vport);
714         return 0;
715 fail:
716         return -ENXIO;
717 }
718
719 /**
720  * lpfc_cmpl_els_flogi - Completion callback function for flogi
721  * @phba: pointer to lpfc hba data structure.
722  * @cmdiocb: pointer to lpfc command iocb data structure.
723  * @rspiocb: pointer to lpfc response iocb data structure.
724  *
725  * This routine is the top-level completion callback function for issuing
726  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
727  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
728  * retry has been made (either immediately or delayed with lpfc_els_retry()
729  * returning 1), the command IOCB will be released and function returned.
730  * If the retry attempt has been given up (possibly reach the maximum
731  * number of retries), one additional decrement of ndlp reference shall be
732  * invoked before going out after releasing the command IOCB. This will
733  * actually release the remote node (Note, lpfc_els_free_iocb() will also
734  * invoke one decrement of ndlp reference count). If no error reported in
735  * the IOCB status, the command Port ID field is used to determine whether
736  * this is a point-to-point topology or a fabric topology: if the Port ID
737  * field is assigned, it is a fabric topology; otherwise, it is a
738  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
739  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
740  * specific topology completion conditions.
741  **/
742 static void
743 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
744                     struct lpfc_iocbq *rspiocb)
745 {
746         struct lpfc_vport *vport = cmdiocb->vport;
747         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
748         IOCB_t *irsp = &rspiocb->iocb;
749         struct lpfc_nodelist *ndlp = cmdiocb->context1;
750         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
751         struct serv_parm *sp;
752         int rc;
753
754         /* Check to see if link went down during discovery */
755         if (lpfc_els_chk_latt(vport)) {
756                 /* One additional decrement on node reference count to
757                  * trigger the release of the node
758                  */
759                 lpfc_nlp_put(ndlp);
760                 goto out;
761         }
762
763         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
764                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
765                 irsp->ulpStatus, irsp->un.ulpWord[4],
766                 vport->port_state);
767
768         if (irsp->ulpStatus) {
769                 /* Check for retry */
770                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
771                         goto out;
772
773                 /* FLOGI failed, so there is no fabric */
774                 spin_lock_irq(shost->host_lock);
775                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
776                 spin_unlock_irq(shost->host_lock);
777
778                 /* If private loop, then allow max outstanding els to be
779                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
780                  * alpa map would take too long otherwise.
781                  */
782                 if (phba->alpa_map[0] == 0) {
783                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
784                 }
785
786                 /* FLOGI failure */
787                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
788                                  "0100 FLOGI failure Data: x%x x%x "
789                                  "x%x\n",
790                                  irsp->ulpStatus, irsp->un.ulpWord[4],
791                                  irsp->ulpTimeout);
792                 goto flogifail;
793         }
794
795         /*
796          * The FLogI succeeded.  Sync the data for the CPU before
797          * accessing it.
798          */
799         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
800
801         sp = prsp->virt + sizeof(uint32_t);
802
803         /* FLOGI completes successfully */
804         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
805                          "0101 FLOGI completes sucessfully "
806                          "Data: x%x x%x x%x x%x\n",
807                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
808                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
809
810         if (vport->port_state == LPFC_FLOGI) {
811                 /*
812                  * If Common Service Parameters indicate Nport
813                  * we are point to point, if Fport we are Fabric.
814                  */
815                 if (sp->cmn.fPort)
816                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
817                 else
818                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
819
820                 if (!rc)
821                         goto out;
822         }
823
824 flogifail:
825         lpfc_nlp_put(ndlp);
826
827         if (!lpfc_error_lost_link(irsp)) {
828                 /* FLOGI failed, so just use loop map to make discovery list */
829                 lpfc_disc_list_loopmap(vport);
830
831                 /* Start discovery */
832                 lpfc_disc_start(vport);
833         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
834                         ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
835                         (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
836                         (phba->link_state != LPFC_CLEAR_LA)) {
837                 /* If FLOGI failed enable link interrupt. */
838                 lpfc_issue_clear_la(phba, vport);
839         }
840 out:
841         lpfc_els_free_iocb(phba, cmdiocb);
842 }
843
844 /**
845  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
846  * @vport: pointer to a host virtual N_Port data structure.
847  * @ndlp: pointer to a node-list data structure.
848  * @retry: number of retries to the command IOCB.
849  *
850  * This routine issues a Fabric Login (FLOGI) Request ELS command
851  * for a @vport. The initiator service parameters are put into the payload
852  * of the FLOGI Request IOCB and the top-level callback function pointer
853  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
854  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
855  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
856  *
857  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
858  * will be incremented by 1 for holding the ndlp and the reference to ndlp
859  * will be stored into the context1 field of the IOCB for the completion
860  * callback function to the FLOGI ELS command.
861  *
862  * Return code
863  *   0 - successfully issued flogi iocb for @vport
864  *   1 - failed to issue flogi iocb for @vport
865  **/
866 static int
867 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
868                      uint8_t retry)
869 {
870         struct lpfc_hba  *phba = vport->phba;
871         struct serv_parm *sp;
872         IOCB_t *icmd;
873         struct lpfc_iocbq *elsiocb;
874         struct lpfc_sli_ring *pring;
875         uint8_t *pcmd;
876         uint16_t cmdsize;
877         uint32_t tmo;
878         int rc;
879
880         pring = &phba->sli.ring[LPFC_ELS_RING];
881
882         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
883         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
884                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
885
886         if (!elsiocb)
887                 return 1;
888
889         icmd = &elsiocb->iocb;
890         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
891
892         /* For FLOGI request, remainder of payload is service parameters */
893         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
894         pcmd += sizeof(uint32_t);
895         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
896         sp = (struct serv_parm *) pcmd;
897
898         /* Setup CSPs accordingly for Fabric */
899         sp->cmn.e_d_tov = 0;
900         sp->cmn.w2.r_a_tov = 0;
901         sp->cls1.classValid = 0;
902         sp->cls2.seqDelivery = 1;
903         sp->cls3.seqDelivery = 1;
904         if (sp->cmn.fcphLow < FC_PH3)
905                 sp->cmn.fcphLow = FC_PH3;
906         if (sp->cmn.fcphHigh < FC_PH3)
907                 sp->cmn.fcphHigh = FC_PH3;
908
909         if  (phba->sli_rev == LPFC_SLI_REV4) {
910                 elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
911                 elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
912                 /* FLOGI needs to be 3 for WQE FCFI */
913                 /* Set the fcfi to the fcfi we registered with */
914                 elsiocb->iocb.ulpContext = phba->fcf.fcfi;
915         } else if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
916                 sp->cmn.request_multiple_Nport = 1;
917                 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
918                 icmd->ulpCt_h = 1;
919                 icmd->ulpCt_l = 0;
920         }
921
922         if (phba->fc_topology != TOPOLOGY_LOOP) {
923                 icmd->un.elsreq64.myID = 0;
924                 icmd->un.elsreq64.fl = 1;
925         }
926
927         tmo = phba->fc_ratov;
928         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
929         lpfc_set_disctmo(vport);
930         phba->fc_ratov = tmo;
931
932         phba->fc_stat.elsXmitFLOGI++;
933         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
934
935         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
936                 "Issue FLOGI:     opt:x%x",
937                 phba->sli3_options, 0, 0);
938
939         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
940         if (rc == IOCB_ERROR) {
941                 lpfc_els_free_iocb(phba, elsiocb);
942                 return 1;
943         }
944         return 0;
945 }
946
947 /**
948  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
949  * @phba: pointer to lpfc hba data structure.
950  *
951  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
952  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
953  * list and issues an abort IOCB commond on each outstanding IOCB that
954  * contains a active Fabric_DID ndlp. Note that this function is to issue
955  * the abort IOCB command on all the outstanding IOCBs, thus when this
956  * function returns, it does not guarantee all the IOCBs are actually aborted.
957  *
958  * Return code
959  *   0 - Sucessfully issued abort iocb on all outstanding flogis (Always 0)
960  **/
961 int
962 lpfc_els_abort_flogi(struct lpfc_hba *phba)
963 {
964         struct lpfc_sli_ring *pring;
965         struct lpfc_iocbq *iocb, *next_iocb;
966         struct lpfc_nodelist *ndlp;
967         IOCB_t *icmd;
968
969         /* Abort outstanding I/O on NPort <nlp_DID> */
970         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
971                         "0201 Abort outstanding I/O on NPort x%x\n",
972                         Fabric_DID);
973
974         pring = &phba->sli.ring[LPFC_ELS_RING];
975
976         /*
977          * Check the txcmplq for an iocb that matches the nport the driver is
978          * searching for.
979          */
980         spin_lock_irq(&phba->hbalock);
981         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
982                 icmd = &iocb->iocb;
983                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
984                     icmd->un.elsreq64.bdl.ulpIoTag32) {
985                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
986                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
987                             (ndlp->nlp_DID == Fabric_DID))
988                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
989                 }
990         }
991         spin_unlock_irq(&phba->hbalock);
992
993         return 0;
994 }
995
996 /**
997  * lpfc_initial_flogi - Issue an initial fabric login for a vport
998  * @vport: pointer to a host virtual N_Port data structure.
999  *
1000  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1001  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1002  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1003  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1004  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1005  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1006  * @vport.
1007  *
1008  * Return code
1009  *   0 - failed to issue initial flogi for @vport
1010  *   1 - successfully issued initial flogi for @vport
1011  **/
1012 int
1013 lpfc_initial_flogi(struct lpfc_vport *vport)
1014 {
1015         struct lpfc_hba *phba = vport->phba;
1016         struct lpfc_nodelist *ndlp;
1017
1018         vport->port_state = LPFC_FLOGI;
1019         lpfc_set_disctmo(vport);
1020
1021         /* First look for the Fabric ndlp */
1022         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1023         if (!ndlp) {
1024                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1025                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1026                 if (!ndlp)
1027                         return 0;
1028                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
1029                 /* Set the node type */
1030                 ndlp->nlp_type |= NLP_FABRIC;
1031                 /* Put ndlp onto node list */
1032                 lpfc_enqueue_node(vport, ndlp);
1033         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1034                 /* re-setup ndlp without removing from node list */
1035                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1036                 if (!ndlp)
1037                         return 0;
1038         }
1039
1040         if (lpfc_issue_els_flogi(vport, ndlp, 0))
1041                 /* This decrement of reference count to node shall kick off
1042                  * the release of the node.
1043                  */
1044                 lpfc_nlp_put(ndlp);
1045
1046         return 1;
1047 }
1048
1049 /**
1050  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1051  * @vport: pointer to a host virtual N_Port data structure.
1052  *
1053  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1054  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1055  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1056  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1057  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1058  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1059  * @vport.
1060  *
1061  * Return code
1062  *   0 - failed to issue initial fdisc for @vport
1063  *   1 - successfully issued initial fdisc for @vport
1064  **/
1065 int
1066 lpfc_initial_fdisc(struct lpfc_vport *vport)
1067 {
1068         struct lpfc_hba *phba = vport->phba;
1069         struct lpfc_nodelist *ndlp;
1070
1071         /* First look for the Fabric ndlp */
1072         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1073         if (!ndlp) {
1074                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1075                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1076                 if (!ndlp)
1077                         return 0;
1078                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
1079                 /* Put ndlp onto node list */
1080                 lpfc_enqueue_node(vport, ndlp);
1081         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1082                 /* re-setup ndlp without removing from node list */
1083                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1084                 if (!ndlp)
1085                         return 0;
1086         }
1087
1088         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1089                 /* decrement node reference count to trigger the release of
1090                  * the node.
1091                  */
1092                 lpfc_nlp_put(ndlp);
1093                 return 0;
1094         }
1095         return 1;
1096 }
1097
1098 /**
1099  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1100  * @vport: pointer to a host virtual N_Port data structure.
1101  *
1102  * This routine checks whether there are more remaining Port Logins
1103  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1104  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1105  * to issue ELS PLOGIs up to the configured discover threads with the
1106  * @vport (@vport->cfg_discovery_threads). The function also decrement
1107  * the @vport's num_disc_node by 1 if it is not already 0.
1108  **/
1109 void
1110 lpfc_more_plogi(struct lpfc_vport *vport)
1111 {
1112         int sentplogi;
1113
1114         if (vport->num_disc_nodes)
1115                 vport->num_disc_nodes--;
1116
1117         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1118         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1119                          "0232 Continue discovery with %d PLOGIs to go "
1120                          "Data: x%x x%x x%x\n",
1121                          vport->num_disc_nodes, vport->fc_plogi_cnt,
1122                          vport->fc_flag, vport->port_state);
1123         /* Check to see if there are more PLOGIs to be sent */
1124         if (vport->fc_flag & FC_NLP_MORE)
1125                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1126                 sentplogi = lpfc_els_disc_plogi(vport);
1127
1128         return;
1129 }
1130
1131 /**
1132  * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1133  * @phba: pointer to lpfc hba data structure.
1134  * @prsp: pointer to response IOCB payload.
1135  * @ndlp: pointer to a node-list data structure.
1136  *
1137  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1138  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1139  * The following cases are considered N_Port confirmed:
1140  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1141  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1142  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1143  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1144  * 1) if there is a node on vport list other than the @ndlp with the same
1145  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1146  * on that node to release the RPI associated with the node; 2) if there is
1147  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1148  * into, a new node shall be allocated (or activated). In either case, the
1149  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1150  * be released and the new_ndlp shall be put on to the vport node list and
1151  * its pointer returned as the confirmed node.
1152  *
1153  * Note that before the @ndlp got "released", the keepDID from not-matching
1154  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1155  * of the @ndlp. This is because the release of @ndlp is actually to put it
1156  * into an inactive state on the vport node list and the vport node list
1157  * management algorithm does not allow two node with a same DID.
1158  *
1159  * Return code
1160  *   pointer to the PLOGI N_Port @ndlp
1161  **/
1162 static struct lpfc_nodelist *
1163 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1164                          struct lpfc_nodelist *ndlp)
1165 {
1166         struct lpfc_vport    *vport = ndlp->vport;
1167         struct lpfc_nodelist *new_ndlp;
1168         struct lpfc_rport_data *rdata;
1169         struct fc_rport *rport;
1170         struct serv_parm *sp;
1171         uint8_t  name[sizeof(struct lpfc_name)];
1172         uint32_t rc, keepDID = 0;
1173
1174         /* Fabric nodes can have the same WWPN so we don't bother searching
1175          * by WWPN.  Just return the ndlp that was given to us.
1176          */
1177         if (ndlp->nlp_type & NLP_FABRIC)
1178                 return ndlp;
1179
1180         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1181         memset(name, 0, sizeof(struct lpfc_name));
1182
1183         /* Now we find out if the NPort we are logging into, matches the WWPN
1184          * we have for that ndlp. If not, we have some work to do.
1185          */
1186         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1187
1188         if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1189                 return ndlp;
1190
1191         if (!new_ndlp) {
1192                 rc = memcmp(&ndlp->nlp_portname, name,
1193                             sizeof(struct lpfc_name));
1194                 if (!rc)
1195                         return ndlp;
1196                 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1197                 if (!new_ndlp)
1198                         return ndlp;
1199                 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
1200         } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1201                 rc = memcmp(&ndlp->nlp_portname, name,
1202                             sizeof(struct lpfc_name));
1203                 if (!rc)
1204                         return ndlp;
1205                 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1206                                                 NLP_STE_UNUSED_NODE);
1207                 if (!new_ndlp)
1208                         return ndlp;
1209                 keepDID = new_ndlp->nlp_DID;
1210         } else
1211                 keepDID = new_ndlp->nlp_DID;
1212
1213         lpfc_unreg_rpi(vport, new_ndlp);
1214         new_ndlp->nlp_DID = ndlp->nlp_DID;
1215         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1216
1217         if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
1218                 new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1219         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1220
1221         /* Set state will put new_ndlp on to node list if not already done */
1222         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1223
1224         /* Move this back to NPR state */
1225         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1226                 /* The new_ndlp is replacing ndlp totally, so we need
1227                  * to put ndlp on UNUSED list and try to free it.
1228                  */
1229
1230                 /* Fix up the rport accordingly */
1231                 rport =  ndlp->rport;
1232                 if (rport) {
1233                         rdata = rport->dd_data;
1234                         if (rdata->pnode == ndlp) {
1235                                 lpfc_nlp_put(ndlp);
1236                                 ndlp->rport = NULL;
1237                                 rdata->pnode = lpfc_nlp_get(new_ndlp);
1238                                 new_ndlp->rport = rport;
1239                         }
1240                         new_ndlp->nlp_type = ndlp->nlp_type;
1241                 }
1242                 /* We shall actually free the ndlp with both nlp_DID and
1243                  * nlp_portname fields equals 0 to avoid any ndlp on the
1244                  * nodelist never to be used.
1245                  */
1246                 if (ndlp->nlp_DID == 0) {
1247                         spin_lock_irq(&phba->ndlp_lock);
1248                         NLP_SET_FREE_REQ(ndlp);
1249                         spin_unlock_irq(&phba->ndlp_lock);
1250                 }
1251
1252                 /* Two ndlps cannot have the same did on the nodelist */
1253                 ndlp->nlp_DID = keepDID;
1254                 lpfc_drop_node(vport, ndlp);
1255         }
1256         else {
1257                 lpfc_unreg_rpi(vport, ndlp);
1258                 /* Two ndlps cannot have the same did */
1259                 ndlp->nlp_DID = keepDID;
1260                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1261         }
1262         return new_ndlp;
1263 }
1264
1265 /**
1266  * lpfc_end_rscn - Check and handle more rscn for a vport
1267  * @vport: pointer to a host virtual N_Port data structure.
1268  *
1269  * This routine checks whether more Registration State Change
1270  * Notifications (RSCNs) came in while the discovery state machine was in
1271  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1272  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1273  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1274  * handling the RSCNs.
1275  **/
1276 void
1277 lpfc_end_rscn(struct lpfc_vport *vport)
1278 {
1279         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1280
1281         if (vport->fc_flag & FC_RSCN_MODE) {
1282                 /*
1283                  * Check to see if more RSCNs came in while we were
1284                  * processing this one.
1285                  */
1286                 if (vport->fc_rscn_id_cnt ||
1287                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1288                         lpfc_els_handle_rscn(vport);
1289                 else {
1290                         spin_lock_irq(shost->host_lock);
1291                         vport->fc_flag &= ~FC_RSCN_MODE;
1292                         spin_unlock_irq(shost->host_lock);
1293                 }
1294         }
1295 }
1296
1297 /**
1298  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1299  * @phba: pointer to lpfc hba data structure.
1300  * @cmdiocb: pointer to lpfc command iocb data structure.
1301  * @rspiocb: pointer to lpfc response iocb data structure.
1302  *
1303  * This routine is the completion callback function for issuing the Port
1304  * Login (PLOGI) command. For PLOGI completion, there must be an active
1305  * ndlp on the vport node list that matches the remote node ID from the
1306  * PLOGI reponse IOCB. If such ndlp does not exist, the PLOGI is simply
1307  * ignored and command IOCB released. The PLOGI response IOCB status is
1308  * checked for error conditons. If there is error status reported, PLOGI
1309  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1310  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1311  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1312  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1313  * there are additional N_Port nodes with the vport that need to perform
1314  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1315  * PLOGIs.
1316  **/
1317 static void
1318 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1319                     struct lpfc_iocbq *rspiocb)
1320 {
1321         struct lpfc_vport *vport = cmdiocb->vport;
1322         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1323         IOCB_t *irsp;
1324         struct lpfc_nodelist *ndlp;
1325         struct lpfc_dmabuf *prsp;
1326         int disc, rc, did, type;
1327
1328         /* we pass cmdiocb to state machine which needs rspiocb as well */
1329         cmdiocb->context_un.rsp_iocb = rspiocb;
1330
1331         irsp = &rspiocb->iocb;
1332         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1333                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
1334                 irsp->ulpStatus, irsp->un.ulpWord[4],
1335                 irsp->un.elsreq64.remoteID);
1336
1337         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1338         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1339                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1340                                  "0136 PLOGI completes to NPort x%x "
1341                                  "with no ndlp. Data: x%x x%x x%x\n",
1342                                  irsp->un.elsreq64.remoteID,
1343                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1344                                  irsp->ulpIoTag);
1345                 goto out;
1346         }
1347
1348         /* Since ndlp can be freed in the disc state machine, note if this node
1349          * is being used during discovery.
1350          */
1351         spin_lock_irq(shost->host_lock);
1352         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1353         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1354         spin_unlock_irq(shost->host_lock);
1355         rc   = 0;
1356
1357         /* PLOGI completes to NPort <nlp_DID> */
1358         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1359                          "0102 PLOGI completes to NPort x%x "
1360                          "Data: x%x x%x x%x x%x x%x\n",
1361                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1362                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1363         /* Check to see if link went down during discovery */
1364         if (lpfc_els_chk_latt(vport)) {
1365                 spin_lock_irq(shost->host_lock);
1366                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1367                 spin_unlock_irq(shost->host_lock);
1368                 goto out;
1369         }
1370
1371         /* ndlp could be freed in DSM, save these values now */
1372         type = ndlp->nlp_type;
1373         did = ndlp->nlp_DID;
1374
1375         if (irsp->ulpStatus) {
1376                 /* Check for retry */
1377                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1378                         /* ELS command is being retried */
1379                         if (disc) {
1380                                 spin_lock_irq(shost->host_lock);
1381                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1382                                 spin_unlock_irq(shost->host_lock);
1383                         }
1384                         goto out;
1385                 }
1386                 /* PLOGI failed */
1387                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1388                 if (lpfc_error_lost_link(irsp))
1389                         rc = NLP_STE_FREED_NODE;
1390                 else
1391                         rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1392                                                      NLP_EVT_CMPL_PLOGI);
1393         } else {
1394                 /* Good status, call state machine */
1395                 prsp = list_entry(((struct lpfc_dmabuf *)
1396                                    cmdiocb->context2)->list.next,
1397                                   struct lpfc_dmabuf, list);
1398                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1399                 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1400                                              NLP_EVT_CMPL_PLOGI);
1401         }
1402
1403         if (disc && vport->num_disc_nodes) {
1404                 /* Check to see if there are more PLOGIs to be sent */
1405                 lpfc_more_plogi(vport);
1406
1407                 if (vport->num_disc_nodes == 0) {
1408                         spin_lock_irq(shost->host_lock);
1409                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
1410                         spin_unlock_irq(shost->host_lock);
1411
1412                         lpfc_can_disctmo(vport);
1413                         lpfc_end_rscn(vport);
1414                 }
1415         }
1416
1417 out:
1418         lpfc_els_free_iocb(phba, cmdiocb);
1419         return;
1420 }
1421
1422 /**
1423  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
1424  * @vport: pointer to a host virtual N_Port data structure.
1425  * @did: destination port identifier.
1426  * @retry: number of retries to the command IOCB.
1427  *
1428  * This routine issues a Port Login (PLOGI) command to a remote N_Port
1429  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1430  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1431  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1432  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1433  *
1434  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1435  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1436  * will be stored into the context1 field of the IOCB for the completion
1437  * callback function to the PLOGI ELS command.
1438  *
1439  * Return code
1440  *   0 - Successfully issued a plogi for @vport
1441  *   1 - failed to issue a plogi for @vport
1442  **/
1443 int
1444 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1445 {
1446         struct lpfc_hba  *phba = vport->phba;
1447         struct serv_parm *sp;
1448         IOCB_t *icmd;
1449         struct lpfc_nodelist *ndlp;
1450         struct lpfc_iocbq *elsiocb;
1451         struct lpfc_sli *psli;
1452         uint8_t *pcmd;
1453         uint16_t cmdsize;
1454         int ret;
1455
1456         psli = &phba->sli;
1457
1458         ndlp = lpfc_findnode_did(vport, did);
1459         if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1460                 ndlp = NULL;
1461
1462         /* If ndlp is not NULL, we will bump the reference count on it */
1463         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1464         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
1465                                      ELS_CMD_PLOGI);
1466         if (!elsiocb)
1467                 return 1;
1468
1469         icmd = &elsiocb->iocb;
1470         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1471
1472         /* For PLOGI request, remainder of payload is service parameters */
1473         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
1474         pcmd += sizeof(uint32_t);
1475         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1476         sp = (struct serv_parm *) pcmd;
1477
1478         if (sp->cmn.fcphLow < FC_PH_4_3)
1479                 sp->cmn.fcphLow = FC_PH_4_3;
1480
1481         if (sp->cmn.fcphHigh < FC_PH3)
1482                 sp->cmn.fcphHigh = FC_PH3;
1483
1484         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1485                 "Issue PLOGI:     did:x%x",
1486                 did, 0, 0);
1487
1488         phba->fc_stat.elsXmitPLOGI++;
1489         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1490         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
1491
1492         if (ret == IOCB_ERROR) {
1493                 lpfc_els_free_iocb(phba, elsiocb);
1494                 return 1;
1495         }
1496         return 0;
1497 }
1498
1499 /**
1500  * lpfc_cmpl_els_prli - Completion callback function for prli
1501  * @phba: pointer to lpfc hba data structure.
1502  * @cmdiocb: pointer to lpfc command iocb data structure.
1503  * @rspiocb: pointer to lpfc response iocb data structure.
1504  *
1505  * This routine is the completion callback function for a Process Login
1506  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
1507  * status. If there is error status reported, PRLI retry shall be attempted
1508  * by invoking the lpfc_els_retry() routine. Otherwise, the state
1509  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
1510  * ndlp to mark the PRLI completion.
1511  **/
1512 static void
1513 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1514                    struct lpfc_iocbq *rspiocb)
1515 {
1516         struct lpfc_vport *vport = cmdiocb->vport;
1517         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1518         IOCB_t *irsp;
1519         struct lpfc_sli *psli;
1520         struct lpfc_nodelist *ndlp;
1521
1522         psli = &phba->sli;
1523         /* we pass cmdiocb to state machine which needs rspiocb as well */
1524         cmdiocb->context_un.rsp_iocb = rspiocb;
1525
1526         irsp = &(rspiocb->iocb);
1527         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1528         spin_lock_irq(shost->host_lock);
1529         ndlp->nlp_flag &= ~NLP_PRLI_SND;
1530         spin_unlock_irq(shost->host_lock);
1531
1532         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1533                 "PRLI cmpl:       status:x%x/x%x did:x%x",
1534                 irsp->ulpStatus, irsp->un.ulpWord[4],
1535                 ndlp->nlp_DID);
1536         /* PRLI completes to NPort <nlp_DID> */
1537         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1538                          "0103 PRLI completes to NPort x%x "
1539                          "Data: x%x x%x x%x x%x\n",
1540                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1541                          irsp->ulpTimeout, vport->num_disc_nodes);
1542
1543         vport->fc_prli_sent--;
1544         /* Check to see if link went down during discovery */
1545         if (lpfc_els_chk_latt(vport))
1546                 goto out;
1547
1548         if (irsp->ulpStatus) {
1549                 /* Check for retry */
1550                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1551                         /* ELS command is being retried */
1552                         goto out;
1553                 }
1554                 /* PRLI failed */
1555                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1556                 if (lpfc_error_lost_link(irsp))
1557                         goto out;
1558                 else
1559                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1560                                                 NLP_EVT_CMPL_PRLI);
1561         } else
1562                 /* Good status, call state machine */
1563                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1564                                         NLP_EVT_CMPL_PRLI);
1565 out:
1566         lpfc_els_free_iocb(phba, cmdiocb);
1567         return;
1568 }
1569
1570 /**
1571  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
1572  * @vport: pointer to a host virtual N_Port data structure.
1573  * @ndlp: pointer to a node-list data structure.
1574  * @retry: number of retries to the command IOCB.
1575  *
1576  * This routine issues a Process Login (PRLI) ELS command for the
1577  * @vport. The PRLI service parameters are set up in the payload of the
1578  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
1579  * is put to the IOCB completion callback func field before invoking the
1580  * routine lpfc_sli_issue_iocb() to send out PRLI command.
1581  *
1582  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1583  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1584  * will be stored into the context1 field of the IOCB for the completion
1585  * callback function to the PRLI ELS command.
1586  *
1587  * Return code
1588  *   0 - successfully issued prli iocb command for @vport
1589  *   1 - failed to issue prli iocb command for @vport
1590  **/
1591 int
1592 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1593                     uint8_t retry)
1594 {
1595         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1596         struct lpfc_hba *phba = vport->phba;
1597         PRLI *npr;
1598         IOCB_t *icmd;
1599         struct lpfc_iocbq *elsiocb;
1600         uint8_t *pcmd;
1601         uint16_t cmdsize;
1602
1603         cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1604         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1605                                      ndlp->nlp_DID, ELS_CMD_PRLI);
1606         if (!elsiocb)
1607                 return 1;
1608
1609         icmd = &elsiocb->iocb;
1610         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1611
1612         /* For PRLI request, remainder of payload is service parameters */
1613         memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1614         *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1615         pcmd += sizeof(uint32_t);
1616
1617         /* For PRLI, remainder of payload is PRLI parameter page */
1618         npr = (PRLI *) pcmd;
1619         /*
1620          * If our firmware version is 3.20 or later,
1621          * set the following bits for FC-TAPE support.
1622          */
1623         if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1624                 npr->ConfmComplAllowed = 1;
1625                 npr->Retry = 1;
1626                 npr->TaskRetryIdReq = 1;
1627         }
1628         npr->estabImagePair = 1;
1629         npr->readXferRdyDis = 1;
1630
1631         /* For FCP support */
1632         npr->prliType = PRLI_FCP_TYPE;
1633         npr->initiatorFunc = 1;
1634
1635         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1636                 "Issue PRLI:      did:x%x",
1637                 ndlp->nlp_DID, 0, 0);
1638
1639         phba->fc_stat.elsXmitPRLI++;
1640         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1641         spin_lock_irq(shost->host_lock);
1642         ndlp->nlp_flag |= NLP_PRLI_SND;
1643         spin_unlock_irq(shost->host_lock);
1644         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
1645             IOCB_ERROR) {
1646                 spin_lock_irq(shost->host_lock);
1647                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
1648                 spin_unlock_irq(shost->host_lock);
1649                 lpfc_els_free_iocb(phba, elsiocb);
1650                 return 1;
1651         }
1652         vport->fc_prli_sent++;
1653         return 0;
1654 }
1655
1656 /**
1657  * lpfc_rscn_disc - Perform rscn discovery for a vport
1658  * @vport: pointer to a host virtual N_Port data structure.
1659  *
1660  * This routine performs Registration State Change Notification (RSCN)
1661  * discovery for a @vport. If the @vport's node port recovery count is not
1662  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
1663  * the nodes that need recovery. If none of the PLOGI were needed through
1664  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
1665  * invoked to check and handle possible more RSCN came in during the period
1666  * of processing the current ones.
1667  **/
1668 static void
1669 lpfc_rscn_disc(struct lpfc_vport *vport)
1670 {
1671         lpfc_can_disctmo(vport);
1672
1673         /* RSCN discovery */
1674         /* go thru NPR nodes and issue ELS PLOGIs */
1675         if (vport->fc_npr_cnt)
1676                 if (lpfc_els_disc_plogi(vport))
1677                         return;
1678
1679         lpfc_end_rscn(vport);
1680 }
1681
1682 /**
1683  * lpfc_adisc_done - Complete the adisc phase of discovery
1684  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
1685  *
1686  * This function is called when the final ADISC is completed during discovery.
1687  * This function handles clearing link attention or issuing reg_vpi depending
1688  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
1689  * discovery.
1690  * This function is called with no locks held.
1691  **/
1692 static void
1693 lpfc_adisc_done(struct lpfc_vport *vport)
1694 {
1695         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1696         struct lpfc_hba   *phba = vport->phba;
1697
1698         /*
1699          * For NPIV, cmpl_reg_vpi will set port_state to READY,
1700          * and continue discovery.
1701          */
1702         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1703             !(vport->fc_flag & FC_RSCN_MODE) &&
1704             (phba->sli_rev < LPFC_SLI_REV4)) {
1705                 lpfc_issue_reg_vpi(phba, vport);
1706                 return;
1707         }
1708         /*
1709         * For SLI2, we need to set port_state to READY
1710         * and continue discovery.
1711         */
1712         if (vport->port_state < LPFC_VPORT_READY) {
1713                 /* If we get here, there is nothing to ADISC */
1714                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1715                         lpfc_issue_clear_la(phba, vport);
1716                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1717                         vport->num_disc_nodes = 0;
1718                         /* go thru NPR list, issue ELS PLOGIs */
1719                         if (vport->fc_npr_cnt)
1720                                 lpfc_els_disc_plogi(vport);
1721                         if (!vport->num_disc_nodes) {
1722                                 spin_lock_irq(shost->host_lock);
1723                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1724                                 spin_unlock_irq(shost->host_lock);
1725                                 lpfc_can_disctmo(vport);
1726                                 lpfc_end_rscn(vport);
1727                         }
1728                 }
1729                 vport->port_state = LPFC_VPORT_READY;
1730         } else
1731                 lpfc_rscn_disc(vport);
1732 }
1733
1734 /**
1735  * lpfc_more_adisc - Issue more adisc as needed
1736  * @vport: pointer to a host virtual N_Port data structure.
1737  *
1738  * This routine determines whether there are more ndlps on a @vport
1739  * node list need to have Address Discover (ADISC) issued. If so, it will
1740  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
1741  * remaining nodes which need to have ADISC sent.
1742  **/
1743 void
1744 lpfc_more_adisc(struct lpfc_vport *vport)
1745 {
1746         int sentadisc;
1747
1748         if (vport->num_disc_nodes)
1749                 vport->num_disc_nodes--;
1750         /* Continue discovery with <num_disc_nodes> ADISCs to go */
1751         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1752                          "0210 Continue discovery with %d ADISCs to go "
1753                          "Data: x%x x%x x%x\n",
1754                          vport->num_disc_nodes, vport->fc_adisc_cnt,
1755                          vport->fc_flag, vport->port_state);
1756         /* Check to see if there are more ADISCs to be sent */
1757         if (vport->fc_flag & FC_NLP_MORE) {
1758                 lpfc_set_disctmo(vport);
1759                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
1760                 sentadisc = lpfc_els_disc_adisc(vport);
1761         }
1762         if (!vport->num_disc_nodes)
1763                 lpfc_adisc_done(vport);
1764         return;
1765 }
1766
1767 /**
1768  * lpfc_cmpl_els_adisc - Completion callback function for adisc
1769  * @phba: pointer to lpfc hba data structure.
1770  * @cmdiocb: pointer to lpfc command iocb data structure.
1771  * @rspiocb: pointer to lpfc response iocb data structure.
1772  *
1773  * This routine is the completion function for issuing the Address Discover
1774  * (ADISC) command. It first checks to see whether link went down during
1775  * the discovery process. If so, the node will be marked as node port
1776  * recovery for issuing discover IOCB by the link attention handler and
1777  * exit. Otherwise, the response status is checked. If error was reported
1778  * in the response status, the ADISC command shall be retried by invoking
1779  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
1780  * the response status, the state machine is invoked to set transition
1781  * with respect to NLP_EVT_CMPL_ADISC event.
1782  **/
1783 static void
1784 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1785                     struct lpfc_iocbq *rspiocb)
1786 {
1787         struct lpfc_vport *vport = cmdiocb->vport;
1788         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1789         IOCB_t *irsp;
1790         struct lpfc_nodelist *ndlp;
1791         int  disc;
1792
1793         /* we pass cmdiocb to state machine which needs rspiocb as well */
1794         cmdiocb->context_un.rsp_iocb = rspiocb;
1795
1796         irsp = &(rspiocb->iocb);
1797         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1798
1799         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1800                 "ADISC cmpl:      status:x%x/x%x did:x%x",
1801                 irsp->ulpStatus, irsp->un.ulpWord[4],
1802                 ndlp->nlp_DID);
1803
1804         /* Since ndlp can be freed in the disc state machine, note if this node
1805          * is being used during discovery.
1806          */
1807         spin_lock_irq(shost->host_lock);
1808         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1809         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1810         spin_unlock_irq(shost->host_lock);
1811         /* ADISC completes to NPort <nlp_DID> */
1812         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1813                          "0104 ADISC completes to NPort x%x "
1814                          "Data: x%x x%x x%x x%x x%x\n",
1815                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1816                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1817         /* Check to see if link went down during discovery */
1818         if (lpfc_els_chk_latt(vport)) {
1819                 spin_lock_irq(shost->host_lock);
1820                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1821                 spin_unlock_irq(shost->host_lock);
1822                 goto out;
1823         }
1824
1825         if (irsp->ulpStatus) {
1826                 /* Check for retry */
1827                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1828                         /* ELS command is being retried */
1829                         if (disc) {
1830                                 spin_lock_irq(shost->host_lock);
1831                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1832                                 spin_unlock_irq(shost->host_lock);
1833                                 lpfc_set_disctmo(vport);
1834                         }
1835                         goto out;
1836                 }
1837                 /* ADISC failed */
1838                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1839                 if (!lpfc_error_lost_link(irsp))
1840                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1841                                                 NLP_EVT_CMPL_ADISC);
1842         } else
1843                 /* Good status, call state machine */
1844                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1845                                         NLP_EVT_CMPL_ADISC);
1846
1847         /* Check to see if there are more ADISCs to be sent */
1848         if (disc && vport->num_disc_nodes)
1849                 lpfc_more_adisc(vport);
1850 out:
1851         lpfc_els_free_iocb(phba, cmdiocb);
1852         return;
1853 }
1854
1855 /**
1856  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
1857  * @vport: pointer to a virtual N_Port data structure.
1858  * @ndlp: pointer to a node-list data structure.
1859  * @retry: number of retries to the command IOCB.
1860  *
1861  * This routine issues an Address Discover (ADISC) for an @ndlp on a
1862  * @vport. It prepares the payload of the ADISC ELS command, updates the
1863  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
1864  * to issue the ADISC ELS command.
1865  *
1866  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1867  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1868  * will be stored into the context1 field of the IOCB for the completion
1869  * callback function to the ADISC ELS command.
1870  *
1871  * Return code
1872  *   0 - successfully issued adisc
1873  *   1 - failed to issue adisc
1874  **/
1875 int
1876 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1877                      uint8_t retry)
1878 {
1879         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1880         struct lpfc_hba  *phba = vport->phba;
1881         ADISC *ap;
1882         IOCB_t *icmd;
1883         struct lpfc_iocbq *elsiocb;
1884         uint8_t *pcmd;
1885         uint16_t cmdsize;
1886
1887         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
1888         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1889                                      ndlp->nlp_DID, ELS_CMD_ADISC);
1890         if (!elsiocb)
1891                 return 1;
1892
1893         icmd = &elsiocb->iocb;
1894         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1895
1896         /* For ADISC request, remainder of payload is service parameters */
1897         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
1898         pcmd += sizeof(uint32_t);
1899
1900         /* Fill in ADISC payload */
1901         ap = (ADISC *) pcmd;
1902         ap->hardAL_PA = phba->fc_pref_ALPA;
1903         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
1904         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
1905         ap->DID = be32_to_cpu(vport->fc_myDID);
1906
1907         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1908                 "Issue ADISC:     did:x%x",
1909                 ndlp->nlp_DID, 0, 0);
1910
1911         phba->fc_stat.elsXmitADISC++;
1912         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
1913         spin_lock_irq(shost->host_lock);
1914         ndlp->nlp_flag |= NLP_ADISC_SND;
1915         spin_unlock_irq(shost->host_lock);
1916         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
1917             IOCB_ERROR) {
1918                 spin_lock_irq(shost->host_lock);
1919                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
1920                 spin_unlock_irq(shost->host_lock);
1921                 lpfc_els_free_iocb(phba, elsiocb);
1922                 return 1;
1923         }
1924         return 0;
1925 }
1926
1927 /**
1928  * lpfc_cmpl_els_logo - Completion callback function for logo
1929  * @phba: pointer to lpfc hba data structure.
1930  * @cmdiocb: pointer to lpfc command iocb data structure.
1931  * @rspiocb: pointer to lpfc response iocb data structure.
1932  *
1933  * This routine is the completion function for issuing the ELS Logout (LOGO)
1934  * command. If no error status was reported from the LOGO response, the
1935  * state machine of the associated ndlp shall be invoked for transition with
1936  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
1937  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
1938  **/
1939 static void
1940 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1941                    struct lpfc_iocbq *rspiocb)
1942 {
1943         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1944         struct lpfc_vport *vport = ndlp->vport;
1945         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1946         IOCB_t *irsp;
1947         struct lpfc_sli *psli;
1948
1949         psli = &phba->sli;
1950         /* we pass cmdiocb to state machine which needs rspiocb as well */
1951         cmdiocb->context_un.rsp_iocb = rspiocb;
1952
1953         irsp = &(rspiocb->iocb);
1954         spin_lock_irq(shost->host_lock);
1955         ndlp->nlp_flag &= ~NLP_LOGO_SND;
1956         spin_unlock_irq(shost->host_lock);
1957
1958         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1959                 "LOGO cmpl:       status:x%x/x%x did:x%x",
1960                 irsp->ulpStatus, irsp->un.ulpWord[4],
1961                 ndlp->nlp_DID);
1962         /* LOGO completes to NPort <nlp_DID> */
1963         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1964                          "0105 LOGO completes to NPort x%x "
1965                          "Data: x%x x%x x%x x%x\n",
1966                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1967                          irsp->ulpTimeout, vport->num_disc_nodes);
1968         /* Check to see if link went down during discovery */
1969         if (lpfc_els_chk_latt(vport))
1970                 goto out;
1971
1972         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
1973                 /* NLP_EVT_DEVICE_RM should unregister the RPI
1974                  * which should abort all outstanding IOs.
1975                  */
1976                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1977                                         NLP_EVT_DEVICE_RM);
1978                 goto out;
1979         }
1980
1981         if (irsp->ulpStatus) {
1982                 /* Check for retry */
1983                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1984                         /* ELS command is being retried */
1985                         goto out;
1986                 /* LOGO failed */
1987                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1988                 if (lpfc_error_lost_link(irsp))
1989                         goto out;
1990                 else
1991                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1992                                                 NLP_EVT_CMPL_LOGO);
1993         } else
1994                 /* Good status, call state machine.
1995                  * This will unregister the rpi if needed.
1996                  */
1997                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1998                                         NLP_EVT_CMPL_LOGO);
1999 out:
2000         lpfc_els_free_iocb(phba, cmdiocb);
2001         return;
2002 }
2003
2004 /**
2005  * lpfc_issue_els_logo - Issue a logo to an node on a vport
2006  * @vport: pointer to a virtual N_Port data structure.
2007  * @ndlp: pointer to a node-list data structure.
2008  * @retry: number of retries to the command IOCB.
2009  *
2010  * This routine constructs and issues an ELS Logout (LOGO) iocb command
2011  * to a remote node, referred by an @ndlp on a @vport. It constructs the
2012  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2013  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2014  *
2015  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2016  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2017  * will be stored into the context1 field of the IOCB for the completion
2018  * callback function to the LOGO ELS command.
2019  *
2020  * Return code
2021  *   0 - successfully issued logo
2022  *   1 - failed to issue logo
2023  **/
2024 int
2025 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2026                     uint8_t retry)
2027 {
2028         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2029         struct lpfc_hba  *phba = vport->phba;
2030         IOCB_t *icmd;
2031         struct lpfc_iocbq *elsiocb;
2032         uint8_t *pcmd;
2033         uint16_t cmdsize;
2034         int rc;
2035
2036         spin_lock_irq(shost->host_lock);
2037         if (ndlp->nlp_flag & NLP_LOGO_SND) {
2038                 spin_unlock_irq(shost->host_lock);
2039                 return 0;
2040         }
2041         spin_unlock_irq(shost->host_lock);
2042
2043         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2044         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2045                                      ndlp->nlp_DID, ELS_CMD_LOGO);
2046         if (!elsiocb)
2047                 return 1;
2048
2049         icmd = &elsiocb->iocb;
2050         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2051         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
2052         pcmd += sizeof(uint32_t);
2053
2054         /* Fill in LOGO payload */
2055         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
2056         pcmd += sizeof(uint32_t);
2057         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
2058
2059         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2060                 "Issue LOGO:      did:x%x",
2061                 ndlp->nlp_DID, 0, 0);
2062
2063         phba->fc_stat.elsXmitLOGO++;
2064         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2065         spin_lock_irq(shost->host_lock);
2066         ndlp->nlp_flag |= NLP_LOGO_SND;
2067         spin_unlock_irq(shost->host_lock);
2068         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2069
2070         if (rc == IOCB_ERROR) {
2071                 spin_lock_irq(shost->host_lock);
2072                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2073                 spin_unlock_irq(shost->host_lock);
2074                 lpfc_els_free_iocb(phba, elsiocb);
2075                 return 1;
2076         }
2077         return 0;
2078 }
2079
2080 /**
2081  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
2082  * @phba: pointer to lpfc hba data structure.
2083  * @cmdiocb: pointer to lpfc command iocb data structure.
2084  * @rspiocb: pointer to lpfc response iocb data structure.
2085  *
2086  * This routine is a generic completion callback function for ELS commands.
2087  * Specifically, it is the callback function which does not need to perform
2088  * any command specific operations. It is currently used by the ELS command
2089  * issuing routines for the ELS State Change  Request (SCR),
2090  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2091  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2092  * certain debug loggings, this callback function simply invokes the
2093  * lpfc_els_chk_latt() routine to check whether link went down during the
2094  * discovery process.
2095  **/
2096 static void
2097 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2098                   struct lpfc_iocbq *rspiocb)
2099 {
2100         struct lpfc_vport *vport = cmdiocb->vport;
2101         IOCB_t *irsp;
2102
2103         irsp = &rspiocb->iocb;
2104
2105         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2106                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
2107                 irsp->ulpStatus, irsp->un.ulpWord[4],
2108                 irsp->un.elsreq64.remoteID);
2109         /* ELS cmd tag <ulpIoTag> completes */
2110         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2111                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2112                          irsp->ulpIoTag, irsp->ulpStatus,
2113                          irsp->un.ulpWord[4], irsp->ulpTimeout);
2114         /* Check to see if link went down during discovery */
2115         lpfc_els_chk_latt(vport);
2116         lpfc_els_free_iocb(phba, cmdiocb);
2117         return;
2118 }
2119
2120 /**
2121  * lpfc_issue_els_scr - Issue a scr to an node on a vport
2122  * @vport: pointer to a host virtual N_Port data structure.
2123  * @nportid: N_Port identifier to the remote node.
2124  * @retry: number of retries to the command IOCB.
2125  *
2126  * This routine issues a State Change Request (SCR) to a fabric node
2127  * on a @vport. The remote node @nportid is passed into the function. It
2128  * first search the @vport node list to find the matching ndlp. If no such
2129  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2130  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2131  * routine is invoked to send the SCR IOCB.
2132  *
2133  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2134  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2135  * will be stored into the context1 field of the IOCB for the completion
2136  * callback function to the SCR ELS command.
2137  *
2138  * Return code
2139  *   0 - Successfully issued scr command
2140  *   1 - Failed to issue scr command
2141  **/
2142 int
2143 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2144 {
2145         struct lpfc_hba  *phba = vport->phba;
2146         IOCB_t *icmd;
2147         struct lpfc_iocbq *elsiocb;
2148         struct lpfc_sli *psli;
2149         uint8_t *pcmd;
2150         uint16_t cmdsize;
2151         struct lpfc_nodelist *ndlp;
2152
2153         psli = &phba->sli;
2154         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2155
2156         ndlp = lpfc_findnode_did(vport, nportid);
2157         if (!ndlp) {
2158                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2159                 if (!ndlp)
2160                         return 1;
2161                 lpfc_nlp_init(vport, ndlp, nportid);
2162                 lpfc_enqueue_node(vport, ndlp);
2163         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2164                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2165                 if (!ndlp)
2166                         return 1;
2167         }
2168
2169         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2170                                      ndlp->nlp_DID, ELS_CMD_SCR);
2171
2172         if (!elsiocb) {
2173                 /* This will trigger the release of the node just
2174                  * allocated
2175                  */
2176                 lpfc_nlp_put(ndlp);
2177                 return 1;
2178         }
2179
2180         icmd = &elsiocb->iocb;
2181         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2182
2183         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2184         pcmd += sizeof(uint32_t);
2185
2186         /* For SCR, remainder of payload is SCR parameter page */
2187         memset(pcmd, 0, sizeof(SCR));
2188         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2189
2190         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2191                 "Issue SCR:       did:x%x",
2192                 ndlp->nlp_DID, 0, 0);
2193
2194         phba->fc_stat.elsXmitSCR++;
2195         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2196         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2197             IOCB_ERROR) {
2198                 /* The additional lpfc_nlp_put will cause the following
2199                  * lpfc_els_free_iocb routine to trigger the rlease of
2200                  * the node.
2201                  */
2202                 lpfc_nlp_put(ndlp);
2203                 lpfc_els_free_iocb(phba, elsiocb);
2204                 return 1;
2205         }
2206         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2207          * trigger the release of node.
2208          */
2209         lpfc_nlp_put(ndlp);
2210         return 0;
2211 }
2212
2213 /**
2214  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
2215  * @vport: pointer to a host virtual N_Port data structure.
2216  * @nportid: N_Port identifier to the remote node.
2217  * @retry: number of retries to the command IOCB.
2218  *
2219  * This routine issues a Fibre Channel Address Resolution Response
2220  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2221  * is passed into the function. It first search the @vport node list to find
2222  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2223  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2224  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2225  *
2226  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2227  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2228  * will be stored into the context1 field of the IOCB for the completion
2229  * callback function to the PARPR ELS command.
2230  *
2231  * Return code
2232  *   0 - Successfully issued farpr command
2233  *   1 - Failed to issue farpr command
2234  **/
2235 static int
2236 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2237 {
2238         struct lpfc_hba  *phba = vport->phba;
2239         IOCB_t *icmd;
2240         struct lpfc_iocbq *elsiocb;
2241         struct lpfc_sli *psli;
2242         FARP *fp;
2243         uint8_t *pcmd;
2244         uint32_t *lp;
2245         uint16_t cmdsize;
2246         struct lpfc_nodelist *ondlp;
2247         struct lpfc_nodelist *ndlp;
2248
2249         psli = &phba->sli;
2250         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
2251
2252         ndlp = lpfc_findnode_did(vport, nportid);
2253         if (!ndlp) {
2254                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2255                 if (!ndlp)
2256                         return 1;
2257                 lpfc_nlp_init(vport, ndlp, nportid);
2258                 lpfc_enqueue_node(vport, ndlp);
2259         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2260                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2261                 if (!ndlp)
2262                         return 1;
2263         }
2264
2265         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2266                                      ndlp->nlp_DID, ELS_CMD_RNID);
2267         if (!elsiocb) {
2268                 /* This will trigger the release of the node just
2269                  * allocated
2270                  */
2271                 lpfc_nlp_put(ndlp);
2272                 return 1;
2273         }
2274
2275         icmd = &elsiocb->iocb;
2276         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2277
2278         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
2279         pcmd += sizeof(uint32_t);
2280
2281         /* Fill in FARPR payload */
2282         fp = (FARP *) (pcmd);
2283         memset(fp, 0, sizeof(FARP));
2284         lp = (uint32_t *) pcmd;
2285         *lp++ = be32_to_cpu(nportid);
2286         *lp++ = be32_to_cpu(vport->fc_myDID);
2287         fp->Rflags = 0;
2288         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2289
2290         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2291         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2292         ondlp = lpfc_findnode_did(vport, nportid);
2293         if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
2294                 memcpy(&fp->OportName, &ondlp->nlp_portname,
2295                        sizeof(struct lpfc_name));
2296                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
2297                        sizeof(struct lpfc_name));
2298         }
2299
2300         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2301                 "Issue FARPR:     did:x%x",
2302                 ndlp->nlp_DID, 0, 0);
2303
2304         phba->fc_stat.elsXmitFARPR++;
2305         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2306         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2307             IOCB_ERROR) {
2308                 /* The additional lpfc_nlp_put will cause the following
2309                  * lpfc_els_free_iocb routine to trigger the release of
2310                  * the node.
2311                  */
2312                 lpfc_nlp_put(ndlp);
2313                 lpfc_els_free_iocb(phba, elsiocb);
2314                 return 1;
2315         }
2316         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2317          * trigger the release of the node.
2318          */
2319         lpfc_nlp_put(ndlp);
2320         return 0;
2321 }
2322
2323 /**
2324  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
2325  * @vport: pointer to a host virtual N_Port data structure.
2326  * @nlp: pointer to a node-list data structure.
2327  *
2328  * This routine cancels the timer with a delayed IOCB-command retry for
2329  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2330  * removes the ELS retry event if it presents. In addition, if the
2331  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2332  * commands are sent for the @vport's nodes that require issuing discovery
2333  * ADISC.
2334  **/
2335 void
2336 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
2337 {
2338         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2339         struct lpfc_work_evt *evtp;
2340
2341         if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2342                 return;
2343         spin_lock_irq(shost->host_lock);
2344         nlp->nlp_flag &= ~NLP_DELAY_TMO;
2345         spin_unlock_irq(shost->host_lock);
2346         del_timer_sync(&nlp->nlp_delayfunc);
2347         nlp->nlp_last_elscmd = 0;
2348         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
2349                 list_del_init(&nlp->els_retry_evt.evt_listp);
2350                 /* Decrement nlp reference count held for the delayed retry */
2351                 evtp = &nlp->els_retry_evt;
2352                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2353         }
2354         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2355                 spin_lock_irq(shost->host_lock);
2356                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2357                 spin_unlock_irq(shost->host_lock);
2358                 if (vport->num_disc_nodes) {
2359                         if (vport->port_state < LPFC_VPORT_READY) {
2360                                 /* Check if there are more ADISCs to be sent */
2361                                 lpfc_more_adisc(vport);
2362                         } else {
2363                                 /* Check if there are more PLOGIs to be sent */
2364                                 lpfc_more_plogi(vport);
2365                                 if (vport->num_disc_nodes == 0) {
2366                                         spin_lock_irq(shost->host_lock);
2367                                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
2368                                         spin_unlock_irq(shost->host_lock);
2369                                         lpfc_can_disctmo(vport);
2370                                         lpfc_end_rscn(vport);
2371                                 }
2372                         }
2373                 }
2374         }
2375         return;
2376 }
2377
2378 /**
2379  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
2380  * @ptr: holder for the pointer to the timer function associated data (ndlp).
2381  *
2382  * This routine is invoked by the ndlp delayed-function timer to check
2383  * whether there is any pending ELS retry event(s) with the node. If not, it
2384  * simply returns. Otherwise, if there is at least one ELS delayed event, it
2385  * adds the delayed events to the HBA work list and invokes the
2386  * lpfc_worker_wake_up() routine to wake up worker thread to process the
2387  * event. Note that lpfc_nlp_get() is called before posting the event to
2388  * the work list to hold reference count of ndlp so that it guarantees the
2389  * reference to ndlp will still be available when the worker thread gets
2390  * to the event associated with the ndlp.
2391  **/
2392 void
2393 lpfc_els_retry_delay(unsigned long ptr)
2394 {
2395         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2396         struct lpfc_vport *vport = ndlp->vport;
2397         struct lpfc_hba   *phba = vport->phba;
2398         unsigned long flags;
2399         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
2400
2401         spin_lock_irqsave(&phba->hbalock, flags);
2402         if (!list_empty(&evtp->evt_listp)) {
2403                 spin_unlock_irqrestore(&phba->hbalock, flags);
2404                 return;
2405         }
2406
2407         /* We need to hold the node by incrementing the reference
2408          * count until the queued work is done
2409          */
2410         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
2411         if (evtp->evt_arg1) {
2412                 evtp->evt = LPFC_EVT_ELS_RETRY;
2413                 list_add_tail(&evtp->evt_listp, &phba->work_list);
2414                 lpfc_worker_wake_up(phba);
2415         }
2416         spin_unlock_irqrestore(&phba->hbalock, flags);
2417         return;
2418 }
2419
2420 /**
2421  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
2422  * @ndlp: pointer to a node-list data structure.
2423  *
2424  * This routine is the worker-thread handler for processing the @ndlp delayed
2425  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2426  * the last ELS command from the associated ndlp and invokes the proper ELS
2427  * function according to the delayed ELS command to retry the command.
2428  **/
2429 void
2430 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
2431 {
2432         struct lpfc_vport *vport = ndlp->vport;
2433         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2434         uint32_t cmd, did, retry;
2435
2436         spin_lock_irq(shost->host_lock);
2437         did = ndlp->nlp_DID;
2438         cmd = ndlp->nlp_last_elscmd;
2439         ndlp->nlp_last_elscmd = 0;
2440
2441         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2442                 spin_unlock_irq(shost->host_lock);
2443                 return;
2444         }
2445
2446         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2447         spin_unlock_irq(shost->host_lock);
2448         /*
2449          * If a discovery event readded nlp_delayfunc after timer
2450          * firing and before processing the timer, cancel the
2451          * nlp_delayfunc.
2452          */
2453         del_timer_sync(&ndlp->nlp_delayfunc);
2454         retry = ndlp->nlp_retry;
2455         ndlp->nlp_retry = 0;
2456
2457         switch (cmd) {
2458         case ELS_CMD_FLOGI:
2459                 lpfc_issue_els_flogi(vport, ndlp, retry);
2460                 break;
2461         case ELS_CMD_PLOGI:
2462                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
2463                         ndlp->nlp_prev_state = ndlp->nlp_state;
2464                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2465                 }
2466                 break;
2467         case ELS_CMD_ADISC:
2468                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
2469                         ndlp->nlp_prev_state = ndlp->nlp_state;
2470                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2471                 }
2472                 break;
2473         case ELS_CMD_PRLI:
2474                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
2475                         ndlp->nlp_prev_state = ndlp->nlp_state;
2476                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2477                 }
2478                 break;
2479         case ELS_CMD_LOGO:
2480                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
2481                         ndlp->nlp_prev_state = ndlp->nlp_state;
2482                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2483                 }
2484                 break;
2485         case ELS_CMD_FDISC:
2486                 lpfc_issue_els_fdisc(vport, ndlp, retry);
2487                 break;
2488         }
2489         return;
2490 }
2491
2492 /**
2493  * lpfc_els_retry - Make retry decision on an els command iocb
2494  * @phba: pointer to lpfc hba data structure.
2495  * @cmdiocb: pointer to lpfc command iocb data structure.
2496  * @rspiocb: pointer to lpfc response iocb data structure.
2497  *
2498  * This routine makes a retry decision on an ELS command IOCB, which has
2499  * failed. The following ELS IOCBs use this function for retrying the command
2500  * when previously issued command responsed with error status: FLOGI, PLOGI,
2501  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
2502  * returned error status, it makes the decision whether a retry shall be
2503  * issued for the command, and whether a retry shall be made immediately or
2504  * delayed. In the former case, the corresponding ELS command issuing-function
2505  * is called to retry the command. In the later case, the ELS command shall
2506  * be posted to the ndlp delayed event and delayed function timer set to the
2507  * ndlp for the delayed command issusing.
2508  *
2509  * Return code
2510  *   0 - No retry of els command is made
2511  *   1 - Immediate or delayed retry of els command is made
2512  **/
2513 static int
2514 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2515                struct lpfc_iocbq *rspiocb)
2516 {
2517         struct lpfc_vport *vport = cmdiocb->vport;
2518         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2519         IOCB_t *irsp = &rspiocb->iocb;
2520         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2521         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2522         uint32_t *elscmd;
2523         struct ls_rjt stat;
2524         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
2525         int logerr = 0;
2526         uint32_t cmd = 0;
2527         uint32_t did;
2528
2529
2530         /* Note: context2 may be 0 for internal driver abort
2531          * of delays ELS command.
2532          */
2533
2534         if (pcmd && pcmd->virt) {
2535                 elscmd = (uint32_t *) (pcmd->virt);
2536                 cmd = *elscmd++;
2537         }
2538
2539         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2540                 did = ndlp->nlp_DID;
2541         else {
2542                 /* We should only hit this case for retrying PLOGI */
2543                 did = irsp->un.elsreq64.remoteID;
2544                 ndlp = lpfc_findnode_did(vport, did);
2545                 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2546                     && (cmd != ELS_CMD_PLOGI))
2547                         return 1;
2548         }
2549
2550         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2551                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
2552                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
2553
2554         switch (irsp->ulpStatus) {
2555         case IOSTAT_FCP_RSP_ERROR:
2556         case IOSTAT_REMOTE_STOP:
2557                 break;
2558
2559         case IOSTAT_LOCAL_REJECT:
2560                 switch ((irsp->un.ulpWord[4] & 0xff)) {
2561                 case IOERR_LOOP_OPEN_FAILURE:
2562                         if (cmd == ELS_CMD_FLOGI) {
2563                                 if (PCI_DEVICE_ID_HORNET ==
2564                                         phba->pcidev->device) {
2565                                         phba->fc_topology = TOPOLOGY_LOOP;
2566                                         phba->pport->fc_myDID = 0;
2567                                         phba->alpa_map[0] = 0;
2568                                         phba->alpa_map[1] = 0;
2569                                 }
2570                         }
2571                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
2572                                 delay = 1000;
2573                         retry = 1;
2574                         break;
2575
2576                 case IOERR_ILLEGAL_COMMAND:
2577                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2578                                          "0124 Retry illegal cmd x%x "
2579                                          "retry:x%x delay:x%x\n",
2580                                          cmd, cmdiocb->retry, delay);
2581                         retry = 1;
2582                         /* All command's retry policy */
2583                         maxretry = 8;
2584                         if (cmdiocb->retry > 2)
2585                                 delay = 1000;
2586                         break;
2587
2588                 case IOERR_NO_RESOURCES:
2589                         logerr = 1; /* HBA out of resources */
2590                         retry = 1;
2591                         if (cmdiocb->retry > 100)
2592                                 delay = 100;
2593                         maxretry = 250;
2594                         break;
2595
2596                 case IOERR_ILLEGAL_FRAME:
2597                         delay = 100;
2598                         retry = 1;
2599                         break;
2600
2601                 case IOERR_SEQUENCE_TIMEOUT:
2602                 case IOERR_INVALID_RPI:
2603                         retry = 1;
2604                         break;
2605                 }
2606                 break;
2607
2608         case IOSTAT_NPORT_RJT:
2609         case IOSTAT_FABRIC_RJT:
2610                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
2611                         retry = 1;
2612                         break;
2613                 }
2614                 break;
2615
2616         case IOSTAT_NPORT_BSY:
2617         case IOSTAT_FABRIC_BSY:
2618                 logerr = 1; /* Fabric / Remote NPort out of resources */
2619                 retry = 1;
2620                 break;
2621
2622         case IOSTAT_LS_RJT:
2623                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
2624                 /* Added for Vendor specifc support
2625                  * Just keep retrying for these Rsn / Exp codes
2626                  */
2627                 switch (stat.un.b.lsRjtRsnCode) {
2628                 case LSRJT_UNABLE_TPC:
2629                         if (stat.un.b.lsRjtRsnCodeExp ==
2630                             LSEXP_CMD_IN_PROGRESS) {
2631                                 if (cmd == ELS_CMD_PLOGI) {
2632                                         delay = 1000;
2633                                         maxretry = 48;
2634                                 }
2635                                 retry = 1;
2636                                 break;
2637                         }
2638                         if (cmd == ELS_CMD_PLOGI) {
2639                                 delay = 1000;
2640                                 maxretry = lpfc_max_els_tries + 1;
2641                                 retry = 1;
2642                                 break;
2643                         }
2644                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2645                           (cmd == ELS_CMD_FDISC) &&
2646                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
2647                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2648                                                  "0125 FDISC Failed (x%x). "
2649                                                  "Fabric out of resources\n",
2650                                                  stat.un.lsRjtError);
2651                                 lpfc_vport_set_state(vport,
2652                                                      FC_VPORT_NO_FABRIC_RSCS);
2653                         }
2654                         break;
2655
2656                 case LSRJT_LOGICAL_BSY:
2657                         if ((cmd == ELS_CMD_PLOGI) ||
2658                             (cmd == ELS_CMD_PRLI)) {
2659                                 delay = 1000;
2660                                 maxretry = 48;
2661                         } else if (cmd == ELS_CMD_FDISC) {
2662                                 /* FDISC retry policy */
2663                                 maxretry = 48;
2664                                 if (cmdiocb->retry >= 32)
2665                                         delay = 1000;
2666                         }
2667                         retry = 1;
2668                         break;
2669
2670                 case LSRJT_LOGICAL_ERR:
2671                         /* There are some cases where switches return this
2672                          * error when they are not ready and should be returning
2673                          * Logical Busy. We should delay every time.
2674                          */
2675                         if (cmd == ELS_CMD_FDISC &&
2676                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
2677                                 maxretry = 3;
2678                                 delay = 1000;
2679                                 retry = 1;
2680                                 break;
2681                         }
2682                 case LSRJT_PROTOCOL_ERR:
2683                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2684                           (cmd == ELS_CMD_FDISC) &&
2685                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
2686                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
2687                           ) {
2688                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2689                                                  "0122 FDISC Failed (x%x). "
2690                                                  "Fabric Detected Bad WWN\n",
2691                                                  stat.un.lsRjtError);
2692                                 lpfc_vport_set_state(vport,
2693                                                      FC_VPORT_FABRIC_REJ_WWN);
2694                         }
2695                         break;
2696                 }
2697                 break;
2698
2699         case IOSTAT_INTERMED_RSP:
2700         case IOSTAT_BA_RJT:
2701                 break;
2702
2703         default:
2704                 break;
2705         }
2706
2707         if (did == FDMI_DID)
2708                 retry = 1;
2709
2710         if ((cmd == ELS_CMD_FLOGI) &&
2711             (phba->fc_topology != TOPOLOGY_LOOP) &&
2712             !lpfc_error_lost_link(irsp)) {
2713                 /* FLOGI retry policy */
2714                 retry = 1;
2715                 /* retry forever */
2716                 maxretry = 0;
2717                 if (cmdiocb->retry >= 100)
2718                         delay = 5000;
2719                 else if (cmdiocb->retry >= 32)
2720                         delay = 1000;
2721         }
2722
2723         cmdiocb->retry++;
2724         if (maxretry && (cmdiocb->retry >= maxretry)) {
2725                 phba->fc_stat.elsRetryExceeded++;
2726                 retry = 0;
2727         }
2728
2729         if ((vport->load_flag & FC_UNLOADING) != 0)
2730                 retry = 0;
2731
2732         if (retry) {
2733
2734                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
2735                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2736                                  "0107 Retry ELS command x%x to remote "
2737                                  "NPORT x%x Data: x%x x%x\n",
2738                                  cmd, did, cmdiocb->retry, delay);
2739
2740                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
2741                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
2742                         ((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
2743                         /* Don't reset timer for no resources */
2744
2745                         /* If discovery / RSCN timer is running, reset it */
2746                         if (timer_pending(&vport->fc_disctmo) ||
2747                             (vport->fc_flag & FC_RSCN_MODE))
2748                                 lpfc_set_disctmo(vport);
2749                 }
2750
2751                 phba->fc_stat.elsXmitRetry++;
2752                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
2753                         phba->fc_stat.elsDelayRetry++;
2754                         ndlp->nlp_retry = cmdiocb->retry;
2755
2756                         /* delay is specified in milliseconds */
2757                         mod_timer(&ndlp->nlp_delayfunc,
2758                                 jiffies + msecs_to_jiffies(delay));
2759                         spin_lock_irq(shost->host_lock);
2760                         ndlp->nlp_flag |= NLP_DELAY_TMO;
2761                         spin_unlock_irq(shost->host_lock);
2762
2763                         ndlp->nlp_prev_state = ndlp->nlp_state;
2764                         if (cmd == ELS_CMD_PRLI)
2765                                 lpfc_nlp_set_state(vport, ndlp,
2766                                         NLP_STE_REG_LOGIN_ISSUE);
2767                         else
2768                                 lpfc_nlp_set_state(vport, ndlp,
2769                                         NLP_STE_NPR_NODE);
2770                         ndlp->nlp_last_elscmd = cmd;
2771
2772                         return 1;
2773                 }
2774                 switch (cmd) {
2775                 case ELS_CMD_FLOGI:
2776                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2777                         return 1;
2778                 case ELS_CMD_FDISC:
2779                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
2780                         return 1;
2781                 case ELS_CMD_PLOGI:
2782                         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2783                                 ndlp->nlp_prev_state = ndlp->nlp_state;
2784                                 lpfc_nlp_set_state(vport, ndlp,
2785                                                    NLP_STE_PLOGI_ISSUE);
2786                         }
2787                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2788                         return 1;
2789                 case ELS_CMD_ADISC:
2790                         ndlp->nlp_prev_state = ndlp->nlp_state;
2791                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2792                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2793                         return 1;
2794                 case ELS_CMD_PRLI:
2795                         ndlp->nlp_prev_state = ndlp->nlp_state;
2796                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2797                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2798                         return 1;
2799                 case ELS_CMD_LOGO:
2800                         ndlp->nlp_prev_state = ndlp->nlp_state;
2801                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2802                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2803                         return 1;
2804                 }
2805         }
2806         /* No retry ELS command <elsCmd> to remote NPORT <did> */
2807         if (logerr) {
2808                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2809                          "0137 No retry ELS command x%x to remote "
2810                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
2811                          cmd, did, irsp->ulpStatus,
2812                          irsp->un.ulpWord[4]);
2813         }
2814         else {
2815                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2816                          "0108 No retry ELS command x%x to remote "
2817                          "NPORT x%x Retried:%d Error:x%x/%x\n",
2818                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
2819                          irsp->un.ulpWord[4]);
2820         }
2821         return 0;
2822 }
2823
2824 /**
2825  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
2826  * @phba: pointer to lpfc hba data structure.
2827  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
2828  *
2829  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
2830  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
2831  * checks to see whether there is a lpfc DMA buffer associated with the
2832  * response of the command IOCB. If so, it will be released before releasing
2833  * the lpfc DMA buffer associated with the IOCB itself.
2834  *
2835  * Return code
2836  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2837  **/
2838 static int
2839 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
2840 {
2841         struct lpfc_dmabuf *buf_ptr;
2842
2843         /* Free the response before processing the command. */
2844         if (!list_empty(&buf_ptr1->list)) {
2845                 list_remove_head(&buf_ptr1->list, buf_ptr,
2846                                  struct lpfc_dmabuf,
2847                                  list);
2848                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2849                 kfree(buf_ptr);
2850         }
2851         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2852         kfree(buf_ptr1);
2853         return 0;
2854 }
2855
2856 /**
2857  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
2858  * @phba: pointer to lpfc hba data structure.
2859  * @buf_ptr: pointer to the lpfc dma buffer data structure.
2860  *
2861  * This routine releases the lpfc Direct Memory Access (DMA) buffer
2862  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
2863  * pool.
2864  *
2865  * Return code
2866  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2867  **/
2868 static int
2869 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
2870 {
2871         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2872         kfree(buf_ptr);
2873         return 0;
2874 }
2875
2876 /**
2877  * lpfc_els_free_iocb - Free a command iocb and its associated resources
2878  * @phba: pointer to lpfc hba data structure.
2879  * @elsiocb: pointer to lpfc els command iocb data structure.
2880  *
2881  * This routine frees a command IOCB and its associated resources. The
2882  * command IOCB data structure contains the reference to various associated
2883  * resources, these fields must be set to NULL if the associated reference
2884  * not present:
2885  *   context1 - reference to ndlp
2886  *   context2 - reference to cmd
2887  *   context2->next - reference to rsp
2888  *   context3 - reference to bpl
2889  *
2890  * It first properly decrements the reference count held on ndlp for the
2891  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
2892  * set, it invokes the lpfc_els_free_data() routine to release the Direct
2893  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
2894  * adds the DMA buffer the @phba data structure for the delayed release.
2895  * If reference to the Buffer Pointer List (BPL) is present, the
2896  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
2897  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
2898  * invoked to release the IOCB data structure back to @phba IOCBQ list.
2899  *
2900  * Return code
2901  *   0 - Success (currently, always return 0)
2902  **/
2903 int
2904 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
2905 {
2906         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2907         struct lpfc_nodelist *ndlp;
2908
2909         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
2910         if (ndlp) {
2911                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
2912                         lpfc_nlp_put(ndlp);
2913
2914                         /* If the ndlp is not being used by another discovery
2915                          * thread, free it.
2916                          */
2917                         if (!lpfc_nlp_not_used(ndlp)) {
2918                                 /* If ndlp is being used by another discovery
2919                                  * thread, just clear NLP_DEFER_RM
2920                                  */
2921                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
2922                         }
2923                 }
2924                 else
2925                         lpfc_nlp_put(ndlp);
2926                 elsiocb->context1 = NULL;
2927         }
2928         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
2929         if (elsiocb->context2) {
2930                 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
2931                         /* Firmware could still be in progress of DMAing
2932                          * payload, so don't free data buffer till after
2933                          * a hbeat.
2934                          */
2935                         elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
2936                         buf_ptr = elsiocb->context2;
2937                         elsiocb->context2 = NULL;
2938                         if (buf_ptr) {
2939                                 buf_ptr1 = NULL;
2940                                 spin_lock_irq(&phba->hbalock);
2941                                 if (!list_empty(&buf_ptr->list)) {
2942                                         list_remove_head(&buf_ptr->list,
2943                                                 buf_ptr1, struct lpfc_dmabuf,
2944                                                 list);
2945                                         INIT_LIST_HEAD(&buf_ptr1->list);
2946                                         list_add_tail(&buf_ptr1->list,
2947                                                 &phba->elsbuf);
2948                                         phba->elsbuf_cnt++;
2949                                 }
2950                                 INIT_LIST_HEAD(&buf_ptr->list);
2951                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
2952                                 phba->elsbuf_cnt++;
2953                                 spin_unlock_irq(&phba->hbalock);
2954                         }
2955                 } else {
2956                         buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
2957                         lpfc_els_free_data(phba, buf_ptr1);
2958                 }
2959         }
2960
2961         if (elsiocb->context3) {
2962                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2963                 lpfc_els_free_bpl(phba, buf_ptr);
2964         }
2965         lpfc_sli_release_iocbq(phba, elsiocb);
2966         return 0;
2967 }
2968
2969 /**
2970  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
2971  * @phba: pointer to lpfc hba data structure.
2972  * @cmdiocb: pointer to lpfc command iocb data structure.
2973  * @rspiocb: pointer to lpfc response iocb data structure.
2974  *
2975  * This routine is the completion callback function to the Logout (LOGO)
2976  * Accept (ACC) Response ELS command. This routine is invoked to indicate
2977  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
2978  * release the ndlp if it has the last reference remaining (reference count
2979  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
2980  * field to NULL to inform the following lpfc_els_free_iocb() routine no
2981  * ndlp reference count needs to be decremented. Otherwise, the ndlp
2982  * reference use-count shall be decremented by the lpfc_els_free_iocb()
2983  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
2984  * IOCB data structure.
2985  **/
2986 static void
2987 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2988                        struct lpfc_iocbq *rspiocb)
2989 {
2990         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2991         struct lpfc_vport *vport = cmdiocb->vport;
2992         IOCB_t *irsp;
2993
2994         irsp = &rspiocb->iocb;
2995         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2996                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
2997                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
2998         /* ACC to LOGO completes to NPort <nlp_DID> */
2999         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3000                          "0109 ACC to LOGO completes to NPort x%x "
3001                          "Data: x%x x%x x%x\n",
3002                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3003                          ndlp->nlp_rpi);
3004
3005         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
3006                 /* NPort Recovery mode or node is just allocated */
3007                 if (!lpfc_nlp_not_used(ndlp)) {
3008                         /* If the ndlp is being used by another discovery
3009                          * thread, just unregister the RPI.
3010                          */
3011                         lpfc_unreg_rpi(vport, ndlp);
3012                 } else {
3013                         /* Indicate the node has already released, should
3014                          * not reference to it from within lpfc_els_free_iocb.
3015                          */
3016                         cmdiocb->context1 = NULL;
3017                 }
3018         }
3019         lpfc_els_free_iocb(phba, cmdiocb);
3020         return;
3021 }
3022
3023 /**
3024  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
3025  * @phba: pointer to lpfc hba data structure.
3026  * @pmb: pointer to the driver internal queue element for mailbox command.
3027  *
3028  * This routine is the completion callback function for unregister default
3029  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
3030  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
3031  * decrements the ndlp reference count held for this completion callback
3032  * function. After that, it invokes the lpfc_nlp_not_used() to check
3033  * whether there is only one reference left on the ndlp. If so, it will
3034  * perform one more decrement and trigger the release of the ndlp.
3035  **/
3036 void
3037 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3038 {
3039         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3040         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3041
3042         /*
3043          * This routine is used to register and unregister in previous SLI
3044          * modes.
3045          */
3046         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
3047             (phba->sli_rev == LPFC_SLI_REV4))
3048                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
3049
3050         pmb->context1 = NULL;
3051         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3052         kfree(mp);
3053         mempool_free(pmb, phba->mbox_mem_pool);
3054         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3055                 lpfc_nlp_put(ndlp);
3056                 /* This is the end of the default RPI cleanup logic for this
3057                  * ndlp. If no other discovery threads are using this ndlp.
3058                  * we should free all resources associated with it.
3059                  */
3060                 lpfc_nlp_not_used(ndlp);
3061         }
3062
3063         return;
3064 }
3065
3066 /**
3067  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
3068  * @phba: pointer to lpfc hba data structure.
3069  * @cmdiocb: pointer to lpfc command iocb data structure.
3070  * @rspiocb: pointer to lpfc response iocb data structure.
3071  *
3072  * This routine is the completion callback function for ELS Response IOCB
3073  * command. In normal case, this callback function just properly sets the
3074  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
3075  * field in the command IOCB is not NULL, the referred mailbox command will
3076  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
3077  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
3078  * link down event occurred during the discovery, the lpfc_nlp_not_used()
3079  * routine shall be invoked trying to release the ndlp if no other threads
3080  * are currently referring it.
3081  **/
3082 static void
3083 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3084                   struct lpfc_iocbq *rspiocb)
3085 {
3086         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3087         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
3088         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
3089         IOCB_t  *irsp;
3090         uint8_t *pcmd;
3091         LPFC_MBOXQ_t *mbox = NULL;
3092         struct lpfc_dmabuf *mp = NULL;
3093         uint32_t ls_rjt = 0;
3094
3095         irsp = &rspiocb->iocb;
3096
3097         if (cmdiocb->context_un.mbox)
3098                 mbox = cmdiocb->context_un.mbox;
3099
3100         /* First determine if this is a LS_RJT cmpl. Note, this callback
3101          * function can have cmdiocb->contest1 (ndlp) field set to NULL.
3102          */
3103         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
3104         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3105             (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
3106                 /* A LS_RJT associated with Default RPI cleanup has its own
3107                  * seperate code path.
3108                  */
3109                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3110                         ls_rjt = 1;
3111         }
3112
3113         /* Check to see if link went down during discovery */
3114         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
3115                 if (mbox) {
3116                         mp = (struct lpfc_dmabuf *) mbox->context1;
3117                         if (mp) {
3118                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3119                                 kfree(mp);
3120                         }
3121                         mempool_free(mbox, phba->mbox_mem_pool);
3122                 }
3123                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3124                     (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3125                         if (lpfc_nlp_not_used(ndlp)) {
3126                                 ndlp = NULL;
3127                                 /* Indicate the node has already released,
3128                                  * should not reference to it from within
3129                                  * the routine lpfc_els_free_iocb.
3130                                  */
3131                                 cmdiocb->context1 = NULL;
3132                         }
3133                 goto out;
3134         }
3135
3136         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3137                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
3138                 irsp->ulpStatus, irsp->un.ulpWord[4],
3139                 cmdiocb->iocb.un.elsreq64.remoteID);
3140         /* ELS response tag <ulpIoTag> completes */
3141         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3142                          "0110 ELS response tag x%x completes "
3143                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3144                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3145                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3146                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3147                          ndlp->nlp_rpi);
3148         if (mbox) {
3149                 if ((rspiocb->iocb.ulpStatus == 0)
3150                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
3151                         lpfc_unreg_rpi(vport, ndlp);
3152                         /* Increment reference count to ndlp to hold the
3153                          * reference to ndlp for the callback function.
3154                          */
3155                         mbox->context2 = lpfc_nlp_get(ndlp);
3156                         mbox->vport = vport;
3157                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3158                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3159                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3160                         }
3161                         else {
3162                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3163                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3164                                 lpfc_nlp_set_state(vport, ndlp,
3165                                            NLP_STE_REG_LOGIN_ISSUE);
3166                         }
3167                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
3168                             != MBX_NOT_FINISHED)
3169                                 goto out;
3170                         else
3171                                 /* Decrement the ndlp reference count we
3172                                  * set for this failed mailbox command.
3173                                  */
3174                                 lpfc_nlp_put(ndlp);
3175
3176                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
3177                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3178                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
3179                                 "Data: x%x x%x x%x\n",
3180                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3181                                 ndlp->nlp_rpi);
3182
3183                         if (lpfc_nlp_not_used(ndlp)) {
3184                                 ndlp = NULL;
3185                                 /* Indicate node has already been released,
3186                                  * should not reference to it from within
3187                                  * the routine lpfc_els_free_iocb.
3188                                  */
3189                                 cmdiocb->context1 = NULL;
3190                         }
3191                 } else {
3192                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
3193                         if (!lpfc_error_lost_link(irsp) &&
3194                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
3195                                 if (lpfc_nlp_not_used(ndlp)) {
3196                                         ndlp = NULL;
3197                                         /* Indicate node has already been
3198                                          * released, should not reference
3199                                          * to it from within the routine
3200                                          * lpfc_els_free_iocb.
3201                                          */
3202                                         cmdiocb->context1 = NULL;
3203                                 }
3204                         }
3205                 }
3206                 mp = (struct lpfc_dmabuf *) mbox->context1;
3207                 if (mp) {
3208                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3209                         kfree(mp);
3210                 }
3211                 mempool_free(mbox, phba->mbox_mem_pool);
3212         }
3213 out:
3214         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3215                 spin_lock_irq(shost->host_lock);
3216                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
3217                 spin_unlock_irq(shost->host_lock);
3218
3219                 /* If the node is not being used by another discovery thread,
3220                  * and we are sending a reject, we are done with it.
3221                  * Release driver reference count here and free associated
3222                  * resources.
3223                  */
3224                 if (ls_rjt)
3225                         if (lpfc_nlp_not_used(ndlp))
3226                                 /* Indicate node has already been released,
3227                                  * should not reference to it from within
3228                                  * the routine lpfc_els_free_iocb.
3229                                  */
3230                                 cmdiocb->context1 = NULL;
3231         }
3232
3233         lpfc_els_free_iocb(phba, cmdiocb);
3234         return;
3235 }
3236
3237 /**
3238  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
3239  * @vport: pointer to a host virtual N_Port data structure.
3240  * @flag: the els command code to be accepted.
3241  * @oldiocb: pointer to the original lpfc command iocb data structure.
3242  * @ndlp: pointer to a node-list data structure.
3243  * @mbox: pointer to the driver internal queue element for mailbox command.
3244  *
3245  * This routine prepares and issues an Accept (ACC) response IOCB
3246  * command. It uses the @flag to properly set up the IOCB field for the
3247  * specific ACC response command to be issued and invokes the
3248  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3249  * @mbox pointer is passed in, it will be put into the context_un.mbox
3250  * field of the IOCB for the completion callback function to issue the
3251  * mailbox command to the HBA later when callback is invoked.
3252  *
3253  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3254  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3255  * will be stored into the context1 field of the IOCB for the completion
3256  * callback function to the corresponding response ELS IOCB command.
3257  *
3258  * Return code
3259  *   0 - Successfully issued acc response
3260  *   1 - Failed to issue acc response
3261  **/
3262 int
3263 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3264                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3265                  LPFC_MBOXQ_t *mbox)
3266 {
3267         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3268         struct lpfc_hba  *phba = vport->phba;
3269         IOCB_t *icmd;
3270         IOCB_t *oldcmd;
3271         struct lpfc_iocbq *elsiocb;
3272         struct lpfc_sli *psli;
3273         uint8_t *pcmd;
3274         uint16_t cmdsize;
3275         int rc;
3276         ELS_PKT *els_pkt_ptr;
3277
3278         psli = &phba->sli;
3279         oldcmd = &oldiocb->iocb;
3280
3281         switch (flag) {
3282         case ELS_CMD_ACC:
3283                 cmdsize = sizeof(uint32_t);
3284                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3285                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3286                 if (!elsiocb) {
3287                         spin_lock_irq(shost->host_lock);
3288                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3289                         spin_unlock_irq(shost->host_lock);
3290                         return 1;
3291                 }
3292
3293                 icmd = &elsiocb->iocb;
3294                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3295                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3296                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3297                 pcmd += sizeof(uint32_t);
3298
3299                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3300                         "Issue ACC:       did:x%x flg:x%x",
3301                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3302                 break;
3303         case ELS_CMD_PLOGI:
3304                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
3305                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3306                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3307                 if (!elsiocb)
3308                         return 1;
3309
3310                 icmd = &elsiocb->iocb;
3311                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3312                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3313
3314                 if (mbox)
3315                         elsiocb->context_un.mbox = mbox;
3316
3317                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3318                 pcmd += sizeof(uint32_t);
3319                 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
3320
3321                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3322                         "Issue ACC PLOGI: did:x%x flg:x%x",
3323                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3324                 break;
3325         case ELS_CMD_PRLO:
3326                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
3327                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3328                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3329                 if (!elsiocb)
3330                         return 1;
3331
3332                 icmd = &elsiocb->iocb;
3333                 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
3334                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3335
3336                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
3337                        sizeof(uint32_t) + sizeof(PRLO));
3338                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3339                 els_pkt_ptr = (ELS_PKT *) pcmd;
3340                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
3341
3342                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3343                         "Issue ACC PRLO:  did:x%x flg:x%x",
3344                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3345                 break;
3346         default:
3347                 return 1;
3348         }
3349         /* Xmit ELS ACC response tag <ulpIoTag> */
3350         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3351                          "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
3352                          "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
3353                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3354                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3355                          ndlp->nlp_rpi);
3356         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
3357                 spin_lock_irq(shost->host_lock);
3358                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3359                 spin_unlock_irq(shost->host_lock);
3360                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
3361         } else {
3362                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3363         }
3364
3365         phba->fc_stat.elsXmitACC++;
3366         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3367         if (rc == IOCB_ERROR) {
3368                 lpfc_els_free_iocb(phba, elsiocb);
3369                 return 1;
3370         }
3371         return 0;
3372 }
3373
3374 /**
3375  * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
3376  * @vport: pointer to a virtual N_Port data structure.
3377  * @rejectError:
3378  * @oldiocb: pointer to the original lpfc command iocb data structure.
3379  * @ndlp: pointer to a node-list data structure.
3380  * @mbox: pointer to the driver internal queue element for mailbox command.
3381  *
3382  * This routine prepares and issue an Reject (RJT) response IOCB
3383  * command. If a @mbox pointer is passed in, it will be put into the
3384  * context_un.mbox field of the IOCB for the completion callback function
3385  * to issue to the HBA later.
3386  *
3387  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3388  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3389  * will be stored into the context1 field of the IOCB for the completion
3390  * callback function to the reject response ELS IOCB command.
3391  *
3392  * Return code
3393  *   0 - Successfully issued reject response
3394  *   1 - Failed to issue reject response
3395  **/
3396 int
3397 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
3398                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3399                     LPFC_MBOXQ_t *mbox)
3400 {
3401         struct lpfc_hba  *phba = vport->phba;
3402         IOCB_t *icmd;
3403         IOCB_t *oldcmd;
3404         struct lpfc_iocbq *elsiocb;
3405         struct lpfc_sli *psli;
3406         uint8_t *pcmd;
3407         uint16_t cmdsize;
3408         int rc;
3409
3410         psli = &phba->sli;
3411         cmdsize = 2 * sizeof(uint32_t);
3412         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3413                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
3414         if (!elsiocb)
3415                 return 1;
3416
3417         icmd = &elsiocb->iocb;
3418         oldcmd = &oldiocb->iocb;
3419         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3420         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3421
3422         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
3423         pcmd += sizeof(uint32_t);
3424         *((uint32_t *) (pcmd)) = rejectError;
3425
3426         if (mbox)
3427                 elsiocb->context_un.mbox = mbox;
3428
3429         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
3430         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3431                          "0129 Xmit ELS RJT x%x response tag x%x "
3432                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3433                          "rpi x%x\n",
3434                          rejectError, elsiocb->iotag,
3435                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
3436                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
3437         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3438                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
3439                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
3440
3441         phba->fc_stat.elsXmitLSRJT++;
3442         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3443         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3444
3445         if (rc == IOCB_ERROR) {
3446                 lpfc_els_free_iocb(phba, elsiocb);
3447                 return 1;
3448         }
3449         return 0;
3450 }
3451
3452 /**
3453  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
3454  * @vport: pointer to a virtual N_Port data structure.
3455  * @oldiocb: pointer to the original lpfc command iocb data structure.
3456  * @ndlp: pointer to a node-list data structure.
3457  *
3458  * This routine prepares and issues an Accept (ACC) response to Address
3459  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
3460  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3461  *
3462  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3463  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3464  * will be stored into the context1 field of the IOCB for the completion
3465  * callback function to the ADISC Accept response ELS IOCB command.
3466  *
3467  * Return code
3468  *   0 - Successfully issued acc adisc response
3469  *   1 - Failed to issue adisc acc response
3470  **/
3471 int
3472 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3473                        struct lpfc_nodelist *ndlp)
3474 {
3475         struct lpfc_hba  *phba = vport->phba;
3476         ADISC *ap;
3477         IOCB_t *icmd, *oldcmd;
3478         struct lpfc_iocbq *elsiocb;
3479         uint8_t *pcmd;
3480         uint16_t cmdsize;
3481         int rc;
3482
3483         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
3484         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3485                                      ndlp->nlp_DID, ELS_CMD_ACC);
3486         if (!elsiocb)
3487                 return 1;
3488
3489         icmd = &elsiocb->iocb;
3490         oldcmd = &oldiocb->iocb;
3491         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3492
3493         /* Xmit ADISC ACC response tag <ulpIoTag> */
3494         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3495                          "0130 Xmit ADISC ACC response iotag x%x xri: "
3496                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
3497                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3498                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3499                          ndlp->nlp_rpi);
3500         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3501
3502         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3503         pcmd += sizeof(uint32_t);
3504
3505         ap = (ADISC *) (pcmd);
3506         ap->hardAL_PA = phba->fc_pref_ALPA;
3507         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3508         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3509         ap->DID = be32_to_cpu(vport->fc_myDID);
3510
3511         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3512                 "Issue ACC ADISC: did:x%x flg:x%x",
3513                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3514
3515         phba->fc_stat.elsXmitACC++;
3516         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3517         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3518         if (rc == IOCB_ERROR) {
3519                 lpfc_els_free_iocb(phba, elsiocb);
3520                 return 1;
3521         }
3522         return 0;
3523 }
3524
3525 /**
3526  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
3527  * @vport: pointer to a virtual N_Port data structure.
3528  * @oldiocb: pointer to the original lpfc command iocb data structure.
3529  * @ndlp: pointer to a node-list data structure.
3530  *
3531  * This routine prepares and issues an Accept (ACC) response to Process
3532  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
3533  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3534  *
3535  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3536  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3537  * will be stored into the context1 field of the IOCB for the completion
3538  * callback function to the PRLI Accept response ELS IOCB command.
3539  *
3540  * Return code
3541  *   0 - Successfully issued acc prli response
3542  *   1 - Failed to issue acc prli response
3543  **/
3544 int
3545 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3546                       struct lpfc_nodelist *ndlp)
3547 {
3548         struct lpfc_hba  *phba = vport->phba;
3549         PRLI *npr;
3550         lpfc_vpd_t *vpd;
3551         IOCB_t *icmd;
3552         IOCB_t *oldcmd;
3553         struct lpfc_iocbq *elsiocb;
3554         struct lpfc_sli *psli;
3555         uint8_t *pcmd;
3556         uint16_t cmdsize;
3557         int rc;
3558
3559         psli = &phba->sli;
3560
3561         cmdsize = sizeof(uint32_t) + sizeof(PRLI);
3562         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3563                 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
3564         if (!elsiocb)
3565                 return 1;
3566
3567         icmd = &elsiocb->iocb;
3568         oldcmd = &oldiocb->iocb;
3569         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3570         /* Xmit PRLI ACC response tag <ulpIoTag> */
3571         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3572                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
3573                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3574                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3575                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3576                          ndlp->nlp_rpi);
3577         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3578
3579         *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
3580         pcmd += sizeof(uint32_t);
3581
3582         /* For PRLI, remainder of payload is PRLI parameter page */
3583         memset(pcmd, 0, sizeof(PRLI));
3584
3585         npr = (PRLI *) pcmd;
3586         vpd = &phba->vpd;
3587         /*
3588          * If the remote port is a target and our firmware version is 3.20 or
3589          * later, set the following bits for FC-TAPE support.
3590          */
3591         if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3592             (vpd->rev.feaLevelHigh >= 0x02)) {
3593                 npr->ConfmComplAllowed = 1;
3594                 npr->Retry = 1;
3595                 npr->TaskRetryIdReq = 1;
3596         }
3597
3598         npr->acceptRspCode = PRLI_REQ_EXECUTED;
3599         npr->estabImagePair = 1;
3600         npr->readXferRdyDis = 1;
3601         npr->ConfmComplAllowed = 1;
3602
3603         npr->prliType = PRLI_FCP_TYPE;
3604         npr->initiatorFunc = 1;
3605
3606         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3607                 "Issue ACC PRLI:  did:x%x flg:x%x",
3608                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3609
3610         phba->fc_stat.elsXmitACC++;
3611         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3612
3613         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3614         if (rc == IOCB_ERROR) {
3615                 lpfc_els_free_iocb(phba, elsiocb);
3616                 return 1;
3617         }
3618         return 0;
3619 }
3620
3621 /**
3622  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
3623  * @vport: pointer to a virtual N_Port data structure.
3624  * @format: rnid command format.
3625  * @oldiocb: pointer to the original lpfc command iocb data structure.
3626  * @ndlp: pointer to a node-list data structure.
3627  *
3628  * This routine issues a Request Node Identification Data (RNID) Accept
3629  * (ACC) response. It constructs the RNID ACC response command according to
3630  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
3631  * issue the response. Note that this command does not need to hold the ndlp
3632  * reference count for the callback. So, the ndlp reference count taken by
3633  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
3634  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
3635  * there is no ndlp reference available.
3636  *
3637  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3638  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3639  * will be stored into the context1 field of the IOCB for the completion
3640  * callback function. However, for the RNID Accept Response ELS command,
3641  * this is undone later by this routine after the IOCB is allocated.
3642  *
3643  * Return code
3644  *   0 - Successfully issued acc rnid response
3645  *   1 - Failed to issue acc rnid response
3646  **/
3647 static int
3648 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
3649                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3650 {
3651         struct lpfc_hba  *phba = vport->phba;
3652         RNID *rn;
3653         IOCB_t *icmd, *oldcmd;
3654         struct lpfc_iocbq *elsiocb;
3655         struct lpfc_sli *psli;
3656         uint8_t *pcmd;
3657         uint16_t cmdsize;
3658         int rc;
3659
3660         psli = &phba->sli;
3661         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
3662                                         + (2 * sizeof(struct lpfc_name));
3663         if (format)
3664                 cmdsize += sizeof(RNID_TOP_DISC);
3665
3666         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3667                                      ndlp->nlp_DID, ELS_CMD_ACC);
3668         if (!elsiocb)
3669                 return 1;
3670
3671         icmd = &elsiocb->iocb;
3672         oldcmd = &oldiocb->iocb;
3673         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3674         /* Xmit RNID ACC response tag <ulpIoTag> */
3675         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3676                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
3677                          elsiocb->iotag, elsiocb->iocb.ulpContext);
3678         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3679         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3680         pcmd += sizeof(uint32_t);
3681
3682         memset(pcmd, 0, sizeof(RNID));
3683         rn = (RNID *) (pcmd);
3684         rn->Format = format;
3685         rn->CommonLen = (2 * sizeof(struct lpfc_name));
3686         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3687         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3688         switch (format) {
3689         case 0:
3690                 rn->SpecificLen = 0;
3691                 break;
3692         case RNID_TOPOLOGY_DISC:
3693                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
3694                 memcpy(&rn->un.topologyDisc.portName,
3695                        &vport->fc_portname, sizeof(struct lpfc_name));
3696                 rn->un.topologyDisc.unitType = RNID_HBA;
3697                 rn->un.topologyDisc.physPort = 0;
3698                 rn->un.topologyDisc.attachedNodes = 0;
3699                 break;
3700         default:
3701                 rn->CommonLen = 0;
3702                 rn->SpecificLen = 0;
3703                 break;
3704         }
3705
3706         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3707                 "Issue ACC RNID:  did:x%x flg:x%x",
3708                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3709
3710         phba->fc_stat.elsXmitACC++;
3711         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3712         lpfc_nlp_put(ndlp);
3713         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
3714                                     * it could be freed */
3715
3716         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3717         if (rc == IOCB_ERROR) {
3718                 lpfc_els_free_iocb(phba, elsiocb);
3719                 return 1;
3720         }
3721         return 0;
3722 }
3723
3724 /**
3725  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
3726  * @vport: pointer to a host virtual N_Port data structure.
3727  *
3728  * This routine issues Address Discover (ADISC) ELS commands to those
3729  * N_Ports which are in node port recovery state and ADISC has not been issued
3730  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
3731  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
3732  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
3733  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
3734  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
3735  * IOCBs quit for later pick up. On the other hand, after walking through
3736  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
3737  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
3738  * no more ADISC need to be sent.
3739  *
3740  * Return code
3741  *    The number of N_Ports with adisc issued.
3742  **/
3743 int
3744 lpfc_els_disc_adisc(struct lpfc_vport *vport)
3745 {
3746         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3747         struct lpfc_nodelist *ndlp, *next_ndlp;
3748         int sentadisc = 0;
3749
3750         /* go thru NPR nodes and issue any remaining ELS ADISCs */
3751         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3752                 if (!NLP_CHK_NODE_ACT(ndlp))
3753                         continue;
3754                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3755                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3756                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
3757                         spin_lock_irq(shost->host_lock);
3758                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3759                         spin_unlock_irq(shost->host_lock);
3760                         ndlp->nlp_prev_state = ndlp->nlp_state;
3761                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3762                         lpfc_issue_els_adisc(vport, ndlp, 0);
3763                         sentadisc++;
3764                         vport->num_disc_nodes++;
3765                         if (vport->num_disc_nodes >=
3766                             vport->cfg_discovery_threads) {
3767                                 spin_lock_irq(shost->host_lock);
3768                                 vport->fc_flag |= FC_NLP_MORE;
3769                                 spin_unlock_irq(shost->host_lock);
3770                                 break;
3771                         }
3772                 }
3773         }
3774         if (sentadisc == 0) {
3775                 spin_lock_irq(shost->host_lock);
3776                 vport->fc_flag &= ~FC_NLP_MORE;
3777                 spin_unlock_irq(shost->host_lock);
3778         }
3779         return sentadisc;
3780 }
3781
3782 /**
3783  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
3784  * @vport: pointer to a host virtual N_Port data structure.
3785  *
3786  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
3787  * which are in node port recovery state, with a @vport. Each time an ELS
3788  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
3789  * the per @vport number of discover count (num_disc_nodes) shall be
3790  * incremented. If the num_disc_nodes reaches a pre-configured threshold
3791  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
3792  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
3793  * later pick up. On the other hand, after walking through all the ndlps with
3794  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
3795  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
3796  * PLOGI need to be sent.
3797  *
3798  * Return code
3799  *   The number of N_Ports with plogi issued.
3800  **/
3801 int
3802 lpfc_els_disc_plogi(struct lpfc_vport *vport)
3803 {
3804         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3805         struct lpfc_nodelist *ndlp, *next_ndlp;
3806         int sentplogi = 0;
3807
3808         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
3809         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3810                 if (!NLP_CHK_NODE_ACT(ndlp))
3811                         continue;
3812                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3813                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3814                     (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
3815                     (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
3816                         ndlp->nlp_prev_state = ndlp->nlp_state;
3817                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3818                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
3819                         sentplogi++;
3820                         vport->num_disc_nodes++;
3821                         if (vport->num_disc_nodes >=
3822                             vport->cfg_discovery_threads) {
3823                                 spin_lock_irq(shost->host_lock);
3824                                 vport->fc_flag |= FC_NLP_MORE;
3825                                 spin_unlock_irq(shost->host_lock);
3826                                 break;
3827                         }
3828                 }
3829         }
3830         if (sentplogi) {
3831                 lpfc_set_disctmo(vport);
3832         }
3833         else {
3834                 spin_lock_irq(shost->host_lock);
3835                 vport->fc_flag &= ~FC_NLP_MORE;
3836                 spin_unlock_irq(shost->host_lock);
3837         }
3838         return sentplogi;
3839 }
3840
3841 /**
3842  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
3843  * @vport: pointer to a host virtual N_Port data structure.
3844  *
3845  * This routine cleans up any Registration State Change Notification
3846  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
3847  * @vport together with the host_lock is used to prevent multiple thread
3848  * trying to access the RSCN array on a same @vport at the same time.
3849  **/
3850 void
3851 lpfc_els_flush_rscn(struct lpfc_vport *vport)
3852 {
3853         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3854         struct lpfc_hba  *phba = vport->phba;
3855         int i;
3856
3857         spin_lock_irq(shost->host_lock);
3858         if (vport->fc_rscn_flush) {
3859                 /* Another thread is walking fc_rscn_id_list on this vport */
3860                 spin_unlock_irq(shost->host_lock);
3861                 return;
3862         }
3863         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
3864         vport->fc_rscn_flush = 1;
3865         spin_unlock_irq(shost->host_lock);
3866
3867         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
3868                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
3869                 vport->fc_rscn_id_list[i] = NULL;
3870         }
3871         spin_lock_irq(shost->host_lock);
3872         vport->fc_rscn_id_cnt = 0;
3873         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
3874         spin_unlock_irq(shost->host_lock);
3875         lpfc_can_disctmo(vport);
3876         /* Indicate we are done walking this fc_rscn_id_list */
3877         vport->fc_rscn_flush = 0;
3878 }
3879
3880 /**
3881  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
3882  * @vport: pointer to a host virtual N_Port data structure.
3883  * @did: remote destination port identifier.
3884  *
3885  * This routine checks whether there is any pending Registration State
3886  * Configuration Notification (RSCN) to a @did on @vport.
3887  *
3888  * Return code
3889  *   None zero - The @did matched with a pending rscn
3890  *   0 - not able to match @did with a pending rscn
3891  **/
3892 int
3893 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
3894 {
3895         D_ID ns_did;
3896         D_ID rscn_did;
3897         uint32_t *lp;
3898         uint32_t payload_len, i;
3899         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3900
3901         ns_did.un.word = did;
3902
3903         /* Never match fabric nodes for RSCNs */
3904         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
3905                 return 0;
3906
3907         /* If we are doing a FULL RSCN rediscovery, match everything */
3908         if (vport->fc_flag & FC_RSCN_DISCOVERY)
3909                 return did;
3910
3911         spin_lock_irq(shost->host_lock);
3912         if (vport->fc_rscn_flush) {
3913                 /* Another thread is walking fc_rscn_id_list on this vport */
3914                 spin_unlock_irq(shost->host_lock);
3915                 return 0;
3916         }
3917         /* Indicate we are walking fc_rscn_id_list on this vport */
3918         vport->fc_rscn_flush = 1;
3919         spin_unlock_irq(shost->host_lock);
3920         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
3921                 lp = vport->fc_rscn_id_list[i]->virt;
3922                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
3923                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
3924                 while (payload_len) {
3925                         rscn_did.un.word = be32_to_cpu(*lp++);
3926                         payload_len -= sizeof(uint32_t);
3927                         switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
3928                         case RSCN_ADDRESS_FORMAT_PORT:
3929                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
3930                                     && (ns_did.un.b.area == rscn_did.un.b.area)
3931                                     && (ns_did.un.b.id == rscn_did.un.b.id))
3932                                         goto return_did_out;
3933                                 break;
3934                         case RSCN_ADDRESS_FORMAT_AREA:
3935                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
3936                                     && (ns_did.un.b.area == rscn_did.un.b.area))
3937                                         goto return_did_out;
3938                                 break;
3939                         case RSCN_ADDRESS_FORMAT_DOMAIN:
3940                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
3941                                         goto return_did_out;
3942                                 break;
3943                         case RSCN_ADDRESS_FORMAT_FABRIC:
3944                                 goto return_did_out;
3945                         }
3946                 }
3947         }
3948         /* Indicate we are done with walking fc_rscn_id_list on this vport */
3949         vport->fc_rscn_flush = 0;
3950         return 0;
3951 return_did_out:
3952         /* Indicate we are done with walking fc_rscn_id_list on this vport */
3953         vport->fc_rscn_flush = 0;
3954         return did;
3955 }
3956
3957 /**
3958  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
3959  * @vport: pointer to a host virtual N_Port data structure.
3960  *
3961  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
3962  * state machine for a @vport's nodes that are with pending RSCN (Registration
3963  * State Change Notification).
3964  *
3965  * Return code
3966  *   0 - Successful (currently alway return 0)
3967  **/
3968 static int
3969 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
3970 {
3971         struct lpfc_nodelist *ndlp = NULL;
3972
3973         /* Move all affected nodes by pending RSCNs to NPR state. */
3974         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3975                 if (!NLP_CHK_NODE_ACT(ndlp) ||
3976                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
3977                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
3978                         continue;
3979                 lpfc_disc_state_machine(vport, ndlp, NULL,
3980                                         NLP_EVT_DEVICE_RECOVERY);
3981                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
3982         }
3983         return 0;
3984 }
3985
3986 /**
3987  * lpfc_send_rscn_event - Send an RSCN event to management application
3988  * @vport: pointer to a host virtual N_Port data structure.
3989  * @cmdiocb: pointer to lpfc command iocb data structure.
3990  *
3991  * lpfc_send_rscn_event sends an RSCN netlink event to management
3992  * applications.
3993  */
3994 static void
3995 lpfc_send_rscn_event(struct lpfc_vport *vport,
3996                 struct lpfc_iocbq *cmdiocb)
3997 {
3998         struct lpfc_dmabuf *pcmd;
3999         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4000         uint32_t *payload_ptr;
4001         uint32_t payload_len;
4002         struct lpfc_rscn_event_header *rscn_event_data;
4003
4004         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4005         payload_ptr = (uint32_t *) pcmd->virt;
4006         payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
4007
4008         rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
4009                 payload_len, GFP_KERNEL);
4010         if (!rscn_event_data) {
4011                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4012                         "0147 Failed to allocate memory for RSCN event\n");
4013                 return;
4014         }
4015         rscn_event_data->event_type = FC_REG_RSCN_EVENT;
4016         rscn_event_data->payload_length = payload_len;
4017         memcpy(rscn_event_data->rscn_payload, payload_ptr,
4018                 payload_len);
4019
4020         fc_host_post_vendor_event(shost,
4021                 fc_get_event_number(),
4022                 sizeof(struct lpfc_els_event_header) + payload_len,
4023                 (char *)rscn_event_data,
4024                 LPFC_NL_VENDOR_ID);
4025
4026         kfree(rscn_event_data);
4027 }
4028
4029 /**
4030  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
4031  * @vport: pointer to a host virtual N_Port data structure.
4032  * @cmdiocb: pointer to lpfc command iocb data structure.
4033  * @ndlp: pointer to a node-list data structure.
4034  *
4035  * This routine processes an unsolicited RSCN (Registration State Change
4036  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
4037  * to invoke fc_host_post_event() routine to the FC transport layer. If the
4038  * discover state machine is about to begin discovery, it just accepts the
4039  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
4040  * contains N_Port IDs for other vports on this HBA, it just accepts the
4041  * RSCN and ignore processing it. If the state machine is in the recovery
4042  * state, the fc_rscn_id_list of this @vport is walked and the
4043  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
4044  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
4045  * routine is invoked to handle the RSCN event.
4046  *
4047  * Return code
4048  *   0 - Just sent the acc response
4049  *   1 - Sent the acc response and waited for name server completion
4050  **/
4051 static int
4052 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4053                   struct lpfc_nodelist *ndlp)
4054 {
4055         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4056         struct lpfc_hba  *phba = vport->phba;
4057         struct lpfc_dmabuf *pcmd;
4058         uint32_t *lp, *datap;
4059         IOCB_t *icmd;
4060         uint32_t payload_len, length, nportid, *cmd;
4061         int rscn_cnt;
4062         int rscn_id = 0, hba_id = 0;
4063         int i;
4064
4065         icmd = &cmdiocb->iocb;
4066         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4067         lp = (uint32_t *) pcmd->virt;
4068
4069         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4070         payload_len -= sizeof(uint32_t);        /* take off word 0 */
4071         /* RSCN received */
4072         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4073                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
4074                          vport->fc_flag, payload_len, *lp,
4075                          vport->fc_rscn_id_cnt);
4076
4077         /* Send an RSCN event to the management application */
4078         lpfc_send_rscn_event(vport, cmdiocb);
4079
4080         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
4081                 fc_host_post_event(shost, fc_get_event_number(),
4082                         FCH_EVT_RSCN, lp[i]);
4083
4084         /* If we are about to begin discovery, just ACC the RSCN.
4085          * Discovery processing will satisfy it.
4086          */
4087         if (vport->port_state <= LPFC_NS_QRY) {
4088                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4089                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
4090                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4091
4092                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4093                 return 0;
4094         }
4095
4096         /* If this RSCN just contains NPortIDs for other vports on this HBA,
4097          * just ACC and ignore it.
4098          */
4099         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4100                 !(vport->cfg_peer_port_login)) {
4101                 i = payload_len;
4102                 datap = lp;
4103                 while (i > 0) {
4104                         nportid = *datap++;
4105                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
4106                         i -= sizeof(uint32_t);
4107                         rscn_id++;
4108                         if (lpfc_find_vport_by_did(phba, nportid))
4109                                 hba_id++;
4110                 }
4111                 if (rscn_id == hba_id) {
4112                         /* ALL NPortIDs in RSCN are on HBA */
4113                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4114                                          "0219 Ignore RSCN "
4115                                          "Data: x%x x%x x%x x%x\n",
4116                                          vport->fc_flag, payload_len,
4117                                          *lp, vport->fc_rscn_id_cnt);
4118                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4119                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
4120                                 ndlp->nlp_DID, vport->port_state,
4121                                 ndlp->nlp_flag);
4122
4123                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
4124                                 ndlp, NULL);
4125                         return 0;
4126                 }
4127         }
4128
4129         spin_lock_irq(shost->host_lock);
4130         if (vport->fc_rscn_flush) {
4131                 /* Another thread is walking fc_rscn_id_list on this vport */
4132                 spin_unlock_irq(shost->host_lock);
4133                 vport->fc_flag |= FC_RSCN_DISCOVERY;
4134                 /* Send back ACC */
4135                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4136                 return 0;
4137         }
4138         /* Indicate we are walking fc_rscn_id_list on this vport */
4139         vport->fc_rscn_flush = 1;
4140         spin_unlock_irq(shost->host_lock);
4141         /* Get the array count after sucessfully have the token */
4142         rscn_cnt = vport->fc_rscn_id_cnt;
4143         /* If we are already processing an RSCN, save the received
4144          * RSCN payload buffer, cmdiocb->context2 to process later.
4145          */
4146         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
4147                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4148                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
4149                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4150
4151                 spin_lock_irq(shost->host_lock);
4152                 vport->fc_flag |= FC_RSCN_DEFERRED;
4153                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
4154                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
4155                         vport->fc_flag |= FC_RSCN_MODE;
4156                         spin_unlock_irq(shost->host_lock);
4157                         if (rscn_cnt) {
4158                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4159                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4160                         }
4161                         if ((rscn_cnt) &&
4162                             (payload_len + length <= LPFC_BPL_SIZE)) {
4163                                 *cmd &= ELS_CMD_MASK;
4164                                 *cmd |= cpu_to_be32(payload_len + length);
4165                                 memcpy(((uint8_t *)cmd) + length, lp,
4166                                        payload_len);
4167                         } else {
4168                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4169                                 vport->fc_rscn_id_cnt++;
4170                                 /* If we zero, cmdiocb->context2, the calling
4171                                  * routine will not try to free it.
4172                                  */
4173                                 cmdiocb->context2 = NULL;
4174                         }
4175                         /* Deferred RSCN */
4176                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4177                                          "0235 Deferred RSCN "
4178                                          "Data: x%x x%x x%x\n",
4179                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4180                                          vport->port_state);
4181                 } else {
4182                         vport->fc_flag |= FC_RSCN_DISCOVERY;
4183                         spin_unlock_irq(shost->host_lock);
4184                         /* ReDiscovery RSCN */
4185                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4186                                          "0234 ReDiscovery RSCN "
4187                                          "Data: x%x x%x x%x\n",
4188                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4189                                          vport->port_state);
4190                 }
4191                 /* Indicate we are done walking fc_rscn_id_list on this vport */
4192                 vport->fc_rscn_flush = 0;
4193                 /* Send back ACC */
4194                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4195                 /* send RECOVERY event for ALL nodes that match RSCN payload */
4196                 lpfc_rscn_recovery_check(vport);
4197                 spin_lock_irq(shost->host_lock);
4198                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
4199                 spin_unlock_irq(shost->host_lock);
4200                 return 0;
4201         }
4202         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4203                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
4204                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4205
4206         spin_lock_irq(shost->host_lock);
4207         vport->fc_flag |= FC_RSCN_MODE;
4208         spin_unlock_irq(shost->host_lock);
4209         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
4210         /* Indicate we are done walking fc_rscn_id_list on this vport */
4211         vport->fc_rscn_flush = 0;
4212         /*
4213          * If we zero, cmdiocb->context2, the calling routine will
4214          * not try to free it.
4215          */
4216         cmdiocb->context2 = NULL;
4217         lpfc_set_disctmo(vport);
4218         /* Send back ACC */
4219         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4220         /* send RECOVERY event for ALL nodes that match RSCN payload */
4221         lpfc_rscn_recovery_check(vport);
4222         return lpfc_els_handle_rscn(vport);
4223 }
4224
4225 /**
4226  * lpfc_els_handle_rscn - Handle rscn for a vport
4227  * @vport: pointer to a host virtual N_Port data structure.
4228  *
4229  * This routine handles the Registration State Configuration Notification
4230  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
4231  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
4232  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
4233  * NameServer shall be issued. If CT command to the NameServer fails to be
4234  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
4235  * RSCN activities with the @vport.
4236  *
4237  * Return code
4238  *   0 - Cleaned up rscn on the @vport
4239  *   1 - Wait for plogi to name server before proceed
4240  **/
4241 int
4242 lpfc_els_handle_rscn(struct lpfc_vport *vport)
4243 {
4244         struct lpfc_nodelist *ndlp;
4245         struct lpfc_hba *phba = vport->phba;
4246
4247         /* Ignore RSCN if the port is being torn down. */
4248         if (vport->load_flag & FC_UNLOADING) {
4249                 lpfc_els_flush_rscn(vport);
4250                 return 0;
4251         }
4252
4253         /* Start timer for RSCN processing */
4254         lpfc_set_disctmo(vport);
4255
4256         /* RSCN processed */
4257         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4258                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
4259                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
4260                          vport->port_state);
4261
4262         /* To process RSCN, first compare RSCN data with NameServer */
4263         vport->fc_ns_retry = 0;
4264         vport->num_disc_nodes = 0;
4265
4266         ndlp = lpfc_findnode_did(vport, NameServer_DID);
4267         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
4268             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
4269                 /* Good ndlp, issue CT Request to NameServer */
4270                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
4271                         /* Wait for NameServer query cmpl before we can
4272                            continue */
4273                         return 1;
4274         } else {
4275                 /* If login to NameServer does not exist, issue one */
4276                 /* Good status, issue PLOGI to NameServer */
4277                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
4278                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4279                         /* Wait for NameServer login cmpl before we can
4280                            continue */
4281                         return 1;
4282
4283                 if (ndlp) {
4284                         ndlp = lpfc_enable_node(vport, ndlp,
4285                                                 NLP_STE_PLOGI_ISSUE);
4286                         if (!ndlp) {
4287                                 lpfc_els_flush_rscn(vport);
4288                                 return 0;
4289                         }
4290                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
4291                 } else {
4292                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4293                         if (!ndlp) {
4294                                 lpfc_els_flush_rscn(vport);
4295                                 return 0;
4296                         }
4297                         lpfc_nlp_init(vport, ndlp, NameServer_DID);
4298                         ndlp->nlp_prev_state = ndlp->nlp_state;
4299                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4300                 }
4301                 ndlp->nlp_type |= NLP_FABRIC;
4302                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
4303                 /* Wait for NameServer login cmpl before we can
4304                  * continue
4305                  */
4306                 return 1;
4307         }
4308
4309         lpfc_els_flush_rscn(vport);
4310         return 0;
4311 }
4312
4313 /**
4314  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
4315  * @vport: pointer to a host virtual N_Port data structure.
4316  * @cmdiocb: pointer to lpfc command iocb data structure.
4317  * @ndlp: pointer to a node-list data structure.
4318  *
4319  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
4320  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
4321  * point topology. As an unsolicited FLOGI should not be received in a loop
4322  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
4323  * lpfc_check_sparm() routine is invoked to check the parameters in the
4324  * unsolicited FLOGI. If parameters validation failed, the routine
4325  * lpfc_els_rsp_reject() shall be called with reject reason code set to
4326  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
4327  * FLOGI shall be compared with the Port WWN of the @vport to determine who
4328  * will initiate PLOGI. The higher lexicographical value party shall has
4329  * higher priority (as the winning port) and will initiate PLOGI and
4330  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
4331  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
4332  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
4333  *
4334  * Return code
4335  *   0 - Successfully processed the unsolicited flogi
4336  *   1 - Failed to process the unsolicited flogi
4337  **/
4338 static int
4339 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4340                    struct lpfc_nodelist *ndlp)
4341 {
4342         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4343         struct lpfc_hba  *phba = vport->phba;
4344         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4345         uint32_t *lp = (uint32_t *) pcmd->virt;
4346         IOCB_t *icmd = &cmdiocb->iocb;
4347         struct serv_parm *sp;
4348         LPFC_MBOXQ_t *mbox;
4349         struct ls_rjt stat;
4350         uint32_t cmd, did;
4351         int rc;
4352
4353         cmd = *lp++;
4354         sp = (struct serv_parm *) lp;
4355
4356         /* FLOGI received */
4357
4358         lpfc_set_disctmo(vport);
4359
4360         if (phba->fc_topology == TOPOLOGY_LOOP) {
4361                 /* We should never receive a FLOGI in loop mode, ignore it */
4362                 did = icmd->un.elsreq64.remoteID;
4363
4364                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
4365                    Loop Mode */
4366                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4367                                  "0113 An FLOGI ELS command x%x was "
4368                                  "received from DID x%x in Loop Mode\n",
4369                                  cmd, did);
4370                 return 1;
4371         }
4372
4373         did = Fabric_DID;
4374
4375         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
4376                 /* For a FLOGI we accept, then if our portname is greater
4377                  * then the remote portname we initiate Nport login.
4378                  */
4379
4380                 rc = memcmp(&vport->fc_portname, &sp->portName,
4381                             sizeof(struct lpfc_name));
4382
4383                 if (!rc) {
4384                         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4385                         if (!mbox)
4386                                 return 1;
4387
4388                         lpfc_linkdown(phba);
4389                         lpfc_init_link(phba, mbox,
4390                                        phba->cfg_topology,
4391                                        phba->cfg_link_speed);
4392                         mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
4393                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4394                         mbox->vport = vport;
4395                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4396                         lpfc_set_loopback_flag(phba);
4397                         if (rc == MBX_NOT_FINISHED) {
4398                                 mempool_free(mbox, phba->mbox_mem_pool);
4399                         }
4400                         return 1;
4401                 } else if (rc > 0) {    /* greater than */
4402                         spin_lock_irq(shost->host_lock);
4403                         vport->fc_flag |= FC_PT2PT_PLOGI;
4404                         spin_unlock_irq(shost->host_lock);
4405                 }
4406                 spin_lock_irq(shost->host_lock);
4407                 vport->fc_flag |= FC_PT2PT;
4408                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4409                 spin_unlock_irq(shost->host_lock);
4410         } else {
4411                 /* Reject this request because invalid parameters */
4412                 stat.un.b.lsRjtRsvd0 = 0;
4413                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4414                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
4415                 stat.un.b.vendorUnique = 0;
4416                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4417                         NULL);
4418                 return 1;
4419         }
4420
4421         /* Send back ACC */
4422         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
4423
4424         return 0;
4425 }
4426
4427 /**
4428  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
4429  * @vport: pointer to a host virtual N_Port data structure.
4430  * @cmdiocb: pointer to lpfc command iocb data structure.
4431  * @ndlp: pointer to a node-list data structure.
4432  *
4433  * This routine processes Request Node Identification Data (RNID) IOCB
4434  * received as an ELS unsolicited event. Only when the RNID specified format
4435  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
4436  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
4437  * Accept (ACC) the RNID ELS command. All the other RNID formats are
4438  * rejected by invoking the lpfc_els_rsp_reject() routine.
4439  *
4440  * Return code
4441  *   0 - Successfully processed rnid iocb (currently always return 0)
4442  **/
4443 static int
4444 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4445                   struct lpfc_nodelist *ndlp)
4446 {
4447         struct lpfc_dmabuf *pcmd;
4448         uint32_t *lp;
4449         IOCB_t *icmd;
4450         RNID *rn;
4451         struct ls_rjt stat;
4452         uint32_t cmd, did;
4453
4454         icmd = &cmdiocb->iocb;
4455         did = icmd->un.elsreq64.remoteID;
4456         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4457         lp = (uint32_t *) pcmd->virt;
4458
4459         cmd = *lp++;
4460         rn = (RNID *) lp;
4461
4462         /* RNID received */
4463
4464         switch (rn->Format) {
4465         case 0:
4466         case RNID_TOPOLOGY_DISC:
4467                 /* Send back ACC */
4468                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
4469                 break;
4470         default:
4471                 /* Reject this request because format not supported */
4472                 stat.un.b.lsRjtRsvd0 = 0;
4473                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4474                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4475                 stat.un.b.vendorUnique = 0;
4476                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4477                         NULL);
4478         }
4479         return 0;
4480 }
4481
4482 /**
4483  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
4484  * @vport: pointer to a host virtual N_Port data structure.
4485  * @cmdiocb: pointer to lpfc command iocb data structure.
4486  * @ndlp: pointer to a node-list data structure.
4487  *
4488  * This routine processes a Link Incident Report Registration(LIRR) IOCB
4489  * received as an ELS unsolicited event. Currently, this function just invokes
4490  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
4491  *
4492  * Return code
4493  *   0 - Successfully processed lirr iocb (currently always return 0)
4494  **/
4495 static int
4496 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4497                   struct lpfc_nodelist *ndlp)
4498 {
4499         struct ls_rjt stat;
4500
4501         /* For now, unconditionally reject this command */
4502         stat.un.b.lsRjtRsvd0 = 0;
4503         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4504         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4505         stat.un.b.vendorUnique = 0;
4506         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4507         return 0;
4508 }
4509
4510 /**
4511  * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
4512  * @phba: pointer to lpfc hba data structure.
4513  * @pmb: pointer to the driver internal queue element for mailbox command.
4514  *
4515  * This routine is the completion callback function for the MBX_READ_LNK_STAT
4516  * mailbox command. This callback function is to actually send the Accept
4517  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
4518  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
4519  * mailbox command, constructs the RPS response with the link statistics
4520  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
4521  * response to the RPS.
4522  *
4523  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4524  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4525  * will be stored into the context1 field of the IOCB for the completion
4526  * callback function to the RPS Accept Response ELS IOCB command.
4527  *
4528  **/
4529 static void
4530 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4531 {
4532         MAILBOX_t *mb;
4533         IOCB_t *icmd;
4534         RPS_RSP *rps_rsp;
4535         uint8_t *pcmd;
4536         struct lpfc_iocbq *elsiocb;
4537         struct lpfc_nodelist *ndlp;
4538         uint16_t xri, status;
4539         uint32_t cmdsize;
4540
4541         mb = &pmb->u.mb;
4542
4543         ndlp = (struct lpfc_nodelist *) pmb->context2;
4544         xri = (uint16_t) ((unsigned long)(pmb->context1));
4545         pmb->context1 = NULL;
4546         pmb->context2 = NULL;
4547
4548         if (mb->mbxStatus) {
4549                 mempool_free(pmb, phba->mbox_mem_pool);
4550                 return;
4551         }
4552
4553         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
4554         mempool_free(pmb, phba->mbox_mem_pool);
4555         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
4556                                      lpfc_max_els_tries, ndlp,
4557                                      ndlp->nlp_DID, ELS_CMD_ACC);
4558
4559         /* Decrement the ndlp reference count from previous mbox command */
4560         lpfc_nlp_put(ndlp);
4561
4562         if (!elsiocb)
4563                 return;
4564
4565         icmd = &elsiocb->iocb;
4566         icmd->ulpContext = xri;
4567
4568         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4569         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4570         pcmd += sizeof(uint32_t); /* Skip past command */
4571         rps_rsp = (RPS_RSP *)pcmd;
4572
4573         if (phba->fc_topology != TOPOLOGY_LOOP)
4574                 status = 0x10;
4575         else
4576                 status = 0x8;
4577         if (phba->pport->fc_flag & FC_FABRIC)
4578                 status |= 0x4;
4579
4580         rps_rsp->rsvd1 = 0;
4581         rps_rsp->portStatus = cpu_to_be16(status);
4582         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
4583         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
4584         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
4585         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
4586         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
4587         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
4588         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
4589         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
4590                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
4591                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4592                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4593                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4594                          ndlp->nlp_rpi);
4595         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4596         phba->fc_stat.elsXmitACC++;
4597         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
4598                 lpfc_els_free_iocb(phba, elsiocb);
4599         return;
4600 }
4601
4602 /**
4603  * lpfc_els_rcv_rps - Process an unsolicited rps iocb
4604  * @vport: pointer to a host virtual N_Port data structure.
4605  * @cmdiocb: pointer to lpfc command iocb data structure.
4606  * @ndlp: pointer to a node-list data structure.
4607  *
4608  * This routine processes Read Port Status (RPS) IOCB received as an
4609  * ELS unsolicited event. It first checks the remote port state. If the
4610  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
4611  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
4612  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
4613  * for reading the HBA link statistics. It is for the callback function,
4614  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
4615  * to actually sending out RPS Accept (ACC) response.
4616  *
4617  * Return codes
4618  *   0 - Successfully processed rps iocb (currently always return 0)
4619  **/
4620 static int
4621 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4622                  struct lpfc_nodelist *ndlp)
4623 {
4624         struct lpfc_hba *phba = vport->phba;
4625         uint32_t *lp;
4626         uint8_t flag;
4627         LPFC_MBOXQ_t *mbox;
4628         struct lpfc_dmabuf *pcmd;
4629         RPS *rps;
4630         struct ls_rjt stat;
4631
4632         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4633             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
4634                 /* reject the unsolicited RPS request and done with it */
4635                 goto reject_out;
4636
4637         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4638         lp = (uint32_t *) pcmd->virt;
4639         flag = (be32_to_cpu(*lp++) & 0xf);
4640         rps = (RPS *) lp;
4641
4642         if ((flag == 0) ||
4643             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
4644             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
4645                                     sizeof(struct lpfc_name)) == 0))) {
4646
4647                 printk("Fix me....\n");
4648                 dump_stack();
4649                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
4650                 if (mbox) {
4651                         lpfc_read_lnk_stat(phba, mbox);
4652                         mbox->context1 =
4653                             (void *)((unsigned long) cmdiocb->iocb.ulpContext);
4654                         mbox->context2 = lpfc_nlp_get(ndlp);
4655                         mbox->vport = vport;
4656                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
4657                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4658                                 != MBX_NOT_FINISHED)
4659                                 /* Mbox completion will send ELS Response */
4660                                 return 0;
4661                         /* Decrement reference count used for the failed mbox
4662                          * command.
4663                          */
4664                         lpfc_nlp_put(ndlp);
4665                         mempool_free(mbox, phba->mbox_mem_pool);
4666                 }
4667         }
4668
4669 reject_out:
4670         /* issue rejection response */
4671         stat.un.b.lsRjtRsvd0 = 0;
4672         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4673         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4674         stat.un.b.vendorUnique = 0;
4675         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4676         return 0;
4677 }
4678
4679 /**
4680  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
4681  * @vport: pointer to a host virtual N_Port data structure.
4682  * @cmdsize: size of the ELS command.
4683  * @oldiocb: pointer to the original lpfc command iocb data structure.
4684  * @ndlp: pointer to a node-list data structure.
4685  *
4686  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
4687  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
4688  *
4689  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4690  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4691  * will be stored into the context1 field of the IOCB for the completion
4692  * callback function to the RPL Accept Response ELS command.
4693  *
4694  * Return code
4695  *   0 - Successfully issued ACC RPL ELS command
4696  *   1 - Failed to issue ACC RPL ELS command
4697  **/
4698 static int
4699 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
4700                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4701 {
4702         struct lpfc_hba *phba = vport->phba;
4703         IOCB_t *icmd, *oldcmd;
4704         RPL_RSP rpl_rsp;
4705         struct lpfc_iocbq *elsiocb;
4706         uint8_t *pcmd;
4707
4708         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4709                                      ndlp->nlp_DID, ELS_CMD_ACC);
4710
4711         if (!elsiocb)
4712                 return 1;
4713
4714         icmd = &elsiocb->iocb;
4715         oldcmd = &oldiocb->iocb;
4716         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
4717
4718         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4719         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4720         pcmd += sizeof(uint16_t);
4721         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
4722         pcmd += sizeof(uint16_t);
4723
4724         /* Setup the RPL ACC payload */
4725         rpl_rsp.listLen = be32_to_cpu(1);
4726         rpl_rsp.index = 0;
4727         rpl_rsp.port_num_blk.portNum = 0;
4728         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
4729         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
4730             sizeof(struct lpfc_name));
4731         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
4732         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
4733         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4734                          "0120 Xmit ELS RPL ACC response tag x%x "
4735                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4736                          "rpi x%x\n",
4737                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4738                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4739                          ndlp->nlp_rpi);
4740         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4741         phba->fc_stat.elsXmitACC++;
4742         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
4743             IOCB_ERROR) {
4744                 lpfc_els_free_iocb(phba, elsiocb);
4745                 return 1;
4746         }
4747         return 0;
4748 }
4749
4750 /**
4751  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
4752  * @vport: pointer to a host virtual N_Port data structure.
4753  * @cmdiocb: pointer to lpfc command iocb data structure.
4754  * @ndlp: pointer to a node-list data structure.
4755  *
4756  * This routine processes Read Port List (RPL) IOCB received as an ELS
4757  * unsolicited event. It first checks the remote port state. If the remote
4758  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
4759  * invokes the lpfc_els_rsp_reject() routine to send reject response.
4760  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
4761  * to accept the RPL.
4762  *
4763  * Return code
4764  *   0 - Successfully processed rpl iocb (currently always return 0)
4765  **/
4766 static int
4767 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4768                  struct lpfc_nodelist *ndlp)
4769 {
4770         struct lpfc_dmabuf *pcmd;
4771         uint32_t *lp;
4772         uint32_t maxsize;
4773         uint16_t cmdsize;
4774         RPL *rpl;
4775         struct ls_rjt stat;
4776
4777         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4778             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
4779                 /* issue rejection response */
4780                 stat.un.b.lsRjtRsvd0 = 0;
4781                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4782                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4783                 stat.un.b.vendorUnique = 0;
4784                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4785                         NULL);
4786                 /* rejected the unsolicited RPL request and done with it */
4787                 return 0;
4788         }
4789
4790         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4791         lp = (uint32_t *) pcmd->virt;
4792         rpl = (RPL *) (lp + 1);
4793
4794         maxsize = be32_to_cpu(rpl->maxsize);
4795
4796         /* We support only one port */
4797         if ((rpl->index == 0) &&
4798             ((maxsize == 0) ||
4799              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
4800                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
4801         } else {
4802                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
4803         }
4804         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
4805
4806         return 0;
4807 }
4808
4809 /**
4810  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
4811  * @vport: pointer to a virtual N_Port data structure.
4812  * @cmdiocb: pointer to lpfc command iocb data structure.
4813  * @ndlp: pointer to a node-list data structure.
4814  *
4815  * This routine processes Fibre Channel Address Resolution Protocol
4816  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
4817  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
4818  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
4819  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
4820  * remote PortName is compared against the FC PortName stored in the @vport
4821  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
4822  * compared against the FC NodeName stored in the @vport data structure.
4823  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
4824  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
4825  * invoked to send out FARP Response to the remote node. Before sending the
4826  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
4827  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
4828  * routine is invoked to log into the remote port first.
4829  *
4830  * Return code
4831  *   0 - Either the FARP Match Mode not supported or successfully processed
4832  **/
4833 static int
4834 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4835                   struct lpfc_nodelist *ndlp)
4836 {
4837         struct lpfc_dmabuf *pcmd;
4838         uint32_t *lp;
4839         IOCB_t *icmd;
4840         FARP *fp;
4841         uint32_t cmd, cnt, did;
4842
4843         icmd = &cmdiocb->iocb;
4844         did = icmd->un.elsreq64.remoteID;
4845         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4846         lp = (uint32_t *) pcmd->virt;
4847
4848         cmd = *lp++;
4849         fp = (FARP *) lp;
4850         /* FARP-REQ received from DID <did> */
4851         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4852                          "0601 FARP-REQ received from DID x%x\n", did);
4853         /* We will only support match on WWPN or WWNN */
4854         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
4855                 return 0;
4856         }
4857
4858         cnt = 0;
4859         /* If this FARP command is searching for my portname */
4860         if (fp->Mflags & FARP_MATCH_PORT) {
4861                 if (memcmp(&fp->RportName, &vport->fc_portname,
4862                            sizeof(struct lpfc_name)) == 0)
4863                         cnt = 1;
4864         }
4865
4866         /* If this FARP command is searching for my nodename */
4867         if (fp->Mflags & FARP_MATCH_NODE) {
4868                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
4869                            sizeof(struct lpfc_name)) == 0)
4870                         cnt = 1;
4871         }
4872
4873         if (cnt) {
4874                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
4875                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
4876                         /* Log back into the node before sending the FARP. */
4877                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
4878                                 ndlp->nlp_prev_state = ndlp->nlp_state;
4879                                 lpfc_nlp_set_state(vport, ndlp,
4880                                                    NLP_STE_PLOGI_ISSUE);
4881                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4882                         }
4883
4884                         /* Send a FARP response to that node */
4885                         if (fp->Rflags & FARP_REQUEST_FARPR)
4886                                 lpfc_issue_els_farpr(vport, did, 0);
4887                 }
4888         }
4889         return 0;
4890 }
4891
4892 /**
4893  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
4894  * @vport: pointer to a host virtual N_Port data structure.
4895  * @cmdiocb: pointer to lpfc command iocb data structure.
4896  * @ndlp: pointer to a node-list data structure.
4897  *
4898  * This routine processes Fibre Channel Address Resolution Protocol
4899  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
4900  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
4901  * the FARP response request.
4902  *
4903  * Return code
4904  *   0 - Successfully processed FARPR IOCB (currently always return 0)
4905  **/
4906 static int
4907 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4908                    struct lpfc_nodelist  *ndlp)
4909 {
4910         struct lpfc_dmabuf *pcmd;
4911         uint32_t *lp;
4912         IOCB_t *icmd;
4913         uint32_t cmd, did;
4914
4915         icmd = &cmdiocb->iocb;
4916         did = icmd->un.elsreq64.remoteID;
4917         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4918         lp = (uint32_t *) pcmd->virt;
4919
4920         cmd = *lp++;
4921         /* FARP-RSP received from DID <did> */
4922         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4923                          "0600 FARP-RSP received from DID x%x\n", did);
4924         /* ACCEPT the Farp resp request */
4925         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4926
4927         return 0;
4928 }
4929
4930 /**
4931  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
4932  * @vport: pointer to a host virtual N_Port data structure.
4933  * @cmdiocb: pointer to lpfc command iocb data structure.
4934  * @fan_ndlp: pointer to a node-list data structure.
4935  *
4936  * This routine processes a Fabric Address Notification (FAN) IOCB
4937  * command received as an ELS unsolicited event. The FAN ELS command will
4938  * only be processed on a physical port (i.e., the @vport represents the
4939  * physical port). The fabric NodeName and PortName from the FAN IOCB are
4940  * compared against those in the phba data structure. If any of those is
4941  * different, the lpfc_initial_flogi() routine is invoked to initialize
4942  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
4943  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
4944  * is invoked to register login to the fabric.
4945  *
4946  * Return code
4947  *   0 - Successfully processed fan iocb (currently always return 0).
4948  **/
4949 static int
4950 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4951                  struct lpfc_nodelist *fan_ndlp)
4952 {
4953         struct lpfc_hba *phba = vport->phba;
4954         uint32_t *lp;
4955         FAN *fp;
4956
4957         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
4958         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
4959         fp = (FAN *) ++lp;
4960         /* FAN received; Fan does not have a reply sequence */
4961         if ((vport == phba->pport) &&
4962             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
4963                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
4964                             sizeof(struct lpfc_name))) ||
4965                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
4966                             sizeof(struct lpfc_name)))) {
4967                         /* This port has switched fabrics. FLOGI is required */
4968                         lpfc_initial_flogi(vport);
4969                 } else {
4970                         /* FAN verified - skip FLOGI */
4971                         vport->fc_myDID = vport->fc_prevDID;
4972                         if (phba->sli_rev < LPFC_SLI_REV4)
4973                                 lpfc_issue_fabric_reglogin(vport);
4974                         else
4975                                 lpfc_issue_reg_vfi(vport);
4976                 }
4977         }
4978         return 0;
4979 }
4980
4981 /**
4982  * lpfc_els_timeout - Handler funciton to the els timer
4983  * @ptr: holder for the timer function associated data.
4984  *
4985  * This routine is invoked by the ELS timer after timeout. It posts the ELS
4986  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
4987  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
4988  * up the worker thread. It is for the worker thread to invoke the routine
4989  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
4990  **/
4991 void
4992 lpfc_els_timeout(unsigned long ptr)
4993 {
4994         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4995         struct lpfc_hba   *phba = vport->phba;
4996         uint32_t tmo_posted;
4997         unsigned long iflag;
4998
4999         spin_lock_irqsave(&vport->work_port_lock, iflag);
5000         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
5001         if (!tmo_posted)
5002                 vport->work_port_events |= WORKER_ELS_TMO;
5003         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
5004
5005         if (!tmo_posted)
5006                 lpfc_worker_wake_up(phba);
5007         return;
5008 }
5009
5010 /**
5011  * lpfc_els_timeout_handler - Process an els timeout event
5012  * @vport: pointer to a virtual N_Port data structure.
5013  *
5014  * This routine is the actual handler function that processes an ELS timeout
5015  * event. It walks the ELS ring to get and abort all the IOCBs (except the
5016  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
5017  * invoking the lpfc_sli_issue_abort_iotag() routine.
5018  **/
5019 void
5020 lpfc_els_timeout_handler(struct lpfc_vport *vport)
5021 {
5022         struct lpfc_hba  *phba = vport->phba;
5023         struct lpfc_sli_ring *pring;
5024         struct lpfc_iocbq *tmp_iocb, *piocb;
5025         IOCB_t *cmd = NULL;
5026         struct lpfc_dmabuf *pcmd;
5027         uint32_t els_command = 0;
5028         uint32_t timeout;
5029         uint32_t remote_ID = 0xffffffff;
5030
5031         spin_lock_irq(&phba->hbalock);
5032         timeout = (uint32_t)(phba->fc_ratov << 1);
5033
5034         pring = &phba->sli.ring[LPFC_ELS_RING];
5035
5036         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5037                 cmd = &piocb->iocb;
5038
5039                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
5040                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
5041                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
5042                         continue;
5043
5044                 if (piocb->vport != vport)
5045                         continue;
5046
5047                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
5048                 if (pcmd)
5049                         els_command = *(uint32_t *) (pcmd->virt);
5050
5051                 if (els_command == ELS_CMD_FARP ||
5052                     els_command == ELS_CMD_FARPR ||
5053                     els_command == ELS_CMD_FDISC)
5054                         continue;
5055
5056                 if (piocb->drvrTimeout > 0) {
5057                         if (piocb->drvrTimeout >= timeout)
5058                                 piocb->drvrTimeout -= timeout;
5059                         else
5060                                 piocb->drvrTimeout = 0;
5061                         continue;
5062                 }
5063
5064                 remote_ID = 0xffffffff;
5065                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
5066                         remote_ID = cmd->un.elsreq64.remoteID;
5067                 else {
5068                         struct lpfc_nodelist *ndlp;
5069                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
5070                         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
5071                                 remote_ID = ndlp->nlp_DID;
5072                 }
5073                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5074                                  "0127 ELS timeout Data: x%x x%x x%x "
5075                                  "x%x\n", els_command,
5076                                  remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
5077                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5078         }
5079         spin_unlock_irq(&phba->hbalock);
5080
5081         if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
5082                 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
5083 }
5084
5085 /**
5086  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
5087  * @vport: pointer to a host virtual N_Port data structure.
5088  *
5089  * This routine is used to clean up all the outstanding ELS commands on a
5090  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
5091  * routine. After that, it walks the ELS transmit queue to remove all the
5092  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
5093  * the IOCBs with a non-NULL completion callback function, the callback
5094  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5095  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
5096  * callback function, the IOCB will simply be released. Finally, it walks
5097  * the ELS transmit completion queue to issue an abort IOCB to any transmit
5098  * completion queue IOCB that is associated with the @vport and is not
5099  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
5100  * part of the discovery state machine) out to HBA by invoking the
5101  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
5102  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
5103  * the IOCBs are aborted when this function returns.
5104  **/
5105 void
5106 lpfc_els_flush_cmd(struct lpfc_vport *vport)
5107 {
5108         LIST_HEAD(completions);
5109         struct lpfc_hba  *phba = vport->phba;
5110         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5111         struct lpfc_iocbq *tmp_iocb, *piocb;
5112         IOCB_t *cmd = NULL;
5113
5114         lpfc_fabric_abort_vport(vport);
5115
5116         spin_lock_irq(&phba->hbalock);
5117         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5118                 cmd = &piocb->iocb;
5119
5120                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5121                         continue;
5122                 }
5123
5124                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5125                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5126                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5127                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5128                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
5129                         continue;
5130
5131                 if (piocb->vport != vport)
5132                         continue;
5133
5134                 list_move_tail(&piocb->list, &completions);
5135                 pring->txq_cnt--;
5136         }
5137
5138         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5139                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5140                         continue;
5141                 }
5142
5143                 if (piocb->vport != vport)
5144                         continue;
5145
5146                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5147         }
5148         spin_unlock_irq(&phba->hbalock);
5149
5150         /* Cancell all the IOCBs from the completions list */
5151         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5152                               IOERR_SLI_ABORTED);
5153
5154         return;
5155 }
5156
5157 /**
5158  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
5159  * @phba: pointer to lpfc hba data structure.
5160  *
5161  * This routine is used to clean up all the outstanding ELS commands on a
5162  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
5163  * routine. After that, it walks the ELS transmit queue to remove all the
5164  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
5165  * the IOCBs with the completion callback function associated, the callback
5166  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5167  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
5168  * callback function associated, the IOCB will simply be released. Finally,
5169  * it walks the ELS transmit completion queue to issue an abort IOCB to any
5170  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
5171  * management plane IOCBs that are not part of the discovery state machine)
5172  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
5173  **/
5174 void
5175 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
5176 {
5177         LIST_HEAD(completions);
5178         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5179         struct lpfc_iocbq *tmp_iocb, *piocb;
5180         IOCB_t *cmd = NULL;
5181
5182         lpfc_fabric_abort_hba(phba);
5183         spin_lock_irq(&phba->hbalock);
5184         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5185                 cmd = &piocb->iocb;
5186                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5187                         continue;
5188                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5189                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5190                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5191                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5192                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
5193                         continue;
5194                 list_move_tail(&piocb->list, &completions);
5195                 pring->txq_cnt--;
5196         }
5197         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5198                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5199                         continue;
5200                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5201         }
5202         spin_unlock_irq(&phba->hbalock);
5203
5204         /* Cancel all the IOCBs from the completions list */
5205         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5206                               IOERR_SLI_ABORTED);
5207
5208         return;
5209 }
5210
5211 /**
5212  * lpfc_send_els_failure_event - Posts an ELS command failure event
5213  * @phba: Pointer to hba context object.
5214  * @cmdiocbp: Pointer to command iocb which reported error.
5215  * @rspiocbp: Pointer to response iocb which reported error.
5216  *
5217  * This function sends an event when there is an ELS command
5218  * failure.
5219  **/
5220 void
5221 lpfc_send_els_failure_event(struct lpfc_hba *phba,
5222                         struct lpfc_iocbq *cmdiocbp,
5223                         struct lpfc_iocbq *rspiocbp)
5224 {
5225         struct lpfc_vport *vport = cmdiocbp->vport;
5226         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5227         struct lpfc_lsrjt_event lsrjt_event;
5228         struct lpfc_fabric_event_header fabric_event;
5229         struct ls_rjt stat;
5230         struct lpfc_nodelist *ndlp;
5231         uint32_t *pcmd;
5232
5233         ndlp = cmdiocbp->context1;
5234         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
5235                 return;
5236
5237         if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
5238                 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
5239                 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
5240                 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
5241                         sizeof(struct lpfc_name));
5242                 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
5243                         sizeof(struct lpfc_name));
5244                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
5245                         cmdiocbp->context2)->virt);
5246                 lsrjt_event.command = *pcmd;
5247                 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
5248                 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
5249                 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
5250                 fc_host_post_vendor_event(shost,
5251                         fc_get_event_number(),
5252                         sizeof(lsrjt_event),
5253                         (char *)&lsrjt_event,
5254                         LPFC_NL_VENDOR_ID);
5255                 return;
5256         }
5257         if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
5258                 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
5259                 fabric_event.event_type = FC_REG_FABRIC_EVENT;
5260                 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
5261                         fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
5262                 else
5263                         fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
5264                 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
5265                         sizeof(struct lpfc_name));
5266                 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
5267                         sizeof(struct lpfc_name));
5268                 fc_host_post_vendor_event(shost,
5269                         fc_get_event_number(),
5270                         sizeof(fabric_event),
5271                         (char *)&fabric_event,
5272                         LPFC_NL_VENDOR_ID);
5273                 return;
5274         }
5275
5276 }
5277
5278 /**
5279  * lpfc_send_els_event - Posts unsolicited els event
5280  * @vport: Pointer to vport object.
5281  * @ndlp: Pointer FC node object.
5282  * @cmd: ELS command code.
5283  *
5284  * This function posts an event when there is an incoming
5285  * unsolicited ELS command.
5286  **/
5287 static void
5288 lpfc_send_els_event(struct lpfc_vport *vport,
5289                     struct lpfc_nodelist *ndlp,
5290                     uint32_t *payload)
5291 {
5292         struct lpfc_els_event_header *els_data = NULL;
5293         struct lpfc_logo_event *logo_data = NULL;
5294         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5295
5296         if (*payload == ELS_CMD_LOGO) {
5297                 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
5298                 if (!logo_data) {
5299                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5300                                 "0148 Failed to allocate memory "
5301                                 "for LOGO event\n");
5302                         return;
5303                 }
5304                 els_data = &logo_data->header;
5305         } else {
5306                 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
5307                         GFP_KERNEL);
5308                 if (!els_data) {
5309                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5310                                 "0149 Failed to allocate memory "
5311                                 "for ELS event\n");
5312                         return;
5313                 }
5314         }
5315         els_data->event_type = FC_REG_ELS_EVENT;
5316         switch (*payload) {
5317         case ELS_CMD_PLOGI:
5318                 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
5319                 break;
5320         case ELS_CMD_PRLO:
5321                 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
5322                 break;
5323         case ELS_CMD_ADISC:
5324                 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
5325                 break;
5326         case ELS_CMD_LOGO:
5327                 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
5328                 /* Copy the WWPN in the LOGO payload */
5329                 memcpy(logo_data->logo_wwpn, &payload[2],
5330                         sizeof(struct lpfc_name));
5331                 break;
5332         default:
5333                 kfree(els_data);
5334                 return;
5335         }
5336         memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
5337         memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
5338         if (*payload == ELS_CMD_LOGO) {
5339                 fc_host_post_vendor_event(shost,
5340                         fc_get_event_number(),
5341                         sizeof(struct lpfc_logo_event),
5342                         (char *)logo_data,
5343                         LPFC_NL_VENDOR_ID);
5344                 kfree(logo_data);
5345         } else {
5346                 fc_host_post_vendor_event(shost,
5347                         fc_get_event_number(),
5348                         sizeof(struct lpfc_els_event_header),
5349                         (char *)els_data,
5350                         LPFC_NL_VENDOR_ID);
5351                 kfree(els_data);
5352         }
5353
5354         return;
5355 }
5356
5357
5358 /**
5359  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
5360  * @phba: pointer to lpfc hba data structure.
5361  * @pring: pointer to a SLI ring.
5362  * @vport: pointer to a host virtual N_Port data structure.
5363  * @elsiocb: pointer to lpfc els command iocb data structure.
5364  *
5365  * This routine is used for processing the IOCB associated with a unsolicited
5366  * event. It first determines whether there is an existing ndlp that matches
5367  * the DID from the unsolicited IOCB. If not, it will create a new one with
5368  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
5369  * IOCB is then used to invoke the proper routine and to set up proper state
5370  * of the discovery state machine.
5371  **/
5372 static void
5373 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5374                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
5375 {
5376         struct Scsi_Host  *shost;
5377         struct lpfc_nodelist *ndlp;
5378         struct ls_rjt stat;
5379         uint32_t *payload;
5380         uint32_t cmd, did, newnode, rjt_err = 0;
5381         IOCB_t *icmd = &elsiocb->iocb;
5382
5383         if (!vport || !(elsiocb->context2))
5384                 goto dropit;
5385
5386         newnode = 0;
5387         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
5388         cmd = *payload;
5389         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
5390                 lpfc_post_buffer(phba, pring, 1);
5391
5392         did = icmd->un.rcvels.remoteID;
5393         if (icmd->ulpStatus) {
5394                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5395                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
5396                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
5397                 goto dropit;
5398         }
5399
5400         /* Check to see if link went down during discovery */
5401         if (lpfc_els_chk_latt(vport))
5402                 goto dropit;
5403
5404         /* Ignore traffic recevied during vport shutdown. */
5405         if (vport->load_flag & FC_UNLOADING)
5406                 goto dropit;
5407
5408         ndlp = lpfc_findnode_did(vport, did);
5409         if (!ndlp) {
5410                 /* Cannot find existing Fabric ndlp, so allocate a new one */
5411                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5412                 if (!ndlp)
5413                         goto dropit;
5414
5415                 lpfc_nlp_init(vport, ndlp, did);
5416                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5417                 newnode = 1;
5418                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5419                         ndlp->nlp_type |= NLP_FABRIC;
5420         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5421                 ndlp = lpfc_enable_node(vport, ndlp,
5422                                         NLP_STE_UNUSED_NODE);
5423                 if (!ndlp)
5424                         goto dropit;
5425                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5426                 newnode = 1;
5427                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5428                         ndlp->nlp_type |= NLP_FABRIC;
5429         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
5430                 /* This is similar to the new node path */
5431                 ndlp = lpfc_nlp_get(ndlp);
5432                 if (!ndlp)
5433                         goto dropit;
5434                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5435                 newnode = 1;
5436         }
5437
5438         phba->fc_stat.elsRcvFrame++;
5439
5440         elsiocb->context1 = lpfc_nlp_get(ndlp);
5441         elsiocb->vport = vport;
5442
5443         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
5444                 cmd &= ELS_CMD_MASK;
5445         }
5446         /* ELS command <elsCmd> received from NPORT <did> */
5447         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5448                          "0112 ELS command x%x received from NPORT x%x "
5449                          "Data: x%x\n", cmd, did, vport->port_state);
5450         switch (cmd) {
5451         case ELS_CMD_PLOGI:
5452                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5453                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
5454                         did, vport->port_state, ndlp->nlp_flag);
5455
5456                 phba->fc_stat.elsRcvPLOGI++;
5457                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
5458
5459                 lpfc_send_els_event(vport, ndlp, payload);
5460                 if (vport->port_state < LPFC_DISC_AUTH) {
5461                         if (!(phba->pport->fc_flag & FC_PT2PT) ||
5462                                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
5463                                 rjt_err = LSRJT_UNABLE_TPC;
5464                                 break;
5465                         }
5466                         /* We get here, and drop thru, if we are PT2PT with
5467                          * another NPort and the other side has initiated
5468                          * the PLOGI before responding to our FLOGI.
5469                          */
5470                 }
5471
5472                 shost = lpfc_shost_from_vport(vport);
5473                 spin_lock_irq(shost->host_lock);
5474                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
5475                 spin_unlock_irq(shost->host_lock);
5476
5477                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5478                                         NLP_EVT_RCV_PLOGI);
5479
5480                 break;
5481         case ELS_CMD_FLOGI:
5482                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5483                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
5484                         did, vport->port_state, ndlp->nlp_flag);
5485
5486                 phba->fc_stat.elsRcvFLOGI++;
5487                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
5488                 if (newnode)
5489                         lpfc_nlp_put(ndlp);
5490                 break;
5491         case ELS_CMD_LOGO:
5492                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5493                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
5494                         did, vport->port_state, ndlp->nlp_flag);
5495
5496                 phba->fc_stat.elsRcvLOGO++;
5497                 lpfc_send_els_event(vport, ndlp, payload);
5498                 if (vport->port_state < LPFC_DISC_AUTH) {
5499                         rjt_err = LSRJT_UNABLE_TPC;
5500                         break;
5501                 }
5502                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
5503                 break;
5504         case ELS_CMD_PRLO:
5505                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5506                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
5507                         did, vport->port_state, ndlp->nlp_flag);
5508
5509                 phba->fc_stat.elsRcvPRLO++;
5510                 lpfc_send_els_event(vport, ndlp, payload);
5511                 if (vport->port_state < LPFC_DISC_AUTH) {
5512                         rjt_err = LSRJT_UNABLE_TPC;
5513                         break;
5514                 }
5515                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
5516                 break;
5517         case ELS_CMD_RSCN:
5518                 phba->fc_stat.elsRcvRSCN++;
5519                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
5520                 if (newnode)
5521                         lpfc_nlp_put(ndlp);
5522                 break;
5523         case ELS_CMD_ADISC:
5524                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5525                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
5526                         did, vport->port_state, ndlp->nlp_flag);
5527
5528                 lpfc_send_els_event(vport, ndlp, payload);
5529                 phba->fc_stat.elsRcvADISC++;
5530                 if (vport->port_state < LPFC_DISC_AUTH) {
5531                         rjt_err = LSRJT_UNABLE_TPC;
5532                         break;
5533                 }
5534                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5535                                         NLP_EVT_RCV_ADISC);
5536                 break;
5537         case ELS_CMD_PDISC:
5538                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5539                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
5540                         did, vport->port_state, ndlp->nlp_flag);
5541
5542                 phba->fc_stat.elsRcvPDISC++;
5543                 if (vport->port_state < LPFC_DISC_AUTH) {
5544                         rjt_err = LSRJT_UNABLE_TPC;
5545                         break;
5546                 }
5547                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5548                                         NLP_EVT_RCV_PDISC);
5549                 break;
5550         case ELS_CMD_FARPR:
5551                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5552                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
5553                         did, vport->port_state, ndlp->nlp_flag);
5554
5555                 phba->fc_stat.elsRcvFARPR++;
5556                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
5557                 break;
5558         case ELS_CMD_FARP:
5559                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5560                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
5561                         did, vport->port_state, ndlp->nlp_flag);
5562
5563                 phba->fc_stat.elsRcvFARP++;
5564                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
5565                 break;
5566         case ELS_CMD_FAN:
5567                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5568                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
5569                         did, vport->port_state, ndlp->nlp_flag);
5570
5571                 phba->fc_stat.elsRcvFAN++;
5572                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
5573                 break;
5574         case ELS_CMD_PRLI:
5575                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5576                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
5577                         did, vport->port_state, ndlp->nlp_flag);
5578
5579                 phba->fc_stat.elsRcvPRLI++;
5580                 if (vport->port_state < LPFC_DISC_AUTH) {
5581                         rjt_err = LSRJT_UNABLE_TPC;
5582                         break;
5583                 }
5584                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
5585                 break;
5586         case ELS_CMD_LIRR:
5587                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5588                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
5589                         did, vport->port_state, ndlp->nlp_flag);
5590
5591                 phba->fc_stat.elsRcvLIRR++;
5592                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
5593                 if (newnode)
5594                         lpfc_nlp_put(ndlp);
5595                 break;
5596         case ELS_CMD_RPS:
5597                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5598                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
5599                         did, vport->port_state, ndlp->nlp_flag);
5600
5601                 phba->fc_stat.elsRcvRPS++;
5602                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
5603                 if (newnode)
5604                         lpfc_nlp_put(ndlp);
5605                 break;
5606         case ELS_CMD_RPL:
5607                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5608                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
5609                         did, vport->port_state, ndlp->nlp_flag);
5610
5611                 phba->fc_stat.elsRcvRPL++;
5612                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
5613                 if (newnode)
5614                         lpfc_nlp_put(ndlp);
5615                 break;
5616         case ELS_CMD_RNID:
5617                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5618                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
5619                         did, vport->port_state, ndlp->nlp_flag);
5620
5621                 phba->fc_stat.elsRcvRNID++;
5622                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
5623                 if (newnode)
5624                         lpfc_nlp_put(ndlp);
5625                 break;
5626         default:
5627                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5628                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
5629                         cmd, did, vport->port_state);
5630
5631                 /* Unsupported ELS command, reject */
5632                 rjt_err = LSRJT_INVALID_CMD;
5633
5634                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
5635                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5636                                  "0115 Unknown ELS command x%x "
5637                                  "received from NPORT x%x\n", cmd, did);
5638                 if (newnode)
5639                         lpfc_nlp_put(ndlp);
5640                 break;
5641         }
5642
5643         /* check if need to LS_RJT received ELS cmd */
5644         if (rjt_err) {
5645                 memset(&stat, 0, sizeof(stat));
5646                 stat.un.b.lsRjtRsnCode = rjt_err;
5647                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
5648                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
5649                         NULL);
5650         }
5651
5652         lpfc_nlp_put(elsiocb->context1);
5653         elsiocb->context1 = NULL;
5654         return;
5655
5656 dropit:
5657         if (vport && !(vport->load_flag & FC_UNLOADING))
5658                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5659                         "0111 Dropping received ELS cmd "
5660                         "Data: x%x x%x x%x\n",
5661                         icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
5662         phba->fc_stat.elsRcvDrop++;
5663 }
5664
5665 /**
5666  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5667  * @phba: pointer to lpfc hba data structure.
5668  * @vpi: host virtual N_Port identifier.
5669  *
5670  * This routine finds a vport on a HBA (referred by @phba) through a
5671  * @vpi. The function walks the HBA's vport list and returns the address
5672  * of the vport with the matching @vpi.
5673  *
5674  * Return code
5675  *    NULL - No vport with the matching @vpi found
5676  *    Otherwise - Address to the vport with the matching @vpi.
5677  **/
5678 struct lpfc_vport *
5679 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5680 {
5681         struct lpfc_vport *vport;
5682         unsigned long flags;
5683
5684         spin_lock_irqsave(&phba->hbalock, flags);
5685         list_for_each_entry(vport, &phba->port_list, listentry) {
5686                 if (vport->vpi == vpi) {
5687                         spin_unlock_irqrestore(&phba->hbalock, flags);
5688                         return vport;
5689                 }
5690         }
5691         spin_unlock_irqrestore(&phba->hbalock, flags);
5692         return NULL;
5693 }
5694
5695 /**
5696  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
5697  * @phba: pointer to lpfc hba data structure.
5698  * @pring: pointer to a SLI ring.
5699  * @elsiocb: pointer to lpfc els iocb data structure.
5700  *
5701  * This routine is used to process an unsolicited event received from a SLI
5702  * (Service Level Interface) ring. The actual processing of the data buffer
5703  * associated with the unsolicited event is done by invoking the routine
5704  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
5705  * SLI ring on which the unsolicited event was received.
5706  **/
5707 void
5708 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5709                      struct lpfc_iocbq *elsiocb)
5710 {
5711         struct lpfc_vport *vport = phba->pport;
5712         IOCB_t *icmd = &elsiocb->iocb;
5713         dma_addr_t paddr;
5714         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
5715         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
5716
5717         elsiocb->context1 = NULL;
5718         elsiocb->context2 = NULL;
5719         elsiocb->context3 = NULL;
5720
5721         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
5722                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
5723         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
5724             (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
5725                 phba->fc_stat.NoRcvBuf++;
5726                 /* Not enough posted buffers; Try posting more buffers */
5727                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
5728                         lpfc_post_buffer(phba, pring, 0);
5729                 return;
5730         }
5731
5732         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5733             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
5734              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
5735                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
5736                         vport = phba->pport;
5737                 else
5738                         vport = lpfc_find_vport_by_vpid(phba,
5739                                 icmd->unsli3.rcvsli3.vpi - phba->vpi_base);
5740         }
5741         /* If there are no BDEs associated
5742          * with this IOCB, there is nothing to do.
5743          */
5744         if (icmd->ulpBdeCount == 0)
5745                 return;
5746
5747         /* type of ELS cmd is first 32bit word
5748          * in packet
5749          */
5750         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5751                 elsiocb->context2 = bdeBuf1;
5752         } else {
5753                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
5754                                  icmd->un.cont64[0].addrLow);
5755                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
5756                                                              paddr);
5757         }
5758
5759         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5760         /*
5761          * The different unsolicited event handlers would tell us
5762          * if they are done with "mp" by setting context2 to NULL.
5763          */
5764         if (elsiocb->context2) {
5765                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
5766                 elsiocb->context2 = NULL;
5767         }
5768
5769         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
5770         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
5771             icmd->ulpBdeCount == 2) {
5772                 elsiocb->context2 = bdeBuf2;
5773                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5774                 /* free mp if we are done with it */
5775                 if (elsiocb->context2) {
5776                         lpfc_in_buf_free(phba, elsiocb->context2);
5777                         elsiocb->context2 = NULL;
5778                 }
5779         }
5780 }
5781
5782 /**
5783  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
5784  * @phba: pointer to lpfc hba data structure.
5785  * @vport: pointer to a virtual N_Port data structure.
5786  *
5787  * This routine issues a Port Login (PLOGI) to the Name Server with
5788  * State Change Request (SCR) for a @vport. This routine will create an
5789  * ndlp for the Name Server associated to the @vport if such node does
5790  * not already exist. The PLOGI to Name Server is issued by invoking the
5791  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
5792  * (FDMI) is configured to the @vport, a FDMI node will be created and
5793  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
5794  **/
5795 void
5796 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5797 {
5798         struct lpfc_nodelist *ndlp, *ndlp_fdmi;
5799
5800         ndlp = lpfc_findnode_did(vport, NameServer_DID);
5801         if (!ndlp) {
5802                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5803                 if (!ndlp) {
5804                         if (phba->fc_topology == TOPOLOGY_LOOP) {
5805                                 lpfc_disc_start(vport);
5806                                 return;
5807                         }
5808                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5809                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5810                                          "0251 NameServer login: no memory\n");
5811                         return;
5812                 }
5813                 lpfc_nlp_init(vport, ndlp, NameServer_DID);
5814         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5815                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5816                 if (!ndlp) {
5817                         if (phba->fc_topology == TOPOLOGY_LOOP) {
5818                                 lpfc_disc_start(vport);
5819                                 return;
5820                         }
5821                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5822                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5823                                         "0348 NameServer login: node freed\n");
5824                         return;
5825                 }
5826         }
5827         ndlp->nlp_type |= NLP_FABRIC;
5828
5829         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5830
5831         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
5832                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5833                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5834                                  "0252 Cannot issue NameServer login\n");
5835                 return;
5836         }
5837
5838         if (vport->cfg_fdmi_on) {
5839                 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
5840                                           GFP_KERNEL);
5841                 if (ndlp_fdmi) {
5842                         lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
5843                         ndlp_fdmi->nlp_type |= NLP_FABRIC;
5844                         lpfc_nlp_set_state(vport, ndlp_fdmi,
5845                                 NLP_STE_PLOGI_ISSUE);
5846                         lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
5847                                              0);
5848                 }
5849         }
5850         return;
5851 }
5852
5853 /**
5854  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
5855  * @phba: pointer to lpfc hba data structure.
5856  * @pmb: pointer to the driver internal queue element for mailbox command.
5857  *
5858  * This routine is the completion callback function to register new vport
5859  * mailbox command. If the new vport mailbox command completes successfully,
5860  * the fabric registration login shall be performed on physical port (the
5861  * new vport created is actually a physical port, with VPI 0) or the port
5862  * login to Name Server for State Change Request (SCR) will be performed
5863  * on virtual port (real virtual port, with VPI greater than 0).
5864  **/
5865 static void
5866 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5867 {
5868         struct lpfc_vport *vport = pmb->vport;
5869         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
5870         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5871         MAILBOX_t *mb = &pmb->u.mb;
5872
5873         spin_lock_irq(shost->host_lock);
5874         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
5875         spin_unlock_irq(shost->host_lock);
5876
5877         if (mb->mbxStatus) {
5878                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5879                                  "0915 Register VPI failed: 0x%x\n",
5880                                  mb->mbxStatus);
5881
5882                 switch (mb->mbxStatus) {
5883                 case 0x11:      /* unsupported feature */
5884                 case 0x9603:    /* max_vpi exceeded */
5885                 case 0x9602:    /* Link event since CLEAR_LA */
5886                         /* giving up on vport registration */
5887                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5888                         spin_lock_irq(shost->host_lock);
5889                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
5890                         spin_unlock_irq(shost->host_lock);
5891                         lpfc_can_disctmo(vport);
5892                         break;
5893                 default:
5894                         /* Try to recover from this error */
5895                         lpfc_mbx_unreg_vpi(vport);
5896                         spin_lock_irq(shost->host_lock);
5897                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
5898                         spin_unlock_irq(shost->host_lock);
5899                         if (vport->port_type == LPFC_PHYSICAL_PORT)
5900                                 lpfc_initial_flogi(vport);
5901                         else
5902                                 lpfc_initial_fdisc(vport);
5903                         break;
5904                 }
5905
5906         } else {
5907                 if (vport == phba->pport)
5908                         if (phba->sli_rev < LPFC_SLI_REV4)
5909                                 lpfc_issue_fabric_reglogin(vport);
5910                         else
5911                                 lpfc_issue_reg_vfi(vport);
5912                 else
5913                         lpfc_do_scr_ns_plogi(phba, vport);
5914         }
5915
5916         /* Now, we decrement the ndlp reference count held for this
5917          * callback function
5918          */
5919         lpfc_nlp_put(ndlp);
5920
5921         mempool_free(pmb, phba->mbox_mem_pool);
5922         return;
5923 }
5924
5925 /**
5926  * lpfc_register_new_vport - Register a new vport with a HBA
5927  * @phba: pointer to lpfc hba data structure.
5928  * @vport: pointer to a host virtual N_Port data structure.
5929  * @ndlp: pointer to a node-list data structure.
5930  *
5931  * This routine registers the @vport as a new virtual port with a HBA.
5932  * It is done through a registering vpi mailbox command.
5933  **/
5934 static void
5935 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
5936                         struct lpfc_nodelist *ndlp)
5937 {
5938         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5939         LPFC_MBOXQ_t *mbox;
5940
5941         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5942         if (mbox) {
5943                 lpfc_reg_vpi(vport, mbox);
5944                 mbox->vport = vport;
5945                 mbox->context2 = lpfc_nlp_get(ndlp);
5946                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
5947                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5948                     == MBX_NOT_FINISHED) {
5949                         /* mailbox command not success, decrement ndlp
5950                          * reference count for this command
5951                          */
5952                         lpfc_nlp_put(ndlp);
5953                         mempool_free(mbox, phba->mbox_mem_pool);
5954
5955                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5956                                 "0253 Register VPI: Can't send mbox\n");
5957                         goto mbox_err_exit;
5958                 }
5959         } else {
5960                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5961                                  "0254 Register VPI: no memory\n");
5962                 goto mbox_err_exit;
5963         }
5964         return;
5965
5966 mbox_err_exit:
5967         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5968         spin_lock_irq(shost->host_lock);
5969         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
5970         spin_unlock_irq(shost->host_lock);
5971         return;
5972 }
5973
5974 /**
5975  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
5976  * @phba: pointer to lpfc hba data structure.
5977  * @cmdiocb: pointer to lpfc command iocb data structure.
5978  * @rspiocb: pointer to lpfc response iocb data structure.
5979  *
5980  * This routine is the completion callback function to a Fabric Discover
5981  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
5982  * single threaded, each FDISC completion callback function will reset
5983  * the discovery timer for all vports such that the timers will not get
5984  * unnecessary timeout. The function checks the FDISC IOCB status. If error
5985  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
5986  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
5987  * assigned to the vport has been changed with the completion of the FDISC
5988  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
5989  * are unregistered from the HBA, and then the lpfc_register_new_vport()
5990  * routine is invoked to register new vport with the HBA. Otherwise, the
5991  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
5992  * Server for State Change Request (SCR).
5993  **/
5994 static void
5995 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5996                     struct lpfc_iocbq *rspiocb)
5997 {
5998         struct lpfc_vport *vport = cmdiocb->vport;
5999         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
6000         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
6001         struct lpfc_nodelist *np;
6002         struct lpfc_nodelist *next_np;
6003         IOCB_t *irsp = &rspiocb->iocb;
6004         struct lpfc_iocbq *piocb;
6005
6006         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6007                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
6008                          irsp->ulpStatus, irsp->un.ulpWord[4],
6009                          vport->fc_prevDID);
6010         /* Since all FDISCs are being single threaded, we
6011          * must reset the discovery timer for ALL vports
6012          * waiting to send FDISC when one completes.
6013          */
6014         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
6015                 lpfc_set_disctmo(piocb->vport);
6016         }
6017
6018         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6019                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
6020                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
6021
6022         if (irsp->ulpStatus) {
6023                 /* Check for retry */
6024                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
6025                         goto out;
6026                 /* FDISC failed */
6027                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6028                                  "0126 FDISC failed. (%d/%d)\n",
6029                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
6030                 goto fdisc_failed;
6031         }
6032                 if (vport->fc_vport->vport_state == FC_VPORT_INITIALIZING)
6033                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6034                 lpfc_nlp_put(ndlp);
6035                 /* giving up on FDISC. Cancel discovery timer */
6036                 lpfc_can_disctmo(vport);
6037         spin_lock_irq(shost->host_lock);
6038         vport->fc_flag |= FC_FABRIC;
6039         if (vport->phba->fc_topology == TOPOLOGY_LOOP)
6040                 vport->fc_flag |=  FC_PUBLIC_LOOP;
6041         spin_unlock_irq(shost->host_lock);
6042
6043         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
6044         lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
6045         if ((vport->fc_prevDID != vport->fc_myDID) &&
6046                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
6047                 /* If our NportID changed, we need to ensure all
6048                  * remaining NPORTs get unreg_login'ed so we can
6049                  * issue unreg_vpi.
6050                  */
6051                 list_for_each_entry_safe(np, next_np,
6052                         &vport->fc_nodes, nlp_listp) {
6053                         if (!NLP_CHK_NODE_ACT(ndlp) ||
6054                             (np->nlp_state != NLP_STE_NPR_NODE) ||
6055                             !(np->nlp_flag & NLP_NPR_ADISC))
6056                                 continue;
6057                         spin_lock_irq(shost->host_lock);
6058                         np->nlp_flag &= ~NLP_NPR_ADISC;
6059                         spin_unlock_irq(shost->host_lock);
6060                         lpfc_unreg_rpi(vport, np);
6061                 }
6062                 lpfc_mbx_unreg_vpi(vport);
6063                 spin_lock_irq(shost->host_lock);
6064                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6065                 spin_unlock_irq(shost->host_lock);
6066         }
6067
6068         if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
6069                 lpfc_register_new_vport(phba, vport, ndlp);
6070         else
6071                 lpfc_do_scr_ns_plogi(phba, vport);
6072         goto out;
6073 fdisc_failed:
6074         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6075         /* Cancel discovery timer */
6076         lpfc_can_disctmo(vport);
6077         lpfc_nlp_put(ndlp);
6078 out:
6079         lpfc_els_free_iocb(phba, cmdiocb);
6080 }
6081
6082 /**
6083  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
6084  * @vport: pointer to a virtual N_Port data structure.
6085  * @ndlp: pointer to a node-list data structure.
6086  * @retry: number of retries to the command IOCB.
6087  *
6088  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
6089  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
6090  * routine to issue the IOCB, which makes sure only one outstanding fabric
6091  * IOCB will be sent off HBA at any given time.
6092  *
6093  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6094  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6095  * will be stored into the context1 field of the IOCB for the completion
6096  * callback function to the FDISC ELS command.
6097  *
6098  * Return code
6099  *   0 - Successfully issued fdisc iocb command
6100  *   1 - Failed to issue fdisc iocb command
6101  **/
6102 static int
6103 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
6104                      uint8_t retry)
6105 {
6106         struct lpfc_hba *phba = vport->phba;
6107         IOCB_t *icmd;
6108         struct lpfc_iocbq *elsiocb;
6109         struct serv_parm *sp;
6110         uint8_t *pcmd;
6111         uint16_t cmdsize;
6112         int did = ndlp->nlp_DID;
6113         int rc;
6114
6115         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
6116         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
6117                                      ELS_CMD_FDISC);
6118         if (!elsiocb) {
6119                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6120                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6121                                  "0255 Issue FDISC: no IOCB\n");
6122                 return 1;
6123         }
6124
6125         icmd = &elsiocb->iocb;
6126         icmd->un.elsreq64.myID = 0;
6127         icmd->un.elsreq64.fl = 1;
6128
6129         if  (phba->sli_rev == LPFC_SLI_REV4) {
6130                 /* FDISC needs to be 1 for WQE VPI */
6131                 elsiocb->iocb.ulpCt_h = (SLI4_CT_VPI >> 1) & 1;
6132                 elsiocb->iocb.ulpCt_l = SLI4_CT_VPI & 1 ;
6133                 /* Set the ulpContext to the vpi */
6134                 elsiocb->iocb.ulpContext = vport->vpi + phba->vpi_base;
6135         } else {
6136                 /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
6137                 icmd->ulpCt_h = 1;
6138                 icmd->ulpCt_l = 0;
6139         }
6140
6141         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6142         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
6143         pcmd += sizeof(uint32_t); /* CSP Word 1 */
6144         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
6145         sp = (struct serv_parm *) pcmd;
6146         /* Setup CSPs accordingly for Fabric */
6147         sp->cmn.e_d_tov = 0;
6148         sp->cmn.w2.r_a_tov = 0;
6149         sp->cls1.classValid = 0;
6150         sp->cls2.seqDelivery = 1;
6151         sp->cls3.seqDelivery = 1;
6152
6153         pcmd += sizeof(uint32_t); /* CSP Word 2 */
6154         pcmd += sizeof(uint32_t); /* CSP Word 3 */
6155         pcmd += sizeof(uint32_t); /* CSP Word 4 */
6156         pcmd += sizeof(uint32_t); /* Port Name */
6157         memcpy(pcmd, &vport->fc_portname, 8);
6158         pcmd += sizeof(uint32_t); /* Node Name */
6159         pcmd += sizeof(uint32_t); /* Node Name */
6160         memcpy(pcmd, &vport->fc_nodename, 8);
6161
6162         lpfc_set_disctmo(vport);
6163
6164         phba->fc_stat.elsXmitFDISC++;
6165         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
6166
6167         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6168                 "Issue FDISC:     did:x%x",
6169                 did, 0, 0);
6170
6171         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
6172         if (rc == IOCB_ERROR) {
6173                 lpfc_els_free_iocb(phba, elsiocb);
6174                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6175                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6176                                  "0256 Issue FDISC: Cannot send IOCB\n");
6177                 return 1;
6178         }
6179         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
6180         vport->port_state = LPFC_FDISC;
6181         return 0;
6182 }
6183
6184 /**
6185  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
6186  * @phba: pointer to lpfc hba data structure.
6187  * @cmdiocb: pointer to lpfc command iocb data structure.
6188  * @rspiocb: pointer to lpfc response iocb data structure.
6189  *
6190  * This routine is the completion callback function to the issuing of a LOGO
6191  * ELS command off a vport. It frees the command IOCB and then decrement the
6192  * reference count held on ndlp for this completion function, indicating that
6193  * the reference to the ndlp is no long needed. Note that the
6194  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
6195  * callback function and an additional explicit ndlp reference decrementation
6196  * will trigger the actual release of the ndlp.
6197  **/
6198 static void
6199 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6200                         struct lpfc_iocbq *rspiocb)
6201 {
6202         struct lpfc_vport *vport = cmdiocb->vport;
6203         IOCB_t *irsp;
6204         struct lpfc_nodelist *ndlp;
6205         ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
6206
6207         irsp = &rspiocb->iocb;
6208         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6209                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
6210                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
6211
6212         lpfc_els_free_iocb(phba, cmdiocb);
6213         vport->unreg_vpi_cmpl = VPORT_ERROR;
6214
6215         /* Trigger the release of the ndlp after logo */
6216         lpfc_nlp_put(ndlp);
6217 }
6218
6219 /**
6220  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
6221  * @vport: pointer to a virtual N_Port data structure.
6222  * @ndlp: pointer to a node-list data structure.
6223  *
6224  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
6225  *
6226  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6227  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6228  * will be stored into the context1 field of the IOCB for the completion
6229  * callback function to the LOGO ELS command.
6230  *
6231  * Return codes
6232  *   0 - Successfully issued logo off the @vport
6233  *   1 - Failed to issue logo off the @vport
6234  **/
6235 int
6236 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
6237 {
6238         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6239         struct lpfc_hba  *phba = vport->phba;
6240         IOCB_t *icmd;
6241         struct lpfc_iocbq *elsiocb;
6242         uint8_t *pcmd;
6243         uint16_t cmdsize;
6244
6245         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
6246         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
6247                                      ELS_CMD_LOGO);
6248         if (!elsiocb)
6249                 return 1;
6250
6251         icmd = &elsiocb->iocb;
6252         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6253         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
6254         pcmd += sizeof(uint32_t);
6255
6256         /* Fill in LOGO payload */
6257         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
6258         pcmd += sizeof(uint32_t);
6259         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
6260
6261         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6262                 "Issue LOGO npiv  did:x%x flg:x%x",
6263                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
6264
6265         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
6266         spin_lock_irq(shost->host_lock);
6267         ndlp->nlp_flag |= NLP_LOGO_SND;
6268         spin_unlock_irq(shost->host_lock);
6269         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
6270             IOCB_ERROR) {
6271                 spin_lock_irq(shost->host_lock);
6272                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
6273                 spin_unlock_irq(shost->host_lock);
6274                 lpfc_els_free_iocb(phba, elsiocb);
6275                 return 1;
6276         }
6277         return 0;
6278 }
6279
6280 /**
6281  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
6282  * @ptr: holder for the timer function associated data.
6283  *
6284  * This routine is invoked by the fabric iocb block timer after
6285  * timeout. It posts the fabric iocb block timeout event by setting the
6286  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
6287  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
6288  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
6289  * posted event WORKER_FABRIC_BLOCK_TMO.
6290  **/
6291 void
6292 lpfc_fabric_block_timeout(unsigned long ptr)
6293 {
6294         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
6295         unsigned long iflags;
6296         uint32_t tmo_posted;
6297
6298         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
6299         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
6300         if (!tmo_posted)
6301                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
6302         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
6303
6304         if (!tmo_posted)
6305                 lpfc_worker_wake_up(phba);
6306         return;
6307 }
6308
6309 /**
6310  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
6311  * @phba: pointer to lpfc hba data structure.
6312  *
6313  * This routine issues one fabric iocb from the driver internal list to
6314  * the HBA. It first checks whether it's ready to issue one fabric iocb to
6315  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
6316  * remove one pending fabric iocb from the driver internal list and invokes
6317  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
6318  **/
6319 static void
6320 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
6321 {
6322         struct lpfc_iocbq *iocb;
6323         unsigned long iflags;
6324         int ret;
6325         IOCB_t *cmd;
6326
6327 repeat:
6328         iocb = NULL;
6329         spin_lock_irqsave(&phba->hbalock, iflags);
6330         /* Post any pending iocb to the SLI layer */
6331         if (atomic_read(&phba->fabric_iocb_count) == 0) {
6332                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
6333                                  list);
6334                 if (iocb)
6335                         /* Increment fabric iocb count to hold the position */
6336                         atomic_inc(&phba->fabric_iocb_count);
6337         }
6338         spin_unlock_irqrestore(&phba->hbalock, iflags);
6339         if (iocb) {
6340                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6341                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6342                 iocb->iocb_flag |= LPFC_IO_FABRIC;
6343
6344                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6345                         "Fabric sched1:   ste:x%x",
6346                         iocb->vport->port_state, 0, 0);
6347
6348                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
6349
6350                 if (ret == IOCB_ERROR) {
6351                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6352                         iocb->fabric_iocb_cmpl = NULL;
6353                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6354                         cmd = &iocb->iocb;
6355                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6356                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6357                         iocb->iocb_cmpl(phba, iocb, iocb);
6358
6359                         atomic_dec(&phba->fabric_iocb_count);
6360                         goto repeat;
6361                 }
6362         }
6363
6364         return;
6365 }
6366
6367 /**
6368  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
6369  * @phba: pointer to lpfc hba data structure.
6370  *
6371  * This routine unblocks the  issuing fabric iocb command. The function
6372  * will clear the fabric iocb block bit and then invoke the routine
6373  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
6374  * from the driver internal fabric iocb list.
6375  **/
6376 void
6377 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
6378 {
6379         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6380
6381         lpfc_resume_fabric_iocbs(phba);
6382         return;
6383 }
6384
6385 /**
6386  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
6387  * @phba: pointer to lpfc hba data structure.
6388  *
6389  * This routine blocks the issuing fabric iocb for a specified amount of
6390  * time (currently 100 ms). This is done by set the fabric iocb block bit
6391  * and set up a timeout timer for 100ms. When the block bit is set, no more
6392  * fabric iocb will be issued out of the HBA.
6393  **/
6394 static void
6395 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
6396 {
6397         int blocked;
6398
6399         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6400         /* Start a timer to unblock fabric iocbs after 100ms */
6401         if (!blocked)
6402                 mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
6403
6404         return;
6405 }
6406
6407 /**
6408  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
6409  * @phba: pointer to lpfc hba data structure.
6410  * @cmdiocb: pointer to lpfc command iocb data structure.
6411  * @rspiocb: pointer to lpfc response iocb data structure.
6412  *
6413  * This routine is the callback function that is put to the fabric iocb's
6414  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
6415  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
6416  * function first restores and invokes the original iocb's callback function
6417  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
6418  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
6419  **/
6420 static void
6421 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6422         struct lpfc_iocbq *rspiocb)
6423 {
6424         struct ls_rjt stat;
6425
6426         if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
6427                 BUG();
6428
6429         switch (rspiocb->iocb.ulpStatus) {
6430                 case IOSTAT_NPORT_RJT:
6431                 case IOSTAT_FABRIC_RJT:
6432                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
6433                                 lpfc_block_fabric_iocbs(phba);
6434                         }
6435                         break;
6436
6437                 case IOSTAT_NPORT_BSY:
6438                 case IOSTAT_FABRIC_BSY:
6439                         lpfc_block_fabric_iocbs(phba);
6440                         break;
6441
6442                 case IOSTAT_LS_RJT:
6443                         stat.un.lsRjtError =
6444                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
6445                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
6446                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
6447                                 lpfc_block_fabric_iocbs(phba);
6448                         break;
6449         }
6450
6451         if (atomic_read(&phba->fabric_iocb_count) == 0)
6452                 BUG();
6453
6454         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
6455         cmdiocb->fabric_iocb_cmpl = NULL;
6456         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
6457         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
6458
6459         atomic_dec(&phba->fabric_iocb_count);
6460         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
6461                 /* Post any pending iocbs to HBA */
6462                 lpfc_resume_fabric_iocbs(phba);
6463         }
6464 }
6465
6466 /**
6467  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
6468  * @phba: pointer to lpfc hba data structure.
6469  * @iocb: pointer to lpfc command iocb data structure.
6470  *
6471  * This routine is used as the top-level API for issuing a fabric iocb command
6472  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
6473  * function makes sure that only one fabric bound iocb will be outstanding at
6474  * any given time. As such, this function will first check to see whether there
6475  * is already an outstanding fabric iocb on the wire. If so, it will put the
6476  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
6477  * issued later. Otherwise, it will issue the iocb on the wire and update the
6478  * fabric iocb count it indicate that there is one fabric iocb on the wire.
6479  *
6480  * Note, this implementation has a potential sending out fabric IOCBs out of
6481  * order. The problem is caused by the construction of the "ready" boolen does
6482  * not include the condition that the internal fabric IOCB list is empty. As
6483  * such, it is possible a fabric IOCB issued by this routine might be "jump"
6484  * ahead of the fabric IOCBs in the internal list.
6485  *
6486  * Return code
6487  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
6488  *   IOCB_ERROR - failed to issue fabric iocb
6489  **/
6490 static int
6491 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
6492 {
6493         unsigned long iflags;
6494         int ready;
6495         int ret;
6496
6497         if (atomic_read(&phba->fabric_iocb_count) > 1)
6498                 BUG();
6499
6500         spin_lock_irqsave(&phba->hbalock, iflags);
6501         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
6502                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6503
6504         if (ready)
6505                 /* Increment fabric iocb count to hold the position */
6506                 atomic_inc(&phba->fabric_iocb_count);
6507         spin_unlock_irqrestore(&phba->hbalock, iflags);
6508         if (ready) {
6509                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6510                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6511                 iocb->iocb_flag |= LPFC_IO_FABRIC;
6512
6513                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6514                         "Fabric sched2:   ste:x%x",
6515                         iocb->vport->port_state, 0, 0);
6516
6517                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
6518
6519                 if (ret == IOCB_ERROR) {
6520                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6521                         iocb->fabric_iocb_cmpl = NULL;
6522                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6523                         atomic_dec(&phba->fabric_iocb_count);
6524                 }
6525         } else {
6526                 spin_lock_irqsave(&phba->hbalock, iflags);
6527                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
6528                 spin_unlock_irqrestore(&phba->hbalock, iflags);
6529                 ret = IOCB_SUCCESS;
6530         }
6531         return ret;
6532 }
6533
6534 /**
6535  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
6536  * @vport: pointer to a virtual N_Port data structure.
6537  *
6538  * This routine aborts all the IOCBs associated with a @vport from the
6539  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6540  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6541  * list, removes each IOCB associated with the @vport off the list, set the
6542  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6543  * associated with the IOCB.
6544  **/
6545 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
6546 {
6547         LIST_HEAD(completions);
6548         struct lpfc_hba  *phba = vport->phba;
6549         struct lpfc_iocbq *tmp_iocb, *piocb;
6550
6551         spin_lock_irq(&phba->hbalock);
6552         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6553                                  list) {
6554
6555                 if (piocb->vport != vport)
6556                         continue;
6557
6558                 list_move_tail(&piocb->list, &completions);
6559         }
6560         spin_unlock_irq(&phba->hbalock);
6561
6562         /* Cancel all the IOCBs from the completions list */
6563         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6564                               IOERR_SLI_ABORTED);
6565 }
6566
6567 /**
6568  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
6569  * @ndlp: pointer to a node-list data structure.
6570  *
6571  * This routine aborts all the IOCBs associated with an @ndlp from the
6572  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6573  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6574  * list, removes each IOCB associated with the @ndlp off the list, set the
6575  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6576  * associated with the IOCB.
6577  **/
6578 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
6579 {
6580         LIST_HEAD(completions);
6581         struct lpfc_hba  *phba = ndlp->phba;
6582         struct lpfc_iocbq *tmp_iocb, *piocb;
6583         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6584
6585         spin_lock_irq(&phba->hbalock);
6586         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6587                                  list) {
6588                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
6589
6590                         list_move_tail(&piocb->list, &completions);
6591                 }
6592         }
6593         spin_unlock_irq(&phba->hbalock);
6594
6595         /* Cancel all the IOCBs from the completions list */
6596         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6597                               IOERR_SLI_ABORTED);
6598 }
6599
6600 /**
6601  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
6602  * @phba: pointer to lpfc hba data structure.
6603  *
6604  * This routine aborts all the IOCBs currently on the driver internal
6605  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
6606  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
6607  * list, removes IOCBs off the list, set the status feild to
6608  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
6609  * the IOCB.
6610  **/
6611 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
6612 {
6613         LIST_HEAD(completions);
6614
6615         spin_lock_irq(&phba->hbalock);
6616         list_splice_init(&phba->fabric_iocb_list, &completions);
6617         spin_unlock_irq(&phba->hbalock);
6618
6619         /* Cancel all the IOCBs from the completions list */
6620         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6621                               IOERR_SLI_ABORTED);
6622 }
6623
6624 /**
6625  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
6626  * @phba: pointer to lpfc hba data structure.
6627  * @axri: pointer to the els xri abort wcqe structure.
6628  *
6629  * This routine is invoked by the worker thread to process a SLI4 slow-path
6630  * ELS aborted xri.
6631  **/
6632 void
6633 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
6634                           struct sli4_wcqe_xri_aborted *axri)
6635 {
6636         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
6637         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6638         unsigned long iflag = 0;
6639
6640         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock, iflag);
6641         list_for_each_entry_safe(sglq_entry, sglq_next,
6642                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
6643                 if (sglq_entry->sli4_xritag == xri) {
6644                         list_del(&sglq_entry->list);
6645                         spin_unlock_irqrestore(
6646                                         &phba->sli4_hba.abts_sgl_list_lock,
6647                                          iflag);
6648                         spin_lock_irqsave(&phba->hbalock, iflag);
6649
6650                         list_add_tail(&sglq_entry->list,
6651                                 &phba->sli4_hba.lpfc_sgl_list);
6652                         spin_unlock_irqrestore(&phba->hbalock, iflag);
6653                         return;
6654                 }
6655         }
6656         spin_unlock_irqrestore(&phba->sli4_hba.abts_sgl_list_lock, iflag);
6657 }