]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/lpfc/lpfc_nportdisc.c
Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[karo-tx-linux.git] / drivers / scsi / lpfc / lpfc_nportdisc.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
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_sli4.h"
36 #include "lpfc_nl.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43 #include "lpfc_debugfs.h"
44
45
46 /* Called to verify a rcv'ed ADISC was intended for us. */
47 static int
48 lpfc_check_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
49                  struct lpfc_name *nn, struct lpfc_name *pn)
50 {
51         /* Compare the ADISC rsp WWNN / WWPN matches our internal node
52          * table entry for that node.
53          */
54         if (memcmp(nn, &ndlp->nlp_nodename, sizeof (struct lpfc_name)))
55                 return 0;
56
57         if (memcmp(pn, &ndlp->nlp_portname, sizeof (struct lpfc_name)))
58                 return 0;
59
60         /* we match, return success */
61         return 1;
62 }
63
64 int
65 lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
66                  struct serv_parm *sp, uint32_t class, int flogi)
67 {
68         volatile struct serv_parm *hsp = &vport->fc_sparam;
69         uint16_t hsp_value, ssp_value = 0;
70
71         /*
72          * The receive data field size and buffer-to-buffer receive data field
73          * size entries are 16 bits but are represented as two 8-bit fields in
74          * the driver data structure to account for rsvd bits and other control
75          * bits.  Reconstruct and compare the fields as a 16-bit values before
76          * correcting the byte values.
77          */
78         if (sp->cls1.classValid) {
79                 if (!flogi) {
80                         hsp_value = ((hsp->cls1.rcvDataSizeMsb << 8) |
81                                      hsp->cls1.rcvDataSizeLsb);
82                         ssp_value = ((sp->cls1.rcvDataSizeMsb << 8) |
83                                      sp->cls1.rcvDataSizeLsb);
84                         if (!ssp_value)
85                                 goto bad_service_param;
86                         if (ssp_value > hsp_value) {
87                                 sp->cls1.rcvDataSizeLsb =
88                                         hsp->cls1.rcvDataSizeLsb;
89                                 sp->cls1.rcvDataSizeMsb =
90                                         hsp->cls1.rcvDataSizeMsb;
91                         }
92                 }
93         } else if (class == CLASS1)
94                 goto bad_service_param;
95         if (sp->cls2.classValid) {
96                 if (!flogi) {
97                         hsp_value = ((hsp->cls2.rcvDataSizeMsb << 8) |
98                                      hsp->cls2.rcvDataSizeLsb);
99                         ssp_value = ((sp->cls2.rcvDataSizeMsb << 8) |
100                                      sp->cls2.rcvDataSizeLsb);
101                         if (!ssp_value)
102                                 goto bad_service_param;
103                         if (ssp_value > hsp_value) {
104                                 sp->cls2.rcvDataSizeLsb =
105                                         hsp->cls2.rcvDataSizeLsb;
106                                 sp->cls2.rcvDataSizeMsb =
107                                         hsp->cls2.rcvDataSizeMsb;
108                         }
109                 }
110         } else if (class == CLASS2)
111                 goto bad_service_param;
112         if (sp->cls3.classValid) {
113                 if (!flogi) {
114                         hsp_value = ((hsp->cls3.rcvDataSizeMsb << 8) |
115                                      hsp->cls3.rcvDataSizeLsb);
116                         ssp_value = ((sp->cls3.rcvDataSizeMsb << 8) |
117                                      sp->cls3.rcvDataSizeLsb);
118                         if (!ssp_value)
119                                 goto bad_service_param;
120                         if (ssp_value > hsp_value) {
121                                 sp->cls3.rcvDataSizeLsb =
122                                         hsp->cls3.rcvDataSizeLsb;
123                                 sp->cls3.rcvDataSizeMsb =
124                                         hsp->cls3.rcvDataSizeMsb;
125                         }
126                 }
127         } else if (class == CLASS3)
128                 goto bad_service_param;
129
130         /*
131          * Preserve the upper four bits of the MSB from the PLOGI response.
132          * These bits contain the Buffer-to-Buffer State Change Number
133          * from the target and need to be passed to the FW.
134          */
135         hsp_value = (hsp->cmn.bbRcvSizeMsb << 8) | hsp->cmn.bbRcvSizeLsb;
136         ssp_value = (sp->cmn.bbRcvSizeMsb << 8) | sp->cmn.bbRcvSizeLsb;
137         if (ssp_value > hsp_value) {
138                 sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
139                 sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
140                                        (hsp->cmn.bbRcvSizeMsb & 0x0F);
141         }
142
143         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
144         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
145         return 1;
146 bad_service_param:
147         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
148                          "0207 Device %x "
149                          "(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x) sent "
150                          "invalid service parameters.  Ignoring device.\n",
151                          ndlp->nlp_DID,
152                          sp->nodeName.u.wwn[0], sp->nodeName.u.wwn[1],
153                          sp->nodeName.u.wwn[2], sp->nodeName.u.wwn[3],
154                          sp->nodeName.u.wwn[4], sp->nodeName.u.wwn[5],
155                          sp->nodeName.u.wwn[6], sp->nodeName.u.wwn[7]);
156         return 0;
157 }
158
159 static void *
160 lpfc_check_elscmpl_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
161                         struct lpfc_iocbq *rspiocb)
162 {
163         struct lpfc_dmabuf *pcmd, *prsp;
164         uint32_t *lp;
165         void     *ptr = NULL;
166         IOCB_t   *irsp;
167
168         irsp = &rspiocb->iocb;
169         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
170
171         /* For lpfc_els_abort, context2 could be zero'ed to delay
172          * freeing associated memory till after ABTS completes.
173          */
174         if (pcmd) {
175                 prsp =  list_get_first(&pcmd->list, struct lpfc_dmabuf,
176                                        list);
177                 if (prsp) {
178                         lp = (uint32_t *) prsp->virt;
179                         ptr = (void *)((uint8_t *)lp + sizeof(uint32_t));
180                 }
181         } else {
182                 /* Force ulpStatus error since we are returning NULL ptr */
183                 if (!(irsp->ulpStatus)) {
184                         irsp->ulpStatus = IOSTAT_LOCAL_REJECT;
185                         irsp->un.ulpWord[4] = IOERR_SLI_ABORTED;
186                 }
187                 ptr = NULL;
188         }
189         return ptr;
190 }
191
192
193
194 /*
195  * Free resources / clean up outstanding I/Os
196  * associated with a LPFC_NODELIST entry. This
197  * routine effectively results in a "software abort".
198  */
199 int
200 lpfc_els_abort(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
201 {
202         LIST_HEAD(completions);
203         LIST_HEAD(txcmplq_completions);
204         LIST_HEAD(abort_list);
205         struct lpfc_sli  *psli = &phba->sli;
206         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
207         struct lpfc_iocbq *iocb, *next_iocb;
208
209         /* Abort outstanding I/O on NPort <nlp_DID> */
210         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_DISCOVERY,
211                          "2819 Abort outstanding I/O on NPort x%x "
212                          "Data: x%x x%x x%x\n",
213                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
214                          ndlp->nlp_rpi);
215
216         lpfc_fabric_abort_nport(ndlp);
217
218         /* First check the txq */
219         spin_lock_irq(&phba->hbalock);
220         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
221                 /* Check to see if iocb matches the nport we are looking for */
222                 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
223                         /* It matches, so deque and call compl with anp error */
224                         list_move_tail(&iocb->list, &completions);
225                         pring->txq_cnt--;
226                 }
227         }
228
229         /* Next check the txcmplq */
230         list_splice_init(&pring->txcmplq, &txcmplq_completions);
231         spin_unlock_irq(&phba->hbalock);
232
233         list_for_each_entry_safe(iocb, next_iocb, &txcmplq_completions, list) {
234                 /* Check to see if iocb matches the nport we are looking for */
235                 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
236                         list_add_tail(&iocb->dlist, &abort_list);
237         }
238         spin_lock_irq(&phba->hbalock);
239         list_splice(&txcmplq_completions, &pring->txcmplq);
240         spin_unlock_irq(&phba->hbalock);
241
242         list_for_each_entry_safe(iocb, next_iocb, &abort_list, dlist) {
243                         spin_lock_irq(&phba->hbalock);
244                         list_del_init(&iocb->dlist);
245                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
246                         spin_unlock_irq(&phba->hbalock);
247         }
248
249         /* Cancel all the IOCBs from the completions list */
250         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
251                               IOERR_SLI_ABORTED);
252
253         lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
254         return 0;
255 }
256
257 static int
258 lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
259                struct lpfc_iocbq *cmdiocb)
260 {
261         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
262         struct lpfc_hba    *phba = vport->phba;
263         struct lpfc_dmabuf *pcmd;
264         uint32_t *lp;
265         IOCB_t *icmd;
266         struct serv_parm *sp;
267         LPFC_MBOXQ_t *mbox;
268         struct ls_rjt stat;
269         int rc;
270
271         memset(&stat, 0, sizeof (struct ls_rjt));
272         if (vport->port_state <= LPFC_FDISC) {
273                 /* Before responding to PLOGI, check for pt2pt mode.
274                  * If we are pt2pt, with an outstanding FLOGI, abort
275                  * the FLOGI and resend it first.
276                  */
277                 if (vport->fc_flag & FC_PT2PT) {
278                          lpfc_els_abort_flogi(phba);
279                         if (!(vport->fc_flag & FC_PT2PT_PLOGI)) {
280                                 /* If the other side is supposed to initiate
281                                  * the PLOGI anyway, just ACC it now and
282                                  * move on with discovery.
283                                  */
284                                 phba->fc_edtov = FF_DEF_EDTOV;
285                                 phba->fc_ratov = FF_DEF_RATOV;
286                                 /* Start discovery - this should just do
287                                    CLEAR_LA */
288                                 lpfc_disc_start(vport);
289                         } else
290                                 lpfc_initial_flogi(vport);
291                 } else {
292                         stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
293                         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
294                         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
295                                             ndlp, NULL);
296                         return 0;
297                 }
298         }
299         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
300         lp = (uint32_t *) pcmd->virt;
301         sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
302         if (wwn_to_u64(sp->portName.u.wwn) == 0) {
303                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
304                                  "0140 PLOGI Reject: invalid nname\n");
305                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
306                 stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_PNAME;
307                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
308                         NULL);
309                 return 0;
310         }
311         if (wwn_to_u64(sp->nodeName.u.wwn) == 0) {
312                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
313                                  "0141 PLOGI Reject: invalid pname\n");
314                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
315                 stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_NNAME;
316                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
317                         NULL);
318                 return 0;
319         }
320         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0) == 0)) {
321                 /* Reject this request because invalid parameters */
322                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
323                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
324                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
325                         NULL);
326                 return 0;
327         }
328         icmd = &cmdiocb->iocb;
329
330         /* PLOGI chkparm OK */
331         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
332                          "0114 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
333                          ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
334                          ndlp->nlp_rpi);
335
336         if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
337                 ndlp->nlp_fcp_info |= CLASS2;
338         else
339                 ndlp->nlp_fcp_info |= CLASS3;
340
341         ndlp->nlp_class_sup = 0;
342         if (sp->cls1.classValid)
343                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
344         if (sp->cls2.classValid)
345                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
346         if (sp->cls3.classValid)
347                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
348         if (sp->cls4.classValid)
349                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
350         ndlp->nlp_maxframe =
351                 ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
352
353         /*
354          * Need to unreg_login if we are already in one of these states and
355          * change to NPR state. This will block the port until after the ACC
356          * completes and the reg_login is issued and completed.
357          */
358         switch (ndlp->nlp_state) {
359         case  NLP_STE_NPR_NODE:
360                 if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
361                         break;
362         case  NLP_STE_REG_LOGIN_ISSUE:
363         case  NLP_STE_PRLI_ISSUE:
364         case  NLP_STE_UNMAPPED_NODE:
365         case  NLP_STE_MAPPED_NODE:
366                 lpfc_unreg_rpi(vport, ndlp);
367                 ndlp->nlp_prev_state = ndlp->nlp_state;
368                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
369         }
370
371         if ((vport->fc_flag & FC_PT2PT) &&
372             !(vport->fc_flag & FC_PT2PT_PLOGI)) {
373                 /* rcv'ed PLOGI decides what our NPortId will be */
374                 vport->fc_myDID = icmd->un.rcvels.parmRo;
375                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
376                 if (mbox == NULL)
377                         goto out;
378                 lpfc_config_link(phba, mbox);
379                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
380                 mbox->vport = vport;
381                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
382                 if (rc == MBX_NOT_FINISHED) {
383                         mempool_free(mbox, phba->mbox_mem_pool);
384                         goto out;
385                 }
386
387                 lpfc_can_disctmo(vport);
388         }
389         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
390         if (!mbox)
391                 goto out;
392
393         rc = lpfc_reg_rpi(phba, vport->vpi, icmd->un.rcvels.remoteID,
394                             (uint8_t *) sp, mbox, ndlp->nlp_rpi);
395         if (rc) {
396                 mempool_free(mbox, phba->mbox_mem_pool);
397                 goto out;
398         }
399
400         /* ACC PLOGI rsp command needs to execute first,
401          * queue this mbox command to be processed later.
402          */
403         mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
404         /*
405          * mbox->context2 = lpfc_nlp_get(ndlp) deferred until mailbox
406          * command issued in lpfc_cmpl_els_acc().
407          */
408         mbox->vport = vport;
409         spin_lock_irq(shost->host_lock);
410         ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
411         spin_unlock_irq(shost->host_lock);
412
413         /*
414          * If there is an outstanding PLOGI issued, abort it before
415          * sending ACC rsp for received PLOGI. If pending plogi
416          * is not canceled here, the plogi will be rejected by
417          * remote port and will be retried. On a configuration with
418          * single discovery thread, this will cause a huge delay in
419          * discovery. Also this will cause multiple state machines
420          * running in parallel for this node.
421          */
422         if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE) {
423                 /* software abort outstanding PLOGI */
424                 lpfc_els_abort(phba, ndlp);
425         }
426
427         if ((vport->port_type == LPFC_NPIV_PORT &&
428              vport->cfg_restrict_login)) {
429
430                 /* In order to preserve RPIs, we want to cleanup
431                  * the default RPI the firmware created to rcv
432                  * this ELS request. The only way to do this is
433                  * to register, then unregister the RPI.
434                  */
435                 spin_lock_irq(shost->host_lock);
436                 ndlp->nlp_flag |= NLP_RM_DFLT_RPI;
437                 spin_unlock_irq(shost->host_lock);
438                 stat.un.b.lsRjtRsnCode = LSRJT_INVALID_CMD;
439                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
440                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
441                         ndlp, mbox);
442                 return 1;
443         }
444         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, mbox);
445         return 1;
446 out:
447         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
448         stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
449         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
450         return 0;
451 }
452
453 static int
454 lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
455                 struct lpfc_iocbq *cmdiocb)
456 {
457         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
458         struct lpfc_dmabuf *pcmd;
459         struct serv_parm   *sp;
460         struct lpfc_name   *pnn, *ppn;
461         struct ls_rjt stat;
462         ADISC *ap;
463         IOCB_t *icmd;
464         uint32_t *lp;
465         uint32_t cmd;
466
467         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
468         lp = (uint32_t *) pcmd->virt;
469
470         cmd = *lp++;
471         if (cmd == ELS_CMD_ADISC) {
472                 ap = (ADISC *) lp;
473                 pnn = (struct lpfc_name *) & ap->nodeName;
474                 ppn = (struct lpfc_name *) & ap->portName;
475         } else {
476                 sp = (struct serv_parm *) lp;
477                 pnn = (struct lpfc_name *) & sp->nodeName;
478                 ppn = (struct lpfc_name *) & sp->portName;
479         }
480
481         icmd = &cmdiocb->iocb;
482         if (icmd->ulpStatus == 0 && lpfc_check_adisc(vport, ndlp, pnn, ppn)) {
483                 if (cmd == ELS_CMD_ADISC) {
484                         lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
485                 } else {
486                         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp,
487                                          NULL);
488                 }
489                 return 1;
490         }
491         /* Reject this request because invalid parameters */
492         stat.un.b.lsRjtRsvd0 = 0;
493         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
494         stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
495         stat.un.b.vendorUnique = 0;
496         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
497
498         /* 1 sec timeout */
499         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
500
501         spin_lock_irq(shost->host_lock);
502         ndlp->nlp_flag |= NLP_DELAY_TMO;
503         spin_unlock_irq(shost->host_lock);
504         ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
505         ndlp->nlp_prev_state = ndlp->nlp_state;
506         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
507         return 0;
508 }
509
510 static int
511 lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
512               struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
513 {
514         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
515         struct lpfc_hba    *phba = vport->phba;
516         struct lpfc_vport **vports;
517         int i, active_vlink_present = 0 ;
518
519         /* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
520         /* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
521          * PLOGIs during LOGO storms from a device.
522          */
523         spin_lock_irq(shost->host_lock);
524         ndlp->nlp_flag |= NLP_LOGO_ACC;
525         spin_unlock_irq(shost->host_lock);
526         if (els_cmd == ELS_CMD_PRLO)
527                 lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
528         else
529                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
530         if (ndlp->nlp_DID == Fabric_DID) {
531                 if (vport->port_state <= LPFC_FDISC)
532                         goto out;
533                 lpfc_linkdown_port(vport);
534                 spin_lock_irq(shost->host_lock);
535                 vport->fc_flag |= FC_VPORT_LOGO_RCVD;
536                 spin_unlock_irq(shost->host_lock);
537                 vports = lpfc_create_vport_work_array(phba);
538                 if (vports) {
539                         for (i = 0; i <= phba->max_vports && vports[i] != NULL;
540                                         i++) {
541                                 if ((!(vports[i]->fc_flag &
542                                         FC_VPORT_LOGO_RCVD)) &&
543                                         (vports[i]->port_state > LPFC_FDISC)) {
544                                         active_vlink_present = 1;
545                                         break;
546                                 }
547                         }
548                         lpfc_destroy_vport_work_array(phba, vports);
549                 }
550
551                 if (active_vlink_present) {
552                         /*
553                          * If there are other active VLinks present,
554                          * re-instantiate the Vlink using FDISC.
555                          */
556                         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
557                         spin_lock_irq(shost->host_lock);
558                         ndlp->nlp_flag |= NLP_DELAY_TMO;
559                         spin_unlock_irq(shost->host_lock);
560                         ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
561                         vport->port_state = LPFC_FDISC;
562                 } else {
563                         spin_lock_irq(shost->host_lock);
564                         phba->pport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG;
565                         spin_unlock_irq(shost->host_lock);
566                         lpfc_retry_pport_discovery(phba);
567                 }
568         } else if ((!(ndlp->nlp_type & NLP_FABRIC) &&
569                 ((ndlp->nlp_type & NLP_FCP_TARGET) ||
570                 !(ndlp->nlp_type & NLP_FCP_INITIATOR))) ||
571                 (ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
572                 /* Only try to re-login if this is NOT a Fabric Node */
573                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
574                 spin_lock_irq(shost->host_lock);
575                 ndlp->nlp_flag |= NLP_DELAY_TMO;
576                 spin_unlock_irq(shost->host_lock);
577
578                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
579         }
580 out:
581         ndlp->nlp_prev_state = ndlp->nlp_state;
582         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
583
584         spin_lock_irq(shost->host_lock);
585         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
586         spin_unlock_irq(shost->host_lock);
587         /* The driver has to wait until the ACC completes before it continues
588          * processing the LOGO.  The action will resume in
589          * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
590          * unreg_login, the driver waits so the ACC does not get aborted.
591          */
592         return 0;
593 }
594
595 static void
596 lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
597               struct lpfc_iocbq *cmdiocb)
598 {
599         struct lpfc_dmabuf *pcmd;
600         uint32_t *lp;
601         PRLI *npr;
602         struct fc_rport *rport = ndlp->rport;
603         u32 roles;
604
605         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
606         lp = (uint32_t *) pcmd->virt;
607         npr = (PRLI *) ((uint8_t *) lp + sizeof (uint32_t));
608
609         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
610         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
611         if (npr->prliType == PRLI_FCP_TYPE) {
612                 if (npr->initiatorFunc)
613                         ndlp->nlp_type |= NLP_FCP_INITIATOR;
614                 if (npr->targetFunc)
615                         ndlp->nlp_type |= NLP_FCP_TARGET;
616                 if (npr->Retry)
617                         ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
618         }
619         if (rport) {
620                 /* We need to update the rport role values */
621                 roles = FC_RPORT_ROLE_UNKNOWN;
622                 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
623                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
624                 if (ndlp->nlp_type & NLP_FCP_TARGET)
625                         roles |= FC_RPORT_ROLE_FCP_TARGET;
626
627                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
628                         "rport rolechg:   role:x%x did:x%x flg:x%x",
629                         roles, ndlp->nlp_DID, ndlp->nlp_flag);
630
631                 fc_remote_port_rolechg(rport, roles);
632         }
633 }
634
635 static uint32_t
636 lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
637 {
638         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
639
640         if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
641                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
642                 return 0;
643         }
644
645         if (!(vport->fc_flag & FC_PT2PT)) {
646                 /* Check config parameter use-adisc or FCP-2 */
647                 if ((vport->cfg_use_adisc && (vport->fc_flag & FC_RSCN_MODE)) ||
648                     ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
649                      (ndlp->nlp_type & NLP_FCP_TARGET))) {
650                         spin_lock_irq(shost->host_lock);
651                         ndlp->nlp_flag |= NLP_NPR_ADISC;
652                         spin_unlock_irq(shost->host_lock);
653                         return 1;
654                 }
655         }
656         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
657         lpfc_unreg_rpi(vport, ndlp);
658         return 0;
659 }
660 /**
661  * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
662  * @phba : Pointer to lpfc_hba structure.
663  * @vport: Pointer to lpfc_vport structure.
664  * @rpi  : rpi to be release.
665  *
666  * This function will send a unreg_login mailbox command to the firmware
667  * to release a rpi.
668  **/
669 void
670 lpfc_release_rpi(struct lpfc_hba *phba,
671                 struct lpfc_vport *vport,
672                 uint16_t rpi)
673 {
674         LPFC_MBOXQ_t *pmb;
675         int rc;
676
677         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
678                         GFP_KERNEL);
679         if (!pmb)
680                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
681                         "2796 mailbox memory allocation failed \n");
682         else {
683                 lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
684                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
685                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
686                 if (rc == MBX_NOT_FINISHED)
687                         mempool_free(pmb, phba->mbox_mem_pool);
688         }
689 }
690
691 static uint32_t
692 lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
693                   void *arg, uint32_t evt)
694 {
695         struct lpfc_hba *phba;
696         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
697         MAILBOX_t *mb;
698         uint16_t rpi;
699
700         phba = vport->phba;
701         /* Release the RPI if reglogin completing */
702         if (!(phba->pport->load_flag & FC_UNLOADING) &&
703                 (evt == NLP_EVT_CMPL_REG_LOGIN) &&
704                 (!pmb->u.mb.mbxStatus)) {
705                 mb = &pmb->u.mb;
706                 rpi = pmb->u.mb.un.varWords[0];
707                 lpfc_release_rpi(phba, vport, rpi);
708         }
709         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
710                          "0271 Illegal State Transition: node x%x "
711                          "event x%x, state x%x Data: x%x x%x\n",
712                          ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
713                          ndlp->nlp_flag);
714         return ndlp->nlp_state;
715 }
716
717 static uint32_t
718 lpfc_cmpl_plogi_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
719                   void *arg, uint32_t evt)
720 {
721         /* This transition is only legal if we previously
722          * rcv'ed a PLOGI. Since we don't want 2 discovery threads
723          * working on the same NPortID, do nothing for this thread
724          * to stop it.
725          */
726         if (!(ndlp->nlp_flag & NLP_RCV_PLOGI)) {
727                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
728                          "0272 Illegal State Transition: node x%x "
729                          "event x%x, state x%x Data: x%x x%x\n",
730                          ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
731                          ndlp->nlp_flag);
732         }
733         return ndlp->nlp_state;
734 }
735
736 /* Start of Discovery State Machine routines */
737
738 static uint32_t
739 lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
740                            void *arg, uint32_t evt)
741 {
742         struct lpfc_iocbq *cmdiocb;
743
744         cmdiocb = (struct lpfc_iocbq *) arg;
745
746         if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
747                 return ndlp->nlp_state;
748         }
749         return NLP_STE_FREED_NODE;
750 }
751
752 static uint32_t
753 lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
754                          void *arg, uint32_t evt)
755 {
756         lpfc_issue_els_logo(vport, ndlp, 0);
757         return ndlp->nlp_state;
758 }
759
760 static uint32_t
761 lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
762                           void *arg, uint32_t evt)
763 {
764         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
765         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
766
767         spin_lock_irq(shost->host_lock);
768         ndlp->nlp_flag |= NLP_LOGO_ACC;
769         spin_unlock_irq(shost->host_lock);
770         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
771
772         return ndlp->nlp_state;
773 }
774
775 static uint32_t
776 lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
777                            void *arg, uint32_t evt)
778 {
779         return NLP_STE_FREED_NODE;
780 }
781
782 static uint32_t
783 lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
784                            void *arg, uint32_t evt)
785 {
786         return NLP_STE_FREED_NODE;
787 }
788
789 static uint32_t
790 lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
791                            void *arg, uint32_t evt)
792 {
793         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
794         struct lpfc_hba   *phba = vport->phba;
795         struct lpfc_iocbq *cmdiocb = arg;
796         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
797         uint32_t *lp = (uint32_t *) pcmd->virt;
798         struct serv_parm *sp = (struct serv_parm *) (lp + 1);
799         struct ls_rjt stat;
800         int port_cmp;
801
802         memset(&stat, 0, sizeof (struct ls_rjt));
803
804         /* For a PLOGI, we only accept if our portname is less
805          * than the remote portname.
806          */
807         phba->fc_stat.elsLogiCol++;
808         port_cmp = memcmp(&vport->fc_portname, &sp->portName,
809                           sizeof(struct lpfc_name));
810
811         if (port_cmp >= 0) {
812                 /* Reject this request because the remote node will accept
813                    ours */
814                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
815                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
816                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
817                         NULL);
818         } else {
819                 if (lpfc_rcv_plogi(vport, ndlp, cmdiocb) &&
820                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
821                     (vport->num_disc_nodes)) {
822                         spin_lock_irq(shost->host_lock);
823                         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
824                         spin_unlock_irq(shost->host_lock);
825                         /* Check if there are more PLOGIs to be sent */
826                         lpfc_more_plogi(vport);
827                         if (vport->num_disc_nodes == 0) {
828                                 spin_lock_irq(shost->host_lock);
829                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
830                                 spin_unlock_irq(shost->host_lock);
831                                 lpfc_can_disctmo(vport);
832                                 lpfc_end_rscn(vport);
833                         }
834                 }
835         } /* If our portname was less */
836
837         return ndlp->nlp_state;
838 }
839
840 static uint32_t
841 lpfc_rcv_prli_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
842                           void *arg, uint32_t evt)
843 {
844         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
845         struct ls_rjt     stat;
846
847         memset(&stat, 0, sizeof (struct ls_rjt));
848         stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
849         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
850         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
851         return ndlp->nlp_state;
852 }
853
854 static uint32_t
855 lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
856                           void *arg, uint32_t evt)
857 {
858         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
859
860                                 /* software abort outstanding PLOGI */
861         lpfc_els_abort(vport->phba, ndlp);
862
863         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
864         return ndlp->nlp_state;
865 }
866
867 static uint32_t
868 lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
869                          void *arg, uint32_t evt)
870 {
871         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
872         struct lpfc_hba   *phba = vport->phba;
873         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
874
875         /* software abort outstanding PLOGI */
876         lpfc_els_abort(phba, ndlp);
877
878         if (evt == NLP_EVT_RCV_LOGO) {
879                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
880         } else {
881                 lpfc_issue_els_logo(vport, ndlp, 0);
882         }
883
884         /* Put ndlp in npr state set plogi timer for 1 sec */
885         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
886         spin_lock_irq(shost->host_lock);
887         ndlp->nlp_flag |= NLP_DELAY_TMO;
888         spin_unlock_irq(shost->host_lock);
889         ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
890         ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
891         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
892
893         return ndlp->nlp_state;
894 }
895
896 static uint32_t
897 lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
898                             struct lpfc_nodelist *ndlp,
899                             void *arg,
900                             uint32_t evt)
901 {
902         struct lpfc_hba    *phba = vport->phba;
903         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
904         struct lpfc_iocbq  *cmdiocb, *rspiocb;
905         struct lpfc_dmabuf *pcmd, *prsp, *mp;
906         uint32_t *lp;
907         IOCB_t *irsp;
908         struct serv_parm *sp;
909         LPFC_MBOXQ_t *mbox;
910
911         cmdiocb = (struct lpfc_iocbq *) arg;
912         rspiocb = cmdiocb->context_un.rsp_iocb;
913
914         if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
915                 /* Recovery from PLOGI collision logic */
916                 return ndlp->nlp_state;
917         }
918
919         irsp = &rspiocb->iocb;
920
921         if (irsp->ulpStatus)
922                 goto out;
923
924         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
925
926         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
927
928         lp = (uint32_t *) prsp->virt;
929         sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
930
931         /* Some switches have FDMI servers returning 0 for WWN */
932         if ((ndlp->nlp_DID != FDMI_DID) &&
933                 (wwn_to_u64(sp->portName.u.wwn) == 0 ||
934                 wwn_to_u64(sp->nodeName.u.wwn) == 0)) {
935                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
936                                  "0142 PLOGI RSP: Invalid WWN.\n");
937                 goto out;
938         }
939         if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
940                 goto out;
941         /* PLOGI chkparm OK */
942         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
943                          "0121 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
944                          ndlp->nlp_DID, ndlp->nlp_state,
945                          ndlp->nlp_flag, ndlp->nlp_rpi);
946         if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
947                 ndlp->nlp_fcp_info |= CLASS2;
948         else
949                 ndlp->nlp_fcp_info |= CLASS3;
950
951         ndlp->nlp_class_sup = 0;
952         if (sp->cls1.classValid)
953                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
954         if (sp->cls2.classValid)
955                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
956         if (sp->cls3.classValid)
957                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
958         if (sp->cls4.classValid)
959                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
960         ndlp->nlp_maxframe =
961                 ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
962
963         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
964         if (!mbox) {
965                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
966                         "0133 PLOGI: no memory for reg_login "
967                         "Data: x%x x%x x%x x%x\n",
968                         ndlp->nlp_DID, ndlp->nlp_state,
969                         ndlp->nlp_flag, ndlp->nlp_rpi);
970                 goto out;
971         }
972
973         lpfc_unreg_rpi(vport, ndlp);
974
975         if (lpfc_reg_rpi(phba, vport->vpi, irsp->un.elsreq64.remoteID,
976                          (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
977                 switch (ndlp->nlp_DID) {
978                 case NameServer_DID:
979                         mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
980                         break;
981                 case FDMI_DID:
982                         mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
983                         break;
984                 default:
985                         ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
986                         mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
987                 }
988                 mbox->context2 = lpfc_nlp_get(ndlp);
989                 mbox->vport = vport;
990                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
991                     != MBX_NOT_FINISHED) {
992                         lpfc_nlp_set_state(vport, ndlp,
993                                            NLP_STE_REG_LOGIN_ISSUE);
994                         return ndlp->nlp_state;
995                 }
996                 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
997                         ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
998                 /* decrement node reference count to the failed mbox
999                  * command
1000                  */
1001                 lpfc_nlp_put(ndlp);
1002                 mp = (struct lpfc_dmabuf *) mbox->context1;
1003                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1004                 kfree(mp);
1005                 mempool_free(mbox, phba->mbox_mem_pool);
1006
1007                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1008                                  "0134 PLOGI: cannot issue reg_login "
1009                                  "Data: x%x x%x x%x x%x\n",
1010                                  ndlp->nlp_DID, ndlp->nlp_state,
1011                                  ndlp->nlp_flag, ndlp->nlp_rpi);
1012         } else {
1013                 mempool_free(mbox, phba->mbox_mem_pool);
1014
1015                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1016                                  "0135 PLOGI: cannot format reg_login "
1017                                  "Data: x%x x%x x%x x%x\n",
1018                                  ndlp->nlp_DID, ndlp->nlp_state,
1019                                  ndlp->nlp_flag, ndlp->nlp_rpi);
1020         }
1021
1022
1023 out:
1024         if (ndlp->nlp_DID == NameServer_DID) {
1025                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1026                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1027                                  "0261 Cannot Register NameServer login\n");
1028         }
1029
1030         spin_lock_irq(shost->host_lock);
1031         ndlp->nlp_flag |= NLP_DEFER_RM;
1032         spin_unlock_irq(shost->host_lock);
1033         return NLP_STE_FREED_NODE;
1034 }
1035
1036 static uint32_t
1037 lpfc_cmpl_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1038                            void *arg, uint32_t evt)
1039 {
1040         return ndlp->nlp_state;
1041 }
1042
1043 static uint32_t
1044 lpfc_cmpl_reglogin_plogi_issue(struct lpfc_vport *vport,
1045         struct lpfc_nodelist *ndlp, void *arg, uint32_t evt)
1046 {
1047         struct lpfc_hba *phba;
1048         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1049         MAILBOX_t *mb = &pmb->u.mb;
1050         uint16_t rpi;
1051
1052         phba = vport->phba;
1053         /* Release the RPI */
1054         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1055                 !mb->mbxStatus) {
1056                 rpi = pmb->u.mb.un.varWords[0];
1057                 lpfc_release_rpi(phba, vport, rpi);
1058         }
1059         return ndlp->nlp_state;
1060 }
1061
1062 static uint32_t
1063 lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1064                            void *arg, uint32_t evt)
1065 {
1066         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1067
1068         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1069                 spin_lock_irq(shost->host_lock);
1070                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1071                 spin_unlock_irq(shost->host_lock);
1072                 return ndlp->nlp_state;
1073         } else {
1074                 /* software abort outstanding PLOGI */
1075                 lpfc_els_abort(vport->phba, ndlp);
1076
1077                 lpfc_drop_node(vport, ndlp);
1078                 return NLP_STE_FREED_NODE;
1079         }
1080 }
1081
1082 static uint32_t
1083 lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
1084                               struct lpfc_nodelist *ndlp,
1085                               void *arg,
1086                               uint32_t evt)
1087 {
1088         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1089         struct lpfc_hba  *phba = vport->phba;
1090
1091         /* Don't do anything that will mess up processing of the
1092          * previous RSCN.
1093          */
1094         if (vport->fc_flag & FC_RSCN_DEFERRED)
1095                 return ndlp->nlp_state;
1096
1097         /* software abort outstanding PLOGI */
1098         lpfc_els_abort(phba, ndlp);
1099
1100         ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1101         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1102         spin_lock_irq(shost->host_lock);
1103         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1104         spin_unlock_irq(shost->host_lock);
1105
1106         return ndlp->nlp_state;
1107 }
1108
1109 static uint32_t
1110 lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1111                            void *arg, uint32_t evt)
1112 {
1113         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1114         struct lpfc_hba   *phba = vport->phba;
1115         struct lpfc_iocbq *cmdiocb;
1116
1117         /* software abort outstanding ADISC */
1118         lpfc_els_abort(phba, ndlp);
1119
1120         cmdiocb = (struct lpfc_iocbq *) arg;
1121
1122         if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1123                 if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1124                         spin_lock_irq(shost->host_lock);
1125                         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1126                         spin_unlock_irq(shost->host_lock);
1127                         if (vport->num_disc_nodes)
1128                                 lpfc_more_adisc(vport);
1129                 }
1130                 return ndlp->nlp_state;
1131         }
1132         ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1133         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
1134         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1135
1136         return ndlp->nlp_state;
1137 }
1138
1139 static uint32_t
1140 lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1141                           void *arg, uint32_t evt)
1142 {
1143         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1144
1145         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1146         return ndlp->nlp_state;
1147 }
1148
1149 static uint32_t
1150 lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1151                           void *arg, uint32_t evt)
1152 {
1153         struct lpfc_hba *phba = vport->phba;
1154         struct lpfc_iocbq *cmdiocb;
1155
1156         cmdiocb = (struct lpfc_iocbq *) arg;
1157
1158         /* software abort outstanding ADISC */
1159         lpfc_els_abort(phba, ndlp);
1160
1161         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1162         return ndlp->nlp_state;
1163 }
1164
1165 static uint32_t
1166 lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
1167                             struct lpfc_nodelist *ndlp,
1168                             void *arg, uint32_t evt)
1169 {
1170         struct lpfc_iocbq *cmdiocb;
1171
1172         cmdiocb = (struct lpfc_iocbq *) arg;
1173
1174         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1175         return ndlp->nlp_state;
1176 }
1177
1178 static uint32_t
1179 lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1180                           void *arg, uint32_t evt)
1181 {
1182         struct lpfc_iocbq *cmdiocb;
1183
1184         cmdiocb = (struct lpfc_iocbq *) arg;
1185
1186         /* Treat like rcv logo */
1187         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1188         return ndlp->nlp_state;
1189 }
1190
1191 static uint32_t
1192 lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
1193                             struct lpfc_nodelist *ndlp,
1194                             void *arg, uint32_t evt)
1195 {
1196         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1197         struct lpfc_hba   *phba = vport->phba;
1198         struct lpfc_iocbq *cmdiocb, *rspiocb;
1199         IOCB_t *irsp;
1200         ADISC *ap;
1201         int rc;
1202
1203         cmdiocb = (struct lpfc_iocbq *) arg;
1204         rspiocb = cmdiocb->context_un.rsp_iocb;
1205
1206         ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1207         irsp = &rspiocb->iocb;
1208
1209         if ((irsp->ulpStatus) ||
1210             (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
1211                 /* 1 sec timeout */
1212                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
1213                 spin_lock_irq(shost->host_lock);
1214                 ndlp->nlp_flag |= NLP_DELAY_TMO;
1215                 spin_unlock_irq(shost->host_lock);
1216                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1217
1218                 memset(&ndlp->nlp_nodename, 0, sizeof(struct lpfc_name));
1219                 memset(&ndlp->nlp_portname, 0, sizeof(struct lpfc_name));
1220
1221                 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1222                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1223                 lpfc_unreg_rpi(vport, ndlp);
1224                 return ndlp->nlp_state;
1225         }
1226
1227         if (phba->sli_rev == LPFC_SLI_REV4) {
1228                 rc = lpfc_sli4_resume_rpi(ndlp);
1229                 if (rc) {
1230                         /* Stay in state and retry. */
1231                         ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1232                         return ndlp->nlp_state;
1233                 }
1234         }
1235
1236         if (ndlp->nlp_type & NLP_FCP_TARGET) {
1237                 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1238                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1239         } else {
1240                 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1241                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1242         }
1243
1244         return ndlp->nlp_state;
1245 }
1246
1247 static uint32_t
1248 lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1249                            void *arg, uint32_t evt)
1250 {
1251         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1252
1253         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1254                 spin_lock_irq(shost->host_lock);
1255                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1256                 spin_unlock_irq(shost->host_lock);
1257                 return ndlp->nlp_state;
1258         } else {
1259                 /* software abort outstanding ADISC */
1260                 lpfc_els_abort(vport->phba, ndlp);
1261
1262                 lpfc_drop_node(vport, ndlp);
1263                 return NLP_STE_FREED_NODE;
1264         }
1265 }
1266
1267 static uint32_t
1268 lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
1269                               struct lpfc_nodelist *ndlp,
1270                               void *arg,
1271                               uint32_t evt)
1272 {
1273         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1274         struct lpfc_hba  *phba = vport->phba;
1275
1276         /* Don't do anything that will mess up processing of the
1277          * previous RSCN.
1278          */
1279         if (vport->fc_flag & FC_RSCN_DEFERRED)
1280                 return ndlp->nlp_state;
1281
1282         /* software abort outstanding ADISC */
1283         lpfc_els_abort(phba, ndlp);
1284
1285         ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1286         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1287         spin_lock_irq(shost->host_lock);
1288         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1289         spin_unlock_irq(shost->host_lock);
1290         lpfc_disc_set_adisc(vport, ndlp);
1291         return ndlp->nlp_state;
1292 }
1293
1294 static uint32_t
1295 lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport *vport,
1296                               struct lpfc_nodelist *ndlp,
1297                               void *arg,
1298                               uint32_t evt)
1299 {
1300         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1301
1302         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1303         return ndlp->nlp_state;
1304 }
1305
1306 static uint32_t
1307 lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
1308                              struct lpfc_nodelist *ndlp,
1309                              void *arg,
1310                              uint32_t evt)
1311 {
1312         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1313
1314         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1315         return ndlp->nlp_state;
1316 }
1317
1318 static uint32_t
1319 lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
1320                              struct lpfc_nodelist *ndlp,
1321                              void *arg,
1322                              uint32_t evt)
1323 {
1324         struct lpfc_hba   *phba = vport->phba;
1325         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1326         LPFC_MBOXQ_t      *mb;
1327         LPFC_MBOXQ_t      *nextmb;
1328         struct lpfc_dmabuf *mp;
1329
1330         cmdiocb = (struct lpfc_iocbq *) arg;
1331
1332         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1333         if ((mb = phba->sli.mbox_active)) {
1334                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1335                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1336                         lpfc_nlp_put(ndlp);
1337                         mb->context2 = NULL;
1338                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1339                 }
1340         }
1341
1342         spin_lock_irq(&phba->hbalock);
1343         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1344                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1345                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1346                         mp = (struct lpfc_dmabuf *) (mb->context1);
1347                         if (mp) {
1348                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
1349                                 kfree(mp);
1350                         }
1351                         lpfc_nlp_put(ndlp);
1352                         list_del(&mb->list);
1353                         phba->sli.mboxq_cnt--;
1354                         mempool_free(mb, phba->mbox_mem_pool);
1355                 }
1356         }
1357         spin_unlock_irq(&phba->hbalock);
1358
1359         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1360         return ndlp->nlp_state;
1361 }
1362
1363 static uint32_t
1364 lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
1365                                struct lpfc_nodelist *ndlp,
1366                                void *arg,
1367                                uint32_t evt)
1368 {
1369         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1370
1371         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1372         return ndlp->nlp_state;
1373 }
1374
1375 static uint32_t
1376 lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
1377                              struct lpfc_nodelist *ndlp,
1378                              void *arg,
1379                              uint32_t evt)
1380 {
1381         struct lpfc_iocbq *cmdiocb;
1382
1383         cmdiocb = (struct lpfc_iocbq *) arg;
1384         lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1385         return ndlp->nlp_state;
1386 }
1387
1388 static uint32_t
1389 lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
1390                                   struct lpfc_nodelist *ndlp,
1391                                   void *arg,
1392                                   uint32_t evt)
1393 {
1394         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1395         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1396         MAILBOX_t *mb = &pmb->u.mb;
1397         uint32_t did  = mb->un.varWords[1];
1398
1399         if (mb->mbxStatus) {
1400                 /* RegLogin failed */
1401                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1402                                 "0246 RegLogin failed Data: x%x x%x x%x\n",
1403                                 did, mb->mbxStatus, vport->port_state);
1404                 /*
1405                  * If RegLogin failed due to lack of HBA resources do not
1406                  * retry discovery.
1407                  */
1408                 if (mb->mbxStatus == MBXERR_RPI_FULL) {
1409                         ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1410                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1411                         return ndlp->nlp_state;
1412                 }
1413
1414                 /* Put ndlp in npr state set plogi timer for 1 sec */
1415                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
1416                 spin_lock_irq(shost->host_lock);
1417                 ndlp->nlp_flag |= NLP_DELAY_TMO;
1418                 spin_unlock_irq(shost->host_lock);
1419                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1420
1421                 lpfc_issue_els_logo(vport, ndlp, 0);
1422                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1423                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1424                 return ndlp->nlp_state;
1425         }
1426
1427         ndlp->nlp_rpi = mb->un.varWords[0];
1428         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1429
1430         /* Only if we are not a fabric nport do we issue PRLI */
1431         if (!(ndlp->nlp_type & NLP_FABRIC)) {
1432                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1433                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1434                 lpfc_issue_els_prli(vport, ndlp, 0);
1435         } else {
1436                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1437                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1438         }
1439         return ndlp->nlp_state;
1440 }
1441
1442 static uint32_t
1443 lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
1444                               struct lpfc_nodelist *ndlp,
1445                               void *arg,
1446                               uint32_t evt)
1447 {
1448         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1449
1450         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1451                 spin_lock_irq(shost->host_lock);
1452                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1453                 spin_unlock_irq(shost->host_lock);
1454                 return ndlp->nlp_state;
1455         } else {
1456                 lpfc_drop_node(vport, ndlp);
1457                 return NLP_STE_FREED_NODE;
1458         }
1459 }
1460
1461 static uint32_t
1462 lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
1463                                  struct lpfc_nodelist *ndlp,
1464                                  void *arg,
1465                                  uint32_t evt)
1466 {
1467         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1468
1469         /* Don't do anything that will mess up processing of the
1470          * previous RSCN.
1471          */
1472         if (vport->fc_flag & FC_RSCN_DEFERRED)
1473                 return ndlp->nlp_state;
1474
1475         ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1476         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1477         spin_lock_irq(shost->host_lock);
1478         ndlp->nlp_flag |= NLP_IGNR_REG_CMPL;
1479         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1480         spin_unlock_irq(shost->host_lock);
1481         lpfc_disc_set_adisc(vport, ndlp);
1482         return ndlp->nlp_state;
1483 }
1484
1485 static uint32_t
1486 lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1487                           void *arg, uint32_t evt)
1488 {
1489         struct lpfc_iocbq *cmdiocb;
1490
1491         cmdiocb = (struct lpfc_iocbq *) arg;
1492
1493         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1494         return ndlp->nlp_state;
1495 }
1496
1497 static uint32_t
1498 lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1499                          void *arg, uint32_t evt)
1500 {
1501         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1502
1503         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1504         return ndlp->nlp_state;
1505 }
1506
1507 static uint32_t
1508 lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1509                          void *arg, uint32_t evt)
1510 {
1511         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1512
1513         /* Software abort outstanding PRLI before sending acc */
1514         lpfc_els_abort(vport->phba, ndlp);
1515
1516         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1517         return ndlp->nlp_state;
1518 }
1519
1520 static uint32_t
1521 lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1522                            void *arg, uint32_t evt)
1523 {
1524         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1525
1526         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1527         return ndlp->nlp_state;
1528 }
1529
1530 /* This routine is envoked when we rcv a PRLO request from a nport
1531  * we are logged into.  We should send back a PRLO rsp setting the
1532  * appropriate bits.
1533  * NEXT STATE = PRLI_ISSUE
1534  */
1535 static uint32_t
1536 lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1537                          void *arg, uint32_t evt)
1538 {
1539         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1540
1541         lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1542         return ndlp->nlp_state;
1543 }
1544
1545 static uint32_t
1546 lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1547                           void *arg, uint32_t evt)
1548 {
1549         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1550         struct lpfc_iocbq *cmdiocb, *rspiocb;
1551         struct lpfc_hba   *phba = vport->phba;
1552         IOCB_t *irsp;
1553         PRLI *npr;
1554
1555         cmdiocb = (struct lpfc_iocbq *) arg;
1556         rspiocb = cmdiocb->context_un.rsp_iocb;
1557         npr = (PRLI *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1558
1559         irsp = &rspiocb->iocb;
1560         if (irsp->ulpStatus) {
1561                 if ((vport->port_type == LPFC_NPIV_PORT) &&
1562                     vport->cfg_restrict_login) {
1563                         goto out;
1564                 }
1565                 ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1566                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1567                 return ndlp->nlp_state;
1568         }
1569
1570         /* Check out PRLI rsp */
1571         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1572         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
1573         if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
1574             (npr->prliType == PRLI_FCP_TYPE)) {
1575                 if (npr->initiatorFunc)
1576                         ndlp->nlp_type |= NLP_FCP_INITIATOR;
1577                 if (npr->targetFunc)
1578                         ndlp->nlp_type |= NLP_FCP_TARGET;
1579                 if (npr->Retry)
1580                         ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
1581         }
1582         if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
1583             (vport->port_type == LPFC_NPIV_PORT) &&
1584              vport->cfg_restrict_login) {
1585 out:
1586                 spin_lock_irq(shost->host_lock);
1587                 ndlp->nlp_flag |= NLP_TARGET_REMOVE;
1588                 spin_unlock_irq(shost->host_lock);
1589                 lpfc_issue_els_logo(vport, ndlp, 0);
1590
1591                 ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1592                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1593                 return ndlp->nlp_state;
1594         }
1595
1596         ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1597         if (ndlp->nlp_type & NLP_FCP_TARGET)
1598                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1599         else
1600                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1601         return ndlp->nlp_state;
1602 }
1603
1604 /*! lpfc_device_rm_prli_issue
1605  *
1606  * \pre
1607  * \post
1608  * \param   phba
1609  * \param   ndlp
1610  * \param   arg
1611  * \param   evt
1612  * \return  uint32_t
1613  *
1614  * \b Description:
1615  *    This routine is envoked when we a request to remove a nport we are in the
1616  *    process of PRLIing. We should software abort outstanding prli, unreg
1617  *    login, send a logout. We will change node state to UNUSED_NODE, put it
1618  *    on plogi list so it can be freed when LOGO completes.
1619  *
1620  */
1621
1622 static uint32_t
1623 lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1624                           void *arg, uint32_t evt)
1625 {
1626         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1627
1628         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1629                 spin_lock_irq(shost->host_lock);
1630                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1631                 spin_unlock_irq(shost->host_lock);
1632                 return ndlp->nlp_state;
1633         } else {
1634                 /* software abort outstanding PLOGI */
1635                 lpfc_els_abort(vport->phba, ndlp);
1636
1637                 lpfc_drop_node(vport, ndlp);
1638                 return NLP_STE_FREED_NODE;
1639         }
1640 }
1641
1642
1643 /*! lpfc_device_recov_prli_issue
1644  *
1645  * \pre
1646  * \post
1647  * \param   phba
1648  * \param   ndlp
1649  * \param   arg
1650  * \param   evt
1651  * \return  uint32_t
1652  *
1653  * \b Description:
1654  *    The routine is envoked when the state of a device is unknown, like
1655  *    during a link down. We should remove the nodelist entry from the
1656  *    unmapped list, issue a UNREG_LOGIN, do a software abort of the
1657  *    outstanding PRLI command, then free the node entry.
1658  */
1659 static uint32_t
1660 lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
1661                              struct lpfc_nodelist *ndlp,
1662                              void *arg,
1663                              uint32_t evt)
1664 {
1665         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1666         struct lpfc_hba  *phba = vport->phba;
1667
1668         /* Don't do anything that will mess up processing of the
1669          * previous RSCN.
1670          */
1671         if (vport->fc_flag & FC_RSCN_DEFERRED)
1672                 return ndlp->nlp_state;
1673
1674         /* software abort outstanding PRLI */
1675         lpfc_els_abort(phba, ndlp);
1676
1677         ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1678         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1679         spin_lock_irq(shost->host_lock);
1680         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1681         spin_unlock_irq(shost->host_lock);
1682         lpfc_disc_set_adisc(vport, ndlp);
1683         return ndlp->nlp_state;
1684 }
1685
1686 static uint32_t
1687 lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1688                           void *arg, uint32_t evt)
1689 {
1690         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1691
1692         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1693         return ndlp->nlp_state;
1694 }
1695
1696 static uint32_t
1697 lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1698                          void *arg, uint32_t evt)
1699 {
1700         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1701
1702         lpfc_rcv_prli(vport, ndlp, cmdiocb);
1703         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1704         return ndlp->nlp_state;
1705 }
1706
1707 static uint32_t
1708 lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1709                          void *arg, uint32_t evt)
1710 {
1711         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1712
1713         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1714         return ndlp->nlp_state;
1715 }
1716
1717 static uint32_t
1718 lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1719                            void *arg, uint32_t evt)
1720 {
1721         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1722
1723         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1724         return ndlp->nlp_state;
1725 }
1726
1727 static uint32_t
1728 lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1729                          void *arg, uint32_t evt)
1730 {
1731         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1732
1733         lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1734         return ndlp->nlp_state;
1735 }
1736
1737 static uint32_t
1738 lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
1739                              struct lpfc_nodelist *ndlp,
1740                              void *arg,
1741                              uint32_t evt)
1742 {
1743         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1744
1745         ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
1746         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1747         spin_lock_irq(shost->host_lock);
1748         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1749         spin_unlock_irq(shost->host_lock);
1750         lpfc_disc_set_adisc(vport, ndlp);
1751
1752         return ndlp->nlp_state;
1753 }
1754
1755 static uint32_t
1756 lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1757                            void *arg, uint32_t evt)
1758 {
1759         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1760
1761         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1762         return ndlp->nlp_state;
1763 }
1764
1765 static uint32_t
1766 lpfc_rcv_prli_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1767                           void *arg, uint32_t evt)
1768 {
1769         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1770
1771         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1772         return ndlp->nlp_state;
1773 }
1774
1775 static uint32_t
1776 lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1777                           void *arg, uint32_t evt)
1778 {
1779         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1780
1781         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1782         return ndlp->nlp_state;
1783 }
1784
1785 static uint32_t
1786 lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
1787                             struct lpfc_nodelist *ndlp,
1788                             void *arg, uint32_t evt)
1789 {
1790         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1791
1792         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1793         return ndlp->nlp_state;
1794 }
1795
1796 static uint32_t
1797 lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1798                           void *arg, uint32_t evt)
1799 {
1800         struct lpfc_hba  *phba = vport->phba;
1801         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1802
1803         /* flush the target */
1804         lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1805                             ndlp->nlp_sid, 0, LPFC_CTX_TGT);
1806
1807         /* Treat like rcv logo */
1808         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1809         return ndlp->nlp_state;
1810 }
1811
1812 static uint32_t
1813 lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
1814                               struct lpfc_nodelist *ndlp,
1815                               void *arg,
1816                               uint32_t evt)
1817 {
1818         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1819
1820         ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
1821         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1822         spin_lock_irq(shost->host_lock);
1823         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1824         spin_unlock_irq(shost->host_lock);
1825         lpfc_disc_set_adisc(vport, ndlp);
1826         return ndlp->nlp_state;
1827 }
1828
1829 static uint32_t
1830 lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1831                         void *arg, uint32_t evt)
1832 {
1833         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1834         struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
1835
1836         /* Ignore PLOGI if we have an outstanding LOGO */
1837         if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
1838                 return ndlp->nlp_state;
1839         if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1840                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
1841                 spin_lock_irq(shost->host_lock);
1842                 ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
1843                 spin_unlock_irq(shost->host_lock);
1844         } else if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
1845                 /* send PLOGI immediately, move to PLOGI issue state */
1846                 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1847                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1848                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1849                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
1850                 }
1851         }
1852         return ndlp->nlp_state;
1853 }
1854
1855 static uint32_t
1856 lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1857                        void *arg, uint32_t evt)
1858 {
1859         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1860         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1861         struct ls_rjt     stat;
1862
1863         memset(&stat, 0, sizeof (struct ls_rjt));
1864         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1865         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1866         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1867
1868         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1869                 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
1870                         spin_lock_irq(shost->host_lock);
1871                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1872                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1873                         spin_unlock_irq(shost->host_lock);
1874                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
1875                         lpfc_issue_els_adisc(vport, ndlp, 0);
1876                 } else {
1877                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1878                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1879                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
1880                 }
1881         }
1882         return ndlp->nlp_state;
1883 }
1884
1885 static uint32_t
1886 lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
1887                        void *arg, uint32_t evt)
1888 {
1889         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1890
1891         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1892         return ndlp->nlp_state;
1893 }
1894
1895 static uint32_t
1896 lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1897                          void *arg, uint32_t evt)
1898 {
1899         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1900
1901         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1902         /*
1903          * Do not start discovery if discovery is about to start
1904          * or discovery in progress for this node. Starting discovery
1905          * here will affect the counting of discovery threads.
1906          */
1907         if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
1908             !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
1909                 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
1910                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1911                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1912                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
1913                         lpfc_issue_els_adisc(vport, ndlp, 0);
1914                 } else {
1915                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1916                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1917                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
1918                 }
1919         }
1920         return ndlp->nlp_state;
1921 }
1922
1923 static uint32_t
1924 lpfc_rcv_prlo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1925                        void *arg, uint32_t evt)
1926 {
1927         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1928         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1929
1930         spin_lock_irq(shost->host_lock);
1931         ndlp->nlp_flag |= NLP_LOGO_ACC;
1932         spin_unlock_irq(shost->host_lock);
1933
1934         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1935
1936         if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
1937                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
1938                 spin_lock_irq(shost->host_lock);
1939                 ndlp->nlp_flag |= NLP_DELAY_TMO;
1940                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1941                 spin_unlock_irq(shost->host_lock);
1942                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1943         } else {
1944                 spin_lock_irq(shost->host_lock);
1945                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1946                 spin_unlock_irq(shost->host_lock);
1947         }
1948         return ndlp->nlp_state;
1949 }
1950
1951 static uint32_t
1952 lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1953                          void *arg, uint32_t evt)
1954 {
1955         struct lpfc_iocbq *cmdiocb, *rspiocb;
1956         IOCB_t *irsp;
1957
1958         cmdiocb = (struct lpfc_iocbq *) arg;
1959         rspiocb = cmdiocb->context_un.rsp_iocb;
1960
1961         irsp = &rspiocb->iocb;
1962         if (irsp->ulpStatus) {
1963                 ndlp->nlp_flag |= NLP_DEFER_RM;
1964                 return NLP_STE_FREED_NODE;
1965         }
1966         return ndlp->nlp_state;
1967 }
1968
1969 static uint32_t
1970 lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1971                         void *arg, uint32_t evt)
1972 {
1973         struct lpfc_iocbq *cmdiocb, *rspiocb;
1974         IOCB_t *irsp;
1975
1976         cmdiocb = (struct lpfc_iocbq *) arg;
1977         rspiocb = cmdiocb->context_un.rsp_iocb;
1978
1979         irsp = &rspiocb->iocb;
1980         if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
1981                 lpfc_drop_node(vport, ndlp);
1982                 return NLP_STE_FREED_NODE;
1983         }
1984         return ndlp->nlp_state;
1985 }
1986
1987 static uint32_t
1988 lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1989                         void *arg, uint32_t evt)
1990 {
1991         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1992         if (ndlp->nlp_DID == Fabric_DID) {
1993                 spin_lock_irq(shost->host_lock);
1994                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1995                 spin_unlock_irq(shost->host_lock);
1996         }
1997         lpfc_unreg_rpi(vport, ndlp);
1998         return ndlp->nlp_state;
1999 }
2000
2001 static uint32_t
2002 lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2003                          void *arg, uint32_t evt)
2004 {
2005         struct lpfc_iocbq *cmdiocb, *rspiocb;
2006         IOCB_t *irsp;
2007
2008         cmdiocb = (struct lpfc_iocbq *) arg;
2009         rspiocb = cmdiocb->context_un.rsp_iocb;
2010
2011         irsp = &rspiocb->iocb;
2012         if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
2013                 lpfc_drop_node(vport, ndlp);
2014                 return NLP_STE_FREED_NODE;
2015         }
2016         return ndlp->nlp_state;
2017 }
2018
2019 static uint32_t
2020 lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
2021                             struct lpfc_nodelist *ndlp,
2022                             void *arg, uint32_t evt)
2023 {
2024         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2025         MAILBOX_t    *mb = &pmb->u.mb;
2026
2027         if (!mb->mbxStatus) {
2028                 ndlp->nlp_rpi = mb->un.varWords[0];
2029                 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
2030         } else {
2031                 if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
2032                         lpfc_drop_node(vport, ndlp);
2033                         return NLP_STE_FREED_NODE;
2034                 }
2035         }
2036         return ndlp->nlp_state;
2037 }
2038
2039 static uint32_t
2040 lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2041                         void *arg, uint32_t evt)
2042 {
2043         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2044
2045         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2046                 spin_lock_irq(shost->host_lock);
2047                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
2048                 spin_unlock_irq(shost->host_lock);
2049                 return ndlp->nlp_state;
2050         }
2051         lpfc_drop_node(vport, ndlp);
2052         return NLP_STE_FREED_NODE;
2053 }
2054
2055 static uint32_t
2056 lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2057                            void *arg, uint32_t evt)
2058 {
2059         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2060
2061         /* Don't do anything that will mess up processing of the
2062          * previous RSCN.
2063          */
2064         if (vport->fc_flag & FC_RSCN_DEFERRED)
2065                 return ndlp->nlp_state;
2066
2067         lpfc_cancel_retry_delay_tmo(vport, ndlp);
2068         spin_lock_irq(shost->host_lock);
2069         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2070         spin_unlock_irq(shost->host_lock);
2071         return ndlp->nlp_state;
2072 }
2073
2074
2075 /* This next section defines the NPort Discovery State Machine */
2076
2077 /* There are 4 different double linked lists nodelist entries can reside on.
2078  * The plogi list and adisc list are used when Link Up discovery or RSCN
2079  * processing is needed. Each list holds the nodes that we will send PLOGI
2080  * or ADISC on. These lists will keep track of what nodes will be effected
2081  * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
2082  * The unmapped_list will contain all nodes that we have successfully logged
2083  * into at the Fibre Channel level. The mapped_list will contain all nodes
2084  * that are mapped FCP targets.
2085  */
2086 /*
2087  * The bind list is a list of undiscovered (potentially non-existent) nodes
2088  * that we have saved binding information on. This information is used when
2089  * nodes transition from the unmapped to the mapped list.
2090  */
2091 /* For UNUSED_NODE state, the node has just been allocated .
2092  * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
2093  * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
2094  * and put on the unmapped list. For ADISC processing, the node is taken off
2095  * the ADISC list and placed on either the mapped or unmapped list (depending
2096  * on its previous state). Once on the unmapped list, a PRLI is issued and the
2097  * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
2098  * changed to UNMAPPED_NODE. If the completion indicates a mapped
2099  * node, the node is taken off the unmapped list. The binding list is checked
2100  * for a valid binding, or a binding is automatically assigned. If binding
2101  * assignment is unsuccessful, the node is left on the unmapped list. If
2102  * binding assignment is successful, the associated binding list entry (if
2103  * any) is removed, and the node is placed on the mapped list.
2104  */
2105 /*
2106  * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
2107  * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
2108  * expire, all effected nodes will receive a DEVICE_RM event.
2109  */
2110 /*
2111  * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
2112  * to either the ADISC or PLOGI list.  After a Nameserver query or ALPA loopmap
2113  * check, additional nodes may be added or removed (via DEVICE_RM) to / from
2114  * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
2115  * we will first process the ADISC list.  32 entries are processed initially and
2116  * ADISC is initited for each one.  Completions / Events for each node are
2117  * funnelled thru the state machine.  As each node finishes ADISC processing, it
2118  * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
2119  * waiting, and the ADISC list count is identically 0, then we are done. For
2120  * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
2121  * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
2122  * list.  32 entries are processed initially and PLOGI is initited for each one.
2123  * Completions / Events for each node are funnelled thru the state machine.  As
2124  * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
2125  * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
2126  * indentically 0, then we are done. We have now completed discovery / RSCN
2127  * handling. Upon completion, ALL nodes should be on either the mapped or
2128  * unmapped lists.
2129  */
2130
2131 static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
2132      (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
2133         /* Action routine                  Event       Current State  */
2134         lpfc_rcv_plogi_unused_node,     /* RCV_PLOGI   UNUSED_NODE    */
2135         lpfc_rcv_els_unused_node,       /* RCV_PRLI        */
2136         lpfc_rcv_logo_unused_node,      /* RCV_LOGO        */
2137         lpfc_rcv_els_unused_node,       /* RCV_ADISC       */
2138         lpfc_rcv_els_unused_node,       /* RCV_PDISC       */
2139         lpfc_rcv_els_unused_node,       /* RCV_PRLO        */
2140         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2141         lpfc_disc_illegal,              /* CMPL_PRLI       */
2142         lpfc_cmpl_logo_unused_node,     /* CMPL_LOGO       */
2143         lpfc_disc_illegal,              /* CMPL_ADISC      */
2144         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2145         lpfc_device_rm_unused_node,     /* DEVICE_RM       */
2146         lpfc_disc_illegal,              /* DEVICE_RECOVERY */
2147
2148         lpfc_rcv_plogi_plogi_issue,     /* RCV_PLOGI   PLOGI_ISSUE    */
2149         lpfc_rcv_prli_plogi_issue,      /* RCV_PRLI        */
2150         lpfc_rcv_logo_plogi_issue,      /* RCV_LOGO        */
2151         lpfc_rcv_els_plogi_issue,       /* RCV_ADISC       */
2152         lpfc_rcv_els_plogi_issue,       /* RCV_PDISC       */
2153         lpfc_rcv_els_plogi_issue,       /* RCV_PRLO        */
2154         lpfc_cmpl_plogi_plogi_issue,    /* CMPL_PLOGI      */
2155         lpfc_disc_illegal,              /* CMPL_PRLI       */
2156         lpfc_cmpl_logo_plogi_issue,     /* CMPL_LOGO       */
2157         lpfc_disc_illegal,              /* CMPL_ADISC      */
2158         lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
2159         lpfc_device_rm_plogi_issue,     /* DEVICE_RM       */
2160         lpfc_device_recov_plogi_issue,  /* DEVICE_RECOVERY */
2161
2162         lpfc_rcv_plogi_adisc_issue,     /* RCV_PLOGI   ADISC_ISSUE    */
2163         lpfc_rcv_prli_adisc_issue,      /* RCV_PRLI        */
2164         lpfc_rcv_logo_adisc_issue,      /* RCV_LOGO        */
2165         lpfc_rcv_padisc_adisc_issue,    /* RCV_ADISC       */
2166         lpfc_rcv_padisc_adisc_issue,    /* RCV_PDISC       */
2167         lpfc_rcv_prlo_adisc_issue,      /* RCV_PRLO        */
2168         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2169         lpfc_disc_illegal,              /* CMPL_PRLI       */
2170         lpfc_disc_illegal,              /* CMPL_LOGO       */
2171         lpfc_cmpl_adisc_adisc_issue,    /* CMPL_ADISC      */
2172         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2173         lpfc_device_rm_adisc_issue,     /* DEVICE_RM       */
2174         lpfc_device_recov_adisc_issue,  /* DEVICE_RECOVERY */
2175
2176         lpfc_rcv_plogi_reglogin_issue,  /* RCV_PLOGI  REG_LOGIN_ISSUE */
2177         lpfc_rcv_prli_reglogin_issue,   /* RCV_PLOGI       */
2178         lpfc_rcv_logo_reglogin_issue,   /* RCV_LOGO        */
2179         lpfc_rcv_padisc_reglogin_issue, /* RCV_ADISC       */
2180         lpfc_rcv_padisc_reglogin_issue, /* RCV_PDISC       */
2181         lpfc_rcv_prlo_reglogin_issue,   /* RCV_PRLO        */
2182         lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
2183         lpfc_disc_illegal,              /* CMPL_PRLI       */
2184         lpfc_disc_illegal,              /* CMPL_LOGO       */
2185         lpfc_disc_illegal,              /* CMPL_ADISC      */
2186         lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN  */
2187         lpfc_device_rm_reglogin_issue,  /* DEVICE_RM       */
2188         lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */
2189
2190         lpfc_rcv_plogi_prli_issue,      /* RCV_PLOGI   PRLI_ISSUE     */
2191         lpfc_rcv_prli_prli_issue,       /* RCV_PRLI        */
2192         lpfc_rcv_logo_prli_issue,       /* RCV_LOGO        */
2193         lpfc_rcv_padisc_prli_issue,     /* RCV_ADISC       */
2194         lpfc_rcv_padisc_prli_issue,     /* RCV_PDISC       */
2195         lpfc_rcv_prlo_prli_issue,       /* RCV_PRLO        */
2196         lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
2197         lpfc_cmpl_prli_prli_issue,      /* CMPL_PRLI       */
2198         lpfc_disc_illegal,              /* CMPL_LOGO       */
2199         lpfc_disc_illegal,              /* CMPL_ADISC      */
2200         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2201         lpfc_device_rm_prli_issue,      /* DEVICE_RM       */
2202         lpfc_device_recov_prli_issue,   /* DEVICE_RECOVERY */
2203
2204         lpfc_rcv_plogi_unmap_node,      /* RCV_PLOGI   UNMAPPED_NODE  */
2205         lpfc_rcv_prli_unmap_node,       /* RCV_PRLI        */
2206         lpfc_rcv_logo_unmap_node,       /* RCV_LOGO        */
2207         lpfc_rcv_padisc_unmap_node,     /* RCV_ADISC       */
2208         lpfc_rcv_padisc_unmap_node,     /* RCV_PDISC       */
2209         lpfc_rcv_prlo_unmap_node,       /* RCV_PRLO        */
2210         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2211         lpfc_disc_illegal,              /* CMPL_PRLI       */
2212         lpfc_disc_illegal,              /* CMPL_LOGO       */
2213         lpfc_disc_illegal,              /* CMPL_ADISC      */
2214         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2215         lpfc_disc_illegal,              /* DEVICE_RM       */
2216         lpfc_device_recov_unmap_node,   /* DEVICE_RECOVERY */
2217
2218         lpfc_rcv_plogi_mapped_node,     /* RCV_PLOGI   MAPPED_NODE    */
2219         lpfc_rcv_prli_mapped_node,      /* RCV_PRLI        */
2220         lpfc_rcv_logo_mapped_node,      /* RCV_LOGO        */
2221         lpfc_rcv_padisc_mapped_node,    /* RCV_ADISC       */
2222         lpfc_rcv_padisc_mapped_node,    /* RCV_PDISC       */
2223         lpfc_rcv_prlo_mapped_node,      /* RCV_PRLO        */
2224         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2225         lpfc_disc_illegal,              /* CMPL_PRLI       */
2226         lpfc_disc_illegal,              /* CMPL_LOGO       */
2227         lpfc_disc_illegal,              /* CMPL_ADISC      */
2228         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2229         lpfc_disc_illegal,              /* DEVICE_RM       */
2230         lpfc_device_recov_mapped_node,  /* DEVICE_RECOVERY */
2231
2232         lpfc_rcv_plogi_npr_node,        /* RCV_PLOGI   NPR_NODE    */
2233         lpfc_rcv_prli_npr_node,         /* RCV_PRLI        */
2234         lpfc_rcv_logo_npr_node,         /* RCV_LOGO        */
2235         lpfc_rcv_padisc_npr_node,       /* RCV_ADISC       */
2236         lpfc_rcv_padisc_npr_node,       /* RCV_PDISC       */
2237         lpfc_rcv_prlo_npr_node,         /* RCV_PRLO        */
2238         lpfc_cmpl_plogi_npr_node,       /* CMPL_PLOGI      */
2239         lpfc_cmpl_prli_npr_node,        /* CMPL_PRLI       */
2240         lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
2241         lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
2242         lpfc_cmpl_reglogin_npr_node,    /* CMPL_REG_LOGIN  */
2243         lpfc_device_rm_npr_node,        /* DEVICE_RM       */
2244         lpfc_device_recov_npr_node,     /* DEVICE_RECOVERY */
2245 };
2246
2247 int
2248 lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2249                         void *arg, uint32_t evt)
2250 {
2251         uint32_t cur_state, rc;
2252         uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
2253                          uint32_t);
2254         uint32_t got_ndlp = 0;
2255
2256         if (lpfc_nlp_get(ndlp))
2257                 got_ndlp = 1;
2258
2259         cur_state = ndlp->nlp_state;
2260
2261         /* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
2262         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2263                          "0211 DSM in event x%x on NPort x%x in "
2264                          "state %d Data: x%x\n",
2265                          evt, ndlp->nlp_DID, cur_state, ndlp->nlp_flag);
2266
2267         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
2268                  "DSM in:          evt:%d ste:%d did:x%x",
2269                 evt, cur_state, ndlp->nlp_DID);
2270
2271         func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
2272         rc = (func) (vport, ndlp, arg, evt);
2273
2274         /* DSM out state <rc> on NPort <nlp_DID> */
2275         if (got_ndlp) {
2276                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2277                          "0212 DSM out state %d on NPort x%x Data: x%x\n",
2278                          rc, ndlp->nlp_DID, ndlp->nlp_flag);
2279
2280                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
2281                         "DSM out:         ste:%d did:x%x flg:x%x",
2282                         rc, ndlp->nlp_DID, ndlp->nlp_flag);
2283                 /* Decrement the ndlp reference count held for this function */
2284                 lpfc_nlp_put(ndlp);
2285         } else {
2286                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2287                         "0213 DSM out state %d on NPort free\n", rc);
2288
2289                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
2290                         "DSM out:         ste:%d did:x%x flg:x%x",
2291                         rc, 0, 0);
2292         }
2293
2294         return rc;
2295 }