xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_mbox.c (revision 8fa5723aa7e053d498336b48448b292fc2e0458b)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2008 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/pci.h>
24 #include <linux/interrupt.h>
25 
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_transport_fc.h>
28 
29 #include <scsi/scsi.h>
30 
31 #include "lpfc_hw.h"
32 #include "lpfc_sli.h"
33 #include "lpfc_nl.h"
34 #include "lpfc_disc.h"
35 #include "lpfc_scsi.h"
36 #include "lpfc.h"
37 #include "lpfc_logmsg.h"
38 #include "lpfc_crtn.h"
39 #include "lpfc_compat.h"
40 
41 /**
42  * lpfc_dump_mem: Prepare a mailbox command for retrieving HBA's VPD memory.
43  * @phba: pointer to lpfc hba data structure.
44  * @pmb: pointer to the driver internal queue element for mailbox command.
45  * @offset: offset for dumping VPD memory mailbox command.
46  *
47  * The dump mailbox command provides a method for the device driver to obtain
48  * various types of information from the HBA device.
49  *
50  * This routine prepares the mailbox command for dumping HBA Vital Product
51  * Data (VPD) memory. This mailbox command is to be used for retrieving a
52  * portion (DMP_RSP_SIZE bytes) of a HBA's VPD from the HBA at an address
53  * offset specified by the offset parameter.
54  **/
55 void
56 lpfc_dump_mem(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb, uint16_t offset)
57 {
58 	MAILBOX_t *mb;
59 	void *ctx;
60 
61 	mb = &pmb->mb;
62 	ctx = pmb->context2;
63 
64 	/* Setup to dump VPD region */
65 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
66 	mb->mbxCommand = MBX_DUMP_MEMORY;
67 	mb->un.varDmp.cv = 1;
68 	mb->un.varDmp.type = DMP_NV_PARAMS;
69 	mb->un.varDmp.entry_index = offset;
70 	mb->un.varDmp.region_id = DMP_REGION_VPD;
71 	mb->un.varDmp.word_cnt = (DMP_RSP_SIZE / sizeof (uint32_t));
72 	mb->un.varDmp.co = 0;
73 	mb->un.varDmp.resp_offset = 0;
74 	pmb->context2 = ctx;
75 	mb->mbxOwner = OWN_HOST;
76 	return;
77 }
78 
79 /**
80  * lpfc_read_nv: Prepare a mailbox command for reading HBA's NVRAM param.
81  * @phba: pointer to lpfc hba data structure.
82  * @pmb: pointer to the driver internal queue element for mailbox command.
83  *
84  * The read NVRAM mailbox command returns the HBA's non-volatile parameters
85  * that are used as defaults when the Fibre Channel link is brought on-line.
86  *
87  * This routine prepares the mailbox command for reading information stored
88  * in the HBA's NVRAM. Specifically, the HBA's WWNN and WWPN.
89  **/
90 void
91 lpfc_read_nv(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
92 {
93 	MAILBOX_t *mb;
94 
95 	mb = &pmb->mb;
96 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
97 	mb->mbxCommand = MBX_READ_NV;
98 	mb->mbxOwner = OWN_HOST;
99 	return;
100 }
101 
102 /**
103  * lpfc_config_async: Prepare a mailbox command for enabling HBA async event.
104  * @phba: pointer to lpfc hba data structure.
105  * @pmb: pointer to the driver internal queue element for mailbox command.
106  * @ring: ring number for the asynchronous event to be configured.
107  *
108  * The asynchronous event enable mailbox command is used to enable the
109  * asynchronous event posting via the ASYNC_STATUS_CN IOCB response and
110  * specifies the default ring to which events are posted.
111  *
112  * This routine prepares the mailbox command for enabling HBA asynchronous
113  * event support on a IOCB ring.
114  **/
115 void
116 lpfc_config_async(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb,
117 		uint32_t ring)
118 {
119 	MAILBOX_t *mb;
120 
121 	mb = &pmb->mb;
122 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
123 	mb->mbxCommand = MBX_ASYNCEVT_ENABLE;
124 	mb->un.varCfgAsyncEvent.ring = ring;
125 	mb->mbxOwner = OWN_HOST;
126 	return;
127 }
128 
129 /**
130  * lpfc_heart_beat: Prepare a mailbox command for heart beat.
131  * @phba: pointer to lpfc hba data structure.
132  * @pmb: pointer to the driver internal queue element for mailbox command.
133  *
134  * The heart beat mailbox command is used to detect an unresponsive HBA, which
135  * is defined as any device where no error attention is sent and both mailbox
136  * and rings are not processed.
137  *
138  * This routine prepares the mailbox command for issuing a heart beat in the
139  * form of mailbox command to the HBA. The timely completion of the heart
140  * beat mailbox command indicates the health of the HBA.
141  **/
142 void
143 lpfc_heart_beat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
144 {
145 	MAILBOX_t *mb;
146 
147 	mb = &pmb->mb;
148 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
149 	mb->mbxCommand = MBX_HEARTBEAT;
150 	mb->mbxOwner = OWN_HOST;
151 	return;
152 }
153 
154 /**
155  * lpfc_read_la: Prepare a mailbox command for reading HBA link attention.
156  * @phba: pointer to lpfc hba data structure.
157  * @pmb: pointer to the driver internal queue element for mailbox command.
158  * @mp: DMA buffer memory for reading the link attention information into.
159  *
160  * The read link attention mailbox command is issued to read the Link Event
161  * Attention information indicated by the HBA port when the Link Event bit
162  * of the Host Attention (HSTATT) register is set to 1. A Link Event
163  * Attention occurs based on an exception detected at the Fibre Channel link
164  * interface.
165  *
166  * This routine prepares the mailbox command for reading HBA link attention
167  * information. A DMA memory has been set aside and address passed to the
168  * HBA through @mp for the HBA to DMA link attention information into the
169  * memory as part of the execution of the mailbox command.
170  *
171  * Return codes
172  *    0 - Success (currently always return 0)
173  **/
174 int
175 lpfc_read_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb, struct lpfc_dmabuf *mp)
176 {
177 	MAILBOX_t *mb;
178 	struct lpfc_sli *psli;
179 
180 	psli = &phba->sli;
181 	mb = &pmb->mb;
182 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
183 
184 	INIT_LIST_HEAD(&mp->list);
185 	mb->mbxCommand = MBX_READ_LA64;
186 	mb->un.varReadLA.un.lilpBde64.tus.f.bdeSize = 128;
187 	mb->un.varReadLA.un.lilpBde64.addrHigh = putPaddrHigh(mp->phys);
188 	mb->un.varReadLA.un.lilpBde64.addrLow = putPaddrLow(mp->phys);
189 
190 	/* Save address for later completion and set the owner to host so that
191 	 * the FW knows this mailbox is available for processing.
192 	 */
193 	pmb->context1 = (uint8_t *) mp;
194 	mb->mbxOwner = OWN_HOST;
195 	return (0);
196 }
197 
198 /**
199  * lpfc_clear_la: Prepare a mailbox command for clearing HBA link attention.
200  * @phba: pointer to lpfc hba data structure.
201  * @pmb: pointer to the driver internal queue element for mailbox command.
202  *
203  * The clear link attention mailbox command is issued to clear the link event
204  * attention condition indicated by the Link Event bit of the Host Attention
205  * (HSTATT) register. The link event attention condition is cleared only if
206  * the event tag specified matches that of the current link event counter.
207  * The current event tag is read using the read link attention event mailbox
208  * command.
209  *
210  * This routine prepares the mailbox command for clearing HBA link attention
211  * information.
212  **/
213 void
214 lpfc_clear_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
215 {
216 	MAILBOX_t *mb;
217 
218 	mb = &pmb->mb;
219 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
220 
221 	mb->un.varClearLA.eventTag = phba->fc_eventTag;
222 	mb->mbxCommand = MBX_CLEAR_LA;
223 	mb->mbxOwner = OWN_HOST;
224 	return;
225 }
226 
227 /**
228  * lpfc_config_link: Prepare a mailbox command for configuring link on a HBA.
229  * @phba: pointer to lpfc hba data structure.
230  * @pmb: pointer to the driver internal queue element for mailbox command.
231  *
232  * The configure link mailbox command is used before the initialize link
233  * mailbox command to override default value and to configure link-oriented
234  * parameters such as DID address and various timers. Typically, this
235  * command would be used after an F_Port login to set the returned DID address
236  * and the fabric timeout values. This command is not valid before a configure
237  * port command has configured the HBA port.
238  *
239  * This routine prepares the mailbox command for configuring link on a HBA.
240  **/
241 void
242 lpfc_config_link(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
243 {
244 	struct lpfc_vport  *vport = phba->pport;
245 	MAILBOX_t *mb = &pmb->mb;
246 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
247 
248 	/* NEW_FEATURE
249 	 * SLI-2, Coalescing Response Feature.
250 	 */
251 	if (phba->cfg_cr_delay) {
252 		mb->un.varCfgLnk.cr = 1;
253 		mb->un.varCfgLnk.ci = 1;
254 		mb->un.varCfgLnk.cr_delay = phba->cfg_cr_delay;
255 		mb->un.varCfgLnk.cr_count = phba->cfg_cr_count;
256 	}
257 
258 	mb->un.varCfgLnk.myId = vport->fc_myDID;
259 	mb->un.varCfgLnk.edtov = phba->fc_edtov;
260 	mb->un.varCfgLnk.arbtov = phba->fc_arbtov;
261 	mb->un.varCfgLnk.ratov = phba->fc_ratov;
262 	mb->un.varCfgLnk.rttov = phba->fc_rttov;
263 	mb->un.varCfgLnk.altov = phba->fc_altov;
264 	mb->un.varCfgLnk.crtov = phba->fc_crtov;
265 	mb->un.varCfgLnk.citov = phba->fc_citov;
266 
267 	if (phba->cfg_ack0)
268 		mb->un.varCfgLnk.ack0_enable = 1;
269 
270 	mb->mbxCommand = MBX_CONFIG_LINK;
271 	mb->mbxOwner = OWN_HOST;
272 	return;
273 }
274 
275 /**
276  * lpfc_config_msi: Prepare a mailbox command for configuring msi-x.
277  * @phba: pointer to lpfc hba data structure.
278  * @pmb: pointer to the driver internal queue element for mailbox command.
279  *
280  * The configure MSI-X mailbox command is used to configure the HBA's SLI-3
281  * MSI-X multi-message interrupt vector association to interrupt attention
282  * conditions.
283  *
284  * Return codes
285  *    0 - Success
286  *    -EINVAL - Failure
287  **/
288 int
289 lpfc_config_msi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
290 {
291 	MAILBOX_t *mb = &pmb->mb;
292 	uint32_t attentionConditions[2];
293 
294 	/* Sanity check */
295 	if (phba->cfg_use_msi != 2) {
296 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
297 				"0475 Not configured for supporting MSI-X "
298 				"cfg_use_msi: 0x%x\n", phba->cfg_use_msi);
299 		return -EINVAL;
300 	}
301 
302 	if (phba->sli_rev < 3) {
303 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
304 				"0476 HBA not supporting SLI-3 or later "
305 				"SLI Revision: 0x%x\n", phba->sli_rev);
306 		return -EINVAL;
307 	}
308 
309 	/* Clear mailbox command fields */
310 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
311 
312 	/*
313 	 * SLI-3, Message Signaled Interrupt Fearure.
314 	 */
315 
316 	/* Multi-message attention configuration */
317 	attentionConditions[0] = (HA_R0ATT | HA_R1ATT | HA_R2ATT | HA_ERATT |
318 				  HA_LATT | HA_MBATT);
319 	attentionConditions[1] = 0;
320 
321 	mb->un.varCfgMSI.attentionConditions[0] = attentionConditions[0];
322 	mb->un.varCfgMSI.attentionConditions[1] = attentionConditions[1];
323 
324 	/*
325 	 * Set up message number to HA bit association
326 	 */
327 #ifdef __BIG_ENDIAN_BITFIELD
328 	/* RA0 (FCP Ring) */
329 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS] = 1;
330 	/* RA1 (Other Protocol Extra Ring) */
331 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS] = 1;
332 #else   /*  __LITTLE_ENDIAN_BITFIELD */
333 	/* RA0 (FCP Ring) */
334 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS^3] = 1;
335 	/* RA1 (Other Protocol Extra Ring) */
336 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS^3] = 1;
337 #endif
338 	/* Multi-message interrupt autoclear configuration*/
339 	mb->un.varCfgMSI.autoClearHA[0] = attentionConditions[0];
340 	mb->un.varCfgMSI.autoClearHA[1] = attentionConditions[1];
341 
342 	/* For now, HBA autoclear does not work reliably, disable it */
343 	mb->un.varCfgMSI.autoClearHA[0] = 0;
344 	mb->un.varCfgMSI.autoClearHA[1] = 0;
345 
346 	/* Set command and owner bit */
347 	mb->mbxCommand = MBX_CONFIG_MSI;
348 	mb->mbxOwner = OWN_HOST;
349 
350 	return 0;
351 }
352 
353 /**
354  * lpfc_init_link: Prepare a mailbox command for initialize link on a HBA.
355  * @phba: pointer to lpfc hba data structure.
356  * @pmb: pointer to the driver internal queue element for mailbox command.
357  * @topology: the link topology for the link to be initialized to.
358  * @linkspeed: the link speed for the link to be initialized to.
359  *
360  * The initialize link mailbox command is used to initialize the Fibre
361  * Channel link. This command must follow a configure port command that
362  * establishes the mode of operation.
363  *
364  * This routine prepares the mailbox command for initializing link on a HBA
365  * with the specified link topology and speed.
366  **/
367 void
368 lpfc_init_link(struct lpfc_hba * phba,
369 	       LPFC_MBOXQ_t * pmb, uint32_t topology, uint32_t linkspeed)
370 {
371 	lpfc_vpd_t *vpd;
372 	struct lpfc_sli *psli;
373 	MAILBOX_t *mb;
374 
375 	mb = &pmb->mb;
376 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
377 
378 	psli = &phba->sli;
379 	switch (topology) {
380 	case FLAGS_TOPOLOGY_MODE_LOOP_PT:
381 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
382 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
383 		break;
384 	case FLAGS_TOPOLOGY_MODE_PT_PT:
385 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
386 		break;
387 	case FLAGS_TOPOLOGY_MODE_LOOP:
388 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
389 		break;
390 	case FLAGS_TOPOLOGY_MODE_PT_LOOP:
391 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
392 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
393 		break;
394 	case FLAGS_LOCAL_LB:
395 		mb->un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
396 		break;
397 	}
398 
399 	/* Enable asynchronous ABTS responses from firmware */
400 	mb->un.varInitLnk.link_flags |= FLAGS_IMED_ABORT;
401 
402 	/* NEW_FEATURE
403 	 * Setting up the link speed
404 	 */
405 	vpd = &phba->vpd;
406 	if (vpd->rev.feaLevelHigh >= 0x02){
407 		switch(linkspeed){
408 			case LINK_SPEED_1G:
409 			case LINK_SPEED_2G:
410 			case LINK_SPEED_4G:
411 			case LINK_SPEED_8G:
412 				mb->un.varInitLnk.link_flags |=
413 							FLAGS_LINK_SPEED;
414 				mb->un.varInitLnk.link_speed = linkspeed;
415 			break;
416 			case LINK_SPEED_AUTO:
417 			default:
418 				mb->un.varInitLnk.link_speed =
419 							LINK_SPEED_AUTO;
420 			break;
421 		}
422 
423 	}
424 	else
425 		mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
426 
427 	mb->mbxCommand = (volatile uint8_t)MBX_INIT_LINK;
428 	mb->mbxOwner = OWN_HOST;
429 	mb->un.varInitLnk.fabric_AL_PA = phba->fc_pref_ALPA;
430 	return;
431 }
432 
433 /**
434  * lpfc_read_sparam: Prepare a mailbox command for reading HBA parameters.
435  * @phba: pointer to lpfc hba data structure.
436  * @pmb: pointer to the driver internal queue element for mailbox command.
437  * @vpi: virtual N_Port identifier.
438  *
439  * The read service parameter mailbox command is used to read the HBA port
440  * service parameters. The service parameters are read into the buffer
441  * specified directly by a BDE in the mailbox command. These service
442  * parameters may then be used to build the payload of an N_Port/F_POrt
443  * login request and reply (LOGI/ACC).
444  *
445  * This routine prepares the mailbox command for reading HBA port service
446  * parameters. The DMA memory is allocated in this function and the addresses
447  * are populated into the mailbox command for the HBA to DMA the service
448  * parameters into.
449  *
450  * Return codes
451  *    0 - Success
452  *    1 - DMA memory allocation failed
453  **/
454 int
455 lpfc_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, int vpi)
456 {
457 	struct lpfc_dmabuf *mp;
458 	MAILBOX_t *mb;
459 	struct lpfc_sli *psli;
460 
461 	psli = &phba->sli;
462 	mb = &pmb->mb;
463 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
464 
465 	mb->mbxOwner = OWN_HOST;
466 
467 	/* Get a buffer to hold the HBAs Service Parameters */
468 
469 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
470 	if (mp)
471 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
472 	if (!mp || !mp->virt) {
473 		kfree(mp);
474 		mb->mbxCommand = MBX_READ_SPARM64;
475 		/* READ_SPARAM: no buffers */
476 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
477 			        "0301 READ_SPARAM: no buffers\n");
478 		return (1);
479 	}
480 	INIT_LIST_HEAD(&mp->list);
481 	mb->mbxCommand = MBX_READ_SPARM64;
482 	mb->un.varRdSparm.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
483 	mb->un.varRdSparm.un.sp64.addrHigh = putPaddrHigh(mp->phys);
484 	mb->un.varRdSparm.un.sp64.addrLow = putPaddrLow(mp->phys);
485 	mb->un.varRdSparm.vpi = vpi;
486 
487 	/* save address for completion */
488 	pmb->context1 = mp;
489 
490 	return (0);
491 }
492 
493 /**
494  * lpfc_unreg_did: Prepare a mailbox command for unregistering DID.
495  * @phba: pointer to lpfc hba data structure.
496  * @vpi: virtual N_Port identifier.
497  * @did: remote port identifier.
498  * @pmb: pointer to the driver internal queue element for mailbox command.
499  *
500  * The unregister DID mailbox command is used to unregister an N_Port/F_Port
501  * login for an unknown RPI by specifying the DID of a remote port. This
502  * command frees an RPI context in the HBA port. This has the effect of
503  * performing an implicit N_Port/F_Port logout.
504  *
505  * This routine prepares the mailbox command for unregistering a remote
506  * N_Port/F_Port (DID) login.
507  **/
508 void
509 lpfc_unreg_did(struct lpfc_hba * phba, uint16_t vpi, uint32_t did,
510 	       LPFC_MBOXQ_t * pmb)
511 {
512 	MAILBOX_t *mb;
513 
514 	mb = &pmb->mb;
515 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
516 
517 	mb->un.varUnregDID.did = did;
518 	mb->un.varUnregDID.vpi = vpi;
519 
520 	mb->mbxCommand = MBX_UNREG_D_ID;
521 	mb->mbxOwner = OWN_HOST;
522 	return;
523 }
524 
525 /**
526  * lpfc_read_config: Prepare a mailbox command for reading HBA configuration.
527  * @phba: pointer to lpfc hba data structure.
528  * @pmb: pointer to the driver internal queue element for mailbox command.
529  *
530  * The read configuration mailbox command is used to read the HBA port
531  * configuration parameters. This mailbox command provides a method for
532  * seeing any parameters that may have changed via various configuration
533  * mailbox commands.
534  *
535  * This routine prepares the mailbox command for reading out HBA configuration
536  * parameters.
537  **/
538 void
539 lpfc_read_config(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
540 {
541 	MAILBOX_t *mb;
542 
543 	mb = &pmb->mb;
544 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
545 
546 	mb->mbxCommand = MBX_READ_CONFIG;
547 	mb->mbxOwner = OWN_HOST;
548 	return;
549 }
550 
551 /**
552  * lpfc_read_lnk_stat: Prepare a mailbox command for reading HBA link stats.
553  * @phba: pointer to lpfc hba data structure.
554  * @pmb: pointer to the driver internal queue element for mailbox command.
555  *
556  * The read link status mailbox command is used to read the link status from
557  * the HBA. Link status includes all link-related error counters. These
558  * counters are maintained by the HBA and originated in the link hardware
559  * unit. Note that all of these counters wrap.
560  *
561  * This routine prepares the mailbox command for reading out HBA link status.
562  **/
563 void
564 lpfc_read_lnk_stat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
565 {
566 	MAILBOX_t *mb;
567 
568 	mb = &pmb->mb;
569 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
570 
571 	mb->mbxCommand = MBX_READ_LNK_STAT;
572 	mb->mbxOwner = OWN_HOST;
573 	return;
574 }
575 
576 /**
577  * lpfc_reg_login: Prepare a mailbox command for registering remote login.
578  * @phba: pointer to lpfc hba data structure.
579  * @vpi: virtual N_Port identifier.
580  * @did: remote port identifier.
581  * @param: pointer to memory holding the server parameters.
582  * @pmb: pointer to the driver internal queue element for mailbox command.
583  * @flag: action flag to be passed back for the complete function.
584  *
585  * The registration login mailbox command is used to register an N_Port or
586  * F_Port login. This registration allows the HBA to cache the remote N_Port
587  * service parameters internally and thereby make the appropriate FC-2
588  * decisions. The remote port service parameters are handed off by the driver
589  * to the HBA using a descriptor entry that directly identifies a buffer in
590  * host memory. In exchange, the HBA returns an RPI identifier.
591  *
592  * This routine prepares the mailbox command for registering remote port login.
593  * The function allocates DMA buffer for passing the service parameters to the
594  * HBA with the mailbox command.
595  *
596  * Return codes
597  *    0 - Success
598  *    1 - DMA memory allocation failed
599  **/
600 int
601 lpfc_reg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t did,
602 	       uint8_t *param, LPFC_MBOXQ_t *pmb, uint32_t flag)
603 {
604 	MAILBOX_t *mb = &pmb->mb;
605 	uint8_t *sparam;
606 	struct lpfc_dmabuf *mp;
607 
608 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
609 
610 	mb->un.varRegLogin.rpi = 0;
611 	mb->un.varRegLogin.vpi = vpi;
612 	mb->un.varRegLogin.did = did;
613 	mb->un.varWords[30] = flag;	/* Set flag to issue action on cmpl */
614 
615 	mb->mbxOwner = OWN_HOST;
616 
617 	/* Get a buffer to hold NPorts Service Parameters */
618 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
619 	if (mp)
620 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
621 	if (!mp || !mp->virt) {
622 		kfree(mp);
623 		mb->mbxCommand = MBX_REG_LOGIN64;
624 		/* REG_LOGIN: no buffers */
625 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
626 				"0302 REG_LOGIN: no buffers, VPI:%d DID:x%x, "
627 				"flag x%x\n", vpi, did, flag);
628 		return (1);
629 	}
630 	INIT_LIST_HEAD(&mp->list);
631 	sparam = mp->virt;
632 
633 	/* Copy param's into a new buffer */
634 	memcpy(sparam, param, sizeof (struct serv_parm));
635 
636 	/* save address for completion */
637 	pmb->context1 = (uint8_t *) mp;
638 
639 	mb->mbxCommand = MBX_REG_LOGIN64;
640 	mb->un.varRegLogin.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
641 	mb->un.varRegLogin.un.sp64.addrHigh = putPaddrHigh(mp->phys);
642 	mb->un.varRegLogin.un.sp64.addrLow = putPaddrLow(mp->phys);
643 
644 	return (0);
645 }
646 
647 /**
648  * lpfc_unreg_login: Prepare a mailbox command for unregistering remote login.
649  * @phba: pointer to lpfc hba data structure.
650  * @vpi: virtual N_Port identifier.
651  * @rpi: remote port identifier
652  * @pmb: pointer to the driver internal queue element for mailbox command.
653  *
654  * The unregistration login mailbox command is used to unregister an N_Port
655  * or F_Port login. This command frees an RPI context in the HBA. It has the
656  * effect of performing an implicit N_Port/F_Port logout.
657  *
658  * This routine prepares the mailbox command for unregistering remote port
659  * login.
660  **/
661 void
662 lpfc_unreg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t rpi,
663 		 LPFC_MBOXQ_t * pmb)
664 {
665 	MAILBOX_t *mb;
666 
667 	mb = &pmb->mb;
668 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
669 
670 	mb->un.varUnregLogin.rpi = (uint16_t) rpi;
671 	mb->un.varUnregLogin.rsvd1 = 0;
672 	mb->un.varUnregLogin.vpi = vpi;
673 
674 	mb->mbxCommand = MBX_UNREG_LOGIN;
675 	mb->mbxOwner = OWN_HOST;
676 	return;
677 }
678 
679 /**
680  * lpfc_reg_vpi: Prepare a mailbox command for registering vport identifier.
681  * @phba: pointer to lpfc hba data structure.
682  * @vpi: virtual N_Port identifier.
683  * @sid: Fibre Channel S_ID (N_Port_ID assigned to a virtual N_Port).
684  * @pmb: pointer to the driver internal queue element for mailbox command.
685  *
686  * The registration vport identifier mailbox command is used to activate a
687  * virtual N_Port after it has acquired an N_Port_ID. The HBA validates the
688  * N_Port_ID against the information in the selected virtual N_Port context
689  * block and marks it active to allow normal processing of IOCB commands and
690  * received unsolicited exchanges.
691  *
692  * This routine prepares the mailbox command for registering a virtual N_Port.
693  **/
694 void
695 lpfc_reg_vpi(struct lpfc_hba *phba, uint16_t vpi, uint32_t sid,
696 	     LPFC_MBOXQ_t *pmb)
697 {
698 	MAILBOX_t *mb = &pmb->mb;
699 
700 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
701 
702 	mb->un.varRegVpi.vpi = vpi;
703 	mb->un.varRegVpi.sid = sid;
704 
705 	mb->mbxCommand = MBX_REG_VPI;
706 	mb->mbxOwner = OWN_HOST;
707 	return;
708 
709 }
710 
711 /**
712  * lpfc_unreg_vpi: Prepare a mailbox command for unregistering vport id.
713  * @phba: pointer to lpfc hba data structure.
714  * @vpi: virtual N_Port identifier.
715  * @pmb: pointer to the driver internal queue element for mailbox command.
716  *
717  * The unregistration vport identifier mailbox command is used to inactivate
718  * a virtual N_Port. The driver must have logged out and unregistered all
719  * remote N_Ports to abort any activity on the virtual N_Port. The HBA will
720  * unregisters any default RPIs associated with the specified vpi, aborting
721  * any active exchanges. The HBA will post the mailbox response after making
722  * the virtual N_Port inactive.
723  *
724  * This routine prepares the mailbox command for unregistering a virtual
725  * N_Port.
726  **/
727 void
728 lpfc_unreg_vpi(struct lpfc_hba *phba, uint16_t vpi, LPFC_MBOXQ_t *pmb)
729 {
730 	MAILBOX_t *mb = &pmb->mb;
731 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
732 
733 	mb->un.varUnregVpi.vpi = vpi;
734 
735 	mb->mbxCommand = MBX_UNREG_VPI;
736 	mb->mbxOwner = OWN_HOST;
737 	return;
738 
739 }
740 
741 /**
742  * lpfc_config_pcb_setup: Set up IOCB rings in the Port Control Block (PCB)
743  * @phba: pointer to lpfc hba data structure.
744  *
745  * This routine sets up and initializes the IOCB rings in the Port Control
746  * Block (PCB).
747  **/
748 static void
749 lpfc_config_pcb_setup(struct lpfc_hba * phba)
750 {
751 	struct lpfc_sli *psli = &phba->sli;
752 	struct lpfc_sli_ring *pring;
753 	PCB_t *pcbp = phba->pcb;
754 	dma_addr_t pdma_addr;
755 	uint32_t offset;
756 	uint32_t iocbCnt = 0;
757 	int i;
758 
759 	pcbp->maxRing = (psli->num_rings - 1);
760 
761 	for (i = 0; i < psli->num_rings; i++) {
762 		pring = &psli->ring[i];
763 
764 		pring->sizeCiocb = phba->sli_rev == 3 ? SLI3_IOCB_CMD_SIZE:
765 							SLI2_IOCB_CMD_SIZE;
766 		pring->sizeRiocb = phba->sli_rev == 3 ? SLI3_IOCB_RSP_SIZE:
767 							SLI2_IOCB_RSP_SIZE;
768 		/* A ring MUST have both cmd and rsp entries defined to be
769 		   valid */
770 		if ((pring->numCiocb == 0) || (pring->numRiocb == 0)) {
771 			pcbp->rdsc[i].cmdEntries = 0;
772 			pcbp->rdsc[i].rspEntries = 0;
773 			pcbp->rdsc[i].cmdAddrHigh = 0;
774 			pcbp->rdsc[i].rspAddrHigh = 0;
775 			pcbp->rdsc[i].cmdAddrLow = 0;
776 			pcbp->rdsc[i].rspAddrLow = 0;
777 			pring->cmdringaddr = NULL;
778 			pring->rspringaddr = NULL;
779 			continue;
780 		}
781 		/* Command ring setup for ring */
782 		pring->cmdringaddr = (void *)&phba->IOCBs[iocbCnt];
783 		pcbp->rdsc[i].cmdEntries = pring->numCiocb;
784 
785 		offset = (uint8_t *) &phba->IOCBs[iocbCnt] -
786 			 (uint8_t *) phba->slim2p.virt;
787 		pdma_addr = phba->slim2p.phys + offset;
788 		pcbp->rdsc[i].cmdAddrHigh = putPaddrHigh(pdma_addr);
789 		pcbp->rdsc[i].cmdAddrLow = putPaddrLow(pdma_addr);
790 		iocbCnt += pring->numCiocb;
791 
792 		/* Response ring setup for ring */
793 		pring->rspringaddr = (void *) &phba->IOCBs[iocbCnt];
794 
795 		pcbp->rdsc[i].rspEntries = pring->numRiocb;
796 		offset = (uint8_t *)&phba->IOCBs[iocbCnt] -
797 			 (uint8_t *)phba->slim2p.virt;
798 		pdma_addr = phba->slim2p.phys + offset;
799 		pcbp->rdsc[i].rspAddrHigh = putPaddrHigh(pdma_addr);
800 		pcbp->rdsc[i].rspAddrLow = putPaddrLow(pdma_addr);
801 		iocbCnt += pring->numRiocb;
802 	}
803 }
804 
805 /**
806  * lpfc_read_rev: Prepare a mailbox command for reading HBA revision.
807  * @phba: pointer to lpfc hba data structure.
808  * @pmb: pointer to the driver internal queue element for mailbox command.
809  *
810  * The read revision mailbox command is used to read the revision levels of
811  * the HBA components. These components include hardware units, resident
812  * firmware, and available firmware. HBAs that supports SLI-3 mode of
813  * operation provide different response information depending on the version
814  * requested by the driver.
815  *
816  * This routine prepares the mailbox command for reading HBA revision
817  * information.
818  **/
819 void
820 lpfc_read_rev(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
821 {
822 	MAILBOX_t *mb = &pmb->mb;
823 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
824 	mb->un.varRdRev.cv = 1;
825 	mb->un.varRdRev.v3req = 1; /* Request SLI3 info */
826 	mb->mbxCommand = MBX_READ_REV;
827 	mb->mbxOwner = OWN_HOST;
828 	return;
829 }
830 
831 /**
832  * lpfc_build_hbq_profile2: Set up the HBQ Selection Profile 2.
833  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
834  * @hbq_desc: pointer to the HBQ selection profile descriptor.
835  *
836  * The Host Buffer Queue (HBQ) Selection Profile 2 specifies that the HBA
837  * tests the incoming frames' R_CTL/TYPE fields with works 10:15 and performs
838  * the Sequence Length Test using the fields in the Selection Profile 2
839  * extension in words 20:31.
840  **/
841 static void
842 lpfc_build_hbq_profile2(struct config_hbq_var *hbqmb,
843 			struct lpfc_hbq_init  *hbq_desc)
844 {
845 	hbqmb->profiles.profile2.seqlenbcnt = hbq_desc->seqlenbcnt;
846 	hbqmb->profiles.profile2.maxlen     = hbq_desc->maxlen;
847 	hbqmb->profiles.profile2.seqlenoff  = hbq_desc->seqlenoff;
848 }
849 
850 /**
851  * lpfc_build_hbq_profile3: Set up the HBQ Selection Profile 3.
852  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
853  * @hbq_desc: pointer to the HBQ selection profile descriptor.
854  *
855  * The Host Buffer Queue (HBQ) Selection Profile 3 specifies that the HBA
856  * tests the incoming frame's R_CTL/TYPE fields with words 10:15 and performs
857  * the Sequence Length Test and Byte Field Test using the fields in the
858  * Selection Profile 3 extension in words 20:31.
859  **/
860 static void
861 lpfc_build_hbq_profile3(struct config_hbq_var *hbqmb,
862 			struct lpfc_hbq_init  *hbq_desc)
863 {
864 	hbqmb->profiles.profile3.seqlenbcnt = hbq_desc->seqlenbcnt;
865 	hbqmb->profiles.profile3.maxlen     = hbq_desc->maxlen;
866 	hbqmb->profiles.profile3.cmdcodeoff = hbq_desc->cmdcodeoff;
867 	hbqmb->profiles.profile3.seqlenoff  = hbq_desc->seqlenoff;
868 	memcpy(&hbqmb->profiles.profile3.cmdmatch, hbq_desc->cmdmatch,
869 	       sizeof(hbqmb->profiles.profile3.cmdmatch));
870 }
871 
872 /**
873  * lpfc_build_hbq_profile5: Set up the HBQ Selection Profile 5.
874  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
875  * @hbq_desc: pointer to the HBQ selection profile descriptor.
876  *
877  * The Host Buffer Queue (HBQ) Selection Profile 5 specifies a header HBQ. The
878  * HBA tests the initial frame of an incoming sequence using the frame's
879  * R_CTL/TYPE fields with words 10:15 and performs the Sequence Length Test
880  * and Byte Field Test using the fields in the Selection Profile 5 extension
881  * words 20:31.
882  **/
883 static void
884 lpfc_build_hbq_profile5(struct config_hbq_var *hbqmb,
885 			struct lpfc_hbq_init  *hbq_desc)
886 {
887 	hbqmb->profiles.profile5.seqlenbcnt = hbq_desc->seqlenbcnt;
888 	hbqmb->profiles.profile5.maxlen     = hbq_desc->maxlen;
889 	hbqmb->profiles.profile5.cmdcodeoff = hbq_desc->cmdcodeoff;
890 	hbqmb->profiles.profile5.seqlenoff  = hbq_desc->seqlenoff;
891 	memcpy(&hbqmb->profiles.profile5.cmdmatch, hbq_desc->cmdmatch,
892 	       sizeof(hbqmb->profiles.profile5.cmdmatch));
893 }
894 
895 /**
896  * lpfc_config_hbq: Prepare a mailbox command for configuring an HBQ.
897  * @phba: pointer to lpfc hba data structure.
898  * @id: HBQ identifier.
899  * @hbq_desc: pointer to the HBA descriptor data structure.
900  * @hbq_entry_index: index of the HBQ entry data structures.
901  * @pmb: pointer to the driver internal queue element for mailbox command.
902  *
903  * The configure HBQ (Host Buffer Queue) mailbox command is used to configure
904  * an HBQ. The configuration binds events that require buffers to a particular
905  * ring and HBQ based on a selection profile.
906  *
907  * This routine prepares the mailbox command for configuring an HBQ.
908  **/
909 void
910 lpfc_config_hbq(struct lpfc_hba *phba, uint32_t id,
911 		 struct lpfc_hbq_init *hbq_desc,
912 		uint32_t hbq_entry_index, LPFC_MBOXQ_t *pmb)
913 {
914 	int i;
915 	MAILBOX_t *mb = &pmb->mb;
916 	struct config_hbq_var *hbqmb = &mb->un.varCfgHbq;
917 
918 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
919 	hbqmb->hbqId = id;
920 	hbqmb->entry_count = hbq_desc->entry_count;   /* # entries in HBQ */
921 	hbqmb->recvNotify = hbq_desc->rn;             /* Receive
922 						       * Notification */
923 	hbqmb->numMask    = hbq_desc->mask_count;     /* # R_CTL/TYPE masks
924 						       * # in words 0-19 */
925 	hbqmb->profile    = hbq_desc->profile;	      /* Selection profile:
926 						       * 0 = all,
927 						       * 7 = logentry */
928 	hbqmb->ringMask   = hbq_desc->ring_mask;      /* Binds HBQ to a ring
929 						       * e.g. Ring0=b0001,
930 						       * ring2=b0100 */
931 	hbqmb->headerLen  = hbq_desc->headerLen;      /* 0 if not profile 4
932 						       * or 5 */
933 	hbqmb->logEntry   = hbq_desc->logEntry;       /* Set to 1 if this
934 						       * HBQ will be used
935 						       * for LogEntry
936 						       * buffers */
937 	hbqmb->hbqaddrLow = putPaddrLow(phba->hbqslimp.phys) +
938 		hbq_entry_index * sizeof(struct lpfc_hbq_entry);
939 	hbqmb->hbqaddrHigh = putPaddrHigh(phba->hbqslimp.phys);
940 
941 	mb->mbxCommand = MBX_CONFIG_HBQ;
942 	mb->mbxOwner = OWN_HOST;
943 
944 				/* Copy info for profiles 2,3,5. Other
945 				 * profiles this area is reserved
946 				 */
947 	if (hbq_desc->profile == 2)
948 		lpfc_build_hbq_profile2(hbqmb, hbq_desc);
949 	else if (hbq_desc->profile == 3)
950 		lpfc_build_hbq_profile3(hbqmb, hbq_desc);
951 	else if (hbq_desc->profile == 5)
952 		lpfc_build_hbq_profile5(hbqmb, hbq_desc);
953 
954 	/* Return if no rctl / type masks for this HBQ */
955 	if (!hbq_desc->mask_count)
956 		return;
957 
958 	/* Otherwise we setup specific rctl / type masks for this HBQ */
959 	for (i = 0; i < hbq_desc->mask_count; i++) {
960 		hbqmb->hbqMasks[i].tmatch = hbq_desc->hbqMasks[i].tmatch;
961 		hbqmb->hbqMasks[i].tmask  = hbq_desc->hbqMasks[i].tmask;
962 		hbqmb->hbqMasks[i].rctlmatch = hbq_desc->hbqMasks[i].rctlmatch;
963 		hbqmb->hbqMasks[i].rctlmask  = hbq_desc->hbqMasks[i].rctlmask;
964 	}
965 
966 	return;
967 }
968 
969 /**
970  * lpfc_config_ring: Prepare a mailbox command for configuring an IOCB ring.
971  * @phba: pointer to lpfc hba data structure.
972  * @ring:
973  * @pmb: pointer to the driver internal queue element for mailbox command.
974  *
975  * The configure ring mailbox command is used to configure an IOCB ring. This
976  * configuration binds from one to six of HBA RC_CTL/TYPE mask entries to the
977  * ring. This is used to map incoming sequences to a particular ring whose
978  * RC_CTL/TYPE mask entry matches that of the sequence. The driver should not
979  * attempt to configure a ring whose number is greater than the number
980  * specified in the Port Control Block (PCB). It is an error to issue the
981  * configure ring command more than once with the same ring number. The HBA
982  * returns an error if the driver attempts this.
983  *
984  * This routine prepares the mailbox command for configuring IOCB ring.
985  **/
986 void
987 lpfc_config_ring(struct lpfc_hba * phba, int ring, LPFC_MBOXQ_t * pmb)
988 {
989 	int i;
990 	MAILBOX_t *mb = &pmb->mb;
991 	struct lpfc_sli *psli;
992 	struct lpfc_sli_ring *pring;
993 
994 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
995 
996 	mb->un.varCfgRing.ring = ring;
997 	mb->un.varCfgRing.maxOrigXchg = 0;
998 	mb->un.varCfgRing.maxRespXchg = 0;
999 	mb->un.varCfgRing.recvNotify = 1;
1000 
1001 	psli = &phba->sli;
1002 	pring = &psli->ring[ring];
1003 	mb->un.varCfgRing.numMask = pring->num_mask;
1004 	mb->mbxCommand = MBX_CONFIG_RING;
1005 	mb->mbxOwner = OWN_HOST;
1006 
1007 	/* Is this ring configured for a specific profile */
1008 	if (pring->prt[0].profile) {
1009 		mb->un.varCfgRing.profile = pring->prt[0].profile;
1010 		return;
1011 	}
1012 
1013 	/* Otherwise we setup specific rctl / type masks for this ring */
1014 	for (i = 0; i < pring->num_mask; i++) {
1015 		mb->un.varCfgRing.rrRegs[i].rval = pring->prt[i].rctl;
1016 		if (mb->un.varCfgRing.rrRegs[i].rval != FC_ELS_REQ)
1017 			mb->un.varCfgRing.rrRegs[i].rmask = 0xff;
1018 		else
1019 			mb->un.varCfgRing.rrRegs[i].rmask = 0xfe;
1020 		mb->un.varCfgRing.rrRegs[i].tval = pring->prt[i].type;
1021 		mb->un.varCfgRing.rrRegs[i].tmask = 0xff;
1022 	}
1023 
1024 	return;
1025 }
1026 
1027 /**
1028  * lpfc_config_port: Prepare a mailbox command for configuring port.
1029  * @phba: pointer to lpfc hba data structure.
1030  * @pmb: pointer to the driver internal queue element for mailbox command.
1031  *
1032  * The configure port mailbox command is used to identify the Port Control
1033  * Block (PCB) in the driver memory. After this command is issued, the
1034  * driver must not access the mailbox in the HBA without first resetting
1035  * the HBA. The HBA may copy the PCB information to internal storage for
1036  * subsequent use; the driver can not change the PCB information unless it
1037  * resets the HBA.
1038  *
1039  * This routine prepares the mailbox command for configuring port.
1040  **/
1041 void
1042 lpfc_config_port(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1043 {
1044 	MAILBOX_t __iomem *mb_slim = (MAILBOX_t __iomem *) phba->MBslimaddr;
1045 	MAILBOX_t *mb = &pmb->mb;
1046 	dma_addr_t pdma_addr;
1047 	uint32_t bar_low, bar_high;
1048 	size_t offset;
1049 	struct lpfc_hgp hgp;
1050 	int i;
1051 	uint32_t pgp_offset;
1052 
1053 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1054 	mb->mbxCommand = MBX_CONFIG_PORT;
1055 	mb->mbxOwner = OWN_HOST;
1056 
1057 	mb->un.varCfgPort.pcbLen = sizeof(PCB_t);
1058 
1059 	offset = (uint8_t *)phba->pcb - (uint8_t *)phba->slim2p.virt;
1060 	pdma_addr = phba->slim2p.phys + offset;
1061 	mb->un.varCfgPort.pcbLow = putPaddrLow(pdma_addr);
1062 	mb->un.varCfgPort.pcbHigh = putPaddrHigh(pdma_addr);
1063 
1064 	/* If HBA supports SLI=3 ask for it */
1065 
1066 	if (phba->sli_rev == 3 && phba->vpd.sli3Feat.cerbm) {
1067 		mb->un.varCfgPort.cerbm = 1; /* Request HBQs */
1068 		mb->un.varCfgPort.ccrp = 1; /* Command Ring Polling */
1069 		mb->un.varCfgPort.cinb = 1; /* Interrupt Notification Block */
1070 		mb->un.varCfgPort.max_hbq = lpfc_sli_hbq_count();
1071 		if (phba->max_vpi && phba->cfg_enable_npiv &&
1072 		    phba->vpd.sli3Feat.cmv) {
1073 			mb->un.varCfgPort.max_vpi = phba->max_vpi;
1074 			mb->un.varCfgPort.cmv = 1;
1075 		} else
1076 			mb->un.varCfgPort.max_vpi = phba->max_vpi = 0;
1077 	} else
1078 		phba->sli_rev = 2;
1079 	mb->un.varCfgPort.sli_mode = phba->sli_rev;
1080 
1081 	/* Now setup pcb */
1082 	phba->pcb->type = TYPE_NATIVE_SLI2;
1083 	phba->pcb->feature = FEATURE_INITIAL_SLI2;
1084 
1085 	/* Setup Mailbox pointers */
1086 	phba->pcb->mailBoxSize = sizeof(MAILBOX_t);
1087 	offset = (uint8_t *)phba->mbox - (uint8_t *)phba->slim2p.virt;
1088 	pdma_addr = phba->slim2p.phys + offset;
1089 	phba->pcb->mbAddrHigh = putPaddrHigh(pdma_addr);
1090 	phba->pcb->mbAddrLow = putPaddrLow(pdma_addr);
1091 
1092 	/*
1093 	 * Setup Host Group ring pointer.
1094 	 *
1095 	 * For efficiency reasons, the ring get/put pointers can be
1096 	 * placed in adapter memory (SLIM) rather than in host memory.
1097 	 * This allows firmware to avoid PCI reads/writes when updating
1098 	 * and checking pointers.
1099 	 *
1100 	 * The firmware recognizes the use of SLIM memory by comparing
1101 	 * the address of the get/put pointers structure with that of
1102 	 * the SLIM BAR (BAR0).
1103 	 *
1104 	 * Caution: be sure to use the PCI config space value of BAR0/BAR1
1105 	 * (the hardware's view of the base address), not the OS's
1106 	 * value of pci_resource_start() as the OS value may be a cookie
1107 	 * for ioremap/iomap.
1108 	 */
1109 
1110 
1111 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_0, &bar_low);
1112 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_1, &bar_high);
1113 
1114 	/*
1115 	 * Set up HGP - Port Memory
1116 	 *
1117 	 * The port expects the host get/put pointers to reside in memory
1118 	 * following the "non-diagnostic" mode mailbox (32 words, 0x80 bytes)
1119 	 * area of SLIM.  In SLI-2 mode, there's an additional 16 reserved
1120 	 * words (0x40 bytes).  This area is not reserved if HBQs are
1121 	 * configured in SLI-3.
1122 	 *
1123 	 * CR0Put    - SLI2(no HBQs) = 0xc0, With HBQs = 0x80
1124 	 * RR0Get                      0xc4              0x84
1125 	 * CR1Put                      0xc8              0x88
1126 	 * RR1Get                      0xcc              0x8c
1127 	 * CR2Put                      0xd0              0x90
1128 	 * RR2Get                      0xd4              0x94
1129 	 * CR3Put                      0xd8              0x98
1130 	 * RR3Get                      0xdc              0x9c
1131 	 *
1132 	 * Reserved                    0xa0-0xbf
1133 	 *    If HBQs configured:
1134 	 *                         HBQ 0 Put ptr  0xc0
1135 	 *                         HBQ 1 Put ptr  0xc4
1136 	 *                         HBQ 2 Put ptr  0xc8
1137 	 *                         ......
1138 	 *                         HBQ(M-1)Put Pointer 0xc0+(M-1)*4
1139 	 *
1140 	 */
1141 
1142 	if (phba->sli_rev == 3) {
1143 		phba->host_gp = &mb_slim->us.s3.host[0];
1144 		phba->hbq_put = &mb_slim->us.s3.hbq_put[0];
1145 	} else {
1146 		phba->host_gp = &mb_slim->us.s2.host[0];
1147 		phba->hbq_put = NULL;
1148 	}
1149 
1150 	/* mask off BAR0's flag bits 0 - 3 */
1151 	phba->pcb->hgpAddrLow = (bar_low & PCI_BASE_ADDRESS_MEM_MASK) +
1152 		(void __iomem *)phba->host_gp -
1153 		(void __iomem *)phba->MBslimaddr;
1154 	if (bar_low & PCI_BASE_ADDRESS_MEM_TYPE_64)
1155 		phba->pcb->hgpAddrHigh = bar_high;
1156 	else
1157 		phba->pcb->hgpAddrHigh = 0;
1158 	/* write HGP data to SLIM at the required longword offset */
1159 	memset(&hgp, 0, sizeof(struct lpfc_hgp));
1160 
1161 	for (i=0; i < phba->sli.num_rings; i++) {
1162 		lpfc_memcpy_to_slim(phba->host_gp + i, &hgp,
1163 				    sizeof(*phba->host_gp));
1164 	}
1165 
1166 	/* Setup Port Group ring pointer */
1167 	if (phba->sli3_options & LPFC_SLI3_INB_ENABLED) {
1168 		pgp_offset = offsetof(struct lpfc_sli2_slim,
1169 				      mbx.us.s3_inb_pgp.port);
1170 		phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
1171 	} else if (phba->sli_rev == 3) {
1172 		pgp_offset = offsetof(struct lpfc_sli2_slim,
1173 				      mbx.us.s3_pgp.port);
1174 		phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
1175 	} else
1176 		pgp_offset = offsetof(struct lpfc_sli2_slim, mbx.us.s2.port);
1177 	pdma_addr = phba->slim2p.phys + pgp_offset;
1178 	phba->pcb->pgpAddrHigh = putPaddrHigh(pdma_addr);
1179 	phba->pcb->pgpAddrLow = putPaddrLow(pdma_addr);
1180 
1181 	/* Use callback routine to setp rings in the pcb */
1182 	lpfc_config_pcb_setup(phba);
1183 
1184 	/* special handling for LC HBAs */
1185 	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
1186 		uint32_t hbainit[5];
1187 
1188 		lpfc_hba_init(phba, hbainit);
1189 
1190 		memcpy(&mb->un.varCfgPort.hbainit, hbainit, 20);
1191 	}
1192 
1193 	/* Swap PCB if needed */
1194 	lpfc_sli_pcimem_bcopy(phba->pcb, phba->pcb, sizeof(PCB_t));
1195 }
1196 
1197 /**
1198  * lpfc_kill_board: Prepare a mailbox command for killing board.
1199  * @phba: pointer to lpfc hba data structure.
1200  * @pmb: pointer to the driver internal queue element for mailbox command.
1201  *
1202  * The kill board mailbox command is used to tell firmware to perform a
1203  * graceful shutdown of a channel on a specified board to prepare for reset.
1204  * When the kill board mailbox command is received, the ER3 bit is set to 1
1205  * in the Host Status register and the ER Attention bit is set to 1 in the
1206  * Host Attention register of the HBA function that received the kill board
1207  * command.
1208  *
1209  * This routine prepares the mailbox command for killing the board in
1210  * preparation for a graceful shutdown.
1211  **/
1212 void
1213 lpfc_kill_board(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1214 {
1215 	MAILBOX_t *mb = &pmb->mb;
1216 
1217 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1218 	mb->mbxCommand = MBX_KILL_BOARD;
1219 	mb->mbxOwner = OWN_HOST;
1220 	return;
1221 }
1222 
1223 /**
1224  * lpfc_mbox_put: Put a mailbox cmd into the tail of driver's mailbox queue.
1225  * @phba: pointer to lpfc hba data structure.
1226  * @mbq: pointer to the driver internal queue element for mailbox command.
1227  *
1228  * Driver maintains a internal mailbox command queue implemented as a linked
1229  * list. When a mailbox command is issued, it shall be put into the mailbox
1230  * command queue such that they shall be processed orderly as HBA can process
1231  * one mailbox command at a time.
1232  **/
1233 void
1234 lpfc_mbox_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
1235 {
1236 	struct lpfc_sli *psli;
1237 
1238 	psli = &phba->sli;
1239 
1240 	list_add_tail(&mbq->list, &psli->mboxq);
1241 
1242 	psli->mboxq_cnt++;
1243 
1244 	return;
1245 }
1246 
1247 /**
1248  * lpfc_mbox_get: Remove a mailbox cmd from the head of driver's mailbox queue.
1249  * @phba: pointer to lpfc hba data structure.
1250  *
1251  * Driver maintains a internal mailbox command queue implemented as a linked
1252  * list. When a mailbox command is issued, it shall be put into the mailbox
1253  * command queue such that they shall be processed orderly as HBA can process
1254  * one mailbox command at a time. After HBA finished processing a mailbox
1255  * command, the driver will remove a pending mailbox command from the head of
1256  * the mailbox command queue and send to the HBA for processing.
1257  *
1258  * Return codes
1259  *    pointer to the driver internal queue element for mailbox command.
1260  **/
1261 LPFC_MBOXQ_t *
1262 lpfc_mbox_get(struct lpfc_hba * phba)
1263 {
1264 	LPFC_MBOXQ_t *mbq = NULL;
1265 	struct lpfc_sli *psli = &phba->sli;
1266 
1267 	list_remove_head((&psli->mboxq), mbq, LPFC_MBOXQ_t, list);
1268 	if (mbq)
1269 		psli->mboxq_cnt--;
1270 
1271 	return mbq;
1272 }
1273 
1274 /**
1275  * lpfc_mbox_cmpl_put: Put mailbox command into mailbox command complete list.
1276  * @phba: pointer to lpfc hba data structure.
1277  * @mbq: pointer to the driver internal queue element for mailbox command.
1278  *
1279  * This routine put the completed mailbox command into the mailbox command
1280  * complete list. This routine is called from driver interrupt handler
1281  * context.The mailbox complete list is used by the driver worker thread
1282  * to process mailbox complete callback functions outside the driver interrupt
1283  * handler.
1284  **/
1285 void
1286 lpfc_mbox_cmpl_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
1287 {
1288 	/* This function expects to be called from interrupt context */
1289 	spin_lock(&phba->hbalock);
1290 	list_add_tail(&mbq->list, &phba->sli.mboxq_cmpl);
1291 	spin_unlock(&phba->hbalock);
1292 	return;
1293 }
1294 
1295 /**
1296  * lpfc_mbox_tmo_val: Retrieve mailbox command timeout value.
1297  * @phba: pointer to lpfc hba data structure.
1298  * @cmd: mailbox command code.
1299  *
1300  * This routine retrieves the proper timeout value according to the mailbox
1301  * command code.
1302  *
1303  * Return codes
1304  *    Timeout value to be used for the given mailbox command
1305  **/
1306 int
1307 lpfc_mbox_tmo_val(struct lpfc_hba *phba, int cmd)
1308 {
1309 	switch (cmd) {
1310 	case MBX_WRITE_NV:	/* 0x03 */
1311 	case MBX_UPDATE_CFG:	/* 0x1B */
1312 	case MBX_DOWN_LOAD:	/* 0x1C */
1313 	case MBX_DEL_LD_ENTRY:	/* 0x1D */
1314 	case MBX_LOAD_AREA:	/* 0x81 */
1315 	case MBX_WRITE_WWN:     /* 0x98 */
1316 	case MBX_LOAD_EXP_ROM:	/* 0x9C */
1317 		return LPFC_MBOX_TMO_FLASH_CMD;
1318 	}
1319 	return LPFC_MBOX_TMO;
1320 }
1321