blob: 744bad6275964afc0ef8d0f39611a68b5a366fc4 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart9413aff2007-04-25 09:53:35 -04004 * Copyright (C) 2004-2007 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * 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. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040031#include <scsi/scsi_transport_fc.h>
dea31012005-04-17 16:05:31 -050032
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
35#include "lpfc_disc.h"
36#include "lpfc_scsi.h"
37#include "lpfc.h"
38#include "lpfc_crtn.h"
39#include "lpfc_logmsg.h"
40#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050041#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050042
43/*
44 * Define macro to log: Mailbox command x%x cannot issue Data
45 * This allows multiple uses of lpfc_msgBlk0311
46 * w/o perturbing log msg utility.
47 */
James Smart92d7f7b2007-06-17 19:56:38 -050048#define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
dea31012005-04-17 16:05:31 -050049 lpfc_printf_log(phba, \
50 KERN_INFO, \
51 LOG_MBOX | LOG_SLI, \
James Smarte8b62012007-08-02 11:10:09 -040052 "(%d):0311 Mailbox command x%x cannot " \
James Smart92d7f7b2007-06-17 19:56:38 -050053 "issue Data: x%x x%x x%x\n", \
James Smart92d7f7b2007-06-17 19:56:38 -050054 pmbox->vport ? pmbox->vport->vpi : 0, \
55 pmbox->mb.mbxCommand, \
James Smart2e0fef82007-06-17 19:56:36 -050056 phba->pport->port_state, \
dea31012005-04-17 16:05:31 -050057 psli->sli_flag, \
James Smart2e0fef82007-06-17 19:56:36 -050058 flag)
dea31012005-04-17 16:05:31 -050059
60
61/* There are only four IOCB completion types. */
62typedef enum _lpfc_iocb_type {
63 LPFC_UNKNOWN_IOCB,
64 LPFC_UNSOL_IOCB,
65 LPFC_SOL_IOCB,
66 LPFC_ABORT_IOCB
67} lpfc_iocb_type;
68
James Smart92d7f7b2007-06-17 19:56:38 -050069 /* SLI-2/SLI-3 provide different sized iocbs. Given a pointer
70 * to the start of the ring, and the slot number of the
71 * desired iocb entry, calc a pointer to that entry.
72 */
James Smarted957682007-06-17 19:56:37 -050073static inline IOCB_t *
74lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
75{
76 return (IOCB_t *) (((char *) pring->cmdringaddr) +
77 pring->cmdidx * phba->iocb_cmd_size);
78}
79
80static inline IOCB_t *
81lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
82{
83 return (IOCB_t *) (((char *) pring->rspringaddr) +
84 pring->rspidx * phba->iocb_rsp_size);
85}
86
James Smart2e0fef82007-06-17 19:56:36 -050087static struct lpfc_iocbq *
88__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040089{
90 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
91 struct lpfc_iocbq * iocbq = NULL;
92
93 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
94 return iocbq;
95}
96
James Smart2e0fef82007-06-17 19:56:36 -050097struct lpfc_iocbq *
98lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -050099{
James Smart2e0fef82007-06-17 19:56:36 -0500100 struct lpfc_iocbq * iocbq = NULL;
101 unsigned long iflags;
102
103 spin_lock_irqsave(&phba->hbalock, iflags);
104 iocbq = __lpfc_sli_get_iocbq(phba);
105 spin_unlock_irqrestore(&phba->hbalock, iflags);
106 return iocbq;
107}
108
109void
110__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
111{
112 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500113
114 /*
115 * Clean all volatile data fields, preserve iotag and node struct.
116 */
117 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
118 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
119}
120
James Smart2e0fef82007-06-17 19:56:36 -0500121void
122lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
123{
124 unsigned long iflags;
125
126 /*
127 * Clean all volatile data fields, preserve iotag and node struct.
128 */
129 spin_lock_irqsave(&phba->hbalock, iflags);
130 __lpfc_sli_release_iocbq(phba, iocbq);
131 spin_unlock_irqrestore(&phba->hbalock, iflags);
132}
133
dea31012005-04-17 16:05:31 -0500134/*
135 * Translate the iocb command to an iocb command type used to decide the final
136 * disposition of each completed IOCB.
137 */
138static lpfc_iocb_type
139lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
140{
141 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
142
143 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
144 return 0;
145
146 switch (iocb_cmnd) {
147 case CMD_XMIT_SEQUENCE_CR:
148 case CMD_XMIT_SEQUENCE_CX:
149 case CMD_XMIT_BCAST_CN:
150 case CMD_XMIT_BCAST_CX:
151 case CMD_ELS_REQUEST_CR:
152 case CMD_ELS_REQUEST_CX:
153 case CMD_CREATE_XRI_CR:
154 case CMD_CREATE_XRI_CX:
155 case CMD_GET_RPI_CN:
156 case CMD_XMIT_ELS_RSP_CX:
157 case CMD_GET_RPI_CR:
158 case CMD_FCP_IWRITE_CR:
159 case CMD_FCP_IWRITE_CX:
160 case CMD_FCP_IREAD_CR:
161 case CMD_FCP_IREAD_CX:
162 case CMD_FCP_ICMND_CR:
163 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500164 case CMD_FCP_TSEND_CX:
165 case CMD_FCP_TRSP_CX:
166 case CMD_FCP_TRECEIVE_CX:
167 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500168 case CMD_ADAPTER_MSG:
169 case CMD_ADAPTER_DUMP:
170 case CMD_XMIT_SEQUENCE64_CR:
171 case CMD_XMIT_SEQUENCE64_CX:
172 case CMD_XMIT_BCAST64_CN:
173 case CMD_XMIT_BCAST64_CX:
174 case CMD_ELS_REQUEST64_CR:
175 case CMD_ELS_REQUEST64_CX:
176 case CMD_FCP_IWRITE64_CR:
177 case CMD_FCP_IWRITE64_CX:
178 case CMD_FCP_IREAD64_CR:
179 case CMD_FCP_IREAD64_CX:
180 case CMD_FCP_ICMND64_CR:
181 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500182 case CMD_FCP_TSEND64_CX:
183 case CMD_FCP_TRSP64_CX:
184 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500185 case CMD_GEN_REQUEST64_CR:
186 case CMD_GEN_REQUEST64_CX:
187 case CMD_XMIT_ELS_RSP64_CX:
188 type = LPFC_SOL_IOCB;
189 break;
190 case CMD_ABORT_XRI_CN:
191 case CMD_ABORT_XRI_CX:
192 case CMD_CLOSE_XRI_CN:
193 case CMD_CLOSE_XRI_CX:
194 case CMD_XRI_ABORTED_CX:
195 case CMD_ABORT_MXRI64_CN:
196 type = LPFC_ABORT_IOCB;
197 break;
198 case CMD_RCV_SEQUENCE_CX:
199 case CMD_RCV_ELS_REQ_CX:
200 case CMD_RCV_SEQUENCE64_CX:
201 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400202 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500203 case CMD_IOCB_RCV_SEQ64_CX:
204 case CMD_IOCB_RCV_ELS64_CX:
205 case CMD_IOCB_RCV_CONT64_CX:
dea31012005-04-17 16:05:31 -0500206 type = LPFC_UNSOL_IOCB;
207 break;
208 default:
209 type = LPFC_UNKNOWN_IOCB;
210 break;
211 }
212
213 return type;
214}
215
216static int
James Smarted957682007-06-17 19:56:37 -0500217lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500218{
219 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500220 LPFC_MBOXQ_t *pmb;
221 MAILBOX_t *pmbox;
222 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500223
James Smarted957682007-06-17 19:56:37 -0500224 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
225 if (!pmb)
226 return -ENOMEM;
227 pmbox = &pmb->mb;
228 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500229 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500230 lpfc_config_ring(phba, i, pmb);
231 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
232 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400234 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500235 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
236 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400237 rc, pmbox->mbxCommand,
238 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500239 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500240 ret = -ENXIO;
241 break;
dea31012005-04-17 16:05:31 -0500242 }
243 }
James Smarted957682007-06-17 19:56:37 -0500244 mempool_free(pmb, phba->mbox_mem_pool);
245 return ret;
dea31012005-04-17 16:05:31 -0500246}
247
248static int
James Smart2e0fef82007-06-17 19:56:36 -0500249lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
250 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500251{
dea31012005-04-17 16:05:31 -0500252 list_add_tail(&piocb->list, &pring->txcmplq);
253 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500254 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
255 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
256 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
257 if (!piocb->vport)
258 BUG();
259 else
260 mod_timer(&piocb->vport->els_tmofunc,
261 jiffies + HZ * (phba->fc_ratov << 1));
262 }
263
dea31012005-04-17 16:05:31 -0500264
James Smart2e0fef82007-06-17 19:56:36 -0500265 return 0;
dea31012005-04-17 16:05:31 -0500266}
267
268static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500269lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500270{
dea31012005-04-17 16:05:31 -0500271 struct lpfc_iocbq *cmd_iocb;
272
James Smart858c9f62007-06-17 19:56:39 -0500273 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
274 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500275 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500276 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500277}
278
279static IOCB_t *
280lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
281{
James Smarted957682007-06-17 19:56:37 -0500282 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
283 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
284 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -0500285 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500286
287 if ((pring->next_cmdidx == pring->cmdidx) &&
288 (++pring->next_cmdidx >= max_cmd_idx))
289 pring->next_cmdidx = 0;
290
291 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
292
293 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
294
295 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
296 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400297 "0315 Ring %d issue: portCmdGet %d "
dea31012005-04-17 16:05:31 -0500298 "is bigger then cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400299 pring->ringno,
dea31012005-04-17 16:05:31 -0500300 pring->local_getidx, max_cmd_idx);
301
James Smart2e0fef82007-06-17 19:56:36 -0500302 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500303 /*
304 * All error attention handlers are posted to
305 * worker thread
306 */
307 phba->work_ha |= HA_ERATT;
308 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500309
310 /* hbalock should already be held */
dea31012005-04-17 16:05:31 -0500311 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -0500312 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500313
314 return NULL;
315 }
316
317 if (pring->local_getidx == pring->next_cmdidx)
318 return NULL;
319 }
320
James Smarted957682007-06-17 19:56:37 -0500321 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500322}
323
James Bottomley604a3e32005-10-29 10:28:33 -0500324uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500325lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500326{
James Smart2e0fef82007-06-17 19:56:36 -0500327 struct lpfc_iocbq **new_arr;
328 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500329 size_t new_len;
330 struct lpfc_sli *psli = &phba->sli;
331 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500332
James Smart2e0fef82007-06-17 19:56:36 -0500333 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500334 iotag = psli->last_iotag;
335 if(++iotag < psli->iocbq_lookup_len) {
336 psli->last_iotag = iotag;
337 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500338 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500339 iocbq->iotag = iotag;
340 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500341 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500342 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
343 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500344 spin_unlock_irq(&phba->hbalock);
345 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500346 GFP_KERNEL);
347 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500348 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500349 old_arr = psli->iocbq_lookup;
350 if (new_len <= psli->iocbq_lookup_len) {
351 /* highly unprobable case */
352 kfree(new_arr);
353 iotag = psli->last_iotag;
354 if(++iotag < psli->iocbq_lookup_len) {
355 psli->last_iotag = iotag;
356 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500357 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500358 iocbq->iotag = iotag;
359 return iotag;
360 }
James Smart2e0fef82007-06-17 19:56:36 -0500361 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500362 return 0;
363 }
364 if (psli->iocbq_lookup)
365 memcpy(new_arr, old_arr,
366 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -0400367 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -0500368 psli->iocbq_lookup = new_arr;
369 psli->iocbq_lookup_len = new_len;
370 psli->last_iotag = iotag;
371 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500372 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500373 iocbq->iotag = iotag;
374 kfree(old_arr);
375 return iotag;
376 }
James Smart8f6d98d2006-08-01 07:34:00 -0400377 } else
James Smart2e0fef82007-06-17 19:56:36 -0500378 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500379
James Bottomley604a3e32005-10-29 10:28:33 -0500380 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400381 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
382 psli->last_iotag);
dea31012005-04-17 16:05:31 -0500383
James Bottomley604a3e32005-10-29 10:28:33 -0500384 return 0;
dea31012005-04-17 16:05:31 -0500385}
386
387static void
388lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
389 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
390{
391 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500392 * Set up an iotag
dea31012005-04-17 16:05:31 -0500393 */
James Bottomley604a3e32005-10-29 10:28:33 -0500394 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500395
James Smarta58cbd52007-08-02 11:09:43 -0400396 if (pring->ringno == LPFC_ELS_RING) {
397 lpfc_debugfs_slow_ring_trc(phba,
398 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
399 *(((uint32_t *) &nextiocb->iocb) + 4),
400 *(((uint32_t *) &nextiocb->iocb) + 6),
401 *(((uint32_t *) &nextiocb->iocb) + 7));
402 }
403
dea31012005-04-17 16:05:31 -0500404 /*
405 * Issue iocb command to adapter
406 */
James Smart92d7f7b2007-06-17 19:56:38 -0500407 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500408 wmb();
409 pring->stats.iocb_cmd++;
410
411 /*
412 * If there is no completion routine to call, we can release the
413 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
414 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
415 */
416 if (nextiocb->iocb_cmpl)
417 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500418 else
James Smart2e0fef82007-06-17 19:56:36 -0500419 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500420
421 /*
422 * Let the HBA know what IOCB slot will be the next one the
423 * driver will put a command into.
424 */
425 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500426 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500427}
428
429static void
James Smart2e0fef82007-06-17 19:56:36 -0500430lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500431{
432 int ringno = pring->ringno;
433
434 pring->flag |= LPFC_CALL_RING_AVAILABLE;
435
436 wmb();
437
438 /*
439 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
440 * The HBA will tell us when an IOCB entry is available.
441 */
442 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
443 readl(phba->CAregaddr); /* flush */
444
445 pring->stats.iocb_cmd_full++;
446}
447
448static void
James Smart2e0fef82007-06-17 19:56:36 -0500449lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500450{
451 int ringno = pring->ringno;
452
453 /*
454 * Tell the HBA that there is work to do in this ring.
455 */
456 wmb();
457 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
458 readl(phba->CAregaddr); /* flush */
459}
460
461static void
James Smart2e0fef82007-06-17 19:56:36 -0500462lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500463{
464 IOCB_t *iocb;
465 struct lpfc_iocbq *nextiocb;
466
467 /*
468 * Check to see if:
469 * (a) there is anything on the txq to send
470 * (b) link is up
471 * (c) link attention events can be processed (fcp ring only)
472 * (d) IOCB processing is not blocked by the outstanding mbox command.
473 */
474 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500475 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500476 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -0400477 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -0500478
479 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
480 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
481 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
482
483 if (iocb)
484 lpfc_sli_update_ring(phba, pring);
485 else
486 lpfc_sli_update_full_ring(phba, pring);
487 }
488
489 return;
490}
491
James Smarted957682007-06-17 19:56:37 -0500492struct lpfc_hbq_entry *
493lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
494{
495 struct hbq_s *hbqp = &phba->hbqs[hbqno];
496
497 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
498 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
499 hbqp->next_hbqPutIdx = 0;
500
501 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500502 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500503 uint32_t getidx = le32_to_cpu(raw_index);
504
505 hbqp->local_hbqGetIdx = getidx;
506
507 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
508 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500509 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400510 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500511 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400512 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500513 hbqp->entry_count);
514
515 phba->link_state = LPFC_HBA_ERROR;
516 return NULL;
517 }
518
519 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
520 return NULL;
521 }
522
James Smart51ef4c22007-08-02 11:10:31 -0400523 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
524 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500525}
526
527void
528lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
529{
James Smart92d7f7b2007-06-17 19:56:38 -0500530 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
531 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400532 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -0500533
James Smart51ef4c22007-08-02 11:10:31 -0400534 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500535 /* Return all memory used by all HBQs */
James Smart51ef4c22007-08-02 11:10:31 -0400536 for (i = 0; i < hbq_count; ++i) {
537 list_for_each_entry_safe(dmabuf, next_dmabuf,
538 &phba->hbqs[i].hbq_buffer_list, list) {
539 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
540 list_del(&hbq_buf->dbuf.list);
541 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
542 }
James Smarted957682007-06-17 19:56:37 -0500543 }
James Smarted957682007-06-17 19:56:37 -0500544}
545
James Smart51ef4c22007-08-02 11:10:31 -0400546static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500547lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500548 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500549{
550 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500551 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500552
553 /* Get next HBQ entry slot to use */
554 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
555 if (hbqe) {
556 struct hbq_s *hbqp = &phba->hbqs[hbqno];
557
James Smart92d7f7b2007-06-17 19:56:38 -0500558 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
559 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400560 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500561 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500562 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
563 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
564 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500565 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
566 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500567 /* flush */
James Smarted957682007-06-17 19:56:37 -0500568 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400569 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smarted957682007-06-17 19:56:37 -0500570 }
James Smart51ef4c22007-08-02 11:10:31 -0400571 return hbqe;
James Smarted957682007-06-17 19:56:37 -0500572}
573
James Smart92d7f7b2007-06-17 19:56:38 -0500574static struct lpfc_hbq_init lpfc_els_hbq = {
575 .rn = 1,
576 .entry_count = 200,
577 .mask_count = 0,
578 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400579 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500580 .buffer_count = 0,
581 .init_count = 20,
582 .add_count = 5,
583};
James Smarted957682007-06-17 19:56:37 -0500584
James Smart51ef4c22007-08-02 11:10:31 -0400585static struct lpfc_hbq_init lpfc_extra_hbq = {
586 .rn = 1,
587 .entry_count = 200,
588 .mask_count = 0,
589 .profile = 0,
590 .ring_mask = (1 << LPFC_EXTRA_RING),
591 .buffer_count = 0,
592 .init_count = 0,
593 .add_count = 5,
594};
595
James Smart78b2d852007-08-02 11:10:21 -0400596struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500597 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400598 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500599};
600
James Smart311464e2007-08-02 11:10:37 -0400601static int
James Smart92d7f7b2007-06-17 19:56:38 -0500602lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
603{
604 uint32_t i, start, end;
605 struct hbq_dmabuf *hbq_buffer;
606
James Smart51ef4c22007-08-02 11:10:31 -0400607 if (!phba->hbqs[hbqno].hbq_alloc_buffer) {
608 return 0;
609 }
610
James Smart92d7f7b2007-06-17 19:56:38 -0500611 start = lpfc_hbq_defs[hbqno]->buffer_count;
612 end = count + lpfc_hbq_defs[hbqno]->buffer_count;
613 if (end > lpfc_hbq_defs[hbqno]->entry_count) {
614 end = lpfc_hbq_defs[hbqno]->entry_count;
James Smarted957682007-06-17 19:56:37 -0500615 }
James Smart92d7f7b2007-06-17 19:56:38 -0500616
617 /* Populate HBQ entries */
618 for (i = start; i < end; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400619 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500620 if (!hbq_buffer)
621 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500622 hbq_buffer->tag = (i | (hbqno << 16));
James Smart51ef4c22007-08-02 11:10:31 -0400623 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
624 lpfc_hbq_defs[hbqno]->buffer_count++;
625 else
626 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -0500627 }
628 return 0;
James Smarted957682007-06-17 19:56:37 -0500629}
630
631int
James Smart92d7f7b2007-06-17 19:56:38 -0500632lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500633{
James Smart92d7f7b2007-06-17 19:56:38 -0500634 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
635 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -0500636}
637
James Smart92d7f7b2007-06-17 19:56:38 -0500638int
639lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500640{
James Smart92d7f7b2007-06-17 19:56:38 -0500641 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
642 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -0500643}
644
645struct hbq_dmabuf *
646lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
647{
James Smart92d7f7b2007-06-17 19:56:38 -0500648 struct lpfc_dmabuf *d_buf;
649 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400650 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500651
James Smart51ef4c22007-08-02 11:10:31 -0400652 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +0200653 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -0400654 return NULL;
655
656 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -0500657 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -0400658 if (hbq_buf->tag == tag) {
James Smart92d7f7b2007-06-17 19:56:38 -0500659 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -0500660 }
661 }
James Smart92d7f7b2007-06-17 19:56:38 -0500662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400663 "1803 Bad hbq tag. Data: x%x x%x\n",
664 tag, lpfc_hbq_defs[tag >> 16]->buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -0500665 return NULL;
James Smarted957682007-06-17 19:56:37 -0500666}
667
668void
James Smart51ef4c22007-08-02 11:10:31 -0400669lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -0500670{
671 uint32_t hbqno;
672
James Smart51ef4c22007-08-02 11:10:31 -0400673 if (hbq_buffer) {
674 hbqno = hbq_buffer->tag >> 16;
675 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
676 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
677 }
James Smarted957682007-06-17 19:56:37 -0500678 }
679}
680
dea31012005-04-17 16:05:31 -0500681static int
682lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
683{
684 uint8_t ret;
685
686 switch (mbxCommand) {
687 case MBX_LOAD_SM:
688 case MBX_READ_NV:
689 case MBX_WRITE_NV:
690 case MBX_RUN_BIU_DIAG:
691 case MBX_INIT_LINK:
692 case MBX_DOWN_LINK:
693 case MBX_CONFIG_LINK:
694 case MBX_CONFIG_RING:
695 case MBX_RESET_RING:
696 case MBX_READ_CONFIG:
697 case MBX_READ_RCONFIG:
698 case MBX_READ_SPARM:
699 case MBX_READ_STATUS:
700 case MBX_READ_RPI:
701 case MBX_READ_XRI:
702 case MBX_READ_REV:
703 case MBX_READ_LNK_STAT:
704 case MBX_REG_LOGIN:
705 case MBX_UNREG_LOGIN:
706 case MBX_READ_LA:
707 case MBX_CLEAR_LA:
708 case MBX_DUMP_MEMORY:
709 case MBX_DUMP_CONTEXT:
710 case MBX_RUN_DIAGS:
711 case MBX_RESTART:
712 case MBX_UPDATE_CFG:
713 case MBX_DOWN_LOAD:
714 case MBX_DEL_LD_ENTRY:
715 case MBX_RUN_PROGRAM:
716 case MBX_SET_MASK:
717 case MBX_SET_SLIM:
718 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500719 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -0500720 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500721 case MBX_BEACON:
dea31012005-04-17 16:05:31 -0500722 case MBX_LOAD_AREA:
723 case MBX_RUN_BIU_DIAG64:
724 case MBX_CONFIG_PORT:
725 case MBX_READ_SPARM64:
726 case MBX_READ_RPI64:
727 case MBX_REG_LOGIN64:
728 case MBX_READ_LA64:
729 case MBX_FLASH_WR_ULA:
730 case MBX_SET_DEBUG:
731 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -0400732 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -0500733 case MBX_REG_VPI:
734 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -0500735 case MBX_HEARTBEAT:
dea31012005-04-17 16:05:31 -0500736 ret = mbxCommand;
737 break;
738 default:
739 ret = MBX_SHUTDOWN;
740 break;
741 }
James Smart2e0fef82007-06-17 19:56:36 -0500742 return ret;
dea31012005-04-17 16:05:31 -0500743}
744static void
James Smart2e0fef82007-06-17 19:56:36 -0500745lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -0500746{
747 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -0500748 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -0500749
750 /*
751 * If pdone_q is empty, the driver thread gave up waiting and
752 * continued running.
753 */
James Smart7054a602007-04-25 09:52:34 -0400754 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -0500755 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -0500756 pdone_q = (wait_queue_head_t *) pmboxq->context1;
757 if (pdone_q)
758 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -0500759 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -0500760 return;
761}
762
763void
James Smart2e0fef82007-06-17 19:56:36 -0500764lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500765{
766 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -0400767 uint16_t rpi;
768 int rc;
769
dea31012005-04-17 16:05:31 -0500770 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -0400771
dea31012005-04-17 16:05:31 -0500772 if (mp) {
773 lpfc_mbuf_free(phba, mp->virt, mp->phys);
774 kfree(mp);
775 }
James Smart7054a602007-04-25 09:52:34 -0400776
777 /*
778 * If a REG_LOGIN succeeded after node is destroyed or node
779 * is in re-discovery driver need to cleanup the RPI.
780 */
James Smart2e0fef82007-06-17 19:56:36 -0500781 if (!(phba->pport->load_flag & FC_UNLOADING) &&
782 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
783 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -0400784
785 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -0500786 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
787 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -0400788 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
789 if (rc != MBX_NOT_FINISHED)
790 return;
791 }
792
James Smart2e0fef82007-06-17 19:56:36 -0500793 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500794 return;
795}
796
797int
James Smart2e0fef82007-06-17 19:56:36 -0500798lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500799{
James Smart92d7f7b2007-06-17 19:56:38 -0500800 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -0500801 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -0500802 int rc;
803 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -0500804
805 phba->sli.slistat.mbox_event++;
806
James Smart92d7f7b2007-06-17 19:56:38 -0500807 /* Get all completed mailboxe buffers into the cmplq */
808 spin_lock_irq(&phba->hbalock);
809 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
810 spin_unlock_irq(&phba->hbalock);
811
dea31012005-04-17 16:05:31 -0500812 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -0500813 do {
814 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
815 if (pmb == NULL)
816 break;
817
dea31012005-04-17 16:05:31 -0500818 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -0500819
James Smart858c9f62007-06-17 19:56:39 -0500820 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
821 if (pmb->vport) {
822 lpfc_debugfs_disc_trc(pmb->vport,
823 LPFC_DISC_TRC_MBOX_VPORT,
824 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
825 (uint32_t)pmbox->mbxCommand,
826 pmbox->un.varWords[0],
827 pmbox->un.varWords[1]);
828 }
829 else {
830 lpfc_debugfs_disc_trc(phba->pport,
831 LPFC_DISC_TRC_MBOX,
832 "MBOX cmpl: cmd:x%x mb:x%x x%x",
833 (uint32_t)pmbox->mbxCommand,
834 pmbox->un.varWords[0],
835 pmbox->un.varWords[1]);
836 }
837 }
838
dea31012005-04-17 16:05:31 -0500839 /*
840 * It is a fatal error if unknown mbox command completion.
841 */
842 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
843 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -0500844 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -0500845 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400846 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -0500847 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500848 pmb->vport ? pmb->vport->vpi : 0,
849 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -0500850 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500851 phba->work_hs = HS_FFER3;
852 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500853 continue;
dea31012005-04-17 16:05:31 -0500854 }
855
dea31012005-04-17 16:05:31 -0500856 if (pmbox->mbxStatus) {
857 phba->sli.slistat.mbox_stat_err++;
858 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
859 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -0500860 lpfc_printf_log(phba, KERN_INFO,
861 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400862 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -0500863 "error - RETRYing Data: x%x "
864 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500865 pmb->vport ? pmb->vport->vpi :0,
866 pmbox->mbxCommand,
867 pmbox->mbxStatus,
868 pmbox->un.varWords[0],
869 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -0500870 pmbox->mbxStatus = 0;
871 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -0500872 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500873 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -0500874 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500875 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
876 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -0500877 continue;
dea31012005-04-17 16:05:31 -0500878 }
879 }
880
881 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -0500882 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400883 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -0500884 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500885 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -0500886 pmbox->mbxCommand,
887 pmb->mbox_cmpl,
888 *((uint32_t *) pmbox),
889 pmbox->un.varWords[0],
890 pmbox->un.varWords[1],
891 pmbox->un.varWords[2],
892 pmbox->un.varWords[3],
893 pmbox->un.varWords[4],
894 pmbox->un.varWords[5],
895 pmbox->un.varWords[6],
896 pmbox->un.varWords[7]);
897
James Smart92d7f7b2007-06-17 19:56:38 -0500898 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -0500899 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -0500900 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -0500901 return 0;
dea31012005-04-17 16:05:31 -0500902}
James Smart92d7f7b2007-06-17 19:56:38 -0500903
904static struct lpfc_dmabuf *
905lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
906{
907 struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
James Smart51ef4c22007-08-02 11:10:31 -0400908 uint32_t hbqno;
909 void *virt; /* virtual address ptr */
910 dma_addr_t phys; /* mapped address */
James Smart92d7f7b2007-06-17 19:56:38 -0500911
912 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
913 if (hbq_entry == NULL)
914 return NULL;
915 list_del(&hbq_entry->dbuf.list);
James Smart51ef4c22007-08-02 11:10:31 -0400916
917 hbqno = tag >> 16;
918 new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500919 if (new_hbq_entry == NULL)
920 return &hbq_entry->dbuf;
James Smart92d7f7b2007-06-17 19:56:38 -0500921 new_hbq_entry->tag = -1;
James Smart51ef4c22007-08-02 11:10:31 -0400922 phys = new_hbq_entry->dbuf.phys;
923 virt = new_hbq_entry->dbuf.virt;
924 new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
925 new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
926 hbq_entry->dbuf.phys = phys;
927 hbq_entry->dbuf.virt = virt;
James Smart92d7f7b2007-06-17 19:56:38 -0500928 lpfc_sli_free_hbq(phba, hbq_entry);
929 return &new_hbq_entry->dbuf;
930}
931
James Smart57127f12007-10-27 13:37:05 -0400932
dea31012005-04-17 16:05:31 -0500933static int
934lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
935 struct lpfc_iocbq *saveq)
936{
937 IOCB_t * irsp;
938 WORD5 * w5p;
939 uint32_t Rctl, Type;
940 uint32_t match, i;
941
942 match = 0;
943 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -0400944
945 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
946 if (pring->lpfc_sli_rcv_async_status)
947 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
948 else
949 lpfc_printf_log(phba,
950 KERN_WARNING,
951 LOG_SLI,
952 "0316 Ring %d handler: unexpected "
953 "ASYNC_STATUS iocb received evt_code "
954 "0x%x\n",
955 pring->ringno,
956 irsp->un.asyncstat.evt_code);
957 return 1;
958 }
959
dea31012005-04-17 16:05:31 -0500960 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
James Smarted957682007-06-17 19:56:37 -0500961 || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)
962 || (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)
963 || (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX)) {
dea31012005-04-17 16:05:31 -0500964 Rctl = FC_ELS_REQ;
965 Type = FC_ELS_DATA;
966 } else {
967 w5p =
968 (WORD5 *) & (saveq->iocb.un.
969 ulpWord[5]);
970 Rctl = w5p->hcsw.Rctl;
971 Type = w5p->hcsw.Type;
972
973 /* Firmware Workaround */
974 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
James Smart92d7f7b2007-06-17 19:56:38 -0500975 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
976 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
dea31012005-04-17 16:05:31 -0500977 Rctl = FC_ELS_REQ;
978 Type = FC_ELS_DATA;
979 w5p->hcsw.Rctl = Rctl;
980 w5p->hcsw.Type = Type;
981 }
982 }
James Smart92d7f7b2007-06-17 19:56:38 -0500983
984 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
985 if (irsp->ulpBdeCount != 0)
986 saveq->context2 = lpfc_sli_replace_hbqbuff(phba,
987 irsp->un.ulpWord[3]);
988 if (irsp->ulpBdeCount == 2)
989 saveq->context3 = lpfc_sli_replace_hbqbuff(phba,
James Smart51ef4c22007-08-02 11:10:31 -0400990 irsp->unsli3.sli3Words[7]);
James Smart92d7f7b2007-06-17 19:56:38 -0500991 }
992
dea31012005-04-17 16:05:31 -0500993 /* unSolicited Responses */
994 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -0500995 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
996 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
997 saveq);
dea31012005-04-17 16:05:31 -0500998 match = 1;
999 } else {
1000 /* We must search, based on rctl / type
1001 for the right routine */
1002 for (i = 0; i < pring->num_mask;
1003 i++) {
1004 if ((pring->prt[i].rctl ==
1005 Rctl)
1006 && (pring->prt[i].
1007 type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001008 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1009 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1010 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -05001011 match = 1;
1012 break;
1013 }
1014 }
1015 }
1016 if (match == 0) {
1017 /* Unexpected Rctl / Type received */
1018 /* Ring <ringno> handler: unexpected
1019 Rctl <Rctl> Type <Type> received */
James Smart92d7f7b2007-06-17 19:56:38 -05001020 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001021 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001022 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001023 pring->ringno, Rctl, Type);
dea31012005-04-17 16:05:31 -05001024 }
James Smart92d7f7b2007-06-17 19:56:38 -05001025 return 1;
dea31012005-04-17 16:05:31 -05001026}
1027
1028static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001029lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1030 struct lpfc_sli_ring *pring,
1031 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001032{
dea31012005-04-17 16:05:31 -05001033 struct lpfc_iocbq *cmd_iocb = NULL;
1034 uint16_t iotag;
1035
James Bottomley604a3e32005-10-29 10:28:33 -05001036 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001037
James Bottomley604a3e32005-10-29 10:28:33 -05001038 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1039 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001040 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001041 pring->txcmplq_cnt--;
1042 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001043 }
1044
dea31012005-04-17 16:05:31 -05001045 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001046 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001047 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001048 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001049 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001050 return NULL;
1051}
1052
1053static int
James Smart2e0fef82007-06-17 19:56:36 -05001054lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001055 struct lpfc_iocbq *saveq)
1056{
James Smart2e0fef82007-06-17 19:56:36 -05001057 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001058 int rc = 1;
1059 unsigned long iflag;
1060
1061 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001062 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001063 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001064 spin_unlock_irqrestore(&phba->hbalock, iflag);
1065
dea31012005-04-17 16:05:31 -05001066 if (cmdiocbp) {
1067 if (cmdiocbp->iocb_cmpl) {
1068 /*
1069 * Post all ELS completions to the worker thread.
1070 * All other are passed to the completion callback.
1071 */
1072 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001073 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1074 cmdiocbp->iocb_flag &=
1075 ~LPFC_DRIVER_ABORTED;
1076 saveq->iocb.ulpStatus =
1077 IOSTAT_LOCAL_REJECT;
1078 saveq->iocb.un.ulpWord[4] =
1079 IOERR_SLI_ABORTED;
1080 }
dea31012005-04-17 16:05:31 -05001081 }
James Smart2e0fef82007-06-17 19:56:36 -05001082 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001083 } else
1084 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001085 } else {
1086 /*
1087 * Unknown initiating command based on the response iotag.
1088 * This could be the case on the ELS ring because of
1089 * lpfc_els_abort().
1090 */
1091 if (pring->ringno != LPFC_ELS_RING) {
1092 /*
1093 * Ring <ringno> handler: unexpected completion IoTag
1094 * <IoTag>
1095 */
James Smarte8b62012007-08-02 11:10:09 -04001096 lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1097 "0322 Ring %d handler: "
1098 "unexpected completion IoTag x%x "
1099 "Data: x%x x%x x%x x%x\n",
1100 pring->ringno,
1101 saveq->iocb.ulpIoTag,
1102 saveq->iocb.ulpStatus,
1103 saveq->iocb.un.ulpWord[4],
1104 saveq->iocb.ulpCommand,
1105 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001106 }
1107 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001108
dea31012005-04-17 16:05:31 -05001109 return rc;
1110}
1111
James Smart2e0fef82007-06-17 19:56:36 -05001112static void
1113lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001114{
James Smarted957682007-06-17 19:56:37 -05001115 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1116 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1117 &phba->slim2p->mbx.us.s2.port[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001118 /*
1119 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1120 * rsp ring <portRspMax>
1121 */
1122 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001123 "0312 Ring %d handler: portRspPut %d "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001124 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001125 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001126 pring->numRiocb);
1127
James Smart2e0fef82007-06-17 19:56:36 -05001128 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001129
1130 /*
1131 * All error attention handlers are posted to
1132 * worker thread
1133 */
1134 phba->work_ha |= HA_ERATT;
1135 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001136
1137 /* hbalock should already be held */
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001138 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05001139 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001140
1141 return;
1142}
1143
James Smart2e0fef82007-06-17 19:56:36 -05001144void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001145{
James Smart2e0fef82007-06-17 19:56:36 -05001146 struct lpfc_sli *psli = &phba->sli;
1147 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001148 IOCB_t *irsp = NULL;
1149 IOCB_t *entry = NULL;
1150 struct lpfc_iocbq *cmdiocbq = NULL;
1151 struct lpfc_iocbq rspiocbq;
1152 struct lpfc_pgp *pgp;
1153 uint32_t status;
1154 uint32_t portRspPut, portRspMax;
1155 int type;
1156 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001157 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001158 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001159
1160 pring->stats.iocb_event++;
1161
James Smarted957682007-06-17 19:56:37 -05001162 pgp = (phba->sli_rev == 3) ?
1163 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1164 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1165
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001166
1167 /*
1168 * The next available response entry should never exceed the maximum
1169 * entries. If it does, treat it as an adapter hardware error.
1170 */
1171 portRspMax = pring->numRiocb;
1172 portRspPut = le32_to_cpu(pgp->rspPutInx);
1173 if (unlikely(portRspPut >= portRspMax)) {
1174 lpfc_sli_rsp_pointers_error(phba, pring);
1175 return;
1176 }
1177
1178 rmb();
1179 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001180 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001181 if (++pring->rspidx >= portRspMax)
1182 pring->rspidx = 0;
1183
1184 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1185 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001186 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001187 irsp = &rspiocbq.iocb;
1188 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1189 pring->stats.iocb_rsp++;
1190 rsp_cmpl++;
1191
1192 if (unlikely(irsp->ulpStatus)) {
1193 /* Rsp ring <ringno> error: IOCB */
1194 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001195 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001196 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001197 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001198 irsp->un.ulpWord[0],
1199 irsp->un.ulpWord[1],
1200 irsp->un.ulpWord[2],
1201 irsp->un.ulpWord[3],
1202 irsp->un.ulpWord[4],
1203 irsp->un.ulpWord[5],
1204 *(((uint32_t *) irsp) + 6),
1205 *(((uint32_t *) irsp) + 7));
1206 }
1207
1208 switch (type) {
1209 case LPFC_ABORT_IOCB:
1210 case LPFC_SOL_IOCB:
1211 /*
1212 * Idle exchange closed via ABTS from port. No iocb
1213 * resources need to be recovered.
1214 */
1215 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001216 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001217 "0314 IOCB cmd 0x%x "
1218 "processed. Skipping "
1219 "completion",
James Smartdca94792006-08-01 07:34:08 -04001220 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001221 break;
1222 }
1223
James Smart2e0fef82007-06-17 19:56:36 -05001224 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001225 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1226 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001227 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001228 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1229 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1230 &rspiocbq);
1231 }
1232 break;
1233 default:
1234 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1235 char adaptermsg[LPFC_MAX_ADPTMSG];
1236 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1237 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1238 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001239 dev_warn(&((phba->pcidev)->dev),
1240 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001241 phba->brd_no, adaptermsg);
1242 } else {
1243 /* Unknown IOCB command */
1244 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001245 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001246 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001247 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001248 irsp->ulpStatus,
1249 irsp->ulpIoTag,
1250 irsp->ulpContext);
1251 }
1252 break;
1253 }
1254
1255 /*
1256 * The response IOCB has been processed. Update the ring
1257 * pointer in SLIM. If the port response put pointer has not
1258 * been updated, sync the pgp->rspPutInx and fetch the new port
1259 * response put pointer.
1260 */
James Smarted957682007-06-17 19:56:37 -05001261 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001262
1263 if (pring->rspidx == portRspPut)
1264 portRspPut = le32_to_cpu(pgp->rspPutInx);
1265 }
1266
1267 ha_copy = readl(phba->HAregaddr);
1268 ha_copy >>= (LPFC_FCP_RING * 4);
1269
1270 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001271 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001272 pring->stats.iocb_rsp_full++;
1273 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1274 writel(status, phba->CAregaddr);
1275 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001276 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001277 }
1278 if ((ha_copy & HA_R0CE_RSP) &&
1279 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001280 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001281 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1282 pring->stats.iocb_cmd_empty++;
1283
1284 /* Force update of the local copy of cmdGetInx */
1285 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1286 lpfc_sli_resume_iocb(phba, pring);
1287
1288 if ((pring->lpfc_sli_cmd_available))
1289 (pring->lpfc_sli_cmd_available) (phba, pring);
1290
James Smart2e0fef82007-06-17 19:56:36 -05001291 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001292 }
1293
1294 return;
1295}
1296
dea31012005-04-17 16:05:31 -05001297/*
1298 * This routine presumes LPFC_FCP_RING handling and doesn't bother
1299 * to check it explicitly.
1300 */
1301static int
James Smart2e0fef82007-06-17 19:56:36 -05001302lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1303 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001304{
James Smarted957682007-06-17 19:56:37 -05001305 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1306 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1307 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001308 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001309 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001310 struct lpfc_iocbq *cmdiocbq = NULL;
1311 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001312 uint32_t status;
1313 uint32_t portRspPut, portRspMax;
1314 int rc = 1;
1315 lpfc_iocb_type type;
1316 unsigned long iflag;
1317 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05001318
James Smart2e0fef82007-06-17 19:56:36 -05001319 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001320 pring->stats.iocb_event++;
1321
dea31012005-04-17 16:05:31 -05001322 /*
1323 * The next available response entry should never exceed the maximum
1324 * entries. If it does, treat it as an adapter hardware error.
1325 */
1326 portRspMax = pring->numRiocb;
1327 portRspPut = le32_to_cpu(pgp->rspPutInx);
1328 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001329 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05001330 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001331 return 1;
1332 }
1333
1334 rmb();
1335 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001336 /*
1337 * Fetch an entry off the ring and copy it into a local data
1338 * structure. The copy involves a byte-swap since the
1339 * network byte order and pci byte orders are different.
1340 */
James Smarted957682007-06-17 19:56:37 -05001341 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05001342 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001343
1344 if (++pring->rspidx >= portRspMax)
1345 pring->rspidx = 0;
1346
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001347 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1348 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05001349 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05001350 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001351 irsp = &rspiocbq.iocb;
1352
dea31012005-04-17 16:05:31 -05001353 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1354 pring->stats.iocb_rsp++;
1355 rsp_cmpl++;
1356
1357 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001358 /*
1359 * If resource errors reported from HBA, reduce
1360 * queuedepths of the SCSI device.
1361 */
1362 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1363 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1364 spin_unlock_irqrestore(&phba->hbalock, iflag);
1365 lpfc_adjust_queue_depth(phba);
1366 spin_lock_irqsave(&phba->hbalock, iflag);
1367 }
1368
dea31012005-04-17 16:05:31 -05001369 /* Rsp ring <ringno> error: IOCB */
1370 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001371 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05001372 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001373 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05001374 irsp->un.ulpWord[0],
1375 irsp->un.ulpWord[1],
1376 irsp->un.ulpWord[2],
1377 irsp->un.ulpWord[3],
1378 irsp->un.ulpWord[4],
1379 irsp->un.ulpWord[5],
1380 *(((uint32_t *) irsp) + 6),
1381 *(((uint32_t *) irsp) + 7));
dea31012005-04-17 16:05:31 -05001382 }
1383
1384 switch (type) {
1385 case LPFC_ABORT_IOCB:
1386 case LPFC_SOL_IOCB:
1387 /*
1388 * Idle exchange closed via ABTS from port. No iocb
1389 * resources need to be recovered.
1390 */
1391 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001392 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001393 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04001394 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05001395 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04001396 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05001397 break;
1398 }
1399
James Bottomley604a3e32005-10-29 10:28:33 -05001400 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1401 &rspiocbq);
dea31012005-04-17 16:05:31 -05001402 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001403 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1404 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1405 &rspiocbq);
1406 } else {
James Smart2e0fef82007-06-17 19:56:36 -05001407 spin_unlock_irqrestore(&phba->hbalock,
1408 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001409 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1410 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001411 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001412 iflag);
1413 }
dea31012005-04-17 16:05:31 -05001414 }
1415 break;
James Smarta4bc3372006-12-02 13:34:16 -05001416 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05001417 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001418 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001419 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001420 break;
dea31012005-04-17 16:05:31 -05001421 default:
1422 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1423 char adaptermsg[LPFC_MAX_ADPTMSG];
1424 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1425 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1426 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001427 dev_warn(&((phba->pcidev)->dev),
1428 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05001429 phba->brd_no, adaptermsg);
1430 } else {
1431 /* Unknown IOCB command */
1432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001433 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05001434 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001435 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05001436 irsp->ulpStatus,
1437 irsp->ulpIoTag,
1438 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05001439 }
1440 break;
1441 }
1442
1443 /*
1444 * The response IOCB has been processed. Update the ring
1445 * pointer in SLIM. If the port response put pointer has not
1446 * been updated, sync the pgp->rspPutInx and fetch the new port
1447 * response put pointer.
1448 */
James Smarted957682007-06-17 19:56:37 -05001449 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05001450
1451 if (pring->rspidx == portRspPut)
1452 portRspPut = le32_to_cpu(pgp->rspPutInx);
1453 }
1454
1455 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
1456 pring->stats.iocb_rsp_full++;
1457 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1458 writel(status, phba->CAregaddr);
1459 readl(phba->CAregaddr);
1460 }
1461 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1462 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1463 pring->stats.iocb_cmd_empty++;
1464
1465 /* Force update of the local copy of cmdGetInx */
1466 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1467 lpfc_sli_resume_iocb(phba, pring);
1468
1469 if ((pring->lpfc_sli_cmd_available))
1470 (pring->lpfc_sli_cmd_available) (phba, pring);
1471
1472 }
1473
James Smart2e0fef82007-06-17 19:56:36 -05001474 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001475 return rc;
1476}
1477
dea31012005-04-17 16:05:31 -05001478int
James Smart2e0fef82007-06-17 19:56:36 -05001479lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
1480 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001481{
James Smarted957682007-06-17 19:56:37 -05001482 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1483 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1484 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001485 IOCB_t *entry;
1486 IOCB_t *irsp = NULL;
1487 struct lpfc_iocbq *rspiocbp = NULL;
1488 struct lpfc_iocbq *next_iocb;
1489 struct lpfc_iocbq *cmdiocbp;
1490 struct lpfc_iocbq *saveq;
dea31012005-04-17 16:05:31 -05001491 uint8_t iocb_cmd_type;
1492 lpfc_iocb_type type;
1493 uint32_t status, free_saveq;
1494 uint32_t portRspPut, portRspMax;
1495 int rc = 1;
1496 unsigned long iflag;
dea31012005-04-17 16:05:31 -05001497
James Smart2e0fef82007-06-17 19:56:36 -05001498 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001499 pring->stats.iocb_event++;
1500
dea31012005-04-17 16:05:31 -05001501 /*
1502 * The next available response entry should never exceed the maximum
1503 * entries. If it does, treat it as an adapter hardware error.
1504 */
1505 portRspMax = pring->numRiocb;
1506 portRspPut = le32_to_cpu(pgp->rspPutInx);
1507 if (portRspPut >= portRspMax) {
1508 /*
1509 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1510 * rsp ring <portRspMax>
1511 */
James Smarted957682007-06-17 19:56:37 -05001512 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001513 "0303 Ring %d handler: portRspPut %d "
dea31012005-04-17 16:05:31 -05001514 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001515 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05001516
James Smart2e0fef82007-06-17 19:56:36 -05001517 phba->link_state = LPFC_HBA_ERROR;
1518 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001519
1520 phba->work_hs = HS_FFER3;
1521 lpfc_handle_eratt(phba);
1522
1523 return 1;
1524 }
1525
1526 rmb();
dea31012005-04-17 16:05:31 -05001527 while (pring->rspidx != portRspPut) {
1528 /*
1529 * Build a completion list and call the appropriate handler.
1530 * The process is to get the next available response iocb, get
1531 * a free iocb from the list, copy the response data into the
1532 * free iocb, insert to the continuation list, and update the
1533 * next response index to slim. This process makes response
1534 * iocb's in the ring available to DMA as fast as possible but
1535 * pays a penalty for a copy operation. Since the iocb is
1536 * only 32 bytes, this penalty is considered small relative to
1537 * the PCI reads for register values and a slim write. When
1538 * the ulpLe field is set, the entire Command has been
1539 * received.
1540 */
James Smarted957682007-06-17 19:56:37 -05001541 entry = lpfc_resp_iocb(phba, pring);
1542
James Smart858c9f62007-06-17 19:56:39 -05001543 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05001544 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001545 if (rspiocbp == NULL) {
1546 printk(KERN_ERR "%s: out of buffers! Failing "
1547 "completion.\n", __FUNCTION__);
1548 break;
1549 }
1550
James Smarted957682007-06-17 19:56:37 -05001551 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
1552 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05001553 irsp = &rspiocbp->iocb;
1554
1555 if (++pring->rspidx >= portRspMax)
1556 pring->rspidx = 0;
1557
James Smarta58cbd52007-08-02 11:09:43 -04001558 if (pring->ringno == LPFC_ELS_RING) {
1559 lpfc_debugfs_slow_ring_trc(phba,
1560 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1561 *(((uint32_t *) irsp) + 4),
1562 *(((uint32_t *) irsp) + 6),
1563 *(((uint32_t *) irsp) + 7));
1564 }
1565
James Smarted957682007-06-17 19:56:37 -05001566 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05001567
1568 if (list_empty(&(pring->iocb_continueq))) {
1569 list_add(&rspiocbp->list, &(pring->iocb_continueq));
1570 } else {
1571 list_add_tail(&rspiocbp->list,
1572 &(pring->iocb_continueq));
1573 }
1574
1575 pring->iocb_continueq_cnt++;
1576 if (irsp->ulpLe) {
1577 /*
1578 * By default, the driver expects to free all resources
1579 * associated with this iocb completion.
1580 */
1581 free_saveq = 1;
1582 saveq = list_get_first(&pring->iocb_continueq,
1583 struct lpfc_iocbq, list);
1584 irsp = &(saveq->iocb);
1585 list_del_init(&pring->iocb_continueq);
1586 pring->iocb_continueq_cnt = 0;
1587
1588 pring->stats.iocb_rsp++;
1589
James Smart92d7f7b2007-06-17 19:56:38 -05001590 /*
1591 * If resource errors reported from HBA, reduce
1592 * queuedepths of the SCSI device.
1593 */
1594 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1595 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1596 spin_unlock_irqrestore(&phba->hbalock, iflag);
1597 lpfc_adjust_queue_depth(phba);
1598 spin_lock_irqsave(&phba->hbalock, iflag);
1599 }
1600
dea31012005-04-17 16:05:31 -05001601 if (irsp->ulpStatus) {
1602 /* Rsp ring <ringno> error: IOCB */
James Smarted957682007-06-17 19:56:37 -05001603 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001604 "0328 Rsp Ring %d error: "
James Smarted957682007-06-17 19:56:37 -05001605 "IOCB Data: "
1606 "x%x x%x x%x x%x "
1607 "x%x x%x x%x x%x "
1608 "x%x x%x x%x x%x "
1609 "x%x x%x x%x x%x\n",
James Smarted957682007-06-17 19:56:37 -05001610 pring->ringno,
1611 irsp->un.ulpWord[0],
1612 irsp->un.ulpWord[1],
1613 irsp->un.ulpWord[2],
1614 irsp->un.ulpWord[3],
1615 irsp->un.ulpWord[4],
1616 irsp->un.ulpWord[5],
1617 *(((uint32_t *) irsp) + 6),
1618 *(((uint32_t *) irsp) + 7),
1619 *(((uint32_t *) irsp) + 8),
1620 *(((uint32_t *) irsp) + 9),
1621 *(((uint32_t *) irsp) + 10),
1622 *(((uint32_t *) irsp) + 11),
1623 *(((uint32_t *) irsp) + 12),
1624 *(((uint32_t *) irsp) + 13),
1625 *(((uint32_t *) irsp) + 14),
1626 *(((uint32_t *) irsp) + 15));
dea31012005-04-17 16:05:31 -05001627 }
1628
1629 /*
1630 * Fetch the IOCB command type and call the correct
1631 * completion routine. Solicited and Unsolicited
1632 * IOCBs on the ELS ring get freed back to the
1633 * lpfc_iocb_list by the discovery kernel thread.
1634 */
1635 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
1636 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
1637 if (type == LPFC_SOL_IOCB) {
James Smart2e0fef82007-06-17 19:56:36 -05001638 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05001639 iflag);
1640 rc = lpfc_sli_process_sol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05001641 saveq);
1642 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001643 } else if (type == LPFC_UNSOL_IOCB) {
James Smart2e0fef82007-06-17 19:56:36 -05001644 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05001645 iflag);
1646 rc = lpfc_sli_process_unsol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05001647 saveq);
1648 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001649 } else if (type == LPFC_ABORT_IOCB) {
1650 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
1651 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05001652 lpfc_sli_iocbq_lookup(phba, pring,
1653 saveq)))) {
dea31012005-04-17 16:05:31 -05001654 /* Call the specified completion
1655 routine */
1656 if (cmdiocbp->iocb_cmpl) {
1657 spin_unlock_irqrestore(
James Smart2e0fef82007-06-17 19:56:36 -05001658 &phba->hbalock,
dea31012005-04-17 16:05:31 -05001659 iflag);
1660 (cmdiocbp->iocb_cmpl) (phba,
1661 cmdiocbp, saveq);
1662 spin_lock_irqsave(
James Smart2e0fef82007-06-17 19:56:36 -05001663 &phba->hbalock,
dea31012005-04-17 16:05:31 -05001664 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001665 } else
James Smart2e0fef82007-06-17 19:56:36 -05001666 __lpfc_sli_release_iocbq(phba,
James Bottomley604a3e32005-10-29 10:28:33 -05001667 cmdiocbp);
dea31012005-04-17 16:05:31 -05001668 }
1669 } else if (type == LPFC_UNKNOWN_IOCB) {
1670 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1671
1672 char adaptermsg[LPFC_MAX_ADPTMSG];
1673
1674 memset(adaptermsg, 0,
1675 LPFC_MAX_ADPTMSG);
1676 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1677 MAX_MSG_DATA);
1678 dev_warn(&((phba->pcidev)->dev),
Joe Perches898eb712007-10-18 03:06:30 -07001679 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05001680 phba->brd_no, adaptermsg);
1681 } else {
1682 /* Unknown IOCB command */
James Smart92d7f7b2007-06-17 19:56:38 -05001683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001684 "0335 Unknown IOCB "
James Smart92d7f7b2007-06-17 19:56:38 -05001685 "command Data: x%x "
1686 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001687 irsp->ulpCommand,
1688 irsp->ulpStatus,
1689 irsp->ulpIoTag,
1690 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05001691 }
1692 }
1693
1694 if (free_saveq) {
James Smart2e0fef82007-06-17 19:56:36 -05001695 list_for_each_entry_safe(rspiocbp, next_iocb,
1696 &saveq->list, list) {
1697 list_del(&rspiocbp->list);
1698 __lpfc_sli_release_iocbq(phba,
1699 rspiocbp);
dea31012005-04-17 16:05:31 -05001700 }
James Smart2e0fef82007-06-17 19:56:36 -05001701 __lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05001702 }
James Smart92d7f7b2007-06-17 19:56:38 -05001703 rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05001704 }
1705
1706 /*
1707 * If the port response put pointer has not been updated, sync
1708 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
1709 * response put pointer.
1710 */
1711 if (pring->rspidx == portRspPut) {
1712 portRspPut = le32_to_cpu(pgp->rspPutInx);
1713 }
1714 } /* while (pring->rspidx != portRspPut) */
1715
James Smart92d7f7b2007-06-17 19:56:38 -05001716 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05001717 /* At least one response entry has been freed */
1718 pring->stats.iocb_rsp_full++;
1719 /* SET RxRE_RSP in Chip Att register */
1720 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1721 writel(status, phba->CAregaddr);
1722 readl(phba->CAregaddr); /* flush */
1723 }
1724 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1725 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1726 pring->stats.iocb_cmd_empty++;
1727
1728 /* Force update of the local copy of cmdGetInx */
1729 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1730 lpfc_sli_resume_iocb(phba, pring);
1731
1732 if ((pring->lpfc_sli_cmd_available))
1733 (pring->lpfc_sli_cmd_available) (phba, pring);
1734
1735 }
1736
James Smart2e0fef82007-06-17 19:56:36 -05001737 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001738 return rc;
1739}
1740
James Smart2e0fef82007-06-17 19:56:36 -05001741void
dea31012005-04-17 16:05:31 -05001742lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1743{
James Smart2534ba72007-04-25 09:52:20 -04001744 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001745 struct lpfc_iocbq *iocb, *next_iocb;
James Smart2534ba72007-04-25 09:52:20 -04001746 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05001747
James Smart92d7f7b2007-06-17 19:56:38 -05001748 if (pring->ringno == LPFC_ELS_RING) {
1749 lpfc_fabric_abort_hba(phba);
1750 }
1751
dea31012005-04-17 16:05:31 -05001752 /* Error everything on txq and txcmplq
1753 * First do the txq.
1754 */
James Smart2e0fef82007-06-17 19:56:36 -05001755 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04001756 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05001757 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05001758
1759 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04001760 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
1761 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1762
James Smart2e0fef82007-06-17 19:56:36 -05001763 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04001764
1765 while (!list_empty(&completions)) {
1766 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
dea31012005-04-17 16:05:31 -05001767 cmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001768 list_del_init(&iocb->list);
dea31012005-04-17 16:05:31 -05001769
James Smart2e0fef82007-06-17 19:56:36 -05001770 if (!iocb->iocb_cmpl)
1771 lpfc_sli_release_iocbq(phba, iocb);
1772 else {
dea31012005-04-17 16:05:31 -05001773 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1774 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
dea31012005-04-17 16:05:31 -05001775 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05001776 }
dea31012005-04-17 16:05:31 -05001777 }
dea31012005-04-17 16:05:31 -05001778}
1779
Jamie Wellnitz41415862006-02-28 19:25:27 -05001780int
James Smart2e0fef82007-06-17 19:56:36 -05001781lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05001782{
Jamie Wellnitz41415862006-02-28 19:25:27 -05001783 uint32_t status;
1784 int i = 0;
1785 int retval = 0;
dea31012005-04-17 16:05:31 -05001786
Jamie Wellnitz41415862006-02-28 19:25:27 -05001787 /* Read the HBA Host Status Register */
1788 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05001789
Jamie Wellnitz41415862006-02-28 19:25:27 -05001790 /*
1791 * Check status register every 100ms for 5 retries, then every
1792 * 500ms for 5, then every 2.5 sec for 5, then reset board and
1793 * every 2.5 sec for 4.
1794 * Break our of the loop if errors occurred during init.
1795 */
1796 while (((status & mask) != mask) &&
1797 !(status & HS_FFERM) &&
1798 i++ < 20) {
dea31012005-04-17 16:05:31 -05001799
Jamie Wellnitz41415862006-02-28 19:25:27 -05001800 if (i <= 5)
1801 msleep(10);
1802 else if (i <= 10)
1803 msleep(500);
1804 else
1805 msleep(2500);
dea31012005-04-17 16:05:31 -05001806
Jamie Wellnitz41415862006-02-28 19:25:27 -05001807 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05001808 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05001809 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001810 lpfc_sli_brdrestart(phba);
1811 }
1812 /* Read the HBA Host Status Register */
1813 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05001814 }
dea31012005-04-17 16:05:31 -05001815
Jamie Wellnitz41415862006-02-28 19:25:27 -05001816 /* Check to see if any errors occurred during init */
1817 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05001818 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001819 retval = 1;
1820 }
dea31012005-04-17 16:05:31 -05001821
Jamie Wellnitz41415862006-02-28 19:25:27 -05001822 return retval;
dea31012005-04-17 16:05:31 -05001823}
1824
James Smart92908312006-03-07 15:04:13 -05001825#define BARRIER_TEST_PATTERN (0xdeadbeef)
1826
James Smart2e0fef82007-06-17 19:56:36 -05001827void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05001828{
James Smart65a29c12006-07-06 15:50:50 -04001829 uint32_t __iomem *resp_buf;
1830 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05001831 volatile uint32_t mbox;
1832 uint32_t hc_copy;
1833 int i;
1834 uint8_t hdrtype;
1835
1836 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
1837 if (hdrtype != 0x80 ||
1838 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
1839 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
1840 return;
1841
1842 /*
1843 * Tell the other part of the chip to suspend temporarily all
1844 * its DMA activity.
1845 */
James Smart65a29c12006-07-06 15:50:50 -04001846 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05001847
1848 /* Disable the error attention */
1849 hc_copy = readl(phba->HCregaddr);
1850 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
1851 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001852 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05001853
1854 if (readl(phba->HAregaddr) & HA_ERATT) {
1855 /* Clear Chip error bit */
1856 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001857 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05001858 }
1859
1860 mbox = 0;
1861 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
1862 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
1863
1864 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04001865 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05001866 writel(mbox, mbox_buf);
1867
1868 for (i = 0;
1869 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
1870 mdelay(1);
1871
1872 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
1873 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05001874 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05001875 goto restore_hc;
1876 else
1877 goto clear_errat;
1878 }
1879
1880 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
1881 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
1882 mdelay(1);
1883
1884clear_errat:
1885
1886 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
1887 mdelay(1);
1888
1889 if (readl(phba->HAregaddr) & HA_ERATT) {
1890 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001891 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05001892 }
1893
1894restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05001895 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05001896 writel(hc_copy, phba->HCregaddr);
1897 readl(phba->HCregaddr); /* flush */
1898}
1899
Jamie Wellnitz41415862006-02-28 19:25:27 -05001900int
James Smart2e0fef82007-06-17 19:56:36 -05001901lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001902{
Jamie Wellnitz41415862006-02-28 19:25:27 -05001903 struct lpfc_sli *psli;
1904 LPFC_MBOXQ_t *pmb;
1905 uint32_t status;
1906 uint32_t ha_copy;
1907 int retval;
1908 int i = 0;
1909
1910 psli = &phba->sli;
1911
1912 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05001913 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001914 "0329 Kill HBA Data: x%x x%x\n",
1915 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001916
1917 if ((pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
James Smart92908312006-03-07 15:04:13 -05001918 GFP_KERNEL)) == 0)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001919 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001920
1921 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05001922 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001923 status = readl(phba->HCregaddr);
1924 status &= ~HC_ERINT_ENA;
1925 writel(status, phba->HCregaddr);
1926 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001927 phba->link_flag |= LS_IGNORE_ERATT;
1928 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001929
1930 lpfc_kill_board(phba, pmb);
1931 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1932 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1933
1934 if (retval != MBX_SUCCESS) {
1935 if (retval != MBX_BUSY)
1936 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05001937 spin_lock_irq(&phba->hbalock);
1938 phba->link_flag &= ~LS_IGNORE_ERATT;
1939 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001940 return 1;
1941 }
1942
James Smart92908312006-03-07 15:04:13 -05001943 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
1944
Jamie Wellnitz41415862006-02-28 19:25:27 -05001945 mempool_free(pmb, phba->mbox_mem_pool);
1946
1947 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
1948 * attention every 100ms for 3 seconds. If we don't get ERATT after
1949 * 3 seconds we still set HBA_ERROR state because the status of the
1950 * board is now undefined.
1951 */
1952 ha_copy = readl(phba->HAregaddr);
1953
1954 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
1955 mdelay(100);
1956 ha_copy = readl(phba->HAregaddr);
1957 }
1958
1959 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05001960 if (ha_copy & HA_ERATT) {
1961 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001962 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05001963 }
James Smart2e0fef82007-06-17 19:56:36 -05001964 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001965 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001966 phba->link_flag &= ~LS_IGNORE_ERATT;
1967 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001968
1969 psli->mbox_active = NULL;
1970 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001971 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001972
James Smart2e0fef82007-06-17 19:56:36 -05001973 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001974}
1975
1976int
James Smart2e0fef82007-06-17 19:56:36 -05001977lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001978{
1979 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05001980 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001981 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05001982 int i;
dea31012005-04-17 16:05:31 -05001983
Jamie Wellnitz41415862006-02-28 19:25:27 -05001984 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001985
Jamie Wellnitz41415862006-02-28 19:25:27 -05001986 /* Reset HBA */
1987 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001988 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05001989 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05001990
1991 /* perform board reset */
1992 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001993 phba->pport->fc_myDID = 0;
1994 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05001995
Jamie Wellnitz41415862006-02-28 19:25:27 -05001996 /* Turn off parity checking and serr during the physical reset */
1997 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
1998 pci_write_config_word(phba->pcidev, PCI_COMMAND,
1999 (cfg_value &
2000 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2001
James Smart1c067a42006-08-01 07:33:52 -04002002 psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002003 /* Now toggle INITFF bit in the Host Control Register */
2004 writel(HC_INITFF, phba->HCregaddr);
2005 mdelay(1);
2006 readl(phba->HCregaddr); /* flush */
2007 writel(0, phba->HCregaddr);
2008 readl(phba->HCregaddr); /* flush */
2009
2010 /* Restore PCI cmd register */
2011 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002012
2013 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002014 for (i = 0; i < psli->num_rings; i++) {
2015 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002016 pring->flag = 0;
2017 pring->rspidx = 0;
2018 pring->next_cmdidx = 0;
2019 pring->local_getidx = 0;
2020 pring->cmdidx = 0;
2021 pring->missbufcnt = 0;
2022 }
dea31012005-04-17 16:05:31 -05002023
James Smart2e0fef82007-06-17 19:56:36 -05002024 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002025 return 0;
2026}
2027
2028int
James Smart2e0fef82007-06-17 19:56:36 -05002029lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002030{
2031 MAILBOX_t *mb;
2032 struct lpfc_sli *psli;
2033 uint16_t skip_post;
2034 volatile uint32_t word0;
2035 void __iomem *to_slim;
2036
James Smart2e0fef82007-06-17 19:56:36 -05002037 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002038
2039 psli = &phba->sli;
2040
2041 /* Restart HBA */
2042 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002043 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002044 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002045
2046 word0 = 0;
2047 mb = (MAILBOX_t *) &word0;
2048 mb->mbxCommand = MBX_RESTART;
2049 mb->mbxHc = 1;
2050
James Smart92908312006-03-07 15:04:13 -05002051 lpfc_reset_barrier(phba);
2052
Jamie Wellnitz41415862006-02-28 19:25:27 -05002053 to_slim = phba->MBslimaddr;
2054 writel(*(uint32_t *) mb, to_slim);
2055 readl(to_slim); /* flush */
2056
2057 /* Only skip post after fc_ffinit is completed */
James Smart2e0fef82007-06-17 19:56:36 -05002058 if (phba->pport->port_state) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002059 skip_post = 1;
2060 word0 = 1; /* This is really setting up word1 */
dea31012005-04-17 16:05:31 -05002061 } else {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002062 skip_post = 0;
2063 word0 = 0; /* This is really setting up word1 */
2064 }
James Smart65a29c12006-07-06 15:50:50 -04002065 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002066 writel(*(uint32_t *) mb, to_slim);
2067 readl(to_slim); /* flush */
2068
2069 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002070 phba->pport->stopped = 0;
2071 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002072
James Smart2e0fef82007-06-17 19:56:36 -05002073 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002074
James Smart64ba8812006-08-02 15:24:34 -04002075 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2076 psli->stats_start = get_seconds();
2077
Jamie Wellnitz41415862006-02-28 19:25:27 -05002078 if (skip_post)
2079 mdelay(100);
2080 else
dea31012005-04-17 16:05:31 -05002081 mdelay(2000);
dea31012005-04-17 16:05:31 -05002082
Jamie Wellnitz41415862006-02-28 19:25:27 -05002083 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002084
2085 return 0;
2086}
2087
2088static int
2089lpfc_sli_chipset_init(struct lpfc_hba *phba)
2090{
2091 uint32_t status, i = 0;
2092
2093 /* Read the HBA Host Status Register */
2094 status = readl(phba->HSregaddr);
2095
2096 /* Check status register to see what current state is */
2097 i = 0;
2098 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2099
2100 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2101 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2102 * 4.
2103 */
2104 if (i++ >= 20) {
2105 /* Adapter failed to init, timeout, status reg
2106 <status> */
James Smarted957682007-06-17 19:56:37 -05002107 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002108 "0436 Adapter failed to init, "
2109 "timeout, status reg x%x\n", status);
James Smart2e0fef82007-06-17 19:56:36 -05002110 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002111 return -ETIMEDOUT;
2112 }
2113
2114 /* Check to see if any errors occurred during init */
2115 if (status & HS_FFERM) {
2116 /* ERROR: During chipset initialization */
2117 /* Adapter failed to init, chipset, status reg
2118 <status> */
James Smarted957682007-06-17 19:56:37 -05002119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002120 "0437 Adapter failed to init, "
2121 "chipset, status reg x%x\n", status);
James Smart2e0fef82007-06-17 19:56:36 -05002122 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002123 return -EIO;
2124 }
2125
2126 if (i <= 5) {
2127 msleep(10);
2128 } else if (i <= 10) {
2129 msleep(500);
2130 } else {
2131 msleep(2500);
2132 }
2133
2134 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002135 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002136 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002137 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002138 }
2139 /* Read the HBA Host Status Register */
2140 status = readl(phba->HSregaddr);
2141 }
2142
2143 /* Check to see if any errors occurred during init */
2144 if (status & HS_FFERM) {
2145 /* ERROR: During chipset initialization */
2146 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05002147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002148 "0438 Adapter failed to init, chipset, "
2149 "status reg x%x\n", status);
James Smart2e0fef82007-06-17 19:56:36 -05002150 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002151 return -EIO;
2152 }
2153
2154 /* Clear all interrupt enable conditions */
2155 writel(0, phba->HCregaddr);
2156 readl(phba->HCregaddr); /* flush */
2157
2158 /* setup host attn register */
2159 writel(0xffffffff, phba->HAregaddr);
2160 readl(phba->HAregaddr); /* flush */
2161 return 0;
2162}
2163
James Smart78b2d852007-08-02 11:10:21 -04002164int
James Smarted957682007-06-17 19:56:37 -05002165lpfc_sli_hbq_count(void)
2166{
James Smart92d7f7b2007-06-17 19:56:38 -05002167 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05002168}
2169
2170static int
2171lpfc_sli_hbq_entry_count(void)
2172{
2173 int hbq_count = lpfc_sli_hbq_count();
2174 int count = 0;
2175 int i;
2176
2177 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05002178 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05002179 return count;
2180}
2181
dea31012005-04-17 16:05:31 -05002182int
James Smarted957682007-06-17 19:56:37 -05002183lpfc_sli_hbq_size(void)
2184{
2185 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2186}
2187
2188static int
2189lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2190{
2191 int hbq_count = lpfc_sli_hbq_count();
2192 LPFC_MBOXQ_t *pmb;
2193 MAILBOX_t *pmbox;
2194 uint32_t hbqno;
2195 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05002196
James Smart92d7f7b2007-06-17 19:56:38 -05002197 /* Get a Mailbox buffer to setup mailbox
2198 * commands for HBA initialization
2199 */
James Smarted957682007-06-17 19:56:37 -05002200 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2201
2202 if (!pmb)
2203 return -ENOMEM;
2204
2205 pmbox = &pmb->mb;
2206
2207 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2208 phba->link_state = LPFC_INIT_MBX_CMDS;
2209
2210 hbq_entry_index = 0;
2211 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2212 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2213 phba->hbqs[hbqno].hbqPutIdx = 0;
2214 phba->hbqs[hbqno].local_hbqGetIdx = 0;
2215 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05002216 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04002217 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2218 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05002219 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2220
2221 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2222 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2223 mbxStatus <status>, ring <num> */
2224
2225 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002226 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002227 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05002228 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002229 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05002230 pmbox->mbxStatus, hbqno);
2231
2232 phba->link_state = LPFC_HBA_ERROR;
2233 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05002234 return ENXIO;
2235 }
2236 }
2237 phba->hbq_count = hbq_count;
2238
James Smarted957682007-06-17 19:56:37 -05002239 mempool_free(pmb, phba->mbox_mem_pool);
2240
James Smart92d7f7b2007-06-17 19:56:38 -05002241 /* Initially populate or replenish the HBQs */
2242 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2243 if (lpfc_sli_hbqbuf_init_hbqs(phba, hbqno))
2244 return -ENOMEM;
2245 }
James Smarted957682007-06-17 19:56:37 -05002246 return 0;
2247}
2248
2249static int
2250lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05002251{
2252 LPFC_MBOXQ_t *pmb;
2253 uint32_t resetcount = 0, rc = 0, done = 0;
2254
2255 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2256 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05002257 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002258 return -ENOMEM;
2259 }
2260
James Smarted957682007-06-17 19:56:37 -05002261 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05002262 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05002263 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002264 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002265 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002266 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002267 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002268 msleep(2500);
2269 rc = lpfc_sli_chipset_init(phba);
2270 if (rc)
2271 break;
2272
James Smart2e0fef82007-06-17 19:56:36 -05002273 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002274 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002275 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002276 resetcount++;
2277
James Smarted957682007-06-17 19:56:37 -05002278 /* Call pre CONFIG_PORT mailbox command initialization. A
2279 * value of 0 means the call was successful. Any other
2280 * nonzero value is a failure, but if ERESTART is returned,
2281 * the driver may reset the HBA and try again.
2282 */
dea31012005-04-17 16:05:31 -05002283 rc = lpfc_config_port_prep(phba);
2284 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05002285 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05002286 continue;
2287 } else if (rc) {
2288 break;
2289 }
2290
James Smart2e0fef82007-06-17 19:56:36 -05002291 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05002292 lpfc_config_port(phba, pmb);
2293 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smarted957682007-06-17 19:56:37 -05002294 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05002295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002296 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002297 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002298 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05002299 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002300 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002301 spin_unlock_irq(&phba->hbalock);
2302 rc = -ENXIO;
James Smarted957682007-06-17 19:56:37 -05002303 } else {
2304 done = 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002305 phba->max_vpi = (phba->max_vpi &&
2306 pmb->mb.un.varCfgPort.gmv) != 0
2307 ? pmb->mb.un.varCfgPort.max_vpi
2308 : 0;
dea31012005-04-17 16:05:31 -05002309 }
2310 }
James Smarted957682007-06-17 19:56:37 -05002311
2312 if (!done) {
2313 rc = -EINVAL;
2314 goto do_prep_failed;
2315 }
2316
2317 if ((pmb->mb.un.varCfgPort.sli_mode == 3) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002318 (!pmb->mb.un.varCfgPort.cMA)) {
James Smarted957682007-06-17 19:56:37 -05002319 rc = -ENXIO;
2320 goto do_prep_failed;
2321 }
2322 return rc;
2323
James Smart92d7f7b2007-06-17 19:56:38 -05002324do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05002325 mempool_free(pmb, phba->mbox_mem_pool);
2326 return rc;
2327}
2328
2329int
2330lpfc_sli_hba_setup(struct lpfc_hba *phba)
2331{
2332 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002333 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05002334
2335 switch (lpfc_sli_mode) {
2336 case 2:
James Smart78b2d852007-08-02 11:10:21 -04002337 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05002338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002339 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05002340 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04002341 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05002342 break;
2343 }
James Smarted957682007-06-17 19:56:37 -05002344 mode = 2;
2345 break;
2346 case 0:
2347 case 3:
2348 break;
2349 default:
James Smart92d7f7b2007-06-17 19:56:38 -05002350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002351 "1819 Unrecognized lpfc_sli_mode "
2352 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05002353
2354 break;
2355 }
2356
2357 rc = lpfc_do_config_port(phba, mode);
2358 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05002359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002360 "1820 Unable to select SLI-3. "
2361 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05002362 if (rc && mode != 2)
2363 rc = lpfc_do_config_port(phba, 2);
2364 if (rc)
dea31012005-04-17 16:05:31 -05002365 goto lpfc_sli_hba_setup_error;
2366
James Smarted957682007-06-17 19:56:37 -05002367 if (phba->sli_rev == 3) {
2368 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
2369 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
2370 phba->sli3_options |= LPFC_SLI3_ENABLED;
2371 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
2372
2373 } else {
2374 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
2375 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05002376 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05002377 }
2378
2379 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002380 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
2381 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05002382 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05002383
2384 if (rc)
2385 goto lpfc_sli_hba_setup_error;
2386
James Smart92d7f7b2007-06-17 19:56:38 -05002387 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05002388
2389 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2390 rc = lpfc_sli_hbq_setup(phba);
2391 if (rc)
2392 goto lpfc_sli_hba_setup_error;
2393 }
2394
dea31012005-04-17 16:05:31 -05002395 phba->sli.sli_flag |= LPFC_PROCESS_LA;
2396
2397 rc = lpfc_config_port_post(phba);
2398 if (rc)
2399 goto lpfc_sli_hba_setup_error;
2400
James Smarted957682007-06-17 19:56:37 -05002401 return rc;
2402
James Smart92d7f7b2007-06-17 19:56:38 -05002403lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05002404 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05002405 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002406 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05002407 return rc;
2408}
2409
dea31012005-04-17 16:05:31 -05002410/*! lpfc_mbox_timeout
2411 *
2412 * \pre
2413 * \post
2414 * \param hba Pointer to per struct lpfc_hba structure
2415 * \param l1 Pointer to the driver's mailbox queue.
2416 * \return
2417 * void
2418 *
2419 * \b Description:
2420 *
2421 * This routine handles mailbox timeout events at timer interrupt context.
2422 */
2423void
2424lpfc_mbox_timeout(unsigned long ptr)
2425{
James Smart92d7f7b2007-06-17 19:56:38 -05002426 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05002427 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05002428 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05002429
James Smart2e0fef82007-06-17 19:56:36 -05002430 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05002431 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002432 if (!tmo_posted)
2433 phba->pport->work_port_events |= WORKER_MBOX_TMO;
2434 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
2435
2436 if (!tmo_posted) {
James Smart92d7f7b2007-06-17 19:56:38 -05002437 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002438 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05002439 lpfc_worker_wake_up(phba);
2440 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002441 }
dea31012005-04-17 16:05:31 -05002442}
2443
2444void
2445lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
2446{
James Smart2e0fef82007-06-17 19:56:36 -05002447 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
2448 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04002449 struct lpfc_sli *psli = &phba->sli;
2450 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002451
James Smart2e0fef82007-06-17 19:56:36 -05002452 if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
dea31012005-04-17 16:05:31 -05002453 return;
2454 }
2455
dea31012005-04-17 16:05:31 -05002456 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05002457 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002458 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002459 mb->mbxCommand,
2460 phba->pport->port_state,
2461 phba->sli.sli_flag,
2462 phba->sli.mbox_active);
dea31012005-04-17 16:05:31 -05002463
James Smart1dcb58e2007-04-25 09:51:30 -04002464 /* Setting state unknown so lpfc_sli_abort_iocb_ring
2465 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
2466 * it to fail all oustanding SCSI IO.
2467 */
James Smart2e0fef82007-06-17 19:56:36 -05002468 spin_lock_irq(&phba->pport->work_port_lock);
2469 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
2470 spin_unlock_irq(&phba->pport->work_port_lock);
2471 spin_lock_irq(&phba->hbalock);
2472 phba->link_state = LPFC_LINK_UNKNOWN;
2473 phba->pport->fc_flag |= FC_ESTABLISH_LINK;
James Smart1dcb58e2007-04-25 09:51:30 -04002474 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002475 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04002476
2477 pring = &psli->ring[psli->fcp_ring];
2478 lpfc_sli_abort_iocb_ring(phba, pring);
2479
2480 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002481 "0316 Resetting board due to mailbox timeout\n");
James Smart1dcb58e2007-04-25 09:51:30 -04002482 /*
2483 * lpfc_offline calls lpfc_sli_hba_down which will clean up
2484 * on oustanding mailbox commands.
2485 */
2486 lpfc_offline_prep(phba);
2487 lpfc_offline(phba);
2488 lpfc_sli_brdrestart(phba);
2489 if (lpfc_online(phba) == 0) /* Initialize the HBA */
2490 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
2491 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002492 return;
2493}
2494
2495int
James Smart2e0fef82007-06-17 19:56:36 -05002496lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
dea31012005-04-17 16:05:31 -05002497{
dea31012005-04-17 16:05:31 -05002498 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05002499 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002500 uint32_t status, evtctr;
2501 uint32_t ha_copy;
2502 int i;
2503 unsigned long drvr_flag = 0;
2504 volatile uint32_t word0, ldata;
2505 void __iomem *to_slim;
2506
James Smarted957682007-06-17 19:56:37 -05002507 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05002508 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05002509 if(!pmbox->vport) {
2510 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002511 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002512 "1806 Mbox x%x failed. No vport\n",
James Smarted957682007-06-17 19:56:37 -05002513 pmbox->mb.mbxCommand);
2514 dump_stack();
2515 return MBXERR_ERROR;
2516 }
2517 }
2518
James Smart92d7f7b2007-06-17 19:56:38 -05002519
Linas Vepstas8d63f372007-02-14 14:28:36 -06002520 /* If the PCI channel is in offline state, do not post mbox. */
2521 if (unlikely(pci_channel_offline(phba->pcidev)))
2522 return MBX_NOT_FINISHED;
2523
James Smart2e0fef82007-06-17 19:56:36 -05002524 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002525 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05002526
2527
dea31012005-04-17 16:05:31 -05002528 mb = &pmbox->mb;
2529 status = MBX_SUCCESS;
2530
James Smart2e0fef82007-06-17 19:56:36 -05002531 if (phba->link_state == LPFC_HBA_ERROR) {
2532 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002533
2534 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002535 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
James Smart2e0fef82007-06-17 19:56:36 -05002536 return MBX_NOT_FINISHED;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002537 }
2538
James Smart92908312006-03-07 15:04:13 -05002539 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
2540 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05002541 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart92d7f7b2007-06-17 19:56:38 -05002542 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
James Smart2e0fef82007-06-17 19:56:36 -05002543 return MBX_NOT_FINISHED;
James Smart92908312006-03-07 15:04:13 -05002544 }
2545
dea31012005-04-17 16:05:31 -05002546 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
2547 /* Polling for a mbox command when another one is already active
2548 * is not allowed in SLI. Also, the driver must have established
2549 * SLI2 mode to queue and process multiple mbox commands.
2550 */
2551
2552 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05002553 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002554
2555 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002556 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart2e0fef82007-06-17 19:56:36 -05002557 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05002558 }
2559
2560 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002561 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002562 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002563 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart2e0fef82007-06-17 19:56:36 -05002564 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05002565 }
2566
dea31012005-04-17 16:05:31 -05002567 /* Another mailbox command is still being processed, queue this
2568 * command to be processed later.
2569 */
2570 lpfc_mbox_put(phba, pmbox);
2571
2572 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05002573 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002574 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002575 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002576 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
2577 mb->mbxCommand, phba->pport->port_state,
2578 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05002579
2580 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05002581 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002582
James Smart858c9f62007-06-17 19:56:39 -05002583 if (pmbox->vport) {
2584 lpfc_debugfs_disc_trc(pmbox->vport,
2585 LPFC_DISC_TRC_MBOX_VPORT,
2586 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
2587 (uint32_t)mb->mbxCommand,
2588 mb->un.varWords[0], mb->un.varWords[1]);
2589 }
2590 else {
2591 lpfc_debugfs_disc_trc(phba->pport,
2592 LPFC_DISC_TRC_MBOX,
2593 "MBOX Bsy: cmd:x%x mb:x%x x%x",
2594 (uint32_t)mb->mbxCommand,
2595 mb->un.varWords[0], mb->un.varWords[1]);
2596 }
2597
James Smart2e0fef82007-06-17 19:56:36 -05002598 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05002599 }
2600
dea31012005-04-17 16:05:31 -05002601 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2602
2603 /* If we are not polling, we MUST be in SLI2 mode */
2604 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002605 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
2606 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05002607 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002608 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002609 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002610 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart2e0fef82007-06-17 19:56:36 -05002611 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05002612 }
2613 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04002614 mod_timer(&psli->mbox_tmo, (jiffies +
2615 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05002616 }
2617
2618 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05002619 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002620 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002621 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002622 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05002623 mb->mbxCommand, phba->pport->port_state,
2624 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05002625
James Smart858c9f62007-06-17 19:56:39 -05002626 if (mb->mbxCommand != MBX_HEARTBEAT) {
2627 if (pmbox->vport) {
2628 lpfc_debugfs_disc_trc(pmbox->vport,
2629 LPFC_DISC_TRC_MBOX_VPORT,
2630 "MBOX Send vport: cmd:x%x mb:x%x x%x",
2631 (uint32_t)mb->mbxCommand,
2632 mb->un.varWords[0], mb->un.varWords[1]);
2633 }
2634 else {
2635 lpfc_debugfs_disc_trc(phba->pport,
2636 LPFC_DISC_TRC_MBOX,
2637 "MBOX Send: cmd:x%x mb:x%x x%x",
2638 (uint32_t)mb->mbxCommand,
2639 mb->un.varWords[0], mb->un.varWords[1]);
2640 }
2641 }
2642
dea31012005-04-17 16:05:31 -05002643 psli->slistat.mbox_cmd++;
2644 evtctr = psli->slistat.mbox_event;
2645
2646 /* next set own bit for the adapter and copy over command word */
2647 mb->mbxOwner = OWN_CHIP;
2648
2649 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002650 /* First copy command data to host SLIM area */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002651 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002652 } else {
James Smart92908312006-03-07 15:04:13 -05002653 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05002654 /* copy command data into host mbox for cmpl */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002655 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
James Smart92d7f7b2007-06-17 19:56:38 -05002656 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002657 }
2658
2659 /* First copy mbox command data to HBA SLIM, skip past first
2660 word */
2661 to_slim = phba->MBslimaddr + sizeof (uint32_t);
2662 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
2663 MAILBOX_CMD_SIZE - sizeof (uint32_t));
2664
2665 /* Next copy over first word, with mbxOwner set */
2666 ldata = *((volatile uint32_t *)mb);
2667 to_slim = phba->MBslimaddr;
2668 writel(ldata, to_slim);
2669 readl(to_slim); /* flush */
2670
2671 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2672 /* switch over to host mailbox */
2673 psli->sli_flag |= LPFC_SLI2_ACTIVE;
2674 }
2675 }
2676
2677 wmb();
2678 /* interrupt board to doit right away */
2679 writel(CA_MBATT, phba->CAregaddr);
2680 readl(phba->CAregaddr); /* flush */
2681
2682 switch (flag) {
2683 case MBX_NOWAIT:
2684 /* Don't wait for it to finish, just return */
2685 psli->mbox_active = pmbox;
2686 break;
2687
2688 case MBX_POLL:
dea31012005-04-17 16:05:31 -05002689 psli->mbox_active = NULL;
2690 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2691 /* First read mbox status word */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002692 word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002693 word0 = le32_to_cpu(word0);
2694 } else {
2695 /* First read mbox status word */
2696 word0 = readl(phba->MBslimaddr);
2697 }
2698
2699 /* Read the HBA Host Attention Register */
2700 ha_copy = readl(phba->HAregaddr);
2701
James Smarta309a6b2006-08-01 07:33:43 -04002702 i = lpfc_mbox_tmo_val(phba, mb->mbxCommand);
2703 i *= 1000; /* Convert to ms */
2704
dea31012005-04-17 16:05:31 -05002705 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002706 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
2707 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05002708 (phba->link_state > LPFC_WARM_START))) {
James Smarta309a6b2006-08-01 07:33:43 -04002709 if (i-- <= 0) {
dea31012005-04-17 16:05:31 -05002710 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002711 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05002712 drvr_flag);
James Smart2e0fef82007-06-17 19:56:36 -05002713 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05002714 }
2715
2716 /* Check if we took a mbox interrupt while we were
2717 polling */
2718 if (((word0 & OWN_CHIP) != OWN_CHIP)
2719 && (evtctr != psli->slistat.mbox_event))
2720 break;
2721
James Smart2e0fef82007-06-17 19:56:36 -05002722 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05002723 drvr_flag);
2724
James Smart1dcb58e2007-04-25 09:51:30 -04002725 msleep(1);
dea31012005-04-17 16:05:31 -05002726
James Smart2e0fef82007-06-17 19:56:36 -05002727 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002728
2729 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2730 /* First copy command data */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002731 word0 = *((volatile uint32_t *)
2732 &phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002733 word0 = le32_to_cpu(word0);
2734 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2735 MAILBOX_t *slimmb;
2736 volatile uint32_t slimword0;
2737 /* Check real SLIM for any errors */
2738 slimword0 = readl(phba->MBslimaddr);
2739 slimmb = (MAILBOX_t *) & slimword0;
2740 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
2741 && slimmb->mbxStatus) {
2742 psli->sli_flag &=
2743 ~LPFC_SLI2_ACTIVE;
2744 word0 = slimword0;
2745 }
2746 }
2747 } else {
2748 /* First copy command data */
2749 word0 = readl(phba->MBslimaddr);
2750 }
2751 /* Read the HBA Host Attention Register */
2752 ha_copy = readl(phba->HAregaddr);
2753 }
2754
2755 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002756 /* copy results back to user */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002757 lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
James Smart92d7f7b2007-06-17 19:56:38 -05002758 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002759 } else {
2760 /* First copy command data */
2761 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2762 MAILBOX_CMD_SIZE);
2763 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2764 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05002765 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05002766 phba->MBslimaddr + DMP_RSP_OFFSET,
2767 mb->un.varDmp.word_cnt);
2768 }
2769 }
2770
2771 writel(HA_MBATT, phba->HAregaddr);
2772 readl(phba->HAregaddr); /* flush */
2773
2774 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2775 status = mb->mbxStatus;
2776 }
2777
James Smart2e0fef82007-06-17 19:56:36 -05002778 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2779 return status;
dea31012005-04-17 16:05:31 -05002780}
2781
James Smart92d7f7b2007-06-17 19:56:38 -05002782/*
2783 * Caller needs to hold lock.
2784 */
James Smart858c9f62007-06-17 19:56:39 -05002785static void
James Smart92d7f7b2007-06-17 19:56:38 -05002786__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05002787 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05002788{
2789 /* Insert the caller's iocb in the txq tail for later processing. */
2790 list_add_tail(&piocb->list, &pring->txq);
2791 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05002792}
2793
2794static struct lpfc_iocbq *
2795lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05002796 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05002797{
2798 struct lpfc_iocbq * nextiocb;
2799
2800 nextiocb = lpfc_sli_ringtx_get(phba, pring);
2801 if (!nextiocb) {
2802 nextiocb = *piocb;
2803 *piocb = NULL;
2804 }
2805
2806 return nextiocb;
2807}
2808
James Smart92d7f7b2007-06-17 19:56:38 -05002809/*
2810 * Lockless version of lpfc_sli_issue_iocb.
2811 */
dea31012005-04-17 16:05:31 -05002812int
James Smart92d7f7b2007-06-17 19:56:38 -05002813__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002814 struct lpfc_iocbq *piocb, uint32_t flag)
2815{
2816 struct lpfc_iocbq *nextiocb;
2817 IOCB_t *iocb;
2818
James Smart92d7f7b2007-06-17 19:56:38 -05002819 if (piocb->iocb_cmpl && (!piocb->vport) &&
2820 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2821 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
2822 lpfc_printf_log(phba, KERN_ERR,
2823 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002824 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002825 piocb->iocb.ulpCommand);
2826 dump_stack();
2827 return IOCB_ERROR;
2828 }
2829
2830
Linas Vepstas8d63f372007-02-14 14:28:36 -06002831 /* If the PCI channel is in offline state, do not post iocbs. */
2832 if (unlikely(pci_channel_offline(phba->pcidev)))
2833 return IOCB_ERROR;
2834
dea31012005-04-17 16:05:31 -05002835 /*
2836 * We should never get an IOCB if we are in a < LINK_DOWN state
2837 */
James Smart2e0fef82007-06-17 19:56:36 -05002838 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05002839 return IOCB_ERROR;
2840
2841 /*
2842 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04002843 * outstanding event.
dea31012005-04-17 16:05:31 -05002844 */
James Smart0b727fe2007-10-27 13:37:25 -04002845 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05002846 goto iocb_busy;
2847
James Smart2e0fef82007-06-17 19:56:36 -05002848 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05002849 /*
James Smart2680eea2007-04-25 09:52:55 -04002850 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05002851 * can be issued if the link is not up.
2852 */
2853 switch (piocb->iocb.ulpCommand) {
2854 case CMD_QUE_RING_BUF_CN:
2855 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05002856 /*
2857 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
2858 * completion, iocb_cmpl MUST be 0.
2859 */
2860 if (piocb->iocb_cmpl)
2861 piocb->iocb_cmpl = NULL;
2862 /*FALLTHROUGH*/
2863 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04002864 case CMD_CLOSE_XRI_CN:
2865 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05002866 break;
2867 default:
2868 goto iocb_busy;
2869 }
2870
2871 /*
2872 * For FCP commands, we must be in a state where we can process link
2873 * attention events.
2874 */
2875 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05002876 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05002877 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05002878 }
dea31012005-04-17 16:05:31 -05002879
dea31012005-04-17 16:05:31 -05002880 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2881 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
2882 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2883
2884 if (iocb)
2885 lpfc_sli_update_ring(phba, pring);
2886 else
2887 lpfc_sli_update_full_ring(phba, pring);
2888
2889 if (!piocb)
2890 return IOCB_SUCCESS;
2891
2892 goto out_busy;
2893
2894 iocb_busy:
2895 pring->stats.iocb_cmd_delay++;
2896
2897 out_busy:
2898
2899 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002900 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05002901 return IOCB_SUCCESS;
2902 }
2903
2904 return IOCB_BUSY;
2905}
2906
James Smart92d7f7b2007-06-17 19:56:38 -05002907
2908int
2909lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2910 struct lpfc_iocbq *piocb, uint32_t flag)
2911{
2912 unsigned long iflags;
2913 int rc;
2914
2915 spin_lock_irqsave(&phba->hbalock, iflags);
2916 rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
2917 spin_unlock_irqrestore(&phba->hbalock, iflags);
2918
2919 return rc;
2920}
2921
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002922static int
2923lpfc_extra_ring_setup( struct lpfc_hba *phba)
2924{
2925 struct lpfc_sli *psli;
2926 struct lpfc_sli_ring *pring;
2927
2928 psli = &phba->sli;
2929
2930 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05002931
2932 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002933 pring = &psli->ring[psli->fcp_ring];
2934 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2935 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2936 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2937 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2938
James Smarta4bc3372006-12-02 13:34:16 -05002939 /* and give them to the extra ring */
2940 pring = &psli->ring[psli->extra_ring];
2941
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002942 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2943 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2944 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2945 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2946
2947 /* Setup default profile for this ring */
2948 pring->iotag_max = 4096;
2949 pring->num_mask = 1;
2950 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05002951 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
2952 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002953 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
2954 return 0;
2955}
2956
James Smart57127f12007-10-27 13:37:05 -04002957void
2958lpfc_sli_async_event_handler(struct lpfc_hba * phba,
2959 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
2960{
2961 IOCB_t *icmd;
2962 uint16_t evt_code;
2963 uint16_t temp;
2964 struct temp_event temp_event_data;
2965 struct Scsi_Host *shost;
2966
2967 icmd = &iocbq->iocb;
2968 evt_code = icmd->un.asyncstat.evt_code;
2969 temp = icmd->ulpContext;
2970
2971 if ((evt_code != ASYNC_TEMP_WARN) &&
2972 (evt_code != ASYNC_TEMP_SAFE)) {
2973 lpfc_printf_log(phba,
2974 KERN_ERR,
2975 LOG_SLI,
2976 "0327 Ring %d handler: unexpected ASYNC_STATUS"
2977 " evt_code 0x%x\n",
2978 pring->ringno,
2979 icmd->un.asyncstat.evt_code);
2980 return;
2981 }
2982 temp_event_data.data = (uint32_t)temp;
2983 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
2984 if (evt_code == ASYNC_TEMP_WARN) {
2985 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
2986 lpfc_printf_log(phba,
2987 KERN_WARNING,
2988 LOG_TEMP,
2989 "0339 Adapter is very hot, please take "
2990 "corrective action. temperature : %d Celsius\n",
2991 temp);
2992 }
2993 if (evt_code == ASYNC_TEMP_SAFE) {
2994 temp_event_data.event_code = LPFC_NORMAL_TEMP;
2995 lpfc_printf_log(phba,
2996 KERN_INFO,
2997 LOG_TEMP,
2998 "0340 Adapter temperature is OK now. "
2999 "temperature : %d Celsius\n",
3000 temp);
3001 }
3002
3003 /* Send temperature change event to applications */
3004 shost = lpfc_shost_from_vport(phba->pport);
3005 fc_host_post_vendor_event(shost, fc_get_event_number(),
3006 sizeof(temp_event_data), (char *) &temp_event_data,
3007 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
3008
3009}
3010
3011
dea31012005-04-17 16:05:31 -05003012int
3013lpfc_sli_setup(struct lpfc_hba *phba)
3014{
James Smarted957682007-06-17 19:56:37 -05003015 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05003016 struct lpfc_sli *psli = &phba->sli;
3017 struct lpfc_sli_ring *pring;
3018
3019 psli->num_rings = MAX_CONFIGURED_RINGS;
3020 psli->sli_flag = 0;
3021 psli->fcp_ring = LPFC_FCP_RING;
3022 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05003023 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05003024
James Bottomley604a3e32005-10-29 10:28:33 -05003025 psli->iocbq_lookup = NULL;
3026 psli->iocbq_lookup_len = 0;
3027 psli->last_iotag = 0;
3028
dea31012005-04-17 16:05:31 -05003029 for (i = 0; i < psli->num_rings; i++) {
3030 pring = &psli->ring[i];
3031 switch (i) {
3032 case LPFC_FCP_RING: /* ring 0 - FCP */
3033 /* numCiocb and numRiocb are used in config_port */
3034 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3035 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3036 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3037 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3038 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3039 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003040 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003041 SLI3_IOCB_CMD_SIZE :
3042 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003043 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003044 SLI3_IOCB_RSP_SIZE :
3045 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003046 pring->iotag_ctr = 0;
3047 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05003048 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05003049 pring->fast_iotag = pring->iotag_max;
3050 pring->num_mask = 0;
3051 break;
James Smarta4bc3372006-12-02 13:34:16 -05003052 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05003053 /* numCiocb and numRiocb are used in config_port */
3054 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
3055 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003056 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003057 SLI3_IOCB_CMD_SIZE :
3058 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003059 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003060 SLI3_IOCB_RSP_SIZE :
3061 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05003062 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05003063 pring->num_mask = 0;
3064 break;
3065 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
3066 /* numCiocb and numRiocb are used in config_port */
3067 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
3068 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003069 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003070 SLI3_IOCB_CMD_SIZE :
3071 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003072 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003073 SLI3_IOCB_RSP_SIZE :
3074 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003075 pring->fast_iotag = 0;
3076 pring->iotag_ctr = 0;
3077 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04003078 pring->lpfc_sli_rcv_async_status =
3079 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05003080 pring->num_mask = 4;
3081 pring->prt[0].profile = 0; /* Mask 0 */
3082 pring->prt[0].rctl = FC_ELS_REQ;
3083 pring->prt[0].type = FC_ELS_DATA;
3084 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003085 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003086 pring->prt[1].profile = 0; /* Mask 1 */
3087 pring->prt[1].rctl = FC_ELS_RSP;
3088 pring->prt[1].type = FC_ELS_DATA;
3089 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003090 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003091 pring->prt[2].profile = 0; /* Mask 2 */
3092 /* NameServer Inquiry */
3093 pring->prt[2].rctl = FC_UNSOL_CTL;
3094 /* NameServer */
3095 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
3096 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003097 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003098 pring->prt[3].profile = 0; /* Mask 3 */
3099 /* NameServer response */
3100 pring->prt[3].rctl = FC_SOL_CTL;
3101 /* NameServer */
3102 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
3103 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003104 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003105 break;
3106 }
James Smarted957682007-06-17 19:56:37 -05003107 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05003108 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05003109 }
James Smarted957682007-06-17 19:56:37 -05003110 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05003111 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04003112 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
3113 "SLI2 SLIM Data: x%x x%lx\n",
3114 phba->brd_no, totiocbsize,
3115 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05003116 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003117 if (phba->cfg_multi_ring_support == 2)
3118 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05003119
3120 return 0;
3121}
3122
3123int
James Smart2e0fef82007-06-17 19:56:36 -05003124lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003125{
3126 struct lpfc_sli *psli;
3127 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05003128 int i;
dea31012005-04-17 16:05:31 -05003129
3130 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003131 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003132 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05003133 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05003134 /* Initialize list headers for txq and txcmplq as double linked lists */
3135 for (i = 0; i < psli->num_rings; i++) {
3136 pring = &psli->ring[i];
3137 pring->ringno = i;
3138 pring->next_cmdidx = 0;
3139 pring->local_getidx = 0;
3140 pring->cmdidx = 0;
3141 INIT_LIST_HEAD(&pring->txq);
3142 INIT_LIST_HEAD(&pring->txcmplq);
3143 INIT_LIST_HEAD(&pring->iocb_continueq);
3144 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05003145 }
James Smart2e0fef82007-06-17 19:56:36 -05003146 spin_unlock_irq(&phba->hbalock);
3147 return 1;
dea31012005-04-17 16:05:31 -05003148}
3149
3150int
James Smart92d7f7b2007-06-17 19:56:38 -05003151lpfc_sli_host_down(struct lpfc_vport *vport)
3152{
James Smart858c9f62007-06-17 19:56:39 -05003153 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05003154 struct lpfc_hba *phba = vport->phba;
3155 struct lpfc_sli *psli = &phba->sli;
3156 struct lpfc_sli_ring *pring;
3157 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003158 int i;
3159 unsigned long flags = 0;
3160 uint16_t prev_pring_flag;
3161
3162 lpfc_cleanup_discovery_resources(vport);
3163
3164 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05003165 for (i = 0; i < psli->num_rings; i++) {
3166 pring = &psli->ring[i];
3167 prev_pring_flag = pring->flag;
James Smart858c9f62007-06-17 19:56:39 -05003168 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
3169 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05003170 /*
3171 * Error everything on the txq since these iocbs have not been
3172 * given to the FW yet.
3173 */
James Smart92d7f7b2007-06-17 19:56:38 -05003174 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3175 if (iocb->vport != vport)
3176 continue;
James Smart858c9f62007-06-17 19:56:39 -05003177 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05003178 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05003179 }
3180
3181 /* Next issue ABTS for everything on the txcmplq */
3182 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
3183 list) {
3184 if (iocb->vport != vport)
3185 continue;
3186 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3187 }
3188
3189 pring->flag = prev_pring_flag;
3190 }
3191
3192 spin_unlock_irqrestore(&phba->hbalock, flags);
3193
James Smart858c9f62007-06-17 19:56:39 -05003194 while (!list_empty(&completions)) {
3195 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
3196
3197 if (!iocb->iocb_cmpl)
3198 lpfc_sli_release_iocbq(phba, iocb);
3199 else {
3200 iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3201 iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
3202 (iocb->iocb_cmpl) (phba, iocb, iocb);
3203 }
3204 }
James Smart92d7f7b2007-06-17 19:56:38 -05003205 return 1;
3206}
3207
3208int
James Smart2e0fef82007-06-17 19:56:36 -05003209lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003210{
James Smart2534ba72007-04-25 09:52:20 -04003211 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05003212 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003213 struct lpfc_sli_ring *pring;
3214 LPFC_MBOXQ_t *pmb;
James Smart2534ba72007-04-25 09:52:20 -04003215 struct lpfc_iocbq *iocb;
3216 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05003217 int i;
3218 unsigned long flags = 0;
3219
dea31012005-04-17 16:05:31 -05003220 lpfc_hba_down_prep(phba);
3221
James Smart92d7f7b2007-06-17 19:56:38 -05003222 lpfc_fabric_abort_hba(phba);
3223
James Smart2e0fef82007-06-17 19:56:36 -05003224 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05003225 for (i = 0; i < psli->num_rings; i++) {
3226 pring = &psli->ring[i];
James Smart858c9f62007-06-17 19:56:39 -05003227 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
3228 pring->flag |= LPFC_DEFERRED_RING_EVENT;
dea31012005-04-17 16:05:31 -05003229
3230 /*
3231 * Error everything on the txq since these iocbs have not been
3232 * given to the FW yet.
3233 */
James Smart2534ba72007-04-25 09:52:20 -04003234 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003235 pring->txq_cnt = 0;
3236
dea31012005-04-17 16:05:31 -05003237 }
James Smart2e0fef82007-06-17 19:56:36 -05003238 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05003239
James Smart2534ba72007-04-25 09:52:20 -04003240 while (!list_empty(&completions)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003241 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
James Smart2534ba72007-04-25 09:52:20 -04003242 cmd = &iocb->iocb;
James Smart2534ba72007-04-25 09:52:20 -04003243
James Smart2e0fef82007-06-17 19:56:36 -05003244 if (!iocb->iocb_cmpl)
3245 lpfc_sli_release_iocbq(phba, iocb);
3246 else {
James Smart2534ba72007-04-25 09:52:20 -04003247 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3248 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
3249 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05003250 }
James Smart2534ba72007-04-25 09:52:20 -04003251 }
3252
dea31012005-04-17 16:05:31 -05003253 /* Return any active mbox cmds */
3254 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05003255 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05003256
3257 spin_lock(&phba->pport->work_port_lock);
3258 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3259 spin_unlock(&phba->pport->work_port_lock);
3260
3261 if (psli->mbox_active) {
3262 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05003263 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003264 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05003265 }
dea31012005-04-17 16:05:31 -05003266
James Smart92d7f7b2007-06-17 19:56:38 -05003267 /* Return any pending or completed mbox cmds */
3268 list_splice_init(&phba->sli.mboxq, &completions);
3269 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
3270 INIT_LIST_HEAD(&psli->mboxq);
3271 INIT_LIST_HEAD(&psli->mboxq_cmpl);
3272
3273 spin_unlock_irqrestore(&phba->hbalock, flags);
3274
3275 while (!list_empty(&completions)) {
3276 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05003277 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
3278 if (pmb->mbox_cmpl) {
dea31012005-04-17 16:05:31 -05003279 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05003280 }
3281 }
dea31012005-04-17 16:05:31 -05003282 return 1;
3283}
3284
3285void
3286lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
3287{
3288 uint32_t *src = srcp;
3289 uint32_t *dest = destp;
3290 uint32_t ldata;
3291 int i;
3292
3293 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
3294 ldata = *src;
3295 ldata = le32_to_cpu(ldata);
3296 *dest = ldata;
3297 src++;
3298 dest++;
3299 }
3300}
3301
3302int
James Smart2e0fef82007-06-17 19:56:36 -05003303lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3304 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05003305{
3306 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
3307 later */
James Smart2e0fef82007-06-17 19:56:36 -05003308 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003309 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05003310 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05003311 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003312 return 0;
3313}
3314
3315
3316struct lpfc_dmabuf *
3317lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3318 dma_addr_t phys)
3319{
3320 struct lpfc_dmabuf *mp, *next_mp;
3321 struct list_head *slp = &pring->postbufq;
3322
3323 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05003324 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003325 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
3326 if (mp->phys == phys) {
3327 list_del_init(&mp->list);
3328 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05003329 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003330 return mp;
3331 }
3332 }
3333
James Smart2e0fef82007-06-17 19:56:36 -05003334 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003335 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003336 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05003337 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003338 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05003339 slp->next, slp->prev, pring->postbufq_cnt);
3340 return NULL;
3341}
3342
3343static void
James Smart2e0fef82007-06-17 19:56:36 -05003344lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3345 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05003346{
James Smart2e0fef82007-06-17 19:56:36 -05003347 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04003348 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05003349 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04003350 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3351
3352 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04003353
3354 if (irsp->ulpStatus) {
3355 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
3356 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
3357
James Smart2e0fef82007-06-17 19:56:36 -05003358 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04003359 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
3360 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
3361
James Smart92d7f7b2007-06-17 19:56:38 -05003362 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003363 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05003364 "with tag %x context %x, abort status %x, "
3365 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04003366 abort_iocb, abort_iotag, abort_context,
3367 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04003368
3369 /*
3370 * make sure we have the right iocbq before taking it
3371 * off the txcmplq and try to call completion routine.
3372 */
James Smart2e0fef82007-06-17 19:56:36 -05003373 if (!abort_iocb ||
3374 abort_iocb->iocb.ulpContext != abort_context ||
3375 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
3376 spin_unlock_irq(&phba->hbalock);
3377 else {
James Smart92d7f7b2007-06-17 19:56:38 -05003378 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04003379 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05003380 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04003381
James Smart92d7f7b2007-06-17 19:56:38 -05003382 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3383 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3384 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
3385 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04003386 }
3387 }
3388
James Bottomley604a3e32005-10-29 10:28:33 -05003389 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05003390 return;
3391}
3392
James Smart92d7f7b2007-06-17 19:56:38 -05003393static void
3394lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3395 struct lpfc_iocbq *rspiocb)
3396{
3397 IOCB_t *irsp = &rspiocb->iocb;
3398
3399 /* ELS cmd tag <ulpIoTag> completes */
3400 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smarte8b62012007-08-02 11:10:09 -04003401 "0133 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003402 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003403 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05003404 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05003405 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
3406 lpfc_ct_free_iocb(phba, cmdiocb);
3407 else
3408 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05003409 return;
3410}
3411
dea31012005-04-17 16:05:31 -05003412int
James Smart2e0fef82007-06-17 19:56:36 -05003413lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3414 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05003415{
James Smart2e0fef82007-06-17 19:56:36 -05003416 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003417 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05003418 IOCB_t *icmd = NULL;
3419 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04003420 int retval = IOCB_ERROR;
3421
James Smart92d7f7b2007-06-17 19:56:38 -05003422 /*
3423 * There are certain command types we don't want to abort. And we
3424 * don't want to abort commands that are already in the process of
3425 * being aborted.
James Smart07951072007-04-25 09:51:38 -04003426 */
3427 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05003428 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05003429 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3430 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04003431 return 0;
3432
James Smart858c9f62007-06-17 19:56:39 -05003433 /* If we're unloading, don't abort iocb on the ELS ring, but change the
3434 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04003435 */
James Smart858c9f62007-06-17 19:56:39 -05003436 if ((vport->load_flag & FC_UNLOADING) &&
3437 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003438 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
3439 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
3440 else
3441 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04003442 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05003443 }
dea31012005-04-17 16:05:31 -05003444
3445 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05003446 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003447 if (abtsiocbp == NULL)
3448 return 0;
dea31012005-04-17 16:05:31 -05003449
James Smart07951072007-04-25 09:51:38 -04003450 /* This signals the response to set the correct status
3451 * before calling the completion handler.
3452 */
3453 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
3454
dea31012005-04-17 16:05:31 -05003455 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04003456 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
3457 iabt->un.acxri.abortContextTag = icmd->ulpContext;
3458 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003459 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04003460 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05003461
James Smart2e0fef82007-06-17 19:56:36 -05003462 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04003463 iabt->ulpCommand = CMD_ABORT_XRI_CN;
3464 else
3465 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
3466
3467 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04003468
James Smarte8b62012007-08-02 11:10:09 -04003469 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3470 "0339 Abort xri x%x, original iotag x%x, "
3471 "abort cmd iotag x%x\n",
3472 iabt->un.acxri.abortContextTag,
3473 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05003474 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04003475
3476abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05003477 /*
3478 * Caller to this routine should check for IOCB_ERROR
3479 * and handle it properly. This routine no longer removes
3480 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04003481 */
James Smart2e0fef82007-06-17 19:56:36 -05003482 return retval;
dea31012005-04-17 16:05:31 -05003483}
3484
3485static int
James Smart51ef4c22007-08-02 11:10:31 -04003486lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
3487 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003488 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05003489{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003490 struct lpfc_scsi_buf *lpfc_cmd;
3491 struct scsi_cmnd *cmnd;
dea31012005-04-17 16:05:31 -05003492 int rc = 1;
3493
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003494 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
3495 return rc;
3496
James Smart51ef4c22007-08-02 11:10:31 -04003497 if (iocbq->vport != vport)
3498 return rc;
3499
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003500 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
3501 cmnd = lpfc_cmd->pCmd;
3502
3503 if (cmnd == NULL)
dea31012005-04-17 16:05:31 -05003504 return rc;
3505
3506 switch (ctx_cmd) {
3507 case LPFC_CTX_LUN:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003508 if ((cmnd->device->id == tgt_id) &&
3509 (cmnd->device->lun == lun_id))
dea31012005-04-17 16:05:31 -05003510 rc = 0;
3511 break;
3512 case LPFC_CTX_TGT:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003513 if (cmnd->device->id == tgt_id)
dea31012005-04-17 16:05:31 -05003514 rc = 0;
3515 break;
dea31012005-04-17 16:05:31 -05003516 case LPFC_CTX_HOST:
3517 rc = 0;
3518 break;
3519 default:
3520 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
3521 __FUNCTION__, ctx_cmd);
3522 break;
3523 }
3524
3525 return rc;
3526}
3527
3528int
James Smart51ef4c22007-08-02 11:10:31 -04003529lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
3530 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05003531{
James Smart51ef4c22007-08-02 11:10:31 -04003532 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003533 struct lpfc_iocbq *iocbq;
3534 int sum, i;
dea31012005-04-17 16:05:31 -05003535
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003536 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
3537 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05003538
James Smart51ef4c22007-08-02 11:10:31 -04003539 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
3540 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003541 sum++;
dea31012005-04-17 16:05:31 -05003542 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003543
dea31012005-04-17 16:05:31 -05003544 return sum;
3545}
3546
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003547void
James Smart2e0fef82007-06-17 19:56:36 -05003548lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3549 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003550{
James Bottomley604a3e32005-10-29 10:28:33 -05003551 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003552 return;
3553}
3554
dea31012005-04-17 16:05:31 -05003555int
James Smart51ef4c22007-08-02 11:10:31 -04003556lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
3557 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05003558{
James Smart51ef4c22007-08-02 11:10:31 -04003559 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003560 struct lpfc_iocbq *iocbq;
3561 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05003562 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05003563 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003564 int i;
dea31012005-04-17 16:05:31 -05003565
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003566 for (i = 1; i <= phba->sli.last_iotag; i++) {
3567 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05003568
James Smart51ef4c22007-08-02 11:10:31 -04003569 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05003570 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05003571 continue;
3572
3573 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003574 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003575 if (abtsiocb == NULL) {
3576 errcnt++;
3577 continue;
3578 }
dea31012005-04-17 16:05:31 -05003579
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003580 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05003581 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
3582 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
3583 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
3584 abtsiocb->iocb.ulpLe = 1;
3585 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05003586 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05003587
James Smart2e0fef82007-06-17 19:56:36 -05003588 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003589 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
3590 else
3591 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
3592
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003593 /* Setup callback routine and issue the command. */
3594 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05003595 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
3596 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05003597 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05003598 errcnt++;
3599 continue;
3600 }
3601 }
3602
3603 return errcnt;
3604}
3605
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003606static void
3607lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
3608 struct lpfc_iocbq *cmdiocbq,
3609 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05003610{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003611 wait_queue_head_t *pdone_q;
3612 unsigned long iflags;
dea31012005-04-17 16:05:31 -05003613
James Smart2e0fef82007-06-17 19:56:36 -05003614 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003615 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
3616 if (cmdiocbq->context2 && rspiocbq)
3617 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
3618 &rspiocbq->iocb, sizeof(IOCB_t));
3619
3620 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003621 if (pdone_q)
3622 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05003623 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05003624 return;
3625}
3626
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003627/*
3628 * Issue the caller's iocb and wait for its completion, but no longer than the
3629 * caller's timeout. Note that iocb_flags is cleared before the
3630 * lpfc_sli_issue_call since the wake routine sets a unique value and by
3631 * definition this is a wait function.
3632 */
James Smart92d7f7b2007-06-17 19:56:38 -05003633
dea31012005-04-17 16:05:31 -05003634int
James Smart2e0fef82007-06-17 19:56:36 -05003635lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
3636 struct lpfc_sli_ring *pring,
3637 struct lpfc_iocbq *piocb,
3638 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003639 uint32_t timeout)
dea31012005-04-17 16:05:31 -05003640{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08003641 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003642 long timeleft, timeout_req = 0;
3643 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003644 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05003645
3646 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003647 * If the caller has provided a response iocbq buffer, then context2
3648 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05003649 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003650 if (prspiocbq) {
3651 if (piocb->context2)
3652 return IOCB_ERROR;
3653 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05003654 }
3655
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003656 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
3657 piocb->context_un.wait_queue = &done_q;
3658 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05003659
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003660 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3661 creg_val = readl(phba->HCregaddr);
3662 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
3663 writel(creg_val, phba->HCregaddr);
3664 readl(phba->HCregaddr); /* flush */
3665 }
3666
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003667 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
3668 if (retval == IOCB_SUCCESS) {
3669 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003670 timeleft = wait_event_timeout(done_q,
3671 piocb->iocb_flag & LPFC_IO_WAKE,
3672 timeout_req);
dea31012005-04-17 16:05:31 -05003673
James Smart7054a602007-04-25 09:52:34 -04003674 if (piocb->iocb_flag & LPFC_IO_WAKE) {
3675 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003676 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04003677 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003678 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003679 "0338 IOCB wait timeout error - no "
3680 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003681 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04003682 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003684 "0330 IOCB wake NOT set, "
3685 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003686 timeout, (timeleft / jiffies));
3687 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05003688 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003689 } else {
3690 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003691 ":0332 IOCB wait issue failed, Data x%x\n",
3692 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003693 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05003694 }
3695
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003696 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3697 creg_val = readl(phba->HCregaddr);
3698 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
3699 writel(creg_val, phba->HCregaddr);
3700 readl(phba->HCregaddr); /* flush */
3701 }
3702
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003703 if (prspiocbq)
3704 piocb->context2 = NULL;
3705
3706 piocb->context_un.wait_queue = NULL;
3707 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05003708 return retval;
3709}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003710
dea31012005-04-17 16:05:31 -05003711int
James Smart2e0fef82007-06-17 19:56:36 -05003712lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05003713 uint32_t timeout)
3714{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08003715 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05003716 int retval;
James Smart858c9f62007-06-17 19:56:39 -05003717 unsigned long flag;
dea31012005-04-17 16:05:31 -05003718
3719 /* The caller must leave context1 empty. */
James Smart92d7f7b2007-06-17 19:56:38 -05003720 if (pmboxq->context1 != 0)
James Smart2e0fef82007-06-17 19:56:36 -05003721 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003722
3723 /* setup wake call as IOCB callback */
3724 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
3725 /* setup context field to pass wait_queue pointer to wake function */
3726 pmboxq->context1 = &done_q;
3727
dea31012005-04-17 16:05:31 -05003728 /* now issue the command */
3729 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3730
3731 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04003732 wait_event_interruptible_timeout(done_q,
3733 pmboxq->mbox_flag & LPFC_MBX_WAKE,
3734 timeout * HZ);
3735
James Smart858c9f62007-06-17 19:56:39 -05003736 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05003737 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04003738 /*
3739 * if LPFC_MBX_WAKE flag is set the mailbox is completed
3740 * else do not free the resources.
3741 */
3742 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05003743 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05003744 else {
James Smart7054a602007-04-25 09:52:34 -04003745 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05003746 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3747 }
3748 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05003749 }
3750
dea31012005-04-17 16:05:31 -05003751 return retval;
3752}
3753
James Smartb4c02652006-07-06 15:50:43 -04003754int
3755lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
3756{
James Smart2e0fef82007-06-17 19:56:36 -05003757 struct lpfc_vport *vport = phba->pport;
James Smartb4c02652006-07-06 15:50:43 -04003758 int i = 0;
James Smarted957682007-06-17 19:56:37 -05003759 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04003760
James Smart2e0fef82007-06-17 19:56:36 -05003761 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
James Smartb4c02652006-07-06 15:50:43 -04003762 if (i++ > LPFC_MBOX_TMO * 1000)
3763 return 1;
3764
James Smarted957682007-06-17 19:56:37 -05003765 /*
3766 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
3767 * did finish. This way we won't get the misleading
3768 * "Stray Mailbox Interrupt" message.
3769 */
3770 spin_lock_irq(&phba->hbalock);
3771 ha_copy = phba->work_ha;
3772 phba->work_ha &= ~HA_MBATT;
3773 spin_unlock_irq(&phba->hbalock);
3774
3775 if (ha_copy & HA_MBATT)
3776 if (lpfc_sli_handle_mb_event(phba) == 0)
3777 i = 0;
James Smartb4c02652006-07-06 15:50:43 -04003778
3779 msleep(1);
3780 }
3781
3782 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
3783}
3784
dea31012005-04-17 16:05:31 -05003785irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01003786lpfc_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05003787{
James Smart2e0fef82007-06-17 19:56:36 -05003788 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05003789 uint32_t ha_copy;
3790 uint32_t work_ha_copy;
3791 unsigned long status;
dea31012005-04-17 16:05:31 -05003792 uint32_t control;
3793
James Smart92d7f7b2007-06-17 19:56:38 -05003794 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05003795 struct lpfc_vport *vport;
3796 struct lpfc_nodelist *ndlp;
3797 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05003798 LPFC_MBOXQ_t *pmb;
3799 int rc;
3800
dea31012005-04-17 16:05:31 -05003801 /*
3802 * Get the driver's phba structure from the dev_id and
3803 * assume the HBA is not interrupting.
3804 */
3805 phba = (struct lpfc_hba *) dev_id;
3806
3807 if (unlikely(!phba))
3808 return IRQ_NONE;
3809
Linas Vepstas8d63f372007-02-14 14:28:36 -06003810 /* If the pci channel is offline, ignore all the interrupts. */
3811 if (unlikely(pci_channel_offline(phba->pcidev)))
3812 return IRQ_NONE;
3813
dea31012005-04-17 16:05:31 -05003814 phba->sli.slistat.sli_intr++;
3815
3816 /*
3817 * Call the HBA to see if it is interrupting. If not, don't claim
3818 * the interrupt
3819 */
3820
3821 /* Ignore all interrupts during initialization. */
James Smart2e0fef82007-06-17 19:56:36 -05003822 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003823 return IRQ_NONE;
3824
3825 /*
3826 * Read host attention register to determine interrupt source
3827 * Clear Attention Sources, except Error Attention (to
3828 * preserve status) and Link Attention
3829 */
James Smart2e0fef82007-06-17 19:56:36 -05003830 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003831 ha_copy = readl(phba->HAregaddr);
James Smartebdbe652007-04-25 09:53:15 -04003832 /* If somebody is waiting to handle an eratt don't process it
3833 * here. The brdkill function will do this.
3834 */
James Smart2e0fef82007-06-17 19:56:36 -05003835 if (phba->link_flag & LS_IGNORE_ERATT)
James Smartebdbe652007-04-25 09:53:15 -04003836 ha_copy &= ~HA_ERATT;
dea31012005-04-17 16:05:31 -05003837 writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
3838 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003839 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003840
3841 if (unlikely(!ha_copy))
3842 return IRQ_NONE;
3843
3844 work_ha_copy = ha_copy & phba->work_ha_mask;
3845
3846 if (unlikely(work_ha_copy)) {
3847 if (work_ha_copy & HA_LATT) {
3848 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
3849 /*
3850 * Turn off Link Attention interrupts
3851 * until CLEAR_LA done
3852 */
James Smart2e0fef82007-06-17 19:56:36 -05003853 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003854 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
3855 control = readl(phba->HCregaddr);
3856 control &= ~HC_LAINT_ENA;
3857 writel(control, phba->HCregaddr);
3858 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003859 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003860 }
3861 else
3862 work_ha_copy &= ~HA_LATT;
3863 }
3864
3865 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05003866 /*
3867 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
3868 * the only slow ring.
3869 */
3870 status = (work_ha_copy &
3871 (HA_RXMASK << (4*LPFC_ELS_RING)));
3872 status >>= (4*LPFC_ELS_RING);
3873 if (status & HA_RXMASK) {
3874 spin_lock(&phba->hbalock);
3875 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04003876
3877 lpfc_debugfs_slow_ring_trc(phba,
3878 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
3879 control, status,
3880 (uint32_t)phba->sli.slistat.sli_intr);
3881
James Smart858c9f62007-06-17 19:56:39 -05003882 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04003883 lpfc_debugfs_slow_ring_trc(phba,
3884 "ISR Disable ring:"
3885 "pwork:x%x hawork:x%x wait:x%x",
3886 phba->work_ha, work_ha_copy,
3887 (uint32_t)((unsigned long)
3888 phba->work_wait));
3889
James Smart858c9f62007-06-17 19:56:39 -05003890 control &=
3891 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05003892 writel(control, phba->HCregaddr);
3893 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05003894 }
James Smarta58cbd52007-08-02 11:09:43 -04003895 else {
3896 lpfc_debugfs_slow_ring_trc(phba,
3897 "ISR slow ring: pwork:"
3898 "x%x hawork:x%x wait:x%x",
3899 phba->work_ha, work_ha_copy,
3900 (uint32_t)((unsigned long)
3901 phba->work_wait));
3902 }
James Smart858c9f62007-06-17 19:56:39 -05003903 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003904 }
3905 }
3906
3907 if (work_ha_copy & HA_ERATT) {
James Smart2e0fef82007-06-17 19:56:36 -05003908 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003909 /*
3910 * There was a link/board error. Read the
3911 * status register to retrieve the error event
3912 * and process it.
3913 */
3914 phba->sli.slistat.err_attn_event++;
3915 /* Save status info */
3916 phba->work_hs = readl(phba->HSregaddr);
3917 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
3918 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
3919
3920 /* Clear Chip error bit */
3921 writel(HA_ERATT, phba->HAregaddr);
3922 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003923 phba->pport->stopped = 1;
dea31012005-04-17 16:05:31 -05003924 }
3925
James Smart92d7f7b2007-06-17 19:56:38 -05003926 if ((work_ha_copy & HA_MBATT) &&
3927 (phba->sli.mbox_active)) {
3928 pmb = phba->sli.mbox_active;
3929 pmbox = &pmb->mb;
3930 mbox = &phba->slim2p->mbx;
James Smart858c9f62007-06-17 19:56:39 -05003931 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003932
3933 /* First check out the status word */
3934 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
3935 if (pmbox->mbxOwner != OWN_HOST) {
3936 /*
3937 * Stray Mailbox Interrupt, mbxCommand <cmd>
3938 * mbxStatus <status>
3939 */
3940 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX |
3941 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003942 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05003943 "Interrupt mbxCommand x%x "
3944 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003945 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05003946 pmbox->mbxCommand,
3947 pmbox->mbxStatus);
3948 }
James Smart858c9f62007-06-17 19:56:39 -05003949 phba->last_completion_time = jiffies;
James Smart92d7f7b2007-06-17 19:56:38 -05003950 del_timer_sync(&phba->sli.mbox_tmo);
3951
James Smart92d7f7b2007-06-17 19:56:38 -05003952 phba->sli.mbox_active = NULL;
3953 if (pmb->mbox_cmpl) {
3954 lpfc_sli_pcimem_bcopy(mbox, pmbox,
3955 MAILBOX_CMD_SIZE);
3956 }
James Smart858c9f62007-06-17 19:56:39 -05003957 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
3958 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
3959
3960 lpfc_debugfs_disc_trc(vport,
3961 LPFC_DISC_TRC_MBOX_VPORT,
3962 "MBOX dflt rpi: : status:x%x rpi:x%x",
3963 (uint32_t)pmbox->mbxStatus,
3964 pmbox->un.varWords[0], 0);
3965
3966 if ( !pmbox->mbxStatus) {
3967 mp = (struct lpfc_dmabuf *)
3968 (pmb->context1);
3969 ndlp = (struct lpfc_nodelist *)
3970 pmb->context2;
3971
3972 /* Reg_LOGIN of dflt RPI was successful.
3973 * new lets get rid of the RPI using the
3974 * same mbox buffer.
3975 */
3976 lpfc_unreg_login(phba, vport->vpi,
3977 pmbox->un.varWords[0], pmb);
3978 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3979 pmb->context1 = mp;
3980 pmb->context2 = ndlp;
3981 pmb->vport = vport;
3982 spin_lock(&phba->hbalock);
3983 phba->sli.sli_flag &=
3984 ~LPFC_SLI_MBOX_ACTIVE;
3985 spin_unlock(&phba->hbalock);
3986 goto send_current_mbox;
3987 }
3988 }
3989 spin_lock(&phba->pport->work_port_lock);
3990 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3991 spin_unlock(&phba->pport->work_port_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003992 lpfc_mbox_cmpl_put(phba, pmb);
3993 }
3994 if ((work_ha_copy & HA_MBATT) &&
3995 (phba->sli.mbox_active == NULL)) {
3996send_next_mbox:
3997 spin_lock(&phba->hbalock);
3998 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3999 pmb = lpfc_mbox_get(phba);
4000 spin_unlock(&phba->hbalock);
James Smart858c9f62007-06-17 19:56:39 -05004001send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05004002 /* Process next mailbox command if there is one */
4003 if (pmb != NULL) {
4004 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4005 if (rc == MBX_NOT_FINISHED) {
4006 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
4007 lpfc_mbox_cmpl_put(phba, pmb);
4008 goto send_next_mbox;
4009 }
James Smart92d7f7b2007-06-17 19:56:38 -05004010 }
4011
4012 }
4013
James Smart2e0fef82007-06-17 19:56:36 -05004014 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004015 phba->work_ha |= work_ha_copy;
4016 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05004017 lpfc_worker_wake_up(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004018 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004019 }
4020
4021 ha_copy &= ~(phba->work_ha_mask);
4022
4023 /*
4024 * Process all events on FCP ring. Take the optimized path for
4025 * FCP IO. Any other IO is slow path and is handled by
4026 * the worker thread.
4027 */
4028 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
4029 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05004030 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05004031 lpfc_sli_handle_fast_ring_event(phba,
4032 &phba->sli.ring[LPFC_FCP_RING],
4033 status);
James Smarta4bc3372006-12-02 13:34:16 -05004034
4035 if (phba->cfg_multi_ring_support == 2) {
4036 /*
4037 * Process all events on extra ring. Take the optimized path
4038 * for extra ring IO. Any other IO is slow path and is handled
4039 * by the worker thread.
4040 */
4041 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
4042 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05004043 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05004044 lpfc_sli_handle_fast_ring_event(phba,
4045 &phba->sli.ring[LPFC_EXTRA_RING],
4046 status);
4047 }
4048 }
dea31012005-04-17 16:05:31 -05004049 return IRQ_HANDLED;
4050
4051} /* lpfc_intr_handler */