Merge tag 'for-3.7' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaas/pci
[linux-3.10.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-2012 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/module.h>
28 #include <linux/kthread.h>
29 #include <linux/pci.h>
30 #include <linux/spinlock.h>
31 #include <linux/ctype.h>
32 #include <linux/aer.h>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
35 #include <linux/miscdevice.h>
36
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_transport_fc.h>
41
42 #include "lpfc_hw4.h"
43 #include "lpfc_hw.h"
44 #include "lpfc_sli.h"
45 #include "lpfc_sli4.h"
46 #include "lpfc_nl.h"
47 #include "lpfc_disc.h"
48 #include "lpfc_scsi.h"
49 #include "lpfc.h"
50 #include "lpfc_logmsg.h"
51 #include "lpfc_crtn.h"
52 #include "lpfc_vport.h"
53 #include "lpfc_version.h"
54
55 char *_dump_buf_data;
56 unsigned long _dump_buf_data_order;
57 char *_dump_buf_dif;
58 unsigned long _dump_buf_dif_order;
59 spinlock_t _dump_buf_lock;
60
61 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
62 static int lpfc_post_rcv_buf(struct lpfc_hba *);
63 static int lpfc_sli4_queue_verify(struct lpfc_hba *);
64 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
65 static int lpfc_setup_endian_order(struct lpfc_hba *);
66 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
67 static void lpfc_free_els_sgl_list(struct lpfc_hba *);
68 static void lpfc_init_sgl_list(struct lpfc_hba *);
69 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
70 static void lpfc_free_active_sgl(struct lpfc_hba *);
71 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
72 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
73 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
74 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
75 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
76 static void lpfc_sli4_disable_intr(struct lpfc_hba *);
77 static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
78
79 static struct scsi_transport_template *lpfc_transport_template = NULL;
80 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
81 static DEFINE_IDR(lpfc_hba_index);
82
83 /**
84  * lpfc_config_port_prep - Perform lpfc initialization prior to config port
85  * @phba: pointer to lpfc hba data structure.
86  *
87  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
88  * mailbox command. It retrieves the revision information from the HBA and
89  * collects the Vital Product Data (VPD) about the HBA for preparing the
90  * configuration of the HBA.
91  *
92  * Return codes:
93  *   0 - success.
94  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
95  *   Any other value - indicates an error.
96  **/
97 int
98 lpfc_config_port_prep(struct lpfc_hba *phba)
99 {
100         lpfc_vpd_t *vp = &phba->vpd;
101         int i = 0, rc;
102         LPFC_MBOXQ_t *pmb;
103         MAILBOX_t *mb;
104         char *lpfc_vpd_data = NULL;
105         uint16_t offset = 0;
106         static char licensed[56] =
107                     "key unlock for use with gnu public licensed code only\0";
108         static int init_key = 1;
109
110         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
111         if (!pmb) {
112                 phba->link_state = LPFC_HBA_ERROR;
113                 return -ENOMEM;
114         }
115
116         mb = &pmb->u.mb;
117         phba->link_state = LPFC_INIT_MBX_CMDS;
118
119         if (lpfc_is_LC_HBA(phba->pcidev->device)) {
120                 if (init_key) {
121                         uint32_t *ptext = (uint32_t *) licensed;
122
123                         for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
124                                 *ptext = cpu_to_be32(*ptext);
125                         init_key = 0;
126                 }
127
128                 lpfc_read_nv(phba, pmb);
129                 memset((char*)mb->un.varRDnvp.rsvd3, 0,
130                         sizeof (mb->un.varRDnvp.rsvd3));
131                 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
132                          sizeof (licensed));
133
134                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
135
136                 if (rc != MBX_SUCCESS) {
137                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
138                                         "0324 Config Port initialization "
139                                         "error, mbxCmd x%x READ_NVPARM, "
140                                         "mbxStatus x%x\n",
141                                         mb->mbxCommand, mb->mbxStatus);
142                         mempool_free(pmb, phba->mbox_mem_pool);
143                         return -ERESTART;
144                 }
145                 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
146                        sizeof(phba->wwnn));
147                 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
148                        sizeof(phba->wwpn));
149         }
150
151         phba->sli3_options = 0x0;
152
153         /* Setup and issue mailbox READ REV command */
154         lpfc_read_rev(phba, pmb);
155         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
156         if (rc != MBX_SUCCESS) {
157                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
158                                 "0439 Adapter failed to init, mbxCmd x%x "
159                                 "READ_REV, mbxStatus x%x\n",
160                                 mb->mbxCommand, mb->mbxStatus);
161                 mempool_free( pmb, phba->mbox_mem_pool);
162                 return -ERESTART;
163         }
164
165
166         /*
167          * The value of rr must be 1 since the driver set the cv field to 1.
168          * This setting requires the FW to set all revision fields.
169          */
170         if (mb->un.varRdRev.rr == 0) {
171                 vp->rev.rBit = 0;
172                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
173                                 "0440 Adapter failed to init, READ_REV has "
174                                 "missing revision information.\n");
175                 mempool_free(pmb, phba->mbox_mem_pool);
176                 return -ERESTART;
177         }
178
179         if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
180                 mempool_free(pmb, phba->mbox_mem_pool);
181                 return -EINVAL;
182         }
183
184         /* Save information as VPD data */
185         vp->rev.rBit = 1;
186         memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
187         vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
188         memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
189         vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
190         memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
191         vp->rev.biuRev = mb->un.varRdRev.biuRev;
192         vp->rev.smRev = mb->un.varRdRev.smRev;
193         vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
194         vp->rev.endecRev = mb->un.varRdRev.endecRev;
195         vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
196         vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
197         vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
198         vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
199         vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
200         vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
201
202         /* If the sli feature level is less then 9, we must
203          * tear down all RPIs and VPIs on link down if NPIV
204          * is enabled.
205          */
206         if (vp->rev.feaLevelHigh < 9)
207                 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
208
209         if (lpfc_is_LC_HBA(phba->pcidev->device))
210                 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
211                                                 sizeof (phba->RandomData));
212
213         /* Get adapter VPD information */
214         lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
215         if (!lpfc_vpd_data)
216                 goto out_free_mbox;
217         do {
218                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
219                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
220
221                 if (rc != MBX_SUCCESS) {
222                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
223                                         "0441 VPD not present on adapter, "
224                                         "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
225                                         mb->mbxCommand, mb->mbxStatus);
226                         mb->un.varDmp.word_cnt = 0;
227                 }
228                 /* dump mem may return a zero when finished or we got a
229                  * mailbox error, either way we are done.
230                  */
231                 if (mb->un.varDmp.word_cnt == 0)
232                         break;
233                 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
234                         mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
235                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
236                                       lpfc_vpd_data + offset,
237                                       mb->un.varDmp.word_cnt);
238                 offset += mb->un.varDmp.word_cnt;
239         } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
240         lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
241
242         kfree(lpfc_vpd_data);
243 out_free_mbox:
244         mempool_free(pmb, phba->mbox_mem_pool);
245         return 0;
246 }
247
248 /**
249  * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
250  * @phba: pointer to lpfc hba data structure.
251  * @pmboxq: pointer to the driver internal queue element for mailbox command.
252  *
253  * This is the completion handler for driver's configuring asynchronous event
254  * mailbox command to the device. If the mailbox command returns successfully,
255  * it will set internal async event support flag to 1; otherwise, it will
256  * set internal async event support flag to 0.
257  **/
258 static void
259 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
260 {
261         if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
262                 phba->temp_sensor_support = 1;
263         else
264                 phba->temp_sensor_support = 0;
265         mempool_free(pmboxq, phba->mbox_mem_pool);
266         return;
267 }
268
269 /**
270  * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
271  * @phba: pointer to lpfc hba data structure.
272  * @pmboxq: pointer to the driver internal queue element for mailbox command.
273  *
274  * This is the completion handler for dump mailbox command for getting
275  * wake up parameters. When this command complete, the response contain
276  * Option rom version of the HBA. This function translate the version number
277  * into a human readable string and store it in OptionROMVersion.
278  **/
279 static void
280 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
281 {
282         struct prog_id *prg;
283         uint32_t prog_id_word;
284         char dist = ' ';
285         /* character array used for decoding dist type. */
286         char dist_char[] = "nabx";
287
288         if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
289                 mempool_free(pmboxq, phba->mbox_mem_pool);
290                 return;
291         }
292
293         prg = (struct prog_id *) &prog_id_word;
294
295         /* word 7 contain option rom version */
296         prog_id_word = pmboxq->u.mb.un.varWords[7];
297
298         /* Decode the Option rom version word to a readable string */
299         if (prg->dist < 4)
300                 dist = dist_char[prg->dist];
301
302         if ((prg->dist == 3) && (prg->num == 0))
303                 sprintf(phba->OptionROMVersion, "%d.%d%d",
304                         prg->ver, prg->rev, prg->lev);
305         else
306                 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
307                         prg->ver, prg->rev, prg->lev,
308                         dist, prg->num);
309         mempool_free(pmboxq, phba->mbox_mem_pool);
310         return;
311 }
312
313 /**
314  * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
315  *      cfg_soft_wwnn, cfg_soft_wwpn
316  * @vport: pointer to lpfc vport data structure.
317  *
318  *
319  * Return codes
320  *   None.
321  **/
322 void
323 lpfc_update_vport_wwn(struct lpfc_vport *vport)
324 {
325         /* If the soft name exists then update it using the service params */
326         if (vport->phba->cfg_soft_wwnn)
327                 u64_to_wwn(vport->phba->cfg_soft_wwnn,
328                            vport->fc_sparam.nodeName.u.wwn);
329         if (vport->phba->cfg_soft_wwpn)
330                 u64_to_wwn(vport->phba->cfg_soft_wwpn,
331                            vport->fc_sparam.portName.u.wwn);
332
333         /*
334          * If the name is empty or there exists a soft name
335          * then copy the service params name, otherwise use the fc name
336          */
337         if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
338                 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
339                         sizeof(struct lpfc_name));
340         else
341                 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
342                         sizeof(struct lpfc_name));
343
344         if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
345                 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
346                         sizeof(struct lpfc_name));
347         else
348                 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
349                         sizeof(struct lpfc_name));
350 }
351
352 /**
353  * lpfc_config_port_post - Perform lpfc initialization after config port
354  * @phba: pointer to lpfc hba data structure.
355  *
356  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
357  * command call. It performs all internal resource and state setups on the
358  * port: post IOCB buffers, enable appropriate host interrupt attentions,
359  * ELS ring timers, etc.
360  *
361  * Return codes
362  *   0 - success.
363  *   Any other value - error.
364  **/
365 int
366 lpfc_config_port_post(struct lpfc_hba *phba)
367 {
368         struct lpfc_vport *vport = phba->pport;
369         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
370         LPFC_MBOXQ_t *pmb;
371         MAILBOX_t *mb;
372         struct lpfc_dmabuf *mp;
373         struct lpfc_sli *psli = &phba->sli;
374         uint32_t status, timeout;
375         int i, j;
376         int rc;
377
378         spin_lock_irq(&phba->hbalock);
379         /*
380          * If the Config port completed correctly the HBA is not
381          * over heated any more.
382          */
383         if (phba->over_temp_state == HBA_OVER_TEMP)
384                 phba->over_temp_state = HBA_NORMAL_TEMP;
385         spin_unlock_irq(&phba->hbalock);
386
387         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
388         if (!pmb) {
389                 phba->link_state = LPFC_HBA_ERROR;
390                 return -ENOMEM;
391         }
392         mb = &pmb->u.mb;
393
394         /* Get login parameters for NID.  */
395         rc = lpfc_read_sparam(phba, pmb, 0);
396         if (rc) {
397                 mempool_free(pmb, phba->mbox_mem_pool);
398                 return -ENOMEM;
399         }
400
401         pmb->vport = vport;
402         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
403                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
404                                 "0448 Adapter failed init, mbxCmd x%x "
405                                 "READ_SPARM mbxStatus x%x\n",
406                                 mb->mbxCommand, mb->mbxStatus);
407                 phba->link_state = LPFC_HBA_ERROR;
408                 mp = (struct lpfc_dmabuf *) pmb->context1;
409                 mempool_free(pmb, phba->mbox_mem_pool);
410                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
411                 kfree(mp);
412                 return -EIO;
413         }
414
415         mp = (struct lpfc_dmabuf *) pmb->context1;
416
417         memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
418         lpfc_mbuf_free(phba, mp->virt, mp->phys);
419         kfree(mp);
420         pmb->context1 = NULL;
421         lpfc_update_vport_wwn(vport);
422
423         /* Update the fc_host data structures with new wwn. */
424         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
425         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
426         fc_host_max_npiv_vports(shost) = phba->max_vpi;
427
428         /* If no serial number in VPD data, use low 6 bytes of WWNN */
429         /* This should be consolidated into parse_vpd ? - mr */
430         if (phba->SerialNumber[0] == 0) {
431                 uint8_t *outptr;
432
433                 outptr = &vport->fc_nodename.u.s.IEEE[0];
434                 for (i = 0; i < 12; i++) {
435                         status = *outptr++;
436                         j = ((status & 0xf0) >> 4);
437                         if (j <= 9)
438                                 phba->SerialNumber[i] =
439                                     (char)((uint8_t) 0x30 + (uint8_t) j);
440                         else
441                                 phba->SerialNumber[i] =
442                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
443                         i++;
444                         j = (status & 0xf);
445                         if (j <= 9)
446                                 phba->SerialNumber[i] =
447                                     (char)((uint8_t) 0x30 + (uint8_t) j);
448                         else
449                                 phba->SerialNumber[i] =
450                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
451                 }
452         }
453
454         lpfc_read_config(phba, pmb);
455         pmb->vport = vport;
456         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
457                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
458                                 "0453 Adapter failed to init, mbxCmd x%x "
459                                 "READ_CONFIG, mbxStatus x%x\n",
460                                 mb->mbxCommand, mb->mbxStatus);
461                 phba->link_state = LPFC_HBA_ERROR;
462                 mempool_free( pmb, phba->mbox_mem_pool);
463                 return -EIO;
464         }
465
466         /* Check if the port is disabled */
467         lpfc_sli_read_link_ste(phba);
468
469         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
470         if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
471                 phba->cfg_hba_queue_depth =
472                         (mb->un.varRdConfig.max_xri + 1) -
473                                         lpfc_sli4_get_els_iocb_cnt(phba);
474
475         phba->lmt = mb->un.varRdConfig.lmt;
476
477         /* Get the default values for Model Name and Description */
478         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
479
480         phba->link_state = LPFC_LINK_DOWN;
481
482         /* Only process IOCBs on ELS ring till hba_state is READY */
483         if (psli->ring[psli->extra_ring].cmdringaddr)
484                 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
485         if (psli->ring[psli->fcp_ring].cmdringaddr)
486                 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
487         if (psli->ring[psli->next_ring].cmdringaddr)
488                 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
489
490         /* Post receive buffers for desired rings */
491         if (phba->sli_rev != 3)
492                 lpfc_post_rcv_buf(phba);
493
494         /*
495          * Configure HBA MSI-X attention conditions to messages if MSI-X mode
496          */
497         if (phba->intr_type == MSIX) {
498                 rc = lpfc_config_msi(phba, pmb);
499                 if (rc) {
500                         mempool_free(pmb, phba->mbox_mem_pool);
501                         return -EIO;
502                 }
503                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
504                 if (rc != MBX_SUCCESS) {
505                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
506                                         "0352 Config MSI mailbox command "
507                                         "failed, mbxCmd x%x, mbxStatus x%x\n",
508                                         pmb->u.mb.mbxCommand,
509                                         pmb->u.mb.mbxStatus);
510                         mempool_free(pmb, phba->mbox_mem_pool);
511                         return -EIO;
512                 }
513         }
514
515         spin_lock_irq(&phba->hbalock);
516         /* Initialize ERATT handling flag */
517         phba->hba_flag &= ~HBA_ERATT_HANDLED;
518
519         /* Enable appropriate host interrupts */
520         if (lpfc_readl(phba->HCregaddr, &status)) {
521                 spin_unlock_irq(&phba->hbalock);
522                 return -EIO;
523         }
524         status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
525         if (psli->num_rings > 0)
526                 status |= HC_R0INT_ENA;
527         if (psli->num_rings > 1)
528                 status |= HC_R1INT_ENA;
529         if (psli->num_rings > 2)
530                 status |= HC_R2INT_ENA;
531         if (psli->num_rings > 3)
532                 status |= HC_R3INT_ENA;
533
534         if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
535             (phba->cfg_poll & DISABLE_FCP_RING_INT))
536                 status &= ~(HC_R0INT_ENA);
537
538         writel(status, phba->HCregaddr);
539         readl(phba->HCregaddr); /* flush */
540         spin_unlock_irq(&phba->hbalock);
541
542         /* Set up ring-0 (ELS) timer */
543         timeout = phba->fc_ratov * 2;
544         mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
545         /* Set up heart beat (HB) timer */
546         mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
547         phba->hb_outstanding = 0;
548         phba->last_completion_time = jiffies;
549         /* Set up error attention (ERATT) polling timer */
550         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
551
552         if (phba->hba_flag & LINK_DISABLED) {
553                 lpfc_printf_log(phba,
554                         KERN_ERR, LOG_INIT,
555                         "2598 Adapter Link is disabled.\n");
556                 lpfc_down_link(phba, pmb);
557                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
558                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
559                 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
560                         lpfc_printf_log(phba,
561                         KERN_ERR, LOG_INIT,
562                         "2599 Adapter failed to issue DOWN_LINK"
563                         " mbox command rc 0x%x\n", rc);
564
565                         mempool_free(pmb, phba->mbox_mem_pool);
566                         return -EIO;
567                 }
568         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
569                 mempool_free(pmb, phba->mbox_mem_pool);
570                 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
571                 if (rc)
572                         return rc;
573         }
574         /* MBOX buffer will be freed in mbox compl */
575         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
576         if (!pmb) {
577                 phba->link_state = LPFC_HBA_ERROR;
578                 return -ENOMEM;
579         }
580
581         lpfc_config_async(phba, pmb, LPFC_ELS_RING);
582         pmb->mbox_cmpl = lpfc_config_async_cmpl;
583         pmb->vport = phba->pport;
584         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
585
586         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
587                 lpfc_printf_log(phba,
588                                 KERN_ERR,
589                                 LOG_INIT,
590                                 "0456 Adapter failed to issue "
591                                 "ASYNCEVT_ENABLE mbox status x%x\n",
592                                 rc);
593                 mempool_free(pmb, phba->mbox_mem_pool);
594         }
595
596         /* Get Option rom version */
597         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
598         if (!pmb) {
599                 phba->link_state = LPFC_HBA_ERROR;
600                 return -ENOMEM;
601         }
602
603         lpfc_dump_wakeup_param(phba, pmb);
604         pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
605         pmb->vport = phba->pport;
606         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
607
608         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
609                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
610                                 "to get Option ROM version status x%x\n", rc);
611                 mempool_free(pmb, phba->mbox_mem_pool);
612         }
613
614         return 0;
615 }
616
617 /**
618  * lpfc_hba_init_link - Initialize the FC link
619  * @phba: pointer to lpfc hba data structure.
620  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
621  *
622  * This routine will issue the INIT_LINK mailbox command call.
623  * It is available to other drivers through the lpfc_hba data
624  * structure for use as a delayed link up mechanism with the
625  * module parameter lpfc_suppress_link_up.
626  *
627  * Return code
628  *              0 - success
629  *              Any other value - error
630  **/
631 int
632 lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
633 {
634         return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
635 }
636
637 /**
638  * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
639  * @phba: pointer to lpfc hba data structure.
640  * @fc_topology: desired fc topology.
641  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
642  *
643  * This routine will issue the INIT_LINK mailbox command call.
644  * It is available to other drivers through the lpfc_hba data
645  * structure for use as a delayed link up mechanism with the
646  * module parameter lpfc_suppress_link_up.
647  *
648  * Return code
649  *              0 - success
650  *              Any other value - error
651  **/
652 int
653 lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
654                                uint32_t flag)
655 {
656         struct lpfc_vport *vport = phba->pport;
657         LPFC_MBOXQ_t *pmb;
658         MAILBOX_t *mb;
659         int rc;
660
661         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
662         if (!pmb) {
663                 phba->link_state = LPFC_HBA_ERROR;
664                 return -ENOMEM;
665         }
666         mb = &pmb->u.mb;
667         pmb->vport = vport;
668
669         if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
670             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
671              !(phba->lmt & LMT_1Gb)) ||
672             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
673              !(phba->lmt & LMT_2Gb)) ||
674             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
675              !(phba->lmt & LMT_4Gb)) ||
676             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
677              !(phba->lmt & LMT_8Gb)) ||
678             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
679              !(phba->lmt & LMT_10Gb)) ||
680             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
681              !(phba->lmt & LMT_16Gb))) {
682                 /* Reset link speed to auto */
683                 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
684                         "1302 Invalid speed for this board:%d "
685                         "Reset link speed to auto.\n",
686                         phba->cfg_link_speed);
687                         phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
688         }
689         lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
690         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
691         if (phba->sli_rev < LPFC_SLI_REV4)
692                 lpfc_set_loopback_flag(phba);
693         rc = lpfc_sli_issue_mbox(phba, pmb, flag);
694         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
695                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
696                         "0498 Adapter failed to init, mbxCmd x%x "
697                         "INIT_LINK, mbxStatus x%x\n",
698                         mb->mbxCommand, mb->mbxStatus);
699                 if (phba->sli_rev <= LPFC_SLI_REV3) {
700                         /* Clear all interrupt enable conditions */
701                         writel(0, phba->HCregaddr);
702                         readl(phba->HCregaddr); /* flush */
703                         /* Clear all pending interrupts */
704                         writel(0xffffffff, phba->HAregaddr);
705                         readl(phba->HAregaddr); /* flush */
706                 }
707                 phba->link_state = LPFC_HBA_ERROR;
708                 if (rc != MBX_BUSY || flag == MBX_POLL)
709                         mempool_free(pmb, phba->mbox_mem_pool);
710                 return -EIO;
711         }
712         phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
713         if (flag == MBX_POLL)
714                 mempool_free(pmb, phba->mbox_mem_pool);
715
716         return 0;
717 }
718
719 /**
720  * lpfc_hba_down_link - this routine downs the FC link
721  * @phba: pointer to lpfc hba data structure.
722  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
723  *
724  * This routine will issue the DOWN_LINK mailbox command call.
725  * It is available to other drivers through the lpfc_hba data
726  * structure for use to stop the link.
727  *
728  * Return code
729  *              0 - success
730  *              Any other value - error
731  **/
732 int
733 lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
734 {
735         LPFC_MBOXQ_t *pmb;
736         int rc;
737
738         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
739         if (!pmb) {
740                 phba->link_state = LPFC_HBA_ERROR;
741                 return -ENOMEM;
742         }
743
744         lpfc_printf_log(phba,
745                 KERN_ERR, LOG_INIT,
746                 "0491 Adapter Link is disabled.\n");
747         lpfc_down_link(phba, pmb);
748         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
749         rc = lpfc_sli_issue_mbox(phba, pmb, flag);
750         if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
751                 lpfc_printf_log(phba,
752                 KERN_ERR, LOG_INIT,
753                 "2522 Adapter failed to issue DOWN_LINK"
754                 " mbox command rc 0x%x\n", rc);
755
756                 mempool_free(pmb, phba->mbox_mem_pool);
757                 return -EIO;
758         }
759         if (flag == MBX_POLL)
760                 mempool_free(pmb, phba->mbox_mem_pool);
761
762         return 0;
763 }
764
765 /**
766  * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
767  * @phba: pointer to lpfc HBA data structure.
768  *
769  * This routine will do LPFC uninitialization before the HBA is reset when
770  * bringing down the SLI Layer.
771  *
772  * Return codes
773  *   0 - success.
774  *   Any other value - error.
775  **/
776 int
777 lpfc_hba_down_prep(struct lpfc_hba *phba)
778 {
779         struct lpfc_vport **vports;
780         int i;
781
782         if (phba->sli_rev <= LPFC_SLI_REV3) {
783                 /* Disable interrupts */
784                 writel(0, phba->HCregaddr);
785                 readl(phba->HCregaddr); /* flush */
786         }
787
788         if (phba->pport->load_flag & FC_UNLOADING)
789                 lpfc_cleanup_discovery_resources(phba->pport);
790         else {
791                 vports = lpfc_create_vport_work_array(phba);
792                 if (vports != NULL)
793                         for (i = 0; i <= phba->max_vports &&
794                                 vports[i] != NULL; i++)
795                                 lpfc_cleanup_discovery_resources(vports[i]);
796                 lpfc_destroy_vport_work_array(phba, vports);
797         }
798         return 0;
799 }
800
801 /**
802  * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
803  * @phba: pointer to lpfc HBA data structure.
804  *
805  * This routine will do uninitialization after the HBA is reset when bring
806  * down the SLI Layer.
807  *
808  * Return codes
809  *   0 - success.
810  *   Any other value - error.
811  **/
812 static int
813 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
814 {
815         struct lpfc_sli *psli = &phba->sli;
816         struct lpfc_sli_ring *pring;
817         struct lpfc_dmabuf *mp, *next_mp;
818         LIST_HEAD(completions);
819         int i;
820
821         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
822                 lpfc_sli_hbqbuf_free_all(phba);
823         else {
824                 /* Cleanup preposted buffers on the ELS ring */
825                 pring = &psli->ring[LPFC_ELS_RING];
826                 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
827                         list_del(&mp->list);
828                         pring->postbufq_cnt--;
829                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
830                         kfree(mp);
831                 }
832         }
833
834         spin_lock_irq(&phba->hbalock);
835         for (i = 0; i < psli->num_rings; i++) {
836                 pring = &psli->ring[i];
837
838                 /* At this point in time the HBA is either reset or DOA. Either
839                  * way, nothing should be on txcmplq as it will NEVER complete.
840                  */
841                 list_splice_init(&pring->txcmplq, &completions);
842                 pring->txcmplq_cnt = 0;
843                 spin_unlock_irq(&phba->hbalock);
844
845                 /* Cancel all the IOCBs from the completions list */
846                 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
847                                       IOERR_SLI_ABORTED);
848
849                 lpfc_sli_abort_iocb_ring(phba, pring);
850                 spin_lock_irq(&phba->hbalock);
851         }
852         spin_unlock_irq(&phba->hbalock);
853
854         return 0;
855 }
856
857 /**
858  * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
859  * @phba: pointer to lpfc HBA data structure.
860  *
861  * This routine will do uninitialization after the HBA is reset when bring
862  * down the SLI Layer.
863  *
864  * Return codes
865  *   0 - success.
866  *   Any other value - error.
867  **/
868 static int
869 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
870 {
871         struct lpfc_scsi_buf *psb, *psb_next;
872         LIST_HEAD(aborts);
873         int ret;
874         unsigned long iflag = 0;
875         struct lpfc_sglq *sglq_entry = NULL;
876
877         ret = lpfc_hba_down_post_s3(phba);
878         if (ret)
879                 return ret;
880         /* At this point in time the HBA is either reset or DOA. Either
881          * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
882          * on the lpfc_sgl_list so that it can either be freed if the
883          * driver is unloading or reposted if the driver is restarting
884          * the port.
885          */
886         spin_lock_irq(&phba->hbalock);  /* required for lpfc_sgl_list and */
887                                         /* scsl_buf_list */
888         /* abts_sgl_list_lock required because worker thread uses this
889          * list.
890          */
891         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
892         list_for_each_entry(sglq_entry,
893                 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
894                 sglq_entry->state = SGL_FREED;
895
896         list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
897                         &phba->sli4_hba.lpfc_sgl_list);
898         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
899         /* abts_scsi_buf_list_lock required because worker thread uses this
900          * list.
901          */
902         spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
903         list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
904                         &aborts);
905         spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
906         spin_unlock_irq(&phba->hbalock);
907
908         list_for_each_entry_safe(psb, psb_next, &aborts, list) {
909                 psb->pCmd = NULL;
910                 psb->status = IOSTAT_SUCCESS;
911         }
912         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
913         list_splice(&aborts, &phba->lpfc_scsi_buf_list);
914         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
915         return 0;
916 }
917
918 /**
919  * lpfc_hba_down_post - Wrapper func for hba down post routine
920  * @phba: pointer to lpfc HBA data structure.
921  *
922  * This routine wraps the actual SLI3 or SLI4 routine for performing
923  * uninitialization after the HBA is reset when bring down the SLI Layer.
924  *
925  * Return codes
926  *   0 - success.
927  *   Any other value - error.
928  **/
929 int
930 lpfc_hba_down_post(struct lpfc_hba *phba)
931 {
932         return (*phba->lpfc_hba_down_post)(phba);
933 }
934
935 /**
936  * lpfc_hb_timeout - The HBA-timer timeout handler
937  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
938  *
939  * This is the HBA-timer timeout handler registered to the lpfc driver. When
940  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
941  * work-port-events bitmap and the worker thread is notified. This timeout
942  * event will be used by the worker thread to invoke the actual timeout
943  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
944  * be performed in the timeout handler and the HBA timeout event bit shall
945  * be cleared by the worker thread after it has taken the event bitmap out.
946  **/
947 static void
948 lpfc_hb_timeout(unsigned long ptr)
949 {
950         struct lpfc_hba *phba;
951         uint32_t tmo_posted;
952         unsigned long iflag;
953
954         phba = (struct lpfc_hba *)ptr;
955
956         /* Check for heart beat timeout conditions */
957         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
958         tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
959         if (!tmo_posted)
960                 phba->pport->work_port_events |= WORKER_HB_TMO;
961         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
962
963         /* Tell the worker thread there is work to do */
964         if (!tmo_posted)
965                 lpfc_worker_wake_up(phba);
966         return;
967 }
968
969 /**
970  * lpfc_rrq_timeout - The RRQ-timer timeout handler
971  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
972  *
973  * This is the RRQ-timer timeout handler registered to the lpfc driver. When
974  * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
975  * work-port-events bitmap and the worker thread is notified. This timeout
976  * event will be used by the worker thread to invoke the actual timeout
977  * handler routine, lpfc_rrq_handler. Any periodical operations will
978  * be performed in the timeout handler and the RRQ timeout event bit shall
979  * be cleared by the worker thread after it has taken the event bitmap out.
980  **/
981 static void
982 lpfc_rrq_timeout(unsigned long ptr)
983 {
984         struct lpfc_hba *phba;
985         unsigned long iflag;
986
987         phba = (struct lpfc_hba *)ptr;
988         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
989         phba->hba_flag |= HBA_RRQ_ACTIVE;
990         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
991         lpfc_worker_wake_up(phba);
992 }
993
994 /**
995  * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
996  * @phba: pointer to lpfc hba data structure.
997  * @pmboxq: pointer to the driver internal queue element for mailbox command.
998  *
999  * This is the callback function to the lpfc heart-beat mailbox command.
1000  * If configured, the lpfc driver issues the heart-beat mailbox command to
1001  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1002  * heart-beat mailbox command is issued, the driver shall set up heart-beat
1003  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1004  * heart-beat outstanding state. Once the mailbox command comes back and
1005  * no error conditions detected, the heart-beat mailbox command timer is
1006  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1007  * state is cleared for the next heart-beat. If the timer expired with the
1008  * heart-beat outstanding state set, the driver will put the HBA offline.
1009  **/
1010 static void
1011 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1012 {
1013         unsigned long drvr_flag;
1014
1015         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1016         phba->hb_outstanding = 0;
1017         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1018
1019         /* Check and reset heart-beat timer is necessary */
1020         mempool_free(pmboxq, phba->mbox_mem_pool);
1021         if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1022                 !(phba->link_state == LPFC_HBA_ERROR) &&
1023                 !(phba->pport->load_flag & FC_UNLOADING))
1024                 mod_timer(&phba->hb_tmofunc,
1025                         jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1026         return;
1027 }
1028
1029 /**
1030  * lpfc_hb_timeout_handler - The HBA-timer timeout handler
1031  * @phba: pointer to lpfc hba data structure.
1032  *
1033  * This is the actual HBA-timer timeout handler to be invoked by the worker
1034  * thread whenever the HBA timer fired and HBA-timeout event posted. This
1035  * handler performs any periodic operations needed for the device. If such
1036  * periodic event has already been attended to either in the interrupt handler
1037  * or by processing slow-ring or fast-ring events within the HBA-timer
1038  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1039  * the timer for the next timeout period. If lpfc heart-beat mailbox command
1040  * is configured and there is no heart-beat mailbox command outstanding, a
1041  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1042  * has been a heart-beat mailbox command outstanding, the HBA shall be put
1043  * to offline.
1044  **/
1045 void
1046 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1047 {
1048         struct lpfc_vport **vports;
1049         LPFC_MBOXQ_t *pmboxq;
1050         struct lpfc_dmabuf *buf_ptr;
1051         int retval, i;
1052         struct lpfc_sli *psli = &phba->sli;
1053         LIST_HEAD(completions);
1054
1055         vports = lpfc_create_vport_work_array(phba);
1056         if (vports != NULL)
1057                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1058                         lpfc_rcv_seq_check_edtov(vports[i]);
1059         lpfc_destroy_vport_work_array(phba, vports);
1060
1061         if ((phba->link_state == LPFC_HBA_ERROR) ||
1062                 (phba->pport->load_flag & FC_UNLOADING) ||
1063                 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1064                 return;
1065
1066         spin_lock_irq(&phba->pport->work_port_lock);
1067
1068         if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1069                 jiffies)) {
1070                 spin_unlock_irq(&phba->pport->work_port_lock);
1071                 if (!phba->hb_outstanding)
1072                         mod_timer(&phba->hb_tmofunc,
1073                                 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1074                 else
1075                         mod_timer(&phba->hb_tmofunc,
1076                                 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1077                 return;
1078         }
1079         spin_unlock_irq(&phba->pport->work_port_lock);
1080
1081         if (phba->elsbuf_cnt &&
1082                 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1083                 spin_lock_irq(&phba->hbalock);
1084                 list_splice_init(&phba->elsbuf, &completions);
1085                 phba->elsbuf_cnt = 0;
1086                 phba->elsbuf_prev_cnt = 0;
1087                 spin_unlock_irq(&phba->hbalock);
1088
1089                 while (!list_empty(&completions)) {
1090                         list_remove_head(&completions, buf_ptr,
1091                                 struct lpfc_dmabuf, list);
1092                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1093                         kfree(buf_ptr);
1094                 }
1095         }
1096         phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1097
1098         /* If there is no heart beat outstanding, issue a heartbeat command */
1099         if (phba->cfg_enable_hba_heartbeat) {
1100                 if (!phba->hb_outstanding) {
1101                         if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1102                                 (list_empty(&psli->mboxq))) {
1103                                 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1104                                                         GFP_KERNEL);
1105                                 if (!pmboxq) {
1106                                         mod_timer(&phba->hb_tmofunc,
1107                                                  jiffies +
1108                                                  HZ * LPFC_HB_MBOX_INTERVAL);
1109                                         return;
1110                                 }
1111
1112                                 lpfc_heart_beat(phba, pmboxq);
1113                                 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1114                                 pmboxq->vport = phba->pport;
1115                                 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1116                                                 MBX_NOWAIT);
1117
1118                                 if (retval != MBX_BUSY &&
1119                                         retval != MBX_SUCCESS) {
1120                                         mempool_free(pmboxq,
1121                                                         phba->mbox_mem_pool);
1122                                         mod_timer(&phba->hb_tmofunc,
1123                                                 jiffies +
1124                                                 HZ * LPFC_HB_MBOX_INTERVAL);
1125                                         return;
1126                                 }
1127                                 phba->skipped_hb = 0;
1128                                 phba->hb_outstanding = 1;
1129                         } else if (time_before_eq(phba->last_completion_time,
1130                                         phba->skipped_hb)) {
1131                                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1132                                         "2857 Last completion time not "
1133                                         " updated in %d ms\n",
1134                                         jiffies_to_msecs(jiffies
1135                                                  - phba->last_completion_time));
1136                         } else
1137                                 phba->skipped_hb = jiffies;
1138
1139                         mod_timer(&phba->hb_tmofunc,
1140                                   jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1141                         return;
1142                 } else {
1143                         /*
1144                         * If heart beat timeout called with hb_outstanding set
1145                         * we need to give the hb mailbox cmd a chance to
1146                         * complete or TMO.
1147                         */
1148                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1149                                         "0459 Adapter heartbeat still out"
1150                                         "standing:last compl time was %d ms.\n",
1151                                         jiffies_to_msecs(jiffies
1152                                                  - phba->last_completion_time));
1153                         mod_timer(&phba->hb_tmofunc,
1154                                   jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1155                 }
1156         }
1157 }
1158
1159 /**
1160  * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1161  * @phba: pointer to lpfc hba data structure.
1162  *
1163  * This routine is called to bring the HBA offline when HBA hardware error
1164  * other than Port Error 6 has been detected.
1165  **/
1166 static void
1167 lpfc_offline_eratt(struct lpfc_hba *phba)
1168 {
1169         struct lpfc_sli   *psli = &phba->sli;
1170
1171         spin_lock_irq(&phba->hbalock);
1172         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1173         spin_unlock_irq(&phba->hbalock);
1174         lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1175
1176         lpfc_offline(phba);
1177         lpfc_reset_barrier(phba);
1178         spin_lock_irq(&phba->hbalock);
1179         lpfc_sli_brdreset(phba);
1180         spin_unlock_irq(&phba->hbalock);
1181         lpfc_hba_down_post(phba);
1182         lpfc_sli_brdready(phba, HS_MBRDY);
1183         lpfc_unblock_mgmt_io(phba);
1184         phba->link_state = LPFC_HBA_ERROR;
1185         return;
1186 }
1187
1188 /**
1189  * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1190  * @phba: pointer to lpfc hba data structure.
1191  *
1192  * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1193  * other than Port Error 6 has been detected.
1194  **/
1195 static void
1196 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1197 {
1198         lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1199         lpfc_offline(phba);
1200         lpfc_sli4_brdreset(phba);
1201         lpfc_hba_down_post(phba);
1202         lpfc_sli4_post_status_check(phba);
1203         lpfc_unblock_mgmt_io(phba);
1204         phba->link_state = LPFC_HBA_ERROR;
1205 }
1206
1207 /**
1208  * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1209  * @phba: pointer to lpfc hba data structure.
1210  *
1211  * This routine is invoked to handle the deferred HBA hardware error
1212  * conditions. This type of error is indicated by HBA by setting ER1
1213  * and another ER bit in the host status register. The driver will
1214  * wait until the ER1 bit clears before handling the error condition.
1215  **/
1216 static void
1217 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1218 {
1219         uint32_t old_host_status = phba->work_hs;
1220         struct lpfc_sli_ring  *pring;
1221         struct lpfc_sli *psli = &phba->sli;
1222
1223         /* If the pci channel is offline, ignore possible errors,
1224          * since we cannot communicate with the pci card anyway.
1225          */
1226         if (pci_channel_offline(phba->pcidev)) {
1227                 spin_lock_irq(&phba->hbalock);
1228                 phba->hba_flag &= ~DEFER_ERATT;
1229                 spin_unlock_irq(&phba->hbalock);
1230                 return;
1231         }
1232
1233         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1234                 "0479 Deferred Adapter Hardware Error "
1235                 "Data: x%x x%x x%x\n",
1236                 phba->work_hs,
1237                 phba->work_status[0], phba->work_status[1]);
1238
1239         spin_lock_irq(&phba->hbalock);
1240         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1241         spin_unlock_irq(&phba->hbalock);
1242
1243
1244         /*
1245          * Firmware stops when it triggred erratt. That could cause the I/Os
1246          * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1247          * SCSI layer retry it after re-establishing link.
1248          */
1249         pring = &psli->ring[psli->fcp_ring];
1250         lpfc_sli_abort_iocb_ring(phba, pring);
1251
1252         /*
1253          * There was a firmware error. Take the hba offline and then
1254          * attempt to restart it.
1255          */
1256         lpfc_offline_prep(phba, LPFC_MBX_WAIT);
1257         lpfc_offline(phba);
1258
1259         /* Wait for the ER1 bit to clear.*/
1260         while (phba->work_hs & HS_FFER1) {
1261                 msleep(100);
1262                 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1263                         phba->work_hs = UNPLUG_ERR ;
1264                         break;
1265                 }
1266                 /* If driver is unloading let the worker thread continue */
1267                 if (phba->pport->load_flag & FC_UNLOADING) {
1268                         phba->work_hs = 0;
1269                         break;
1270                 }
1271         }
1272
1273         /*
1274          * This is to ptrotect against a race condition in which
1275          * first write to the host attention register clear the
1276          * host status register.
1277          */
1278         if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1279                 phba->work_hs = old_host_status & ~HS_FFER1;
1280
1281         spin_lock_irq(&phba->hbalock);
1282         phba->hba_flag &= ~DEFER_ERATT;
1283         spin_unlock_irq(&phba->hbalock);
1284         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1285         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1286 }
1287
1288 static void
1289 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1290 {
1291         struct lpfc_board_event_header board_event;
1292         struct Scsi_Host *shost;
1293
1294         board_event.event_type = FC_REG_BOARD_EVENT;
1295         board_event.subcategory = LPFC_EVENT_PORTINTERR;
1296         shost = lpfc_shost_from_vport(phba->pport);
1297         fc_host_post_vendor_event(shost, fc_get_event_number(),
1298                                   sizeof(board_event),
1299                                   (char *) &board_event,
1300                                   LPFC_NL_VENDOR_ID);
1301 }
1302
1303 /**
1304  * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1305  * @phba: pointer to lpfc hba data structure.
1306  *
1307  * This routine is invoked to handle the following HBA hardware error
1308  * conditions:
1309  * 1 - HBA error attention interrupt
1310  * 2 - DMA ring index out of range
1311  * 3 - Mailbox command came back as unknown
1312  **/
1313 static void
1314 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1315 {
1316         struct lpfc_vport *vport = phba->pport;
1317         struct lpfc_sli   *psli = &phba->sli;
1318         struct lpfc_sli_ring  *pring;
1319         uint32_t event_data;
1320         unsigned long temperature;
1321         struct temp_event temp_event_data;
1322         struct Scsi_Host  *shost;
1323
1324         /* If the pci channel is offline, ignore possible errors,
1325          * since we cannot communicate with the pci card anyway.
1326          */
1327         if (pci_channel_offline(phba->pcidev)) {
1328                 spin_lock_irq(&phba->hbalock);
1329                 phba->hba_flag &= ~DEFER_ERATT;
1330                 spin_unlock_irq(&phba->hbalock);
1331                 return;
1332         }
1333
1334         /* If resets are disabled then leave the HBA alone and return */
1335         if (!phba->cfg_enable_hba_reset)
1336                 return;
1337
1338         /* Send an internal error event to mgmt application */
1339         lpfc_board_errevt_to_mgmt(phba);
1340
1341         if (phba->hba_flag & DEFER_ERATT)
1342                 lpfc_handle_deferred_eratt(phba);
1343
1344         if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1345                 if (phba->work_hs & HS_FFER6)
1346                         /* Re-establishing Link */
1347                         lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1348                                         "1301 Re-establishing Link "
1349                                         "Data: x%x x%x x%x\n",
1350                                         phba->work_hs, phba->work_status[0],
1351                                         phba->work_status[1]);
1352                 if (phba->work_hs & HS_FFER8)
1353                         /* Device Zeroization */
1354                         lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1355                                         "2861 Host Authentication device "
1356                                         "zeroization Data:x%x x%x x%x\n",
1357                                         phba->work_hs, phba->work_status[0],
1358                                         phba->work_status[1]);
1359
1360                 spin_lock_irq(&phba->hbalock);
1361                 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1362                 spin_unlock_irq(&phba->hbalock);
1363
1364                 /*
1365                 * Firmware stops when it triggled erratt with HS_FFER6.
1366                 * That could cause the I/Os dropped by the firmware.
1367                 * Error iocb (I/O) on txcmplq and let the SCSI layer
1368                 * retry it after re-establishing link.
1369                 */
1370                 pring = &psli->ring[psli->fcp_ring];
1371                 lpfc_sli_abort_iocb_ring(phba, pring);
1372
1373                 /*
1374                  * There was a firmware error.  Take the hba offline and then
1375                  * attempt to restart it.
1376                  */
1377                 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1378                 lpfc_offline(phba);
1379                 lpfc_sli_brdrestart(phba);
1380                 if (lpfc_online(phba) == 0) {   /* Initialize the HBA */
1381                         lpfc_unblock_mgmt_io(phba);
1382                         return;
1383                 }
1384                 lpfc_unblock_mgmt_io(phba);
1385         } else if (phba->work_hs & HS_CRIT_TEMP) {
1386                 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1387                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1388                 temp_event_data.event_code = LPFC_CRIT_TEMP;
1389                 temp_event_data.data = (uint32_t)temperature;
1390
1391                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1392                                 "0406 Adapter maximum temperature exceeded "
1393                                 "(%ld), taking this port offline "
1394                                 "Data: x%x x%x x%x\n",
1395                                 temperature, phba->work_hs,
1396                                 phba->work_status[0], phba->work_status[1]);
1397
1398                 shost = lpfc_shost_from_vport(phba->pport);
1399                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1400                                           sizeof(temp_event_data),
1401                                           (char *) &temp_event_data,
1402                                           SCSI_NL_VID_TYPE_PCI
1403                                           | PCI_VENDOR_ID_EMULEX);
1404
1405                 spin_lock_irq(&phba->hbalock);
1406                 phba->over_temp_state = HBA_OVER_TEMP;
1407                 spin_unlock_irq(&phba->hbalock);
1408                 lpfc_offline_eratt(phba);
1409
1410         } else {
1411                 /* The if clause above forces this code path when the status
1412                  * failure is a value other than FFER6. Do not call the offline
1413                  * twice. This is the adapter hardware error path.
1414                  */
1415                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1416                                 "0457 Adapter Hardware Error "
1417                                 "Data: x%x x%x x%x\n",
1418                                 phba->work_hs,
1419                                 phba->work_status[0], phba->work_status[1]);
1420
1421                 event_data = FC_REG_DUMP_EVENT;
1422                 shost = lpfc_shost_from_vport(vport);
1423                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1424                                 sizeof(event_data), (char *) &event_data,
1425                                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1426
1427                 lpfc_offline_eratt(phba);
1428         }
1429         return;
1430 }
1431
1432 /**
1433  * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1434  * @phba: pointer to lpfc hba data structure.
1435  * @mbx_action: flag for mailbox shutdown action.
1436  *
1437  * This routine is invoked to perform an SLI4 port PCI function reset in
1438  * response to port status register polling attention. It waits for port
1439  * status register (ERR, RDY, RN) bits before proceeding with function reset.
1440  * During this process, interrupt vectors are freed and later requested
1441  * for handling possible port resource change.
1442  **/
1443 static int
1444 lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action)
1445 {
1446         int rc;
1447         uint32_t intr_mode;
1448
1449         /*
1450          * On error status condition, driver need to wait for port
1451          * ready before performing reset.
1452          */
1453         rc = lpfc_sli4_pdev_status_reg_wait(phba);
1454         if (!rc) {
1455                 /* need reset: attempt for port recovery */
1456                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1457                                 "2887 Reset Needed: Attempting Port "
1458                                 "Recovery...\n");
1459                 lpfc_offline_prep(phba, mbx_action);
1460                 lpfc_offline(phba);
1461                 /* release interrupt for possible resource change */
1462                 lpfc_sli4_disable_intr(phba);
1463                 lpfc_sli_brdrestart(phba);
1464                 /* request and enable interrupt */
1465                 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1466                 if (intr_mode == LPFC_INTR_ERROR) {
1467                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1468                                         "3175 Failed to enable interrupt\n");
1469                         return -EIO;
1470                 } else {
1471                         phba->intr_mode = intr_mode;
1472                 }
1473                 rc = lpfc_online(phba);
1474                 if (rc == 0)
1475                         lpfc_unblock_mgmt_io(phba);
1476         }
1477         return rc;
1478 }
1479
1480 /**
1481  * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1482  * @phba: pointer to lpfc hba data structure.
1483  *
1484  * This routine is invoked to handle the SLI4 HBA hardware error attention
1485  * conditions.
1486  **/
1487 static void
1488 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1489 {
1490         struct lpfc_vport *vport = phba->pport;
1491         uint32_t event_data;
1492         struct Scsi_Host *shost;
1493         uint32_t if_type;
1494         struct lpfc_register portstat_reg = {0};
1495         uint32_t reg_err1, reg_err2;
1496         uint32_t uerrlo_reg, uemasklo_reg;
1497         uint32_t pci_rd_rc1, pci_rd_rc2;
1498         int rc;
1499
1500         /* If the pci channel is offline, ignore possible errors, since
1501          * we cannot communicate with the pci card anyway.
1502          */
1503         if (pci_channel_offline(phba->pcidev))
1504                 return;
1505         /* If resets are disabled then leave the HBA alone and return */
1506         if (!phba->cfg_enable_hba_reset)
1507                 return;
1508
1509         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1510         switch (if_type) {
1511         case LPFC_SLI_INTF_IF_TYPE_0:
1512                 pci_rd_rc1 = lpfc_readl(
1513                                 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1514                                 &uerrlo_reg);
1515                 pci_rd_rc2 = lpfc_readl(
1516                                 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1517                                 &uemasklo_reg);
1518                 /* consider PCI bus read error as pci_channel_offline */
1519                 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1520                         return;
1521                 lpfc_sli4_offline_eratt(phba);
1522                 break;
1523         case LPFC_SLI_INTF_IF_TYPE_2:
1524                 pci_rd_rc1 = lpfc_readl(
1525                                 phba->sli4_hba.u.if_type2.STATUSregaddr,
1526                                 &portstat_reg.word0);
1527                 /* consider PCI bus read error as pci_channel_offline */
1528                 if (pci_rd_rc1 == -EIO) {
1529                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1530                                 "3151 PCI bus read access failure: x%x\n",
1531                                 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
1532                         return;
1533                 }
1534                 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1535                 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
1536                 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1537                         /* TODO: Register for Overtemp async events. */
1538                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1539                                 "2889 Port Overtemperature event, "
1540                                 "taking port offline\n");
1541                         spin_lock_irq(&phba->hbalock);
1542                         phba->over_temp_state = HBA_OVER_TEMP;
1543                         spin_unlock_irq(&phba->hbalock);
1544                         lpfc_sli4_offline_eratt(phba);
1545                         break;
1546                 }
1547                 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1548                     reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1549                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1550                                         "3143 Port Down: Firmware Restarted\n");
1551                 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1552                          reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1553                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1554                                         "3144 Port Down: Debug Dump\n");
1555                 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1556                          reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1557                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1558                                         "3145 Port Down: Provisioning\n");
1559
1560                 /* Check port status register for function reset */
1561                 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT);
1562                 if (rc == 0) {
1563                         /* don't report event on forced debug dump */
1564                         if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1565                             reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1566                                 return;
1567                         else
1568                                 break;
1569                 }
1570                 /* fall through for not able to recover */
1571                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1572                                 "3152 Unrecoverable error, bring the port "
1573                                 "offline\n");
1574                 lpfc_sli4_offline_eratt(phba);
1575                 break;
1576         case LPFC_SLI_INTF_IF_TYPE_1:
1577         default:
1578                 break;
1579         }
1580         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1581                         "3123 Report dump event to upper layer\n");
1582         /* Send an internal error event to mgmt application */
1583         lpfc_board_errevt_to_mgmt(phba);
1584
1585         event_data = FC_REG_DUMP_EVENT;
1586         shost = lpfc_shost_from_vport(vport);
1587         fc_host_post_vendor_event(shost, fc_get_event_number(),
1588                                   sizeof(event_data), (char *) &event_data,
1589                                   SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1590 }
1591
1592 /**
1593  * lpfc_handle_eratt - Wrapper func for handling hba error attention
1594  * @phba: pointer to lpfc HBA data structure.
1595  *
1596  * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1597  * routine from the API jump table function pointer from the lpfc_hba struct.
1598  *
1599  * Return codes
1600  *   0 - success.
1601  *   Any other value - error.
1602  **/
1603 void
1604 lpfc_handle_eratt(struct lpfc_hba *phba)
1605 {
1606         (*phba->lpfc_handle_eratt)(phba);
1607 }
1608
1609 /**
1610  * lpfc_handle_latt - The HBA link event handler
1611  * @phba: pointer to lpfc hba data structure.
1612  *
1613  * This routine is invoked from the worker thread to handle a HBA host
1614  * attention link event.
1615  **/
1616 void
1617 lpfc_handle_latt(struct lpfc_hba *phba)
1618 {
1619         struct lpfc_vport *vport = phba->pport;
1620         struct lpfc_sli   *psli = &phba->sli;
1621         LPFC_MBOXQ_t *pmb;
1622         volatile uint32_t control;
1623         struct lpfc_dmabuf *mp;
1624         int rc = 0;
1625
1626         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1627         if (!pmb) {
1628                 rc = 1;
1629                 goto lpfc_handle_latt_err_exit;
1630         }
1631
1632         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1633         if (!mp) {
1634                 rc = 2;
1635                 goto lpfc_handle_latt_free_pmb;
1636         }
1637
1638         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1639         if (!mp->virt) {
1640                 rc = 3;
1641                 goto lpfc_handle_latt_free_mp;
1642         }
1643
1644         /* Cleanup any outstanding ELS commands */
1645         lpfc_els_flush_all_cmd(phba);
1646
1647         psli->slistat.link_event++;
1648         lpfc_read_topology(phba, pmb, mp);
1649         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
1650         pmb->vport = vport;
1651         /* Block ELS IOCBs until we have processed this mbox command */
1652         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
1653         rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
1654         if (rc == MBX_NOT_FINISHED) {
1655                 rc = 4;
1656                 goto lpfc_handle_latt_free_mbuf;
1657         }
1658
1659         /* Clear Link Attention in HA REG */
1660         spin_lock_irq(&phba->hbalock);
1661         writel(HA_LATT, phba->HAregaddr);
1662         readl(phba->HAregaddr); /* flush */
1663         spin_unlock_irq(&phba->hbalock);
1664
1665         return;
1666
1667 lpfc_handle_latt_free_mbuf:
1668         phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1669         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1670 lpfc_handle_latt_free_mp:
1671         kfree(mp);
1672 lpfc_handle_latt_free_pmb:
1673         mempool_free(pmb, phba->mbox_mem_pool);
1674 lpfc_handle_latt_err_exit:
1675         /* Enable Link attention interrupts */
1676         spin_lock_irq(&phba->hbalock);
1677         psli->sli_flag |= LPFC_PROCESS_LA;
1678         control = readl(phba->HCregaddr);
1679         control |= HC_LAINT_ENA;
1680         writel(control, phba->HCregaddr);
1681         readl(phba->HCregaddr); /* flush */
1682
1683         /* Clear Link Attention in HA REG */
1684         writel(HA_LATT, phba->HAregaddr);
1685         readl(phba->HAregaddr); /* flush */
1686         spin_unlock_irq(&phba->hbalock);
1687         lpfc_linkdown(phba);
1688         phba->link_state = LPFC_HBA_ERROR;
1689
1690         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1691                      "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1692
1693         return;
1694 }
1695
1696 /**
1697  * lpfc_parse_vpd - Parse VPD (Vital Product Data)
1698  * @phba: pointer to lpfc hba data structure.
1699  * @vpd: pointer to the vital product data.
1700  * @len: length of the vital product data in bytes.
1701  *
1702  * This routine parses the Vital Product Data (VPD). The VPD is treated as
1703  * an array of characters. In this routine, the ModelName, ProgramType, and
1704  * ModelDesc, etc. fields of the phba data structure will be populated.
1705  *
1706  * Return codes
1707  *   0 - pointer to the VPD passed in is NULL
1708  *   1 - success
1709  **/
1710 int
1711 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1712 {
1713         uint8_t lenlo, lenhi;
1714         int Length;
1715         int i, j;
1716         int finished = 0;
1717         int index = 0;
1718
1719         if (!vpd)
1720                 return 0;
1721
1722         /* Vital Product */
1723         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1724                         "0455 Vital Product Data: x%x x%x x%x x%x\n",
1725                         (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1726                         (uint32_t) vpd[3]);
1727         while (!finished && (index < (len - 4))) {
1728                 switch (vpd[index]) {
1729                 case 0x82:
1730                 case 0x91:
1731                         index += 1;
1732                         lenlo = vpd[index];
1733                         index += 1;
1734                         lenhi = vpd[index];
1735                         index += 1;
1736                         i = ((((unsigned short)lenhi) << 8) + lenlo);
1737                         index += i;
1738                         break;
1739                 case 0x90:
1740                         index += 1;
1741                         lenlo = vpd[index];
1742                         index += 1;
1743                         lenhi = vpd[index];
1744                         index += 1;
1745                         Length = ((((unsigned short)lenhi) << 8) + lenlo);
1746                         if (Length > len - index)
1747                                 Length = len - index;
1748                         while (Length > 0) {
1749                         /* Look for Serial Number */
1750                         if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1751                                 index += 2;
1752                                 i = vpd[index];
1753                                 index += 1;
1754                                 j = 0;
1755                                 Length -= (3+i);
1756                                 while(i--) {
1757                                         phba->SerialNumber[j++] = vpd[index++];
1758                                         if (j == 31)
1759                                                 break;
1760                                 }
1761                                 phba->SerialNumber[j] = 0;
1762                                 continue;
1763                         }
1764                         else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1765                                 phba->vpd_flag |= VPD_MODEL_DESC;
1766                                 index += 2;
1767                                 i = vpd[index];
1768                                 index += 1;
1769                                 j = 0;
1770                                 Length -= (3+i);
1771                                 while(i--) {
1772                                         phba->ModelDesc[j++] = vpd[index++];
1773                                         if (j == 255)
1774                                                 break;
1775                                 }
1776                                 phba->ModelDesc[j] = 0;
1777                                 continue;
1778                         }
1779                         else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1780                                 phba->vpd_flag |= VPD_MODEL_NAME;
1781                                 index += 2;
1782                                 i = vpd[index];
1783                                 index += 1;
1784                                 j = 0;
1785                                 Length -= (3+i);
1786                                 while(i--) {
1787                                         phba->ModelName[j++] = vpd[index++];
1788                                         if (j == 79)
1789                                                 break;
1790                                 }
1791                                 phba->ModelName[j] = 0;
1792                                 continue;
1793                         }
1794                         else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1795                                 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1796                                 index += 2;
1797                                 i = vpd[index];
1798                                 index += 1;
1799                                 j = 0;
1800                                 Length -= (3+i);
1801                                 while(i--) {
1802                                         phba->ProgramType[j++] = vpd[index++];
1803                                         if (j == 255)
1804                                                 break;
1805                                 }
1806                                 phba->ProgramType[j] = 0;
1807                                 continue;
1808                         }
1809                         else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1810                                 phba->vpd_flag |= VPD_PORT;
1811                                 index += 2;
1812                                 i = vpd[index];
1813                                 index += 1;
1814                                 j = 0;
1815                                 Length -= (3+i);
1816                                 while(i--) {
1817                                         if ((phba->sli_rev == LPFC_SLI_REV4) &&
1818                                             (phba->sli4_hba.pport_name_sta ==
1819                                              LPFC_SLI4_PPNAME_GET)) {
1820                                                 j++;
1821                                                 index++;
1822                                         } else
1823                                                 phba->Port[j++] = vpd[index++];
1824                                         if (j == 19)
1825                                                 break;
1826                                 }
1827                                 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1828                                     (phba->sli4_hba.pport_name_sta ==
1829                                      LPFC_SLI4_PPNAME_NON))
1830                                         phba->Port[j] = 0;
1831                                 continue;
1832                         }
1833                         else {
1834                                 index += 2;
1835                                 i = vpd[index];
1836                                 index += 1;
1837                                 index += i;
1838                                 Length -= (3 + i);
1839                         }
1840                 }
1841                 finished = 0;
1842                 break;
1843                 case 0x78:
1844                         finished = 1;
1845                         break;
1846                 default:
1847                         index ++;
1848                         break;
1849                 }
1850         }
1851
1852         return(1);
1853 }
1854
1855 /**
1856  * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
1857  * @phba: pointer to lpfc hba data structure.
1858  * @mdp: pointer to the data structure to hold the derived model name.
1859  * @descp: pointer to the data structure to hold the derived description.
1860  *
1861  * This routine retrieves HBA's description based on its registered PCI device
1862  * ID. The @descp passed into this function points to an array of 256 chars. It
1863  * shall be returned with the model name, maximum speed, and the host bus type.
1864  * The @mdp passed into this function points to an array of 80 chars. When the
1865  * function returns, the @mdp will be filled with the model name.
1866  **/
1867 static void
1868 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1869 {
1870         lpfc_vpd_t *vp;
1871         uint16_t dev_id = phba->pcidev->device;
1872         int max_speed;
1873         int GE = 0;
1874         int oneConnect = 0; /* default is not a oneConnect */
1875         struct {
1876                 char *name;
1877                 char *bus;
1878                 char *function;
1879         } m = {"<Unknown>", "", ""};
1880
1881         if (mdp && mdp[0] != '\0'
1882                 && descp && descp[0] != '\0')
1883                 return;
1884
1885         if (phba->lmt & LMT_16Gb)
1886                 max_speed = 16;
1887         else if (phba->lmt & LMT_10Gb)
1888                 max_speed = 10;
1889         else if (phba->lmt & LMT_8Gb)
1890                 max_speed = 8;
1891         else if (phba->lmt & LMT_4Gb)
1892                 max_speed = 4;
1893         else if (phba->lmt & LMT_2Gb)
1894                 max_speed = 2;
1895         else
1896                 max_speed = 1;
1897
1898         vp = &phba->vpd;
1899
1900         switch (dev_id) {
1901         case PCI_DEVICE_ID_FIREFLY:
1902                 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
1903                 break;
1904         case PCI_DEVICE_ID_SUPERFLY:
1905                 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1906                         m = (typeof(m)){"LP7000", "PCI",
1907                                         "Fibre Channel Adapter"};
1908                 else
1909                         m = (typeof(m)){"LP7000E", "PCI",
1910                                         "Fibre Channel Adapter"};
1911                 break;
1912         case PCI_DEVICE_ID_DRAGONFLY:
1913                 m = (typeof(m)){"LP8000", "PCI",
1914                                 "Fibre Channel Adapter"};
1915                 break;
1916         case PCI_DEVICE_ID_CENTAUR:
1917                 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1918                         m = (typeof(m)){"LP9002", "PCI",
1919                                         "Fibre Channel Adapter"};
1920                 else
1921                         m = (typeof(m)){"LP9000", "PCI",
1922                                         "Fibre Channel Adapter"};
1923                 break;
1924         case PCI_DEVICE_ID_RFLY:
1925                 m = (typeof(m)){"LP952", "PCI",
1926                                 "Fibre Channel Adapter"};
1927                 break;
1928         case PCI_DEVICE_ID_PEGASUS:
1929                 m = (typeof(m)){"LP9802", "PCI-X",
1930                                 "Fibre Channel Adapter"};
1931                 break;
1932         case PCI_DEVICE_ID_THOR:
1933                 m = (typeof(m)){"LP10000", "PCI-X",
1934                                 "Fibre Channel Adapter"};
1935                 break;
1936         case PCI_DEVICE_ID_VIPER:
1937                 m = (typeof(m)){"LPX1000",  "PCI-X",
1938                                 "Fibre Channel Adapter"};
1939                 break;
1940         case PCI_DEVICE_ID_PFLY:
1941                 m = (typeof(m)){"LP982", "PCI-X",
1942                                 "Fibre Channel Adapter"};
1943                 break;
1944         case PCI_DEVICE_ID_TFLY:
1945                 m = (typeof(m)){"LP1050", "PCI-X",
1946                                 "Fibre Channel Adapter"};
1947                 break;
1948         case PCI_DEVICE_ID_HELIOS:
1949                 m = (typeof(m)){"LP11000", "PCI-X2",
1950                                 "Fibre Channel Adapter"};
1951                 break;
1952         case PCI_DEVICE_ID_HELIOS_SCSP:
1953                 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1954                                 "Fibre Channel Adapter"};
1955                 break;
1956         case PCI_DEVICE_ID_HELIOS_DCSP:
1957                 m = (typeof(m)){"LP11002-SP",  "PCI-X2",
1958                                 "Fibre Channel Adapter"};
1959                 break;
1960         case PCI_DEVICE_ID_NEPTUNE:
1961                 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
1962                 break;
1963         case PCI_DEVICE_ID_NEPTUNE_SCSP:
1964                 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
1965                 break;
1966         case PCI_DEVICE_ID_NEPTUNE_DCSP:
1967                 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
1968                 break;
1969         case PCI_DEVICE_ID_BMID:
1970                 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
1971                 break;
1972         case PCI_DEVICE_ID_BSMB:
1973                 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
1974                 break;
1975         case PCI_DEVICE_ID_ZEPHYR:
1976                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1977                 break;
1978         case PCI_DEVICE_ID_ZEPHYR_SCSP:
1979                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1980                 break;
1981         case PCI_DEVICE_ID_ZEPHYR_DCSP:
1982                 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
1983                 GE = 1;
1984                 break;
1985         case PCI_DEVICE_ID_ZMID:
1986                 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
1987                 break;
1988         case PCI_DEVICE_ID_ZSMB:
1989                 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
1990                 break;
1991         case PCI_DEVICE_ID_LP101:
1992                 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
1993                 break;
1994         case PCI_DEVICE_ID_LP10000S:
1995                 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
1996                 break;
1997         case PCI_DEVICE_ID_LP11000S:
1998                 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
1999                 break;
2000         case PCI_DEVICE_ID_LPE11000S:
2001                 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
2002                 break;
2003         case PCI_DEVICE_ID_SAT:
2004                 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
2005                 break;
2006         case PCI_DEVICE_ID_SAT_MID:
2007                 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
2008                 break;
2009         case PCI_DEVICE_ID_SAT_SMB:
2010                 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
2011                 break;
2012         case PCI_DEVICE_ID_SAT_DCSP:
2013                 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
2014                 break;
2015         case PCI_DEVICE_ID_SAT_SCSP:
2016                 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
2017                 break;
2018         case PCI_DEVICE_ID_SAT_S:
2019                 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
2020                 break;
2021         case PCI_DEVICE_ID_HORNET:
2022                 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
2023                 GE = 1;
2024                 break;
2025         case PCI_DEVICE_ID_PROTEUS_VF:
2026                 m = (typeof(m)){"LPev12000", "PCIe IOV",
2027                                 "Fibre Channel Adapter"};
2028                 break;
2029         case PCI_DEVICE_ID_PROTEUS_PF:
2030                 m = (typeof(m)){"LPev12000", "PCIe IOV",
2031                                 "Fibre Channel Adapter"};
2032                 break;
2033         case PCI_DEVICE_ID_PROTEUS_S:
2034                 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2035                                 "Fibre Channel Adapter"};
2036                 break;
2037         case PCI_DEVICE_ID_TIGERSHARK:
2038                 oneConnect = 1;
2039                 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
2040                 break;
2041         case PCI_DEVICE_ID_TOMCAT:
2042                 oneConnect = 1;
2043                 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2044                 break;
2045         case PCI_DEVICE_ID_FALCON:
2046                 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2047                                 "EmulexSecure Fibre"};
2048                 break;
2049         case PCI_DEVICE_ID_BALIUS:
2050                 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2051                                 "Fibre Channel Adapter"};
2052                 break;
2053         case PCI_DEVICE_ID_LANCER_FC:
2054         case PCI_DEVICE_ID_LANCER_FC_VF:
2055                 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
2056                 break;
2057         case PCI_DEVICE_ID_LANCER_FCOE:
2058         case PCI_DEVICE_ID_LANCER_FCOE_VF:
2059                 oneConnect = 1;
2060                 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
2061                 break;
2062         default:
2063                 m = (typeof(m)){"Unknown", "", ""};
2064                 break;
2065         }
2066
2067         if (mdp && mdp[0] == '\0')
2068                 snprintf(mdp, 79,"%s", m.name);
2069         /*
2070          * oneConnect hba requires special processing, they are all initiators
2071          * and we put the port number on the end
2072          */
2073         if (descp && descp[0] == '\0') {
2074                 if (oneConnect)
2075                         snprintf(descp, 255,
2076                                 "Emulex OneConnect %s, %s Initiator, Port %s",
2077                                 m.name, m.function,
2078                                 phba->Port);
2079                 else
2080                         snprintf(descp, 255,
2081                                 "Emulex %s %d%s %s %s",
2082                                 m.name, max_speed, (GE) ? "GE" : "Gb",
2083                                 m.bus, m.function);
2084         }
2085 }
2086
2087 /**
2088  * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
2089  * @phba: pointer to lpfc hba data structure.
2090  * @pring: pointer to a IOCB ring.
2091  * @cnt: the number of IOCBs to be posted to the IOCB ring.
2092  *
2093  * This routine posts a given number of IOCBs with the associated DMA buffer
2094  * descriptors specified by the cnt argument to the given IOCB ring.
2095  *
2096  * Return codes
2097  *   The number of IOCBs NOT able to be posted to the IOCB ring.
2098  **/
2099 int
2100 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
2101 {
2102         IOCB_t *icmd;
2103         struct lpfc_iocbq *iocb;
2104         struct lpfc_dmabuf *mp1, *mp2;
2105
2106         cnt += pring->missbufcnt;
2107
2108         /* While there are buffers to post */
2109         while (cnt > 0) {
2110                 /* Allocate buffer for  command iocb */
2111                 iocb = lpfc_sli_get_iocbq(phba);
2112                 if (iocb == NULL) {
2113                         pring->missbufcnt = cnt;
2114                         return cnt;
2115                 }
2116                 icmd = &iocb->iocb;
2117
2118                 /* 2 buffers can be posted per command */
2119                 /* Allocate buffer to post */
2120                 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2121                 if (mp1)
2122                     mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2123                 if (!mp1 || !mp1->virt) {
2124                         kfree(mp1);
2125                         lpfc_sli_release_iocbq(phba, iocb);
2126                         pring->missbufcnt = cnt;
2127                         return cnt;
2128                 }
2129
2130                 INIT_LIST_HEAD(&mp1->list);
2131                 /* Allocate buffer to post */
2132                 if (cnt > 1) {
2133                         mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2134                         if (mp2)
2135                                 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2136                                                             &mp2->phys);
2137                         if (!mp2 || !mp2->virt) {
2138                                 kfree(mp2);
2139                                 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2140                                 kfree(mp1);
2141                                 lpfc_sli_release_iocbq(phba, iocb);
2142                                 pring->missbufcnt = cnt;
2143                                 return cnt;
2144                         }
2145
2146                         INIT_LIST_HEAD(&mp2->list);
2147                 } else {
2148                         mp2 = NULL;
2149                 }
2150
2151                 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2152                 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2153                 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2154                 icmd->ulpBdeCount = 1;
2155                 cnt--;
2156                 if (mp2) {
2157                         icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2158                         icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2159                         icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2160                         cnt--;
2161                         icmd->ulpBdeCount = 2;
2162                 }
2163
2164                 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2165                 icmd->ulpLe = 1;
2166
2167                 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2168                     IOCB_ERROR) {
2169                         lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2170                         kfree(mp1);
2171                         cnt++;
2172                         if (mp2) {
2173                                 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2174                                 kfree(mp2);
2175                                 cnt++;
2176                         }
2177                         lpfc_sli_release_iocbq(phba, iocb);
2178                         pring->missbufcnt = cnt;
2179                         return cnt;
2180                 }
2181                 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
2182                 if (mp2)
2183                         lpfc_sli_ringpostbuf_put(phba, pring, mp2);
2184         }
2185         pring->missbufcnt = 0;
2186         return 0;
2187 }
2188
2189 /**
2190  * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
2191  * @phba: pointer to lpfc hba data structure.
2192  *
2193  * This routine posts initial receive IOCB buffers to the ELS ring. The
2194  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2195  * set to 64 IOCBs.
2196  *
2197  * Return codes
2198  *   0 - success (currently always success)
2199  **/
2200 static int
2201 lpfc_post_rcv_buf(struct lpfc_hba *phba)
2202 {
2203         struct lpfc_sli *psli = &phba->sli;
2204
2205         /* Ring 0, ELS / CT buffers */
2206         lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
2207         /* Ring 2 - FCP no buffers needed */
2208
2209         return 0;
2210 }
2211
2212 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2213
2214 /**
2215  * lpfc_sha_init - Set up initial array of hash table entries
2216  * @HashResultPointer: pointer to an array as hash table.
2217  *
2218  * This routine sets up the initial values to the array of hash table entries
2219  * for the LC HBAs.
2220  **/
2221 static void
2222 lpfc_sha_init(uint32_t * HashResultPointer)
2223 {
2224         HashResultPointer[0] = 0x67452301;
2225         HashResultPointer[1] = 0xEFCDAB89;
2226         HashResultPointer[2] = 0x98BADCFE;
2227         HashResultPointer[3] = 0x10325476;
2228         HashResultPointer[4] = 0xC3D2E1F0;
2229 }
2230
2231 /**
2232  * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2233  * @HashResultPointer: pointer to an initial/result hash table.
2234  * @HashWorkingPointer: pointer to an working hash table.
2235  *
2236  * This routine iterates an initial hash table pointed by @HashResultPointer
2237  * with the values from the working hash table pointeed by @HashWorkingPointer.
2238  * The results are putting back to the initial hash table, returned through
2239  * the @HashResultPointer as the result hash table.
2240  **/
2241 static void
2242 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2243 {
2244         int t;
2245         uint32_t TEMP;
2246         uint32_t A, B, C, D, E;
2247         t = 16;
2248         do {
2249                 HashWorkingPointer[t] =
2250                     S(1,
2251                       HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2252                                                                      8] ^
2253                       HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2254         } while (++t <= 79);
2255         t = 0;
2256         A = HashResultPointer[0];
2257         B = HashResultPointer[1];
2258         C = HashResultPointer[2];
2259         D = HashResultPointer[3];
2260         E = HashResultPointer[4];
2261
2262         do {
2263                 if (t < 20) {
2264                         TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2265                 } else if (t < 40) {
2266                         TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2267                 } else if (t < 60) {
2268                         TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2269                 } else {
2270                         TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2271                 }
2272                 TEMP += S(5, A) + E + HashWorkingPointer[t];
2273                 E = D;
2274                 D = C;
2275                 C = S(30, B);
2276                 B = A;
2277                 A = TEMP;
2278         } while (++t <= 79);
2279
2280         HashResultPointer[0] += A;
2281         HashResultPointer[1] += B;
2282         HashResultPointer[2] += C;
2283         HashResultPointer[3] += D;
2284         HashResultPointer[4] += E;
2285
2286 }
2287
2288 /**
2289  * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2290  * @RandomChallenge: pointer to the entry of host challenge random number array.
2291  * @HashWorking: pointer to the entry of the working hash array.
2292  *
2293  * This routine calculates the working hash array referred by @HashWorking
2294  * from the challenge random numbers associated with the host, referred by
2295  * @RandomChallenge. The result is put into the entry of the working hash
2296  * array and returned by reference through @HashWorking.
2297  **/
2298 static void
2299 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2300 {
2301         *HashWorking = (*RandomChallenge ^ *HashWorking);
2302 }
2303
2304 /**
2305  * lpfc_hba_init - Perform special handling for LC HBA initialization
2306  * @phba: pointer to lpfc hba data structure.
2307  * @hbainit: pointer to an array of unsigned 32-bit integers.
2308  *
2309  * This routine performs the special handling for LC HBA initialization.
2310  **/
2311 void
2312 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2313 {
2314         int t;
2315         uint32_t *HashWorking;
2316         uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2317
2318         HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2319         if (!HashWorking)
2320                 return;
2321
2322         HashWorking[0] = HashWorking[78] = *pwwnn++;
2323         HashWorking[1] = HashWorking[79] = *pwwnn;
2324
2325         for (t = 0; t < 7; t++)
2326                 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2327
2328         lpfc_sha_init(hbainit);
2329         lpfc_sha_iterate(hbainit, HashWorking);
2330         kfree(HashWorking);
2331 }
2332
2333 /**
2334  * lpfc_cleanup - Performs vport cleanups before deleting a vport
2335  * @vport: pointer to a virtual N_Port data structure.
2336  *
2337  * This routine performs the necessary cleanups before deleting the @vport.
2338  * It invokes the discovery state machine to perform necessary state
2339  * transitions and to release the ndlps associated with the @vport. Note,
2340  * the physical port is treated as @vport 0.
2341  **/
2342 void
2343 lpfc_cleanup(struct lpfc_vport *vport)
2344 {
2345         struct lpfc_hba   *phba = vport->phba;
2346         struct lpfc_nodelist *ndlp, *next_ndlp;
2347         int i = 0;
2348
2349         if (phba->link_state > LPFC_LINK_DOWN)
2350                 lpfc_port_link_failure(vport);
2351
2352         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2353                 if (!NLP_CHK_NODE_ACT(ndlp)) {
2354                         ndlp = lpfc_enable_node(vport, ndlp,
2355                                                 NLP_STE_UNUSED_NODE);
2356                         if (!ndlp)
2357                                 continue;
2358                         spin_lock_irq(&phba->ndlp_lock);
2359                         NLP_SET_FREE_REQ(ndlp);
2360                         spin_unlock_irq(&phba->ndlp_lock);
2361                         /* Trigger the release of the ndlp memory */
2362                         lpfc_nlp_put(ndlp);
2363                         continue;
2364                 }
2365                 spin_lock_irq(&phba->ndlp_lock);
2366                 if (NLP_CHK_FREE_REQ(ndlp)) {
2367                         /* The ndlp should not be in memory free mode already */
2368                         spin_unlock_irq(&phba->ndlp_lock);
2369                         continue;
2370                 } else
2371                         /* Indicate request for freeing ndlp memory */
2372                         NLP_SET_FREE_REQ(ndlp);
2373                 spin_unlock_irq(&phba->ndlp_lock);
2374
2375                 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2376                     ndlp->nlp_DID == Fabric_DID) {
2377                         /* Just free up ndlp with Fabric_DID for vports */
2378                         lpfc_nlp_put(ndlp);
2379                         continue;
2380                 }
2381
2382                 /* take care of nodes in unused state before the state
2383                  * machine taking action.
2384                  */
2385                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2386                         lpfc_nlp_put(ndlp);
2387                         continue;
2388                 }
2389
2390                 if (ndlp->nlp_type & NLP_FABRIC)
2391                         lpfc_disc_state_machine(vport, ndlp, NULL,
2392                                         NLP_EVT_DEVICE_RECOVERY);
2393
2394                 lpfc_disc_state_machine(vport, ndlp, NULL,
2395                                              NLP_EVT_DEVICE_RM);
2396         }
2397
2398         /* At this point, ALL ndlp's should be gone
2399          * because of the previous NLP_EVT_DEVICE_RM.
2400          * Lets wait for this to happen, if needed.
2401          */
2402         while (!list_empty(&vport->fc_nodes)) {
2403                 if (i++ > 3000) {
2404                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2405                                 "0233 Nodelist not empty\n");
2406                         list_for_each_entry_safe(ndlp, next_ndlp,
2407                                                 &vport->fc_nodes, nlp_listp) {
2408                                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2409                                                 LOG_NODE,
2410                                                 "0282 did:x%x ndlp:x%p "
2411                                                 "usgmap:x%x refcnt:%d\n",
2412                                                 ndlp->nlp_DID, (void *)ndlp,
2413                                                 ndlp->nlp_usg_map,
2414                                                 atomic_read(
2415                                                         &ndlp->kref.refcount));
2416                         }
2417                         break;
2418                 }
2419
2420                 /* Wait for any activity on ndlps to settle */
2421                 msleep(10);
2422         }
2423         lpfc_cleanup_vports_rrqs(vport, NULL);
2424 }
2425
2426 /**
2427  * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2428  * @vport: pointer to a virtual N_Port data structure.
2429  *
2430  * This routine stops all the timers associated with a @vport. This function
2431  * is invoked before disabling or deleting a @vport. Note that the physical
2432  * port is treated as @vport 0.
2433  **/
2434 void
2435 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2436 {
2437         del_timer_sync(&vport->els_tmofunc);
2438         del_timer_sync(&vport->fc_fdmitmo);
2439         del_timer_sync(&vport->delayed_disc_tmo);
2440         lpfc_can_disctmo(vport);
2441         return;
2442 }
2443
2444 /**
2445  * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2446  * @phba: pointer to lpfc hba data structure.
2447  *
2448  * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2449  * caller of this routine should already hold the host lock.
2450  **/
2451 void
2452 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2453 {
2454         /* Clear pending FCF rediscovery wait flag */
2455         phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2456
2457         /* Now, try to stop the timer */
2458         del_timer(&phba->fcf.redisc_wait);
2459 }
2460
2461 /**
2462  * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2463  * @phba: pointer to lpfc hba data structure.
2464  *
2465  * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2466  * checks whether the FCF rediscovery wait timer is pending with the host
2467  * lock held before proceeding with disabling the timer and clearing the
2468  * wait timer pendig flag.
2469  **/
2470 void
2471 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2472 {
2473         spin_lock_irq(&phba->hbalock);
2474         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2475                 /* FCF rediscovery timer already fired or stopped */
2476                 spin_unlock_irq(&phba->hbalock);
2477                 return;
2478         }
2479         __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2480         /* Clear failover in progress flags */
2481         phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
2482         spin_unlock_irq(&phba->hbalock);
2483 }
2484
2485 /**
2486  * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2487  * @phba: pointer to lpfc hba data structure.
2488  *
2489  * This routine stops all the timers associated with a HBA. This function is
2490  * invoked before either putting a HBA offline or unloading the driver.
2491  **/
2492 void
2493 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2494 {
2495         lpfc_stop_vport_timers(phba->pport);
2496         del_timer_sync(&phba->sli.mbox_tmo);
2497         del_timer_sync(&phba->fabric_block_timer);
2498         del_timer_sync(&phba->eratt_poll);
2499         del_timer_sync(&phba->hb_tmofunc);
2500         if (phba->sli_rev == LPFC_SLI_REV4) {
2501                 del_timer_sync(&phba->rrq_tmr);
2502                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2503         }
2504         phba->hb_outstanding = 0;
2505
2506         switch (phba->pci_dev_grp) {
2507         case LPFC_PCI_DEV_LP:
2508                 /* Stop any LightPulse device specific driver timers */
2509                 del_timer_sync(&phba->fcp_poll_timer);
2510                 break;
2511         case LPFC_PCI_DEV_OC:
2512                 /* Stop any OneConnect device sepcific driver timers */
2513                 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2514                 break;
2515         default:
2516                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2517                                 "0297 Invalid device group (x%x)\n",
2518                                 phba->pci_dev_grp);
2519                 break;
2520         }
2521         return;
2522 }
2523
2524 /**
2525  * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2526  * @phba: pointer to lpfc hba data structure.
2527  *
2528  * This routine marks a HBA's management interface as blocked. Once the HBA's
2529  * management interface is marked as blocked, all the user space access to
2530  * the HBA, whether they are from sysfs interface or libdfc interface will
2531  * all be blocked. The HBA is set to block the management interface when the
2532  * driver prepares the HBA interface for online or offline.
2533  **/
2534 static void
2535 lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
2536 {
2537         unsigned long iflag;
2538         uint8_t actcmd = MBX_HEARTBEAT;
2539         unsigned long timeout;
2540
2541         spin_lock_irqsave(&phba->hbalock, iflag);
2542         phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2543         spin_unlock_irqrestore(&phba->hbalock, iflag);
2544         if (mbx_action == LPFC_MBX_NO_WAIT)
2545                 return;
2546         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2547         spin_lock_irqsave(&phba->hbalock, iflag);
2548         if (phba->sli.mbox_active) {
2549                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
2550                 /* Determine how long we might wait for the active mailbox
2551                  * command to be gracefully completed by firmware.
2552                  */
2553                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2554                                 phba->sli.mbox_active) * 1000) + jiffies;
2555         }
2556         spin_unlock_irqrestore(&phba->hbalock, iflag);
2557
2558         /* Wait for the outstnading mailbox command to complete */
2559         while (phba->sli.mbox_active) {
2560                 /* Check active mailbox complete status every 2ms */
2561                 msleep(2);
2562                 if (time_after(jiffies, timeout)) {
2563                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2564                                 "2813 Mgmt IO is Blocked %x "
2565                                 "- mbox cmd %x still active\n",
2566                                 phba->sli.sli_flag, actcmd);
2567                         break;
2568                 }
2569         }
2570 }
2571
2572 /**
2573  * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2574  * @phba: pointer to lpfc hba data structure.
2575  *
2576  * Allocate RPIs for all active remote nodes. This is needed whenever
2577  * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2578  * is to fixup the temporary rpi assignments.
2579  **/
2580 void
2581 lpfc_sli4_node_prep(struct lpfc_hba *phba)
2582 {
2583         struct lpfc_nodelist  *ndlp, *next_ndlp;
2584         struct lpfc_vport **vports;
2585         int i;
2586
2587         if (phba->sli_rev != LPFC_SLI_REV4)
2588                 return;
2589
2590         vports = lpfc_create_vport_work_array(phba);
2591         if (vports != NULL) {
2592                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2593                         if (vports[i]->load_flag & FC_UNLOADING)
2594                                 continue;
2595
2596                         list_for_each_entry_safe(ndlp, next_ndlp,
2597                                                  &vports[i]->fc_nodes,
2598                                                  nlp_listp) {
2599                                 if (NLP_CHK_NODE_ACT(ndlp))
2600                                         ndlp->nlp_rpi =
2601                                                 lpfc_sli4_alloc_rpi(phba);
2602                         }
2603                 }
2604         }
2605         lpfc_destroy_vport_work_array(phba, vports);
2606 }
2607
2608 /**
2609  * lpfc_online - Initialize and bring a HBA online
2610  * @phba: pointer to lpfc hba data structure.
2611  *
2612  * This routine initializes the HBA and brings a HBA online. During this
2613  * process, the management interface is blocked to prevent user space access
2614  * to the HBA interfering with the driver initialization.
2615  *
2616  * Return codes
2617  *   0 - successful
2618  *   1 - failed
2619  **/
2620 int
2621 lpfc_online(struct lpfc_hba *phba)
2622 {
2623         struct lpfc_vport *vport;
2624         struct lpfc_vport **vports;
2625         int i;
2626
2627         if (!phba)
2628                 return 0;
2629         vport = phba->pport;
2630
2631         if (!(vport->fc_flag & FC_OFFLINE_MODE))
2632                 return 0;
2633
2634         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2635                         "0458 Bring Adapter online\n");
2636
2637         lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
2638
2639         if (!lpfc_sli_queue_setup(phba)) {
2640                 lpfc_unblock_mgmt_io(phba);
2641                 return 1;
2642         }
2643
2644         if (phba->sli_rev == LPFC_SLI_REV4) {
2645                 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2646                         lpfc_unblock_mgmt_io(phba);
2647                         return 1;
2648                 }
2649         } else {
2650                 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2651                         lpfc_unblock_mgmt_io(phba);
2652                         return 1;
2653                 }
2654         }
2655
2656         vports = lpfc_create_vport_work_array(phba);
2657         if (vports != NULL)
2658                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2659                         struct Scsi_Host *shost;
2660                         shost = lpfc_shost_from_vport(vports[i]);
2661                         spin_lock_irq(shost->host_lock);
2662                         vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2663                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2664                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2665                         if (phba->sli_rev == LPFC_SLI_REV4)
2666                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
2667                         spin_unlock_irq(shost->host_lock);
2668                 }
2669                 lpfc_destroy_vport_work_array(phba, vports);
2670
2671         lpfc_unblock_mgmt_io(phba);
2672         return 0;
2673 }
2674
2675 /**
2676  * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
2677  * @phba: pointer to lpfc hba data structure.
2678  *
2679  * This routine marks a HBA's management interface as not blocked. Once the
2680  * HBA's management interface is marked as not blocked, all the user space
2681  * access to the HBA, whether they are from sysfs interface or libdfc
2682  * interface will be allowed. The HBA is set to block the management interface
2683  * when the driver prepares the HBA interface for online or offline and then
2684  * set to unblock the management interface afterwards.
2685  **/
2686 void
2687 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2688 {
2689         unsigned long iflag;
2690
2691         spin_lock_irqsave(&phba->hbalock, iflag);
2692         phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2693         spin_unlock_irqrestore(&phba->hbalock, iflag);
2694 }
2695
2696 /**
2697  * lpfc_offline_prep - Prepare a HBA to be brought offline
2698  * @phba: pointer to lpfc hba data structure.
2699  *
2700  * This routine is invoked to prepare a HBA to be brought offline. It performs
2701  * unregistration login to all the nodes on all vports and flushes the mailbox
2702  * queue to make it ready to be brought offline.
2703  **/
2704 void
2705 lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
2706 {
2707         struct lpfc_vport *vport = phba->pport;
2708         struct lpfc_nodelist  *ndlp, *next_ndlp;
2709         struct lpfc_vport **vports;
2710         struct Scsi_Host *shost;
2711         int i;
2712
2713         if (vport->fc_flag & FC_OFFLINE_MODE)
2714                 return;
2715
2716         lpfc_block_mgmt_io(phba, mbx_action);
2717
2718         lpfc_linkdown(phba);
2719
2720         /* Issue an unreg_login to all nodes on all vports */
2721         vports = lpfc_create_vport_work_array(phba);
2722         if (vports != NULL) {
2723                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2724                         if (vports[i]->load_flag & FC_UNLOADING)
2725                                 continue;
2726                         shost = lpfc_shost_from_vport(vports[i]);
2727                         spin_lock_irq(shost->host_lock);
2728                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
2729                         vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2730                         vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2731                         spin_unlock_irq(shost->host_lock);
2732
2733                         shost = lpfc_shost_from_vport(vports[i]);
2734                         list_for_each_entry_safe(ndlp, next_ndlp,
2735                                                  &vports[i]->fc_nodes,
2736                                                  nlp_listp) {
2737                                 if (!NLP_CHK_NODE_ACT(ndlp))
2738                                         continue;
2739                                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2740                                         continue;
2741                                 if (ndlp->nlp_type & NLP_FABRIC) {
2742                                         lpfc_disc_state_machine(vports[i], ndlp,
2743                                                 NULL, NLP_EVT_DEVICE_RECOVERY);
2744                                         lpfc_disc_state_machine(vports[i], ndlp,
2745                                                 NULL, NLP_EVT_DEVICE_RM);
2746                                 }
2747                                 spin_lock_irq(shost->host_lock);
2748                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2749                                 spin_unlock_irq(shost->host_lock);
2750                                 /*
2751                                  * Whenever an SLI4 port goes offline, free the
2752                                  * RPI. Get a new RPI when the adapter port
2753                                  * comes back online.
2754                                  */
2755                                 if (phba->sli_rev == LPFC_SLI_REV4)
2756                                         lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
2757                                 lpfc_unreg_rpi(vports[i], ndlp);
2758                         }
2759                 }
2760         }
2761         lpfc_destroy_vport_work_array(phba, vports);
2762
2763         lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
2764 }
2765
2766 /**
2767  * lpfc_offline - Bring a HBA offline
2768  * @phba: pointer to lpfc hba data structure.
2769  *
2770  * This routine actually brings a HBA offline. It stops all the timers
2771  * associated with the HBA, brings down the SLI layer, and eventually
2772  * marks the HBA as in offline state for the upper layer protocol.
2773  **/
2774 void
2775 lpfc_offline(struct lpfc_hba *phba)
2776 {
2777         struct Scsi_Host  *shost;
2778         struct lpfc_vport **vports;
2779         int i;
2780
2781         if (phba->pport->fc_flag & FC_OFFLINE_MODE)
2782                 return;
2783
2784         /* stop port and all timers associated with this hba */
2785         lpfc_stop_port(phba);
2786         vports = lpfc_create_vport_work_array(phba);
2787         if (vports != NULL)
2788                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
2789                         lpfc_stop_vport_timers(vports[i]);
2790         lpfc_destroy_vport_work_array(phba, vports);
2791         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2792                         "0460 Bring Adapter offline\n");
2793         /* Bring down the SLI Layer and cleanup.  The HBA is offline
2794            now.  */
2795         lpfc_sli_hba_down(phba);
2796         spin_lock_irq(&phba->hbalock);
2797         phba->work_ha = 0;
2798         spin_unlock_irq(&phba->hbalock);
2799         vports = lpfc_create_vport_work_array(phba);
2800         if (vports != NULL)
2801                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2802                         shost = lpfc_shost_from_vport(vports[i]);
2803                         spin_lock_irq(shost->host_lock);
2804                         vports[i]->work_port_events = 0;
2805                         vports[i]->fc_flag |= FC_OFFLINE_MODE;
2806                         spin_unlock_irq(shost->host_lock);
2807                 }
2808         lpfc_destroy_vport_work_array(phba, vports);
2809 }
2810
2811 /**
2812  * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
2813  * @phba: pointer to lpfc hba data structure.
2814  *
2815  * This routine is to free all the SCSI buffers and IOCBs from the driver
2816  * list back to kernel. It is called from lpfc_pci_remove_one to free
2817  * the internal resources before the device is removed from the system.
2818  **/
2819 static void
2820 lpfc_scsi_free(struct lpfc_hba *phba)
2821 {
2822         struct lpfc_scsi_buf *sb, *sb_next;
2823         struct lpfc_iocbq *io, *io_next;
2824
2825         spin_lock_irq(&phba->hbalock);
2826         /* Release all the lpfc_scsi_bufs maintained by this host. */
2827         spin_lock(&phba->scsi_buf_list_lock);
2828         list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2829                 list_del(&sb->list);
2830                 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2831                               sb->dma_handle);
2832                 kfree(sb);
2833                 phba->total_scsi_bufs--;
2834         }
2835         spin_unlock(&phba->scsi_buf_list_lock);
2836
2837         /* Release all the lpfc_iocbq entries maintained by this host. */
2838         list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2839                 list_del(&io->list);
2840                 kfree(io);
2841                 phba->total_iocbq_bufs--;
2842         }
2843
2844         spin_unlock_irq(&phba->hbalock);
2845 }
2846
2847 /**
2848  * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2849  * @phba: pointer to lpfc hba data structure.
2850  *
2851  * This routine first calculates the sizes of the current els and allocated
2852  * scsi sgl lists, and then goes through all sgls to updates the physical
2853  * XRIs assigned due to port function reset. During port initialization, the
2854  * current els and allocated scsi sgl lists are 0s.
2855  *
2856  * Return codes
2857  *   0 - successful (for now, it always returns 0)
2858  **/
2859 int
2860 lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2861 {
2862         struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2863         struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2864         uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2865         LIST_HEAD(els_sgl_list);
2866         LIST_HEAD(scsi_sgl_list);
2867         int rc;
2868
2869         /*
2870          * update on pci function's els xri-sgl list
2871          */
2872         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2873         if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2874                 /* els xri-sgl expanded */
2875                 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2876                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2877                                 "3157 ELS xri-sgl count increased from "
2878                                 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2879                                 els_xri_cnt);
2880                 /* allocate the additional els sgls */
2881                 for (i = 0; i < xri_cnt; i++) {
2882                         sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2883                                              GFP_KERNEL);
2884                         if (sglq_entry == NULL) {
2885                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2886                                                 "2562 Failure to allocate an "
2887                                                 "ELS sgl entry:%d\n", i);
2888                                 rc = -ENOMEM;
2889                                 goto out_free_mem;
2890                         }
2891                         sglq_entry->buff_type = GEN_BUFF_TYPE;
2892                         sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2893                                                            &sglq_entry->phys);
2894                         if (sglq_entry->virt == NULL) {
2895                                 kfree(sglq_entry);
2896                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2897                                                 "2563 Failure to allocate an "
2898                                                 "ELS mbuf:%d\n", i);
2899                                 rc = -ENOMEM;
2900                                 goto out_free_mem;
2901                         }
2902                         sglq_entry->sgl = sglq_entry->virt;
2903                         memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2904                         sglq_entry->state = SGL_FREED;
2905                         list_add_tail(&sglq_entry->list, &els_sgl_list);
2906                 }
2907                 spin_lock(&phba->hbalock);
2908                 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2909                 spin_unlock(&phba->hbalock);
2910         } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2911                 /* els xri-sgl shrinked */
2912                 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2913                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2914                                 "3158 ELS xri-sgl count decreased from "
2915                                 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2916                                 els_xri_cnt);
2917                 spin_lock_irq(&phba->hbalock);
2918                 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2919                 spin_unlock_irq(&phba->hbalock);
2920                 /* release extra els sgls from list */
2921                 for (i = 0; i < xri_cnt; i++) {
2922                         list_remove_head(&els_sgl_list,
2923                                          sglq_entry, struct lpfc_sglq, list);
2924                         if (sglq_entry) {
2925                                 lpfc_mbuf_free(phba, sglq_entry->virt,
2926                                                sglq_entry->phys);
2927                                 kfree(sglq_entry);
2928                         }
2929                 }
2930                 spin_lock_irq(&phba->hbalock);
2931                 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2932                 spin_unlock_irq(&phba->hbalock);
2933         } else
2934                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2935                                 "3163 ELS xri-sgl count unchanged: %d\n",
2936                                 els_xri_cnt);
2937         phba->sli4_hba.els_xri_cnt = els_xri_cnt;
2938
2939         /* update xris to els sgls on the list */
2940         sglq_entry = NULL;
2941         sglq_entry_next = NULL;
2942         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
2943                                  &phba->sli4_hba.lpfc_sgl_list, list) {
2944                 lxri = lpfc_sli4_next_xritag(phba);
2945                 if (lxri == NO_XRI) {
2946                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2947                                         "2400 Failed to allocate xri for "
2948                                         "ELS sgl\n");
2949                         rc = -ENOMEM;
2950                         goto out_free_mem;
2951                 }
2952                 sglq_entry->sli4_lxritag = lxri;
2953                 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2954         }
2955
2956         /*
2957          * update on pci function's allocated scsi xri-sgl list
2958          */
2959         phba->total_scsi_bufs = 0;
2960
2961         /* maximum number of xris available for scsi buffers */
2962         phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
2963                                       els_xri_cnt;
2964
2965         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2966                         "2401 Current allocated SCSI xri-sgl count:%d, "
2967                         "maximum  SCSI xri count:%d\n",
2968                         phba->sli4_hba.scsi_xri_cnt,
2969                         phba->sli4_hba.scsi_xri_max);
2970
2971         spin_lock_irq(&phba->scsi_buf_list_lock);
2972         list_splice_init(&phba->lpfc_scsi_buf_list, &scsi_sgl_list);
2973         spin_unlock_irq(&phba->scsi_buf_list_lock);
2974
2975         if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
2976                 /* max scsi xri shrinked below the allocated scsi buffers */
2977                 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
2978                                         phba->sli4_hba.scsi_xri_max;
2979                 /* release the extra allocated scsi buffers */
2980                 for (i = 0; i < scsi_xri_cnt; i++) {
2981                         list_remove_head(&scsi_sgl_list, psb,
2982                                          struct lpfc_scsi_buf, list);
2983                         pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
2984                                       psb->dma_handle);
2985                         kfree(psb);
2986                 }
2987                 spin_lock_irq(&phba->scsi_buf_list_lock);
2988                 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
2989                 spin_unlock_irq(&phba->scsi_buf_list_lock);
2990         }
2991
2992         /* update xris associated to remaining allocated scsi buffers */
2993         psb = NULL;
2994         psb_next = NULL;
2995         list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
2996                 lxri = lpfc_sli4_next_xritag(phba);
2997                 if (lxri == NO_XRI) {
2998                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2999                                         "2560 Failed to allocate xri for "
3000                                         "scsi buffer\n");
3001                         rc = -ENOMEM;
3002                         goto out_free_mem;
3003                 }
3004                 psb->cur_iocbq.sli4_lxritag = lxri;
3005                 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3006         }
3007         spin_lock(&phba->scsi_buf_list_lock);
3008         list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list);
3009         spin_unlock(&phba->scsi_buf_list_lock);
3010
3011         return 0;
3012
3013 out_free_mem:
3014         lpfc_free_els_sgl_list(phba);
3015         lpfc_scsi_free(phba);
3016         return rc;
3017 }
3018
3019 /**
3020  * lpfc_create_port - Create an FC port
3021  * @phba: pointer to lpfc hba data structure.
3022  * @instance: a unique integer ID to this FC port.
3023  * @dev: pointer to the device data structure.
3024  *
3025  * This routine creates a FC port for the upper layer protocol. The FC port
3026  * can be created on top of either a physical port or a virtual port provided
3027  * by the HBA. This routine also allocates a SCSI host data structure (shost)
3028  * and associates the FC port created before adding the shost into the SCSI
3029  * layer.
3030  *
3031  * Return codes
3032  *   @vport - pointer to the virtual N_Port data structure.
3033  *   NULL - port create failed.
3034  **/
3035 struct lpfc_vport *
3036 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
3037 {
3038         struct lpfc_vport *vport;
3039         struct Scsi_Host  *shost;
3040         int error = 0;
3041
3042         if (dev != &phba->pcidev->dev)
3043                 shost = scsi_host_alloc(&lpfc_vport_template,
3044                                         sizeof(struct lpfc_vport));
3045         else
3046                 shost = scsi_host_alloc(&lpfc_template,
3047                                         sizeof(struct lpfc_vport));
3048         if (!shost)
3049                 goto out;
3050
3051         vport = (struct lpfc_vport *) shost->hostdata;
3052         vport->phba = phba;
3053         vport->load_flag |= FC_LOADING;
3054         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3055         vport->fc_rscn_flush = 0;
3056
3057         lpfc_get_vport_cfgparam(vport);
3058         shost->unique_id = instance;
3059         shost->max_id = LPFC_MAX_TARGET;
3060         shost->max_lun = vport->cfg_max_luns;
3061         shost->this_id = -1;
3062         shost->max_cmd_len = 16;
3063         if (phba->sli_rev == LPFC_SLI_REV4) {
3064                 shost->dma_boundary =
3065                         phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
3066                 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3067         }
3068
3069         /*
3070          * Set initial can_queue value since 0 is no longer supported and
3071          * scsi_add_host will fail. This will be adjusted later based on the
3072          * max xri value determined in hba setup.
3073          */
3074         shost->can_queue = phba->cfg_hba_queue_depth - 10;
3075         if (dev != &phba->pcidev->dev) {
3076                 shost->transportt = lpfc_vport_transport_template;
3077                 vport->port_type = LPFC_NPIV_PORT;
3078         } else {
3079                 shost->transportt = lpfc_transport_template;
3080                 vport->port_type = LPFC_PHYSICAL_PORT;
3081         }
3082
3083         /* Initialize all internally managed lists. */
3084         INIT_LIST_HEAD(&vport->fc_nodes);
3085         INIT_LIST_HEAD(&vport->rcv_buffer_list);
3086         spin_lock_init(&vport->work_port_lock);
3087
3088         init_timer(&vport->fc_disctmo);
3089         vport->fc_disctmo.function = lpfc_disc_timeout;
3090         vport->fc_disctmo.data = (unsigned long)vport;
3091
3092         init_timer(&vport->fc_fdmitmo);
3093         vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
3094         vport->fc_fdmitmo.data = (unsigned long)vport;
3095
3096         init_timer(&vport->els_tmofunc);
3097         vport->els_tmofunc.function = lpfc_els_timeout;
3098         vport->els_tmofunc.data = (unsigned long)vport;
3099
3100         init_timer(&vport->delayed_disc_tmo);
3101         vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3102         vport->delayed_disc_tmo.data = (unsigned long)vport;
3103
3104         error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
3105         if (error)
3106                 goto out_put_shost;
3107
3108         spin_lock_irq(&phba->hbalock);
3109         list_add_tail(&vport->listentry, &phba->port_list);
3110         spin_unlock_irq(&phba->hbalock);
3111         return vport;
3112
3113 out_put_shost:
3114         scsi_host_put(shost);
3115 out:
3116         return NULL;
3117 }
3118
3119 /**
3120  * destroy_port -  destroy an FC port
3121  * @vport: pointer to an lpfc virtual N_Port data structure.
3122  *
3123  * This routine destroys a FC port from the upper layer protocol. All the
3124  * resources associated with the port are released.
3125  **/
3126 void
3127 destroy_port(struct lpfc_vport *vport)
3128 {
3129         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3130         struct lpfc_hba  *phba = vport->phba;
3131
3132         lpfc_debugfs_terminate(vport);
3133         fc_remove_host(shost);
3134         scsi_remove_host(shost);
3135
3136         spin_lock_irq(&phba->hbalock);
3137         list_del_init(&vport->listentry);
3138         spin_unlock_irq(&phba->hbalock);
3139
3140         lpfc_cleanup(vport);
3141         return;
3142 }
3143
3144 /**
3145  * lpfc_get_instance - Get a unique integer ID
3146  *
3147  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3148  * uses the kernel idr facility to perform the task.
3149  *
3150  * Return codes:
3151  *   instance - a unique integer ID allocated as the new instance.
3152  *   -1 - lpfc get instance failed.
3153  **/
3154 int
3155 lpfc_get_instance(void)
3156 {
3157         int instance = 0;
3158
3159         /* Assign an unused number */
3160         if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
3161                 return -1;
3162         if (idr_get_new(&lpfc_hba_index, NULL, &instance))
3163                 return -1;
3164         return instance;
3165 }
3166
3167 /**
3168  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
3169  * @shost: pointer to SCSI host data structure.
3170  * @time: elapsed time of the scan in jiffies.
3171  *
3172  * This routine is called by the SCSI layer with a SCSI host to determine
3173  * whether the scan host is finished.
3174  *
3175  * Note: there is no scan_start function as adapter initialization will have
3176  * asynchronously kicked off the link initialization.
3177  *
3178  * Return codes
3179  *   0 - SCSI host scan is not over yet.
3180  *   1 - SCSI host scan is over.
3181  **/
3182 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3183 {
3184         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3185         struct lpfc_hba   *phba = vport->phba;
3186         int stat = 0;
3187
3188         spin_lock_irq(shost->host_lock);
3189
3190         if (vport->load_flag & FC_UNLOADING) {
3191                 stat = 1;
3192                 goto finished;
3193         }
3194         if (time >= 30 * HZ) {
3195                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3196                                 "0461 Scanning longer than 30 "
3197                                 "seconds.  Continuing initialization\n");
3198                 stat = 1;
3199                 goto finished;
3200         }
3201         if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
3202                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3203                                 "0465 Link down longer than 15 "
3204                                 "seconds.  Continuing initialization\n");
3205                 stat = 1;
3206                 goto finished;
3207         }
3208
3209         if (vport->port_state != LPFC_VPORT_READY)
3210                 goto finished;
3211         if (vport->num_disc_nodes || vport->fc_prli_sent)
3212                 goto finished;
3213         if (vport->fc_map_cnt == 0 && time < 2 * HZ)
3214                 goto finished;
3215         if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
3216                 goto finished;
3217
3218         stat = 1;
3219
3220 finished:
3221         spin_unlock_irq(shost->host_lock);
3222         return stat;
3223 }
3224
3225 /**
3226  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
3227  * @shost: pointer to SCSI host data structure.
3228  *
3229  * This routine initializes a given SCSI host attributes on a FC port. The
3230  * SCSI host can be either on top of a physical port or a virtual port.
3231  **/
3232 void lpfc_host_attrib_init(struct Scsi_Host *shost)
3233 {
3234         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3235         struct lpfc_hba   *phba = vport->phba;
3236         /*
3237          * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
3238          */
3239
3240         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3241         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
3242         fc_host_supported_classes(shost) = FC_COS_CLASS3;
3243
3244         memset(fc_host_supported_fc4s(shost), 0,
3245                sizeof(fc_host_supported_fc4s(shost)));
3246         fc_host_supported_fc4s(shost)[2] = 1;
3247         fc_host_supported_fc4s(shost)[7] = 1;
3248
3249         lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3250                                  sizeof fc_host_symbolic_name(shost));
3251
3252         fc_host_supported_speeds(shost) = 0;
3253         if (phba->lmt & LMT_16Gb)
3254                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
3255         if (phba->lmt & LMT_10Gb)
3256                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
3257         if (phba->lmt & LMT_8Gb)
3258                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
3259         if (phba->lmt & LMT_4Gb)
3260                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3261         if (phba->lmt & LMT_2Gb)
3262                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3263         if (phba->lmt & LMT_1Gb)
3264                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3265
3266         fc_host_maxframe_size(shost) =
3267                 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3268                 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
3269
3270         fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3271
3272         /* This value is also unchanging */
3273         memset(fc_host_active_fc4s(shost), 0,
3274                sizeof(fc_host_active_fc4s(shost)));
3275         fc_host_active_fc4s(shost)[2] = 1;
3276         fc_host_active_fc4s(shost)[7] = 1;
3277
3278         fc_host_max_npiv_vports(shost) = phba->max_vpi;
3279         spin_lock_irq(shost->host_lock);
3280         vport->load_flag &= ~FC_LOADING;
3281         spin_unlock_irq(shost->host_lock);
3282 }
3283
3284 /**
3285  * lpfc_stop_port_s3 - Stop SLI3 device port
3286  * @phba: pointer to lpfc hba data structure.
3287  *
3288  * This routine is invoked to stop an SLI3 device port, it stops the device
3289  * from generating interrupts and stops the device driver's timers for the
3290  * device.
3291  **/
3292 static void
3293 lpfc_stop_port_s3(struct lpfc_hba *phba)
3294 {
3295         /* Clear all interrupt enable conditions */
3296         writel(0, phba->HCregaddr);
3297         readl(phba->HCregaddr); /* flush */
3298         /* Clear all pending interrupts */
3299         writel(0xffffffff, phba->HAregaddr);
3300         readl(phba->HAregaddr); /* flush */
3301
3302         /* Reset some HBA SLI setup states */
3303         lpfc_stop_hba_timers(phba);
3304         phba->pport->work_port_events = 0;
3305 }
3306
3307 /**
3308  * lpfc_stop_port_s4 - Stop SLI4 device port
3309  * @phba: pointer to lpfc hba data structure.
3310  *
3311  * This routine is invoked to stop an SLI4 device port, it stops the device
3312  * from generating interrupts and stops the device driver's timers for the
3313  * device.
3314  **/
3315 static void
3316 lpfc_stop_port_s4(struct lpfc_hba *phba)
3317 {
3318         /* Reset some HBA SLI4 setup states */
3319         lpfc_stop_hba_timers(phba);
3320         phba->pport->work_port_events = 0;
3321         phba->sli4_hba.intr_enable = 0;
3322 }
3323
3324 /**
3325  * lpfc_stop_port - Wrapper function for stopping hba port
3326  * @phba: Pointer to HBA context object.
3327  *
3328  * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3329  * the API jump table function pointer from the lpfc_hba struct.
3330  **/
3331 void
3332 lpfc_stop_port(struct lpfc_hba *phba)
3333 {
3334         phba->lpfc_stop_port(phba);
3335 }
3336
3337 /**
3338  * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3339  * @phba: Pointer to hba for which this call is being executed.
3340  *
3341  * This routine starts the timer waiting for the FCF rediscovery to complete.
3342  **/
3343 void
3344 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3345 {
3346         unsigned long fcf_redisc_wait_tmo =
3347                 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3348         /* Start fcf rediscovery wait period timer */
3349         mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3350         spin_lock_irq(&phba->hbalock);
3351         /* Allow action to new fcf asynchronous event */
3352         phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3353         /* Mark the FCF rediscovery pending state */
3354         phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3355         spin_unlock_irq(&phba->hbalock);
3356 }
3357
3358 /**
3359  * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3360  * @ptr: Map to lpfc_hba data structure pointer.
3361  *
3362  * This routine is invoked when waiting for FCF table rediscover has been
3363  * timed out. If new FCF record(s) has (have) been discovered during the
3364  * wait period, a new FCF event shall be added to the FCOE async event
3365  * list, and then worker thread shall be waked up for processing from the
3366  * worker thread context.
3367  **/
3368 void
3369 lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3370 {
3371         struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3372
3373         /* Don't send FCF rediscovery event if timer cancelled */
3374         spin_lock_irq(&phba->hbalock);
3375         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3376                 spin_unlock_irq(&phba->hbalock);
3377                 return;
3378         }
3379         /* Clear FCF rediscovery timer pending flag */
3380         phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3381         /* FCF rediscovery event to worker thread */
3382         phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3383         spin_unlock_irq(&phba->hbalock);
3384         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3385                         "2776 FCF rediscover quiescent timer expired\n");
3386         /* wake up worker thread */
3387         lpfc_worker_wake_up(phba);
3388 }
3389
3390 /**
3391  * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3392  * @phba: pointer to lpfc hba data structure.
3393  * @acqe_link: pointer to the async link completion queue entry.
3394  *
3395  * This routine is to parse the SLI4 link-attention link fault code and
3396  * translate it into the base driver's read link attention mailbox command
3397  * status.
3398  *
3399  * Return: Link-attention status in terms of base driver's coding.
3400  **/
3401 static uint16_t
3402 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3403                            struct lpfc_acqe_link *acqe_link)
3404 {
3405         uint16_t latt_fault;
3406
3407         switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3408         case LPFC_ASYNC_LINK_FAULT_NONE:
3409         case LPFC_ASYNC_LINK_FAULT_LOCAL:
3410         case LPFC_ASYNC_LINK_FAULT_REMOTE:
3411                 latt_fault = 0;
3412                 break;
3413         default:
3414                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3415                                 "0398 Invalid link fault code: x%x\n",
3416                                 bf_get(lpfc_acqe_link_fault, acqe_link));
3417                 latt_fault = MBXERR_ERROR;
3418                 break;
3419         }
3420         return latt_fault;
3421 }
3422
3423 /**
3424  * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3425  * @phba: pointer to lpfc hba data structure.
3426  * @acqe_link: pointer to the async link completion queue entry.
3427  *
3428  * This routine is to parse the SLI4 link attention type and translate it
3429  * into the base driver's link attention type coding.
3430  *
3431  * Return: Link attention type in terms of base driver's coding.
3432  **/
3433 static uint8_t
3434 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3435                           struct lpfc_acqe_link *acqe_link)
3436 {
3437         uint8_t att_type;
3438
3439         switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3440         case LPFC_ASYNC_LINK_STATUS_DOWN:
3441         case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3442                 att_type = LPFC_ATT_LINK_DOWN;
3443                 break;
3444         case LPFC_ASYNC_LINK_STATUS_UP:
3445                 /* Ignore physical link up events - wait for logical link up */
3446                 att_type = LPFC_ATT_RESERVED;
3447                 break;
3448         case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3449                 att_type = LPFC_ATT_LINK_UP;
3450                 break;
3451         default:
3452                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3453                                 "0399 Invalid link attention type: x%x\n",
3454                                 bf_get(lpfc_acqe_link_status, acqe_link));
3455                 att_type = LPFC_ATT_RESERVED;
3456                 break;
3457         }
3458         return att_type;
3459 }
3460
3461 /**
3462  * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3463  * @phba: pointer to lpfc hba data structure.
3464  * @acqe_link: pointer to the async link completion queue entry.
3465  *
3466  * This routine is to parse the SLI4 link-attention link speed and translate
3467  * it into the base driver's link-attention link speed coding.
3468  *
3469  * Return: Link-attention link speed in terms of base driver's coding.
3470  **/
3471 static uint8_t
3472 lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3473                                 struct lpfc_acqe_link *acqe_link)
3474 {
3475         uint8_t link_speed;
3476
3477         switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3478         case LPFC_ASYNC_LINK_SPEED_ZERO:
3479         case LPFC_ASYNC_LINK_SPEED_10MBPS:
3480         case LPFC_ASYNC_LINK_SPEED_100MBPS:
3481                 link_speed = LPFC_LINK_SPEED_UNKNOWN;
3482                 break;
3483         case LPFC_ASYNC_LINK_SPEED_1GBPS:
3484                 link_speed = LPFC_LINK_SPEED_1GHZ;
3485                 break;
3486         case LPFC_ASYNC_LINK_SPEED_10GBPS:
3487                 link_speed = LPFC_LINK_SPEED_10GHZ;
3488                 break;
3489         default:
3490                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3491                                 "0483 Invalid link-attention link speed: x%x\n",
3492                                 bf_get(lpfc_acqe_link_speed, acqe_link));
3493                 link_speed = LPFC_LINK_SPEED_UNKNOWN;
3494                 break;
3495         }
3496         return link_speed;
3497 }
3498
3499 /**
3500  * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
3501  * @phba: pointer to lpfc hba data structure.
3502  * @acqe_link: pointer to the async link completion queue entry.
3503  *
3504  * This routine is to handle the SLI4 asynchronous FCoE link event.
3505  **/
3506 static void
3507 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3508                          struct lpfc_acqe_link *acqe_link)
3509 {
3510         struct lpfc_dmabuf *mp;
3511         LPFC_MBOXQ_t *pmb;
3512         MAILBOX_t *mb;
3513         struct lpfc_mbx_read_top *la;
3514         uint8_t att_type;
3515         int rc;
3516
3517         att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3518         if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
3519                 return;
3520         phba->fcoe_eventtag = acqe_link->event_tag;
3521         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3522         if (!pmb) {
3523                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3524                                 "0395 The mboxq allocation failed\n");
3525                 return;
3526         }
3527         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3528         if (!mp) {
3529                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3530                                 "0396 The lpfc_dmabuf allocation failed\n");
3531                 goto out_free_pmb;
3532         }
3533         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3534         if (!mp->virt) {
3535                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3536                                 "0397 The mbuf allocation failed\n");
3537                 goto out_free_dmabuf;
3538         }
3539
3540         /* Cleanup any outstanding ELS commands */
3541         lpfc_els_flush_all_cmd(phba);
3542
3543         /* Block ELS IOCBs until we have done process link event */
3544         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3545
3546         /* Update link event statistics */
3547         phba->sli.slistat.link_event++;
3548
3549         /* Create lpfc_handle_latt mailbox command from link ACQE */
3550         lpfc_read_topology(phba, pmb, mp);
3551         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3552         pmb->vport = phba->pport;
3553
3554         /* Keep the link status for extra SLI4 state machine reference */
3555         phba->sli4_hba.link_state.speed =
3556                                 bf_get(lpfc_acqe_link_speed, acqe_link);
3557         phba->sli4_hba.link_state.duplex =
3558                                 bf_get(lpfc_acqe_link_duplex, acqe_link);
3559         phba->sli4_hba.link_state.status =
3560                                 bf_get(lpfc_acqe_link_status, acqe_link);
3561         phba->sli4_hba.link_state.type =
3562                                 bf_get(lpfc_acqe_link_type, acqe_link);
3563         phba->sli4_hba.link_state.number =
3564                                 bf_get(lpfc_acqe_link_number, acqe_link);
3565         phba->sli4_hba.link_state.fault =
3566                                 bf_get(lpfc_acqe_link_fault, acqe_link);
3567         phba->sli4_hba.link_state.logical_speed =
3568                         bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3569         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3570                         "2900 Async FC/FCoE Link event - Speed:%dGBit "
3571                         "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3572                         "Logical speed:%dMbps Fault:%d\n",
3573                         phba->sli4_hba.link_state.speed,
3574                         phba->sli4_hba.link_state.topology,
3575                         phba->sli4_hba.link_state.status,
3576                         phba->sli4_hba.link_state.type,
3577                         phba->sli4_hba.link_state.number,
3578                         phba->sli4_hba.link_state.logical_speed * 10,
3579                         phba->sli4_hba.link_state.fault);
3580         /*
3581          * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3582          * topology info. Note: Optional for non FC-AL ports.
3583          */
3584         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3585                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3586                 if (rc == MBX_NOT_FINISHED)
3587                         goto out_free_dmabuf;
3588                 return;
3589         }
3590         /*
3591          * For FCoE Mode: fill in all the topology information we need and call
3592          * the READ_TOPOLOGY completion routine to continue without actually
3593          * sending the READ_TOPOLOGY mailbox command to the port.
3594          */
3595         /* Parse and translate status field */
3596         mb = &pmb->u.mb;
3597         mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3598
3599         /* Parse and translate link attention fields */
3600         la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3601         la->eventTag = acqe_link->event_tag;
3602         bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3603         bf_set(lpfc_mbx_read_top_link_spd, la,
3604                lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3605
3606         /* Fake the the following irrelvant fields */
3607         bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3608         bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3609         bf_set(lpfc_mbx_read_top_il, la, 0);
3610         bf_set(lpfc_mbx_read_top_pb, la, 0);
3611         bf_set(lpfc_mbx_read_top_fa, la, 0);
3612         bf_set(lpfc_mbx_read_top_mm, la, 0);
3613
3614         /* Invoke the lpfc_handle_latt mailbox command callback function */
3615         lpfc_mbx_cmpl_read_topology(phba, pmb);
3616
3617         return;
3618
3619 out_free_dmabuf:
3620         kfree(mp);
3621 out_free_pmb:
3622         mempool_free(pmb, phba->mbox_mem_pool);
3623 }
3624
3625 /**
3626  * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3627  * @phba: pointer to lpfc hba data structure.
3628  * @acqe_fc: pointer to the async fc completion queue entry.
3629  *
3630  * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3631  * that the event was received and then issue a read_topology mailbox command so
3632  * that the rest of the driver will treat it the same as SLI3.
3633  **/
3634 static void
3635 lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3636 {
3637         struct lpfc_dmabuf *mp;
3638         LPFC_MBOXQ_t *pmb;
3639         int rc;
3640
3641         if (bf_get(lpfc_trailer_type, acqe_fc) !=
3642             LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3643                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3644                                 "2895 Non FC link Event detected.(%d)\n",
3645                                 bf_get(lpfc_trailer_type, acqe_fc));
3646                 return;
3647         }
3648         /* Keep the link status for extra SLI4 state machine reference */
3649         phba->sli4_hba.link_state.speed =
3650                                 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3651         phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3652         phba->sli4_hba.link_state.topology =
3653                                 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3654         phba->sli4_hba.link_state.status =
3655                                 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3656         phba->sli4_hba.link_state.type =
3657                                 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3658         phba->sli4_hba.link_state.number =
3659                                 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3660         phba->sli4_hba.link_state.fault =
3661                                 bf_get(lpfc_acqe_link_fault, acqe_fc);
3662         phba->sli4_hba.link_state.logical_speed =
3663                                 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3664         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3665                         "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3666                         "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3667                         "%dMbps Fault:%d\n",
3668                         phba->sli4_hba.link_state.speed,
3669                         phba->sli4_hba.link_state.topology,
3670                         phba->sli4_hba.link_state.status,
3671                         phba->sli4_hba.link_state.type,
3672                         phba->sli4_hba.link_state.number,
3673                         phba->sli4_hba.link_state.logical_speed * 10,
3674                         phba->sli4_hba.link_state.fault);
3675         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3676         if (!pmb) {
3677                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3678                                 "2897 The mboxq allocation failed\n");
3679                 return;
3680         }
3681         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3682         if (!mp) {
3683                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3684                                 "2898 The lpfc_dmabuf allocation failed\n");
3685                 goto out_free_pmb;
3686         }
3687         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3688         if (!mp->virt) {
3689                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3690                                 "2899 The mbuf allocation failed\n");
3691                 goto out_free_dmabuf;
3692         }
3693
3694         /* Cleanup any outstanding ELS commands */
3695         lpfc_els_flush_all_cmd(phba);
3696
3697         /* Block ELS IOCBs until we have done process link event */
3698         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3699
3700         /* Update link event statistics */
3701         phba->sli.slistat.link_event++;
3702
3703         /* Create lpfc_handle_latt mailbox command from link ACQE */
3704         lpfc_read_topology(phba, pmb, mp);
3705         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3706         pmb->vport = phba->pport;
3707
3708         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3709         if (rc == MBX_NOT_FINISHED)
3710                 goto out_free_dmabuf;
3711         return;
3712
3713 out_free_dmabuf:
3714         kfree(mp);
3715 out_free_pmb:
3716         mempool_free(pmb, phba->mbox_mem_pool);
3717 }
3718
3719 /**
3720  * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3721  * @phba: pointer to lpfc hba data structure.
3722  * @acqe_fc: pointer to the async SLI completion queue entry.
3723  *
3724  * This routine is to handle the SLI4 asynchronous SLI events.
3725  **/
3726 static void
3727 lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3728 {
3729         char port_name;
3730         char message[80];
3731         uint8_t status;
3732         struct lpfc_acqe_misconfigured_event *misconfigured;
3733
3734         /* special case misconfigured event as it contains data for all ports */
3735         if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3736                  LPFC_SLI_INTF_IF_TYPE_2) ||
3737                 (bf_get(lpfc_trailer_type, acqe_sli) !=
3738                         LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3739                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3740                                 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3741                                 "x%08x SLI Event Type:%d\n",
3742                                 acqe_sli->event_data1, acqe_sli->event_data2,
3743                                 bf_get(lpfc_trailer_type, acqe_sli));
3744                 return;
3745         }
3746
3747         port_name = phba->Port[0];
3748         if (port_name == 0x00)
3749                 port_name = '?'; /* get port name is empty */
3750
3751         misconfigured = (struct lpfc_acqe_misconfigured_event *)
3752                                         &acqe_sli->event_data1;
3753
3754         /* fetch the status for this port */
3755         switch (phba->sli4_hba.lnk_info.lnk_no) {
3756         case LPFC_LINK_NUMBER_0:
3757                 status = bf_get(lpfc_sli_misconfigured_port0,
3758                                         &misconfigured->theEvent);
3759                 break;
3760         case LPFC_LINK_NUMBER_1:
3761                 status = bf_get(lpfc_sli_misconfigured_port1,
3762                                         &misconfigured->theEvent);
3763                 break;
3764         case LPFC_LINK_NUMBER_2:
3765                 status = bf_get(lpfc_sli_misconfigured_port2,
3766                                         &misconfigured->theEvent);
3767                 break;
3768         case LPFC_LINK_NUMBER_3:
3769                 status = bf_get(lpfc_sli_misconfigured_port3,
3770                                         &misconfigured->theEvent);
3771                 break;
3772         default:
3773                 status = ~LPFC_SLI_EVENT_STATUS_VALID;
3774                 break;
3775         }
3776
3777         switch (status) {
3778         case LPFC_SLI_EVENT_STATUS_VALID:
3779                 return; /* no message if the sfp is okay */
3780         case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
3781                 sprintf(message, "Not installed");
3782                 break;
3783         case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3784                 sprintf(message,
3785                         "Optics of two types installed");
3786                 break;
3787         case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
3788                 sprintf(message, "Incompatible optics");
3789                 break;
3790         default:
3791                 /* firmware is reporting a status we don't know about */
3792                 sprintf(message, "Unknown event status x%02x", status);
3793                 break;
3794         }
3795
3796         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3797                         "3176 Misconfigured Physical Port - "
3798                         "Port Name %c %s\n", port_name, message);
3799 }
3800
3801 /**
3802  * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3803  * @vport: pointer to vport data structure.
3804  *
3805  * This routine is to perform Clear Virtual Link (CVL) on a vport in
3806  * response to a CVL event.
3807  *
3808  * Return the pointer to the ndlp with the vport if successful, otherwise
3809  * return NULL.
3810  **/
3811 static struct lpfc_nodelist *
3812 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3813 {
3814         struct lpfc_nodelist *ndlp;
3815         struct Scsi_Host *shost;
3816         struct lpfc_hba *phba;
3817
3818         if (!vport)
3819                 return NULL;
3820         phba = vport->phba;
3821         if (!phba)
3822                 return NULL;
3823         ndlp = lpfc_findnode_did(vport, Fabric_DID);
3824         if (!ndlp) {
3825                 /* Cannot find existing Fabric ndlp, so allocate a new one */
3826                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3827                 if (!ndlp)
3828                         return 0;
3829                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3830                 /* Set the node type */
3831                 ndlp->nlp_type |= NLP_FABRIC;
3832                 /* Put ndlp onto node list */
3833                 lpfc_enqueue_node(vport, ndlp);
3834         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3835                 /* re-setup ndlp without removing from node list */
3836                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3837                 if (!ndlp)
3838                         return 0;
3839         }
3840         if ((phba->pport->port_state < LPFC_FLOGI) &&
3841                 (phba->pport->port_state != LPFC_VPORT_FAILED))
3842                 return NULL;
3843         /* If virtual link is not yet instantiated ignore CVL */
3844         if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3845                 && (vport->port_state != LPFC_VPORT_FAILED))
3846                 return NULL;
3847         shost = lpfc_shost_from_vport(vport);
3848         if (!shost)
3849                 return NULL;
3850         lpfc_linkdown_port(vport);
3851         lpfc_cleanup_pending_mbox(vport);
3852         spin_lock_irq(shost->host_lock);
3853         vport->fc_flag |= FC_VPORT_CVL_RCVD;
3854         spin_unlock_irq(shost->host_lock);
3855
3856         return ndlp;
3857 }
3858
3859 /**
3860  * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3861  * @vport: pointer to lpfc hba data structure.
3862  *
3863  * This routine is to perform Clear Virtual Link (CVL) on all vports in
3864  * response to a FCF dead event.
3865  **/
3866 static void
3867 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3868 {
3869         struct lpfc_vport **vports;
3870         int i;
3871
3872         vports = lpfc_create_vport_work_array(phba);
3873         if (vports)
3874                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3875                         lpfc_sli4_perform_vport_cvl(vports[i]);
3876         lpfc_destroy_vport_work_array(phba, vports);
3877 }
3878
3879 /**
3880  * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
3881  * @phba: pointer to lpfc hba data structure.
3882  * @acqe_link: pointer to the async fcoe completion queue entry.
3883  *
3884  * This routine is to handle the SLI4 asynchronous fcoe event.
3885  **/
3886 static void
3887 lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
3888                         struct lpfc_acqe_fip *acqe_fip)
3889 {
3890         uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
3891         int rc;
3892         struct lpfc_vport *vport;
3893         struct lpfc_nodelist *ndlp;
3894         struct Scsi_Host  *shost;
3895         int active_vlink_present;
3896         struct lpfc_vport **vports;
3897         int i;
3898
3899         phba->fc_eventTag = acqe_fip->event_tag;
3900         phba->fcoe_eventtag = acqe_fip->event_tag;
3901         switch (event_type) {
3902         case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3903         case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3904                 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
3905                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3906                                         LOG_DISCOVERY,
3907                                         "2546 New FCF event, evt_tag:x%x, "
3908                                         "index:x%x\n",
3909                                         acqe_fip->event_tag,
3910                                         acqe_fip->index);
3911                 else
3912                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3913                                         LOG_DISCOVERY,
3914                                         "2788 FCF param modified event, "
3915                                         "evt_tag:x%x, index:x%x\n",
3916                                         acqe_fip->event_tag,
3917                                         acqe_fip->index);
3918                 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3919                         /*
3920                          * During period of FCF discovery, read the FCF
3921                          * table record indexed by the event to update
3922                          * FCF roundrobin failover eligible FCF bmask.
3923                          */
3924                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3925                                         LOG_DISCOVERY,
3926                                         "2779 Read FCF (x%x) for updating "
3927                                         "roundrobin FCF failover bmask\n",
3928                                         acqe_fip->index);
3929                         rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
3930                 }
3931
3932                 /* If the FCF discovery is in progress, do nothing. */
3933                 spin_lock_irq(&phba->hbalock);
3934                 if (phba->hba_flag & FCF_TS_INPROG) {
3935                         spin_unlock_irq(&phba->hbalock);
3936                         break;
3937                 }
3938                 /* If fast FCF failover rescan event is pending, do nothing */
3939                 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3940                         spin_unlock_irq(&phba->hbalock);
3941                         break;
3942                 }
3943
3944                 /* If the FCF has been in discovered state, do nothing. */
3945                 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3946                         spin_unlock_irq(&phba->hbalock);
3947                         break;
3948                 }
3949                 spin_unlock_irq(&phba->hbalock);
3950
3951                 /* Otherwise, scan the entire FCF table and re-discover SAN */
3952                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3953                                 "2770 Start FCF table scan per async FCF "
3954                                 "event, evt_tag:x%x, index:x%x\n",
3955                                 acqe_fip->event_tag, acqe_fip->index);
3956                 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3957                                                      LPFC_FCOE_FCF_GET_FIRST);
3958                 if (rc)
3959                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3960                                         "2547 Issue FCF scan read FCF mailbox "
3961                                         "command failed (x%x)\n", rc);
3962                 break;
3963
3964         case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
3965                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3966                         "2548 FCF Table full count 0x%x tag 0x%x\n",
3967                         bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3968                         acqe_fip->event_tag);
3969                 break;
3970
3971         case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
3972                 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
3973                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3974                         "2549 FCF (x%x) disconnected from network, "
3975                         "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
3976                 /*
3977                  * If we are in the middle of FCF failover process, clear
3978                  * the corresponding FCF bit in the roundrobin bitmap.
3979                  */
3980                 spin_lock_irq(&phba->hbalock);
3981                 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3982                         spin_unlock_irq(&phba->hbalock);
3983                         /* Update FLOGI FCF failover eligible FCF bmask */
3984                         lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
3985                         break;
3986                 }
3987                 spin_unlock_irq(&phba->hbalock);
3988
3989                 /* If the event is not for currently used fcf do nothing */
3990                 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
3991                         break;
3992
3993                 /*
3994                  * Otherwise, request the port to rediscover the entire FCF
3995                  * table for a fast recovery from case that the current FCF
3996                  * is no longer valid as we are not in the middle of FCF
3997                  * failover process already.
3998                  */
3999                 spin_lock_irq(&phba->hbalock);
4000                 /* Mark the fast failover process in progress */
4001                 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4002                 spin_unlock_irq(&phba->hbalock);
4003
4004                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4005                                 "2771 Start FCF fast failover process due to "
4006                                 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4007                                 "\n", acqe_fip->event_tag, acqe_fip->index);
4008                 rc = lpfc_sli4_redisc_fcf_table(phba);
4009                 if (rc) {
4010                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4011                                         LOG_DISCOVERY,
4012                                         "2772 Issue FCF rediscover mabilbox "
4013                                         "command failed, fail through to FCF "
4014                                         "dead event\n");
4015                         spin_lock_irq(&phba->hbalock);
4016                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4017                         spin_unlock_irq(&phba->hbalock);
4018                         /*
4019                          * Last resort will fail over by treating this
4020                          * as a link down to FCF registration.
4021                          */
4022                         lpfc_sli4_fcf_dead_failthrough(phba);
4023                 } else {
4024                         /* Reset FCF roundrobin bmask for new discovery */
4025                         lpfc_sli4_clear_fcf_rr_bmask(phba);
4026                         /*
4027                          * Handling fast FCF failover to a DEAD FCF event is
4028                          * considered equalivant to receiving CVL to all vports.
4029                          */
4030                         lpfc_sli4_perform_all_vport_cvl(phba);
4031                 }
4032                 break;
4033         case LPFC_FIP_EVENT_TYPE_CVL:
4034                 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
4035                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4036                         "2718 Clear Virtual Link Received for VPI 0x%x"
4037                         " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
4038
4039                 vport = lpfc_find_vport_by_vpid(phba,
4040                                                 acqe_fip->index);
4041                 ndlp = lpfc_sli4_perform_vport_cvl(vport);
4042                 if (!ndlp)
4043                         break;
4044                 active_vlink_present = 0;
4045
4046                 vports = lpfc_create_vport_work_array(phba);
4047                 if (vports) {
4048                         for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4049                                         i++) {
4050                                 if ((!(vports[i]->fc_flag &
4051                                         FC_VPORT_CVL_RCVD)) &&
4052                                         (vports[i]->port_state > LPFC_FDISC)) {
4053                                         active_vlink_present = 1;
4054                                         break;
4055                                 }
4056                         }
4057                         lpfc_destroy_vport_work_array(phba, vports);
4058                 }
4059
4060                 if (active_vlink_present) {
4061                         /*
4062                          * If there are other active VLinks present,
4063                          * re-instantiate the Vlink using FDISC.
4064                          */
4065                         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
4066                         shost = lpfc_shost_from_vport(vport);
4067                         spin_lock_irq(shost->host_lock);
4068                         ndlp->nlp_flag |= NLP_DELAY_TMO;
4069                         spin_unlock_irq(shost->host_lock);
4070                         ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4071                         vport->port_state = LPFC_FDISC;
4072                 } else {
4073                         /*
4074                          * Otherwise, we request port to rediscover
4075                          * the entire FCF table for a fast recovery
4076                          * from possible case that the current FCF
4077                          * is no longer valid if we are not already
4078                          * in the FCF failover process.
4079                          */
4080                         spin_lock_irq(&phba->hbalock);
4081                         if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4082                                 spin_unlock_irq(&phba->hbalock);
4083                                 break;
4084                         }
4085                         /* Mark the fast failover process in progress */
4086                         phba->fcf.fcf_flag |= FCF_ACVL_DISC;
4087                         spin_unlock_irq(&phba->hbalock);
4088                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4089                                         LOG_DISCOVERY,
4090                                         "2773 Start FCF failover per CVL, "
4091                                         "evt_tag:x%x\n", acqe_fip->event_tag);
4092                         rc = lpfc_sli4_redisc_fcf_table(phba);
4093                         if (rc) {
4094                                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4095                                                 LOG_DISCOVERY,
4096                                                 "2774 Issue FCF rediscover "
4097                                                 "mabilbox command failed, "
4098                                                 "through to CVL event\n");
4099                                 spin_lock_irq(&phba->hbalock);
4100                                 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
4101                                 spin_unlock_irq(&phba->hbalock);
4102                                 /*
4103                                  * Last resort will be re-try on the
4104                                  * the current registered FCF entry.
4105                                  */
4106                                 lpfc_retry_pport_discovery(phba);
4107                         } else
4108                                 /*
4109                                  * Reset FCF roundrobin bmask for new
4110                                  * discovery.
4111                                  */
4112                                 lpfc_sli4_clear_fcf_rr_bmask(phba);
4113                 }
4114                 break;
4115         default:
4116                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4117                         "0288 Unknown FCoE event type 0x%x event tag "
4118                         "0x%x\n", event_type, acqe_fip->event_tag);
4119                 break;
4120         }
4121 }
4122
4123 /**
4124  * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4125  * @phba: pointer to lpfc hba data structure.
4126  * @acqe_link: pointer to the async dcbx completion queue entry.
4127  *
4128  * This routine is to handle the SLI4 asynchronous dcbx event.
4129  **/
4130 static void
4131 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4132                          struct lpfc_acqe_dcbx *acqe_dcbx)
4133 {
4134         phba->fc_eventTag = acqe_dcbx->event_tag;
4135         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4136                         "0290 The SLI4 DCBX asynchronous event is not "
4137                         "handled yet\n");
4138 }
4139
4140 /**
4141  * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4142  * @phba: pointer to lpfc hba data structure.
4143  * @acqe_link: pointer to the async grp5 completion queue entry.
4144  *
4145  * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4146  * is an asynchronous notified of a logical link speed change.  The Port
4147  * reports the logical link speed in units of 10Mbps.
4148  **/
4149 static void
4150 lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4151                          struct lpfc_acqe_grp5 *acqe_grp5)
4152 {
4153         uint16_t prev_ll_spd;
4154
4155         phba->fc_eventTag = acqe_grp5->event_tag;
4156         phba->fcoe_eventtag = acqe_grp5->event_tag;
4157         prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4158         phba->sli4_hba.link_state.logical_speed =
4159                 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
4160         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4161                         "2789 GRP5 Async Event: Updating logical link speed "
4162                         "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
4163                         (phba->sli4_hba.link_state.logical_speed*10));
4164 }
4165
4166 /**
4167  * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4168  * @phba: pointer to lpfc hba data structure.
4169  *
4170  * This routine is invoked by the worker thread to process all the pending
4171  * SLI4 asynchronous events.
4172  **/
4173 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4174 {
4175         struct lpfc_cq_event *cq_event;
4176
4177         /* First, declare the async event has been handled */
4178         spin_lock_irq(&phba->hbalock);
4179         phba->hba_flag &= ~ASYNC_EVENT;
4180         spin_unlock_irq(&phba->hbalock);
4181         /* Now, handle all the async events */
4182         while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4183                 /* Get the first event from the head of the event queue */
4184                 spin_lock_irq(&phba->hbalock);
4185                 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4186                                  cq_event, struct lpfc_cq_event, list);
4187                 spin_unlock_irq(&phba->hbalock);
4188                 /* Process the asynchronous event */
4189                 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4190                 case LPFC_TRAILER_CODE_LINK:
4191                         lpfc_sli4_async_link_evt(phba,
4192                                                  &cq_event->cqe.acqe_link);
4193                         break;
4194                 case LPFC_TRAILER_CODE_FCOE:
4195                         lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
4196                         break;
4197                 case LPFC_TRAILER_CODE_DCBX:
4198                         lpfc_sli4_async_dcbx_evt(phba,
4199                                                  &cq_event->cqe.acqe_dcbx);
4200                         break;
4201                 case LPFC_TRAILER_CODE_GRP5:
4202                         lpfc_sli4_async_grp5_evt(phba,
4203                                                  &cq_event->cqe.acqe_grp5);
4204                         break;
4205                 case LPFC_TRAILER_CODE_FC:
4206                         lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4207                         break;
4208                 case LPFC_TRAILER_CODE_SLI:
4209                         lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4210                         break;
4211                 default:
4212                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4213                                         "1804 Invalid asynchrous event code: "
4214                                         "x%x\n", bf_get(lpfc_trailer_code,
4215                                         &cq_event->cqe.mcqe_cmpl));
4216                         break;
4217                 }
4218                 /* Free the completion event processed to the free pool */
4219                 lpfc_sli4_cq_event_release(phba, cq_event);
4220         }
4221 }
4222
4223 /**
4224  * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4225  * @phba: pointer to lpfc hba data structure.
4226  *
4227  * This routine is invoked by the worker thread to process FCF table
4228  * rediscovery pending completion event.
4229  **/
4230 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4231 {
4232         int rc;
4233
4234         spin_lock_irq(&phba->hbalock);
4235         /* Clear FCF rediscovery timeout event */
4236         phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4237         /* Clear driver fast failover FCF record flag */
4238         phba->fcf.failover_rec.flag = 0;
4239         /* Set state for FCF fast failover */
4240         phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4241         spin_unlock_irq(&phba->hbalock);
4242
4243         /* Scan FCF table from the first entry to re-discover SAN */
4244         lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4245                         "2777 Start post-quiescent FCF table scan\n");
4246         rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
4247         if (rc)
4248                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4249                                 "2747 Issue FCF scan read FCF mailbox "
4250                                 "command failed 0x%x\n", rc);
4251 }
4252
4253 /**
4254  * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4255  * @phba: pointer to lpfc hba data structure.
4256  * @dev_grp: The HBA PCI-Device group number.
4257  *
4258  * This routine is invoked to set up the per HBA PCI-Device group function
4259  * API jump table entries.
4260  *
4261  * Return: 0 if success, otherwise -ENODEV
4262  **/
4263 int
4264 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4265 {
4266         int rc;
4267
4268         /* Set up lpfc PCI-device group */
4269         phba->pci_dev_grp = dev_grp;
4270
4271         /* The LPFC_PCI_DEV_OC uses SLI4 */
4272         if (dev_grp == LPFC_PCI_DEV_OC)
4273                 phba->sli_rev = LPFC_SLI_REV4;
4274
4275         /* Set up device INIT API function jump table */
4276         rc = lpfc_init_api_table_setup(phba, dev_grp);
4277         if (rc)
4278                 return -ENODEV;
4279         /* Set up SCSI API function jump table */
4280         rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4281         if (rc)
4282                 return -ENODEV;
4283         /* Set up SLI API function jump table */
4284         rc = lpfc_sli_api_table_setup(phba, dev_grp);
4285         if (rc)
4286                 return -ENODEV;
4287         /* Set up MBOX API function jump table */
4288         rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4289         if (rc)
4290                 return -ENODEV;
4291
4292         return 0;
4293 }
4294
4295 /**
4296  * lpfc_log_intr_mode - Log the active interrupt mode
4297  * @phba: pointer to lpfc hba data structure.
4298  * @intr_mode: active interrupt mode adopted.
4299  *
4300  * This routine it invoked to log the currently used active interrupt mode
4301  * to the device.
4302  **/
4303 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
4304 {
4305         switch (intr_mode) {
4306         case 0:
4307                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4308                                 "0470 Enable INTx interrupt mode.\n");
4309                 break;
4310         case 1:
4311                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4312                                 "0481 Enabled MSI interrupt mode.\n");
4313                 break;
4314         case 2:
4315                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4316                                 "0480 Enabled MSI-X interrupt mode.\n");
4317                 break;
4318         default:
4319                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4320                                 "0482 Illegal interrupt mode.\n");
4321                 break;
4322         }
4323         return;
4324 }
4325
4326 /**
4327  * lpfc_enable_pci_dev - Enable a generic PCI device.
4328  * @phba: pointer to lpfc hba data structure.
4329  *
4330  * This routine is invoked to enable the PCI device that is common to all
4331  * PCI devices.
4332  *
4333  * Return codes
4334  *      0 - successful
4335  *      other values - error
4336  **/
4337 static int
4338 lpfc_enable_pci_dev(struct lpfc_hba *phba)
4339 {
4340         struct pci_dev *pdev;
4341         int bars = 0;
4342
4343         /* Obtain PCI device reference */
4344         if (!phba->pcidev)
4345                 goto out_error;
4346         else
4347                 pdev = phba->pcidev;
4348         /* Select PCI BARs */
4349         bars = pci_select_bars(pdev, IORESOURCE_MEM);
4350         /* Enable PCI device */
4351         if (pci_enable_device_mem(pdev))
4352                 goto out_error;
4353         /* Request PCI resource for the device */
4354         if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4355                 goto out_disable_device;
4356         /* Set up device as PCI master and save state for EEH */
4357         pci_set_master(pdev);
4358         pci_try_set_mwi(pdev);
4359         pci_save_state(pdev);
4360
4361         /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4362         if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4363                 pdev->needs_freset = 1;
4364
4365         return 0;
4366
4367 out_disable_device:
4368         pci_disable_device(pdev);
4369 out_error:
4370         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4371                         "1401 Failed to enable pci device, bars:x%x\n", bars);
4372         return -ENODEV;
4373 }
4374
4375 /**
4376  * lpfc_disable_pci_dev - Disable a generic PCI device.
4377  * @phba: pointer to lpfc hba data structure.
4378  *
4379  * This routine is invoked to disable the PCI device that is common to all
4380  * PCI devices.
4381  **/
4382 static void
4383 lpfc_disable_pci_dev(struct lpfc_hba *phba)
4384 {
4385         struct pci_dev *pdev;
4386         int bars;
4387
4388         /* Obtain PCI device reference */
4389         if (!phba->pcidev)
4390                 return;
4391         else
4392                 pdev = phba->pcidev;
4393         /* Select PCI BARs */
4394         bars = pci_select_bars(pdev, IORESOURCE_MEM);
4395         /* Release PCI resource and disable PCI device */
4396         pci_release_selected_regions(pdev, bars);
4397         pci_disable_device(pdev);
4398         /* Null out PCI private reference to driver */
4399         pci_set_drvdata(pdev, NULL);
4400
4401         return;
4402 }
4403
4404 /**
4405  * lpfc_reset_hba - Reset a hba
4406  * @phba: pointer to lpfc hba data structure.
4407  *
4408  * This routine is invoked to reset a hba device. It brings the HBA
4409  * offline, performs a board restart, and then brings the board back
4410  * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4411  * on outstanding mailbox commands.
4412  **/
4413 void
4414 lpfc_reset_hba(struct lpfc_hba *phba)
4415 {
4416         /* If resets are disabled then set error state and return. */
4417         if (!phba->cfg_enable_hba_reset) {
4418                 phba->link_state = LPFC_HBA_ERROR;
4419                 return;
4420         }
4421         lpfc_offline_prep(phba, LPFC_MBX_WAIT);
4422         lpfc_offline(phba);
4423         lpfc_sli_brdrestart(phba);
4424         lpfc_online(phba);
4425         lpfc_unblock_mgmt_io(phba);
4426 }
4427
4428 /**
4429  * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4430  * @phba: pointer to lpfc hba data structure.
4431  *
4432  * This function enables the PCI SR-IOV virtual functions to a physical
4433  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4434  * enable the number of virtual functions to the physical function. As
4435  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4436  * API call does not considered as an error condition for most of the device.
4437  **/
4438 uint16_t
4439 lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4440 {
4441         struct pci_dev *pdev = phba->pcidev;
4442         uint16_t nr_virtfn;
4443         int pos;
4444
4445         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4446         if (pos == 0)
4447                 return 0;
4448
4449         pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4450         return nr_virtfn;
4451 }
4452
4453 /**
4454  * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4455  * @phba: pointer to lpfc hba data structure.
4456  * @nr_vfn: number of virtual functions to be enabled.
4457  *
4458  * This function enables the PCI SR-IOV virtual functions to a physical
4459  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4460  * enable the number of virtual functions to the physical function. As
4461  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4462  * API call does not considered as an error condition for most of the device.
4463  **/
4464 int
4465 lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4466 {
4467         struct pci_dev *pdev = phba->pcidev;
4468         uint16_t max_nr_vfn;
4469         int rc;
4470
4471         max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4472         if (nr_vfn > max_nr_vfn) {
4473                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4474                                 "3057 Requested vfs (%d) greater than "
4475                                 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4476                 return -EINVAL;
4477         }
4478
4479         rc = pci_enable_sriov(pdev, nr_vfn);
4480         if (rc) {
4481                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4482                                 "2806 Failed to enable sriov on this device "
4483                                 "with vfn number nr_vf:%d, rc:%d\n",
4484                                 nr_vfn, rc);
4485         } else
4486                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4487                                 "2807 Successful enable sriov on this device "
4488                                 "with vfn number nr_vf:%d\n", nr_vfn);
4489         return rc;
4490 }
4491
4492 /**
4493  * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4494  * @phba: pointer to lpfc hba data structure.
4495  *
4496  * This routine is invoked to set up the driver internal resources specific to
4497  * support the SLI-3 HBA device it attached to.
4498  *
4499  * Return codes
4500  *      0 - successful
4501  *      other values - error
4502  **/
4503 static int
4504 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4505 {
4506         struct lpfc_sli *psli;
4507         int rc;
4508
4509         /*
4510          * Initialize timers used by driver
4511          */
4512
4513         /* Heartbeat timer */
4514         init_timer(&phba->hb_tmofunc);
4515         phba->hb_tmofunc.function = lpfc_hb_timeout;
4516         phba->hb_tmofunc.data = (unsigned long)phba;
4517
4518         psli = &phba->sli;
4519         /* MBOX heartbeat timer */
4520         init_timer(&psli->mbox_tmo);
4521         psli->mbox_tmo.function = lpfc_mbox_timeout;
4522         psli->mbox_tmo.data = (unsigned long) phba;
4523         /* FCP polling mode timer */
4524         init_timer(&phba->fcp_poll_timer);
4525         phba->fcp_poll_timer.function = lpfc_poll_timeout;
4526         phba->fcp_poll_timer.data = (unsigned long) phba;
4527         /* Fabric block timer */
4528         init_timer(&phba->fabric_block_timer);
4529         phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4530         phba->fabric_block_timer.data = (unsigned long) phba;
4531         /* EA polling mode timer */
4532         init_timer(&phba->eratt_poll);
4533         phba->eratt_poll.function = lpfc_poll_eratt;
4534         phba->eratt_poll.data = (unsigned long) phba;
4535
4536         /* Host attention work mask setup */
4537         phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4538         phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4539
4540         /* Get all the module params for configuring this host */
4541         lpfc_get_cfgparam(phba);
4542         if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4543                 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4544                 /* check for menlo minimum sg count */
4545                 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4546                         phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4547         }
4548
4549         /*
4550          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4551          * used to create the sg_dma_buf_pool must be dynamically calculated.
4552          * 2 segments are added since the IOCB needs a command and response bde.
4553          */
4554         phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4555                 sizeof(struct fcp_rsp) +
4556                         ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4557
4558         if (phba->cfg_enable_bg) {
4559                 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4560                 phba->cfg_sg_dma_buf_size +=
4561                         phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4562         }
4563
4564         /* Also reinitialize the host templates with new values. */
4565         lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4566         lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4567
4568         phba->max_vpi = LPFC_MAX_VPI;
4569         /* This will be set to correct value after config_port mbox */
4570         phba->max_vports = 0;
4571
4572         /*
4573          * Initialize the SLI Layer to run with lpfc HBAs.
4574          */
4575         lpfc_sli_setup(phba);
4576         lpfc_sli_queue_setup(phba);
4577
4578         /* Allocate device driver memory */
4579         if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4580                 return -ENOMEM;
4581
4582         /*
4583          * Enable sr-iov virtual functions if supported and configured
4584          * through the module parameter.
4585          */
4586         if (phba->cfg_sriov_nr_virtfn > 0) {
4587                 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4588                                                  phba->cfg_sriov_nr_virtfn);
4589                 if (rc) {
4590                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4591                                         "2808 Requested number of SR-IOV "
4592                                         "virtual functions (%d) is not "
4593                                         "supported\n",
4594                                         phba->cfg_sriov_nr_virtfn);
4595                         phba->cfg_sriov_nr_virtfn = 0;
4596                 }
4597         }
4598
4599         return 0;
4600 }
4601
4602 /**
4603  * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4604  * @phba: pointer to lpfc hba data structure.
4605  *
4606  * This routine is invoked to unset the driver internal resources set up
4607  * specific for supporting the SLI-3 HBA device it attached to.
4608  **/
4609 static void
4610 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4611 {
4612         /* Free device driver memory allocated */
4613         lpfc_mem_free_all(phba);
4614
4615         return;
4616 }
4617
4618 /**
4619  * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4620  * @phba: pointer to lpfc hba data structure.
4621  *
4622  * This routine is invoked to set up the driver internal resources specific to
4623  * support the SLI-4 HBA device it attached to.
4624  *
4625  * Return codes
4626  *      0 - successful
4627  *      other values - error
4628  **/
4629 static int
4630 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4631 {
4632         struct lpfc_sli *psli;
4633         LPFC_MBOXQ_t *mboxq;
4634         int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4635         uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4636         struct lpfc_mqe *mqe;
4637         int longs, sli_family;
4638         int sges_per_segment;
4639
4640         /* Before proceed, wait for POST done and device ready */
4641         rc = lpfc_sli4_post_status_check(phba);
4642         if (rc)
4643                 return -ENODEV;
4644
4645         /*
4646          * Initialize timers used by driver
4647          */
4648
4649         /* Heartbeat timer */
4650         init_timer(&phba->hb_tmofunc);
4651         phba->hb_tmofunc.function = lpfc_hb_timeout;
4652         phba->hb_tmofunc.data = (unsigned long)phba;
4653         init_timer(&phba->rrq_tmr);
4654         phba->rrq_tmr.function = lpfc_rrq_timeout;
4655         phba->rrq_tmr.data = (unsigned long)phba;
4656
4657         psli = &phba->sli;
4658         /* MBOX heartbeat timer */
4659         init_timer(&psli->mbox_tmo);
4660         psli->mbox_tmo.function = lpfc_mbox_timeout;
4661         psli->mbox_tmo.data = (unsigned long) phba;
4662         /* Fabric block timer */
4663         init_timer(&phba->fabric_block_timer);
4664         phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4665         phba->fabric_block_timer.data = (unsigned long) phba;
4666         /* EA polling mode timer */
4667         init_timer(&phba->eratt_poll);
4668         phba->eratt_poll.function = lpfc_poll_eratt;
4669         phba->eratt_poll.data = (unsigned long) phba;
4670         /* FCF rediscover timer */
4671         init_timer(&phba->fcf.redisc_wait);
4672         phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4673         phba->fcf.redisc_wait.data = (unsigned long)phba;
4674
4675         /*
4676          * Control structure for handling external multi-buffer mailbox
4677          * command pass-through.
4678          */
4679         memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4680                 sizeof(struct lpfc_mbox_ext_buf_ctx));
4681         INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4682
4683         /*
4684          * We need to do a READ_CONFIG mailbox command here before
4685          * calling lpfc_get_cfgparam. For VFs this will report the
4686          * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4687          * All of the resources allocated
4688          * for this Port are tied to these values.
4689          */
4690         /* Get all the module params for configuring this host */
4691         lpfc_get_cfgparam(phba);
4692         phba->max_vpi = LPFC_MAX_VPI;
4693         /* This will be set to correct value after the read_config mbox */
4694         phba->max_vports = 0;
4695
4696         /* Program the default value of vlan_id and fc_map */
4697         phba->valid_vlan = 0;
4698         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4699         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4700         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4701
4702         /* With BlockGuard we can have multiple SGEs per Data Segemnt */
4703         sges_per_segment = 1;
4704         if (phba->cfg_enable_bg)
4705                 sges_per_segment = 2;
4706
4707         /*
4708          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4709          * used to create the sg_dma_buf_pool must be dynamically calculated.
4710          * 2 segments are added since the IOCB needs a command and response bde.
4711          * To insure that the scsi sgl does not cross a 4k page boundary only
4712          * sgl sizes of must be a power of 2.
4713          */
4714         buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4715                     (((phba->cfg_sg_seg_cnt * sges_per_segment) + 2) *
4716                     sizeof(struct sli4_sge)));
4717
4718         sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4719         max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4720         switch (sli_family) {
4721         case LPFC_SLI_INTF_FAMILY_BE2:
4722         case LPFC_SLI_INTF_FAMILY_BE3:
4723                 /* There is a single hint for BE - 2 pages per BPL. */
4724                 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4725                     LPFC_SLI_INTF_SLI_HINT1_1)
4726                         max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4727                 break;
4728         case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4729         case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4730         default:
4731                 break;
4732         }
4733
4734         for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4735              dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4736              dma_buf_size = dma_buf_size << 1)
4737                 ;
4738         if (dma_buf_size == max_buf_size)
4739                 phba->cfg_sg_seg_cnt = (dma_buf_size -
4740                         sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4741                         (2 * sizeof(struct sli4_sge))) /
4742                                 sizeof(struct sli4_sge);
4743         phba->cfg_sg_dma_buf_size = dma_buf_size;
4744
4745         /* Initialize buffer queue management fields */
4746         hbq_count = lpfc_sli_hbq_count();
4747         for (i = 0; i < hbq_count; ++i)
4748                 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4749         INIT_LIST_HEAD(&phba->rb_pend_list);
4750         phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4751         phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4752
4753         /*
4754          * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4755          */
4756         /* Initialize the Abort scsi buffer list used by driver */
4757         spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4758         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4759         /* This abort list used by worker thread */
4760         spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4761
4762         /*
4763          * Initialize driver internal slow-path work queues
4764          */
4765
4766         /* Driver internel slow-path CQ Event pool */
4767         INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4768         /* Response IOCB work queue list */
4769         INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
4770         /* Asynchronous event CQ Event work queue list */
4771         INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4772         /* Fast-path XRI aborted CQ Event work queue list */
4773         INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4774         /* Slow-path XRI aborted CQ Event work queue list */
4775         INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4776         /* Receive queue CQ Event work queue list */
4777         INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4778
4779         /* Initialize extent block lists. */
4780         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4781         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4782         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4783         INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4784
4785         /* Initialize the driver internal SLI layer lists. */
4786         lpfc_sli_setup(phba);
4787         lpfc_sli_queue_setup(phba);
4788
4789         /* Allocate device driver memory */
4790         rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4791         if (rc)
4792                 return -ENOMEM;
4793
4794         /* IF Type 2 ports get initialized now. */
4795         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4796             LPFC_SLI_INTF_IF_TYPE_2) {
4797                 rc = lpfc_pci_function_reset(phba);
4798                 if (unlikely(rc))
4799                         return -ENODEV;
4800         }
4801
4802         /* Create the bootstrap mailbox command */
4803         rc = lpfc_create_bootstrap_mbox(phba);
4804         if (unlikely(rc))
4805                 goto out_free_mem;
4806
4807         /* Set up the host's endian order with the device. */
4808         rc = lpfc_setup_endian_order(phba);
4809         if (unlikely(rc))
4810                 goto out_free_bsmbx;
4811
4812         /* Set up the hba's configuration parameters. */
4813         rc = lpfc_sli4_read_config(phba);
4814         if (unlikely(rc))
4815                 goto out_free_bsmbx;
4816
4817         /* IF Type 0 ports get initialized now. */
4818         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4819             LPFC_SLI_INTF_IF_TYPE_0) {
4820                 rc = lpfc_pci_function_reset(phba);
4821                 if (unlikely(rc))
4822                         goto out_free_bsmbx;
4823         }
4824
4825         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4826                                                        GFP_KERNEL);
4827         if (!mboxq) {
4828                 rc = -ENOMEM;
4829                 goto out_free_bsmbx;
4830         }
4831
4832         /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
4833         lpfc_supported_pages(mboxq);
4834         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4835         if (!rc) {
4836                 mqe = &mboxq->u.mqe;
4837                 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4838                        LPFC_MAX_SUPPORTED_PAGES);
4839                 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4840                         switch (pn_page[i]) {
4841                         case LPFC_SLI4_PARAMETERS:
4842                                 phba->sli4_hba.pc_sli4_params.supported = 1;
4843                                 break;
4844                         default:
4845                                 break;
4846                         }
4847                 }
4848                 /* Read the port's SLI4 Parameters capabilities if supported. */
4849                 if (phba->sli4_hba.pc_sli4_params.supported)
4850                         rc = lpfc_pc_sli4_params_get(phba, mboxq);
4851                 if (rc) {
4852                         mempool_free(mboxq, phba->mbox_mem_pool);
4853                         rc = -EIO;
4854                         goto out_free_bsmbx;
4855                 }
4856         }
4857         /*
4858          * Get sli4 parameters that override parameters from Port capabilities.
4859          * If this call fails, it isn't critical unless the SLI4 parameters come
4860          * back in conflict.
4861          */
4862         rc = lpfc_get_sli4_parameters(phba, mboxq);
4863         if (rc) {
4864                 if (phba->sli4_hba.extents_in_use &&
4865                     phba->sli4_hba.rpi_hdrs_in_use) {
4866                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4867                                 "2999 Unsupported SLI4 Parameters "
4868                                 "Extents and RPI headers enabled.\n");
4869                         goto out_free_bsmbx;
4870                 }
4871         }
4872         mempool_free(mboxq, phba->mbox_mem_pool);
4873         /* Verify all the SLI4 queues */
4874         rc = lpfc_sli4_queue_verify(phba);
4875         if (rc)
4876                 goto out_free_bsmbx;
4877
4878         /* Create driver internal CQE event pool */
4879         rc = lpfc_sli4_cq_event_pool_create(phba);
4880         if (rc)
4881                 goto out_free_bsmbx;
4882
4883         /* Initialize sgl lists per host */
4884         lpfc_init_sgl_list(phba);
4885
4886         /* Allocate and initialize active sgl array */
4887         rc = lpfc_init_active_sgl_array(phba);
4888         if (rc) {
4889                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4890                                 "1430 Failed to initialize sgl list.\n");
4891                 goto out_destroy_cq_event_pool;
4892         }
4893         rc = lpfc_sli4_init_rpi_hdrs(phba);
4894         if (rc) {
4895                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4896                                 "1432 Failed to initialize rpi headers.\n");
4897                 goto out_free_active_sgl;
4898         }
4899
4900         /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
4901         longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4902         phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4903                                          GFP_KERNEL);
4904         if (!phba->fcf.fcf_rr_bmask) {
4905                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4906                                 "2759 Failed allocate memory for FCF round "
4907                                 "robin failover bmask\n");
4908                 rc = -ENOMEM;
4909                 goto out_remove_rpi_hdrs;
4910         }
4911
4912         /*
4913          * The cfg_fcp_eq_count can be zero whenever there is exactly one
4914          * interrupt vector.  This is not an error
4915          */
4916         if (phba->cfg_fcp_eq_count) {
4917                 phba->sli4_hba.fcp_eq_hdl =
4918                                 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4919                                     phba->cfg_fcp_eq_count), GFP_KERNEL);
4920                 if (!phba->sli4_hba.fcp_eq_hdl) {
4921                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4922                                         "2572 Failed allocate memory for "
4923                                         "fast-path per-EQ handle array\n");
4924                         rc = -ENOMEM;
4925                         goto out_free_fcf_rr_bmask;
4926                 }
4927         }
4928
4929         phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4930                                       phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4931         if (!phba->sli4_hba.msix_entries) {
4932                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4933                                 "2573 Failed allocate memory for msi-x "
4934                                 "interrupt vector entries\n");
4935                 rc = -ENOMEM;
4936                 goto out_free_fcp_eq_hdl;
4937         }
4938
4939         /*
4940          * Enable sr-iov virtual functions if supported and configured
4941          * through the module parameter.
4942          */
4943         if (phba->cfg_sriov_nr_virtfn > 0) {
4944                 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4945                                                  phba->cfg_sriov_nr_virtfn);
4946                 if (rc) {
4947                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4948                                         "3020 Requested number of SR-IOV "
4949                                         "virtual functions (%d) is not "
4950                                         "supported\n",
4951                                         phba->cfg_sriov_nr_virtfn);
4952                         phba->cfg_sriov_nr_virtfn = 0;
4953                 }
4954         }
4955
4956         return 0;
4957
4958 out_free_fcp_eq_hdl:
4959         kfree(phba->sli4_hba.fcp_eq_hdl);
4960 out_free_fcf_rr_bmask:
4961         kfree(phba->fcf.fcf_rr_bmask);
4962 out_remove_rpi_hdrs:
4963         lpfc_sli4_remove_rpi_hdrs(phba);
4964 out_free_active_sgl:
4965         lpfc_free_active_sgl(phba);
4966 out_destroy_cq_event_pool:
4967         lpfc_sli4_cq_event_pool_destroy(phba);
4968 out_free_bsmbx:
4969         lpfc_destroy_bootstrap_mbox(phba);
4970 out_free_mem:
4971         lpfc_mem_free(phba);
4972         return rc;
4973 }
4974
4975 /**
4976  * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4977  * @phba: pointer to lpfc hba data structure.
4978  *
4979  * This routine is invoked to unset the driver internal resources set up
4980  * specific for supporting the SLI-4 HBA device it attached to.
4981  **/
4982 static void
4983 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4984 {
4985         struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4986
4987         /* Free memory allocated for msi-x interrupt vector entries */
4988         kfree(phba->sli4_hba.msix_entries);
4989
4990         /* Free memory allocated for fast-path work queue handles */
4991         kfree(phba->sli4_hba.fcp_eq_hdl);
4992
4993         /* Free the allocated rpi headers. */
4994         lpfc_sli4_remove_rpi_hdrs(phba);
4995         lpfc_sli4_remove_rpis(phba);
4996
4997         /* Free eligible FCF index bmask */
4998         kfree(phba->fcf.fcf_rr_bmask);
4999
5000         /* Free the ELS sgl list */
5001         lpfc_free_active_sgl(phba);
5002         lpfc_free_els_sgl_list(phba);
5003
5004         /* Free the completion queue EQ event pool */
5005         lpfc_sli4_cq_event_release_all(phba);
5006         lpfc_sli4_cq_event_pool_destroy(phba);
5007
5008         /* Release resource identifiers. */
5009         lpfc_sli4_dealloc_resource_identifiers(phba);
5010
5011         /* Free the bsmbx region. */
5012         lpfc_destroy_bootstrap_mbox(phba);
5013
5014         /* Free the SLI Layer memory with SLI4 HBAs */
5015         lpfc_mem_free_all(phba);
5016
5017         /* Free the current connect table */
5018         list_for_each_entry_safe(conn_entry, next_conn_entry,
5019                 &phba->fcf_conn_rec_list, list) {
5020                 list_del_init(&conn_entry->list);
5021                 kfree(conn_entry);
5022         }
5023
5024         return;
5025 }
5026
5027 /**
5028  * lpfc_init_api_table_setup - Set up init api function jump table
5029  * @phba: The hba struct for which this call is being executed.
5030  * @dev_grp: The HBA PCI-Device group number.
5031  *
5032  * This routine sets up the device INIT interface API function jump table
5033  * in @phba struct.
5034  *
5035  * Returns: 0 - success, -ENODEV - failure.
5036  **/
5037 int
5038 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5039 {
5040         phba->lpfc_hba_init_link = lpfc_hba_init_link;
5041         phba->lpfc_hba_down_link = lpfc_hba_down_link;
5042         phba->lpfc_selective_reset = lpfc_selective_reset;
5043         switch (dev_grp) {
5044         case LPFC_PCI_DEV_LP:
5045                 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5046                 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5047                 phba->lpfc_stop_port = lpfc_stop_port_s3;
5048                 break;
5049         case LPFC_PCI_DEV_OC:
5050                 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5051                 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5052                 phba->lpfc_stop_port = lpfc_stop_port_s4;
5053                 break;
5054         default:
5055                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5056                                 "1431 Invalid HBA PCI-device group: 0x%x\n",
5057                                 dev_grp);
5058                 return -ENODEV;
5059                 break;
5060         }
5061         return 0;
5062 }
5063
5064 /**
5065  * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5066  * @phba: pointer to lpfc hba data structure.
5067  *
5068  * This routine is invoked to set up the driver internal resources before the
5069  * device specific resource setup to support the HBA device it attached to.
5070  *
5071  * Return codes
5072  *      0 - successful
5073  *      other values - error
5074  **/
5075 static int
5076 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5077 {
5078         /*
5079          * Driver resources common to all SLI revisions
5080          */
5081         atomic_set(&phba->fast_event_count, 0);
5082         spin_lock_init(&phba->hbalock);
5083
5084         /* Initialize ndlp management spinlock */
5085         spin_lock_init(&phba->ndlp_lock);
5086
5087         INIT_LIST_HEAD(&phba->port_list);
5088         INIT_LIST_HEAD(&phba->work_list);
5089         init_waitqueue_head(&phba->wait_4_mlo_m_q);
5090
5091         /* Initialize the wait queue head for the kernel thread */
5092         init_waitqueue_head(&phba->work_waitq);
5093
5094         /* Initialize the scsi buffer list used by driver for scsi IO */
5095         spin_lock_init(&phba->scsi_buf_list_lock);
5096         INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
5097
5098         /* Initialize the fabric iocb list */
5099         INIT_LIST_HEAD(&phba->fabric_iocb_list);
5100
5101         /* Initialize list to save ELS buffers */
5102         INIT_LIST_HEAD(&phba->elsbuf);
5103
5104         /* Initialize FCF connection rec list */
5105         INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5106
5107         return 0;
5108 }
5109
5110 /**
5111  * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5112  * @phba: pointer to lpfc hba data structure.
5113  *
5114  * This routine is invoked to set up the driver internal resources after the
5115  * device specific resource setup to support the HBA device it attached to.
5116  *
5117  * Return codes
5118  *      0 - successful
5119  *      other values - error
5120  **/
5121 static int
5122 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5123 {
5124         int error;
5125
5126         /* Startup the kernel thread for this host adapter. */
5127         phba->worker_thread = kthread_run(lpfc_do_work, phba,
5128                                           "lpfc_worker_%d", phba->brd_no);
5129         if (IS_ERR(phba->worker_thread)) {
5130                 error = PTR_ERR(phba->worker_thread);
5131                 return error;
5132         }
5133
5134         return 0;
5135 }
5136
5137 /**
5138  * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5139  * @phba: pointer to lpfc hba data structure.
5140  *
5141  * This routine is invoked to unset the driver internal resources set up after
5142  * the device specific resource setup for supporting the HBA device it
5143  * attached to.
5144  **/
5145 static void
5146 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5147 {
5148         /* Stop kernel worker thread */
5149         kthread_stop(phba->worker_thread);
5150 }
5151
5152 /**
5153  * lpfc_free_iocb_list - Free iocb list.
5154  * @phba: pointer to lpfc hba data structure.
5155  *
5156  * This routine is invoked to free the driver's IOCB list and memory.
5157  **/
5158 static void
5159 lpfc_free_iocb_list(struct lpfc_hba *phba)
5160 {
5161         struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5162
5163         spin_lock_irq(&phba->hbalock);
5164         list_for_each_entry_safe(iocbq_entry, iocbq_next,
5165                                  &phba->lpfc_iocb_list, list) {
5166                 list_del(&iocbq_entry->list);
5167                 kfree(iocbq_entry);
5168                 phba->total_iocbq_bufs--;
5169         }
5170         spin_unlock_irq(&phba->hbalock);
5171
5172         return;
5173 }
5174
5175 /**
5176  * lpfc_init_iocb_list - Allocate and initialize iocb list.
5177  * @phba: pointer to lpfc hba data structure.
5178  *
5179  * This routine is invoked to allocate and initizlize the driver's IOCB
5180  * list and set up the IOCB tag array accordingly.
5181  *
5182  * Return codes
5183  *      0 - successful
5184  *      other values - error
5185  **/
5186 static int
5187 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5188 {
5189         struct lpfc_iocbq *iocbq_entry = NULL;
5190         uint16_t iotag;
5191         int i;
5192
5193         /* Initialize and populate the iocb list per host.  */
5194         INIT_LIST_HEAD(&phba->lpfc_iocb_list);
5195         for (i = 0; i < iocb_count; i++) {
5196                 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
5197                 if (iocbq_entry == NULL) {
5198                         printk(KERN_ERR "%s: only allocated %d iocbs of "
5199                                 "expected %d count. Unloading driver.\n",
5200                                 __func__, i, LPFC_IOCB_LIST_CNT);
5201                         goto out_free_iocbq;
5202                 }
5203
5204                 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5205                 if (iotag == 0) {
5206                         kfree(iocbq_entry);
5207                         printk(KERN_ERR "%s: failed to allocate IOTAG. "
5208                                 "Unloading driver.\n", __func__);
5209                         goto out_free_iocbq;
5210                 }
5211                 iocbq_entry->sli4_lxritag = NO_XRI;
5212                 iocbq_entry->sli4_xritag = NO_XRI;
5213
5214                 spin_lock_irq(&phba->hbalock);
5215                 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5216                 phba->total_iocbq_bufs++;
5217                 spin_unlock_irq(&phba->hbalock);
5218         }
5219
5220         return 0;
5221
5222 out_free_iocbq:
5223         lpfc_free_iocb_list(phba);
5224
5225         return -ENOMEM;
5226 }
5227
5228 /**
5229  * lpfc_free_sgl_list - Free a given sgl list.
5230  * @phba: pointer to lpfc hba data structure.
5231  * @sglq_list: pointer to the head of sgl list.
5232  *
5233  * This routine is invoked to free a give sgl list and memory.
5234  **/
5235 void
5236 lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
5237 {
5238         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
5239
5240         list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5241                 list_del(&sglq_entry->list);
5242                 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5243                 kfree(sglq_entry);
5244         }
5245 }
5246
5247 /**
5248  * lpfc_free_els_sgl_list - Free els sgl list.
5249  * @phba: pointer to lpfc hba data structure.
5250  *
5251  * This routine is invoked to free the driver's els sgl list and memory.
5252  **/
5253 static void
5254 lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5255 {
5256         LIST_HEAD(sglq_list);
5257
5258         /* Retrieve all els sgls from driver list */
5259         spin_lock_irq(&phba->hbalock);
5260         list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5261         spin_unlock_irq(&phba->hbalock);
5262
5263         /* Now free the sgl list */
5264         lpfc_free_sgl_list(phba, &sglq_list);
5265 }
5266
5267 /**
5268  * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5269  * @phba: pointer to lpfc hba data structure.
5270  *
5271  * This routine is invoked to allocate the driver's active sgl memory.
5272  * This array will hold the sglq_entry's for active IOs.
5273  **/
5274 static int
5275 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5276 {
5277         int size;
5278         size = sizeof(struct lpfc_sglq *);
5279         size *= phba->sli4_hba.max_cfg_param.max_xri;
5280
5281         phba->sli4_hba.lpfc_sglq_active_list =
5282                 kzalloc(size, GFP_KERNEL);
5283         if (!phba->sli4_hba.lpfc_sglq_active_list)
5284                 return -ENOMEM;
5285         return 0;
5286 }
5287
5288 /**
5289  * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5290  * @phba: pointer to lpfc hba data structure.
5291  *
5292  * This routine is invoked to walk through the array of active sglq entries
5293  * and free all of the resources.
5294  * This is just a place holder for now.
5295  **/
5296 static void
5297 lpfc_free_active_sgl(struct lpfc_hba *phba)
5298 {
5299         kfree(phba->sli4_hba.lpfc_sglq_active_list);
5300 }
5301
5302 /**
5303  * lpfc_init_sgl_list - Allocate and initialize sgl list.
5304  * @phba: pointer to lpfc hba data structure.
5305  *
5306  * This routine is invoked to allocate and initizlize the driver's sgl
5307  * list and set up the sgl xritag tag array accordingly.
5308  *
5309  **/
5310 static void
5311 lpfc_init_sgl_list(struct lpfc_hba *phba)
5312 {
5313         /* Initialize and populate the sglq list per host/VF. */
5314         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5315         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5316
5317         /* els xri-sgl book keeping */
5318         phba->sli4_hba.els_xri_cnt = 0;
5319
5320         /* scsi xri-buffer book keeping */
5321         phba->sli4_hba.scsi_xri_cnt = 0;
5322 }
5323
5324 /**
5325  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5326  * @phba: pointer to lpfc hba data structure.
5327  *
5328  * This routine is invoked to post rpi header templates to the
5329  * port for those SLI4 ports that do not support extents.  This routine
5330  * posts a PAGE_SIZE memory region to the port to hold up to
5331  * PAGE_SIZE modulo 64 rpi context headers.  This is an initialization routine
5332  * and should be called only when interrupts are disabled.
5333  *
5334  * Return codes
5335  *      0 - successful
5336  *      -ERROR - otherwise.
5337  **/
5338 int
5339 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5340 {
5341         int rc = 0;
5342         struct lpfc_rpi_hdr *rpi_hdr;
5343
5344         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
5345         if (!phba->sli4_hba.rpi_hdrs_in_use)
5346                 return rc;
5347         if (phba->sli4_hba.extents_in_use)
5348                 return -EIO;
5349
5350         rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5351         if (!rpi_hdr) {
5352                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5353                                 "0391 Error during rpi post operation\n");
5354                 lpfc_sli4_remove_rpis(phba);
5355                 rc = -ENODEV;
5356         }
5357
5358         return rc;
5359 }
5360
5361 /**
5362  * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5363  * @phba: pointer to lpfc hba data structure.
5364  *
5365  * This routine is invoked to allocate a single 4KB memory region to
5366  * support rpis and stores them in the phba.  This single region
5367  * provides support for up to 64 rpis.  The region is used globally
5368  * by the device.
5369  *
5370  * Returns:
5371  *   A valid rpi hdr on success.
5372  *   A NULL pointer on any failure.
5373  **/
5374 struct lpfc_rpi_hdr *
5375 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5376 {
5377         uint16_t rpi_limit, curr_rpi_range;
5378         struct lpfc_dmabuf *dmabuf;
5379         struct lpfc_rpi_hdr *rpi_hdr;
5380         uint32_t rpi_count;
5381
5382         /*
5383          * If the SLI4 port supports extents, posting the rpi header isn't
5384          * required.  Set the expected maximum count and let the actual value
5385          * get set when extents are fully allocated.
5386          */
5387         if (!phba->sli4_hba.rpi_hdrs_in_use)
5388                 return NULL;
5389         if (phba->sli4_hba.extents_in_use)
5390                 return NULL;
5391
5392         /* The limit on the logical index is just the max_rpi count. */
5393         rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
5394         phba->sli4_hba.max_cfg_param.max_rpi - 1;
5395
5396         spin_lock_irq(&phba->hbalock);
5397         /*
5398          * Establish the starting RPI in this header block.  The starting
5399          * rpi is normalized to a zero base because the physical rpi is
5400          * port based.
5401          */
5402         curr_rpi_range = phba->sli4_hba.next_rpi;
5403         spin_unlock_irq(&phba->hbalock);
5404
5405         /*
5406          * The port has a limited number of rpis. The increment here
5407          * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5408          * and to allow the full max_rpi range per port.
5409          */
5410         if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
5411                 rpi_count = rpi_limit - curr_rpi_range;
5412         else
5413                 rpi_count = LPFC_RPI_HDR_COUNT;
5414
5415         if (!rpi_count)
5416                 return NULL;
5417         /*
5418          * First allocate the protocol header region for the port.  The
5419          * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5420          */
5421         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5422         if (!dmabuf)
5423                 return NULL;
5424
5425         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5426                                           LPFC_HDR_TEMPLATE_SIZE,
5427                                           &dmabuf->phys,
5428                                           GFP_KERNEL);
5429         if (!dmabuf->virt) {
5430                 rpi_hdr = NULL;
5431                 goto err_free_dmabuf;
5432         }
5433
5434         memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5435         if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5436                 rpi_hdr = NULL;
5437                 goto err_free_coherent;
5438         }
5439
5440         /* Save the rpi header data for cleanup later. */
5441         rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5442         if (!rpi_hdr)
5443                 goto err_free_coherent;
5444
5445         rpi_hdr->dmabuf = dmabuf;
5446         rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5447         rpi_hdr->page_count = 1;
5448         spin_lock_irq(&phba->hbalock);
5449
5450         /* The rpi_hdr stores the logical index only. */
5451         rpi_hdr->start_rpi = curr_rpi_range;
5452         list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5453
5454         /*
5455          * The next_rpi stores the next logical module-64 rpi value used
5456          * to post physical rpis in subsequent rpi postings.
5457          */
5458         phba->sli4_hba.next_rpi += rpi_count;
5459         spin_unlock_irq(&phba->hbalock);
5460         return rpi_hdr;
5461
5462  err_free_coherent:
5463         dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5464                           dmabuf->virt, dmabuf->phys);
5465  err_free_dmabuf:
5466         kfree(dmabuf);
5467         return NULL;
5468 }
5469
5470 /**
5471  * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5472  * @phba: pointer to lpfc hba data structure.
5473  *
5474  * This routine is invoked to remove all memory resources allocated
5475  * to support rpis for SLI4 ports not supporting extents. This routine
5476  * presumes the caller has released all rpis consumed by fabric or port
5477  * logins and is prepared to have the header pages removed.
5478  **/
5479 void
5480 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5481 {
5482         struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5483
5484         if (!phba->sli4_hba.rpi_hdrs_in_use)
5485                 goto exit;
5486
5487         list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5488                                  &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5489                 list_del(&rpi_hdr->list);
5490                 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5491                                   rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5492                 kfree(rpi_hdr->dmabuf);
5493                 kfree(rpi_hdr);
5494         }
5495  exit:
5496         /* There are no rpis available to the port now. */
5497         phba->sli4_hba.next_rpi = 0;
5498 }
5499
5500 /**
5501  * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5502  * @pdev: pointer to pci device data structure.
5503  *
5504  * This routine is invoked to allocate the driver hba data structure for an
5505  * HBA device. If the allocation is successful, the phba reference to the
5506  * PCI device data structure is set.
5507  *
5508  * Return codes
5509  *      pointer to @phba - successful
5510  *      NULL - error
5511  **/
5512 static struct lpfc_hba *
5513 lpfc_hba_alloc(struct pci_dev *pdev)
5514 {
5515         struct lpfc_hba *phba;
5516
5517         /* Allocate memory for HBA structure */
5518         phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5519         if (!phba) {
5520                 dev_err(&pdev->dev, "failed to allocate hba struct\n");
5521                 return NULL;
5522         }
5523
5524         /* Set reference to PCI device in HBA structure */
5525         phba->pcidev = pdev;
5526
5527         /* Assign an unused board number */
5528         phba->brd_no = lpfc_get_instance();
5529         if (phba->brd_no < 0) {
5530                 kfree(phba);
5531                 return NULL;
5532         }
5533
5534         spin_lock_init(&phba->ct_ev_lock);
5535         INIT_LIST_HEAD(&phba->ct_ev_waiters);
5536
5537         return phba;
5538 }
5539
5540 /**
5541  * lpfc_hba_free - Free driver hba data structure with a device.
5542  * @phba: pointer to lpfc hba data structure.
5543  *
5544  * This routine is invoked to free the driver hba data structure with an
5545  * HBA device.
5546  **/
5547 static void
5548 lpfc_hba_free(struct lpfc_hba *phba)
5549 {
5550         /* Release the driver assigned board number */
5551         idr_remove(&lpfc_hba_index, phba->brd_no);
5552
5553         kfree(phba);
5554         return;
5555 }
5556
5557 /**
5558  * lpfc_create_shost - Create hba physical port with associated scsi host.
5559  * @phba: pointer to lpfc hba data structure.
5560  *
5561  * This routine is invoked to create HBA physical port and associate a SCSI
5562  * host with it.
5563  *
5564  * Return codes
5565  *      0 - successful
5566  *      other values - error
5567  **/
5568 static int
5569 lpfc_create_shost(struct lpfc_hba *phba)
5570 {
5571         struct lpfc_vport *vport;
5572         struct Scsi_Host  *shost;
5573
5574         /* Initialize HBA FC structure */
5575         phba->fc_edtov = FF_DEF_EDTOV;
5576         phba->fc_ratov = FF_DEF_RATOV;
5577         phba->fc_altov = FF_DEF_ALTOV;
5578         phba->fc_arbtov = FF_DEF_ARBTOV;
5579
5580         atomic_set(&phba->sdev_cnt, 0);
5581         vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
5582         if (!vport)
5583                 return -ENODEV;
5584
5585         shost = lpfc_shost_from_vport(vport);
5586         phba->pport = vport;
5587         lpfc_debugfs_initialize(vport);
5588         /* Put reference to SCSI host to driver's device private data */
5589         pci_set_drvdata(phba->pcidev, shost);
5590
5591         return 0;
5592 }
5593
5594 /**
5595  * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5596  * @phba: pointer to lpfc hba data structure.
5597  *
5598  * This routine is invoked to destroy HBA physical port and the associated
5599  * SCSI host.
5600  **/
5601 static void
5602 lpfc_destroy_shost(struct lpfc_hba *phba)
5603 {
5604         struct lpfc_vport *vport = phba->pport;
5605
5606         /* Destroy physical port that associated with the SCSI host */
5607         destroy_port(vport);
5608
5609         return;
5610 }
5611
5612 /**
5613  * lpfc_setup_bg - Setup Block guard structures and debug areas.
5614  * @phba: pointer to lpfc hba data structure.
5615  * @shost: the shost to be used to detect Block guard settings.
5616  *
5617  * This routine sets up the local Block guard protocol settings for @shost.
5618  * This routine also allocates memory for debugging bg buffers.
5619  **/
5620 static void
5621 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5622 {
5623         uint32_t old_mask;
5624         uint32_t old_guard;
5625
5626         int pagecnt = 10;
5627         if (lpfc_prot_mask && lpfc_prot_guard) {
5628                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5629                                 "1478 Registering BlockGuard with the "
5630                                 "SCSI layer\n");
5631
5632                 old_mask = lpfc_prot_mask;
5633                 old_guard = lpfc_prot_guard;
5634
5635                 /* Only allow supported values */
5636                 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5637                         SHOST_DIX_TYPE0_PROTECTION |
5638                         SHOST_DIX_TYPE1_PROTECTION);
5639                 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5640
5641                 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5642                 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5643                         lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5644
5645                 if (lpfc_prot_mask && lpfc_prot_guard) {
5646                         if ((old_mask != lpfc_prot_mask) ||
5647                                 (old_guard != lpfc_prot_guard))
5648                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5649                                         "1475 Registering BlockGuard with the "
5650                                         "SCSI layer: mask %d  guard %d\n",
5651                                         lpfc_prot_mask, lpfc_prot_guard);
5652
5653                         scsi_host_set_prot(shost, lpfc_prot_mask);
5654                         scsi_host_set_guard(shost, lpfc_prot_guard);
5655                 } else
5656                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5657                                 "1479 Not Registering BlockGuard with the SCSI "
5658                                 "layer, Bad protection parameters: %d %d\n",
5659                                 old_mask, old_guard);
5660         }
5661
5662         if (!_dump_buf_data) {
5663                 while (pagecnt) {
5664                         spin_lock_init(&_dump_buf_lock);
5665                         _dump_buf_data =
5666                                 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5667                         if (_dump_buf_data) {
5668                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5669                                         "9043 BLKGRD: allocated %d pages for "
5670                                        "_dump_buf_data at 0x%p\n",
5671                                        (1 << pagecnt), _dump_buf_data);
5672                                 _dump_buf_data_order = pagecnt;
5673                                 memset(_dump_buf_data, 0,
5674                                        ((1 << PAGE_SHIFT) << pagecnt));
5675                                 break;
5676                         } else
5677                                 --pagecnt;
5678                 }
5679                 if (!_dump_buf_data_order)
5680                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5681                                 "9044 BLKGRD: ERROR unable to allocate "
5682                                "memory for hexdump\n");
5683         } else
5684                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5685                         "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
5686                        "\n", _dump_buf_data);
5687         if (!_dump_buf_dif) {
5688                 while (pagecnt) {
5689                         _dump_buf_dif =
5690                                 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5691                         if (_dump_buf_dif) {
5692                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5693                                         "9046 BLKGRD: allocated %d pages for "
5694                                        "_dump_buf_dif at 0x%p\n",
5695                                        (1 << pagecnt), _dump_buf_dif);
5696                                 _dump_buf_dif_order = pagecnt;
5697                                 memset(_dump_buf_dif, 0,
5698                                        ((1 << PAGE_SHIFT) << pagecnt));
5699                                 break;
5700                         } else
5701                                 --pagecnt;
5702                 }
5703                 if (!_dump_buf_dif_order)
5704                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5705                         "9047 BLKGRD: ERROR unable to allocate "
5706                                "memory for hexdump\n");
5707         } else
5708                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5709                         "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
5710                        _dump_buf_dif);
5711 }
5712
5713 /**
5714  * lpfc_post_init_setup - Perform necessary device post initialization setup.
5715  * @phba: pointer to lpfc hba data structure.
5716  *
5717  * This routine is invoked to perform all the necessary post initialization
5718  * setup for the device.
5719  **/
5720 static void
5721 lpfc_post_init_setup(struct lpfc_hba *phba)
5722 {
5723         struct Scsi_Host  *shost;
5724         struct lpfc_adapter_event_header adapter_event;
5725
5726         /* Get the default values for Model Name and Description */
5727         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5728
5729         /*
5730          * hba setup may have changed the hba_queue_depth so we need to
5731          * adjust the value of can_queue.
5732      &nbs