[SCSI] lpfc 8.2.8 : Add kernel-doc function headers
[cascardo/linux.git] / drivers / scsi / lpfc / lpfc_els.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2008 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21 /* See Fibre Channel protocol T11 FC-LS for details */
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_transport_fc.h>
30
31 #include "lpfc_hw.h"
32 #include "lpfc_sli.h"
33 #include "lpfc_disc.h"
34 #include "lpfc_scsi.h"
35 #include "lpfc.h"
36 #include "lpfc_logmsg.h"
37 #include "lpfc_crtn.h"
38 #include "lpfc_vport.h"
39 #include "lpfc_debugfs.h"
40
41 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
42                           struct lpfc_iocbq *);
43 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
44                         struct lpfc_iocbq *);
45 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
46 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
47                                 struct lpfc_nodelist *ndlp, uint8_t retry);
48 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
49                                   struct lpfc_iocbq *iocb);
50 static void lpfc_register_new_vport(struct lpfc_hba *phba,
51                                     struct lpfc_vport *vport,
52                                     struct lpfc_nodelist *ndlp);
53
54 static int lpfc_max_els_tries = 3;
55
56 /**
57  * lpfc_els_chk_latt: Check host link attention event for a vport.
58  * @vport: pointer to a host virtual N_Port data structure.
59  *
60  * This routine checks whether there is an outstanding host link
61  * attention event during the discovery process with the @vport. It is done
62  * by reading the HBA's Host Attention (HA) register. If there is any host
63  * link attention events during this @vport's discovery process, the @vport
64  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
65  * be issued if the link state is not already in host link cleared state,
66  * and a return code shall indicate whether the host link attention event
67  * had happened.
68  *
69  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
70  * state in LPFC_VPORT_READY, the request for checking host link attention
71  * event will be ignored and a return code shall indicate no host link
72  * attention event had happened.
73  *
74  * Return codes
75  *   0 - no host link attention event happened
76  *   1 - host link attention event happened
77  **/
78 int
79 lpfc_els_chk_latt(struct lpfc_vport *vport)
80 {
81         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
82         struct lpfc_hba  *phba = vport->phba;
83         uint32_t ha_copy;
84
85         if (vport->port_state >= LPFC_VPORT_READY ||
86             phba->link_state == LPFC_LINK_DOWN)
87                 return 0;
88
89         /* Read the HBA Host Attention Register */
90         ha_copy = readl(phba->HAregaddr);
91
92         if (!(ha_copy & HA_LATT))
93                 return 0;
94
95         /* Pending Link Event during Discovery */
96         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
97                          "0237 Pending Link Event during "
98                          "Discovery: State x%x\n",
99                          phba->pport->port_state);
100
101         /* CLEAR_LA should re-enable link attention events and
102          * we should then imediately take a LATT event. The
103          * LATT processing should call lpfc_linkdown() which
104          * will cleanup any left over in-progress discovery
105          * events.
106          */
107         spin_lock_irq(shost->host_lock);
108         vport->fc_flag |= FC_ABORT_DISCOVERY;
109         spin_unlock_irq(shost->host_lock);
110
111         if (phba->link_state != LPFC_CLEAR_LA)
112                 lpfc_issue_clear_la(phba, vport);
113
114         return 1;
115 }
116
117 /**
118  * lpfc_prep_els_iocb: Allocate and prepare a lpfc iocb data structure.
119  * @vport: pointer to a host virtual N_Port data structure.
120  * @expectRsp: flag indicating whether response is expected.
121  * @cmdSize: size of the ELS command.
122  * @retry: number of retries to the command IOCB when it fails.
123  * @ndlp: pointer to a node-list data structure.
124  * @did: destination identifier.
125  * @elscmd: the ELS command code.
126  *
127  * This routine is used for allocating a lpfc-IOCB data structure from
128  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
129  * passed into the routine for discovery state machine to issue an Extended
130  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
131  * and preparation routine that is used by all the discovery state machine
132  * routines and the ELS command-specific fields will be later set up by
133  * the individual discovery machine routines after calling this routine
134  * allocating and preparing a generic IOCB data structure. It fills in the
135  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
136  * payload and response payload (if expected). The reference count on the
137  * ndlp is incremented by 1 and the reference to the ndlp is put into
138  * context1 of the IOCB data structure for this IOCB to hold the ndlp
139  * reference for the command's callback function to access later.
140  *
141  * Return code
142  *   Pointer to the newly allocated/prepared els iocb data structure
143  *   NULL - when els iocb data structure allocation/preparation failed
144  **/
145 static struct lpfc_iocbq *
146 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
147                    uint16_t cmdSize, uint8_t retry,
148                    struct lpfc_nodelist *ndlp, uint32_t did,
149                    uint32_t elscmd)
150 {
151         struct lpfc_hba  *phba = vport->phba;
152         struct lpfc_iocbq *elsiocb;
153         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
154         struct ulp_bde64 *bpl;
155         IOCB_t *icmd;
156
157
158         if (!lpfc_is_link_up(phba))
159                 return NULL;
160
161         /* Allocate buffer for  command iocb */
162         elsiocb = lpfc_sli_get_iocbq(phba);
163
164         if (elsiocb == NULL)
165                 return NULL;
166
167         icmd = &elsiocb->iocb;
168
169         /* fill in BDEs for command */
170         /* Allocate buffer for command payload */
171         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
172         if (pcmd)
173                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
174         if (!pcmd || !pcmd->virt)
175                 goto els_iocb_free_pcmb_exit;
176
177         INIT_LIST_HEAD(&pcmd->list);
178
179         /* Allocate buffer for response payload */
180         if (expectRsp) {
181                 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
182                 if (prsp)
183                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
184                                                      &prsp->phys);
185                 if (!prsp || !prsp->virt)
186                         goto els_iocb_free_prsp_exit;
187                 INIT_LIST_HEAD(&prsp->list);
188         } else
189                 prsp = NULL;
190
191         /* Allocate buffer for Buffer ptr list */
192         pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
193         if (pbuflist)
194                 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
195                                                  &pbuflist->phys);
196         if (!pbuflist || !pbuflist->virt)
197                 goto els_iocb_free_pbuf_exit;
198
199         INIT_LIST_HEAD(&pbuflist->list);
200
201         icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
202         icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
203         icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BDL;
204         icmd->un.elsreq64.remoteID = did;       /* DID */
205         if (expectRsp) {
206                 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
207                 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
208                 icmd->ulpTimeout = phba->fc_ratov * 2;
209         } else {
210                 icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
211                 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
212         }
213         icmd->ulpBdeCount = 1;
214         icmd->ulpLe = 1;
215         icmd->ulpClass = CLASS3;
216
217         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
218                 icmd->un.elsreq64.myID = vport->fc_myDID;
219
220                 /* For ELS_REQUEST64_CR, use the VPI by default */
221                 icmd->ulpContext = vport->vpi;
222                 icmd->ulpCt_h = 0;
223                 icmd->ulpCt_l = 1;
224         }
225
226         bpl = (struct ulp_bde64 *) pbuflist->virt;
227         bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
228         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
229         bpl->tus.f.bdeSize = cmdSize;
230         bpl->tus.f.bdeFlags = 0;
231         bpl->tus.w = le32_to_cpu(bpl->tus.w);
232
233         if (expectRsp) {
234                 bpl++;
235                 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
236                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
237                 bpl->tus.f.bdeSize = FCELSSIZE;
238                 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
239                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
240         }
241
242         /* prevent preparing iocb with NULL ndlp reference */
243         elsiocb->context1 = lpfc_nlp_get(ndlp);
244         if (!elsiocb->context1)
245                 goto els_iocb_free_pbuf_exit;
246         elsiocb->context2 = pcmd;
247         elsiocb->context3 = pbuflist;
248         elsiocb->retry = retry;
249         elsiocb->vport = vport;
250         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
251
252         if (prsp) {
253                 list_add(&prsp->list, &pcmd->list);
254         }
255         if (expectRsp) {
256                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
257                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
258                                  "0116 Xmit ELS command x%x to remote "
259                                  "NPORT x%x I/O tag: x%x, port state: x%x\n",
260                                  elscmd, did, elsiocb->iotag,
261                                  vport->port_state);
262         } else {
263                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
264                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
265                                  "0117 Xmit ELS response x%x to remote "
266                                  "NPORT x%x I/O tag: x%x, size: x%x\n",
267                                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
268                                  cmdSize);
269         }
270         return elsiocb;
271
272 els_iocb_free_pbuf_exit:
273         lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
274         kfree(pbuflist);
275
276 els_iocb_free_prsp_exit:
277         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
278         kfree(prsp);
279
280 els_iocb_free_pcmb_exit:
281         kfree(pcmd);
282         lpfc_sli_release_iocbq(phba, elsiocb);
283         return NULL;
284 }
285
286 /**
287  * lpfc_issue_fabric_reglogin: Issue fabric registration login for a vport.
288  * @vport: pointer to a host virtual N_Port data structure.
289  *
290  * This routine issues a fabric registration login for a @vport. An
291  * active ndlp node with Fabric_DID must already exist for this @vport.
292  * The routine invokes two mailbox commands to carry out fabric registration
293  * login through the HBA firmware: the first mailbox command requests the
294  * HBA to perform link configuration for the @vport; and the second mailbox
295  * command requests the HBA to perform the actual fabric registration login
296  * with the @vport.
297  *
298  * Return code
299  *   0 - successfully issued fabric registration login for @vport
300  *   -ENXIO -- failed to issue fabric registration login for @vport
301  **/
302 static int
303 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
304 {
305         struct lpfc_hba  *phba = vport->phba;
306         LPFC_MBOXQ_t *mbox;
307         struct lpfc_dmabuf *mp;
308         struct lpfc_nodelist *ndlp;
309         struct serv_parm *sp;
310         int rc;
311         int err = 0;
312
313         sp = &phba->fc_fabparam;
314         ndlp = lpfc_findnode_did(vport, Fabric_DID);
315         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
316                 err = 1;
317                 goto fail;
318         }
319
320         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
321         if (!mbox) {
322                 err = 2;
323                 goto fail;
324         }
325
326         vport->port_state = LPFC_FABRIC_CFG_LINK;
327         lpfc_config_link(phba, mbox);
328         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
329         mbox->vport = vport;
330
331         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
332         if (rc == MBX_NOT_FINISHED) {
333                 err = 3;
334                 goto fail_free_mbox;
335         }
336
337         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
338         if (!mbox) {
339                 err = 4;
340                 goto fail;
341         }
342         rc = lpfc_reg_login(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
343                             0);
344         if (rc) {
345                 err = 5;
346                 goto fail_free_mbox;
347         }
348
349         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
350         mbox->vport = vport;
351         /* increment the reference count on ndlp to hold reference
352          * for the callback routine.
353          */
354         mbox->context2 = lpfc_nlp_get(ndlp);
355
356         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
357         if (rc == MBX_NOT_FINISHED) {
358                 err = 6;
359                 goto fail_issue_reg_login;
360         }
361
362         return 0;
363
364 fail_issue_reg_login:
365         /* decrement the reference count on ndlp just incremented
366          * for the failed mbox command.
367          */
368         lpfc_nlp_put(ndlp);
369         mp = (struct lpfc_dmabuf *) mbox->context1;
370         lpfc_mbuf_free(phba, mp->virt, mp->phys);
371         kfree(mp);
372 fail_free_mbox:
373         mempool_free(mbox, phba->mbox_mem_pool);
374
375 fail:
376         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
377         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
378                 "0249 Cannot issue Register Fabric login: Err %d\n", err);
379         return -ENXIO;
380 }
381
382 /**
383  * lpfc_cmpl_els_flogi_fabric: Completion function for flogi to a fabric port.
384  * @vport: pointer to a host virtual N_Port data structure.
385  * @ndlp: pointer to a node-list data structure.
386  * @sp: pointer to service parameter data structure.
387  * @irsp: pointer to the IOCB within the lpfc response IOCB.
388  *
389  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
390  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
391  * port in a fabric topology. It properly sets up the parameters to the @ndlp
392  * from the IOCB response. It also check the newly assigned N_Port ID to the
393  * @vport against the previously assigned N_Port ID. If it is different from
394  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
395  * is invoked on all the remaining nodes with the @vport to unregister the
396  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
397  * is invoked to register login to the fabric.
398  *
399  * Return code
400  *   0 - Success (currently, always return 0)
401  **/
402 static int
403 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
404                            struct serv_parm *sp, IOCB_t *irsp)
405 {
406         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
407         struct lpfc_hba  *phba = vport->phba;
408         struct lpfc_nodelist *np;
409         struct lpfc_nodelist *next_np;
410
411         spin_lock_irq(shost->host_lock);
412         vport->fc_flag |= FC_FABRIC;
413         spin_unlock_irq(shost->host_lock);
414
415         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
416         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
417                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
418
419         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
420
421         if (phba->fc_topology == TOPOLOGY_LOOP) {
422                 spin_lock_irq(shost->host_lock);
423                 vport->fc_flag |= FC_PUBLIC_LOOP;
424                 spin_unlock_irq(shost->host_lock);
425         } else {
426                 /*
427                  * If we are a N-port connected to a Fabric, fixup sparam's so
428                  * logins to devices on remote loops work.
429                  */
430                 vport->fc_sparam.cmn.altBbCredit = 1;
431         }
432
433         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
434         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
435         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
436         ndlp->nlp_class_sup = 0;
437         if (sp->cls1.classValid)
438                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
439         if (sp->cls2.classValid)
440                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
441         if (sp->cls3.classValid)
442                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
443         if (sp->cls4.classValid)
444                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
445         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
446                                 sp->cmn.bbRcvSizeLsb;
447         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
448
449         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
450                 if (sp->cmn.response_multiple_NPort) {
451                         lpfc_printf_vlog(vport, KERN_WARNING,
452                                          LOG_ELS | LOG_VPORT,
453                                          "1816 FLOGI NPIV supported, "
454                                          "response data 0x%x\n",
455                                          sp->cmn.response_multiple_NPort);
456                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
457                 } else {
458                         /* Because we asked f/w for NPIV it still expects us
459                         to call reg_vnpid atleast for the physcial host */
460                         lpfc_printf_vlog(vport, KERN_WARNING,
461                                          LOG_ELS | LOG_VPORT,
462                                          "1817 Fabric does not support NPIV "
463                                          "- configuring single port mode.\n");
464                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
465                 }
466         }
467
468         if ((vport->fc_prevDID != vport->fc_myDID) &&
469                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
470
471                 /* If our NportID changed, we need to ensure all
472                  * remaining NPORTs get unreg_login'ed.
473                  */
474                 list_for_each_entry_safe(np, next_np,
475                                         &vport->fc_nodes, nlp_listp) {
476                         if (!NLP_CHK_NODE_ACT(ndlp))
477                                 continue;
478                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
479                                    !(np->nlp_flag & NLP_NPR_ADISC))
480                                 continue;
481                         spin_lock_irq(shost->host_lock);
482                         np->nlp_flag &= ~NLP_NPR_ADISC;
483                         spin_unlock_irq(shost->host_lock);
484                         lpfc_unreg_rpi(vport, np);
485                 }
486                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
487                         lpfc_mbx_unreg_vpi(vport);
488                         spin_lock_irq(shost->host_lock);
489                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
490                         spin_unlock_irq(shost->host_lock);
491                 }
492         }
493
494         lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
495
496         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
497             vport->fc_flag & FC_VPORT_NEEDS_REG_VPI) {
498                 lpfc_register_new_vport(phba, vport, ndlp);
499                 return 0;
500         }
501         lpfc_issue_fabric_reglogin(vport);
502         return 0;
503 }
504
505 /**
506  * lpfc_cmpl_els_flogi_nport: Completion function for flogi to an N_Port.
507  * @vport: pointer to a host virtual N_Port data structure.
508  * @ndlp: pointer to a node-list data structure.
509  * @sp: pointer to service parameter data structure.
510  *
511  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
512  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
513  * in a point-to-point topology. First, the @vport's N_Port Name is compared
514  * with the received N_Port Name: if the @vport's N_Port Name is greater than
515  * the received N_Port Name lexicographically, this node shall assign local
516  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
517  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
518  * this node shall just wait for the remote node to issue PLOGI and assign
519  * N_Port IDs.
520  *
521  * Return code
522  *   0 - Success
523  *   -ENXIO - Fail
524  **/
525 static int
526 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
527                           struct serv_parm *sp)
528 {
529         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
530         struct lpfc_hba  *phba = vport->phba;
531         LPFC_MBOXQ_t *mbox;
532         int rc;
533
534         spin_lock_irq(shost->host_lock);
535         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
536         spin_unlock_irq(shost->host_lock);
537
538         phba->fc_edtov = FF_DEF_EDTOV;
539         phba->fc_ratov = FF_DEF_RATOV;
540         rc = memcmp(&vport->fc_portname, &sp->portName,
541                     sizeof(vport->fc_portname));
542         if (rc >= 0) {
543                 /* This side will initiate the PLOGI */
544                 spin_lock_irq(shost->host_lock);
545                 vport->fc_flag |= FC_PT2PT_PLOGI;
546                 spin_unlock_irq(shost->host_lock);
547
548                 /*
549                  * N_Port ID cannot be 0, set our to LocalID the other
550                  * side will be RemoteID.
551                  */
552
553                 /* not equal */
554                 if (rc)
555                         vport->fc_myDID = PT2PT_LocalID;
556
557                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
558                 if (!mbox)
559                         goto fail;
560
561                 lpfc_config_link(phba, mbox);
562
563                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
564                 mbox->vport = vport;
565                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
566                 if (rc == MBX_NOT_FINISHED) {
567                         mempool_free(mbox, phba->mbox_mem_pool);
568                         goto fail;
569                 }
570                 /* Decrement ndlp reference count indicating that ndlp can be
571                  * safely released when other references to it are done.
572                  */
573                 lpfc_nlp_put(ndlp);
574
575                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
576                 if (!ndlp) {
577                         /*
578                          * Cannot find existing Fabric ndlp, so allocate a
579                          * new one
580                          */
581                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
582                         if (!ndlp)
583                                 goto fail;
584                         lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
585                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
586                         ndlp = lpfc_enable_node(vport, ndlp,
587                                                 NLP_STE_UNUSED_NODE);
588                         if(!ndlp)
589                                 goto fail;
590                 }
591
592                 memcpy(&ndlp->nlp_portname, &sp->portName,
593                        sizeof(struct lpfc_name));
594                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
595                        sizeof(struct lpfc_name));
596                 /* Set state will put ndlp onto node list if not already done */
597                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
598                 spin_lock_irq(shost->host_lock);
599                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
600                 spin_unlock_irq(shost->host_lock);
601         } else
602                 /* This side will wait for the PLOGI, decrement ndlp reference
603                  * count indicating that ndlp can be released when other
604                  * references to it are done.
605                  */
606                 lpfc_nlp_put(ndlp);
607
608         /* If we are pt2pt with another NPort, force NPIV off! */
609         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
610
611         spin_lock_irq(shost->host_lock);
612         vport->fc_flag |= FC_PT2PT;
613         spin_unlock_irq(shost->host_lock);
614
615         /* Start discovery - this should just do CLEAR_LA */
616         lpfc_disc_start(vport);
617         return 0;
618 fail:
619         return -ENXIO;
620 }
621
622 /**
623  * lpfc_cmpl_els_flogi: Completion callback function for flogi.
624  * @phba: pointer to lpfc hba data structure.
625  * @cmdiocb: pointer to lpfc command iocb data structure.
626  * @rspiocb: pointer to lpfc response iocb data structure.
627  *
628  * This routine is the top-level completion callback function for issuing
629  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
630  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
631  * retry has been made (either immediately or delayed with lpfc_els_retry()
632  * returning 1), the command IOCB will be released and function returned.
633  * If the retry attempt has been given up (possibly reach the maximum
634  * number of retries), one additional decrement of ndlp reference shall be
635  * invoked before going out after releasing the command IOCB. This will
636  * actually release the remote node (Note, lpfc_els_free_iocb() will also
637  * invoke one decrement of ndlp reference count). If no error reported in
638  * the IOCB status, the command Port ID field is used to determine whether
639  * this is a point-to-point topology or a fabric topology: if the Port ID
640  * field is assigned, it is a fabric topology; otherwise, it is a
641  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
642  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
643  * specific topology completion conditions.
644  **/
645 static void
646 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
647                     struct lpfc_iocbq *rspiocb)
648 {
649         struct lpfc_vport *vport = cmdiocb->vport;
650         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
651         IOCB_t *irsp = &rspiocb->iocb;
652         struct lpfc_nodelist *ndlp = cmdiocb->context1;
653         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
654         struct serv_parm *sp;
655         int rc;
656
657         /* Check to see if link went down during discovery */
658         if (lpfc_els_chk_latt(vport)) {
659                 /* One additional decrement on node reference count to
660                  * trigger the release of the node
661                  */
662                 lpfc_nlp_put(ndlp);
663                 goto out;
664         }
665
666         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
667                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
668                 irsp->ulpStatus, irsp->un.ulpWord[4],
669                 vport->port_state);
670
671         if (irsp->ulpStatus) {
672                 /* Check for retry */
673                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
674                         goto out;
675
676                 /* FLOGI failed, so there is no fabric */
677                 spin_lock_irq(shost->host_lock);
678                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
679                 spin_unlock_irq(shost->host_lock);
680
681                 /* If private loop, then allow max outstanding els to be
682                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
683                  * alpa map would take too long otherwise.
684                  */
685                 if (phba->alpa_map[0] == 0) {
686                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
687                 }
688
689                 /* FLOGI failure */
690                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
691                                  "0100 FLOGI failure Data: x%x x%x "
692                                  "x%x\n",
693                                  irsp->ulpStatus, irsp->un.ulpWord[4],
694                                  irsp->ulpTimeout);
695                 goto flogifail;
696         }
697
698         /*
699          * The FLogI succeeded.  Sync the data for the CPU before
700          * accessing it.
701          */
702         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
703
704         sp = prsp->virt + sizeof(uint32_t);
705
706         /* FLOGI completes successfully */
707         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
708                          "0101 FLOGI completes sucessfully "
709                          "Data: x%x x%x x%x x%x\n",
710                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
711                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
712
713         if (vport->port_state == LPFC_FLOGI) {
714                 /*
715                  * If Common Service Parameters indicate Nport
716                  * we are point to point, if Fport we are Fabric.
717                  */
718                 if (sp->cmn.fPort)
719                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
720                 else
721                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
722
723                 if (!rc)
724                         goto out;
725         }
726
727 flogifail:
728         lpfc_nlp_put(ndlp);
729
730         if (!lpfc_error_lost_link(irsp)) {
731                 /* FLOGI failed, so just use loop map to make discovery list */
732                 lpfc_disc_list_loopmap(vport);
733
734                 /* Start discovery */
735                 lpfc_disc_start(vport);
736         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
737                         ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
738                         (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
739                         (phba->link_state != LPFC_CLEAR_LA)) {
740                 /* If FLOGI failed enable link interrupt. */
741                 lpfc_issue_clear_la(phba, vport);
742         }
743 out:
744         lpfc_els_free_iocb(phba, cmdiocb);
745 }
746
747 /**
748  * lpfc_issue_els_flogi: Issue an flogi iocb command for a vport.
749  * @vport: pointer to a host virtual N_Port data structure.
750  * @ndlp: pointer to a node-list data structure.
751  * @retry: number of retries to the command IOCB.
752  *
753  * This routine issues a Fabric Login (FLOGI) Request ELS command
754  * for a @vport. The initiator service parameters are put into the payload
755  * of the FLOGI Request IOCB and the top-level callback function pointer
756  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
757  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
758  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
759  *
760  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
761  * will be incremented by 1 for holding the ndlp and the reference to ndlp
762  * will be stored into the context1 field of the IOCB for the completion
763  * callback function to the FLOGI ELS command.
764  *
765  * Return code
766  *   0 - successfully issued flogi iocb for @vport
767  *   1 - failed to issue flogi iocb for @vport
768  **/
769 static int
770 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
771                      uint8_t retry)
772 {
773         struct lpfc_hba  *phba = vport->phba;
774         struct serv_parm *sp;
775         IOCB_t *icmd;
776         struct lpfc_iocbq *elsiocb;
777         struct lpfc_sli_ring *pring;
778         uint8_t *pcmd;
779         uint16_t cmdsize;
780         uint32_t tmo;
781         int rc;
782
783         pring = &phba->sli.ring[LPFC_ELS_RING];
784
785         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
786         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
787                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
788
789         if (!elsiocb)
790                 return 1;
791
792         icmd = &elsiocb->iocb;
793         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
794
795         /* For FLOGI request, remainder of payload is service parameters */
796         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
797         pcmd += sizeof(uint32_t);
798         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
799         sp = (struct serv_parm *) pcmd;
800
801         /* Setup CSPs accordingly for Fabric */
802         sp->cmn.e_d_tov = 0;
803         sp->cmn.w2.r_a_tov = 0;
804         sp->cls1.classValid = 0;
805         sp->cls2.seqDelivery = 1;
806         sp->cls3.seqDelivery = 1;
807         if (sp->cmn.fcphLow < FC_PH3)
808                 sp->cmn.fcphLow = FC_PH3;
809         if (sp->cmn.fcphHigh < FC_PH3)
810                 sp->cmn.fcphHigh = FC_PH3;
811
812         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
813                 sp->cmn.request_multiple_Nport = 1;
814
815                 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
816                 icmd->ulpCt_h = 1;
817                 icmd->ulpCt_l = 0;
818         }
819
820         if (phba->fc_topology != TOPOLOGY_LOOP) {
821                 icmd->un.elsreq64.myID = 0;
822                 icmd->un.elsreq64.fl = 1;
823         }
824
825         tmo = phba->fc_ratov;
826         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
827         lpfc_set_disctmo(vport);
828         phba->fc_ratov = tmo;
829
830         phba->fc_stat.elsXmitFLOGI++;
831         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
832
833         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
834                 "Issue FLOGI:     opt:x%x",
835                 phba->sli3_options, 0, 0);
836
837         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
838         if (rc == IOCB_ERROR) {
839                 lpfc_els_free_iocb(phba, elsiocb);
840                 return 1;
841         }
842         return 0;
843 }
844
845 /**
846  * lpfc_els_abort_flogi: Abort all outstanding flogi iocbs.
847  * @phba: pointer to lpfc hba data structure.
848  *
849  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
850  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
851  * list and issues an abort IOCB commond on each outstanding IOCB that
852  * contains a active Fabric_DID ndlp. Note that this function is to issue
853  * the abort IOCB command on all the outstanding IOCBs, thus when this
854  * function returns, it does not guarantee all the IOCBs are actually aborted.
855  *
856  * Return code
857  *   0 - Sucessfully issued abort iocb on all outstanding flogis (Always 0)
858  **/
859 int
860 lpfc_els_abort_flogi(struct lpfc_hba *phba)
861 {
862         struct lpfc_sli_ring *pring;
863         struct lpfc_iocbq *iocb, *next_iocb;
864         struct lpfc_nodelist *ndlp;
865         IOCB_t *icmd;
866
867         /* Abort outstanding I/O on NPort <nlp_DID> */
868         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
869                         "0201 Abort outstanding I/O on NPort x%x\n",
870                         Fabric_DID);
871
872         pring = &phba->sli.ring[LPFC_ELS_RING];
873
874         /*
875          * Check the txcmplq for an iocb that matches the nport the driver is
876          * searching for.
877          */
878         spin_lock_irq(&phba->hbalock);
879         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
880                 icmd = &iocb->iocb;
881                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
882                     icmd->un.elsreq64.bdl.ulpIoTag32) {
883                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
884                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
885                             (ndlp->nlp_DID == Fabric_DID))
886                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
887                 }
888         }
889         spin_unlock_irq(&phba->hbalock);
890
891         return 0;
892 }
893
894 /**
895  * lpfc_initial_flogi: Issue an initial fabric login for a vport.
896  * @vport: pointer to a host virtual N_Port data structure.
897  *
898  * This routine issues an initial Fabric Login (FLOGI) for the @vport
899  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
900  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
901  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
902  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
903  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
904  * @vport.
905  *
906  * Return code
907  *   0 - failed to issue initial flogi for @vport
908  *   1 - successfully issued initial flogi for @vport
909  **/
910 int
911 lpfc_initial_flogi(struct lpfc_vport *vport)
912 {
913         struct lpfc_hba *phba = vport->phba;
914         struct lpfc_nodelist *ndlp;
915
916         vport->port_state = LPFC_FLOGI;
917         lpfc_set_disctmo(vport);
918
919         /* First look for the Fabric ndlp */
920         ndlp = lpfc_findnode_did(vport, Fabric_DID);
921         if (!ndlp) {
922                 /* Cannot find existing Fabric ndlp, so allocate a new one */
923                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
924                 if (!ndlp)
925                         return 0;
926                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
927                 /* Put ndlp onto node list */
928                 lpfc_enqueue_node(vport, ndlp);
929         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
930                 /* re-setup ndlp without removing from node list */
931                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
932                 if (!ndlp)
933                         return 0;
934         }
935
936         if (lpfc_issue_els_flogi(vport, ndlp, 0))
937                 /* This decrement of reference count to node shall kick off
938                  * the release of the node.
939                  */
940                 lpfc_nlp_put(ndlp);
941
942         return 1;
943 }
944
945 /**
946  * lpfc_initial_fdisc: Issue an initial fabric discovery for a vport.
947  * @vport: pointer to a host virtual N_Port data structure.
948  *
949  * This routine issues an initial Fabric Discover (FDISC) for the @vport
950  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
951  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
952  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
953  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
954  * is then invoked with the @vport and the ndlp to perform the FDISC for the
955  * @vport.
956  *
957  * Return code
958  *   0 - failed to issue initial fdisc for @vport
959  *   1 - successfully issued initial fdisc for @vport
960  **/
961 int
962 lpfc_initial_fdisc(struct lpfc_vport *vport)
963 {
964         struct lpfc_hba *phba = vport->phba;
965         struct lpfc_nodelist *ndlp;
966
967         /* First look for the Fabric ndlp */
968         ndlp = lpfc_findnode_did(vport, Fabric_DID);
969         if (!ndlp) {
970                 /* Cannot find existing Fabric ndlp, so allocate a new one */
971                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
972                 if (!ndlp)
973                         return 0;
974                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
975                 /* Put ndlp onto node list */
976                 lpfc_enqueue_node(vport, ndlp);
977         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
978                 /* re-setup ndlp without removing from node list */
979                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
980                 if (!ndlp)
981                         return 0;
982         }
983
984         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
985                 /* decrement node reference count to trigger the release of
986                  * the node.
987                  */
988                 lpfc_nlp_put(ndlp);
989                 return 0;
990         }
991         return 1;
992 }
993
994 /**
995  * lpfc_more_plogi: Check and issue remaining plogis for a vport.
996  * @vport: pointer to a host virtual N_Port data structure.
997  *
998  * This routine checks whether there are more remaining Port Logins
999  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1000  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1001  * to issue ELS PLOGIs up to the configured discover threads with the
1002  * @vport (@vport->cfg_discovery_threads). The function also decrement
1003  * the @vport's num_disc_node by 1 if it is not already 0.
1004  **/
1005 void
1006 lpfc_more_plogi(struct lpfc_vport *vport)
1007 {
1008         int sentplogi;
1009
1010         if (vport->num_disc_nodes)
1011                 vport->num_disc_nodes--;
1012
1013         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1014         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1015                          "0232 Continue discovery with %d PLOGIs to go "
1016                          "Data: x%x x%x x%x\n",
1017                          vport->num_disc_nodes, vport->fc_plogi_cnt,
1018                          vport->fc_flag, vport->port_state);
1019         /* Check to see if there are more PLOGIs to be sent */
1020         if (vport->fc_flag & FC_NLP_MORE)
1021                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1022                 sentplogi = lpfc_els_disc_plogi(vport);
1023
1024         return;
1025 }
1026
1027 /**
1028  * lpfc_plogi_confirm_nport: Confirm pologi wwpn matches stored ndlp.
1029  * @phba: pointer to lpfc hba data structure.
1030  * @prsp: pointer to response IOCB payload.
1031  * @ndlp: pointer to a node-list data structure.
1032  *
1033  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1034  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1035  * The following cases are considered N_Port confirmed:
1036  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1037  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1038  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1039  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1040  * 1) if there is a node on vport list other than the @ndlp with the same
1041  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1042  * on that node to release the RPI associated with the node; 2) if there is
1043  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1044  * into, a new node shall be allocated (or activated). In either case, the
1045  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1046  * be released and the new_ndlp shall be put on to the vport node list and
1047  * its pointer returned as the confirmed node.
1048  *
1049  * Note that before the @ndlp got "released", the keepDID from not-matching
1050  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1051  * of the @ndlp. This is because the release of @ndlp is actually to put it
1052  * into an inactive state on the vport node list and the vport node list
1053  * management algorithm does not allow two node with a same DID.
1054  *
1055  * Return code
1056  *   pointer to the PLOGI N_Port @ndlp
1057  **/
1058 static struct lpfc_nodelist *
1059 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1060                          struct lpfc_nodelist *ndlp)
1061 {
1062         struct lpfc_vport    *vport = ndlp->vport;
1063         struct lpfc_nodelist *new_ndlp;
1064         struct lpfc_rport_data *rdata;
1065         struct fc_rport *rport;
1066         struct serv_parm *sp;
1067         uint8_t  name[sizeof(struct lpfc_name)];
1068         uint32_t rc, keepDID = 0;
1069
1070         /* Fabric nodes can have the same WWPN so we don't bother searching
1071          * by WWPN.  Just return the ndlp that was given to us.
1072          */
1073         if (ndlp->nlp_type & NLP_FABRIC)
1074                 return ndlp;
1075
1076         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1077         memset(name, 0, sizeof(struct lpfc_name));
1078
1079         /* Now we find out if the NPort we are logging into, matches the WWPN
1080          * we have for that ndlp. If not, we have some work to do.
1081          */
1082         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1083
1084         if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1085                 return ndlp;
1086
1087         if (!new_ndlp) {
1088                 rc = memcmp(&ndlp->nlp_portname, name,
1089                             sizeof(struct lpfc_name));
1090                 if (!rc)
1091                         return ndlp;
1092                 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1093                 if (!new_ndlp)
1094                         return ndlp;
1095                 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
1096         } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1097                 rc = memcmp(&ndlp->nlp_portname, name,
1098                             sizeof(struct lpfc_name));
1099                 if (!rc)
1100                         return ndlp;
1101                 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1102                                                 NLP_STE_UNUSED_NODE);
1103                 if (!new_ndlp)
1104                         return ndlp;
1105                 keepDID = new_ndlp->nlp_DID;
1106         } else
1107                 keepDID = new_ndlp->nlp_DID;
1108
1109         lpfc_unreg_rpi(vport, new_ndlp);
1110         new_ndlp->nlp_DID = ndlp->nlp_DID;
1111         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1112
1113         if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
1114                 new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1115         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1116
1117         /* Set state will put new_ndlp on to node list if not already done */
1118         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1119
1120         /* Move this back to NPR state */
1121         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1122                 /* The new_ndlp is replacing ndlp totally, so we need
1123                  * to put ndlp on UNUSED list and try to free it.
1124                  */
1125
1126                 /* Fix up the rport accordingly */
1127                 rport =  ndlp->rport;
1128                 if (rport) {
1129                         rdata = rport->dd_data;
1130                         if (rdata->pnode == ndlp) {
1131                                 lpfc_nlp_put(ndlp);
1132                                 ndlp->rport = NULL;
1133                                 rdata->pnode = lpfc_nlp_get(new_ndlp);
1134                                 new_ndlp->rport = rport;
1135                         }
1136                         new_ndlp->nlp_type = ndlp->nlp_type;
1137                 }
1138                 /* We shall actually free the ndlp with both nlp_DID and
1139                  * nlp_portname fields equals 0 to avoid any ndlp on the
1140                  * nodelist never to be used.
1141                  */
1142                 if (ndlp->nlp_DID == 0) {
1143                         spin_lock_irq(&phba->ndlp_lock);
1144                         NLP_SET_FREE_REQ(ndlp);
1145                         spin_unlock_irq(&phba->ndlp_lock);
1146                 }
1147
1148                 /* Two ndlps cannot have the same did on the nodelist */
1149                 ndlp->nlp_DID = keepDID;
1150                 lpfc_drop_node(vport, ndlp);
1151         }
1152         else {
1153                 lpfc_unreg_rpi(vport, ndlp);
1154                 /* Two ndlps cannot have the same did */
1155                 ndlp->nlp_DID = keepDID;
1156                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1157         }
1158         return new_ndlp;
1159 }
1160
1161 /**
1162  * lpfc_end_rscn: Check and handle more rscn for a vport.
1163  * @vport: pointer to a host virtual N_Port data structure.
1164  *
1165  * This routine checks whether more Registration State Change
1166  * Notifications (RSCNs) came in while the discovery state machine was in
1167  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1168  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1169  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1170  * handling the RSCNs.
1171  **/
1172 void
1173 lpfc_end_rscn(struct lpfc_vport *vport)
1174 {
1175         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1176
1177         if (vport->fc_flag & FC_RSCN_MODE) {
1178                 /*
1179                  * Check to see if more RSCNs came in while we were
1180                  * processing this one.
1181                  */
1182                 if (vport->fc_rscn_id_cnt ||
1183                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1184                         lpfc_els_handle_rscn(vport);
1185                 else {
1186                         spin_lock_irq(shost->host_lock);
1187                         vport->fc_flag &= ~FC_RSCN_MODE;
1188                         spin_unlock_irq(shost->host_lock);
1189                 }
1190         }
1191 }
1192
1193 /**
1194  * lpfc_cmpl_els_plogi: Completion callback function for plogi.
1195  * @phba: pointer to lpfc hba data structure.
1196  * @cmdiocb: pointer to lpfc command iocb data structure.
1197  * @rspiocb: pointer to lpfc response iocb data structure.
1198  *
1199  * This routine is the completion callback function for issuing the Port
1200  * Login (PLOGI) command. For PLOGI completion, there must be an active
1201  * ndlp on the vport node list that matches the remote node ID from the
1202  * PLOGI reponse IOCB. If such ndlp does not exist, the PLOGI is simply
1203  * ignored and command IOCB released. The PLOGI response IOCB status is
1204  * checked for error conditons. If there is error status reported, PLOGI
1205  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1206  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1207  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1208  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1209  * there are additional N_Port nodes with the vport that need to perform
1210  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1211  * PLOGIs.
1212  **/
1213 static void
1214 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1215                     struct lpfc_iocbq *rspiocb)
1216 {
1217         struct lpfc_vport *vport = cmdiocb->vport;
1218         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1219         IOCB_t *irsp;
1220         struct lpfc_nodelist *ndlp;
1221         struct lpfc_dmabuf *prsp;
1222         int disc, rc, did, type;
1223
1224         /* we pass cmdiocb to state machine which needs rspiocb as well */
1225         cmdiocb->context_un.rsp_iocb = rspiocb;
1226
1227         irsp = &rspiocb->iocb;
1228         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1229                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
1230                 irsp->ulpStatus, irsp->un.ulpWord[4],
1231                 irsp->un.elsreq64.remoteID);
1232
1233         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1234         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1235                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1236                                  "0136 PLOGI completes to NPort x%x "
1237                                  "with no ndlp. Data: x%x x%x x%x\n",
1238                                  irsp->un.elsreq64.remoteID,
1239                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1240                                  irsp->ulpIoTag);
1241                 goto out;
1242         }
1243
1244         /* Since ndlp can be freed in the disc state machine, note if this node
1245          * is being used during discovery.
1246          */
1247         spin_lock_irq(shost->host_lock);
1248         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1249         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1250         spin_unlock_irq(shost->host_lock);
1251         rc   = 0;
1252
1253         /* PLOGI completes to NPort <nlp_DID> */
1254         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1255                          "0102 PLOGI completes to NPort x%x "
1256                          "Data: x%x x%x x%x x%x x%x\n",
1257                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1258                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1259         /* Check to see if link went down during discovery */
1260         if (lpfc_els_chk_latt(vport)) {
1261                 spin_lock_irq(shost->host_lock);
1262                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1263                 spin_unlock_irq(shost->host_lock);
1264                 goto out;
1265         }
1266
1267         /* ndlp could be freed in DSM, save these values now */
1268         type = ndlp->nlp_type;
1269         did = ndlp->nlp_DID;
1270
1271         if (irsp->ulpStatus) {
1272                 /* Check for retry */
1273                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1274                         /* ELS command is being retried */
1275                         if (disc) {
1276                                 spin_lock_irq(shost->host_lock);
1277                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1278                                 spin_unlock_irq(shost->host_lock);
1279                         }
1280                         goto out;
1281                 }
1282                 /* PLOGI failed */
1283                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1284                 if (lpfc_error_lost_link(irsp))
1285                         rc = NLP_STE_FREED_NODE;
1286                 else
1287                         rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1288                                                      NLP_EVT_CMPL_PLOGI);
1289         } else {
1290                 /* Good status, call state machine */
1291                 prsp = list_entry(((struct lpfc_dmabuf *)
1292                                    cmdiocb->context2)->list.next,
1293                                   struct lpfc_dmabuf, list);
1294                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1295                 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1296                                              NLP_EVT_CMPL_PLOGI);
1297         }
1298
1299         if (disc && vport->num_disc_nodes) {
1300                 /* Check to see if there are more PLOGIs to be sent */
1301                 lpfc_more_plogi(vport);
1302
1303                 if (vport->num_disc_nodes == 0) {
1304                         spin_lock_irq(shost->host_lock);
1305                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
1306                         spin_unlock_irq(shost->host_lock);
1307
1308                         lpfc_can_disctmo(vport);
1309                         lpfc_end_rscn(vport);
1310                 }
1311         }
1312
1313 out:
1314         lpfc_els_free_iocb(phba, cmdiocb);
1315         return;
1316 }
1317
1318 /**
1319  * lpfc_issue_els_plogi: Issue an plogi iocb command for a vport.
1320  * @vport: pointer to a host virtual N_Port data structure.
1321  * @did: destination port identifier.
1322  * @retry: number of retries to the command IOCB.
1323  *
1324  * This routine issues a Port Login (PLOGI) command to a remote N_Port
1325  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1326  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1327  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1328  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1329  *
1330  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1331  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1332  * will be stored into the context1 field of the IOCB for the completion
1333  * callback function to the PLOGI ELS command.
1334  *
1335  * Return code
1336  *   0 - Successfully issued a plogi for @vport
1337  *   1 - failed to issue a plogi for @vport
1338  **/
1339 int
1340 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1341 {
1342         struct lpfc_hba  *phba = vport->phba;
1343         struct serv_parm *sp;
1344         IOCB_t *icmd;
1345         struct lpfc_nodelist *ndlp;
1346         struct lpfc_iocbq *elsiocb;
1347         struct lpfc_sli_ring *pring;
1348         struct lpfc_sli *psli;
1349         uint8_t *pcmd;
1350         uint16_t cmdsize;
1351         int ret;
1352
1353         psli = &phba->sli;
1354         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1355
1356         ndlp = lpfc_findnode_did(vport, did);
1357         if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1358                 ndlp = NULL;
1359
1360         /* If ndlp is not NULL, we will bump the reference count on it */
1361         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1362         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
1363                                      ELS_CMD_PLOGI);
1364         if (!elsiocb)
1365                 return 1;
1366
1367         icmd = &elsiocb->iocb;
1368         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1369
1370         /* For PLOGI request, remainder of payload is service parameters */
1371         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
1372         pcmd += sizeof(uint32_t);
1373         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1374         sp = (struct serv_parm *) pcmd;
1375
1376         if (sp->cmn.fcphLow < FC_PH_4_3)
1377                 sp->cmn.fcphLow = FC_PH_4_3;
1378
1379         if (sp->cmn.fcphHigh < FC_PH3)
1380                 sp->cmn.fcphHigh = FC_PH3;
1381
1382         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1383                 "Issue PLOGI:     did:x%x",
1384                 did, 0, 0);
1385
1386         phba->fc_stat.elsXmitPLOGI++;
1387         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1388         ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1389
1390         if (ret == IOCB_ERROR) {
1391                 lpfc_els_free_iocb(phba, elsiocb);
1392                 return 1;
1393         }
1394         return 0;
1395 }
1396
1397 /**
1398  * lpfc_cmpl_els_prli: Completion callback function for prli.
1399  * @phba: pointer to lpfc hba data structure.
1400  * @cmdiocb: pointer to lpfc command iocb data structure.
1401  * @rspiocb: pointer to lpfc response iocb data structure.
1402  *
1403  * This routine is the completion callback function for a Process Login
1404  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
1405  * status. If there is error status reported, PRLI retry shall be attempted
1406  * by invoking the lpfc_els_retry() routine. Otherwise, the state
1407  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
1408  * ndlp to mark the PRLI completion.
1409  **/
1410 static void
1411 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1412                    struct lpfc_iocbq *rspiocb)
1413 {
1414         struct lpfc_vport *vport = cmdiocb->vport;
1415         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1416         IOCB_t *irsp;
1417         struct lpfc_sli *psli;
1418         struct lpfc_nodelist *ndlp;
1419
1420         psli = &phba->sli;
1421         /* we pass cmdiocb to state machine which needs rspiocb as well */
1422         cmdiocb->context_un.rsp_iocb = rspiocb;
1423
1424         irsp = &(rspiocb->iocb);
1425         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1426         spin_lock_irq(shost->host_lock);
1427         ndlp->nlp_flag &= ~NLP_PRLI_SND;
1428         spin_unlock_irq(shost->host_lock);
1429
1430         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1431                 "PRLI cmpl:       status:x%x/x%x did:x%x",
1432                 irsp->ulpStatus, irsp->un.ulpWord[4],
1433                 ndlp->nlp_DID);
1434         /* PRLI completes to NPort <nlp_DID> */
1435         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1436                          "0103 PRLI completes to NPort x%x "
1437                          "Data: x%x x%x x%x x%x\n",
1438                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1439                          irsp->ulpTimeout, vport->num_disc_nodes);
1440
1441         vport->fc_prli_sent--;
1442         /* Check to see if link went down during discovery */
1443         if (lpfc_els_chk_latt(vport))
1444                 goto out;
1445
1446         if (irsp->ulpStatus) {
1447                 /* Check for retry */
1448                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1449                         /* ELS command is being retried */
1450                         goto out;
1451                 }
1452                 /* PRLI failed */
1453                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1454                 if (lpfc_error_lost_link(irsp))
1455                         goto out;
1456                 else
1457                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1458                                                 NLP_EVT_CMPL_PRLI);
1459         } else
1460                 /* Good status, call state machine */
1461                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1462                                         NLP_EVT_CMPL_PRLI);
1463 out:
1464         lpfc_els_free_iocb(phba, cmdiocb);
1465         return;
1466 }
1467
1468 /**
1469  * lpfc_issue_els_prli: Issue a prli iocb command for a vport.
1470  * @vport: pointer to a host virtual N_Port data structure.
1471  * @ndlp: pointer to a node-list data structure.
1472  * @retry: number of retries to the command IOCB.
1473  *
1474  * This routine issues a Process Login (PRLI) ELS command for the
1475  * @vport. The PRLI service parameters are set up in the payload of the
1476  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
1477  * is put to the IOCB completion callback func field before invoking the
1478  * routine lpfc_sli_issue_iocb() to send out PRLI command.
1479  *
1480  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1481  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1482  * will be stored into the context1 field of the IOCB for the completion
1483  * callback function to the PRLI ELS command.
1484  *
1485  * Return code
1486  *   0 - successfully issued prli iocb command for @vport
1487  *   1 - failed to issue prli iocb command for @vport
1488  **/
1489 int
1490 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1491                     uint8_t retry)
1492 {
1493         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1494         struct lpfc_hba *phba = vport->phba;
1495         PRLI *npr;
1496         IOCB_t *icmd;
1497         struct lpfc_iocbq *elsiocb;
1498         struct lpfc_sli_ring *pring;
1499         struct lpfc_sli *psli;
1500         uint8_t *pcmd;
1501         uint16_t cmdsize;
1502
1503         psli = &phba->sli;
1504         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1505
1506         cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1507         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1508                                      ndlp->nlp_DID, ELS_CMD_PRLI);
1509         if (!elsiocb)
1510                 return 1;
1511
1512         icmd = &elsiocb->iocb;
1513         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1514
1515         /* For PRLI request, remainder of payload is service parameters */
1516         memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1517         *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1518         pcmd += sizeof(uint32_t);
1519
1520         /* For PRLI, remainder of payload is PRLI parameter page */
1521         npr = (PRLI *) pcmd;
1522         /*
1523          * If our firmware version is 3.20 or later,
1524          * set the following bits for FC-TAPE support.
1525          */
1526         if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1527                 npr->ConfmComplAllowed = 1;
1528                 npr->Retry = 1;
1529                 npr->TaskRetryIdReq = 1;
1530         }
1531         npr->estabImagePair = 1;
1532         npr->readXferRdyDis = 1;
1533
1534         /* For FCP support */
1535         npr->prliType = PRLI_FCP_TYPE;
1536         npr->initiatorFunc = 1;
1537
1538         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1539                 "Issue PRLI:      did:x%x",
1540                 ndlp->nlp_DID, 0, 0);
1541
1542         phba->fc_stat.elsXmitPRLI++;
1543         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1544         spin_lock_irq(shost->host_lock);
1545         ndlp->nlp_flag |= NLP_PRLI_SND;
1546         spin_unlock_irq(shost->host_lock);
1547         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1548                 spin_lock_irq(shost->host_lock);
1549                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
1550                 spin_unlock_irq(shost->host_lock);
1551                 lpfc_els_free_iocb(phba, elsiocb);
1552                 return 1;
1553         }
1554         vport->fc_prli_sent++;
1555         return 0;
1556 }
1557
1558 /**
1559  * lpfc_more_adisc: Issue more adisc as needed.
1560  * @vport: pointer to a host virtual N_Port data structure.
1561  *
1562  * This routine determines whether there are more ndlps on a @vport
1563  * node list need to have Address Discover (ADISC) issued. If so, it will
1564  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
1565  * remaining nodes which need to have ADISC sent.
1566  **/
1567 void
1568 lpfc_more_adisc(struct lpfc_vport *vport)
1569 {
1570         int sentadisc;
1571
1572         if (vport->num_disc_nodes)
1573                 vport->num_disc_nodes--;
1574         /* Continue discovery with <num_disc_nodes> ADISCs to go */
1575         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1576                          "0210 Continue discovery with %d ADISCs to go "
1577                          "Data: x%x x%x x%x\n",
1578                          vport->num_disc_nodes, vport->fc_adisc_cnt,
1579                          vport->fc_flag, vport->port_state);
1580         /* Check to see if there are more ADISCs to be sent */
1581         if (vport->fc_flag & FC_NLP_MORE) {
1582                 lpfc_set_disctmo(vport);
1583                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
1584                 sentadisc = lpfc_els_disc_adisc(vport);
1585         }
1586         return;
1587 }
1588
1589 /**
1590  * lpfc_rscn_disc: Perform rscn discovery for a vport.
1591  * @vport: pointer to a host virtual N_Port data structure.
1592  *
1593  * This routine performs Registration State Change Notification (RSCN)
1594  * discovery for a @vport. If the @vport's node port recovery count is not
1595  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
1596  * the nodes that need recovery. If none of the PLOGI were needed through
1597  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
1598  * invoked to check and handle possible more RSCN came in during the period
1599  * of processing the current ones.
1600  **/
1601 static void
1602 lpfc_rscn_disc(struct lpfc_vport *vport)
1603 {
1604         lpfc_can_disctmo(vport);
1605
1606         /* RSCN discovery */
1607         /* go thru NPR nodes and issue ELS PLOGIs */
1608         if (vport->fc_npr_cnt)
1609                 if (lpfc_els_disc_plogi(vport))
1610                         return;
1611
1612         lpfc_end_rscn(vport);
1613 }
1614
1615 /**
1616  * lpfc_cmpl_els_adisc: Completion callback function for adisc.
1617  * @phba: pointer to lpfc hba data structure.
1618  * @cmdiocb: pointer to lpfc command iocb data structure.
1619  * @rspiocb: pointer to lpfc response iocb data structure.
1620  *
1621  * This routine is the completion function for issuing the Address Discover
1622  * (ADISC) command. It first checks to see whether link went down during
1623  * the discovery process. If so, the node will be marked as node port
1624  * recovery for issuing discover IOCB by the link attention handler and
1625  * exit. Otherwise, the response status is checked. If error was reported
1626  * in the response status, the ADISC command shall be retried by invoking
1627  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
1628  * the response status, the state machine is invoked to set transition
1629  * with respect to NLP_EVT_CMPL_ADISC event.
1630  **/
1631 static void
1632 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1633                     struct lpfc_iocbq *rspiocb)
1634 {
1635         struct lpfc_vport *vport = cmdiocb->vport;
1636         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1637         IOCB_t *irsp;
1638         struct lpfc_nodelist *ndlp;
1639         int  disc;
1640
1641         /* we pass cmdiocb to state machine which needs rspiocb as well */
1642         cmdiocb->context_un.rsp_iocb = rspiocb;
1643
1644         irsp = &(rspiocb->iocb);
1645         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1646
1647         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1648                 "ADISC cmpl:      status:x%x/x%x did:x%x",
1649                 irsp->ulpStatus, irsp->un.ulpWord[4],
1650                 ndlp->nlp_DID);
1651
1652         /* Since ndlp can be freed in the disc state machine, note if this node
1653          * is being used during discovery.
1654          */
1655         spin_lock_irq(shost->host_lock);
1656         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1657         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1658         spin_unlock_irq(shost->host_lock);
1659         /* ADISC completes to NPort <nlp_DID> */
1660         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1661                          "0104 ADISC completes to NPort x%x "
1662                          "Data: x%x x%x x%x x%x x%x\n",
1663                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1664                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1665         /* Check to see if link went down during discovery */
1666         if (lpfc_els_chk_latt(vport)) {
1667                 spin_lock_irq(shost->host_lock);
1668                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1669                 spin_unlock_irq(shost->host_lock);
1670                 goto out;
1671         }
1672
1673         if (irsp->ulpStatus) {
1674                 /* Check for retry */
1675                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1676                         /* ELS command is being retried */
1677                         if (disc) {
1678                                 spin_lock_irq(shost->host_lock);
1679                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1680                                 spin_unlock_irq(shost->host_lock);
1681                                 lpfc_set_disctmo(vport);
1682                         }
1683                         goto out;
1684                 }
1685                 /* ADISC failed */
1686                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1687                 if (!lpfc_error_lost_link(irsp))
1688                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1689                                                 NLP_EVT_CMPL_ADISC);
1690         } else
1691                 /* Good status, call state machine */
1692                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1693                                         NLP_EVT_CMPL_ADISC);
1694
1695         if (disc && vport->num_disc_nodes) {
1696                 /* Check to see if there are more ADISCs to be sent */
1697                 lpfc_more_adisc(vport);
1698
1699                 /* Check to see if we are done with ADISC authentication */
1700                 if (vport->num_disc_nodes == 0) {
1701                         /* If we get here, there is nothing left to ADISC */
1702                         /*
1703                          * For NPIV, cmpl_reg_vpi will set port_state to READY,
1704                          * and continue discovery.
1705                          */
1706                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1707                            !(vport->fc_flag & FC_RSCN_MODE)) {
1708                                 lpfc_issue_reg_vpi(phba, vport);
1709                                 goto out;
1710                         }
1711                         /*
1712                          * For SLI2, we need to set port_state to READY
1713                          * and continue discovery.
1714                          */
1715                         if (vport->port_state < LPFC_VPORT_READY) {
1716                                 /* If we get here, there is nothing to ADISC */
1717                                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1718                                         lpfc_issue_clear_la(phba, vport);
1719
1720                                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1721                                         vport->num_disc_nodes = 0;
1722                                         /* go thru NPR list, issue ELS PLOGIs */
1723                                         if (vport->fc_npr_cnt)
1724                                                 lpfc_els_disc_plogi(vport);
1725
1726                                         if (!vport->num_disc_nodes) {
1727                                                 spin_lock_irq(shost->host_lock);
1728                                                 vport->fc_flag &=
1729                                                         ~FC_NDISC_ACTIVE;
1730                                                 spin_unlock_irq(
1731                                                         shost->host_lock);
1732                                                 lpfc_can_disctmo(vport);
1733                                         }
1734                                 }
1735                                 vport->port_state = LPFC_VPORT_READY;
1736                         } else {
1737                                 lpfc_rscn_disc(vport);
1738                         }
1739                 }
1740         }
1741 out:
1742         lpfc_els_free_iocb(phba, cmdiocb);
1743         return;
1744 }
1745
1746 /**
1747  * lpfc_issue_els_adisc: Issue an address discover iocb to an node on a vport.
1748  * @vport: pointer to a virtual N_Port data structure.
1749  * @ndlp: pointer to a node-list data structure.
1750  * @retry: number of retries to the command IOCB.
1751  *
1752  * This routine issues an Address Discover (ADISC) for an @ndlp on a
1753  * @vport. It prepares the payload of the ADISC ELS command, updates the
1754  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
1755  * to issue the ADISC ELS command.
1756  *
1757  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1758  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1759  * will be stored into the context1 field of the IOCB for the completion
1760  * callback function to the ADISC ELS command.
1761  *
1762  * Return code
1763  *   0 - successfully issued adisc
1764  *   1 - failed to issue adisc
1765  **/
1766 int
1767 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1768                      uint8_t retry)
1769 {
1770         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1771         struct lpfc_hba  *phba = vport->phba;
1772         ADISC *ap;
1773         IOCB_t *icmd;
1774         struct lpfc_iocbq *elsiocb;
1775         struct lpfc_sli *psli = &phba->sli;
1776         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1777         uint8_t *pcmd;
1778         uint16_t cmdsize;
1779
1780         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
1781         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1782                                      ndlp->nlp_DID, ELS_CMD_ADISC);
1783         if (!elsiocb)
1784                 return 1;
1785
1786         icmd = &elsiocb->iocb;
1787         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1788
1789         /* For ADISC request, remainder of payload is service parameters */
1790         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
1791         pcmd += sizeof(uint32_t);
1792
1793         /* Fill in ADISC payload */
1794         ap = (ADISC *) pcmd;
1795         ap->hardAL_PA = phba->fc_pref_ALPA;
1796         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
1797         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
1798         ap->DID = be32_to_cpu(vport->fc_myDID);
1799
1800         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1801                 "Issue ADISC:     did:x%x",
1802                 ndlp->nlp_DID, 0, 0);
1803
1804         phba->fc_stat.elsXmitADISC++;
1805         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
1806         spin_lock_irq(shost->host_lock);
1807         ndlp->nlp_flag |= NLP_ADISC_SND;
1808         spin_unlock_irq(shost->host_lock);
1809         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1810                 spin_lock_irq(shost->host_lock);
1811                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
1812                 spin_unlock_irq(shost->host_lock);
1813                 lpfc_els_free_iocb(phba, elsiocb);
1814                 return 1;
1815         }
1816         return 0;
1817 }
1818
1819 /**
1820  * lpfc_cmpl_els_logo: Completion callback function for logo.
1821  * @phba: pointer to lpfc hba data structure.
1822  * @cmdiocb: pointer to lpfc command iocb data structure.
1823  * @rspiocb: pointer to lpfc response iocb data structure.
1824  *
1825  * This routine is the completion function for issuing the ELS Logout (LOGO)
1826  * command. If no error status was reported from the LOGO response, the
1827  * state machine of the associated ndlp shall be invoked for transition with
1828  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
1829  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
1830  **/
1831 static void
1832 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1833                    struct lpfc_iocbq *rspiocb)
1834 {
1835         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1836         struct lpfc_vport *vport = ndlp->vport;
1837         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1838         IOCB_t *irsp;
1839         struct lpfc_sli *psli;
1840
1841         psli = &phba->sli;
1842         /* we pass cmdiocb to state machine which needs rspiocb as well */
1843         cmdiocb->context_un.rsp_iocb = rspiocb;
1844
1845         irsp = &(rspiocb->iocb);
1846         spin_lock_irq(shost->host_lock);
1847         ndlp->nlp_flag &= ~NLP_LOGO_SND;
1848         spin_unlock_irq(shost->host_lock);
1849
1850         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1851                 "LOGO cmpl:       status:x%x/x%x did:x%x",
1852                 irsp->ulpStatus, irsp->un.ulpWord[4],
1853                 ndlp->nlp_DID);
1854         /* LOGO completes to NPort <nlp_DID> */
1855         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1856                          "0105 LOGO completes to NPort x%x "
1857                          "Data: x%x x%x x%x x%x\n",
1858                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1859                          irsp->ulpTimeout, vport->num_disc_nodes);
1860         /* Check to see if link went down during discovery */
1861         if (lpfc_els_chk_latt(vport))
1862                 goto out;
1863
1864         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
1865                 /* NLP_EVT_DEVICE_RM should unregister the RPI
1866                  * which should abort all outstanding IOs.
1867                  */
1868                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1869                                         NLP_EVT_DEVICE_RM);
1870                 goto out;
1871         }
1872
1873         if (irsp->ulpStatus) {
1874                 /* Check for retry */
1875                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1876                         /* ELS command is being retried */
1877                         goto out;
1878                 /* LOGO failed */
1879                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1880                 if (lpfc_error_lost_link(irsp))
1881                         goto out;
1882                 else
1883                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1884                                                 NLP_EVT_CMPL_LOGO);
1885         } else
1886                 /* Good status, call state machine.
1887                  * This will unregister the rpi if needed.
1888                  */
1889                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1890                                         NLP_EVT_CMPL_LOGO);
1891 out:
1892         lpfc_els_free_iocb(phba, cmdiocb);
1893         return;
1894 }
1895
1896 /**
1897  * lpfc_issue_els_logo: Issue a logo to an node on a vport.
1898  * @vport: pointer to a virtual N_Port data structure.
1899  * @ndlp: pointer to a node-list data structure.
1900  * @retry: number of retries to the command IOCB.
1901  *
1902  * This routine constructs and issues an ELS Logout (LOGO) iocb command
1903  * to a remote node, referred by an @ndlp on a @vport. It constructs the
1904  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
1905  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
1906  *
1907  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1908  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1909  * will be stored into the context1 field of the IOCB for the completion
1910  * callback function to the LOGO ELS command.
1911  *
1912  * Return code
1913  *   0 - successfully issued logo
1914  *   1 - failed to issue logo
1915  **/
1916 int
1917 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1918                     uint8_t retry)
1919 {
1920         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1921         struct lpfc_hba  *phba = vport->phba;
1922         IOCB_t *icmd;
1923         struct lpfc_iocbq *elsiocb;
1924         struct lpfc_sli_ring *pring;
1925         struct lpfc_sli *psli;
1926         uint8_t *pcmd;
1927         uint16_t cmdsize;
1928         int rc;
1929
1930         psli = &phba->sli;
1931         pring = &psli->ring[LPFC_ELS_RING];
1932
1933         spin_lock_irq(shost->host_lock);
1934         if (ndlp->nlp_flag & NLP_LOGO_SND) {
1935                 spin_unlock_irq(shost->host_lock);
1936                 return 0;
1937         }
1938         spin_unlock_irq(shost->host_lock);
1939
1940         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
1941         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1942                                      ndlp->nlp_DID, ELS_CMD_LOGO);
1943         if (!elsiocb)
1944                 return 1;
1945
1946         icmd = &elsiocb->iocb;
1947         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1948         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
1949         pcmd += sizeof(uint32_t);
1950
1951         /* Fill in LOGO payload */
1952         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
1953         pcmd += sizeof(uint32_t);
1954         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
1955
1956         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1957                 "Issue LOGO:      did:x%x",
1958                 ndlp->nlp_DID, 0, 0);
1959
1960         phba->fc_stat.elsXmitLOGO++;
1961         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
1962         spin_lock_irq(shost->host_lock);
1963         ndlp->nlp_flag |= NLP_LOGO_SND;
1964         spin_unlock_irq(shost->host_lock);
1965         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1966
1967         if (rc == IOCB_ERROR) {
1968                 spin_lock_irq(shost->host_lock);
1969                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
1970                 spin_unlock_irq(shost->host_lock);
1971                 lpfc_els_free_iocb(phba, elsiocb);
1972                 return 1;
1973         }
1974         return 0;
1975 }
1976
1977 /**
1978  * lpfc_cmpl_els_cmd: Completion callback function for generic els command.
1979  * @phba: pointer to lpfc hba data structure.
1980  * @cmdiocb: pointer to lpfc command iocb data structure.
1981  * @rspiocb: pointer to lpfc response iocb data structure.
1982  *
1983  * This routine is a generic completion callback function for ELS commands.
1984  * Specifically, it is the callback function which does not need to perform
1985  * any command specific operations. It is currently used by the ELS command
1986  * issuing routines for the ELS State Change  Request (SCR),
1987  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
1988  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
1989  * certain debug loggings, this callback function simply invokes the
1990  * lpfc_els_chk_latt() routine to check whether link went down during the
1991  * discovery process.
1992  **/
1993 static void
1994 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1995                   struct lpfc_iocbq *rspiocb)
1996 {
1997         struct lpfc_vport *vport = cmdiocb->vport;
1998         IOCB_t *irsp;
1999
2000         irsp = &rspiocb->iocb;
2001
2002         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2003                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
2004                 irsp->ulpStatus, irsp->un.ulpWord[4],
2005                 irsp->un.elsreq64.remoteID);
2006         /* ELS cmd tag <ulpIoTag> completes */
2007         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2008                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2009                          irsp->ulpIoTag, irsp->ulpStatus,
2010                          irsp->un.ulpWord[4], irsp->ulpTimeout);
2011         /* Check to see if link went down during discovery */
2012         lpfc_els_chk_latt(vport);
2013         lpfc_els_free_iocb(phba, cmdiocb);
2014         return;
2015 }
2016
2017 /**
2018  * lpfc_issue_els_scr: Issue a scr to an node on a vport.
2019  * @vport: pointer to a host virtual N_Port data structure.
2020  * @nportid: N_Port identifier to the remote node.
2021  * @retry: number of retries to the command IOCB.
2022  *
2023  * This routine issues a State Change Request (SCR) to a fabric node
2024  * on a @vport. The remote node @nportid is passed into the function. It
2025  * first search the @vport node list to find the matching ndlp. If no such
2026  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2027  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2028  * routine is invoked to send the SCR IOCB.
2029  *
2030  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2031  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2032  * will be stored into the context1 field of the IOCB for the completion
2033  * callback function to the SCR ELS command.
2034  *
2035  * Return code
2036  *   0 - Successfully issued scr command
2037  *   1 - Failed to issue scr command
2038  **/
2039 int
2040 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2041 {
2042         struct lpfc_hba  *phba = vport->phba;
2043         IOCB_t *icmd;
2044         struct lpfc_iocbq *elsiocb;
2045         struct lpfc_sli_ring *pring;
2046         struct lpfc_sli *psli;
2047         uint8_t *pcmd;
2048         uint16_t cmdsize;
2049         struct lpfc_nodelist *ndlp;
2050
2051         psli = &phba->sli;
2052         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2053         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2054
2055         ndlp = lpfc_findnode_did(vport, nportid);
2056         if (!ndlp) {
2057                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2058                 if (!ndlp)
2059                         return 1;
2060                 lpfc_nlp_init(vport, ndlp, nportid);
2061                 lpfc_enqueue_node(vport, ndlp);
2062         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2063                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2064                 if (!ndlp)
2065                         return 1;
2066         }
2067
2068         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2069                                      ndlp->nlp_DID, ELS_CMD_SCR);
2070
2071         if (!elsiocb) {
2072                 /* This will trigger the release of the node just
2073                  * allocated
2074                  */
2075                 lpfc_nlp_put(ndlp);
2076                 return 1;
2077         }
2078
2079         icmd = &elsiocb->iocb;
2080         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2081
2082         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2083         pcmd += sizeof(uint32_t);
2084
2085         /* For SCR, remainder of payload is SCR parameter page */
2086         memset(pcmd, 0, sizeof(SCR));
2087         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2088
2089         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2090                 "Issue SCR:       did:x%x",
2091                 ndlp->nlp_DID, 0, 0);
2092
2093         phba->fc_stat.elsXmitSCR++;
2094         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2095         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
2096                 /* The additional lpfc_nlp_put will cause the following
2097                  * lpfc_els_free_iocb routine to trigger the rlease of
2098                  * the node.
2099                  */
2100                 lpfc_nlp_put(ndlp);
2101                 lpfc_els_free_iocb(phba, elsiocb);
2102                 return 1;
2103         }
2104         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2105          * trigger the release of node.
2106          */
2107         lpfc_nlp_put(ndlp);
2108         return 0;
2109 }
2110
2111 /**
2112  * lpfc_issue_els_farpr: Issue a farp to an node on a vport.
2113  * @vport: pointer to a host virtual N_Port data structure.
2114  * @nportid: N_Port identifier to the remote node.
2115  * @retry: number of retries to the command IOCB.
2116  *
2117  * This routine issues a Fibre Channel Address Resolution Response
2118  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2119  * is passed into the function. It first search the @vport node list to find
2120  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2121  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2122  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2123  *
2124  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2125  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2126  * will be stored into the context1 field of the IOCB for the completion
2127  * callback function to the PARPR ELS command.
2128  *
2129  * Return code
2130  *   0 - Successfully issued farpr command
2131  *   1 - Failed to issue farpr command
2132  **/
2133 static int
2134 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2135 {
2136         struct lpfc_hba  *phba = vport->phba;
2137         IOCB_t *icmd;
2138         struct lpfc_iocbq *elsiocb;
2139         struct lpfc_sli_ring *pring;
2140         struct lpfc_sli *psli;
2141         FARP *fp;
2142         uint8_t *pcmd;
2143         uint32_t *lp;
2144         uint16_t cmdsize;
2145         struct lpfc_nodelist *ondlp;
2146         struct lpfc_nodelist *ndlp;
2147
2148         psli = &phba->sli;
2149         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2150         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
2151
2152         ndlp = lpfc_findnode_did(vport, nportid);
2153         if (!ndlp) {
2154                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2155                 if (!ndlp)
2156                         return 1;
2157                 lpfc_nlp_init(vport, ndlp, nportid);
2158                 lpfc_enqueue_node(vport, ndlp);
2159         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2160                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2161                 if (!ndlp)
2162                         return 1;
2163         }
2164
2165         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2166                                      ndlp->nlp_DID, ELS_CMD_RNID);
2167         if (!elsiocb) {
2168                 /* This will trigger the release of the node just
2169                  * allocated
2170                  */
2171                 lpfc_nlp_put(ndlp);
2172                 return 1;
2173         }
2174
2175         icmd = &elsiocb->iocb;
2176         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2177
2178         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
2179         pcmd += sizeof(uint32_t);
2180
2181         /* Fill in FARPR payload */
2182         fp = (FARP *) (pcmd);
2183         memset(fp, 0, sizeof(FARP));
2184         lp = (uint32_t *) pcmd;
2185         *lp++ = be32_to_cpu(nportid);
2186         *lp++ = be32_to_cpu(vport->fc_myDID);
2187         fp->Rflags = 0;
2188         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2189
2190         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2191         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2192         ondlp = lpfc_findnode_did(vport, nportid);
2193         if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
2194                 memcpy(&fp->OportName, &ondlp->nlp_portname,
2195                        sizeof(struct lpfc_name));
2196                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
2197                        sizeof(struct lpfc_name));
2198         }
2199
2200         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2201                 "Issue FARPR:     did:x%x",
2202                 ndlp->nlp_DID, 0, 0);
2203
2204         phba->fc_stat.elsXmitFARPR++;
2205         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2206         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
2207                 /* The additional lpfc_nlp_put will cause the following
2208                  * lpfc_els_free_iocb routine to trigger the release of
2209                  * the node.
2210                  */
2211                 lpfc_nlp_put(ndlp);
2212                 lpfc_els_free_iocb(phba, elsiocb);
2213                 return 1;
2214         }
2215         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2216          * trigger the release of the node.
2217          */
2218         lpfc_nlp_put(ndlp);
2219         return 0;
2220 }
2221
2222 /**
2223  * lpfc_cancel_retry_delay_tmo: Cancel the timer with delayed iocb-cmd retry.
2224  * @vport: pointer to a host virtual N_Port data structure.
2225  * @nlp: pointer to a node-list data structure.
2226  *
2227  * This routine cancels the timer with a delayed IOCB-command retry for
2228  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2229  * removes the ELS retry event if it presents. In addition, if the
2230  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2231  * commands are sent for the @vport's nodes that require issuing discovery
2232  * ADISC.
2233  **/
2234 void
2235 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
2236 {
2237         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2238         struct lpfc_work_evt *evtp;
2239
2240         if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2241                 return;
2242         spin_lock_irq(shost->host_lock);
2243         nlp->nlp_flag &= ~NLP_DELAY_TMO;
2244         spin_unlock_irq(shost->host_lock);
2245         del_timer_sync(&nlp->nlp_delayfunc);
2246         nlp->nlp_last_elscmd = 0;
2247         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
2248                 list_del_init(&nlp->els_retry_evt.evt_listp);
2249                 /* Decrement nlp reference count held for the delayed retry */
2250                 evtp = &nlp->els_retry_evt;
2251                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2252         }
2253         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2254                 spin_lock_irq(shost->host_lock);
2255                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2256                 spin_unlock_irq(shost->host_lock);
2257                 if (vport->num_disc_nodes) {
2258                         if (vport->port_state < LPFC_VPORT_READY) {
2259                                 /* Check if there are more ADISCs to be sent */
2260                                 lpfc_more_adisc(vport);
2261                                 if ((vport->num_disc_nodes == 0) &&
2262                                     (vport->fc_npr_cnt))
2263                                         lpfc_els_disc_plogi(vport);
2264                         } else {
2265                                 /* Check if there are more PLOGIs to be sent */
2266                                 lpfc_more_plogi(vport);
2267                         }
2268                         if (vport->num_disc_nodes == 0) {
2269                                 spin_lock_irq(shost->host_lock);
2270                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2271                                 spin_unlock_irq(shost->host_lock);
2272                                 lpfc_can_disctmo(vport);
2273                                 lpfc_end_rscn(vport);
2274                         }
2275                 }
2276         }
2277         return;
2278 }
2279
2280 /**
2281  * lpfc_els_retry_delay: Timer function with a ndlp delayed function timer.
2282  * @ptr: holder for the pointer to the timer function associated data (ndlp).
2283  *
2284  * This routine is invoked by the ndlp delayed-function timer to check
2285  * whether there is any pending ELS retry event(s) with the node. If not, it
2286  * simply returns. Otherwise, if there is at least one ELS delayed event, it
2287  * adds the delayed events to the HBA work list and invokes the
2288  * lpfc_worker_wake_up() routine to wake up worker thread to process the
2289  * event. Note that lpfc_nlp_get() is called before posting the event to
2290  * the work list to hold reference count of ndlp so that it guarantees the
2291  * reference to ndlp will still be available when the worker thread gets
2292  * to the event associated with the ndlp.
2293  **/
2294 void
2295 lpfc_els_retry_delay(unsigned long ptr)
2296 {
2297         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2298         struct lpfc_vport *vport = ndlp->vport;
2299         struct lpfc_hba   *phba = vport->phba;
2300         unsigned long flags;
2301         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
2302
2303         spin_lock_irqsave(&phba->hbalock, flags);
2304         if (!list_empty(&evtp->evt_listp)) {
2305                 spin_unlock_irqrestore(&phba->hbalock, flags);
2306                 return;
2307         }
2308
2309         /* We need to hold the node by incrementing the reference
2310          * count until the queued work is done
2311          */
2312         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
2313         if (evtp->evt_arg1) {
2314                 evtp->evt = LPFC_EVT_ELS_RETRY;
2315                 list_add_tail(&evtp->evt_listp, &phba->work_list);
2316                 lpfc_worker_wake_up(phba);
2317         }
2318         spin_unlock_irqrestore(&phba->hbalock, flags);
2319         return;
2320 }
2321
2322 /**
2323  * lpfc_els_retry_delay_handler: Work thread handler for ndlp delayed function.
2324  * @ndlp: pointer to a node-list data structure.
2325  *
2326  * This routine is the worker-thread handler for processing the @ndlp delayed
2327  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2328  * the last ELS command from the associated ndlp and invokes the proper ELS
2329  * function according to the delayed ELS command to retry the command.
2330  **/
2331 void
2332 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
2333 {
2334         struct lpfc_vport *vport = ndlp->vport;
2335         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2336         uint32_t cmd, did, retry;
2337
2338         spin_lock_irq(shost->host_lock);
2339         did = ndlp->nlp_DID;
2340         cmd = ndlp->nlp_last_elscmd;
2341         ndlp->nlp_last_elscmd = 0;
2342
2343         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2344                 spin_unlock_irq(shost->host_lock);
2345                 return;
2346         }
2347
2348         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2349         spin_unlock_irq(shost->host_lock);
2350         /*
2351          * If a discovery event readded nlp_delayfunc after timer
2352          * firing and before processing the timer, cancel the
2353          * nlp_delayfunc.
2354          */
2355         del_timer_sync(&ndlp->nlp_delayfunc);
2356         retry = ndlp->nlp_retry;
2357
2358         switch (cmd) {
2359         case ELS_CMD_FLOGI:
2360                 lpfc_issue_els_flogi(vport, ndlp, retry);
2361                 break;
2362         case ELS_CMD_PLOGI:
2363                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
2364                         ndlp->nlp_prev_state = ndlp->nlp_state;
2365                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2366                 }
2367                 break;
2368         case ELS_CMD_ADISC:
2369                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
2370                         ndlp->nlp_prev_state = ndlp->nlp_state;
2371                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2372                 }
2373                 break;
2374         case ELS_CMD_PRLI:
2375                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
2376                         ndlp->nlp_prev_state = ndlp->nlp_state;
2377                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2378                 }
2379                 break;
2380         case ELS_CMD_LOGO:
2381                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
2382                         ndlp->nlp_prev_state = ndlp->nlp_state;
2383                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2384                 }
2385                 break;
2386         case ELS_CMD_FDISC:
2387                 lpfc_issue_els_fdisc(vport, ndlp, retry);
2388                 break;
2389         }
2390         return;
2391 }
2392
2393 /**
2394  * lpfc_els_retry: Make retry decision on an els command iocb.
2395  * @phba: pointer to lpfc hba data structure.
2396  * @cmdiocb: pointer to lpfc command iocb data structure.
2397  * @rspiocb: pointer to lpfc response iocb data structure.
2398  *
2399  * This routine makes a retry decision on an ELS command IOCB, which has
2400  * failed. The following ELS IOCBs use this function for retrying the command
2401  * when previously issued command responsed with error status: FLOGI, PLOGI,
2402  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
2403  * returned error status, it makes the decision whether a retry shall be
2404  * issued for the command, and whether a retry shall be made immediately or
2405  * delayed. In the former case, the corresponding ELS command issuing-function
2406  * is called to retry the command. In the later case, the ELS command shall
2407  * be posted to the ndlp delayed event and delayed function timer set to the
2408  * ndlp for the delayed command issusing.
2409  *
2410  * Return code
2411  *   0 - No retry of els command is made
2412  *   1 - Immediate or delayed retry of els command is made
2413  **/
2414 static int
2415 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2416                struct lpfc_iocbq *rspiocb)
2417 {
2418         struct lpfc_vport *vport = cmdiocb->vport;
2419         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2420         IOCB_t *irsp = &rspiocb->iocb;
2421         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2422         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2423         uint32_t *elscmd;
2424         struct ls_rjt stat;
2425         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
2426         int logerr = 0;
2427         uint32_t cmd = 0;
2428         uint32_t did;
2429
2430
2431         /* Note: context2 may be 0 for internal driver abort
2432          * of delays ELS command.
2433          */
2434
2435         if (pcmd && pcmd->virt) {
2436                 elscmd = (uint32_t *) (pcmd->virt);
2437                 cmd = *elscmd++;
2438         }
2439
2440         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2441                 did = ndlp->nlp_DID;
2442         else {
2443                 /* We should only hit this case for retrying PLOGI */
2444                 did = irsp->un.elsreq64.remoteID;
2445                 ndlp = lpfc_findnode_did(vport, did);
2446                 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2447                     && (cmd != ELS_CMD_PLOGI))
2448                         return 1;
2449         }
2450
2451         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2452                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
2453                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
2454
2455         switch (irsp->ulpStatus) {
2456         case IOSTAT_FCP_RSP_ERROR:
2457         case IOSTAT_REMOTE_STOP:
2458                 break;
2459
2460         case IOSTAT_LOCAL_REJECT:
2461                 switch ((irsp->un.ulpWord[4] & 0xff)) {
2462                 case IOERR_LOOP_OPEN_FAILURE:
2463                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
2464                                 delay = 1000;
2465                         retry = 1;
2466                         break;
2467
2468                 case IOERR_ILLEGAL_COMMAND:
2469                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2470                                          "0124 Retry illegal cmd x%x "
2471                                          "retry:x%x delay:x%x\n",
2472                                          cmd, cmdiocb->retry, delay);
2473                         retry = 1;
2474                         /* All command's retry policy */
2475                         maxretry = 8;
2476                         if (cmdiocb->retry > 2)
2477                                 delay = 1000;
2478                         break;
2479
2480                 case IOERR_NO_RESOURCES:
2481                         logerr = 1; /* HBA out of resources */
2482                         retry = 1;
2483                         if (cmdiocb->retry > 100)
2484                                 delay = 100;
2485                         maxretry = 250;
2486                         break;
2487
2488                 case IOERR_ILLEGAL_FRAME:
2489                         delay = 100;
2490                         retry = 1;
2491                         break;
2492
2493                 case IOERR_SEQUENCE_TIMEOUT:
2494                 case IOERR_INVALID_RPI:
2495                         retry = 1;
2496                         break;
2497                 }
2498                 break;
2499
2500         case IOSTAT_NPORT_RJT:
2501         case IOSTAT_FABRIC_RJT:
2502                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
2503                         retry = 1;
2504                         break;
2505                 }
2506                 break;
2507
2508         case IOSTAT_NPORT_BSY:
2509         case IOSTAT_FABRIC_BSY:
2510                 logerr = 1; /* Fabric / Remote NPort out of resources */
2511                 retry = 1;
2512                 break;
2513
2514         case IOSTAT_LS_RJT:
2515                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
2516                 /* Added for Vendor specifc support
2517                  * Just keep retrying for these Rsn / Exp codes
2518                  */
2519                 switch (stat.un.b.lsRjtRsnCode) {
2520                 case LSRJT_UNABLE_TPC:
2521                         if (stat.un.b.lsRjtRsnCodeExp ==
2522                             LSEXP_CMD_IN_PROGRESS) {
2523                                 if (cmd == ELS_CMD_PLOGI) {
2524                                         delay = 1000;
2525                                         maxretry = 48;
2526                                 }
2527                                 retry = 1;
2528                                 break;
2529                         }
2530                         if (cmd == ELS_CMD_PLOGI) {
2531                                 delay = 1000;
2532                                 maxretry = lpfc_max_els_tries + 1;
2533                                 retry = 1;
2534                                 break;
2535                         }
2536                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2537                           (cmd == ELS_CMD_FDISC) &&
2538                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
2539                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2540                                                  "0125 FDISC Failed (x%x). "
2541                                                  "Fabric out of resources\n",
2542                                                  stat.un.lsRjtError);
2543                                 lpfc_vport_set_state(vport,
2544                                                      FC_VPORT_NO_FABRIC_RSCS);
2545                         }
2546                         break;
2547
2548                 case LSRJT_LOGICAL_BSY:
2549                         if ((cmd == ELS_CMD_PLOGI) ||
2550                             (cmd == ELS_CMD_PRLI)) {
2551                                 delay = 1000;
2552                                 maxretry = 48;
2553                         } else if (cmd == ELS_CMD_FDISC) {
2554                                 /* FDISC retry policy */
2555                                 maxretry = 48;
2556                                 if (cmdiocb->retry >= 32)
2557                                         delay = 1000;
2558                         }
2559                         retry = 1;
2560                         break;
2561
2562                 case LSRJT_LOGICAL_ERR:
2563                         /* There are some cases where switches return this
2564                          * error when they are not ready and should be returning
2565                          * Logical Busy. We should delay every time.
2566                          */
2567                         if (cmd == ELS_CMD_FDISC &&
2568                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
2569                                 maxretry = 3;
2570                                 delay = 1000;
2571                                 retry = 1;
2572                                 break;
2573                         }
2574                 case LSRJT_PROTOCOL_ERR:
2575                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2576                           (cmd == ELS_CMD_FDISC) &&
2577                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
2578                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
2579                           ) {
2580                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2581                                                  "0123 FDISC Failed (x%x). "
2582                                                  "Fabric Detected Bad WWN\n",
2583                                                  stat.un.lsRjtError);
2584                                 lpfc_vport_set_state(vport,
2585                                                      FC_VPORT_FABRIC_REJ_WWN);
2586                         }
2587                         break;
2588                 }
2589                 break;
2590
2591         case IOSTAT_INTERMED_RSP:
2592         case IOSTAT_BA_RJT:
2593                 break;
2594
2595         default:
2596                 break;
2597         }
2598
2599         if (did == FDMI_DID)
2600                 retry = 1;
2601
2602         if ((cmd == ELS_CMD_FLOGI) &&
2603             (phba->fc_topology != TOPOLOGY_LOOP) &&
2604             !lpfc_error_lost_link(irsp)) {
2605                 /* FLOGI retry policy */
2606                 retry = 1;
2607                 maxretry = 48;
2608                 if (cmdiocb->retry >= 32)
2609                         delay = 1000;
2610         }
2611
2612         if ((++cmdiocb->retry) >= maxretry) {
2613                 phba->fc_stat.elsRetryExceeded++;
2614                 retry = 0;
2615         }
2616
2617         if ((vport->load_flag & FC_UNLOADING) != 0)
2618                 retry = 0;
2619
2620         if (retry) {
2621
2622                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
2623                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2624                                  "0107 Retry ELS command x%x to remote "
2625                                  "NPORT x%x Data: x%x x%x\n",
2626                                  cmd, did, cmdiocb->retry, delay);
2627
2628                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
2629                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
2630                         ((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
2631                         /* Don't reset timer for no resources */
2632
2633                         /* If discovery / RSCN timer is running, reset it */
2634                         if (timer_pending(&vport->fc_disctmo) ||
2635                             (vport->fc_flag & FC_RSCN_MODE))
2636                                 lpfc_set_disctmo(vport);
2637                 }
2638
2639                 phba->fc_stat.elsXmitRetry++;
2640                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
2641                         phba->fc_stat.elsDelayRetry++;
2642                         ndlp->nlp_retry = cmdiocb->retry;
2643
2644                         /* delay is specified in milliseconds */
2645                         mod_timer(&ndlp->nlp_delayfunc,
2646                                 jiffies + msecs_to_jiffies(delay));
2647                         spin_lock_irq(shost->host_lock);
2648                         ndlp->nlp_flag |= NLP_DELAY_TMO;
2649                         spin_unlock_irq(shost->host_lock);
2650
2651                         ndlp->nlp_prev_state = ndlp->nlp_state;
2652                         if (cmd == ELS_CMD_PRLI)
2653                                 lpfc_nlp_set_state(vport, ndlp,
2654                                         NLP_STE_REG_LOGIN_ISSUE);
2655                         else
2656                                 lpfc_nlp_set_state(vport, ndlp,
2657                                         NLP_STE_NPR_NODE);
2658                         ndlp->nlp_last_elscmd = cmd;
2659
2660                         return 1;
2661                 }
2662                 switch (cmd) {
2663                 case ELS_CMD_FLOGI:
2664                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2665                         return 1;
2666                 case ELS_CMD_FDISC:
2667                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
2668                         return 1;
2669                 case ELS_CMD_PLOGI:
2670                         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2671                                 ndlp->nlp_prev_state = ndlp->nlp_state;
2672                                 lpfc_nlp_set_state(vport, ndlp,
2673                                                    NLP_STE_PLOGI_ISSUE);
2674                         }
2675                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2676                         return 1;
2677                 case ELS_CMD_ADISC:
2678                         ndlp->nlp_prev_state = ndlp->nlp_state;
2679                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2680                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2681                         return 1;
2682                 case ELS_CMD_PRLI:
2683                         ndlp->nlp_prev_state = ndlp->nlp_state;
2684                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2685                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2686                         return 1;
2687                 case ELS_CMD_LOGO:
2688                         ndlp->nlp_prev_state = ndlp->nlp_state;
2689                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2690                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2691                         return 1;
2692                 }
2693         }
2694         /* No retry ELS command <elsCmd> to remote NPORT <did> */
2695         if (logerr) {
2696                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2697                          "0137 No retry ELS command x%x to remote "
2698                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
2699                          cmd, did, irsp->ulpStatus,
2700                          irsp->un.ulpWord[4]);
2701         }
2702         else {
2703                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2704                          "0108 No retry ELS command x%x to remote "
2705                          "NPORT x%x Retried:%d Error:x%x/%x\n",
2706                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
2707                          irsp->un.ulpWord[4]);
2708         }
2709         return 0;
2710 }
2711
2712 /**
2713  * lpfc_els_free_data: Free lpfc dma buffer and data structure with an iocb.
2714  * @phba: pointer to lpfc hba data structure.
2715  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
2716  *
2717  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
2718  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
2719  * checks to see whether there is a lpfc DMA buffer associated with the
2720  * response of the command IOCB. If so, it will be released before releasing
2721  * the lpfc DMA buffer associated with the IOCB itself.
2722  *
2723  * Return code
2724  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2725  **/
2726 static int
2727 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
2728 {
2729         struct lpfc_dmabuf *buf_ptr;
2730
2731         /* Free the response before processing the command. */
2732         if (!list_empty(&buf_ptr1->list)) {
2733                 list_remove_head(&buf_ptr1->list, buf_ptr,
2734                                  struct lpfc_dmabuf,
2735                                  list);
2736                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2737                 kfree(buf_ptr);
2738         }
2739         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2740         kfree(buf_ptr1);
2741         return 0;
2742 }
2743
2744 /**
2745  * lpfc_els_free_bpl: Free lpfc dma buffer and data structure with bpl.
2746  * @phba: pointer to lpfc hba data structure.
2747  * @buf_ptr: pointer to the lpfc dma buffer data structure.
2748  *
2749  * This routine releases the lpfc Direct Memory Access (DMA) buffer
2750  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
2751  * pool.
2752  *
2753  * Return code
2754  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2755  **/
2756 static int
2757 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
2758 {
2759         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2760         kfree(buf_ptr);
2761         return 0;
2762 }
2763
2764 /**
2765  * lpfc_els_free_iocb: Free a command iocb and its associated resources.
2766  * @phba: pointer to lpfc hba data structure.
2767  * @elsiocb: pointer to lpfc els command iocb data structure.
2768  *
2769  * This routine frees a command IOCB and its associated resources. The
2770  * command IOCB data structure contains the reference to various associated
2771  * resources, these fields must be set to NULL if the associated reference
2772  * not present:
2773  *   context1 - reference to ndlp
2774  *   context2 - reference to cmd
2775  *   context2->next - reference to rsp
2776  *   context3 - reference to bpl
2777  *
2778  * It first properly decrements the reference count held on ndlp for the
2779  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
2780  * set, it invokes the lpfc_els_free_data() routine to release the Direct
2781  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
2782  * adds the DMA buffer the @phba data structure for the delayed release.
2783  * If reference to the Buffer Pointer List (BPL) is present, the
2784  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
2785  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
2786  * invoked to release the IOCB data structure back to @phba IOCBQ list.
2787  *
2788  * Return code
2789  *   0 - Success (currently, always return 0)
2790  **/
2791 int
2792 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
2793 {
2794         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2795         struct lpfc_nodelist *ndlp;
2796
2797         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
2798         if (ndlp) {
2799                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
2800                         lpfc_nlp_put(ndlp);
2801
2802                         /* If the ndlp is not being used by another discovery
2803                          * thread, free it.
2804                          */
2805                         if (!lpfc_nlp_not_used(ndlp)) {
2806                                 /* If ndlp is being used by another discovery
2807                                  * thread, just clear NLP_DEFER_RM
2808                                  */
2809                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
2810                         }
2811                 }
2812                 else
2813                         lpfc_nlp_put(ndlp);
2814                 elsiocb->context1 = NULL;
2815         }
2816         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
2817         if (elsiocb->context2) {
2818                 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
2819                         /* Firmware could still be in progress of DMAing
2820                          * payload, so don't free data buffer till after
2821                          * a hbeat.
2822                          */
2823                         elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
2824                         buf_ptr = elsiocb->context2;
2825                         elsiocb->context2 = NULL;
2826                         if (buf_ptr) {
2827                                 buf_ptr1 = NULL;
2828                                 spin_lock_irq(&phba->hbalock);
2829                                 if (!list_empty(&buf_ptr->list)) {
2830                                         list_remove_head(&buf_ptr->list,
2831                                                 buf_ptr1, struct lpfc_dmabuf,
2832                                                 list);
2833                                         INIT_LIST_HEAD(&buf_ptr1->list);
2834                                         list_add_tail(&buf_ptr1->list,
2835                                                 &phba->elsbuf);
2836                                         phba->elsbuf_cnt++;
2837                                 }
2838                                 INIT_LIST_HEAD(&buf_ptr->list);
2839                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
2840                                 phba->elsbuf_cnt++;
2841                                 spin_unlock_irq(&phba->hbalock);
2842                         }
2843                 } else {
2844                         buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
2845                         lpfc_els_free_data(phba, buf_ptr1);
2846                 }
2847         }
2848
2849         if (elsiocb->context3) {
2850                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2851                 lpfc_els_free_bpl(phba, buf_ptr);
2852         }
2853         lpfc_sli_release_iocbq(phba, elsiocb);
2854         return 0;
2855 }
2856
2857 /**
2858  * lpfc_cmpl_els_logo_acc: Completion callback function to logo acc response.
2859  * @phba: pointer to lpfc hba data structure.
2860  * @cmdiocb: pointer to lpfc command iocb data structure.
2861  * @rspiocb: pointer to lpfc response iocb data structure.
2862  *
2863  * This routine is the completion callback function to the Logout (LOGO)
2864  * Accept (ACC) Response ELS command. This routine is invoked to indicate
2865  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
2866  * release the ndlp if it has the last reference remaining (reference count
2867  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
2868  * field to NULL to inform the following lpfc_els_free_iocb() routine no
2869  * ndlp reference count needs to be decremented. Otherwise, the ndlp
2870  * reference use-count shall be decremented by the lpfc_els_free_iocb()
2871  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
2872  * IOCB data structure.
2873  **/
2874 static void
2875 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2876                        struct lpfc_iocbq *rspiocb)
2877 {
2878         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2879         struct lpfc_vport *vport = cmdiocb->vport;
2880         IOCB_t *irsp;
2881
2882         irsp = &rspiocb->iocb;
2883         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2884                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
2885                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
2886         /* ACC to LOGO completes to NPort <nlp_DID> */
2887         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2888                          "0109 ACC to LOGO completes to NPort x%x "
2889                          "Data: x%x x%x x%x\n",
2890                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2891                          ndlp->nlp_rpi);
2892
2893         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
2894                 /* NPort Recovery mode or node is just allocated */
2895                 if (!lpfc_nlp_not_used(ndlp)) {
2896                         /* If the ndlp is being used by another discovery
2897                          * thread, just unregister the RPI.
2898                          */
2899                         lpfc_unreg_rpi(vport, ndlp);
2900                 } else {
2901                         /* Indicate the node has already released, should
2902                          * not reference to it from within lpfc_els_free_iocb.
2903                          */
2904                         cmdiocb->context1 = NULL;
2905                 }
2906         }
2907         lpfc_els_free_iocb(phba, cmdiocb);
2908         return;
2909 }
2910
2911 /**
2912  * lpfc_mbx_cmpl_dflt_rpi: Completion callbk func for unreg dflt rpi mbox cmd.
2913  * @phba: pointer to lpfc hba data structure.
2914  * @pmb: pointer to the driver internal queue element for mailbox command.
2915  *
2916  * This routine is the completion callback function for unregister default
2917  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
2918  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
2919  * decrements the ndlp reference count held for this completion callback
2920  * function. After that, it invokes the lpfc_nlp_not_used() to check
2921  * whether there is only one reference left on the ndlp. If so, it will
2922  * perform one more decrement and trigger the release of the ndlp.
2923  **/
2924 void
2925 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2926 {
2927         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2928         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2929
2930         pmb->context1 = NULL;
2931         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2932         kfree(mp);
2933         mempool_free(pmb, phba->mbox_mem_pool);
2934         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2935                 lpfc_nlp_put(ndlp);
2936                 /* This is the end of the default RPI cleanup logic for this
2937                  * ndlp. If no other discovery threads are using this ndlp.
2938                  * we should free all resources associated with it.
2939                  */
2940                 lpfc_nlp_not_used(ndlp);
2941         }
2942         return;
2943 }
2944
2945 /**
2946  * lpfc_cmpl_els_rsp: Completion callback function for els response iocb cmd.
2947  * @phba: pointer to lpfc hba data structure.
2948  * @cmdiocb: pointer to lpfc command iocb data structure.
2949  * @rspiocb: pointer to lpfc response iocb data structure.
2950  *
2951  * This routine is the completion callback function for ELS Response IOCB
2952  * command. In normal case, this callback function just properly sets the
2953  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
2954  * field in the command IOCB is not NULL, the referred mailbox command will
2955  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
2956  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
2957  * link down event occurred during the discovery, the lpfc_nlp_not_used()
2958  * routine shall be invoked trying to release the ndlp if no other threads
2959  * are currently referring it.
2960  **/
2961 static void
2962 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2963                   struct lpfc_iocbq *rspiocb)
2964 {
2965         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2966         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
2967         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
2968         IOCB_t  *irsp;
2969         uint8_t *pcmd;
2970         LPFC_MBOXQ_t *mbox = NULL;
2971         struct lpfc_dmabuf *mp = NULL;
2972         uint32_t ls_rjt = 0;
2973
2974         irsp = &rspiocb->iocb;
2975
2976         if (cmdiocb->context_un.mbox)
2977                 mbox = cmdiocb->context_un.mbox;
2978
2979         /* First determine if this is a LS_RJT cmpl. Note, this callback
2980          * function can have cmdiocb->contest1 (ndlp) field set to NULL.
2981          */
2982         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
2983         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
2984             (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
2985                 /* A LS_RJT associated with Default RPI cleanup has its own
2986                  * seperate code path.
2987                  */
2988                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
2989                         ls_rjt = 1;
2990         }
2991
2992         /* Check to see if link went down during discovery */
2993         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
2994                 if (mbox) {
2995                         mp = (struct lpfc_dmabuf *) mbox->context1;
2996                         if (mp) {
2997                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2998                                 kfree(mp);
2999                         }
3000                         mempool_free(mbox, phba->mbox_mem_pool);
3001                 }
3002                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3003                     (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3004                         if (lpfc_nlp_not_used(ndlp)) {
3005                                 ndlp = NULL;
3006                                 /* Indicate the node has already released,
3007                                  * should not reference to it from within
3008                                  * the routine lpfc_els_free_iocb.
3009                                  */
3010                                 cmdiocb->context1 = NULL;
3011                         }
3012                 goto out;
3013         }
3014
3015         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3016                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
3017                 irsp->ulpStatus, irsp->un.ulpWord[4],
3018                 cmdiocb->iocb.un.elsreq64.remoteID);
3019         /* ELS response tag <ulpIoTag> completes */
3020         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3021                          "0110 ELS response tag x%x completes "
3022                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3023                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3024                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3025                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3026                          ndlp->nlp_rpi);
3027         if (mbox) {
3028                 if ((rspiocb->iocb.ulpStatus == 0)
3029                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
3030                         lpfc_unreg_rpi(vport, ndlp);
3031                         /* Increment reference count to ndlp to hold the
3032                          * reference to ndlp for the callback function.
3033                          */
3034                         mbox->context2 = lpfc_nlp_get(ndlp);
3035                         mbox->vport = vport;
3036                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3037                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3038                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3039                         }
3040                         else {
3041                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3042                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3043                                 lpfc_nlp_set_state(vport, ndlp,
3044                                            NLP_STE_REG_LOGIN_ISSUE);
3045                         }
3046                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
3047                             != MBX_NOT_FINISHED)
3048                                 goto out;
3049                         else
3050                                 /* Decrement the ndlp reference count we
3051                                  * set for this failed mailbox command.
3052                                  */
3053                                 lpfc_nlp_put(ndlp);
3054
3055                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
3056                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3057                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
3058                                 "Data: x%x x%x x%x\n",
3059                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3060                                 ndlp->nlp_rpi);
3061
3062                         if (lpfc_nlp_not_used(ndlp)) {
3063                                 ndlp = NULL;
3064                                 /* Indicate node has already been released,
3065                                  * should not reference to it from within
3066                                  * the routine lpfc_els_free_iocb.
3067                                  */
3068                                 cmdiocb->context1 = NULL;
3069                         }
3070                 } else {
3071                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
3072                         if (!lpfc_error_lost_link(irsp) &&
3073                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
3074                                 if (lpfc_nlp_not_used(ndlp)) {
3075                                         ndlp = NULL;
3076                                         /* Indicate node has already been
3077                                          * released, should not reference
3078                                          * to it from within the routine
3079                                          * lpfc_els_free_iocb.
3080                                          */
3081                                         cmdiocb->context1 = NULL;
3082                                 }
3083                         }
3084                 }
3085                 mp = (struct lpfc_dmabuf *) mbox->context1;
3086                 if (mp) {
3087                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3088                         kfree(mp);
3089                 }
3090                 mempool_free(mbox, phba->mbox_mem_pool);
3091         }
3092 out:
3093         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3094                 spin_lock_irq(shost->host_lock);
3095                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
3096                 spin_unlock_irq(shost->host_lock);
3097
3098                 /* If the node is not being used by another discovery thread,
3099                  * and we are sending a reject, we are done with it.
3100                  * Release driver reference count here and free associated
3101                  * resources.
3102                  */
3103                 if (ls_rjt)
3104                         if (lpfc_nlp_not_used(ndlp))
3105                                 /* Indicate node has already been released,
3106                                  * should not reference to it from within
3107                                  * the routine lpfc_els_free_iocb.
3108                                  */
3109                                 cmdiocb->context1 = NULL;
3110         }
3111
3112         lpfc_els_free_iocb(phba, cmdiocb);
3113         return;
3114 }
3115
3116 /**
3117  * lpfc_els_rsp_acc: Prepare and issue an acc response iocb command.
3118  * @vport: pointer to a host virtual N_Port data structure.
3119  * @flag: the els command code to be accepted.
3120  * @oldiocb: pointer to the original lpfc command iocb data structure.
3121  * @ndlp: pointer to a node-list data structure.
3122  * @mbox: pointer to the driver internal queue element for mailbox command.
3123  *
3124  * This routine prepares and issues an Accept (ACC) response IOCB
3125  * command. It uses the @flag to properly set up the IOCB field for the
3126  * specific ACC response command to be issued and invokes the
3127  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3128  * @mbox pointer is passed in, it will be put into the context_un.mbox
3129  * field of the IOCB for the completion callback function to issue the
3130  * mailbox command to the HBA later when callback is invoked.
3131  *
3132  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3133  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3134  * will be stored into the context1 field of the IOCB for the completion
3135  * callback function to the corresponding response ELS IOCB command.
3136  *
3137  * Return code
3138  *   0 - Successfully issued acc response
3139  *   1 - Failed to issue acc response
3140  **/
3141 int
3142 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3143                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3144                  LPFC_MBOXQ_t *mbox)
3145 {
3146         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3147         struct lpfc_hba  *phba = vport->phba;
3148         IOCB_t *icmd;
3149         IOCB_t *oldcmd;
3150         struct lpfc_iocbq *elsiocb;
3151         struct lpfc_sli_ring *pring;
3152         struct lpfc_sli *psli;
3153         uint8_t *pcmd;
3154         uint16_t cmdsize;
3155         int rc;
3156         ELS_PKT *els_pkt_ptr;
3157
3158         psli = &phba->sli;
3159         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
3160         oldcmd = &oldiocb->iocb;
3161
3162         switch (flag) {
3163         case ELS_CMD_ACC:
3164                 cmdsize = sizeof(uint32_t);
3165                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3166                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3167                 if (!elsiocb) {
3168                         spin_lock_irq(shost->host_lock);
3169                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3170                         spin_unlock_irq(shost->host_lock);
3171                         return 1;
3172                 }
3173
3174                 icmd = &elsiocb->iocb;
3175                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3176                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3177                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3178                 pcmd += sizeof(uint32_t);
3179
3180                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3181                         "Issue ACC:       did:x%x flg:x%x",
3182                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3183                 break;
3184         case ELS_CMD_PLOGI:
3185                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
3186                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3187                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3188                 if (!elsiocb)
3189                         return 1;
3190
3191                 icmd = &elsiocb->iocb;
3192                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3193                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3194
3195                 if (mbox)
3196                         elsiocb->context_un.mbox = mbox;
3197
3198                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3199                 pcmd += sizeof(uint32_t);
3200                 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
3201
3202                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3203                         "Issue ACC PLOGI: did:x%x flg:x%x",
3204                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3205                 break;
3206         case ELS_CMD_PRLO:
3207                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
3208                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3209                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3210                 if (!elsiocb)
3211                         return 1;
3212
3213                 icmd = &elsiocb->iocb;
3214                 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
3215                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3216
3217                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
3218                        sizeof(uint32_t) + sizeof(PRLO));
3219                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3220                 els_pkt_ptr = (ELS_PKT *) pcmd;
3221                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
3222
3223                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3224                         "Issue ACC PRLO:  did:x%x flg:x%x",
3225                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3226                 break;
3227         default:
3228                 return 1;
3229         }
3230         /* Xmit ELS ACC response tag <ulpIoTag> */
3231         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3232                          "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
3233                          "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
3234                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3235                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3236                          ndlp->nlp_rpi);
3237         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
3238                 spin_lock_irq(shost->host_lock);
3239                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3240                 spin_unlock_irq(shost->host_lock);
3241                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
3242         } else {
3243                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3244         }
3245
3246         phba->fc_stat.elsXmitACC++;
3247         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3248         if (rc == IOCB_ERROR) {
3249                 lpfc_els_free_iocb(phba, elsiocb);
3250                 return 1;
3251         }
3252         return 0;
3253 }
3254
3255 /**
3256  * lpfc_els_rsp_reject: Propare and issue a rjt response iocb command.
3257  * @vport: pointer to a virtual N_Port data structure.
3258  * @rejectError:
3259  * @oldiocb: pointer to the original lpfc command iocb data structure.
3260  * @ndlp: pointer to a node-list data structure.
3261  * @mbox: pointer to the driver internal queue element for mailbox command.
3262  *
3263  * This routine prepares and issue an Reject (RJT) response IOCB
3264  * command. If a @mbox pointer is passed in, it will be put into the
3265  * context_un.mbox field of the IOCB for the completion callback function
3266  * to issue to the HBA later.
3267  *
3268  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3269  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3270  * will be stored into the context1 field of the IOCB for the completion
3271  * callback function to the reject response ELS IOCB command.
3272  *
3273  * Return code
3274  *   0 - Successfully issued reject response
3275  *   1 - Failed to issue reject response
3276  **/
3277 int
3278 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
3279                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3280                     LPFC_MBOXQ_t *mbox)
3281 {
3282         struct lpfc_hba  *phba = vport->phba;
3283         IOCB_t *icmd;
3284         IOCB_t *oldcmd;
3285         struct lpfc_iocbq *elsiocb;
3286         struct lpfc_sli_ring *pring;
3287         struct lpfc_sli *psli;
3288         uint8_t *pcmd;
3289         uint16_t cmdsize;
3290         int rc;
3291
3292         psli = &phba->sli;
3293         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
3294
3295         cmdsize = 2 * sizeof(uint32_t);
3296         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3297                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
3298         if (!elsiocb)
3299                 return 1;
3300
3301         icmd = &elsiocb->iocb;
3302         oldcmd = &oldiocb->iocb;
3303         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3304         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3305
3306         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
3307         pcmd += sizeof(uint32_t);
3308         *((uint32_t *) (pcmd)) = rejectError;
3309
3310         if (mbox)
3311                 elsiocb->context_un.mbox = mbox;
3312
3313         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
3314         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3315                          "0129 Xmit ELS RJT x%x response tag x%x "
3316                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3317                          "rpi x%x\n",
3318                          rejectError, elsiocb->iotag,
3319                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
3320                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
3321         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3322                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
3323                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
3324
3325         phba->fc_stat.elsXmitLSRJT++;
3326         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3327         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3328
3329         if (rc == IOCB_ERROR) {
3330                 lpfc_els_free_iocb(phba, elsiocb);
3331                 return 1;
3332         }
3333         return 0;
3334 }
3335
3336 /**
3337  * lpfc_els_rsp_adisc_acc: Prepare and issue acc response to adisc iocb cmd.
3338  * @vport: pointer to a virtual N_Port data structure.
3339  * @oldiocb: pointer to the original lpfc command iocb data structure.
3340  * @ndlp: pointer to a node-list data structure.
3341  *
3342  * This routine prepares and issues an Accept (ACC) response to Address
3343  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
3344  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3345  *
3346  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3347  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3348  * will be stored into the context1 field of the IOCB for the completion
3349  * callback function to the ADISC Accept response ELS IOCB command.
3350  *
3351  * Return code
3352  *   0 - Successfully issued acc adisc response
3353  *   1 - Failed to issue adisc acc response
3354  **/
3355 int
3356 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3357                        struct lpfc_nodelist *ndlp)
3358 {
3359         struct lpfc_hba  *phba = vport->phba;
3360         struct lpfc_sli  *psli = &phba->sli;
3361         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3362         ADISC *ap;
3363         IOCB_t *icmd, *oldcmd;
3364         struct lpfc_iocbq *elsiocb;
3365         uint8_t *pcmd;
3366         uint16_t cmdsize;
3367         int rc;
3368
3369         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
3370         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3371                                      ndlp->nlp_DID, ELS_CMD_ACC);
3372         if (!elsiocb)
3373                 return 1;
3374
3375         icmd = &elsiocb->iocb;
3376         oldcmd = &oldiocb->iocb;
3377         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3378
3379         /* Xmit ADISC ACC response tag <ulpIoTag> */
3380         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3381                          "0130 Xmit ADISC ACC response iotag x%x xri: "
3382                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
3383                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3384                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3385                          ndlp->nlp_rpi);
3386         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3387
3388         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3389         pcmd += sizeof(uint32_t);
3390
3391         ap = (ADISC *) (pcmd);
3392         ap->hardAL_PA = phba->fc_pref_ALPA;
3393         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3394         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3395         ap->DID = be32_to_cpu(vport->fc_myDID);
3396
3397         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3398                 "Issue ACC ADISC: did:x%x flg:x%x",
3399                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3400
3401         phba->fc_stat.elsXmitACC++;
3402         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3403         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3404         if (rc == IOCB_ERROR) {
3405                 lpfc_els_free_iocb(phba, elsiocb);
3406                 return 1;
3407         }
3408         return 0;
3409 }
3410
3411 /**
3412  * lpfc_els_rsp_prli_acc: Prepare and issue acc response to prli iocb cmd.
3413  * @vport: pointer to a virtual N_Port data structure.
3414  * @oldiocb: pointer to the original lpfc command iocb data structure.
3415  * @ndlp: pointer to a node-list data structure.
3416  *
3417  * This routine prepares and issues an Accept (ACC) response to Process
3418  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
3419  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3420  *
3421  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3422  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3423  * will be stored into the context1 field of the IOCB for the completion
3424  * callback function to the PRLI Accept response ELS IOCB command.
3425  *
3426  * Return code
3427  *   0 - Successfully issued acc prli response
3428  *   1 - Failed to issue acc prli response
3429  **/
3430 int
3431 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3432                       struct lpfc_nodelist *ndlp)
3433 {
3434         struct lpfc_hba  *phba = vport->phba;
3435         PRLI *npr;
3436         lpfc_vpd_t *vpd;
3437         IOCB_t *icmd;
3438         IOCB_t *oldcmd;
3439         struct lpfc_iocbq *elsiocb;
3440         struct lpfc_sli_ring *pring;
3441         struct lpfc_sli *psli;
3442         uint8_t *pcmd;
3443         uint16_t cmdsize;
3444         int rc;
3445
3446         psli = &phba->sli;
3447         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
3448
3449         cmdsize = sizeof(uint32_t) + sizeof(PRLI);
3450         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3451                 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
3452         if (!elsiocb)
3453                 return 1;
3454
3455         icmd = &elsiocb->iocb;
3456         oldcmd = &oldiocb->iocb;
3457         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3458         /* Xmit PRLI ACC response tag <ulpIoTag> */
3459         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3460                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
3461                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3462                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3463                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3464                          ndlp->nlp_rpi);
3465         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3466
3467         *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
3468         pcmd += sizeof(uint32_t);
3469
3470         /* For PRLI, remainder of payload is PRLI parameter page */
3471         memset(pcmd, 0, sizeof(PRLI));
3472
3473         npr = (PRLI *) pcmd;
3474         vpd = &phba->vpd;
3475         /*
3476          * If the remote port is a target and our firmware version is 3.20 or
3477          * later, set the following bits for FC-TAPE support.
3478          */
3479         if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3480             (vpd->rev.feaLevelHigh >= 0x02)) {
3481                 npr->ConfmComplAllowed = 1;
3482                 npr->Retry = 1;
3483                 npr->TaskRetryIdReq = 1;
3484         }
3485
3486         npr->acceptRspCode = PRLI_REQ_EXECUTED;
3487         npr->estabImagePair = 1;
3488         npr->readXferRdyDis = 1;
3489         npr->ConfmComplAllowed = 1;
3490
3491         npr->prliType = PRLI_FCP_TYPE;
3492         npr->initiatorFunc = 1;
3493
3494         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3495                 "Issue ACC PRLI:  did:x%x flg:x%x",
3496                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3497
3498         phba->fc_stat.elsXmitACC++;
3499         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3500
3501         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3502         if (rc == IOCB_ERROR) {
3503                 lpfc_els_free_iocb(phba, elsiocb);
3504                 return 1;
3505         }
3506         return 0;
3507 }
3508
3509 /**
3510  * lpfc_els_rsp_rnid_acc: Issue rnid acc response iocb command.
3511  * @vport: pointer to a virtual N_Port data structure.
3512  * @format: rnid command format.
3513  * @oldiocb: pointer to the original lpfc command iocb data structure.
3514  * @ndlp: pointer to a node-list data structure.
3515  *
3516  * This routine issues a Request Node Identification Data (RNID) Accept
3517  * (ACC) response. It constructs the RNID ACC response command according to
3518  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
3519  * issue the response. Note that this command does not need to hold the ndlp
3520  * reference count for the callback. So, the ndlp reference count taken by
3521  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
3522  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
3523  * there is no ndlp reference available.
3524  *
3525  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3526  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3527  * will be stored into the context1 field of the IOCB for the completion
3528  * callback function. However, for the RNID Accept Response ELS command,
3529  * this is undone later by this routine after the IOCB is allocated.
3530  *
3531  * Return code
3532  *   0 - Successfully issued acc rnid response
3533  *   1 - Failed to issue acc rnid response
3534  **/
3535 static int
3536 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
3537                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3538 {
3539         struct lpfc_hba  *phba = vport->phba;
3540         RNID *rn;
3541         IOCB_t *icmd, *oldcmd;
3542         struct lpfc_iocbq *elsiocb;
3543         struct lpfc_sli_ring *pring;
3544         struct lpfc_sli *psli;
3545         uint8_t *pcmd;
3546         uint16_t cmdsize;
3547         int rc;
3548
3549         psli = &phba->sli;
3550         pring = &psli->ring[LPFC_ELS_RING];
3551
3552         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
3553                                         + (2 * sizeof(struct lpfc_name));
3554         if (format)
3555                 cmdsize += sizeof(RNID_TOP_DISC);
3556
3557         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3558                                      ndlp->nlp_DID, ELS_CMD_ACC);
3559         if (!elsiocb)
3560                 return 1;
3561
3562         icmd = &elsiocb->iocb;
3563         oldcmd = &oldiocb->iocb;
3564         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3565         /* Xmit RNID ACC response tag <ulpIoTag> */
3566         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3567                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
3568                          elsiocb->iotag, elsiocb->iocb.ulpContext);
3569         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3570         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3571         pcmd += sizeof(uint32_t);
3572
3573         memset(pcmd, 0, sizeof(RNID));
3574         rn = (RNID *) (pcmd);
3575         rn->Format = format;
3576         rn->CommonLen = (2 * sizeof(struct lpfc_name));
3577         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3578         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3579         switch (format) {
3580         case 0:
3581                 rn->SpecificLen = 0;
3582                 break;
3583         case RNID_TOPOLOGY_DISC:
3584                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
3585                 memcpy(&rn->un.topologyDisc.portName,
3586                        &vport->fc_portname, sizeof(struct lpfc_name));
3587                 rn->un.topologyDisc.unitType = RNID_HBA;
3588                 rn->un.topologyDisc.physPort = 0;
3589                 rn->un.topologyDisc.attachedNodes = 0;
3590                 break;
3591         default:
3592                 rn->CommonLen = 0;
3593                 rn->SpecificLen = 0;
3594                 break;
3595         }
3596
3597         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3598                 "Issue ACC RNID:  did:x%x flg:x%x",
3599                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3600
3601         phba->fc_stat.elsXmitACC++;
3602         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3603         lpfc_nlp_put(ndlp);
3604         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
3605                                     * it could be freed */
3606
3607         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3608         if (rc == IOCB_ERROR) {
3609                 lpfc_els_free_iocb(phba, elsiocb);
3610                 return 1;
3611         }
3612         return 0;
3613 }
3614
3615 /**
3616  * lpfc_els_disc_adisc: Issue remaining adisc iocbs to npr nodes of a vport.
3617  * @vport: pointer to a host virtual N_Port data structure.
3618  *
3619  * This routine issues Address Discover (ADISC) ELS commands to those
3620  * N_Ports which are in node port recovery state and ADISC has not been issued
3621  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
3622  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
3623  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
3624  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
3625  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
3626  * IOCBs quit for later pick up. On the other hand, after walking through
3627  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
3628  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
3629  * no more ADISC need to be sent.
3630  *
3631  * Return code
3632  *    The number of N_Ports with adisc issued.
3633  **/
3634 int
3635 lpfc_els_disc_adisc(struct lpfc_vport *vport)
3636 {
3637         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3638         struct lpfc_nodelist *ndlp, *next_ndlp;
3639         int sentadisc = 0;
3640
3641         /* go thru NPR nodes and issue any remaining ELS ADISCs */
3642         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3643                 if (!NLP_CHK_NODE_ACT(ndlp))
3644                         continue;
3645                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3646                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3647                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
3648                         spin_lock_irq(shost->host_lock);
3649                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3650                         spin_unlock_irq(shost->host_lock);
3651                         ndlp->nlp_prev_state = ndlp->nlp_state;
3652                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3653                         lpfc_issue_els_adisc(vport, ndlp, 0);
3654                         sentadisc++;
3655                         vport->num_disc_nodes++;
3656                         if (vport->num_disc_nodes >=
3657                             vport->cfg_discovery_threads) {
3658                                 spin_lock_irq(shost->host_lock);
3659                                 vport->fc_flag |= FC_NLP_MORE;
3660                                 spin_unlock_irq(shost->host_lock);
3661                                 break;
3662                         }
3663                 }
3664         }
3665         if (sentadisc == 0) {
3666                 spin_lock_irq(shost->host_lock);
3667                 vport->fc_flag &= ~FC_NLP_MORE;
3668                 spin_unlock_irq(shost->host_lock);
3669         }
3670         return sentadisc;
3671 }
3672
3673 /**
3674  * lpfc_els_disc_plogi: Issue plogi for all npr nodes of a vport before adisc.
3675  * @vport: pointer to a host virtual N_Port data structure.
3676  *
3677  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
3678  * which are in node port recovery state, with a @vport. Each time an ELS
3679  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
3680  * the per @vport number of discover count (num_disc_nodes) shall be
3681  * incremented. If the num_disc_nodes reaches a pre-configured threshold
3682  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
3683  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
3684  * later pick up. On the other hand, after walking through all the ndlps with
3685  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
3686  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
3687  * PLOGI need to be sent.
3688  *
3689  * Return code
3690  *   The number of N_Ports with plogi issued.
3691  **/
3692 int
3693 lpfc_els_disc_plogi(struct lpfc_vport *vport)
3694 {
3695         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3696         struct lpfc_nodelist *ndlp, *next_ndlp;
3697         int sentplogi = 0;
3698
3699         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
3700         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3701                 if (!NLP_CHK_NODE_ACT(ndlp))
3702                         continue;
3703                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3704                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3705                     (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
3706                     (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
3707                         ndlp->nlp_prev_state = ndlp->nlp_state;
3708                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3709                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
3710                         sentplogi++;
3711                         vport->num_disc_nodes++;
3712                         if (vport->num_disc_nodes >=
3713                             vport->cfg_discovery_threads) {
3714                                 spin_lock_irq(shost->host_lock);
3715                                 vport->fc_flag |= FC_NLP_MORE;
3716                                 spin_unlock_irq(shost->host_lock);
3717                                 break;
3718                         }
3719                 }
3720         }
3721         if (sentplogi) {
3722                 lpfc_set_disctmo(vport);
3723         }
3724         else {
3725                 spin_lock_irq(shost->host_lock);
3726                 vport->fc_flag &= ~FC_NLP_MORE;
3727                 spin_unlock_irq(shost->host_lock);
3728         }
3729         return sentplogi;
3730 }
3731
3732 /**
3733  * lpfc_els_flush_rscn: Clean up any rscn activities with a vport.
3734  * @vport: pointer to a host virtual N_Port data structure.
3735  *
3736  * This routine cleans up any Registration State Change Notification
3737  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
3738  * @vport together with the host_lock is used to prevent multiple thread
3739  * trying to access the RSCN array on a same @vport at the same time.
3740  **/
3741 void
3742 lpfc_els_flush_rscn(struct lpfc_vport *vport)
3743 {
3744         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3745         struct lpfc_hba  *phba = vport->phba;
3746         int i;
3747
3748         spin_lock_irq(shost->host_lock);
3749         if (vport->fc_rscn_flush) {
3750                 /* Another thread is walking fc_rscn_id_list on this vport */
3751                 spin_unlock_irq(shost->host_lock);
3752                 return;
3753         }
3754         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
3755         vport->fc_rscn_flush = 1;
3756         spin_unlock_irq(shost->host_lock);
3757
3758         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
3759                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
3760                 vport->fc_rscn_id_list[i] = NULL;
3761         }
3762         spin_lock_irq(shost->host_lock);
3763         vport->fc_rscn_id_cnt = 0;
3764         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
3765         spin_unlock_irq(shost->host_lock);
3766         lpfc_can_disctmo(vport);
3767         /* Indicate we are done walking this fc_rscn_id_list */
3768         vport->fc_rscn_flush = 0;
3769 }
3770
3771 /**
3772  * lpfc_rscn_payload_check: Check whether there is a pending rscn to a did.
3773  * @vport: pointer to a host virtual N_Port data structure.
3774  * @did: remote destination port identifier.
3775  *
3776  * This routine checks whether there is any pending Registration State
3777  * Configuration Notification (RSCN) to a @did on @vport.
3778  *
3779  * Return code
3780  *   None zero - The @did matched with a pending rscn
3781  *   0 - not able to match @did with a pending rscn
3782  **/
3783 int
3784 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
3785 {
3786         D_ID ns_did;
3787         D_ID rscn_did;
3788         uint32_t *lp;
3789         uint32_t payload_len, i;
3790         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3791
3792         ns_did.un.word = did;
3793
3794         /* Never match fabric nodes for RSCNs */
3795         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
3796                 return 0;
3797
3798         /* If we are doing a FULL RSCN rediscovery, match everything */
3799         if (vport->fc_flag & FC_RSCN_DISCOVERY)
3800                 return did;
3801
3802         spin_lock_irq(shost->host_lock);
3803         if (vport->fc_rscn_flush) {
3804                 /* Another thread is walking fc_rscn_id_list on this vport */
3805                 spin_unlock_irq(shost->host_lock);
3806                 return 0;
3807         }
3808         /* Indicate we are walking fc_rscn_id_list on this vport */
3809         vport->fc_rscn_flush = 1;
3810         spin_unlock_irq(shost->host_lock);
3811         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
3812                 lp = vport->fc_rscn_id_list[i]->virt;
3813                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
3814                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
3815                 while (payload_len) {
3816                         rscn_did.un.word = be32_to_cpu(*lp++);
3817                         payload_len -= sizeof(uint32_t);
3818                         switch (rscn_did.un.b.resv) {
3819                         case 0: /* Single N_Port ID effected */
3820                                 if (ns_did.un.word == rscn_did.un.word)
3821                                         goto return_did_out;
3822                                 break;
3823                         case 1: /* Whole N_Port Area effected */
3824                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
3825                                     && (ns_did.un.b.area == rscn_did.un.b.area))
3826                                         goto return_did_out;
3827                                 break;
3828                         case 2: /* Whole N_Port Domain effected */
3829                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
3830                                         goto return_did_out;
3831                                 break;
3832                         default:
3833                                 /* Unknown Identifier in RSCN node */
3834                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3835                                                  "0217 Unknown Identifier in "
3836                                                  "RSCN payload Data: x%x\n",
3837                                                  rscn_did.un.word);
3838                         case 3: /* Whole Fabric effected */
3839                                 goto return_did_out;
3840                         }
3841                 }
3842         }
3843         /* Indicate we are done with walking fc_rscn_id_list on this vport */
3844         vport->fc_rscn_flush = 0;
3845         return 0;
3846 return_did_out:
3847         /* Indicate we are done with walking fc_rscn_id_list on this vport */
3848         vport->fc_rscn_flush = 0;
3849         return did;
3850 }
3851
3852 /**
3853  * lpfc_rscn_recovery_check: Send recovery event to vport nodes matching rscn
3854  * @vport: pointer to a host virtual N_Port data structure.
3855  *
3856  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
3857  * state machine for a @vport's nodes that are with pending RSCN (Registration
3858  * State Change Notification).
3859  *
3860  * Return code
3861  *   0 - Successful (currently alway return 0)
3862  **/
3863 static int
3864 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
3865 {
3866         struct lpfc_nodelist *ndlp = NULL;
3867
3868         /* Move all affected nodes by pending RSCNs to NPR state. */
3869         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3870                 if (!NLP_CHK_NODE_ACT(ndlp) ||
3871                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
3872                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
3873                         continue;
3874                 lpfc_disc_state_machine(vport, ndlp, NULL,
3875                                         NLP_EVT_DEVICE_RECOVERY);
3876                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
3877         }
3878         return 0;
3879 }
3880
3881 /**
3882  * lpfc_els_rcv_rscn: Process an unsolicited rscn iocb.
3883  * @vport: pointer to a host virtual N_Port data structure.
3884  * @cmdiocb: pointer to lpfc command iocb data structure.
3885  * @ndlp: pointer to a node-list data structure.
3886  *
3887  * This routine processes an unsolicited RSCN (Registration State Change
3888  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
3889  * to invoke fc_host_post_event() routine to the FC transport layer. If the
3890  * discover state machine is about to begin discovery, it just accepts the
3891  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
3892  * contains N_Port IDs for other vports on this HBA, it just accepts the
3893  * RSCN and ignore processing it. If the state machine is in the recovery
3894  * state, the fc_rscn_id_list of this @vport is walked and the
3895  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
3896  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
3897  * routine is invoked to handle the RSCN event.
3898  *
3899  * Return code
3900  *   0 - Just sent the acc response
3901  *   1 - Sent the acc response and waited for name server completion
3902  **/
3903 static int
3904 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3905                   struct lpfc_nodelist *ndlp)
3906 {
3907         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3908         struct lpfc_hba  *phba = vport->phba;
3909         struct lpfc_dmabuf *pcmd;
3910         uint32_t *lp, *datap;
3911         IOCB_t *icmd;
3912         uint32_t payload_len, length, nportid, *cmd;
3913         int rscn_cnt;
3914         int rscn_id = 0, hba_id = 0;
3915         int i;
3916
3917         icmd = &cmdiocb->iocb;
3918         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3919         lp = (uint32_t *) pcmd->virt;
3920
3921         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
3922         payload_len -= sizeof(uint32_t);        /* take off word 0 */
3923         /* RSCN received */
3924         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3925                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
3926                          vport->fc_flag, payload_len, *lp,
3927                          vport->fc_rscn_id_cnt);
3928         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
3929                 fc_host_post_event(shost, fc_get_event_number(),
3930                         FCH_EVT_RSCN, lp[i]);
3931
3932         /* If we are about to begin discovery, just ACC the RSCN.
3933          * Discovery processing will satisfy it.
3934          */
3935         if (vport->port_state <= LPFC_NS_QRY) {
3936                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3937                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
3938                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
3939
3940                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
3941                 return 0;
3942         }
3943
3944         /* If this RSCN just contains NPortIDs for other vports on this HBA,
3945          * just ACC and ignore it.
3946          */
3947         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3948                 !(vport->cfg_peer_port_login)) {
3949                 i = payload_len;
3950                 datap = lp;
3951                 while (i > 0) {
3952                         nportid = *datap++;
3953                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
3954                         i -= sizeof(uint32_t);
3955                         rscn_id++;
3956                         if (lpfc_find_vport_by_did(phba, nportid))
3957                                 hba_id++;
3958                 }
3959                 if (rscn_id == hba_id) {
3960                         /* ALL NPortIDs in RSCN are on HBA */
3961                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3962                                          "0214 Ignore RSCN "
3963                                          "Data: x%x x%x x%x x%x\n",
3964                                          vport->fc_flag, payload_len,
3965                                          *lp, vport->fc_rscn_id_cnt);
3966                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3967                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
3968                                 ndlp->nlp_DID, vport->port_state,
3969                                 ndlp->nlp_flag);
3970
3971                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
3972                                 ndlp, NULL);
3973                         return 0;
3974                 }
3975         }
3976
3977         spin_lock_irq(shost->host_lock);
3978         if (vport->fc_rscn_flush) {
3979                 /* Another thread is walking fc_rscn_id_list on this vport */
3980                 spin_unlock_irq(shost->host_lock);
3981                 vport->fc_flag |= FC_RSCN_DISCOVERY;
3982                 /* Send back ACC */
3983                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
3984                 return 0;
3985         }
3986         /* Indicate we are walking fc_rscn_id_list on this vport */
3987         vport->fc_rscn_flush = 1;
3988         spin_unlock_irq(shost->host_lock);
3989         /* Get the array count after sucessfully have the token */
3990         rscn_cnt = vport->fc_rscn_id_cnt;
3991         /* If we are already processing an RSCN, save the received
3992          * RSCN payload buffer, cmdiocb->context2 to process later.
3993          */
3994         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
3995                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3996                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
3997                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
3998
3999                 spin_lock_irq(shost->host_lock);
4000                 vport->fc_flag |= FC_RSCN_DEFERRED;
4001                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
4002                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
4003                         vport->fc_flag |= FC_RSCN_MODE;
4004                         spin_unlock_irq(shost->host_lock);
4005                         if (rscn_cnt) {
4006                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4007                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4008                         }
4009                         if ((rscn_cnt) &&
4010                             (payload_len + length <= LPFC_BPL_SIZE)) {
4011                                 *cmd &= ELS_CMD_MASK;
4012                                 *cmd |= cpu_to_be32(payload_len + length);
4013                                 memcpy(((uint8_t *)cmd) + length, lp,
4014                                        payload_len);
4015                         } else {
4016                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4017                                 vport->fc_rscn_id_cnt++;
4018                                 /* If we zero, cmdiocb->context2, the calling
4019                                  * routine will not try to free it.
4020                                  */
4021                                 cmdiocb->context2 = NULL;
4022                         }
4023                         /* Deferred RSCN */
4024                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4025                                          "0235 Deferred RSCN "
4026                                          "Data: x%x x%x x%x\n",
4027                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4028                                          vport->port_state);
4029                 } else {
4030                         vport->fc_flag |= FC_RSCN_DISCOVERY;
4031                         spin_unlock_irq(shost->host_lock);
4032                         /* ReDiscovery RSCN */
4033                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4034                                          "0234 ReDiscovery RSCN "
4035                                          "Data: x%x x%x x%x\n",
4036                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4037                                          vport->port_state);
4038                 }
4039                 /* Indicate we are done walking fc_rscn_id_list on this vport */
4040                 vport->fc_rscn_flush = 0;
4041                 /* Send back ACC */
4042                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4043                 /* send RECOVERY event for ALL nodes that match RSCN payload */
4044                 lpfc_rscn_recovery_check(vport);
4045                 spin_lock_irq(shost->host_lock);
4046                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
4047                 spin_unlock_irq(shost->host_lock);
4048                 return 0;
4049         }
4050         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4051                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
4052                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4053
4054         spin_lock_irq(shost->host_lock);
4055         vport->fc_flag |= FC_RSCN_MODE;
4056         spin_unlock_irq(shost->host_lock);
4057         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
4058         /* Indicate we are done walking fc_rscn_id_list on this vport */
4059         vport->fc_rscn_flush = 0;
4060         /*
4061          * If we zero, cmdiocb->context2, the calling routine will
4062          * not try to free it.
4063          */
4064         cmdiocb->context2 = NULL;
4065         lpfc_set_disctmo(vport);
4066         /* Send back ACC */
4067         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4068         /* send RECOVERY event for ALL nodes that match RSCN payload */
4069         lpfc_rscn_recovery_check(vport);
4070         return lpfc_els_handle_rscn(vport);
4071 }
4072
4073 /**
4074  * lpfc_els_handle_rscn: Handle rscn for a vport.
4075  * @vport: pointer to a host virtual N_Port data structure.
4076  *
4077  * This routine handles the Registration State Configuration Notification
4078  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
4079  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
4080  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
4081  * NameServer shall be issued. If CT command to the NameServer fails to be
4082  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
4083  * RSCN activities with the @vport.
4084  *
4085  * Return code
4086  *   0 - Cleaned up rscn on the @vport
4087  *   1 - Wait for plogi to name server before proceed
4088  **/
4089 int
4090 lpfc_els_handle_rscn(struct lpfc_vport *vport)
4091 {
4092         struct lpfc_nodelist *ndlp;
4093         struct lpfc_hba *phba = vport->phba;
4094
4095         /* Ignore RSCN if the port is being torn down. */
4096         if (vport->load_flag & FC_UNLOADING) {
4097                 lpfc_els_flush_rscn(vport);
4098                 return 0;
4099         }
4100
4101         /* Start timer for RSCN processing */
4102         lpfc_set_disctmo(vport);
4103
4104         /* RSCN processed */
4105         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4106                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
4107                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
4108                          vport->port_state);
4109
4110         /* To process RSCN, first compare RSCN data with NameServer */
4111         vport->fc_ns_retry = 0;
4112         vport->num_disc_nodes = 0;
4113
4114         ndlp = lpfc_findnode_did(vport, NameServer_DID);
4115         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
4116             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
4117                 /* Good ndlp, issue CT Request to NameServer */
4118                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
4119                         /* Wait for NameServer query cmpl before we can
4120                            continue */
4121                         return 1;
4122         } else {
4123                 /* If login to NameServer does not exist, issue one */
4124                 /* Good status, issue PLOGI to NameServer */
4125                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
4126                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4127                         /* Wait for NameServer login cmpl before we can
4128                            continue */
4129                         return 1;
4130
4131                 if (ndlp) {
4132                         ndlp = lpfc_enable_node(vport, ndlp,
4133                                                 NLP_STE_PLOGI_ISSUE);
4134                         if (!ndlp) {
4135                                 lpfc_els_flush_rscn(vport);
4136                                 return 0;
4137                         }
4138                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
4139                 } else {
4140                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4141                         if (!ndlp) {
4142                                 lpfc_els_flush_rscn(vport);
4143                                 return 0;
4144                         }
4145                         lpfc_nlp_init(vport, ndlp, NameServer_DID);
4146                         ndlp->nlp_prev_state = ndlp->nlp_state;
4147                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4148                 }
4149                 ndlp->nlp_type |= NLP_FABRIC;
4150                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
4151                 /* Wait for NameServer login cmpl before we can
4152                  * continue
4153                  */
4154                 return 1;
4155         }
4156
4157         lpfc_els_flush_rscn(vport);
4158         return 0;
4159 }
4160
4161 /**
4162  * lpfc_els_rcv_flogi: Process an unsolicited flogi iocb.
4163  * @vport: pointer to a host virtual N_Port data structure.
4164  * @cmdiocb: pointer to lpfc command iocb data structure.
4165  * @ndlp: pointer to a node-list data structure.
4166  *
4167  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
4168  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
4169  * point topology. As an unsolicited FLOGI should not be received in a loop
4170  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
4171  * lpfc_check_sparm() routine is invoked to check the parameters in the
4172  * unsolicited FLOGI. If parameters validation failed, the routine
4173  * lpfc_els_rsp_reject() shall be called with reject reason code set to
4174  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
4175  * FLOGI shall be compared with the Port WWN of the @vport to determine who
4176  * will initiate PLOGI. The higher lexicographical value party shall has
4177  * higher priority (as the winning port) and will initiate PLOGI and
4178  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
4179  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
4180  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
4181  *
4182  * Return code
4183  *   0 - Successfully processed the unsolicited flogi
4184  *   1 - Failed to process the unsolicited flogi
4185  **/
4186 static int
4187 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4188                    struct lpfc_nodelist *ndlp)
4189 {
4190         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4191         struct lpfc_hba  *phba = vport->phba;
4192         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4193         uint32_t *lp = (uint32_t *) pcmd->virt;
4194         IOCB_t *icmd = &cmdiocb->iocb;
4195         struct serv_parm *sp;
4196         LPFC_MBOXQ_t *mbox;
4197         struct ls_rjt stat;
4198         uint32_t cmd, did;
4199         int rc;
4200
4201         cmd = *lp++;
4202         sp = (struct serv_parm *) lp;
4203
4204         /* FLOGI received */
4205
4206         lpfc_set_disctmo(vport);
4207
4208         if (phba->fc_topology == TOPOLOGY_LOOP) {
4209                 /* We should never receive a FLOGI in loop mode, ignore it */
4210                 did = icmd->un.elsreq64.remoteID;
4211
4212                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
4213                    Loop Mode */
4214                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4215                                  "0113 An FLOGI ELS command x%x was "
4216                                  "received from DID x%x in Loop Mode\n",
4217                                  cmd, did);
4218                 return 1;
4219         }
4220
4221         did = Fabric_DID;
4222
4223         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
4224                 /* For a FLOGI we accept, then if our portname is greater
4225                  * then the remote portname we initiate Nport login.
4226                  */
4227
4228                 rc = memcmp(&vport->fc_portname, &sp->portName,
4229                             sizeof(struct lpfc_name));
4230
4231                 if (!rc) {
4232                         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4233                         if (!mbox)
4234                                 return 1;
4235
4236                         lpfc_linkdown(phba);
4237                         lpfc_init_link(phba, mbox,
4238                                        phba->cfg_topology,
4239                                        phba->cfg_link_speed);
4240                         mbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
4241                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4242                         mbox->vport = vport;
4243                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4244                         lpfc_set_loopback_flag(phba);
4245                         if (rc == MBX_NOT_FINISHED) {
4246                                 mempool_free(mbox, phba->mbox_mem_pool);
4247                         }
4248                         return 1;
4249                 } else if (rc > 0) {    /* greater than */
4250                         spin_lock_irq(shost->host_lock);
4251                         vport->fc_flag |= FC_PT2PT_PLOGI;
4252                         spin_unlock_irq(shost->host_lock);
4253                 }
4254                 spin_lock_irq(shost->host_lock);
4255                 vport->fc_flag |= FC_PT2PT;
4256                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4257                 spin_unlock_irq(shost->host_lock);
4258         } else {
4259                 /* Reject this request because invalid parameters */
4260                 stat.un.b.lsRjtRsvd0 = 0;
4261                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4262                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
4263                 stat.un.b.vendorUnique = 0;
4264                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4265                         NULL);
4266                 return 1;
4267         }
4268
4269         /* Send back ACC */
4270         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
4271
4272         return 0;
4273 }
4274
4275 /**
4276  * lpfc_els_rcv_rnid: Process an unsolicited rnid iocb.
4277  * @vport: pointer to a host virtual N_Port data structure.
4278  * @cmdiocb: pointer to lpfc command iocb data structure.
4279  * @ndlp: pointer to a node-list data structure.
4280  *
4281  * This routine processes Request Node Identification Data (RNID) IOCB
4282  * received as an ELS unsolicited event. Only when the RNID specified format
4283  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
4284  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
4285  * Accept (ACC) the RNID ELS command. All the other RNID formats are
4286  * rejected by invoking the lpfc_els_rsp_reject() routine.
4287  *
4288  * Return code
4289  *   0 - Successfully processed rnid iocb (currently always return 0)
4290  **/
4291 static int
4292 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4293                   struct lpfc_nodelist *ndlp)
4294 {
4295         struct lpfc_dmabuf *pcmd;
4296         uint32_t *lp;
4297         IOCB_t *icmd;
4298         RNID *rn;
4299         struct ls_rjt stat;
4300         uint32_t cmd, did;
4301
4302         icmd = &cmdiocb->iocb;
4303         did = icmd->un.elsreq64.remoteID;
4304         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4305         lp = (uint32_t *) pcmd->virt;
4306
4307         cmd = *lp++;
4308         rn = (RNID *) lp;
4309
4310         /* RNID received */
4311
4312         switch (rn->Format) {
4313         case 0:
4314         case RNID_TOPOLOGY_DISC:
4315                 /* Send back ACC */
4316                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
4317                 break;
4318         default:
4319                 /* Reject this request because format not supported */
4320                 stat.un.b.lsRjtRsvd0 = 0;
4321                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4322                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4323                 stat.un.b.vendorUnique = 0;
4324                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4325                         NULL);
4326         }
4327         return 0;
4328 }
4329
4330 /**
4331  * lpfc_els_rcv_lirr: Process an unsolicited lirr iocb.
4332  * @vport: pointer to a host virtual N_Port data structure.
4333  * @cmdiocb: pointer to lpfc command iocb data structure.
4334  * @ndlp: pointer to a node-list data structure.
4335  *
4336  * This routine processes a Link Incident Report Registration(LIRR) IOCB
4337  * received as an ELS unsolicited event. Currently, this function just invokes
4338  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
4339  *
4340  * Return code
4341  *   0 - Successfully processed lirr iocb (currently always return 0)
4342  **/
4343 static int
4344 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4345                   struct lpfc_nodelist *ndlp)
4346 {
4347         struct ls_rjt stat;
4348
4349         /* For now, unconditionally reject this command */
4350         stat.un.b.lsRjtRsvd0 = 0;
4351         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4352         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4353         stat.un.b.vendorUnique = 0;
4354         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4355         return 0;
4356 }
4357
4358 /**
4359  * lpfc_els_rsp_rps_acc: Completion callbk func for MBX_READ_LNK_STAT mbox cmd.
4360  * @phba: pointer to lpfc hba data structure.
4361  * @pmb: pointer to the driver internal queue element for mailbox command.
4362  *
4363  * This routine is the completion callback function for the MBX_READ_LNK_STAT
4364  * mailbox command. This callback function is to actually send the Accept
4365  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
4366  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
4367  * mailbox command, constructs the RPS response with the link statistics
4368  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
4369  * response to the RPS.
4370  *
4371  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4372  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4373  * will be stored into the context1 field of the IOCB for the completion
4374  * callback function to the RPS Accept Response ELS IOCB command.
4375  *
4376  **/
4377 static void
4378 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4379 {
4380         struct lpfc_sli *psli = &phba->sli;
4381         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
4382         MAILBOX_t *mb;
4383         IOCB_t *icmd;
4384         RPS_RSP *rps_rsp;
4385         uint8_t *pcmd;
4386         struct lpfc_iocbq *elsiocb;
4387         struct lpfc_nodelist *ndlp;
4388         uint16_t xri, status;
4389         uint32_t cmdsize;
4390
4391         mb = &pmb->mb;
4392
4393         ndlp = (struct lpfc_nodelist *) pmb->context2;
4394         xri = (uint16_t) ((unsigned long)(pmb->context1));
4395         pmb->context1 = NULL;
4396         pmb->context2 = NULL;
4397
4398         if (mb->mbxStatus) {
4399                 mempool_free(pmb, phba->mbox_mem_pool);
4400                 return;
4401         }
4402
4403         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
4404         mempool_free(pmb, phba->mbox_mem_pool);
4405         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
4406                                      lpfc_max_els_tries, ndlp,
4407                                      ndlp->nlp_DID, ELS_CMD_ACC);
4408
4409         /* Decrement the ndlp reference count from previous mbox command */
4410         lpfc_nlp_put(ndlp);
4411
4412         if (!elsiocb)
4413                 return;
4414
4415         icmd = &elsiocb->iocb;
4416         icmd->ulpContext = xri;
4417
4418         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4419         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4420         pcmd += sizeof(uint32_t); /* Skip past command */
4421         rps_rsp = (RPS_RSP *)pcmd;
4422
4423         if (phba->fc_topology != TOPOLOGY_LOOP)
4424                 status = 0x10;
4425         else
4426                 status = 0x8;
4427         if (phba->pport->fc_flag & FC_FABRIC)
4428                 status |= 0x4;
4429
4430         rps_rsp->rsvd1 = 0;
4431         rps_rsp->portStatus = cpu_to_be16(status);
4432         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
4433         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
4434         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
4435         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
4436         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
4437         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
4438         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
4439         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
4440                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
4441                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4442                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4443                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4444                          ndlp->nlp_rpi);
4445         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4446         phba->fc_stat.elsXmitACC++;
4447         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR)
4448                 lpfc_els_free_iocb(phba, elsiocb);
4449         return;
4450 }
4451
4452 /**
4453  * lpfc_els_rcv_rps: Process an unsolicited rps iocb.
4454  * @vport: pointer to a host virtual N_Port data structure.
4455  * @cmdiocb: pointer to lpfc command iocb data structure.
4456  * @ndlp: pointer to a node-list data structure.
4457  *
4458  * This routine processes Read Port Status (RPS) IOCB received as an
4459  * ELS unsolicited event. It first checks the remote port state. If the
4460  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
4461  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
4462  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
4463  * for reading the HBA link statistics. It is for the callback function,
4464  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
4465  * to actually sending out RPS Accept (ACC) response.
4466  *
4467  * Return codes
4468  *   0 - Successfully processed rps iocb (currently always return 0)
4469  **/
4470 static int
4471 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4472                  struct lpfc_nodelist *ndlp)
4473 {
4474         struct lpfc_hba *phba = vport->phba;
4475         uint32_t *lp;
4476         uint8_t flag;
4477         LPFC_MBOXQ_t *mbox;
4478         struct lpfc_dmabuf *pcmd;
4479         RPS *rps;
4480         struct ls_rjt stat;
4481
4482         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4483             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
4484                 stat.un.b.lsRjtRsvd0 = 0;
4485                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4486                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4487                 stat.un.b.vendorUnique = 0;
4488                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4489                         NULL);
4490         }
4491
4492         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4493         lp = (uint32_t *) pcmd->virt;
4494         flag = (be32_to_cpu(*lp++) & 0xf);
4495         rps = (RPS *) lp;
4496
4497         if ((flag == 0) ||
4498             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
4499             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
4500                                     sizeof(struct lpfc_name)) == 0))) {
4501
4502                 printk("Fix me....\n");
4503                 dump_stack();
4504                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
4505                 if (mbox) {
4506                         lpfc_read_lnk_stat(phba, mbox);
4507                         mbox->context1 =
4508                             (void *)((unsigned long) cmdiocb->iocb.ulpContext);
4509                         mbox->context2 = lpfc_nlp_get(ndlp);
4510                         mbox->vport = vport;
4511                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
4512                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4513                                 != MBX_NOT_FINISHED)
4514                                 /* Mbox completion will send ELS Response */
4515                                 return 0;
4516                         /* Decrement reference count used for the failed mbox
4517                          * command.
4518                          */
4519                         lpfc_nlp_put(ndlp);
4520                         mempool_free(mbox, phba->mbox_mem_pool);
4521                 }
4522         }
4523         stat.un.b.lsRjtRsvd0 = 0;
4524         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4525         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4526         stat.un.b.vendorUnique = 0;
4527         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4528         return 0;
4529 }
4530
4531 /**
4532  * lpfc_els_rsp_rpl_acc: Issue an accept rpl els command.
4533  * @vport: pointer to a host virtual N_Port data structure.
4534  * @cmdsize: size of the ELS command.
4535  * @oldiocb: pointer to the original lpfc command iocb data structure.
4536  * @ndlp: pointer to a node-list data structure.
4537  *
4538  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
4539  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
4540  *
4541  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4542  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4543  * will be stored into the context1 field of the IOCB for the completion
4544  * callback function to the RPL Accept Response ELS command.
4545  *
4546  * Return code
4547  *   0 - Successfully issued ACC RPL ELS command
4548  *   1 - Failed to issue ACC RPL ELS command
4549  **/
4550 static int
4551 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
4552                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4553 {
4554         struct lpfc_hba *phba = vport->phba;
4555         IOCB_t *icmd, *oldcmd;
4556         RPL_RSP rpl_rsp;
4557         struct lpfc_iocbq *elsiocb;
4558         struct lpfc_sli *psli = &phba->sli;
4559         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
4560         uint8_t *pcmd;
4561
4562         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4563                                      ndlp->nlp_DID, ELS_CMD_ACC);
4564
4565         if (!elsiocb)
4566                 return 1;
4567
4568         icmd = &elsiocb->iocb;
4569         oldcmd = &oldiocb->iocb;
4570         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
4571
4572         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4573         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4574         pcmd += sizeof(uint16_t);
4575         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
4576         pcmd += sizeof(uint16_t);
4577
4578         /* Setup the RPL ACC payload */
4579         rpl_rsp.listLen = be32_to_cpu(1);
4580         rpl_rsp.index = 0;
4581         rpl_rsp.port_num_blk.portNum = 0;
4582         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
4583         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
4584             sizeof(struct lpfc_name));
4585         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
4586         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
4587         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4588                          "0120 Xmit ELS RPL ACC response tag x%x "
4589                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4590                          "rpi x%x\n",
4591                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4592                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4593                          ndlp->nlp_rpi);
4594         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4595         phba->fc_stat.elsXmitACC++;
4596         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
4597                 lpfc_els_free_iocb(phba, elsiocb);
4598                 return 1;
4599         }
4600         return 0;
4601 }
4602
4603 /**
4604  * lpfc_els_rcv_rpl: Process an unsolicited rpl iocb.
4605  * @vport: pointer to a host virtual N_Port data structure.
4606  * @cmdiocb: pointer to lpfc command iocb data structure.
4607  * @ndlp: pointer to a node-list data structure.
4608  *
4609  * This routine processes Read Port List (RPL) IOCB received as an ELS
4610  * unsolicited event. It first checks the remote port state. If the remote
4611  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
4612  * invokes the lpfc_els_rsp_reject() routine to send reject response.
4613  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
4614  * to accept the RPL.
4615  *
4616  * Return code
4617  *   0 - Successfully processed rpl iocb (currently always return 0)
4618  **/
4619 static int
4620 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4621                  struct lpfc_nodelist *ndlp)
4622 {
4623         struct lpfc_dmabuf *pcmd;
4624         uint32_t *lp;
4625         uint32_t maxsize;
4626         uint16_t cmdsize;
4627         RPL *rpl;
4628         struct ls_rjt stat;
4629
4630         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4631             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
4632                 stat.un.b.lsRjtRsvd0 = 0;
4633                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4634                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4635                 stat.un.b.vendorUnique = 0;
4636                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4637                         NULL);
4638         }
4639
4640         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4641         lp = (uint32_t *) pcmd->virt;
4642         rpl = (RPL *) (lp + 1);
4643
4644         maxsize = be32_to_cpu(rpl->maxsize);
4645
4646         /* We support only one port */
4647         if ((rpl->index == 0) &&
4648             ((maxsize == 0) ||
4649              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
4650                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
4651         } else {
4652                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
4653         }
4654         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
4655
4656         return 0;
4657 }
4658
4659 /**
4660  * lpfc_els_rcv_farp: Process an unsolicited farp request els command.
4661  * @vport: pointer to a virtual N_Port data structure.
4662  * @cmdiocb: pointer to lpfc command iocb data structure.
4663  * @ndlp: pointer to a node-list data structure.
4664  *
4665  * This routine processes Fibre Channel Address Resolution Protocol
4666  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
4667  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
4668  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
4669  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
4670  * remote PortName is compared against the FC PortName stored in the @vport
4671  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
4672  * compared against the FC NodeName stored in the @vport data structure.
4673  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
4674  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
4675  * invoked to send out FARP Response to the remote node. Before sending the
4676  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
4677  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
4678  * routine is invoked to log into the remote port first.
4679  *
4680  * Return code
4681  *   0 - Either the FARP Match Mode not supported or successfully processed
4682  **/
4683 static int
4684 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4685                   struct lpfc_nodelist *ndlp)
4686 {
4687         struct lpfc_dmabuf *pcmd;
4688         uint32_t *lp;
4689         IOCB_t *icmd;
4690         FARP *fp;
4691         uint32_t cmd, cnt, did;
4692
4693         icmd = &cmdiocb->iocb;
4694         did = icmd->un.elsreq64.remoteID;
4695         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4696         lp = (uint32_t *) pcmd->virt;
4697
4698         cmd = *lp++;
4699         fp = (FARP *) lp;
4700         /* FARP-REQ received from DID <did> */
4701         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4702                          "0601 FARP-REQ received from DID x%x\n", did);
4703         /* We will only support match on WWPN or WWNN */
4704         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
4705                 return 0;
4706         }
4707
4708         cnt = 0;
4709         /* If this FARP command is searching for my portname */
4710         if (fp->Mflags & FARP_MATCH_PORT) {
4711                 if (memcmp(&fp->RportName, &vport->fc_portname,
4712                            sizeof(struct lpfc_name)) == 0)
4713                         cnt = 1;
4714         }
4715
4716         /* If this FARP command is searching for my nodename */
4717         if (fp->Mflags & FARP_MATCH_NODE) {
4718                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
4719                            sizeof(struct lpfc_name)) == 0)
4720                         cnt = 1;
4721         }
4722
4723         if (cnt) {
4724                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
4725                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
4726                         /* Log back into the node before sending the FARP. */
4727                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
4728                                 ndlp->nlp_prev_state = ndlp->nlp_state;
4729                                 lpfc_nlp_set_state(vport, ndlp,
4730                                                    NLP_STE_PLOGI_ISSUE);
4731                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4732                         }
4733
4734                         /* Send a FARP response to that node */
4735                         if (fp->Rflags & FARP_REQUEST_FARPR)
4736                                 lpfc_issue_els_farpr(vport, did, 0);
4737                 }
4738         }
4739         return 0;
4740 }
4741
4742 /**
4743  * lpfc_els_rcv_farpr: Process an unsolicited farp response iocb.
4744  * @vport: pointer to a host virtual N_Port data structure.
4745  * @cmdiocb: pointer to lpfc command iocb data structure.
4746  * @ndlp: pointer to a node-list data structure.
4747  *
4748  * This routine processes Fibre Channel Address Resolution Protocol
4749  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
4750  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
4751  * the FARP response request.
4752  *
4753  * Return code
4754  *   0 - Successfully processed FARPR IOCB (currently always return 0)
4755  **/
4756 static int
4757 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4758                    struct lpfc_nodelist  *ndlp)
4759 {
4760         struct lpfc_dmabuf *pcmd;
4761         uint32_t *lp;
4762         IOCB_t *icmd;
4763         uint32_t cmd, did;
4764
4765         icmd = &cmdiocb->iocb;
4766         did = icmd->un.elsreq64.remoteID;
4767         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4768         lp = (uint32_t *) pcmd->virt;
4769
4770         cmd = *lp++;
4771         /* FARP-RSP received from DID <did> */
4772         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4773                          "0600 FARP-RSP received from DID x%x\n", did);
4774         /* ACCEPT the Farp resp request */
4775         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4776
4777         return 0;
4778 }
4779
4780 /**
4781  * lpfc_els_rcv_fan: Process an unsolicited fan iocb command.
4782  * @vport: pointer to a host virtual N_Port data structure.
4783  * @cmdiocb: pointer to lpfc command iocb data structure.
4784  * @fan_ndlp: pointer to a node-list data structure.
4785  *
4786  * This routine processes a Fabric Address Notification (FAN) IOCB
4787  * command received as an ELS unsolicited event. The FAN ELS command will
4788  * only be processed on a physical port (i.e., the @vport represents the
4789  * physical port). The fabric NodeName and PortName from the FAN IOCB are
4790  * compared against those in the phba data structure. If any of those is
4791  * different, the lpfc_initial_flogi() routine is invoked to initialize
4792  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
4793  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
4794  * is invoked to register login to the fabric.
4795  *
4796  * Return code
4797  *   0 - Successfully processed fan iocb (currently always return 0).
4798  **/
4799 static int
4800 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4801                  struct lpfc_nodelist *fan_ndlp)
4802 {
4803         struct lpfc_hba *phba = vport->phba;
4804         uint32_t *lp;
4805         FAN *fp;
4806
4807         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
4808         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
4809         fp = (FAN *) ++lp;
4810         /* FAN received; Fan does not have a reply sequence */
4811         if ((vport == phba->pport) &&
4812             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
4813                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
4814                             sizeof(struct lpfc_name))) ||
4815                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
4816                             sizeof(struct lpfc_name)))) {
4817                         /* This port has switched fabrics. FLOGI is required */
4818                         lpfc_initial_flogi(vport);
4819                 } else {
4820                         /* FAN verified - skip FLOGI */
4821                         vport->fc_myDID = vport->fc_prevDID;
4822                         lpfc_issue_fabric_reglogin(vport);
4823                 }
4824         }
4825         return 0;
4826 }
4827
4828 /**
4829  * lpfc_els_timeout: Handler funciton to the els timer.
4830  * @ptr: holder for the timer function associated data.
4831  *
4832  * This routine is invoked by the ELS timer after timeout. It posts the ELS
4833  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
4834  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
4835  * up the worker thread. It is for the worker thread to invoke the routine
4836  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
4837  **/
4838 void
4839 lpfc_els_timeout(unsigned long ptr)
4840 {
4841         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4842         struct lpfc_hba   *phba = vport->phba;
4843         uint32_t tmo_posted;
4844         unsigned long iflag;
4845
4846         spin_lock_irqsave(&vport->work_port_lock, iflag);
4847         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
4848         if (!tmo_posted)
4849                 vport->work_port_events |= WORKER_ELS_TMO;
4850         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
4851
4852         if (!tmo_posted)
4853                 lpfc_worker_wake_up(phba);
4854         return;
4855 }
4856
4857 /**
4858  * lpfc_els_timeout_handler: Process an els timeout event.
4859  * @vport: pointer to a virtual N_Port data structure.
4860  *
4861  * This routine is the actual handler function that processes an ELS timeout
4862  * event. It walks the ELS ring to get and abort all the IOCBs (except the
4863  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
4864  * invoking the lpfc_sli_issue_abort_iotag() routine.
4865  **/
4866 void
4867 lpfc_els_timeout_handler(struct lpfc_vport *vport)
4868 {
4869         struct lpfc_hba  *phba = vport->phba;
4870         struct lpfc_sli_ring *pring;
4871         struct lpfc_iocbq *tmp_iocb, *piocb;
4872         IOCB_t *cmd = NULL;
4873         struct lpfc_dmabuf *pcmd;
4874         uint32_t els_command = 0;
4875         uint32_t timeout;
4876         uint32_t remote_ID = 0xffffffff;
4877
4878         /* If the timer is already canceled do nothing */
4879         if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
4880                 return;
4881         }
4882         spin_lock_irq(&phba->hbalock);
4883         timeout = (uint32_t)(phba->fc_ratov << 1);
4884
4885         pring = &phba->sli.ring[LPFC_ELS_RING];
4886
4887         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
4888                 cmd = &piocb->iocb;
4889
4890                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
4891                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
4892                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4893                         continue;
4894
4895                 if (piocb->vport != vport)
4896                         continue;
4897
4898                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
4899                 if (pcmd)
4900                         els_command = *(uint32_t *) (pcmd->virt);
4901
4902                 if (els_command == ELS_CMD_FARP ||
4903                     els_command == ELS_CMD_FARPR ||
4904                     els_command == ELS_CMD_FDISC)
4905                         continue;
4906
4907                 if (piocb->drvrTimeout > 0) {
4908                         if (piocb->drvrTimeout >= timeout)
4909                                 piocb->drvrTimeout -= timeout;
4910                         else
4911                                 piocb->drvrTimeout = 0;
4912                         continue;
4913                 }
4914
4915                 remote_ID = 0xffffffff;
4916                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
4917                         remote_ID = cmd->un.elsreq64.remoteID;
4918                 else {
4919                         struct lpfc_nodelist *ndlp;
4920                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
4921                         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4922                                 remote_ID = ndlp->nlp_DID;
4923                 }
4924                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4925                                  "0127 ELS timeout Data: x%x x%x x%x "
4926                                  "x%x\n", els_command,
4927                                  remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
4928                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
4929         }
4930         spin_unlock_irq(&phba->hbalock);
4931
4932         if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
4933                 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
4934 }
4935
4936 /**
4937  * lpfc_els_flush_cmd: Clean up the outstanding els commands to a vport.
4938  * @vport: pointer to a host virtual N_Port data structure.
4939  *
4940  * This routine is used to clean up all the outstanding ELS commands on a
4941  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
4942  * routine. After that, it walks the ELS transmit queue to remove all the
4943  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
4944  * the IOCBs with a non-NULL completion callback function, the callback
4945  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
4946  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
4947  * callback function, the IOCB will simply be released. Finally, it walks
4948  * the ELS transmit completion queue to issue an abort IOCB to any transmit
4949  * completion queue IOCB that is associated with the @vport and is not
4950  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
4951  * part of the discovery state machine) out to HBA by invoking the
4952  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
4953  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
4954  * the IOCBs are aborted when this function returns.
4955  **/
4956 void
4957 lpfc_els_flush_cmd(struct lpfc_vport *vport)
4958 {
4959         LIST_HEAD(completions);
4960         struct lpfc_hba  *phba = vport->phba;
4961         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4962         struct lpfc_iocbq *tmp_iocb, *piocb;
4963         IOCB_t *cmd = NULL;
4964
4965         lpfc_fabric_abort_vport(vport);
4966
4967         spin_lock_irq(&phba->hbalock);
4968         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
4969                 cmd = &piocb->iocb;
4970
4971                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
4972                         continue;
4973                 }
4974
4975                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
4976                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
4977                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
4978                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4979                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
4980                         continue;
4981
4982                 if (piocb->vport != vport)
4983                         continue;
4984
4985                 list_move_tail(&piocb->list, &completions);
4986                 pring->txq_cnt--;
4987         }
4988
4989         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
4990                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
4991                         continue;
4992                 }
4993
4994                 if (piocb->vport != vport)
4995                         continue;
4996
4997                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
4998         }
4999         spin_unlock_irq(&phba->hbalock);
5000
5001         while (!list_empty(&completions)) {
5002                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
5003                 cmd = &piocb->iocb;
5004                 list_del_init(&piocb->list);
5005
5006                 if (!piocb->iocb_cmpl)
5007                         lpfc_sli_release_iocbq(phba, piocb);
5008                 else {
5009                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
5010                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
5011                         (piocb->iocb_cmpl) (phba, piocb, piocb);
5012                 }
5013         }
5014
5015         return;
5016 }
5017
5018 /**
5019  * lpfc_els_flush_all_cmd: Clean up all the outstanding els commands to a HBA.
5020  * @phba: pointer to lpfc hba data structure.
5021  *
5022  * This routine is used to clean up all the outstanding ELS commands on a
5023  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
5024  * routine. After that, it walks the ELS transmit queue to remove all the
5025  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
5026  * the IOCBs with the completion callback function associated, the callback
5027  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5028  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
5029  * callback function associated, the IOCB will simply be released. Finally,
5030  * it walks the ELS transmit completion queue to issue an abort IOCB to any
5031  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
5032  * management plane IOCBs that are not part of the discovery state machine)
5033  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
5034  **/
5035 void
5036 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
5037 {
5038         LIST_HEAD(completions);
5039         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5040         struct lpfc_iocbq *tmp_iocb, *piocb;
5041         IOCB_t *cmd = NULL;
5042
5043         lpfc_fabric_abort_hba(phba);
5044         spin_lock_irq(&phba->hbalock);
5045         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5046                 cmd = &piocb->iocb;
5047                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5048                         continue;
5049                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5050                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5051                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5052                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5053                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
5054                         continue;
5055                 list_move_tail(&piocb->list, &completions);
5056                 pring->txq_cnt--;
5057         }
5058         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5059                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5060                         continue;
5061                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5062         }
5063         spin_unlock_irq(&phba->hbalock);
5064         while (!list_empty(&completions)) {
5065                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
5066                 cmd = &piocb->iocb;
5067                 list_del_init(&piocb->list);
5068                 if (!piocb->iocb_cmpl)
5069                         lpfc_sli_release_iocbq(phba, piocb);
5070                 else {
5071                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
5072                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
5073                         (piocb->iocb_cmpl) (phba, piocb, piocb);
5074                 }
5075         }
5076         return;
5077 }
5078
5079 /**
5080  * lpfc_els_unsol_buffer: Process an unsolicited event data buffer.
5081  * @phba: pointer to lpfc hba data structure.
5082  * @pring: pointer to a SLI ring.
5083  * @vport: pointer to a host virtual N_Port data structure.
5084  * @elsiocb: pointer to lpfc els command iocb data structure.
5085  *
5086  * This routine is used for processing the IOCB associated with a unsolicited
5087  * event. It first determines whether there is an existing ndlp that matches
5088  * the DID from the unsolicited IOCB. If not, it will create a new one with
5089  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
5090  * IOCB is then used to invoke the proper routine and to set up proper state
5091  * of the discovery state machine.
5092  **/
5093 static void
5094 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5095                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
5096 {
5097         struct Scsi_Host  *shost;
5098         struct lpfc_nodelist *ndlp;
5099         struct ls_rjt stat;
5100         uint32_t *payload;
5101         uint32_t cmd, did, newnode, rjt_err = 0;
5102         IOCB_t *icmd = &elsiocb->iocb;
5103
5104         if (!vport || !(elsiocb->context2))
5105                 goto dropit;
5106
5107         newnode = 0;
5108         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
5109         cmd = *payload;
5110         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
5111                 lpfc_post_buffer(phba, pring, 1);
5112
5113         did = icmd->un.rcvels.remoteID;
5114         if (icmd->ulpStatus) {
5115                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5116                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
5117                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
5118                 goto dropit;
5119         }
5120
5121         /* Check to see if link went down during discovery */
5122         if (lpfc_els_chk_latt(vport))
5123                 goto dropit;
5124
5125         /* Ignore traffic recevied during vport shutdown. */
5126         if (vport->load_flag & FC_UNLOADING)
5127                 goto dropit;
5128
5129         ndlp = lpfc_findnode_did(vport, did);
5130         if (!ndlp) {
5131                 /* Cannot find existing Fabric ndlp, so allocate a new one */
5132                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5133                 if (!ndlp)
5134                         goto dropit;
5135
5136                 lpfc_nlp_init(vport, ndlp, did);
5137                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5138                 newnode = 1;
5139                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5140                         ndlp->nlp_type |= NLP_FABRIC;
5141         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5142                 ndlp = lpfc_enable_node(vport, ndlp,
5143                                         NLP_STE_UNUSED_NODE);
5144                 if (!ndlp)
5145                         goto dropit;
5146                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5147                 newnode = 1;
5148                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5149                         ndlp->nlp_type |= NLP_FABRIC;
5150         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
5151                 /* This is similar to the new node path */
5152                 ndlp = lpfc_nlp_get(ndlp);
5153                 if (!ndlp)
5154                         goto dropit;
5155                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5156                 newnode = 1;
5157         }
5158
5159         phba->fc_stat.elsRcvFrame++;
5160         if (elsiocb->context1)
5161                 lpfc_nlp_put(elsiocb->context1);
5162
5163         elsiocb->context1 = lpfc_nlp_get(ndlp);
5164         elsiocb->vport = vport;
5165
5166         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
5167                 cmd &= ELS_CMD_MASK;
5168         }
5169         /* ELS command <elsCmd> received from NPORT <did> */
5170         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5171                          "0112 ELS command x%x received from NPORT x%x "
5172                          "Data: x%x\n", cmd, did, vport->port_state);
5173         switch (cmd) {
5174         case ELS_CMD_PLOGI:
5175                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5176                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
5177                         did, vport->port_state, ndlp->nlp_flag);
5178
5179                 phba->fc_stat.elsRcvPLOGI++;
5180                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
5181
5182                 if (vport->port_state < LPFC_DISC_AUTH) {
5183                         if (!(phba->pport->fc_flag & FC_PT2PT) ||
5184                                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
5185                                 rjt_err = LSRJT_UNABLE_TPC;
5186                                 break;
5187                         }
5188                         /* We get here, and drop thru, if we are PT2PT with
5189                          * another NPort and the other side has initiated
5190                          * the PLOGI before responding to our FLOGI.
5191                          */
5192                 }
5193
5194                 shost = lpfc_shost_from_vport(vport);
5195                 spin_lock_irq(shost->host_lock);
5196                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
5197                 spin_unlock_irq(shost->host_lock);
5198
5199                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5200                                         NLP_EVT_RCV_PLOGI);
5201
5202                 break;
5203         case ELS_CMD_FLOGI:
5204                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5205                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
5206                         did, vport->port_state, ndlp->nlp_flag);
5207
5208                 phba->fc_stat.elsRcvFLOGI++;
5209                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
5210                 if (newnode)
5211                         lpfc_nlp_put(ndlp);
5212                 break;
5213         case ELS_CMD_LOGO:
5214                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5215                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
5216                         did, vport->port_state, ndlp->nlp_flag);
5217
5218                 phba->fc_stat.elsRcvLOGO++;
5219                 if (vport->port_state < LPFC_DISC_AUTH) {
5220                         rjt_err = LSRJT_UNABLE_TPC;
5221                         break;
5222                 }
5223                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
5224                 break;
5225         case ELS_CMD_PRLO:
5226                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5227                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
5228                         did, vport->port_state, ndlp->nlp_flag);
5229
5230                 phba->fc_stat.elsRcvPRLO++;
5231                 if (vport->port_state < LPFC_DISC_AUTH) {
5232                         rjt_err = LSRJT_UNABLE_TPC;
5233                         break;
5234                 }
5235                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
5236                 break;
5237         case ELS_CMD_RSCN:
5238                 phba->fc_stat.elsRcvRSCN++;
5239                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
5240                 if (newnode)
5241                         lpfc_nlp_put(ndlp);
5242                 break;
5243         case ELS_CMD_ADISC:
5244                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5245                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
5246                         did, vport->port_state, ndlp->nlp_flag);
5247
5248                 phba->fc_stat.elsRcvADISC++;
5249                 if (vport->port_state < LPFC_DISC_AUTH) {
5250                         rjt_err = LSRJT_UNABLE_TPC;
5251                         break;
5252                 }
5253                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5254                                         NLP_EVT_RCV_ADISC);
5255                 break;
5256         case ELS_CMD_PDISC:
5257                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5258                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
5259                         did, vport->port_state, ndlp->nlp_flag);
5260
5261                 phba->fc_stat.elsRcvPDISC++;
5262                 if (vport->port_state < LPFC_DISC_AUTH) {
5263                         rjt_err = LSRJT_UNABLE_TPC;
5264                         break;
5265                 }
5266                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5267                                         NLP_EVT_RCV_PDISC);
5268                 break;
5269         case ELS_CMD_FARPR:
5270                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5271                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
5272                         did, vport->port_state, ndlp->nlp_flag);
5273
5274                 phba->fc_stat.elsRcvFARPR++;
5275                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
5276                 break;
5277         case ELS_CMD_FARP:
5278                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5279                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
5280                         did, vport->port_state, ndlp->nlp_flag);
5281
5282                 phba->fc_stat.elsRcvFARP++;
5283                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
5284                 break;
5285         case ELS_CMD_FAN:
5286                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5287                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
5288                         did, vport->port_state, ndlp->nlp_flag);
5289
5290                 phba->fc_stat.elsRcvFAN++;
5291                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
5292                 break;
5293         case ELS_CMD_PRLI:
5294                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5295                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
5296                         did, vport->port_state, ndlp->nlp_flag);
5297
5298                 phba->fc_stat.elsRcvPRLI++;
5299                 if (vport->port_state < LPFC_DISC_AUTH) {
5300                         rjt_err = LSRJT_UNABLE_TPC;
5301                         break;
5302                 }
5303                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
5304                 break;
5305         case ELS_CMD_LIRR:
5306                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5307                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
5308                         did, vport->port_state, ndlp->nlp_flag);
5309
5310                 phba->fc_stat.elsRcvLIRR++;
5311                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
5312                 if (newnode)
5313                         lpfc_nlp_put(ndlp);
5314                 break;
5315         case ELS_CMD_RPS:
5316                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5317                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
5318                         did, vport->port_state, ndlp->nlp_flag);
5319
5320                 phba->fc_stat.elsRcvRPS++;
5321                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
5322                 if (newnode)
5323                         lpfc_nlp_put(ndlp);
5324                 break;
5325         case ELS_CMD_RPL:
5326                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5327                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
5328                         did, vport->port_state, ndlp->nlp_flag);
5329
5330                 phba->fc_stat.elsRcvRPL++;
5331                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
5332                 if (newnode)
5333                         lpfc_nlp_put(ndlp);
5334                 break;
5335         case ELS_CMD_RNID:
5336                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5337                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
5338                         did, vport->port_state, ndlp->nlp_flag);
5339
5340                 phba->fc_stat.elsRcvRNID++;
5341                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
5342                 if (newnode)
5343                         lpfc_nlp_put(ndlp);
5344                 break;
5345         default:
5346                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5347                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
5348                         cmd, did, vport->port_state);
5349
5350                 /* Unsupported ELS command, reject */
5351                 rjt_err = LSRJT_INVALID_CMD;
5352
5353                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
5354                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5355                                  "0115 Unknown ELS command x%x "
5356                                  "received from NPORT x%x\n", cmd, did);
5357                 if (newnode)
5358                         lpfc_nlp_put(ndlp);
5359                 break;
5360         }
5361
5362         /* check if need to LS_RJT received ELS cmd */
5363         if (rjt_err) {
5364                 memset(&stat, 0, sizeof(stat));
5365                 stat.un.b.lsRjtRsnCode = rjt_err;
5366                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
5367                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
5368                         NULL);
5369         }
5370
5371         return;
5372
5373 dropit:
5374         if (vport && !(vport->load_flag & FC_UNLOADING))
5375                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5376                         "(%d):0111 Dropping received ELS cmd "
5377                         "Data: x%x x%x x%x\n",
5378                         vport->vpi, icmd->ulpStatus,
5379                         icmd->un.ulpWord[4], icmd->ulpTimeout);
5380         phba->fc_stat.elsRcvDrop++;
5381 }
5382
5383 /**
5384  * lpfc_find_vport_by_vpid: Find a vport on a HBA through vport identifier.
5385  * @phba: pointer to lpfc hba data structure.
5386  * @vpi: host virtual N_Port identifier.
5387  *
5388  * This routine finds a vport on a HBA (referred by @phba) through a
5389  * @vpi. The function walks the HBA's vport list and returns the address
5390  * of the vport with the matching @vpi.
5391  *
5392  * Return code
5393  *    NULL - No vport with the matching @vpi found
5394  *    Otherwise - Address to the vport with the matching @vpi.
5395  **/
5396 static struct lpfc_vport *
5397 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5398 {
5399         struct lpfc_vport *vport;
5400         unsigned long flags;
5401
5402         spin_lock_irqsave(&phba->hbalock, flags);
5403         list_for_each_entry(vport, &phba->port_list, listentry) {
5404                 if (vport->vpi == vpi) {
5405                         spin_unlock_irqrestore(&phba->hbalock, flags);
5406                         return vport;
5407                 }
5408         }
5409         spin_unlock_irqrestore(&phba->hbalock, flags);
5410         return NULL;
5411 }
5412
5413 /**
5414  * lpfc_els_unsol_event: Process an unsolicited event from an els sli ring.
5415  * @phba: pointer to lpfc hba data structure.
5416  * @pring: pointer to a SLI ring.
5417  * @elsiocb: pointer to lpfc els iocb data structure.
5418  *
5419  * This routine is used to process an unsolicited event received from a SLI
5420  * (Service Level Interface) ring. The actual processing of the data buffer
5421  * associated with the unsolicited event is done by invoking the routine
5422  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
5423  * SLI ring on which the unsolicited event was received.
5424  **/
5425 void
5426 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5427                      struct lpfc_iocbq *elsiocb)
5428 {
5429         struct lpfc_vport *vport = phba->pport;
5430         IOCB_t *icmd = &elsiocb->iocb;
5431         dma_addr_t paddr;
5432         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
5433         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
5434
5435         elsiocb->context2 = NULL;
5436         elsiocb->context3 = NULL;
5437
5438         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
5439                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
5440         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
5441             (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
5442                 phba->fc_stat.NoRcvBuf++;
5443                 /* Not enough posted buffers; Try posting more buffers */
5444                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
5445                         lpfc_post_buffer(phba, pring, 0);
5446                 return;
5447         }
5448
5449         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5450             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
5451              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
5452                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
5453                         vport = phba->pport;
5454                 else {
5455                         uint16_t vpi = icmd->unsli3.rcvsli3.vpi;
5456                         vport = lpfc_find_vport_by_vpid(phba, vpi);
5457                 }
5458         }
5459         /* If there are no BDEs associated
5460          * with this IOCB, there is nothing to do.
5461          */
5462         if (icmd->ulpBdeCount == 0)
5463                 return;
5464
5465         /* type of ELS cmd is first 32bit word
5466          * in packet
5467          */
5468         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5469                 elsiocb->context2 = bdeBuf1;
5470         } else {
5471                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
5472                                  icmd->un.cont64[0].addrLow);
5473                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
5474                                                              paddr);
5475         }
5476
5477         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5478         /*
5479          * The different unsolicited event handlers would tell us
5480          * if they are done with "mp" by setting context2 to NULL.
5481          */
5482         lpfc_nlp_put(elsiocb->context1);
5483         elsiocb->context1 = NULL;
5484         if (elsiocb->context2) {
5485                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
5486                 elsiocb->context2 = NULL;
5487         }
5488
5489         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
5490         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
5491             icmd->ulpBdeCount == 2) {
5492                 elsiocb->context2 = bdeBuf2;
5493                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5494                 /* free mp if we are done with it */
5495                 if (elsiocb->context2) {
5496                         lpfc_in_buf_free(phba, elsiocb->context2);
5497                         elsiocb->context2 = NULL;
5498                 }
5499         }
5500 }
5501
5502 /**
5503  * lpfc_do_scr_ns_plogi: Issue a plogi to the name server for scr.
5504  * @phba: pointer to lpfc hba data structure.
5505  * @vport: pointer to a virtual N_Port data structure.
5506  *
5507  * This routine issues a Port Login (PLOGI) to the Name Server with
5508  * State Change Request (SCR) for a @vport. This routine will create an
5509  * ndlp for the Name Server associated to the @vport if such node does
5510  * not already exist. The PLOGI to Name Server is issued by invoking the
5511  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
5512  * (FDMI) is configured to the @vport, a FDMI node will be created and
5513  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
5514  **/
5515 void
5516 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5517 {
5518         struct lpfc_nodelist *ndlp, *ndlp_fdmi;
5519
5520         ndlp = lpfc_findnode_did(vport, NameServer_DID);
5521         if (!ndlp) {
5522                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5523                 if (!ndlp) {
5524                         if (phba->fc_topology == TOPOLOGY_LOOP) {
5525                                 lpfc_disc_start(vport);
5526                                 return;
5527                         }
5528                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5529                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5530                                          "0251 NameServer login: no memory\n");
5531                         return;
5532                 }
5533                 lpfc_nlp_init(vport, ndlp, NameServer_DID);
5534         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5535                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5536                 if (!ndlp) {
5537                         if (phba->fc_topology == TOPOLOGY_LOOP) {
5538                                 lpfc_disc_start(vport);
5539                                 return;
5540                         }
5541                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5542                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5543                                         "0348 NameServer login: node freed\n");
5544                         return;
5545                 }
5546         }
5547         ndlp->nlp_type |= NLP_FABRIC;
5548
5549         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5550
5551         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
5552                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5553                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5554                                  "0252 Cannot issue NameServer login\n");
5555                 return;
5556         }
5557
5558         if (vport->cfg_fdmi_on) {
5559                 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
5560                                           GFP_KERNEL);
5561                 if (ndlp_fdmi) {
5562                         lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
5563                         ndlp_fdmi->nlp_type |= NLP_FABRIC;
5564                         lpfc_nlp_set_state(vport, ndlp_fdmi,
5565                                 NLP_STE_PLOGI_ISSUE);
5566                         lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
5567                                              0);
5568                 }
5569         }
5570         return;
5571 }
5572
5573 /**
5574  * lpfc_cmpl_reg_new_vport: Completion callback function to register new vport.
5575  * @phba: pointer to lpfc hba data structure.
5576  * @pmb: pointer to the driver internal queue element for mailbox command.
5577  *
5578  * This routine is the completion callback function to register new vport
5579  * mailbox command. If the new vport mailbox command completes successfully,
5580  * the fabric registration login shall be performed on physical port (the
5581  * new vport created is actually a physical port, with VPI 0) or the port
5582  * login to Name Server for State Change Request (SCR) will be performed
5583  * on virtual port (real virtual port, with VPI greater than 0).
5584  **/
5585 static void
5586 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5587 {
5588         struct lpfc_vport *vport = pmb->vport;
5589         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
5590         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5591         MAILBOX_t *mb = &pmb->mb;
5592
5593         spin_lock_irq(shost->host_lock);
5594         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
5595         spin_unlock_irq(shost->host_lock);
5596
5597         if (mb->mbxStatus) {
5598                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5599                                  "0915 Register VPI failed: 0x%x\n",
5600                                  mb->mbxStatus);
5601
5602                 switch (mb->mbxStatus) {
5603                 case 0x11:      /* unsupported feature */
5604                 case 0x9603:    /* max_vpi exceeded */
5605                 case 0x9602:    /* Link event since CLEAR_LA */
5606                         /* giving up on vport registration */
5607                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5608                         spin_lock_irq(shost->host_lock);
5609                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
5610                         spin_unlock_irq(shost->host_lock);
5611                         lpfc_can_disctmo(vport);
5612                         break;
5613                 default:
5614                         /* Try to recover from this error */
5615                         lpfc_mbx_unreg_vpi(vport);
5616                         spin_lock_irq(shost->host_lock);
5617                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
5618                         spin_unlock_irq(shost->host_lock);
5619                         if (vport->port_type == LPFC_PHYSICAL_PORT)
5620                                 lpfc_initial_flogi(vport);
5621                         else
5622                                 lpfc_initial_fdisc(vport);
5623                         break;
5624                 }
5625
5626         } else {
5627                 if (vport == phba->pport)
5628                         lpfc_issue_fabric_reglogin(vport);
5629                 else
5630                         lpfc_do_scr_ns_plogi(phba, vport);
5631         }
5632
5633         /* Now, we decrement the ndlp reference count held for this
5634          * callback function
5635          */
5636         lpfc_nlp_put(ndlp);
5637
5638         mempool_free(pmb, phba->mbox_mem_pool);
5639         return;
5640 }
5641
5642 /**
5643  * lpfc_register_new_vport: Register a new vport with a HBA.
5644  * @phba: pointer to lpfc hba data structure.
5645  * @vport: pointer to a host virtual N_Port data structure.
5646  * @ndlp: pointer to a node-list data structure.
5647  *
5648  * This routine registers the @vport as a new virtual port with a HBA.
5649  * It is done through a registering vpi mailbox command.
5650  **/
5651 static void
5652 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
5653                         struct lpfc_nodelist *ndlp)
5654 {
5655         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5656         LPFC_MBOXQ_t *mbox;
5657
5658         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5659         if (mbox) {
5660                 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, mbox);
5661                 mbox->vport = vport;
5662                 mbox->context2 = lpfc_nlp_get(ndlp);
5663                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
5664                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5665                     == MBX_NOT_FINISHED) {
5666                         /* mailbox command not success, decrement ndlp
5667                          * reference count for this command
5668                          */
5669                         lpfc_nlp_put(ndlp);
5670                         mempool_free(mbox, phba->mbox_mem_pool);
5671
5672                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5673                                 "0253 Register VPI: Can't send mbox\n");
5674                         goto mbox_err_exit;
5675                 }
5676         } else {
5677                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5678                                  "0254 Register VPI: no memory\n");
5679                 goto mbox_err_exit;
5680         }
5681         return;
5682
5683 mbox_err_exit:
5684         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5685         spin_lock_irq(shost->host_lock);
5686         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
5687         spin_unlock_irq(shost->host_lock);
5688         return;
5689 }
5690
5691 /**
5692  * lpfc_cmpl_els_fdisc: Completion function for fdisc iocb command.
5693  * @phba: pointer to lpfc hba data structure.
5694  * @cmdiocb: pointer to lpfc command iocb data structure.
5695  * @rspiocb: pointer to lpfc response iocb data structure.
5696  *
5697  * This routine is the completion callback function to a Fabric Discover
5698  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
5699  * single threaded, each FDISC completion callback function will reset
5700  * the discovery timer for all vports such that the timers will not get
5701  * unnecessary timeout. The function checks the FDISC IOCB status. If error
5702  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
5703  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
5704  * assigned to the vport has been changed with the completion of the FDISC
5705  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
5706  * are unregistered from the HBA, and then the lpfc_register_new_vport()
5707  * routine is invoked to register new vport with the HBA. Otherwise, the
5708  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
5709  * Server for State Change Request (SCR).
5710  **/
5711 static void
5712 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5713                     struct lpfc_iocbq *rspiocb)
5714 {
5715         struct lpfc_vport *vport = cmdiocb->vport;
5716         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
5717         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
5718         struct lpfc_nodelist *np;
5719         struct lpfc_nodelist *next_np;
5720         IOCB_t *irsp = &rspiocb->iocb;
5721         struct lpfc_iocbq *piocb;
5722
5723         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5724                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
5725                          irsp->ulpStatus, irsp->un.ulpWord[4],
5726                          vport->fc_prevDID);
5727         /* Since all FDISCs are being single threaded, we
5728          * must reset the discovery timer for ALL vports
5729          * waiting to send FDISC when one completes.
5730          */
5731         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
5732                 lpfc_set_disctmo(piocb->vport);
5733         }
5734
5735         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5736                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
5737                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
5738
5739         if (irsp->ulpStatus) {
5740                 /* Check for retry */
5741                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
5742                         goto out;
5743                 /* FDISC failed */
5744                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5745                                  "0124 FDISC failed. (%d/%d)\n",
5746                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
5747                 if (vport->fc_vport->vport_state == FC_VPORT_INITIALIZING)
5748                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5749                 lpfc_nlp_put(ndlp);
5750                 /* giving up on FDISC. Cancel discovery timer */
5751                 lpfc_can_disctmo(vport);
5752         } else {
5753                 spin_lock_irq(shost->host_lock);
5754                 vport->fc_flag |= FC_FABRIC;
5755                 if (vport->phba->fc_topology == TOPOLOGY_LOOP)
5756                         vport->fc_flag |=  FC_PUBLIC_LOOP;
5757                 spin_unlock_irq(shost->host_lock);
5758
5759                 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
5760                 lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
5761                 if ((vport->fc_prevDID != vport->fc_myDID) &&
5762                         !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
5763                         /* If our NportID changed, we need to ensure all
5764                          * remaining NPORTs get unreg_login'ed so we can
5765                          * issue unreg_vpi.
5766                          */
5767                         list_for_each_entry_safe(np, next_np,
5768                                 &vport->fc_nodes, nlp_listp) {
5769                                 if (!NLP_CHK_NODE_ACT(ndlp) ||
5770                                     (np->nlp_state != NLP_STE_NPR_NODE) ||
5771                                     !(np->nlp_flag & NLP_NPR_ADISC))
5772                                         continue;
5773                                 spin_lock_irq(shost->host_lock);
5774                                 np->nlp_flag &= ~NLP_NPR_ADISC;
5775                                 spin_unlock_irq(shost->host_lock);
5776                                 lpfc_unreg_rpi(vport, np);
5777                         }
5778                         lpfc_mbx_unreg_vpi(vport);
5779                         spin_lock_irq(shost->host_lock);
5780                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
5781                         spin_unlock_irq(shost->host_lock);
5782                 }
5783
5784                 if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
5785                         lpfc_register_new_vport(phba, vport, ndlp);
5786                 else
5787                         lpfc_do_scr_ns_plogi(phba, vport);
5788
5789                 /* Unconditionaly kick off releasing fabric node for vports */
5790                 lpfc_nlp_put(ndlp);
5791         }
5792
5793 out:
5794         lpfc_els_free_iocb(phba, cmdiocb);
5795 }
5796
5797 /**
5798  * lpfc_issue_els_fdisc: Issue a fdisc iocb command.
5799  * @vport: pointer to a virtual N_Port data structure.
5800  * @ndlp: pointer to a node-list data structure.
5801  * @retry: number of retries to the command IOCB.
5802  *
5803  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
5804  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
5805  * routine to issue the IOCB, which makes sure only one outstanding fabric
5806  * IOCB will be sent off HBA at any given time.
5807  *
5808  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5809  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5810  * will be stored into the context1 field of the IOCB for the completion
5811  * callback function to the FDISC ELS command.
5812  *
5813  * Return code
5814  *   0 - Successfully issued fdisc iocb command
5815  *   1 - Failed to issue fdisc iocb command
5816  **/
5817 static int
5818 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5819                      uint8_t retry)
5820 {
5821         struct lpfc_hba *phba = vport->phba;
5822         IOCB_t *icmd;
5823         struct lpfc_iocbq *elsiocb;
5824         struct serv_parm *sp;
5825         uint8_t *pcmd;
5826         uint16_t cmdsize;
5827         int did = ndlp->nlp_DID;
5828         int rc;
5829
5830         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
5831         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
5832                                      ELS_CMD_FDISC);
5833         if (!elsiocb) {
5834                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5835                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5836                                  "0255 Issue FDISC: no IOCB\n");
5837                 return 1;
5838         }
5839
5840         icmd = &elsiocb->iocb;
5841         icmd->un.elsreq64.myID = 0;
5842         icmd->un.elsreq64.fl = 1;
5843
5844         /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
5845         icmd->ulpCt_h = 1;
5846         icmd->ulpCt_l = 0;
5847
5848         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5849         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
5850         pcmd += sizeof(uint32_t); /* CSP Word 1 */
5851         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
5852         sp = (struct serv_parm *) pcmd;
5853         /* Setup CSPs accordingly for Fabric */
5854         sp->cmn.e_d_tov = 0;
5855         sp->cmn.w2.r_a_tov = 0;
5856         sp->cls1.classValid = 0;
5857         sp->cls2.seqDelivery = 1;
5858         sp->cls3.seqDelivery = 1;
5859
5860         pcmd += sizeof(uint32_t); /* CSP Word 2 */
5861         pcmd += sizeof(uint32_t); /* CSP Word 3 */
5862         pcmd += sizeof(uint32_t); /* CSP Word 4 */
5863         pcmd += sizeof(uint32_t); /* Port Name */
5864         memcpy(pcmd, &vport->fc_portname, 8);
5865         pcmd += sizeof(uint32_t); /* Node Name */
5866         pcmd += sizeof(uint32_t); /* Node Name */
5867         memcpy(pcmd, &vport->fc_nodename, 8);
5868
5869         lpfc_set_disctmo(vport);
5870
5871         phba->fc_stat.elsXmitFDISC++;
5872         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
5873
5874         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5875                 "Issue FDISC:     did:x%x",
5876                 did, 0, 0);
5877
5878         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
5879         if (rc == IOCB_ERROR) {
5880                 lpfc_els_free_iocb(phba, elsiocb);
5881                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5882                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5883                                  "0256 Issue FDISC: Cannot send IOCB\n");
5884                 return 1;
5885         }
5886         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
5887         vport->port_state = LPFC_FDISC;
5888         return 0;
5889 }
5890
5891 /**
5892  * lpfc_cmpl_els_npiv_logo: Completion function with vport logo.
5893  * @phba: pointer to lpfc hba data structure.
5894  * @cmdiocb: pointer to lpfc command iocb data structure.
5895  * @rspiocb: pointer to lpfc response iocb data structure.
5896  *
5897  * This routine is the completion callback function to the issuing of a LOGO
5898  * ELS command off a vport. It frees the command IOCB and then decrement the
5899  * reference count held on ndlp for this completion function, indicating that
5900  * the reference to the ndlp is no long needed. Note that the
5901  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
5902  * callback function and an additional explicit ndlp reference decrementation
5903  * will trigger the actual release of the ndlp.
5904  **/
5905 static void
5906 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5907                         struct lpfc_iocbq *rspiocb)
5908 {
5909         struct lpfc_vport *vport = cmdiocb->vport;
5910         IOCB_t *irsp;
5911         struct lpfc_nodelist *ndlp;
5912         ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
5913
5914         irsp = &rspiocb->iocb;
5915         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5916                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
5917                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
5918
5919         lpfc_els_free_iocb(phba, cmdiocb);
5920         vport->unreg_vpi_cmpl = VPORT_ERROR;
5921
5922         /* Trigger the release of the ndlp after logo */
5923         lpfc_nlp_put(ndlp);
5924 }
5925
5926 /**
5927  * lpfc_issue_els_npiv_logo: Issue a logo off a vport.
5928  * @vport: pointer to a virtual N_Port data structure.
5929  * @ndlp: pointer to a node-list data structure.
5930  *
5931  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
5932  *
5933  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5934  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5935  * will be stored into the context1 field of the IOCB for the completion
5936  * callback function to the LOGO ELS command.
5937  *
5938  * Return codes
5939  *   0 - Successfully issued logo off the @vport
5940  *   1 - Failed to issue logo off the @vport
5941  **/
5942 int
5943 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5944 {
5945         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5946         struct lpfc_hba  *phba = vport->phba;
5947         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5948         IOCB_t *icmd;
5949         struct lpfc_iocbq *elsiocb;
5950         uint8_t *pcmd;
5951         uint16_t cmdsize;
5952
5953         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
5954         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
5955                                      ELS_CMD_LOGO);
5956         if (!elsiocb)
5957                 return 1;
5958
5959         icmd = &elsiocb->iocb;
5960         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5961         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
5962         pcmd += sizeof(uint32_t);
5963
5964         /* Fill in LOGO payload */
5965         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
5966         pcmd += sizeof(uint32_t);
5967         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
5968
5969         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5970                 "Issue LOGO npiv  did:x%x flg:x%x",
5971                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
5972
5973         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
5974         spin_lock_irq(shost->host_lock);
5975         ndlp->nlp_flag |= NLP_LOGO_SND;
5976         spin_unlock_irq(shost->host_lock);
5977         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
5978                 spin_lock_irq(shost->host_lock);
5979                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
5980                 spin_unlock_irq(shost->host_lock);
5981                 lpfc_els_free_iocb(phba, elsiocb);
5982                 return 1;
5983         }
5984         return 0;
5985 }
5986
5987 /**
5988  * lpfc_fabric_block_timeout: Handler function to the fabric block timer.
5989  * @ptr: holder for the timer function associated data.
5990  *
5991  * This routine is invoked by the fabric iocb block timer after
5992  * timeout. It posts the fabric iocb block timeout event by setting the
5993  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
5994  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
5995  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
5996  * posted event WORKER_FABRIC_BLOCK_TMO.
5997  **/
5998 void
5999 lpfc_fabric_block_timeout(unsigned long ptr)
6000 {
6001         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
6002         unsigned long iflags;
6003         uint32_t tmo_posted;
6004
6005         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
6006         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
6007         if (!tmo_posted)
6008                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
6009         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
6010
6011         if (!tmo_posted)
6012                 lpfc_worker_wake_up(phba);
6013         return;
6014 }
6015
6016 /**
6017  * lpfc_resume_fabric_iocbs: Issue a fabric iocb from driver internal list.
6018  * @phba: pointer to lpfc hba data structure.
6019  *
6020  * This routine issues one fabric iocb from the driver internal list to
6021  * the HBA. It first checks whether it's ready to issue one fabric iocb to
6022  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
6023  * remove one pending fabric iocb from the driver internal list and invokes
6024  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
6025  **/
6026 static void
6027 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
6028 {
6029         struct lpfc_iocbq *iocb;
6030         unsigned long iflags;
6031         int ret;
6032         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6033         IOCB_t *cmd;
6034
6035 repeat:
6036         iocb = NULL;
6037         spin_lock_irqsave(&phba->hbalock, iflags);
6038         /* Post any pending iocb to the SLI layer */
6039         if (atomic_read(&phba->fabric_iocb_count) == 0) {
6040                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
6041                                  list);
6042                 if (iocb)
6043                         /* Increment fabric iocb count to hold the position */
6044                         atomic_inc(&phba->fabric_iocb_count);
6045         }
6046         spin_unlock_irqrestore(&phba->hbalock, iflags);
6047         if (iocb) {
6048                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6049                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6050                 iocb->iocb_flag |= LPFC_IO_FABRIC;
6051
6052                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6053                         "Fabric sched1:   ste:x%x",
6054                         iocb->vport->port_state, 0, 0);
6055
6056                 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
6057
6058                 if (ret == IOCB_ERROR) {
6059                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6060                         iocb->fabric_iocb_cmpl = NULL;
6061                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6062                         cmd = &iocb->iocb;
6063                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6064                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6065                         iocb->iocb_cmpl(phba, iocb, iocb);
6066
6067                         atomic_dec(&phba->fabric_iocb_count);
6068                         goto repeat;
6069                 }
6070         }
6071
6072         return;
6073 }
6074
6075 /**
6076  * lpfc_unblock_fabric_iocbs: Unblock issuing fabric iocb command.
6077  * @phba: pointer to lpfc hba data structure.
6078  *
6079  * This routine unblocks the  issuing fabric iocb command. The function
6080  * will clear the fabric iocb block bit and then invoke the routine
6081  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
6082  * from the driver internal fabric iocb list.
6083  **/
6084 void
6085 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
6086 {
6087         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6088
6089         lpfc_resume_fabric_iocbs(phba);
6090         return;
6091 }
6092
6093 /**
6094  * lpfc_block_fabric_iocbs: Block issuing fabric iocb command.
6095  * @phba: pointer to lpfc hba data structure.
6096  *
6097  * This routine blocks the issuing fabric iocb for a specified amount of
6098  * time (currently 100 ms). This is done by set the fabric iocb block bit
6099  * and set up a timeout timer for 100ms. When the block bit is set, no more
6100  * fabric iocb will be issued out of the HBA.
6101  **/
6102 static void
6103 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
6104 {
6105         int blocked;
6106
6107         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6108         /* Start a timer to unblock fabric iocbs after 100ms */
6109         if (!blocked)
6110                 mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
6111
6112         return;
6113 }
6114
6115 /**
6116  * lpfc_cmpl_fabric_iocb: Completion callback function for fabric iocb.
6117  * @phba: pointer to lpfc hba data structure.
6118  * @cmdiocb: pointer to lpfc command iocb data structure.
6119  * @rspiocb: pointer to lpfc response iocb data structure.
6120  *
6121  * This routine is the callback function that is put to the fabric iocb's
6122  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
6123  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
6124  * function first restores and invokes the original iocb's callback function
6125  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
6126  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
6127  **/
6128 static void
6129 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6130         struct lpfc_iocbq *rspiocb)
6131 {
6132         struct ls_rjt stat;
6133
6134         if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
6135                 BUG();
6136
6137         switch (rspiocb->iocb.ulpStatus) {
6138                 case IOSTAT_NPORT_RJT:
6139                 case IOSTAT_FABRIC_RJT:
6140                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
6141                                 lpfc_block_fabric_iocbs(phba);
6142                         }
6143                         break;
6144
6145                 case IOSTAT_NPORT_BSY:
6146                 case IOSTAT_FABRIC_BSY:
6147                         lpfc_block_fabric_iocbs(phba);
6148                         break;
6149
6150                 case IOSTAT_LS_RJT:
6151                         stat.un.lsRjtError =
6152                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
6153                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
6154                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
6155                                 lpfc_block_fabric_iocbs(phba);
6156                         break;
6157         }
6158
6159         if (atomic_read(&phba->fabric_iocb_count) == 0)
6160                 BUG();
6161
6162         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
6163         cmdiocb->fabric_iocb_cmpl = NULL;
6164         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
6165         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
6166
6167         atomic_dec(&phba->fabric_iocb_count);
6168         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
6169                 /* Post any pending iocbs to HBA */
6170                 lpfc_resume_fabric_iocbs(phba);
6171         }
6172 }
6173
6174 /**
6175  * lpfc_issue_fabric_iocb: Issue a fabric iocb command.
6176  * @phba: pointer to lpfc hba data structure.
6177  * @iocb: pointer to lpfc command iocb data structure.
6178  *
6179  * This routine is used as the top-level API for issuing a fabric iocb command
6180  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
6181  * function makes sure that only one fabric bound iocb will be outstanding at
6182  * any given time. As such, this function will first check to see whether there
6183  * is already an outstanding fabric iocb on the wire. If so, it will put the
6184  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
6185  * issued later. Otherwise, it will issue the iocb on the wire and update the
6186  * fabric iocb count it indicate that there is one fabric iocb on the wire.
6187  *
6188  * Note, this implementation has a potential sending out fabric IOCBs out of
6189  * order. The problem is caused by the construction of the "ready" boolen does
6190  * not include the condition that the internal fabric IOCB list is empty. As
6191  * such, it is possible a fabric IOCB issued by this routine might be "jump"
6192  * ahead of the fabric IOCBs in the internal list.
6193  *
6194  * Return code
6195  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
6196  *   IOCB_ERROR - failed to issue fabric iocb
6197  **/
6198 static int
6199 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
6200 {
6201         unsigned long iflags;
6202         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6203         int ready;
6204         int ret;
6205
6206         if (atomic_read(&phba->fabric_iocb_count) > 1)
6207                 BUG();
6208
6209         spin_lock_irqsave(&phba->hbalock, iflags);
6210         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
6211                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6212
6213         if (ready)
6214                 /* Increment fabric iocb count to hold the position */
6215                 atomic_inc(&phba->fabric_iocb_count);
6216         spin_unlock_irqrestore(&phba->hbalock, iflags);
6217         if (ready) {
6218                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6219                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6220                 iocb->iocb_flag |= LPFC_IO_FABRIC;
6221
6222                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6223                         "Fabric sched2:   ste:x%x",
6224                         iocb->vport->port_state, 0, 0);
6225
6226                 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
6227
6228                 if (ret == IOCB_ERROR) {
6229                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6230                         iocb->fabric_iocb_cmpl = NULL;
6231                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6232                         atomic_dec(&phba->fabric_iocb_count);
6233                 }
6234         } else {
6235                 spin_lock_irqsave(&phba->hbalock, iflags);
6236                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
6237                 spin_unlock_irqrestore(&phba->hbalock, iflags);
6238                 ret = IOCB_SUCCESS;
6239         }
6240         return ret;
6241 }
6242
6243 /**
6244  * lpfc_fabric_abort_vport: Abort a vport's iocbs from driver fabric iocb list.
6245  * @vport: pointer to a virtual N_Port data structure.
6246  *
6247  * This routine aborts all the IOCBs associated with a @vport from the
6248  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6249  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6250  * list, removes each IOCB associated with the @vport off the list, set the
6251  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6252  * associated with the IOCB.
6253  **/
6254 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
6255 {
6256         LIST_HEAD(completions);
6257         struct lpfc_hba  *phba = vport->phba;
6258         struct lpfc_iocbq *tmp_iocb, *piocb;
6259         IOCB_t *cmd;
6260
6261         spin_lock_irq(&phba->hbalock);
6262         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6263                                  list) {
6264
6265                 if (piocb->vport != vport)
6266                         continue;
6267
6268                 list_move_tail(&piocb->list, &completions);
6269         }
6270         spin_unlock_irq(&phba->hbalock);
6271
6272         while (!list_empty(&completions)) {
6273                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
6274                 list_del_init(&piocb->list);
6275
6276                 cmd = &piocb->iocb;
6277                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6278                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6279                 (piocb->iocb_cmpl) (phba, piocb, piocb);
6280         }
6281 }
6282
6283 /**
6284  * lpfc_fabric_abort_nport: Abort a ndlp's iocbs from driver fabric iocb list.
6285  * @ndlp: pointer to a node-list data structure.
6286  *
6287  * This routine aborts all the IOCBs associated with an @ndlp from the
6288  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6289  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6290  * list, removes each IOCB associated with the @ndlp off the list, set the
6291  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6292  * associated with the IOCB.
6293  **/
6294 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
6295 {
6296         LIST_HEAD(completions);
6297         struct lpfc_hba  *phba = ndlp->vport->phba;
6298         struct lpfc_iocbq *tmp_iocb, *piocb;
6299         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6300         IOCB_t *cmd;
6301
6302         spin_lock_irq(&phba->hbalock);
6303         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6304                                  list) {
6305                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
6306
6307                         list_move_tail(&piocb->list, &completions);
6308                 }
6309         }
6310         spin_unlock_irq(&phba->hbalock);
6311
6312         while (!list_empty(&completions)) {
6313                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
6314                 list_del_init(&piocb->list);
6315
6316                 cmd = &piocb->iocb;
6317                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6318                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6319                 (piocb->iocb_cmpl) (phba, piocb, piocb);
6320         }
6321 }
6322
6323 /**
6324  * lpfc_fabric_abort_hba: Abort all iocbs on driver fabric iocb list.
6325  * @phba: pointer to lpfc hba data structure.
6326  *
6327  * This routine aborts all the IOCBs currently on the driver internal
6328  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
6329  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
6330  * list, removes IOCBs off the list, set the status feild to
6331  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
6332  * the IOCB.
6333  **/
6334 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
6335 {
6336         LIST_HEAD(completions);
6337         struct lpfc_iocbq *piocb;
6338         IOCB_t *cmd;
6339
6340         spin_lock_irq(&phba->hbalock);
6341         list_splice_init(&phba->fabric_iocb_list, &completions);
6342         spin_unlock_irq(&phba->hbalock);
6343
6344         while (!list_empty(&completions)) {
6345                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
6346                 list_del_init(&piocb->list);
6347
6348                 cmd = &piocb->iocb;
6349                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6350                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6351                 (piocb->iocb_cmpl) (phba, piocb, piocb);
6352         }
6353 }