blob: 3cf488496d869583ea3bffffd9c9460d887e5cc2 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart1b32f6a2008-02-08 18:49:39 -05004 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040031#include <scsi/scsi_transport_fc.h>
dea31012005-04-17 16:05:31 -050032
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
35#include "lpfc_disc.h"
36#include "lpfc_scsi.h"
37#include "lpfc.h"
38#include "lpfc_crtn.h"
39#include "lpfc_logmsg.h"
40#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050041#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050042
43/*
44 * Define macro to log: Mailbox command x%x cannot issue Data
45 * This allows multiple uses of lpfc_msgBlk0311
46 * w/o perturbing log msg utility.
47 */
James Smart92d7f7b2007-06-17 19:56:38 -050048#define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
dea31012005-04-17 16:05:31 -050049 lpfc_printf_log(phba, \
50 KERN_INFO, \
51 LOG_MBOX | LOG_SLI, \
James Smarte8b62012007-08-02 11:10:09 -040052 "(%d):0311 Mailbox command x%x cannot " \
James Smart92d7f7b2007-06-17 19:56:38 -050053 "issue Data: x%x x%x x%x\n", \
James Smart92d7f7b2007-06-17 19:56:38 -050054 pmbox->vport ? pmbox->vport->vpi : 0, \
55 pmbox->mb.mbxCommand, \
James Smart2e0fef82007-06-17 19:56:36 -050056 phba->pport->port_state, \
dea31012005-04-17 16:05:31 -050057 psli->sli_flag, \
James Smart2e0fef82007-06-17 19:56:36 -050058 flag)
dea31012005-04-17 16:05:31 -050059
60
61/* There are only four IOCB completion types. */
62typedef enum _lpfc_iocb_type {
63 LPFC_UNKNOWN_IOCB,
64 LPFC_UNSOL_IOCB,
65 LPFC_SOL_IOCB,
66 LPFC_ABORT_IOCB
67} lpfc_iocb_type;
68
James Smarte59058c2008-08-24 21:49:00 -040069/**
70 * lpfc_cmd_iocb: Get next command iocb entry in the ring.
71 * @phba: Pointer to HBA context object.
72 * @pring: Pointer to driver SLI ring object.
73 *
74 * This function returns pointer to next command iocb entry
75 * in the command ring. The caller must hold hbalock to prevent
76 * other threads consume the next command iocb.
77 * SLI-2/SLI-3 provide different sized iocbs.
78 **/
James Smarted957682007-06-17 19:56:37 -050079static inline IOCB_t *
80lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
81{
82 return (IOCB_t *) (((char *) pring->cmdringaddr) +
83 pring->cmdidx * phba->iocb_cmd_size);
84}
85
James Smarte59058c2008-08-24 21:49:00 -040086/**
87 * lpfc_resp_iocb: Get next response iocb entry in the ring.
88 * @phba: Pointer to HBA context object.
89 * @pring: Pointer to driver SLI ring object.
90 *
91 * This function returns pointer to next response iocb entry
92 * in the response ring. The caller must hold hbalock to make sure
93 * that no other thread consume the next response iocb.
94 * SLI-2/SLI-3 provide different sized iocbs.
95 **/
James Smarted957682007-06-17 19:56:37 -050096static inline IOCB_t *
97lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
98{
99 return (IOCB_t *) (((char *) pring->rspringaddr) +
100 pring->rspidx * phba->iocb_rsp_size);
101}
102
James Smarte59058c2008-08-24 21:49:00 -0400103/**
104 * __lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
105 * @phba: Pointer to HBA context object.
106 *
107 * This function is called with hbalock held. This function
108 * allocates a new driver iocb object from the iocb pool. If the
109 * allocation is successful, it returns pointer to the newly
110 * allocated iocb object else it returns NULL.
111 **/
James Smart2e0fef82007-06-17 19:56:36 -0500112static struct lpfc_iocbq *
113__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400114{
115 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
116 struct lpfc_iocbq * iocbq = NULL;
117
118 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
119 return iocbq;
120}
121
James Smarte59058c2008-08-24 21:49:00 -0400122/**
123 * lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
124 * @phba: Pointer to HBA context object.
125 *
126 * This function is called with no lock held. This function
127 * allocates a new driver iocb object from the iocb pool. If the
128 * allocation is successful, it returns pointer to the newly
129 * allocated iocb object else it returns NULL.
130 **/
James Smart2e0fef82007-06-17 19:56:36 -0500131struct lpfc_iocbq *
132lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500133{
James Smart2e0fef82007-06-17 19:56:36 -0500134 struct lpfc_iocbq * iocbq = NULL;
135 unsigned long iflags;
136
137 spin_lock_irqsave(&phba->hbalock, iflags);
138 iocbq = __lpfc_sli_get_iocbq(phba);
139 spin_unlock_irqrestore(&phba->hbalock, iflags);
140 return iocbq;
141}
142
James Smarte59058c2008-08-24 21:49:00 -0400143/**
144 * __lpfc_sli_release_iocbq: Release iocb to the iocb pool.
145 * @phba: Pointer to HBA context object.
146 * @iocbq: Pointer to driver iocb object.
147 *
148 * This function is called with hbalock held to release driver
149 * iocb object to the iocb pool. The iotag in the iocb object
150 * does not change for each use of the iocb object. This function
151 * clears all other fields of the iocb object when it is freed.
152 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100153static void
James Smart2e0fef82007-06-17 19:56:36 -0500154__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
155{
156 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500157
158 /*
159 * Clean all volatile data fields, preserve iotag and node struct.
160 */
161 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
162 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
163}
164
James Smarte59058c2008-08-24 21:49:00 -0400165/**
166 * lpfc_sli_release_iocbq: Release iocb to the iocb pool.
167 * @phba: Pointer to HBA context object.
168 * @iocbq: Pointer to driver iocb object.
169 *
170 * This function is called with no lock held to release the iocb to
171 * iocb pool.
172 **/
James Smart2e0fef82007-06-17 19:56:36 -0500173void
174lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
175{
176 unsigned long iflags;
177
178 /*
179 * Clean all volatile data fields, preserve iotag and node struct.
180 */
181 spin_lock_irqsave(&phba->hbalock, iflags);
182 __lpfc_sli_release_iocbq(phba, iocbq);
183 spin_unlock_irqrestore(&phba->hbalock, iflags);
184}
185
James Smarte59058c2008-08-24 21:49:00 -0400186/**
187 * lpfc_sli_iocb_cmd_type: Get the iocb type.
188 * @iocb_cmnd : iocb command code.
189 *
190 * This function is called by ring event handler function to get the iocb type.
191 * This function translates the iocb command to an iocb command type used to
192 * decide the final disposition of each completed IOCB.
193 * The function returns
194 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
195 * LPFC_SOL_IOCB if it is a solicited iocb completion
196 * LPFC_ABORT_IOCB if it is an abort iocb
197 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
198 *
199 * The caller is not required to hold any lock.
200 **/
dea31012005-04-17 16:05:31 -0500201static lpfc_iocb_type
202lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
203{
204 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
205
206 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
207 return 0;
208
209 switch (iocb_cmnd) {
210 case CMD_XMIT_SEQUENCE_CR:
211 case CMD_XMIT_SEQUENCE_CX:
212 case CMD_XMIT_BCAST_CN:
213 case CMD_XMIT_BCAST_CX:
214 case CMD_ELS_REQUEST_CR:
215 case CMD_ELS_REQUEST_CX:
216 case CMD_CREATE_XRI_CR:
217 case CMD_CREATE_XRI_CX:
218 case CMD_GET_RPI_CN:
219 case CMD_XMIT_ELS_RSP_CX:
220 case CMD_GET_RPI_CR:
221 case CMD_FCP_IWRITE_CR:
222 case CMD_FCP_IWRITE_CX:
223 case CMD_FCP_IREAD_CR:
224 case CMD_FCP_IREAD_CX:
225 case CMD_FCP_ICMND_CR:
226 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500227 case CMD_FCP_TSEND_CX:
228 case CMD_FCP_TRSP_CX:
229 case CMD_FCP_TRECEIVE_CX:
230 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500231 case CMD_ADAPTER_MSG:
232 case CMD_ADAPTER_DUMP:
233 case CMD_XMIT_SEQUENCE64_CR:
234 case CMD_XMIT_SEQUENCE64_CX:
235 case CMD_XMIT_BCAST64_CN:
236 case CMD_XMIT_BCAST64_CX:
237 case CMD_ELS_REQUEST64_CR:
238 case CMD_ELS_REQUEST64_CX:
239 case CMD_FCP_IWRITE64_CR:
240 case CMD_FCP_IWRITE64_CX:
241 case CMD_FCP_IREAD64_CR:
242 case CMD_FCP_IREAD64_CX:
243 case CMD_FCP_ICMND64_CR:
244 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500245 case CMD_FCP_TSEND64_CX:
246 case CMD_FCP_TRSP64_CX:
247 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500248 case CMD_GEN_REQUEST64_CR:
249 case CMD_GEN_REQUEST64_CX:
250 case CMD_XMIT_ELS_RSP64_CX:
251 type = LPFC_SOL_IOCB;
252 break;
253 case CMD_ABORT_XRI_CN:
254 case CMD_ABORT_XRI_CX:
255 case CMD_CLOSE_XRI_CN:
256 case CMD_CLOSE_XRI_CX:
257 case CMD_XRI_ABORTED_CX:
258 case CMD_ABORT_MXRI64_CN:
259 type = LPFC_ABORT_IOCB;
260 break;
261 case CMD_RCV_SEQUENCE_CX:
262 case CMD_RCV_ELS_REQ_CX:
263 case CMD_RCV_SEQUENCE64_CX:
264 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400265 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500266 case CMD_IOCB_RCV_SEQ64_CX:
267 case CMD_IOCB_RCV_ELS64_CX:
268 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500269 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500270 type = LPFC_UNSOL_IOCB;
271 break;
James Smart3163f722008-02-08 18:50:25 -0500272 case CMD_IOCB_XMIT_MSEQ64_CR:
273 case CMD_IOCB_XMIT_MSEQ64_CX:
274 case CMD_IOCB_RCV_SEQ_LIST64_CX:
275 case CMD_IOCB_RCV_ELS_LIST64_CX:
276 case CMD_IOCB_CLOSE_EXTENDED_CN:
277 case CMD_IOCB_ABORT_EXTENDED_CN:
278 case CMD_IOCB_RET_HBQE64_CN:
279 case CMD_IOCB_FCP_IBIDIR64_CR:
280 case CMD_IOCB_FCP_IBIDIR64_CX:
281 case CMD_IOCB_FCP_ITASKMGT64_CX:
282 case CMD_IOCB_LOGENTRY_CN:
283 case CMD_IOCB_LOGENTRY_ASYNC_CN:
284 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700285 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500286 type = LPFC_UNKNOWN_IOCB;
287 break;
dea31012005-04-17 16:05:31 -0500288 default:
289 type = LPFC_UNKNOWN_IOCB;
290 break;
291 }
292
293 return type;
294}
295
James Smarte59058c2008-08-24 21:49:00 -0400296/**
297 * lpfc_sli_ring_map: Issue config_ring mbox for all rings.
298 * @phba: Pointer to HBA context object.
299 *
300 * This function is called from SLI initialization code
301 * to configure every ring of the HBA's SLI interface. The
302 * caller is not required to hold any lock. This function issues
303 * a config_ring mailbox command for each ring.
304 * This function returns zero if successful else returns a negative
305 * error code.
306 **/
dea31012005-04-17 16:05:31 -0500307static int
James Smarted957682007-06-17 19:56:37 -0500308lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500309{
310 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500311 LPFC_MBOXQ_t *pmb;
312 MAILBOX_t *pmbox;
313 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500314
James Smarted957682007-06-17 19:56:37 -0500315 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
316 if (!pmb)
317 return -ENOMEM;
318 pmbox = &pmb->mb;
319 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500320 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500321 lpfc_config_ring(phba, i, pmb);
322 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
323 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400325 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500326 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
327 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400328 rc, pmbox->mbxCommand,
329 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500330 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500331 ret = -ENXIO;
332 break;
dea31012005-04-17 16:05:31 -0500333 }
334 }
James Smarted957682007-06-17 19:56:37 -0500335 mempool_free(pmb, phba->mbox_mem_pool);
336 return ret;
dea31012005-04-17 16:05:31 -0500337}
338
James Smarte59058c2008-08-24 21:49:00 -0400339/**
340 * lpfc_sli_ringtxcmpl_put: Adds new iocb to the txcmplq.
341 * @phba: Pointer to HBA context object.
342 * @pring: Pointer to driver SLI ring object.
343 * @piocb: Pointer to the driver iocb object.
344 *
345 * This function is called with hbalock held. The function adds the
346 * new iocb to txcmplq of the given ring. This function always returns
347 * 0. If this function is called for ELS ring, this function checks if
348 * there is a vport associated with the ELS command. This function also
349 * starts els_tmofunc timer if this is an ELS command.
350 **/
dea31012005-04-17 16:05:31 -0500351static int
James Smart2e0fef82007-06-17 19:56:36 -0500352lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
353 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500354{
dea31012005-04-17 16:05:31 -0500355 list_add_tail(&piocb->list, &pring->txcmplq);
356 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500357 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
358 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
359 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
360 if (!piocb->vport)
361 BUG();
362 else
363 mod_timer(&piocb->vport->els_tmofunc,
364 jiffies + HZ * (phba->fc_ratov << 1));
365 }
366
dea31012005-04-17 16:05:31 -0500367
James Smart2e0fef82007-06-17 19:56:36 -0500368 return 0;
dea31012005-04-17 16:05:31 -0500369}
370
James Smarte59058c2008-08-24 21:49:00 -0400371/**
372 * lpfc_sli_ringtx_get: Get first element of the txq.
373 * @phba: Pointer to HBA context object.
374 * @pring: Pointer to driver SLI ring object.
375 *
376 * This function is called with hbalock held to get next
377 * iocb in txq of the given ring. If there is any iocb in
378 * the txq, the function returns first iocb in the list after
379 * removing the iocb from the list, else it returns NULL.
380 **/
dea31012005-04-17 16:05:31 -0500381static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500382lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500383{
dea31012005-04-17 16:05:31 -0500384 struct lpfc_iocbq *cmd_iocb;
385
James Smart858c9f62007-06-17 19:56:39 -0500386 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
387 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500388 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500389 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500390}
391
James Smarte59058c2008-08-24 21:49:00 -0400392/**
393 * lpfc_sli_next_iocb_slot: Get next iocb slot in the ring.
394 * @phba: Pointer to HBA context object.
395 * @pring: Pointer to driver SLI ring object.
396 *
397 * This function is called with hbalock held and the caller must post the
398 * iocb without releasing the lock. If the caller releases the lock,
399 * iocb slot returned by the function is not guaranteed to be available.
400 * The function returns pointer to the next available iocb slot if there
401 * is available slot in the ring, else it returns NULL.
402 * If the get index of the ring is ahead of the put index, the function
403 * will post an error attention event to the worker thread to take the
404 * HBA to offline state.
405 **/
dea31012005-04-17 16:05:31 -0500406static IOCB_t *
407lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
408{
James Smarted957682007-06-17 19:56:37 -0500409 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
410 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
411 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -0500412 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500413
414 if ((pring->next_cmdidx == pring->cmdidx) &&
415 (++pring->next_cmdidx >= max_cmd_idx))
416 pring->next_cmdidx = 0;
417
418 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
419
420 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
421
422 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
423 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400424 "0315 Ring %d issue: portCmdGet %d "
dea31012005-04-17 16:05:31 -0500425 "is bigger then cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400426 pring->ringno,
dea31012005-04-17 16:05:31 -0500427 pring->local_getidx, max_cmd_idx);
428
James Smart2e0fef82007-06-17 19:56:36 -0500429 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500430 /*
431 * All error attention handlers are posted to
432 * worker thread
433 */
434 phba->work_ha |= HA_ERATT;
435 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500436
James Smart5e9d9b82008-06-14 22:52:53 -0400437 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500438
439 return NULL;
440 }
441
442 if (pring->local_getidx == pring->next_cmdidx)
443 return NULL;
444 }
445
James Smarted957682007-06-17 19:56:37 -0500446 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500447}
448
James Smarte59058c2008-08-24 21:49:00 -0400449/**
450 * lpfc_sli_next_iotag: Get an iotag for the iocb.
451 * @phba: Pointer to HBA context object.
452 * @iocbq: Pointer to driver iocb object.
453 *
454 * This function gets an iotag for the iocb. If there is no unused iotag and
455 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
456 * array and assigns a new iotag.
457 * The function returns the allocated iotag if successful, else returns zero.
458 * Zero is not a valid iotag.
459 * The caller is not required to hold any lock.
460 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500461uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500462lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500463{
James Smart2e0fef82007-06-17 19:56:36 -0500464 struct lpfc_iocbq **new_arr;
465 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500466 size_t new_len;
467 struct lpfc_sli *psli = &phba->sli;
468 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500469
James Smart2e0fef82007-06-17 19:56:36 -0500470 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500471 iotag = psli->last_iotag;
472 if(++iotag < psli->iocbq_lookup_len) {
473 psli->last_iotag = iotag;
474 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500475 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500476 iocbq->iotag = iotag;
477 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500478 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500479 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
480 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500481 spin_unlock_irq(&phba->hbalock);
482 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500483 GFP_KERNEL);
484 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500485 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500486 old_arr = psli->iocbq_lookup;
487 if (new_len <= psli->iocbq_lookup_len) {
488 /* highly unprobable case */
489 kfree(new_arr);
490 iotag = psli->last_iotag;
491 if(++iotag < psli->iocbq_lookup_len) {
492 psli->last_iotag = iotag;
493 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500494 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500495 iocbq->iotag = iotag;
496 return iotag;
497 }
James Smart2e0fef82007-06-17 19:56:36 -0500498 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500499 return 0;
500 }
501 if (psli->iocbq_lookup)
502 memcpy(new_arr, old_arr,
503 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -0400504 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -0500505 psli->iocbq_lookup = new_arr;
506 psli->iocbq_lookup_len = new_len;
507 psli->last_iotag = iotag;
508 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500509 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500510 iocbq->iotag = iotag;
511 kfree(old_arr);
512 return iotag;
513 }
James Smart8f6d98d2006-08-01 07:34:00 -0400514 } else
James Smart2e0fef82007-06-17 19:56:36 -0500515 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500516
James Bottomley604a3e32005-10-29 10:28:33 -0500517 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400518 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
519 psli->last_iotag);
dea31012005-04-17 16:05:31 -0500520
James Bottomley604a3e32005-10-29 10:28:33 -0500521 return 0;
dea31012005-04-17 16:05:31 -0500522}
523
James Smarte59058c2008-08-24 21:49:00 -0400524/**
525 * lpfc_sli_submit_iocb: Submit an iocb to the firmware.
526 * @phba: Pointer to HBA context object.
527 * @pring: Pointer to driver SLI ring object.
528 * @iocb: Pointer to iocb slot in the ring.
529 * @nextiocb: Pointer to driver iocb object which need to be
530 * posted to firmware.
531 *
532 * This function is called with hbalock held to post a new iocb to
533 * the firmware. This function copies the new iocb to ring iocb slot and
534 * updates the ring pointers. It adds the new iocb to txcmplq if there is
535 * a completion call back for this iocb else the function will free the
536 * iocb object.
537 **/
dea31012005-04-17 16:05:31 -0500538static void
539lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
540 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
541{
542 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500543 * Set up an iotag
dea31012005-04-17 16:05:31 -0500544 */
James Bottomley604a3e32005-10-29 10:28:33 -0500545 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500546
James Smarta58cbd52007-08-02 11:09:43 -0400547 if (pring->ringno == LPFC_ELS_RING) {
548 lpfc_debugfs_slow_ring_trc(phba,
549 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
550 *(((uint32_t *) &nextiocb->iocb) + 4),
551 *(((uint32_t *) &nextiocb->iocb) + 6),
552 *(((uint32_t *) &nextiocb->iocb) + 7));
553 }
554
dea31012005-04-17 16:05:31 -0500555 /*
556 * Issue iocb command to adapter
557 */
James Smart92d7f7b2007-06-17 19:56:38 -0500558 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500559 wmb();
560 pring->stats.iocb_cmd++;
561
562 /*
563 * If there is no completion routine to call, we can release the
564 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
565 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
566 */
567 if (nextiocb->iocb_cmpl)
568 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500569 else
James Smart2e0fef82007-06-17 19:56:36 -0500570 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500571
572 /*
573 * Let the HBA know what IOCB slot will be the next one the
574 * driver will put a command into.
575 */
576 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500577 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500578}
579
James Smarte59058c2008-08-24 21:49:00 -0400580/**
581 * lpfc_sli_update_full_ring: Update the chip attention register.
582 * @phba: Pointer to HBA context object.
583 * @pring: Pointer to driver SLI ring object.
584 *
585 * The caller is not required to hold any lock for calling this function.
586 * This function updates the chip attention bits for the ring to inform firmware
587 * that there are pending work to be done for this ring and requests an
588 * interrupt when there is space available in the ring. This function is
589 * called when the driver is unable to post more iocbs to the ring due
590 * to unavailability of space in the ring.
591 **/
dea31012005-04-17 16:05:31 -0500592static void
James Smart2e0fef82007-06-17 19:56:36 -0500593lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500594{
595 int ringno = pring->ringno;
596
597 pring->flag |= LPFC_CALL_RING_AVAILABLE;
598
599 wmb();
600
601 /*
602 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
603 * The HBA will tell us when an IOCB entry is available.
604 */
605 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
606 readl(phba->CAregaddr); /* flush */
607
608 pring->stats.iocb_cmd_full++;
609}
610
James Smarte59058c2008-08-24 21:49:00 -0400611/**
612 * lpfc_sli_update_ring: Update chip attention register.
613 * @phba: Pointer to HBA context object.
614 * @pring: Pointer to driver SLI ring object.
615 *
616 * This function updates the chip attention register bit for the
617 * given ring to inform HBA that there is more work to be done
618 * in this ring. The caller is not required to hold any lock.
619 **/
dea31012005-04-17 16:05:31 -0500620static void
James Smart2e0fef82007-06-17 19:56:36 -0500621lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500622{
623 int ringno = pring->ringno;
624
625 /*
626 * Tell the HBA that there is work to do in this ring.
627 */
628 wmb();
629 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
630 readl(phba->CAregaddr); /* flush */
631}
632
James Smarte59058c2008-08-24 21:49:00 -0400633/**
634 * lpfc_sli_resume_iocb: Process iocbs in the txq.
635 * @phba: Pointer to HBA context object.
636 * @pring: Pointer to driver SLI ring object.
637 *
638 * This function is called with hbalock held to post pending iocbs
639 * in the txq to the firmware. This function is called when driver
640 * detects space available in the ring.
641 **/
dea31012005-04-17 16:05:31 -0500642static void
James Smart2e0fef82007-06-17 19:56:36 -0500643lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500644{
645 IOCB_t *iocb;
646 struct lpfc_iocbq *nextiocb;
647
648 /*
649 * Check to see if:
650 * (a) there is anything on the txq to send
651 * (b) link is up
652 * (c) link attention events can be processed (fcp ring only)
653 * (d) IOCB processing is not blocked by the outstanding mbox command.
654 */
655 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500656 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500657 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -0400658 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -0500659
660 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
661 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
662 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
663
664 if (iocb)
665 lpfc_sli_update_ring(phba, pring);
666 else
667 lpfc_sli_update_full_ring(phba, pring);
668 }
669
670 return;
671}
672
James Smarte59058c2008-08-24 21:49:00 -0400673/**
674 * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
675 * @phba: Pointer to HBA context object.
676 * @hbqno: HBQ number.
677 *
678 * This function is called with hbalock held to get the next
679 * available slot for the given HBQ. If there is free slot
680 * available for the HBQ it will return pointer to the next available
681 * HBQ entry else it will return NULL.
682 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100683static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500684lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
685{
686 struct hbq_s *hbqp = &phba->hbqs[hbqno];
687
688 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
689 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
690 hbqp->next_hbqPutIdx = 0;
691
692 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500693 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500694 uint32_t getidx = le32_to_cpu(raw_index);
695
696 hbqp->local_hbqGetIdx = getidx;
697
698 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
699 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500700 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400701 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500702 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400703 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500704 hbqp->entry_count);
705
706 phba->link_state = LPFC_HBA_ERROR;
707 return NULL;
708 }
709
710 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
711 return NULL;
712 }
713
James Smart51ef4c22007-08-02 11:10:31 -0400714 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
715 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500716}
717
James Smarte59058c2008-08-24 21:49:00 -0400718/**
719 * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
720 * @phba: Pointer to HBA context object.
721 *
722 * This function is called with no lock held to free all the
723 * hbq buffers while uninitializing the SLI interface. It also
724 * frees the HBQ buffers returned by the firmware but not yet
725 * processed by the upper layers.
726 **/
James Smarted957682007-06-17 19:56:37 -0500727void
728lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
729{
James Smart92d7f7b2007-06-17 19:56:38 -0500730 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
731 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -0500732 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -0400733 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -0500734 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500735
James Smart51ef4c22007-08-02 11:10:31 -0400736 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500737 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -0500738 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -0400739 for (i = 0; i < hbq_count; ++i) {
740 list_for_each_entry_safe(dmabuf, next_dmabuf,
741 &phba->hbqs[i].hbq_buffer_list, list) {
742 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
743 list_del(&hbq_buf->dbuf.list);
744 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
745 }
James Smarta8adb832007-10-27 13:37:53 -0400746 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -0500747 }
James Smart3163f722008-02-08 18:50:25 -0500748 /* Return all HBQ buffer that are in-fly */
749 list_for_each_entry_safe(dmabuf, next_dmabuf,
750 &phba->hbqbuf_in_list, list) {
751 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
752 list_del(&hbq_buf->dbuf.list);
753 if (hbq_buf->tag == -1) {
754 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
755 (phba, hbq_buf);
756 } else {
757 hbqno = hbq_buf->tag >> 16;
758 if (hbqno >= LPFC_MAX_HBQS)
759 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
760 (phba, hbq_buf);
761 else
762 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
763 hbq_buf);
764 }
765 }
766
767 /* Mark the HBQs not in use */
768 phba->hbq_in_use = 0;
769 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -0500770}
771
James Smarte59058c2008-08-24 21:49:00 -0400772/**
773 * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
774 * @phba: Pointer to HBA context object.
775 * @hbqno: HBQ number.
776 * @hbq_buf: Pointer to HBQ buffer.
777 *
778 * This function is called with the hbalock held to post a
779 * hbq buffer to the firmware. If the function finds an empty
780 * slot in the HBQ, it will post the buffer. The function will return
781 * pointer to the hbq entry if it successfully post the buffer
782 * else it will return NULL.
783 **/
James Smart51ef4c22007-08-02 11:10:31 -0400784static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500785lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500786 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500787{
788 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500789 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500790
791 /* Get next HBQ entry slot to use */
792 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
793 if (hbqe) {
794 struct hbq_s *hbqp = &phba->hbqs[hbqno];
795
James Smart92d7f7b2007-06-17 19:56:38 -0500796 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
797 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400798 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500799 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500800 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
801 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
802 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500803 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
804 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500805 /* flush */
James Smarted957682007-06-17 19:56:37 -0500806 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400807 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smarted957682007-06-17 19:56:37 -0500808 }
James Smart51ef4c22007-08-02 11:10:31 -0400809 return hbqe;
James Smarted957682007-06-17 19:56:37 -0500810}
811
James Smarte59058c2008-08-24 21:49:00 -0400812/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -0500813static struct lpfc_hbq_init lpfc_els_hbq = {
814 .rn = 1,
815 .entry_count = 200,
816 .mask_count = 0,
817 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400818 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500819 .buffer_count = 0,
820 .init_count = 20,
821 .add_count = 5,
822};
James Smarted957682007-06-17 19:56:37 -0500823
James Smarte59058c2008-08-24 21:49:00 -0400824/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -0400825static struct lpfc_hbq_init lpfc_extra_hbq = {
826 .rn = 1,
827 .entry_count = 200,
828 .mask_count = 0,
829 .profile = 0,
830 .ring_mask = (1 << LPFC_EXTRA_RING),
831 .buffer_count = 0,
832 .init_count = 0,
833 .add_count = 5,
834};
835
James Smarte59058c2008-08-24 21:49:00 -0400836/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -0400837struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500838 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400839 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500840};
841
James Smarte59058c2008-08-24 21:49:00 -0400842/**
843 * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
844 * @phba: Pointer to HBA context object.
845 * @hbqno: HBQ number.
846 * @count: Number of HBQ buffers to be posted.
847 *
848 * This function is called with no lock held to post more
849 * hbq buffers to the given HBQ. The function returns 0
850 * when successful and returns 1 other wise.
851 **/
James Smart311464e2007-08-02 11:10:37 -0400852static int
James Smart92d7f7b2007-06-17 19:56:38 -0500853lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
854{
855 uint32_t i, start, end;
James Smart3163f722008-02-08 18:50:25 -0500856 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500857 struct hbq_dmabuf *hbq_buffer;
858
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700859 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -0400860 return 0;
James Smart51ef4c22007-08-02 11:10:31 -0400861
James Smarta8adb832007-10-27 13:37:53 -0400862 start = phba->hbqs[hbqno].buffer_count;
863 end = count + start;
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700864 if (end > lpfc_hbq_defs[hbqno]->entry_count)
James Smart92d7f7b2007-06-17 19:56:38 -0500865 end = lpfc_hbq_defs[hbqno]->entry_count;
James Smart92d7f7b2007-06-17 19:56:38 -0500866
James Smart3163f722008-02-08 18:50:25 -0500867 /* Check whether HBQ is still in use */
868 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700869 if (!phba->hbq_in_use)
870 goto out;
James Smart3163f722008-02-08 18:50:25 -0500871
James Smart92d7f7b2007-06-17 19:56:38 -0500872 /* Populate HBQ entries */
873 for (i = start; i < end; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400874 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500875 if (!hbq_buffer)
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700876 goto err;
James Smart92d7f7b2007-06-17 19:56:38 -0500877 hbq_buffer->tag = (i | (hbqno << 16));
James Smart51ef4c22007-08-02 11:10:31 -0400878 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smarta8adb832007-10-27 13:37:53 -0400879 phba->hbqs[hbqno].buffer_count++;
James Smart51ef4c22007-08-02 11:10:31 -0400880 else
881 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -0500882 }
James Smart3163f722008-02-08 18:50:25 -0500883
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700884 out:
James Smart3163f722008-02-08 18:50:25 -0500885 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500886 return 0;
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700887 err:
888 spin_unlock_irqrestore(&phba->hbalock, flags);
889 return 1;
James Smarted957682007-06-17 19:56:37 -0500890}
891
James Smarte59058c2008-08-24 21:49:00 -0400892/**
893 * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
894 * @phba: Pointer to HBA context object.
895 * @qno: HBQ number.
896 *
897 * This function posts more buffers to the HBQ. This function
898 * is called with no lock held. The function returns 0 when
899 * successful and returns 1 otherwise.
900 **/
James Smarted957682007-06-17 19:56:37 -0500901int
James Smart92d7f7b2007-06-17 19:56:38 -0500902lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500903{
James Smart92d7f7b2007-06-17 19:56:38 -0500904 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
905 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -0500906}
907
James Smarte59058c2008-08-24 21:49:00 -0400908/**
909 * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
910 * @phba: Pointer to HBA context object.
911 * @qno: HBQ queue number.
912 *
913 * This function is called from SLI initialization code path with
914 * no lock held to post initial HBQ buffers to firmware. The
915 * function returns 0 when successful and returns 1 otherwise.
916 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100917static int
James Smart92d7f7b2007-06-17 19:56:38 -0500918lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500919{
James Smart92d7f7b2007-06-17 19:56:38 -0500920 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
921 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -0500922}
923
James Smarte59058c2008-08-24 21:49:00 -0400924/**
925 * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
926 * @phba: Pointer to HBA context object.
927 * @tag: Tag of the hbq buffer.
928 *
929 * This function is called with hbalock held. This function searches
930 * for the hbq buffer associated with the given tag in the hbq buffer
931 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
932 * it returns NULL.
933 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100934static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -0500935lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
936{
James Smart92d7f7b2007-06-17 19:56:38 -0500937 struct lpfc_dmabuf *d_buf;
938 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400939 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500940
James Smart51ef4c22007-08-02 11:10:31 -0400941 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +0200942 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -0400943 return NULL;
944
945 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -0500946 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -0400947 if (hbq_buf->tag == tag) {
James Smart92d7f7b2007-06-17 19:56:38 -0500948 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -0500949 }
950 }
James Smart92d7f7b2007-06-17 19:56:38 -0500951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400952 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -0400953 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -0500954 return NULL;
James Smarted957682007-06-17 19:56:37 -0500955}
956
James Smarte59058c2008-08-24 21:49:00 -0400957/**
958 * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
959 * @phba: Pointer to HBA context object.
960 * @hbq_buffer: Pointer to HBQ buffer.
961 *
962 * This function is called with hbalock. This function gives back
963 * the hbq buffer to firmware. If the HBQ does not have space to
964 * post the buffer, it will free the buffer.
965 **/
James Smarted957682007-06-17 19:56:37 -0500966void
James Smart51ef4c22007-08-02 11:10:31 -0400967lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -0500968{
969 uint32_t hbqno;
970
James Smart51ef4c22007-08-02 11:10:31 -0400971 if (hbq_buffer) {
972 hbqno = hbq_buffer->tag >> 16;
973 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
974 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
975 }
James Smarted957682007-06-17 19:56:37 -0500976 }
977}
978
James Smarte59058c2008-08-24 21:49:00 -0400979/**
980 * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
981 * @mbxCommand: mailbox command code.
982 *
983 * This function is called by the mailbox event handler function to verify
984 * that the completed mailbox command is a legitimate mailbox command. If the
985 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
986 * and the mailbox event handler will take the HBA offline.
987 **/
dea31012005-04-17 16:05:31 -0500988static int
989lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
990{
991 uint8_t ret;
992
993 switch (mbxCommand) {
994 case MBX_LOAD_SM:
995 case MBX_READ_NV:
996 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -0400997 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -0500998 case MBX_RUN_BIU_DIAG:
999 case MBX_INIT_LINK:
1000 case MBX_DOWN_LINK:
1001 case MBX_CONFIG_LINK:
1002 case MBX_CONFIG_RING:
1003 case MBX_RESET_RING:
1004 case MBX_READ_CONFIG:
1005 case MBX_READ_RCONFIG:
1006 case MBX_READ_SPARM:
1007 case MBX_READ_STATUS:
1008 case MBX_READ_RPI:
1009 case MBX_READ_XRI:
1010 case MBX_READ_REV:
1011 case MBX_READ_LNK_STAT:
1012 case MBX_REG_LOGIN:
1013 case MBX_UNREG_LOGIN:
1014 case MBX_READ_LA:
1015 case MBX_CLEAR_LA:
1016 case MBX_DUMP_MEMORY:
1017 case MBX_DUMP_CONTEXT:
1018 case MBX_RUN_DIAGS:
1019 case MBX_RESTART:
1020 case MBX_UPDATE_CFG:
1021 case MBX_DOWN_LOAD:
1022 case MBX_DEL_LD_ENTRY:
1023 case MBX_RUN_PROGRAM:
1024 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001025 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001026 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001027 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001028 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001029 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001030 case MBX_LOAD_AREA:
1031 case MBX_RUN_BIU_DIAG64:
1032 case MBX_CONFIG_PORT:
1033 case MBX_READ_SPARM64:
1034 case MBX_READ_RPI64:
1035 case MBX_REG_LOGIN64:
1036 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001037 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001038 case MBX_SET_DEBUG:
1039 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001040 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001041 case MBX_REG_VPI:
1042 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001043 case MBX_HEARTBEAT:
dea31012005-04-17 16:05:31 -05001044 ret = mbxCommand;
1045 break;
1046 default:
1047 ret = MBX_SHUTDOWN;
1048 break;
1049 }
James Smart2e0fef82007-06-17 19:56:36 -05001050 return ret;
dea31012005-04-17 16:05:31 -05001051}
James Smarte59058c2008-08-24 21:49:00 -04001052
1053/**
1054 * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
1055 * lpfc_sli_issue_mbox_wait.
1056 * @phba: Pointer to HBA context object.
1057 * @pmboxq: Pointer to mailbox command.
1058 *
1059 * This is completion handler function for mailbox commands issued from
1060 * lpfc_sli_issue_mbox_wait function. This function is called by the
1061 * mailbox event handler function with no lock held. This function
1062 * will wake up thread waiting on the wait queue pointed by context1
1063 * of the mailbox.
1064 **/
dea31012005-04-17 16:05:31 -05001065static void
James Smart2e0fef82007-06-17 19:56:36 -05001066lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001067{
1068 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001069 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001070
1071 /*
1072 * If pdone_q is empty, the driver thread gave up waiting and
1073 * continued running.
1074 */
James Smart7054a602007-04-25 09:52:34 -04001075 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001076 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001077 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1078 if (pdone_q)
1079 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001080 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001081 return;
1082}
1083
James Smarte59058c2008-08-24 21:49:00 -04001084
1085/**
1086 * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
1087 * @phba: Pointer to HBA context object.
1088 * @pmb: Pointer to mailbox object.
1089 *
1090 * This function is the default mailbox completion handler. It
1091 * frees the memory resources associated with the completed mailbox
1092 * command. If the completed command is a REG_LOGIN mailbox command,
1093 * this function will issue a UREG_LOGIN to re-claim the RPI.
1094 **/
dea31012005-04-17 16:05:31 -05001095void
James Smart2e0fef82007-06-17 19:56:36 -05001096lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001097{
1098 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -04001099 uint16_t rpi;
1100 int rc;
1101
dea31012005-04-17 16:05:31 -05001102 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001103
dea31012005-04-17 16:05:31 -05001104 if (mp) {
1105 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1106 kfree(mp);
1107 }
James Smart7054a602007-04-25 09:52:34 -04001108
1109 /*
1110 * If a REG_LOGIN succeeded after node is destroyed or node
1111 * is in re-discovery driver need to cleanup the RPI.
1112 */
James Smart2e0fef82007-06-17 19:56:36 -05001113 if (!(phba->pport->load_flag & FC_UNLOADING) &&
1114 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1115 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -04001116
1117 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -05001118 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1119 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001120 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1121 if (rc != MBX_NOT_FINISHED)
1122 return;
1123 }
1124
James Smart2e0fef82007-06-17 19:56:36 -05001125 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001126 return;
1127}
1128
James Smarte59058c2008-08-24 21:49:00 -04001129/**
1130 * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
1131 * @phba: Pointer to HBA context object.
1132 *
1133 * This function is called with no lock held. This function processes all
1134 * the completed mailbox commands and gives it to upper layers. The interrupt
1135 * service routine processes mailbox completion interrupt and adds completed
1136 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1137 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1138 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1139 * function returns the mailbox commands to the upper layer by calling the
1140 * completion handler function of each mailbox.
1141 **/
dea31012005-04-17 16:05:31 -05001142int
James Smart2e0fef82007-06-17 19:56:36 -05001143lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001144{
James Smart92d7f7b2007-06-17 19:56:38 -05001145 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001146 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001147 int rc;
1148 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001149
1150 phba->sli.slistat.mbox_event++;
1151
James Smart92d7f7b2007-06-17 19:56:38 -05001152 /* Get all completed mailboxe buffers into the cmplq */
1153 spin_lock_irq(&phba->hbalock);
1154 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1155 spin_unlock_irq(&phba->hbalock);
1156
dea31012005-04-17 16:05:31 -05001157 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001158 do {
1159 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1160 if (pmb == NULL)
1161 break;
1162
dea31012005-04-17 16:05:31 -05001163 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -05001164
James Smart858c9f62007-06-17 19:56:39 -05001165 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1166 if (pmb->vport) {
1167 lpfc_debugfs_disc_trc(pmb->vport,
1168 LPFC_DISC_TRC_MBOX_VPORT,
1169 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1170 (uint32_t)pmbox->mbxCommand,
1171 pmbox->un.varWords[0],
1172 pmbox->un.varWords[1]);
1173 }
1174 else {
1175 lpfc_debugfs_disc_trc(phba->pport,
1176 LPFC_DISC_TRC_MBOX,
1177 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1178 (uint32_t)pmbox->mbxCommand,
1179 pmbox->un.varWords[0],
1180 pmbox->un.varWords[1]);
1181 }
1182 }
1183
dea31012005-04-17 16:05:31 -05001184 /*
1185 * It is a fatal error if unknown mbox command completion.
1186 */
1187 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1188 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001189 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001190 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001191 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -05001192 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001193 pmb->vport ? pmb->vport->vpi : 0,
1194 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -05001195 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001196 phba->work_hs = HS_FFER3;
1197 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001198 continue;
dea31012005-04-17 16:05:31 -05001199 }
1200
dea31012005-04-17 16:05:31 -05001201 if (pmbox->mbxStatus) {
1202 phba->sli.slistat.mbox_stat_err++;
1203 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1204 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001205 lpfc_printf_log(phba, KERN_INFO,
1206 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001207 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001208 "error - RETRYing Data: x%x "
1209 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001210 pmb->vport ? pmb->vport->vpi :0,
1211 pmbox->mbxCommand,
1212 pmbox->mbxStatus,
1213 pmbox->un.varWords[0],
1214 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001215 pmbox->mbxStatus = 0;
1216 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -05001217 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001218 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001219 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001220 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1221 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -05001222 continue;
dea31012005-04-17 16:05:31 -05001223 }
1224 }
1225
1226 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001227 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001228 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -05001229 "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 -05001230 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001231 pmbox->mbxCommand,
1232 pmb->mbox_cmpl,
1233 *((uint32_t *) pmbox),
1234 pmbox->un.varWords[0],
1235 pmbox->un.varWords[1],
1236 pmbox->un.varWords[2],
1237 pmbox->un.varWords[3],
1238 pmbox->un.varWords[4],
1239 pmbox->un.varWords[5],
1240 pmbox->un.varWords[6],
1241 pmbox->un.varWords[7]);
1242
James Smart92d7f7b2007-06-17 19:56:38 -05001243 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001244 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001245 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001246 return 0;
dea31012005-04-17 16:05:31 -05001247}
James Smart92d7f7b2007-06-17 19:56:38 -05001248
James Smarte59058c2008-08-24 21:49:00 -04001249/**
1250 * lpfc_sli_replace_hbqbuff: Replace the HBQ buffer with a new buffer.
1251 * @phba: Pointer to HBA context object.
1252 * @tag: Tag for the HBQ buffer.
1253 *
1254 * This function is called from unsolicited event handler code path to get the
1255 * HBQ buffer associated with an unsolicited iocb. This function is called with
1256 * no lock held. It returns the buffer associated with the given tag and posts
1257 * another buffer to the firmware.
1258 **/
James Smart92d7f7b2007-06-17 19:56:38 -05001259static struct lpfc_dmabuf *
1260lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
1261{
1262 struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
James Smart51ef4c22007-08-02 11:10:31 -04001263 uint32_t hbqno;
1264 void *virt; /* virtual address ptr */
1265 dma_addr_t phys; /* mapped address */
James Smart3163f722008-02-08 18:50:25 -05001266 unsigned long flags;
1267
1268 /* Check whether HBQ is still in use */
1269 spin_lock_irqsave(&phba->hbalock, flags);
1270 if (!phba->hbq_in_use) {
1271 spin_unlock_irqrestore(&phba->hbalock, flags);
1272 return NULL;
1273 }
James Smart92d7f7b2007-06-17 19:56:38 -05001274
1275 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
James Smart3163f722008-02-08 18:50:25 -05001276 if (hbq_entry == NULL) {
1277 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05001278 return NULL;
James Smart3163f722008-02-08 18:50:25 -05001279 }
James Smart92d7f7b2007-06-17 19:56:38 -05001280 list_del(&hbq_entry->dbuf.list);
James Smart51ef4c22007-08-02 11:10:31 -04001281
1282 hbqno = tag >> 16;
1283 new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart3163f722008-02-08 18:50:25 -05001284 if (new_hbq_entry == NULL) {
1285 list_add_tail(&hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1286 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05001287 return &hbq_entry->dbuf;
James Smart3163f722008-02-08 18:50:25 -05001288 }
James Smart92d7f7b2007-06-17 19:56:38 -05001289 new_hbq_entry->tag = -1;
James Smart51ef4c22007-08-02 11:10:31 -04001290 phys = new_hbq_entry->dbuf.phys;
1291 virt = new_hbq_entry->dbuf.virt;
1292 new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
1293 new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
1294 hbq_entry->dbuf.phys = phys;
1295 hbq_entry->dbuf.virt = virt;
James Smart92d7f7b2007-06-17 19:56:38 -05001296 lpfc_sli_free_hbq(phba, hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001297 list_add_tail(&new_hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1298 spin_unlock_irqrestore(&phba->hbalock, flags);
1299
James Smart92d7f7b2007-06-17 19:56:38 -05001300 return &new_hbq_entry->dbuf;
1301}
1302
James Smarte59058c2008-08-24 21:49:00 -04001303/**
1304 * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
1305 * @phba: Pointer to HBA context object.
1306 * @pring: Pointer to driver SLI ring object.
1307 * @tag: buffer tag.
1308 *
1309 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1310 * is set in the tag the buffer is posted for a particular exchange,
1311 * the function will return the buffer without replacing the buffer.
1312 * If the buffer is for unsolicited ELS or CT traffic, this function
1313 * returns the buffer and also posts another buffer to the firmware.
1314 **/
James Smart76bb24e2007-10-27 13:38:00 -04001315static struct lpfc_dmabuf *
1316lpfc_sli_get_buff(struct lpfc_hba *phba,
1317 struct lpfc_sli_ring *pring,
1318 uint32_t tag)
1319{
1320 if (tag & QUE_BUFTAG_BIT)
1321 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1322 else
1323 return lpfc_sli_replace_hbqbuff(phba, tag);
1324}
James Smart57127f12007-10-27 13:37:05 -04001325
James Smarte59058c2008-08-24 21:49:00 -04001326
1327/**
1328 * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
1329 * @phba: Pointer to HBA context object.
1330 * @pring: Pointer to driver SLI ring object.
1331 * @saveq: Pointer to the unsolicited iocb.
1332 *
1333 * This function is called with no lock held by the ring event handler
1334 * when there is an unsolicited iocb posted to the response ring by the
1335 * firmware. This function gets the buffer associated with the iocbs
1336 * and calls the event handler for the ring. This function handles both
1337 * qring buffers and hbq buffers.
1338 * When the function returns 1 the caller can free the iocb object otherwise
1339 * upper layer functions will free the iocb objects.
1340 **/
dea31012005-04-17 16:05:31 -05001341static int
1342lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1343 struct lpfc_iocbq *saveq)
1344{
1345 IOCB_t * irsp;
1346 WORD5 * w5p;
1347 uint32_t Rctl, Type;
1348 uint32_t match, i;
James Smart76bb24e2007-10-27 13:38:00 -04001349 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001350 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001351
1352 match = 0;
1353 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001354
James Smart9c2face2008-01-11 01:53:18 -05001355 if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
1356 return 1;
James Smart57127f12007-10-27 13:37:05 -04001357 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1358 if (pring->lpfc_sli_rcv_async_status)
1359 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1360 else
1361 lpfc_printf_log(phba,
1362 KERN_WARNING,
1363 LOG_SLI,
1364 "0316 Ring %d handler: unexpected "
1365 "ASYNC_STATUS iocb received evt_code "
1366 "0x%x\n",
1367 pring->ringno,
1368 irsp->un.asyncstat.evt_code);
1369 return 1;
1370 }
1371
James Smart3163f722008-02-08 18:50:25 -05001372 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1373 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1374 if (irsp->ulpBdeCount > 0) {
1375 dmzbuf = lpfc_sli_get_buff(phba, pring,
1376 irsp->un.ulpWord[3]);
1377 lpfc_in_buf_free(phba, dmzbuf);
1378 }
1379
1380 if (irsp->ulpBdeCount > 1) {
1381 dmzbuf = lpfc_sli_get_buff(phba, pring,
1382 irsp->unsli3.sli3Words[3]);
1383 lpfc_in_buf_free(phba, dmzbuf);
1384 }
1385
1386 if (irsp->ulpBdeCount > 2) {
1387 dmzbuf = lpfc_sli_get_buff(phba, pring,
1388 irsp->unsli3.sli3Words[7]);
1389 lpfc_in_buf_free(phba, dmzbuf);
1390 }
1391
1392 return 1;
1393 }
1394
James Smart92d7f7b2007-06-17 19:56:38 -05001395 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001396 if (irsp->ulpBdeCount != 0) {
1397 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001398 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001399 if (!saveq->context2)
1400 lpfc_printf_log(phba,
1401 KERN_ERR,
1402 LOG_SLI,
1403 "0341 Ring %d Cannot find buffer for "
1404 "an unsolicited iocb. tag 0x%x\n",
1405 pring->ringno,
1406 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001407 }
1408 if (irsp->ulpBdeCount == 2) {
1409 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04001410 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04001411 if (!saveq->context3)
1412 lpfc_printf_log(phba,
1413 KERN_ERR,
1414 LOG_SLI,
1415 "0342 Ring %d Cannot find buffer for an"
1416 " unsolicited iocb. tag 0x%x\n",
1417 pring->ringno,
1418 irsp->unsli3.sli3Words[7]);
1419 }
1420 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04001421 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04001422 if (irsp->ulpBdeCount != 0) {
1423 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1424 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05001425 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04001426 lpfc_printf_log(phba,
1427 KERN_ERR,
1428 LOG_SLI,
1429 "0343 Ring %d Cannot find "
1430 "buffer for an unsolicited iocb"
1431 ". tag 0x%x\n", pring->ringno,
1432 irsp->un.ulpWord[3]);
1433 }
1434 if (irsp->ulpBdeCount == 2) {
1435 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1436 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05001437 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04001438 lpfc_printf_log(phba,
1439 KERN_ERR,
1440 LOG_SLI,
1441 "0344 Ring %d Cannot find "
1442 "buffer for an unsolicited "
1443 "iocb. tag 0x%x\n",
1444 pring->ringno,
1445 irsp->unsli3.sli3Words[7]);
1446 }
1447 }
James Smart92d7f7b2007-06-17 19:56:38 -05001448 }
James Smart9c2face2008-01-11 01:53:18 -05001449 if (irsp->ulpBdeCount != 0 &&
1450 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1451 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1452 int found = 0;
1453
1454 /* search continue save q for same XRI */
1455 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1456 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1457 list_add_tail(&saveq->list, &iocbq->list);
1458 found = 1;
1459 break;
1460 }
1461 }
1462 if (!found)
1463 list_add_tail(&saveq->clist,
1464 &pring->iocb_continue_saveq);
1465 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1466 list_del_init(&iocbq->clist);
1467 saveq = iocbq;
1468 irsp = &(saveq->iocb);
1469 } else
1470 return 0;
1471 }
1472 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1473 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1474 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1475 Rctl = FC_ELS_REQ;
1476 Type = FC_ELS_DATA;
1477 } else {
1478 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1479 Rctl = w5p->hcsw.Rctl;
1480 Type = w5p->hcsw.Type;
1481
1482 /* Firmware Workaround */
1483 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1484 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1485 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1486 Rctl = FC_ELS_REQ;
1487 Type = FC_ELS_DATA;
1488 w5p->hcsw.Rctl = Rctl;
1489 w5p->hcsw.Type = Type;
1490 }
1491 }
James Smart92d7f7b2007-06-17 19:56:38 -05001492
dea31012005-04-17 16:05:31 -05001493 /* unSolicited Responses */
1494 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001495 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1496 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1497 saveq);
dea31012005-04-17 16:05:31 -05001498 match = 1;
1499 } else {
1500 /* We must search, based on rctl / type
1501 for the right routine */
James Smart9c2face2008-01-11 01:53:18 -05001502 for (i = 0; i < pring->num_mask; i++) {
1503 if ((pring->prt[i].rctl == Rctl)
1504 && (pring->prt[i].type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001505 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1506 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1507 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -05001508 match = 1;
1509 break;
1510 }
1511 }
1512 }
1513 if (match == 0) {
1514 /* Unexpected Rctl / Type received */
1515 /* Ring <ringno> handler: unexpected
1516 Rctl <Rctl> Type <Type> received */
James Smart92d7f7b2007-06-17 19:56:38 -05001517 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001518 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001519 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001520 pring->ringno, Rctl, Type);
dea31012005-04-17 16:05:31 -05001521 }
James Smart92d7f7b2007-06-17 19:56:38 -05001522 return 1;
dea31012005-04-17 16:05:31 -05001523}
1524
James Smarte59058c2008-08-24 21:49:00 -04001525/**
1526 * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
1527 * @phba: Pointer to HBA context object.
1528 * @pring: Pointer to driver SLI ring object.
1529 * @prspiocb: Pointer to response iocb object.
1530 *
1531 * This function looks up the iocb_lookup table to get the command iocb
1532 * corresponding to the given response iocb using the iotag of the
1533 * response iocb. This function is called with the hbalock held.
1534 * This function returns the command iocb object if it finds the command
1535 * iocb else returns NULL.
1536 **/
dea31012005-04-17 16:05:31 -05001537static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001538lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1539 struct lpfc_sli_ring *pring,
1540 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001541{
dea31012005-04-17 16:05:31 -05001542 struct lpfc_iocbq *cmd_iocb = NULL;
1543 uint16_t iotag;
1544
James Bottomley604a3e32005-10-29 10:28:33 -05001545 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001546
James Bottomley604a3e32005-10-29 10:28:33 -05001547 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1548 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001549 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001550 pring->txcmplq_cnt--;
1551 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001552 }
1553
dea31012005-04-17 16:05:31 -05001554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001555 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001556 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001557 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001558 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001559 return NULL;
1560}
1561
James Smarte59058c2008-08-24 21:49:00 -04001562/**
1563 * lpfc_sli_process_sol_iocb: process solicited iocb completion.
1564 * @phba: Pointer to HBA context object.
1565 * @pring: Pointer to driver SLI ring object.
1566 * @saveq: Pointer to the response iocb to be processed.
1567 *
1568 * This function is called by the ring event handler for non-fcp
1569 * rings when there is a new response iocb in the response ring.
1570 * The caller is not required to hold any locks. This function
1571 * gets the command iocb associated with the response iocb and
1572 * calls the completion handler for the command iocb. If there
1573 * is no completion handler, the function will free the resources
1574 * associated with command iocb. If the response iocb is for
1575 * an already aborted command iocb, the status of the completion
1576 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1577 * This function always returns 1.
1578 **/
dea31012005-04-17 16:05:31 -05001579static int
James Smart2e0fef82007-06-17 19:56:36 -05001580lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001581 struct lpfc_iocbq *saveq)
1582{
James Smart2e0fef82007-06-17 19:56:36 -05001583 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001584 int rc = 1;
1585 unsigned long iflag;
1586
1587 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001588 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001589 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001590 spin_unlock_irqrestore(&phba->hbalock, iflag);
1591
dea31012005-04-17 16:05:31 -05001592 if (cmdiocbp) {
1593 if (cmdiocbp->iocb_cmpl) {
1594 /*
1595 * Post all ELS completions to the worker thread.
1596 * All other are passed to the completion callback.
1597 */
1598 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001599 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1600 cmdiocbp->iocb_flag &=
1601 ~LPFC_DRIVER_ABORTED;
1602 saveq->iocb.ulpStatus =
1603 IOSTAT_LOCAL_REJECT;
1604 saveq->iocb.un.ulpWord[4] =
1605 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05001606
1607 /* Firmware could still be in progress
1608 * of DMAing payload, so don't free data
1609 * buffer till after a hbeat.
1610 */
1611 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04001612 }
dea31012005-04-17 16:05:31 -05001613 }
James Smart2e0fef82007-06-17 19:56:36 -05001614 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001615 } else
1616 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001617 } else {
1618 /*
1619 * Unknown initiating command based on the response iotag.
1620 * This could be the case on the ELS ring because of
1621 * lpfc_els_abort().
1622 */
1623 if (pring->ringno != LPFC_ELS_RING) {
1624 /*
1625 * Ring <ringno> handler: unexpected completion IoTag
1626 * <IoTag>
1627 */
James Smarte8b62012007-08-02 11:10:09 -04001628 lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1629 "0322 Ring %d handler: "
1630 "unexpected completion IoTag x%x "
1631 "Data: x%x x%x x%x x%x\n",
1632 pring->ringno,
1633 saveq->iocb.ulpIoTag,
1634 saveq->iocb.ulpStatus,
1635 saveq->iocb.un.ulpWord[4],
1636 saveq->iocb.ulpCommand,
1637 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001638 }
1639 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001640
dea31012005-04-17 16:05:31 -05001641 return rc;
1642}
1643
James Smarte59058c2008-08-24 21:49:00 -04001644/**
1645 * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
1646 * @phba: Pointer to HBA context object.
1647 * @pring: Pointer to driver SLI ring object.
1648 *
1649 * This function is called from the iocb ring event handlers when
1650 * put pointer is ahead of the get pointer for a ring. This function signal
1651 * an error attention condition to the worker thread and the worker
1652 * thread will transition the HBA to offline state.
1653 **/
James Smart2e0fef82007-06-17 19:56:36 -05001654static void
1655lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001656{
James Smarted957682007-06-17 19:56:37 -05001657 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1658 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1659 &phba->slim2p->mbx.us.s2.port[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001660 /*
1661 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1662 * rsp ring <portRspMax>
1663 */
1664 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001665 "0312 Ring %d handler: portRspPut %d "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001666 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001667 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001668 pring->numRiocb);
1669
James Smart2e0fef82007-06-17 19:56:36 -05001670 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001671
1672 /*
1673 * All error attention handlers are posted to
1674 * worker thread
1675 */
1676 phba->work_ha |= HA_ERATT;
1677 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001678
James Smart5e9d9b82008-06-14 22:52:53 -04001679 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001680
1681 return;
1682}
1683
James Smarte59058c2008-08-24 21:49:00 -04001684/**
1685 * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
1686 * @phba: Pointer to HBA context object.
1687 *
1688 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1689 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1690 * is enabled.
1691 *
1692 * The caller does not hold any lock.
1693 * The function processes each response iocb in the response ring until it
1694 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1695 * LE bit set. The function will call the completion handler of the command iocb
1696 * if the response iocb indicates a completion for a command iocb or it is
1697 * an abort completion.
1698 **/
James Smart2e0fef82007-06-17 19:56:36 -05001699void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001700{
James Smart2e0fef82007-06-17 19:56:36 -05001701 struct lpfc_sli *psli = &phba->sli;
1702 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001703 IOCB_t *irsp = NULL;
1704 IOCB_t *entry = NULL;
1705 struct lpfc_iocbq *cmdiocbq = NULL;
1706 struct lpfc_iocbq rspiocbq;
1707 struct lpfc_pgp *pgp;
1708 uint32_t status;
1709 uint32_t portRspPut, portRspMax;
1710 int type;
1711 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001712 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001713 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001714
1715 pring->stats.iocb_event++;
1716
James Smarted957682007-06-17 19:56:37 -05001717 pgp = (phba->sli_rev == 3) ?
1718 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1719 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1720
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001721
1722 /*
1723 * The next available response entry should never exceed the maximum
1724 * entries. If it does, treat it as an adapter hardware error.
1725 */
1726 portRspMax = pring->numRiocb;
1727 portRspPut = le32_to_cpu(pgp->rspPutInx);
1728 if (unlikely(portRspPut >= portRspMax)) {
1729 lpfc_sli_rsp_pointers_error(phba, pring);
1730 return;
1731 }
1732
1733 rmb();
1734 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001735 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001736 if (++pring->rspidx >= portRspMax)
1737 pring->rspidx = 0;
1738
1739 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1740 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001741 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001742 irsp = &rspiocbq.iocb;
1743 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1744 pring->stats.iocb_rsp++;
1745 rsp_cmpl++;
1746
1747 if (unlikely(irsp->ulpStatus)) {
1748 /* Rsp ring <ringno> error: IOCB */
1749 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001750 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001751 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001752 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001753 irsp->un.ulpWord[0],
1754 irsp->un.ulpWord[1],
1755 irsp->un.ulpWord[2],
1756 irsp->un.ulpWord[3],
1757 irsp->un.ulpWord[4],
1758 irsp->un.ulpWord[5],
1759 *(((uint32_t *) irsp) + 6),
1760 *(((uint32_t *) irsp) + 7));
1761 }
1762
1763 switch (type) {
1764 case LPFC_ABORT_IOCB:
1765 case LPFC_SOL_IOCB:
1766 /*
1767 * Idle exchange closed via ABTS from port. No iocb
1768 * resources need to be recovered.
1769 */
1770 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001771 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001772 "0314 IOCB cmd 0x%x "
1773 "processed. Skipping "
1774 "completion",
James Smartdca94792006-08-01 07:34:08 -04001775 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001776 break;
1777 }
1778
James Smart2e0fef82007-06-17 19:56:36 -05001779 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001780 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1781 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001782 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001783 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1784 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1785 &rspiocbq);
1786 }
1787 break;
1788 default:
1789 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1790 char adaptermsg[LPFC_MAX_ADPTMSG];
1791 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1792 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1793 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001794 dev_warn(&((phba->pcidev)->dev),
1795 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001796 phba->brd_no, adaptermsg);
1797 } else {
1798 /* Unknown IOCB command */
1799 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001800 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001801 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001802 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001803 irsp->ulpStatus,
1804 irsp->ulpIoTag,
1805 irsp->ulpContext);
1806 }
1807 break;
1808 }
1809
1810 /*
1811 * The response IOCB has been processed. Update the ring
1812 * pointer in SLIM. If the port response put pointer has not
1813 * been updated, sync the pgp->rspPutInx and fetch the new port
1814 * response put pointer.
1815 */
James Smarted957682007-06-17 19:56:37 -05001816 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001817
1818 if (pring->rspidx == portRspPut)
1819 portRspPut = le32_to_cpu(pgp->rspPutInx);
1820 }
1821
1822 ha_copy = readl(phba->HAregaddr);
1823 ha_copy >>= (LPFC_FCP_RING * 4);
1824
1825 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001826 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001827 pring->stats.iocb_rsp_full++;
1828 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1829 writel(status, phba->CAregaddr);
1830 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001831 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001832 }
1833 if ((ha_copy & HA_R0CE_RSP) &&
1834 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001835 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001836 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1837 pring->stats.iocb_cmd_empty++;
1838
1839 /* Force update of the local copy of cmdGetInx */
1840 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1841 lpfc_sli_resume_iocb(phba, pring);
1842
1843 if ((pring->lpfc_sli_cmd_available))
1844 (pring->lpfc_sli_cmd_available) (phba, pring);
1845
James Smart2e0fef82007-06-17 19:56:36 -05001846 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001847 }
1848
1849 return;
1850}
1851
James Smarte59058c2008-08-24 21:49:00 -04001852/**
1853 * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
1854 * @phba: Pointer to HBA context object.
1855 * @pring: Pointer to driver SLI ring object.
1856 * @mask: Host attention register mask for this ring.
1857 *
1858 * This function is called from the interrupt context when there is a ring
1859 * event for the fcp ring. The caller does not hold any lock.
1860 * The function processes each response iocb in the response ring until it
1861 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1862 * LE bit set. The function will call the completion handler of the command iocb
1863 * if the response iocb indicates a completion for a command iocb or it is
1864 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
1865 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05001866 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04001867 * to check it explicitly. This function always returns 1.
1868 **/
dea31012005-04-17 16:05:31 -05001869static int
James Smart2e0fef82007-06-17 19:56:36 -05001870lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1871 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001872{
James Smarted957682007-06-17 19:56:37 -05001873 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1874 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1875 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001876 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001877 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001878 struct lpfc_iocbq *cmdiocbq = NULL;
1879 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001880 uint32_t status;
1881 uint32_t portRspPut, portRspMax;
1882 int rc = 1;
1883 lpfc_iocb_type type;
1884 unsigned long iflag;
1885 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05001886
James Smart2e0fef82007-06-17 19:56:36 -05001887 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001888 pring->stats.iocb_event++;
1889
dea31012005-04-17 16:05:31 -05001890 /*
1891 * The next available response entry should never exceed the maximum
1892 * entries. If it does, treat it as an adapter hardware error.
1893 */
1894 portRspMax = pring->numRiocb;
1895 portRspPut = le32_to_cpu(pgp->rspPutInx);
1896 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001897 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05001898 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001899 return 1;
1900 }
1901
1902 rmb();
1903 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001904 /*
1905 * Fetch an entry off the ring and copy it into a local data
1906 * structure. The copy involves a byte-swap since the
1907 * network byte order and pci byte orders are different.
1908 */
James Smarted957682007-06-17 19:56:37 -05001909 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05001910 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001911
1912 if (++pring->rspidx >= portRspMax)
1913 pring->rspidx = 0;
1914
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001915 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1916 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05001917 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05001918 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001919 irsp = &rspiocbq.iocb;
1920
dea31012005-04-17 16:05:31 -05001921 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1922 pring->stats.iocb_rsp++;
1923 rsp_cmpl++;
1924
1925 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001926 /*
1927 * If resource errors reported from HBA, reduce
1928 * queuedepths of the SCSI device.
1929 */
1930 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1931 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1932 spin_unlock_irqrestore(&phba->hbalock, iflag);
1933 lpfc_adjust_queue_depth(phba);
1934 spin_lock_irqsave(&phba->hbalock, iflag);
1935 }
1936
dea31012005-04-17 16:05:31 -05001937 /* Rsp ring <ringno> error: IOCB */
1938 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001939 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05001940 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001941 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05001942 irsp->un.ulpWord[0],
1943 irsp->un.ulpWord[1],
1944 irsp->un.ulpWord[2],
1945 irsp->un.ulpWord[3],
1946 irsp->un.ulpWord[4],
1947 irsp->un.ulpWord[5],
1948 *(((uint32_t *) irsp) + 6),
1949 *(((uint32_t *) irsp) + 7));
dea31012005-04-17 16:05:31 -05001950 }
1951
1952 switch (type) {
1953 case LPFC_ABORT_IOCB:
1954 case LPFC_SOL_IOCB:
1955 /*
1956 * Idle exchange closed via ABTS from port. No iocb
1957 * resources need to be recovered.
1958 */
1959 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001960 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001961 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04001962 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05001963 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04001964 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05001965 break;
1966 }
1967
James Bottomley604a3e32005-10-29 10:28:33 -05001968 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1969 &rspiocbq);
dea31012005-04-17 16:05:31 -05001970 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001971 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1972 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1973 &rspiocbq);
1974 } else {
James Smart2e0fef82007-06-17 19:56:36 -05001975 spin_unlock_irqrestore(&phba->hbalock,
1976 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001977 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1978 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001979 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001980 iflag);
1981 }
dea31012005-04-17 16:05:31 -05001982 }
1983 break;
James Smarta4bc3372006-12-02 13:34:16 -05001984 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05001985 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001986 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001987 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001988 break;
dea31012005-04-17 16:05:31 -05001989 default:
1990 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1991 char adaptermsg[LPFC_MAX_ADPTMSG];
1992 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1993 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1994 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001995 dev_warn(&((phba->pcidev)->dev),
1996 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05001997 phba->brd_no, adaptermsg);
1998 } else {
1999 /* Unknown IOCB command */
2000 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002001 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002002 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002003 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002004 irsp->ulpStatus,
2005 irsp->ulpIoTag,
2006 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002007 }
2008 break;
2009 }
2010
2011 /*
2012 * The response IOCB has been processed. Update the ring
2013 * pointer in SLIM. If the port response put pointer has not
2014 * been updated, sync the pgp->rspPutInx and fetch the new port
2015 * response put pointer.
2016 */
James Smarted957682007-06-17 19:56:37 -05002017 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002018
2019 if (pring->rspidx == portRspPut)
2020 portRspPut = le32_to_cpu(pgp->rspPutInx);
2021 }
2022
2023 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2024 pring->stats.iocb_rsp_full++;
2025 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2026 writel(status, phba->CAregaddr);
2027 readl(phba->CAregaddr);
2028 }
2029 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2030 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2031 pring->stats.iocb_cmd_empty++;
2032
2033 /* Force update of the local copy of cmdGetInx */
2034 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2035 lpfc_sli_resume_iocb(phba, pring);
2036
2037 if ((pring->lpfc_sli_cmd_available))
2038 (pring->lpfc_sli_cmd_available) (phba, pring);
2039
2040 }
2041
James Smart2e0fef82007-06-17 19:56:36 -05002042 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002043 return rc;
2044}
2045
James Smarte59058c2008-08-24 21:49:00 -04002046/**
2047 * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
2048 * @phba: Pointer to HBA context object.
2049 * @pring: Pointer to driver SLI ring object.
2050 * @mask: Host attention register mask for this ring.
2051 *
2052 * This function is called from the worker thread when there is a ring
2053 * event for non-fcp rings. The caller does not hold any lock .
2054 * The function processes each response iocb in the response ring until it
2055 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2056 * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
2057 * response iocb indicates a completion of a command iocb. The function
2058 * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
2059 * iocb. The function frees the resources or calls the completion handler if
2060 * this iocb is an abort completion. The function returns 0 when the allocated
2061 * iocbs are not freed, otherwise returns 1.
2062 **/
dea31012005-04-17 16:05:31 -05002063int
James Smart2e0fef82007-06-17 19:56:36 -05002064lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2065 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002066{
James Smarted957682007-06-17 19:56:37 -05002067 struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
2068 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
2069 &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05002070 IOCB_t *entry;
2071 IOCB_t *irsp = NULL;
2072 struct lpfc_iocbq *rspiocbp = NULL;
2073 struct lpfc_iocbq *next_iocb;
2074 struct lpfc_iocbq *cmdiocbp;
2075 struct lpfc_iocbq *saveq;
dea31012005-04-17 16:05:31 -05002076 uint8_t iocb_cmd_type;
2077 lpfc_iocb_type type;
2078 uint32_t status, free_saveq;
2079 uint32_t portRspPut, portRspMax;
2080 int rc = 1;
2081 unsigned long iflag;
dea31012005-04-17 16:05:31 -05002082
James Smart2e0fef82007-06-17 19:56:36 -05002083 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002084 pring->stats.iocb_event++;
2085
dea31012005-04-17 16:05:31 -05002086 /*
2087 * The next available response entry should never exceed the maximum
2088 * entries. If it does, treat it as an adapter hardware error.
2089 */
2090 portRspMax = pring->numRiocb;
2091 portRspPut = le32_to_cpu(pgp->rspPutInx);
2092 if (portRspPut >= portRspMax) {
2093 /*
2094 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
2095 * rsp ring <portRspMax>
2096 */
James Smarted957682007-06-17 19:56:37 -05002097 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002098 "0303 Ring %d handler: portRspPut %d "
dea31012005-04-17 16:05:31 -05002099 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002100 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002101
James Smart2e0fef82007-06-17 19:56:36 -05002102 phba->link_state = LPFC_HBA_ERROR;
2103 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002104
2105 phba->work_hs = HS_FFER3;
2106 lpfc_handle_eratt(phba);
2107
2108 return 1;
2109 }
2110
2111 rmb();
dea31012005-04-17 16:05:31 -05002112 while (pring->rspidx != portRspPut) {
2113 /*
2114 * Build a completion list and call the appropriate handler.
2115 * The process is to get the next available response iocb, get
2116 * a free iocb from the list, copy the response data into the
2117 * free iocb, insert to the continuation list, and update the
2118 * next response index to slim. This process makes response
2119 * iocb's in the ring available to DMA as fast as possible but
2120 * pays a penalty for a copy operation. Since the iocb is
2121 * only 32 bytes, this penalty is considered small relative to
2122 * the PCI reads for register values and a slim write. When
2123 * the ulpLe field is set, the entire Command has been
2124 * received.
2125 */
James Smarted957682007-06-17 19:56:37 -05002126 entry = lpfc_resp_iocb(phba, pring);
2127
James Smart858c9f62007-06-17 19:56:39 -05002128 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002129 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002130 if (rspiocbp == NULL) {
2131 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002132 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002133 break;
2134 }
2135
James Smarted957682007-06-17 19:56:37 -05002136 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2137 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002138 irsp = &rspiocbp->iocb;
2139
2140 if (++pring->rspidx >= portRspMax)
2141 pring->rspidx = 0;
2142
James Smarta58cbd52007-08-02 11:09:43 -04002143 if (pring->ringno == LPFC_ELS_RING) {
2144 lpfc_debugfs_slow_ring_trc(phba,
2145 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2146 *(((uint32_t *) irsp) + 4),
2147 *(((uint32_t *) irsp) + 6),
2148 *(((uint32_t *) irsp) + 7));
2149 }
2150
James Smarted957682007-06-17 19:56:37 -05002151 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002152
James Smart9c2face2008-01-11 01:53:18 -05002153 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
dea31012005-04-17 16:05:31 -05002154
2155 pring->iocb_continueq_cnt++;
2156 if (irsp->ulpLe) {
2157 /*
2158 * By default, the driver expects to free all resources
2159 * associated with this iocb completion.
2160 */
2161 free_saveq = 1;
2162 saveq = list_get_first(&pring->iocb_continueq,
2163 struct lpfc_iocbq, list);
2164 irsp = &(saveq->iocb);
2165 list_del_init(&pring->iocb_continueq);
2166 pring->iocb_continueq_cnt = 0;
2167
2168 pring->stats.iocb_rsp++;
2169
James Smart92d7f7b2007-06-17 19:56:38 -05002170 /*
2171 * If resource errors reported from HBA, reduce
2172 * queuedepths of the SCSI device.
2173 */
2174 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2175 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2176 spin_unlock_irqrestore(&phba->hbalock, iflag);
2177 lpfc_adjust_queue_depth(phba);
2178 spin_lock_irqsave(&phba->hbalock, iflag);
2179 }
2180
dea31012005-04-17 16:05:31 -05002181 if (irsp->ulpStatus) {
2182 /* Rsp ring <ringno> error: IOCB */
James Smarted957682007-06-17 19:56:37 -05002183 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002184 "0328 Rsp Ring %d error: "
James Smarted957682007-06-17 19:56:37 -05002185 "IOCB Data: "
2186 "x%x x%x x%x x%x "
2187 "x%x x%x x%x x%x "
2188 "x%x x%x x%x x%x "
2189 "x%x x%x x%x x%x\n",
James Smarted957682007-06-17 19:56:37 -05002190 pring->ringno,
2191 irsp->un.ulpWord[0],
2192 irsp->un.ulpWord[1],
2193 irsp->un.ulpWord[2],
2194 irsp->un.ulpWord[3],
2195 irsp->un.ulpWord[4],
2196 irsp->un.ulpWord[5],
2197 *(((uint32_t *) irsp) + 6),
2198 *(((uint32_t *) irsp) + 7),
2199 *(((uint32_t *) irsp) + 8),
2200 *(((uint32_t *) irsp) + 9),
2201 *(((uint32_t *) irsp) + 10),
2202 *(((uint32_t *) irsp) + 11),
2203 *(((uint32_t *) irsp) + 12),
2204 *(((uint32_t *) irsp) + 13),
2205 *(((uint32_t *) irsp) + 14),
2206 *(((uint32_t *) irsp) + 15));
dea31012005-04-17 16:05:31 -05002207 }
2208
2209 /*
2210 * Fetch the IOCB command type and call the correct
2211 * completion routine. Solicited and Unsolicited
2212 * IOCBs on the ELS ring get freed back to the
2213 * lpfc_iocb_list by the discovery kernel thread.
2214 */
2215 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2216 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2217 if (type == LPFC_SOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002218 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002219 rc = lpfc_sli_process_sol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002220 saveq);
2221 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002222 } else if (type == LPFC_UNSOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002223 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002224 rc = lpfc_sli_process_unsol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002225 saveq);
2226 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9c2face2008-01-11 01:53:18 -05002227 if (!rc)
2228 free_saveq = 0;
dea31012005-04-17 16:05:31 -05002229 } else if (type == LPFC_ABORT_IOCB) {
2230 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
2231 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05002232 lpfc_sli_iocbq_lookup(phba, pring,
2233 saveq)))) {
dea31012005-04-17 16:05:31 -05002234 /* Call the specified completion
2235 routine */
2236 if (cmdiocbp->iocb_cmpl) {
2237 spin_unlock_irqrestore(
James Smart2e0fef82007-06-17 19:56:36 -05002238 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002239 iflag);
2240 (cmdiocbp->iocb_cmpl) (phba,
2241 cmdiocbp, saveq);
2242 spin_lock_irqsave(
James Smart2e0fef82007-06-17 19:56:36 -05002243 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002244 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002245 } else
James Smart2e0fef82007-06-17 19:56:36 -05002246 __lpfc_sli_release_iocbq(phba,
James Bottomley604a3e32005-10-29 10:28:33 -05002247 cmdiocbp);
dea31012005-04-17 16:05:31 -05002248 }
2249 } else if (type == LPFC_UNKNOWN_IOCB) {
2250 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2251
2252 char adaptermsg[LPFC_MAX_ADPTMSG];
2253
2254 memset(adaptermsg, 0,
2255 LPFC_MAX_ADPTMSG);
2256 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2257 MAX_MSG_DATA);
2258 dev_warn(&((phba->pcidev)->dev),
Joe Perches898eb712007-10-18 03:06:30 -07002259 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002260 phba->brd_no, adaptermsg);
2261 } else {
2262 /* Unknown IOCB command */
James Smart92d7f7b2007-06-17 19:56:38 -05002263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002264 "0335 Unknown IOCB "
James Smart92d7f7b2007-06-17 19:56:38 -05002265 "command Data: x%x "
2266 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002267 irsp->ulpCommand,
2268 irsp->ulpStatus,
2269 irsp->ulpIoTag,
2270 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002271 }
2272 }
2273
2274 if (free_saveq) {
James Smart2e0fef82007-06-17 19:56:36 -05002275 list_for_each_entry_safe(rspiocbp, next_iocb,
2276 &saveq->list, list) {
2277 list_del(&rspiocbp->list);
2278 __lpfc_sli_release_iocbq(phba,
2279 rspiocbp);
dea31012005-04-17 16:05:31 -05002280 }
James Smart2e0fef82007-06-17 19:56:36 -05002281 __lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05002282 }
James Smart92d7f7b2007-06-17 19:56:38 -05002283 rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002284 }
2285
2286 /*
2287 * If the port response put pointer has not been updated, sync
2288 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2289 * response put pointer.
2290 */
2291 if (pring->rspidx == portRspPut) {
2292 portRspPut = le32_to_cpu(pgp->rspPutInx);
2293 }
2294 } /* while (pring->rspidx != portRspPut) */
2295
James Smart92d7f7b2007-06-17 19:56:38 -05002296 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002297 /* At least one response entry has been freed */
2298 pring->stats.iocb_rsp_full++;
2299 /* SET RxRE_RSP in Chip Att register */
2300 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2301 writel(status, phba->CAregaddr);
2302 readl(phba->CAregaddr); /* flush */
2303 }
2304 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2305 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2306 pring->stats.iocb_cmd_empty++;
2307
2308 /* Force update of the local copy of cmdGetInx */
2309 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2310 lpfc_sli_resume_iocb(phba, pring);
2311
2312 if ((pring->lpfc_sli_cmd_available))
2313 (pring->lpfc_sli_cmd_available) (phba, pring);
2314
2315 }
2316
James Smart2e0fef82007-06-17 19:56:36 -05002317 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002318 return rc;
2319}
2320
James Smarte59058c2008-08-24 21:49:00 -04002321/**
2322 * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
2323 * @phba: Pointer to HBA context object.
2324 * @pring: Pointer to driver SLI ring object.
2325 *
2326 * This function aborts all iocbs in the given ring and frees all the iocb
2327 * objects in txq. This function issues an abort iocb for all the iocb commands
2328 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2329 * the return of this function. The caller is not required to hold any locks.
2330 **/
James Smart2e0fef82007-06-17 19:56:36 -05002331void
dea31012005-04-17 16:05:31 -05002332lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2333{
James Smart2534ba72007-04-25 09:52:20 -04002334 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002335 struct lpfc_iocbq *iocb, *next_iocb;
James Smart2534ba72007-04-25 09:52:20 -04002336 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05002337
James Smart92d7f7b2007-06-17 19:56:38 -05002338 if (pring->ringno == LPFC_ELS_RING) {
2339 lpfc_fabric_abort_hba(phba);
2340 }
2341
dea31012005-04-17 16:05:31 -05002342 /* Error everything on txq and txcmplq
2343 * First do the txq.
2344 */
James Smart2e0fef82007-06-17 19:56:36 -05002345 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002346 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002347 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002348
2349 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002350 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2351 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2352
James Smart2e0fef82007-06-17 19:56:36 -05002353 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002354
2355 while (!list_empty(&completions)) {
2356 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
dea31012005-04-17 16:05:31 -05002357 cmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05002358 list_del_init(&iocb->list);
dea31012005-04-17 16:05:31 -05002359
James Smart2e0fef82007-06-17 19:56:36 -05002360 if (!iocb->iocb_cmpl)
2361 lpfc_sli_release_iocbq(phba, iocb);
2362 else {
dea31012005-04-17 16:05:31 -05002363 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2364 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
dea31012005-04-17 16:05:31 -05002365 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002366 }
dea31012005-04-17 16:05:31 -05002367 }
dea31012005-04-17 16:05:31 -05002368}
2369
James Smarte59058c2008-08-24 21:49:00 -04002370/**
2371 * lpfc_sli_brdready: Check for host status bits.
2372 * @phba: Pointer to HBA context object.
2373 * @mask: Bit mask to be checked.
2374 *
2375 * This function reads the host status register and compares
2376 * with the provided bit mask to check if HBA completed
2377 * the restart. This function will wait in a loop for the
2378 * HBA to complete restart. If the HBA does not restart within
2379 * 15 iterations, the function will reset the HBA again. The
2380 * function returns 1 when HBA fail to restart otherwise returns
2381 * zero.
2382 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002383int
James Smart2e0fef82007-06-17 19:56:36 -05002384lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05002385{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002386 uint32_t status;
2387 int i = 0;
2388 int retval = 0;
dea31012005-04-17 16:05:31 -05002389
Jamie Wellnitz41415862006-02-28 19:25:27 -05002390 /* Read the HBA Host Status Register */
2391 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002392
Jamie Wellnitz41415862006-02-28 19:25:27 -05002393 /*
2394 * Check status register every 100ms for 5 retries, then every
2395 * 500ms for 5, then every 2.5 sec for 5, then reset board and
2396 * every 2.5 sec for 4.
2397 * Break our of the loop if errors occurred during init.
2398 */
2399 while (((status & mask) != mask) &&
2400 !(status & HS_FFERM) &&
2401 i++ < 20) {
dea31012005-04-17 16:05:31 -05002402
Jamie Wellnitz41415862006-02-28 19:25:27 -05002403 if (i <= 5)
2404 msleep(10);
2405 else if (i <= 10)
2406 msleep(500);
2407 else
2408 msleep(2500);
dea31012005-04-17 16:05:31 -05002409
Jamie Wellnitz41415862006-02-28 19:25:27 -05002410 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002411 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002412 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002413 lpfc_sli_brdrestart(phba);
2414 }
2415 /* Read the HBA Host Status Register */
2416 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002417 }
dea31012005-04-17 16:05:31 -05002418
Jamie Wellnitz41415862006-02-28 19:25:27 -05002419 /* Check to see if any errors occurred during init */
2420 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05002421 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002422 retval = 1;
2423 }
dea31012005-04-17 16:05:31 -05002424
Jamie Wellnitz41415862006-02-28 19:25:27 -05002425 return retval;
dea31012005-04-17 16:05:31 -05002426}
2427
James Smart92908312006-03-07 15:04:13 -05002428#define BARRIER_TEST_PATTERN (0xdeadbeef)
2429
James Smarte59058c2008-08-24 21:49:00 -04002430/**
2431 * lpfc_reset_barrier: Make HBA ready for HBA reset.
2432 * @phba: Pointer to HBA context object.
2433 *
2434 * This function is called before resetting an HBA. This
2435 * function requests HBA to quiesce DMAs before a reset.
2436 **/
James Smart2e0fef82007-06-17 19:56:36 -05002437void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05002438{
James Smart65a29c12006-07-06 15:50:50 -04002439 uint32_t __iomem *resp_buf;
2440 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05002441 volatile uint32_t mbox;
2442 uint32_t hc_copy;
2443 int i;
2444 uint8_t hdrtype;
2445
2446 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2447 if (hdrtype != 0x80 ||
2448 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2449 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2450 return;
2451
2452 /*
2453 * Tell the other part of the chip to suspend temporarily all
2454 * its DMA activity.
2455 */
James Smart65a29c12006-07-06 15:50:50 -04002456 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002457
2458 /* Disable the error attention */
2459 hc_copy = readl(phba->HCregaddr);
2460 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2461 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002462 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002463
2464 if (readl(phba->HAregaddr) & HA_ERATT) {
2465 /* Clear Chip error bit */
2466 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002467 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002468 }
2469
2470 mbox = 0;
2471 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2472 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2473
2474 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04002475 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002476 writel(mbox, mbox_buf);
2477
2478 for (i = 0;
2479 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2480 mdelay(1);
2481
2482 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2483 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05002484 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05002485 goto restore_hc;
2486 else
2487 goto clear_errat;
2488 }
2489
2490 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2491 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
2492 mdelay(1);
2493
2494clear_errat:
2495
2496 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2497 mdelay(1);
2498
2499 if (readl(phba->HAregaddr) & HA_ERATT) {
2500 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002501 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002502 }
2503
2504restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05002505 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002506 writel(hc_copy, phba->HCregaddr);
2507 readl(phba->HCregaddr); /* flush */
2508}
2509
James Smarte59058c2008-08-24 21:49:00 -04002510/**
2511 * lpfc_sli_brdkill: Issue a kill_board mailbox command.
2512 * @phba: Pointer to HBA context object.
2513 *
2514 * This function issues a kill_board mailbox command and waits for
2515 * the error attention interrupt. This function is called for stopping
2516 * the firmware processing. The caller is not required to hold any
2517 * locks. This function calls lpfc_hba_down_post function to free
2518 * any pending commands after the kill. The function will return 1 when it
2519 * fails to kill the board else will return 0.
2520 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002521int
James Smart2e0fef82007-06-17 19:56:36 -05002522lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002523{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002524 struct lpfc_sli *psli;
2525 LPFC_MBOXQ_t *pmb;
2526 uint32_t status;
2527 uint32_t ha_copy;
2528 int retval;
2529 int i = 0;
2530
2531 psli = &phba->sli;
2532
2533 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05002534 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002535 "0329 Kill HBA Data: x%x x%x\n",
2536 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002537
James Smart98c9ea52007-10-27 13:37:33 -04002538 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2539 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002540 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002541
2542 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05002543 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002544 status = readl(phba->HCregaddr);
2545 status &= ~HC_ERINT_ENA;
2546 writel(status, phba->HCregaddr);
2547 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002548 phba->link_flag |= LS_IGNORE_ERATT;
2549 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002550
2551 lpfc_kill_board(phba, pmb);
2552 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2553 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2554
2555 if (retval != MBX_SUCCESS) {
2556 if (retval != MBX_BUSY)
2557 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002558 spin_lock_irq(&phba->hbalock);
2559 phba->link_flag &= ~LS_IGNORE_ERATT;
2560 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002561 return 1;
2562 }
2563
James Smart92908312006-03-07 15:04:13 -05002564 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2565
Jamie Wellnitz41415862006-02-28 19:25:27 -05002566 mempool_free(pmb, phba->mbox_mem_pool);
2567
2568 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2569 * attention every 100ms for 3 seconds. If we don't get ERATT after
2570 * 3 seconds we still set HBA_ERROR state because the status of the
2571 * board is now undefined.
2572 */
2573 ha_copy = readl(phba->HAregaddr);
2574
2575 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2576 mdelay(100);
2577 ha_copy = readl(phba->HAregaddr);
2578 }
2579
2580 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05002581 if (ha_copy & HA_ERATT) {
2582 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002583 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002584 }
James Smart2e0fef82007-06-17 19:56:36 -05002585 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002586 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002587 phba->link_flag &= ~LS_IGNORE_ERATT;
2588 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002589
2590 psli->mbox_active = NULL;
2591 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002592 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002593
James Smart2e0fef82007-06-17 19:56:36 -05002594 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002595}
2596
James Smarte59058c2008-08-24 21:49:00 -04002597/**
2598 * lpfc_sli_brdreset: Reset the HBA.
2599 * @phba: Pointer to HBA context object.
2600 *
2601 * This function resets the HBA by writing HC_INITFF to the control
2602 * register. After the HBA resets, this function resets all the iocb ring
2603 * indices. This function disables PCI layer parity checking during
2604 * the reset.
2605 * This function returns 0 always.
2606 * The caller is not required to hold any locks.
2607 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002608int
James Smart2e0fef82007-06-17 19:56:36 -05002609lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002610{
2611 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002612 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002613 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05002614 int i;
dea31012005-04-17 16:05:31 -05002615
Jamie Wellnitz41415862006-02-28 19:25:27 -05002616 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002617
Jamie Wellnitz41415862006-02-28 19:25:27 -05002618 /* Reset HBA */
2619 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002620 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002621 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05002622
2623 /* perform board reset */
2624 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002625 phba->pport->fc_myDID = 0;
2626 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002627
Jamie Wellnitz41415862006-02-28 19:25:27 -05002628 /* Turn off parity checking and serr during the physical reset */
2629 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2630 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2631 (cfg_value &
2632 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2633
James Smart1c067a42006-08-01 07:33:52 -04002634 psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002635 /* Now toggle INITFF bit in the Host Control Register */
2636 writel(HC_INITFF, phba->HCregaddr);
2637 mdelay(1);
2638 readl(phba->HCregaddr); /* flush */
2639 writel(0, phba->HCregaddr);
2640 readl(phba->HCregaddr); /* flush */
2641
2642 /* Restore PCI cmd register */
2643 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002644
2645 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002646 for (i = 0; i < psli->num_rings; i++) {
2647 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002648 pring->flag = 0;
2649 pring->rspidx = 0;
2650 pring->next_cmdidx = 0;
2651 pring->local_getidx = 0;
2652 pring->cmdidx = 0;
2653 pring->missbufcnt = 0;
2654 }
dea31012005-04-17 16:05:31 -05002655
James Smart2e0fef82007-06-17 19:56:36 -05002656 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002657 return 0;
2658}
2659
James Smarte59058c2008-08-24 21:49:00 -04002660/**
2661 * lpfc_sli_brdrestart: Restart the HBA.
2662 * @phba: Pointer to HBA context object.
2663 *
2664 * This function is called in the SLI initialization code path to
2665 * restart the HBA. The caller is not required to hold any lock.
2666 * This function writes MBX_RESTART mailbox command to the SLIM and
2667 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
2668 * function to free any pending commands. The function enables
2669 * POST only during the first initialization. The function returns zero.
2670 * The function does not guarantee completion of MBX_RESTART mailbox
2671 * command before the return of this function.
2672 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002673int
James Smart2e0fef82007-06-17 19:56:36 -05002674lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002675{
2676 MAILBOX_t *mb;
2677 struct lpfc_sli *psli;
2678 uint16_t skip_post;
2679 volatile uint32_t word0;
2680 void __iomem *to_slim;
2681
James Smart2e0fef82007-06-17 19:56:36 -05002682 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002683
2684 psli = &phba->sli;
2685
2686 /* Restart HBA */
2687 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002688 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002689 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002690
2691 word0 = 0;
2692 mb = (MAILBOX_t *) &word0;
2693 mb->mbxCommand = MBX_RESTART;
2694 mb->mbxHc = 1;
2695
James Smart92908312006-03-07 15:04:13 -05002696 lpfc_reset_barrier(phba);
2697
Jamie Wellnitz41415862006-02-28 19:25:27 -05002698 to_slim = phba->MBslimaddr;
2699 writel(*(uint32_t *) mb, to_slim);
2700 readl(to_slim); /* flush */
2701
2702 /* Only skip post after fc_ffinit is completed */
James Smart2e0fef82007-06-17 19:56:36 -05002703 if (phba->pport->port_state) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002704 skip_post = 1;
2705 word0 = 1; /* This is really setting up word1 */
dea31012005-04-17 16:05:31 -05002706 } else {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002707 skip_post = 0;
2708 word0 = 0; /* This is really setting up word1 */
2709 }
James Smart65a29c12006-07-06 15:50:50 -04002710 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002711 writel(*(uint32_t *) mb, to_slim);
2712 readl(to_slim); /* flush */
2713
2714 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002715 phba->pport->stopped = 0;
2716 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002717
James Smart2e0fef82007-06-17 19:56:36 -05002718 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002719
James Smart64ba8812006-08-02 15:24:34 -04002720 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2721 psli->stats_start = get_seconds();
2722
Jamie Wellnitz41415862006-02-28 19:25:27 -05002723 if (skip_post)
2724 mdelay(100);
2725 else
dea31012005-04-17 16:05:31 -05002726 mdelay(2000);
dea31012005-04-17 16:05:31 -05002727
Jamie Wellnitz41415862006-02-28 19:25:27 -05002728 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002729
2730 return 0;
2731}
2732
James Smarte59058c2008-08-24 21:49:00 -04002733/**
2734 * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
2735 * @phba: Pointer to HBA context object.
2736 *
2737 * This function is called after a HBA restart to wait for successful
2738 * restart of the HBA. Successful restart of the HBA is indicated by
2739 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
2740 * iteration, the function will restart the HBA again. The function returns
2741 * zero if HBA successfully restarted else returns negative error code.
2742 **/
dea31012005-04-17 16:05:31 -05002743static int
2744lpfc_sli_chipset_init(struct lpfc_hba *phba)
2745{
2746 uint32_t status, i = 0;
2747
2748 /* Read the HBA Host Status Register */
2749 status = readl(phba->HSregaddr);
2750
2751 /* Check status register to see what current state is */
2752 i = 0;
2753 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2754
2755 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2756 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2757 * 4.
2758 */
2759 if (i++ >= 20) {
2760 /* Adapter failed to init, timeout, status reg
2761 <status> */
James Smarted957682007-06-17 19:56:37 -05002762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002763 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002764 "timeout, status reg x%x, "
2765 "FW Data: A8 x%x AC x%x\n", status,
2766 readl(phba->MBslimaddr + 0xa8),
2767 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002768 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002769 return -ETIMEDOUT;
2770 }
2771
2772 /* Check to see if any errors occurred during init */
2773 if (status & HS_FFERM) {
2774 /* ERROR: During chipset initialization */
2775 /* Adapter failed to init, chipset, status reg
2776 <status> */
James Smarted957682007-06-17 19:56:37 -05002777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002778 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002779 "chipset, status reg x%x, "
2780 "FW Data: A8 x%x AC x%x\n", status,
2781 readl(phba->MBslimaddr + 0xa8),
2782 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002783 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002784 return -EIO;
2785 }
2786
2787 if (i <= 5) {
2788 msleep(10);
2789 } else if (i <= 10) {
2790 msleep(500);
2791 } else {
2792 msleep(2500);
2793 }
2794
2795 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002796 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002797 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002798 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002799 }
2800 /* Read the HBA Host Status Register */
2801 status = readl(phba->HSregaddr);
2802 }
2803
2804 /* Check to see if any errors occurred during init */
2805 if (status & HS_FFERM) {
2806 /* ERROR: During chipset initialization */
2807 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05002808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002809 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05002810 "status reg x%x, "
2811 "FW Data: A8 x%x AC x%x\n", status,
2812 readl(phba->MBslimaddr + 0xa8),
2813 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002814 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002815 return -EIO;
2816 }
2817
2818 /* Clear all interrupt enable conditions */
2819 writel(0, phba->HCregaddr);
2820 readl(phba->HCregaddr); /* flush */
2821
2822 /* setup host attn register */
2823 writel(0xffffffff, phba->HAregaddr);
2824 readl(phba->HAregaddr); /* flush */
2825 return 0;
2826}
2827
James Smarte59058c2008-08-24 21:49:00 -04002828/**
2829 * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
2830 *
2831 * This function calculates and returns the number of HBQs required to be
2832 * configured.
2833 **/
James Smart78b2d852007-08-02 11:10:21 -04002834int
James Smarted957682007-06-17 19:56:37 -05002835lpfc_sli_hbq_count(void)
2836{
James Smart92d7f7b2007-06-17 19:56:38 -05002837 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05002838}
2839
James Smarte59058c2008-08-24 21:49:00 -04002840/**
2841 * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
2842 *
2843 * This function adds the number of hbq entries in every HBQ to get
2844 * the total number of hbq entries required for the HBA and returns
2845 * the total count.
2846 **/
James Smarted957682007-06-17 19:56:37 -05002847static int
2848lpfc_sli_hbq_entry_count(void)
2849{
2850 int hbq_count = lpfc_sli_hbq_count();
2851 int count = 0;
2852 int i;
2853
2854 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05002855 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05002856 return count;
2857}
2858
James Smarte59058c2008-08-24 21:49:00 -04002859/**
2860 * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
2861 *
2862 * This function calculates amount of memory required for all hbq entries
2863 * to be configured and returns the total memory required.
2864 **/
dea31012005-04-17 16:05:31 -05002865int
James Smarted957682007-06-17 19:56:37 -05002866lpfc_sli_hbq_size(void)
2867{
2868 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2869}
2870
James Smarte59058c2008-08-24 21:49:00 -04002871/**
2872 * lpfc_sli_hbq_setup: configure and initialize HBQs.
2873 * @phba: Pointer to HBA context object.
2874 *
2875 * This function is called during the SLI initialization to configure
2876 * all the HBQs and post buffers to the HBQ. The caller is not
2877 * required to hold any locks. This function will return zero if successful
2878 * else it will return negative error code.
2879 **/
James Smarted957682007-06-17 19:56:37 -05002880static int
2881lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2882{
2883 int hbq_count = lpfc_sli_hbq_count();
2884 LPFC_MBOXQ_t *pmb;
2885 MAILBOX_t *pmbox;
2886 uint32_t hbqno;
2887 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05002888
James Smart92d7f7b2007-06-17 19:56:38 -05002889 /* Get a Mailbox buffer to setup mailbox
2890 * commands for HBA initialization
2891 */
James Smarted957682007-06-17 19:56:37 -05002892 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2893
2894 if (!pmb)
2895 return -ENOMEM;
2896
2897 pmbox = &pmb->mb;
2898
2899 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2900 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05002901 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05002902
2903 hbq_entry_index = 0;
2904 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2905 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2906 phba->hbqs[hbqno].hbqPutIdx = 0;
2907 phba->hbqs[hbqno].local_hbqGetIdx = 0;
2908 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05002909 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04002910 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2911 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05002912 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2913
2914 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2915 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2916 mbxStatus <status>, ring <num> */
2917
2918 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002919 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002920 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05002921 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002922 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05002923 pmbox->mbxStatus, hbqno);
2924
2925 phba->link_state = LPFC_HBA_ERROR;
2926 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05002927 return ENXIO;
2928 }
2929 }
2930 phba->hbq_count = hbq_count;
2931
James Smarted957682007-06-17 19:56:37 -05002932 mempool_free(pmb, phba->mbox_mem_pool);
2933
James Smart92d7f7b2007-06-17 19:56:38 -05002934 /* Initially populate or replenish the HBQs */
2935 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2936 if (lpfc_sli_hbqbuf_init_hbqs(phba, hbqno))
2937 return -ENOMEM;
2938 }
James Smarted957682007-06-17 19:56:37 -05002939 return 0;
2940}
2941
James Smarte59058c2008-08-24 21:49:00 -04002942/**
2943 * lpfc_do_config_port: Issue config port mailbox command.
2944 * @phba: Pointer to HBA context object.
2945 * @sli_mode: sli mode - 2/3
2946 *
2947 * This function is called by the sli intialization code path
2948 * to issue config_port mailbox command. This function restarts the
2949 * HBA firmware and issues a config_port mailbox command to configure
2950 * the SLI interface in the sli mode specified by sli_mode
2951 * variable. The caller is not required to hold any locks.
2952 * The function returns 0 if successful, else returns negative error
2953 * code.
2954 **/
James Smarted957682007-06-17 19:56:37 -05002955static int
2956lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05002957{
2958 LPFC_MBOXQ_t *pmb;
2959 uint32_t resetcount = 0, rc = 0, done = 0;
2960
2961 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2962 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05002963 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002964 return -ENOMEM;
2965 }
2966
James Smarted957682007-06-17 19:56:37 -05002967 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05002968 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05002969 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002970 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002971 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002972 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002973 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002974 msleep(2500);
2975 rc = lpfc_sli_chipset_init(phba);
2976 if (rc)
2977 break;
2978
James Smart2e0fef82007-06-17 19:56:36 -05002979 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002980 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002981 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002982 resetcount++;
2983
James Smarted957682007-06-17 19:56:37 -05002984 /* Call pre CONFIG_PORT mailbox command initialization. A
2985 * value of 0 means the call was successful. Any other
2986 * nonzero value is a failure, but if ERESTART is returned,
2987 * the driver may reset the HBA and try again.
2988 */
dea31012005-04-17 16:05:31 -05002989 rc = lpfc_config_port_prep(phba);
2990 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05002991 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05002992 continue;
2993 } else if (rc) {
2994 break;
2995 }
2996
James Smart2e0fef82007-06-17 19:56:36 -05002997 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05002998 lpfc_config_port(phba, pmb);
2999 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smarted957682007-06-17 19:56:37 -05003000 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003001 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003002 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003003 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003004 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003005 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003006 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003007 spin_unlock_irq(&phba->hbalock);
3008 rc = -ENXIO;
James Smarted957682007-06-17 19:56:37 -05003009 } else {
3010 done = 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003011 phba->max_vpi = (phba->max_vpi &&
3012 pmb->mb.un.varCfgPort.gmv) != 0
3013 ? pmb->mb.un.varCfgPort.max_vpi
3014 : 0;
dea31012005-04-17 16:05:31 -05003015 }
3016 }
James Smarted957682007-06-17 19:56:37 -05003017
3018 if (!done) {
3019 rc = -EINVAL;
3020 goto do_prep_failed;
3021 }
3022
3023 if ((pmb->mb.un.varCfgPort.sli_mode == 3) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003024 (!pmb->mb.un.varCfgPort.cMA)) {
James Smarted957682007-06-17 19:56:37 -05003025 rc = -ENXIO;
James Smarted957682007-06-17 19:56:37 -05003026 }
James Smarted957682007-06-17 19:56:37 -05003027
James Smart92d7f7b2007-06-17 19:56:38 -05003028do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003029 mempool_free(pmb, phba->mbox_mem_pool);
3030 return rc;
3031}
3032
James Smarte59058c2008-08-24 21:49:00 -04003033
3034/**
3035 * lpfc_sli_hba_setup: SLI intialization function.
3036 * @phba: Pointer to HBA context object.
3037 *
3038 * This function is the main SLI intialization function. This function
3039 * is called by the HBA intialization code, HBA reset code and HBA
3040 * error attention handler code. Caller is not required to hold any
3041 * locks. This function issues config_port mailbox command to configure
3042 * the SLI, setup iocb rings and HBQ rings. In the end the function
3043 * calls the config_port_post function to issue init_link mailbox
3044 * command and to start the discovery. The function will return zero
3045 * if successful, else it will return negative error code.
3046 **/
James Smarted957682007-06-17 19:56:37 -05003047int
3048lpfc_sli_hba_setup(struct lpfc_hba *phba)
3049{
3050 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003051 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003052
3053 switch (lpfc_sli_mode) {
3054 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003055 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003056 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003057 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003058 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003059 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003060 break;
3061 }
James Smarted957682007-06-17 19:56:37 -05003062 mode = 2;
3063 break;
3064 case 0:
3065 case 3:
3066 break;
3067 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003069 "1819 Unrecognized lpfc_sli_mode "
3070 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003071
3072 break;
3073 }
3074
3075 rc = lpfc_do_config_port(phba, mode);
3076 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003078 "1820 Unable to select SLI-3. "
3079 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003080 if (rc && mode != 2)
3081 rc = lpfc_do_config_port(phba, 2);
3082 if (rc)
dea31012005-04-17 16:05:31 -05003083 goto lpfc_sli_hba_setup_error;
3084
James Smarted957682007-06-17 19:56:37 -05003085 if (phba->sli_rev == 3) {
3086 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3087 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
3088 phba->sli3_options |= LPFC_SLI3_ENABLED;
3089 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3090
3091 } else {
3092 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3093 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003094 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003095 }
3096
3097 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003098 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3099 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05003100 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05003101
3102 if (rc)
3103 goto lpfc_sli_hba_setup_error;
3104
James Smart92d7f7b2007-06-17 19:56:38 -05003105 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05003106
3107 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3108 rc = lpfc_sli_hbq_setup(phba);
3109 if (rc)
3110 goto lpfc_sli_hba_setup_error;
3111 }
3112
dea31012005-04-17 16:05:31 -05003113 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3114
3115 rc = lpfc_config_port_post(phba);
3116 if (rc)
3117 goto lpfc_sli_hba_setup_error;
3118
James Smarted957682007-06-17 19:56:37 -05003119 return rc;
3120
James Smart92d7f7b2007-06-17 19:56:38 -05003121lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05003122 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05003123 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003124 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05003125 return rc;
3126}
3127
James Smarte59058c2008-08-24 21:49:00 -04003128
3129/**
3130 * lpfc_mbox_timeout: Timeout call back function for mbox timer.
3131 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05003132 *
James Smarte59058c2008-08-24 21:49:00 -04003133 * This is the callback function for mailbox timer. The mailbox
3134 * timer is armed when a new mailbox command is issued and the timer
3135 * is deleted when the mailbox complete. The function is called by
3136 * the kernel timer code when a mailbox does not complete within
3137 * expected time. This function wakes up the worker thread to
3138 * process the mailbox timeout and returns. All the processing is
3139 * done by the worker thread function lpfc_mbox_timeout_handler.
3140 **/
dea31012005-04-17 16:05:31 -05003141void
3142lpfc_mbox_timeout(unsigned long ptr)
3143{
James Smart92d7f7b2007-06-17 19:56:38 -05003144 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05003145 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05003146 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003147
James Smart2e0fef82007-06-17 19:56:36 -05003148 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05003149 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003150 if (!tmo_posted)
3151 phba->pport->work_port_events |= WORKER_MBOX_TMO;
3152 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
3153
James Smart5e9d9b82008-06-14 22:52:53 -04003154 if (!tmo_posted)
3155 lpfc_worker_wake_up(phba);
3156 return;
dea31012005-04-17 16:05:31 -05003157}
3158
James Smarte59058c2008-08-24 21:49:00 -04003159
3160/**
3161 * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
3162 * @phba: Pointer to HBA context object.
3163 *
3164 * This function is called from worker thread when a mailbox command times out.
3165 * The caller is not required to hold any locks. This function will reset the
3166 * HBA and recover all the pending commands.
3167 **/
dea31012005-04-17 16:05:31 -05003168void
3169lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
3170{
James Smart2e0fef82007-06-17 19:56:36 -05003171 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
3172 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04003173 struct lpfc_sli *psli = &phba->sli;
3174 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003175
James Smart2e0fef82007-06-17 19:56:36 -05003176 if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
dea31012005-04-17 16:05:31 -05003177 return;
3178 }
3179
dea31012005-04-17 16:05:31 -05003180 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05003181 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003182 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003183 mb->mbxCommand,
3184 phba->pport->port_state,
3185 phba->sli.sli_flag,
3186 phba->sli.mbox_active);
dea31012005-04-17 16:05:31 -05003187
James Smart1dcb58e2007-04-25 09:51:30 -04003188 /* Setting state unknown so lpfc_sli_abort_iocb_ring
3189 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
3190 * it to fail all oustanding SCSI IO.
3191 */
James Smart2e0fef82007-06-17 19:56:36 -05003192 spin_lock_irq(&phba->pport->work_port_lock);
3193 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3194 spin_unlock_irq(&phba->pport->work_port_lock);
3195 spin_lock_irq(&phba->hbalock);
3196 phba->link_state = LPFC_LINK_UNKNOWN;
James Smart1dcb58e2007-04-25 09:51:30 -04003197 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003198 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04003199
3200 pring = &psli->ring[psli->fcp_ring];
3201 lpfc_sli_abort_iocb_ring(phba, pring);
3202
3203 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003204 "0345 Resetting board due to mailbox timeout\n");
James Smart1dcb58e2007-04-25 09:51:30 -04003205 /*
3206 * lpfc_offline calls lpfc_sli_hba_down which will clean up
3207 * on oustanding mailbox commands.
3208 */
James Smart13815c82008-01-11 01:52:48 -05003209 /* If resets are disabled then set error state and return. */
3210 if (!phba->cfg_enable_hba_reset) {
3211 phba->link_state = LPFC_HBA_ERROR;
3212 return;
3213 }
James Smart1dcb58e2007-04-25 09:51:30 -04003214 lpfc_offline_prep(phba);
3215 lpfc_offline(phba);
3216 lpfc_sli_brdrestart(phba);
James Smart58da1ff2008-04-07 10:15:56 -04003217 lpfc_online(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04003218 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003219 return;
3220}
3221
James Smarte59058c2008-08-24 21:49:00 -04003222/**
3223 * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
3224 * @phba: Pointer to HBA context object.
3225 * @pmbox: Pointer to mailbox object.
3226 * @flag: Flag indicating how the mailbox need to be processed.
3227 *
3228 * This function is called by discovery code and HBA management code
3229 * to submit a mailbox command to firmware. This function gets the
3230 * hbalock to protect the data structures.
3231 * The mailbox command can be submitted in polling mode, in which case
3232 * this function will wait in a polling loop for the completion of the
3233 * mailbox.
3234 * If the mailbox is submitted in no_wait mode (not polling) the
3235 * function will submit the command and returns immediately without waiting
3236 * for the mailbox completion. The no_wait is supported only when HBA
3237 * is in SLI2/SLI3 mode - interrupts are enabled.
3238 * The SLI interface allows only one mailbox pending at a time. If the
3239 * mailbox is issued in polling mode and there is already a mailbox
3240 * pending, then the function will return an error. If the mailbox is issued
3241 * in NO_WAIT mode and there is a mailbox pending already, the function
3242 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
3243 * The sli layer owns the mailbox object until the completion of mailbox
3244 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
3245 * return codes the caller owns the mailbox command after the return of
3246 * the function.
3247 **/
dea31012005-04-17 16:05:31 -05003248int
James Smart2e0fef82007-06-17 19:56:36 -05003249lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
dea31012005-04-17 16:05:31 -05003250{
dea31012005-04-17 16:05:31 -05003251 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05003252 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003253 uint32_t status, evtctr;
3254 uint32_t ha_copy;
3255 int i;
James Smart09372822008-01-11 01:52:54 -05003256 unsigned long timeout;
dea31012005-04-17 16:05:31 -05003257 unsigned long drvr_flag = 0;
3258 volatile uint32_t word0, ldata;
3259 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04003260 int processing_queue = 0;
3261
3262 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3263 if (!pmbox) {
3264 /* processing mbox queue from intr_handler */
3265 processing_queue = 1;
3266 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3267 pmbox = lpfc_mbox_get(phba);
3268 if (!pmbox) {
3269 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3270 return MBX_SUCCESS;
3271 }
3272 }
dea31012005-04-17 16:05:31 -05003273
James Smarted957682007-06-17 19:56:37 -05003274 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05003275 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05003276 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04003277 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05003278 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003279 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003280 "1806 Mbox x%x failed. No vport\n",
James Smarted957682007-06-17 19:56:37 -05003281 pmbox->mb.mbxCommand);
3282 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04003283 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05003284 }
3285 }
3286
Linas Vepstas8d63f372007-02-14 14:28:36 -06003287 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04003288 if (unlikely(pci_channel_offline(phba->pcidev))) {
3289 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3290 goto out_not_finished;
3291 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06003292
dea31012005-04-17 16:05:31 -05003293 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05003294
dea31012005-04-17 16:05:31 -05003295 mb = &pmbox->mb;
3296 status = MBX_SUCCESS;
3297
James Smart2e0fef82007-06-17 19:56:36 -05003298 if (phba->link_state == LPFC_HBA_ERROR) {
3299 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003300
3301 /* Mbox command <mbxCommand> cannot issue */
James Smart7f5f3d02008-02-08 18:50:14 -05003302 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003303 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003304 }
3305
James Smart92908312006-03-07 15:04:13 -05003306 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
3307 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05003308 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart7f5f3d02008-02-08 18:50:14 -05003309 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003310 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05003311 }
3312
dea31012005-04-17 16:05:31 -05003313 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
3314 /* Polling for a mbox command when another one is already active
3315 * is not allowed in SLI. Also, the driver must have established
3316 * SLI2 mode to queue and process multiple mbox commands.
3317 */
3318
3319 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05003320 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003321
3322 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003323 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003324 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003325 }
3326
3327 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003328 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003329 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003330 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003331 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003332 }
3333
dea31012005-04-17 16:05:31 -05003334 /* Another mailbox command is still being processed, queue this
3335 * command to be processed later.
3336 */
3337 lpfc_mbox_put(phba, pmbox);
3338
3339 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05003340 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003341 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003342 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003343 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
3344 mb->mbxCommand, phba->pport->port_state,
3345 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003346
3347 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05003348 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003349
James Smart858c9f62007-06-17 19:56:39 -05003350 if (pmbox->vport) {
3351 lpfc_debugfs_disc_trc(pmbox->vport,
3352 LPFC_DISC_TRC_MBOX_VPORT,
3353 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
3354 (uint32_t)mb->mbxCommand,
3355 mb->un.varWords[0], mb->un.varWords[1]);
3356 }
3357 else {
3358 lpfc_debugfs_disc_trc(phba->pport,
3359 LPFC_DISC_TRC_MBOX,
3360 "MBOX Bsy: cmd:x%x mb:x%x x%x",
3361 (uint32_t)mb->mbxCommand,
3362 mb->un.varWords[0], mb->un.varWords[1]);
3363 }
3364
James Smart2e0fef82007-06-17 19:56:36 -05003365 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05003366 }
3367
dea31012005-04-17 16:05:31 -05003368 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3369
3370 /* If we are not polling, we MUST be in SLI2 mode */
3371 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05003372 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
3373 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05003374 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003375 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003376 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003377 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003378 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003379 }
3380 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04003381 mod_timer(&psli->mbox_tmo, (jiffies +
3382 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05003383 }
3384
3385 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05003386 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003387 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003388 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003389 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05003390 mb->mbxCommand, phba->pport->port_state,
3391 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003392
James Smart858c9f62007-06-17 19:56:39 -05003393 if (mb->mbxCommand != MBX_HEARTBEAT) {
3394 if (pmbox->vport) {
3395 lpfc_debugfs_disc_trc(pmbox->vport,
3396 LPFC_DISC_TRC_MBOX_VPORT,
3397 "MBOX Send vport: cmd:x%x mb:x%x x%x",
3398 (uint32_t)mb->mbxCommand,
3399 mb->un.varWords[0], mb->un.varWords[1]);
3400 }
3401 else {
3402 lpfc_debugfs_disc_trc(phba->pport,
3403 LPFC_DISC_TRC_MBOX,
3404 "MBOX Send: cmd:x%x mb:x%x x%x",
3405 (uint32_t)mb->mbxCommand,
3406 mb->un.varWords[0], mb->un.varWords[1]);
3407 }
3408 }
3409
dea31012005-04-17 16:05:31 -05003410 psli->slistat.mbox_cmd++;
3411 evtctr = psli->slistat.mbox_event;
3412
3413 /* next set own bit for the adapter and copy over command word */
3414 mb->mbxOwner = OWN_CHIP;
3415
3416 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003417 /* First copy command data to host SLIM area */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04003418 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003419 } else {
James Smart92908312006-03-07 15:04:13 -05003420 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05003421 /* copy command data into host mbox for cmpl */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04003422 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
James Smart92d7f7b2007-06-17 19:56:38 -05003423 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003424 }
3425
3426 /* First copy mbox command data to HBA SLIM, skip past first
3427 word */
3428 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3429 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
3430 MAILBOX_CMD_SIZE - sizeof (uint32_t));
3431
3432 /* Next copy over first word, with mbxOwner set */
3433 ldata = *((volatile uint32_t *)mb);
3434 to_slim = phba->MBslimaddr;
3435 writel(ldata, to_slim);
3436 readl(to_slim); /* flush */
3437
3438 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3439 /* switch over to host mailbox */
3440 psli->sli_flag |= LPFC_SLI2_ACTIVE;
3441 }
3442 }
3443
3444 wmb();
dea31012005-04-17 16:05:31 -05003445
3446 switch (flag) {
3447 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05003448 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05003449 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05003450 /* Interrupt board to do it */
3451 writel(CA_MBATT, phba->CAregaddr);
3452 readl(phba->CAregaddr); /* flush */
3453 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05003454 break;
3455
3456 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05003457 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05003458 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05003459 /* Interrupt board to do it */
3460 writel(CA_MBATT, phba->CAregaddr);
3461 readl(phba->CAregaddr); /* flush */
3462
dea31012005-04-17 16:05:31 -05003463 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3464 /* First read mbox status word */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04003465 word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05003466 word0 = le32_to_cpu(word0);
3467 } else {
3468 /* First read mbox status word */
3469 word0 = readl(phba->MBslimaddr);
3470 }
3471
3472 /* Read the HBA Host Attention Register */
3473 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05003474 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3475 mb->mbxCommand) *
3476 1000) + jiffies;
3477 i = 0;
dea31012005-04-17 16:05:31 -05003478 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003479 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
3480 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05003481 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05003482 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05003483 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003484 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05003485 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04003486 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003487 }
3488
3489 /* Check if we took a mbox interrupt while we were
3490 polling */
3491 if (((word0 & OWN_CHIP) != OWN_CHIP)
3492 && (evtctr != psli->slistat.mbox_event))
3493 break;
3494
James Smart09372822008-01-11 01:52:54 -05003495 if (i++ > 10) {
3496 spin_unlock_irqrestore(&phba->hbalock,
3497 drvr_flag);
3498 msleep(1);
3499 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3500 }
dea31012005-04-17 16:05:31 -05003501
3502 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3503 /* First copy command data */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04003504 word0 = *((volatile uint32_t *)
3505 &phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05003506 word0 = le32_to_cpu(word0);
3507 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3508 MAILBOX_t *slimmb;
3509 volatile uint32_t slimword0;
3510 /* Check real SLIM for any errors */
3511 slimword0 = readl(phba->MBslimaddr);
3512 slimmb = (MAILBOX_t *) & slimword0;
3513 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
3514 && slimmb->mbxStatus) {
3515 psli->sli_flag &=
3516 ~LPFC_SLI2_ACTIVE;
3517 word0 = slimword0;
3518 }
3519 }
3520 } else {
3521 /* First copy command data */
3522 word0 = readl(phba->MBslimaddr);
3523 }
3524 /* Read the HBA Host Attention Register */
3525 ha_copy = readl(phba->HAregaddr);
3526 }
3527
3528 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003529 /* copy results back to user */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04003530 lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
James Smart92d7f7b2007-06-17 19:56:38 -05003531 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003532 } else {
3533 /* First copy command data */
3534 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
3535 MAILBOX_CMD_SIZE);
3536 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
3537 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05003538 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05003539 phba->MBslimaddr + DMP_RSP_OFFSET,
3540 mb->un.varDmp.word_cnt);
3541 }
3542 }
3543
3544 writel(HA_MBATT, phba->HAregaddr);
3545 readl(phba->HAregaddr); /* flush */
3546
3547 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3548 status = mb->mbxStatus;
3549 }
3550
James Smart2e0fef82007-06-17 19:56:36 -05003551 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3552 return status;
James Smart58da1ff2008-04-07 10:15:56 -04003553
3554out_not_finished:
3555 if (processing_queue) {
3556 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
3557 lpfc_mbox_cmpl_put(phba, pmbox);
3558 }
3559 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003560}
3561
James Smarte59058c2008-08-24 21:49:00 -04003562/**
3563 * __lpfc_sli_ringtx_put: Add an iocb to the txq.
3564 * @phba: Pointer to HBA context object.
3565 * @pring: Pointer to driver SLI ring object.
3566 * @piocb: Pointer to address of newly added command iocb.
3567 *
3568 * This function is called with hbalock held to add a command
3569 * iocb to the txq when SLI layer cannot submit the command iocb
3570 * to the ring.
3571 **/
James Smart858c9f62007-06-17 19:56:39 -05003572static void
James Smart92d7f7b2007-06-17 19:56:38 -05003573__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003574 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05003575{
3576 /* Insert the caller's iocb in the txq tail for later processing. */
3577 list_add_tail(&piocb->list, &pring->txq);
3578 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05003579}
3580
James Smarte59058c2008-08-24 21:49:00 -04003581/**
3582 * lpfc_sli_next_iocb: Get the next iocb in the txq.
3583 * @phba: Pointer to HBA context object.
3584 * @pring: Pointer to driver SLI ring object.
3585 * @piocb: Pointer to address of newly added command iocb.
3586 *
3587 * This function is called with hbalock held before a new
3588 * iocb is submitted to the firmware. This function checks
3589 * txq to flush the iocbs in txq to Firmware before
3590 * submitting new iocbs to the Firmware.
3591 * If there are iocbs in the txq which need to be submitted
3592 * to firmware, lpfc_sli_next_iocb returns the first element
3593 * of the txq after dequeuing it from txq.
3594 * If there is no iocb in the txq then the function will return
3595 * *piocb and *piocb is set to NULL. Caller needs to check
3596 * *piocb to find if there are more commands in the txq.
3597 **/
dea31012005-04-17 16:05:31 -05003598static struct lpfc_iocbq *
3599lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003600 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05003601{
3602 struct lpfc_iocbq * nextiocb;
3603
3604 nextiocb = lpfc_sli_ringtx_get(phba, pring);
3605 if (!nextiocb) {
3606 nextiocb = *piocb;
3607 *piocb = NULL;
3608 }
3609
3610 return nextiocb;
3611}
3612
James Smarte59058c2008-08-24 21:49:00 -04003613/**
3614 * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
3615 * @phba: Pointer to HBA context object.
3616 * @pring: Pointer to driver SLI ring object.
3617 * @piocb: Pointer to command iocb.
3618 * @flag: Flag indicating if this command can be put into txq.
3619 *
3620 * __lpfc_sli_issue_iocb is used by other functions in the driver
3621 * to issue an iocb command to the HBA. If the PCI slot is recovering
3622 * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
3623 * flag is turned on, the function returns IOCB_ERROR.
3624 * When the link is down, this function allows only iocbs for
3625 * posting buffers.
3626 * This function finds next available slot in the command ring and
3627 * posts the command to the available slot and writes the port
3628 * attention register to request HBA start processing new iocb.
3629 * If there is no slot available in the ring and
3630 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
3631 * txq, otherwise the function returns IOCB_BUSY.
3632 *
3633 * This function is called with hbalock held.
3634 * The function will return success after it successfully submit the
3635 * iocb to firmware or after adding to the txq.
3636 **/
James Smart98c9ea52007-10-27 13:37:33 -04003637static int
James Smart92d7f7b2007-06-17 19:56:38 -05003638__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003639 struct lpfc_iocbq *piocb, uint32_t flag)
3640{
3641 struct lpfc_iocbq *nextiocb;
3642 IOCB_t *iocb;
3643
James Smart92d7f7b2007-06-17 19:56:38 -05003644 if (piocb->iocb_cmpl && (!piocb->vport) &&
3645 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
3646 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
3647 lpfc_printf_log(phba, KERN_ERR,
3648 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003649 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003650 piocb->iocb.ulpCommand);
3651 dump_stack();
3652 return IOCB_ERROR;
3653 }
3654
3655
Linas Vepstas8d63f372007-02-14 14:28:36 -06003656 /* If the PCI channel is in offline state, do not post iocbs. */
3657 if (unlikely(pci_channel_offline(phba->pcidev)))
3658 return IOCB_ERROR;
3659
dea31012005-04-17 16:05:31 -05003660 /*
3661 * We should never get an IOCB if we are in a < LINK_DOWN state
3662 */
James Smart2e0fef82007-06-17 19:56:36 -05003663 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003664 return IOCB_ERROR;
3665
3666 /*
3667 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04003668 * outstanding event.
dea31012005-04-17 16:05:31 -05003669 */
James Smart0b727fe2007-10-27 13:37:25 -04003670 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05003671 goto iocb_busy;
3672
James Smart2e0fef82007-06-17 19:56:36 -05003673 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05003674 /*
James Smart2680eea2007-04-25 09:52:55 -04003675 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05003676 * can be issued if the link is not up.
3677 */
3678 switch (piocb->iocb.ulpCommand) {
3679 case CMD_QUE_RING_BUF_CN:
3680 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05003681 /*
3682 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
3683 * completion, iocb_cmpl MUST be 0.
3684 */
3685 if (piocb->iocb_cmpl)
3686 piocb->iocb_cmpl = NULL;
3687 /*FALLTHROUGH*/
3688 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04003689 case CMD_CLOSE_XRI_CN:
3690 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05003691 break;
3692 default:
3693 goto iocb_busy;
3694 }
3695
3696 /*
3697 * For FCP commands, we must be in a state where we can process link
3698 * attention events.
3699 */
3700 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05003701 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05003702 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05003703 }
dea31012005-04-17 16:05:31 -05003704
dea31012005-04-17 16:05:31 -05003705 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
3706 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
3707 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
3708
3709 if (iocb)
3710 lpfc_sli_update_ring(phba, pring);
3711 else
3712 lpfc_sli_update_full_ring(phba, pring);
3713
3714 if (!piocb)
3715 return IOCB_SUCCESS;
3716
3717 goto out_busy;
3718
3719 iocb_busy:
3720 pring->stats.iocb_cmd_delay++;
3721
3722 out_busy:
3723
3724 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003725 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05003726 return IOCB_SUCCESS;
3727 }
3728
3729 return IOCB_BUSY;
3730}
3731
James Smart92d7f7b2007-06-17 19:56:38 -05003732
James Smarte59058c2008-08-24 21:49:00 -04003733/**
3734 * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
3735 * @phba: Pointer to HBA context object.
3736 * @pring: Pointer to driver SLI ring object.
3737 * @piocb: Pointer to command iocb.
3738 * @flag: Flag indicating if this command can be put into txq.
3739 *
3740 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
3741 * function. This function gets the hbalock and calls
3742 * __lpfc_sli_issue_iocb function and will return the error returned
3743 * by __lpfc_sli_issue_iocb function. This wrapper is used by
3744 * functions which do not hold hbalock.
3745 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003746int
3747lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3748 struct lpfc_iocbq *piocb, uint32_t flag)
3749{
3750 unsigned long iflags;
3751 int rc;
3752
3753 spin_lock_irqsave(&phba->hbalock, iflags);
3754 rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
3755 spin_unlock_irqrestore(&phba->hbalock, iflags);
3756
3757 return rc;
3758}
3759
James Smarte59058c2008-08-24 21:49:00 -04003760/**
3761 * lpfc_extra_ring_setup: Extra ring setup function.
3762 * @phba: Pointer to HBA context object.
3763 *
3764 * This function is called while driver attaches with the
3765 * HBA to setup the extra ring. The extra ring is used
3766 * only when driver needs to support target mode functionality
3767 * or IP over FC functionalities.
3768 *
3769 * This function is called with no lock held.
3770 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003771static int
3772lpfc_extra_ring_setup( struct lpfc_hba *phba)
3773{
3774 struct lpfc_sli *psli;
3775 struct lpfc_sli_ring *pring;
3776
3777 psli = &phba->sli;
3778
3779 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05003780
3781 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003782 pring = &psli->ring[psli->fcp_ring];
3783 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3784 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3785 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3786 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3787
James Smarta4bc3372006-12-02 13:34:16 -05003788 /* and give them to the extra ring */
3789 pring = &psli->ring[psli->extra_ring];
3790
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003791 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3792 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3793 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3794 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3795
3796 /* Setup default profile for this ring */
3797 pring->iotag_max = 4096;
3798 pring->num_mask = 1;
3799 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05003800 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
3801 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003802 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
3803 return 0;
3804}
3805
James Smarte59058c2008-08-24 21:49:00 -04003806/**
3807 * lpfc_sli_async_event_handler: ASYNC iocb handler function.
3808 * @phba: Pointer to HBA context object.
3809 * @pring: Pointer to driver SLI ring object.
3810 * @iocbq: Pointer to iocb object.
3811 *
3812 * This function is called by the slow ring event handler
3813 * function when there is an ASYNC event iocb in the ring.
3814 * This function is called with no lock held.
3815 * Currently this function handles only temperature related
3816 * ASYNC events. The function decodes the temperature sensor
3817 * event message and posts events for the management applications.
3818 **/
James Smart98c9ea52007-10-27 13:37:33 -04003819static void
James Smart57127f12007-10-27 13:37:05 -04003820lpfc_sli_async_event_handler(struct lpfc_hba * phba,
3821 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
3822{
3823 IOCB_t *icmd;
3824 uint16_t evt_code;
3825 uint16_t temp;
3826 struct temp_event temp_event_data;
3827 struct Scsi_Host *shost;
3828
3829 icmd = &iocbq->iocb;
3830 evt_code = icmd->un.asyncstat.evt_code;
3831 temp = icmd->ulpContext;
3832
3833 if ((evt_code != ASYNC_TEMP_WARN) &&
3834 (evt_code != ASYNC_TEMP_SAFE)) {
3835 lpfc_printf_log(phba,
3836 KERN_ERR,
3837 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003838 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smart57127f12007-10-27 13:37:05 -04003839 " evt_code 0x%x\n",
3840 pring->ringno,
3841 icmd->un.asyncstat.evt_code);
3842 return;
3843 }
3844 temp_event_data.data = (uint32_t)temp;
3845 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
3846 if (evt_code == ASYNC_TEMP_WARN) {
3847 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
3848 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003849 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003850 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04003851 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04003852 "corrective action. temperature : %d Celsius\n",
3853 temp);
3854 }
3855 if (evt_code == ASYNC_TEMP_SAFE) {
3856 temp_event_data.event_code = LPFC_NORMAL_TEMP;
3857 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003858 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003859 LOG_TEMP,
3860 "0340 Adapter temperature is OK now. "
3861 "temperature : %d Celsius\n",
3862 temp);
3863 }
3864
3865 /* Send temperature change event to applications */
3866 shost = lpfc_shost_from_vport(phba->pport);
3867 fc_host_post_vendor_event(shost, fc_get_event_number(),
3868 sizeof(temp_event_data), (char *) &temp_event_data,
3869 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
3870
3871}
3872
3873
James Smarte59058c2008-08-24 21:49:00 -04003874/**
3875 * lpfc_sli_setup: SLI ring setup function.
3876 * @phba: Pointer to HBA context object.
3877 *
3878 * lpfc_sli_setup sets up rings of the SLI interface with
3879 * number of iocbs per ring and iotags. This function is
3880 * called while driver attach to the HBA and before the
3881 * interrupts are enabled. So there is no need for locking.
3882 *
3883 * This function always returns 0.
3884 **/
dea31012005-04-17 16:05:31 -05003885int
3886lpfc_sli_setup(struct lpfc_hba *phba)
3887{
James Smarted957682007-06-17 19:56:37 -05003888 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05003889 struct lpfc_sli *psli = &phba->sli;
3890 struct lpfc_sli_ring *pring;
3891
3892 psli->num_rings = MAX_CONFIGURED_RINGS;
3893 psli->sli_flag = 0;
3894 psli->fcp_ring = LPFC_FCP_RING;
3895 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05003896 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05003897
James Bottomley604a3e32005-10-29 10:28:33 -05003898 psli->iocbq_lookup = NULL;
3899 psli->iocbq_lookup_len = 0;
3900 psli->last_iotag = 0;
3901
dea31012005-04-17 16:05:31 -05003902 for (i = 0; i < psli->num_rings; i++) {
3903 pring = &psli->ring[i];
3904 switch (i) {
3905 case LPFC_FCP_RING: /* ring 0 - FCP */
3906 /* numCiocb and numRiocb are used in config_port */
3907 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3908 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3909 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3910 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3911 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3912 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003913 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003914 SLI3_IOCB_CMD_SIZE :
3915 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003916 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003917 SLI3_IOCB_RSP_SIZE :
3918 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003919 pring->iotag_ctr = 0;
3920 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05003921 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05003922 pring->fast_iotag = pring->iotag_max;
3923 pring->num_mask = 0;
3924 break;
James Smarta4bc3372006-12-02 13:34:16 -05003925 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05003926 /* numCiocb and numRiocb are used in config_port */
3927 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
3928 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003929 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003930 SLI3_IOCB_CMD_SIZE :
3931 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003932 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003933 SLI3_IOCB_RSP_SIZE :
3934 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05003935 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05003936 pring->num_mask = 0;
3937 break;
3938 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
3939 /* numCiocb and numRiocb are used in config_port */
3940 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
3941 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003942 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003943 SLI3_IOCB_CMD_SIZE :
3944 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003945 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003946 SLI3_IOCB_RSP_SIZE :
3947 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003948 pring->fast_iotag = 0;
3949 pring->iotag_ctr = 0;
3950 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04003951 pring->lpfc_sli_rcv_async_status =
3952 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05003953 pring->num_mask = 4;
3954 pring->prt[0].profile = 0; /* Mask 0 */
3955 pring->prt[0].rctl = FC_ELS_REQ;
3956 pring->prt[0].type = FC_ELS_DATA;
3957 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003958 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003959 pring->prt[1].profile = 0; /* Mask 1 */
3960 pring->prt[1].rctl = FC_ELS_RSP;
3961 pring->prt[1].type = FC_ELS_DATA;
3962 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003963 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003964 pring->prt[2].profile = 0; /* Mask 2 */
3965 /* NameServer Inquiry */
3966 pring->prt[2].rctl = FC_UNSOL_CTL;
3967 /* NameServer */
3968 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
3969 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003970 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003971 pring->prt[3].profile = 0; /* Mask 3 */
3972 /* NameServer response */
3973 pring->prt[3].rctl = FC_SOL_CTL;
3974 /* NameServer */
3975 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
3976 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003977 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003978 break;
3979 }
James Smarted957682007-06-17 19:56:37 -05003980 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05003981 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05003982 }
James Smarted957682007-06-17 19:56:37 -05003983 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05003984 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04003985 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
3986 "SLI2 SLIM Data: x%x x%lx\n",
3987 phba->brd_no, totiocbsize,
3988 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05003989 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003990 if (phba->cfg_multi_ring_support == 2)
3991 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05003992
3993 return 0;
3994}
3995
James Smarte59058c2008-08-24 21:49:00 -04003996/**
3997 * lpfc_sli_queue_setup: Queue initialization function.
3998 * @phba: Pointer to HBA context object.
3999 *
4000 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
4001 * ring. This function also initializes ring indices of each ring.
4002 * This function is called during the initialization of the SLI
4003 * interface of an HBA.
4004 * This function is called with no lock held and always returns
4005 * 1.
4006 **/
dea31012005-04-17 16:05:31 -05004007int
James Smart2e0fef82007-06-17 19:56:36 -05004008lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004009{
4010 struct lpfc_sli *psli;
4011 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05004012 int i;
dea31012005-04-17 16:05:31 -05004013
4014 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05004015 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004016 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05004017 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05004018 /* Initialize list headers for txq and txcmplq as double linked lists */
4019 for (i = 0; i < psli->num_rings; i++) {
4020 pring = &psli->ring[i];
4021 pring->ringno = i;
4022 pring->next_cmdidx = 0;
4023 pring->local_getidx = 0;
4024 pring->cmdidx = 0;
4025 INIT_LIST_HEAD(&pring->txq);
4026 INIT_LIST_HEAD(&pring->txcmplq);
4027 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05004028 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05004029 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05004030 }
James Smart2e0fef82007-06-17 19:56:36 -05004031 spin_unlock_irq(&phba->hbalock);
4032 return 1;
dea31012005-04-17 16:05:31 -05004033}
4034
James Smarte59058c2008-08-24 21:49:00 -04004035/**
4036 * lpfc_sli_host_down: Vport cleanup function.
4037 * @vport: Pointer to virtual port object.
4038 *
4039 * lpfc_sli_host_down is called to clean up the resources
4040 * associated with a vport before destroying virtual
4041 * port data structures.
4042 * This function does following operations:
4043 * - Free discovery resources associated with this virtual
4044 * port.
4045 * - Free iocbs associated with this virtual port in
4046 * the txq.
4047 * - Send abort for all iocb commands associated with this
4048 * vport in txcmplq.
4049 *
4050 * This function is called with no lock held and always returns 1.
4051 **/
dea31012005-04-17 16:05:31 -05004052int
James Smart92d7f7b2007-06-17 19:56:38 -05004053lpfc_sli_host_down(struct lpfc_vport *vport)
4054{
James Smart858c9f62007-06-17 19:56:39 -05004055 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004056 struct lpfc_hba *phba = vport->phba;
4057 struct lpfc_sli *psli = &phba->sli;
4058 struct lpfc_sli_ring *pring;
4059 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004060 int i;
4061 unsigned long flags = 0;
4062 uint16_t prev_pring_flag;
4063
4064 lpfc_cleanup_discovery_resources(vport);
4065
4066 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004067 for (i = 0; i < psli->num_rings; i++) {
4068 pring = &psli->ring[i];
4069 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04004070 /* Only slow rings */
4071 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004072 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004073 /* Set the lpfc data pending flag */
4074 set_bit(LPFC_DATA_READY, &phba->data_flags);
4075 }
James Smart92d7f7b2007-06-17 19:56:38 -05004076 /*
4077 * Error everything on the txq since these iocbs have not been
4078 * given to the FW yet.
4079 */
James Smart92d7f7b2007-06-17 19:56:38 -05004080 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4081 if (iocb->vport != vport)
4082 continue;
James Smart858c9f62007-06-17 19:56:39 -05004083 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004084 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05004085 }
4086
4087 /* Next issue ABTS for everything on the txcmplq */
4088 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
4089 list) {
4090 if (iocb->vport != vport)
4091 continue;
4092 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4093 }
4094
4095 pring->flag = prev_pring_flag;
4096 }
4097
4098 spin_unlock_irqrestore(&phba->hbalock, flags);
4099
James Smart858c9f62007-06-17 19:56:39 -05004100 while (!list_empty(&completions)) {
4101 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
4102
4103 if (!iocb->iocb_cmpl)
4104 lpfc_sli_release_iocbq(phba, iocb);
4105 else {
4106 iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4107 iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
4108 (iocb->iocb_cmpl) (phba, iocb, iocb);
4109 }
4110 }
James Smart92d7f7b2007-06-17 19:56:38 -05004111 return 1;
4112}
4113
James Smarte59058c2008-08-24 21:49:00 -04004114/**
4115 * lpfc_sli_hba_down: Resource cleanup function for the HBA.
4116 * @phba: Pointer to HBA context object.
4117 *
4118 * This function cleans up all iocb, buffers, mailbox commands
4119 * while shutting down the HBA. This function is called with no
4120 * lock held and always returns 1.
4121 * This function does the following to cleanup driver resources:
4122 * - Free discovery resources for each virtual port
4123 * - Cleanup any pending fabric iocbs
4124 * - Iterate through the iocb txq and free each entry
4125 * in the list.
4126 * - Free up any buffer posted to the HBA
4127 * - Free mailbox commands in the mailbox queue.
4128 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004129int
James Smart2e0fef82007-06-17 19:56:36 -05004130lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004131{
James Smart2534ba72007-04-25 09:52:20 -04004132 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05004133 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004134 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05004135 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05004136 LPFC_MBOXQ_t *pmb;
James Smart2534ba72007-04-25 09:52:20 -04004137 struct lpfc_iocbq *iocb;
4138 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004139 int i;
4140 unsigned long flags = 0;
4141
dea31012005-04-17 16:05:31 -05004142 lpfc_hba_down_prep(phba);
4143
James Smart92d7f7b2007-06-17 19:56:38 -05004144 lpfc_fabric_abort_hba(phba);
4145
James Smart2e0fef82007-06-17 19:56:36 -05004146 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004147 for (i = 0; i < psli->num_rings; i++) {
4148 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04004149 /* Only slow rings */
4150 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004151 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004152 /* Set the lpfc data pending flag */
4153 set_bit(LPFC_DATA_READY, &phba->data_flags);
4154 }
dea31012005-04-17 16:05:31 -05004155
4156 /*
4157 * Error everything on the txq since these iocbs have not been
4158 * given to the FW yet.
4159 */
James Smart2534ba72007-04-25 09:52:20 -04004160 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05004161 pring->txq_cnt = 0;
4162
dea31012005-04-17 16:05:31 -05004163 }
James Smart2e0fef82007-06-17 19:56:36 -05004164 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004165
James Smart2534ba72007-04-25 09:52:20 -04004166 while (!list_empty(&completions)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004167 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
James Smart2534ba72007-04-25 09:52:20 -04004168 cmd = &iocb->iocb;
James Smart2534ba72007-04-25 09:52:20 -04004169
James Smart2e0fef82007-06-17 19:56:36 -05004170 if (!iocb->iocb_cmpl)
4171 lpfc_sli_release_iocbq(phba, iocb);
4172 else {
James Smart2534ba72007-04-25 09:52:20 -04004173 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4174 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
4175 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05004176 }
James Smart2534ba72007-04-25 09:52:20 -04004177 }
4178
James Smart0ff10d42008-01-11 01:52:36 -05004179 spin_lock_irqsave(&phba->hbalock, flags);
4180 list_splice_init(&phba->elsbuf, &completions);
4181 phba->elsbuf_cnt = 0;
4182 phba->elsbuf_prev_cnt = 0;
4183 spin_unlock_irqrestore(&phba->hbalock, flags);
4184
4185 while (!list_empty(&completions)) {
4186 list_remove_head(&completions, buf_ptr,
4187 struct lpfc_dmabuf, list);
4188 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4189 kfree(buf_ptr);
4190 }
4191
dea31012005-04-17 16:05:31 -05004192 /* Return any active mbox cmds */
4193 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05004194 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004195
4196 spin_lock(&phba->pport->work_port_lock);
4197 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4198 spin_unlock(&phba->pport->work_port_lock);
4199
James Smart97eab632008-04-07 10:16:05 -04004200 /* Return any pending or completed mbox cmds */
4201 list_splice_init(&phba->sli.mboxq, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004202 if (psli->mbox_active) {
4203 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05004204 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004205 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05004206 }
James Smart92d7f7b2007-06-17 19:56:38 -05004207 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004208 spin_unlock_irqrestore(&phba->hbalock, flags);
4209
4210 while (!list_empty(&completions)) {
4211 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05004212 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
James Smart97eab632008-04-07 10:16:05 -04004213 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05004214 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05004215 }
dea31012005-04-17 16:05:31 -05004216 return 1;
4217}
4218
James Smarte59058c2008-08-24 21:49:00 -04004219/**
4220 * lpfc_sli_pcimem_bcopy: SLI memory copy function.
4221 * @srcp: Source memory pointer.
4222 * @destp: Destination memory pointer.
4223 * @cnt: Number of words required to be copied.
4224 *
4225 * This function is used for copying data between driver memory
4226 * and the SLI memory. This function also changes the endianness
4227 * of each word if native endianness is different from SLI
4228 * endianness. This function can be called with or without
4229 * lock.
4230 **/
dea31012005-04-17 16:05:31 -05004231void
4232lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
4233{
4234 uint32_t *src = srcp;
4235 uint32_t *dest = destp;
4236 uint32_t ldata;
4237 int i;
4238
4239 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
4240 ldata = *src;
4241 ldata = le32_to_cpu(ldata);
4242 *dest = ldata;
4243 src++;
4244 dest++;
4245 }
4246}
4247
James Smarte59058c2008-08-24 21:49:00 -04004248
4249/**
4250 * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
4251 * @phba: Pointer to HBA context object.
4252 * @pring: Pointer to driver SLI ring object.
4253 * @mp: Pointer to driver buffer object.
4254 *
4255 * This function is called with no lock held.
4256 * It always return zero after adding the buffer to the postbufq
4257 * buffer list.
4258 **/
dea31012005-04-17 16:05:31 -05004259int
James Smart2e0fef82007-06-17 19:56:36 -05004260lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4261 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05004262{
4263 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
4264 later */
James Smart2e0fef82007-06-17 19:56:36 -05004265 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004266 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05004267 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05004268 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004269 return 0;
4270}
4271
James Smarte59058c2008-08-24 21:49:00 -04004272/**
4273 * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
4274 * using CMD_QUE_XRI64_CX iocb.
4275 * @phba: Pointer to HBA context object.
4276 *
4277 * When HBQ is enabled, buffers are searched based on tags. This function
4278 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
4279 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
4280 * does not conflict with tags of buffer posted for unsolicited events.
4281 * The function returns the allocated tag. The function is called with
4282 * no locks held.
4283 **/
James Smart76bb24e2007-10-27 13:38:00 -04004284uint32_t
4285lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
4286{
4287 spin_lock_irq(&phba->hbalock);
4288 phba->buffer_tag_count++;
4289 /*
4290 * Always set the QUE_BUFTAG_BIT to distiguish between
4291 * a tag assigned by HBQ.
4292 */
4293 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
4294 spin_unlock_irq(&phba->hbalock);
4295 return phba->buffer_tag_count;
4296}
4297
James Smarte59058c2008-08-24 21:49:00 -04004298/**
4299 * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
4300 * posted using CMD_QUE_XRI64_CX iocb.
4301 * @phba: Pointer to HBA context object.
4302 * @pring: Pointer to driver SLI ring object.
4303 * @tag: Buffer tag.
4304 *
4305 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
4306 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
4307 * iocb is posted to the response ring with the tag of the buffer.
4308 * This function searches the pring->postbufq list using the tag
4309 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
4310 * iocb. If the buffer is found then lpfc_dmabuf object of the
4311 * buffer is returned to the caller else NULL is returned.
4312 * This function is called with no lock held.
4313 **/
James Smart76bb24e2007-10-27 13:38:00 -04004314struct lpfc_dmabuf *
4315lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4316 uint32_t tag)
4317{
4318 struct lpfc_dmabuf *mp, *next_mp;
4319 struct list_head *slp = &pring->postbufq;
4320
4321 /* Search postbufq, from the begining, looking for a match on tag */
4322 spin_lock_irq(&phba->hbalock);
4323 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4324 if (mp->buffer_tag == tag) {
4325 list_del_init(&mp->list);
4326 pring->postbufq_cnt--;
4327 spin_unlock_irq(&phba->hbalock);
4328 return mp;
4329 }
4330 }
4331
4332 spin_unlock_irq(&phba->hbalock);
4333 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4334 "0410 Cannot find virtual addr for buffer tag on "
4335 "ring %d Data x%lx x%p x%p x%x\n",
4336 pring->ringno, (unsigned long) tag,
4337 slp->next, slp->prev, pring->postbufq_cnt);
4338
4339 return NULL;
4340}
dea31012005-04-17 16:05:31 -05004341
James Smarte59058c2008-08-24 21:49:00 -04004342/**
4343 * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
4344 * unsolicited ct and els events.
4345 * @phba: Pointer to HBA context object.
4346 * @pring: Pointer to driver SLI ring object.
4347 * @phys: DMA address of the buffer.
4348 *
4349 * This function searches the buffer list using the dma_address
4350 * of unsolicited event to find the driver's lpfc_dmabuf object
4351 * corresponding to the dma_address. The function returns the
4352 * lpfc_dmabuf object if a buffer is found else it returns NULL.
4353 * This function is called by the ct and els unsolicited event
4354 * handlers to get the buffer associated with the unsolicited
4355 * event.
4356 *
4357 * This function is called with no lock held.
4358 **/
dea31012005-04-17 16:05:31 -05004359struct lpfc_dmabuf *
4360lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4361 dma_addr_t phys)
4362{
4363 struct lpfc_dmabuf *mp, *next_mp;
4364 struct list_head *slp = &pring->postbufq;
4365
4366 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05004367 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004368 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4369 if (mp->phys == phys) {
4370 list_del_init(&mp->list);
4371 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004372 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004373 return mp;
4374 }
4375 }
4376
James Smart2e0fef82007-06-17 19:56:36 -05004377 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004379 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05004380 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004381 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05004382 slp->next, slp->prev, pring->postbufq_cnt);
4383 return NULL;
4384}
4385
James Smarte59058c2008-08-24 21:49:00 -04004386/**
4387 * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
4388 * @phba: Pointer to HBA context object.
4389 * @cmdiocb: Pointer to driver command iocb object.
4390 * @rspiocb: Pointer to driver response iocb object.
4391 *
4392 * This function is the completion handler for the abort iocbs for
4393 * ELS commands. This function is called from the ELS ring event
4394 * handler with no lock held. This function frees memory resources
4395 * associated with the abort iocb.
4396 **/
dea31012005-04-17 16:05:31 -05004397static void
James Smart2e0fef82007-06-17 19:56:36 -05004398lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4399 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05004400{
James Smart2e0fef82007-06-17 19:56:36 -05004401 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04004402 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05004403 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04004404 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4405
4406 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04004407
4408 if (irsp->ulpStatus) {
4409 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
4410 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
4411
James Smart2e0fef82007-06-17 19:56:36 -05004412 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004413 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
4414 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
4415
James Smart92d7f7b2007-06-17 19:56:38 -05004416 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004417 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05004418 "with tag %x context %x, abort status %x, "
4419 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04004420 abort_iocb, abort_iotag, abort_context,
4421 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04004422
4423 /*
James Smart58da1ff2008-04-07 10:15:56 -04004424 * If the iocb is not found in Firmware queue the iocb
4425 * might have completed already. Do not free it again.
4426 */
James Smart9b379602008-04-07 10:16:00 -04004427 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04004428 spin_unlock_irq(&phba->hbalock);
4429 lpfc_sli_release_iocbq(phba, cmdiocb);
4430 return;
4431 }
4432 /*
James Smart2680eea2007-04-25 09:52:55 -04004433 * make sure we have the right iocbq before taking it
4434 * off the txcmplq and try to call completion routine.
4435 */
James Smart2e0fef82007-06-17 19:56:36 -05004436 if (!abort_iocb ||
4437 abort_iocb->iocb.ulpContext != abort_context ||
4438 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
4439 spin_unlock_irq(&phba->hbalock);
4440 else {
James Smart92d7f7b2007-06-17 19:56:38 -05004441 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04004442 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004443 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004444
James Smart0ff10d42008-01-11 01:52:36 -05004445 /* Firmware could still be in progress of DMAing
4446 * payload, so don't free data buffer till after
4447 * a hbeat.
4448 */
4449 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
4450
James Smart92d7f7b2007-06-17 19:56:38 -05004451 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4452 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4453 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
4454 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04004455 }
4456 }
4457
James Bottomley604a3e32005-10-29 10:28:33 -05004458 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05004459 return;
4460}
4461
James Smarte59058c2008-08-24 21:49:00 -04004462/**
4463 * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
4464 * @phba: Pointer to HBA context object.
4465 * @cmdiocb: Pointer to driver command iocb object.
4466 * @rspiocb: Pointer to driver response iocb object.
4467 *
4468 * The function is called from SLI ring event handler with no
4469 * lock held. This function is the completion handler for ELS commands
4470 * which are aborted. The function frees memory resources used for
4471 * the aborted ELS commands.
4472 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004473static void
4474lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4475 struct lpfc_iocbq *rspiocb)
4476{
4477 IOCB_t *irsp = &rspiocb->iocb;
4478
4479 /* ELS cmd tag <ulpIoTag> completes */
4480 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smarte8b62012007-08-02 11:10:09 -04004481 "0133 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004482 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004483 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05004484 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05004485 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
4486 lpfc_ct_free_iocb(phba, cmdiocb);
4487 else
4488 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05004489 return;
4490}
4491
James Smarte59058c2008-08-24 21:49:00 -04004492/**
4493 * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
4494 * @phba: Pointer to HBA context object.
4495 * @pring: Pointer to driver SLI ring object.
4496 * @cmdiocb: Pointer to driver command iocb object.
4497 *
4498 * This function issues an abort iocb for the provided command
4499 * iocb. This function is called with hbalock held.
4500 * The function returns 0 when it fails due to memory allocation
4501 * failure or when the command iocb is an abort request.
4502 **/
dea31012005-04-17 16:05:31 -05004503int
James Smart2e0fef82007-06-17 19:56:36 -05004504lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4505 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05004506{
James Smart2e0fef82007-06-17 19:56:36 -05004507 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004508 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05004509 IOCB_t *icmd = NULL;
4510 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04004511 int retval = IOCB_ERROR;
4512
James Smart92d7f7b2007-06-17 19:56:38 -05004513 /*
4514 * There are certain command types we don't want to abort. And we
4515 * don't want to abort commands that are already in the process of
4516 * being aborted.
James Smart07951072007-04-25 09:51:38 -04004517 */
4518 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004519 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05004520 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4521 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04004522 return 0;
4523
James Smart858c9f62007-06-17 19:56:39 -05004524 /* If we're unloading, don't abort iocb on the ELS ring, but change the
4525 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04004526 */
James Smart858c9f62007-06-17 19:56:39 -05004527 if ((vport->load_flag & FC_UNLOADING) &&
4528 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004529 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
4530 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
4531 else
4532 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04004533 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05004534 }
dea31012005-04-17 16:05:31 -05004535
4536 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05004537 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004538 if (abtsiocbp == NULL)
4539 return 0;
dea31012005-04-17 16:05:31 -05004540
James Smart07951072007-04-25 09:51:38 -04004541 /* This signals the response to set the correct status
4542 * before calling the completion handler.
4543 */
4544 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4545
dea31012005-04-17 16:05:31 -05004546 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04004547 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
4548 iabt->un.acxri.abortContextTag = icmd->ulpContext;
4549 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004550 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04004551 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05004552
James Smart2e0fef82007-06-17 19:56:36 -05004553 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04004554 iabt->ulpCommand = CMD_ABORT_XRI_CN;
4555 else
4556 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
4557
4558 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04004559
James Smarte8b62012007-08-02 11:10:09 -04004560 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4561 "0339 Abort xri x%x, original iotag x%x, "
4562 "abort cmd iotag x%x\n",
4563 iabt->un.acxri.abortContextTag,
4564 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05004565 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04004566
4567abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05004568 /*
4569 * Caller to this routine should check for IOCB_ERROR
4570 * and handle it properly. This routine no longer removes
4571 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04004572 */
James Smart2e0fef82007-06-17 19:56:36 -05004573 return retval;
dea31012005-04-17 16:05:31 -05004574}
4575
James Smarte59058c2008-08-24 21:49:00 -04004576/**
4577 * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
4578 * associated with a vport/SCSI target/lun.
4579 * @iocbq: Pointer to driver iocb object.
4580 * @vport: Pointer to driver virtual port object.
4581 * @tgt_id: SCSI ID of the target.
4582 * @lun_id: LUN ID of the scsi device.
4583 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
4584 *
4585 * This function acts as iocb filter for functions which abort or count
4586 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
4587 * 0 if the filtering criteria is met for the given iocb and will return
4588 * 1 if the filtering criteria is not met.
4589 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
4590 * given iocb is for the SCSI device specified by vport, tgt_id and
4591 * lun_id parameter.
4592 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
4593 * given iocb is for the SCSI target specified by vport and tgt_id
4594 * parameters.
4595 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
4596 * given iocb is for the SCSI host associated with the given vport.
4597 * This function is called with no locks held.
4598 **/
dea31012005-04-17 16:05:31 -05004599static int
James Smart51ef4c22007-08-02 11:10:31 -04004600lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
4601 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004602 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004603{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004604 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004605 int rc = 1;
4606
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004607 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
4608 return rc;
4609
James Smart51ef4c22007-08-02 11:10:31 -04004610 if (iocbq->vport != vport)
4611 return rc;
4612
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004613 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004614
James Smart495a7142008-06-14 22:52:59 -04004615 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05004616 return rc;
4617
4618 switch (ctx_cmd) {
4619 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04004620 if ((lpfc_cmd->rdata->pnode) &&
4621 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
4622 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05004623 rc = 0;
4624 break;
4625 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04004626 if ((lpfc_cmd->rdata->pnode) &&
4627 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05004628 rc = 0;
4629 break;
dea31012005-04-17 16:05:31 -05004630 case LPFC_CTX_HOST:
4631 rc = 0;
4632 break;
4633 default:
4634 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004635 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05004636 break;
4637 }
4638
4639 return rc;
4640}
4641
James Smarte59058c2008-08-24 21:49:00 -04004642/**
4643 * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
4644 * @vport: Pointer to virtual port.
4645 * @tgt_id: SCSI ID of the target.
4646 * @lun_id: LUN ID of the scsi device.
4647 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4648 *
4649 * This function returns number of FCP commands pending for the vport.
4650 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
4651 * commands pending on the vport associated with SCSI device specified
4652 * by tgt_id and lun_id parameters.
4653 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
4654 * commands pending on the vport associated with SCSI target specified
4655 * by tgt_id parameter.
4656 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
4657 * commands pending on the vport.
4658 * This function returns the number of iocbs which satisfy the filter.
4659 * This function is called without any lock held.
4660 **/
dea31012005-04-17 16:05:31 -05004661int
James Smart51ef4c22007-08-02 11:10:31 -04004662lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
4663 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004664{
James Smart51ef4c22007-08-02 11:10:31 -04004665 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004666 struct lpfc_iocbq *iocbq;
4667 int sum, i;
dea31012005-04-17 16:05:31 -05004668
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004669 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
4670 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004671
James Smart51ef4c22007-08-02 11:10:31 -04004672 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
4673 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004674 sum++;
dea31012005-04-17 16:05:31 -05004675 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004676
dea31012005-04-17 16:05:31 -05004677 return sum;
4678}
4679
James Smarte59058c2008-08-24 21:49:00 -04004680/**
4681 * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
4682 * FCP iocb.
4683 * @phba: Pointer to HBA context object
4684 * @cmdiocb: Pointer to command iocb object.
4685 * @rspiocb: Pointer to response iocb object.
4686 *
4687 * This function is called when an aborted FCP iocb completes. This
4688 * function is called by the ring event handler with no lock held.
4689 * This function frees the iocb.
4690 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004691void
James Smart2e0fef82007-06-17 19:56:36 -05004692lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4693 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004694{
James Bottomley604a3e32005-10-29 10:28:33 -05004695 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004696 return;
4697}
4698
James Smarte59058c2008-08-24 21:49:00 -04004699/**
4700 * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
4701 * pending on a SCSI host(vport)/target/lun.
4702 * @vport: Pointer to virtual port.
4703 * @pring: Pointer to driver SLI ring object.
4704 * @tgt_id: SCSI ID of the target.
4705 * @lun_id: LUN ID of the scsi device.
4706 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4707 *
4708 * This function sends an abort command for every SCSI command
4709 * associated with the given virtual port pending on the ring
4710 * filtered by lpfc_sli_validate_fcp_iocb function.
4711 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
4712 * FCP iocbs associated with lun specified by tgt_id and lun_id
4713 * parameters
4714 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
4715 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
4716 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
4717 * FCP iocbs associated with virtual port.
4718 * This function returns number of iocbs it failed to abort.
4719 * This function is called with no locks held.
4720 **/
dea31012005-04-17 16:05:31 -05004721int
James Smart51ef4c22007-08-02 11:10:31 -04004722lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
4723 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05004724{
James Smart51ef4c22007-08-02 11:10:31 -04004725 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004726 struct lpfc_iocbq *iocbq;
4727 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004728 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004729 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004730 int i;
dea31012005-04-17 16:05:31 -05004731
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004732 for (i = 1; i <= phba->sli.last_iotag; i++) {
4733 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004734
James Smart51ef4c22007-08-02 11:10:31 -04004735 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05004736 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05004737 continue;
4738
4739 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004740 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004741 if (abtsiocb == NULL) {
4742 errcnt++;
4743 continue;
4744 }
dea31012005-04-17 16:05:31 -05004745
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004746 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05004747 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
4748 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
4749 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
4750 abtsiocb->iocb.ulpLe = 1;
4751 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05004752 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05004753
James Smart2e0fef82007-06-17 19:56:36 -05004754 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004755 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
4756 else
4757 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
4758
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004759 /* Setup callback routine and issue the command. */
4760 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05004761 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
4762 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05004763 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05004764 errcnt++;
4765 continue;
4766 }
4767 }
4768
4769 return errcnt;
4770}
4771
James Smarte59058c2008-08-24 21:49:00 -04004772/**
4773 * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
4774 * lpfc_sli_issue_iocb_wait.
4775 * @phba: Pointer to HBA context object.
4776 * @cmdiocbq: Pointer to command iocb.
4777 * @rspiocbq: Pointer to response iocb.
4778 *
4779 * This function is the completion handler for iocbs issued using
4780 * lpfc_sli_issue_iocb_wait function. This function is called by the
4781 * ring event handler function without any lock held. This function
4782 * can be called from both worker thread context and interrupt
4783 * context. This function also can be called from other thread which
4784 * cleans up the SLI layer objects.
4785 * This function copy the contents of the response iocb to the
4786 * response iocb memory object provided by the caller of
4787 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4788 * sleeps for the iocb completion.
4789 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004790static void
4791lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
4792 struct lpfc_iocbq *cmdiocbq,
4793 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05004794{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004795 wait_queue_head_t *pdone_q;
4796 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004797
James Smart2e0fef82007-06-17 19:56:36 -05004798 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004799 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4800 if (cmdiocbq->context2 && rspiocbq)
4801 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4802 &rspiocbq->iocb, sizeof(IOCB_t));
4803
4804 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004805 if (pdone_q)
4806 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05004807 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05004808 return;
4809}
4810
James Smarte59058c2008-08-24 21:49:00 -04004811/**
4812 * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
4813 * @phba: Pointer to HBA context object..
4814 * @pring: Pointer to sli ring.
4815 * @piocb: Pointer to command iocb.
4816 * @prspiocbq: Pointer to response iocb.
4817 * @timeout: Timeout in number of seconds.
4818 *
4819 * This function issues the iocb to firmware and waits for the
4820 * iocb to complete. If the iocb command is not
4821 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
4822 * Caller should not free the iocb resources if this function
4823 * returns IOCB_TIMEDOUT.
4824 * The function waits for the iocb completion using an
4825 * non-interruptible wait.
4826 * This function will sleep while waiting for iocb completion.
4827 * So, this function should not be called from any context which
4828 * does not allow sleeping. Due to the same reason, this function
4829 * cannot be called with interrupt disabled.
4830 * This function assumes that the iocb completions occur while
4831 * this function sleep. So, this function cannot be called from
4832 * the thread which process iocb completion for this ring.
4833 * This function clears the iocb_flag of the iocb object before
4834 * issuing the iocb and the iocb completion handler sets this
4835 * flag and wakes this thread when the iocb completes.
4836 * The contents of the response iocb will be copied to prspiocbq
4837 * by the completion handler when the command completes.
4838 * This function returns IOCB_SUCCESS when success.
4839 * This function is called with no lock held.
4840 **/
dea31012005-04-17 16:05:31 -05004841int
James Smart2e0fef82007-06-17 19:56:36 -05004842lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
4843 struct lpfc_sli_ring *pring,
4844 struct lpfc_iocbq *piocb,
4845 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004846 uint32_t timeout)
dea31012005-04-17 16:05:31 -05004847{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08004848 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004849 long timeleft, timeout_req = 0;
4850 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004851 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05004852
4853 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004854 * If the caller has provided a response iocbq buffer, then context2
4855 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05004856 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004857 if (prspiocbq) {
4858 if (piocb->context2)
4859 return IOCB_ERROR;
4860 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05004861 }
4862
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004863 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
4864 piocb->context_un.wait_queue = &done_q;
4865 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05004866
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004867 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4868 creg_val = readl(phba->HCregaddr);
4869 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
4870 writel(creg_val, phba->HCregaddr);
4871 readl(phba->HCregaddr); /* flush */
4872 }
4873
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004874 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
4875 if (retval == IOCB_SUCCESS) {
4876 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004877 timeleft = wait_event_timeout(done_q,
4878 piocb->iocb_flag & LPFC_IO_WAKE,
4879 timeout_req);
dea31012005-04-17 16:05:31 -05004880
James Smart7054a602007-04-25 09:52:34 -04004881 if (piocb->iocb_flag & LPFC_IO_WAKE) {
4882 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004883 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04004884 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004885 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004886 "0338 IOCB wait timeout error - no "
4887 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004888 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04004889 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004890 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004891 "0330 IOCB wake NOT set, "
4892 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004893 timeout, (timeleft / jiffies));
4894 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05004895 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004896 } else {
4897 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004898 ":0332 IOCB wait issue failed, Data x%x\n",
4899 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004900 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05004901 }
4902
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004903 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4904 creg_val = readl(phba->HCregaddr);
4905 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
4906 writel(creg_val, phba->HCregaddr);
4907 readl(phba->HCregaddr); /* flush */
4908 }
4909
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004910 if (prspiocbq)
4911 piocb->context2 = NULL;
4912
4913 piocb->context_un.wait_queue = NULL;
4914 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05004915 return retval;
4916}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004917
James Smarte59058c2008-08-24 21:49:00 -04004918/**
4919 * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
4920 * @phba: Pointer to HBA context object.
4921 * @pmboxq: Pointer to driver mailbox object.
4922 * @timeout: Timeout in number of seconds.
4923 *
4924 * This function issues the mailbox to firmware and waits for the
4925 * mailbox command to complete. If the mailbox command is not
4926 * completed within timeout seconds, it returns MBX_TIMEOUT.
4927 * The function waits for the mailbox completion using an
4928 * interruptible wait. If the thread is woken up due to a
4929 * signal, MBX_TIMEOUT error is returned to the caller. Caller
4930 * should not free the mailbox resources, if this function returns
4931 * MBX_TIMEOUT.
4932 * This function will sleep while waiting for mailbox completion.
4933 * So, this function should not be called from any context which
4934 * does not allow sleeping. Due to the same reason, this function
4935 * cannot be called with interrupt disabled.
4936 * This function assumes that the mailbox completion occurs while
4937 * this function sleep. So, this function cannot be called from
4938 * the worker thread which processes mailbox completion.
4939 * This function is called in the context of HBA management
4940 * applications.
4941 * This function returns MBX_SUCCESS when successful.
4942 * This function is called with no lock held.
4943 **/
dea31012005-04-17 16:05:31 -05004944int
James Smart2e0fef82007-06-17 19:56:36 -05004945lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05004946 uint32_t timeout)
4947{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08004948 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05004949 int retval;
James Smart858c9f62007-06-17 19:56:39 -05004950 unsigned long flag;
dea31012005-04-17 16:05:31 -05004951
4952 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04004953 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05004954 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05004955
James Smart495a7142008-06-14 22:52:59 -04004956 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05004957 /* setup wake call as IOCB callback */
4958 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
4959 /* setup context field to pass wait_queue pointer to wake function */
4960 pmboxq->context1 = &done_q;
4961
dea31012005-04-17 16:05:31 -05004962 /* now issue the command */
4963 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4964
4965 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04004966 wait_event_interruptible_timeout(done_q,
4967 pmboxq->mbox_flag & LPFC_MBX_WAKE,
4968 timeout * HZ);
4969
James Smart858c9f62007-06-17 19:56:39 -05004970 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05004971 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04004972 /*
4973 * if LPFC_MBX_WAKE flag is set the mailbox is completed
4974 * else do not free the resources.
4975 */
4976 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05004977 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05004978 else {
James Smart7054a602007-04-25 09:52:34 -04004979 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05004980 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4981 }
4982 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05004983 }
4984
dea31012005-04-17 16:05:31 -05004985 return retval;
4986}
4987
James Smarte59058c2008-08-24 21:49:00 -04004988/**
4989 * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
4990 * @phba: Pointer to HBA context.
4991 *
4992 * This function is called to cleanup any pending mailbox
4993 * objects in the driver queue before bringing the HBA offline.
4994 * This function is called while resetting the HBA.
4995 * The function is called without any lock held. The function
4996 * takes hbalock to update SLI data structure.
4997 * This function returns 1 when there is an active mailbox
4998 * command pending else returns 0.
4999 **/
James Smartb4c02652006-07-06 15:50:43 -04005000int
5001lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
5002{
James Smart2e0fef82007-06-17 19:56:36 -05005003 struct lpfc_vport *vport = phba->pport;
James Smartb4c02652006-07-06 15:50:43 -04005004 int i = 0;
James Smarted957682007-06-17 19:56:37 -05005005 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04005006
James Smart2e0fef82007-06-17 19:56:36 -05005007 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
James Smartb4c02652006-07-06 15:50:43 -04005008 if (i++ > LPFC_MBOX_TMO * 1000)
5009 return 1;
5010
James Smarted957682007-06-17 19:56:37 -05005011 /*
5012 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
5013 * did finish. This way we won't get the misleading
5014 * "Stray Mailbox Interrupt" message.
5015 */
5016 spin_lock_irq(&phba->hbalock);
5017 ha_copy = phba->work_ha;
5018 phba->work_ha &= ~HA_MBATT;
5019 spin_unlock_irq(&phba->hbalock);
5020
5021 if (ha_copy & HA_MBATT)
5022 if (lpfc_sli_handle_mb_event(phba) == 0)
5023 i = 0;
James Smartb4c02652006-07-06 15:50:43 -04005024
5025 msleep(1);
5026 }
5027
5028 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
5029}
5030
James Smarte59058c2008-08-24 21:49:00 -04005031/**
5032 * lpfc_intr_handler: The interrupt handler of lpfc driver.
5033 * @irq: Interrupt number.
5034 * @dev_id: The device context pointer.
5035 *
5036 * This function is called from the PCI layer when there is
5037 * an event in the HBA which requires driver attention. When
5038 * the PCI slot is in error recovery or the HBA is undergoing
5039 * initialization the interrupt handler will not process the
5040 * interrupt.
5041 * The error attention, link attention and els ring attention
5042 * events are handled by the worker thread. The interrupt
5043 * handler signals the worker thread and returns for these
5044 * events.
5045 * The SCSI ring event and mailbox events are handled in the
5046 * interrupt context.
5047 * This function is called without any lock held. It gets the
5048 * hbalock to access and update SLI data structures.
5049 * This function returns IRQ_HANDLED when interrupt is handled
5050 * else it returns IRQ_NONE.
5051 **/
dea31012005-04-17 16:05:31 -05005052irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01005053lpfc_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05005054{
James Smart2e0fef82007-06-17 19:56:36 -05005055 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05005056 uint32_t ha_copy;
5057 uint32_t work_ha_copy;
5058 unsigned long status;
dea31012005-04-17 16:05:31 -05005059 uint32_t control;
5060
James Smart92d7f7b2007-06-17 19:56:38 -05005061 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05005062 struct lpfc_vport *vport;
5063 struct lpfc_nodelist *ndlp;
5064 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05005065 LPFC_MBOXQ_t *pmb;
5066 int rc;
5067
dea31012005-04-17 16:05:31 -05005068 /*
5069 * Get the driver's phba structure from the dev_id and
5070 * assume the HBA is not interrupting.
5071 */
5072 phba = (struct lpfc_hba *) dev_id;
5073
5074 if (unlikely(!phba))
5075 return IRQ_NONE;
5076
Linas Vepstas8d63f372007-02-14 14:28:36 -06005077 /* If the pci channel is offline, ignore all the interrupts. */
5078 if (unlikely(pci_channel_offline(phba->pcidev)))
5079 return IRQ_NONE;
5080
dea31012005-04-17 16:05:31 -05005081 phba->sli.slistat.sli_intr++;
5082
5083 /*
5084 * Call the HBA to see if it is interrupting. If not, don't claim
5085 * the interrupt
5086 */
5087
5088 /* Ignore all interrupts during initialization. */
James Smart2e0fef82007-06-17 19:56:36 -05005089 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005090 return IRQ_NONE;
5091
5092 /*
5093 * Read host attention register to determine interrupt source
5094 * Clear Attention Sources, except Error Attention (to
5095 * preserve status) and Link Attention
5096 */
James Smart2e0fef82007-06-17 19:56:36 -05005097 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005098 ha_copy = readl(phba->HAregaddr);
James Smartebdbe652007-04-25 09:53:15 -04005099 /* If somebody is waiting to handle an eratt don't process it
5100 * here. The brdkill function will do this.
5101 */
James Smart2e0fef82007-06-17 19:56:36 -05005102 if (phba->link_flag & LS_IGNORE_ERATT)
James Smartebdbe652007-04-25 09:53:15 -04005103 ha_copy &= ~HA_ERATT;
dea31012005-04-17 16:05:31 -05005104 writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
5105 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05005106 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005107
5108 if (unlikely(!ha_copy))
5109 return IRQ_NONE;
5110
5111 work_ha_copy = ha_copy & phba->work_ha_mask;
5112
5113 if (unlikely(work_ha_copy)) {
5114 if (work_ha_copy & HA_LATT) {
5115 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
5116 /*
5117 * Turn off Link Attention interrupts
5118 * until CLEAR_LA done
5119 */
James Smart2e0fef82007-06-17 19:56:36 -05005120 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005121 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
5122 control = readl(phba->HCregaddr);
5123 control &= ~HC_LAINT_ENA;
5124 writel(control, phba->HCregaddr);
5125 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05005126 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005127 }
5128 else
5129 work_ha_copy &= ~HA_LATT;
5130 }
5131
5132 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05005133 /*
5134 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
5135 * the only slow ring.
5136 */
5137 status = (work_ha_copy &
5138 (HA_RXMASK << (4*LPFC_ELS_RING)));
5139 status >>= (4*LPFC_ELS_RING);
5140 if (status & HA_RXMASK) {
5141 spin_lock(&phba->hbalock);
5142 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04005143
5144 lpfc_debugfs_slow_ring_trc(phba,
5145 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
5146 control, status,
5147 (uint32_t)phba->sli.slistat.sli_intr);
5148
James Smart858c9f62007-06-17 19:56:39 -05005149 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04005150 lpfc_debugfs_slow_ring_trc(phba,
5151 "ISR Disable ring:"
5152 "pwork:x%x hawork:x%x wait:x%x",
5153 phba->work_ha, work_ha_copy,
5154 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005155 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005156
James Smart858c9f62007-06-17 19:56:39 -05005157 control &=
5158 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05005159 writel(control, phba->HCregaddr);
5160 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05005161 }
James Smarta58cbd52007-08-02 11:09:43 -04005162 else {
5163 lpfc_debugfs_slow_ring_trc(phba,
5164 "ISR slow ring: pwork:"
5165 "x%x hawork:x%x wait:x%x",
5166 phba->work_ha, work_ha_copy,
5167 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005168 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005169 }
James Smart858c9f62007-06-17 19:56:39 -05005170 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005171 }
5172 }
5173
5174 if (work_ha_copy & HA_ERATT) {
dea31012005-04-17 16:05:31 -05005175 /*
5176 * There was a link/board error. Read the
5177 * status register to retrieve the error event
5178 * and process it.
5179 */
5180 phba->sli.slistat.err_attn_event++;
5181 /* Save status info */
5182 phba->work_hs = readl(phba->HSregaddr);
5183 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
5184 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
5185
5186 /* Clear Chip error bit */
5187 writel(HA_ERATT, phba->HAregaddr);
5188 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05005189 phba->pport->stopped = 1;
dea31012005-04-17 16:05:31 -05005190 }
5191
James Smart97eab632008-04-07 10:16:05 -04005192 spin_lock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005193 if ((work_ha_copy & HA_MBATT) &&
5194 (phba->sli.mbox_active)) {
5195 pmb = phba->sli.mbox_active;
5196 pmbox = &pmb->mb;
5197 mbox = &phba->slim2p->mbx;
James Smart858c9f62007-06-17 19:56:39 -05005198 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005199
5200 /* First check out the status word */
5201 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
5202 if (pmbox->mbxOwner != OWN_HOST) {
James Smart97eab632008-04-07 10:16:05 -04005203 spin_unlock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005204 /*
5205 * Stray Mailbox Interrupt, mbxCommand <cmd>
5206 * mbxStatus <status>
5207 */
James Smart09372822008-01-11 01:52:54 -05005208 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05005209 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005210 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05005211 "Interrupt mbxCommand x%x "
5212 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005213 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05005214 pmbox->mbxCommand,
5215 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05005216 /* clear mailbox attention bit */
5217 work_ha_copy &= ~HA_MBATT;
5218 } else {
James Smart97eab632008-04-07 10:16:05 -04005219 phba->sli.mbox_active = NULL;
5220 spin_unlock(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05005221 phba->last_completion_time = jiffies;
5222 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05005223 if (pmb->mbox_cmpl) {
5224 lpfc_sli_pcimem_bcopy(mbox, pmbox,
5225 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05005226 }
James Smart09372822008-01-11 01:52:54 -05005227 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
5228 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
5229
5230 lpfc_debugfs_disc_trc(vport,
5231 LPFC_DISC_TRC_MBOX_VPORT,
5232 "MBOX dflt rpi: : "
5233 "status:x%x rpi:x%x",
5234 (uint32_t)pmbox->mbxStatus,
5235 pmbox->un.varWords[0], 0);
5236
5237 if (!pmbox->mbxStatus) {
5238 mp = (struct lpfc_dmabuf *)
5239 (pmb->context1);
5240 ndlp = (struct lpfc_nodelist *)
5241 pmb->context2;
5242
5243 /* Reg_LOGIN of dflt RPI was
5244 * successful. new lets get
5245 * rid of the RPI using the
5246 * same mbox buffer.
5247 */
5248 lpfc_unreg_login(phba,
5249 vport->vpi,
5250 pmbox->un.varWords[0],
5251 pmb);
5252 pmb->mbox_cmpl =
5253 lpfc_mbx_cmpl_dflt_rpi;
5254 pmb->context1 = mp;
5255 pmb->context2 = ndlp;
5256 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04005257 rc = lpfc_sli_issue_mbox(phba,
5258 pmb,
5259 MBX_NOWAIT);
5260 if (rc != MBX_BUSY)
5261 lpfc_printf_log(phba,
5262 KERN_ERR,
5263 LOG_MBOX | LOG_SLI,
5264 "0306 rc should have"
5265 "been MBX_BUSY");
James Smart09372822008-01-11 01:52:54 -05005266 goto send_current_mbox;
5267 }
5268 }
5269 spin_lock(&phba->pport->work_port_lock);
5270 phba->pport->work_port_events &=
5271 ~WORKER_MBOX_TMO;
5272 spin_unlock(&phba->pport->work_port_lock);
5273 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05005274 }
James Smart97eab632008-04-07 10:16:05 -04005275 } else
5276 spin_unlock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005277 if ((work_ha_copy & HA_MBATT) &&
5278 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05005279send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05005280 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04005281 do {
5282 rc = lpfc_sli_issue_mbox(phba, NULL,
5283 MBX_NOWAIT);
5284 } while (rc == MBX_NOT_FINISHED);
5285 if (rc != MBX_SUCCESS)
5286 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
5287 LOG_SLI, "0349 rc should be "
5288 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05005289 }
5290
James Smart2e0fef82007-06-17 19:56:36 -05005291 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005292 phba->work_ha |= work_ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05005293 spin_unlock(&phba->hbalock);
James Smart5e9d9b82008-06-14 22:52:53 -04005294 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005295 }
5296
5297 ha_copy &= ~(phba->work_ha_mask);
5298
5299 /*
5300 * Process all events on FCP ring. Take the optimized path for
5301 * FCP IO. Any other IO is slow path and is handled by
5302 * the worker thread.
5303 */
5304 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
5305 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05005306 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05005307 lpfc_sli_handle_fast_ring_event(phba,
5308 &phba->sli.ring[LPFC_FCP_RING],
5309 status);
James Smarta4bc3372006-12-02 13:34:16 -05005310
5311 if (phba->cfg_multi_ring_support == 2) {
5312 /*
5313 * Process all events on extra ring. Take the optimized path
5314 * for extra ring IO. Any other IO is slow path and is handled
5315 * by the worker thread.
5316 */
5317 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
5318 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05005319 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05005320 lpfc_sli_handle_fast_ring_event(phba,
5321 &phba->sli.ring[LPFC_EXTRA_RING],
5322 status);
5323 }
5324 }
dea31012005-04-17 16:05:31 -05005325 return IRQ_HANDLED;
5326
5327} /* lpfc_intr_handler */