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