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