]> Pileus Git - ~andy/linux/blob - drivers/scsi/lpfc/lpfc_hbadisc.c
[SCSI] lpfc: NPIV: add NPIV support on top of SLI-3
[~andy/linux] / drivers / scsi / lpfc / lpfc_hbadisc.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2007 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/kthread.h>
25 #include <linux/interrupt.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_hw.h"
33 #include "lpfc_disc.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_scsi.h"
36 #include "lpfc.h"
37 #include "lpfc_logmsg.h"
38 #include "lpfc_crtn.h"
39 #include "lpfc_vport.h"
40
41 /* AlpaArray for assignment of scsid for scan-down and bind_method */
42 static uint8_t lpfcAlpaArray[] = {
43         0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
44         0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
45         0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
46         0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
47         0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
48         0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
49         0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
50         0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
51         0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
52         0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
53         0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
54         0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
55         0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
56 };
57
58 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
59
60 void
61 lpfc_terminate_rport_io(struct fc_rport *rport)
62 {
63         struct lpfc_rport_data *rdata;
64         struct lpfc_nodelist * ndlp;
65         struct lpfc_hba *phba;
66
67         rdata = rport->dd_data;
68         ndlp = rdata->pnode;
69
70         if (!ndlp) {
71                 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
72                         printk(KERN_ERR "Cannot find remote node"
73                         " to terminate I/O Data x%x\n",
74                         rport->port_id);
75                 return;
76         }
77
78         phba  = ndlp->vport->phba;
79
80         if (ndlp->nlp_sid != NLP_NO_SID) {
81                 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
82                         ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
83         }
84
85         return;
86 }
87
88 /*
89  * This function will be called when dev_loss_tmo fire.
90  */
91 void
92 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
93 {
94         struct lpfc_rport_data *rdata;
95         struct lpfc_nodelist * ndlp;
96         uint8_t *name;
97         int warn_on = 0;
98         struct lpfc_hba *phba;
99         struct lpfc_vport *vport;
100         int  put_node;
101         int  put_rport;
102
103         rdata = rport->dd_data;
104         ndlp = rdata->pnode;
105
106         if (!ndlp) {
107                 if (rport->scsi_target_id != -1) {
108                         printk(KERN_ERR "Cannot find remote node"
109                                 " for rport in dev_loss_tmo_callbk x%x\n",
110                                 rport->port_id);
111                 }
112                 return;
113         }
114
115         if (ndlp->nlp_type & NLP_FABRIC) {
116                 /* We will clean up these Nodes in linkup */
117                 put_node = rdata->pnode != NULL;
118                 put_rport = ndlp->rport != NULL;
119                 rdata->pnode = NULL;
120                 ndlp->rport = NULL;
121                 if (put_node)
122                         lpfc_nlp_put(ndlp);
123                 if (put_rport)
124                         put_device(&rport->dev);
125                 return;
126         }
127
128         name = (uint8_t *)&ndlp->nlp_portname;
129         vport = ndlp->vport;
130         phba  = vport->phba;
131
132         if (!(vport->load_flag & FC_UNLOADING) &&
133             ndlp->nlp_state == NLP_STE_MAPPED_NODE)
134                 return;
135
136
137         if (ndlp->nlp_sid != NLP_NO_SID) {
138                 warn_on = 1;
139                 /* flush the target */
140                 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
141                                     ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
142         }
143         if (vport->load_flag & FC_UNLOADING)
144                 warn_on = 0;
145
146         if (warn_on) {
147                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
148                                 "%d (%d):0203 Devloss timeout on "
149                                 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
150                                 "NPort x%x Data: x%x x%x x%x\n",
151                                 phba->brd_no, vport->vpi,
152                                 *name, *(name+1), *(name+2), *(name+3),
153                                 *(name+4), *(name+5), *(name+6), *(name+7),
154                                 ndlp->nlp_DID, ndlp->nlp_flag,
155                                 ndlp->nlp_state, ndlp->nlp_rpi);
156         } else {
157                 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
158                                 "%d (%d):0204 Devloss timeout on "
159                                 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
160                                 "NPort x%x Data: x%x x%x x%x\n",
161                                 phba->brd_no, vport->vpi,
162                                 *name, *(name+1), *(name+2), *(name+3),
163                                 *(name+4), *(name+5), *(name+6), *(name+7),
164                                 ndlp->nlp_DID, ndlp->nlp_flag,
165                                 ndlp->nlp_state, ndlp->nlp_rpi);
166         }
167
168         if (!(vport->load_flag & FC_UNLOADING) &&
169             !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
170             !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
171             (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
172                 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
173         else {
174                 put_node = rdata->pnode != NULL;
175                 put_rport = ndlp->rport != NULL;
176                 rdata->pnode = NULL;
177                 ndlp->rport = NULL;
178                 if (put_node)
179                         lpfc_nlp_put(ndlp);
180                 if (put_rport)
181                         put_device(&rport->dev);
182         }
183         return;
184 }
185
186
187 void
188 lpfc_worker_wake_up(struct lpfc_hba *phba)
189 {
190         wake_up(phba->work_wait);
191         return;
192 }
193
194 static void
195 lpfc_work_list_done(struct lpfc_hba *phba)
196 {
197         struct lpfc_work_evt  *evtp = NULL;
198         struct lpfc_nodelist  *ndlp;
199         struct lpfc_vport     *vport;
200         int free_evt;
201
202         spin_lock_irq(&phba->hbalock);
203         while (!list_empty(&phba->work_list)) {
204                 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
205                                  evt_listp);
206                 spin_unlock_irq(&phba->hbalock);
207                 free_evt = 1;
208                 switch (evtp->evt) {
209                 case LPFC_EVT_DEV_LOSS:
210                         free_evt = 0; /* evt is part of ndlp */
211                         ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
212                         vport = ndlp->vport;
213                         if (!vport)
214                                 break;
215                         if (!(vport->load_flag & FC_UNLOADING) &&
216                             !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
217                             !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
218                                 lpfc_disc_state_machine(vport, ndlp, NULL,
219                                         NLP_EVT_DEVICE_RM);
220                         }
221                         break;
222                 case LPFC_EVT_ELS_RETRY:
223                         ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
224                         lpfc_els_retry_delay_handler(ndlp);
225                         free_evt = 0; /* evt is part of ndlp */
226                         break;
227                 case LPFC_EVT_ONLINE:
228                         if (phba->link_state < LPFC_LINK_DOWN)
229                                 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
230                         else
231                                 *(int *) (evtp->evt_arg1) = 0;
232                         complete((struct completion *)(evtp->evt_arg2));
233                         break;
234                 case LPFC_EVT_OFFLINE_PREP:
235                         if (phba->link_state >= LPFC_LINK_DOWN)
236                                 lpfc_offline_prep(phba);
237                         *(int *)(evtp->evt_arg1) = 0;
238                         complete((struct completion *)(evtp->evt_arg2));
239                         break;
240                 case LPFC_EVT_OFFLINE:
241                         lpfc_offline(phba);
242                         lpfc_sli_brdrestart(phba);
243                         *(int *)(evtp->evt_arg1) =
244                                 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
245                         lpfc_unblock_mgmt_io(phba);
246                         complete((struct completion *)(evtp->evt_arg2));
247                         break;
248                 case LPFC_EVT_WARM_START:
249                         lpfc_offline(phba);
250                         lpfc_reset_barrier(phba);
251                         lpfc_sli_brdreset(phba);
252                         lpfc_hba_down_post(phba);
253                         *(int *)(evtp->evt_arg1) =
254                                 lpfc_sli_brdready(phba, HS_MBRDY);
255                         lpfc_unblock_mgmt_io(phba);
256                         complete((struct completion *)(evtp->evt_arg2));
257                         break;
258                 case LPFC_EVT_KILL:
259                         lpfc_offline(phba);
260                         *(int *)(evtp->evt_arg1)
261                                 = (phba->pport->stopped)
262                                         ? 0 : lpfc_sli_brdkill(phba);
263                         lpfc_unblock_mgmt_io(phba);
264                         complete((struct completion *)(evtp->evt_arg2));
265                         break;
266                 }
267                 if (free_evt)
268                         kfree(evtp);
269                 spin_lock_irq(&phba->hbalock);
270         }
271         spin_unlock_irq(&phba->hbalock);
272
273 }
274
275 static void
276 lpfc_work_done(struct lpfc_hba *phba)
277 {
278         struct lpfc_sli_ring *pring;
279         uint32_t ha_copy, control, work_port_events;
280         struct lpfc_vport *vport;
281         int i;
282
283         spin_lock_irq(&phba->hbalock);
284         ha_copy = phba->work_ha;
285         phba->work_ha = 0;
286         spin_unlock_irq(&phba->hbalock);
287
288         if (ha_copy & HA_ERATT)
289                 lpfc_handle_eratt(phba);
290
291         if (ha_copy & HA_MBATT)
292                 lpfc_sli_handle_mb_event(phba);
293
294         if (ha_copy & HA_LATT)
295                 lpfc_handle_latt(phba);
296
297         spin_lock_irq(&phba->hbalock);
298         list_for_each_entry(vport, &phba->port_list, listentry) {
299                 struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
300
301                 if (!scsi_host_get(shost)) {
302                         continue;
303                 }
304                 spin_unlock_irq(&phba->hbalock);
305                 work_port_events = vport->work_port_events;
306
307                 if (work_port_events & WORKER_DISC_TMO)
308                         lpfc_disc_timeout_handler(vport);
309
310                 if (work_port_events & WORKER_ELS_TMO)
311                         lpfc_els_timeout_handler(vport);
312
313                 if (work_port_events & WORKER_MBOX_TMO)
314                         lpfc_mbox_timeout_handler(phba);
315
316                 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
317                         lpfc_unblock_fabric_iocbs(phba);
318
319                 if (work_port_events & WORKER_FDMI_TMO)
320                         lpfc_fdmi_timeout_handler(vport);
321
322                 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
323                         lpfc_ramp_down_queue_handler(phba);
324
325                 if (work_port_events & WORKER_RAMP_UP_QUEUE)
326                         lpfc_ramp_up_queue_handler(phba);
327
328                 spin_lock_irq(&vport->work_port_lock);
329                 vport->work_port_events &= ~work_port_events;
330                 spin_unlock_irq(&vport->work_port_lock);
331                 scsi_host_put(shost);
332                 spin_lock_irq(&phba->hbalock);
333         }
334         spin_unlock_irq(&phba->hbalock);
335
336         for (i = 0; i < phba->sli.num_rings; i++, ha_copy >>= 4) {
337                 pring = &phba->sli.ring[i];
338                 if ((ha_copy & HA_RXATT)
339                     || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
340                         if (pring->flag & LPFC_STOP_IOCB_MASK) {
341                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
342                         } else {
343                                 lpfc_sli_handle_slow_ring_event(phba, pring,
344                                                                 (ha_copy &
345                                                                  HA_RXMASK));
346                                 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
347                         }
348                         /*
349                          * Turn on Ring interrupts
350                          */
351                         spin_lock_irq(&phba->hbalock);
352                         control = readl(phba->HCregaddr);
353                         control |= (HC_R0INT_ENA << i);
354                         writel(control, phba->HCregaddr);
355                         readl(phba->HCregaddr); /* flush */
356                         spin_unlock_irq(&phba->hbalock);
357                 }
358         }
359
360         lpfc_work_list_done(phba);
361 }
362
363 static int
364 check_work_wait_done(struct lpfc_hba *phba)
365 {
366         struct lpfc_vport *vport;
367         struct lpfc_sli_ring *pring;
368         int i, rc = 0;
369
370         spin_lock_irq(&phba->hbalock);
371         list_for_each_entry(vport, &phba->port_list, listentry) {
372                 if (vport->work_port_events) {
373                         rc = 1;
374                         goto exit;
375                 }
376         }
377
378         if (phba->work_ha || (!list_empty(&phba->work_list)) ||
379             kthread_should_stop()) {
380                 rc = 1;
381                 goto exit;
382         }
383         for (i = 0; i < phba->sli.num_rings; i++) {
384                 pring = &phba->sli.ring[i];
385                 if (pring->flag & LPFC_DEFERRED_RING_EVENT) {
386                         rc = 1;
387                         goto exit;
388                 }
389         }
390 exit:
391         if (rc)
392                 phba->work_found++;
393         else
394                 phba->work_found = 0;
395
396         spin_unlock_irq(&phba->hbalock);
397         return rc;
398 }
399
400
401 int
402 lpfc_do_work(void *p)
403 {
404         struct lpfc_hba *phba = p;
405         int rc;
406         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
407
408         set_user_nice(current, -20);
409         phba->work_wait = &work_waitq;
410         phba->work_found = 0;
411
412         while (1) {
413
414                 rc = wait_event_interruptible(work_waitq,
415                                               check_work_wait_done(phba));
416
417                 BUG_ON(rc);
418
419                 if (kthread_should_stop())
420                         break;
421
422                 lpfc_work_done(phba);
423
424                 /* If there is alot of slow ring work, like during link up
425                  * check_work_wait_done() may cause this thread to not give
426                  * up the CPU for very long periods of time. This may cause
427                  * soft lockups or other problems. To avoid these situations
428                  * give up the CPU here after LPFC_MAX_WORKER_ITERATION
429                  * consecutive iterations.
430                  */
431                 if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
432                         phba->work_found = 0;
433                         schedule();
434                 }
435         }
436         phba->work_wait = NULL;
437         return 0;
438 }
439
440 /*
441  * This is only called to handle FC worker events. Since this a rare
442  * occurance, we allocate a struct lpfc_work_evt structure here instead of
443  * embedding it in the IOCB.
444  */
445 int
446 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
447                       uint32_t evt)
448 {
449         struct lpfc_work_evt  *evtp;
450         unsigned long flags;
451
452         /*
453          * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
454          * be queued to worker thread for processing
455          */
456         evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
457         if (!evtp)
458                 return 0;
459
460         evtp->evt_arg1  = arg1;
461         evtp->evt_arg2  = arg2;
462         evtp->evt       = evt;
463
464         spin_lock_irqsave(&phba->hbalock, flags);
465         list_add_tail(&evtp->evt_listp, &phba->work_list);
466         if (phba->work_wait)
467                 lpfc_worker_wake_up(phba);
468         spin_unlock_irqrestore(&phba->hbalock, flags);
469
470         return 1;
471 }
472
473 void
474 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
475 {
476         struct lpfc_hba  *phba = vport->phba;
477         struct lpfc_nodelist *ndlp, *next_ndlp;
478         int  rc;
479
480         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
481                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
482                         continue;
483
484                 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN)
485                         lpfc_unreg_rpi(vport, ndlp);
486
487                 /* Leave Fabric nodes alone on link down */
488                 if (!remove && ndlp->nlp_type & NLP_FABRIC)
489                         continue;
490                 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
491                                              remove
492                                              ? NLP_EVT_DEVICE_RM
493                                              : NLP_EVT_DEVICE_RECOVERY);
494         }
495         if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
496                 lpfc_mbx_unreg_vpi(vport);
497                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
498         }
499 }
500
501 static void
502 lpfc_linkdown_port(struct lpfc_vport *vport)
503 {
504         struct lpfc_nodelist *ndlp, *next_ndlp;
505         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
506
507         fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
508
509         /* Cleanup any outstanding RSCN activity */
510         lpfc_els_flush_rscn(vport);
511
512         /* Cleanup any outstanding ELS commands */
513         lpfc_els_flush_cmd(vport);
514
515         lpfc_cleanup_rpis(vport, 0);
516
517         /* free any ndlp's on unused list */
518         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
519                                 /* free any ndlp's in unused state */
520                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
521                         lpfc_drop_node(vport, ndlp);
522
523         /* Turn off discovery timer if its running */
524         lpfc_can_disctmo(vport);
525 }
526
527 int
528 lpfc_linkdown(struct lpfc_hba *phba)
529 {
530         struct lpfc_vport *vport = phba->pport;
531         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
532         struct lpfc_vport *port_iterator;
533         LPFC_MBOXQ_t          *mb;
534
535         if (phba->link_state == LPFC_LINK_DOWN) {
536                 return 0;
537         }
538         spin_lock_irq(&phba->hbalock);
539         if (phba->link_state > LPFC_LINK_DOWN) {
540                 phba->link_state = LPFC_LINK_DOWN;
541                 phba->pport->fc_flag &= ~FC_LBIT;
542         }
543         spin_unlock_irq(&phba->hbalock);
544
545         list_for_each_entry(port_iterator, &phba->port_list, listentry) {
546
547                                 /* Issue a LINK DOWN event to all nodes */
548                 lpfc_linkdown_port(port_iterator);
549         }
550
551         /* Clean up any firmware default rpi's */
552         mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
553         if (mb) {
554                 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
555                 mb->vport = vport;
556                 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
557                 if (lpfc_sli_issue_mbox(phba, mb, (MBX_NOWAIT | MBX_STOP_IOCB))
558                     == MBX_NOT_FINISHED) {
559                         mempool_free(mb, phba->mbox_mem_pool);
560                 }
561         }
562
563         /* Setup myDID for link up if we are in pt2pt mode */
564         if (phba->pport->fc_flag & FC_PT2PT) {
565                 phba->pport->fc_myDID = 0;
566                 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
567                 if (mb) {
568                         lpfc_config_link(phba, mb);
569                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
570                         mb->vport = vport;
571                         if (lpfc_sli_issue_mbox(phba, mb,
572                                                 (MBX_NOWAIT | MBX_STOP_IOCB))
573                             == MBX_NOT_FINISHED) {
574                                 mempool_free(mb, phba->mbox_mem_pool);
575                         }
576                 }
577                 spin_lock_irq(shost->host_lock);
578                 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
579                 spin_unlock_irq(shost->host_lock);
580         }
581
582         return 0;
583 }
584
585 static void
586 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
587 {
588         struct lpfc_nodelist *ndlp;
589
590         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
591                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
592                         continue;
593
594                 if (ndlp->nlp_type & NLP_FABRIC) {
595                                 /* On Linkup its safe to clean up the ndlp
596                                  * from Fabric connections.
597                                  */
598                         if (ndlp->nlp_DID != Fabric_DID)
599                                 lpfc_unreg_rpi(vport, ndlp);
600                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
601                 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
602                                 /* Fail outstanding IO now since device is
603                                  * marked for PLOGI.
604                                  */
605                         lpfc_unreg_rpi(vport, ndlp);
606                 }
607         }
608 }
609
610 static void
611 lpfc_linkup_port(struct lpfc_vport *vport)
612 {
613         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
614         struct lpfc_nodelist *ndlp, *next_ndlp;
615         struct lpfc_hba  *phba = vport->phba;
616
617         if ((vport->load_flag & FC_UNLOADING) != 0)
618                 return;
619
620         /* If NPIV is not enabled, only bring the physical port up */
621         if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
622                 (vport != phba->pport))
623                 return;
624
625         fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
626
627         spin_lock_irq(shost->host_lock);
628         vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
629                             FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
630         vport->fc_flag |= FC_NDISC_ACTIVE;
631         vport->fc_ns_retry = 0;
632         spin_unlock_irq(shost->host_lock);
633
634         if (vport->fc_flag & FC_LBIT)
635                 lpfc_linkup_cleanup_nodes(vport);
636
637                                 /* free any ndlp's in unused state */
638         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
639                                  nlp_listp)
640                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
641                         lpfc_drop_node(vport, ndlp);
642 }
643
644 static int
645 lpfc_linkup(struct lpfc_hba *phba)
646 {
647         struct lpfc_vport *vport;
648
649         phba->link_state = LPFC_LINK_UP;
650
651         /* Unblock fabric iocbs if they are blocked */
652         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
653         del_timer_sync(&phba->fabric_block_timer);
654
655         list_for_each_entry(vport, &phba->port_list, listentry) {
656                 lpfc_linkup_port(vport);
657         }
658         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
659                 lpfc_issue_clear_la(phba, phba->pport);
660
661         return 0;
662 }
663
664 /*
665  * This routine handles processing a CLEAR_LA mailbox
666  * command upon completion. It is setup in the LPFC_MBOXQ
667  * as the completion routine when the command is
668  * handed off to the SLI layer.
669  */
670 void
671 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
672 {
673         struct lpfc_vport *vport = pmb->vport;
674         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
675         struct lpfc_sli   *psli = &phba->sli;
676         MAILBOX_t *mb = &pmb->mb;
677         uint32_t control;
678
679         /* Since we don't do discovery right now, turn these off here */
680         psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
681         psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
682         psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
683
684         /* Check for error */
685         if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
686                 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
687                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
688                                 "%d (%d):0320 CLEAR_LA mbxStatus error x%x hba "
689                                 "state x%x\n",
690                                 phba->brd_no, vport->vpi, mb->mbxStatus,
691                                 vport->port_state);
692
693                 phba->link_state = LPFC_HBA_ERROR;
694                 goto out;
695         }
696
697         if (vport->port_type == LPFC_PHYSICAL_PORT)
698                 phba->link_state = LPFC_HBA_READY;
699
700         spin_lock_irq(&phba->hbalock);
701         psli->sli_flag |= LPFC_PROCESS_LA;
702         control = readl(phba->HCregaddr);
703         control |= HC_LAINT_ENA;
704         writel(control, phba->HCregaddr);
705         readl(phba->HCregaddr); /* flush */
706         spin_unlock_irq(&phba->hbalock);
707         return;
708
709         vport->num_disc_nodes = 0;
710         /* go thru NPR nodes and issue ELS PLOGIs */
711         if (vport->fc_npr_cnt)
712                 lpfc_els_disc_plogi(vport);
713
714         if (!vport->num_disc_nodes) {
715                 spin_lock_irq(shost->host_lock);
716                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
717                 spin_unlock_irq(shost->host_lock);
718         }
719
720         vport->port_state = LPFC_VPORT_READY;
721
722 out:
723         /* Device Discovery completes */
724         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
725                         "%d (%d):0225 Device Discovery completes\n",
726                         phba->brd_no, vport->vpi);
727
728         mempool_free(pmb, phba->mbox_mem_pool);
729
730         spin_lock_irq(shost->host_lock);
731         vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
732         spin_unlock_irq(shost->host_lock);
733
734         del_timer_sync(&phba->fc_estabtmo);
735
736         lpfc_can_disctmo(vport);
737
738         /* turn on Link Attention interrupts */
739
740         spin_lock_irq(&phba->hbalock);
741         psli->sli_flag |= LPFC_PROCESS_LA;
742         control = readl(phba->HCregaddr);
743         control |= HC_LAINT_ENA;
744         writel(control, phba->HCregaddr);
745         readl(phba->HCregaddr); /* flush */
746         spin_unlock_irq(&phba->hbalock);
747
748         return;
749 }
750
751
752 static void
753 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
754 {
755         struct lpfc_vport *vport = pmb->vport;
756
757         if (pmb->mb.mbxStatus)
758                 goto out;
759
760         mempool_free(pmb, phba->mbox_mem_pool);
761
762         if (phba->fc_topology == TOPOLOGY_LOOP &&
763             vport->fc_flag & FC_PUBLIC_LOOP &&
764             !(vport->fc_flag & FC_LBIT)) {
765                         /* Need to wait for FAN - use discovery timer
766                          * for timeout.  port_state is identically
767                          * LPFC_LOCAL_CFG_LINK while waiting for FAN
768                          */
769                         lpfc_set_disctmo(vport);
770                         return;
771         }
772
773         /* Start discovery by sending a FLOGI. port_state is identically
774          * LPFC_FLOGI while waiting for FLOGI cmpl
775          */
776         if (vport->port_state != LPFC_FLOGI) {
777                 vport->port_state = LPFC_FLOGI;
778                 lpfc_set_disctmo(vport);
779                 lpfc_initial_flogi(vport);
780         }
781         return;
782
783 out:
784         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
785                         "%d (%d):0306 CONFIG_LINK mbxStatus error x%x "
786                         "HBA state x%x\n",
787                         phba->brd_no, vport->vpi, pmb->mb.mbxStatus,
788                         vport->port_state);
789
790         mempool_free(pmb, phba->mbox_mem_pool);
791
792         lpfc_linkdown(phba);
793
794         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
795                         "%d (%d):0200 CONFIG_LINK bad hba state x%x\n",
796                         phba->brd_no, vport->vpi, vport->port_state);
797
798         lpfc_issue_clear_la(phba, vport);
799         return;
800 }
801
802 static void
803 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
804 {
805         MAILBOX_t *mb = &pmb->mb;
806         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
807         struct lpfc_vport  *vport = pmb->vport;
808
809
810         /* Check for error */
811         if (mb->mbxStatus) {
812                 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
813                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
814                                 "%d (%d):0319 READ_SPARAM mbxStatus error x%x "
815                                 "hba state x%x>\n",
816                                 phba->brd_no, vport->vpi, mb->mbxStatus,
817                                 vport->port_state);
818
819                 lpfc_linkdown(phba);
820                 goto out;
821         }
822
823         memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
824                sizeof (struct serv_parm));
825         if (phba->cfg_soft_wwnn)
826                 u64_to_wwn(phba->cfg_soft_wwnn,
827                            vport->fc_sparam.nodeName.u.wwn);
828         if (phba->cfg_soft_wwpn)
829                 u64_to_wwn(phba->cfg_soft_wwpn,
830                            vport->fc_sparam.portName.u.wwn);
831         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
832                sizeof(vport->fc_nodename));
833         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
834                sizeof(vport->fc_portname));
835         if (vport->port_type == LPFC_PHYSICAL_PORT) {
836                 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
837                 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
838         }
839
840         lpfc_mbuf_free(phba, mp->virt, mp->phys);
841         kfree(mp);
842         mempool_free(pmb, phba->mbox_mem_pool);
843         return;
844
845 out:
846         pmb->context1 = NULL;
847         lpfc_mbuf_free(phba, mp->virt, mp->phys);
848         kfree(mp);
849         lpfc_issue_clear_la(phba, vport);
850         mempool_free(pmb, phba->mbox_mem_pool);
851         return;
852 }
853
854 static void
855 lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
856 {
857         struct lpfc_vport *vport = phba->pport;
858         LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
859         int i;
860         struct lpfc_dmabuf *mp;
861         int rc;
862
863         sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
864         cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
865
866         spin_lock_irq(&phba->hbalock);
867         switch (la->UlnkSpeed) {
868         case LA_1GHZ_LINK:
869                 phba->fc_linkspeed = LA_1GHZ_LINK;
870                 break;
871         case LA_2GHZ_LINK:
872                 phba->fc_linkspeed = LA_2GHZ_LINK;
873                 break;
874         case LA_4GHZ_LINK:
875                 phba->fc_linkspeed = LA_4GHZ_LINK;
876                 break;
877         case LA_8GHZ_LINK:
878                 phba->fc_linkspeed = LA_8GHZ_LINK;
879                 break;
880         default:
881                 phba->fc_linkspeed = LA_UNKNW_LINK;
882                 break;
883         }
884
885         phba->fc_topology = la->topology;
886         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
887
888         if (phba->fc_topology == TOPOLOGY_LOOP) {
889                 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
890
891                                 /* Get Loop Map information */
892                 if (la->il)
893                         vport->fc_flag |= FC_LBIT;
894
895                 vport->fc_myDID = la->granted_AL_PA;
896                 i = la->un.lilpBde64.tus.f.bdeSize;
897
898                 if (i == 0) {
899                         phba->alpa_map[0] = 0;
900                 } else {
901                         if (phba->cfg_log_verbose & LOG_LINK_EVENT) {
902                                 int numalpa, j, k;
903                                 union {
904                                         uint8_t pamap[16];
905                                         struct {
906                                                 uint32_t wd1;
907                                                 uint32_t wd2;
908                                                 uint32_t wd3;
909                                                 uint32_t wd4;
910                                         } pa;
911                                 } un;
912                                 numalpa = phba->alpa_map[0];
913                                 j = 0;
914                                 while (j < numalpa) {
915                                         memset(un.pamap, 0, 16);
916                                         for (k = 1; j < numalpa; k++) {
917                                                 un.pamap[k - 1] =
918                                                         phba->alpa_map[j + 1];
919                                                 j++;
920                                                 if (k == 16)
921                                                         break;
922                                         }
923                                         /* Link Up Event ALPA map */
924                                         lpfc_printf_log(phba,
925                                                         KERN_WARNING,
926                                                         LOG_LINK_EVENT,
927                                                         "%d:1304 Link Up Event "
928                                                         "ALPA map Data: x%x "
929                                                         "x%x x%x x%x\n",
930                                                         phba->brd_no,
931                                                         un.pa.wd1, un.pa.wd2,
932                                                         un.pa.wd3, un.pa.wd4);
933                                 }
934                         }
935                 }
936         } else {
937                 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
938                         if (phba->max_vpi && lpfc_npiv_enable &&
939                            (phba->sli_rev == 3))
940                                 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
941                 }
942                 vport->fc_myDID = phba->fc_pref_DID;
943                 vport->fc_flag |= FC_LBIT;
944         }
945         spin_unlock_irq(&phba->hbalock);
946
947         lpfc_linkup(phba);
948         if (sparam_mbox) {
949                 lpfc_read_sparam(phba, sparam_mbox, 0);
950                 sparam_mbox->vport = vport;
951                 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
952                 rc = lpfc_sli_issue_mbox(phba, sparam_mbox,
953                                     (MBX_NOWAIT | MBX_STOP_IOCB));
954                 if (rc == MBX_NOT_FINISHED) {
955                         mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
956                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
957                         kfree(mp);
958                         mempool_free(sparam_mbox, phba->mbox_mem_pool);
959                         if (cfglink_mbox)
960                                 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
961                         goto out;
962                 }
963         }
964
965         if (cfglink_mbox) {
966                 vport->port_state = LPFC_LOCAL_CFG_LINK;
967                 lpfc_config_link(phba, cfglink_mbox);
968                 cfglink_mbox->vport = vport;
969                 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
970                 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox,
971                                     (MBX_NOWAIT | MBX_STOP_IOCB));
972                 if (rc != MBX_NOT_FINISHED)
973                         return;
974                 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
975         }
976 out:
977         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
978         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
979                 "%d (%d):0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
980                 phba->brd_no, vport->vpi,
981                 vport->port_state, sparam_mbox, cfglink_mbox);
982
983         lpfc_issue_clear_la(phba, vport);
984         return;
985 }
986
987 static void
988 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
989 {
990         uint32_t control;
991         struct lpfc_sli *psli = &phba->sli;
992
993         lpfc_linkdown(phba);
994
995         /* turn on Link Attention interrupts - no CLEAR_LA needed */
996         spin_lock_irq(&phba->hbalock);
997         psli->sli_flag |= LPFC_PROCESS_LA;
998         control = readl(phba->HCregaddr);
999         control |= HC_LAINT_ENA;
1000         writel(control, phba->HCregaddr);
1001         readl(phba->HCregaddr); /* flush */
1002         spin_unlock_irq(&phba->hbalock);
1003 }
1004
1005 /*
1006  * This routine handles processing a READ_LA mailbox
1007  * command upon completion. It is setup in the LPFC_MBOXQ
1008  * as the completion routine when the command is
1009  * handed off to the SLI layer.
1010  */
1011 void
1012 lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1013 {
1014         struct lpfc_vport *vport = pmb->vport;
1015         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1016         READ_LA_VAR *la;
1017         MAILBOX_t *mb = &pmb->mb;
1018         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1019
1020         /* Check for error */
1021         if (mb->mbxStatus) {
1022                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1023                                 "%d:1307 READ_LA mbox error x%x state x%x\n",
1024                                 phba->brd_no, mb->mbxStatus, vport->port_state);
1025                 lpfc_mbx_issue_link_down(phba);
1026                 phba->link_state = LPFC_HBA_ERROR;
1027                 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1028         }
1029
1030         la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1031
1032         memcpy(&phba->alpa_map[0], mp->virt, 128);
1033
1034         spin_lock_irq(shost->host_lock);
1035         if (la->pb)
1036                 vport->fc_flag |= FC_BYPASSED_MODE;
1037         else
1038                 vport->fc_flag &= ~FC_BYPASSED_MODE;
1039         spin_unlock_irq(shost->host_lock);
1040
1041         if (((phba->fc_eventTag + 1) < la->eventTag) ||
1042             (phba->fc_eventTag == la->eventTag)) {
1043                 phba->fc_stat.LinkMultiEvent++;
1044                 if (la->attType == AT_LINK_UP)
1045                         if (phba->fc_eventTag != 0)
1046                                 lpfc_linkdown(phba);
1047         }
1048
1049         phba->fc_eventTag = la->eventTag;
1050
1051         if (la->attType == AT_LINK_UP) {
1052                 phba->fc_stat.LinkUp++;
1053                 if (phba->link_flag & LS_LOOPBACK_MODE) {
1054                         lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1055                                 "%d:1306 Link Up Event in loop back mode "
1056                                 "x%x received Data: x%x x%x x%x x%x\n",
1057                                 phba->brd_no, la->eventTag, phba->fc_eventTag,
1058                                 la->granted_AL_PA, la->UlnkSpeed,
1059                                 phba->alpa_map[0]);
1060                 } else {
1061                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1062                                 "%d:1303 Link Up Event x%x received "
1063                                 "Data: x%x x%x x%x x%x\n",
1064                                 phba->brd_no, la->eventTag, phba->fc_eventTag,
1065                                 la->granted_AL_PA, la->UlnkSpeed,
1066                                 phba->alpa_map[0]);
1067                 }
1068                 lpfc_mbx_process_link_up(phba, la);
1069         } else {
1070                 phba->fc_stat.LinkDown++;
1071                 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1072                                 "%d:1305 Link Down Event x%x received "
1073                                 "Data: x%x x%x x%x\n",
1074                                 phba->brd_no, la->eventTag, phba->fc_eventTag,
1075                                 phba->pport->port_state, vport->fc_flag);
1076                 lpfc_mbx_issue_link_down(phba);
1077         }
1078
1079 lpfc_mbx_cmpl_read_la_free_mbuf:
1080         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1081         kfree(mp);
1082         mempool_free(pmb, phba->mbox_mem_pool);
1083         return;
1084 }
1085
1086 /*
1087  * This routine handles processing a REG_LOGIN mailbox
1088  * command upon completion. It is setup in the LPFC_MBOXQ
1089  * as the completion routine when the command is
1090  * handed off to the SLI layer.
1091  */
1092 void
1093 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1094 {
1095         struct lpfc_vport  *vport = pmb->vport;
1096         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1097         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1098
1099         pmb->context1 = NULL;
1100
1101         /* Good status, call state machine */
1102         lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
1103         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1104         kfree(mp);
1105         mempool_free(pmb, phba->mbox_mem_pool);
1106         lpfc_nlp_put(ndlp);
1107
1108         return;
1109 }
1110
1111 static void
1112 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1113 {
1114         MAILBOX_t *mb = &pmb->mb;
1115         struct lpfc_vport *vport = pmb->vport;
1116         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1117
1118         switch (mb->mbxStatus) {
1119         case 0x0011:
1120         case 0x0020:
1121         case 0x9700:
1122                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1123                                 "%d (%d):0911 cmpl_unreg_vpi, "
1124                                 "mb status = 0x%x\n",
1125                                 phba->brd_no, vport->vpi, mb->mbxStatus);
1126                 break;
1127         default:
1128                 phba->vpi_cnt--;
1129         }
1130         vport->unreg_vpi_cmpl = VPORT_OK;
1131         mempool_free(pmb, phba->mbox_mem_pool);
1132         /*
1133          * This shost reference might have been taken at the beginning of
1134          * lpfc_vport_delete()
1135          */
1136         if (vport->load_flag & FC_UNLOADING)
1137                 scsi_host_put(shost);
1138 }
1139
1140 void
1141 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1142 {
1143         struct lpfc_hba  *phba = vport->phba;
1144         LPFC_MBOXQ_t *mbox;
1145         int rc;
1146
1147         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1148         if (!mbox)
1149                 return;
1150
1151         lpfc_unreg_vpi(phba, vport->vpi, mbox);
1152         mbox->vport = vport;
1153         mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
1154         rc = lpfc_sli_issue_mbox(phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
1155         if (rc == MBX_NOT_FINISHED) {
1156                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1157                                 "%d (%d):1800 Could not issue unreg_vpi\n",
1158                                 phba->brd_no, vport->vpi);
1159                 mempool_free(mbox, phba->mbox_mem_pool);
1160                 vport->unreg_vpi_cmpl = VPORT_ERROR;
1161         }
1162 }
1163
1164 static void
1165 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1166 {
1167         struct lpfc_vport *vport = pmb->vport;
1168         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1169         MAILBOX_t *mb = &pmb->mb;
1170
1171         switch (mb->mbxStatus) {
1172         case 0x0011:
1173         case 0x9601:
1174         case 0x9602:
1175                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1176                                 "%d (%d):0912 cmpl_reg_vpi, mb status = 0x%x\n",
1177                                 phba->brd_no, vport->vpi, mb->mbxStatus);
1178                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1179                 spin_lock_irq(shost->host_lock);
1180                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1181                 spin_unlock_irq(shost->host_lock);
1182                 vport->fc_myDID = 0;
1183                 goto out;
1184         }
1185         phba->vpi_cnt++;
1186
1187         vport->num_disc_nodes = 0;
1188         /* go thru NPR list and issue ELS PLOGIs */
1189         if (vport->fc_npr_cnt)
1190                 lpfc_els_disc_plogi(vport);
1191
1192         if (!vport->num_disc_nodes) {
1193                 spin_lock_irq(shost->host_lock);
1194                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1195                 spin_unlock_irq(shost->host_lock);
1196                 lpfc_can_disctmo(vport);
1197         }
1198         vport->port_state = LPFC_VPORT_READY;
1199
1200 out:
1201         mempool_free(pmb, phba->mbox_mem_pool);
1202         return;
1203 }
1204
1205 /*
1206  * This routine handles processing a Fabric REG_LOGIN mailbox
1207  * command upon completion. It is setup in the LPFC_MBOXQ
1208  * as the completion routine when the command is
1209  * handed off to the SLI layer.
1210  */
1211 void
1212 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1213 {
1214         struct lpfc_vport *vport = pmb->vport;
1215         struct lpfc_vport *next_vport;
1216         MAILBOX_t *mb = &pmb->mb;
1217         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1218         struct lpfc_nodelist *ndlp;
1219         ndlp = (struct lpfc_nodelist *) pmb->context2;
1220
1221         pmb->context1 = NULL;
1222         pmb->context2 = NULL;
1223
1224         if (mb->mbxStatus) {
1225                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1226                 kfree(mp);
1227                 mempool_free(pmb, phba->mbox_mem_pool);
1228                 lpfc_nlp_put(ndlp);
1229
1230                 if (phba->fc_topology == TOPOLOGY_LOOP) {
1231                         /* FLOGI failed, use loop map to make discovery list */
1232                         lpfc_disc_list_loopmap(vport);
1233
1234                         /* Start discovery */
1235                         lpfc_disc_start(vport);
1236                         return;
1237                 }
1238
1239                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1240                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1241                         "%d (%d):0258 Register Fabric login error: 0x%x\n",
1242                         phba->brd_no, vport->vpi, mb->mbxStatus);
1243
1244                 return;
1245         }
1246
1247         ndlp->nlp_rpi = mb->un.varWords[0];
1248         ndlp->nlp_type |= NLP_FABRIC;
1249         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1250
1251         lpfc_nlp_put(ndlp);     /* Drop the reference from the mbox */
1252
1253         if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1254                 list_for_each_entry(next_vport, &phba->port_list, listentry) {
1255                         if (next_vport->port_type == LPFC_PHYSICAL_PORT)
1256                                 continue;
1257
1258                         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1259                                 lpfc_initial_fdisc(next_vport);
1260                         else {
1261                                 if (phba->sli3_options &
1262                                         LPFC_SLI3_NPIV_ENABLED) {
1263                                         lpfc_vport_set_state(vport,
1264                                                 FC_VPORT_NO_FABRIC_SUPP);
1265                                         lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
1266                                                 "%d (%d):0259 No NPIV Fabric "
1267                                                 "support\n",
1268                                                 phba->brd_no, vport->vpi);
1269                                 }
1270                         }
1271                 }
1272                 lpfc_do_scr_ns_plogi(phba, vport);
1273         }
1274
1275         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1276         kfree(mp);
1277         mempool_free(pmb, phba->mbox_mem_pool);
1278         return;
1279 }
1280
1281 /*
1282  * This routine handles processing a NameServer REG_LOGIN mailbox
1283  * command upon completion. It is setup in the LPFC_MBOXQ
1284  * as the completion routine when the command is
1285  * handed off to the SLI layer.
1286  */
1287 void
1288 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1289 {
1290         MAILBOX_t *mb = &pmb->mb;
1291         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1292         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1293         struct lpfc_vport *vport = pmb->vport;
1294
1295         if (mb->mbxStatus) {
1296 out:
1297                 lpfc_nlp_put(ndlp);
1298                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1299                 kfree(mp);
1300                 mempool_free(pmb, phba->mbox_mem_pool);
1301                 lpfc_drop_node(vport, ndlp);
1302
1303                 if (phba->fc_topology == TOPOLOGY_LOOP) {
1304                         /*
1305                          * RegLogin failed, use loop map to make discovery
1306                          * list
1307                          */
1308                         lpfc_disc_list_loopmap(vport);
1309
1310                         /* Start discovery */
1311                         lpfc_disc_start(vport);
1312                         return;
1313                 }
1314                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1315                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
1316                         "%d (%d):0260 Register NameServer error: 0x%x\n",
1317                         phba->brd_no, vport->vpi, mb->mbxStatus);
1318                 return;
1319         }
1320
1321         pmb->context1 = NULL;
1322
1323         ndlp->nlp_rpi = mb->un.varWords[0];
1324         ndlp->nlp_type |= NLP_FABRIC;
1325         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1326
1327         if (vport->port_state < LPFC_VPORT_READY) {
1328                 /* Link up discovery requires Fabric registration. */
1329                 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1330                 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1331                 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1332                 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1333                 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1334
1335                 /* Issue SCR just before NameServer GID_FT Query */
1336                 lpfc_issue_els_scr(vport, SCR_DID, 0);
1337         }
1338
1339         vport->fc_ns_retry = 0;
1340         /* Good status, issue CT Request to NameServer */
1341         if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
1342                 /* Cannot issue NameServer Query, so finish up discovery */
1343                 goto out;
1344         }
1345
1346         lpfc_nlp_put(ndlp);
1347         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1348         kfree(mp);
1349         mempool_free(pmb, phba->mbox_mem_pool);
1350
1351         return;
1352 }
1353
1354 static void
1355 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1356 {
1357         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1358         struct fc_rport  *rport;
1359         struct lpfc_rport_data *rdata;
1360         struct fc_rport_identifiers rport_ids;
1361         struct lpfc_hba  *phba = vport->phba;
1362
1363         /* Remote port has reappeared. Re-register w/ FC transport */
1364         rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1365         rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
1366         rport_ids.port_id = ndlp->nlp_DID;
1367         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
1368
1369         /*
1370          * We leave our node pointer in rport->dd_data when we unregister a
1371          * FCP target port.  But fc_remote_port_add zeros the space to which
1372          * rport->dd_data points.  So, if we're reusing a previously
1373          * registered port, drop the reference that we took the last time we
1374          * registered the port.
1375          */
1376         if (ndlp->rport && ndlp->rport->dd_data &&
1377             ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp) {
1378                 lpfc_nlp_put(ndlp);
1379         }
1380         ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
1381         if (!rport || !get_device(&rport->dev)) {
1382                 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1383                            "Warning: fc_remote_port_add failed\n");
1384                 return;
1385         }
1386
1387         /* initialize static port data */
1388         rport->maxframe_size = ndlp->nlp_maxframe;
1389         rport->supported_classes = ndlp->nlp_class_sup;
1390         rdata = rport->dd_data;
1391         rdata->pnode = lpfc_nlp_get(ndlp);
1392
1393         if (ndlp->nlp_type & NLP_FCP_TARGET)
1394                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1395         if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1396                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1397         del_timer_sync(&ndlp->nlp_initiator_tmr);
1398
1399
1400         if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
1401                 fc_remote_port_rolechg(rport, rport_ids.roles);
1402
1403         if ((rport->scsi_target_id != -1) &&
1404             (rport->scsi_target_id < LPFC_MAX_TARGET)) {
1405                 ndlp->nlp_sid = rport->scsi_target_id;
1406         }
1407         return;
1408 }
1409
1410 static void
1411 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
1412 {
1413         struct fc_rport *rport = ndlp->rport;
1414
1415         fc_remote_port_delete(rport);
1416
1417         return;
1418 }
1419
1420 static void
1421 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
1422 {
1423         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1424
1425         spin_lock_irq(shost->host_lock);
1426         switch (state) {
1427         case NLP_STE_UNUSED_NODE:
1428                 vport->fc_unused_cnt += count;
1429                 break;
1430         case NLP_STE_PLOGI_ISSUE:
1431                 vport->fc_plogi_cnt += count;
1432                 break;
1433         case NLP_STE_ADISC_ISSUE:
1434                 vport->fc_adisc_cnt += count;
1435                 break;
1436         case NLP_STE_REG_LOGIN_ISSUE:
1437                 vport->fc_reglogin_cnt += count;
1438                 break;
1439         case NLP_STE_PRLI_ISSUE:
1440                 vport->fc_prli_cnt += count;
1441                 break;
1442         case NLP_STE_UNMAPPED_NODE:
1443                 vport->fc_unmap_cnt += count;
1444                 break;
1445         case NLP_STE_MAPPED_NODE:
1446                 vport->fc_map_cnt += count;
1447                 break;
1448         case NLP_STE_NPR_NODE:
1449                 vport->fc_npr_cnt += count;
1450                 break;
1451         }
1452         spin_unlock_irq(shost->host_lock);
1453 }
1454
1455 static void
1456 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1457                        int old_state, int new_state)
1458 {
1459         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1460
1461         if (new_state == NLP_STE_UNMAPPED_NODE) {
1462                 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1463                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1464                 ndlp->nlp_type |= NLP_FC_NODE;
1465         }
1466         if (new_state == NLP_STE_MAPPED_NODE)
1467                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1468         if (new_state == NLP_STE_NPR_NODE)
1469                 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
1470
1471         /* Transport interface */
1472         if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1473                             old_state == NLP_STE_UNMAPPED_NODE)) {
1474                 vport->phba->nport_event_cnt++;
1475                 lpfc_unregister_remote_port(ndlp);
1476         }
1477
1478         if (new_state ==  NLP_STE_MAPPED_NODE ||
1479             new_state == NLP_STE_UNMAPPED_NODE) {
1480                 vport->phba->nport_event_cnt++;
1481                         /*
1482                          * Tell the fc transport about the port, if we haven't
1483                          * already. If we have, and it's a scsi entity, be
1484                          * sure to unblock any attached scsi devices
1485                          */
1486                         lpfc_register_remote_port(vport, ndlp);
1487         }
1488
1489                         /*
1490                          * if we added to Mapped list, but the remote port
1491                          * registration failed or assigned a target id outside
1492                          * our presentable range - move the node to the
1493                          * Unmapped List
1494                          */
1495         if (new_state == NLP_STE_MAPPED_NODE &&
1496             (!ndlp->rport ||
1497              ndlp->rport->scsi_target_id == -1 ||
1498              ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
1499                 spin_lock_irq(shost->host_lock);
1500                 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
1501                 spin_unlock_irq(shost->host_lock);
1502                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1503         }
1504 }
1505
1506 static char *
1507 lpfc_nlp_state_name(char *buffer, size_t size, int state)
1508 {
1509         static char *states[] = {
1510                 [NLP_STE_UNUSED_NODE] = "UNUSED",
1511                 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1512                 [NLP_STE_ADISC_ISSUE] = "ADISC",
1513                 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1514                 [NLP_STE_PRLI_ISSUE] = "PRLI",
1515                 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1516                 [NLP_STE_MAPPED_NODE] = "MAPPED",
1517                 [NLP_STE_NPR_NODE] = "NPR",
1518         };
1519
1520         if (state < ARRAY_SIZE(states) && states[state])
1521                 strlcpy(buffer, states[state], size);
1522         else
1523                 snprintf(buffer, size, "unknown (%d)", state);
1524         return buffer;
1525 }
1526
1527 void
1528 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1529                    int state)
1530 {
1531         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1532         int  old_state = ndlp->nlp_state;
1533         char name1[16], name2[16];
1534
1535         lpfc_printf_log(vport->phba, KERN_INFO, LOG_NODE,
1536                         "%d:0904 NPort state transition x%06x, %s -> %s\n",
1537                         vport->phba->brd_no,
1538                         ndlp->nlp_DID,
1539                         lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1540                         lpfc_nlp_state_name(name2, sizeof(name2), state));
1541         if (old_state == NLP_STE_NPR_NODE &&
1542             (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1543             state != NLP_STE_NPR_NODE)
1544                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
1545         if (old_state == NLP_STE_UNMAPPED_NODE) {
1546                 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1547                 ndlp->nlp_type &= ~NLP_FC_NODE;
1548         }
1549
1550         if (list_empty(&ndlp->nlp_listp)) {
1551                 spin_lock_irq(shost->host_lock);
1552                 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1553                 spin_unlock_irq(shost->host_lock);
1554         } else if (old_state)
1555                 lpfc_nlp_counters(vport, old_state, -1);
1556
1557         ndlp->nlp_state = state;
1558         lpfc_nlp_counters(vport, state, 1);
1559         lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
1560 }
1561
1562 void
1563 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1564 {
1565         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1566
1567         if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
1568                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
1569         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1570                 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1571         spin_lock_irq(shost->host_lock);
1572         list_del_init(&ndlp->nlp_listp);
1573         spin_unlock_irq(shost->host_lock);
1574         lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state, 0);
1575 }
1576
1577 void
1578 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1579 {
1580         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1581
1582         if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
1583                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
1584         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1585                 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1586         spin_lock_irq(shost->host_lock);
1587         list_del_init(&ndlp->nlp_listp);
1588         spin_unlock_irq(shost->host_lock);
1589         lpfc_nlp_put(ndlp);
1590 }
1591
1592 /*
1593  * Start / ReStart rescue timer for Discovery / RSCN handling
1594  */
1595 void
1596 lpfc_set_disctmo(struct lpfc_vport *vport)
1597 {
1598         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1599         struct lpfc_hba  *phba = vport->phba;
1600         uint32_t tmo;
1601
1602         if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
1603                 /* For FAN, timeout should be greater then edtov */
1604                 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1605         } else {
1606                 /* Normal discovery timeout should be > then ELS/CT timeout
1607                  * FC spec states we need 3 * ratov for CT requests
1608                  */
1609                 tmo = ((phba->fc_ratov * 3) + 3);
1610         }
1611
1612         mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1613         spin_lock_irq(shost->host_lock);
1614         vport->fc_flag |= FC_DISC_TMO;
1615         spin_unlock_irq(shost->host_lock);
1616
1617         /* Start Discovery Timer state <hba_state> */
1618         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1619                         "%d (%d):0247 Start Discovery Timer state x%x "
1620                         "Data: x%x x%lx x%x x%x\n",
1621                         phba->brd_no, vport->vpi, vport->port_state, tmo,
1622                         (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1623                         vport->fc_adisc_cnt);
1624
1625         return;
1626 }
1627
1628 /*
1629  * Cancel rescue timer for Discovery / RSCN handling
1630  */
1631 int
1632 lpfc_can_disctmo(struct lpfc_vport *vport)
1633 {
1634         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1635         struct lpfc_hba  *phba = vport->phba;
1636         unsigned long iflags;
1637
1638         /* Turn off discovery timer if its running */
1639         if (vport->fc_flag & FC_DISC_TMO) {
1640                 spin_lock_irqsave(shost->host_lock, iflags);
1641                 vport->fc_flag &= ~FC_DISC_TMO;
1642                 spin_unlock_irqrestore(shost->host_lock, iflags);
1643                 del_timer_sync(&vport->fc_disctmo);
1644                 spin_lock_irqsave(&vport->work_port_lock, iflags);
1645                 vport->work_port_events &= ~WORKER_DISC_TMO;
1646                 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
1647         }
1648
1649         /* Cancel Discovery Timer state <hba_state> */
1650         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1651                         "%d (%d):0248 Cancel Discovery Timer state x%x "
1652                         "Data: x%x x%x x%x\n",
1653                         phba->brd_no, vport->vpi, vport->port_state,
1654                         vport->fc_flag, vport->fc_plogi_cnt,
1655                         vport->fc_adisc_cnt);
1656
1657         return 0;
1658 }
1659
1660 /*
1661  * Check specified ring for outstanding IOCB on the SLI queue
1662  * Return true if iocb matches the specified nport
1663  */
1664 int
1665 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1666                     struct lpfc_sli_ring *pring,
1667                     struct lpfc_iocbq *iocb,
1668                     struct lpfc_nodelist *ndlp)
1669 {
1670         struct lpfc_sli *psli = &phba->sli;
1671         IOCB_t *icmd = &iocb->iocb;
1672         struct lpfc_vport    *vport = ndlp->vport;
1673
1674         if (iocb->vport != vport)
1675                 return 0;
1676
1677         if (pring->ringno == LPFC_ELS_RING) {
1678                 switch (icmd->ulpCommand) {
1679                 case CMD_GEN_REQUEST64_CR:
1680                         if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
1681                                 return 1;
1682                 case CMD_ELS_REQUEST64_CR:
1683                         if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1684                                 return 1;
1685                 case CMD_XMIT_ELS_RSP64_CX:
1686                         if (iocb->context1 == (uint8_t *) ndlp)
1687                                 return 1;
1688                 }
1689         } else if (pring->ringno == psli->extra_ring) {
1690
1691         } else if (pring->ringno == psli->fcp_ring) {
1692                 /* Skip match check if waiting to relogin to FCP target */
1693                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
1694                     (ndlp->nlp_flag & NLP_DELAY_TMO)) {
1695                         return 0;
1696                 }
1697                 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
1698                         return 1;
1699                 }
1700         } else if (pring->ringno == psli->next_ring) {
1701
1702         }
1703         return 0;
1704 }
1705
1706 /*
1707  * Free resources / clean up outstanding I/Os
1708  * associated with nlp_rpi in the LPFC_NODELIST entry.
1709  */
1710 static int
1711 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1712 {
1713         LIST_HEAD(completions);
1714         struct lpfc_sli *psli;
1715         struct lpfc_sli_ring *pring;
1716         struct lpfc_iocbq *iocb, *next_iocb;
1717         IOCB_t *icmd;
1718         uint32_t rpi, i;
1719
1720         lpfc_fabric_abort_nport(ndlp);
1721
1722         /*
1723          * Everything that matches on txcmplq will be returned
1724          * by firmware with a no rpi error.
1725          */
1726         psli = &phba->sli;
1727         rpi = ndlp->nlp_rpi;
1728         if (rpi) {
1729                 /* Now process each ring */
1730                 for (i = 0; i < psli->num_rings; i++) {
1731                         pring = &psli->ring[i];
1732
1733                         spin_lock_irq(&phba->hbalock);
1734                         list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
1735                                                  list) {
1736                                 /*
1737                                  * Check to see if iocb matches the nport we are
1738                                  * looking for
1739                                  */
1740                                 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1741                                                          ndlp))) {
1742                                         /* It matches, so deque and call compl
1743                                            with an error */
1744                                         list_move_tail(&iocb->list,
1745                                                        &completions);
1746                                         pring->txq_cnt--;
1747                                 }
1748                         }
1749                         spin_unlock_irq(&phba->hbalock);
1750                 }
1751         }
1752
1753         while (!list_empty(&completions)) {
1754                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
1755                 list_del_init(&iocb->list);
1756
1757                 if (!iocb->iocb_cmpl)
1758                         lpfc_sli_release_iocbq(phba, iocb);
1759                 else {
1760                         icmd = &iocb->iocb;
1761                         icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1762                         icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1763                         (iocb->iocb_cmpl)(phba, iocb, iocb);
1764                 }
1765         }
1766
1767         return 0;
1768 }
1769
1770 /*
1771  * Free rpi associated with LPFC_NODELIST entry.
1772  * This routine is called from lpfc_freenode(), when we are removing
1773  * a LPFC_NODELIST entry. It is also called if the driver initiates a
1774  * LOGO that completes successfully, and we are waiting to PLOGI back
1775  * to the remote NPort. In addition, it is called after we receive
1776  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1777  * we are waiting to PLOGI back to the remote NPort.
1778  */
1779 int
1780 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1781 {
1782         struct lpfc_hba *phba = vport->phba;
1783         LPFC_MBOXQ_t    *mbox;
1784         int rc;
1785
1786         if (ndlp->nlp_rpi) {
1787                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1788                 if (mbox) {
1789                         lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
1790                         mbox->vport = vport;
1791                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1792                         rc = lpfc_sli_issue_mbox(phba, mbox,
1793                                                  (MBX_NOWAIT | MBX_STOP_IOCB));
1794                         if (rc == MBX_NOT_FINISHED)
1795                                 mempool_free(mbox, phba->mbox_mem_pool);
1796                 }
1797                 lpfc_no_rpi(phba, ndlp);
1798                 ndlp->nlp_rpi = 0;
1799                 return 1;
1800         }
1801         return 0;
1802 }
1803
1804 void
1805 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
1806 {
1807         struct lpfc_hba  *phba  = vport->phba;
1808         LPFC_MBOXQ_t     *mbox;
1809         int rc;
1810
1811         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1812         if (mbox) {
1813                 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
1814                 mbox->vport = vport;
1815                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1816                 rc = lpfc_sli_issue_mbox(phba, mbox,
1817                                          (MBX_NOWAIT | MBX_STOP_IOCB));
1818                 if (rc == MBX_NOT_FINISHED) {
1819                         mempool_free(mbox, phba->mbox_mem_pool);
1820                 }
1821         }
1822 }
1823
1824 void
1825 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
1826 {
1827         struct lpfc_hba  *phba  = vport->phba;
1828         LPFC_MBOXQ_t     *mbox;
1829         int rc;
1830
1831         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1832         if (mbox) {
1833                 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
1834                 mbox->vport = vport;
1835                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1836                 rc = lpfc_sli_issue_mbox(phba, mbox,
1837                                          (MBX_NOWAIT | MBX_STOP_IOCB));
1838                 if (rc == MBX_NOT_FINISHED) {
1839                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1840                                         "%d (%d):1815 Could not issue "
1841                                         "unreg_did (default rpis)\n",
1842                                         phba->brd_no, vport->vpi);
1843                         mempool_free(mbox, phba->mbox_mem_pool);
1844                 }
1845         }
1846 }
1847
1848 /*
1849  * Free resources associated with LPFC_NODELIST entry
1850  * so it can be freed.
1851  */
1852 static int
1853 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1854 {
1855         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1856         struct lpfc_hba  *phba = vport->phba;
1857         LPFC_MBOXQ_t *mb, *nextmb;
1858         struct lpfc_dmabuf *mp;
1859
1860         /* Cleanup node for NPort <nlp_DID> */
1861         lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1862                         "%d (%d):0900 Cleanup node for NPort x%x "
1863                         "Data: x%x x%x x%x\n",
1864                         phba->brd_no, vport->vpi, ndlp->nlp_DID, ndlp->nlp_flag,
1865                         ndlp->nlp_state, ndlp->nlp_rpi);
1866
1867         lpfc_dequeue_node(vport, ndlp);
1868
1869         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1870         if ((mb = phba->sli.mbox_active)) {
1871                 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1872                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1873                         mb->context2 = NULL;
1874                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1875                 }
1876         }
1877
1878         spin_lock_irq(&phba->hbalock);
1879         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1880                 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1881                     (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1882                         mp = (struct lpfc_dmabuf *) (mb->context1);
1883                         if (mp) {
1884                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
1885                                 kfree(mp);
1886                         }
1887                         list_del(&mb->list);
1888                         mempool_free(mb, phba->mbox_mem_pool);
1889                         lpfc_nlp_put(ndlp);
1890                 }
1891         }
1892         spin_unlock_irq(&phba->hbalock);
1893
1894         lpfc_els_abort(phba,ndlp);
1895         spin_lock_irq(shost->host_lock);
1896         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1897         spin_unlock_irq(shost->host_lock);
1898
1899         ndlp->nlp_last_elscmd = 0;
1900         del_timer_sync(&ndlp->nlp_delayfunc);
1901         del_timer_sync(&ndlp->nlp_initiator_tmr);
1902
1903         if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1904                 list_del_init(&ndlp->els_retry_evt.evt_listp);
1905         if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
1906                 list_del_init(&ndlp->dev_loss_evt.evt_listp);
1907
1908         lpfc_unreg_rpi(vport, ndlp);
1909
1910         return 0;
1911 }
1912
1913 /*
1914  * Check to see if we can free the nlp back to the freelist.
1915  * If we are in the middle of using the nlp in the discovery state
1916  * machine, defer the free till we reach the end of the state machine.
1917  */
1918 static void
1919 lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1920 {
1921         struct lpfc_rport_data *rdata;
1922
1923         if (ndlp->nlp_flag & NLP_DELAY_TMO) {
1924                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
1925         }
1926
1927         lpfc_cleanup_node(vport, ndlp);
1928
1929         /*
1930          * We can get here with a non-NULL ndlp->rport because when we
1931          * unregister a rport we don't break the rport/node linkage.  So if we
1932          * do, make sure we don't leaving any dangling pointers behind.
1933          */
1934         if (ndlp->rport) {
1935                 rdata = ndlp->rport->dd_data;
1936                 rdata->pnode = NULL;
1937                 ndlp->rport = NULL;
1938         }
1939 }
1940
1941 static int
1942 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1943               uint32_t did)
1944 {
1945         D_ID mydid, ndlpdid, matchdid;
1946
1947         if (did == Bcast_DID)
1948                 return 0;
1949
1950         if (ndlp->nlp_DID == 0) {
1951                 return 0;
1952         }
1953
1954         /* First check for Direct match */
1955         if (ndlp->nlp_DID == did)
1956                 return 1;
1957
1958         /* Next check for area/domain identically equals 0 match */
1959         mydid.un.word = vport->fc_myDID;
1960         if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
1961                 return 0;
1962         }
1963
1964         matchdid.un.word = did;
1965         ndlpdid.un.word = ndlp->nlp_DID;
1966         if (matchdid.un.b.id == ndlpdid.un.b.id) {
1967                 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
1968                     (mydid.un.b.area == matchdid.un.b.area)) {
1969                         if ((ndlpdid.un.b.domain == 0) &&
1970                             (ndlpdid.un.b.area == 0)) {
1971                                 if (ndlpdid.un.b.id)
1972                                         return 1;
1973                         }
1974                         return 0;
1975                 }
1976
1977                 matchdid.un.word = ndlp->nlp_DID;
1978                 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
1979                     (mydid.un.b.area == ndlpdid.un.b.area)) {
1980                         if ((matchdid.un.b.domain == 0) &&
1981                             (matchdid.un.b.area == 0)) {
1982                                 if (matchdid.un.b.id)
1983                                         return 1;
1984                         }
1985                 }
1986         }
1987         return 0;
1988 }
1989
1990 /* Search for a nodelist entry */
1991 static struct lpfc_nodelist *
1992 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
1993 {
1994         struct lpfc_hba  *phba = vport->phba;
1995         struct lpfc_nodelist *ndlp;
1996         uint32_t data1;
1997
1998         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1999                 if (lpfc_matchdid(vport, ndlp, did)) {
2000                         data1 = (((uint32_t) ndlp->nlp_state << 24) |
2001                                  ((uint32_t) ndlp->nlp_xri << 16) |
2002                                  ((uint32_t) ndlp->nlp_type << 8) |
2003                                  ((uint32_t) ndlp->nlp_rpi & 0xff));
2004                         lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
2005                                         "%d (%d):0929 FIND node DID "
2006                                         " Data: x%p x%x x%x x%x\n",
2007                                         phba->brd_no, vport->vpi,
2008                                         ndlp, ndlp->nlp_DID,
2009                                         ndlp->nlp_flag, data1);
2010                         return ndlp;
2011                 }
2012         }
2013
2014         /* FIND node did <did> NOT FOUND */
2015         lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
2016                         "%d (%d):0932 FIND node did x%x NOT FOUND.\n",
2017                         phba->brd_no, vport->vpi, did);
2018         return NULL;
2019 }
2020
2021 struct lpfc_nodelist *
2022 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
2023 {
2024         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2025         struct lpfc_nodelist *ndlp;
2026
2027         spin_lock_irq(shost->host_lock);
2028         ndlp = __lpfc_findnode_did(vport, did);
2029         spin_unlock_irq(shost->host_lock);
2030         return ndlp;
2031 }
2032
2033 struct lpfc_nodelist *
2034 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2035 {
2036         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2037         struct lpfc_nodelist *ndlp;
2038
2039         ndlp = lpfc_findnode_did(vport, did);
2040         if (!ndlp) {
2041                 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2042                     lpfc_rscn_payload_check(vport, did) == 0)
2043                         return NULL;
2044                 ndlp = (struct lpfc_nodelist *)
2045                      mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
2046                 if (!ndlp)
2047                         return NULL;
2048                 lpfc_nlp_init(vport, ndlp, did);
2049                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2050                 spin_lock_irq(shost->host_lock);
2051                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2052                 spin_unlock_irq(shost->host_lock);
2053                 return ndlp;
2054         }
2055         if (vport->fc_flag & FC_RSCN_MODE) {
2056                 if (lpfc_rscn_payload_check(vport, did)) {
2057                         spin_lock_irq(shost->host_lock);
2058                         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2059                         spin_unlock_irq(shost->host_lock);
2060
2061                         /* Since this node is marked for discovery,
2062                          * delay timeout is not needed.
2063                          */
2064                         if (ndlp->nlp_flag & NLP_DELAY_TMO)
2065                                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2066                 } else
2067                         ndlp = NULL;
2068         } else {
2069                 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
2070                     ndlp->nlp_state == NLP_STE_PLOGI_ISSUE)
2071                         return NULL;
2072                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2073                 spin_lock_irq(shost->host_lock);
2074                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2075                 spin_unlock_irq(shost->host_lock);
2076         }
2077         return ndlp;
2078 }
2079
2080 /* Build a list of nodes to discover based on the loopmap */
2081 void
2082 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
2083 {
2084         struct lpfc_hba  *phba = vport->phba;
2085         int j;
2086         uint32_t alpa, index;
2087
2088         if (!lpfc_is_link_up(phba))
2089                 return;
2090
2091         if (phba->fc_topology != TOPOLOGY_LOOP)
2092                 return;
2093
2094         /* Check for loop map present or not */
2095         if (phba->alpa_map[0]) {
2096                 for (j = 1; j <= phba->alpa_map[0]; j++) {
2097                         alpa = phba->alpa_map[j];
2098                         if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
2099                                 continue;
2100                         lpfc_setup_disc_node(vport, alpa);
2101                 }
2102         } else {
2103                 /* No alpamap, so try all alpa's */
2104                 for (j = 0; j < FC_MAXLOOP; j++) {
2105                         /* If cfg_scan_down is set, start from highest
2106                          * ALPA (0xef) to lowest (0x1).
2107                          */
2108                         if (phba->cfg_scan_down)
2109                                 index = j;
2110                         else
2111                                 index = FC_MAXLOOP - j - 1;
2112                         alpa = lpfcAlpaArray[index];
2113                         if ((vport->fc_myDID & 0xff) == alpa)
2114                                 continue;
2115                         lpfc_setup_disc_node(vport, alpa);
2116                 }
2117         }
2118         return;
2119 }
2120
2121 void
2122 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
2123 {
2124         LPFC_MBOXQ_t *mbox;
2125         struct lpfc_sli *psli = &phba->sli;
2126         struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2127         struct lpfc_sli_ring *fcp_ring   = &psli->ring[psli->fcp_ring];
2128         struct lpfc_sli_ring *next_ring  = &psli->ring[psli->next_ring];
2129         int  rc;
2130
2131         /*
2132          * if it's not a physical port or if we already send
2133          * clear_la then don't send it.
2134          */
2135         if ((phba->link_state >= LPFC_CLEAR_LA) ||
2136             (vport->port_type != LPFC_PHYSICAL_PORT))
2137                 return;
2138
2139                         /* Link up discovery */
2140         if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2141                 phba->link_state = LPFC_CLEAR_LA;
2142                 lpfc_clear_la(phba, mbox);
2143                 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2144                 mbox->vport = vport;
2145                 rc = lpfc_sli_issue_mbox(phba, mbox, (MBX_NOWAIT |
2146                                                       MBX_STOP_IOCB));
2147                 if (rc == MBX_NOT_FINISHED) {
2148                         mempool_free(mbox, phba->mbox_mem_pool);
2149                         lpfc_disc_flush_list(vport);
2150                         extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2151                         fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2152                         next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2153                         phba->link_state = LPFC_HBA_ERROR;
2154                 }
2155         }
2156 }
2157
2158 /* Reg_vpi to tell firmware to resume normal operations */
2159 void
2160 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2161 {
2162         LPFC_MBOXQ_t *regvpimbox;
2163
2164         regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2165         if (regvpimbox) {
2166                 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2167                 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2168                 regvpimbox->vport = vport;
2169                 if (lpfc_sli_issue_mbox(phba, regvpimbox,
2170                                         (MBX_NOWAIT | MBX_STOP_IOCB))
2171                                         == MBX_NOT_FINISHED) {
2172                         mempool_free(regvpimbox, phba->mbox_mem_pool);
2173                 }
2174         }
2175 }
2176
2177 /* Start Link up / RSCN discovery on NPR nodes */
2178 void
2179 lpfc_disc_start(struct lpfc_vport *vport)
2180 {
2181         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2182         struct lpfc_hba  *phba = vport->phba;
2183         uint32_t num_sent;
2184         uint32_t clear_la_pending;
2185         int did_changed;
2186
2187         if (!lpfc_is_link_up(phba))
2188                 return;
2189
2190         if (phba->link_state == LPFC_CLEAR_LA)
2191                 clear_la_pending = 1;
2192         else
2193                 clear_la_pending = 0;
2194
2195         if (vport->port_state < LPFC_VPORT_READY)
2196                 vport->port_state = LPFC_DISC_AUTH;
2197
2198         lpfc_set_disctmo(vport);
2199
2200         if (vport->fc_prevDID == vport->fc_myDID)
2201                 did_changed = 0;
2202         else
2203                 did_changed = 1;
2204
2205         vport->fc_prevDID = vport->fc_myDID;
2206         vport->num_disc_nodes = 0;
2207
2208         /* Start Discovery state <hba_state> */
2209         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
2210                         "%d (%d):0202 Start Discovery hba state x%x "
2211                         "Data: x%x x%x x%x\n",
2212                         phba->brd_no, vport->vpi, vport->port_state,
2213                         vport->fc_flag, vport->fc_plogi_cnt,
2214                         vport->fc_adisc_cnt);
2215
2216         /* First do ADISCs - if any */
2217         num_sent = lpfc_els_disc_adisc(vport);
2218
2219         if (num_sent)
2220                 return;
2221
2222         /*
2223          * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2224          * continue discovery.
2225          */
2226         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2227             !(vport->fc_flag & FC_RSCN_MODE)) {
2228                 lpfc_issue_reg_vpi(phba, vport);
2229                 return;
2230         }
2231
2232         /*
2233          * For SLI2, we need to set port_state to READY and continue
2234          * discovery.
2235          */
2236         if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2237                 /* If we get here, there is nothing to ADISC */
2238                 if (vport->port_type == LPFC_PHYSICAL_PORT)
2239                         lpfc_issue_clear_la(phba, vport);
2240
2241                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2242                         vport->num_disc_nodes = 0;
2243                         /* go thru NPR nodes and issue ELS PLOGIs */
2244                         if (vport->fc_npr_cnt)
2245                                 lpfc_els_disc_plogi(vport);
2246
2247                         if (!vport->num_disc_nodes) {
2248                                 spin_lock_irq(shost->host_lock);
2249                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2250                                 spin_unlock_irq(shost->host_lock);
2251                                 lpfc_can_disctmo(vport);
2252                         }
2253                 }
2254                 vport->port_state = LPFC_VPORT_READY;
2255         } else {
2256                 /* Next do PLOGIs - if any */
2257                 num_sent = lpfc_els_disc_plogi(vport);
2258
2259                 if (num_sent)
2260                         return;
2261
2262                 if (vport->fc_flag & FC_RSCN_MODE) {
2263                         /* Check to see if more RSCNs came in while we
2264                          * were processing this one.
2265                          */
2266                         if ((vport->fc_rscn_id_cnt == 0) &&
2267                             (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2268                                 spin_lock_irq(shost->host_lock);
2269                                 vport->fc_flag &= ~FC_RSCN_MODE;
2270                                 spin_unlock_irq(shost->host_lock);
2271                                 lpfc_can_disctmo(vport);
2272                         } else
2273                                 lpfc_els_handle_rscn(vport);
2274                 }
2275         }
2276         return;
2277 }
2278
2279 /*
2280  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2281  *  ring the match the sppecified nodelist.
2282  */
2283 static void
2284 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
2285 {
2286         LIST_HEAD(completions);
2287         struct lpfc_sli *psli;
2288         IOCB_t     *icmd;
2289         struct lpfc_iocbq    *iocb, *next_iocb;
2290         struct lpfc_sli_ring *pring;
2291
2292         psli = &phba->sli;
2293         pring = &psli->ring[LPFC_ELS_RING];
2294
2295         /* Error matching iocb on txq or txcmplq
2296          * First check the txq.
2297          */
2298         spin_lock_irq(&phba->hbalock);
2299         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2300                 if (iocb->context1 != ndlp) {
2301                         continue;
2302                 }
2303                 icmd = &iocb->iocb;
2304                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2305                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2306
2307                         list_move_tail(&iocb->list, &completions);
2308                         pring->txq_cnt--;
2309                 }
2310         }
2311
2312         /* Next check the txcmplq */
2313         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2314                 if (iocb->context1 != ndlp) {
2315                         continue;
2316                 }
2317                 icmd = &iocb->iocb;
2318                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2319                     icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
2320                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2321                 }
2322         }
2323         spin_unlock_irq(&phba->hbalock);
2324
2325         while (!list_empty(&completions)) {
2326                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
2327                 list_del_init(&iocb->list);
2328
2329                 if (!iocb->iocb_cmpl)
2330                         lpfc_sli_release_iocbq(phba, iocb);
2331                 else {
2332                         icmd = &iocb->iocb;
2333                         icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2334                         icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2335                         (iocb->iocb_cmpl) (phba, iocb, iocb);
2336                 }
2337         }
2338 }
2339
2340 void
2341 lpfc_disc_flush_list(struct lpfc_vport *vport)
2342 {
2343         struct lpfc_nodelist *ndlp, *next_ndlp;
2344         struct lpfc_hba *phba = vport->phba;
2345
2346         if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2347                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
2348                                          nlp_listp) {
2349                         if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2350                             ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2351                                 lpfc_free_tx(phba, ndlp);
2352                                 lpfc_nlp_put(ndlp);
2353                         }
2354                 }
2355         }
2356 }
2357
2358 void
2359 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2360 {
2361         lpfc_els_flush_rscn(vport);
2362         lpfc_els_flush_cmd(vport);
2363         lpfc_disc_flush_list(vport);
2364 }
2365
2366 /*****************************************************************************/
2367 /*
2368  * NAME:     lpfc_disc_timeout
2369  *
2370  * FUNCTION: Fibre Channel driver discovery timeout routine.
2371  *
2372  * EXECUTION ENVIRONMENT: interrupt only
2373  *
2374  * CALLED FROM:
2375  *      Timer function
2376  *
2377  * RETURNS:
2378  *      none
2379  */
2380 /*****************************************************************************/
2381 void
2382 lpfc_disc_timeout(unsigned long ptr)
2383 {
2384         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2385         struct lpfc_hba   *phba = vport->phba;
2386         unsigned long flags = 0;
2387
2388         if (unlikely(!phba))
2389                 return;
2390
2391         if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2392                 spin_lock_irqsave(&vport->work_port_lock, flags);
2393                 vport->work_port_events |= WORKER_DISC_TMO;
2394                 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2395
2396                 spin_lock_irqsave(&phba->hbalock, flags);
2397                 if (phba->work_wait)
2398                         lpfc_worker_wake_up(phba);
2399                 spin_unlock_irqrestore(&phba->hbalock, flags);
2400         }
2401         return;
2402 }
2403
2404 static void
2405 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
2406 {
2407         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2408         struct lpfc_hba  *phba = vport->phba;
2409         struct lpfc_sli  *psli = &phba->sli;
2410         struct lpfc_nodelist *ndlp, *next_ndlp;
2411         LPFC_MBOXQ_t *initlinkmbox;
2412         int rc, clrlaerr = 0;
2413
2414         if (!(vport->fc_flag & FC_DISC_TMO))
2415                 return;
2416
2417         spin_lock_irq(shost->host_lock);
2418         vport->fc_flag &= ~FC_DISC_TMO;
2419         spin_unlock_irq(shost->host_lock);
2420
2421         switch (vport->port_state) {
2422
2423         case LPFC_LOCAL_CFG_LINK:
2424         /* port_state is identically  LPFC_LOCAL_CFG_LINK while waiting for
2425          * FAN
2426          */
2427                                 /* FAN timeout */
2428                 lpfc_printf_log(phba, KERN_WARNING, LOG_DISCOVERY,
2429                                 "%d (%d):0221 FAN timeout\n",
2430                                 phba->brd_no, vport->vpi);
2431
2432                 /* Start discovery by sending FLOGI, clean up old rpis */
2433                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
2434                                          nlp_listp) {
2435                         if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2436                                 continue;
2437                         if (ndlp->nlp_type & NLP_FABRIC) {
2438                                 /* Clean up the ndlp on Fabric connections */
2439                                 lpfc_drop_node(vport, ndlp);
2440                         } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2441                                 /* Fail outstanding IO now since device
2442                                  * is marked for PLOGI.
2443                                  */
2444                                 lpfc_unreg_rpi(vport, ndlp);
2445                         }
2446                 }
2447                 if (vport->port_state != LPFC_FLOGI) {
2448                         vport->port_state = LPFC_FLOGI;
2449                         lpfc_set_disctmo(vport);
2450                         lpfc_initial_flogi(vport);
2451                 }
2452                 break;
2453
2454         case LPFC_FDISC:
2455         case LPFC_FLOGI:
2456         /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
2457                 /* Initial FLOGI timeout */
2458                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2459                                 "%d (%d):0222 Initial %s timeout\n",
2460                                 phba->brd_no, vport->vpi,
2461                                 vport->vpi ? "FLOGI" : "FDISC");
2462
2463                 /* Assume no Fabric and go on with discovery.
2464                  * Check for outstanding ELS FLOGI to abort.
2465                  */
2466
2467                 /* FLOGI failed, so just use loop map to make discovery list */
2468                 lpfc_disc_list_loopmap(vport);
2469
2470                 /* Start discovery */
2471                 lpfc_disc_start(vport);
2472                 break;
2473
2474         case LPFC_FABRIC_CFG_LINK:
2475         /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2476            NameServer login */
2477                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2478                                 "%d (%d):0223 Timeout while waiting for "
2479                                 "NameServer login\n",
2480                                 phba->brd_no, vport->vpi);
2481
2482                 /* Next look for NameServer ndlp */
2483                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
2484                 if (ndlp)
2485                         lpfc_nlp_put(ndlp);
2486                 /* Start discovery */
2487                 lpfc_disc_start(vport);
2488                 break;
2489
2490         case LPFC_NS_QRY:
2491         /* Check for wait for NameServer Rsp timeout */
2492                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2493                                 "%d (%d):0224 NameServer Query timeout "
2494                                 "Data: x%x x%x\n",
2495                                 phba->brd_no, vport->vpi,
2496                                 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
2497
2498                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2499                         /* Try it one more time */
2500                         vport->fc_ns_retry++;
2501                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2502                                          vport->fc_ns_retry, 0);
2503                         if (rc == 0)
2504                                 break;
2505                 }
2506                 vport->fc_ns_retry = 0;
2507
2508                 /*
2509                  * Discovery is over.
2510                  * set port_state to PORT_READY if SLI2.
2511                  * cmpl_reg_vpi will set port_state to READY for SLI3.
2512                  */
2513                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2514                         lpfc_issue_reg_vpi(phba, vport);
2515                 else  { /* NPIV Not enabled */
2516                         lpfc_issue_clear_la(phba, vport);
2517                         vport->port_state = LPFC_VPORT_READY;
2518                 }
2519
2520                 /* Setup and issue mailbox INITIALIZE LINK command */
2521                 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2522                 if (!initlinkmbox) {
2523                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2524                                         "%d (%d):0206 Device Discovery "
2525                                         "completion error\n",
2526                                         phba->brd_no, vport->vpi);
2527                         phba->link_state = LPFC_HBA_ERROR;
2528                         break;
2529                 }
2530
2531                 lpfc_linkdown(phba);
2532                 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2533                                phba->cfg_link_speed);
2534                 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
2535                 initlinkmbox->vport = vport;
2536                 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2537                 rc = lpfc_sli_issue_mbox(phba, initlinkmbox,
2538                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2539                 lpfc_set_loopback_flag(phba);
2540                 if (rc == MBX_NOT_FINISHED)
2541                         mempool_free(initlinkmbox, phba->mbox_mem_pool);
2542
2543                 break;
2544
2545         case LPFC_DISC_AUTH:
2546         /* Node Authentication timeout */
2547                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2548                                 "%d (%d):0227 Node Authentication timeout\n",
2549                                 phba->brd_no, vport->vpi);
2550                 lpfc_disc_flush_list(vport);
2551
2552                 /*
2553                  * set port_state to PORT_READY if SLI2.
2554                  * cmpl_reg_vpi will set port_state to READY for SLI3.
2555                  */
2556                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2557                         lpfc_issue_reg_vpi(phba, vport);
2558                 else {  /* NPIV Not enabled */
2559                         lpfc_issue_clear_la(phba, vport);
2560                         vport->port_state = LPFC_VPORT_READY;
2561                 }
2562                 break;
2563
2564         case LPFC_VPORT_READY:
2565                 if (vport->fc_flag & FC_RSCN_MODE) {
2566                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2567                                         "%d (%d):0231 RSCN timeout Data: x%x "
2568                                         "x%x\n",
2569                                         phba->brd_no, vport->vpi,
2570                                         vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
2571
2572                         /* Cleanup any outstanding ELS commands */
2573                         lpfc_els_flush_cmd(vport);
2574
2575                         lpfc_els_flush_rscn(vport);
2576                         lpfc_disc_flush_list(vport);
2577                 }
2578                 break;
2579
2580         default:
2581                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2582                                 "%d (%d):0229 Unexpected discovery timeout, "
2583                                 "vport State x%x\n",
2584                                 phba->brd_no, vport->vpi, vport->port_state);
2585
2586                 break;
2587         }
2588
2589         switch (phba->link_state) {
2590         case LPFC_CLEAR_LA:
2591                                 /* CLEAR LA timeout */
2592                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2593                                 "%d (%d):0228 CLEAR LA timeout\n",
2594                                 phba->brd_no, vport->vpi);
2595                 clrlaerr = 1;
2596                 break;
2597
2598         case LPFC_LINK_UNKNOWN:
2599         case LPFC_WARM_START:
2600         case LPFC_INIT_START:
2601         case LPFC_INIT_MBX_CMDS:
2602         case LPFC_LINK_DOWN:
2603         case LPFC_LINK_UP:
2604         case LPFC_HBA_ERROR:
2605                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2606                                 "%d (%d):0230 Unexpected timeout, hba link "
2607                                 "state x%x\n",
2608                                 phba->brd_no, vport->vpi, phba->link_state);
2609                 clrlaerr = 1;
2610                 break;
2611
2612         case LPFC_HBA_READY:
2613                 break;
2614         }
2615
2616         if (clrlaerr) {
2617                 lpfc_disc_flush_list(vport);
2618                 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2619                 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2620                 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2621                 vport->port_state = LPFC_VPORT_READY;
2622         }
2623
2624         return;
2625 }
2626
2627 /*
2628  * This routine handles processing a NameServer REG_LOGIN mailbox
2629  * command upon completion. It is setup in the LPFC_MBOXQ
2630  * as the completion routine when the command is
2631  * handed off to the SLI layer.
2632  */
2633 void
2634 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2635 {
2636         MAILBOX_t *mb = &pmb->mb;
2637         struct lpfc_dmabuf   *mp = (struct lpfc_dmabuf *) (pmb->context1);
2638         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2639         struct lpfc_vport    *vport = pmb->vport;
2640
2641         pmb->context1 = NULL;
2642
2643         ndlp->nlp_rpi = mb->un.varWords[0];
2644         ndlp->nlp_type |= NLP_FABRIC;
2645         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2646
2647         /*
2648          * Start issuing Fabric-Device Management Interface (FDMI) command to
2649          * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2650          * fdmi-on=2 (supporting RPA/hostnmae)
2651          */
2652
2653         if (phba->cfg_fdmi_on == 1)
2654                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2655         else
2656                 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
2657
2658                                 /* Mailbox took a reference to the node */
2659         lpfc_nlp_put(ndlp);
2660         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2661         kfree(mp);
2662         mempool_free(pmb, phba->mbox_mem_pool);
2663
2664         return;
2665 }
2666
2667 static int
2668 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2669 {
2670         uint16_t *rpi = param;
2671
2672         return ndlp->nlp_rpi == *rpi;
2673 }
2674
2675 static int
2676 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2677 {
2678         return memcmp(&ndlp->nlp_portname, param,
2679                       sizeof(ndlp->nlp_portname)) == 0;
2680 }
2681
2682 struct lpfc_nodelist *
2683 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
2684 {
2685         struct lpfc_nodelist *ndlp;
2686
2687         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2688                 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE &&
2689                     filter(ndlp, param))
2690                         return ndlp;
2691         }
2692         return NULL;
2693 }
2694
2695 /*
2696  * Search node lists for a remote port matching filter criteria
2697  * Caller needs to hold host_lock before calling this routine.
2698  */
2699 struct lpfc_nodelist *
2700 lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
2701 {
2702         struct Scsi_Host     *shost = lpfc_shost_from_vport(vport);
2703         struct lpfc_nodelist *ndlp;
2704
2705         spin_lock_irq(shost->host_lock);
2706         ndlp = __lpfc_find_node(vport, filter, param);
2707         spin_unlock_irq(shost->host_lock);
2708         return ndlp;
2709 }
2710
2711 /*
2712  * This routine looks up the ndlp lists for the given RPI. If rpi found it
2713  * returns the node list element pointer else return NULL.
2714  */
2715 struct lpfc_nodelist *
2716 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
2717 {
2718         return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
2719 }
2720
2721 struct lpfc_nodelist *
2722 lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
2723 {
2724         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2725         struct lpfc_nodelist *ndlp;
2726
2727         spin_lock_irq(shost->host_lock);
2728         ndlp = __lpfc_findnode_rpi(vport, rpi);
2729         spin_unlock_irq(shost->host_lock);
2730         return ndlp;
2731 }
2732
2733 /*
2734  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
2735  * returns the node element list pointer else return NULL.
2736  */
2737 struct lpfc_nodelist *
2738 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
2739 {
2740         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2741         struct lpfc_nodelist *ndlp;
2742
2743         spin_lock_irq(shost->host_lock);
2744         ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2745         spin_unlock_irq(shost->host_lock);
2746         return NULL;
2747 }
2748
2749 void
2750 lpfc_dev_loss_delay(unsigned long ptr)
2751 {
2752         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2753         struct lpfc_vport *vport = ndlp->vport;
2754         struct lpfc_hba   *phba = vport->phba;
2755         struct lpfc_work_evt  *evtp = &ndlp->dev_loss_evt;
2756         unsigned long flags;
2757
2758         evtp = &ndlp->dev_loss_evt;
2759
2760         spin_lock_irqsave(&phba->hbalock, flags);
2761         if (!list_empty(&evtp->evt_listp)) {
2762                 spin_unlock_irqrestore(&phba->hbalock, flags);
2763                 return;
2764         }
2765
2766         evtp->evt_arg1  = ndlp;
2767         evtp->evt       = LPFC_EVT_DEV_LOSS;
2768         list_add_tail(&evtp->evt_listp, &phba->work_list);
2769         if (phba->work_wait)
2770                 lpfc_worker_wake_up(phba);
2771         spin_unlock_irqrestore(&phba->hbalock, flags);
2772         return;
2773 }
2774
2775 void
2776 lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2777               uint32_t did)
2778 {
2779         memset(ndlp, 0, sizeof (struct lpfc_nodelist));
2780         INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2781         INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2782         init_timer(&ndlp->nlp_initiator_tmr);
2783         ndlp->nlp_initiator_tmr.function = lpfc_dev_loss_delay;
2784         ndlp->nlp_initiator_tmr.data = (unsigned long)ndlp;
2785         init_timer(&ndlp->nlp_delayfunc);
2786         ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2787         ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2788         ndlp->nlp_DID = did;
2789         ndlp->vport = vport;
2790         ndlp->nlp_sid = NLP_NO_SID;
2791         INIT_LIST_HEAD(&ndlp->nlp_listp);
2792         kref_init(&ndlp->kref);
2793         return;
2794 }
2795
2796 void
2797 lpfc_nlp_release(struct kref *kref)
2798 {
2799         struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
2800                                                   kref);
2801         lpfc_nlp_remove(ndlp->vport, ndlp);
2802         mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
2803 }
2804
2805 struct lpfc_nodelist *
2806 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
2807 {
2808         if (ndlp)
2809                 kref_get(&ndlp->kref);
2810         return ndlp;
2811 }
2812
2813 int
2814 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
2815 {
2816         return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
2817 }