xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_mbox.c (revision e2a0a9d6)
1dea3101eS /*******************************************************************
2dea3101eS  * This file is part of the Emulex Linux Device Driver for         *
3c44ce173SJames.Smart@Emulex.Com  * Fibre Channel Host Bus Adapters.                                *
49399627fSJames Smart  * Copyright (C) 2004-2008 Emulex.  All rights reserved.           *
5c44ce173SJames.Smart@Emulex.Com  * EMULEX and SLI are trademarks of Emulex.                        *
6dea3101eS  * www.emulex.com                                                  *
7c44ce173SJames.Smart@Emulex.Com  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8dea3101eS  *                                                                 *
9dea3101eS  * This program is free software; you can redistribute it and/or   *
10c44ce173SJames.Smart@Emulex.Com  * modify it under the terms of version 2 of the GNU General       *
11c44ce173SJames.Smart@Emulex.Com  * Public License as published by the Free Software Foundation.    *
12c44ce173SJames.Smart@Emulex.Com  * This program is distributed in the hope that it will be useful. *
13c44ce173SJames.Smart@Emulex.Com  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14c44ce173SJames.Smart@Emulex.Com  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15c44ce173SJames.Smart@Emulex.Com  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16c44ce173SJames.Smart@Emulex.Com  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17c44ce173SJames.Smart@Emulex.Com  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18c44ce173SJames.Smart@Emulex.Com  * more details, a copy of which can be found in the file COPYING  *
19c44ce173SJames.Smart@Emulex.Com  * included with this package.                                     *
20dea3101eS  *******************************************************************/
21dea3101eS 
22dea3101eS #include <linux/blkdev.h>
23dea3101eS #include <linux/pci.h>
24dea3101eS #include <linux/interrupt.h>
25dea3101eS 
26f888ba3cSJames.Smart@Emulex.Com #include <scsi/scsi_device.h>
27f888ba3cSJames.Smart@Emulex.Com #include <scsi/scsi_transport_fc.h>
28f888ba3cSJames.Smart@Emulex.Com 
2991886523SJames.Smart@Emulex.Com #include <scsi/scsi.h>
3091886523SJames.Smart@Emulex.Com 
31dea3101eS #include "lpfc_hw.h"
32dea3101eS #include "lpfc_sli.h"
33ea2151b4SJames Smart #include "lpfc_nl.h"
34dea3101eS #include "lpfc_disc.h"
35dea3101eS #include "lpfc_scsi.h"
36dea3101eS #include "lpfc.h"
37dea3101eS #include "lpfc_logmsg.h"
38dea3101eS #include "lpfc_crtn.h"
39dea3101eS #include "lpfc_compat.h"
40dea3101eS 
41e59058c4SJames Smart /**
42e59058c4SJames Smart  * lpfc_dump_mem: Prepare a mailbox command for retrieving HBA's VPD memory.
43e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
44e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
45e59058c4SJames Smart  * @offset: offset for dumping VPD memory mailbox command.
46e59058c4SJames Smart  *
47e59058c4SJames Smart  * The dump mailbox command provides a method for the device driver to obtain
48e59058c4SJames Smart  * various types of information from the HBA device.
49e59058c4SJames Smart  *
50e59058c4SJames Smart  * This routine prepares the mailbox command for dumping HBA Vital Product
51e59058c4SJames Smart  * Data (VPD) memory. This mailbox command is to be used for retrieving a
52e59058c4SJames Smart  * portion (DMP_RSP_SIZE bytes) of a HBA's VPD from the HBA at an address
53e59058c4SJames Smart  * offset specified by the offset parameter.
54e59058c4SJames Smart  **/
55dea3101eS void
56dea3101eS lpfc_dump_mem(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb, uint16_t offset)
57dea3101eS {
58dea3101eS 	MAILBOX_t *mb;
59dea3101eS 	void *ctx;
60dea3101eS 
61dea3101eS 	mb = &pmb->mb;
62dea3101eS 	ctx = pmb->context2;
63dea3101eS 
64dea3101eS 	/* Setup to dump VPD region */
65dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
66dea3101eS 	mb->mbxCommand = MBX_DUMP_MEMORY;
67dea3101eS 	mb->un.varDmp.cv = 1;
68dea3101eS 	mb->un.varDmp.type = DMP_NV_PARAMS;
69dea3101eS 	mb->un.varDmp.entry_index = offset;
70dea3101eS 	mb->un.varDmp.region_id = DMP_REGION_VPD;
71dea3101eS 	mb->un.varDmp.word_cnt = (DMP_RSP_SIZE / sizeof (uint32_t));
72dea3101eS 	mb->un.varDmp.co = 0;
73dea3101eS 	mb->un.varDmp.resp_offset = 0;
74dea3101eS 	pmb->context2 = ctx;
75dea3101eS 	mb->mbxOwner = OWN_HOST;
76dea3101eS 	return;
77dea3101eS }
78dea3101eS 
79e59058c4SJames Smart /**
8097207482SJames Smart  * lpfc_dump_mem: Prepare a mailbox command for retrieving wakeup params.
8197207482SJames Smart  * @phba: pointer to lpfc hba data structure.
8297207482SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
8397207482SJames Smart  * This function create a dump memory mailbox command to dump wake up
8497207482SJames Smart  * parameters.
8597207482SJames Smart  */
8697207482SJames Smart void
8797207482SJames Smart lpfc_dump_wakeup_param(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
8897207482SJames Smart {
8997207482SJames Smart 	MAILBOX_t *mb;
9097207482SJames Smart 	void *ctx;
9197207482SJames Smart 
9297207482SJames Smart 	mb = &pmb->mb;
9397207482SJames Smart 	/* Save context so that we can restore after memset */
9497207482SJames Smart 	ctx = pmb->context2;
9597207482SJames Smart 
9697207482SJames Smart 	/* Setup to dump VPD region */
9797207482SJames Smart 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
9897207482SJames Smart 	mb->mbxCommand = MBX_DUMP_MEMORY;
9997207482SJames Smart 	mb->mbxOwner = OWN_HOST;
10097207482SJames Smart 	mb->un.varDmp.cv = 1;
10197207482SJames Smart 	mb->un.varDmp.type = DMP_NV_PARAMS;
10297207482SJames Smart 	mb->un.varDmp.entry_index = 0;
10397207482SJames Smart 	mb->un.varDmp.region_id = WAKE_UP_PARMS_REGION_ID;
10497207482SJames Smart 	mb->un.varDmp.word_cnt = WAKE_UP_PARMS_WORD_SIZE;
10597207482SJames Smart 	mb->un.varDmp.co = 0;
10697207482SJames Smart 	mb->un.varDmp.resp_offset = 0;
10797207482SJames Smart 	pmb->context2 = ctx;
10897207482SJames Smart 	return;
10997207482SJames Smart }
11097207482SJames Smart 
11197207482SJames Smart /**
112e59058c4SJames Smart  * lpfc_read_nv: Prepare a mailbox command for reading HBA's NVRAM param.
113e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
114e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
115e59058c4SJames Smart  *
116e59058c4SJames Smart  * The read NVRAM mailbox command returns the HBA's non-volatile parameters
117e59058c4SJames Smart  * that are used as defaults when the Fibre Channel link is brought on-line.
118e59058c4SJames Smart  *
119e59058c4SJames Smart  * This routine prepares the mailbox command for reading information stored
120e59058c4SJames Smart  * in the HBA's NVRAM. Specifically, the HBA's WWNN and WWPN.
121e59058c4SJames Smart  **/
122dea3101eS void
123dea3101eS lpfc_read_nv(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
124dea3101eS {
125dea3101eS 	MAILBOX_t *mb;
126dea3101eS 
127dea3101eS 	mb = &pmb->mb;
128dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
129dea3101eS 	mb->mbxCommand = MBX_READ_NV;
130dea3101eS 	mb->mbxOwner = OWN_HOST;
131dea3101eS 	return;
132dea3101eS }
133dea3101eS 
134e59058c4SJames Smart /**
135e59058c4SJames Smart  * lpfc_config_async: Prepare a mailbox command for enabling HBA async event.
136e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
137e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
138e59058c4SJames Smart  * @ring: ring number for the asynchronous event to be configured.
139e59058c4SJames Smart  *
140e59058c4SJames Smart  * The asynchronous event enable mailbox command is used to enable the
141e59058c4SJames Smart  * asynchronous event posting via the ASYNC_STATUS_CN IOCB response and
142e59058c4SJames Smart  * specifies the default ring to which events are posted.
143e59058c4SJames Smart  *
144e59058c4SJames Smart  * This routine prepares the mailbox command for enabling HBA asynchronous
145e59058c4SJames Smart  * event support on a IOCB ring.
146e59058c4SJames Smart  **/
14757127f15SJames Smart void
14857127f15SJames Smart lpfc_config_async(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb,
14957127f15SJames Smart 		uint32_t ring)
15057127f15SJames Smart {
15157127f15SJames Smart 	MAILBOX_t *mb;
15257127f15SJames Smart 
15357127f15SJames Smart 	mb = &pmb->mb;
15457127f15SJames Smart 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
15557127f15SJames Smart 	mb->mbxCommand = MBX_ASYNCEVT_ENABLE;
15657127f15SJames Smart 	mb->un.varCfgAsyncEvent.ring = ring;
15757127f15SJames Smart 	mb->mbxOwner = OWN_HOST;
15857127f15SJames Smart 	return;
15957127f15SJames Smart }
16057127f15SJames Smart 
161e59058c4SJames Smart /**
162e59058c4SJames Smart  * lpfc_heart_beat: Prepare a mailbox command for heart beat.
163e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
164e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
165e59058c4SJames Smart  *
166e59058c4SJames Smart  * The heart beat mailbox command is used to detect an unresponsive HBA, which
167e59058c4SJames Smart  * is defined as any device where no error attention is sent and both mailbox
168e59058c4SJames Smart  * and rings are not processed.
169e59058c4SJames Smart  *
170e59058c4SJames Smart  * This routine prepares the mailbox command for issuing a heart beat in the
171e59058c4SJames Smart  * form of mailbox command to the HBA. The timely completion of the heart
172e59058c4SJames Smart  * beat mailbox command indicates the health of the HBA.
173e59058c4SJames Smart  **/
174858c9f6cSJames Smart void
175858c9f6cSJames Smart lpfc_heart_beat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
176858c9f6cSJames Smart {
177858c9f6cSJames Smart 	MAILBOX_t *mb;
178858c9f6cSJames Smart 
179858c9f6cSJames Smart 	mb = &pmb->mb;
180858c9f6cSJames Smart 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
181858c9f6cSJames Smart 	mb->mbxCommand = MBX_HEARTBEAT;
182858c9f6cSJames Smart 	mb->mbxOwner = OWN_HOST;
183858c9f6cSJames Smart 	return;
184858c9f6cSJames Smart }
185858c9f6cSJames Smart 
186e59058c4SJames Smart /**
187e59058c4SJames Smart  * lpfc_read_la: Prepare a mailbox command for reading HBA link attention.
188e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
189e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
190e59058c4SJames Smart  * @mp: DMA buffer memory for reading the link attention information into.
191e59058c4SJames Smart  *
192e59058c4SJames Smart  * The read link attention mailbox command is issued to read the Link Event
193e59058c4SJames Smart  * Attention information indicated by the HBA port when the Link Event bit
194e59058c4SJames Smart  * of the Host Attention (HSTATT) register is set to 1. A Link Event
195e59058c4SJames Smart  * Attention occurs based on an exception detected at the Fibre Channel link
196e59058c4SJames Smart  * interface.
197e59058c4SJames Smart  *
198e59058c4SJames Smart  * This routine prepares the mailbox command for reading HBA link attention
199e59058c4SJames Smart  * information. A DMA memory has been set aside and address passed to the
200e59058c4SJames Smart  * HBA through @mp for the HBA to DMA link attention information into the
201e59058c4SJames Smart  * memory as part of the execution of the mailbox command.
202e59058c4SJames Smart  *
203e59058c4SJames Smart  * Return codes
204e59058c4SJames Smart  *    0 - Success (currently always return 0)
205e59058c4SJames Smart  **/
206dea3101eS int
207dea3101eS lpfc_read_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb, struct lpfc_dmabuf *mp)
208dea3101eS {
209dea3101eS 	MAILBOX_t *mb;
210dea3101eS 	struct lpfc_sli *psli;
211dea3101eS 
212dea3101eS 	psli = &phba->sli;
213dea3101eS 	mb = &pmb->mb;
214dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
215dea3101eS 
216dea3101eS 	INIT_LIST_HEAD(&mp->list);
217dea3101eS 	mb->mbxCommand = MBX_READ_LA64;
218dea3101eS 	mb->un.varReadLA.un.lilpBde64.tus.f.bdeSize = 128;
219dea3101eS 	mb->un.varReadLA.un.lilpBde64.addrHigh = putPaddrHigh(mp->phys);
220dea3101eS 	mb->un.varReadLA.un.lilpBde64.addrLow = putPaddrLow(mp->phys);
221dea3101eS 
222dea3101eS 	/* Save address for later completion and set the owner to host so that
223dea3101eS 	 * the FW knows this mailbox is available for processing.
224dea3101eS 	 */
225dea3101eS 	pmb->context1 = (uint8_t *) mp;
226dea3101eS 	mb->mbxOwner = OWN_HOST;
22792d7f7b0SJames Smart 	return (0);
228dea3101eS }
229dea3101eS 
230e59058c4SJames Smart /**
231e59058c4SJames Smart  * lpfc_clear_la: Prepare a mailbox command for clearing HBA link attention.
232e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
233e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
234e59058c4SJames Smart  *
235e59058c4SJames Smart  * The clear link attention mailbox command is issued to clear the link event
236e59058c4SJames Smart  * attention condition indicated by the Link Event bit of the Host Attention
237e59058c4SJames Smart  * (HSTATT) register. The link event attention condition is cleared only if
238e59058c4SJames Smart  * the event tag specified matches that of the current link event counter.
239e59058c4SJames Smart  * The current event tag is read using the read link attention event mailbox
240e59058c4SJames Smart  * command.
241e59058c4SJames Smart  *
242e59058c4SJames Smart  * This routine prepares the mailbox command for clearing HBA link attention
243e59058c4SJames Smart  * information.
244e59058c4SJames Smart  **/
245dea3101eS void
246dea3101eS lpfc_clear_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
247dea3101eS {
248dea3101eS 	MAILBOX_t *mb;
249dea3101eS 
250dea3101eS 	mb = &pmb->mb;
251dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
252dea3101eS 
253dea3101eS 	mb->un.varClearLA.eventTag = phba->fc_eventTag;
254dea3101eS 	mb->mbxCommand = MBX_CLEAR_LA;
255dea3101eS 	mb->mbxOwner = OWN_HOST;
256dea3101eS 	return;
257dea3101eS }
258dea3101eS 
259e59058c4SJames Smart /**
260e59058c4SJames Smart  * lpfc_config_link: Prepare a mailbox command for configuring link on a HBA.
261e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
262e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
263e59058c4SJames Smart  *
264e59058c4SJames Smart  * The configure link mailbox command is used before the initialize link
265e59058c4SJames Smart  * mailbox command to override default value and to configure link-oriented
266e59058c4SJames Smart  * parameters such as DID address and various timers. Typically, this
267e59058c4SJames Smart  * command would be used after an F_Port login to set the returned DID address
268e59058c4SJames Smart  * and the fabric timeout values. This command is not valid before a configure
269e59058c4SJames Smart  * port command has configured the HBA port.
270e59058c4SJames Smart  *
271e59058c4SJames Smart  * This routine prepares the mailbox command for configuring link on a HBA.
272e59058c4SJames Smart  **/
273dea3101eS void
274dea3101eS lpfc_config_link(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
275dea3101eS {
2762e0fef85SJames Smart 	struct lpfc_vport  *vport = phba->pport;
277dea3101eS 	MAILBOX_t *mb = &pmb->mb;
278dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
279dea3101eS 
280dea3101eS 	/* NEW_FEATURE
281dea3101eS 	 * SLI-2, Coalescing Response Feature.
282dea3101eS 	 */
283dea3101eS 	if (phba->cfg_cr_delay) {
284dea3101eS 		mb->un.varCfgLnk.cr = 1;
285dea3101eS 		mb->un.varCfgLnk.ci = 1;
286dea3101eS 		mb->un.varCfgLnk.cr_delay = phba->cfg_cr_delay;
287dea3101eS 		mb->un.varCfgLnk.cr_count = phba->cfg_cr_count;
288dea3101eS 	}
289dea3101eS 
2902e0fef85SJames Smart 	mb->un.varCfgLnk.myId = vport->fc_myDID;
291dea3101eS 	mb->un.varCfgLnk.edtov = phba->fc_edtov;
292dea3101eS 	mb->un.varCfgLnk.arbtov = phba->fc_arbtov;
293dea3101eS 	mb->un.varCfgLnk.ratov = phba->fc_ratov;
294dea3101eS 	mb->un.varCfgLnk.rttov = phba->fc_rttov;
295dea3101eS 	mb->un.varCfgLnk.altov = phba->fc_altov;
296dea3101eS 	mb->un.varCfgLnk.crtov = phba->fc_crtov;
297dea3101eS 	mb->un.varCfgLnk.citov = phba->fc_citov;
298dea3101eS 
299dea3101eS 	if (phba->cfg_ack0)
300dea3101eS 		mb->un.varCfgLnk.ack0_enable = 1;
301dea3101eS 
302dea3101eS 	mb->mbxCommand = MBX_CONFIG_LINK;
303dea3101eS 	mb->mbxOwner = OWN_HOST;
304dea3101eS 	return;
305dea3101eS }
306dea3101eS 
307e59058c4SJames Smart /**
3089399627fSJames Smart  * lpfc_config_msi: Prepare a mailbox command for configuring msi-x.
3099399627fSJames Smart  * @phba: pointer to lpfc hba data structure.
3109399627fSJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
3119399627fSJames Smart  *
3129399627fSJames Smart  * The configure MSI-X mailbox command is used to configure the HBA's SLI-3
3139399627fSJames Smart  * MSI-X multi-message interrupt vector association to interrupt attention
3149399627fSJames Smart  * conditions.
3159399627fSJames Smart  *
3169399627fSJames Smart  * Return codes
3179399627fSJames Smart  *    0 - Success
3189399627fSJames Smart  *    -EINVAL - Failure
3199399627fSJames Smart  **/
3209399627fSJames Smart int
3219399627fSJames Smart lpfc_config_msi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3229399627fSJames Smart {
3239399627fSJames Smart 	MAILBOX_t *mb = &pmb->mb;
3249399627fSJames Smart 	uint32_t attentionConditions[2];
3259399627fSJames Smart 
3269399627fSJames Smart 	/* Sanity check */
3279399627fSJames Smart 	if (phba->cfg_use_msi != 2) {
3289399627fSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3299399627fSJames Smart 				"0475 Not configured for supporting MSI-X "
3309399627fSJames Smart 				"cfg_use_msi: 0x%x\n", phba->cfg_use_msi);
3319399627fSJames Smart 		return -EINVAL;
3329399627fSJames Smart 	}
3339399627fSJames Smart 
3349399627fSJames Smart 	if (phba->sli_rev < 3) {
3359399627fSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3369399627fSJames Smart 				"0476 HBA not supporting SLI-3 or later "
3379399627fSJames Smart 				"SLI Revision: 0x%x\n", phba->sli_rev);
3389399627fSJames Smart 		return -EINVAL;
3399399627fSJames Smart 	}
3409399627fSJames Smart 
3419399627fSJames Smart 	/* Clear mailbox command fields */
3429399627fSJames Smart 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
3439399627fSJames Smart 
3449399627fSJames Smart 	/*
3459399627fSJames Smart 	 * SLI-3, Message Signaled Interrupt Fearure.
3469399627fSJames Smart 	 */
3479399627fSJames Smart 
3489399627fSJames Smart 	/* Multi-message attention configuration */
3499399627fSJames Smart 	attentionConditions[0] = (HA_R0ATT | HA_R1ATT | HA_R2ATT | HA_ERATT |
3509399627fSJames Smart 				  HA_LATT | HA_MBATT);
3519399627fSJames Smart 	attentionConditions[1] = 0;
3529399627fSJames Smart 
3539399627fSJames Smart 	mb->un.varCfgMSI.attentionConditions[0] = attentionConditions[0];
3549399627fSJames Smart 	mb->un.varCfgMSI.attentionConditions[1] = attentionConditions[1];
3559399627fSJames Smart 
3569399627fSJames Smart 	/*
3579399627fSJames Smart 	 * Set up message number to HA bit association
3589399627fSJames Smart 	 */
3599399627fSJames Smart #ifdef __BIG_ENDIAN_BITFIELD
3609399627fSJames Smart 	/* RA0 (FCP Ring) */
3619399627fSJames Smart 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS] = 1;
3629399627fSJames Smart 	/* RA1 (Other Protocol Extra Ring) */
3639399627fSJames Smart 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS] = 1;
3649399627fSJames Smart #else   /*  __LITTLE_ENDIAN_BITFIELD */
3659399627fSJames Smart 	/* RA0 (FCP Ring) */
3669399627fSJames Smart 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS^3] = 1;
3679399627fSJames Smart 	/* RA1 (Other Protocol Extra Ring) */
3689399627fSJames Smart 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS^3] = 1;
3699399627fSJames Smart #endif
3709399627fSJames Smart 	/* Multi-message interrupt autoclear configuration*/
3719399627fSJames Smart 	mb->un.varCfgMSI.autoClearHA[0] = attentionConditions[0];
3729399627fSJames Smart 	mb->un.varCfgMSI.autoClearHA[1] = attentionConditions[1];
3739399627fSJames Smart 
3749399627fSJames Smart 	/* For now, HBA autoclear does not work reliably, disable it */
3759399627fSJames Smart 	mb->un.varCfgMSI.autoClearHA[0] = 0;
3769399627fSJames Smart 	mb->un.varCfgMSI.autoClearHA[1] = 0;
3779399627fSJames Smart 
3789399627fSJames Smart 	/* Set command and owner bit */
3799399627fSJames Smart 	mb->mbxCommand = MBX_CONFIG_MSI;
3809399627fSJames Smart 	mb->mbxOwner = OWN_HOST;
3819399627fSJames Smart 
3829399627fSJames Smart 	return 0;
3839399627fSJames Smart }
3849399627fSJames Smart 
3859399627fSJames Smart /**
386e59058c4SJames Smart  * lpfc_init_link: Prepare a mailbox command for initialize link on a HBA.
387e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
388e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
389e59058c4SJames Smart  * @topology: the link topology for the link to be initialized to.
390e59058c4SJames Smart  * @linkspeed: the link speed for the link to be initialized to.
391e59058c4SJames Smart  *
392e59058c4SJames Smart  * The initialize link mailbox command is used to initialize the Fibre
393e59058c4SJames Smart  * Channel link. This command must follow a configure port command that
394e59058c4SJames Smart  * establishes the mode of operation.
395e59058c4SJames Smart  *
396e59058c4SJames Smart  * This routine prepares the mailbox command for initializing link on a HBA
397e59058c4SJames Smart  * with the specified link topology and speed.
398e59058c4SJames Smart  **/
399dea3101eS void
400dea3101eS lpfc_init_link(struct lpfc_hba * phba,
401dea3101eS 	       LPFC_MBOXQ_t * pmb, uint32_t topology, uint32_t linkspeed)
402dea3101eS {
403dea3101eS 	lpfc_vpd_t *vpd;
404dea3101eS 	struct lpfc_sli *psli;
405dea3101eS 	MAILBOX_t *mb;
406dea3101eS 
407dea3101eS 	mb = &pmb->mb;
408dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
409dea3101eS 
410dea3101eS 	psli = &phba->sli;
411dea3101eS 	switch (topology) {
412dea3101eS 	case FLAGS_TOPOLOGY_MODE_LOOP_PT:
413dea3101eS 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
414dea3101eS 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
415dea3101eS 		break;
416dea3101eS 	case FLAGS_TOPOLOGY_MODE_PT_PT:
417dea3101eS 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
418dea3101eS 		break;
419dea3101eS 	case FLAGS_TOPOLOGY_MODE_LOOP:
420dea3101eS 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
421dea3101eS 		break;
422dea3101eS 	case FLAGS_TOPOLOGY_MODE_PT_LOOP:
423dea3101eS 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
424dea3101eS 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
425dea3101eS 		break;
426367c2713SJamie Wellnitz 	case FLAGS_LOCAL_LB:
427367c2713SJamie Wellnitz 		mb->un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
428367c2713SJamie Wellnitz 		break;
429dea3101eS 	}
430dea3101eS 
4314b0b91d4SJames Smart 	/* Enable asynchronous ABTS responses from firmware */
4324b0b91d4SJames Smart 	mb->un.varInitLnk.link_flags |= FLAGS_IMED_ABORT;
4334b0b91d4SJames Smart 
434dea3101eS 	/* NEW_FEATURE
435dea3101eS 	 * Setting up the link speed
436dea3101eS 	 */
437dea3101eS 	vpd = &phba->vpd;
438dea3101eS 	if (vpd->rev.feaLevelHigh >= 0x02){
439dea3101eS 		switch(linkspeed){
440dea3101eS 			case LINK_SPEED_1G:
441dea3101eS 			case LINK_SPEED_2G:
442dea3101eS 			case LINK_SPEED_4G:
443b87eab38SJames Smart 			case LINK_SPEED_8G:
444dea3101eS 				mb->un.varInitLnk.link_flags |=
445dea3101eS 							FLAGS_LINK_SPEED;
446dea3101eS 				mb->un.varInitLnk.link_speed = linkspeed;
447dea3101eS 			break;
448dea3101eS 			case LINK_SPEED_AUTO:
449dea3101eS 			default:
450dea3101eS 				mb->un.varInitLnk.link_speed =
451dea3101eS 							LINK_SPEED_AUTO;
452dea3101eS 			break;
453dea3101eS 		}
454dea3101eS 
455dea3101eS 	}
456dea3101eS 	else
457dea3101eS 		mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
458dea3101eS 
459dea3101eS 	mb->mbxCommand = (volatile uint8_t)MBX_INIT_LINK;
460dea3101eS 	mb->mbxOwner = OWN_HOST;
461dea3101eS 	mb->un.varInitLnk.fabric_AL_PA = phba->fc_pref_ALPA;
462dea3101eS 	return;
463dea3101eS }
464dea3101eS 
465e59058c4SJames Smart /**
466e59058c4SJames Smart  * lpfc_read_sparam: Prepare a mailbox command for reading HBA parameters.
467e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
468e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
469e59058c4SJames Smart  * @vpi: virtual N_Port identifier.
470e59058c4SJames Smart  *
471e59058c4SJames Smart  * The read service parameter mailbox command is used to read the HBA port
472e59058c4SJames Smart  * service parameters. The service parameters are read into the buffer
473e59058c4SJames Smart  * specified directly by a BDE in the mailbox command. These service
474e59058c4SJames Smart  * parameters may then be used to build the payload of an N_Port/F_POrt
475e59058c4SJames Smart  * login request and reply (LOGI/ACC).
476e59058c4SJames Smart  *
477e59058c4SJames Smart  * This routine prepares the mailbox command for reading HBA port service
478e59058c4SJames Smart  * parameters. The DMA memory is allocated in this function and the addresses
479e59058c4SJames Smart  * are populated into the mailbox command for the HBA to DMA the service
480e59058c4SJames Smart  * parameters into.
481e59058c4SJames Smart  *
482e59058c4SJames Smart  * Return codes
483e59058c4SJames Smart  *    0 - Success
484e59058c4SJames Smart  *    1 - DMA memory allocation failed
485e59058c4SJames Smart  **/
486dea3101eS int
48792d7f7b0SJames Smart lpfc_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, int vpi)
488dea3101eS {
489dea3101eS 	struct lpfc_dmabuf *mp;
490dea3101eS 	MAILBOX_t *mb;
491dea3101eS 	struct lpfc_sli *psli;
492dea3101eS 
493dea3101eS 	psli = &phba->sli;
494dea3101eS 	mb = &pmb->mb;
495dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
496dea3101eS 
497dea3101eS 	mb->mbxOwner = OWN_HOST;
498dea3101eS 
499dea3101eS 	/* Get a buffer to hold the HBAs Service Parameters */
500dea3101eS 
50198c9ea5cSJames Smart 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
50298c9ea5cSJames Smart 	if (mp)
50398c9ea5cSJames Smart 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
50498c9ea5cSJames Smart 	if (!mp || !mp->virt) {
505dea3101eS 		kfree(mp);
506dea3101eS 		mb->mbxCommand = MBX_READ_SPARM64;
507dea3101eS 		/* READ_SPARAM: no buffers */
508e8b62011SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
509e8b62011SJames Smart 			        "0301 READ_SPARAM: no buffers\n");
51092d7f7b0SJames Smart 		return (1);
511dea3101eS 	}
512dea3101eS 	INIT_LIST_HEAD(&mp->list);
513dea3101eS 	mb->mbxCommand = MBX_READ_SPARM64;
514dea3101eS 	mb->un.varRdSparm.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
515dea3101eS 	mb->un.varRdSparm.un.sp64.addrHigh = putPaddrHigh(mp->phys);
516dea3101eS 	mb->un.varRdSparm.un.sp64.addrLow = putPaddrLow(mp->phys);
51792d7f7b0SJames Smart 	mb->un.varRdSparm.vpi = vpi;
518dea3101eS 
519dea3101eS 	/* save address for completion */
520dea3101eS 	pmb->context1 = mp;
521dea3101eS 
52292d7f7b0SJames Smart 	return (0);
523dea3101eS }
524dea3101eS 
525e59058c4SJames Smart /**
526e59058c4SJames Smart  * lpfc_unreg_did: Prepare a mailbox command for unregistering DID.
527e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
528e59058c4SJames Smart  * @vpi: virtual N_Port identifier.
529e59058c4SJames Smart  * @did: remote port identifier.
530e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
531e59058c4SJames Smart  *
532e59058c4SJames Smart  * The unregister DID mailbox command is used to unregister an N_Port/F_Port
533e59058c4SJames Smart  * login for an unknown RPI by specifying the DID of a remote port. This
534e59058c4SJames Smart  * command frees an RPI context in the HBA port. This has the effect of
535e59058c4SJames Smart  * performing an implicit N_Port/F_Port logout.
536e59058c4SJames Smart  *
537e59058c4SJames Smart  * This routine prepares the mailbox command for unregistering a remote
538e59058c4SJames Smart  * N_Port/F_Port (DID) login.
539e59058c4SJames Smart  **/
540dea3101eS void
54192d7f7b0SJames Smart lpfc_unreg_did(struct lpfc_hba * phba, uint16_t vpi, uint32_t did,
54292d7f7b0SJames Smart 	       LPFC_MBOXQ_t * pmb)
543dea3101eS {
544dea3101eS 	MAILBOX_t *mb;
545dea3101eS 
546dea3101eS 	mb = &pmb->mb;
547dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
548dea3101eS 
549dea3101eS 	mb->un.varUnregDID.did = did;
55092d7f7b0SJames Smart 	mb->un.varUnregDID.vpi = vpi;
551dea3101eS 
552dea3101eS 	mb->mbxCommand = MBX_UNREG_D_ID;
553dea3101eS 	mb->mbxOwner = OWN_HOST;
554dea3101eS 	return;
555dea3101eS }
556dea3101eS 
557e59058c4SJames Smart /**
558e59058c4SJames Smart  * lpfc_read_config: Prepare a mailbox command for reading HBA configuration.
559e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
560e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
561e59058c4SJames Smart  *
562e59058c4SJames Smart  * The read configuration mailbox command is used to read the HBA port
563e59058c4SJames Smart  * configuration parameters. This mailbox command provides a method for
564e59058c4SJames Smart  * seeing any parameters that may have changed via various configuration
565e59058c4SJames Smart  * mailbox commands.
566e59058c4SJames Smart  *
567e59058c4SJames Smart  * This routine prepares the mailbox command for reading out HBA configuration
568e59058c4SJames Smart  * parameters.
569e59058c4SJames Smart  **/
570dea3101eS void
571dea3101eS lpfc_read_config(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
572dea3101eS {
573dea3101eS 	MAILBOX_t *mb;
574dea3101eS 
575dea3101eS 	mb = &pmb->mb;
576dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
577dea3101eS 
578dea3101eS 	mb->mbxCommand = MBX_READ_CONFIG;
579dea3101eS 	mb->mbxOwner = OWN_HOST;
580dea3101eS 	return;
581dea3101eS }
582dea3101eS 
583e59058c4SJames Smart /**
584e59058c4SJames Smart  * lpfc_read_lnk_stat: Prepare a mailbox command for reading HBA link stats.
585e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
586e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
587e59058c4SJames Smart  *
588e59058c4SJames Smart  * The read link status mailbox command is used to read the link status from
589e59058c4SJames Smart  * the HBA. Link status includes all link-related error counters. These
590e59058c4SJames Smart  * counters are maintained by the HBA and originated in the link hardware
591e59058c4SJames Smart  * unit. Note that all of these counters wrap.
592e59058c4SJames Smart  *
593e59058c4SJames Smart  * This routine prepares the mailbox command for reading out HBA link status.
594e59058c4SJames Smart  **/
5957bb3b137SJamie Wellnitz void
5967bb3b137SJamie Wellnitz lpfc_read_lnk_stat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
5977bb3b137SJamie Wellnitz {
5987bb3b137SJamie Wellnitz 	MAILBOX_t *mb;
5997bb3b137SJamie Wellnitz 
6007bb3b137SJamie Wellnitz 	mb = &pmb->mb;
6017bb3b137SJamie Wellnitz 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
6027bb3b137SJamie Wellnitz 
6037bb3b137SJamie Wellnitz 	mb->mbxCommand = MBX_READ_LNK_STAT;
6047bb3b137SJamie Wellnitz 	mb->mbxOwner = OWN_HOST;
6057bb3b137SJamie Wellnitz 	return;
6067bb3b137SJamie Wellnitz }
6077bb3b137SJamie Wellnitz 
608e59058c4SJames Smart /**
609e59058c4SJames Smart  * lpfc_reg_login: Prepare a mailbox command for registering remote login.
610e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
611e59058c4SJames Smart  * @vpi: virtual N_Port identifier.
612e59058c4SJames Smart  * @did: remote port identifier.
613e59058c4SJames Smart  * @param: pointer to memory holding the server parameters.
614e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
615e59058c4SJames Smart  * @flag: action flag to be passed back for the complete function.
616e59058c4SJames Smart  *
617e59058c4SJames Smart  * The registration login mailbox command is used to register an N_Port or
618e59058c4SJames Smart  * F_Port login. This registration allows the HBA to cache the remote N_Port
619e59058c4SJames Smart  * service parameters internally and thereby make the appropriate FC-2
620e59058c4SJames Smart  * decisions. The remote port service parameters are handed off by the driver
621e59058c4SJames Smart  * to the HBA using a descriptor entry that directly identifies a buffer in
622e59058c4SJames Smart  * host memory. In exchange, the HBA returns an RPI identifier.
623e59058c4SJames Smart  *
624e59058c4SJames Smart  * This routine prepares the mailbox command for registering remote port login.
625e59058c4SJames Smart  * The function allocates DMA buffer for passing the service parameters to the
626e59058c4SJames Smart  * HBA with the mailbox command.
627e59058c4SJames Smart  *
628e59058c4SJames Smart  * Return codes
629e59058c4SJames Smart  *    0 - Success
630e59058c4SJames Smart  *    1 - DMA memory allocation failed
631e59058c4SJames Smart  **/
632dea3101eS int
63392d7f7b0SJames Smart lpfc_reg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t did,
63492d7f7b0SJames Smart 	       uint8_t *param, LPFC_MBOXQ_t *pmb, uint32_t flag)
635dea3101eS {
6362e0fef85SJames Smart 	MAILBOX_t *mb = &pmb->mb;
637dea3101eS 	uint8_t *sparam;
638dea3101eS 	struct lpfc_dmabuf *mp;
639dea3101eS 
640dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
641dea3101eS 
642dea3101eS 	mb->un.varRegLogin.rpi = 0;
64392d7f7b0SJames Smart 	mb->un.varRegLogin.vpi = vpi;
644dea3101eS 	mb->un.varRegLogin.did = did;
645dea3101eS 	mb->un.varWords[30] = flag;	/* Set flag to issue action on cmpl */
646dea3101eS 
647dea3101eS 	mb->mbxOwner = OWN_HOST;
648dea3101eS 
649dea3101eS 	/* Get a buffer to hold NPorts Service Parameters */
65098c9ea5cSJames Smart 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
65198c9ea5cSJames Smart 	if (mp)
65298c9ea5cSJames Smart 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
65398c9ea5cSJames Smart 	if (!mp || !mp->virt) {
654dea3101eS 		kfree(mp);
655dea3101eS 		mb->mbxCommand = MBX_REG_LOGIN64;
656dea3101eS 		/* REG_LOGIN: no buffers */
65792d7f7b0SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
658e8b62011SJames Smart 				"0302 REG_LOGIN: no buffers, VPI:%d DID:x%x, "
659e8b62011SJames Smart 				"flag x%x\n", vpi, did, flag);
66092d7f7b0SJames Smart 		return (1);
661dea3101eS 	}
662dea3101eS 	INIT_LIST_HEAD(&mp->list);
663dea3101eS 	sparam = mp->virt;
664dea3101eS 
665dea3101eS 	/* Copy param's into a new buffer */
666dea3101eS 	memcpy(sparam, param, sizeof (struct serv_parm));
667dea3101eS 
668dea3101eS 	/* save address for completion */
669dea3101eS 	pmb->context1 = (uint8_t *) mp;
670dea3101eS 
671dea3101eS 	mb->mbxCommand = MBX_REG_LOGIN64;
672dea3101eS 	mb->un.varRegLogin.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
673dea3101eS 	mb->un.varRegLogin.un.sp64.addrHigh = putPaddrHigh(mp->phys);
674dea3101eS 	mb->un.varRegLogin.un.sp64.addrLow = putPaddrLow(mp->phys);
675dea3101eS 
67692d7f7b0SJames Smart 	return (0);
677dea3101eS }
678dea3101eS 
679e59058c4SJames Smart /**
680e59058c4SJames Smart  * lpfc_unreg_login: Prepare a mailbox command for unregistering remote login.
681e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
682e59058c4SJames Smart  * @vpi: virtual N_Port identifier.
683e59058c4SJames Smart  * @rpi: remote port identifier
684e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
685e59058c4SJames Smart  *
686e59058c4SJames Smart  * The unregistration login mailbox command is used to unregister an N_Port
687e59058c4SJames Smart  * or F_Port login. This command frees an RPI context in the HBA. It has the
688e59058c4SJames Smart  * effect of performing an implicit N_Port/F_Port logout.
689e59058c4SJames Smart  *
690e59058c4SJames Smart  * This routine prepares the mailbox command for unregistering remote port
691e59058c4SJames Smart  * login.
692e59058c4SJames Smart  **/
693dea3101eS void
69492d7f7b0SJames Smart lpfc_unreg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t rpi,
69592d7f7b0SJames Smart 		 LPFC_MBOXQ_t * pmb)
696dea3101eS {
697dea3101eS 	MAILBOX_t *mb;
698dea3101eS 
699dea3101eS 	mb = &pmb->mb;
700dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
701dea3101eS 
702dea3101eS 	mb->un.varUnregLogin.rpi = (uint16_t) rpi;
703dea3101eS 	mb->un.varUnregLogin.rsvd1 = 0;
70492d7f7b0SJames Smart 	mb->un.varUnregLogin.vpi = vpi;
705dea3101eS 
706dea3101eS 	mb->mbxCommand = MBX_UNREG_LOGIN;
707dea3101eS 	mb->mbxOwner = OWN_HOST;
708dea3101eS 	return;
709dea3101eS }
710dea3101eS 
711e59058c4SJames Smart /**
712e59058c4SJames Smart  * lpfc_reg_vpi: Prepare a mailbox command for registering vport identifier.
713e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
714e59058c4SJames Smart  * @vpi: virtual N_Port identifier.
715e59058c4SJames Smart  * @sid: Fibre Channel S_ID (N_Port_ID assigned to a virtual N_Port).
716e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
717e59058c4SJames Smart  *
718e59058c4SJames Smart  * The registration vport identifier mailbox command is used to activate a
719e59058c4SJames Smart  * virtual N_Port after it has acquired an N_Port_ID. The HBA validates the
720e59058c4SJames Smart  * N_Port_ID against the information in the selected virtual N_Port context
721e59058c4SJames Smart  * block and marks it active to allow normal processing of IOCB commands and
722e59058c4SJames Smart  * received unsolicited exchanges.
723e59058c4SJames Smart  *
724e59058c4SJames Smart  * This routine prepares the mailbox command for registering a virtual N_Port.
725e59058c4SJames Smart  **/
72692d7f7b0SJames Smart void
72792d7f7b0SJames Smart lpfc_reg_vpi(struct lpfc_hba *phba, uint16_t vpi, uint32_t sid,
72892d7f7b0SJames Smart 	     LPFC_MBOXQ_t *pmb)
72992d7f7b0SJames Smart {
73092d7f7b0SJames Smart 	MAILBOX_t *mb = &pmb->mb;
73192d7f7b0SJames Smart 
73292d7f7b0SJames Smart 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
73392d7f7b0SJames Smart 
73492d7f7b0SJames Smart 	mb->un.varRegVpi.vpi = vpi;
73592d7f7b0SJames Smart 	mb->un.varRegVpi.sid = sid;
73692d7f7b0SJames Smart 
73792d7f7b0SJames Smart 	mb->mbxCommand = MBX_REG_VPI;
73892d7f7b0SJames Smart 	mb->mbxOwner = OWN_HOST;
73992d7f7b0SJames Smart 	return;
74092d7f7b0SJames Smart 
74192d7f7b0SJames Smart }
74292d7f7b0SJames Smart 
743e59058c4SJames Smart /**
744e59058c4SJames Smart  * lpfc_unreg_vpi: Prepare a mailbox command for unregistering vport id.
745e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
746e59058c4SJames Smart  * @vpi: virtual N_Port identifier.
747e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
748e59058c4SJames Smart  *
749e59058c4SJames Smart  * The unregistration vport identifier mailbox command is used to inactivate
750e59058c4SJames Smart  * a virtual N_Port. The driver must have logged out and unregistered all
751e59058c4SJames Smart  * remote N_Ports to abort any activity on the virtual N_Port. The HBA will
752e59058c4SJames Smart  * unregisters any default RPIs associated with the specified vpi, aborting
753e59058c4SJames Smart  * any active exchanges. The HBA will post the mailbox response after making
754e59058c4SJames Smart  * the virtual N_Port inactive.
755e59058c4SJames Smart  *
756e59058c4SJames Smart  * This routine prepares the mailbox command for unregistering a virtual
757e59058c4SJames Smart  * N_Port.
758e59058c4SJames Smart  **/
75992d7f7b0SJames Smart void
76092d7f7b0SJames Smart lpfc_unreg_vpi(struct lpfc_hba *phba, uint16_t vpi, LPFC_MBOXQ_t *pmb)
76192d7f7b0SJames Smart {
76292d7f7b0SJames Smart 	MAILBOX_t *mb = &pmb->mb;
76392d7f7b0SJames Smart 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
76492d7f7b0SJames Smart 
76592d7f7b0SJames Smart 	mb->un.varUnregVpi.vpi = vpi;
76692d7f7b0SJames Smart 
76792d7f7b0SJames Smart 	mb->mbxCommand = MBX_UNREG_VPI;
76892d7f7b0SJames Smart 	mb->mbxOwner = OWN_HOST;
76992d7f7b0SJames Smart 	return;
77092d7f7b0SJames Smart 
77192d7f7b0SJames Smart }
77292d7f7b0SJames Smart 
773e59058c4SJames Smart /**
774e59058c4SJames Smart  * lpfc_config_pcb_setup: Set up IOCB rings in the Port Control Block (PCB)
775e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
776e59058c4SJames Smart  *
777e59058c4SJames Smart  * This routine sets up and initializes the IOCB rings in the Port Control
778e59058c4SJames Smart  * Block (PCB).
779e59058c4SJames Smart  **/
780dea3101eS static void
781dea3101eS lpfc_config_pcb_setup(struct lpfc_hba * phba)
782dea3101eS {
783dea3101eS 	struct lpfc_sli *psli = &phba->sli;
784dea3101eS 	struct lpfc_sli_ring *pring;
78534b02dcdSJames Smart 	PCB_t *pcbp = phba->pcb;
786dea3101eS 	dma_addr_t pdma_addr;
787dea3101eS 	uint32_t offset;
7882e0fef85SJames Smart 	uint32_t iocbCnt = 0;
789dea3101eS 	int i;
790dea3101eS 
791dea3101eS 	pcbp->maxRing = (psli->num_rings - 1);
792dea3101eS 
793dea3101eS 	for (i = 0; i < psli->num_rings; i++) {
794dea3101eS 		pring = &psli->ring[i];
7952e0fef85SJames Smart 
796ed957684SJames Smart 		pring->sizeCiocb = phba->sli_rev == 3 ? SLI3_IOCB_CMD_SIZE:
797ed957684SJames Smart 							SLI2_IOCB_CMD_SIZE;
798ed957684SJames Smart 		pring->sizeRiocb = phba->sli_rev == 3 ? SLI3_IOCB_RSP_SIZE:
799ed957684SJames Smart 							SLI2_IOCB_RSP_SIZE;
800dea3101eS 		/* A ring MUST have both cmd and rsp entries defined to be
801dea3101eS 		   valid */
802dea3101eS 		if ((pring->numCiocb == 0) || (pring->numRiocb == 0)) {
803dea3101eS 			pcbp->rdsc[i].cmdEntries = 0;
804dea3101eS 			pcbp->rdsc[i].rspEntries = 0;
805dea3101eS 			pcbp->rdsc[i].cmdAddrHigh = 0;
806dea3101eS 			pcbp->rdsc[i].rspAddrHigh = 0;
807dea3101eS 			pcbp->rdsc[i].cmdAddrLow = 0;
808dea3101eS 			pcbp->rdsc[i].rspAddrLow = 0;
809dea3101eS 			pring->cmdringaddr = NULL;
810dea3101eS 			pring->rspringaddr = NULL;
811dea3101eS 			continue;
812dea3101eS 		}
813dea3101eS 		/* Command ring setup for ring */
81434b02dcdSJames Smart 		pring->cmdringaddr = (void *)&phba->IOCBs[iocbCnt];
815dea3101eS 		pcbp->rdsc[i].cmdEntries = pring->numCiocb;
816dea3101eS 
81734b02dcdSJames Smart 		offset = (uint8_t *) &phba->IOCBs[iocbCnt] -
81834b02dcdSJames Smart 			 (uint8_t *) phba->slim2p.virt;
81934b02dcdSJames Smart 		pdma_addr = phba->slim2p.phys + offset;
820dea3101eS 		pcbp->rdsc[i].cmdAddrHigh = putPaddrHigh(pdma_addr);
821dea3101eS 		pcbp->rdsc[i].cmdAddrLow = putPaddrLow(pdma_addr);
822dea3101eS 		iocbCnt += pring->numCiocb;
823dea3101eS 
824dea3101eS 		/* Response ring setup for ring */
82534b02dcdSJames Smart 		pring->rspringaddr = (void *) &phba->IOCBs[iocbCnt];
826dea3101eS 
827dea3101eS 		pcbp->rdsc[i].rspEntries = pring->numRiocb;
82834b02dcdSJames Smart 		offset = (uint8_t *)&phba->IOCBs[iocbCnt] -
82934b02dcdSJames Smart 			 (uint8_t *)phba->slim2p.virt;
83034b02dcdSJames Smart 		pdma_addr = phba->slim2p.phys + offset;
831dea3101eS 		pcbp->rdsc[i].rspAddrHigh = putPaddrHigh(pdma_addr);
832dea3101eS 		pcbp->rdsc[i].rspAddrLow = putPaddrLow(pdma_addr);
833dea3101eS 		iocbCnt += pring->numRiocb;
834dea3101eS 	}
835dea3101eS }
836dea3101eS 
837e59058c4SJames Smart /**
838e59058c4SJames Smart  * lpfc_read_rev: Prepare a mailbox command for reading HBA revision.
839e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
840e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
841e59058c4SJames Smart  *
842e59058c4SJames Smart  * The read revision mailbox command is used to read the revision levels of
843e59058c4SJames Smart  * the HBA components. These components include hardware units, resident
844e59058c4SJames Smart  * firmware, and available firmware. HBAs that supports SLI-3 mode of
845e59058c4SJames Smart  * operation provide different response information depending on the version
846e59058c4SJames Smart  * requested by the driver.
847e59058c4SJames Smart  *
848e59058c4SJames Smart  * This routine prepares the mailbox command for reading HBA revision
849e59058c4SJames Smart  * information.
850e59058c4SJames Smart  **/
851dea3101eS void
852dea3101eS lpfc_read_rev(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
853dea3101eS {
8542e0fef85SJames Smart 	MAILBOX_t *mb = &pmb->mb;
855dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
856dea3101eS 	mb->un.varRdRev.cv = 1;
857ed957684SJames Smart 	mb->un.varRdRev.v3req = 1; /* Request SLI3 info */
858dea3101eS 	mb->mbxCommand = MBX_READ_REV;
859dea3101eS 	mb->mbxOwner = OWN_HOST;
860dea3101eS 	return;
861dea3101eS }
862dea3101eS 
863e59058c4SJames Smart /**
864e59058c4SJames Smart  * lpfc_build_hbq_profile2: Set up the HBQ Selection Profile 2.
865e59058c4SJames Smart  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
866e59058c4SJames Smart  * @hbq_desc: pointer to the HBQ selection profile descriptor.
867e59058c4SJames Smart  *
868e59058c4SJames Smart  * The Host Buffer Queue (HBQ) Selection Profile 2 specifies that the HBA
869e59058c4SJames Smart  * tests the incoming frames' R_CTL/TYPE fields with works 10:15 and performs
870e59058c4SJames Smart  * the Sequence Length Test using the fields in the Selection Profile 2
871e59058c4SJames Smart  * extension in words 20:31.
872e59058c4SJames Smart  **/
873ed957684SJames Smart static void
874ed957684SJames Smart lpfc_build_hbq_profile2(struct config_hbq_var *hbqmb,
875ed957684SJames Smart 			struct lpfc_hbq_init  *hbq_desc)
876ed957684SJames Smart {
877ed957684SJames Smart 	hbqmb->profiles.profile2.seqlenbcnt = hbq_desc->seqlenbcnt;
878ed957684SJames Smart 	hbqmb->profiles.profile2.maxlen     = hbq_desc->maxlen;
879ed957684SJames Smart 	hbqmb->profiles.profile2.seqlenoff  = hbq_desc->seqlenoff;
880ed957684SJames Smart }
881ed957684SJames Smart 
882e59058c4SJames Smart /**
883e59058c4SJames Smart  * lpfc_build_hbq_profile3: Set up the HBQ Selection Profile 3.
884e59058c4SJames Smart  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
885e59058c4SJames Smart  * @hbq_desc: pointer to the HBQ selection profile descriptor.
886e59058c4SJames Smart  *
887e59058c4SJames Smart  * The Host Buffer Queue (HBQ) Selection Profile 3 specifies that the HBA
888e59058c4SJames Smart  * tests the incoming frame's R_CTL/TYPE fields with words 10:15 and performs
889e59058c4SJames Smart  * the Sequence Length Test and Byte Field Test using the fields in the
890e59058c4SJames Smart  * Selection Profile 3 extension in words 20:31.
891e59058c4SJames Smart  **/
892ed957684SJames Smart static void
893ed957684SJames Smart lpfc_build_hbq_profile3(struct config_hbq_var *hbqmb,
894ed957684SJames Smart 			struct lpfc_hbq_init  *hbq_desc)
895ed957684SJames Smart {
896ed957684SJames Smart 	hbqmb->profiles.profile3.seqlenbcnt = hbq_desc->seqlenbcnt;
897ed957684SJames Smart 	hbqmb->profiles.profile3.maxlen     = hbq_desc->maxlen;
898ed957684SJames Smart 	hbqmb->profiles.profile3.cmdcodeoff = hbq_desc->cmdcodeoff;
899ed957684SJames Smart 	hbqmb->profiles.profile3.seqlenoff  = hbq_desc->seqlenoff;
900ed957684SJames Smart 	memcpy(&hbqmb->profiles.profile3.cmdmatch, hbq_desc->cmdmatch,
901ed957684SJames Smart 	       sizeof(hbqmb->profiles.profile3.cmdmatch));
902ed957684SJames Smart }
903ed957684SJames Smart 
904e59058c4SJames Smart /**
905e59058c4SJames Smart  * lpfc_build_hbq_profile5: Set up the HBQ Selection Profile 5.
906e59058c4SJames Smart  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
907e59058c4SJames Smart  * @hbq_desc: pointer to the HBQ selection profile descriptor.
908e59058c4SJames Smart  *
909e59058c4SJames Smart  * The Host Buffer Queue (HBQ) Selection Profile 5 specifies a header HBQ. The
910e59058c4SJames Smart  * HBA tests the initial frame of an incoming sequence using the frame's
911e59058c4SJames Smart  * R_CTL/TYPE fields with words 10:15 and performs the Sequence Length Test
912e59058c4SJames Smart  * and Byte Field Test using the fields in the Selection Profile 5 extension
913e59058c4SJames Smart  * words 20:31.
914e59058c4SJames Smart  **/
915ed957684SJames Smart static void
916ed957684SJames Smart lpfc_build_hbq_profile5(struct config_hbq_var *hbqmb,
917ed957684SJames Smart 			struct lpfc_hbq_init  *hbq_desc)
918ed957684SJames Smart {
919ed957684SJames Smart 	hbqmb->profiles.profile5.seqlenbcnt = hbq_desc->seqlenbcnt;
920ed957684SJames Smart 	hbqmb->profiles.profile5.maxlen     = hbq_desc->maxlen;
921ed957684SJames Smart 	hbqmb->profiles.profile5.cmdcodeoff = hbq_desc->cmdcodeoff;
922ed957684SJames Smart 	hbqmb->profiles.profile5.seqlenoff  = hbq_desc->seqlenoff;
923ed957684SJames Smart 	memcpy(&hbqmb->profiles.profile5.cmdmatch, hbq_desc->cmdmatch,
924ed957684SJames Smart 	       sizeof(hbqmb->profiles.profile5.cmdmatch));
925ed957684SJames Smart }
926ed957684SJames Smart 
927e59058c4SJames Smart /**
928e59058c4SJames Smart  * lpfc_config_hbq: Prepare a mailbox command for configuring an HBQ.
929e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
930e59058c4SJames Smart  * @id: HBQ identifier.
931e59058c4SJames Smart  * @hbq_desc: pointer to the HBA descriptor data structure.
932e59058c4SJames Smart  * @hbq_entry_index: index of the HBQ entry data structures.
933e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
934e59058c4SJames Smart  *
935e59058c4SJames Smart  * The configure HBQ (Host Buffer Queue) mailbox command is used to configure
936e59058c4SJames Smart  * an HBQ. The configuration binds events that require buffers to a particular
937e59058c4SJames Smart  * ring and HBQ based on a selection profile.
938e59058c4SJames Smart  *
939e59058c4SJames Smart  * This routine prepares the mailbox command for configuring an HBQ.
940e59058c4SJames Smart  **/
941ed957684SJames Smart void
94251ef4c26SJames Smart lpfc_config_hbq(struct lpfc_hba *phba, uint32_t id,
94351ef4c26SJames Smart 		 struct lpfc_hbq_init *hbq_desc,
944ed957684SJames Smart 		uint32_t hbq_entry_index, LPFC_MBOXQ_t *pmb)
945ed957684SJames Smart {
946ed957684SJames Smart 	int i;
947ed957684SJames Smart 	MAILBOX_t *mb = &pmb->mb;
948ed957684SJames Smart 	struct config_hbq_var *hbqmb = &mb->un.varCfgHbq;
949ed957684SJames Smart 
950ed957684SJames Smart 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
95151ef4c26SJames Smart 	hbqmb->hbqId = id;
952ed957684SJames Smart 	hbqmb->entry_count = hbq_desc->entry_count;   /* # entries in HBQ */
953ed957684SJames Smart 	hbqmb->recvNotify = hbq_desc->rn;             /* Receive
954ed957684SJames Smart 						       * Notification */
955ed957684SJames Smart 	hbqmb->numMask    = hbq_desc->mask_count;     /* # R_CTL/TYPE masks
956ed957684SJames Smart 						       * # in words 0-19 */
957ed957684SJames Smart 	hbqmb->profile    = hbq_desc->profile;	      /* Selection profile:
958ed957684SJames Smart 						       * 0 = all,
959ed957684SJames Smart 						       * 7 = logentry */
960ed957684SJames Smart 	hbqmb->ringMask   = hbq_desc->ring_mask;      /* Binds HBQ to a ring
961ed957684SJames Smart 						       * e.g. Ring0=b0001,
962ed957684SJames Smart 						       * ring2=b0100 */
963ed957684SJames Smart 	hbqmb->headerLen  = hbq_desc->headerLen;      /* 0 if not profile 4
964ed957684SJames Smart 						       * or 5 */
965ed957684SJames Smart 	hbqmb->logEntry   = hbq_desc->logEntry;       /* Set to 1 if this
966ed957684SJames Smart 						       * HBQ will be used
967ed957684SJames Smart 						       * for LogEntry
968ed957684SJames Smart 						       * buffers */
969ed957684SJames Smart 	hbqmb->hbqaddrLow = putPaddrLow(phba->hbqslimp.phys) +
970ed957684SJames Smart 		hbq_entry_index * sizeof(struct lpfc_hbq_entry);
971ed957684SJames Smart 	hbqmb->hbqaddrHigh = putPaddrHigh(phba->hbqslimp.phys);
972ed957684SJames Smart 
973ed957684SJames Smart 	mb->mbxCommand = MBX_CONFIG_HBQ;
974ed957684SJames Smart 	mb->mbxOwner = OWN_HOST;
975ed957684SJames Smart 
976ed957684SJames Smart 				/* Copy info for profiles 2,3,5. Other
977ed957684SJames Smart 				 * profiles this area is reserved
978ed957684SJames Smart 				 */
979ed957684SJames Smart 	if (hbq_desc->profile == 2)
980ed957684SJames Smart 		lpfc_build_hbq_profile2(hbqmb, hbq_desc);
981ed957684SJames Smart 	else if (hbq_desc->profile == 3)
982ed957684SJames Smart 		lpfc_build_hbq_profile3(hbqmb, hbq_desc);
983ed957684SJames Smart 	else if (hbq_desc->profile == 5)
984ed957684SJames Smart 		lpfc_build_hbq_profile5(hbqmb, hbq_desc);
985ed957684SJames Smart 
986ed957684SJames Smart 	/* Return if no rctl / type masks for this HBQ */
987ed957684SJames Smart 	if (!hbq_desc->mask_count)
988ed957684SJames Smart 		return;
989ed957684SJames Smart 
990ed957684SJames Smart 	/* Otherwise we setup specific rctl / type masks for this HBQ */
991ed957684SJames Smart 	for (i = 0; i < hbq_desc->mask_count; i++) {
992ed957684SJames Smart 		hbqmb->hbqMasks[i].tmatch = hbq_desc->hbqMasks[i].tmatch;
993ed957684SJames Smart 		hbqmb->hbqMasks[i].tmask  = hbq_desc->hbqMasks[i].tmask;
994ed957684SJames Smart 		hbqmb->hbqMasks[i].rctlmatch = hbq_desc->hbqMasks[i].rctlmatch;
995ed957684SJames Smart 		hbqmb->hbqMasks[i].rctlmask  = hbq_desc->hbqMasks[i].rctlmask;
996ed957684SJames Smart 	}
997ed957684SJames Smart 
998ed957684SJames Smart 	return;
999ed957684SJames Smart }
1000ed957684SJames Smart 
1001e59058c4SJames Smart /**
1002e59058c4SJames Smart  * lpfc_config_ring: Prepare a mailbox command for configuring an IOCB ring.
1003e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
1004e59058c4SJames Smart  * @ring:
1005e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
1006e59058c4SJames Smart  *
1007e59058c4SJames Smart  * The configure ring mailbox command is used to configure an IOCB ring. This
1008e59058c4SJames Smart  * configuration binds from one to six of HBA RC_CTL/TYPE mask entries to the
1009e59058c4SJames Smart  * ring. This is used to map incoming sequences to a particular ring whose
1010e59058c4SJames Smart  * RC_CTL/TYPE mask entry matches that of the sequence. The driver should not
1011e59058c4SJames Smart  * attempt to configure a ring whose number is greater than the number
1012e59058c4SJames Smart  * specified in the Port Control Block (PCB). It is an error to issue the
1013e59058c4SJames Smart  * configure ring command more than once with the same ring number. The HBA
1014e59058c4SJames Smart  * returns an error if the driver attempts this.
1015e59058c4SJames Smart  *
1016e59058c4SJames Smart  * This routine prepares the mailbox command for configuring IOCB ring.
1017e59058c4SJames Smart  **/
1018dea3101eS void
1019dea3101eS lpfc_config_ring(struct lpfc_hba * phba, int ring, LPFC_MBOXQ_t * pmb)
1020dea3101eS {
1021dea3101eS 	int i;
1022dea3101eS 	MAILBOX_t *mb = &pmb->mb;
1023dea3101eS 	struct lpfc_sli *psli;
1024dea3101eS 	struct lpfc_sli_ring *pring;
1025dea3101eS 
1026dea3101eS 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1027dea3101eS 
1028dea3101eS 	mb->un.varCfgRing.ring = ring;
1029dea3101eS 	mb->un.varCfgRing.maxOrigXchg = 0;
1030dea3101eS 	mb->un.varCfgRing.maxRespXchg = 0;
1031dea3101eS 	mb->un.varCfgRing.recvNotify = 1;
1032dea3101eS 
1033dea3101eS 	psli = &phba->sli;
1034dea3101eS 	pring = &psli->ring[ring];
1035dea3101eS 	mb->un.varCfgRing.numMask = pring->num_mask;
1036dea3101eS 	mb->mbxCommand = MBX_CONFIG_RING;
1037dea3101eS 	mb->mbxOwner = OWN_HOST;
1038dea3101eS 
1039dea3101eS 	/* Is this ring configured for a specific profile */
1040dea3101eS 	if (pring->prt[0].profile) {
1041dea3101eS 		mb->un.varCfgRing.profile = pring->prt[0].profile;
1042dea3101eS 		return;
1043dea3101eS 	}
1044dea3101eS 
1045dea3101eS 	/* Otherwise we setup specific rctl / type masks for this ring */
1046dea3101eS 	for (i = 0; i < pring->num_mask; i++) {
1047dea3101eS 		mb->un.varCfgRing.rrRegs[i].rval = pring->prt[i].rctl;
1048dea3101eS 		if (mb->un.varCfgRing.rrRegs[i].rval != FC_ELS_REQ)
1049dea3101eS 			mb->un.varCfgRing.rrRegs[i].rmask = 0xff;
1050dea3101eS 		else
1051dea3101eS 			mb->un.varCfgRing.rrRegs[i].rmask = 0xfe;
1052dea3101eS 		mb->un.varCfgRing.rrRegs[i].tval = pring->prt[i].type;
1053dea3101eS 		mb->un.varCfgRing.rrRegs[i].tmask = 0xff;
1054dea3101eS 	}
1055dea3101eS 
1056dea3101eS 	return;
1057dea3101eS }
1058dea3101eS 
1059e59058c4SJames Smart /**
1060e59058c4SJames Smart  * lpfc_config_port: Prepare a mailbox command for configuring port.
1061e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
1062e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
1063e59058c4SJames Smart  *
1064e59058c4SJames Smart  * The configure port mailbox command is used to identify the Port Control
1065e59058c4SJames Smart  * Block (PCB) in the driver memory. After this command is issued, the
1066e59058c4SJames Smart  * driver must not access the mailbox in the HBA without first resetting
1067e59058c4SJames Smart  * the HBA. The HBA may copy the PCB information to internal storage for
1068e59058c4SJames Smart  * subsequent use; the driver can not change the PCB information unless it
1069e59058c4SJames Smart  * resets the HBA.
1070e59058c4SJames Smart  *
1071e59058c4SJames Smart  * This routine prepares the mailbox command for configuring port.
1072e59058c4SJames Smart  **/
1073dea3101eS void
1074dea3101eS lpfc_config_port(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1075dea3101eS {
1076ed957684SJames Smart 	MAILBOX_t __iomem *mb_slim = (MAILBOX_t __iomem *) phba->MBslimaddr;
1077dea3101eS 	MAILBOX_t *mb = &pmb->mb;
1078dea3101eS 	dma_addr_t pdma_addr;
1079dea3101eS 	uint32_t bar_low, bar_high;
1080dea3101eS 	size_t offset;
10814cc2da1dSJames.Smart@Emulex.Com 	struct lpfc_hgp hgp;
1082f91b392cSJames.Smart@Emulex.Com 	int i;
1083ed957684SJames Smart 	uint32_t pgp_offset;
1084dea3101eS 
1085dea3101eS 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1086dea3101eS 	mb->mbxCommand = MBX_CONFIG_PORT;
1087dea3101eS 	mb->mbxOwner = OWN_HOST;
1088dea3101eS 
1089dea3101eS 	mb->un.varCfgPort.pcbLen = sizeof(PCB_t);
1090dea3101eS 
109134b02dcdSJames Smart 	offset = (uint8_t *)phba->pcb - (uint8_t *)phba->slim2p.virt;
109234b02dcdSJames Smart 	pdma_addr = phba->slim2p.phys + offset;
1093dea3101eS 	mb->un.varCfgPort.pcbLow = putPaddrLow(pdma_addr);
1094dea3101eS 	mb->un.varCfgPort.pcbHigh = putPaddrHigh(pdma_addr);
1095dea3101eS 
109697207482SJames Smart 	/* Always Host Group Pointer is in SLIM */
109797207482SJames Smart 	mb->un.varCfgPort.hps = 1;
109897207482SJames Smart 
1099ed957684SJames Smart 	/* If HBA supports SLI=3 ask for it */
1100ed957684SJames Smart 
110192d7f7b0SJames Smart 	if (phba->sli_rev == 3 && phba->vpd.sli3Feat.cerbm) {
1102e2a0a9d6SJames Smart 		if (phba->cfg_enable_bg)
1103e2a0a9d6SJames Smart 			mb->un.varCfgPort.cbg = 1; /* configure BlockGuard */
1104ed957684SJames Smart 		mb->un.varCfgPort.cerbm = 1; /* Request HBQs */
110534b02dcdSJames Smart 		mb->un.varCfgPort.ccrp = 1; /* Command Ring Polling */
110634b02dcdSJames Smart 		mb->un.varCfgPort.cinb = 1; /* Interrupt Notification Block */
110751ef4c26SJames Smart 		mb->un.varCfgPort.max_hbq = lpfc_sli_hbq_count();
110878b2d852SJames Smart 		if (phba->max_vpi && phba->cfg_enable_npiv &&
110992d7f7b0SJames Smart 		    phba->vpd.sli3Feat.cmv) {
111092d7f7b0SJames Smart 			mb->un.varCfgPort.max_vpi = phba->max_vpi;
111192d7f7b0SJames Smart 			mb->un.varCfgPort.cmv = 1;
111292d7f7b0SJames Smart 		} else
111392d7f7b0SJames Smart 			mb->un.varCfgPort.max_vpi = phba->max_vpi = 0;
111492d7f7b0SJames Smart 	} else
111592d7f7b0SJames Smart 		phba->sli_rev = 2;
111692d7f7b0SJames Smart 	mb->un.varCfgPort.sli_mode = phba->sli_rev;
1117ed957684SJames Smart 
1118dea3101eS 	/* Now setup pcb */
111934b02dcdSJames Smart 	phba->pcb->type = TYPE_NATIVE_SLI2;
112034b02dcdSJames Smart 	phba->pcb->feature = FEATURE_INITIAL_SLI2;
1121dea3101eS 
1122dea3101eS 	/* Setup Mailbox pointers */
112334b02dcdSJames Smart 	phba->pcb->mailBoxSize = sizeof(MAILBOX_t);
112434b02dcdSJames Smart 	offset = (uint8_t *)phba->mbox - (uint8_t *)phba->slim2p.virt;
112534b02dcdSJames Smart 	pdma_addr = phba->slim2p.phys + offset;
112634b02dcdSJames Smart 	phba->pcb->mbAddrHigh = putPaddrHigh(pdma_addr);
112734b02dcdSJames Smart 	phba->pcb->mbAddrLow = putPaddrLow(pdma_addr);
1128dea3101eS 
1129dea3101eS 	/*
1130dea3101eS 	 * Setup Host Group ring pointer.
1131dea3101eS 	 *
1132dea3101eS 	 * For efficiency reasons, the ring get/put pointers can be
1133dea3101eS 	 * placed in adapter memory (SLIM) rather than in host memory.
1134dea3101eS 	 * This allows firmware to avoid PCI reads/writes when updating
1135dea3101eS 	 * and checking pointers.
1136dea3101eS 	 *
1137dea3101eS 	 * The firmware recognizes the use of SLIM memory by comparing
1138dea3101eS 	 * the address of the get/put pointers structure with that of
1139dea3101eS 	 * the SLIM BAR (BAR0).
1140dea3101eS 	 *
1141dea3101eS 	 * Caution: be sure to use the PCI config space value of BAR0/BAR1
1142dea3101eS 	 * (the hardware's view of the base address), not the OS's
1143dea3101eS 	 * value of pci_resource_start() as the OS value may be a cookie
1144dea3101eS 	 * for ioremap/iomap.
1145dea3101eS 	 */
1146dea3101eS 
1147dea3101eS 
1148dea3101eS 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_0, &bar_low);
1149dea3101eS 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_1, &bar_high);
1150dea3101eS 
1151ed957684SJames Smart 	/*
1152ed957684SJames Smart 	 * Set up HGP - Port Memory
1153ed957684SJames Smart 	 *
1154ed957684SJames Smart 	 * The port expects the host get/put pointers to reside in memory
1155ed957684SJames Smart 	 * following the "non-diagnostic" mode mailbox (32 words, 0x80 bytes)
1156ed957684SJames Smart 	 * area of SLIM.  In SLI-2 mode, there's an additional 16 reserved
1157ed957684SJames Smart 	 * words (0x40 bytes).  This area is not reserved if HBQs are
1158ed957684SJames Smart 	 * configured in SLI-3.
1159ed957684SJames Smart 	 *
1160ed957684SJames Smart 	 * CR0Put    - SLI2(no HBQs) = 0xc0, With HBQs = 0x80
1161ed957684SJames Smart 	 * RR0Get                      0xc4              0x84
1162ed957684SJames Smart 	 * CR1Put                      0xc8              0x88
1163ed957684SJames Smart 	 * RR1Get                      0xcc              0x8c
1164ed957684SJames Smart 	 * CR2Put                      0xd0              0x90
1165ed957684SJames Smart 	 * RR2Get                      0xd4              0x94
1166ed957684SJames Smart 	 * CR3Put                      0xd8              0x98
1167ed957684SJames Smart 	 * RR3Get                      0xdc              0x9c
1168ed957684SJames Smart 	 *
1169ed957684SJames Smart 	 * Reserved                    0xa0-0xbf
1170ed957684SJames Smart 	 *    If HBQs configured:
1171ed957684SJames Smart 	 *                         HBQ 0 Put ptr  0xc0
1172ed957684SJames Smart 	 *                         HBQ 1 Put ptr  0xc4
1173ed957684SJames Smart 	 *                         HBQ 2 Put ptr  0xc8
1174ed957684SJames Smart 	 *                         ......
1175ed957684SJames Smart 	 *                         HBQ(M-1)Put Pointer 0xc0+(M-1)*4
1176ed957684SJames Smart 	 *
1177ed957684SJames Smart 	 */
1178ed957684SJames Smart 
1179ed957684SJames Smart 	if (phba->sli_rev == 3) {
1180ed957684SJames Smart 		phba->host_gp = &mb_slim->us.s3.host[0];
1181ed957684SJames Smart 		phba->hbq_put = &mb_slim->us.s3.hbq_put[0];
1182ed957684SJames Smart 	} else {
1183ed957684SJames Smart 		phba->host_gp = &mb_slim->us.s2.host[0];
1184ed957684SJames Smart 		phba->hbq_put = NULL;
1185ed957684SJames Smart 	}
1186dea3101eS 
1187dea3101eS 	/* mask off BAR0's flag bits 0 - 3 */
118834b02dcdSJames Smart 	phba->pcb->hgpAddrLow = (bar_low & PCI_BASE_ADDRESS_MEM_MASK) +
1189ed957684SJames Smart 		(void __iomem *)phba->host_gp -
1190ed957684SJames Smart 		(void __iomem *)phba->MBslimaddr;
1191dea3101eS 	if (bar_low & PCI_BASE_ADDRESS_MEM_TYPE_64)
119234b02dcdSJames Smart 		phba->pcb->hgpAddrHigh = bar_high;
1193dea3101eS 	else
119434b02dcdSJames Smart 		phba->pcb->hgpAddrHigh = 0;
1195dea3101eS 	/* write HGP data to SLIM at the required longword offset */
11964cc2da1dSJames.Smart@Emulex.Com 	memset(&hgp, 0, sizeof(struct lpfc_hgp));
1197f91b392cSJames.Smart@Emulex.Com 
1198f91b392cSJames.Smart@Emulex.Com 	for (i=0; i < phba->sli.num_rings; i++) {
1199ed957684SJames Smart 		lpfc_memcpy_to_slim(phba->host_gp + i, &hgp,
1200ed957684SJames Smart 				    sizeof(*phba->host_gp));
1201f91b392cSJames.Smart@Emulex.Com 	}
1202dea3101eS 
12038f34f4ceSJames Smart 	/* Setup Port Group offset */
12048f34f4ceSJames Smart 	if (phba->sli_rev == 3)
120534b02dcdSJames Smart 		pgp_offset = offsetof(struct lpfc_sli2_slim,
120634b02dcdSJames Smart 				      mbx.us.s3_pgp.port);
12078f34f4ceSJames Smart 	else
120834b02dcdSJames Smart 		pgp_offset = offsetof(struct lpfc_sli2_slim, mbx.us.s2.port);
120934b02dcdSJames Smart 	pdma_addr = phba->slim2p.phys + pgp_offset;
121034b02dcdSJames Smart 	phba->pcb->pgpAddrHigh = putPaddrHigh(pdma_addr);
121134b02dcdSJames Smart 	phba->pcb->pgpAddrLow = putPaddrLow(pdma_addr);
1212dea3101eS 
1213dea3101eS 	/* Use callback routine to setp rings in the pcb */
1214dea3101eS 	lpfc_config_pcb_setup(phba);
1215dea3101eS 
1216dea3101eS 	/* special handling for LC HBAs */
1217dea3101eS 	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
1218dea3101eS 		uint32_t hbainit[5];
1219dea3101eS 
1220dea3101eS 		lpfc_hba_init(phba, hbainit);
1221dea3101eS 
1222dea3101eS 		memcpy(&mb->un.varCfgPort.hbainit, hbainit, 20);
1223dea3101eS 	}
1224dea3101eS 
1225dea3101eS 	/* Swap PCB if needed */
122634b02dcdSJames Smart 	lpfc_sli_pcimem_bcopy(phba->pcb, phba->pcb, sizeof(PCB_t));
1227dea3101eS }
1228dea3101eS 
1229e59058c4SJames Smart /**
1230e59058c4SJames Smart  * lpfc_kill_board: Prepare a mailbox command for killing board.
1231e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
1232e59058c4SJames Smart  * @pmb: pointer to the driver internal queue element for mailbox command.
1233e59058c4SJames Smart  *
1234e59058c4SJames Smart  * The kill board mailbox command is used to tell firmware to perform a
1235e59058c4SJames Smart  * graceful shutdown of a channel on a specified board to prepare for reset.
1236e59058c4SJames Smart  * When the kill board mailbox command is received, the ER3 bit is set to 1
1237e59058c4SJames Smart  * in the Host Status register and the ER Attention bit is set to 1 in the
1238e59058c4SJames Smart  * Host Attention register of the HBA function that received the kill board
1239e59058c4SJames Smart  * command.
1240e59058c4SJames Smart  *
1241e59058c4SJames Smart  * This routine prepares the mailbox command for killing the board in
1242e59058c4SJames Smart  * preparation for a graceful shutdown.
1243e59058c4SJames Smart  **/
1244dea3101eS void
124541415862SJamie Wellnitz lpfc_kill_board(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
124641415862SJamie Wellnitz {
124741415862SJamie Wellnitz 	MAILBOX_t *mb = &pmb->mb;
124841415862SJamie Wellnitz 
124941415862SJamie Wellnitz 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
125041415862SJamie Wellnitz 	mb->mbxCommand = MBX_KILL_BOARD;
125141415862SJamie Wellnitz 	mb->mbxOwner = OWN_HOST;
125241415862SJamie Wellnitz 	return;
125341415862SJamie Wellnitz }
125441415862SJamie Wellnitz 
1255e59058c4SJames Smart /**
1256e59058c4SJames Smart  * lpfc_mbox_put: Put a mailbox cmd into the tail of driver's mailbox queue.
1257e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
1258e59058c4SJames Smart  * @mbq: pointer to the driver internal queue element for mailbox command.
1259e59058c4SJames Smart  *
1260e59058c4SJames Smart  * Driver maintains a internal mailbox command queue implemented as a linked
1261e59058c4SJames Smart  * list. When a mailbox command is issued, it shall be put into the mailbox
1262e59058c4SJames Smart  * command queue such that they shall be processed orderly as HBA can process
1263e59058c4SJames Smart  * one mailbox command at a time.
1264e59058c4SJames Smart  **/
126541415862SJamie Wellnitz void
1266dea3101eS lpfc_mbox_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
1267dea3101eS {
1268dea3101eS 	struct lpfc_sli *psli;
1269dea3101eS 
1270dea3101eS 	psli = &phba->sli;
1271dea3101eS 
1272dea3101eS 	list_add_tail(&mbq->list, &psli->mboxq);
1273dea3101eS 
1274dea3101eS 	psli->mboxq_cnt++;
1275dea3101eS 
1276dea3101eS 	return;
1277dea3101eS }
1278dea3101eS 
1279e59058c4SJames Smart /**
1280e59058c4SJames Smart  * lpfc_mbox_get: Remove a mailbox cmd from the head of driver's mailbox queue.
1281e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
1282e59058c4SJames Smart  *
1283e59058c4SJames Smart  * Driver maintains a internal mailbox command queue implemented as a linked
1284e59058c4SJames Smart  * list. When a mailbox command is issued, it shall be put into the mailbox
1285e59058c4SJames Smart  * command queue such that they shall be processed orderly as HBA can process
1286e59058c4SJames Smart  * one mailbox command at a time. After HBA finished processing a mailbox
1287e59058c4SJames Smart  * command, the driver will remove a pending mailbox command from the head of
1288e59058c4SJames Smart  * the mailbox command queue and send to the HBA for processing.
1289e59058c4SJames Smart  *
1290e59058c4SJames Smart  * Return codes
1291e59058c4SJames Smart  *    pointer to the driver internal queue element for mailbox command.
1292e59058c4SJames Smart  **/
1293dea3101eS LPFC_MBOXQ_t *
1294dea3101eS lpfc_mbox_get(struct lpfc_hba * phba)
1295dea3101eS {
1296dea3101eS 	LPFC_MBOXQ_t *mbq = NULL;
1297dea3101eS 	struct lpfc_sli *psli = &phba->sli;
1298dea3101eS 
12992e0fef85SJames Smart 	list_remove_head((&psli->mboxq), mbq, LPFC_MBOXQ_t, list);
130092d7f7b0SJames Smart 	if (mbq)
1301dea3101eS 		psli->mboxq_cnt--;
1302dea3101eS 
1303dea3101eS 	return mbq;
1304dea3101eS }
1305a309a6b6SJames Smart 
1306e59058c4SJames Smart /**
1307e59058c4SJames Smart  * lpfc_mbox_cmpl_put: Put mailbox command into mailbox command complete list.
1308e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
1309e59058c4SJames Smart  * @mbq: pointer to the driver internal queue element for mailbox command.
1310e59058c4SJames Smart  *
1311e59058c4SJames Smart  * This routine put the completed mailbox command into the mailbox command
1312e59058c4SJames Smart  * complete list. This routine is called from driver interrupt handler
1313e59058c4SJames Smart  * context.The mailbox complete list is used by the driver worker thread
1314e59058c4SJames Smart  * to process mailbox complete callback functions outside the driver interrupt
1315e59058c4SJames Smart  * handler.
1316e59058c4SJames Smart  **/
131792d7f7b0SJames Smart void
131892d7f7b0SJames Smart lpfc_mbox_cmpl_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
131992d7f7b0SJames Smart {
13205b75da2fSJames Smart 	unsigned long iflag;
13215b75da2fSJames Smart 
1322b1c11812SJoe Perches 	/* This function expects to be called from interrupt context */
13235b75da2fSJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
132492d7f7b0SJames Smart 	list_add_tail(&mbq->list, &phba->sli.mboxq_cmpl);
13255b75da2fSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
132692d7f7b0SJames Smart 	return;
132792d7f7b0SJames Smart }
132892d7f7b0SJames Smart 
1329e59058c4SJames Smart /**
1330e59058c4SJames Smart  * lpfc_mbox_tmo_val: Retrieve mailbox command timeout value.
1331e59058c4SJames Smart  * @phba: pointer to lpfc hba data structure.
1332e59058c4SJames Smart  * @cmd: mailbox command code.
1333e59058c4SJames Smart  *
1334e59058c4SJames Smart  * This routine retrieves the proper timeout value according to the mailbox
1335e59058c4SJames Smart  * command code.
1336e59058c4SJames Smart  *
1337e59058c4SJames Smart  * Return codes
1338e59058c4SJames Smart  *    Timeout value to be used for the given mailbox command
1339e59058c4SJames Smart  **/
1340a309a6b6SJames Smart int
1341a309a6b6SJames Smart lpfc_mbox_tmo_val(struct lpfc_hba *phba, int cmd)
1342a309a6b6SJames Smart {
1343a309a6b6SJames Smart 	switch (cmd) {
1344a309a6b6SJames Smart 	case MBX_WRITE_NV:	/* 0x03 */
1345a309a6b6SJames Smart 	case MBX_UPDATE_CFG:	/* 0x1B */
1346a309a6b6SJames Smart 	case MBX_DOWN_LOAD:	/* 0x1C */
1347a309a6b6SJames Smart 	case MBX_DEL_LD_ENTRY:	/* 0x1D */
1348a309a6b6SJames Smart 	case MBX_LOAD_AREA:	/* 0x81 */
134909372820SJames Smart 	case MBX_WRITE_WWN:     /* 0x98 */
1350a309a6b6SJames Smart 	case MBX_LOAD_EXP_ROM:	/* 0x9C */
1351a309a6b6SJames Smart 		return LPFC_MBOX_TMO_FLASH_CMD;
1352a309a6b6SJames Smart 	}
1353a309a6b6SJames Smart 	return LPFC_MBOX_TMO;
1354a309a6b6SJames Smart }
1355