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