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