blob: f40aa7b905f716e335b4d30b33e1859ed478b214 [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 Smart1b32f6a2008-02-08 18:49:39 -05004 * Copyright (C) 2004-2008 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
Adrian Bunka6ababd2007-11-05 18:07:33 +0100109static void
James Smart2e0fef82007-06-17 19:56:36 -0500110__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:
James Smart3163f722008-02-08 18:50:25 -0500206 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500207 type = LPFC_UNSOL_IOCB;
208 break;
James Smart3163f722008-02-08 18:50:25 -0500209 case CMD_IOCB_XMIT_MSEQ64_CR:
210 case CMD_IOCB_XMIT_MSEQ64_CX:
211 case CMD_IOCB_RCV_SEQ_LIST64_CX:
212 case CMD_IOCB_RCV_ELS_LIST64_CX:
213 case CMD_IOCB_CLOSE_EXTENDED_CN:
214 case CMD_IOCB_ABORT_EXTENDED_CN:
215 case CMD_IOCB_RET_HBQE64_CN:
216 case CMD_IOCB_FCP_IBIDIR64_CR:
217 case CMD_IOCB_FCP_IBIDIR64_CX:
218 case CMD_IOCB_FCP_ITASKMGT64_CX:
219 case CMD_IOCB_LOGENTRY_CN:
220 case CMD_IOCB_LOGENTRY_ASYNC_CN:
221 printk("%s - Unhandled SLI-3 Command x%x\n",
222 __FUNCTION__, iocb_cmnd);
223 type = LPFC_UNKNOWN_IOCB;
224 break;
dea31012005-04-17 16:05:31 -0500225 default:
226 type = LPFC_UNKNOWN_IOCB;
227 break;
228 }
229
230 return type;
231}
232
233static int
James Smarted957682007-06-17 19:56:37 -0500234lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500235{
236 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500237 LPFC_MBOXQ_t *pmb;
238 MAILBOX_t *pmbox;
239 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500240
James Smarted957682007-06-17 19:56:37 -0500241 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
242 if (!pmb)
243 return -ENOMEM;
244 pmbox = &pmb->mb;
245 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500246 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500247 lpfc_config_ring(phba, i, pmb);
248 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
249 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400251 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500252 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
253 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400254 rc, pmbox->mbxCommand,
255 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500256 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500257 ret = -ENXIO;
258 break;
dea31012005-04-17 16:05:31 -0500259 }
260 }
James Smarted957682007-06-17 19:56:37 -0500261 mempool_free(pmb, phba->mbox_mem_pool);
262 return ret;
dea31012005-04-17 16:05:31 -0500263}
264
265static int
James Smart2e0fef82007-06-17 19:56:36 -0500266lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
267 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500268{
dea31012005-04-17 16:05:31 -0500269 list_add_tail(&piocb->list, &pring->txcmplq);
270 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500271 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
272 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
273 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
274 if (!piocb->vport)
275 BUG();
276 else
277 mod_timer(&piocb->vport->els_tmofunc,
278 jiffies + HZ * (phba->fc_ratov << 1));
279 }
280
dea31012005-04-17 16:05:31 -0500281
James Smart2e0fef82007-06-17 19:56:36 -0500282 return 0;
dea31012005-04-17 16:05:31 -0500283}
284
285static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500286lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500287{
dea31012005-04-17 16:05:31 -0500288 struct lpfc_iocbq *cmd_iocb;
289
James Smart858c9f62007-06-17 19:56:39 -0500290 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
291 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500292 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500293 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500294}
295
296static IOCB_t *
297lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
298{
James Smarted957682007-06-17 19:56:37 -0500299 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
300 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
301 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -0500302 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500303
304 if ((pring->next_cmdidx == pring->cmdidx) &&
305 (++pring->next_cmdidx >= max_cmd_idx))
306 pring->next_cmdidx = 0;
307
308 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
309
310 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
311
312 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
313 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400314 "0315 Ring %d issue: portCmdGet %d "
dea31012005-04-17 16:05:31 -0500315 "is bigger then cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400316 pring->ringno,
dea31012005-04-17 16:05:31 -0500317 pring->local_getidx, max_cmd_idx);
318
James Smart2e0fef82007-06-17 19:56:36 -0500319 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500320 /*
321 * All error attention handlers are posted to
322 * worker thread
323 */
324 phba->work_ha |= HA_ERATT;
325 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500326
James Smart5e9d9b82008-06-14 22:52:53 -0400327 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500328
329 return NULL;
330 }
331
332 if (pring->local_getidx == pring->next_cmdidx)
333 return NULL;
334 }
335
James Smarted957682007-06-17 19:56:37 -0500336 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500337}
338
James Bottomley604a3e32005-10-29 10:28:33 -0500339uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500340lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500341{
James Smart2e0fef82007-06-17 19:56:36 -0500342 struct lpfc_iocbq **new_arr;
343 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500344 size_t new_len;
345 struct lpfc_sli *psli = &phba->sli;
346 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500347
James Smart2e0fef82007-06-17 19:56:36 -0500348 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500349 iotag = psli->last_iotag;
350 if(++iotag < psli->iocbq_lookup_len) {
351 psli->last_iotag = iotag;
352 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500353 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500354 iocbq->iotag = iotag;
355 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500356 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500357 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
358 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500359 spin_unlock_irq(&phba->hbalock);
360 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500361 GFP_KERNEL);
362 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500363 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500364 old_arr = psli->iocbq_lookup;
365 if (new_len <= psli->iocbq_lookup_len) {
366 /* highly unprobable case */
367 kfree(new_arr);
368 iotag = psli->last_iotag;
369 if(++iotag < psli->iocbq_lookup_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 return iotag;
375 }
James Smart2e0fef82007-06-17 19:56:36 -0500376 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500377 return 0;
378 }
379 if (psli->iocbq_lookup)
380 memcpy(new_arr, old_arr,
381 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -0400382 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -0500383 psli->iocbq_lookup = new_arr;
384 psli->iocbq_lookup_len = new_len;
385 psli->last_iotag = iotag;
386 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500387 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500388 iocbq->iotag = iotag;
389 kfree(old_arr);
390 return iotag;
391 }
James Smart8f6d98d2006-08-01 07:34:00 -0400392 } else
James Smart2e0fef82007-06-17 19:56:36 -0500393 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500394
James Bottomley604a3e32005-10-29 10:28:33 -0500395 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400396 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
397 psli->last_iotag);
dea31012005-04-17 16:05:31 -0500398
James Bottomley604a3e32005-10-29 10:28:33 -0500399 return 0;
dea31012005-04-17 16:05:31 -0500400}
401
402static void
403lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
404 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
405{
406 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500407 * Set up an iotag
dea31012005-04-17 16:05:31 -0500408 */
James Bottomley604a3e32005-10-29 10:28:33 -0500409 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500410
James Smarta58cbd52007-08-02 11:09:43 -0400411 if (pring->ringno == LPFC_ELS_RING) {
412 lpfc_debugfs_slow_ring_trc(phba,
413 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
414 *(((uint32_t *) &nextiocb->iocb) + 4),
415 *(((uint32_t *) &nextiocb->iocb) + 6),
416 *(((uint32_t *) &nextiocb->iocb) + 7));
417 }
418
dea31012005-04-17 16:05:31 -0500419 /*
420 * Issue iocb command to adapter
421 */
James Smart92d7f7b2007-06-17 19:56:38 -0500422 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500423 wmb();
424 pring->stats.iocb_cmd++;
425
426 /*
427 * If there is no completion routine to call, we can release the
428 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
429 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
430 */
431 if (nextiocb->iocb_cmpl)
432 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500433 else
James Smart2e0fef82007-06-17 19:56:36 -0500434 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500435
436 /*
437 * Let the HBA know what IOCB slot will be the next one the
438 * driver will put a command into.
439 */
440 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500441 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500442}
443
444static void
James Smart2e0fef82007-06-17 19:56:36 -0500445lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500446{
447 int ringno = pring->ringno;
448
449 pring->flag |= LPFC_CALL_RING_AVAILABLE;
450
451 wmb();
452
453 /*
454 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
455 * The HBA will tell us when an IOCB entry is available.
456 */
457 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
458 readl(phba->CAregaddr); /* flush */
459
460 pring->stats.iocb_cmd_full++;
461}
462
463static void
James Smart2e0fef82007-06-17 19:56:36 -0500464lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500465{
466 int ringno = pring->ringno;
467
468 /*
469 * Tell the HBA that there is work to do in this ring.
470 */
471 wmb();
472 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
473 readl(phba->CAregaddr); /* flush */
474}
475
476static void
James Smart2e0fef82007-06-17 19:56:36 -0500477lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500478{
479 IOCB_t *iocb;
480 struct lpfc_iocbq *nextiocb;
481
482 /*
483 * Check to see if:
484 * (a) there is anything on the txq to send
485 * (b) link is up
486 * (c) link attention events can be processed (fcp ring only)
487 * (d) IOCB processing is not blocked by the outstanding mbox command.
488 */
489 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500490 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500491 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -0400492 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -0500493
494 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
495 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
496 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
497
498 if (iocb)
499 lpfc_sli_update_ring(phba, pring);
500 else
501 lpfc_sli_update_full_ring(phba, pring);
502 }
503
504 return;
505}
506
Adrian Bunka6ababd2007-11-05 18:07:33 +0100507static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500508lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
509{
510 struct hbq_s *hbqp = &phba->hbqs[hbqno];
511
512 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
513 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
514 hbqp->next_hbqPutIdx = 0;
515
516 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500517 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500518 uint32_t getidx = le32_to_cpu(raw_index);
519
520 hbqp->local_hbqGetIdx = getidx;
521
522 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
523 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500524 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400525 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500526 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400527 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500528 hbqp->entry_count);
529
530 phba->link_state = LPFC_HBA_ERROR;
531 return NULL;
532 }
533
534 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
535 return NULL;
536 }
537
James Smart51ef4c22007-08-02 11:10:31 -0400538 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
539 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500540}
541
542void
543lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
544{
James Smart92d7f7b2007-06-17 19:56:38 -0500545 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
546 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -0500547 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -0400548 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -0500549 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500550
James Smart51ef4c22007-08-02 11:10:31 -0400551 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500552 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -0500553 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -0400554 for (i = 0; i < hbq_count; ++i) {
555 list_for_each_entry_safe(dmabuf, next_dmabuf,
556 &phba->hbqs[i].hbq_buffer_list, list) {
557 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
558 list_del(&hbq_buf->dbuf.list);
559 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
560 }
James Smarta8adb832007-10-27 13:37:53 -0400561 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -0500562 }
James Smart3163f722008-02-08 18:50:25 -0500563 /* Return all HBQ buffer that are in-fly */
564 list_for_each_entry_safe(dmabuf, next_dmabuf,
565 &phba->hbqbuf_in_list, list) {
566 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
567 list_del(&hbq_buf->dbuf.list);
568 if (hbq_buf->tag == -1) {
569 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
570 (phba, hbq_buf);
571 } else {
572 hbqno = hbq_buf->tag >> 16;
573 if (hbqno >= LPFC_MAX_HBQS)
574 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
575 (phba, hbq_buf);
576 else
577 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
578 hbq_buf);
579 }
580 }
581
582 /* Mark the HBQs not in use */
583 phba->hbq_in_use = 0;
584 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -0500585}
586
James Smart51ef4c22007-08-02 11:10:31 -0400587static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500588lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500589 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500590{
591 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500592 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500593
594 /* Get next HBQ entry slot to use */
595 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
596 if (hbqe) {
597 struct hbq_s *hbqp = &phba->hbqs[hbqno];
598
James Smart92d7f7b2007-06-17 19:56:38 -0500599 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
600 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400601 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500602 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500603 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
604 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
605 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500606 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
607 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500608 /* flush */
James Smarted957682007-06-17 19:56:37 -0500609 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400610 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smarted957682007-06-17 19:56:37 -0500611 }
James Smart51ef4c22007-08-02 11:10:31 -0400612 return hbqe;
James Smarted957682007-06-17 19:56:37 -0500613}
614
James Smart92d7f7b2007-06-17 19:56:38 -0500615static struct lpfc_hbq_init lpfc_els_hbq = {
616 .rn = 1,
617 .entry_count = 200,
618 .mask_count = 0,
619 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400620 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500621 .buffer_count = 0,
622 .init_count = 20,
623 .add_count = 5,
624};
James Smarted957682007-06-17 19:56:37 -0500625
James Smart51ef4c22007-08-02 11:10:31 -0400626static struct lpfc_hbq_init lpfc_extra_hbq = {
627 .rn = 1,
628 .entry_count = 200,
629 .mask_count = 0,
630 .profile = 0,
631 .ring_mask = (1 << LPFC_EXTRA_RING),
632 .buffer_count = 0,
633 .init_count = 0,
634 .add_count = 5,
635};
636
James Smart78b2d852007-08-02 11:10:21 -0400637struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500638 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400639 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500640};
641
James Smart311464e2007-08-02 11:10:37 -0400642static int
James Smart92d7f7b2007-06-17 19:56:38 -0500643lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
644{
645 uint32_t i, start, end;
James Smart3163f722008-02-08 18:50:25 -0500646 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500647 struct hbq_dmabuf *hbq_buffer;
648
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700649 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -0400650 return 0;
James Smart51ef4c22007-08-02 11:10:31 -0400651
James Smarta8adb832007-10-27 13:37:53 -0400652 start = phba->hbqs[hbqno].buffer_count;
653 end = count + start;
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700654 if (end > lpfc_hbq_defs[hbqno]->entry_count)
James Smart92d7f7b2007-06-17 19:56:38 -0500655 end = lpfc_hbq_defs[hbqno]->entry_count;
James Smart92d7f7b2007-06-17 19:56:38 -0500656
James Smart3163f722008-02-08 18:50:25 -0500657 /* Check whether HBQ is still in use */
658 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700659 if (!phba->hbq_in_use)
660 goto out;
James Smart3163f722008-02-08 18:50:25 -0500661
James Smart92d7f7b2007-06-17 19:56:38 -0500662 /* Populate HBQ entries */
663 for (i = start; i < end; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400664 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500665 if (!hbq_buffer)
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700666 goto err;
James Smart92d7f7b2007-06-17 19:56:38 -0500667 hbq_buffer->tag = (i | (hbqno << 16));
James Smart51ef4c22007-08-02 11:10:31 -0400668 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smarta8adb832007-10-27 13:37:53 -0400669 phba->hbqs[hbqno].buffer_count++;
James Smart51ef4c22007-08-02 11:10:31 -0400670 else
671 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -0500672 }
James Smart3163f722008-02-08 18:50:25 -0500673
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700674 out:
James Smart3163f722008-02-08 18:50:25 -0500675 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500676 return 0;
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700677 err:
678 spin_unlock_irqrestore(&phba->hbalock, flags);
679 return 1;
James Smarted957682007-06-17 19:56:37 -0500680}
681
682int
James Smart92d7f7b2007-06-17 19:56:38 -0500683lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500684{
James Smart92d7f7b2007-06-17 19:56:38 -0500685 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
686 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -0500687}
688
Adrian Bunka6ababd2007-11-05 18:07:33 +0100689static int
James Smart92d7f7b2007-06-17 19:56:38 -0500690lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500691{
James Smart92d7f7b2007-06-17 19:56:38 -0500692 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
693 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -0500694}
695
Adrian Bunka6ababd2007-11-05 18:07:33 +0100696static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -0500697lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
698{
James Smart92d7f7b2007-06-17 19:56:38 -0500699 struct lpfc_dmabuf *d_buf;
700 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400701 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500702
James Smart51ef4c22007-08-02 11:10:31 -0400703 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +0200704 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -0400705 return NULL;
706
707 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -0500708 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -0400709 if (hbq_buf->tag == tag) {
James Smart92d7f7b2007-06-17 19:56:38 -0500710 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -0500711 }
712 }
James Smart92d7f7b2007-06-17 19:56:38 -0500713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400714 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -0400715 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -0500716 return NULL;
James Smarted957682007-06-17 19:56:37 -0500717}
718
719void
James Smart51ef4c22007-08-02 11:10:31 -0400720lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -0500721{
722 uint32_t hbqno;
723
James Smart51ef4c22007-08-02 11:10:31 -0400724 if (hbq_buffer) {
725 hbqno = hbq_buffer->tag >> 16;
726 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
727 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
728 }
James Smarted957682007-06-17 19:56:37 -0500729 }
730}
731
dea31012005-04-17 16:05:31 -0500732static int
733lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
734{
735 uint8_t ret;
736
737 switch (mbxCommand) {
738 case MBX_LOAD_SM:
739 case MBX_READ_NV:
740 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -0400741 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -0500742 case MBX_RUN_BIU_DIAG:
743 case MBX_INIT_LINK:
744 case MBX_DOWN_LINK:
745 case MBX_CONFIG_LINK:
746 case MBX_CONFIG_RING:
747 case MBX_RESET_RING:
748 case MBX_READ_CONFIG:
749 case MBX_READ_RCONFIG:
750 case MBX_READ_SPARM:
751 case MBX_READ_STATUS:
752 case MBX_READ_RPI:
753 case MBX_READ_XRI:
754 case MBX_READ_REV:
755 case MBX_READ_LNK_STAT:
756 case MBX_REG_LOGIN:
757 case MBX_UNREG_LOGIN:
758 case MBX_READ_LA:
759 case MBX_CLEAR_LA:
760 case MBX_DUMP_MEMORY:
761 case MBX_DUMP_CONTEXT:
762 case MBX_RUN_DIAGS:
763 case MBX_RESTART:
764 case MBX_UPDATE_CFG:
765 case MBX_DOWN_LOAD:
766 case MBX_DEL_LD_ENTRY:
767 case MBX_RUN_PROGRAM:
768 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -0500769 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -0500770 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500771 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -0500772 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500773 case MBX_BEACON:
dea31012005-04-17 16:05:31 -0500774 case MBX_LOAD_AREA:
775 case MBX_RUN_BIU_DIAG64:
776 case MBX_CONFIG_PORT:
777 case MBX_READ_SPARM64:
778 case MBX_READ_RPI64:
779 case MBX_REG_LOGIN64:
780 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -0500781 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -0500782 case MBX_SET_DEBUG:
783 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -0400784 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -0500785 case MBX_REG_VPI:
786 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -0500787 case MBX_HEARTBEAT:
dea31012005-04-17 16:05:31 -0500788 ret = mbxCommand;
789 break;
790 default:
791 ret = MBX_SHUTDOWN;
792 break;
793 }
James Smart2e0fef82007-06-17 19:56:36 -0500794 return ret;
dea31012005-04-17 16:05:31 -0500795}
796static void
James Smart2e0fef82007-06-17 19:56:36 -0500797lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -0500798{
799 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -0500800 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -0500801
802 /*
803 * If pdone_q is empty, the driver thread gave up waiting and
804 * continued running.
805 */
James Smart7054a602007-04-25 09:52:34 -0400806 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -0500807 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -0500808 pdone_q = (wait_queue_head_t *) pmboxq->context1;
809 if (pdone_q)
810 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -0500811 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -0500812 return;
813}
814
815void
James Smart2e0fef82007-06-17 19:56:36 -0500816lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500817{
818 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -0400819 uint16_t rpi;
820 int rc;
821
dea31012005-04-17 16:05:31 -0500822 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -0400823
dea31012005-04-17 16:05:31 -0500824 if (mp) {
825 lpfc_mbuf_free(phba, mp->virt, mp->phys);
826 kfree(mp);
827 }
James Smart7054a602007-04-25 09:52:34 -0400828
829 /*
830 * If a REG_LOGIN succeeded after node is destroyed or node
831 * is in re-discovery driver need to cleanup the RPI.
832 */
James Smart2e0fef82007-06-17 19:56:36 -0500833 if (!(phba->pport->load_flag & FC_UNLOADING) &&
834 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
835 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -0400836
837 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -0500838 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
839 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -0400840 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
841 if (rc != MBX_NOT_FINISHED)
842 return;
843 }
844
James Smart2e0fef82007-06-17 19:56:36 -0500845 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500846 return;
847}
848
849int
James Smart2e0fef82007-06-17 19:56:36 -0500850lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500851{
James Smart92d7f7b2007-06-17 19:56:38 -0500852 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -0500853 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -0500854 int rc;
855 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -0500856
857 phba->sli.slistat.mbox_event++;
858
James Smart92d7f7b2007-06-17 19:56:38 -0500859 /* Get all completed mailboxe buffers into the cmplq */
860 spin_lock_irq(&phba->hbalock);
861 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
862 spin_unlock_irq(&phba->hbalock);
863
dea31012005-04-17 16:05:31 -0500864 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -0500865 do {
866 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
867 if (pmb == NULL)
868 break;
869
dea31012005-04-17 16:05:31 -0500870 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -0500871
James Smart858c9f62007-06-17 19:56:39 -0500872 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
873 if (pmb->vport) {
874 lpfc_debugfs_disc_trc(pmb->vport,
875 LPFC_DISC_TRC_MBOX_VPORT,
876 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
877 (uint32_t)pmbox->mbxCommand,
878 pmbox->un.varWords[0],
879 pmbox->un.varWords[1]);
880 }
881 else {
882 lpfc_debugfs_disc_trc(phba->pport,
883 LPFC_DISC_TRC_MBOX,
884 "MBOX cmpl: cmd:x%x mb:x%x x%x",
885 (uint32_t)pmbox->mbxCommand,
886 pmbox->un.varWords[0],
887 pmbox->un.varWords[1]);
888 }
889 }
890
dea31012005-04-17 16:05:31 -0500891 /*
892 * It is a fatal error if unknown mbox command completion.
893 */
894 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
895 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -0500896 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -0500897 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400898 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -0500899 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500900 pmb->vport ? pmb->vport->vpi : 0,
901 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -0500902 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500903 phba->work_hs = HS_FFER3;
904 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500905 continue;
dea31012005-04-17 16:05:31 -0500906 }
907
dea31012005-04-17 16:05:31 -0500908 if (pmbox->mbxStatus) {
909 phba->sli.slistat.mbox_stat_err++;
910 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
911 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -0500912 lpfc_printf_log(phba, KERN_INFO,
913 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400914 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -0500915 "error - RETRYing Data: x%x "
916 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500917 pmb->vport ? pmb->vport->vpi :0,
918 pmbox->mbxCommand,
919 pmbox->mbxStatus,
920 pmbox->un.varWords[0],
921 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -0500922 pmbox->mbxStatus = 0;
923 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -0500924 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500925 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -0500926 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500927 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
928 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -0500929 continue;
dea31012005-04-17 16:05:31 -0500930 }
931 }
932
933 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -0500934 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400935 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -0500936 "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 -0500937 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -0500938 pmbox->mbxCommand,
939 pmb->mbox_cmpl,
940 *((uint32_t *) pmbox),
941 pmbox->un.varWords[0],
942 pmbox->un.varWords[1],
943 pmbox->un.varWords[2],
944 pmbox->un.varWords[3],
945 pmbox->un.varWords[4],
946 pmbox->un.varWords[5],
947 pmbox->un.varWords[6],
948 pmbox->un.varWords[7]);
949
James Smart92d7f7b2007-06-17 19:56:38 -0500950 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -0500951 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -0500952 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -0500953 return 0;
dea31012005-04-17 16:05:31 -0500954}
James Smart92d7f7b2007-06-17 19:56:38 -0500955
956static struct lpfc_dmabuf *
957lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
958{
959 struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
James Smart51ef4c22007-08-02 11:10:31 -0400960 uint32_t hbqno;
961 void *virt; /* virtual address ptr */
962 dma_addr_t phys; /* mapped address */
James Smart3163f722008-02-08 18:50:25 -0500963 unsigned long flags;
964
965 /* Check whether HBQ is still in use */
966 spin_lock_irqsave(&phba->hbalock, flags);
967 if (!phba->hbq_in_use) {
968 spin_unlock_irqrestore(&phba->hbalock, flags);
969 return NULL;
970 }
James Smart92d7f7b2007-06-17 19:56:38 -0500971
972 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
James Smart3163f722008-02-08 18:50:25 -0500973 if (hbq_entry == NULL) {
974 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500975 return NULL;
James Smart3163f722008-02-08 18:50:25 -0500976 }
James Smart92d7f7b2007-06-17 19:56:38 -0500977 list_del(&hbq_entry->dbuf.list);
James Smart51ef4c22007-08-02 11:10:31 -0400978
979 hbqno = tag >> 16;
980 new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart3163f722008-02-08 18:50:25 -0500981 if (new_hbq_entry == NULL) {
982 list_add_tail(&hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
983 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500984 return &hbq_entry->dbuf;
James Smart3163f722008-02-08 18:50:25 -0500985 }
James Smart92d7f7b2007-06-17 19:56:38 -0500986 new_hbq_entry->tag = -1;
James Smart51ef4c22007-08-02 11:10:31 -0400987 phys = new_hbq_entry->dbuf.phys;
988 virt = new_hbq_entry->dbuf.virt;
989 new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
990 new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
991 hbq_entry->dbuf.phys = phys;
992 hbq_entry->dbuf.virt = virt;
James Smart92d7f7b2007-06-17 19:56:38 -0500993 lpfc_sli_free_hbq(phba, hbq_entry);
James Smart3163f722008-02-08 18:50:25 -0500994 list_add_tail(&new_hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
995 spin_unlock_irqrestore(&phba->hbalock, flags);
996
James Smart92d7f7b2007-06-17 19:56:38 -0500997 return &new_hbq_entry->dbuf;
998}
999
James Smart76bb24e2007-10-27 13:38:00 -04001000static struct lpfc_dmabuf *
1001lpfc_sli_get_buff(struct lpfc_hba *phba,
1002 struct lpfc_sli_ring *pring,
1003 uint32_t tag)
1004{
1005 if (tag & QUE_BUFTAG_BIT)
1006 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1007 else
1008 return lpfc_sli_replace_hbqbuff(phba, tag);
1009}
James Smart57127f12007-10-27 13:37:05 -04001010
dea31012005-04-17 16:05:31 -05001011static int
1012lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1013 struct lpfc_iocbq *saveq)
1014{
1015 IOCB_t * irsp;
1016 WORD5 * w5p;
1017 uint32_t Rctl, Type;
1018 uint32_t match, i;
James Smart76bb24e2007-10-27 13:38:00 -04001019 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001020 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001021
1022 match = 0;
1023 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001024
James Smart9c2face2008-01-11 01:53:18 -05001025 if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
1026 return 1;
James Smart57127f12007-10-27 13:37:05 -04001027 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1028 if (pring->lpfc_sli_rcv_async_status)
1029 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1030 else
1031 lpfc_printf_log(phba,
1032 KERN_WARNING,
1033 LOG_SLI,
1034 "0316 Ring %d handler: unexpected "
1035 "ASYNC_STATUS iocb received evt_code "
1036 "0x%x\n",
1037 pring->ringno,
1038 irsp->un.asyncstat.evt_code);
1039 return 1;
1040 }
1041
James Smart3163f722008-02-08 18:50:25 -05001042 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1043 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1044 if (irsp->ulpBdeCount > 0) {
1045 dmzbuf = lpfc_sli_get_buff(phba, pring,
1046 irsp->un.ulpWord[3]);
1047 lpfc_in_buf_free(phba, dmzbuf);
1048 }
1049
1050 if (irsp->ulpBdeCount > 1) {
1051 dmzbuf = lpfc_sli_get_buff(phba, pring,
1052 irsp->unsli3.sli3Words[3]);
1053 lpfc_in_buf_free(phba, dmzbuf);
1054 }
1055
1056 if (irsp->ulpBdeCount > 2) {
1057 dmzbuf = lpfc_sli_get_buff(phba, pring,
1058 irsp->unsli3.sli3Words[7]);
1059 lpfc_in_buf_free(phba, dmzbuf);
1060 }
1061
1062 return 1;
1063 }
1064
James Smart92d7f7b2007-06-17 19:56:38 -05001065 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001066 if (irsp->ulpBdeCount != 0) {
1067 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001068 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001069 if (!saveq->context2)
1070 lpfc_printf_log(phba,
1071 KERN_ERR,
1072 LOG_SLI,
1073 "0341 Ring %d Cannot find buffer for "
1074 "an unsolicited iocb. tag 0x%x\n",
1075 pring->ringno,
1076 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001077 }
1078 if (irsp->ulpBdeCount == 2) {
1079 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04001080 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04001081 if (!saveq->context3)
1082 lpfc_printf_log(phba,
1083 KERN_ERR,
1084 LOG_SLI,
1085 "0342 Ring %d Cannot find buffer for an"
1086 " unsolicited iocb. tag 0x%x\n",
1087 pring->ringno,
1088 irsp->unsli3.sli3Words[7]);
1089 }
1090 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04001091 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04001092 if (irsp->ulpBdeCount != 0) {
1093 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1094 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05001095 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04001096 lpfc_printf_log(phba,
1097 KERN_ERR,
1098 LOG_SLI,
1099 "0343 Ring %d Cannot find "
1100 "buffer for an unsolicited iocb"
1101 ". tag 0x%x\n", pring->ringno,
1102 irsp->un.ulpWord[3]);
1103 }
1104 if (irsp->ulpBdeCount == 2) {
1105 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1106 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05001107 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04001108 lpfc_printf_log(phba,
1109 KERN_ERR,
1110 LOG_SLI,
1111 "0344 Ring %d Cannot find "
1112 "buffer for an unsolicited "
1113 "iocb. tag 0x%x\n",
1114 pring->ringno,
1115 irsp->unsli3.sli3Words[7]);
1116 }
1117 }
James Smart92d7f7b2007-06-17 19:56:38 -05001118 }
James Smart9c2face2008-01-11 01:53:18 -05001119 if (irsp->ulpBdeCount != 0 &&
1120 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1121 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1122 int found = 0;
1123
1124 /* search continue save q for same XRI */
1125 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1126 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1127 list_add_tail(&saveq->list, &iocbq->list);
1128 found = 1;
1129 break;
1130 }
1131 }
1132 if (!found)
1133 list_add_tail(&saveq->clist,
1134 &pring->iocb_continue_saveq);
1135 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1136 list_del_init(&iocbq->clist);
1137 saveq = iocbq;
1138 irsp = &(saveq->iocb);
1139 } else
1140 return 0;
1141 }
1142 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1143 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1144 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1145 Rctl = FC_ELS_REQ;
1146 Type = FC_ELS_DATA;
1147 } else {
1148 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1149 Rctl = w5p->hcsw.Rctl;
1150 Type = w5p->hcsw.Type;
1151
1152 /* Firmware Workaround */
1153 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1154 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1155 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1156 Rctl = FC_ELS_REQ;
1157 Type = FC_ELS_DATA;
1158 w5p->hcsw.Rctl = Rctl;
1159 w5p->hcsw.Type = Type;
1160 }
1161 }
James Smart92d7f7b2007-06-17 19:56:38 -05001162
dea31012005-04-17 16:05:31 -05001163 /* unSolicited Responses */
1164 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001165 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1166 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1167 saveq);
dea31012005-04-17 16:05:31 -05001168 match = 1;
1169 } else {
1170 /* We must search, based on rctl / type
1171 for the right routine */
James Smart9c2face2008-01-11 01:53:18 -05001172 for (i = 0; i < pring->num_mask; i++) {
1173 if ((pring->prt[i].rctl == Rctl)
1174 && (pring->prt[i].type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001175 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1176 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1177 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -05001178 match = 1;
1179 break;
1180 }
1181 }
1182 }
1183 if (match == 0) {
1184 /* Unexpected Rctl / Type received */
1185 /* Ring <ringno> handler: unexpected
1186 Rctl <Rctl> Type <Type> received */
James Smart92d7f7b2007-06-17 19:56:38 -05001187 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001188 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001189 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001190 pring->ringno, Rctl, Type);
dea31012005-04-17 16:05:31 -05001191 }
James Smart92d7f7b2007-06-17 19:56:38 -05001192 return 1;
dea31012005-04-17 16:05:31 -05001193}
1194
1195static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001196lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1197 struct lpfc_sli_ring *pring,
1198 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001199{
dea31012005-04-17 16:05:31 -05001200 struct lpfc_iocbq *cmd_iocb = NULL;
1201 uint16_t iotag;
1202
James Bottomley604a3e32005-10-29 10:28:33 -05001203 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001204
James Bottomley604a3e32005-10-29 10:28:33 -05001205 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1206 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001207 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001208 pring->txcmplq_cnt--;
1209 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001210 }
1211
dea31012005-04-17 16:05:31 -05001212 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001213 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001214 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001215 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001216 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001217 return NULL;
1218}
1219
1220static int
James Smart2e0fef82007-06-17 19:56:36 -05001221lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001222 struct lpfc_iocbq *saveq)
1223{
James Smart2e0fef82007-06-17 19:56:36 -05001224 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001225 int rc = 1;
1226 unsigned long iflag;
1227
1228 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001229 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001230 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001231 spin_unlock_irqrestore(&phba->hbalock, iflag);
1232
dea31012005-04-17 16:05:31 -05001233 if (cmdiocbp) {
1234 if (cmdiocbp->iocb_cmpl) {
1235 /*
1236 * Post all ELS completions to the worker thread.
1237 * All other are passed to the completion callback.
1238 */
1239 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001240 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1241 cmdiocbp->iocb_flag &=
1242 ~LPFC_DRIVER_ABORTED;
1243 saveq->iocb.ulpStatus =
1244 IOSTAT_LOCAL_REJECT;
1245 saveq->iocb.un.ulpWord[4] =
1246 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05001247
1248 /* Firmware could still be in progress
1249 * of DMAing payload, so don't free data
1250 * buffer till after a hbeat.
1251 */
1252 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04001253 }
dea31012005-04-17 16:05:31 -05001254 }
James Smart2e0fef82007-06-17 19:56:36 -05001255 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001256 } else
1257 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001258 } else {
1259 /*
1260 * Unknown initiating command based on the response iotag.
1261 * This could be the case on the ELS ring because of
1262 * lpfc_els_abort().
1263 */
1264 if (pring->ringno != LPFC_ELS_RING) {
1265 /*
1266 * Ring <ringno> handler: unexpected completion IoTag
1267 * <IoTag>
1268 */
James Smarte8b62012007-08-02 11:10:09 -04001269 lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1270 "0322 Ring %d handler: "
1271 "unexpected completion IoTag x%x "
1272 "Data: x%x x%x x%x x%x\n",
1273 pring->ringno,
1274 saveq->iocb.ulpIoTag,
1275 saveq->iocb.ulpStatus,
1276 saveq->iocb.un.ulpWord[4],
1277 saveq->iocb.ulpCommand,
1278 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001279 }
1280 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001281
dea31012005-04-17 16:05:31 -05001282 return rc;
1283}
1284
James Smart2e0fef82007-06-17 19:56:36 -05001285static void
1286lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001287{
James Smarted957682007-06-17 19:56:37 -05001288 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1289 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1290 &phba->slim2p->mbx.us.s2.port[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001291 /*
1292 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1293 * rsp ring <portRspMax>
1294 */
1295 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001296 "0312 Ring %d handler: portRspPut %d "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001297 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001298 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001299 pring->numRiocb);
1300
James Smart2e0fef82007-06-17 19:56:36 -05001301 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001302
1303 /*
1304 * All error attention handlers are posted to
1305 * worker thread
1306 */
1307 phba->work_ha |= HA_ERATT;
1308 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001309
James Smart5e9d9b82008-06-14 22:52:53 -04001310 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001311
1312 return;
1313}
1314
James Smart2e0fef82007-06-17 19:56:36 -05001315void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001316{
James Smart2e0fef82007-06-17 19:56:36 -05001317 struct lpfc_sli *psli = &phba->sli;
1318 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001319 IOCB_t *irsp = NULL;
1320 IOCB_t *entry = NULL;
1321 struct lpfc_iocbq *cmdiocbq = NULL;
1322 struct lpfc_iocbq rspiocbq;
1323 struct lpfc_pgp *pgp;
1324 uint32_t status;
1325 uint32_t portRspPut, portRspMax;
1326 int type;
1327 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001328 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001329 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001330
1331 pring->stats.iocb_event++;
1332
James Smarted957682007-06-17 19:56:37 -05001333 pgp = (phba->sli_rev == 3) ?
1334 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1335 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1336
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001337
1338 /*
1339 * The next available response entry should never exceed the maximum
1340 * entries. If it does, treat it as an adapter hardware error.
1341 */
1342 portRspMax = pring->numRiocb;
1343 portRspPut = le32_to_cpu(pgp->rspPutInx);
1344 if (unlikely(portRspPut >= portRspMax)) {
1345 lpfc_sli_rsp_pointers_error(phba, pring);
1346 return;
1347 }
1348
1349 rmb();
1350 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001351 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001352 if (++pring->rspidx >= portRspMax)
1353 pring->rspidx = 0;
1354
1355 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1356 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001357 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001358 irsp = &rspiocbq.iocb;
1359 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1360 pring->stats.iocb_rsp++;
1361 rsp_cmpl++;
1362
1363 if (unlikely(irsp->ulpStatus)) {
1364 /* Rsp ring <ringno> error: IOCB */
1365 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001366 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001367 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001368 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001369 irsp->un.ulpWord[0],
1370 irsp->un.ulpWord[1],
1371 irsp->un.ulpWord[2],
1372 irsp->un.ulpWord[3],
1373 irsp->un.ulpWord[4],
1374 irsp->un.ulpWord[5],
1375 *(((uint32_t *) irsp) + 6),
1376 *(((uint32_t *) irsp) + 7));
1377 }
1378
1379 switch (type) {
1380 case LPFC_ABORT_IOCB:
1381 case LPFC_SOL_IOCB:
1382 /*
1383 * Idle exchange closed via ABTS from port. No iocb
1384 * resources need to be recovered.
1385 */
1386 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001387 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001388 "0314 IOCB cmd 0x%x "
1389 "processed. Skipping "
1390 "completion",
James Smartdca94792006-08-01 07:34:08 -04001391 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001392 break;
1393 }
1394
James Smart2e0fef82007-06-17 19:56:36 -05001395 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001396 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1397 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001398 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001399 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1400 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1401 &rspiocbq);
1402 }
1403 break;
1404 default:
1405 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1406 char adaptermsg[LPFC_MAX_ADPTMSG];
1407 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1408 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1409 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001410 dev_warn(&((phba->pcidev)->dev),
1411 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001412 phba->brd_no, adaptermsg);
1413 } else {
1414 /* Unknown IOCB command */
1415 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001416 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001417 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001418 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001419 irsp->ulpStatus,
1420 irsp->ulpIoTag,
1421 irsp->ulpContext);
1422 }
1423 break;
1424 }
1425
1426 /*
1427 * The response IOCB has been processed. Update the ring
1428 * pointer in SLIM. If the port response put pointer has not
1429 * been updated, sync the pgp->rspPutInx and fetch the new port
1430 * response put pointer.
1431 */
James Smarted957682007-06-17 19:56:37 -05001432 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001433
1434 if (pring->rspidx == portRspPut)
1435 portRspPut = le32_to_cpu(pgp->rspPutInx);
1436 }
1437
1438 ha_copy = readl(phba->HAregaddr);
1439 ha_copy >>= (LPFC_FCP_RING * 4);
1440
1441 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001442 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001443 pring->stats.iocb_rsp_full++;
1444 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1445 writel(status, phba->CAregaddr);
1446 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001447 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001448 }
1449 if ((ha_copy & HA_R0CE_RSP) &&
1450 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001451 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001452 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1453 pring->stats.iocb_cmd_empty++;
1454
1455 /* Force update of the local copy of cmdGetInx */
1456 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1457 lpfc_sli_resume_iocb(phba, pring);
1458
1459 if ((pring->lpfc_sli_cmd_available))
1460 (pring->lpfc_sli_cmd_available) (phba, pring);
1461
James Smart2e0fef82007-06-17 19:56:36 -05001462 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001463 }
1464
1465 return;
1466}
1467
dea31012005-04-17 16:05:31 -05001468/*
1469 * This routine presumes LPFC_FCP_RING handling and doesn't bother
1470 * to check it explicitly.
1471 */
1472static int
James Smart2e0fef82007-06-17 19:56:36 -05001473lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1474 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001475{
James Smarted957682007-06-17 19:56:37 -05001476 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1477 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1478 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001479 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001480 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001481 struct lpfc_iocbq *cmdiocbq = NULL;
1482 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001483 uint32_t status;
1484 uint32_t portRspPut, portRspMax;
1485 int rc = 1;
1486 lpfc_iocb_type type;
1487 unsigned long iflag;
1488 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05001489
James Smart2e0fef82007-06-17 19:56:36 -05001490 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001491 pring->stats.iocb_event++;
1492
dea31012005-04-17 16:05:31 -05001493 /*
1494 * The next available response entry should never exceed the maximum
1495 * entries. If it does, treat it as an adapter hardware error.
1496 */
1497 portRspMax = pring->numRiocb;
1498 portRspPut = le32_to_cpu(pgp->rspPutInx);
1499 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001500 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05001501 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001502 return 1;
1503 }
1504
1505 rmb();
1506 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001507 /*
1508 * Fetch an entry off the ring and copy it into a local data
1509 * structure. The copy involves a byte-swap since the
1510 * network byte order and pci byte orders are different.
1511 */
James Smarted957682007-06-17 19:56:37 -05001512 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05001513 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001514
1515 if (++pring->rspidx >= portRspMax)
1516 pring->rspidx = 0;
1517
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001518 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1519 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05001520 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05001521 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001522 irsp = &rspiocbq.iocb;
1523
dea31012005-04-17 16:05:31 -05001524 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1525 pring->stats.iocb_rsp++;
1526 rsp_cmpl++;
1527
1528 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001529 /*
1530 * If resource errors reported from HBA, reduce
1531 * queuedepths of the SCSI device.
1532 */
1533 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1534 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1535 spin_unlock_irqrestore(&phba->hbalock, iflag);
1536 lpfc_adjust_queue_depth(phba);
1537 spin_lock_irqsave(&phba->hbalock, iflag);
1538 }
1539
dea31012005-04-17 16:05:31 -05001540 /* Rsp ring <ringno> error: IOCB */
1541 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001542 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05001543 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001544 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05001545 irsp->un.ulpWord[0],
1546 irsp->un.ulpWord[1],
1547 irsp->un.ulpWord[2],
1548 irsp->un.ulpWord[3],
1549 irsp->un.ulpWord[4],
1550 irsp->un.ulpWord[5],
1551 *(((uint32_t *) irsp) + 6),
1552 *(((uint32_t *) irsp) + 7));
dea31012005-04-17 16:05:31 -05001553 }
1554
1555 switch (type) {
1556 case LPFC_ABORT_IOCB:
1557 case LPFC_SOL_IOCB:
1558 /*
1559 * Idle exchange closed via ABTS from port. No iocb
1560 * resources need to be recovered.
1561 */
1562 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001563 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001564 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04001565 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05001566 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04001567 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05001568 break;
1569 }
1570
James Bottomley604a3e32005-10-29 10:28:33 -05001571 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1572 &rspiocbq);
dea31012005-04-17 16:05:31 -05001573 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001574 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1575 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1576 &rspiocbq);
1577 } else {
James Smart2e0fef82007-06-17 19:56:36 -05001578 spin_unlock_irqrestore(&phba->hbalock,
1579 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001580 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1581 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001582 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001583 iflag);
1584 }
dea31012005-04-17 16:05:31 -05001585 }
1586 break;
James Smarta4bc3372006-12-02 13:34:16 -05001587 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05001588 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001589 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001590 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001591 break;
dea31012005-04-17 16:05:31 -05001592 default:
1593 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1594 char adaptermsg[LPFC_MAX_ADPTMSG];
1595 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1596 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1597 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001598 dev_warn(&((phba->pcidev)->dev),
1599 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05001600 phba->brd_no, adaptermsg);
1601 } else {
1602 /* Unknown IOCB command */
1603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001604 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05001605 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001606 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05001607 irsp->ulpStatus,
1608 irsp->ulpIoTag,
1609 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05001610 }
1611 break;
1612 }
1613
1614 /*
1615 * The response IOCB has been processed. Update the ring
1616 * pointer in SLIM. If the port response put pointer has not
1617 * been updated, sync the pgp->rspPutInx and fetch the new port
1618 * response put pointer.
1619 */
James Smarted957682007-06-17 19:56:37 -05001620 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05001621
1622 if (pring->rspidx == portRspPut)
1623 portRspPut = le32_to_cpu(pgp->rspPutInx);
1624 }
1625
1626 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
1627 pring->stats.iocb_rsp_full++;
1628 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1629 writel(status, phba->CAregaddr);
1630 readl(phba->CAregaddr);
1631 }
1632 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1633 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1634 pring->stats.iocb_cmd_empty++;
1635
1636 /* Force update of the local copy of cmdGetInx */
1637 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1638 lpfc_sli_resume_iocb(phba, pring);
1639
1640 if ((pring->lpfc_sli_cmd_available))
1641 (pring->lpfc_sli_cmd_available) (phba, pring);
1642
1643 }
1644
James Smart2e0fef82007-06-17 19:56:36 -05001645 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001646 return rc;
1647}
1648
dea31012005-04-17 16:05:31 -05001649int
James Smart2e0fef82007-06-17 19:56:36 -05001650lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
1651 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001652{
James Smarted957682007-06-17 19:56:37 -05001653 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1654 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1655 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001656 IOCB_t *entry;
1657 IOCB_t *irsp = NULL;
1658 struct lpfc_iocbq *rspiocbp = NULL;
1659 struct lpfc_iocbq *next_iocb;
1660 struct lpfc_iocbq *cmdiocbp;
1661 struct lpfc_iocbq *saveq;
dea31012005-04-17 16:05:31 -05001662 uint8_t iocb_cmd_type;
1663 lpfc_iocb_type type;
1664 uint32_t status, free_saveq;
1665 uint32_t portRspPut, portRspMax;
1666 int rc = 1;
1667 unsigned long iflag;
dea31012005-04-17 16:05:31 -05001668
James Smart2e0fef82007-06-17 19:56:36 -05001669 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001670 pring->stats.iocb_event++;
1671
dea31012005-04-17 16:05:31 -05001672 /*
1673 * The next available response entry should never exceed the maximum
1674 * entries. If it does, treat it as an adapter hardware error.
1675 */
1676 portRspMax = pring->numRiocb;
1677 portRspPut = le32_to_cpu(pgp->rspPutInx);
1678 if (portRspPut >= portRspMax) {
1679 /*
1680 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1681 * rsp ring <portRspMax>
1682 */
James Smarted957682007-06-17 19:56:37 -05001683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001684 "0303 Ring %d handler: portRspPut %d "
dea31012005-04-17 16:05:31 -05001685 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001686 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05001687
James Smart2e0fef82007-06-17 19:56:36 -05001688 phba->link_state = LPFC_HBA_ERROR;
1689 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001690
1691 phba->work_hs = HS_FFER3;
1692 lpfc_handle_eratt(phba);
1693
1694 return 1;
1695 }
1696
1697 rmb();
dea31012005-04-17 16:05:31 -05001698 while (pring->rspidx != portRspPut) {
1699 /*
1700 * Build a completion list and call the appropriate handler.
1701 * The process is to get the next available response iocb, get
1702 * a free iocb from the list, copy the response data into the
1703 * free iocb, insert to the continuation list, and update the
1704 * next response index to slim. This process makes response
1705 * iocb's in the ring available to DMA as fast as possible but
1706 * pays a penalty for a copy operation. Since the iocb is
1707 * only 32 bytes, this penalty is considered small relative to
1708 * the PCI reads for register values and a slim write. When
1709 * the ulpLe field is set, the entire Command has been
1710 * received.
1711 */
James Smarted957682007-06-17 19:56:37 -05001712 entry = lpfc_resp_iocb(phba, pring);
1713
James Smart858c9f62007-06-17 19:56:39 -05001714 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05001715 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001716 if (rspiocbp == NULL) {
1717 printk(KERN_ERR "%s: out of buffers! Failing "
1718 "completion.\n", __FUNCTION__);
1719 break;
1720 }
1721
James Smarted957682007-06-17 19:56:37 -05001722 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
1723 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05001724 irsp = &rspiocbp->iocb;
1725
1726 if (++pring->rspidx >= portRspMax)
1727 pring->rspidx = 0;
1728
James Smarta58cbd52007-08-02 11:09:43 -04001729 if (pring->ringno == LPFC_ELS_RING) {
1730 lpfc_debugfs_slow_ring_trc(phba,
1731 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1732 *(((uint32_t *) irsp) + 4),
1733 *(((uint32_t *) irsp) + 6),
1734 *(((uint32_t *) irsp) + 7));
1735 }
1736
James Smarted957682007-06-17 19:56:37 -05001737 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05001738
James Smart9c2face2008-01-11 01:53:18 -05001739 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
dea31012005-04-17 16:05:31 -05001740
1741 pring->iocb_continueq_cnt++;
1742 if (irsp->ulpLe) {
1743 /*
1744 * By default, the driver expects to free all resources
1745 * associated with this iocb completion.
1746 */
1747 free_saveq = 1;
1748 saveq = list_get_first(&pring->iocb_continueq,
1749 struct lpfc_iocbq, list);
1750 irsp = &(saveq->iocb);
1751 list_del_init(&pring->iocb_continueq);
1752 pring->iocb_continueq_cnt = 0;
1753
1754 pring->stats.iocb_rsp++;
1755
James Smart92d7f7b2007-06-17 19:56:38 -05001756 /*
1757 * If resource errors reported from HBA, reduce
1758 * queuedepths of the SCSI device.
1759 */
1760 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1761 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1762 spin_unlock_irqrestore(&phba->hbalock, iflag);
1763 lpfc_adjust_queue_depth(phba);
1764 spin_lock_irqsave(&phba->hbalock, iflag);
1765 }
1766
dea31012005-04-17 16:05:31 -05001767 if (irsp->ulpStatus) {
1768 /* Rsp ring <ringno> error: IOCB */
James Smarted957682007-06-17 19:56:37 -05001769 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001770 "0328 Rsp Ring %d error: "
James Smarted957682007-06-17 19:56:37 -05001771 "IOCB Data: "
1772 "x%x x%x x%x x%x "
1773 "x%x x%x x%x x%x "
1774 "x%x x%x x%x x%x "
1775 "x%x x%x x%x x%x\n",
James Smarted957682007-06-17 19:56:37 -05001776 pring->ringno,
1777 irsp->un.ulpWord[0],
1778 irsp->un.ulpWord[1],
1779 irsp->un.ulpWord[2],
1780 irsp->un.ulpWord[3],
1781 irsp->un.ulpWord[4],
1782 irsp->un.ulpWord[5],
1783 *(((uint32_t *) irsp) + 6),
1784 *(((uint32_t *) irsp) + 7),
1785 *(((uint32_t *) irsp) + 8),
1786 *(((uint32_t *) irsp) + 9),
1787 *(((uint32_t *) irsp) + 10),
1788 *(((uint32_t *) irsp) + 11),
1789 *(((uint32_t *) irsp) + 12),
1790 *(((uint32_t *) irsp) + 13),
1791 *(((uint32_t *) irsp) + 14),
1792 *(((uint32_t *) irsp) + 15));
dea31012005-04-17 16:05:31 -05001793 }
1794
1795 /*
1796 * Fetch the IOCB command type and call the correct
1797 * completion routine. Solicited and Unsolicited
1798 * IOCBs on the ELS ring get freed back to the
1799 * lpfc_iocb_list by the discovery kernel thread.
1800 */
1801 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
1802 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
1803 if (type == LPFC_SOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05001804 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001805 rc = lpfc_sli_process_sol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05001806 saveq);
1807 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001808 } else if (type == LPFC_UNSOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05001809 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001810 rc = lpfc_sli_process_unsol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05001811 saveq);
1812 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9c2face2008-01-11 01:53:18 -05001813 if (!rc)
1814 free_saveq = 0;
dea31012005-04-17 16:05:31 -05001815 } else if (type == LPFC_ABORT_IOCB) {
1816 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
1817 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05001818 lpfc_sli_iocbq_lookup(phba, pring,
1819 saveq)))) {
dea31012005-04-17 16:05:31 -05001820 /* Call the specified completion
1821 routine */
1822 if (cmdiocbp->iocb_cmpl) {
1823 spin_unlock_irqrestore(
James Smart2e0fef82007-06-17 19:56:36 -05001824 &phba->hbalock,
dea31012005-04-17 16:05:31 -05001825 iflag);
1826 (cmdiocbp->iocb_cmpl) (phba,
1827 cmdiocbp, saveq);
1828 spin_lock_irqsave(
James Smart2e0fef82007-06-17 19:56:36 -05001829 &phba->hbalock,
dea31012005-04-17 16:05:31 -05001830 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001831 } else
James Smart2e0fef82007-06-17 19:56:36 -05001832 __lpfc_sli_release_iocbq(phba,
James Bottomley604a3e32005-10-29 10:28:33 -05001833 cmdiocbp);
dea31012005-04-17 16:05:31 -05001834 }
1835 } else if (type == LPFC_UNKNOWN_IOCB) {
1836 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1837
1838 char adaptermsg[LPFC_MAX_ADPTMSG];
1839
1840 memset(adaptermsg, 0,
1841 LPFC_MAX_ADPTMSG);
1842 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1843 MAX_MSG_DATA);
1844 dev_warn(&((phba->pcidev)->dev),
Joe Perches898eb712007-10-18 03:06:30 -07001845 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05001846 phba->brd_no, adaptermsg);
1847 } else {
1848 /* Unknown IOCB command */
James Smart92d7f7b2007-06-17 19:56:38 -05001849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001850 "0335 Unknown IOCB "
James Smart92d7f7b2007-06-17 19:56:38 -05001851 "command Data: x%x "
1852 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001853 irsp->ulpCommand,
1854 irsp->ulpStatus,
1855 irsp->ulpIoTag,
1856 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05001857 }
1858 }
1859
1860 if (free_saveq) {
James Smart2e0fef82007-06-17 19:56:36 -05001861 list_for_each_entry_safe(rspiocbp, next_iocb,
1862 &saveq->list, list) {
1863 list_del(&rspiocbp->list);
1864 __lpfc_sli_release_iocbq(phba,
1865 rspiocbp);
dea31012005-04-17 16:05:31 -05001866 }
James Smart2e0fef82007-06-17 19:56:36 -05001867 __lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05001868 }
James Smart92d7f7b2007-06-17 19:56:38 -05001869 rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05001870 }
1871
1872 /*
1873 * If the port response put pointer has not been updated, sync
1874 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
1875 * response put pointer.
1876 */
1877 if (pring->rspidx == portRspPut) {
1878 portRspPut = le32_to_cpu(pgp->rspPutInx);
1879 }
1880 } /* while (pring->rspidx != portRspPut) */
1881
James Smart92d7f7b2007-06-17 19:56:38 -05001882 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05001883 /* At least one response entry has been freed */
1884 pring->stats.iocb_rsp_full++;
1885 /* SET RxRE_RSP in Chip Att register */
1886 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1887 writel(status, phba->CAregaddr);
1888 readl(phba->CAregaddr); /* flush */
1889 }
1890 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1891 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1892 pring->stats.iocb_cmd_empty++;
1893
1894 /* Force update of the local copy of cmdGetInx */
1895 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1896 lpfc_sli_resume_iocb(phba, pring);
1897
1898 if ((pring->lpfc_sli_cmd_available))
1899 (pring->lpfc_sli_cmd_available) (phba, pring);
1900
1901 }
1902
James Smart2e0fef82007-06-17 19:56:36 -05001903 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001904 return rc;
1905}
1906
James Smart2e0fef82007-06-17 19:56:36 -05001907void
dea31012005-04-17 16:05:31 -05001908lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1909{
James Smart2534ba72007-04-25 09:52:20 -04001910 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001911 struct lpfc_iocbq *iocb, *next_iocb;
James Smart2534ba72007-04-25 09:52:20 -04001912 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05001913
James Smart92d7f7b2007-06-17 19:56:38 -05001914 if (pring->ringno == LPFC_ELS_RING) {
1915 lpfc_fabric_abort_hba(phba);
1916 }
1917
dea31012005-04-17 16:05:31 -05001918 /* Error everything on txq and txcmplq
1919 * First do the txq.
1920 */
James Smart2e0fef82007-06-17 19:56:36 -05001921 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04001922 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05001923 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05001924
1925 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04001926 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
1927 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1928
James Smart2e0fef82007-06-17 19:56:36 -05001929 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04001930
1931 while (!list_empty(&completions)) {
1932 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
dea31012005-04-17 16:05:31 -05001933 cmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001934 list_del_init(&iocb->list);
dea31012005-04-17 16:05:31 -05001935
James Smart2e0fef82007-06-17 19:56:36 -05001936 if (!iocb->iocb_cmpl)
1937 lpfc_sli_release_iocbq(phba, iocb);
1938 else {
dea31012005-04-17 16:05:31 -05001939 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1940 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
dea31012005-04-17 16:05:31 -05001941 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05001942 }
dea31012005-04-17 16:05:31 -05001943 }
dea31012005-04-17 16:05:31 -05001944}
1945
Jamie Wellnitz41415862006-02-28 19:25:27 -05001946int
James Smart2e0fef82007-06-17 19:56:36 -05001947lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05001948{
Jamie Wellnitz41415862006-02-28 19:25:27 -05001949 uint32_t status;
1950 int i = 0;
1951 int retval = 0;
dea31012005-04-17 16:05:31 -05001952
Jamie Wellnitz41415862006-02-28 19:25:27 -05001953 /* Read the HBA Host Status Register */
1954 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05001955
Jamie Wellnitz41415862006-02-28 19:25:27 -05001956 /*
1957 * Check status register every 100ms for 5 retries, then every
1958 * 500ms for 5, then every 2.5 sec for 5, then reset board and
1959 * every 2.5 sec for 4.
1960 * Break our of the loop if errors occurred during init.
1961 */
1962 while (((status & mask) != mask) &&
1963 !(status & HS_FFERM) &&
1964 i++ < 20) {
dea31012005-04-17 16:05:31 -05001965
Jamie Wellnitz41415862006-02-28 19:25:27 -05001966 if (i <= 5)
1967 msleep(10);
1968 else if (i <= 10)
1969 msleep(500);
1970 else
1971 msleep(2500);
dea31012005-04-17 16:05:31 -05001972
Jamie Wellnitz41415862006-02-28 19:25:27 -05001973 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05001974 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05001975 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001976 lpfc_sli_brdrestart(phba);
1977 }
1978 /* Read the HBA Host Status Register */
1979 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05001980 }
dea31012005-04-17 16:05:31 -05001981
Jamie Wellnitz41415862006-02-28 19:25:27 -05001982 /* Check to see if any errors occurred during init */
1983 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05001984 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001985 retval = 1;
1986 }
dea31012005-04-17 16:05:31 -05001987
Jamie Wellnitz41415862006-02-28 19:25:27 -05001988 return retval;
dea31012005-04-17 16:05:31 -05001989}
1990
James Smart92908312006-03-07 15:04:13 -05001991#define BARRIER_TEST_PATTERN (0xdeadbeef)
1992
James Smart2e0fef82007-06-17 19:56:36 -05001993void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05001994{
James Smart65a29c12006-07-06 15:50:50 -04001995 uint32_t __iomem *resp_buf;
1996 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05001997 volatile uint32_t mbox;
1998 uint32_t hc_copy;
1999 int i;
2000 uint8_t hdrtype;
2001
2002 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2003 if (hdrtype != 0x80 ||
2004 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2005 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2006 return;
2007
2008 /*
2009 * Tell the other part of the chip to suspend temporarily all
2010 * its DMA activity.
2011 */
James Smart65a29c12006-07-06 15:50:50 -04002012 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002013
2014 /* Disable the error attention */
2015 hc_copy = readl(phba->HCregaddr);
2016 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2017 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002018 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002019
2020 if (readl(phba->HAregaddr) & HA_ERATT) {
2021 /* Clear Chip error bit */
2022 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002023 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002024 }
2025
2026 mbox = 0;
2027 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2028 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2029
2030 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04002031 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002032 writel(mbox, mbox_buf);
2033
2034 for (i = 0;
2035 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2036 mdelay(1);
2037
2038 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2039 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05002040 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05002041 goto restore_hc;
2042 else
2043 goto clear_errat;
2044 }
2045
2046 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2047 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
2048 mdelay(1);
2049
2050clear_errat:
2051
2052 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2053 mdelay(1);
2054
2055 if (readl(phba->HAregaddr) & HA_ERATT) {
2056 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002057 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002058 }
2059
2060restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05002061 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002062 writel(hc_copy, phba->HCregaddr);
2063 readl(phba->HCregaddr); /* flush */
2064}
2065
Jamie Wellnitz41415862006-02-28 19:25:27 -05002066int
James Smart2e0fef82007-06-17 19:56:36 -05002067lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002068{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002069 struct lpfc_sli *psli;
2070 LPFC_MBOXQ_t *pmb;
2071 uint32_t status;
2072 uint32_t ha_copy;
2073 int retval;
2074 int i = 0;
2075
2076 psli = &phba->sli;
2077
2078 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05002079 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002080 "0329 Kill HBA Data: x%x x%x\n",
2081 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002082
James Smart98c9ea52007-10-27 13:37:33 -04002083 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2084 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002085 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002086
2087 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05002088 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002089 status = readl(phba->HCregaddr);
2090 status &= ~HC_ERINT_ENA;
2091 writel(status, phba->HCregaddr);
2092 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002093 phba->link_flag |= LS_IGNORE_ERATT;
2094 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002095
2096 lpfc_kill_board(phba, pmb);
2097 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2098 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2099
2100 if (retval != MBX_SUCCESS) {
2101 if (retval != MBX_BUSY)
2102 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002103 spin_lock_irq(&phba->hbalock);
2104 phba->link_flag &= ~LS_IGNORE_ERATT;
2105 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002106 return 1;
2107 }
2108
James Smart92908312006-03-07 15:04:13 -05002109 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2110
Jamie Wellnitz41415862006-02-28 19:25:27 -05002111 mempool_free(pmb, phba->mbox_mem_pool);
2112
2113 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2114 * attention every 100ms for 3 seconds. If we don't get ERATT after
2115 * 3 seconds we still set HBA_ERROR state because the status of the
2116 * board is now undefined.
2117 */
2118 ha_copy = readl(phba->HAregaddr);
2119
2120 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2121 mdelay(100);
2122 ha_copy = readl(phba->HAregaddr);
2123 }
2124
2125 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05002126 if (ha_copy & HA_ERATT) {
2127 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002128 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002129 }
James Smart2e0fef82007-06-17 19:56:36 -05002130 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002131 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002132 phba->link_flag &= ~LS_IGNORE_ERATT;
2133 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002134
2135 psli->mbox_active = NULL;
2136 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002137 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002138
James Smart2e0fef82007-06-17 19:56:36 -05002139 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002140}
2141
2142int
James Smart2e0fef82007-06-17 19:56:36 -05002143lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002144{
2145 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002146 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002147 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05002148 int i;
dea31012005-04-17 16:05:31 -05002149
Jamie Wellnitz41415862006-02-28 19:25:27 -05002150 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002151
Jamie Wellnitz41415862006-02-28 19:25:27 -05002152 /* Reset HBA */
2153 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002154 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002155 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05002156
2157 /* perform board reset */
2158 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002159 phba->pport->fc_myDID = 0;
2160 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002161
Jamie Wellnitz41415862006-02-28 19:25:27 -05002162 /* Turn off parity checking and serr during the physical reset */
2163 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2164 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2165 (cfg_value &
2166 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2167
James Smart1c067a42006-08-01 07:33:52 -04002168 psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002169 /* Now toggle INITFF bit in the Host Control Register */
2170 writel(HC_INITFF, phba->HCregaddr);
2171 mdelay(1);
2172 readl(phba->HCregaddr); /* flush */
2173 writel(0, phba->HCregaddr);
2174 readl(phba->HCregaddr); /* flush */
2175
2176 /* Restore PCI cmd register */
2177 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002178
2179 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002180 for (i = 0; i < psli->num_rings; i++) {
2181 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002182 pring->flag = 0;
2183 pring->rspidx = 0;
2184 pring->next_cmdidx = 0;
2185 pring->local_getidx = 0;
2186 pring->cmdidx = 0;
2187 pring->missbufcnt = 0;
2188 }
dea31012005-04-17 16:05:31 -05002189
James Smart2e0fef82007-06-17 19:56:36 -05002190 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002191 return 0;
2192}
2193
2194int
James Smart2e0fef82007-06-17 19:56:36 -05002195lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002196{
2197 MAILBOX_t *mb;
2198 struct lpfc_sli *psli;
2199 uint16_t skip_post;
2200 volatile uint32_t word0;
2201 void __iomem *to_slim;
2202
James Smart2e0fef82007-06-17 19:56:36 -05002203 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002204
2205 psli = &phba->sli;
2206
2207 /* Restart HBA */
2208 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002209 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002210 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002211
2212 word0 = 0;
2213 mb = (MAILBOX_t *) &word0;
2214 mb->mbxCommand = MBX_RESTART;
2215 mb->mbxHc = 1;
2216
James Smart92908312006-03-07 15:04:13 -05002217 lpfc_reset_barrier(phba);
2218
Jamie Wellnitz41415862006-02-28 19:25:27 -05002219 to_slim = phba->MBslimaddr;
2220 writel(*(uint32_t *) mb, to_slim);
2221 readl(to_slim); /* flush */
2222
2223 /* Only skip post after fc_ffinit is completed */
James Smart2e0fef82007-06-17 19:56:36 -05002224 if (phba->pport->port_state) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002225 skip_post = 1;
2226 word0 = 1; /* This is really setting up word1 */
dea31012005-04-17 16:05:31 -05002227 } else {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002228 skip_post = 0;
2229 word0 = 0; /* This is really setting up word1 */
2230 }
James Smart65a29c12006-07-06 15:50:50 -04002231 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002232 writel(*(uint32_t *) mb, to_slim);
2233 readl(to_slim); /* flush */
2234
2235 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002236 phba->pport->stopped = 0;
2237 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002238
James Smart2e0fef82007-06-17 19:56:36 -05002239 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002240
James Smart64ba8812006-08-02 15:24:34 -04002241 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2242 psli->stats_start = get_seconds();
2243
Jamie Wellnitz41415862006-02-28 19:25:27 -05002244 if (skip_post)
2245 mdelay(100);
2246 else
dea31012005-04-17 16:05:31 -05002247 mdelay(2000);
dea31012005-04-17 16:05:31 -05002248
Jamie Wellnitz41415862006-02-28 19:25:27 -05002249 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002250
2251 return 0;
2252}
2253
2254static int
2255lpfc_sli_chipset_init(struct lpfc_hba *phba)
2256{
2257 uint32_t status, i = 0;
2258
2259 /* Read the HBA Host Status Register */
2260 status = readl(phba->HSregaddr);
2261
2262 /* Check status register to see what current state is */
2263 i = 0;
2264 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2265
2266 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2267 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2268 * 4.
2269 */
2270 if (i++ >= 20) {
2271 /* Adapter failed to init, timeout, status reg
2272 <status> */
James Smarted957682007-06-17 19:56:37 -05002273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002274 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002275 "timeout, status reg x%x, "
2276 "FW Data: A8 x%x AC x%x\n", status,
2277 readl(phba->MBslimaddr + 0xa8),
2278 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002279 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002280 return -ETIMEDOUT;
2281 }
2282
2283 /* Check to see if any errors occurred during init */
2284 if (status & HS_FFERM) {
2285 /* ERROR: During chipset initialization */
2286 /* Adapter failed to init, chipset, status reg
2287 <status> */
James Smarted957682007-06-17 19:56:37 -05002288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002289 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002290 "chipset, status reg x%x, "
2291 "FW Data: A8 x%x AC x%x\n", status,
2292 readl(phba->MBslimaddr + 0xa8),
2293 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002294 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002295 return -EIO;
2296 }
2297
2298 if (i <= 5) {
2299 msleep(10);
2300 } else if (i <= 10) {
2301 msleep(500);
2302 } else {
2303 msleep(2500);
2304 }
2305
2306 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002307 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002308 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002309 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002310 }
2311 /* Read the HBA Host Status Register */
2312 status = readl(phba->HSregaddr);
2313 }
2314
2315 /* Check to see if any errors occurred during init */
2316 if (status & HS_FFERM) {
2317 /* ERROR: During chipset initialization */
2318 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05002319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002320 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05002321 "status reg x%x, "
2322 "FW Data: A8 x%x AC x%x\n", status,
2323 readl(phba->MBslimaddr + 0xa8),
2324 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002325 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002326 return -EIO;
2327 }
2328
2329 /* Clear all interrupt enable conditions */
2330 writel(0, phba->HCregaddr);
2331 readl(phba->HCregaddr); /* flush */
2332
2333 /* setup host attn register */
2334 writel(0xffffffff, phba->HAregaddr);
2335 readl(phba->HAregaddr); /* flush */
2336 return 0;
2337}
2338
James Smart78b2d852007-08-02 11:10:21 -04002339int
James Smarted957682007-06-17 19:56:37 -05002340lpfc_sli_hbq_count(void)
2341{
James Smart92d7f7b2007-06-17 19:56:38 -05002342 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05002343}
2344
2345static int
2346lpfc_sli_hbq_entry_count(void)
2347{
2348 int hbq_count = lpfc_sli_hbq_count();
2349 int count = 0;
2350 int i;
2351
2352 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05002353 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05002354 return count;
2355}
2356
dea31012005-04-17 16:05:31 -05002357int
James Smarted957682007-06-17 19:56:37 -05002358lpfc_sli_hbq_size(void)
2359{
2360 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2361}
2362
2363static int
2364lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2365{
2366 int hbq_count = lpfc_sli_hbq_count();
2367 LPFC_MBOXQ_t *pmb;
2368 MAILBOX_t *pmbox;
2369 uint32_t hbqno;
2370 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05002371
James Smart92d7f7b2007-06-17 19:56:38 -05002372 /* Get a Mailbox buffer to setup mailbox
2373 * commands for HBA initialization
2374 */
James Smarted957682007-06-17 19:56:37 -05002375 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2376
2377 if (!pmb)
2378 return -ENOMEM;
2379
2380 pmbox = &pmb->mb;
2381
2382 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2383 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05002384 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05002385
2386 hbq_entry_index = 0;
2387 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2388 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2389 phba->hbqs[hbqno].hbqPutIdx = 0;
2390 phba->hbqs[hbqno].local_hbqGetIdx = 0;
2391 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05002392 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04002393 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2394 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05002395 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2396
2397 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2398 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2399 mbxStatus <status>, ring <num> */
2400
2401 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002402 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002403 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05002404 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002405 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05002406 pmbox->mbxStatus, hbqno);
2407
2408 phba->link_state = LPFC_HBA_ERROR;
2409 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05002410 return ENXIO;
2411 }
2412 }
2413 phba->hbq_count = hbq_count;
2414
James Smarted957682007-06-17 19:56:37 -05002415 mempool_free(pmb, phba->mbox_mem_pool);
2416
James Smart92d7f7b2007-06-17 19:56:38 -05002417 /* Initially populate or replenish the HBQs */
2418 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2419 if (lpfc_sli_hbqbuf_init_hbqs(phba, hbqno))
2420 return -ENOMEM;
2421 }
James Smarted957682007-06-17 19:56:37 -05002422 return 0;
2423}
2424
2425static int
2426lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05002427{
2428 LPFC_MBOXQ_t *pmb;
2429 uint32_t resetcount = 0, rc = 0, done = 0;
2430
2431 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2432 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05002433 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002434 return -ENOMEM;
2435 }
2436
James Smarted957682007-06-17 19:56:37 -05002437 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05002438 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05002439 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002440 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002441 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002442 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002443 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002444 msleep(2500);
2445 rc = lpfc_sli_chipset_init(phba);
2446 if (rc)
2447 break;
2448
James Smart2e0fef82007-06-17 19:56:36 -05002449 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002450 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002451 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002452 resetcount++;
2453
James Smarted957682007-06-17 19:56:37 -05002454 /* Call pre CONFIG_PORT mailbox command initialization. A
2455 * value of 0 means the call was successful. Any other
2456 * nonzero value is a failure, but if ERESTART is returned,
2457 * the driver may reset the HBA and try again.
2458 */
dea31012005-04-17 16:05:31 -05002459 rc = lpfc_config_port_prep(phba);
2460 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05002461 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05002462 continue;
2463 } else if (rc) {
2464 break;
2465 }
2466
James Smart2e0fef82007-06-17 19:56:36 -05002467 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05002468 lpfc_config_port(phba, pmb);
2469 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smarted957682007-06-17 19:56:37 -05002470 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05002471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002472 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002473 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002474 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05002475 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002476 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002477 spin_unlock_irq(&phba->hbalock);
2478 rc = -ENXIO;
James Smarted957682007-06-17 19:56:37 -05002479 } else {
2480 done = 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002481 phba->max_vpi = (phba->max_vpi &&
2482 pmb->mb.un.varCfgPort.gmv) != 0
2483 ? pmb->mb.un.varCfgPort.max_vpi
2484 : 0;
dea31012005-04-17 16:05:31 -05002485 }
2486 }
James Smarted957682007-06-17 19:56:37 -05002487
2488 if (!done) {
2489 rc = -EINVAL;
2490 goto do_prep_failed;
2491 }
2492
2493 if ((pmb->mb.un.varCfgPort.sli_mode == 3) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002494 (!pmb->mb.un.varCfgPort.cMA)) {
James Smarted957682007-06-17 19:56:37 -05002495 rc = -ENXIO;
James Smarted957682007-06-17 19:56:37 -05002496 }
James Smarted957682007-06-17 19:56:37 -05002497
James Smart92d7f7b2007-06-17 19:56:38 -05002498do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05002499 mempool_free(pmb, phba->mbox_mem_pool);
2500 return rc;
2501}
2502
2503int
2504lpfc_sli_hba_setup(struct lpfc_hba *phba)
2505{
2506 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002507 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05002508
2509 switch (lpfc_sli_mode) {
2510 case 2:
James Smart78b2d852007-08-02 11:10:21 -04002511 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05002512 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002513 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05002514 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04002515 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05002516 break;
2517 }
James Smarted957682007-06-17 19:56:37 -05002518 mode = 2;
2519 break;
2520 case 0:
2521 case 3:
2522 break;
2523 default:
James Smart92d7f7b2007-06-17 19:56:38 -05002524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002525 "1819 Unrecognized lpfc_sli_mode "
2526 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05002527
2528 break;
2529 }
2530
2531 rc = lpfc_do_config_port(phba, mode);
2532 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05002533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002534 "1820 Unable to select SLI-3. "
2535 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05002536 if (rc && mode != 2)
2537 rc = lpfc_do_config_port(phba, 2);
2538 if (rc)
dea31012005-04-17 16:05:31 -05002539 goto lpfc_sli_hba_setup_error;
2540
James Smarted957682007-06-17 19:56:37 -05002541 if (phba->sli_rev == 3) {
2542 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
2543 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
2544 phba->sli3_options |= LPFC_SLI3_ENABLED;
2545 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
2546
2547 } else {
2548 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
2549 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05002550 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05002551 }
2552
2553 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002554 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
2555 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05002556 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05002557
2558 if (rc)
2559 goto lpfc_sli_hba_setup_error;
2560
James Smart92d7f7b2007-06-17 19:56:38 -05002561 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05002562
2563 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2564 rc = lpfc_sli_hbq_setup(phba);
2565 if (rc)
2566 goto lpfc_sli_hba_setup_error;
2567 }
2568
dea31012005-04-17 16:05:31 -05002569 phba->sli.sli_flag |= LPFC_PROCESS_LA;
2570
2571 rc = lpfc_config_port_post(phba);
2572 if (rc)
2573 goto lpfc_sli_hba_setup_error;
2574
James Smarted957682007-06-17 19:56:37 -05002575 return rc;
2576
James Smart92d7f7b2007-06-17 19:56:38 -05002577lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05002578 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05002579 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002580 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05002581 return rc;
2582}
2583
dea31012005-04-17 16:05:31 -05002584/*! lpfc_mbox_timeout
2585 *
2586 * \pre
2587 * \post
2588 * \param hba Pointer to per struct lpfc_hba structure
2589 * \param l1 Pointer to the driver's mailbox queue.
2590 * \return
2591 * void
2592 *
2593 * \b Description:
2594 *
2595 * This routine handles mailbox timeout events at timer interrupt context.
2596 */
2597void
2598lpfc_mbox_timeout(unsigned long ptr)
2599{
James Smart92d7f7b2007-06-17 19:56:38 -05002600 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05002601 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05002602 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05002603
James Smart2e0fef82007-06-17 19:56:36 -05002604 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05002605 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002606 if (!tmo_posted)
2607 phba->pport->work_port_events |= WORKER_MBOX_TMO;
2608 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
2609
James Smart5e9d9b82008-06-14 22:52:53 -04002610 if (!tmo_posted)
2611 lpfc_worker_wake_up(phba);
2612 return;
dea31012005-04-17 16:05:31 -05002613}
2614
2615void
2616lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
2617{
James Smart2e0fef82007-06-17 19:56:36 -05002618 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
2619 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04002620 struct lpfc_sli *psli = &phba->sli;
2621 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002622
James Smart2e0fef82007-06-17 19:56:36 -05002623 if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
dea31012005-04-17 16:05:31 -05002624 return;
2625 }
2626
dea31012005-04-17 16:05:31 -05002627 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05002628 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002629 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002630 mb->mbxCommand,
2631 phba->pport->port_state,
2632 phba->sli.sli_flag,
2633 phba->sli.mbox_active);
dea31012005-04-17 16:05:31 -05002634
James Smart1dcb58e2007-04-25 09:51:30 -04002635 /* Setting state unknown so lpfc_sli_abort_iocb_ring
2636 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
2637 * it to fail all oustanding SCSI IO.
2638 */
James Smart2e0fef82007-06-17 19:56:36 -05002639 spin_lock_irq(&phba->pport->work_port_lock);
2640 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
2641 spin_unlock_irq(&phba->pport->work_port_lock);
2642 spin_lock_irq(&phba->hbalock);
2643 phba->link_state = LPFC_LINK_UNKNOWN;
James Smart1dcb58e2007-04-25 09:51:30 -04002644 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002645 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04002646
2647 pring = &psli->ring[psli->fcp_ring];
2648 lpfc_sli_abort_iocb_ring(phba, pring);
2649
2650 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04002651 "0345 Resetting board due to mailbox timeout\n");
James Smart1dcb58e2007-04-25 09:51:30 -04002652 /*
2653 * lpfc_offline calls lpfc_sli_hba_down which will clean up
2654 * on oustanding mailbox commands.
2655 */
James Smart13815c82008-01-11 01:52:48 -05002656 /* If resets are disabled then set error state and return. */
2657 if (!phba->cfg_enable_hba_reset) {
2658 phba->link_state = LPFC_HBA_ERROR;
2659 return;
2660 }
James Smart1dcb58e2007-04-25 09:51:30 -04002661 lpfc_offline_prep(phba);
2662 lpfc_offline(phba);
2663 lpfc_sli_brdrestart(phba);
James Smart58da1ff2008-04-07 10:15:56 -04002664 lpfc_online(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04002665 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002666 return;
2667}
2668
2669int
James Smart2e0fef82007-06-17 19:56:36 -05002670lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
dea31012005-04-17 16:05:31 -05002671{
dea31012005-04-17 16:05:31 -05002672 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05002673 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002674 uint32_t status, evtctr;
2675 uint32_t ha_copy;
2676 int i;
James Smart09372822008-01-11 01:52:54 -05002677 unsigned long timeout;
dea31012005-04-17 16:05:31 -05002678 unsigned long drvr_flag = 0;
2679 volatile uint32_t word0, ldata;
2680 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04002681 int processing_queue = 0;
2682
2683 spin_lock_irqsave(&phba->hbalock, drvr_flag);
2684 if (!pmbox) {
2685 /* processing mbox queue from intr_handler */
2686 processing_queue = 1;
2687 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2688 pmbox = lpfc_mbox_get(phba);
2689 if (!pmbox) {
2690 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2691 return MBX_SUCCESS;
2692 }
2693 }
dea31012005-04-17 16:05:31 -05002694
James Smarted957682007-06-17 19:56:37 -05002695 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05002696 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05002697 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04002698 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05002699 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002700 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002701 "1806 Mbox x%x failed. No vport\n",
James Smarted957682007-06-17 19:56:37 -05002702 pmbox->mb.mbxCommand);
2703 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04002704 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05002705 }
2706 }
2707
Linas Vepstas8d63f372007-02-14 14:28:36 -06002708 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04002709 if (unlikely(pci_channel_offline(phba->pcidev))) {
2710 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2711 goto out_not_finished;
2712 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06002713
dea31012005-04-17 16:05:31 -05002714 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05002715
dea31012005-04-17 16:05:31 -05002716 mb = &pmbox->mb;
2717 status = MBX_SUCCESS;
2718
James Smart2e0fef82007-06-17 19:56:36 -05002719 if (phba->link_state == LPFC_HBA_ERROR) {
2720 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002721
2722 /* Mbox command <mbxCommand> cannot issue */
James Smart7f5f3d02008-02-08 18:50:14 -05002723 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04002724 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002725 }
2726
James Smart92908312006-03-07 15:04:13 -05002727 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
2728 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05002729 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart7f5f3d02008-02-08 18:50:14 -05002730 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04002731 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05002732 }
2733
dea31012005-04-17 16:05:31 -05002734 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
2735 /* Polling for a mbox command when another one is already active
2736 * is not allowed in SLI. Also, the driver must have established
2737 * SLI2 mode to queue and process multiple mbox commands.
2738 */
2739
2740 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05002741 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002742
2743 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002744 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04002745 goto out_not_finished;
dea31012005-04-17 16:05:31 -05002746 }
2747
2748 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002749 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002750 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002751 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04002752 goto out_not_finished;
dea31012005-04-17 16:05:31 -05002753 }
2754
dea31012005-04-17 16:05:31 -05002755 /* Another mailbox command is still being processed, queue this
2756 * command to be processed later.
2757 */
2758 lpfc_mbox_put(phba, pmbox);
2759
2760 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05002761 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002762 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002763 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002764 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
2765 mb->mbxCommand, phba->pport->port_state,
2766 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05002767
2768 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05002769 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002770
James Smart858c9f62007-06-17 19:56:39 -05002771 if (pmbox->vport) {
2772 lpfc_debugfs_disc_trc(pmbox->vport,
2773 LPFC_DISC_TRC_MBOX_VPORT,
2774 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
2775 (uint32_t)mb->mbxCommand,
2776 mb->un.varWords[0], mb->un.varWords[1]);
2777 }
2778 else {
2779 lpfc_debugfs_disc_trc(phba->pport,
2780 LPFC_DISC_TRC_MBOX,
2781 "MBOX Bsy: cmd:x%x mb:x%x x%x",
2782 (uint32_t)mb->mbxCommand,
2783 mb->un.varWords[0], mb->un.varWords[1]);
2784 }
2785
James Smart2e0fef82007-06-17 19:56:36 -05002786 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05002787 }
2788
dea31012005-04-17 16:05:31 -05002789 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2790
2791 /* If we are not polling, we MUST be in SLI2 mode */
2792 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002793 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
2794 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05002795 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002796 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002797 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002798 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04002799 goto out_not_finished;
dea31012005-04-17 16:05:31 -05002800 }
2801 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04002802 mod_timer(&psli->mbox_tmo, (jiffies +
2803 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05002804 }
2805
2806 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05002807 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002808 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002809 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002810 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05002811 mb->mbxCommand, phba->pport->port_state,
2812 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05002813
James Smart858c9f62007-06-17 19:56:39 -05002814 if (mb->mbxCommand != MBX_HEARTBEAT) {
2815 if (pmbox->vport) {
2816 lpfc_debugfs_disc_trc(pmbox->vport,
2817 LPFC_DISC_TRC_MBOX_VPORT,
2818 "MBOX Send vport: cmd:x%x mb:x%x x%x",
2819 (uint32_t)mb->mbxCommand,
2820 mb->un.varWords[0], mb->un.varWords[1]);
2821 }
2822 else {
2823 lpfc_debugfs_disc_trc(phba->pport,
2824 LPFC_DISC_TRC_MBOX,
2825 "MBOX Send: cmd:x%x mb:x%x x%x",
2826 (uint32_t)mb->mbxCommand,
2827 mb->un.varWords[0], mb->un.varWords[1]);
2828 }
2829 }
2830
dea31012005-04-17 16:05:31 -05002831 psli->slistat.mbox_cmd++;
2832 evtctr = psli->slistat.mbox_event;
2833
2834 /* next set own bit for the adapter and copy over command word */
2835 mb->mbxOwner = OWN_CHIP;
2836
2837 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002838 /* First copy command data to host SLIM area */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002839 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002840 } else {
James Smart92908312006-03-07 15:04:13 -05002841 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05002842 /* copy command data into host mbox for cmpl */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002843 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
James Smart92d7f7b2007-06-17 19:56:38 -05002844 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002845 }
2846
2847 /* First copy mbox command data to HBA SLIM, skip past first
2848 word */
2849 to_slim = phba->MBslimaddr + sizeof (uint32_t);
2850 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
2851 MAILBOX_CMD_SIZE - sizeof (uint32_t));
2852
2853 /* Next copy over first word, with mbxOwner set */
2854 ldata = *((volatile uint32_t *)mb);
2855 to_slim = phba->MBslimaddr;
2856 writel(ldata, to_slim);
2857 readl(to_slim); /* flush */
2858
2859 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2860 /* switch over to host mailbox */
2861 psli->sli_flag |= LPFC_SLI2_ACTIVE;
2862 }
2863 }
2864
2865 wmb();
dea31012005-04-17 16:05:31 -05002866
2867 switch (flag) {
2868 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05002869 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05002870 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05002871 /* Interrupt board to do it */
2872 writel(CA_MBATT, phba->CAregaddr);
2873 readl(phba->CAregaddr); /* flush */
2874 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05002875 break;
2876
2877 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05002878 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05002879 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05002880 /* Interrupt board to do it */
2881 writel(CA_MBATT, phba->CAregaddr);
2882 readl(phba->CAregaddr); /* flush */
2883
dea31012005-04-17 16:05:31 -05002884 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2885 /* First read mbox status word */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002886 word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002887 word0 = le32_to_cpu(word0);
2888 } else {
2889 /* First read mbox status word */
2890 word0 = readl(phba->MBslimaddr);
2891 }
2892
2893 /* Read the HBA Host Attention Register */
2894 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05002895 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2896 mb->mbxCommand) *
2897 1000) + jiffies;
2898 i = 0;
dea31012005-04-17 16:05:31 -05002899 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002900 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
2901 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05002902 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05002903 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05002904 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002905 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05002906 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04002907 goto out_not_finished;
dea31012005-04-17 16:05:31 -05002908 }
2909
2910 /* Check if we took a mbox interrupt while we were
2911 polling */
2912 if (((word0 & OWN_CHIP) != OWN_CHIP)
2913 && (evtctr != psli->slistat.mbox_event))
2914 break;
2915
James Smart09372822008-01-11 01:52:54 -05002916 if (i++ > 10) {
2917 spin_unlock_irqrestore(&phba->hbalock,
2918 drvr_flag);
2919 msleep(1);
2920 spin_lock_irqsave(&phba->hbalock, drvr_flag);
2921 }
dea31012005-04-17 16:05:31 -05002922
2923 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2924 /* First copy command data */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002925 word0 = *((volatile uint32_t *)
2926 &phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002927 word0 = le32_to_cpu(word0);
2928 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2929 MAILBOX_t *slimmb;
2930 volatile uint32_t slimword0;
2931 /* Check real SLIM for any errors */
2932 slimword0 = readl(phba->MBslimaddr);
2933 slimmb = (MAILBOX_t *) & slimword0;
2934 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
2935 && slimmb->mbxStatus) {
2936 psli->sli_flag &=
2937 ~LPFC_SLI2_ACTIVE;
2938 word0 = slimword0;
2939 }
2940 }
2941 } else {
2942 /* First copy command data */
2943 word0 = readl(phba->MBslimaddr);
2944 }
2945 /* Read the HBA Host Attention Register */
2946 ha_copy = readl(phba->HAregaddr);
2947 }
2948
2949 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002950 /* copy results back to user */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002951 lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
James Smart92d7f7b2007-06-17 19:56:38 -05002952 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002953 } else {
2954 /* First copy command data */
2955 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2956 MAILBOX_CMD_SIZE);
2957 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2958 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05002959 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05002960 phba->MBslimaddr + DMP_RSP_OFFSET,
2961 mb->un.varDmp.word_cnt);
2962 }
2963 }
2964
2965 writel(HA_MBATT, phba->HAregaddr);
2966 readl(phba->HAregaddr); /* flush */
2967
2968 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2969 status = mb->mbxStatus;
2970 }
2971
James Smart2e0fef82007-06-17 19:56:36 -05002972 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2973 return status;
James Smart58da1ff2008-04-07 10:15:56 -04002974
2975out_not_finished:
2976 if (processing_queue) {
2977 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
2978 lpfc_mbox_cmpl_put(phba, pmbox);
2979 }
2980 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05002981}
2982
James Smart92d7f7b2007-06-17 19:56:38 -05002983/*
2984 * Caller needs to hold lock.
2985 */
James Smart858c9f62007-06-17 19:56:39 -05002986static void
James Smart92d7f7b2007-06-17 19:56:38 -05002987__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05002988 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05002989{
2990 /* Insert the caller's iocb in the txq tail for later processing. */
2991 list_add_tail(&piocb->list, &pring->txq);
2992 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05002993}
2994
2995static struct lpfc_iocbq *
2996lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05002997 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05002998{
2999 struct lpfc_iocbq * nextiocb;
3000
3001 nextiocb = lpfc_sli_ringtx_get(phba, pring);
3002 if (!nextiocb) {
3003 nextiocb = *piocb;
3004 *piocb = NULL;
3005 }
3006
3007 return nextiocb;
3008}
3009
James Smart92d7f7b2007-06-17 19:56:38 -05003010/*
3011 * Lockless version of lpfc_sli_issue_iocb.
3012 */
James Smart98c9ea52007-10-27 13:37:33 -04003013static int
James Smart92d7f7b2007-06-17 19:56:38 -05003014__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003015 struct lpfc_iocbq *piocb, uint32_t flag)
3016{
3017 struct lpfc_iocbq *nextiocb;
3018 IOCB_t *iocb;
3019
James Smart92d7f7b2007-06-17 19:56:38 -05003020 if (piocb->iocb_cmpl && (!piocb->vport) &&
3021 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
3022 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
3023 lpfc_printf_log(phba, KERN_ERR,
3024 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003025 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003026 piocb->iocb.ulpCommand);
3027 dump_stack();
3028 return IOCB_ERROR;
3029 }
3030
3031
Linas Vepstas8d63f372007-02-14 14:28:36 -06003032 /* If the PCI channel is in offline state, do not post iocbs. */
3033 if (unlikely(pci_channel_offline(phba->pcidev)))
3034 return IOCB_ERROR;
3035
dea31012005-04-17 16:05:31 -05003036 /*
3037 * We should never get an IOCB if we are in a < LINK_DOWN state
3038 */
James Smart2e0fef82007-06-17 19:56:36 -05003039 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003040 return IOCB_ERROR;
3041
3042 /*
3043 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04003044 * outstanding event.
dea31012005-04-17 16:05:31 -05003045 */
James Smart0b727fe2007-10-27 13:37:25 -04003046 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05003047 goto iocb_busy;
3048
James Smart2e0fef82007-06-17 19:56:36 -05003049 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05003050 /*
James Smart2680eea2007-04-25 09:52:55 -04003051 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05003052 * can be issued if the link is not up.
3053 */
3054 switch (piocb->iocb.ulpCommand) {
3055 case CMD_QUE_RING_BUF_CN:
3056 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05003057 /*
3058 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
3059 * completion, iocb_cmpl MUST be 0.
3060 */
3061 if (piocb->iocb_cmpl)
3062 piocb->iocb_cmpl = NULL;
3063 /*FALLTHROUGH*/
3064 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04003065 case CMD_CLOSE_XRI_CN:
3066 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05003067 break;
3068 default:
3069 goto iocb_busy;
3070 }
3071
3072 /*
3073 * For FCP commands, we must be in a state where we can process link
3074 * attention events.
3075 */
3076 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05003077 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05003078 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05003079 }
dea31012005-04-17 16:05:31 -05003080
dea31012005-04-17 16:05:31 -05003081 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
3082 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
3083 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
3084
3085 if (iocb)
3086 lpfc_sli_update_ring(phba, pring);
3087 else
3088 lpfc_sli_update_full_ring(phba, pring);
3089
3090 if (!piocb)
3091 return IOCB_SUCCESS;
3092
3093 goto out_busy;
3094
3095 iocb_busy:
3096 pring->stats.iocb_cmd_delay++;
3097
3098 out_busy:
3099
3100 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003101 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05003102 return IOCB_SUCCESS;
3103 }
3104
3105 return IOCB_BUSY;
3106}
3107
James Smart92d7f7b2007-06-17 19:56:38 -05003108
3109int
3110lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3111 struct lpfc_iocbq *piocb, uint32_t flag)
3112{
3113 unsigned long iflags;
3114 int rc;
3115
3116 spin_lock_irqsave(&phba->hbalock, iflags);
3117 rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
3118 spin_unlock_irqrestore(&phba->hbalock, iflags);
3119
3120 return rc;
3121}
3122
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003123static int
3124lpfc_extra_ring_setup( struct lpfc_hba *phba)
3125{
3126 struct lpfc_sli *psli;
3127 struct lpfc_sli_ring *pring;
3128
3129 psli = &phba->sli;
3130
3131 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05003132
3133 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003134 pring = &psli->ring[psli->fcp_ring];
3135 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3136 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3137 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3138 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3139
James Smarta4bc3372006-12-02 13:34:16 -05003140 /* and give them to the extra ring */
3141 pring = &psli->ring[psli->extra_ring];
3142
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003143 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3144 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3145 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3146 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3147
3148 /* Setup default profile for this ring */
3149 pring->iotag_max = 4096;
3150 pring->num_mask = 1;
3151 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05003152 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
3153 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003154 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
3155 return 0;
3156}
3157
James Smart98c9ea52007-10-27 13:37:33 -04003158static void
James Smart57127f12007-10-27 13:37:05 -04003159lpfc_sli_async_event_handler(struct lpfc_hba * phba,
3160 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
3161{
3162 IOCB_t *icmd;
3163 uint16_t evt_code;
3164 uint16_t temp;
3165 struct temp_event temp_event_data;
3166 struct Scsi_Host *shost;
3167
3168 icmd = &iocbq->iocb;
3169 evt_code = icmd->un.asyncstat.evt_code;
3170 temp = icmd->ulpContext;
3171
3172 if ((evt_code != ASYNC_TEMP_WARN) &&
3173 (evt_code != ASYNC_TEMP_SAFE)) {
3174 lpfc_printf_log(phba,
3175 KERN_ERR,
3176 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003177 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smart57127f12007-10-27 13:37:05 -04003178 " evt_code 0x%x\n",
3179 pring->ringno,
3180 icmd->un.asyncstat.evt_code);
3181 return;
3182 }
3183 temp_event_data.data = (uint32_t)temp;
3184 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
3185 if (evt_code == ASYNC_TEMP_WARN) {
3186 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
3187 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003188 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003189 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04003190 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04003191 "corrective action. temperature : %d Celsius\n",
3192 temp);
3193 }
3194 if (evt_code == ASYNC_TEMP_SAFE) {
3195 temp_event_data.event_code = LPFC_NORMAL_TEMP;
3196 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003197 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003198 LOG_TEMP,
3199 "0340 Adapter temperature is OK now. "
3200 "temperature : %d Celsius\n",
3201 temp);
3202 }
3203
3204 /* Send temperature change event to applications */
3205 shost = lpfc_shost_from_vport(phba->pport);
3206 fc_host_post_vendor_event(shost, fc_get_event_number(),
3207 sizeof(temp_event_data), (char *) &temp_event_data,
3208 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
3209
3210}
3211
3212
dea31012005-04-17 16:05:31 -05003213int
3214lpfc_sli_setup(struct lpfc_hba *phba)
3215{
James Smarted957682007-06-17 19:56:37 -05003216 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05003217 struct lpfc_sli *psli = &phba->sli;
3218 struct lpfc_sli_ring *pring;
3219
3220 psli->num_rings = MAX_CONFIGURED_RINGS;
3221 psli->sli_flag = 0;
3222 psli->fcp_ring = LPFC_FCP_RING;
3223 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05003224 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05003225
James Bottomley604a3e32005-10-29 10:28:33 -05003226 psli->iocbq_lookup = NULL;
3227 psli->iocbq_lookup_len = 0;
3228 psli->last_iotag = 0;
3229
dea31012005-04-17 16:05:31 -05003230 for (i = 0; i < psli->num_rings; i++) {
3231 pring = &psli->ring[i];
3232 switch (i) {
3233 case LPFC_FCP_RING: /* ring 0 - FCP */
3234 /* numCiocb and numRiocb are used in config_port */
3235 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3236 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3237 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3238 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3239 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3240 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003241 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003242 SLI3_IOCB_CMD_SIZE :
3243 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003244 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003245 SLI3_IOCB_RSP_SIZE :
3246 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003247 pring->iotag_ctr = 0;
3248 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05003249 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05003250 pring->fast_iotag = pring->iotag_max;
3251 pring->num_mask = 0;
3252 break;
James Smarta4bc3372006-12-02 13:34:16 -05003253 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05003254 /* numCiocb and numRiocb are used in config_port */
3255 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
3256 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003257 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003258 SLI3_IOCB_CMD_SIZE :
3259 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003260 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003261 SLI3_IOCB_RSP_SIZE :
3262 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05003263 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05003264 pring->num_mask = 0;
3265 break;
3266 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
3267 /* numCiocb and numRiocb are used in config_port */
3268 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
3269 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003270 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003271 SLI3_IOCB_CMD_SIZE :
3272 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003273 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003274 SLI3_IOCB_RSP_SIZE :
3275 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003276 pring->fast_iotag = 0;
3277 pring->iotag_ctr = 0;
3278 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04003279 pring->lpfc_sli_rcv_async_status =
3280 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05003281 pring->num_mask = 4;
3282 pring->prt[0].profile = 0; /* Mask 0 */
3283 pring->prt[0].rctl = FC_ELS_REQ;
3284 pring->prt[0].type = FC_ELS_DATA;
3285 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003286 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003287 pring->prt[1].profile = 0; /* Mask 1 */
3288 pring->prt[1].rctl = FC_ELS_RSP;
3289 pring->prt[1].type = FC_ELS_DATA;
3290 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003291 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003292 pring->prt[2].profile = 0; /* Mask 2 */
3293 /* NameServer Inquiry */
3294 pring->prt[2].rctl = FC_UNSOL_CTL;
3295 /* NameServer */
3296 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
3297 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003298 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003299 pring->prt[3].profile = 0; /* Mask 3 */
3300 /* NameServer response */
3301 pring->prt[3].rctl = FC_SOL_CTL;
3302 /* NameServer */
3303 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
3304 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003305 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003306 break;
3307 }
James Smarted957682007-06-17 19:56:37 -05003308 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05003309 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05003310 }
James Smarted957682007-06-17 19:56:37 -05003311 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05003312 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04003313 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
3314 "SLI2 SLIM Data: x%x x%lx\n",
3315 phba->brd_no, totiocbsize,
3316 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05003317 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003318 if (phba->cfg_multi_ring_support == 2)
3319 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05003320
3321 return 0;
3322}
3323
3324int
James Smart2e0fef82007-06-17 19:56:36 -05003325lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003326{
3327 struct lpfc_sli *psli;
3328 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05003329 int i;
dea31012005-04-17 16:05:31 -05003330
3331 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003332 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003333 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05003334 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05003335 /* Initialize list headers for txq and txcmplq as double linked lists */
3336 for (i = 0; i < psli->num_rings; i++) {
3337 pring = &psli->ring[i];
3338 pring->ringno = i;
3339 pring->next_cmdidx = 0;
3340 pring->local_getidx = 0;
3341 pring->cmdidx = 0;
3342 INIT_LIST_HEAD(&pring->txq);
3343 INIT_LIST_HEAD(&pring->txcmplq);
3344 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05003345 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05003346 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05003347 }
James Smart2e0fef82007-06-17 19:56:36 -05003348 spin_unlock_irq(&phba->hbalock);
3349 return 1;
dea31012005-04-17 16:05:31 -05003350}
3351
3352int
James Smart92d7f7b2007-06-17 19:56:38 -05003353lpfc_sli_host_down(struct lpfc_vport *vport)
3354{
James Smart858c9f62007-06-17 19:56:39 -05003355 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05003356 struct lpfc_hba *phba = vport->phba;
3357 struct lpfc_sli *psli = &phba->sli;
3358 struct lpfc_sli_ring *pring;
3359 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003360 int i;
3361 unsigned long flags = 0;
3362 uint16_t prev_pring_flag;
3363
3364 lpfc_cleanup_discovery_resources(vport);
3365
3366 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05003367 for (i = 0; i < psli->num_rings; i++) {
3368 pring = &psli->ring[i];
3369 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04003370 /* Only slow rings */
3371 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05003372 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04003373 /* Set the lpfc data pending flag */
3374 set_bit(LPFC_DATA_READY, &phba->data_flags);
3375 }
James Smart92d7f7b2007-06-17 19:56:38 -05003376 /*
3377 * Error everything on the txq since these iocbs have not been
3378 * given to the FW yet.
3379 */
James Smart92d7f7b2007-06-17 19:56:38 -05003380 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3381 if (iocb->vport != vport)
3382 continue;
James Smart858c9f62007-06-17 19:56:39 -05003383 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05003384 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05003385 }
3386
3387 /* Next issue ABTS for everything on the txcmplq */
3388 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
3389 list) {
3390 if (iocb->vport != vport)
3391 continue;
3392 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3393 }
3394
3395 pring->flag = prev_pring_flag;
3396 }
3397
3398 spin_unlock_irqrestore(&phba->hbalock, flags);
3399
James Smart858c9f62007-06-17 19:56:39 -05003400 while (!list_empty(&completions)) {
3401 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
3402
3403 if (!iocb->iocb_cmpl)
3404 lpfc_sli_release_iocbq(phba, iocb);
3405 else {
3406 iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3407 iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
3408 (iocb->iocb_cmpl) (phba, iocb, iocb);
3409 }
3410 }
James Smart92d7f7b2007-06-17 19:56:38 -05003411 return 1;
3412}
3413
3414int
James Smart2e0fef82007-06-17 19:56:36 -05003415lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003416{
James Smart2534ba72007-04-25 09:52:20 -04003417 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05003418 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003419 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05003420 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05003421 LPFC_MBOXQ_t *pmb;
James Smart2534ba72007-04-25 09:52:20 -04003422 struct lpfc_iocbq *iocb;
3423 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05003424 int i;
3425 unsigned long flags = 0;
3426
dea31012005-04-17 16:05:31 -05003427 lpfc_hba_down_prep(phba);
3428
James Smart92d7f7b2007-06-17 19:56:38 -05003429 lpfc_fabric_abort_hba(phba);
3430
James Smart2e0fef82007-06-17 19:56:36 -05003431 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05003432 for (i = 0; i < psli->num_rings; i++) {
3433 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04003434 /* Only slow rings */
3435 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05003436 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04003437 /* Set the lpfc data pending flag */
3438 set_bit(LPFC_DATA_READY, &phba->data_flags);
3439 }
dea31012005-04-17 16:05:31 -05003440
3441 /*
3442 * Error everything on the txq since these iocbs have not been
3443 * given to the FW yet.
3444 */
James Smart2534ba72007-04-25 09:52:20 -04003445 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003446 pring->txq_cnt = 0;
3447
dea31012005-04-17 16:05:31 -05003448 }
James Smart2e0fef82007-06-17 19:56:36 -05003449 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05003450
James Smart2534ba72007-04-25 09:52:20 -04003451 while (!list_empty(&completions)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003452 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
James Smart2534ba72007-04-25 09:52:20 -04003453 cmd = &iocb->iocb;
James Smart2534ba72007-04-25 09:52:20 -04003454
James Smart2e0fef82007-06-17 19:56:36 -05003455 if (!iocb->iocb_cmpl)
3456 lpfc_sli_release_iocbq(phba, iocb);
3457 else {
James Smart2534ba72007-04-25 09:52:20 -04003458 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3459 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
3460 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05003461 }
James Smart2534ba72007-04-25 09:52:20 -04003462 }
3463
James Smart0ff10d42008-01-11 01:52:36 -05003464 spin_lock_irqsave(&phba->hbalock, flags);
3465 list_splice_init(&phba->elsbuf, &completions);
3466 phba->elsbuf_cnt = 0;
3467 phba->elsbuf_prev_cnt = 0;
3468 spin_unlock_irqrestore(&phba->hbalock, flags);
3469
3470 while (!list_empty(&completions)) {
3471 list_remove_head(&completions, buf_ptr,
3472 struct lpfc_dmabuf, list);
3473 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3474 kfree(buf_ptr);
3475 }
3476
dea31012005-04-17 16:05:31 -05003477 /* Return any active mbox cmds */
3478 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05003479 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05003480
3481 spin_lock(&phba->pport->work_port_lock);
3482 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3483 spin_unlock(&phba->pport->work_port_lock);
3484
James Smart97eab632008-04-07 10:16:05 -04003485 /* Return any pending or completed mbox cmds */
3486 list_splice_init(&phba->sli.mboxq, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05003487 if (psli->mbox_active) {
3488 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05003489 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003490 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05003491 }
James Smart92d7f7b2007-06-17 19:56:38 -05003492 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05003493 spin_unlock_irqrestore(&phba->hbalock, flags);
3494
3495 while (!list_empty(&completions)) {
3496 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05003497 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
James Smart97eab632008-04-07 10:16:05 -04003498 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05003499 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05003500 }
dea31012005-04-17 16:05:31 -05003501 return 1;
3502}
3503
3504void
3505lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
3506{
3507 uint32_t *src = srcp;
3508 uint32_t *dest = destp;
3509 uint32_t ldata;
3510 int i;
3511
3512 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
3513 ldata = *src;
3514 ldata = le32_to_cpu(ldata);
3515 *dest = ldata;
3516 src++;
3517 dest++;
3518 }
3519}
3520
3521int
James Smart2e0fef82007-06-17 19:56:36 -05003522lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3523 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05003524{
3525 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
3526 later */
James Smart2e0fef82007-06-17 19:56:36 -05003527 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003528 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05003529 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05003530 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003531 return 0;
3532}
3533
James Smart76bb24e2007-10-27 13:38:00 -04003534uint32_t
3535lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
3536{
3537 spin_lock_irq(&phba->hbalock);
3538 phba->buffer_tag_count++;
3539 /*
3540 * Always set the QUE_BUFTAG_BIT to distiguish between
3541 * a tag assigned by HBQ.
3542 */
3543 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
3544 spin_unlock_irq(&phba->hbalock);
3545 return phba->buffer_tag_count;
3546}
3547
3548struct lpfc_dmabuf *
3549lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3550 uint32_t tag)
3551{
3552 struct lpfc_dmabuf *mp, *next_mp;
3553 struct list_head *slp = &pring->postbufq;
3554
3555 /* Search postbufq, from the begining, looking for a match on tag */
3556 spin_lock_irq(&phba->hbalock);
3557 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
3558 if (mp->buffer_tag == tag) {
3559 list_del_init(&mp->list);
3560 pring->postbufq_cnt--;
3561 spin_unlock_irq(&phba->hbalock);
3562 return mp;
3563 }
3564 }
3565
3566 spin_unlock_irq(&phba->hbalock);
3567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3568 "0410 Cannot find virtual addr for buffer tag on "
3569 "ring %d Data x%lx x%p x%p x%x\n",
3570 pring->ringno, (unsigned long) tag,
3571 slp->next, slp->prev, pring->postbufq_cnt);
3572
3573 return NULL;
3574}
dea31012005-04-17 16:05:31 -05003575
3576struct lpfc_dmabuf *
3577lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3578 dma_addr_t phys)
3579{
3580 struct lpfc_dmabuf *mp, *next_mp;
3581 struct list_head *slp = &pring->postbufq;
3582
3583 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05003584 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003585 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
3586 if (mp->phys == phys) {
3587 list_del_init(&mp->list);
3588 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05003589 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003590 return mp;
3591 }
3592 }
3593
James Smart2e0fef82007-06-17 19:56:36 -05003594 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003596 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05003597 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003598 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05003599 slp->next, slp->prev, pring->postbufq_cnt);
3600 return NULL;
3601}
3602
3603static void
James Smart2e0fef82007-06-17 19:56:36 -05003604lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3605 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05003606{
James Smart2e0fef82007-06-17 19:56:36 -05003607 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04003608 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05003609 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04003610 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3611
3612 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04003613
3614 if (irsp->ulpStatus) {
3615 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
3616 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
3617
James Smart2e0fef82007-06-17 19:56:36 -05003618 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04003619 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
3620 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
3621
James Smart92d7f7b2007-06-17 19:56:38 -05003622 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003623 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05003624 "with tag %x context %x, abort status %x, "
3625 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04003626 abort_iocb, abort_iotag, abort_context,
3627 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04003628
3629 /*
James Smart58da1ff2008-04-07 10:15:56 -04003630 * If the iocb is not found in Firmware queue the iocb
3631 * might have completed already. Do not free it again.
3632 */
James Smart9b379602008-04-07 10:16:00 -04003633 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04003634 spin_unlock_irq(&phba->hbalock);
3635 lpfc_sli_release_iocbq(phba, cmdiocb);
3636 return;
3637 }
3638 /*
James Smart2680eea2007-04-25 09:52:55 -04003639 * make sure we have the right iocbq before taking it
3640 * off the txcmplq and try to call completion routine.
3641 */
James Smart2e0fef82007-06-17 19:56:36 -05003642 if (!abort_iocb ||
3643 abort_iocb->iocb.ulpContext != abort_context ||
3644 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
3645 spin_unlock_irq(&phba->hbalock);
3646 else {
James Smart92d7f7b2007-06-17 19:56:38 -05003647 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04003648 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05003649 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04003650
James Smart0ff10d42008-01-11 01:52:36 -05003651 /* Firmware could still be in progress of DMAing
3652 * payload, so don't free data buffer till after
3653 * a hbeat.
3654 */
3655 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
3656
James Smart92d7f7b2007-06-17 19:56:38 -05003657 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3658 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3659 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
3660 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04003661 }
3662 }
3663
James Bottomley604a3e32005-10-29 10:28:33 -05003664 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05003665 return;
3666}
3667
James Smart92d7f7b2007-06-17 19:56:38 -05003668static void
3669lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3670 struct lpfc_iocbq *rspiocb)
3671{
3672 IOCB_t *irsp = &rspiocb->iocb;
3673
3674 /* ELS cmd tag <ulpIoTag> completes */
3675 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smarte8b62012007-08-02 11:10:09 -04003676 "0133 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003677 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003678 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05003679 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05003680 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
3681 lpfc_ct_free_iocb(phba, cmdiocb);
3682 else
3683 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05003684 return;
3685}
3686
dea31012005-04-17 16:05:31 -05003687int
James Smart2e0fef82007-06-17 19:56:36 -05003688lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3689 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05003690{
James Smart2e0fef82007-06-17 19:56:36 -05003691 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003692 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05003693 IOCB_t *icmd = NULL;
3694 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04003695 int retval = IOCB_ERROR;
3696
James Smart92d7f7b2007-06-17 19:56:38 -05003697 /*
3698 * There are certain command types we don't want to abort. And we
3699 * don't want to abort commands that are already in the process of
3700 * being aborted.
James Smart07951072007-04-25 09:51:38 -04003701 */
3702 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05003703 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05003704 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3705 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04003706 return 0;
3707
James Smart858c9f62007-06-17 19:56:39 -05003708 /* If we're unloading, don't abort iocb on the ELS ring, but change the
3709 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04003710 */
James Smart858c9f62007-06-17 19:56:39 -05003711 if ((vport->load_flag & FC_UNLOADING) &&
3712 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003713 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
3714 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
3715 else
3716 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04003717 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05003718 }
dea31012005-04-17 16:05:31 -05003719
3720 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05003721 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003722 if (abtsiocbp == NULL)
3723 return 0;
dea31012005-04-17 16:05:31 -05003724
James Smart07951072007-04-25 09:51:38 -04003725 /* This signals the response to set the correct status
3726 * before calling the completion handler.
3727 */
3728 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
3729
dea31012005-04-17 16:05:31 -05003730 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04003731 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
3732 iabt->un.acxri.abortContextTag = icmd->ulpContext;
3733 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003734 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04003735 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05003736
James Smart2e0fef82007-06-17 19:56:36 -05003737 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04003738 iabt->ulpCommand = CMD_ABORT_XRI_CN;
3739 else
3740 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
3741
3742 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04003743
James Smarte8b62012007-08-02 11:10:09 -04003744 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3745 "0339 Abort xri x%x, original iotag x%x, "
3746 "abort cmd iotag x%x\n",
3747 iabt->un.acxri.abortContextTag,
3748 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05003749 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04003750
3751abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05003752 /*
3753 * Caller to this routine should check for IOCB_ERROR
3754 * and handle it properly. This routine no longer removes
3755 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04003756 */
James Smart2e0fef82007-06-17 19:56:36 -05003757 return retval;
dea31012005-04-17 16:05:31 -05003758}
3759
3760static int
James Smart51ef4c22007-08-02 11:10:31 -04003761lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
3762 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003763 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05003764{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003765 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05003766 int rc = 1;
3767
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003768 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
3769 return rc;
3770
James Smart51ef4c22007-08-02 11:10:31 -04003771 if (iocbq->vport != vport)
3772 return rc;
3773
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003774 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003775
James Smart495a7142008-06-14 22:52:59 -04003776 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05003777 return rc;
3778
3779 switch (ctx_cmd) {
3780 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04003781 if ((lpfc_cmd->rdata->pnode) &&
3782 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
3783 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05003784 rc = 0;
3785 break;
3786 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04003787 if ((lpfc_cmd->rdata->pnode) &&
3788 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05003789 rc = 0;
3790 break;
dea31012005-04-17 16:05:31 -05003791 case LPFC_CTX_HOST:
3792 rc = 0;
3793 break;
3794 default:
3795 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
3796 __FUNCTION__, ctx_cmd);
3797 break;
3798 }
3799
3800 return rc;
3801}
3802
3803int
James Smart51ef4c22007-08-02 11:10:31 -04003804lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
3805 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05003806{
James Smart51ef4c22007-08-02 11:10:31 -04003807 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003808 struct lpfc_iocbq *iocbq;
3809 int sum, i;
dea31012005-04-17 16:05:31 -05003810
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003811 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
3812 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05003813
James Smart51ef4c22007-08-02 11:10:31 -04003814 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
3815 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003816 sum++;
dea31012005-04-17 16:05:31 -05003817 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003818
dea31012005-04-17 16:05:31 -05003819 return sum;
3820}
3821
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003822void
James Smart2e0fef82007-06-17 19:56:36 -05003823lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3824 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003825{
James Bottomley604a3e32005-10-29 10:28:33 -05003826 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003827 return;
3828}
3829
dea31012005-04-17 16:05:31 -05003830int
James Smart51ef4c22007-08-02 11:10:31 -04003831lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
3832 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05003833{
James Smart51ef4c22007-08-02 11:10:31 -04003834 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003835 struct lpfc_iocbq *iocbq;
3836 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05003837 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05003838 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003839 int i;
dea31012005-04-17 16:05:31 -05003840
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003841 for (i = 1; i <= phba->sli.last_iotag; i++) {
3842 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05003843
James Smart51ef4c22007-08-02 11:10:31 -04003844 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05003845 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05003846 continue;
3847
3848 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003849 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003850 if (abtsiocb == NULL) {
3851 errcnt++;
3852 continue;
3853 }
dea31012005-04-17 16:05:31 -05003854
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003855 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05003856 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
3857 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
3858 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
3859 abtsiocb->iocb.ulpLe = 1;
3860 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05003861 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05003862
James Smart2e0fef82007-06-17 19:56:36 -05003863 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003864 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
3865 else
3866 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
3867
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003868 /* Setup callback routine and issue the command. */
3869 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05003870 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
3871 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05003872 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05003873 errcnt++;
3874 continue;
3875 }
3876 }
3877
3878 return errcnt;
3879}
3880
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003881static void
3882lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
3883 struct lpfc_iocbq *cmdiocbq,
3884 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05003885{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003886 wait_queue_head_t *pdone_q;
3887 unsigned long iflags;
dea31012005-04-17 16:05:31 -05003888
James Smart2e0fef82007-06-17 19:56:36 -05003889 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003890 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
3891 if (cmdiocbq->context2 && rspiocbq)
3892 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
3893 &rspiocbq->iocb, sizeof(IOCB_t));
3894
3895 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003896 if (pdone_q)
3897 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05003898 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05003899 return;
3900}
3901
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003902/*
3903 * Issue the caller's iocb and wait for its completion, but no longer than the
3904 * caller's timeout. Note that iocb_flags is cleared before the
3905 * lpfc_sli_issue_call since the wake routine sets a unique value and by
3906 * definition this is a wait function.
3907 */
James Smart92d7f7b2007-06-17 19:56:38 -05003908
dea31012005-04-17 16:05:31 -05003909int
James Smart2e0fef82007-06-17 19:56:36 -05003910lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
3911 struct lpfc_sli_ring *pring,
3912 struct lpfc_iocbq *piocb,
3913 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003914 uint32_t timeout)
dea31012005-04-17 16:05:31 -05003915{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08003916 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003917 long timeleft, timeout_req = 0;
3918 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003919 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05003920
3921 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003922 * If the caller has provided a response iocbq buffer, then context2
3923 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05003924 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003925 if (prspiocbq) {
3926 if (piocb->context2)
3927 return IOCB_ERROR;
3928 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05003929 }
3930
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003931 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
3932 piocb->context_un.wait_queue = &done_q;
3933 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05003934
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003935 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3936 creg_val = readl(phba->HCregaddr);
3937 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
3938 writel(creg_val, phba->HCregaddr);
3939 readl(phba->HCregaddr); /* flush */
3940 }
3941
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003942 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
3943 if (retval == IOCB_SUCCESS) {
3944 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003945 timeleft = wait_event_timeout(done_q,
3946 piocb->iocb_flag & LPFC_IO_WAKE,
3947 timeout_req);
dea31012005-04-17 16:05:31 -05003948
James Smart7054a602007-04-25 09:52:34 -04003949 if (piocb->iocb_flag & LPFC_IO_WAKE) {
3950 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003951 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04003952 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003954 "0338 IOCB wait timeout error - no "
3955 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003956 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04003957 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003958 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003959 "0330 IOCB wake NOT set, "
3960 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003961 timeout, (timeleft / jiffies));
3962 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05003963 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003964 } else {
3965 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003966 ":0332 IOCB wait issue failed, Data x%x\n",
3967 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003968 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05003969 }
3970
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003971 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3972 creg_val = readl(phba->HCregaddr);
3973 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
3974 writel(creg_val, phba->HCregaddr);
3975 readl(phba->HCregaddr); /* flush */
3976 }
3977
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003978 if (prspiocbq)
3979 piocb->context2 = NULL;
3980
3981 piocb->context_un.wait_queue = NULL;
3982 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05003983 return retval;
3984}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003985
dea31012005-04-17 16:05:31 -05003986int
James Smart2e0fef82007-06-17 19:56:36 -05003987lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05003988 uint32_t timeout)
3989{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08003990 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05003991 int retval;
James Smart858c9f62007-06-17 19:56:39 -05003992 unsigned long flag;
dea31012005-04-17 16:05:31 -05003993
3994 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04003995 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05003996 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003997
James Smart495a7142008-06-14 22:52:59 -04003998 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05003999 /* setup wake call as IOCB callback */
4000 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
4001 /* setup context field to pass wait_queue pointer to wake function */
4002 pmboxq->context1 = &done_q;
4003
dea31012005-04-17 16:05:31 -05004004 /* now issue the command */
4005 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4006
4007 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04004008 wait_event_interruptible_timeout(done_q,
4009 pmboxq->mbox_flag & LPFC_MBX_WAKE,
4010 timeout * HZ);
4011
James Smart858c9f62007-06-17 19:56:39 -05004012 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05004013 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04004014 /*
4015 * if LPFC_MBX_WAKE flag is set the mailbox is completed
4016 * else do not free the resources.
4017 */
4018 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05004019 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05004020 else {
James Smart7054a602007-04-25 09:52:34 -04004021 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05004022 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4023 }
4024 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05004025 }
4026
dea31012005-04-17 16:05:31 -05004027 return retval;
4028}
4029
James Smartb4c02652006-07-06 15:50:43 -04004030int
4031lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
4032{
James Smart2e0fef82007-06-17 19:56:36 -05004033 struct lpfc_vport *vport = phba->pport;
James Smartb4c02652006-07-06 15:50:43 -04004034 int i = 0;
James Smarted957682007-06-17 19:56:37 -05004035 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04004036
James Smart2e0fef82007-06-17 19:56:36 -05004037 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
James Smartb4c02652006-07-06 15:50:43 -04004038 if (i++ > LPFC_MBOX_TMO * 1000)
4039 return 1;
4040
James Smarted957682007-06-17 19:56:37 -05004041 /*
4042 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
4043 * did finish. This way we won't get the misleading
4044 * "Stray Mailbox Interrupt" message.
4045 */
4046 spin_lock_irq(&phba->hbalock);
4047 ha_copy = phba->work_ha;
4048 phba->work_ha &= ~HA_MBATT;
4049 spin_unlock_irq(&phba->hbalock);
4050
4051 if (ha_copy & HA_MBATT)
4052 if (lpfc_sli_handle_mb_event(phba) == 0)
4053 i = 0;
James Smartb4c02652006-07-06 15:50:43 -04004054
4055 msleep(1);
4056 }
4057
4058 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
4059}
4060
dea31012005-04-17 16:05:31 -05004061irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01004062lpfc_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05004063{
James Smart2e0fef82007-06-17 19:56:36 -05004064 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05004065 uint32_t ha_copy;
4066 uint32_t work_ha_copy;
4067 unsigned long status;
dea31012005-04-17 16:05:31 -05004068 uint32_t control;
4069
James Smart92d7f7b2007-06-17 19:56:38 -05004070 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05004071 struct lpfc_vport *vport;
4072 struct lpfc_nodelist *ndlp;
4073 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05004074 LPFC_MBOXQ_t *pmb;
4075 int rc;
4076
dea31012005-04-17 16:05:31 -05004077 /*
4078 * Get the driver's phba structure from the dev_id and
4079 * assume the HBA is not interrupting.
4080 */
4081 phba = (struct lpfc_hba *) dev_id;
4082
4083 if (unlikely(!phba))
4084 return IRQ_NONE;
4085
Linas Vepstas8d63f372007-02-14 14:28:36 -06004086 /* If the pci channel is offline, ignore all the interrupts. */
4087 if (unlikely(pci_channel_offline(phba->pcidev)))
4088 return IRQ_NONE;
4089
dea31012005-04-17 16:05:31 -05004090 phba->sli.slistat.sli_intr++;
4091
4092 /*
4093 * Call the HBA to see if it is interrupting. If not, don't claim
4094 * the interrupt
4095 */
4096
4097 /* Ignore all interrupts during initialization. */
James Smart2e0fef82007-06-17 19:56:36 -05004098 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05004099 return IRQ_NONE;
4100
4101 /*
4102 * Read host attention register to determine interrupt source
4103 * Clear Attention Sources, except Error Attention (to
4104 * preserve status) and Link Attention
4105 */
James Smart2e0fef82007-06-17 19:56:36 -05004106 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004107 ha_copy = readl(phba->HAregaddr);
James Smartebdbe652007-04-25 09:53:15 -04004108 /* If somebody is waiting to handle an eratt don't process it
4109 * here. The brdkill function will do this.
4110 */
James Smart2e0fef82007-06-17 19:56:36 -05004111 if (phba->link_flag & LS_IGNORE_ERATT)
James Smartebdbe652007-04-25 09:53:15 -04004112 ha_copy &= ~HA_ERATT;
dea31012005-04-17 16:05:31 -05004113 writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
4114 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004115 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004116
4117 if (unlikely(!ha_copy))
4118 return IRQ_NONE;
4119
4120 work_ha_copy = ha_copy & phba->work_ha_mask;
4121
4122 if (unlikely(work_ha_copy)) {
4123 if (work_ha_copy & HA_LATT) {
4124 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
4125 /*
4126 * Turn off Link Attention interrupts
4127 * until CLEAR_LA done
4128 */
James Smart2e0fef82007-06-17 19:56:36 -05004129 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004130 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
4131 control = readl(phba->HCregaddr);
4132 control &= ~HC_LAINT_ENA;
4133 writel(control, phba->HCregaddr);
4134 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004135 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004136 }
4137 else
4138 work_ha_copy &= ~HA_LATT;
4139 }
4140
4141 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05004142 /*
4143 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
4144 * the only slow ring.
4145 */
4146 status = (work_ha_copy &
4147 (HA_RXMASK << (4*LPFC_ELS_RING)));
4148 status >>= (4*LPFC_ELS_RING);
4149 if (status & HA_RXMASK) {
4150 spin_lock(&phba->hbalock);
4151 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04004152
4153 lpfc_debugfs_slow_ring_trc(phba,
4154 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
4155 control, status,
4156 (uint32_t)phba->sli.slistat.sli_intr);
4157
James Smart858c9f62007-06-17 19:56:39 -05004158 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04004159 lpfc_debugfs_slow_ring_trc(phba,
4160 "ISR Disable ring:"
4161 "pwork:x%x hawork:x%x wait:x%x",
4162 phba->work_ha, work_ha_copy,
4163 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04004164 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04004165
James Smart858c9f62007-06-17 19:56:39 -05004166 control &=
4167 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05004168 writel(control, phba->HCregaddr);
4169 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05004170 }
James Smarta58cbd52007-08-02 11:09:43 -04004171 else {
4172 lpfc_debugfs_slow_ring_trc(phba,
4173 "ISR slow ring: pwork:"
4174 "x%x hawork:x%x wait:x%x",
4175 phba->work_ha, work_ha_copy,
4176 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04004177 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04004178 }
James Smart858c9f62007-06-17 19:56:39 -05004179 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004180 }
4181 }
4182
4183 if (work_ha_copy & HA_ERATT) {
dea31012005-04-17 16:05:31 -05004184 /*
4185 * There was a link/board error. Read the
4186 * status register to retrieve the error event
4187 * and process it.
4188 */
4189 phba->sli.slistat.err_attn_event++;
4190 /* Save status info */
4191 phba->work_hs = readl(phba->HSregaddr);
4192 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
4193 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
4194
4195 /* Clear Chip error bit */
4196 writel(HA_ERATT, phba->HAregaddr);
4197 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004198 phba->pport->stopped = 1;
dea31012005-04-17 16:05:31 -05004199 }
4200
James Smart97eab632008-04-07 10:16:05 -04004201 spin_lock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004202 if ((work_ha_copy & HA_MBATT) &&
4203 (phba->sli.mbox_active)) {
4204 pmb = phba->sli.mbox_active;
4205 pmbox = &pmb->mb;
4206 mbox = &phba->slim2p->mbx;
James Smart858c9f62007-06-17 19:56:39 -05004207 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004208
4209 /* First check out the status word */
4210 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
4211 if (pmbox->mbxOwner != OWN_HOST) {
James Smart97eab632008-04-07 10:16:05 -04004212 spin_unlock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004213 /*
4214 * Stray Mailbox Interrupt, mbxCommand <cmd>
4215 * mbxStatus <status>
4216 */
James Smart09372822008-01-11 01:52:54 -05004217 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05004218 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004219 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05004220 "Interrupt mbxCommand x%x "
4221 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004222 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05004223 pmbox->mbxCommand,
4224 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05004225 /* clear mailbox attention bit */
4226 work_ha_copy &= ~HA_MBATT;
4227 } else {
James Smart97eab632008-04-07 10:16:05 -04004228 phba->sli.mbox_active = NULL;
4229 spin_unlock(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05004230 phba->last_completion_time = jiffies;
4231 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05004232 if (pmb->mbox_cmpl) {
4233 lpfc_sli_pcimem_bcopy(mbox, pmbox,
4234 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05004235 }
James Smart09372822008-01-11 01:52:54 -05004236 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
4237 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
4238
4239 lpfc_debugfs_disc_trc(vport,
4240 LPFC_DISC_TRC_MBOX_VPORT,
4241 "MBOX dflt rpi: : "
4242 "status:x%x rpi:x%x",
4243 (uint32_t)pmbox->mbxStatus,
4244 pmbox->un.varWords[0], 0);
4245
4246 if (!pmbox->mbxStatus) {
4247 mp = (struct lpfc_dmabuf *)
4248 (pmb->context1);
4249 ndlp = (struct lpfc_nodelist *)
4250 pmb->context2;
4251
4252 /* Reg_LOGIN of dflt RPI was
4253 * successful. new lets get
4254 * rid of the RPI using the
4255 * same mbox buffer.
4256 */
4257 lpfc_unreg_login(phba,
4258 vport->vpi,
4259 pmbox->un.varWords[0],
4260 pmb);
4261 pmb->mbox_cmpl =
4262 lpfc_mbx_cmpl_dflt_rpi;
4263 pmb->context1 = mp;
4264 pmb->context2 = ndlp;
4265 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04004266 rc = lpfc_sli_issue_mbox(phba,
4267 pmb,
4268 MBX_NOWAIT);
4269 if (rc != MBX_BUSY)
4270 lpfc_printf_log(phba,
4271 KERN_ERR,
4272 LOG_MBOX | LOG_SLI,
4273 "0306 rc should have"
4274 "been MBX_BUSY");
James Smart09372822008-01-11 01:52:54 -05004275 goto send_current_mbox;
4276 }
4277 }
4278 spin_lock(&phba->pport->work_port_lock);
4279 phba->pport->work_port_events &=
4280 ~WORKER_MBOX_TMO;
4281 spin_unlock(&phba->pport->work_port_lock);
4282 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05004283 }
James Smart97eab632008-04-07 10:16:05 -04004284 } else
4285 spin_unlock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004286 if ((work_ha_copy & HA_MBATT) &&
4287 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05004288send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05004289 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04004290 do {
4291 rc = lpfc_sli_issue_mbox(phba, NULL,
4292 MBX_NOWAIT);
4293 } while (rc == MBX_NOT_FINISHED);
4294 if (rc != MBX_SUCCESS)
4295 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
4296 LOG_SLI, "0349 rc should be "
4297 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05004298 }
4299
James Smart2e0fef82007-06-17 19:56:36 -05004300 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004301 phba->work_ha |= work_ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05004302 spin_unlock(&phba->hbalock);
James Smart5e9d9b82008-06-14 22:52:53 -04004303 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004304 }
4305
4306 ha_copy &= ~(phba->work_ha_mask);
4307
4308 /*
4309 * Process all events on FCP ring. Take the optimized path for
4310 * FCP IO. Any other IO is slow path and is handled by
4311 * the worker thread.
4312 */
4313 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
4314 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05004315 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05004316 lpfc_sli_handle_fast_ring_event(phba,
4317 &phba->sli.ring[LPFC_FCP_RING],
4318 status);
James Smarta4bc3372006-12-02 13:34:16 -05004319
4320 if (phba->cfg_multi_ring_support == 2) {
4321 /*
4322 * Process all events on extra ring. Take the optimized path
4323 * for extra ring IO. Any other IO is slow path and is handled
4324 * by the worker thread.
4325 */
4326 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
4327 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05004328 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05004329 lpfc_sli_handle_fast_ring_event(phba,
4330 &phba->sli.ring[LPFC_EXTRA_RING],
4331 status);
4332 }
4333 }
dea31012005-04-17 16:05:31 -05004334 return IRQ_HANDLED;
4335
4336} /* lpfc_intr_handler */