This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_init.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Enterprise Fibre Channel Host Bus Adapters.                     *
4  * Refer to the README file included with this package for         *
5  * driver version and adapter support.                             *
6  * Copyright (C) 2004 Emulex Corporation.                          *
7  * www.emulex.com                                                  *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of the GNU General Public License     *
11  * as published by the Free Software Foundation; either version 2  *
12  * of the License, or (at your option) any later version.          *
13  *                                                                 *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of  *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
17  * GNU General Public License for more details, a copy of which    *
18  * can be found in the file COPYING included with this package.    *
19  *******************************************************************/
20
21 /*
22  * $Id: lpfc_init.c 1.168 2004/11/15 11:01:33EST sf_support Exp  $
23  */
24
25 #include <linux/version.h>
26 #include <linux/blkdev.h>
27 #include <linux/ctype.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/pci.h>
30 #include <linux/spinlock.h>
31
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_host.h>
34
35 #include "lpfc_sli.h"
36 #include "lpfc_disc.h"
37 #include "lpfc_scsi.h"
38 #include "lpfc.h"
39 #include "lpfc_crtn.h"
40 #include "lpfc_hw.h"
41 #include "lpfc_logmsg.h"
42 #include "lpfc_mem.h"
43 #include "lpfc_version.h"
44 #include "lpfc_compat.h"
45
46 static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *);
47 static int lpfc_post_rcv_buf(struct lpfc_hba *);
48 static int lpfc_rdrev_wd30 = 0;
49
50 /************************************************************************/
51 /*                                                                      */
52 /*    lpfc_config_port_prep                                             */
53 /*    This routine will do LPFC initialization prior to the             */
54 /*    CONFIG_PORT mailbox command. This will be initialized             */
55 /*    as a SLI layer callback routine.                                  */
56 /*    This routine returns 0 on success or -ERESTART if it wants        */
57 /*    the SLI layer to reset the HBA and try again. Any                 */
58 /*    other return value indicates an error.                            */
59 /*                                                                      */
60 /************************************************************************/
61 int
62 lpfc_config_port_prep(struct lpfc_hba * phba)
63 {
64         lpfc_vpd_t *vp = &phba->vpd;
65         int i = 0;
66         LPFC_MBOXQ_t *pmb;
67         MAILBOX_t *mb;
68
69
70         /* Get a Mailbox buffer to setup mailbox commands for HBA
71            initialization */
72         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
73         if (!pmb) {
74                 phba->hba_state = LPFC_HBA_ERROR;
75                 return -ENOMEM;
76         }
77
78         mb = &pmb->mb;
79         phba->hba_state = LPFC_INIT_MBX_CMDS;
80
81         /* special handling for LC HBAs */
82         if (lpfc_is_LC_HBA(phba->pcidev->device)) {
83                 char licensed[56] =
84                     "key unlock for use with gnu public licensed code only\0";
85                 uint32_t *ptext = (uint32_t *) licensed;
86
87                 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
88                         *ptext = cpu_to_be32(*ptext);
89
90                 /* Setup and issue mailbox READ NVPARAMS command */
91                 lpfc_read_nv(phba, pmb);
92                 memset((char*)mb->un.varRDnvp.rsvd3, 0,
93                         sizeof (mb->un.varRDnvp.rsvd3));
94                 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
95                          sizeof (licensed));
96
97                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
98                         /* Adapter initialization error, mbxCmd <cmd>
99                            READ_NVPARM, mbxStatus <status> */
100                         lpfc_printf_log(phba,
101                                         KERN_ERR,
102                                         LOG_MBOX,
103                                         "%d:0324 Config Port initialization "
104                                         "error, mbxCmd x%x READ_NVPARM, "
105                                         "mbxStatus x%x\n",
106                                         phba->brd_no,
107                                         mb->mbxCommand, mb->mbxStatus);
108                         return -ERESTART;
109                 }
110                 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
111                        sizeof (mb->un.varRDnvp.nodename));
112         }
113
114         /* Setup and issue mailbox READ REV command */
115         lpfc_read_rev(phba, pmb);
116         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
117                 /* Adapter failed to init, mbxCmd <mbxCmd> READ_REV, mbxStatus
118                    <status> */
119                 lpfc_printf_log(phba,
120                                 KERN_ERR,
121                                 LOG_INIT,
122                                 "%d:0439 Adapter failed to init, mbxCmd x%x "
123                                 "READ_REV, mbxStatus x%x\n",
124                                 phba->brd_no,
125                                 mb->mbxCommand, mb->mbxStatus);
126                 mempool_free( pmb, phba->mbox_mem_pool);
127                 return -ERESTART;
128         }
129
130         /* The HBA's current state is provided by the ProgType and rr fields.
131          * Read and check the value of these fields before continuing to config
132          * this port.
133          */
134         if (mb->un.varRdRev.rr == 0 || mb->un.varRdRev.un.b.ProgType != 2) {
135                 /* Old firmware */
136                 vp->rev.rBit = 0;
137                 /* Adapter failed to init, mbxCmd <cmd> READ_REV detected
138                    outdated firmware */
139                 lpfc_printf_log(phba,
140                                 KERN_ERR,
141                                 LOG_INIT,
142                                 "%d:0440 Adapter failed to init, mbxCmd x%x "
143                                 "READ_REV detected outdated firmware"
144                                 "Data: x%x\n",
145                                 phba->brd_no,
146                                 mb->mbxCommand, 0);
147                 mempool_free(pmb, phba->mbox_mem_pool);
148                 return -ERESTART;
149         } else {
150                 vp->rev.rBit = 1;
151                 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
152                 memcpy(vp->rev.sli1FwName,
153                         (char*)mb->un.varRdRev.sli1FwName, 16);
154                 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
155                 memcpy(vp->rev.sli2FwName,
156                         (char *)mb->un.varRdRev.sli2FwName, 16);
157         }
158
159         /* Save information as VPD data */
160         vp->rev.biuRev = mb->un.varRdRev.biuRev;
161         vp->rev.smRev = mb->un.varRdRev.smRev;
162         vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
163         vp->rev.endecRev = mb->un.varRdRev.endecRev;
164         vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
165         vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
166         vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
167         vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
168         vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
169         vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
170         lpfc_rdrev_wd30 = mb->un.varWords[30];
171
172         if (lpfc_is_LC_HBA(phba->pcidev->device))
173                 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
174                         sizeof (phba->RandomData));
175
176         /* Get adapter VPD information */
177         lpfc_dump_mem(phba, pmb);
178         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
179                 /* Let it go through even if failed. */
180                 /* Adapter failed to init, mbxCmd <cmd> DUMP VPD,
181                    mbxStatus <status> */
182                 lpfc_printf_log(phba,
183                                 KERN_INFO,
184                                 LOG_INIT,
185                                 "%d:0441 VPD not present on adapter, mbxCmd "
186                                 "x%x DUMP VPD, mbxStatus x%x\n",
187                                 phba->brd_no,
188                                 mb->mbxCommand, mb->mbxStatus);
189         } else if (mb->un.varDmp.ra == 1) {
190                 lpfc_parse_vpd(phba, (uint8_t *)&mb->un.varDmp.resp_offset);
191         }
192         mempool_free(pmb, phba->mbox_mem_pool);
193         return 0;
194 }
195
196 /************************************************************************/
197 /*                                                                      */
198 /*    lpfc_config_port_post                                             */
199 /*    This routine will do LPFC initialization after the                */
200 /*    CONFIG_PORT mailbox command. This will be initialized             */
201 /*    as a SLI layer callback routine.                                  */
202 /*    This routine returns 0 on success. Any other return value         */
203 /*    indicates an error.                                               */
204 /*                                                                      */
205 /************************************************************************/
206 int
207 lpfc_config_port_post(struct lpfc_hba * phba)
208 {
209         LPFC_MBOXQ_t *pmb;
210         MAILBOX_t *mb;
211         struct lpfc_dmabuf *mp;
212         struct lpfc_sli *psli = &phba->sli;
213         uint32_t status, timeout;
214         int i, j, flogi_sent;
215         unsigned long isr_cnt, clk_cnt;
216
217
218         /* Get a Mailbox buffer to setup mailbox commands for HBA
219            initialization */
220         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
221         if (!pmb) {
222                 phba->hba_state = LPFC_HBA_ERROR;
223                 return -ENOMEM;
224         }
225         mb = &pmb->mb;
226
227         /* Setup link timers */
228         lpfc_config_link(phba, pmb);
229         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
230                 lpfc_printf_log(phba,
231                                 KERN_ERR,
232                                 LOG_INIT,
233                                 "%d:0447 Adapter failed init, mbxCmd x%x "
234                                 "CONFIG_LINK mbxStatus x%x\n",
235                                 phba->brd_no,
236                                 mb->mbxCommand, mb->mbxStatus);
237                 phba->hba_state = LPFC_HBA_ERROR;
238                 mempool_free( pmb, phba->mbox_mem_pool);
239                 return -EIO;
240         }
241
242         /* Get login parameters for NID.  */
243         lpfc_read_sparam(phba, pmb);
244         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
245                 lpfc_printf_log(phba,
246                                 KERN_ERR,
247                                 LOG_INIT,
248                                 "%d:0448 Adapter failed init, mbxCmd x%x "
249                                 "READ_SPARM mbxStatus x%x\n",
250                                 phba->brd_no,
251                                 mb->mbxCommand, mb->mbxStatus);
252                 phba->hba_state = LPFC_HBA_ERROR;
253                 mempool_free( pmb, phba->mbox_mem_pool);
254                 return -EIO;
255         }
256
257         mp = (struct lpfc_dmabuf *) pmb->context1;
258
259         /* The mailbox was populated by the HBA.  Flush it to main store for the
260          * driver.  Note that all context buffers are from the driver's
261          * dma pool and have length LPFC_BPL_SIZE.
262          */
263          pci_dma_sync_single_for_cpu(phba->pcidev, mp->phys, LPFC_BPL_SIZE,
264                 PCI_DMA_FROMDEVICE);
265
266         memcpy(&phba->fc_sparam, mp->virt, sizeof (struct serv_parm));
267         lpfc_mbuf_free(phba, mp->virt, mp->phys);
268         kfree(mp);
269         pmb->context1 = NULL;
270
271         memcpy(&phba->fc_nodename, &phba->fc_sparam.nodeName,
272                sizeof (struct lpfc_name));
273         memcpy(&phba->fc_portname, &phba->fc_sparam.portName,
274                sizeof (struct lpfc_name));
275         /* If no serial number in VPD data, use low 6 bytes of WWNN */
276         /* This should be consolidated into parse_vpd ? - mr */
277         if (phba->SerialNumber[0] == 0) {
278                 uint8_t *outptr;
279
280                 outptr = (uint8_t *) & phba->fc_nodename.IEEE[0];
281                 for (i = 0; i < 12; i++) {
282                         status = *outptr++;
283                         j = ((status & 0xf0) >> 4);
284                         if (j <= 9)
285                                 phba->SerialNumber[i] =
286                                     (char)((uint8_t) 0x30 + (uint8_t) j);
287                         else
288                                 phba->SerialNumber[i] =
289                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
290                         i++;
291                         j = (status & 0xf);
292                         if (j <= 9)
293                                 phba->SerialNumber[i] =
294                                     (char)((uint8_t) 0x30 + (uint8_t) j);
295                         else
296                                 phba->SerialNumber[i] =
297                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
298                 }
299         }
300
301         /* This should turn on DELAYED ABTS for ELS timeouts */
302         lpfc_set_slim(phba, pmb, 0x052198, 0x1);
303         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
304                 phba->hba_state = LPFC_HBA_ERROR;
305                 mempool_free( pmb, phba->mbox_mem_pool);
306                 return -EIO;
307         }
308
309
310         lpfc_read_config(phba, pmb);
311         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
312                 lpfc_printf_log(phba,
313                                 KERN_ERR,
314                                 LOG_INIT,
315                                 "%d:0453 Adapter failed to init, mbxCmd x%x "
316                                 "READ_CONFIG, mbxStatus x%x\n",
317                                 phba->brd_no,
318                                 mb->mbxCommand, mb->mbxStatus);
319                 phba->hba_state = LPFC_HBA_ERROR;
320                 mempool_free( pmb, phba->mbox_mem_pool);
321                 return -EIO;
322         }
323
324         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
325         if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
326                 phba->cfg_hba_queue_depth =
327                         mb->un.varRdConfig.max_xri + 1;
328
329         phba->lmt = mb->un.varRdConfig.lmt;
330         /* HBA is not 4GB capable, or HBA is not 2GB capable,
331         don't let link speed ask for it */
332         if ((((phba->lmt & LMT_4250_10bit) != LMT_4250_10bit) &&
333                 (phba->cfg_link_speed > LINK_SPEED_2G)) ||
334                 (((phba->lmt & LMT_2125_10bit) != LMT_2125_10bit) && 
335                 (phba->cfg_link_speed > LINK_SPEED_1G))) {
336                 /* Reset link speed to auto. 1G/2GB HBA cfg'd for 4G */
337                 lpfc_printf_log(phba,
338                         KERN_WARNING,
339                         LOG_LINK_EVENT,
340                         "%d:1302 Invalid speed for this board: "
341                         "Reset link speed to auto: x%x\n",
342                         phba->brd_no,
343                         phba->cfg_link_speed);
344                         phba->cfg_link_speed = LINK_SPEED_AUTO;
345         }
346
347         if (!phba->intr_inited) {
348                 /* Add our interrupt routine to kernel's interrupt chain &
349                    enable it */
350
351                 if (request_irq(phba->pcidev->irq,
352                                 lpfc_intr_handler,
353                                 SA_SHIRQ,
354                                 LPFC_DRIVER_NAME,
355                                 phba) != 0) {
356                         /* Enable interrupt handler failed */
357                         lpfc_printf_log(phba,
358                                         KERN_ERR,
359                                         LOG_INIT,
360                                         "%d:0451 Enable interrupt handler "
361                                         "failed\n",
362                                         phba->brd_no);
363                         phba->hba_state = LPFC_HBA_ERROR;
364                         mempool_free(pmb, phba->mbox_mem_pool);
365                         return -EIO;
366                 }
367                 phba->intr_inited =
368                         (HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA);
369         }
370
371         phba->hba_state = LPFC_LINK_DOWN;
372
373         /* Only process IOCBs on ring 0 till hba_state is READY */
374         if (psli->ring[psli->ip_ring].cmdringaddr)
375                 psli->ring[psli->ip_ring].flag |= LPFC_STOP_IOCB_EVENT;
376         if (psli->ring[psli->fcp_ring].cmdringaddr)
377                 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
378         if (psli->ring[psli->next_ring].cmdringaddr)
379                 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
380
381         /* Post receive buffers for desired rings */
382         lpfc_post_rcv_buf(phba);
383
384         /* Enable appropriate host interrupts */
385         status = readl(phba->HCregaddr);
386         status |= phba->intr_inited;
387         if (psli->sliinit.num_rings > 0)
388                 status |= HC_R0INT_ENA;
389         if (psli->sliinit.num_rings > 1)
390                 status |= HC_R1INT_ENA;
391         if (psli->sliinit.num_rings > 2)
392                 status |= HC_R2INT_ENA;
393         if (psli->sliinit.num_rings > 3)
394                 status |= HC_R3INT_ENA;
395
396         writel(status, phba->HCregaddr);
397         readl(phba->HCregaddr); /* flush */
398
399         /* Setup and issue mailbox INITIALIZE LINK command */
400         lpfc_init_link(phba, pmb, phba->cfg_topology,
401                        phba->cfg_link_speed);
402
403         isr_cnt = psli->slistat.sliIntr;
404         clk_cnt = jiffies;
405
406         if (lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT) != MBX_SUCCESS) {
407                 lpfc_printf_log(phba,
408                                 KERN_ERR,
409                                 LOG_INIT,
410                                 "%d:0454 Adapter failed to init, mbxCmd x%x "
411                                 "INIT_LINK, mbxStatus x%x\n",
412                                 phba->brd_no,
413                                 mb->mbxCommand, mb->mbxStatus);
414
415                 /* Clear all interrupt enable conditions */
416                 writel(0, phba->HCregaddr);
417                 readl(phba->HCregaddr); /* flush */
418                 /* Clear all pending interrupts */
419                 writel(0xffffffff, phba->HAregaddr);
420                 readl(phba->HAregaddr); /* flush */
421
422                 free_irq(phba->pcidev->irq, phba);
423                 phba->hba_state = LPFC_HBA_ERROR;
424                 mempool_free(pmb, phba->mbox_mem_pool);
425                 return -EIO;
426         }
427         /* MBOX buffer will be freed in mbox compl */
428
429         /*
430          * Setup the ring 0 (els)  timeout handler
431          */
432         timeout = phba->fc_ratov << 1;
433
434         phba->els_tmofunc.expires = jiffies + HZ * timeout;
435         add_timer(&phba->els_tmofunc);
436
437         phba->fc_prevDID = Mask_DID;
438         flogi_sent = 0;
439         i = 0;
440         while ((phba->hba_state != LPFC_HBA_READY) ||
441                (phba->num_disc_nodes) || (phba->fc_prli_sent) ||
442                ((phba->fc_map_cnt == 0) && (i<2)) ||
443                (psli->sliinit.sli_flag & LPFC_SLI_MBOX_ACTIVE)) {
444                 /* Check every second for 30 retries. */
445                 i++;
446                 if (i > 30) {
447                         break;
448                 }
449                 if ((i >= 15) && (phba->hba_state <= LPFC_LINK_DOWN)) {
450                         /* The link is down.  Set linkdown timeout */
451                         break;
452                 }
453
454                 /* Delay for 1 second to give discovery time to complete. */
455                 for (j = 0; j < 20; j++) {
456                         /* On some systems, the driver's attach/detect routines
457                          * are uninterruptible.  Since the driver cannot predict
458                          * when this is true, just manually call the ISR every
459                          * 50 ms to service any interrupts.
460                          */
461                         msleep(50);
462                         if (isr_cnt == psli->slistat.sliIntr) {
463                                 lpfc_sli_intr(phba);
464                                 isr_cnt = psli->slistat.sliIntr;
465                         }
466                 }
467                 isr_cnt = psli->slistat.sliIntr;
468
469                 if (clk_cnt == jiffies) {
470                         /* REMOVE: IF THIS HAPPENS, SYSTEM CLOCK IS NOT RUNNING.
471                          * WE HAVE TO MANUALLY CALL OUR TIMEOUT ROUTINES.
472                          */
473                         clk_cnt = jiffies;
474                 }
475         }
476
477         /* Since num_disc_nodes keys off of PLOGI, delay a bit to let
478          * any potential PRLIs to flush thru the SLI sub-system.
479          */
480         msleep(50);
481         if (isr_cnt == psli->slistat.sliIntr) {
482                 lpfc_sli_intr(phba);
483         }
484
485         return (0);
486 }
487
488 /************************************************************************/
489 /*                                                                      */
490 /*    lpfc_hba_down_prep                                                */
491 /*    This routine will do LPFC uninitialization before the             */
492 /*    HBA is reset when bringing down the SLI Layer. This will be       */
493 /*    initialized as a SLI layer callback routine.                      */
494 /*    This routine returns 0 on success. Any other return value         */
495 /*    indicates an error.                                               */
496 /*                                                                      */
497 /************************************************************************/
498 int
499 lpfc_hba_down_prep(struct lpfc_hba * phba)
500 {
501         /* Disable interrupts */
502         writel(0, phba->HCregaddr);
503         readl(phba->HCregaddr); /* flush */
504
505         /* Cleanup potential discovery resources */
506         lpfc_els_flush_rscn(phba);
507         lpfc_els_flush_cmd(phba);
508         lpfc_disc_flush_list(phba);
509
510         return (0);
511 }
512
513 /************************************************************************/
514 /*                                                                      */
515 /*    lpfc_handle_eratt                                                 */
516 /*    This routine will handle processing a Host Attention              */
517 /*    Error Status event. This will be initialized                      */
518 /*    as a SLI layer callback routine.                                  */
519 /*                                                                      */
520 /************************************************************************/
521 void
522 lpfc_handle_eratt(struct lpfc_hba * phba, uint32_t status)
523 {
524         struct lpfc_sli *psli;
525         struct lpfc_sli_ring  *pring;
526         struct lpfc_iocbq     *iocb, *next_iocb;
527         IOCB_t          *icmd = NULL, *cmd = NULL;
528         struct lpfc_scsi_buf  *lpfc_cmd;
529         volatile uint32_t status1, status2;
530         void *from_slim;
531         unsigned long iflag;
532
533         psli = &phba->sli;
534         from_slim = ((uint8_t *)phba->MBslimaddr + 0xa8);
535         status1 = readl( from_slim);
536         from_slim =  ((uint8_t *)phba->MBslimaddr + 0xac);
537         status2 = readl( from_slim);
538
539         if (status & HS_FFER6) {
540                 /* Re-establishing Link */
541                 spin_lock_irqsave(phba->host->host_lock, iflag);
542                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
543                                 "%d:1301 Re-establishing Link "
544                                 "Data: x%x x%x x%x\n",
545                                 phba->brd_no, status, status1, status2);
546                 phba->fc_flag |= FC_ESTABLISH_LINK;
547
548                 /*
549                 * Firmware stops when it triggled erratt with HS_FFER6.
550                 * That could cause the I/Os dropped by the firmware.
551                 * Error iocb (I/O) on txcmplq and let the SCSI layer
552                 * retry it after re-establishing link.
553                 */
554                 pring = &psli->ring[psli->fcp_ring];
555
556                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
557                                          list) {
558                         cmd = &iocb->iocb;
559
560                         /* Must be a FCP command */
561                         if ((cmd->ulpCommand != CMD_FCP_ICMND64_CR) &&
562                                 (cmd->ulpCommand != CMD_FCP_IWRITE64_CR) &&
563                                 (cmd->ulpCommand != CMD_FCP_IREAD64_CR)) {
564                                 continue;
565                                 }
566
567                         /* context1 MUST be a struct lpfc_scsi_buf */
568                         lpfc_cmd = (struct lpfc_scsi_buf *)(iocb->context1);
569                         if (lpfc_cmd == 0) {
570                                 continue;
571                         }
572
573                         list_del(&iocb->list);
574                         pring->txcmplq_cnt--;
575
576                         if (iocb->iocb_cmpl) {
577                                 icmd = &iocb->iocb;
578                                 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
579                                 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
580                                 (iocb->iocb_cmpl)(phba, iocb, iocb);
581                         } else {
582                                 mempool_free( iocb, phba->iocb_mem_pool);
583                         }
584                 }
585
586                 /*
587                  * There was a firmware error.  Take the hba offline and then
588                  * attempt to restart it.
589                  */
590                 spin_unlock_irqrestore(phba->host->host_lock, iflag);
591                 lpfc_offline(phba);
592                 if (lpfc_online(phba) == 0) {   /* Initialize the HBA */
593                         mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
594                         return;
595                 }
596         } else {
597                 /* The if clause above forces this code path when the status
598                  * failure is a value other than FFER6.  Do not call the offline
599                  *  twice. This is the adapter hardware error path.
600                  */
601                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
602                                 "%d:0457 Adapter Hardware Error "
603                                 "Data: x%x x%x x%x\n",
604                                 phba->brd_no, status, status1, status2);
605
606                 lpfc_offline(phba);
607
608                 /*
609                  * Restart all traffic to this host.  Since the fc_transport
610                  * block functions (future) were not called in lpfc_offline,
611                  * don't call them here.
612                  */
613                 scsi_unblock_requests(phba->host);
614         }
615         return;
616 }
617
618 /************************************************************************/
619 /*                                                                      */
620 /*    lpfc_handle_latt                                                  */
621 /*    This routine will handle processing a Host Attention              */
622 /*    Link Status event. This will be initialized                       */
623 /*    as a SLI layer callback routine.                                  */
624 /*                                                                      */
625 /************************************************************************/
626 void
627 lpfc_handle_latt(struct lpfc_hba * phba)
628 {
629         struct lpfc_sli *psli;
630         LPFC_MBOXQ_t *pmb;
631         volatile uint32_t control;
632         unsigned long iflag;
633
634
635         spin_lock_irqsave(phba->host->host_lock, iflag);
636
637         /* called from host_interrupt, to process LATT */
638         psli = &phba->sli;
639         psli->slistat.linkEvent++;
640
641         /* Get a buffer which will be used for mailbox commands */
642         if ((pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
643                                                   GFP_ATOMIC))) {
644                 if (lpfc_read_la(phba, pmb) == 0) {
645                         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
646                         if (lpfc_sli_issue_mbox
647                             (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB))
648                             != MBX_NOT_FINISHED) {
649                                 /* Turn off Link Attention interrupts until
650                                    CLEAR_LA done */
651                                 psli->sliinit.sli_flag &= ~LPFC_PROCESS_LA;
652                                 control = readl(phba->HCregaddr);
653                                 control &= ~HC_LAINT_ENA;
654                                 writel(control, phba->HCregaddr);
655                                 readl(phba->HCregaddr); /* flush */
656
657                                 /* Clear Link Attention in HA REG */
658                                 writel(HA_LATT, phba->HAregaddr);
659                                 readl(phba->HAregaddr); /* flush */
660                                 spin_unlock_irqrestore(phba->host->host_lock,
661                                                        iflag);
662                                 return;
663                         } else {
664                                 mempool_free(pmb, phba->mbox_mem_pool);
665                         }
666                 } else {
667                         mempool_free(pmb, phba->mbox_mem_pool);
668                 }
669         }
670
671         /* Clear Link Attention in HA REG */
672         writel(HA_LATT, phba->HAregaddr);
673         readl(phba->HAregaddr); /* flush */
674         lpfc_linkdown(phba);
675         phba->hba_state = LPFC_HBA_ERROR;
676         spin_unlock_irqrestore(phba->host->host_lock, iflag);
677         return;
678 }
679
680 /************************************************************************/
681 /*                                                                      */
682 /*   lpfc_parse_vpd                                                     */
683 /*   This routine will parse the VPD data                               */
684 /*                                                                      */
685 /************************************************************************/
686 static int
687 lpfc_parse_vpd(struct lpfc_hba * phba, uint8_t * vpd)
688 {
689         uint8_t lenlo, lenhi;
690         uint8_t *Length;
691         int i, j;
692         int finished = 0;
693         int index = 0;
694
695         /* Vital Product */
696         lpfc_printf_log(phba,
697                         KERN_INFO,
698                         LOG_INIT,
699                         "%d:0455 Vital Product Data: x%x x%x x%x x%x\n",
700                         phba->brd_no,
701                         (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
702                         (uint32_t) vpd[3]);
703         do {
704                 switch (vpd[index]) {
705                 case 0x82:
706                         index += 1;
707                         lenlo = vpd[index];
708                         index += 1;
709                         lenhi = vpd[index];
710                         index += 1;
711                         i = ((((unsigned short)lenhi) << 8) + lenlo);
712                         index += i;
713                         break;
714                 case 0x90:
715                         index += 1;
716                         lenlo = vpd[index];
717                         index += 1;
718                         lenhi = vpd[index];
719                         index += 1;
720                         i = ((((unsigned short)lenhi) << 8) + lenlo);
721                         do {
722                                 /* Look for Serial Number */
723                                 if ((vpd[index] == 'S')
724                                     && (vpd[index + 1] == 'N')) {
725                                         index += 2;
726                                         Length = &vpd[index];
727                                         index += 1;
728                                         i = *Length;
729                                         j = 0;
730                                         while (i--) {
731                                                 phba->SerialNumber[j++] =
732                                                     vpd[index++];
733                                                 if (j == 31)
734                                                         break;
735                                         }
736                                         phba->SerialNumber[j] = 0;
737                                         return (1);
738                                 } else {
739                                         index += 2;
740                                         Length = &vpd[index];
741                                         index += 1;
742                                         j = (int)(*Length);
743                                         index += j;
744                                         i -= (3 + j);
745                                 }
746                         } while (i > 0);
747                         finished = 0;
748                         break;
749                 case 0x78:
750                         finished = 1;
751                         break;
752                 default:
753                         return (0);
754                 }
755         } while (!finished);
756         return (1);
757 }
758
759 /**************************************************/
760 /*   lpfc_post_buffer                             */
761 /*                                                */
762 /*   This routine will post count buffers to the  */
763 /*   ring with the QUE_RING_BUF_CN command. This  */
764 /*   allows 3 buffers / command to be posted.     */
765 /*   Returns the number of buffers NOT posted.    */
766 /**************************************************/
767 int
768 lpfc_post_buffer(struct lpfc_hba * phba, struct lpfc_sli_ring * pring, int cnt,
769                  int type)
770 {
771         IOCB_t *icmd;
772         struct lpfc_iocbq *iocb;
773         struct lpfc_dmabuf *mp1, *mp2;
774
775         cnt += pring->missbufcnt;
776
777         /* While there are buffers to post */
778         while (cnt > 0) {
779                 /* Allocate buffer for  command iocb */
780                 if ((iocb = mempool_alloc(phba->iocb_mem_pool, GFP_ATOMIC))
781                     == 0) {
782                         pring->missbufcnt = cnt;
783                         return (cnt);
784                 }
785                 memset(iocb, 0, sizeof (struct lpfc_iocbq));
786                 icmd = &iocb->iocb;
787
788                 /* 2 buffers can be posted per command */
789                 /* Allocate buffer to post */
790                 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_ATOMIC);
791                 if (mp1)
792                     mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
793                                                 &mp1->phys);
794                 if (mp1 == 0 || mp1->virt == 0) {
795                         if (mp1)
796                                 kfree(mp1);
797
798                         mempool_free( iocb, phba->iocb_mem_pool);
799                         pring->missbufcnt = cnt;
800                         return (cnt);
801                 }
802
803                 INIT_LIST_HEAD(&mp1->list);
804                 /* Allocate buffer to post */
805                 if (cnt > 1) {
806                         mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
807                         if (mp2)
808                                 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
809                                                             &mp2->phys);
810                         if (mp2 == 0 || mp2->virt == 0) {
811                                 if (mp2)
812                                         kfree(mp2);
813                                 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
814                                 kfree(mp1);
815                                 mempool_free( iocb, phba->iocb_mem_pool);
816                                 pring->missbufcnt = cnt;
817                                 return (cnt);
818                         }
819
820                         INIT_LIST_HEAD(&mp2->list);
821                 } else {
822                         mp2 = NULL;
823                 }
824
825                 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
826                 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
827                 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
828                 icmd->ulpBdeCount = 1;
829                 cnt--;
830                 if (mp2) {
831                         icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
832                         icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
833                         icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
834                         cnt--;
835                         icmd->ulpBdeCount = 2;
836                 }
837
838                 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
839                 icmd->ulpIoTag = lpfc_sli_next_iotag(phba, pring);
840                 icmd->ulpLe = 1;
841
842                 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
843                         lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
844                         kfree(mp1);
845                         if (mp2) {
846                                 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
847                                 kfree(mp2);
848                         }
849                         mempool_free( iocb, phba->iocb_mem_pool);
850                         pring->missbufcnt = cnt;
851                         return (cnt);
852                 }
853                 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
854                 if (mp2) {
855                         lpfc_sli_ringpostbuf_put(phba, pring, mp2);
856                 }
857         }
858         pring->missbufcnt = 0;
859         return (0);
860 }
861
862 /************************************************************************/
863 /*                                                                      */
864 /*   lpfc_post_rcv_buf                                                  */
865 /*   This routine post initial rcv buffers to the configured rings      */
866 /*                                                                      */
867 /************************************************************************/
868 static int
869 lpfc_post_rcv_buf(struct lpfc_hba * phba)
870 {
871         struct lpfc_sli *psli = &phba->sli;
872
873         /* Ring 0, ELS / CT buffers */
874         lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
875         /* Ring 2 - FCP no buffers needed */
876
877         return 0;
878 }
879
880 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
881
882 /************************************************************************/
883 /*                                                                      */
884 /*   lpfc_sha_init                                                      */
885 /*                                                                      */
886 /************************************************************************/
887 static void
888 lpfc_sha_init(uint32_t * HashResultPointer)
889 {
890         HashResultPointer[0] = 0x67452301;
891         HashResultPointer[1] = 0xEFCDAB89;
892         HashResultPointer[2] = 0x98BADCFE;
893         HashResultPointer[3] = 0x10325476;
894         HashResultPointer[4] = 0xC3D2E1F0;
895 }
896
897 /************************************************************************/
898 /*                                                                      */
899 /*   lpfc_sha_iterate                                                   */
900 /*                                                                      */
901 /************************************************************************/
902 static void
903 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
904 {
905         int t;
906         uint32_t TEMP;
907         uint32_t A, B, C, D, E;
908         t = 16;
909         do {
910                 HashWorkingPointer[t] =
911                     S(1,
912                       HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
913                                                                      8] ^
914                       HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
915         } while (++t <= 79);
916         t = 0;
917         A = HashResultPointer[0];
918         B = HashResultPointer[1];
919         C = HashResultPointer[2];
920         D = HashResultPointer[3];
921         E = HashResultPointer[4];
922
923         do {
924                 if (t < 20) {
925                         TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
926                 } else if (t < 40) {
927                         TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
928                 } else if (t < 60) {
929                         TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
930                 } else {
931                         TEMP = (B ^ C ^ D) + 0xCA62C1D6;
932                 }
933                 TEMP += S(5, A) + E + HashWorkingPointer[t];
934                 E = D;
935                 D = C;
936                 C = S(30, B);
937                 B = A;
938                 A = TEMP;
939         } while (++t <= 79);
940
941         HashResultPointer[0] += A;
942         HashResultPointer[1] += B;
943         HashResultPointer[2] += C;
944         HashResultPointer[3] += D;
945         HashResultPointer[4] += E;
946
947 }
948
949 /************************************************************************/
950 /*                                                                      */
951 /*   lpfc_challenge_key                                                 */
952 /*                                                                      */
953 /************************************************************************/
954 static void
955 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
956 {
957         *HashWorking = (*RandomChallenge ^ *HashWorking);
958 }
959
960 /************************************************************************/
961 /*                                                                      */
962 /*   lpfc_hba_init                                                      */
963 /*                                                                      */
964 /************************************************************************/
965 void
966 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
967 {
968         int t;
969         uint32_t *HashWorking;
970         uint32_t *pwwnn = phba->wwnn;
971
972         HashWorking = kmalloc(80 * sizeof(uint32_t), GFP_ATOMIC);
973         if (!HashWorking)
974                 return;
975         
976         memset(HashWorking, 0, (80 * sizeof(uint32_t)));
977         HashWorking[0] = HashWorking[78] = *pwwnn++;
978         HashWorking[1] = HashWorking[79] = *pwwnn;
979
980         for (t = 0; t < 7; t++)
981                 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
982
983         lpfc_sha_init(hbainit);
984         lpfc_sha_iterate(hbainit, HashWorking);
985         kfree(HashWorking);
986 }
987
988 static void
989 lpfc_consistent_bind_cleanup(struct lpfc_hba * phba)
990 {
991         struct lpfc_bindlist *bdlp, *next_bdlp;
992
993         list_for_each_entry_safe(bdlp, next_bdlp,
994                                  &phba->fc_nlpbind_list, nlp_listp) {
995                 list_del(&bdlp->nlp_listp);
996                 mempool_free( bdlp, phba->bind_mem_pool);
997         }
998         phba->fc_bind_cnt = 0;
999 }
1000
1001 void
1002 lpfc_cleanup(struct lpfc_hba * phba, uint32_t save_bind)
1003 {
1004         struct lpfc_nodelist *ndlp, *next_ndlp;
1005
1006         /* clean up phba - lpfc specific */
1007         lpfc_can_disctmo(phba);
1008         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpunmap_list,
1009                                 nlp_listp) {
1010                 lpfc_nlp_remove(phba, ndlp);
1011         }
1012
1013         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpmap_list,
1014                                  nlp_listp) {
1015                 lpfc_nlp_remove(phba, ndlp);
1016         }
1017
1018         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_unused_list,
1019                                 nlp_listp) {
1020                 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1021         }
1022
1023         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_plogi_list,
1024                                 nlp_listp) {
1025                 lpfc_nlp_remove(phba, ndlp);
1026         }
1027
1028         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
1029                                 nlp_listp) {
1030                 lpfc_nlp_remove(phba, ndlp);
1031         }
1032
1033         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_reglogin_list,
1034                                 nlp_listp) {
1035                 lpfc_nlp_remove(phba, ndlp);
1036         }
1037
1038         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_prli_list,
1039                                 nlp_listp) {
1040                 lpfc_nlp_remove(phba, ndlp);
1041         }
1042
1043         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
1044                                 nlp_listp) {
1045                 lpfc_nlp_remove(phba, ndlp);
1046         }
1047
1048         if (save_bind == 0) {
1049                 lpfc_consistent_bind_cleanup(phba);
1050         }
1051
1052         INIT_LIST_HEAD(&phba->fc_nlpmap_list);
1053         INIT_LIST_HEAD(&phba->fc_nlpunmap_list);
1054         INIT_LIST_HEAD(&phba->fc_unused_list);
1055         INIT_LIST_HEAD(&phba->fc_plogi_list);
1056         INIT_LIST_HEAD(&phba->fc_adisc_list);
1057         INIT_LIST_HEAD(&phba->fc_reglogin_list);
1058         INIT_LIST_HEAD(&phba->fc_prli_list);
1059         INIT_LIST_HEAD(&phba->fc_npr_list);
1060
1061         phba->fc_map_cnt   = 0;
1062         phba->fc_unmap_cnt = 0;
1063         phba->fc_plogi_cnt = 0;
1064         phba->fc_adisc_cnt = 0;
1065         phba->fc_reglogin_cnt = 0;
1066         phba->fc_prli_cnt  = 0;
1067         phba->fc_npr_cnt   = 0;
1068         phba->fc_unused_cnt= 0;
1069         return;
1070 }
1071
1072 void
1073 lpfc_establish_link_tmo(unsigned long ptr)
1074 {
1075         struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
1076         unsigned long iflag;
1077
1078         spin_lock_irqsave(phba->host->host_lock, iflag);
1079
1080         /* Re-establishing Link, timer expired */
1081         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1082                         "%d:1300 Re-establishing Link, timer expired "
1083                         "Data: x%x x%x\n",
1084                         phba->brd_no, phba->fc_flag, phba->hba_state);
1085         phba->fc_flag &= ~FC_ESTABLISH_LINK;
1086         spin_unlock_irqrestore(phba->host->host_lock, iflag);
1087 }
1088
1089 int
1090 lpfc_online(struct lpfc_hba * phba)
1091 {
1092         if (!phba)
1093                 return 0;
1094
1095         if (!(phba->fc_flag & FC_OFFLINE_MODE))
1096                 return 0;
1097
1098         lpfc_printf_log(phba,
1099                        KERN_WARNING,
1100                        LOG_INIT,
1101                        "%d:0458 Bring Adapter online\n",
1102                        phba->brd_no);
1103
1104         if (!lpfc_sli_queue_setup(phba))
1105                 return 1;
1106
1107         if (lpfc_sli_hba_setup(phba))   /* Initialize the HBA */
1108                 return 1;
1109
1110         phba->fc_flag &= ~FC_OFFLINE_MODE;
1111
1112         /*
1113          * Restart all traffic to this host.  Since the fc_transport block
1114          * functions (future) were not called in lpfc_offline, don't call them
1115          * here.
1116          */
1117         scsi_unblock_requests(phba->host);
1118         return 0;
1119 }
1120
1121 int
1122 lpfc_offline(struct lpfc_hba * phba)
1123 {
1124         struct lpfc_sli_ring *pring;
1125         struct lpfc_sli *psli;
1126         unsigned long iflag;
1127         int i = 0;
1128
1129         if (!phba)
1130                 return 0;
1131
1132         if (phba->fc_flag & FC_OFFLINE_MODE)
1133                 return 0;
1134
1135         /*
1136          * Don't call the fc_transport block api (future).  The device is
1137          * going offline and causing a timer to fire in the midlayer is
1138          * unproductive.  Just block all new requests until the driver
1139          * comes back online.
1140          */
1141         scsi_block_requests(phba->host);
1142         psli = &phba->sli;
1143         pring = &psli->ring[psli->fcp_ring];
1144
1145         lpfc_linkdown(phba);
1146
1147         /* The linkdown event takes 30 seconds to timeout. */
1148         while (pring->txcmplq_cnt) {
1149                 mdelay(10);
1150                 if (i++ > 3000)
1151                         break;
1152         }
1153
1154         /* stop all timers associated with this hba */
1155         spin_lock_irqsave(phba->host->host_lock, iflag);
1156         lpfc_stop_timer(phba);
1157         spin_unlock_irqrestore(phba->host->host_lock, iflag);
1158
1159         lpfc_printf_log(phba,
1160                        KERN_WARNING,
1161                        LOG_INIT,
1162                        "%d:0460 Bring Adapter offline\n",
1163                        phba->brd_no);
1164
1165         /* Bring down the SLI Layer and cleanup.  The HBA is offline
1166            now.  */
1167         lpfc_sli_hba_down(phba);
1168         lpfc_cleanup(phba, 1);
1169         phba->fc_flag |= FC_OFFLINE_MODE;
1170         return 0;
1171 }
1172
1173 /******************************************************************************
1174 * Function name : lpfc_scsi_free
1175 *
1176 * Description   : Called from fc_detach to free scsi tgt / lun resources
1177 *
1178 ******************************************************************************/
1179 int
1180 lpfc_scsi_free(struct lpfc_hba * phba)
1181 {
1182         struct lpfc_target *targetp;
1183         int i;
1184
1185         for (i = 0; i < MAX_FCP_TARGET; i++) {
1186                 targetp = phba->device_queue_hash[i];
1187                 if (targetp) {
1188                         kfree(targetp);
1189                         phba->device_queue_hash[i] = NULL;
1190                 }
1191         }
1192         return 0;
1193 }
1194
1195 static void
1196 lpfc_wakeup_event(struct lpfc_hba * phba, fcEVTHDR_t * ep)
1197 {
1198         ep->e_mode &= ~E_SLEEPING_MODE;
1199         switch (ep->e_mask) {
1200         case FC_REG_LINK_EVENT:
1201                 wake_up_interruptible(&phba->linkevtwq);
1202                 break;
1203         case FC_REG_RSCN_EVENT:
1204                 wake_up_interruptible(&phba->rscnevtwq);
1205                 break;
1206         case FC_REG_CT_EVENT:
1207                 wake_up_interruptible(&phba->ctevtwq);
1208                 break;
1209         }
1210         return;
1211 }
1212
1213 int
1214 lpfc_put_event(struct lpfc_hba * phba, uint32_t evcode, uint32_t evdata0,
1215                void * evdata1, uint32_t evdata2, uint32_t evdata3)
1216 {
1217         fcEVT_t *ep;
1218         fcEVTHDR_t *ehp = phba->fc_evt_head;
1219         int found = 0;
1220         void *fstype = NULL;
1221         struct lpfc_dmabuf *mp;
1222         struct lpfc_sli_ct_request *ctp;
1223         struct lpfc_hba_event *rec;
1224         uint32_t evtype;
1225
1226         switch (evcode) {
1227                 case HBA_EVENT_RSCN:
1228                         evtype = FC_REG_RSCN_EVENT;
1229                         break;
1230                 case HBA_EVENT_LINK_DOWN:
1231                 case HBA_EVENT_LINK_UP:
1232                         evtype = FC_REG_LINK_EVENT;
1233                         break;
1234                 default:
1235                         evtype = FC_REG_CT_EVENT;
1236         }
1237
1238         if (evtype == FC_REG_RSCN_EVENT || evtype == FC_REG_LINK_EVENT) {
1239                 rec = &phba->hbaevt[phba->hba_event_put];
1240                 rec->fc_eventcode = evcode;
1241                 rec->fc_evdata1 = evdata0;
1242                 rec->fc_evdata2 = (uint32_t)(unsigned long)evdata1;
1243                 rec->fc_evdata3 = evdata2;
1244                 rec->fc_evdata4 = evdata3;
1245
1246                 phba->hba_event_put++;
1247                 if (phba->hba_event_put >= MAX_HBAEVT)
1248                         phba->hba_event_put = 0;
1249
1250                 if (phba->hba_event_put == phba->hba_event_get) {
1251                         phba->hba_event_missed++;
1252                         phba->hba_event_get++;
1253                         if (phba->hba_event_get >= MAX_HBAEVT)
1254                                 phba->hba_event_get = 0;
1255                 }
1256         }
1257
1258         if (evtype == FC_REG_CT_EVENT) {
1259                 mp = (struct lpfc_dmabuf *) evdata1;
1260                 ctp = (struct lpfc_sli_ct_request *) mp->virt;
1261                 fstype = (void *)(ulong) (ctp->FsType);
1262         }
1263
1264         while (ehp && ((ehp->e_mask != evtype) || (ehp->e_type != fstype)))
1265                 ehp = (fcEVTHDR_t *) ehp->e_next_header;
1266
1267         if (!ehp)
1268                 return (0);
1269
1270         ep = ehp->e_head;
1271
1272         while (ep && !(found)) {
1273                 if (ep->evt_sleep) {
1274                         switch (evtype) {
1275                         case FC_REG_CT_EVENT:
1276                                 if ((ep->evt_type ==
1277                                      (void *)(ulong) FC_FSTYPE_ALL)
1278                                     || (ep->evt_type == fstype)) {
1279                                         found++;
1280                                         ep->evt_data0 = evdata0; /* tag */
1281                                         ep->evt_data1 = evdata1; /* buffer
1282                                                                     ptr */
1283                                         ep->evt_data2 = evdata2; /* count */
1284                                         ep->evt_sleep = 0;
1285                                         if (ehp->e_mode & E_SLEEPING_MODE) {
1286                                                 ehp->e_flag |=
1287                                                     E_GET_EVENT_ACTIVE;
1288                                                 lpfc_wakeup_event(phba, ehp);
1289                                         }
1290                                         /* For FC_REG_CT_EVENT just give it to
1291                                            first one found */
1292                                 }
1293                                 break;
1294                         default:
1295                                 found++;
1296                                 ep->evt_data0 = evdata0;
1297                                 ep->evt_data1 = evdata1;
1298                                 ep->evt_data2 = evdata2;
1299                                 ep->evt_sleep = 0;
1300                                 if ((ehp->e_mode & E_SLEEPING_MODE)
1301                                     && !(ehp->e_flag & E_GET_EVENT_ACTIVE)) {
1302                                         ehp->e_flag |= E_GET_EVENT_ACTIVE;
1303                                         lpfc_wakeup_event(phba, ehp);
1304                                 }
1305                                 /* For all other events, give it to every one
1306                                    waiting */
1307                                 break;
1308                         }
1309                 }
1310                 ep = ep->evt_next;
1311         }
1312         if (evtype == FC_REG_LINK_EVENT)
1313                 phba->nport_event_cnt++;
1314
1315         return (found);
1316 }
1317
1318 int
1319 lpfc_stop_timer(struct lpfc_hba * phba)
1320 {
1321         struct lpfc_sli *psli = &phba->sli;
1322
1323         /* Instead of a timer, this has been converted to a
1324          * deferred procedding list.
1325          */
1326         while (!list_empty(&phba->freebufList)) {
1327                 struct lpfc_dmabuf *mp;
1328
1329                 mp = (struct lpfc_dmabuf *)(phba->freebufList.next);
1330                 if (mp) {
1331                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1332                         list_del(&mp->list);
1333                         kfree(mp);
1334                 }
1335         }
1336
1337         del_timer_sync(&phba->fc_estabtmo);
1338         del_timer_sync(&phba->fc_disctmo);
1339         del_timer_sync(&phba->fc_scantmo);
1340         del_timer_sync(&phba->fc_fdmitmo);
1341         del_timer_sync(&phba->els_tmofunc);
1342         psli = &phba->sli;
1343         del_timer_sync(&psli->mbox_tmo);
1344         return(1);
1345 }