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