blob: e2d07d97fa8bdb20519778e5a884d0dc351fff90 [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"
James Smartea2151b2008-09-07 11:52:10 -040035#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_disc.h"
37#include "lpfc_scsi.h"
38#include "lpfc.h"
39#include "lpfc_crtn.h"
40#include "lpfc_logmsg.h"
41#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050042#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050043
44/*
45 * Define macro to log: Mailbox command x%x cannot issue Data
46 * This allows multiple uses of lpfc_msgBlk0311
47 * w/o perturbing log msg utility.
48 */
James Smart92d7f7b2007-06-17 19:56:38 -050049#define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
dea31012005-04-17 16:05:31 -050050 lpfc_printf_log(phba, \
51 KERN_INFO, \
52 LOG_MBOX | LOG_SLI, \
James Smarte8b62012007-08-02 11:10:09 -040053 "(%d):0311 Mailbox command x%x cannot " \
James Smart92d7f7b2007-06-17 19:56:38 -050054 "issue Data: x%x x%x x%x\n", \
James Smart92d7f7b2007-06-17 19:56:38 -050055 pmbox->vport ? pmbox->vport->vpi : 0, \
56 pmbox->mb.mbxCommand, \
James Smart2e0fef82007-06-17 19:56:36 -050057 phba->pport->port_state, \
dea31012005-04-17 16:05:31 -050058 psli->sli_flag, \
James Smart2e0fef82007-06-17 19:56:36 -050059 flag)
dea31012005-04-17 16:05:31 -050060
61
62/* There are only four IOCB completion types. */
63typedef enum _lpfc_iocb_type {
64 LPFC_UNKNOWN_IOCB,
65 LPFC_UNSOL_IOCB,
66 LPFC_SOL_IOCB,
67 LPFC_ABORT_IOCB
68} lpfc_iocb_type;
69
James Smarte59058c2008-08-24 21:49:00 -040070/**
James Smart3621a712009-04-06 18:47:14 -040071 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -040072 * @phba: Pointer to HBA context object.
73 * @pring: Pointer to driver SLI ring object.
74 *
75 * This function returns pointer to next command iocb entry
76 * in the command ring. The caller must hold hbalock to prevent
77 * other threads consume the next command iocb.
78 * SLI-2/SLI-3 provide different sized iocbs.
79 **/
James Smarted957682007-06-17 19:56:37 -050080static inline IOCB_t *
81lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
82{
83 return (IOCB_t *) (((char *) pring->cmdringaddr) +
84 pring->cmdidx * phba->iocb_cmd_size);
85}
86
James Smarte59058c2008-08-24 21:49:00 -040087/**
James Smart3621a712009-04-06 18:47:14 -040088 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -040089 * @phba: Pointer to HBA context object.
90 * @pring: Pointer to driver SLI ring object.
91 *
92 * This function returns pointer to next response iocb entry
93 * in the response ring. The caller must hold hbalock to make sure
94 * that no other thread consume the next response iocb.
95 * SLI-2/SLI-3 provide different sized iocbs.
96 **/
James Smarted957682007-06-17 19:56:37 -050097static inline IOCB_t *
98lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
99{
100 return (IOCB_t *) (((char *) pring->rspringaddr) +
101 pring->rspidx * phba->iocb_rsp_size);
102}
103
James Smarte59058c2008-08-24 21:49:00 -0400104/**
James Smart3621a712009-04-06 18:47:14 -0400105 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400106 * @phba: Pointer to HBA context object.
107 *
108 * This function is called with hbalock held. This function
109 * allocates a new driver iocb object from the iocb pool. If the
110 * allocation is successful, it returns pointer to the newly
111 * allocated iocb object else it returns NULL.
112 **/
James Smart2e0fef82007-06-17 19:56:36 -0500113static struct lpfc_iocbq *
114__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400115{
116 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
117 struct lpfc_iocbq * iocbq = NULL;
118
119 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
120 return iocbq;
121}
122
James Smarte59058c2008-08-24 21:49:00 -0400123/**
James Smart3621a712009-04-06 18:47:14 -0400124 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400125 * @phba: Pointer to HBA context object.
126 *
127 * This function is called with no lock held. This function
128 * allocates a new driver iocb object from the iocb pool. If the
129 * allocation is successful, it returns pointer to the newly
130 * allocated iocb object else it returns NULL.
131 **/
James Smart2e0fef82007-06-17 19:56:36 -0500132struct lpfc_iocbq *
133lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500134{
James Smart2e0fef82007-06-17 19:56:36 -0500135 struct lpfc_iocbq * iocbq = NULL;
136 unsigned long iflags;
137
138 spin_lock_irqsave(&phba->hbalock, iflags);
139 iocbq = __lpfc_sli_get_iocbq(phba);
140 spin_unlock_irqrestore(&phba->hbalock, iflags);
141 return iocbq;
142}
143
James Smarte59058c2008-08-24 21:49:00 -0400144/**
James Smart3772a992009-05-22 14:50:54 -0400145 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
146 * @phba: Pointer to HBA context object.
147 * @iocbq: Pointer to driver iocb object.
148 *
149 * This function is called with hbalock held to release driver
150 * iocb object to the iocb pool. The iotag in the iocb object
151 * does not change for each use of the iocb object. This function
152 * clears all other fields of the iocb object when it is freed.
153 **/
154static void
155__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
156{
157 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
158
159 /*
160 * Clean all volatile data fields, preserve iotag and node struct.
161 */
162 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
163 iocbq->sli4_xritag = NO_XRI;
164 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
165}
166
167/**
James Smart3621a712009-04-06 18:47:14 -0400168 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400169 * @phba: Pointer to HBA context object.
170 * @iocbq: Pointer to driver iocb object.
171 *
172 * This function is called with hbalock held to release driver
173 * iocb object to the iocb pool. The iotag in the iocb object
174 * does not change for each use of the iocb object. This function
175 * clears all other fields of the iocb object when it is freed.
176 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100177static void
James Smart2e0fef82007-06-17 19:56:36 -0500178__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
179{
James Smart3772a992009-05-22 14:50:54 -0400180 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Bottomley604a3e32005-10-29 10:28:33 -0500181}
182
James Smarte59058c2008-08-24 21:49:00 -0400183/**
James Smart3621a712009-04-06 18:47:14 -0400184 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400185 * @phba: Pointer to HBA context object.
186 * @iocbq: Pointer to driver iocb object.
187 *
188 * This function is called with no lock held to release the iocb to
189 * iocb pool.
190 **/
James Smart2e0fef82007-06-17 19:56:36 -0500191void
192lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
193{
194 unsigned long iflags;
195
196 /*
197 * Clean all volatile data fields, preserve iotag and node struct.
198 */
199 spin_lock_irqsave(&phba->hbalock, iflags);
200 __lpfc_sli_release_iocbq(phba, iocbq);
201 spin_unlock_irqrestore(&phba->hbalock, iflags);
202}
203
James Smarte59058c2008-08-24 21:49:00 -0400204/**
James Smarta257bf92009-04-06 18:48:10 -0400205 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
206 * @phba: Pointer to HBA context object.
207 * @iocblist: List of IOCBs.
208 * @ulpstatus: ULP status in IOCB command field.
209 * @ulpWord4: ULP word-4 in IOCB command field.
210 *
211 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
212 * on the list by invoking the complete callback function associated with the
213 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
214 * fields.
215 **/
216void
217lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
218 uint32_t ulpstatus, uint32_t ulpWord4)
219{
220 struct lpfc_iocbq *piocb;
221
222 while (!list_empty(iocblist)) {
223 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
224
225 if (!piocb->iocb_cmpl)
226 lpfc_sli_release_iocbq(phba, piocb);
227 else {
228 piocb->iocb.ulpStatus = ulpstatus;
229 piocb->iocb.un.ulpWord[4] = ulpWord4;
230 (piocb->iocb_cmpl) (phba, piocb, piocb);
231 }
232 }
233 return;
234}
235
236/**
James Smart3621a712009-04-06 18:47:14 -0400237 * lpfc_sli_iocb_cmd_type - Get the iocb type
238 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400239 *
240 * This function is called by ring event handler function to get the iocb type.
241 * This function translates the iocb command to an iocb command type used to
242 * decide the final disposition of each completed IOCB.
243 * The function returns
244 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
245 * LPFC_SOL_IOCB if it is a solicited iocb completion
246 * LPFC_ABORT_IOCB if it is an abort iocb
247 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
248 *
249 * The caller is not required to hold any lock.
250 **/
dea31012005-04-17 16:05:31 -0500251static lpfc_iocb_type
252lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
253{
254 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
255
256 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
257 return 0;
258
259 switch (iocb_cmnd) {
260 case CMD_XMIT_SEQUENCE_CR:
261 case CMD_XMIT_SEQUENCE_CX:
262 case CMD_XMIT_BCAST_CN:
263 case CMD_XMIT_BCAST_CX:
264 case CMD_ELS_REQUEST_CR:
265 case CMD_ELS_REQUEST_CX:
266 case CMD_CREATE_XRI_CR:
267 case CMD_CREATE_XRI_CX:
268 case CMD_GET_RPI_CN:
269 case CMD_XMIT_ELS_RSP_CX:
270 case CMD_GET_RPI_CR:
271 case CMD_FCP_IWRITE_CR:
272 case CMD_FCP_IWRITE_CX:
273 case CMD_FCP_IREAD_CR:
274 case CMD_FCP_IREAD_CX:
275 case CMD_FCP_ICMND_CR:
276 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500277 case CMD_FCP_TSEND_CX:
278 case CMD_FCP_TRSP_CX:
279 case CMD_FCP_TRECEIVE_CX:
280 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500281 case CMD_ADAPTER_MSG:
282 case CMD_ADAPTER_DUMP:
283 case CMD_XMIT_SEQUENCE64_CR:
284 case CMD_XMIT_SEQUENCE64_CX:
285 case CMD_XMIT_BCAST64_CN:
286 case CMD_XMIT_BCAST64_CX:
287 case CMD_ELS_REQUEST64_CR:
288 case CMD_ELS_REQUEST64_CX:
289 case CMD_FCP_IWRITE64_CR:
290 case CMD_FCP_IWRITE64_CX:
291 case CMD_FCP_IREAD64_CR:
292 case CMD_FCP_IREAD64_CX:
293 case CMD_FCP_ICMND64_CR:
294 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500295 case CMD_FCP_TSEND64_CX:
296 case CMD_FCP_TRSP64_CX:
297 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500298 case CMD_GEN_REQUEST64_CR:
299 case CMD_GEN_REQUEST64_CX:
300 case CMD_XMIT_ELS_RSP64_CX:
301 type = LPFC_SOL_IOCB;
302 break;
303 case CMD_ABORT_XRI_CN:
304 case CMD_ABORT_XRI_CX:
305 case CMD_CLOSE_XRI_CN:
306 case CMD_CLOSE_XRI_CX:
307 case CMD_XRI_ABORTED_CX:
308 case CMD_ABORT_MXRI64_CN:
309 type = LPFC_ABORT_IOCB;
310 break;
311 case CMD_RCV_SEQUENCE_CX:
312 case CMD_RCV_ELS_REQ_CX:
313 case CMD_RCV_SEQUENCE64_CX:
314 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400315 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500316 case CMD_IOCB_RCV_SEQ64_CX:
317 case CMD_IOCB_RCV_ELS64_CX:
318 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500319 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500320 type = LPFC_UNSOL_IOCB;
321 break;
James Smart3163f722008-02-08 18:50:25 -0500322 case CMD_IOCB_XMIT_MSEQ64_CR:
323 case CMD_IOCB_XMIT_MSEQ64_CX:
324 case CMD_IOCB_RCV_SEQ_LIST64_CX:
325 case CMD_IOCB_RCV_ELS_LIST64_CX:
326 case CMD_IOCB_CLOSE_EXTENDED_CN:
327 case CMD_IOCB_ABORT_EXTENDED_CN:
328 case CMD_IOCB_RET_HBQE64_CN:
329 case CMD_IOCB_FCP_IBIDIR64_CR:
330 case CMD_IOCB_FCP_IBIDIR64_CX:
331 case CMD_IOCB_FCP_ITASKMGT64_CX:
332 case CMD_IOCB_LOGENTRY_CN:
333 case CMD_IOCB_LOGENTRY_ASYNC_CN:
334 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700335 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500336 type = LPFC_UNKNOWN_IOCB;
337 break;
dea31012005-04-17 16:05:31 -0500338 default:
339 type = LPFC_UNKNOWN_IOCB;
340 break;
341 }
342
343 return type;
344}
345
James Smarte59058c2008-08-24 21:49:00 -0400346/**
James Smart3621a712009-04-06 18:47:14 -0400347 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400348 * @phba: Pointer to HBA context object.
349 *
350 * This function is called from SLI initialization code
351 * to configure every ring of the HBA's SLI interface. The
352 * caller is not required to hold any lock. This function issues
353 * a config_ring mailbox command for each ring.
354 * This function returns zero if successful else returns a negative
355 * error code.
356 **/
dea31012005-04-17 16:05:31 -0500357static int
James Smarted957682007-06-17 19:56:37 -0500358lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500359{
360 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500361 LPFC_MBOXQ_t *pmb;
362 MAILBOX_t *pmbox;
363 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500364
James Smarted957682007-06-17 19:56:37 -0500365 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
366 if (!pmb)
367 return -ENOMEM;
368 pmbox = &pmb->mb;
369 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500370 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500371 lpfc_config_ring(phba, i, pmb);
372 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
373 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400375 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500376 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
377 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400378 rc, pmbox->mbxCommand,
379 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500380 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500381 ret = -ENXIO;
382 break;
dea31012005-04-17 16:05:31 -0500383 }
384 }
James Smarted957682007-06-17 19:56:37 -0500385 mempool_free(pmb, phba->mbox_mem_pool);
386 return ret;
dea31012005-04-17 16:05:31 -0500387}
388
James Smarte59058c2008-08-24 21:49:00 -0400389/**
James Smart3621a712009-04-06 18:47:14 -0400390 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400391 * @phba: Pointer to HBA context object.
392 * @pring: Pointer to driver SLI ring object.
393 * @piocb: Pointer to the driver iocb object.
394 *
395 * This function is called with hbalock held. The function adds the
396 * new iocb to txcmplq of the given ring. This function always returns
397 * 0. If this function is called for ELS ring, this function checks if
398 * there is a vport associated with the ELS command. This function also
399 * starts els_tmofunc timer if this is an ELS command.
400 **/
dea31012005-04-17 16:05:31 -0500401static int
James Smart2e0fef82007-06-17 19:56:36 -0500402lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
403 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500404{
dea31012005-04-17 16:05:31 -0500405 list_add_tail(&piocb->list, &pring->txcmplq);
406 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500407 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
408 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
409 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
410 if (!piocb->vport)
411 BUG();
412 else
413 mod_timer(&piocb->vport->els_tmofunc,
414 jiffies + HZ * (phba->fc_ratov << 1));
415 }
416
dea31012005-04-17 16:05:31 -0500417
James Smart2e0fef82007-06-17 19:56:36 -0500418 return 0;
dea31012005-04-17 16:05:31 -0500419}
420
James Smarte59058c2008-08-24 21:49:00 -0400421/**
James Smart3621a712009-04-06 18:47:14 -0400422 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400423 * @phba: Pointer to HBA context object.
424 * @pring: Pointer to driver SLI ring object.
425 *
426 * This function is called with hbalock held to get next
427 * iocb in txq of the given ring. If there is any iocb in
428 * the txq, the function returns first iocb in the list after
429 * removing the iocb from the list, else it returns NULL.
430 **/
dea31012005-04-17 16:05:31 -0500431static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500432lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500433{
dea31012005-04-17 16:05:31 -0500434 struct lpfc_iocbq *cmd_iocb;
435
James Smart858c9f62007-06-17 19:56:39 -0500436 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
437 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500438 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500439 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500440}
441
James Smarte59058c2008-08-24 21:49:00 -0400442/**
James Smart3621a712009-04-06 18:47:14 -0400443 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400444 * @phba: Pointer to HBA context object.
445 * @pring: Pointer to driver SLI ring object.
446 *
447 * This function is called with hbalock held and the caller must post the
448 * iocb without releasing the lock. If the caller releases the lock,
449 * iocb slot returned by the function is not guaranteed to be available.
450 * The function returns pointer to the next available iocb slot if there
451 * is available slot in the ring, else it returns NULL.
452 * If the get index of the ring is ahead of the put index, the function
453 * will post an error attention event to the worker thread to take the
454 * HBA to offline state.
455 **/
dea31012005-04-17 16:05:31 -0500456static IOCB_t *
457lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
458{
James Smart34b02dc2008-08-24 21:49:55 -0400459 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500460 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500461 if ((pring->next_cmdidx == pring->cmdidx) &&
462 (++pring->next_cmdidx >= max_cmd_idx))
463 pring->next_cmdidx = 0;
464
465 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
466
467 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
468
469 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
470 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400471 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200472 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400473 pring->ringno,
dea31012005-04-17 16:05:31 -0500474 pring->local_getidx, max_cmd_idx);
475
James Smart2e0fef82007-06-17 19:56:36 -0500476 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500477 /*
478 * All error attention handlers are posted to
479 * worker thread
480 */
481 phba->work_ha |= HA_ERATT;
482 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500483
James Smart5e9d9b82008-06-14 22:52:53 -0400484 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500485
486 return NULL;
487 }
488
489 if (pring->local_getidx == pring->next_cmdidx)
490 return NULL;
491 }
492
James Smarted957682007-06-17 19:56:37 -0500493 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500494}
495
James Smarte59058c2008-08-24 21:49:00 -0400496/**
James Smart3621a712009-04-06 18:47:14 -0400497 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400498 * @phba: Pointer to HBA context object.
499 * @iocbq: Pointer to driver iocb object.
500 *
501 * This function gets an iotag for the iocb. If there is no unused iotag and
502 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
503 * array and assigns a new iotag.
504 * The function returns the allocated iotag if successful, else returns zero.
505 * Zero is not a valid iotag.
506 * The caller is not required to hold any lock.
507 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500508uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500509lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500510{
James Smart2e0fef82007-06-17 19:56:36 -0500511 struct lpfc_iocbq **new_arr;
512 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500513 size_t new_len;
514 struct lpfc_sli *psli = &phba->sli;
515 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500516
James Smart2e0fef82007-06-17 19:56:36 -0500517 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500518 iotag = psli->last_iotag;
519 if(++iotag < psli->iocbq_lookup_len) {
520 psli->last_iotag = iotag;
521 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500522 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500523 iocbq->iotag = iotag;
524 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500525 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500526 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
527 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500528 spin_unlock_irq(&phba->hbalock);
529 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500530 GFP_KERNEL);
531 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500532 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500533 old_arr = psli->iocbq_lookup;
534 if (new_len <= psli->iocbq_lookup_len) {
535 /* highly unprobable case */
536 kfree(new_arr);
537 iotag = psli->last_iotag;
538 if(++iotag < psli->iocbq_lookup_len) {
539 psli->last_iotag = iotag;
540 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500541 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500542 iocbq->iotag = iotag;
543 return iotag;
544 }
James Smart2e0fef82007-06-17 19:56:36 -0500545 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500546 return 0;
547 }
548 if (psli->iocbq_lookup)
549 memcpy(new_arr, old_arr,
550 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -0400551 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -0500552 psli->iocbq_lookup = new_arr;
553 psli->iocbq_lookup_len = new_len;
554 psli->last_iotag = iotag;
555 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500556 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500557 iocbq->iotag = iotag;
558 kfree(old_arr);
559 return iotag;
560 }
James Smart8f6d98d2006-08-01 07:34:00 -0400561 } else
James Smart2e0fef82007-06-17 19:56:36 -0500562 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500563
James Bottomley604a3e32005-10-29 10:28:33 -0500564 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400565 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
566 psli->last_iotag);
dea31012005-04-17 16:05:31 -0500567
James Bottomley604a3e32005-10-29 10:28:33 -0500568 return 0;
dea31012005-04-17 16:05:31 -0500569}
570
James Smarte59058c2008-08-24 21:49:00 -0400571/**
James Smart3621a712009-04-06 18:47:14 -0400572 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -0400573 * @phba: Pointer to HBA context object.
574 * @pring: Pointer to driver SLI ring object.
575 * @iocb: Pointer to iocb slot in the ring.
576 * @nextiocb: Pointer to driver iocb object which need to be
577 * posted to firmware.
578 *
579 * This function is called with hbalock held to post a new iocb to
580 * the firmware. This function copies the new iocb to ring iocb slot and
581 * updates the ring pointers. It adds the new iocb to txcmplq if there is
582 * a completion call back for this iocb else the function will free the
583 * iocb object.
584 **/
dea31012005-04-17 16:05:31 -0500585static void
586lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
587 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
588{
589 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500590 * Set up an iotag
dea31012005-04-17 16:05:31 -0500591 */
James Bottomley604a3e32005-10-29 10:28:33 -0500592 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500593
James Smarte2a0a9d2008-12-04 22:40:02 -0500594
James Smarta58cbd52007-08-02 11:09:43 -0400595 if (pring->ringno == LPFC_ELS_RING) {
596 lpfc_debugfs_slow_ring_trc(phba,
597 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
598 *(((uint32_t *) &nextiocb->iocb) + 4),
599 *(((uint32_t *) &nextiocb->iocb) + 6),
600 *(((uint32_t *) &nextiocb->iocb) + 7));
601 }
602
dea31012005-04-17 16:05:31 -0500603 /*
604 * Issue iocb command to adapter
605 */
James Smart92d7f7b2007-06-17 19:56:38 -0500606 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500607 wmb();
608 pring->stats.iocb_cmd++;
609
610 /*
611 * If there is no completion routine to call, we can release the
612 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
613 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
614 */
615 if (nextiocb->iocb_cmpl)
616 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500617 else
James Smart2e0fef82007-06-17 19:56:36 -0500618 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500619
620 /*
621 * Let the HBA know what IOCB slot will be the next one the
622 * driver will put a command into.
623 */
624 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500625 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500626}
627
James Smarte59058c2008-08-24 21:49:00 -0400628/**
James Smart3621a712009-04-06 18:47:14 -0400629 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -0400630 * @phba: Pointer to HBA context object.
631 * @pring: Pointer to driver SLI ring object.
632 *
633 * The caller is not required to hold any lock for calling this function.
634 * This function updates the chip attention bits for the ring to inform firmware
635 * that there are pending work to be done for this ring and requests an
636 * interrupt when there is space available in the ring. This function is
637 * called when the driver is unable to post more iocbs to the ring due
638 * to unavailability of space in the ring.
639 **/
dea31012005-04-17 16:05:31 -0500640static void
James Smart2e0fef82007-06-17 19:56:36 -0500641lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500642{
643 int ringno = pring->ringno;
644
645 pring->flag |= LPFC_CALL_RING_AVAILABLE;
646
647 wmb();
648
649 /*
650 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
651 * The HBA will tell us when an IOCB entry is available.
652 */
653 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
654 readl(phba->CAregaddr); /* flush */
655
656 pring->stats.iocb_cmd_full++;
657}
658
James Smarte59058c2008-08-24 21:49:00 -0400659/**
James Smart3621a712009-04-06 18:47:14 -0400660 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -0400661 * @phba: Pointer to HBA context object.
662 * @pring: Pointer to driver SLI ring object.
663 *
664 * This function updates the chip attention register bit for the
665 * given ring to inform HBA that there is more work to be done
666 * in this ring. The caller is not required to hold any lock.
667 **/
dea31012005-04-17 16:05:31 -0500668static void
James Smart2e0fef82007-06-17 19:56:36 -0500669lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500670{
671 int ringno = pring->ringno;
672
673 /*
674 * Tell the HBA that there is work to do in this ring.
675 */
James Smart34b02dc2008-08-24 21:49:55 -0400676 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
677 wmb();
678 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
679 readl(phba->CAregaddr); /* flush */
680 }
dea31012005-04-17 16:05:31 -0500681}
682
James Smarte59058c2008-08-24 21:49:00 -0400683/**
James Smart3621a712009-04-06 18:47:14 -0400684 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -0400685 * @phba: Pointer to HBA context object.
686 * @pring: Pointer to driver SLI ring object.
687 *
688 * This function is called with hbalock held to post pending iocbs
689 * in the txq to the firmware. This function is called when driver
690 * detects space available in the ring.
691 **/
dea31012005-04-17 16:05:31 -0500692static void
James Smart2e0fef82007-06-17 19:56:36 -0500693lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500694{
695 IOCB_t *iocb;
696 struct lpfc_iocbq *nextiocb;
697
698 /*
699 * Check to see if:
700 * (a) there is anything on the txq to send
701 * (b) link is up
702 * (c) link attention events can be processed (fcp ring only)
703 * (d) IOCB processing is not blocked by the outstanding mbox command.
704 */
705 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500706 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500707 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -0400708 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -0500709
710 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
711 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
712 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
713
714 if (iocb)
715 lpfc_sli_update_ring(phba, pring);
716 else
717 lpfc_sli_update_full_ring(phba, pring);
718 }
719
720 return;
721}
722
James Smarte59058c2008-08-24 21:49:00 -0400723/**
James Smart3621a712009-04-06 18:47:14 -0400724 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -0400725 * @phba: Pointer to HBA context object.
726 * @hbqno: HBQ number.
727 *
728 * This function is called with hbalock held to get the next
729 * available slot for the given HBQ. If there is free slot
730 * available for the HBQ it will return pointer to the next available
731 * HBQ entry else it will return NULL.
732 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100733static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500734lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
735{
736 struct hbq_s *hbqp = &phba->hbqs[hbqno];
737
738 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
739 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
740 hbqp->next_hbqPutIdx = 0;
741
742 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500743 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500744 uint32_t getidx = le32_to_cpu(raw_index);
745
746 hbqp->local_hbqGetIdx = getidx;
747
748 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
749 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500750 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400751 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500752 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400753 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500754 hbqp->entry_count);
755
756 phba->link_state = LPFC_HBA_ERROR;
757 return NULL;
758 }
759
760 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
761 return NULL;
762 }
763
James Smart51ef4c22007-08-02 11:10:31 -0400764 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
765 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500766}
767
James Smarte59058c2008-08-24 21:49:00 -0400768/**
James Smart3621a712009-04-06 18:47:14 -0400769 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -0400770 * @phba: Pointer to HBA context object.
771 *
772 * This function is called with no lock held to free all the
773 * hbq buffers while uninitializing the SLI interface. It also
774 * frees the HBQ buffers returned by the firmware but not yet
775 * processed by the upper layers.
776 **/
James Smarted957682007-06-17 19:56:37 -0500777void
778lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
779{
James Smart92d7f7b2007-06-17 19:56:38 -0500780 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
781 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -0500782 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -0400783 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -0500784 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500785
James Smart51ef4c22007-08-02 11:10:31 -0400786 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500787 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -0500788 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -0400789 for (i = 0; i < hbq_count; ++i) {
790 list_for_each_entry_safe(dmabuf, next_dmabuf,
791 &phba->hbqs[i].hbq_buffer_list, list) {
792 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
793 list_del(&hbq_buf->dbuf.list);
794 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
795 }
James Smarta8adb832007-10-27 13:37:53 -0400796 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -0500797 }
James Smart3163f722008-02-08 18:50:25 -0500798 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -0400799 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
800 list) {
James Smart3163f722008-02-08 18:50:25 -0500801 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
802 list_del(&hbq_buf->dbuf.list);
803 if (hbq_buf->tag == -1) {
804 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
805 (phba, hbq_buf);
806 } else {
807 hbqno = hbq_buf->tag >> 16;
808 if (hbqno >= LPFC_MAX_HBQS)
809 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
810 (phba, hbq_buf);
811 else
812 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
813 hbq_buf);
814 }
815 }
816
817 /* Mark the HBQs not in use */
818 phba->hbq_in_use = 0;
819 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -0500820}
821
James Smarte59058c2008-08-24 21:49:00 -0400822/**
James Smart3621a712009-04-06 18:47:14 -0400823 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -0400824 * @phba: Pointer to HBA context object.
825 * @hbqno: HBQ number.
826 * @hbq_buf: Pointer to HBQ buffer.
827 *
828 * This function is called with the hbalock held to post a
829 * hbq buffer to the firmware. If the function finds an empty
830 * slot in the HBQ, it will post the buffer. The function will return
831 * pointer to the hbq entry if it successfully post the buffer
832 * else it will return NULL.
833 **/
James Smart3772a992009-05-22 14:50:54 -0400834static int
James Smarted957682007-06-17 19:56:37 -0500835lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500836 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500837{
James Smart3772a992009-05-22 14:50:54 -0400838 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
839}
840
841/**
842 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
843 * @phba: Pointer to HBA context object.
844 * @hbqno: HBQ number.
845 * @hbq_buf: Pointer to HBQ buffer.
846 *
847 * This function is called with the hbalock held to post a hbq buffer to the
848 * firmware. If the function finds an empty slot in the HBQ, it will post the
849 * buffer and place it on the hbq_buffer_list. The function will return zero if
850 * it successfully post the buffer else it will return an error.
851 **/
852static int
853lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
854 struct hbq_dmabuf *hbq_buf)
855{
James Smarted957682007-06-17 19:56:37 -0500856 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500857 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500858
859 /* Get next HBQ entry slot to use */
860 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
861 if (hbqe) {
862 struct hbq_s *hbqp = &phba->hbqs[hbqno];
863
James Smart92d7f7b2007-06-17 19:56:38 -0500864 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
865 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400866 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500867 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500868 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
869 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
870 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500871 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
872 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500873 /* flush */
James Smarted957682007-06-17 19:56:37 -0500874 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400875 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -0400876 return 0;
877 } else
878 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -0500879}
880
James Smarte59058c2008-08-24 21:49:00 -0400881/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -0500882static struct lpfc_hbq_init lpfc_els_hbq = {
883 .rn = 1,
884 .entry_count = 200,
885 .mask_count = 0,
886 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400887 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500888 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -0400889 .init_count = 40,
890 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -0500891};
James Smarted957682007-06-17 19:56:37 -0500892
James Smarte59058c2008-08-24 21:49:00 -0400893/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -0400894static struct lpfc_hbq_init lpfc_extra_hbq = {
895 .rn = 1,
896 .entry_count = 200,
897 .mask_count = 0,
898 .profile = 0,
899 .ring_mask = (1 << LPFC_EXTRA_RING),
900 .buffer_count = 0,
901 .init_count = 0,
902 .add_count = 5,
903};
904
James Smarte59058c2008-08-24 21:49:00 -0400905/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -0400906struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500907 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400908 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500909};
910
James Smarte59058c2008-08-24 21:49:00 -0400911/**
James Smart3621a712009-04-06 18:47:14 -0400912 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -0400913 * @phba: Pointer to HBA context object.
914 * @hbqno: HBQ number.
915 * @count: Number of HBQ buffers to be posted.
916 *
James Smartd7c255b2008-08-24 21:50:00 -0400917 * This function is called with no lock held to post more hbq buffers to the
918 * given HBQ. The function returns the number of HBQ buffers successfully
919 * posted.
James Smarte59058c2008-08-24 21:49:00 -0400920 **/
James Smart311464e2007-08-02 11:10:37 -0400921static int
James Smart92d7f7b2007-06-17 19:56:38 -0500922lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
923{
James Smartd7c255b2008-08-24 21:50:00 -0400924 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -0500925 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500926 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -0400927 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700928 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -0400929 return 0;
James Smart51ef4c22007-08-02 11:10:31 -0400930
James Smartd7c255b2008-08-24 21:50:00 -0400931 if ((phba->hbqs[hbqno].buffer_count + count) >
932 lpfc_hbq_defs[hbqno]->entry_count)
933 count = lpfc_hbq_defs[hbqno]->entry_count -
934 phba->hbqs[hbqno].buffer_count;
935 if (!count)
936 return 0;
937 /* Allocate HBQ entries */
938 for (i = 0; i < count; i++) {
939 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
940 if (!hbq_buffer)
941 break;
942 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
943 }
James Smart3163f722008-02-08 18:50:25 -0500944 /* Check whether HBQ is still in use */
945 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700946 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -0400947 goto err;
948 while (!list_empty(&hbq_buf_list)) {
949 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
950 dbuf.list);
951 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
952 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -0400953 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -0400954 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -0400955 posted++;
956 } else
James Smart51ef4c22007-08-02 11:10:31 -0400957 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -0500958 }
James Smart3163f722008-02-08 18:50:25 -0500959 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -0400960 return posted;
961err:
962 spin_unlock_irqrestore(&phba->hbalock, flags);
963 while (!list_empty(&hbq_buf_list)) {
964 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
965 dbuf.list);
966 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
967 }
James Smart92d7f7b2007-06-17 19:56:38 -0500968 return 0;
James Smarted957682007-06-17 19:56:37 -0500969}
970
James Smarte59058c2008-08-24 21:49:00 -0400971/**
James Smart3621a712009-04-06 18:47:14 -0400972 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -0400973 * @phba: Pointer to HBA context object.
974 * @qno: HBQ number.
975 *
976 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -0400977 * is called with no lock held. The function returns the number of HBQ entries
978 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400979 **/
James Smarted957682007-06-17 19:56:37 -0500980int
James Smart92d7f7b2007-06-17 19:56:38 -0500981lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500982{
James Smart92d7f7b2007-06-17 19:56:38 -0500983 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
984 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -0500985}
986
James Smarte59058c2008-08-24 21:49:00 -0400987/**
James Smart3621a712009-04-06 18:47:14 -0400988 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -0400989 * @phba: Pointer to HBA context object.
990 * @qno: HBQ queue number.
991 *
992 * This function is called from SLI initialization code path with
993 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -0400994 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400995 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100996static int
James Smart92d7f7b2007-06-17 19:56:38 -0500997lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500998{
James Smart92d7f7b2007-06-17 19:56:38 -0500999 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1000 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -05001001}
1002
James Smarte59058c2008-08-24 21:49:00 -04001003/**
James Smart3772a992009-05-22 14:50:54 -04001004 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1005 * @phba: Pointer to HBA context object.
1006 * @hbqno: HBQ number.
1007 *
1008 * This function removes the first hbq buffer on an hbq list and returns a
1009 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1010 **/
1011static struct hbq_dmabuf *
1012lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1013{
1014 struct lpfc_dmabuf *d_buf;
1015
1016 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1017 if (!d_buf)
1018 return NULL;
1019 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1020}
1021
1022/**
James Smart3621a712009-04-06 18:47:14 -04001023 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001024 * @phba: Pointer to HBA context object.
1025 * @tag: Tag of the hbq buffer.
1026 *
1027 * This function is called with hbalock held. This function searches
1028 * for the hbq buffer associated with the given tag in the hbq buffer
1029 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1030 * it returns NULL.
1031 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001032static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001033lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1034{
James Smart92d7f7b2007-06-17 19:56:38 -05001035 struct lpfc_dmabuf *d_buf;
1036 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001037 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001038
James Smart51ef4c22007-08-02 11:10:31 -04001039 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001040 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001041 return NULL;
1042
James Smart3772a992009-05-22 14:50:54 -04001043 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001044 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001045 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001046 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001047 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001048 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001049 }
1050 }
James Smart3772a992009-05-22 14:50:54 -04001051 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001053 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001054 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001055 return NULL;
James Smarted957682007-06-17 19:56:37 -05001056}
1057
James Smarte59058c2008-08-24 21:49:00 -04001058/**
James Smart3621a712009-04-06 18:47:14 -04001059 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001060 * @phba: Pointer to HBA context object.
1061 * @hbq_buffer: Pointer to HBQ buffer.
1062 *
1063 * This function is called with hbalock. This function gives back
1064 * the hbq buffer to firmware. If the HBQ does not have space to
1065 * post the buffer, it will free the buffer.
1066 **/
James Smarted957682007-06-17 19:56:37 -05001067void
James Smart51ef4c22007-08-02 11:10:31 -04001068lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001069{
1070 uint32_t hbqno;
1071
James Smart51ef4c22007-08-02 11:10:31 -04001072 if (hbq_buffer) {
1073 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001074 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001075 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001076 }
1077}
1078
James Smarte59058c2008-08-24 21:49:00 -04001079/**
James Smart3621a712009-04-06 18:47:14 -04001080 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001081 * @mbxCommand: mailbox command code.
1082 *
1083 * This function is called by the mailbox event handler function to verify
1084 * that the completed mailbox command is a legitimate mailbox command. If the
1085 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1086 * and the mailbox event handler will take the HBA offline.
1087 **/
dea31012005-04-17 16:05:31 -05001088static int
1089lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1090{
1091 uint8_t ret;
1092
1093 switch (mbxCommand) {
1094 case MBX_LOAD_SM:
1095 case MBX_READ_NV:
1096 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001097 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001098 case MBX_RUN_BIU_DIAG:
1099 case MBX_INIT_LINK:
1100 case MBX_DOWN_LINK:
1101 case MBX_CONFIG_LINK:
1102 case MBX_CONFIG_RING:
1103 case MBX_RESET_RING:
1104 case MBX_READ_CONFIG:
1105 case MBX_READ_RCONFIG:
1106 case MBX_READ_SPARM:
1107 case MBX_READ_STATUS:
1108 case MBX_READ_RPI:
1109 case MBX_READ_XRI:
1110 case MBX_READ_REV:
1111 case MBX_READ_LNK_STAT:
1112 case MBX_REG_LOGIN:
1113 case MBX_UNREG_LOGIN:
1114 case MBX_READ_LA:
1115 case MBX_CLEAR_LA:
1116 case MBX_DUMP_MEMORY:
1117 case MBX_DUMP_CONTEXT:
1118 case MBX_RUN_DIAGS:
1119 case MBX_RESTART:
1120 case MBX_UPDATE_CFG:
1121 case MBX_DOWN_LOAD:
1122 case MBX_DEL_LD_ENTRY:
1123 case MBX_RUN_PROGRAM:
1124 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001125 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001126 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001127 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001128 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001129 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001130 case MBX_LOAD_AREA:
1131 case MBX_RUN_BIU_DIAG64:
1132 case MBX_CONFIG_PORT:
1133 case MBX_READ_SPARM64:
1134 case MBX_READ_RPI64:
1135 case MBX_REG_LOGIN64:
1136 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001137 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001138 case MBX_SET_DEBUG:
1139 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001140 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001141 case MBX_REG_VPI:
1142 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001143 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001144 case MBX_PORT_CAPABILITIES:
1145 case MBX_PORT_IOV_CONTROL:
dea31012005-04-17 16:05:31 -05001146 ret = mbxCommand;
1147 break;
1148 default:
1149 ret = MBX_SHUTDOWN;
1150 break;
1151 }
James Smart2e0fef82007-06-17 19:56:36 -05001152 return ret;
dea31012005-04-17 16:05:31 -05001153}
James Smarte59058c2008-08-24 21:49:00 -04001154
1155/**
James Smart3621a712009-04-06 18:47:14 -04001156 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001157 * @phba: Pointer to HBA context object.
1158 * @pmboxq: Pointer to mailbox command.
1159 *
1160 * This is completion handler function for mailbox commands issued from
1161 * lpfc_sli_issue_mbox_wait function. This function is called by the
1162 * mailbox event handler function with no lock held. This function
1163 * will wake up thread waiting on the wait queue pointed by context1
1164 * of the mailbox.
1165 **/
dea31012005-04-17 16:05:31 -05001166static void
James Smart2e0fef82007-06-17 19:56:36 -05001167lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001168{
1169 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001170 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001171
1172 /*
1173 * If pdone_q is empty, the driver thread gave up waiting and
1174 * continued running.
1175 */
James Smart7054a602007-04-25 09:52:34 -04001176 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001177 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001178 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1179 if (pdone_q)
1180 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001181 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001182 return;
1183}
1184
James Smarte59058c2008-08-24 21:49:00 -04001185
1186/**
James Smart3621a712009-04-06 18:47:14 -04001187 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001188 * @phba: Pointer to HBA context object.
1189 * @pmb: Pointer to mailbox object.
1190 *
1191 * This function is the default mailbox completion handler. It
1192 * frees the memory resources associated with the completed mailbox
1193 * command. If the completed command is a REG_LOGIN mailbox command,
1194 * this function will issue a UREG_LOGIN to re-claim the RPI.
1195 **/
dea31012005-04-17 16:05:31 -05001196void
James Smart2e0fef82007-06-17 19:56:36 -05001197lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001198{
1199 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -04001200 uint16_t rpi;
1201 int rc;
1202
dea31012005-04-17 16:05:31 -05001203 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001204
dea31012005-04-17 16:05:31 -05001205 if (mp) {
1206 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1207 kfree(mp);
1208 }
James Smart7054a602007-04-25 09:52:34 -04001209
1210 /*
1211 * If a REG_LOGIN succeeded after node is destroyed or node
1212 * is in re-discovery driver need to cleanup the RPI.
1213 */
James Smart2e0fef82007-06-17 19:56:36 -05001214 if (!(phba->pport->load_flag & FC_UNLOADING) &&
1215 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1216 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -04001217
1218 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -05001219 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1220 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001221 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1222 if (rc != MBX_NOT_FINISHED)
1223 return;
1224 }
1225
James Smart2e0fef82007-06-17 19:56:36 -05001226 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001227 return;
1228}
1229
James Smarte59058c2008-08-24 21:49:00 -04001230/**
James Smart3621a712009-04-06 18:47:14 -04001231 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001232 * @phba: Pointer to HBA context object.
1233 *
1234 * This function is called with no lock held. This function processes all
1235 * the completed mailbox commands and gives it to upper layers. The interrupt
1236 * service routine processes mailbox completion interrupt and adds completed
1237 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1238 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1239 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1240 * function returns the mailbox commands to the upper layer by calling the
1241 * completion handler function of each mailbox.
1242 **/
dea31012005-04-17 16:05:31 -05001243int
James Smart2e0fef82007-06-17 19:56:36 -05001244lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001245{
James Smart92d7f7b2007-06-17 19:56:38 -05001246 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001247 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001248 int rc;
1249 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001250
1251 phba->sli.slistat.mbox_event++;
1252
James Smart92d7f7b2007-06-17 19:56:38 -05001253 /* Get all completed mailboxe buffers into the cmplq */
1254 spin_lock_irq(&phba->hbalock);
1255 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1256 spin_unlock_irq(&phba->hbalock);
1257
dea31012005-04-17 16:05:31 -05001258 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001259 do {
1260 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1261 if (pmb == NULL)
1262 break;
1263
dea31012005-04-17 16:05:31 -05001264 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -05001265
James Smart858c9f62007-06-17 19:56:39 -05001266 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1267 if (pmb->vport) {
1268 lpfc_debugfs_disc_trc(pmb->vport,
1269 LPFC_DISC_TRC_MBOX_VPORT,
1270 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1271 (uint32_t)pmbox->mbxCommand,
1272 pmbox->un.varWords[0],
1273 pmbox->un.varWords[1]);
1274 }
1275 else {
1276 lpfc_debugfs_disc_trc(phba->pport,
1277 LPFC_DISC_TRC_MBOX,
1278 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1279 (uint32_t)pmbox->mbxCommand,
1280 pmbox->un.varWords[0],
1281 pmbox->un.varWords[1]);
1282 }
1283 }
1284
dea31012005-04-17 16:05:31 -05001285 /*
1286 * It is a fatal error if unknown mbox command completion.
1287 */
1288 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1289 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001290 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001291 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001292 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -05001293 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001294 pmb->vport ? pmb->vport->vpi : 0,
1295 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -05001296 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001297 phba->work_hs = HS_FFER3;
1298 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001299 continue;
dea31012005-04-17 16:05:31 -05001300 }
1301
dea31012005-04-17 16:05:31 -05001302 if (pmbox->mbxStatus) {
1303 phba->sli.slistat.mbox_stat_err++;
1304 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1305 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001306 lpfc_printf_log(phba, KERN_INFO,
1307 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001308 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001309 "error - RETRYing Data: x%x "
1310 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001311 pmb->vport ? pmb->vport->vpi :0,
1312 pmbox->mbxCommand,
1313 pmbox->mbxStatus,
1314 pmbox->un.varWords[0],
1315 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001316 pmbox->mbxStatus = 0;
1317 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -05001318 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001319 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001320 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001321 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1322 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -05001323 continue;
dea31012005-04-17 16:05:31 -05001324 }
1325 }
1326
1327 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001328 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001329 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -05001330 "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 -05001331 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001332 pmbox->mbxCommand,
1333 pmb->mbox_cmpl,
1334 *((uint32_t *) pmbox),
1335 pmbox->un.varWords[0],
1336 pmbox->un.varWords[1],
1337 pmbox->un.varWords[2],
1338 pmbox->un.varWords[3],
1339 pmbox->un.varWords[4],
1340 pmbox->un.varWords[5],
1341 pmbox->un.varWords[6],
1342 pmbox->un.varWords[7]);
1343
James Smart92d7f7b2007-06-17 19:56:38 -05001344 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001345 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001346 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001347 return 0;
dea31012005-04-17 16:05:31 -05001348}
James Smart92d7f7b2007-06-17 19:56:38 -05001349
James Smarte59058c2008-08-24 21:49:00 -04001350/**
James Smart3621a712009-04-06 18:47:14 -04001351 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001352 * @phba: Pointer to HBA context object.
1353 * @pring: Pointer to driver SLI ring object.
1354 * @tag: buffer tag.
1355 *
1356 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1357 * is set in the tag the buffer is posted for a particular exchange,
1358 * the function will return the buffer without replacing the buffer.
1359 * If the buffer is for unsolicited ELS or CT traffic, this function
1360 * returns the buffer and also posts another buffer to the firmware.
1361 **/
James Smart76bb24e2007-10-27 13:38:00 -04001362static struct lpfc_dmabuf *
1363lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001364 struct lpfc_sli_ring *pring,
1365 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001366{
James Smart9f1e1b52008-12-04 22:39:40 -05001367 struct hbq_dmabuf *hbq_entry;
1368
James Smart76bb24e2007-10-27 13:38:00 -04001369 if (tag & QUE_BUFTAG_BIT)
1370 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001371 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1372 if (!hbq_entry)
1373 return NULL;
1374 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001375}
James Smart57127f12007-10-27 13:37:05 -04001376
James Smart3772a992009-05-22 14:50:54 -04001377/**
1378 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1379 * @phba: Pointer to HBA context object.
1380 * @pring: Pointer to driver SLI ring object.
1381 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1382 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1383 * @fch_type: the type for the first frame of the sequence.
1384 *
1385 * This function is called with no lock held. This function uses the r_ctl and
1386 * type of the received sequence to find the correct callback function to call
1387 * to process the sequence.
1388 **/
1389static int
1390lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1391 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1392 uint32_t fch_type)
1393{
1394 int i;
1395
1396 /* unSolicited Responses */
1397 if (pring->prt[0].profile) {
1398 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1399 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1400 saveq);
1401 return 1;
1402 }
1403 /* We must search, based on rctl / type
1404 for the right routine */
1405 for (i = 0; i < pring->num_mask; i++) {
1406 if ((pring->prt[i].rctl == fch_r_ctl) &&
1407 (pring->prt[i].type == fch_type)) {
1408 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1409 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1410 (phba, pring, saveq);
1411 return 1;
1412 }
1413 }
1414 return 0;
1415}
James Smarte59058c2008-08-24 21:49:00 -04001416
1417/**
James Smart3621a712009-04-06 18:47:14 -04001418 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001419 * @phba: Pointer to HBA context object.
1420 * @pring: Pointer to driver SLI ring object.
1421 * @saveq: Pointer to the unsolicited iocb.
1422 *
1423 * This function is called with no lock held by the ring event handler
1424 * when there is an unsolicited iocb posted to the response ring by the
1425 * firmware. This function gets the buffer associated with the iocbs
1426 * and calls the event handler for the ring. This function handles both
1427 * qring buffers and hbq buffers.
1428 * When the function returns 1 the caller can free the iocb object otherwise
1429 * upper layer functions will free the iocb objects.
1430 **/
dea31012005-04-17 16:05:31 -05001431static int
1432lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1433 struct lpfc_iocbq *saveq)
1434{
1435 IOCB_t * irsp;
1436 WORD5 * w5p;
1437 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001438 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04001439 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001440 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001441
1442 match = 0;
1443 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001444
1445 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1446 if (pring->lpfc_sli_rcv_async_status)
1447 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1448 else
1449 lpfc_printf_log(phba,
1450 KERN_WARNING,
1451 LOG_SLI,
1452 "0316 Ring %d handler: unexpected "
1453 "ASYNC_STATUS iocb received evt_code "
1454 "0x%x\n",
1455 pring->ringno,
1456 irsp->un.asyncstat.evt_code);
1457 return 1;
1458 }
1459
James Smart3163f722008-02-08 18:50:25 -05001460 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1461 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1462 if (irsp->ulpBdeCount > 0) {
1463 dmzbuf = lpfc_sli_get_buff(phba, pring,
1464 irsp->un.ulpWord[3]);
1465 lpfc_in_buf_free(phba, dmzbuf);
1466 }
1467
1468 if (irsp->ulpBdeCount > 1) {
1469 dmzbuf = lpfc_sli_get_buff(phba, pring,
1470 irsp->unsli3.sli3Words[3]);
1471 lpfc_in_buf_free(phba, dmzbuf);
1472 }
1473
1474 if (irsp->ulpBdeCount > 2) {
1475 dmzbuf = lpfc_sli_get_buff(phba, pring,
1476 irsp->unsli3.sli3Words[7]);
1477 lpfc_in_buf_free(phba, dmzbuf);
1478 }
1479
1480 return 1;
1481 }
1482
James Smart92d7f7b2007-06-17 19:56:38 -05001483 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001484 if (irsp->ulpBdeCount != 0) {
1485 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001486 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001487 if (!saveq->context2)
1488 lpfc_printf_log(phba,
1489 KERN_ERR,
1490 LOG_SLI,
1491 "0341 Ring %d Cannot find buffer for "
1492 "an unsolicited iocb. tag 0x%x\n",
1493 pring->ringno,
1494 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001495 }
1496 if (irsp->ulpBdeCount == 2) {
1497 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04001498 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04001499 if (!saveq->context3)
1500 lpfc_printf_log(phba,
1501 KERN_ERR,
1502 LOG_SLI,
1503 "0342 Ring %d Cannot find buffer for an"
1504 " unsolicited iocb. tag 0x%x\n",
1505 pring->ringno,
1506 irsp->unsli3.sli3Words[7]);
1507 }
1508 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04001509 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04001510 if (irsp->ulpBdeCount != 0) {
1511 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1512 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05001513 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04001514 lpfc_printf_log(phba,
1515 KERN_ERR,
1516 LOG_SLI,
1517 "0343 Ring %d Cannot find "
1518 "buffer for an unsolicited iocb"
1519 ". tag 0x%x\n", pring->ringno,
1520 irsp->un.ulpWord[3]);
1521 }
1522 if (irsp->ulpBdeCount == 2) {
1523 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1524 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05001525 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04001526 lpfc_printf_log(phba,
1527 KERN_ERR,
1528 LOG_SLI,
1529 "0344 Ring %d Cannot find "
1530 "buffer for an unsolicited "
1531 "iocb. tag 0x%x\n",
1532 pring->ringno,
1533 irsp->unsli3.sli3Words[7]);
1534 }
1535 }
James Smart92d7f7b2007-06-17 19:56:38 -05001536 }
James Smart9c2face2008-01-11 01:53:18 -05001537 if (irsp->ulpBdeCount != 0 &&
1538 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1539 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1540 int found = 0;
1541
1542 /* search continue save q for same XRI */
1543 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1544 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1545 list_add_tail(&saveq->list, &iocbq->list);
1546 found = 1;
1547 break;
1548 }
1549 }
1550 if (!found)
1551 list_add_tail(&saveq->clist,
1552 &pring->iocb_continue_saveq);
1553 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1554 list_del_init(&iocbq->clist);
1555 saveq = iocbq;
1556 irsp = &(saveq->iocb);
1557 } else
1558 return 0;
1559 }
1560 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1561 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1562 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1563 Rctl = FC_ELS_REQ;
1564 Type = FC_ELS_DATA;
1565 } else {
1566 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1567 Rctl = w5p->hcsw.Rctl;
1568 Type = w5p->hcsw.Type;
1569
1570 /* Firmware Workaround */
1571 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1572 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1573 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1574 Rctl = FC_ELS_REQ;
1575 Type = FC_ELS_DATA;
1576 w5p->hcsw.Rctl = Rctl;
1577 w5p->hcsw.Type = Type;
1578 }
1579 }
James Smart92d7f7b2007-06-17 19:56:38 -05001580
James Smart3772a992009-05-22 14:50:54 -04001581 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05001582 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001583 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001584 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001585 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04001586
James Smart92d7f7b2007-06-17 19:56:38 -05001587 return 1;
dea31012005-04-17 16:05:31 -05001588}
1589
James Smarte59058c2008-08-24 21:49:00 -04001590/**
James Smart3621a712009-04-06 18:47:14 -04001591 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04001592 * @phba: Pointer to HBA context object.
1593 * @pring: Pointer to driver SLI ring object.
1594 * @prspiocb: Pointer to response iocb object.
1595 *
1596 * This function looks up the iocb_lookup table to get the command iocb
1597 * corresponding to the given response iocb using the iotag of the
1598 * response iocb. This function is called with the hbalock held.
1599 * This function returns the command iocb object if it finds the command
1600 * iocb else returns NULL.
1601 **/
dea31012005-04-17 16:05:31 -05001602static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001603lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1604 struct lpfc_sli_ring *pring,
1605 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001606{
dea31012005-04-17 16:05:31 -05001607 struct lpfc_iocbq *cmd_iocb = NULL;
1608 uint16_t iotag;
1609
James Bottomley604a3e32005-10-29 10:28:33 -05001610 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001611
James Bottomley604a3e32005-10-29 10:28:33 -05001612 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1613 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001614 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001615 pring->txcmplq_cnt--;
1616 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001617 }
1618
dea31012005-04-17 16:05:31 -05001619 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001620 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001621 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001622 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001623 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001624 return NULL;
1625}
1626
James Smarte59058c2008-08-24 21:49:00 -04001627/**
James Smart3772a992009-05-22 14:50:54 -04001628 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
1629 * @phba: Pointer to HBA context object.
1630 * @pring: Pointer to driver SLI ring object.
1631 * @iotag: IOCB tag.
1632 *
1633 * This function looks up the iocb_lookup table to get the command iocb
1634 * corresponding to the given iotag. This function is called with the
1635 * hbalock held.
1636 * This function returns the command iocb object if it finds the command
1637 * iocb else returns NULL.
1638 **/
1639static struct lpfc_iocbq *
1640lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
1641 struct lpfc_sli_ring *pring, uint16_t iotag)
1642{
1643 struct lpfc_iocbq *cmd_iocb;
1644
1645 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1646 cmd_iocb = phba->sli.iocbq_lookup[iotag];
1647 list_del_init(&cmd_iocb->list);
1648 pring->txcmplq_cnt--;
1649 return cmd_iocb;
1650 }
1651
1652 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1653 "0372 iotag x%x is out off range: max iotag (x%x)\n",
1654 iotag, phba->sli.last_iotag);
1655 return NULL;
1656}
1657
1658/**
James Smart3621a712009-04-06 18:47:14 -04001659 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04001660 * @phba: Pointer to HBA context object.
1661 * @pring: Pointer to driver SLI ring object.
1662 * @saveq: Pointer to the response iocb to be processed.
1663 *
1664 * This function is called by the ring event handler for non-fcp
1665 * rings when there is a new response iocb in the response ring.
1666 * The caller is not required to hold any locks. This function
1667 * gets the command iocb associated with the response iocb and
1668 * calls the completion handler for the command iocb. If there
1669 * is no completion handler, the function will free the resources
1670 * associated with command iocb. If the response iocb is for
1671 * an already aborted command iocb, the status of the completion
1672 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1673 * This function always returns 1.
1674 **/
dea31012005-04-17 16:05:31 -05001675static int
James Smart2e0fef82007-06-17 19:56:36 -05001676lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001677 struct lpfc_iocbq *saveq)
1678{
James Smart2e0fef82007-06-17 19:56:36 -05001679 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001680 int rc = 1;
1681 unsigned long iflag;
1682
1683 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001684 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001685 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001686 spin_unlock_irqrestore(&phba->hbalock, iflag);
1687
dea31012005-04-17 16:05:31 -05001688 if (cmdiocbp) {
1689 if (cmdiocbp->iocb_cmpl) {
1690 /*
James Smartea2151b2008-09-07 11:52:10 -04001691 * If an ELS command failed send an event to mgmt
1692 * application.
1693 */
1694 if (saveq->iocb.ulpStatus &&
1695 (pring->ringno == LPFC_ELS_RING) &&
1696 (cmdiocbp->iocb.ulpCommand ==
1697 CMD_ELS_REQUEST64_CR))
1698 lpfc_send_els_failure_event(phba,
1699 cmdiocbp, saveq);
1700
1701 /*
dea31012005-04-17 16:05:31 -05001702 * Post all ELS completions to the worker thread.
1703 * All other are passed to the completion callback.
1704 */
1705 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001706 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1707 cmdiocbp->iocb_flag &=
1708 ~LPFC_DRIVER_ABORTED;
1709 saveq->iocb.ulpStatus =
1710 IOSTAT_LOCAL_REJECT;
1711 saveq->iocb.un.ulpWord[4] =
1712 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05001713
1714 /* Firmware could still be in progress
1715 * of DMAing payload, so don't free data
1716 * buffer till after a hbeat.
1717 */
1718 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04001719 }
dea31012005-04-17 16:05:31 -05001720 }
James Smart2e0fef82007-06-17 19:56:36 -05001721 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001722 } else
1723 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001724 } else {
1725 /*
1726 * Unknown initiating command based on the response iotag.
1727 * This could be the case on the ELS ring because of
1728 * lpfc_els_abort().
1729 */
1730 if (pring->ringno != LPFC_ELS_RING) {
1731 /*
1732 * Ring <ringno> handler: unexpected completion IoTag
1733 * <IoTag>
1734 */
James Smarta257bf92009-04-06 18:48:10 -04001735 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001736 "0322 Ring %d handler: "
1737 "unexpected completion IoTag x%x "
1738 "Data: x%x x%x x%x x%x\n",
1739 pring->ringno,
1740 saveq->iocb.ulpIoTag,
1741 saveq->iocb.ulpStatus,
1742 saveq->iocb.un.ulpWord[4],
1743 saveq->iocb.ulpCommand,
1744 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001745 }
1746 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001747
dea31012005-04-17 16:05:31 -05001748 return rc;
1749}
1750
James Smarte59058c2008-08-24 21:49:00 -04001751/**
James Smart3621a712009-04-06 18:47:14 -04001752 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04001753 * @phba: Pointer to HBA context object.
1754 * @pring: Pointer to driver SLI ring object.
1755 *
1756 * This function is called from the iocb ring event handlers when
1757 * put pointer is ahead of the get pointer for a ring. This function signal
1758 * an error attention condition to the worker thread and the worker
1759 * thread will transition the HBA to offline state.
1760 **/
James Smart2e0fef82007-06-17 19:56:36 -05001761static void
1762lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001763{
James Smart34b02dc2008-08-24 21:49:55 -04001764 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001765 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001766 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001767 * rsp ring <portRspMax>
1768 */
1769 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001770 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001771 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001772 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001773 pring->numRiocb);
1774
James Smart2e0fef82007-06-17 19:56:36 -05001775 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001776
1777 /*
1778 * All error attention handlers are posted to
1779 * worker thread
1780 */
1781 phba->work_ha |= HA_ERATT;
1782 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001783
James Smart5e9d9b82008-06-14 22:52:53 -04001784 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001785
1786 return;
1787}
1788
James Smarte59058c2008-08-24 21:49:00 -04001789/**
James Smart3621a712009-04-06 18:47:14 -04001790 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04001791 * @ptr: Pointer to address of HBA context object.
1792 *
1793 * This function is invoked by the Error Attention polling timer when the
1794 * timer times out. It will check the SLI Error Attention register for
1795 * possible attention events. If so, it will post an Error Attention event
1796 * and wake up worker thread to process it. Otherwise, it will set up the
1797 * Error Attention polling timer for the next poll.
1798 **/
1799void lpfc_poll_eratt(unsigned long ptr)
1800{
1801 struct lpfc_hba *phba;
1802 uint32_t eratt = 0;
1803
1804 phba = (struct lpfc_hba *)ptr;
1805
1806 /* Check chip HA register for error event */
1807 eratt = lpfc_sli_check_eratt(phba);
1808
1809 if (eratt)
1810 /* Tell the worker thread there is work to do */
1811 lpfc_worker_wake_up(phba);
1812 else
1813 /* Restart the timer for next eratt poll */
1814 mod_timer(&phba->eratt_poll, jiffies +
1815 HZ * LPFC_ERATT_POLL_INTERVAL);
1816 return;
1817}
1818
1819/**
James Smart3621a712009-04-06 18:47:14 -04001820 * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
James Smarte59058c2008-08-24 21:49:00 -04001821 * @phba: Pointer to HBA context object.
1822 *
1823 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1824 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1825 * is enabled.
1826 *
1827 * The caller does not hold any lock.
1828 * The function processes each response iocb in the response ring until it
1829 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1830 * LE bit set. The function will call the completion handler of the command iocb
1831 * if the response iocb indicates a completion for a command iocb or it is
1832 * an abort completion.
1833 **/
James Smart2e0fef82007-06-17 19:56:36 -05001834void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001835{
James Smart2e0fef82007-06-17 19:56:36 -05001836 struct lpfc_sli *psli = &phba->sli;
1837 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001838 IOCB_t *irsp = NULL;
1839 IOCB_t *entry = NULL;
1840 struct lpfc_iocbq *cmdiocbq = NULL;
1841 struct lpfc_iocbq rspiocbq;
James Smart34b02dc2008-08-24 21:49:55 -04001842 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001843 uint32_t status;
1844 uint32_t portRspPut, portRspMax;
1845 int type;
1846 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001847 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001848 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001849
1850 pring->stats.iocb_event++;
1851
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001852 /*
1853 * The next available response entry should never exceed the maximum
1854 * entries. If it does, treat it as an adapter hardware error.
1855 */
1856 portRspMax = pring->numRiocb;
1857 portRspPut = le32_to_cpu(pgp->rspPutInx);
1858 if (unlikely(portRspPut >= portRspMax)) {
1859 lpfc_sli_rsp_pointers_error(phba, pring);
1860 return;
1861 }
1862
1863 rmb();
1864 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001865 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001866 if (++pring->rspidx >= portRspMax)
1867 pring->rspidx = 0;
1868
1869 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1870 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001871 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001872 irsp = &rspiocbq.iocb;
1873 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1874 pring->stats.iocb_rsp++;
1875 rsp_cmpl++;
1876
1877 if (unlikely(irsp->ulpStatus)) {
1878 /* Rsp ring <ringno> error: IOCB */
1879 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001880 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001881 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001882 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001883 irsp->un.ulpWord[0],
1884 irsp->un.ulpWord[1],
1885 irsp->un.ulpWord[2],
1886 irsp->un.ulpWord[3],
1887 irsp->un.ulpWord[4],
1888 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001889 *(uint32_t *)&irsp->un1,
1890 *((uint32_t *)&irsp->un1 + 1));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001891 }
1892
1893 switch (type) {
1894 case LPFC_ABORT_IOCB:
1895 case LPFC_SOL_IOCB:
1896 /*
1897 * Idle exchange closed via ABTS from port. No iocb
1898 * resources need to be recovered.
1899 */
1900 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001901 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001902 "0314 IOCB cmd 0x%x "
1903 "processed. Skipping "
1904 "completion",
James Smartdca94792006-08-01 07:34:08 -04001905 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001906 break;
1907 }
1908
James Smart2e0fef82007-06-17 19:56:36 -05001909 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001910 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1911 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001912 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001913 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1914 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1915 &rspiocbq);
1916 }
1917 break;
1918 default:
1919 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1920 char adaptermsg[LPFC_MAX_ADPTMSG];
1921 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1922 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1923 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001924 dev_warn(&((phba->pcidev)->dev),
1925 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001926 phba->brd_no, adaptermsg);
1927 } else {
1928 /* Unknown IOCB command */
1929 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001930 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001931 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001932 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001933 irsp->ulpStatus,
1934 irsp->ulpIoTag,
1935 irsp->ulpContext);
1936 }
1937 break;
1938 }
1939
1940 /*
1941 * The response IOCB has been processed. Update the ring
1942 * pointer in SLIM. If the port response put pointer has not
1943 * been updated, sync the pgp->rspPutInx and fetch the new port
1944 * response put pointer.
1945 */
James Smarted957682007-06-17 19:56:37 -05001946 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001947
1948 if (pring->rspidx == portRspPut)
1949 portRspPut = le32_to_cpu(pgp->rspPutInx);
1950 }
1951
1952 ha_copy = readl(phba->HAregaddr);
1953 ha_copy >>= (LPFC_FCP_RING * 4);
1954
1955 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001956 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001957 pring->stats.iocb_rsp_full++;
1958 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1959 writel(status, phba->CAregaddr);
1960 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001961 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001962 }
1963 if ((ha_copy & HA_R0CE_RSP) &&
1964 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001965 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001966 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1967 pring->stats.iocb_cmd_empty++;
1968
1969 /* Force update of the local copy of cmdGetInx */
1970 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1971 lpfc_sli_resume_iocb(phba, pring);
1972
1973 if ((pring->lpfc_sli_cmd_available))
1974 (pring->lpfc_sli_cmd_available) (phba, pring);
1975
James Smart2e0fef82007-06-17 19:56:36 -05001976 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001977 }
1978
1979 return;
1980}
1981
James Smarte59058c2008-08-24 21:49:00 -04001982/**
James Smart3621a712009-04-06 18:47:14 -04001983 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04001984 * @phba: Pointer to HBA context object.
1985 * @pring: Pointer to driver SLI ring object.
1986 * @mask: Host attention register mask for this ring.
1987 *
1988 * This function is called from the interrupt context when there is a ring
1989 * event for the fcp ring. The caller does not hold any lock.
1990 * The function processes each response iocb in the response ring until it
1991 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1992 * LE bit set. The function will call the completion handler of the command iocb
1993 * if the response iocb indicates a completion for a command iocb or it is
1994 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
1995 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05001996 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04001997 * to check it explicitly. This function always returns 1.
1998 **/
dea31012005-04-17 16:05:31 -05001999static int
James Smart2e0fef82007-06-17 19:56:36 -05002000lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2001 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002002{
James Smart34b02dc2008-08-24 21:49:55 -04002003 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002004 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002005 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002006 struct lpfc_iocbq *cmdiocbq = NULL;
2007 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002008 uint32_t status;
2009 uint32_t portRspPut, portRspMax;
2010 int rc = 1;
2011 lpfc_iocb_type type;
2012 unsigned long iflag;
2013 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002014
James Smart2e0fef82007-06-17 19:56:36 -05002015 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002016 pring->stats.iocb_event++;
2017
dea31012005-04-17 16:05:31 -05002018 /*
2019 * The next available response entry should never exceed the maximum
2020 * entries. If it does, treat it as an adapter hardware error.
2021 */
2022 portRspMax = pring->numRiocb;
2023 portRspPut = le32_to_cpu(pgp->rspPutInx);
2024 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002025 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002026 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002027 return 1;
2028 }
2029
2030 rmb();
2031 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002032 /*
2033 * Fetch an entry off the ring and copy it into a local data
2034 * structure. The copy involves a byte-swap since the
2035 * network byte order and pci byte orders are different.
2036 */
James Smarted957682007-06-17 19:56:37 -05002037 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002038 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002039
2040 if (++pring->rspidx >= portRspMax)
2041 pring->rspidx = 0;
2042
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002043 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2044 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002045 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002046 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002047 irsp = &rspiocbq.iocb;
2048
dea31012005-04-17 16:05:31 -05002049 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2050 pring->stats.iocb_rsp++;
2051 rsp_cmpl++;
2052
2053 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002054 /*
2055 * If resource errors reported from HBA, reduce
2056 * queuedepths of the SCSI device.
2057 */
2058 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2059 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2060 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002061 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002062 spin_lock_irqsave(&phba->hbalock, iflag);
2063 }
2064
dea31012005-04-17 16:05:31 -05002065 /* Rsp ring <ringno> error: IOCB */
2066 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002067 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002068 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002069 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002070 irsp->un.ulpWord[0],
2071 irsp->un.ulpWord[1],
2072 irsp->un.ulpWord[2],
2073 irsp->un.ulpWord[3],
2074 irsp->un.ulpWord[4],
2075 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002076 *(uint32_t *)&irsp->un1,
2077 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002078 }
2079
2080 switch (type) {
2081 case LPFC_ABORT_IOCB:
2082 case LPFC_SOL_IOCB:
2083 /*
2084 * Idle exchange closed via ABTS from port. No iocb
2085 * resources need to be recovered.
2086 */
2087 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002088 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002089 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002090 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002091 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002092 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002093 break;
2094 }
2095
James Bottomley604a3e32005-10-29 10:28:33 -05002096 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2097 &rspiocbq);
dea31012005-04-17 16:05:31 -05002098 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002099 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2100 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2101 &rspiocbq);
2102 } else {
James Smart2e0fef82007-06-17 19:56:36 -05002103 spin_unlock_irqrestore(&phba->hbalock,
2104 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002105 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2106 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002107 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002108 iflag);
2109 }
dea31012005-04-17 16:05:31 -05002110 }
2111 break;
James Smarta4bc3372006-12-02 13:34:16 -05002112 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002113 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002114 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002115 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002116 break;
dea31012005-04-17 16:05:31 -05002117 default:
2118 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2119 char adaptermsg[LPFC_MAX_ADPTMSG];
2120 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2121 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2122 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002123 dev_warn(&((phba->pcidev)->dev),
2124 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002125 phba->brd_no, adaptermsg);
2126 } else {
2127 /* Unknown IOCB command */
2128 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002129 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002130 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002131 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002132 irsp->ulpStatus,
2133 irsp->ulpIoTag,
2134 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002135 }
2136 break;
2137 }
2138
2139 /*
2140 * The response IOCB has been processed. Update the ring
2141 * pointer in SLIM. If the port response put pointer has not
2142 * been updated, sync the pgp->rspPutInx and fetch the new port
2143 * response put pointer.
2144 */
James Smarted957682007-06-17 19:56:37 -05002145 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002146
2147 if (pring->rspidx == portRspPut)
2148 portRspPut = le32_to_cpu(pgp->rspPutInx);
2149 }
2150
2151 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2152 pring->stats.iocb_rsp_full++;
2153 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2154 writel(status, phba->CAregaddr);
2155 readl(phba->CAregaddr);
2156 }
2157 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2158 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2159 pring->stats.iocb_cmd_empty++;
2160
2161 /* Force update of the local copy of cmdGetInx */
2162 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2163 lpfc_sli_resume_iocb(phba, pring);
2164
2165 if ((pring->lpfc_sli_cmd_available))
2166 (pring->lpfc_sli_cmd_available) (phba, pring);
2167
2168 }
2169
James Smart2e0fef82007-06-17 19:56:36 -05002170 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002171 return rc;
2172}
2173
James Smarte59058c2008-08-24 21:49:00 -04002174/**
James Smart3772a992009-05-22 14:50:54 -04002175 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2176 * @phba: Pointer to HBA context object.
2177 * @pring: Pointer to driver SLI ring object.
2178 * @rspiocbp: Pointer to driver response IOCB object.
2179 *
2180 * This function is called from the worker thread when there is a slow-path
2181 * response IOCB to process. This function chains all the response iocbs until
2182 * seeing the iocb with the LE bit set. The function will call
2183 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2184 * completion of a command iocb. The function will call the
2185 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2186 * The function frees the resources or calls the completion handler if this
2187 * iocb is an abort completion. The function returns NULL when the response
2188 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2189 * this function shall chain the iocb on to the iocb_continueq and return the
2190 * response iocb passed in.
2191 **/
2192static struct lpfc_iocbq *
2193lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2194 struct lpfc_iocbq *rspiocbp)
2195{
2196 struct lpfc_iocbq *saveq;
2197 struct lpfc_iocbq *cmdiocbp;
2198 struct lpfc_iocbq *next_iocb;
2199 IOCB_t *irsp = NULL;
2200 uint32_t free_saveq;
2201 uint8_t iocb_cmd_type;
2202 lpfc_iocb_type type;
2203 unsigned long iflag;
2204 int rc;
2205
2206 spin_lock_irqsave(&phba->hbalock, iflag);
2207 /* First add the response iocb to the countinueq list */
2208 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2209 pring->iocb_continueq_cnt++;
2210
2211 /* Now, determine whetehr the list is completed for processing */
2212 irsp = &rspiocbp->iocb;
2213 if (irsp->ulpLe) {
2214 /*
2215 * By default, the driver expects to free all resources
2216 * associated with this iocb completion.
2217 */
2218 free_saveq = 1;
2219 saveq = list_get_first(&pring->iocb_continueq,
2220 struct lpfc_iocbq, list);
2221 irsp = &(saveq->iocb);
2222 list_del_init(&pring->iocb_continueq);
2223 pring->iocb_continueq_cnt = 0;
2224
2225 pring->stats.iocb_rsp++;
2226
2227 /*
2228 * If resource errors reported from HBA, reduce
2229 * queuedepths of the SCSI device.
2230 */
2231 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2232 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2233 spin_unlock_irqrestore(&phba->hbalock, iflag);
2234 phba->lpfc_rampdown_queue_depth(phba);
2235 spin_lock_irqsave(&phba->hbalock, iflag);
2236 }
2237
2238 if (irsp->ulpStatus) {
2239 /* Rsp ring <ringno> error: IOCB */
2240 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2241 "0328 Rsp Ring %d error: "
2242 "IOCB Data: "
2243 "x%x x%x x%x x%x "
2244 "x%x x%x x%x x%x "
2245 "x%x x%x x%x x%x "
2246 "x%x x%x x%x x%x\n",
2247 pring->ringno,
2248 irsp->un.ulpWord[0],
2249 irsp->un.ulpWord[1],
2250 irsp->un.ulpWord[2],
2251 irsp->un.ulpWord[3],
2252 irsp->un.ulpWord[4],
2253 irsp->un.ulpWord[5],
2254 *(((uint32_t *) irsp) + 6),
2255 *(((uint32_t *) irsp) + 7),
2256 *(((uint32_t *) irsp) + 8),
2257 *(((uint32_t *) irsp) + 9),
2258 *(((uint32_t *) irsp) + 10),
2259 *(((uint32_t *) irsp) + 11),
2260 *(((uint32_t *) irsp) + 12),
2261 *(((uint32_t *) irsp) + 13),
2262 *(((uint32_t *) irsp) + 14),
2263 *(((uint32_t *) irsp) + 15));
2264 }
2265
2266 /*
2267 * Fetch the IOCB command type and call the correct completion
2268 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2269 * get freed back to the lpfc_iocb_list by the discovery
2270 * kernel thread.
2271 */
2272 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2273 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2274 switch (type) {
2275 case LPFC_SOL_IOCB:
2276 spin_unlock_irqrestore(&phba->hbalock, iflag);
2277 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2278 spin_lock_irqsave(&phba->hbalock, iflag);
2279 break;
2280
2281 case LPFC_UNSOL_IOCB:
2282 spin_unlock_irqrestore(&phba->hbalock, iflag);
2283 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2284 spin_lock_irqsave(&phba->hbalock, iflag);
2285 if (!rc)
2286 free_saveq = 0;
2287 break;
2288
2289 case LPFC_ABORT_IOCB:
2290 cmdiocbp = NULL;
2291 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2292 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2293 saveq);
2294 if (cmdiocbp) {
2295 /* Call the specified completion routine */
2296 if (cmdiocbp->iocb_cmpl) {
2297 spin_unlock_irqrestore(&phba->hbalock,
2298 iflag);
2299 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2300 saveq);
2301 spin_lock_irqsave(&phba->hbalock,
2302 iflag);
2303 } else
2304 __lpfc_sli_release_iocbq(phba,
2305 cmdiocbp);
2306 }
2307 break;
2308
2309 case LPFC_UNKNOWN_IOCB:
2310 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2311 char adaptermsg[LPFC_MAX_ADPTMSG];
2312 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2313 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2314 MAX_MSG_DATA);
2315 dev_warn(&((phba->pcidev)->dev),
2316 "lpfc%d: %s\n",
2317 phba->brd_no, adaptermsg);
2318 } else {
2319 /* Unknown IOCB command */
2320 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2321 "0335 Unknown IOCB "
2322 "command Data: x%x "
2323 "x%x x%x x%x\n",
2324 irsp->ulpCommand,
2325 irsp->ulpStatus,
2326 irsp->ulpIoTag,
2327 irsp->ulpContext);
2328 }
2329 break;
2330 }
2331
2332 if (free_saveq) {
2333 list_for_each_entry_safe(rspiocbp, next_iocb,
2334 &saveq->list, list) {
2335 list_del(&rspiocbp->list);
2336 __lpfc_sli_release_iocbq(phba, rspiocbp);
2337 }
2338 __lpfc_sli_release_iocbq(phba, saveq);
2339 }
2340 rspiocbp = NULL;
2341 }
2342 spin_unlock_irqrestore(&phba->hbalock, iflag);
2343 return rspiocbp;
2344}
2345
2346/**
2347 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002348 * @phba: Pointer to HBA context object.
2349 * @pring: Pointer to driver SLI ring object.
2350 * @mask: Host attention register mask for this ring.
2351 *
James Smart3772a992009-05-22 14:50:54 -04002352 * This routine wraps the actual slow_ring event process routine from the
2353 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002354 **/
James Smart3772a992009-05-22 14:50:54 -04002355void
James Smart2e0fef82007-06-17 19:56:36 -05002356lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2357 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002358{
James Smart3772a992009-05-22 14:50:54 -04002359 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2360}
2361
2362/**
2363 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2364 * @phba: Pointer to HBA context object.
2365 * @pring: Pointer to driver SLI ring object.
2366 * @mask: Host attention register mask for this ring.
2367 *
2368 * This function is called from the worker thread when there is a ring event
2369 * for non-fcp rings. The caller does not hold any lock. The function will
2370 * remove each response iocb in the response ring and calls the handle
2371 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2372 **/
2373static void
2374lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2375 struct lpfc_sli_ring *pring, uint32_t mask)
2376{
James Smart34b02dc2008-08-24 21:49:55 -04002377 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002378 IOCB_t *entry;
2379 IOCB_t *irsp = NULL;
2380 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002381 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002382 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002383 uint32_t status;
dea31012005-04-17 16:05:31 -05002384
James Smart34b02dc2008-08-24 21:49:55 -04002385 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002386 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002387 pring->stats.iocb_event++;
2388
dea31012005-04-17 16:05:31 -05002389 /*
2390 * The next available response entry should never exceed the maximum
2391 * entries. If it does, treat it as an adapter hardware error.
2392 */
2393 portRspMax = pring->numRiocb;
2394 portRspPut = le32_to_cpu(pgp->rspPutInx);
2395 if (portRspPut >= portRspMax) {
2396 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002397 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002398 * rsp ring <portRspMax>
2399 */
James Smarted957682007-06-17 19:56:37 -05002400 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002401 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002402 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002403 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002404
James Smart2e0fef82007-06-17 19:56:36 -05002405 phba->link_state = LPFC_HBA_ERROR;
2406 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002407
2408 phba->work_hs = HS_FFER3;
2409 lpfc_handle_eratt(phba);
2410
James Smart3772a992009-05-22 14:50:54 -04002411 return;
dea31012005-04-17 16:05:31 -05002412 }
2413
2414 rmb();
dea31012005-04-17 16:05:31 -05002415 while (pring->rspidx != portRspPut) {
2416 /*
2417 * Build a completion list and call the appropriate handler.
2418 * The process is to get the next available response iocb, get
2419 * a free iocb from the list, copy the response data into the
2420 * free iocb, insert to the continuation list, and update the
2421 * next response index to slim. This process makes response
2422 * iocb's in the ring available to DMA as fast as possible but
2423 * pays a penalty for a copy operation. Since the iocb is
2424 * only 32 bytes, this penalty is considered small relative to
2425 * the PCI reads for register values and a slim write. When
2426 * the ulpLe field is set, the entire Command has been
2427 * received.
2428 */
James Smarted957682007-06-17 19:56:37 -05002429 entry = lpfc_resp_iocb(phba, pring);
2430
James Smart858c9f62007-06-17 19:56:39 -05002431 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002432 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002433 if (rspiocbp == NULL) {
2434 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002435 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002436 break;
2437 }
2438
James Smarted957682007-06-17 19:56:37 -05002439 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2440 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002441 irsp = &rspiocbp->iocb;
2442
2443 if (++pring->rspidx >= portRspMax)
2444 pring->rspidx = 0;
2445
James Smarta58cbd52007-08-02 11:09:43 -04002446 if (pring->ringno == LPFC_ELS_RING) {
2447 lpfc_debugfs_slow_ring_trc(phba,
2448 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2449 *(((uint32_t *) irsp) + 4),
2450 *(((uint32_t *) irsp) + 6),
2451 *(((uint32_t *) irsp) + 7));
2452 }
2453
James Smarted957682007-06-17 19:56:37 -05002454 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002455
James Smart3772a992009-05-22 14:50:54 -04002456 spin_unlock_irqrestore(&phba->hbalock, iflag);
2457 /* Handle the response IOCB */
2458 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2459 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002460
2461 /*
2462 * If the port response put pointer has not been updated, sync
2463 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2464 * response put pointer.
2465 */
2466 if (pring->rspidx == portRspPut) {
2467 portRspPut = le32_to_cpu(pgp->rspPutInx);
2468 }
2469 } /* while (pring->rspidx != portRspPut) */
2470
James Smart92d7f7b2007-06-17 19:56:38 -05002471 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002472 /* At least one response entry has been freed */
2473 pring->stats.iocb_rsp_full++;
2474 /* SET RxRE_RSP in Chip Att register */
2475 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2476 writel(status, phba->CAregaddr);
2477 readl(phba->CAregaddr); /* flush */
2478 }
2479 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2480 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2481 pring->stats.iocb_cmd_empty++;
2482
2483 /* Force update of the local copy of cmdGetInx */
2484 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2485 lpfc_sli_resume_iocb(phba, pring);
2486
2487 if ((pring->lpfc_sli_cmd_available))
2488 (pring->lpfc_sli_cmd_available) (phba, pring);
2489
2490 }
2491
James Smart2e0fef82007-06-17 19:56:36 -05002492 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002493 return;
dea31012005-04-17 16:05:31 -05002494}
2495
James Smarte59058c2008-08-24 21:49:00 -04002496/**
James Smart3621a712009-04-06 18:47:14 -04002497 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04002498 * @phba: Pointer to HBA context object.
2499 * @pring: Pointer to driver SLI ring object.
2500 *
2501 * This function aborts all iocbs in the given ring and frees all the iocb
2502 * objects in txq. This function issues an abort iocb for all the iocb commands
2503 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2504 * the return of this function. The caller is not required to hold any locks.
2505 **/
James Smart2e0fef82007-06-17 19:56:36 -05002506void
dea31012005-04-17 16:05:31 -05002507lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2508{
James Smart2534ba72007-04-25 09:52:20 -04002509 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002510 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05002511
James Smart92d7f7b2007-06-17 19:56:38 -05002512 if (pring->ringno == LPFC_ELS_RING) {
2513 lpfc_fabric_abort_hba(phba);
2514 }
2515
dea31012005-04-17 16:05:31 -05002516 /* Error everything on txq and txcmplq
2517 * First do the txq.
2518 */
James Smart2e0fef82007-06-17 19:56:36 -05002519 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002520 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002521 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002522
2523 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002524 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2525 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2526
James Smart2e0fef82007-06-17 19:56:36 -05002527 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002528
James Smarta257bf92009-04-06 18:48:10 -04002529 /* Cancel all the IOCBs from the completions list */
2530 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2531 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05002532}
2533
James Smarte59058c2008-08-24 21:49:00 -04002534/**
James Smart3621a712009-04-06 18:47:14 -04002535 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04002536 * @phba: Pointer to HBA context object.
2537 *
2538 * This function flushes all iocbs in the fcp ring and frees all the iocb
2539 * objects in txq and txcmplq. This function will not issue abort iocbs
2540 * for all the iocb commands in txcmplq, they will just be returned with
2541 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2542 * slot has been permanently disabled.
2543 **/
2544void
2545lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2546{
2547 LIST_HEAD(txq);
2548 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04002549 struct lpfc_sli *psli = &phba->sli;
2550 struct lpfc_sli_ring *pring;
2551
2552 /* Currently, only one fcp ring */
2553 pring = &psli->ring[psli->fcp_ring];
2554
2555 spin_lock_irq(&phba->hbalock);
2556 /* Retrieve everything on txq */
2557 list_splice_init(&pring->txq, &txq);
2558 pring->txq_cnt = 0;
2559
2560 /* Retrieve everything on the txcmplq */
2561 list_splice_init(&pring->txcmplq, &txcmplq);
2562 pring->txcmplq_cnt = 0;
2563 spin_unlock_irq(&phba->hbalock);
2564
2565 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04002566 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
2567 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04002568
2569 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04002570 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
2571 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04002572}
2573
2574/**
James Smart3772a992009-05-22 14:50:54 -04002575 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04002576 * @phba: Pointer to HBA context object.
2577 * @mask: Bit mask to be checked.
2578 *
2579 * This function reads the host status register and compares
2580 * with the provided bit mask to check if HBA completed
2581 * the restart. This function will wait in a loop for the
2582 * HBA to complete restart. If the HBA does not restart within
2583 * 15 iterations, the function will reset the HBA again. The
2584 * function returns 1 when HBA fail to restart otherwise returns
2585 * zero.
2586 **/
James Smart3772a992009-05-22 14:50:54 -04002587static int
2588lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05002589{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002590 uint32_t status;
2591 int i = 0;
2592 int retval = 0;
dea31012005-04-17 16:05:31 -05002593
Jamie Wellnitz41415862006-02-28 19:25:27 -05002594 /* Read the HBA Host Status Register */
2595 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002596
Jamie Wellnitz41415862006-02-28 19:25:27 -05002597 /*
2598 * Check status register every 100ms for 5 retries, then every
2599 * 500ms for 5, then every 2.5 sec for 5, then reset board and
2600 * every 2.5 sec for 4.
2601 * Break our of the loop if errors occurred during init.
2602 */
2603 while (((status & mask) != mask) &&
2604 !(status & HS_FFERM) &&
2605 i++ < 20) {
dea31012005-04-17 16:05:31 -05002606
Jamie Wellnitz41415862006-02-28 19:25:27 -05002607 if (i <= 5)
2608 msleep(10);
2609 else if (i <= 10)
2610 msleep(500);
2611 else
2612 msleep(2500);
dea31012005-04-17 16:05:31 -05002613
Jamie Wellnitz41415862006-02-28 19:25:27 -05002614 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002615 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002616 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002617 lpfc_sli_brdrestart(phba);
2618 }
2619 /* Read the HBA Host Status Register */
2620 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002621 }
dea31012005-04-17 16:05:31 -05002622
Jamie Wellnitz41415862006-02-28 19:25:27 -05002623 /* Check to see if any errors occurred during init */
2624 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05002625 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002626 retval = 1;
2627 }
dea31012005-04-17 16:05:31 -05002628
Jamie Wellnitz41415862006-02-28 19:25:27 -05002629 return retval;
dea31012005-04-17 16:05:31 -05002630}
2631
James Smart92908312006-03-07 15:04:13 -05002632#define BARRIER_TEST_PATTERN (0xdeadbeef)
2633
James Smarte59058c2008-08-24 21:49:00 -04002634/**
James Smart3621a712009-04-06 18:47:14 -04002635 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04002636 * @phba: Pointer to HBA context object.
2637 *
2638 * This function is called before resetting an HBA. This
2639 * function requests HBA to quiesce DMAs before a reset.
2640 **/
James Smart2e0fef82007-06-17 19:56:36 -05002641void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05002642{
James Smart65a29c12006-07-06 15:50:50 -04002643 uint32_t __iomem *resp_buf;
2644 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05002645 volatile uint32_t mbox;
2646 uint32_t hc_copy;
2647 int i;
2648 uint8_t hdrtype;
2649
2650 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2651 if (hdrtype != 0x80 ||
2652 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2653 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2654 return;
2655
2656 /*
2657 * Tell the other part of the chip to suspend temporarily all
2658 * its DMA activity.
2659 */
James Smart65a29c12006-07-06 15:50:50 -04002660 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002661
2662 /* Disable the error attention */
2663 hc_copy = readl(phba->HCregaddr);
2664 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2665 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002666 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002667
2668 if (readl(phba->HAregaddr) & HA_ERATT) {
2669 /* Clear Chip error bit */
2670 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002671 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002672 }
2673
2674 mbox = 0;
2675 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2676 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2677
2678 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04002679 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002680 writel(mbox, mbox_buf);
2681
2682 for (i = 0;
2683 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2684 mdelay(1);
2685
2686 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2687 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05002688 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05002689 goto restore_hc;
2690 else
2691 goto clear_errat;
2692 }
2693
2694 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2695 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
2696 mdelay(1);
2697
2698clear_errat:
2699
2700 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2701 mdelay(1);
2702
2703 if (readl(phba->HAregaddr) & HA_ERATT) {
2704 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002705 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002706 }
2707
2708restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05002709 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002710 writel(hc_copy, phba->HCregaddr);
2711 readl(phba->HCregaddr); /* flush */
2712}
2713
James Smarte59058c2008-08-24 21:49:00 -04002714/**
James Smart3621a712009-04-06 18:47:14 -04002715 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04002716 * @phba: Pointer to HBA context object.
2717 *
2718 * This function issues a kill_board mailbox command and waits for
2719 * the error attention interrupt. This function is called for stopping
2720 * the firmware processing. The caller is not required to hold any
2721 * locks. This function calls lpfc_hba_down_post function to free
2722 * any pending commands after the kill. The function will return 1 when it
2723 * fails to kill the board else will return 0.
2724 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002725int
James Smart2e0fef82007-06-17 19:56:36 -05002726lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002727{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002728 struct lpfc_sli *psli;
2729 LPFC_MBOXQ_t *pmb;
2730 uint32_t status;
2731 uint32_t ha_copy;
2732 int retval;
2733 int i = 0;
2734
2735 psli = &phba->sli;
2736
2737 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05002738 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002739 "0329 Kill HBA Data: x%x x%x\n",
2740 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002741
James Smart98c9ea52007-10-27 13:37:33 -04002742 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2743 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002744 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002745
2746 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05002747 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002748 status = readl(phba->HCregaddr);
2749 status &= ~HC_ERINT_ENA;
2750 writel(status, phba->HCregaddr);
2751 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002752 phba->link_flag |= LS_IGNORE_ERATT;
2753 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002754
2755 lpfc_kill_board(phba, pmb);
2756 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2757 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2758
2759 if (retval != MBX_SUCCESS) {
2760 if (retval != MBX_BUSY)
2761 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002762 spin_lock_irq(&phba->hbalock);
2763 phba->link_flag &= ~LS_IGNORE_ERATT;
2764 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002765 return 1;
2766 }
2767
James Smart92908312006-03-07 15:04:13 -05002768 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2769
Jamie Wellnitz41415862006-02-28 19:25:27 -05002770 mempool_free(pmb, phba->mbox_mem_pool);
2771
2772 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2773 * attention every 100ms for 3 seconds. If we don't get ERATT after
2774 * 3 seconds we still set HBA_ERROR state because the status of the
2775 * board is now undefined.
2776 */
2777 ha_copy = readl(phba->HAregaddr);
2778
2779 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2780 mdelay(100);
2781 ha_copy = readl(phba->HAregaddr);
2782 }
2783
2784 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05002785 if (ha_copy & HA_ERATT) {
2786 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002787 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002788 }
James Smart2e0fef82007-06-17 19:56:36 -05002789 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002790 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002791 phba->link_flag &= ~LS_IGNORE_ERATT;
2792 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002793
2794 psli->mbox_active = NULL;
2795 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002796 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002797
James Smart2e0fef82007-06-17 19:56:36 -05002798 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002799}
2800
James Smarte59058c2008-08-24 21:49:00 -04002801/**
James Smart3772a992009-05-22 14:50:54 -04002802 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04002803 * @phba: Pointer to HBA context object.
2804 *
2805 * This function resets the HBA by writing HC_INITFF to the control
2806 * register. After the HBA resets, this function resets all the iocb ring
2807 * indices. This function disables PCI layer parity checking during
2808 * the reset.
2809 * This function returns 0 always.
2810 * The caller is not required to hold any locks.
2811 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002812int
James Smart2e0fef82007-06-17 19:56:36 -05002813lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002814{
2815 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002816 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002817 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05002818 int i;
dea31012005-04-17 16:05:31 -05002819
Jamie Wellnitz41415862006-02-28 19:25:27 -05002820 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002821
Jamie Wellnitz41415862006-02-28 19:25:27 -05002822 /* Reset HBA */
2823 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002824 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002825 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05002826
2827 /* perform board reset */
2828 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002829 phba->pport->fc_myDID = 0;
2830 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002831
Jamie Wellnitz41415862006-02-28 19:25:27 -05002832 /* Turn off parity checking and serr during the physical reset */
2833 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2834 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2835 (cfg_value &
2836 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2837
James Smart3772a992009-05-22 14:50:54 -04002838 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
2839
Jamie Wellnitz41415862006-02-28 19:25:27 -05002840 /* Now toggle INITFF bit in the Host Control Register */
2841 writel(HC_INITFF, phba->HCregaddr);
2842 mdelay(1);
2843 readl(phba->HCregaddr); /* flush */
2844 writel(0, phba->HCregaddr);
2845 readl(phba->HCregaddr); /* flush */
2846
2847 /* Restore PCI cmd register */
2848 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002849
2850 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002851 for (i = 0; i < psli->num_rings; i++) {
2852 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002853 pring->flag = 0;
2854 pring->rspidx = 0;
2855 pring->next_cmdidx = 0;
2856 pring->local_getidx = 0;
2857 pring->cmdidx = 0;
2858 pring->missbufcnt = 0;
2859 }
dea31012005-04-17 16:05:31 -05002860
James Smart2e0fef82007-06-17 19:56:36 -05002861 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002862 return 0;
2863}
2864
James Smarte59058c2008-08-24 21:49:00 -04002865/**
James Smart3621a712009-04-06 18:47:14 -04002866 * lpfc_sli_brdrestart - Restart the HBA
James Smarte59058c2008-08-24 21:49:00 -04002867 * @phba: Pointer to HBA context object.
2868 *
2869 * This function is called in the SLI initialization code path to
2870 * restart the HBA. The caller is not required to hold any lock.
2871 * This function writes MBX_RESTART mailbox command to the SLIM and
2872 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
2873 * function to free any pending commands. The function enables
2874 * POST only during the first initialization. The function returns zero.
2875 * The function does not guarantee completion of MBX_RESTART mailbox
2876 * command before the return of this function.
2877 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002878int
James Smart2e0fef82007-06-17 19:56:36 -05002879lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002880{
2881 MAILBOX_t *mb;
2882 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002883 volatile uint32_t word0;
2884 void __iomem *to_slim;
2885
James Smart2e0fef82007-06-17 19:56:36 -05002886 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002887
2888 psli = &phba->sli;
2889
2890 /* Restart HBA */
2891 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002892 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002893 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002894
2895 word0 = 0;
2896 mb = (MAILBOX_t *) &word0;
2897 mb->mbxCommand = MBX_RESTART;
2898 mb->mbxHc = 1;
2899
James Smart92908312006-03-07 15:04:13 -05002900 lpfc_reset_barrier(phba);
2901
Jamie Wellnitz41415862006-02-28 19:25:27 -05002902 to_slim = phba->MBslimaddr;
2903 writel(*(uint32_t *) mb, to_slim);
2904 readl(to_slim); /* flush */
2905
2906 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05002907 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002908 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05002909 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05002910 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04002911 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002912 writel(*(uint32_t *) mb, to_slim);
2913 readl(to_slim); /* flush */
2914
2915 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002916 phba->pport->stopped = 0;
2917 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002918
James Smart2e0fef82007-06-17 19:56:36 -05002919 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002920
James Smart64ba8812006-08-02 15:24:34 -04002921 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2922 psli->stats_start = get_seconds();
2923
James Smarteaf15d52008-12-04 22:39:29 -05002924 /* Give the INITFF and Post time to settle. */
2925 mdelay(100);
dea31012005-04-17 16:05:31 -05002926
Jamie Wellnitz41415862006-02-28 19:25:27 -05002927 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002928
2929 return 0;
2930}
2931
James Smarte59058c2008-08-24 21:49:00 -04002932/**
James Smart3621a712009-04-06 18:47:14 -04002933 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04002934 * @phba: Pointer to HBA context object.
2935 *
2936 * This function is called after a HBA restart to wait for successful
2937 * restart of the HBA. Successful restart of the HBA is indicated by
2938 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
2939 * iteration, the function will restart the HBA again. The function returns
2940 * zero if HBA successfully restarted else returns negative error code.
2941 **/
dea31012005-04-17 16:05:31 -05002942static int
2943lpfc_sli_chipset_init(struct lpfc_hba *phba)
2944{
2945 uint32_t status, i = 0;
2946
2947 /* Read the HBA Host Status Register */
2948 status = readl(phba->HSregaddr);
2949
2950 /* Check status register to see what current state is */
2951 i = 0;
2952 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2953
2954 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2955 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2956 * 4.
2957 */
2958 if (i++ >= 20) {
2959 /* Adapter failed to init, timeout, status reg
2960 <status> */
James Smarted957682007-06-17 19:56:37 -05002961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002962 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002963 "timeout, status reg x%x, "
2964 "FW Data: A8 x%x AC x%x\n", status,
2965 readl(phba->MBslimaddr + 0xa8),
2966 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002967 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002968 return -ETIMEDOUT;
2969 }
2970
2971 /* Check to see if any errors occurred during init */
2972 if (status & HS_FFERM) {
2973 /* ERROR: During chipset initialization */
2974 /* Adapter failed to init, chipset, status reg
2975 <status> */
James Smarted957682007-06-17 19:56:37 -05002976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002977 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002978 "chipset, status reg x%x, "
2979 "FW Data: A8 x%x AC x%x\n", status,
2980 readl(phba->MBslimaddr + 0xa8),
2981 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002982 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002983 return -EIO;
2984 }
2985
2986 if (i <= 5) {
2987 msleep(10);
2988 } else if (i <= 10) {
2989 msleep(500);
2990 } else {
2991 msleep(2500);
2992 }
2993
2994 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002995 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002996 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002997 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002998 }
2999 /* Read the HBA Host Status Register */
3000 status = readl(phba->HSregaddr);
3001 }
3002
3003 /* Check to see if any errors occurred during init */
3004 if (status & HS_FFERM) {
3005 /* ERROR: During chipset initialization */
3006 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003008 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003009 "status reg x%x, "
3010 "FW Data: A8 x%x AC x%x\n", status,
3011 readl(phba->MBslimaddr + 0xa8),
3012 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003013 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003014 return -EIO;
3015 }
3016
3017 /* Clear all interrupt enable conditions */
3018 writel(0, phba->HCregaddr);
3019 readl(phba->HCregaddr); /* flush */
3020
3021 /* setup host attn register */
3022 writel(0xffffffff, phba->HAregaddr);
3023 readl(phba->HAregaddr); /* flush */
3024 return 0;
3025}
3026
James Smarte59058c2008-08-24 21:49:00 -04003027/**
James Smart3621a712009-04-06 18:47:14 -04003028 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003029 *
3030 * This function calculates and returns the number of HBQs required to be
3031 * configured.
3032 **/
James Smart78b2d852007-08-02 11:10:21 -04003033int
James Smarted957682007-06-17 19:56:37 -05003034lpfc_sli_hbq_count(void)
3035{
James Smart92d7f7b2007-06-17 19:56:38 -05003036 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003037}
3038
James Smarte59058c2008-08-24 21:49:00 -04003039/**
James Smart3621a712009-04-06 18:47:14 -04003040 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003041 *
3042 * This function adds the number of hbq entries in every HBQ to get
3043 * the total number of hbq entries required for the HBA and returns
3044 * the total count.
3045 **/
James Smarted957682007-06-17 19:56:37 -05003046static int
3047lpfc_sli_hbq_entry_count(void)
3048{
3049 int hbq_count = lpfc_sli_hbq_count();
3050 int count = 0;
3051 int i;
3052
3053 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003054 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003055 return count;
3056}
3057
James Smarte59058c2008-08-24 21:49:00 -04003058/**
James Smart3621a712009-04-06 18:47:14 -04003059 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003060 *
3061 * This function calculates amount of memory required for all hbq entries
3062 * to be configured and returns the total memory required.
3063 **/
dea31012005-04-17 16:05:31 -05003064int
James Smarted957682007-06-17 19:56:37 -05003065lpfc_sli_hbq_size(void)
3066{
3067 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3068}
3069
James Smarte59058c2008-08-24 21:49:00 -04003070/**
James Smart3621a712009-04-06 18:47:14 -04003071 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003072 * @phba: Pointer to HBA context object.
3073 *
3074 * This function is called during the SLI initialization to configure
3075 * all the HBQs and post buffers to the HBQ. The caller is not
3076 * required to hold any locks. This function will return zero if successful
3077 * else it will return negative error code.
3078 **/
James Smarted957682007-06-17 19:56:37 -05003079static int
3080lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3081{
3082 int hbq_count = lpfc_sli_hbq_count();
3083 LPFC_MBOXQ_t *pmb;
3084 MAILBOX_t *pmbox;
3085 uint32_t hbqno;
3086 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003087
James Smart92d7f7b2007-06-17 19:56:38 -05003088 /* Get a Mailbox buffer to setup mailbox
3089 * commands for HBA initialization
3090 */
James Smarted957682007-06-17 19:56:37 -05003091 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3092
3093 if (!pmb)
3094 return -ENOMEM;
3095
3096 pmbox = &pmb->mb;
3097
3098 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3099 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003100 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003101
3102 hbq_entry_index = 0;
3103 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3104 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3105 phba->hbqs[hbqno].hbqPutIdx = 0;
3106 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3107 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003108 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003109 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3110 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003111 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3112
3113 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3114 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3115 mbxStatus <status>, ring <num> */
3116
3117 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003118 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003119 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003120 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003121 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003122 pmbox->mbxStatus, hbqno);
3123
3124 phba->link_state = LPFC_HBA_ERROR;
3125 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003126 return ENXIO;
3127 }
3128 }
3129 phba->hbq_count = hbq_count;
3130
James Smarted957682007-06-17 19:56:37 -05003131 mempool_free(pmb, phba->mbox_mem_pool);
3132
James Smart92d7f7b2007-06-17 19:56:38 -05003133 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003134 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3135 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003136 return 0;
3137}
3138
James Smarte59058c2008-08-24 21:49:00 -04003139/**
James Smart3621a712009-04-06 18:47:14 -04003140 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003141 * @phba: Pointer to HBA context object.
3142 * @sli_mode: sli mode - 2/3
3143 *
3144 * This function is called by the sli intialization code path
3145 * to issue config_port mailbox command. This function restarts the
3146 * HBA firmware and issues a config_port mailbox command to configure
3147 * the SLI interface in the sli mode specified by sli_mode
3148 * variable. The caller is not required to hold any locks.
3149 * The function returns 0 if successful, else returns negative error
3150 * code.
3151 **/
James Smart93996272008-08-24 21:50:30 -04003152int
3153lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003154{
3155 LPFC_MBOXQ_t *pmb;
3156 uint32_t resetcount = 0, rc = 0, done = 0;
3157
3158 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3159 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003160 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003161 return -ENOMEM;
3162 }
3163
James Smarted957682007-06-17 19:56:37 -05003164 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003165 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003166 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003167 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003168 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003169 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003170 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003171 rc = lpfc_sli_chipset_init(phba);
3172 if (rc)
3173 break;
3174
James Smart2e0fef82007-06-17 19:56:36 -05003175 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003176 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003177 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003178 resetcount++;
3179
James Smarted957682007-06-17 19:56:37 -05003180 /* Call pre CONFIG_PORT mailbox command initialization. A
3181 * value of 0 means the call was successful. Any other
3182 * nonzero value is a failure, but if ERESTART is returned,
3183 * the driver may reset the HBA and try again.
3184 */
dea31012005-04-17 16:05:31 -05003185 rc = lpfc_config_port_prep(phba);
3186 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003187 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003188 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003189 } else if (rc)
dea31012005-04-17 16:05:31 -05003190 break;
James Smart2e0fef82007-06-17 19:56:36 -05003191 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003192 lpfc_config_port(phba, pmb);
3193 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003194 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3195 LPFC_SLI3_HBQ_ENABLED |
3196 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003197 LPFC_SLI3_INB_ENABLED |
3198 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003199 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003201 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003202 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003203 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003204 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003205 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003206 spin_unlock_irq(&phba->hbalock);
3207 rc = -ENXIO;
James Smart34b02dc2008-08-24 21:49:55 -04003208 } else
James Smarted957682007-06-17 19:56:37 -05003209 done = 1;
dea31012005-04-17 16:05:31 -05003210 }
James Smarted957682007-06-17 19:56:37 -05003211 if (!done) {
3212 rc = -EINVAL;
3213 goto do_prep_failed;
3214 }
James Smart34b02dc2008-08-24 21:49:55 -04003215 if (pmb->mb.un.varCfgPort.sli_mode == 3) {
3216 if (!pmb->mb.un.varCfgPort.cMA) {
3217 rc = -ENXIO;
3218 goto do_prep_failed;
3219 }
3220 if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
3221 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3222 phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
3223 } else
3224 phba->max_vpi = 0;
3225 if (pmb->mb.un.varCfgPort.gerbm)
3226 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3227 if (pmb->mb.un.varCfgPort.gcrp)
3228 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3229 if (pmb->mb.un.varCfgPort.ginb) {
3230 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003231 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003232 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3233 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3234 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3235 phba->inb_last_counter =
3236 phba->mbox->us.s3_inb_pgp.counter;
3237 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003238 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003239 phba->port_gp = phba->mbox->us.s3_pgp.port;
3240 phba->inb_ha_copy = NULL;
3241 phba->inb_counter = NULL;
3242 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003243
3244 if (phba->cfg_enable_bg) {
3245 if (pmb->mb.un.varCfgPort.gbg)
3246 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3247 else
3248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3249 "0443 Adapter did not grant "
3250 "BlockGuard\n");
3251 }
James Smart34b02dc2008-08-24 21:49:55 -04003252 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003253 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003254 phba->port_gp = phba->mbox->us.s2.port;
3255 phba->inb_ha_copy = NULL;
3256 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003257 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003258 }
James Smart92d7f7b2007-06-17 19:56:38 -05003259do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003260 mempool_free(pmb, phba->mbox_mem_pool);
3261 return rc;
3262}
3263
James Smarte59058c2008-08-24 21:49:00 -04003264
3265/**
James Smart3621a712009-04-06 18:47:14 -04003266 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003267 * @phba: Pointer to HBA context object.
3268 *
3269 * This function is the main SLI intialization function. This function
3270 * is called by the HBA intialization code, HBA reset code and HBA
3271 * error attention handler code. Caller is not required to hold any
3272 * locks. This function issues config_port mailbox command to configure
3273 * the SLI, setup iocb rings and HBQ rings. In the end the function
3274 * calls the config_port_post function to issue init_link mailbox
3275 * command and to start the discovery. The function will return zero
3276 * if successful, else it will return negative error code.
3277 **/
James Smarted957682007-06-17 19:56:37 -05003278int
3279lpfc_sli_hba_setup(struct lpfc_hba *phba)
3280{
3281 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003282 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003283
3284 switch (lpfc_sli_mode) {
3285 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003286 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003288 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003289 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003290 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003291 break;
3292 }
James Smarted957682007-06-17 19:56:37 -05003293 mode = 2;
3294 break;
3295 case 0:
3296 case 3:
3297 break;
3298 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003300 "1819 Unrecognized lpfc_sli_mode "
3301 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003302
3303 break;
3304 }
3305
James Smart93996272008-08-24 21:50:30 -04003306 rc = lpfc_sli_config_port(phba, mode);
3307
James Smarted957682007-06-17 19:56:37 -05003308 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003310 "1820 Unable to select SLI-3. "
3311 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003312 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04003313 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05003314 if (rc)
dea31012005-04-17 16:05:31 -05003315 goto lpfc_sli_hba_setup_error;
3316
James Smarted957682007-06-17 19:56:37 -05003317 if (phba->sli_rev == 3) {
3318 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3319 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05003320 } else {
3321 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3322 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003323 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003324 }
3325
3326 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003327 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3328 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05003329 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05003330
3331 if (rc)
3332 goto lpfc_sli_hba_setup_error;
3333
James Smart93996272008-08-24 21:50:30 -04003334 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05003335 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3336 rc = lpfc_sli_hbq_setup(phba);
3337 if (rc)
3338 goto lpfc_sli_hba_setup_error;
3339 }
3340
dea31012005-04-17 16:05:31 -05003341 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3342
3343 rc = lpfc_config_port_post(phba);
3344 if (rc)
3345 goto lpfc_sli_hba_setup_error;
3346
James Smarted957682007-06-17 19:56:37 -05003347 return rc;
3348
James Smart92d7f7b2007-06-17 19:56:38 -05003349lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05003350 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05003351 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003352 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05003353 return rc;
3354}
3355
James Smarte59058c2008-08-24 21:49:00 -04003356
3357/**
James Smart3621a712009-04-06 18:47:14 -04003358 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04003359 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05003360 *
James Smarte59058c2008-08-24 21:49:00 -04003361 * This is the callback function for mailbox timer. The mailbox
3362 * timer is armed when a new mailbox command is issued and the timer
3363 * is deleted when the mailbox complete. The function is called by
3364 * the kernel timer code when a mailbox does not complete within
3365 * expected time. This function wakes up the worker thread to
3366 * process the mailbox timeout and returns. All the processing is
3367 * done by the worker thread function lpfc_mbox_timeout_handler.
3368 **/
dea31012005-04-17 16:05:31 -05003369void
3370lpfc_mbox_timeout(unsigned long ptr)
3371{
James Smart92d7f7b2007-06-17 19:56:38 -05003372 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05003373 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05003374 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003375
James Smart2e0fef82007-06-17 19:56:36 -05003376 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05003377 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003378 if (!tmo_posted)
3379 phba->pport->work_port_events |= WORKER_MBOX_TMO;
3380 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
3381
James Smart5e9d9b82008-06-14 22:52:53 -04003382 if (!tmo_posted)
3383 lpfc_worker_wake_up(phba);
3384 return;
dea31012005-04-17 16:05:31 -05003385}
3386
James Smarte59058c2008-08-24 21:49:00 -04003387
3388/**
James Smart3621a712009-04-06 18:47:14 -04003389 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04003390 * @phba: Pointer to HBA context object.
3391 *
3392 * This function is called from worker thread when a mailbox command times out.
3393 * The caller is not required to hold any locks. This function will reset the
3394 * HBA and recover all the pending commands.
3395 **/
dea31012005-04-17 16:05:31 -05003396void
3397lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
3398{
James Smart2e0fef82007-06-17 19:56:36 -05003399 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
3400 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04003401 struct lpfc_sli *psli = &phba->sli;
3402 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003403
James Smarta257bf92009-04-06 18:48:10 -04003404 /* Check the pmbox pointer first. There is a race condition
3405 * between the mbox timeout handler getting executed in the
3406 * worklist and the mailbox actually completing. When this
3407 * race condition occurs, the mbox_active will be NULL.
3408 */
3409 spin_lock_irq(&phba->hbalock);
3410 if (pmbox == NULL) {
3411 lpfc_printf_log(phba, KERN_WARNING,
3412 LOG_MBOX | LOG_SLI,
3413 "0353 Active Mailbox cleared - mailbox timeout "
3414 "exiting\n");
3415 spin_unlock_irq(&phba->hbalock);
3416 return;
3417 }
3418
dea31012005-04-17 16:05:31 -05003419 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05003420 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003421 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003422 mb->mbxCommand,
3423 phba->pport->port_state,
3424 phba->sli.sli_flag,
3425 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04003426 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003427
James Smart1dcb58e2007-04-25 09:51:30 -04003428 /* Setting state unknown so lpfc_sli_abort_iocb_ring
3429 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
3430 * it to fail all oustanding SCSI IO.
3431 */
James Smart2e0fef82007-06-17 19:56:36 -05003432 spin_lock_irq(&phba->pport->work_port_lock);
3433 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3434 spin_unlock_irq(&phba->pport->work_port_lock);
3435 spin_lock_irq(&phba->hbalock);
3436 phba->link_state = LPFC_LINK_UNKNOWN;
James Smart1dcb58e2007-04-25 09:51:30 -04003437 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003438 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04003439
3440 pring = &psli->ring[psli->fcp_ring];
3441 lpfc_sli_abort_iocb_ring(phba, pring);
3442
3443 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003444 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04003445
3446 /* Reset the HBA device */
3447 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05003448}
3449
James Smarte59058c2008-08-24 21:49:00 -04003450/**
James Smart3772a992009-05-22 14:50:54 -04003451 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04003452 * @phba: Pointer to HBA context object.
3453 * @pmbox: Pointer to mailbox object.
3454 * @flag: Flag indicating how the mailbox need to be processed.
3455 *
3456 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04003457 * to submit a mailbox command to firmware with SLI-3 interface spec. This
3458 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04003459 * The mailbox command can be submitted in polling mode, in which case
3460 * this function will wait in a polling loop for the completion of the
3461 * mailbox.
3462 * If the mailbox is submitted in no_wait mode (not polling) the
3463 * function will submit the command and returns immediately without waiting
3464 * for the mailbox completion. The no_wait is supported only when HBA
3465 * is in SLI2/SLI3 mode - interrupts are enabled.
3466 * The SLI interface allows only one mailbox pending at a time. If the
3467 * mailbox is issued in polling mode and there is already a mailbox
3468 * pending, then the function will return an error. If the mailbox is issued
3469 * in NO_WAIT mode and there is a mailbox pending already, the function
3470 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
3471 * The sli layer owns the mailbox object until the completion of mailbox
3472 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
3473 * return codes the caller owns the mailbox command after the return of
3474 * the function.
3475 **/
James Smart3772a992009-05-22 14:50:54 -04003476static int
3477lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
3478 uint32_t flag)
dea31012005-04-17 16:05:31 -05003479{
dea31012005-04-17 16:05:31 -05003480 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05003481 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003482 uint32_t status, evtctr;
3483 uint32_t ha_copy;
3484 int i;
James Smart09372822008-01-11 01:52:54 -05003485 unsigned long timeout;
dea31012005-04-17 16:05:31 -05003486 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04003487 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05003488 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04003489 int processing_queue = 0;
3490
3491 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3492 if (!pmbox) {
3493 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04003494 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
3495 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3496 return MBX_SUCCESS;
3497 }
James Smart58da1ff2008-04-07 10:15:56 -04003498 processing_queue = 1;
3499 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3500 pmbox = lpfc_mbox_get(phba);
3501 if (!pmbox) {
3502 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3503 return MBX_SUCCESS;
3504 }
3505 }
dea31012005-04-17 16:05:31 -05003506
James Smarted957682007-06-17 19:56:37 -05003507 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05003508 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05003509 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04003510 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05003511 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003512 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003513 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04003514 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05003515 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04003516 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05003517 }
3518 }
3519
Linas Vepstas8d63f372007-02-14 14:28:36 -06003520 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04003521 if (unlikely(pci_channel_offline(phba->pcidev))) {
3522 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3523 goto out_not_finished;
3524 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06003525
James Smarta257bf92009-04-06 18:48:10 -04003526 /* If HBA has a deferred error attention, fail the iocb. */
3527 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3528 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3529 goto out_not_finished;
3530 }
3531
dea31012005-04-17 16:05:31 -05003532 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05003533
James Smart3772a992009-05-22 14:50:54 -04003534 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05003535 status = MBX_SUCCESS;
3536
James Smart2e0fef82007-06-17 19:56:36 -05003537 if (phba->link_state == LPFC_HBA_ERROR) {
3538 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003539
3540 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04003541 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3542 "(%d):0311 Mailbox command x%x cannot "
3543 "issue Data: x%x x%x\n",
3544 pmbox->vport ? pmbox->vport->vpi : 0,
3545 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003546 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003547 }
3548
James Smart92908312006-03-07 15:04:13 -05003549 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
3550 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05003551 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04003552 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3553 "(%d):2528 Mailbox command x%x cannot "
3554 "issue Data: x%x x%x\n",
3555 pmbox->vport ? pmbox->vport->vpi : 0,
3556 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003557 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05003558 }
3559
dea31012005-04-17 16:05:31 -05003560 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
3561 /* Polling for a mbox command when another one is already active
3562 * is not allowed in SLI. Also, the driver must have established
3563 * SLI2 mode to queue and process multiple mbox commands.
3564 */
3565
3566 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05003567 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003568
3569 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04003570 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3571 "(%d):2529 Mailbox command x%x "
3572 "cannot issue Data: x%x x%x\n",
3573 pmbox->vport ? pmbox->vport->vpi : 0,
3574 pmbox->u.mb.mbxCommand,
3575 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003576 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003577 }
3578
James Smart3772a992009-05-22 14:50:54 -04003579 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003580 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003581 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04003582 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3583 "(%d):2530 Mailbox command x%x "
3584 "cannot issue Data: x%x x%x\n",
3585 pmbox->vport ? pmbox->vport->vpi : 0,
3586 pmbox->u.mb.mbxCommand,
3587 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003588 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003589 }
3590
dea31012005-04-17 16:05:31 -05003591 /* Another mailbox command is still being processed, queue this
3592 * command to be processed later.
3593 */
3594 lpfc_mbox_put(phba, pmbox);
3595
3596 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05003597 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003598 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003599 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003600 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
3601 mb->mbxCommand, phba->pport->port_state,
3602 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003603
3604 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05003605 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003606
James Smart858c9f62007-06-17 19:56:39 -05003607 if (pmbox->vport) {
3608 lpfc_debugfs_disc_trc(pmbox->vport,
3609 LPFC_DISC_TRC_MBOX_VPORT,
3610 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
3611 (uint32_t)mb->mbxCommand,
3612 mb->un.varWords[0], mb->un.varWords[1]);
3613 }
3614 else {
3615 lpfc_debugfs_disc_trc(phba->pport,
3616 LPFC_DISC_TRC_MBOX,
3617 "MBOX Bsy: cmd:x%x mb:x%x x%x",
3618 (uint32_t)mb->mbxCommand,
3619 mb->un.varWords[0], mb->un.varWords[1]);
3620 }
3621
James Smart2e0fef82007-06-17 19:56:36 -05003622 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05003623 }
3624
dea31012005-04-17 16:05:31 -05003625 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3626
3627 /* If we are not polling, we MUST be in SLI2 mode */
3628 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04003629 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05003630 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05003631 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003632 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003633 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04003634 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3635 "(%d):2531 Mailbox command x%x "
3636 "cannot issue Data: x%x x%x\n",
3637 pmbox->vport ? pmbox->vport->vpi : 0,
3638 pmbox->u.mb.mbxCommand,
3639 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003640 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003641 }
3642 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04003643 mod_timer(&psli->mbox_tmo, (jiffies +
3644 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05003645 }
3646
3647 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05003648 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003649 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003650 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003651 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05003652 mb->mbxCommand, phba->pport->port_state,
3653 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003654
James Smart858c9f62007-06-17 19:56:39 -05003655 if (mb->mbxCommand != MBX_HEARTBEAT) {
3656 if (pmbox->vport) {
3657 lpfc_debugfs_disc_trc(pmbox->vport,
3658 LPFC_DISC_TRC_MBOX_VPORT,
3659 "MBOX Send vport: cmd:x%x mb:x%x x%x",
3660 (uint32_t)mb->mbxCommand,
3661 mb->un.varWords[0], mb->un.varWords[1]);
3662 }
3663 else {
3664 lpfc_debugfs_disc_trc(phba->pport,
3665 LPFC_DISC_TRC_MBOX,
3666 "MBOX Send: cmd:x%x mb:x%x x%x",
3667 (uint32_t)mb->mbxCommand,
3668 mb->un.varWords[0], mb->un.varWords[1]);
3669 }
3670 }
3671
dea31012005-04-17 16:05:31 -05003672 psli->slistat.mbox_cmd++;
3673 evtctr = psli->slistat.mbox_event;
3674
3675 /* next set own bit for the adapter and copy over command word */
3676 mb->mbxOwner = OWN_CHIP;
3677
James Smart3772a992009-05-22 14:50:54 -04003678 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05003679 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04003680 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003681 } else {
James Smart92908312006-03-07 15:04:13 -05003682 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05003683 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04003684 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003685 }
3686
3687 /* First copy mbox command data to HBA SLIM, skip past first
3688 word */
3689 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3690 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
3691 MAILBOX_CMD_SIZE - sizeof (uint32_t));
3692
3693 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04003694 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05003695 to_slim = phba->MBslimaddr;
3696 writel(ldata, to_slim);
3697 readl(to_slim); /* flush */
3698
3699 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3700 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04003701 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05003702 }
3703 }
3704
3705 wmb();
dea31012005-04-17 16:05:31 -05003706
3707 switch (flag) {
3708 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05003709 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05003710 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05003711 /* Interrupt board to do it */
3712 writel(CA_MBATT, phba->CAregaddr);
3713 readl(phba->CAregaddr); /* flush */
3714 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05003715 break;
3716
3717 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05003718 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05003719 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05003720 /* Interrupt board to do it */
3721 writel(CA_MBATT, phba->CAregaddr);
3722 readl(phba->CAregaddr); /* flush */
3723
James Smart3772a992009-05-22 14:50:54 -04003724 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05003725 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04003726 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003727 word0 = le32_to_cpu(word0);
3728 } else {
3729 /* First read mbox status word */
3730 word0 = readl(phba->MBslimaddr);
3731 }
3732
3733 /* Read the HBA Host Attention Register */
3734 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05003735 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3736 mb->mbxCommand) *
3737 1000) + jiffies;
3738 i = 0;
dea31012005-04-17 16:05:31 -05003739 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003740 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
3741 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05003742 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05003743 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05003744 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003745 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05003746 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04003747 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003748 }
3749
3750 /* Check if we took a mbox interrupt while we were
3751 polling */
3752 if (((word0 & OWN_CHIP) != OWN_CHIP)
3753 && (evtctr != psli->slistat.mbox_event))
3754 break;
3755
James Smart09372822008-01-11 01:52:54 -05003756 if (i++ > 10) {
3757 spin_unlock_irqrestore(&phba->hbalock,
3758 drvr_flag);
3759 msleep(1);
3760 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3761 }
dea31012005-04-17 16:05:31 -05003762
James Smart3772a992009-05-22 14:50:54 -04003763 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05003764 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04003765 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003766 word0 = le32_to_cpu(word0);
3767 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3768 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04003769 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05003770 /* Check real SLIM for any errors */
3771 slimword0 = readl(phba->MBslimaddr);
3772 slimmb = (MAILBOX_t *) & slimword0;
3773 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
3774 && slimmb->mbxStatus) {
3775 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04003776 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05003777 word0 = slimword0;
3778 }
3779 }
3780 } else {
3781 /* First copy command data */
3782 word0 = readl(phba->MBslimaddr);
3783 }
3784 /* Read the HBA Host Attention Register */
3785 ha_copy = readl(phba->HAregaddr);
3786 }
3787
James Smart3772a992009-05-22 14:50:54 -04003788 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05003789 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04003790 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003791 } else {
3792 /* First copy command data */
3793 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
3794 MAILBOX_CMD_SIZE);
3795 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
3796 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05003797 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05003798 phba->MBslimaddr + DMP_RSP_OFFSET,
3799 mb->un.varDmp.word_cnt);
3800 }
3801 }
3802
3803 writel(HA_MBATT, phba->HAregaddr);
3804 readl(phba->HAregaddr); /* flush */
3805
3806 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3807 status = mb->mbxStatus;
3808 }
3809
James Smart2e0fef82007-06-17 19:56:36 -05003810 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3811 return status;
James Smart58da1ff2008-04-07 10:15:56 -04003812
3813out_not_finished:
3814 if (processing_queue) {
3815 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
3816 lpfc_mbox_cmpl_put(phba, pmbox);
3817 }
3818 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003819}
3820
James Smarte59058c2008-08-24 21:49:00 -04003821/**
James Smart3621a712009-04-06 18:47:14 -04003822 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04003823 * @phba: Pointer to HBA context object.
3824 * @pring: Pointer to driver SLI ring object.
3825 * @piocb: Pointer to address of newly added command iocb.
3826 *
3827 * This function is called with hbalock held to add a command
3828 * iocb to the txq when SLI layer cannot submit the command iocb
3829 * to the ring.
3830 **/
James Smart858c9f62007-06-17 19:56:39 -05003831static void
James Smart92d7f7b2007-06-17 19:56:38 -05003832__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003833 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05003834{
3835 /* Insert the caller's iocb in the txq tail for later processing. */
3836 list_add_tail(&piocb->list, &pring->txq);
3837 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05003838}
3839
James Smarte59058c2008-08-24 21:49:00 -04003840/**
James Smart3621a712009-04-06 18:47:14 -04003841 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04003842 * @phba: Pointer to HBA context object.
3843 * @pring: Pointer to driver SLI ring object.
3844 * @piocb: Pointer to address of newly added command iocb.
3845 *
3846 * This function is called with hbalock held before a new
3847 * iocb is submitted to the firmware. This function checks
3848 * txq to flush the iocbs in txq to Firmware before
3849 * submitting new iocbs to the Firmware.
3850 * If there are iocbs in the txq which need to be submitted
3851 * to firmware, lpfc_sli_next_iocb returns the first element
3852 * of the txq after dequeuing it from txq.
3853 * If there is no iocb in the txq then the function will return
3854 * *piocb and *piocb is set to NULL. Caller needs to check
3855 * *piocb to find if there are more commands in the txq.
3856 **/
dea31012005-04-17 16:05:31 -05003857static struct lpfc_iocbq *
3858lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003859 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05003860{
3861 struct lpfc_iocbq * nextiocb;
3862
3863 nextiocb = lpfc_sli_ringtx_get(phba, pring);
3864 if (!nextiocb) {
3865 nextiocb = *piocb;
3866 *piocb = NULL;
3867 }
3868
3869 return nextiocb;
3870}
3871
James Smarte59058c2008-08-24 21:49:00 -04003872/**
James Smart3772a992009-05-22 14:50:54 -04003873 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04003874 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04003875 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04003876 * @piocb: Pointer to command iocb.
3877 * @flag: Flag indicating if this command can be put into txq.
3878 *
James Smart3772a992009-05-22 14:50:54 -04003879 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
3880 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
3881 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
3882 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
3883 * this function allows only iocbs for posting buffers. This function finds
3884 * next available slot in the command ring and posts the command to the
3885 * available slot and writes the port attention register to request HBA start
3886 * processing new iocb. If there is no slot available in the ring and
3887 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
3888 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04003889 *
James Smart3772a992009-05-22 14:50:54 -04003890 * This function is called with hbalock held. The function will return success
3891 * after it successfully submit the iocb to firmware or after adding to the
3892 * txq.
James Smarte59058c2008-08-24 21:49:00 -04003893 **/
James Smart98c9ea52007-10-27 13:37:33 -04003894static int
James Smart3772a992009-05-22 14:50:54 -04003895__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05003896 struct lpfc_iocbq *piocb, uint32_t flag)
3897{
3898 struct lpfc_iocbq *nextiocb;
3899 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04003900 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05003901
James Smart92d7f7b2007-06-17 19:56:38 -05003902 if (piocb->iocb_cmpl && (!piocb->vport) &&
3903 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
3904 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
3905 lpfc_printf_log(phba, KERN_ERR,
3906 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003907 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003908 piocb->iocb.ulpCommand);
3909 dump_stack();
3910 return IOCB_ERROR;
3911 }
3912
3913
Linas Vepstas8d63f372007-02-14 14:28:36 -06003914 /* If the PCI channel is in offline state, do not post iocbs. */
3915 if (unlikely(pci_channel_offline(phba->pcidev)))
3916 return IOCB_ERROR;
3917
James Smarta257bf92009-04-06 18:48:10 -04003918 /* If HBA has a deferred error attention, fail the iocb. */
3919 if (unlikely(phba->hba_flag & DEFER_ERATT))
3920 return IOCB_ERROR;
3921
dea31012005-04-17 16:05:31 -05003922 /*
3923 * We should never get an IOCB if we are in a < LINK_DOWN state
3924 */
James Smart2e0fef82007-06-17 19:56:36 -05003925 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003926 return IOCB_ERROR;
3927
3928 /*
3929 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04003930 * outstanding event.
dea31012005-04-17 16:05:31 -05003931 */
James Smart0b727fe2007-10-27 13:37:25 -04003932 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05003933 goto iocb_busy;
3934
James Smart2e0fef82007-06-17 19:56:36 -05003935 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05003936 /*
James Smart2680eea2007-04-25 09:52:55 -04003937 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05003938 * can be issued if the link is not up.
3939 */
3940 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04003941 case CMD_GEN_REQUEST64_CR:
3942 case CMD_GEN_REQUEST64_CX:
3943 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
3944 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
3945 FC_FCP_CMND) ||
3946 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
3947 MENLO_TRANSPORT_TYPE))
3948
3949 goto iocb_busy;
3950 break;
dea31012005-04-17 16:05:31 -05003951 case CMD_QUE_RING_BUF_CN:
3952 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05003953 /*
3954 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
3955 * completion, iocb_cmpl MUST be 0.
3956 */
3957 if (piocb->iocb_cmpl)
3958 piocb->iocb_cmpl = NULL;
3959 /*FALLTHROUGH*/
3960 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04003961 case CMD_CLOSE_XRI_CN:
3962 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05003963 break;
3964 default:
3965 goto iocb_busy;
3966 }
3967
3968 /*
3969 * For FCP commands, we must be in a state where we can process link
3970 * attention events.
3971 */
3972 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05003973 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05003974 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05003975 }
dea31012005-04-17 16:05:31 -05003976
dea31012005-04-17 16:05:31 -05003977 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
3978 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
3979 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
3980
3981 if (iocb)
3982 lpfc_sli_update_ring(phba, pring);
3983 else
3984 lpfc_sli_update_full_ring(phba, pring);
3985
3986 if (!piocb)
3987 return IOCB_SUCCESS;
3988
3989 goto out_busy;
3990
3991 iocb_busy:
3992 pring->stats.iocb_cmd_delay++;
3993
3994 out_busy:
3995
3996 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003997 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05003998 return IOCB_SUCCESS;
3999 }
4000
4001 return IOCB_BUSY;
4002}
4003
James Smart3772a992009-05-22 14:50:54 -04004004/**
4005 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
4006 *
4007 * This routine wraps the actual lockless version for issusing IOCB function
4008 * pointer from the lpfc_hba struct.
4009 *
4010 * Return codes:
4011 * IOCB_ERROR - Error
4012 * IOCB_SUCCESS - Success
4013 * IOCB_BUSY - Busy
4014 **/
4015static inline int
4016__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
4017 struct lpfc_iocbq *piocb, uint32_t flag)
4018{
4019 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
4020}
4021
4022/**
4023 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
4024 * @phba: The hba struct for which this call is being executed.
4025 * @dev_grp: The HBA PCI-Device group number.
4026 *
4027 * This routine sets up the SLI interface API function jump table in @phba
4028 * struct.
4029 * Returns: 0 - success, -ENODEV - failure.
4030 **/
4031int
4032lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4033{
4034
4035 switch (dev_grp) {
4036 case LPFC_PCI_DEV_LP:
4037 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
4038 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
4039 break;
4040 default:
4041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4042 "1419 Invalid HBA PCI-device group: 0x%x\n",
4043 dev_grp);
4044 return -ENODEV;
4045 break;
4046 }
4047 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
4048 return 0;
4049}
James Smart92d7f7b2007-06-17 19:56:38 -05004050
James Smarte59058c2008-08-24 21:49:00 -04004051/**
James Smart3621a712009-04-06 18:47:14 -04004052 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04004053 * @phba: Pointer to HBA context object.
4054 * @pring: Pointer to driver SLI ring object.
4055 * @piocb: Pointer to command iocb.
4056 * @flag: Flag indicating if this command can be put into txq.
4057 *
4058 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
4059 * function. This function gets the hbalock and calls
4060 * __lpfc_sli_issue_iocb function and will return the error returned
4061 * by __lpfc_sli_issue_iocb function. This wrapper is used by
4062 * functions which do not hold hbalock.
4063 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004064int
James Smart3772a992009-05-22 14:50:54 -04004065lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05004066 struct lpfc_iocbq *piocb, uint32_t flag)
4067{
4068 unsigned long iflags;
4069 int rc;
4070
4071 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04004072 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05004073 spin_unlock_irqrestore(&phba->hbalock, iflags);
4074
4075 return rc;
4076}
4077
James Smarte59058c2008-08-24 21:49:00 -04004078/**
James Smart3621a712009-04-06 18:47:14 -04004079 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04004080 * @phba: Pointer to HBA context object.
4081 *
4082 * This function is called while driver attaches with the
4083 * HBA to setup the extra ring. The extra ring is used
4084 * only when driver needs to support target mode functionality
4085 * or IP over FC functionalities.
4086 *
4087 * This function is called with no lock held.
4088 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004089static int
4090lpfc_extra_ring_setup( struct lpfc_hba *phba)
4091{
4092 struct lpfc_sli *psli;
4093 struct lpfc_sli_ring *pring;
4094
4095 psli = &phba->sli;
4096
4097 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05004098
4099 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004100 pring = &psli->ring[psli->fcp_ring];
4101 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
4102 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
4103 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
4104 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
4105
James Smarta4bc3372006-12-02 13:34:16 -05004106 /* and give them to the extra ring */
4107 pring = &psli->ring[psli->extra_ring];
4108
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004109 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
4110 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
4111 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
4112 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
4113
4114 /* Setup default profile for this ring */
4115 pring->iotag_max = 4096;
4116 pring->num_mask = 1;
4117 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05004118 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
4119 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004120 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
4121 return 0;
4122}
4123
James Smarte59058c2008-08-24 21:49:00 -04004124/**
James Smart3621a712009-04-06 18:47:14 -04004125 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04004126 * @phba: Pointer to HBA context object.
4127 * @pring: Pointer to driver SLI ring object.
4128 * @iocbq: Pointer to iocb object.
4129 *
4130 * This function is called by the slow ring event handler
4131 * function when there is an ASYNC event iocb in the ring.
4132 * This function is called with no lock held.
4133 * Currently this function handles only temperature related
4134 * ASYNC events. The function decodes the temperature sensor
4135 * event message and posts events for the management applications.
4136 **/
James Smart98c9ea52007-10-27 13:37:33 -04004137static void
James Smart57127f12007-10-27 13:37:05 -04004138lpfc_sli_async_event_handler(struct lpfc_hba * phba,
4139 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
4140{
4141 IOCB_t *icmd;
4142 uint16_t evt_code;
4143 uint16_t temp;
4144 struct temp_event temp_event_data;
4145 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04004146 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04004147
4148 icmd = &iocbq->iocb;
4149 evt_code = icmd->un.asyncstat.evt_code;
4150 temp = icmd->ulpContext;
4151
4152 if ((evt_code != ASYNC_TEMP_WARN) &&
4153 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04004154 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04004155 lpfc_printf_log(phba,
4156 KERN_ERR,
4157 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004158 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarta257bf92009-04-06 18:48:10 -04004159 " evt_code 0x%x \n"
4160 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
4161 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
4162 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
4163 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04004164 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04004165 icmd->un.asyncstat.evt_code,
4166 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
4167 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
4168 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
4169 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
4170
James Smart57127f12007-10-27 13:37:05 -04004171 return;
4172 }
4173 temp_event_data.data = (uint32_t)temp;
4174 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4175 if (evt_code == ASYNC_TEMP_WARN) {
4176 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4177 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05004178 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04004179 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04004180 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04004181 "corrective action. temperature : %d Celsius\n",
4182 temp);
4183 }
4184 if (evt_code == ASYNC_TEMP_SAFE) {
4185 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4186 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05004187 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04004188 LOG_TEMP,
4189 "0340 Adapter temperature is OK now. "
4190 "temperature : %d Celsius\n",
4191 temp);
4192 }
4193
4194 /* Send temperature change event to applications */
4195 shost = lpfc_shost_from_vport(phba->pport);
4196 fc_host_post_vendor_event(shost, fc_get_event_number(),
4197 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05004198 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04004199
4200}
4201
4202
James Smarte59058c2008-08-24 21:49:00 -04004203/**
James Smart3621a712009-04-06 18:47:14 -04004204 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04004205 * @phba: Pointer to HBA context object.
4206 *
4207 * lpfc_sli_setup sets up rings of the SLI interface with
4208 * number of iocbs per ring and iotags. This function is
4209 * called while driver attach to the HBA and before the
4210 * interrupts are enabled. So there is no need for locking.
4211 *
4212 * This function always returns 0.
4213 **/
dea31012005-04-17 16:05:31 -05004214int
4215lpfc_sli_setup(struct lpfc_hba *phba)
4216{
James Smarted957682007-06-17 19:56:37 -05004217 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05004218 struct lpfc_sli *psli = &phba->sli;
4219 struct lpfc_sli_ring *pring;
4220
4221 psli->num_rings = MAX_CONFIGURED_RINGS;
4222 psli->sli_flag = 0;
4223 psli->fcp_ring = LPFC_FCP_RING;
4224 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05004225 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05004226
James Bottomley604a3e32005-10-29 10:28:33 -05004227 psli->iocbq_lookup = NULL;
4228 psli->iocbq_lookup_len = 0;
4229 psli->last_iotag = 0;
4230
dea31012005-04-17 16:05:31 -05004231 for (i = 0; i < psli->num_rings; i++) {
4232 pring = &psli->ring[i];
4233 switch (i) {
4234 case LPFC_FCP_RING: /* ring 0 - FCP */
4235 /* numCiocb and numRiocb are used in config_port */
4236 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
4237 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
4238 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
4239 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
4240 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
4241 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004242 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004243 SLI3_IOCB_CMD_SIZE :
4244 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004245 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004246 SLI3_IOCB_RSP_SIZE :
4247 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05004248 pring->iotag_ctr = 0;
4249 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05004250 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05004251 pring->fast_iotag = pring->iotag_max;
4252 pring->num_mask = 0;
4253 break;
James Smarta4bc3372006-12-02 13:34:16 -05004254 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05004255 /* numCiocb and numRiocb are used in config_port */
4256 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
4257 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004258 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004259 SLI3_IOCB_CMD_SIZE :
4260 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004261 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004262 SLI3_IOCB_RSP_SIZE :
4263 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05004264 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05004265 pring->num_mask = 0;
4266 break;
4267 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
4268 /* numCiocb and numRiocb are used in config_port */
4269 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
4270 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004271 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004272 SLI3_IOCB_CMD_SIZE :
4273 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004274 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004275 SLI3_IOCB_RSP_SIZE :
4276 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05004277 pring->fast_iotag = 0;
4278 pring->iotag_ctr = 0;
4279 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04004280 pring->lpfc_sli_rcv_async_status =
4281 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05004282 pring->num_mask = 4;
4283 pring->prt[0].profile = 0; /* Mask 0 */
4284 pring->prt[0].rctl = FC_ELS_REQ;
4285 pring->prt[0].type = FC_ELS_DATA;
4286 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004287 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004288 pring->prt[1].profile = 0; /* Mask 1 */
4289 pring->prt[1].rctl = FC_ELS_RSP;
4290 pring->prt[1].type = FC_ELS_DATA;
4291 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004292 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004293 pring->prt[2].profile = 0; /* Mask 2 */
4294 /* NameServer Inquiry */
4295 pring->prt[2].rctl = FC_UNSOL_CTL;
4296 /* NameServer */
4297 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
4298 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004299 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004300 pring->prt[3].profile = 0; /* Mask 3 */
4301 /* NameServer response */
4302 pring->prt[3].rctl = FC_SOL_CTL;
4303 /* NameServer */
4304 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
4305 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004306 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004307 break;
4308 }
James Smarted957682007-06-17 19:56:37 -05004309 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05004310 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05004311 }
James Smarted957682007-06-17 19:56:37 -05004312 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05004313 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04004314 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
4315 "SLI2 SLIM Data: x%x x%lx\n",
4316 phba->brd_no, totiocbsize,
4317 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05004318 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004319 if (phba->cfg_multi_ring_support == 2)
4320 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05004321
4322 return 0;
4323}
4324
James Smarte59058c2008-08-24 21:49:00 -04004325/**
James Smart3621a712009-04-06 18:47:14 -04004326 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04004327 * @phba: Pointer to HBA context object.
4328 *
4329 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
4330 * ring. This function also initializes ring indices of each ring.
4331 * This function is called during the initialization of the SLI
4332 * interface of an HBA.
4333 * This function is called with no lock held and always returns
4334 * 1.
4335 **/
dea31012005-04-17 16:05:31 -05004336int
James Smart2e0fef82007-06-17 19:56:36 -05004337lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004338{
4339 struct lpfc_sli *psli;
4340 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05004341 int i;
dea31012005-04-17 16:05:31 -05004342
4343 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05004344 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004345 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05004346 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05004347 /* Initialize list headers for txq and txcmplq as double linked lists */
4348 for (i = 0; i < psli->num_rings; i++) {
4349 pring = &psli->ring[i];
4350 pring->ringno = i;
4351 pring->next_cmdidx = 0;
4352 pring->local_getidx = 0;
4353 pring->cmdidx = 0;
4354 INIT_LIST_HEAD(&pring->txq);
4355 INIT_LIST_HEAD(&pring->txcmplq);
4356 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05004357 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05004358 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05004359 }
James Smart2e0fef82007-06-17 19:56:36 -05004360 spin_unlock_irq(&phba->hbalock);
4361 return 1;
dea31012005-04-17 16:05:31 -05004362}
4363
James Smarte59058c2008-08-24 21:49:00 -04004364/**
James Smart3621a712009-04-06 18:47:14 -04004365 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04004366 * @vport: Pointer to virtual port object.
4367 *
4368 * lpfc_sli_host_down is called to clean up the resources
4369 * associated with a vport before destroying virtual
4370 * port data structures.
4371 * This function does following operations:
4372 * - Free discovery resources associated with this virtual
4373 * port.
4374 * - Free iocbs associated with this virtual port in
4375 * the txq.
4376 * - Send abort for all iocb commands associated with this
4377 * vport in txcmplq.
4378 *
4379 * This function is called with no lock held and always returns 1.
4380 **/
dea31012005-04-17 16:05:31 -05004381int
James Smart92d7f7b2007-06-17 19:56:38 -05004382lpfc_sli_host_down(struct lpfc_vport *vport)
4383{
James Smart858c9f62007-06-17 19:56:39 -05004384 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004385 struct lpfc_hba *phba = vport->phba;
4386 struct lpfc_sli *psli = &phba->sli;
4387 struct lpfc_sli_ring *pring;
4388 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004389 int i;
4390 unsigned long flags = 0;
4391 uint16_t prev_pring_flag;
4392
4393 lpfc_cleanup_discovery_resources(vport);
4394
4395 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004396 for (i = 0; i < psli->num_rings; i++) {
4397 pring = &psli->ring[i];
4398 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04004399 /* Only slow rings */
4400 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004401 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004402 /* Set the lpfc data pending flag */
4403 set_bit(LPFC_DATA_READY, &phba->data_flags);
4404 }
James Smart92d7f7b2007-06-17 19:56:38 -05004405 /*
4406 * Error everything on the txq since these iocbs have not been
4407 * given to the FW yet.
4408 */
James Smart92d7f7b2007-06-17 19:56:38 -05004409 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4410 if (iocb->vport != vport)
4411 continue;
James Smart858c9f62007-06-17 19:56:39 -05004412 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004413 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05004414 }
4415
4416 /* Next issue ABTS for everything on the txcmplq */
4417 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
4418 list) {
4419 if (iocb->vport != vport)
4420 continue;
4421 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4422 }
4423
4424 pring->flag = prev_pring_flag;
4425 }
4426
4427 spin_unlock_irqrestore(&phba->hbalock, flags);
4428
James Smarta257bf92009-04-06 18:48:10 -04004429 /* Cancel all the IOCBs from the completions list */
4430 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4431 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05004432 return 1;
4433}
4434
James Smarte59058c2008-08-24 21:49:00 -04004435/**
James Smart3621a712009-04-06 18:47:14 -04004436 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04004437 * @phba: Pointer to HBA context object.
4438 *
4439 * This function cleans up all iocb, buffers, mailbox commands
4440 * while shutting down the HBA. This function is called with no
4441 * lock held and always returns 1.
4442 * This function does the following to cleanup driver resources:
4443 * - Free discovery resources for each virtual port
4444 * - Cleanup any pending fabric iocbs
4445 * - Iterate through the iocb txq and free each entry
4446 * in the list.
4447 * - Free up any buffer posted to the HBA
4448 * - Free mailbox commands in the mailbox queue.
4449 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004450int
James Smart2e0fef82007-06-17 19:56:36 -05004451lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004452{
James Smart2534ba72007-04-25 09:52:20 -04004453 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05004454 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004455 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05004456 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05004457 LPFC_MBOXQ_t *pmb;
dea31012005-04-17 16:05:31 -05004458 int i;
4459 unsigned long flags = 0;
4460
dea31012005-04-17 16:05:31 -05004461 lpfc_hba_down_prep(phba);
4462
James Smart92d7f7b2007-06-17 19:56:38 -05004463 lpfc_fabric_abort_hba(phba);
4464
James Smart2e0fef82007-06-17 19:56:36 -05004465 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004466 for (i = 0; i < psli->num_rings; i++) {
4467 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04004468 /* Only slow rings */
4469 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004470 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004471 /* Set the lpfc data pending flag */
4472 set_bit(LPFC_DATA_READY, &phba->data_flags);
4473 }
dea31012005-04-17 16:05:31 -05004474
4475 /*
4476 * Error everything on the txq since these iocbs have not been
4477 * given to the FW yet.
4478 */
James Smart2534ba72007-04-25 09:52:20 -04004479 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05004480 pring->txq_cnt = 0;
4481
dea31012005-04-17 16:05:31 -05004482 }
James Smart2e0fef82007-06-17 19:56:36 -05004483 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004484
James Smarta257bf92009-04-06 18:48:10 -04004485 /* Cancel all the IOCBs from the completions list */
4486 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4487 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04004488
James Smart0ff10d42008-01-11 01:52:36 -05004489 spin_lock_irqsave(&phba->hbalock, flags);
4490 list_splice_init(&phba->elsbuf, &completions);
4491 phba->elsbuf_cnt = 0;
4492 phba->elsbuf_prev_cnt = 0;
4493 spin_unlock_irqrestore(&phba->hbalock, flags);
4494
4495 while (!list_empty(&completions)) {
4496 list_remove_head(&completions, buf_ptr,
4497 struct lpfc_dmabuf, list);
4498 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4499 kfree(buf_ptr);
4500 }
4501
dea31012005-04-17 16:05:31 -05004502 /* Return any active mbox cmds */
4503 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05004504 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004505
4506 spin_lock(&phba->pport->work_port_lock);
4507 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4508 spin_unlock(&phba->pport->work_port_lock);
4509
James Smart97eab632008-04-07 10:16:05 -04004510 /* Return any pending or completed mbox cmds */
4511 list_splice_init(&phba->sli.mboxq, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004512 if (psli->mbox_active) {
4513 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05004514 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004515 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05004516 }
James Smart92d7f7b2007-06-17 19:56:38 -05004517 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004518 spin_unlock_irqrestore(&phba->hbalock, flags);
4519
4520 while (!list_empty(&completions)) {
4521 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05004522 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
James Smart97eab632008-04-07 10:16:05 -04004523 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05004524 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05004525 }
dea31012005-04-17 16:05:31 -05004526 return 1;
4527}
4528
James Smarte59058c2008-08-24 21:49:00 -04004529/**
James Smart3621a712009-04-06 18:47:14 -04004530 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04004531 * @srcp: Source memory pointer.
4532 * @destp: Destination memory pointer.
4533 * @cnt: Number of words required to be copied.
4534 *
4535 * This function is used for copying data between driver memory
4536 * and the SLI memory. This function also changes the endianness
4537 * of each word if native endianness is different from SLI
4538 * endianness. This function can be called with or without
4539 * lock.
4540 **/
dea31012005-04-17 16:05:31 -05004541void
4542lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
4543{
4544 uint32_t *src = srcp;
4545 uint32_t *dest = destp;
4546 uint32_t ldata;
4547 int i;
4548
4549 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
4550 ldata = *src;
4551 ldata = le32_to_cpu(ldata);
4552 *dest = ldata;
4553 src++;
4554 dest++;
4555 }
4556}
4557
James Smarte59058c2008-08-24 21:49:00 -04004558
4559/**
James Smart3621a712009-04-06 18:47:14 -04004560 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04004561 * @phba: Pointer to HBA context object.
4562 * @pring: Pointer to driver SLI ring object.
4563 * @mp: Pointer to driver buffer object.
4564 *
4565 * This function is called with no lock held.
4566 * It always return zero after adding the buffer to the postbufq
4567 * buffer list.
4568 **/
dea31012005-04-17 16:05:31 -05004569int
James Smart2e0fef82007-06-17 19:56:36 -05004570lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4571 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05004572{
4573 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
4574 later */
James Smart2e0fef82007-06-17 19:56:36 -05004575 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004576 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05004577 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05004578 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004579 return 0;
4580}
4581
James Smarte59058c2008-08-24 21:49:00 -04004582/**
James Smart3621a712009-04-06 18:47:14 -04004583 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04004584 * @phba: Pointer to HBA context object.
4585 *
4586 * When HBQ is enabled, buffers are searched based on tags. This function
4587 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
4588 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
4589 * does not conflict with tags of buffer posted for unsolicited events.
4590 * The function returns the allocated tag. The function is called with
4591 * no locks held.
4592 **/
James Smart76bb24e2007-10-27 13:38:00 -04004593uint32_t
4594lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
4595{
4596 spin_lock_irq(&phba->hbalock);
4597 phba->buffer_tag_count++;
4598 /*
4599 * Always set the QUE_BUFTAG_BIT to distiguish between
4600 * a tag assigned by HBQ.
4601 */
4602 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
4603 spin_unlock_irq(&phba->hbalock);
4604 return phba->buffer_tag_count;
4605}
4606
James Smarte59058c2008-08-24 21:49:00 -04004607/**
James Smart3621a712009-04-06 18:47:14 -04004608 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04004609 * @phba: Pointer to HBA context object.
4610 * @pring: Pointer to driver SLI ring object.
4611 * @tag: Buffer tag.
4612 *
4613 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
4614 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
4615 * iocb is posted to the response ring with the tag of the buffer.
4616 * This function searches the pring->postbufq list using the tag
4617 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
4618 * iocb. If the buffer is found then lpfc_dmabuf object of the
4619 * buffer is returned to the caller else NULL is returned.
4620 * This function is called with no lock held.
4621 **/
James Smart76bb24e2007-10-27 13:38:00 -04004622struct lpfc_dmabuf *
4623lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4624 uint32_t tag)
4625{
4626 struct lpfc_dmabuf *mp, *next_mp;
4627 struct list_head *slp = &pring->postbufq;
4628
4629 /* Search postbufq, from the begining, looking for a match on tag */
4630 spin_lock_irq(&phba->hbalock);
4631 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4632 if (mp->buffer_tag == tag) {
4633 list_del_init(&mp->list);
4634 pring->postbufq_cnt--;
4635 spin_unlock_irq(&phba->hbalock);
4636 return mp;
4637 }
4638 }
4639
4640 spin_unlock_irq(&phba->hbalock);
4641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04004642 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04004643 "ring %d Data x%lx x%p x%p x%x\n",
4644 pring->ringno, (unsigned long) tag,
4645 slp->next, slp->prev, pring->postbufq_cnt);
4646
4647 return NULL;
4648}
dea31012005-04-17 16:05:31 -05004649
James Smarte59058c2008-08-24 21:49:00 -04004650/**
James Smart3621a712009-04-06 18:47:14 -04004651 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04004652 * @phba: Pointer to HBA context object.
4653 * @pring: Pointer to driver SLI ring object.
4654 * @phys: DMA address of the buffer.
4655 *
4656 * This function searches the buffer list using the dma_address
4657 * of unsolicited event to find the driver's lpfc_dmabuf object
4658 * corresponding to the dma_address. The function returns the
4659 * lpfc_dmabuf object if a buffer is found else it returns NULL.
4660 * This function is called by the ct and els unsolicited event
4661 * handlers to get the buffer associated with the unsolicited
4662 * event.
4663 *
4664 * This function is called with no lock held.
4665 **/
dea31012005-04-17 16:05:31 -05004666struct lpfc_dmabuf *
4667lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4668 dma_addr_t phys)
4669{
4670 struct lpfc_dmabuf *mp, *next_mp;
4671 struct list_head *slp = &pring->postbufq;
4672
4673 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05004674 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004675 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4676 if (mp->phys == phys) {
4677 list_del_init(&mp->list);
4678 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004679 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004680 return mp;
4681 }
4682 }
4683
James Smart2e0fef82007-06-17 19:56:36 -05004684 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004685 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004686 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05004687 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004688 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05004689 slp->next, slp->prev, pring->postbufq_cnt);
4690 return NULL;
4691}
4692
James Smarte59058c2008-08-24 21:49:00 -04004693/**
James Smart3621a712009-04-06 18:47:14 -04004694 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04004695 * @phba: Pointer to HBA context object.
4696 * @cmdiocb: Pointer to driver command iocb object.
4697 * @rspiocb: Pointer to driver response iocb object.
4698 *
4699 * This function is the completion handler for the abort iocbs for
4700 * ELS commands. This function is called from the ELS ring event
4701 * handler with no lock held. This function frees memory resources
4702 * associated with the abort iocb.
4703 **/
dea31012005-04-17 16:05:31 -05004704static void
James Smart2e0fef82007-06-17 19:56:36 -05004705lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4706 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05004707{
James Smart2e0fef82007-06-17 19:56:36 -05004708 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04004709 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05004710 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04004711 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4712
4713 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04004714
4715 if (irsp->ulpStatus) {
4716 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
4717 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
4718
James Smart2e0fef82007-06-17 19:56:36 -05004719 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004720 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
4721 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
4722
James Smart92d7f7b2007-06-17 19:56:38 -05004723 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004724 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05004725 "with tag %x context %x, abort status %x, "
4726 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04004727 abort_iocb, abort_iotag, abort_context,
4728 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04004729
4730 /*
James Smart58da1ff2008-04-07 10:15:56 -04004731 * If the iocb is not found in Firmware queue the iocb
4732 * might have completed already. Do not free it again.
4733 */
James Smart9b379602008-04-07 10:16:00 -04004734 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04004735 spin_unlock_irq(&phba->hbalock);
4736 lpfc_sli_release_iocbq(phba, cmdiocb);
4737 return;
4738 }
4739 /*
James Smart2680eea2007-04-25 09:52:55 -04004740 * make sure we have the right iocbq before taking it
4741 * off the txcmplq and try to call completion routine.
4742 */
James Smart2e0fef82007-06-17 19:56:36 -05004743 if (!abort_iocb ||
4744 abort_iocb->iocb.ulpContext != abort_context ||
4745 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
4746 spin_unlock_irq(&phba->hbalock);
4747 else {
James Smart92d7f7b2007-06-17 19:56:38 -05004748 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04004749 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004750 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004751
James Smart0ff10d42008-01-11 01:52:36 -05004752 /* Firmware could still be in progress of DMAing
4753 * payload, so don't free data buffer till after
4754 * a hbeat.
4755 */
4756 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
4757
James Smart92d7f7b2007-06-17 19:56:38 -05004758 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4759 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4760 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
4761 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04004762 }
4763 }
4764
James Bottomley604a3e32005-10-29 10:28:33 -05004765 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05004766 return;
4767}
4768
James Smarte59058c2008-08-24 21:49:00 -04004769/**
James Smart3621a712009-04-06 18:47:14 -04004770 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04004771 * @phba: Pointer to HBA context object.
4772 * @cmdiocb: Pointer to driver command iocb object.
4773 * @rspiocb: Pointer to driver response iocb object.
4774 *
4775 * The function is called from SLI ring event handler with no
4776 * lock held. This function is the completion handler for ELS commands
4777 * which are aborted. The function frees memory resources used for
4778 * the aborted ELS commands.
4779 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004780static void
4781lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4782 struct lpfc_iocbq *rspiocb)
4783{
4784 IOCB_t *irsp = &rspiocb->iocb;
4785
4786 /* ELS cmd tag <ulpIoTag> completes */
4787 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04004788 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004789 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004790 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05004791 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05004792 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
4793 lpfc_ct_free_iocb(phba, cmdiocb);
4794 else
4795 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05004796 return;
4797}
4798
James Smarte59058c2008-08-24 21:49:00 -04004799/**
James Smart3621a712009-04-06 18:47:14 -04004800 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04004801 * @phba: Pointer to HBA context object.
4802 * @pring: Pointer to driver SLI ring object.
4803 * @cmdiocb: Pointer to driver command iocb object.
4804 *
4805 * This function issues an abort iocb for the provided command
4806 * iocb. This function is called with hbalock held.
4807 * The function returns 0 when it fails due to memory allocation
4808 * failure or when the command iocb is an abort request.
4809 **/
dea31012005-04-17 16:05:31 -05004810int
James Smart2e0fef82007-06-17 19:56:36 -05004811lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4812 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05004813{
James Smart2e0fef82007-06-17 19:56:36 -05004814 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004815 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05004816 IOCB_t *icmd = NULL;
4817 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04004818 int retval = IOCB_ERROR;
4819
James Smart92d7f7b2007-06-17 19:56:38 -05004820 /*
4821 * There are certain command types we don't want to abort. And we
4822 * don't want to abort commands that are already in the process of
4823 * being aborted.
James Smart07951072007-04-25 09:51:38 -04004824 */
4825 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004826 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05004827 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4828 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04004829 return 0;
4830
James Smart858c9f62007-06-17 19:56:39 -05004831 /* If we're unloading, don't abort iocb on the ELS ring, but change the
4832 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04004833 */
James Smart858c9f62007-06-17 19:56:39 -05004834 if ((vport->load_flag & FC_UNLOADING) &&
4835 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004836 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
4837 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
4838 else
4839 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04004840 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05004841 }
dea31012005-04-17 16:05:31 -05004842
4843 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05004844 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004845 if (abtsiocbp == NULL)
4846 return 0;
dea31012005-04-17 16:05:31 -05004847
James Smart07951072007-04-25 09:51:38 -04004848 /* This signals the response to set the correct status
4849 * before calling the completion handler.
4850 */
4851 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4852
dea31012005-04-17 16:05:31 -05004853 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04004854 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
4855 iabt->un.acxri.abortContextTag = icmd->ulpContext;
4856 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004857 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04004858 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05004859
James Smart2e0fef82007-06-17 19:56:36 -05004860 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04004861 iabt->ulpCommand = CMD_ABORT_XRI_CN;
4862 else
4863 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
4864
4865 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04004866
James Smarte8b62012007-08-02 11:10:09 -04004867 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4868 "0339 Abort xri x%x, original iotag x%x, "
4869 "abort cmd iotag x%x\n",
4870 iabt->un.acxri.abortContextTag,
4871 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05004872 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04004873
James Smartd7c255b2008-08-24 21:50:00 -04004874 if (retval)
4875 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04004876abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05004877 /*
4878 * Caller to this routine should check for IOCB_ERROR
4879 * and handle it properly. This routine no longer removes
4880 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04004881 */
James Smart2e0fef82007-06-17 19:56:36 -05004882 return retval;
dea31012005-04-17 16:05:31 -05004883}
4884
James Smarte59058c2008-08-24 21:49:00 -04004885/**
James Smart3621a712009-04-06 18:47:14 -04004886 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04004887 * @iocbq: Pointer to driver iocb object.
4888 * @vport: Pointer to driver virtual port object.
4889 * @tgt_id: SCSI ID of the target.
4890 * @lun_id: LUN ID of the scsi device.
4891 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
4892 *
James Smart3621a712009-04-06 18:47:14 -04004893 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04004894 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
4895 * 0 if the filtering criteria is met for the given iocb and will return
4896 * 1 if the filtering criteria is not met.
4897 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
4898 * given iocb is for the SCSI device specified by vport, tgt_id and
4899 * lun_id parameter.
4900 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
4901 * given iocb is for the SCSI target specified by vport and tgt_id
4902 * parameters.
4903 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
4904 * given iocb is for the SCSI host associated with the given vport.
4905 * This function is called with no locks held.
4906 **/
dea31012005-04-17 16:05:31 -05004907static int
James Smart51ef4c22007-08-02 11:10:31 -04004908lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
4909 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004910 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004911{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004912 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004913 int rc = 1;
4914
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004915 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
4916 return rc;
4917
James Smart51ef4c22007-08-02 11:10:31 -04004918 if (iocbq->vport != vport)
4919 return rc;
4920
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004921 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004922
James Smart495a7142008-06-14 22:52:59 -04004923 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05004924 return rc;
4925
4926 switch (ctx_cmd) {
4927 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04004928 if ((lpfc_cmd->rdata->pnode) &&
4929 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
4930 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05004931 rc = 0;
4932 break;
4933 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04004934 if ((lpfc_cmd->rdata->pnode) &&
4935 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05004936 rc = 0;
4937 break;
dea31012005-04-17 16:05:31 -05004938 case LPFC_CTX_HOST:
4939 rc = 0;
4940 break;
4941 default:
4942 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004943 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05004944 break;
4945 }
4946
4947 return rc;
4948}
4949
James Smarte59058c2008-08-24 21:49:00 -04004950/**
James Smart3621a712009-04-06 18:47:14 -04004951 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04004952 * @vport: Pointer to virtual port.
4953 * @tgt_id: SCSI ID of the target.
4954 * @lun_id: LUN ID of the scsi device.
4955 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4956 *
4957 * This function returns number of FCP commands pending for the vport.
4958 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
4959 * commands pending on the vport associated with SCSI device specified
4960 * by tgt_id and lun_id parameters.
4961 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
4962 * commands pending on the vport associated with SCSI target specified
4963 * by tgt_id parameter.
4964 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
4965 * commands pending on the vport.
4966 * This function returns the number of iocbs which satisfy the filter.
4967 * This function is called without any lock held.
4968 **/
dea31012005-04-17 16:05:31 -05004969int
James Smart51ef4c22007-08-02 11:10:31 -04004970lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
4971 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004972{
James Smart51ef4c22007-08-02 11:10:31 -04004973 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004974 struct lpfc_iocbq *iocbq;
4975 int sum, i;
dea31012005-04-17 16:05:31 -05004976
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004977 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
4978 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004979
James Smart51ef4c22007-08-02 11:10:31 -04004980 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
4981 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004982 sum++;
dea31012005-04-17 16:05:31 -05004983 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004984
dea31012005-04-17 16:05:31 -05004985 return sum;
4986}
4987
James Smarte59058c2008-08-24 21:49:00 -04004988/**
James Smart3621a712009-04-06 18:47:14 -04004989 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04004990 * @phba: Pointer to HBA context object
4991 * @cmdiocb: Pointer to command iocb object.
4992 * @rspiocb: Pointer to response iocb object.
4993 *
4994 * This function is called when an aborted FCP iocb completes. This
4995 * function is called by the ring event handler with no lock held.
4996 * This function frees the iocb.
4997 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004998void
James Smart2e0fef82007-06-17 19:56:36 -05004999lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5000 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04005001{
James Bottomley604a3e32005-10-29 10:28:33 -05005002 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04005003 return;
5004}
5005
James Smarte59058c2008-08-24 21:49:00 -04005006/**
James Smart3621a712009-04-06 18:47:14 -04005007 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04005008 * @vport: Pointer to virtual port.
5009 * @pring: Pointer to driver SLI ring object.
5010 * @tgt_id: SCSI ID of the target.
5011 * @lun_id: LUN ID of the scsi device.
5012 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
5013 *
5014 * This function sends an abort command for every SCSI command
5015 * associated with the given virtual port pending on the ring
5016 * filtered by lpfc_sli_validate_fcp_iocb function.
5017 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
5018 * FCP iocbs associated with lun specified by tgt_id and lun_id
5019 * parameters
5020 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
5021 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
5022 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
5023 * FCP iocbs associated with virtual port.
5024 * This function returns number of iocbs it failed to abort.
5025 * This function is called with no locks held.
5026 **/
dea31012005-04-17 16:05:31 -05005027int
James Smart51ef4c22007-08-02 11:10:31 -04005028lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
5029 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05005030{
James Smart51ef4c22007-08-02 11:10:31 -04005031 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005032 struct lpfc_iocbq *iocbq;
5033 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05005034 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05005035 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005036 int i;
dea31012005-04-17 16:05:31 -05005037
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005038 for (i = 1; i <= phba->sli.last_iotag; i++) {
5039 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05005040
James Smart51ef4c22007-08-02 11:10:31 -04005041 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05005042 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05005043 continue;
5044
5045 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005046 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05005047 if (abtsiocb == NULL) {
5048 errcnt++;
5049 continue;
5050 }
dea31012005-04-17 16:05:31 -05005051
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005052 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05005053 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
5054 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
5055 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
5056 abtsiocb->iocb.ulpLe = 1;
5057 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05005058 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05005059
James Smart2e0fef82007-06-17 19:56:36 -05005060 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005061 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
5062 else
5063 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
5064
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04005065 /* Setup callback routine and issue the command. */
5066 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05005067 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
5068 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05005069 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05005070 errcnt++;
5071 continue;
5072 }
5073 }
5074
5075 return errcnt;
5076}
5077
James Smarte59058c2008-08-24 21:49:00 -04005078/**
James Smart3621a712009-04-06 18:47:14 -04005079 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04005080 * @phba: Pointer to HBA context object.
5081 * @cmdiocbq: Pointer to command iocb.
5082 * @rspiocbq: Pointer to response iocb.
5083 *
5084 * This function is the completion handler for iocbs issued using
5085 * lpfc_sli_issue_iocb_wait function. This function is called by the
5086 * ring event handler function without any lock held. This function
5087 * can be called from both worker thread context and interrupt
5088 * context. This function also can be called from other thread which
5089 * cleans up the SLI layer objects.
5090 * This function copy the contents of the response iocb to the
5091 * response iocb memory object provided by the caller of
5092 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
5093 * sleeps for the iocb completion.
5094 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005095static void
5096lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
5097 struct lpfc_iocbq *cmdiocbq,
5098 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05005099{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005100 wait_queue_head_t *pdone_q;
5101 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005102
James Smart2e0fef82007-06-17 19:56:36 -05005103 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005104 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
5105 if (cmdiocbq->context2 && rspiocbq)
5106 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
5107 &rspiocbq->iocb, sizeof(IOCB_t));
5108
5109 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005110 if (pdone_q)
5111 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05005112 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05005113 return;
5114}
5115
James Smarte59058c2008-08-24 21:49:00 -04005116/**
James Smart3621a712009-04-06 18:47:14 -04005117 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04005118 * @phba: Pointer to HBA context object..
5119 * @pring: Pointer to sli ring.
5120 * @piocb: Pointer to command iocb.
5121 * @prspiocbq: Pointer to response iocb.
5122 * @timeout: Timeout in number of seconds.
5123 *
5124 * This function issues the iocb to firmware and waits for the
5125 * iocb to complete. If the iocb command is not
5126 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
5127 * Caller should not free the iocb resources if this function
5128 * returns IOCB_TIMEDOUT.
5129 * The function waits for the iocb completion using an
5130 * non-interruptible wait.
5131 * This function will sleep while waiting for iocb completion.
5132 * So, this function should not be called from any context which
5133 * does not allow sleeping. Due to the same reason, this function
5134 * cannot be called with interrupt disabled.
5135 * This function assumes that the iocb completions occur while
5136 * this function sleep. So, this function cannot be called from
5137 * the thread which process iocb completion for this ring.
5138 * This function clears the iocb_flag of the iocb object before
5139 * issuing the iocb and the iocb completion handler sets this
5140 * flag and wakes this thread when the iocb completes.
5141 * The contents of the response iocb will be copied to prspiocbq
5142 * by the completion handler when the command completes.
5143 * This function returns IOCB_SUCCESS when success.
5144 * This function is called with no lock held.
5145 **/
dea31012005-04-17 16:05:31 -05005146int
James Smart2e0fef82007-06-17 19:56:36 -05005147lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
5148 struct lpfc_sli_ring *pring,
5149 struct lpfc_iocbq *piocb,
5150 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005151 uint32_t timeout)
dea31012005-04-17 16:05:31 -05005152{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08005153 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005154 long timeleft, timeout_req = 0;
5155 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005156 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05005157
5158 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005159 * If the caller has provided a response iocbq buffer, then context2
5160 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05005161 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005162 if (prspiocbq) {
5163 if (piocb->context2)
5164 return IOCB_ERROR;
5165 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05005166 }
5167
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005168 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
5169 piocb->context_un.wait_queue = &done_q;
5170 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05005171
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005172 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5173 creg_val = readl(phba->HCregaddr);
5174 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
5175 writel(creg_val, phba->HCregaddr);
5176 readl(phba->HCregaddr); /* flush */
5177 }
5178
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005179 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
5180 if (retval == IOCB_SUCCESS) {
5181 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005182 timeleft = wait_event_timeout(done_q,
5183 piocb->iocb_flag & LPFC_IO_WAKE,
5184 timeout_req);
dea31012005-04-17 16:05:31 -05005185
James Smart7054a602007-04-25 09:52:34 -04005186 if (piocb->iocb_flag & LPFC_IO_WAKE) {
5187 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005188 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04005189 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005190 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005191 "0338 IOCB wait timeout error - no "
5192 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005193 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04005194 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005195 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005196 "0330 IOCB wake NOT set, "
5197 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005198 timeout, (timeleft / jiffies));
5199 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05005200 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005201 } else {
5202 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04005203 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005204 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005205 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05005206 }
5207
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005208 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5209 creg_val = readl(phba->HCregaddr);
5210 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
5211 writel(creg_val, phba->HCregaddr);
5212 readl(phba->HCregaddr); /* flush */
5213 }
5214
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005215 if (prspiocbq)
5216 piocb->context2 = NULL;
5217
5218 piocb->context_un.wait_queue = NULL;
5219 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05005220 return retval;
5221}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005222
James Smarte59058c2008-08-24 21:49:00 -04005223/**
James Smart3621a712009-04-06 18:47:14 -04005224 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04005225 * @phba: Pointer to HBA context object.
5226 * @pmboxq: Pointer to driver mailbox object.
5227 * @timeout: Timeout in number of seconds.
5228 *
5229 * This function issues the mailbox to firmware and waits for the
5230 * mailbox command to complete. If the mailbox command is not
5231 * completed within timeout seconds, it returns MBX_TIMEOUT.
5232 * The function waits for the mailbox completion using an
5233 * interruptible wait. If the thread is woken up due to a
5234 * signal, MBX_TIMEOUT error is returned to the caller. Caller
5235 * should not free the mailbox resources, if this function returns
5236 * MBX_TIMEOUT.
5237 * This function will sleep while waiting for mailbox completion.
5238 * So, this function should not be called from any context which
5239 * does not allow sleeping. Due to the same reason, this function
5240 * cannot be called with interrupt disabled.
5241 * This function assumes that the mailbox completion occurs while
5242 * this function sleep. So, this function cannot be called from
5243 * the worker thread which processes mailbox completion.
5244 * This function is called in the context of HBA management
5245 * applications.
5246 * This function returns MBX_SUCCESS when successful.
5247 * This function is called with no lock held.
5248 **/
dea31012005-04-17 16:05:31 -05005249int
James Smart2e0fef82007-06-17 19:56:36 -05005250lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05005251 uint32_t timeout)
5252{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08005253 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05005254 int retval;
James Smart858c9f62007-06-17 19:56:39 -05005255 unsigned long flag;
dea31012005-04-17 16:05:31 -05005256
5257 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04005258 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05005259 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005260
James Smart495a7142008-06-14 22:52:59 -04005261 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05005262 /* setup wake call as IOCB callback */
5263 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
5264 /* setup context field to pass wait_queue pointer to wake function */
5265 pmboxq->context1 = &done_q;
5266
dea31012005-04-17 16:05:31 -05005267 /* now issue the command */
5268 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
5269
5270 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04005271 wait_event_interruptible_timeout(done_q,
5272 pmboxq->mbox_flag & LPFC_MBX_WAKE,
5273 timeout * HZ);
5274
James Smart858c9f62007-06-17 19:56:39 -05005275 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005276 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04005277 /*
5278 * if LPFC_MBX_WAKE flag is set the mailbox is completed
5279 * else do not free the resources.
5280 */
5281 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05005282 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05005283 else {
James Smart7054a602007-04-25 09:52:34 -04005284 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05005285 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5286 }
5287 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005288 }
5289
dea31012005-04-17 16:05:31 -05005290 return retval;
5291}
5292
James Smarte59058c2008-08-24 21:49:00 -04005293/**
James Smart3772a992009-05-22 14:50:54 -04005294 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04005295 * @phba: Pointer to HBA context.
5296 *
James Smart3772a992009-05-22 14:50:54 -04005297 * This function is called to shutdown the driver's mailbox sub-system.
5298 * It first marks the mailbox sub-system is in a block state to prevent
5299 * the asynchronous mailbox command from issued off the pending mailbox
5300 * command queue. If the mailbox command sub-system shutdown is due to
5301 * HBA error conditions such as EEH or ERATT, this routine shall invoke
5302 * the mailbox sub-system flush routine to forcefully bring down the
5303 * mailbox sub-system. Otherwise, if it is due to normal condition (such
5304 * as with offline or HBA function reset), this routine will wait for the
5305 * outstanding mailbox command to complete before invoking the mailbox
5306 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04005307 **/
James Smart3772a992009-05-22 14:50:54 -04005308void
5309lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04005310{
James Smart3772a992009-05-22 14:50:54 -04005311 struct lpfc_sli *psli = &phba->sli;
5312 uint8_t actcmd = MBX_HEARTBEAT;
5313 unsigned long timeout;
5314
5315 spin_lock_irq(&phba->hbalock);
5316 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5317 spin_unlock_irq(&phba->hbalock);
5318
5319 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5320 spin_lock_irq(&phba->hbalock);
5321 if (phba->sli.mbox_active)
5322 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5323 spin_unlock_irq(&phba->hbalock);
5324 /* Determine how long we might wait for the active mailbox
5325 * command to be gracefully completed by firmware.
5326 */
5327 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
5328 1000) + jiffies;
5329 while (phba->sli.mbox_active) {
5330 /* Check active mailbox complete status every 2ms */
5331 msleep(2);
5332 if (time_after(jiffies, timeout))
5333 /* Timeout, let the mailbox flush routine to
5334 * forcefully release active mailbox command
5335 */
5336 break;
5337 }
5338 }
5339 lpfc_sli_mbox_sys_flush(phba);
5340}
5341
5342/**
5343 * lpfc_sli_eratt_read - read sli-3 error attention events
5344 * @phba: Pointer to HBA context.
5345 *
5346 * This function is called to read the SLI3 device error attention registers
5347 * for possible error attention events. The caller must hold the hostlock
5348 * with spin_lock_irq().
5349 *
5350 * This fucntion returns 1 when there is Error Attention in the Host Attention
5351 * Register and returns 0 otherwise.
5352 **/
5353static int
5354lpfc_sli_eratt_read(struct lpfc_hba *phba)
5355{
James Smarted957682007-06-17 19:56:37 -05005356 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04005357
James Smart3772a992009-05-22 14:50:54 -04005358 /* Read chip Host Attention (HA) register */
5359 ha_copy = readl(phba->HAregaddr);
5360 if (ha_copy & HA_ERATT) {
5361 /* Read host status register to retrieve error event */
5362 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04005363
James Smart3772a992009-05-22 14:50:54 -04005364 /* Check if there is a deferred error condition is active */
5365 if ((HS_FFER1 & phba->work_hs) &&
5366 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
5367 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
5368 spin_lock_irq(&phba->hbalock);
5369 phba->hba_flag |= DEFER_ERATT;
5370 spin_unlock_irq(&phba->hbalock);
5371 /* Clear all interrupt enable conditions */
5372 writel(0, phba->HCregaddr);
5373 readl(phba->HCregaddr);
5374 }
5375
5376 /* Set the driver HA work bitmap */
James Smarted957682007-06-17 19:56:37 -05005377 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04005378 phba->work_ha |= HA_ERATT;
5379 /* Indicate polling handles this ERATT */
5380 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smarted957682007-06-17 19:56:37 -05005381 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04005382 return 1;
James Smartb4c02652006-07-06 15:50:43 -04005383 }
James Smart3772a992009-05-22 14:50:54 -04005384 return 0;
James Smartb4c02652006-07-06 15:50:43 -04005385}
5386
James Smarte59058c2008-08-24 21:49:00 -04005387/**
James Smart3621a712009-04-06 18:47:14 -04005388 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04005389 * @phba: Pointer to HBA context.
5390 *
James Smart3772a992009-05-22 14:50:54 -04005391 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04005392 * error attention register bit for error attention events.
5393 *
5394 * This fucntion returns 1 when there is Error Attention in the Host Attention
5395 * Register and returns 0 otherwise.
5396 **/
5397int
5398lpfc_sli_check_eratt(struct lpfc_hba *phba)
5399{
5400 uint32_t ha_copy;
5401
5402 /* If somebody is waiting to handle an eratt, don't process it
5403 * here. The brdkill function will do this.
5404 */
5405 if (phba->link_flag & LS_IGNORE_ERATT)
5406 return 0;
5407
5408 /* Check if interrupt handler handles this ERATT */
5409 spin_lock_irq(&phba->hbalock);
5410 if (phba->hba_flag & HBA_ERATT_HANDLED) {
5411 /* Interrupt handler has handled ERATT */
5412 spin_unlock_irq(&phba->hbalock);
5413 return 0;
5414 }
5415
James Smarta257bf92009-04-06 18:48:10 -04005416 /*
5417 * If there is deferred error attention, do not check for error
5418 * attention
5419 */
5420 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5421 spin_unlock_irq(&phba->hbalock);
5422 return 0;
5423 }
5424
James Smart3772a992009-05-22 14:50:54 -04005425 /* If PCI channel is offline, don't process it */
5426 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04005427 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04005428 return 0;
5429 }
5430
5431 switch (phba->sli_rev) {
5432 case LPFC_SLI_REV2:
5433 case LPFC_SLI_REV3:
5434 /* Read chip Host Attention (HA) register */
5435 ha_copy = lpfc_sli_eratt_read(phba);
5436 break;
5437 default:
5438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5439 "0299 Invalid SLI revision (%d)\n",
5440 phba->sli_rev);
5441 ha_copy = 0;
5442 break;
James Smart93996272008-08-24 21:50:30 -04005443 }
5444 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04005445
5446 return ha_copy;
5447}
5448
5449/**
5450 * lpfc_intr_state_check - Check device state for interrupt handling
5451 * @phba: Pointer to HBA context.
5452 *
5453 * This inline routine checks whether a device or its PCI slot is in a state
5454 * that the interrupt should be handled.
5455 *
5456 * This function returns 0 if the device or the PCI slot is in a state that
5457 * interrupt should be handled, otherwise -EIO.
5458 */
5459static inline int
5460lpfc_intr_state_check(struct lpfc_hba *phba)
5461{
5462 /* If the pci channel is offline, ignore all the interrupts */
5463 if (unlikely(pci_channel_offline(phba->pcidev)))
5464 return -EIO;
5465
5466 /* Update device level interrupt statistics */
5467 phba->sli.slistat.sli_intr++;
5468
5469 /* Ignore all interrupts during initialization. */
5470 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5471 return -EIO;
5472
James Smart93996272008-08-24 21:50:30 -04005473 return 0;
5474}
5475
5476/**
James Smart3772a992009-05-22 14:50:54 -04005477 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04005478 * @irq: Interrupt number.
5479 * @dev_id: The device context pointer.
5480 *
James Smart93996272008-08-24 21:50:30 -04005481 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04005482 * service routine when device with SLI-3 interface spec is enabled with
5483 * MSI-X multi-message interrupt mode and there are slow-path events in
5484 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
5485 * interrupt mode, this function is called as part of the device-level
5486 * interrupt handler. When the PCI slot is in error recovery or the HBA
5487 * is undergoing initialization, the interrupt handler will not process
5488 * the interrupt. The link attention and ELS ring attention events are
5489 * handled by the worker thread. The interrupt handler signals the worker
5490 * thread and returns for these events. This function is called without
5491 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04005492 * structures.
5493 *
5494 * This function returns IRQ_HANDLED when interrupt is handled else it
5495 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04005496 **/
dea31012005-04-17 16:05:31 -05005497irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04005498lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05005499{
James Smart2e0fef82007-06-17 19:56:36 -05005500 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05005501 uint32_t ha_copy;
5502 uint32_t work_ha_copy;
5503 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05005504 unsigned long iflag;
dea31012005-04-17 16:05:31 -05005505 uint32_t control;
5506
James Smart92d7f7b2007-06-17 19:56:38 -05005507 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05005508 struct lpfc_vport *vport;
5509 struct lpfc_nodelist *ndlp;
5510 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05005511 LPFC_MBOXQ_t *pmb;
5512 int rc;
5513
dea31012005-04-17 16:05:31 -05005514 /*
5515 * Get the driver's phba structure from the dev_id and
5516 * assume the HBA is not interrupting.
5517 */
James Smart93996272008-08-24 21:50:30 -04005518 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05005519
5520 if (unlikely(!phba))
5521 return IRQ_NONE;
5522
dea31012005-04-17 16:05:31 -05005523 /*
James Smart93996272008-08-24 21:50:30 -04005524 * Stuff needs to be attented to when this function is invoked as an
5525 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005526 */
James Smart93996272008-08-24 21:50:30 -04005527 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04005528 /* Check device state for handling interrupt */
5529 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04005530 return IRQ_NONE;
5531 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05005532 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04005533 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04005534 /* If somebody is waiting to handle an eratt don't process it
5535 * here. The brdkill function will do this.
5536 */
5537 if (phba->link_flag & LS_IGNORE_ERATT)
5538 ha_copy &= ~HA_ERATT;
5539 /* Check the need for handling ERATT in interrupt handler */
5540 if (ha_copy & HA_ERATT) {
5541 if (phba->hba_flag & HBA_ERATT_HANDLED)
5542 /* ERATT polling has handled ERATT */
5543 ha_copy &= ~HA_ERATT;
5544 else
5545 /* Indicate interrupt handler handles ERATT */
5546 phba->hba_flag |= HBA_ERATT_HANDLED;
5547 }
James Smarta257bf92009-04-06 18:48:10 -04005548
5549 /*
5550 * If there is deferred error attention, do not check for any
5551 * interrupt.
5552 */
5553 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04005554 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04005555 return IRQ_NONE;
5556 }
5557
James Smart93996272008-08-24 21:50:30 -04005558 /* Clear up only attention source related to slow-path */
5559 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
5560 phba->HAregaddr);
5561 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005562 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005563 } else
5564 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05005565
dea31012005-04-17 16:05:31 -05005566 work_ha_copy = ha_copy & phba->work_ha_mask;
5567
James Smart93996272008-08-24 21:50:30 -04005568 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05005569 if (work_ha_copy & HA_LATT) {
5570 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
5571 /*
5572 * Turn off Link Attention interrupts
5573 * until CLEAR_LA done
5574 */
James Smart5b75da22008-12-04 22:39:35 -05005575 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005576 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
5577 control = readl(phba->HCregaddr);
5578 control &= ~HC_LAINT_ENA;
5579 writel(control, phba->HCregaddr);
5580 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005581 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005582 }
5583 else
5584 work_ha_copy &= ~HA_LATT;
5585 }
5586
James Smart93996272008-08-24 21:50:30 -04005587 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05005588 /*
5589 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
5590 * the only slow ring.
5591 */
5592 status = (work_ha_copy &
5593 (HA_RXMASK << (4*LPFC_ELS_RING)));
5594 status >>= (4*LPFC_ELS_RING);
5595 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05005596 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05005597 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04005598
5599 lpfc_debugfs_slow_ring_trc(phba,
5600 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
5601 control, status,
5602 (uint32_t)phba->sli.slistat.sli_intr);
5603
James Smart858c9f62007-06-17 19:56:39 -05005604 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04005605 lpfc_debugfs_slow_ring_trc(phba,
5606 "ISR Disable ring:"
5607 "pwork:x%x hawork:x%x wait:x%x",
5608 phba->work_ha, work_ha_copy,
5609 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005610 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005611
James Smart858c9f62007-06-17 19:56:39 -05005612 control &=
5613 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05005614 writel(control, phba->HCregaddr);
5615 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05005616 }
James Smarta58cbd52007-08-02 11:09:43 -04005617 else {
5618 lpfc_debugfs_slow_ring_trc(phba,
5619 "ISR slow ring: pwork:"
5620 "x%x hawork:x%x wait:x%x",
5621 phba->work_ha, work_ha_copy,
5622 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005623 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005624 }
James Smart5b75da22008-12-04 22:39:35 -05005625 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005626 }
5627 }
James Smart5b75da22008-12-04 22:39:35 -05005628 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04005629 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04005630 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04005631 /*
5632 * Check if there is a deferred error condition
5633 * is active
5634 */
5635 if ((HS_FFER1 & phba->work_hs) &&
5636 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
5637 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
5638 phba->hba_flag |= DEFER_ERATT;
5639 /* Clear all interrupt enable conditions */
5640 writel(0, phba->HCregaddr);
5641 readl(phba->HCregaddr);
5642 }
5643 }
5644
James Smart93996272008-08-24 21:50:30 -04005645 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005646 pmb = phba->sli.mbox_active;
5647 pmbox = &pmb->mb;
James Smart34b02dc2008-08-24 21:49:55 -04005648 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05005649 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005650
5651 /* First check out the status word */
5652 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
5653 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05005654 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05005655 /*
5656 * Stray Mailbox Interrupt, mbxCommand <cmd>
5657 * mbxStatus <status>
5658 */
James Smart09372822008-01-11 01:52:54 -05005659 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05005660 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005661 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05005662 "Interrupt mbxCommand x%x "
5663 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005664 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05005665 pmbox->mbxCommand,
5666 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05005667 /* clear mailbox attention bit */
5668 work_ha_copy &= ~HA_MBATT;
5669 } else {
James Smart97eab632008-04-07 10:16:05 -04005670 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05005671 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05005672 phba->last_completion_time = jiffies;
5673 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05005674 if (pmb->mbox_cmpl) {
5675 lpfc_sli_pcimem_bcopy(mbox, pmbox,
5676 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05005677 }
James Smart09372822008-01-11 01:52:54 -05005678 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
5679 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
5680
5681 lpfc_debugfs_disc_trc(vport,
5682 LPFC_DISC_TRC_MBOX_VPORT,
5683 "MBOX dflt rpi: : "
5684 "status:x%x rpi:x%x",
5685 (uint32_t)pmbox->mbxStatus,
5686 pmbox->un.varWords[0], 0);
5687
5688 if (!pmbox->mbxStatus) {
5689 mp = (struct lpfc_dmabuf *)
5690 (pmb->context1);
5691 ndlp = (struct lpfc_nodelist *)
5692 pmb->context2;
5693
5694 /* Reg_LOGIN of dflt RPI was
5695 * successful. new lets get
5696 * rid of the RPI using the
5697 * same mbox buffer.
5698 */
5699 lpfc_unreg_login(phba,
5700 vport->vpi,
5701 pmbox->un.varWords[0],
5702 pmb);
5703 pmb->mbox_cmpl =
5704 lpfc_mbx_cmpl_dflt_rpi;
5705 pmb->context1 = mp;
5706 pmb->context2 = ndlp;
5707 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04005708 rc = lpfc_sli_issue_mbox(phba,
5709 pmb,
5710 MBX_NOWAIT);
5711 if (rc != MBX_BUSY)
5712 lpfc_printf_log(phba,
5713 KERN_ERR,
5714 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04005715 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04005716 "been MBX_BUSY");
James Smart3772a992009-05-22 14:50:54 -04005717 if (rc != MBX_NOT_FINISHED)
5718 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05005719 }
5720 }
James Smart5b75da22008-12-04 22:39:35 -05005721 spin_lock_irqsave(
5722 &phba->pport->work_port_lock,
5723 iflag);
James Smart09372822008-01-11 01:52:54 -05005724 phba->pport->work_port_events &=
5725 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05005726 spin_unlock_irqrestore(
5727 &phba->pport->work_port_lock,
5728 iflag);
James Smart09372822008-01-11 01:52:54 -05005729 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05005730 }
James Smart97eab632008-04-07 10:16:05 -04005731 } else
James Smart5b75da22008-12-04 22:39:35 -05005732 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005733
James Smart92d7f7b2007-06-17 19:56:38 -05005734 if ((work_ha_copy & HA_MBATT) &&
5735 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05005736send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05005737 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04005738 do {
5739 rc = lpfc_sli_issue_mbox(phba, NULL,
5740 MBX_NOWAIT);
5741 } while (rc == MBX_NOT_FINISHED);
5742 if (rc != MBX_SUCCESS)
5743 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
5744 LOG_SLI, "0349 rc should be "
5745 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05005746 }
5747
James Smart5b75da22008-12-04 22:39:35 -05005748 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005749 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05005750 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04005751 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005752 }
James Smart93996272008-08-24 21:50:30 -04005753 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05005754
James Smart3772a992009-05-22 14:50:54 -04005755} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04005756
5757/**
James Smart3772a992009-05-22 14:50:54 -04005758 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04005759 * @irq: Interrupt number.
5760 * @dev_id: The device context pointer.
5761 *
5762 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04005763 * service routine when device with SLI-3 interface spec is enabled with
5764 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
5765 * ring event in the HBA. However, when the device is enabled with either
5766 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
5767 * device-level interrupt handler. When the PCI slot is in error recovery
5768 * or the HBA is undergoing initialization, the interrupt handler will not
5769 * process the interrupt. The SCSI FCP fast-path ring event are handled in
5770 * the intrrupt context. This function is called without any lock held.
5771 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04005772 *
5773 * This function returns IRQ_HANDLED when interrupt is handled else it
5774 * returns IRQ_NONE.
5775 **/
5776irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04005777lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04005778{
5779 struct lpfc_hba *phba;
5780 uint32_t ha_copy;
5781 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05005782 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04005783
5784 /* Get the driver's phba structure from the dev_id and
5785 * assume the HBA is not interrupting.
5786 */
5787 phba = (struct lpfc_hba *) dev_id;
5788
5789 if (unlikely(!phba))
5790 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05005791
5792 /*
James Smart93996272008-08-24 21:50:30 -04005793 * Stuff needs to be attented to when this function is invoked as an
5794 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005795 */
James Smart93996272008-08-24 21:50:30 -04005796 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04005797 /* Check device state for handling interrupt */
5798 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04005799 return IRQ_NONE;
5800 /* Need to read HA REG for FCP ring and other ring events */
5801 ha_copy = readl(phba->HAregaddr);
5802 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05005803 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04005804 /*
5805 * If there is deferred error attention, do not check for
5806 * any interrupt.
5807 */
5808 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04005809 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04005810 return IRQ_NONE;
5811 }
James Smart93996272008-08-24 21:50:30 -04005812 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
5813 phba->HAregaddr);
5814 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005815 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005816 } else
5817 ha_copy = phba->ha_copy;
5818
5819 /*
5820 * Process all events on FCP ring. Take the optimized path for FCP IO.
5821 */
5822 ha_copy &= ~(phba->work_ha_mask);
5823
5824 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05005825 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05005826 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05005827 lpfc_sli_handle_fast_ring_event(phba,
5828 &phba->sli.ring[LPFC_FCP_RING],
5829 status);
James Smarta4bc3372006-12-02 13:34:16 -05005830
5831 if (phba->cfg_multi_ring_support == 2) {
5832 /*
James Smart93996272008-08-24 21:50:30 -04005833 * Process all events on extra ring. Take the optimized path
5834 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05005835 */
James Smart93996272008-08-24 21:50:30 -04005836 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05005837 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05005838 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05005839 lpfc_sli_handle_fast_ring_event(phba,
5840 &phba->sli.ring[LPFC_EXTRA_RING],
5841 status);
5842 }
5843 }
dea31012005-04-17 16:05:31 -05005844 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04005845} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05005846
James Smart93996272008-08-24 21:50:30 -04005847/**
James Smart3772a992009-05-22 14:50:54 -04005848 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04005849 * @irq: Interrupt number.
5850 * @dev_id: The device context pointer.
5851 *
James Smart3772a992009-05-22 14:50:54 -04005852 * This function is the HBA device-level interrupt handler to device with
5853 * SLI-3 interface spec, called from the PCI layer when either MSI or
5854 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
5855 * requires driver attention. This function invokes the slow-path interrupt
5856 * attention handling function and fast-path interrupt attention handling
5857 * function in turn to process the relevant HBA attention events. This
5858 * function is called without any lock held. It gets the hbalock to access
5859 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04005860 *
5861 * This function returns IRQ_HANDLED when interrupt is handled, else it
5862 * returns IRQ_NONE.
5863 **/
5864irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04005865lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04005866{
5867 struct lpfc_hba *phba;
5868 irqreturn_t sp_irq_rc, fp_irq_rc;
5869 unsigned long status1, status2;
5870
5871 /*
5872 * Get the driver's phba structure from the dev_id and
5873 * assume the HBA is not interrupting.
5874 */
5875 phba = (struct lpfc_hba *) dev_id;
5876
5877 if (unlikely(!phba))
5878 return IRQ_NONE;
5879
James Smart3772a992009-05-22 14:50:54 -04005880 /* Check device state for handling interrupt */
5881 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04005882 return IRQ_NONE;
5883
5884 spin_lock(&phba->hbalock);
5885 phba->ha_copy = readl(phba->HAregaddr);
5886 if (unlikely(!phba->ha_copy)) {
5887 spin_unlock(&phba->hbalock);
5888 return IRQ_NONE;
5889 } else if (phba->ha_copy & HA_ERATT) {
5890 if (phba->hba_flag & HBA_ERATT_HANDLED)
5891 /* ERATT polling has handled ERATT */
5892 phba->ha_copy &= ~HA_ERATT;
5893 else
5894 /* Indicate interrupt handler handles ERATT */
5895 phba->hba_flag |= HBA_ERATT_HANDLED;
5896 }
5897
James Smarta257bf92009-04-06 18:48:10 -04005898 /*
5899 * If there is deferred error attention, do not check for any interrupt.
5900 */
5901 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5902 spin_unlock_irq(&phba->hbalock);
5903 return IRQ_NONE;
5904 }
5905
James Smart93996272008-08-24 21:50:30 -04005906 /* Clear attention sources except link and error attentions */
5907 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
5908 readl(phba->HAregaddr); /* flush */
5909 spin_unlock(&phba->hbalock);
5910
5911 /*
5912 * Invokes slow-path host attention interrupt handling as appropriate.
5913 */
5914
5915 /* status of events with mailbox and link attention */
5916 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
5917
5918 /* status of events with ELS ring */
5919 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
5920 status2 >>= (4*LPFC_ELS_RING);
5921
5922 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04005923 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04005924 else
5925 sp_irq_rc = IRQ_NONE;
5926
5927 /*
5928 * Invoke fast-path host attention interrupt handling as appropriate.
5929 */
5930
5931 /* status of events with FCP ring */
5932 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
5933 status1 >>= (4*LPFC_FCP_RING);
5934
5935 /* status of events with extra ring */
5936 if (phba->cfg_multi_ring_support == 2) {
5937 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
5938 status2 >>= (4*LPFC_EXTRA_RING);
5939 } else
5940 status2 = 0;
5941
5942 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04005943 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04005944 else
5945 fp_irq_rc = IRQ_NONE;
5946
5947 /* Return device-level interrupt handling status */
5948 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04005949} /* lpfc_sli_intr_handler */