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