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