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