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