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