blob: 01dfdc8696f8a430180be91d414817acf4ade009 [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 Smarte2a0a9d2008-12-04 22:40:02 -0500545
James Smarta58cbd52007-08-02 11:09:43 -0400546 if (pring->ringno == LPFC_ELS_RING) {
547 lpfc_debugfs_slow_ring_trc(phba,
548 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
549 *(((uint32_t *) &nextiocb->iocb) + 4),
550 *(((uint32_t *) &nextiocb->iocb) + 6),
551 *(((uint32_t *) &nextiocb->iocb) + 7));
552 }
553
dea31012005-04-17 16:05:31 -0500554 /*
555 * Issue iocb command to adapter
556 */
James Smart92d7f7b2007-06-17 19:56:38 -0500557 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500558 wmb();
559 pring->stats.iocb_cmd++;
560
561 /*
562 * If there is no completion routine to call, we can release the
563 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
564 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
565 */
566 if (nextiocb->iocb_cmpl)
567 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500568 else
James Smart2e0fef82007-06-17 19:56:36 -0500569 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500570
571 /*
572 * Let the HBA know what IOCB slot will be the next one the
573 * driver will put a command into.
574 */
575 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500576 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500577}
578
James Smarte59058c2008-08-24 21:49:00 -0400579/**
580 * lpfc_sli_update_full_ring: Update the chip attention register.
581 * @phba: Pointer to HBA context object.
582 * @pring: Pointer to driver SLI ring object.
583 *
584 * The caller is not required to hold any lock for calling this function.
585 * This function updates the chip attention bits for the ring to inform firmware
586 * that there are pending work to be done for this ring and requests an
587 * interrupt when there is space available in the ring. This function is
588 * called when the driver is unable to post more iocbs to the ring due
589 * to unavailability of space in the ring.
590 **/
dea31012005-04-17 16:05:31 -0500591static void
James Smart2e0fef82007-06-17 19:56:36 -0500592lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500593{
594 int ringno = pring->ringno;
595
596 pring->flag |= LPFC_CALL_RING_AVAILABLE;
597
598 wmb();
599
600 /*
601 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
602 * The HBA will tell us when an IOCB entry is available.
603 */
604 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
605 readl(phba->CAregaddr); /* flush */
606
607 pring->stats.iocb_cmd_full++;
608}
609
James Smarte59058c2008-08-24 21:49:00 -0400610/**
611 * lpfc_sli_update_ring: Update chip attention register.
612 * @phba: Pointer to HBA context object.
613 * @pring: Pointer to driver SLI ring object.
614 *
615 * This function updates the chip attention register bit for the
616 * given ring to inform HBA that there is more work to be done
617 * in this ring. The caller is not required to hold any lock.
618 **/
dea31012005-04-17 16:05:31 -0500619static void
James Smart2e0fef82007-06-17 19:56:36 -0500620lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500621{
622 int ringno = pring->ringno;
623
624 /*
625 * Tell the HBA that there is work to do in this ring.
626 */
James Smart34b02dc2008-08-24 21:49:55 -0400627 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
628 wmb();
629 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
630 readl(phba->CAregaddr); /* flush */
631 }
dea31012005-04-17 16:05:31 -0500632}
633
James Smarte59058c2008-08-24 21:49:00 -0400634/**
635 * lpfc_sli_resume_iocb: Process iocbs in the txq.
636 * @phba: Pointer to HBA context object.
637 * @pring: Pointer to driver SLI ring object.
638 *
639 * This function is called with hbalock held to post pending iocbs
640 * in the txq to the firmware. This function is called when driver
641 * detects space available in the ring.
642 **/
dea31012005-04-17 16:05:31 -0500643static void
James Smart2e0fef82007-06-17 19:56:36 -0500644lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500645{
646 IOCB_t *iocb;
647 struct lpfc_iocbq *nextiocb;
648
649 /*
650 * Check to see if:
651 * (a) there is anything on the txq to send
652 * (b) link is up
653 * (c) link attention events can be processed (fcp ring only)
654 * (d) IOCB processing is not blocked by the outstanding mbox command.
655 */
656 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500657 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500658 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -0400659 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -0500660
661 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
662 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
663 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
664
665 if (iocb)
666 lpfc_sli_update_ring(phba, pring);
667 else
668 lpfc_sli_update_full_ring(phba, pring);
669 }
670
671 return;
672}
673
James Smarte59058c2008-08-24 21:49:00 -0400674/**
675 * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
676 * @phba: Pointer to HBA context object.
677 * @hbqno: HBQ number.
678 *
679 * This function is called with hbalock held to get the next
680 * available slot for the given HBQ. If there is free slot
681 * available for the HBQ it will return pointer to the next available
682 * HBQ entry else it will return NULL.
683 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100684static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500685lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
686{
687 struct hbq_s *hbqp = &phba->hbqs[hbqno];
688
689 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
690 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
691 hbqp->next_hbqPutIdx = 0;
692
693 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500694 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500695 uint32_t getidx = le32_to_cpu(raw_index);
696
697 hbqp->local_hbqGetIdx = getidx;
698
699 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
700 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500701 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400702 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500703 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400704 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500705 hbqp->entry_count);
706
707 phba->link_state = LPFC_HBA_ERROR;
708 return NULL;
709 }
710
711 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
712 return NULL;
713 }
714
James Smart51ef4c22007-08-02 11:10:31 -0400715 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
716 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500717}
718
James Smarte59058c2008-08-24 21:49:00 -0400719/**
720 * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
721 * @phba: Pointer to HBA context object.
722 *
723 * This function is called with no lock held to free all the
724 * hbq buffers while uninitializing the SLI interface. It also
725 * frees the HBQ buffers returned by the firmware but not yet
726 * processed by the upper layers.
727 **/
James Smarted957682007-06-17 19:56:37 -0500728void
729lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
730{
James Smart92d7f7b2007-06-17 19:56:38 -0500731 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
732 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -0500733 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -0400734 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -0500735 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500736
James Smart51ef4c22007-08-02 11:10:31 -0400737 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500738 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -0500739 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -0400740 for (i = 0; i < hbq_count; ++i) {
741 list_for_each_entry_safe(dmabuf, next_dmabuf,
742 &phba->hbqs[i].hbq_buffer_list, list) {
743 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
744 list_del(&hbq_buf->dbuf.list);
745 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
746 }
James Smarta8adb832007-10-27 13:37:53 -0400747 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -0500748 }
James Smart3163f722008-02-08 18:50:25 -0500749 /* Return all HBQ buffer that are in-fly */
750 list_for_each_entry_safe(dmabuf, next_dmabuf,
751 &phba->hbqbuf_in_list, list) {
752 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
753 list_del(&hbq_buf->dbuf.list);
754 if (hbq_buf->tag == -1) {
755 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
756 (phba, hbq_buf);
757 } else {
758 hbqno = hbq_buf->tag >> 16;
759 if (hbqno >= LPFC_MAX_HBQS)
760 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
761 (phba, hbq_buf);
762 else
763 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
764 hbq_buf);
765 }
766 }
767
768 /* Mark the HBQs not in use */
769 phba->hbq_in_use = 0;
770 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -0500771}
772
James Smarte59058c2008-08-24 21:49:00 -0400773/**
774 * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
775 * @phba: Pointer to HBA context object.
776 * @hbqno: HBQ number.
777 * @hbq_buf: Pointer to HBQ buffer.
778 *
779 * This function is called with the hbalock held to post a
780 * hbq buffer to the firmware. If the function finds an empty
781 * slot in the HBQ, it will post the buffer. The function will return
782 * pointer to the hbq entry if it successfully post the buffer
783 * else it will return NULL.
784 **/
James Smart51ef4c22007-08-02 11:10:31 -0400785static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500786lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500787 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500788{
789 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500790 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500791
792 /* Get next HBQ entry slot to use */
793 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
794 if (hbqe) {
795 struct hbq_s *hbqp = &phba->hbqs[hbqno];
796
James Smart92d7f7b2007-06-17 19:56:38 -0500797 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
798 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400799 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500800 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500801 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
802 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
803 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500804 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
805 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500806 /* flush */
James Smarted957682007-06-17 19:56:37 -0500807 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400808 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smarted957682007-06-17 19:56:37 -0500809 }
James Smart51ef4c22007-08-02 11:10:31 -0400810 return hbqe;
James Smarted957682007-06-17 19:56:37 -0500811}
812
James Smarte59058c2008-08-24 21:49:00 -0400813/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -0500814static struct lpfc_hbq_init lpfc_els_hbq = {
815 .rn = 1,
816 .entry_count = 200,
817 .mask_count = 0,
818 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400819 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500820 .buffer_count = 0,
821 .init_count = 20,
822 .add_count = 5,
823};
James Smarted957682007-06-17 19:56:37 -0500824
James Smarte59058c2008-08-24 21:49:00 -0400825/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -0400826static struct lpfc_hbq_init lpfc_extra_hbq = {
827 .rn = 1,
828 .entry_count = 200,
829 .mask_count = 0,
830 .profile = 0,
831 .ring_mask = (1 << LPFC_EXTRA_RING),
832 .buffer_count = 0,
833 .init_count = 0,
834 .add_count = 5,
835};
836
James Smarte59058c2008-08-24 21:49:00 -0400837/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -0400838struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500839 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400840 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500841};
842
James Smarte59058c2008-08-24 21:49:00 -0400843/**
844 * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
845 * @phba: Pointer to HBA context object.
846 * @hbqno: HBQ number.
847 * @count: Number of HBQ buffers to be posted.
848 *
James Smartd7c255b2008-08-24 21:50:00 -0400849 * This function is called with no lock held to post more hbq buffers to the
850 * given HBQ. The function returns the number of HBQ buffers successfully
851 * posted.
James Smarte59058c2008-08-24 21:49:00 -0400852 **/
James Smart311464e2007-08-02 11:10:37 -0400853static int
James Smart92d7f7b2007-06-17 19:56:38 -0500854lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
855{
James Smartd7c255b2008-08-24 21:50:00 -0400856 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -0500857 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500858 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -0400859 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700860 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -0400861 return 0;
James Smart51ef4c22007-08-02 11:10:31 -0400862
James Smartd7c255b2008-08-24 21:50:00 -0400863 if ((phba->hbqs[hbqno].buffer_count + count) >
864 lpfc_hbq_defs[hbqno]->entry_count)
865 count = lpfc_hbq_defs[hbqno]->entry_count -
866 phba->hbqs[hbqno].buffer_count;
867 if (!count)
868 return 0;
869 /* Allocate HBQ entries */
870 for (i = 0; i < count; i++) {
871 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
872 if (!hbq_buffer)
873 break;
874 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
875 }
James Smart3163f722008-02-08 18:50:25 -0500876 /* Check whether HBQ is still in use */
877 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700878 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -0400879 goto err;
880 while (!list_empty(&hbq_buf_list)) {
881 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
882 dbuf.list);
883 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
884 (hbqno << 16));
885 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -0400886 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -0400887 posted++;
888 } else
James Smart51ef4c22007-08-02 11:10:31 -0400889 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -0500890 }
James Smart3163f722008-02-08 18:50:25 -0500891 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -0400892 return posted;
893err:
894 spin_unlock_irqrestore(&phba->hbalock, flags);
895 while (!list_empty(&hbq_buf_list)) {
896 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
897 dbuf.list);
898 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
899 }
James Smart92d7f7b2007-06-17 19:56:38 -0500900 return 0;
James Smarted957682007-06-17 19:56:37 -0500901}
902
James Smarte59058c2008-08-24 21:49:00 -0400903/**
904 * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
905 * @phba: Pointer to HBA context object.
906 * @qno: HBQ number.
907 *
908 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -0400909 * is called with no lock held. The function returns the number of HBQ entries
910 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400911 **/
James Smarted957682007-06-17 19:56:37 -0500912int
James Smart92d7f7b2007-06-17 19:56:38 -0500913lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500914{
James Smart92d7f7b2007-06-17 19:56:38 -0500915 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
916 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -0500917}
918
James Smarte59058c2008-08-24 21:49:00 -0400919/**
920 * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
921 * @phba: Pointer to HBA context object.
922 * @qno: HBQ queue number.
923 *
924 * This function is called from SLI initialization code path with
925 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -0400926 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400927 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100928static int
James Smart92d7f7b2007-06-17 19:56:38 -0500929lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500930{
James Smart92d7f7b2007-06-17 19:56:38 -0500931 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
932 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -0500933}
934
James Smarte59058c2008-08-24 21:49:00 -0400935/**
936 * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
937 * @phba: Pointer to HBA context object.
938 * @tag: Tag of the hbq buffer.
939 *
940 * This function is called with hbalock held. This function searches
941 * for the hbq buffer associated with the given tag in the hbq buffer
942 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
943 * it returns NULL.
944 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100945static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -0500946lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
947{
James Smart92d7f7b2007-06-17 19:56:38 -0500948 struct lpfc_dmabuf *d_buf;
949 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400950 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500951
James Smart51ef4c22007-08-02 11:10:31 -0400952 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +0200953 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -0400954 return NULL;
955
956 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -0500957 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -0400958 if (hbq_buf->tag == tag) {
James Smart92d7f7b2007-06-17 19:56:38 -0500959 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -0500960 }
961 }
James Smart92d7f7b2007-06-17 19:56:38 -0500962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400963 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -0400964 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -0500965 return NULL;
James Smarted957682007-06-17 19:56:37 -0500966}
967
James Smarte59058c2008-08-24 21:49:00 -0400968/**
969 * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
970 * @phba: Pointer to HBA context object.
971 * @hbq_buffer: Pointer to HBQ buffer.
972 *
973 * This function is called with hbalock. This function gives back
974 * the hbq buffer to firmware. If the HBQ does not have space to
975 * post the buffer, it will free the buffer.
976 **/
James Smarted957682007-06-17 19:56:37 -0500977void
James Smart51ef4c22007-08-02 11:10:31 -0400978lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -0500979{
980 uint32_t hbqno;
981
James Smart51ef4c22007-08-02 11:10:31 -0400982 if (hbq_buffer) {
983 hbqno = hbq_buffer->tag >> 16;
984 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
985 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
986 }
James Smarted957682007-06-17 19:56:37 -0500987 }
988}
989
James Smarte59058c2008-08-24 21:49:00 -0400990/**
991 * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
992 * @mbxCommand: mailbox command code.
993 *
994 * This function is called by the mailbox event handler function to verify
995 * that the completed mailbox command is a legitimate mailbox command. If the
996 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
997 * and the mailbox event handler will take the HBA offline.
998 **/
dea31012005-04-17 16:05:31 -0500999static int
1000lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1001{
1002 uint8_t ret;
1003
1004 switch (mbxCommand) {
1005 case MBX_LOAD_SM:
1006 case MBX_READ_NV:
1007 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001008 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001009 case MBX_RUN_BIU_DIAG:
1010 case MBX_INIT_LINK:
1011 case MBX_DOWN_LINK:
1012 case MBX_CONFIG_LINK:
1013 case MBX_CONFIG_RING:
1014 case MBX_RESET_RING:
1015 case MBX_READ_CONFIG:
1016 case MBX_READ_RCONFIG:
1017 case MBX_READ_SPARM:
1018 case MBX_READ_STATUS:
1019 case MBX_READ_RPI:
1020 case MBX_READ_XRI:
1021 case MBX_READ_REV:
1022 case MBX_READ_LNK_STAT:
1023 case MBX_REG_LOGIN:
1024 case MBX_UNREG_LOGIN:
1025 case MBX_READ_LA:
1026 case MBX_CLEAR_LA:
1027 case MBX_DUMP_MEMORY:
1028 case MBX_DUMP_CONTEXT:
1029 case MBX_RUN_DIAGS:
1030 case MBX_RESTART:
1031 case MBX_UPDATE_CFG:
1032 case MBX_DOWN_LOAD:
1033 case MBX_DEL_LD_ENTRY:
1034 case MBX_RUN_PROGRAM:
1035 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001036 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001037 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001038 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001039 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001040 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001041 case MBX_LOAD_AREA:
1042 case MBX_RUN_BIU_DIAG64:
1043 case MBX_CONFIG_PORT:
1044 case MBX_READ_SPARM64:
1045 case MBX_READ_RPI64:
1046 case MBX_REG_LOGIN64:
1047 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001048 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001049 case MBX_SET_DEBUG:
1050 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001051 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001052 case MBX_REG_VPI:
1053 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001054 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001055 case MBX_PORT_CAPABILITIES:
1056 case MBX_PORT_IOV_CONTROL:
dea31012005-04-17 16:05:31 -05001057 ret = mbxCommand;
1058 break;
1059 default:
1060 ret = MBX_SHUTDOWN;
1061 break;
1062 }
James Smart2e0fef82007-06-17 19:56:36 -05001063 return ret;
dea31012005-04-17 16:05:31 -05001064}
James Smarte59058c2008-08-24 21:49:00 -04001065
1066/**
1067 * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
1068 * lpfc_sli_issue_mbox_wait.
1069 * @phba: Pointer to HBA context object.
1070 * @pmboxq: Pointer to mailbox command.
1071 *
1072 * This is completion handler function for mailbox commands issued from
1073 * lpfc_sli_issue_mbox_wait function. This function is called by the
1074 * mailbox event handler function with no lock held. This function
1075 * will wake up thread waiting on the wait queue pointed by context1
1076 * of the mailbox.
1077 **/
dea31012005-04-17 16:05:31 -05001078static void
James Smart2e0fef82007-06-17 19:56:36 -05001079lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001080{
1081 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001082 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001083
1084 /*
1085 * If pdone_q is empty, the driver thread gave up waiting and
1086 * continued running.
1087 */
James Smart7054a602007-04-25 09:52:34 -04001088 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001089 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001090 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1091 if (pdone_q)
1092 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001093 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001094 return;
1095}
1096
James Smarte59058c2008-08-24 21:49:00 -04001097
1098/**
1099 * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
1100 * @phba: Pointer to HBA context object.
1101 * @pmb: Pointer to mailbox object.
1102 *
1103 * This function is the default mailbox completion handler. It
1104 * frees the memory resources associated with the completed mailbox
1105 * command. If the completed command is a REG_LOGIN mailbox command,
1106 * this function will issue a UREG_LOGIN to re-claim the RPI.
1107 **/
dea31012005-04-17 16:05:31 -05001108void
James Smart2e0fef82007-06-17 19:56:36 -05001109lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001110{
1111 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -04001112 uint16_t rpi;
1113 int rc;
1114
dea31012005-04-17 16:05:31 -05001115 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001116
dea31012005-04-17 16:05:31 -05001117 if (mp) {
1118 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1119 kfree(mp);
1120 }
James Smart7054a602007-04-25 09:52:34 -04001121
1122 /*
1123 * If a REG_LOGIN succeeded after node is destroyed or node
1124 * is in re-discovery driver need to cleanup the RPI.
1125 */
James Smart2e0fef82007-06-17 19:56:36 -05001126 if (!(phba->pport->load_flag & FC_UNLOADING) &&
1127 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1128 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -04001129
1130 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -05001131 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1132 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001133 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1134 if (rc != MBX_NOT_FINISHED)
1135 return;
1136 }
1137
James Smart2e0fef82007-06-17 19:56:36 -05001138 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001139 return;
1140}
1141
James Smarte59058c2008-08-24 21:49:00 -04001142/**
1143 * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
1144 * @phba: Pointer to HBA context object.
1145 *
1146 * This function is called with no lock held. This function processes all
1147 * the completed mailbox commands and gives it to upper layers. The interrupt
1148 * service routine processes mailbox completion interrupt and adds completed
1149 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1150 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1151 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1152 * function returns the mailbox commands to the upper layer by calling the
1153 * completion handler function of each mailbox.
1154 **/
dea31012005-04-17 16:05:31 -05001155int
James Smart2e0fef82007-06-17 19:56:36 -05001156lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001157{
James Smart92d7f7b2007-06-17 19:56:38 -05001158 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001159 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001160 int rc;
1161 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001162
1163 phba->sli.slistat.mbox_event++;
1164
James Smart92d7f7b2007-06-17 19:56:38 -05001165 /* Get all completed mailboxe buffers into the cmplq */
1166 spin_lock_irq(&phba->hbalock);
1167 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1168 spin_unlock_irq(&phba->hbalock);
1169
dea31012005-04-17 16:05:31 -05001170 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001171 do {
1172 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1173 if (pmb == NULL)
1174 break;
1175
dea31012005-04-17 16:05:31 -05001176 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -05001177
James Smart858c9f62007-06-17 19:56:39 -05001178 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1179 if (pmb->vport) {
1180 lpfc_debugfs_disc_trc(pmb->vport,
1181 LPFC_DISC_TRC_MBOX_VPORT,
1182 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1183 (uint32_t)pmbox->mbxCommand,
1184 pmbox->un.varWords[0],
1185 pmbox->un.varWords[1]);
1186 }
1187 else {
1188 lpfc_debugfs_disc_trc(phba->pport,
1189 LPFC_DISC_TRC_MBOX,
1190 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1191 (uint32_t)pmbox->mbxCommand,
1192 pmbox->un.varWords[0],
1193 pmbox->un.varWords[1]);
1194 }
1195 }
1196
dea31012005-04-17 16:05:31 -05001197 /*
1198 * It is a fatal error if unknown mbox command completion.
1199 */
1200 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1201 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001202 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001203 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001204 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -05001205 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001206 pmb->vport ? pmb->vport->vpi : 0,
1207 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -05001208 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001209 phba->work_hs = HS_FFER3;
1210 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001211 continue;
dea31012005-04-17 16:05:31 -05001212 }
1213
dea31012005-04-17 16:05:31 -05001214 if (pmbox->mbxStatus) {
1215 phba->sli.slistat.mbox_stat_err++;
1216 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1217 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001218 lpfc_printf_log(phba, KERN_INFO,
1219 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001220 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001221 "error - RETRYing Data: x%x "
1222 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001223 pmb->vport ? pmb->vport->vpi :0,
1224 pmbox->mbxCommand,
1225 pmbox->mbxStatus,
1226 pmbox->un.varWords[0],
1227 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001228 pmbox->mbxStatus = 0;
1229 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -05001230 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001231 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001232 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001233 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1234 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -05001235 continue;
dea31012005-04-17 16:05:31 -05001236 }
1237 }
1238
1239 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001240 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001241 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -05001242 "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 -05001243 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001244 pmbox->mbxCommand,
1245 pmb->mbox_cmpl,
1246 *((uint32_t *) pmbox),
1247 pmbox->un.varWords[0],
1248 pmbox->un.varWords[1],
1249 pmbox->un.varWords[2],
1250 pmbox->un.varWords[3],
1251 pmbox->un.varWords[4],
1252 pmbox->un.varWords[5],
1253 pmbox->un.varWords[6],
1254 pmbox->un.varWords[7]);
1255
James Smart92d7f7b2007-06-17 19:56:38 -05001256 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001257 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001258 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001259 return 0;
dea31012005-04-17 16:05:31 -05001260}
James Smart92d7f7b2007-06-17 19:56:38 -05001261
James Smarte59058c2008-08-24 21:49:00 -04001262/**
James Smarte59058c2008-08-24 21:49:00 -04001263 * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
1264 * @phba: Pointer to HBA context object.
1265 * @pring: Pointer to driver SLI ring object.
1266 * @tag: buffer tag.
1267 *
1268 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1269 * is set in the tag the buffer is posted for a particular exchange,
1270 * the function will return the buffer without replacing the buffer.
1271 * If the buffer is for unsolicited ELS or CT traffic, this function
1272 * returns the buffer and also posts another buffer to the firmware.
1273 **/
James Smart76bb24e2007-10-27 13:38:00 -04001274static struct lpfc_dmabuf *
1275lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001276 struct lpfc_sli_ring *pring,
1277 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001278{
James Smart9f1e1b52008-12-04 22:39:40 -05001279 struct hbq_dmabuf *hbq_entry;
1280
James Smart76bb24e2007-10-27 13:38:00 -04001281 if (tag & QUE_BUFTAG_BIT)
1282 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001283 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1284 if (!hbq_entry)
1285 return NULL;
1286 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001287}
James Smart57127f12007-10-27 13:37:05 -04001288
James Smarte59058c2008-08-24 21:49:00 -04001289
1290/**
1291 * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
1292 * @phba: Pointer to HBA context object.
1293 * @pring: Pointer to driver SLI ring object.
1294 * @saveq: Pointer to the unsolicited iocb.
1295 *
1296 * This function is called with no lock held by the ring event handler
1297 * when there is an unsolicited iocb posted to the response ring by the
1298 * firmware. This function gets the buffer associated with the iocbs
1299 * and calls the event handler for the ring. This function handles both
1300 * qring buffers and hbq buffers.
1301 * When the function returns 1 the caller can free the iocb object otherwise
1302 * upper layer functions will free the iocb objects.
1303 **/
dea31012005-04-17 16:05:31 -05001304static int
1305lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1306 struct lpfc_iocbq *saveq)
1307{
1308 IOCB_t * irsp;
1309 WORD5 * w5p;
1310 uint32_t Rctl, Type;
1311 uint32_t match, i;
James Smart76bb24e2007-10-27 13:38:00 -04001312 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001313 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001314
1315 match = 0;
1316 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001317
1318 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1319 if (pring->lpfc_sli_rcv_async_status)
1320 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1321 else
1322 lpfc_printf_log(phba,
1323 KERN_WARNING,
1324 LOG_SLI,
1325 "0316 Ring %d handler: unexpected "
1326 "ASYNC_STATUS iocb received evt_code "
1327 "0x%x\n",
1328 pring->ringno,
1329 irsp->un.asyncstat.evt_code);
1330 return 1;
1331 }
1332
James Smart3163f722008-02-08 18:50:25 -05001333 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1334 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1335 if (irsp->ulpBdeCount > 0) {
1336 dmzbuf = lpfc_sli_get_buff(phba, pring,
1337 irsp->un.ulpWord[3]);
1338 lpfc_in_buf_free(phba, dmzbuf);
1339 }
1340
1341 if (irsp->ulpBdeCount > 1) {
1342 dmzbuf = lpfc_sli_get_buff(phba, pring,
1343 irsp->unsli3.sli3Words[3]);
1344 lpfc_in_buf_free(phba, dmzbuf);
1345 }
1346
1347 if (irsp->ulpBdeCount > 2) {
1348 dmzbuf = lpfc_sli_get_buff(phba, pring,
1349 irsp->unsli3.sli3Words[7]);
1350 lpfc_in_buf_free(phba, dmzbuf);
1351 }
1352
1353 return 1;
1354 }
1355
James Smart92d7f7b2007-06-17 19:56:38 -05001356 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001357 if (irsp->ulpBdeCount != 0) {
1358 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001359 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001360 if (!saveq->context2)
1361 lpfc_printf_log(phba,
1362 KERN_ERR,
1363 LOG_SLI,
1364 "0341 Ring %d Cannot find buffer for "
1365 "an unsolicited iocb. tag 0x%x\n",
1366 pring->ringno,
1367 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001368 }
1369 if (irsp->ulpBdeCount == 2) {
1370 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04001371 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04001372 if (!saveq->context3)
1373 lpfc_printf_log(phba,
1374 KERN_ERR,
1375 LOG_SLI,
1376 "0342 Ring %d Cannot find buffer for an"
1377 " unsolicited iocb. tag 0x%x\n",
1378 pring->ringno,
1379 irsp->unsli3.sli3Words[7]);
1380 }
1381 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04001382 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04001383 if (irsp->ulpBdeCount != 0) {
1384 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1385 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05001386 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04001387 lpfc_printf_log(phba,
1388 KERN_ERR,
1389 LOG_SLI,
1390 "0343 Ring %d Cannot find "
1391 "buffer for an unsolicited iocb"
1392 ". tag 0x%x\n", pring->ringno,
1393 irsp->un.ulpWord[3]);
1394 }
1395 if (irsp->ulpBdeCount == 2) {
1396 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1397 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05001398 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04001399 lpfc_printf_log(phba,
1400 KERN_ERR,
1401 LOG_SLI,
1402 "0344 Ring %d Cannot find "
1403 "buffer for an unsolicited "
1404 "iocb. tag 0x%x\n",
1405 pring->ringno,
1406 irsp->unsli3.sli3Words[7]);
1407 }
1408 }
James Smart92d7f7b2007-06-17 19:56:38 -05001409 }
James Smart9c2face2008-01-11 01:53:18 -05001410 if (irsp->ulpBdeCount != 0 &&
1411 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1412 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1413 int found = 0;
1414
1415 /* search continue save q for same XRI */
1416 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1417 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1418 list_add_tail(&saveq->list, &iocbq->list);
1419 found = 1;
1420 break;
1421 }
1422 }
1423 if (!found)
1424 list_add_tail(&saveq->clist,
1425 &pring->iocb_continue_saveq);
1426 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1427 list_del_init(&iocbq->clist);
1428 saveq = iocbq;
1429 irsp = &(saveq->iocb);
1430 } else
1431 return 0;
1432 }
1433 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1434 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1435 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1436 Rctl = FC_ELS_REQ;
1437 Type = FC_ELS_DATA;
1438 } else {
1439 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1440 Rctl = w5p->hcsw.Rctl;
1441 Type = w5p->hcsw.Type;
1442
1443 /* Firmware Workaround */
1444 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1445 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1446 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1447 Rctl = FC_ELS_REQ;
1448 Type = FC_ELS_DATA;
1449 w5p->hcsw.Rctl = Rctl;
1450 w5p->hcsw.Type = Type;
1451 }
1452 }
James Smart92d7f7b2007-06-17 19:56:38 -05001453
dea31012005-04-17 16:05:31 -05001454 /* unSolicited Responses */
1455 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001456 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1457 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1458 saveq);
dea31012005-04-17 16:05:31 -05001459 match = 1;
1460 } else {
1461 /* We must search, based on rctl / type
1462 for the right routine */
James Smart9c2face2008-01-11 01:53:18 -05001463 for (i = 0; i < pring->num_mask; i++) {
1464 if ((pring->prt[i].rctl == Rctl)
1465 && (pring->prt[i].type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001466 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1467 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1468 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -05001469 match = 1;
1470 break;
1471 }
1472 }
1473 }
1474 if (match == 0) {
1475 /* Unexpected Rctl / Type received */
1476 /* Ring <ringno> handler: unexpected
1477 Rctl <Rctl> Type <Type> received */
James Smart92d7f7b2007-06-17 19:56:38 -05001478 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001479 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001480 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001481 pring->ringno, Rctl, Type);
dea31012005-04-17 16:05:31 -05001482 }
James Smart92d7f7b2007-06-17 19:56:38 -05001483 return 1;
dea31012005-04-17 16:05:31 -05001484}
1485
James Smarte59058c2008-08-24 21:49:00 -04001486/**
1487 * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
1488 * @phba: Pointer to HBA context object.
1489 * @pring: Pointer to driver SLI ring object.
1490 * @prspiocb: Pointer to response iocb object.
1491 *
1492 * This function looks up the iocb_lookup table to get the command iocb
1493 * corresponding to the given response iocb using the iotag of the
1494 * response iocb. This function is called with the hbalock held.
1495 * This function returns the command iocb object if it finds the command
1496 * iocb else returns NULL.
1497 **/
dea31012005-04-17 16:05:31 -05001498static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001499lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1500 struct lpfc_sli_ring *pring,
1501 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001502{
dea31012005-04-17 16:05:31 -05001503 struct lpfc_iocbq *cmd_iocb = NULL;
1504 uint16_t iotag;
1505
James Bottomley604a3e32005-10-29 10:28:33 -05001506 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001507
James Bottomley604a3e32005-10-29 10:28:33 -05001508 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1509 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001510 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001511 pring->txcmplq_cnt--;
1512 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001513 }
1514
dea31012005-04-17 16:05:31 -05001515 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001516 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001517 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001518 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001519 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001520 return NULL;
1521}
1522
James Smarte59058c2008-08-24 21:49:00 -04001523/**
1524 * lpfc_sli_process_sol_iocb: process solicited iocb completion.
1525 * @phba: Pointer to HBA context object.
1526 * @pring: Pointer to driver SLI ring object.
1527 * @saveq: Pointer to the response iocb to be processed.
1528 *
1529 * This function is called by the ring event handler for non-fcp
1530 * rings when there is a new response iocb in the response ring.
1531 * The caller is not required to hold any locks. This function
1532 * gets the command iocb associated with the response iocb and
1533 * calls the completion handler for the command iocb. If there
1534 * is no completion handler, the function will free the resources
1535 * associated with command iocb. If the response iocb is for
1536 * an already aborted command iocb, the status of the completion
1537 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1538 * This function always returns 1.
1539 **/
dea31012005-04-17 16:05:31 -05001540static int
James Smart2e0fef82007-06-17 19:56:36 -05001541lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001542 struct lpfc_iocbq *saveq)
1543{
James Smart2e0fef82007-06-17 19:56:36 -05001544 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001545 int rc = 1;
1546 unsigned long iflag;
1547
1548 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001549 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001550 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001551 spin_unlock_irqrestore(&phba->hbalock, iflag);
1552
dea31012005-04-17 16:05:31 -05001553 if (cmdiocbp) {
1554 if (cmdiocbp->iocb_cmpl) {
1555 /*
James Smartea2151b2008-09-07 11:52:10 -04001556 * If an ELS command failed send an event to mgmt
1557 * application.
1558 */
1559 if (saveq->iocb.ulpStatus &&
1560 (pring->ringno == LPFC_ELS_RING) &&
1561 (cmdiocbp->iocb.ulpCommand ==
1562 CMD_ELS_REQUEST64_CR))
1563 lpfc_send_els_failure_event(phba,
1564 cmdiocbp, saveq);
1565
1566 /*
dea31012005-04-17 16:05:31 -05001567 * Post all ELS completions to the worker thread.
1568 * All other are passed to the completion callback.
1569 */
1570 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001571 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1572 cmdiocbp->iocb_flag &=
1573 ~LPFC_DRIVER_ABORTED;
1574 saveq->iocb.ulpStatus =
1575 IOSTAT_LOCAL_REJECT;
1576 saveq->iocb.un.ulpWord[4] =
1577 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05001578
1579 /* Firmware could still be in progress
1580 * of DMAing payload, so don't free data
1581 * buffer till after a hbeat.
1582 */
1583 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04001584 }
dea31012005-04-17 16:05:31 -05001585 }
James Smart2e0fef82007-06-17 19:56:36 -05001586 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001587 } else
1588 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001589 } else {
1590 /*
1591 * Unknown initiating command based on the response iotag.
1592 * This could be the case on the ELS ring because of
1593 * lpfc_els_abort().
1594 */
1595 if (pring->ringno != LPFC_ELS_RING) {
1596 /*
1597 * Ring <ringno> handler: unexpected completion IoTag
1598 * <IoTag>
1599 */
James Smarte8b62012007-08-02 11:10:09 -04001600 lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1601 "0322 Ring %d handler: "
1602 "unexpected completion IoTag x%x "
1603 "Data: x%x x%x x%x x%x\n",
1604 pring->ringno,
1605 saveq->iocb.ulpIoTag,
1606 saveq->iocb.ulpStatus,
1607 saveq->iocb.un.ulpWord[4],
1608 saveq->iocb.ulpCommand,
1609 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001610 }
1611 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001612
dea31012005-04-17 16:05:31 -05001613 return rc;
1614}
1615
James Smarte59058c2008-08-24 21:49:00 -04001616/**
1617 * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
1618 * @phba: Pointer to HBA context object.
1619 * @pring: Pointer to driver SLI ring object.
1620 *
1621 * This function is called from the iocb ring event handlers when
1622 * put pointer is ahead of the get pointer for a ring. This function signal
1623 * an error attention condition to the worker thread and the worker
1624 * thread will transition the HBA to offline state.
1625 **/
James Smart2e0fef82007-06-17 19:56:36 -05001626static void
1627lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001628{
James Smart34b02dc2008-08-24 21:49:55 -04001629 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001630 /*
1631 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1632 * rsp ring <portRspMax>
1633 */
1634 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001635 "0312 Ring %d handler: portRspPut %d "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001636 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001637 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001638 pring->numRiocb);
1639
James Smart2e0fef82007-06-17 19:56:36 -05001640 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001641
1642 /*
1643 * All error attention handlers are posted to
1644 * worker thread
1645 */
1646 phba->work_ha |= HA_ERATT;
1647 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001648
James Smart5e9d9b82008-06-14 22:52:53 -04001649 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001650
1651 return;
1652}
1653
James Smarte59058c2008-08-24 21:49:00 -04001654/**
James Smart93996272008-08-24 21:50:30 -04001655 * lpfc_poll_eratt: Error attention polling timer timeout handler.
1656 * @ptr: Pointer to address of HBA context object.
1657 *
1658 * This function is invoked by the Error Attention polling timer when the
1659 * timer times out. It will check the SLI Error Attention register for
1660 * possible attention events. If so, it will post an Error Attention event
1661 * and wake up worker thread to process it. Otherwise, it will set up the
1662 * Error Attention polling timer for the next poll.
1663 **/
1664void lpfc_poll_eratt(unsigned long ptr)
1665{
1666 struct lpfc_hba *phba;
1667 uint32_t eratt = 0;
1668
1669 phba = (struct lpfc_hba *)ptr;
1670
1671 /* Check chip HA register for error event */
1672 eratt = lpfc_sli_check_eratt(phba);
1673
1674 if (eratt)
1675 /* Tell the worker thread there is work to do */
1676 lpfc_worker_wake_up(phba);
1677 else
1678 /* Restart the timer for next eratt poll */
1679 mod_timer(&phba->eratt_poll, jiffies +
1680 HZ * LPFC_ERATT_POLL_INTERVAL);
1681 return;
1682}
1683
1684/**
James Smarte59058c2008-08-24 21:49:00 -04001685 * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
1686 * @phba: Pointer to HBA context object.
1687 *
1688 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1689 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1690 * is enabled.
1691 *
1692 * The caller does not hold any lock.
1693 * The function processes each response iocb in the response ring until it
1694 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1695 * LE bit set. The function will call the completion handler of the command iocb
1696 * if the response iocb indicates a completion for a command iocb or it is
1697 * an abort completion.
1698 **/
James Smart2e0fef82007-06-17 19:56:36 -05001699void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001700{
James Smart2e0fef82007-06-17 19:56:36 -05001701 struct lpfc_sli *psli = &phba->sli;
1702 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001703 IOCB_t *irsp = NULL;
1704 IOCB_t *entry = NULL;
1705 struct lpfc_iocbq *cmdiocbq = NULL;
1706 struct lpfc_iocbq rspiocbq;
James Smart34b02dc2008-08-24 21:49:55 -04001707 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001708 uint32_t status;
1709 uint32_t portRspPut, portRspMax;
1710 int type;
1711 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001712 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001713 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001714
1715 pring->stats.iocb_event++;
1716
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001717 /*
1718 * The next available response entry should never exceed the maximum
1719 * entries. If it does, treat it as an adapter hardware error.
1720 */
1721 portRspMax = pring->numRiocb;
1722 portRspPut = le32_to_cpu(pgp->rspPutInx);
1723 if (unlikely(portRspPut >= portRspMax)) {
1724 lpfc_sli_rsp_pointers_error(phba, pring);
1725 return;
1726 }
1727
1728 rmb();
1729 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001730 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001731 if (++pring->rspidx >= portRspMax)
1732 pring->rspidx = 0;
1733
1734 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1735 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001736 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001737 irsp = &rspiocbq.iocb;
1738 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1739 pring->stats.iocb_rsp++;
1740 rsp_cmpl++;
1741
1742 if (unlikely(irsp->ulpStatus)) {
1743 /* Rsp ring <ringno> error: IOCB */
1744 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001745 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001746 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001747 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001748 irsp->un.ulpWord[0],
1749 irsp->un.ulpWord[1],
1750 irsp->un.ulpWord[2],
1751 irsp->un.ulpWord[3],
1752 irsp->un.ulpWord[4],
1753 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001754 *(uint32_t *)&irsp->un1,
1755 *((uint32_t *)&irsp->un1 + 1));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001756 }
1757
1758 switch (type) {
1759 case LPFC_ABORT_IOCB:
1760 case LPFC_SOL_IOCB:
1761 /*
1762 * Idle exchange closed via ABTS from port. No iocb
1763 * resources need to be recovered.
1764 */
1765 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001766 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001767 "0314 IOCB cmd 0x%x "
1768 "processed. Skipping "
1769 "completion",
James Smartdca94792006-08-01 07:34:08 -04001770 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001771 break;
1772 }
1773
James Smart2e0fef82007-06-17 19:56:36 -05001774 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001775 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1776 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001777 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001778 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1779 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1780 &rspiocbq);
1781 }
1782 break;
1783 default:
1784 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1785 char adaptermsg[LPFC_MAX_ADPTMSG];
1786 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1787 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1788 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001789 dev_warn(&((phba->pcidev)->dev),
1790 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001791 phba->brd_no, adaptermsg);
1792 } else {
1793 /* Unknown IOCB command */
1794 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001795 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001796 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001797 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001798 irsp->ulpStatus,
1799 irsp->ulpIoTag,
1800 irsp->ulpContext);
1801 }
1802 break;
1803 }
1804
1805 /*
1806 * The response IOCB has been processed. Update the ring
1807 * pointer in SLIM. If the port response put pointer has not
1808 * been updated, sync the pgp->rspPutInx and fetch the new port
1809 * response put pointer.
1810 */
James Smarted957682007-06-17 19:56:37 -05001811 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001812
1813 if (pring->rspidx == portRspPut)
1814 portRspPut = le32_to_cpu(pgp->rspPutInx);
1815 }
1816
1817 ha_copy = readl(phba->HAregaddr);
1818 ha_copy >>= (LPFC_FCP_RING * 4);
1819
1820 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001821 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001822 pring->stats.iocb_rsp_full++;
1823 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1824 writel(status, phba->CAregaddr);
1825 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001826 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001827 }
1828 if ((ha_copy & HA_R0CE_RSP) &&
1829 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001830 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001831 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1832 pring->stats.iocb_cmd_empty++;
1833
1834 /* Force update of the local copy of cmdGetInx */
1835 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1836 lpfc_sli_resume_iocb(phba, pring);
1837
1838 if ((pring->lpfc_sli_cmd_available))
1839 (pring->lpfc_sli_cmd_available) (phba, pring);
1840
James Smart2e0fef82007-06-17 19:56:36 -05001841 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001842 }
1843
1844 return;
1845}
1846
James Smarte59058c2008-08-24 21:49:00 -04001847/**
1848 * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
1849 * @phba: Pointer to HBA context object.
1850 * @pring: Pointer to driver SLI ring object.
1851 * @mask: Host attention register mask for this ring.
1852 *
1853 * This function is called from the interrupt context when there is a ring
1854 * event for the fcp ring. The caller does not hold any lock.
1855 * The function processes each response iocb in the response ring until it
1856 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1857 * LE bit set. The function will call the completion handler of the command iocb
1858 * if the response iocb indicates a completion for a command iocb or it is
1859 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
1860 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05001861 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04001862 * to check it explicitly. This function always returns 1.
1863 **/
dea31012005-04-17 16:05:31 -05001864static int
James Smart2e0fef82007-06-17 19:56:36 -05001865lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1866 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001867{
James Smart34b02dc2008-08-24 21:49:55 -04001868 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001869 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001870 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001871 struct lpfc_iocbq *cmdiocbq = NULL;
1872 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001873 uint32_t status;
1874 uint32_t portRspPut, portRspMax;
1875 int rc = 1;
1876 lpfc_iocb_type type;
1877 unsigned long iflag;
1878 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05001879
James Smart2e0fef82007-06-17 19:56:36 -05001880 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001881 pring->stats.iocb_event++;
1882
dea31012005-04-17 16:05:31 -05001883 /*
1884 * The next available response entry should never exceed the maximum
1885 * entries. If it does, treat it as an adapter hardware error.
1886 */
1887 portRspMax = pring->numRiocb;
1888 portRspPut = le32_to_cpu(pgp->rspPutInx);
1889 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001890 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05001891 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001892 return 1;
1893 }
1894
1895 rmb();
1896 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001897 /*
1898 * Fetch an entry off the ring and copy it into a local data
1899 * structure. The copy involves a byte-swap since the
1900 * network byte order and pci byte orders are different.
1901 */
James Smarted957682007-06-17 19:56:37 -05001902 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05001903 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001904
1905 if (++pring->rspidx >= portRspMax)
1906 pring->rspidx = 0;
1907
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001908 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1909 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05001910 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05001911 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001912 irsp = &rspiocbq.iocb;
1913
dea31012005-04-17 16:05:31 -05001914 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1915 pring->stats.iocb_rsp++;
1916 rsp_cmpl++;
1917
1918 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001919 /*
1920 * If resource errors reported from HBA, reduce
1921 * queuedepths of the SCSI device.
1922 */
1923 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1924 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1925 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarteaf15d52008-12-04 22:39:29 -05001926 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001927 spin_lock_irqsave(&phba->hbalock, iflag);
1928 }
1929
dea31012005-04-17 16:05:31 -05001930 /* Rsp ring <ringno> error: IOCB */
1931 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001932 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05001933 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001934 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05001935 irsp->un.ulpWord[0],
1936 irsp->un.ulpWord[1],
1937 irsp->un.ulpWord[2],
1938 irsp->un.ulpWord[3],
1939 irsp->un.ulpWord[4],
1940 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001941 *(uint32_t *)&irsp->un1,
1942 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05001943 }
1944
1945 switch (type) {
1946 case LPFC_ABORT_IOCB:
1947 case LPFC_SOL_IOCB:
1948 /*
1949 * Idle exchange closed via ABTS from port. No iocb
1950 * resources need to be recovered.
1951 */
1952 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001953 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001954 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04001955 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05001956 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04001957 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05001958 break;
1959 }
1960
James Bottomley604a3e32005-10-29 10:28:33 -05001961 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1962 &rspiocbq);
dea31012005-04-17 16:05:31 -05001963 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001964 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1965 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1966 &rspiocbq);
1967 } else {
James Smart2e0fef82007-06-17 19:56:36 -05001968 spin_unlock_irqrestore(&phba->hbalock,
1969 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001970 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1971 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001972 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001973 iflag);
1974 }
dea31012005-04-17 16:05:31 -05001975 }
1976 break;
James Smarta4bc3372006-12-02 13:34:16 -05001977 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05001978 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001979 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001980 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001981 break;
dea31012005-04-17 16:05:31 -05001982 default:
1983 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1984 char adaptermsg[LPFC_MAX_ADPTMSG];
1985 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1986 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1987 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001988 dev_warn(&((phba->pcidev)->dev),
1989 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05001990 phba->brd_no, adaptermsg);
1991 } else {
1992 /* Unknown IOCB command */
1993 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001994 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05001995 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001996 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05001997 irsp->ulpStatus,
1998 irsp->ulpIoTag,
1999 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002000 }
2001 break;
2002 }
2003
2004 /*
2005 * The response IOCB has been processed. Update the ring
2006 * pointer in SLIM. If the port response put pointer has not
2007 * been updated, sync the pgp->rspPutInx and fetch the new port
2008 * response put pointer.
2009 */
James Smarted957682007-06-17 19:56:37 -05002010 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002011
2012 if (pring->rspidx == portRspPut)
2013 portRspPut = le32_to_cpu(pgp->rspPutInx);
2014 }
2015
2016 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2017 pring->stats.iocb_rsp_full++;
2018 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2019 writel(status, phba->CAregaddr);
2020 readl(phba->CAregaddr);
2021 }
2022 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2023 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2024 pring->stats.iocb_cmd_empty++;
2025
2026 /* Force update of the local copy of cmdGetInx */
2027 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2028 lpfc_sli_resume_iocb(phba, pring);
2029
2030 if ((pring->lpfc_sli_cmd_available))
2031 (pring->lpfc_sli_cmd_available) (phba, pring);
2032
2033 }
2034
James Smart2e0fef82007-06-17 19:56:36 -05002035 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002036 return rc;
2037}
2038
James Smarte59058c2008-08-24 21:49:00 -04002039/**
2040 * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
2041 * @phba: Pointer to HBA context object.
2042 * @pring: Pointer to driver SLI ring object.
2043 * @mask: Host attention register mask for this ring.
2044 *
2045 * This function is called from the worker thread when there is a ring
2046 * event for non-fcp rings. The caller does not hold any lock .
2047 * The function processes each response iocb in the response ring until it
2048 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2049 * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
2050 * response iocb indicates a completion of a command iocb. The function
2051 * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
2052 * iocb. The function frees the resources or calls the completion handler if
2053 * this iocb is an abort completion. The function returns 0 when the allocated
2054 * iocbs are not freed, otherwise returns 1.
2055 **/
dea31012005-04-17 16:05:31 -05002056int
James Smart2e0fef82007-06-17 19:56:36 -05002057lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2058 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002059{
James Smart34b02dc2008-08-24 21:49:55 -04002060 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002061 IOCB_t *entry;
2062 IOCB_t *irsp = NULL;
2063 struct lpfc_iocbq *rspiocbp = NULL;
2064 struct lpfc_iocbq *next_iocb;
2065 struct lpfc_iocbq *cmdiocbp;
2066 struct lpfc_iocbq *saveq;
dea31012005-04-17 16:05:31 -05002067 uint8_t iocb_cmd_type;
2068 lpfc_iocb_type type;
2069 uint32_t status, free_saveq;
2070 uint32_t portRspPut, portRspMax;
2071 int rc = 1;
2072 unsigned long iflag;
dea31012005-04-17 16:05:31 -05002073
James Smart34b02dc2008-08-24 21:49:55 -04002074 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002075 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002076 pring->stats.iocb_event++;
2077
dea31012005-04-17 16:05:31 -05002078 /*
2079 * The next available response entry should never exceed the maximum
2080 * entries. If it does, treat it as an adapter hardware error.
2081 */
2082 portRspMax = pring->numRiocb;
2083 portRspPut = le32_to_cpu(pgp->rspPutInx);
2084 if (portRspPut >= portRspMax) {
2085 /*
2086 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
2087 * rsp ring <portRspMax>
2088 */
James Smarted957682007-06-17 19:56:37 -05002089 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002090 "0303 Ring %d handler: portRspPut %d "
dea31012005-04-17 16:05:31 -05002091 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002092 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002093
James Smart2e0fef82007-06-17 19:56:36 -05002094 phba->link_state = LPFC_HBA_ERROR;
2095 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002096
2097 phba->work_hs = HS_FFER3;
2098 lpfc_handle_eratt(phba);
2099
2100 return 1;
2101 }
2102
2103 rmb();
dea31012005-04-17 16:05:31 -05002104 while (pring->rspidx != portRspPut) {
2105 /*
2106 * Build a completion list and call the appropriate handler.
2107 * The process is to get the next available response iocb, get
2108 * a free iocb from the list, copy the response data into the
2109 * free iocb, insert to the continuation list, and update the
2110 * next response index to slim. This process makes response
2111 * iocb's in the ring available to DMA as fast as possible but
2112 * pays a penalty for a copy operation. Since the iocb is
2113 * only 32 bytes, this penalty is considered small relative to
2114 * the PCI reads for register values and a slim write. When
2115 * the ulpLe field is set, the entire Command has been
2116 * received.
2117 */
James Smarted957682007-06-17 19:56:37 -05002118 entry = lpfc_resp_iocb(phba, pring);
2119
James Smart858c9f62007-06-17 19:56:39 -05002120 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002121 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002122 if (rspiocbp == NULL) {
2123 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002124 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002125 break;
2126 }
2127
James Smarted957682007-06-17 19:56:37 -05002128 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2129 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002130 irsp = &rspiocbp->iocb;
2131
2132 if (++pring->rspidx >= portRspMax)
2133 pring->rspidx = 0;
2134
James Smarta58cbd52007-08-02 11:09:43 -04002135 if (pring->ringno == LPFC_ELS_RING) {
2136 lpfc_debugfs_slow_ring_trc(phba,
2137 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2138 *(((uint32_t *) irsp) + 4),
2139 *(((uint32_t *) irsp) + 6),
2140 *(((uint32_t *) irsp) + 7));
2141 }
2142
James Smarted957682007-06-17 19:56:37 -05002143 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002144
James Smart9c2face2008-01-11 01:53:18 -05002145 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
dea31012005-04-17 16:05:31 -05002146
2147 pring->iocb_continueq_cnt++;
2148 if (irsp->ulpLe) {
2149 /*
2150 * By default, the driver expects to free all resources
2151 * associated with this iocb completion.
2152 */
2153 free_saveq = 1;
2154 saveq = list_get_first(&pring->iocb_continueq,
2155 struct lpfc_iocbq, list);
2156 irsp = &(saveq->iocb);
2157 list_del_init(&pring->iocb_continueq);
2158 pring->iocb_continueq_cnt = 0;
2159
2160 pring->stats.iocb_rsp++;
2161
James Smart92d7f7b2007-06-17 19:56:38 -05002162 /*
2163 * If resource errors reported from HBA, reduce
2164 * queuedepths of the SCSI device.
2165 */
2166 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2167 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2168 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarteaf15d52008-12-04 22:39:29 -05002169 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002170 spin_lock_irqsave(&phba->hbalock, iflag);
2171 }
2172
dea31012005-04-17 16:05:31 -05002173 if (irsp->ulpStatus) {
2174 /* Rsp ring <ringno> error: IOCB */
James Smarted957682007-06-17 19:56:37 -05002175 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002176 "0328 Rsp Ring %d error: "
James Smarted957682007-06-17 19:56:37 -05002177 "IOCB Data: "
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 "
2181 "x%x x%x x%x x%x\n",
James Smarted957682007-06-17 19:56:37 -05002182 pring->ringno,
2183 irsp->un.ulpWord[0],
2184 irsp->un.ulpWord[1],
2185 irsp->un.ulpWord[2],
2186 irsp->un.ulpWord[3],
2187 irsp->un.ulpWord[4],
2188 irsp->un.ulpWord[5],
2189 *(((uint32_t *) irsp) + 6),
2190 *(((uint32_t *) irsp) + 7),
2191 *(((uint32_t *) irsp) + 8),
2192 *(((uint32_t *) irsp) + 9),
2193 *(((uint32_t *) irsp) + 10),
2194 *(((uint32_t *) irsp) + 11),
2195 *(((uint32_t *) irsp) + 12),
2196 *(((uint32_t *) irsp) + 13),
2197 *(((uint32_t *) irsp) + 14),
2198 *(((uint32_t *) irsp) + 15));
dea31012005-04-17 16:05:31 -05002199 }
2200
2201 /*
2202 * Fetch the IOCB command type and call the correct
2203 * completion routine. Solicited and Unsolicited
2204 * IOCBs on the ELS ring get freed back to the
2205 * lpfc_iocb_list by the discovery kernel thread.
2206 */
2207 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2208 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2209 if (type == LPFC_SOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002210 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002211 rc = lpfc_sli_process_sol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002212 saveq);
2213 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002214 } else if (type == LPFC_UNSOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002215 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002216 rc = lpfc_sli_process_unsol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002217 saveq);
2218 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9c2face2008-01-11 01:53:18 -05002219 if (!rc)
2220 free_saveq = 0;
dea31012005-04-17 16:05:31 -05002221 } else if (type == LPFC_ABORT_IOCB) {
2222 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
2223 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05002224 lpfc_sli_iocbq_lookup(phba, pring,
2225 saveq)))) {
dea31012005-04-17 16:05:31 -05002226 /* Call the specified completion
2227 routine */
2228 if (cmdiocbp->iocb_cmpl) {
2229 spin_unlock_irqrestore(
James Smart2e0fef82007-06-17 19:56:36 -05002230 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002231 iflag);
2232 (cmdiocbp->iocb_cmpl) (phba,
2233 cmdiocbp, saveq);
2234 spin_lock_irqsave(
James Smart2e0fef82007-06-17 19:56:36 -05002235 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002236 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002237 } else
James Smart2e0fef82007-06-17 19:56:36 -05002238 __lpfc_sli_release_iocbq(phba,
James Bottomley604a3e32005-10-29 10:28:33 -05002239 cmdiocbp);
dea31012005-04-17 16:05:31 -05002240 }
2241 } else if (type == LPFC_UNKNOWN_IOCB) {
2242 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2243
2244 char adaptermsg[LPFC_MAX_ADPTMSG];
2245
2246 memset(adaptermsg, 0,
2247 LPFC_MAX_ADPTMSG);
2248 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2249 MAX_MSG_DATA);
2250 dev_warn(&((phba->pcidev)->dev),
Joe Perches898eb712007-10-18 03:06:30 -07002251 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002252 phba->brd_no, adaptermsg);
2253 } else {
2254 /* Unknown IOCB command */
James Smart92d7f7b2007-06-17 19:56:38 -05002255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002256 "0335 Unknown IOCB "
James Smart92d7f7b2007-06-17 19:56:38 -05002257 "command Data: x%x "
2258 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002259 irsp->ulpCommand,
2260 irsp->ulpStatus,
2261 irsp->ulpIoTag,
2262 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002263 }
2264 }
2265
2266 if (free_saveq) {
James Smart2e0fef82007-06-17 19:56:36 -05002267 list_for_each_entry_safe(rspiocbp, next_iocb,
2268 &saveq->list, list) {
2269 list_del(&rspiocbp->list);
2270 __lpfc_sli_release_iocbq(phba,
2271 rspiocbp);
dea31012005-04-17 16:05:31 -05002272 }
James Smart2e0fef82007-06-17 19:56:36 -05002273 __lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05002274 }
James Smart92d7f7b2007-06-17 19:56:38 -05002275 rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002276 }
2277
2278 /*
2279 * If the port response put pointer has not been updated, sync
2280 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2281 * response put pointer.
2282 */
2283 if (pring->rspidx == portRspPut) {
2284 portRspPut = le32_to_cpu(pgp->rspPutInx);
2285 }
2286 } /* while (pring->rspidx != portRspPut) */
2287
James Smart92d7f7b2007-06-17 19:56:38 -05002288 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002289 /* At least one response entry has been freed */
2290 pring->stats.iocb_rsp_full++;
2291 /* SET RxRE_RSP in Chip Att register */
2292 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2293 writel(status, phba->CAregaddr);
2294 readl(phba->CAregaddr); /* flush */
2295 }
2296 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2297 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2298 pring->stats.iocb_cmd_empty++;
2299
2300 /* Force update of the local copy of cmdGetInx */
2301 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2302 lpfc_sli_resume_iocb(phba, pring);
2303
2304 if ((pring->lpfc_sli_cmd_available))
2305 (pring->lpfc_sli_cmd_available) (phba, pring);
2306
2307 }
2308
James Smart2e0fef82007-06-17 19:56:36 -05002309 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002310 return rc;
2311}
2312
James Smarte59058c2008-08-24 21:49:00 -04002313/**
2314 * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
2315 * @phba: Pointer to HBA context object.
2316 * @pring: Pointer to driver SLI ring object.
2317 *
2318 * This function aborts all iocbs in the given ring and frees all the iocb
2319 * objects in txq. This function issues an abort iocb for all the iocb commands
2320 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2321 * the return of this function. The caller is not required to hold any locks.
2322 **/
James Smart2e0fef82007-06-17 19:56:36 -05002323void
dea31012005-04-17 16:05:31 -05002324lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2325{
James Smart2534ba72007-04-25 09:52:20 -04002326 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002327 struct lpfc_iocbq *iocb, *next_iocb;
James Smart2534ba72007-04-25 09:52:20 -04002328 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05002329
James Smart92d7f7b2007-06-17 19:56:38 -05002330 if (pring->ringno == LPFC_ELS_RING) {
2331 lpfc_fabric_abort_hba(phba);
2332 }
2333
dea31012005-04-17 16:05:31 -05002334 /* Error everything on txq and txcmplq
2335 * First do the txq.
2336 */
James Smart2e0fef82007-06-17 19:56:36 -05002337 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002338 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002339 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002340
2341 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002342 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2343 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2344
James Smart2e0fef82007-06-17 19:56:36 -05002345 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002346
2347 while (!list_empty(&completions)) {
2348 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
dea31012005-04-17 16:05:31 -05002349 cmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05002350 list_del_init(&iocb->list);
dea31012005-04-17 16:05:31 -05002351
James Smart2e0fef82007-06-17 19:56:36 -05002352 if (!iocb->iocb_cmpl)
2353 lpfc_sli_release_iocbq(phba, iocb);
2354 else {
dea31012005-04-17 16:05:31 -05002355 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2356 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
dea31012005-04-17 16:05:31 -05002357 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002358 }
dea31012005-04-17 16:05:31 -05002359 }
dea31012005-04-17 16:05:31 -05002360}
2361
James Smarte59058c2008-08-24 21:49:00 -04002362/**
James Smarta8e497d2008-08-24 21:50:11 -04002363 * lpfc_sli_flush_fcp_rings: flush all iocbs in the fcp ring.
2364 * @phba: Pointer to HBA context object.
2365 *
2366 * This function flushes all iocbs in the fcp ring and frees all the iocb
2367 * objects in txq and txcmplq. This function will not issue abort iocbs
2368 * for all the iocb commands in txcmplq, they will just be returned with
2369 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2370 * slot has been permanently disabled.
2371 **/
2372void
2373lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2374{
2375 LIST_HEAD(txq);
2376 LIST_HEAD(txcmplq);
2377 struct lpfc_iocbq *iocb;
2378 IOCB_t *cmd = NULL;
2379 struct lpfc_sli *psli = &phba->sli;
2380 struct lpfc_sli_ring *pring;
2381
2382 /* Currently, only one fcp ring */
2383 pring = &psli->ring[psli->fcp_ring];
2384
2385 spin_lock_irq(&phba->hbalock);
2386 /* Retrieve everything on txq */
2387 list_splice_init(&pring->txq, &txq);
2388 pring->txq_cnt = 0;
2389
2390 /* Retrieve everything on the txcmplq */
2391 list_splice_init(&pring->txcmplq, &txcmplq);
2392 pring->txcmplq_cnt = 0;
2393 spin_unlock_irq(&phba->hbalock);
2394
2395 /* Flush the txq */
2396 while (!list_empty(&txq)) {
2397 iocb = list_get_first(&txq, struct lpfc_iocbq, list);
2398 cmd = &iocb->iocb;
2399 list_del_init(&iocb->list);
2400
2401 if (!iocb->iocb_cmpl)
2402 lpfc_sli_release_iocbq(phba, iocb);
2403 else {
2404 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2405 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2406 (iocb->iocb_cmpl) (phba, iocb, iocb);
2407 }
2408 }
2409
2410 /* Flush the txcmpq */
2411 while (!list_empty(&txcmplq)) {
2412 iocb = list_get_first(&txcmplq, struct lpfc_iocbq, list);
2413 cmd = &iocb->iocb;
2414 list_del_init(&iocb->list);
2415
2416 if (!iocb->iocb_cmpl)
2417 lpfc_sli_release_iocbq(phba, iocb);
2418 else {
2419 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2420 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2421 (iocb->iocb_cmpl) (phba, iocb, iocb);
2422 }
2423 }
2424}
2425
2426/**
James Smarte59058c2008-08-24 21:49:00 -04002427 * lpfc_sli_brdready: Check for host status bits.
2428 * @phba: Pointer to HBA context object.
2429 * @mask: Bit mask to be checked.
2430 *
2431 * This function reads the host status register and compares
2432 * with the provided bit mask to check if HBA completed
2433 * the restart. This function will wait in a loop for the
2434 * HBA to complete restart. If the HBA does not restart within
2435 * 15 iterations, the function will reset the HBA again. The
2436 * function returns 1 when HBA fail to restart otherwise returns
2437 * zero.
2438 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002439int
James Smart2e0fef82007-06-17 19:56:36 -05002440lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05002441{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002442 uint32_t status;
2443 int i = 0;
2444 int retval = 0;
dea31012005-04-17 16:05:31 -05002445
Jamie Wellnitz41415862006-02-28 19:25:27 -05002446 /* Read the HBA Host Status Register */
2447 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002448
Jamie Wellnitz41415862006-02-28 19:25:27 -05002449 /*
2450 * Check status register every 100ms for 5 retries, then every
2451 * 500ms for 5, then every 2.5 sec for 5, then reset board and
2452 * every 2.5 sec for 4.
2453 * Break our of the loop if errors occurred during init.
2454 */
2455 while (((status & mask) != mask) &&
2456 !(status & HS_FFERM) &&
2457 i++ < 20) {
dea31012005-04-17 16:05:31 -05002458
Jamie Wellnitz41415862006-02-28 19:25:27 -05002459 if (i <= 5)
2460 msleep(10);
2461 else if (i <= 10)
2462 msleep(500);
2463 else
2464 msleep(2500);
dea31012005-04-17 16:05:31 -05002465
Jamie Wellnitz41415862006-02-28 19:25:27 -05002466 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002467 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002468 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002469 lpfc_sli_brdrestart(phba);
2470 }
2471 /* Read the HBA Host Status Register */
2472 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002473 }
dea31012005-04-17 16:05:31 -05002474
Jamie Wellnitz41415862006-02-28 19:25:27 -05002475 /* Check to see if any errors occurred during init */
2476 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05002477 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002478 retval = 1;
2479 }
dea31012005-04-17 16:05:31 -05002480
Jamie Wellnitz41415862006-02-28 19:25:27 -05002481 return retval;
dea31012005-04-17 16:05:31 -05002482}
2483
James Smart92908312006-03-07 15:04:13 -05002484#define BARRIER_TEST_PATTERN (0xdeadbeef)
2485
James Smarte59058c2008-08-24 21:49:00 -04002486/**
2487 * lpfc_reset_barrier: Make HBA ready for HBA reset.
2488 * @phba: Pointer to HBA context object.
2489 *
2490 * This function is called before resetting an HBA. This
2491 * function requests HBA to quiesce DMAs before a reset.
2492 **/
James Smart2e0fef82007-06-17 19:56:36 -05002493void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05002494{
James Smart65a29c12006-07-06 15:50:50 -04002495 uint32_t __iomem *resp_buf;
2496 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05002497 volatile uint32_t mbox;
2498 uint32_t hc_copy;
2499 int i;
2500 uint8_t hdrtype;
2501
2502 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2503 if (hdrtype != 0x80 ||
2504 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2505 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2506 return;
2507
2508 /*
2509 * Tell the other part of the chip to suspend temporarily all
2510 * its DMA activity.
2511 */
James Smart65a29c12006-07-06 15:50:50 -04002512 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002513
2514 /* Disable the error attention */
2515 hc_copy = readl(phba->HCregaddr);
2516 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2517 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002518 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002519
2520 if (readl(phba->HAregaddr) & HA_ERATT) {
2521 /* Clear Chip error bit */
2522 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002523 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002524 }
2525
2526 mbox = 0;
2527 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2528 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2529
2530 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04002531 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002532 writel(mbox, mbox_buf);
2533
2534 for (i = 0;
2535 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2536 mdelay(1);
2537
2538 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2539 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05002540 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05002541 goto restore_hc;
2542 else
2543 goto clear_errat;
2544 }
2545
2546 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2547 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
2548 mdelay(1);
2549
2550clear_errat:
2551
2552 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2553 mdelay(1);
2554
2555 if (readl(phba->HAregaddr) & HA_ERATT) {
2556 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002557 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002558 }
2559
2560restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05002561 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002562 writel(hc_copy, phba->HCregaddr);
2563 readl(phba->HCregaddr); /* flush */
2564}
2565
James Smarte59058c2008-08-24 21:49:00 -04002566/**
2567 * lpfc_sli_brdkill: Issue a kill_board mailbox command.
2568 * @phba: Pointer to HBA context object.
2569 *
2570 * This function issues a kill_board mailbox command and waits for
2571 * the error attention interrupt. This function is called for stopping
2572 * the firmware processing. The caller is not required to hold any
2573 * locks. This function calls lpfc_hba_down_post function to free
2574 * any pending commands after the kill. The function will return 1 when it
2575 * fails to kill the board else will return 0.
2576 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002577int
James Smart2e0fef82007-06-17 19:56:36 -05002578lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002579{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002580 struct lpfc_sli *psli;
2581 LPFC_MBOXQ_t *pmb;
2582 uint32_t status;
2583 uint32_t ha_copy;
2584 int retval;
2585 int i = 0;
2586
2587 psli = &phba->sli;
2588
2589 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05002590 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002591 "0329 Kill HBA Data: x%x x%x\n",
2592 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002593
James Smart98c9ea52007-10-27 13:37:33 -04002594 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2595 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002596 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002597
2598 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05002599 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002600 status = readl(phba->HCregaddr);
2601 status &= ~HC_ERINT_ENA;
2602 writel(status, phba->HCregaddr);
2603 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002604 phba->link_flag |= LS_IGNORE_ERATT;
2605 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002606
2607 lpfc_kill_board(phba, pmb);
2608 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2609 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2610
2611 if (retval != MBX_SUCCESS) {
2612 if (retval != MBX_BUSY)
2613 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002614 spin_lock_irq(&phba->hbalock);
2615 phba->link_flag &= ~LS_IGNORE_ERATT;
2616 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002617 return 1;
2618 }
2619
James Smart92908312006-03-07 15:04:13 -05002620 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2621
Jamie Wellnitz41415862006-02-28 19:25:27 -05002622 mempool_free(pmb, phba->mbox_mem_pool);
2623
2624 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2625 * attention every 100ms for 3 seconds. If we don't get ERATT after
2626 * 3 seconds we still set HBA_ERROR state because the status of the
2627 * board is now undefined.
2628 */
2629 ha_copy = readl(phba->HAregaddr);
2630
2631 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2632 mdelay(100);
2633 ha_copy = readl(phba->HAregaddr);
2634 }
2635
2636 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05002637 if (ha_copy & HA_ERATT) {
2638 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002639 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002640 }
James Smart2e0fef82007-06-17 19:56:36 -05002641 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002642 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002643 phba->link_flag &= ~LS_IGNORE_ERATT;
2644 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002645
2646 psli->mbox_active = NULL;
2647 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002648 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002649
James Smart2e0fef82007-06-17 19:56:36 -05002650 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002651}
2652
James Smarte59058c2008-08-24 21:49:00 -04002653/**
2654 * lpfc_sli_brdreset: Reset the HBA.
2655 * @phba: Pointer to HBA context object.
2656 *
2657 * This function resets the HBA by writing HC_INITFF to the control
2658 * register. After the HBA resets, this function resets all the iocb ring
2659 * indices. This function disables PCI layer parity checking during
2660 * the reset.
2661 * This function returns 0 always.
2662 * The caller is not required to hold any locks.
2663 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002664int
James Smart2e0fef82007-06-17 19:56:36 -05002665lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002666{
2667 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002668 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002669 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05002670 int i;
dea31012005-04-17 16:05:31 -05002671
Jamie Wellnitz41415862006-02-28 19:25:27 -05002672 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002673
Jamie Wellnitz41415862006-02-28 19:25:27 -05002674 /* Reset HBA */
2675 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002676 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002677 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05002678
2679 /* perform board reset */
2680 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002681 phba->pport->fc_myDID = 0;
2682 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002683
Jamie Wellnitz41415862006-02-28 19:25:27 -05002684 /* Turn off parity checking and serr during the physical reset */
2685 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2686 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2687 (cfg_value &
2688 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2689
James Smart1c067a42006-08-01 07:33:52 -04002690 psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002691 /* Now toggle INITFF bit in the Host Control Register */
2692 writel(HC_INITFF, phba->HCregaddr);
2693 mdelay(1);
2694 readl(phba->HCregaddr); /* flush */
2695 writel(0, phba->HCregaddr);
2696 readl(phba->HCregaddr); /* flush */
2697
2698 /* Restore PCI cmd register */
2699 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002700
2701 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002702 for (i = 0; i < psli->num_rings; i++) {
2703 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002704 pring->flag = 0;
2705 pring->rspidx = 0;
2706 pring->next_cmdidx = 0;
2707 pring->local_getidx = 0;
2708 pring->cmdidx = 0;
2709 pring->missbufcnt = 0;
2710 }
dea31012005-04-17 16:05:31 -05002711
James Smart2e0fef82007-06-17 19:56:36 -05002712 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002713 return 0;
2714}
2715
James Smarte59058c2008-08-24 21:49:00 -04002716/**
2717 * lpfc_sli_brdrestart: Restart the HBA.
2718 * @phba: Pointer to HBA context object.
2719 *
2720 * This function is called in the SLI initialization code path to
2721 * restart the HBA. The caller is not required to hold any lock.
2722 * This function writes MBX_RESTART mailbox command to the SLIM and
2723 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
2724 * function to free any pending commands. The function enables
2725 * POST only during the first initialization. The function returns zero.
2726 * The function does not guarantee completion of MBX_RESTART mailbox
2727 * command before the return of this function.
2728 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002729int
James Smart2e0fef82007-06-17 19:56:36 -05002730lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002731{
2732 MAILBOX_t *mb;
2733 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002734 volatile uint32_t word0;
2735 void __iomem *to_slim;
2736
James Smart2e0fef82007-06-17 19:56:36 -05002737 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002738
2739 psli = &phba->sli;
2740
2741 /* Restart HBA */
2742 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002743 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002744 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002745
2746 word0 = 0;
2747 mb = (MAILBOX_t *) &word0;
2748 mb->mbxCommand = MBX_RESTART;
2749 mb->mbxHc = 1;
2750
James Smart92908312006-03-07 15:04:13 -05002751 lpfc_reset_barrier(phba);
2752
Jamie Wellnitz41415862006-02-28 19:25:27 -05002753 to_slim = phba->MBslimaddr;
2754 writel(*(uint32_t *) mb, to_slim);
2755 readl(to_slim); /* flush */
2756
2757 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05002758 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002759 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05002760 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05002761 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04002762 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002763 writel(*(uint32_t *) mb, to_slim);
2764 readl(to_slim); /* flush */
2765
2766 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002767 phba->pport->stopped = 0;
2768 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002769
James Smart2e0fef82007-06-17 19:56:36 -05002770 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002771
James Smart64ba8812006-08-02 15:24:34 -04002772 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2773 psli->stats_start = get_seconds();
2774
James Smarteaf15d52008-12-04 22:39:29 -05002775 /* Give the INITFF and Post time to settle. */
2776 mdelay(100);
dea31012005-04-17 16:05:31 -05002777
Jamie Wellnitz41415862006-02-28 19:25:27 -05002778 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002779
2780 return 0;
2781}
2782
James Smarte59058c2008-08-24 21:49:00 -04002783/**
2784 * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
2785 * @phba: Pointer to HBA context object.
2786 *
2787 * This function is called after a HBA restart to wait for successful
2788 * restart of the HBA. Successful restart of the HBA is indicated by
2789 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
2790 * iteration, the function will restart the HBA again. The function returns
2791 * zero if HBA successfully restarted else returns negative error code.
2792 **/
dea31012005-04-17 16:05:31 -05002793static int
2794lpfc_sli_chipset_init(struct lpfc_hba *phba)
2795{
2796 uint32_t status, i = 0;
2797
2798 /* Read the HBA Host Status Register */
2799 status = readl(phba->HSregaddr);
2800
2801 /* Check status register to see what current state is */
2802 i = 0;
2803 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2804
2805 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2806 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2807 * 4.
2808 */
2809 if (i++ >= 20) {
2810 /* Adapter failed to init, timeout, status reg
2811 <status> */
James Smarted957682007-06-17 19:56:37 -05002812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002813 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002814 "timeout, status reg x%x, "
2815 "FW Data: A8 x%x AC x%x\n", status,
2816 readl(phba->MBslimaddr + 0xa8),
2817 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002818 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002819 return -ETIMEDOUT;
2820 }
2821
2822 /* Check to see if any errors occurred during init */
2823 if (status & HS_FFERM) {
2824 /* ERROR: During chipset initialization */
2825 /* Adapter failed to init, chipset, status reg
2826 <status> */
James Smarted957682007-06-17 19:56:37 -05002827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002828 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002829 "chipset, status reg x%x, "
2830 "FW Data: A8 x%x AC x%x\n", status,
2831 readl(phba->MBslimaddr + 0xa8),
2832 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002833 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002834 return -EIO;
2835 }
2836
2837 if (i <= 5) {
2838 msleep(10);
2839 } else if (i <= 10) {
2840 msleep(500);
2841 } else {
2842 msleep(2500);
2843 }
2844
2845 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002846 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002847 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002848 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002849 }
2850 /* Read the HBA Host Status Register */
2851 status = readl(phba->HSregaddr);
2852 }
2853
2854 /* Check to see if any errors occurred during init */
2855 if (status & HS_FFERM) {
2856 /* ERROR: During chipset initialization */
2857 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05002858 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002859 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05002860 "status reg x%x, "
2861 "FW Data: A8 x%x AC x%x\n", status,
2862 readl(phba->MBslimaddr + 0xa8),
2863 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002864 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002865 return -EIO;
2866 }
2867
2868 /* Clear all interrupt enable conditions */
2869 writel(0, phba->HCregaddr);
2870 readl(phba->HCregaddr); /* flush */
2871
2872 /* setup host attn register */
2873 writel(0xffffffff, phba->HAregaddr);
2874 readl(phba->HAregaddr); /* flush */
2875 return 0;
2876}
2877
James Smarte59058c2008-08-24 21:49:00 -04002878/**
2879 * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
2880 *
2881 * This function calculates and returns the number of HBQs required to be
2882 * configured.
2883 **/
James Smart78b2d852007-08-02 11:10:21 -04002884int
James Smarted957682007-06-17 19:56:37 -05002885lpfc_sli_hbq_count(void)
2886{
James Smart92d7f7b2007-06-17 19:56:38 -05002887 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05002888}
2889
James Smarte59058c2008-08-24 21:49:00 -04002890/**
2891 * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
2892 *
2893 * This function adds the number of hbq entries in every HBQ to get
2894 * the total number of hbq entries required for the HBA and returns
2895 * the total count.
2896 **/
James Smarted957682007-06-17 19:56:37 -05002897static int
2898lpfc_sli_hbq_entry_count(void)
2899{
2900 int hbq_count = lpfc_sli_hbq_count();
2901 int count = 0;
2902 int i;
2903
2904 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05002905 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05002906 return count;
2907}
2908
James Smarte59058c2008-08-24 21:49:00 -04002909/**
2910 * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
2911 *
2912 * This function calculates amount of memory required for all hbq entries
2913 * to be configured and returns the total memory required.
2914 **/
dea31012005-04-17 16:05:31 -05002915int
James Smarted957682007-06-17 19:56:37 -05002916lpfc_sli_hbq_size(void)
2917{
2918 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2919}
2920
James Smarte59058c2008-08-24 21:49:00 -04002921/**
2922 * lpfc_sli_hbq_setup: configure and initialize HBQs.
2923 * @phba: Pointer to HBA context object.
2924 *
2925 * This function is called during the SLI initialization to configure
2926 * all the HBQs and post buffers to the HBQ. The caller is not
2927 * required to hold any locks. This function will return zero if successful
2928 * else it will return negative error code.
2929 **/
James Smarted957682007-06-17 19:56:37 -05002930static int
2931lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2932{
2933 int hbq_count = lpfc_sli_hbq_count();
2934 LPFC_MBOXQ_t *pmb;
2935 MAILBOX_t *pmbox;
2936 uint32_t hbqno;
2937 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05002938
James Smart92d7f7b2007-06-17 19:56:38 -05002939 /* Get a Mailbox buffer to setup mailbox
2940 * commands for HBA initialization
2941 */
James Smarted957682007-06-17 19:56:37 -05002942 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2943
2944 if (!pmb)
2945 return -ENOMEM;
2946
2947 pmbox = &pmb->mb;
2948
2949 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2950 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05002951 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05002952
2953 hbq_entry_index = 0;
2954 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2955 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2956 phba->hbqs[hbqno].hbqPutIdx = 0;
2957 phba->hbqs[hbqno].local_hbqGetIdx = 0;
2958 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05002959 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04002960 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2961 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05002962 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2963
2964 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2965 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2966 mbxStatus <status>, ring <num> */
2967
2968 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002969 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002970 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05002971 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002972 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05002973 pmbox->mbxStatus, hbqno);
2974
2975 phba->link_state = LPFC_HBA_ERROR;
2976 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05002977 return ENXIO;
2978 }
2979 }
2980 phba->hbq_count = hbq_count;
2981
James Smarted957682007-06-17 19:56:37 -05002982 mempool_free(pmb, phba->mbox_mem_pool);
2983
James Smart92d7f7b2007-06-17 19:56:38 -05002984 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04002985 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
2986 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05002987 return 0;
2988}
2989
James Smarte59058c2008-08-24 21:49:00 -04002990/**
James Smart93996272008-08-24 21:50:30 -04002991 * lpfc_sli_config_port: Issue config port mailbox command.
James Smarte59058c2008-08-24 21:49:00 -04002992 * @phba: Pointer to HBA context object.
2993 * @sli_mode: sli mode - 2/3
2994 *
2995 * This function is called by the sli intialization code path
2996 * to issue config_port mailbox command. This function restarts the
2997 * HBA firmware and issues a config_port mailbox command to configure
2998 * the SLI interface in the sli mode specified by sli_mode
2999 * variable. The caller is not required to hold any locks.
3000 * The function returns 0 if successful, else returns negative error
3001 * code.
3002 **/
James Smart93996272008-08-24 21:50:30 -04003003int
3004lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003005{
3006 LPFC_MBOXQ_t *pmb;
3007 uint32_t resetcount = 0, rc = 0, done = 0;
3008
3009 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3010 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003011 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003012 return -ENOMEM;
3013 }
3014
James Smarted957682007-06-17 19:56:37 -05003015 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003016 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003017 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003018 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003019 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003020 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003021 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003022 rc = lpfc_sli_chipset_init(phba);
3023 if (rc)
3024 break;
3025
James Smart2e0fef82007-06-17 19:56:36 -05003026 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003027 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003028 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003029 resetcount++;
3030
James Smarted957682007-06-17 19:56:37 -05003031 /* Call pre CONFIG_PORT mailbox command initialization. A
3032 * value of 0 means the call was successful. Any other
3033 * nonzero value is a failure, but if ERESTART is returned,
3034 * the driver may reset the HBA and try again.
3035 */
dea31012005-04-17 16:05:31 -05003036 rc = lpfc_config_port_prep(phba);
3037 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003038 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003039 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003040 } else if (rc)
dea31012005-04-17 16:05:31 -05003041 break;
James Smart2e0fef82007-06-17 19:56:36 -05003042 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003043 lpfc_config_port(phba, pmb);
3044 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003045 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3046 LPFC_SLI3_HBQ_ENABLED |
3047 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003048 LPFC_SLI3_INB_ENABLED |
3049 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003050 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003052 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003053 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003054 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003055 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003056 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003057 spin_unlock_irq(&phba->hbalock);
3058 rc = -ENXIO;
James Smart34b02dc2008-08-24 21:49:55 -04003059 } else
James Smarted957682007-06-17 19:56:37 -05003060 done = 1;
dea31012005-04-17 16:05:31 -05003061 }
James Smarted957682007-06-17 19:56:37 -05003062 if (!done) {
3063 rc = -EINVAL;
3064 goto do_prep_failed;
3065 }
James Smart34b02dc2008-08-24 21:49:55 -04003066 if (pmb->mb.un.varCfgPort.sli_mode == 3) {
3067 if (!pmb->mb.un.varCfgPort.cMA) {
3068 rc = -ENXIO;
3069 goto do_prep_failed;
3070 }
3071 if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
3072 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3073 phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
3074 } else
3075 phba->max_vpi = 0;
3076 if (pmb->mb.un.varCfgPort.gerbm)
3077 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3078 if (pmb->mb.un.varCfgPort.gcrp)
3079 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3080 if (pmb->mb.un.varCfgPort.ginb) {
3081 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003082 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003083 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3084 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3085 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3086 phba->inb_last_counter =
3087 phba->mbox->us.s3_inb_pgp.counter;
3088 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003089 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003090 phba->port_gp = phba->mbox->us.s3_pgp.port;
3091 phba->inb_ha_copy = NULL;
3092 phba->inb_counter = NULL;
3093 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003094
3095 if (phba->cfg_enable_bg) {
3096 if (pmb->mb.un.varCfgPort.gbg)
3097 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3098 else
3099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3100 "0443 Adapter did not grant "
3101 "BlockGuard\n");
3102 }
James Smart34b02dc2008-08-24 21:49:55 -04003103 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003104 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003105 phba->port_gp = phba->mbox->us.s2.port;
3106 phba->inb_ha_copy = NULL;
3107 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003108 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003109 }
James Smart92d7f7b2007-06-17 19:56:38 -05003110do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003111 mempool_free(pmb, phba->mbox_mem_pool);
3112 return rc;
3113}
3114
James Smarte59058c2008-08-24 21:49:00 -04003115
3116/**
3117 * lpfc_sli_hba_setup: SLI intialization function.
3118 * @phba: Pointer to HBA context object.
3119 *
3120 * This function is the main SLI intialization function. This function
3121 * is called by the HBA intialization code, HBA reset code and HBA
3122 * error attention handler code. Caller is not required to hold any
3123 * locks. This function issues config_port mailbox command to configure
3124 * the SLI, setup iocb rings and HBQ rings. In the end the function
3125 * calls the config_port_post function to issue init_link mailbox
3126 * command and to start the discovery. The function will return zero
3127 * if successful, else it will return negative error code.
3128 **/
James Smarted957682007-06-17 19:56:37 -05003129int
3130lpfc_sli_hba_setup(struct lpfc_hba *phba)
3131{
3132 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003133 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003134
3135 switch (lpfc_sli_mode) {
3136 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003137 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003138 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003139 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003140 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003141 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003142 break;
3143 }
James Smarted957682007-06-17 19:56:37 -05003144 mode = 2;
3145 break;
3146 case 0:
3147 case 3:
3148 break;
3149 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003151 "1819 Unrecognized lpfc_sli_mode "
3152 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003153
3154 break;
3155 }
3156
James Smart93996272008-08-24 21:50:30 -04003157 rc = lpfc_sli_config_port(phba, mode);
3158
James Smarted957682007-06-17 19:56:37 -05003159 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003160 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003161 "1820 Unable to select SLI-3. "
3162 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003163 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04003164 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05003165 if (rc)
dea31012005-04-17 16:05:31 -05003166 goto lpfc_sli_hba_setup_error;
3167
James Smarted957682007-06-17 19:56:37 -05003168 if (phba->sli_rev == 3) {
3169 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3170 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05003171 } else {
3172 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3173 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003174 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003175 }
3176
3177 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003178 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3179 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05003180 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05003181
3182 if (rc)
3183 goto lpfc_sli_hba_setup_error;
3184
James Smart93996272008-08-24 21:50:30 -04003185 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05003186 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3187 rc = lpfc_sli_hbq_setup(phba);
3188 if (rc)
3189 goto lpfc_sli_hba_setup_error;
3190 }
3191
dea31012005-04-17 16:05:31 -05003192 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3193
3194 rc = lpfc_config_port_post(phba);
3195 if (rc)
3196 goto lpfc_sli_hba_setup_error;
3197
James Smarted957682007-06-17 19:56:37 -05003198 return rc;
3199
James Smart92d7f7b2007-06-17 19:56:38 -05003200lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05003201 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05003202 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003203 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05003204 return rc;
3205}
3206
James Smarte59058c2008-08-24 21:49:00 -04003207
3208/**
3209 * lpfc_mbox_timeout: Timeout call back function for mbox timer.
3210 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05003211 *
James Smarte59058c2008-08-24 21:49:00 -04003212 * This is the callback function for mailbox timer. The mailbox
3213 * timer is armed when a new mailbox command is issued and the timer
3214 * is deleted when the mailbox complete. The function is called by
3215 * the kernel timer code when a mailbox does not complete within
3216 * expected time. This function wakes up the worker thread to
3217 * process the mailbox timeout and returns. All the processing is
3218 * done by the worker thread function lpfc_mbox_timeout_handler.
3219 **/
dea31012005-04-17 16:05:31 -05003220void
3221lpfc_mbox_timeout(unsigned long ptr)
3222{
James Smart92d7f7b2007-06-17 19:56:38 -05003223 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05003224 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05003225 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003226
James Smart2e0fef82007-06-17 19:56:36 -05003227 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05003228 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003229 if (!tmo_posted)
3230 phba->pport->work_port_events |= WORKER_MBOX_TMO;
3231 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
3232
James Smart5e9d9b82008-06-14 22:52:53 -04003233 if (!tmo_posted)
3234 lpfc_worker_wake_up(phba);
3235 return;
dea31012005-04-17 16:05:31 -05003236}
3237
James Smarte59058c2008-08-24 21:49:00 -04003238
3239/**
3240 * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
3241 * @phba: Pointer to HBA context object.
3242 *
3243 * This function is called from worker thread when a mailbox command times out.
3244 * The caller is not required to hold any locks. This function will reset the
3245 * HBA and recover all the pending commands.
3246 **/
dea31012005-04-17 16:05:31 -05003247void
3248lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
3249{
James Smart2e0fef82007-06-17 19:56:36 -05003250 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
3251 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04003252 struct lpfc_sli *psli = &phba->sli;
3253 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003254
dea31012005-04-17 16:05:31 -05003255 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05003256 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003257 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003258 mb->mbxCommand,
3259 phba->pport->port_state,
3260 phba->sli.sli_flag,
3261 phba->sli.mbox_active);
dea31012005-04-17 16:05:31 -05003262
James Smart1dcb58e2007-04-25 09:51:30 -04003263 /* Setting state unknown so lpfc_sli_abort_iocb_ring
3264 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
3265 * it to fail all oustanding SCSI IO.
3266 */
James Smart2e0fef82007-06-17 19:56:36 -05003267 spin_lock_irq(&phba->pport->work_port_lock);
3268 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3269 spin_unlock_irq(&phba->pport->work_port_lock);
3270 spin_lock_irq(&phba->hbalock);
3271 phba->link_state = LPFC_LINK_UNKNOWN;
James Smart1dcb58e2007-04-25 09:51:30 -04003272 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003273 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04003274
3275 pring = &psli->ring[psli->fcp_ring];
3276 lpfc_sli_abort_iocb_ring(phba, pring);
3277
3278 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003279 "0345 Resetting board due to mailbox timeout\n");
James Smart1dcb58e2007-04-25 09:51:30 -04003280 /*
3281 * lpfc_offline calls lpfc_sli_hba_down which will clean up
3282 * on oustanding mailbox commands.
3283 */
James Smart13815c82008-01-11 01:52:48 -05003284 /* If resets are disabled then set error state and return. */
3285 if (!phba->cfg_enable_hba_reset) {
3286 phba->link_state = LPFC_HBA_ERROR;
3287 return;
3288 }
James Smart1dcb58e2007-04-25 09:51:30 -04003289 lpfc_offline_prep(phba);
3290 lpfc_offline(phba);
3291 lpfc_sli_brdrestart(phba);
James Smart58da1ff2008-04-07 10:15:56 -04003292 lpfc_online(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04003293 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003294 return;
3295}
3296
James Smarte59058c2008-08-24 21:49:00 -04003297/**
3298 * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
3299 * @phba: Pointer to HBA context object.
3300 * @pmbox: Pointer to mailbox object.
3301 * @flag: Flag indicating how the mailbox need to be processed.
3302 *
3303 * This function is called by discovery code and HBA management code
3304 * to submit a mailbox command to firmware. This function gets the
3305 * hbalock to protect the data structures.
3306 * The mailbox command can be submitted in polling mode, in which case
3307 * this function will wait in a polling loop for the completion of the
3308 * mailbox.
3309 * If the mailbox is submitted in no_wait mode (not polling) the
3310 * function will submit the command and returns immediately without waiting
3311 * for the mailbox completion. The no_wait is supported only when HBA
3312 * is in SLI2/SLI3 mode - interrupts are enabled.
3313 * The SLI interface allows only one mailbox pending at a time. If the
3314 * mailbox is issued in polling mode and there is already a mailbox
3315 * pending, then the function will return an error. If the mailbox is issued
3316 * in NO_WAIT mode and there is a mailbox pending already, the function
3317 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
3318 * The sli layer owns the mailbox object until the completion of mailbox
3319 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
3320 * return codes the caller owns the mailbox command after the return of
3321 * the function.
3322 **/
dea31012005-04-17 16:05:31 -05003323int
James Smart2e0fef82007-06-17 19:56:36 -05003324lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
dea31012005-04-17 16:05:31 -05003325{
dea31012005-04-17 16:05:31 -05003326 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05003327 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003328 uint32_t status, evtctr;
3329 uint32_t ha_copy;
3330 int i;
James Smart09372822008-01-11 01:52:54 -05003331 unsigned long timeout;
dea31012005-04-17 16:05:31 -05003332 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04003333 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05003334 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04003335 int processing_queue = 0;
3336
3337 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3338 if (!pmbox) {
3339 /* processing mbox queue from intr_handler */
3340 processing_queue = 1;
3341 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3342 pmbox = lpfc_mbox_get(phba);
3343 if (!pmbox) {
3344 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3345 return MBX_SUCCESS;
3346 }
3347 }
dea31012005-04-17 16:05:31 -05003348
James Smarted957682007-06-17 19:56:37 -05003349 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05003350 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05003351 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04003352 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05003353 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003354 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003355 "1806 Mbox x%x failed. No vport\n",
James Smarted957682007-06-17 19:56:37 -05003356 pmbox->mb.mbxCommand);
3357 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04003358 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05003359 }
3360 }
3361
Linas Vepstas8d63f372007-02-14 14:28:36 -06003362 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04003363 if (unlikely(pci_channel_offline(phba->pcidev))) {
3364 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3365 goto out_not_finished;
3366 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06003367
dea31012005-04-17 16:05:31 -05003368 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05003369
dea31012005-04-17 16:05:31 -05003370 mb = &pmbox->mb;
3371 status = MBX_SUCCESS;
3372
James Smart2e0fef82007-06-17 19:56:36 -05003373 if (phba->link_state == LPFC_HBA_ERROR) {
3374 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003375
3376 /* Mbox command <mbxCommand> cannot issue */
James Smart7f5f3d02008-02-08 18:50:14 -05003377 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003378 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003379 }
3380
James Smart92908312006-03-07 15:04:13 -05003381 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
3382 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05003383 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart7f5f3d02008-02-08 18:50:14 -05003384 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003385 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05003386 }
3387
dea31012005-04-17 16:05:31 -05003388 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
3389 /* Polling for a mbox command when another one is already active
3390 * is not allowed in SLI. Also, the driver must have established
3391 * SLI2 mode to queue and process multiple mbox commands.
3392 */
3393
3394 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05003395 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003396
3397 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003398 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003399 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003400 }
3401
3402 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003403 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003404 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003405 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003406 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003407 }
3408
dea31012005-04-17 16:05:31 -05003409 /* Another mailbox command is still being processed, queue this
3410 * command to be processed later.
3411 */
3412 lpfc_mbox_put(phba, pmbox);
3413
3414 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05003415 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003416 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003417 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003418 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
3419 mb->mbxCommand, phba->pport->port_state,
3420 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003421
3422 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05003423 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003424
James Smart858c9f62007-06-17 19:56:39 -05003425 if (pmbox->vport) {
3426 lpfc_debugfs_disc_trc(pmbox->vport,
3427 LPFC_DISC_TRC_MBOX_VPORT,
3428 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
3429 (uint32_t)mb->mbxCommand,
3430 mb->un.varWords[0], mb->un.varWords[1]);
3431 }
3432 else {
3433 lpfc_debugfs_disc_trc(phba->pport,
3434 LPFC_DISC_TRC_MBOX,
3435 "MBOX Bsy: cmd:x%x mb:x%x x%x",
3436 (uint32_t)mb->mbxCommand,
3437 mb->un.varWords[0], mb->un.varWords[1]);
3438 }
3439
James Smart2e0fef82007-06-17 19:56:36 -05003440 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05003441 }
3442
dea31012005-04-17 16:05:31 -05003443 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3444
3445 /* If we are not polling, we MUST be in SLI2 mode */
3446 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05003447 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
3448 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05003449 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003450 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003451 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003452 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003453 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003454 }
3455 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04003456 mod_timer(&psli->mbox_tmo, (jiffies +
3457 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05003458 }
3459
3460 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05003461 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003462 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003463 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003464 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05003465 mb->mbxCommand, phba->pport->port_state,
3466 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003467
James Smart858c9f62007-06-17 19:56:39 -05003468 if (mb->mbxCommand != MBX_HEARTBEAT) {
3469 if (pmbox->vport) {
3470 lpfc_debugfs_disc_trc(pmbox->vport,
3471 LPFC_DISC_TRC_MBOX_VPORT,
3472 "MBOX Send vport: cmd:x%x mb:x%x x%x",
3473 (uint32_t)mb->mbxCommand,
3474 mb->un.varWords[0], mb->un.varWords[1]);
3475 }
3476 else {
3477 lpfc_debugfs_disc_trc(phba->pport,
3478 LPFC_DISC_TRC_MBOX,
3479 "MBOX Send: cmd:x%x mb:x%x x%x",
3480 (uint32_t)mb->mbxCommand,
3481 mb->un.varWords[0], mb->un.varWords[1]);
3482 }
3483 }
3484
dea31012005-04-17 16:05:31 -05003485 psli->slistat.mbox_cmd++;
3486 evtctr = psli->slistat.mbox_event;
3487
3488 /* next set own bit for the adapter and copy over command word */
3489 mb->mbxOwner = OWN_CHIP;
3490
3491 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003492 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04003493 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003494 } else {
James Smart92908312006-03-07 15:04:13 -05003495 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05003496 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04003497 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003498 }
3499
3500 /* First copy mbox command data to HBA SLIM, skip past first
3501 word */
3502 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3503 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
3504 MAILBOX_CMD_SIZE - sizeof (uint32_t));
3505
3506 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04003507 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05003508 to_slim = phba->MBslimaddr;
3509 writel(ldata, to_slim);
3510 readl(to_slim); /* flush */
3511
3512 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3513 /* switch over to host mailbox */
3514 psli->sli_flag |= LPFC_SLI2_ACTIVE;
3515 }
3516 }
3517
3518 wmb();
dea31012005-04-17 16:05:31 -05003519
3520 switch (flag) {
3521 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05003522 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05003523 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05003524 /* Interrupt board to do it */
3525 writel(CA_MBATT, phba->CAregaddr);
3526 readl(phba->CAregaddr); /* flush */
3527 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05003528 break;
3529
3530 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05003531 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05003532 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05003533 /* Interrupt board to do it */
3534 writel(CA_MBATT, phba->CAregaddr);
3535 readl(phba->CAregaddr); /* flush */
3536
dea31012005-04-17 16:05:31 -05003537 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3538 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04003539 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003540 word0 = le32_to_cpu(word0);
3541 } else {
3542 /* First read mbox status word */
3543 word0 = readl(phba->MBslimaddr);
3544 }
3545
3546 /* Read the HBA Host Attention Register */
3547 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05003548 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3549 mb->mbxCommand) *
3550 1000) + jiffies;
3551 i = 0;
dea31012005-04-17 16:05:31 -05003552 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003553 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
3554 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05003555 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05003556 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05003557 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003558 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05003559 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04003560 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003561 }
3562
3563 /* Check if we took a mbox interrupt while we were
3564 polling */
3565 if (((word0 & OWN_CHIP) != OWN_CHIP)
3566 && (evtctr != psli->slistat.mbox_event))
3567 break;
3568
James Smart09372822008-01-11 01:52:54 -05003569 if (i++ > 10) {
3570 spin_unlock_irqrestore(&phba->hbalock,
3571 drvr_flag);
3572 msleep(1);
3573 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3574 }
dea31012005-04-17 16:05:31 -05003575
3576 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3577 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04003578 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003579 word0 = le32_to_cpu(word0);
3580 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3581 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04003582 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05003583 /* Check real SLIM for any errors */
3584 slimword0 = readl(phba->MBslimaddr);
3585 slimmb = (MAILBOX_t *) & slimword0;
3586 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
3587 && slimmb->mbxStatus) {
3588 psli->sli_flag &=
3589 ~LPFC_SLI2_ACTIVE;
3590 word0 = slimword0;
3591 }
3592 }
3593 } else {
3594 /* First copy command data */
3595 word0 = readl(phba->MBslimaddr);
3596 }
3597 /* Read the HBA Host Attention Register */
3598 ha_copy = readl(phba->HAregaddr);
3599 }
3600
3601 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003602 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04003603 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003604 } else {
3605 /* First copy command data */
3606 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
3607 MAILBOX_CMD_SIZE);
3608 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
3609 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05003610 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05003611 phba->MBslimaddr + DMP_RSP_OFFSET,
3612 mb->un.varDmp.word_cnt);
3613 }
3614 }
3615
3616 writel(HA_MBATT, phba->HAregaddr);
3617 readl(phba->HAregaddr); /* flush */
3618
3619 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3620 status = mb->mbxStatus;
3621 }
3622
James Smart2e0fef82007-06-17 19:56:36 -05003623 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3624 return status;
James Smart58da1ff2008-04-07 10:15:56 -04003625
3626out_not_finished:
3627 if (processing_queue) {
3628 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
3629 lpfc_mbox_cmpl_put(phba, pmbox);
3630 }
3631 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003632}
3633
James Smarte59058c2008-08-24 21:49:00 -04003634/**
3635 * __lpfc_sli_ringtx_put: Add an iocb to the txq.
3636 * @phba: Pointer to HBA context object.
3637 * @pring: Pointer to driver SLI ring object.
3638 * @piocb: Pointer to address of newly added command iocb.
3639 *
3640 * This function is called with hbalock held to add a command
3641 * iocb to the txq when SLI layer cannot submit the command iocb
3642 * to the ring.
3643 **/
James Smart858c9f62007-06-17 19:56:39 -05003644static void
James Smart92d7f7b2007-06-17 19:56:38 -05003645__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003646 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05003647{
3648 /* Insert the caller's iocb in the txq tail for later processing. */
3649 list_add_tail(&piocb->list, &pring->txq);
3650 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05003651}
3652
James Smarte59058c2008-08-24 21:49:00 -04003653/**
3654 * lpfc_sli_next_iocb: Get the next iocb in the txq.
3655 * @phba: Pointer to HBA context object.
3656 * @pring: Pointer to driver SLI ring object.
3657 * @piocb: Pointer to address of newly added command iocb.
3658 *
3659 * This function is called with hbalock held before a new
3660 * iocb is submitted to the firmware. This function checks
3661 * txq to flush the iocbs in txq to Firmware before
3662 * submitting new iocbs to the Firmware.
3663 * If there are iocbs in the txq which need to be submitted
3664 * to firmware, lpfc_sli_next_iocb returns the first element
3665 * of the txq after dequeuing it from txq.
3666 * If there is no iocb in the txq then the function will return
3667 * *piocb and *piocb is set to NULL. Caller needs to check
3668 * *piocb to find if there are more commands in the txq.
3669 **/
dea31012005-04-17 16:05:31 -05003670static struct lpfc_iocbq *
3671lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003672 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05003673{
3674 struct lpfc_iocbq * nextiocb;
3675
3676 nextiocb = lpfc_sli_ringtx_get(phba, pring);
3677 if (!nextiocb) {
3678 nextiocb = *piocb;
3679 *piocb = NULL;
3680 }
3681
3682 return nextiocb;
3683}
3684
James Smarte59058c2008-08-24 21:49:00 -04003685/**
3686 * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
3687 * @phba: Pointer to HBA context object.
3688 * @pring: Pointer to driver SLI ring object.
3689 * @piocb: Pointer to command iocb.
3690 * @flag: Flag indicating if this command can be put into txq.
3691 *
3692 * __lpfc_sli_issue_iocb is used by other functions in the driver
3693 * to issue an iocb command to the HBA. If the PCI slot is recovering
3694 * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
3695 * flag is turned on, the function returns IOCB_ERROR.
3696 * When the link is down, this function allows only iocbs for
3697 * posting buffers.
3698 * This function finds next available slot in the command ring and
3699 * posts the command to the available slot and writes the port
3700 * attention register to request HBA start processing new iocb.
3701 * If there is no slot available in the ring and
3702 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
3703 * txq, otherwise the function returns IOCB_BUSY.
3704 *
3705 * This function is called with hbalock held.
3706 * The function will return success after it successfully submit the
3707 * iocb to firmware or after adding to the txq.
3708 **/
James Smart98c9ea52007-10-27 13:37:33 -04003709static int
James Smart92d7f7b2007-06-17 19:56:38 -05003710__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003711 struct lpfc_iocbq *piocb, uint32_t flag)
3712{
3713 struct lpfc_iocbq *nextiocb;
3714 IOCB_t *iocb;
3715
James Smart92d7f7b2007-06-17 19:56:38 -05003716 if (piocb->iocb_cmpl && (!piocb->vport) &&
3717 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
3718 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
3719 lpfc_printf_log(phba, KERN_ERR,
3720 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003721 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003722 piocb->iocb.ulpCommand);
3723 dump_stack();
3724 return IOCB_ERROR;
3725 }
3726
3727
Linas Vepstas8d63f372007-02-14 14:28:36 -06003728 /* If the PCI channel is in offline state, do not post iocbs. */
3729 if (unlikely(pci_channel_offline(phba->pcidev)))
3730 return IOCB_ERROR;
3731
dea31012005-04-17 16:05:31 -05003732 /*
3733 * We should never get an IOCB if we are in a < LINK_DOWN state
3734 */
James Smart2e0fef82007-06-17 19:56:36 -05003735 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003736 return IOCB_ERROR;
3737
3738 /*
3739 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04003740 * outstanding event.
dea31012005-04-17 16:05:31 -05003741 */
James Smart0b727fe2007-10-27 13:37:25 -04003742 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05003743 goto iocb_busy;
3744
James Smart2e0fef82007-06-17 19:56:36 -05003745 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05003746 /*
James Smart2680eea2007-04-25 09:52:55 -04003747 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05003748 * can be issued if the link is not up.
3749 */
3750 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04003751 case CMD_GEN_REQUEST64_CR:
3752 case CMD_GEN_REQUEST64_CX:
3753 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
3754 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
3755 FC_FCP_CMND) ||
3756 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
3757 MENLO_TRANSPORT_TYPE))
3758
3759 goto iocb_busy;
3760 break;
dea31012005-04-17 16:05:31 -05003761 case CMD_QUE_RING_BUF_CN:
3762 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05003763 /*
3764 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
3765 * completion, iocb_cmpl MUST be 0.
3766 */
3767 if (piocb->iocb_cmpl)
3768 piocb->iocb_cmpl = NULL;
3769 /*FALLTHROUGH*/
3770 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04003771 case CMD_CLOSE_XRI_CN:
3772 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05003773 break;
3774 default:
3775 goto iocb_busy;
3776 }
3777
3778 /*
3779 * For FCP commands, we must be in a state where we can process link
3780 * attention events.
3781 */
3782 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05003783 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05003784 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05003785 }
dea31012005-04-17 16:05:31 -05003786
dea31012005-04-17 16:05:31 -05003787 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
3788 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
3789 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
3790
3791 if (iocb)
3792 lpfc_sli_update_ring(phba, pring);
3793 else
3794 lpfc_sli_update_full_ring(phba, pring);
3795
3796 if (!piocb)
3797 return IOCB_SUCCESS;
3798
3799 goto out_busy;
3800
3801 iocb_busy:
3802 pring->stats.iocb_cmd_delay++;
3803
3804 out_busy:
3805
3806 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003807 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05003808 return IOCB_SUCCESS;
3809 }
3810
3811 return IOCB_BUSY;
3812}
3813
James Smart92d7f7b2007-06-17 19:56:38 -05003814
James Smarte59058c2008-08-24 21:49:00 -04003815/**
3816 * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
3817 * @phba: Pointer to HBA context object.
3818 * @pring: Pointer to driver SLI ring object.
3819 * @piocb: Pointer to command iocb.
3820 * @flag: Flag indicating if this command can be put into txq.
3821 *
3822 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
3823 * function. This function gets the hbalock and calls
3824 * __lpfc_sli_issue_iocb function and will return the error returned
3825 * by __lpfc_sli_issue_iocb function. This wrapper is used by
3826 * functions which do not hold hbalock.
3827 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003828int
3829lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3830 struct lpfc_iocbq *piocb, uint32_t flag)
3831{
3832 unsigned long iflags;
3833 int rc;
3834
3835 spin_lock_irqsave(&phba->hbalock, iflags);
3836 rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
3837 spin_unlock_irqrestore(&phba->hbalock, iflags);
3838
3839 return rc;
3840}
3841
James Smarte59058c2008-08-24 21:49:00 -04003842/**
3843 * lpfc_extra_ring_setup: Extra ring setup function.
3844 * @phba: Pointer to HBA context object.
3845 *
3846 * This function is called while driver attaches with the
3847 * HBA to setup the extra ring. The extra ring is used
3848 * only when driver needs to support target mode functionality
3849 * or IP over FC functionalities.
3850 *
3851 * This function is called with no lock held.
3852 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003853static int
3854lpfc_extra_ring_setup( struct lpfc_hba *phba)
3855{
3856 struct lpfc_sli *psli;
3857 struct lpfc_sli_ring *pring;
3858
3859 psli = &phba->sli;
3860
3861 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05003862
3863 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003864 pring = &psli->ring[psli->fcp_ring];
3865 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3866 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3867 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3868 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3869
James Smarta4bc3372006-12-02 13:34:16 -05003870 /* and give them to the extra ring */
3871 pring = &psli->ring[psli->extra_ring];
3872
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003873 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3874 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3875 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3876 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3877
3878 /* Setup default profile for this ring */
3879 pring->iotag_max = 4096;
3880 pring->num_mask = 1;
3881 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05003882 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
3883 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003884 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
3885 return 0;
3886}
3887
James Smarte59058c2008-08-24 21:49:00 -04003888/**
3889 * lpfc_sli_async_event_handler: ASYNC iocb handler function.
3890 * @phba: Pointer to HBA context object.
3891 * @pring: Pointer to driver SLI ring object.
3892 * @iocbq: Pointer to iocb object.
3893 *
3894 * This function is called by the slow ring event handler
3895 * function when there is an ASYNC event iocb in the ring.
3896 * This function is called with no lock held.
3897 * Currently this function handles only temperature related
3898 * ASYNC events. The function decodes the temperature sensor
3899 * event message and posts events for the management applications.
3900 **/
James Smart98c9ea52007-10-27 13:37:33 -04003901static void
James Smart57127f12007-10-27 13:37:05 -04003902lpfc_sli_async_event_handler(struct lpfc_hba * phba,
3903 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
3904{
3905 IOCB_t *icmd;
3906 uint16_t evt_code;
3907 uint16_t temp;
3908 struct temp_event temp_event_data;
3909 struct Scsi_Host *shost;
3910
3911 icmd = &iocbq->iocb;
3912 evt_code = icmd->un.asyncstat.evt_code;
3913 temp = icmd->ulpContext;
3914
3915 if ((evt_code != ASYNC_TEMP_WARN) &&
3916 (evt_code != ASYNC_TEMP_SAFE)) {
3917 lpfc_printf_log(phba,
3918 KERN_ERR,
3919 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003920 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smart57127f12007-10-27 13:37:05 -04003921 " evt_code 0x%x\n",
3922 pring->ringno,
3923 icmd->un.asyncstat.evt_code);
3924 return;
3925 }
3926 temp_event_data.data = (uint32_t)temp;
3927 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
3928 if (evt_code == ASYNC_TEMP_WARN) {
3929 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
3930 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003931 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003932 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04003933 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04003934 "corrective action. temperature : %d Celsius\n",
3935 temp);
3936 }
3937 if (evt_code == ASYNC_TEMP_SAFE) {
3938 temp_event_data.event_code = LPFC_NORMAL_TEMP;
3939 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003940 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003941 LOG_TEMP,
3942 "0340 Adapter temperature is OK now. "
3943 "temperature : %d Celsius\n",
3944 temp);
3945 }
3946
3947 /* Send temperature change event to applications */
3948 shost = lpfc_shost_from_vport(phba->pport);
3949 fc_host_post_vendor_event(shost, fc_get_event_number(),
3950 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05003951 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04003952
3953}
3954
3955
James Smarte59058c2008-08-24 21:49:00 -04003956/**
3957 * lpfc_sli_setup: SLI ring setup function.
3958 * @phba: Pointer to HBA context object.
3959 *
3960 * lpfc_sli_setup sets up rings of the SLI interface with
3961 * number of iocbs per ring and iotags. This function is
3962 * called while driver attach to the HBA and before the
3963 * interrupts are enabled. So there is no need for locking.
3964 *
3965 * This function always returns 0.
3966 **/
dea31012005-04-17 16:05:31 -05003967int
3968lpfc_sli_setup(struct lpfc_hba *phba)
3969{
James Smarted957682007-06-17 19:56:37 -05003970 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05003971 struct lpfc_sli *psli = &phba->sli;
3972 struct lpfc_sli_ring *pring;
3973
3974 psli->num_rings = MAX_CONFIGURED_RINGS;
3975 psli->sli_flag = 0;
3976 psli->fcp_ring = LPFC_FCP_RING;
3977 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05003978 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05003979
James Bottomley604a3e32005-10-29 10:28:33 -05003980 psli->iocbq_lookup = NULL;
3981 psli->iocbq_lookup_len = 0;
3982 psli->last_iotag = 0;
3983
dea31012005-04-17 16:05:31 -05003984 for (i = 0; i < psli->num_rings; i++) {
3985 pring = &psli->ring[i];
3986 switch (i) {
3987 case LPFC_FCP_RING: /* ring 0 - FCP */
3988 /* numCiocb and numRiocb are used in config_port */
3989 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3990 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3991 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3992 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3993 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3994 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003995 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003996 SLI3_IOCB_CMD_SIZE :
3997 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003998 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003999 SLI3_IOCB_RSP_SIZE :
4000 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05004001 pring->iotag_ctr = 0;
4002 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05004003 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05004004 pring->fast_iotag = pring->iotag_max;
4005 pring->num_mask = 0;
4006 break;
James Smarta4bc3372006-12-02 13:34:16 -05004007 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05004008 /* numCiocb and numRiocb are used in config_port */
4009 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
4010 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004011 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004012 SLI3_IOCB_CMD_SIZE :
4013 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004014 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004015 SLI3_IOCB_RSP_SIZE :
4016 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05004017 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05004018 pring->num_mask = 0;
4019 break;
4020 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
4021 /* numCiocb and numRiocb are used in config_port */
4022 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
4023 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004024 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004025 SLI3_IOCB_CMD_SIZE :
4026 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004027 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004028 SLI3_IOCB_RSP_SIZE :
4029 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05004030 pring->fast_iotag = 0;
4031 pring->iotag_ctr = 0;
4032 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04004033 pring->lpfc_sli_rcv_async_status =
4034 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05004035 pring->num_mask = 4;
4036 pring->prt[0].profile = 0; /* Mask 0 */
4037 pring->prt[0].rctl = FC_ELS_REQ;
4038 pring->prt[0].type = FC_ELS_DATA;
4039 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004040 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004041 pring->prt[1].profile = 0; /* Mask 1 */
4042 pring->prt[1].rctl = FC_ELS_RSP;
4043 pring->prt[1].type = FC_ELS_DATA;
4044 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004045 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004046 pring->prt[2].profile = 0; /* Mask 2 */
4047 /* NameServer Inquiry */
4048 pring->prt[2].rctl = FC_UNSOL_CTL;
4049 /* NameServer */
4050 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
4051 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004052 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004053 pring->prt[3].profile = 0; /* Mask 3 */
4054 /* NameServer response */
4055 pring->prt[3].rctl = FC_SOL_CTL;
4056 /* NameServer */
4057 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
4058 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004059 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004060 break;
4061 }
James Smarted957682007-06-17 19:56:37 -05004062 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05004063 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05004064 }
James Smarted957682007-06-17 19:56:37 -05004065 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05004066 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04004067 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
4068 "SLI2 SLIM Data: x%x x%lx\n",
4069 phba->brd_no, totiocbsize,
4070 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05004071 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004072 if (phba->cfg_multi_ring_support == 2)
4073 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05004074
4075 return 0;
4076}
4077
James Smarte59058c2008-08-24 21:49:00 -04004078/**
4079 * lpfc_sli_queue_setup: Queue initialization function.
4080 * @phba: Pointer to HBA context object.
4081 *
4082 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
4083 * ring. This function also initializes ring indices of each ring.
4084 * This function is called during the initialization of the SLI
4085 * interface of an HBA.
4086 * This function is called with no lock held and always returns
4087 * 1.
4088 **/
dea31012005-04-17 16:05:31 -05004089int
James Smart2e0fef82007-06-17 19:56:36 -05004090lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004091{
4092 struct lpfc_sli *psli;
4093 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05004094 int i;
dea31012005-04-17 16:05:31 -05004095
4096 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05004097 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004098 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05004099 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05004100 /* Initialize list headers for txq and txcmplq as double linked lists */
4101 for (i = 0; i < psli->num_rings; i++) {
4102 pring = &psli->ring[i];
4103 pring->ringno = i;
4104 pring->next_cmdidx = 0;
4105 pring->local_getidx = 0;
4106 pring->cmdidx = 0;
4107 INIT_LIST_HEAD(&pring->txq);
4108 INIT_LIST_HEAD(&pring->txcmplq);
4109 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05004110 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05004111 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05004112 }
James Smart2e0fef82007-06-17 19:56:36 -05004113 spin_unlock_irq(&phba->hbalock);
4114 return 1;
dea31012005-04-17 16:05:31 -05004115}
4116
James Smarte59058c2008-08-24 21:49:00 -04004117/**
4118 * lpfc_sli_host_down: Vport cleanup function.
4119 * @vport: Pointer to virtual port object.
4120 *
4121 * lpfc_sli_host_down is called to clean up the resources
4122 * associated with a vport before destroying virtual
4123 * port data structures.
4124 * This function does following operations:
4125 * - Free discovery resources associated with this virtual
4126 * port.
4127 * - Free iocbs associated with this virtual port in
4128 * the txq.
4129 * - Send abort for all iocb commands associated with this
4130 * vport in txcmplq.
4131 *
4132 * This function is called with no lock held and always returns 1.
4133 **/
dea31012005-04-17 16:05:31 -05004134int
James Smart92d7f7b2007-06-17 19:56:38 -05004135lpfc_sli_host_down(struct lpfc_vport *vport)
4136{
James Smart858c9f62007-06-17 19:56:39 -05004137 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004138 struct lpfc_hba *phba = vport->phba;
4139 struct lpfc_sli *psli = &phba->sli;
4140 struct lpfc_sli_ring *pring;
4141 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004142 int i;
4143 unsigned long flags = 0;
4144 uint16_t prev_pring_flag;
4145
4146 lpfc_cleanup_discovery_resources(vport);
4147
4148 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004149 for (i = 0; i < psli->num_rings; i++) {
4150 pring = &psli->ring[i];
4151 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04004152 /* Only slow rings */
4153 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004154 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004155 /* Set the lpfc data pending flag */
4156 set_bit(LPFC_DATA_READY, &phba->data_flags);
4157 }
James Smart92d7f7b2007-06-17 19:56:38 -05004158 /*
4159 * Error everything on the txq since these iocbs have not been
4160 * given to the FW yet.
4161 */
James Smart92d7f7b2007-06-17 19:56:38 -05004162 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4163 if (iocb->vport != vport)
4164 continue;
James Smart858c9f62007-06-17 19:56:39 -05004165 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004166 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05004167 }
4168
4169 /* Next issue ABTS for everything on the txcmplq */
4170 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
4171 list) {
4172 if (iocb->vport != vport)
4173 continue;
4174 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4175 }
4176
4177 pring->flag = prev_pring_flag;
4178 }
4179
4180 spin_unlock_irqrestore(&phba->hbalock, flags);
4181
James Smart858c9f62007-06-17 19:56:39 -05004182 while (!list_empty(&completions)) {
4183 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
4184
4185 if (!iocb->iocb_cmpl)
4186 lpfc_sli_release_iocbq(phba, iocb);
4187 else {
4188 iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4189 iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
4190 (iocb->iocb_cmpl) (phba, iocb, iocb);
4191 }
4192 }
James Smart92d7f7b2007-06-17 19:56:38 -05004193 return 1;
4194}
4195
James Smarte59058c2008-08-24 21:49:00 -04004196/**
4197 * lpfc_sli_hba_down: Resource cleanup function for the HBA.
4198 * @phba: Pointer to HBA context object.
4199 *
4200 * This function cleans up all iocb, buffers, mailbox commands
4201 * while shutting down the HBA. This function is called with no
4202 * lock held and always returns 1.
4203 * This function does the following to cleanup driver resources:
4204 * - Free discovery resources for each virtual port
4205 * - Cleanup any pending fabric iocbs
4206 * - Iterate through the iocb txq and free each entry
4207 * in the list.
4208 * - Free up any buffer posted to the HBA
4209 * - Free mailbox commands in the mailbox queue.
4210 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004211int
James Smart2e0fef82007-06-17 19:56:36 -05004212lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004213{
James Smart2534ba72007-04-25 09:52:20 -04004214 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05004215 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004216 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05004217 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05004218 LPFC_MBOXQ_t *pmb;
James Smart2534ba72007-04-25 09:52:20 -04004219 struct lpfc_iocbq *iocb;
4220 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004221 int i;
4222 unsigned long flags = 0;
4223
dea31012005-04-17 16:05:31 -05004224 lpfc_hba_down_prep(phba);
4225
James Smart92d7f7b2007-06-17 19:56:38 -05004226 lpfc_fabric_abort_hba(phba);
4227
James Smart2e0fef82007-06-17 19:56:36 -05004228 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004229 for (i = 0; i < psli->num_rings; i++) {
4230 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04004231 /* Only slow rings */
4232 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004233 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004234 /* Set the lpfc data pending flag */
4235 set_bit(LPFC_DATA_READY, &phba->data_flags);
4236 }
dea31012005-04-17 16:05:31 -05004237
4238 /*
4239 * Error everything on the txq since these iocbs have not been
4240 * given to the FW yet.
4241 */
James Smart2534ba72007-04-25 09:52:20 -04004242 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05004243 pring->txq_cnt = 0;
4244
dea31012005-04-17 16:05:31 -05004245 }
James Smart2e0fef82007-06-17 19:56:36 -05004246 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004247
James Smart2534ba72007-04-25 09:52:20 -04004248 while (!list_empty(&completions)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004249 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
James Smart2534ba72007-04-25 09:52:20 -04004250 cmd = &iocb->iocb;
James Smart2534ba72007-04-25 09:52:20 -04004251
James Smart2e0fef82007-06-17 19:56:36 -05004252 if (!iocb->iocb_cmpl)
4253 lpfc_sli_release_iocbq(phba, iocb);
4254 else {
James Smart2534ba72007-04-25 09:52:20 -04004255 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4256 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
4257 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05004258 }
James Smart2534ba72007-04-25 09:52:20 -04004259 }
4260
James Smart0ff10d42008-01-11 01:52:36 -05004261 spin_lock_irqsave(&phba->hbalock, flags);
4262 list_splice_init(&phba->elsbuf, &completions);
4263 phba->elsbuf_cnt = 0;
4264 phba->elsbuf_prev_cnt = 0;
4265 spin_unlock_irqrestore(&phba->hbalock, flags);
4266
4267 while (!list_empty(&completions)) {
4268 list_remove_head(&completions, buf_ptr,
4269 struct lpfc_dmabuf, list);
4270 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4271 kfree(buf_ptr);
4272 }
4273
dea31012005-04-17 16:05:31 -05004274 /* Return any active mbox cmds */
4275 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05004276 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004277
4278 spin_lock(&phba->pport->work_port_lock);
4279 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4280 spin_unlock(&phba->pport->work_port_lock);
4281
James Smart97eab632008-04-07 10:16:05 -04004282 /* Return any pending or completed mbox cmds */
4283 list_splice_init(&phba->sli.mboxq, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004284 if (psli->mbox_active) {
4285 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05004286 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004287 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05004288 }
James Smart92d7f7b2007-06-17 19:56:38 -05004289 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004290 spin_unlock_irqrestore(&phba->hbalock, flags);
4291
4292 while (!list_empty(&completions)) {
4293 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05004294 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
James Smart97eab632008-04-07 10:16:05 -04004295 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05004296 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05004297 }
dea31012005-04-17 16:05:31 -05004298 return 1;
4299}
4300
James Smarte59058c2008-08-24 21:49:00 -04004301/**
4302 * lpfc_sli_pcimem_bcopy: SLI memory copy function.
4303 * @srcp: Source memory pointer.
4304 * @destp: Destination memory pointer.
4305 * @cnt: Number of words required to be copied.
4306 *
4307 * This function is used for copying data between driver memory
4308 * and the SLI memory. This function also changes the endianness
4309 * of each word if native endianness is different from SLI
4310 * endianness. This function can be called with or without
4311 * lock.
4312 **/
dea31012005-04-17 16:05:31 -05004313void
4314lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
4315{
4316 uint32_t *src = srcp;
4317 uint32_t *dest = destp;
4318 uint32_t ldata;
4319 int i;
4320
4321 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
4322 ldata = *src;
4323 ldata = le32_to_cpu(ldata);
4324 *dest = ldata;
4325 src++;
4326 dest++;
4327 }
4328}
4329
James Smarte59058c2008-08-24 21:49:00 -04004330
4331/**
4332 * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
4333 * @phba: Pointer to HBA context object.
4334 * @pring: Pointer to driver SLI ring object.
4335 * @mp: Pointer to driver buffer object.
4336 *
4337 * This function is called with no lock held.
4338 * It always return zero after adding the buffer to the postbufq
4339 * buffer list.
4340 **/
dea31012005-04-17 16:05:31 -05004341int
James Smart2e0fef82007-06-17 19:56:36 -05004342lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4343 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05004344{
4345 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
4346 later */
James Smart2e0fef82007-06-17 19:56:36 -05004347 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004348 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05004349 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05004350 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004351 return 0;
4352}
4353
James Smarte59058c2008-08-24 21:49:00 -04004354/**
4355 * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
4356 * using CMD_QUE_XRI64_CX iocb.
4357 * @phba: Pointer to HBA context object.
4358 *
4359 * When HBQ is enabled, buffers are searched based on tags. This function
4360 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
4361 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
4362 * does not conflict with tags of buffer posted for unsolicited events.
4363 * The function returns the allocated tag. The function is called with
4364 * no locks held.
4365 **/
James Smart76bb24e2007-10-27 13:38:00 -04004366uint32_t
4367lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
4368{
4369 spin_lock_irq(&phba->hbalock);
4370 phba->buffer_tag_count++;
4371 /*
4372 * Always set the QUE_BUFTAG_BIT to distiguish between
4373 * a tag assigned by HBQ.
4374 */
4375 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
4376 spin_unlock_irq(&phba->hbalock);
4377 return phba->buffer_tag_count;
4378}
4379
James Smarte59058c2008-08-24 21:49:00 -04004380/**
4381 * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
4382 * posted using CMD_QUE_XRI64_CX iocb.
4383 * @phba: Pointer to HBA context object.
4384 * @pring: Pointer to driver SLI ring object.
4385 * @tag: Buffer tag.
4386 *
4387 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
4388 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
4389 * iocb is posted to the response ring with the tag of the buffer.
4390 * This function searches the pring->postbufq list using the tag
4391 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
4392 * iocb. If the buffer is found then lpfc_dmabuf object of the
4393 * buffer is returned to the caller else NULL is returned.
4394 * This function is called with no lock held.
4395 **/
James Smart76bb24e2007-10-27 13:38:00 -04004396struct lpfc_dmabuf *
4397lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4398 uint32_t tag)
4399{
4400 struct lpfc_dmabuf *mp, *next_mp;
4401 struct list_head *slp = &pring->postbufq;
4402
4403 /* Search postbufq, from the begining, looking for a match on tag */
4404 spin_lock_irq(&phba->hbalock);
4405 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4406 if (mp->buffer_tag == tag) {
4407 list_del_init(&mp->list);
4408 pring->postbufq_cnt--;
4409 spin_unlock_irq(&phba->hbalock);
4410 return mp;
4411 }
4412 }
4413
4414 spin_unlock_irq(&phba->hbalock);
4415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04004416 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04004417 "ring %d Data x%lx x%p x%p x%x\n",
4418 pring->ringno, (unsigned long) tag,
4419 slp->next, slp->prev, pring->postbufq_cnt);
4420
4421 return NULL;
4422}
dea31012005-04-17 16:05:31 -05004423
James Smarte59058c2008-08-24 21:49:00 -04004424/**
4425 * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
4426 * unsolicited ct and els events.
4427 * @phba: Pointer to HBA context object.
4428 * @pring: Pointer to driver SLI ring object.
4429 * @phys: DMA address of the buffer.
4430 *
4431 * This function searches the buffer list using the dma_address
4432 * of unsolicited event to find the driver's lpfc_dmabuf object
4433 * corresponding to the dma_address. The function returns the
4434 * lpfc_dmabuf object if a buffer is found else it returns NULL.
4435 * This function is called by the ct and els unsolicited event
4436 * handlers to get the buffer associated with the unsolicited
4437 * event.
4438 *
4439 * This function is called with no lock held.
4440 **/
dea31012005-04-17 16:05:31 -05004441struct lpfc_dmabuf *
4442lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4443 dma_addr_t phys)
4444{
4445 struct lpfc_dmabuf *mp, *next_mp;
4446 struct list_head *slp = &pring->postbufq;
4447
4448 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05004449 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004450 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4451 if (mp->phys == phys) {
4452 list_del_init(&mp->list);
4453 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004454 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004455 return mp;
4456 }
4457 }
4458
James Smart2e0fef82007-06-17 19:56:36 -05004459 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004461 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05004462 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004463 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05004464 slp->next, slp->prev, pring->postbufq_cnt);
4465 return NULL;
4466}
4467
James Smarte59058c2008-08-24 21:49:00 -04004468/**
4469 * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
4470 * @phba: Pointer to HBA context object.
4471 * @cmdiocb: Pointer to driver command iocb object.
4472 * @rspiocb: Pointer to driver response iocb object.
4473 *
4474 * This function is the completion handler for the abort iocbs for
4475 * ELS commands. This function is called from the ELS ring event
4476 * handler with no lock held. This function frees memory resources
4477 * associated with the abort iocb.
4478 **/
dea31012005-04-17 16:05:31 -05004479static void
James Smart2e0fef82007-06-17 19:56:36 -05004480lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4481 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05004482{
James Smart2e0fef82007-06-17 19:56:36 -05004483 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04004484 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05004485 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04004486 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4487
4488 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04004489
4490 if (irsp->ulpStatus) {
4491 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
4492 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
4493
James Smart2e0fef82007-06-17 19:56:36 -05004494 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004495 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
4496 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
4497
James Smart92d7f7b2007-06-17 19:56:38 -05004498 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004499 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05004500 "with tag %x context %x, abort status %x, "
4501 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04004502 abort_iocb, abort_iotag, abort_context,
4503 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04004504
4505 /*
James Smart58da1ff2008-04-07 10:15:56 -04004506 * If the iocb is not found in Firmware queue the iocb
4507 * might have completed already. Do not free it again.
4508 */
James Smart9b379602008-04-07 10:16:00 -04004509 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04004510 spin_unlock_irq(&phba->hbalock);
4511 lpfc_sli_release_iocbq(phba, cmdiocb);
4512 return;
4513 }
4514 /*
James Smart2680eea2007-04-25 09:52:55 -04004515 * make sure we have the right iocbq before taking it
4516 * off the txcmplq and try to call completion routine.
4517 */
James Smart2e0fef82007-06-17 19:56:36 -05004518 if (!abort_iocb ||
4519 abort_iocb->iocb.ulpContext != abort_context ||
4520 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
4521 spin_unlock_irq(&phba->hbalock);
4522 else {
James Smart92d7f7b2007-06-17 19:56:38 -05004523 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04004524 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004525 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004526
James Smart0ff10d42008-01-11 01:52:36 -05004527 /* Firmware could still be in progress of DMAing
4528 * payload, so don't free data buffer till after
4529 * a hbeat.
4530 */
4531 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
4532
James Smart92d7f7b2007-06-17 19:56:38 -05004533 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4534 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4535 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
4536 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04004537 }
4538 }
4539
James Bottomley604a3e32005-10-29 10:28:33 -05004540 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05004541 return;
4542}
4543
James Smarte59058c2008-08-24 21:49:00 -04004544/**
4545 * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
4546 * @phba: Pointer to HBA context object.
4547 * @cmdiocb: Pointer to driver command iocb object.
4548 * @rspiocb: Pointer to driver response iocb object.
4549 *
4550 * The function is called from SLI ring event handler with no
4551 * lock held. This function is the completion handler for ELS commands
4552 * which are aborted. The function frees memory resources used for
4553 * the aborted ELS commands.
4554 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004555static void
4556lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4557 struct lpfc_iocbq *rspiocb)
4558{
4559 IOCB_t *irsp = &rspiocb->iocb;
4560
4561 /* ELS cmd tag <ulpIoTag> completes */
4562 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04004563 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004564 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004565 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05004566 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05004567 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
4568 lpfc_ct_free_iocb(phba, cmdiocb);
4569 else
4570 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05004571 return;
4572}
4573
James Smarte59058c2008-08-24 21:49:00 -04004574/**
4575 * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
4576 * @phba: Pointer to HBA context object.
4577 * @pring: Pointer to driver SLI ring object.
4578 * @cmdiocb: Pointer to driver command iocb object.
4579 *
4580 * This function issues an abort iocb for the provided command
4581 * iocb. This function is called with hbalock held.
4582 * The function returns 0 when it fails due to memory allocation
4583 * failure or when the command iocb is an abort request.
4584 **/
dea31012005-04-17 16:05:31 -05004585int
James Smart2e0fef82007-06-17 19:56:36 -05004586lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4587 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05004588{
James Smart2e0fef82007-06-17 19:56:36 -05004589 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004590 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05004591 IOCB_t *icmd = NULL;
4592 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04004593 int retval = IOCB_ERROR;
4594
James Smart92d7f7b2007-06-17 19:56:38 -05004595 /*
4596 * There are certain command types we don't want to abort. And we
4597 * don't want to abort commands that are already in the process of
4598 * being aborted.
James Smart07951072007-04-25 09:51:38 -04004599 */
4600 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004601 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05004602 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4603 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04004604 return 0;
4605
James Smart858c9f62007-06-17 19:56:39 -05004606 /* If we're unloading, don't abort iocb on the ELS ring, but change the
4607 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04004608 */
James Smart858c9f62007-06-17 19:56:39 -05004609 if ((vport->load_flag & FC_UNLOADING) &&
4610 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004611 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
4612 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
4613 else
4614 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04004615 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05004616 }
dea31012005-04-17 16:05:31 -05004617
4618 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05004619 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004620 if (abtsiocbp == NULL)
4621 return 0;
dea31012005-04-17 16:05:31 -05004622
James Smart07951072007-04-25 09:51:38 -04004623 /* This signals the response to set the correct status
4624 * before calling the completion handler.
4625 */
4626 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4627
dea31012005-04-17 16:05:31 -05004628 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04004629 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
4630 iabt->un.acxri.abortContextTag = icmd->ulpContext;
4631 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004632 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04004633 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05004634
James Smart2e0fef82007-06-17 19:56:36 -05004635 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04004636 iabt->ulpCommand = CMD_ABORT_XRI_CN;
4637 else
4638 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
4639
4640 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04004641
James Smarte8b62012007-08-02 11:10:09 -04004642 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4643 "0339 Abort xri x%x, original iotag x%x, "
4644 "abort cmd iotag x%x\n",
4645 iabt->un.acxri.abortContextTag,
4646 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05004647 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04004648
James Smartd7c255b2008-08-24 21:50:00 -04004649 if (retval)
4650 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04004651abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05004652 /*
4653 * Caller to this routine should check for IOCB_ERROR
4654 * and handle it properly. This routine no longer removes
4655 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04004656 */
James Smart2e0fef82007-06-17 19:56:36 -05004657 return retval;
dea31012005-04-17 16:05:31 -05004658}
4659
James Smarte59058c2008-08-24 21:49:00 -04004660/**
4661 * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
4662 * associated with a vport/SCSI target/lun.
4663 * @iocbq: Pointer to driver iocb object.
4664 * @vport: Pointer to driver virtual port object.
4665 * @tgt_id: SCSI ID of the target.
4666 * @lun_id: LUN ID of the scsi device.
4667 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
4668 *
4669 * This function acts as iocb filter for functions which abort or count
4670 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
4671 * 0 if the filtering criteria is met for the given iocb and will return
4672 * 1 if the filtering criteria is not met.
4673 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
4674 * given iocb is for the SCSI device specified by vport, tgt_id and
4675 * lun_id parameter.
4676 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
4677 * given iocb is for the SCSI target specified by vport and tgt_id
4678 * parameters.
4679 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
4680 * given iocb is for the SCSI host associated with the given vport.
4681 * This function is called with no locks held.
4682 **/
dea31012005-04-17 16:05:31 -05004683static int
James Smart51ef4c22007-08-02 11:10:31 -04004684lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
4685 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004686 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004687{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004688 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004689 int rc = 1;
4690
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004691 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
4692 return rc;
4693
James Smart51ef4c22007-08-02 11:10:31 -04004694 if (iocbq->vport != vport)
4695 return rc;
4696
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004697 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004698
James Smart495a7142008-06-14 22:52:59 -04004699 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05004700 return rc;
4701
4702 switch (ctx_cmd) {
4703 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04004704 if ((lpfc_cmd->rdata->pnode) &&
4705 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
4706 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05004707 rc = 0;
4708 break;
4709 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04004710 if ((lpfc_cmd->rdata->pnode) &&
4711 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05004712 rc = 0;
4713 break;
dea31012005-04-17 16:05:31 -05004714 case LPFC_CTX_HOST:
4715 rc = 0;
4716 break;
4717 default:
4718 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004719 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05004720 break;
4721 }
4722
4723 return rc;
4724}
4725
James Smarte59058c2008-08-24 21:49:00 -04004726/**
4727 * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
4728 * @vport: Pointer to virtual port.
4729 * @tgt_id: SCSI ID of the target.
4730 * @lun_id: LUN ID of the scsi device.
4731 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4732 *
4733 * This function returns number of FCP commands pending for the vport.
4734 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
4735 * commands pending on the vport associated with SCSI device specified
4736 * by tgt_id and lun_id parameters.
4737 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
4738 * commands pending on the vport associated with SCSI target specified
4739 * by tgt_id parameter.
4740 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
4741 * commands pending on the vport.
4742 * This function returns the number of iocbs which satisfy the filter.
4743 * This function is called without any lock held.
4744 **/
dea31012005-04-17 16:05:31 -05004745int
James Smart51ef4c22007-08-02 11:10:31 -04004746lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
4747 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004748{
James Smart51ef4c22007-08-02 11:10:31 -04004749 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004750 struct lpfc_iocbq *iocbq;
4751 int sum, i;
dea31012005-04-17 16:05:31 -05004752
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004753 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
4754 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004755
James Smart51ef4c22007-08-02 11:10:31 -04004756 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
4757 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004758 sum++;
dea31012005-04-17 16:05:31 -05004759 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004760
dea31012005-04-17 16:05:31 -05004761 return sum;
4762}
4763
James Smarte59058c2008-08-24 21:49:00 -04004764/**
4765 * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
4766 * FCP iocb.
4767 * @phba: Pointer to HBA context object
4768 * @cmdiocb: Pointer to command iocb object.
4769 * @rspiocb: Pointer to response iocb object.
4770 *
4771 * This function is called when an aborted FCP iocb completes. This
4772 * function is called by the ring event handler with no lock held.
4773 * This function frees the iocb.
4774 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004775void
James Smart2e0fef82007-06-17 19:56:36 -05004776lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4777 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004778{
James Bottomley604a3e32005-10-29 10:28:33 -05004779 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004780 return;
4781}
4782
James Smarte59058c2008-08-24 21:49:00 -04004783/**
4784 * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
4785 * pending on a SCSI host(vport)/target/lun.
4786 * @vport: Pointer to virtual port.
4787 * @pring: Pointer to driver SLI ring object.
4788 * @tgt_id: SCSI ID of the target.
4789 * @lun_id: LUN ID of the scsi device.
4790 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4791 *
4792 * This function sends an abort command for every SCSI command
4793 * associated with the given virtual port pending on the ring
4794 * filtered by lpfc_sli_validate_fcp_iocb function.
4795 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
4796 * FCP iocbs associated with lun specified by tgt_id and lun_id
4797 * parameters
4798 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
4799 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
4800 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
4801 * FCP iocbs associated with virtual port.
4802 * This function returns number of iocbs it failed to abort.
4803 * This function is called with no locks held.
4804 **/
dea31012005-04-17 16:05:31 -05004805int
James Smart51ef4c22007-08-02 11:10:31 -04004806lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
4807 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05004808{
James Smart51ef4c22007-08-02 11:10:31 -04004809 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004810 struct lpfc_iocbq *iocbq;
4811 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004812 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004813 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004814 int i;
dea31012005-04-17 16:05:31 -05004815
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004816 for (i = 1; i <= phba->sli.last_iotag; i++) {
4817 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004818
James Smart51ef4c22007-08-02 11:10:31 -04004819 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05004820 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05004821 continue;
4822
4823 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004824 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004825 if (abtsiocb == NULL) {
4826 errcnt++;
4827 continue;
4828 }
dea31012005-04-17 16:05:31 -05004829
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004830 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05004831 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
4832 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
4833 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
4834 abtsiocb->iocb.ulpLe = 1;
4835 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05004836 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05004837
James Smart2e0fef82007-06-17 19:56:36 -05004838 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004839 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
4840 else
4841 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
4842
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004843 /* Setup callback routine and issue the command. */
4844 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05004845 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
4846 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05004847 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05004848 errcnt++;
4849 continue;
4850 }
4851 }
4852
4853 return errcnt;
4854}
4855
James Smarte59058c2008-08-24 21:49:00 -04004856/**
4857 * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
4858 * lpfc_sli_issue_iocb_wait.
4859 * @phba: Pointer to HBA context object.
4860 * @cmdiocbq: Pointer to command iocb.
4861 * @rspiocbq: Pointer to response iocb.
4862 *
4863 * This function is the completion handler for iocbs issued using
4864 * lpfc_sli_issue_iocb_wait function. This function is called by the
4865 * ring event handler function without any lock held. This function
4866 * can be called from both worker thread context and interrupt
4867 * context. This function also can be called from other thread which
4868 * cleans up the SLI layer objects.
4869 * This function copy the contents of the response iocb to the
4870 * response iocb memory object provided by the caller of
4871 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4872 * sleeps for the iocb completion.
4873 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004874static void
4875lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
4876 struct lpfc_iocbq *cmdiocbq,
4877 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05004878{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004879 wait_queue_head_t *pdone_q;
4880 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004881
James Smart2e0fef82007-06-17 19:56:36 -05004882 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004883 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4884 if (cmdiocbq->context2 && rspiocbq)
4885 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4886 &rspiocbq->iocb, sizeof(IOCB_t));
4887
4888 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004889 if (pdone_q)
4890 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05004891 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05004892 return;
4893}
4894
James Smarte59058c2008-08-24 21:49:00 -04004895/**
4896 * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
4897 * @phba: Pointer to HBA context object..
4898 * @pring: Pointer to sli ring.
4899 * @piocb: Pointer to command iocb.
4900 * @prspiocbq: Pointer to response iocb.
4901 * @timeout: Timeout in number of seconds.
4902 *
4903 * This function issues the iocb to firmware and waits for the
4904 * iocb to complete. If the iocb command is not
4905 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
4906 * Caller should not free the iocb resources if this function
4907 * returns IOCB_TIMEDOUT.
4908 * The function waits for the iocb completion using an
4909 * non-interruptible wait.
4910 * This function will sleep while waiting for iocb completion.
4911 * So, this function should not be called from any context which
4912 * does not allow sleeping. Due to the same reason, this function
4913 * cannot be called with interrupt disabled.
4914 * This function assumes that the iocb completions occur while
4915 * this function sleep. So, this function cannot be called from
4916 * the thread which process iocb completion for this ring.
4917 * This function clears the iocb_flag of the iocb object before
4918 * issuing the iocb and the iocb completion handler sets this
4919 * flag and wakes this thread when the iocb completes.
4920 * The contents of the response iocb will be copied to prspiocbq
4921 * by the completion handler when the command completes.
4922 * This function returns IOCB_SUCCESS when success.
4923 * This function is called with no lock held.
4924 **/
dea31012005-04-17 16:05:31 -05004925int
James Smart2e0fef82007-06-17 19:56:36 -05004926lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
4927 struct lpfc_sli_ring *pring,
4928 struct lpfc_iocbq *piocb,
4929 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004930 uint32_t timeout)
dea31012005-04-17 16:05:31 -05004931{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08004932 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004933 long timeleft, timeout_req = 0;
4934 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004935 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05004936
4937 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004938 * If the caller has provided a response iocbq buffer, then context2
4939 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05004940 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004941 if (prspiocbq) {
4942 if (piocb->context2)
4943 return IOCB_ERROR;
4944 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05004945 }
4946
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004947 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
4948 piocb->context_un.wait_queue = &done_q;
4949 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05004950
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004951 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4952 creg_val = readl(phba->HCregaddr);
4953 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
4954 writel(creg_val, phba->HCregaddr);
4955 readl(phba->HCregaddr); /* flush */
4956 }
4957
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004958 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
4959 if (retval == IOCB_SUCCESS) {
4960 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004961 timeleft = wait_event_timeout(done_q,
4962 piocb->iocb_flag & LPFC_IO_WAKE,
4963 timeout_req);
dea31012005-04-17 16:05:31 -05004964
James Smart7054a602007-04-25 09:52:34 -04004965 if (piocb->iocb_flag & LPFC_IO_WAKE) {
4966 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004967 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04004968 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004969 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004970 "0338 IOCB wait timeout error - no "
4971 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004972 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04004973 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004974 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004975 "0330 IOCB wake NOT set, "
4976 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004977 timeout, (timeleft / jiffies));
4978 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05004979 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004980 } else {
4981 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04004982 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004983 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004984 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05004985 }
4986
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004987 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4988 creg_val = readl(phba->HCregaddr);
4989 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
4990 writel(creg_val, phba->HCregaddr);
4991 readl(phba->HCregaddr); /* flush */
4992 }
4993
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004994 if (prspiocbq)
4995 piocb->context2 = NULL;
4996
4997 piocb->context_un.wait_queue = NULL;
4998 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05004999 return retval;
5000}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005001
James Smarte59058c2008-08-24 21:49:00 -04005002/**
5003 * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
5004 * @phba: Pointer to HBA context object.
5005 * @pmboxq: Pointer to driver mailbox object.
5006 * @timeout: Timeout in number of seconds.
5007 *
5008 * This function issues the mailbox to firmware and waits for the
5009 * mailbox command to complete. If the mailbox command is not
5010 * completed within timeout seconds, it returns MBX_TIMEOUT.
5011 * The function waits for the mailbox completion using an
5012 * interruptible wait. If the thread is woken up due to a
5013 * signal, MBX_TIMEOUT error is returned to the caller. Caller
5014 * should not free the mailbox resources, if this function returns
5015 * MBX_TIMEOUT.
5016 * This function will sleep while waiting for mailbox completion.
5017 * So, this function should not be called from any context which
5018 * does not allow sleeping. Due to the same reason, this function
5019 * cannot be called with interrupt disabled.
5020 * This function assumes that the mailbox completion occurs while
5021 * this function sleep. So, this function cannot be called from
5022 * the worker thread which processes mailbox completion.
5023 * This function is called in the context of HBA management
5024 * applications.
5025 * This function returns MBX_SUCCESS when successful.
5026 * This function is called with no lock held.
5027 **/
dea31012005-04-17 16:05:31 -05005028int
James Smart2e0fef82007-06-17 19:56:36 -05005029lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05005030 uint32_t timeout)
5031{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08005032 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05005033 int retval;
James Smart858c9f62007-06-17 19:56:39 -05005034 unsigned long flag;
dea31012005-04-17 16:05:31 -05005035
5036 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04005037 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05005038 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005039
James Smart495a7142008-06-14 22:52:59 -04005040 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05005041 /* setup wake call as IOCB callback */
5042 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
5043 /* setup context field to pass wait_queue pointer to wake function */
5044 pmboxq->context1 = &done_q;
5045
dea31012005-04-17 16:05:31 -05005046 /* now issue the command */
5047 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
5048
5049 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04005050 wait_event_interruptible_timeout(done_q,
5051 pmboxq->mbox_flag & LPFC_MBX_WAKE,
5052 timeout * HZ);
5053
James Smart858c9f62007-06-17 19:56:39 -05005054 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005055 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04005056 /*
5057 * if LPFC_MBX_WAKE flag is set the mailbox is completed
5058 * else do not free the resources.
5059 */
5060 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05005061 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05005062 else {
James Smart7054a602007-04-25 09:52:34 -04005063 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05005064 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5065 }
5066 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005067 }
5068
dea31012005-04-17 16:05:31 -05005069 return retval;
5070}
5071
James Smarte59058c2008-08-24 21:49:00 -04005072/**
5073 * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
5074 * @phba: Pointer to HBA context.
5075 *
5076 * This function is called to cleanup any pending mailbox
5077 * objects in the driver queue before bringing the HBA offline.
5078 * This function is called while resetting the HBA.
5079 * The function is called without any lock held. The function
5080 * takes hbalock to update SLI data structure.
5081 * This function returns 1 when there is an active mailbox
5082 * command pending else returns 0.
5083 **/
James Smartb4c02652006-07-06 15:50:43 -04005084int
5085lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
5086{
James Smart2e0fef82007-06-17 19:56:36 -05005087 struct lpfc_vport *vport = phba->pport;
James Smartb4c02652006-07-06 15:50:43 -04005088 int i = 0;
James Smarted957682007-06-17 19:56:37 -05005089 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04005090
James Smart2e0fef82007-06-17 19:56:36 -05005091 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
James Smartb4c02652006-07-06 15:50:43 -04005092 if (i++ > LPFC_MBOX_TMO * 1000)
5093 return 1;
5094
James Smarted957682007-06-17 19:56:37 -05005095 /*
5096 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
5097 * did finish. This way we won't get the misleading
5098 * "Stray Mailbox Interrupt" message.
5099 */
5100 spin_lock_irq(&phba->hbalock);
5101 ha_copy = phba->work_ha;
5102 phba->work_ha &= ~HA_MBATT;
5103 spin_unlock_irq(&phba->hbalock);
5104
5105 if (ha_copy & HA_MBATT)
5106 if (lpfc_sli_handle_mb_event(phba) == 0)
5107 i = 0;
James Smartb4c02652006-07-06 15:50:43 -04005108
5109 msleep(1);
5110 }
5111
5112 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
5113}
5114
James Smarte59058c2008-08-24 21:49:00 -04005115/**
James Smart93996272008-08-24 21:50:30 -04005116 * lpfc_sli_check_eratt: check error attention events
5117 * @phba: Pointer to HBA context.
5118 *
5119 * This function is called form timer soft interrupt context to check HBA's
5120 * error attention register bit for error attention events.
5121 *
5122 * This fucntion returns 1 when there is Error Attention in the Host Attention
5123 * Register and returns 0 otherwise.
5124 **/
5125int
5126lpfc_sli_check_eratt(struct lpfc_hba *phba)
5127{
5128 uint32_t ha_copy;
5129
James Smarteaf15d52008-12-04 22:39:29 -05005130 /* If PCI channel is offline, don't process it */
5131 if (unlikely(pci_channel_offline(phba->pcidev)))
5132 return 0;
5133
James Smart93996272008-08-24 21:50:30 -04005134 /* If somebody is waiting to handle an eratt, don't process it
5135 * here. The brdkill function will do this.
5136 */
5137 if (phba->link_flag & LS_IGNORE_ERATT)
5138 return 0;
5139
5140 /* Check if interrupt handler handles this ERATT */
5141 spin_lock_irq(&phba->hbalock);
5142 if (phba->hba_flag & HBA_ERATT_HANDLED) {
5143 /* Interrupt handler has handled ERATT */
5144 spin_unlock_irq(&phba->hbalock);
5145 return 0;
5146 }
5147
5148 /* Read chip Host Attention (HA) register */
5149 ha_copy = readl(phba->HAregaddr);
5150 if (ha_copy & HA_ERATT) {
5151 /* Read host status register to retrieve error event */
5152 lpfc_sli_read_hs(phba);
5153 /* Set the driver HA work bitmap */
5154 phba->work_ha |= HA_ERATT;
5155 /* Indicate polling handles this ERATT */
5156 phba->hba_flag |= HBA_ERATT_HANDLED;
5157 spin_unlock_irq(&phba->hbalock);
5158 return 1;
5159 }
5160 spin_unlock_irq(&phba->hbalock);
5161 return 0;
5162}
5163
5164/**
5165 * lpfc_sp_intr_handler: The slow-path interrupt handler of lpfc driver.
James Smarte59058c2008-08-24 21:49:00 -04005166 * @irq: Interrupt number.
5167 * @dev_id: The device context pointer.
5168 *
James Smart93996272008-08-24 21:50:30 -04005169 * This function is directly called from the PCI layer as an interrupt
5170 * service routine when the device is enabled with MSI-X multi-message
5171 * interrupt mode and there are slow-path events in the HBA. However,
5172 * when the device is enabled with either MSI or Pin-IRQ interrupt mode,
5173 * this function is called as part of the device-level interrupt handler.
5174 * When the PCI slot is in error recovery or the HBA is undergoing
5175 * initialization, the interrupt handler will not process the interrupt.
5176 * The link attention and ELS ring attention events are handled by the
5177 * worker thread. The interrupt handler signals the worker thread and
5178 * and returns for these events. This function is called without any
5179 * lock held. It gets the hbalock to access and update SLI data
5180 * structures.
5181 *
5182 * This function returns IRQ_HANDLED when interrupt is handled else it
5183 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04005184 **/
dea31012005-04-17 16:05:31 -05005185irqreturn_t
James Smart93996272008-08-24 21:50:30 -04005186lpfc_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05005187{
James Smart2e0fef82007-06-17 19:56:36 -05005188 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05005189 uint32_t ha_copy;
5190 uint32_t work_ha_copy;
5191 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05005192 unsigned long iflag;
dea31012005-04-17 16:05:31 -05005193 uint32_t control;
5194
James Smart92d7f7b2007-06-17 19:56:38 -05005195 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05005196 struct lpfc_vport *vport;
5197 struct lpfc_nodelist *ndlp;
5198 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05005199 LPFC_MBOXQ_t *pmb;
5200 int rc;
5201
dea31012005-04-17 16:05:31 -05005202 /*
5203 * Get the driver's phba structure from the dev_id and
5204 * assume the HBA is not interrupting.
5205 */
James Smart93996272008-08-24 21:50:30 -04005206 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05005207
5208 if (unlikely(!phba))
5209 return IRQ_NONE;
5210
dea31012005-04-17 16:05:31 -05005211 /*
James Smart93996272008-08-24 21:50:30 -04005212 * Stuff needs to be attented to when this function is invoked as an
5213 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005214 */
James Smart93996272008-08-24 21:50:30 -04005215 if (phba->intr_type == MSIX) {
5216 /* If the pci channel is offline, ignore all the interrupts */
5217 if (unlikely(pci_channel_offline(phba->pcidev)))
5218 return IRQ_NONE;
5219 /* Update device-level interrupt statistics */
5220 phba->sli.slistat.sli_intr++;
5221 /* Ignore all interrupts during initialization. */
5222 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5223 return IRQ_NONE;
5224 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05005225 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04005226 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04005227 /* If somebody is waiting to handle an eratt don't process it
5228 * here. The brdkill function will do this.
5229 */
5230 if (phba->link_flag & LS_IGNORE_ERATT)
5231 ha_copy &= ~HA_ERATT;
5232 /* Check the need for handling ERATT in interrupt handler */
5233 if (ha_copy & HA_ERATT) {
5234 if (phba->hba_flag & HBA_ERATT_HANDLED)
5235 /* ERATT polling has handled ERATT */
5236 ha_copy &= ~HA_ERATT;
5237 else
5238 /* Indicate interrupt handler handles ERATT */
5239 phba->hba_flag |= HBA_ERATT_HANDLED;
5240 }
5241 /* Clear up only attention source related to slow-path */
5242 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
5243 phba->HAregaddr);
5244 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005245 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005246 } else
5247 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05005248
dea31012005-04-17 16:05:31 -05005249 work_ha_copy = ha_copy & phba->work_ha_mask;
5250
James Smart93996272008-08-24 21:50:30 -04005251 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05005252 if (work_ha_copy & HA_LATT) {
5253 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
5254 /*
5255 * Turn off Link Attention interrupts
5256 * until CLEAR_LA done
5257 */
James Smart5b75da22008-12-04 22:39:35 -05005258 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005259 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
5260 control = readl(phba->HCregaddr);
5261 control &= ~HC_LAINT_ENA;
5262 writel(control, phba->HCregaddr);
5263 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005264 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005265 }
5266 else
5267 work_ha_copy &= ~HA_LATT;
5268 }
5269
James Smart93996272008-08-24 21:50:30 -04005270 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05005271 /*
5272 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
5273 * the only slow ring.
5274 */
5275 status = (work_ha_copy &
5276 (HA_RXMASK << (4*LPFC_ELS_RING)));
5277 status >>= (4*LPFC_ELS_RING);
5278 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05005279 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05005280 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04005281
5282 lpfc_debugfs_slow_ring_trc(phba,
5283 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
5284 control, status,
5285 (uint32_t)phba->sli.slistat.sli_intr);
5286
James Smart858c9f62007-06-17 19:56:39 -05005287 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04005288 lpfc_debugfs_slow_ring_trc(phba,
5289 "ISR Disable ring:"
5290 "pwork:x%x hawork:x%x wait:x%x",
5291 phba->work_ha, work_ha_copy,
5292 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005293 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005294
James Smart858c9f62007-06-17 19:56:39 -05005295 control &=
5296 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05005297 writel(control, phba->HCregaddr);
5298 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05005299 }
James Smarta58cbd52007-08-02 11:09:43 -04005300 else {
5301 lpfc_debugfs_slow_ring_trc(phba,
5302 "ISR slow ring: pwork:"
5303 "x%x hawork:x%x wait:x%x",
5304 phba->work_ha, work_ha_copy,
5305 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005306 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005307 }
James Smart5b75da22008-12-04 22:39:35 -05005308 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005309 }
5310 }
James Smart5b75da22008-12-04 22:39:35 -05005311 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005312 if (work_ha_copy & HA_ERATT)
5313 lpfc_sli_read_hs(phba);
5314 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005315 pmb = phba->sli.mbox_active;
5316 pmbox = &pmb->mb;
James Smart34b02dc2008-08-24 21:49:55 -04005317 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05005318 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005319
5320 /* First check out the status word */
5321 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
5322 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05005323 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05005324 /*
5325 * Stray Mailbox Interrupt, mbxCommand <cmd>
5326 * mbxStatus <status>
5327 */
James Smart09372822008-01-11 01:52:54 -05005328 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05005329 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005330 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05005331 "Interrupt mbxCommand x%x "
5332 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005333 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05005334 pmbox->mbxCommand,
5335 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05005336 /* clear mailbox attention bit */
5337 work_ha_copy &= ~HA_MBATT;
5338 } else {
James Smart97eab632008-04-07 10:16:05 -04005339 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05005340 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05005341 phba->last_completion_time = jiffies;
5342 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05005343 if (pmb->mbox_cmpl) {
5344 lpfc_sli_pcimem_bcopy(mbox, pmbox,
5345 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05005346 }
James Smart09372822008-01-11 01:52:54 -05005347 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
5348 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
5349
5350 lpfc_debugfs_disc_trc(vport,
5351 LPFC_DISC_TRC_MBOX_VPORT,
5352 "MBOX dflt rpi: : "
5353 "status:x%x rpi:x%x",
5354 (uint32_t)pmbox->mbxStatus,
5355 pmbox->un.varWords[0], 0);
5356
5357 if (!pmbox->mbxStatus) {
5358 mp = (struct lpfc_dmabuf *)
5359 (pmb->context1);
5360 ndlp = (struct lpfc_nodelist *)
5361 pmb->context2;
5362
5363 /* Reg_LOGIN of dflt RPI was
5364 * successful. new lets get
5365 * rid of the RPI using the
5366 * same mbox buffer.
5367 */
5368 lpfc_unreg_login(phba,
5369 vport->vpi,
5370 pmbox->un.varWords[0],
5371 pmb);
5372 pmb->mbox_cmpl =
5373 lpfc_mbx_cmpl_dflt_rpi;
5374 pmb->context1 = mp;
5375 pmb->context2 = ndlp;
5376 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04005377 rc = lpfc_sli_issue_mbox(phba,
5378 pmb,
5379 MBX_NOWAIT);
5380 if (rc != MBX_BUSY)
5381 lpfc_printf_log(phba,
5382 KERN_ERR,
5383 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04005384 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04005385 "been MBX_BUSY");
James Smart09372822008-01-11 01:52:54 -05005386 goto send_current_mbox;
5387 }
5388 }
James Smart5b75da22008-12-04 22:39:35 -05005389 spin_lock_irqsave(
5390 &phba->pport->work_port_lock,
5391 iflag);
James Smart09372822008-01-11 01:52:54 -05005392 phba->pport->work_port_events &=
5393 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05005394 spin_unlock_irqrestore(
5395 &phba->pport->work_port_lock,
5396 iflag);
James Smart09372822008-01-11 01:52:54 -05005397 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05005398 }
James Smart97eab632008-04-07 10:16:05 -04005399 } else
James Smart5b75da22008-12-04 22:39:35 -05005400 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005401
James Smart92d7f7b2007-06-17 19:56:38 -05005402 if ((work_ha_copy & HA_MBATT) &&
5403 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05005404send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05005405 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04005406 do {
5407 rc = lpfc_sli_issue_mbox(phba, NULL,
5408 MBX_NOWAIT);
5409 } while (rc == MBX_NOT_FINISHED);
5410 if (rc != MBX_SUCCESS)
5411 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
5412 LOG_SLI, "0349 rc should be "
5413 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05005414 }
5415
James Smart5b75da22008-12-04 22:39:35 -05005416 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005417 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05005418 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04005419 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005420 }
James Smart93996272008-08-24 21:50:30 -04005421 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05005422
James Smart93996272008-08-24 21:50:30 -04005423} /* lpfc_sp_intr_handler */
5424
5425/**
5426 * lpfc_fp_intr_handler: The fast-path interrupt handler of lpfc driver.
5427 * @irq: Interrupt number.
5428 * @dev_id: The device context pointer.
5429 *
5430 * This function is directly called from the PCI layer as an interrupt
5431 * service routine when the device is enabled with MSI-X multi-message
5432 * interrupt mode and there is a fast-path FCP IOCB ring event in the
5433 * HBA. However, when the device is enabled with either MSI or Pin-IRQ
5434 * interrupt mode, this function is called as part of the device-level
5435 * interrupt handler. When the PCI slot is in error recovery or the HBA
5436 * is undergoing initialization, the interrupt handler will not process
5437 * the interrupt. The SCSI FCP fast-path ring event are handled in the
5438 * intrrupt context. This function is called without any lock held. It
5439 * gets the hbalock to access and update SLI data structures.
5440 *
5441 * This function returns IRQ_HANDLED when interrupt is handled else it
5442 * returns IRQ_NONE.
5443 **/
5444irqreturn_t
5445lpfc_fp_intr_handler(int irq, void *dev_id)
5446{
5447 struct lpfc_hba *phba;
5448 uint32_t ha_copy;
5449 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05005450 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04005451
5452 /* Get the driver's phba structure from the dev_id and
5453 * assume the HBA is not interrupting.
5454 */
5455 phba = (struct lpfc_hba *) dev_id;
5456
5457 if (unlikely(!phba))
5458 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05005459
5460 /*
James Smart93996272008-08-24 21:50:30 -04005461 * Stuff needs to be attented to when this function is invoked as an
5462 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005463 */
James Smart93996272008-08-24 21:50:30 -04005464 if (phba->intr_type == MSIX) {
5465 /* If pci channel is offline, ignore all the interrupts */
5466 if (unlikely(pci_channel_offline(phba->pcidev)))
5467 return IRQ_NONE;
5468 /* Update device-level interrupt statistics */
5469 phba->sli.slistat.sli_intr++;
5470 /* Ignore all interrupts during initialization. */
5471 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5472 return IRQ_NONE;
5473 /* Need to read HA REG for FCP ring and other ring events */
5474 ha_copy = readl(phba->HAregaddr);
5475 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05005476 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005477 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
5478 phba->HAregaddr);
5479 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005480 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005481 } else
5482 ha_copy = phba->ha_copy;
5483
5484 /*
5485 * Process all events on FCP ring. Take the optimized path for FCP IO.
5486 */
5487 ha_copy &= ~(phba->work_ha_mask);
5488
5489 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05005490 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05005491 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05005492 lpfc_sli_handle_fast_ring_event(phba,
5493 &phba->sli.ring[LPFC_FCP_RING],
5494 status);
James Smarta4bc3372006-12-02 13:34:16 -05005495
5496 if (phba->cfg_multi_ring_support == 2) {
5497 /*
James Smart93996272008-08-24 21:50:30 -04005498 * Process all events on extra ring. Take the optimized path
5499 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05005500 */
James Smart93996272008-08-24 21:50:30 -04005501 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05005502 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05005503 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05005504 lpfc_sli_handle_fast_ring_event(phba,
5505 &phba->sli.ring[LPFC_EXTRA_RING],
5506 status);
5507 }
5508 }
dea31012005-04-17 16:05:31 -05005509 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -04005510} /* lpfc_fp_intr_handler */
dea31012005-04-17 16:05:31 -05005511
James Smart93996272008-08-24 21:50:30 -04005512/**
5513 * lpfc_intr_handler: The device-level interrupt handler of lpfc driver.
5514 * @irq: Interrupt number.
5515 * @dev_id: The device context pointer.
5516 *
5517 * This function is the device-level interrupt handler called from the PCI
5518 * layer when either MSI or Pin-IRQ interrupt mode is enabled and there is
5519 * an event in the HBA which requires driver attention. This function
5520 * invokes the slow-path interrupt attention handling function and fast-path
5521 * interrupt attention handling function in turn to process the relevant
5522 * HBA attention events. This function is called without any lock held. It
5523 * gets the hbalock to access and update SLI data structures.
5524 *
5525 * This function returns IRQ_HANDLED when interrupt is handled, else it
5526 * returns IRQ_NONE.
5527 **/
5528irqreturn_t
5529lpfc_intr_handler(int irq, void *dev_id)
5530{
5531 struct lpfc_hba *phba;
5532 irqreturn_t sp_irq_rc, fp_irq_rc;
5533 unsigned long status1, status2;
5534
5535 /*
5536 * Get the driver's phba structure from the dev_id and
5537 * assume the HBA is not interrupting.
5538 */
5539 phba = (struct lpfc_hba *) dev_id;
5540
5541 if (unlikely(!phba))
5542 return IRQ_NONE;
5543
5544 /* If the pci channel is offline, ignore all the interrupts. */
5545 if (unlikely(pci_channel_offline(phba->pcidev)))
5546 return IRQ_NONE;
5547
5548 /* Update device level interrupt statistics */
5549 phba->sli.slistat.sli_intr++;
5550
5551 /* Ignore all interrupts during initialization. */
5552 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5553 return IRQ_NONE;
5554
5555 spin_lock(&phba->hbalock);
5556 phba->ha_copy = readl(phba->HAregaddr);
5557 if (unlikely(!phba->ha_copy)) {
5558 spin_unlock(&phba->hbalock);
5559 return IRQ_NONE;
5560 } else if (phba->ha_copy & HA_ERATT) {
5561 if (phba->hba_flag & HBA_ERATT_HANDLED)
5562 /* ERATT polling has handled ERATT */
5563 phba->ha_copy &= ~HA_ERATT;
5564 else
5565 /* Indicate interrupt handler handles ERATT */
5566 phba->hba_flag |= HBA_ERATT_HANDLED;
5567 }
5568
5569 /* Clear attention sources except link and error attentions */
5570 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
5571 readl(phba->HAregaddr); /* flush */
5572 spin_unlock(&phba->hbalock);
5573
5574 /*
5575 * Invokes slow-path host attention interrupt handling as appropriate.
5576 */
5577
5578 /* status of events with mailbox and link attention */
5579 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
5580
5581 /* status of events with ELS ring */
5582 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
5583 status2 >>= (4*LPFC_ELS_RING);
5584
5585 if (status1 || (status2 & HA_RXMASK))
5586 sp_irq_rc = lpfc_sp_intr_handler(irq, dev_id);
5587 else
5588 sp_irq_rc = IRQ_NONE;
5589
5590 /*
5591 * Invoke fast-path host attention interrupt handling as appropriate.
5592 */
5593
5594 /* status of events with FCP ring */
5595 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
5596 status1 >>= (4*LPFC_FCP_RING);
5597
5598 /* status of events with extra ring */
5599 if (phba->cfg_multi_ring_support == 2) {
5600 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
5601 status2 >>= (4*LPFC_EXTRA_RING);
5602 } else
5603 status2 = 0;
5604
5605 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
5606 fp_irq_rc = lpfc_fp_intr_handler(irq, dev_id);
5607 else
5608 fp_irq_rc = IRQ_NONE;
5609
5610 /* Return device-level interrupt handling status */
5611 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
5612} /* lpfc_intr_handler */