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