blob: 632feee233ca1acc7850878abbdd4bb2135182d4 [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/**
71 * lpfc_cmd_iocb: Get next command iocb entry in the ring.
72 * @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/**
88 * lpfc_resp_iocb: Get next response iocb entry in the ring.
89 * @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/**
105 * __lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
106 * @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/**
124 * lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
125 * @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/**
145 * __lpfc_sli_release_iocbq: 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 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100154static void
James Smart2e0fef82007-06-17 19:56:36 -0500155__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
156{
157 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500158
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 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
164}
165
James Smarte59058c2008-08-24 21:49:00 -0400166/**
167 * lpfc_sli_release_iocbq: Release iocb to the iocb pool.
168 * @phba: Pointer to HBA context object.
169 * @iocbq: Pointer to driver iocb object.
170 *
171 * This function is called with no lock held to release the iocb to
172 * iocb pool.
173 **/
James Smart2e0fef82007-06-17 19:56:36 -0500174void
175lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
176{
177 unsigned long iflags;
178
179 /*
180 * Clean all volatile data fields, preserve iotag and node struct.
181 */
182 spin_lock_irqsave(&phba->hbalock, iflags);
183 __lpfc_sli_release_iocbq(phba, iocbq);
184 spin_unlock_irqrestore(&phba->hbalock, iflags);
185}
186
James Smarte59058c2008-08-24 21:49:00 -0400187/**
188 * lpfc_sli_iocb_cmd_type: Get the iocb type.
189 * @iocb_cmnd : iocb command code.
190 *
191 * This function is called by ring event handler function to get the iocb type.
192 * This function translates the iocb command to an iocb command type used to
193 * decide the final disposition of each completed IOCB.
194 * The function returns
195 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
196 * LPFC_SOL_IOCB if it is a solicited iocb completion
197 * LPFC_ABORT_IOCB if it is an abort iocb
198 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
199 *
200 * The caller is not required to hold any lock.
201 **/
dea31012005-04-17 16:05:31 -0500202static lpfc_iocb_type
203lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
204{
205 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
206
207 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
208 return 0;
209
210 switch (iocb_cmnd) {
211 case CMD_XMIT_SEQUENCE_CR:
212 case CMD_XMIT_SEQUENCE_CX:
213 case CMD_XMIT_BCAST_CN:
214 case CMD_XMIT_BCAST_CX:
215 case CMD_ELS_REQUEST_CR:
216 case CMD_ELS_REQUEST_CX:
217 case CMD_CREATE_XRI_CR:
218 case CMD_CREATE_XRI_CX:
219 case CMD_GET_RPI_CN:
220 case CMD_XMIT_ELS_RSP_CX:
221 case CMD_GET_RPI_CR:
222 case CMD_FCP_IWRITE_CR:
223 case CMD_FCP_IWRITE_CX:
224 case CMD_FCP_IREAD_CR:
225 case CMD_FCP_IREAD_CX:
226 case CMD_FCP_ICMND_CR:
227 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500228 case CMD_FCP_TSEND_CX:
229 case CMD_FCP_TRSP_CX:
230 case CMD_FCP_TRECEIVE_CX:
231 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500232 case CMD_ADAPTER_MSG:
233 case CMD_ADAPTER_DUMP:
234 case CMD_XMIT_SEQUENCE64_CR:
235 case CMD_XMIT_SEQUENCE64_CX:
236 case CMD_XMIT_BCAST64_CN:
237 case CMD_XMIT_BCAST64_CX:
238 case CMD_ELS_REQUEST64_CR:
239 case CMD_ELS_REQUEST64_CX:
240 case CMD_FCP_IWRITE64_CR:
241 case CMD_FCP_IWRITE64_CX:
242 case CMD_FCP_IREAD64_CR:
243 case CMD_FCP_IREAD64_CX:
244 case CMD_FCP_ICMND64_CR:
245 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500246 case CMD_FCP_TSEND64_CX:
247 case CMD_FCP_TRSP64_CX:
248 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500249 case CMD_GEN_REQUEST64_CR:
250 case CMD_GEN_REQUEST64_CX:
251 case CMD_XMIT_ELS_RSP64_CX:
252 type = LPFC_SOL_IOCB;
253 break;
254 case CMD_ABORT_XRI_CN:
255 case CMD_ABORT_XRI_CX:
256 case CMD_CLOSE_XRI_CN:
257 case CMD_CLOSE_XRI_CX:
258 case CMD_XRI_ABORTED_CX:
259 case CMD_ABORT_MXRI64_CN:
260 type = LPFC_ABORT_IOCB;
261 break;
262 case CMD_RCV_SEQUENCE_CX:
263 case CMD_RCV_ELS_REQ_CX:
264 case CMD_RCV_SEQUENCE64_CX:
265 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400266 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500267 case CMD_IOCB_RCV_SEQ64_CX:
268 case CMD_IOCB_RCV_ELS64_CX:
269 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500270 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500271 type = LPFC_UNSOL_IOCB;
272 break;
James Smart3163f722008-02-08 18:50:25 -0500273 case CMD_IOCB_XMIT_MSEQ64_CR:
274 case CMD_IOCB_XMIT_MSEQ64_CX:
275 case CMD_IOCB_RCV_SEQ_LIST64_CX:
276 case CMD_IOCB_RCV_ELS_LIST64_CX:
277 case CMD_IOCB_CLOSE_EXTENDED_CN:
278 case CMD_IOCB_ABORT_EXTENDED_CN:
279 case CMD_IOCB_RET_HBQE64_CN:
280 case CMD_IOCB_FCP_IBIDIR64_CR:
281 case CMD_IOCB_FCP_IBIDIR64_CX:
282 case CMD_IOCB_FCP_ITASKMGT64_CX:
283 case CMD_IOCB_LOGENTRY_CN:
284 case CMD_IOCB_LOGENTRY_ASYNC_CN:
285 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700286 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500287 type = LPFC_UNKNOWN_IOCB;
288 break;
dea31012005-04-17 16:05:31 -0500289 default:
290 type = LPFC_UNKNOWN_IOCB;
291 break;
292 }
293
294 return type;
295}
296
James Smarte59058c2008-08-24 21:49:00 -0400297/**
298 * lpfc_sli_ring_map: Issue config_ring mbox for all rings.
299 * @phba: Pointer to HBA context object.
300 *
301 * This function is called from SLI initialization code
302 * to configure every ring of the HBA's SLI interface. The
303 * caller is not required to hold any lock. This function issues
304 * a config_ring mailbox command for each ring.
305 * This function returns zero if successful else returns a negative
306 * error code.
307 **/
dea31012005-04-17 16:05:31 -0500308static int
James Smarted957682007-06-17 19:56:37 -0500309lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500310{
311 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500312 LPFC_MBOXQ_t *pmb;
313 MAILBOX_t *pmbox;
314 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500315
James Smarted957682007-06-17 19:56:37 -0500316 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
317 if (!pmb)
318 return -ENOMEM;
319 pmbox = &pmb->mb;
320 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500321 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500322 lpfc_config_ring(phba, i, pmb);
323 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
324 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400326 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500327 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
328 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400329 rc, pmbox->mbxCommand,
330 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500331 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500332 ret = -ENXIO;
333 break;
dea31012005-04-17 16:05:31 -0500334 }
335 }
James Smarted957682007-06-17 19:56:37 -0500336 mempool_free(pmb, phba->mbox_mem_pool);
337 return ret;
dea31012005-04-17 16:05:31 -0500338}
339
James Smarte59058c2008-08-24 21:49:00 -0400340/**
341 * lpfc_sli_ringtxcmpl_put: Adds new iocb to the txcmplq.
342 * @phba: Pointer to HBA context object.
343 * @pring: Pointer to driver SLI ring object.
344 * @piocb: Pointer to the driver iocb object.
345 *
346 * This function is called with hbalock held. The function adds the
347 * new iocb to txcmplq of the given ring. This function always returns
348 * 0. If this function is called for ELS ring, this function checks if
349 * there is a vport associated with the ELS command. This function also
350 * starts els_tmofunc timer if this is an ELS command.
351 **/
dea31012005-04-17 16:05:31 -0500352static int
James Smart2e0fef82007-06-17 19:56:36 -0500353lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
354 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500355{
dea31012005-04-17 16:05:31 -0500356 list_add_tail(&piocb->list, &pring->txcmplq);
357 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500358 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
359 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
360 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
361 if (!piocb->vport)
362 BUG();
363 else
364 mod_timer(&piocb->vport->els_tmofunc,
365 jiffies + HZ * (phba->fc_ratov << 1));
366 }
367
dea31012005-04-17 16:05:31 -0500368
James Smart2e0fef82007-06-17 19:56:36 -0500369 return 0;
dea31012005-04-17 16:05:31 -0500370}
371
James Smarte59058c2008-08-24 21:49:00 -0400372/**
373 * lpfc_sli_ringtx_get: Get first element of the txq.
374 * @phba: Pointer to HBA context object.
375 * @pring: Pointer to driver SLI ring object.
376 *
377 * This function is called with hbalock held to get next
378 * iocb in txq of the given ring. If there is any iocb in
379 * the txq, the function returns first iocb in the list after
380 * removing the iocb from the list, else it returns NULL.
381 **/
dea31012005-04-17 16:05:31 -0500382static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500383lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500384{
dea31012005-04-17 16:05:31 -0500385 struct lpfc_iocbq *cmd_iocb;
386
James Smart858c9f62007-06-17 19:56:39 -0500387 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
388 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500389 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500390 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500391}
392
James Smarte59058c2008-08-24 21:49:00 -0400393/**
394 * lpfc_sli_next_iocb_slot: Get next iocb slot in the ring.
395 * @phba: Pointer to HBA context object.
396 * @pring: Pointer to driver SLI ring object.
397 *
398 * This function is called with hbalock held and the caller must post the
399 * iocb without releasing the lock. If the caller releases the lock,
400 * iocb slot returned by the function is not guaranteed to be available.
401 * The function returns pointer to the next available iocb slot if there
402 * is available slot in the ring, else it returns NULL.
403 * If the get index of the ring is ahead of the put index, the function
404 * will post an error attention event to the worker thread to take the
405 * HBA to offline state.
406 **/
dea31012005-04-17 16:05:31 -0500407static IOCB_t *
408lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
409{
James Smart34b02dc2008-08-24 21:49:55 -0400410 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500411 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500412 if ((pring->next_cmdidx == pring->cmdidx) &&
413 (++pring->next_cmdidx >= max_cmd_idx))
414 pring->next_cmdidx = 0;
415
416 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
417
418 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
419
420 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
421 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400422 "0315 Ring %d issue: portCmdGet %d "
dea31012005-04-17 16:05:31 -0500423 "is bigger then cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400424 pring->ringno,
dea31012005-04-17 16:05:31 -0500425 pring->local_getidx, max_cmd_idx);
426
James Smart2e0fef82007-06-17 19:56:36 -0500427 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500428 /*
429 * All error attention handlers are posted to
430 * worker thread
431 */
432 phba->work_ha |= HA_ERATT;
433 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500434
James Smart5e9d9b82008-06-14 22:52:53 -0400435 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500436
437 return NULL;
438 }
439
440 if (pring->local_getidx == pring->next_cmdidx)
441 return NULL;
442 }
443
James Smarted957682007-06-17 19:56:37 -0500444 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500445}
446
James Smarte59058c2008-08-24 21:49:00 -0400447/**
448 * lpfc_sli_next_iotag: Get an iotag for the iocb.
449 * @phba: Pointer to HBA context object.
450 * @iocbq: Pointer to driver iocb object.
451 *
452 * This function gets an iotag for the iocb. If there is no unused iotag and
453 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
454 * array and assigns a new iotag.
455 * The function returns the allocated iotag if successful, else returns zero.
456 * Zero is not a valid iotag.
457 * The caller is not required to hold any lock.
458 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500459uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500460lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500461{
James Smart2e0fef82007-06-17 19:56:36 -0500462 struct lpfc_iocbq **new_arr;
463 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500464 size_t new_len;
465 struct lpfc_sli *psli = &phba->sli;
466 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500467
James Smart2e0fef82007-06-17 19:56:36 -0500468 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500469 iotag = psli->last_iotag;
470 if(++iotag < psli->iocbq_lookup_len) {
471 psli->last_iotag = iotag;
472 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500473 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500474 iocbq->iotag = iotag;
475 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500476 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500477 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
478 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500479 spin_unlock_irq(&phba->hbalock);
480 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500481 GFP_KERNEL);
482 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500483 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500484 old_arr = psli->iocbq_lookup;
485 if (new_len <= psli->iocbq_lookup_len) {
486 /* highly unprobable case */
487 kfree(new_arr);
488 iotag = psli->last_iotag;
489 if(++iotag < psli->iocbq_lookup_len) {
490 psli->last_iotag = iotag;
491 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500492 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500493 iocbq->iotag = iotag;
494 return iotag;
495 }
James Smart2e0fef82007-06-17 19:56:36 -0500496 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500497 return 0;
498 }
499 if (psli->iocbq_lookup)
500 memcpy(new_arr, old_arr,
501 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -0400502 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -0500503 psli->iocbq_lookup = new_arr;
504 psli->iocbq_lookup_len = new_len;
505 psli->last_iotag = iotag;
506 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500507 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500508 iocbq->iotag = iotag;
509 kfree(old_arr);
510 return iotag;
511 }
James Smart8f6d98d2006-08-01 07:34:00 -0400512 } else
James Smart2e0fef82007-06-17 19:56:36 -0500513 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500514
James Bottomley604a3e32005-10-29 10:28:33 -0500515 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400516 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
517 psli->last_iotag);
dea31012005-04-17 16:05:31 -0500518
James Bottomley604a3e32005-10-29 10:28:33 -0500519 return 0;
dea31012005-04-17 16:05:31 -0500520}
521
James Smarte59058c2008-08-24 21:49:00 -0400522/**
523 * lpfc_sli_submit_iocb: Submit an iocb to the firmware.
524 * @phba: Pointer to HBA context object.
525 * @pring: Pointer to driver SLI ring object.
526 * @iocb: Pointer to iocb slot in the ring.
527 * @nextiocb: Pointer to driver iocb object which need to be
528 * posted to firmware.
529 *
530 * This function is called with hbalock held to post a new iocb to
531 * the firmware. This function copies the new iocb to ring iocb slot and
532 * updates the ring pointers. It adds the new iocb to txcmplq if there is
533 * a completion call back for this iocb else the function will free the
534 * iocb object.
535 **/
dea31012005-04-17 16:05:31 -0500536static void
537lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
538 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
539{
540 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500541 * Set up an iotag
dea31012005-04-17 16:05:31 -0500542 */
James Bottomley604a3e32005-10-29 10:28:33 -0500543 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500544
James Smarta58cbd52007-08-02 11:09:43 -0400545 if (pring->ringno == LPFC_ELS_RING) {
546 lpfc_debugfs_slow_ring_trc(phba,
547 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
548 *(((uint32_t *) &nextiocb->iocb) + 4),
549 *(((uint32_t *) &nextiocb->iocb) + 6),
550 *(((uint32_t *) &nextiocb->iocb) + 7));
551 }
552
dea31012005-04-17 16:05:31 -0500553 /*
554 * Issue iocb command to adapter
555 */
James Smart92d7f7b2007-06-17 19:56:38 -0500556 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500557 wmb();
558 pring->stats.iocb_cmd++;
559
560 /*
561 * If there is no completion routine to call, we can release the
562 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
563 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
564 */
565 if (nextiocb->iocb_cmpl)
566 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500567 else
James Smart2e0fef82007-06-17 19:56:36 -0500568 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500569
570 /*
571 * Let the HBA know what IOCB slot will be the next one the
572 * driver will put a command into.
573 */
574 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500575 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500576}
577
James Smarte59058c2008-08-24 21:49:00 -0400578/**
579 * lpfc_sli_update_full_ring: Update the chip attention register.
580 * @phba: Pointer to HBA context object.
581 * @pring: Pointer to driver SLI ring object.
582 *
583 * The caller is not required to hold any lock for calling this function.
584 * This function updates the chip attention bits for the ring to inform firmware
585 * that there are pending work to be done for this ring and requests an
586 * interrupt when there is space available in the ring. This function is
587 * called when the driver is unable to post more iocbs to the ring due
588 * to unavailability of space in the ring.
589 **/
dea31012005-04-17 16:05:31 -0500590static void
James Smart2e0fef82007-06-17 19:56:36 -0500591lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500592{
593 int ringno = pring->ringno;
594
595 pring->flag |= LPFC_CALL_RING_AVAILABLE;
596
597 wmb();
598
599 /*
600 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
601 * The HBA will tell us when an IOCB entry is available.
602 */
603 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
604 readl(phba->CAregaddr); /* flush */
605
606 pring->stats.iocb_cmd_full++;
607}
608
James Smarte59058c2008-08-24 21:49:00 -0400609/**
610 * lpfc_sli_update_ring: Update chip attention register.
611 * @phba: Pointer to HBA context object.
612 * @pring: Pointer to driver SLI ring object.
613 *
614 * This function updates the chip attention register bit for the
615 * given ring to inform HBA that there is more work to be done
616 * in this ring. The caller is not required to hold any lock.
617 **/
dea31012005-04-17 16:05:31 -0500618static void
James Smart2e0fef82007-06-17 19:56:36 -0500619lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500620{
621 int ringno = pring->ringno;
622
623 /*
624 * Tell the HBA that there is work to do in this ring.
625 */
James Smart34b02dc2008-08-24 21:49:55 -0400626 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
627 wmb();
628 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
629 readl(phba->CAregaddr); /* flush */
630 }
dea31012005-04-17 16:05:31 -0500631}
632
James Smarte59058c2008-08-24 21:49:00 -0400633/**
634 * lpfc_sli_resume_iocb: Process iocbs in the txq.
635 * @phba: Pointer to HBA context object.
636 * @pring: Pointer to driver SLI ring object.
637 *
638 * This function is called with hbalock held to post pending iocbs
639 * in the txq to the firmware. This function is called when driver
640 * detects space available in the ring.
641 **/
dea31012005-04-17 16:05:31 -0500642static void
James Smart2e0fef82007-06-17 19:56:36 -0500643lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500644{
645 IOCB_t *iocb;
646 struct lpfc_iocbq *nextiocb;
647
648 /*
649 * Check to see if:
650 * (a) there is anything on the txq to send
651 * (b) link is up
652 * (c) link attention events can be processed (fcp ring only)
653 * (d) IOCB processing is not blocked by the outstanding mbox command.
654 */
655 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500656 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500657 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -0400658 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -0500659
660 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
661 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
662 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
663
664 if (iocb)
665 lpfc_sli_update_ring(phba, pring);
666 else
667 lpfc_sli_update_full_ring(phba, pring);
668 }
669
670 return;
671}
672
James Smarte59058c2008-08-24 21:49:00 -0400673/**
674 * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
675 * @phba: Pointer to HBA context object.
676 * @hbqno: HBQ number.
677 *
678 * This function is called with hbalock held to get the next
679 * available slot for the given HBQ. If there is free slot
680 * available for the HBQ it will return pointer to the next available
681 * HBQ entry else it will return NULL.
682 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100683static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500684lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
685{
686 struct hbq_s *hbqp = &phba->hbqs[hbqno];
687
688 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
689 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
690 hbqp->next_hbqPutIdx = 0;
691
692 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500693 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500694 uint32_t getidx = le32_to_cpu(raw_index);
695
696 hbqp->local_hbqGetIdx = getidx;
697
698 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
699 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500700 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400701 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500702 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400703 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500704 hbqp->entry_count);
705
706 phba->link_state = LPFC_HBA_ERROR;
707 return NULL;
708 }
709
710 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
711 return NULL;
712 }
713
James Smart51ef4c22007-08-02 11:10:31 -0400714 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
715 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500716}
717
James Smarte59058c2008-08-24 21:49:00 -0400718/**
719 * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
720 * @phba: Pointer to HBA context object.
721 *
722 * This function is called with no lock held to free all the
723 * hbq buffers while uninitializing the SLI interface. It also
724 * frees the HBQ buffers returned by the firmware but not yet
725 * processed by the upper layers.
726 **/
James Smarted957682007-06-17 19:56:37 -0500727void
728lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
729{
James Smart92d7f7b2007-06-17 19:56:38 -0500730 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
731 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -0500732 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -0400733 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -0500734 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500735
James Smart51ef4c22007-08-02 11:10:31 -0400736 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500737 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -0500738 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -0400739 for (i = 0; i < hbq_count; ++i) {
740 list_for_each_entry_safe(dmabuf, next_dmabuf,
741 &phba->hbqs[i].hbq_buffer_list, list) {
742 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
743 list_del(&hbq_buf->dbuf.list);
744 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
745 }
James Smarta8adb832007-10-27 13:37:53 -0400746 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -0500747 }
James Smart3163f722008-02-08 18:50:25 -0500748 /* Return all HBQ buffer that are in-fly */
749 list_for_each_entry_safe(dmabuf, next_dmabuf,
750 &phba->hbqbuf_in_list, list) {
751 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
752 list_del(&hbq_buf->dbuf.list);
753 if (hbq_buf->tag == -1) {
754 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
755 (phba, hbq_buf);
756 } else {
757 hbqno = hbq_buf->tag >> 16;
758 if (hbqno >= LPFC_MAX_HBQS)
759 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
760 (phba, hbq_buf);
761 else
762 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
763 hbq_buf);
764 }
765 }
766
767 /* Mark the HBQs not in use */
768 phba->hbq_in_use = 0;
769 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -0500770}
771
James Smarte59058c2008-08-24 21:49:00 -0400772/**
773 * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
774 * @phba: Pointer to HBA context object.
775 * @hbqno: HBQ number.
776 * @hbq_buf: Pointer to HBQ buffer.
777 *
778 * This function is called with the hbalock held to post a
779 * hbq buffer to the firmware. If the function finds an empty
780 * slot in the HBQ, it will post the buffer. The function will return
781 * pointer to the hbq entry if it successfully post the buffer
782 * else it will return NULL.
783 **/
James Smart51ef4c22007-08-02 11:10:31 -0400784static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500785lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500786 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500787{
788 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500789 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500790
791 /* Get next HBQ entry slot to use */
792 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
793 if (hbqe) {
794 struct hbq_s *hbqp = &phba->hbqs[hbqno];
795
James Smart92d7f7b2007-06-17 19:56:38 -0500796 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
797 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400798 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500799 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500800 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
801 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
802 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500803 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
804 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500805 /* flush */
James Smarted957682007-06-17 19:56:37 -0500806 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400807 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smarted957682007-06-17 19:56:37 -0500808 }
James Smart51ef4c22007-08-02 11:10:31 -0400809 return hbqe;
James Smarted957682007-06-17 19:56:37 -0500810}
811
James Smarte59058c2008-08-24 21:49:00 -0400812/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -0500813static struct lpfc_hbq_init lpfc_els_hbq = {
814 .rn = 1,
815 .entry_count = 200,
816 .mask_count = 0,
817 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400818 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500819 .buffer_count = 0,
820 .init_count = 20,
821 .add_count = 5,
822};
James Smarted957682007-06-17 19:56:37 -0500823
James Smarte59058c2008-08-24 21:49:00 -0400824/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -0400825static struct lpfc_hbq_init lpfc_extra_hbq = {
826 .rn = 1,
827 .entry_count = 200,
828 .mask_count = 0,
829 .profile = 0,
830 .ring_mask = (1 << LPFC_EXTRA_RING),
831 .buffer_count = 0,
832 .init_count = 0,
833 .add_count = 5,
834};
835
James Smarte59058c2008-08-24 21:49:00 -0400836/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -0400837struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500838 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400839 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500840};
841
James Smarte59058c2008-08-24 21:49:00 -0400842/**
843 * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
844 * @phba: Pointer to HBA context object.
845 * @hbqno: HBQ number.
846 * @count: Number of HBQ buffers to be posted.
847 *
James Smartd7c255b2008-08-24 21:50:00 -0400848 * This function is called with no lock held to post more hbq buffers to the
849 * given HBQ. The function returns the number of HBQ buffers successfully
850 * posted.
James Smarte59058c2008-08-24 21:49:00 -0400851 **/
James Smart311464e2007-08-02 11:10:37 -0400852static int
James Smart92d7f7b2007-06-17 19:56:38 -0500853lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
854{
James Smartd7c255b2008-08-24 21:50:00 -0400855 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -0500856 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500857 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -0400858 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700859 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -0400860 return 0;
James Smart51ef4c22007-08-02 11:10:31 -0400861
James Smartd7c255b2008-08-24 21:50:00 -0400862 if ((phba->hbqs[hbqno].buffer_count + count) >
863 lpfc_hbq_defs[hbqno]->entry_count)
864 count = lpfc_hbq_defs[hbqno]->entry_count -
865 phba->hbqs[hbqno].buffer_count;
866 if (!count)
867 return 0;
868 /* Allocate HBQ entries */
869 for (i = 0; i < count; i++) {
870 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
871 if (!hbq_buffer)
872 break;
873 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
874 }
James Smart3163f722008-02-08 18:50:25 -0500875 /* Check whether HBQ is still in use */
876 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700877 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -0400878 goto err;
879 while (!list_empty(&hbq_buf_list)) {
880 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
881 dbuf.list);
882 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
883 (hbqno << 16));
884 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -0400885 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -0400886 posted++;
887 } else
James Smart51ef4c22007-08-02 11:10:31 -0400888 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -0500889 }
James Smart3163f722008-02-08 18:50:25 -0500890 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -0400891 return posted;
892err:
893 spin_unlock_irqrestore(&phba->hbalock, flags);
894 while (!list_empty(&hbq_buf_list)) {
895 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
896 dbuf.list);
897 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
898 }
James Smart92d7f7b2007-06-17 19:56:38 -0500899 return 0;
James Smarted957682007-06-17 19:56:37 -0500900}
901
James Smarte59058c2008-08-24 21:49:00 -0400902/**
903 * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
904 * @phba: Pointer to HBA context object.
905 * @qno: HBQ number.
906 *
907 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -0400908 * is called with no lock held. The function returns the number of HBQ entries
909 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400910 **/
James Smarted957682007-06-17 19:56:37 -0500911int
James Smart92d7f7b2007-06-17 19:56:38 -0500912lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500913{
James Smart92d7f7b2007-06-17 19:56:38 -0500914 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
915 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -0500916}
917
James Smarte59058c2008-08-24 21:49:00 -0400918/**
919 * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
920 * @phba: Pointer to HBA context object.
921 * @qno: HBQ queue number.
922 *
923 * This function is called from SLI initialization code path with
924 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -0400925 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400926 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100927static int
James Smart92d7f7b2007-06-17 19:56:38 -0500928lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500929{
James Smart92d7f7b2007-06-17 19:56:38 -0500930 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
931 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -0500932}
933
James Smarte59058c2008-08-24 21:49:00 -0400934/**
935 * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
936 * @phba: Pointer to HBA context object.
937 * @tag: Tag of the hbq buffer.
938 *
939 * This function is called with hbalock held. This function searches
940 * for the hbq buffer associated with the given tag in the hbq buffer
941 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
942 * it returns NULL.
943 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100944static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -0500945lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
946{
James Smart92d7f7b2007-06-17 19:56:38 -0500947 struct lpfc_dmabuf *d_buf;
948 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400949 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500950
James Smart51ef4c22007-08-02 11:10:31 -0400951 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +0200952 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -0400953 return NULL;
954
955 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -0500956 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -0400957 if (hbq_buf->tag == tag) {
James Smart92d7f7b2007-06-17 19:56:38 -0500958 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -0500959 }
960 }
James Smart92d7f7b2007-06-17 19:56:38 -0500961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400962 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -0400963 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -0500964 return NULL;
James Smarted957682007-06-17 19:56:37 -0500965}
966
James Smarte59058c2008-08-24 21:49:00 -0400967/**
968 * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
969 * @phba: Pointer to HBA context object.
970 * @hbq_buffer: Pointer to HBQ buffer.
971 *
972 * This function is called with hbalock. This function gives back
973 * the hbq buffer to firmware. If the HBQ does not have space to
974 * post the buffer, it will free the buffer.
975 **/
James Smarted957682007-06-17 19:56:37 -0500976void
James Smart51ef4c22007-08-02 11:10:31 -0400977lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -0500978{
979 uint32_t hbqno;
980
James Smart51ef4c22007-08-02 11:10:31 -0400981 if (hbq_buffer) {
982 hbqno = hbq_buffer->tag >> 16;
983 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
984 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
985 }
James Smarted957682007-06-17 19:56:37 -0500986 }
987}
988
James Smarte59058c2008-08-24 21:49:00 -0400989/**
990 * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
991 * @mbxCommand: mailbox command code.
992 *
993 * This function is called by the mailbox event handler function to verify
994 * that the completed mailbox command is a legitimate mailbox command. If the
995 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
996 * and the mailbox event handler will take the HBA offline.
997 **/
dea31012005-04-17 16:05:31 -0500998static int
999lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1000{
1001 uint8_t ret;
1002
1003 switch (mbxCommand) {
1004 case MBX_LOAD_SM:
1005 case MBX_READ_NV:
1006 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001007 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001008 case MBX_RUN_BIU_DIAG:
1009 case MBX_INIT_LINK:
1010 case MBX_DOWN_LINK:
1011 case MBX_CONFIG_LINK:
1012 case MBX_CONFIG_RING:
1013 case MBX_RESET_RING:
1014 case MBX_READ_CONFIG:
1015 case MBX_READ_RCONFIG:
1016 case MBX_READ_SPARM:
1017 case MBX_READ_STATUS:
1018 case MBX_READ_RPI:
1019 case MBX_READ_XRI:
1020 case MBX_READ_REV:
1021 case MBX_READ_LNK_STAT:
1022 case MBX_REG_LOGIN:
1023 case MBX_UNREG_LOGIN:
1024 case MBX_READ_LA:
1025 case MBX_CLEAR_LA:
1026 case MBX_DUMP_MEMORY:
1027 case MBX_DUMP_CONTEXT:
1028 case MBX_RUN_DIAGS:
1029 case MBX_RESTART:
1030 case MBX_UPDATE_CFG:
1031 case MBX_DOWN_LOAD:
1032 case MBX_DEL_LD_ENTRY:
1033 case MBX_RUN_PROGRAM:
1034 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001035 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001036 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001037 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001038 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001039 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001040 case MBX_LOAD_AREA:
1041 case MBX_RUN_BIU_DIAG64:
1042 case MBX_CONFIG_PORT:
1043 case MBX_READ_SPARM64:
1044 case MBX_READ_RPI64:
1045 case MBX_REG_LOGIN64:
1046 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001047 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001048 case MBX_SET_DEBUG:
1049 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001050 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001051 case MBX_REG_VPI:
1052 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001053 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001054 case MBX_PORT_CAPABILITIES:
1055 case MBX_PORT_IOV_CONTROL:
dea31012005-04-17 16:05:31 -05001056 ret = mbxCommand;
1057 break;
1058 default:
1059 ret = MBX_SHUTDOWN;
1060 break;
1061 }
James Smart2e0fef82007-06-17 19:56:36 -05001062 return ret;
dea31012005-04-17 16:05:31 -05001063}
James Smarte59058c2008-08-24 21:49:00 -04001064
1065/**
1066 * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
1067 * lpfc_sli_issue_mbox_wait.
1068 * @phba: Pointer to HBA context object.
1069 * @pmboxq: Pointer to mailbox command.
1070 *
1071 * This is completion handler function for mailbox commands issued from
1072 * lpfc_sli_issue_mbox_wait function. This function is called by the
1073 * mailbox event handler function with no lock held. This function
1074 * will wake up thread waiting on the wait queue pointed by context1
1075 * of the mailbox.
1076 **/
dea31012005-04-17 16:05:31 -05001077static void
James Smart2e0fef82007-06-17 19:56:36 -05001078lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001079{
1080 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001081 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001082
1083 /*
1084 * If pdone_q is empty, the driver thread gave up waiting and
1085 * continued running.
1086 */
James Smart7054a602007-04-25 09:52:34 -04001087 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001088 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001089 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1090 if (pdone_q)
1091 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001092 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001093 return;
1094}
1095
James Smarte59058c2008-08-24 21:49:00 -04001096
1097/**
1098 * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
1099 * @phba: Pointer to HBA context object.
1100 * @pmb: Pointer to mailbox object.
1101 *
1102 * This function is the default mailbox completion handler. It
1103 * frees the memory resources associated with the completed mailbox
1104 * command. If the completed command is a REG_LOGIN mailbox command,
1105 * this function will issue a UREG_LOGIN to re-claim the RPI.
1106 **/
dea31012005-04-17 16:05:31 -05001107void
James Smart2e0fef82007-06-17 19:56:36 -05001108lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001109{
1110 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -04001111 uint16_t rpi;
1112 int rc;
1113
dea31012005-04-17 16:05:31 -05001114 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001115
dea31012005-04-17 16:05:31 -05001116 if (mp) {
1117 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1118 kfree(mp);
1119 }
James Smart7054a602007-04-25 09:52:34 -04001120
1121 /*
1122 * If a REG_LOGIN succeeded after node is destroyed or node
1123 * is in re-discovery driver need to cleanup the RPI.
1124 */
James Smart2e0fef82007-06-17 19:56:36 -05001125 if (!(phba->pport->load_flag & FC_UNLOADING) &&
1126 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1127 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -04001128
1129 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -05001130 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1131 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001132 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1133 if (rc != MBX_NOT_FINISHED)
1134 return;
1135 }
1136
James Smart2e0fef82007-06-17 19:56:36 -05001137 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001138 return;
1139}
1140
James Smarte59058c2008-08-24 21:49:00 -04001141/**
1142 * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
1143 * @phba: Pointer to HBA context object.
1144 *
1145 * This function is called with no lock held. This function processes all
1146 * the completed mailbox commands and gives it to upper layers. The interrupt
1147 * service routine processes mailbox completion interrupt and adds completed
1148 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1149 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1150 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1151 * function returns the mailbox commands to the upper layer by calling the
1152 * completion handler function of each mailbox.
1153 **/
dea31012005-04-17 16:05:31 -05001154int
James Smart2e0fef82007-06-17 19:56:36 -05001155lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001156{
James Smart92d7f7b2007-06-17 19:56:38 -05001157 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001158 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001159 int rc;
1160 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001161
1162 phba->sli.slistat.mbox_event++;
1163
James Smart92d7f7b2007-06-17 19:56:38 -05001164 /* Get all completed mailboxe buffers into the cmplq */
1165 spin_lock_irq(&phba->hbalock);
1166 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1167 spin_unlock_irq(&phba->hbalock);
1168
dea31012005-04-17 16:05:31 -05001169 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001170 do {
1171 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1172 if (pmb == NULL)
1173 break;
1174
dea31012005-04-17 16:05:31 -05001175 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -05001176
James Smart858c9f62007-06-17 19:56:39 -05001177 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1178 if (pmb->vport) {
1179 lpfc_debugfs_disc_trc(pmb->vport,
1180 LPFC_DISC_TRC_MBOX_VPORT,
1181 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1182 (uint32_t)pmbox->mbxCommand,
1183 pmbox->un.varWords[0],
1184 pmbox->un.varWords[1]);
1185 }
1186 else {
1187 lpfc_debugfs_disc_trc(phba->pport,
1188 LPFC_DISC_TRC_MBOX,
1189 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1190 (uint32_t)pmbox->mbxCommand,
1191 pmbox->un.varWords[0],
1192 pmbox->un.varWords[1]);
1193 }
1194 }
1195
dea31012005-04-17 16:05:31 -05001196 /*
1197 * It is a fatal error if unknown mbox command completion.
1198 */
1199 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1200 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001201 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001202 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001203 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -05001204 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001205 pmb->vport ? pmb->vport->vpi : 0,
1206 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -05001207 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001208 phba->work_hs = HS_FFER3;
1209 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001210 continue;
dea31012005-04-17 16:05:31 -05001211 }
1212
dea31012005-04-17 16:05:31 -05001213 if (pmbox->mbxStatus) {
1214 phba->sli.slistat.mbox_stat_err++;
1215 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1216 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001217 lpfc_printf_log(phba, KERN_INFO,
1218 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001219 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001220 "error - RETRYing Data: x%x "
1221 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001222 pmb->vport ? pmb->vport->vpi :0,
1223 pmbox->mbxCommand,
1224 pmbox->mbxStatus,
1225 pmbox->un.varWords[0],
1226 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001227 pmbox->mbxStatus = 0;
1228 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -05001229 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001230 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001231 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001232 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1233 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -05001234 continue;
dea31012005-04-17 16:05:31 -05001235 }
1236 }
1237
1238 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001239 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001240 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -05001241 "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 -05001242 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001243 pmbox->mbxCommand,
1244 pmb->mbox_cmpl,
1245 *((uint32_t *) pmbox),
1246 pmbox->un.varWords[0],
1247 pmbox->un.varWords[1],
1248 pmbox->un.varWords[2],
1249 pmbox->un.varWords[3],
1250 pmbox->un.varWords[4],
1251 pmbox->un.varWords[5],
1252 pmbox->un.varWords[6],
1253 pmbox->un.varWords[7]);
1254
James Smart92d7f7b2007-06-17 19:56:38 -05001255 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001256 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001257 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001258 return 0;
dea31012005-04-17 16:05:31 -05001259}
James Smart92d7f7b2007-06-17 19:56:38 -05001260
James Smarte59058c2008-08-24 21:49:00 -04001261/**
James Smarte59058c2008-08-24 21:49:00 -04001262 * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
1263 * @phba: Pointer to HBA context object.
1264 * @pring: Pointer to driver SLI ring object.
1265 * @tag: buffer tag.
1266 *
1267 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1268 * is set in the tag the buffer is posted for a particular exchange,
1269 * the function will return the buffer without replacing the buffer.
1270 * If the buffer is for unsolicited ELS or CT traffic, this function
1271 * returns the buffer and also posts another buffer to the firmware.
1272 **/
James Smart76bb24e2007-10-27 13:38:00 -04001273static struct lpfc_dmabuf *
1274lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001275 struct lpfc_sli_ring *pring,
1276 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001277{
James Smart9f1e1b52008-12-04 22:39:40 -05001278 struct hbq_dmabuf *hbq_entry;
1279
James Smart76bb24e2007-10-27 13:38:00 -04001280 if (tag & QUE_BUFTAG_BIT)
1281 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001282 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1283 if (!hbq_entry)
1284 return NULL;
1285 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001286}
James Smart57127f12007-10-27 13:37:05 -04001287
James Smarte59058c2008-08-24 21:49:00 -04001288
1289/**
1290 * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
1291 * @phba: Pointer to HBA context object.
1292 * @pring: Pointer to driver SLI ring object.
1293 * @saveq: Pointer to the unsolicited iocb.
1294 *
1295 * This function is called with no lock held by the ring event handler
1296 * when there is an unsolicited iocb posted to the response ring by the
1297 * firmware. This function gets the buffer associated with the iocbs
1298 * and calls the event handler for the ring. This function handles both
1299 * qring buffers and hbq buffers.
1300 * When the function returns 1 the caller can free the iocb object otherwise
1301 * upper layer functions will free the iocb objects.
1302 **/
dea31012005-04-17 16:05:31 -05001303static int
1304lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1305 struct lpfc_iocbq *saveq)
1306{
1307 IOCB_t * irsp;
1308 WORD5 * w5p;
1309 uint32_t Rctl, Type;
1310 uint32_t match, i;
James Smart76bb24e2007-10-27 13:38:00 -04001311 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001312 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001313
1314 match = 0;
1315 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001316
1317 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1318 if (pring->lpfc_sli_rcv_async_status)
1319 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1320 else
1321 lpfc_printf_log(phba,
1322 KERN_WARNING,
1323 LOG_SLI,
1324 "0316 Ring %d handler: unexpected "
1325 "ASYNC_STATUS iocb received evt_code "
1326 "0x%x\n",
1327 pring->ringno,
1328 irsp->un.asyncstat.evt_code);
1329 return 1;
1330 }
1331
James Smart3163f722008-02-08 18:50:25 -05001332 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1333 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1334 if (irsp->ulpBdeCount > 0) {
1335 dmzbuf = lpfc_sli_get_buff(phba, pring,
1336 irsp->un.ulpWord[3]);
1337 lpfc_in_buf_free(phba, dmzbuf);
1338 }
1339
1340 if (irsp->ulpBdeCount > 1) {
1341 dmzbuf = lpfc_sli_get_buff(phba, pring,
1342 irsp->unsli3.sli3Words[3]);
1343 lpfc_in_buf_free(phba, dmzbuf);
1344 }
1345
1346 if (irsp->ulpBdeCount > 2) {
1347 dmzbuf = lpfc_sli_get_buff(phba, pring,
1348 irsp->unsli3.sli3Words[7]);
1349 lpfc_in_buf_free(phba, dmzbuf);
1350 }
1351
1352 return 1;
1353 }
1354
James Smart92d7f7b2007-06-17 19:56:38 -05001355 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001356 if (irsp->ulpBdeCount != 0) {
1357 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001358 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001359 if (!saveq->context2)
1360 lpfc_printf_log(phba,
1361 KERN_ERR,
1362 LOG_SLI,
1363 "0341 Ring %d Cannot find buffer for "
1364 "an unsolicited iocb. tag 0x%x\n",
1365 pring->ringno,
1366 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001367 }
1368 if (irsp->ulpBdeCount == 2) {
1369 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04001370 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04001371 if (!saveq->context3)
1372 lpfc_printf_log(phba,
1373 KERN_ERR,
1374 LOG_SLI,
1375 "0342 Ring %d Cannot find buffer for an"
1376 " unsolicited iocb. tag 0x%x\n",
1377 pring->ringno,
1378 irsp->unsli3.sli3Words[7]);
1379 }
1380 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04001381 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04001382 if (irsp->ulpBdeCount != 0) {
1383 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1384 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05001385 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04001386 lpfc_printf_log(phba,
1387 KERN_ERR,
1388 LOG_SLI,
1389 "0343 Ring %d Cannot find "
1390 "buffer for an unsolicited iocb"
1391 ". tag 0x%x\n", pring->ringno,
1392 irsp->un.ulpWord[3]);
1393 }
1394 if (irsp->ulpBdeCount == 2) {
1395 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1396 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05001397 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04001398 lpfc_printf_log(phba,
1399 KERN_ERR,
1400 LOG_SLI,
1401 "0344 Ring %d Cannot find "
1402 "buffer for an unsolicited "
1403 "iocb. tag 0x%x\n",
1404 pring->ringno,
1405 irsp->unsli3.sli3Words[7]);
1406 }
1407 }
James Smart92d7f7b2007-06-17 19:56:38 -05001408 }
James Smart9c2face2008-01-11 01:53:18 -05001409 if (irsp->ulpBdeCount != 0 &&
1410 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1411 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1412 int found = 0;
1413
1414 /* search continue save q for same XRI */
1415 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1416 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1417 list_add_tail(&saveq->list, &iocbq->list);
1418 found = 1;
1419 break;
1420 }
1421 }
1422 if (!found)
1423 list_add_tail(&saveq->clist,
1424 &pring->iocb_continue_saveq);
1425 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1426 list_del_init(&iocbq->clist);
1427 saveq = iocbq;
1428 irsp = &(saveq->iocb);
1429 } else
1430 return 0;
1431 }
1432 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1433 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1434 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1435 Rctl = FC_ELS_REQ;
1436 Type = FC_ELS_DATA;
1437 } else {
1438 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1439 Rctl = w5p->hcsw.Rctl;
1440 Type = w5p->hcsw.Type;
1441
1442 /* Firmware Workaround */
1443 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1444 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1445 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1446 Rctl = FC_ELS_REQ;
1447 Type = FC_ELS_DATA;
1448 w5p->hcsw.Rctl = Rctl;
1449 w5p->hcsw.Type = Type;
1450 }
1451 }
James Smart92d7f7b2007-06-17 19:56:38 -05001452
dea31012005-04-17 16:05:31 -05001453 /* unSolicited Responses */
1454 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001455 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1456 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1457 saveq);
dea31012005-04-17 16:05:31 -05001458 match = 1;
1459 } else {
1460 /* We must search, based on rctl / type
1461 for the right routine */
James Smart9c2face2008-01-11 01:53:18 -05001462 for (i = 0; i < pring->num_mask; i++) {
1463 if ((pring->prt[i].rctl == Rctl)
1464 && (pring->prt[i].type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001465 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1466 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1467 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -05001468 match = 1;
1469 break;
1470 }
1471 }
1472 }
1473 if (match == 0) {
1474 /* Unexpected Rctl / Type received */
1475 /* Ring <ringno> handler: unexpected
1476 Rctl <Rctl> Type <Type> received */
James Smart92d7f7b2007-06-17 19:56:38 -05001477 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001478 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001479 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001480 pring->ringno, Rctl, Type);
dea31012005-04-17 16:05:31 -05001481 }
James Smart92d7f7b2007-06-17 19:56:38 -05001482 return 1;
dea31012005-04-17 16:05:31 -05001483}
1484
James Smarte59058c2008-08-24 21:49:00 -04001485/**
1486 * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
1487 * @phba: Pointer to HBA context object.
1488 * @pring: Pointer to driver SLI ring object.
1489 * @prspiocb: Pointer to response iocb object.
1490 *
1491 * This function looks up the iocb_lookup table to get the command iocb
1492 * corresponding to the given response iocb using the iotag of the
1493 * response iocb. This function is called with the hbalock held.
1494 * This function returns the command iocb object if it finds the command
1495 * iocb else returns NULL.
1496 **/
dea31012005-04-17 16:05:31 -05001497static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001498lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1499 struct lpfc_sli_ring *pring,
1500 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001501{
dea31012005-04-17 16:05:31 -05001502 struct lpfc_iocbq *cmd_iocb = NULL;
1503 uint16_t iotag;
1504
James Bottomley604a3e32005-10-29 10:28:33 -05001505 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001506
James Bottomley604a3e32005-10-29 10:28:33 -05001507 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1508 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001509 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001510 pring->txcmplq_cnt--;
1511 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001512 }
1513
dea31012005-04-17 16:05:31 -05001514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001515 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001516 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001517 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001518 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001519 return NULL;
1520}
1521
James Smarte59058c2008-08-24 21:49:00 -04001522/**
1523 * lpfc_sli_process_sol_iocb: process solicited iocb completion.
1524 * @phba: Pointer to HBA context object.
1525 * @pring: Pointer to driver SLI ring object.
1526 * @saveq: Pointer to the response iocb to be processed.
1527 *
1528 * This function is called by the ring event handler for non-fcp
1529 * rings when there is a new response iocb in the response ring.
1530 * The caller is not required to hold any locks. This function
1531 * gets the command iocb associated with the response iocb and
1532 * calls the completion handler for the command iocb. If there
1533 * is no completion handler, the function will free the resources
1534 * associated with command iocb. If the response iocb is for
1535 * an already aborted command iocb, the status of the completion
1536 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1537 * This function always returns 1.
1538 **/
dea31012005-04-17 16:05:31 -05001539static int
James Smart2e0fef82007-06-17 19:56:36 -05001540lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001541 struct lpfc_iocbq *saveq)
1542{
James Smart2e0fef82007-06-17 19:56:36 -05001543 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001544 int rc = 1;
1545 unsigned long iflag;
1546
1547 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001548 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001549 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001550 spin_unlock_irqrestore(&phba->hbalock, iflag);
1551
dea31012005-04-17 16:05:31 -05001552 if (cmdiocbp) {
1553 if (cmdiocbp->iocb_cmpl) {
1554 /*
James Smartea2151b2008-09-07 11:52:10 -04001555 * If an ELS command failed send an event to mgmt
1556 * application.
1557 */
1558 if (saveq->iocb.ulpStatus &&
1559 (pring->ringno == LPFC_ELS_RING) &&
1560 (cmdiocbp->iocb.ulpCommand ==
1561 CMD_ELS_REQUEST64_CR))
1562 lpfc_send_els_failure_event(phba,
1563 cmdiocbp, saveq);
1564
1565 /*
dea31012005-04-17 16:05:31 -05001566 * Post all ELS completions to the worker thread.
1567 * All other are passed to the completion callback.
1568 */
1569 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001570 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1571 cmdiocbp->iocb_flag &=
1572 ~LPFC_DRIVER_ABORTED;
1573 saveq->iocb.ulpStatus =
1574 IOSTAT_LOCAL_REJECT;
1575 saveq->iocb.un.ulpWord[4] =
1576 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05001577
1578 /* Firmware could still be in progress
1579 * of DMAing payload, so don't free data
1580 * buffer till after a hbeat.
1581 */
1582 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04001583 }
dea31012005-04-17 16:05:31 -05001584 }
James Smart2e0fef82007-06-17 19:56:36 -05001585 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001586 } else
1587 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001588 } else {
1589 /*
1590 * Unknown initiating command based on the response iotag.
1591 * This could be the case on the ELS ring because of
1592 * lpfc_els_abort().
1593 */
1594 if (pring->ringno != LPFC_ELS_RING) {
1595 /*
1596 * Ring <ringno> handler: unexpected completion IoTag
1597 * <IoTag>
1598 */
James Smarte8b62012007-08-02 11:10:09 -04001599 lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1600 "0322 Ring %d handler: "
1601 "unexpected completion IoTag x%x "
1602 "Data: x%x x%x x%x x%x\n",
1603 pring->ringno,
1604 saveq->iocb.ulpIoTag,
1605 saveq->iocb.ulpStatus,
1606 saveq->iocb.un.ulpWord[4],
1607 saveq->iocb.ulpCommand,
1608 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001609 }
1610 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001611
dea31012005-04-17 16:05:31 -05001612 return rc;
1613}
1614
James Smarte59058c2008-08-24 21:49:00 -04001615/**
1616 * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
1617 * @phba: Pointer to HBA context object.
1618 * @pring: Pointer to driver SLI ring object.
1619 *
1620 * This function is called from the iocb ring event handlers when
1621 * put pointer is ahead of the get pointer for a ring. This function signal
1622 * an error attention condition to the worker thread and the worker
1623 * thread will transition the HBA to offline state.
1624 **/
James Smart2e0fef82007-06-17 19:56:36 -05001625static void
1626lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001627{
James Smart34b02dc2008-08-24 21:49:55 -04001628 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001629 /*
1630 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1631 * rsp ring <portRspMax>
1632 */
1633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001634 "0312 Ring %d handler: portRspPut %d "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001635 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001636 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001637 pring->numRiocb);
1638
James Smart2e0fef82007-06-17 19:56:36 -05001639 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001640
1641 /*
1642 * All error attention handlers are posted to
1643 * worker thread
1644 */
1645 phba->work_ha |= HA_ERATT;
1646 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001647
James Smart5e9d9b82008-06-14 22:52:53 -04001648 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001649
1650 return;
1651}
1652
James Smarte59058c2008-08-24 21:49:00 -04001653/**
James Smart93996272008-08-24 21:50:30 -04001654 * lpfc_poll_eratt: Error attention polling timer timeout handler.
1655 * @ptr: Pointer to address of HBA context object.
1656 *
1657 * This function is invoked by the Error Attention polling timer when the
1658 * timer times out. It will check the SLI Error Attention register for
1659 * possible attention events. If so, it will post an Error Attention event
1660 * and wake up worker thread to process it. Otherwise, it will set up the
1661 * Error Attention polling timer for the next poll.
1662 **/
1663void lpfc_poll_eratt(unsigned long ptr)
1664{
1665 struct lpfc_hba *phba;
1666 uint32_t eratt = 0;
1667
1668 phba = (struct lpfc_hba *)ptr;
1669
1670 /* Check chip HA register for error event */
1671 eratt = lpfc_sli_check_eratt(phba);
1672
1673 if (eratt)
1674 /* Tell the worker thread there is work to do */
1675 lpfc_worker_wake_up(phba);
1676 else
1677 /* Restart the timer for next eratt poll */
1678 mod_timer(&phba->eratt_poll, jiffies +
1679 HZ * LPFC_ERATT_POLL_INTERVAL);
1680 return;
1681}
1682
1683/**
James Smarte59058c2008-08-24 21:49:00 -04001684 * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
1685 * @phba: Pointer to HBA context object.
1686 *
1687 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1688 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1689 * is enabled.
1690 *
1691 * The caller does not hold any lock.
1692 * The function processes each response iocb in the response ring until it
1693 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1694 * LE bit set. The function will call the completion handler of the command iocb
1695 * if the response iocb indicates a completion for a command iocb or it is
1696 * an abort completion.
1697 **/
James Smart2e0fef82007-06-17 19:56:36 -05001698void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001699{
James Smart2e0fef82007-06-17 19:56:36 -05001700 struct lpfc_sli *psli = &phba->sli;
1701 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001702 IOCB_t *irsp = NULL;
1703 IOCB_t *entry = NULL;
1704 struct lpfc_iocbq *cmdiocbq = NULL;
1705 struct lpfc_iocbq rspiocbq;
James Smart34b02dc2008-08-24 21:49:55 -04001706 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001707 uint32_t status;
1708 uint32_t portRspPut, portRspMax;
1709 int type;
1710 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001711 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001712 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001713
1714 pring->stats.iocb_event++;
1715
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001716 /*
1717 * The next available response entry should never exceed the maximum
1718 * entries. If it does, treat it as an adapter hardware error.
1719 */
1720 portRspMax = pring->numRiocb;
1721 portRspPut = le32_to_cpu(pgp->rspPutInx);
1722 if (unlikely(portRspPut >= portRspMax)) {
1723 lpfc_sli_rsp_pointers_error(phba, pring);
1724 return;
1725 }
1726
1727 rmb();
1728 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001729 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001730 if (++pring->rspidx >= portRspMax)
1731 pring->rspidx = 0;
1732
1733 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1734 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001735 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001736 irsp = &rspiocbq.iocb;
1737 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1738 pring->stats.iocb_rsp++;
1739 rsp_cmpl++;
1740
1741 if (unlikely(irsp->ulpStatus)) {
1742 /* Rsp ring <ringno> error: IOCB */
1743 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001744 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001745 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001746 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001747 irsp->un.ulpWord[0],
1748 irsp->un.ulpWord[1],
1749 irsp->un.ulpWord[2],
1750 irsp->un.ulpWord[3],
1751 irsp->un.ulpWord[4],
1752 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001753 *(uint32_t *)&irsp->un1,
1754 *((uint32_t *)&irsp->un1 + 1));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001755 }
1756
1757 switch (type) {
1758 case LPFC_ABORT_IOCB:
1759 case LPFC_SOL_IOCB:
1760 /*
1761 * Idle exchange closed via ABTS from port. No iocb
1762 * resources need to be recovered.
1763 */
1764 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001765 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001766 "0314 IOCB cmd 0x%x "
1767 "processed. Skipping "
1768 "completion",
James Smartdca94792006-08-01 07:34:08 -04001769 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001770 break;
1771 }
1772
James Smart2e0fef82007-06-17 19:56:36 -05001773 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001774 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1775 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001776 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001777 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1778 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1779 &rspiocbq);
1780 }
1781 break;
1782 default:
1783 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1784 char adaptermsg[LPFC_MAX_ADPTMSG];
1785 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1786 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1787 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001788 dev_warn(&((phba->pcidev)->dev),
1789 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001790 phba->brd_no, adaptermsg);
1791 } else {
1792 /* Unknown IOCB command */
1793 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001794 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001795 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001796 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001797 irsp->ulpStatus,
1798 irsp->ulpIoTag,
1799 irsp->ulpContext);
1800 }
1801 break;
1802 }
1803
1804 /*
1805 * The response IOCB has been processed. Update the ring
1806 * pointer in SLIM. If the port response put pointer has not
1807 * been updated, sync the pgp->rspPutInx and fetch the new port
1808 * response put pointer.
1809 */
James Smarted957682007-06-17 19:56:37 -05001810 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001811
1812 if (pring->rspidx == portRspPut)
1813 portRspPut = le32_to_cpu(pgp->rspPutInx);
1814 }
1815
1816 ha_copy = readl(phba->HAregaddr);
1817 ha_copy >>= (LPFC_FCP_RING * 4);
1818
1819 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001820 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001821 pring->stats.iocb_rsp_full++;
1822 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1823 writel(status, phba->CAregaddr);
1824 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001825 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001826 }
1827 if ((ha_copy & HA_R0CE_RSP) &&
1828 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001829 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001830 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1831 pring->stats.iocb_cmd_empty++;
1832
1833 /* Force update of the local copy of cmdGetInx */
1834 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1835 lpfc_sli_resume_iocb(phba, pring);
1836
1837 if ((pring->lpfc_sli_cmd_available))
1838 (pring->lpfc_sli_cmd_available) (phba, pring);
1839
James Smart2e0fef82007-06-17 19:56:36 -05001840 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001841 }
1842
1843 return;
1844}
1845
James Smarte59058c2008-08-24 21:49:00 -04001846/**
1847 * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
1848 * @phba: Pointer to HBA context object.
1849 * @pring: Pointer to driver SLI ring object.
1850 * @mask: Host attention register mask for this ring.
1851 *
1852 * This function is called from the interrupt context when there is a ring
1853 * event for the fcp ring. The caller does not hold any lock.
1854 * The function processes each response iocb in the response ring until it
1855 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1856 * LE bit set. The function will call the completion handler of the command iocb
1857 * if the response iocb indicates a completion for a command iocb or it is
1858 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
1859 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05001860 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04001861 * to check it explicitly. This function always returns 1.
1862 **/
dea31012005-04-17 16:05:31 -05001863static int
James Smart2e0fef82007-06-17 19:56:36 -05001864lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1865 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001866{
James Smart34b02dc2008-08-24 21:49:55 -04001867 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001868 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001869 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001870 struct lpfc_iocbq *cmdiocbq = NULL;
1871 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001872 uint32_t status;
1873 uint32_t portRspPut, portRspMax;
1874 int rc = 1;
1875 lpfc_iocb_type type;
1876 unsigned long iflag;
1877 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05001878
James Smart2e0fef82007-06-17 19:56:36 -05001879 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001880 pring->stats.iocb_event++;
1881
dea31012005-04-17 16:05:31 -05001882 /*
1883 * The next available response entry should never exceed the maximum
1884 * entries. If it does, treat it as an adapter hardware error.
1885 */
1886 portRspMax = pring->numRiocb;
1887 portRspPut = le32_to_cpu(pgp->rspPutInx);
1888 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001889 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05001890 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001891 return 1;
1892 }
1893
1894 rmb();
1895 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001896 /*
1897 * Fetch an entry off the ring and copy it into a local data
1898 * structure. The copy involves a byte-swap since the
1899 * network byte order and pci byte orders are different.
1900 */
James Smarted957682007-06-17 19:56:37 -05001901 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05001902 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001903
1904 if (++pring->rspidx >= portRspMax)
1905 pring->rspidx = 0;
1906
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001907 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1908 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05001909 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05001910 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001911 irsp = &rspiocbq.iocb;
1912
dea31012005-04-17 16:05:31 -05001913 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1914 pring->stats.iocb_rsp++;
1915 rsp_cmpl++;
1916
1917 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001918 /*
1919 * If resource errors reported from HBA, reduce
1920 * queuedepths of the SCSI device.
1921 */
1922 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1923 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1924 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarteaf15d52008-12-04 22:39:29 -05001925 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001926 spin_lock_irqsave(&phba->hbalock, iflag);
1927 }
1928
dea31012005-04-17 16:05:31 -05001929 /* Rsp ring <ringno> error: IOCB */
1930 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001931 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05001932 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001933 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05001934 irsp->un.ulpWord[0],
1935 irsp->un.ulpWord[1],
1936 irsp->un.ulpWord[2],
1937 irsp->un.ulpWord[3],
1938 irsp->un.ulpWord[4],
1939 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001940 *(uint32_t *)&irsp->un1,
1941 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05001942 }
1943
1944 switch (type) {
1945 case LPFC_ABORT_IOCB:
1946 case LPFC_SOL_IOCB:
1947 /*
1948 * Idle exchange closed via ABTS from port. No iocb
1949 * resources need to be recovered.
1950 */
1951 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001952 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001953 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04001954 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05001955 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04001956 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05001957 break;
1958 }
1959
James Bottomley604a3e32005-10-29 10:28:33 -05001960 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1961 &rspiocbq);
dea31012005-04-17 16:05:31 -05001962 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001963 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1964 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1965 &rspiocbq);
1966 } else {
James Smart2e0fef82007-06-17 19:56:36 -05001967 spin_unlock_irqrestore(&phba->hbalock,
1968 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001969 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1970 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001971 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001972 iflag);
1973 }
dea31012005-04-17 16:05:31 -05001974 }
1975 break;
James Smarta4bc3372006-12-02 13:34:16 -05001976 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05001977 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001978 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001979 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001980 break;
dea31012005-04-17 16:05:31 -05001981 default:
1982 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1983 char adaptermsg[LPFC_MAX_ADPTMSG];
1984 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1985 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1986 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001987 dev_warn(&((phba->pcidev)->dev),
1988 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05001989 phba->brd_no, adaptermsg);
1990 } else {
1991 /* Unknown IOCB command */
1992 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001993 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05001994 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001995 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05001996 irsp->ulpStatus,
1997 irsp->ulpIoTag,
1998 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05001999 }
2000 break;
2001 }
2002
2003 /*
2004 * The response IOCB has been processed. Update the ring
2005 * pointer in SLIM. If the port response put pointer has not
2006 * been updated, sync the pgp->rspPutInx and fetch the new port
2007 * response put pointer.
2008 */
James Smarted957682007-06-17 19:56:37 -05002009 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002010
2011 if (pring->rspidx == portRspPut)
2012 portRspPut = le32_to_cpu(pgp->rspPutInx);
2013 }
2014
2015 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2016 pring->stats.iocb_rsp_full++;
2017 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2018 writel(status, phba->CAregaddr);
2019 readl(phba->CAregaddr);
2020 }
2021 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2022 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2023 pring->stats.iocb_cmd_empty++;
2024
2025 /* Force update of the local copy of cmdGetInx */
2026 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2027 lpfc_sli_resume_iocb(phba, pring);
2028
2029 if ((pring->lpfc_sli_cmd_available))
2030 (pring->lpfc_sli_cmd_available) (phba, pring);
2031
2032 }
2033
James Smart2e0fef82007-06-17 19:56:36 -05002034 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002035 return rc;
2036}
2037
James Smarte59058c2008-08-24 21:49:00 -04002038/**
2039 * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
2040 * @phba: Pointer to HBA context object.
2041 * @pring: Pointer to driver SLI ring object.
2042 * @mask: Host attention register mask for this ring.
2043 *
2044 * This function is called from the worker thread when there is a ring
2045 * event for non-fcp rings. The caller does not hold any lock .
2046 * The function processes each response iocb in the response ring until it
2047 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2048 * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
2049 * response iocb indicates a completion of a command iocb. The function
2050 * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
2051 * iocb. The function frees the resources or calls the completion handler if
2052 * this iocb is an abort completion. The function returns 0 when the allocated
2053 * iocbs are not freed, otherwise returns 1.
2054 **/
dea31012005-04-17 16:05:31 -05002055int
James Smart2e0fef82007-06-17 19:56:36 -05002056lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2057 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002058{
James Smart34b02dc2008-08-24 21:49:55 -04002059 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002060 IOCB_t *entry;
2061 IOCB_t *irsp = NULL;
2062 struct lpfc_iocbq *rspiocbp = NULL;
2063 struct lpfc_iocbq *next_iocb;
2064 struct lpfc_iocbq *cmdiocbp;
2065 struct lpfc_iocbq *saveq;
dea31012005-04-17 16:05:31 -05002066 uint8_t iocb_cmd_type;
2067 lpfc_iocb_type type;
2068 uint32_t status, free_saveq;
2069 uint32_t portRspPut, portRspMax;
2070 int rc = 1;
2071 unsigned long iflag;
dea31012005-04-17 16:05:31 -05002072
James Smart34b02dc2008-08-24 21:49:55 -04002073 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002074 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002075 pring->stats.iocb_event++;
2076
dea31012005-04-17 16:05:31 -05002077 /*
2078 * The next available response entry should never exceed the maximum
2079 * entries. If it does, treat it as an adapter hardware error.
2080 */
2081 portRspMax = pring->numRiocb;
2082 portRspPut = le32_to_cpu(pgp->rspPutInx);
2083 if (portRspPut >= portRspMax) {
2084 /*
2085 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
2086 * rsp ring <portRspMax>
2087 */
James Smarted957682007-06-17 19:56:37 -05002088 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002089 "0303 Ring %d handler: portRspPut %d "
dea31012005-04-17 16:05:31 -05002090 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002091 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002092
James Smart2e0fef82007-06-17 19:56:36 -05002093 phba->link_state = LPFC_HBA_ERROR;
2094 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002095
2096 phba->work_hs = HS_FFER3;
2097 lpfc_handle_eratt(phba);
2098
2099 return 1;
2100 }
2101
2102 rmb();
dea31012005-04-17 16:05:31 -05002103 while (pring->rspidx != portRspPut) {
2104 /*
2105 * Build a completion list and call the appropriate handler.
2106 * The process is to get the next available response iocb, get
2107 * a free iocb from the list, copy the response data into the
2108 * free iocb, insert to the continuation list, and update the
2109 * next response index to slim. This process makes response
2110 * iocb's in the ring available to DMA as fast as possible but
2111 * pays a penalty for a copy operation. Since the iocb is
2112 * only 32 bytes, this penalty is considered small relative to
2113 * the PCI reads for register values and a slim write. When
2114 * the ulpLe field is set, the entire Command has been
2115 * received.
2116 */
James Smarted957682007-06-17 19:56:37 -05002117 entry = lpfc_resp_iocb(phba, pring);
2118
James Smart858c9f62007-06-17 19:56:39 -05002119 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002120 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002121 if (rspiocbp == NULL) {
2122 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002123 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002124 break;
2125 }
2126
James Smarted957682007-06-17 19:56:37 -05002127 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2128 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002129 irsp = &rspiocbp->iocb;
2130
2131 if (++pring->rspidx >= portRspMax)
2132 pring->rspidx = 0;
2133
James Smarta58cbd52007-08-02 11:09:43 -04002134 if (pring->ringno == LPFC_ELS_RING) {
2135 lpfc_debugfs_slow_ring_trc(phba,
2136 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2137 *(((uint32_t *) irsp) + 4),
2138 *(((uint32_t *) irsp) + 6),
2139 *(((uint32_t *) irsp) + 7));
2140 }
2141
James Smarted957682007-06-17 19:56:37 -05002142 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002143
James Smart9c2face2008-01-11 01:53:18 -05002144 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
dea31012005-04-17 16:05:31 -05002145
2146 pring->iocb_continueq_cnt++;
2147 if (irsp->ulpLe) {
2148 /*
2149 * By default, the driver expects to free all resources
2150 * associated with this iocb completion.
2151 */
2152 free_saveq = 1;
2153 saveq = list_get_first(&pring->iocb_continueq,
2154 struct lpfc_iocbq, list);
2155 irsp = &(saveq->iocb);
2156 list_del_init(&pring->iocb_continueq);
2157 pring->iocb_continueq_cnt = 0;
2158
2159 pring->stats.iocb_rsp++;
2160
James Smart92d7f7b2007-06-17 19:56:38 -05002161 /*
2162 * If resource errors reported from HBA, reduce
2163 * queuedepths of the SCSI device.
2164 */
2165 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2166 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2167 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarteaf15d52008-12-04 22:39:29 -05002168 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002169 spin_lock_irqsave(&phba->hbalock, iflag);
2170 }
2171
dea31012005-04-17 16:05:31 -05002172 if (irsp->ulpStatus) {
2173 /* Rsp ring <ringno> error: IOCB */
James Smarted957682007-06-17 19:56:37 -05002174 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002175 "0328 Rsp Ring %d error: "
James Smarted957682007-06-17 19:56:37 -05002176 "IOCB Data: "
2177 "x%x x%x x%x x%x "
2178 "x%x x%x x%x x%x "
2179 "x%x x%x x%x x%x "
2180 "x%x x%x x%x x%x\n",
James Smarted957682007-06-17 19:56:37 -05002181 pring->ringno,
2182 irsp->un.ulpWord[0],
2183 irsp->un.ulpWord[1],
2184 irsp->un.ulpWord[2],
2185 irsp->un.ulpWord[3],
2186 irsp->un.ulpWord[4],
2187 irsp->un.ulpWord[5],
2188 *(((uint32_t *) irsp) + 6),
2189 *(((uint32_t *) irsp) + 7),
2190 *(((uint32_t *) irsp) + 8),
2191 *(((uint32_t *) irsp) + 9),
2192 *(((uint32_t *) irsp) + 10),
2193 *(((uint32_t *) irsp) + 11),
2194 *(((uint32_t *) irsp) + 12),
2195 *(((uint32_t *) irsp) + 13),
2196 *(((uint32_t *) irsp) + 14),
2197 *(((uint32_t *) irsp) + 15));
dea31012005-04-17 16:05:31 -05002198 }
2199
2200 /*
2201 * Fetch the IOCB command type and call the correct
2202 * completion routine. Solicited and Unsolicited
2203 * IOCBs on the ELS ring get freed back to the
2204 * lpfc_iocb_list by the discovery kernel thread.
2205 */
2206 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2207 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2208 if (type == LPFC_SOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002209 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002210 rc = lpfc_sli_process_sol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002211 saveq);
2212 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002213 } else if (type == LPFC_UNSOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002214 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002215 rc = lpfc_sli_process_unsol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002216 saveq);
2217 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9c2face2008-01-11 01:53:18 -05002218 if (!rc)
2219 free_saveq = 0;
dea31012005-04-17 16:05:31 -05002220 } else if (type == LPFC_ABORT_IOCB) {
2221 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
2222 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05002223 lpfc_sli_iocbq_lookup(phba, pring,
2224 saveq)))) {
dea31012005-04-17 16:05:31 -05002225 /* Call the specified completion
2226 routine */
2227 if (cmdiocbp->iocb_cmpl) {
2228 spin_unlock_irqrestore(
James Smart2e0fef82007-06-17 19:56:36 -05002229 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002230 iflag);
2231 (cmdiocbp->iocb_cmpl) (phba,
2232 cmdiocbp, saveq);
2233 spin_lock_irqsave(
James Smart2e0fef82007-06-17 19:56:36 -05002234 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002235 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002236 } else
James Smart2e0fef82007-06-17 19:56:36 -05002237 __lpfc_sli_release_iocbq(phba,
James Bottomley604a3e32005-10-29 10:28:33 -05002238 cmdiocbp);
dea31012005-04-17 16:05:31 -05002239 }
2240 } else if (type == LPFC_UNKNOWN_IOCB) {
2241 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2242
2243 char adaptermsg[LPFC_MAX_ADPTMSG];
2244
2245 memset(adaptermsg, 0,
2246 LPFC_MAX_ADPTMSG);
2247 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2248 MAX_MSG_DATA);
2249 dev_warn(&((phba->pcidev)->dev),
Joe Perches898eb712007-10-18 03:06:30 -07002250 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002251 phba->brd_no, adaptermsg);
2252 } else {
2253 /* Unknown IOCB command */
James Smart92d7f7b2007-06-17 19:56:38 -05002254 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002255 "0335 Unknown IOCB "
James Smart92d7f7b2007-06-17 19:56:38 -05002256 "command Data: x%x "
2257 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002258 irsp->ulpCommand,
2259 irsp->ulpStatus,
2260 irsp->ulpIoTag,
2261 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002262 }
2263 }
2264
2265 if (free_saveq) {
James Smart2e0fef82007-06-17 19:56:36 -05002266 list_for_each_entry_safe(rspiocbp, next_iocb,
2267 &saveq->list, list) {
2268 list_del(&rspiocbp->list);
2269 __lpfc_sli_release_iocbq(phba,
2270 rspiocbp);
dea31012005-04-17 16:05:31 -05002271 }
James Smart2e0fef82007-06-17 19:56:36 -05002272 __lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05002273 }
James Smart92d7f7b2007-06-17 19:56:38 -05002274 rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002275 }
2276
2277 /*
2278 * If the port response put pointer has not been updated, sync
2279 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2280 * response put pointer.
2281 */
2282 if (pring->rspidx == portRspPut) {
2283 portRspPut = le32_to_cpu(pgp->rspPutInx);
2284 }
2285 } /* while (pring->rspidx != portRspPut) */
2286
James Smart92d7f7b2007-06-17 19:56:38 -05002287 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002288 /* At least one response entry has been freed */
2289 pring->stats.iocb_rsp_full++;
2290 /* SET RxRE_RSP in Chip Att register */
2291 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2292 writel(status, phba->CAregaddr);
2293 readl(phba->CAregaddr); /* flush */
2294 }
2295 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2296 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2297 pring->stats.iocb_cmd_empty++;
2298
2299 /* Force update of the local copy of cmdGetInx */
2300 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2301 lpfc_sli_resume_iocb(phba, pring);
2302
2303 if ((pring->lpfc_sli_cmd_available))
2304 (pring->lpfc_sli_cmd_available) (phba, pring);
2305
2306 }
2307
James Smart2e0fef82007-06-17 19:56:36 -05002308 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002309 return rc;
2310}
2311
James Smarte59058c2008-08-24 21:49:00 -04002312/**
2313 * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
2314 * @phba: Pointer to HBA context object.
2315 * @pring: Pointer to driver SLI ring object.
2316 *
2317 * This function aborts all iocbs in the given ring and frees all the iocb
2318 * objects in txq. This function issues an abort iocb for all the iocb commands
2319 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2320 * the return of this function. The caller is not required to hold any locks.
2321 **/
James Smart2e0fef82007-06-17 19:56:36 -05002322void
dea31012005-04-17 16:05:31 -05002323lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2324{
James Smart2534ba72007-04-25 09:52:20 -04002325 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002326 struct lpfc_iocbq *iocb, *next_iocb;
James Smart2534ba72007-04-25 09:52:20 -04002327 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05002328
James Smart92d7f7b2007-06-17 19:56:38 -05002329 if (pring->ringno == LPFC_ELS_RING) {
2330 lpfc_fabric_abort_hba(phba);
2331 }
2332
dea31012005-04-17 16:05:31 -05002333 /* Error everything on txq and txcmplq
2334 * First do the txq.
2335 */
James Smart2e0fef82007-06-17 19:56:36 -05002336 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002337 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002338 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002339
2340 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002341 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2342 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2343
James Smart2e0fef82007-06-17 19:56:36 -05002344 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002345
2346 while (!list_empty(&completions)) {
2347 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
dea31012005-04-17 16:05:31 -05002348 cmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05002349 list_del_init(&iocb->list);
dea31012005-04-17 16:05:31 -05002350
James Smart2e0fef82007-06-17 19:56:36 -05002351 if (!iocb->iocb_cmpl)
2352 lpfc_sli_release_iocbq(phba, iocb);
2353 else {
dea31012005-04-17 16:05:31 -05002354 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2355 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
dea31012005-04-17 16:05:31 -05002356 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002357 }
dea31012005-04-17 16:05:31 -05002358 }
dea31012005-04-17 16:05:31 -05002359}
2360
James Smarte59058c2008-08-24 21:49:00 -04002361/**
James Smarta8e497d2008-08-24 21:50:11 -04002362 * lpfc_sli_flush_fcp_rings: flush all iocbs in the fcp ring.
2363 * @phba: Pointer to HBA context object.
2364 *
2365 * This function flushes all iocbs in the fcp ring and frees all the iocb
2366 * objects in txq and txcmplq. This function will not issue abort iocbs
2367 * for all the iocb commands in txcmplq, they will just be returned with
2368 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2369 * slot has been permanently disabled.
2370 **/
2371void
2372lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2373{
2374 LIST_HEAD(txq);
2375 LIST_HEAD(txcmplq);
2376 struct lpfc_iocbq *iocb;
2377 IOCB_t *cmd = NULL;
2378 struct lpfc_sli *psli = &phba->sli;
2379 struct lpfc_sli_ring *pring;
2380
2381 /* Currently, only one fcp ring */
2382 pring = &psli->ring[psli->fcp_ring];
2383
2384 spin_lock_irq(&phba->hbalock);
2385 /* Retrieve everything on txq */
2386 list_splice_init(&pring->txq, &txq);
2387 pring->txq_cnt = 0;
2388
2389 /* Retrieve everything on the txcmplq */
2390 list_splice_init(&pring->txcmplq, &txcmplq);
2391 pring->txcmplq_cnt = 0;
2392 spin_unlock_irq(&phba->hbalock);
2393
2394 /* Flush the txq */
2395 while (!list_empty(&txq)) {
2396 iocb = list_get_first(&txq, struct lpfc_iocbq, list);
2397 cmd = &iocb->iocb;
2398 list_del_init(&iocb->list);
2399
2400 if (!iocb->iocb_cmpl)
2401 lpfc_sli_release_iocbq(phba, iocb);
2402 else {
2403 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2404 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2405 (iocb->iocb_cmpl) (phba, iocb, iocb);
2406 }
2407 }
2408
2409 /* Flush the txcmpq */
2410 while (!list_empty(&txcmplq)) {
2411 iocb = list_get_first(&txcmplq, struct lpfc_iocbq, list);
2412 cmd = &iocb->iocb;
2413 list_del_init(&iocb->list);
2414
2415 if (!iocb->iocb_cmpl)
2416 lpfc_sli_release_iocbq(phba, iocb);
2417 else {
2418 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2419 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2420 (iocb->iocb_cmpl) (phba, iocb, iocb);
2421 }
2422 }
2423}
2424
2425/**
James Smarte59058c2008-08-24 21:49:00 -04002426 * lpfc_sli_brdready: Check for host status bits.
2427 * @phba: Pointer to HBA context object.
2428 * @mask: Bit mask to be checked.
2429 *
2430 * This function reads the host status register and compares
2431 * with the provided bit mask to check if HBA completed
2432 * the restart. This function will wait in a loop for the
2433 * HBA to complete restart. If the HBA does not restart within
2434 * 15 iterations, the function will reset the HBA again. The
2435 * function returns 1 when HBA fail to restart otherwise returns
2436 * zero.
2437 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002438int
James Smart2e0fef82007-06-17 19:56:36 -05002439lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05002440{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002441 uint32_t status;
2442 int i = 0;
2443 int retval = 0;
dea31012005-04-17 16:05:31 -05002444
Jamie Wellnitz41415862006-02-28 19:25:27 -05002445 /* Read the HBA Host Status Register */
2446 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002447
Jamie Wellnitz41415862006-02-28 19:25:27 -05002448 /*
2449 * Check status register every 100ms for 5 retries, then every
2450 * 500ms for 5, then every 2.5 sec for 5, then reset board and
2451 * every 2.5 sec for 4.
2452 * Break our of the loop if errors occurred during init.
2453 */
2454 while (((status & mask) != mask) &&
2455 !(status & HS_FFERM) &&
2456 i++ < 20) {
dea31012005-04-17 16:05:31 -05002457
Jamie Wellnitz41415862006-02-28 19:25:27 -05002458 if (i <= 5)
2459 msleep(10);
2460 else if (i <= 10)
2461 msleep(500);
2462 else
2463 msleep(2500);
dea31012005-04-17 16:05:31 -05002464
Jamie Wellnitz41415862006-02-28 19:25:27 -05002465 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002466 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002467 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002468 lpfc_sli_brdrestart(phba);
2469 }
2470 /* Read the HBA Host Status Register */
2471 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002472 }
dea31012005-04-17 16:05:31 -05002473
Jamie Wellnitz41415862006-02-28 19:25:27 -05002474 /* Check to see if any errors occurred during init */
2475 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05002476 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002477 retval = 1;
2478 }
dea31012005-04-17 16:05:31 -05002479
Jamie Wellnitz41415862006-02-28 19:25:27 -05002480 return retval;
dea31012005-04-17 16:05:31 -05002481}
2482
James Smart92908312006-03-07 15:04:13 -05002483#define BARRIER_TEST_PATTERN (0xdeadbeef)
2484
James Smarte59058c2008-08-24 21:49:00 -04002485/**
2486 * lpfc_reset_barrier: Make HBA ready for HBA reset.
2487 * @phba: Pointer to HBA context object.
2488 *
2489 * This function is called before resetting an HBA. This
2490 * function requests HBA to quiesce DMAs before a reset.
2491 **/
James Smart2e0fef82007-06-17 19:56:36 -05002492void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05002493{
James Smart65a29c12006-07-06 15:50:50 -04002494 uint32_t __iomem *resp_buf;
2495 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05002496 volatile uint32_t mbox;
2497 uint32_t hc_copy;
2498 int i;
2499 uint8_t hdrtype;
2500
2501 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2502 if (hdrtype != 0x80 ||
2503 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2504 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2505 return;
2506
2507 /*
2508 * Tell the other part of the chip to suspend temporarily all
2509 * its DMA activity.
2510 */
James Smart65a29c12006-07-06 15:50:50 -04002511 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002512
2513 /* Disable the error attention */
2514 hc_copy = readl(phba->HCregaddr);
2515 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2516 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002517 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002518
2519 if (readl(phba->HAregaddr) & HA_ERATT) {
2520 /* Clear Chip error bit */
2521 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002522 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002523 }
2524
2525 mbox = 0;
2526 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2527 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2528
2529 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04002530 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002531 writel(mbox, mbox_buf);
2532
2533 for (i = 0;
2534 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2535 mdelay(1);
2536
2537 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2538 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05002539 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05002540 goto restore_hc;
2541 else
2542 goto clear_errat;
2543 }
2544
2545 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2546 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
2547 mdelay(1);
2548
2549clear_errat:
2550
2551 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2552 mdelay(1);
2553
2554 if (readl(phba->HAregaddr) & HA_ERATT) {
2555 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002556 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002557 }
2558
2559restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05002560 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002561 writel(hc_copy, phba->HCregaddr);
2562 readl(phba->HCregaddr); /* flush */
2563}
2564
James Smarte59058c2008-08-24 21:49:00 -04002565/**
2566 * lpfc_sli_brdkill: Issue a kill_board mailbox command.
2567 * @phba: Pointer to HBA context object.
2568 *
2569 * This function issues a kill_board mailbox command and waits for
2570 * the error attention interrupt. This function is called for stopping
2571 * the firmware processing. The caller is not required to hold any
2572 * locks. This function calls lpfc_hba_down_post function to free
2573 * any pending commands after the kill. The function will return 1 when it
2574 * fails to kill the board else will return 0.
2575 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002576int
James Smart2e0fef82007-06-17 19:56:36 -05002577lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002578{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002579 struct lpfc_sli *psli;
2580 LPFC_MBOXQ_t *pmb;
2581 uint32_t status;
2582 uint32_t ha_copy;
2583 int retval;
2584 int i = 0;
2585
2586 psli = &phba->sli;
2587
2588 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05002589 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002590 "0329 Kill HBA Data: x%x x%x\n",
2591 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002592
James Smart98c9ea52007-10-27 13:37:33 -04002593 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2594 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002595 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002596
2597 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05002598 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002599 status = readl(phba->HCregaddr);
2600 status &= ~HC_ERINT_ENA;
2601 writel(status, phba->HCregaddr);
2602 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002603 phba->link_flag |= LS_IGNORE_ERATT;
2604 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002605
2606 lpfc_kill_board(phba, pmb);
2607 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2608 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2609
2610 if (retval != MBX_SUCCESS) {
2611 if (retval != MBX_BUSY)
2612 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002613 spin_lock_irq(&phba->hbalock);
2614 phba->link_flag &= ~LS_IGNORE_ERATT;
2615 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002616 return 1;
2617 }
2618
James Smart92908312006-03-07 15:04:13 -05002619 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2620
Jamie Wellnitz41415862006-02-28 19:25:27 -05002621 mempool_free(pmb, phba->mbox_mem_pool);
2622
2623 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2624 * attention every 100ms for 3 seconds. If we don't get ERATT after
2625 * 3 seconds we still set HBA_ERROR state because the status of the
2626 * board is now undefined.
2627 */
2628 ha_copy = readl(phba->HAregaddr);
2629
2630 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2631 mdelay(100);
2632 ha_copy = readl(phba->HAregaddr);
2633 }
2634
2635 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05002636 if (ha_copy & HA_ERATT) {
2637 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002638 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002639 }
James Smart2e0fef82007-06-17 19:56:36 -05002640 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002641 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002642 phba->link_flag &= ~LS_IGNORE_ERATT;
2643 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002644
2645 psli->mbox_active = NULL;
2646 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002647 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002648
James Smart2e0fef82007-06-17 19:56:36 -05002649 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002650}
2651
James Smarte59058c2008-08-24 21:49:00 -04002652/**
2653 * lpfc_sli_brdreset: Reset the HBA.
2654 * @phba: Pointer to HBA context object.
2655 *
2656 * This function resets the HBA by writing HC_INITFF to the control
2657 * register. After the HBA resets, this function resets all the iocb ring
2658 * indices. This function disables PCI layer parity checking during
2659 * the reset.
2660 * This function returns 0 always.
2661 * The caller is not required to hold any locks.
2662 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002663int
James Smart2e0fef82007-06-17 19:56:36 -05002664lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002665{
2666 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002667 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002668 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05002669 int i;
dea31012005-04-17 16:05:31 -05002670
Jamie Wellnitz41415862006-02-28 19:25:27 -05002671 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002672
Jamie Wellnitz41415862006-02-28 19:25:27 -05002673 /* Reset HBA */
2674 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002675 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002676 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05002677
2678 /* perform board reset */
2679 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002680 phba->pport->fc_myDID = 0;
2681 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002682
Jamie Wellnitz41415862006-02-28 19:25:27 -05002683 /* Turn off parity checking and serr during the physical reset */
2684 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2685 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2686 (cfg_value &
2687 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2688
James Smart1c067a42006-08-01 07:33:52 -04002689 psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002690 /* Now toggle INITFF bit in the Host Control Register */
2691 writel(HC_INITFF, phba->HCregaddr);
2692 mdelay(1);
2693 readl(phba->HCregaddr); /* flush */
2694 writel(0, phba->HCregaddr);
2695 readl(phba->HCregaddr); /* flush */
2696
2697 /* Restore PCI cmd register */
2698 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002699
2700 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002701 for (i = 0; i < psli->num_rings; i++) {
2702 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002703 pring->flag = 0;
2704 pring->rspidx = 0;
2705 pring->next_cmdidx = 0;
2706 pring->local_getidx = 0;
2707 pring->cmdidx = 0;
2708 pring->missbufcnt = 0;
2709 }
dea31012005-04-17 16:05:31 -05002710
James Smart2e0fef82007-06-17 19:56:36 -05002711 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002712 return 0;
2713}
2714
James Smarte59058c2008-08-24 21:49:00 -04002715/**
2716 * lpfc_sli_brdrestart: Restart the HBA.
2717 * @phba: Pointer to HBA context object.
2718 *
2719 * This function is called in the SLI initialization code path to
2720 * restart the HBA. The caller is not required to hold any lock.
2721 * This function writes MBX_RESTART mailbox command to the SLIM and
2722 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
2723 * function to free any pending commands. The function enables
2724 * POST only during the first initialization. The function returns zero.
2725 * The function does not guarantee completion of MBX_RESTART mailbox
2726 * command before the return of this function.
2727 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002728int
James Smart2e0fef82007-06-17 19:56:36 -05002729lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002730{
2731 MAILBOX_t *mb;
2732 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002733 volatile uint32_t word0;
2734 void __iomem *to_slim;
2735
James Smart2e0fef82007-06-17 19:56:36 -05002736 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002737
2738 psli = &phba->sli;
2739
2740 /* Restart HBA */
2741 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002742 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002743 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002744
2745 word0 = 0;
2746 mb = (MAILBOX_t *) &word0;
2747 mb->mbxCommand = MBX_RESTART;
2748 mb->mbxHc = 1;
2749
James Smart92908312006-03-07 15:04:13 -05002750 lpfc_reset_barrier(phba);
2751
Jamie Wellnitz41415862006-02-28 19:25:27 -05002752 to_slim = phba->MBslimaddr;
2753 writel(*(uint32_t *) mb, to_slim);
2754 readl(to_slim); /* flush */
2755
2756 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05002757 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002758 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05002759 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05002760 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04002761 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002762 writel(*(uint32_t *) mb, to_slim);
2763 readl(to_slim); /* flush */
2764
2765 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002766 phba->pport->stopped = 0;
2767 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002768
James Smart2e0fef82007-06-17 19:56:36 -05002769 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002770
James Smart64ba8812006-08-02 15:24:34 -04002771 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2772 psli->stats_start = get_seconds();
2773
James Smarteaf15d52008-12-04 22:39:29 -05002774 /* Give the INITFF and Post time to settle. */
2775 mdelay(100);
dea31012005-04-17 16:05:31 -05002776
Jamie Wellnitz41415862006-02-28 19:25:27 -05002777 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002778
2779 return 0;
2780}
2781
James Smarte59058c2008-08-24 21:49:00 -04002782/**
2783 * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
2784 * @phba: Pointer to HBA context object.
2785 *
2786 * This function is called after a HBA restart to wait for successful
2787 * restart of the HBA. Successful restart of the HBA is indicated by
2788 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
2789 * iteration, the function will restart the HBA again. The function returns
2790 * zero if HBA successfully restarted else returns negative error code.
2791 **/
dea31012005-04-17 16:05:31 -05002792static int
2793lpfc_sli_chipset_init(struct lpfc_hba *phba)
2794{
2795 uint32_t status, i = 0;
2796
2797 /* Read the HBA Host Status Register */
2798 status = readl(phba->HSregaddr);
2799
2800 /* Check status register to see what current state is */
2801 i = 0;
2802 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2803
2804 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2805 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2806 * 4.
2807 */
2808 if (i++ >= 20) {
2809 /* Adapter failed to init, timeout, status reg
2810 <status> */
James Smarted957682007-06-17 19:56:37 -05002811 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002812 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002813 "timeout, status reg x%x, "
2814 "FW Data: A8 x%x AC x%x\n", status,
2815 readl(phba->MBslimaddr + 0xa8),
2816 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002817 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002818 return -ETIMEDOUT;
2819 }
2820
2821 /* Check to see if any errors occurred during init */
2822 if (status & HS_FFERM) {
2823 /* ERROR: During chipset initialization */
2824 /* Adapter failed to init, chipset, status reg
2825 <status> */
James Smarted957682007-06-17 19:56:37 -05002826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002827 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002828 "chipset, status reg x%x, "
2829 "FW Data: A8 x%x AC x%x\n", status,
2830 readl(phba->MBslimaddr + 0xa8),
2831 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002832 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002833 return -EIO;
2834 }
2835
2836 if (i <= 5) {
2837 msleep(10);
2838 } else if (i <= 10) {
2839 msleep(500);
2840 } else {
2841 msleep(2500);
2842 }
2843
2844 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002845 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002846 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002847 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002848 }
2849 /* Read the HBA Host Status Register */
2850 status = readl(phba->HSregaddr);
2851 }
2852
2853 /* Check to see if any errors occurred during init */
2854 if (status & HS_FFERM) {
2855 /* ERROR: During chipset initialization */
2856 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05002857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002858 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05002859 "status reg x%x, "
2860 "FW Data: A8 x%x AC x%x\n", status,
2861 readl(phba->MBslimaddr + 0xa8),
2862 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002863 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002864 return -EIO;
2865 }
2866
2867 /* Clear all interrupt enable conditions */
2868 writel(0, phba->HCregaddr);
2869 readl(phba->HCregaddr); /* flush */
2870
2871 /* setup host attn register */
2872 writel(0xffffffff, phba->HAregaddr);
2873 readl(phba->HAregaddr); /* flush */
2874 return 0;
2875}
2876
James Smarte59058c2008-08-24 21:49:00 -04002877/**
2878 * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
2879 *
2880 * This function calculates and returns the number of HBQs required to be
2881 * configured.
2882 **/
James Smart78b2d852007-08-02 11:10:21 -04002883int
James Smarted957682007-06-17 19:56:37 -05002884lpfc_sli_hbq_count(void)
2885{
James Smart92d7f7b2007-06-17 19:56:38 -05002886 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05002887}
2888
James Smarte59058c2008-08-24 21:49:00 -04002889/**
2890 * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
2891 *
2892 * This function adds the number of hbq entries in every HBQ to get
2893 * the total number of hbq entries required for the HBA and returns
2894 * the total count.
2895 **/
James Smarted957682007-06-17 19:56:37 -05002896static int
2897lpfc_sli_hbq_entry_count(void)
2898{
2899 int hbq_count = lpfc_sli_hbq_count();
2900 int count = 0;
2901 int i;
2902
2903 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05002904 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05002905 return count;
2906}
2907
James Smarte59058c2008-08-24 21:49:00 -04002908/**
2909 * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
2910 *
2911 * This function calculates amount of memory required for all hbq entries
2912 * to be configured and returns the total memory required.
2913 **/
dea31012005-04-17 16:05:31 -05002914int
James Smarted957682007-06-17 19:56:37 -05002915lpfc_sli_hbq_size(void)
2916{
2917 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2918}
2919
James Smarte59058c2008-08-24 21:49:00 -04002920/**
2921 * lpfc_sli_hbq_setup: configure and initialize HBQs.
2922 * @phba: Pointer to HBA context object.
2923 *
2924 * This function is called during the SLI initialization to configure
2925 * all the HBQs and post buffers to the HBQ. The caller is not
2926 * required to hold any locks. This function will return zero if successful
2927 * else it will return negative error code.
2928 **/
James Smarted957682007-06-17 19:56:37 -05002929static int
2930lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2931{
2932 int hbq_count = lpfc_sli_hbq_count();
2933 LPFC_MBOXQ_t *pmb;
2934 MAILBOX_t *pmbox;
2935 uint32_t hbqno;
2936 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05002937
James Smart92d7f7b2007-06-17 19:56:38 -05002938 /* Get a Mailbox buffer to setup mailbox
2939 * commands for HBA initialization
2940 */
James Smarted957682007-06-17 19:56:37 -05002941 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2942
2943 if (!pmb)
2944 return -ENOMEM;
2945
2946 pmbox = &pmb->mb;
2947
2948 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2949 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05002950 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05002951
2952 hbq_entry_index = 0;
2953 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2954 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2955 phba->hbqs[hbqno].hbqPutIdx = 0;
2956 phba->hbqs[hbqno].local_hbqGetIdx = 0;
2957 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05002958 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04002959 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2960 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05002961 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2962
2963 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2964 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2965 mbxStatus <status>, ring <num> */
2966
2967 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002968 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002969 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05002970 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002971 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05002972 pmbox->mbxStatus, hbqno);
2973
2974 phba->link_state = LPFC_HBA_ERROR;
2975 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05002976 return ENXIO;
2977 }
2978 }
2979 phba->hbq_count = hbq_count;
2980
James Smarted957682007-06-17 19:56:37 -05002981 mempool_free(pmb, phba->mbox_mem_pool);
2982
James Smart92d7f7b2007-06-17 19:56:38 -05002983 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04002984 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
2985 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05002986 return 0;
2987}
2988
James Smarte59058c2008-08-24 21:49:00 -04002989/**
James Smart93996272008-08-24 21:50:30 -04002990 * lpfc_sli_config_port: Issue config port mailbox command.
James Smarte59058c2008-08-24 21:49:00 -04002991 * @phba: Pointer to HBA context object.
2992 * @sli_mode: sli mode - 2/3
2993 *
2994 * This function is called by the sli intialization code path
2995 * to issue config_port mailbox command. This function restarts the
2996 * HBA firmware and issues a config_port mailbox command to configure
2997 * the SLI interface in the sli mode specified by sli_mode
2998 * variable. The caller is not required to hold any locks.
2999 * The function returns 0 if successful, else returns negative error
3000 * code.
3001 **/
James Smart93996272008-08-24 21:50:30 -04003002int
3003lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003004{
3005 LPFC_MBOXQ_t *pmb;
3006 uint32_t resetcount = 0, rc = 0, done = 0;
3007
3008 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3009 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003010 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003011 return -ENOMEM;
3012 }
3013
James Smarted957682007-06-17 19:56:37 -05003014 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003015 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003016 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003017 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003018 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003019 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003020 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003021 rc = lpfc_sli_chipset_init(phba);
3022 if (rc)
3023 break;
3024
James Smart2e0fef82007-06-17 19:56:36 -05003025 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003026 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003027 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003028 resetcount++;
3029
James Smarted957682007-06-17 19:56:37 -05003030 /* Call pre CONFIG_PORT mailbox command initialization. A
3031 * value of 0 means the call was successful. Any other
3032 * nonzero value is a failure, but if ERESTART is returned,
3033 * the driver may reset the HBA and try again.
3034 */
dea31012005-04-17 16:05:31 -05003035 rc = lpfc_config_port_prep(phba);
3036 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003037 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003038 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003039 } else if (rc)
dea31012005-04-17 16:05:31 -05003040 break;
James Smart2e0fef82007-06-17 19:56:36 -05003041 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003042 lpfc_config_port(phba, pmb);
3043 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003044 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3045 LPFC_SLI3_HBQ_ENABLED |
3046 LPFC_SLI3_CRP_ENABLED |
3047 LPFC_SLI3_INB_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003048 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003050 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003051 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003052 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003053 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003054 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003055 spin_unlock_irq(&phba->hbalock);
3056 rc = -ENXIO;
James Smart34b02dc2008-08-24 21:49:55 -04003057 } else
James Smarted957682007-06-17 19:56:37 -05003058 done = 1;
dea31012005-04-17 16:05:31 -05003059 }
James Smarted957682007-06-17 19:56:37 -05003060 if (!done) {
3061 rc = -EINVAL;
3062 goto do_prep_failed;
3063 }
James Smart34b02dc2008-08-24 21:49:55 -04003064 if (pmb->mb.un.varCfgPort.sli_mode == 3) {
3065 if (!pmb->mb.un.varCfgPort.cMA) {
3066 rc = -ENXIO;
3067 goto do_prep_failed;
3068 }
3069 if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
3070 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3071 phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
3072 } else
3073 phba->max_vpi = 0;
3074 if (pmb->mb.un.varCfgPort.gerbm)
3075 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3076 if (pmb->mb.un.varCfgPort.gcrp)
3077 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3078 if (pmb->mb.un.varCfgPort.ginb) {
3079 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003080 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003081 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3082 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3083 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3084 phba->inb_last_counter =
3085 phba->mbox->us.s3_inb_pgp.counter;
3086 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003087 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003088 phba->port_gp = phba->mbox->us.s3_pgp.port;
3089 phba->inb_ha_copy = NULL;
3090 phba->inb_counter = NULL;
3091 }
3092 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003093 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003094 phba->port_gp = phba->mbox->us.s2.port;
3095 phba->inb_ha_copy = NULL;
3096 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003097 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003098 }
James Smart92d7f7b2007-06-17 19:56:38 -05003099do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003100 mempool_free(pmb, phba->mbox_mem_pool);
3101 return rc;
3102}
3103
James Smarte59058c2008-08-24 21:49:00 -04003104
3105/**
3106 * lpfc_sli_hba_setup: SLI intialization function.
3107 * @phba: Pointer to HBA context object.
3108 *
3109 * This function is the main SLI intialization function. This function
3110 * is called by the HBA intialization code, HBA reset code and HBA
3111 * error attention handler code. Caller is not required to hold any
3112 * locks. This function issues config_port mailbox command to configure
3113 * the SLI, setup iocb rings and HBQ rings. In the end the function
3114 * calls the config_port_post function to issue init_link mailbox
3115 * command and to start the discovery. The function will return zero
3116 * if successful, else it will return negative error code.
3117 **/
James Smarted957682007-06-17 19:56:37 -05003118int
3119lpfc_sli_hba_setup(struct lpfc_hba *phba)
3120{
3121 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003122 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003123
3124 switch (lpfc_sli_mode) {
3125 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003126 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003128 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003129 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003130 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003131 break;
3132 }
James Smarted957682007-06-17 19:56:37 -05003133 mode = 2;
3134 break;
3135 case 0:
3136 case 3:
3137 break;
3138 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003140 "1819 Unrecognized lpfc_sli_mode "
3141 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003142
3143 break;
3144 }
3145
James Smart93996272008-08-24 21:50:30 -04003146 rc = lpfc_sli_config_port(phba, mode);
3147
James Smarted957682007-06-17 19:56:37 -05003148 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003150 "1820 Unable to select SLI-3. "
3151 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003152 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04003153 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05003154 if (rc)
dea31012005-04-17 16:05:31 -05003155 goto lpfc_sli_hba_setup_error;
3156
James Smarted957682007-06-17 19:56:37 -05003157 if (phba->sli_rev == 3) {
3158 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3159 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05003160 } else {
3161 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3162 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003163 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003164 }
3165
3166 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003167 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3168 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05003169 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05003170
3171 if (rc)
3172 goto lpfc_sli_hba_setup_error;
3173
James Smart93996272008-08-24 21:50:30 -04003174 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05003175 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3176 rc = lpfc_sli_hbq_setup(phba);
3177 if (rc)
3178 goto lpfc_sli_hba_setup_error;
3179 }
3180
dea31012005-04-17 16:05:31 -05003181 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3182
3183 rc = lpfc_config_port_post(phba);
3184 if (rc)
3185 goto lpfc_sli_hba_setup_error;
3186
James Smarted957682007-06-17 19:56:37 -05003187 return rc;
3188
James Smart92d7f7b2007-06-17 19:56:38 -05003189lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05003190 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05003191 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003192 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05003193 return rc;
3194}
3195
James Smarte59058c2008-08-24 21:49:00 -04003196
3197/**
3198 * lpfc_mbox_timeout: Timeout call back function for mbox timer.
3199 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05003200 *
James Smarte59058c2008-08-24 21:49:00 -04003201 * This is the callback function for mailbox timer. The mailbox
3202 * timer is armed when a new mailbox command is issued and the timer
3203 * is deleted when the mailbox complete. The function is called by
3204 * the kernel timer code when a mailbox does not complete within
3205 * expected time. This function wakes up the worker thread to
3206 * process the mailbox timeout and returns. All the processing is
3207 * done by the worker thread function lpfc_mbox_timeout_handler.
3208 **/
dea31012005-04-17 16:05:31 -05003209void
3210lpfc_mbox_timeout(unsigned long ptr)
3211{
James Smart92d7f7b2007-06-17 19:56:38 -05003212 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05003213 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05003214 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003215
James Smart2e0fef82007-06-17 19:56:36 -05003216 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05003217 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003218 if (!tmo_posted)
3219 phba->pport->work_port_events |= WORKER_MBOX_TMO;
3220 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
3221
James Smart5e9d9b82008-06-14 22:52:53 -04003222 if (!tmo_posted)
3223 lpfc_worker_wake_up(phba);
3224 return;
dea31012005-04-17 16:05:31 -05003225}
3226
James Smarte59058c2008-08-24 21:49:00 -04003227
3228/**
3229 * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
3230 * @phba: Pointer to HBA context object.
3231 *
3232 * This function is called from worker thread when a mailbox command times out.
3233 * The caller is not required to hold any locks. This function will reset the
3234 * HBA and recover all the pending commands.
3235 **/
dea31012005-04-17 16:05:31 -05003236void
3237lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
3238{
James Smart2e0fef82007-06-17 19:56:36 -05003239 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
3240 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04003241 struct lpfc_sli *psli = &phba->sli;
3242 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003243
dea31012005-04-17 16:05:31 -05003244 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05003245 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003246 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003247 mb->mbxCommand,
3248 phba->pport->port_state,
3249 phba->sli.sli_flag,
3250 phba->sli.mbox_active);
dea31012005-04-17 16:05:31 -05003251
James Smart1dcb58e2007-04-25 09:51:30 -04003252 /* Setting state unknown so lpfc_sli_abort_iocb_ring
3253 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
3254 * it to fail all oustanding SCSI IO.
3255 */
James Smart2e0fef82007-06-17 19:56:36 -05003256 spin_lock_irq(&phba->pport->work_port_lock);
3257 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3258 spin_unlock_irq(&phba->pport->work_port_lock);
3259 spin_lock_irq(&phba->hbalock);
3260 phba->link_state = LPFC_LINK_UNKNOWN;
James Smart1dcb58e2007-04-25 09:51:30 -04003261 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003262 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04003263
3264 pring = &psli->ring[psli->fcp_ring];
3265 lpfc_sli_abort_iocb_ring(phba, pring);
3266
3267 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003268 "0345 Resetting board due to mailbox timeout\n");
James Smart1dcb58e2007-04-25 09:51:30 -04003269 /*
3270 * lpfc_offline calls lpfc_sli_hba_down which will clean up
3271 * on oustanding mailbox commands.
3272 */
James Smart13815c82008-01-11 01:52:48 -05003273 /* If resets are disabled then set error state and return. */
3274 if (!phba->cfg_enable_hba_reset) {
3275 phba->link_state = LPFC_HBA_ERROR;
3276 return;
3277 }
James Smart1dcb58e2007-04-25 09:51:30 -04003278 lpfc_offline_prep(phba);
3279 lpfc_offline(phba);
3280 lpfc_sli_brdrestart(phba);
James Smart58da1ff2008-04-07 10:15:56 -04003281 lpfc_online(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04003282 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003283 return;
3284}
3285
James Smarte59058c2008-08-24 21:49:00 -04003286/**
3287 * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
3288 * @phba: Pointer to HBA context object.
3289 * @pmbox: Pointer to mailbox object.
3290 * @flag: Flag indicating how the mailbox need to be processed.
3291 *
3292 * This function is called by discovery code and HBA management code
3293 * to submit a mailbox command to firmware. This function gets the
3294 * hbalock to protect the data structures.
3295 * The mailbox command can be submitted in polling mode, in which case
3296 * this function will wait in a polling loop for the completion of the
3297 * mailbox.
3298 * If the mailbox is submitted in no_wait mode (not polling) the
3299 * function will submit the command and returns immediately without waiting
3300 * for the mailbox completion. The no_wait is supported only when HBA
3301 * is in SLI2/SLI3 mode - interrupts are enabled.
3302 * The SLI interface allows only one mailbox pending at a time. If the
3303 * mailbox is issued in polling mode and there is already a mailbox
3304 * pending, then the function will return an error. If the mailbox is issued
3305 * in NO_WAIT mode and there is a mailbox pending already, the function
3306 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
3307 * The sli layer owns the mailbox object until the completion of mailbox
3308 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
3309 * return codes the caller owns the mailbox command after the return of
3310 * the function.
3311 **/
dea31012005-04-17 16:05:31 -05003312int
James Smart2e0fef82007-06-17 19:56:36 -05003313lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
dea31012005-04-17 16:05:31 -05003314{
dea31012005-04-17 16:05:31 -05003315 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05003316 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003317 uint32_t status, evtctr;
3318 uint32_t ha_copy;
3319 int i;
James Smart09372822008-01-11 01:52:54 -05003320 unsigned long timeout;
dea31012005-04-17 16:05:31 -05003321 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04003322 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05003323 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04003324 int processing_queue = 0;
3325
3326 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3327 if (!pmbox) {
3328 /* processing mbox queue from intr_handler */
3329 processing_queue = 1;
3330 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3331 pmbox = lpfc_mbox_get(phba);
3332 if (!pmbox) {
3333 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3334 return MBX_SUCCESS;
3335 }
3336 }
dea31012005-04-17 16:05:31 -05003337
James Smarted957682007-06-17 19:56:37 -05003338 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05003339 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05003340 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04003341 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05003342 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003343 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003344 "1806 Mbox x%x failed. No vport\n",
James Smarted957682007-06-17 19:56:37 -05003345 pmbox->mb.mbxCommand);
3346 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04003347 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05003348 }
3349 }
3350
Linas Vepstas8d63f372007-02-14 14:28:36 -06003351 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04003352 if (unlikely(pci_channel_offline(phba->pcidev))) {
3353 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3354 goto out_not_finished;
3355 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06003356
dea31012005-04-17 16:05:31 -05003357 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05003358
dea31012005-04-17 16:05:31 -05003359 mb = &pmbox->mb;
3360 status = MBX_SUCCESS;
3361
James Smart2e0fef82007-06-17 19:56:36 -05003362 if (phba->link_state == LPFC_HBA_ERROR) {
3363 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003364
3365 /* Mbox command <mbxCommand> cannot issue */
James Smart7f5f3d02008-02-08 18:50:14 -05003366 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003367 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003368 }
3369
James Smart92908312006-03-07 15:04:13 -05003370 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
3371 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05003372 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart7f5f3d02008-02-08 18:50:14 -05003373 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003374 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05003375 }
3376
dea31012005-04-17 16:05:31 -05003377 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
3378 /* Polling for a mbox command when another one is already active
3379 * is not allowed in SLI. Also, the driver must have established
3380 * SLI2 mode to queue and process multiple mbox commands.
3381 */
3382
3383 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05003384 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003385
3386 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003387 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003388 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003389 }
3390
3391 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003392 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003393 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003394 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003395 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003396 }
3397
dea31012005-04-17 16:05:31 -05003398 /* Another mailbox command is still being processed, queue this
3399 * command to be processed later.
3400 */
3401 lpfc_mbox_put(phba, pmbox);
3402
3403 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05003404 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003405 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003406 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003407 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
3408 mb->mbxCommand, phba->pport->port_state,
3409 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003410
3411 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05003412 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003413
James Smart858c9f62007-06-17 19:56:39 -05003414 if (pmbox->vport) {
3415 lpfc_debugfs_disc_trc(pmbox->vport,
3416 LPFC_DISC_TRC_MBOX_VPORT,
3417 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
3418 (uint32_t)mb->mbxCommand,
3419 mb->un.varWords[0], mb->un.varWords[1]);
3420 }
3421 else {
3422 lpfc_debugfs_disc_trc(phba->pport,
3423 LPFC_DISC_TRC_MBOX,
3424 "MBOX Bsy: cmd:x%x mb:x%x x%x",
3425 (uint32_t)mb->mbxCommand,
3426 mb->un.varWords[0], mb->un.varWords[1]);
3427 }
3428
James Smart2e0fef82007-06-17 19:56:36 -05003429 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05003430 }
3431
dea31012005-04-17 16:05:31 -05003432 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3433
3434 /* If we are not polling, we MUST be in SLI2 mode */
3435 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05003436 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
3437 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05003438 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003439 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003440 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003441 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003442 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003443 }
3444 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04003445 mod_timer(&psli->mbox_tmo, (jiffies +
3446 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05003447 }
3448
3449 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05003450 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003451 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003452 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003453 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05003454 mb->mbxCommand, phba->pport->port_state,
3455 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003456
James Smart858c9f62007-06-17 19:56:39 -05003457 if (mb->mbxCommand != MBX_HEARTBEAT) {
3458 if (pmbox->vport) {
3459 lpfc_debugfs_disc_trc(pmbox->vport,
3460 LPFC_DISC_TRC_MBOX_VPORT,
3461 "MBOX Send vport: cmd:x%x mb:x%x x%x",
3462 (uint32_t)mb->mbxCommand,
3463 mb->un.varWords[0], mb->un.varWords[1]);
3464 }
3465 else {
3466 lpfc_debugfs_disc_trc(phba->pport,
3467 LPFC_DISC_TRC_MBOX,
3468 "MBOX Send: cmd:x%x mb:x%x x%x",
3469 (uint32_t)mb->mbxCommand,
3470 mb->un.varWords[0], mb->un.varWords[1]);
3471 }
3472 }
3473
dea31012005-04-17 16:05:31 -05003474 psli->slistat.mbox_cmd++;
3475 evtctr = psli->slistat.mbox_event;
3476
3477 /* next set own bit for the adapter and copy over command word */
3478 mb->mbxOwner = OWN_CHIP;
3479
3480 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003481 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04003482 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003483 } else {
James Smart92908312006-03-07 15:04:13 -05003484 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05003485 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04003486 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003487 }
3488
3489 /* First copy mbox command data to HBA SLIM, skip past first
3490 word */
3491 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3492 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
3493 MAILBOX_CMD_SIZE - sizeof (uint32_t));
3494
3495 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04003496 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05003497 to_slim = phba->MBslimaddr;
3498 writel(ldata, to_slim);
3499 readl(to_slim); /* flush */
3500
3501 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3502 /* switch over to host mailbox */
3503 psli->sli_flag |= LPFC_SLI2_ACTIVE;
3504 }
3505 }
3506
3507 wmb();
dea31012005-04-17 16:05:31 -05003508
3509 switch (flag) {
3510 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05003511 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05003512 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05003513 /* Interrupt board to do it */
3514 writel(CA_MBATT, phba->CAregaddr);
3515 readl(phba->CAregaddr); /* flush */
3516 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05003517 break;
3518
3519 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05003520 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05003521 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05003522 /* Interrupt board to do it */
3523 writel(CA_MBATT, phba->CAregaddr);
3524 readl(phba->CAregaddr); /* flush */
3525
dea31012005-04-17 16:05:31 -05003526 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3527 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04003528 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003529 word0 = le32_to_cpu(word0);
3530 } else {
3531 /* First read mbox status word */
3532 word0 = readl(phba->MBslimaddr);
3533 }
3534
3535 /* Read the HBA Host Attention Register */
3536 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05003537 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3538 mb->mbxCommand) *
3539 1000) + jiffies;
3540 i = 0;
dea31012005-04-17 16:05:31 -05003541 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003542 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
3543 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05003544 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05003545 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05003546 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003547 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05003548 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04003549 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003550 }
3551
3552 /* Check if we took a mbox interrupt while we were
3553 polling */
3554 if (((word0 & OWN_CHIP) != OWN_CHIP)
3555 && (evtctr != psli->slistat.mbox_event))
3556 break;
3557
James Smart09372822008-01-11 01:52:54 -05003558 if (i++ > 10) {
3559 spin_unlock_irqrestore(&phba->hbalock,
3560 drvr_flag);
3561 msleep(1);
3562 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3563 }
dea31012005-04-17 16:05:31 -05003564
3565 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3566 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04003567 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003568 word0 = le32_to_cpu(word0);
3569 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3570 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04003571 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05003572 /* Check real SLIM for any errors */
3573 slimword0 = readl(phba->MBslimaddr);
3574 slimmb = (MAILBOX_t *) & slimword0;
3575 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
3576 && slimmb->mbxStatus) {
3577 psli->sli_flag &=
3578 ~LPFC_SLI2_ACTIVE;
3579 word0 = slimword0;
3580 }
3581 }
3582 } else {
3583 /* First copy command data */
3584 word0 = readl(phba->MBslimaddr);
3585 }
3586 /* Read the HBA Host Attention Register */
3587 ha_copy = readl(phba->HAregaddr);
3588 }
3589
3590 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003591 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04003592 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003593 } else {
3594 /* First copy command data */
3595 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
3596 MAILBOX_CMD_SIZE);
3597 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
3598 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05003599 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05003600 phba->MBslimaddr + DMP_RSP_OFFSET,
3601 mb->un.varDmp.word_cnt);
3602 }
3603 }
3604
3605 writel(HA_MBATT, phba->HAregaddr);
3606 readl(phba->HAregaddr); /* flush */
3607
3608 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3609 status = mb->mbxStatus;
3610 }
3611
James Smart2e0fef82007-06-17 19:56:36 -05003612 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3613 return status;
James Smart58da1ff2008-04-07 10:15:56 -04003614
3615out_not_finished:
3616 if (processing_queue) {
3617 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
3618 lpfc_mbox_cmpl_put(phba, pmbox);
3619 }
3620 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003621}
3622
James Smarte59058c2008-08-24 21:49:00 -04003623/**
3624 * __lpfc_sli_ringtx_put: Add an iocb to the txq.
3625 * @phba: Pointer to HBA context object.
3626 * @pring: Pointer to driver SLI ring object.
3627 * @piocb: Pointer to address of newly added command iocb.
3628 *
3629 * This function is called with hbalock held to add a command
3630 * iocb to the txq when SLI layer cannot submit the command iocb
3631 * to the ring.
3632 **/
James Smart858c9f62007-06-17 19:56:39 -05003633static void
James Smart92d7f7b2007-06-17 19:56:38 -05003634__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003635 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05003636{
3637 /* Insert the caller's iocb in the txq tail for later processing. */
3638 list_add_tail(&piocb->list, &pring->txq);
3639 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05003640}
3641
James Smarte59058c2008-08-24 21:49:00 -04003642/**
3643 * lpfc_sli_next_iocb: Get the next iocb in the txq.
3644 * @phba: Pointer to HBA context object.
3645 * @pring: Pointer to driver SLI ring object.
3646 * @piocb: Pointer to address of newly added command iocb.
3647 *
3648 * This function is called with hbalock held before a new
3649 * iocb is submitted to the firmware. This function checks
3650 * txq to flush the iocbs in txq to Firmware before
3651 * submitting new iocbs to the Firmware.
3652 * If there are iocbs in the txq which need to be submitted
3653 * to firmware, lpfc_sli_next_iocb returns the first element
3654 * of the txq after dequeuing it from txq.
3655 * If there is no iocb in the txq then the function will return
3656 * *piocb and *piocb is set to NULL. Caller needs to check
3657 * *piocb to find if there are more commands in the txq.
3658 **/
dea31012005-04-17 16:05:31 -05003659static struct lpfc_iocbq *
3660lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003661 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05003662{
3663 struct lpfc_iocbq * nextiocb;
3664
3665 nextiocb = lpfc_sli_ringtx_get(phba, pring);
3666 if (!nextiocb) {
3667 nextiocb = *piocb;
3668 *piocb = NULL;
3669 }
3670
3671 return nextiocb;
3672}
3673
James Smarte59058c2008-08-24 21:49:00 -04003674/**
3675 * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
3676 * @phba: Pointer to HBA context object.
3677 * @pring: Pointer to driver SLI ring object.
3678 * @piocb: Pointer to command iocb.
3679 * @flag: Flag indicating if this command can be put into txq.
3680 *
3681 * __lpfc_sli_issue_iocb is used by other functions in the driver
3682 * to issue an iocb command to the HBA. If the PCI slot is recovering
3683 * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
3684 * flag is turned on, the function returns IOCB_ERROR.
3685 * When the link is down, this function allows only iocbs for
3686 * posting buffers.
3687 * This function finds next available slot in the command ring and
3688 * posts the command to the available slot and writes the port
3689 * attention register to request HBA start processing new iocb.
3690 * If there is no slot available in the ring and
3691 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
3692 * txq, otherwise the function returns IOCB_BUSY.
3693 *
3694 * This function is called with hbalock held.
3695 * The function will return success after it successfully submit the
3696 * iocb to firmware or after adding to the txq.
3697 **/
James Smart98c9ea52007-10-27 13:37:33 -04003698static int
James Smart92d7f7b2007-06-17 19:56:38 -05003699__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003700 struct lpfc_iocbq *piocb, uint32_t flag)
3701{
3702 struct lpfc_iocbq *nextiocb;
3703 IOCB_t *iocb;
3704
James Smart92d7f7b2007-06-17 19:56:38 -05003705 if (piocb->iocb_cmpl && (!piocb->vport) &&
3706 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
3707 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
3708 lpfc_printf_log(phba, KERN_ERR,
3709 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003710 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003711 piocb->iocb.ulpCommand);
3712 dump_stack();
3713 return IOCB_ERROR;
3714 }
3715
3716
Linas Vepstas8d63f372007-02-14 14:28:36 -06003717 /* If the PCI channel is in offline state, do not post iocbs. */
3718 if (unlikely(pci_channel_offline(phba->pcidev)))
3719 return IOCB_ERROR;
3720
dea31012005-04-17 16:05:31 -05003721 /*
3722 * We should never get an IOCB if we are in a < LINK_DOWN state
3723 */
James Smart2e0fef82007-06-17 19:56:36 -05003724 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003725 return IOCB_ERROR;
3726
3727 /*
3728 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04003729 * outstanding event.
dea31012005-04-17 16:05:31 -05003730 */
James Smart0b727fe2007-10-27 13:37:25 -04003731 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05003732 goto iocb_busy;
3733
James Smart2e0fef82007-06-17 19:56:36 -05003734 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05003735 /*
James Smart2680eea2007-04-25 09:52:55 -04003736 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05003737 * can be issued if the link is not up.
3738 */
3739 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04003740 case CMD_GEN_REQUEST64_CR:
3741 case CMD_GEN_REQUEST64_CX:
3742 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
3743 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
3744 FC_FCP_CMND) ||
3745 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
3746 MENLO_TRANSPORT_TYPE))
3747
3748 goto iocb_busy;
3749 break;
dea31012005-04-17 16:05:31 -05003750 case CMD_QUE_RING_BUF_CN:
3751 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05003752 /*
3753 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
3754 * completion, iocb_cmpl MUST be 0.
3755 */
3756 if (piocb->iocb_cmpl)
3757 piocb->iocb_cmpl = NULL;
3758 /*FALLTHROUGH*/
3759 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04003760 case CMD_CLOSE_XRI_CN:
3761 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05003762 break;
3763 default:
3764 goto iocb_busy;
3765 }
3766
3767 /*
3768 * For FCP commands, we must be in a state where we can process link
3769 * attention events.
3770 */
3771 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05003772 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05003773 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05003774 }
dea31012005-04-17 16:05:31 -05003775
dea31012005-04-17 16:05:31 -05003776 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
3777 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
3778 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
3779
3780 if (iocb)
3781 lpfc_sli_update_ring(phba, pring);
3782 else
3783 lpfc_sli_update_full_ring(phba, pring);
3784
3785 if (!piocb)
3786 return IOCB_SUCCESS;
3787
3788 goto out_busy;
3789
3790 iocb_busy:
3791 pring->stats.iocb_cmd_delay++;
3792
3793 out_busy:
3794
3795 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003796 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05003797 return IOCB_SUCCESS;
3798 }
3799
3800 return IOCB_BUSY;
3801}
3802
James Smart92d7f7b2007-06-17 19:56:38 -05003803
James Smarte59058c2008-08-24 21:49:00 -04003804/**
3805 * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
3806 * @phba: Pointer to HBA context object.
3807 * @pring: Pointer to driver SLI ring object.
3808 * @piocb: Pointer to command iocb.
3809 * @flag: Flag indicating if this command can be put into txq.
3810 *
3811 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
3812 * function. This function gets the hbalock and calls
3813 * __lpfc_sli_issue_iocb function and will return the error returned
3814 * by __lpfc_sli_issue_iocb function. This wrapper is used by
3815 * functions which do not hold hbalock.
3816 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003817int
3818lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3819 struct lpfc_iocbq *piocb, uint32_t flag)
3820{
3821 unsigned long iflags;
3822 int rc;
3823
3824 spin_lock_irqsave(&phba->hbalock, iflags);
3825 rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
3826 spin_unlock_irqrestore(&phba->hbalock, iflags);
3827
3828 return rc;
3829}
3830
James Smarte59058c2008-08-24 21:49:00 -04003831/**
3832 * lpfc_extra_ring_setup: Extra ring setup function.
3833 * @phba: Pointer to HBA context object.
3834 *
3835 * This function is called while driver attaches with the
3836 * HBA to setup the extra ring. The extra ring is used
3837 * only when driver needs to support target mode functionality
3838 * or IP over FC functionalities.
3839 *
3840 * This function is called with no lock held.
3841 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003842static int
3843lpfc_extra_ring_setup( struct lpfc_hba *phba)
3844{
3845 struct lpfc_sli *psli;
3846 struct lpfc_sli_ring *pring;
3847
3848 psli = &phba->sli;
3849
3850 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05003851
3852 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003853 pring = &psli->ring[psli->fcp_ring];
3854 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3855 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3856 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3857 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3858
James Smarta4bc3372006-12-02 13:34:16 -05003859 /* and give them to the extra ring */
3860 pring = &psli->ring[psli->extra_ring];
3861
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003862 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3863 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3864 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3865 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3866
3867 /* Setup default profile for this ring */
3868 pring->iotag_max = 4096;
3869 pring->num_mask = 1;
3870 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05003871 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
3872 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003873 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
3874 return 0;
3875}
3876
James Smarte59058c2008-08-24 21:49:00 -04003877/**
3878 * lpfc_sli_async_event_handler: ASYNC iocb handler function.
3879 * @phba: Pointer to HBA context object.
3880 * @pring: Pointer to driver SLI ring object.
3881 * @iocbq: Pointer to iocb object.
3882 *
3883 * This function is called by the slow ring event handler
3884 * function when there is an ASYNC event iocb in the ring.
3885 * This function is called with no lock held.
3886 * Currently this function handles only temperature related
3887 * ASYNC events. The function decodes the temperature sensor
3888 * event message and posts events for the management applications.
3889 **/
James Smart98c9ea52007-10-27 13:37:33 -04003890static void
James Smart57127f12007-10-27 13:37:05 -04003891lpfc_sli_async_event_handler(struct lpfc_hba * phba,
3892 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
3893{
3894 IOCB_t *icmd;
3895 uint16_t evt_code;
3896 uint16_t temp;
3897 struct temp_event temp_event_data;
3898 struct Scsi_Host *shost;
3899
3900 icmd = &iocbq->iocb;
3901 evt_code = icmd->un.asyncstat.evt_code;
3902 temp = icmd->ulpContext;
3903
3904 if ((evt_code != ASYNC_TEMP_WARN) &&
3905 (evt_code != ASYNC_TEMP_SAFE)) {
3906 lpfc_printf_log(phba,
3907 KERN_ERR,
3908 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003909 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smart57127f12007-10-27 13:37:05 -04003910 " evt_code 0x%x\n",
3911 pring->ringno,
3912 icmd->un.asyncstat.evt_code);
3913 return;
3914 }
3915 temp_event_data.data = (uint32_t)temp;
3916 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
3917 if (evt_code == ASYNC_TEMP_WARN) {
3918 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
3919 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003920 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003921 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04003922 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04003923 "corrective action. temperature : %d Celsius\n",
3924 temp);
3925 }
3926 if (evt_code == ASYNC_TEMP_SAFE) {
3927 temp_event_data.event_code = LPFC_NORMAL_TEMP;
3928 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003929 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003930 LOG_TEMP,
3931 "0340 Adapter temperature is OK now. "
3932 "temperature : %d Celsius\n",
3933 temp);
3934 }
3935
3936 /* Send temperature change event to applications */
3937 shost = lpfc_shost_from_vport(phba->pport);
3938 fc_host_post_vendor_event(shost, fc_get_event_number(),
3939 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05003940 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04003941
3942}
3943
3944
James Smarte59058c2008-08-24 21:49:00 -04003945/**
3946 * lpfc_sli_setup: SLI ring setup function.
3947 * @phba: Pointer to HBA context object.
3948 *
3949 * lpfc_sli_setup sets up rings of the SLI interface with
3950 * number of iocbs per ring and iotags. This function is
3951 * called while driver attach to the HBA and before the
3952 * interrupts are enabled. So there is no need for locking.
3953 *
3954 * This function always returns 0.
3955 **/
dea31012005-04-17 16:05:31 -05003956int
3957lpfc_sli_setup(struct lpfc_hba *phba)
3958{
James Smarted957682007-06-17 19:56:37 -05003959 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05003960 struct lpfc_sli *psli = &phba->sli;
3961 struct lpfc_sli_ring *pring;
3962
3963 psli->num_rings = MAX_CONFIGURED_RINGS;
3964 psli->sli_flag = 0;
3965 psli->fcp_ring = LPFC_FCP_RING;
3966 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05003967 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05003968
James Bottomley604a3e32005-10-29 10:28:33 -05003969 psli->iocbq_lookup = NULL;
3970 psli->iocbq_lookup_len = 0;
3971 psli->last_iotag = 0;
3972
dea31012005-04-17 16:05:31 -05003973 for (i = 0; i < psli->num_rings; i++) {
3974 pring = &psli->ring[i];
3975 switch (i) {
3976 case LPFC_FCP_RING: /* ring 0 - FCP */
3977 /* numCiocb and numRiocb are used in config_port */
3978 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3979 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3980 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3981 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3982 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3983 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003984 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003985 SLI3_IOCB_CMD_SIZE :
3986 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003987 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003988 SLI3_IOCB_RSP_SIZE :
3989 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003990 pring->iotag_ctr = 0;
3991 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05003992 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05003993 pring->fast_iotag = pring->iotag_max;
3994 pring->num_mask = 0;
3995 break;
James Smarta4bc3372006-12-02 13:34:16 -05003996 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05003997 /* numCiocb and numRiocb are used in config_port */
3998 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
3999 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004000 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004001 SLI3_IOCB_CMD_SIZE :
4002 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004003 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004004 SLI3_IOCB_RSP_SIZE :
4005 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05004006 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05004007 pring->num_mask = 0;
4008 break;
4009 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
4010 /* numCiocb and numRiocb are used in config_port */
4011 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
4012 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004013 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004014 SLI3_IOCB_CMD_SIZE :
4015 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004016 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004017 SLI3_IOCB_RSP_SIZE :
4018 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05004019 pring->fast_iotag = 0;
4020 pring->iotag_ctr = 0;
4021 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04004022 pring->lpfc_sli_rcv_async_status =
4023 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05004024 pring->num_mask = 4;
4025 pring->prt[0].profile = 0; /* Mask 0 */
4026 pring->prt[0].rctl = FC_ELS_REQ;
4027 pring->prt[0].type = FC_ELS_DATA;
4028 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004029 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004030 pring->prt[1].profile = 0; /* Mask 1 */
4031 pring->prt[1].rctl = FC_ELS_RSP;
4032 pring->prt[1].type = FC_ELS_DATA;
4033 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004034 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004035 pring->prt[2].profile = 0; /* Mask 2 */
4036 /* NameServer Inquiry */
4037 pring->prt[2].rctl = FC_UNSOL_CTL;
4038 /* NameServer */
4039 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
4040 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004041 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004042 pring->prt[3].profile = 0; /* Mask 3 */
4043 /* NameServer response */
4044 pring->prt[3].rctl = FC_SOL_CTL;
4045 /* NameServer */
4046 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
4047 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004048 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004049 break;
4050 }
James Smarted957682007-06-17 19:56:37 -05004051 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05004052 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05004053 }
James Smarted957682007-06-17 19:56:37 -05004054 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05004055 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04004056 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
4057 "SLI2 SLIM Data: x%x x%lx\n",
4058 phba->brd_no, totiocbsize,
4059 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05004060 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004061 if (phba->cfg_multi_ring_support == 2)
4062 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05004063
4064 return 0;
4065}
4066
James Smarte59058c2008-08-24 21:49:00 -04004067/**
4068 * lpfc_sli_queue_setup: Queue initialization function.
4069 * @phba: Pointer to HBA context object.
4070 *
4071 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
4072 * ring. This function also initializes ring indices of each ring.
4073 * This function is called during the initialization of the SLI
4074 * interface of an HBA.
4075 * This function is called with no lock held and always returns
4076 * 1.
4077 **/
dea31012005-04-17 16:05:31 -05004078int
James Smart2e0fef82007-06-17 19:56:36 -05004079lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004080{
4081 struct lpfc_sli *psli;
4082 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05004083 int i;
dea31012005-04-17 16:05:31 -05004084
4085 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05004086 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004087 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05004088 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05004089 /* Initialize list headers for txq and txcmplq as double linked lists */
4090 for (i = 0; i < psli->num_rings; i++) {
4091 pring = &psli->ring[i];
4092 pring->ringno = i;
4093 pring->next_cmdidx = 0;
4094 pring->local_getidx = 0;
4095 pring->cmdidx = 0;
4096 INIT_LIST_HEAD(&pring->txq);
4097 INIT_LIST_HEAD(&pring->txcmplq);
4098 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05004099 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05004100 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05004101 }
James Smart2e0fef82007-06-17 19:56:36 -05004102 spin_unlock_irq(&phba->hbalock);
4103 return 1;
dea31012005-04-17 16:05:31 -05004104}
4105
James Smarte59058c2008-08-24 21:49:00 -04004106/**
4107 * lpfc_sli_host_down: Vport cleanup function.
4108 * @vport: Pointer to virtual port object.
4109 *
4110 * lpfc_sli_host_down is called to clean up the resources
4111 * associated with a vport before destroying virtual
4112 * port data structures.
4113 * This function does following operations:
4114 * - Free discovery resources associated with this virtual
4115 * port.
4116 * - Free iocbs associated with this virtual port in
4117 * the txq.
4118 * - Send abort for all iocb commands associated with this
4119 * vport in txcmplq.
4120 *
4121 * This function is called with no lock held and always returns 1.
4122 **/
dea31012005-04-17 16:05:31 -05004123int
James Smart92d7f7b2007-06-17 19:56:38 -05004124lpfc_sli_host_down(struct lpfc_vport *vport)
4125{
James Smart858c9f62007-06-17 19:56:39 -05004126 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004127 struct lpfc_hba *phba = vport->phba;
4128 struct lpfc_sli *psli = &phba->sli;
4129 struct lpfc_sli_ring *pring;
4130 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004131 int i;
4132 unsigned long flags = 0;
4133 uint16_t prev_pring_flag;
4134
4135 lpfc_cleanup_discovery_resources(vport);
4136
4137 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004138 for (i = 0; i < psli->num_rings; i++) {
4139 pring = &psli->ring[i];
4140 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04004141 /* Only slow rings */
4142 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004143 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004144 /* Set the lpfc data pending flag */
4145 set_bit(LPFC_DATA_READY, &phba->data_flags);
4146 }
James Smart92d7f7b2007-06-17 19:56:38 -05004147 /*
4148 * Error everything on the txq since these iocbs have not been
4149 * given to the FW yet.
4150 */
James Smart92d7f7b2007-06-17 19:56:38 -05004151 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4152 if (iocb->vport != vport)
4153 continue;
James Smart858c9f62007-06-17 19:56:39 -05004154 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004155 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05004156 }
4157
4158 /* Next issue ABTS for everything on the txcmplq */
4159 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
4160 list) {
4161 if (iocb->vport != vport)
4162 continue;
4163 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4164 }
4165
4166 pring->flag = prev_pring_flag;
4167 }
4168
4169 spin_unlock_irqrestore(&phba->hbalock, flags);
4170
James Smart858c9f62007-06-17 19:56:39 -05004171 while (!list_empty(&completions)) {
4172 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
4173
4174 if (!iocb->iocb_cmpl)
4175 lpfc_sli_release_iocbq(phba, iocb);
4176 else {
4177 iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4178 iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
4179 (iocb->iocb_cmpl) (phba, iocb, iocb);
4180 }
4181 }
James Smart92d7f7b2007-06-17 19:56:38 -05004182 return 1;
4183}
4184
James Smarte59058c2008-08-24 21:49:00 -04004185/**
4186 * lpfc_sli_hba_down: Resource cleanup function for the HBA.
4187 * @phba: Pointer to HBA context object.
4188 *
4189 * This function cleans up all iocb, buffers, mailbox commands
4190 * while shutting down the HBA. This function is called with no
4191 * lock held and always returns 1.
4192 * This function does the following to cleanup driver resources:
4193 * - Free discovery resources for each virtual port
4194 * - Cleanup any pending fabric iocbs
4195 * - Iterate through the iocb txq and free each entry
4196 * in the list.
4197 * - Free up any buffer posted to the HBA
4198 * - Free mailbox commands in the mailbox queue.
4199 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004200int
James Smart2e0fef82007-06-17 19:56:36 -05004201lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004202{
James Smart2534ba72007-04-25 09:52:20 -04004203 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05004204 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004205 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05004206 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05004207 LPFC_MBOXQ_t *pmb;
James Smart2534ba72007-04-25 09:52:20 -04004208 struct lpfc_iocbq *iocb;
4209 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004210 int i;
4211 unsigned long flags = 0;
4212
dea31012005-04-17 16:05:31 -05004213 lpfc_hba_down_prep(phba);
4214
James Smart92d7f7b2007-06-17 19:56:38 -05004215 lpfc_fabric_abort_hba(phba);
4216
James Smart2e0fef82007-06-17 19:56:36 -05004217 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004218 for (i = 0; i < psli->num_rings; i++) {
4219 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04004220 /* Only slow rings */
4221 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004222 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004223 /* Set the lpfc data pending flag */
4224 set_bit(LPFC_DATA_READY, &phba->data_flags);
4225 }
dea31012005-04-17 16:05:31 -05004226
4227 /*
4228 * Error everything on the txq since these iocbs have not been
4229 * given to the FW yet.
4230 */
James Smart2534ba72007-04-25 09:52:20 -04004231 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05004232 pring->txq_cnt = 0;
4233
dea31012005-04-17 16:05:31 -05004234 }
James Smart2e0fef82007-06-17 19:56:36 -05004235 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004236
James Smart2534ba72007-04-25 09:52:20 -04004237 while (!list_empty(&completions)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004238 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
James Smart2534ba72007-04-25 09:52:20 -04004239 cmd = &iocb->iocb;
James Smart2534ba72007-04-25 09:52:20 -04004240
James Smart2e0fef82007-06-17 19:56:36 -05004241 if (!iocb->iocb_cmpl)
4242 lpfc_sli_release_iocbq(phba, iocb);
4243 else {
James Smart2534ba72007-04-25 09:52:20 -04004244 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4245 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
4246 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05004247 }
James Smart2534ba72007-04-25 09:52:20 -04004248 }
4249
James Smart0ff10d42008-01-11 01:52:36 -05004250 spin_lock_irqsave(&phba->hbalock, flags);
4251 list_splice_init(&phba->elsbuf, &completions);
4252 phba->elsbuf_cnt = 0;
4253 phba->elsbuf_prev_cnt = 0;
4254 spin_unlock_irqrestore(&phba->hbalock, flags);
4255
4256 while (!list_empty(&completions)) {
4257 list_remove_head(&completions, buf_ptr,
4258 struct lpfc_dmabuf, list);
4259 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4260 kfree(buf_ptr);
4261 }
4262
dea31012005-04-17 16:05:31 -05004263 /* Return any active mbox cmds */
4264 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05004265 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004266
4267 spin_lock(&phba->pport->work_port_lock);
4268 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4269 spin_unlock(&phba->pport->work_port_lock);
4270
James Smart97eab632008-04-07 10:16:05 -04004271 /* Return any pending or completed mbox cmds */
4272 list_splice_init(&phba->sli.mboxq, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004273 if (psli->mbox_active) {
4274 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05004275 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004276 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05004277 }
James Smart92d7f7b2007-06-17 19:56:38 -05004278 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004279 spin_unlock_irqrestore(&phba->hbalock, flags);
4280
4281 while (!list_empty(&completions)) {
4282 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05004283 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
James Smart97eab632008-04-07 10:16:05 -04004284 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05004285 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05004286 }
dea31012005-04-17 16:05:31 -05004287 return 1;
4288}
4289
James Smarte59058c2008-08-24 21:49:00 -04004290/**
4291 * lpfc_sli_pcimem_bcopy: SLI memory copy function.
4292 * @srcp: Source memory pointer.
4293 * @destp: Destination memory pointer.
4294 * @cnt: Number of words required to be copied.
4295 *
4296 * This function is used for copying data between driver memory
4297 * and the SLI memory. This function also changes the endianness
4298 * of each word if native endianness is different from SLI
4299 * endianness. This function can be called with or without
4300 * lock.
4301 **/
dea31012005-04-17 16:05:31 -05004302void
4303lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
4304{
4305 uint32_t *src = srcp;
4306 uint32_t *dest = destp;
4307 uint32_t ldata;
4308 int i;
4309
4310 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
4311 ldata = *src;
4312 ldata = le32_to_cpu(ldata);
4313 *dest = ldata;
4314 src++;
4315 dest++;
4316 }
4317}
4318
James Smarte59058c2008-08-24 21:49:00 -04004319
4320/**
4321 * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
4322 * @phba: Pointer to HBA context object.
4323 * @pring: Pointer to driver SLI ring object.
4324 * @mp: Pointer to driver buffer object.
4325 *
4326 * This function is called with no lock held.
4327 * It always return zero after adding the buffer to the postbufq
4328 * buffer list.
4329 **/
dea31012005-04-17 16:05:31 -05004330int
James Smart2e0fef82007-06-17 19:56:36 -05004331lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4332 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05004333{
4334 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
4335 later */
James Smart2e0fef82007-06-17 19:56:36 -05004336 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004337 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05004338 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05004339 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004340 return 0;
4341}
4342
James Smarte59058c2008-08-24 21:49:00 -04004343/**
4344 * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
4345 * using CMD_QUE_XRI64_CX iocb.
4346 * @phba: Pointer to HBA context object.
4347 *
4348 * When HBQ is enabled, buffers are searched based on tags. This function
4349 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
4350 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
4351 * does not conflict with tags of buffer posted for unsolicited events.
4352 * The function returns the allocated tag. The function is called with
4353 * no locks held.
4354 **/
James Smart76bb24e2007-10-27 13:38:00 -04004355uint32_t
4356lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
4357{
4358 spin_lock_irq(&phba->hbalock);
4359 phba->buffer_tag_count++;
4360 /*
4361 * Always set the QUE_BUFTAG_BIT to distiguish between
4362 * a tag assigned by HBQ.
4363 */
4364 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
4365 spin_unlock_irq(&phba->hbalock);
4366 return phba->buffer_tag_count;
4367}
4368
James Smarte59058c2008-08-24 21:49:00 -04004369/**
4370 * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
4371 * posted using CMD_QUE_XRI64_CX iocb.
4372 * @phba: Pointer to HBA context object.
4373 * @pring: Pointer to driver SLI ring object.
4374 * @tag: Buffer tag.
4375 *
4376 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
4377 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
4378 * iocb is posted to the response ring with the tag of the buffer.
4379 * This function searches the pring->postbufq list using the tag
4380 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
4381 * iocb. If the buffer is found then lpfc_dmabuf object of the
4382 * buffer is returned to the caller else NULL is returned.
4383 * This function is called with no lock held.
4384 **/
James Smart76bb24e2007-10-27 13:38:00 -04004385struct lpfc_dmabuf *
4386lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4387 uint32_t tag)
4388{
4389 struct lpfc_dmabuf *mp, *next_mp;
4390 struct list_head *slp = &pring->postbufq;
4391
4392 /* Search postbufq, from the begining, looking for a match on tag */
4393 spin_lock_irq(&phba->hbalock);
4394 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4395 if (mp->buffer_tag == tag) {
4396 list_del_init(&mp->list);
4397 pring->postbufq_cnt--;
4398 spin_unlock_irq(&phba->hbalock);
4399 return mp;
4400 }
4401 }
4402
4403 spin_unlock_irq(&phba->hbalock);
4404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04004405 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04004406 "ring %d Data x%lx x%p x%p x%x\n",
4407 pring->ringno, (unsigned long) tag,
4408 slp->next, slp->prev, pring->postbufq_cnt);
4409
4410 return NULL;
4411}
dea31012005-04-17 16:05:31 -05004412
James Smarte59058c2008-08-24 21:49:00 -04004413/**
4414 * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
4415 * unsolicited ct and els events.
4416 * @phba: Pointer to HBA context object.
4417 * @pring: Pointer to driver SLI ring object.
4418 * @phys: DMA address of the buffer.
4419 *
4420 * This function searches the buffer list using the dma_address
4421 * of unsolicited event to find the driver's lpfc_dmabuf object
4422 * corresponding to the dma_address. The function returns the
4423 * lpfc_dmabuf object if a buffer is found else it returns NULL.
4424 * This function is called by the ct and els unsolicited event
4425 * handlers to get the buffer associated with the unsolicited
4426 * event.
4427 *
4428 * This function is called with no lock held.
4429 **/
dea31012005-04-17 16:05:31 -05004430struct lpfc_dmabuf *
4431lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4432 dma_addr_t phys)
4433{
4434 struct lpfc_dmabuf *mp, *next_mp;
4435 struct list_head *slp = &pring->postbufq;
4436
4437 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05004438 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004439 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4440 if (mp->phys == phys) {
4441 list_del_init(&mp->list);
4442 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004443 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004444 return mp;
4445 }
4446 }
4447
James Smart2e0fef82007-06-17 19:56:36 -05004448 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004450 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05004451 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004452 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05004453 slp->next, slp->prev, pring->postbufq_cnt);
4454 return NULL;
4455}
4456
James Smarte59058c2008-08-24 21:49:00 -04004457/**
4458 * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
4459 * @phba: Pointer to HBA context object.
4460 * @cmdiocb: Pointer to driver command iocb object.
4461 * @rspiocb: Pointer to driver response iocb object.
4462 *
4463 * This function is the completion handler for the abort iocbs for
4464 * ELS commands. This function is called from the ELS ring event
4465 * handler with no lock held. This function frees memory resources
4466 * associated with the abort iocb.
4467 **/
dea31012005-04-17 16:05:31 -05004468static void
James Smart2e0fef82007-06-17 19:56:36 -05004469lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4470 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05004471{
James Smart2e0fef82007-06-17 19:56:36 -05004472 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04004473 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05004474 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04004475 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4476
4477 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04004478
4479 if (irsp->ulpStatus) {
4480 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
4481 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
4482
James Smart2e0fef82007-06-17 19:56:36 -05004483 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004484 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
4485 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
4486
James Smart92d7f7b2007-06-17 19:56:38 -05004487 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004488 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05004489 "with tag %x context %x, abort status %x, "
4490 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04004491 abort_iocb, abort_iotag, abort_context,
4492 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04004493
4494 /*
James Smart58da1ff2008-04-07 10:15:56 -04004495 * If the iocb is not found in Firmware queue the iocb
4496 * might have completed already. Do not free it again.
4497 */
James Smart9b379602008-04-07 10:16:00 -04004498 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04004499 spin_unlock_irq(&phba->hbalock);
4500 lpfc_sli_release_iocbq(phba, cmdiocb);
4501 return;
4502 }
4503 /*
James Smart2680eea2007-04-25 09:52:55 -04004504 * make sure we have the right iocbq before taking it
4505 * off the txcmplq and try to call completion routine.
4506 */
James Smart2e0fef82007-06-17 19:56:36 -05004507 if (!abort_iocb ||
4508 abort_iocb->iocb.ulpContext != abort_context ||
4509 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
4510 spin_unlock_irq(&phba->hbalock);
4511 else {
James Smart92d7f7b2007-06-17 19:56:38 -05004512 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04004513 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004514 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004515
James Smart0ff10d42008-01-11 01:52:36 -05004516 /* Firmware could still be in progress of DMAing
4517 * payload, so don't free data buffer till after
4518 * a hbeat.
4519 */
4520 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
4521
James Smart92d7f7b2007-06-17 19:56:38 -05004522 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4523 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4524 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
4525 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04004526 }
4527 }
4528
James Bottomley604a3e32005-10-29 10:28:33 -05004529 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05004530 return;
4531}
4532
James Smarte59058c2008-08-24 21:49:00 -04004533/**
4534 * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
4535 * @phba: Pointer to HBA context object.
4536 * @cmdiocb: Pointer to driver command iocb object.
4537 * @rspiocb: Pointer to driver response iocb object.
4538 *
4539 * The function is called from SLI ring event handler with no
4540 * lock held. This function is the completion handler for ELS commands
4541 * which are aborted. The function frees memory resources used for
4542 * the aborted ELS commands.
4543 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004544static void
4545lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4546 struct lpfc_iocbq *rspiocb)
4547{
4548 IOCB_t *irsp = &rspiocb->iocb;
4549
4550 /* ELS cmd tag <ulpIoTag> completes */
4551 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04004552 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004553 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004554 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05004555 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05004556 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
4557 lpfc_ct_free_iocb(phba, cmdiocb);
4558 else
4559 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05004560 return;
4561}
4562
James Smarte59058c2008-08-24 21:49:00 -04004563/**
4564 * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
4565 * @phba: Pointer to HBA context object.
4566 * @pring: Pointer to driver SLI ring object.
4567 * @cmdiocb: Pointer to driver command iocb object.
4568 *
4569 * This function issues an abort iocb for the provided command
4570 * iocb. This function is called with hbalock held.
4571 * The function returns 0 when it fails due to memory allocation
4572 * failure or when the command iocb is an abort request.
4573 **/
dea31012005-04-17 16:05:31 -05004574int
James Smart2e0fef82007-06-17 19:56:36 -05004575lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4576 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05004577{
James Smart2e0fef82007-06-17 19:56:36 -05004578 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004579 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05004580 IOCB_t *icmd = NULL;
4581 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04004582 int retval = IOCB_ERROR;
4583
James Smart92d7f7b2007-06-17 19:56:38 -05004584 /*
4585 * There are certain command types we don't want to abort. And we
4586 * don't want to abort commands that are already in the process of
4587 * being aborted.
James Smart07951072007-04-25 09:51:38 -04004588 */
4589 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004590 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05004591 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4592 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04004593 return 0;
4594
James Smart858c9f62007-06-17 19:56:39 -05004595 /* If we're unloading, don't abort iocb on the ELS ring, but change the
4596 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04004597 */
James Smart858c9f62007-06-17 19:56:39 -05004598 if ((vport->load_flag & FC_UNLOADING) &&
4599 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004600 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
4601 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
4602 else
4603 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04004604 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05004605 }
dea31012005-04-17 16:05:31 -05004606
4607 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05004608 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004609 if (abtsiocbp == NULL)
4610 return 0;
dea31012005-04-17 16:05:31 -05004611
James Smart07951072007-04-25 09:51:38 -04004612 /* This signals the response to set the correct status
4613 * before calling the completion handler.
4614 */
4615 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4616
dea31012005-04-17 16:05:31 -05004617 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04004618 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
4619 iabt->un.acxri.abortContextTag = icmd->ulpContext;
4620 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004621 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04004622 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05004623
James Smart2e0fef82007-06-17 19:56:36 -05004624 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04004625 iabt->ulpCommand = CMD_ABORT_XRI_CN;
4626 else
4627 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
4628
4629 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04004630
James Smarte8b62012007-08-02 11:10:09 -04004631 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4632 "0339 Abort xri x%x, original iotag x%x, "
4633 "abort cmd iotag x%x\n",
4634 iabt->un.acxri.abortContextTag,
4635 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05004636 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04004637
James Smartd7c255b2008-08-24 21:50:00 -04004638 if (retval)
4639 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04004640abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05004641 /*
4642 * Caller to this routine should check for IOCB_ERROR
4643 * and handle it properly. This routine no longer removes
4644 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04004645 */
James Smart2e0fef82007-06-17 19:56:36 -05004646 return retval;
dea31012005-04-17 16:05:31 -05004647}
4648
James Smarte59058c2008-08-24 21:49:00 -04004649/**
4650 * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
4651 * associated with a vport/SCSI target/lun.
4652 * @iocbq: Pointer to driver iocb object.
4653 * @vport: Pointer to driver virtual port object.
4654 * @tgt_id: SCSI ID of the target.
4655 * @lun_id: LUN ID of the scsi device.
4656 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
4657 *
4658 * This function acts as iocb filter for functions which abort or count
4659 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
4660 * 0 if the filtering criteria is met for the given iocb and will return
4661 * 1 if the filtering criteria is not met.
4662 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
4663 * given iocb is for the SCSI device specified by vport, tgt_id and
4664 * lun_id parameter.
4665 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
4666 * given iocb is for the SCSI target specified by vport and tgt_id
4667 * parameters.
4668 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
4669 * given iocb is for the SCSI host associated with the given vport.
4670 * This function is called with no locks held.
4671 **/
dea31012005-04-17 16:05:31 -05004672static int
James Smart51ef4c22007-08-02 11:10:31 -04004673lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
4674 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004675 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004676{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004677 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004678 int rc = 1;
4679
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004680 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
4681 return rc;
4682
James Smart51ef4c22007-08-02 11:10:31 -04004683 if (iocbq->vport != vport)
4684 return rc;
4685
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004686 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004687
James Smart495a7142008-06-14 22:52:59 -04004688 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05004689 return rc;
4690
4691 switch (ctx_cmd) {
4692 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04004693 if ((lpfc_cmd->rdata->pnode) &&
4694 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
4695 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05004696 rc = 0;
4697 break;
4698 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04004699 if ((lpfc_cmd->rdata->pnode) &&
4700 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05004701 rc = 0;
4702 break;
dea31012005-04-17 16:05:31 -05004703 case LPFC_CTX_HOST:
4704 rc = 0;
4705 break;
4706 default:
4707 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004708 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05004709 break;
4710 }
4711
4712 return rc;
4713}
4714
James Smarte59058c2008-08-24 21:49:00 -04004715/**
4716 * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
4717 * @vport: Pointer to virtual port.
4718 * @tgt_id: SCSI ID of the target.
4719 * @lun_id: LUN ID of the scsi device.
4720 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4721 *
4722 * This function returns number of FCP commands pending for the vport.
4723 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
4724 * commands pending on the vport associated with SCSI device specified
4725 * by tgt_id and lun_id parameters.
4726 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
4727 * commands pending on the vport associated with SCSI target specified
4728 * by tgt_id parameter.
4729 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
4730 * commands pending on the vport.
4731 * This function returns the number of iocbs which satisfy the filter.
4732 * This function is called without any lock held.
4733 **/
dea31012005-04-17 16:05:31 -05004734int
James Smart51ef4c22007-08-02 11:10:31 -04004735lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
4736 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004737{
James Smart51ef4c22007-08-02 11:10:31 -04004738 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004739 struct lpfc_iocbq *iocbq;
4740 int sum, i;
dea31012005-04-17 16:05:31 -05004741
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004742 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
4743 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004744
James Smart51ef4c22007-08-02 11:10:31 -04004745 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
4746 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004747 sum++;
dea31012005-04-17 16:05:31 -05004748 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004749
dea31012005-04-17 16:05:31 -05004750 return sum;
4751}
4752
James Smarte59058c2008-08-24 21:49:00 -04004753/**
4754 * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
4755 * FCP iocb.
4756 * @phba: Pointer to HBA context object
4757 * @cmdiocb: Pointer to command iocb object.
4758 * @rspiocb: Pointer to response iocb object.
4759 *
4760 * This function is called when an aborted FCP iocb completes. This
4761 * function is called by the ring event handler with no lock held.
4762 * This function frees the iocb.
4763 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004764void
James Smart2e0fef82007-06-17 19:56:36 -05004765lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4766 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004767{
James Bottomley604a3e32005-10-29 10:28:33 -05004768 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004769 return;
4770}
4771
James Smarte59058c2008-08-24 21:49:00 -04004772/**
4773 * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
4774 * pending on a SCSI host(vport)/target/lun.
4775 * @vport: Pointer to virtual port.
4776 * @pring: Pointer to driver SLI ring object.
4777 * @tgt_id: SCSI ID of the target.
4778 * @lun_id: LUN ID of the scsi device.
4779 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4780 *
4781 * This function sends an abort command for every SCSI command
4782 * associated with the given virtual port pending on the ring
4783 * filtered by lpfc_sli_validate_fcp_iocb function.
4784 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
4785 * FCP iocbs associated with lun specified by tgt_id and lun_id
4786 * parameters
4787 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
4788 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
4789 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
4790 * FCP iocbs associated with virtual port.
4791 * This function returns number of iocbs it failed to abort.
4792 * This function is called with no locks held.
4793 **/
dea31012005-04-17 16:05:31 -05004794int
James Smart51ef4c22007-08-02 11:10:31 -04004795lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
4796 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05004797{
James Smart51ef4c22007-08-02 11:10:31 -04004798 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004799 struct lpfc_iocbq *iocbq;
4800 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004801 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004802 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004803 int i;
dea31012005-04-17 16:05:31 -05004804
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004805 for (i = 1; i <= phba->sli.last_iotag; i++) {
4806 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004807
James Smart51ef4c22007-08-02 11:10:31 -04004808 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05004809 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05004810 continue;
4811
4812 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004813 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004814 if (abtsiocb == NULL) {
4815 errcnt++;
4816 continue;
4817 }
dea31012005-04-17 16:05:31 -05004818
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004819 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05004820 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
4821 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
4822 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
4823 abtsiocb->iocb.ulpLe = 1;
4824 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05004825 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05004826
James Smart2e0fef82007-06-17 19:56:36 -05004827 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004828 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
4829 else
4830 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
4831
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004832 /* Setup callback routine and issue the command. */
4833 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05004834 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
4835 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05004836 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05004837 errcnt++;
4838 continue;
4839 }
4840 }
4841
4842 return errcnt;
4843}
4844
James Smarte59058c2008-08-24 21:49:00 -04004845/**
4846 * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
4847 * lpfc_sli_issue_iocb_wait.
4848 * @phba: Pointer to HBA context object.
4849 * @cmdiocbq: Pointer to command iocb.
4850 * @rspiocbq: Pointer to response iocb.
4851 *
4852 * This function is the completion handler for iocbs issued using
4853 * lpfc_sli_issue_iocb_wait function. This function is called by the
4854 * ring event handler function without any lock held. This function
4855 * can be called from both worker thread context and interrupt
4856 * context. This function also can be called from other thread which
4857 * cleans up the SLI layer objects.
4858 * This function copy the contents of the response iocb to the
4859 * response iocb memory object provided by the caller of
4860 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4861 * sleeps for the iocb completion.
4862 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004863static void
4864lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
4865 struct lpfc_iocbq *cmdiocbq,
4866 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05004867{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004868 wait_queue_head_t *pdone_q;
4869 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004870
James Smart2e0fef82007-06-17 19:56:36 -05004871 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004872 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4873 if (cmdiocbq->context2 && rspiocbq)
4874 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4875 &rspiocbq->iocb, sizeof(IOCB_t));
4876
4877 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004878 if (pdone_q)
4879 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05004880 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05004881 return;
4882}
4883
James Smarte59058c2008-08-24 21:49:00 -04004884/**
4885 * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
4886 * @phba: Pointer to HBA context object..
4887 * @pring: Pointer to sli ring.
4888 * @piocb: Pointer to command iocb.
4889 * @prspiocbq: Pointer to response iocb.
4890 * @timeout: Timeout in number of seconds.
4891 *
4892 * This function issues the iocb to firmware and waits for the
4893 * iocb to complete. If the iocb command is not
4894 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
4895 * Caller should not free the iocb resources if this function
4896 * returns IOCB_TIMEDOUT.
4897 * The function waits for the iocb completion using an
4898 * non-interruptible wait.
4899 * This function will sleep while waiting for iocb completion.
4900 * So, this function should not be called from any context which
4901 * does not allow sleeping. Due to the same reason, this function
4902 * cannot be called with interrupt disabled.
4903 * This function assumes that the iocb completions occur while
4904 * this function sleep. So, this function cannot be called from
4905 * the thread which process iocb completion for this ring.
4906 * This function clears the iocb_flag of the iocb object before
4907 * issuing the iocb and the iocb completion handler sets this
4908 * flag and wakes this thread when the iocb completes.
4909 * The contents of the response iocb will be copied to prspiocbq
4910 * by the completion handler when the command completes.
4911 * This function returns IOCB_SUCCESS when success.
4912 * This function is called with no lock held.
4913 **/
dea31012005-04-17 16:05:31 -05004914int
James Smart2e0fef82007-06-17 19:56:36 -05004915lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
4916 struct lpfc_sli_ring *pring,
4917 struct lpfc_iocbq *piocb,
4918 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004919 uint32_t timeout)
dea31012005-04-17 16:05:31 -05004920{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08004921 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004922 long timeleft, timeout_req = 0;
4923 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004924 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05004925
4926 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004927 * If the caller has provided a response iocbq buffer, then context2
4928 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05004929 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004930 if (prspiocbq) {
4931 if (piocb->context2)
4932 return IOCB_ERROR;
4933 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05004934 }
4935
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004936 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
4937 piocb->context_un.wait_queue = &done_q;
4938 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05004939
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004940 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4941 creg_val = readl(phba->HCregaddr);
4942 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
4943 writel(creg_val, phba->HCregaddr);
4944 readl(phba->HCregaddr); /* flush */
4945 }
4946
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004947 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
4948 if (retval == IOCB_SUCCESS) {
4949 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004950 timeleft = wait_event_timeout(done_q,
4951 piocb->iocb_flag & LPFC_IO_WAKE,
4952 timeout_req);
dea31012005-04-17 16:05:31 -05004953
James Smart7054a602007-04-25 09:52:34 -04004954 if (piocb->iocb_flag & LPFC_IO_WAKE) {
4955 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004956 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04004957 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004958 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004959 "0338 IOCB wait timeout error - no "
4960 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004961 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04004962 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004963 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004964 "0330 IOCB wake NOT set, "
4965 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004966 timeout, (timeleft / jiffies));
4967 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05004968 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004969 } else {
4970 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04004971 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004972 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004973 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05004974 }
4975
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004976 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4977 creg_val = readl(phba->HCregaddr);
4978 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
4979 writel(creg_val, phba->HCregaddr);
4980 readl(phba->HCregaddr); /* flush */
4981 }
4982
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004983 if (prspiocbq)
4984 piocb->context2 = NULL;
4985
4986 piocb->context_un.wait_queue = NULL;
4987 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05004988 return retval;
4989}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004990
James Smarte59058c2008-08-24 21:49:00 -04004991/**
4992 * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
4993 * @phba: Pointer to HBA context object.
4994 * @pmboxq: Pointer to driver mailbox object.
4995 * @timeout: Timeout in number of seconds.
4996 *
4997 * This function issues the mailbox to firmware and waits for the
4998 * mailbox command to complete. If the mailbox command is not
4999 * completed within timeout seconds, it returns MBX_TIMEOUT.
5000 * The function waits for the mailbox completion using an
5001 * interruptible wait. If the thread is woken up due to a
5002 * signal, MBX_TIMEOUT error is returned to the caller. Caller
5003 * should not free the mailbox resources, if this function returns
5004 * MBX_TIMEOUT.
5005 * This function will sleep while waiting for mailbox completion.
5006 * So, this function should not be called from any context which
5007 * does not allow sleeping. Due to the same reason, this function
5008 * cannot be called with interrupt disabled.
5009 * This function assumes that the mailbox completion occurs while
5010 * this function sleep. So, this function cannot be called from
5011 * the worker thread which processes mailbox completion.
5012 * This function is called in the context of HBA management
5013 * applications.
5014 * This function returns MBX_SUCCESS when successful.
5015 * This function is called with no lock held.
5016 **/
dea31012005-04-17 16:05:31 -05005017int
James Smart2e0fef82007-06-17 19:56:36 -05005018lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05005019 uint32_t timeout)
5020{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08005021 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05005022 int retval;
James Smart858c9f62007-06-17 19:56:39 -05005023 unsigned long flag;
dea31012005-04-17 16:05:31 -05005024
5025 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04005026 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05005027 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005028
James Smart495a7142008-06-14 22:52:59 -04005029 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05005030 /* setup wake call as IOCB callback */
5031 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
5032 /* setup context field to pass wait_queue pointer to wake function */
5033 pmboxq->context1 = &done_q;
5034
dea31012005-04-17 16:05:31 -05005035 /* now issue the command */
5036 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
5037
5038 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04005039 wait_event_interruptible_timeout(done_q,
5040 pmboxq->mbox_flag & LPFC_MBX_WAKE,
5041 timeout * HZ);
5042
James Smart858c9f62007-06-17 19:56:39 -05005043 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005044 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04005045 /*
5046 * if LPFC_MBX_WAKE flag is set the mailbox is completed
5047 * else do not free the resources.
5048 */
5049 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05005050 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05005051 else {
James Smart7054a602007-04-25 09:52:34 -04005052 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05005053 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5054 }
5055 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005056 }
5057
dea31012005-04-17 16:05:31 -05005058 return retval;
5059}
5060
James Smarte59058c2008-08-24 21:49:00 -04005061/**
5062 * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
5063 * @phba: Pointer to HBA context.
5064 *
5065 * This function is called to cleanup any pending mailbox
5066 * objects in the driver queue before bringing the HBA offline.
5067 * This function is called while resetting the HBA.
5068 * The function is called without any lock held. The function
5069 * takes hbalock to update SLI data structure.
5070 * This function returns 1 when there is an active mailbox
5071 * command pending else returns 0.
5072 **/
James Smartb4c02652006-07-06 15:50:43 -04005073int
5074lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
5075{
James Smart2e0fef82007-06-17 19:56:36 -05005076 struct lpfc_vport *vport = phba->pport;
James Smartb4c02652006-07-06 15:50:43 -04005077 int i = 0;
James Smarted957682007-06-17 19:56:37 -05005078 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04005079
James Smart2e0fef82007-06-17 19:56:36 -05005080 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
James Smartb4c02652006-07-06 15:50:43 -04005081 if (i++ > LPFC_MBOX_TMO * 1000)
5082 return 1;
5083
James Smarted957682007-06-17 19:56:37 -05005084 /*
5085 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
5086 * did finish. This way we won't get the misleading
5087 * "Stray Mailbox Interrupt" message.
5088 */
5089 spin_lock_irq(&phba->hbalock);
5090 ha_copy = phba->work_ha;
5091 phba->work_ha &= ~HA_MBATT;
5092 spin_unlock_irq(&phba->hbalock);
5093
5094 if (ha_copy & HA_MBATT)
5095 if (lpfc_sli_handle_mb_event(phba) == 0)
5096 i = 0;
James Smartb4c02652006-07-06 15:50:43 -04005097
5098 msleep(1);
5099 }
5100
5101 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
5102}
5103
James Smarte59058c2008-08-24 21:49:00 -04005104/**
James Smart93996272008-08-24 21:50:30 -04005105 * lpfc_sli_check_eratt: check error attention events
5106 * @phba: Pointer to HBA context.
5107 *
5108 * This function is called form timer soft interrupt context to check HBA's
5109 * error attention register bit for error attention events.
5110 *
5111 * This fucntion returns 1 when there is Error Attention in the Host Attention
5112 * Register and returns 0 otherwise.
5113 **/
5114int
5115lpfc_sli_check_eratt(struct lpfc_hba *phba)
5116{
5117 uint32_t ha_copy;
5118
James Smarteaf15d52008-12-04 22:39:29 -05005119 /* If PCI channel is offline, don't process it */
5120 if (unlikely(pci_channel_offline(phba->pcidev)))
5121 return 0;
5122
James Smart93996272008-08-24 21:50:30 -04005123 /* If somebody is waiting to handle an eratt, don't process it
5124 * here. The brdkill function will do this.
5125 */
5126 if (phba->link_flag & LS_IGNORE_ERATT)
5127 return 0;
5128
5129 /* Check if interrupt handler handles this ERATT */
5130 spin_lock_irq(&phba->hbalock);
5131 if (phba->hba_flag & HBA_ERATT_HANDLED) {
5132 /* Interrupt handler has handled ERATT */
5133 spin_unlock_irq(&phba->hbalock);
5134 return 0;
5135 }
5136
5137 /* Read chip Host Attention (HA) register */
5138 ha_copy = readl(phba->HAregaddr);
5139 if (ha_copy & HA_ERATT) {
5140 /* Read host status register to retrieve error event */
5141 lpfc_sli_read_hs(phba);
5142 /* Set the driver HA work bitmap */
5143 phba->work_ha |= HA_ERATT;
5144 /* Indicate polling handles this ERATT */
5145 phba->hba_flag |= HBA_ERATT_HANDLED;
5146 spin_unlock_irq(&phba->hbalock);
5147 return 1;
5148 }
5149 spin_unlock_irq(&phba->hbalock);
5150 return 0;
5151}
5152
5153/**
5154 * lpfc_sp_intr_handler: The slow-path interrupt handler of lpfc driver.
James Smarte59058c2008-08-24 21:49:00 -04005155 * @irq: Interrupt number.
5156 * @dev_id: The device context pointer.
5157 *
James Smart93996272008-08-24 21:50:30 -04005158 * This function is directly called from the PCI layer as an interrupt
5159 * service routine when the device is enabled with MSI-X multi-message
5160 * interrupt mode and there are slow-path events in the HBA. However,
5161 * when the device is enabled with either MSI or Pin-IRQ interrupt mode,
5162 * this function is called as part of the device-level interrupt handler.
5163 * When the PCI slot is in error recovery or the HBA is undergoing
5164 * initialization, the interrupt handler will not process the interrupt.
5165 * The link attention and ELS ring attention events are handled by the
5166 * worker thread. The interrupt handler signals the worker thread and
5167 * and returns for these events. This function is called without any
5168 * lock held. It gets the hbalock to access and update SLI data
5169 * structures.
5170 *
5171 * This function returns IRQ_HANDLED when interrupt is handled else it
5172 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04005173 **/
dea31012005-04-17 16:05:31 -05005174irqreturn_t
James Smart93996272008-08-24 21:50:30 -04005175lpfc_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05005176{
James Smart2e0fef82007-06-17 19:56:36 -05005177 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05005178 uint32_t ha_copy;
5179 uint32_t work_ha_copy;
5180 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05005181 unsigned long iflag;
dea31012005-04-17 16:05:31 -05005182 uint32_t control;
5183
James Smart92d7f7b2007-06-17 19:56:38 -05005184 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05005185 struct lpfc_vport *vport;
5186 struct lpfc_nodelist *ndlp;
5187 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05005188 LPFC_MBOXQ_t *pmb;
5189 int rc;
5190
dea31012005-04-17 16:05:31 -05005191 /*
5192 * Get the driver's phba structure from the dev_id and
5193 * assume the HBA is not interrupting.
5194 */
James Smart93996272008-08-24 21:50:30 -04005195 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05005196
5197 if (unlikely(!phba))
5198 return IRQ_NONE;
5199
dea31012005-04-17 16:05:31 -05005200 /*
James Smart93996272008-08-24 21:50:30 -04005201 * Stuff needs to be attented to when this function is invoked as an
5202 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005203 */
James Smart93996272008-08-24 21:50:30 -04005204 if (phba->intr_type == MSIX) {
5205 /* If the pci channel is offline, ignore all the interrupts */
5206 if (unlikely(pci_channel_offline(phba->pcidev)))
5207 return IRQ_NONE;
5208 /* Update device-level interrupt statistics */
5209 phba->sli.slistat.sli_intr++;
5210 /* Ignore all interrupts during initialization. */
5211 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5212 return IRQ_NONE;
5213 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05005214 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04005215 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04005216 /* If somebody is waiting to handle an eratt don't process it
5217 * here. The brdkill function will do this.
5218 */
5219 if (phba->link_flag & LS_IGNORE_ERATT)
5220 ha_copy &= ~HA_ERATT;
5221 /* Check the need for handling ERATT in interrupt handler */
5222 if (ha_copy & HA_ERATT) {
5223 if (phba->hba_flag & HBA_ERATT_HANDLED)
5224 /* ERATT polling has handled ERATT */
5225 ha_copy &= ~HA_ERATT;
5226 else
5227 /* Indicate interrupt handler handles ERATT */
5228 phba->hba_flag |= HBA_ERATT_HANDLED;
5229 }
5230 /* Clear up only attention source related to slow-path */
5231 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
5232 phba->HAregaddr);
5233 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005234 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005235 } else
5236 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05005237
dea31012005-04-17 16:05:31 -05005238 work_ha_copy = ha_copy & phba->work_ha_mask;
5239
James Smart93996272008-08-24 21:50:30 -04005240 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05005241 if (work_ha_copy & HA_LATT) {
5242 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
5243 /*
5244 * Turn off Link Attention interrupts
5245 * until CLEAR_LA done
5246 */
James Smart5b75da22008-12-04 22:39:35 -05005247 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005248 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
5249 control = readl(phba->HCregaddr);
5250 control &= ~HC_LAINT_ENA;
5251 writel(control, phba->HCregaddr);
5252 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005253 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005254 }
5255 else
5256 work_ha_copy &= ~HA_LATT;
5257 }
5258
James Smart93996272008-08-24 21:50:30 -04005259 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05005260 /*
5261 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
5262 * the only slow ring.
5263 */
5264 status = (work_ha_copy &
5265 (HA_RXMASK << (4*LPFC_ELS_RING)));
5266 status >>= (4*LPFC_ELS_RING);
5267 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05005268 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05005269 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04005270
5271 lpfc_debugfs_slow_ring_trc(phba,
5272 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
5273 control, status,
5274 (uint32_t)phba->sli.slistat.sli_intr);
5275
James Smart858c9f62007-06-17 19:56:39 -05005276 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04005277 lpfc_debugfs_slow_ring_trc(phba,
5278 "ISR Disable ring:"
5279 "pwork:x%x hawork:x%x wait:x%x",
5280 phba->work_ha, work_ha_copy,
5281 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005282 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005283
James Smart858c9f62007-06-17 19:56:39 -05005284 control &=
5285 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05005286 writel(control, phba->HCregaddr);
5287 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05005288 }
James Smarta58cbd52007-08-02 11:09:43 -04005289 else {
5290 lpfc_debugfs_slow_ring_trc(phba,
5291 "ISR slow ring: pwork:"
5292 "x%x hawork:x%x wait:x%x",
5293 phba->work_ha, work_ha_copy,
5294 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005295 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005296 }
James Smart5b75da22008-12-04 22:39:35 -05005297 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005298 }
5299 }
James Smart5b75da22008-12-04 22:39:35 -05005300 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005301 if (work_ha_copy & HA_ERATT)
5302 lpfc_sli_read_hs(phba);
5303 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005304 pmb = phba->sli.mbox_active;
5305 pmbox = &pmb->mb;
James Smart34b02dc2008-08-24 21:49:55 -04005306 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05005307 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005308
5309 /* First check out the status word */
5310 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
5311 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05005312 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05005313 /*
5314 * Stray Mailbox Interrupt, mbxCommand <cmd>
5315 * mbxStatus <status>
5316 */
James Smart09372822008-01-11 01:52:54 -05005317 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05005318 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005319 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05005320 "Interrupt mbxCommand x%x "
5321 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005322 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05005323 pmbox->mbxCommand,
5324 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05005325 /* clear mailbox attention bit */
5326 work_ha_copy &= ~HA_MBATT;
5327 } else {
James Smart97eab632008-04-07 10:16:05 -04005328 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05005329 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05005330 phba->last_completion_time = jiffies;
5331 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05005332 if (pmb->mbox_cmpl) {
5333 lpfc_sli_pcimem_bcopy(mbox, pmbox,
5334 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05005335 }
James Smart09372822008-01-11 01:52:54 -05005336 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
5337 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
5338
5339 lpfc_debugfs_disc_trc(vport,
5340 LPFC_DISC_TRC_MBOX_VPORT,
5341 "MBOX dflt rpi: : "
5342 "status:x%x rpi:x%x",
5343 (uint32_t)pmbox->mbxStatus,
5344 pmbox->un.varWords[0], 0);
5345
5346 if (!pmbox->mbxStatus) {
5347 mp = (struct lpfc_dmabuf *)
5348 (pmb->context1);
5349 ndlp = (struct lpfc_nodelist *)
5350 pmb->context2;
5351
5352 /* Reg_LOGIN of dflt RPI was
5353 * successful. new lets get
5354 * rid of the RPI using the
5355 * same mbox buffer.
5356 */
5357 lpfc_unreg_login(phba,
5358 vport->vpi,
5359 pmbox->un.varWords[0],
5360 pmb);
5361 pmb->mbox_cmpl =
5362 lpfc_mbx_cmpl_dflt_rpi;
5363 pmb->context1 = mp;
5364 pmb->context2 = ndlp;
5365 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04005366 rc = lpfc_sli_issue_mbox(phba,
5367 pmb,
5368 MBX_NOWAIT);
5369 if (rc != MBX_BUSY)
5370 lpfc_printf_log(phba,
5371 KERN_ERR,
5372 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04005373 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04005374 "been MBX_BUSY");
James Smart09372822008-01-11 01:52:54 -05005375 goto send_current_mbox;
5376 }
5377 }
James Smart5b75da22008-12-04 22:39:35 -05005378 spin_lock_irqsave(
5379 &phba->pport->work_port_lock,
5380 iflag);
James Smart09372822008-01-11 01:52:54 -05005381 phba->pport->work_port_events &=
5382 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05005383 spin_unlock_irqrestore(
5384 &phba->pport->work_port_lock,
5385 iflag);
James Smart09372822008-01-11 01:52:54 -05005386 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05005387 }
James Smart97eab632008-04-07 10:16:05 -04005388 } else
James Smart5b75da22008-12-04 22:39:35 -05005389 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005390
James Smart92d7f7b2007-06-17 19:56:38 -05005391 if ((work_ha_copy & HA_MBATT) &&
5392 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05005393send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05005394 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04005395 do {
5396 rc = lpfc_sli_issue_mbox(phba, NULL,
5397 MBX_NOWAIT);
5398 } while (rc == MBX_NOT_FINISHED);
5399 if (rc != MBX_SUCCESS)
5400 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
5401 LOG_SLI, "0349 rc should be "
5402 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05005403 }
5404
James Smart5b75da22008-12-04 22:39:35 -05005405 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005406 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05005407 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04005408 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005409 }
James Smart93996272008-08-24 21:50:30 -04005410 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05005411
James Smart93996272008-08-24 21:50:30 -04005412} /* lpfc_sp_intr_handler */
5413
5414/**
5415 * lpfc_fp_intr_handler: The fast-path interrupt handler of lpfc driver.
5416 * @irq: Interrupt number.
5417 * @dev_id: The device context pointer.
5418 *
5419 * This function is directly called from the PCI layer as an interrupt
5420 * service routine when the device is enabled with MSI-X multi-message
5421 * interrupt mode and there is a fast-path FCP IOCB ring event in the
5422 * HBA. However, when the device is enabled with either MSI or Pin-IRQ
5423 * interrupt mode, this function is called as part of the device-level
5424 * interrupt handler. When the PCI slot is in error recovery or the HBA
5425 * is undergoing initialization, the interrupt handler will not process
5426 * the interrupt. The SCSI FCP fast-path ring event are handled in the
5427 * intrrupt context. This function is called without any lock held. It
5428 * gets the hbalock to access and update SLI data structures.
5429 *
5430 * This function returns IRQ_HANDLED when interrupt is handled else it
5431 * returns IRQ_NONE.
5432 **/
5433irqreturn_t
5434lpfc_fp_intr_handler(int irq, void *dev_id)
5435{
5436 struct lpfc_hba *phba;
5437 uint32_t ha_copy;
5438 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05005439 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04005440
5441 /* Get the driver's phba structure from the dev_id and
5442 * assume the HBA is not interrupting.
5443 */
5444 phba = (struct lpfc_hba *) dev_id;
5445
5446 if (unlikely(!phba))
5447 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05005448
5449 /*
James Smart93996272008-08-24 21:50:30 -04005450 * Stuff needs to be attented to when this function is invoked as an
5451 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005452 */
James Smart93996272008-08-24 21:50:30 -04005453 if (phba->intr_type == MSIX) {
5454 /* If pci channel is offline, ignore all the interrupts */
5455 if (unlikely(pci_channel_offline(phba->pcidev)))
5456 return IRQ_NONE;
5457 /* Update device-level interrupt statistics */
5458 phba->sli.slistat.sli_intr++;
5459 /* Ignore all interrupts during initialization. */
5460 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5461 return IRQ_NONE;
5462 /* Need to read HA REG for FCP ring and other ring events */
5463 ha_copy = readl(phba->HAregaddr);
5464 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05005465 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005466 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
5467 phba->HAregaddr);
5468 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005469 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005470 } else
5471 ha_copy = phba->ha_copy;
5472
5473 /*
5474 * Process all events on FCP ring. Take the optimized path for FCP IO.
5475 */
5476 ha_copy &= ~(phba->work_ha_mask);
5477
5478 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05005479 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05005480 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05005481 lpfc_sli_handle_fast_ring_event(phba,
5482 &phba->sli.ring[LPFC_FCP_RING],
5483 status);
James Smarta4bc3372006-12-02 13:34:16 -05005484
5485 if (phba->cfg_multi_ring_support == 2) {
5486 /*
James Smart93996272008-08-24 21:50:30 -04005487 * Process all events on extra ring. Take the optimized path
5488 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05005489 */
James Smart93996272008-08-24 21:50:30 -04005490 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05005491 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05005492 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05005493 lpfc_sli_handle_fast_ring_event(phba,
5494 &phba->sli.ring[LPFC_EXTRA_RING],
5495 status);
5496 }
5497 }
dea31012005-04-17 16:05:31 -05005498 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -04005499} /* lpfc_fp_intr_handler */
dea31012005-04-17 16:05:31 -05005500
James Smart93996272008-08-24 21:50:30 -04005501/**
5502 * lpfc_intr_handler: The device-level interrupt handler of lpfc driver.
5503 * @irq: Interrupt number.
5504 * @dev_id: The device context pointer.
5505 *
5506 * This function is the device-level interrupt handler called from the PCI
5507 * layer when either MSI or Pin-IRQ interrupt mode is enabled and there is
5508 * an event in the HBA which requires driver attention. This function
5509 * invokes the slow-path interrupt attention handling function and fast-path
5510 * interrupt attention handling function in turn to process the relevant
5511 * HBA attention events. This function is called without any lock held. It
5512 * gets the hbalock to access and update SLI data structures.
5513 *
5514 * This function returns IRQ_HANDLED when interrupt is handled, else it
5515 * returns IRQ_NONE.
5516 **/
5517irqreturn_t
5518lpfc_intr_handler(int irq, void *dev_id)
5519{
5520 struct lpfc_hba *phba;
5521 irqreturn_t sp_irq_rc, fp_irq_rc;
5522 unsigned long status1, status2;
5523
5524 /*
5525 * Get the driver's phba structure from the dev_id and
5526 * assume the HBA is not interrupting.
5527 */
5528 phba = (struct lpfc_hba *) dev_id;
5529
5530 if (unlikely(!phba))
5531 return IRQ_NONE;
5532
5533 /* If the pci channel is offline, ignore all the interrupts. */
5534 if (unlikely(pci_channel_offline(phba->pcidev)))
5535 return IRQ_NONE;
5536
5537 /* Update device level interrupt statistics */
5538 phba->sli.slistat.sli_intr++;
5539
5540 /* Ignore all interrupts during initialization. */
5541 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5542 return IRQ_NONE;
5543
5544 spin_lock(&phba->hbalock);
5545 phba->ha_copy = readl(phba->HAregaddr);
5546 if (unlikely(!phba->ha_copy)) {
5547 spin_unlock(&phba->hbalock);
5548 return IRQ_NONE;
5549 } else if (phba->ha_copy & HA_ERATT) {
5550 if (phba->hba_flag & HBA_ERATT_HANDLED)
5551 /* ERATT polling has handled ERATT */
5552 phba->ha_copy &= ~HA_ERATT;
5553 else
5554 /* Indicate interrupt handler handles ERATT */
5555 phba->hba_flag |= HBA_ERATT_HANDLED;
5556 }
5557
5558 /* Clear attention sources except link and error attentions */
5559 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
5560 readl(phba->HAregaddr); /* flush */
5561 spin_unlock(&phba->hbalock);
5562
5563 /*
5564 * Invokes slow-path host attention interrupt handling as appropriate.
5565 */
5566
5567 /* status of events with mailbox and link attention */
5568 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
5569
5570 /* status of events with ELS ring */
5571 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
5572 status2 >>= (4*LPFC_ELS_RING);
5573
5574 if (status1 || (status2 & HA_RXMASK))
5575 sp_irq_rc = lpfc_sp_intr_handler(irq, dev_id);
5576 else
5577 sp_irq_rc = IRQ_NONE;
5578
5579 /*
5580 * Invoke fast-path host attention interrupt handling as appropriate.
5581 */
5582
5583 /* status of events with FCP ring */
5584 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
5585 status1 >>= (4*LPFC_FCP_RING);
5586
5587 /* status of events with extra ring */
5588 if (phba->cfg_multi_ring_support == 2) {
5589 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
5590 status2 >>= (4*LPFC_EXTRA_RING);
5591 } else
5592 status2 = 0;
5593
5594 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
5595 fp_irq_rc = lpfc_fp_intr_handler(irq, dev_id);
5596 else
5597 fp_irq_rc = IRQ_NONE;
5598
5599 /* Return device-level interrupt handling status */
5600 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
5601} /* lpfc_intr_handler */