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