blob: ce348c5c706ce901f05cc72349804c63cc2b9935 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart9413aff2007-04-25 09:53:35 -04004 * Copyright (C) 2004-2007 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040031#include <scsi/scsi_transport_fc.h>
dea31012005-04-17 16:05:31 -050032
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
35#include "lpfc_disc.h"
36#include "lpfc_scsi.h"
37#include "lpfc.h"
38#include "lpfc_crtn.h"
39#include "lpfc_logmsg.h"
40#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050041#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050042
43/*
44 * Define macro to log: Mailbox command x%x cannot issue Data
45 * This allows multiple uses of lpfc_msgBlk0311
46 * w/o perturbing log msg utility.
47 */
James Smart92d7f7b2007-06-17 19:56:38 -050048#define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
dea31012005-04-17 16:05:31 -050049 lpfc_printf_log(phba, \
50 KERN_INFO, \
51 LOG_MBOX | LOG_SLI, \
James Smarte8b62012007-08-02 11:10:09 -040052 "(%d):0311 Mailbox command x%x cannot " \
James Smart92d7f7b2007-06-17 19:56:38 -050053 "issue Data: x%x x%x x%x\n", \
James Smart92d7f7b2007-06-17 19:56:38 -050054 pmbox->vport ? pmbox->vport->vpi : 0, \
55 pmbox->mb.mbxCommand, \
James Smart2e0fef82007-06-17 19:56:36 -050056 phba->pport->port_state, \
dea31012005-04-17 16:05:31 -050057 psli->sli_flag, \
James Smart2e0fef82007-06-17 19:56:36 -050058 flag)
dea31012005-04-17 16:05:31 -050059
60
61/* There are only four IOCB completion types. */
62typedef enum _lpfc_iocb_type {
63 LPFC_UNKNOWN_IOCB,
64 LPFC_UNSOL_IOCB,
65 LPFC_SOL_IOCB,
66 LPFC_ABORT_IOCB
67} lpfc_iocb_type;
68
James Smart92d7f7b2007-06-17 19:56:38 -050069 /* SLI-2/SLI-3 provide different sized iocbs. Given a pointer
70 * to the start of the ring, and the slot number of the
71 * desired iocb entry, calc a pointer to that entry.
72 */
James Smarted957682007-06-17 19:56:37 -050073static inline IOCB_t *
74lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
75{
76 return (IOCB_t *) (((char *) pring->cmdringaddr) +
77 pring->cmdidx * phba->iocb_cmd_size);
78}
79
80static inline IOCB_t *
81lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
82{
83 return (IOCB_t *) (((char *) pring->rspringaddr) +
84 pring->rspidx * phba->iocb_rsp_size);
85}
86
James Smart2e0fef82007-06-17 19:56:36 -050087static struct lpfc_iocbq *
88__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040089{
90 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
91 struct lpfc_iocbq * iocbq = NULL;
92
93 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
94 return iocbq;
95}
96
James Smart2e0fef82007-06-17 19:56:36 -050097struct lpfc_iocbq *
98lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -050099{
James Smart2e0fef82007-06-17 19:56:36 -0500100 struct lpfc_iocbq * iocbq = NULL;
101 unsigned long iflags;
102
103 spin_lock_irqsave(&phba->hbalock, iflags);
104 iocbq = __lpfc_sli_get_iocbq(phba);
105 spin_unlock_irqrestore(&phba->hbalock, iflags);
106 return iocbq;
107}
108
109void
110__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
111{
112 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500113
114 /*
115 * Clean all volatile data fields, preserve iotag and node struct.
116 */
117 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
118 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
119}
120
James Smart2e0fef82007-06-17 19:56:36 -0500121void
122lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
123{
124 unsigned long iflags;
125
126 /*
127 * Clean all volatile data fields, preserve iotag and node struct.
128 */
129 spin_lock_irqsave(&phba->hbalock, iflags);
130 __lpfc_sli_release_iocbq(phba, iocbq);
131 spin_unlock_irqrestore(&phba->hbalock, iflags);
132}
133
dea31012005-04-17 16:05:31 -0500134/*
135 * Translate the iocb command to an iocb command type used to decide the final
136 * disposition of each completed IOCB.
137 */
138static lpfc_iocb_type
139lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
140{
141 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
142
143 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
144 return 0;
145
146 switch (iocb_cmnd) {
147 case CMD_XMIT_SEQUENCE_CR:
148 case CMD_XMIT_SEQUENCE_CX:
149 case CMD_XMIT_BCAST_CN:
150 case CMD_XMIT_BCAST_CX:
151 case CMD_ELS_REQUEST_CR:
152 case CMD_ELS_REQUEST_CX:
153 case CMD_CREATE_XRI_CR:
154 case CMD_CREATE_XRI_CX:
155 case CMD_GET_RPI_CN:
156 case CMD_XMIT_ELS_RSP_CX:
157 case CMD_GET_RPI_CR:
158 case CMD_FCP_IWRITE_CR:
159 case CMD_FCP_IWRITE_CX:
160 case CMD_FCP_IREAD_CR:
161 case CMD_FCP_IREAD_CX:
162 case CMD_FCP_ICMND_CR:
163 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500164 case CMD_FCP_TSEND_CX:
165 case CMD_FCP_TRSP_CX:
166 case CMD_FCP_TRECEIVE_CX:
167 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500168 case CMD_ADAPTER_MSG:
169 case CMD_ADAPTER_DUMP:
170 case CMD_XMIT_SEQUENCE64_CR:
171 case CMD_XMIT_SEQUENCE64_CX:
172 case CMD_XMIT_BCAST64_CN:
173 case CMD_XMIT_BCAST64_CX:
174 case CMD_ELS_REQUEST64_CR:
175 case CMD_ELS_REQUEST64_CX:
176 case CMD_FCP_IWRITE64_CR:
177 case CMD_FCP_IWRITE64_CX:
178 case CMD_FCP_IREAD64_CR:
179 case CMD_FCP_IREAD64_CX:
180 case CMD_FCP_ICMND64_CR:
181 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500182 case CMD_FCP_TSEND64_CX:
183 case CMD_FCP_TRSP64_CX:
184 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500185 case CMD_GEN_REQUEST64_CR:
186 case CMD_GEN_REQUEST64_CX:
187 case CMD_XMIT_ELS_RSP64_CX:
188 type = LPFC_SOL_IOCB;
189 break;
190 case CMD_ABORT_XRI_CN:
191 case CMD_ABORT_XRI_CX:
192 case CMD_CLOSE_XRI_CN:
193 case CMD_CLOSE_XRI_CX:
194 case CMD_XRI_ABORTED_CX:
195 case CMD_ABORT_MXRI64_CN:
196 type = LPFC_ABORT_IOCB;
197 break;
198 case CMD_RCV_SEQUENCE_CX:
199 case CMD_RCV_ELS_REQ_CX:
200 case CMD_RCV_SEQUENCE64_CX:
201 case CMD_RCV_ELS_REQ64_CX:
James Smarted957682007-06-17 19:56:37 -0500202 case CMD_IOCB_RCV_SEQ64_CX:
203 case CMD_IOCB_RCV_ELS64_CX:
204 case CMD_IOCB_RCV_CONT64_CX:
dea31012005-04-17 16:05:31 -0500205 type = LPFC_UNSOL_IOCB;
206 break;
207 default:
208 type = LPFC_UNKNOWN_IOCB;
209 break;
210 }
211
212 return type;
213}
214
215static int
James Smarted957682007-06-17 19:56:37 -0500216lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500217{
218 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500219 LPFC_MBOXQ_t *pmb;
220 MAILBOX_t *pmbox;
221 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500222
James Smarted957682007-06-17 19:56:37 -0500223 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
224 if (!pmb)
225 return -ENOMEM;
226 pmbox = &pmb->mb;
227 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500228 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500229 lpfc_config_ring(phba, i, pmb);
230 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
231 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400233 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500234 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
235 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400236 rc, pmbox->mbxCommand,
237 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500238 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500239 ret = -ENXIO;
240 break;
dea31012005-04-17 16:05:31 -0500241 }
242 }
James Smarted957682007-06-17 19:56:37 -0500243 mempool_free(pmb, phba->mbox_mem_pool);
244 return ret;
dea31012005-04-17 16:05:31 -0500245}
246
247static int
James Smart2e0fef82007-06-17 19:56:36 -0500248lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
249 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500250{
dea31012005-04-17 16:05:31 -0500251 list_add_tail(&piocb->list, &pring->txcmplq);
252 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500253 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
254 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
255 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
256 if (!piocb->vport)
257 BUG();
258 else
259 mod_timer(&piocb->vport->els_tmofunc,
260 jiffies + HZ * (phba->fc_ratov << 1));
261 }
262
dea31012005-04-17 16:05:31 -0500263
James Smart2e0fef82007-06-17 19:56:36 -0500264 return 0;
dea31012005-04-17 16:05:31 -0500265}
266
267static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500268lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500269{
dea31012005-04-17 16:05:31 -0500270 struct lpfc_iocbq *cmd_iocb;
271
James Smart858c9f62007-06-17 19:56:39 -0500272 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
273 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500274 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500275 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500276}
277
278static IOCB_t *
279lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
280{
James Smarted957682007-06-17 19:56:37 -0500281 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
282 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
283 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -0500284 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500285
286 if ((pring->next_cmdidx == pring->cmdidx) &&
287 (++pring->next_cmdidx >= max_cmd_idx))
288 pring->next_cmdidx = 0;
289
290 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
291
292 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
293
294 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
295 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400296 "0315 Ring %d issue: portCmdGet %d "
dea31012005-04-17 16:05:31 -0500297 "is bigger then cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400298 pring->ringno,
dea31012005-04-17 16:05:31 -0500299 pring->local_getidx, max_cmd_idx);
300
James Smart2e0fef82007-06-17 19:56:36 -0500301 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500302 /*
303 * All error attention handlers are posted to
304 * worker thread
305 */
306 phba->work_ha |= HA_ERATT;
307 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500308
309 /* hbalock should already be held */
dea31012005-04-17 16:05:31 -0500310 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -0500311 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500312
313 return NULL;
314 }
315
316 if (pring->local_getidx == pring->next_cmdidx)
317 return NULL;
318 }
319
James Smarted957682007-06-17 19:56:37 -0500320 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500321}
322
James Bottomley604a3e32005-10-29 10:28:33 -0500323uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500324lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500325{
James Smart2e0fef82007-06-17 19:56:36 -0500326 struct lpfc_iocbq **new_arr;
327 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500328 size_t new_len;
329 struct lpfc_sli *psli = &phba->sli;
330 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500331
James Smart2e0fef82007-06-17 19:56:36 -0500332 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500333 iotag = psli->last_iotag;
334 if(++iotag < psli->iocbq_lookup_len) {
335 psli->last_iotag = iotag;
336 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500337 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500338 iocbq->iotag = iotag;
339 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500340 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500341 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
342 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500343 spin_unlock_irq(&phba->hbalock);
344 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500345 GFP_KERNEL);
346 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500347 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500348 old_arr = psli->iocbq_lookup;
349 if (new_len <= psli->iocbq_lookup_len) {
350 /* highly unprobable case */
351 kfree(new_arr);
352 iotag = psli->last_iotag;
353 if(++iotag < psli->iocbq_lookup_len) {
354 psli->last_iotag = iotag;
355 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500356 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500357 iocbq->iotag = iotag;
358 return iotag;
359 }
James Smart2e0fef82007-06-17 19:56:36 -0500360 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500361 return 0;
362 }
363 if (psli->iocbq_lookup)
364 memcpy(new_arr, old_arr,
365 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -0400366 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -0500367 psli->iocbq_lookup = new_arr;
368 psli->iocbq_lookup_len = new_len;
369 psli->last_iotag = iotag;
370 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500371 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500372 iocbq->iotag = iotag;
373 kfree(old_arr);
374 return iotag;
375 }
James Smart8f6d98d2006-08-01 07:34:00 -0400376 } else
James Smart2e0fef82007-06-17 19:56:36 -0500377 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500378
James Bottomley604a3e32005-10-29 10:28:33 -0500379 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400380 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
381 psli->last_iotag);
dea31012005-04-17 16:05:31 -0500382
James Bottomley604a3e32005-10-29 10:28:33 -0500383 return 0;
dea31012005-04-17 16:05:31 -0500384}
385
386static void
387lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
388 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
389{
390 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500391 * Set up an iotag
dea31012005-04-17 16:05:31 -0500392 */
James Bottomley604a3e32005-10-29 10:28:33 -0500393 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500394
James Smarta58cbd52007-08-02 11:09:43 -0400395 if (pring->ringno == LPFC_ELS_RING) {
396 lpfc_debugfs_slow_ring_trc(phba,
397 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
398 *(((uint32_t *) &nextiocb->iocb) + 4),
399 *(((uint32_t *) &nextiocb->iocb) + 6),
400 *(((uint32_t *) &nextiocb->iocb) + 7));
401 }
402
dea31012005-04-17 16:05:31 -0500403 /*
404 * Issue iocb command to adapter
405 */
James Smart92d7f7b2007-06-17 19:56:38 -0500406 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500407 wmb();
408 pring->stats.iocb_cmd++;
409
410 /*
411 * If there is no completion routine to call, we can release the
412 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
413 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
414 */
415 if (nextiocb->iocb_cmpl)
416 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500417 else
James Smart2e0fef82007-06-17 19:56:36 -0500418 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500419
420 /*
421 * Let the HBA know what IOCB slot will be the next one the
422 * driver will put a command into.
423 */
424 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500425 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500426}
427
428static void
James Smart2e0fef82007-06-17 19:56:36 -0500429lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500430{
431 int ringno = pring->ringno;
432
433 pring->flag |= LPFC_CALL_RING_AVAILABLE;
434
435 wmb();
436
437 /*
438 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
439 * The HBA will tell us when an IOCB entry is available.
440 */
441 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
442 readl(phba->CAregaddr); /* flush */
443
444 pring->stats.iocb_cmd_full++;
445}
446
447static void
James Smart2e0fef82007-06-17 19:56:36 -0500448lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500449{
450 int ringno = pring->ringno;
451
452 /*
453 * Tell the HBA that there is work to do in this ring.
454 */
455 wmb();
456 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
457 readl(phba->CAregaddr); /* flush */
458}
459
460static void
James Smart2e0fef82007-06-17 19:56:36 -0500461lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500462{
463 IOCB_t *iocb;
464 struct lpfc_iocbq *nextiocb;
465
466 /*
467 * Check to see if:
468 * (a) there is anything on the txq to send
469 * (b) link is up
470 * (c) link attention events can be processed (fcp ring only)
471 * (d) IOCB processing is not blocked by the outstanding mbox command.
472 */
473 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500474 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500475 (pring->ringno != phba->sli.fcp_ring ||
476 phba->sli.sli_flag & LPFC_PROCESS_LA) &&
477 !(pring->flag & LPFC_STOP_IOCB_MBX)) {
478
479 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
480 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
481 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
482
483 if (iocb)
484 lpfc_sli_update_ring(phba, pring);
485 else
486 lpfc_sli_update_full_ring(phba, pring);
487 }
488
489 return;
490}
491
492/* lpfc_sli_turn_on_ring is only called by lpfc_sli_handle_mb_event below */
493static void
James Smart2e0fef82007-06-17 19:56:36 -0500494lpfc_sli_turn_on_ring(struct lpfc_hba *phba, int ringno)
dea31012005-04-17 16:05:31 -0500495{
James Smarted957682007-06-17 19:56:37 -0500496 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
497 &phba->slim2p->mbx.us.s3_pgp.port[ringno] :
498 &phba->slim2p->mbx.us.s2.port[ringno];
James Smart2e0fef82007-06-17 19:56:36 -0500499 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500500
501 /* If the ring is active, flag it */
James Smart2e0fef82007-06-17 19:56:36 -0500502 spin_lock_irqsave(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500503 if (phba->sli.ring[ringno].cmdringaddr) {
504 if (phba->sli.ring[ringno].flag & LPFC_STOP_IOCB_MBX) {
505 phba->sli.ring[ringno].flag &= ~LPFC_STOP_IOCB_MBX;
506 /*
507 * Force update of the local copy of cmdGetInx
508 */
509 phba->sli.ring[ringno].local_getidx
510 = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -0500511 lpfc_sli_resume_iocb(phba, &phba->sli.ring[ringno]);
dea31012005-04-17 16:05:31 -0500512 }
513 }
James Smart2e0fef82007-06-17 19:56:36 -0500514 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500515}
516
James Smarted957682007-06-17 19:56:37 -0500517struct lpfc_hbq_entry *
518lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
519{
520 struct hbq_s *hbqp = &phba->hbqs[hbqno];
521
522 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
523 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
524 hbqp->next_hbqPutIdx = 0;
525
526 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500527 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500528 uint32_t getidx = le32_to_cpu(raw_index);
529
530 hbqp->local_hbqGetIdx = getidx;
531
532 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
533 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500534 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400535 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500536 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400537 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500538 hbqp->entry_count);
539
540 phba->link_state = LPFC_HBA_ERROR;
541 return NULL;
542 }
543
544 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
545 return NULL;
546 }
547
James Smart51ef4c22007-08-02 11:10:31 -0400548 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
549 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500550}
551
552void
553lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
554{
James Smart92d7f7b2007-06-17 19:56:38 -0500555 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
556 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400557 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -0500558
James Smart51ef4c22007-08-02 11:10:31 -0400559 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500560 /* Return all memory used by all HBQs */
James Smart51ef4c22007-08-02 11:10:31 -0400561 for (i = 0; i < hbq_count; ++i) {
562 list_for_each_entry_safe(dmabuf, next_dmabuf,
563 &phba->hbqs[i].hbq_buffer_list, list) {
564 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
565 list_del(&hbq_buf->dbuf.list);
566 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
567 }
James Smarted957682007-06-17 19:56:37 -0500568 }
James Smarted957682007-06-17 19:56:37 -0500569}
570
James Smart51ef4c22007-08-02 11:10:31 -0400571static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500572lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500573 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500574{
575 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500576 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500577
578 /* Get next HBQ entry slot to use */
579 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
580 if (hbqe) {
581 struct hbq_s *hbqp = &phba->hbqs[hbqno];
582
James Smart92d7f7b2007-06-17 19:56:38 -0500583 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
584 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400585 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500586 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500587 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
588 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
589 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500590 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
591 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500592 /* flush */
James Smarted957682007-06-17 19:56:37 -0500593 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400594 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smarted957682007-06-17 19:56:37 -0500595 }
James Smart51ef4c22007-08-02 11:10:31 -0400596 return hbqe;
James Smarted957682007-06-17 19:56:37 -0500597}
598
James Smart92d7f7b2007-06-17 19:56:38 -0500599static struct lpfc_hbq_init lpfc_els_hbq = {
600 .rn = 1,
601 .entry_count = 200,
602 .mask_count = 0,
603 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400604 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500605 .buffer_count = 0,
606 .init_count = 20,
607 .add_count = 5,
608};
James Smarted957682007-06-17 19:56:37 -0500609
James Smart51ef4c22007-08-02 11:10:31 -0400610static struct lpfc_hbq_init lpfc_extra_hbq = {
611 .rn = 1,
612 .entry_count = 200,
613 .mask_count = 0,
614 .profile = 0,
615 .ring_mask = (1 << LPFC_EXTRA_RING),
616 .buffer_count = 0,
617 .init_count = 0,
618 .add_count = 5,
619};
620
James Smart78b2d852007-08-02 11:10:21 -0400621struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500622 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400623 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500624};
625
James Smart311464e2007-08-02 11:10:37 -0400626static int
James Smart92d7f7b2007-06-17 19:56:38 -0500627lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
628{
629 uint32_t i, start, end;
630 struct hbq_dmabuf *hbq_buffer;
631
James Smart51ef4c22007-08-02 11:10:31 -0400632 if (!phba->hbqs[hbqno].hbq_alloc_buffer) {
633 return 0;
634 }
635
James Smart92d7f7b2007-06-17 19:56:38 -0500636 start = lpfc_hbq_defs[hbqno]->buffer_count;
637 end = count + lpfc_hbq_defs[hbqno]->buffer_count;
638 if (end > lpfc_hbq_defs[hbqno]->entry_count) {
639 end = lpfc_hbq_defs[hbqno]->entry_count;
James Smarted957682007-06-17 19:56:37 -0500640 }
James Smart92d7f7b2007-06-17 19:56:38 -0500641
642 /* Populate HBQ entries */
643 for (i = start; i < end; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400644 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500645 if (!hbq_buffer)
646 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500647 hbq_buffer->tag = (i | (hbqno << 16));
James Smart51ef4c22007-08-02 11:10:31 -0400648 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
649 lpfc_hbq_defs[hbqno]->buffer_count++;
650 else
651 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -0500652 }
653 return 0;
James Smarted957682007-06-17 19:56:37 -0500654}
655
656int
James Smart92d7f7b2007-06-17 19:56:38 -0500657lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500658{
James Smart92d7f7b2007-06-17 19:56:38 -0500659 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
660 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -0500661}
662
James Smart92d7f7b2007-06-17 19:56:38 -0500663int
664lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500665{
James Smart92d7f7b2007-06-17 19:56:38 -0500666 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
667 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -0500668}
669
670struct hbq_dmabuf *
671lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
672{
James Smart92d7f7b2007-06-17 19:56:38 -0500673 struct lpfc_dmabuf *d_buf;
674 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400675 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500676
James Smart51ef4c22007-08-02 11:10:31 -0400677 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +0200678 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -0400679 return NULL;
680
681 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -0500682 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -0400683 if (hbq_buf->tag == tag) {
James Smart92d7f7b2007-06-17 19:56:38 -0500684 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -0500685 }
686 }
James Smart92d7f7b2007-06-17 19:56:38 -0500687 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400688 "1803 Bad hbq tag. Data: x%x x%x\n",
689 tag, lpfc_hbq_defs[tag >> 16]->buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -0500690 return NULL;
James Smarted957682007-06-17 19:56:37 -0500691}
692
693void
James Smart51ef4c22007-08-02 11:10:31 -0400694lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -0500695{
696 uint32_t hbqno;
697
James Smart51ef4c22007-08-02 11:10:31 -0400698 if (hbq_buffer) {
699 hbqno = hbq_buffer->tag >> 16;
700 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
701 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
702 }
James Smarted957682007-06-17 19:56:37 -0500703 }
704}
705
dea31012005-04-17 16:05:31 -0500706static int
707lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
708{
709 uint8_t ret;
710
711 switch (mbxCommand) {
712 case MBX_LOAD_SM:
713 case MBX_READ_NV:
714 case MBX_WRITE_NV:
715 case MBX_RUN_BIU_DIAG:
716 case MBX_INIT_LINK:
717 case MBX_DOWN_LINK:
718 case MBX_CONFIG_LINK:
719 case MBX_CONFIG_RING:
720 case MBX_RESET_RING:
721 case MBX_READ_CONFIG:
722 case MBX_READ_RCONFIG:
723 case MBX_READ_SPARM:
724 case MBX_READ_STATUS:
725 case MBX_READ_RPI:
726 case MBX_READ_XRI:
727 case MBX_READ_REV:
728 case MBX_READ_LNK_STAT:
729 case MBX_REG_LOGIN:
730 case MBX_UNREG_LOGIN:
731 case MBX_READ_LA:
732 case MBX_CLEAR_LA:
733 case MBX_DUMP_MEMORY:
734 case MBX_DUMP_CONTEXT:
735 case MBX_RUN_DIAGS:
736 case MBX_RESTART:
737 case MBX_UPDATE_CFG:
738 case MBX_DOWN_LOAD:
739 case MBX_DEL_LD_ENTRY:
740 case MBX_RUN_PROGRAM:
741 case MBX_SET_MASK:
742 case MBX_SET_SLIM:
743 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500744 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -0500745 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500746 case MBX_BEACON:
dea31012005-04-17 16:05:31 -0500747 case MBX_LOAD_AREA:
748 case MBX_RUN_BIU_DIAG64:
749 case MBX_CONFIG_PORT:
750 case MBX_READ_SPARM64:
751 case MBX_READ_RPI64:
752 case MBX_REG_LOGIN64:
753 case MBX_READ_LA64:
754 case MBX_FLASH_WR_ULA:
755 case MBX_SET_DEBUG:
756 case MBX_LOAD_EXP_ROM:
James Smart92d7f7b2007-06-17 19:56:38 -0500757 case MBX_REG_VPI:
758 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -0500759 case MBX_HEARTBEAT:
dea31012005-04-17 16:05:31 -0500760 ret = mbxCommand;
761 break;
762 default:
763 ret = MBX_SHUTDOWN;
764 break;
765 }
James Smart2e0fef82007-06-17 19:56:36 -0500766 return ret;
dea31012005-04-17 16:05:31 -0500767}
768static void
James Smart2e0fef82007-06-17 19:56:36 -0500769lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -0500770{
771 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -0500772 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -0500773
774 /*
775 * If pdone_q is empty, the driver thread gave up waiting and
776 * continued running.
777 */
James Smart7054a602007-04-25 09:52:34 -0400778 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -0500779 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -0500780 pdone_q = (wait_queue_head_t *) pmboxq->context1;
781 if (pdone_q)
782 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -0500783 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -0500784 return;
785}
786
787void
James Smart2e0fef82007-06-17 19:56:36 -0500788lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500789{
790 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -0400791 uint16_t rpi;
792 int rc;
793
dea31012005-04-17 16:05:31 -0500794 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -0400795
dea31012005-04-17 16:05:31 -0500796 if (mp) {
797 lpfc_mbuf_free(phba, mp->virt, mp->phys);
798 kfree(mp);
799 }
James Smart7054a602007-04-25 09:52:34 -0400800
801 /*
802 * If a REG_LOGIN succeeded after node is destroyed or node
803 * is in re-discovery driver need to cleanup the RPI.
804 */
James Smart2e0fef82007-06-17 19:56:36 -0500805 if (!(phba->pport->load_flag & FC_UNLOADING) &&
806 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
807 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -0400808
809 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -0500810 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
811 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -0400812 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
813 if (rc != MBX_NOT_FINISHED)
814 return;
815 }
816
James Smart2e0fef82007-06-17 19:56:36 -0500817 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500818 return;
819}
820
821int
James Smart2e0fef82007-06-17 19:56:36 -0500822lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500823{
James Smart92d7f7b2007-06-17 19:56:38 -0500824 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -0500825 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -0500826 int rc;
827 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -0500828
829 phba->sli.slistat.mbox_event++;
830
James Smart92d7f7b2007-06-17 19:56:38 -0500831 /* Get all completed mailboxe buffers into the cmplq */
832 spin_lock_irq(&phba->hbalock);
833 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
834 spin_unlock_irq(&phba->hbalock);
835
dea31012005-04-17 16:05:31 -0500836 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -0500837 do {
838 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
839 if (pmb == NULL)
840 break;
841
dea31012005-04-17 16:05:31 -0500842 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -0500843
James Smart858c9f62007-06-17 19:56:39 -0500844 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
845 if (pmb->vport) {
846 lpfc_debugfs_disc_trc(pmb->vport,
847 LPFC_DISC_TRC_MBOX_VPORT,
848 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
849 (uint32_t)pmbox->mbxCommand,
850 pmbox->un.varWords[0],
851 pmbox->un.varWords[1]);
852 }
853 else {
854 lpfc_debugfs_disc_trc(phba->pport,
855 LPFC_DISC_TRC_MBOX,
856 "MBOX cmpl: cmd:x%x mb:x%x x%x",
857 (uint32_t)pmbox->mbxCommand,
858 pmbox->un.varWords[0],
859 pmbox->un.varWords[1]);
860 }
861 }
862
dea31012005-04-17 16:05:31 -0500863 /*
864 * It is a fatal error if unknown mbox command completion.
865 */
866 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
867 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -0500868 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -0500869 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400870 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -0500871 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500872 pmb->vport ? pmb->vport->vpi : 0,
873 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -0500874 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500875 phba->work_hs = HS_FFER3;
876 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500877 continue;
dea31012005-04-17 16:05:31 -0500878 }
879
dea31012005-04-17 16:05:31 -0500880 if (pmbox->mbxStatus) {
881 phba->sli.slistat.mbox_stat_err++;
882 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
883 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -0500884 lpfc_printf_log(phba, KERN_INFO,
885 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400886 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -0500887 "error - RETRYing Data: x%x "
888 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500889 pmb->vport ? pmb->vport->vpi :0,
890 pmbox->mbxCommand,
891 pmbox->mbxStatus,
892 pmbox->un.varWords[0],
893 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -0500894 pmbox->mbxStatus = 0;
895 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -0500896 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500897 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -0500898 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500899 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
900 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -0500901 continue;
dea31012005-04-17 16:05:31 -0500902 }
903 }
904
905 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -0500906 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400907 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -0500908 "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 -0500909 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -0500910 pmbox->mbxCommand,
911 pmb->mbox_cmpl,
912 *((uint32_t *) pmbox),
913 pmbox->un.varWords[0],
914 pmbox->un.varWords[1],
915 pmbox->un.varWords[2],
916 pmbox->un.varWords[3],
917 pmbox->un.varWords[4],
918 pmbox->un.varWords[5],
919 pmbox->un.varWords[6],
920 pmbox->un.varWords[7]);
921
James Smart92d7f7b2007-06-17 19:56:38 -0500922 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -0500923 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -0500924 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -0500925 return 0;
dea31012005-04-17 16:05:31 -0500926}
James Smart92d7f7b2007-06-17 19:56:38 -0500927
928static struct lpfc_dmabuf *
929lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
930{
931 struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
James Smart51ef4c22007-08-02 11:10:31 -0400932 uint32_t hbqno;
933 void *virt; /* virtual address ptr */
934 dma_addr_t phys; /* mapped address */
James Smart92d7f7b2007-06-17 19:56:38 -0500935
936 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
937 if (hbq_entry == NULL)
938 return NULL;
939 list_del(&hbq_entry->dbuf.list);
James Smart51ef4c22007-08-02 11:10:31 -0400940
941 hbqno = tag >> 16;
942 new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500943 if (new_hbq_entry == NULL)
944 return &hbq_entry->dbuf;
James Smart92d7f7b2007-06-17 19:56:38 -0500945 new_hbq_entry->tag = -1;
James Smart51ef4c22007-08-02 11:10:31 -0400946 phys = new_hbq_entry->dbuf.phys;
947 virt = new_hbq_entry->dbuf.virt;
948 new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
949 new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
950 hbq_entry->dbuf.phys = phys;
951 hbq_entry->dbuf.virt = virt;
James Smart92d7f7b2007-06-17 19:56:38 -0500952 lpfc_sli_free_hbq(phba, hbq_entry);
953 return &new_hbq_entry->dbuf;
954}
955
dea31012005-04-17 16:05:31 -0500956static int
957lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
958 struct lpfc_iocbq *saveq)
959{
960 IOCB_t * irsp;
961 WORD5 * w5p;
962 uint32_t Rctl, Type;
963 uint32_t match, i;
964
965 match = 0;
966 irsp = &(saveq->iocb);
967 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
James Smarted957682007-06-17 19:56:37 -0500968 || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)
969 || (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)
970 || (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX)) {
dea31012005-04-17 16:05:31 -0500971 Rctl = FC_ELS_REQ;
972 Type = FC_ELS_DATA;
973 } else {
974 w5p =
975 (WORD5 *) & (saveq->iocb.un.
976 ulpWord[5]);
977 Rctl = w5p->hcsw.Rctl;
978 Type = w5p->hcsw.Type;
979
980 /* Firmware Workaround */
981 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
James Smart92d7f7b2007-06-17 19:56:38 -0500982 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
983 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
dea31012005-04-17 16:05:31 -0500984 Rctl = FC_ELS_REQ;
985 Type = FC_ELS_DATA;
986 w5p->hcsw.Rctl = Rctl;
987 w5p->hcsw.Type = Type;
988 }
989 }
James Smart92d7f7b2007-06-17 19:56:38 -0500990
991 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
992 if (irsp->ulpBdeCount != 0)
993 saveq->context2 = lpfc_sli_replace_hbqbuff(phba,
994 irsp->un.ulpWord[3]);
995 if (irsp->ulpBdeCount == 2)
996 saveq->context3 = lpfc_sli_replace_hbqbuff(phba,
James Smart51ef4c22007-08-02 11:10:31 -0400997 irsp->unsli3.sli3Words[7]);
James Smart92d7f7b2007-06-17 19:56:38 -0500998 }
999
dea31012005-04-17 16:05:31 -05001000 /* unSolicited Responses */
1001 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001002 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1003 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1004 saveq);
dea31012005-04-17 16:05:31 -05001005 match = 1;
1006 } else {
1007 /* We must search, based on rctl / type
1008 for the right routine */
1009 for (i = 0; i < pring->num_mask;
1010 i++) {
1011 if ((pring->prt[i].rctl ==
1012 Rctl)
1013 && (pring->prt[i].
1014 type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001015 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1016 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1017 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -05001018 match = 1;
1019 break;
1020 }
1021 }
1022 }
1023 if (match == 0) {
1024 /* Unexpected Rctl / Type received */
1025 /* Ring <ringno> handler: unexpected
1026 Rctl <Rctl> Type <Type> received */
James Smart92d7f7b2007-06-17 19:56:38 -05001027 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001028 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001029 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001030 pring->ringno, Rctl, Type);
dea31012005-04-17 16:05:31 -05001031 }
James Smart92d7f7b2007-06-17 19:56:38 -05001032 return 1;
dea31012005-04-17 16:05:31 -05001033}
1034
1035static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001036lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1037 struct lpfc_sli_ring *pring,
1038 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001039{
dea31012005-04-17 16:05:31 -05001040 struct lpfc_iocbq *cmd_iocb = NULL;
1041 uint16_t iotag;
1042
James Bottomley604a3e32005-10-29 10:28:33 -05001043 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001044
James Bottomley604a3e32005-10-29 10:28:33 -05001045 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1046 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001047 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001048 pring->txcmplq_cnt--;
1049 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001050 }
1051
dea31012005-04-17 16:05:31 -05001052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001053 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001054 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001055 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001056 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001057 return NULL;
1058}
1059
1060static int
James Smart2e0fef82007-06-17 19:56:36 -05001061lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001062 struct lpfc_iocbq *saveq)
1063{
James Smart2e0fef82007-06-17 19:56:36 -05001064 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001065 int rc = 1;
1066 unsigned long iflag;
1067
1068 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001069 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001070 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001071 spin_unlock_irqrestore(&phba->hbalock, iflag);
1072
dea31012005-04-17 16:05:31 -05001073 if (cmdiocbp) {
1074 if (cmdiocbp->iocb_cmpl) {
1075 /*
1076 * Post all ELS completions to the worker thread.
1077 * All other are passed to the completion callback.
1078 */
1079 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001080 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1081 cmdiocbp->iocb_flag &=
1082 ~LPFC_DRIVER_ABORTED;
1083 saveq->iocb.ulpStatus =
1084 IOSTAT_LOCAL_REJECT;
1085 saveq->iocb.un.ulpWord[4] =
1086 IOERR_SLI_ABORTED;
1087 }
dea31012005-04-17 16:05:31 -05001088 }
James Smart2e0fef82007-06-17 19:56:36 -05001089 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001090 } else
1091 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001092 } else {
1093 /*
1094 * Unknown initiating command based on the response iotag.
1095 * This could be the case on the ELS ring because of
1096 * lpfc_els_abort().
1097 */
1098 if (pring->ringno != LPFC_ELS_RING) {
1099 /*
1100 * Ring <ringno> handler: unexpected completion IoTag
1101 * <IoTag>
1102 */
James Smarte8b62012007-08-02 11:10:09 -04001103 lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1104 "0322 Ring %d handler: "
1105 "unexpected completion IoTag x%x "
1106 "Data: x%x x%x x%x x%x\n",
1107 pring->ringno,
1108 saveq->iocb.ulpIoTag,
1109 saveq->iocb.ulpStatus,
1110 saveq->iocb.un.ulpWord[4],
1111 saveq->iocb.ulpCommand,
1112 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001113 }
1114 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001115
dea31012005-04-17 16:05:31 -05001116 return rc;
1117}
1118
James Smart2e0fef82007-06-17 19:56:36 -05001119static void
1120lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001121{
James Smarted957682007-06-17 19:56:37 -05001122 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1123 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1124 &phba->slim2p->mbx.us.s2.port[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001125 /*
1126 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1127 * rsp ring <portRspMax>
1128 */
1129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001130 "0312 Ring %d handler: portRspPut %d "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001131 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001132 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001133 pring->numRiocb);
1134
James Smart2e0fef82007-06-17 19:56:36 -05001135 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001136
1137 /*
1138 * All error attention handlers are posted to
1139 * worker thread
1140 */
1141 phba->work_ha |= HA_ERATT;
1142 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001143
1144 /* hbalock should already be held */
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001145 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05001146 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001147
1148 return;
1149}
1150
James Smart2e0fef82007-06-17 19:56:36 -05001151void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001152{
James Smart2e0fef82007-06-17 19:56:36 -05001153 struct lpfc_sli *psli = &phba->sli;
1154 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001155 IOCB_t *irsp = NULL;
1156 IOCB_t *entry = NULL;
1157 struct lpfc_iocbq *cmdiocbq = NULL;
1158 struct lpfc_iocbq rspiocbq;
1159 struct lpfc_pgp *pgp;
1160 uint32_t status;
1161 uint32_t portRspPut, portRspMax;
1162 int type;
1163 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001164 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001165 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001166
1167 pring->stats.iocb_event++;
1168
James Smarted957682007-06-17 19:56:37 -05001169 pgp = (phba->sli_rev == 3) ?
1170 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1171 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1172
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001173
1174 /*
1175 * The next available response entry should never exceed the maximum
1176 * entries. If it does, treat it as an adapter hardware error.
1177 */
1178 portRspMax = pring->numRiocb;
1179 portRspPut = le32_to_cpu(pgp->rspPutInx);
1180 if (unlikely(portRspPut >= portRspMax)) {
1181 lpfc_sli_rsp_pointers_error(phba, pring);
1182 return;
1183 }
1184
1185 rmb();
1186 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001187 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001188 if (++pring->rspidx >= portRspMax)
1189 pring->rspidx = 0;
1190
1191 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1192 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001193 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001194 irsp = &rspiocbq.iocb;
1195 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1196 pring->stats.iocb_rsp++;
1197 rsp_cmpl++;
1198
1199 if (unlikely(irsp->ulpStatus)) {
1200 /* Rsp ring <ringno> error: IOCB */
1201 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001202 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001203 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001204 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001205 irsp->un.ulpWord[0],
1206 irsp->un.ulpWord[1],
1207 irsp->un.ulpWord[2],
1208 irsp->un.ulpWord[3],
1209 irsp->un.ulpWord[4],
1210 irsp->un.ulpWord[5],
1211 *(((uint32_t *) irsp) + 6),
1212 *(((uint32_t *) irsp) + 7));
1213 }
1214
1215 switch (type) {
1216 case LPFC_ABORT_IOCB:
1217 case LPFC_SOL_IOCB:
1218 /*
1219 * Idle exchange closed via ABTS from port. No iocb
1220 * resources need to be recovered.
1221 */
1222 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001223 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001224 "0314 IOCB cmd 0x%x "
1225 "processed. Skipping "
1226 "completion",
James Smartdca94792006-08-01 07:34:08 -04001227 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001228 break;
1229 }
1230
James Smart2e0fef82007-06-17 19:56:36 -05001231 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001232 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1233 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001234 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001235 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1236 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1237 &rspiocbq);
1238 }
1239 break;
1240 default:
1241 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1242 char adaptermsg[LPFC_MAX_ADPTMSG];
1243 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1244 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1245 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001246 dev_warn(&((phba->pcidev)->dev),
1247 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001248 phba->brd_no, adaptermsg);
1249 } else {
1250 /* Unknown IOCB command */
1251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001252 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001253 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001254 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001255 irsp->ulpStatus,
1256 irsp->ulpIoTag,
1257 irsp->ulpContext);
1258 }
1259 break;
1260 }
1261
1262 /*
1263 * The response IOCB has been processed. Update the ring
1264 * pointer in SLIM. If the port response put pointer has not
1265 * been updated, sync the pgp->rspPutInx and fetch the new port
1266 * response put pointer.
1267 */
James Smarted957682007-06-17 19:56:37 -05001268 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001269
1270 if (pring->rspidx == portRspPut)
1271 portRspPut = le32_to_cpu(pgp->rspPutInx);
1272 }
1273
1274 ha_copy = readl(phba->HAregaddr);
1275 ha_copy >>= (LPFC_FCP_RING * 4);
1276
1277 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001278 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001279 pring->stats.iocb_rsp_full++;
1280 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1281 writel(status, phba->CAregaddr);
1282 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001283 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001284 }
1285 if ((ha_copy & HA_R0CE_RSP) &&
1286 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001287 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001288 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1289 pring->stats.iocb_cmd_empty++;
1290
1291 /* Force update of the local copy of cmdGetInx */
1292 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1293 lpfc_sli_resume_iocb(phba, pring);
1294
1295 if ((pring->lpfc_sli_cmd_available))
1296 (pring->lpfc_sli_cmd_available) (phba, pring);
1297
James Smart2e0fef82007-06-17 19:56:36 -05001298 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001299 }
1300
1301 return;
1302}
1303
dea31012005-04-17 16:05:31 -05001304/*
1305 * This routine presumes LPFC_FCP_RING handling and doesn't bother
1306 * to check it explicitly.
1307 */
1308static int
James Smart2e0fef82007-06-17 19:56:36 -05001309lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1310 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001311{
James Smarted957682007-06-17 19:56:37 -05001312 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1313 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1314 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001315 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001316 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001317 struct lpfc_iocbq *cmdiocbq = NULL;
1318 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001319 uint32_t status;
1320 uint32_t portRspPut, portRspMax;
1321 int rc = 1;
1322 lpfc_iocb_type type;
1323 unsigned long iflag;
1324 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05001325
James Smart2e0fef82007-06-17 19:56:36 -05001326 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001327 pring->stats.iocb_event++;
1328
dea31012005-04-17 16:05:31 -05001329 /*
1330 * The next available response entry should never exceed the maximum
1331 * entries. If it does, treat it as an adapter hardware error.
1332 */
1333 portRspMax = pring->numRiocb;
1334 portRspPut = le32_to_cpu(pgp->rspPutInx);
1335 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001336 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05001337 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001338 return 1;
1339 }
1340
1341 rmb();
1342 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001343 /*
1344 * Fetch an entry off the ring and copy it into a local data
1345 * structure. The copy involves a byte-swap since the
1346 * network byte order and pci byte orders are different.
1347 */
James Smarted957682007-06-17 19:56:37 -05001348 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05001349 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001350
1351 if (++pring->rspidx >= portRspMax)
1352 pring->rspidx = 0;
1353
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001354 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1355 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05001356 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05001357 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001358 irsp = &rspiocbq.iocb;
1359
dea31012005-04-17 16:05:31 -05001360 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1361 pring->stats.iocb_rsp++;
1362 rsp_cmpl++;
1363
1364 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001365 /*
1366 * If resource errors reported from HBA, reduce
1367 * queuedepths of the SCSI device.
1368 */
1369 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1370 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1371 spin_unlock_irqrestore(&phba->hbalock, iflag);
1372 lpfc_adjust_queue_depth(phba);
1373 spin_lock_irqsave(&phba->hbalock, iflag);
1374 }
1375
dea31012005-04-17 16:05:31 -05001376 /* Rsp ring <ringno> error: IOCB */
1377 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001378 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05001379 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001380 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05001381 irsp->un.ulpWord[0],
1382 irsp->un.ulpWord[1],
1383 irsp->un.ulpWord[2],
1384 irsp->un.ulpWord[3],
1385 irsp->un.ulpWord[4],
1386 irsp->un.ulpWord[5],
1387 *(((uint32_t *) irsp) + 6),
1388 *(((uint32_t *) irsp) + 7));
dea31012005-04-17 16:05:31 -05001389 }
1390
1391 switch (type) {
1392 case LPFC_ABORT_IOCB:
1393 case LPFC_SOL_IOCB:
1394 /*
1395 * Idle exchange closed via ABTS from port. No iocb
1396 * resources need to be recovered.
1397 */
1398 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001399 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001400 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04001401 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05001402 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04001403 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05001404 break;
1405 }
1406
James Bottomley604a3e32005-10-29 10:28:33 -05001407 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1408 &rspiocbq);
dea31012005-04-17 16:05:31 -05001409 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001410 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1411 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1412 &rspiocbq);
1413 } else {
James Smart2e0fef82007-06-17 19:56:36 -05001414 spin_unlock_irqrestore(&phba->hbalock,
1415 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001416 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1417 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001418 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001419 iflag);
1420 }
dea31012005-04-17 16:05:31 -05001421 }
1422 break;
James Smarta4bc3372006-12-02 13:34:16 -05001423 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05001424 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001425 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001426 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001427 break;
dea31012005-04-17 16:05:31 -05001428 default:
1429 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1430 char adaptermsg[LPFC_MAX_ADPTMSG];
1431 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1432 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1433 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001434 dev_warn(&((phba->pcidev)->dev),
1435 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05001436 phba->brd_no, adaptermsg);
1437 } else {
1438 /* Unknown IOCB command */
1439 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001440 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05001441 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001442 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05001443 irsp->ulpStatus,
1444 irsp->ulpIoTag,
1445 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05001446 }
1447 break;
1448 }
1449
1450 /*
1451 * The response IOCB has been processed. Update the ring
1452 * pointer in SLIM. If the port response put pointer has not
1453 * been updated, sync the pgp->rspPutInx and fetch the new port
1454 * response put pointer.
1455 */
James Smarted957682007-06-17 19:56:37 -05001456 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05001457
1458 if (pring->rspidx == portRspPut)
1459 portRspPut = le32_to_cpu(pgp->rspPutInx);
1460 }
1461
1462 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
1463 pring->stats.iocb_rsp_full++;
1464 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1465 writel(status, phba->CAregaddr);
1466 readl(phba->CAregaddr);
1467 }
1468 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1469 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1470 pring->stats.iocb_cmd_empty++;
1471
1472 /* Force update of the local copy of cmdGetInx */
1473 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1474 lpfc_sli_resume_iocb(phba, pring);
1475
1476 if ((pring->lpfc_sli_cmd_available))
1477 (pring->lpfc_sli_cmd_available) (phba, pring);
1478
1479 }
1480
James Smart2e0fef82007-06-17 19:56:36 -05001481 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001482 return rc;
1483}
1484
dea31012005-04-17 16:05:31 -05001485int
James Smart2e0fef82007-06-17 19:56:36 -05001486lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
1487 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001488{
James Smarted957682007-06-17 19:56:37 -05001489 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1490 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1491 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001492 IOCB_t *entry;
1493 IOCB_t *irsp = NULL;
1494 struct lpfc_iocbq *rspiocbp = NULL;
1495 struct lpfc_iocbq *next_iocb;
1496 struct lpfc_iocbq *cmdiocbp;
1497 struct lpfc_iocbq *saveq;
dea31012005-04-17 16:05:31 -05001498 uint8_t iocb_cmd_type;
1499 lpfc_iocb_type type;
1500 uint32_t status, free_saveq;
1501 uint32_t portRspPut, portRspMax;
1502 int rc = 1;
1503 unsigned long iflag;
dea31012005-04-17 16:05:31 -05001504
James Smart2e0fef82007-06-17 19:56:36 -05001505 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001506 pring->stats.iocb_event++;
1507
dea31012005-04-17 16:05:31 -05001508 /*
1509 * The next available response entry should never exceed the maximum
1510 * entries. If it does, treat it as an adapter hardware error.
1511 */
1512 portRspMax = pring->numRiocb;
1513 portRspPut = le32_to_cpu(pgp->rspPutInx);
1514 if (portRspPut >= portRspMax) {
1515 /*
1516 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1517 * rsp ring <portRspMax>
1518 */
James Smarted957682007-06-17 19:56:37 -05001519 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001520 "0303 Ring %d handler: portRspPut %d "
dea31012005-04-17 16:05:31 -05001521 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001522 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05001523
James Smart2e0fef82007-06-17 19:56:36 -05001524 phba->link_state = LPFC_HBA_ERROR;
1525 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001526
1527 phba->work_hs = HS_FFER3;
1528 lpfc_handle_eratt(phba);
1529
1530 return 1;
1531 }
1532
1533 rmb();
dea31012005-04-17 16:05:31 -05001534 while (pring->rspidx != portRspPut) {
1535 /*
1536 * Build a completion list and call the appropriate handler.
1537 * The process is to get the next available response iocb, get
1538 * a free iocb from the list, copy the response data into the
1539 * free iocb, insert to the continuation list, and update the
1540 * next response index to slim. This process makes response
1541 * iocb's in the ring available to DMA as fast as possible but
1542 * pays a penalty for a copy operation. Since the iocb is
1543 * only 32 bytes, this penalty is considered small relative to
1544 * the PCI reads for register values and a slim write. When
1545 * the ulpLe field is set, the entire Command has been
1546 * received.
1547 */
James Smarted957682007-06-17 19:56:37 -05001548 entry = lpfc_resp_iocb(phba, pring);
1549
James Smart858c9f62007-06-17 19:56:39 -05001550 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05001551 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001552 if (rspiocbp == NULL) {
1553 printk(KERN_ERR "%s: out of buffers! Failing "
1554 "completion.\n", __FUNCTION__);
1555 break;
1556 }
1557
James Smarted957682007-06-17 19:56:37 -05001558 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
1559 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05001560 irsp = &rspiocbp->iocb;
1561
1562 if (++pring->rspidx >= portRspMax)
1563 pring->rspidx = 0;
1564
James Smarta58cbd52007-08-02 11:09:43 -04001565 if (pring->ringno == LPFC_ELS_RING) {
1566 lpfc_debugfs_slow_ring_trc(phba,
1567 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1568 *(((uint32_t *) irsp) + 4),
1569 *(((uint32_t *) irsp) + 6),
1570 *(((uint32_t *) irsp) + 7));
1571 }
1572
James Smarted957682007-06-17 19:56:37 -05001573 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05001574
1575 if (list_empty(&(pring->iocb_continueq))) {
1576 list_add(&rspiocbp->list, &(pring->iocb_continueq));
1577 } else {
1578 list_add_tail(&rspiocbp->list,
1579 &(pring->iocb_continueq));
1580 }
1581
1582 pring->iocb_continueq_cnt++;
1583 if (irsp->ulpLe) {
1584 /*
1585 * By default, the driver expects to free all resources
1586 * associated with this iocb completion.
1587 */
1588 free_saveq = 1;
1589 saveq = list_get_first(&pring->iocb_continueq,
1590 struct lpfc_iocbq, list);
1591 irsp = &(saveq->iocb);
1592 list_del_init(&pring->iocb_continueq);
1593 pring->iocb_continueq_cnt = 0;
1594
1595 pring->stats.iocb_rsp++;
1596
James Smart92d7f7b2007-06-17 19:56:38 -05001597 /*
1598 * If resource errors reported from HBA, reduce
1599 * queuedepths of the SCSI device.
1600 */
1601 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1602 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1603 spin_unlock_irqrestore(&phba->hbalock, iflag);
1604 lpfc_adjust_queue_depth(phba);
1605 spin_lock_irqsave(&phba->hbalock, iflag);
1606 }
1607
dea31012005-04-17 16:05:31 -05001608 if (irsp->ulpStatus) {
1609 /* Rsp ring <ringno> error: IOCB */
James Smarted957682007-06-17 19:56:37 -05001610 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001611 "0328 Rsp Ring %d error: "
James Smarted957682007-06-17 19:56:37 -05001612 "IOCB Data: "
1613 "x%x x%x x%x x%x "
1614 "x%x x%x x%x x%x "
1615 "x%x x%x x%x x%x "
1616 "x%x x%x x%x x%x\n",
James Smarted957682007-06-17 19:56:37 -05001617 pring->ringno,
1618 irsp->un.ulpWord[0],
1619 irsp->un.ulpWord[1],
1620 irsp->un.ulpWord[2],
1621 irsp->un.ulpWord[3],
1622 irsp->un.ulpWord[4],
1623 irsp->un.ulpWord[5],
1624 *(((uint32_t *) irsp) + 6),
1625 *(((uint32_t *) irsp) + 7),
1626 *(((uint32_t *) irsp) + 8),
1627 *(((uint32_t *) irsp) + 9),
1628 *(((uint32_t *) irsp) + 10),
1629 *(((uint32_t *) irsp) + 11),
1630 *(((uint32_t *) irsp) + 12),
1631 *(((uint32_t *) irsp) + 13),
1632 *(((uint32_t *) irsp) + 14),
1633 *(((uint32_t *) irsp) + 15));
dea31012005-04-17 16:05:31 -05001634 }
1635
1636 /*
1637 * Fetch the IOCB command type and call the correct
1638 * completion routine. Solicited and Unsolicited
1639 * IOCBs on the ELS ring get freed back to the
1640 * lpfc_iocb_list by the discovery kernel thread.
1641 */
1642 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
1643 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
1644 if (type == LPFC_SOL_IOCB) {
James Smart2e0fef82007-06-17 19:56:36 -05001645 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05001646 iflag);
1647 rc = lpfc_sli_process_sol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05001648 saveq);
1649 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001650 } else if (type == LPFC_UNSOL_IOCB) {
James Smart2e0fef82007-06-17 19:56:36 -05001651 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05001652 iflag);
1653 rc = lpfc_sli_process_unsol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05001654 saveq);
1655 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001656 } else if (type == LPFC_ABORT_IOCB) {
1657 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
1658 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05001659 lpfc_sli_iocbq_lookup(phba, pring,
1660 saveq)))) {
dea31012005-04-17 16:05:31 -05001661 /* Call the specified completion
1662 routine */
1663 if (cmdiocbp->iocb_cmpl) {
1664 spin_unlock_irqrestore(
James Smart2e0fef82007-06-17 19:56:36 -05001665 &phba->hbalock,
dea31012005-04-17 16:05:31 -05001666 iflag);
1667 (cmdiocbp->iocb_cmpl) (phba,
1668 cmdiocbp, saveq);
1669 spin_lock_irqsave(
James Smart2e0fef82007-06-17 19:56:36 -05001670 &phba->hbalock,
dea31012005-04-17 16:05:31 -05001671 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001672 } else
James Smart2e0fef82007-06-17 19:56:36 -05001673 __lpfc_sli_release_iocbq(phba,
James Bottomley604a3e32005-10-29 10:28:33 -05001674 cmdiocbp);
dea31012005-04-17 16:05:31 -05001675 }
1676 } else if (type == LPFC_UNKNOWN_IOCB) {
1677 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1678
1679 char adaptermsg[LPFC_MAX_ADPTMSG];
1680
1681 memset(adaptermsg, 0,
1682 LPFC_MAX_ADPTMSG);
1683 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1684 MAX_MSG_DATA);
1685 dev_warn(&((phba->pcidev)->dev),
Joe Perches898eb712007-10-18 03:06:30 -07001686 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05001687 phba->brd_no, adaptermsg);
1688 } else {
1689 /* Unknown IOCB command */
James Smart92d7f7b2007-06-17 19:56:38 -05001690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001691 "0335 Unknown IOCB "
James Smart92d7f7b2007-06-17 19:56:38 -05001692 "command Data: x%x "
1693 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001694 irsp->ulpCommand,
1695 irsp->ulpStatus,
1696 irsp->ulpIoTag,
1697 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05001698 }
1699 }
1700
1701 if (free_saveq) {
James Smart2e0fef82007-06-17 19:56:36 -05001702 list_for_each_entry_safe(rspiocbp, next_iocb,
1703 &saveq->list, list) {
1704 list_del(&rspiocbp->list);
1705 __lpfc_sli_release_iocbq(phba,
1706 rspiocbp);
dea31012005-04-17 16:05:31 -05001707 }
James Smart2e0fef82007-06-17 19:56:36 -05001708 __lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05001709 }
James Smart92d7f7b2007-06-17 19:56:38 -05001710 rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05001711 }
1712
1713 /*
1714 * If the port response put pointer has not been updated, sync
1715 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
1716 * response put pointer.
1717 */
1718 if (pring->rspidx == portRspPut) {
1719 portRspPut = le32_to_cpu(pgp->rspPutInx);
1720 }
1721 } /* while (pring->rspidx != portRspPut) */
1722
James Smart92d7f7b2007-06-17 19:56:38 -05001723 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05001724 /* At least one response entry has been freed */
1725 pring->stats.iocb_rsp_full++;
1726 /* SET RxRE_RSP in Chip Att register */
1727 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1728 writel(status, phba->CAregaddr);
1729 readl(phba->CAregaddr); /* flush */
1730 }
1731 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1732 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1733 pring->stats.iocb_cmd_empty++;
1734
1735 /* Force update of the local copy of cmdGetInx */
1736 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1737 lpfc_sli_resume_iocb(phba, pring);
1738
1739 if ((pring->lpfc_sli_cmd_available))
1740 (pring->lpfc_sli_cmd_available) (phba, pring);
1741
1742 }
1743
James Smart2e0fef82007-06-17 19:56:36 -05001744 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001745 return rc;
1746}
1747
James Smart2e0fef82007-06-17 19:56:36 -05001748void
dea31012005-04-17 16:05:31 -05001749lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1750{
James Smart2534ba72007-04-25 09:52:20 -04001751 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05001752 struct lpfc_iocbq *iocb, *next_iocb;
James Smart2534ba72007-04-25 09:52:20 -04001753 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05001754
James Smart92d7f7b2007-06-17 19:56:38 -05001755 if (pring->ringno == LPFC_ELS_RING) {
1756 lpfc_fabric_abort_hba(phba);
1757 }
1758
dea31012005-04-17 16:05:31 -05001759 /* Error everything on txq and txcmplq
1760 * First do the txq.
1761 */
James Smart2e0fef82007-06-17 19:56:36 -05001762 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04001763 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05001764 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05001765
1766 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04001767 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
1768 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1769
James Smart2e0fef82007-06-17 19:56:36 -05001770 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04001771
1772 while (!list_empty(&completions)) {
1773 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
dea31012005-04-17 16:05:31 -05001774 cmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05001775 list_del_init(&iocb->list);
dea31012005-04-17 16:05:31 -05001776
James Smart2e0fef82007-06-17 19:56:36 -05001777 if (!iocb->iocb_cmpl)
1778 lpfc_sli_release_iocbq(phba, iocb);
1779 else {
dea31012005-04-17 16:05:31 -05001780 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1781 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
dea31012005-04-17 16:05:31 -05001782 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05001783 }
dea31012005-04-17 16:05:31 -05001784 }
dea31012005-04-17 16:05:31 -05001785}
1786
Jamie Wellnitz41415862006-02-28 19:25:27 -05001787int
James Smart2e0fef82007-06-17 19:56:36 -05001788lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05001789{
Jamie Wellnitz41415862006-02-28 19:25:27 -05001790 uint32_t status;
1791 int i = 0;
1792 int retval = 0;
dea31012005-04-17 16:05:31 -05001793
Jamie Wellnitz41415862006-02-28 19:25:27 -05001794 /* Read the HBA Host Status Register */
1795 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05001796
Jamie Wellnitz41415862006-02-28 19:25:27 -05001797 /*
1798 * Check status register every 100ms for 5 retries, then every
1799 * 500ms for 5, then every 2.5 sec for 5, then reset board and
1800 * every 2.5 sec for 4.
1801 * Break our of the loop if errors occurred during init.
1802 */
1803 while (((status & mask) != mask) &&
1804 !(status & HS_FFERM) &&
1805 i++ < 20) {
dea31012005-04-17 16:05:31 -05001806
Jamie Wellnitz41415862006-02-28 19:25:27 -05001807 if (i <= 5)
1808 msleep(10);
1809 else if (i <= 10)
1810 msleep(500);
1811 else
1812 msleep(2500);
dea31012005-04-17 16:05:31 -05001813
Jamie Wellnitz41415862006-02-28 19:25:27 -05001814 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05001815 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05001816 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001817 lpfc_sli_brdrestart(phba);
1818 }
1819 /* Read the HBA Host Status Register */
1820 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05001821 }
dea31012005-04-17 16:05:31 -05001822
Jamie Wellnitz41415862006-02-28 19:25:27 -05001823 /* Check to see if any errors occurred during init */
1824 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05001825 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001826 retval = 1;
1827 }
dea31012005-04-17 16:05:31 -05001828
Jamie Wellnitz41415862006-02-28 19:25:27 -05001829 return retval;
dea31012005-04-17 16:05:31 -05001830}
1831
James Smart92908312006-03-07 15:04:13 -05001832#define BARRIER_TEST_PATTERN (0xdeadbeef)
1833
James Smart2e0fef82007-06-17 19:56:36 -05001834void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05001835{
James Smart65a29c12006-07-06 15:50:50 -04001836 uint32_t __iomem *resp_buf;
1837 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05001838 volatile uint32_t mbox;
1839 uint32_t hc_copy;
1840 int i;
1841 uint8_t hdrtype;
1842
1843 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
1844 if (hdrtype != 0x80 ||
1845 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
1846 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
1847 return;
1848
1849 /*
1850 * Tell the other part of the chip to suspend temporarily all
1851 * its DMA activity.
1852 */
James Smart65a29c12006-07-06 15:50:50 -04001853 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05001854
1855 /* Disable the error attention */
1856 hc_copy = readl(phba->HCregaddr);
1857 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
1858 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001859 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05001860
1861 if (readl(phba->HAregaddr) & HA_ERATT) {
1862 /* Clear Chip error bit */
1863 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001864 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05001865 }
1866
1867 mbox = 0;
1868 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
1869 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
1870
1871 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04001872 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05001873 writel(mbox, mbox_buf);
1874
1875 for (i = 0;
1876 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
1877 mdelay(1);
1878
1879 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
1880 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05001881 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05001882 goto restore_hc;
1883 else
1884 goto clear_errat;
1885 }
1886
1887 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
1888 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
1889 mdelay(1);
1890
1891clear_errat:
1892
1893 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
1894 mdelay(1);
1895
1896 if (readl(phba->HAregaddr) & HA_ERATT) {
1897 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001898 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05001899 }
1900
1901restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05001902 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05001903 writel(hc_copy, phba->HCregaddr);
1904 readl(phba->HCregaddr); /* flush */
1905}
1906
Jamie Wellnitz41415862006-02-28 19:25:27 -05001907int
James Smart2e0fef82007-06-17 19:56:36 -05001908lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001909{
Jamie Wellnitz41415862006-02-28 19:25:27 -05001910 struct lpfc_sli *psli;
1911 LPFC_MBOXQ_t *pmb;
1912 uint32_t status;
1913 uint32_t ha_copy;
1914 int retval;
1915 int i = 0;
1916
1917 psli = &phba->sli;
1918
1919 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05001920 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001921 "0329 Kill HBA Data: x%x x%x\n",
1922 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001923
1924 if ((pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
James Smart92908312006-03-07 15:04:13 -05001925 GFP_KERNEL)) == 0)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001926 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001927
1928 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05001929 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001930 status = readl(phba->HCregaddr);
1931 status &= ~HC_ERINT_ENA;
1932 writel(status, phba->HCregaddr);
1933 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001934 phba->link_flag |= LS_IGNORE_ERATT;
1935 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001936
1937 lpfc_kill_board(phba, pmb);
1938 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1939 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1940
1941 if (retval != MBX_SUCCESS) {
1942 if (retval != MBX_BUSY)
1943 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05001944 spin_lock_irq(&phba->hbalock);
1945 phba->link_flag &= ~LS_IGNORE_ERATT;
1946 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001947 return 1;
1948 }
1949
James Smart92908312006-03-07 15:04:13 -05001950 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
1951
Jamie Wellnitz41415862006-02-28 19:25:27 -05001952 mempool_free(pmb, phba->mbox_mem_pool);
1953
1954 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
1955 * attention every 100ms for 3 seconds. If we don't get ERATT after
1956 * 3 seconds we still set HBA_ERROR state because the status of the
1957 * board is now undefined.
1958 */
1959 ha_copy = readl(phba->HAregaddr);
1960
1961 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
1962 mdelay(100);
1963 ha_copy = readl(phba->HAregaddr);
1964 }
1965
1966 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05001967 if (ha_copy & HA_ERATT) {
1968 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001969 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05001970 }
James Smart2e0fef82007-06-17 19:56:36 -05001971 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001972 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001973 phba->link_flag &= ~LS_IGNORE_ERATT;
1974 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001975
1976 psli->mbox_active = NULL;
1977 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001978 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001979
James Smart2e0fef82007-06-17 19:56:36 -05001980 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001981}
1982
1983int
James Smart2e0fef82007-06-17 19:56:36 -05001984lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001985{
1986 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05001987 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001988 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05001989 int i;
dea31012005-04-17 16:05:31 -05001990
Jamie Wellnitz41415862006-02-28 19:25:27 -05001991 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001992
Jamie Wellnitz41415862006-02-28 19:25:27 -05001993 /* Reset HBA */
1994 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001995 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05001996 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05001997
1998 /* perform board reset */
1999 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002000 phba->pport->fc_myDID = 0;
2001 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002002
Jamie Wellnitz41415862006-02-28 19:25:27 -05002003 /* Turn off parity checking and serr during the physical reset */
2004 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2005 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2006 (cfg_value &
2007 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2008
James Smart1c067a42006-08-01 07:33:52 -04002009 psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002010 /* Now toggle INITFF bit in the Host Control Register */
2011 writel(HC_INITFF, phba->HCregaddr);
2012 mdelay(1);
2013 readl(phba->HCregaddr); /* flush */
2014 writel(0, phba->HCregaddr);
2015 readl(phba->HCregaddr); /* flush */
2016
2017 /* Restore PCI cmd register */
2018 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002019
2020 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002021 for (i = 0; i < psli->num_rings; i++) {
2022 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002023 pring->flag = 0;
2024 pring->rspidx = 0;
2025 pring->next_cmdidx = 0;
2026 pring->local_getidx = 0;
2027 pring->cmdidx = 0;
2028 pring->missbufcnt = 0;
2029 }
dea31012005-04-17 16:05:31 -05002030
James Smart2e0fef82007-06-17 19:56:36 -05002031 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002032 return 0;
2033}
2034
2035int
James Smart2e0fef82007-06-17 19:56:36 -05002036lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002037{
2038 MAILBOX_t *mb;
2039 struct lpfc_sli *psli;
2040 uint16_t skip_post;
2041 volatile uint32_t word0;
2042 void __iomem *to_slim;
2043
James Smart2e0fef82007-06-17 19:56:36 -05002044 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002045
2046 psli = &phba->sli;
2047
2048 /* Restart HBA */
2049 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002050 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002051 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002052
2053 word0 = 0;
2054 mb = (MAILBOX_t *) &word0;
2055 mb->mbxCommand = MBX_RESTART;
2056 mb->mbxHc = 1;
2057
James Smart92908312006-03-07 15:04:13 -05002058 lpfc_reset_barrier(phba);
2059
Jamie Wellnitz41415862006-02-28 19:25:27 -05002060 to_slim = phba->MBslimaddr;
2061 writel(*(uint32_t *) mb, to_slim);
2062 readl(to_slim); /* flush */
2063
2064 /* Only skip post after fc_ffinit is completed */
James Smart2e0fef82007-06-17 19:56:36 -05002065 if (phba->pport->port_state) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002066 skip_post = 1;
2067 word0 = 1; /* This is really setting up word1 */
dea31012005-04-17 16:05:31 -05002068 } else {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002069 skip_post = 0;
2070 word0 = 0; /* This is really setting up word1 */
2071 }
James Smart65a29c12006-07-06 15:50:50 -04002072 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002073 writel(*(uint32_t *) mb, to_slim);
2074 readl(to_slim); /* flush */
2075
2076 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002077 phba->pport->stopped = 0;
2078 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002079
James Smart2e0fef82007-06-17 19:56:36 -05002080 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002081
James Smart64ba8812006-08-02 15:24:34 -04002082 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2083 psli->stats_start = get_seconds();
2084
Jamie Wellnitz41415862006-02-28 19:25:27 -05002085 if (skip_post)
2086 mdelay(100);
2087 else
dea31012005-04-17 16:05:31 -05002088 mdelay(2000);
dea31012005-04-17 16:05:31 -05002089
Jamie Wellnitz41415862006-02-28 19:25:27 -05002090 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002091
2092 return 0;
2093}
2094
2095static int
2096lpfc_sli_chipset_init(struct lpfc_hba *phba)
2097{
2098 uint32_t status, i = 0;
2099
2100 /* Read the HBA Host Status Register */
2101 status = readl(phba->HSregaddr);
2102
2103 /* Check status register to see what current state is */
2104 i = 0;
2105 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2106
2107 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2108 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2109 * 4.
2110 */
2111 if (i++ >= 20) {
2112 /* Adapter failed to init, timeout, status reg
2113 <status> */
James Smarted957682007-06-17 19:56:37 -05002114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002115 "0436 Adapter failed to init, "
2116 "timeout, status reg x%x\n", status);
James Smart2e0fef82007-06-17 19:56:36 -05002117 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002118 return -ETIMEDOUT;
2119 }
2120
2121 /* Check to see if any errors occurred during init */
2122 if (status & HS_FFERM) {
2123 /* ERROR: During chipset initialization */
2124 /* Adapter failed to init, chipset, status reg
2125 <status> */
James Smarted957682007-06-17 19:56:37 -05002126 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002127 "0437 Adapter failed to init, "
2128 "chipset, status reg x%x\n", status);
James Smart2e0fef82007-06-17 19:56:36 -05002129 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002130 return -EIO;
2131 }
2132
2133 if (i <= 5) {
2134 msleep(10);
2135 } else if (i <= 10) {
2136 msleep(500);
2137 } else {
2138 msleep(2500);
2139 }
2140
2141 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002142 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002143 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002144 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002145 }
2146 /* Read the HBA Host Status Register */
2147 status = readl(phba->HSregaddr);
2148 }
2149
2150 /* Check to see if any errors occurred during init */
2151 if (status & HS_FFERM) {
2152 /* ERROR: During chipset initialization */
2153 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05002154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002155 "0438 Adapter failed to init, chipset, "
2156 "status reg x%x\n", status);
James Smart2e0fef82007-06-17 19:56:36 -05002157 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002158 return -EIO;
2159 }
2160
2161 /* Clear all interrupt enable conditions */
2162 writel(0, phba->HCregaddr);
2163 readl(phba->HCregaddr); /* flush */
2164
2165 /* setup host attn register */
2166 writel(0xffffffff, phba->HAregaddr);
2167 readl(phba->HAregaddr); /* flush */
2168 return 0;
2169}
2170
James Smart78b2d852007-08-02 11:10:21 -04002171int
James Smarted957682007-06-17 19:56:37 -05002172lpfc_sli_hbq_count(void)
2173{
James Smart92d7f7b2007-06-17 19:56:38 -05002174 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05002175}
2176
2177static int
2178lpfc_sli_hbq_entry_count(void)
2179{
2180 int hbq_count = lpfc_sli_hbq_count();
2181 int count = 0;
2182 int i;
2183
2184 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05002185 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05002186 return count;
2187}
2188
dea31012005-04-17 16:05:31 -05002189int
James Smarted957682007-06-17 19:56:37 -05002190lpfc_sli_hbq_size(void)
2191{
2192 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2193}
2194
2195static int
2196lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2197{
2198 int hbq_count = lpfc_sli_hbq_count();
2199 LPFC_MBOXQ_t *pmb;
2200 MAILBOX_t *pmbox;
2201 uint32_t hbqno;
2202 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05002203
James Smart92d7f7b2007-06-17 19:56:38 -05002204 /* Get a Mailbox buffer to setup mailbox
2205 * commands for HBA initialization
2206 */
James Smarted957682007-06-17 19:56:37 -05002207 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2208
2209 if (!pmb)
2210 return -ENOMEM;
2211
2212 pmbox = &pmb->mb;
2213
2214 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2215 phba->link_state = LPFC_INIT_MBX_CMDS;
2216
2217 hbq_entry_index = 0;
2218 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2219 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2220 phba->hbqs[hbqno].hbqPutIdx = 0;
2221 phba->hbqs[hbqno].local_hbqGetIdx = 0;
2222 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05002223 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04002224 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2225 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05002226 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2227
2228 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2229 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2230 mbxStatus <status>, ring <num> */
2231
2232 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002233 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002234 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05002235 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002236 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05002237 pmbox->mbxStatus, hbqno);
2238
2239 phba->link_state = LPFC_HBA_ERROR;
2240 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05002241 return ENXIO;
2242 }
2243 }
2244 phba->hbq_count = hbq_count;
2245
James Smarted957682007-06-17 19:56:37 -05002246 mempool_free(pmb, phba->mbox_mem_pool);
2247
James Smart92d7f7b2007-06-17 19:56:38 -05002248 /* Initially populate or replenish the HBQs */
2249 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2250 if (lpfc_sli_hbqbuf_init_hbqs(phba, hbqno))
2251 return -ENOMEM;
2252 }
James Smarted957682007-06-17 19:56:37 -05002253 return 0;
2254}
2255
2256static int
2257lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05002258{
2259 LPFC_MBOXQ_t *pmb;
2260 uint32_t resetcount = 0, rc = 0, done = 0;
2261
2262 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2263 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05002264 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002265 return -ENOMEM;
2266 }
2267
James Smarted957682007-06-17 19:56:37 -05002268 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05002269 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05002270 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002271 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002272 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002273 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002274 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002275 msleep(2500);
2276 rc = lpfc_sli_chipset_init(phba);
2277 if (rc)
2278 break;
2279
James Smart2e0fef82007-06-17 19:56:36 -05002280 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002281 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002282 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002283 resetcount++;
2284
James Smarted957682007-06-17 19:56:37 -05002285 /* Call pre CONFIG_PORT mailbox command initialization. A
2286 * value of 0 means the call was successful. Any other
2287 * nonzero value is a failure, but if ERESTART is returned,
2288 * the driver may reset the HBA and try again.
2289 */
dea31012005-04-17 16:05:31 -05002290 rc = lpfc_config_port_prep(phba);
2291 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05002292 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05002293 continue;
2294 } else if (rc) {
2295 break;
2296 }
2297
James Smart2e0fef82007-06-17 19:56:36 -05002298 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05002299 lpfc_config_port(phba, pmb);
2300 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smarted957682007-06-17 19:56:37 -05002301 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05002302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002303 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002304 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002305 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05002306 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002307 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002308 spin_unlock_irq(&phba->hbalock);
2309 rc = -ENXIO;
James Smarted957682007-06-17 19:56:37 -05002310 } else {
2311 done = 1;
James Smart92d7f7b2007-06-17 19:56:38 -05002312 phba->max_vpi = (phba->max_vpi &&
2313 pmb->mb.un.varCfgPort.gmv) != 0
2314 ? pmb->mb.un.varCfgPort.max_vpi
2315 : 0;
dea31012005-04-17 16:05:31 -05002316 }
2317 }
James Smarted957682007-06-17 19:56:37 -05002318
2319 if (!done) {
2320 rc = -EINVAL;
2321 goto do_prep_failed;
2322 }
2323
2324 if ((pmb->mb.un.varCfgPort.sli_mode == 3) &&
James Smart92d7f7b2007-06-17 19:56:38 -05002325 (!pmb->mb.un.varCfgPort.cMA)) {
James Smarted957682007-06-17 19:56:37 -05002326 rc = -ENXIO;
2327 goto do_prep_failed;
2328 }
2329 return rc;
2330
James Smart92d7f7b2007-06-17 19:56:38 -05002331do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05002332 mempool_free(pmb, phba->mbox_mem_pool);
2333 return rc;
2334}
2335
2336int
2337lpfc_sli_hba_setup(struct lpfc_hba *phba)
2338{
2339 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05002340 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05002341
2342 switch (lpfc_sli_mode) {
2343 case 2:
James Smart78b2d852007-08-02 11:10:21 -04002344 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05002345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002346 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05002347 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04002348 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05002349 break;
2350 }
James Smarted957682007-06-17 19:56:37 -05002351 mode = 2;
2352 break;
2353 case 0:
2354 case 3:
2355 break;
2356 default:
James Smart92d7f7b2007-06-17 19:56:38 -05002357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002358 "1819 Unrecognized lpfc_sli_mode "
2359 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05002360
2361 break;
2362 }
2363
2364 rc = lpfc_do_config_port(phba, mode);
2365 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05002366 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002367 "1820 Unable to select SLI-3. "
2368 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05002369 if (rc && mode != 2)
2370 rc = lpfc_do_config_port(phba, 2);
2371 if (rc)
dea31012005-04-17 16:05:31 -05002372 goto lpfc_sli_hba_setup_error;
2373
James Smarted957682007-06-17 19:56:37 -05002374 if (phba->sli_rev == 3) {
2375 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
2376 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
2377 phba->sli3_options |= LPFC_SLI3_ENABLED;
2378 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
2379
2380 } else {
2381 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
2382 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05002383 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05002384 }
2385
2386 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002387 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
2388 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05002389 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05002390
2391 if (rc)
2392 goto lpfc_sli_hba_setup_error;
2393
James Smart92d7f7b2007-06-17 19:56:38 -05002394 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05002395
2396 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2397 rc = lpfc_sli_hbq_setup(phba);
2398 if (rc)
2399 goto lpfc_sli_hba_setup_error;
2400 }
2401
dea31012005-04-17 16:05:31 -05002402 phba->sli.sli_flag |= LPFC_PROCESS_LA;
2403
2404 rc = lpfc_config_port_post(phba);
2405 if (rc)
2406 goto lpfc_sli_hba_setup_error;
2407
James Smarted957682007-06-17 19:56:37 -05002408 return rc;
2409
James Smart92d7f7b2007-06-17 19:56:38 -05002410lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05002411 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05002412 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002413 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05002414 return rc;
2415}
2416
dea31012005-04-17 16:05:31 -05002417/*! lpfc_mbox_timeout
2418 *
2419 * \pre
2420 * \post
2421 * \param hba Pointer to per struct lpfc_hba structure
2422 * \param l1 Pointer to the driver's mailbox queue.
2423 * \return
2424 * void
2425 *
2426 * \b Description:
2427 *
2428 * This routine handles mailbox timeout events at timer interrupt context.
2429 */
2430void
2431lpfc_mbox_timeout(unsigned long ptr)
2432{
James Smart92d7f7b2007-06-17 19:56:38 -05002433 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05002434 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05002435 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05002436
James Smart2e0fef82007-06-17 19:56:36 -05002437 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05002438 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05002439 if (!tmo_posted)
2440 phba->pport->work_port_events |= WORKER_MBOX_TMO;
2441 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
2442
2443 if (!tmo_posted) {
James Smart92d7f7b2007-06-17 19:56:38 -05002444 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002445 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05002446 lpfc_worker_wake_up(phba);
2447 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002448 }
dea31012005-04-17 16:05:31 -05002449}
2450
2451void
2452lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
2453{
James Smart2e0fef82007-06-17 19:56:36 -05002454 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
2455 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04002456 struct lpfc_sli *psli = &phba->sli;
2457 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05002458
James Smart2e0fef82007-06-17 19:56:36 -05002459 if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
dea31012005-04-17 16:05:31 -05002460 return;
2461 }
2462
dea31012005-04-17 16:05:31 -05002463 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05002464 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002465 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002466 mb->mbxCommand,
2467 phba->pport->port_state,
2468 phba->sli.sli_flag,
2469 phba->sli.mbox_active);
dea31012005-04-17 16:05:31 -05002470
James Smart1dcb58e2007-04-25 09:51:30 -04002471 /* Setting state unknown so lpfc_sli_abort_iocb_ring
2472 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
2473 * it to fail all oustanding SCSI IO.
2474 */
James Smart2e0fef82007-06-17 19:56:36 -05002475 spin_lock_irq(&phba->pport->work_port_lock);
2476 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
2477 spin_unlock_irq(&phba->pport->work_port_lock);
2478 spin_lock_irq(&phba->hbalock);
2479 phba->link_state = LPFC_LINK_UNKNOWN;
2480 phba->pport->fc_flag |= FC_ESTABLISH_LINK;
James Smart1dcb58e2007-04-25 09:51:30 -04002481 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002482 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04002483
2484 pring = &psli->ring[psli->fcp_ring];
2485 lpfc_sli_abort_iocb_ring(phba, pring);
2486
2487 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002488 "0316 Resetting board due to mailbox timeout\n");
James Smart1dcb58e2007-04-25 09:51:30 -04002489 /*
2490 * lpfc_offline calls lpfc_sli_hba_down which will clean up
2491 * on oustanding mailbox commands.
2492 */
2493 lpfc_offline_prep(phba);
2494 lpfc_offline(phba);
2495 lpfc_sli_brdrestart(phba);
2496 if (lpfc_online(phba) == 0) /* Initialize the HBA */
2497 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
2498 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002499 return;
2500}
2501
2502int
James Smart2e0fef82007-06-17 19:56:36 -05002503lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
dea31012005-04-17 16:05:31 -05002504{
dea31012005-04-17 16:05:31 -05002505 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05002506 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002507 uint32_t status, evtctr;
2508 uint32_t ha_copy;
2509 int i;
2510 unsigned long drvr_flag = 0;
2511 volatile uint32_t word0, ldata;
2512 void __iomem *to_slim;
2513
James Smarted957682007-06-17 19:56:37 -05002514 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05002515 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05002516 if(!pmbox->vport) {
2517 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002518 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002519 "1806 Mbox x%x failed. No vport\n",
James Smarted957682007-06-17 19:56:37 -05002520 pmbox->mb.mbxCommand);
2521 dump_stack();
2522 return MBXERR_ERROR;
2523 }
2524 }
2525
James Smart92d7f7b2007-06-17 19:56:38 -05002526
Linas Vepstas8d63f372007-02-14 14:28:36 -06002527 /* If the PCI channel is in offline state, do not post mbox. */
2528 if (unlikely(pci_channel_offline(phba->pcidev)))
2529 return MBX_NOT_FINISHED;
2530
James Smart2e0fef82007-06-17 19:56:36 -05002531 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002532 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05002533
2534
dea31012005-04-17 16:05:31 -05002535 mb = &pmbox->mb;
2536 status = MBX_SUCCESS;
2537
James Smart2e0fef82007-06-17 19:56:36 -05002538 if (phba->link_state == LPFC_HBA_ERROR) {
2539 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002540
2541 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002542 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
James Smart2e0fef82007-06-17 19:56:36 -05002543 return MBX_NOT_FINISHED;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002544 }
2545
James Smart92908312006-03-07 15:04:13 -05002546 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
2547 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05002548 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart92d7f7b2007-06-17 19:56:38 -05002549 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
James Smart2e0fef82007-06-17 19:56:36 -05002550 return MBX_NOT_FINISHED;
James Smart92908312006-03-07 15:04:13 -05002551 }
2552
dea31012005-04-17 16:05:31 -05002553 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
2554 /* Polling for a mbox command when another one is already active
2555 * is not allowed in SLI. Also, the driver must have established
2556 * SLI2 mode to queue and process multiple mbox commands.
2557 */
2558
2559 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05002560 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002561
2562 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002563 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart2e0fef82007-06-17 19:56:36 -05002564 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05002565 }
2566
2567 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002568 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002569 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002570 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart2e0fef82007-06-17 19:56:36 -05002571 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05002572 }
2573
2574 /* Handle STOP IOCB processing flag. This is only meaningful
2575 * if we are not polling for mbox completion.
2576 */
2577 if (flag & MBX_STOP_IOCB) {
2578 flag &= ~MBX_STOP_IOCB;
2579 /* Now flag each ring */
2580 for (i = 0; i < psli->num_rings; i++) {
2581 /* If the ring is active, flag it */
2582 if (psli->ring[i].cmdringaddr) {
2583 psli->ring[i].flag |=
2584 LPFC_STOP_IOCB_MBX;
2585 }
2586 }
2587 }
2588
2589 /* Another mailbox command is still being processed, queue this
2590 * command to be processed later.
2591 */
2592 lpfc_mbox_put(phba, pmbox);
2593
2594 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05002595 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002596 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002597 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002598 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
2599 mb->mbxCommand, phba->pport->port_state,
2600 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05002601
2602 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05002603 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002604
James Smart858c9f62007-06-17 19:56:39 -05002605 if (pmbox->vport) {
2606 lpfc_debugfs_disc_trc(pmbox->vport,
2607 LPFC_DISC_TRC_MBOX_VPORT,
2608 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
2609 (uint32_t)mb->mbxCommand,
2610 mb->un.varWords[0], mb->un.varWords[1]);
2611 }
2612 else {
2613 lpfc_debugfs_disc_trc(phba->pport,
2614 LPFC_DISC_TRC_MBOX,
2615 "MBOX Bsy: cmd:x%x mb:x%x x%x",
2616 (uint32_t)mb->mbxCommand,
2617 mb->un.varWords[0], mb->un.varWords[1]);
2618 }
2619
James Smart2e0fef82007-06-17 19:56:36 -05002620 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05002621 }
2622
2623 /* Handle STOP IOCB processing flag. This is only meaningful
2624 * if we are not polling for mbox completion.
2625 */
2626 if (flag & MBX_STOP_IOCB) {
2627 flag &= ~MBX_STOP_IOCB;
2628 if (flag == MBX_NOWAIT) {
2629 /* Now flag each ring */
2630 for (i = 0; i < psli->num_rings; i++) {
2631 /* If the ring is active, flag it */
2632 if (psli->ring[i].cmdringaddr) {
2633 psli->ring[i].flag |=
2634 LPFC_STOP_IOCB_MBX;
2635 }
2636 }
2637 }
2638 }
2639
2640 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2641
2642 /* If we are not polling, we MUST be in SLI2 mode */
2643 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002644 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
2645 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05002646 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002647 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002648 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05002649 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart2e0fef82007-06-17 19:56:36 -05002650 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05002651 }
2652 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04002653 mod_timer(&psli->mbox_tmo, (jiffies +
2654 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05002655 }
2656
2657 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05002658 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002659 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002660 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002661 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05002662 mb->mbxCommand, phba->pport->port_state,
2663 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05002664
James Smart858c9f62007-06-17 19:56:39 -05002665 if (mb->mbxCommand != MBX_HEARTBEAT) {
2666 if (pmbox->vport) {
2667 lpfc_debugfs_disc_trc(pmbox->vport,
2668 LPFC_DISC_TRC_MBOX_VPORT,
2669 "MBOX Send vport: cmd:x%x mb:x%x x%x",
2670 (uint32_t)mb->mbxCommand,
2671 mb->un.varWords[0], mb->un.varWords[1]);
2672 }
2673 else {
2674 lpfc_debugfs_disc_trc(phba->pport,
2675 LPFC_DISC_TRC_MBOX,
2676 "MBOX Send: cmd:x%x mb:x%x x%x",
2677 (uint32_t)mb->mbxCommand,
2678 mb->un.varWords[0], mb->un.varWords[1]);
2679 }
2680 }
2681
dea31012005-04-17 16:05:31 -05002682 psli->slistat.mbox_cmd++;
2683 evtctr = psli->slistat.mbox_event;
2684
2685 /* next set own bit for the adapter and copy over command word */
2686 mb->mbxOwner = OWN_CHIP;
2687
2688 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002689 /* First copy command data to host SLIM area */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002690 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002691 } else {
James Smart92908312006-03-07 15:04:13 -05002692 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05002693 /* copy command data into host mbox for cmpl */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002694 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
James Smart92d7f7b2007-06-17 19:56:38 -05002695 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002696 }
2697
2698 /* First copy mbox command data to HBA SLIM, skip past first
2699 word */
2700 to_slim = phba->MBslimaddr + sizeof (uint32_t);
2701 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
2702 MAILBOX_CMD_SIZE - sizeof (uint32_t));
2703
2704 /* Next copy over first word, with mbxOwner set */
2705 ldata = *((volatile uint32_t *)mb);
2706 to_slim = phba->MBslimaddr;
2707 writel(ldata, to_slim);
2708 readl(to_slim); /* flush */
2709
2710 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2711 /* switch over to host mailbox */
2712 psli->sli_flag |= LPFC_SLI2_ACTIVE;
2713 }
2714 }
2715
2716 wmb();
2717 /* interrupt board to doit right away */
2718 writel(CA_MBATT, phba->CAregaddr);
2719 readl(phba->CAregaddr); /* flush */
2720
2721 switch (flag) {
2722 case MBX_NOWAIT:
2723 /* Don't wait for it to finish, just return */
2724 psli->mbox_active = pmbox;
2725 break;
2726
2727 case MBX_POLL:
dea31012005-04-17 16:05:31 -05002728 psli->mbox_active = NULL;
2729 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2730 /* First read mbox status word */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002731 word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002732 word0 = le32_to_cpu(word0);
2733 } else {
2734 /* First read mbox status word */
2735 word0 = readl(phba->MBslimaddr);
2736 }
2737
2738 /* Read the HBA Host Attention Register */
2739 ha_copy = readl(phba->HAregaddr);
2740
James Smarta309a6b2006-08-01 07:33:43 -04002741 i = lpfc_mbox_tmo_val(phba, mb->mbxCommand);
2742 i *= 1000; /* Convert to ms */
2743
dea31012005-04-17 16:05:31 -05002744 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002745 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
2746 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05002747 (phba->link_state > LPFC_WARM_START))) {
James Smarta309a6b2006-08-01 07:33:43 -04002748 if (i-- <= 0) {
dea31012005-04-17 16:05:31 -05002749 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002750 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05002751 drvr_flag);
James Smart2e0fef82007-06-17 19:56:36 -05002752 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05002753 }
2754
2755 /* Check if we took a mbox interrupt while we were
2756 polling */
2757 if (((word0 & OWN_CHIP) != OWN_CHIP)
2758 && (evtctr != psli->slistat.mbox_event))
2759 break;
2760
James Smart2e0fef82007-06-17 19:56:36 -05002761 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05002762 drvr_flag);
2763
James Smart1dcb58e2007-04-25 09:51:30 -04002764 msleep(1);
dea31012005-04-17 16:05:31 -05002765
James Smart2e0fef82007-06-17 19:56:36 -05002766 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002767
2768 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2769 /* First copy command data */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002770 word0 = *((volatile uint32_t *)
2771 &phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002772 word0 = le32_to_cpu(word0);
2773 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2774 MAILBOX_t *slimmb;
2775 volatile uint32_t slimword0;
2776 /* Check real SLIM for any errors */
2777 slimword0 = readl(phba->MBslimaddr);
2778 slimmb = (MAILBOX_t *) & slimword0;
2779 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
2780 && slimmb->mbxStatus) {
2781 psli->sli_flag &=
2782 ~LPFC_SLI2_ACTIVE;
2783 word0 = slimword0;
2784 }
2785 }
2786 } else {
2787 /* First copy command data */
2788 word0 = readl(phba->MBslimaddr);
2789 }
2790 /* Read the HBA Host Attention Register */
2791 ha_copy = readl(phba->HAregaddr);
2792 }
2793
2794 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002795 /* copy results back to user */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002796 lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
James Smart92d7f7b2007-06-17 19:56:38 -05002797 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002798 } else {
2799 /* First copy command data */
2800 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2801 MAILBOX_CMD_SIZE);
2802 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2803 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05002804 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05002805 phba->MBslimaddr + DMP_RSP_OFFSET,
2806 mb->un.varDmp.word_cnt);
2807 }
2808 }
2809
2810 writel(HA_MBATT, phba->HAregaddr);
2811 readl(phba->HAregaddr); /* flush */
2812
2813 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2814 status = mb->mbxStatus;
2815 }
2816
James Smart2e0fef82007-06-17 19:56:36 -05002817 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2818 return status;
dea31012005-04-17 16:05:31 -05002819}
2820
James Smart92d7f7b2007-06-17 19:56:38 -05002821/*
2822 * Caller needs to hold lock.
2823 */
James Smart858c9f62007-06-17 19:56:39 -05002824static void
James Smart92d7f7b2007-06-17 19:56:38 -05002825__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05002826 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05002827{
2828 /* Insert the caller's iocb in the txq tail for later processing. */
2829 list_add_tail(&piocb->list, &pring->txq);
2830 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05002831}
2832
2833static struct lpfc_iocbq *
2834lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05002835 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05002836{
2837 struct lpfc_iocbq * nextiocb;
2838
2839 nextiocb = lpfc_sli_ringtx_get(phba, pring);
2840 if (!nextiocb) {
2841 nextiocb = *piocb;
2842 *piocb = NULL;
2843 }
2844
2845 return nextiocb;
2846}
2847
James Smart92d7f7b2007-06-17 19:56:38 -05002848/*
2849 * Lockless version of lpfc_sli_issue_iocb.
2850 */
dea31012005-04-17 16:05:31 -05002851int
James Smart92d7f7b2007-06-17 19:56:38 -05002852__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002853 struct lpfc_iocbq *piocb, uint32_t flag)
2854{
2855 struct lpfc_iocbq *nextiocb;
2856 IOCB_t *iocb;
2857
James Smart92d7f7b2007-06-17 19:56:38 -05002858 if (piocb->iocb_cmpl && (!piocb->vport) &&
2859 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2860 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
2861 lpfc_printf_log(phba, KERN_ERR,
2862 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002863 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002864 piocb->iocb.ulpCommand);
2865 dump_stack();
2866 return IOCB_ERROR;
2867 }
2868
2869
Linas Vepstas8d63f372007-02-14 14:28:36 -06002870 /* If the PCI channel is in offline state, do not post iocbs. */
2871 if (unlikely(pci_channel_offline(phba->pcidev)))
2872 return IOCB_ERROR;
2873
dea31012005-04-17 16:05:31 -05002874 /*
2875 * We should never get an IOCB if we are in a < LINK_DOWN state
2876 */
James Smart2e0fef82007-06-17 19:56:36 -05002877 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05002878 return IOCB_ERROR;
2879
2880 /*
2881 * Check to see if we are blocking IOCB processing because of a
2882 * outstanding mbox command.
2883 */
2884 if (unlikely(pring->flag & LPFC_STOP_IOCB_MBX))
2885 goto iocb_busy;
2886
James Smart2e0fef82007-06-17 19:56:36 -05002887 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05002888 /*
James Smart2680eea2007-04-25 09:52:55 -04002889 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05002890 * can be issued if the link is not up.
2891 */
2892 switch (piocb->iocb.ulpCommand) {
2893 case CMD_QUE_RING_BUF_CN:
2894 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05002895 /*
2896 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
2897 * completion, iocb_cmpl MUST be 0.
2898 */
2899 if (piocb->iocb_cmpl)
2900 piocb->iocb_cmpl = NULL;
2901 /*FALLTHROUGH*/
2902 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04002903 case CMD_CLOSE_XRI_CN:
2904 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05002905 break;
2906 default:
2907 goto iocb_busy;
2908 }
2909
2910 /*
2911 * For FCP commands, we must be in a state where we can process link
2912 * attention events.
2913 */
2914 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05002915 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05002916 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05002917 }
dea31012005-04-17 16:05:31 -05002918
dea31012005-04-17 16:05:31 -05002919 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2920 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
2921 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2922
2923 if (iocb)
2924 lpfc_sli_update_ring(phba, pring);
2925 else
2926 lpfc_sli_update_full_ring(phba, pring);
2927
2928 if (!piocb)
2929 return IOCB_SUCCESS;
2930
2931 goto out_busy;
2932
2933 iocb_busy:
2934 pring->stats.iocb_cmd_delay++;
2935
2936 out_busy:
2937
2938 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002939 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05002940 return IOCB_SUCCESS;
2941 }
2942
2943 return IOCB_BUSY;
2944}
2945
James Smart92d7f7b2007-06-17 19:56:38 -05002946
2947int
2948lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2949 struct lpfc_iocbq *piocb, uint32_t flag)
2950{
2951 unsigned long iflags;
2952 int rc;
2953
2954 spin_lock_irqsave(&phba->hbalock, iflags);
2955 rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
2956 spin_unlock_irqrestore(&phba->hbalock, iflags);
2957
2958 return rc;
2959}
2960
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002961static int
2962lpfc_extra_ring_setup( struct lpfc_hba *phba)
2963{
2964 struct lpfc_sli *psli;
2965 struct lpfc_sli_ring *pring;
2966
2967 psli = &phba->sli;
2968
2969 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05002970
2971 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002972 pring = &psli->ring[psli->fcp_ring];
2973 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2974 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2975 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2976 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2977
James Smarta4bc3372006-12-02 13:34:16 -05002978 /* and give them to the extra ring */
2979 pring = &psli->ring[psli->extra_ring];
2980
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002981 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2982 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2983 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2984 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2985
2986 /* Setup default profile for this ring */
2987 pring->iotag_max = 4096;
2988 pring->num_mask = 1;
2989 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05002990 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
2991 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002992 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
2993 return 0;
2994}
2995
dea31012005-04-17 16:05:31 -05002996int
2997lpfc_sli_setup(struct lpfc_hba *phba)
2998{
James Smarted957682007-06-17 19:56:37 -05002999 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05003000 struct lpfc_sli *psli = &phba->sli;
3001 struct lpfc_sli_ring *pring;
3002
3003 psli->num_rings = MAX_CONFIGURED_RINGS;
3004 psli->sli_flag = 0;
3005 psli->fcp_ring = LPFC_FCP_RING;
3006 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05003007 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05003008
James Bottomley604a3e32005-10-29 10:28:33 -05003009 psli->iocbq_lookup = NULL;
3010 psli->iocbq_lookup_len = 0;
3011 psli->last_iotag = 0;
3012
dea31012005-04-17 16:05:31 -05003013 for (i = 0; i < psli->num_rings; i++) {
3014 pring = &psli->ring[i];
3015 switch (i) {
3016 case LPFC_FCP_RING: /* ring 0 - FCP */
3017 /* numCiocb and numRiocb are used in config_port */
3018 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3019 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3020 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3021 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3022 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3023 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003024 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003025 SLI3_IOCB_CMD_SIZE :
3026 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003027 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003028 SLI3_IOCB_RSP_SIZE :
3029 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003030 pring->iotag_ctr = 0;
3031 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05003032 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05003033 pring->fast_iotag = pring->iotag_max;
3034 pring->num_mask = 0;
3035 break;
James Smarta4bc3372006-12-02 13:34:16 -05003036 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05003037 /* numCiocb and numRiocb are used in config_port */
3038 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
3039 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003040 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003041 SLI3_IOCB_CMD_SIZE :
3042 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003043 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003044 SLI3_IOCB_RSP_SIZE :
3045 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05003046 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05003047 pring->num_mask = 0;
3048 break;
3049 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
3050 /* numCiocb and numRiocb are used in config_port */
3051 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
3052 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003053 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003054 SLI3_IOCB_CMD_SIZE :
3055 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003056 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003057 SLI3_IOCB_RSP_SIZE :
3058 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003059 pring->fast_iotag = 0;
3060 pring->iotag_ctr = 0;
3061 pring->iotag_max = 4096;
3062 pring->num_mask = 4;
3063 pring->prt[0].profile = 0; /* Mask 0 */
3064 pring->prt[0].rctl = FC_ELS_REQ;
3065 pring->prt[0].type = FC_ELS_DATA;
3066 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003067 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003068 pring->prt[1].profile = 0; /* Mask 1 */
3069 pring->prt[1].rctl = FC_ELS_RSP;
3070 pring->prt[1].type = FC_ELS_DATA;
3071 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003072 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003073 pring->prt[2].profile = 0; /* Mask 2 */
3074 /* NameServer Inquiry */
3075 pring->prt[2].rctl = FC_UNSOL_CTL;
3076 /* NameServer */
3077 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
3078 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003079 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003080 pring->prt[3].profile = 0; /* Mask 3 */
3081 /* NameServer response */
3082 pring->prt[3].rctl = FC_SOL_CTL;
3083 /* NameServer */
3084 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
3085 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003086 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003087 break;
3088 }
James Smarted957682007-06-17 19:56:37 -05003089 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05003090 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05003091 }
James Smarted957682007-06-17 19:56:37 -05003092 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05003093 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04003094 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
3095 "SLI2 SLIM Data: x%x x%lx\n",
3096 phba->brd_no, totiocbsize,
3097 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05003098 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003099 if (phba->cfg_multi_ring_support == 2)
3100 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05003101
3102 return 0;
3103}
3104
3105int
James Smart2e0fef82007-06-17 19:56:36 -05003106lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003107{
3108 struct lpfc_sli *psli;
3109 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05003110 int i;
dea31012005-04-17 16:05:31 -05003111
3112 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003113 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003114 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05003115 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05003116 /* Initialize list headers for txq and txcmplq as double linked lists */
3117 for (i = 0; i < psli->num_rings; i++) {
3118 pring = &psli->ring[i];
3119 pring->ringno = i;
3120 pring->next_cmdidx = 0;
3121 pring->local_getidx = 0;
3122 pring->cmdidx = 0;
3123 INIT_LIST_HEAD(&pring->txq);
3124 INIT_LIST_HEAD(&pring->txcmplq);
3125 INIT_LIST_HEAD(&pring->iocb_continueq);
3126 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05003127 }
James Smart2e0fef82007-06-17 19:56:36 -05003128 spin_unlock_irq(&phba->hbalock);
3129 return 1;
dea31012005-04-17 16:05:31 -05003130}
3131
3132int
James Smart92d7f7b2007-06-17 19:56:38 -05003133lpfc_sli_host_down(struct lpfc_vport *vport)
3134{
James Smart858c9f62007-06-17 19:56:39 -05003135 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05003136 struct lpfc_hba *phba = vport->phba;
3137 struct lpfc_sli *psli = &phba->sli;
3138 struct lpfc_sli_ring *pring;
3139 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003140 int i;
3141 unsigned long flags = 0;
3142 uint16_t prev_pring_flag;
3143
3144 lpfc_cleanup_discovery_resources(vport);
3145
3146 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05003147 for (i = 0; i < psli->num_rings; i++) {
3148 pring = &psli->ring[i];
3149 prev_pring_flag = pring->flag;
James Smart858c9f62007-06-17 19:56:39 -05003150 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
3151 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05003152 /*
3153 * Error everything on the txq since these iocbs have not been
3154 * given to the FW yet.
3155 */
James Smart92d7f7b2007-06-17 19:56:38 -05003156 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3157 if (iocb->vport != vport)
3158 continue;
James Smart858c9f62007-06-17 19:56:39 -05003159 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05003160 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05003161 }
3162
3163 /* Next issue ABTS for everything on the txcmplq */
3164 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
3165 list) {
3166 if (iocb->vport != vport)
3167 continue;
3168 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3169 }
3170
3171 pring->flag = prev_pring_flag;
3172 }
3173
3174 spin_unlock_irqrestore(&phba->hbalock, flags);
3175
James Smart858c9f62007-06-17 19:56:39 -05003176 while (!list_empty(&completions)) {
3177 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
3178
3179 if (!iocb->iocb_cmpl)
3180 lpfc_sli_release_iocbq(phba, iocb);
3181 else {
3182 iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3183 iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
3184 (iocb->iocb_cmpl) (phba, iocb, iocb);
3185 }
3186 }
James Smart92d7f7b2007-06-17 19:56:38 -05003187 return 1;
3188}
3189
3190int
James Smart2e0fef82007-06-17 19:56:36 -05003191lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003192{
James Smart2534ba72007-04-25 09:52:20 -04003193 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05003194 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003195 struct lpfc_sli_ring *pring;
3196 LPFC_MBOXQ_t *pmb;
James Smart2534ba72007-04-25 09:52:20 -04003197 struct lpfc_iocbq *iocb;
3198 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05003199 int i;
3200 unsigned long flags = 0;
3201
dea31012005-04-17 16:05:31 -05003202 lpfc_hba_down_prep(phba);
3203
James Smart92d7f7b2007-06-17 19:56:38 -05003204 lpfc_fabric_abort_hba(phba);
3205
James Smart2e0fef82007-06-17 19:56:36 -05003206 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05003207 for (i = 0; i < psli->num_rings; i++) {
3208 pring = &psli->ring[i];
James Smart858c9f62007-06-17 19:56:39 -05003209 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
3210 pring->flag |= LPFC_DEFERRED_RING_EVENT;
dea31012005-04-17 16:05:31 -05003211
3212 /*
3213 * Error everything on the txq since these iocbs have not been
3214 * given to the FW yet.
3215 */
James Smart2534ba72007-04-25 09:52:20 -04003216 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003217 pring->txq_cnt = 0;
3218
dea31012005-04-17 16:05:31 -05003219 }
James Smart2e0fef82007-06-17 19:56:36 -05003220 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05003221
James Smart2534ba72007-04-25 09:52:20 -04003222 while (!list_empty(&completions)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003223 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
James Smart2534ba72007-04-25 09:52:20 -04003224 cmd = &iocb->iocb;
James Smart2534ba72007-04-25 09:52:20 -04003225
James Smart2e0fef82007-06-17 19:56:36 -05003226 if (!iocb->iocb_cmpl)
3227 lpfc_sli_release_iocbq(phba, iocb);
3228 else {
James Smart2534ba72007-04-25 09:52:20 -04003229 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3230 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
3231 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05003232 }
James Smart2534ba72007-04-25 09:52:20 -04003233 }
3234
dea31012005-04-17 16:05:31 -05003235 /* Return any active mbox cmds */
3236 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05003237 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05003238
3239 spin_lock(&phba->pport->work_port_lock);
3240 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3241 spin_unlock(&phba->pport->work_port_lock);
3242
3243 if (psli->mbox_active) {
3244 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05003245 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003246 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05003247 }
dea31012005-04-17 16:05:31 -05003248
James Smart92d7f7b2007-06-17 19:56:38 -05003249 /* Return any pending or completed mbox cmds */
3250 list_splice_init(&phba->sli.mboxq, &completions);
3251 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
3252 INIT_LIST_HEAD(&psli->mboxq);
3253 INIT_LIST_HEAD(&psli->mboxq_cmpl);
3254
3255 spin_unlock_irqrestore(&phba->hbalock, flags);
3256
3257 while (!list_empty(&completions)) {
3258 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05003259 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
3260 if (pmb->mbox_cmpl) {
dea31012005-04-17 16:05:31 -05003261 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05003262 }
3263 }
dea31012005-04-17 16:05:31 -05003264 return 1;
3265}
3266
3267void
3268lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
3269{
3270 uint32_t *src = srcp;
3271 uint32_t *dest = destp;
3272 uint32_t ldata;
3273 int i;
3274
3275 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
3276 ldata = *src;
3277 ldata = le32_to_cpu(ldata);
3278 *dest = ldata;
3279 src++;
3280 dest++;
3281 }
3282}
3283
3284int
James Smart2e0fef82007-06-17 19:56:36 -05003285lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3286 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05003287{
3288 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
3289 later */
James Smart2e0fef82007-06-17 19:56:36 -05003290 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003291 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05003292 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05003293 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003294 return 0;
3295}
3296
3297
3298struct lpfc_dmabuf *
3299lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3300 dma_addr_t phys)
3301{
3302 struct lpfc_dmabuf *mp, *next_mp;
3303 struct list_head *slp = &pring->postbufq;
3304
3305 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05003306 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003307 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
3308 if (mp->phys == phys) {
3309 list_del_init(&mp->list);
3310 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05003311 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003312 return mp;
3313 }
3314 }
3315
James Smart2e0fef82007-06-17 19:56:36 -05003316 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003318 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05003319 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003320 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05003321 slp->next, slp->prev, pring->postbufq_cnt);
3322 return NULL;
3323}
3324
3325static void
James Smart2e0fef82007-06-17 19:56:36 -05003326lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3327 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05003328{
James Smart2e0fef82007-06-17 19:56:36 -05003329 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04003330 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05003331 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04003332 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3333
3334 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04003335
3336 if (irsp->ulpStatus) {
3337 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
3338 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
3339
James Smart2e0fef82007-06-17 19:56:36 -05003340 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04003341 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
3342 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
3343
James Smart92d7f7b2007-06-17 19:56:38 -05003344 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003345 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05003346 "with tag %x context %x, abort status %x, "
3347 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04003348 abort_iocb, abort_iotag, abort_context,
3349 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04003350
3351 /*
3352 * make sure we have the right iocbq before taking it
3353 * off the txcmplq and try to call completion routine.
3354 */
James Smart2e0fef82007-06-17 19:56:36 -05003355 if (!abort_iocb ||
3356 abort_iocb->iocb.ulpContext != abort_context ||
3357 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
3358 spin_unlock_irq(&phba->hbalock);
3359 else {
James Smart92d7f7b2007-06-17 19:56:38 -05003360 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04003361 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05003362 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04003363
James Smart92d7f7b2007-06-17 19:56:38 -05003364 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3365 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3366 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
3367 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04003368 }
3369 }
3370
James Bottomley604a3e32005-10-29 10:28:33 -05003371 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05003372 return;
3373}
3374
James Smart92d7f7b2007-06-17 19:56:38 -05003375static void
3376lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3377 struct lpfc_iocbq *rspiocb)
3378{
3379 IOCB_t *irsp = &rspiocb->iocb;
3380
3381 /* ELS cmd tag <ulpIoTag> completes */
3382 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smarte8b62012007-08-02 11:10:09 -04003383 "0133 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003384 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003385 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05003386 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05003387 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
3388 lpfc_ct_free_iocb(phba, cmdiocb);
3389 else
3390 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05003391 return;
3392}
3393
dea31012005-04-17 16:05:31 -05003394int
James Smart2e0fef82007-06-17 19:56:36 -05003395lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3396 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05003397{
James Smart2e0fef82007-06-17 19:56:36 -05003398 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003399 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05003400 IOCB_t *icmd = NULL;
3401 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04003402 int retval = IOCB_ERROR;
3403
James Smart92d7f7b2007-06-17 19:56:38 -05003404 /*
3405 * There are certain command types we don't want to abort. And we
3406 * don't want to abort commands that are already in the process of
3407 * being aborted.
James Smart07951072007-04-25 09:51:38 -04003408 */
3409 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05003410 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05003411 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3412 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04003413 return 0;
3414
James Smart858c9f62007-06-17 19:56:39 -05003415 /* If we're unloading, don't abort iocb on the ELS ring, but change the
3416 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04003417 */
James Smart858c9f62007-06-17 19:56:39 -05003418 if ((vport->load_flag & FC_UNLOADING) &&
3419 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003420 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
3421 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
3422 else
3423 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04003424 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05003425 }
dea31012005-04-17 16:05:31 -05003426
3427 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05003428 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003429 if (abtsiocbp == NULL)
3430 return 0;
dea31012005-04-17 16:05:31 -05003431
James Smart07951072007-04-25 09:51:38 -04003432 /* This signals the response to set the correct status
3433 * before calling the completion handler.
3434 */
3435 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
3436
dea31012005-04-17 16:05:31 -05003437 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04003438 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
3439 iabt->un.acxri.abortContextTag = icmd->ulpContext;
3440 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003441 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04003442 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05003443
James Smart2e0fef82007-06-17 19:56:36 -05003444 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04003445 iabt->ulpCommand = CMD_ABORT_XRI_CN;
3446 else
3447 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
3448
3449 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04003450
James Smarte8b62012007-08-02 11:10:09 -04003451 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3452 "0339 Abort xri x%x, original iotag x%x, "
3453 "abort cmd iotag x%x\n",
3454 iabt->un.acxri.abortContextTag,
3455 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05003456 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04003457
3458abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05003459 /*
3460 * Caller to this routine should check for IOCB_ERROR
3461 * and handle it properly. This routine no longer removes
3462 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04003463 */
James Smart2e0fef82007-06-17 19:56:36 -05003464 return retval;
dea31012005-04-17 16:05:31 -05003465}
3466
3467static int
James Smart51ef4c22007-08-02 11:10:31 -04003468lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
3469 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003470 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05003471{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003472 struct lpfc_scsi_buf *lpfc_cmd;
3473 struct scsi_cmnd *cmnd;
dea31012005-04-17 16:05:31 -05003474 int rc = 1;
3475
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003476 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
3477 return rc;
3478
James Smart51ef4c22007-08-02 11:10:31 -04003479 if (iocbq->vport != vport)
3480 return rc;
3481
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003482 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
3483 cmnd = lpfc_cmd->pCmd;
3484
3485 if (cmnd == NULL)
dea31012005-04-17 16:05:31 -05003486 return rc;
3487
3488 switch (ctx_cmd) {
3489 case LPFC_CTX_LUN:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003490 if ((cmnd->device->id == tgt_id) &&
3491 (cmnd->device->lun == lun_id))
dea31012005-04-17 16:05:31 -05003492 rc = 0;
3493 break;
3494 case LPFC_CTX_TGT:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003495 if (cmnd->device->id == tgt_id)
dea31012005-04-17 16:05:31 -05003496 rc = 0;
3497 break;
dea31012005-04-17 16:05:31 -05003498 case LPFC_CTX_HOST:
3499 rc = 0;
3500 break;
3501 default:
3502 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
3503 __FUNCTION__, ctx_cmd);
3504 break;
3505 }
3506
3507 return rc;
3508}
3509
3510int
James Smart51ef4c22007-08-02 11:10:31 -04003511lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
3512 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05003513{
James Smart51ef4c22007-08-02 11:10:31 -04003514 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003515 struct lpfc_iocbq *iocbq;
3516 int sum, i;
dea31012005-04-17 16:05:31 -05003517
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003518 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
3519 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05003520
James Smart51ef4c22007-08-02 11:10:31 -04003521 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
3522 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003523 sum++;
dea31012005-04-17 16:05:31 -05003524 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003525
dea31012005-04-17 16:05:31 -05003526 return sum;
3527}
3528
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003529void
James Smart2e0fef82007-06-17 19:56:36 -05003530lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3531 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003532{
James Bottomley604a3e32005-10-29 10:28:33 -05003533 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003534 return;
3535}
3536
dea31012005-04-17 16:05:31 -05003537int
James Smart51ef4c22007-08-02 11:10:31 -04003538lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
3539 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05003540{
James Smart51ef4c22007-08-02 11:10:31 -04003541 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003542 struct lpfc_iocbq *iocbq;
3543 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05003544 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05003545 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003546 int i;
dea31012005-04-17 16:05:31 -05003547
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003548 for (i = 1; i <= phba->sli.last_iotag; i++) {
3549 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05003550
James Smart51ef4c22007-08-02 11:10:31 -04003551 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05003552 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05003553 continue;
3554
3555 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003556 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003557 if (abtsiocb == NULL) {
3558 errcnt++;
3559 continue;
3560 }
dea31012005-04-17 16:05:31 -05003561
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003562 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05003563 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
3564 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
3565 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
3566 abtsiocb->iocb.ulpLe = 1;
3567 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05003568 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05003569
James Smart2e0fef82007-06-17 19:56:36 -05003570 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05003571 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
3572 else
3573 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
3574
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04003575 /* Setup callback routine and issue the command. */
3576 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05003577 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
3578 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05003579 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05003580 errcnt++;
3581 continue;
3582 }
3583 }
3584
3585 return errcnt;
3586}
3587
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003588static void
3589lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
3590 struct lpfc_iocbq *cmdiocbq,
3591 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05003592{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003593 wait_queue_head_t *pdone_q;
3594 unsigned long iflags;
dea31012005-04-17 16:05:31 -05003595
James Smart2e0fef82007-06-17 19:56:36 -05003596 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003597 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
3598 if (cmdiocbq->context2 && rspiocbq)
3599 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
3600 &rspiocbq->iocb, sizeof(IOCB_t));
3601
3602 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003603 if (pdone_q)
3604 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05003605 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05003606 return;
3607}
3608
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003609/*
3610 * Issue the caller's iocb and wait for its completion, but no longer than the
3611 * caller's timeout. Note that iocb_flags is cleared before the
3612 * lpfc_sli_issue_call since the wake routine sets a unique value and by
3613 * definition this is a wait function.
3614 */
James Smart92d7f7b2007-06-17 19:56:38 -05003615
dea31012005-04-17 16:05:31 -05003616int
James Smart2e0fef82007-06-17 19:56:36 -05003617lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
3618 struct lpfc_sli_ring *pring,
3619 struct lpfc_iocbq *piocb,
3620 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003621 uint32_t timeout)
dea31012005-04-17 16:05:31 -05003622{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08003623 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003624 long timeleft, timeout_req = 0;
3625 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003626 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05003627
3628 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003629 * If the caller has provided a response iocbq buffer, then context2
3630 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05003631 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003632 if (prspiocbq) {
3633 if (piocb->context2)
3634 return IOCB_ERROR;
3635 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05003636 }
3637
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003638 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
3639 piocb->context_un.wait_queue = &done_q;
3640 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05003641
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003642 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3643 creg_val = readl(phba->HCregaddr);
3644 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
3645 writel(creg_val, phba->HCregaddr);
3646 readl(phba->HCregaddr); /* flush */
3647 }
3648
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003649 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
3650 if (retval == IOCB_SUCCESS) {
3651 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003652 timeleft = wait_event_timeout(done_q,
3653 piocb->iocb_flag & LPFC_IO_WAKE,
3654 timeout_req);
dea31012005-04-17 16:05:31 -05003655
James Smart7054a602007-04-25 09:52:34 -04003656 if (piocb->iocb_flag & LPFC_IO_WAKE) {
3657 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003658 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04003659 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003660 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003661 "0338 IOCB wait timeout error - no "
3662 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003663 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04003664 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003665 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003666 "0330 IOCB wake NOT set, "
3667 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003668 timeout, (timeleft / jiffies));
3669 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05003670 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003671 } else {
3672 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003673 ":0332 IOCB wait issue failed, Data x%x\n",
3674 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003675 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05003676 }
3677
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003678 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3679 creg_val = readl(phba->HCregaddr);
3680 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
3681 writel(creg_val, phba->HCregaddr);
3682 readl(phba->HCregaddr); /* flush */
3683 }
3684
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003685 if (prspiocbq)
3686 piocb->context2 = NULL;
3687
3688 piocb->context_un.wait_queue = NULL;
3689 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05003690 return retval;
3691}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003692
dea31012005-04-17 16:05:31 -05003693int
James Smart2e0fef82007-06-17 19:56:36 -05003694lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05003695 uint32_t timeout)
3696{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08003697 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05003698 int retval;
James Smart858c9f62007-06-17 19:56:39 -05003699 unsigned long flag;
dea31012005-04-17 16:05:31 -05003700
3701 /* The caller must leave context1 empty. */
James Smart92d7f7b2007-06-17 19:56:38 -05003702 if (pmboxq->context1 != 0)
James Smart2e0fef82007-06-17 19:56:36 -05003703 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003704
3705 /* setup wake call as IOCB callback */
3706 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
3707 /* setup context field to pass wait_queue pointer to wake function */
3708 pmboxq->context1 = &done_q;
3709
dea31012005-04-17 16:05:31 -05003710 /* now issue the command */
3711 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3712
3713 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04003714 wait_event_interruptible_timeout(done_q,
3715 pmboxq->mbox_flag & LPFC_MBX_WAKE,
3716 timeout * HZ);
3717
James Smart858c9f62007-06-17 19:56:39 -05003718 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05003719 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04003720 /*
3721 * if LPFC_MBX_WAKE flag is set the mailbox is completed
3722 * else do not free the resources.
3723 */
3724 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05003725 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05003726 else {
James Smart7054a602007-04-25 09:52:34 -04003727 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05003728 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3729 }
3730 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05003731 }
3732
dea31012005-04-17 16:05:31 -05003733 return retval;
3734}
3735
James Smartb4c02652006-07-06 15:50:43 -04003736int
3737lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
3738{
James Smart2e0fef82007-06-17 19:56:36 -05003739 struct lpfc_vport *vport = phba->pport;
James Smartb4c02652006-07-06 15:50:43 -04003740 int i = 0;
James Smarted957682007-06-17 19:56:37 -05003741 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04003742
James Smart2e0fef82007-06-17 19:56:36 -05003743 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
James Smartb4c02652006-07-06 15:50:43 -04003744 if (i++ > LPFC_MBOX_TMO * 1000)
3745 return 1;
3746
James Smarted957682007-06-17 19:56:37 -05003747 /*
3748 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
3749 * did finish. This way we won't get the misleading
3750 * "Stray Mailbox Interrupt" message.
3751 */
3752 spin_lock_irq(&phba->hbalock);
3753 ha_copy = phba->work_ha;
3754 phba->work_ha &= ~HA_MBATT;
3755 spin_unlock_irq(&phba->hbalock);
3756
3757 if (ha_copy & HA_MBATT)
3758 if (lpfc_sli_handle_mb_event(phba) == 0)
3759 i = 0;
James Smartb4c02652006-07-06 15:50:43 -04003760
3761 msleep(1);
3762 }
3763
3764 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
3765}
3766
dea31012005-04-17 16:05:31 -05003767irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01003768lpfc_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05003769{
James Smart2e0fef82007-06-17 19:56:36 -05003770 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05003771 uint32_t ha_copy;
3772 uint32_t work_ha_copy;
3773 unsigned long status;
3774 int i;
3775 uint32_t control;
3776
James Smart92d7f7b2007-06-17 19:56:38 -05003777 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05003778 struct lpfc_vport *vport;
3779 struct lpfc_nodelist *ndlp;
3780 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05003781 LPFC_MBOXQ_t *pmb;
3782 int rc;
3783
dea31012005-04-17 16:05:31 -05003784 /*
3785 * Get the driver's phba structure from the dev_id and
3786 * assume the HBA is not interrupting.
3787 */
3788 phba = (struct lpfc_hba *) dev_id;
3789
3790 if (unlikely(!phba))
3791 return IRQ_NONE;
3792
Linas Vepstas8d63f372007-02-14 14:28:36 -06003793 /* If the pci channel is offline, ignore all the interrupts. */
3794 if (unlikely(pci_channel_offline(phba->pcidev)))
3795 return IRQ_NONE;
3796
dea31012005-04-17 16:05:31 -05003797 phba->sli.slistat.sli_intr++;
3798
3799 /*
3800 * Call the HBA to see if it is interrupting. If not, don't claim
3801 * the interrupt
3802 */
3803
3804 /* Ignore all interrupts during initialization. */
James Smart2e0fef82007-06-17 19:56:36 -05003805 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003806 return IRQ_NONE;
3807
3808 /*
3809 * Read host attention register to determine interrupt source
3810 * Clear Attention Sources, except Error Attention (to
3811 * preserve status) and Link Attention
3812 */
James Smart2e0fef82007-06-17 19:56:36 -05003813 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003814 ha_copy = readl(phba->HAregaddr);
James Smartebdbe652007-04-25 09:53:15 -04003815 /* If somebody is waiting to handle an eratt don't process it
3816 * here. The brdkill function will do this.
3817 */
James Smart2e0fef82007-06-17 19:56:36 -05003818 if (phba->link_flag & LS_IGNORE_ERATT)
James Smartebdbe652007-04-25 09:53:15 -04003819 ha_copy &= ~HA_ERATT;
dea31012005-04-17 16:05:31 -05003820 writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
3821 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003822 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003823
3824 if (unlikely(!ha_copy))
3825 return IRQ_NONE;
3826
3827 work_ha_copy = ha_copy & phba->work_ha_mask;
3828
3829 if (unlikely(work_ha_copy)) {
3830 if (work_ha_copy & HA_LATT) {
3831 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
3832 /*
3833 * Turn off Link Attention interrupts
3834 * until CLEAR_LA done
3835 */
James Smart2e0fef82007-06-17 19:56:36 -05003836 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003837 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
3838 control = readl(phba->HCregaddr);
3839 control &= ~HC_LAINT_ENA;
3840 writel(control, phba->HCregaddr);
3841 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003842 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003843 }
3844 else
3845 work_ha_copy &= ~HA_LATT;
3846 }
3847
3848 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05003849 /*
3850 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
3851 * the only slow ring.
3852 */
3853 status = (work_ha_copy &
3854 (HA_RXMASK << (4*LPFC_ELS_RING)));
3855 status >>= (4*LPFC_ELS_RING);
3856 if (status & HA_RXMASK) {
3857 spin_lock(&phba->hbalock);
3858 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04003859
3860 lpfc_debugfs_slow_ring_trc(phba,
3861 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
3862 control, status,
3863 (uint32_t)phba->sli.slistat.sli_intr);
3864
James Smart858c9f62007-06-17 19:56:39 -05003865 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04003866 lpfc_debugfs_slow_ring_trc(phba,
3867 "ISR Disable ring:"
3868 "pwork:x%x hawork:x%x wait:x%x",
3869 phba->work_ha, work_ha_copy,
3870 (uint32_t)((unsigned long)
3871 phba->work_wait));
3872
James Smart858c9f62007-06-17 19:56:39 -05003873 control &=
3874 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05003875 writel(control, phba->HCregaddr);
3876 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05003877 }
James Smarta58cbd52007-08-02 11:09:43 -04003878 else {
3879 lpfc_debugfs_slow_ring_trc(phba,
3880 "ISR slow ring: pwork:"
3881 "x%x hawork:x%x wait:x%x",
3882 phba->work_ha, work_ha_copy,
3883 (uint32_t)((unsigned long)
3884 phba->work_wait));
3885 }
James Smart858c9f62007-06-17 19:56:39 -05003886 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003887 }
3888 }
3889
3890 if (work_ha_copy & HA_ERATT) {
James Smart2e0fef82007-06-17 19:56:36 -05003891 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003892 /*
3893 * There was a link/board error. Read the
3894 * status register to retrieve the error event
3895 * and process it.
3896 */
3897 phba->sli.slistat.err_attn_event++;
3898 /* Save status info */
3899 phba->work_hs = readl(phba->HSregaddr);
3900 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
3901 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
3902
3903 /* Clear Chip error bit */
3904 writel(HA_ERATT, phba->HAregaddr);
3905 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003906 phba->pport->stopped = 1;
dea31012005-04-17 16:05:31 -05003907 }
3908
James Smart92d7f7b2007-06-17 19:56:38 -05003909 if ((work_ha_copy & HA_MBATT) &&
3910 (phba->sli.mbox_active)) {
3911 pmb = phba->sli.mbox_active;
3912 pmbox = &pmb->mb;
3913 mbox = &phba->slim2p->mbx;
James Smart858c9f62007-06-17 19:56:39 -05003914 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003915
3916 /* First check out the status word */
3917 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
3918 if (pmbox->mbxOwner != OWN_HOST) {
3919 /*
3920 * Stray Mailbox Interrupt, mbxCommand <cmd>
3921 * mbxStatus <status>
3922 */
3923 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX |
3924 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003925 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05003926 "Interrupt mbxCommand x%x "
3927 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003928 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05003929 pmbox->mbxCommand,
3930 pmbox->mbxStatus);
3931 }
James Smart858c9f62007-06-17 19:56:39 -05003932 phba->last_completion_time = jiffies;
James Smart92d7f7b2007-06-17 19:56:38 -05003933 del_timer_sync(&phba->sli.mbox_tmo);
3934
James Smart92d7f7b2007-06-17 19:56:38 -05003935 phba->sli.mbox_active = NULL;
3936 if (pmb->mbox_cmpl) {
3937 lpfc_sli_pcimem_bcopy(mbox, pmbox,
3938 MAILBOX_CMD_SIZE);
3939 }
James Smart858c9f62007-06-17 19:56:39 -05003940 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
3941 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
3942
3943 lpfc_debugfs_disc_trc(vport,
3944 LPFC_DISC_TRC_MBOX_VPORT,
3945 "MBOX dflt rpi: : status:x%x rpi:x%x",
3946 (uint32_t)pmbox->mbxStatus,
3947 pmbox->un.varWords[0], 0);
3948
3949 if ( !pmbox->mbxStatus) {
3950 mp = (struct lpfc_dmabuf *)
3951 (pmb->context1);
3952 ndlp = (struct lpfc_nodelist *)
3953 pmb->context2;
3954
3955 /* Reg_LOGIN of dflt RPI was successful.
3956 * new lets get rid of the RPI using the
3957 * same mbox buffer.
3958 */
3959 lpfc_unreg_login(phba, vport->vpi,
3960 pmbox->un.varWords[0], pmb);
3961 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3962 pmb->context1 = mp;
3963 pmb->context2 = ndlp;
3964 pmb->vport = vport;
3965 spin_lock(&phba->hbalock);
3966 phba->sli.sli_flag &=
3967 ~LPFC_SLI_MBOX_ACTIVE;
3968 spin_unlock(&phba->hbalock);
3969 goto send_current_mbox;
3970 }
3971 }
3972 spin_lock(&phba->pport->work_port_lock);
3973 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3974 spin_unlock(&phba->pport->work_port_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003975 lpfc_mbox_cmpl_put(phba, pmb);
3976 }
3977 if ((work_ha_copy & HA_MBATT) &&
3978 (phba->sli.mbox_active == NULL)) {
3979send_next_mbox:
3980 spin_lock(&phba->hbalock);
3981 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3982 pmb = lpfc_mbox_get(phba);
3983 spin_unlock(&phba->hbalock);
James Smart858c9f62007-06-17 19:56:39 -05003984send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05003985 /* Process next mailbox command if there is one */
3986 if (pmb != NULL) {
3987 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3988 if (rc == MBX_NOT_FINISHED) {
3989 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
3990 lpfc_mbox_cmpl_put(phba, pmb);
3991 goto send_next_mbox;
3992 }
3993 } else {
3994 /* Turn on IOCB processing */
3995 for (i = 0; i < phba->sli.num_rings; i++)
3996 lpfc_sli_turn_on_ring(phba, i);
3997 }
3998
3999 }
4000
James Smart2e0fef82007-06-17 19:56:36 -05004001 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004002 phba->work_ha |= work_ha_copy;
4003 if (phba->work_wait)
James Smart92d7f7b2007-06-17 19:56:38 -05004004 lpfc_worker_wake_up(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004005 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004006 }
4007
4008 ha_copy &= ~(phba->work_ha_mask);
4009
4010 /*
4011 * Process all events on FCP ring. Take the optimized path for
4012 * FCP IO. Any other IO is slow path and is handled by
4013 * the worker thread.
4014 */
4015 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
4016 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05004017 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05004018 lpfc_sli_handle_fast_ring_event(phba,
4019 &phba->sli.ring[LPFC_FCP_RING],
4020 status);
James Smarta4bc3372006-12-02 13:34:16 -05004021
4022 if (phba->cfg_multi_ring_support == 2) {
4023 /*
4024 * Process all events on extra ring. Take the optimized path
4025 * for extra ring IO. Any other IO is slow path and is handled
4026 * by the worker thread.
4027 */
4028 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
4029 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05004030 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05004031 lpfc_sli_handle_fast_ring_event(phba,
4032 &phba->sli.ring[LPFC_EXTRA_RING],
4033 status);
4034 }
4035 }
dea31012005-04-17 16:05:31 -05004036 return IRQ_HANDLED;
4037
4038} /* lpfc_intr_handler */