Merge branch 'for-linus' of git://git.open-osd.org/linux-open-osd
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_init.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 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/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/idr.h>
26 #include <linux/interrupt.h>
27 #include <linux/kthread.h>
28 #include <linux/pci.h>
29 #include <linux/spinlock.h>
30 #include <linux/ctype.h>
31 #include <linux/aer.h>
32
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37
38 #include "lpfc_hw4.h"
39 #include "lpfc_hw.h"
40 #include "lpfc_sli.h"
41 #include "lpfc_sli4.h"
42 #include "lpfc_nl.h"
43 #include "lpfc_disc.h"
44 #include "lpfc_scsi.h"
45 #include "lpfc.h"
46 #include "lpfc_logmsg.h"
47 #include "lpfc_crtn.h"
48 #include "lpfc_vport.h"
49 #include "lpfc_version.h"
50
51 char *_dump_buf_data;
52 unsigned long _dump_buf_data_order;
53 char *_dump_buf_dif;
54 unsigned long _dump_buf_dif_order;
55 spinlock_t _dump_buf_lock;
56
57 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
58 static int lpfc_post_rcv_buf(struct lpfc_hba *);
59 static int lpfc_sli4_queue_create(struct lpfc_hba *);
60 static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
61 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
62 static int lpfc_setup_endian_order(struct lpfc_hba *);
63 static int lpfc_sli4_read_config(struct lpfc_hba *);
64 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
65 static void lpfc_free_sgl_list(struct lpfc_hba *);
66 static int lpfc_init_sgl_list(struct lpfc_hba *);
67 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
68 static void lpfc_free_active_sgl(struct lpfc_hba *);
69 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
70 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
71 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
72 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
73 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
74
75 static struct scsi_transport_template *lpfc_transport_template = NULL;
76 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
77 static DEFINE_IDR(lpfc_hba_index);
78
79 /**
80  * lpfc_config_port_prep - Perform lpfc initialization prior to config port
81  * @phba: pointer to lpfc hba data structure.
82  *
83  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
84  * mailbox command. It retrieves the revision information from the HBA and
85  * collects the Vital Product Data (VPD) about the HBA for preparing the
86  * configuration of the HBA.
87  *
88  * Return codes:
89  *   0 - success.
90  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
91  *   Any other value - indicates an error.
92  **/
93 int
94 lpfc_config_port_prep(struct lpfc_hba *phba)
95 {
96         lpfc_vpd_t *vp = &phba->vpd;
97         int i = 0, rc;
98         LPFC_MBOXQ_t *pmb;
99         MAILBOX_t *mb;
100         char *lpfc_vpd_data = NULL;
101         uint16_t offset = 0;
102         static char licensed[56] =
103                     "key unlock for use with gnu public licensed code only\0";
104         static int init_key = 1;
105
106         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
107         if (!pmb) {
108                 phba->link_state = LPFC_HBA_ERROR;
109                 return -ENOMEM;
110         }
111
112         mb = &pmb->u.mb;
113         phba->link_state = LPFC_INIT_MBX_CMDS;
114
115         if (lpfc_is_LC_HBA(phba->pcidev->device)) {
116                 if (init_key) {
117                         uint32_t *ptext = (uint32_t *) licensed;
118
119                         for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
120                                 *ptext = cpu_to_be32(*ptext);
121                         init_key = 0;
122                 }
123
124                 lpfc_read_nv(phba, pmb);
125                 memset((char*)mb->un.varRDnvp.rsvd3, 0,
126                         sizeof (mb->un.varRDnvp.rsvd3));
127                 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
128                          sizeof (licensed));
129
130                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
131
132                 if (rc != MBX_SUCCESS) {
133                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
134                                         "0324 Config Port initialization "
135                                         "error, mbxCmd x%x READ_NVPARM, "
136                                         "mbxStatus x%x\n",
137                                         mb->mbxCommand, mb->mbxStatus);
138                         mempool_free(pmb, phba->mbox_mem_pool);
139                         return -ERESTART;
140                 }
141                 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
142                        sizeof(phba->wwnn));
143                 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
144                        sizeof(phba->wwpn));
145         }
146
147         phba->sli3_options = 0x0;
148
149         /* Setup and issue mailbox READ REV command */
150         lpfc_read_rev(phba, pmb);
151         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
152         if (rc != MBX_SUCCESS) {
153                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
154                                 "0439 Adapter failed to init, mbxCmd x%x "
155                                 "READ_REV, mbxStatus x%x\n",
156                                 mb->mbxCommand, mb->mbxStatus);
157                 mempool_free( pmb, phba->mbox_mem_pool);
158                 return -ERESTART;
159         }
160
161
162         /*
163          * The value of rr must be 1 since the driver set the cv field to 1.
164          * This setting requires the FW to set all revision fields.
165          */
166         if (mb->un.varRdRev.rr == 0) {
167                 vp->rev.rBit = 0;
168                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
169                                 "0440 Adapter failed to init, READ_REV has "
170                                 "missing revision information.\n");
171                 mempool_free(pmb, phba->mbox_mem_pool);
172                 return -ERESTART;
173         }
174
175         if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
176                 mempool_free(pmb, phba->mbox_mem_pool);
177                 return -EINVAL;
178         }
179
180         /* Save information as VPD data */
181         vp->rev.rBit = 1;
182         memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
183         vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
184         memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
185         vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
186         memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
187         vp->rev.biuRev = mb->un.varRdRev.biuRev;
188         vp->rev.smRev = mb->un.varRdRev.smRev;
189         vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
190         vp->rev.endecRev = mb->un.varRdRev.endecRev;
191         vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
192         vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
193         vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
194         vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
195         vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
196         vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
197
198         /* If the sli feature level is less then 9, we must
199          * tear down all RPIs and VPIs on link down if NPIV
200          * is enabled.
201          */
202         if (vp->rev.feaLevelHigh < 9)
203                 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
204
205         if (lpfc_is_LC_HBA(phba->pcidev->device))
206                 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
207                                                 sizeof (phba->RandomData));
208
209         /* Get adapter VPD information */
210         lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
211         if (!lpfc_vpd_data)
212                 goto out_free_mbox;
213
214         do {
215                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
216                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
217
218                 if (rc != MBX_SUCCESS) {
219                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
220                                         "0441 VPD not present on adapter, "
221                                         "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
222                                         mb->mbxCommand, mb->mbxStatus);
223                         mb->un.varDmp.word_cnt = 0;
224                 }
225                 /* dump mem may return a zero when finished or we got a
226                  * mailbox error, either way we are done.
227                  */
228                 if (mb->un.varDmp.word_cnt == 0)
229                         break;
230                 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
231                         mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
232                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
233                                       lpfc_vpd_data + offset,
234                                       mb->un.varDmp.word_cnt);
235                 offset += mb->un.varDmp.word_cnt;
236         } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
237         lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
238
239         kfree(lpfc_vpd_data);
240 out_free_mbox:
241         mempool_free(pmb, phba->mbox_mem_pool);
242         return 0;
243 }
244
245 /**
246  * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
247  * @phba: pointer to lpfc hba data structure.
248  * @pmboxq: pointer to the driver internal queue element for mailbox command.
249  *
250  * This is the completion handler for driver's configuring asynchronous event
251  * mailbox command to the device. If the mailbox command returns successfully,
252  * it will set internal async event support flag to 1; otherwise, it will
253  * set internal async event support flag to 0.
254  **/
255 static void
256 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
257 {
258         if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
259                 phba->temp_sensor_support = 1;
260         else
261                 phba->temp_sensor_support = 0;
262         mempool_free(pmboxq, phba->mbox_mem_pool);
263         return;
264 }
265
266 /**
267  * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
268  * @phba: pointer to lpfc hba data structure.
269  * @pmboxq: pointer to the driver internal queue element for mailbox command.
270  *
271  * This is the completion handler for dump mailbox command for getting
272  * wake up parameters. When this command complete, the response contain
273  * Option rom version of the HBA. This function translate the version number
274  * into a human readable string and store it in OptionROMVersion.
275  **/
276 static void
277 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
278 {
279         struct prog_id *prg;
280         uint32_t prog_id_word;
281         char dist = ' ';
282         /* character array used for decoding dist type. */
283         char dist_char[] = "nabx";
284
285         if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
286                 mempool_free(pmboxq, phba->mbox_mem_pool);
287                 return;
288         }
289
290         prg = (struct prog_id *) &prog_id_word;
291
292         /* word 7 contain option rom version */
293         prog_id_word = pmboxq->u.mb.un.varWords[7];
294
295         /* Decode the Option rom version word to a readable string */
296         if (prg->dist < 4)
297                 dist = dist_char[prg->dist];
298
299         if ((prg->dist == 3) && (prg->num == 0))
300                 sprintf(phba->OptionROMVersion, "%d.%d%d",
301                         prg->ver, prg->rev, prg->lev);
302         else
303                 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
304                         prg->ver, prg->rev, prg->lev,
305                         dist, prg->num);
306         mempool_free(pmboxq, phba->mbox_mem_pool);
307         return;
308 }
309
310 /**
311  * lpfc_config_port_post - Perform lpfc initialization after config port
312  * @phba: pointer to lpfc hba data structure.
313  *
314  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
315  * command call. It performs all internal resource and state setups on the
316  * port: post IOCB buffers, enable appropriate host interrupt attentions,
317  * ELS ring timers, etc.
318  *
319  * Return codes
320  *   0 - success.
321  *   Any other value - error.
322  **/
323 int
324 lpfc_config_port_post(struct lpfc_hba *phba)
325 {
326         struct lpfc_vport *vport = phba->pport;
327         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
328         LPFC_MBOXQ_t *pmb;
329         MAILBOX_t *mb;
330         struct lpfc_dmabuf *mp;
331         struct lpfc_sli *psli = &phba->sli;
332         uint32_t status, timeout;
333         int i, j;
334         int rc;
335
336         spin_lock_irq(&phba->hbalock);
337         /*
338          * If the Config port completed correctly the HBA is not
339          * over heated any more.
340          */
341         if (phba->over_temp_state == HBA_OVER_TEMP)
342                 phba->over_temp_state = HBA_NORMAL_TEMP;
343         spin_unlock_irq(&phba->hbalock);
344
345         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
346         if (!pmb) {
347                 phba->link_state = LPFC_HBA_ERROR;
348                 return -ENOMEM;
349         }
350         mb = &pmb->u.mb;
351
352         /* Get login parameters for NID.  */
353         lpfc_read_sparam(phba, pmb, 0);
354         pmb->vport = vport;
355         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
356                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
357                                 "0448 Adapter failed init, mbxCmd x%x "
358                                 "READ_SPARM mbxStatus x%x\n",
359                                 mb->mbxCommand, mb->mbxStatus);
360                 phba->link_state = LPFC_HBA_ERROR;
361                 mp = (struct lpfc_dmabuf *) pmb->context1;
362                 mempool_free( pmb, phba->mbox_mem_pool);
363                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
364                 kfree(mp);
365                 return -EIO;
366         }
367
368         mp = (struct lpfc_dmabuf *) pmb->context1;
369
370         memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
371         lpfc_mbuf_free(phba, mp->virt, mp->phys);
372         kfree(mp);
373         pmb->context1 = NULL;
374
375         if (phba->cfg_soft_wwnn)
376                 u64_to_wwn(phba->cfg_soft_wwnn,
377                            vport->fc_sparam.nodeName.u.wwn);
378         if (phba->cfg_soft_wwpn)
379                 u64_to_wwn(phba->cfg_soft_wwpn,
380                            vport->fc_sparam.portName.u.wwn);
381         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
382                sizeof (struct lpfc_name));
383         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
384                sizeof (struct lpfc_name));
385
386         /* Update the fc_host data structures with new wwn. */
387         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
388         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
389         fc_host_max_npiv_vports(shost) = phba->max_vpi;
390
391         /* If no serial number in VPD data, use low 6 bytes of WWNN */
392         /* This should be consolidated into parse_vpd ? - mr */
393         if (phba->SerialNumber[0] == 0) {
394                 uint8_t *outptr;
395
396                 outptr = &vport->fc_nodename.u.s.IEEE[0];
397                 for (i = 0; i < 12; i++) {
398                         status = *outptr++;
399                         j = ((status & 0xf0) >> 4);
400                         if (j <= 9)
401                                 phba->SerialNumber[i] =
402                                     (char)((uint8_t) 0x30 + (uint8_t) j);
403                         else
404                                 phba->SerialNumber[i] =
405                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
406                         i++;
407                         j = (status & 0xf);
408                         if (j <= 9)
409                                 phba->SerialNumber[i] =
410                                     (char)((uint8_t) 0x30 + (uint8_t) j);
411                         else
412                                 phba->SerialNumber[i] =
413                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
414                 }
415         }
416
417         lpfc_read_config(phba, pmb);
418         pmb->vport = vport;
419         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
420                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
421                                 "0453 Adapter failed to init, mbxCmd x%x "
422                                 "READ_CONFIG, mbxStatus x%x\n",
423                                 mb->mbxCommand, mb->mbxStatus);
424                 phba->link_state = LPFC_HBA_ERROR;
425                 mempool_free( pmb, phba->mbox_mem_pool);
426                 return -EIO;
427         }
428
429         /* Check if the port is disabled */
430         lpfc_sli_read_link_ste(phba);
431
432         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
433         if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
434                 phba->cfg_hba_queue_depth =
435                         (mb->un.varRdConfig.max_xri + 1) -
436                                         lpfc_sli4_get_els_iocb_cnt(phba);
437
438         phba->lmt = mb->un.varRdConfig.lmt;
439
440         /* Get the default values for Model Name and Description */
441         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
442
443         if ((phba->cfg_link_speed > LINK_SPEED_10G)
444             || ((phba->cfg_link_speed == LINK_SPEED_1G)
445                 && !(phba->lmt & LMT_1Gb))
446             || ((phba->cfg_link_speed == LINK_SPEED_2G)
447                 && !(phba->lmt & LMT_2Gb))
448             || ((phba->cfg_link_speed == LINK_SPEED_4G)
449                 && !(phba->lmt & LMT_4Gb))
450             || ((phba->cfg_link_speed == LINK_SPEED_8G)
451                 && !(phba->lmt & LMT_8Gb))
452             || ((phba->cfg_link_speed == LINK_SPEED_10G)
453                 && !(phba->lmt & LMT_10Gb))) {
454                 /* Reset link speed to auto */
455                 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
456                         "1302 Invalid speed for this board: "
457                         "Reset link speed to auto: x%x\n",
458                         phba->cfg_link_speed);
459                         phba->cfg_link_speed = LINK_SPEED_AUTO;
460         }
461
462         phba->link_state = LPFC_LINK_DOWN;
463
464         /* Only process IOCBs on ELS ring till hba_state is READY */
465         if (psli->ring[psli->extra_ring].cmdringaddr)
466                 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
467         if (psli->ring[psli->fcp_ring].cmdringaddr)
468                 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
469         if (psli->ring[psli->next_ring].cmdringaddr)
470                 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
471
472         /* Post receive buffers for desired rings */
473         if (phba->sli_rev != 3)
474                 lpfc_post_rcv_buf(phba);
475
476         /*
477          * Configure HBA MSI-X attention conditions to messages if MSI-X mode
478          */
479         if (phba->intr_type == MSIX) {
480                 rc = lpfc_config_msi(phba, pmb);
481                 if (rc) {
482                         mempool_free(pmb, phba->mbox_mem_pool);
483                         return -EIO;
484                 }
485                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
486                 if (rc != MBX_SUCCESS) {
487                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
488                                         "0352 Config MSI mailbox command "
489                                         "failed, mbxCmd x%x, mbxStatus x%x\n",
490                                         pmb->u.mb.mbxCommand,
491                                         pmb->u.mb.mbxStatus);
492                         mempool_free(pmb, phba->mbox_mem_pool);
493                         return -EIO;
494                 }
495         }
496
497         spin_lock_irq(&phba->hbalock);
498         /* Initialize ERATT handling flag */
499         phba->hba_flag &= ~HBA_ERATT_HANDLED;
500
501         /* Enable appropriate host interrupts */
502         status = readl(phba->HCregaddr);
503         status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
504         if (psli->num_rings > 0)
505                 status |= HC_R0INT_ENA;
506         if (psli->num_rings > 1)
507                 status |= HC_R1INT_ENA;
508         if (psli->num_rings > 2)
509                 status |= HC_R2INT_ENA;
510         if (psli->num_rings > 3)
511                 status |= HC_R3INT_ENA;
512
513         if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
514             (phba->cfg_poll & DISABLE_FCP_RING_INT))
515                 status &= ~(HC_R0INT_ENA);
516
517         writel(status, phba->HCregaddr);
518         readl(phba->HCregaddr); /* flush */
519         spin_unlock_irq(&phba->hbalock);
520
521         /* Set up ring-0 (ELS) timer */
522         timeout = phba->fc_ratov * 2;
523         mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
524         /* Set up heart beat (HB) timer */
525         mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
526         phba->hb_outstanding = 0;
527         phba->last_completion_time = jiffies;
528         /* Set up error attention (ERATT) polling timer */
529         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
530
531         if (phba->hba_flag & LINK_DISABLED) {
532                 lpfc_printf_log(phba,
533                         KERN_ERR, LOG_INIT,
534                         "2598 Adapter Link is disabled.\n");
535                 lpfc_down_link(phba, pmb);
536                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
537                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
538                 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
539                         lpfc_printf_log(phba,
540                         KERN_ERR, LOG_INIT,
541                         "2599 Adapter failed to issue DOWN_LINK"
542                         " mbox command rc 0x%x\n", rc);
543
544                         mempool_free(pmb, phba->mbox_mem_pool);
545                         return -EIO;
546                 }
547         } else {
548                 lpfc_init_link(phba, pmb, phba->cfg_topology,
549                         phba->cfg_link_speed);
550                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
551                 lpfc_set_loopback_flag(phba);
552                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
553                 if (rc != MBX_SUCCESS) {
554                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
555                                 "0454 Adapter failed to init, mbxCmd x%x "
556                                 "INIT_LINK, mbxStatus x%x\n",
557                                 mb->mbxCommand, mb->mbxStatus);
558
559                         /* Clear all interrupt enable conditions */
560                         writel(0, phba->HCregaddr);
561                         readl(phba->HCregaddr); /* flush */
562                         /* Clear all pending interrupts */
563                         writel(0xffffffff, phba->HAregaddr);
564                         readl(phba->HAregaddr); /* flush */
565
566                         phba->link_state = LPFC_HBA_ERROR;
567                         if (rc != MBX_BUSY)
568                                 mempool_free(pmb, phba->mbox_mem_pool);
569                         return -EIO;
570                 }
571         }
572         /* MBOX buffer will be freed in mbox compl */
573         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
574         lpfc_config_async(phba, pmb, LPFC_ELS_RING);
575         pmb->mbox_cmpl = lpfc_config_async_cmpl;
576         pmb->vport = phba->pport;
577         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
578
579         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
580                 lpfc_printf_log(phba,
581                                 KERN_ERR,
582                                 LOG_INIT,
583                                 "0456 Adapter failed to issue "
584                                 "ASYNCEVT_ENABLE mbox status x%x\n",
585                                 rc);
586                 mempool_free(pmb, phba->mbox_mem_pool);
587         }
588
589         /* Get Option rom version */
590         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
591         lpfc_dump_wakeup_param(phba, pmb);
592         pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
593         pmb->vport = phba->pport;
594         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
595
596         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
597                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
598                                 "to get Option ROM version status x%x\n", rc);
599                 mempool_free(pmb, phba->mbox_mem_pool);
600         }
601
602         return 0;
603 }
604
605 /**
606  * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
607  * @phba: pointer to lpfc HBA data structure.
608  *
609  * This routine will do LPFC uninitialization before the HBA is reset when
610  * bringing down the SLI Layer.
611  *
612  * Return codes
613  *   0 - success.
614  *   Any other value - error.
615  **/
616 int
617 lpfc_hba_down_prep(struct lpfc_hba *phba)
618 {
619         struct lpfc_vport **vports;
620         int i;
621
622         if (phba->sli_rev <= LPFC_SLI_REV3) {
623                 /* Disable interrupts */
624                 writel(0, phba->HCregaddr);
625                 readl(phba->HCregaddr); /* flush */
626         }
627
628         if (phba->pport->load_flag & FC_UNLOADING)
629                 lpfc_cleanup_discovery_resources(phba->pport);
630         else {
631                 vports = lpfc_create_vport_work_array(phba);
632                 if (vports != NULL)
633                         for (i = 0; i <= phba->max_vports &&
634                                 vports[i] != NULL; i++)
635                                 lpfc_cleanup_discovery_resources(vports[i]);
636                 lpfc_destroy_vport_work_array(phba, vports);
637         }
638         return 0;
639 }
640
641 /**
642  * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
643  * @phba: pointer to lpfc HBA data structure.
644  *
645  * This routine will do uninitialization after the HBA is reset when bring
646  * down the SLI Layer.
647  *
648  * Return codes
649  *   0 - success.
650  *   Any other value - error.
651  **/
652 static int
653 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
654 {
655         struct lpfc_sli *psli = &phba->sli;
656         struct lpfc_sli_ring *pring;
657         struct lpfc_dmabuf *mp, *next_mp;
658         LIST_HEAD(completions);
659         int i;
660
661         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
662                 lpfc_sli_hbqbuf_free_all(phba);
663         else {
664                 /* Cleanup preposted buffers on the ELS ring */
665                 pring = &psli->ring[LPFC_ELS_RING];
666                 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
667                         list_del(&mp->list);
668                         pring->postbufq_cnt--;
669                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
670                         kfree(mp);
671                 }
672         }
673
674         spin_lock_irq(&phba->hbalock);
675         for (i = 0; i < psli->num_rings; i++) {
676                 pring = &psli->ring[i];
677
678                 /* At this point in time the HBA is either reset or DOA. Either
679                  * way, nothing should be on txcmplq as it will NEVER complete.
680                  */
681                 list_splice_init(&pring->txcmplq, &completions);
682                 pring->txcmplq_cnt = 0;
683                 spin_unlock_irq(&phba->hbalock);
684
685                 /* Cancel all the IOCBs from the completions list */
686                 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
687                                       IOERR_SLI_ABORTED);
688
689                 lpfc_sli_abort_iocb_ring(phba, pring);
690                 spin_lock_irq(&phba->hbalock);
691         }
692         spin_unlock_irq(&phba->hbalock);
693
694         return 0;
695 }
696 /**
697  * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
698  * @phba: pointer to lpfc HBA data structure.
699  *
700  * This routine will do uninitialization after the HBA is reset when bring
701  * down the SLI Layer.
702  *
703  * Return codes
704  *   0 - success.
705  *   Any other value - error.
706  **/
707 static int
708 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
709 {
710         struct lpfc_scsi_buf *psb, *psb_next;
711         LIST_HEAD(aborts);
712         int ret;
713         unsigned long iflag = 0;
714         ret = lpfc_hba_down_post_s3(phba);
715         if (ret)
716                 return ret;
717         /* At this point in time the HBA is either reset or DOA. Either
718          * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
719          * on the lpfc_sgl_list so that it can either be freed if the
720          * driver is unloading or reposted if the driver is restarting
721          * the port.
722          */
723         spin_lock_irq(&phba->hbalock);  /* required for lpfc_sgl_list and */
724                                         /* scsl_buf_list */
725         /* abts_sgl_list_lock required because worker thread uses this
726          * list.
727          */
728         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
729         list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
730                         &phba->sli4_hba.lpfc_sgl_list);
731         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
732         /* abts_scsi_buf_list_lock required because worker thread uses this
733          * list.
734          */
735         spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
736         list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
737                         &aborts);
738         spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
739         spin_unlock_irq(&phba->hbalock);
740
741         list_for_each_entry_safe(psb, psb_next, &aborts, list) {
742                 psb->pCmd = NULL;
743                 psb->status = IOSTAT_SUCCESS;
744         }
745         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
746         list_splice(&aborts, &phba->lpfc_scsi_buf_list);
747         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
748         return 0;
749 }
750
751 /**
752  * lpfc_hba_down_post - Wrapper func for hba down post routine
753  * @phba: pointer to lpfc HBA data structure.
754  *
755  * This routine wraps the actual SLI3 or SLI4 routine for performing
756  * uninitialization after the HBA is reset when bring down the SLI Layer.
757  *
758  * Return codes
759  *   0 - success.
760  *   Any other value - error.
761  **/
762 int
763 lpfc_hba_down_post(struct lpfc_hba *phba)
764 {
765         return (*phba->lpfc_hba_down_post)(phba);
766 }
767
768 /**
769  * lpfc_hb_timeout - The HBA-timer timeout handler
770  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
771  *
772  * This is the HBA-timer timeout handler registered to the lpfc driver. When
773  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
774  * work-port-events bitmap and the worker thread is notified. This timeout
775  * event will be used by the worker thread to invoke the actual timeout
776  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
777  * be performed in the timeout handler and the HBA timeout event bit shall
778  * be cleared by the worker thread after it has taken the event bitmap out.
779  **/
780 static void
781 lpfc_hb_timeout(unsigned long ptr)
782 {
783         struct lpfc_hba *phba;
784         uint32_t tmo_posted;
785         unsigned long iflag;
786
787         phba = (struct lpfc_hba *)ptr;
788
789         /* Check for heart beat timeout conditions */
790         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
791         tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
792         if (!tmo_posted)
793                 phba->pport->work_port_events |= WORKER_HB_TMO;
794         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
795
796         /* Tell the worker thread there is work to do */
797         if (!tmo_posted)
798                 lpfc_worker_wake_up(phba);
799         return;
800 }
801
802 /**
803  * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
804  * @phba: pointer to lpfc hba data structure.
805  * @pmboxq: pointer to the driver internal queue element for mailbox command.
806  *
807  * This is the callback function to the lpfc heart-beat mailbox command.
808  * If configured, the lpfc driver issues the heart-beat mailbox command to
809  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
810  * heart-beat mailbox command is issued, the driver shall set up heart-beat
811  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
812  * heart-beat outstanding state. Once the mailbox command comes back and
813  * no error conditions detected, the heart-beat mailbox command timer is
814  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
815  * state is cleared for the next heart-beat. If the timer expired with the
816  * heart-beat outstanding state set, the driver will put the HBA offline.
817  **/
818 static void
819 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
820 {
821         unsigned long drvr_flag;
822
823         spin_lock_irqsave(&phba->hbalock, drvr_flag);
824         phba->hb_outstanding = 0;
825         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
826
827         /* Check and reset heart-beat timer is necessary */
828         mempool_free(pmboxq, phba->mbox_mem_pool);
829         if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
830                 !(phba->link_state == LPFC_HBA_ERROR) &&
831                 !(phba->pport->load_flag & FC_UNLOADING))
832                 mod_timer(&phba->hb_tmofunc,
833                         jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
834         return;
835 }
836
837 /**
838  * lpfc_hb_timeout_handler - The HBA-timer timeout handler
839  * @phba: pointer to lpfc hba data structure.
840  *
841  * This is the actual HBA-timer timeout handler to be invoked by the worker
842  * thread whenever the HBA timer fired and HBA-timeout event posted. This
843  * handler performs any periodic operations needed for the device. If such
844  * periodic event has already been attended to either in the interrupt handler
845  * or by processing slow-ring or fast-ring events within the HBA-timer
846  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
847  * the timer for the next timeout period. If lpfc heart-beat mailbox command
848  * is configured and there is no heart-beat mailbox command outstanding, a
849  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
850  * has been a heart-beat mailbox command outstanding, the HBA shall be put
851  * to offline.
852  **/
853 void
854 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
855 {
856         struct lpfc_vport **vports;
857         LPFC_MBOXQ_t *pmboxq;
858         struct lpfc_dmabuf *buf_ptr;
859         int retval, i;
860         struct lpfc_sli *psli = &phba->sli;
861         LIST_HEAD(completions);
862
863         vports = lpfc_create_vport_work_array(phba);
864         if (vports != NULL)
865                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
866                         lpfc_rcv_seq_check_edtov(vports[i]);
867         lpfc_destroy_vport_work_array(phba, vports);
868
869         if ((phba->link_state == LPFC_HBA_ERROR) ||
870                 (phba->pport->load_flag & FC_UNLOADING) ||
871                 (phba->pport->fc_flag & FC_OFFLINE_MODE))
872                 return;
873
874         spin_lock_irq(&phba->pport->work_port_lock);
875
876         if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
877                 jiffies)) {
878                 spin_unlock_irq(&phba->pport->work_port_lock);
879                 if (!phba->hb_outstanding)
880                         mod_timer(&phba->hb_tmofunc,
881                                 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
882                 else
883                         mod_timer(&phba->hb_tmofunc,
884                                 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
885                 return;
886         }
887         spin_unlock_irq(&phba->pport->work_port_lock);
888
889         if (phba->elsbuf_cnt &&
890                 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
891                 spin_lock_irq(&phba->hbalock);
892                 list_splice_init(&phba->elsbuf, &completions);
893                 phba->elsbuf_cnt = 0;
894                 phba->elsbuf_prev_cnt = 0;
895                 spin_unlock_irq(&phba->hbalock);
896
897                 while (!list_empty(&completions)) {
898                         list_remove_head(&completions, buf_ptr,
899                                 struct lpfc_dmabuf, list);
900                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
901                         kfree(buf_ptr);
902                 }
903         }
904         phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
905
906         /* If there is no heart beat outstanding, issue a heartbeat command */
907         if (phba->cfg_enable_hba_heartbeat) {
908                 if (!phba->hb_outstanding) {
909                         pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
910                         if (!pmboxq) {
911                                 mod_timer(&phba->hb_tmofunc,
912                                           jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
913                                 return;
914                         }
915
916                         lpfc_heart_beat(phba, pmboxq);
917                         pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
918                         pmboxq->vport = phba->pport;
919                         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
920
921                         if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
922                                 mempool_free(pmboxq, phba->mbox_mem_pool);
923                                 mod_timer(&phba->hb_tmofunc,
924                                           jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
925                                 return;
926                         }
927                         mod_timer(&phba->hb_tmofunc,
928                                   jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
929                         phba->hb_outstanding = 1;
930                         return;
931                 } else {
932                         /*
933                         * If heart beat timeout called with hb_outstanding set
934                         * we need to take the HBA offline.
935                         */
936                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
937                                         "0459 Adapter heartbeat failure, "
938                                         "taking this port offline.\n");
939
940                         spin_lock_irq(&phba->hbalock);
941                         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
942                         spin_unlock_irq(&phba->hbalock);
943
944                         lpfc_offline_prep(phba);
945                         lpfc_offline(phba);
946                         lpfc_unblock_mgmt_io(phba);
947                         phba->link_state = LPFC_HBA_ERROR;
948                         lpfc_hba_down_post(phba);
949                 }
950         }
951 }
952
953 /**
954  * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
955  * @phba: pointer to lpfc hba data structure.
956  *
957  * This routine is called to bring the HBA offline when HBA hardware error
958  * other than Port Error 6 has been detected.
959  **/
960 static void
961 lpfc_offline_eratt(struct lpfc_hba *phba)
962 {
963         struct lpfc_sli   *psli = &phba->sli;
964
965         spin_lock_irq(&phba->hbalock);
966         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
967         spin_unlock_irq(&phba->hbalock);
968         lpfc_offline_prep(phba);
969
970         lpfc_offline(phba);
971         lpfc_reset_barrier(phba);
972         spin_lock_irq(&phba->hbalock);
973         lpfc_sli_brdreset(phba);
974         spin_unlock_irq(&phba->hbalock);
975         lpfc_hba_down_post(phba);
976         lpfc_sli_brdready(phba, HS_MBRDY);
977         lpfc_unblock_mgmt_io(phba);
978         phba->link_state = LPFC_HBA_ERROR;
979         return;
980 }
981
982 /**
983  * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
984  * @phba: pointer to lpfc hba data structure.
985  *
986  * This routine is called to bring a SLI4 HBA offline when HBA hardware error
987  * other than Port Error 6 has been detected.
988  **/
989 static void
990 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
991 {
992         lpfc_offline_prep(phba);
993         lpfc_offline(phba);
994         lpfc_sli4_brdreset(phba);
995         lpfc_hba_down_post(phba);
996         lpfc_sli4_post_status_check(phba);
997         lpfc_unblock_mgmt_io(phba);
998         phba->link_state = LPFC_HBA_ERROR;
999 }
1000
1001 /**
1002  * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1003  * @phba: pointer to lpfc hba data structure.
1004  *
1005  * This routine is invoked to handle the deferred HBA hardware error
1006  * conditions. This type of error is indicated by HBA by setting ER1
1007  * and another ER bit in the host status register. The driver will
1008  * wait until the ER1 bit clears before handling the error condition.
1009  **/
1010 static void
1011 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1012 {
1013         uint32_t old_host_status = phba->work_hs;
1014         struct lpfc_sli_ring  *pring;
1015         struct lpfc_sli *psli = &phba->sli;
1016
1017         /* If the pci channel is offline, ignore possible errors,
1018          * since we cannot communicate with the pci card anyway.
1019          */
1020         if (pci_channel_offline(phba->pcidev)) {
1021                 spin_lock_irq(&phba->hbalock);
1022                 phba->hba_flag &= ~DEFER_ERATT;
1023                 spin_unlock_irq(&phba->hbalock);
1024                 return;
1025         }
1026
1027         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1028                 "0479 Deferred Adapter Hardware Error "
1029                 "Data: x%x x%x x%x\n",
1030                 phba->work_hs,
1031                 phba->work_status[0], phba->work_status[1]);
1032
1033         spin_lock_irq(&phba->hbalock);
1034         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1035         spin_unlock_irq(&phba->hbalock);
1036
1037
1038         /*
1039          * Firmware stops when it triggred erratt. That could cause the I/Os
1040          * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1041          * SCSI layer retry it after re-establishing link.
1042          */
1043         pring = &psli->ring[psli->fcp_ring];
1044         lpfc_sli_abort_iocb_ring(phba, pring);
1045
1046         /*
1047          * There was a firmware error. Take the hba offline and then
1048          * attempt to restart it.
1049          */
1050         lpfc_offline_prep(phba);
1051         lpfc_offline(phba);
1052
1053         /* Wait for the ER1 bit to clear.*/
1054         while (phba->work_hs & HS_FFER1) {
1055                 msleep(100);
1056                 phba->work_hs = readl(phba->HSregaddr);
1057                 /* If driver is unloading let the worker thread continue */
1058                 if (phba->pport->load_flag & FC_UNLOADING) {
1059                         phba->work_hs = 0;
1060                         break;
1061                 }
1062         }
1063
1064         /*
1065          * This is to ptrotect against a race condition in which
1066          * first write to the host attention register clear the
1067          * host status register.
1068          */
1069         if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1070                 phba->work_hs = old_host_status & ~HS_FFER1;
1071
1072         spin_lock_irq(&phba->hbalock);
1073         phba->hba_flag &= ~DEFER_ERATT;
1074         spin_unlock_irq(&phba->hbalock);
1075         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1076         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1077 }
1078
1079 static void
1080 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1081 {
1082         struct lpfc_board_event_header board_event;
1083         struct Scsi_Host *shost;
1084
1085         board_event.event_type = FC_REG_BOARD_EVENT;
1086         board_event.subcategory = LPFC_EVENT_PORTINTERR;
1087         shost = lpfc_shost_from_vport(phba->pport);
1088         fc_host_post_vendor_event(shost, fc_get_event_number(),
1089                                   sizeof(board_event),
1090                                   (char *) &board_event,
1091                                   LPFC_NL_VENDOR_ID);
1092 }
1093
1094 /**
1095  * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1096  * @phba: pointer to lpfc hba data structure.
1097  *
1098  * This routine is invoked to handle the following HBA hardware error
1099  * conditions:
1100  * 1 - HBA error attention interrupt
1101  * 2 - DMA ring index out of range
1102  * 3 - Mailbox command came back as unknown
1103  **/
1104 static void
1105 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1106 {
1107         struct lpfc_vport *vport = phba->pport;
1108         struct lpfc_sli   *psli = &phba->sli;
1109         struct lpfc_sli_ring  *pring;
1110         uint32_t event_data;
1111         unsigned long temperature;
1112         struct temp_event temp_event_data;
1113         struct Scsi_Host  *shost;
1114
1115         /* If the pci channel is offline, ignore possible errors,
1116          * since we cannot communicate with the pci card anyway.
1117          */
1118         if (pci_channel_offline(phba->pcidev)) {
1119                 spin_lock_irq(&phba->hbalock);
1120                 phba->hba_flag &= ~DEFER_ERATT;
1121                 spin_unlock_irq(&phba->hbalock);
1122                 return;
1123         }
1124
1125         /* If resets are disabled then leave the HBA alone and return */
1126         if (!phba->cfg_enable_hba_reset)
1127                 return;
1128
1129         /* Send an internal error event to mgmt application */
1130         lpfc_board_errevt_to_mgmt(phba);
1131
1132         if (phba->hba_flag & DEFER_ERATT)
1133                 lpfc_handle_deferred_eratt(phba);
1134
1135         if (phba->work_hs & HS_FFER6) {
1136                 /* Re-establishing Link */
1137                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1138                                 "1301 Re-establishing Link "
1139                                 "Data: x%x x%x x%x\n",
1140                                 phba->work_hs,
1141                                 phba->work_status[0], phba->work_status[1]);
1142
1143                 spin_lock_irq(&phba->hbalock);
1144                 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1145                 spin_unlock_irq(&phba->hbalock);
1146
1147                 /*
1148                 * Firmware stops when it triggled erratt with HS_FFER6.
1149                 * That could cause the I/Os dropped by the firmware.
1150                 * Error iocb (I/O) on txcmplq and let the SCSI layer
1151                 * retry it after re-establishing link.
1152                 */
1153                 pring = &psli->ring[psli->fcp_ring];
1154                 lpfc_sli_abort_iocb_ring(phba, pring);
1155
1156                 /*
1157                  * There was a firmware error.  Take the hba offline and then
1158                  * attempt to restart it.
1159                  */
1160                 lpfc_offline_prep(phba);
1161                 lpfc_offline(phba);
1162                 lpfc_sli_brdrestart(phba);
1163                 if (lpfc_online(phba) == 0) {   /* Initialize the HBA */
1164                         lpfc_unblock_mgmt_io(phba);
1165                         return;
1166                 }
1167                 lpfc_unblock_mgmt_io(phba);
1168         } else if (phba->work_hs & HS_CRIT_TEMP) {
1169                 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1170                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1171                 temp_event_data.event_code = LPFC_CRIT_TEMP;
1172                 temp_event_data.data = (uint32_t)temperature;
1173
1174                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1175                                 "0406 Adapter maximum temperature exceeded "
1176                                 "(%ld), taking this port offline "
1177                                 "Data: x%x x%x x%x\n",
1178                                 temperature, phba->work_hs,
1179                                 phba->work_status[0], phba->work_status[1]);
1180
1181                 shost = lpfc_shost_from_vport(phba->pport);
1182                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1183                                           sizeof(temp_event_data),
1184                                           (char *) &temp_event_data,
1185                                           SCSI_NL_VID_TYPE_PCI
1186                                           | PCI_VENDOR_ID_EMULEX);
1187
1188                 spin_lock_irq(&phba->hbalock);
1189                 phba->over_temp_state = HBA_OVER_TEMP;
1190                 spin_unlock_irq(&phba->hbalock);
1191                 lpfc_offline_eratt(phba);
1192
1193         } else {
1194                 /* The if clause above forces this code path when the status
1195                  * failure is a value other than FFER6. Do not call the offline
1196                  * twice. This is the adapter hardware error path.
1197                  */
1198                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1199                                 "0457 Adapter Hardware Error "
1200                                 "Data: x%x x%x x%x\n",
1201                                 phba->work_hs,
1202                                 phba->work_status[0], phba->work_status[1]);
1203
1204                 event_data = FC_REG_DUMP_EVENT;
1205                 shost = lpfc_shost_from_vport(vport);
1206                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1207                                 sizeof(event_data), (char *) &event_data,
1208                                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1209
1210                 lpfc_offline_eratt(phba);
1211         }
1212         return;
1213 }
1214
1215 /**
1216  * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1217  * @phba: pointer to lpfc hba data structure.
1218  *
1219  * This routine is invoked to handle the SLI4 HBA hardware error attention
1220  * conditions.
1221  **/
1222 static void
1223 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1224 {
1225         struct lpfc_vport *vport = phba->pport;
1226         uint32_t event_data;
1227         struct Scsi_Host *shost;
1228
1229         /* If the pci channel is offline, ignore possible errors, since
1230          * we cannot communicate with the pci card anyway.
1231          */
1232         if (pci_channel_offline(phba->pcidev))
1233                 return;
1234         /* If resets are disabled then leave the HBA alone and return */
1235         if (!phba->cfg_enable_hba_reset)
1236                 return;
1237
1238         /* Send an internal error event to mgmt application */
1239         lpfc_board_errevt_to_mgmt(phba);
1240
1241         /* For now, the actual action for SLI4 device handling is not
1242          * specified yet, just treated it as adaptor hardware failure
1243          */
1244         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1245                         "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1246                         phba->work_status[0], phba->work_status[1]);
1247
1248         event_data = FC_REG_DUMP_EVENT;
1249         shost = lpfc_shost_from_vport(vport);
1250         fc_host_post_vendor_event(shost, fc_get_event_number(),
1251                                   sizeof(event_data), (char *) &event_data,
1252                                   SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1253
1254         lpfc_sli4_offline_eratt(phba);
1255 }
1256
1257 /**
1258  * lpfc_handle_eratt - Wrapper func for handling hba error attention
1259  * @phba: pointer to lpfc HBA data structure.
1260  *
1261  * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1262  * routine from the API jump table function pointer from the lpfc_hba struct.
1263  *
1264  * Return codes
1265  *   0 - success.
1266  *   Any other value - error.
1267  **/
1268 void
1269 lpfc_handle_eratt(struct lpfc_hba *phba)
1270 {
1271         (*phba->lpfc_handle_eratt)(phba);
1272 }
1273
1274 /**
1275  * lpfc_handle_latt - The HBA link event handler
1276  * @phba: pointer to lpfc hba data structure.
1277  *
1278  * This routine is invoked from the worker thread to handle a HBA host
1279  * attention link event.
1280  **/
1281 void
1282 lpfc_handle_latt(struct lpfc_hba *phba)
1283 {
1284         struct lpfc_vport *vport = phba->pport;
1285         struct lpfc_sli   *psli = &phba->sli;
1286         LPFC_MBOXQ_t *pmb;
1287         volatile uint32_t control;
1288         struct lpfc_dmabuf *mp;
1289         int rc = 0;
1290
1291         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1292         if (!pmb) {
1293                 rc = 1;
1294                 goto lpfc_handle_latt_err_exit;
1295         }
1296
1297         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1298         if (!mp) {
1299                 rc = 2;
1300                 goto lpfc_handle_latt_free_pmb;
1301         }
1302
1303         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1304         if (!mp->virt) {
1305                 rc = 3;
1306                 goto lpfc_handle_latt_free_mp;
1307         }
1308
1309         /* Cleanup any outstanding ELS commands */
1310         lpfc_els_flush_all_cmd(phba);
1311
1312         psli->slistat.link_event++;
1313         lpfc_read_la(phba, pmb, mp);
1314         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
1315         pmb->vport = vport;
1316         /* Block ELS IOCBs until we have processed this mbox command */
1317         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
1318         rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
1319         if (rc == MBX_NOT_FINISHED) {
1320                 rc = 4;
1321                 goto lpfc_handle_latt_free_mbuf;
1322         }
1323
1324         /* Clear Link Attention in HA REG */
1325         spin_lock_irq(&phba->hbalock);
1326         writel(HA_LATT, phba->HAregaddr);
1327         readl(phba->HAregaddr); /* flush */
1328         spin_unlock_irq(&phba->hbalock);
1329
1330         return;
1331
1332 lpfc_handle_latt_free_mbuf:
1333         phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1334         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1335 lpfc_handle_latt_free_mp:
1336         kfree(mp);
1337 lpfc_handle_latt_free_pmb:
1338         mempool_free(pmb, phba->mbox_mem_pool);
1339 lpfc_handle_latt_err_exit:
1340         /* Enable Link attention interrupts */
1341         spin_lock_irq(&phba->hbalock);
1342         psli->sli_flag |= LPFC_PROCESS_LA;
1343         control = readl(phba->HCregaddr);
1344         control |= HC_LAINT_ENA;
1345         writel(control, phba->HCregaddr);
1346         readl(phba->HCregaddr); /* flush */
1347
1348         /* Clear Link Attention in HA REG */
1349         writel(HA_LATT, phba->HAregaddr);
1350         readl(phba->HAregaddr); /* flush */
1351         spin_unlock_irq(&phba->hbalock);
1352         lpfc_linkdown(phba);
1353         phba->link_state = LPFC_HBA_ERROR;
1354
1355         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1356                      "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1357
1358         return;
1359 }
1360
1361 /**
1362  * lpfc_parse_vpd - Parse VPD (Vital Product Data)
1363  * @phba: pointer to lpfc hba data structure.
1364  * @vpd: pointer to the vital product data.
1365  * @len: length of the vital product data in bytes.
1366  *
1367  * This routine parses the Vital Product Data (VPD). The VPD is treated as
1368  * an array of characters. In this routine, the ModelName, ProgramType, and
1369  * ModelDesc, etc. fields of the phba data structure will be populated.
1370  *
1371  * Return codes
1372  *   0 - pointer to the VPD passed in is NULL
1373  *   1 - success
1374  **/
1375 int
1376 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1377 {
1378         uint8_t lenlo, lenhi;
1379         int Length;
1380         int i, j;
1381         int finished = 0;
1382         int index = 0;
1383
1384         if (!vpd)
1385                 return 0;
1386
1387         /* Vital Product */
1388         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1389                         "0455 Vital Product Data: x%x x%x x%x x%x\n",
1390                         (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1391                         (uint32_t) vpd[3]);
1392         while (!finished && (index < (len - 4))) {
1393                 switch (vpd[index]) {
1394                 case 0x82:
1395                 case 0x91:
1396                         index += 1;
1397                         lenlo = vpd[index];
1398                         index += 1;
1399                         lenhi = vpd[index];
1400                         index += 1;
1401                         i = ((((unsigned short)lenhi) << 8) + lenlo);
1402                         index += i;
1403                         break;
1404                 case 0x90:
1405                         index += 1;
1406                         lenlo = vpd[index];
1407                         index += 1;
1408                         lenhi = vpd[index];
1409                         index += 1;
1410                         Length = ((((unsigned short)lenhi) << 8) + lenlo);
1411                         if (Length > len - index)
1412                                 Length = len - index;
1413                         while (Length > 0) {
1414                         /* Look for Serial Number */
1415                         if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1416                                 index += 2;
1417                                 i = vpd[index];
1418                                 index += 1;
1419                                 j = 0;
1420                                 Length -= (3+i);
1421                                 while(i--) {
1422                                         phba->SerialNumber[j++] = vpd[index++];
1423                                         if (j == 31)
1424                                                 break;
1425                                 }
1426                                 phba->SerialNumber[j] = 0;
1427                                 continue;
1428                         }
1429                         else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1430                                 phba->vpd_flag |= VPD_MODEL_DESC;
1431                                 index += 2;
1432                                 i = vpd[index];
1433                                 index += 1;
1434                                 j = 0;
1435                                 Length -= (3+i);
1436                                 while(i--) {
1437                                         phba->ModelDesc[j++] = vpd[index++];
1438                                         if (j == 255)
1439                                                 break;
1440                                 }
1441                                 phba->ModelDesc[j] = 0;
1442                                 continue;
1443                         }
1444                         else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1445                                 phba->vpd_flag |= VPD_MODEL_NAME;
1446                                 index += 2;
1447                                 i = vpd[index];
1448                                 index += 1;
1449                                 j = 0;
1450                                 Length -= (3+i);
1451                                 while(i--) {
1452                                         phba->ModelName[j++] = vpd[index++];
1453                                         if (j == 79)
1454                                                 break;
1455                                 }
1456                                 phba->ModelName[j] = 0;
1457                                 continue;
1458                         }
1459                         else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1460                                 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1461                                 index += 2;
1462                                 i = vpd[index];
1463                                 index += 1;
1464                                 j = 0;
1465                                 Length -= (3+i);
1466                                 while(i--) {
1467                                         phba->ProgramType[j++] = vpd[index++];
1468                                         if (j == 255)
1469                                                 break;
1470                                 }
1471                                 phba->ProgramType[j] = 0;
1472                                 continue;
1473                         }
1474                         else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1475                                 phba->vpd_flag |= VPD_PORT;
1476                                 index += 2;
1477                                 i = vpd[index];
1478                                 index += 1;
1479                                 j = 0;
1480                                 Length -= (3+i);
1481                                 while(i--) {
1482                                 phba->Port[j++] = vpd[index++];
1483                                 if (j == 19)
1484                                         break;
1485                                 }
1486                                 phba->Port[j] = 0;
1487                                 continue;
1488                         }
1489                         else {
1490                                 index += 2;
1491                                 i = vpd[index];
1492                                 index += 1;
1493                                 index += i;
1494                                 Length -= (3 + i);
1495                         }
1496                 }
1497                 finished = 0;
1498                 break;
1499                 case 0x78:
1500                         finished = 1;
1501                         break;
1502                 default:
1503                         index ++;
1504                         break;
1505                 }
1506         }
1507
1508         return(1);
1509 }
1510
1511 /**
1512  * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
1513  * @phba: pointer to lpfc hba data structure.
1514  * @mdp: pointer to the data structure to hold the derived model name.
1515  * @descp: pointer to the data structure to hold the derived description.
1516  *
1517  * This routine retrieves HBA's description based on its registered PCI device
1518  * ID. The @descp passed into this function points to an array of 256 chars. It
1519  * shall be returned with the model name, maximum speed, and the host bus type.
1520  * The @mdp passed into this function points to an array of 80 chars. When the
1521  * function returns, the @mdp will be filled with the model name.
1522  **/
1523 static void
1524 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1525 {
1526         lpfc_vpd_t *vp;
1527         uint16_t dev_id = phba->pcidev->device;
1528         int max_speed;
1529         int GE = 0;
1530         int oneConnect = 0; /* default is not a oneConnect */
1531         struct {
1532                 char *name;
1533                 char *bus;
1534                 char *function;
1535         } m = {"<Unknown>", "", ""};
1536
1537         if (mdp && mdp[0] != '\0'
1538                 && descp && descp[0] != '\0')
1539                 return;
1540
1541         if (phba->lmt & LMT_10Gb)
1542                 max_speed = 10;
1543         else if (phba->lmt & LMT_8Gb)
1544                 max_speed = 8;
1545         else if (phba->lmt & LMT_4Gb)
1546                 max_speed = 4;
1547         else if (phba->lmt & LMT_2Gb)
1548                 max_speed = 2;
1549         else
1550                 max_speed = 1;
1551
1552         vp = &phba->vpd;
1553
1554         switch (dev_id) {
1555         case PCI_DEVICE_ID_FIREFLY:
1556                 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
1557                 break;
1558         case PCI_DEVICE_ID_SUPERFLY:
1559                 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1560                         m = (typeof(m)){"LP7000", "PCI",
1561                                         "Fibre Channel Adapter"};
1562                 else
1563                         m = (typeof(m)){"LP7000E", "PCI",
1564                                         "Fibre Channel Adapter"};
1565                 break;
1566         case PCI_DEVICE_ID_DRAGONFLY:
1567                 m = (typeof(m)){"LP8000", "PCI",
1568                                 "Fibre Channel Adapter"};
1569                 break;
1570         case PCI_DEVICE_ID_CENTAUR:
1571                 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1572                         m = (typeof(m)){"LP9002", "PCI",
1573                                         "Fibre Channel Adapter"};
1574                 else
1575                         m = (typeof(m)){"LP9000", "PCI",
1576                                         "Fibre Channel Adapter"};
1577                 break;
1578         case PCI_DEVICE_ID_RFLY:
1579                 m = (typeof(m)){"LP952", "PCI",
1580                                 "Fibre Channel Adapter"};
1581                 break;
1582         case PCI_DEVICE_ID_PEGASUS:
1583                 m = (typeof(m)){"LP9802", "PCI-X",
1584                                 "Fibre Channel Adapter"};
1585                 break;
1586         case PCI_DEVICE_ID_THOR:
1587                 m = (typeof(m)){"LP10000", "PCI-X",
1588                                 "Fibre Channel Adapter"};
1589                 break;
1590         case PCI_DEVICE_ID_VIPER:
1591                 m = (typeof(m)){"LPX1000",  "PCI-X",
1592                                 "Fibre Channel Adapter"};
1593                 break;
1594         case PCI_DEVICE_ID_PFLY:
1595                 m = (typeof(m)){"LP982", "PCI-X",
1596                                 "Fibre Channel Adapter"};
1597                 break;
1598         case PCI_DEVICE_ID_TFLY:
1599                 m = (typeof(m)){"LP1050", "PCI-X",
1600                                 "Fibre Channel Adapter"};
1601                 break;
1602         case PCI_DEVICE_ID_HELIOS:
1603                 m = (typeof(m)){"LP11000", "PCI-X2",
1604                                 "Fibre Channel Adapter"};
1605                 break;
1606         case PCI_DEVICE_ID_HELIOS_SCSP:
1607                 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1608                                 "Fibre Channel Adapter"};
1609                 break;
1610         case PCI_DEVICE_ID_HELIOS_DCSP:
1611                 m = (typeof(m)){"LP11002-SP",  "PCI-X2",
1612                                 "Fibre Channel Adapter"};
1613                 break;
1614         case PCI_DEVICE_ID_NEPTUNE:
1615                 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
1616                 break;
1617         case PCI_DEVICE_ID_NEPTUNE_SCSP:
1618                 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
1619                 break;
1620         case PCI_DEVICE_ID_NEPTUNE_DCSP:
1621                 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
1622                 break;
1623         case PCI_DEVICE_ID_BMID:
1624                 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
1625                 break;
1626         case PCI_DEVICE_ID_BSMB:
1627                 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
1628                 break;
1629         case PCI_DEVICE_ID_ZEPHYR:
1630                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1631                 break;
1632         case PCI_DEVICE_ID_ZEPHYR_SCSP:
1633                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1634                 break;
1635         case PCI_DEVICE_ID_ZEPHYR_DCSP:
1636                 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
1637                 GE = 1;
1638                 break;
1639         case PCI_DEVICE_ID_ZMID:
1640                 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
1641                 break;
1642         case PCI_DEVICE_ID_ZSMB:
1643                 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
1644                 break;
1645         case PCI_DEVICE_ID_LP101:
1646                 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
1647                 break;
1648         case PCI_DEVICE_ID_LP10000S:
1649                 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
1650                 break;
1651         case PCI_DEVICE_ID_LP11000S:
1652                 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
1653                 break;
1654         case PCI_DEVICE_ID_LPE11000S:
1655                 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
1656                 break;
1657         case PCI_DEVICE_ID_SAT:
1658                 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
1659                 break;
1660         case PCI_DEVICE_ID_SAT_MID:
1661                 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
1662                 break;
1663         case PCI_DEVICE_ID_SAT_SMB:
1664                 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
1665                 break;
1666         case PCI_DEVICE_ID_SAT_DCSP:
1667                 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
1668                 break;
1669         case PCI_DEVICE_ID_SAT_SCSP:
1670                 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
1671                 break;
1672         case PCI_DEVICE_ID_SAT_S:
1673                 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
1674                 break;
1675         case PCI_DEVICE_ID_HORNET:
1676                 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
1677                 GE = 1;
1678                 break;
1679         case PCI_DEVICE_ID_PROTEUS_VF:
1680                 m = (typeof(m)){"LPev12000", "PCIe IOV",
1681                                 "Fibre Channel Adapter"};
1682                 break;
1683         case PCI_DEVICE_ID_PROTEUS_PF:
1684                 m = (typeof(m)){"LPev12000", "PCIe IOV",
1685                                 "Fibre Channel Adapter"};
1686                 break;
1687         case PCI_DEVICE_ID_PROTEUS_S:
1688                 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1689                                 "Fibre Channel Adapter"};
1690                 break;
1691         case PCI_DEVICE_ID_TIGERSHARK:
1692                 oneConnect = 1;
1693                 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
1694                 break;
1695         case PCI_DEVICE_ID_TOMCAT:
1696                 oneConnect = 1;
1697                 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1698                 break;
1699         case PCI_DEVICE_ID_FALCON:
1700                 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1701                                 "EmulexSecure Fibre"};
1702                 break;
1703         default:
1704                 m = (typeof(m)){"Unknown", "", ""};
1705                 break;
1706         }
1707
1708         if (mdp && mdp[0] == '\0')
1709                 snprintf(mdp, 79,"%s", m.name);
1710         /* oneConnect hba requires special processing, they are all initiators
1711          * and we put the port number on the end
1712          */
1713         if (descp && descp[0] == '\0') {
1714                 if (oneConnect)
1715                         snprintf(descp, 255,
1716                                 "Emulex OneConnect %s, %s Initiator, Port %s",
1717                                 m.name, m.function,
1718                                 phba->Port);
1719                 else
1720                         snprintf(descp, 255,
1721                                 "Emulex %s %d%s %s %s",
1722                                 m.name, max_speed, (GE) ? "GE" : "Gb",
1723                                 m.bus, m.function);
1724         }
1725 }
1726
1727 /**
1728  * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
1729  * @phba: pointer to lpfc hba data structure.
1730  * @pring: pointer to a IOCB ring.
1731  * @cnt: the number of IOCBs to be posted to the IOCB ring.
1732  *
1733  * This routine posts a given number of IOCBs with the associated DMA buffer
1734  * descriptors specified by the cnt argument to the given IOCB ring.
1735  *
1736  * Return codes
1737  *   The number of IOCBs NOT able to be posted to the IOCB ring.
1738  **/
1739 int
1740 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
1741 {
1742         IOCB_t *icmd;
1743         struct lpfc_iocbq *iocb;
1744         struct lpfc_dmabuf *mp1, *mp2;
1745
1746         cnt += pring->missbufcnt;
1747
1748         /* While there are buffers to post */
1749         while (cnt > 0) {
1750                 /* Allocate buffer for  command iocb */
1751                 iocb = lpfc_sli_get_iocbq(phba);
1752                 if (iocb == NULL) {
1753                         pring->missbufcnt = cnt;
1754                         return cnt;
1755                 }
1756                 icmd = &iocb->iocb;
1757
1758                 /* 2 buffers can be posted per command */
1759                 /* Allocate buffer to post */
1760                 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1761                 if (mp1)
1762                     mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1763                 if (!mp1 || !mp1->virt) {
1764                         kfree(mp1);
1765                         lpfc_sli_release_iocbq(phba, iocb);
1766                         pring->missbufcnt = cnt;
1767                         return cnt;
1768                 }
1769
1770                 INIT_LIST_HEAD(&mp1->list);
1771                 /* Allocate buffer to post */
1772                 if (cnt > 1) {
1773                         mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1774                         if (mp2)
1775                                 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1776                                                             &mp2->phys);
1777                         if (!mp2 || !mp2->virt) {
1778                                 kfree(mp2);
1779                                 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1780                                 kfree(mp1);
1781                                 lpfc_sli_release_iocbq(phba, iocb);
1782                                 pring->missbufcnt = cnt;
1783                                 return cnt;
1784                         }
1785
1786                         INIT_LIST_HEAD(&mp2->list);
1787                 } else {
1788                         mp2 = NULL;
1789                 }
1790
1791                 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1792                 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1793                 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1794                 icmd->ulpBdeCount = 1;
1795                 cnt--;
1796                 if (mp2) {
1797                         icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1798                         icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1799                         icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1800                         cnt--;
1801                         icmd->ulpBdeCount = 2;
1802                 }
1803
1804                 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1805                 icmd->ulpLe = 1;
1806
1807                 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1808                     IOCB_ERROR) {
1809                         lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1810                         kfree(mp1);
1811                         cnt++;
1812                         if (mp2) {
1813                                 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1814                                 kfree(mp2);
1815                                 cnt++;
1816                         }
1817                         lpfc_sli_release_iocbq(phba, iocb);
1818                         pring->missbufcnt = cnt;
1819                         return cnt;
1820                 }
1821                 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1822                 if (mp2)
1823                         lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1824         }
1825         pring->missbufcnt = 0;
1826         return 0;
1827 }
1828
1829 /**
1830  * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
1831  * @phba: pointer to lpfc hba data structure.
1832  *
1833  * This routine posts initial receive IOCB buffers to the ELS ring. The
1834  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1835  * set to 64 IOCBs.
1836  *
1837  * Return codes
1838  *   0 - success (currently always success)
1839  **/
1840 static int
1841 lpfc_post_rcv_buf(struct lpfc_hba *phba)
1842 {
1843         struct lpfc_sli *psli = &phba->sli;
1844
1845         /* Ring 0, ELS / CT buffers */
1846         lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
1847         /* Ring 2 - FCP no buffers needed */
1848
1849         return 0;
1850 }
1851
1852 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1853
1854 /**
1855  * lpfc_sha_init - Set up initial array of hash table entries
1856  * @HashResultPointer: pointer to an array as hash table.
1857  *
1858  * This routine sets up the initial values to the array of hash table entries
1859  * for the LC HBAs.
1860  **/
1861 static void
1862 lpfc_sha_init(uint32_t * HashResultPointer)
1863 {
1864         HashResultPointer[0] = 0x67452301;
1865         HashResultPointer[1] = 0xEFCDAB89;
1866         HashResultPointer[2] = 0x98BADCFE;
1867         HashResultPointer[3] = 0x10325476;
1868         HashResultPointer[4] = 0xC3D2E1F0;
1869 }
1870
1871 /**
1872  * lpfc_sha_iterate - Iterate initial hash table with the working hash table
1873  * @HashResultPointer: pointer to an initial/result hash table.
1874  * @HashWorkingPointer: pointer to an working hash table.
1875  *
1876  * This routine iterates an initial hash table pointed by @HashResultPointer
1877  * with the values from the working hash table pointeed by @HashWorkingPointer.
1878  * The results are putting back to the initial hash table, returned through
1879  * the @HashResultPointer as the result hash table.
1880  **/
1881 static void
1882 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1883 {
1884         int t;
1885         uint32_t TEMP;
1886         uint32_t A, B, C, D, E;
1887         t = 16;
1888         do {
1889                 HashWorkingPointer[t] =
1890                     S(1,
1891                       HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1892                                                                      8] ^
1893                       HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1894         } while (++t <= 79);
1895         t = 0;
1896         A = HashResultPointer[0];
1897         B = HashResultPointer[1];
1898         C = HashResultPointer[2];
1899         D = HashResultPointer[3];
1900         E = HashResultPointer[4];
1901
1902         do {
1903                 if (t < 20) {
1904                         TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1905                 } else if (t < 40) {
1906                         TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1907                 } else if (t < 60) {
1908                         TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1909                 } else {
1910                         TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1911                 }
1912                 TEMP += S(5, A) + E + HashWorkingPointer[t];
1913                 E = D;
1914                 D = C;
1915                 C = S(30, B);
1916                 B = A;
1917                 A = TEMP;
1918         } while (++t <= 79);
1919
1920         HashResultPointer[0] += A;
1921         HashResultPointer[1] += B;
1922         HashResultPointer[2] += C;
1923         HashResultPointer[3] += D;
1924         HashResultPointer[4] += E;
1925
1926 }
1927
1928 /**
1929  * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
1930  * @RandomChallenge: pointer to the entry of host challenge random number array.
1931  * @HashWorking: pointer to the entry of the working hash array.
1932  *
1933  * This routine calculates the working hash array referred by @HashWorking
1934  * from the challenge random numbers associated with the host, referred by
1935  * @RandomChallenge. The result is put into the entry of the working hash
1936  * array and returned by reference through @HashWorking.
1937  **/
1938 static void
1939 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1940 {
1941         *HashWorking = (*RandomChallenge ^ *HashWorking);
1942 }
1943
1944 /**
1945  * lpfc_hba_init - Perform special handling for LC HBA initialization
1946  * @phba: pointer to lpfc hba data structure.
1947  * @hbainit: pointer to an array of unsigned 32-bit integers.
1948  *
1949  * This routine performs the special handling for LC HBA initialization.
1950  **/
1951 void
1952 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1953 {
1954         int t;
1955         uint32_t *HashWorking;
1956         uint32_t *pwwnn = (uint32_t *) phba->wwnn;
1957
1958         HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
1959         if (!HashWorking)
1960                 return;
1961
1962         HashWorking[0] = HashWorking[78] = *pwwnn++;
1963         HashWorking[1] = HashWorking[79] = *pwwnn;
1964
1965         for (t = 0; t < 7; t++)
1966                 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1967
1968         lpfc_sha_init(hbainit);
1969         lpfc_sha_iterate(hbainit, HashWorking);
1970         kfree(HashWorking);
1971 }
1972
1973 /**
1974  * lpfc_cleanup - Performs vport cleanups before deleting a vport
1975  * @vport: pointer to a virtual N_Port data structure.
1976  *
1977  * This routine performs the necessary cleanups before deleting the @vport.
1978  * It invokes the discovery state machine to perform necessary state
1979  * transitions and to release the ndlps associated with the @vport. Note,
1980  * the physical port is treated as @vport 0.
1981  **/
1982 void
1983 lpfc_cleanup(struct lpfc_vport *vport)
1984 {
1985         struct lpfc_hba   *phba = vport->phba;
1986         struct lpfc_nodelist *ndlp, *next_ndlp;
1987         int i = 0;
1988
1989         if (phba->link_state > LPFC_LINK_DOWN)
1990                 lpfc_port_link_failure(vport);
1991
1992         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1993                 if (!NLP_CHK_NODE_ACT(ndlp)) {
1994                         ndlp = lpfc_enable_node(vport, ndlp,
1995                                                 NLP_STE_UNUSED_NODE);
1996                         if (!ndlp)
1997                                 continue;
1998                         spin_lock_irq(&phba->ndlp_lock);
1999                         NLP_SET_FREE_REQ(ndlp);
2000                         spin_unlock_irq(&phba->ndlp_lock);
2001                         /* Trigger the release of the ndlp memory */
2002                         lpfc_nlp_put(ndlp);
2003                         continue;
2004                 }
2005                 spin_lock_irq(&phba->ndlp_lock);
2006                 if (NLP_CHK_FREE_REQ(ndlp)) {
2007                         /* The ndlp should not be in memory free mode already */
2008                         spin_unlock_irq(&phba->ndlp_lock);
2009                         continue;
2010                 } else
2011                         /* Indicate request for freeing ndlp memory */
2012                         NLP_SET_FREE_REQ(ndlp);
2013                 spin_unlock_irq(&phba->ndlp_lock);
2014
2015                 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2016                     ndlp->nlp_DID == Fabric_DID) {
2017                         /* Just free up ndlp with Fabric_DID for vports */
2018                         lpfc_nlp_put(ndlp);
2019                         continue;
2020                 }
2021
2022                 if (ndlp->nlp_type & NLP_FABRIC)
2023                         lpfc_disc_state_machine(vport, ndlp, NULL,
2024                                         NLP_EVT_DEVICE_RECOVERY);
2025
2026                 lpfc_disc_state_machine(vport, ndlp, NULL,
2027                                              NLP_EVT_DEVICE_RM);
2028
2029         }
2030
2031         /* At this point, ALL ndlp's should be gone
2032          * because of the previous NLP_EVT_DEVICE_RM.
2033          * Lets wait for this to happen, if needed.
2034          */
2035         while (!list_empty(&vport->fc_nodes)) {
2036                 if (i++ > 3000) {
2037                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2038                                 "0233 Nodelist not empty\n");
2039                         list_for_each_entry_safe(ndlp, next_ndlp,
2040                                                 &vport->fc_nodes, nlp_listp) {
2041                                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2042                                                 LOG_NODE,
2043                                                 "0282 did:x%x ndlp:x%p "
2044                                                 "usgmap:x%x refcnt:%d\n",
2045                                                 ndlp->nlp_DID, (void *)ndlp,
2046                                                 ndlp->nlp_usg_map,
2047                                                 atomic_read(
2048                                                         &ndlp->kref.refcount));
2049                         }
2050                         break;
2051                 }
2052
2053                 /* Wait for any activity on ndlps to settle */
2054                 msleep(10);
2055         }
2056 }
2057
2058 /**
2059  * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2060  * @vport: pointer to a virtual N_Port data structure.
2061  *
2062  * This routine stops all the timers associated with a @vport. This function
2063  * is invoked before disabling or deleting a @vport. Note that the physical
2064  * port is treated as @vport 0.
2065  **/
2066 void
2067 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2068 {
2069         del_timer_sync(&vport->els_tmofunc);
2070         del_timer_sync(&vport->fc_fdmitmo);
2071         lpfc_can_disctmo(vport);
2072         return;
2073 }
2074
2075 /**
2076  * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2077  * @phba: pointer to lpfc hba data structure.
2078  *
2079  * This routine stops all the timers associated with a HBA. This function is
2080  * invoked before either putting a HBA offline or unloading the driver.
2081  **/
2082 void
2083 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2084 {
2085         lpfc_stop_vport_timers(phba->pport);
2086         del_timer_sync(&phba->sli.mbox_tmo);
2087         del_timer_sync(&phba->fabric_block_timer);
2088         del_timer_sync(&phba->eratt_poll);
2089         del_timer_sync(&phba->hb_tmofunc);
2090         phba->hb_outstanding = 0;
2091
2092         switch (phba->pci_dev_grp) {
2093         case LPFC_PCI_DEV_LP:
2094                 /* Stop any LightPulse device specific driver timers */
2095                 del_timer_sync(&phba->fcp_poll_timer);
2096                 break;
2097         case LPFC_PCI_DEV_OC:
2098                 /* Stop any OneConnect device sepcific driver timers */
2099                 break;
2100         default:
2101                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2102                                 "0297 Invalid device group (x%x)\n",
2103                                 phba->pci_dev_grp);
2104                 break;
2105         }
2106         return;
2107 }
2108
2109 /**
2110  * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2111  * @phba: pointer to lpfc hba data structure.
2112  *
2113  * This routine marks a HBA's management interface as blocked. Once the HBA's
2114  * management interface is marked as blocked, all the user space access to
2115  * the HBA, whether they are from sysfs interface or libdfc interface will
2116  * all be blocked. The HBA is set to block the management interface when the
2117  * driver prepares the HBA interface for online or offline.
2118  **/
2119 static void
2120 lpfc_block_mgmt_io(struct lpfc_hba * phba)
2121 {
2122         unsigned long iflag;
2123
2124         spin_lock_irqsave(&phba->hbalock, iflag);
2125         phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2126         spin_unlock_irqrestore(&phba->hbalock, iflag);
2127 }
2128
2129 /**
2130  * lpfc_online - Initialize and bring a HBA online
2131  * @phba: pointer to lpfc hba data structure.
2132  *
2133  * This routine initializes the HBA and brings a HBA online. During this
2134  * process, the management interface is blocked to prevent user space access
2135  * to the HBA interfering with the driver initialization.
2136  *
2137  * Return codes
2138  *   0 - successful
2139  *   1 - failed
2140  **/
2141 int
2142 lpfc_online(struct lpfc_hba *phba)
2143 {
2144         struct lpfc_vport *vport;
2145         struct lpfc_vport **vports;
2146         int i;
2147
2148         if (!phba)
2149                 return 0;
2150         vport = phba->pport;
2151
2152         if (!(vport->fc_flag & FC_OFFLINE_MODE))
2153                 return 0;
2154
2155         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2156                         "0458 Bring Adapter online\n");
2157
2158         lpfc_block_mgmt_io(phba);
2159
2160         if (!lpfc_sli_queue_setup(phba)) {
2161                 lpfc_unblock_mgmt_io(phba);
2162                 return 1;
2163         }
2164
2165         if (phba->sli_rev == LPFC_SLI_REV4) {
2166                 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2167                         lpfc_unblock_mgmt_io(phba);
2168                         return 1;
2169                 }
2170         } else {
2171                 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2172                         lpfc_unblock_mgmt_io(phba);
2173                         return 1;
2174                 }
2175         }
2176
2177         vports = lpfc_create_vport_work_array(phba);
2178         if (vports != NULL)
2179                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2180                         struct Scsi_Host *shost;
2181                         shost = lpfc_shost_from_vport(vports[i]);
2182                         spin_lock_irq(shost->host_lock);
2183                         vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2184                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2185                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2186                         if (phba->sli_rev == LPFC_SLI_REV4)
2187                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
2188                         spin_unlock_irq(shost->host_lock);
2189                 }
2190                 lpfc_destroy_vport_work_array(phba, vports);
2191
2192         lpfc_unblock_mgmt_io(phba);
2193         return 0;
2194 }
2195
2196 /**
2197  * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
2198  * @phba: pointer to lpfc hba data structure.
2199  *
2200  * This routine marks a HBA's management interface as not blocked. Once the
2201  * HBA's management interface is marked as not blocked, all the user space
2202  * access to the HBA, whether they are from sysfs interface or libdfc
2203  * interface will be allowed. The HBA is set to block the management interface
2204  * when the driver prepares the HBA interface for online or offline and then
2205  * set to unblock the management interface afterwards.
2206  **/
2207 void
2208 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2209 {
2210         unsigned long iflag;
2211
2212         spin_lock_irqsave(&phba->hbalock, iflag);
2213         phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2214         spin_unlock_irqrestore(&phba->hbalock, iflag);
2215 }
2216
2217 /**
2218  * lpfc_offline_prep - Prepare a HBA to be brought offline
2219  * @phba: pointer to lpfc hba data structure.
2220  *
2221  * This routine is invoked to prepare a HBA to be brought offline. It performs
2222  * unregistration login to all the nodes on all vports and flushes the mailbox
2223  * queue to make it ready to be brought offline.
2224  **/
2225 void
2226 lpfc_offline_prep(struct lpfc_hba * phba)
2227 {
2228         struct lpfc_vport *vport = phba->pport;
2229         struct lpfc_nodelist  *ndlp, *next_ndlp;
2230         struct lpfc_vport **vports;
2231         int i;
2232
2233         if (vport->fc_flag & FC_OFFLINE_MODE)
2234                 return;
2235
2236         lpfc_block_mgmt_io(phba);
2237
2238         lpfc_linkdown(phba);
2239
2240         /* Issue an unreg_login to all nodes on all vports */
2241         vports = lpfc_create_vport_work_array(phba);
2242         if (vports != NULL) {
2243                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2244                         struct Scsi_Host *shost;
2245
2246                         if (vports[i]->load_flag & FC_UNLOADING)
2247                                 continue;
2248                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
2249                         shost = lpfc_shost_from_vport(vports[i]);
2250                         list_for_each_entry_safe(ndlp, next_ndlp,
2251                                                  &vports[i]->fc_nodes,
2252                                                  nlp_listp) {
2253                                 if (!NLP_CHK_NODE_ACT(ndlp))
2254                                         continue;
2255                                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2256                                         continue;
2257                                 if (ndlp->nlp_type & NLP_FABRIC) {
2258                                         lpfc_disc_state_machine(vports[i], ndlp,
2259                                                 NULL, NLP_EVT_DEVICE_RECOVERY);
2260                                         lpfc_disc_state_machine(vports[i], ndlp,
2261                                                 NULL, NLP_EVT_DEVICE_RM);
2262                                 }
2263                                 spin_lock_irq(shost->host_lock);
2264                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2265                                 spin_unlock_irq(shost->host_lock);
2266                                 lpfc_unreg_rpi(vports[i], ndlp);
2267                         }
2268                 }
2269         }
2270         lpfc_destroy_vport_work_array(phba, vports);
2271
2272         lpfc_sli_mbox_sys_shutdown(phba);
2273 }
2274
2275 /**
2276  * lpfc_offline - Bring a HBA offline
2277  * @phba: pointer to lpfc hba data structure.
2278  *
2279  * This routine actually brings a HBA offline. It stops all the timers
2280  * associated with the HBA, brings down the SLI layer, and eventually
2281  * marks the HBA as in offline state for the upper layer protocol.
2282  **/
2283 void
2284 lpfc_offline(struct lpfc_hba *phba)
2285 {
2286         struct Scsi_Host  *shost;
2287         struct lpfc_vport **vports;
2288         int i;
2289
2290         if (phba->pport->fc_flag & FC_OFFLINE_MODE)
2291                 return;
2292
2293         /* stop port and all timers associated with this hba */
2294         lpfc_stop_port(phba);
2295         vports = lpfc_create_vport_work_array(phba);
2296         if (vports != NULL)
2297                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
2298                         lpfc_stop_vport_timers(vports[i]);
2299         lpfc_destroy_vport_work_array(phba, vports);
2300         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2301                         "0460 Bring Adapter offline\n");
2302         /* Bring down the SLI Layer and cleanup.  The HBA is offline
2303            now.  */
2304         lpfc_sli_hba_down(phba);
2305         spin_lock_irq(&phba->hbalock);
2306         phba->work_ha = 0;
2307         spin_unlock_irq(&phba->hbalock);
2308         vports = lpfc_create_vport_work_array(phba);
2309         if (vports != NULL)
2310                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2311                         shost = lpfc_shost_from_vport(vports[i]);
2312                         spin_lock_irq(shost->host_lock);
2313                         vports[i]->work_port_events = 0;
2314                         vports[i]->fc_flag |= FC_OFFLINE_MODE;
2315                         spin_unlock_irq(shost->host_lock);
2316                 }
2317         lpfc_destroy_vport_work_array(phba, vports);
2318 }
2319
2320 /**
2321  * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
2322  * @phba: pointer to lpfc hba data structure.
2323  *
2324  * This routine is to free all the SCSI buffers and IOCBs from the driver
2325  * list back to kernel. It is called from lpfc_pci_remove_one to free
2326  * the internal resources before the device is removed from the system.
2327  *
2328  * Return codes
2329  *   0 - successful (for now, it always returns 0)
2330  **/
2331 static int
2332 lpfc_scsi_free(struct lpfc_hba *phba)
2333 {
2334         struct lpfc_scsi_buf *sb, *sb_next;
2335         struct lpfc_iocbq *io, *io_next;
2336
2337         spin_lock_irq(&phba->hbalock);
2338         /* Release all the lpfc_scsi_bufs maintained by this host. */
2339         spin_lock(&phba->scsi_buf_list_lock);
2340         list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2341                 list_del(&sb->list);
2342                 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2343                               sb->dma_handle);
2344                 kfree(sb);
2345                 phba->total_scsi_bufs--;
2346         }
2347         spin_unlock(&phba->scsi_buf_list_lock);
2348
2349         /* Release all the lpfc_iocbq entries maintained by this host. */
2350         list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2351                 list_del(&io->list);
2352                 kfree(io);
2353                 phba->total_iocbq_bufs--;
2354         }
2355         spin_unlock_irq(&phba->hbalock);
2356         return 0;
2357 }
2358
2359 /**
2360  * lpfc_create_port - Create an FC port
2361  * @phba: pointer to lpfc hba data structure.
2362  * @instance: a unique integer ID to this FC port.
2363  * @dev: pointer to the device data structure.
2364  *
2365  * This routine creates a FC port for the upper layer protocol. The FC port
2366  * can be created on top of either a physical port or a virtual port provided
2367  * by the HBA. This routine also allocates a SCSI host data structure (shost)
2368  * and associates the FC port created before adding the shost into the SCSI
2369  * layer.
2370  *
2371  * Return codes
2372  *   @vport - pointer to the virtual N_Port data structure.
2373  *   NULL - port create failed.
2374  **/
2375 struct lpfc_vport *
2376 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
2377 {
2378         struct lpfc_vport *vport;
2379         struct Scsi_Host  *shost;
2380         int error = 0;
2381
2382         if (dev != &phba->pcidev->dev)
2383                 shost = scsi_host_alloc(&lpfc_vport_template,
2384                                         sizeof(struct lpfc_vport));
2385         else
2386                 shost = scsi_host_alloc(&lpfc_template,
2387                                         sizeof(struct lpfc_vport));
2388         if (!shost)
2389                 goto out;
2390
2391         vport = (struct lpfc_vport *) shost->hostdata;
2392         vport->phba = phba;
2393         vport->load_flag |= FC_LOADING;
2394         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2395         vport->fc_rscn_flush = 0;
2396
2397         lpfc_get_vport_cfgparam(vport);
2398         shost->unique_id = instance;
2399         shost->max_id = LPFC_MAX_TARGET;
2400         shost->max_lun = vport->cfg_max_luns;
2401         shost->this_id = -1;
2402         shost->max_cmd_len = 16;
2403         if (phba->sli_rev == LPFC_SLI_REV4) {
2404                 shost->dma_boundary = LPFC_SLI4_MAX_SEGMENT_SIZE;
2405                 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2406         }
2407
2408         /*
2409          * Set initial can_queue value since 0 is no longer supported and
2410          * scsi_add_host will fail. This will be adjusted later based on the
2411          * max xri value determined in hba setup.
2412          */
2413         shost->can_queue = phba->cfg_hba_queue_depth - 10;
2414         if (dev != &phba->pcidev->dev) {
2415                 shost->transportt = lpfc_vport_transport_template;
2416                 vport->port_type = LPFC_NPIV_PORT;
2417         } else {
2418                 shost->transportt = lpfc_transport_template;
2419                 vport->port_type = LPFC_PHYSICAL_PORT;
2420         }
2421
2422         /* Initialize all internally managed lists. */
2423         INIT_LIST_HEAD(&vport->fc_nodes);
2424         INIT_LIST_HEAD(&vport->rcv_buffer_list);
2425         spin_lock_init(&vport->work_port_lock);
2426
2427         init_timer(&vport->fc_disctmo);
2428         vport->fc_disctmo.function = lpfc_disc_timeout;
2429         vport->fc_disctmo.data = (unsigned long)vport;
2430
2431         init_timer(&vport->fc_fdmitmo);
2432         vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
2433         vport->fc_fdmitmo.data = (unsigned long)vport;
2434
2435         init_timer(&vport->els_tmofunc);
2436         vport->els_tmofunc.function = lpfc_els_timeout;
2437         vport->els_tmofunc.data = (unsigned long)vport;
2438
2439         error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2440         if (error)
2441                 goto out_put_shost;
2442
2443         spin_lock_irq(&phba->hbalock);
2444         list_add_tail(&vport->listentry, &phba->port_list);
2445         spin_unlock_irq(&phba->hbalock);
2446         return vport;
2447
2448 out_put_shost:
2449         scsi_host_put(shost);
2450 out:
2451         return NULL;
2452 }
2453
2454 /**
2455  * destroy_port -  destroy an FC port
2456  * @vport: pointer to an lpfc virtual N_Port data structure.
2457  *
2458  * This routine destroys a FC port from the upper layer protocol. All the
2459  * resources associated with the port are released.
2460  **/
2461 void
2462 destroy_port(struct lpfc_vport *vport)
2463 {
2464         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2465         struct lpfc_hba  *phba = vport->phba;
2466
2467         lpfc_debugfs_terminate(vport);
2468         fc_remove_host(shost);
2469         scsi_remove_host(shost);
2470
2471         spin_lock_irq(&phba->hbalock);
2472         list_del_init(&vport->listentry);
2473         spin_unlock_irq(&phba->hbalock);
2474
2475         lpfc_cleanup(vport);
2476         return;
2477 }
2478
2479 /**
2480  * lpfc_get_instance - Get a unique integer ID
2481  *
2482  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2483  * uses the kernel idr facility to perform the task.
2484  *
2485  * Return codes:
2486  *   instance - a unique integer ID allocated as the new instance.
2487  *   -1 - lpfc get instance failed.
2488  **/
2489 int
2490 lpfc_get_instance(void)
2491 {
2492         int instance = 0;
2493
2494         /* Assign an unused number */
2495         if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2496                 return -1;
2497         if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2498                 return -1;
2499         return instance;
2500 }
2501
2502 /**
2503  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
2504  * @shost: pointer to SCSI host data structure.
2505  * @time: elapsed time of the scan in jiffies.
2506  *
2507  * This routine is called by the SCSI layer with a SCSI host to determine
2508  * whether the scan host is finished.
2509  *
2510  * Note: there is no scan_start function as adapter initialization will have
2511  * asynchronously kicked off the link initialization.
2512  *
2513  * Return codes
2514  *   0 - SCSI host scan is not over yet.
2515  *   1 - SCSI host scan is over.
2516  **/
2517 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2518 {
2519         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2520         struct lpfc_hba   *phba = vport->phba;
2521         int stat = 0;
2522
2523         spin_lock_irq(shost->host_lock);
2524
2525         if (vport->load_flag & FC_UNLOADING) {
2526                 stat = 1;
2527                 goto finished;
2528         }
2529         if (time >= 30 * HZ) {
2530                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2531                                 "0461 Scanning longer than 30 "
2532                                 "seconds.  Continuing initialization\n");
2533                 stat = 1;
2534                 goto finished;
2535         }
2536         if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2537                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2538                                 "0465 Link down longer than 15 "
2539                                 "seconds.  Continuing initialization\n");
2540                 stat = 1;
2541                 goto finished;
2542         }
2543
2544         if (vport->port_state != LPFC_VPORT_READY)
2545                 goto finished;
2546         if (vport->num_disc_nodes || vport->fc_prli_sent)
2547                 goto finished;
2548         if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2549                 goto finished;
2550         if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2551                 goto finished;
2552
2553         stat = 1;
2554
2555 finished:
2556         spin_unlock_irq(shost->host_lock);
2557         return stat;
2558 }
2559
2560 /**
2561  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
2562  * @shost: pointer to SCSI host data structure.
2563  *
2564  * This routine initializes a given SCSI host attributes on a FC port. The
2565  * SCSI host can be either on top of a physical port or a virtual port.
2566  **/
2567 void lpfc_host_attrib_init(struct Scsi_Host *shost)
2568 {
2569         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2570         struct lpfc_hba   *phba = vport->phba;
2571         /*
2572          * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
2573          */
2574
2575         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2576         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2577         fc_host_supported_classes(shost) = FC_COS_CLASS3;
2578
2579         memset(fc_host_supported_fc4s(shost), 0,
2580                sizeof(fc_host_supported_fc4s(shost)));
2581         fc_host_supported_fc4s(shost)[2] = 1;
2582         fc_host_supported_fc4s(shost)[7] = 1;
2583
2584         lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2585                                  sizeof fc_host_symbolic_name(shost));
2586
2587         fc_host_supported_speeds(shost) = 0;
2588         if (phba->lmt & LMT_10Gb)
2589                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2590         if (phba->lmt & LMT_8Gb)
2591                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2592         if (phba->lmt & LMT_4Gb)
2593                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2594         if (phba->lmt & LMT_2Gb)
2595                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2596         if (phba->lmt & LMT_1Gb)
2597                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2598
2599         fc_host_maxframe_size(shost) =
2600                 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2601                 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2602
2603         /* This value is also unchanging */
2604         memset(fc_host_active_fc4s(shost), 0,
2605                sizeof(fc_host_active_fc4s(shost)));
2606         fc_host_active_fc4s(shost)[2] = 1;
2607         fc_host_active_fc4s(shost)[7] = 1;
2608
2609         fc_host_max_npiv_vports(shost) = phba->max_vpi;
2610         spin_lock_irq(shost->host_lock);
2611         vport->load_flag &= ~FC_LOADING;
2612         spin_unlock_irq(shost->host_lock);
2613 }
2614
2615 /**
2616  * lpfc_stop_port_s3 - Stop SLI3 device port
2617  * @phba: pointer to lpfc hba data structure.
2618  *
2619  * This routine is invoked to stop an SLI3 device port, it stops the device
2620  * from generating interrupts and stops the device driver's timers for the
2621  * device.
2622  **/
2623 static void
2624 lpfc_stop_port_s3(struct lpfc_hba *phba)
2625 {
2626         /* Clear all interrupt enable conditions */
2627         writel(0, phba->HCregaddr);
2628         readl(phba->HCregaddr); /* flush */
2629         /* Clear all pending interrupts */
2630         writel(0xffffffff, phba->HAregaddr);
2631         readl(phba->HAregaddr); /* flush */
2632
2633         /* Reset some HBA SLI setup states */
2634         lpfc_stop_hba_timers(phba);
2635         phba->pport->work_port_events = 0;
2636 }
2637
2638 /**
2639  * lpfc_stop_port_s4 - Stop SLI4 device port
2640  * @phba: pointer to lpfc hba data structure.
2641  *
2642  * This routine is invoked to stop an SLI4 device port, it stops the device
2643  * from generating interrupts and stops the device driver's timers for the
2644  * device.
2645  **/
2646 static void
2647 lpfc_stop_port_s4(struct lpfc_hba *phba)
2648 {
2649         /* Reset some HBA SLI4 setup states */
2650         lpfc_stop_hba_timers(phba);
2651         phba->pport->work_port_events = 0;
2652         phba->sli4_hba.intr_enable = 0;
2653         /* Hard clear it for now, shall have more graceful way to wait later */
2654         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2655 }
2656
2657 /**
2658  * lpfc_stop_port - Wrapper function for stopping hba port
2659  * @phba: Pointer to HBA context object.
2660  *
2661  * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2662  * the API jump table function pointer from the lpfc_hba struct.
2663  **/
2664 void
2665 lpfc_stop_port(struct lpfc_hba *phba)
2666 {
2667         phba->lpfc_stop_port(phba);
2668 }
2669
2670 /**
2671  * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2672  * @phba: pointer to lpfc hba data structure.
2673  *
2674  * This routine is invoked to remove the driver default fcf record from
2675  * the port.  This routine currently acts on FCF Index 0.
2676  *
2677  **/
2678 void
2679 lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2680 {
2681         int rc = 0;
2682         LPFC_MBOXQ_t *mboxq;
2683         struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2684         uint32_t mbox_tmo, req_len;
2685         uint32_t shdr_status, shdr_add_status;
2686
2687         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2688         if (!mboxq) {
2689                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2690                         "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2691                 return;
2692         }
2693
2694         req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2695                   sizeof(struct lpfc_sli4_cfg_mhdr);
2696         rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2697                               LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2698                               req_len, LPFC_SLI4_MBX_EMBED);
2699         /*
2700          * In phase 1, there is a single FCF index, 0.  In phase2, the driver
2701          * supports multiple FCF indices.
2702          */
2703         del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2704         bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2705         bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2706                phba->fcf.fcf_indx);
2707
2708         if (!phba->sli4_hba.intr_enable)
2709                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2710         else {
2711                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2712                 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2713         }
2714         /* The IOCTL status is embedded in the mailbox subheader. */
2715         shdr_status = bf_get(lpfc_mbox_hdr_status,
2716                              &del_fcf_record->header.cfg_shdr.response);
2717         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2718                                  &del_fcf_record->header.cfg_shdr.response);
2719         if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2720                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2721                                 "2516 DEL FCF of default FCF Index failed "
2722                                 "mbx status x%x, status x%x add_status x%x\n",
2723                                 rc, shdr_status, shdr_add_status);
2724         }
2725         if (rc != MBX_TIMEOUT)
2726                 mempool_free(mboxq, phba->mbox_mem_pool);
2727 }
2728
2729 /**
2730  * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2731  * @phba: pointer to lpfc hba data structure.
2732  *
2733  * This function uses the QUERY_FW_CFG mailbox command to determine if the
2734  * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2735  * was successful and the firmware supports FCoE. Any other return indicates
2736  * a error. It is assumed that this function will be called before interrupts
2737  * are enabled.
2738  **/
2739 static int
2740 lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2741 {
2742         int rc = 0;
2743         LPFC_MBOXQ_t *mboxq;
2744         struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2745         uint32_t length;
2746         uint32_t shdr_status, shdr_add_status;
2747
2748         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2749         if (!mboxq) {
2750                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2751                                 "2621 Failed to allocate mbox for "
2752                                 "query firmware config cmd\n");
2753                 return -ENOMEM;
2754         }
2755         query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2756         length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2757                   sizeof(struct lpfc_sli4_cfg_mhdr));
2758         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2759                          LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2760                          length, LPFC_SLI4_MBX_EMBED);
2761         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2762         /* The IOCTL status is embedded in the mailbox subheader. */
2763         shdr_status = bf_get(lpfc_mbox_hdr_status,
2764                              &query_fw_cfg->header.cfg_shdr.response);
2765         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2766                                  &query_fw_cfg->header.cfg_shdr.response);
2767         if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2768                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2769                                 "2622 Query Firmware Config failed "
2770                                 "mbx status x%x, status x%x add_status x%x\n",
2771                                 rc, shdr_status, shdr_add_status);
2772                 return -EINVAL;
2773         }
2774         if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2775                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2776                                 "2623 FCoE Function not supported by firmware. "
2777                                 "Function mode = %08x\n",
2778                                 query_fw_cfg->function_mode);
2779                 return -EINVAL;
2780         }
2781         if (rc != MBX_TIMEOUT)
2782                 mempool_free(mboxq, phba->mbox_mem_pool);
2783         return 0;
2784 }
2785
2786 /**
2787  * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2788  * @phba: pointer to lpfc hba data structure.
2789  * @acqe_link: pointer to the async link completion queue entry.
2790  *
2791  * This routine is to parse the SLI4 link-attention link fault code and
2792  * translate it into the base driver's read link attention mailbox command
2793  * status.
2794  *
2795  * Return: Link-attention status in terms of base driver's coding.
2796  **/
2797 static uint16_t
2798 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2799                            struct lpfc_acqe_link *acqe_link)
2800 {
2801         uint16_t latt_fault;
2802
2803         switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2804         case LPFC_ASYNC_LINK_FAULT_NONE:
2805         case LPFC_ASYNC_LINK_FAULT_LOCAL:
2806         case LPFC_ASYNC_LINK_FAULT_REMOTE:
2807                 latt_fault = 0;
2808                 break;
2809         default:
2810                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2811                                 "0398 Invalid link fault code: x%x\n",
2812                                 bf_get(lpfc_acqe_link_fault, acqe_link));
2813                 latt_fault = MBXERR_ERROR;
2814                 break;
2815         }
2816         return latt_fault;
2817 }
2818
2819 /**
2820  * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
2821  * @phba: pointer to lpfc hba data structure.
2822  * @acqe_link: pointer to the async link completion queue entry.
2823  *
2824  * This routine is to parse the SLI4 link attention type and translate it
2825  * into the base driver's link attention type coding.
2826  *
2827  * Return: Link attention type in terms of base driver's coding.
2828  **/
2829 static uint8_t
2830 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2831                           struct lpfc_acqe_link *acqe_link)
2832 {
2833         uint8_t att_type;
2834
2835         switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2836         case LPFC_ASYNC_LINK_STATUS_DOWN:
2837         case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2838                 att_type = AT_LINK_DOWN;
2839                 break;
2840         case LPFC_ASYNC_LINK_STATUS_UP:
2841                 /* Ignore physical link up events - wait for logical link up */
2842                 att_type = AT_RESERVED;
2843                 break;
2844         case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2845                 att_type = AT_LINK_UP;
2846                 break;
2847         default:
2848                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2849                                 "0399 Invalid link attention type: x%x\n",
2850                                 bf_get(lpfc_acqe_link_status, acqe_link));
2851                 att_type = AT_RESERVED;
2852                 break;
2853         }
2854         return att_type;
2855 }
2856
2857 /**
2858  * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
2859  * @phba: pointer to lpfc hba data structure.
2860  * @acqe_link: pointer to the async link completion queue entry.
2861  *
2862  * This routine is to parse the SLI4 link-attention link speed and translate
2863  * it into the base driver's link-attention link speed coding.
2864  *
2865  * Return: Link-attention link speed in terms of base driver's coding.
2866  **/
2867 static uint8_t
2868 lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2869                                 struct lpfc_acqe_link *acqe_link)
2870 {
2871         uint8_t link_speed;
2872
2873         switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2874         case LPFC_ASYNC_LINK_SPEED_ZERO:
2875                 link_speed = LA_UNKNW_LINK;
2876                 break;
2877         case LPFC_ASYNC_LINK_SPEED_10MBPS:
2878                 link_speed = LA_UNKNW_LINK;
2879                 break;
2880         case LPFC_ASYNC_LINK_SPEED_100MBPS:
2881                 link_speed = LA_UNKNW_LINK;
2882                 break;
2883         case LPFC_ASYNC_LINK_SPEED_1GBPS:
2884                 link_speed = LA_1GHZ_LINK;
2885                 break;
2886         case LPFC_ASYNC_LINK_SPEED_10GBPS:
2887                 link_speed = LA_10GHZ_LINK;
2888                 break;
2889         default:
2890                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2891                                 "0483 Invalid link-attention link speed: x%x\n",
2892                                 bf_get(lpfc_acqe_link_speed, acqe_link));
2893                 link_speed = LA_UNKNW_LINK;
2894                 break;
2895         }
2896         return link_speed;
2897 }
2898
2899 /**
2900  * lpfc_sli4_async_link_evt - Process the asynchronous link event
2901  * @phba: pointer to lpfc hba data structure.
2902  * @acqe_link: pointer to the async link completion queue entry.
2903  *
2904  * This routine is to handle the SLI4 asynchronous link event.
2905  **/
2906 static void
2907 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
2908                          struct lpfc_acqe_link *acqe_link)
2909 {
2910         struct lpfc_dmabuf *mp;
2911         LPFC_MBOXQ_t *pmb;
2912         MAILBOX_t *mb;
2913         READ_LA_VAR *la;
2914         uint8_t att_type;
2915
2916         att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
2917         if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
2918                 return;
2919         phba->fcoe_eventtag = acqe_link->event_tag;
2920         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2921         if (!pmb) {
2922                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2923                                 "0395 The mboxq allocation failed\n");
2924                 return;
2925         }
2926         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2927         if (!mp) {
2928                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2929                                 "0396 The lpfc_dmabuf allocation failed\n");
2930                 goto out_free_pmb;
2931         }
2932         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2933         if (!mp->virt) {
2934                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2935                                 "0397 The mbuf allocation failed\n");
2936                 goto out_free_dmabuf;
2937         }
2938
2939         /* Cleanup any outstanding ELS commands */
2940         lpfc_els_flush_all_cmd(phba);
2941
2942         /* Block ELS IOCBs until we have done process link event */
2943         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2944
2945         /* Update link event statistics */
2946         phba->sli.slistat.link_event++;
2947
2948         /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
2949         lpfc_read_la(phba, pmb, mp);
2950         pmb->vport = phba->pport;
2951
2952         /* Parse and translate status field */
2953         mb = &pmb->u.mb;
2954         mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
2955
2956         /* Parse and translate link attention fields */
2957         la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2958         la->eventTag = acqe_link->event_tag;
2959         la->attType = att_type;
2960         la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
2961
2962         /* Fake the the following irrelvant fields */
2963         la->topology = TOPOLOGY_PT_PT;
2964         la->granted_AL_PA = 0;
2965         la->il = 0;
2966         la->pb = 0;
2967         la->fa = 0;
2968         la->mm = 0;
2969
2970         /* Keep the link status for extra SLI4 state machine reference */
2971         phba->sli4_hba.link_state.speed =
2972                                 bf_get(lpfc_acqe_link_speed, acqe_link);
2973         phba->sli4_hba.link_state.duplex =
2974                                 bf_get(lpfc_acqe_link_duplex, acqe_link);
2975         phba->sli4_hba.link_state.status =
2976                                 bf_get(lpfc_acqe_link_status, acqe_link);
2977         phba->sli4_hba.link_state.physical =
2978                                 bf_get(lpfc_acqe_link_physical, acqe_link);
2979         phba->sli4_hba.link_state.fault =
2980                                 bf_get(lpfc_acqe_link_fault, acqe_link);
2981
2982         /* Invoke the lpfc_handle_latt mailbox command callback function */
2983         lpfc_mbx_cmpl_read_la(phba, pmb);
2984
2985         return;
2986
2987 out_free_dmabuf:
2988         kfree(mp);
2989 out_free_pmb:
2990         mempool_free(pmb, phba->mbox_mem_pool);
2991 }
2992
2993 /**
2994  * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
2995  * @phba: pointer to lpfc hba data structure.
2996  * @acqe_link: pointer to the async fcoe completion queue entry.
2997  *
2998  * This routine is to handle the SLI4 asynchronous fcoe event.
2999  **/
3000 static void
3001 lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3002                          struct lpfc_acqe_fcoe *acqe_fcoe)
3003 {
3004         uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3005         int rc;
3006         struct lpfc_vport *vport;
3007         struct lpfc_nodelist *ndlp;
3008         struct Scsi_Host  *shost;
3009
3010         phba->fc_eventTag = acqe_fcoe->event_tag;
3011         phba->fcoe_eventtag = acqe_fcoe->event_tag;
3012         switch (event_type) {
3013         case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
3014                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3015                         "2546 New FCF found index 0x%x tag 0x%x\n",
3016                         acqe_fcoe->index,
3017                         acqe_fcoe->event_tag);
3018                 /*
3019                  * If the current FCF is in discovered state, or
3020                  * FCF discovery is in progress do nothing.
3021                  */
3022                 spin_lock_irq(&phba->hbalock);
3023                 if ((phba->fcf.fcf_flag & FCF_DISCOVERED) ||
3024                    (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3025                         spin_unlock_irq(&phba->hbalock);
3026                         break;
3027                 }
3028                 spin_unlock_irq(&phba->hbalock);
3029
3030                 /* Read the FCF table and re-discover SAN. */
3031                 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
3032                 if (rc)
3033                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3034                                         "2547 Read FCF record failed 0x%x\n",
3035                                         rc);
3036                 break;
3037
3038         case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3039                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3040                         "2548 FCF Table full count 0x%x tag 0x%x\n",
3041                         bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3042                         acqe_fcoe->event_tag);
3043                 break;
3044
3045         case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3046                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3047                         "2549 FCF disconnected from network index 0x%x"
3048                         " tag 0x%x\n", acqe_fcoe->index,
3049                         acqe_fcoe->event_tag);
3050                 /* If the event is not for currently used fcf do nothing */
3051                 if (phba->fcf.fcf_indx != acqe_fcoe->index)
3052                         break;
3053                 /*
3054                  * Currently, driver support only one FCF - so treat this as
3055                  * a link down.
3056                  */
3057                 lpfc_linkdown(phba);
3058                 /* Unregister FCF if no devices connected to it */
3059                 lpfc_unregister_unused_fcf(phba);
3060                 break;
3061         case LPFC_FCOE_EVENT_TYPE_CVL:
3062                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3063                         "2718 Clear Virtual Link Received for VPI 0x%x"
3064                         " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3065                 vport = lpfc_find_vport_by_vpid(phba,
3066                                 acqe_fcoe->index - phba->vpi_base);
3067                 if (!vport)
3068                         break;
3069                 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3070                 if (!ndlp)
3071                         break;
3072                 shost = lpfc_shost_from_vport(vport);
3073                 lpfc_linkdown_port(vport);
3074                 if (vport->port_type != LPFC_NPIV_PORT) {
3075                         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3076                         spin_lock_irq(shost->host_lock);
3077                         ndlp->nlp_flag |= NLP_DELAY_TMO;
3078                         spin_unlock_irq(shost->host_lock);
3079                         ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
3080                         vport->port_state = LPFC_FLOGI;
3081                 }
3082                 break;
3083         default:
3084                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3085                         "0288 Unknown FCoE event type 0x%x event tag "
3086                         "0x%x\n", event_type, acqe_fcoe->event_tag);
3087                 break;
3088         }
3089 }
3090
3091 /**
3092  * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3093  * @phba: pointer to lpfc hba data structure.
3094  * @acqe_link: pointer to the async dcbx completion queue entry.
3095  *
3096  * This routine is to handle the SLI4 asynchronous dcbx event.
3097  **/
3098 static void
3099 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3100                          struct lpfc_acqe_dcbx *acqe_dcbx)
3101 {
3102         phba->fc_eventTag = acqe_dcbx->event_tag;
3103         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3104                         "0290 The SLI4 DCBX asynchronous event is not "
3105                         "handled yet\n");
3106 }
3107
3108 /**
3109  * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3110  * @phba: pointer to lpfc hba data structure.
3111  *
3112  * This routine is invoked by the worker thread to process all the pending
3113  * SLI4 asynchronous events.
3114  **/
3115 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3116 {
3117         struct lpfc_cq_event *cq_event;
3118
3119         /* First, declare the async event has been handled */
3120         spin_lock_irq(&phba->hbalock);
3121         phba->hba_flag &= ~ASYNC_EVENT;
3122         spin_unlock_irq(&phba->hbalock);
3123         /* Now, handle all the async events */
3124         while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3125                 /* Get the first event from the head of the event queue */
3126                 spin_lock_irq(&phba->hbalock);
3127                 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3128                                  cq_event, struct lpfc_cq_event, list);
3129                 spin_unlock_irq(&phba->hbalock);
3130                 /* Process the asynchronous event */
3131                 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3132                 case LPFC_TRAILER_CODE_LINK:
3133                         lpfc_sli4_async_link_evt(phba,
3134                                                  &cq_event->cqe.acqe_link);
3135                         break;
3136                 case LPFC_TRAILER_CODE_FCOE:
3137                         lpfc_sli4_async_fcoe_evt(phba,
3138                                                  &cq_event->cqe.acqe_fcoe);
3139                         break;
3140                 case LPFC_TRAILER_CODE_DCBX:
3141                         lpfc_sli4_async_dcbx_evt(phba,
3142                                                  &cq_event->cqe.acqe_dcbx);
3143