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