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