blob: 9acb5b44ce4c13a5753deb9c51143e0751d0be41 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
6 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
7 * EMULEX and SLI are trademarks of Emulex. *
8 * www.broadcom.com *
9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
10 * *
11 * This program is free software; you can redistribute it and/or *
12 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
22 *******************************************************************/
23
24#include <linux/blkdev.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/idr.h>
28#include <linux/interrupt.h>
29#include <linux/module.h>
30#include <linux/kthread.h>
31#include <linux/pci.h>
32#include <linux/spinlock.h>
33#include <linux/ctype.h>
34#include <linux/aer.h>
35#include <linux/slab.h>
36#include <linux/firmware.h>
37#include <linux/miscdevice.h>
38#include <linux/percpu.h>
39#include <linux/msi.h>
40#include <linux/bitops.h>
41
42#include <scsi/scsi.h>
43#include <scsi/scsi_device.h>
44#include <scsi/scsi_host.h>
45#include <scsi/scsi_transport_fc.h>
46#include <scsi/scsi_tcq.h>
47#include <scsi/fc/fc_fs.h>
48
49#include <linux/nvme-fc-driver.h>
50
51#include "lpfc_hw4.h"
52#include "lpfc_hw.h"
53#include "lpfc_sli.h"
54#include "lpfc_sli4.h"
55#include "lpfc_nl.h"
56#include "lpfc_disc.h"
57#include "lpfc.h"
58#include "lpfc_scsi.h"
59#include "lpfc_nvme.h"
60#include "lpfc_nvmet.h"
61#include "lpfc_logmsg.h"
62#include "lpfc_crtn.h"
63#include "lpfc_vport.h"
64#include "lpfc_version.h"
65#include "lpfc_ids.h"
66
67char *_dump_buf_data;
68unsigned long _dump_buf_data_order;
69char *_dump_buf_dif;
70unsigned long _dump_buf_dif_order;
71spinlock_t _dump_buf_lock;
72
73/* Used when mapping IRQ vectors in a driver centric manner */
74uint16_t *lpfc_used_cpu;
75uint32_t lpfc_present_cpu;
76
77static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
78static int lpfc_post_rcv_buf(struct lpfc_hba *);
79static int lpfc_sli4_queue_verify(struct lpfc_hba *);
80static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
81static int lpfc_setup_endian_order(struct lpfc_hba *);
82static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
83static void lpfc_free_els_sgl_list(struct lpfc_hba *);
84static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
85static void lpfc_init_sgl_list(struct lpfc_hba *);
86static int lpfc_init_active_sgl_array(struct lpfc_hba *);
87static void lpfc_free_active_sgl(struct lpfc_hba *);
88static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
89static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
90static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
91static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
92static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
93static void lpfc_sli4_disable_intr(struct lpfc_hba *);
94static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
95static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
96
97static struct scsi_transport_template *lpfc_transport_template = NULL;
98static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
99static DEFINE_IDR(lpfc_hba_index);
100#define LPFC_NVMET_BUF_POST 254
101
102/**
103 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
104 * @phba: pointer to lpfc hba data structure.
105 *
106 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
107 * mailbox command. It retrieves the revision information from the HBA and
108 * collects the Vital Product Data (VPD) about the HBA for preparing the
109 * configuration of the HBA.
110 *
111 * Return codes:
112 * 0 - success.
113 * -ERESTART - requests the SLI layer to reset the HBA and try again.
114 * Any other value - indicates an error.
115 **/
116int
117lpfc_config_port_prep(struct lpfc_hba *phba)
118{
119 lpfc_vpd_t *vp = &phba->vpd;
120 int i = 0, rc;
121 LPFC_MBOXQ_t *pmb;
122 MAILBOX_t *mb;
123 char *lpfc_vpd_data = NULL;
124 uint16_t offset = 0;
125 static char licensed[56] =
126 "key unlock for use with gnu public licensed code only\0";
127 static int init_key = 1;
128
129 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
130 if (!pmb) {
131 phba->link_state = LPFC_HBA_ERROR;
132 return -ENOMEM;
133 }
134
135 mb = &pmb->u.mb;
136 phba->link_state = LPFC_INIT_MBX_CMDS;
137
138 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
139 if (init_key) {
140 uint32_t *ptext = (uint32_t *) licensed;
141
142 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
143 *ptext = cpu_to_be32(*ptext);
144 init_key = 0;
145 }
146
147 lpfc_read_nv(phba, pmb);
148 memset((char*)mb->un.varRDnvp.rsvd3, 0,
149 sizeof (mb->un.varRDnvp.rsvd3));
150 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
151 sizeof (licensed));
152
153 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154
155 if (rc != MBX_SUCCESS) {
156 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
157 "0324 Config Port initialization "
158 "error, mbxCmd x%x READ_NVPARM, "
159 "mbxStatus x%x\n",
160 mb->mbxCommand, mb->mbxStatus);
161 mempool_free(pmb, phba->mbox_mem_pool);
162 return -ERESTART;
163 }
164 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
165 sizeof(phba->wwnn));
166 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
167 sizeof(phba->wwpn));
168 }
169
170 /*
171 * Clear all option bits except LPFC_SLI3_BG_ENABLED,
172 * which was already set in lpfc_get_cfgparam()
173 */
174 phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
175
176 /* Setup and issue mailbox READ REV command */
177 lpfc_read_rev(phba, pmb);
178 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
179 if (rc != MBX_SUCCESS) {
180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
181 "0439 Adapter failed to init, mbxCmd x%x "
182 "READ_REV, mbxStatus x%x\n",
183 mb->mbxCommand, mb->mbxStatus);
184 mempool_free( pmb, phba->mbox_mem_pool);
185 return -ERESTART;
186 }
187
188
189 /*
190 * The value of rr must be 1 since the driver set the cv field to 1.
191 * This setting requires the FW to set all revision fields.
192 */
193 if (mb->un.varRdRev.rr == 0) {
194 vp->rev.rBit = 0;
195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
196 "0440 Adapter failed to init, READ_REV has "
197 "missing revision information.\n");
198 mempool_free(pmb, phba->mbox_mem_pool);
199 return -ERESTART;
200 }
201
202 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
203 mempool_free(pmb, phba->mbox_mem_pool);
204 return -EINVAL;
205 }
206
207 /* Save information as VPD data */
208 vp->rev.rBit = 1;
209 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
210 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
211 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
212 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
213 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
214 vp->rev.biuRev = mb->un.varRdRev.biuRev;
215 vp->rev.smRev = mb->un.varRdRev.smRev;
216 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
217 vp->rev.endecRev = mb->un.varRdRev.endecRev;
218 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
219 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
220 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
221 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
222 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
223 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
224
225 /* If the sli feature level is less then 9, we must
226 * tear down all RPIs and VPIs on link down if NPIV
227 * is enabled.
228 */
229 if (vp->rev.feaLevelHigh < 9)
230 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
231
232 if (lpfc_is_LC_HBA(phba->pcidev->device))
233 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
234 sizeof (phba->RandomData));
235
236 /* Get adapter VPD information */
237 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
238 if (!lpfc_vpd_data)
239 goto out_free_mbox;
240 do {
241 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
242 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
243
244 if (rc != MBX_SUCCESS) {
245 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
246 "0441 VPD not present on adapter, "
247 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
248 mb->mbxCommand, mb->mbxStatus);
249 mb->un.varDmp.word_cnt = 0;
250 }
251 /* dump mem may return a zero when finished or we got a
252 * mailbox error, either way we are done.
253 */
254 if (mb->un.varDmp.word_cnt == 0)
255 break;
256 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
257 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
258 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
259 lpfc_vpd_data + offset,
260 mb->un.varDmp.word_cnt);
261 offset += mb->un.varDmp.word_cnt;
262 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
263 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
264
265 kfree(lpfc_vpd_data);
266out_free_mbox:
267 mempool_free(pmb, phba->mbox_mem_pool);
268 return 0;
269}
270
271/**
272 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
273 * @phba: pointer to lpfc hba data structure.
274 * @pmboxq: pointer to the driver internal queue element for mailbox command.
275 *
276 * This is the completion handler for driver's configuring asynchronous event
277 * mailbox command to the device. If the mailbox command returns successfully,
278 * it will set internal async event support flag to 1; otherwise, it will
279 * set internal async event support flag to 0.
280 **/
281static void
282lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
283{
284 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
285 phba->temp_sensor_support = 1;
286 else
287 phba->temp_sensor_support = 0;
288 mempool_free(pmboxq, phba->mbox_mem_pool);
289 return;
290}
291
292/**
293 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
294 * @phba: pointer to lpfc hba data structure.
295 * @pmboxq: pointer to the driver internal queue element for mailbox command.
296 *
297 * This is the completion handler for dump mailbox command for getting
298 * wake up parameters. When this command complete, the response contain
299 * Option rom version of the HBA. This function translate the version number
300 * into a human readable string and store it in OptionROMVersion.
301 **/
302static void
303lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
304{
305 struct prog_id *prg;
306 uint32_t prog_id_word;
307 char dist = ' ';
308 /* character array used for decoding dist type. */
309 char dist_char[] = "nabx";
310
311 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
312 mempool_free(pmboxq, phba->mbox_mem_pool);
313 return;
314 }
315
316 prg = (struct prog_id *) &prog_id_word;
317
318 /* word 7 contain option rom version */
319 prog_id_word = pmboxq->u.mb.un.varWords[7];
320
321 /* Decode the Option rom version word to a readable string */
322 if (prg->dist < 4)
323 dist = dist_char[prg->dist];
324
325 if ((prg->dist == 3) && (prg->num == 0))
326 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
327 prg->ver, prg->rev, prg->lev);
328 else
329 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
330 prg->ver, prg->rev, prg->lev,
331 dist, prg->num);
332 mempool_free(pmboxq, phba->mbox_mem_pool);
333 return;
334}
335
336/**
337 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
338 * cfg_soft_wwnn, cfg_soft_wwpn
339 * @vport: pointer to lpfc vport data structure.
340 *
341 *
342 * Return codes
343 * None.
344 **/
345void
346lpfc_update_vport_wwn(struct lpfc_vport *vport)
347{
348 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
349 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
350
351 /* If the soft name exists then update it using the service params */
352 if (vport->phba->cfg_soft_wwnn)
353 u64_to_wwn(vport->phba->cfg_soft_wwnn,
354 vport->fc_sparam.nodeName.u.wwn);
355 if (vport->phba->cfg_soft_wwpn)
356 u64_to_wwn(vport->phba->cfg_soft_wwpn,
357 vport->fc_sparam.portName.u.wwn);
358
359 /*
360 * If the name is empty or there exists a soft name
361 * then copy the service params name, otherwise use the fc name
362 */
363 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
364 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
365 sizeof(struct lpfc_name));
366 else
367 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
368 sizeof(struct lpfc_name));
369
370 /*
371 * If the port name has changed, then set the Param changes flag
372 * to unreg the login
373 */
374 if (vport->fc_portname.u.wwn[0] != 0 &&
375 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
376 sizeof(struct lpfc_name)))
377 vport->vport_flag |= FAWWPN_PARAM_CHG;
378
379 if (vport->fc_portname.u.wwn[0] == 0 ||
380 vport->phba->cfg_soft_wwpn ||
381 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
382 vport->vport_flag & FAWWPN_SET) {
383 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
384 sizeof(struct lpfc_name));
385 vport->vport_flag &= ~FAWWPN_SET;
386 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
387 vport->vport_flag |= FAWWPN_SET;
388 }
389 else
390 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
391 sizeof(struct lpfc_name));
392}
393
394/**
395 * lpfc_config_port_post - Perform lpfc initialization after config port
396 * @phba: pointer to lpfc hba data structure.
397 *
398 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
399 * command call. It performs all internal resource and state setups on the
400 * port: post IOCB buffers, enable appropriate host interrupt attentions,
401 * ELS ring timers, etc.
402 *
403 * Return codes
404 * 0 - success.
405 * Any other value - error.
406 **/
407int
408lpfc_config_port_post(struct lpfc_hba *phba)
409{
410 struct lpfc_vport *vport = phba->pport;
411 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
412 LPFC_MBOXQ_t *pmb;
413 MAILBOX_t *mb;
414 struct lpfc_dmabuf *mp;
415 struct lpfc_sli *psli = &phba->sli;
416 uint32_t status, timeout;
417 int i, j;
418 int rc;
419
420 spin_lock_irq(&phba->hbalock);
421 /*
422 * If the Config port completed correctly the HBA is not
423 * over heated any more.
424 */
425 if (phba->over_temp_state == HBA_OVER_TEMP)
426 phba->over_temp_state = HBA_NORMAL_TEMP;
427 spin_unlock_irq(&phba->hbalock);
428
429 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
430 if (!pmb) {
431 phba->link_state = LPFC_HBA_ERROR;
432 return -ENOMEM;
433 }
434 mb = &pmb->u.mb;
435
436 /* Get login parameters for NID. */
437 rc = lpfc_read_sparam(phba, pmb, 0);
438 if (rc) {
439 mempool_free(pmb, phba->mbox_mem_pool);
440 return -ENOMEM;
441 }
442
443 pmb->vport = vport;
444 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
446 "0448 Adapter failed init, mbxCmd x%x "
447 "READ_SPARM mbxStatus x%x\n",
448 mb->mbxCommand, mb->mbxStatus);
449 phba->link_state = LPFC_HBA_ERROR;
450 mp = (struct lpfc_dmabuf *) pmb->context1;
451 mempool_free(pmb, phba->mbox_mem_pool);
452 lpfc_mbuf_free(phba, mp->virt, mp->phys);
453 kfree(mp);
454 return -EIO;
455 }
456
457 mp = (struct lpfc_dmabuf *) pmb->context1;
458
459 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
460 lpfc_mbuf_free(phba, mp->virt, mp->phys);
461 kfree(mp);
462 pmb->context1 = NULL;
463 lpfc_update_vport_wwn(vport);
464
465 /* Update the fc_host data structures with new wwn. */
466 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
467 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
468 fc_host_max_npiv_vports(shost) = phba->max_vpi;
469
470 /* If no serial number in VPD data, use low 6 bytes of WWNN */
471 /* This should be consolidated into parse_vpd ? - mr */
472 if (phba->SerialNumber[0] == 0) {
473 uint8_t *outptr;
474
475 outptr = &vport->fc_nodename.u.s.IEEE[0];
476 for (i = 0; i < 12; i++) {
477 status = *outptr++;
478 j = ((status & 0xf0) >> 4);
479 if (j <= 9)
480 phba->SerialNumber[i] =
481 (char)((uint8_t) 0x30 + (uint8_t) j);
482 else
483 phba->SerialNumber[i] =
484 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
485 i++;
486 j = (status & 0xf);
487 if (j <= 9)
488 phba->SerialNumber[i] =
489 (char)((uint8_t) 0x30 + (uint8_t) j);
490 else
491 phba->SerialNumber[i] =
492 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
493 }
494 }
495
496 lpfc_read_config(phba, pmb);
497 pmb->vport = vport;
498 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
500 "0453 Adapter failed to init, mbxCmd x%x "
501 "READ_CONFIG, mbxStatus x%x\n",
502 mb->mbxCommand, mb->mbxStatus);
503 phba->link_state = LPFC_HBA_ERROR;
504 mempool_free( pmb, phba->mbox_mem_pool);
505 return -EIO;
506 }
507
508 /* Check if the port is disabled */
509 lpfc_sli_read_link_ste(phba);
510
511 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
512 i = (mb->un.varRdConfig.max_xri + 1);
513 if (phba->cfg_hba_queue_depth > i) {
514 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
515 "3359 HBA queue depth changed from %d to %d\n",
516 phba->cfg_hba_queue_depth, i);
517 phba->cfg_hba_queue_depth = i;
518 }
519
520 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
521 i = (mb->un.varRdConfig.max_xri >> 3);
522 if (phba->pport->cfg_lun_queue_depth > i) {
523 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
524 "3360 LUN queue depth changed from %d to %d\n",
525 phba->pport->cfg_lun_queue_depth, i);
526 phba->pport->cfg_lun_queue_depth = i;
527 }
528
529 phba->lmt = mb->un.varRdConfig.lmt;
530
531 /* Get the default values for Model Name and Description */
532 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
533
534 phba->link_state = LPFC_LINK_DOWN;
535
536 /* Only process IOCBs on ELS ring till hba_state is READY */
537 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
538 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
539 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
540 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
541
542 /* Post receive buffers for desired rings */
543 if (phba->sli_rev != 3)
544 lpfc_post_rcv_buf(phba);
545
546 /*
547 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
548 */
549 if (phba->intr_type == MSIX) {
550 rc = lpfc_config_msi(phba, pmb);
551 if (rc) {
552 mempool_free(pmb, phba->mbox_mem_pool);
553 return -EIO;
554 }
555 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
556 if (rc != MBX_SUCCESS) {
557 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
558 "0352 Config MSI mailbox command "
559 "failed, mbxCmd x%x, mbxStatus x%x\n",
560 pmb->u.mb.mbxCommand,
561 pmb->u.mb.mbxStatus);
562 mempool_free(pmb, phba->mbox_mem_pool);
563 return -EIO;
564 }
565 }
566
567 spin_lock_irq(&phba->hbalock);
568 /* Initialize ERATT handling flag */
569 phba->hba_flag &= ~HBA_ERATT_HANDLED;
570
571 /* Enable appropriate host interrupts */
572 if (lpfc_readl(phba->HCregaddr, &status)) {
573 spin_unlock_irq(&phba->hbalock);
574 return -EIO;
575 }
576 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
577 if (psli->num_rings > 0)
578 status |= HC_R0INT_ENA;
579 if (psli->num_rings > 1)
580 status |= HC_R1INT_ENA;
581 if (psli->num_rings > 2)
582 status |= HC_R2INT_ENA;
583 if (psli->num_rings > 3)
584 status |= HC_R3INT_ENA;
585
586 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
587 (phba->cfg_poll & DISABLE_FCP_RING_INT))
588 status &= ~(HC_R0INT_ENA);
589
590 writel(status, phba->HCregaddr);
591 readl(phba->HCregaddr); /* flush */
592 spin_unlock_irq(&phba->hbalock);
593
594 /* Set up ring-0 (ELS) timer */
595 timeout = phba->fc_ratov * 2;
596 mod_timer(&vport->els_tmofunc,
597 jiffies + msecs_to_jiffies(1000 * timeout));
598 /* Set up heart beat (HB) timer */
599 mod_timer(&phba->hb_tmofunc,
600 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
601 phba->hb_outstanding = 0;
602 phba->last_completion_time = jiffies;
603 /* Set up error attention (ERATT) polling timer */
604 mod_timer(&phba->eratt_poll,
605 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
606
607 if (phba->hba_flag & LINK_DISABLED) {
608 lpfc_printf_log(phba,
609 KERN_ERR, LOG_INIT,
610 "2598 Adapter Link is disabled.\n");
611 lpfc_down_link(phba, pmb);
612 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
613 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
614 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
615 lpfc_printf_log(phba,
616 KERN_ERR, LOG_INIT,
617 "2599 Adapter failed to issue DOWN_LINK"
618 " mbox command rc 0x%x\n", rc);
619
620 mempool_free(pmb, phba->mbox_mem_pool);
621 return -EIO;
622 }
623 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
624 mempool_free(pmb, phba->mbox_mem_pool);
625 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
626 if (rc)
627 return rc;
628 }
629 /* MBOX buffer will be freed in mbox compl */
630 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
631 if (!pmb) {
632 phba->link_state = LPFC_HBA_ERROR;
633 return -ENOMEM;
634 }
635
636 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
637 pmb->mbox_cmpl = lpfc_config_async_cmpl;
638 pmb->vport = phba->pport;
639 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
640
641 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
642 lpfc_printf_log(phba,
643 KERN_ERR,
644 LOG_INIT,
645 "0456 Adapter failed to issue "
646 "ASYNCEVT_ENABLE mbox status x%x\n",
647 rc);
648 mempool_free(pmb, phba->mbox_mem_pool);
649 }
650
651 /* Get Option rom version */
652 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
653 if (!pmb) {
654 phba->link_state = LPFC_HBA_ERROR;
655 return -ENOMEM;
656 }
657
658 lpfc_dump_wakeup_param(phba, pmb);
659 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
660 pmb->vport = phba->pport;
661 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
662
663 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
665 "to get Option ROM version status x%x\n", rc);
666 mempool_free(pmb, phba->mbox_mem_pool);
667 }
668
669 return 0;
670}
671
672/**
673 * lpfc_hba_init_link - Initialize the FC link
674 * @phba: pointer to lpfc hba data structure.
675 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
676 *
677 * This routine will issue the INIT_LINK mailbox command call.
678 * It is available to other drivers through the lpfc_hba data
679 * structure for use as a delayed link up mechanism with the
680 * module parameter lpfc_suppress_link_up.
681 *
682 * Return code
683 * 0 - success
684 * Any other value - error
685 **/
686static int
687lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
688{
689 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
690}
691
692/**
693 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
694 * @phba: pointer to lpfc hba data structure.
695 * @fc_topology: desired fc topology.
696 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
697 *
698 * This routine will issue the INIT_LINK mailbox command call.
699 * It is available to other drivers through the lpfc_hba data
700 * structure for use as a delayed link up mechanism with the
701 * module parameter lpfc_suppress_link_up.
702 *
703 * Return code
704 * 0 - success
705 * Any other value - error
706 **/
707int
708lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
709 uint32_t flag)
710{
711 struct lpfc_vport *vport = phba->pport;
712 LPFC_MBOXQ_t *pmb;
713 MAILBOX_t *mb;
714 int rc;
715
716 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
717 if (!pmb) {
718 phba->link_state = LPFC_HBA_ERROR;
719 return -ENOMEM;
720 }
721 mb = &pmb->u.mb;
722 pmb->vport = vport;
723
724 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
725 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
726 !(phba->lmt & LMT_1Gb)) ||
727 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
728 !(phba->lmt & LMT_2Gb)) ||
729 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
730 !(phba->lmt & LMT_4Gb)) ||
731 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
732 !(phba->lmt & LMT_8Gb)) ||
733 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
734 !(phba->lmt & LMT_10Gb)) ||
735 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
736 !(phba->lmt & LMT_16Gb)) ||
737 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
738 !(phba->lmt & LMT_32Gb)) ||
739 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
740 !(phba->lmt & LMT_64Gb))) {
741 /* Reset link speed to auto */
742 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
743 "1302 Invalid speed for this board:%d "
744 "Reset link speed to auto.\n",
745 phba->cfg_link_speed);
746 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
747 }
748 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
749 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
750 if (phba->sli_rev < LPFC_SLI_REV4)
751 lpfc_set_loopback_flag(phba);
752 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
753 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
755 "0498 Adapter failed to init, mbxCmd x%x "
756 "INIT_LINK, mbxStatus x%x\n",
757 mb->mbxCommand, mb->mbxStatus);
758 if (phba->sli_rev <= LPFC_SLI_REV3) {
759 /* Clear all interrupt enable conditions */
760 writel(0, phba->HCregaddr);
761 readl(phba->HCregaddr); /* flush */
762 /* Clear all pending interrupts */
763 writel(0xffffffff, phba->HAregaddr);
764 readl(phba->HAregaddr); /* flush */
765 }
766 phba->link_state = LPFC_HBA_ERROR;
767 if (rc != MBX_BUSY || flag == MBX_POLL)
768 mempool_free(pmb, phba->mbox_mem_pool);
769 return -EIO;
770 }
771 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
772 if (flag == MBX_POLL)
773 mempool_free(pmb, phba->mbox_mem_pool);
774
775 return 0;
776}
777
778/**
779 * lpfc_hba_down_link - this routine downs the FC link
780 * @phba: pointer to lpfc hba data structure.
781 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
782 *
783 * This routine will issue the DOWN_LINK mailbox command call.
784 * It is available to other drivers through the lpfc_hba data
785 * structure for use to stop the link.
786 *
787 * Return code
788 * 0 - success
789 * Any other value - error
790 **/
791static int
792lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
793{
794 LPFC_MBOXQ_t *pmb;
795 int rc;
796
797 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
798 if (!pmb) {
799 phba->link_state = LPFC_HBA_ERROR;
800 return -ENOMEM;
801 }
802
803 lpfc_printf_log(phba,
804 KERN_ERR, LOG_INIT,
805 "0491 Adapter Link is disabled.\n");
806 lpfc_down_link(phba, pmb);
807 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
808 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
809 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
810 lpfc_printf_log(phba,
811 KERN_ERR, LOG_INIT,
812 "2522 Adapter failed to issue DOWN_LINK"
813 " mbox command rc 0x%x\n", rc);
814
815 mempool_free(pmb, phba->mbox_mem_pool);
816 return -EIO;
817 }
818 if (flag == MBX_POLL)
819 mempool_free(pmb, phba->mbox_mem_pool);
820
821 return 0;
822}
823
824/**
825 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
826 * @phba: pointer to lpfc HBA data structure.
827 *
828 * This routine will do LPFC uninitialization before the HBA is reset when
829 * bringing down the SLI Layer.
830 *
831 * Return codes
832 * 0 - success.
833 * Any other value - error.
834 **/
835int
836lpfc_hba_down_prep(struct lpfc_hba *phba)
837{
838 struct lpfc_vport **vports;
839 int i;
840
841 if (phba->sli_rev <= LPFC_SLI_REV3) {
842 /* Disable interrupts */
843 writel(0, phba->HCregaddr);
844 readl(phba->HCregaddr); /* flush */
845 }
846
847 if (phba->pport->load_flag & FC_UNLOADING)
848 lpfc_cleanup_discovery_resources(phba->pport);
849 else {
850 vports = lpfc_create_vport_work_array(phba);
851 if (vports != NULL)
852 for (i = 0; i <= phba->max_vports &&
853 vports[i] != NULL; i++)
854 lpfc_cleanup_discovery_resources(vports[i]);
855 lpfc_destroy_vport_work_array(phba, vports);
856 }
857 return 0;
858}
859
860/**
861 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
862 * rspiocb which got deferred
863 *
864 * @phba: pointer to lpfc HBA data structure.
865 *
866 * This routine will cleanup completed slow path events after HBA is reset
867 * when bringing down the SLI Layer.
868 *
869 *
870 * Return codes
871 * void.
872 **/
873static void
874lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
875{
876 struct lpfc_iocbq *rspiocbq;
877 struct hbq_dmabuf *dmabuf;
878 struct lpfc_cq_event *cq_event;
879
880 spin_lock_irq(&phba->hbalock);
881 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
882 spin_unlock_irq(&phba->hbalock);
883
884 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
885 /* Get the response iocb from the head of work queue */
886 spin_lock_irq(&phba->hbalock);
887 list_remove_head(&phba->sli4_hba.sp_queue_event,
888 cq_event, struct lpfc_cq_event, list);
889 spin_unlock_irq(&phba->hbalock);
890
891 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
892 case CQE_CODE_COMPL_WQE:
893 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
894 cq_event);
895 lpfc_sli_release_iocbq(phba, rspiocbq);
896 break;
897 case CQE_CODE_RECEIVE:
898 case CQE_CODE_RECEIVE_V1:
899 dmabuf = container_of(cq_event, struct hbq_dmabuf,
900 cq_event);
901 lpfc_in_buf_free(phba, &dmabuf->dbuf);
902 }
903 }
904}
905
906/**
907 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
908 * @phba: pointer to lpfc HBA data structure.
909 *
910 * This routine will cleanup posted ELS buffers after the HBA is reset
911 * when bringing down the SLI Layer.
912 *
913 *
914 * Return codes
915 * void.
916 **/
917static void
918lpfc_hba_free_post_buf(struct lpfc_hba *phba)
919{
920 struct lpfc_sli *psli = &phba->sli;
921 struct lpfc_sli_ring *pring;
922 struct lpfc_dmabuf *mp, *next_mp;
923 LIST_HEAD(buflist);
924 int count;
925
926 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
927 lpfc_sli_hbqbuf_free_all(phba);
928 else {
929 /* Cleanup preposted buffers on the ELS ring */
930 pring = &psli->sli3_ring[LPFC_ELS_RING];
931 spin_lock_irq(&phba->hbalock);
932 list_splice_init(&pring->postbufq, &buflist);
933 spin_unlock_irq(&phba->hbalock);
934
935 count = 0;
936 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
937 list_del(&mp->list);
938 count++;
939 lpfc_mbuf_free(phba, mp->virt, mp->phys);
940 kfree(mp);
941 }
942
943 spin_lock_irq(&phba->hbalock);
944 pring->postbufq_cnt -= count;
945 spin_unlock_irq(&phba->hbalock);
946 }
947}
948
949/**
950 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
951 * @phba: pointer to lpfc HBA data structure.
952 *
953 * This routine will cleanup the txcmplq after the HBA is reset when bringing
954 * down the SLI Layer.
955 *
956 * Return codes
957 * void
958 **/
959static void
960lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
961{
962 struct lpfc_sli *psli = &phba->sli;
963 struct lpfc_queue *qp = NULL;
964 struct lpfc_sli_ring *pring;
965 LIST_HEAD(completions);
966 int i;
967 struct lpfc_iocbq *piocb, *next_iocb;
968
969 if (phba->sli_rev != LPFC_SLI_REV4) {
970 for (i = 0; i < psli->num_rings; i++) {
971 pring = &psli->sli3_ring[i];
972 spin_lock_irq(&phba->hbalock);
973 /* At this point in time the HBA is either reset or DOA
974 * Nothing should be on txcmplq as it will
975 * NEVER complete.
976 */
977 list_splice_init(&pring->txcmplq, &completions);
978 pring->txcmplq_cnt = 0;
979 spin_unlock_irq(&phba->hbalock);
980
981 lpfc_sli_abort_iocb_ring(phba, pring);
982 }
983 /* Cancel all the IOCBs from the completions list */
984 lpfc_sli_cancel_iocbs(phba, &completions,
985 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
986 return;
987 }
988 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
989 pring = qp->pring;
990 if (!pring)
991 continue;
992 spin_lock_irq(&pring->ring_lock);
993 list_for_each_entry_safe(piocb, next_iocb,
994 &pring->txcmplq, list)
995 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
996 list_splice_init(&pring->txcmplq, &completions);
997 pring->txcmplq_cnt = 0;
998 spin_unlock_irq(&pring->ring_lock);
999 lpfc_sli_abort_iocb_ring(phba, pring);
1000 }
1001 /* Cancel all the IOCBs from the completions list */
1002 lpfc_sli_cancel_iocbs(phba, &completions,
1003 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
1004}
1005
1006/**
1007 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
1008 int i;
1009 * @phba: pointer to lpfc HBA data structure.
1010 *
1011 * This routine will do uninitialization after the HBA is reset when bring
1012 * down the SLI Layer.
1013 *
1014 * Return codes
1015 * 0 - success.
1016 * Any other value - error.
1017 **/
1018static int
1019lpfc_hba_down_post_s3(struct lpfc_hba *phba)
1020{
1021 lpfc_hba_free_post_buf(phba);
1022 lpfc_hba_clean_txcmplq(phba);
1023 return 0;
1024}
1025
1026/**
1027 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1028 * @phba: pointer to lpfc HBA data structure.
1029 *
1030 * This routine will do uninitialization after the HBA is reset when bring
1031 * down the SLI Layer.
1032 *
1033 * Return codes
1034 * 0 - success.
1035 * Any other value - error.
1036 **/
1037static int
1038lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1039{
1040 struct lpfc_scsi_buf *psb, *psb_next;
1041 struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
1042 LIST_HEAD(aborts);
1043 LIST_HEAD(nvme_aborts);
1044 LIST_HEAD(nvmet_aborts);
1045 unsigned long iflag = 0;
1046 struct lpfc_sglq *sglq_entry = NULL;
1047 int cnt;
1048
1049
1050 lpfc_sli_hbqbuf_free_all(phba);
1051 lpfc_hba_clean_txcmplq(phba);
1052
1053 /* At this point in time the HBA is either reset or DOA. Either
1054 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
1055 * on the lpfc_els_sgl_list so that it can either be freed if the
1056 * driver is unloading or reposted if the driver is restarting
1057 * the port.
1058 */
1059 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
1060 /* scsl_buf_list */
1061 /* sgl_list_lock required because worker thread uses this
1062 * list.
1063 */
1064 spin_lock(&phba->sli4_hba.sgl_list_lock);
1065 list_for_each_entry(sglq_entry,
1066 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1067 sglq_entry->state = SGL_FREED;
1068
1069 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
1070 &phba->sli4_hba.lpfc_els_sgl_list);
1071
1072
1073 spin_unlock(&phba->sli4_hba.sgl_list_lock);
1074 /* abts_scsi_buf_list_lock required because worker thread uses this
1075 * list.
1076 */
1077 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1078 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1079 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1080 &aborts);
1081 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1082 }
1083
1084 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1085 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1086 list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1087 &nvme_aborts);
1088 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1089 &nvmet_aborts);
1090 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1091 }
1092
1093 spin_unlock_irq(&phba->hbalock);
1094
1095 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1096 psb->pCmd = NULL;
1097 psb->status = IOSTAT_SUCCESS;
1098 }
1099 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1100 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1101 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1102
1103 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1104 cnt = 0;
1105 list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1106 psb->pCmd = NULL;
1107 psb->status = IOSTAT_SUCCESS;
1108 cnt++;
1109 }
1110 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1111 phba->put_nvme_bufs += cnt;
1112 list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1113 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1114
1115 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1116 ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
1117 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
1118 }
1119 }
1120
1121 lpfc_sli4_free_sp_events(phba);
1122 return 0;
1123}
1124
1125/**
1126 * lpfc_hba_down_post - Wrapper func for hba down post routine
1127 * @phba: pointer to lpfc HBA data structure.
1128 *
1129 * This routine wraps the actual SLI3 or SLI4 routine for performing
1130 * uninitialization after the HBA is reset when bring down the SLI Layer.
1131 *
1132 * Return codes
1133 * 0 - success.
1134 * Any other value - error.
1135 **/
1136int
1137lpfc_hba_down_post(struct lpfc_hba *phba)
1138{
1139 return (*phba->lpfc_hba_down_post)(phba);
1140}
1141
1142/**
1143 * lpfc_hb_timeout - The HBA-timer timeout handler
1144 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1145 *
1146 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1147 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1148 * work-port-events bitmap and the worker thread is notified. This timeout
1149 * event will be used by the worker thread to invoke the actual timeout
1150 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1151 * be performed in the timeout handler and the HBA timeout event bit shall
1152 * be cleared by the worker thread after it has taken the event bitmap out.
1153 **/
1154static void
1155lpfc_hb_timeout(struct timer_list *t)
1156{
1157 struct lpfc_hba *phba;
1158 uint32_t tmo_posted;
1159 unsigned long iflag;
1160
1161 phba = from_timer(phba, t, hb_tmofunc);
1162
1163 /* Check for heart beat timeout conditions */
1164 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
1165 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1166 if (!tmo_posted)
1167 phba->pport->work_port_events |= WORKER_HB_TMO;
1168 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1169
1170 /* Tell the worker thread there is work to do */
1171 if (!tmo_posted)
1172 lpfc_worker_wake_up(phba);
1173 return;
1174}
1175
1176/**
1177 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1178 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1179 *
1180 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1181 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1182 * work-port-events bitmap and the worker thread is notified. This timeout
1183 * event will be used by the worker thread to invoke the actual timeout
1184 * handler routine, lpfc_rrq_handler. Any periodical operations will
1185 * be performed in the timeout handler and the RRQ timeout event bit shall
1186 * be cleared by the worker thread after it has taken the event bitmap out.
1187 **/
1188static void
1189lpfc_rrq_timeout(struct timer_list *t)
1190{
1191 struct lpfc_hba *phba;
1192 unsigned long iflag;
1193
1194 phba = from_timer(phba, t, rrq_tmr);
1195 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
1196 if (!(phba->pport->load_flag & FC_UNLOADING))
1197 phba->hba_flag |= HBA_RRQ_ACTIVE;
1198 else
1199 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1200 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1201
1202 if (!(phba->pport->load_flag & FC_UNLOADING))
1203 lpfc_worker_wake_up(phba);
1204}
1205
1206/**
1207 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
1208 * @phba: pointer to lpfc hba data structure.
1209 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1210 *
1211 * This is the callback function to the lpfc heart-beat mailbox command.
1212 * If configured, the lpfc driver issues the heart-beat mailbox command to
1213 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1214 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1215 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1216 * heart-beat outstanding state. Once the mailbox command comes back and
1217 * no error conditions detected, the heart-beat mailbox command timer is
1218 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1219 * state is cleared for the next heart-beat. If the timer expired with the
1220 * heart-beat outstanding state set, the driver will put the HBA offline.
1221 **/
1222static void
1223lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1224{
1225 unsigned long drvr_flag;
1226
1227 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1228 phba->hb_outstanding = 0;
1229 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1230
1231 /* Check and reset heart-beat timer is necessary */
1232 mempool_free(pmboxq, phba->mbox_mem_pool);
1233 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1234 !(phba->link_state == LPFC_HBA_ERROR) &&
1235 !(phba->pport->load_flag & FC_UNLOADING))
1236 mod_timer(&phba->hb_tmofunc,
1237 jiffies +
1238 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1239 return;
1240}
1241
1242/**
1243 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
1244 * @phba: pointer to lpfc hba data structure.
1245 *
1246 * This is the actual HBA-timer timeout handler to be invoked by the worker
1247 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1248 * handler performs any periodic operations needed for the device. If such
1249 * periodic event has already been attended to either in the interrupt handler
1250 * or by processing slow-ring or fast-ring events within the HBA-timer
1251 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1252 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1253 * is configured and there is no heart-beat mailbox command outstanding, a
1254 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1255 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1256 * to offline.
1257 **/
1258void
1259lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1260{
1261 struct lpfc_vport **vports;
1262 LPFC_MBOXQ_t *pmboxq;
1263 struct lpfc_dmabuf *buf_ptr;
1264 int retval, i;
1265 struct lpfc_sli *psli = &phba->sli;
1266 LIST_HEAD(completions);
1267 struct lpfc_queue *qp;
1268 unsigned long time_elapsed;
1269 uint32_t tick_cqe, max_cqe, val;
1270 uint64_t tot, data1, data2, data3;
1271 struct lpfc_nvmet_tgtport *tgtp;
1272 struct lpfc_register reg_data;
1273 struct nvme_fc_local_port *localport;
1274 struct lpfc_nvme_lport *lport;
1275 struct lpfc_nvme_ctrl_stat *cstat;
1276 void __iomem *eqdreg = phba->sli4_hba.u.if_type2.EQDregaddr;
1277
1278 vports = lpfc_create_vport_work_array(phba);
1279 if (vports != NULL)
1280 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1281 lpfc_rcv_seq_check_edtov(vports[i]);
1282 lpfc_fdmi_num_disc_check(vports[i]);
1283 }
1284 lpfc_destroy_vport_work_array(phba, vports);
1285
1286 if ((phba->link_state == LPFC_HBA_ERROR) ||
1287 (phba->pport->load_flag & FC_UNLOADING) ||
1288 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1289 return;
1290
1291 if (phba->cfg_auto_imax) {
1292 if (!phba->last_eqdelay_time) {
1293 phba->last_eqdelay_time = jiffies;
1294 goto skip_eqdelay;
1295 }
1296 time_elapsed = jiffies - phba->last_eqdelay_time;
1297 phba->last_eqdelay_time = jiffies;
1298
1299 tot = 0xffff;
1300 /* Check outstanding IO count */
1301 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1302 if (phba->nvmet_support) {
1303 tgtp = phba->targetport->private;
1304 /* Calculate outstanding IOs */
1305 tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
1306 tot += atomic_read(&tgtp->xmt_fcp_release);
1307 tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
1308 } else {
1309 localport = phba->pport->localport;
1310 if (!localport || !localport->private)
1311 goto skip_eqdelay;
1312 lport = (struct lpfc_nvme_lport *)
1313 localport->private;
1314 tot = 0;
1315 for (i = 0;
1316 i < phba->cfg_nvme_io_channel; i++) {
1317 cstat = &lport->cstat[i];
1318 data1 = atomic_read(
1319 &cstat->fc4NvmeInputRequests);
1320 data2 = atomic_read(
1321 &cstat->fc4NvmeOutputRequests);
1322 data3 = atomic_read(
1323 &cstat->fc4NvmeControlRequests);
1324 tot += (data1 + data2 + data3);
1325 tot -= atomic_read(
1326 &cstat->fc4NvmeIoCmpls);
1327 }
1328 }
1329 }
1330
1331 /* Interrupts per sec per EQ */
1332 val = phba->cfg_fcp_imax / phba->io_channel_irqs;
1333 tick_cqe = val / CONFIG_HZ; /* Per tick per EQ */
1334
1335 /* Assume 1 CQE/ISR, calc max CQEs allowed for time duration */
1336 max_cqe = time_elapsed * tick_cqe;
1337
1338 for (i = 0; i < phba->io_channel_irqs; i++) {
1339 /* Fast-path EQ */
1340 qp = phba->sli4_hba.hba_eq[i];
1341 if (!qp)
1342 continue;
1343
1344 /* Use no EQ delay if we don't have many outstanding
1345 * IOs, or if we are only processing 1 CQE/ISR or less.
1346 * Otherwise, assume we can process up to lpfc_fcp_imax
1347 * interrupts per HBA.
1348 */
1349 if (tot < LPFC_NODELAY_MAX_IO ||
1350 qp->EQ_cqe_cnt <= max_cqe)
1351 val = 0;
1352 else
1353 val = phba->cfg_fcp_imax;
1354
1355 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
1356 /* Use EQ Delay Register method */
1357
1358 /* Convert for EQ Delay register */
1359 if (val) {
1360 /* First, interrupts per sec per EQ */
1361 val = phba->cfg_fcp_imax /
1362 phba->io_channel_irqs;
1363
1364 /* us delay between each interrupt */
1365 val = LPFC_SEC_TO_USEC / val;
1366 }
1367 if (val != qp->q_mode) {
1368 reg_data.word0 = 0;
1369 bf_set(lpfc_sliport_eqdelay_id,
1370 &reg_data, qp->queue_id);
1371 bf_set(lpfc_sliport_eqdelay_delay,
1372 &reg_data, val);
1373 writel(reg_data.word0, eqdreg);
1374 }
1375 } else {
1376 /* Use mbox command method */
1377 if (val != qp->q_mode)
1378 lpfc_modify_hba_eq_delay(phba, i,
1379 1, val);
1380 }
1381
1382 /*
1383 * val is cfg_fcp_imax or 0 for mbox delay or us delay
1384 * between interrupts for EQDR.
1385 */
1386 qp->q_mode = val;
1387 qp->EQ_cqe_cnt = 0;
1388 }
1389 }
1390
1391skip_eqdelay:
1392 spin_lock_irq(&phba->pport->work_port_lock);
1393
1394 if (time_after(phba->last_completion_time +
1395 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1396 jiffies)) {
1397 spin_unlock_irq(&phba->pport->work_port_lock);
1398 if (!phba->hb_outstanding)
1399 mod_timer(&phba->hb_tmofunc,
1400 jiffies +
1401 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1402 else
1403 mod_timer(&phba->hb_tmofunc,
1404 jiffies +
1405 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1406 return;
1407 }
1408 spin_unlock_irq(&phba->pport->work_port_lock);
1409
1410 if (phba->elsbuf_cnt &&
1411 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1412 spin_lock_irq(&phba->hbalock);
1413 list_splice_init(&phba->elsbuf, &completions);
1414 phba->elsbuf_cnt = 0;
1415 phba->elsbuf_prev_cnt = 0;
1416 spin_unlock_irq(&phba->hbalock);
1417
1418 while (!list_empty(&completions)) {
1419 list_remove_head(&completions, buf_ptr,
1420 struct lpfc_dmabuf, list);
1421 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1422 kfree(buf_ptr);
1423 }
1424 }
1425 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1426
1427 /* If there is no heart beat outstanding, issue a heartbeat command */
1428 if (phba->cfg_enable_hba_heartbeat) {
1429 if (!phba->hb_outstanding) {
1430 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1431 (list_empty(&psli->mboxq))) {
1432 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1433 GFP_KERNEL);
1434 if (!pmboxq) {
1435 mod_timer(&phba->hb_tmofunc,
1436 jiffies +
1437 msecs_to_jiffies(1000 *
1438 LPFC_HB_MBOX_INTERVAL));
1439 return;
1440 }
1441
1442 lpfc_heart_beat(phba, pmboxq);
1443 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1444 pmboxq->vport = phba->pport;
1445 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1446 MBX_NOWAIT);
1447
1448 if (retval != MBX_BUSY &&
1449 retval != MBX_SUCCESS) {
1450 mempool_free(pmboxq,
1451 phba->mbox_mem_pool);
1452 mod_timer(&phba->hb_tmofunc,
1453 jiffies +
1454 msecs_to_jiffies(1000 *
1455 LPFC_HB_MBOX_INTERVAL));
1456 return;
1457 }
1458 phba->skipped_hb = 0;
1459 phba->hb_outstanding = 1;
1460 } else if (time_before_eq(phba->last_completion_time,
1461 phba->skipped_hb)) {
1462 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1463 "2857 Last completion time not "
1464 " updated in %d ms\n",
1465 jiffies_to_msecs(jiffies
1466 - phba->last_completion_time));
1467 } else
1468 phba->skipped_hb = jiffies;
1469
1470 mod_timer(&phba->hb_tmofunc,
1471 jiffies +
1472 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1473 return;
1474 } else {
1475 /*
1476 * If heart beat timeout called with hb_outstanding set
1477 * we need to give the hb mailbox cmd a chance to
1478 * complete or TMO.
1479 */
1480 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1481 "0459 Adapter heartbeat still out"
1482 "standing:last compl time was %d ms.\n",
1483 jiffies_to_msecs(jiffies
1484 - phba->last_completion_time));
1485 mod_timer(&phba->hb_tmofunc,
1486 jiffies +
1487 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1488 }
1489 } else {
1490 mod_timer(&phba->hb_tmofunc,
1491 jiffies +
1492 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1493 }
1494}
1495
1496/**
1497 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1498 * @phba: pointer to lpfc hba data structure.
1499 *
1500 * This routine is called to bring the HBA offline when HBA hardware error
1501 * other than Port Error 6 has been detected.
1502 **/
1503static void
1504lpfc_offline_eratt(struct lpfc_hba *phba)
1505{
1506 struct lpfc_sli *psli = &phba->sli;
1507
1508 spin_lock_irq(&phba->hbalock);
1509 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1510 spin_unlock_irq(&phba->hbalock);
1511 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1512
1513 lpfc_offline(phba);
1514 lpfc_reset_barrier(phba);
1515 spin_lock_irq(&phba->hbalock);
1516 lpfc_sli_brdreset(phba);
1517 spin_unlock_irq(&phba->hbalock);
1518 lpfc_hba_down_post(phba);
1519 lpfc_sli_brdready(phba, HS_MBRDY);
1520 lpfc_unblock_mgmt_io(phba);
1521 phba->link_state = LPFC_HBA_ERROR;
1522 return;
1523}
1524
1525/**
1526 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1527 * @phba: pointer to lpfc hba data structure.
1528 *
1529 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1530 * other than Port Error 6 has been detected.
1531 **/
1532void
1533lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1534{
1535 spin_lock_irq(&phba->hbalock);
1536 phba->link_state = LPFC_HBA_ERROR;
1537 spin_unlock_irq(&phba->hbalock);
1538
1539 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1540 lpfc_offline(phba);
1541 lpfc_hba_down_post(phba);
1542 lpfc_unblock_mgmt_io(phba);
1543}
1544
1545/**
1546 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1547 * @phba: pointer to lpfc hba data structure.
1548 *
1549 * This routine is invoked to handle the deferred HBA hardware error
1550 * conditions. This type of error is indicated by HBA by setting ER1
1551 * and another ER bit in the host status register. The driver will
1552 * wait until the ER1 bit clears before handling the error condition.
1553 **/
1554static void
1555lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1556{
1557 uint32_t old_host_status = phba->work_hs;
1558 struct lpfc_sli *psli = &phba->sli;
1559
1560 /* If the pci channel is offline, ignore possible errors,
1561 * since we cannot communicate with the pci card anyway.
1562 */
1563 if (pci_channel_offline(phba->pcidev)) {
1564 spin_lock_irq(&phba->hbalock);
1565 phba->hba_flag &= ~DEFER_ERATT;
1566 spin_unlock_irq(&phba->hbalock);
1567 return;
1568 }
1569
1570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1571 "0479 Deferred Adapter Hardware Error "
1572 "Data: x%x x%x x%x\n",
1573 phba->work_hs,
1574 phba->work_status[0], phba->work_status[1]);
1575
1576 spin_lock_irq(&phba->hbalock);
1577 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1578 spin_unlock_irq(&phba->hbalock);
1579
1580
1581 /*
1582 * Firmware stops when it triggred erratt. That could cause the I/Os
1583 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1584 * SCSI layer retry it after re-establishing link.
1585 */
1586 lpfc_sli_abort_fcp_rings(phba);
1587
1588 /*
1589 * There was a firmware error. Take the hba offline and then
1590 * attempt to restart it.
1591 */
1592 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
1593 lpfc_offline(phba);
1594
1595 /* Wait for the ER1 bit to clear.*/
1596 while (phba->work_hs & HS_FFER1) {
1597 msleep(100);
1598 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1599 phba->work_hs = UNPLUG_ERR ;
1600 break;
1601 }
1602 /* If driver is unloading let the worker thread continue */
1603 if (phba->pport->load_flag & FC_UNLOADING) {
1604 phba->work_hs = 0;
1605 break;
1606 }
1607 }
1608
1609 /*
1610 * This is to ptrotect against a race condition in which
1611 * first write to the host attention register clear the
1612 * host status register.
1613 */
1614 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1615 phba->work_hs = old_host_status & ~HS_FFER1;
1616
1617 spin_lock_irq(&phba->hbalock);
1618 phba->hba_flag &= ~DEFER_ERATT;
1619 spin_unlock_irq(&phba->hbalock);
1620 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1621 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1622}
1623
1624static void
1625lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1626{
1627 struct lpfc_board_event_header board_event;
1628 struct Scsi_Host *shost;
1629
1630 board_event.event_type = FC_REG_BOARD_EVENT;
1631 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1632 shost = lpfc_shost_from_vport(phba->pport);
1633 fc_host_post_vendor_event(shost, fc_get_event_number(),
1634 sizeof(board_event),
1635 (char *) &board_event,
1636 LPFC_NL_VENDOR_ID);
1637}
1638
1639/**
1640 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1641 * @phba: pointer to lpfc hba data structure.
1642 *
1643 * This routine is invoked to handle the following HBA hardware error
1644 * conditions:
1645 * 1 - HBA error attention interrupt
1646 * 2 - DMA ring index out of range
1647 * 3 - Mailbox command came back as unknown
1648 **/
1649static void
1650lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1651{
1652 struct lpfc_vport *vport = phba->pport;
1653 struct lpfc_sli *psli = &phba->sli;
1654 uint32_t event_data;
1655 unsigned long temperature;
1656 struct temp_event temp_event_data;
1657 struct Scsi_Host *shost;
1658
1659 /* If the pci channel is offline, ignore possible errors,
1660 * since we cannot communicate with the pci card anyway.
1661 */
1662 if (pci_channel_offline(phba->pcidev)) {
1663 spin_lock_irq(&phba->hbalock);
1664 phba->hba_flag &= ~DEFER_ERATT;
1665 spin_unlock_irq(&phba->hbalock);
1666 return;
1667 }
1668
1669 /* If resets are disabled then leave the HBA alone and return */
1670 if (!phba->cfg_enable_hba_reset)
1671 return;
1672
1673 /* Send an internal error event to mgmt application */
1674 lpfc_board_errevt_to_mgmt(phba);
1675
1676 if (phba->hba_flag & DEFER_ERATT)
1677 lpfc_handle_deferred_eratt(phba);
1678
1679 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1680 if (phba->work_hs & HS_FFER6)
1681 /* Re-establishing Link */
1682 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1683 "1301 Re-establishing Link "
1684 "Data: x%x x%x x%x\n",
1685 phba->work_hs, phba->work_status[0],
1686 phba->work_status[1]);
1687 if (phba->work_hs & HS_FFER8)
1688 /* Device Zeroization */
1689 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1690 "2861 Host Authentication device "
1691 "zeroization Data:x%x x%x x%x\n",
1692 phba->work_hs, phba->work_status[0],
1693 phba->work_status[1]);
1694
1695 spin_lock_irq(&phba->hbalock);
1696 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1697 spin_unlock_irq(&phba->hbalock);
1698
1699 /*
1700 * Firmware stops when it triggled erratt with HS_FFER6.
1701 * That could cause the I/Os dropped by the firmware.
1702 * Error iocb (I/O) on txcmplq and let the SCSI layer
1703 * retry it after re-establishing link.
1704 */
1705 lpfc_sli_abort_fcp_rings(phba);
1706
1707 /*
1708 * There was a firmware error. Take the hba offline and then
1709 * attempt to restart it.
1710 */
1711 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1712 lpfc_offline(phba);
1713 lpfc_sli_brdrestart(phba);
1714 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
1715 lpfc_unblock_mgmt_io(phba);
1716 return;
1717 }
1718 lpfc_unblock_mgmt_io(phba);
1719 } else if (phba->work_hs & HS_CRIT_TEMP) {
1720 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1721 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1722 temp_event_data.event_code = LPFC_CRIT_TEMP;
1723 temp_event_data.data = (uint32_t)temperature;
1724
1725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1726 "0406 Adapter maximum temperature exceeded "
1727 "(%ld), taking this port offline "
1728 "Data: x%x x%x x%x\n",
1729 temperature, phba->work_hs,
1730 phba->work_status[0], phba->work_status[1]);
1731
1732 shost = lpfc_shost_from_vport(phba->pport);
1733 fc_host_post_vendor_event(shost, fc_get_event_number(),
1734 sizeof(temp_event_data),
1735 (char *) &temp_event_data,
1736 SCSI_NL_VID_TYPE_PCI
1737 | PCI_VENDOR_ID_EMULEX);
1738
1739 spin_lock_irq(&phba->hbalock);
1740 phba->over_temp_state = HBA_OVER_TEMP;
1741 spin_unlock_irq(&phba->hbalock);
1742 lpfc_offline_eratt(phba);
1743
1744 } else {
1745 /* The if clause above forces this code path when the status
1746 * failure is a value other than FFER6. Do not call the offline
1747 * twice. This is the adapter hardware error path.
1748 */
1749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1750 "0457 Adapter Hardware Error "
1751 "Data: x%x x%x x%x\n",
1752 phba->work_hs,
1753 phba->work_status[0], phba->work_status[1]);
1754
1755 event_data = FC_REG_DUMP_EVENT;
1756 shost = lpfc_shost_from_vport(vport);
1757 fc_host_post_vendor_event(shost, fc_get_event_number(),
1758 sizeof(event_data), (char *) &event_data,
1759 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1760
1761 lpfc_offline_eratt(phba);
1762 }
1763 return;
1764}
1765
1766/**
1767 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1768 * @phba: pointer to lpfc hba data structure.
1769 * @mbx_action: flag for mailbox shutdown action.
1770 *
1771 * This routine is invoked to perform an SLI4 port PCI function reset in
1772 * response to port status register polling attention. It waits for port
1773 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1774 * During this process, interrupt vectors are freed and later requested
1775 * for handling possible port resource change.
1776 **/
1777static int
1778lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1779 bool en_rn_msg)
1780{
1781 int rc;
1782 uint32_t intr_mode;
1783
1784 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
1785 LPFC_SLI_INTF_IF_TYPE_2) {
1786 /*
1787 * On error status condition, driver need to wait for port
1788 * ready before performing reset.
1789 */
1790 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1791 if (rc)
1792 return rc;
1793 }
1794
1795 /* need reset: attempt for port recovery */
1796 if (en_rn_msg)
1797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1798 "2887 Reset Needed: Attempting Port "
1799 "Recovery...\n");
1800 lpfc_offline_prep(phba, mbx_action);
1801 lpfc_offline(phba);
1802 /* release interrupt for possible resource change */
1803 lpfc_sli4_disable_intr(phba);
1804 lpfc_sli_brdrestart(phba);
1805 /* request and enable interrupt */
1806 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1807 if (intr_mode == LPFC_INTR_ERROR) {
1808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1809 "3175 Failed to enable interrupt\n");
1810 return -EIO;
1811 }
1812 phba->intr_mode = intr_mode;
1813 rc = lpfc_online(phba);
1814 if (rc == 0)
1815 lpfc_unblock_mgmt_io(phba);
1816
1817 return rc;
1818}
1819
1820/**
1821 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1822 * @phba: pointer to lpfc hba data structure.
1823 *
1824 * This routine is invoked to handle the SLI4 HBA hardware error attention
1825 * conditions.
1826 **/
1827static void
1828lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1829{
1830 struct lpfc_vport *vport = phba->pport;
1831 uint32_t event_data;
1832 struct Scsi_Host *shost;
1833 uint32_t if_type;
1834 struct lpfc_register portstat_reg = {0};
1835 uint32_t reg_err1, reg_err2;
1836 uint32_t uerrlo_reg, uemasklo_reg;
1837 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
1838 bool en_rn_msg = true;
1839 struct temp_event temp_event_data;
1840 struct lpfc_register portsmphr_reg;
1841 int rc, i;
1842
1843 /* If the pci channel is offline, ignore possible errors, since
1844 * we cannot communicate with the pci card anyway.
1845 */
1846 if (pci_channel_offline(phba->pcidev))
1847 return;
1848
1849 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
1850 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1851 switch (if_type) {
1852 case LPFC_SLI_INTF_IF_TYPE_0:
1853 pci_rd_rc1 = lpfc_readl(
1854 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1855 &uerrlo_reg);
1856 pci_rd_rc2 = lpfc_readl(
1857 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1858 &uemasklo_reg);
1859 /* consider PCI bus read error as pci_channel_offline */
1860 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1861 return;
1862 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1863 lpfc_sli4_offline_eratt(phba);
1864 return;
1865 }
1866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1867 "7623 Checking UE recoverable");
1868
1869 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1870 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1871 &portsmphr_reg.word0))
1872 continue;
1873
1874 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1875 &portsmphr_reg);
1876 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1877 LPFC_PORT_SEM_UE_RECOVERABLE)
1878 break;
1879 /*Sleep for 1Sec, before checking SEMAPHORE */
1880 msleep(1000);
1881 }
1882
1883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1884 "4827 smphr_port_status x%x : Waited %dSec",
1885 smphr_port_status, i);
1886
1887 /* Recoverable UE, reset the HBA device */
1888 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1889 LPFC_PORT_SEM_UE_RECOVERABLE) {
1890 for (i = 0; i < 20; i++) {
1891 msleep(1000);
1892 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1893 &portsmphr_reg.word0) &&
1894 (LPFC_POST_STAGE_PORT_READY ==
1895 bf_get(lpfc_port_smphr_port_status,
1896 &portsmphr_reg))) {
1897 rc = lpfc_sli4_port_sta_fn_reset(phba,
1898 LPFC_MBX_NO_WAIT, en_rn_msg);
1899 if (rc == 0)
1900 return;
1901 lpfc_printf_log(phba,
1902 KERN_ERR, LOG_INIT,
1903 "4215 Failed to recover UE");
1904 break;
1905 }
1906 }
1907 }
1908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1909 "7624 Firmware not ready: Failing UE recovery,"
1910 " waited %dSec", i);
1911 lpfc_sli4_offline_eratt(phba);
1912 break;
1913
1914 case LPFC_SLI_INTF_IF_TYPE_2:
1915 case LPFC_SLI_INTF_IF_TYPE_6:
1916 pci_rd_rc1 = lpfc_readl(
1917 phba->sli4_hba.u.if_type2.STATUSregaddr,
1918 &portstat_reg.word0);
1919 /* consider PCI bus read error as pci_channel_offline */
1920 if (pci_rd_rc1 == -EIO) {
1921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1922 "3151 PCI bus read access failure: x%x\n",
1923 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
1924 return;
1925 }
1926 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1927 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
1928 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1930 "2889 Port Overtemperature event, "
1931 "taking port offline Data: x%x x%x\n",
1932 reg_err1, reg_err2);
1933
1934 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
1935 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1936 temp_event_data.event_code = LPFC_CRIT_TEMP;
1937 temp_event_data.data = 0xFFFFFFFF;
1938
1939 shost = lpfc_shost_from_vport(phba->pport);
1940 fc_host_post_vendor_event(shost, fc_get_event_number(),
1941 sizeof(temp_event_data),
1942 (char *)&temp_event_data,
1943 SCSI_NL_VID_TYPE_PCI
1944 | PCI_VENDOR_ID_EMULEX);
1945
1946 spin_lock_irq(&phba->hbalock);
1947 phba->over_temp_state = HBA_OVER_TEMP;
1948 spin_unlock_irq(&phba->hbalock);
1949 lpfc_sli4_offline_eratt(phba);
1950 return;
1951 }
1952 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1953 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
1954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1955 "3143 Port Down: Firmware Update "
1956 "Detected\n");
1957 en_rn_msg = false;
1958 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1959 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1960 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1961 "3144 Port Down: Debug Dump\n");
1962 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1963 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1965 "3145 Port Down: Provisioning\n");
1966
1967 /* If resets are disabled then leave the HBA alone and return */
1968 if (!phba->cfg_enable_hba_reset)
1969 return;
1970
1971 /* Check port status register for function reset */
1972 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1973 en_rn_msg);
1974 if (rc == 0) {
1975 /* don't report event on forced debug dump */
1976 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1977 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1978 return;
1979 else
1980 break;
1981 }
1982 /* fall through for not able to recover */
1983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1984 "3152 Unrecoverable error, bring the port "
1985 "offline\n");
1986 lpfc_sli4_offline_eratt(phba);
1987 break;
1988 case LPFC_SLI_INTF_IF_TYPE_1:
1989 default:
1990 break;
1991 }
1992 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1993 "3123 Report dump event to upper layer\n");
1994 /* Send an internal error event to mgmt application */
1995 lpfc_board_errevt_to_mgmt(phba);
1996
1997 event_data = FC_REG_DUMP_EVENT;
1998 shost = lpfc_shost_from_vport(vport);
1999 fc_host_post_vendor_event(shost, fc_get_event_number(),
2000 sizeof(event_data), (char *) &event_data,
2001 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
2002}
2003
2004/**
2005 * lpfc_handle_eratt - Wrapper func for handling hba error attention
2006 * @phba: pointer to lpfc HBA data structure.
2007 *
2008 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2009 * routine from the API jump table function pointer from the lpfc_hba struct.
2010 *
2011 * Return codes
2012 * 0 - success.
2013 * Any other value - error.
2014 **/
2015void
2016lpfc_handle_eratt(struct lpfc_hba *phba)
2017{
2018 (*phba->lpfc_handle_eratt)(phba);
2019}
2020
2021/**
2022 * lpfc_handle_latt - The HBA link event handler
2023 * @phba: pointer to lpfc hba data structure.
2024 *
2025 * This routine is invoked from the worker thread to handle a HBA host
2026 * attention link event. SLI3 only.
2027 **/
2028void
2029lpfc_handle_latt(struct lpfc_hba *phba)
2030{
2031 struct lpfc_vport *vport = phba->pport;
2032 struct lpfc_sli *psli = &phba->sli;
2033 LPFC_MBOXQ_t *pmb;
2034 volatile uint32_t control;
2035 struct lpfc_dmabuf *mp;
2036 int rc = 0;
2037
2038 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2039 if (!pmb) {
2040 rc = 1;
2041 goto lpfc_handle_latt_err_exit;
2042 }
2043
2044 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2045 if (!mp) {
2046 rc = 2;
2047 goto lpfc_handle_latt_free_pmb;
2048 }
2049
2050 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2051 if (!mp->virt) {
2052 rc = 3;
2053 goto lpfc_handle_latt_free_mp;
2054 }
2055
2056 /* Cleanup any outstanding ELS commands */
2057 lpfc_els_flush_all_cmd(phba);
2058
2059 psli->slistat.link_event++;
2060 lpfc_read_topology(phba, pmb, mp);
2061 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
2062 pmb->vport = vport;
2063 /* Block ELS IOCBs until we have processed this mbox command */
2064 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2065 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
2066 if (rc == MBX_NOT_FINISHED) {
2067 rc = 4;
2068 goto lpfc_handle_latt_free_mbuf;
2069 }
2070
2071 /* Clear Link Attention in HA REG */
2072 spin_lock_irq(&phba->hbalock);
2073 writel(HA_LATT, phba->HAregaddr);
2074 readl(phba->HAregaddr); /* flush */
2075 spin_unlock_irq(&phba->hbalock);
2076
2077 return;
2078
2079lpfc_handle_latt_free_mbuf:
2080 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
2081 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2082lpfc_handle_latt_free_mp:
2083 kfree(mp);
2084lpfc_handle_latt_free_pmb:
2085 mempool_free(pmb, phba->mbox_mem_pool);
2086lpfc_handle_latt_err_exit:
2087 /* Enable Link attention interrupts */
2088 spin_lock_irq(&phba->hbalock);
2089 psli->sli_flag |= LPFC_PROCESS_LA;
2090 control = readl(phba->HCregaddr);
2091 control |= HC_LAINT_ENA;
2092 writel(control, phba->HCregaddr);
2093 readl(phba->HCregaddr); /* flush */
2094
2095 /* Clear Link Attention in HA REG */
2096 writel(HA_LATT, phba->HAregaddr);
2097 readl(phba->HAregaddr); /* flush */
2098 spin_unlock_irq(&phba->hbalock);
2099 lpfc_linkdown(phba);
2100 phba->link_state = LPFC_HBA_ERROR;
2101
2102 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2103 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
2104
2105 return;
2106}
2107
2108/**
2109 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
2110 * @phba: pointer to lpfc hba data structure.
2111 * @vpd: pointer to the vital product data.
2112 * @len: length of the vital product data in bytes.
2113 *
2114 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2115 * an array of characters. In this routine, the ModelName, ProgramType, and
2116 * ModelDesc, etc. fields of the phba data structure will be populated.
2117 *
2118 * Return codes
2119 * 0 - pointer to the VPD passed in is NULL
2120 * 1 - success
2121 **/
2122int
2123lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
2124{
2125 uint8_t lenlo, lenhi;
2126 int Length;
2127 int i, j;
2128 int finished = 0;
2129 int index = 0;
2130
2131 if (!vpd)
2132 return 0;
2133
2134 /* Vital Product */
2135 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2136 "0455 Vital Product Data: x%x x%x x%x x%x\n",
2137 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2138 (uint32_t) vpd[3]);
2139 while (!finished && (index < (len - 4))) {
2140 switch (vpd[index]) {
2141 case 0x82:
2142 case 0x91:
2143 index += 1;
2144 lenlo = vpd[index];
2145 index += 1;
2146 lenhi = vpd[index];
2147 index += 1;
2148 i = ((((unsigned short)lenhi) << 8) + lenlo);
2149 index += i;
2150 break;
2151 case 0x90:
2152 index += 1;
2153 lenlo = vpd[index];
2154 index += 1;
2155 lenhi = vpd[index];
2156 index += 1;
2157 Length = ((((unsigned short)lenhi) << 8) + lenlo);
2158 if (Length > len - index)
2159 Length = len - index;
2160 while (Length > 0) {
2161 /* Look for Serial Number */
2162 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2163 index += 2;
2164 i = vpd[index];
2165 index += 1;
2166 j = 0;
2167 Length -= (3+i);
2168 while(i--) {
2169 phba->SerialNumber[j++] = vpd[index++];
2170 if (j == 31)
2171 break;
2172 }
2173 phba->SerialNumber[j] = 0;
2174 continue;
2175 }
2176 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2177 phba->vpd_flag |= VPD_MODEL_DESC;
2178 index += 2;
2179 i = vpd[index];
2180 index += 1;
2181 j = 0;
2182 Length -= (3+i);
2183 while(i--) {
2184 phba->ModelDesc[j++] = vpd[index++];
2185 if (j == 255)
2186 break;
2187 }
2188 phba->ModelDesc[j] = 0;
2189 continue;
2190 }
2191 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2192 phba->vpd_flag |= VPD_MODEL_NAME;
2193 index += 2;
2194 i = vpd[index];
2195 index += 1;
2196 j = 0;
2197 Length -= (3+i);
2198 while(i--) {
2199 phba->ModelName[j++] = vpd[index++];
2200 if (j == 79)
2201 break;
2202 }
2203 phba->ModelName[j] = 0;
2204 continue;
2205 }
2206 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2207 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2208 index += 2;
2209 i = vpd[index];
2210 index += 1;
2211 j = 0;
2212 Length -= (3+i);
2213 while(i--) {
2214 phba->ProgramType[j++] = vpd[index++];
2215 if (j == 255)
2216 break;
2217 }
2218 phba->ProgramType[j] = 0;
2219 continue;
2220 }
2221 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2222 phba->vpd_flag |= VPD_PORT;
2223 index += 2;
2224 i = vpd[index];
2225 index += 1;
2226 j = 0;
2227 Length -= (3+i);
2228 while(i--) {
2229 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2230 (phba->sli4_hba.pport_name_sta ==
2231 LPFC_SLI4_PPNAME_GET)) {
2232 j++;
2233 index++;
2234 } else
2235 phba->Port[j++] = vpd[index++];
2236 if (j == 19)
2237 break;
2238 }
2239 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2240 (phba->sli4_hba.pport_name_sta ==
2241 LPFC_SLI4_PPNAME_NON))
2242 phba->Port[j] = 0;
2243 continue;
2244 }
2245 else {
2246 index += 2;
2247 i = vpd[index];
2248 index += 1;
2249 index += i;
2250 Length -= (3 + i);
2251 }
2252 }
2253 finished = 0;
2254 break;
2255 case 0x78:
2256 finished = 1;
2257 break;
2258 default:
2259 index ++;
2260 break;
2261 }
2262 }
2263
2264 return(1);
2265}
2266
2267/**
2268 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
2269 * @phba: pointer to lpfc hba data structure.
2270 * @mdp: pointer to the data structure to hold the derived model name.
2271 * @descp: pointer to the data structure to hold the derived description.
2272 *
2273 * This routine retrieves HBA's description based on its registered PCI device
2274 * ID. The @descp passed into this function points to an array of 256 chars. It
2275 * shall be returned with the model name, maximum speed, and the host bus type.
2276 * The @mdp passed into this function points to an array of 80 chars. When the
2277 * function returns, the @mdp will be filled with the model name.
2278 **/
2279static void
2280lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
2281{
2282 lpfc_vpd_t *vp;
2283 uint16_t dev_id = phba->pcidev->device;
2284 int max_speed;
2285 int GE = 0;
2286 int oneConnect = 0; /* default is not a oneConnect */
2287 struct {
2288 char *name;
2289 char *bus;
2290 char *function;
2291 } m = {"<Unknown>", "", ""};
2292
2293 if (mdp && mdp[0] != '\0'
2294 && descp && descp[0] != '\0')
2295 return;
2296
2297 if (phba->lmt & LMT_64Gb)
2298 max_speed = 64;
2299 else if (phba->lmt & LMT_32Gb)
2300 max_speed = 32;
2301 else if (phba->lmt & LMT_16Gb)
2302 max_speed = 16;
2303 else if (phba->lmt & LMT_10Gb)
2304 max_speed = 10;
2305 else if (phba->lmt & LMT_8Gb)
2306 max_speed = 8;
2307 else if (phba->lmt & LMT_4Gb)
2308 max_speed = 4;
2309 else if (phba->lmt & LMT_2Gb)
2310 max_speed = 2;
2311 else if (phba->lmt & LMT_1Gb)
2312 max_speed = 1;
2313 else
2314 max_speed = 0;
2315
2316 vp = &phba->vpd;
2317
2318 switch (dev_id) {
2319 case PCI_DEVICE_ID_FIREFLY:
2320 m = (typeof(m)){"LP6000", "PCI",
2321 "Obsolete, Unsupported Fibre Channel Adapter"};
2322 break;
2323 case PCI_DEVICE_ID_SUPERFLY:
2324 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
2325 m = (typeof(m)){"LP7000", "PCI", ""};
2326 else
2327 m = (typeof(m)){"LP7000E", "PCI", ""};
2328 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
2329 break;
2330 case PCI_DEVICE_ID_DRAGONFLY:
2331 m = (typeof(m)){"LP8000", "PCI",
2332 "Obsolete, Unsupported Fibre Channel Adapter"};
2333 break;
2334 case PCI_DEVICE_ID_CENTAUR:
2335 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
2336 m = (typeof(m)){"LP9002", "PCI", ""};
2337 else
2338 m = (typeof(m)){"LP9000", "PCI", ""};
2339 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
2340 break;
2341 case PCI_DEVICE_ID_RFLY:
2342 m = (typeof(m)){"LP952", "PCI",
2343 "Obsolete, Unsupported Fibre Channel Adapter"};
2344 break;
2345 case PCI_DEVICE_ID_PEGASUS:
2346 m = (typeof(m)){"LP9802", "PCI-X",
2347 "Obsolete, Unsupported Fibre Channel Adapter"};
2348 break;
2349 case PCI_DEVICE_ID_THOR:
2350 m = (typeof(m)){"LP10000", "PCI-X",
2351 "Obsolete, Unsupported Fibre Channel Adapter"};
2352 break;
2353 case PCI_DEVICE_ID_VIPER:
2354 m = (typeof(m)){"LPX1000", "PCI-X",
2355 "Obsolete, Unsupported Fibre Channel Adapter"};
2356 break;
2357 case PCI_DEVICE_ID_PFLY:
2358 m = (typeof(m)){"LP982", "PCI-X",
2359 "Obsolete, Unsupported Fibre Channel Adapter"};
2360 break;
2361 case PCI_DEVICE_ID_TFLY:
2362 m = (typeof(m)){"LP1050", "PCI-X",
2363 "Obsolete, Unsupported Fibre Channel Adapter"};
2364 break;
2365 case PCI_DEVICE_ID_HELIOS:
2366 m = (typeof(m)){"LP11000", "PCI-X2",
2367 "Obsolete, Unsupported Fibre Channel Adapter"};
2368 break;
2369 case PCI_DEVICE_ID_HELIOS_SCSP:
2370 m = (typeof(m)){"LP11000-SP", "PCI-X2",
2371 "Obsolete, Unsupported Fibre Channel Adapter"};
2372 break;
2373 case PCI_DEVICE_ID_HELIOS_DCSP:
2374 m = (typeof(m)){"LP11002-SP", "PCI-X2",
2375 "Obsolete, Unsupported Fibre Channel Adapter"};
2376 break;
2377 case PCI_DEVICE_ID_NEPTUNE:
2378 m = (typeof(m)){"LPe1000", "PCIe",
2379 "Obsolete, Unsupported Fibre Channel Adapter"};
2380 break;
2381 case PCI_DEVICE_ID_NEPTUNE_SCSP:
2382 m = (typeof(m)){"LPe1000-SP", "PCIe",
2383 "Obsolete, Unsupported Fibre Channel Adapter"};
2384 break;
2385 case PCI_DEVICE_ID_NEPTUNE_DCSP:
2386 m = (typeof(m)){"LPe1002-SP", "PCIe",
2387 "Obsolete, Unsupported Fibre Channel Adapter"};
2388 break;
2389 case PCI_DEVICE_ID_BMID:
2390 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
2391 break;
2392 case PCI_DEVICE_ID_BSMB:
2393 m = (typeof(m)){"LP111", "PCI-X2",
2394 "Obsolete, Unsupported Fibre Channel Adapter"};
2395 break;
2396 case PCI_DEVICE_ID_ZEPHYR:
2397 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
2398 break;
2399 case PCI_DEVICE_ID_ZEPHYR_SCSP:
2400 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
2401 break;
2402 case PCI_DEVICE_ID_ZEPHYR_DCSP:
2403 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
2404 GE = 1;
2405 break;
2406 case PCI_DEVICE_ID_ZMID:
2407 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
2408 break;
2409 case PCI_DEVICE_ID_ZSMB:
2410 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
2411 break;
2412 case PCI_DEVICE_ID_LP101:
2413 m = (typeof(m)){"LP101", "PCI-X",
2414 "Obsolete, Unsupported Fibre Channel Adapter"};
2415 break;
2416 case PCI_DEVICE_ID_LP10000S:
2417 m = (typeof(m)){"LP10000-S", "PCI",
2418 "Obsolete, Unsupported Fibre Channel Adapter"};
2419 break;
2420 case PCI_DEVICE_ID_LP11000S:
2421 m = (typeof(m)){"LP11000-S", "PCI-X2",
2422 "Obsolete, Unsupported Fibre Channel Adapter"};
2423 break;
2424 case PCI_DEVICE_ID_LPE11000S:
2425 m = (typeof(m)){"LPe11000-S", "PCIe",
2426 "Obsolete, Unsupported Fibre Channel Adapter"};
2427 break;
2428 case PCI_DEVICE_ID_SAT:
2429 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
2430 break;
2431 case PCI_DEVICE_ID_SAT_MID:
2432 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
2433 break;
2434 case PCI_DEVICE_ID_SAT_SMB:
2435 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
2436 break;
2437 case PCI_DEVICE_ID_SAT_DCSP:
2438 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
2439 break;
2440 case PCI_DEVICE_ID_SAT_SCSP:
2441 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
2442 break;
2443 case PCI_DEVICE_ID_SAT_S:
2444 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
2445 break;
2446 case PCI_DEVICE_ID_HORNET:
2447 m = (typeof(m)){"LP21000", "PCIe",
2448 "Obsolete, Unsupported FCoE Adapter"};
2449 GE = 1;
2450 break;
2451 case PCI_DEVICE_ID_PROTEUS_VF:
2452 m = (typeof(m)){"LPev12000", "PCIe IOV",
2453 "Obsolete, Unsupported Fibre Channel Adapter"};
2454 break;
2455 case PCI_DEVICE_ID_PROTEUS_PF:
2456 m = (typeof(m)){"LPev12000", "PCIe IOV",
2457 "Obsolete, Unsupported Fibre Channel Adapter"};
2458 break;
2459 case PCI_DEVICE_ID_PROTEUS_S:
2460 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2461 "Obsolete, Unsupported Fibre Channel Adapter"};
2462 break;
2463 case PCI_DEVICE_ID_TIGERSHARK:
2464 oneConnect = 1;
2465 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
2466 break;
2467 case PCI_DEVICE_ID_TOMCAT:
2468 oneConnect = 1;
2469 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2470 break;
2471 case PCI_DEVICE_ID_FALCON:
2472 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2473 "EmulexSecure Fibre"};
2474 break;
2475 case PCI_DEVICE_ID_BALIUS:
2476 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2477 "Obsolete, Unsupported Fibre Channel Adapter"};
2478 break;
2479 case PCI_DEVICE_ID_LANCER_FC:
2480 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
2481 break;
2482 case PCI_DEVICE_ID_LANCER_FC_VF:
2483 m = (typeof(m)){"LPe16000", "PCIe",
2484 "Obsolete, Unsupported Fibre Channel Adapter"};
2485 break;
2486 case PCI_DEVICE_ID_LANCER_FCOE:
2487 oneConnect = 1;
2488 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
2489 break;
2490 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2491 oneConnect = 1;
2492 m = (typeof(m)){"OCe15100", "PCIe",
2493 "Obsolete, Unsupported FCoE"};
2494 break;
2495 case PCI_DEVICE_ID_LANCER_G6_FC:
2496 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2497 break;
2498 case PCI_DEVICE_ID_LANCER_G7_FC:
2499 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2500 break;
2501 case PCI_DEVICE_ID_SKYHAWK:
2502 case PCI_DEVICE_ID_SKYHAWK_VF:
2503 oneConnect = 1;
2504 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2505 break;
2506 default:
2507 m = (typeof(m)){"Unknown", "", ""};
2508 break;
2509 }
2510
2511 if (mdp && mdp[0] == '\0')
2512 snprintf(mdp, 79,"%s", m.name);
2513 /*
2514 * oneConnect hba requires special processing, they are all initiators
2515 * and we put the port number on the end
2516 */
2517 if (descp && descp[0] == '\0') {
2518 if (oneConnect)
2519 snprintf(descp, 255,
2520 "Emulex OneConnect %s, %s Initiator %s",
2521 m.name, m.function,
2522 phba->Port);
2523 else if (max_speed == 0)
2524 snprintf(descp, 255,
2525 "Emulex %s %s %s",
2526 m.name, m.bus, m.function);
2527 else
2528 snprintf(descp, 255,
2529 "Emulex %s %d%s %s %s",
2530 m.name, max_speed, (GE) ? "GE" : "Gb",
2531 m.bus, m.function);
2532 }
2533}
2534
2535/**
2536 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
2537 * @phba: pointer to lpfc hba data structure.
2538 * @pring: pointer to a IOCB ring.
2539 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2540 *
2541 * This routine posts a given number of IOCBs with the associated DMA buffer
2542 * descriptors specified by the cnt argument to the given IOCB ring.
2543 *
2544 * Return codes
2545 * The number of IOCBs NOT able to be posted to the IOCB ring.
2546 **/
2547int
2548lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
2549{
2550 IOCB_t *icmd;
2551 struct lpfc_iocbq *iocb;
2552 struct lpfc_dmabuf *mp1, *mp2;
2553
2554 cnt += pring->missbufcnt;
2555
2556 /* While there are buffers to post */
2557 while (cnt > 0) {
2558 /* Allocate buffer for command iocb */
2559 iocb = lpfc_sli_get_iocbq(phba);
2560 if (iocb == NULL) {
2561 pring->missbufcnt = cnt;
2562 return cnt;
2563 }
2564 icmd = &iocb->iocb;
2565
2566 /* 2 buffers can be posted per command */
2567 /* Allocate buffer to post */
2568 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2569 if (mp1)
2570 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2571 if (!mp1 || !mp1->virt) {
2572 kfree(mp1);
2573 lpfc_sli_release_iocbq(phba, iocb);
2574 pring->missbufcnt = cnt;
2575 return cnt;
2576 }
2577
2578 INIT_LIST_HEAD(&mp1->list);
2579 /* Allocate buffer to post */
2580 if (cnt > 1) {
2581 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2582 if (mp2)
2583 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2584 &mp2->phys);
2585 if (!mp2 || !mp2->virt) {
2586 kfree(mp2);
2587 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2588 kfree(mp1);
2589 lpfc_sli_release_iocbq(phba, iocb);
2590 pring->missbufcnt = cnt;
2591 return cnt;
2592 }
2593
2594 INIT_LIST_HEAD(&mp2->list);
2595 } else {
2596 mp2 = NULL;
2597 }
2598
2599 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2600 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2601 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2602 icmd->ulpBdeCount = 1;
2603 cnt--;
2604 if (mp2) {
2605 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2606 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2607 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2608 cnt--;
2609 icmd->ulpBdeCount = 2;
2610 }
2611
2612 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2613 icmd->ulpLe = 1;
2614
2615 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2616 IOCB_ERROR) {
2617 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2618 kfree(mp1);
2619 cnt++;
2620 if (mp2) {
2621 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2622 kfree(mp2);
2623 cnt++;
2624 }
2625 lpfc_sli_release_iocbq(phba, iocb);
2626 pring->missbufcnt = cnt;
2627 return cnt;
2628 }
2629 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
2630 if (mp2)
2631 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
2632 }
2633 pring->missbufcnt = 0;
2634 return 0;
2635}
2636
2637/**
2638 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
2639 * @phba: pointer to lpfc hba data structure.
2640 *
2641 * This routine posts initial receive IOCB buffers to the ELS ring. The
2642 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2643 * set to 64 IOCBs. SLI3 only.
2644 *
2645 * Return codes
2646 * 0 - success (currently always success)
2647 **/
2648static int
2649lpfc_post_rcv_buf(struct lpfc_hba *phba)
2650{
2651 struct lpfc_sli *psli = &phba->sli;
2652
2653 /* Ring 0, ELS / CT buffers */
2654 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
2655 /* Ring 2 - FCP no buffers needed */
2656
2657 return 0;
2658}
2659
2660#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2661
2662/**
2663 * lpfc_sha_init - Set up initial array of hash table entries
2664 * @HashResultPointer: pointer to an array as hash table.
2665 *
2666 * This routine sets up the initial values to the array of hash table entries
2667 * for the LC HBAs.
2668 **/
2669static void
2670lpfc_sha_init(uint32_t * HashResultPointer)
2671{
2672 HashResultPointer[0] = 0x67452301;
2673 HashResultPointer[1] = 0xEFCDAB89;
2674 HashResultPointer[2] = 0x98BADCFE;
2675 HashResultPointer[3] = 0x10325476;
2676 HashResultPointer[4] = 0xC3D2E1F0;
2677}
2678
2679/**
2680 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2681 * @HashResultPointer: pointer to an initial/result hash table.
2682 * @HashWorkingPointer: pointer to an working hash table.
2683 *
2684 * This routine iterates an initial hash table pointed by @HashResultPointer
2685 * with the values from the working hash table pointeed by @HashWorkingPointer.
2686 * The results are putting back to the initial hash table, returned through
2687 * the @HashResultPointer as the result hash table.
2688 **/
2689static void
2690lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2691{
2692 int t;
2693 uint32_t TEMP;
2694 uint32_t A, B, C, D, E;
2695 t = 16;
2696 do {
2697 HashWorkingPointer[t] =
2698 S(1,
2699 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2700 8] ^
2701 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2702 } while (++t <= 79);
2703 t = 0;
2704 A = HashResultPointer[0];
2705 B = HashResultPointer[1];
2706 C = HashResultPointer[2];
2707 D = HashResultPointer[3];
2708 E = HashResultPointer[4];
2709
2710 do {
2711 if (t < 20) {
2712 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2713 } else if (t < 40) {
2714 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2715 } else if (t < 60) {
2716 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2717 } else {
2718 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2719 }
2720 TEMP += S(5, A) + E + HashWorkingPointer[t];
2721 E = D;
2722 D = C;
2723 C = S(30, B);
2724 B = A;
2725 A = TEMP;
2726 } while (++t <= 79);
2727
2728 HashResultPointer[0] += A;
2729 HashResultPointer[1] += B;
2730 HashResultPointer[2] += C;
2731 HashResultPointer[3] += D;
2732 HashResultPointer[4] += E;
2733
2734}
2735
2736/**
2737 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2738 * @RandomChallenge: pointer to the entry of host challenge random number array.
2739 * @HashWorking: pointer to the entry of the working hash array.
2740 *
2741 * This routine calculates the working hash array referred by @HashWorking
2742 * from the challenge random numbers associated with the host, referred by
2743 * @RandomChallenge. The result is put into the entry of the working hash
2744 * array and returned by reference through @HashWorking.
2745 **/
2746static void
2747lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2748{
2749 *HashWorking = (*RandomChallenge ^ *HashWorking);
2750}
2751
2752/**
2753 * lpfc_hba_init - Perform special handling for LC HBA initialization
2754 * @phba: pointer to lpfc hba data structure.
2755 * @hbainit: pointer to an array of unsigned 32-bit integers.
2756 *
2757 * This routine performs the special handling for LC HBA initialization.
2758 **/
2759void
2760lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2761{
2762 int t;
2763 uint32_t *HashWorking;
2764 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2765
2766 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2767 if (!HashWorking)
2768 return;
2769
2770 HashWorking[0] = HashWorking[78] = *pwwnn++;
2771 HashWorking[1] = HashWorking[79] = *pwwnn;
2772
2773 for (t = 0; t < 7; t++)
2774 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2775
2776 lpfc_sha_init(hbainit);
2777 lpfc_sha_iterate(hbainit, HashWorking);
2778 kfree(HashWorking);
2779}
2780
2781/**
2782 * lpfc_cleanup - Performs vport cleanups before deleting a vport
2783 * @vport: pointer to a virtual N_Port data structure.
2784 *
2785 * This routine performs the necessary cleanups before deleting the @vport.
2786 * It invokes the discovery state machine to perform necessary state
2787 * transitions and to release the ndlps associated with the @vport. Note,
2788 * the physical port is treated as @vport 0.
2789 **/
2790void
2791lpfc_cleanup(struct lpfc_vport *vport)
2792{
2793 struct lpfc_hba *phba = vport->phba;
2794 struct lpfc_nodelist *ndlp, *next_ndlp;
2795 int i = 0;
2796
2797 if (phba->link_state > LPFC_LINK_DOWN)
2798 lpfc_port_link_failure(vport);
2799
2800 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2801 if (!NLP_CHK_NODE_ACT(ndlp)) {
2802 ndlp = lpfc_enable_node(vport, ndlp,
2803 NLP_STE_UNUSED_NODE);
2804 if (!ndlp)
2805 continue;
2806 spin_lock_irq(&phba->ndlp_lock);
2807 NLP_SET_FREE_REQ(ndlp);
2808 spin_unlock_irq(&phba->ndlp_lock);
2809 /* Trigger the release of the ndlp memory */
2810 lpfc_nlp_put(ndlp);
2811 continue;
2812 }
2813 spin_lock_irq(&phba->ndlp_lock);
2814 if (NLP_CHK_FREE_REQ(ndlp)) {
2815 /* The ndlp should not be in memory free mode already */
2816 spin_unlock_irq(&phba->ndlp_lock);
2817 continue;
2818 } else
2819 /* Indicate request for freeing ndlp memory */
2820 NLP_SET_FREE_REQ(ndlp);
2821 spin_unlock_irq(&phba->ndlp_lock);
2822
2823 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2824 ndlp->nlp_DID == Fabric_DID) {
2825 /* Just free up ndlp with Fabric_DID for vports */
2826 lpfc_nlp_put(ndlp);
2827 continue;
2828 }
2829
2830 /* take care of nodes in unused state before the state
2831 * machine taking action.
2832 */
2833 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2834 lpfc_nlp_put(ndlp);
2835 continue;
2836 }
2837
2838 if (ndlp->nlp_type & NLP_FABRIC)
2839 lpfc_disc_state_machine(vport, ndlp, NULL,
2840 NLP_EVT_DEVICE_RECOVERY);
2841
2842 lpfc_disc_state_machine(vport, ndlp, NULL,
2843 NLP_EVT_DEVICE_RM);
2844 }
2845
2846 /* At this point, ALL ndlp's should be gone
2847 * because of the previous NLP_EVT_DEVICE_RM.
2848 * Lets wait for this to happen, if needed.
2849 */
2850 while (!list_empty(&vport->fc_nodes)) {
2851 if (i++ > 3000) {
2852 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2853 "0233 Nodelist not empty\n");
2854 list_for_each_entry_safe(ndlp, next_ndlp,
2855 &vport->fc_nodes, nlp_listp) {
2856 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2857 LOG_NODE,
2858 "0282 did:x%x ndlp:x%p "
2859 "usgmap:x%x refcnt:%d\n",
2860 ndlp->nlp_DID, (void *)ndlp,
2861 ndlp->nlp_usg_map,
2862 kref_read(&ndlp->kref));
2863 }
2864 break;
2865 }
2866
2867 /* Wait for any activity on ndlps to settle */
2868 msleep(10);
2869 }
2870 lpfc_cleanup_vports_rrqs(vport, NULL);
2871}
2872
2873/**
2874 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2875 * @vport: pointer to a virtual N_Port data structure.
2876 *
2877 * This routine stops all the timers associated with a @vport. This function
2878 * is invoked before disabling or deleting a @vport. Note that the physical
2879 * port is treated as @vport 0.
2880 **/
2881void
2882lpfc_stop_vport_timers(struct lpfc_vport *vport)
2883{
2884 del_timer_sync(&vport->els_tmofunc);
2885 del_timer_sync(&vport->delayed_disc_tmo);
2886 lpfc_can_disctmo(vport);
2887 return;
2888}
2889
2890/**
2891 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2892 * @phba: pointer to lpfc hba data structure.
2893 *
2894 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2895 * caller of this routine should already hold the host lock.
2896 **/
2897void
2898__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2899{
2900 /* Clear pending FCF rediscovery wait flag */
2901 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2902
2903 /* Now, try to stop the timer */
2904 del_timer(&phba->fcf.redisc_wait);
2905}
2906
2907/**
2908 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2909 * @phba: pointer to lpfc hba data structure.
2910 *
2911 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2912 * checks whether the FCF rediscovery wait timer is pending with the host
2913 * lock held before proceeding with disabling the timer and clearing the
2914 * wait timer pendig flag.
2915 **/
2916void
2917lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2918{
2919 spin_lock_irq(&phba->hbalock);
2920 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2921 /* FCF rediscovery timer already fired or stopped */
2922 spin_unlock_irq(&phba->hbalock);
2923 return;
2924 }
2925 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2926 /* Clear failover in progress flags */
2927 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
2928 spin_unlock_irq(&phba->hbalock);
2929}
2930
2931/**
2932 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2933 * @phba: pointer to lpfc hba data structure.
2934 *
2935 * This routine stops all the timers associated with a HBA. This function is
2936 * invoked before either putting a HBA offline or unloading the driver.
2937 **/
2938void
2939lpfc_stop_hba_timers(struct lpfc_hba *phba)
2940{
2941 lpfc_stop_vport_timers(phba->pport);
2942 del_timer_sync(&phba->sli.mbox_tmo);
2943 del_timer_sync(&phba->fabric_block_timer);
2944 del_timer_sync(&phba->eratt_poll);
2945 del_timer_sync(&phba->hb_tmofunc);
2946 if (phba->sli_rev == LPFC_SLI_REV4) {
2947 del_timer_sync(&phba->rrq_tmr);
2948 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2949 }
2950 phba->hb_outstanding = 0;
2951
2952 switch (phba->pci_dev_grp) {
2953 case LPFC_PCI_DEV_LP:
2954 /* Stop any LightPulse device specific driver timers */
2955 del_timer_sync(&phba->fcp_poll_timer);
2956 break;
2957 case LPFC_PCI_DEV_OC:
2958 /* Stop any OneConnect device sepcific driver timers */
2959 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2960 break;
2961 default:
2962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2963 "0297 Invalid device group (x%x)\n",
2964 phba->pci_dev_grp);
2965 break;
2966 }
2967 return;
2968}
2969
2970/**
2971 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2972 * @phba: pointer to lpfc hba data structure.
2973 *
2974 * This routine marks a HBA's management interface as blocked. Once the HBA's
2975 * management interface is marked as blocked, all the user space access to
2976 * the HBA, whether they are from sysfs interface or libdfc interface will
2977 * all be blocked. The HBA is set to block the management interface when the
2978 * driver prepares the HBA interface for online or offline.
2979 **/
2980static void
2981lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
2982{
2983 unsigned long iflag;
2984 uint8_t actcmd = MBX_HEARTBEAT;
2985 unsigned long timeout;
2986
2987 spin_lock_irqsave(&phba->hbalock, iflag);
2988 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2989 spin_unlock_irqrestore(&phba->hbalock, iflag);
2990 if (mbx_action == LPFC_MBX_NO_WAIT)
2991 return;
2992 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2993 spin_lock_irqsave(&phba->hbalock, iflag);
2994 if (phba->sli.mbox_active) {
2995 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
2996 /* Determine how long we might wait for the active mailbox
2997 * command to be gracefully completed by firmware.
2998 */
2999 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3000 phba->sli.mbox_active) * 1000) + jiffies;
3001 }
3002 spin_unlock_irqrestore(&phba->hbalock, iflag);
3003
3004 /* Wait for the outstnading mailbox command to complete */
3005 while (phba->sli.mbox_active) {
3006 /* Check active mailbox complete status every 2ms */
3007 msleep(2);
3008 if (time_after(jiffies, timeout)) {
3009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3010 "2813 Mgmt IO is Blocked %x "
3011 "- mbox cmd %x still active\n",
3012 phba->sli.sli_flag, actcmd);
3013 break;
3014 }
3015 }
3016}
3017
3018/**
3019 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3020 * @phba: pointer to lpfc hba data structure.
3021 *
3022 * Allocate RPIs for all active remote nodes. This is needed whenever
3023 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3024 * is to fixup the temporary rpi assignments.
3025 **/
3026void
3027lpfc_sli4_node_prep(struct lpfc_hba *phba)
3028{
3029 struct lpfc_nodelist *ndlp, *next_ndlp;
3030 struct lpfc_vport **vports;
3031 int i, rpi;
3032 unsigned long flags;
3033
3034 if (phba->sli_rev != LPFC_SLI_REV4)
3035 return;
3036
3037 vports = lpfc_create_vport_work_array(phba);
3038 if (vports == NULL)
3039 return;
3040
3041 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3042 if (vports[i]->load_flag & FC_UNLOADING)
3043 continue;
3044
3045 list_for_each_entry_safe(ndlp, next_ndlp,
3046 &vports[i]->fc_nodes,
3047 nlp_listp) {
3048 if (!NLP_CHK_NODE_ACT(ndlp))
3049 continue;
3050 rpi = lpfc_sli4_alloc_rpi(phba);
3051 if (rpi == LPFC_RPI_ALLOC_ERROR) {
3052 spin_lock_irqsave(&phba->ndlp_lock, flags);
3053 NLP_CLR_NODE_ACT(ndlp);
3054 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3055 continue;
3056 }
3057 ndlp->nlp_rpi = rpi;
3058 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3059 "0009 rpi:%x DID:%x "
3060 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3061 ndlp->nlp_DID, ndlp->nlp_flag,
3062 ndlp->nlp_usg_map, ndlp);
3063 }
3064 }
3065 lpfc_destroy_vport_work_array(phba, vports);
3066}
3067
3068/**
3069 * lpfc_online - Initialize and bring a HBA online
3070 * @phba: pointer to lpfc hba data structure.
3071 *
3072 * This routine initializes the HBA and brings a HBA online. During this
3073 * process, the management interface is blocked to prevent user space access
3074 * to the HBA interfering with the driver initialization.
3075 *
3076 * Return codes
3077 * 0 - successful
3078 * 1 - failed
3079 **/
3080int
3081lpfc_online(struct lpfc_hba *phba)
3082{
3083 struct lpfc_vport *vport;
3084 struct lpfc_vport **vports;
3085 int i, error = 0;
3086 bool vpis_cleared = false;
3087
3088 if (!phba)
3089 return 0;
3090 vport = phba->pport;
3091
3092 if (!(vport->fc_flag & FC_OFFLINE_MODE))
3093 return 0;
3094
3095 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3096 "0458 Bring Adapter online\n");
3097
3098 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
3099
3100 if (phba->sli_rev == LPFC_SLI_REV4) {
3101 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3102 lpfc_unblock_mgmt_io(phba);
3103 return 1;
3104 }
3105 spin_lock_irq(&phba->hbalock);
3106 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3107 vpis_cleared = true;
3108 spin_unlock_irq(&phba->hbalock);
3109
3110 /* Reestablish the local initiator port.
3111 * The offline process destroyed the previous lport.
3112 */
3113 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3114 !phba->nvmet_support) {
3115 error = lpfc_nvme_create_localport(phba->pport);
3116 if (error)
3117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3118 "6132 NVME restore reg failed "
3119 "on nvmei error x%x\n", error);
3120 }
3121 } else {
3122 lpfc_sli_queue_init(phba);
3123 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3124 lpfc_unblock_mgmt_io(phba);
3125 return 1;
3126 }
3127 }
3128
3129 vports = lpfc_create_vport_work_array(phba);
3130 if (vports != NULL) {
3131 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3132 struct Scsi_Host *shost;
3133 shost = lpfc_shost_from_vport(vports[i]);
3134 spin_lock_irq(shost->host_lock);
3135 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3136 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3137 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3138 if (phba->sli_rev == LPFC_SLI_REV4) {
3139 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
3140 if ((vpis_cleared) &&
3141 (vports[i]->port_type !=
3142 LPFC_PHYSICAL_PORT))
3143 vports[i]->vpi = 0;
3144 }
3145 spin_unlock_irq(shost->host_lock);
3146 }
3147 }
3148 lpfc_destroy_vport_work_array(phba, vports);
3149
3150 lpfc_unblock_mgmt_io(phba);
3151 return 0;
3152}
3153
3154/**
3155 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
3156 * @phba: pointer to lpfc hba data structure.
3157 *
3158 * This routine marks a HBA's management interface as not blocked. Once the
3159 * HBA's management interface is marked as not blocked, all the user space
3160 * access to the HBA, whether they are from sysfs interface or libdfc
3161 * interface will be allowed. The HBA is set to block the management interface
3162 * when the driver prepares the HBA interface for online or offline and then
3163 * set to unblock the management interface afterwards.
3164 **/
3165void
3166lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3167{
3168 unsigned long iflag;
3169
3170 spin_lock_irqsave(&phba->hbalock, iflag);
3171 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3172 spin_unlock_irqrestore(&phba->hbalock, iflag);
3173}
3174
3175/**
3176 * lpfc_offline_prep - Prepare a HBA to be brought offline
3177 * @phba: pointer to lpfc hba data structure.
3178 *
3179 * This routine is invoked to prepare a HBA to be brought offline. It performs
3180 * unregistration login to all the nodes on all vports and flushes the mailbox
3181 * queue to make it ready to be brought offline.
3182 **/
3183void
3184lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
3185{
3186 struct lpfc_vport *vport = phba->pport;
3187 struct lpfc_nodelist *ndlp, *next_ndlp;
3188 struct lpfc_vport **vports;
3189 struct Scsi_Host *shost;
3190 int i;
3191
3192 if (vport->fc_flag & FC_OFFLINE_MODE)
3193 return;
3194
3195 lpfc_block_mgmt_io(phba, mbx_action);
3196
3197 lpfc_linkdown(phba);
3198
3199 /* Issue an unreg_login to all nodes on all vports */
3200 vports = lpfc_create_vport_work_array(phba);
3201 if (vports != NULL) {
3202 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3203 if (vports[i]->load_flag & FC_UNLOADING)
3204 continue;
3205 shost = lpfc_shost_from_vport(vports[i]);
3206 spin_lock_irq(shost->host_lock);
3207 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
3208 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3209 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
3210 spin_unlock_irq(shost->host_lock);
3211
3212 shost = lpfc_shost_from_vport(vports[i]);
3213 list_for_each_entry_safe(ndlp, next_ndlp,
3214 &vports[i]->fc_nodes,
3215 nlp_listp) {
3216 if (!NLP_CHK_NODE_ACT(ndlp))
3217 continue;
3218 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3219 continue;
3220 if (ndlp->nlp_type & NLP_FABRIC) {
3221 lpfc_disc_state_machine(vports[i], ndlp,
3222 NULL, NLP_EVT_DEVICE_RECOVERY);
3223 lpfc_disc_state_machine(vports[i], ndlp,
3224 NULL, NLP_EVT_DEVICE_RM);
3225 }
3226 spin_lock_irq(shost->host_lock);
3227 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3228 spin_unlock_irq(shost->host_lock);
3229 /*
3230 * Whenever an SLI4 port goes offline, free the
3231 * RPI. Get a new RPI when the adapter port
3232 * comes back online.
3233 */
3234 if (phba->sli_rev == LPFC_SLI_REV4) {
3235 lpfc_printf_vlog(ndlp->vport,
3236 KERN_INFO, LOG_NODE,
3237 "0011 lpfc_offline: "
3238 "ndlp:x%p did %x "
3239 "usgmap:x%x rpi:%x\n",
3240 ndlp, ndlp->nlp_DID,
3241 ndlp->nlp_usg_map,
3242 ndlp->nlp_rpi);
3243
3244 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
3245 }
3246 lpfc_unreg_rpi(vports[i], ndlp);
3247 }
3248 }
3249 }
3250 lpfc_destroy_vport_work_array(phba, vports);
3251
3252 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
3253
3254 if (phba->wq)
3255 flush_workqueue(phba->wq);
3256}
3257
3258/**
3259 * lpfc_offline - Bring a HBA offline
3260 * @phba: pointer to lpfc hba data structure.
3261 *
3262 * This routine actually brings a HBA offline. It stops all the timers
3263 * associated with the HBA, brings down the SLI layer, and eventually
3264 * marks the HBA as in offline state for the upper layer protocol.
3265 **/
3266void
3267lpfc_offline(struct lpfc_hba *phba)
3268{
3269 struct Scsi_Host *shost;
3270 struct lpfc_vport **vports;
3271 int i;
3272
3273 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
3274 return;
3275
3276 /* stop port and all timers associated with this hba */
3277 lpfc_stop_port(phba);
3278
3279 /* Tear down the local and target port registrations. The
3280 * nvme transports need to cleanup.
3281 */
3282 lpfc_nvmet_destroy_targetport(phba);
3283 lpfc_nvme_destroy_localport(phba->pport);
3284
3285 vports = lpfc_create_vport_work_array(phba);
3286 if (vports != NULL)
3287 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3288 lpfc_stop_vport_timers(vports[i]);
3289 lpfc_destroy_vport_work_array(phba, vports);
3290 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3291 "0460 Bring Adapter offline\n");
3292 /* Bring down the SLI Layer and cleanup. The HBA is offline
3293 now. */
3294 lpfc_sli_hba_down(phba);
3295 spin_lock_irq(&phba->hbalock);
3296 phba->work_ha = 0;
3297 spin_unlock_irq(&phba->hbalock);
3298 vports = lpfc_create_vport_work_array(phba);
3299 if (vports != NULL)
3300 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3301 shost = lpfc_shost_from_vport(vports[i]);
3302 spin_lock_irq(shost->host_lock);
3303 vports[i]->work_port_events = 0;
3304 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3305 spin_unlock_irq(shost->host_lock);
3306 }
3307 lpfc_destroy_vport_work_array(phba, vports);
3308}
3309
3310/**
3311 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
3312 * @phba: pointer to lpfc hba data structure.
3313 *
3314 * This routine is to free all the SCSI buffers and IOCBs from the driver
3315 * list back to kernel. It is called from lpfc_pci_remove_one to free
3316 * the internal resources before the device is removed from the system.
3317 **/
3318static void
3319lpfc_scsi_free(struct lpfc_hba *phba)
3320{
3321 struct lpfc_scsi_buf *sb, *sb_next;
3322
3323 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3324 return;
3325
3326 spin_lock_irq(&phba->hbalock);
3327
3328 /* Release all the lpfc_scsi_bufs maintained by this host. */
3329
3330 spin_lock(&phba->scsi_buf_list_put_lock);
3331 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3332 list) {
3333 list_del(&sb->list);
3334 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
3335 sb->dma_handle);
3336 kfree(sb);
3337 phba->total_scsi_bufs--;
3338 }
3339 spin_unlock(&phba->scsi_buf_list_put_lock);
3340
3341 spin_lock(&phba->scsi_buf_list_get_lock);
3342 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3343 list) {
3344 list_del(&sb->list);
3345 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
3346 sb->dma_handle);
3347 kfree(sb);
3348 phba->total_scsi_bufs--;
3349 }
3350 spin_unlock(&phba->scsi_buf_list_get_lock);
3351 spin_unlock_irq(&phba->hbalock);
3352}
3353/**
3354 * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3355 * @phba: pointer to lpfc hba data structure.
3356 *
3357 * This routine is to free all the NVME buffers and IOCBs from the driver
3358 * list back to kernel. It is called from lpfc_pci_remove_one to free
3359 * the internal resources before the device is removed from the system.
3360 **/
3361static void
3362lpfc_nvme_free(struct lpfc_hba *phba)
3363{
3364 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
3365
3366 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3367 return;
3368
3369 spin_lock_irq(&phba->hbalock);
3370
3371 /* Release all the lpfc_nvme_bufs maintained by this host. */
3372 spin_lock(&phba->nvme_buf_list_put_lock);
3373 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3374 &phba->lpfc_nvme_buf_list_put, list) {
3375 list_del(&lpfc_ncmd->list);
3376 phba->put_nvme_bufs--;
3377 dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3378 lpfc_ncmd->dma_handle);
3379 kfree(lpfc_ncmd);
3380 phba->total_nvme_bufs--;
3381 }
3382 spin_unlock(&phba->nvme_buf_list_put_lock);
3383
3384 spin_lock(&phba->nvme_buf_list_get_lock);
3385 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3386 &phba->lpfc_nvme_buf_list_get, list) {
3387 list_del(&lpfc_ncmd->list);
3388 phba->get_nvme_bufs--;
3389 dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3390 lpfc_ncmd->dma_handle);
3391 kfree(lpfc_ncmd);
3392 phba->total_nvme_bufs--;
3393 }
3394 spin_unlock(&phba->nvme_buf_list_get_lock);
3395 spin_unlock_irq(&phba->hbalock);
3396}
3397/**
3398 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
3399 * @phba: pointer to lpfc hba data structure.
3400 *
3401 * This routine first calculates the sizes of the current els and allocated
3402 * scsi sgl lists, and then goes through all sgls to updates the physical
3403 * XRIs assigned due to port function reset. During port initialization, the
3404 * current els and allocated scsi sgl lists are 0s.
3405 *
3406 * Return codes
3407 * 0 - successful (for now, it always returns 0)
3408 **/
3409int
3410lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
3411{
3412 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3413 uint16_t i, lxri, xri_cnt, els_xri_cnt;
3414 LIST_HEAD(els_sgl_list);
3415 int rc;
3416
3417 /*
3418 * update on pci function's els xri-sgl list
3419 */
3420 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3421
3422 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3423 /* els xri-sgl expanded */
3424 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3425 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3426 "3157 ELS xri-sgl count increased from "
3427 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3428 els_xri_cnt);
3429 /* allocate the additional els sgls */
3430 for (i = 0; i < xri_cnt; i++) {
3431 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3432 GFP_KERNEL);
3433 if (sglq_entry == NULL) {
3434 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3435 "2562 Failure to allocate an "
3436 "ELS sgl entry:%d\n", i);
3437 rc = -ENOMEM;
3438 goto out_free_mem;
3439 }
3440 sglq_entry->buff_type = GEN_BUFF_TYPE;
3441 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3442 &sglq_entry->phys);
3443 if (sglq_entry->virt == NULL) {
3444 kfree(sglq_entry);
3445 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3446 "2563 Failure to allocate an "
3447 "ELS mbuf:%d\n", i);
3448 rc = -ENOMEM;
3449 goto out_free_mem;
3450 }
3451 sglq_entry->sgl = sglq_entry->virt;
3452 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3453 sglq_entry->state = SGL_FREED;
3454 list_add_tail(&sglq_entry->list, &els_sgl_list);
3455 }
3456 spin_lock_irq(&phba->hbalock);
3457 spin_lock(&phba->sli4_hba.sgl_list_lock);
3458 list_splice_init(&els_sgl_list,
3459 &phba->sli4_hba.lpfc_els_sgl_list);
3460 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3461 spin_unlock_irq(&phba->hbalock);
3462 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3463 /* els xri-sgl shrinked */
3464 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3465 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3466 "3158 ELS xri-sgl count decreased from "
3467 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3468 els_xri_cnt);
3469 spin_lock_irq(&phba->hbalock);
3470 spin_lock(&phba->sli4_hba.sgl_list_lock);
3471 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3472 &els_sgl_list);
3473 /* release extra els sgls from list */
3474 for (i = 0; i < xri_cnt; i++) {
3475 list_remove_head(&els_sgl_list,
3476 sglq_entry, struct lpfc_sglq, list);
3477 if (sglq_entry) {
3478 __lpfc_mbuf_free(phba, sglq_entry->virt,
3479 sglq_entry->phys);
3480 kfree(sglq_entry);
3481 }
3482 }
3483 list_splice_init(&els_sgl_list,
3484 &phba->sli4_hba.lpfc_els_sgl_list);
3485 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3486 spin_unlock_irq(&phba->hbalock);
3487 } else
3488 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3489 "3163 ELS xri-sgl count unchanged: %d\n",
3490 els_xri_cnt);
3491 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3492
3493 /* update xris to els sgls on the list */
3494 sglq_entry = NULL;
3495 sglq_entry_next = NULL;
3496 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3497 &phba->sli4_hba.lpfc_els_sgl_list, list) {
3498 lxri = lpfc_sli4_next_xritag(phba);
3499 if (lxri == NO_XRI) {
3500 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3501 "2400 Failed to allocate xri for "
3502 "ELS sgl\n");
3503 rc = -ENOMEM;
3504 goto out_free_mem;
3505 }
3506 sglq_entry->sli4_lxritag = lxri;
3507 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3508 }
3509 return 0;
3510
3511out_free_mem:
3512 lpfc_free_els_sgl_list(phba);
3513 return rc;
3514}
3515
3516/**
3517 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3518 * @phba: pointer to lpfc hba data structure.
3519 *
3520 * This routine first calculates the sizes of the current els and allocated
3521 * scsi sgl lists, and then goes through all sgls to updates the physical
3522 * XRIs assigned due to port function reset. During port initialization, the
3523 * current els and allocated scsi sgl lists are 0s.
3524 *
3525 * Return codes
3526 * 0 - successful (for now, it always returns 0)
3527 **/
3528int
3529lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3530{
3531 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3532 uint16_t i, lxri, xri_cnt, els_xri_cnt;
3533 uint16_t nvmet_xri_cnt;
3534 LIST_HEAD(nvmet_sgl_list);
3535 int rc;
3536
3537 /*
3538 * update on pci function's nvmet xri-sgl list
3539 */
3540 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3541
3542 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3543 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3544 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3545 /* els xri-sgl expanded */
3546 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3547 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3548 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3549 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3550 /* allocate the additional nvmet sgls */
3551 for (i = 0; i < xri_cnt; i++) {
3552 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3553 GFP_KERNEL);
3554 if (sglq_entry == NULL) {
3555 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3556 "6303 Failure to allocate an "
3557 "NVMET sgl entry:%d\n", i);
3558 rc = -ENOMEM;
3559 goto out_free_mem;
3560 }
3561 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3562 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3563 &sglq_entry->phys);
3564 if (sglq_entry->virt == NULL) {
3565 kfree(sglq_entry);
3566 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3567 "6304 Failure to allocate an "
3568 "NVMET buf:%d\n", i);
3569 rc = -ENOMEM;
3570 goto out_free_mem;
3571 }
3572 sglq_entry->sgl = sglq_entry->virt;
3573 memset(sglq_entry->sgl, 0,
3574 phba->cfg_sg_dma_buf_size);
3575 sglq_entry->state = SGL_FREED;
3576 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3577 }
3578 spin_lock_irq(&phba->hbalock);
3579 spin_lock(&phba->sli4_hba.sgl_list_lock);
3580 list_splice_init(&nvmet_sgl_list,
3581 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3582 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3583 spin_unlock_irq(&phba->hbalock);
3584 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3585 /* nvmet xri-sgl shrunk */
3586 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3587 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3588 "6305 NVMET xri-sgl count decreased from "
3589 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3590 nvmet_xri_cnt);
3591 spin_lock_irq(&phba->hbalock);
3592 spin_lock(&phba->sli4_hba.sgl_list_lock);
3593 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3594 &nvmet_sgl_list);
3595 /* release extra nvmet sgls from list */
3596 for (i = 0; i < xri_cnt; i++) {
3597 list_remove_head(&nvmet_sgl_list,
3598 sglq_entry, struct lpfc_sglq, list);
3599 if (sglq_entry) {
3600 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3601 sglq_entry->phys);
3602 kfree(sglq_entry);
3603 }
3604 }
3605 list_splice_init(&nvmet_sgl_list,
3606 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3607 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3608 spin_unlock_irq(&phba->hbalock);
3609 } else
3610 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3611 "6306 NVMET xri-sgl count unchanged: %d\n",
3612 nvmet_xri_cnt);
3613 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3614
3615 /* update xris to nvmet sgls on the list */
3616 sglq_entry = NULL;
3617 sglq_entry_next = NULL;
3618 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3619 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3620 lxri = lpfc_sli4_next_xritag(phba);
3621 if (lxri == NO_XRI) {
3622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3623 "6307 Failed to allocate xri for "
3624 "NVMET sgl\n");
3625 rc = -ENOMEM;
3626 goto out_free_mem;
3627 }
3628 sglq_entry->sli4_lxritag = lxri;
3629 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3630 }
3631 return 0;
3632
3633out_free_mem:
3634 lpfc_free_nvmet_sgl_list(phba);
3635 return rc;
3636}
3637
3638/**
3639 * lpfc_sli4_scsi_sgl_update - update xri-sgl sizing and mapping
3640 * @phba: pointer to lpfc hba data structure.
3641 *
3642 * This routine first calculates the sizes of the current els and allocated
3643 * scsi sgl lists, and then goes through all sgls to updates the physical
3644 * XRIs assigned due to port function reset. During port initialization, the
3645 * current els and allocated scsi sgl lists are 0s.
3646 *
3647 * Return codes
3648 * 0 - successful (for now, it always returns 0)
3649 **/
3650int
3651lpfc_sli4_scsi_sgl_update(struct lpfc_hba *phba)
3652{
3653 struct lpfc_scsi_buf *psb, *psb_next;
3654 uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3655 LIST_HEAD(scsi_sgl_list);
3656 int rc;
3657
3658 /*
3659 * update on pci function's els xri-sgl list
3660 */
3661 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3662 phba->total_scsi_bufs = 0;
3663
3664 /*
3665 * update on pci function's allocated scsi xri-sgl list
3666 */
3667 /* maximum number of xris available for scsi buffers */
3668 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3669 els_xri_cnt;
3670
3671 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3672 return 0;
3673
3674 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3675 phba->sli4_hba.scsi_xri_max = /* Split them up */
3676 (phba->sli4_hba.scsi_xri_max *
3677 phba->cfg_xri_split) / 100;
3678
3679 spin_lock_irq(&phba->scsi_buf_list_get_lock);
3680 spin_lock(&phba->scsi_buf_list_put_lock);
3681 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3682 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
3683 spin_unlock(&phba->scsi_buf_list_put_lock);
3684 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3685
3686 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3687 "6060 Current allocated SCSI xri-sgl count:%d, "
3688 "maximum SCSI xri count:%d (split:%d)\n",
3689 phba->sli4_hba.scsi_xri_cnt,
3690 phba->sli4_hba.scsi_xri_max, phba->cfg_xri_split);
3691
3692 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3693 /* max scsi xri shrinked below the allocated scsi buffers */
3694 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3695 phba->sli4_hba.scsi_xri_max;
3696 /* release the extra allocated scsi buffers */
3697 for (i = 0; i < scsi_xri_cnt; i++) {
3698 list_remove_head(&scsi_sgl_list, psb,
3699 struct lpfc_scsi_buf, list);
3700 if (psb) {
3701 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3702 psb->data, psb->dma_handle);
3703 kfree(psb);
3704 }
3705 }
3706 spin_lock_irq(&phba->scsi_buf_list_get_lock);
3707 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
3708 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3709 }
3710
3711 /* update xris associated to remaining allocated scsi buffers */
3712 psb = NULL;
3713 psb_next = NULL;
3714 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3715 lxri = lpfc_sli4_next_xritag(phba);
3716 if (lxri == NO_XRI) {
3717 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3718 "2560 Failed to allocate xri for "
3719 "scsi buffer\n");
3720 rc = -ENOMEM;
3721 goto out_free_mem;
3722 }
3723 psb->cur_iocbq.sli4_lxritag = lxri;
3724 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3725 }
3726 spin_lock_irq(&phba->scsi_buf_list_get_lock);
3727 spin_lock(&phba->scsi_buf_list_put_lock);
3728 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3729 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
3730 spin_unlock(&phba->scsi_buf_list_put_lock);
3731 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3732 return 0;
3733
3734out_free_mem:
3735 lpfc_scsi_free(phba);
3736 return rc;
3737}
3738
3739static uint64_t
3740lpfc_get_wwpn(struct lpfc_hba *phba)
3741{
3742 uint64_t wwn;
3743 int rc;
3744 LPFC_MBOXQ_t *mboxq;
3745 MAILBOX_t *mb;
3746
3747 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3748 GFP_KERNEL);
3749 if (!mboxq)
3750 return (uint64_t)-1;
3751
3752 /* First get WWN of HBA instance */
3753 lpfc_read_nv(phba, mboxq);
3754 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3755 if (rc != MBX_SUCCESS) {
3756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3757 "6019 Mailbox failed , mbxCmd x%x "
3758 "READ_NV, mbxStatus x%x\n",
3759 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
3760 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
3761 mempool_free(mboxq, phba->mbox_mem_pool);
3762 return (uint64_t) -1;
3763 }
3764 mb = &mboxq->u.mb;
3765 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
3766 /* wwn is WWPN of HBA instance */
3767 mempool_free(mboxq, phba->mbox_mem_pool);
3768 if (phba->sli_rev == LPFC_SLI_REV4)
3769 return be64_to_cpu(wwn);
3770 else
3771 return rol64(wwn, 32);
3772}
3773
3774/**
3775 * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3776 * @phba: pointer to lpfc hba data structure.
3777 *
3778 * This routine first calculates the sizes of the current els and allocated
3779 * scsi sgl lists, and then goes through all sgls to updates the physical
3780 * XRIs assigned due to port function reset. During port initialization, the
3781 * current els and allocated scsi sgl lists are 0s.
3782 *
3783 * Return codes
3784 * 0 - successful (for now, it always returns 0)
3785 **/
3786int
3787lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3788{
3789 struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3790 uint16_t i, lxri, els_xri_cnt;
3791 uint16_t nvme_xri_cnt, nvme_xri_max;
3792 LIST_HEAD(nvme_sgl_list);
3793 int rc, cnt;
3794
3795 phba->total_nvme_bufs = 0;
3796 phba->get_nvme_bufs = 0;
3797 phba->put_nvme_bufs = 0;
3798
3799 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3800 return 0;
3801 /*
3802 * update on pci function's allocated nvme xri-sgl list
3803 */
3804
3805 /* maximum number of xris available for nvme buffers */
3806 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3807 nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3808 phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3809 phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3810
3811 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3812 "6074 Current allocated NVME xri-sgl count:%d, "
3813 "maximum NVME xri count:%d\n",
3814 phba->sli4_hba.nvme_xri_cnt,
3815 phba->sli4_hba.nvme_xri_max);
3816
3817 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3818 spin_lock(&phba->nvme_buf_list_put_lock);
3819 list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3820 list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
3821 cnt = phba->get_nvme_bufs + phba->put_nvme_bufs;
3822 phba->get_nvme_bufs = 0;
3823 phba->put_nvme_bufs = 0;
3824 spin_unlock(&phba->nvme_buf_list_put_lock);
3825 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3826
3827 if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3828 /* max nvme xri shrunk below the allocated nvme buffers */
3829 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3830 nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3831 phba->sli4_hba.nvme_xri_max;
3832 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3833 /* release the extra allocated nvme buffers */
3834 for (i = 0; i < nvme_xri_cnt; i++) {
3835 list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3836 struct lpfc_nvme_buf, list);
3837 if (lpfc_ncmd) {
3838 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3839 lpfc_ncmd->data,
3840 lpfc_ncmd->dma_handle);
3841 kfree(lpfc_ncmd);
3842 }
3843 }
3844 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3845 phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3846 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3847 }
3848
3849 /* update xris associated to remaining allocated nvme buffers */
3850 lpfc_ncmd = NULL;
3851 lpfc_ncmd_next = NULL;
3852 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3853 &nvme_sgl_list, list) {
3854 lxri = lpfc_sli4_next_xritag(phba);
3855 if (lxri == NO_XRI) {
3856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3857 "6075 Failed to allocate xri for "
3858 "nvme buffer\n");
3859 rc = -ENOMEM;
3860 goto out_free_mem;
3861 }
3862 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3863 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3864 }
3865 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3866 spin_lock(&phba->nvme_buf_list_put_lock);
3867 list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
3868 phba->get_nvme_bufs = cnt;
3869 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3870 spin_unlock(&phba->nvme_buf_list_put_lock);
3871 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3872 return 0;
3873
3874out_free_mem:
3875 lpfc_nvme_free(phba);
3876 return rc;
3877}
3878
3879/**
3880 * lpfc_create_port - Create an FC port
3881 * @phba: pointer to lpfc hba data structure.
3882 * @instance: a unique integer ID to this FC port.
3883 * @dev: pointer to the device data structure.
3884 *
3885 * This routine creates a FC port for the upper layer protocol. The FC port
3886 * can be created on top of either a physical port or a virtual port provided
3887 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3888 * and associates the FC port created before adding the shost into the SCSI
3889 * layer.
3890 *
3891 * Return codes
3892 * @vport - pointer to the virtual N_Port data structure.
3893 * NULL - port create failed.
3894 **/
3895struct lpfc_vport *
3896lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
3897{
3898 struct lpfc_vport *vport;
3899 struct Scsi_Host *shost = NULL;
3900 int error = 0;
3901 int i;
3902 uint64_t wwn;
3903 bool use_no_reset_hba = false;
3904 int rc;
3905
3906 if (lpfc_no_hba_reset_cnt) {
3907 if (phba->sli_rev < LPFC_SLI_REV4 &&
3908 dev == &phba->pcidev->dev) {
3909 /* Reset the port first */
3910 lpfc_sli_brdrestart(phba);
3911 rc = lpfc_sli_chipset_init(phba);
3912 if (rc)
3913 return NULL;
3914 }
3915 wwn = lpfc_get_wwpn(phba);
3916 }
3917
3918 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
3919 if (wwn == lpfc_no_hba_reset[i]) {
3920 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3921 "6020 Setting use_no_reset port=%llx\n",
3922 wwn);
3923 use_no_reset_hba = true;
3924 break;
3925 }
3926 }
3927
3928 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3929 if (dev != &phba->pcidev->dev) {
3930 shost = scsi_host_alloc(&lpfc_vport_template,
3931 sizeof(struct lpfc_vport));
3932 } else {
3933 if (!use_no_reset_hba)
3934 shost = scsi_host_alloc(&lpfc_template,
3935 sizeof(struct lpfc_vport));
3936 else
3937 shost = scsi_host_alloc(&lpfc_template_no_hr,
3938 sizeof(struct lpfc_vport));
3939 }
3940 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3941 shost = scsi_host_alloc(&lpfc_template_nvme,
3942 sizeof(struct lpfc_vport));
3943 }
3944 if (!shost)
3945 goto out;
3946
3947 vport = (struct lpfc_vport *) shost->hostdata;
3948 vport->phba = phba;
3949 vport->load_flag |= FC_LOADING;
3950 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3951 vport->fc_rscn_flush = 0;
3952 lpfc_get_vport_cfgparam(vport);
3953
3954 shost->unique_id = instance;
3955 shost->max_id = LPFC_MAX_TARGET;
3956 shost->max_lun = vport->cfg_max_luns;
3957 shost->this_id = -1;
3958 shost->max_cmd_len = 16;
3959 shost->nr_hw_queues = phba->cfg_fcp_io_channel;
3960 if (phba->sli_rev == LPFC_SLI_REV4) {
3961 shost->dma_boundary =
3962 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
3963 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3964 }
3965
3966 /*
3967 * Set initial can_queue value since 0 is no longer supported and
3968 * scsi_add_host will fail. This will be adjusted later based on the
3969 * max xri value determined in hba setup.
3970 */
3971 shost->can_queue = phba->cfg_hba_queue_depth - 10;
3972 if (dev != &phba->pcidev->dev) {
3973 shost->transportt = lpfc_vport_transport_template;
3974 vport->port_type = LPFC_NPIV_PORT;
3975 } else {
3976 shost->transportt = lpfc_transport_template;
3977 vport->port_type = LPFC_PHYSICAL_PORT;
3978 }
3979
3980 /* Initialize all internally managed lists. */
3981 INIT_LIST_HEAD(&vport->fc_nodes);
3982 INIT_LIST_HEAD(&vport->rcv_buffer_list);
3983 spin_lock_init(&vport->work_port_lock);
3984
3985 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
3986
3987 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
3988
3989 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
3990
3991 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
3992 if (error)
3993 goto out_put_shost;
3994
3995 spin_lock_irq(&phba->hbalock);
3996 list_add_tail(&vport->listentry, &phba->port_list);
3997 spin_unlock_irq(&phba->hbalock);
3998 return vport;
3999
4000out_put_shost:
4001 scsi_host_put(shost);
4002out:
4003 return NULL;
4004}
4005
4006/**
4007 * destroy_port - destroy an FC port
4008 * @vport: pointer to an lpfc virtual N_Port data structure.
4009 *
4010 * This routine destroys a FC port from the upper layer protocol. All the
4011 * resources associated with the port are released.
4012 **/
4013void
4014destroy_port(struct lpfc_vport *vport)
4015{
4016 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4017 struct lpfc_hba *phba = vport->phba;
4018
4019 lpfc_debugfs_terminate(vport);
4020 fc_remove_host(shost);
4021 scsi_remove_host(shost);
4022
4023 spin_lock_irq(&phba->hbalock);
4024 list_del_init(&vport->listentry);
4025 spin_unlock_irq(&phba->hbalock);
4026
4027 lpfc_cleanup(vport);
4028 return;
4029}
4030
4031/**
4032 * lpfc_get_instance - Get a unique integer ID
4033 *
4034 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4035 * uses the kernel idr facility to perform the task.
4036 *
4037 * Return codes:
4038 * instance - a unique integer ID allocated as the new instance.
4039 * -1 - lpfc get instance failed.
4040 **/
4041int
4042lpfc_get_instance(void)
4043{
4044 int ret;
4045
4046 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4047 return ret < 0 ? -1 : ret;
4048}
4049
4050/**
4051 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
4052 * @shost: pointer to SCSI host data structure.
4053 * @time: elapsed time of the scan in jiffies.
4054 *
4055 * This routine is called by the SCSI layer with a SCSI host to determine
4056 * whether the scan host is finished.
4057 *
4058 * Note: there is no scan_start function as adapter initialization will have
4059 * asynchronously kicked off the link initialization.
4060 *
4061 * Return codes
4062 * 0 - SCSI host scan is not over yet.
4063 * 1 - SCSI host scan is over.
4064 **/
4065int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4066{
4067 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4068 struct lpfc_hba *phba = vport->phba;
4069 int stat = 0;
4070
4071 spin_lock_irq(shost->host_lock);
4072
4073 if (vport->load_flag & FC_UNLOADING) {
4074 stat = 1;
4075 goto finished;
4076 }
4077 if (time >= msecs_to_jiffies(30 * 1000)) {
4078 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4079 "0461 Scanning longer than 30 "
4080 "seconds. Continuing initialization\n");
4081 stat = 1;
4082 goto finished;
4083 }
4084 if (time >= msecs_to_jiffies(15 * 1000) &&
4085 phba->link_state <= LPFC_LINK_DOWN) {
4086 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4087 "0465 Link down longer than 15 "
4088 "seconds. Continuing initialization\n");
4089 stat = 1;
4090 goto finished;
4091 }
4092
4093 if (vport->port_state != LPFC_VPORT_READY)
4094 goto finished;
4095 if (vport->num_disc_nodes || vport->fc_prli_sent)
4096 goto finished;
4097 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
4098 goto finished;
4099 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
4100 goto finished;
4101
4102 stat = 1;
4103
4104finished:
4105 spin_unlock_irq(shost->host_lock);
4106 return stat;
4107}
4108
4109/**
4110 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
4111 * @shost: pointer to SCSI host data structure.
4112 *
4113 * This routine initializes a given SCSI host attributes on a FC port. The
4114 * SCSI host can be either on top of a physical port or a virtual port.
4115 **/
4116void lpfc_host_attrib_init(struct Scsi_Host *shost)
4117{
4118 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4119 struct lpfc_hba *phba = vport->phba;
4120 /*
4121 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
4122 */
4123
4124 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4125 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4126 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4127
4128 memset(fc_host_supported_fc4s(shost), 0,
4129 sizeof(fc_host_supported_fc4s(shost)));
4130 fc_host_supported_fc4s(shost)[2] = 1;
4131 fc_host_supported_fc4s(shost)[7] = 1;
4132
4133 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4134 sizeof fc_host_symbolic_name(shost));
4135
4136 fc_host_supported_speeds(shost) = 0;
4137 if (phba->lmt & LMT_64Gb)
4138 fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4139 if (phba->lmt & LMT_32Gb)
4140 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4141 if (phba->lmt & LMT_16Gb)
4142 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4143 if (phba->lmt & LMT_10Gb)
4144 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4145 if (phba->lmt & LMT_8Gb)
4146 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4147 if (phba->lmt & LMT_4Gb)
4148 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4149 if (phba->lmt & LMT_2Gb)
4150 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4151 if (phba->lmt & LMT_1Gb)
4152 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4153
4154 fc_host_maxframe_size(shost) =
4155 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4156 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
4157
4158 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4159
4160 /* This value is also unchanging */
4161 memset(fc_host_active_fc4s(shost), 0,
4162 sizeof(fc_host_active_fc4s(shost)));
4163 fc_host_active_fc4s(shost)[2] = 1;
4164 fc_host_active_fc4s(shost)[7] = 1;
4165
4166 fc_host_max_npiv_vports(shost) = phba->max_vpi;
4167 spin_lock_irq(shost->host_lock);
4168 vport->load_flag &= ~FC_LOADING;
4169 spin_unlock_irq(shost->host_lock);
4170}
4171
4172/**
4173 * lpfc_stop_port_s3 - Stop SLI3 device port
4174 * @phba: pointer to lpfc hba data structure.
4175 *
4176 * This routine is invoked to stop an SLI3 device port, it stops the device
4177 * from generating interrupts and stops the device driver's timers for the
4178 * device.
4179 **/
4180static void
4181lpfc_stop_port_s3(struct lpfc_hba *phba)
4182{
4183 /* Clear all interrupt enable conditions */
4184 writel(0, phba->HCregaddr);
4185 readl(phba->HCregaddr); /* flush */
4186 /* Clear all pending interrupts */
4187 writel(0xffffffff, phba->HAregaddr);
4188 readl(phba->HAregaddr); /* flush */
4189
4190 /* Reset some HBA SLI setup states */
4191 lpfc_stop_hba_timers(phba);
4192 phba->pport->work_port_events = 0;
4193}
4194
4195/**
4196 * lpfc_stop_port_s4 - Stop SLI4 device port
4197 * @phba: pointer to lpfc hba data structure.
4198 *
4199 * This routine is invoked to stop an SLI4 device port, it stops the device
4200 * from generating interrupts and stops the device driver's timers for the
4201 * device.
4202 **/
4203static void
4204lpfc_stop_port_s4(struct lpfc_hba *phba)
4205{
4206 /* Reset some HBA SLI4 setup states */
4207 lpfc_stop_hba_timers(phba);
4208 phba->pport->work_port_events = 0;
4209 phba->sli4_hba.intr_enable = 0;
4210}
4211
4212/**
4213 * lpfc_stop_port - Wrapper function for stopping hba port
4214 * @phba: Pointer to HBA context object.
4215 *
4216 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4217 * the API jump table function pointer from the lpfc_hba struct.
4218 **/
4219void
4220lpfc_stop_port(struct lpfc_hba *phba)
4221{
4222 phba->lpfc_stop_port(phba);
4223
4224 if (phba->wq)
4225 flush_workqueue(phba->wq);
4226}
4227
4228/**
4229 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4230 * @phba: Pointer to hba for which this call is being executed.
4231 *
4232 * This routine starts the timer waiting for the FCF rediscovery to complete.
4233 **/
4234void
4235lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4236{
4237 unsigned long fcf_redisc_wait_tmo =
4238 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4239 /* Start fcf rediscovery wait period timer */
4240 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4241 spin_lock_irq(&phba->hbalock);
4242 /* Allow action to new fcf asynchronous event */
4243 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4244 /* Mark the FCF rediscovery pending state */
4245 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4246 spin_unlock_irq(&phba->hbalock);
4247}
4248
4249/**
4250 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4251 * @ptr: Map to lpfc_hba data structure pointer.
4252 *
4253 * This routine is invoked when waiting for FCF table rediscover has been
4254 * timed out. If new FCF record(s) has (have) been discovered during the
4255 * wait period, a new FCF event shall be added to the FCOE async event
4256 * list, and then worker thread shall be waked up for processing from the
4257 * worker thread context.
4258 **/
4259static void
4260lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
4261{
4262 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
4263
4264 /* Don't send FCF rediscovery event if timer cancelled */
4265 spin_lock_irq(&phba->hbalock);
4266 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4267 spin_unlock_irq(&phba->hbalock);
4268 return;
4269 }
4270 /* Clear FCF rediscovery timer pending flag */
4271 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4272 /* FCF rediscovery event to worker thread */
4273 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4274 spin_unlock_irq(&phba->hbalock);
4275 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
4276 "2776 FCF rediscover quiescent timer expired\n");
4277 /* wake up worker thread */
4278 lpfc_worker_wake_up(phba);
4279}
4280
4281/**
4282 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4283 * @phba: pointer to lpfc hba data structure.
4284 * @acqe_link: pointer to the async link completion queue entry.
4285 *
4286 * This routine is to parse the SLI4 link-attention link fault code.
4287 **/
4288static void
4289lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4290 struct lpfc_acqe_link *acqe_link)
4291{
4292 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4293 case LPFC_ASYNC_LINK_FAULT_NONE:
4294 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4295 case LPFC_ASYNC_LINK_FAULT_REMOTE:
4296 case LPFC_ASYNC_LINK_FAULT_LR_LRR:
4297 break;
4298 default:
4299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4300 "0398 Unknown link fault code: x%x\n",
4301 bf_get(lpfc_acqe_link_fault, acqe_link));
4302 break;
4303 }
4304}
4305
4306/**
4307 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4308 * @phba: pointer to lpfc hba data structure.
4309 * @acqe_link: pointer to the async link completion queue entry.
4310 *
4311 * This routine is to parse the SLI4 link attention type and translate it
4312 * into the base driver's link attention type coding.
4313 *
4314 * Return: Link attention type in terms of base driver's coding.
4315 **/
4316static uint8_t
4317lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4318 struct lpfc_acqe_link *acqe_link)
4319{
4320 uint8_t att_type;
4321
4322 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4323 case LPFC_ASYNC_LINK_STATUS_DOWN:
4324 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
4325 att_type = LPFC_ATT_LINK_DOWN;
4326 break;
4327 case LPFC_ASYNC_LINK_STATUS_UP:
4328 /* Ignore physical link up events - wait for logical link up */
4329 att_type = LPFC_ATT_RESERVED;
4330 break;
4331 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
4332 att_type = LPFC_ATT_LINK_UP;
4333 break;
4334 default:
4335 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4336 "0399 Invalid link attention type: x%x\n",
4337 bf_get(lpfc_acqe_link_status, acqe_link));
4338 att_type = LPFC_ATT_RESERVED;
4339 break;
4340 }
4341 return att_type;
4342}
4343
4344/**
4345 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4346 * @phba: pointer to lpfc hba data structure.
4347 *
4348 * This routine is to get an SLI3 FC port's link speed in Mbps.
4349 *
4350 * Return: link speed in terms of Mbps.
4351 **/
4352uint32_t
4353lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4354{
4355 uint32_t link_speed;
4356
4357 if (!lpfc_is_link_up(phba))
4358 return 0;
4359
4360 if (phba->sli_rev <= LPFC_SLI_REV3) {
4361 switch (phba->fc_linkspeed) {
4362 case LPFC_LINK_SPEED_1GHZ:
4363 link_speed = 1000;
4364 break;
4365 case LPFC_LINK_SPEED_2GHZ:
4366 link_speed = 2000;
4367 break;
4368 case LPFC_LINK_SPEED_4GHZ:
4369 link_speed = 4000;
4370 break;
4371 case LPFC_LINK_SPEED_8GHZ:
4372 link_speed = 8000;
4373 break;
4374 case LPFC_LINK_SPEED_10GHZ:
4375 link_speed = 10000;
4376 break;
4377 case LPFC_LINK_SPEED_16GHZ:
4378 link_speed = 16000;
4379 break;
4380 default:
4381 link_speed = 0;
4382 }
4383 } else {
4384 if (phba->sli4_hba.link_state.logical_speed)
4385 link_speed =
4386 phba->sli4_hba.link_state.logical_speed;
4387 else
4388 link_speed = phba->sli4_hba.link_state.speed;
4389 }
4390 return link_speed;
4391}
4392
4393/**
4394 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4395 * @phba: pointer to lpfc hba data structure.
4396 * @evt_code: asynchronous event code.
4397 * @speed_code: asynchronous event link speed code.
4398 *
4399 * This routine is to parse the giving SLI4 async event link speed code into
4400 * value of Mbps for the link speed.
4401 *
4402 * Return: link speed in terms of Mbps.
4403 **/
4404static uint32_t
4405lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4406 uint8_t speed_code)
4407{
4408 uint32_t port_speed;
4409
4410 switch (evt_code) {
4411 case LPFC_TRAILER_CODE_LINK:
4412 switch (speed_code) {
4413 case LPFC_ASYNC_LINK_SPEED_ZERO:
4414 port_speed = 0;
4415 break;
4416 case LPFC_ASYNC_LINK_SPEED_10MBPS:
4417 port_speed = 10;
4418 break;
4419 case LPFC_ASYNC_LINK_SPEED_100MBPS:
4420 port_speed = 100;
4421 break;
4422 case LPFC_ASYNC_LINK_SPEED_1GBPS:
4423 port_speed = 1000;
4424 break;
4425 case LPFC_ASYNC_LINK_SPEED_10GBPS:
4426 port_speed = 10000;
4427 break;
4428 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4429 port_speed = 20000;
4430 break;
4431 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4432 port_speed = 25000;
4433 break;
4434 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4435 port_speed = 40000;
4436 break;
4437 default:
4438 port_speed = 0;
4439 }
4440 break;
4441 case LPFC_TRAILER_CODE_FC:
4442 switch (speed_code) {
4443 case LPFC_FC_LA_SPEED_UNKNOWN:
4444 port_speed = 0;
4445 break;
4446 case LPFC_FC_LA_SPEED_1G:
4447 port_speed = 1000;
4448 break;
4449 case LPFC_FC_LA_SPEED_2G:
4450 port_speed = 2000;
4451 break;
4452 case LPFC_FC_LA_SPEED_4G:
4453 port_speed = 4000;
4454 break;
4455 case LPFC_FC_LA_SPEED_8G:
4456 port_speed = 8000;
4457 break;
4458 case LPFC_FC_LA_SPEED_10G:
4459 port_speed = 10000;
4460 break;
4461 case LPFC_FC_LA_SPEED_16G:
4462 port_speed = 16000;
4463 break;
4464 case LPFC_FC_LA_SPEED_32G:
4465 port_speed = 32000;
4466 break;
4467 case LPFC_FC_LA_SPEED_64G:
4468 port_speed = 64000;
4469 break;
4470 default:
4471 port_speed = 0;
4472 }
4473 break;
4474 default:
4475 port_speed = 0;
4476 }
4477 return port_speed;
4478}
4479
4480/**
4481 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
4482 * @phba: pointer to lpfc hba data structure.
4483 * @acqe_link: pointer to the async link completion queue entry.
4484 *
4485 * This routine is to handle the SLI4 asynchronous FCoE link event.
4486 **/
4487static void
4488lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4489 struct lpfc_acqe_link *acqe_link)
4490{
4491 struct lpfc_dmabuf *mp;
4492 LPFC_MBOXQ_t *pmb;
4493 MAILBOX_t *mb;
4494 struct lpfc_mbx_read_top *la;
4495 uint8_t att_type;
4496 int rc;
4497
4498 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
4499 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
4500 return;
4501 phba->fcoe_eventtag = acqe_link->event_tag;
4502 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4503 if (!pmb) {
4504 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4505 "0395 The mboxq allocation failed\n");
4506 return;
4507 }
4508 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4509 if (!mp) {
4510 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4511 "0396 The lpfc_dmabuf allocation failed\n");
4512 goto out_free_pmb;
4513 }
4514 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4515 if (!mp->virt) {
4516 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4517 "0397 The mbuf allocation failed\n");
4518 goto out_free_dmabuf;
4519 }
4520
4521 /* Cleanup any outstanding ELS commands */
4522 lpfc_els_flush_all_cmd(phba);
4523
4524 /* Block ELS IOCBs until we have done process link event */
4525 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4526
4527 /* Update link event statistics */
4528 phba->sli.slistat.link_event++;
4529
4530 /* Create lpfc_handle_latt mailbox command from link ACQE */
4531 lpfc_read_topology(phba, pmb, mp);
4532 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4533 pmb->vport = phba->pport;
4534
4535 /* Keep the link status for extra SLI4 state machine reference */
4536 phba->sli4_hba.link_state.speed =
4537 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4538 bf_get(lpfc_acqe_link_speed, acqe_link));
4539 phba->sli4_hba.link_state.duplex =
4540 bf_get(lpfc_acqe_link_duplex, acqe_link);
4541 phba->sli4_hba.link_state.status =
4542 bf_get(lpfc_acqe_link_status, acqe_link);
4543 phba->sli4_hba.link_state.type =
4544 bf_get(lpfc_acqe_link_type, acqe_link);
4545 phba->sli4_hba.link_state.number =
4546 bf_get(lpfc_acqe_link_number, acqe_link);
4547 phba->sli4_hba.link_state.fault =
4548 bf_get(lpfc_acqe_link_fault, acqe_link);
4549 phba->sli4_hba.link_state.logical_speed =
4550 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4551
4552 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4553 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4554 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4555 "Logical speed:%dMbps Fault:%d\n",
4556 phba->sli4_hba.link_state.speed,
4557 phba->sli4_hba.link_state.topology,
4558 phba->sli4_hba.link_state.status,
4559 phba->sli4_hba.link_state.type,
4560 phba->sli4_hba.link_state.number,
4561 phba->sli4_hba.link_state.logical_speed,
4562 phba->sli4_hba.link_state.fault);
4563 /*
4564 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4565 * topology info. Note: Optional for non FC-AL ports.
4566 */
4567 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4568 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4569 if (rc == MBX_NOT_FINISHED)
4570 goto out_free_dmabuf;
4571 return;
4572 }
4573 /*
4574 * For FCoE Mode: fill in all the topology information we need and call
4575 * the READ_TOPOLOGY completion routine to continue without actually
4576 * sending the READ_TOPOLOGY mailbox command to the port.
4577 */
4578 /* Initialize completion status */
4579 mb = &pmb->u.mb;
4580 mb->mbxStatus = MBX_SUCCESS;
4581
4582 /* Parse port fault information field */
4583 lpfc_sli4_parse_latt_fault(phba, acqe_link);
4584
4585 /* Parse and translate link attention fields */
4586 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4587 la->eventTag = acqe_link->event_tag;
4588 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4589 bf_set(lpfc_mbx_read_top_link_spd, la,
4590 (bf_get(lpfc_acqe_link_speed, acqe_link)));
4591
4592 /* Fake the the following irrelvant fields */
4593 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4594 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4595 bf_set(lpfc_mbx_read_top_il, la, 0);
4596 bf_set(lpfc_mbx_read_top_pb, la, 0);
4597 bf_set(lpfc_mbx_read_top_fa, la, 0);
4598 bf_set(lpfc_mbx_read_top_mm, la, 0);
4599
4600 /* Invoke the lpfc_handle_latt mailbox command callback function */
4601 lpfc_mbx_cmpl_read_topology(phba, pmb);
4602
4603 return;
4604
4605out_free_dmabuf:
4606 kfree(mp);
4607out_free_pmb:
4608 mempool_free(pmb, phba->mbox_mem_pool);
4609}
4610
4611/**
4612 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4613 * @phba: pointer to lpfc hba data structure.
4614 * @acqe_fc: pointer to the async fc completion queue entry.
4615 *
4616 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4617 * that the event was received and then issue a read_topology mailbox command so
4618 * that the rest of the driver will treat it the same as SLI3.
4619 **/
4620static void
4621lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4622{
4623 struct lpfc_dmabuf *mp;
4624 LPFC_MBOXQ_t *pmb;
4625 MAILBOX_t *mb;
4626 struct lpfc_mbx_read_top *la;
4627 int rc;
4628
4629 if (bf_get(lpfc_trailer_type, acqe_fc) !=
4630 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4631 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4632 "2895 Non FC link Event detected.(%d)\n",
4633 bf_get(lpfc_trailer_type, acqe_fc));
4634 return;
4635 }
4636 /* Keep the link status for extra SLI4 state machine reference */
4637 phba->sli4_hba.link_state.speed =
4638 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4639 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
4640 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4641 phba->sli4_hba.link_state.topology =
4642 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4643 phba->sli4_hba.link_state.status =
4644 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4645 phba->sli4_hba.link_state.type =
4646 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4647 phba->sli4_hba.link_state.number =
4648 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4649 phba->sli4_hba.link_state.fault =
4650 bf_get(lpfc_acqe_link_fault, acqe_fc);
4651 phba->sli4_hba.link_state.logical_speed =
4652 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
4653 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4654 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
4655 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4656 "%dMbps Fault:%d\n",
4657 phba->sli4_hba.link_state.speed,
4658 phba->sli4_hba.link_state.topology,
4659 phba->sli4_hba.link_state.status,
4660 phba->sli4_hba.link_state.type,
4661 phba->sli4_hba.link_state.number,
4662 phba->sli4_hba.link_state.logical_speed,
4663 phba->sli4_hba.link_state.fault);
4664 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4665 if (!pmb) {
4666 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4667 "2897 The mboxq allocation failed\n");
4668 return;
4669 }
4670 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4671 if (!mp) {
4672 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4673 "2898 The lpfc_dmabuf allocation failed\n");
4674 goto out_free_pmb;
4675 }
4676 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4677 if (!mp->virt) {
4678 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4679 "2899 The mbuf allocation failed\n");
4680 goto out_free_dmabuf;
4681 }
4682
4683 /* Cleanup any outstanding ELS commands */
4684 lpfc_els_flush_all_cmd(phba);
4685
4686 /* Block ELS IOCBs until we have done process link event */
4687 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4688
4689 /* Update link event statistics */
4690 phba->sli.slistat.link_event++;
4691
4692 /* Create lpfc_handle_latt mailbox command from link ACQE */
4693 lpfc_read_topology(phba, pmb, mp);
4694 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4695 pmb->vport = phba->pport;
4696
4697 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4698 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
4699
4700 switch (phba->sli4_hba.link_state.status) {
4701 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
4702 phba->link_flag |= LS_MDS_LINK_DOWN;
4703 break;
4704 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
4705 phba->link_flag |= LS_MDS_LOOPBACK;
4706 break;
4707 default:
4708 break;
4709 }
4710
4711 /* Initialize completion status */
4712 mb = &pmb->u.mb;
4713 mb->mbxStatus = MBX_SUCCESS;
4714
4715 /* Parse port fault information field */
4716 lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
4717
4718 /* Parse and translate link attention fields */
4719 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4720 la->eventTag = acqe_fc->event_tag;
4721
4722 if (phba->sli4_hba.link_state.status ==
4723 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
4724 bf_set(lpfc_mbx_read_top_att_type, la,
4725 LPFC_FC_LA_TYPE_UNEXP_WWPN);
4726 } else {
4727 bf_set(lpfc_mbx_read_top_att_type, la,
4728 LPFC_FC_LA_TYPE_LINK_DOWN);
4729 }
4730 /* Invoke the mailbox command callback function */
4731 lpfc_mbx_cmpl_read_topology(phba, pmb);
4732
4733 return;
4734 }
4735
4736 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4737 if (rc == MBX_NOT_FINISHED)
4738 goto out_free_dmabuf;
4739 return;
4740
4741out_free_dmabuf:
4742 kfree(mp);
4743out_free_pmb:
4744 mempool_free(pmb, phba->mbox_mem_pool);
4745}
4746
4747/**
4748 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4749 * @phba: pointer to lpfc hba data structure.
4750 * @acqe_fc: pointer to the async SLI completion queue entry.
4751 *
4752 * This routine is to handle the SLI4 asynchronous SLI events.
4753 **/
4754static void
4755lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4756{
4757 char port_name;
4758 char message[128];
4759 uint8_t status;
4760 uint8_t evt_type;
4761 uint8_t operational = 0;
4762 struct temp_event temp_event_data;
4763 struct lpfc_acqe_misconfigured_event *misconfigured;
4764 struct Scsi_Host *shost;
4765
4766 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4767
4768 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4769 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4770 "x%08x SLI Event Type:%d\n",
4771 acqe_sli->event_data1, acqe_sli->event_data2,
4772 evt_type);
4773
4774 port_name = phba->Port[0];
4775 if (port_name == 0x00)
4776 port_name = '?'; /* get port name is empty */
4777
4778 switch (evt_type) {
4779 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4780 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4781 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4782 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4783
4784 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4785 "3190 Over Temperature:%d Celsius- Port Name %c\n",
4786 acqe_sli->event_data1, port_name);
4787
4788 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
4789 shost = lpfc_shost_from_vport(phba->pport);
4790 fc_host_post_vendor_event(shost, fc_get_event_number(),
4791 sizeof(temp_event_data),
4792 (char *)&temp_event_data,
4793 SCSI_NL_VID_TYPE_PCI
4794 | PCI_VENDOR_ID_EMULEX);
4795 break;
4796 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4797 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4798 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4799 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4800
4801 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4802 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
4803 acqe_sli->event_data1, port_name);
4804
4805 shost = lpfc_shost_from_vport(phba->pport);
4806 fc_host_post_vendor_event(shost, fc_get_event_number(),
4807 sizeof(temp_event_data),
4808 (char *)&temp_event_data,
4809 SCSI_NL_VID_TYPE_PCI
4810 | PCI_VENDOR_ID_EMULEX);
4811 break;
4812 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4813 misconfigured = (struct lpfc_acqe_misconfigured_event *)
4814 &acqe_sli->event_data1;
4815
4816 /* fetch the status for this port */
4817 switch (phba->sli4_hba.lnk_info.lnk_no) {
4818 case LPFC_LINK_NUMBER_0:
4819 status = bf_get(lpfc_sli_misconfigured_port0_state,
4820 &misconfigured->theEvent);
4821 operational = bf_get(lpfc_sli_misconfigured_port0_op,
4822 &misconfigured->theEvent);
4823 break;
4824 case LPFC_LINK_NUMBER_1:
4825 status = bf_get(lpfc_sli_misconfigured_port1_state,
4826 &misconfigured->theEvent);
4827 operational = bf_get(lpfc_sli_misconfigured_port1_op,
4828 &misconfigured->theEvent);
4829 break;
4830 case LPFC_LINK_NUMBER_2:
4831 status = bf_get(lpfc_sli_misconfigured_port2_state,
4832 &misconfigured->theEvent);
4833 operational = bf_get(lpfc_sli_misconfigured_port2_op,
4834 &misconfigured->theEvent);
4835 break;
4836 case LPFC_LINK_NUMBER_3:
4837 status = bf_get(lpfc_sli_misconfigured_port3_state,
4838 &misconfigured->theEvent);
4839 operational = bf_get(lpfc_sli_misconfigured_port3_op,
4840 &misconfigured->theEvent);
4841 break;
4842 default:
4843 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4844 "3296 "
4845 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4846 "event: Invalid link %d",
4847 phba->sli4_hba.lnk_info.lnk_no);
4848 return;
4849 }
4850
4851 /* Skip if optic state unchanged */
4852 if (phba->sli4_hba.lnk_info.optic_state == status)
4853 return;
4854
4855 switch (status) {
4856 case LPFC_SLI_EVENT_STATUS_VALID:
4857 sprintf(message, "Physical Link is functional");
4858 break;
4859 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4860 sprintf(message, "Optics faulted/incorrectly "
4861 "installed/not installed - Reseat optics, "
4862 "if issue not resolved, replace.");
4863 break;
4864 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4865 sprintf(message,
4866 "Optics of two types installed - Remove one "
4867 "optic or install matching pair of optics.");
4868 break;
4869 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4870 sprintf(message, "Incompatible optics - Replace with "
4871 "compatible optics for card to function.");
4872 break;
4873 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4874 sprintf(message, "Unqualified optics - Replace with "
4875 "Avago optics for Warranty and Technical "
4876 "Support - Link is%s operational",
4877 (operational) ? " not" : "");
4878 break;
4879 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4880 sprintf(message, "Uncertified optics - Replace with "
4881 "Avago-certified optics to enable link "
4882 "operation - Link is%s operational",
4883 (operational) ? " not" : "");
4884 break;
4885 default:
4886 /* firmware is reporting a status we don't know about */
4887 sprintf(message, "Unknown event status x%02x", status);
4888 break;
4889 }
4890 phba->sli4_hba.lnk_info.optic_state = status;
4891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4892 "3176 Port Name %c %s\n", port_name, message);
4893 break;
4894 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4895 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4896 "3192 Remote DPort Test Initiated - "
4897 "Event Data1:x%08x Event Data2: x%08x\n",
4898 acqe_sli->event_data1, acqe_sli->event_data2);
4899 break;
4900 default:
4901 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4902 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
4903 "x%08x SLI Event Type:%d\n",
4904 acqe_sli->event_data1, acqe_sli->event_data2,
4905 evt_type);
4906 break;
4907 }
4908}
4909
4910/**
4911 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4912 * @vport: pointer to vport data structure.
4913 *
4914 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4915 * response to a CVL event.
4916 *
4917 * Return the pointer to the ndlp with the vport if successful, otherwise
4918 * return NULL.
4919 **/
4920static struct lpfc_nodelist *
4921lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4922{
4923 struct lpfc_nodelist *ndlp;
4924 struct Scsi_Host *shost;
4925 struct lpfc_hba *phba;
4926
4927 if (!vport)
4928 return NULL;
4929 phba = vport->phba;
4930 if (!phba)
4931 return NULL;
4932 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4933 if (!ndlp) {
4934 /* Cannot find existing Fabric ndlp, so allocate a new one */
4935 ndlp = lpfc_nlp_init(vport, Fabric_DID);
4936 if (!ndlp)
4937 return 0;
4938 /* Set the node type */
4939 ndlp->nlp_type |= NLP_FABRIC;
4940 /* Put ndlp onto node list */
4941 lpfc_enqueue_node(vport, ndlp);
4942 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4943 /* re-setup ndlp without removing from node list */
4944 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4945 if (!ndlp)
4946 return 0;
4947 }
4948 if ((phba->pport->port_state < LPFC_FLOGI) &&
4949 (phba->pport->port_state != LPFC_VPORT_FAILED))
4950 return NULL;
4951 /* If virtual link is not yet instantiated ignore CVL */
4952 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4953 && (vport->port_state != LPFC_VPORT_FAILED))
4954 return NULL;
4955 shost = lpfc_shost_from_vport(vport);
4956 if (!shost)
4957 return NULL;
4958 lpfc_linkdown_port(vport);
4959 lpfc_cleanup_pending_mbox(vport);
4960 spin_lock_irq(shost->host_lock);
4961 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4962 spin_unlock_irq(shost->host_lock);
4963
4964 return ndlp;
4965}
4966
4967/**
4968 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4969 * @vport: pointer to lpfc hba data structure.
4970 *
4971 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4972 * response to a FCF dead event.
4973 **/
4974static void
4975lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4976{
4977 struct lpfc_vport **vports;
4978 int i;
4979
4980 vports = lpfc_create_vport_work_array(phba);
4981 if (vports)
4982 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4983 lpfc_sli4_perform_vport_cvl(vports[i]);
4984 lpfc_destroy_vport_work_array(phba, vports);
4985}
4986
4987/**
4988 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
4989 * @phba: pointer to lpfc hba data structure.
4990 * @acqe_link: pointer to the async fcoe completion queue entry.
4991 *
4992 * This routine is to handle the SLI4 asynchronous fcoe event.
4993 **/
4994static void
4995lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
4996 struct lpfc_acqe_fip *acqe_fip)
4997{
4998 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
4999 int rc;
5000 struct lpfc_vport *vport;
5001 struct lpfc_nodelist *ndlp;
5002 struct Scsi_Host *shost;
5003 int active_vlink_present;
5004 struct lpfc_vport **vports;
5005 int i;
5006
5007 phba->fc_eventTag = acqe_fip->event_tag;
5008 phba->fcoe_eventtag = acqe_fip->event_tag;
5009 switch (event_type) {
5010 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
5011 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
5012 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
5013 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5014 LOG_DISCOVERY,
5015 "2546 New FCF event, evt_tag:x%x, "
5016 "index:x%x\n",
5017 acqe_fip->event_tag,
5018 acqe_fip->index);
5019 else
5020 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5021 LOG_DISCOVERY,
5022 "2788 FCF param modified event, "
5023 "evt_tag:x%x, index:x%x\n",
5024 acqe_fip->event_tag,
5025 acqe_fip->index);
5026 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
5027 /*
5028 * During period of FCF discovery, read the FCF
5029 * table record indexed by the event to update
5030 * FCF roundrobin failover eligible FCF bmask.
5031 */
5032 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5033 LOG_DISCOVERY,
5034 "2779 Read FCF (x%x) for updating "
5035 "roundrobin FCF failover bmask\n",
5036 acqe_fip->index);
5037 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
5038 }
5039
5040 /* If the FCF discovery is in progress, do nothing. */
5041 spin_lock_irq(&phba->hbalock);
5042 if (phba->hba_flag & FCF_TS_INPROG) {
5043 spin_unlock_irq(&phba->hbalock);
5044 break;
5045 }
5046 /* If fast FCF failover rescan event is pending, do nothing */
5047 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
5048 spin_unlock_irq(&phba->hbalock);
5049 break;
5050 }
5051
5052 /* If the FCF has been in discovered state, do nothing. */
5053 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
5054 spin_unlock_irq(&phba->hbalock);
5055 break;
5056 }
5057 spin_unlock_irq(&phba->hbalock);
5058
5059 /* Otherwise, scan the entire FCF table and re-discover SAN */
5060 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5061 "2770 Start FCF table scan per async FCF "
5062 "event, evt_tag:x%x, index:x%x\n",
5063 acqe_fip->event_tag, acqe_fip->index);
5064 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5065 LPFC_FCOE_FCF_GET_FIRST);
5066 if (rc)
5067 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5068 "2547 Issue FCF scan read FCF mailbox "
5069 "command failed (x%x)\n", rc);
5070 break;
5071
5072 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
5073 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5074 "2548 FCF Table full count 0x%x tag 0x%x\n",
5075 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5076 acqe_fip->event_tag);
5077 break;
5078
5079 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
5080 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5081 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5082 "2549 FCF (x%x) disconnected from network, "
5083 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
5084 /*
5085 * If we are in the middle of FCF failover process, clear
5086 * the corresponding FCF bit in the roundrobin bitmap.
5087 */
5088 spin_lock_irq(&phba->hbalock);
5089 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5090 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
5091 spin_unlock_irq(&phba->hbalock);
5092 /* Update FLOGI FCF failover eligible FCF bmask */
5093 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
5094 break;
5095 }
5096 spin_unlock_irq(&phba->hbalock);
5097
5098 /* If the event is not for currently used fcf do nothing */
5099 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
5100 break;
5101
5102 /*
5103 * Otherwise, request the port to rediscover the entire FCF
5104 * table for a fast recovery from case that the current FCF
5105 * is no longer valid as we are not in the middle of FCF
5106 * failover process already.
5107 */
5108 spin_lock_irq(&phba->hbalock);
5109 /* Mark the fast failover process in progress */
5110 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5111 spin_unlock_irq(&phba->hbalock);
5112
5113 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5114 "2771 Start FCF fast failover process due to "
5115 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5116 "\n", acqe_fip->event_tag, acqe_fip->index);
5117 rc = lpfc_sli4_redisc_fcf_table(phba);
5118 if (rc) {
5119 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5120 LOG_DISCOVERY,
5121 "2772 Issue FCF rediscover mailbox "
5122 "command failed, fail through to FCF "
5123 "dead event\n");
5124 spin_lock_irq(&phba->hbalock);
5125 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5126 spin_unlock_irq(&phba->hbalock);
5127 /*
5128 * Last resort will fail over by treating this
5129 * as a link down to FCF registration.
5130 */
5131 lpfc_sli4_fcf_dead_failthrough(phba);
5132 } else {
5133 /* Reset FCF roundrobin bmask for new discovery */
5134 lpfc_sli4_clear_fcf_rr_bmask(phba);
5135 /*
5136 * Handling fast FCF failover to a DEAD FCF event is
5137 * considered equalivant to receiving CVL to all vports.
5138 */
5139 lpfc_sli4_perform_all_vport_cvl(phba);
5140 }
5141 break;
5142 case LPFC_FIP_EVENT_TYPE_CVL:
5143 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5144 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5145 "2718 Clear Virtual Link Received for VPI 0x%x"
5146 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
5147
5148 vport = lpfc_find_vport_by_vpid(phba,
5149 acqe_fip->index);
5150 ndlp = lpfc_sli4_perform_vport_cvl(vport);
5151 if (!ndlp)
5152 break;
5153 active_vlink_present = 0;
5154
5155 vports = lpfc_create_vport_work_array(phba);
5156 if (vports) {
5157 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5158 i++) {
5159 if ((!(vports[i]->fc_flag &
5160 FC_VPORT_CVL_RCVD)) &&
5161 (vports[i]->port_state > LPFC_FDISC)) {
5162 active_vlink_present = 1;
5163 break;
5164 }
5165 }
5166 lpfc_destroy_vport_work_array(phba, vports);
5167 }
5168
5169 /*
5170 * Don't re-instantiate if vport is marked for deletion.
5171 * If we are here first then vport_delete is going to wait
5172 * for discovery to complete.
5173 */
5174 if (!(vport->load_flag & FC_UNLOADING) &&
5175 active_vlink_present) {
5176 /*
5177 * If there are other active VLinks present,
5178 * re-instantiate the Vlink using FDISC.
5179 */
5180 mod_timer(&ndlp->nlp_delayfunc,
5181 jiffies + msecs_to_jiffies(1000));
5182 shost = lpfc_shost_from_vport(vport);
5183 spin_lock_irq(shost->host_lock);
5184 ndlp->nlp_flag |= NLP_DELAY_TMO;
5185 spin_unlock_irq(shost->host_lock);
5186 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5187 vport->port_state = LPFC_FDISC;
5188 } else {
5189 /*
5190 * Otherwise, we request port to rediscover
5191 * the entire FCF table for a fast recovery
5192 * from possible case that the current FCF
5193 * is no longer valid if we are not already
5194 * in the FCF failover process.
5195 */
5196 spin_lock_irq(&phba->hbalock);
5197 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
5198 spin_unlock_irq(&phba->hbalock);
5199 break;
5200 }
5201 /* Mark the fast failover process in progress */
5202 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
5203 spin_unlock_irq(&phba->hbalock);
5204 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5205 LOG_DISCOVERY,
5206 "2773 Start FCF failover per CVL, "
5207 "evt_tag:x%x\n", acqe_fip->event_tag);
5208 rc = lpfc_sli4_redisc_fcf_table(phba);
5209 if (rc) {
5210 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5211 LOG_DISCOVERY,
5212 "2774 Issue FCF rediscover "
5213 "mailbox command failed, "
5214 "through to CVL event\n");
5215 spin_lock_irq(&phba->hbalock);
5216 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
5217 spin_unlock_irq(&phba->hbalock);
5218 /*
5219 * Last resort will be re-try on the
5220 * the current registered FCF entry.
5221 */
5222 lpfc_retry_pport_discovery(phba);
5223 } else
5224 /*
5225 * Reset FCF roundrobin bmask for new
5226 * discovery.
5227 */
5228 lpfc_sli4_clear_fcf_rr_bmask(phba);
5229 }
5230 break;
5231 default:
5232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5233 "0288 Unknown FCoE event type 0x%x event tag "
5234 "0x%x\n", event_type, acqe_fip->event_tag);
5235 break;
5236 }
5237}
5238
5239/**
5240 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5241 * @phba: pointer to lpfc hba data structure.
5242 * @acqe_link: pointer to the async dcbx completion queue entry.
5243 *
5244 * This routine is to handle the SLI4 asynchronous dcbx event.
5245 **/
5246static void
5247lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5248 struct lpfc_acqe_dcbx *acqe_dcbx)
5249{
5250 phba->fc_eventTag = acqe_dcbx->event_tag;
5251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5252 "0290 The SLI4 DCBX asynchronous event is not "
5253 "handled yet\n");
5254}
5255
5256/**
5257 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5258 * @phba: pointer to lpfc hba data structure.
5259 * @acqe_link: pointer to the async grp5 completion queue entry.
5260 *
5261 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5262 * is an asynchronous notified of a logical link speed change. The Port
5263 * reports the logical link speed in units of 10Mbps.
5264 **/
5265static void
5266lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5267 struct lpfc_acqe_grp5 *acqe_grp5)
5268{
5269 uint16_t prev_ll_spd;
5270
5271 phba->fc_eventTag = acqe_grp5->event_tag;
5272 phba->fcoe_eventtag = acqe_grp5->event_tag;
5273 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5274 phba->sli4_hba.link_state.logical_speed =
5275 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
5276 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5277 "2789 GRP5 Async Event: Updating logical link speed "
5278 "from %dMbps to %dMbps\n", prev_ll_spd,
5279 phba->sli4_hba.link_state.logical_speed);
5280}
5281
5282/**
5283 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5284 * @phba: pointer to lpfc hba data structure.
5285 *
5286 * This routine is invoked by the worker thread to process all the pending
5287 * SLI4 asynchronous events.
5288 **/
5289void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5290{
5291 struct lpfc_cq_event *cq_event;
5292
5293 /* First, declare the async event has been handled */
5294 spin_lock_irq(&phba->hbalock);
5295 phba->hba_flag &= ~ASYNC_EVENT;
5296 spin_unlock_irq(&phba->hbalock);
5297 /* Now, handle all the async events */
5298 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5299 /* Get the first event from the head of the event queue */
5300 spin_lock_irq(&phba->hbalock);
5301 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5302 cq_event, struct lpfc_cq_event, list);
5303 spin_unlock_irq(&phba->hbalock);
5304 /* Process the asynchronous event */
5305 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5306 case LPFC_TRAILER_CODE_LINK:
5307 lpfc_sli4_async_link_evt(phba,
5308 &cq_event->cqe.acqe_link);
5309 break;
5310 case LPFC_TRAILER_CODE_FCOE:
5311 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
5312 break;
5313 case LPFC_TRAILER_CODE_DCBX:
5314 lpfc_sli4_async_dcbx_evt(phba,
5315 &cq_event->cqe.acqe_dcbx);
5316 break;
5317 case LPFC_TRAILER_CODE_GRP5:
5318 lpfc_sli4_async_grp5_evt(phba,
5319 &cq_event->cqe.acqe_grp5);
5320 break;
5321 case LPFC_TRAILER_CODE_FC:
5322 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5323 break;
5324 case LPFC_TRAILER_CODE_SLI:
5325 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5326 break;
5327 default:
5328 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5329 "1804 Invalid asynchrous event code: "
5330 "x%x\n", bf_get(lpfc_trailer_code,
5331 &cq_event->cqe.mcqe_cmpl));
5332 break;
5333 }
5334 /* Free the completion event processed to the free pool */
5335 lpfc_sli4_cq_event_release(phba, cq_event);
5336 }
5337}
5338
5339/**
5340 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5341 * @phba: pointer to lpfc hba data structure.
5342 *
5343 * This routine is invoked by the worker thread to process FCF table
5344 * rediscovery pending completion event.
5345 **/
5346void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5347{
5348 int rc;
5349
5350 spin_lock_irq(&phba->hbalock);
5351 /* Clear FCF rediscovery timeout event */
5352 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5353 /* Clear driver fast failover FCF record flag */
5354 phba->fcf.failover_rec.flag = 0;
5355 /* Set state for FCF fast failover */
5356 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5357 spin_unlock_irq(&phba->hbalock);
5358
5359 /* Scan FCF table from the first entry to re-discover SAN */
5360 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5361 "2777 Start post-quiescent FCF table scan\n");
5362 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
5363 if (rc)
5364 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5365 "2747 Issue FCF scan read FCF mailbox "
5366 "command failed 0x%x\n", rc);
5367}
5368
5369/**
5370 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5371 * @phba: pointer to lpfc hba data structure.
5372 * @dev_grp: The HBA PCI-Device group number.
5373 *
5374 * This routine is invoked to set up the per HBA PCI-Device group function
5375 * API jump table entries.
5376 *
5377 * Return: 0 if success, otherwise -ENODEV
5378 **/
5379int
5380lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5381{
5382 int rc;
5383
5384 /* Set up lpfc PCI-device group */
5385 phba->pci_dev_grp = dev_grp;
5386
5387 /* The LPFC_PCI_DEV_OC uses SLI4 */
5388 if (dev_grp == LPFC_PCI_DEV_OC)
5389 phba->sli_rev = LPFC_SLI_REV4;
5390
5391 /* Set up device INIT API function jump table */
5392 rc = lpfc_init_api_table_setup(phba, dev_grp);
5393 if (rc)
5394 return -ENODEV;
5395 /* Set up SCSI API function jump table */
5396 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5397 if (rc)
5398 return -ENODEV;
5399 /* Set up SLI API function jump table */
5400 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5401 if (rc)
5402 return -ENODEV;
5403 /* Set up MBOX API function jump table */
5404 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5405 if (rc)
5406 return -ENODEV;
5407
5408 return 0;
5409}
5410
5411/**
5412 * lpfc_log_intr_mode - Log the active interrupt mode
5413 * @phba: pointer to lpfc hba data structure.
5414 * @intr_mode: active interrupt mode adopted.
5415 *
5416 * This routine it invoked to log the currently used active interrupt mode
5417 * to the device.
5418 **/
5419static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
5420{
5421 switch (intr_mode) {
5422 case 0:
5423 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5424 "0470 Enable INTx interrupt mode.\n");
5425 break;
5426 case 1:
5427 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5428 "0481 Enabled MSI interrupt mode.\n");
5429 break;
5430 case 2:
5431 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5432 "0480 Enabled MSI-X interrupt mode.\n");
5433 break;
5434 default:
5435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5436 "0482 Illegal interrupt mode.\n");
5437 break;
5438 }
5439 return;
5440}
5441
5442/**
5443 * lpfc_enable_pci_dev - Enable a generic PCI device.
5444 * @phba: pointer to lpfc hba data structure.
5445 *
5446 * This routine is invoked to enable the PCI device that is common to all
5447 * PCI devices.
5448 *
5449 * Return codes
5450 * 0 - successful
5451 * other values - error
5452 **/
5453static int
5454lpfc_enable_pci_dev(struct lpfc_hba *phba)
5455{
5456 struct pci_dev *pdev;
5457
5458 /* Obtain PCI device reference */
5459 if (!phba->pcidev)
5460 goto out_error;
5461 else
5462 pdev = phba->pcidev;
5463 /* Enable PCI device */
5464 if (pci_enable_device_mem(pdev))
5465 goto out_error;
5466 /* Request PCI resource for the device */
5467 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
5468 goto out_disable_device;
5469 /* Set up device as PCI master and save state for EEH */
5470 pci_set_master(pdev);
5471 pci_try_set_mwi(pdev);
5472 pci_save_state(pdev);
5473
5474 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5475 if (pci_is_pcie(pdev))
5476 pdev->needs_freset = 1;
5477
5478 return 0;
5479
5480out_disable_device:
5481 pci_disable_device(pdev);
5482out_error:
5483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5484 "1401 Failed to enable pci device\n");
5485 return -ENODEV;
5486}
5487
5488/**
5489 * lpfc_disable_pci_dev - Disable a generic PCI device.
5490 * @phba: pointer to lpfc hba data structure.
5491 *
5492 * This routine is invoked to disable the PCI device that is common to all
5493 * PCI devices.
5494 **/
5495static void
5496lpfc_disable_pci_dev(struct lpfc_hba *phba)
5497{
5498 struct pci_dev *pdev;
5499
5500 /* Obtain PCI device reference */
5501 if (!phba->pcidev)
5502 return;
5503 else
5504 pdev = phba->pcidev;
5505 /* Release PCI resource and disable PCI device */
5506 pci_release_mem_regions(pdev);
5507 pci_disable_device(pdev);
5508
5509 return;
5510}
5511
5512/**
5513 * lpfc_reset_hba - Reset a hba
5514 * @phba: pointer to lpfc hba data structure.
5515 *
5516 * This routine is invoked to reset a hba device. It brings the HBA
5517 * offline, performs a board restart, and then brings the board back
5518 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5519 * on outstanding mailbox commands.
5520 **/
5521void
5522lpfc_reset_hba(struct lpfc_hba *phba)
5523{
5524 /* If resets are disabled then set error state and return. */
5525 if (!phba->cfg_enable_hba_reset) {
5526 phba->link_state = LPFC_HBA_ERROR;
5527 return;
5528 }
5529 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5530 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5531 else
5532 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
5533 lpfc_offline(phba);
5534 lpfc_sli_brdrestart(phba);
5535 lpfc_online(phba);
5536 lpfc_unblock_mgmt_io(phba);
5537}
5538
5539/**
5540 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5541 * @phba: pointer to lpfc hba data structure.
5542 *
5543 * This function enables the PCI SR-IOV virtual functions to a physical
5544 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5545 * enable the number of virtual functions to the physical function. As
5546 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5547 * API call does not considered as an error condition for most of the device.
5548 **/
5549uint16_t
5550lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5551{
5552 struct pci_dev *pdev = phba->pcidev;
5553 uint16_t nr_virtfn;
5554 int pos;
5555
5556 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5557 if (pos == 0)
5558 return 0;
5559
5560 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5561 return nr_virtfn;
5562}
5563
5564/**
5565 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5566 * @phba: pointer to lpfc hba data structure.
5567 * @nr_vfn: number of virtual functions to be enabled.
5568 *
5569 * This function enables the PCI SR-IOV virtual functions to a physical
5570 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5571 * enable the number of virtual functions to the physical function. As
5572 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5573 * API call does not considered as an error condition for most of the device.
5574 **/
5575int
5576lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5577{
5578 struct pci_dev *pdev = phba->pcidev;
5579 uint16_t max_nr_vfn;
5580 int rc;
5581
5582 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5583 if (nr_vfn > max_nr_vfn) {
5584 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5585 "3057 Requested vfs (%d) greater than "
5586 "supported vfs (%d)", nr_vfn, max_nr_vfn);
5587 return -EINVAL;
5588 }
5589
5590 rc = pci_enable_sriov(pdev, nr_vfn);
5591 if (rc) {
5592 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5593 "2806 Failed to enable sriov on this device "
5594 "with vfn number nr_vf:%d, rc:%d\n",
5595 nr_vfn, rc);
5596 } else
5597 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5598 "2807 Successful enable sriov on this device "
5599 "with vfn number nr_vf:%d\n", nr_vfn);
5600 return rc;
5601}
5602
5603/**
5604 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5605 * @phba: pointer to lpfc hba data structure.
5606 *
5607 * This routine is invoked to set up the driver internal resources before the
5608 * device specific resource setup to support the HBA device it attached to.
5609 *
5610 * Return codes
5611 * 0 - successful
5612 * other values - error
5613 **/
5614static int
5615lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5616{
5617 struct lpfc_sli *psli = &phba->sli;
5618
5619 /*
5620 * Driver resources common to all SLI revisions
5621 */
5622 atomic_set(&phba->fast_event_count, 0);
5623 spin_lock_init(&phba->hbalock);
5624
5625 /* Initialize ndlp management spinlock */
5626 spin_lock_init(&phba->ndlp_lock);
5627
5628 INIT_LIST_HEAD(&phba->port_list);
5629 INIT_LIST_HEAD(&phba->work_list);
5630 init_waitqueue_head(&phba->wait_4_mlo_m_q);
5631
5632 /* Initialize the wait queue head for the kernel thread */
5633 init_waitqueue_head(&phba->work_waitq);
5634
5635 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5636 "1403 Protocols supported %s %s %s\n",
5637 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5638 "SCSI" : " "),
5639 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
5640 "NVME" : " "),
5641 (phba->nvmet_support ? "NVMET" : " "));
5642
5643 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5644 /* Initialize the scsi buffer list used by driver for scsi IO */
5645 spin_lock_init(&phba->scsi_buf_list_get_lock);
5646 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5647 spin_lock_init(&phba->scsi_buf_list_put_lock);
5648 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5649 }
5650
5651 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5652 (phba->nvmet_support == 0)) {
5653 /* Initialize the NVME buffer list used by driver for NVME IO */
5654 spin_lock_init(&phba->nvme_buf_list_get_lock);
5655 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5656 phba->get_nvme_bufs = 0;
5657 spin_lock_init(&phba->nvme_buf_list_put_lock);
5658 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5659 phba->put_nvme_bufs = 0;
5660 }
5661
5662 /* Initialize the fabric iocb list */
5663 INIT_LIST_HEAD(&phba->fabric_iocb_list);
5664
5665 /* Initialize list to save ELS buffers */
5666 INIT_LIST_HEAD(&phba->elsbuf);
5667
5668 /* Initialize FCF connection rec list */
5669 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5670
5671 /* Initialize OAS configuration list */
5672 spin_lock_init(&phba->devicelock);
5673 INIT_LIST_HEAD(&phba->luns);
5674
5675 /* MBOX heartbeat timer */
5676 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
5677 /* Fabric block timer */
5678 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
5679 /* EA polling mode timer */
5680 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
5681 /* Heartbeat timer */
5682 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
5683
5684 return 0;
5685}
5686
5687/**
5688 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5689 * @phba: pointer to lpfc hba data structure.
5690 *
5691 * This routine is invoked to set up the driver internal resources specific to
5692 * support the SLI-3 HBA device it attached to.
5693 *
5694 * Return codes
5695 * 0 - successful
5696 * other values - error
5697 **/
5698static int
5699lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5700{
5701 int rc;
5702
5703 /*
5704 * Initialize timers used by driver
5705 */
5706
5707 /* FCP polling mode timer */
5708 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
5709
5710 /* Host attention work mask setup */
5711 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5712 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5713
5714 /* Get all the module params for configuring this host */
5715 lpfc_get_cfgparam(phba);
5716 /* Set up phase-1 common device driver resources */
5717
5718 rc = lpfc_setup_driver_resource_phase1(phba);
5719 if (rc)
5720 return -ENODEV;
5721
5722 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5723 phba->menlo_flag |= HBA_MENLO_SUPPORT;
5724 /* check for menlo minimum sg count */
5725 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5726 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5727 }
5728
5729 if (!phba->sli.sli3_ring)
5730 phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
5731 sizeof(struct lpfc_sli_ring),
5732 GFP_KERNEL);
5733 if (!phba->sli.sli3_ring)
5734 return -ENOMEM;
5735
5736 /*
5737 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
5738 * used to create the sg_dma_buf_pool must be dynamically calculated.
5739 */
5740
5741 /* Initialize the host templates the configured values. */
5742 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5743 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5744 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5745
5746 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5747 if (phba->cfg_enable_bg) {
5748 /*
5749 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5750 * the FCP rsp, and a BDE for each. Sice we have no control
5751 * over how many protection data segments the SCSI Layer
5752 * will hand us (ie: there could be one for every block
5753 * in the IO), we just allocate enough BDEs to accomidate
5754 * our max amount and we need to limit lpfc_sg_seg_cnt to
5755 * minimize the risk of running out.
5756 */
5757 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5758 sizeof(struct fcp_rsp) +
5759 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5760
5761 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5762 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5763
5764 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5765 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5766 } else {
5767 /*
5768 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5769 * the FCP rsp, a BDE for each, and a BDE for up to
5770 * cfg_sg_seg_cnt data segments.
5771 */
5772 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5773 sizeof(struct fcp_rsp) +
5774 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5775
5776 /* Total BDEs in BPL for scsi_sg_list */
5777 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5778 }
5779
5780 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5781 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5782 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5783 phba->cfg_total_seg_cnt);
5784
5785 phba->max_vpi = LPFC_MAX_VPI;
5786 /* This will be set to correct value after config_port mbox */
5787 phba->max_vports = 0;
5788
5789 /*
5790 * Initialize the SLI Layer to run with lpfc HBAs.
5791 */
5792 lpfc_sli_setup(phba);
5793 lpfc_sli_queue_init(phba);
5794
5795 /* Allocate device driver memory */
5796 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5797 return -ENOMEM;
5798
5799 /*
5800 * Enable sr-iov virtual functions if supported and configured
5801 * through the module parameter.
5802 */
5803 if (phba->cfg_sriov_nr_virtfn > 0) {
5804 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5805 phba->cfg_sriov_nr_virtfn);
5806 if (rc) {
5807 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5808 "2808 Requested number of SR-IOV "
5809 "virtual functions (%d) is not "
5810 "supported\n",
5811 phba->cfg_sriov_nr_virtfn);
5812 phba->cfg_sriov_nr_virtfn = 0;
5813 }
5814 }
5815
5816 return 0;
5817}
5818
5819/**
5820 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5821 * @phba: pointer to lpfc hba data structure.
5822 *
5823 * This routine is invoked to unset the driver internal resources set up
5824 * specific for supporting the SLI-3 HBA device it attached to.
5825 **/
5826static void
5827lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5828{
5829 /* Free device driver memory allocated */
5830 lpfc_mem_free_all(phba);
5831
5832 return;
5833}
5834
5835/**
5836 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5837 * @phba: pointer to lpfc hba data structure.
5838 *
5839 * This routine is invoked to set up the driver internal resources specific to
5840 * support the SLI-4 HBA device it attached to.
5841 *
5842 * Return codes
5843 * 0 - successful
5844 * other values - error
5845 **/
5846static int
5847lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5848{
5849 LPFC_MBOXQ_t *mboxq;
5850 MAILBOX_t *mb;
5851 int rc, i, max_buf_size;
5852 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5853 struct lpfc_mqe *mqe;
5854 int longs;
5855 int fof_vectors = 0;
5856 int extra;
5857 uint64_t wwn;
5858 u32 if_type;
5859 u32 if_fam;
5860
5861 phba->sli4_hba.num_online_cpu = num_online_cpus();
5862 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5863 phba->sli4_hba.curr_disp_cpu = 0;
5864
5865 /* Get all the module params for configuring this host */
5866 lpfc_get_cfgparam(phba);
5867
5868 /* Set up phase-1 common device driver resources */
5869 rc = lpfc_setup_driver_resource_phase1(phba);
5870 if (rc)
5871 return -ENODEV;
5872
5873 /* Before proceed, wait for POST done and device ready */
5874 rc = lpfc_sli4_post_status_check(phba);
5875 if (rc)
5876 return -ENODEV;
5877
5878 /*
5879 * Initialize timers used by driver
5880 */
5881
5882 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
5883
5884 /* FCF rediscover timer */
5885 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
5886
5887 /*
5888 * Control structure for handling external multi-buffer mailbox
5889 * command pass-through.
5890 */
5891 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5892 sizeof(struct lpfc_mbox_ext_buf_ctx));
5893 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5894
5895 phba->max_vpi = LPFC_MAX_VPI;
5896
5897 /* This will be set to correct value after the read_config mbox */
5898 phba->max_vports = 0;
5899
5900 /* Program the default value of vlan_id and fc_map */
5901 phba->valid_vlan = 0;
5902 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5903 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5904 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5905
5906 /*
5907 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5908 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5909 * The WQ create will allocate the ring.
5910 */
5911
5912 /*
5913 * 1 for cmd, 1 for rsp, NVME adds an extra one
5914 * for boundary conditions in its max_sgl_segment template.
5915 */
5916 extra = 2;
5917 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
5918 extra++;
5919
5920 /*
5921 * It doesn't matter what family our adapter is in, we are
5922 * limited to 2 Pages, 512 SGEs, for our SGL.
5923 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5924 */
5925 max_buf_size = (2 * SLI4_PAGE_SIZE);
5926 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - extra)
5927 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - extra;
5928
5929 /*
5930 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5931 * used to create the sg_dma_buf_pool must be calculated.
5932 */
5933 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5934 /*
5935 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5936 * the FCP rsp, and a SGE. Sice we have no control
5937 * over how many protection segments the SCSI Layer
5938 * will hand us (ie: there could be one for every block
5939 * in the IO), just allocate enough SGEs to accomidate
5940 * our max amount and we need to limit lpfc_sg_seg_cnt
5941 * to minimize the risk of running out.
5942 */
5943 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5944 sizeof(struct fcp_rsp) + max_buf_size;
5945
5946 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5947 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5948
5949 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5950 phba->cfg_sg_seg_cnt =
5951 LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5952 } else {
5953 /*
5954 * The scsi_buf for a regular I/O holds the FCP cmnd,
5955 * the FCP rsp, a SGE for each, and a SGE for up to
5956 * cfg_sg_seg_cnt data segments.
5957 */
5958 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5959 sizeof(struct fcp_rsp) +
5960 ((phba->cfg_sg_seg_cnt + extra) *
5961 sizeof(struct sli4_sge));
5962
5963 /* Total SGEs for scsi_sg_list */
5964 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
5965
5966 /*
5967 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
5968 * need to post 1 page for the SGL.
5969 */
5970 }
5971
5972 /* Initialize the host templates with the updated values. */
5973 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5974 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5975 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5976
5977 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5978 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5979 else
5980 phba->cfg_sg_dma_buf_size =
5981 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5982
5983 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5984 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5985 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5986 phba->cfg_total_seg_cnt);
5987
5988 /* Initialize buffer queue management fields */
5989 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
5990 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5991 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5992
5993 /*
5994 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5995 */
5996 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5997 /* Initialize the Abort scsi buffer list used by driver */
5998 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5999 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
6000 }
6001
6002 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6003 /* Initialize the Abort nvme buffer list used by driver */
6004 spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
6005 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
6006 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
6007 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
6008 }
6009
6010 /* This abort list used by worker thread */
6011 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
6012 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
6013
6014 /*
6015 * Initialize driver internal slow-path work queues
6016 */
6017
6018 /* Driver internel slow-path CQ Event pool */
6019 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6020 /* Response IOCB work queue list */
6021 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
6022 /* Asynchronous event CQ Event work queue list */
6023 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
6024 /* Fast-path XRI aborted CQ Event work queue list */
6025 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
6026 /* Slow-path XRI aborted CQ Event work queue list */
6027 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6028 /* Receive queue CQ Event work queue list */
6029 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6030
6031 /* Initialize extent block lists. */
6032 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6033 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6034 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6035 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6036
6037 /* Initialize mboxq lists. If the early init routines fail
6038 * these lists need to be correctly initialized.
6039 */
6040 INIT_LIST_HEAD(&phba->sli.mboxq);
6041 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6042
6043 /* initialize optic_state to 0xFF */
6044 phba->sli4_hba.lnk_info.optic_state = 0xff;
6045
6046 /* Allocate device driver memory */
6047 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6048 if (rc)
6049 return -ENOMEM;
6050
6051 /* IF Type 2 ports get initialized now. */
6052 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
6053 LPFC_SLI_INTF_IF_TYPE_2) {
6054 rc = lpfc_pci_function_reset(phba);
6055 if (unlikely(rc)) {
6056 rc = -ENODEV;
6057 goto out_free_mem;
6058 }
6059 phba->temp_sensor_support = 1;
6060 }
6061
6062 /* Create the bootstrap mailbox command */
6063 rc = lpfc_create_bootstrap_mbox(phba);
6064 if (unlikely(rc))
6065 goto out_free_mem;
6066
6067 /* Set up the host's endian order with the device. */
6068 rc = lpfc_setup_endian_order(phba);
6069 if (unlikely(rc))
6070 goto out_free_bsmbx;
6071
6072 /* Set up the hba's configuration parameters. */
6073 rc = lpfc_sli4_read_config(phba);
6074 if (unlikely(rc))
6075 goto out_free_bsmbx;
6076 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6077 if (unlikely(rc))
6078 goto out_free_bsmbx;
6079
6080 /* IF Type 0 ports get initialized now. */
6081 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6082 LPFC_SLI_INTF_IF_TYPE_0) {
6083 rc = lpfc_pci_function_reset(phba);
6084 if (unlikely(rc))
6085 goto out_free_bsmbx;
6086 }
6087
6088 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6089 GFP_KERNEL);
6090 if (!mboxq) {
6091 rc = -ENOMEM;
6092 goto out_free_bsmbx;
6093 }
6094
6095 /* Check for NVMET being configured */
6096 phba->nvmet_support = 0;
6097 if (lpfc_enable_nvmet_cnt) {
6098
6099 /* First get WWN of HBA instance */
6100 lpfc_read_nv(phba, mboxq);
6101 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6102 if (rc != MBX_SUCCESS) {
6103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6104 "6016 Mailbox failed , mbxCmd x%x "
6105 "READ_NV, mbxStatus x%x\n",
6106 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6107 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6108 mempool_free(mboxq, phba->mbox_mem_pool);
6109 rc = -EIO;
6110 goto out_free_bsmbx;
6111 }
6112 mb = &mboxq->u.mb;
6113 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6114 sizeof(uint64_t));
6115 wwn = cpu_to_be64(wwn);
6116 phba->sli4_hba.wwnn.u.name = wwn;
6117 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6118 sizeof(uint64_t));
6119 /* wwn is WWPN of HBA instance */
6120 wwn = cpu_to_be64(wwn);
6121 phba->sli4_hba.wwpn.u.name = wwn;
6122
6123 /* Check to see if it matches any module parameter */
6124 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6125 if (wwn == lpfc_enable_nvmet[i]) {
6126#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
6127 if (lpfc_nvmet_mem_alloc(phba))
6128 break;
6129
6130 phba->nvmet_support = 1; /* a match */
6131
6132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6133 "6017 NVME Target %016llx\n",
6134 wwn);
6135#else
6136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6137 "6021 Can't enable NVME Target."
6138 " NVME_TARGET_FC infrastructure"
6139 " is not in kernel\n");
6140#endif
6141 break;
6142 }
6143 }
6144 }
6145
6146 lpfc_nvme_mod_param_dep(phba);
6147
6148 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
6149 lpfc_supported_pages(mboxq);
6150 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6151 if (!rc) {
6152 mqe = &mboxq->u.mqe;
6153 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6154 LPFC_MAX_SUPPORTED_PAGES);
6155 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6156 switch (pn_page[i]) {
6157 case LPFC_SLI4_PARAMETERS:
6158 phba->sli4_hba.pc_sli4_params.supported = 1;
6159 break;
6160 default:
6161 break;
6162 }
6163 }
6164 /* Read the port's SLI4 Parameters capabilities if supported. */
6165 if (phba->sli4_hba.pc_sli4_params.supported)
6166 rc = lpfc_pc_sli4_params_get(phba, mboxq);
6167 if (rc) {
6168 mempool_free(mboxq, phba->mbox_mem_pool);
6169 rc = -EIO;
6170 goto out_free_bsmbx;
6171 }
6172 }
6173
6174 /*
6175 * Get sli4 parameters that override parameters from Port capabilities.
6176 * If this call fails, it isn't critical unless the SLI4 parameters come
6177 * back in conflict.
6178 */
6179 rc = lpfc_get_sli4_parameters(phba, mboxq);
6180 if (rc) {
6181 if_type = bf_get(lpfc_sli_intf_if_type,
6182 &phba->sli4_hba.sli_intf);
6183 if_fam = bf_get(lpfc_sli_intf_sli_family,
6184 &phba->sli4_hba.sli_intf);
6185 if (phba->sli4_hba.extents_in_use &&
6186 phba->sli4_hba.rpi_hdrs_in_use) {
6187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6188 "2999 Unsupported SLI4 Parameters "
6189 "Extents and RPI headers enabled.\n");
6190 if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6191 if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
6192 mempool_free(mboxq, phba->mbox_mem_pool);
6193 rc = -EIO;
6194 goto out_free_bsmbx;
6195 }
6196 }
6197 if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6198 if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
6199 mempool_free(mboxq, phba->mbox_mem_pool);
6200 rc = -EIO;
6201 goto out_free_bsmbx;
6202 }
6203 }
6204
6205 mempool_free(mboxq, phba->mbox_mem_pool);
6206
6207 /* Verify OAS is supported */
6208 lpfc_sli4_oas_verify(phba);
6209 if (phba->cfg_fof)
6210 fof_vectors = 1;
6211
6212 /* Verify all the SLI4 queues */
6213 rc = lpfc_sli4_queue_verify(phba);
6214 if (rc)
6215 goto out_free_bsmbx;
6216
6217 /* Create driver internal CQE event pool */
6218 rc = lpfc_sli4_cq_event_pool_create(phba);
6219 if (rc)
6220 goto out_free_bsmbx;
6221
6222 /* Initialize sgl lists per host */
6223 lpfc_init_sgl_list(phba);
6224
6225 /* Allocate and initialize active sgl array */
6226 rc = lpfc_init_active_sgl_array(phba);
6227 if (rc) {
6228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6229 "1430 Failed to initialize sgl list.\n");
6230 goto out_destroy_cq_event_pool;
6231 }
6232 rc = lpfc_sli4_init_rpi_hdrs(phba);
6233 if (rc) {
6234 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6235 "1432 Failed to initialize rpi headers.\n");
6236 goto out_free_active_sgl;
6237 }
6238
6239 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
6240 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
6241 phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
6242 GFP_KERNEL);
6243 if (!phba->fcf.fcf_rr_bmask) {
6244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6245 "2759 Failed allocate memory for FCF round "
6246 "robin failover bmask\n");
6247 rc = -ENOMEM;
6248 goto out_remove_rpi_hdrs;
6249 }
6250
6251 phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6252 sizeof(struct lpfc_hba_eq_hdl),
6253 GFP_KERNEL);
6254 if (!phba->sli4_hba.hba_eq_hdl) {
6255 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6256 "2572 Failed allocate memory for "
6257 "fast-path per-EQ handle array\n");
6258 rc = -ENOMEM;
6259 goto out_free_fcf_rr_bmask;
6260 }
6261
6262 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6263 sizeof(struct lpfc_vector_map_info),
6264 GFP_KERNEL);
6265 if (!phba->sli4_hba.cpu_map) {
6266 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6267 "3327 Failed allocate memory for msi-x "
6268 "interrupt vector mapping\n");
6269 rc = -ENOMEM;
6270 goto out_free_hba_eq_hdl;
6271 }
6272 if (lpfc_used_cpu == NULL) {
6273 lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6274 GFP_KERNEL);
6275 if (!lpfc_used_cpu) {
6276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6277 "3335 Failed allocate memory for msi-x "
6278 "interrupt vector mapping\n");
6279 kfree(phba->sli4_hba.cpu_map);
6280 rc = -ENOMEM;
6281 goto out_free_hba_eq_hdl;
6282 }
6283 for (i = 0; i < lpfc_present_cpu; i++)
6284 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6285 }
6286
6287 /*
6288 * Enable sr-iov virtual functions if supported and configured
6289 * through the module parameter.
6290 */
6291 if (phba->cfg_sriov_nr_virtfn > 0) {
6292 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6293 phba->cfg_sriov_nr_virtfn);
6294 if (rc) {
6295 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6296 "3020 Requested number of SR-IOV "
6297 "virtual functions (%d) is not "
6298 "supported\n",
6299 phba->cfg_sriov_nr_virtfn);
6300 phba->cfg_sriov_nr_virtfn = 0;
6301 }
6302 }
6303
6304 return 0;
6305
6306out_free_hba_eq_hdl:
6307 kfree(phba->sli4_hba.hba_eq_hdl);
6308out_free_fcf_rr_bmask:
6309 kfree(phba->fcf.fcf_rr_bmask);
6310out_remove_rpi_hdrs:
6311 lpfc_sli4_remove_rpi_hdrs(phba);
6312out_free_active_sgl:
6313 lpfc_free_active_sgl(phba);
6314out_destroy_cq_event_pool:
6315 lpfc_sli4_cq_event_pool_destroy(phba);
6316out_free_bsmbx:
6317 lpfc_destroy_bootstrap_mbox(phba);
6318out_free_mem:
6319 lpfc_mem_free(phba);
6320 return rc;
6321}
6322
6323/**
6324 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6325 * @phba: pointer to lpfc hba data structure.
6326 *
6327 * This routine is invoked to unset the driver internal resources set up
6328 * specific for supporting the SLI-4 HBA device it attached to.
6329 **/
6330static void
6331lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6332{
6333 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6334
6335 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6336 kfree(phba->sli4_hba.cpu_map);
6337 phba->sli4_hba.num_present_cpu = 0;
6338 phba->sli4_hba.num_online_cpu = 0;
6339 phba->sli4_hba.curr_disp_cpu = 0;
6340
6341 /* Free memory allocated for fast-path work queue handles */
6342 kfree(phba->sli4_hba.hba_eq_hdl);
6343
6344 /* Free the allocated rpi headers. */
6345 lpfc_sli4_remove_rpi_hdrs(phba);
6346 lpfc_sli4_remove_rpis(phba);
6347
6348 /* Free eligible FCF index bmask */
6349 kfree(phba->fcf.fcf_rr_bmask);
6350
6351 /* Free the ELS sgl list */
6352 lpfc_free_active_sgl(phba);
6353 lpfc_free_els_sgl_list(phba);
6354 lpfc_free_nvmet_sgl_list(phba);
6355
6356 /* Free the completion queue EQ event pool */
6357 lpfc_sli4_cq_event_release_all(phba);
6358 lpfc_sli4_cq_event_pool_destroy(phba);
6359
6360 /* Release resource identifiers. */
6361 lpfc_sli4_dealloc_resource_identifiers(phba);
6362
6363 /* Free the bsmbx region. */
6364 lpfc_destroy_bootstrap_mbox(phba);
6365
6366 /* Free the SLI Layer memory with SLI4 HBAs */
6367 lpfc_mem_free_all(phba);
6368
6369 /* Free the current connect table */
6370 list_for_each_entry_safe(conn_entry, next_conn_entry,
6371 &phba->fcf_conn_rec_list, list) {
6372 list_del_init(&conn_entry->list);
6373 kfree(conn_entry);
6374 }
6375
6376 return;
6377}
6378
6379/**
6380 * lpfc_init_api_table_setup - Set up init api function jump table
6381 * @phba: The hba struct for which this call is being executed.
6382 * @dev_grp: The HBA PCI-Device group number.
6383 *
6384 * This routine sets up the device INIT interface API function jump table
6385 * in @phba struct.
6386 *
6387 * Returns: 0 - success, -ENODEV - failure.
6388 **/
6389int
6390lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6391{
6392 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6393 phba->lpfc_hba_down_link = lpfc_hba_down_link;
6394 phba->lpfc_selective_reset = lpfc_selective_reset;
6395 switch (dev_grp) {
6396 case LPFC_PCI_DEV_LP:
6397 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6398 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6399 phba->lpfc_stop_port = lpfc_stop_port_s3;
6400 break;
6401 case LPFC_PCI_DEV_OC:
6402 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6403 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6404 phba->lpfc_stop_port = lpfc_stop_port_s4;
6405 break;
6406 default:
6407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6408 "1431 Invalid HBA PCI-device group: 0x%x\n",
6409 dev_grp);
6410 return -ENODEV;
6411 break;
6412 }
6413 return 0;
6414}
6415
6416/**
6417 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6418 * @phba: pointer to lpfc hba data structure.
6419 *
6420 * This routine is invoked to set up the driver internal resources after the
6421 * device specific resource setup to support the HBA device it attached to.
6422 *
6423 * Return codes
6424 * 0 - successful
6425 * other values - error
6426 **/
6427static int
6428lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6429{
6430 int error;
6431
6432 /* Startup the kernel thread for this host adapter. */
6433 phba->worker_thread = kthread_run(lpfc_do_work, phba,
6434 "lpfc_worker_%d", phba->brd_no);
6435 if (IS_ERR(phba->worker_thread)) {
6436 error = PTR_ERR(phba->worker_thread);
6437 return error;
6438 }
6439
6440 /* The lpfc_wq workqueue for deferred irq use, is only used for SLI4 */
6441 if (phba->sli_rev == LPFC_SLI_REV4)
6442 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6443 else
6444 phba->wq = NULL;
6445
6446 return 0;
6447}
6448
6449/**
6450 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6451 * @phba: pointer to lpfc hba data structure.
6452 *
6453 * This routine is invoked to unset the driver internal resources set up after
6454 * the device specific resource setup for supporting the HBA device it
6455 * attached to.
6456 **/
6457static void
6458lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6459{
6460 if (phba->wq) {
6461 flush_workqueue(phba->wq);
6462 destroy_workqueue(phba->wq);
6463 phba->wq = NULL;
6464 }
6465
6466 /* Stop kernel worker thread */
6467 if (phba->worker_thread)
6468 kthread_stop(phba->worker_thread);
6469}
6470
6471/**
6472 * lpfc_free_iocb_list - Free iocb list.
6473 * @phba: pointer to lpfc hba data structure.
6474 *
6475 * This routine is invoked to free the driver's IOCB list and memory.
6476 **/
6477void
6478lpfc_free_iocb_list(struct lpfc_hba *phba)
6479{
6480 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6481
6482 spin_lock_irq(&phba->hbalock);
6483 list_for_each_entry_safe(iocbq_entry, iocbq_next,
6484 &phba->lpfc_iocb_list, list) {
6485 list_del(&iocbq_entry->list);
6486 kfree(iocbq_entry);
6487 phba->total_iocbq_bufs--;
6488 }
6489 spin_unlock_irq(&phba->hbalock);
6490
6491 return;
6492}
6493
6494/**
6495 * lpfc_init_iocb_list - Allocate and initialize iocb list.
6496 * @phba: pointer to lpfc hba data structure.
6497 *
6498 * This routine is invoked to allocate and initizlize the driver's IOCB
6499 * list and set up the IOCB tag array accordingly.
6500 *
6501 * Return codes
6502 * 0 - successful
6503 * other values - error
6504 **/
6505int
6506lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6507{
6508 struct lpfc_iocbq *iocbq_entry = NULL;
6509 uint16_t iotag;
6510 int i;
6511
6512 /* Initialize and populate the iocb list per host. */
6513 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
6514 for (i = 0; i < iocb_count; i++) {
6515 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
6516 if (iocbq_entry == NULL) {
6517 printk(KERN_ERR "%s: only allocated %d iocbs of "
6518 "expected %d count. Unloading driver.\n",
6519 __func__, i, LPFC_IOCB_LIST_CNT);
6520 goto out_free_iocbq;
6521 }
6522
6523 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6524 if (iotag == 0) {
6525 kfree(iocbq_entry);
6526 printk(KERN_ERR "%s: failed to allocate IOTAG. "
6527 "Unloading driver.\n", __func__);
6528 goto out_free_iocbq;
6529 }
6530 iocbq_entry->sli4_lxritag = NO_XRI;
6531 iocbq_entry->sli4_xritag = NO_XRI;
6532
6533 spin_lock_irq(&phba->hbalock);
6534 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6535 phba->total_iocbq_bufs++;
6536 spin_unlock_irq(&phba->hbalock);
6537 }
6538
6539 return 0;
6540
6541out_free_iocbq:
6542 lpfc_free_iocb_list(phba);
6543
6544 return -ENOMEM;
6545}
6546
6547/**
6548 * lpfc_free_sgl_list - Free a given sgl list.
6549 * @phba: pointer to lpfc hba data structure.
6550 * @sglq_list: pointer to the head of sgl list.
6551 *
6552 * This routine is invoked to free a give sgl list and memory.
6553 **/
6554void
6555lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
6556{
6557 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6558
6559 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6560 list_del(&sglq_entry->list);
6561 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6562 kfree(sglq_entry);
6563 }
6564}
6565
6566/**
6567 * lpfc_free_els_sgl_list - Free els sgl list.
6568 * @phba: pointer to lpfc hba data structure.
6569 *
6570 * This routine is invoked to free the driver's els sgl list and memory.
6571 **/
6572static void
6573lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6574{
6575 LIST_HEAD(sglq_list);
6576
6577 /* Retrieve all els sgls from driver list */
6578 spin_lock_irq(&phba->hbalock);
6579 spin_lock(&phba->sli4_hba.sgl_list_lock);
6580 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6581 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6582 spin_unlock_irq(&phba->hbalock);
6583
6584 /* Now free the sgl list */
6585 lpfc_free_sgl_list(phba, &sglq_list);
6586}
6587
6588/**
6589 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6590 * @phba: pointer to lpfc hba data structure.
6591 *
6592 * This routine is invoked to free the driver's nvmet sgl list and memory.
6593 **/
6594static void
6595lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6596{
6597 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6598 LIST_HEAD(sglq_list);
6599
6600 /* Retrieve all nvmet sgls from driver list */
6601 spin_lock_irq(&phba->hbalock);
6602 spin_lock(&phba->sli4_hba.sgl_list_lock);
6603 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6604 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6605 spin_unlock_irq(&phba->hbalock);
6606
6607 /* Now free the sgl list */
6608 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6609 list_del(&sglq_entry->list);
6610 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6611 kfree(sglq_entry);
6612 }
6613
6614 /* Update the nvmet_xri_cnt to reflect no current sgls.
6615 * The next initialization cycle sets the count and allocates
6616 * the sgls over again.
6617 */
6618 phba->sli4_hba.nvmet_xri_cnt = 0;
6619}
6620
6621/**
6622 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6623 * @phba: pointer to lpfc hba data structure.
6624 *
6625 * This routine is invoked to allocate the driver's active sgl memory.
6626 * This array will hold the sglq_entry's for active IOs.
6627 **/
6628static int
6629lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6630{
6631 int size;
6632 size = sizeof(struct lpfc_sglq *);
6633 size *= phba->sli4_hba.max_cfg_param.max_xri;
6634
6635 phba->sli4_hba.lpfc_sglq_active_list =
6636 kzalloc(size, GFP_KERNEL);
6637 if (!phba->sli4_hba.lpfc_sglq_active_list)
6638 return -ENOMEM;
6639 return 0;
6640}
6641
6642/**
6643 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6644 * @phba: pointer to lpfc hba data structure.
6645 *
6646 * This routine is invoked to walk through the array of active sglq entries
6647 * and free all of the resources.
6648 * This is just a place holder for now.
6649 **/
6650static void
6651lpfc_free_active_sgl(struct lpfc_hba *phba)
6652{
6653 kfree(phba->sli4_hba.lpfc_sglq_active_list);
6654}
6655
6656/**
6657 * lpfc_init_sgl_list - Allocate and initialize sgl list.
6658 * @phba: pointer to lpfc hba data structure.
6659 *
6660 * This routine is invoked to allocate and initizlize the driver's sgl
6661 * list and set up the sgl xritag tag array accordingly.
6662 *
6663 **/
6664static void
6665lpfc_init_sgl_list(struct lpfc_hba *phba)
6666{
6667 /* Initialize and populate the sglq list per host/VF. */
6668 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
6669 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
6670 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
6671 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
6672
6673 /* els xri-sgl book keeping */
6674 phba->sli4_hba.els_xri_cnt = 0;
6675
6676 /* scsi xri-buffer book keeping */
6677 phba->sli4_hba.scsi_xri_cnt = 0;
6678
6679 /* nvme xri-buffer book keeping */
6680 phba->sli4_hba.nvme_xri_cnt = 0;
6681}
6682
6683/**
6684 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6685 * @phba: pointer to lpfc hba data structure.
6686 *
6687 * This routine is invoked to post rpi header templates to the
6688 * port for those SLI4 ports that do not support extents. This routine
6689 * posts a PAGE_SIZE memory region to the port to hold up to
6690 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
6691 * and should be called only when interrupts are disabled.
6692 *
6693 * Return codes
6694 * 0 - successful
6695 * -ERROR - otherwise.
6696 **/
6697int
6698lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6699{
6700 int rc = 0;
6701 struct lpfc_rpi_hdr *rpi_hdr;
6702
6703 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
6704 if (!phba->sli4_hba.rpi_hdrs_in_use)
6705 return rc;
6706 if (phba->sli4_hba.extents_in_use)
6707 return -EIO;
6708
6709 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6710 if (!rpi_hdr) {
6711 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6712 "0391 Error during rpi post operation\n");
6713 lpfc_sli4_remove_rpis(phba);
6714 rc = -ENODEV;
6715 }
6716
6717 return rc;
6718}
6719
6720/**
6721 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6722 * @phba: pointer to lpfc hba data structure.
6723 *
6724 * This routine is invoked to allocate a single 4KB memory region to
6725 * support rpis and stores them in the phba. This single region
6726 * provides support for up to 64 rpis. The region is used globally
6727 * by the device.
6728 *
6729 * Returns:
6730 * A valid rpi hdr on success.
6731 * A NULL pointer on any failure.
6732 **/
6733struct lpfc_rpi_hdr *
6734lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6735{
6736 uint16_t rpi_limit, curr_rpi_range;
6737 struct lpfc_dmabuf *dmabuf;
6738 struct lpfc_rpi_hdr *rpi_hdr;
6739
6740 /*
6741 * If the SLI4 port supports extents, posting the rpi header isn't
6742 * required. Set the expected maximum count and let the actual value
6743 * get set when extents are fully allocated.
6744 */
6745 if (!phba->sli4_hba.rpi_hdrs_in_use)
6746 return NULL;
6747 if (phba->sli4_hba.extents_in_use)
6748 return NULL;
6749
6750 /* The limit on the logical index is just the max_rpi count. */
6751 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
6752
6753 spin_lock_irq(&phba->hbalock);
6754 /*
6755 * Establish the starting RPI in this header block. The starting
6756 * rpi is normalized to a zero base because the physical rpi is
6757 * port based.
6758 */
6759 curr_rpi_range = phba->sli4_hba.next_rpi;
6760 spin_unlock_irq(&phba->hbalock);
6761
6762 /* Reached full RPI range */
6763 if (curr_rpi_range == rpi_limit)
6764 return NULL;
6765
6766 /*
6767 * First allocate the protocol header region for the port. The
6768 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6769 */
6770 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6771 if (!dmabuf)
6772 return NULL;
6773
6774 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6775 LPFC_HDR_TEMPLATE_SIZE,
6776 &dmabuf->phys, GFP_KERNEL);
6777 if (!dmabuf->virt) {
6778 rpi_hdr = NULL;
6779 goto err_free_dmabuf;
6780 }
6781
6782 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6783 rpi_hdr = NULL;
6784 goto err_free_coherent;
6785 }
6786
6787 /* Save the rpi header data for cleanup later. */
6788 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6789 if (!rpi_hdr)
6790 goto err_free_coherent;
6791
6792 rpi_hdr->dmabuf = dmabuf;
6793 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6794 rpi_hdr->page_count = 1;
6795 spin_lock_irq(&phba->hbalock);
6796
6797 /* The rpi_hdr stores the logical index only. */
6798 rpi_hdr->start_rpi = curr_rpi_range;
6799 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
6800 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6801
6802 spin_unlock_irq(&phba->hbalock);
6803 return rpi_hdr;
6804
6805 err_free_coherent:
6806 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6807 dmabuf->virt, dmabuf->phys);
6808 err_free_dmabuf:
6809 kfree(dmabuf);
6810 return NULL;
6811}
6812
6813/**
6814 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6815 * @phba: pointer to lpfc hba data structure.
6816 *
6817 * This routine is invoked to remove all memory resources allocated
6818 * to support rpis for SLI4 ports not supporting extents. This routine
6819 * presumes the caller has released all rpis consumed by fabric or port
6820 * logins and is prepared to have the header pages removed.
6821 **/
6822void
6823lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6824{
6825 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6826
6827 if (!phba->sli4_hba.rpi_hdrs_in_use)
6828 goto exit;
6829
6830 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6831 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6832 list_del(&rpi_hdr->list);
6833 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6834 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6835 kfree(rpi_hdr->dmabuf);
6836 kfree(rpi_hdr);
6837 }
6838 exit:
6839 /* There are no rpis available to the port now. */
6840 phba->sli4_hba.next_rpi = 0;
6841}
6842
6843/**
6844 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6845 * @pdev: pointer to pci device data structure.
6846 *
6847 * This routine is invoked to allocate the driver hba data structure for an
6848 * HBA device. If the allocation is successful, the phba reference to the
6849 * PCI device data structure is set.
6850 *
6851 * Return codes
6852 * pointer to @phba - successful
6853 * NULL - error
6854 **/
6855static struct lpfc_hba *
6856lpfc_hba_alloc(struct pci_dev *pdev)
6857{
6858 struct lpfc_hba *phba;
6859
6860 /* Allocate memory for HBA structure */
6861 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6862 if (!phba) {
6863 dev_err(&pdev->dev, "failed to allocate hba struct\n");
6864 return NULL;
6865 }
6866
6867 /* Set reference to PCI device in HBA structure */
6868 phba->pcidev = pdev;
6869
6870 /* Assign an unused board number */
6871 phba->brd_no = lpfc_get_instance();
6872 if (phba->brd_no < 0) {
6873 kfree(phba);
6874 return NULL;
6875 }
6876 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
6877
6878 spin_lock_init(&phba->ct_ev_lock);
6879 INIT_LIST_HEAD(&phba->ct_ev_waiters);
6880
6881 return phba;
6882}
6883
6884/**
6885 * lpfc_hba_free - Free driver hba data structure with a device.
6886 * @phba: pointer to lpfc hba data structure.
6887 *
6888 * This routine is invoked to free the driver hba data structure with an
6889 * HBA device.
6890 **/
6891static void
6892lpfc_hba_free(struct lpfc_hba *phba)
6893{
6894 /* Release the driver assigned board number */
6895 idr_remove(&lpfc_hba_index, phba->brd_no);
6896
6897 /* Free memory allocated with sli3 rings */
6898 kfree(phba->sli.sli3_ring);
6899 phba->sli.sli3_ring = NULL;
6900
6901 kfree(phba);
6902 return;
6903}
6904
6905/**
6906 * lpfc_create_shost - Create hba physical port with associated scsi host.
6907 * @phba: pointer to lpfc hba data structure.
6908 *
6909 * This routine is invoked to create HBA physical port and associate a SCSI
6910 * host with it.
6911 *
6912 * Return codes
6913 * 0 - successful
6914 * other values - error
6915 **/
6916static int
6917lpfc_create_shost(struct lpfc_hba *phba)
6918{
6919 struct lpfc_vport *vport;
6920 struct Scsi_Host *shost;
6921
6922 /* Initialize HBA FC structure */
6923 phba->fc_edtov = FF_DEF_EDTOV;
6924 phba->fc_ratov = FF_DEF_RATOV;
6925 phba->fc_altov = FF_DEF_ALTOV;
6926 phba->fc_arbtov = FF_DEF_ARBTOV;
6927
6928 atomic_set(&phba->sdev_cnt, 0);
6929 atomic_set(&phba->fc4ScsiInputRequests, 0);
6930 atomic_set(&phba->fc4ScsiOutputRequests, 0);
6931 atomic_set(&phba->fc4ScsiControlRequests, 0);
6932 atomic_set(&phba->fc4ScsiIoCmpls, 0);
6933 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
6934 if (!vport)
6935 return -ENODEV;
6936
6937 shost = lpfc_shost_from_vport(vport);
6938 phba->pport = vport;
6939
6940 if (phba->nvmet_support) {
6941 /* Only 1 vport (pport) will support NVME target */
6942 if (phba->txrdy_payload_pool == NULL) {
6943 phba->txrdy_payload_pool = dma_pool_create(
6944 "txrdy_pool", &phba->pcidev->dev,
6945 TXRDY_PAYLOAD_LEN, 16, 0);
6946 if (phba->txrdy_payload_pool) {
6947 phba->targetport = NULL;
6948 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6949 lpfc_printf_log(phba, KERN_INFO,
6950 LOG_INIT | LOG_NVME_DISC,
6951 "6076 NVME Target Found\n");
6952 }
6953 }
6954 }
6955
6956 lpfc_debugfs_initialize(vport);
6957 /* Put reference to SCSI host to driver's device private data */
6958 pci_set_drvdata(phba->pcidev, shost);
6959
6960 /*
6961 * At this point we are fully registered with PSA. In addition,
6962 * any initial discovery should be completed.
6963 */
6964 vport->load_flag |= FC_ALLOW_FDMI;
6965 if (phba->cfg_enable_SmartSAN ||
6966 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
6967
6968 /* Setup appropriate attribute masks */
6969 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
6970 if (phba->cfg_enable_SmartSAN)
6971 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6972 else
6973 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6974 }
6975 return 0;
6976}
6977
6978/**
6979 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6980 * @phba: pointer to lpfc hba data structure.
6981 *
6982 * This routine is invoked to destroy HBA physical port and the associated
6983 * SCSI host.
6984 **/
6985static void
6986lpfc_destroy_shost(struct lpfc_hba *phba)
6987{
6988 struct lpfc_vport *vport = phba->pport;
6989
6990 /* Destroy physical port that associated with the SCSI host */
6991 destroy_port(vport);
6992
6993 return;
6994}
6995
6996/**
6997 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6998 * @phba: pointer to lpfc hba data structure.
6999 * @shost: the shost to be used to detect Block guard settings.
7000 *
7001 * This routine sets up the local Block guard protocol settings for @shost.
7002 * This routine also allocates memory for debugging bg buffers.
7003 **/
7004static void
7005lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
7006{
7007 uint32_t old_mask;
7008 uint32_t old_guard;
7009
7010 int pagecnt = 10;
7011 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7012 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7013 "1478 Registering BlockGuard with the "
7014 "SCSI layer\n");
7015
7016 old_mask = phba->cfg_prot_mask;
7017 old_guard = phba->cfg_prot_guard;
7018
7019 /* Only allow supported values */
7020 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
7021 SHOST_DIX_TYPE0_PROTECTION |
7022 SHOST_DIX_TYPE1_PROTECTION);
7023 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
7024 SHOST_DIX_GUARD_CRC);
7025
7026 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
7027 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
7028 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
7029
7030 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7031 if ((old_mask != phba->cfg_prot_mask) ||
7032 (old_guard != phba->cfg_prot_guard))
7033 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7034 "1475 Registering BlockGuard with the "
7035 "SCSI layer: mask %d guard %d\n",
7036 phba->cfg_prot_mask,
7037 phba->cfg_prot_guard);
7038
7039 scsi_host_set_prot(shost, phba->cfg_prot_mask);
7040 scsi_host_set_guard(shost, phba->cfg_prot_guard);
7041 } else
7042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7043 "1479 Not Registering BlockGuard with the SCSI "
7044 "layer, Bad protection parameters: %d %d\n",
7045 old_mask, old_guard);
7046 }
7047
7048 if (!_dump_buf_data) {
7049 while (pagecnt) {
7050 spin_lock_init(&_dump_buf_lock);
7051 _dump_buf_data =
7052 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7053 if (_dump_buf_data) {
7054 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7055 "9043 BLKGRD: allocated %d pages for "
7056 "_dump_buf_data at 0x%p\n",
7057 (1 << pagecnt), _dump_buf_data);
7058 _dump_buf_data_order = pagecnt;
7059 memset(_dump_buf_data, 0,
7060 ((1 << PAGE_SHIFT) << pagecnt));
7061 break;
7062 } else
7063 --pagecnt;
7064 }
7065 if (!_dump_buf_data_order)
7066 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7067 "9044 BLKGRD: ERROR unable to allocate "
7068 "memory for hexdump\n");
7069 } else
7070 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7071 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
7072 "\n", _dump_buf_data);
7073 if (!_dump_buf_dif) {
7074 while (pagecnt) {
7075 _dump_buf_dif =
7076 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7077 if (_dump_buf_dif) {
7078 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7079 "9046 BLKGRD: allocated %d pages for "
7080 "_dump_buf_dif at 0x%p\n",
7081 (1 << pagecnt), _dump_buf_dif);
7082 _dump_buf_dif_order = pagecnt;
7083 memset(_dump_buf_dif, 0,
7084 ((1 << PAGE_SHIFT) << pagecnt));
7085 break;
7086 } else
7087 --pagecnt;
7088 }
7089 if (!_dump_buf_dif_order)
7090 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7091 "9047 BLKGRD: ERROR unable to allocate "
7092 "memory for hexdump\n");
7093 } else
7094 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7095 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
7096 _dump_buf_dif);
7097}
7098
7099/**
7100 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7101 * @phba: pointer to lpfc hba data structure.
7102 *
7103 * This routine is invoked to perform all the necessary post initialization
7104 * setup for the device.
7105 **/
7106static void
7107lpfc_post_init_setup(struct lpfc_hba *phba)
7108{
7109 struct Scsi_Host *shost;
7110 struct lpfc_adapter_event_header adapter_event;
7111
7112 /* Get the default values for Model Name and Description */
7113 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7114
7115 /*
7116 * hba setup may have changed the hba_queue_depth so we need to
7117 * adjust the value of can_queue.
7118 */
7119 shost = pci_get_drvdata(phba->pcidev);
7120 shost->can_queue = phba->cfg_hba_queue_depth - 10;
7121 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7122 lpfc_setup_bg(phba, shost);
7123
7124 lpfc_host_attrib_init(shost);
7125
7126 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7127 spin_lock_irq(shost->host_lock);
7128 lpfc_poll_start_timer(phba);
7129 spin_unlock_irq(shost->host_lock);
7130 }
7131
7132 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7133 "0428 Perform SCSI scan\n");
7134 /* Send board arrival event to upper layer */
7135 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7136 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7137 fc_host_post_vendor_event(shost, fc_get_event_number(),
7138 sizeof(adapter_event),
7139 (char *) &adapter_event,
7140 LPFC_NL_VENDOR_ID);
7141 return;
7142}
7143
7144/**
7145 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7146 * @phba: pointer to lpfc hba data structure.
7147 *
7148 * This routine is invoked to set up the PCI device memory space for device
7149 * with SLI-3 interface spec.
7150 *
7151 * Return codes
7152 * 0 - successful
7153 * other values - error
7154 **/
7155static int
7156lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7157{
7158 struct pci_dev *pdev;
7159 unsigned long bar0map_len, bar2map_len;
7160 int i, hbq_count;
7161 void *ptr;
7162 int error = -ENODEV;
7163
7164 /* Obtain PCI device reference */
7165 if (!phba->pcidev)
7166 return error;
7167 else
7168 pdev = phba->pcidev;
7169
7170 /* Set the device DMA mask size */
7171 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7172 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7173 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7174 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
7175 return error;
7176 }
7177 }
7178
7179 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7180 * required by each mapping.
7181 */
7182 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7183 bar0map_len = pci_resource_len(pdev, 0);
7184
7185 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7186 bar2map_len = pci_resource_len(pdev, 2);
7187
7188 /* Map HBA SLIM to a kernel virtual address. */
7189 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7190 if (!phba->slim_memmap_p) {
7191 dev_printk(KERN_ERR, &pdev->dev,
7192 "ioremap failed for SLIM memory.\n");
7193 goto out;
7194 }
7195
7196 /* Map HBA Control Registers to a kernel virtual address. */
7197 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7198 if (!phba->ctrl_regs_memmap_p) {
7199 dev_printk(KERN_ERR, &pdev->dev,
7200 "ioremap failed for HBA control registers.\n");
7201 goto out_iounmap_slim;
7202 }
7203
7204 /* Allocate memory for SLI-2 structures */
7205 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7206 &phba->slim2p.phys, GFP_KERNEL);
7207 if (!phba->slim2p.virt)
7208 goto out_iounmap;
7209
7210 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
7211 phba->mbox_ext = (phba->slim2p.virt +
7212 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
7213 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7214 phba->IOCBs = (phba->slim2p.virt +
7215 offsetof(struct lpfc_sli2_slim, IOCBs));
7216
7217 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7218 lpfc_sli_hbq_size(),
7219 &phba->hbqslimp.phys,
7220 GFP_KERNEL);
7221 if (!phba->hbqslimp.virt)
7222 goto out_free_slim;
7223
7224 hbq_count = lpfc_sli_hbq_count();
7225 ptr = phba->hbqslimp.virt;
7226 for (i = 0; i < hbq_count; ++i) {
7227 phba->hbqs[i].hbq_virt = ptr;
7228 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7229 ptr += (lpfc_hbq_defs[i]->entry_count *
7230 sizeof(struct lpfc_hbq_entry));
7231 }
7232 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7233 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7234
7235 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7236
7237 phba->MBslimaddr = phba->slim_memmap_p;
7238 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7239 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7240 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7241 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
7242
7243 return 0;
7244
7245out_free_slim:
7246 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7247 phba->slim2p.virt, phba->slim2p.phys);
7248out_iounmap:
7249 iounmap(phba->ctrl_regs_memmap_p);
7250out_iounmap_slim:
7251 iounmap(phba->slim_memmap_p);
7252out:
7253 return error;
7254}
7255
7256/**
7257 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7258 * @phba: pointer to lpfc hba data structure.
7259 *
7260 * This routine is invoked to unset the PCI device memory space for device
7261 * with SLI-3 interface spec.
7262 **/
7263static void
7264lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7265{
7266 struct pci_dev *pdev;
7267
7268 /* Obtain PCI device reference */
7269 if (!phba->pcidev)
7270 return;
7271 else
7272 pdev = phba->pcidev;
7273
7274 /* Free coherent DMA memory allocated */
7275 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7276 phba->hbqslimp.virt, phba->hbqslimp.phys);
7277 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7278 phba->slim2p.virt, phba->slim2p.phys);
7279
7280 /* I/O memory unmap */
7281 iounmap(phba->ctrl_regs_memmap_p);
7282 iounmap(phba->slim_memmap_p);
7283
7284 return;
7285}
7286
7287/**
7288 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7289 * @phba: pointer to lpfc hba data structure.
7290 *
7291 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7292 * done and check status.
7293 *
7294 * Return 0 if successful, otherwise -ENODEV.
7295 **/
7296int
7297lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7298{
7299 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7300 struct lpfc_register reg_data;
7301 int i, port_error = 0;
7302 uint32_t if_type;
7303
7304 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7305 memset(&reg_data, 0, sizeof(reg_data));
7306 if (!phba->sli4_hba.PSMPHRregaddr)
7307 return -ENODEV;
7308
7309 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7310 for (i = 0; i < 3000; i++) {
7311 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7312 &portsmphr_reg.word0) ||
7313 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
7314 /* Port has a fatal POST error, break out */
7315 port_error = -ENODEV;
7316 break;
7317 }
7318 if (LPFC_POST_STAGE_PORT_READY ==
7319 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
7320 break;
7321 msleep(10);
7322 }
7323
7324 /*
7325 * If there was a port error during POST, then don't proceed with
7326 * other register reads as the data may not be valid. Just exit.
7327 */
7328 if (port_error) {
7329 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7330 "1408 Port Failed POST - portsmphr=0x%x, "
7331 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7332 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7333 portsmphr_reg.word0,
7334 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7335 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7336 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7337 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7338 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7339 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7340 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7341 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7342 } else {
7343 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7344 "2534 Device Info: SLIFamily=0x%x, "
7345 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7346 "SLIHint_2=0x%x, FT=0x%x\n",
7347 bf_get(lpfc_sli_intf_sli_family,
7348 &phba->sli4_hba.sli_intf),
7349 bf_get(lpfc_sli_intf_slirev,
7350 &phba->sli4_hba.sli_intf),
7351 bf_get(lpfc_sli_intf_if_type,
7352 &phba->sli4_hba.sli_intf),
7353 bf_get(lpfc_sli_intf_sli_hint1,
7354 &phba->sli4_hba.sli_intf),
7355 bf_get(lpfc_sli_intf_sli_hint2,
7356 &phba->sli4_hba.sli_intf),
7357 bf_get(lpfc_sli_intf_func_type,
7358 &phba->sli4_hba.sli_intf));
7359 /*
7360 * Check for other Port errors during the initialization
7361 * process. Fail the load if the port did not come up
7362 * correctly.
7363 */
7364 if_type = bf_get(lpfc_sli_intf_if_type,
7365 &phba->sli4_hba.sli_intf);
7366 switch (if_type) {
7367 case LPFC_SLI_INTF_IF_TYPE_0:
7368 phba->sli4_hba.ue_mask_lo =
7369 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7370 phba->sli4_hba.ue_mask_hi =
7371 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7372 uerrlo_reg.word0 =
7373 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7374 uerrhi_reg.word0 =
7375 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7376 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7377 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7379 "1422 Unrecoverable Error "
7380 "Detected during POST "
7381 "uerr_lo_reg=0x%x, "
7382 "uerr_hi_reg=0x%x, "
7383 "ue_mask_lo_reg=0x%x, "
7384 "ue_mask_hi_reg=0x%x\n",
7385 uerrlo_reg.word0,
7386 uerrhi_reg.word0,
7387 phba->sli4_hba.ue_mask_lo,
7388 phba->sli4_hba.ue_mask_hi);
7389 port_error = -ENODEV;
7390 }
7391 break;
7392 case LPFC_SLI_INTF_IF_TYPE_2:
7393 case LPFC_SLI_INTF_IF_TYPE_6:
7394 /* Final checks. The port status should be clean. */
7395 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7396 &reg_data.word0) ||
7397 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7398 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
7399 phba->work_status[0] =
7400 readl(phba->sli4_hba.u.if_type2.
7401 ERR1regaddr);
7402 phba->work_status[1] =
7403 readl(phba->sli4_hba.u.if_type2.
7404 ERR2regaddr);
7405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7406 "2888 Unrecoverable port error "
7407 "following POST: port status reg "
7408 "0x%x, port_smphr reg 0x%x, "
7409 "error 1=0x%x, error 2=0x%x\n",
7410 reg_data.word0,
7411 portsmphr_reg.word0,
7412 phba->work_status[0],
7413 phba->work_status[1]);
7414 port_error = -ENODEV;
7415 }
7416 break;
7417 case LPFC_SLI_INTF_IF_TYPE_1:
7418 default:
7419 break;
7420 }
7421 }
7422 return port_error;
7423}
7424
7425/**
7426 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7427 * @phba: pointer to lpfc hba data structure.
7428 * @if_type: The SLI4 interface type getting configured.
7429 *
7430 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7431 * memory map.
7432 **/
7433static void
7434lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
7435{
7436 switch (if_type) {
7437 case LPFC_SLI_INTF_IF_TYPE_0:
7438 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7439 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7440 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7441 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7442 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7443 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7444 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7445 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7446 phba->sli4_hba.SLIINTFregaddr =
7447 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7448 break;
7449 case LPFC_SLI_INTF_IF_TYPE_2:
7450 phba->sli4_hba.u.if_type2.EQDregaddr =
7451 phba->sli4_hba.conf_regs_memmap_p +
7452 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
7453 phba->sli4_hba.u.if_type2.ERR1regaddr =
7454 phba->sli4_hba.conf_regs_memmap_p +
7455 LPFC_CTL_PORT_ER1_OFFSET;
7456 phba->sli4_hba.u.if_type2.ERR2regaddr =
7457 phba->sli4_hba.conf_regs_memmap_p +
7458 LPFC_CTL_PORT_ER2_OFFSET;
7459 phba->sli4_hba.u.if_type2.CTRLregaddr =
7460 phba->sli4_hba.conf_regs_memmap_p +
7461 LPFC_CTL_PORT_CTL_OFFSET;
7462 phba->sli4_hba.u.if_type2.STATUSregaddr =
7463 phba->sli4_hba.conf_regs_memmap_p +
7464 LPFC_CTL_PORT_STA_OFFSET;
7465 phba->sli4_hba.SLIINTFregaddr =
7466 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7467 phba->sli4_hba.PSMPHRregaddr =
7468 phba->sli4_hba.conf_regs_memmap_p +
7469 LPFC_CTL_PORT_SEM_OFFSET;
7470 phba->sli4_hba.RQDBregaddr =
7471 phba->sli4_hba.conf_regs_memmap_p +
7472 LPFC_ULP0_RQ_DOORBELL;
7473 phba->sli4_hba.WQDBregaddr =
7474 phba->sli4_hba.conf_regs_memmap_p +
7475 LPFC_ULP0_WQ_DOORBELL;
7476 phba->sli4_hba.CQDBregaddr =
7477 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7478 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
7479 phba->sli4_hba.MQDBregaddr =
7480 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7481 phba->sli4_hba.BMBXregaddr =
7482 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7483 break;
7484 case LPFC_SLI_INTF_IF_TYPE_6:
7485 phba->sli4_hba.u.if_type2.EQDregaddr =
7486 phba->sli4_hba.conf_regs_memmap_p +
7487 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
7488 phba->sli4_hba.u.if_type2.ERR1regaddr =
7489 phba->sli4_hba.conf_regs_memmap_p +
7490 LPFC_CTL_PORT_ER1_OFFSET;
7491 phba->sli4_hba.u.if_type2.ERR2regaddr =
7492 phba->sli4_hba.conf_regs_memmap_p +
7493 LPFC_CTL_PORT_ER2_OFFSET;
7494 phba->sli4_hba.u.if_type2.CTRLregaddr =
7495 phba->sli4_hba.conf_regs_memmap_p +
7496 LPFC_CTL_PORT_CTL_OFFSET;
7497 phba->sli4_hba.u.if_type2.STATUSregaddr =
7498 phba->sli4_hba.conf_regs_memmap_p +
7499 LPFC_CTL_PORT_STA_OFFSET;
7500 phba->sli4_hba.PSMPHRregaddr =
7501 phba->sli4_hba.conf_regs_memmap_p +
7502 LPFC_CTL_PORT_SEM_OFFSET;
7503 phba->sli4_hba.BMBXregaddr =
7504 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7505 break;
7506 case LPFC_SLI_INTF_IF_TYPE_1:
7507 default:
7508 dev_printk(KERN_ERR, &phba->pcidev->dev,
7509 "FATAL - unsupported SLI4 interface type - %d\n",
7510 if_type);
7511 break;
7512 }
7513}
7514
7515/**
7516 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7517 * @phba: pointer to lpfc hba data structure.
7518 *
7519 * This routine is invoked to set up SLI4 BAR1 register memory map.
7520 **/
7521static void
7522lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
7523{
7524 switch (if_type) {
7525 case LPFC_SLI_INTF_IF_TYPE_0:
7526 phba->sli4_hba.PSMPHRregaddr =
7527 phba->sli4_hba.ctrl_regs_memmap_p +
7528 LPFC_SLIPORT_IF0_SMPHR;
7529 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7530 LPFC_HST_ISR0;
7531 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7532 LPFC_HST_IMR0;
7533 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7534 LPFC_HST_ISCR0;
7535 break;
7536 case LPFC_SLI_INTF_IF_TYPE_6:
7537 phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7538 LPFC_IF6_RQ_DOORBELL;
7539 phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7540 LPFC_IF6_WQ_DOORBELL;
7541 phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7542 LPFC_IF6_CQ_DOORBELL;
7543 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7544 LPFC_IF6_EQ_DOORBELL;
7545 phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7546 LPFC_IF6_MQ_DOORBELL;
7547 break;
7548 case LPFC_SLI_INTF_IF_TYPE_2:
7549 case LPFC_SLI_INTF_IF_TYPE_1:
7550 default:
7551 dev_err(&phba->pcidev->dev,
7552 "FATAL - unsupported SLI4 interface type - %d\n",
7553 if_type);
7554 break;
7555 }
7556}
7557
7558/**
7559 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7560 * @phba: pointer to lpfc hba data structure.
7561 * @vf: virtual function number
7562 *
7563 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7564 * based on the given viftual function number, @vf.
7565 *
7566 * Return 0 if successful, otherwise -ENODEV.
7567 **/
7568static int
7569lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7570{
7571 if (vf > LPFC_VIR_FUNC_MAX)
7572 return -ENODEV;
7573
7574 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7575 vf * LPFC_VFR_PAGE_SIZE +
7576 LPFC_ULP0_RQ_DOORBELL);
7577 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7578 vf * LPFC_VFR_PAGE_SIZE +
7579 LPFC_ULP0_WQ_DOORBELL);
7580 phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7581 vf * LPFC_VFR_PAGE_SIZE +
7582 LPFC_EQCQ_DOORBELL);
7583 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
7584 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7585 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7586 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7587 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7588 return 0;
7589}
7590
7591/**
7592 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7593 * @phba: pointer to lpfc hba data structure.
7594 *
7595 * This routine is invoked to create the bootstrap mailbox
7596 * region consistent with the SLI-4 interface spec. This
7597 * routine allocates all memory necessary to communicate
7598 * mailbox commands to the port and sets up all alignment
7599 * needs. No locks are expected to be held when calling
7600 * this routine.
7601 *
7602 * Return codes
7603 * 0 - successful
7604 * -ENOMEM - could not allocated memory.
7605 **/
7606static int
7607lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7608{
7609 uint32_t bmbx_size;
7610 struct lpfc_dmabuf *dmabuf;
7611 struct dma_address *dma_address;
7612 uint32_t pa_addr;
7613 uint64_t phys_addr;
7614
7615 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7616 if (!dmabuf)
7617 return -ENOMEM;
7618
7619 /*
7620 * The bootstrap mailbox region is comprised of 2 parts
7621 * plus an alignment restriction of 16 bytes.
7622 */
7623 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
7624 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7625 &dmabuf->phys, GFP_KERNEL);
7626 if (!dmabuf->virt) {
7627 kfree(dmabuf);
7628 return -ENOMEM;
7629 }
7630
7631 /*
7632 * Initialize the bootstrap mailbox pointers now so that the register
7633 * operations are simple later. The mailbox dma address is required
7634 * to be 16-byte aligned. Also align the virtual memory as each
7635 * maibox is copied into the bmbx mailbox region before issuing the
7636 * command to the port.
7637 */
7638 phba->sli4_hba.bmbx.dmabuf = dmabuf;
7639 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7640
7641 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7642 LPFC_ALIGN_16_BYTE);
7643 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7644 LPFC_ALIGN_16_BYTE);
7645
7646 /*
7647 * Set the high and low physical addresses now. The SLI4 alignment
7648 * requirement is 16 bytes and the mailbox is posted to the port
7649 * as two 30-bit addresses. The other data is a bit marking whether
7650 * the 30-bit address is the high or low address.
7651 * Upcast bmbx aphys to 64bits so shift instruction compiles
7652 * clean on 32 bit machines.
7653 */
7654 dma_address = &phba->sli4_hba.bmbx.dma_address;
7655 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7656 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7657 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7658 LPFC_BMBX_BIT1_ADDR_HI);
7659
7660 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7661 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7662 LPFC_BMBX_BIT1_ADDR_LO);
7663 return 0;
7664}
7665
7666/**
7667 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7668 * @phba: pointer to lpfc hba data structure.
7669 *
7670 * This routine is invoked to teardown the bootstrap mailbox
7671 * region and release all host resources. This routine requires
7672 * the caller to ensure all mailbox commands recovered, no
7673 * additional mailbox comands are sent, and interrupts are disabled
7674 * before calling this routine.
7675 *
7676 **/
7677static void
7678lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7679{
7680 dma_free_coherent(&phba->pcidev->dev,
7681 phba->sli4_hba.bmbx.bmbx_size,
7682 phba->sli4_hba.bmbx.dmabuf->virt,
7683 phba->sli4_hba.bmbx.dmabuf->phys);
7684
7685 kfree(phba->sli4_hba.bmbx.dmabuf);
7686 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7687}
7688
7689/**
7690 * lpfc_sli4_read_config - Get the config parameters.
7691 * @phba: pointer to lpfc hba data structure.
7692 *
7693 * This routine is invoked to read the configuration parameters from the HBA.
7694 * The configuration parameters are used to set the base and maximum values
7695 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7696 * allocation for the port.
7697 *
7698 * Return codes
7699 * 0 - successful
7700 * -ENOMEM - No available memory
7701 * -EIO - The mailbox failed to complete successfully.
7702 **/
7703int
7704lpfc_sli4_read_config(struct lpfc_hba *phba)
7705{
7706 LPFC_MBOXQ_t *pmb;
7707 struct lpfc_mbx_read_config *rd_config;
7708 union lpfc_sli4_cfg_shdr *shdr;
7709 uint32_t shdr_status, shdr_add_status;
7710 struct lpfc_mbx_get_func_cfg *get_func_cfg;
7711 struct lpfc_rsrc_desc_fcfcoe *desc;
7712 char *pdesc_0;
7713 uint16_t forced_link_speed;
7714 uint32_t if_type;
7715 int length, i, rc = 0, rc2;
7716
7717 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7718 if (!pmb) {
7719 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7720 "2011 Unable to allocate memory for issuing "
7721 "SLI_CONFIG_SPECIAL mailbox command\n");
7722 return -ENOMEM;
7723 }
7724
7725 lpfc_read_config(phba, pmb);
7726
7727 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7728 if (rc != MBX_SUCCESS) {
7729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7730 "2012 Mailbox failed , mbxCmd x%x "
7731 "READ_CONFIG, mbxStatus x%x\n",
7732 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7733 bf_get(lpfc_mqe_status, &pmb->u.mqe));
7734 rc = -EIO;
7735 } else {
7736 rd_config = &pmb->u.mqe.un.rd_config;
7737 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7738 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7739 phba->sli4_hba.lnk_info.lnk_tp =
7740 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7741 phba->sli4_hba.lnk_info.lnk_no =
7742 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7743 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7744 "3081 lnk_type:%d, lnk_numb:%d\n",
7745 phba->sli4_hba.lnk_info.lnk_tp,
7746 phba->sli4_hba.lnk_info.lnk_no);
7747 } else
7748 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7749 "3082 Mailbox (x%x) returned ldv:x0\n",
7750 bf_get(lpfc_mqe_command, &pmb->u.mqe));
7751 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
7752 phba->bbcredit_support = 1;
7753 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
7754 }
7755
7756 phba->sli4_hba.extents_in_use =
7757 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
7758 phba->sli4_hba.max_cfg_param.max_xri =
7759 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7760 phba->sli4_hba.max_cfg_param.xri_base =
7761 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7762 phba->sli4_hba.max_cfg_param.max_vpi =
7763 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7764 phba->sli4_hba.max_cfg_param.vpi_base =
7765 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7766 phba->sli4_hba.max_cfg_param.max_rpi =
7767 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7768 phba->sli4_hba.max_cfg_param.rpi_base =
7769 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7770 phba->sli4_hba.max_cfg_param.max_vfi =
7771 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7772 phba->sli4_hba.max_cfg_param.vfi_base =
7773 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7774 phba->sli4_hba.max_cfg_param.max_fcfi =
7775 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
7776 phba->sli4_hba.max_cfg_param.max_eq =
7777 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7778 phba->sli4_hba.max_cfg_param.max_rq =
7779 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7780 phba->sli4_hba.max_cfg_param.max_wq =
7781 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7782 phba->sli4_hba.max_cfg_param.max_cq =
7783 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7784 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7785 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7786 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7787 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
7788 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7789 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
7790 phba->max_vports = phba->max_vpi;
7791 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7792 "2003 cfg params Extents? %d "
7793 "XRI(B:%d M:%d), "
7794 "VPI(B:%d M:%d) "
7795 "VFI(B:%d M:%d) "
7796 "RPI(B:%d M:%d) "
7797 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
7798 phba->sli4_hba.extents_in_use,
7799 phba->sli4_hba.max_cfg_param.xri_base,
7800 phba->sli4_hba.max_cfg_param.max_xri,
7801 phba->sli4_hba.max_cfg_param.vpi_base,
7802 phba->sli4_hba.max_cfg_param.max_vpi,
7803 phba->sli4_hba.max_cfg_param.vfi_base,
7804 phba->sli4_hba.max_cfg_param.max_vfi,
7805 phba->sli4_hba.max_cfg_param.rpi_base,
7806 phba->sli4_hba.max_cfg_param.max_rpi,
7807 phba->sli4_hba.max_cfg_param.max_fcfi,
7808 phba->sli4_hba.max_cfg_param.max_eq,
7809 phba->sli4_hba.max_cfg_param.max_cq,
7810 phba->sli4_hba.max_cfg_param.max_wq,
7811 phba->sli4_hba.max_cfg_param.max_rq);
7812
7813 /*
7814 * Calculate NVME queue resources based on how
7815 * many WQ/CQs are available.
7816 */
7817 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
7818 length = phba->sli4_hba.max_cfg_param.max_wq;
7819 if (phba->sli4_hba.max_cfg_param.max_cq <
7820 phba->sli4_hba.max_cfg_param.max_wq)
7821 length = phba->sli4_hba.max_cfg_param.max_cq;
7822
7823 /*
7824 * Whats left after this can go toward NVME.
7825 * The minus 6 accounts for ELS, NVME LS, MBOX
7826 * fof plus a couple extra. When configured for
7827 * NVMET, FCP io channel WQs are not created.
7828 */
7829 length -= 6;
7830 if (!phba->nvmet_support)
7831 length -= phba->cfg_fcp_io_channel;
7832
7833 if (phba->cfg_nvme_io_channel > length) {
7834 lpfc_printf_log(
7835 phba, KERN_ERR, LOG_SLI,
7836 "2005 Reducing NVME IO channel to %d: "
7837 "WQ %d CQ %d NVMEIO %d FCPIO %d\n",
7838 length,
7839 phba->sli4_hba.max_cfg_param.max_wq,
7840 phba->sli4_hba.max_cfg_param.max_cq,
7841 phba->cfg_nvme_io_channel,
7842 phba->cfg_fcp_io_channel);
7843
7844 phba->cfg_nvme_io_channel = length;
7845 }
7846 }
7847 }
7848
7849 if (rc)
7850 goto read_cfg_out;
7851
7852 /* Update link speed if forced link speed is supported */
7853 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7854 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
7855 forced_link_speed =
7856 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7857 if (forced_link_speed) {
7858 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7859
7860 switch (forced_link_speed) {
7861 case LINK_SPEED_1G:
7862 phba->cfg_link_speed =
7863 LPFC_USER_LINK_SPEED_1G;
7864 break;
7865 case LINK_SPEED_2G:
7866 phba->cfg_link_speed =
7867 LPFC_USER_LINK_SPEED_2G;
7868 break;
7869 case LINK_SPEED_4G:
7870 phba->cfg_link_speed =
7871 LPFC_USER_LINK_SPEED_4G;
7872 break;
7873 case LINK_SPEED_8G:
7874 phba->cfg_link_speed =
7875 LPFC_USER_LINK_SPEED_8G;
7876 break;
7877 case LINK_SPEED_10G:
7878 phba->cfg_link_speed =
7879 LPFC_USER_LINK_SPEED_10G;
7880 break;
7881 case LINK_SPEED_16G:
7882 phba->cfg_link_speed =
7883 LPFC_USER_LINK_SPEED_16G;
7884 break;
7885 case LINK_SPEED_32G:
7886 phba->cfg_link_speed =
7887 LPFC_USER_LINK_SPEED_32G;
7888 break;
7889 case LINK_SPEED_64G:
7890 phba->cfg_link_speed =
7891 LPFC_USER_LINK_SPEED_64G;
7892 break;
7893 case 0xffff:
7894 phba->cfg_link_speed =
7895 LPFC_USER_LINK_SPEED_AUTO;
7896 break;
7897 default:
7898 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7899 "0047 Unrecognized link "
7900 "speed : %d\n",
7901 forced_link_speed);
7902 phba->cfg_link_speed =
7903 LPFC_USER_LINK_SPEED_AUTO;
7904 }
7905 }
7906 }
7907
7908 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
7909 length = phba->sli4_hba.max_cfg_param.max_xri -
7910 lpfc_sli4_get_els_iocb_cnt(phba);
7911 if (phba->cfg_hba_queue_depth > length) {
7912 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7913 "3361 HBA queue depth changed from %d to %d\n",
7914 phba->cfg_hba_queue_depth, length);
7915 phba->cfg_hba_queue_depth = length;
7916 }
7917
7918 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7919 LPFC_SLI_INTF_IF_TYPE_2)
7920 goto read_cfg_out;
7921
7922 /* get the pf# and vf# for SLI4 if_type 2 port */
7923 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7924 sizeof(struct lpfc_sli4_cfg_mhdr));
7925 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7926 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7927 length, LPFC_SLI4_MBX_EMBED);
7928
7929 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7930 shdr = (union lpfc_sli4_cfg_shdr *)
7931 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7932 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7933 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7934 if (rc2 || shdr_status || shdr_add_status) {
7935 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7936 "3026 Mailbox failed , mbxCmd x%x "
7937 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7938 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7939 bf_get(lpfc_mqe_status, &pmb->u.mqe));
7940 goto read_cfg_out;
7941 }
7942
7943 /* search for fc_fcoe resrouce descriptor */
7944 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
7945
7946 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7947 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7948 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7949 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7950 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7951 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7952 goto read_cfg_out;
7953
7954 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
7955 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
7956 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
7957 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
7958 phba->sli4_hba.iov.pf_number =
7959 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7960 phba->sli4_hba.iov.vf_number =
7961 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7962 break;
7963 }
7964 }
7965
7966 if (i < LPFC_RSRC_DESC_MAX_NUM)
7967 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7968 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7969 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7970 phba->sli4_hba.iov.vf_number);
7971 else
7972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7973 "3028 GET_FUNCTION_CONFIG: failed to find "
7974 "Resrouce Descriptor:x%x\n",
7975 LPFC_RSRC_DESC_TYPE_FCFCOE);
7976
7977read_cfg_out:
7978 mempool_free(pmb, phba->mbox_mem_pool);
7979 return rc;
7980}
7981
7982/**
7983 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
7984 * @phba: pointer to lpfc hba data structure.
7985 *
7986 * This routine is invoked to setup the port-side endian order when
7987 * the port if_type is 0. This routine has no function for other
7988 * if_types.
7989 *
7990 * Return codes
7991 * 0 - successful
7992 * -ENOMEM - No available memory
7993 * -EIO - The mailbox failed to complete successfully.
7994 **/
7995static int
7996lpfc_setup_endian_order(struct lpfc_hba *phba)
7997{
7998 LPFC_MBOXQ_t *mboxq;
7999 uint32_t if_type, rc = 0;
8000 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
8001 HOST_ENDIAN_HIGH_WORD1};
8002
8003 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8004 switch (if_type) {
8005 case LPFC_SLI_INTF_IF_TYPE_0:
8006 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8007 GFP_KERNEL);
8008 if (!mboxq) {
8009 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8010 "0492 Unable to allocate memory for "
8011 "issuing SLI_CONFIG_SPECIAL mailbox "
8012 "command\n");
8013 return -ENOMEM;
8014 }
8015
8016 /*
8017 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
8018 * two words to contain special data values and no other data.
8019 */
8020 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
8021 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
8022 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8023 if (rc != MBX_SUCCESS) {
8024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8025 "0493 SLI_CONFIG_SPECIAL mailbox "
8026 "failed with status x%x\n",
8027 rc);
8028 rc = -EIO;
8029 }
8030 mempool_free(mboxq, phba->mbox_mem_pool);
8031 break;
8032 case LPFC_SLI_INTF_IF_TYPE_6:
8033 case LPFC_SLI_INTF_IF_TYPE_2:
8034 case LPFC_SLI_INTF_IF_TYPE_1:
8035 default:
8036 break;
8037 }
8038 return rc;
8039}
8040
8041/**
8042 * lpfc_sli4_queue_verify - Verify and update EQ counts
8043 * @phba: pointer to lpfc hba data structure.
8044 *
8045 * This routine is invoked to check the user settable queue counts for EQs.
8046 * After this routine is called the counts will be set to valid values that
8047 * adhere to the constraints of the system's interrupt vectors and the port's
8048 * queue resources.
8049 *
8050 * Return codes
8051 * 0 - successful
8052 * -ENOMEM - No available memory
8053 **/
8054static int
8055lpfc_sli4_queue_verify(struct lpfc_hba *phba)
8056{
8057 int io_channel;
8058 int fof_vectors = phba->cfg_fof ? 1 : 0;
8059
8060 /*
8061 * Sanity check for configured queue parameters against the run-time
8062 * device parameters
8063 */
8064
8065 /* Sanity check on HBA EQ parameters */
8066 io_channel = phba->io_channel_irqs;
8067
8068 if (phba->sli4_hba.num_online_cpu < io_channel) {
8069 lpfc_printf_log(phba,
8070 KERN_ERR, LOG_INIT,
8071 "3188 Reducing IO channels to match number of "
8072 "online CPUs: from %d to %d\n",
8073 io_channel, phba->sli4_hba.num_online_cpu);
8074 io_channel = phba->sli4_hba.num_online_cpu;
8075 }
8076
8077 if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
8078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8079 "2575 Reducing IO channels to match number of "
8080 "available EQs: from %d to %d\n",
8081 io_channel,
8082 phba->sli4_hba.max_cfg_param.max_eq);
8083 io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
8084 }
8085
8086 /* The actual number of FCP / NVME event queues adopted */
8087 if (io_channel != phba->io_channel_irqs)
8088 phba->io_channel_irqs = io_channel;
8089 if (phba->cfg_fcp_io_channel > io_channel)
8090 phba->cfg_fcp_io_channel = io_channel;
8091 if (phba->cfg_nvme_io_channel > io_channel)
8092 phba->cfg_nvme_io_channel = io_channel;
8093 if (phba->nvmet_support) {
8094 if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
8095 phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
8096 }
8097 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8098 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
8099
8100 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8101 "2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
8102 phba->io_channel_irqs, phba->cfg_fcp_io_channel,
8103 phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
8104
8105 /* Get EQ depth from module parameter, fake the default for now */
8106 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8107 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8108
8109 /* Get CQ depth from module parameter, fake the default for now */
8110 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8111 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
8112 return 0;
8113}
8114
8115static int
8116lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
8117{
8118 struct lpfc_queue *qdesc;
8119
8120 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8121 phba->sli4_hba.cq_esize,
8122 LPFC_CQE_EXP_COUNT);
8123 if (!qdesc) {
8124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8125 "0508 Failed allocate fast-path NVME CQ (%d)\n",
8126 wqidx);
8127 return 1;
8128 }
8129 qdesc->qe_valid = 1;
8130 phba->sli4_hba.nvme_cq[wqidx] = qdesc;
8131
8132 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8133 LPFC_WQE128_SIZE, LPFC_WQE_EXP_COUNT);
8134 if (!qdesc) {
8135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8136 "0509 Failed allocate fast-path NVME WQ (%d)\n",
8137 wqidx);
8138 return 1;
8139 }
8140 phba->sli4_hba.nvme_wq[wqidx] = qdesc;
8141 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8142 return 0;
8143}
8144
8145static int
8146lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
8147{
8148 struct lpfc_queue *qdesc;
8149 uint32_t wqesize;
8150
8151 /* Create Fast Path FCP CQs */
8152 if (phba->enab_exp_wqcq_pages)
8153 /* Increase the CQ size when WQEs contain an embedded cdb */
8154 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8155 phba->sli4_hba.cq_esize,
8156 LPFC_CQE_EXP_COUNT);
8157
8158 else
8159 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8160 phba->sli4_hba.cq_esize,
8161 phba->sli4_hba.cq_ecount);
8162 if (!qdesc) {
8163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8164 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
8165 return 1;
8166 }
8167 qdesc->qe_valid = 1;
8168 phba->sli4_hba.fcp_cq[wqidx] = qdesc;
8169
8170 /* Create Fast Path FCP WQs */
8171 if (phba->enab_exp_wqcq_pages) {
8172 /* Increase the WQ size when WQEs contain an embedded cdb */
8173 wqesize = (phba->fcp_embed_io) ?
8174 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
8175 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8176 wqesize,
8177 LPFC_WQE_EXP_COUNT);
8178 } else
8179 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8180 phba->sli4_hba.wq_esize,
8181 phba->sli4_hba.wq_ecount);
8182
8183 if (!qdesc) {
8184 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8185 "0503 Failed allocate fast-path FCP WQ (%d)\n",
8186 wqidx);
8187 return 1;
8188 }
8189 phba->sli4_hba.fcp_wq[wqidx] = qdesc;
8190 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8191 return 0;
8192}
8193
8194/**
8195 * lpfc_sli4_queue_create - Create all the SLI4 queues
8196 * @phba: pointer to lpfc hba data structure.
8197 *
8198 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8199 * operation. For each SLI4 queue type, the parameters such as queue entry
8200 * count (queue depth) shall be taken from the module parameter. For now,
8201 * we just use some constant number as place holder.
8202 *
8203 * Return codes
8204 * 0 - successful
8205 * -ENOMEM - No availble memory
8206 * -EIO - The mailbox failed to complete successfully.
8207 **/
8208int
8209lpfc_sli4_queue_create(struct lpfc_hba *phba)
8210{
8211 struct lpfc_queue *qdesc;
8212 int idx, io_channel;
8213
8214 /*
8215 * Create HBA Record arrays.
8216 * Both NVME and FCP will share that same vectors / EQs
8217 */
8218 io_channel = phba->io_channel_irqs;
8219 if (!io_channel)
8220 return -ERANGE;
8221
8222 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8223 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8224 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8225 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8226 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8227 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
8228 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8229 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8230 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8231 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
8232
8233 phba->sli4_hba.hba_eq = kcalloc(io_channel,
8234 sizeof(struct lpfc_queue *),
8235 GFP_KERNEL);
8236 if (!phba->sli4_hba.hba_eq) {
8237 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8238 "2576 Failed allocate memory for "
8239 "fast-path EQ record array\n");
8240 goto out_error;
8241 }
8242
8243 if (phba->cfg_fcp_io_channel) {
8244 phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
8245 sizeof(struct lpfc_queue *),
8246 GFP_KERNEL);
8247 if (!phba->sli4_hba.fcp_cq) {
8248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8249 "2577 Failed allocate memory for "
8250 "fast-path CQ record array\n");
8251 goto out_error;
8252 }
8253 phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
8254 sizeof(struct lpfc_queue *),
8255 GFP_KERNEL);
8256 if (!phba->sli4_hba.fcp_wq) {
8257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8258 "2578 Failed allocate memory for "
8259 "fast-path FCP WQ record array\n");
8260 goto out_error;
8261 }
8262 /*
8263 * Since the first EQ can have multiple CQs associated with it,
8264 * this array is used to quickly see if we have a FCP fast-path
8265 * CQ match.
8266 */
8267 phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
8268 sizeof(uint16_t),
8269 GFP_KERNEL);
8270 if (!phba->sli4_hba.fcp_cq_map) {
8271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8272 "2545 Failed allocate memory for "
8273 "fast-path CQ map\n");
8274 goto out_error;
8275 }
8276 }
8277
8278 if (phba->cfg_nvme_io_channel) {
8279 phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
8280 sizeof(struct lpfc_queue *),
8281 GFP_KERNEL);
8282 if (!phba->sli4_hba.nvme_cq) {
8283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8284 "6077 Failed allocate memory for "
8285 "fast-path CQ record array\n");
8286 goto out_error;
8287 }
8288
8289 phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
8290 sizeof(struct lpfc_queue *),
8291 GFP_KERNEL);
8292 if (!phba->sli4_hba.nvme_wq) {
8293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8294 "2581 Failed allocate memory for "
8295 "fast-path NVME WQ record array\n");
8296 goto out_error;
8297 }
8298
8299 /*
8300 * Since the first EQ can have multiple CQs associated with it,
8301 * this array is used to quickly see if we have a NVME fast-path
8302 * CQ match.
8303 */
8304 phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
8305 sizeof(uint16_t),
8306 GFP_KERNEL);
8307 if (!phba->sli4_hba.nvme_cq_map) {
8308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8309 "6078 Failed allocate memory for "
8310 "fast-path CQ map\n");
8311 goto out_error;
8312 }
8313
8314 if (phba->nvmet_support) {
8315 phba->sli4_hba.nvmet_cqset = kcalloc(
8316 phba->cfg_nvmet_mrq,
8317 sizeof(struct lpfc_queue *),
8318 GFP_KERNEL);
8319 if (!phba->sli4_hba.nvmet_cqset) {
8320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8321 "3121 Fail allocate memory for "
8322 "fast-path CQ set array\n");
8323 goto out_error;
8324 }
8325 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8326 phba->cfg_nvmet_mrq,
8327 sizeof(struct lpfc_queue *),
8328 GFP_KERNEL);
8329 if (!phba->sli4_hba.nvmet_mrq_hdr) {
8330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8331 "3122 Fail allocate memory for "
8332 "fast-path RQ set hdr array\n");
8333 goto out_error;
8334 }
8335 phba->sli4_hba.nvmet_mrq_data = kcalloc(
8336 phba->cfg_nvmet_mrq,
8337 sizeof(struct lpfc_queue *),
8338 GFP_KERNEL);
8339 if (!phba->sli4_hba.nvmet_mrq_data) {
8340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8341 "3124 Fail allocate memory for "
8342 "fast-path RQ set data array\n");
8343 goto out_error;
8344 }
8345 }
8346 }
8347
8348 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8349
8350 /* Create HBA Event Queues (EQs) */
8351 for (idx = 0; idx < io_channel; idx++) {
8352 /* Create EQs */
8353 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8354 phba->sli4_hba.eq_esize,
8355 phba->sli4_hba.eq_ecount);
8356 if (!qdesc) {
8357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8358 "0497 Failed allocate EQ (%d)\n", idx);
8359 goto out_error;
8360 }
8361 qdesc->qe_valid = 1;
8362 phba->sli4_hba.hba_eq[idx] = qdesc;
8363 }
8364
8365 /* FCP and NVME io channels are not required to be balanced */
8366
8367 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8368 if (lpfc_alloc_fcp_wq_cq(phba, idx))
8369 goto out_error;
8370
8371 for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
8372 if (lpfc_alloc_nvme_wq_cq(phba, idx))
8373 goto out_error;
8374
8375 if (phba->nvmet_support) {
8376 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8377 qdesc = lpfc_sli4_queue_alloc(phba,
8378 LPFC_DEFAULT_PAGE_SIZE,
8379 phba->sli4_hba.cq_esize,
8380 phba->sli4_hba.cq_ecount);
8381 if (!qdesc) {
8382 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8383 "3142 Failed allocate NVME "
8384 "CQ Set (%d)\n", idx);
8385 goto out_error;
8386 }
8387 qdesc->qe_valid = 1;
8388 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8389 }
8390 }
8391
8392 /*
8393 * Create Slow Path Completion Queues (CQs)
8394 */
8395
8396 /* Create slow-path Mailbox Command Complete Queue */
8397 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8398 phba->sli4_hba.cq_esize,
8399 phba->sli4_hba.cq_ecount);
8400 if (!qdesc) {
8401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8402 "0500 Failed allocate slow-path mailbox CQ\n");
8403 goto out_error;
8404 }
8405 qdesc->qe_valid = 1;
8406 phba->sli4_hba.mbx_cq = qdesc;
8407
8408 /* Create slow-path ELS Complete Queue */
8409 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8410 phba->sli4_hba.cq_esize,
8411 phba->sli4_hba.cq_ecount);
8412 if (!qdesc) {
8413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8414 "0501 Failed allocate slow-path ELS CQ\n");
8415 goto out_error;
8416 }
8417 qdesc->qe_valid = 1;
8418 phba->sli4_hba.els_cq = qdesc;
8419
8420
8421 /*
8422 * Create Slow Path Work Queues (WQs)
8423 */
8424
8425 /* Create Mailbox Command Queue */
8426
8427 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8428 phba->sli4_hba.mq_esize,
8429 phba->sli4_hba.mq_ecount);
8430 if (!qdesc) {
8431 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8432 "0505 Failed allocate slow-path MQ\n");
8433 goto out_error;
8434 }
8435 phba->sli4_hba.mbx_wq = qdesc;
8436
8437 /*
8438 * Create ELS Work Queues
8439 */
8440
8441 /* Create slow-path ELS Work Queue */
8442 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8443 phba->sli4_hba.wq_esize,
8444 phba->sli4_hba.wq_ecount);
8445 if (!qdesc) {
8446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8447 "0504 Failed allocate slow-path ELS WQ\n");
8448 goto out_error;
8449 }
8450 phba->sli4_hba.els_wq = qdesc;
8451 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8452
8453 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8454 /* Create NVME LS Complete Queue */
8455 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8456 phba->sli4_hba.cq_esize,
8457 phba->sli4_hba.cq_ecount);
8458 if (!qdesc) {
8459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8460 "6079 Failed allocate NVME LS CQ\n");
8461 goto out_error;
8462 }
8463 qdesc->qe_valid = 1;
8464 phba->sli4_hba.nvmels_cq = qdesc;
8465
8466 /* Create NVME LS Work Queue */
8467 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8468 phba->sli4_hba.wq_esize,
8469 phba->sli4_hba.wq_ecount);
8470 if (!qdesc) {
8471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8472 "6080 Failed allocate NVME LS WQ\n");
8473 goto out_error;
8474 }
8475 phba->sli4_hba.nvmels_wq = qdesc;
8476 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8477 }
8478
8479 /*
8480 * Create Receive Queue (RQ)
8481 */
8482
8483 /* Create Receive Queue for header */
8484 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8485 phba->sli4_hba.rq_esize,
8486 phba->sli4_hba.rq_ecount);
8487 if (!qdesc) {
8488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8489 "0506 Failed allocate receive HRQ\n");
8490 goto out_error;
8491 }
8492 phba->sli4_hba.hdr_rq = qdesc;
8493
8494 /* Create Receive Queue for data */
8495 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8496 phba->sli4_hba.rq_esize,
8497 phba->sli4_hba.rq_ecount);
8498 if (!qdesc) {
8499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8500 "0507 Failed allocate receive DRQ\n");
8501 goto out_error;
8502 }
8503 phba->sli4_hba.dat_rq = qdesc;
8504
8505 if (phba->nvmet_support) {
8506 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8507 /* Create NVMET Receive Queue for header */
8508 qdesc = lpfc_sli4_queue_alloc(phba,
8509 LPFC_DEFAULT_PAGE_SIZE,
8510 phba->sli4_hba.rq_esize,
8511 LPFC_NVMET_RQE_DEF_COUNT);
8512 if (!qdesc) {
8513 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8514 "3146 Failed allocate "
8515 "receive HRQ\n");
8516 goto out_error;
8517 }
8518 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8519
8520 /* Only needed for header of RQ pair */
8521 qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8522 GFP_KERNEL);
8523 if (qdesc->rqbp == NULL) {
8524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8525 "6131 Failed allocate "
8526 "Header RQBP\n");
8527 goto out_error;
8528 }
8529
8530 /* Put list in known state in case driver load fails. */
8531 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
8532
8533 /* Create NVMET Receive Queue for data */
8534 qdesc = lpfc_sli4_queue_alloc(phba,
8535 LPFC_DEFAULT_PAGE_SIZE,
8536 phba->sli4_hba.rq_esize,
8537 LPFC_NVMET_RQE_DEF_COUNT);
8538 if (!qdesc) {
8539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8540 "3156 Failed allocate "
8541 "receive DRQ\n");
8542 goto out_error;
8543 }
8544 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8545 }
8546 }
8547
8548 /* Create the Queues needed for Flash Optimized Fabric operations */
8549 if (phba->cfg_fof)
8550 lpfc_fof_queue_create(phba);
8551 return 0;
8552
8553out_error:
8554 lpfc_sli4_queue_destroy(phba);
8555 return -ENOMEM;
8556}
8557
8558static inline void
8559__lpfc_sli4_release_queue(struct lpfc_queue **qp)
8560{
8561 if (*qp != NULL) {
8562 lpfc_sli4_queue_free(*qp);
8563 *qp = NULL;
8564 }
8565}
8566
8567static inline void
8568lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8569{
8570 int idx;
8571
8572 if (*qs == NULL)
8573 return;
8574
8575 for (idx = 0; idx < max; idx++)
8576 __lpfc_sli4_release_queue(&(*qs)[idx]);
8577
8578 kfree(*qs);
8579 *qs = NULL;
8580}
8581
8582static inline void
8583lpfc_sli4_release_queue_map(uint16_t **qmap)
8584{
8585 if (*qmap != NULL) {
8586 kfree(*qmap);
8587 *qmap = NULL;
8588 }
8589}
8590
8591/**
8592 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8593 * @phba: pointer to lpfc hba data structure.
8594 *
8595 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8596 * operation.
8597 *
8598 * Return codes
8599 * 0 - successful
8600 * -ENOMEM - No available memory
8601 * -EIO - The mailbox failed to complete successfully.
8602 **/
8603void
8604lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8605{
8606 if (phba->cfg_fof)
8607 lpfc_fof_queue_destroy(phba);
8608
8609 /* Release HBA eqs */
8610 lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
8611
8612 /* Release FCP cqs */
8613 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
8614 phba->cfg_fcp_io_channel);
8615
8616 /* Release FCP wqs */
8617 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
8618 phba->cfg_fcp_io_channel);
8619
8620 /* Release FCP CQ mapping array */
8621 lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8622
8623 /* Release NVME cqs */
8624 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8625 phba->cfg_nvme_io_channel);
8626
8627 /* Release NVME wqs */
8628 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8629 phba->cfg_nvme_io_channel);
8630
8631 /* Release NVME CQ mapping array */
8632 lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
8633
8634 if (phba->nvmet_support) {
8635 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8636 phba->cfg_nvmet_mrq);
8637
8638 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8639 phba->cfg_nvmet_mrq);
8640 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8641 phba->cfg_nvmet_mrq);
8642 }
8643
8644 /* Release mailbox command work queue */
8645 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
8646
8647 /* Release ELS work queue */
8648 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8649
8650 /* Release ELS work queue */
8651 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
8652
8653 /* Release unsolicited receive queue */
8654 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8655 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
8656
8657 /* Release ELS complete queue */
8658 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8659
8660 /* Release NVME LS complete queue */
8661 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
8662
8663 /* Release mailbox command complete queue */
8664 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8665
8666 /* Everything on this list has been freed */
8667 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8668}
8669
8670int
8671lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8672{
8673 struct lpfc_rqb *rqbp;
8674 struct lpfc_dmabuf *h_buf;
8675 struct rqb_dmabuf *rqb_buffer;
8676
8677 rqbp = rq->rqbp;
8678 while (!list_empty(&rqbp->rqb_buffer_list)) {
8679 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8680 struct lpfc_dmabuf, list);
8681
8682 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8683 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
8684 rqbp->buffer_count--;
8685 }
8686 return 1;
8687}
8688
8689static int
8690lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8691 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8692 int qidx, uint32_t qtype)
8693{
8694 struct lpfc_sli_ring *pring;
8695 int rc;
8696
8697 if (!eq || !cq || !wq) {
8698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8699 "6085 Fast-path %s (%d) not allocated\n",
8700 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8701 return -ENOMEM;
8702 }
8703
8704 /* create the Cq first */
8705 rc = lpfc_cq_create(phba, cq, eq,
8706 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8707 if (rc) {
8708 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8709 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
8710 qidx, (uint32_t)rc);
8711 return rc;
8712 }
8713 cq->chann = qidx;
8714
8715 if (qtype != LPFC_MBOX) {
8716 /* Setup nvme_cq_map for fast lookup */
8717 if (cq_map)
8718 *cq_map = cq->queue_id;
8719
8720 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8721 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8722 qidx, cq->queue_id, qidx, eq->queue_id);
8723
8724 /* create the wq */
8725 rc = lpfc_wq_create(phba, wq, cq, qtype);
8726 if (rc) {
8727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8728 "6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8729 qidx, (uint32_t)rc);
8730 /* no need to tear down cq - caller will do so */
8731 return rc;
8732 }
8733 wq->chann = qidx;
8734
8735 /* Bind this CQ/WQ to the NVME ring */
8736 pring = wq->pring;
8737 pring->sli.sli4.wqp = (void *)wq;
8738 cq->pring = pring;
8739
8740 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8741 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8742 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8743 } else {
8744 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8745 if (rc) {
8746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8747 "0539 Failed setup of slow-path MQ: "
8748 "rc = 0x%x\n", rc);
8749 /* no need to tear down cq - caller will do so */
8750 return rc;
8751 }
8752
8753 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8754 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8755 phba->sli4_hba.mbx_wq->queue_id,
8756 phba->sli4_hba.mbx_cq->queue_id);
8757 }
8758
8759 return 0;
8760}
8761
8762/**
8763 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8764 * @phba: pointer to lpfc hba data structure.
8765 *
8766 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8767 * operation.
8768 *
8769 * Return codes
8770 * 0 - successful
8771 * -ENOMEM - No available memory
8772 * -EIO - The mailbox failed to complete successfully.
8773 **/
8774int
8775lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8776{
8777 uint32_t shdr_status, shdr_add_status;
8778 union lpfc_sli4_cfg_shdr *shdr;
8779 LPFC_MBOXQ_t *mboxq;
8780 int qidx;
8781 uint32_t length, io_channel;
8782 int rc = -ENOMEM;
8783
8784 /* Check for dual-ULP support */
8785 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8786 if (!mboxq) {
8787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8788 "3249 Unable to allocate memory for "
8789 "QUERY_FW_CFG mailbox command\n");
8790 return -ENOMEM;
8791 }
8792 length = (sizeof(struct lpfc_mbx_query_fw_config) -
8793 sizeof(struct lpfc_sli4_cfg_mhdr));
8794 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8795 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8796 length, LPFC_SLI4_MBX_EMBED);
8797
8798 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8799
8800 shdr = (union lpfc_sli4_cfg_shdr *)
8801 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8802 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8803 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8804 if (shdr_status || shdr_add_status || rc) {
8805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8806 "3250 QUERY_FW_CFG mailbox failed with status "
8807 "x%x add_status x%x, mbx status x%x\n",
8808 shdr_status, shdr_add_status, rc);
8809 if (rc != MBX_TIMEOUT)
8810 mempool_free(mboxq, phba->mbox_mem_pool);
8811 rc = -ENXIO;
8812 goto out_error;
8813 }
8814
8815 phba->sli4_hba.fw_func_mode =
8816 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8817 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8818 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
8819 phba->sli4_hba.physical_port =
8820 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
8821 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8822 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8823 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8824 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8825
8826 if (rc != MBX_TIMEOUT)
8827 mempool_free(mboxq, phba->mbox_mem_pool);
8828
8829 /*
8830 * Set up HBA Event Queues (EQs)
8831 */
8832 io_channel = phba->io_channel_irqs;
8833
8834 /* Set up HBA event queue */
8835 if (io_channel && !phba->sli4_hba.hba_eq) {
8836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8837 "3147 Fast-path EQs not allocated\n");
8838 rc = -ENOMEM;
8839 goto out_error;
8840 }
8841 for (qidx = 0; qidx < io_channel; qidx++) {
8842 if (!phba->sli4_hba.hba_eq[qidx]) {
8843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8844 "0522 Fast-path EQ (%d) not "
8845 "allocated\n", qidx);
8846 rc = -ENOMEM;
8847 goto out_destroy;
8848 }
8849 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8850 phba->cfg_fcp_imax);
8851 if (rc) {
8852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8853 "0523 Failed setup of fast-path EQ "
8854 "(%d), rc = 0x%x\n", qidx,
8855 (uint32_t)rc);
8856 goto out_destroy;
8857 }
8858 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8859 "2584 HBA EQ setup: queue[%d]-id=%d\n",
8860 qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
8861 }
8862
8863 if (phba->cfg_nvme_io_channel) {
8864 if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
8865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8866 "6084 Fast-path NVME %s array not allocated\n",
8867 (phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
8868 rc = -ENOMEM;
8869 goto out_destroy;
8870 }
8871
8872 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8873 rc = lpfc_create_wq_cq(phba,
8874 phba->sli4_hba.hba_eq[
8875 qidx % io_channel],
8876 phba->sli4_hba.nvme_cq[qidx],
8877 phba->sli4_hba.nvme_wq[qidx],
8878 &phba->sli4_hba.nvme_cq_map[qidx],
8879 qidx, LPFC_NVME);
8880 if (rc) {
8881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8882 "6123 Failed to setup fastpath "
8883 "NVME WQ/CQ (%d), rc = 0x%x\n",
8884 qidx, (uint32_t)rc);
8885 goto out_destroy;
8886 }
8887 }
8888 }
8889
8890 if (phba->cfg_fcp_io_channel) {
8891 /* Set up fast-path FCP Response Complete Queue */
8892 if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
8893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8894 "3148 Fast-path FCP %s array not allocated\n",
8895 phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
8896 rc = -ENOMEM;
8897 goto out_destroy;
8898 }
8899
8900 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8901 rc = lpfc_create_wq_cq(phba,
8902 phba->sli4_hba.hba_eq[
8903 qidx % io_channel],
8904 phba->sli4_hba.fcp_cq[qidx],
8905 phba->sli4_hba.fcp_wq[qidx],
8906 &phba->sli4_hba.fcp_cq_map[qidx],
8907 qidx, LPFC_FCP);
8908 if (rc) {
8909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8910 "0535 Failed to setup fastpath "
8911 "FCP WQ/CQ (%d), rc = 0x%x\n",
8912 qidx, (uint32_t)rc);
8913 goto out_destroy;
8914 }
8915 }
8916 }
8917
8918 /*
8919 * Set up Slow Path Complete Queues (CQs)
8920 */
8921
8922 /* Set up slow-path MBOX CQ/MQ */
8923
8924 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
8925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8926 "0528 %s not allocated\n",
8927 phba->sli4_hba.mbx_cq ?
8928 "Mailbox WQ" : "Mailbox CQ");
8929 rc = -ENOMEM;
8930 goto out_destroy;
8931 }
8932
8933 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8934 phba->sli4_hba.mbx_cq,
8935 phba->sli4_hba.mbx_wq,
8936 NULL, 0, LPFC_MBOX);
8937 if (rc) {
8938 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8939 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8940 (uint32_t)rc);
8941 goto out_destroy;
8942 }
8943 if (phba->nvmet_support) {
8944 if (!phba->sli4_hba.nvmet_cqset) {
8945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8946 "3165 Fast-path NVME CQ Set "
8947 "array not allocated\n");
8948 rc = -ENOMEM;
8949 goto out_destroy;
8950 }
8951 if (phba->cfg_nvmet_mrq > 1) {
8952 rc = lpfc_cq_create_set(phba,
8953 phba->sli4_hba.nvmet_cqset,
8954 phba->sli4_hba.hba_eq,
8955 LPFC_WCQ, LPFC_NVMET);
8956 if (rc) {
8957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8958 "3164 Failed setup of NVME CQ "
8959 "Set, rc = 0x%x\n",
8960 (uint32_t)rc);
8961 goto out_destroy;
8962 }
8963 } else {
8964 /* Set up NVMET Receive Complete Queue */
8965 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8966 phba->sli4_hba.hba_eq[0],
8967 LPFC_WCQ, LPFC_NVMET);
8968 if (rc) {
8969 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8970 "6089 Failed setup NVMET CQ: "
8971 "rc = 0x%x\n", (uint32_t)rc);
8972 goto out_destroy;
8973 }
8974 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
8975
8976 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8977 "6090 NVMET CQ setup: cq-id=%d, "
8978 "parent eq-id=%d\n",
8979 phba->sli4_hba.nvmet_cqset[0]->queue_id,
8980 phba->sli4_hba.hba_eq[0]->queue_id);
8981 }
8982 }
8983
8984 /* Set up slow-path ELS WQ/CQ */
8985 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
8986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8987 "0530 ELS %s not allocated\n",
8988 phba->sli4_hba.els_cq ? "WQ" : "CQ");
8989 rc = -ENOMEM;
8990 goto out_destroy;
8991 }
8992 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8993 phba->sli4_hba.els_cq,
8994 phba->sli4_hba.els_wq,
8995 NULL, 0, LPFC_ELS);
8996 if (rc) {
8997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8998 "0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8999 (uint32_t)rc);
9000 goto out_destroy;
9001 }
9002 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9003 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
9004 phba->sli4_hba.els_wq->queue_id,
9005 phba->sli4_hba.els_cq->queue_id);
9006
9007 if (phba->cfg_nvme_io_channel) {
9008 /* Set up NVME LS Complete Queue */
9009 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
9010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9011 "6091 LS %s not allocated\n",
9012 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
9013 rc = -ENOMEM;
9014 goto out_destroy;
9015 }
9016 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
9017 phba->sli4_hba.nvmels_cq,
9018 phba->sli4_hba.nvmels_wq,
9019 NULL, 0, LPFC_NVME_LS);
9020 if (rc) {
9021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9022 "0529 Failed setup of NVVME LS WQ/CQ: "
9023 "rc = 0x%x\n", (uint32_t)rc);
9024 goto out_destroy;
9025 }
9026
9027 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9028 "6096 ELS WQ setup: wq-id=%d, "
9029 "parent cq-id=%d\n",
9030 phba->sli4_hba.nvmels_wq->queue_id,
9031 phba->sli4_hba.nvmels_cq->queue_id);
9032 }
9033
9034 /*
9035 * Create NVMET Receive Queue (RQ)
9036 */
9037 if (phba->nvmet_support) {
9038 if ((!phba->sli4_hba.nvmet_cqset) ||
9039 (!phba->sli4_hba.nvmet_mrq_hdr) ||
9040 (!phba->sli4_hba.nvmet_mrq_data)) {
9041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9042 "6130 MRQ CQ Queues not "
9043 "allocated\n");
9044 rc = -ENOMEM;
9045 goto out_destroy;
9046 }
9047 if (phba->cfg_nvmet_mrq > 1) {
9048 rc = lpfc_mrq_create(phba,
9049 phba->sli4_hba.nvmet_mrq_hdr,
9050 phba->sli4_hba.nvmet_mrq_data,
9051 phba->sli4_hba.nvmet_cqset,
9052 LPFC_NVMET);
9053 if (rc) {
9054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9055 "6098 Failed setup of NVMET "
9056 "MRQ: rc = 0x%x\n",
9057 (uint32_t)rc);
9058 goto out_destroy;
9059 }
9060
9061 } else {
9062 rc = lpfc_rq_create(phba,
9063 phba->sli4_hba.nvmet_mrq_hdr[0],
9064 phba->sli4_hba.nvmet_mrq_data[0],
9065 phba->sli4_hba.nvmet_cqset[0],
9066 LPFC_NVMET);
9067 if (rc) {
9068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9069 "6057 Failed setup of NVMET "
9070 "Receive Queue: rc = 0x%x\n",
9071 (uint32_t)rc);
9072 goto out_destroy;
9073 }
9074
9075 lpfc_printf_log(
9076 phba, KERN_INFO, LOG_INIT,
9077 "6099 NVMET RQ setup: hdr-rq-id=%d, "
9078 "dat-rq-id=%d parent cq-id=%d\n",
9079 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9080 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9081 phba->sli4_hba.nvmet_cqset[0]->queue_id);
9082
9083 }
9084 }
9085
9086 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
9087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9088 "0540 Receive Queue not allocated\n");
9089 rc = -ENOMEM;
9090 goto out_destroy;
9091 }
9092
9093 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
9094 phba->sli4_hba.els_cq, LPFC_USOL);
9095 if (rc) {
9096 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9097 "0541 Failed setup of Receive Queue: "
9098 "rc = 0x%x\n", (uint32_t)rc);
9099 goto out_destroy;
9100 }
9101
9102 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9103 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
9104 "parent cq-id=%d\n",
9105 phba->sli4_hba.hdr_rq->queue_id,
9106 phba->sli4_hba.dat_rq->queue_id,
9107 phba->sli4_hba.els_cq->queue_id);
9108
9109 if (phba->cfg_fof) {
9110 rc = lpfc_fof_queue_setup(phba);
9111 if (rc) {
9112 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9113 "0549 Failed setup of FOF Queues: "
9114 "rc = 0x%x\n", rc);
9115 goto out_destroy;
9116 }
9117 }
9118
9119 for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
9120 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
9121 phba->cfg_fcp_imax);
9122
9123 return 0;
9124
9125out_destroy:
9126 lpfc_sli4_queue_unset(phba);
9127out_error:
9128 return rc;
9129}
9130
9131/**
9132 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
9133 * @phba: pointer to lpfc hba data structure.
9134 *
9135 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
9136 * operation.
9137 *
9138 * Return codes
9139 * 0 - successful
9140 * -ENOMEM - No available memory
9141 * -EIO - The mailbox failed to complete successfully.
9142 **/
9143void
9144lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9145{
9146 int qidx;
9147
9148 /* Unset the queues created for Flash Optimized Fabric operations */
9149 if (phba->cfg_fof)
9150 lpfc_fof_queue_destroy(phba);
9151
9152 /* Unset mailbox command work queue */
9153 if (phba->sli4_hba.mbx_wq)
9154 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9155
9156 /* Unset NVME LS work queue */
9157 if (phba->sli4_hba.nvmels_wq)
9158 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9159
9160 /* Unset ELS work queue */
9161 if (phba->sli4_hba.els_wq)
9162 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9163
9164 /* Unset unsolicited receive queue */
9165 if (phba->sli4_hba.hdr_rq)
9166 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9167 phba->sli4_hba.dat_rq);
9168
9169 /* Unset FCP work queue */
9170 if (phba->sli4_hba.fcp_wq)
9171 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9172 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
9173
9174 /* Unset NVME work queue */
9175 if (phba->sli4_hba.nvme_wq) {
9176 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
9177 lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
9178 }
9179
9180 /* Unset mailbox command complete queue */
9181 if (phba->sli4_hba.mbx_cq)
9182 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9183
9184 /* Unset ELS complete queue */
9185 if (phba->sli4_hba.els_cq)
9186 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9187
9188 /* Unset NVME LS complete queue */
9189 if (phba->sli4_hba.nvmels_cq)
9190 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9191
9192 /* Unset NVME response complete queue */
9193 if (phba->sli4_hba.nvme_cq)
9194 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
9195 lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
9196
9197 if (phba->nvmet_support) {
9198 /* Unset NVMET MRQ queue */
9199 if (phba->sli4_hba.nvmet_mrq_hdr) {
9200 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9201 lpfc_rq_destroy(
9202 phba,
9203 phba->sli4_hba.nvmet_mrq_hdr[qidx],
9204 phba->sli4_hba.nvmet_mrq_data[qidx]);
9205 }
9206
9207 /* Unset NVMET CQ Set complete queue */
9208 if (phba->sli4_hba.nvmet_cqset) {
9209 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9210 lpfc_cq_destroy(
9211 phba, phba->sli4_hba.nvmet_cqset[qidx]);
9212 }
9213 }
9214
9215 /* Unset FCP response complete queue */
9216 if (phba->sli4_hba.fcp_cq)
9217 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9218 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
9219
9220 /* Unset fast-path event queue */
9221 if (phba->sli4_hba.hba_eq)
9222 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
9223 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
9224}
9225
9226/**
9227 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9228 * @phba: pointer to lpfc hba data structure.
9229 *
9230 * This routine is invoked to allocate and set up a pool of completion queue
9231 * events. The body of the completion queue event is a completion queue entry
9232 * CQE. For now, this pool is used for the interrupt service routine to queue
9233 * the following HBA completion queue events for the worker thread to process:
9234 * - Mailbox asynchronous events
9235 * - Receive queue completion unsolicited events
9236 * Later, this can be used for all the slow-path events.
9237 *
9238 * Return codes
9239 * 0 - successful
9240 * -ENOMEM - No available memory
9241 **/
9242static int
9243lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9244{
9245 struct lpfc_cq_event *cq_event;
9246 int i;
9247
9248 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9249 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9250 if (!cq_event)
9251 goto out_pool_create_fail;
9252 list_add_tail(&cq_event->list,
9253 &phba->sli4_hba.sp_cqe_event_pool);
9254 }
9255 return 0;
9256
9257out_pool_create_fail:
9258 lpfc_sli4_cq_event_pool_destroy(phba);
9259 return -ENOMEM;
9260}
9261
9262/**
9263 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9264 * @phba: pointer to lpfc hba data structure.
9265 *
9266 * This routine is invoked to free the pool of completion queue events at
9267 * driver unload time. Note that, it is the responsibility of the driver
9268 * cleanup routine to free all the outstanding completion-queue events
9269 * allocated from this pool back into the pool before invoking this routine
9270 * to destroy the pool.
9271 **/
9272static void
9273lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9274{
9275 struct lpfc_cq_event *cq_event, *next_cq_event;
9276
9277 list_for_each_entry_safe(cq_event, next_cq_event,
9278 &phba->sli4_hba.sp_cqe_event_pool, list) {
9279 list_del(&cq_event->list);
9280 kfree(cq_event);
9281 }
9282}
9283
9284/**
9285 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9286 * @phba: pointer to lpfc hba data structure.
9287 *
9288 * This routine is the lock free version of the API invoked to allocate a
9289 * completion-queue event from the free pool.
9290 *
9291 * Return: Pointer to the newly allocated completion-queue event if successful
9292 * NULL otherwise.
9293 **/
9294struct lpfc_cq_event *
9295__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9296{
9297 struct lpfc_cq_event *cq_event = NULL;
9298
9299 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9300 struct lpfc_cq_event, list);
9301 return cq_event;
9302}
9303
9304/**
9305 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9306 * @phba: pointer to lpfc hba data structure.
9307 *
9308 * This routine is the lock version of the API invoked to allocate a
9309 * completion-queue event from the free pool.
9310 *
9311 * Return: Pointer to the newly allocated completion-queue event if successful
9312 * NULL otherwise.
9313 **/
9314struct lpfc_cq_event *
9315lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9316{
9317 struct lpfc_cq_event *cq_event;
9318 unsigned long iflags;
9319
9320 spin_lock_irqsave(&phba->hbalock, iflags);
9321 cq_event = __lpfc_sli4_cq_event_alloc(phba);
9322 spin_unlock_irqrestore(&phba->hbalock, iflags);
9323 return cq_event;
9324}
9325
9326/**
9327 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9328 * @phba: pointer to lpfc hba data structure.
9329 * @cq_event: pointer to the completion queue event to be freed.
9330 *
9331 * This routine is the lock free version of the API invoked to release a
9332 * completion-queue event back into the free pool.
9333 **/
9334void
9335__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9336 struct lpfc_cq_event *cq_event)
9337{
9338 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9339}
9340
9341/**
9342 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9343 * @phba: pointer to lpfc hba data structure.
9344 * @cq_event: pointer to the completion queue event to be freed.
9345 *
9346 * This routine is the lock version of the API invoked to release a
9347 * completion-queue event back into the free pool.
9348 **/
9349void
9350lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9351 struct lpfc_cq_event *cq_event)
9352{
9353 unsigned long iflags;
9354 spin_lock_irqsave(&phba->hbalock, iflags);
9355 __lpfc_sli4_cq_event_release(phba, cq_event);
9356 spin_unlock_irqrestore(&phba->hbalock, iflags);
9357}
9358
9359/**
9360 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9361 * @phba: pointer to lpfc hba data structure.
9362 *
9363 * This routine is to free all the pending completion-queue events to the
9364 * back into the free pool for device reset.
9365 **/
9366static void
9367lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9368{
9369 LIST_HEAD(cqelist);
9370 struct lpfc_cq_event *cqe;
9371 unsigned long iflags;
9372
9373 /* Retrieve all the pending WCQEs from pending WCQE lists */
9374 spin_lock_irqsave(&phba->hbalock, iflags);
9375 /* Pending FCP XRI abort events */
9376 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9377 &cqelist);
9378 /* Pending ELS XRI abort events */
9379 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9380 &cqelist);
9381 /* Pending asynnc events */
9382 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9383 &cqelist);
9384 spin_unlock_irqrestore(&phba->hbalock, iflags);
9385
9386 while (!list_empty(&cqelist)) {
9387 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9388 lpfc_sli4_cq_event_release(phba, cqe);
9389 }
9390}
9391
9392/**
9393 * lpfc_pci_function_reset - Reset pci function.
9394 * @phba: pointer to lpfc hba data structure.
9395 *
9396 * This routine is invoked to request a PCI function reset. It will destroys
9397 * all resources assigned to the PCI function which originates this request.
9398 *
9399 * Return codes
9400 * 0 - successful
9401 * -ENOMEM - No available memory
9402 * -EIO - The mailbox failed to complete successfully.
9403 **/
9404int
9405lpfc_pci_function_reset(struct lpfc_hba *phba)
9406{
9407 LPFC_MBOXQ_t *mboxq;
9408 uint32_t rc = 0, if_type;
9409 uint32_t shdr_status, shdr_add_status;
9410 uint32_t rdy_chk;
9411 uint32_t port_reset = 0;
9412 union lpfc_sli4_cfg_shdr *shdr;
9413 struct lpfc_register reg_data;
9414 uint16_t devid;
9415
9416 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9417 switch (if_type) {
9418 case LPFC_SLI_INTF_IF_TYPE_0:
9419 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9420 GFP_KERNEL);
9421 if (!mboxq) {
9422 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9423 "0494 Unable to allocate memory for "
9424 "issuing SLI_FUNCTION_RESET mailbox "
9425 "command\n");
9426 return -ENOMEM;
9427 }
9428
9429 /* Setup PCI function reset mailbox-ioctl command */
9430 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9431 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9432 LPFC_SLI4_MBX_EMBED);
9433 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9434 shdr = (union lpfc_sli4_cfg_shdr *)
9435 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9436 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9437 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9438 &shdr->response);
9439 if (rc != MBX_TIMEOUT)
9440 mempool_free(mboxq, phba->mbox_mem_pool);
9441 if (shdr_status || shdr_add_status || rc) {
9442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9443 "0495 SLI_FUNCTION_RESET mailbox "
9444 "failed with status x%x add_status x%x,"
9445 " mbx status x%x\n",
9446 shdr_status, shdr_add_status, rc);
9447 rc = -ENXIO;
9448 }
9449 break;
9450 case LPFC_SLI_INTF_IF_TYPE_2:
9451 case LPFC_SLI_INTF_IF_TYPE_6:
9452wait:
9453 /*
9454 * Poll the Port Status Register and wait for RDY for
9455 * up to 30 seconds. If the port doesn't respond, treat
9456 * it as an error.
9457 */
9458 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
9459 if (lpfc_readl(phba->sli4_hba.u.if_type2.
9460 STATUSregaddr, &reg_data.word0)) {
9461 rc = -ENODEV;
9462 goto out;
9463 }
9464 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9465 break;
9466 msleep(20);
9467 }
9468
9469 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9470 phba->work_status[0] = readl(
9471 phba->sli4_hba.u.if_type2.ERR1regaddr);
9472 phba->work_status[1] = readl(
9473 phba->sli4_hba.u.if_type2.ERR2regaddr);
9474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9475 "2890 Port not ready, port status reg "
9476 "0x%x error 1=0x%x, error 2=0x%x\n",
9477 reg_data.word0,
9478 phba->work_status[0],
9479 phba->work_status[1]);
9480 rc = -ENODEV;
9481 goto out;
9482 }
9483
9484 if (!port_reset) {
9485 /*
9486 * Reset the port now
9487 */
9488 reg_data.word0 = 0;
9489 bf_set(lpfc_sliport_ctrl_end, &reg_data,
9490 LPFC_SLIPORT_LITTLE_ENDIAN);
9491 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9492 LPFC_SLIPORT_INIT_PORT);
9493 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9494 CTRLregaddr);
9495 /* flush */
9496 pci_read_config_word(phba->pcidev,
9497 PCI_DEVICE_ID, &devid);
9498
9499 port_reset = 1;
9500 msleep(20);
9501 goto wait;
9502 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9503 rc = -ENODEV;
9504 goto out;
9505 }
9506 break;
9507
9508 case LPFC_SLI_INTF_IF_TYPE_1:
9509 default:
9510 break;
9511 }
9512
9513out:
9514 /* Catch the not-ready port failure after a port reset. */
9515 if (rc) {
9516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9517 "3317 HBA not functional: IP Reset Failed "
9518 "try: echo fw_reset > board_mode\n");
9519 rc = -ENODEV;
9520 }
9521
9522 return rc;
9523}
9524
9525/**
9526 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9527 * @phba: pointer to lpfc hba data structure.
9528 *
9529 * This routine is invoked to set up the PCI device memory space for device
9530 * with SLI-4 interface spec.
9531 *
9532 * Return codes
9533 * 0 - successful
9534 * other values - error
9535 **/
9536static int
9537lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9538{
9539 struct pci_dev *pdev;
9540 unsigned long bar0map_len, bar1map_len, bar2map_len;
9541 int error = -ENODEV;
9542 uint32_t if_type;
9543
9544 /* Obtain PCI device reference */
9545 if (!phba->pcidev)
9546 return error;
9547 else
9548 pdev = phba->pcidev;
9549
9550 /* Set the device DMA mask size */
9551 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9552 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9553 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9554 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
9555 return error;
9556 }
9557 }
9558
9559 /*
9560 * The BARs and register set definitions and offset locations are
9561 * dependent on the if_type.
9562 */
9563 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9564 &phba->sli4_hba.sli_intf.word0)) {
9565 return error;
9566 }
9567
9568 /* There is no SLI3 failback for SLI4 devices. */
9569 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9570 LPFC_SLI_INTF_VALID) {
9571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9572 "2894 SLI_INTF reg contents invalid "
9573 "sli_intf reg 0x%x\n",
9574 phba->sli4_hba.sli_intf.word0);
9575 return error;
9576 }
9577
9578 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9579 /*
9580 * Get the bus address of SLI4 device Bar regions and the
9581 * number of bytes required by each mapping. The mapping of the
9582 * particular PCI BARs regions is dependent on the type of
9583 * SLI4 device.
9584 */
9585 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9586 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9587 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
9588
9589 /*
9590 * Map SLI4 PCI Config Space Register base to a kernel virtual
9591 * addr
9592 */
9593 phba->sli4_hba.conf_regs_memmap_p =
9594 ioremap(phba->pci_bar0_map, bar0map_len);
9595 if (!phba->sli4_hba.conf_regs_memmap_p) {
9596 dev_printk(KERN_ERR, &pdev->dev,
9597 "ioremap failed for SLI4 PCI config "
9598 "registers.\n");
9599 goto out;
9600 }
9601 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
9602 /* Set up BAR0 PCI config space register memory map */
9603 lpfc_sli4_bar0_register_memmap(phba, if_type);
9604 } else {
9605 phba->pci_bar0_map = pci_resource_start(pdev, 1);
9606 bar0map_len = pci_resource_len(pdev, 1);
9607 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9608 dev_printk(KERN_ERR, &pdev->dev,
9609 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9610 goto out;
9611 }
9612 phba->sli4_hba.conf_regs_memmap_p =
9613 ioremap(phba->pci_bar0_map, bar0map_len);
9614 if (!phba->sli4_hba.conf_regs_memmap_p) {
9615 dev_printk(KERN_ERR, &pdev->dev,
9616 "ioremap failed for SLI4 PCI config "
9617 "registers.\n");
9618 goto out;
9619 }
9620 lpfc_sli4_bar0_register_memmap(phba, if_type);
9621 }
9622
9623 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9624 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
9625 /*
9626 * Map SLI4 if type 0 HBA Control Register base to a
9627 * kernel virtual address and setup the registers.
9628 */
9629 phba->pci_bar1_map = pci_resource_start(pdev,
9630 PCI_64BIT_BAR2);
9631 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9632 phba->sli4_hba.ctrl_regs_memmap_p =
9633 ioremap(phba->pci_bar1_map,
9634 bar1map_len);
9635 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9636 dev_err(&pdev->dev,
9637 "ioremap failed for SLI4 HBA "
9638 "control registers.\n");
9639 error = -ENOMEM;
9640 goto out_iounmap_conf;
9641 }
9642 phba->pci_bar2_memmap_p =
9643 phba->sli4_hba.ctrl_regs_memmap_p;
9644 lpfc_sli4_bar1_register_memmap(phba, if_type);
9645 } else {
9646 error = -ENOMEM;
9647 goto out_iounmap_conf;
9648 }
9649 }
9650
9651 if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
9652 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
9653 /*
9654 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
9655 * virtual address and setup the registers.
9656 */
9657 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
9658 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9659 phba->sli4_hba.drbl_regs_memmap_p =
9660 ioremap(phba->pci_bar1_map, bar1map_len);
9661 if (!phba->sli4_hba.drbl_regs_memmap_p) {
9662 dev_err(&pdev->dev,
9663 "ioremap failed for SLI4 HBA doorbell registers.\n");
9664 goto out_iounmap_conf;
9665 }
9666 phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
9667 lpfc_sli4_bar1_register_memmap(phba, if_type);
9668 }
9669
9670 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9671 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
9672 /*
9673 * Map SLI4 if type 0 HBA Doorbell Register base to
9674 * a kernel virtual address and setup the registers.
9675 */
9676 phba->pci_bar2_map = pci_resource_start(pdev,
9677 PCI_64BIT_BAR4);
9678 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9679 phba->sli4_hba.drbl_regs_memmap_p =
9680 ioremap(phba->pci_bar2_map,
9681 bar2map_len);
9682 if (!phba->sli4_hba.drbl_regs_memmap_p) {
9683 dev_err(&pdev->dev,
9684 "ioremap failed for SLI4 HBA"
9685 " doorbell registers.\n");
9686 error = -ENOMEM;
9687 goto out_iounmap_ctrl;
9688 }
9689 phba->pci_bar4_memmap_p =
9690 phba->sli4_hba.drbl_regs_memmap_p;
9691 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9692 if (error)
9693 goto out_iounmap_all;
9694 } else {
9695 error = -ENOMEM;
9696 goto out_iounmap_all;
9697 }
9698 }
9699
9700 if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
9701 pci_resource_start(pdev, PCI_64BIT_BAR4)) {
9702 /*
9703 * Map SLI4 if type 6 HBA DPP Register base to a kernel
9704 * virtual address and setup the registers.
9705 */
9706 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
9707 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9708 phba->sli4_hba.dpp_regs_memmap_p =
9709 ioremap(phba->pci_bar2_map, bar2map_len);
9710 if (!phba->sli4_hba.dpp_regs_memmap_p) {
9711 dev_err(&pdev->dev,
9712 "ioremap failed for SLI4 HBA dpp registers.\n");
9713 goto out_iounmap_ctrl;
9714 }
9715 phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
9716 }
9717
9718 /* Set up the EQ/CQ register handeling functions now */
9719 switch (if_type) {
9720 case LPFC_SLI_INTF_IF_TYPE_0:
9721 case LPFC_SLI_INTF_IF_TYPE_2:
9722 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
9723 phba->sli4_hba.sli4_eq_release = lpfc_sli4_eq_release;
9724 phba->sli4_hba.sli4_cq_release = lpfc_sli4_cq_release;
9725 break;
9726 case LPFC_SLI_INTF_IF_TYPE_6:
9727 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
9728 phba->sli4_hba.sli4_eq_release = lpfc_sli4_if6_eq_release;
9729 phba->sli4_hba.sli4_cq_release = lpfc_sli4_if6_cq_release;
9730 break;
9731 default:
9732 break;
9733 }
9734
9735 return 0;
9736
9737out_iounmap_all:
9738 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9739out_iounmap_ctrl:
9740 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9741out_iounmap_conf:
9742 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9743out:
9744 return error;
9745}
9746
9747/**
9748 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9749 * @phba: pointer to lpfc hba data structure.
9750 *
9751 * This routine is invoked to unset the PCI device memory space for device
9752 * with SLI-4 interface spec.
9753 **/
9754static void
9755lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9756{
9757 uint32_t if_type;
9758 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9759
9760 switch (if_type) {
9761 case LPFC_SLI_INTF_IF_TYPE_0:
9762 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9763 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9764 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9765 break;
9766 case LPFC_SLI_INTF_IF_TYPE_2:
9767 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9768 break;
9769 case LPFC_SLI_INTF_IF_TYPE_6:
9770 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9771 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9772 break;
9773 case LPFC_SLI_INTF_IF_TYPE_1:
9774 default:
9775 dev_printk(KERN_ERR, &phba->pcidev->dev,
9776 "FATAL - unsupported SLI4 interface type - %d\n",
9777 if_type);
9778 break;
9779 }
9780}
9781
9782/**
9783 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9784 * @phba: pointer to lpfc hba data structure.
9785 *
9786 * This routine is invoked to enable the MSI-X interrupt vectors to device
9787 * with SLI-3 interface specs.
9788 *
9789 * Return codes
9790 * 0 - successful
9791 * other values - error
9792 **/
9793static int
9794lpfc_sli_enable_msix(struct lpfc_hba *phba)
9795{
9796 int rc;
9797 LPFC_MBOXQ_t *pmb;
9798
9799 /* Set up MSI-X multi-message vectors */
9800 rc = pci_alloc_irq_vectors(phba->pcidev,
9801 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9802 if (rc < 0) {
9803 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9804 "0420 PCI enable MSI-X failed (%d)\n", rc);
9805 goto vec_fail_out;
9806 }
9807
9808 /*
9809 * Assign MSI-X vectors to interrupt handlers
9810 */
9811
9812 /* vector-0 is associated to slow-path handler */
9813 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
9814 &lpfc_sli_sp_intr_handler, 0,
9815 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9816 if (rc) {
9817 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9818 "0421 MSI-X slow-path request_irq failed "
9819 "(%d)\n", rc);
9820 goto msi_fail_out;
9821 }
9822
9823 /* vector-1 is associated to fast-path handler */
9824 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
9825 &lpfc_sli_fp_intr_handler, 0,
9826 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9827
9828 if (rc) {
9829 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9830 "0429 MSI-X fast-path request_irq failed "
9831 "(%d)\n", rc);
9832 goto irq_fail_out;
9833 }
9834
9835 /*
9836 * Configure HBA MSI-X attention conditions to messages
9837 */
9838 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9839
9840 if (!pmb) {
9841 rc = -ENOMEM;
9842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9843 "0474 Unable to allocate memory for issuing "
9844 "MBOX_CONFIG_MSI command\n");
9845 goto mem_fail_out;
9846 }
9847 rc = lpfc_config_msi(phba, pmb);
9848 if (rc)
9849 goto mbx_fail_out;
9850 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9851 if (rc != MBX_SUCCESS) {
9852 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9853 "0351 Config MSI mailbox command failed, "
9854 "mbxCmd x%x, mbxStatus x%x\n",
9855 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9856 goto mbx_fail_out;
9857 }
9858
9859 /* Free memory allocated for mailbox command */
9860 mempool_free(pmb, phba->mbox_mem_pool);
9861 return rc;
9862
9863mbx_fail_out:
9864 /* Free memory allocated for mailbox command */
9865 mempool_free(pmb, phba->mbox_mem_pool);
9866
9867mem_fail_out:
9868 /* free the irq already requested */
9869 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
9870
9871irq_fail_out:
9872 /* free the irq already requested */
9873 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
9874
9875msi_fail_out:
9876 /* Unconfigure MSI-X capability structure */
9877 pci_free_irq_vectors(phba->pcidev);
9878
9879vec_fail_out:
9880 return rc;
9881}
9882
9883/**
9884 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9885 * @phba: pointer to lpfc hba data structure.
9886 *
9887 * This routine is invoked to enable the MSI interrupt mode to device with
9888 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9889 * enable the MSI vector. The device driver is responsible for calling the
9890 * request_irq() to register MSI vector with a interrupt the handler, which
9891 * is done in this function.
9892 *
9893 * Return codes
9894 * 0 - successful
9895 * other values - error
9896 */
9897static int
9898lpfc_sli_enable_msi(struct lpfc_hba *phba)
9899{
9900 int rc;
9901
9902 rc = pci_enable_msi(phba->pcidev);
9903 if (!rc)
9904 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9905 "0462 PCI enable MSI mode success.\n");
9906 else {
9907 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9908 "0471 PCI enable MSI mode failed (%d)\n", rc);
9909 return rc;
9910 }
9911
9912 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9913 0, LPFC_DRIVER_NAME, phba);
9914 if (rc) {
9915 pci_disable_msi(phba->pcidev);
9916 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9917 "0478 MSI request_irq failed (%d)\n", rc);
9918 }
9919 return rc;
9920}
9921
9922/**
9923 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9924 * @phba: pointer to lpfc hba data structure.
9925 *
9926 * This routine is invoked to enable device interrupt and associate driver's
9927 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9928 * spec. Depends on the interrupt mode configured to the driver, the driver
9929 * will try to fallback from the configured interrupt mode to an interrupt
9930 * mode which is supported by the platform, kernel, and device in the order
9931 * of:
9932 * MSI-X -> MSI -> IRQ.
9933 *
9934 * Return codes
9935 * 0 - successful
9936 * other values - error
9937 **/
9938static uint32_t
9939lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9940{
9941 uint32_t intr_mode = LPFC_INTR_ERROR;
9942 int retval;
9943
9944 if (cfg_mode == 2) {
9945 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9946 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9947 if (!retval) {
9948 /* Now, try to enable MSI-X interrupt mode */
9949 retval = lpfc_sli_enable_msix(phba);
9950 if (!retval) {
9951 /* Indicate initialization to MSI-X mode */
9952 phba->intr_type = MSIX;
9953 intr_mode = 2;
9954 }
9955 }
9956 }
9957
9958 /* Fallback to MSI if MSI-X initialization failed */
9959 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9960 retval = lpfc_sli_enable_msi(phba);
9961 if (!retval) {
9962 /* Indicate initialization to MSI mode */
9963 phba->intr_type = MSI;
9964 intr_mode = 1;
9965 }
9966 }
9967
9968 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9969 if (phba->intr_type == NONE) {
9970 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9971 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9972 if (!retval) {
9973 /* Indicate initialization to INTx mode */
9974 phba->intr_type = INTx;
9975 intr_mode = 0;
9976 }
9977 }
9978 return intr_mode;
9979}
9980
9981/**
9982 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9983 * @phba: pointer to lpfc hba data structure.
9984 *
9985 * This routine is invoked to disable device interrupt and disassociate the
9986 * driver's interrupt handler(s) from interrupt vector(s) to device with
9987 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9988 * release the interrupt vector(s) for the message signaled interrupt.
9989 **/
9990static void
9991lpfc_sli_disable_intr(struct lpfc_hba *phba)
9992{
9993 int nr_irqs, i;
9994
9995 if (phba->intr_type == MSIX)
9996 nr_irqs = LPFC_MSIX_VECTORS;
9997 else
9998 nr_irqs = 1;
9999
10000 for (i = 0; i < nr_irqs; i++)
10001 free_irq(pci_irq_vector(phba->pcidev, i), phba);
10002 pci_free_irq_vectors(phba->pcidev);
10003
10004 /* Reset interrupt management states */
10005 phba->intr_type = NONE;
10006 phba->sli.slistat.sli_intr = 0;
10007}
10008
10009/**
10010 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
10011 * @phba: pointer to lpfc hba data structure.
10012 * @vectors: number of msix vectors allocated.
10013 *
10014 * The routine will figure out the CPU affinity assignment for every
10015 * MSI-X vector allocated for the HBA. The hba_eq_hdl will be updated
10016 * with a pointer to the CPU mask that defines ALL the CPUs this vector
10017 * can be associated with. If the vector can be unquely associated with
10018 * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
10019 * In addition, the CPU to IO channel mapping will be calculated
10020 * and the phba->sli4_hba.cpu_map array will reflect this.
10021 */
10022static void
10023lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
10024{
10025 struct lpfc_vector_map_info *cpup;
10026 int index = 0;
10027 int vec = 0;
10028 int cpu;
10029#ifdef CONFIG_X86
10030 struct cpuinfo_x86 *cpuinfo;
10031#endif
10032
10033 /* Init cpu_map array */
10034 memset(phba->sli4_hba.cpu_map, 0xff,
10035 (sizeof(struct lpfc_vector_map_info) *
10036 phba->sli4_hba.num_present_cpu));
10037
10038 /* Update CPU map with physical id and core id of each CPU */
10039 cpup = phba->sli4_hba.cpu_map;
10040 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
10041#ifdef CONFIG_X86
10042 cpuinfo = &cpu_data(cpu);
10043 cpup->phys_id = cpuinfo->phys_proc_id;
10044 cpup->core_id = cpuinfo->cpu_core_id;
10045#else
10046 /* No distinction between CPUs for other platforms */
10047 cpup->phys_id = 0;
10048 cpup->core_id = 0;
10049#endif
10050 cpup->channel_id = index; /* For now round robin */
10051 cpup->irq = pci_irq_vector(phba->pcidev, vec);
10052 vec++;
10053 if (vec >= vectors)
10054 vec = 0;
10055 index++;
10056 if (index >= phba->cfg_fcp_io_channel)
10057 index = 0;
10058 cpup++;
10059 }
10060}
10061
10062
10063/**
10064 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
10065 * @phba: pointer to lpfc hba data structure.
10066 *
10067 * This routine is invoked to enable the MSI-X interrupt vectors to device
10068 * with SLI-4 interface spec.
10069 *
10070 * Return codes
10071 * 0 - successful
10072 * other values - error
10073 **/
10074static int
10075lpfc_sli4_enable_msix(struct lpfc_hba *phba)
10076{
10077 int vectors, rc, index;
10078 char *name;
10079
10080 /* Set up MSI-X multi-message vectors */
10081 vectors = phba->io_channel_irqs;
10082 if (phba->cfg_fof)
10083 vectors++;
10084
10085 rc = pci_alloc_irq_vectors(phba->pcidev,
10086 (phba->nvmet_support) ? 1 : 2,
10087 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
10088 if (rc < 0) {
10089 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10090 "0484 PCI enable MSI-X failed (%d)\n", rc);
10091 goto vec_fail_out;
10092 }
10093 vectors = rc;
10094
10095 /* Assign MSI-X vectors to interrupt handlers */
10096 for (index = 0; index < vectors; index++) {
10097 name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
10098 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
10099 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
10100 LPFC_DRIVER_HANDLER_NAME"%d", index);
10101
10102 phba->sli4_hba.hba_eq_hdl[index].idx = index;
10103 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
10104 atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
10105 if (phba->cfg_fof && (index == (vectors - 1)))
10106 rc = request_irq(pci_irq_vector(phba->pcidev, index),
10107 &lpfc_sli4_fof_intr_handler, 0,
10108 name,
10109 &phba->sli4_hba.hba_eq_hdl[index]);
10110 else
10111 rc = request_irq(pci_irq_vector(phba->pcidev, index),
10112 &lpfc_sli4_hba_intr_handler, 0,
10113 name,
10114 &phba->sli4_hba.hba_eq_hdl[index]);
10115 if (rc) {
10116 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10117 "0486 MSI-X fast-path (%d) "
10118 "request_irq failed (%d)\n", index, rc);
10119 goto cfg_fail_out;
10120 }
10121 }
10122
10123 if (phba->cfg_fof)
10124 vectors--;
10125
10126 if (vectors != phba->io_channel_irqs) {
10127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10128 "3238 Reducing IO channels to match number of "
10129 "MSI-X vectors, requested %d got %d\n",
10130 phba->io_channel_irqs, vectors);
10131 if (phba->cfg_fcp_io_channel > vectors)
10132 phba->cfg_fcp_io_channel = vectors;
10133 if (phba->cfg_nvme_io_channel > vectors)
10134 phba->cfg_nvme_io_channel = vectors;
10135 if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
10136 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10137 else
10138 phba->io_channel_irqs = phba->cfg_nvme_io_channel;
10139 }
10140 lpfc_cpu_affinity_check(phba, vectors);
10141
10142 return rc;
10143
10144cfg_fail_out:
10145 /* free the irq already requested */
10146 for (--index; index >= 0; index--)
10147 free_irq(pci_irq_vector(phba->pcidev, index),
10148 &phba->sli4_hba.hba_eq_hdl[index]);
10149
10150 /* Unconfigure MSI-X capability structure */
10151 pci_free_irq_vectors(phba->pcidev);
10152
10153vec_fail_out:
10154 return rc;
10155}
10156
10157/**
10158 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
10159 * @phba: pointer to lpfc hba data structure.
10160 *
10161 * This routine is invoked to enable the MSI interrupt mode to device with
10162 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
10163 * to enable the MSI vector. The device driver is responsible for calling
10164 * the request_irq() to register MSI vector with a interrupt the handler,
10165 * which is done in this function.
10166 *
10167 * Return codes
10168 * 0 - successful
10169 * other values - error
10170 **/
10171static int
10172lpfc_sli4_enable_msi(struct lpfc_hba *phba)
10173{
10174 int rc, index;
10175
10176 rc = pci_enable_msi(phba->pcidev);
10177 if (!rc)
10178 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10179 "0487 PCI enable MSI mode success.\n");
10180 else {
10181 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10182 "0488 PCI enable MSI mode failed (%d)\n", rc);
10183 return rc;
10184 }
10185
10186 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
10187 0, LPFC_DRIVER_NAME, phba);
10188 if (rc) {
10189 pci_disable_msi(phba->pcidev);
10190 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10191 "0490 MSI request_irq failed (%d)\n", rc);
10192 return rc;
10193 }
10194
10195 for (index = 0; index < phba->io_channel_irqs; index++) {
10196 phba->sli4_hba.hba_eq_hdl[index].idx = index;
10197 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
10198 }
10199
10200 if (phba->cfg_fof) {
10201 phba->sli4_hba.hba_eq_hdl[index].idx = index;
10202 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
10203 }
10204 return 0;
10205}
10206
10207/**
10208 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
10209 * @phba: pointer to lpfc hba data structure.
10210 *
10211 * This routine is invoked to enable device interrupt and associate driver's
10212 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
10213 * interface spec. Depends on the interrupt mode configured to the driver,
10214 * the driver will try to fallback from the configured interrupt mode to an
10215 * interrupt mode which is supported by the platform, kernel, and device in
10216 * the order of:
10217 * MSI-X -> MSI -> IRQ.
10218 *
10219 * Return codes
10220 * 0 - successful
10221 * other values - error
10222 **/
10223static uint32_t
10224lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10225{
10226 uint32_t intr_mode = LPFC_INTR_ERROR;
10227 int retval, idx;
10228
10229 if (cfg_mode == 2) {
10230 /* Preparation before conf_msi mbox cmd */
10231 retval = 0;
10232 if (!retval) {
10233 /* Now, try to enable MSI-X interrupt mode */
10234 retval = lpfc_sli4_enable_msix(phba);
10235 if (!retval) {
10236 /* Indicate initialization to MSI-X mode */
10237 phba->intr_type = MSIX;
10238 intr_mode = 2;
10239 }
10240 }
10241 }
10242
10243 /* Fallback to MSI if MSI-X initialization failed */
10244 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10245 retval = lpfc_sli4_enable_msi(phba);
10246 if (!retval) {
10247 /* Indicate initialization to MSI mode */
10248 phba->intr_type = MSI;
10249 intr_mode = 1;
10250 }
10251 }
10252
10253 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10254 if (phba->intr_type == NONE) {
10255 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
10256 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10257 if (!retval) {
10258 struct lpfc_hba_eq_hdl *eqhdl;
10259
10260 /* Indicate initialization to INTx mode */
10261 phba->intr_type = INTx;
10262 intr_mode = 0;
10263
10264 for (idx = 0; idx < phba->io_channel_irqs; idx++) {
10265 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10266 eqhdl->idx = idx;
10267 eqhdl->phba = phba;
10268 atomic_set(&eqhdl->hba_eq_in_use, 1);
10269 }
10270 if (phba->cfg_fof) {
10271 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10272 eqhdl->idx = idx;
10273 eqhdl->phba = phba;
10274 atomic_set(&eqhdl->hba_eq_in_use, 1);
10275 }
10276 }
10277 }
10278 return intr_mode;
10279}
10280
10281/**
10282 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
10283 * @phba: pointer to lpfc hba data structure.
10284 *
10285 * This routine is invoked to disable device interrupt and disassociate
10286 * the driver's interrupt handler(s) from interrupt vector(s) to device
10287 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
10288 * will release the interrupt vector(s) for the message signaled interrupt.
10289 **/
10290static void
10291lpfc_sli4_disable_intr(struct lpfc_hba *phba)
10292{
10293 /* Disable the currently initialized interrupt mode */
10294 if (phba->intr_type == MSIX) {
10295 int index;
10296
10297 /* Free up MSI-X multi-message vectors */
10298 for (index = 0; index < phba->io_channel_irqs; index++)
10299 free_irq(pci_irq_vector(phba->pcidev, index),
10300 &phba->sli4_hba.hba_eq_hdl[index]);
10301
10302 if (phba->cfg_fof)
10303 free_irq(pci_irq_vector(phba->pcidev, index),
10304 &phba->sli4_hba.hba_eq_hdl[index]);
10305 } else {
10306 free_irq(phba->pcidev->irq, phba);
10307 }
10308
10309 pci_free_irq_vectors(phba->pcidev);
10310
10311 /* Reset interrupt management states */
10312 phba->intr_type = NONE;
10313 phba->sli.slistat.sli_intr = 0;
10314}
10315
10316/**
10317 * lpfc_unset_hba - Unset SLI3 hba device initialization
10318 * @phba: pointer to lpfc hba data structure.
10319 *
10320 * This routine is invoked to unset the HBA device initialization steps to
10321 * a device with SLI-3 interface spec.
10322 **/
10323static void
10324lpfc_unset_hba(struct lpfc_hba *phba)
10325{
10326 struct lpfc_vport *vport = phba->pport;
10327 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10328
10329 spin_lock_irq(shost->host_lock);
10330 vport->load_flag |= FC_UNLOADING;
10331 spin_unlock_irq(shost->host_lock);
10332
10333 kfree(phba->vpi_bmask);
10334 kfree(phba->vpi_ids);
10335
10336 lpfc_stop_hba_timers(phba);
10337
10338 phba->pport->work_port_events = 0;
10339
10340 lpfc_sli_hba_down(phba);
10341
10342 lpfc_sli_brdrestart(phba);
10343
10344 lpfc_sli_disable_intr(phba);
10345
10346 return;
10347}
10348
10349/**
10350 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10351 * @phba: Pointer to HBA context object.
10352 *
10353 * This function is called in the SLI4 code path to wait for completion
10354 * of device's XRIs exchange busy. It will check the XRI exchange busy
10355 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10356 * that, it will check the XRI exchange busy on outstanding FCP and ELS
10357 * I/Os every 30 seconds, log error message, and wait forever. Only when
10358 * all XRI exchange busy complete, the driver unload shall proceed with
10359 * invoking the function reset ioctl mailbox command to the CNA and the
10360 * the rest of the driver unload resource release.
10361 **/
10362static void
10363lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
10364{
10365 int wait_time = 0;
10366 int nvme_xri_cmpl = 1;
10367 int nvmet_xri_cmpl = 1;
10368 int fcp_xri_cmpl = 1;
10369 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10370
10371 /* Driver just aborted IOs during the hba_unset process. Pause
10372 * here to give the HBA time to complete the IO and get entries
10373 * into the abts lists.
10374 */
10375 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
10376
10377 /* Wait for NVME pending IO to flush back to transport. */
10378 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
10379 lpfc_nvme_wait_for_io_drain(phba);
10380
10381 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10382 fcp_xri_cmpl =
10383 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10384 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10385 nvme_xri_cmpl =
10386 list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10387 nvmet_xri_cmpl =
10388 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10389 }
10390
10391 while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
10392 !nvmet_xri_cmpl) {
10393 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
10394 if (!nvmet_xri_cmpl)
10395 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10396 "6424 NVMET XRI exchange busy "
10397 "wait time: %d seconds.\n",
10398 wait_time/1000);
10399 if (!nvme_xri_cmpl)
10400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10401 "6100 NVME XRI exchange busy "
10402 "wait time: %d seconds.\n",
10403 wait_time/1000);
10404 if (!fcp_xri_cmpl)
10405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10406 "2877 FCP XRI exchange busy "
10407 "wait time: %d seconds.\n",
10408 wait_time/1000);
10409 if (!els_xri_cmpl)
10410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10411 "2878 ELS XRI exchange busy "
10412 "wait time: %d seconds.\n",
10413 wait_time/1000);
10414 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
10415 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
10416 } else {
10417 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
10418 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
10419 }
10420 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10421 nvme_xri_cmpl = list_empty(
10422 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
10423 nvmet_xri_cmpl = list_empty(
10424 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10425 }
10426
10427 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10428 fcp_xri_cmpl = list_empty(
10429 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
10430
10431 els_xri_cmpl =
10432 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10433
10434 }
10435}
10436
10437/**
10438 * lpfc_sli4_hba_unset - Unset the fcoe hba
10439 * @phba: Pointer to HBA context object.
10440 *
10441 * This function is called in the SLI4 code path to reset the HBA's FCoE
10442 * function. The caller is not required to hold any lock. This routine
10443 * issues PCI function reset mailbox command to reset the FCoE function.
10444 * At the end of the function, it calls lpfc_hba_down_post function to
10445 * free any pending commands.
10446 **/
10447static void
10448lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10449{
10450 int wait_cnt = 0;
10451 LPFC_MBOXQ_t *mboxq;
10452 struct pci_dev *pdev = phba->pcidev;
10453
10454 lpfc_stop_hba_timers(phba);
10455 phba->sli4_hba.intr_enable = 0;
10456
10457 /*
10458 * Gracefully wait out the potential current outstanding asynchronous
10459 * mailbox command.
10460 */
10461
10462 /* First, block any pending async mailbox command from posted */
10463 spin_lock_irq(&phba->hbalock);
10464 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10465 spin_unlock_irq(&phba->hbalock);
10466 /* Now, trying to wait it out if we can */
10467 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10468 msleep(10);
10469 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10470 break;
10471 }
10472 /* Forcefully release the outstanding mailbox command if timed out */
10473 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10474 spin_lock_irq(&phba->hbalock);
10475 mboxq = phba->sli.mbox_active;
10476 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10477 __lpfc_mbox_cmpl_put(phba, mboxq);
10478 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10479 phba->sli.mbox_active = NULL;
10480 spin_unlock_irq(&phba->hbalock);
10481 }
10482
10483 /* Abort all iocbs associated with the hba */
10484 lpfc_sli_hba_iocb_abort(phba);
10485
10486 /* Wait for completion of device XRI exchange busy */
10487 lpfc_sli4_xri_exchange_busy_wait(phba);
10488
10489 /* Disable PCI subsystem interrupt */
10490 lpfc_sli4_disable_intr(phba);
10491
10492 /* Disable SR-IOV if enabled */
10493 if (phba->cfg_sriov_nr_virtfn)
10494 pci_disable_sriov(pdev);
10495
10496 /* Stop kthread signal shall trigger work_done one more time */
10497 kthread_stop(phba->worker_thread);
10498
10499 /* Unset the queues shared with the hardware then release all
10500 * allocated resources.
10501 */
10502 lpfc_sli4_queue_unset(phba);
10503 lpfc_sli4_queue_destroy(phba);
10504
10505 /* Reset SLI4 HBA FCoE function */
10506 lpfc_pci_function_reset(phba);
10507
10508 /* Stop the SLI4 device port */
10509 phba->pport->work_port_events = 0;
10510}
10511
10512 /**
10513 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10514 * @phba: Pointer to HBA context object.
10515 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10516 *
10517 * This function is called in the SLI4 code path to read the port's
10518 * sli4 capabilities.
10519 *
10520 * This function may be be called from any context that can block-wait
10521 * for the completion. The expectation is that this routine is called
10522 * typically from probe_one or from the online routine.
10523 **/
10524int
10525lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10526{
10527 int rc;
10528 struct lpfc_mqe *mqe;
10529 struct lpfc_pc_sli4_params *sli4_params;
10530 uint32_t mbox_tmo;
10531
10532 rc = 0;
10533 mqe = &mboxq->u.mqe;
10534
10535 /* Read the port's SLI4 Parameters port capabilities */
10536 lpfc_pc_sli4_params(mboxq);
10537 if (!phba->sli4_hba.intr_enable)
10538 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10539 else {
10540 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10541 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10542 }
10543
10544 if (unlikely(rc))
10545 return 1;
10546
10547 sli4_params = &phba->sli4_hba.pc_sli4_params;
10548 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10549 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10550 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10551 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10552 &mqe->un.sli4_params);
10553 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10554 &mqe->un.sli4_params);
10555 sli4_params->proto_types = mqe->un.sli4_params.word3;
10556 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10557 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10558 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10559 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10560 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10561 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10562 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10563 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10564 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10565 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10566 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10567 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10568 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10569 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10570 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10571 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10572 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10573 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10574 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10575 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
10576
10577 /* Make sure that sge_supp_len can be handled by the driver */
10578 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10579 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10580
10581 return rc;
10582}
10583
10584/**
10585 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10586 * @phba: Pointer to HBA context object.
10587 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10588 *
10589 * This function is called in the SLI4 code path to read the port's
10590 * sli4 capabilities.
10591 *
10592 * This function may be be called from any context that can block-wait
10593 * for the completion. The expectation is that this routine is called
10594 * typically from probe_one or from the online routine.
10595 **/
10596int
10597lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10598{
10599 int rc;
10600 struct lpfc_mqe *mqe = &mboxq->u.mqe;
10601 struct lpfc_pc_sli4_params *sli4_params;
10602 uint32_t mbox_tmo;
10603 int length;
10604 bool exp_wqcq_pages = true;
10605 struct lpfc_sli4_parameters *mbx_sli4_parameters;
10606
10607 /*
10608 * By default, the driver assumes the SLI4 port requires RPI
10609 * header postings. The SLI4_PARAM response will correct this
10610 * assumption.
10611 */
10612 phba->sli4_hba.rpi_hdrs_in_use = 1;
10613
10614 /* Read the port's SLI4 Config Parameters */
10615 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10616 sizeof(struct lpfc_sli4_cfg_mhdr));
10617 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10618 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10619 length, LPFC_SLI4_MBX_EMBED);
10620 if (!phba->sli4_hba.intr_enable)
10621 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10622 else {
10623 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10624 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10625 }
10626 if (unlikely(rc))
10627 return rc;
10628 sli4_params = &phba->sli4_hba.pc_sli4_params;
10629 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10630 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10631 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10632 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10633 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10634 mbx_sli4_parameters);
10635 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10636 mbx_sli4_parameters);
10637 if (bf_get(cfg_phwq, mbx_sli4_parameters))
10638 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10639 else
10640 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10641 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10642 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
10643 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
10644 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10645 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10646 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10647 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
10648 sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
10649 sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
10650 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
10651 sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
10652 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10653 mbx_sli4_parameters);
10654 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
10655 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10656 mbx_sli4_parameters);
10657 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10658 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
10659 phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10660 bf_get(cfg_xib, mbx_sli4_parameters));
10661
10662 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10663 !phba->nvme_support) {
10664 phba->nvme_support = 0;
10665 phba->nvmet_support = 0;
10666 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_OFF;
10667 phba->cfg_nvme_io_channel = 0;
10668 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10670 "6101 Disabling NVME support: "
10671 "Not supported by firmware: %d %d\n",
10672 bf_get(cfg_nvme, mbx_sli4_parameters),
10673 bf_get(cfg_xib, mbx_sli4_parameters));
10674
10675 /* If firmware doesn't support NVME, just use SCSI support */
10676 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10677 return -ENODEV;
10678 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10679 }
10680
10681 /* Only embed PBDE for if_type 6, PBDE support requires xib be set */
10682 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10683 LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
10684 phba->cfg_enable_pbde = 0;
10685
10686 /*
10687 * To support Suppress Response feature we must satisfy 3 conditions.
10688 * lpfc_suppress_rsp module parameter must be set (default).
10689 * In SLI4-Parameters Descriptor:
10690 * Extended Inline Buffers (XIB) must be supported.
10691 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
10692 * (double negative).
10693 */
10694 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
10695 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
10696 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
10697 else
10698 phba->cfg_suppress_rsp = 0;
10699
10700 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
10701 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
10702
10703 /* Make sure that sge_supp_len can be handled by the driver */
10704 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10705 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10706
10707 /*
10708 * Check whether the adapter supports an embedded copy of the
10709 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
10710 * to use this option, 128-byte WQEs must be used.
10711 */
10712 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10713 phba->fcp_embed_io = 1;
10714 else
10715 phba->fcp_embed_io = 0;
10716
10717 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
10718 "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
10719 bf_get(cfg_xib, mbx_sli4_parameters),
10720 phba->cfg_enable_pbde,
10721 phba->fcp_embed_io, phba->nvme_support,
10722 phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
10723
10724 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
10725 LPFC_SLI_INTF_IF_TYPE_2) &&
10726 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
10727 LPFC_SLI_INTF_FAMILY_LNCR_A0))
10728 exp_wqcq_pages = false;
10729
10730 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
10731 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
10732 exp_wqcq_pages &&
10733 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
10734 phba->enab_exp_wqcq_pages = 1;
10735 else
10736 phba->enab_exp_wqcq_pages = 0;
10737 /*
10738 * Check if the SLI port supports MDS Diagnostics
10739 */
10740 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10741 phba->mds_diags_support = 1;
10742 else
10743 phba->mds_diags_support = 0;
10744 return 0;
10745}
10746
10747/**
10748 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10749 * @pdev: pointer to PCI device
10750 * @pid: pointer to PCI device identifier
10751 *
10752 * This routine is to be called to attach a device with SLI-3 interface spec
10753 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10754 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10755 * information of the device and driver to see if the driver state that it can
10756 * support this kind of device. If the match is successful, the driver core
10757 * invokes this routine. If this routine determines it can claim the HBA, it
10758 * does all the initialization that it needs to do to handle the HBA properly.
10759 *
10760 * Return code
10761 * 0 - driver can claim the device
10762 * negative value - driver can not claim the device
10763 **/
10764static int
10765lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10766{
10767 struct lpfc_hba *phba;
10768 struct lpfc_vport *vport = NULL;
10769 struct Scsi_Host *shost = NULL;
10770 int error;
10771 uint32_t cfg_mode, intr_mode;
10772
10773 /* Allocate memory for HBA structure */
10774 phba = lpfc_hba_alloc(pdev);
10775 if (!phba)
10776 return -ENOMEM;
10777
10778 /* Perform generic PCI device enabling operation */
10779 error = lpfc_enable_pci_dev(phba);
10780 if (error)
10781 goto out_free_phba;
10782
10783 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
10784 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10785 if (error)
10786 goto out_disable_pci_dev;
10787
10788 /* Set up SLI-3 specific device PCI memory space */
10789 error = lpfc_sli_pci_mem_setup(phba);
10790 if (error) {
10791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10792 "1402 Failed to set up pci memory space.\n");
10793 goto out_disable_pci_dev;
10794 }
10795
10796 /* Set up SLI-3 specific device driver resources */
10797 error = lpfc_sli_driver_resource_setup(phba);
10798 if (error) {
10799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10800 "1404 Failed to set up driver resource.\n");
10801 goto out_unset_pci_mem_s3;
10802 }
10803
10804 /* Initialize and populate the iocb list per host */
10805
10806 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10807 if (error) {
10808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10809 "1405 Failed to initialize iocb list.\n");
10810 goto out_unset_driver_resource_s3;
10811 }
10812
10813 /* Set up common device driver resources */
10814 error = lpfc_setup_driver_resource_phase2(phba);
10815 if (error) {
10816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10817 "1406 Failed to set up driver resource.\n");
10818 goto out_free_iocb_list;
10819 }
10820
10821 /* Get the default values for Model Name and Description */
10822 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10823
10824 /* Create SCSI host to the physical port */
10825 error = lpfc_create_shost(phba);
10826 if (error) {
10827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10828 "1407 Failed to create scsi host.\n");
10829 goto out_unset_driver_resource;
10830 }
10831
10832 /* Configure sysfs attributes */
10833 vport = phba->pport;
10834 error = lpfc_alloc_sysfs_attr(vport);
10835 if (error) {
10836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10837 "1476 Failed to allocate sysfs attr\n");
10838 goto out_destroy_shost;
10839 }
10840
10841 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
10842 /* Now, trying to enable interrupt and bring up the device */
10843 cfg_mode = phba->cfg_use_msi;
10844 while (true) {
10845 /* Put device to a known state before enabling interrupt */
10846 lpfc_stop_port(phba);
10847 /* Configure and enable interrupt */
10848 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10849 if (intr_mode == LPFC_INTR_ERROR) {
10850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10851 "0431 Failed to enable interrupt.\n");
10852 error = -ENODEV;
10853 goto out_free_sysfs_attr;
10854 }
10855 /* SLI-3 HBA setup */
10856 if (lpfc_sli_hba_setup(phba)) {
10857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10858 "1477 Failed to set up hba\n");
10859 error = -ENODEV;
10860 goto out_remove_device;
10861 }
10862
10863 /* Wait 50ms for the interrupts of previous mailbox commands */
10864 msleep(50);
10865 /* Check active interrupts on message signaled interrupts */
10866 if (intr_mode == 0 ||
10867 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10868 /* Log the current active interrupt mode */
10869 phba->intr_mode = intr_mode;
10870 lpfc_log_intr_mode(phba, intr_mode);
10871 break;
10872 } else {
10873 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10874 "0447 Configure interrupt mode (%d) "
10875 "failed active interrupt test.\n",
10876 intr_mode);
10877 /* Disable the current interrupt mode */
10878 lpfc_sli_disable_intr(phba);
10879 /* Try next level of interrupt mode */
10880 cfg_mode = --intr_mode;
10881 }
10882 }
10883
10884 /* Perform post initialization setup */
10885 lpfc_post_init_setup(phba);
10886
10887 /* Check if there are static vports to be created. */
10888 lpfc_create_static_vport(phba);
10889
10890 return 0;
10891
10892out_remove_device:
10893 lpfc_unset_hba(phba);
10894out_free_sysfs_attr:
10895 lpfc_free_sysfs_attr(vport);
10896out_destroy_shost:
10897 lpfc_destroy_shost(phba);
10898out_unset_driver_resource:
10899 lpfc_unset_driver_resource_phase2(phba);
10900out_free_iocb_list:
10901 lpfc_free_iocb_list(phba);
10902out_unset_driver_resource_s3:
10903 lpfc_sli_driver_resource_unset(phba);
10904out_unset_pci_mem_s3:
10905 lpfc_sli_pci_mem_unset(phba);
10906out_disable_pci_dev:
10907 lpfc_disable_pci_dev(phba);
10908 if (shost)
10909 scsi_host_put(shost);
10910out_free_phba:
10911 lpfc_hba_free(phba);
10912 return error;
10913}
10914
10915/**
10916 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
10917 * @pdev: pointer to PCI device
10918 *
10919 * This routine is to be called to disattach a device with SLI-3 interface
10920 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10921 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10922 * device to be removed from the PCI subsystem properly.
10923 **/
10924static void
10925lpfc_pci_remove_one_s3(struct pci_dev *pdev)
10926{
10927 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10928 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10929 struct lpfc_vport **vports;
10930 struct lpfc_hba *phba = vport->phba;
10931 int i;
10932
10933 spin_lock_irq(&phba->hbalock);
10934 vport->load_flag |= FC_UNLOADING;
10935 spin_unlock_irq(&phba->hbalock);
10936
10937 lpfc_free_sysfs_attr(vport);
10938
10939 /* Release all the vports against this physical port */
10940 vports = lpfc_create_vport_work_array(phba);
10941 if (vports != NULL)
10942 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10943 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10944 continue;
10945 fc_vport_terminate(vports[i]->fc_vport);
10946 }
10947 lpfc_destroy_vport_work_array(phba, vports);
10948
10949 /* Remove FC host and then SCSI host with the physical port */
10950 fc_remove_host(shost);
10951 scsi_remove_host(shost);
10952
10953 lpfc_cleanup(vport);
10954
10955 /*
10956 * Bring down the SLI Layer. This step disable all interrupts,
10957 * clears the rings, discards all mailbox commands, and resets
10958 * the HBA.
10959 */
10960
10961 /* HBA interrupt will be disabled after this call */
10962 lpfc_sli_hba_down(phba);
10963 /* Stop kthread signal shall trigger work_done one more time */
10964 kthread_stop(phba->worker_thread);
10965 /* Final cleanup of txcmplq and reset the HBA */
10966 lpfc_sli_brdrestart(phba);
10967
10968 kfree(phba->vpi_bmask);
10969 kfree(phba->vpi_ids);
10970
10971 lpfc_stop_hba_timers(phba);
10972 spin_lock_irq(&phba->hbalock);
10973 list_del_init(&vport->listentry);
10974 spin_unlock_irq(&phba->hbalock);
10975
10976 lpfc_debugfs_terminate(vport);
10977
10978 /* Disable SR-IOV if enabled */
10979 if (phba->cfg_sriov_nr_virtfn)
10980 pci_disable_sriov(pdev);
10981
10982 /* Disable interrupt */
10983 lpfc_sli_disable_intr(phba);
10984
10985 scsi_host_put(shost);
10986
10987 /*
10988 * Call scsi_free before mem_free since scsi bufs are released to their
10989 * corresponding pools here.
10990 */
10991 lpfc_scsi_free(phba);
10992 lpfc_mem_free_all(phba);
10993
10994 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10995 phba->hbqslimp.virt, phba->hbqslimp.phys);
10996
10997 /* Free resources associated with SLI2 interface */
10998 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
10999 phba->slim2p.virt, phba->slim2p.phys);
11000
11001 /* unmap adapter SLIM and Control Registers */
11002 iounmap(phba->ctrl_regs_memmap_p);
11003 iounmap(phba->slim_memmap_p);
11004
11005 lpfc_hba_free(phba);
11006
11007 pci_release_mem_regions(pdev);
11008 pci_disable_device(pdev);
11009}
11010
11011/**
11012 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
11013 * @pdev: pointer to PCI device
11014 * @msg: power management message
11015 *
11016 * This routine is to be called from the kernel's PCI subsystem to support
11017 * system Power Management (PM) to device with SLI-3 interface spec. When
11018 * PM invokes this method, it quiesces the device by stopping the driver's
11019 * worker thread for the device, turning off device's interrupt and DMA,
11020 * and bring the device offline. Note that as the driver implements the
11021 * minimum PM requirements to a power-aware driver's PM support for the
11022 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11023 * to the suspend() method call will be treated as SUSPEND and the driver will
11024 * fully reinitialize its device during resume() method call, the driver will
11025 * set device to PCI_D3hot state in PCI config space instead of setting it
11026 * according to the @msg provided by the PM.
11027 *
11028 * Return code
11029 * 0 - driver suspended the device
11030 * Error otherwise
11031 **/
11032static int
11033lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
11034{
11035 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11036 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11037
11038 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11039 "0473 PCI device Power Management suspend.\n");
11040
11041 /* Bring down the device */
11042 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11043 lpfc_offline(phba);
11044 kthread_stop(phba->worker_thread);
11045
11046 /* Disable interrupt from device */
11047 lpfc_sli_disable_intr(phba);
11048
11049 /* Save device state to PCI config space */
11050 pci_save_state(pdev);
11051 pci_set_power_state(pdev, PCI_D3hot);
11052
11053 return 0;
11054}
11055
11056/**
11057 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
11058 * @pdev: pointer to PCI device
11059 *
11060 * This routine is to be called from the kernel's PCI subsystem to support
11061 * system Power Management (PM) to device with SLI-3 interface spec. When PM
11062 * invokes this method, it restores the device's PCI config space state and
11063 * fully reinitializes the device and brings it online. Note that as the
11064 * driver implements the minimum PM requirements to a power-aware driver's
11065 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
11066 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
11067 * driver will fully reinitialize its device during resume() method call,
11068 * the device will be set to PCI_D0 directly in PCI config space before
11069 * restoring the state.
11070 *
11071 * Return code
11072 * 0 - driver suspended the device
11073 * Error otherwise
11074 **/
11075static int
11076lpfc_pci_resume_one_s3(struct pci_dev *pdev)
11077{
11078 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11079 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11080 uint32_t intr_mode;
11081 int error;
11082
11083 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11084 "0452 PCI device Power Management resume.\n");
11085
11086 /* Restore device state from PCI config space */
11087 pci_set_power_state(pdev, PCI_D0);
11088 pci_restore_state(pdev);
11089
11090 /*
11091 * As the new kernel behavior of pci_restore_state() API call clears
11092 * device saved_state flag, need to save the restored state again.
11093 */
11094 pci_save_state(pdev);
11095
11096 if (pdev->is_busmaster)
11097 pci_set_master(pdev);
11098
11099 /* Startup the kernel thread for this host adapter. */
11100 phba->worker_thread = kthread_run(lpfc_do_work, phba,
11101 "lpfc_worker_%d", phba->brd_no);
11102 if (IS_ERR(phba->worker_thread)) {
11103 error = PTR_ERR(phba->worker_thread);
11104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11105 "0434 PM resume failed to start worker "
11106 "thread: error=x%x.\n", error);
11107 return error;
11108 }
11109
11110 /* Configure and enable interrupt */
11111 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
11112 if (intr_mode == LPFC_INTR_ERROR) {
11113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11114 "0430 PM resume Failed to enable interrupt\n");
11115 return -EIO;
11116 } else
11117 phba->intr_mode = intr_mode;
11118
11119 /* Restart HBA and bring it online */
11120 lpfc_sli_brdrestart(phba);
11121 lpfc_online(phba);
11122
11123 /* Log the current active interrupt mode */
11124 lpfc_log_intr_mode(phba, phba->intr_mode);
11125
11126 return 0;
11127}
11128
11129/**
11130 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
11131 * @phba: pointer to lpfc hba data structure.
11132 *
11133 * This routine is called to prepare the SLI3 device for PCI slot recover. It
11134 * aborts all the outstanding SCSI I/Os to the pci device.
11135 **/
11136static void
11137lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
11138{
11139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11140 "2723 PCI channel I/O abort preparing for recovery\n");
11141
11142 /*
11143 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11144 * and let the SCSI mid-layer to retry them to recover.
11145 */
11146 lpfc_sli_abort_fcp_rings(phba);
11147}
11148
11149/**
11150 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
11151 * @phba: pointer to lpfc hba data structure.
11152 *
11153 * This routine is called to prepare the SLI3 device for PCI slot reset. It
11154 * disables the device interrupt and pci device, and aborts the internal FCP
11155 * pending I/Os.
11156 **/
11157static void
11158lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
11159{
11160 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11161 "2710 PCI channel disable preparing for reset\n");
11162
11163 /* Block any management I/Os to the device */
11164 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
11165
11166 /* Block all SCSI devices' I/Os on the host */
11167 lpfc_scsi_dev_block(phba);
11168
11169 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
11170 lpfc_sli_flush_fcp_rings(phba);
11171
11172 /* stop all timers */
11173 lpfc_stop_hba_timers(phba);
11174
11175 /* Disable interrupt and pci device */
11176 lpfc_sli_disable_intr(phba);
11177 pci_disable_device(phba->pcidev);
11178}
11179
11180/**
11181 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
11182 * @phba: pointer to lpfc hba data structure.
11183 *
11184 * This routine is called to prepare the SLI3 device for PCI slot permanently
11185 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11186 * pending I/Os.
11187 **/
11188static void
11189lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11190{
11191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11192 "2711 PCI channel permanent disable for failure\n");
11193 /* Block all SCSI devices' I/Os on the host */
11194 lpfc_scsi_dev_block(phba);
11195
11196 /* stop all timers */
11197 lpfc_stop_hba_timers(phba);
11198
11199 /* Clean up all driver's outstanding SCSI I/Os */
11200 lpfc_sli_flush_fcp_rings(phba);
11201}
11202
11203/**
11204 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
11205 * @pdev: pointer to PCI device.
11206 * @state: the current PCI connection state.
11207 *
11208 * This routine is called from the PCI subsystem for I/O error handling to
11209 * device with SLI-3 interface spec. This function is called by the PCI
11210 * subsystem after a PCI bus error affecting this device has been detected.
11211 * When this function is invoked, it will need to stop all the I/Os and
11212 * interrupt(s) to the device. Once that is done, it will return
11213 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
11214 * as desired.
11215 *
11216 * Return codes
11217 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
11218 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11219 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11220 **/
11221static pci_ers_result_t
11222lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
11223{
11224 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11225 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11226
11227 switch (state) {
11228 case pci_channel_io_normal:
11229 /* Non-fatal error, prepare for recovery */
11230 lpfc_sli_prep_dev_for_recover(phba);
11231 return PCI_ERS_RESULT_CAN_RECOVER;
11232 case pci_channel_io_frozen:
11233 /* Fatal error, prepare for slot reset */
11234 lpfc_sli_prep_dev_for_reset(phba);
11235 return PCI_ERS_RESULT_NEED_RESET;
11236 case pci_channel_io_perm_failure:
11237 /* Permanent failure, prepare for device down */
11238 lpfc_sli_prep_dev_for_perm_failure(phba);
11239 return PCI_ERS_RESULT_DISCONNECT;
11240 default:
11241 /* Unknown state, prepare and request slot reset */
11242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11243 "0472 Unknown PCI error state: x%x\n", state);
11244 lpfc_sli_prep_dev_for_reset(phba);
11245 return PCI_ERS_RESULT_NEED_RESET;
11246 }
11247}
11248
11249/**
11250 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
11251 * @pdev: pointer to PCI device.
11252 *
11253 * This routine is called from the PCI subsystem for error handling to
11254 * device with SLI-3 interface spec. This is called after PCI bus has been
11255 * reset to restart the PCI card from scratch, as if from a cold-boot.
11256 * During the PCI subsystem error recovery, after driver returns
11257 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11258 * recovery and then call this routine before calling the .resume method
11259 * to recover the device. This function will initialize the HBA device,
11260 * enable the interrupt, but it will just put the HBA to offline state
11261 * without passing any I/O traffic.
11262 *
11263 * Return codes
11264 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11265 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11266 */
11267static pci_ers_result_t
11268lpfc_io_slot_reset_s3(struct pci_dev *pdev)
11269{
11270 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11271 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11272 struct lpfc_sli *psli = &phba->sli;
11273 uint32_t intr_mode;
11274
11275 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11276 if (pci_enable_device_mem(pdev)) {
11277 printk(KERN_ERR "lpfc: Cannot re-enable "
11278 "PCI device after reset.\n");
11279 return PCI_ERS_RESULT_DISCONNECT;
11280 }
11281
11282 pci_restore_state(pdev);
11283
11284 /*
11285 * As the new kernel behavior of pci_restore_state() API call clears
11286 * device saved_state flag, need to save the restored state again.
11287 */
11288 pci_save_state(pdev);
11289
11290 if (pdev->is_busmaster)
11291 pci_set_master(pdev);
11292
11293 spin_lock_irq(&phba->hbalock);
11294 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11295 spin_unlock_irq(&phba->hbalock);
11296
11297 /* Configure and enable interrupt */
11298 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
11299 if (intr_mode == LPFC_INTR_ERROR) {
11300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11301 "0427 Cannot re-enable interrupt after "
11302 "slot reset.\n");
11303 return PCI_ERS_RESULT_DISCONNECT;
11304 } else
11305 phba->intr_mode = intr_mode;
11306
11307 /* Take device offline, it will perform cleanup */
11308 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11309 lpfc_offline(phba);
11310 lpfc_sli_brdrestart(phba);
11311
11312 /* Log the current active interrupt mode */
11313 lpfc_log_intr_mode(phba, phba->intr_mode);
11314
11315 return PCI_ERS_RESULT_RECOVERED;
11316}
11317
11318/**
11319 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
11320 * @pdev: pointer to PCI device
11321 *
11322 * This routine is called from the PCI subsystem for error handling to device
11323 * with SLI-3 interface spec. It is called when kernel error recovery tells
11324 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11325 * error recovery. After this call, traffic can start to flow from this device
11326 * again.
11327 */
11328static void
11329lpfc_io_resume_s3(struct pci_dev *pdev)
11330{
11331 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11332 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11333
11334 /* Bring device online, it will be no-op for non-fatal error resume */
11335 lpfc_online(phba);
11336
11337 /* Clean up Advanced Error Reporting (AER) if needed */
11338 if (phba->hba_flag & HBA_AER_ENABLED)
11339 pci_cleanup_aer_uncorrect_error_status(pdev);
11340}
11341
11342/**
11343 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
11344 * @phba: pointer to lpfc hba data structure.
11345 *
11346 * returns the number of ELS/CT IOCBs to reserve
11347 **/
11348int
11349lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
11350{
11351 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
11352
11353 if (phba->sli_rev == LPFC_SLI_REV4) {
11354 if (max_xri <= 100)
11355 return 10;
11356 else if (max_xri <= 256)
11357 return 25;
11358 else if (max_xri <= 512)
11359 return 50;
11360 else if (max_xri <= 1024)
11361 return 100;
11362 else if (max_xri <= 1536)
11363 return 150;
11364 else if (max_xri <= 2048)
11365 return 200;
11366 else
11367 return 250;
11368 } else
11369 return 0;
11370}
11371
11372/**
11373 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
11374 * @phba: pointer to lpfc hba data structure.
11375 *
11376 * returns the number of ELS/CT + NVMET IOCBs to reserve
11377 **/
11378int
11379lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
11380{
11381 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
11382
11383 if (phba->nvmet_support)
11384 max_xri += LPFC_NVMET_BUF_POST;
11385 return max_xri;
11386}
11387
11388
11389static void
11390lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
11391 uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
11392 const struct firmware *fw)
11393{
11394 if ((offset == ADD_STATUS_FW_NOT_SUPPORTED) ||
11395 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
11396 magic_number != MAGIC_NUMER_G6) ||
11397 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
11398 magic_number != MAGIC_NUMER_G7))
11399 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11400 "3030 This firmware version is not supported on "
11401 "this HBA model. Device:%x Magic:%x Type:%x "
11402 "ID:%x Size %d %zd\n",
11403 phba->pcidev->device, magic_number, ftype, fid,
11404 fsize, fw->size);
11405 else
11406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11407 "3022 FW Download failed. Device:%x Magic:%x Type:%x "
11408 "ID:%x Size %d %zd\n",
11409 phba->pcidev->device, magic_number, ftype, fid,
11410 fsize, fw->size);
11411}
11412
11413
11414/**
11415 * lpfc_write_firmware - attempt to write a firmware image to the port
11416 * @fw: pointer to firmware image returned from request_firmware.
11417 * @phba: pointer to lpfc hba data structure.
11418 *
11419 **/
11420static void
11421lpfc_write_firmware(const struct firmware *fw, void *context)
11422{
11423 struct lpfc_hba *phba = (struct lpfc_hba *)context;
11424 char fwrev[FW_REV_STR_SIZE];
11425 struct lpfc_grp_hdr *image;
11426 struct list_head dma_buffer_list;
11427 int i, rc = 0;
11428 struct lpfc_dmabuf *dmabuf, *next;
11429 uint32_t offset = 0, temp_offset = 0;
11430 uint32_t magic_number, ftype, fid, fsize;
11431
11432 /* It can be null in no-wait mode, sanity check */
11433 if (!fw) {
11434 rc = -ENXIO;
11435 goto out;
11436 }
11437 image = (struct lpfc_grp_hdr *)fw->data;
11438
11439 magic_number = be32_to_cpu(image->magic_number);
11440 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
11441 fid = bf_get_be32(lpfc_grp_hdr_id, image);
11442 fsize = be32_to_cpu(image->size);
11443
11444 INIT_LIST_HEAD(&dma_buffer_list);
11445 lpfc_decode_firmware_rev(phba, fwrev, 1);
11446 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
11447 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11448 "3023 Updating Firmware, Current Version:%s "
11449 "New Version:%s\n",
11450 fwrev, image->revision);
11451 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
11452 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
11453 GFP_KERNEL);
11454 if (!dmabuf) {
11455 rc = -ENOMEM;
11456 goto release_out;
11457 }
11458 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11459 SLI4_PAGE_SIZE,
11460 &dmabuf->phys,
11461 GFP_KERNEL);
11462 if (!dmabuf->virt) {
11463 kfree(dmabuf);
11464 rc = -ENOMEM;
11465 goto release_out;
11466 }
11467 list_add_tail(&dmabuf->list, &dma_buffer_list);
11468 }
11469 while (offset < fw->size) {
11470 temp_offset = offset;
11471 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
11472 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
11473 memcpy(dmabuf->virt,
11474 fw->data + temp_offset,
11475 fw->size - temp_offset);
11476 temp_offset = fw->size;
11477 break;
11478 }
11479 memcpy(dmabuf->virt, fw->data + temp_offset,
11480 SLI4_PAGE_SIZE);
11481 temp_offset += SLI4_PAGE_SIZE;
11482 }
11483 rc = lpfc_wr_object(phba, &dma_buffer_list,
11484 (fw->size - offset), &offset);
11485 if (rc) {
11486 lpfc_log_write_firmware_error(phba, offset,
11487 magic_number, ftype, fid, fsize, fw);
11488 goto release_out;
11489 }
11490 }
11491 rc = offset;
11492 } else
11493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11494 "3029 Skipped Firmware update, Current "
11495 "Version:%s New Version:%s\n",
11496 fwrev, image->revision);
11497
11498release_out:
11499 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
11500 list_del(&dmabuf->list);
11501 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
11502 dmabuf->virt, dmabuf->phys);
11503 kfree(dmabuf);
11504 }
11505 release_firmware(fw);
11506out:
11507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11508 "3024 Firmware update done: %d.\n", rc);
11509 return;
11510}
11511
11512/**
11513 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11514 * @phba: pointer to lpfc hba data structure.
11515 *
11516 * This routine is called to perform Linux generic firmware upgrade on device
11517 * that supports such feature.
11518 **/
11519int
11520lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11521{
11522 uint8_t file_name[ELX_MODEL_NAME_SIZE];
11523 int ret;
11524 const struct firmware *fw;
11525
11526 /* Only supported on SLI4 interface type 2 for now */
11527 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
11528 LPFC_SLI_INTF_IF_TYPE_2)
11529 return -EPERM;
11530
11531 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11532
11533 if (fw_upgrade == INT_FW_UPGRADE) {
11534 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11535 file_name, &phba->pcidev->dev,
11536 GFP_KERNEL, (void *)phba,
11537 lpfc_write_firmware);
11538 } else if (fw_upgrade == RUN_FW_UPGRADE) {
11539 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11540 if (!ret)
11541 lpfc_write_firmware(fw, (void *)phba);
11542 } else {
11543 ret = -EINVAL;
11544 }
11545
11546 return ret;
11547}
11548
11549/**
11550 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11551 * @pdev: pointer to PCI device
11552 * @pid: pointer to PCI device identifier
11553 *
11554 * This routine is called from the kernel's PCI subsystem to device with
11555 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11556 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11557 * information of the device and driver to see if the driver state that it
11558 * can support this kind of device. If the match is successful, the driver
11559 * core invokes this routine. If this routine determines it can claim the HBA,
11560 * it does all the initialization that it needs to do to handle the HBA
11561 * properly.
11562 *
11563 * Return code
11564 * 0 - driver can claim the device
11565 * negative value - driver can not claim the device
11566 **/
11567static int
11568lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11569{
11570 struct lpfc_hba *phba;
11571 struct lpfc_vport *vport = NULL;
11572 struct Scsi_Host *shost = NULL;
11573 int error;
11574 uint32_t cfg_mode, intr_mode;
11575
11576 /* Allocate memory for HBA structure */
11577 phba = lpfc_hba_alloc(pdev);
11578 if (!phba)
11579 return -ENOMEM;
11580
11581 /* Perform generic PCI device enabling operation */
11582 error = lpfc_enable_pci_dev(phba);
11583 if (error)
11584 goto out_free_phba;
11585
11586 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
11587 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11588 if (error)
11589 goto out_disable_pci_dev;
11590
11591 /* Set up SLI-4 specific device PCI memory space */
11592 error = lpfc_sli4_pci_mem_setup(phba);
11593 if (error) {
11594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11595 "1410 Failed to set up pci memory space.\n");
11596 goto out_disable_pci_dev;
11597 }
11598
11599 /* Set up SLI-4 Specific device driver resources */
11600 error = lpfc_sli4_driver_resource_setup(phba);
11601 if (error) {
11602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11603 "1412 Failed to set up driver resource.\n");
11604 goto out_unset_pci_mem_s4;
11605 }
11606
11607 INIT_LIST_HEAD(&phba->active_rrq_list);
11608 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
11609
11610 /* Set up common device driver resources */
11611 error = lpfc_setup_driver_resource_phase2(phba);
11612 if (error) {
11613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11614 "1414 Failed to set up driver resource.\n");
11615 goto out_unset_driver_resource_s4;
11616 }
11617
11618 /* Get the default values for Model Name and Description */
11619 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11620
11621 /* Create SCSI host to the physical port */
11622 error = lpfc_create_shost(phba);
11623 if (error) {
11624 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11625 "1415 Failed to create scsi host.\n");
11626 goto out_unset_driver_resource;
11627 }
11628
11629 /* Configure sysfs attributes */
11630 vport = phba->pport;
11631 error = lpfc_alloc_sysfs_attr(vport);
11632 if (error) {
11633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11634 "1416 Failed to allocate sysfs attr\n");
11635 goto out_destroy_shost;
11636 }
11637
11638 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
11639 /* Now, trying to enable interrupt and bring up the device */
11640 cfg_mode = phba->cfg_use_msi;
11641
11642 /* Put device to a known state before enabling interrupt */
11643 lpfc_stop_port(phba);
11644
11645 /* Configure and enable interrupt */
11646 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11647 if (intr_mode == LPFC_INTR_ERROR) {
11648 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11649 "0426 Failed to enable interrupt.\n");
11650 error = -ENODEV;
11651 goto out_free_sysfs_attr;
11652 }
11653 /* Default to single EQ for non-MSI-X */
11654 if (phba->intr_type != MSIX) {
11655 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11656 phba->cfg_fcp_io_channel = 1;
11657 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11658 phba->cfg_nvme_io_channel = 1;
11659 if (phba->nvmet_support)
11660 phba->cfg_nvmet_mrq = 1;
11661 }
11662 phba->io_channel_irqs = 1;
11663 }
11664
11665 /* Set up SLI-4 HBA */
11666 if (lpfc_sli4_hba_setup(phba)) {
11667 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11668 "1421 Failed to set up hba\n");
11669 error = -ENODEV;
11670 goto out_disable_intr;
11671 }
11672
11673 /* Log the current active interrupt mode */
11674 phba->intr_mode = intr_mode;
11675 lpfc_log_intr_mode(phba, intr_mode);
11676
11677 /* Perform post initialization setup */
11678 lpfc_post_init_setup(phba);
11679
11680 /* NVME support in FW earlier in the driver load corrects the
11681 * FC4 type making a check for nvme_support unnecessary.
11682 */
11683 if ((phba->nvmet_support == 0) &&
11684 (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11685 /* Create NVME binding with nvme_fc_transport. This
11686 * ensures the vport is initialized. If the localport
11687 * create fails, it should not unload the driver to
11688 * support field issues.
11689 */
11690 error = lpfc_nvme_create_localport(vport);
11691 if (error) {
11692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11693 "6004 NVME registration failed, "
11694 "error x%x\n",
11695 error);
11696 }
11697 }
11698
11699 /* check for firmware upgrade or downgrade */
11700 if (phba->cfg_request_firmware_upgrade)
11701 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
11702
11703 /* Check if there are static vports to be created. */
11704 lpfc_create_static_vport(phba);
11705 return 0;
11706
11707out_disable_intr:
11708 lpfc_sli4_disable_intr(phba);
11709out_free_sysfs_attr:
11710 lpfc_free_sysfs_attr(vport);
11711out_destroy_shost:
11712 lpfc_destroy_shost(phba);
11713out_unset_driver_resource:
11714 lpfc_unset_driver_resource_phase2(phba);
11715out_unset_driver_resource_s4:
11716 lpfc_sli4_driver_resource_unset(phba);
11717out_unset_pci_mem_s4:
11718 lpfc_sli4_pci_mem_unset(phba);
11719out_disable_pci_dev:
11720 lpfc_disable_pci_dev(phba);
11721 if (shost)
11722 scsi_host_put(shost);
11723out_free_phba:
11724 lpfc_hba_free(phba);
11725 return error;
11726}
11727
11728/**
11729 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11730 * @pdev: pointer to PCI device
11731 *
11732 * This routine is called from the kernel's PCI subsystem to device with
11733 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11734 * removed from PCI bus, it performs all the necessary cleanup for the HBA
11735 * device to be removed from the PCI subsystem properly.
11736 **/
11737static void
11738lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11739{
11740 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11741 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11742 struct lpfc_vport **vports;
11743 struct lpfc_hba *phba = vport->phba;
11744 int i;
11745
11746 /* Mark the device unloading flag */
11747 spin_lock_irq(&phba->hbalock);
11748 vport->load_flag |= FC_UNLOADING;
11749 spin_unlock_irq(&phba->hbalock);
11750
11751 /* Free the HBA sysfs attributes */
11752 lpfc_free_sysfs_attr(vport);
11753
11754 /* Release all the vports against this physical port */
11755 vports = lpfc_create_vport_work_array(phba);
11756 if (vports != NULL)
11757 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11758 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11759 continue;
11760 fc_vport_terminate(vports[i]->fc_vport);
11761 }
11762 lpfc_destroy_vport_work_array(phba, vports);
11763
11764 /* Remove FC host and then SCSI host with the physical port */
11765 fc_remove_host(shost);
11766 scsi_remove_host(shost);
11767
11768 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
11769 * localports are destroyed after to cleanup all transport memory.
11770 */
11771 lpfc_cleanup(vport);
11772 lpfc_nvmet_destroy_targetport(phba);
11773 lpfc_nvme_destroy_localport(vport);
11774
11775 /*
11776 * Bring down the SLI Layer. This step disables all interrupts,
11777 * clears the rings, discards all mailbox commands, and resets
11778 * the HBA FCoE function.
11779 */
11780 lpfc_debugfs_terminate(vport);
11781 lpfc_sli4_hba_unset(phba);
11782
11783 lpfc_stop_hba_timers(phba);
11784 spin_lock_irq(&phba->hbalock);
11785 list_del_init(&vport->listentry);
11786 spin_unlock_irq(&phba->hbalock);
11787
11788 /* Perform scsi free before driver resource_unset since scsi
11789 * buffers are released to their corresponding pools here.
11790 */
11791 lpfc_scsi_free(phba);
11792 lpfc_nvme_free(phba);
11793 lpfc_free_iocb_list(phba);
11794
11795 lpfc_unset_driver_resource_phase2(phba);
11796 lpfc_sli4_driver_resource_unset(phba);
11797
11798 /* Unmap adapter Control and Doorbell registers */
11799 lpfc_sli4_pci_mem_unset(phba);
11800
11801 /* Release PCI resources and disable device's PCI function */
11802 scsi_host_put(shost);
11803 lpfc_disable_pci_dev(phba);
11804
11805 /* Finally, free the driver's device data structure */
11806 lpfc_hba_free(phba);
11807
11808 return;
11809}
11810
11811/**
11812 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11813 * @pdev: pointer to PCI device
11814 * @msg: power management message
11815 *
11816 * This routine is called from the kernel's PCI subsystem to support system
11817 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11818 * this method, it quiesces the device by stopping the driver's worker
11819 * thread for the device, turning off device's interrupt and DMA, and bring
11820 * the device offline. Note that as the driver implements the minimum PM
11821 * requirements to a power-aware driver's PM support for suspend/resume -- all
11822 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11823 * method call will be treated as SUSPEND and the driver will fully
11824 * reinitialize its device during resume() method call, the driver will set
11825 * device to PCI_D3hot state in PCI config space instead of setting it
11826 * according to the @msg provided by the PM.
11827 *
11828 * Return code
11829 * 0 - driver suspended the device
11830 * Error otherwise
11831 **/
11832static int
11833lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11834{
11835 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11836 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11837
11838 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11839 "2843 PCI device Power Management suspend.\n");
11840
11841 /* Bring down the device */
11842 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11843 lpfc_offline(phba);
11844 kthread_stop(phba->worker_thread);
11845
11846 /* Disable interrupt from device */
11847 lpfc_sli4_disable_intr(phba);
11848 lpfc_sli4_queue_destroy(phba);
11849
11850 /* Save device state to PCI config space */
11851 pci_save_state(pdev);
11852 pci_set_power_state(pdev, PCI_D3hot);
11853
11854 return 0;
11855}
11856
11857/**
11858 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11859 * @pdev: pointer to PCI device
11860 *
11861 * This routine is called from the kernel's PCI subsystem to support system
11862 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11863 * this method, it restores the device's PCI config space state and fully
11864 * reinitializes the device and brings it online. Note that as the driver
11865 * implements the minimum PM requirements to a power-aware driver's PM for
11866 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11867 * to the suspend() method call will be treated as SUSPEND and the driver
11868 * will fully reinitialize its device during resume() method call, the device
11869 * will be set to PCI_D0 directly in PCI config space before restoring the
11870 * state.
11871 *
11872 * Return code
11873 * 0 - driver suspended the device
11874 * Error otherwise
11875 **/
11876static int
11877lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11878{
11879 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11880 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11881 uint32_t intr_mode;
11882 int error;
11883
11884 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11885 "0292 PCI device Power Management resume.\n");
11886
11887 /* Restore device state from PCI config space */
11888 pci_set_power_state(pdev, PCI_D0);
11889 pci_restore_state(pdev);
11890
11891 /*
11892 * As the new kernel behavior of pci_restore_state() API call clears
11893 * device saved_state flag, need to save the restored state again.
11894 */
11895 pci_save_state(pdev);
11896
11897 if (pdev->is_busmaster)
11898 pci_set_master(pdev);
11899
11900 /* Startup the kernel thread for this host adapter. */
11901 phba->worker_thread = kthread_run(lpfc_do_work, phba,
11902 "lpfc_worker_%d", phba->brd_no);
11903 if (IS_ERR(phba->worker_thread)) {
11904 error = PTR_ERR(phba->worker_thread);
11905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11906 "0293 PM resume failed to start worker "
11907 "thread: error=x%x.\n", error);
11908 return error;
11909 }
11910
11911 /* Configure and enable interrupt */
11912 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11913 if (intr_mode == LPFC_INTR_ERROR) {
11914 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11915 "0294 PM resume Failed to enable interrupt\n");
11916 return -EIO;
11917 } else
11918 phba->intr_mode = intr_mode;
11919
11920 /* Restart HBA and bring it online */
11921 lpfc_sli_brdrestart(phba);
11922 lpfc_online(phba);
11923
11924 /* Log the current active interrupt mode */
11925 lpfc_log_intr_mode(phba, phba->intr_mode);
11926
11927 return 0;
11928}
11929
11930/**
11931 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11932 * @phba: pointer to lpfc hba data structure.
11933 *
11934 * This routine is called to prepare the SLI4 device for PCI slot recover. It
11935 * aborts all the outstanding SCSI I/Os to the pci device.
11936 **/
11937static void
11938lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11939{
11940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11941 "2828 PCI channel I/O abort preparing for recovery\n");
11942 /*
11943 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11944 * and let the SCSI mid-layer to retry them to recover.
11945 */
11946 lpfc_sli_abort_fcp_rings(phba);
11947}
11948
11949/**
11950 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11951 * @phba: pointer to lpfc hba data structure.
11952 *
11953 * This routine is called to prepare the SLI4 device for PCI slot reset. It
11954 * disables the device interrupt and pci device, and aborts the internal FCP
11955 * pending I/Os.
11956 **/
11957static void
11958lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11959{
11960 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11961 "2826 PCI channel disable preparing for reset\n");
11962
11963 /* Block any management I/Os to the device */
11964 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
11965
11966 /* Block all SCSI devices' I/Os on the host */
11967 lpfc_scsi_dev_block(phba);
11968
11969 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
11970 lpfc_sli_flush_fcp_rings(phba);
11971
11972 /* Flush the outstanding NVME IOs if fc4 type enabled. */
11973 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11974 lpfc_sli_flush_nvme_rings(phba);
11975
11976 /* stop all timers */
11977 lpfc_stop_hba_timers(phba);
11978
11979 /* Disable interrupt and pci device */
11980 lpfc_sli4_disable_intr(phba);
11981 lpfc_sli4_queue_destroy(phba);
11982 pci_disable_device(phba->pcidev);
11983}
11984
11985/**
11986 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11987 * @phba: pointer to lpfc hba data structure.
11988 *
11989 * This routine is called to prepare the SLI4 device for PCI slot permanently
11990 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11991 * pending I/Os.
11992 **/
11993static void
11994lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11995{
11996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11997 "2827 PCI channel permanent disable for failure\n");
11998
11999 /* Block all SCSI devices' I/Os on the host */
12000 lpfc_scsi_dev_block(phba);
12001
12002 /* stop all timers */
12003 lpfc_stop_hba_timers(phba);
12004
12005 /* Clean up all driver's outstanding SCSI I/Os */
12006 lpfc_sli_flush_fcp_rings(phba);
12007
12008 /* Flush the outstanding NVME IOs if fc4 type enabled. */
12009 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
12010 lpfc_sli_flush_nvme_rings(phba);
12011}
12012
12013/**
12014 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
12015 * @pdev: pointer to PCI device.
12016 * @state: the current PCI connection state.
12017 *
12018 * This routine is called from the PCI subsystem for error handling to device
12019 * with SLI-4 interface spec. This function is called by the PCI subsystem
12020 * after a PCI bus error affecting this device has been detected. When this
12021 * function is invoked, it will need to stop all the I/Os and interrupt(s)
12022 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
12023 * for the PCI subsystem to perform proper recovery as desired.
12024 *
12025 * Return codes
12026 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12027 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12028 **/
12029static pci_ers_result_t
12030lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
12031{
12032 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12033 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12034
12035 switch (state) {
12036 case pci_channel_io_normal:
12037 /* Non-fatal error, prepare for recovery */
12038 lpfc_sli4_prep_dev_for_recover(phba);
12039 return PCI_ERS_RESULT_CAN_RECOVER;
12040 case pci_channel_io_frozen:
12041 /* Fatal error, prepare for slot reset */
12042 lpfc_sli4_prep_dev_for_reset(phba);
12043 return PCI_ERS_RESULT_NEED_RESET;
12044 case pci_channel_io_perm_failure:
12045 /* Permanent failure, prepare for device down */
12046 lpfc_sli4_prep_dev_for_perm_failure(phba);
12047 return PCI_ERS_RESULT_DISCONNECT;
12048 default:
12049 /* Unknown state, prepare and request slot reset */
12050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12051 "2825 Unknown PCI error state: x%x\n", state);
12052 lpfc_sli4_prep_dev_for_reset(phba);
12053 return PCI_ERS_RESULT_NEED_RESET;
12054 }
12055}
12056
12057/**
12058 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
12059 * @pdev: pointer to PCI device.
12060 *
12061 * This routine is called from the PCI subsystem for error handling to device
12062 * with SLI-4 interface spec. It is called after PCI bus has been reset to
12063 * restart the PCI card from scratch, as if from a cold-boot. During the
12064 * PCI subsystem error recovery, after the driver returns
12065 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
12066 * recovery and then call this routine before calling the .resume method to
12067 * recover the device. This function will initialize the HBA device, enable
12068 * the interrupt, but it will just put the HBA to offline state without
12069 * passing any I/O traffic.
12070 *
12071 * Return codes
12072 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12073 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12074 */
12075static pci_ers_result_t
12076lpfc_io_slot_reset_s4(struct pci_dev *pdev)
12077{
12078 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12079 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12080 struct lpfc_sli *psli = &phba->sli;
12081 uint32_t intr_mode;
12082
12083 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
12084 if (pci_enable_device_mem(pdev)) {
12085 printk(KERN_ERR "lpfc: Cannot re-enable "
12086 "PCI device after reset.\n");
12087 return PCI_ERS_RESULT_DISCONNECT;
12088 }
12089
12090 pci_restore_state(pdev);
12091
12092 /*
12093 * As the new kernel behavior of pci_restore_state() API call clears
12094 * device saved_state flag, need to save the restored state again.
12095 */
12096 pci_save_state(pdev);
12097
12098 if (pdev->is_busmaster)
12099 pci_set_master(pdev);
12100
12101 spin_lock_irq(&phba->hbalock);
12102 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
12103 spin_unlock_irq(&phba->hbalock);
12104
12105 /* Configure and enable interrupt */
12106 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
12107 if (intr_mode == LPFC_INTR_ERROR) {
12108 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12109 "2824 Cannot re-enable interrupt after "
12110 "slot reset.\n");
12111 return PCI_ERS_RESULT_DISCONNECT;
12112 } else
12113 phba->intr_mode = intr_mode;
12114
12115 /* Log the current active interrupt mode */
12116 lpfc_log_intr_mode(phba, phba->intr_mode);
12117
12118 return PCI_ERS_RESULT_RECOVERED;
12119}
12120
12121/**
12122 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
12123 * @pdev: pointer to PCI device
12124 *
12125 * This routine is called from the PCI subsystem for error handling to device
12126 * with SLI-4 interface spec. It is called when kernel error recovery tells
12127 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12128 * error recovery. After this call, traffic can start to flow from this device
12129 * again.
12130 **/
12131static void
12132lpfc_io_resume_s4(struct pci_dev *pdev)
12133{
12134 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12135 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12136
12137 /*
12138 * In case of slot reset, as function reset is performed through
12139 * mailbox command which needs DMA to be enabled, this operation
12140 * has to be moved to the io resume phase. Taking device offline
12141 * will perform the necessary cleanup.
12142 */
12143 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
12144 /* Perform device reset */
12145 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
12146 lpfc_offline(phba);
12147 lpfc_sli_brdrestart(phba);
12148 /* Bring the device back online */
12149 lpfc_online(phba);
12150 }
12151
12152 /* Clean up Advanced Error Reporting (AER) if needed */
12153 if (phba->hba_flag & HBA_AER_ENABLED)
12154 pci_cleanup_aer_uncorrect_error_status(pdev);
12155}
12156
12157/**
12158 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
12159 * @pdev: pointer to PCI device
12160 * @pid: pointer to PCI device identifier
12161 *
12162 * This routine is to be registered to the kernel's PCI subsystem. When an
12163 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
12164 * at PCI device-specific information of the device and driver to see if the
12165 * driver state that it can support this kind of device. If the match is
12166 * successful, the driver core invokes this routine. This routine dispatches
12167 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
12168 * do all the initialization that it needs to do to handle the HBA device
12169 * properly.
12170 *
12171 * Return code
12172 * 0 - driver can claim the device
12173 * negative value - driver can not claim the device
12174 **/
12175static int
12176lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
12177{
12178 int rc;
12179 struct lpfc_sli_intf intf;
12180
12181 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
12182 return -ENODEV;
12183
12184 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
12185 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
12186 rc = lpfc_pci_probe_one_s4(pdev, pid);
12187 else
12188 rc = lpfc_pci_probe_one_s3(pdev, pid);
12189
12190 return rc;
12191}
12192
12193/**
12194 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
12195 * @pdev: pointer to PCI device
12196 *
12197 * This routine is to be registered to the kernel's PCI subsystem. When an
12198 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
12199 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
12200 * remove routine, which will perform all the necessary cleanup for the
12201 * device to be removed from the PCI subsystem properly.
12202 **/
12203static void
12204lpfc_pci_remove_one(struct pci_dev *pdev)
12205{
12206 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12207 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12208
12209 switch (phba->pci_dev_grp) {
12210 case LPFC_PCI_DEV_LP:
12211 lpfc_pci_remove_one_s3(pdev);
12212 break;
12213 case LPFC_PCI_DEV_OC:
12214 lpfc_pci_remove_one_s4(pdev);
12215 break;
12216 default:
12217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12218 "1424 Invalid PCI device group: 0x%x\n",
12219 phba->pci_dev_grp);
12220 break;
12221 }
12222 return;
12223}
12224
12225/**
12226 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
12227 * @pdev: pointer to PCI device
12228 * @msg: power management message
12229 *
12230 * This routine is to be registered to the kernel's PCI subsystem to support
12231 * system Power Management (PM). When PM invokes this method, it dispatches
12232 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
12233 * suspend the device.
12234 *
12235 * Return code
12236 * 0 - driver suspended the device
12237 * Error otherwise
12238 **/
12239static int
12240lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
12241{
12242 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12243 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12244 int rc = -ENODEV;
12245
12246 switch (phba->pci_dev_grp) {
12247 case LPFC_PCI_DEV_LP:
12248 rc = lpfc_pci_suspend_one_s3(pdev, msg);
12249 break;
12250 case LPFC_PCI_DEV_OC:
12251 rc = lpfc_pci_suspend_one_s4(pdev, msg);
12252 break;
12253 default:
12254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12255 "1425 Invalid PCI device group: 0x%x\n",
12256 phba->pci_dev_grp);
12257 break;
12258 }
12259 return rc;
12260}
12261
12262/**
12263 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
12264 * @pdev: pointer to PCI device
12265 *
12266 * This routine is to be registered to the kernel's PCI subsystem to support
12267 * system Power Management (PM). When PM invokes this method, it dispatches
12268 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
12269 * resume the device.
12270 *
12271 * Return code
12272 * 0 - driver suspended the device
12273 * Error otherwise
12274 **/
12275static int
12276lpfc_pci_resume_one(struct pci_dev *pdev)
12277{
12278 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12279 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12280 int rc = -ENODEV;
12281
12282 switch (phba->pci_dev_grp) {
12283 case LPFC_PCI_DEV_LP:
12284 rc = lpfc_pci_resume_one_s3(pdev);
12285 break;
12286 case LPFC_PCI_DEV_OC:
12287 rc = lpfc_pci_resume_one_s4(pdev);
12288 break;
12289 default:
12290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12291 "1426 Invalid PCI device group: 0x%x\n",
12292 phba->pci_dev_grp);
12293 break;
12294 }
12295 return rc;
12296}
12297
12298/**
12299 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
12300 * @pdev: pointer to PCI device.
12301 * @state: the current PCI connection state.
12302 *
12303 * This routine is registered to the PCI subsystem for error handling. This
12304 * function is called by the PCI subsystem after a PCI bus error affecting
12305 * this device has been detected. When this routine is invoked, it dispatches
12306 * the action to the proper SLI-3 or SLI-4 device error detected handling
12307 * routine, which will perform the proper error detected operation.
12308 *
12309 * Return codes
12310 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12311 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12312 **/
12313static pci_ers_result_t
12314lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
12315{
12316 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12317 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12318 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12319
12320 switch (phba->pci_dev_grp) {
12321 case LPFC_PCI_DEV_LP:
12322 rc = lpfc_io_error_detected_s3(pdev, state);
12323 break;
12324 case LPFC_PCI_DEV_OC:
12325 rc = lpfc_io_error_detected_s4(pdev, state);
12326 break;
12327 default:
12328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12329 "1427 Invalid PCI device group: 0x%x\n",
12330 phba->pci_dev_grp);
12331 break;
12332 }
12333 return rc;
12334}
12335
12336/**
12337 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
12338 * @pdev: pointer to PCI device.
12339 *
12340 * This routine is registered to the PCI subsystem for error handling. This
12341 * function is called after PCI bus has been reset to restart the PCI card
12342 * from scratch, as if from a cold-boot. When this routine is invoked, it
12343 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
12344 * routine, which will perform the proper device reset.
12345 *
12346 * Return codes
12347 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12348 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12349 **/
12350static pci_ers_result_t
12351lpfc_io_slot_reset(struct pci_dev *pdev)
12352{
12353 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12354 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12355 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12356
12357 switch (phba->pci_dev_grp) {
12358 case LPFC_PCI_DEV_LP:
12359 rc = lpfc_io_slot_reset_s3(pdev);
12360 break;
12361 case LPFC_PCI_DEV_OC:
12362 rc = lpfc_io_slot_reset_s4(pdev);
12363 break;
12364 default:
12365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12366 "1428 Invalid PCI device group: 0x%x\n",
12367 phba->pci_dev_grp);
12368 break;
12369 }
12370 return rc;
12371}
12372
12373/**
12374 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
12375 * @pdev: pointer to PCI device
12376 *
12377 * This routine is registered to the PCI subsystem for error handling. It
12378 * is called when kernel error recovery tells the lpfc driver that it is
12379 * OK to resume normal PCI operation after PCI bus error recovery. When
12380 * this routine is invoked, it dispatches the action to the proper SLI-3
12381 * or SLI-4 device io_resume routine, which will resume the device operation.
12382 **/
12383static void
12384lpfc_io_resume(struct pci_dev *pdev)
12385{
12386 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12387 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12388
12389 switch (phba->pci_dev_grp) {
12390 case LPFC_PCI_DEV_LP:
12391 lpfc_io_resume_s3(pdev);
12392 break;
12393 case LPFC_PCI_DEV_OC:
12394 lpfc_io_resume_s4(pdev);
12395 break;
12396 default:
12397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12398 "1429 Invalid PCI device group: 0x%x\n",
12399 phba->pci_dev_grp);
12400 break;
12401 }
12402 return;
12403}
12404
12405/**
12406 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
12407 * @phba: pointer to lpfc hba data structure.
12408 *
12409 * This routine checks to see if OAS is supported for this adapter. If
12410 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
12411 * the enable oas flag is cleared and the pool created for OAS device data
12412 * is destroyed.
12413 *
12414 **/
12415void
12416lpfc_sli4_oas_verify(struct lpfc_hba *phba)
12417{
12418
12419 if (!phba->cfg_EnableXLane)
12420 return;
12421
12422 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
12423 phba->cfg_fof = 1;
12424 } else {
12425 phba->cfg_fof = 0;
12426 if (phba->device_data_mem_pool)
12427 mempool_destroy(phba->device_data_mem_pool);
12428 phba->device_data_mem_pool = NULL;
12429 }
12430
12431 return;
12432}
12433
12434/**
12435 * lpfc_fof_queue_setup - Set up all the fof queues
12436 * @phba: pointer to lpfc hba data structure.
12437 *
12438 * This routine is invoked to set up all the fof queues for the FC HBA
12439 * operation.
12440 *
12441 * Return codes
12442 * 0 - successful
12443 * -ENOMEM - No available memory
12444 **/
12445int
12446lpfc_fof_queue_setup(struct lpfc_hba *phba)
12447{
12448 struct lpfc_sli_ring *pring;
12449 int rc;
12450
12451 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
12452 if (rc)
12453 return -ENOMEM;
12454
12455 if (phba->cfg_fof) {
12456
12457 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
12458 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
12459 if (rc)
12460 goto out_oas_cq;
12461
12462 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
12463 phba->sli4_hba.oas_cq, LPFC_FCP);
12464 if (rc)
12465 goto out_oas_wq;
12466
12467 /* Bind this CQ/WQ to the NVME ring */
12468 pring = phba->sli4_hba.oas_wq->pring;
12469 pring->sli.sli4.wqp =
12470 (void *)phba->sli4_hba.oas_wq;
12471 phba->sli4_hba.oas_cq->pring = pring;
12472 }
12473
12474 return 0;
12475
12476out_oas_wq:
12477 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
12478out_oas_cq:
12479 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
12480 return rc;
12481
12482}
12483
12484/**
12485 * lpfc_fof_queue_create - Create all the fof queues
12486 * @phba: pointer to lpfc hba data structure.
12487 *
12488 * This routine is invoked to allocate all the fof queues for the FC HBA
12489 * operation. For each SLI4 queue type, the parameters such as queue entry
12490 * count (queue depth) shall be taken from the module parameter. For now,
12491 * we just use some constant number as place holder.
12492 *
12493 * Return codes
12494 * 0 - successful
12495 * -ENOMEM - No availble memory
12496 * -EIO - The mailbox failed to complete successfully.
12497 **/
12498int
12499lpfc_fof_queue_create(struct lpfc_hba *phba)
12500{
12501 struct lpfc_queue *qdesc;
12502 uint32_t wqesize;
12503
12504 /* Create FOF EQ */
12505 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
12506 phba->sli4_hba.eq_esize,
12507 phba->sli4_hba.eq_ecount);
12508 if (!qdesc)
12509 goto out_error;
12510
12511 qdesc->qe_valid = 1;
12512 phba->sli4_hba.fof_eq = qdesc;
12513
12514 if (phba->cfg_fof) {
12515
12516 /* Create OAS CQ */
12517 if (phba->enab_exp_wqcq_pages)
12518 qdesc = lpfc_sli4_queue_alloc(phba,
12519 LPFC_EXPANDED_PAGE_SIZE,
12520 phba->sli4_hba.cq_esize,
12521 LPFC_CQE_EXP_COUNT);
12522 else
12523 qdesc = lpfc_sli4_queue_alloc(phba,
12524 LPFC_DEFAULT_PAGE_SIZE,
12525 phba->sli4_hba.cq_esize,
12526 phba->sli4_hba.cq_ecount);
12527 if (!qdesc)
12528 goto out_error;
12529
12530 qdesc->qe_valid = 1;
12531 phba->sli4_hba.oas_cq = qdesc;
12532
12533 /* Create OAS WQ */
12534 if (phba->enab_exp_wqcq_pages) {
12535 wqesize = (phba->fcp_embed_io) ?
12536 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
12537 qdesc = lpfc_sli4_queue_alloc(phba,
12538 LPFC_EXPANDED_PAGE_SIZE,
12539 wqesize,
12540 LPFC_WQE_EXP_COUNT);
12541 } else
12542 qdesc = lpfc_sli4_queue_alloc(phba,
12543 LPFC_DEFAULT_PAGE_SIZE,
12544 phba->sli4_hba.wq_esize,
12545 phba->sli4_hba.wq_ecount);
12546
12547 if (!qdesc)
12548 goto out_error;
12549
12550 phba->sli4_hba.oas_wq = qdesc;
12551 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
12552
12553 }
12554 return 0;
12555
12556out_error:
12557 lpfc_fof_queue_destroy(phba);
12558 return -ENOMEM;
12559}
12560
12561/**
12562 * lpfc_fof_queue_destroy - Destroy all the fof queues
12563 * @phba: pointer to lpfc hba data structure.
12564 *
12565 * This routine is invoked to release all the SLI4 queues with the FC HBA
12566 * operation.
12567 *
12568 * Return codes
12569 * 0 - successful
12570 **/
12571int
12572lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12573{
12574 /* Release FOF Event queue */
12575 if (phba->sli4_hba.fof_eq != NULL) {
12576 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12577 phba->sli4_hba.fof_eq = NULL;
12578 }
12579
12580 /* Release OAS Completion queue */
12581 if (phba->sli4_hba.oas_cq != NULL) {
12582 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12583 phba->sli4_hba.oas_cq = NULL;
12584 }
12585
12586 /* Release OAS Work queue */
12587 if (phba->sli4_hba.oas_wq != NULL) {
12588 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12589 phba->sli4_hba.oas_wq = NULL;
12590 }
12591 return 0;
12592}
12593
12594MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12595
12596static const struct pci_error_handlers lpfc_err_handler = {
12597 .error_detected = lpfc_io_error_detected,
12598 .slot_reset = lpfc_io_slot_reset,
12599 .resume = lpfc_io_resume,
12600};
12601
12602static struct pci_driver lpfc_driver = {
12603 .name = LPFC_DRIVER_NAME,
12604 .id_table = lpfc_id_table,
12605 .probe = lpfc_pci_probe_one,
12606 .remove = lpfc_pci_remove_one,
12607 .shutdown = lpfc_pci_remove_one,
12608 .suspend = lpfc_pci_suspend_one,
12609 .resume = lpfc_pci_resume_one,
12610 .err_handler = &lpfc_err_handler,
12611};
12612
12613static const struct file_operations lpfc_mgmt_fop = {
12614 .owner = THIS_MODULE,
12615};
12616
12617static struct miscdevice lpfc_mgmt_dev = {
12618 .minor = MISC_DYNAMIC_MINOR,
12619 .name = "lpfcmgmt",
12620 .fops = &lpfc_mgmt_fop,
12621};
12622
12623/**
12624 * lpfc_init - lpfc module initialization routine
12625 *
12626 * This routine is to be invoked when the lpfc module is loaded into the
12627 * kernel. The special kernel macro module_init() is used to indicate the
12628 * role of this routine to the kernel as lpfc module entry point.
12629 *
12630 * Return codes
12631 * 0 - successful
12632 * -ENOMEM - FC attach transport failed
12633 * all others - failed
12634 */
12635static int __init
12636lpfc_init(void)
12637{
12638 int error = 0;
12639
12640 printk(LPFC_MODULE_DESC "\n");
12641 printk(LPFC_COPYRIGHT "\n");
12642
12643 error = misc_register(&lpfc_mgmt_dev);
12644 if (error)
12645 printk(KERN_ERR "Could not register lpfcmgmt device, "
12646 "misc_register returned with status %d", error);
12647
12648 lpfc_transport_functions.vport_create = lpfc_vport_create;
12649 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
12650 lpfc_transport_template =
12651 fc_attach_transport(&lpfc_transport_functions);
12652 if (lpfc_transport_template == NULL)
12653 return -ENOMEM;
12654 lpfc_vport_transport_template =
12655 fc_attach_transport(&lpfc_vport_transport_functions);
12656 if (lpfc_vport_transport_template == NULL) {
12657 fc_release_transport(lpfc_transport_template);
12658 return -ENOMEM;
12659 }
12660 lpfc_nvme_cmd_template();
12661 lpfc_nvmet_cmd_template();
12662
12663 /* Initialize in case vector mapping is needed */
12664 lpfc_used_cpu = NULL;
12665 lpfc_present_cpu = num_present_cpus();
12666
12667 error = pci_register_driver(&lpfc_driver);
12668 if (error) {
12669 fc_release_transport(lpfc_transport_template);
12670 fc_release_transport(lpfc_vport_transport_template);
12671 }
12672
12673 return error;
12674}
12675
12676/**
12677 * lpfc_exit - lpfc module removal routine
12678 *
12679 * This routine is invoked when the lpfc module is removed from the kernel.
12680 * The special kernel macro module_exit() is used to indicate the role of
12681 * this routine to the kernel as lpfc module exit point.
12682 */
12683static void __exit
12684lpfc_exit(void)
12685{
12686 misc_deregister(&lpfc_mgmt_dev);
12687 pci_unregister_driver(&lpfc_driver);
12688 fc_release_transport(lpfc_transport_template);
12689 fc_release_transport(lpfc_vport_transport_template);
12690 if (_dump_buf_data) {
12691 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
12692 "_dump_buf_data at 0x%p\n",
12693 (1L << _dump_buf_data_order), _dump_buf_data);
12694 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12695 }
12696
12697 if (_dump_buf_dif) {
12698 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
12699 "_dump_buf_dif at 0x%p\n",
12700 (1L << _dump_buf_dif_order), _dump_buf_dif);
12701 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12702 }
12703 kfree(lpfc_used_cpu);
12704 idr_destroy(&lpfc_hba_index);
12705}
12706
12707module_init(lpfc_init);
12708module_exit(lpfc_exit);
12709MODULE_LICENSE("GPL");
12710MODULE_DESCRIPTION(LPFC_MODULE_DESC);
12711MODULE_AUTHOR("Broadcom");
12712MODULE_VERSION("0:" LPFC_DRIVER_VERSION);