blob: 4e5b4ee121f13da94f5016d81d05e47a3664d6b3 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart1b32f6a2008-02-08 18:49:39 -05004 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040031#include <scsi/scsi_transport_fc.h>
dea31012005-04-17 16:05:31 -050032
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
James Smartea2151b2008-09-07 11:52:10 -040035#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_disc.h"
37#include "lpfc_scsi.h"
38#include "lpfc.h"
39#include "lpfc_crtn.h"
40#include "lpfc_logmsg.h"
41#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050042#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050043
44/*
45 * Define macro to log: Mailbox command x%x cannot issue Data
46 * This allows multiple uses of lpfc_msgBlk0311
47 * w/o perturbing log msg utility.
48 */
James Smart92d7f7b2007-06-17 19:56:38 -050049#define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
dea31012005-04-17 16:05:31 -050050 lpfc_printf_log(phba, \
51 KERN_INFO, \
52 LOG_MBOX | LOG_SLI, \
James Smarte8b62012007-08-02 11:10:09 -040053 "(%d):0311 Mailbox command x%x cannot " \
James Smart92d7f7b2007-06-17 19:56:38 -050054 "issue Data: x%x x%x x%x\n", \
James Smart92d7f7b2007-06-17 19:56:38 -050055 pmbox->vport ? pmbox->vport->vpi : 0, \
56 pmbox->mb.mbxCommand, \
James Smart2e0fef82007-06-17 19:56:36 -050057 phba->pport->port_state, \
dea31012005-04-17 16:05:31 -050058 psli->sli_flag, \
James Smart2e0fef82007-06-17 19:56:36 -050059 flag)
dea31012005-04-17 16:05:31 -050060
61
62/* There are only four IOCB completion types. */
63typedef enum _lpfc_iocb_type {
64 LPFC_UNKNOWN_IOCB,
65 LPFC_UNSOL_IOCB,
66 LPFC_SOL_IOCB,
67 LPFC_ABORT_IOCB
68} lpfc_iocb_type;
69
James Smarte59058c2008-08-24 21:49:00 -040070/**
71 * lpfc_cmd_iocb: Get next command iocb entry in the ring.
72 * @phba: Pointer to HBA context object.
73 * @pring: Pointer to driver SLI ring object.
74 *
75 * This function returns pointer to next command iocb entry
76 * in the command ring. The caller must hold hbalock to prevent
77 * other threads consume the next command iocb.
78 * SLI-2/SLI-3 provide different sized iocbs.
79 **/
James Smarted957682007-06-17 19:56:37 -050080static inline IOCB_t *
81lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
82{
83 return (IOCB_t *) (((char *) pring->cmdringaddr) +
84 pring->cmdidx * phba->iocb_cmd_size);
85}
86
James Smarte59058c2008-08-24 21:49:00 -040087/**
88 * lpfc_resp_iocb: Get next response iocb entry in the ring.
89 * @phba: Pointer to HBA context object.
90 * @pring: Pointer to driver SLI ring object.
91 *
92 * This function returns pointer to next response iocb entry
93 * in the response ring. The caller must hold hbalock to make sure
94 * that no other thread consume the next response iocb.
95 * SLI-2/SLI-3 provide different sized iocbs.
96 **/
James Smarted957682007-06-17 19:56:37 -050097static inline IOCB_t *
98lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
99{
100 return (IOCB_t *) (((char *) pring->rspringaddr) +
101 pring->rspidx * phba->iocb_rsp_size);
102}
103
James Smarte59058c2008-08-24 21:49:00 -0400104/**
105 * __lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
106 * @phba: Pointer to HBA context object.
107 *
108 * This function is called with hbalock held. This function
109 * allocates a new driver iocb object from the iocb pool. If the
110 * allocation is successful, it returns pointer to the newly
111 * allocated iocb object else it returns NULL.
112 **/
James Smart2e0fef82007-06-17 19:56:36 -0500113static struct lpfc_iocbq *
114__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400115{
116 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
117 struct lpfc_iocbq * iocbq = NULL;
118
119 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
120 return iocbq;
121}
122
James Smarte59058c2008-08-24 21:49:00 -0400123/**
124 * lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
125 * @phba: Pointer to HBA context object.
126 *
127 * This function is called with no lock held. This function
128 * allocates a new driver iocb object from the iocb pool. If the
129 * allocation is successful, it returns pointer to the newly
130 * allocated iocb object else it returns NULL.
131 **/
James Smart2e0fef82007-06-17 19:56:36 -0500132struct lpfc_iocbq *
133lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500134{
James Smart2e0fef82007-06-17 19:56:36 -0500135 struct lpfc_iocbq * iocbq = NULL;
136 unsigned long iflags;
137
138 spin_lock_irqsave(&phba->hbalock, iflags);
139 iocbq = __lpfc_sli_get_iocbq(phba);
140 spin_unlock_irqrestore(&phba->hbalock, iflags);
141 return iocbq;
142}
143
James Smarte59058c2008-08-24 21:49:00 -0400144/**
145 * __lpfc_sli_release_iocbq: Release iocb to the iocb pool.
146 * @phba: Pointer to HBA context object.
147 * @iocbq: Pointer to driver iocb object.
148 *
149 * This function is called with hbalock held to release driver
150 * iocb object to the iocb pool. The iotag in the iocb object
151 * does not change for each use of the iocb object. This function
152 * clears all other fields of the iocb object when it is freed.
153 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100154static void
James Smart2e0fef82007-06-17 19:56:36 -0500155__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
156{
157 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500158
159 /*
160 * Clean all volatile data fields, preserve iotag and node struct.
161 */
162 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
163 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
164}
165
James Smarte59058c2008-08-24 21:49:00 -0400166/**
167 * lpfc_sli_release_iocbq: Release iocb to the iocb pool.
168 * @phba: Pointer to HBA context object.
169 * @iocbq: Pointer to driver iocb object.
170 *
171 * This function is called with no lock held to release the iocb to
172 * iocb pool.
173 **/
James Smart2e0fef82007-06-17 19:56:36 -0500174void
175lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
176{
177 unsigned long iflags;
178
179 /*
180 * Clean all volatile data fields, preserve iotag and node struct.
181 */
182 spin_lock_irqsave(&phba->hbalock, iflags);
183 __lpfc_sli_release_iocbq(phba, iocbq);
184 spin_unlock_irqrestore(&phba->hbalock, iflags);
185}
186
James Smarte59058c2008-08-24 21:49:00 -0400187/**
188 * lpfc_sli_iocb_cmd_type: Get the iocb type.
189 * @iocb_cmnd : iocb command code.
190 *
191 * This function is called by ring event handler function to get the iocb type.
192 * This function translates the iocb command to an iocb command type used to
193 * decide the final disposition of each completed IOCB.
194 * The function returns
195 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
196 * LPFC_SOL_IOCB if it is a solicited iocb completion
197 * LPFC_ABORT_IOCB if it is an abort iocb
198 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
199 *
200 * The caller is not required to hold any lock.
201 **/
dea31012005-04-17 16:05:31 -0500202static lpfc_iocb_type
203lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
204{
205 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
206
207 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
208 return 0;
209
210 switch (iocb_cmnd) {
211 case CMD_XMIT_SEQUENCE_CR:
212 case CMD_XMIT_SEQUENCE_CX:
213 case CMD_XMIT_BCAST_CN:
214 case CMD_XMIT_BCAST_CX:
215 case CMD_ELS_REQUEST_CR:
216 case CMD_ELS_REQUEST_CX:
217 case CMD_CREATE_XRI_CR:
218 case CMD_CREATE_XRI_CX:
219 case CMD_GET_RPI_CN:
220 case CMD_XMIT_ELS_RSP_CX:
221 case CMD_GET_RPI_CR:
222 case CMD_FCP_IWRITE_CR:
223 case CMD_FCP_IWRITE_CX:
224 case CMD_FCP_IREAD_CR:
225 case CMD_FCP_IREAD_CX:
226 case CMD_FCP_ICMND_CR:
227 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500228 case CMD_FCP_TSEND_CX:
229 case CMD_FCP_TRSP_CX:
230 case CMD_FCP_TRECEIVE_CX:
231 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500232 case CMD_ADAPTER_MSG:
233 case CMD_ADAPTER_DUMP:
234 case CMD_XMIT_SEQUENCE64_CR:
235 case CMD_XMIT_SEQUENCE64_CX:
236 case CMD_XMIT_BCAST64_CN:
237 case CMD_XMIT_BCAST64_CX:
238 case CMD_ELS_REQUEST64_CR:
239 case CMD_ELS_REQUEST64_CX:
240 case CMD_FCP_IWRITE64_CR:
241 case CMD_FCP_IWRITE64_CX:
242 case CMD_FCP_IREAD64_CR:
243 case CMD_FCP_IREAD64_CX:
244 case CMD_FCP_ICMND64_CR:
245 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500246 case CMD_FCP_TSEND64_CX:
247 case CMD_FCP_TRSP64_CX:
248 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500249 case CMD_GEN_REQUEST64_CR:
250 case CMD_GEN_REQUEST64_CX:
251 case CMD_XMIT_ELS_RSP64_CX:
252 type = LPFC_SOL_IOCB;
253 break;
254 case CMD_ABORT_XRI_CN:
255 case CMD_ABORT_XRI_CX:
256 case CMD_CLOSE_XRI_CN:
257 case CMD_CLOSE_XRI_CX:
258 case CMD_XRI_ABORTED_CX:
259 case CMD_ABORT_MXRI64_CN:
260 type = LPFC_ABORT_IOCB;
261 break;
262 case CMD_RCV_SEQUENCE_CX:
263 case CMD_RCV_ELS_REQ_CX:
264 case CMD_RCV_SEQUENCE64_CX:
265 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400266 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500267 case CMD_IOCB_RCV_SEQ64_CX:
268 case CMD_IOCB_RCV_ELS64_CX:
269 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500270 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500271 type = LPFC_UNSOL_IOCB;
272 break;
James Smart3163f722008-02-08 18:50:25 -0500273 case CMD_IOCB_XMIT_MSEQ64_CR:
274 case CMD_IOCB_XMIT_MSEQ64_CX:
275 case CMD_IOCB_RCV_SEQ_LIST64_CX:
276 case CMD_IOCB_RCV_ELS_LIST64_CX:
277 case CMD_IOCB_CLOSE_EXTENDED_CN:
278 case CMD_IOCB_ABORT_EXTENDED_CN:
279 case CMD_IOCB_RET_HBQE64_CN:
280 case CMD_IOCB_FCP_IBIDIR64_CR:
281 case CMD_IOCB_FCP_IBIDIR64_CX:
282 case CMD_IOCB_FCP_ITASKMGT64_CX:
283 case CMD_IOCB_LOGENTRY_CN:
284 case CMD_IOCB_LOGENTRY_ASYNC_CN:
285 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700286 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500287 type = LPFC_UNKNOWN_IOCB;
288 break;
dea31012005-04-17 16:05:31 -0500289 default:
290 type = LPFC_UNKNOWN_IOCB;
291 break;
292 }
293
294 return type;
295}
296
James Smarte59058c2008-08-24 21:49:00 -0400297/**
298 * lpfc_sli_ring_map: Issue config_ring mbox for all rings.
299 * @phba: Pointer to HBA context object.
300 *
301 * This function is called from SLI initialization code
302 * to configure every ring of the HBA's SLI interface. The
303 * caller is not required to hold any lock. This function issues
304 * a config_ring mailbox command for each ring.
305 * This function returns zero if successful else returns a negative
306 * error code.
307 **/
dea31012005-04-17 16:05:31 -0500308static int
James Smarted957682007-06-17 19:56:37 -0500309lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500310{
311 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500312 LPFC_MBOXQ_t *pmb;
313 MAILBOX_t *pmbox;
314 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500315
James Smarted957682007-06-17 19:56:37 -0500316 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
317 if (!pmb)
318 return -ENOMEM;
319 pmbox = &pmb->mb;
320 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500321 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500322 lpfc_config_ring(phba, i, pmb);
323 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
324 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400326 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500327 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
328 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400329 rc, pmbox->mbxCommand,
330 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500331 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500332 ret = -ENXIO;
333 break;
dea31012005-04-17 16:05:31 -0500334 }
335 }
James Smarted957682007-06-17 19:56:37 -0500336 mempool_free(pmb, phba->mbox_mem_pool);
337 return ret;
dea31012005-04-17 16:05:31 -0500338}
339
James Smarte59058c2008-08-24 21:49:00 -0400340/**
341 * lpfc_sli_ringtxcmpl_put: Adds new iocb to the txcmplq.
342 * @phba: Pointer to HBA context object.
343 * @pring: Pointer to driver SLI ring object.
344 * @piocb: Pointer to the driver iocb object.
345 *
346 * This function is called with hbalock held. The function adds the
347 * new iocb to txcmplq of the given ring. This function always returns
348 * 0. If this function is called for ELS ring, this function checks if
349 * there is a vport associated with the ELS command. This function also
350 * starts els_tmofunc timer if this is an ELS command.
351 **/
dea31012005-04-17 16:05:31 -0500352static int
James Smart2e0fef82007-06-17 19:56:36 -0500353lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
354 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500355{
dea31012005-04-17 16:05:31 -0500356 list_add_tail(&piocb->list, &pring->txcmplq);
357 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500358 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
359 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
360 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
361 if (!piocb->vport)
362 BUG();
363 else
364 mod_timer(&piocb->vport->els_tmofunc,
365 jiffies + HZ * (phba->fc_ratov << 1));
366 }
367
dea31012005-04-17 16:05:31 -0500368
James Smart2e0fef82007-06-17 19:56:36 -0500369 return 0;
dea31012005-04-17 16:05:31 -0500370}
371
James Smarte59058c2008-08-24 21:49:00 -0400372/**
373 * lpfc_sli_ringtx_get: Get first element of the txq.
374 * @phba: Pointer to HBA context object.
375 * @pring: Pointer to driver SLI ring object.
376 *
377 * This function is called with hbalock held to get next
378 * iocb in txq of the given ring. If there is any iocb in
379 * the txq, the function returns first iocb in the list after
380 * removing the iocb from the list, else it returns NULL.
381 **/
dea31012005-04-17 16:05:31 -0500382static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500383lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500384{
dea31012005-04-17 16:05:31 -0500385 struct lpfc_iocbq *cmd_iocb;
386
James Smart858c9f62007-06-17 19:56:39 -0500387 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
388 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500389 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500390 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500391}
392
James Smarte59058c2008-08-24 21:49:00 -0400393/**
394 * lpfc_sli_next_iocb_slot: Get next iocb slot in the ring.
395 * @phba: Pointer to HBA context object.
396 * @pring: Pointer to driver SLI ring object.
397 *
398 * This function is called with hbalock held and the caller must post the
399 * iocb without releasing the lock. If the caller releases the lock,
400 * iocb slot returned by the function is not guaranteed to be available.
401 * The function returns pointer to the next available iocb slot if there
402 * is available slot in the ring, else it returns NULL.
403 * If the get index of the ring is ahead of the put index, the function
404 * will post an error attention event to the worker thread to take the
405 * HBA to offline state.
406 **/
dea31012005-04-17 16:05:31 -0500407static IOCB_t *
408lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
409{
James Smart34b02dc2008-08-24 21:49:55 -0400410 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500411 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500412 if ((pring->next_cmdidx == pring->cmdidx) &&
413 (++pring->next_cmdidx >= max_cmd_idx))
414 pring->next_cmdidx = 0;
415
416 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
417
418 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
419
420 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
421 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400422 "0315 Ring %d issue: portCmdGet %d "
dea31012005-04-17 16:05:31 -0500423 "is bigger then cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400424 pring->ringno,
dea31012005-04-17 16:05:31 -0500425 pring->local_getidx, max_cmd_idx);
426
James Smart2e0fef82007-06-17 19:56:36 -0500427 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500428 /*
429 * All error attention handlers are posted to
430 * worker thread
431 */
432 phba->work_ha |= HA_ERATT;
433 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500434
James Smart5e9d9b82008-06-14 22:52:53 -0400435 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500436
437 return NULL;
438 }
439
440 if (pring->local_getidx == pring->next_cmdidx)
441 return NULL;
442 }
443
James Smarted957682007-06-17 19:56:37 -0500444 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500445}
446
James Smarte59058c2008-08-24 21:49:00 -0400447/**
448 * lpfc_sli_next_iotag: Get an iotag for the iocb.
449 * @phba: Pointer to HBA context object.
450 * @iocbq: Pointer to driver iocb object.
451 *
452 * This function gets an iotag for the iocb. If there is no unused iotag and
453 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
454 * array and assigns a new iotag.
455 * The function returns the allocated iotag if successful, else returns zero.
456 * Zero is not a valid iotag.
457 * The caller is not required to hold any lock.
458 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500459uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500460lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500461{
James Smart2e0fef82007-06-17 19:56:36 -0500462 struct lpfc_iocbq **new_arr;
463 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500464 size_t new_len;
465 struct lpfc_sli *psli = &phba->sli;
466 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500467
James Smart2e0fef82007-06-17 19:56:36 -0500468 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500469 iotag = psli->last_iotag;
470 if(++iotag < psli->iocbq_lookup_len) {
471 psli->last_iotag = iotag;
472 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500473 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500474 iocbq->iotag = iotag;
475 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500476 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500477 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
478 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500479 spin_unlock_irq(&phba->hbalock);
480 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500481 GFP_KERNEL);
482 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500483 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500484 old_arr = psli->iocbq_lookup;
485 if (new_len <= psli->iocbq_lookup_len) {
486 /* highly unprobable case */
487 kfree(new_arr);
488 iotag = psli->last_iotag;
489 if(++iotag < psli->iocbq_lookup_len) {
490 psli->last_iotag = iotag;
491 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500492 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500493 iocbq->iotag = iotag;
494 return iotag;
495 }
James Smart2e0fef82007-06-17 19:56:36 -0500496 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500497 return 0;
498 }
499 if (psli->iocbq_lookup)
500 memcpy(new_arr, old_arr,
501 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -0400502 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -0500503 psli->iocbq_lookup = new_arr;
504 psli->iocbq_lookup_len = new_len;
505 psli->last_iotag = iotag;
506 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500507 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500508 iocbq->iotag = iotag;
509 kfree(old_arr);
510 return iotag;
511 }
James Smart8f6d98d2006-08-01 07:34:00 -0400512 } else
James Smart2e0fef82007-06-17 19:56:36 -0500513 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500514
James Bottomley604a3e32005-10-29 10:28:33 -0500515 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400516 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
517 psli->last_iotag);
dea31012005-04-17 16:05:31 -0500518
James Bottomley604a3e32005-10-29 10:28:33 -0500519 return 0;
dea31012005-04-17 16:05:31 -0500520}
521
James Smarte59058c2008-08-24 21:49:00 -0400522/**
523 * lpfc_sli_submit_iocb: Submit an iocb to the firmware.
524 * @phba: Pointer to HBA context object.
525 * @pring: Pointer to driver SLI ring object.
526 * @iocb: Pointer to iocb slot in the ring.
527 * @nextiocb: Pointer to driver iocb object which need to be
528 * posted to firmware.
529 *
530 * This function is called with hbalock held to post a new iocb to
531 * the firmware. This function copies the new iocb to ring iocb slot and
532 * updates the ring pointers. It adds the new iocb to txcmplq if there is
533 * a completion call back for this iocb else the function will free the
534 * iocb object.
535 **/
dea31012005-04-17 16:05:31 -0500536static void
537lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
538 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
539{
540 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500541 * Set up an iotag
dea31012005-04-17 16:05:31 -0500542 */
James Bottomley604a3e32005-10-29 10:28:33 -0500543 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500544
James Smarta58cbd52007-08-02 11:09:43 -0400545 if (pring->ringno == LPFC_ELS_RING) {
546 lpfc_debugfs_slow_ring_trc(phba,
547 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
548 *(((uint32_t *) &nextiocb->iocb) + 4),
549 *(((uint32_t *) &nextiocb->iocb) + 6),
550 *(((uint32_t *) &nextiocb->iocb) + 7));
551 }
552
dea31012005-04-17 16:05:31 -0500553 /*
554 * Issue iocb command to adapter
555 */
James Smart92d7f7b2007-06-17 19:56:38 -0500556 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500557 wmb();
558 pring->stats.iocb_cmd++;
559
560 /*
561 * If there is no completion routine to call, we can release the
562 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
563 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
564 */
565 if (nextiocb->iocb_cmpl)
566 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500567 else
James Smart2e0fef82007-06-17 19:56:36 -0500568 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500569
570 /*
571 * Let the HBA know what IOCB slot will be the next one the
572 * driver will put a command into.
573 */
574 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500575 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500576}
577
James Smarte59058c2008-08-24 21:49:00 -0400578/**
579 * lpfc_sli_update_full_ring: Update the chip attention register.
580 * @phba: Pointer to HBA context object.
581 * @pring: Pointer to driver SLI ring object.
582 *
583 * The caller is not required to hold any lock for calling this function.
584 * This function updates the chip attention bits for the ring to inform firmware
585 * that there are pending work to be done for this ring and requests an
586 * interrupt when there is space available in the ring. This function is
587 * called when the driver is unable to post more iocbs to the ring due
588 * to unavailability of space in the ring.
589 **/
dea31012005-04-17 16:05:31 -0500590static void
James Smart2e0fef82007-06-17 19:56:36 -0500591lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500592{
593 int ringno = pring->ringno;
594
595 pring->flag |= LPFC_CALL_RING_AVAILABLE;
596
597 wmb();
598
599 /*
600 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
601 * The HBA will tell us when an IOCB entry is available.
602 */
603 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
604 readl(phba->CAregaddr); /* flush */
605
606 pring->stats.iocb_cmd_full++;
607}
608
James Smarte59058c2008-08-24 21:49:00 -0400609/**
610 * lpfc_sli_update_ring: Update chip attention register.
611 * @phba: Pointer to HBA context object.
612 * @pring: Pointer to driver SLI ring object.
613 *
614 * This function updates the chip attention register bit for the
615 * given ring to inform HBA that there is more work to be done
616 * in this ring. The caller is not required to hold any lock.
617 **/
dea31012005-04-17 16:05:31 -0500618static void
James Smart2e0fef82007-06-17 19:56:36 -0500619lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500620{
621 int ringno = pring->ringno;
622
623 /*
624 * Tell the HBA that there is work to do in this ring.
625 */
James Smart34b02dc2008-08-24 21:49:55 -0400626 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
627 wmb();
628 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
629 readl(phba->CAregaddr); /* flush */
630 }
dea31012005-04-17 16:05:31 -0500631}
632
James Smarte59058c2008-08-24 21:49:00 -0400633/**
634 * lpfc_sli_resume_iocb: Process iocbs in the txq.
635 * @phba: Pointer to HBA context object.
636 * @pring: Pointer to driver SLI ring object.
637 *
638 * This function is called with hbalock held to post pending iocbs
639 * in the txq to the firmware. This function is called when driver
640 * detects space available in the ring.
641 **/
dea31012005-04-17 16:05:31 -0500642static void
James Smart2e0fef82007-06-17 19:56:36 -0500643lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500644{
645 IOCB_t *iocb;
646 struct lpfc_iocbq *nextiocb;
647
648 /*
649 * Check to see if:
650 * (a) there is anything on the txq to send
651 * (b) link is up
652 * (c) link attention events can be processed (fcp ring only)
653 * (d) IOCB processing is not blocked by the outstanding mbox command.
654 */
655 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500656 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500657 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -0400658 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -0500659
660 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
661 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
662 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
663
664 if (iocb)
665 lpfc_sli_update_ring(phba, pring);
666 else
667 lpfc_sli_update_full_ring(phba, pring);
668 }
669
670 return;
671}
672
James Smarte59058c2008-08-24 21:49:00 -0400673/**
674 * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
675 * @phba: Pointer to HBA context object.
676 * @hbqno: HBQ number.
677 *
678 * This function is called with hbalock held to get the next
679 * available slot for the given HBQ. If there is free slot
680 * available for the HBQ it will return pointer to the next available
681 * HBQ entry else it will return NULL.
682 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100683static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500684lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
685{
686 struct hbq_s *hbqp = &phba->hbqs[hbqno];
687
688 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
689 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
690 hbqp->next_hbqPutIdx = 0;
691
692 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500693 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500694 uint32_t getidx = le32_to_cpu(raw_index);
695
696 hbqp->local_hbqGetIdx = getidx;
697
698 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
699 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500700 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400701 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500702 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400703 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500704 hbqp->entry_count);
705
706 phba->link_state = LPFC_HBA_ERROR;
707 return NULL;
708 }
709
710 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
711 return NULL;
712 }
713
James Smart51ef4c22007-08-02 11:10:31 -0400714 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
715 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500716}
717
James Smarte59058c2008-08-24 21:49:00 -0400718/**
719 * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
720 * @phba: Pointer to HBA context object.
721 *
722 * This function is called with no lock held to free all the
723 * hbq buffers while uninitializing the SLI interface. It also
724 * frees the HBQ buffers returned by the firmware but not yet
725 * processed by the upper layers.
726 **/
James Smarted957682007-06-17 19:56:37 -0500727void
728lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
729{
James Smart92d7f7b2007-06-17 19:56:38 -0500730 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
731 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -0500732 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -0400733 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -0500734 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500735
James Smart51ef4c22007-08-02 11:10:31 -0400736 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500737 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -0500738 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -0400739 for (i = 0; i < hbq_count; ++i) {
740 list_for_each_entry_safe(dmabuf, next_dmabuf,
741 &phba->hbqs[i].hbq_buffer_list, list) {
742 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
743 list_del(&hbq_buf->dbuf.list);
744 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
745 }
James Smarta8adb832007-10-27 13:37:53 -0400746 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -0500747 }
James Smart3163f722008-02-08 18:50:25 -0500748 /* Return all HBQ buffer that are in-fly */
749 list_for_each_entry_safe(dmabuf, next_dmabuf,
750 &phba->hbqbuf_in_list, list) {
751 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
752 list_del(&hbq_buf->dbuf.list);
753 if (hbq_buf->tag == -1) {
754 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
755 (phba, hbq_buf);
756 } else {
757 hbqno = hbq_buf->tag >> 16;
758 if (hbqno >= LPFC_MAX_HBQS)
759 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
760 (phba, hbq_buf);
761 else
762 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
763 hbq_buf);
764 }
765 }
766
767 /* Mark the HBQs not in use */
768 phba->hbq_in_use = 0;
769 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -0500770}
771
James Smarte59058c2008-08-24 21:49:00 -0400772/**
773 * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
774 * @phba: Pointer to HBA context object.
775 * @hbqno: HBQ number.
776 * @hbq_buf: Pointer to HBQ buffer.
777 *
778 * This function is called with the hbalock held to post a
779 * hbq buffer to the firmware. If the function finds an empty
780 * slot in the HBQ, it will post the buffer. The function will return
781 * pointer to the hbq entry if it successfully post the buffer
782 * else it will return NULL.
783 **/
James Smart51ef4c22007-08-02 11:10:31 -0400784static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500785lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500786 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500787{
788 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500789 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500790
791 /* Get next HBQ entry slot to use */
792 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
793 if (hbqe) {
794 struct hbq_s *hbqp = &phba->hbqs[hbqno];
795
James Smart92d7f7b2007-06-17 19:56:38 -0500796 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
797 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400798 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500799 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500800 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
801 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
802 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500803 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
804 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500805 /* flush */
James Smarted957682007-06-17 19:56:37 -0500806 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400807 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smarted957682007-06-17 19:56:37 -0500808 }
James Smart51ef4c22007-08-02 11:10:31 -0400809 return hbqe;
James Smarted957682007-06-17 19:56:37 -0500810}
811
James Smarte59058c2008-08-24 21:49:00 -0400812/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -0500813static struct lpfc_hbq_init lpfc_els_hbq = {
814 .rn = 1,
815 .entry_count = 200,
816 .mask_count = 0,
817 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400818 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500819 .buffer_count = 0,
820 .init_count = 20,
821 .add_count = 5,
822};
James Smarted957682007-06-17 19:56:37 -0500823
James Smarte59058c2008-08-24 21:49:00 -0400824/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -0400825static struct lpfc_hbq_init lpfc_extra_hbq = {
826 .rn = 1,
827 .entry_count = 200,
828 .mask_count = 0,
829 .profile = 0,
830 .ring_mask = (1 << LPFC_EXTRA_RING),
831 .buffer_count = 0,
832 .init_count = 0,
833 .add_count = 5,
834};
835
James Smarte59058c2008-08-24 21:49:00 -0400836/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -0400837struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500838 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400839 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500840};
841
James Smarte59058c2008-08-24 21:49:00 -0400842/**
843 * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
844 * @phba: Pointer to HBA context object.
845 * @hbqno: HBQ number.
846 * @count: Number of HBQ buffers to be posted.
847 *
James Smartd7c255b2008-08-24 21:50:00 -0400848 * This function is called with no lock held to post more hbq buffers to the
849 * given HBQ. The function returns the number of HBQ buffers successfully
850 * posted.
James Smarte59058c2008-08-24 21:49:00 -0400851 **/
James Smart311464e2007-08-02 11:10:37 -0400852static int
James Smart92d7f7b2007-06-17 19:56:38 -0500853lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
854{
James Smartd7c255b2008-08-24 21:50:00 -0400855 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -0500856 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500857 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -0400858 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700859 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -0400860 return 0;
James Smart51ef4c22007-08-02 11:10:31 -0400861
James Smartd7c255b2008-08-24 21:50:00 -0400862 if ((phba->hbqs[hbqno].buffer_count + count) >
863 lpfc_hbq_defs[hbqno]->entry_count)
864 count = lpfc_hbq_defs[hbqno]->entry_count -
865 phba->hbqs[hbqno].buffer_count;
866 if (!count)
867 return 0;
868 /* Allocate HBQ entries */
869 for (i = 0; i < count; i++) {
870 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
871 if (!hbq_buffer)
872 break;
873 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
874 }
James Smart3163f722008-02-08 18:50:25 -0500875 /* Check whether HBQ is still in use */
876 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700877 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -0400878 goto err;
879 while (!list_empty(&hbq_buf_list)) {
880 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
881 dbuf.list);
882 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
883 (hbqno << 16));
884 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -0400885 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -0400886 posted++;
887 } else
James Smart51ef4c22007-08-02 11:10:31 -0400888 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -0500889 }
James Smart3163f722008-02-08 18:50:25 -0500890 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -0400891 return posted;
892err:
893 spin_unlock_irqrestore(&phba->hbalock, flags);
894 while (!list_empty(&hbq_buf_list)) {
895 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
896 dbuf.list);
897 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
898 }
James Smart92d7f7b2007-06-17 19:56:38 -0500899 return 0;
James Smarted957682007-06-17 19:56:37 -0500900}
901
James Smarte59058c2008-08-24 21:49:00 -0400902/**
903 * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
904 * @phba: Pointer to HBA context object.
905 * @qno: HBQ number.
906 *
907 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -0400908 * is called with no lock held. The function returns the number of HBQ entries
909 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400910 **/
James Smarted957682007-06-17 19:56:37 -0500911int
James Smart92d7f7b2007-06-17 19:56:38 -0500912lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500913{
James Smart92d7f7b2007-06-17 19:56:38 -0500914 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
915 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -0500916}
917
James Smarte59058c2008-08-24 21:49:00 -0400918/**
919 * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
920 * @phba: Pointer to HBA context object.
921 * @qno: HBQ queue number.
922 *
923 * This function is called from SLI initialization code path with
924 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -0400925 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400926 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100927static int
James Smart92d7f7b2007-06-17 19:56:38 -0500928lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500929{
James Smart92d7f7b2007-06-17 19:56:38 -0500930 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
931 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -0500932}
933
James Smarte59058c2008-08-24 21:49:00 -0400934/**
935 * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
936 * @phba: Pointer to HBA context object.
937 * @tag: Tag of the hbq buffer.
938 *
939 * This function is called with hbalock held. This function searches
940 * for the hbq buffer associated with the given tag in the hbq buffer
941 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
942 * it returns NULL.
943 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100944static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -0500945lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
946{
James Smart92d7f7b2007-06-17 19:56:38 -0500947 struct lpfc_dmabuf *d_buf;
948 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400949 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500950
James Smart51ef4c22007-08-02 11:10:31 -0400951 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +0200952 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -0400953 return NULL;
954
955 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -0500956 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -0400957 if (hbq_buf->tag == tag) {
James Smart92d7f7b2007-06-17 19:56:38 -0500958 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -0500959 }
960 }
James Smart92d7f7b2007-06-17 19:56:38 -0500961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400962 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -0400963 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -0500964 return NULL;
James Smarted957682007-06-17 19:56:37 -0500965}
966
James Smarte59058c2008-08-24 21:49:00 -0400967/**
968 * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
969 * @phba: Pointer to HBA context object.
970 * @hbq_buffer: Pointer to HBQ buffer.
971 *
972 * This function is called with hbalock. This function gives back
973 * the hbq buffer to firmware. If the HBQ does not have space to
974 * post the buffer, it will free the buffer.
975 **/
James Smarted957682007-06-17 19:56:37 -0500976void
James Smart51ef4c22007-08-02 11:10:31 -0400977lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -0500978{
979 uint32_t hbqno;
980
James Smart51ef4c22007-08-02 11:10:31 -0400981 if (hbq_buffer) {
982 hbqno = hbq_buffer->tag >> 16;
983 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
984 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
985 }
James Smarted957682007-06-17 19:56:37 -0500986 }
987}
988
James Smarte59058c2008-08-24 21:49:00 -0400989/**
990 * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
991 * @mbxCommand: mailbox command code.
992 *
993 * This function is called by the mailbox event handler function to verify
994 * that the completed mailbox command is a legitimate mailbox command. If the
995 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
996 * and the mailbox event handler will take the HBA offline.
997 **/
dea31012005-04-17 16:05:31 -0500998static int
999lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1000{
1001 uint8_t ret;
1002
1003 switch (mbxCommand) {
1004 case MBX_LOAD_SM:
1005 case MBX_READ_NV:
1006 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001007 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001008 case MBX_RUN_BIU_DIAG:
1009 case MBX_INIT_LINK:
1010 case MBX_DOWN_LINK:
1011 case MBX_CONFIG_LINK:
1012 case MBX_CONFIG_RING:
1013 case MBX_RESET_RING:
1014 case MBX_READ_CONFIG:
1015 case MBX_READ_RCONFIG:
1016 case MBX_READ_SPARM:
1017 case MBX_READ_STATUS:
1018 case MBX_READ_RPI:
1019 case MBX_READ_XRI:
1020 case MBX_READ_REV:
1021 case MBX_READ_LNK_STAT:
1022 case MBX_REG_LOGIN:
1023 case MBX_UNREG_LOGIN:
1024 case MBX_READ_LA:
1025 case MBX_CLEAR_LA:
1026 case MBX_DUMP_MEMORY:
1027 case MBX_DUMP_CONTEXT:
1028 case MBX_RUN_DIAGS:
1029 case MBX_RESTART:
1030 case MBX_UPDATE_CFG:
1031 case MBX_DOWN_LOAD:
1032 case MBX_DEL_LD_ENTRY:
1033 case MBX_RUN_PROGRAM:
1034 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001035 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001036 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001037 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001038 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001039 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001040 case MBX_LOAD_AREA:
1041 case MBX_RUN_BIU_DIAG64:
1042 case MBX_CONFIG_PORT:
1043 case MBX_READ_SPARM64:
1044 case MBX_READ_RPI64:
1045 case MBX_REG_LOGIN64:
1046 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001047 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001048 case MBX_SET_DEBUG:
1049 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001050 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001051 case MBX_REG_VPI:
1052 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001053 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001054 case MBX_PORT_CAPABILITIES:
1055 case MBX_PORT_IOV_CONTROL:
dea31012005-04-17 16:05:31 -05001056 ret = mbxCommand;
1057 break;
1058 default:
1059 ret = MBX_SHUTDOWN;
1060 break;
1061 }
James Smart2e0fef82007-06-17 19:56:36 -05001062 return ret;
dea31012005-04-17 16:05:31 -05001063}
James Smarte59058c2008-08-24 21:49:00 -04001064
1065/**
1066 * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
1067 * lpfc_sli_issue_mbox_wait.
1068 * @phba: Pointer to HBA context object.
1069 * @pmboxq: Pointer to mailbox command.
1070 *
1071 * This is completion handler function for mailbox commands issued from
1072 * lpfc_sli_issue_mbox_wait function. This function is called by the
1073 * mailbox event handler function with no lock held. This function
1074 * will wake up thread waiting on the wait queue pointed by context1
1075 * of the mailbox.
1076 **/
dea31012005-04-17 16:05:31 -05001077static void
James Smart2e0fef82007-06-17 19:56:36 -05001078lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001079{
1080 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001081 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001082
1083 /*
1084 * If pdone_q is empty, the driver thread gave up waiting and
1085 * continued running.
1086 */
James Smart7054a602007-04-25 09:52:34 -04001087 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001088 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001089 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1090 if (pdone_q)
1091 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001092 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001093 return;
1094}
1095
James Smarte59058c2008-08-24 21:49:00 -04001096
1097/**
1098 * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
1099 * @phba: Pointer to HBA context object.
1100 * @pmb: Pointer to mailbox object.
1101 *
1102 * This function is the default mailbox completion handler. It
1103 * frees the memory resources associated with the completed mailbox
1104 * command. If the completed command is a REG_LOGIN mailbox command,
1105 * this function will issue a UREG_LOGIN to re-claim the RPI.
1106 **/
dea31012005-04-17 16:05:31 -05001107void
James Smart2e0fef82007-06-17 19:56:36 -05001108lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001109{
1110 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -04001111 uint16_t rpi;
1112 int rc;
1113
dea31012005-04-17 16:05:31 -05001114 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001115
dea31012005-04-17 16:05:31 -05001116 if (mp) {
1117 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1118 kfree(mp);
1119 }
James Smart7054a602007-04-25 09:52:34 -04001120
1121 /*
1122 * If a REG_LOGIN succeeded after node is destroyed or node
1123 * is in re-discovery driver need to cleanup the RPI.
1124 */
James Smart2e0fef82007-06-17 19:56:36 -05001125 if (!(phba->pport->load_flag & FC_UNLOADING) &&
1126 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1127 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -04001128
1129 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -05001130 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1131 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001132 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1133 if (rc != MBX_NOT_FINISHED)
1134 return;
1135 }
1136
James Smart2e0fef82007-06-17 19:56:36 -05001137 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001138 return;
1139}
1140
James Smarte59058c2008-08-24 21:49:00 -04001141/**
1142 * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
1143 * @phba: Pointer to HBA context object.
1144 *
1145 * This function is called with no lock held. This function processes all
1146 * the completed mailbox commands and gives it to upper layers. The interrupt
1147 * service routine processes mailbox completion interrupt and adds completed
1148 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1149 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1150 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1151 * function returns the mailbox commands to the upper layer by calling the
1152 * completion handler function of each mailbox.
1153 **/
dea31012005-04-17 16:05:31 -05001154int
James Smart2e0fef82007-06-17 19:56:36 -05001155lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001156{
James Smart92d7f7b2007-06-17 19:56:38 -05001157 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001158 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001159 int rc;
1160 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001161
1162 phba->sli.slistat.mbox_event++;
1163
James Smart92d7f7b2007-06-17 19:56:38 -05001164 /* Get all completed mailboxe buffers into the cmplq */
1165 spin_lock_irq(&phba->hbalock);
1166 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1167 spin_unlock_irq(&phba->hbalock);
1168
dea31012005-04-17 16:05:31 -05001169 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001170 do {
1171 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1172 if (pmb == NULL)
1173 break;
1174
dea31012005-04-17 16:05:31 -05001175 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -05001176
James Smart858c9f62007-06-17 19:56:39 -05001177 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1178 if (pmb->vport) {
1179 lpfc_debugfs_disc_trc(pmb->vport,
1180 LPFC_DISC_TRC_MBOX_VPORT,
1181 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1182 (uint32_t)pmbox->mbxCommand,
1183 pmbox->un.varWords[0],
1184 pmbox->un.varWords[1]);
1185 }
1186 else {
1187 lpfc_debugfs_disc_trc(phba->pport,
1188 LPFC_DISC_TRC_MBOX,
1189 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1190 (uint32_t)pmbox->mbxCommand,
1191 pmbox->un.varWords[0],
1192 pmbox->un.varWords[1]);
1193 }
1194 }
1195
dea31012005-04-17 16:05:31 -05001196 /*
1197 * It is a fatal error if unknown mbox command completion.
1198 */
1199 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1200 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001201 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001202 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001203 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -05001204 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001205 pmb->vport ? pmb->vport->vpi : 0,
1206 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -05001207 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001208 phba->work_hs = HS_FFER3;
1209 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001210 continue;
dea31012005-04-17 16:05:31 -05001211 }
1212
dea31012005-04-17 16:05:31 -05001213 if (pmbox->mbxStatus) {
1214 phba->sli.slistat.mbox_stat_err++;
1215 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1216 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001217 lpfc_printf_log(phba, KERN_INFO,
1218 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001219 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001220 "error - RETRYing Data: x%x "
1221 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001222 pmb->vport ? pmb->vport->vpi :0,
1223 pmbox->mbxCommand,
1224 pmbox->mbxStatus,
1225 pmbox->un.varWords[0],
1226 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001227 pmbox->mbxStatus = 0;
1228 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -05001229 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001230 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001231 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001232 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1233 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -05001234 continue;
dea31012005-04-17 16:05:31 -05001235 }
1236 }
1237
1238 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001239 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001240 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -05001241 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001242 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001243 pmbox->mbxCommand,
1244 pmb->mbox_cmpl,
1245 *((uint32_t *) pmbox),
1246 pmbox->un.varWords[0],
1247 pmbox->un.varWords[1],
1248 pmbox->un.varWords[2],
1249 pmbox->un.varWords[3],
1250 pmbox->un.varWords[4],
1251 pmbox->un.varWords[5],
1252 pmbox->un.varWords[6],
1253 pmbox->un.varWords[7]);
1254
James Smart92d7f7b2007-06-17 19:56:38 -05001255 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001256 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001257 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001258 return 0;
dea31012005-04-17 16:05:31 -05001259}
James Smart92d7f7b2007-06-17 19:56:38 -05001260
James Smarte59058c2008-08-24 21:49:00 -04001261/**
1262 * lpfc_sli_replace_hbqbuff: Replace the HBQ buffer with a new buffer.
1263 * @phba: Pointer to HBA context object.
1264 * @tag: Tag for the HBQ buffer.
1265 *
1266 * This function is called from unsolicited event handler code path to get the
1267 * HBQ buffer associated with an unsolicited iocb. This function is called with
1268 * no lock held. It returns the buffer associated with the given tag and posts
James Smartd7c255b2008-08-24 21:50:00 -04001269 * another buffer to the firmware. Note that the new buffer must be allocated
1270 * before taking the hbalock and that the hba lock must be held until it is
1271 * finished with the hbq entry swap.
James Smarte59058c2008-08-24 21:49:00 -04001272 **/
James Smart92d7f7b2007-06-17 19:56:38 -05001273static struct lpfc_dmabuf *
1274lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
1275{
1276 struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
James Smart51ef4c22007-08-02 11:10:31 -04001277 uint32_t hbqno;
1278 void *virt; /* virtual address ptr */
1279 dma_addr_t phys; /* mapped address */
James Smart3163f722008-02-08 18:50:25 -05001280 unsigned long flags;
1281
James Smartd7c255b2008-08-24 21:50:00 -04001282 hbqno = tag >> 16;
1283 new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart3163f722008-02-08 18:50:25 -05001284 /* Check whether HBQ is still in use */
1285 spin_lock_irqsave(&phba->hbalock, flags);
1286 if (!phba->hbq_in_use) {
James Smartd7c255b2008-08-24 21:50:00 -04001287 if (new_hbq_entry)
1288 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1289 new_hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001290 spin_unlock_irqrestore(&phba->hbalock, flags);
1291 return NULL;
1292 }
James Smart92d7f7b2007-06-17 19:56:38 -05001293
1294 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
James Smart3163f722008-02-08 18:50:25 -05001295 if (hbq_entry == NULL) {
James Smartd7c255b2008-08-24 21:50:00 -04001296 if (new_hbq_entry)
1297 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1298 new_hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001299 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05001300 return NULL;
James Smart3163f722008-02-08 18:50:25 -05001301 }
James Smart92d7f7b2007-06-17 19:56:38 -05001302 list_del(&hbq_entry->dbuf.list);
James Smart51ef4c22007-08-02 11:10:31 -04001303
James Smart3163f722008-02-08 18:50:25 -05001304 if (new_hbq_entry == NULL) {
1305 list_add_tail(&hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1306 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05001307 return &hbq_entry->dbuf;
James Smart3163f722008-02-08 18:50:25 -05001308 }
James Smart92d7f7b2007-06-17 19:56:38 -05001309 new_hbq_entry->tag = -1;
James Smart51ef4c22007-08-02 11:10:31 -04001310 phys = new_hbq_entry->dbuf.phys;
1311 virt = new_hbq_entry->dbuf.virt;
1312 new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
1313 new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
1314 hbq_entry->dbuf.phys = phys;
1315 hbq_entry->dbuf.virt = virt;
James Smart92d7f7b2007-06-17 19:56:38 -05001316 lpfc_sli_free_hbq(phba, hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001317 list_add_tail(&new_hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1318 spin_unlock_irqrestore(&phba->hbalock, flags);
1319
James Smart92d7f7b2007-06-17 19:56:38 -05001320 return &new_hbq_entry->dbuf;
1321}
1322
James Smarte59058c2008-08-24 21:49:00 -04001323/**
1324 * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
1325 * @phba: Pointer to HBA context object.
1326 * @pring: Pointer to driver SLI ring object.
1327 * @tag: buffer tag.
1328 *
1329 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1330 * is set in the tag the buffer is posted for a particular exchange,
1331 * the function will return the buffer without replacing the buffer.
1332 * If the buffer is for unsolicited ELS or CT traffic, this function
1333 * returns the buffer and also posts another buffer to the firmware.
1334 **/
James Smart76bb24e2007-10-27 13:38:00 -04001335static struct lpfc_dmabuf *
1336lpfc_sli_get_buff(struct lpfc_hba *phba,
1337 struct lpfc_sli_ring *pring,
1338 uint32_t tag)
1339{
1340 if (tag & QUE_BUFTAG_BIT)
1341 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1342 else
1343 return lpfc_sli_replace_hbqbuff(phba, tag);
1344}
James Smart57127f12007-10-27 13:37:05 -04001345
James Smarte59058c2008-08-24 21:49:00 -04001346
1347/**
1348 * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
1349 * @phba: Pointer to HBA context object.
1350 * @pring: Pointer to driver SLI ring object.
1351 * @saveq: Pointer to the unsolicited iocb.
1352 *
1353 * This function is called with no lock held by the ring event handler
1354 * when there is an unsolicited iocb posted to the response ring by the
1355 * firmware. This function gets the buffer associated with the iocbs
1356 * and calls the event handler for the ring. This function handles both
1357 * qring buffers and hbq buffers.
1358 * When the function returns 1 the caller can free the iocb object otherwise
1359 * upper layer functions will free the iocb objects.
1360 **/
dea31012005-04-17 16:05:31 -05001361static int
1362lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1363 struct lpfc_iocbq *saveq)
1364{
1365 IOCB_t * irsp;
1366 WORD5 * w5p;
1367 uint32_t Rctl, Type;
1368 uint32_t match, i;
James Smart76bb24e2007-10-27 13:38:00 -04001369 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001370 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001371
1372 match = 0;
1373 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001374
James Smart9c2face2008-01-11 01:53:18 -05001375 if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
1376 return 1;
James Smart57127f12007-10-27 13:37:05 -04001377 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1378 if (pring->lpfc_sli_rcv_async_status)
1379 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1380 else
1381 lpfc_printf_log(phba,
1382 KERN_WARNING,
1383 LOG_SLI,
1384 "0316 Ring %d handler: unexpected "
1385 "ASYNC_STATUS iocb received evt_code "
1386 "0x%x\n",
1387 pring->ringno,
1388 irsp->un.asyncstat.evt_code);
1389 return 1;
1390 }
1391
James Smart3163f722008-02-08 18:50:25 -05001392 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1393 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1394 if (irsp->ulpBdeCount > 0) {
1395 dmzbuf = lpfc_sli_get_buff(phba, pring,
1396 irsp->un.ulpWord[3]);
1397 lpfc_in_buf_free(phba, dmzbuf);
1398 }
1399
1400 if (irsp->ulpBdeCount > 1) {
1401 dmzbuf = lpfc_sli_get_buff(phba, pring,
1402 irsp->unsli3.sli3Words[3]);
1403 lpfc_in_buf_free(phba, dmzbuf);
1404 }
1405
1406 if (irsp->ulpBdeCount > 2) {
1407 dmzbuf = lpfc_sli_get_buff(phba, pring,
1408 irsp->unsli3.sli3Words[7]);
1409 lpfc_in_buf_free(phba, dmzbuf);
1410 }
1411
1412 return 1;
1413 }
1414
James Smart92d7f7b2007-06-17 19:56:38 -05001415 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001416 if (irsp->ulpBdeCount != 0) {
1417 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001418 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001419 if (!saveq->context2)
1420 lpfc_printf_log(phba,
1421 KERN_ERR,
1422 LOG_SLI,
1423 "0341 Ring %d Cannot find buffer for "
1424 "an unsolicited iocb. tag 0x%x\n",
1425 pring->ringno,
1426 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001427 }
1428 if (irsp->ulpBdeCount == 2) {
1429 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04001430 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04001431 if (!saveq->context3)
1432 lpfc_printf_log(phba,
1433 KERN_ERR,
1434 LOG_SLI,
1435 "0342 Ring %d Cannot find buffer for an"
1436 " unsolicited iocb. tag 0x%x\n",
1437 pring->ringno,
1438 irsp->unsli3.sli3Words[7]);
1439 }
1440 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04001441 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04001442 if (irsp->ulpBdeCount != 0) {
1443 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1444 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05001445 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04001446 lpfc_printf_log(phba,
1447 KERN_ERR,
1448 LOG_SLI,
1449 "0343 Ring %d Cannot find "
1450 "buffer for an unsolicited iocb"
1451 ". tag 0x%x\n", pring->ringno,
1452 irsp->un.ulpWord[3]);
1453 }
1454 if (irsp->ulpBdeCount == 2) {
1455 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1456 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05001457 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04001458 lpfc_printf_log(phba,
1459 KERN_ERR,
1460 LOG_SLI,
1461 "0344 Ring %d Cannot find "
1462 "buffer for an unsolicited "
1463 "iocb. tag 0x%x\n",
1464 pring->ringno,
1465 irsp->unsli3.sli3Words[7]);
1466 }
1467 }
James Smart92d7f7b2007-06-17 19:56:38 -05001468 }
James Smart9c2face2008-01-11 01:53:18 -05001469 if (irsp->ulpBdeCount != 0 &&
1470 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1471 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1472 int found = 0;
1473
1474 /* search continue save q for same XRI */
1475 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1476 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1477 list_add_tail(&saveq->list, &iocbq->list);
1478 found = 1;
1479 break;
1480 }
1481 }
1482 if (!found)
1483 list_add_tail(&saveq->clist,
1484 &pring->iocb_continue_saveq);
1485 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1486 list_del_init(&iocbq->clist);
1487 saveq = iocbq;
1488 irsp = &(saveq->iocb);
1489 } else
1490 return 0;
1491 }
1492 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1493 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1494 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1495 Rctl = FC_ELS_REQ;
1496 Type = FC_ELS_DATA;
1497 } else {
1498 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1499 Rctl = w5p->hcsw.Rctl;
1500 Type = w5p->hcsw.Type;
1501
1502 /* Firmware Workaround */
1503 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1504 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1505 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1506 Rctl = FC_ELS_REQ;
1507 Type = FC_ELS_DATA;
1508 w5p->hcsw.Rctl = Rctl;
1509 w5p->hcsw.Type = Type;
1510 }
1511 }
James Smart92d7f7b2007-06-17 19:56:38 -05001512
dea31012005-04-17 16:05:31 -05001513 /* unSolicited Responses */
1514 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001515 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1516 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1517 saveq);
dea31012005-04-17 16:05:31 -05001518 match = 1;
1519 } else {
1520 /* We must search, based on rctl / type
1521 for the right routine */
James Smart9c2face2008-01-11 01:53:18 -05001522 for (i = 0; i < pring->num_mask; i++) {
1523 if ((pring->prt[i].rctl == Rctl)
1524 && (pring->prt[i].type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001525 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1526 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1527 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -05001528 match = 1;
1529 break;
1530 }
1531 }
1532 }
1533 if (match == 0) {
1534 /* Unexpected Rctl / Type received */
1535 /* Ring <ringno> handler: unexpected
1536 Rctl <Rctl> Type <Type> received */
James Smart92d7f7b2007-06-17 19:56:38 -05001537 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001538 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001539 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001540 pring->ringno, Rctl, Type);
dea31012005-04-17 16:05:31 -05001541 }
James Smart92d7f7b2007-06-17 19:56:38 -05001542 return 1;
dea31012005-04-17 16:05:31 -05001543}
1544
James Smarte59058c2008-08-24 21:49:00 -04001545/**
1546 * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
1547 * @phba: Pointer to HBA context object.
1548 * @pring: Pointer to driver SLI ring object.
1549 * @prspiocb: Pointer to response iocb object.
1550 *
1551 * This function looks up the iocb_lookup table to get the command iocb
1552 * corresponding to the given response iocb using the iotag of the
1553 * response iocb. This function is called with the hbalock held.
1554 * This function returns the command iocb object if it finds the command
1555 * iocb else returns NULL.
1556 **/
dea31012005-04-17 16:05:31 -05001557static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001558lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1559 struct lpfc_sli_ring *pring,
1560 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001561{
dea31012005-04-17 16:05:31 -05001562 struct lpfc_iocbq *cmd_iocb = NULL;
1563 uint16_t iotag;
1564
James Bottomley604a3e32005-10-29 10:28:33 -05001565 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001566
James Bottomley604a3e32005-10-29 10:28:33 -05001567 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1568 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001569 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001570 pring->txcmplq_cnt--;
1571 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001572 }
1573
dea31012005-04-17 16:05:31 -05001574 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001575 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001576 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001577 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001578 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001579 return NULL;
1580}
1581
James Smarte59058c2008-08-24 21:49:00 -04001582/**
1583 * lpfc_sli_process_sol_iocb: process solicited iocb completion.
1584 * @phba: Pointer to HBA context object.
1585 * @pring: Pointer to driver SLI ring object.
1586 * @saveq: Pointer to the response iocb to be processed.
1587 *
1588 * This function is called by the ring event handler for non-fcp
1589 * rings when there is a new response iocb in the response ring.
1590 * The caller is not required to hold any locks. This function
1591 * gets the command iocb associated with the response iocb and
1592 * calls the completion handler for the command iocb. If there
1593 * is no completion handler, the function will free the resources
1594 * associated with command iocb. If the response iocb is for
1595 * an already aborted command iocb, the status of the completion
1596 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1597 * This function always returns 1.
1598 **/
dea31012005-04-17 16:05:31 -05001599static int
James Smart2e0fef82007-06-17 19:56:36 -05001600lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001601 struct lpfc_iocbq *saveq)
1602{
James Smart2e0fef82007-06-17 19:56:36 -05001603 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001604 int rc = 1;
1605 unsigned long iflag;
1606
1607 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001608 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001609 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001610 spin_unlock_irqrestore(&phba->hbalock, iflag);
1611
dea31012005-04-17 16:05:31 -05001612 if (cmdiocbp) {
1613 if (cmdiocbp->iocb_cmpl) {
1614 /*
James Smartea2151b2008-09-07 11:52:10 -04001615 * If an ELS command failed send an event to mgmt
1616 * application.
1617 */
1618 if (saveq->iocb.ulpStatus &&
1619 (pring->ringno == LPFC_ELS_RING) &&
1620 (cmdiocbp->iocb.ulpCommand ==
1621 CMD_ELS_REQUEST64_CR))
1622 lpfc_send_els_failure_event(phba,
1623 cmdiocbp, saveq);
1624
1625 /*
dea31012005-04-17 16:05:31 -05001626 * Post all ELS completions to the worker thread.
1627 * All other are passed to the completion callback.
1628 */
1629 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001630 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1631 cmdiocbp->iocb_flag &=
1632 ~LPFC_DRIVER_ABORTED;
1633 saveq->iocb.ulpStatus =
1634 IOSTAT_LOCAL_REJECT;
1635 saveq->iocb.un.ulpWord[4] =
1636 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05001637
1638 /* Firmware could still be in progress
1639 * of DMAing payload, so don't free data
1640 * buffer till after a hbeat.
1641 */
1642 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04001643 }
dea31012005-04-17 16:05:31 -05001644 }
James Smart2e0fef82007-06-17 19:56:36 -05001645 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001646 } else
1647 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001648 } else {
1649 /*
1650 * Unknown initiating command based on the response iotag.
1651 * This could be the case on the ELS ring because of
1652 * lpfc_els_abort().
1653 */
1654 if (pring->ringno != LPFC_ELS_RING) {
1655 /*
1656 * Ring <ringno> handler: unexpected completion IoTag
1657 * <IoTag>
1658 */
James Smarte8b62012007-08-02 11:10:09 -04001659 lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1660 "0322 Ring %d handler: "
1661 "unexpected completion IoTag x%x "
1662 "Data: x%x x%x x%x x%x\n",
1663 pring->ringno,
1664 saveq->iocb.ulpIoTag,
1665 saveq->iocb.ulpStatus,
1666 saveq->iocb.un.ulpWord[4],
1667 saveq->iocb.ulpCommand,
1668 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001669 }
1670 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001671
dea31012005-04-17 16:05:31 -05001672 return rc;
1673}
1674
James Smarte59058c2008-08-24 21:49:00 -04001675/**
1676 * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
1677 * @phba: Pointer to HBA context object.
1678 * @pring: Pointer to driver SLI ring object.
1679 *
1680 * This function is called from the iocb ring event handlers when
1681 * put pointer is ahead of the get pointer for a ring. This function signal
1682 * an error attention condition to the worker thread and the worker
1683 * thread will transition the HBA to offline state.
1684 **/
James Smart2e0fef82007-06-17 19:56:36 -05001685static void
1686lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001687{
James Smart34b02dc2008-08-24 21:49:55 -04001688 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001689 /*
1690 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1691 * rsp ring <portRspMax>
1692 */
1693 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001694 "0312 Ring %d handler: portRspPut %d "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001695 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001696 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001697 pring->numRiocb);
1698
James Smart2e0fef82007-06-17 19:56:36 -05001699 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001700
1701 /*
1702 * All error attention handlers are posted to
1703 * worker thread
1704 */
1705 phba->work_ha |= HA_ERATT;
1706 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001707
James Smart5e9d9b82008-06-14 22:52:53 -04001708 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001709
1710 return;
1711}
1712
James Smarte59058c2008-08-24 21:49:00 -04001713/**
James Smart93996272008-08-24 21:50:30 -04001714 * lpfc_poll_eratt: Error attention polling timer timeout handler.
1715 * @ptr: Pointer to address of HBA context object.
1716 *
1717 * This function is invoked by the Error Attention polling timer when the
1718 * timer times out. It will check the SLI Error Attention register for
1719 * possible attention events. If so, it will post an Error Attention event
1720 * and wake up worker thread to process it. Otherwise, it will set up the
1721 * Error Attention polling timer for the next poll.
1722 **/
1723void lpfc_poll_eratt(unsigned long ptr)
1724{
1725 struct lpfc_hba *phba;
1726 uint32_t eratt = 0;
1727
1728 phba = (struct lpfc_hba *)ptr;
1729
1730 /* Check chip HA register for error event */
1731 eratt = lpfc_sli_check_eratt(phba);
1732
1733 if (eratt)
1734 /* Tell the worker thread there is work to do */
1735 lpfc_worker_wake_up(phba);
1736 else
1737 /* Restart the timer for next eratt poll */
1738 mod_timer(&phba->eratt_poll, jiffies +
1739 HZ * LPFC_ERATT_POLL_INTERVAL);
1740 return;
1741}
1742
1743/**
James Smarte59058c2008-08-24 21:49:00 -04001744 * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
1745 * @phba: Pointer to HBA context object.
1746 *
1747 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1748 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1749 * is enabled.
1750 *
1751 * The caller does not hold any lock.
1752 * The function processes each response iocb in the response ring until it
1753 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1754 * LE bit set. The function will call the completion handler of the command iocb
1755 * if the response iocb indicates a completion for a command iocb or it is
1756 * an abort completion.
1757 **/
James Smart2e0fef82007-06-17 19:56:36 -05001758void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001759{
James Smart2e0fef82007-06-17 19:56:36 -05001760 struct lpfc_sli *psli = &phba->sli;
1761 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001762 IOCB_t *irsp = NULL;
1763 IOCB_t *entry = NULL;
1764 struct lpfc_iocbq *cmdiocbq = NULL;
1765 struct lpfc_iocbq rspiocbq;
James Smart34b02dc2008-08-24 21:49:55 -04001766 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001767 uint32_t status;
1768 uint32_t portRspPut, portRspMax;
1769 int type;
1770 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001771 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001772 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001773
1774 pring->stats.iocb_event++;
1775
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001776 /*
1777 * The next available response entry should never exceed the maximum
1778 * entries. If it does, treat it as an adapter hardware error.
1779 */
1780 portRspMax = pring->numRiocb;
1781 portRspPut = le32_to_cpu(pgp->rspPutInx);
1782 if (unlikely(portRspPut >= portRspMax)) {
1783 lpfc_sli_rsp_pointers_error(phba, pring);
1784 return;
1785 }
1786
1787 rmb();
1788 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001789 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001790 if (++pring->rspidx >= portRspMax)
1791 pring->rspidx = 0;
1792
1793 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1794 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001795 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001796 irsp = &rspiocbq.iocb;
1797 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1798 pring->stats.iocb_rsp++;
1799 rsp_cmpl++;
1800
1801 if (unlikely(irsp->ulpStatus)) {
1802 /* Rsp ring <ringno> error: IOCB */
1803 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001804 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001805 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001806 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001807 irsp->un.ulpWord[0],
1808 irsp->un.ulpWord[1],
1809 irsp->un.ulpWord[2],
1810 irsp->un.ulpWord[3],
1811 irsp->un.ulpWord[4],
1812 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001813 *(uint32_t *)&irsp->un1,
1814 *((uint32_t *)&irsp->un1 + 1));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001815 }
1816
1817 switch (type) {
1818 case LPFC_ABORT_IOCB:
1819 case LPFC_SOL_IOCB:
1820 /*
1821 * Idle exchange closed via ABTS from port. No iocb
1822 * resources need to be recovered.
1823 */
1824 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001825 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001826 "0314 IOCB cmd 0x%x "
1827 "processed. Skipping "
1828 "completion",
James Smartdca94792006-08-01 07:34:08 -04001829 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001830 break;
1831 }
1832
James Smart2e0fef82007-06-17 19:56:36 -05001833 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001834 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1835 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001836 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001837 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1838 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1839 &rspiocbq);
1840 }
1841 break;
1842 default:
1843 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1844 char adaptermsg[LPFC_MAX_ADPTMSG];
1845 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1846 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1847 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001848 dev_warn(&((phba->pcidev)->dev),
1849 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001850 phba->brd_no, adaptermsg);
1851 } else {
1852 /* Unknown IOCB command */
1853 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001854 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001855 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001856 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001857 irsp->ulpStatus,
1858 irsp->ulpIoTag,
1859 irsp->ulpContext);
1860 }
1861 break;
1862 }
1863
1864 /*
1865 * The response IOCB has been processed. Update the ring
1866 * pointer in SLIM. If the port response put pointer has not
1867 * been updated, sync the pgp->rspPutInx and fetch the new port
1868 * response put pointer.
1869 */
James Smarted957682007-06-17 19:56:37 -05001870 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001871
1872 if (pring->rspidx == portRspPut)
1873 portRspPut = le32_to_cpu(pgp->rspPutInx);
1874 }
1875
1876 ha_copy = readl(phba->HAregaddr);
1877 ha_copy >>= (LPFC_FCP_RING * 4);
1878
1879 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001880 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001881 pring->stats.iocb_rsp_full++;
1882 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1883 writel(status, phba->CAregaddr);
1884 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001885 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001886 }
1887 if ((ha_copy & HA_R0CE_RSP) &&
1888 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001889 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001890 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1891 pring->stats.iocb_cmd_empty++;
1892
1893 /* Force update of the local copy of cmdGetInx */
1894 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1895 lpfc_sli_resume_iocb(phba, pring);
1896
1897 if ((pring->lpfc_sli_cmd_available))
1898 (pring->lpfc_sli_cmd_available) (phba, pring);
1899
James Smart2e0fef82007-06-17 19:56:36 -05001900 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001901 }
1902
1903 return;
1904}
1905
James Smarte59058c2008-08-24 21:49:00 -04001906/**
1907 * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
1908 * @phba: Pointer to HBA context object.
1909 * @pring: Pointer to driver SLI ring object.
1910 * @mask: Host attention register mask for this ring.
1911 *
1912 * This function is called from the interrupt context when there is a ring
1913 * event for the fcp ring. The caller does not hold any lock.
1914 * The function processes each response iocb in the response ring until it
1915 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1916 * LE bit set. The function will call the completion handler of the command iocb
1917 * if the response iocb indicates a completion for a command iocb or it is
1918 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
1919 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05001920 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04001921 * to check it explicitly. This function always returns 1.
1922 **/
dea31012005-04-17 16:05:31 -05001923static int
James Smart2e0fef82007-06-17 19:56:36 -05001924lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1925 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001926{
James Smart34b02dc2008-08-24 21:49:55 -04001927 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001928 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001929 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001930 struct lpfc_iocbq *cmdiocbq = NULL;
1931 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001932 uint32_t status;
1933 uint32_t portRspPut, portRspMax;
1934 int rc = 1;
1935 lpfc_iocb_type type;
1936 unsigned long iflag;
1937 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05001938
James Smart2e0fef82007-06-17 19:56:36 -05001939 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001940 pring->stats.iocb_event++;
1941
dea31012005-04-17 16:05:31 -05001942 /*
1943 * The next available response entry should never exceed the maximum
1944 * entries. If it does, treat it as an adapter hardware error.
1945 */
1946 portRspMax = pring->numRiocb;
1947 portRspPut = le32_to_cpu(pgp->rspPutInx);
1948 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001949 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05001950 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001951 return 1;
1952 }
1953
1954 rmb();
1955 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001956 /*
1957 * Fetch an entry off the ring and copy it into a local data
1958 * structure. The copy involves a byte-swap since the
1959 * network byte order and pci byte orders are different.
1960 */
James Smarted957682007-06-17 19:56:37 -05001961 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05001962 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001963
1964 if (++pring->rspidx >= portRspMax)
1965 pring->rspidx = 0;
1966
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001967 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1968 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05001969 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05001970 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001971 irsp = &rspiocbq.iocb;
1972
dea31012005-04-17 16:05:31 -05001973 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1974 pring->stats.iocb_rsp++;
1975 rsp_cmpl++;
1976
1977 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001978 /*
1979 * If resource errors reported from HBA, reduce
1980 * queuedepths of the SCSI device.
1981 */
1982 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1983 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1984 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarteaf15d52008-12-04 22:39:29 -05001985 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001986 spin_lock_irqsave(&phba->hbalock, iflag);
1987 }
1988
dea31012005-04-17 16:05:31 -05001989 /* Rsp ring <ringno> error: IOCB */
1990 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001991 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05001992 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001993 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05001994 irsp->un.ulpWord[0],
1995 irsp->un.ulpWord[1],
1996 irsp->un.ulpWord[2],
1997 irsp->un.ulpWord[3],
1998 irsp->un.ulpWord[4],
1999 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002000 *(uint32_t *)&irsp->un1,
2001 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002002 }
2003
2004 switch (type) {
2005 case LPFC_ABORT_IOCB:
2006 case LPFC_SOL_IOCB:
2007 /*
2008 * Idle exchange closed via ABTS from port. No iocb
2009 * resources need to be recovered.
2010 */
2011 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002012 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002013 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002014 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002015 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002016 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002017 break;
2018 }
2019
James Bottomley604a3e32005-10-29 10:28:33 -05002020 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2021 &rspiocbq);
dea31012005-04-17 16:05:31 -05002022 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002023 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2024 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2025 &rspiocbq);
2026 } else {
James Smart2e0fef82007-06-17 19:56:36 -05002027 spin_unlock_irqrestore(&phba->hbalock,
2028 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002029 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2030 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002031 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002032 iflag);
2033 }
dea31012005-04-17 16:05:31 -05002034 }
2035 break;
James Smarta4bc3372006-12-02 13:34:16 -05002036 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002037 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002038 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002039 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002040 break;
dea31012005-04-17 16:05:31 -05002041 default:
2042 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2043 char adaptermsg[LPFC_MAX_ADPTMSG];
2044 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2045 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2046 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002047 dev_warn(&((phba->pcidev)->dev),
2048 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002049 phba->brd_no, adaptermsg);
2050 } else {
2051 /* Unknown IOCB command */
2052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002053 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002054 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002055 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002056 irsp->ulpStatus,
2057 irsp->ulpIoTag,
2058 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002059 }
2060 break;
2061 }
2062
2063 /*
2064 * The response IOCB has been processed. Update the ring
2065 * pointer in SLIM. If the port response put pointer has not
2066 * been updated, sync the pgp->rspPutInx and fetch the new port
2067 * response put pointer.
2068 */
James Smarted957682007-06-17 19:56:37 -05002069 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002070
2071 if (pring->rspidx == portRspPut)
2072 portRspPut = le32_to_cpu(pgp->rspPutInx);
2073 }
2074
2075 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2076 pring->stats.iocb_rsp_full++;
2077 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2078 writel(status, phba->CAregaddr);
2079 readl(phba->CAregaddr);
2080 }
2081 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2082 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2083 pring->stats.iocb_cmd_empty++;
2084
2085 /* Force update of the local copy of cmdGetInx */
2086 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2087 lpfc_sli_resume_iocb(phba, pring);
2088
2089 if ((pring->lpfc_sli_cmd_available))
2090 (pring->lpfc_sli_cmd_available) (phba, pring);
2091
2092 }
2093
James Smart2e0fef82007-06-17 19:56:36 -05002094 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002095 return rc;
2096}
2097
James Smarte59058c2008-08-24 21:49:00 -04002098/**
2099 * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
2100 * @phba: Pointer to HBA context object.
2101 * @pring: Pointer to driver SLI ring object.
2102 * @mask: Host attention register mask for this ring.
2103 *
2104 * This function is called from the worker thread when there is a ring
2105 * event for non-fcp rings. The caller does not hold any lock .
2106 * The function processes each response iocb in the response ring until it
2107 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2108 * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
2109 * response iocb indicates a completion of a command iocb. The function
2110 * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
2111 * iocb. The function frees the resources or calls the completion handler if
2112 * this iocb is an abort completion. The function returns 0 when the allocated
2113 * iocbs are not freed, otherwise returns 1.
2114 **/
dea31012005-04-17 16:05:31 -05002115int
James Smart2e0fef82007-06-17 19:56:36 -05002116lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2117 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002118{
James Smart34b02dc2008-08-24 21:49:55 -04002119 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002120 IOCB_t *entry;
2121 IOCB_t *irsp = NULL;
2122 struct lpfc_iocbq *rspiocbp = NULL;
2123 struct lpfc_iocbq *next_iocb;
2124 struct lpfc_iocbq *cmdiocbp;
2125 struct lpfc_iocbq *saveq;
dea31012005-04-17 16:05:31 -05002126 uint8_t iocb_cmd_type;
2127 lpfc_iocb_type type;
2128 uint32_t status, free_saveq;
2129 uint32_t portRspPut, portRspMax;
2130 int rc = 1;
2131 unsigned long iflag;
dea31012005-04-17 16:05:31 -05002132
James Smart34b02dc2008-08-24 21:49:55 -04002133 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002134 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002135 pring->stats.iocb_event++;
2136
dea31012005-04-17 16:05:31 -05002137 /*
2138 * The next available response entry should never exceed the maximum
2139 * entries. If it does, treat it as an adapter hardware error.
2140 */
2141 portRspMax = pring->numRiocb;
2142 portRspPut = le32_to_cpu(pgp->rspPutInx);
2143 if (portRspPut >= portRspMax) {
2144 /*
2145 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
2146 * rsp ring <portRspMax>
2147 */
James Smarted957682007-06-17 19:56:37 -05002148 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002149 "0303 Ring %d handler: portRspPut %d "
dea31012005-04-17 16:05:31 -05002150 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002151 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002152
James Smart2e0fef82007-06-17 19:56:36 -05002153 phba->link_state = LPFC_HBA_ERROR;
2154 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002155
2156 phba->work_hs = HS_FFER3;
2157 lpfc_handle_eratt(phba);
2158
2159 return 1;
2160 }
2161
2162 rmb();
dea31012005-04-17 16:05:31 -05002163 while (pring->rspidx != portRspPut) {
2164 /*
2165 * Build a completion list and call the appropriate handler.
2166 * The process is to get the next available response iocb, get
2167 * a free iocb from the list, copy the response data into the
2168 * free iocb, insert to the continuation list, and update the
2169 * next response index to slim. This process makes response
2170 * iocb's in the ring available to DMA as fast as possible but
2171 * pays a penalty for a copy operation. Since the iocb is
2172 * only 32 bytes, this penalty is considered small relative to
2173 * the PCI reads for register values and a slim write. When
2174 * the ulpLe field is set, the entire Command has been
2175 * received.
2176 */
James Smarted957682007-06-17 19:56:37 -05002177 entry = lpfc_resp_iocb(phba, pring);
2178
James Smart858c9f62007-06-17 19:56:39 -05002179 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002180 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002181 if (rspiocbp == NULL) {
2182 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002183 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002184 break;
2185 }
2186
James Smarted957682007-06-17 19:56:37 -05002187 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2188 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002189 irsp = &rspiocbp->iocb;
2190
2191 if (++pring->rspidx >= portRspMax)
2192 pring->rspidx = 0;
2193
James Smarta58cbd52007-08-02 11:09:43 -04002194 if (pring->ringno == LPFC_ELS_RING) {
2195 lpfc_debugfs_slow_ring_trc(phba,
2196 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2197 *(((uint32_t *) irsp) + 4),
2198 *(((uint32_t *) irsp) + 6),
2199 *(((uint32_t *) irsp) + 7));
2200 }
2201
James Smarted957682007-06-17 19:56:37 -05002202 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002203
James Smart9c2face2008-01-11 01:53:18 -05002204 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
dea31012005-04-17 16:05:31 -05002205
2206 pring->iocb_continueq_cnt++;
2207 if (irsp->ulpLe) {
2208 /*
2209 * By default, the driver expects to free all resources
2210 * associated with this iocb completion.
2211 */
2212 free_saveq = 1;
2213 saveq = list_get_first(&pring->iocb_continueq,
2214 struct lpfc_iocbq, list);
2215 irsp = &(saveq->iocb);
2216 list_del_init(&pring->iocb_continueq);
2217 pring->iocb_continueq_cnt = 0;
2218
2219 pring->stats.iocb_rsp++;
2220
James Smart92d7f7b2007-06-17 19:56:38 -05002221 /*
2222 * If resource errors reported from HBA, reduce
2223 * queuedepths of the SCSI device.
2224 */
2225 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2226 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2227 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarteaf15d52008-12-04 22:39:29 -05002228 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002229 spin_lock_irqsave(&phba->hbalock, iflag);
2230 }
2231
dea31012005-04-17 16:05:31 -05002232 if (irsp->ulpStatus) {
2233 /* Rsp ring <ringno> error: IOCB */
James Smarted957682007-06-17 19:56:37 -05002234 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002235 "0328 Rsp Ring %d error: "
James Smarted957682007-06-17 19:56:37 -05002236 "IOCB Data: "
2237 "x%x x%x x%x x%x "
2238 "x%x x%x x%x x%x "
2239 "x%x x%x x%x x%x "
2240 "x%x x%x x%x x%x\n",
James Smarted957682007-06-17 19:56:37 -05002241 pring->ringno,
2242 irsp->un.ulpWord[0],
2243 irsp->un.ulpWord[1],
2244 irsp->un.ulpWord[2],
2245 irsp->un.ulpWord[3],
2246 irsp->un.ulpWord[4],
2247 irsp->un.ulpWord[5],
2248 *(((uint32_t *) irsp) + 6),
2249 *(((uint32_t *) irsp) + 7),
2250 *(((uint32_t *) irsp) + 8),
2251 *(((uint32_t *) irsp) + 9),
2252 *(((uint32_t *) irsp) + 10),
2253 *(((uint32_t *) irsp) + 11),
2254 *(((uint32_t *) irsp) + 12),
2255 *(((uint32_t *) irsp) + 13),
2256 *(((uint32_t *) irsp) + 14),
2257 *(((uint32_t *) irsp) + 15));
dea31012005-04-17 16:05:31 -05002258 }
2259
2260 /*
2261 * Fetch the IOCB command type and call the correct
2262 * completion routine. Solicited and Unsolicited
2263 * IOCBs on the ELS ring get freed back to the
2264 * lpfc_iocb_list by the discovery kernel thread.
2265 */
2266 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2267 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2268 if (type == LPFC_SOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002269 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002270 rc = lpfc_sli_process_sol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002271 saveq);
2272 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002273 } else if (type == LPFC_UNSOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002274 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002275 rc = lpfc_sli_process_unsol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002276 saveq);
2277 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9c2face2008-01-11 01:53:18 -05002278 if (!rc)
2279 free_saveq = 0;
dea31012005-04-17 16:05:31 -05002280 } else if (type == LPFC_ABORT_IOCB) {
2281 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
2282 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05002283 lpfc_sli_iocbq_lookup(phba, pring,
2284 saveq)))) {
dea31012005-04-17 16:05:31 -05002285 /* Call the specified completion
2286 routine */
2287 if (cmdiocbp->iocb_cmpl) {
2288 spin_unlock_irqrestore(
James Smart2e0fef82007-06-17 19:56:36 -05002289 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002290 iflag);
2291 (cmdiocbp->iocb_cmpl) (phba,
2292 cmdiocbp, saveq);
2293 spin_lock_irqsave(
James Smart2e0fef82007-06-17 19:56:36 -05002294 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002295 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002296 } else
James Smart2e0fef82007-06-17 19:56:36 -05002297 __lpfc_sli_release_iocbq(phba,
James Bottomley604a3e32005-10-29 10:28:33 -05002298 cmdiocbp);
dea31012005-04-17 16:05:31 -05002299 }
2300 } else if (type == LPFC_UNKNOWN_IOCB) {
2301 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2302
2303 char adaptermsg[LPFC_MAX_ADPTMSG];
2304
2305 memset(adaptermsg, 0,
2306 LPFC_MAX_ADPTMSG);
2307 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2308 MAX_MSG_DATA);
2309 dev_warn(&((phba->pcidev)->dev),
Joe Perches898eb712007-10-18 03:06:30 -07002310 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002311 phba->brd_no, adaptermsg);
2312 } else {
2313 /* Unknown IOCB command */
James Smart92d7f7b2007-06-17 19:56:38 -05002314 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002315 "0335 Unknown IOCB "
James Smart92d7f7b2007-06-17 19:56:38 -05002316 "command Data: x%x "
2317 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002318 irsp->ulpCommand,
2319 irsp->ulpStatus,
2320 irsp->ulpIoTag,
2321 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002322 }
2323 }
2324
2325 if (free_saveq) {
James Smart2e0fef82007-06-17 19:56:36 -05002326 list_for_each_entry_safe(rspiocbp, next_iocb,
2327 &saveq->list, list) {
2328 list_del(&rspiocbp->list);
2329 __lpfc_sli_release_iocbq(phba,
2330 rspiocbp);
dea31012005-04-17 16:05:31 -05002331 }
James Smart2e0fef82007-06-17 19:56:36 -05002332 __lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05002333 }
James Smart92d7f7b2007-06-17 19:56:38 -05002334 rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002335 }
2336
2337 /*
2338 * If the port response put pointer has not been updated, sync
2339 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2340 * response put pointer.
2341 */
2342 if (pring->rspidx == portRspPut) {
2343 portRspPut = le32_to_cpu(pgp->rspPutInx);
2344 }
2345 } /* while (pring->rspidx != portRspPut) */
2346
James Smart92d7f7b2007-06-17 19:56:38 -05002347 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002348 /* At least one response entry has been freed */
2349 pring->stats.iocb_rsp_full++;
2350 /* SET RxRE_RSP in Chip Att register */
2351 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2352 writel(status, phba->CAregaddr);
2353 readl(phba->CAregaddr); /* flush */
2354 }
2355 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2356 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2357 pring->stats.iocb_cmd_empty++;
2358
2359 /* Force update of the local copy of cmdGetInx */
2360 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2361 lpfc_sli_resume_iocb(phba, pring);
2362
2363 if ((pring->lpfc_sli_cmd_available))
2364 (pring->lpfc_sli_cmd_available) (phba, pring);
2365
2366 }
2367
James Smart2e0fef82007-06-17 19:56:36 -05002368 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002369 return rc;
2370}
2371
James Smarte59058c2008-08-24 21:49:00 -04002372/**
2373 * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
2374 * @phba: Pointer to HBA context object.
2375 * @pring: Pointer to driver SLI ring object.
2376 *
2377 * This function aborts all iocbs in the given ring and frees all the iocb
2378 * objects in txq. This function issues an abort iocb for all the iocb commands
2379 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2380 * the return of this function. The caller is not required to hold any locks.
2381 **/
James Smart2e0fef82007-06-17 19:56:36 -05002382void
dea31012005-04-17 16:05:31 -05002383lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2384{
James Smart2534ba72007-04-25 09:52:20 -04002385 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002386 struct lpfc_iocbq *iocb, *next_iocb;
James Smart2534ba72007-04-25 09:52:20 -04002387 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05002388
James Smart92d7f7b2007-06-17 19:56:38 -05002389 if (pring->ringno == LPFC_ELS_RING) {
2390 lpfc_fabric_abort_hba(phba);
2391 }
2392
dea31012005-04-17 16:05:31 -05002393 /* Error everything on txq and txcmplq
2394 * First do the txq.
2395 */
James Smart2e0fef82007-06-17 19:56:36 -05002396 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002397 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002398 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002399
2400 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002401 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2402 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2403
James Smart2e0fef82007-06-17 19:56:36 -05002404 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002405
2406 while (!list_empty(&completions)) {
2407 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
dea31012005-04-17 16:05:31 -05002408 cmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05002409 list_del_init(&iocb->list);
dea31012005-04-17 16:05:31 -05002410
James Smart2e0fef82007-06-17 19:56:36 -05002411 if (!iocb->iocb_cmpl)
2412 lpfc_sli_release_iocbq(phba, iocb);
2413 else {
dea31012005-04-17 16:05:31 -05002414 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2415 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
dea31012005-04-17 16:05:31 -05002416 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002417 }
dea31012005-04-17 16:05:31 -05002418 }
dea31012005-04-17 16:05:31 -05002419}
2420
James Smarte59058c2008-08-24 21:49:00 -04002421/**
James Smarta8e497d2008-08-24 21:50:11 -04002422 * lpfc_sli_flush_fcp_rings: flush all iocbs in the fcp ring.
2423 * @phba: Pointer to HBA context object.
2424 *
2425 * This function flushes all iocbs in the fcp ring and frees all the iocb
2426 * objects in txq and txcmplq. This function will not issue abort iocbs
2427 * for all the iocb commands in txcmplq, they will just be returned with
2428 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2429 * slot has been permanently disabled.
2430 **/
2431void
2432lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2433{
2434 LIST_HEAD(txq);
2435 LIST_HEAD(txcmplq);
2436 struct lpfc_iocbq *iocb;
2437 IOCB_t *cmd = NULL;
2438 struct lpfc_sli *psli = &phba->sli;
2439 struct lpfc_sli_ring *pring;
2440
2441 /* Currently, only one fcp ring */
2442 pring = &psli->ring[psli->fcp_ring];
2443
2444 spin_lock_irq(&phba->hbalock);
2445 /* Retrieve everything on txq */
2446 list_splice_init(&pring->txq, &txq);
2447 pring->txq_cnt = 0;
2448
2449 /* Retrieve everything on the txcmplq */
2450 list_splice_init(&pring->txcmplq, &txcmplq);
2451 pring->txcmplq_cnt = 0;
2452 spin_unlock_irq(&phba->hbalock);
2453
2454 /* Flush the txq */
2455 while (!list_empty(&txq)) {
2456 iocb = list_get_first(&txq, struct lpfc_iocbq, list);
2457 cmd = &iocb->iocb;
2458 list_del_init(&iocb->list);
2459
2460 if (!iocb->iocb_cmpl)
2461 lpfc_sli_release_iocbq(phba, iocb);
2462 else {
2463 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2464 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2465 (iocb->iocb_cmpl) (phba, iocb, iocb);
2466 }
2467 }
2468
2469 /* Flush the txcmpq */
2470 while (!list_empty(&txcmplq)) {
2471 iocb = list_get_first(&txcmplq, struct lpfc_iocbq, list);
2472 cmd = &iocb->iocb;
2473 list_del_init(&iocb->list);
2474
2475 if (!iocb->iocb_cmpl)
2476 lpfc_sli_release_iocbq(phba, iocb);
2477 else {
2478 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2479 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2480 (iocb->iocb_cmpl) (phba, iocb, iocb);
2481 }
2482 }
2483}
2484
2485/**
James Smarte59058c2008-08-24 21:49:00 -04002486 * lpfc_sli_brdready: Check for host status bits.
2487 * @phba: Pointer to HBA context object.
2488 * @mask: Bit mask to be checked.
2489 *
2490 * This function reads the host status register and compares
2491 * with the provided bit mask to check if HBA completed
2492 * the restart. This function will wait in a loop for the
2493 * HBA to complete restart. If the HBA does not restart within
2494 * 15 iterations, the function will reset the HBA again. The
2495 * function returns 1 when HBA fail to restart otherwise returns
2496 * zero.
2497 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002498int
James Smart2e0fef82007-06-17 19:56:36 -05002499lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05002500{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002501 uint32_t status;
2502 int i = 0;
2503 int retval = 0;
dea31012005-04-17 16:05:31 -05002504
Jamie Wellnitz41415862006-02-28 19:25:27 -05002505 /* Read the HBA Host Status Register */
2506 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002507
Jamie Wellnitz41415862006-02-28 19:25:27 -05002508 /*
2509 * Check status register every 100ms for 5 retries, then every
2510 * 500ms for 5, then every 2.5 sec for 5, then reset board and
2511 * every 2.5 sec for 4.
2512 * Break our of the loop if errors occurred during init.
2513 */
2514 while (((status & mask) != mask) &&
2515 !(status & HS_FFERM) &&
2516 i++ < 20) {
dea31012005-04-17 16:05:31 -05002517
Jamie Wellnitz41415862006-02-28 19:25:27 -05002518 if (i <= 5)
2519 msleep(10);
2520 else if (i <= 10)
2521 msleep(500);
2522 else
2523 msleep(2500);
dea31012005-04-17 16:05:31 -05002524
Jamie Wellnitz41415862006-02-28 19:25:27 -05002525 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002526 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002527 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002528 lpfc_sli_brdrestart(phba);
2529 }
2530 /* Read the HBA Host Status Register */
2531 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002532 }
dea31012005-04-17 16:05:31 -05002533
Jamie Wellnitz41415862006-02-28 19:25:27 -05002534 /* Check to see if any errors occurred during init */
2535 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05002536 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002537 retval = 1;
2538 }
dea31012005-04-17 16:05:31 -05002539
Jamie Wellnitz41415862006-02-28 19:25:27 -05002540 return retval;
dea31012005-04-17 16:05:31 -05002541}
2542
James Smart92908312006-03-07 15:04:13 -05002543#define BARRIER_TEST_PATTERN (0xdeadbeef)
2544
James Smarte59058c2008-08-24 21:49:00 -04002545/**
2546 * lpfc_reset_barrier: Make HBA ready for HBA reset.
2547 * @phba: Pointer to HBA context object.
2548 *
2549 * This function is called before resetting an HBA. This
2550 * function requests HBA to quiesce DMAs before a reset.
2551 **/
James Smart2e0fef82007-06-17 19:56:36 -05002552void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05002553{
James Smart65a29c12006-07-06 15:50:50 -04002554 uint32_t __iomem *resp_buf;
2555 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05002556 volatile uint32_t mbox;
2557 uint32_t hc_copy;
2558 int i;
2559 uint8_t hdrtype;
2560
2561 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2562 if (hdrtype != 0x80 ||
2563 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2564 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2565 return;
2566
2567 /*
2568 * Tell the other part of the chip to suspend temporarily all
2569 * its DMA activity.
2570 */
James Smart65a29c12006-07-06 15:50:50 -04002571 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002572
2573 /* Disable the error attention */
2574 hc_copy = readl(phba->HCregaddr);
2575 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2576 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002577 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002578
2579 if (readl(phba->HAregaddr) & HA_ERATT) {
2580 /* Clear Chip error bit */
2581 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002582 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002583 }
2584
2585 mbox = 0;
2586 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2587 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2588
2589 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04002590 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002591 writel(mbox, mbox_buf);
2592
2593 for (i = 0;
2594 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2595 mdelay(1);
2596
2597 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2598 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05002599 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05002600 goto restore_hc;
2601 else
2602 goto clear_errat;
2603 }
2604
2605 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2606 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
2607 mdelay(1);
2608
2609clear_errat:
2610
2611 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2612 mdelay(1);
2613
2614 if (readl(phba->HAregaddr) & HA_ERATT) {
2615 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002616 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002617 }
2618
2619restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05002620 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002621 writel(hc_copy, phba->HCregaddr);
2622 readl(phba->HCregaddr); /* flush */
2623}
2624
James Smarte59058c2008-08-24 21:49:00 -04002625/**
2626 * lpfc_sli_brdkill: Issue a kill_board mailbox command.
2627 * @phba: Pointer to HBA context object.
2628 *
2629 * This function issues a kill_board mailbox command and waits for
2630 * the error attention interrupt. This function is called for stopping
2631 * the firmware processing. The caller is not required to hold any
2632 * locks. This function calls lpfc_hba_down_post function to free
2633 * any pending commands after the kill. The function will return 1 when it
2634 * fails to kill the board else will return 0.
2635 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002636int
James Smart2e0fef82007-06-17 19:56:36 -05002637lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002638{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002639 struct lpfc_sli *psli;
2640 LPFC_MBOXQ_t *pmb;
2641 uint32_t status;
2642 uint32_t ha_copy;
2643 int retval;
2644 int i = 0;
2645
2646 psli = &phba->sli;
2647
2648 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05002649 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002650 "0329 Kill HBA Data: x%x x%x\n",
2651 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002652
James Smart98c9ea52007-10-27 13:37:33 -04002653 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2654 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002655 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002656
2657 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05002658 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002659 status = readl(phba->HCregaddr);
2660 status &= ~HC_ERINT_ENA;
2661 writel(status, phba->HCregaddr);
2662 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002663 phba->link_flag |= LS_IGNORE_ERATT;
2664 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002665
2666 lpfc_kill_board(phba, pmb);
2667 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2668 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2669
2670 if (retval != MBX_SUCCESS) {
2671 if (retval != MBX_BUSY)
2672 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002673 spin_lock_irq(&phba->hbalock);
2674 phba->link_flag &= ~LS_IGNORE_ERATT;
2675 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002676 return 1;
2677 }
2678
James Smart92908312006-03-07 15:04:13 -05002679 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2680
Jamie Wellnitz41415862006-02-28 19:25:27 -05002681 mempool_free(pmb, phba->mbox_mem_pool);
2682
2683 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2684 * attention every 100ms for 3 seconds. If we don't get ERATT after
2685 * 3 seconds we still set HBA_ERROR state because the status of the
2686 * board is now undefined.
2687 */
2688 ha_copy = readl(phba->HAregaddr);
2689
2690 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2691 mdelay(100);
2692 ha_copy = readl(phba->HAregaddr);
2693 }
2694
2695 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05002696 if (ha_copy & HA_ERATT) {
2697 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002698 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002699 }
James Smart2e0fef82007-06-17 19:56:36 -05002700 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002701 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002702 phba->link_flag &= ~LS_IGNORE_ERATT;
2703 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002704
2705 psli->mbox_active = NULL;
2706 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002707 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002708
James Smart2e0fef82007-06-17 19:56:36 -05002709 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002710}
2711
James Smarte59058c2008-08-24 21:49:00 -04002712/**
2713 * lpfc_sli_brdreset: Reset the HBA.
2714 * @phba: Pointer to HBA context object.
2715 *
2716 * This function resets the HBA by writing HC_INITFF to the control
2717 * register. After the HBA resets, this function resets all the iocb ring
2718 * indices. This function disables PCI layer parity checking during
2719 * the reset.
2720 * This function returns 0 always.
2721 * The caller is not required to hold any locks.
2722 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002723int
James Smart2e0fef82007-06-17 19:56:36 -05002724lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002725{
2726 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002727 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002728 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05002729 int i;
dea31012005-04-17 16:05:31 -05002730
Jamie Wellnitz41415862006-02-28 19:25:27 -05002731 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002732
Jamie Wellnitz41415862006-02-28 19:25:27 -05002733 /* Reset HBA */
2734 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002735 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002736 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05002737
2738 /* perform board reset */
2739 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002740 phba->pport->fc_myDID = 0;
2741 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002742
Jamie Wellnitz41415862006-02-28 19:25:27 -05002743 /* Turn off parity checking and serr during the physical reset */
2744 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2745 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2746 (cfg_value &
2747 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2748
James Smart1c067a42006-08-01 07:33:52 -04002749 psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002750 /* Now toggle INITFF bit in the Host Control Register */
2751 writel(HC_INITFF, phba->HCregaddr);
2752 mdelay(1);
2753 readl(phba->HCregaddr); /* flush */
2754 writel(0, phba->HCregaddr);
2755 readl(phba->HCregaddr); /* flush */
2756
2757 /* Restore PCI cmd register */
2758 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002759
2760 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002761 for (i = 0; i < psli->num_rings; i++) {
2762 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002763 pring->flag = 0;
2764 pring->rspidx = 0;
2765 pring->next_cmdidx = 0;
2766 pring->local_getidx = 0;
2767 pring->cmdidx = 0;
2768 pring->missbufcnt = 0;
2769 }
dea31012005-04-17 16:05:31 -05002770
James Smart2e0fef82007-06-17 19:56:36 -05002771 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002772 return 0;
2773}
2774
James Smarte59058c2008-08-24 21:49:00 -04002775/**
2776 * lpfc_sli_brdrestart: Restart the HBA.
2777 * @phba: Pointer to HBA context object.
2778 *
2779 * This function is called in the SLI initialization code path to
2780 * restart the HBA. The caller is not required to hold any lock.
2781 * This function writes MBX_RESTART mailbox command to the SLIM and
2782 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
2783 * function to free any pending commands. The function enables
2784 * POST only during the first initialization. The function returns zero.
2785 * The function does not guarantee completion of MBX_RESTART mailbox
2786 * command before the return of this function.
2787 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002788int
James Smart2e0fef82007-06-17 19:56:36 -05002789lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002790{
2791 MAILBOX_t *mb;
2792 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002793 volatile uint32_t word0;
2794 void __iomem *to_slim;
2795
James Smart2e0fef82007-06-17 19:56:36 -05002796 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002797
2798 psli = &phba->sli;
2799
2800 /* Restart HBA */
2801 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002802 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002803 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002804
2805 word0 = 0;
2806 mb = (MAILBOX_t *) &word0;
2807 mb->mbxCommand = MBX_RESTART;
2808 mb->mbxHc = 1;
2809
James Smart92908312006-03-07 15:04:13 -05002810 lpfc_reset_barrier(phba);
2811
Jamie Wellnitz41415862006-02-28 19:25:27 -05002812 to_slim = phba->MBslimaddr;
2813 writel(*(uint32_t *) mb, to_slim);
2814 readl(to_slim); /* flush */
2815
2816 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05002817 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002818 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05002819 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05002820 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04002821 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002822 writel(*(uint32_t *) mb, to_slim);
2823 readl(to_slim); /* flush */
2824
2825 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002826 phba->pport->stopped = 0;
2827 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002828
James Smart2e0fef82007-06-17 19:56:36 -05002829 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002830
James Smart64ba8812006-08-02 15:24:34 -04002831 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2832 psli->stats_start = get_seconds();
2833
James Smarteaf15d52008-12-04 22:39:29 -05002834 /* Give the INITFF and Post time to settle. */
2835 mdelay(100);
dea31012005-04-17 16:05:31 -05002836
Jamie Wellnitz41415862006-02-28 19:25:27 -05002837 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002838
2839 return 0;
2840}
2841
James Smarte59058c2008-08-24 21:49:00 -04002842/**
2843 * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
2844 * @phba: Pointer to HBA context object.
2845 *
2846 * This function is called after a HBA restart to wait for successful
2847 * restart of the HBA. Successful restart of the HBA is indicated by
2848 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
2849 * iteration, the function will restart the HBA again. The function returns
2850 * zero if HBA successfully restarted else returns negative error code.
2851 **/
dea31012005-04-17 16:05:31 -05002852static int
2853lpfc_sli_chipset_init(struct lpfc_hba *phba)
2854{
2855 uint32_t status, i = 0;
2856
2857 /* Read the HBA Host Status Register */
2858 status = readl(phba->HSregaddr);
2859
2860 /* Check status register to see what current state is */
2861 i = 0;
2862 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2863
2864 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2865 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2866 * 4.
2867 */
2868 if (i++ >= 20) {
2869 /* Adapter failed to init, timeout, status reg
2870 <status> */
James Smarted957682007-06-17 19:56:37 -05002871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002872 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002873 "timeout, status reg x%x, "
2874 "FW Data: A8 x%x AC x%x\n", status,
2875 readl(phba->MBslimaddr + 0xa8),
2876 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002877 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002878 return -ETIMEDOUT;
2879 }
2880
2881 /* Check to see if any errors occurred during init */
2882 if (status & HS_FFERM) {
2883 /* ERROR: During chipset initialization */
2884 /* Adapter failed to init, chipset, status reg
2885 <status> */
James Smarted957682007-06-17 19:56:37 -05002886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002887 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002888 "chipset, status reg x%x, "
2889 "FW Data: A8 x%x AC x%x\n", status,
2890 readl(phba->MBslimaddr + 0xa8),
2891 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002892 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002893 return -EIO;
2894 }
2895
2896 if (i <= 5) {
2897 msleep(10);
2898 } else if (i <= 10) {
2899 msleep(500);
2900 } else {
2901 msleep(2500);
2902 }
2903
2904 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002905 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002906 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002907 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002908 }
2909 /* Read the HBA Host Status Register */
2910 status = readl(phba->HSregaddr);
2911 }
2912
2913 /* Check to see if any errors occurred during init */
2914 if (status & HS_FFERM) {
2915 /* ERROR: During chipset initialization */
2916 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05002917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002918 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05002919 "status reg x%x, "
2920 "FW Data: A8 x%x AC x%x\n", status,
2921 readl(phba->MBslimaddr + 0xa8),
2922 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002923 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002924 return -EIO;
2925 }
2926
2927 /* Clear all interrupt enable conditions */
2928 writel(0, phba->HCregaddr);
2929 readl(phba->HCregaddr); /* flush */
2930
2931 /* setup host attn register */
2932 writel(0xffffffff, phba->HAregaddr);
2933 readl(phba->HAregaddr); /* flush */
2934 return 0;
2935}
2936
James Smarte59058c2008-08-24 21:49:00 -04002937/**
2938 * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
2939 *
2940 * This function calculates and returns the number of HBQs required to be
2941 * configured.
2942 **/
James Smart78b2d852007-08-02 11:10:21 -04002943int
James Smarted957682007-06-17 19:56:37 -05002944lpfc_sli_hbq_count(void)
2945{
James Smart92d7f7b2007-06-17 19:56:38 -05002946 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05002947}
2948
James Smarte59058c2008-08-24 21:49:00 -04002949/**
2950 * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
2951 *
2952 * This function adds the number of hbq entries in every HBQ to get
2953 * the total number of hbq entries required for the HBA and returns
2954 * the total count.
2955 **/
James Smarted957682007-06-17 19:56:37 -05002956static int
2957lpfc_sli_hbq_entry_count(void)
2958{
2959 int hbq_count = lpfc_sli_hbq_count();
2960 int count = 0;
2961 int i;
2962
2963 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05002964 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05002965 return count;
2966}
2967
James Smarte59058c2008-08-24 21:49:00 -04002968/**
2969 * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
2970 *
2971 * This function calculates amount of memory required for all hbq entries
2972 * to be configured and returns the total memory required.
2973 **/
dea31012005-04-17 16:05:31 -05002974int
James Smarted957682007-06-17 19:56:37 -05002975lpfc_sli_hbq_size(void)
2976{
2977 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2978}
2979
James Smarte59058c2008-08-24 21:49:00 -04002980/**
2981 * lpfc_sli_hbq_setup: configure and initialize HBQs.
2982 * @phba: Pointer to HBA context object.
2983 *
2984 * This function is called during the SLI initialization to configure
2985 * all the HBQs and post buffers to the HBQ. The caller is not
2986 * required to hold any locks. This function will return zero if successful
2987 * else it will return negative error code.
2988 **/
James Smarted957682007-06-17 19:56:37 -05002989static int
2990lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2991{
2992 int hbq_count = lpfc_sli_hbq_count();
2993 LPFC_MBOXQ_t *pmb;
2994 MAILBOX_t *pmbox;
2995 uint32_t hbqno;
2996 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05002997
James Smart92d7f7b2007-06-17 19:56:38 -05002998 /* Get a Mailbox buffer to setup mailbox
2999 * commands for HBA initialization
3000 */
James Smarted957682007-06-17 19:56:37 -05003001 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3002
3003 if (!pmb)
3004 return -ENOMEM;
3005
3006 pmbox = &pmb->mb;
3007
3008 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3009 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003010 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003011
3012 hbq_entry_index = 0;
3013 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3014 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3015 phba->hbqs[hbqno].hbqPutIdx = 0;
3016 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3017 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003018 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003019 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3020 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003021 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3022
3023 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3024 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3025 mbxStatus <status>, ring <num> */
3026
3027 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003028 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003029 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003030 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003031 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003032 pmbox->mbxStatus, hbqno);
3033
3034 phba->link_state = LPFC_HBA_ERROR;
3035 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003036 return ENXIO;
3037 }
3038 }
3039 phba->hbq_count = hbq_count;
3040
James Smarted957682007-06-17 19:56:37 -05003041 mempool_free(pmb, phba->mbox_mem_pool);
3042
James Smart92d7f7b2007-06-17 19:56:38 -05003043 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003044 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3045 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003046 return 0;
3047}
3048
James Smarte59058c2008-08-24 21:49:00 -04003049/**
James Smart93996272008-08-24 21:50:30 -04003050 * lpfc_sli_config_port: Issue config port mailbox command.
James Smarte59058c2008-08-24 21:49:00 -04003051 * @phba: Pointer to HBA context object.
3052 * @sli_mode: sli mode - 2/3
3053 *
3054 * This function is called by the sli intialization code path
3055 * to issue config_port mailbox command. This function restarts the
3056 * HBA firmware and issues a config_port mailbox command to configure
3057 * the SLI interface in the sli mode specified by sli_mode
3058 * variable. The caller is not required to hold any locks.
3059 * The function returns 0 if successful, else returns negative error
3060 * code.
3061 **/
James Smart93996272008-08-24 21:50:30 -04003062int
3063lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003064{
3065 LPFC_MBOXQ_t *pmb;
3066 uint32_t resetcount = 0, rc = 0, done = 0;
3067
3068 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3069 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003070 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003071 return -ENOMEM;
3072 }
3073
James Smarted957682007-06-17 19:56:37 -05003074 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003075 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003076 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003077 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003078 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003079 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003080 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003081 rc = lpfc_sli_chipset_init(phba);
3082 if (rc)
3083 break;
3084
James Smart2e0fef82007-06-17 19:56:36 -05003085 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003086 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003087 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003088 resetcount++;
3089
James Smarted957682007-06-17 19:56:37 -05003090 /* Call pre CONFIG_PORT mailbox command initialization. A
3091 * value of 0 means the call was successful. Any other
3092 * nonzero value is a failure, but if ERESTART is returned,
3093 * the driver may reset the HBA and try again.
3094 */
dea31012005-04-17 16:05:31 -05003095 rc = lpfc_config_port_prep(phba);
3096 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003097 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003098 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003099 } else if (rc)
dea31012005-04-17 16:05:31 -05003100 break;
James Smart2e0fef82007-06-17 19:56:36 -05003101 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003102 lpfc_config_port(phba, pmb);
3103 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003104 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3105 LPFC_SLI3_HBQ_ENABLED |
3106 LPFC_SLI3_CRP_ENABLED |
3107 LPFC_SLI3_INB_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003108 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003110 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003111 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003112 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003113 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003114 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003115 spin_unlock_irq(&phba->hbalock);
3116 rc = -ENXIO;
James Smart34b02dc2008-08-24 21:49:55 -04003117 } else
James Smarted957682007-06-17 19:56:37 -05003118 done = 1;
dea31012005-04-17 16:05:31 -05003119 }
James Smarted957682007-06-17 19:56:37 -05003120 if (!done) {
3121 rc = -EINVAL;
3122 goto do_prep_failed;
3123 }
James Smart34b02dc2008-08-24 21:49:55 -04003124 if (pmb->mb.un.varCfgPort.sli_mode == 3) {
3125 if (!pmb->mb.un.varCfgPort.cMA) {
3126 rc = -ENXIO;
3127 goto do_prep_failed;
3128 }
3129 if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
3130 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3131 phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
3132 } else
3133 phba->max_vpi = 0;
3134 if (pmb->mb.un.varCfgPort.gerbm)
3135 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3136 if (pmb->mb.un.varCfgPort.gcrp)
3137 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3138 if (pmb->mb.un.varCfgPort.ginb) {
3139 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003140 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003141 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3142 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3143 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3144 phba->inb_last_counter =
3145 phba->mbox->us.s3_inb_pgp.counter;
3146 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003147 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003148 phba->port_gp = phba->mbox->us.s3_pgp.port;
3149 phba->inb_ha_copy = NULL;
3150 phba->inb_counter = NULL;
3151 }
3152 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003153 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003154 phba->port_gp = phba->mbox->us.s2.port;
3155 phba->inb_ha_copy = NULL;
3156 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003157 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003158 }
James Smart92d7f7b2007-06-17 19:56:38 -05003159do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003160 mempool_free(pmb, phba->mbox_mem_pool);
3161 return rc;
3162}
3163
James Smarte59058c2008-08-24 21:49:00 -04003164
3165/**
3166 * lpfc_sli_hba_setup: SLI intialization function.
3167 * @phba: Pointer to HBA context object.
3168 *
3169 * This function is the main SLI intialization function. This function
3170 * is called by the HBA intialization code, HBA reset code and HBA
3171 * error attention handler code. Caller is not required to hold any
3172 * locks. This function issues config_port mailbox command to configure
3173 * the SLI, setup iocb rings and HBQ rings. In the end the function
3174 * calls the config_port_post function to issue init_link mailbox
3175 * command and to start the discovery. The function will return zero
3176 * if successful, else it will return negative error code.
3177 **/
James Smarted957682007-06-17 19:56:37 -05003178int
3179lpfc_sli_hba_setup(struct lpfc_hba *phba)
3180{
3181 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003182 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003183
3184 switch (lpfc_sli_mode) {
3185 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003186 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003188 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003189 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003190 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003191 break;
3192 }
James Smarted957682007-06-17 19:56:37 -05003193 mode = 2;
3194 break;
3195 case 0:
3196 case 3:
3197 break;
3198 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003200 "1819 Unrecognized lpfc_sli_mode "
3201 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003202
3203 break;
3204 }
3205
James Smart93996272008-08-24 21:50:30 -04003206 rc = lpfc_sli_config_port(phba, mode);
3207
James Smarted957682007-06-17 19:56:37 -05003208 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003210 "1820 Unable to select SLI-3. "
3211 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003212 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04003213 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05003214 if (rc)
dea31012005-04-17 16:05:31 -05003215 goto lpfc_sli_hba_setup_error;
3216
James Smarted957682007-06-17 19:56:37 -05003217 if (phba->sli_rev == 3) {
3218 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3219 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05003220 } else {
3221 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3222 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003223 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003224 }
3225
3226 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003227 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3228 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05003229 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05003230
3231 if (rc)
3232 goto lpfc_sli_hba_setup_error;
3233
James Smart93996272008-08-24 21:50:30 -04003234 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05003235 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3236 rc = lpfc_sli_hbq_setup(phba);
3237 if (rc)
3238 goto lpfc_sli_hba_setup_error;
3239 }
3240
dea31012005-04-17 16:05:31 -05003241 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3242
3243 rc = lpfc_config_port_post(phba);
3244 if (rc)
3245 goto lpfc_sli_hba_setup_error;
3246
James Smarted957682007-06-17 19:56:37 -05003247 return rc;
3248
James Smart92d7f7b2007-06-17 19:56:38 -05003249lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05003250 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05003251 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003252 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05003253 return rc;
3254}
3255
James Smarte59058c2008-08-24 21:49:00 -04003256
3257/**
3258 * lpfc_mbox_timeout: Timeout call back function for mbox timer.
3259 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05003260 *
James Smarte59058c2008-08-24 21:49:00 -04003261 * This is the callback function for mailbox timer. The mailbox
3262 * timer is armed when a new mailbox command is issued and the timer
3263 * is deleted when the mailbox complete. The function is called by
3264 * the kernel timer code when a mailbox does not complete within
3265 * expected time. This function wakes up the worker thread to
3266 * process the mailbox timeout and returns. All the processing is
3267 * done by the worker thread function lpfc_mbox_timeout_handler.
3268 **/
dea31012005-04-17 16:05:31 -05003269void
3270lpfc_mbox_timeout(unsigned long ptr)
3271{
James Smart92d7f7b2007-06-17 19:56:38 -05003272 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05003273 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05003274 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003275
James Smart2e0fef82007-06-17 19:56:36 -05003276 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05003277 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003278 if (!tmo_posted)
3279 phba->pport->work_port_events |= WORKER_MBOX_TMO;
3280 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
3281
James Smart5e9d9b82008-06-14 22:52:53 -04003282 if (!tmo_posted)
3283 lpfc_worker_wake_up(phba);
3284 return;
dea31012005-04-17 16:05:31 -05003285}
3286
James Smarte59058c2008-08-24 21:49:00 -04003287
3288/**
3289 * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
3290 * @phba: Pointer to HBA context object.
3291 *
3292 * This function is called from worker thread when a mailbox command times out.
3293 * The caller is not required to hold any locks. This function will reset the
3294 * HBA and recover all the pending commands.
3295 **/
dea31012005-04-17 16:05:31 -05003296void
3297lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
3298{
James Smart2e0fef82007-06-17 19:56:36 -05003299 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
3300 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04003301 struct lpfc_sli *psli = &phba->sli;
3302 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003303
dea31012005-04-17 16:05:31 -05003304 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05003305 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003306 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003307 mb->mbxCommand,
3308 phba->pport->port_state,
3309 phba->sli.sli_flag,
3310 phba->sli.mbox_active);
dea31012005-04-17 16:05:31 -05003311
James Smart1dcb58e2007-04-25 09:51:30 -04003312 /* Setting state unknown so lpfc_sli_abort_iocb_ring
3313 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
3314 * it to fail all oustanding SCSI IO.
3315 */
James Smart2e0fef82007-06-17 19:56:36 -05003316 spin_lock_irq(&phba->pport->work_port_lock);
3317 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3318 spin_unlock_irq(&phba->pport->work_port_lock);
3319 spin_lock_irq(&phba->hbalock);
3320 phba->link_state = LPFC_LINK_UNKNOWN;
James Smart1dcb58e2007-04-25 09:51:30 -04003321 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003322 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04003323
3324 pring = &psli->ring[psli->fcp_ring];
3325 lpfc_sli_abort_iocb_ring(phba, pring);
3326
3327 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003328 "0345 Resetting board due to mailbox timeout\n");
James Smart1dcb58e2007-04-25 09:51:30 -04003329 /*
3330 * lpfc_offline calls lpfc_sli_hba_down which will clean up
3331 * on oustanding mailbox commands.
3332 */
James Smart13815c82008-01-11 01:52:48 -05003333 /* If resets are disabled then set error state and return. */
3334 if (!phba->cfg_enable_hba_reset) {
3335 phba->link_state = LPFC_HBA_ERROR;
3336 return;
3337 }
James Smart1dcb58e2007-04-25 09:51:30 -04003338 lpfc_offline_prep(phba);
3339 lpfc_offline(phba);
3340 lpfc_sli_brdrestart(phba);
James Smart58da1ff2008-04-07 10:15:56 -04003341 lpfc_online(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04003342 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003343 return;
3344}
3345
James Smarte59058c2008-08-24 21:49:00 -04003346/**
3347 * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
3348 * @phba: Pointer to HBA context object.
3349 * @pmbox: Pointer to mailbox object.
3350 * @flag: Flag indicating how the mailbox need to be processed.
3351 *
3352 * This function is called by discovery code and HBA management code
3353 * to submit a mailbox command to firmware. This function gets the
3354 * hbalock to protect the data structures.
3355 * The mailbox command can be submitted in polling mode, in which case
3356 * this function will wait in a polling loop for the completion of the
3357 * mailbox.
3358 * If the mailbox is submitted in no_wait mode (not polling) the
3359 * function will submit the command and returns immediately without waiting
3360 * for the mailbox completion. The no_wait is supported only when HBA
3361 * is in SLI2/SLI3 mode - interrupts are enabled.
3362 * The SLI interface allows only one mailbox pending at a time. If the
3363 * mailbox is issued in polling mode and there is already a mailbox
3364 * pending, then the function will return an error. If the mailbox is issued
3365 * in NO_WAIT mode and there is a mailbox pending already, the function
3366 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
3367 * The sli layer owns the mailbox object until the completion of mailbox
3368 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
3369 * return codes the caller owns the mailbox command after the return of
3370 * the function.
3371 **/
dea31012005-04-17 16:05:31 -05003372int
James Smart2e0fef82007-06-17 19:56:36 -05003373lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
dea31012005-04-17 16:05:31 -05003374{
dea31012005-04-17 16:05:31 -05003375 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05003376 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003377 uint32_t status, evtctr;
3378 uint32_t ha_copy;
3379 int i;
James Smart09372822008-01-11 01:52:54 -05003380 unsigned long timeout;
dea31012005-04-17 16:05:31 -05003381 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04003382 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05003383 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04003384 int processing_queue = 0;
3385
3386 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3387 if (!pmbox) {
3388 /* processing mbox queue from intr_handler */
3389 processing_queue = 1;
3390 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3391 pmbox = lpfc_mbox_get(phba);
3392 if (!pmbox) {
3393 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3394 return MBX_SUCCESS;
3395 }
3396 }
dea31012005-04-17 16:05:31 -05003397
James Smarted957682007-06-17 19:56:37 -05003398 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05003399 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05003400 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04003401 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05003402 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003403 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003404 "1806 Mbox x%x failed. No vport\n",
James Smarted957682007-06-17 19:56:37 -05003405 pmbox->mb.mbxCommand);
3406 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04003407 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05003408 }
3409 }
3410
Linas Vepstas8d63f372007-02-14 14:28:36 -06003411 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04003412 if (unlikely(pci_channel_offline(phba->pcidev))) {
3413 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3414 goto out_not_finished;
3415 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06003416
dea31012005-04-17 16:05:31 -05003417 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05003418
dea31012005-04-17 16:05:31 -05003419 mb = &pmbox->mb;
3420 status = MBX_SUCCESS;
3421
James Smart2e0fef82007-06-17 19:56:36 -05003422 if (phba->link_state == LPFC_HBA_ERROR) {
3423 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003424
3425 /* Mbox command <mbxCommand> cannot issue */
James Smart7f5f3d02008-02-08 18:50:14 -05003426 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003427 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003428 }
3429
James Smart92908312006-03-07 15:04:13 -05003430 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
3431 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05003432 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart7f5f3d02008-02-08 18:50:14 -05003433 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003434 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05003435 }
3436
dea31012005-04-17 16:05:31 -05003437 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
3438 /* Polling for a mbox command when another one is already active
3439 * is not allowed in SLI. Also, the driver must have established
3440 * SLI2 mode to queue and process multiple mbox commands.
3441 */
3442
3443 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05003444 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003445
3446 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003447 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003448 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003449 }
3450
3451 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003452 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003453 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003454 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003455 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003456 }
3457
dea31012005-04-17 16:05:31 -05003458 /* Another mailbox command is still being processed, queue this
3459 * command to be processed later.
3460 */
3461 lpfc_mbox_put(phba, pmbox);
3462
3463 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05003464 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003465 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003466 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003467 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
3468 mb->mbxCommand, phba->pport->port_state,
3469 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003470
3471 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05003472 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003473
James Smart858c9f62007-06-17 19:56:39 -05003474 if (pmbox->vport) {
3475 lpfc_debugfs_disc_trc(pmbox->vport,
3476 LPFC_DISC_TRC_MBOX_VPORT,
3477 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
3478 (uint32_t)mb->mbxCommand,
3479 mb->un.varWords[0], mb->un.varWords[1]);
3480 }
3481 else {
3482 lpfc_debugfs_disc_trc(phba->pport,
3483 LPFC_DISC_TRC_MBOX,
3484 "MBOX Bsy: cmd:x%x mb:x%x x%x",
3485 (uint32_t)mb->mbxCommand,
3486 mb->un.varWords[0], mb->un.varWords[1]);
3487 }
3488
James Smart2e0fef82007-06-17 19:56:36 -05003489 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05003490 }
3491
dea31012005-04-17 16:05:31 -05003492 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3493
3494 /* If we are not polling, we MUST be in SLI2 mode */
3495 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05003496 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
3497 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05003498 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003499 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003500 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003501 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003502 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003503 }
3504 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04003505 mod_timer(&psli->mbox_tmo, (jiffies +
3506 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05003507 }
3508
3509 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05003510 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003511 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003512 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003513 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05003514 mb->mbxCommand, phba->pport->port_state,
3515 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003516
James Smart858c9f62007-06-17 19:56:39 -05003517 if (mb->mbxCommand != MBX_HEARTBEAT) {
3518 if (pmbox->vport) {
3519 lpfc_debugfs_disc_trc(pmbox->vport,
3520 LPFC_DISC_TRC_MBOX_VPORT,
3521 "MBOX Send vport: cmd:x%x mb:x%x x%x",
3522 (uint32_t)mb->mbxCommand,
3523 mb->un.varWords[0], mb->un.varWords[1]);
3524 }
3525 else {
3526 lpfc_debugfs_disc_trc(phba->pport,
3527 LPFC_DISC_TRC_MBOX,
3528 "MBOX Send: cmd:x%x mb:x%x x%x",
3529 (uint32_t)mb->mbxCommand,
3530 mb->un.varWords[0], mb->un.varWords[1]);
3531 }
3532 }
3533
dea31012005-04-17 16:05:31 -05003534 psli->slistat.mbox_cmd++;
3535 evtctr = psli->slistat.mbox_event;
3536
3537 /* next set own bit for the adapter and copy over command word */
3538 mb->mbxOwner = OWN_CHIP;
3539
3540 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003541 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04003542 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003543 } else {
James Smart92908312006-03-07 15:04:13 -05003544 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05003545 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04003546 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003547 }
3548
3549 /* First copy mbox command data to HBA SLIM, skip past first
3550 word */
3551 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3552 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
3553 MAILBOX_CMD_SIZE - sizeof (uint32_t));
3554
3555 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04003556 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05003557 to_slim = phba->MBslimaddr;
3558 writel(ldata, to_slim);
3559 readl(to_slim); /* flush */
3560
3561 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3562 /* switch over to host mailbox */
3563 psli->sli_flag |= LPFC_SLI2_ACTIVE;
3564 }
3565 }
3566
3567 wmb();
dea31012005-04-17 16:05:31 -05003568
3569 switch (flag) {
3570 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05003571 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05003572 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05003573 /* Interrupt board to do it */
3574 writel(CA_MBATT, phba->CAregaddr);
3575 readl(phba->CAregaddr); /* flush */
3576 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05003577 break;
3578
3579 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05003580 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05003581 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05003582 /* Interrupt board to do it */
3583 writel(CA_MBATT, phba->CAregaddr);
3584 readl(phba->CAregaddr); /* flush */
3585
dea31012005-04-17 16:05:31 -05003586 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3587 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04003588 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003589 word0 = le32_to_cpu(word0);
3590 } else {
3591 /* First read mbox status word */
3592 word0 = readl(phba->MBslimaddr);
3593 }
3594
3595 /* Read the HBA Host Attention Register */
3596 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05003597 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3598 mb->mbxCommand) *
3599 1000) + jiffies;
3600 i = 0;
dea31012005-04-17 16:05:31 -05003601 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003602 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
3603 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05003604 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05003605 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05003606 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003607 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05003608 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04003609 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003610 }
3611
3612 /* Check if we took a mbox interrupt while we were
3613 polling */
3614 if (((word0 & OWN_CHIP) != OWN_CHIP)
3615 && (evtctr != psli->slistat.mbox_event))
3616 break;
3617
James Smart09372822008-01-11 01:52:54 -05003618 if (i++ > 10) {
3619 spin_unlock_irqrestore(&phba->hbalock,
3620 drvr_flag);
3621 msleep(1);
3622 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3623 }
dea31012005-04-17 16:05:31 -05003624
3625 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3626 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04003627 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003628 word0 = le32_to_cpu(word0);
3629 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3630 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04003631 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05003632 /* Check real SLIM for any errors */
3633 slimword0 = readl(phba->MBslimaddr);
3634 slimmb = (MAILBOX_t *) & slimword0;
3635 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
3636 && slimmb->mbxStatus) {
3637 psli->sli_flag &=
3638 ~LPFC_SLI2_ACTIVE;
3639 word0 = slimword0;
3640 }
3641 }
3642 } else {
3643 /* First copy command data */
3644 word0 = readl(phba->MBslimaddr);
3645 }
3646 /* Read the HBA Host Attention Register */
3647 ha_copy = readl(phba->HAregaddr);
3648 }
3649
3650 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003651 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04003652 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003653 } else {
3654 /* First copy command data */
3655 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
3656 MAILBOX_CMD_SIZE);
3657 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
3658 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05003659 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05003660 phba->MBslimaddr + DMP_RSP_OFFSET,
3661 mb->un.varDmp.word_cnt);
3662 }
3663 }
3664
3665 writel(HA_MBATT, phba->HAregaddr);
3666 readl(phba->HAregaddr); /* flush */
3667
3668 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3669 status = mb->mbxStatus;
3670 }
3671
James Smart2e0fef82007-06-17 19:56:36 -05003672 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3673 return status;
James Smart58da1ff2008-04-07 10:15:56 -04003674
3675out_not_finished:
3676 if (processing_queue) {
3677 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
3678 lpfc_mbox_cmpl_put(phba, pmbox);
3679 }
3680 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003681}
3682
James Smarte59058c2008-08-24 21:49:00 -04003683/**
3684 * __lpfc_sli_ringtx_put: Add an iocb to the txq.
3685 * @phba: Pointer to HBA context object.
3686 * @pring: Pointer to driver SLI ring object.
3687 * @piocb: Pointer to address of newly added command iocb.
3688 *
3689 * This function is called with hbalock held to add a command
3690 * iocb to the txq when SLI layer cannot submit the command iocb
3691 * to the ring.
3692 **/
James Smart858c9f62007-06-17 19:56:39 -05003693static void
James Smart92d7f7b2007-06-17 19:56:38 -05003694__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003695 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05003696{
3697 /* Insert the caller's iocb in the txq tail for later processing. */
3698 list_add_tail(&piocb->list, &pring->txq);
3699 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05003700}
3701
James Smarte59058c2008-08-24 21:49:00 -04003702/**
3703 * lpfc_sli_next_iocb: Get the next iocb in the txq.
3704 * @phba: Pointer to HBA context object.
3705 * @pring: Pointer to driver SLI ring object.
3706 * @piocb: Pointer to address of newly added command iocb.
3707 *
3708 * This function is called with hbalock held before a new
3709 * iocb is submitted to the firmware. This function checks
3710 * txq to flush the iocbs in txq to Firmware before
3711 * submitting new iocbs to the Firmware.
3712 * If there are iocbs in the txq which need to be submitted
3713 * to firmware, lpfc_sli_next_iocb returns the first element
3714 * of the txq after dequeuing it from txq.
3715 * If there is no iocb in the txq then the function will return
3716 * *piocb and *piocb is set to NULL. Caller needs to check
3717 * *piocb to find if there are more commands in the txq.
3718 **/
dea31012005-04-17 16:05:31 -05003719static struct lpfc_iocbq *
3720lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003721 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05003722{
3723 struct lpfc_iocbq * nextiocb;
3724
3725 nextiocb = lpfc_sli_ringtx_get(phba, pring);
3726 if (!nextiocb) {
3727 nextiocb = *piocb;
3728 *piocb = NULL;
3729 }
3730
3731 return nextiocb;
3732}
3733
James Smarte59058c2008-08-24 21:49:00 -04003734/**
3735 * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
3736 * @phba: Pointer to HBA context object.
3737 * @pring: Pointer to driver SLI ring object.
3738 * @piocb: Pointer to command iocb.
3739 * @flag: Flag indicating if this command can be put into txq.
3740 *
3741 * __lpfc_sli_issue_iocb is used by other functions in the driver
3742 * to issue an iocb command to the HBA. If the PCI slot is recovering
3743 * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
3744 * flag is turned on, the function returns IOCB_ERROR.
3745 * When the link is down, this function allows only iocbs for
3746 * posting buffers.
3747 * This function finds next available slot in the command ring and
3748 * posts the command to the available slot and writes the port
3749 * attention register to request HBA start processing new iocb.
3750 * If there is no slot available in the ring and
3751 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
3752 * txq, otherwise the function returns IOCB_BUSY.
3753 *
3754 * This function is called with hbalock held.
3755 * The function will return success after it successfully submit the
3756 * iocb to firmware or after adding to the txq.
3757 **/
James Smart98c9ea52007-10-27 13:37:33 -04003758static int
James Smart92d7f7b2007-06-17 19:56:38 -05003759__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003760 struct lpfc_iocbq *piocb, uint32_t flag)
3761{
3762 struct lpfc_iocbq *nextiocb;
3763 IOCB_t *iocb;
3764
James Smart92d7f7b2007-06-17 19:56:38 -05003765 if (piocb->iocb_cmpl && (!piocb->vport) &&
3766 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
3767 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
3768 lpfc_printf_log(phba, KERN_ERR,
3769 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003770 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003771 piocb->iocb.ulpCommand);
3772 dump_stack();
3773 return IOCB_ERROR;
3774 }
3775
3776
Linas Vepstas8d63f372007-02-14 14:28:36 -06003777 /* If the PCI channel is in offline state, do not post iocbs. */
3778 if (unlikely(pci_channel_offline(phba->pcidev)))
3779 return IOCB_ERROR;
3780
dea31012005-04-17 16:05:31 -05003781 /*
3782 * We should never get an IOCB if we are in a < LINK_DOWN state
3783 */
James Smart2e0fef82007-06-17 19:56:36 -05003784 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003785 return IOCB_ERROR;
3786
3787 /*
3788 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04003789 * outstanding event.
dea31012005-04-17 16:05:31 -05003790 */
James Smart0b727fe2007-10-27 13:37:25 -04003791 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05003792 goto iocb_busy;
3793
James Smart2e0fef82007-06-17 19:56:36 -05003794 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05003795 /*
James Smart2680eea2007-04-25 09:52:55 -04003796 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05003797 * can be issued if the link is not up.
3798 */
3799 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04003800 case CMD_GEN_REQUEST64_CR:
3801 case CMD_GEN_REQUEST64_CX:
3802 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
3803 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
3804 FC_FCP_CMND) ||
3805 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
3806 MENLO_TRANSPORT_TYPE))
3807
3808 goto iocb_busy;
3809 break;
dea31012005-04-17 16:05:31 -05003810 case CMD_QUE_RING_BUF_CN:
3811 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05003812 /*
3813 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
3814 * completion, iocb_cmpl MUST be 0.
3815 */
3816 if (piocb->iocb_cmpl)
3817 piocb->iocb_cmpl = NULL;
3818 /*FALLTHROUGH*/
3819 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04003820 case CMD_CLOSE_XRI_CN:
3821 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05003822 break;
3823 default:
3824 goto iocb_busy;
3825 }
3826
3827 /*
3828 * For FCP commands, we must be in a state where we can process link
3829 * attention events.
3830 */
3831 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05003832 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05003833 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05003834 }
dea31012005-04-17 16:05:31 -05003835
dea31012005-04-17 16:05:31 -05003836 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
3837 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
3838 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
3839
3840 if (iocb)
3841 lpfc_sli_update_ring(phba, pring);
3842 else
3843 lpfc_sli_update_full_ring(phba, pring);
3844
3845 if (!piocb)
3846 return IOCB_SUCCESS;
3847
3848 goto out_busy;
3849
3850 iocb_busy:
3851 pring->stats.iocb_cmd_delay++;
3852
3853 out_busy:
3854
3855 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003856 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05003857 return IOCB_SUCCESS;
3858 }
3859
3860 return IOCB_BUSY;
3861}
3862
James Smart92d7f7b2007-06-17 19:56:38 -05003863
James Smarte59058c2008-08-24 21:49:00 -04003864/**
3865 * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
3866 * @phba: Pointer to HBA context object.
3867 * @pring: Pointer to driver SLI ring object.
3868 * @piocb: Pointer to command iocb.
3869 * @flag: Flag indicating if this command can be put into txq.
3870 *
3871 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
3872 * function. This function gets the hbalock and calls
3873 * __lpfc_sli_issue_iocb function and will return the error returned
3874 * by __lpfc_sli_issue_iocb function. This wrapper is used by
3875 * functions which do not hold hbalock.
3876 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003877int
3878lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3879 struct lpfc_iocbq *piocb, uint32_t flag)
3880{
3881 unsigned long iflags;
3882 int rc;
3883
3884 spin_lock_irqsave(&phba->hbalock, iflags);
3885 rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
3886 spin_unlock_irqrestore(&phba->hbalock, iflags);
3887
3888 return rc;
3889}
3890
James Smarte59058c2008-08-24 21:49:00 -04003891/**
3892 * lpfc_extra_ring_setup: Extra ring setup function.
3893 * @phba: Pointer to HBA context object.
3894 *
3895 * This function is called while driver attaches with the
3896 * HBA to setup the extra ring. The extra ring is used
3897 * only when driver needs to support target mode functionality
3898 * or IP over FC functionalities.
3899 *
3900 * This function is called with no lock held.
3901 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003902static int
3903lpfc_extra_ring_setup( struct lpfc_hba *phba)
3904{
3905 struct lpfc_sli *psli;
3906 struct lpfc_sli_ring *pring;
3907
3908 psli = &phba->sli;
3909
3910 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05003911
3912 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003913 pring = &psli->ring[psli->fcp_ring];
3914 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3915 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3916 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3917 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3918
James Smarta4bc3372006-12-02 13:34:16 -05003919 /* and give them to the extra ring */
3920 pring = &psli->ring[psli->extra_ring];
3921
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003922 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3923 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3924 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3925 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3926
3927 /* Setup default profile for this ring */
3928 pring->iotag_max = 4096;
3929 pring->num_mask = 1;
3930 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05003931 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
3932 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003933 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
3934 return 0;
3935}
3936
James Smarte59058c2008-08-24 21:49:00 -04003937/**
3938 * lpfc_sli_async_event_handler: ASYNC iocb handler function.
3939 * @phba: Pointer to HBA context object.
3940 * @pring: Pointer to driver SLI ring object.
3941 * @iocbq: Pointer to iocb object.
3942 *
3943 * This function is called by the slow ring event handler
3944 * function when there is an ASYNC event iocb in the ring.
3945 * This function is called with no lock held.
3946 * Currently this function handles only temperature related
3947 * ASYNC events. The function decodes the temperature sensor
3948 * event message and posts events for the management applications.
3949 **/
James Smart98c9ea52007-10-27 13:37:33 -04003950static void
James Smart57127f12007-10-27 13:37:05 -04003951lpfc_sli_async_event_handler(struct lpfc_hba * phba,
3952 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
3953{
3954 IOCB_t *icmd;
3955 uint16_t evt_code;
3956 uint16_t temp;
3957 struct temp_event temp_event_data;
3958 struct Scsi_Host *shost;
3959
3960 icmd = &iocbq->iocb;
3961 evt_code = icmd->un.asyncstat.evt_code;
3962 temp = icmd->ulpContext;
3963
3964 if ((evt_code != ASYNC_TEMP_WARN) &&
3965 (evt_code != ASYNC_TEMP_SAFE)) {
3966 lpfc_printf_log(phba,
3967 KERN_ERR,
3968 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003969 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smart57127f12007-10-27 13:37:05 -04003970 " evt_code 0x%x\n",
3971 pring->ringno,
3972 icmd->un.asyncstat.evt_code);
3973 return;
3974 }
3975 temp_event_data.data = (uint32_t)temp;
3976 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
3977 if (evt_code == ASYNC_TEMP_WARN) {
3978 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
3979 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003980 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003981 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04003982 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04003983 "corrective action. temperature : %d Celsius\n",
3984 temp);
3985 }
3986 if (evt_code == ASYNC_TEMP_SAFE) {
3987 temp_event_data.event_code = LPFC_NORMAL_TEMP;
3988 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003989 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003990 LOG_TEMP,
3991 "0340 Adapter temperature is OK now. "
3992 "temperature : %d Celsius\n",
3993 temp);
3994 }
3995
3996 /* Send temperature change event to applications */
3997 shost = lpfc_shost_from_vport(phba->pport);
3998 fc_host_post_vendor_event(shost, fc_get_event_number(),
3999 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05004000 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04004001
4002}
4003
4004
James Smarte59058c2008-08-24 21:49:00 -04004005/**
4006 * lpfc_sli_setup: SLI ring setup function.
4007 * @phba: Pointer to HBA context object.
4008 *
4009 * lpfc_sli_setup sets up rings of the SLI interface with
4010 * number of iocbs per ring and iotags. This function is
4011 * called while driver attach to the HBA and before the
4012 * interrupts are enabled. So there is no need for locking.
4013 *
4014 * This function always returns 0.
4015 **/
dea31012005-04-17 16:05:31 -05004016int
4017lpfc_sli_setup(struct lpfc_hba *phba)
4018{
James Smarted957682007-06-17 19:56:37 -05004019 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05004020 struct lpfc_sli *psli = &phba->sli;
4021 struct lpfc_sli_ring *pring;
4022
4023 psli->num_rings = MAX_CONFIGURED_RINGS;
4024 psli->sli_flag = 0;
4025 psli->fcp_ring = LPFC_FCP_RING;
4026 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05004027 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05004028
James Bottomley604a3e32005-10-29 10:28:33 -05004029 psli->iocbq_lookup = NULL;
4030 psli->iocbq_lookup_len = 0;
4031 psli->last_iotag = 0;
4032
dea31012005-04-17 16:05:31 -05004033 for (i = 0; i < psli->num_rings; i++) {
4034 pring = &psli->ring[i];
4035 switch (i) {
4036 case LPFC_FCP_RING: /* ring 0 - FCP */
4037 /* numCiocb and numRiocb are used in config_port */
4038 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
4039 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
4040 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
4041 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
4042 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
4043 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004044 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004045 SLI3_IOCB_CMD_SIZE :
4046 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004047 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004048 SLI3_IOCB_RSP_SIZE :
4049 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05004050 pring->iotag_ctr = 0;
4051 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05004052 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05004053 pring->fast_iotag = pring->iotag_max;
4054 pring->num_mask = 0;
4055 break;
James Smarta4bc3372006-12-02 13:34:16 -05004056 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05004057 /* numCiocb and numRiocb are used in config_port */
4058 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
4059 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004060 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004061 SLI3_IOCB_CMD_SIZE :
4062 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004063 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004064 SLI3_IOCB_RSP_SIZE :
4065 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05004066 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05004067 pring->num_mask = 0;
4068 break;
4069 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
4070 /* numCiocb and numRiocb are used in config_port */
4071 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
4072 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004073 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004074 SLI3_IOCB_CMD_SIZE :
4075 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004076 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004077 SLI3_IOCB_RSP_SIZE :
4078 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05004079 pring->fast_iotag = 0;
4080 pring->iotag_ctr = 0;
4081 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04004082 pring->lpfc_sli_rcv_async_status =
4083 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05004084 pring->num_mask = 4;
4085 pring->prt[0].profile = 0; /* Mask 0 */
4086 pring->prt[0].rctl = FC_ELS_REQ;
4087 pring->prt[0].type = FC_ELS_DATA;
4088 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004089 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004090 pring->prt[1].profile = 0; /* Mask 1 */
4091 pring->prt[1].rctl = FC_ELS_RSP;
4092 pring->prt[1].type = FC_ELS_DATA;
4093 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004094 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004095 pring->prt[2].profile = 0; /* Mask 2 */
4096 /* NameServer Inquiry */
4097 pring->prt[2].rctl = FC_UNSOL_CTL;
4098 /* NameServer */
4099 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
4100 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004101 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004102 pring->prt[3].profile = 0; /* Mask 3 */
4103 /* NameServer response */
4104 pring->prt[3].rctl = FC_SOL_CTL;
4105 /* NameServer */
4106 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
4107 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004108 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004109 break;
4110 }
James Smarted957682007-06-17 19:56:37 -05004111 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05004112 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05004113 }
James Smarted957682007-06-17 19:56:37 -05004114 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05004115 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04004116 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
4117 "SLI2 SLIM Data: x%x x%lx\n",
4118 phba->brd_no, totiocbsize,
4119 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05004120 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004121 if (phba->cfg_multi_ring_support == 2)
4122 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05004123
4124 return 0;
4125}
4126
James Smarte59058c2008-08-24 21:49:00 -04004127/**
4128 * lpfc_sli_queue_setup: Queue initialization function.
4129 * @phba: Pointer to HBA context object.
4130 *
4131 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
4132 * ring. This function also initializes ring indices of each ring.
4133 * This function is called during the initialization of the SLI
4134 * interface of an HBA.
4135 * This function is called with no lock held and always returns
4136 * 1.
4137 **/
dea31012005-04-17 16:05:31 -05004138int
James Smart2e0fef82007-06-17 19:56:36 -05004139lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004140{
4141 struct lpfc_sli *psli;
4142 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05004143 int i;
dea31012005-04-17 16:05:31 -05004144
4145 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05004146 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004147 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05004148 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05004149 /* Initialize list headers for txq and txcmplq as double linked lists */
4150 for (i = 0; i < psli->num_rings; i++) {
4151 pring = &psli->ring[i];
4152 pring->ringno = i;
4153 pring->next_cmdidx = 0;
4154 pring->local_getidx = 0;
4155 pring->cmdidx = 0;
4156 INIT_LIST_HEAD(&pring->txq);
4157 INIT_LIST_HEAD(&pring->txcmplq);
4158 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05004159 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05004160 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05004161 }
James Smart2e0fef82007-06-17 19:56:36 -05004162 spin_unlock_irq(&phba->hbalock);
4163 return 1;
dea31012005-04-17 16:05:31 -05004164}
4165
James Smarte59058c2008-08-24 21:49:00 -04004166/**
4167 * lpfc_sli_host_down: Vport cleanup function.
4168 * @vport: Pointer to virtual port object.
4169 *
4170 * lpfc_sli_host_down is called to clean up the resources
4171 * associated with a vport before destroying virtual
4172 * port data structures.
4173 * This function does following operations:
4174 * - Free discovery resources associated with this virtual
4175 * port.
4176 * - Free iocbs associated with this virtual port in
4177 * the txq.
4178 * - Send abort for all iocb commands associated with this
4179 * vport in txcmplq.
4180 *
4181 * This function is called with no lock held and always returns 1.
4182 **/
dea31012005-04-17 16:05:31 -05004183int
James Smart92d7f7b2007-06-17 19:56:38 -05004184lpfc_sli_host_down(struct lpfc_vport *vport)
4185{
James Smart858c9f62007-06-17 19:56:39 -05004186 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004187 struct lpfc_hba *phba = vport->phba;
4188 struct lpfc_sli *psli = &phba->sli;
4189 struct lpfc_sli_ring *pring;
4190 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004191 int i;
4192 unsigned long flags = 0;
4193 uint16_t prev_pring_flag;
4194
4195 lpfc_cleanup_discovery_resources(vport);
4196
4197 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004198 for (i = 0; i < psli->num_rings; i++) {
4199 pring = &psli->ring[i];
4200 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04004201 /* Only slow rings */
4202 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004203 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004204 /* Set the lpfc data pending flag */
4205 set_bit(LPFC_DATA_READY, &phba->data_flags);
4206 }
James Smart92d7f7b2007-06-17 19:56:38 -05004207 /*
4208 * Error everything on the txq since these iocbs have not been
4209 * given to the FW yet.
4210 */
James Smart92d7f7b2007-06-17 19:56:38 -05004211 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4212 if (iocb->vport != vport)
4213 continue;
James Smart858c9f62007-06-17 19:56:39 -05004214 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004215 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05004216 }
4217
4218 /* Next issue ABTS for everything on the txcmplq */
4219 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
4220 list) {
4221 if (iocb->vport != vport)
4222 continue;
4223 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4224 }
4225
4226 pring->flag = prev_pring_flag;
4227 }
4228
4229 spin_unlock_irqrestore(&phba->hbalock, flags);
4230
James Smart858c9f62007-06-17 19:56:39 -05004231 while (!list_empty(&completions)) {
4232 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
4233
4234 if (!iocb->iocb_cmpl)
4235 lpfc_sli_release_iocbq(phba, iocb);
4236 else {
4237 iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4238 iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
4239 (iocb->iocb_cmpl) (phba, iocb, iocb);
4240 }
4241 }
James Smart92d7f7b2007-06-17 19:56:38 -05004242 return 1;
4243}
4244
James Smarte59058c2008-08-24 21:49:00 -04004245/**
4246 * lpfc_sli_hba_down: Resource cleanup function for the HBA.
4247 * @phba: Pointer to HBA context object.
4248 *
4249 * This function cleans up all iocb, buffers, mailbox commands
4250 * while shutting down the HBA. This function is called with no
4251 * lock held and always returns 1.
4252 * This function does the following to cleanup driver resources:
4253 * - Free discovery resources for each virtual port
4254 * - Cleanup any pending fabric iocbs
4255 * - Iterate through the iocb txq and free each entry
4256 * in the list.
4257 * - Free up any buffer posted to the HBA
4258 * - Free mailbox commands in the mailbox queue.
4259 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004260int
James Smart2e0fef82007-06-17 19:56:36 -05004261lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004262{
James Smart2534ba72007-04-25 09:52:20 -04004263 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05004264 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004265 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05004266 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05004267 LPFC_MBOXQ_t *pmb;
James Smart2534ba72007-04-25 09:52:20 -04004268 struct lpfc_iocbq *iocb;
4269 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004270 int i;
4271 unsigned long flags = 0;
4272
dea31012005-04-17 16:05:31 -05004273 lpfc_hba_down_prep(phba);
4274
James Smart92d7f7b2007-06-17 19:56:38 -05004275 lpfc_fabric_abort_hba(phba);
4276
James Smart2e0fef82007-06-17 19:56:36 -05004277 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004278 for (i = 0; i < psli->num_rings; i++) {
4279 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04004280 /* Only slow rings */
4281 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004282 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004283 /* Set the lpfc data pending flag */
4284 set_bit(LPFC_DATA_READY, &phba->data_flags);
4285 }
dea31012005-04-17 16:05:31 -05004286
4287 /*
4288 * Error everything on the txq since these iocbs have not been
4289 * given to the FW yet.
4290 */
James Smart2534ba72007-04-25 09:52:20 -04004291 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05004292 pring->txq_cnt = 0;
4293
dea31012005-04-17 16:05:31 -05004294 }
James Smart2e0fef82007-06-17 19:56:36 -05004295 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004296
James Smart2534ba72007-04-25 09:52:20 -04004297 while (!list_empty(&completions)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004298 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
James Smart2534ba72007-04-25 09:52:20 -04004299 cmd = &iocb->iocb;
James Smart2534ba72007-04-25 09:52:20 -04004300
James Smart2e0fef82007-06-17 19:56:36 -05004301 if (!iocb->iocb_cmpl)
4302 lpfc_sli_release_iocbq(phba, iocb);
4303 else {
James Smart2534ba72007-04-25 09:52:20 -04004304 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4305 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
4306 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05004307 }
James Smart2534ba72007-04-25 09:52:20 -04004308 }
4309
James Smart0ff10d42008-01-11 01:52:36 -05004310 spin_lock_irqsave(&phba->hbalock, flags);
4311 list_splice_init(&phba->elsbuf, &completions);
4312 phba->elsbuf_cnt = 0;
4313 phba->elsbuf_prev_cnt = 0;
4314 spin_unlock_irqrestore(&phba->hbalock, flags);
4315
4316 while (!list_empty(&completions)) {
4317 list_remove_head(&completions, buf_ptr,
4318 struct lpfc_dmabuf, list);
4319 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4320 kfree(buf_ptr);
4321 }
4322
dea31012005-04-17 16:05:31 -05004323 /* Return any active mbox cmds */
4324 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05004325 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004326
4327 spin_lock(&phba->pport->work_port_lock);
4328 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4329 spin_unlock(&phba->pport->work_port_lock);
4330
James Smart97eab632008-04-07 10:16:05 -04004331 /* Return any pending or completed mbox cmds */
4332 list_splice_init(&phba->sli.mboxq, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004333 if (psli->mbox_active) {
4334 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05004335 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004336 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05004337 }
James Smart92d7f7b2007-06-17 19:56:38 -05004338 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004339 spin_unlock_irqrestore(&phba->hbalock, flags);
4340
4341 while (!list_empty(&completions)) {
4342 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05004343 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
James Smart97eab632008-04-07 10:16:05 -04004344 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05004345 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05004346 }
dea31012005-04-17 16:05:31 -05004347 return 1;
4348}
4349
James Smarte59058c2008-08-24 21:49:00 -04004350/**
4351 * lpfc_sli_pcimem_bcopy: SLI memory copy function.
4352 * @srcp: Source memory pointer.
4353 * @destp: Destination memory pointer.
4354 * @cnt: Number of words required to be copied.
4355 *
4356 * This function is used for copying data between driver memory
4357 * and the SLI memory. This function also changes the endianness
4358 * of each word if native endianness is different from SLI
4359 * endianness. This function can be called with or without
4360 * lock.
4361 **/
dea31012005-04-17 16:05:31 -05004362void
4363lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
4364{
4365 uint32_t *src = srcp;
4366 uint32_t *dest = destp;
4367 uint32_t ldata;
4368 int i;
4369
4370 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
4371 ldata = *src;
4372 ldata = le32_to_cpu(ldata);
4373 *dest = ldata;
4374 src++;
4375 dest++;
4376 }
4377}
4378
James Smarte59058c2008-08-24 21:49:00 -04004379
4380/**
4381 * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
4382 * @phba: Pointer to HBA context object.
4383 * @pring: Pointer to driver SLI ring object.
4384 * @mp: Pointer to driver buffer object.
4385 *
4386 * This function is called with no lock held.
4387 * It always return zero after adding the buffer to the postbufq
4388 * buffer list.
4389 **/
dea31012005-04-17 16:05:31 -05004390int
James Smart2e0fef82007-06-17 19:56:36 -05004391lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4392 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05004393{
4394 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
4395 later */
James Smart2e0fef82007-06-17 19:56:36 -05004396 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004397 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05004398 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05004399 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004400 return 0;
4401}
4402
James Smarte59058c2008-08-24 21:49:00 -04004403/**
4404 * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
4405 * using CMD_QUE_XRI64_CX iocb.
4406 * @phba: Pointer to HBA context object.
4407 *
4408 * When HBQ is enabled, buffers are searched based on tags. This function
4409 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
4410 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
4411 * does not conflict with tags of buffer posted for unsolicited events.
4412 * The function returns the allocated tag. The function is called with
4413 * no locks held.
4414 **/
James Smart76bb24e2007-10-27 13:38:00 -04004415uint32_t
4416lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
4417{
4418 spin_lock_irq(&phba->hbalock);
4419 phba->buffer_tag_count++;
4420 /*
4421 * Always set the QUE_BUFTAG_BIT to distiguish between
4422 * a tag assigned by HBQ.
4423 */
4424 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
4425 spin_unlock_irq(&phba->hbalock);
4426 return phba->buffer_tag_count;
4427}
4428
James Smarte59058c2008-08-24 21:49:00 -04004429/**
4430 * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
4431 * posted using CMD_QUE_XRI64_CX iocb.
4432 * @phba: Pointer to HBA context object.
4433 * @pring: Pointer to driver SLI ring object.
4434 * @tag: Buffer tag.
4435 *
4436 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
4437 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
4438 * iocb is posted to the response ring with the tag of the buffer.
4439 * This function searches the pring->postbufq list using the tag
4440 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
4441 * iocb. If the buffer is found then lpfc_dmabuf object of the
4442 * buffer is returned to the caller else NULL is returned.
4443 * This function is called with no lock held.
4444 **/
James Smart76bb24e2007-10-27 13:38:00 -04004445struct lpfc_dmabuf *
4446lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4447 uint32_t tag)
4448{
4449 struct lpfc_dmabuf *mp, *next_mp;
4450 struct list_head *slp = &pring->postbufq;
4451
4452 /* Search postbufq, from the begining, looking for a match on tag */
4453 spin_lock_irq(&phba->hbalock);
4454 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4455 if (mp->buffer_tag == tag) {
4456 list_del_init(&mp->list);
4457 pring->postbufq_cnt--;
4458 spin_unlock_irq(&phba->hbalock);
4459 return mp;
4460 }
4461 }
4462
4463 spin_unlock_irq(&phba->hbalock);
4464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04004465 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04004466 "ring %d Data x%lx x%p x%p x%x\n",
4467 pring->ringno, (unsigned long) tag,
4468 slp->next, slp->prev, pring->postbufq_cnt);
4469
4470 return NULL;
4471}
dea31012005-04-17 16:05:31 -05004472
James Smarte59058c2008-08-24 21:49:00 -04004473/**
4474 * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
4475 * unsolicited ct and els events.
4476 * @phba: Pointer to HBA context object.
4477 * @pring: Pointer to driver SLI ring object.
4478 * @phys: DMA address of the buffer.
4479 *
4480 * This function searches the buffer list using the dma_address
4481 * of unsolicited event to find the driver's lpfc_dmabuf object
4482 * corresponding to the dma_address. The function returns the
4483 * lpfc_dmabuf object if a buffer is found else it returns NULL.
4484 * This function is called by the ct and els unsolicited event
4485 * handlers to get the buffer associated with the unsolicited
4486 * event.
4487 *
4488 * This function is called with no lock held.
4489 **/
dea31012005-04-17 16:05:31 -05004490struct lpfc_dmabuf *
4491lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4492 dma_addr_t phys)
4493{
4494 struct lpfc_dmabuf *mp, *next_mp;
4495 struct list_head *slp = &pring->postbufq;
4496
4497 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05004498 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004499 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4500 if (mp->phys == phys) {
4501 list_del_init(&mp->list);
4502 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004503 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004504 return mp;
4505 }
4506 }
4507
James Smart2e0fef82007-06-17 19:56:36 -05004508 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004510 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05004511 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004512 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05004513 slp->next, slp->prev, pring->postbufq_cnt);
4514 return NULL;
4515}
4516
James Smarte59058c2008-08-24 21:49:00 -04004517/**
4518 * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
4519 * @phba: Pointer to HBA context object.
4520 * @cmdiocb: Pointer to driver command iocb object.
4521 * @rspiocb: Pointer to driver response iocb object.
4522 *
4523 * This function is the completion handler for the abort iocbs for
4524 * ELS commands. This function is called from the ELS ring event
4525 * handler with no lock held. This function frees memory resources
4526 * associated with the abort iocb.
4527 **/
dea31012005-04-17 16:05:31 -05004528static void
James Smart2e0fef82007-06-17 19:56:36 -05004529lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4530 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05004531{
James Smart2e0fef82007-06-17 19:56:36 -05004532 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04004533 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05004534 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04004535 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4536
4537 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04004538
4539 if (irsp->ulpStatus) {
4540 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
4541 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
4542
James Smart2e0fef82007-06-17 19:56:36 -05004543 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004544 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
4545 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
4546
James Smart92d7f7b2007-06-17 19:56:38 -05004547 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004548 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05004549 "with tag %x context %x, abort status %x, "
4550 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04004551 abort_iocb, abort_iotag, abort_context,
4552 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04004553
4554 /*
James Smart58da1ff2008-04-07 10:15:56 -04004555 * If the iocb is not found in Firmware queue the iocb
4556 * might have completed already. Do not free it again.
4557 */
James Smart9b379602008-04-07 10:16:00 -04004558 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04004559 spin_unlock_irq(&phba->hbalock);
4560 lpfc_sli_release_iocbq(phba, cmdiocb);
4561 return;
4562 }
4563 /*
James Smart2680eea2007-04-25 09:52:55 -04004564 * make sure we have the right iocbq before taking it
4565 * off the txcmplq and try to call completion routine.
4566 */
James Smart2e0fef82007-06-17 19:56:36 -05004567 if (!abort_iocb ||
4568 abort_iocb->iocb.ulpContext != abort_context ||
4569 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
4570 spin_unlock_irq(&phba->hbalock);
4571 else {
James Smart92d7f7b2007-06-17 19:56:38 -05004572 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04004573 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004574 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004575
James Smart0ff10d42008-01-11 01:52:36 -05004576 /* Firmware could still be in progress of DMAing
4577 * payload, so don't free data buffer till after
4578 * a hbeat.
4579 */
4580 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
4581
James Smart92d7f7b2007-06-17 19:56:38 -05004582 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4583 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4584 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
4585 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04004586 }
4587 }
4588
James Bottomley604a3e32005-10-29 10:28:33 -05004589 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05004590 return;
4591}
4592
James Smarte59058c2008-08-24 21:49:00 -04004593/**
4594 * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
4595 * @phba: Pointer to HBA context object.
4596 * @cmdiocb: Pointer to driver command iocb object.
4597 * @rspiocb: Pointer to driver response iocb object.
4598 *
4599 * The function is called from SLI ring event handler with no
4600 * lock held. This function is the completion handler for ELS commands
4601 * which are aborted. The function frees memory resources used for
4602 * the aborted ELS commands.
4603 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004604static void
4605lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4606 struct lpfc_iocbq *rspiocb)
4607{
4608 IOCB_t *irsp = &rspiocb->iocb;
4609
4610 /* ELS cmd tag <ulpIoTag> completes */
4611 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04004612 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004613 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004614 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05004615 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05004616 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
4617 lpfc_ct_free_iocb(phba, cmdiocb);
4618 else
4619 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05004620 return;
4621}
4622
James Smarte59058c2008-08-24 21:49:00 -04004623/**
4624 * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
4625 * @phba: Pointer to HBA context object.
4626 * @pring: Pointer to driver SLI ring object.
4627 * @cmdiocb: Pointer to driver command iocb object.
4628 *
4629 * This function issues an abort iocb for the provided command
4630 * iocb. This function is called with hbalock held.
4631 * The function returns 0 when it fails due to memory allocation
4632 * failure or when the command iocb is an abort request.
4633 **/
dea31012005-04-17 16:05:31 -05004634int
James Smart2e0fef82007-06-17 19:56:36 -05004635lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4636 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05004637{
James Smart2e0fef82007-06-17 19:56:36 -05004638 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004639 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05004640 IOCB_t *icmd = NULL;
4641 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04004642 int retval = IOCB_ERROR;
4643
James Smart92d7f7b2007-06-17 19:56:38 -05004644 /*
4645 * There are certain command types we don't want to abort. And we
4646 * don't want to abort commands that are already in the process of
4647 * being aborted.
James Smart07951072007-04-25 09:51:38 -04004648 */
4649 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004650 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05004651 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4652 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04004653 return 0;
4654
James Smart858c9f62007-06-17 19:56:39 -05004655 /* If we're unloading, don't abort iocb on the ELS ring, but change the
4656 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04004657 */
James Smart858c9f62007-06-17 19:56:39 -05004658 if ((vport->load_flag & FC_UNLOADING) &&
4659 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004660 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
4661 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
4662 else
4663 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04004664 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05004665 }
dea31012005-04-17 16:05:31 -05004666
4667 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05004668 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004669 if (abtsiocbp == NULL)
4670 return 0;
dea31012005-04-17 16:05:31 -05004671
James Smart07951072007-04-25 09:51:38 -04004672 /* This signals the response to set the correct status
4673 * before calling the completion handler.
4674 */
4675 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4676
dea31012005-04-17 16:05:31 -05004677 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04004678 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
4679 iabt->un.acxri.abortContextTag = icmd->ulpContext;
4680 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004681 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04004682 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05004683
James Smart2e0fef82007-06-17 19:56:36 -05004684 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04004685 iabt->ulpCommand = CMD_ABORT_XRI_CN;
4686 else
4687 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
4688
4689 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04004690
James Smarte8b62012007-08-02 11:10:09 -04004691 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4692 "0339 Abort xri x%x, original iotag x%x, "
4693 "abort cmd iotag x%x\n",
4694 iabt->un.acxri.abortContextTag,
4695 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05004696 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04004697
James Smartd7c255b2008-08-24 21:50:00 -04004698 if (retval)
4699 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04004700abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05004701 /*
4702 * Caller to this routine should check for IOCB_ERROR
4703 * and handle it properly. This routine no longer removes
4704 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04004705 */
James Smart2e0fef82007-06-17 19:56:36 -05004706 return retval;
dea31012005-04-17 16:05:31 -05004707}
4708
James Smarte59058c2008-08-24 21:49:00 -04004709/**
4710 * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
4711 * associated with a vport/SCSI target/lun.
4712 * @iocbq: Pointer to driver iocb object.
4713 * @vport: Pointer to driver virtual port object.
4714 * @tgt_id: SCSI ID of the target.
4715 * @lun_id: LUN ID of the scsi device.
4716 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
4717 *
4718 * This function acts as iocb filter for functions which abort or count
4719 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
4720 * 0 if the filtering criteria is met for the given iocb and will return
4721 * 1 if the filtering criteria is not met.
4722 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
4723 * given iocb is for the SCSI device specified by vport, tgt_id and
4724 * lun_id parameter.
4725 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
4726 * given iocb is for the SCSI target specified by vport and tgt_id
4727 * parameters.
4728 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
4729 * given iocb is for the SCSI host associated with the given vport.
4730 * This function is called with no locks held.
4731 **/
dea31012005-04-17 16:05:31 -05004732static int
James Smart51ef4c22007-08-02 11:10:31 -04004733lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
4734 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004735 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004736{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004737 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004738 int rc = 1;
4739
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004740 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
4741 return rc;
4742
James Smart51ef4c22007-08-02 11:10:31 -04004743 if (iocbq->vport != vport)
4744 return rc;
4745
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004746 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004747
James Smart495a7142008-06-14 22:52:59 -04004748 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05004749 return rc;
4750
4751 switch (ctx_cmd) {
4752 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04004753 if ((lpfc_cmd->rdata->pnode) &&
4754 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
4755 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05004756 rc = 0;
4757 break;
4758 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04004759 if ((lpfc_cmd->rdata->pnode) &&
4760 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05004761 rc = 0;
4762 break;
dea31012005-04-17 16:05:31 -05004763 case LPFC_CTX_HOST:
4764 rc = 0;
4765 break;
4766 default:
4767 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004768 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05004769 break;
4770 }
4771
4772 return rc;
4773}
4774
James Smarte59058c2008-08-24 21:49:00 -04004775/**
4776 * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
4777 * @vport: Pointer to virtual port.
4778 * @tgt_id: SCSI ID of the target.
4779 * @lun_id: LUN ID of the scsi device.
4780 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4781 *
4782 * This function returns number of FCP commands pending for the vport.
4783 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
4784 * commands pending on the vport associated with SCSI device specified
4785 * by tgt_id and lun_id parameters.
4786 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
4787 * commands pending on the vport associated with SCSI target specified
4788 * by tgt_id parameter.
4789 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
4790 * commands pending on the vport.
4791 * This function returns the number of iocbs which satisfy the filter.
4792 * This function is called without any lock held.
4793 **/
dea31012005-04-17 16:05:31 -05004794int
James Smart51ef4c22007-08-02 11:10:31 -04004795lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
4796 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004797{
James Smart51ef4c22007-08-02 11:10:31 -04004798 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004799 struct lpfc_iocbq *iocbq;
4800 int sum, i;
dea31012005-04-17 16:05:31 -05004801
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004802 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
4803 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004804
James Smart51ef4c22007-08-02 11:10:31 -04004805 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
4806 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004807 sum++;
dea31012005-04-17 16:05:31 -05004808 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004809
dea31012005-04-17 16:05:31 -05004810 return sum;
4811}
4812
James Smarte59058c2008-08-24 21:49:00 -04004813/**
4814 * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
4815 * FCP iocb.
4816 * @phba: Pointer to HBA context object
4817 * @cmdiocb: Pointer to command iocb object.
4818 * @rspiocb: Pointer to response iocb object.
4819 *
4820 * This function is called when an aborted FCP iocb completes. This
4821 * function is called by the ring event handler with no lock held.
4822 * This function frees the iocb.
4823 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004824void
James Smart2e0fef82007-06-17 19:56:36 -05004825lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4826 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004827{
James Bottomley604a3e32005-10-29 10:28:33 -05004828 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004829 return;
4830}
4831
James Smarte59058c2008-08-24 21:49:00 -04004832/**
4833 * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
4834 * pending on a SCSI host(vport)/target/lun.
4835 * @vport: Pointer to virtual port.
4836 * @pring: Pointer to driver SLI ring object.
4837 * @tgt_id: SCSI ID of the target.
4838 * @lun_id: LUN ID of the scsi device.
4839 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4840 *
4841 * This function sends an abort command for every SCSI command
4842 * associated with the given virtual port pending on the ring
4843 * filtered by lpfc_sli_validate_fcp_iocb function.
4844 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
4845 * FCP iocbs associated with lun specified by tgt_id and lun_id
4846 * parameters
4847 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
4848 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
4849 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
4850 * FCP iocbs associated with virtual port.
4851 * This function returns number of iocbs it failed to abort.
4852 * This function is called with no locks held.
4853 **/
dea31012005-04-17 16:05:31 -05004854int
James Smart51ef4c22007-08-02 11:10:31 -04004855lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
4856 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05004857{
James Smart51ef4c22007-08-02 11:10:31 -04004858 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004859 struct lpfc_iocbq *iocbq;
4860 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004861 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004862 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004863 int i;
dea31012005-04-17 16:05:31 -05004864
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004865 for (i = 1; i <= phba->sli.last_iotag; i++) {
4866 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004867
James Smart51ef4c22007-08-02 11:10:31 -04004868 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05004869 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05004870 continue;
4871
4872 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004873 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004874 if (abtsiocb == NULL) {
4875 errcnt++;
4876 continue;
4877 }
dea31012005-04-17 16:05:31 -05004878
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004879 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05004880 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
4881 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
4882 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
4883 abtsiocb->iocb.ulpLe = 1;
4884 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05004885 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05004886
James Smart2e0fef82007-06-17 19:56:36 -05004887 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004888 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
4889 else
4890 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
4891
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004892 /* Setup callback routine and issue the command. */
4893 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05004894 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
4895 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05004896 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05004897 errcnt++;
4898 continue;
4899 }
4900 }
4901
4902 return errcnt;
4903}
4904
James Smarte59058c2008-08-24 21:49:00 -04004905/**
4906 * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
4907 * lpfc_sli_issue_iocb_wait.
4908 * @phba: Pointer to HBA context object.
4909 * @cmdiocbq: Pointer to command iocb.
4910 * @rspiocbq: Pointer to response iocb.
4911 *
4912 * This function is the completion handler for iocbs issued using
4913 * lpfc_sli_issue_iocb_wait function. This function is called by the
4914 * ring event handler function without any lock held. This function
4915 * can be called from both worker thread context and interrupt
4916 * context. This function also can be called from other thread which
4917 * cleans up the SLI layer objects.
4918 * This function copy the contents of the response iocb to the
4919 * response iocb memory object provided by the caller of
4920 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4921 * sleeps for the iocb completion.
4922 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004923static void
4924lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
4925 struct lpfc_iocbq *cmdiocbq,
4926 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05004927{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004928 wait_queue_head_t *pdone_q;
4929 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004930
James Smart2e0fef82007-06-17 19:56:36 -05004931 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004932 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4933 if (cmdiocbq->context2 && rspiocbq)
4934 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4935 &rspiocbq->iocb, sizeof(IOCB_t));
4936
4937 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004938 if (pdone_q)
4939 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05004940 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05004941 return;
4942}
4943
James Smarte59058c2008-08-24 21:49:00 -04004944/**
4945 * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
4946 * @phba: Pointer to HBA context object..
4947 * @pring: Pointer to sli ring.
4948 * @piocb: Pointer to command iocb.
4949 * @prspiocbq: Pointer to response iocb.
4950 * @timeout: Timeout in number of seconds.
4951 *
4952 * This function issues the iocb to firmware and waits for the
4953 * iocb to complete. If the iocb command is not
4954 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
4955 * Caller should not free the iocb resources if this function
4956 * returns IOCB_TIMEDOUT.
4957 * The function waits for the iocb completion using an
4958 * non-interruptible wait.
4959 * This function will sleep while waiting for iocb completion.
4960 * So, this function should not be called from any context which
4961 * does not allow sleeping. Due to the same reason, this function
4962 * cannot be called with interrupt disabled.
4963 * This function assumes that the iocb completions occur while
4964 * this function sleep. So, this function cannot be called from
4965 * the thread which process iocb completion for this ring.
4966 * This function clears the iocb_flag of the iocb object before
4967 * issuing the iocb and the iocb completion handler sets this
4968 * flag and wakes this thread when the iocb completes.
4969 * The contents of the response iocb will be copied to prspiocbq
4970 * by the completion handler when the command completes.
4971 * This function returns IOCB_SUCCESS when success.
4972 * This function is called with no lock held.
4973 **/
dea31012005-04-17 16:05:31 -05004974int
James Smart2e0fef82007-06-17 19:56:36 -05004975lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
4976 struct lpfc_sli_ring *pring,
4977 struct lpfc_iocbq *piocb,
4978 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004979 uint32_t timeout)
dea31012005-04-17 16:05:31 -05004980{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08004981 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004982 long timeleft, timeout_req = 0;
4983 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004984 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05004985
4986 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004987 * If the caller has provided a response iocbq buffer, then context2
4988 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05004989 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004990 if (prspiocbq) {
4991 if (piocb->context2)
4992 return IOCB_ERROR;
4993 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05004994 }
4995
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004996 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
4997 piocb->context_un.wait_queue = &done_q;
4998 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05004999
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005000 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5001 creg_val = readl(phba->HCregaddr);
5002 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
5003 writel(creg_val, phba->HCregaddr);
5004 readl(phba->HCregaddr); /* flush */
5005 }
5006
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005007 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
5008 if (retval == IOCB_SUCCESS) {
5009 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005010 timeleft = wait_event_timeout(done_q,
5011 piocb->iocb_flag & LPFC_IO_WAKE,
5012 timeout_req);
dea31012005-04-17 16:05:31 -05005013
James Smart7054a602007-04-25 09:52:34 -04005014 if (piocb->iocb_flag & LPFC_IO_WAKE) {
5015 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005016 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04005017 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005018 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005019 "0338 IOCB wait timeout error - no "
5020 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005021 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04005022 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005024 "0330 IOCB wake NOT set, "
5025 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005026 timeout, (timeleft / jiffies));
5027 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05005028 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005029 } else {
5030 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04005031 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005032 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005033 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05005034 }
5035
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005036 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5037 creg_val = readl(phba->HCregaddr);
5038 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
5039 writel(creg_val, phba->HCregaddr);
5040 readl(phba->HCregaddr); /* flush */
5041 }
5042
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005043 if (prspiocbq)
5044 piocb->context2 = NULL;
5045
5046 piocb->context_un.wait_queue = NULL;
5047 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05005048 return retval;
5049}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005050
James Smarte59058c2008-08-24 21:49:00 -04005051/**
5052 * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
5053 * @phba: Pointer to HBA context object.
5054 * @pmboxq: Pointer to driver mailbox object.
5055 * @timeout: Timeout in number of seconds.
5056 *
5057 * This function issues the mailbox to firmware and waits for the
5058 * mailbox command to complete. If the mailbox command is not
5059 * completed within timeout seconds, it returns MBX_TIMEOUT.
5060 * The function waits for the mailbox completion using an
5061 * interruptible wait. If the thread is woken up due to a
5062 * signal, MBX_TIMEOUT error is returned to the caller. Caller
5063 * should not free the mailbox resources, if this function returns
5064 * MBX_TIMEOUT.
5065 * This function will sleep while waiting for mailbox completion.
5066 * So, this function should not be called from any context which
5067 * does not allow sleeping. Due to the same reason, this function
5068 * cannot be called with interrupt disabled.
5069 * This function assumes that the mailbox completion occurs while
5070 * this function sleep. So, this function cannot be called from
5071 * the worker thread which processes mailbox completion.
5072 * This function is called in the context of HBA management
5073 * applications.
5074 * This function returns MBX_SUCCESS when successful.
5075 * This function is called with no lock held.
5076 **/
dea31012005-04-17 16:05:31 -05005077int
James Smart2e0fef82007-06-17 19:56:36 -05005078lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05005079 uint32_t timeout)
5080{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08005081 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05005082 int retval;
James Smart858c9f62007-06-17 19:56:39 -05005083 unsigned long flag;
dea31012005-04-17 16:05:31 -05005084
5085 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04005086 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05005087 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005088
James Smart495a7142008-06-14 22:52:59 -04005089 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05005090 /* setup wake call as IOCB callback */
5091 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
5092 /* setup context field to pass wait_queue pointer to wake function */
5093 pmboxq->context1 = &done_q;
5094
dea31012005-04-17 16:05:31 -05005095 /* now issue the command */
5096 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
5097
5098 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04005099 wait_event_interruptible_timeout(done_q,
5100 pmboxq->mbox_flag & LPFC_MBX_WAKE,
5101 timeout * HZ);
5102
James Smart858c9f62007-06-17 19:56:39 -05005103 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005104 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04005105 /*
5106 * if LPFC_MBX_WAKE flag is set the mailbox is completed
5107 * else do not free the resources.
5108 */
5109 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05005110 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05005111 else {
James Smart7054a602007-04-25 09:52:34 -04005112 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05005113 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5114 }
5115 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005116 }
5117
dea31012005-04-17 16:05:31 -05005118 return retval;
5119}
5120
James Smarte59058c2008-08-24 21:49:00 -04005121/**
5122 * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
5123 * @phba: Pointer to HBA context.
5124 *
5125 * This function is called to cleanup any pending mailbox
5126 * objects in the driver queue before bringing the HBA offline.
5127 * This function is called while resetting the HBA.
5128 * The function is called without any lock held. The function
5129 * takes hbalock to update SLI data structure.
5130 * This function returns 1 when there is an active mailbox
5131 * command pending else returns 0.
5132 **/
James Smartb4c02652006-07-06 15:50:43 -04005133int
5134lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
5135{
James Smart2e0fef82007-06-17 19:56:36 -05005136 struct lpfc_vport *vport = phba->pport;
James Smartb4c02652006-07-06 15:50:43 -04005137 int i = 0;
James Smarted957682007-06-17 19:56:37 -05005138 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04005139
James Smart2e0fef82007-06-17 19:56:36 -05005140 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
James Smartb4c02652006-07-06 15:50:43 -04005141 if (i++ > LPFC_MBOX_TMO * 1000)
5142 return 1;
5143
James Smarted957682007-06-17 19:56:37 -05005144 /*
5145 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
5146 * did finish. This way we won't get the misleading
5147 * "Stray Mailbox Interrupt" message.
5148 */
5149 spin_lock_irq(&phba->hbalock);
5150 ha_copy = phba->work_ha;
5151 phba->work_ha &= ~HA_MBATT;
5152 spin_unlock_irq(&phba->hbalock);
5153
5154 if (ha_copy & HA_MBATT)
5155 if (lpfc_sli_handle_mb_event(phba) == 0)
5156 i = 0;
James Smartb4c02652006-07-06 15:50:43 -04005157
5158 msleep(1);
5159 }
5160
5161 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
5162}
5163
James Smarte59058c2008-08-24 21:49:00 -04005164/**
James Smart93996272008-08-24 21:50:30 -04005165 * lpfc_sli_check_eratt: check error attention events
5166 * @phba: Pointer to HBA context.
5167 *
5168 * This function is called form timer soft interrupt context to check HBA's
5169 * error attention register bit for error attention events.
5170 *
5171 * This fucntion returns 1 when there is Error Attention in the Host Attention
5172 * Register and returns 0 otherwise.
5173 **/
5174int
5175lpfc_sli_check_eratt(struct lpfc_hba *phba)
5176{
5177 uint32_t ha_copy;
5178
James Smarteaf15d52008-12-04 22:39:29 -05005179 /* If PCI channel is offline, don't process it */
5180 if (unlikely(pci_channel_offline(phba->pcidev)))
5181 return 0;
5182
James Smart93996272008-08-24 21:50:30 -04005183 /* If somebody is waiting to handle an eratt, don't process it
5184 * here. The brdkill function will do this.
5185 */
5186 if (phba->link_flag & LS_IGNORE_ERATT)
5187 return 0;
5188
5189 /* Check if interrupt handler handles this ERATT */
5190 spin_lock_irq(&phba->hbalock);
5191 if (phba->hba_flag & HBA_ERATT_HANDLED) {
5192 /* Interrupt handler has handled ERATT */
5193 spin_unlock_irq(&phba->hbalock);
5194 return 0;
5195 }
5196
5197 /* Read chip Host Attention (HA) register */
5198 ha_copy = readl(phba->HAregaddr);
5199 if (ha_copy & HA_ERATT) {
5200 /* Read host status register to retrieve error event */
5201 lpfc_sli_read_hs(phba);
5202 /* Set the driver HA work bitmap */
5203 phba->work_ha |= HA_ERATT;
5204 /* Indicate polling handles this ERATT */
5205 phba->hba_flag |= HBA_ERATT_HANDLED;
5206 spin_unlock_irq(&phba->hbalock);
5207 return 1;
5208 }
5209 spin_unlock_irq(&phba->hbalock);
5210 return 0;
5211}
5212
5213/**
5214 * lpfc_sp_intr_handler: The slow-path interrupt handler of lpfc driver.
James Smarte59058c2008-08-24 21:49:00 -04005215 * @irq: Interrupt number.
5216 * @dev_id: The device context pointer.
5217 *
James Smart93996272008-08-24 21:50:30 -04005218 * This function is directly called from the PCI layer as an interrupt
5219 * service routine when the device is enabled with MSI-X multi-message
5220 * interrupt mode and there are slow-path events in the HBA. However,
5221 * when the device is enabled with either MSI or Pin-IRQ interrupt mode,
5222 * this function is called as part of the device-level interrupt handler.
5223 * When the PCI slot is in error recovery or the HBA is undergoing
5224 * initialization, the interrupt handler will not process the interrupt.
5225 * The link attention and ELS ring attention events are handled by the
5226 * worker thread. The interrupt handler signals the worker thread and
5227 * and returns for these events. This function is called without any
5228 * lock held. It gets the hbalock to access and update SLI data
5229 * structures.
5230 *
5231 * This function returns IRQ_HANDLED when interrupt is handled else it
5232 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04005233 **/
dea31012005-04-17 16:05:31 -05005234irqreturn_t
James Smart93996272008-08-24 21:50:30 -04005235lpfc_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05005236{
James Smart2e0fef82007-06-17 19:56:36 -05005237 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05005238 uint32_t ha_copy;
5239 uint32_t work_ha_copy;
5240 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05005241 unsigned long iflag;
dea31012005-04-17 16:05:31 -05005242 uint32_t control;
5243
James Smart92d7f7b2007-06-17 19:56:38 -05005244 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05005245 struct lpfc_vport *vport;
5246 struct lpfc_nodelist *ndlp;
5247 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05005248 LPFC_MBOXQ_t *pmb;
5249 int rc;
5250
dea31012005-04-17 16:05:31 -05005251 /*
5252 * Get the driver's phba structure from the dev_id and
5253 * assume the HBA is not interrupting.
5254 */
James Smart93996272008-08-24 21:50:30 -04005255 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05005256
5257 if (unlikely(!phba))
5258 return IRQ_NONE;
5259
dea31012005-04-17 16:05:31 -05005260 /*
James Smart93996272008-08-24 21:50:30 -04005261 * Stuff needs to be attented to when this function is invoked as an
5262 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005263 */
James Smart93996272008-08-24 21:50:30 -04005264 if (phba->intr_type == MSIX) {
5265 /* If the pci channel is offline, ignore all the interrupts */
5266 if (unlikely(pci_channel_offline(phba->pcidev)))
5267 return IRQ_NONE;
5268 /* Update device-level interrupt statistics */
5269 phba->sli.slistat.sli_intr++;
5270 /* Ignore all interrupts during initialization. */
5271 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5272 return IRQ_NONE;
5273 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05005274 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04005275 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04005276 /* If somebody is waiting to handle an eratt don't process it
5277 * here. The brdkill function will do this.
5278 */
5279 if (phba->link_flag & LS_IGNORE_ERATT)
5280 ha_copy &= ~HA_ERATT;
5281 /* Check the need for handling ERATT in interrupt handler */
5282 if (ha_copy & HA_ERATT) {
5283 if (phba->hba_flag & HBA_ERATT_HANDLED)
5284 /* ERATT polling has handled ERATT */
5285 ha_copy &= ~HA_ERATT;
5286 else
5287 /* Indicate interrupt handler handles ERATT */
5288 phba->hba_flag |= HBA_ERATT_HANDLED;
5289 }
5290 /* Clear up only attention source related to slow-path */
5291 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
5292 phba->HAregaddr);
5293 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005294 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005295 } else
5296 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05005297
dea31012005-04-17 16:05:31 -05005298 work_ha_copy = ha_copy & phba->work_ha_mask;
5299
James Smart93996272008-08-24 21:50:30 -04005300 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05005301 if (work_ha_copy & HA_LATT) {
5302 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
5303 /*
5304 * Turn off Link Attention interrupts
5305 * until CLEAR_LA done
5306 */
James Smart5b75da22008-12-04 22:39:35 -05005307 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005308 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
5309 control = readl(phba->HCregaddr);
5310 control &= ~HC_LAINT_ENA;
5311 writel(control, phba->HCregaddr);
5312 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005313 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005314 }
5315 else
5316 work_ha_copy &= ~HA_LATT;
5317 }
5318
James Smart93996272008-08-24 21:50:30 -04005319 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05005320 /*
5321 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
5322 * the only slow ring.
5323 */
5324 status = (work_ha_copy &
5325 (HA_RXMASK << (4*LPFC_ELS_RING)));
5326 status >>= (4*LPFC_ELS_RING);
5327 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05005328 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05005329 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04005330
5331 lpfc_debugfs_slow_ring_trc(phba,
5332 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
5333 control, status,
5334 (uint32_t)phba->sli.slistat.sli_intr);
5335
James Smart858c9f62007-06-17 19:56:39 -05005336 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04005337 lpfc_debugfs_slow_ring_trc(phba,
5338 "ISR Disable ring:"
5339 "pwork:x%x hawork:x%x wait:x%x",
5340 phba->work_ha, work_ha_copy,
5341 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005342 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005343
James Smart858c9f62007-06-17 19:56:39 -05005344 control &=
5345 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05005346 writel(control, phba->HCregaddr);
5347 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05005348 }
James Smarta58cbd52007-08-02 11:09:43 -04005349 else {
5350 lpfc_debugfs_slow_ring_trc(phba,
5351 "ISR slow ring: pwork:"
5352 "x%x hawork:x%x wait:x%x",
5353 phba->work_ha, work_ha_copy,
5354 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005355 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005356 }
James Smart5b75da22008-12-04 22:39:35 -05005357 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005358 }
5359 }
James Smart5b75da22008-12-04 22:39:35 -05005360 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005361 if (work_ha_copy & HA_ERATT)
5362 lpfc_sli_read_hs(phba);
5363 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005364 pmb = phba->sli.mbox_active;
5365 pmbox = &pmb->mb;
James Smart34b02dc2008-08-24 21:49:55 -04005366 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05005367 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005368
5369 /* First check out the status word */
5370 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
5371 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05005372 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05005373 /*
5374 * Stray Mailbox Interrupt, mbxCommand <cmd>
5375 * mbxStatus <status>
5376 */
James Smart09372822008-01-11 01:52:54 -05005377 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05005378 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005379 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05005380 "Interrupt mbxCommand x%x "
5381 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005382 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05005383 pmbox->mbxCommand,
5384 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05005385 /* clear mailbox attention bit */
5386 work_ha_copy &= ~HA_MBATT;
5387 } else {
James Smart97eab632008-04-07 10:16:05 -04005388 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05005389 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05005390 phba->last_completion_time = jiffies;
5391 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05005392 if (pmb->mbox_cmpl) {
5393 lpfc_sli_pcimem_bcopy(mbox, pmbox,
5394 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05005395 }
James Smart09372822008-01-11 01:52:54 -05005396 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
5397 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
5398
5399 lpfc_debugfs_disc_trc(vport,
5400 LPFC_DISC_TRC_MBOX_VPORT,
5401 "MBOX dflt rpi: : "
5402 "status:x%x rpi:x%x",
5403 (uint32_t)pmbox->mbxStatus,
5404 pmbox->un.varWords[0], 0);
5405
5406 if (!pmbox->mbxStatus) {
5407 mp = (struct lpfc_dmabuf *)
5408 (pmb->context1);
5409 ndlp = (struct lpfc_nodelist *)
5410 pmb->context2;
5411
5412 /* Reg_LOGIN of dflt RPI was
5413 * successful. new lets get
5414 * rid of the RPI using the
5415 * same mbox buffer.
5416 */
5417 lpfc_unreg_login(phba,
5418 vport->vpi,
5419 pmbox->un.varWords[0],
5420 pmb);
5421 pmb->mbox_cmpl =
5422 lpfc_mbx_cmpl_dflt_rpi;
5423 pmb->context1 = mp;
5424 pmb->context2 = ndlp;
5425 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04005426 rc = lpfc_sli_issue_mbox(phba,
5427 pmb,
5428 MBX_NOWAIT);
5429 if (rc != MBX_BUSY)
5430 lpfc_printf_log(phba,
5431 KERN_ERR,
5432 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04005433 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04005434 "been MBX_BUSY");
James Smart09372822008-01-11 01:52:54 -05005435 goto send_current_mbox;
5436 }
5437 }
James Smart5b75da22008-12-04 22:39:35 -05005438 spin_lock_irqsave(
5439 &phba->pport->work_port_lock,
5440 iflag);
James Smart09372822008-01-11 01:52:54 -05005441 phba->pport->work_port_events &=
5442 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05005443 spin_unlock_irqrestore(
5444 &phba->pport->work_port_lock,
5445 iflag);
James Smart09372822008-01-11 01:52:54 -05005446 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05005447 }
James Smart97eab632008-04-07 10:16:05 -04005448 } else
James Smart5b75da22008-12-04 22:39:35 -05005449 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005450
James Smart92d7f7b2007-06-17 19:56:38 -05005451 if ((work_ha_copy & HA_MBATT) &&
5452 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05005453send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05005454 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04005455 do {
5456 rc = lpfc_sli_issue_mbox(phba, NULL,
5457 MBX_NOWAIT);
5458 } while (rc == MBX_NOT_FINISHED);
5459 if (rc != MBX_SUCCESS)
5460 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
5461 LOG_SLI, "0349 rc should be "
5462 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05005463 }
5464
James Smart5b75da22008-12-04 22:39:35 -05005465 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05005466 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05005467 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04005468 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005469 }
James Smart93996272008-08-24 21:50:30 -04005470 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05005471
James Smart93996272008-08-24 21:50:30 -04005472} /* lpfc_sp_intr_handler */
5473
5474/**
5475 * lpfc_fp_intr_handler: The fast-path interrupt handler of lpfc driver.
5476 * @irq: Interrupt number.
5477 * @dev_id: The device context pointer.
5478 *
5479 * This function is directly called from the PCI layer as an interrupt
5480 * service routine when the device is enabled with MSI-X multi-message
5481 * interrupt mode and there is a fast-path FCP IOCB ring event in the
5482 * HBA. However, when the device is enabled with either MSI or Pin-IRQ
5483 * interrupt mode, this function is called as part of the device-level
5484 * interrupt handler. When the PCI slot is in error recovery or the HBA
5485 * is undergoing initialization, the interrupt handler will not process
5486 * the interrupt. The SCSI FCP fast-path ring event are handled in the
5487 * intrrupt context. This function is called without any lock held. It
5488 * gets the hbalock to access and update SLI data structures.
5489 *
5490 * This function returns IRQ_HANDLED when interrupt is handled else it
5491 * returns IRQ_NONE.
5492 **/
5493irqreturn_t
5494lpfc_fp_intr_handler(int irq, void *dev_id)
5495{
5496 struct lpfc_hba *phba;
5497 uint32_t ha_copy;
5498 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05005499 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04005500
5501 /* Get the driver's phba structure from the dev_id and
5502 * assume the HBA is not interrupting.
5503 */
5504 phba = (struct lpfc_hba *) dev_id;
5505
5506 if (unlikely(!phba))
5507 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05005508
5509 /*
James Smart93996272008-08-24 21:50:30 -04005510 * Stuff needs to be attented to when this function is invoked as an
5511 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005512 */
James Smart93996272008-08-24 21:50:30 -04005513 if (phba->intr_type == MSIX) {
5514 /* If pci channel is offline, ignore all the interrupts */
5515 if (unlikely(pci_channel_offline(phba->pcidev)))
5516 return IRQ_NONE;
5517 /* Update device-level interrupt statistics */
5518 phba->sli.slistat.sli_intr++;
5519 /* Ignore all interrupts during initialization. */
5520 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5521 return IRQ_NONE;
5522 /* Need to read HA REG for FCP ring and other ring events */
5523 ha_copy = readl(phba->HAregaddr);
5524 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05005525 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005526 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
5527 phba->HAregaddr);
5528 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05005529 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04005530 } else
5531 ha_copy = phba->ha_copy;
5532
5533 /*
5534 * Process all events on FCP ring. Take the optimized path for FCP IO.
5535 */
5536 ha_copy &= ~(phba->work_ha_mask);
5537
5538 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05005539 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05005540 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05005541 lpfc_sli_handle_fast_ring_event(phba,
5542 &phba->sli.ring[LPFC_FCP_RING],
5543 status);
James Smarta4bc3372006-12-02 13:34:16 -05005544
5545 if (phba->cfg_multi_ring_support == 2) {
5546 /*
James Smart93996272008-08-24 21:50:30 -04005547 * Process all events on extra ring. Take the optimized path
5548 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05005549 */
James Smart93996272008-08-24 21:50:30 -04005550 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05005551 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05005552 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05005553 lpfc_sli_handle_fast_ring_event(phba,
5554 &phba->sli.ring[LPFC_EXTRA_RING],
5555 status);
5556 }
5557 }
dea31012005-04-17 16:05:31 -05005558 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -04005559} /* lpfc_fp_intr_handler */
dea31012005-04-17 16:05:31 -05005560
James Smart93996272008-08-24 21:50:30 -04005561/**
5562 * lpfc_intr_handler: The device-level interrupt handler of lpfc driver.
5563 * @irq: Interrupt number.
5564 * @dev_id: The device context pointer.
5565 *
5566 * This function is the device-level interrupt handler called from the PCI
5567 * layer when either MSI or Pin-IRQ interrupt mode is enabled and there is
5568 * an event in the HBA which requires driver attention. This function
5569 * invokes the slow-path interrupt attention handling function and fast-path
5570 * interrupt attention handling function in turn to process the relevant
5571 * HBA attention events. This function is called without any lock held. It
5572 * gets the hbalock to access and update SLI data structures.
5573 *
5574 * This function returns IRQ_HANDLED when interrupt is handled, else it
5575 * returns IRQ_NONE.
5576 **/
5577irqreturn_t
5578lpfc_intr_handler(int irq, void *dev_id)
5579{
5580 struct lpfc_hba *phba;
5581 irqreturn_t sp_irq_rc, fp_irq_rc;
5582 unsigned long status1, status2;
5583
5584 /*
5585 * Get the driver's phba structure from the dev_id and
5586 * assume the HBA is not interrupting.
5587 */
5588 phba = (struct lpfc_hba *) dev_id;
5589
5590 if (unlikely(!phba))
5591 return IRQ_NONE;
5592
5593 /* If the pci channel is offline, ignore all the interrupts. */
5594 if (unlikely(pci_channel_offline(phba->pcidev)))
5595 return IRQ_NONE;
5596
5597 /* Update device level interrupt statistics */
5598 phba->sli.slistat.sli_intr++;
5599
5600 /* Ignore all interrupts during initialization. */
5601 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5602 return IRQ_NONE;
5603
5604 spin_lock(&phba->hbalock);
5605 phba->ha_copy = readl(phba->HAregaddr);
5606 if (unlikely(!phba->ha_copy)) {
5607 spin_unlock(&phba->hbalock);
5608 return IRQ_NONE;
5609 } else if (phba->ha_copy & HA_ERATT) {
5610 if (phba->hba_flag & HBA_ERATT_HANDLED)
5611 /* ERATT polling has handled ERATT */
5612 phba->ha_copy &= ~HA_ERATT;
5613 else
5614 /* Indicate interrupt handler handles ERATT */
5615 phba->hba_flag |= HBA_ERATT_HANDLED;
5616 }
5617
5618 /* Clear attention sources except link and error attentions */
5619 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
5620 readl(phba->HAregaddr); /* flush */
5621 spin_unlock(&phba->hbalock);
5622
5623 /*
5624 * Invokes slow-path host attention interrupt handling as appropriate.
5625 */
5626
5627 /* status of events with mailbox and link attention */
5628 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
5629
5630 /* status of events with ELS ring */
5631 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
5632 status2 >>= (4*LPFC_ELS_RING);
5633
5634 if (status1 || (status2 & HA_RXMASK))
5635 sp_irq_rc = lpfc_sp_intr_handler(irq, dev_id);
5636 else
5637 sp_irq_rc = IRQ_NONE;
5638
5639 /*
5640 * Invoke fast-path host attention interrupt handling as appropriate.
5641 */
5642
5643 /* status of events with FCP ring */
5644 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
5645 status1 >>= (4*LPFC_FCP_RING);
5646
5647 /* status of events with extra ring */
5648 if (phba->cfg_multi_ring_support == 2) {
5649 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
5650 status2 >>= (4*LPFC_EXTRA_RING);
5651 } else
5652 status2 = 0;
5653
5654 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
5655 fp_irq_rc = lpfc_fp_intr_handler(irq, dev_id);
5656 else
5657 fp_irq_rc = IRQ_NONE;
5658
5659 /* Return device-level interrupt handling status */
5660 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
5661} /* lpfc_intr_handler */