blob: c7a520fa1aaad24b1976a8d5a6561974ea3e2d49 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart1b32f6a2008-02-08 18:49:39 -05004 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040031#include <scsi/scsi_transport_fc.h>
dea31012005-04-17 16:05:31 -050032
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
35#include "lpfc_disc.h"
36#include "lpfc_scsi.h"
37#include "lpfc.h"
38#include "lpfc_crtn.h"
39#include "lpfc_logmsg.h"
40#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050041#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050042
43/*
44 * Define macro to log: Mailbox command x%x cannot issue Data
45 * This allows multiple uses of lpfc_msgBlk0311
46 * w/o perturbing log msg utility.
47 */
James Smart92d7f7b2007-06-17 19:56:38 -050048#define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
dea31012005-04-17 16:05:31 -050049 lpfc_printf_log(phba, \
50 KERN_INFO, \
51 LOG_MBOX | LOG_SLI, \
James Smarte8b62012007-08-02 11:10:09 -040052 "(%d):0311 Mailbox command x%x cannot " \
James Smart92d7f7b2007-06-17 19:56:38 -050053 "issue Data: x%x x%x x%x\n", \
James Smart92d7f7b2007-06-17 19:56:38 -050054 pmbox->vport ? pmbox->vport->vpi : 0, \
55 pmbox->mb.mbxCommand, \
James Smart2e0fef82007-06-17 19:56:36 -050056 phba->pport->port_state, \
dea31012005-04-17 16:05:31 -050057 psli->sli_flag, \
James Smart2e0fef82007-06-17 19:56:36 -050058 flag)
dea31012005-04-17 16:05:31 -050059
60
61/* There are only four IOCB completion types. */
62typedef enum _lpfc_iocb_type {
63 LPFC_UNKNOWN_IOCB,
64 LPFC_UNSOL_IOCB,
65 LPFC_SOL_IOCB,
66 LPFC_ABORT_IOCB
67} lpfc_iocb_type;
68
James Smarte59058c2008-08-24 21:49:00 -040069/**
70 * lpfc_cmd_iocb: Get next command iocb entry in the ring.
71 * @phba: Pointer to HBA context object.
72 * @pring: Pointer to driver SLI ring object.
73 *
74 * This function returns pointer to next command iocb entry
75 * in the command ring. The caller must hold hbalock to prevent
76 * other threads consume the next command iocb.
77 * SLI-2/SLI-3 provide different sized iocbs.
78 **/
James Smarted957682007-06-17 19:56:37 -050079static inline IOCB_t *
80lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
81{
82 return (IOCB_t *) (((char *) pring->cmdringaddr) +
83 pring->cmdidx * phba->iocb_cmd_size);
84}
85
James Smarte59058c2008-08-24 21:49:00 -040086/**
87 * lpfc_resp_iocb: Get next response iocb entry in the ring.
88 * @phba: Pointer to HBA context object.
89 * @pring: Pointer to driver SLI ring object.
90 *
91 * This function returns pointer to next response iocb entry
92 * in the response ring. The caller must hold hbalock to make sure
93 * that no other thread consume the next response iocb.
94 * SLI-2/SLI-3 provide different sized iocbs.
95 **/
James Smarted957682007-06-17 19:56:37 -050096static inline IOCB_t *
97lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
98{
99 return (IOCB_t *) (((char *) pring->rspringaddr) +
100 pring->rspidx * phba->iocb_rsp_size);
101}
102
James Smarte59058c2008-08-24 21:49:00 -0400103/**
104 * __lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
105 * @phba: Pointer to HBA context object.
106 *
107 * This function is called with hbalock held. This function
108 * allocates a new driver iocb object from the iocb pool. If the
109 * allocation is successful, it returns pointer to the newly
110 * allocated iocb object else it returns NULL.
111 **/
James Smart2e0fef82007-06-17 19:56:36 -0500112static struct lpfc_iocbq *
113__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400114{
115 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
116 struct lpfc_iocbq * iocbq = NULL;
117
118 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
119 return iocbq;
120}
121
James Smarte59058c2008-08-24 21:49:00 -0400122/**
123 * lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
124 * @phba: Pointer to HBA context object.
125 *
126 * This function is called with no lock held. This function
127 * allocates a new driver iocb object from the iocb pool. If the
128 * allocation is successful, it returns pointer to the newly
129 * allocated iocb object else it returns NULL.
130 **/
James Smart2e0fef82007-06-17 19:56:36 -0500131struct lpfc_iocbq *
132lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500133{
James Smart2e0fef82007-06-17 19:56:36 -0500134 struct lpfc_iocbq * iocbq = NULL;
135 unsigned long iflags;
136
137 spin_lock_irqsave(&phba->hbalock, iflags);
138 iocbq = __lpfc_sli_get_iocbq(phba);
139 spin_unlock_irqrestore(&phba->hbalock, iflags);
140 return iocbq;
141}
142
James Smarte59058c2008-08-24 21:49:00 -0400143/**
144 * __lpfc_sli_release_iocbq: Release iocb to the iocb pool.
145 * @phba: Pointer to HBA context object.
146 * @iocbq: Pointer to driver iocb object.
147 *
148 * This function is called with hbalock held to release driver
149 * iocb object to the iocb pool. The iotag in the iocb object
150 * does not change for each use of the iocb object. This function
151 * clears all other fields of the iocb object when it is freed.
152 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100153static void
James Smart2e0fef82007-06-17 19:56:36 -0500154__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
155{
156 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500157
158 /*
159 * Clean all volatile data fields, preserve iotag and node struct.
160 */
161 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
162 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
163}
164
James Smarte59058c2008-08-24 21:49:00 -0400165/**
166 * lpfc_sli_release_iocbq: Release iocb to the iocb pool.
167 * @phba: Pointer to HBA context object.
168 * @iocbq: Pointer to driver iocb object.
169 *
170 * This function is called with no lock held to release the iocb to
171 * iocb pool.
172 **/
James Smart2e0fef82007-06-17 19:56:36 -0500173void
174lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
175{
176 unsigned long iflags;
177
178 /*
179 * Clean all volatile data fields, preserve iotag and node struct.
180 */
181 spin_lock_irqsave(&phba->hbalock, iflags);
182 __lpfc_sli_release_iocbq(phba, iocbq);
183 spin_unlock_irqrestore(&phba->hbalock, iflags);
184}
185
James Smarte59058c2008-08-24 21:49:00 -0400186/**
187 * lpfc_sli_iocb_cmd_type: Get the iocb type.
188 * @iocb_cmnd : iocb command code.
189 *
190 * This function is called by ring event handler function to get the iocb type.
191 * This function translates the iocb command to an iocb command type used to
192 * decide the final disposition of each completed IOCB.
193 * The function returns
194 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
195 * LPFC_SOL_IOCB if it is a solicited iocb completion
196 * LPFC_ABORT_IOCB if it is an abort iocb
197 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
198 *
199 * The caller is not required to hold any lock.
200 **/
dea31012005-04-17 16:05:31 -0500201static lpfc_iocb_type
202lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
203{
204 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
205
206 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
207 return 0;
208
209 switch (iocb_cmnd) {
210 case CMD_XMIT_SEQUENCE_CR:
211 case CMD_XMIT_SEQUENCE_CX:
212 case CMD_XMIT_BCAST_CN:
213 case CMD_XMIT_BCAST_CX:
214 case CMD_ELS_REQUEST_CR:
215 case CMD_ELS_REQUEST_CX:
216 case CMD_CREATE_XRI_CR:
217 case CMD_CREATE_XRI_CX:
218 case CMD_GET_RPI_CN:
219 case CMD_XMIT_ELS_RSP_CX:
220 case CMD_GET_RPI_CR:
221 case CMD_FCP_IWRITE_CR:
222 case CMD_FCP_IWRITE_CX:
223 case CMD_FCP_IREAD_CR:
224 case CMD_FCP_IREAD_CX:
225 case CMD_FCP_ICMND_CR:
226 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500227 case CMD_FCP_TSEND_CX:
228 case CMD_FCP_TRSP_CX:
229 case CMD_FCP_TRECEIVE_CX:
230 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500231 case CMD_ADAPTER_MSG:
232 case CMD_ADAPTER_DUMP:
233 case CMD_XMIT_SEQUENCE64_CR:
234 case CMD_XMIT_SEQUENCE64_CX:
235 case CMD_XMIT_BCAST64_CN:
236 case CMD_XMIT_BCAST64_CX:
237 case CMD_ELS_REQUEST64_CR:
238 case CMD_ELS_REQUEST64_CX:
239 case CMD_FCP_IWRITE64_CR:
240 case CMD_FCP_IWRITE64_CX:
241 case CMD_FCP_IREAD64_CR:
242 case CMD_FCP_IREAD64_CX:
243 case CMD_FCP_ICMND64_CR:
244 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500245 case CMD_FCP_TSEND64_CX:
246 case CMD_FCP_TRSP64_CX:
247 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500248 case CMD_GEN_REQUEST64_CR:
249 case CMD_GEN_REQUEST64_CX:
250 case CMD_XMIT_ELS_RSP64_CX:
251 type = LPFC_SOL_IOCB;
252 break;
253 case CMD_ABORT_XRI_CN:
254 case CMD_ABORT_XRI_CX:
255 case CMD_CLOSE_XRI_CN:
256 case CMD_CLOSE_XRI_CX:
257 case CMD_XRI_ABORTED_CX:
258 case CMD_ABORT_MXRI64_CN:
259 type = LPFC_ABORT_IOCB;
260 break;
261 case CMD_RCV_SEQUENCE_CX:
262 case CMD_RCV_ELS_REQ_CX:
263 case CMD_RCV_SEQUENCE64_CX:
264 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400265 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500266 case CMD_IOCB_RCV_SEQ64_CX:
267 case CMD_IOCB_RCV_ELS64_CX:
268 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500269 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500270 type = LPFC_UNSOL_IOCB;
271 break;
James Smart3163f722008-02-08 18:50:25 -0500272 case CMD_IOCB_XMIT_MSEQ64_CR:
273 case CMD_IOCB_XMIT_MSEQ64_CX:
274 case CMD_IOCB_RCV_SEQ_LIST64_CX:
275 case CMD_IOCB_RCV_ELS_LIST64_CX:
276 case CMD_IOCB_CLOSE_EXTENDED_CN:
277 case CMD_IOCB_ABORT_EXTENDED_CN:
278 case CMD_IOCB_RET_HBQE64_CN:
279 case CMD_IOCB_FCP_IBIDIR64_CR:
280 case CMD_IOCB_FCP_IBIDIR64_CX:
281 case CMD_IOCB_FCP_ITASKMGT64_CX:
282 case CMD_IOCB_LOGENTRY_CN:
283 case CMD_IOCB_LOGENTRY_ASYNC_CN:
284 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700285 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500286 type = LPFC_UNKNOWN_IOCB;
287 break;
dea31012005-04-17 16:05:31 -0500288 default:
289 type = LPFC_UNKNOWN_IOCB;
290 break;
291 }
292
293 return type;
294}
295
James Smarte59058c2008-08-24 21:49:00 -0400296/**
297 * lpfc_sli_ring_map: Issue config_ring mbox for all rings.
298 * @phba: Pointer to HBA context object.
299 *
300 * This function is called from SLI initialization code
301 * to configure every ring of the HBA's SLI interface. The
302 * caller is not required to hold any lock. This function issues
303 * a config_ring mailbox command for each ring.
304 * This function returns zero if successful else returns a negative
305 * error code.
306 **/
dea31012005-04-17 16:05:31 -0500307static int
James Smarted957682007-06-17 19:56:37 -0500308lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500309{
310 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500311 LPFC_MBOXQ_t *pmb;
312 MAILBOX_t *pmbox;
313 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500314
James Smarted957682007-06-17 19:56:37 -0500315 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
316 if (!pmb)
317 return -ENOMEM;
318 pmbox = &pmb->mb;
319 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500320 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500321 lpfc_config_ring(phba, i, pmb);
322 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
323 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400325 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500326 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
327 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400328 rc, pmbox->mbxCommand,
329 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500330 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500331 ret = -ENXIO;
332 break;
dea31012005-04-17 16:05:31 -0500333 }
334 }
James Smarted957682007-06-17 19:56:37 -0500335 mempool_free(pmb, phba->mbox_mem_pool);
336 return ret;
dea31012005-04-17 16:05:31 -0500337}
338
James Smarte59058c2008-08-24 21:49:00 -0400339/**
340 * lpfc_sli_ringtxcmpl_put: Adds new iocb to the txcmplq.
341 * @phba: Pointer to HBA context object.
342 * @pring: Pointer to driver SLI ring object.
343 * @piocb: Pointer to the driver iocb object.
344 *
345 * This function is called with hbalock held. The function adds the
346 * new iocb to txcmplq of the given ring. This function always returns
347 * 0. If this function is called for ELS ring, this function checks if
348 * there is a vport associated with the ELS command. This function also
349 * starts els_tmofunc timer if this is an ELS command.
350 **/
dea31012005-04-17 16:05:31 -0500351static int
James Smart2e0fef82007-06-17 19:56:36 -0500352lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
353 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500354{
dea31012005-04-17 16:05:31 -0500355 list_add_tail(&piocb->list, &pring->txcmplq);
356 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500357 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
358 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
359 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
360 if (!piocb->vport)
361 BUG();
362 else
363 mod_timer(&piocb->vport->els_tmofunc,
364 jiffies + HZ * (phba->fc_ratov << 1));
365 }
366
dea31012005-04-17 16:05:31 -0500367
James Smart2e0fef82007-06-17 19:56:36 -0500368 return 0;
dea31012005-04-17 16:05:31 -0500369}
370
James Smarte59058c2008-08-24 21:49:00 -0400371/**
372 * lpfc_sli_ringtx_get: Get first element of the txq.
373 * @phba: Pointer to HBA context object.
374 * @pring: Pointer to driver SLI ring object.
375 *
376 * This function is called with hbalock held to get next
377 * iocb in txq of the given ring. If there is any iocb in
378 * the txq, the function returns first iocb in the list after
379 * removing the iocb from the list, else it returns NULL.
380 **/
dea31012005-04-17 16:05:31 -0500381static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500382lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500383{
dea31012005-04-17 16:05:31 -0500384 struct lpfc_iocbq *cmd_iocb;
385
James Smart858c9f62007-06-17 19:56:39 -0500386 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
387 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500388 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500389 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500390}
391
James Smarte59058c2008-08-24 21:49:00 -0400392/**
393 * lpfc_sli_next_iocb_slot: Get next iocb slot in the ring.
394 * @phba: Pointer to HBA context object.
395 * @pring: Pointer to driver SLI ring object.
396 *
397 * This function is called with hbalock held and the caller must post the
398 * iocb without releasing the lock. If the caller releases the lock,
399 * iocb slot returned by the function is not guaranteed to be available.
400 * The function returns pointer to the next available iocb slot if there
401 * is available slot in the ring, else it returns NULL.
402 * If the get index of the ring is ahead of the put index, the function
403 * will post an error attention event to the worker thread to take the
404 * HBA to offline state.
405 **/
dea31012005-04-17 16:05:31 -0500406static IOCB_t *
407lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
408{
James Smart34b02dc2008-08-24 21:49:55 -0400409 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500410 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500411 if ((pring->next_cmdidx == pring->cmdidx) &&
412 (++pring->next_cmdidx >= max_cmd_idx))
413 pring->next_cmdidx = 0;
414
415 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
416
417 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
418
419 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
420 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400421 "0315 Ring %d issue: portCmdGet %d "
dea31012005-04-17 16:05:31 -0500422 "is bigger then cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400423 pring->ringno,
dea31012005-04-17 16:05:31 -0500424 pring->local_getidx, max_cmd_idx);
425
James Smart2e0fef82007-06-17 19:56:36 -0500426 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500427 /*
428 * All error attention handlers are posted to
429 * worker thread
430 */
431 phba->work_ha |= HA_ERATT;
432 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500433
James Smart5e9d9b82008-06-14 22:52:53 -0400434 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500435
436 return NULL;
437 }
438
439 if (pring->local_getidx == pring->next_cmdidx)
440 return NULL;
441 }
442
James Smarted957682007-06-17 19:56:37 -0500443 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500444}
445
James Smarte59058c2008-08-24 21:49:00 -0400446/**
447 * lpfc_sli_next_iotag: Get an iotag for the iocb.
448 * @phba: Pointer to HBA context object.
449 * @iocbq: Pointer to driver iocb object.
450 *
451 * This function gets an iotag for the iocb. If there is no unused iotag and
452 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
453 * array and assigns a new iotag.
454 * The function returns the allocated iotag if successful, else returns zero.
455 * Zero is not a valid iotag.
456 * The caller is not required to hold any lock.
457 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500458uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500459lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500460{
James Smart2e0fef82007-06-17 19:56:36 -0500461 struct lpfc_iocbq **new_arr;
462 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500463 size_t new_len;
464 struct lpfc_sli *psli = &phba->sli;
465 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500466
James Smart2e0fef82007-06-17 19:56:36 -0500467 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500468 iotag = psli->last_iotag;
469 if(++iotag < psli->iocbq_lookup_len) {
470 psli->last_iotag = iotag;
471 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500472 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500473 iocbq->iotag = iotag;
474 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500475 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500476 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
477 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500478 spin_unlock_irq(&phba->hbalock);
479 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500480 GFP_KERNEL);
481 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500482 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500483 old_arr = psli->iocbq_lookup;
484 if (new_len <= psli->iocbq_lookup_len) {
485 /* highly unprobable case */
486 kfree(new_arr);
487 iotag = psli->last_iotag;
488 if(++iotag < psli->iocbq_lookup_len) {
489 psli->last_iotag = iotag;
490 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500491 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500492 iocbq->iotag = iotag;
493 return iotag;
494 }
James Smart2e0fef82007-06-17 19:56:36 -0500495 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500496 return 0;
497 }
498 if (psli->iocbq_lookup)
499 memcpy(new_arr, old_arr,
500 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -0400501 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -0500502 psli->iocbq_lookup = new_arr;
503 psli->iocbq_lookup_len = new_len;
504 psli->last_iotag = iotag;
505 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500506 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500507 iocbq->iotag = iotag;
508 kfree(old_arr);
509 return iotag;
510 }
James Smart8f6d98d2006-08-01 07:34:00 -0400511 } else
James Smart2e0fef82007-06-17 19:56:36 -0500512 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500513
James Bottomley604a3e32005-10-29 10:28:33 -0500514 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400515 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
516 psli->last_iotag);
dea31012005-04-17 16:05:31 -0500517
James Bottomley604a3e32005-10-29 10:28:33 -0500518 return 0;
dea31012005-04-17 16:05:31 -0500519}
520
James Smarte59058c2008-08-24 21:49:00 -0400521/**
522 * lpfc_sli_submit_iocb: Submit an iocb to the firmware.
523 * @phba: Pointer to HBA context object.
524 * @pring: Pointer to driver SLI ring object.
525 * @iocb: Pointer to iocb slot in the ring.
526 * @nextiocb: Pointer to driver iocb object which need to be
527 * posted to firmware.
528 *
529 * This function is called with hbalock held to post a new iocb to
530 * the firmware. This function copies the new iocb to ring iocb slot and
531 * updates the ring pointers. It adds the new iocb to txcmplq if there is
532 * a completion call back for this iocb else the function will free the
533 * iocb object.
534 **/
dea31012005-04-17 16:05:31 -0500535static void
536lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
537 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
538{
539 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500540 * Set up an iotag
dea31012005-04-17 16:05:31 -0500541 */
James Bottomley604a3e32005-10-29 10:28:33 -0500542 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500543
James Smarta58cbd52007-08-02 11:09:43 -0400544 if (pring->ringno == LPFC_ELS_RING) {
545 lpfc_debugfs_slow_ring_trc(phba,
546 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
547 *(((uint32_t *) &nextiocb->iocb) + 4),
548 *(((uint32_t *) &nextiocb->iocb) + 6),
549 *(((uint32_t *) &nextiocb->iocb) + 7));
550 }
551
dea31012005-04-17 16:05:31 -0500552 /*
553 * Issue iocb command to adapter
554 */
James Smart92d7f7b2007-06-17 19:56:38 -0500555 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500556 wmb();
557 pring->stats.iocb_cmd++;
558
559 /*
560 * If there is no completion routine to call, we can release the
561 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
562 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
563 */
564 if (nextiocb->iocb_cmpl)
565 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500566 else
James Smart2e0fef82007-06-17 19:56:36 -0500567 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500568
569 /*
570 * Let the HBA know what IOCB slot will be the next one the
571 * driver will put a command into.
572 */
573 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500574 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500575}
576
James Smarte59058c2008-08-24 21:49:00 -0400577/**
578 * lpfc_sli_update_full_ring: Update the chip attention register.
579 * @phba: Pointer to HBA context object.
580 * @pring: Pointer to driver SLI ring object.
581 *
582 * The caller is not required to hold any lock for calling this function.
583 * This function updates the chip attention bits for the ring to inform firmware
584 * that there are pending work to be done for this ring and requests an
585 * interrupt when there is space available in the ring. This function is
586 * called when the driver is unable to post more iocbs to the ring due
587 * to unavailability of space in the ring.
588 **/
dea31012005-04-17 16:05:31 -0500589static void
James Smart2e0fef82007-06-17 19:56:36 -0500590lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500591{
592 int ringno = pring->ringno;
593
594 pring->flag |= LPFC_CALL_RING_AVAILABLE;
595
596 wmb();
597
598 /*
599 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
600 * The HBA will tell us when an IOCB entry is available.
601 */
602 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
603 readl(phba->CAregaddr); /* flush */
604
605 pring->stats.iocb_cmd_full++;
606}
607
James Smarte59058c2008-08-24 21:49:00 -0400608/**
609 * lpfc_sli_update_ring: Update chip attention register.
610 * @phba: Pointer to HBA context object.
611 * @pring: Pointer to driver SLI ring object.
612 *
613 * This function updates the chip attention register bit for the
614 * given ring to inform HBA that there is more work to be done
615 * in this ring. The caller is not required to hold any lock.
616 **/
dea31012005-04-17 16:05:31 -0500617static void
James Smart2e0fef82007-06-17 19:56:36 -0500618lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500619{
620 int ringno = pring->ringno;
621
622 /*
623 * Tell the HBA that there is work to do in this ring.
624 */
James Smart34b02dc2008-08-24 21:49:55 -0400625 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
626 wmb();
627 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
628 readl(phba->CAregaddr); /* flush */
629 }
dea31012005-04-17 16:05:31 -0500630}
631
James Smarte59058c2008-08-24 21:49:00 -0400632/**
633 * lpfc_sli_resume_iocb: Process iocbs in the txq.
634 * @phba: Pointer to HBA context object.
635 * @pring: Pointer to driver SLI ring object.
636 *
637 * This function is called with hbalock held to post pending iocbs
638 * in the txq to the firmware. This function is called when driver
639 * detects space available in the ring.
640 **/
dea31012005-04-17 16:05:31 -0500641static void
James Smart2e0fef82007-06-17 19:56:36 -0500642lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500643{
644 IOCB_t *iocb;
645 struct lpfc_iocbq *nextiocb;
646
647 /*
648 * Check to see if:
649 * (a) there is anything on the txq to send
650 * (b) link is up
651 * (c) link attention events can be processed (fcp ring only)
652 * (d) IOCB processing is not blocked by the outstanding mbox command.
653 */
654 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500655 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500656 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -0400657 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -0500658
659 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
660 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
661 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
662
663 if (iocb)
664 lpfc_sli_update_ring(phba, pring);
665 else
666 lpfc_sli_update_full_ring(phba, pring);
667 }
668
669 return;
670}
671
James Smarte59058c2008-08-24 21:49:00 -0400672/**
673 * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
674 * @phba: Pointer to HBA context object.
675 * @hbqno: HBQ number.
676 *
677 * This function is called with hbalock held to get the next
678 * available slot for the given HBQ. If there is free slot
679 * available for the HBQ it will return pointer to the next available
680 * HBQ entry else it will return NULL.
681 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100682static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500683lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
684{
685 struct hbq_s *hbqp = &phba->hbqs[hbqno];
686
687 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
688 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
689 hbqp->next_hbqPutIdx = 0;
690
691 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500692 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500693 uint32_t getidx = le32_to_cpu(raw_index);
694
695 hbqp->local_hbqGetIdx = getidx;
696
697 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
698 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500699 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400700 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500701 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400702 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500703 hbqp->entry_count);
704
705 phba->link_state = LPFC_HBA_ERROR;
706 return NULL;
707 }
708
709 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
710 return NULL;
711 }
712
James Smart51ef4c22007-08-02 11:10:31 -0400713 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
714 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500715}
716
James Smarte59058c2008-08-24 21:49:00 -0400717/**
718 * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
719 * @phba: Pointer to HBA context object.
720 *
721 * This function is called with no lock held to free all the
722 * hbq buffers while uninitializing the SLI interface. It also
723 * frees the HBQ buffers returned by the firmware but not yet
724 * processed by the upper layers.
725 **/
James Smarted957682007-06-17 19:56:37 -0500726void
727lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
728{
James Smart92d7f7b2007-06-17 19:56:38 -0500729 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
730 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -0500731 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -0400732 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -0500733 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500734
James Smart51ef4c22007-08-02 11:10:31 -0400735 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500736 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -0500737 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -0400738 for (i = 0; i < hbq_count; ++i) {
739 list_for_each_entry_safe(dmabuf, next_dmabuf,
740 &phba->hbqs[i].hbq_buffer_list, list) {
741 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
742 list_del(&hbq_buf->dbuf.list);
743 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
744 }
James Smarta8adb832007-10-27 13:37:53 -0400745 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -0500746 }
James Smart3163f722008-02-08 18:50:25 -0500747 /* Return all HBQ buffer that are in-fly */
748 list_for_each_entry_safe(dmabuf, next_dmabuf,
749 &phba->hbqbuf_in_list, list) {
750 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
751 list_del(&hbq_buf->dbuf.list);
752 if (hbq_buf->tag == -1) {
753 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
754 (phba, hbq_buf);
755 } else {
756 hbqno = hbq_buf->tag >> 16;
757 if (hbqno >= LPFC_MAX_HBQS)
758 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
759 (phba, hbq_buf);
760 else
761 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
762 hbq_buf);
763 }
764 }
765
766 /* Mark the HBQs not in use */
767 phba->hbq_in_use = 0;
768 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -0500769}
770
James Smarte59058c2008-08-24 21:49:00 -0400771/**
772 * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
773 * @phba: Pointer to HBA context object.
774 * @hbqno: HBQ number.
775 * @hbq_buf: Pointer to HBQ buffer.
776 *
777 * This function is called with the hbalock held to post a
778 * hbq buffer to the firmware. If the function finds an empty
779 * slot in the HBQ, it will post the buffer. The function will return
780 * pointer to the hbq entry if it successfully post the buffer
781 * else it will return NULL.
782 **/
James Smart51ef4c22007-08-02 11:10:31 -0400783static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500784lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500785 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500786{
787 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500788 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500789
790 /* Get next HBQ entry slot to use */
791 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
792 if (hbqe) {
793 struct hbq_s *hbqp = &phba->hbqs[hbqno];
794
James Smart92d7f7b2007-06-17 19:56:38 -0500795 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
796 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400797 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500798 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500799 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
800 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
801 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500802 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
803 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500804 /* flush */
James Smarted957682007-06-17 19:56:37 -0500805 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400806 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smarted957682007-06-17 19:56:37 -0500807 }
James Smart51ef4c22007-08-02 11:10:31 -0400808 return hbqe;
James Smarted957682007-06-17 19:56:37 -0500809}
810
James Smarte59058c2008-08-24 21:49:00 -0400811/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -0500812static struct lpfc_hbq_init lpfc_els_hbq = {
813 .rn = 1,
814 .entry_count = 200,
815 .mask_count = 0,
816 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400817 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500818 .buffer_count = 0,
819 .init_count = 20,
820 .add_count = 5,
821};
James Smarted957682007-06-17 19:56:37 -0500822
James Smarte59058c2008-08-24 21:49:00 -0400823/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -0400824static struct lpfc_hbq_init lpfc_extra_hbq = {
825 .rn = 1,
826 .entry_count = 200,
827 .mask_count = 0,
828 .profile = 0,
829 .ring_mask = (1 << LPFC_EXTRA_RING),
830 .buffer_count = 0,
831 .init_count = 0,
832 .add_count = 5,
833};
834
James Smarte59058c2008-08-24 21:49:00 -0400835/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -0400836struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500837 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400838 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500839};
840
James Smarte59058c2008-08-24 21:49:00 -0400841/**
842 * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
843 * @phba: Pointer to HBA context object.
844 * @hbqno: HBQ number.
845 * @count: Number of HBQ buffers to be posted.
846 *
James Smartd7c255b2008-08-24 21:50:00 -0400847 * This function is called with no lock held to post more hbq buffers to the
848 * given HBQ. The function returns the number of HBQ buffers successfully
849 * posted.
James Smarte59058c2008-08-24 21:49:00 -0400850 **/
James Smart311464e2007-08-02 11:10:37 -0400851static int
James Smart92d7f7b2007-06-17 19:56:38 -0500852lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
853{
James Smartd7c255b2008-08-24 21:50:00 -0400854 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -0500855 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500856 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -0400857 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700858 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -0400859 return 0;
James Smart51ef4c22007-08-02 11:10:31 -0400860
James Smartd7c255b2008-08-24 21:50:00 -0400861 if ((phba->hbqs[hbqno].buffer_count + count) >
862 lpfc_hbq_defs[hbqno]->entry_count)
863 count = lpfc_hbq_defs[hbqno]->entry_count -
864 phba->hbqs[hbqno].buffer_count;
865 if (!count)
866 return 0;
867 /* Allocate HBQ entries */
868 for (i = 0; i < count; i++) {
869 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
870 if (!hbq_buffer)
871 break;
872 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
873 }
James Smart3163f722008-02-08 18:50:25 -0500874 /* Check whether HBQ is still in use */
875 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -0700876 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -0400877 goto err;
878 while (!list_empty(&hbq_buf_list)) {
879 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
880 dbuf.list);
881 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
882 (hbqno << 16));
883 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -0400884 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -0400885 posted++;
886 } else
James Smart51ef4c22007-08-02 11:10:31 -0400887 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -0500888 }
James Smart3163f722008-02-08 18:50:25 -0500889 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -0400890 return posted;
891err:
892 spin_unlock_irqrestore(&phba->hbalock, flags);
893 while (!list_empty(&hbq_buf_list)) {
894 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
895 dbuf.list);
896 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
897 }
James Smart92d7f7b2007-06-17 19:56:38 -0500898 return 0;
James Smarted957682007-06-17 19:56:37 -0500899}
900
James Smarte59058c2008-08-24 21:49:00 -0400901/**
902 * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
903 * @phba: Pointer to HBA context object.
904 * @qno: HBQ number.
905 *
906 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -0400907 * is called with no lock held. The function returns the number of HBQ entries
908 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400909 **/
James Smarted957682007-06-17 19:56:37 -0500910int
James Smart92d7f7b2007-06-17 19:56:38 -0500911lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500912{
James Smart92d7f7b2007-06-17 19:56:38 -0500913 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
914 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -0500915}
916
James Smarte59058c2008-08-24 21:49:00 -0400917/**
918 * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
919 * @phba: Pointer to HBA context object.
920 * @qno: HBQ queue number.
921 *
922 * This function is called from SLI initialization code path with
923 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -0400924 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -0400925 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100926static int
James Smart92d7f7b2007-06-17 19:56:38 -0500927lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -0500928{
James Smart92d7f7b2007-06-17 19:56:38 -0500929 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
930 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -0500931}
932
James Smarte59058c2008-08-24 21:49:00 -0400933/**
934 * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
935 * @phba: Pointer to HBA context object.
936 * @tag: Tag of the hbq buffer.
937 *
938 * This function is called with hbalock held. This function searches
939 * for the hbq buffer associated with the given tag in the hbq buffer
940 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
941 * it returns NULL.
942 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100943static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -0500944lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
945{
James Smart92d7f7b2007-06-17 19:56:38 -0500946 struct lpfc_dmabuf *d_buf;
947 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -0400948 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500949
James Smart51ef4c22007-08-02 11:10:31 -0400950 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +0200951 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -0400952 return NULL;
953
954 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -0500955 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -0400956 if (hbq_buf->tag == tag) {
James Smart92d7f7b2007-06-17 19:56:38 -0500957 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -0500958 }
959 }
James Smart92d7f7b2007-06-17 19:56:38 -0500960 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400961 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -0400962 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -0500963 return NULL;
James Smarted957682007-06-17 19:56:37 -0500964}
965
James Smarte59058c2008-08-24 21:49:00 -0400966/**
967 * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
968 * @phba: Pointer to HBA context object.
969 * @hbq_buffer: Pointer to HBQ buffer.
970 *
971 * This function is called with hbalock. This function gives back
972 * the hbq buffer to firmware. If the HBQ does not have space to
973 * post the buffer, it will free the buffer.
974 **/
James Smarted957682007-06-17 19:56:37 -0500975void
James Smart51ef4c22007-08-02 11:10:31 -0400976lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -0500977{
978 uint32_t hbqno;
979
James Smart51ef4c22007-08-02 11:10:31 -0400980 if (hbq_buffer) {
981 hbqno = hbq_buffer->tag >> 16;
982 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
983 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
984 }
James Smarted957682007-06-17 19:56:37 -0500985 }
986}
987
James Smarte59058c2008-08-24 21:49:00 -0400988/**
989 * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
990 * @mbxCommand: mailbox command code.
991 *
992 * This function is called by the mailbox event handler function to verify
993 * that the completed mailbox command is a legitimate mailbox command. If the
994 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
995 * and the mailbox event handler will take the HBA offline.
996 **/
dea31012005-04-17 16:05:31 -0500997static int
998lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
999{
1000 uint8_t ret;
1001
1002 switch (mbxCommand) {
1003 case MBX_LOAD_SM:
1004 case MBX_READ_NV:
1005 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001006 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001007 case MBX_RUN_BIU_DIAG:
1008 case MBX_INIT_LINK:
1009 case MBX_DOWN_LINK:
1010 case MBX_CONFIG_LINK:
1011 case MBX_CONFIG_RING:
1012 case MBX_RESET_RING:
1013 case MBX_READ_CONFIG:
1014 case MBX_READ_RCONFIG:
1015 case MBX_READ_SPARM:
1016 case MBX_READ_STATUS:
1017 case MBX_READ_RPI:
1018 case MBX_READ_XRI:
1019 case MBX_READ_REV:
1020 case MBX_READ_LNK_STAT:
1021 case MBX_REG_LOGIN:
1022 case MBX_UNREG_LOGIN:
1023 case MBX_READ_LA:
1024 case MBX_CLEAR_LA:
1025 case MBX_DUMP_MEMORY:
1026 case MBX_DUMP_CONTEXT:
1027 case MBX_RUN_DIAGS:
1028 case MBX_RESTART:
1029 case MBX_UPDATE_CFG:
1030 case MBX_DOWN_LOAD:
1031 case MBX_DEL_LD_ENTRY:
1032 case MBX_RUN_PROGRAM:
1033 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001034 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001035 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001036 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001037 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001038 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001039 case MBX_LOAD_AREA:
1040 case MBX_RUN_BIU_DIAG64:
1041 case MBX_CONFIG_PORT:
1042 case MBX_READ_SPARM64:
1043 case MBX_READ_RPI64:
1044 case MBX_REG_LOGIN64:
1045 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001046 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001047 case MBX_SET_DEBUG:
1048 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001049 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001050 case MBX_REG_VPI:
1051 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001052 case MBX_HEARTBEAT:
dea31012005-04-17 16:05:31 -05001053 ret = mbxCommand;
1054 break;
1055 default:
1056 ret = MBX_SHUTDOWN;
1057 break;
1058 }
James Smart2e0fef82007-06-17 19:56:36 -05001059 return ret;
dea31012005-04-17 16:05:31 -05001060}
James Smarte59058c2008-08-24 21:49:00 -04001061
1062/**
1063 * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
1064 * lpfc_sli_issue_mbox_wait.
1065 * @phba: Pointer to HBA context object.
1066 * @pmboxq: Pointer to mailbox command.
1067 *
1068 * This is completion handler function for mailbox commands issued from
1069 * lpfc_sli_issue_mbox_wait function. This function is called by the
1070 * mailbox event handler function with no lock held. This function
1071 * will wake up thread waiting on the wait queue pointed by context1
1072 * of the mailbox.
1073 **/
dea31012005-04-17 16:05:31 -05001074static void
James Smart2e0fef82007-06-17 19:56:36 -05001075lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001076{
1077 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001078 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001079
1080 /*
1081 * If pdone_q is empty, the driver thread gave up waiting and
1082 * continued running.
1083 */
James Smart7054a602007-04-25 09:52:34 -04001084 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001085 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001086 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1087 if (pdone_q)
1088 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001089 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001090 return;
1091}
1092
James Smarte59058c2008-08-24 21:49:00 -04001093
1094/**
1095 * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
1096 * @phba: Pointer to HBA context object.
1097 * @pmb: Pointer to mailbox object.
1098 *
1099 * This function is the default mailbox completion handler. It
1100 * frees the memory resources associated with the completed mailbox
1101 * command. If the completed command is a REG_LOGIN mailbox command,
1102 * this function will issue a UREG_LOGIN to re-claim the RPI.
1103 **/
dea31012005-04-17 16:05:31 -05001104void
James Smart2e0fef82007-06-17 19:56:36 -05001105lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001106{
1107 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -04001108 uint16_t rpi;
1109 int rc;
1110
dea31012005-04-17 16:05:31 -05001111 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001112
dea31012005-04-17 16:05:31 -05001113 if (mp) {
1114 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1115 kfree(mp);
1116 }
James Smart7054a602007-04-25 09:52:34 -04001117
1118 /*
1119 * If a REG_LOGIN succeeded after node is destroyed or node
1120 * is in re-discovery driver need to cleanup the RPI.
1121 */
James Smart2e0fef82007-06-17 19:56:36 -05001122 if (!(phba->pport->load_flag & FC_UNLOADING) &&
1123 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1124 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -04001125
1126 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -05001127 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1128 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001129 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1130 if (rc != MBX_NOT_FINISHED)
1131 return;
1132 }
1133
James Smart2e0fef82007-06-17 19:56:36 -05001134 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001135 return;
1136}
1137
James Smarte59058c2008-08-24 21:49:00 -04001138/**
1139 * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
1140 * @phba: Pointer to HBA context object.
1141 *
1142 * This function is called with no lock held. This function processes all
1143 * the completed mailbox commands and gives it to upper layers. The interrupt
1144 * service routine processes mailbox completion interrupt and adds completed
1145 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1146 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1147 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1148 * function returns the mailbox commands to the upper layer by calling the
1149 * completion handler function of each mailbox.
1150 **/
dea31012005-04-17 16:05:31 -05001151int
James Smart2e0fef82007-06-17 19:56:36 -05001152lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001153{
James Smart92d7f7b2007-06-17 19:56:38 -05001154 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001155 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001156 int rc;
1157 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001158
1159 phba->sli.slistat.mbox_event++;
1160
James Smart92d7f7b2007-06-17 19:56:38 -05001161 /* Get all completed mailboxe buffers into the cmplq */
1162 spin_lock_irq(&phba->hbalock);
1163 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1164 spin_unlock_irq(&phba->hbalock);
1165
dea31012005-04-17 16:05:31 -05001166 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001167 do {
1168 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1169 if (pmb == NULL)
1170 break;
1171
dea31012005-04-17 16:05:31 -05001172 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -05001173
James Smart858c9f62007-06-17 19:56:39 -05001174 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1175 if (pmb->vport) {
1176 lpfc_debugfs_disc_trc(pmb->vport,
1177 LPFC_DISC_TRC_MBOX_VPORT,
1178 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1179 (uint32_t)pmbox->mbxCommand,
1180 pmbox->un.varWords[0],
1181 pmbox->un.varWords[1]);
1182 }
1183 else {
1184 lpfc_debugfs_disc_trc(phba->pport,
1185 LPFC_DISC_TRC_MBOX,
1186 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1187 (uint32_t)pmbox->mbxCommand,
1188 pmbox->un.varWords[0],
1189 pmbox->un.varWords[1]);
1190 }
1191 }
1192
dea31012005-04-17 16:05:31 -05001193 /*
1194 * It is a fatal error if unknown mbox command completion.
1195 */
1196 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1197 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001198 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001199 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001200 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -05001201 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001202 pmb->vport ? pmb->vport->vpi : 0,
1203 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -05001204 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001205 phba->work_hs = HS_FFER3;
1206 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001207 continue;
dea31012005-04-17 16:05:31 -05001208 }
1209
dea31012005-04-17 16:05:31 -05001210 if (pmbox->mbxStatus) {
1211 phba->sli.slistat.mbox_stat_err++;
1212 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1213 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001214 lpfc_printf_log(phba, KERN_INFO,
1215 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001216 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001217 "error - RETRYing Data: x%x "
1218 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001219 pmb->vport ? pmb->vport->vpi :0,
1220 pmbox->mbxCommand,
1221 pmbox->mbxStatus,
1222 pmbox->un.varWords[0],
1223 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001224 pmbox->mbxStatus = 0;
1225 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -05001226 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001227 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001228 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001229 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1230 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -05001231 continue;
dea31012005-04-17 16:05:31 -05001232 }
1233 }
1234
1235 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001236 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001237 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -05001238 "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 -05001239 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001240 pmbox->mbxCommand,
1241 pmb->mbox_cmpl,
1242 *((uint32_t *) pmbox),
1243 pmbox->un.varWords[0],
1244 pmbox->un.varWords[1],
1245 pmbox->un.varWords[2],
1246 pmbox->un.varWords[3],
1247 pmbox->un.varWords[4],
1248 pmbox->un.varWords[5],
1249 pmbox->un.varWords[6],
1250 pmbox->un.varWords[7]);
1251
James Smart92d7f7b2007-06-17 19:56:38 -05001252 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001253 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001254 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001255 return 0;
dea31012005-04-17 16:05:31 -05001256}
James Smart92d7f7b2007-06-17 19:56:38 -05001257
James Smarte59058c2008-08-24 21:49:00 -04001258/**
1259 * lpfc_sli_replace_hbqbuff: Replace the HBQ buffer with a new buffer.
1260 * @phba: Pointer to HBA context object.
1261 * @tag: Tag for the HBQ buffer.
1262 *
1263 * This function is called from unsolicited event handler code path to get the
1264 * HBQ buffer associated with an unsolicited iocb. This function is called with
1265 * no lock held. It returns the buffer associated with the given tag and posts
James Smartd7c255b2008-08-24 21:50:00 -04001266 * another buffer to the firmware. Note that the new buffer must be allocated
1267 * before taking the hbalock and that the hba lock must be held until it is
1268 * finished with the hbq entry swap.
James Smarte59058c2008-08-24 21:49:00 -04001269 **/
James Smart92d7f7b2007-06-17 19:56:38 -05001270static struct lpfc_dmabuf *
1271lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
1272{
1273 struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
James Smart51ef4c22007-08-02 11:10:31 -04001274 uint32_t hbqno;
1275 void *virt; /* virtual address ptr */
1276 dma_addr_t phys; /* mapped address */
James Smart3163f722008-02-08 18:50:25 -05001277 unsigned long flags;
1278
James Smartd7c255b2008-08-24 21:50:00 -04001279 hbqno = tag >> 16;
1280 new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart3163f722008-02-08 18:50:25 -05001281 /* Check whether HBQ is still in use */
1282 spin_lock_irqsave(&phba->hbalock, flags);
1283 if (!phba->hbq_in_use) {
James Smartd7c255b2008-08-24 21:50:00 -04001284 if (new_hbq_entry)
1285 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1286 new_hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001287 spin_unlock_irqrestore(&phba->hbalock, flags);
1288 return NULL;
1289 }
James Smart92d7f7b2007-06-17 19:56:38 -05001290
1291 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
James Smart3163f722008-02-08 18:50:25 -05001292 if (hbq_entry == NULL) {
James Smartd7c255b2008-08-24 21:50:00 -04001293 if (new_hbq_entry)
1294 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1295 new_hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001296 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05001297 return NULL;
James Smart3163f722008-02-08 18:50:25 -05001298 }
James Smart92d7f7b2007-06-17 19:56:38 -05001299 list_del(&hbq_entry->dbuf.list);
James Smart51ef4c22007-08-02 11:10:31 -04001300
James Smart3163f722008-02-08 18:50:25 -05001301 if (new_hbq_entry == NULL) {
1302 list_add_tail(&hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1303 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05001304 return &hbq_entry->dbuf;
James Smart3163f722008-02-08 18:50:25 -05001305 }
James Smart92d7f7b2007-06-17 19:56:38 -05001306 new_hbq_entry->tag = -1;
James Smart51ef4c22007-08-02 11:10:31 -04001307 phys = new_hbq_entry->dbuf.phys;
1308 virt = new_hbq_entry->dbuf.virt;
1309 new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
1310 new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
1311 hbq_entry->dbuf.phys = phys;
1312 hbq_entry->dbuf.virt = virt;
James Smart92d7f7b2007-06-17 19:56:38 -05001313 lpfc_sli_free_hbq(phba, hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001314 list_add_tail(&new_hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1315 spin_unlock_irqrestore(&phba->hbalock, flags);
1316
James Smart92d7f7b2007-06-17 19:56:38 -05001317 return &new_hbq_entry->dbuf;
1318}
1319
James Smarte59058c2008-08-24 21:49:00 -04001320/**
1321 * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
1322 * @phba: Pointer to HBA context object.
1323 * @pring: Pointer to driver SLI ring object.
1324 * @tag: buffer tag.
1325 *
1326 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1327 * is set in the tag the buffer is posted for a particular exchange,
1328 * the function will return the buffer without replacing the buffer.
1329 * If the buffer is for unsolicited ELS or CT traffic, this function
1330 * returns the buffer and also posts another buffer to the firmware.
1331 **/
James Smart76bb24e2007-10-27 13:38:00 -04001332static struct lpfc_dmabuf *
1333lpfc_sli_get_buff(struct lpfc_hba *phba,
1334 struct lpfc_sli_ring *pring,
1335 uint32_t tag)
1336{
1337 if (tag & QUE_BUFTAG_BIT)
1338 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1339 else
1340 return lpfc_sli_replace_hbqbuff(phba, tag);
1341}
James Smart57127f12007-10-27 13:37:05 -04001342
James Smarte59058c2008-08-24 21:49:00 -04001343
1344/**
1345 * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
1346 * @phba: Pointer to HBA context object.
1347 * @pring: Pointer to driver SLI ring object.
1348 * @saveq: Pointer to the unsolicited iocb.
1349 *
1350 * This function is called with no lock held by the ring event handler
1351 * when there is an unsolicited iocb posted to the response ring by the
1352 * firmware. This function gets the buffer associated with the iocbs
1353 * and calls the event handler for the ring. This function handles both
1354 * qring buffers and hbq buffers.
1355 * When the function returns 1 the caller can free the iocb object otherwise
1356 * upper layer functions will free the iocb objects.
1357 **/
dea31012005-04-17 16:05:31 -05001358static int
1359lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1360 struct lpfc_iocbq *saveq)
1361{
1362 IOCB_t * irsp;
1363 WORD5 * w5p;
1364 uint32_t Rctl, Type;
1365 uint32_t match, i;
James Smart76bb24e2007-10-27 13:38:00 -04001366 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001367 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001368
1369 match = 0;
1370 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001371
James Smart9c2face2008-01-11 01:53:18 -05001372 if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
1373 return 1;
James Smart57127f12007-10-27 13:37:05 -04001374 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1375 if (pring->lpfc_sli_rcv_async_status)
1376 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1377 else
1378 lpfc_printf_log(phba,
1379 KERN_WARNING,
1380 LOG_SLI,
1381 "0316 Ring %d handler: unexpected "
1382 "ASYNC_STATUS iocb received evt_code "
1383 "0x%x\n",
1384 pring->ringno,
1385 irsp->un.asyncstat.evt_code);
1386 return 1;
1387 }
1388
James Smart3163f722008-02-08 18:50:25 -05001389 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1390 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1391 if (irsp->ulpBdeCount > 0) {
1392 dmzbuf = lpfc_sli_get_buff(phba, pring,
1393 irsp->un.ulpWord[3]);
1394 lpfc_in_buf_free(phba, dmzbuf);
1395 }
1396
1397 if (irsp->ulpBdeCount > 1) {
1398 dmzbuf = lpfc_sli_get_buff(phba, pring,
1399 irsp->unsli3.sli3Words[3]);
1400 lpfc_in_buf_free(phba, dmzbuf);
1401 }
1402
1403 if (irsp->ulpBdeCount > 2) {
1404 dmzbuf = lpfc_sli_get_buff(phba, pring,
1405 irsp->unsli3.sli3Words[7]);
1406 lpfc_in_buf_free(phba, dmzbuf);
1407 }
1408
1409 return 1;
1410 }
1411
James Smart92d7f7b2007-06-17 19:56:38 -05001412 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001413 if (irsp->ulpBdeCount != 0) {
1414 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001415 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001416 if (!saveq->context2)
1417 lpfc_printf_log(phba,
1418 KERN_ERR,
1419 LOG_SLI,
1420 "0341 Ring %d Cannot find buffer for "
1421 "an unsolicited iocb. tag 0x%x\n",
1422 pring->ringno,
1423 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001424 }
1425 if (irsp->ulpBdeCount == 2) {
1426 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04001427 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04001428 if (!saveq->context3)
1429 lpfc_printf_log(phba,
1430 KERN_ERR,
1431 LOG_SLI,
1432 "0342 Ring %d Cannot find buffer for an"
1433 " unsolicited iocb. tag 0x%x\n",
1434 pring->ringno,
1435 irsp->unsli3.sli3Words[7]);
1436 }
1437 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04001438 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04001439 if (irsp->ulpBdeCount != 0) {
1440 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1441 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05001442 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04001443 lpfc_printf_log(phba,
1444 KERN_ERR,
1445 LOG_SLI,
1446 "0343 Ring %d Cannot find "
1447 "buffer for an unsolicited iocb"
1448 ". tag 0x%x\n", pring->ringno,
1449 irsp->un.ulpWord[3]);
1450 }
1451 if (irsp->ulpBdeCount == 2) {
1452 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1453 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05001454 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04001455 lpfc_printf_log(phba,
1456 KERN_ERR,
1457 LOG_SLI,
1458 "0344 Ring %d Cannot find "
1459 "buffer for an unsolicited "
1460 "iocb. tag 0x%x\n",
1461 pring->ringno,
1462 irsp->unsli3.sli3Words[7]);
1463 }
1464 }
James Smart92d7f7b2007-06-17 19:56:38 -05001465 }
James Smart9c2face2008-01-11 01:53:18 -05001466 if (irsp->ulpBdeCount != 0 &&
1467 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1468 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1469 int found = 0;
1470
1471 /* search continue save q for same XRI */
1472 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1473 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1474 list_add_tail(&saveq->list, &iocbq->list);
1475 found = 1;
1476 break;
1477 }
1478 }
1479 if (!found)
1480 list_add_tail(&saveq->clist,
1481 &pring->iocb_continue_saveq);
1482 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1483 list_del_init(&iocbq->clist);
1484 saveq = iocbq;
1485 irsp = &(saveq->iocb);
1486 } else
1487 return 0;
1488 }
1489 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1490 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1491 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1492 Rctl = FC_ELS_REQ;
1493 Type = FC_ELS_DATA;
1494 } else {
1495 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1496 Rctl = w5p->hcsw.Rctl;
1497 Type = w5p->hcsw.Type;
1498
1499 /* Firmware Workaround */
1500 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1501 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1502 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1503 Rctl = FC_ELS_REQ;
1504 Type = FC_ELS_DATA;
1505 w5p->hcsw.Rctl = Rctl;
1506 w5p->hcsw.Type = Type;
1507 }
1508 }
James Smart92d7f7b2007-06-17 19:56:38 -05001509
dea31012005-04-17 16:05:31 -05001510 /* unSolicited Responses */
1511 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001512 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1513 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1514 saveq);
dea31012005-04-17 16:05:31 -05001515 match = 1;
1516 } else {
1517 /* We must search, based on rctl / type
1518 for the right routine */
James Smart9c2face2008-01-11 01:53:18 -05001519 for (i = 0; i < pring->num_mask; i++) {
1520 if ((pring->prt[i].rctl == Rctl)
1521 && (pring->prt[i].type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001522 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1523 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1524 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -05001525 match = 1;
1526 break;
1527 }
1528 }
1529 }
1530 if (match == 0) {
1531 /* Unexpected Rctl / Type received */
1532 /* Ring <ringno> handler: unexpected
1533 Rctl <Rctl> Type <Type> received */
James Smart92d7f7b2007-06-17 19:56:38 -05001534 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001535 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001536 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001537 pring->ringno, Rctl, Type);
dea31012005-04-17 16:05:31 -05001538 }
James Smart92d7f7b2007-06-17 19:56:38 -05001539 return 1;
dea31012005-04-17 16:05:31 -05001540}
1541
James Smarte59058c2008-08-24 21:49:00 -04001542/**
1543 * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
1544 * @phba: Pointer to HBA context object.
1545 * @pring: Pointer to driver SLI ring object.
1546 * @prspiocb: Pointer to response iocb object.
1547 *
1548 * This function looks up the iocb_lookup table to get the command iocb
1549 * corresponding to the given response iocb using the iotag of the
1550 * response iocb. This function is called with the hbalock held.
1551 * This function returns the command iocb object if it finds the command
1552 * iocb else returns NULL.
1553 **/
dea31012005-04-17 16:05:31 -05001554static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001555lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1556 struct lpfc_sli_ring *pring,
1557 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001558{
dea31012005-04-17 16:05:31 -05001559 struct lpfc_iocbq *cmd_iocb = NULL;
1560 uint16_t iotag;
1561
James Bottomley604a3e32005-10-29 10:28:33 -05001562 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001563
James Bottomley604a3e32005-10-29 10:28:33 -05001564 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1565 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001566 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001567 pring->txcmplq_cnt--;
1568 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001569 }
1570
dea31012005-04-17 16:05:31 -05001571 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001572 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001573 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001574 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001575 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001576 return NULL;
1577}
1578
James Smarte59058c2008-08-24 21:49:00 -04001579/**
1580 * lpfc_sli_process_sol_iocb: process solicited iocb completion.
1581 * @phba: Pointer to HBA context object.
1582 * @pring: Pointer to driver SLI ring object.
1583 * @saveq: Pointer to the response iocb to be processed.
1584 *
1585 * This function is called by the ring event handler for non-fcp
1586 * rings when there is a new response iocb in the response ring.
1587 * The caller is not required to hold any locks. This function
1588 * gets the command iocb associated with the response iocb and
1589 * calls the completion handler for the command iocb. If there
1590 * is no completion handler, the function will free the resources
1591 * associated with command iocb. If the response iocb is for
1592 * an already aborted command iocb, the status of the completion
1593 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1594 * This function always returns 1.
1595 **/
dea31012005-04-17 16:05:31 -05001596static int
James Smart2e0fef82007-06-17 19:56:36 -05001597lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001598 struct lpfc_iocbq *saveq)
1599{
James Smart2e0fef82007-06-17 19:56:36 -05001600 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001601 int rc = 1;
1602 unsigned long iflag;
1603
1604 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001605 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001606 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001607 spin_unlock_irqrestore(&phba->hbalock, iflag);
1608
dea31012005-04-17 16:05:31 -05001609 if (cmdiocbp) {
1610 if (cmdiocbp->iocb_cmpl) {
1611 /*
1612 * Post all ELS completions to the worker thread.
1613 * All other are passed to the completion callback.
1614 */
1615 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001616 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1617 cmdiocbp->iocb_flag &=
1618 ~LPFC_DRIVER_ABORTED;
1619 saveq->iocb.ulpStatus =
1620 IOSTAT_LOCAL_REJECT;
1621 saveq->iocb.un.ulpWord[4] =
1622 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05001623
1624 /* Firmware could still be in progress
1625 * of DMAing payload, so don't free data
1626 * buffer till after a hbeat.
1627 */
1628 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04001629 }
dea31012005-04-17 16:05:31 -05001630 }
James Smart2e0fef82007-06-17 19:56:36 -05001631 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001632 } else
1633 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001634 } else {
1635 /*
1636 * Unknown initiating command based on the response iotag.
1637 * This could be the case on the ELS ring because of
1638 * lpfc_els_abort().
1639 */
1640 if (pring->ringno != LPFC_ELS_RING) {
1641 /*
1642 * Ring <ringno> handler: unexpected completion IoTag
1643 * <IoTag>
1644 */
James Smarte8b62012007-08-02 11:10:09 -04001645 lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1646 "0322 Ring %d handler: "
1647 "unexpected completion IoTag x%x "
1648 "Data: x%x x%x x%x x%x\n",
1649 pring->ringno,
1650 saveq->iocb.ulpIoTag,
1651 saveq->iocb.ulpStatus,
1652 saveq->iocb.un.ulpWord[4],
1653 saveq->iocb.ulpCommand,
1654 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001655 }
1656 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001657
dea31012005-04-17 16:05:31 -05001658 return rc;
1659}
1660
James Smarte59058c2008-08-24 21:49:00 -04001661/**
1662 * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
1663 * @phba: Pointer to HBA context object.
1664 * @pring: Pointer to driver SLI ring object.
1665 *
1666 * This function is called from the iocb ring event handlers when
1667 * put pointer is ahead of the get pointer for a ring. This function signal
1668 * an error attention condition to the worker thread and the worker
1669 * thread will transition the HBA to offline state.
1670 **/
James Smart2e0fef82007-06-17 19:56:36 -05001671static void
1672lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001673{
James Smart34b02dc2008-08-24 21:49:55 -04001674 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001675 /*
1676 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1677 * rsp ring <portRspMax>
1678 */
1679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001680 "0312 Ring %d handler: portRspPut %d "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001681 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001682 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001683 pring->numRiocb);
1684
James Smart2e0fef82007-06-17 19:56:36 -05001685 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001686
1687 /*
1688 * All error attention handlers are posted to
1689 * worker thread
1690 */
1691 phba->work_ha |= HA_ERATT;
1692 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001693
James Smart5e9d9b82008-06-14 22:52:53 -04001694 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001695
1696 return;
1697}
1698
James Smarte59058c2008-08-24 21:49:00 -04001699/**
1700 * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
1701 * @phba: Pointer to HBA context object.
1702 *
1703 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1704 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1705 * is enabled.
1706 *
1707 * The caller does not hold any lock.
1708 * The function processes each response iocb in the response ring until it
1709 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1710 * LE bit set. The function will call the completion handler of the command iocb
1711 * if the response iocb indicates a completion for a command iocb or it is
1712 * an abort completion.
1713 **/
James Smart2e0fef82007-06-17 19:56:36 -05001714void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001715{
James Smart2e0fef82007-06-17 19:56:36 -05001716 struct lpfc_sli *psli = &phba->sli;
1717 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001718 IOCB_t *irsp = NULL;
1719 IOCB_t *entry = NULL;
1720 struct lpfc_iocbq *cmdiocbq = NULL;
1721 struct lpfc_iocbq rspiocbq;
James Smart34b02dc2008-08-24 21:49:55 -04001722 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001723 uint32_t status;
1724 uint32_t portRspPut, portRspMax;
1725 int type;
1726 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001727 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001728 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001729
1730 pring->stats.iocb_event++;
1731
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001732 /*
1733 * The next available response entry should never exceed the maximum
1734 * entries. If it does, treat it as an adapter hardware error.
1735 */
1736 portRspMax = pring->numRiocb;
1737 portRspPut = le32_to_cpu(pgp->rspPutInx);
1738 if (unlikely(portRspPut >= portRspMax)) {
1739 lpfc_sli_rsp_pointers_error(phba, pring);
1740 return;
1741 }
1742
1743 rmb();
1744 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001745 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001746 if (++pring->rspidx >= portRspMax)
1747 pring->rspidx = 0;
1748
1749 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1750 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001751 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001752 irsp = &rspiocbq.iocb;
1753 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1754 pring->stats.iocb_rsp++;
1755 rsp_cmpl++;
1756
1757 if (unlikely(irsp->ulpStatus)) {
1758 /* Rsp ring <ringno> error: IOCB */
1759 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001760 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001761 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001762 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001763 irsp->un.ulpWord[0],
1764 irsp->un.ulpWord[1],
1765 irsp->un.ulpWord[2],
1766 irsp->un.ulpWord[3],
1767 irsp->un.ulpWord[4],
1768 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001769 *(uint32_t *)&irsp->un1,
1770 *((uint32_t *)&irsp->un1 + 1));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001771 }
1772
1773 switch (type) {
1774 case LPFC_ABORT_IOCB:
1775 case LPFC_SOL_IOCB:
1776 /*
1777 * Idle exchange closed via ABTS from port. No iocb
1778 * resources need to be recovered.
1779 */
1780 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001781 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001782 "0314 IOCB cmd 0x%x "
1783 "processed. Skipping "
1784 "completion",
James Smartdca94792006-08-01 07:34:08 -04001785 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001786 break;
1787 }
1788
James Smart2e0fef82007-06-17 19:56:36 -05001789 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001790 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1791 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001792 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001793 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1794 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1795 &rspiocbq);
1796 }
1797 break;
1798 default:
1799 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1800 char adaptermsg[LPFC_MAX_ADPTMSG];
1801 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1802 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1803 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001804 dev_warn(&((phba->pcidev)->dev),
1805 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001806 phba->brd_no, adaptermsg);
1807 } else {
1808 /* Unknown IOCB command */
1809 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001810 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001811 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001812 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001813 irsp->ulpStatus,
1814 irsp->ulpIoTag,
1815 irsp->ulpContext);
1816 }
1817 break;
1818 }
1819
1820 /*
1821 * The response IOCB has been processed. Update the ring
1822 * pointer in SLIM. If the port response put pointer has not
1823 * been updated, sync the pgp->rspPutInx and fetch the new port
1824 * response put pointer.
1825 */
James Smarted957682007-06-17 19:56:37 -05001826 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001827
1828 if (pring->rspidx == portRspPut)
1829 portRspPut = le32_to_cpu(pgp->rspPutInx);
1830 }
1831
1832 ha_copy = readl(phba->HAregaddr);
1833 ha_copy >>= (LPFC_FCP_RING * 4);
1834
1835 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001836 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001837 pring->stats.iocb_rsp_full++;
1838 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1839 writel(status, phba->CAregaddr);
1840 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001841 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001842 }
1843 if ((ha_copy & HA_R0CE_RSP) &&
1844 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001845 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001846 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1847 pring->stats.iocb_cmd_empty++;
1848
1849 /* Force update of the local copy of cmdGetInx */
1850 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1851 lpfc_sli_resume_iocb(phba, pring);
1852
1853 if ((pring->lpfc_sli_cmd_available))
1854 (pring->lpfc_sli_cmd_available) (phba, pring);
1855
James Smart2e0fef82007-06-17 19:56:36 -05001856 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001857 }
1858
1859 return;
1860}
1861
James Smarte59058c2008-08-24 21:49:00 -04001862/**
1863 * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
1864 * @phba: Pointer to HBA context object.
1865 * @pring: Pointer to driver SLI ring object.
1866 * @mask: Host attention register mask for this ring.
1867 *
1868 * This function is called from the interrupt context when there is a ring
1869 * event for the fcp ring. The caller does not hold any lock.
1870 * The function processes each response iocb in the response ring until it
1871 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1872 * LE bit set. The function will call the completion handler of the command iocb
1873 * if the response iocb indicates a completion for a command iocb or it is
1874 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
1875 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05001876 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04001877 * to check it explicitly. This function always returns 1.
1878 **/
dea31012005-04-17 16:05:31 -05001879static int
James Smart2e0fef82007-06-17 19:56:36 -05001880lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1881 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001882{
James Smart34b02dc2008-08-24 21:49:55 -04001883 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001884 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001885 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001886 struct lpfc_iocbq *cmdiocbq = NULL;
1887 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001888 uint32_t status;
1889 uint32_t portRspPut, portRspMax;
1890 int rc = 1;
1891 lpfc_iocb_type type;
1892 unsigned long iflag;
1893 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05001894
James Smart2e0fef82007-06-17 19:56:36 -05001895 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001896 pring->stats.iocb_event++;
1897
dea31012005-04-17 16:05:31 -05001898 /*
1899 * The next available response entry should never exceed the maximum
1900 * entries. If it does, treat it as an adapter hardware error.
1901 */
1902 portRspMax = pring->numRiocb;
1903 portRspPut = le32_to_cpu(pgp->rspPutInx);
1904 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001905 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05001906 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001907 return 1;
1908 }
1909
1910 rmb();
1911 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001912 /*
1913 * Fetch an entry off the ring and copy it into a local data
1914 * structure. The copy involves a byte-swap since the
1915 * network byte order and pci byte orders are different.
1916 */
James Smarted957682007-06-17 19:56:37 -05001917 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05001918 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001919
1920 if (++pring->rspidx >= portRspMax)
1921 pring->rspidx = 0;
1922
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001923 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1924 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05001925 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05001926 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001927 irsp = &rspiocbq.iocb;
1928
dea31012005-04-17 16:05:31 -05001929 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1930 pring->stats.iocb_rsp++;
1931 rsp_cmpl++;
1932
1933 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001934 /*
1935 * If resource errors reported from HBA, reduce
1936 * queuedepths of the SCSI device.
1937 */
1938 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1939 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1940 spin_unlock_irqrestore(&phba->hbalock, iflag);
1941 lpfc_adjust_queue_depth(phba);
1942 spin_lock_irqsave(&phba->hbalock, iflag);
1943 }
1944
dea31012005-04-17 16:05:31 -05001945 /* Rsp ring <ringno> error: IOCB */
1946 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001947 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05001948 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001949 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05001950 irsp->un.ulpWord[0],
1951 irsp->un.ulpWord[1],
1952 irsp->un.ulpWord[2],
1953 irsp->un.ulpWord[3],
1954 irsp->un.ulpWord[4],
1955 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001956 *(uint32_t *)&irsp->un1,
1957 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05001958 }
1959
1960 switch (type) {
1961 case LPFC_ABORT_IOCB:
1962 case LPFC_SOL_IOCB:
1963 /*
1964 * Idle exchange closed via ABTS from port. No iocb
1965 * resources need to be recovered.
1966 */
1967 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001968 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001969 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04001970 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05001971 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04001972 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05001973 break;
1974 }
1975
James Bottomley604a3e32005-10-29 10:28:33 -05001976 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1977 &rspiocbq);
dea31012005-04-17 16:05:31 -05001978 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001979 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1980 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1981 &rspiocbq);
1982 } else {
James Smart2e0fef82007-06-17 19:56:36 -05001983 spin_unlock_irqrestore(&phba->hbalock,
1984 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001985 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1986 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001987 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001988 iflag);
1989 }
dea31012005-04-17 16:05:31 -05001990 }
1991 break;
James Smarta4bc3372006-12-02 13:34:16 -05001992 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05001993 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001994 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001995 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05001996 break;
dea31012005-04-17 16:05:31 -05001997 default:
1998 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1999 char adaptermsg[LPFC_MAX_ADPTMSG];
2000 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2001 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2002 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002003 dev_warn(&((phba->pcidev)->dev),
2004 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002005 phba->brd_no, adaptermsg);
2006 } else {
2007 /* Unknown IOCB command */
2008 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002009 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002010 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002011 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002012 irsp->ulpStatus,
2013 irsp->ulpIoTag,
2014 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002015 }
2016 break;
2017 }
2018
2019 /*
2020 * The response IOCB has been processed. Update the ring
2021 * pointer in SLIM. If the port response put pointer has not
2022 * been updated, sync the pgp->rspPutInx and fetch the new port
2023 * response put pointer.
2024 */
James Smarted957682007-06-17 19:56:37 -05002025 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002026
2027 if (pring->rspidx == portRspPut)
2028 portRspPut = le32_to_cpu(pgp->rspPutInx);
2029 }
2030
2031 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2032 pring->stats.iocb_rsp_full++;
2033 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2034 writel(status, phba->CAregaddr);
2035 readl(phba->CAregaddr);
2036 }
2037 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2038 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2039 pring->stats.iocb_cmd_empty++;
2040
2041 /* Force update of the local copy of cmdGetInx */
2042 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2043 lpfc_sli_resume_iocb(phba, pring);
2044
2045 if ((pring->lpfc_sli_cmd_available))
2046 (pring->lpfc_sli_cmd_available) (phba, pring);
2047
2048 }
2049
James Smart2e0fef82007-06-17 19:56:36 -05002050 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002051 return rc;
2052}
2053
James Smarte59058c2008-08-24 21:49:00 -04002054/**
2055 * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
2056 * @phba: Pointer to HBA context object.
2057 * @pring: Pointer to driver SLI ring object.
2058 * @mask: Host attention register mask for this ring.
2059 *
2060 * This function is called from the worker thread when there is a ring
2061 * event for non-fcp rings. The caller does not hold any lock .
2062 * The function processes each response iocb in the response ring until it
2063 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2064 * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
2065 * response iocb indicates a completion of a command iocb. The function
2066 * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
2067 * iocb. The function frees the resources or calls the completion handler if
2068 * this iocb is an abort completion. The function returns 0 when the allocated
2069 * iocbs are not freed, otherwise returns 1.
2070 **/
dea31012005-04-17 16:05:31 -05002071int
James Smart2e0fef82007-06-17 19:56:36 -05002072lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2073 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002074{
James Smart34b02dc2008-08-24 21:49:55 -04002075 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002076 IOCB_t *entry;
2077 IOCB_t *irsp = NULL;
2078 struct lpfc_iocbq *rspiocbp = NULL;
2079 struct lpfc_iocbq *next_iocb;
2080 struct lpfc_iocbq *cmdiocbp;
2081 struct lpfc_iocbq *saveq;
dea31012005-04-17 16:05:31 -05002082 uint8_t iocb_cmd_type;
2083 lpfc_iocb_type type;
2084 uint32_t status, free_saveq;
2085 uint32_t portRspPut, portRspMax;
2086 int rc = 1;
2087 unsigned long iflag;
dea31012005-04-17 16:05:31 -05002088
James Smart34b02dc2008-08-24 21:49:55 -04002089 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002090 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002091 pring->stats.iocb_event++;
2092
dea31012005-04-17 16:05:31 -05002093 /*
2094 * The next available response entry should never exceed the maximum
2095 * entries. If it does, treat it as an adapter hardware error.
2096 */
2097 portRspMax = pring->numRiocb;
2098 portRspPut = le32_to_cpu(pgp->rspPutInx);
2099 if (portRspPut >= portRspMax) {
2100 /*
2101 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
2102 * rsp ring <portRspMax>
2103 */
James Smarted957682007-06-17 19:56:37 -05002104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002105 "0303 Ring %d handler: portRspPut %d "
dea31012005-04-17 16:05:31 -05002106 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002107 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002108
James Smart2e0fef82007-06-17 19:56:36 -05002109 phba->link_state = LPFC_HBA_ERROR;
2110 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002111
2112 phba->work_hs = HS_FFER3;
2113 lpfc_handle_eratt(phba);
2114
2115 return 1;
2116 }
2117
2118 rmb();
dea31012005-04-17 16:05:31 -05002119 while (pring->rspidx != portRspPut) {
2120 /*
2121 * Build a completion list and call the appropriate handler.
2122 * The process is to get the next available response iocb, get
2123 * a free iocb from the list, copy the response data into the
2124 * free iocb, insert to the continuation list, and update the
2125 * next response index to slim. This process makes response
2126 * iocb's in the ring available to DMA as fast as possible but
2127 * pays a penalty for a copy operation. Since the iocb is
2128 * only 32 bytes, this penalty is considered small relative to
2129 * the PCI reads for register values and a slim write. When
2130 * the ulpLe field is set, the entire Command has been
2131 * received.
2132 */
James Smarted957682007-06-17 19:56:37 -05002133 entry = lpfc_resp_iocb(phba, pring);
2134
James Smart858c9f62007-06-17 19:56:39 -05002135 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002136 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002137 if (rspiocbp == NULL) {
2138 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002139 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002140 break;
2141 }
2142
James Smarted957682007-06-17 19:56:37 -05002143 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2144 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002145 irsp = &rspiocbp->iocb;
2146
2147 if (++pring->rspidx >= portRspMax)
2148 pring->rspidx = 0;
2149
James Smarta58cbd52007-08-02 11:09:43 -04002150 if (pring->ringno == LPFC_ELS_RING) {
2151 lpfc_debugfs_slow_ring_trc(phba,
2152 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2153 *(((uint32_t *) irsp) + 4),
2154 *(((uint32_t *) irsp) + 6),
2155 *(((uint32_t *) irsp) + 7));
2156 }
2157
James Smarted957682007-06-17 19:56:37 -05002158 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002159
James Smart9c2face2008-01-11 01:53:18 -05002160 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
dea31012005-04-17 16:05:31 -05002161
2162 pring->iocb_continueq_cnt++;
2163 if (irsp->ulpLe) {
2164 /*
2165 * By default, the driver expects to free all resources
2166 * associated with this iocb completion.
2167 */
2168 free_saveq = 1;
2169 saveq = list_get_first(&pring->iocb_continueq,
2170 struct lpfc_iocbq, list);
2171 irsp = &(saveq->iocb);
2172 list_del_init(&pring->iocb_continueq);
2173 pring->iocb_continueq_cnt = 0;
2174
2175 pring->stats.iocb_rsp++;
2176
James Smart92d7f7b2007-06-17 19:56:38 -05002177 /*
2178 * If resource errors reported from HBA, reduce
2179 * queuedepths of the SCSI device.
2180 */
2181 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2182 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2183 spin_unlock_irqrestore(&phba->hbalock, iflag);
2184 lpfc_adjust_queue_depth(phba);
2185 spin_lock_irqsave(&phba->hbalock, iflag);
2186 }
2187
dea31012005-04-17 16:05:31 -05002188 if (irsp->ulpStatus) {
2189 /* Rsp ring <ringno> error: IOCB */
James Smarted957682007-06-17 19:56:37 -05002190 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002191 "0328 Rsp Ring %d error: "
James Smarted957682007-06-17 19:56:37 -05002192 "IOCB Data: "
2193 "x%x x%x x%x x%x "
2194 "x%x x%x x%x x%x "
2195 "x%x x%x x%x x%x "
2196 "x%x x%x x%x x%x\n",
James Smarted957682007-06-17 19:56:37 -05002197 pring->ringno,
2198 irsp->un.ulpWord[0],
2199 irsp->un.ulpWord[1],
2200 irsp->un.ulpWord[2],
2201 irsp->un.ulpWord[3],
2202 irsp->un.ulpWord[4],
2203 irsp->un.ulpWord[5],
2204 *(((uint32_t *) irsp) + 6),
2205 *(((uint32_t *) irsp) + 7),
2206 *(((uint32_t *) irsp) + 8),
2207 *(((uint32_t *) irsp) + 9),
2208 *(((uint32_t *) irsp) + 10),
2209 *(((uint32_t *) irsp) + 11),
2210 *(((uint32_t *) irsp) + 12),
2211 *(((uint32_t *) irsp) + 13),
2212 *(((uint32_t *) irsp) + 14),
2213 *(((uint32_t *) irsp) + 15));
dea31012005-04-17 16:05:31 -05002214 }
2215
2216 /*
2217 * Fetch the IOCB command type and call the correct
2218 * completion routine. Solicited and Unsolicited
2219 * IOCBs on the ELS ring get freed back to the
2220 * lpfc_iocb_list by the discovery kernel thread.
2221 */
2222 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2223 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2224 if (type == LPFC_SOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002225 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002226 rc = lpfc_sli_process_sol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002227 saveq);
2228 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002229 } else if (type == LPFC_UNSOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002230 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002231 rc = lpfc_sli_process_unsol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002232 saveq);
2233 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9c2face2008-01-11 01:53:18 -05002234 if (!rc)
2235 free_saveq = 0;
dea31012005-04-17 16:05:31 -05002236 } else if (type == LPFC_ABORT_IOCB) {
2237 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
2238 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05002239 lpfc_sli_iocbq_lookup(phba, pring,
2240 saveq)))) {
dea31012005-04-17 16:05:31 -05002241 /* Call the specified completion
2242 routine */
2243 if (cmdiocbp->iocb_cmpl) {
2244 spin_unlock_irqrestore(
James Smart2e0fef82007-06-17 19:56:36 -05002245 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002246 iflag);
2247 (cmdiocbp->iocb_cmpl) (phba,
2248 cmdiocbp, saveq);
2249 spin_lock_irqsave(
James Smart2e0fef82007-06-17 19:56:36 -05002250 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002251 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002252 } else
James Smart2e0fef82007-06-17 19:56:36 -05002253 __lpfc_sli_release_iocbq(phba,
James Bottomley604a3e32005-10-29 10:28:33 -05002254 cmdiocbp);
dea31012005-04-17 16:05:31 -05002255 }
2256 } else if (type == LPFC_UNKNOWN_IOCB) {
2257 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2258
2259 char adaptermsg[LPFC_MAX_ADPTMSG];
2260
2261 memset(adaptermsg, 0,
2262 LPFC_MAX_ADPTMSG);
2263 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2264 MAX_MSG_DATA);
2265 dev_warn(&((phba->pcidev)->dev),
Joe Perches898eb712007-10-18 03:06:30 -07002266 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002267 phba->brd_no, adaptermsg);
2268 } else {
2269 /* Unknown IOCB command */
James Smart92d7f7b2007-06-17 19:56:38 -05002270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002271 "0335 Unknown IOCB "
James Smart92d7f7b2007-06-17 19:56:38 -05002272 "command Data: x%x "
2273 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002274 irsp->ulpCommand,
2275 irsp->ulpStatus,
2276 irsp->ulpIoTag,
2277 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002278 }
2279 }
2280
2281 if (free_saveq) {
James Smart2e0fef82007-06-17 19:56:36 -05002282 list_for_each_entry_safe(rspiocbp, next_iocb,
2283 &saveq->list, list) {
2284 list_del(&rspiocbp->list);
2285 __lpfc_sli_release_iocbq(phba,
2286 rspiocbp);
dea31012005-04-17 16:05:31 -05002287 }
James Smart2e0fef82007-06-17 19:56:36 -05002288 __lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05002289 }
James Smart92d7f7b2007-06-17 19:56:38 -05002290 rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002291 }
2292
2293 /*
2294 * If the port response put pointer has not been updated, sync
2295 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2296 * response put pointer.
2297 */
2298 if (pring->rspidx == portRspPut) {
2299 portRspPut = le32_to_cpu(pgp->rspPutInx);
2300 }
2301 } /* while (pring->rspidx != portRspPut) */
2302
James Smart92d7f7b2007-06-17 19:56:38 -05002303 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002304 /* At least one response entry has been freed */
2305 pring->stats.iocb_rsp_full++;
2306 /* SET RxRE_RSP in Chip Att register */
2307 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2308 writel(status, phba->CAregaddr);
2309 readl(phba->CAregaddr); /* flush */
2310 }
2311 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2312 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2313 pring->stats.iocb_cmd_empty++;
2314
2315 /* Force update of the local copy of cmdGetInx */
2316 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2317 lpfc_sli_resume_iocb(phba, pring);
2318
2319 if ((pring->lpfc_sli_cmd_available))
2320 (pring->lpfc_sli_cmd_available) (phba, pring);
2321
2322 }
2323
James Smart2e0fef82007-06-17 19:56:36 -05002324 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002325 return rc;
2326}
2327
James Smarte59058c2008-08-24 21:49:00 -04002328/**
2329 * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
2330 * @phba: Pointer to HBA context object.
2331 * @pring: Pointer to driver SLI ring object.
2332 *
2333 * This function aborts all iocbs in the given ring and frees all the iocb
2334 * objects in txq. This function issues an abort iocb for all the iocb commands
2335 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2336 * the return of this function. The caller is not required to hold any locks.
2337 **/
James Smart2e0fef82007-06-17 19:56:36 -05002338void
dea31012005-04-17 16:05:31 -05002339lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2340{
James Smart2534ba72007-04-25 09:52:20 -04002341 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002342 struct lpfc_iocbq *iocb, *next_iocb;
James Smart2534ba72007-04-25 09:52:20 -04002343 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05002344
James Smart92d7f7b2007-06-17 19:56:38 -05002345 if (pring->ringno == LPFC_ELS_RING) {
2346 lpfc_fabric_abort_hba(phba);
2347 }
2348
dea31012005-04-17 16:05:31 -05002349 /* Error everything on txq and txcmplq
2350 * First do the txq.
2351 */
James Smart2e0fef82007-06-17 19:56:36 -05002352 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002353 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002354 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002355
2356 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002357 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2358 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2359
James Smart2e0fef82007-06-17 19:56:36 -05002360 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002361
2362 while (!list_empty(&completions)) {
2363 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
dea31012005-04-17 16:05:31 -05002364 cmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05002365 list_del_init(&iocb->list);
dea31012005-04-17 16:05:31 -05002366
James Smart2e0fef82007-06-17 19:56:36 -05002367 if (!iocb->iocb_cmpl)
2368 lpfc_sli_release_iocbq(phba, iocb);
2369 else {
dea31012005-04-17 16:05:31 -05002370 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2371 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
dea31012005-04-17 16:05:31 -05002372 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002373 }
dea31012005-04-17 16:05:31 -05002374 }
dea31012005-04-17 16:05:31 -05002375}
2376
James Smarte59058c2008-08-24 21:49:00 -04002377/**
2378 * lpfc_sli_brdready: Check for host status bits.
2379 * @phba: Pointer to HBA context object.
2380 * @mask: Bit mask to be checked.
2381 *
2382 * This function reads the host status register and compares
2383 * with the provided bit mask to check if HBA completed
2384 * the restart. This function will wait in a loop for the
2385 * HBA to complete restart. If the HBA does not restart within
2386 * 15 iterations, the function will reset the HBA again. The
2387 * function returns 1 when HBA fail to restart otherwise returns
2388 * zero.
2389 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002390int
James Smart2e0fef82007-06-17 19:56:36 -05002391lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05002392{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002393 uint32_t status;
2394 int i = 0;
2395 int retval = 0;
dea31012005-04-17 16:05:31 -05002396
Jamie Wellnitz41415862006-02-28 19:25:27 -05002397 /* Read the HBA Host Status Register */
2398 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002399
Jamie Wellnitz41415862006-02-28 19:25:27 -05002400 /*
2401 * Check status register every 100ms for 5 retries, then every
2402 * 500ms for 5, then every 2.5 sec for 5, then reset board and
2403 * every 2.5 sec for 4.
2404 * Break our of the loop if errors occurred during init.
2405 */
2406 while (((status & mask) != mask) &&
2407 !(status & HS_FFERM) &&
2408 i++ < 20) {
dea31012005-04-17 16:05:31 -05002409
Jamie Wellnitz41415862006-02-28 19:25:27 -05002410 if (i <= 5)
2411 msleep(10);
2412 else if (i <= 10)
2413 msleep(500);
2414 else
2415 msleep(2500);
dea31012005-04-17 16:05:31 -05002416
Jamie Wellnitz41415862006-02-28 19:25:27 -05002417 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002418 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002419 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002420 lpfc_sli_brdrestart(phba);
2421 }
2422 /* Read the HBA Host Status Register */
2423 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002424 }
dea31012005-04-17 16:05:31 -05002425
Jamie Wellnitz41415862006-02-28 19:25:27 -05002426 /* Check to see if any errors occurred during init */
2427 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05002428 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002429 retval = 1;
2430 }
dea31012005-04-17 16:05:31 -05002431
Jamie Wellnitz41415862006-02-28 19:25:27 -05002432 return retval;
dea31012005-04-17 16:05:31 -05002433}
2434
James Smart92908312006-03-07 15:04:13 -05002435#define BARRIER_TEST_PATTERN (0xdeadbeef)
2436
James Smarte59058c2008-08-24 21:49:00 -04002437/**
2438 * lpfc_reset_barrier: Make HBA ready for HBA reset.
2439 * @phba: Pointer to HBA context object.
2440 *
2441 * This function is called before resetting an HBA. This
2442 * function requests HBA to quiesce DMAs before a reset.
2443 **/
James Smart2e0fef82007-06-17 19:56:36 -05002444void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05002445{
James Smart65a29c12006-07-06 15:50:50 -04002446 uint32_t __iomem *resp_buf;
2447 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05002448 volatile uint32_t mbox;
2449 uint32_t hc_copy;
2450 int i;
2451 uint8_t hdrtype;
2452
2453 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2454 if (hdrtype != 0x80 ||
2455 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2456 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2457 return;
2458
2459 /*
2460 * Tell the other part of the chip to suspend temporarily all
2461 * its DMA activity.
2462 */
James Smart65a29c12006-07-06 15:50:50 -04002463 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002464
2465 /* Disable the error attention */
2466 hc_copy = readl(phba->HCregaddr);
2467 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2468 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002469 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002470
2471 if (readl(phba->HAregaddr) & HA_ERATT) {
2472 /* Clear Chip error bit */
2473 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002474 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002475 }
2476
2477 mbox = 0;
2478 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2479 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2480
2481 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04002482 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002483 writel(mbox, mbox_buf);
2484
2485 for (i = 0;
2486 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2487 mdelay(1);
2488
2489 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2490 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05002491 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05002492 goto restore_hc;
2493 else
2494 goto clear_errat;
2495 }
2496
2497 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2498 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
2499 mdelay(1);
2500
2501clear_errat:
2502
2503 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2504 mdelay(1);
2505
2506 if (readl(phba->HAregaddr) & HA_ERATT) {
2507 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002508 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002509 }
2510
2511restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05002512 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002513 writel(hc_copy, phba->HCregaddr);
2514 readl(phba->HCregaddr); /* flush */
2515}
2516
James Smarte59058c2008-08-24 21:49:00 -04002517/**
2518 * lpfc_sli_brdkill: Issue a kill_board mailbox command.
2519 * @phba: Pointer to HBA context object.
2520 *
2521 * This function issues a kill_board mailbox command and waits for
2522 * the error attention interrupt. This function is called for stopping
2523 * the firmware processing. The caller is not required to hold any
2524 * locks. This function calls lpfc_hba_down_post function to free
2525 * any pending commands after the kill. The function will return 1 when it
2526 * fails to kill the board else will return 0.
2527 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002528int
James Smart2e0fef82007-06-17 19:56:36 -05002529lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002530{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002531 struct lpfc_sli *psli;
2532 LPFC_MBOXQ_t *pmb;
2533 uint32_t status;
2534 uint32_t ha_copy;
2535 int retval;
2536 int i = 0;
2537
2538 psli = &phba->sli;
2539
2540 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05002541 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002542 "0329 Kill HBA Data: x%x x%x\n",
2543 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002544
James Smart98c9ea52007-10-27 13:37:33 -04002545 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2546 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002547 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002548
2549 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05002550 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002551 status = readl(phba->HCregaddr);
2552 status &= ~HC_ERINT_ENA;
2553 writel(status, phba->HCregaddr);
2554 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002555 phba->link_flag |= LS_IGNORE_ERATT;
2556 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002557
2558 lpfc_kill_board(phba, pmb);
2559 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2560 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2561
2562 if (retval != MBX_SUCCESS) {
2563 if (retval != MBX_BUSY)
2564 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002565 spin_lock_irq(&phba->hbalock);
2566 phba->link_flag &= ~LS_IGNORE_ERATT;
2567 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002568 return 1;
2569 }
2570
James Smart92908312006-03-07 15:04:13 -05002571 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2572
Jamie Wellnitz41415862006-02-28 19:25:27 -05002573 mempool_free(pmb, phba->mbox_mem_pool);
2574
2575 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2576 * attention every 100ms for 3 seconds. If we don't get ERATT after
2577 * 3 seconds we still set HBA_ERROR state because the status of the
2578 * board is now undefined.
2579 */
2580 ha_copy = readl(phba->HAregaddr);
2581
2582 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2583 mdelay(100);
2584 ha_copy = readl(phba->HAregaddr);
2585 }
2586
2587 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05002588 if (ha_copy & HA_ERATT) {
2589 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002590 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002591 }
James Smart2e0fef82007-06-17 19:56:36 -05002592 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002593 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002594 phba->link_flag &= ~LS_IGNORE_ERATT;
2595 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002596
2597 psli->mbox_active = NULL;
2598 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002599 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002600
James Smart2e0fef82007-06-17 19:56:36 -05002601 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002602}
2603
James Smarte59058c2008-08-24 21:49:00 -04002604/**
2605 * lpfc_sli_brdreset: Reset the HBA.
2606 * @phba: Pointer to HBA context object.
2607 *
2608 * This function resets the HBA by writing HC_INITFF to the control
2609 * register. After the HBA resets, this function resets all the iocb ring
2610 * indices. This function disables PCI layer parity checking during
2611 * the reset.
2612 * This function returns 0 always.
2613 * The caller is not required to hold any locks.
2614 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002615int
James Smart2e0fef82007-06-17 19:56:36 -05002616lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002617{
2618 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002619 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002620 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05002621 int i;
dea31012005-04-17 16:05:31 -05002622
Jamie Wellnitz41415862006-02-28 19:25:27 -05002623 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002624
Jamie Wellnitz41415862006-02-28 19:25:27 -05002625 /* Reset HBA */
2626 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002627 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002628 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05002629
2630 /* perform board reset */
2631 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002632 phba->pport->fc_myDID = 0;
2633 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002634
Jamie Wellnitz41415862006-02-28 19:25:27 -05002635 /* Turn off parity checking and serr during the physical reset */
2636 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2637 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2638 (cfg_value &
2639 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2640
James Smart1c067a42006-08-01 07:33:52 -04002641 psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002642 /* Now toggle INITFF bit in the Host Control Register */
2643 writel(HC_INITFF, phba->HCregaddr);
2644 mdelay(1);
2645 readl(phba->HCregaddr); /* flush */
2646 writel(0, phba->HCregaddr);
2647 readl(phba->HCregaddr); /* flush */
2648
2649 /* Restore PCI cmd register */
2650 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002651
2652 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002653 for (i = 0; i < psli->num_rings; i++) {
2654 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002655 pring->flag = 0;
2656 pring->rspidx = 0;
2657 pring->next_cmdidx = 0;
2658 pring->local_getidx = 0;
2659 pring->cmdidx = 0;
2660 pring->missbufcnt = 0;
2661 }
dea31012005-04-17 16:05:31 -05002662
James Smart2e0fef82007-06-17 19:56:36 -05002663 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002664 return 0;
2665}
2666
James Smarte59058c2008-08-24 21:49:00 -04002667/**
2668 * lpfc_sli_brdrestart: Restart the HBA.
2669 * @phba: Pointer to HBA context object.
2670 *
2671 * This function is called in the SLI initialization code path to
2672 * restart the HBA. The caller is not required to hold any lock.
2673 * This function writes MBX_RESTART mailbox command to the SLIM and
2674 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
2675 * function to free any pending commands. The function enables
2676 * POST only during the first initialization. The function returns zero.
2677 * The function does not guarantee completion of MBX_RESTART mailbox
2678 * command before the return of this function.
2679 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002680int
James Smart2e0fef82007-06-17 19:56:36 -05002681lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002682{
2683 MAILBOX_t *mb;
2684 struct lpfc_sli *psli;
2685 uint16_t skip_post;
2686 volatile uint32_t word0;
2687 void __iomem *to_slim;
2688
James Smart2e0fef82007-06-17 19:56:36 -05002689 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002690
2691 psli = &phba->sli;
2692
2693 /* Restart HBA */
2694 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002695 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002696 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002697
2698 word0 = 0;
2699 mb = (MAILBOX_t *) &word0;
2700 mb->mbxCommand = MBX_RESTART;
2701 mb->mbxHc = 1;
2702
James Smart92908312006-03-07 15:04:13 -05002703 lpfc_reset_barrier(phba);
2704
Jamie Wellnitz41415862006-02-28 19:25:27 -05002705 to_slim = phba->MBslimaddr;
2706 writel(*(uint32_t *) mb, to_slim);
2707 readl(to_slim); /* flush */
2708
2709 /* Only skip post after fc_ffinit is completed */
James Smart2e0fef82007-06-17 19:56:36 -05002710 if (phba->pport->port_state) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002711 skip_post = 1;
2712 word0 = 1; /* This is really setting up word1 */
dea31012005-04-17 16:05:31 -05002713 } else {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002714 skip_post = 0;
2715 word0 = 0; /* This is really setting up word1 */
2716 }
James Smart65a29c12006-07-06 15:50:50 -04002717 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002718 writel(*(uint32_t *) mb, to_slim);
2719 readl(to_slim); /* flush */
2720
2721 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002722 phba->pport->stopped = 0;
2723 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002724
James Smart2e0fef82007-06-17 19:56:36 -05002725 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002726
James Smart64ba8812006-08-02 15:24:34 -04002727 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2728 psli->stats_start = get_seconds();
2729
Jamie Wellnitz41415862006-02-28 19:25:27 -05002730 if (skip_post)
2731 mdelay(100);
2732 else
dea31012005-04-17 16:05:31 -05002733 mdelay(2000);
dea31012005-04-17 16:05:31 -05002734
Jamie Wellnitz41415862006-02-28 19:25:27 -05002735 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002736
2737 return 0;
2738}
2739
James Smarte59058c2008-08-24 21:49:00 -04002740/**
2741 * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
2742 * @phba: Pointer to HBA context object.
2743 *
2744 * This function is called after a HBA restart to wait for successful
2745 * restart of the HBA. Successful restart of the HBA is indicated by
2746 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
2747 * iteration, the function will restart the HBA again. The function returns
2748 * zero if HBA successfully restarted else returns negative error code.
2749 **/
dea31012005-04-17 16:05:31 -05002750static int
2751lpfc_sli_chipset_init(struct lpfc_hba *phba)
2752{
2753 uint32_t status, i = 0;
2754
2755 /* Read the HBA Host Status Register */
2756 status = readl(phba->HSregaddr);
2757
2758 /* Check status register to see what current state is */
2759 i = 0;
2760 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2761
2762 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2763 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2764 * 4.
2765 */
2766 if (i++ >= 20) {
2767 /* Adapter failed to init, timeout, status reg
2768 <status> */
James Smarted957682007-06-17 19:56:37 -05002769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002770 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002771 "timeout, status reg x%x, "
2772 "FW Data: A8 x%x AC x%x\n", status,
2773 readl(phba->MBslimaddr + 0xa8),
2774 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002775 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002776 return -ETIMEDOUT;
2777 }
2778
2779 /* Check to see if any errors occurred during init */
2780 if (status & HS_FFERM) {
2781 /* ERROR: During chipset initialization */
2782 /* Adapter failed to init, chipset, status reg
2783 <status> */
James Smarted957682007-06-17 19:56:37 -05002784 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002785 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002786 "chipset, status reg x%x, "
2787 "FW Data: A8 x%x AC x%x\n", status,
2788 readl(phba->MBslimaddr + 0xa8),
2789 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002790 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002791 return -EIO;
2792 }
2793
2794 if (i <= 5) {
2795 msleep(10);
2796 } else if (i <= 10) {
2797 msleep(500);
2798 } else {
2799 msleep(2500);
2800 }
2801
2802 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002803 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002804 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002805 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002806 }
2807 /* Read the HBA Host Status Register */
2808 status = readl(phba->HSregaddr);
2809 }
2810
2811 /* Check to see if any errors occurred during init */
2812 if (status & HS_FFERM) {
2813 /* ERROR: During chipset initialization */
2814 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05002815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002816 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05002817 "status reg x%x, "
2818 "FW Data: A8 x%x AC x%x\n", status,
2819 readl(phba->MBslimaddr + 0xa8),
2820 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002821 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002822 return -EIO;
2823 }
2824
2825 /* Clear all interrupt enable conditions */
2826 writel(0, phba->HCregaddr);
2827 readl(phba->HCregaddr); /* flush */
2828
2829 /* setup host attn register */
2830 writel(0xffffffff, phba->HAregaddr);
2831 readl(phba->HAregaddr); /* flush */
2832 return 0;
2833}
2834
James Smarte59058c2008-08-24 21:49:00 -04002835/**
2836 * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
2837 *
2838 * This function calculates and returns the number of HBQs required to be
2839 * configured.
2840 **/
James Smart78b2d852007-08-02 11:10:21 -04002841int
James Smarted957682007-06-17 19:56:37 -05002842lpfc_sli_hbq_count(void)
2843{
James Smart92d7f7b2007-06-17 19:56:38 -05002844 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05002845}
2846
James Smarte59058c2008-08-24 21:49:00 -04002847/**
2848 * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
2849 *
2850 * This function adds the number of hbq entries in every HBQ to get
2851 * the total number of hbq entries required for the HBA and returns
2852 * the total count.
2853 **/
James Smarted957682007-06-17 19:56:37 -05002854static int
2855lpfc_sli_hbq_entry_count(void)
2856{
2857 int hbq_count = lpfc_sli_hbq_count();
2858 int count = 0;
2859 int i;
2860
2861 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05002862 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05002863 return count;
2864}
2865
James Smarte59058c2008-08-24 21:49:00 -04002866/**
2867 * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
2868 *
2869 * This function calculates amount of memory required for all hbq entries
2870 * to be configured and returns the total memory required.
2871 **/
dea31012005-04-17 16:05:31 -05002872int
James Smarted957682007-06-17 19:56:37 -05002873lpfc_sli_hbq_size(void)
2874{
2875 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2876}
2877
James Smarte59058c2008-08-24 21:49:00 -04002878/**
2879 * lpfc_sli_hbq_setup: configure and initialize HBQs.
2880 * @phba: Pointer to HBA context object.
2881 *
2882 * This function is called during the SLI initialization to configure
2883 * all the HBQs and post buffers to the HBQ. The caller is not
2884 * required to hold any locks. This function will return zero if successful
2885 * else it will return negative error code.
2886 **/
James Smarted957682007-06-17 19:56:37 -05002887static int
2888lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2889{
2890 int hbq_count = lpfc_sli_hbq_count();
2891 LPFC_MBOXQ_t *pmb;
2892 MAILBOX_t *pmbox;
2893 uint32_t hbqno;
2894 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05002895
James Smart92d7f7b2007-06-17 19:56:38 -05002896 /* Get a Mailbox buffer to setup mailbox
2897 * commands for HBA initialization
2898 */
James Smarted957682007-06-17 19:56:37 -05002899 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2900
2901 if (!pmb)
2902 return -ENOMEM;
2903
2904 pmbox = &pmb->mb;
2905
2906 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2907 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05002908 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05002909
2910 hbq_entry_index = 0;
2911 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2912 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2913 phba->hbqs[hbqno].hbqPutIdx = 0;
2914 phba->hbqs[hbqno].local_hbqGetIdx = 0;
2915 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05002916 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04002917 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2918 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05002919 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2920
2921 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2922 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2923 mbxStatus <status>, ring <num> */
2924
2925 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05002926 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002927 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05002928 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002929 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05002930 pmbox->mbxStatus, hbqno);
2931
2932 phba->link_state = LPFC_HBA_ERROR;
2933 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05002934 return ENXIO;
2935 }
2936 }
2937 phba->hbq_count = hbq_count;
2938
James Smarted957682007-06-17 19:56:37 -05002939 mempool_free(pmb, phba->mbox_mem_pool);
2940
James Smart92d7f7b2007-06-17 19:56:38 -05002941 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04002942 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
2943 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05002944 return 0;
2945}
2946
James Smarte59058c2008-08-24 21:49:00 -04002947/**
2948 * lpfc_do_config_port: Issue config port mailbox command.
2949 * @phba: Pointer to HBA context object.
2950 * @sli_mode: sli mode - 2/3
2951 *
2952 * This function is called by the sli intialization code path
2953 * to issue config_port mailbox command. This function restarts the
2954 * HBA firmware and issues a config_port mailbox command to configure
2955 * the SLI interface in the sli mode specified by sli_mode
2956 * variable. The caller is not required to hold any locks.
2957 * The function returns 0 if successful, else returns negative error
2958 * code.
2959 **/
James Smarted957682007-06-17 19:56:37 -05002960static int
2961lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05002962{
2963 LPFC_MBOXQ_t *pmb;
2964 uint32_t resetcount = 0, rc = 0, done = 0;
2965
2966 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2967 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05002968 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002969 return -ENOMEM;
2970 }
2971
James Smarted957682007-06-17 19:56:37 -05002972 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05002973 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05002974 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002975 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002976 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002977 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002978 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002979 msleep(2500);
2980 rc = lpfc_sli_chipset_init(phba);
2981 if (rc)
2982 break;
2983
James Smart2e0fef82007-06-17 19:56:36 -05002984 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04002985 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002986 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002987 resetcount++;
2988
James Smarted957682007-06-17 19:56:37 -05002989 /* Call pre CONFIG_PORT mailbox command initialization. A
2990 * value of 0 means the call was successful. Any other
2991 * nonzero value is a failure, but if ERESTART is returned,
2992 * the driver may reset the HBA and try again.
2993 */
dea31012005-04-17 16:05:31 -05002994 rc = lpfc_config_port_prep(phba);
2995 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05002996 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05002997 continue;
James Smart34b02dc2008-08-24 21:49:55 -04002998 } else if (rc)
dea31012005-04-17 16:05:31 -05002999 break;
James Smart2e0fef82007-06-17 19:56:36 -05003000 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003001 lpfc_config_port(phba, pmb);
3002 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003003 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3004 LPFC_SLI3_HBQ_ENABLED |
3005 LPFC_SLI3_CRP_ENABLED |
3006 LPFC_SLI3_INB_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003007 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003009 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003010 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003011 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003012 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003013 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003014 spin_unlock_irq(&phba->hbalock);
3015 rc = -ENXIO;
James Smart34b02dc2008-08-24 21:49:55 -04003016 } else
James Smarted957682007-06-17 19:56:37 -05003017 done = 1;
dea31012005-04-17 16:05:31 -05003018 }
James Smarted957682007-06-17 19:56:37 -05003019 if (!done) {
3020 rc = -EINVAL;
3021 goto do_prep_failed;
3022 }
James Smart34b02dc2008-08-24 21:49:55 -04003023 if (pmb->mb.un.varCfgPort.sli_mode == 3) {
3024 if (!pmb->mb.un.varCfgPort.cMA) {
3025 rc = -ENXIO;
3026 goto do_prep_failed;
3027 }
3028 if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
3029 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3030 phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
3031 } else
3032 phba->max_vpi = 0;
3033 if (pmb->mb.un.varCfgPort.gerbm)
3034 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3035 if (pmb->mb.un.varCfgPort.gcrp)
3036 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3037 if (pmb->mb.un.varCfgPort.ginb) {
3038 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
3039 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3040 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3041 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3042 phba->inb_last_counter =
3043 phba->mbox->us.s3_inb_pgp.counter;
3044 } else {
3045 phba->port_gp = phba->mbox->us.s3_pgp.port;
3046 phba->inb_ha_copy = NULL;
3047 phba->inb_counter = NULL;
3048 }
3049 } else {
3050 phba->port_gp = phba->mbox->us.s2.port;
3051 phba->inb_ha_copy = NULL;
3052 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003053 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003054 }
James Smart92d7f7b2007-06-17 19:56:38 -05003055do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003056 mempool_free(pmb, phba->mbox_mem_pool);
3057 return rc;
3058}
3059
James Smarte59058c2008-08-24 21:49:00 -04003060
3061/**
3062 * lpfc_sli_hba_setup: SLI intialization function.
3063 * @phba: Pointer to HBA context object.
3064 *
3065 * This function is the main SLI intialization function. This function
3066 * is called by the HBA intialization code, HBA reset code and HBA
3067 * error attention handler code. Caller is not required to hold any
3068 * locks. This function issues config_port mailbox command to configure
3069 * the SLI, setup iocb rings and HBQ rings. In the end the function
3070 * calls the config_port_post function to issue init_link mailbox
3071 * command and to start the discovery. The function will return zero
3072 * if successful, else it will return negative error code.
3073 **/
James Smarted957682007-06-17 19:56:37 -05003074int
3075lpfc_sli_hba_setup(struct lpfc_hba *phba)
3076{
3077 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003078 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003079
3080 switch (lpfc_sli_mode) {
3081 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003082 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003084 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003085 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003086 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003087 break;
3088 }
James Smarted957682007-06-17 19:56:37 -05003089 mode = 2;
3090 break;
3091 case 0:
3092 case 3:
3093 break;
3094 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003096 "1819 Unrecognized lpfc_sli_mode "
3097 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003098
3099 break;
3100 }
3101
3102 rc = lpfc_do_config_port(phba, mode);
3103 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003105 "1820 Unable to select SLI-3. "
3106 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003107 if (rc && mode != 2)
3108 rc = lpfc_do_config_port(phba, 2);
3109 if (rc)
dea31012005-04-17 16:05:31 -05003110 goto lpfc_sli_hba_setup_error;
3111
James Smarted957682007-06-17 19:56:37 -05003112 if (phba->sli_rev == 3) {
3113 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3114 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05003115 } else {
3116 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3117 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003118 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003119 }
3120
3121 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003122 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3123 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05003124 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05003125
3126 if (rc)
3127 goto lpfc_sli_hba_setup_error;
3128
James Smart92d7f7b2007-06-17 19:56:38 -05003129 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05003130
3131 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3132 rc = lpfc_sli_hbq_setup(phba);
3133 if (rc)
3134 goto lpfc_sli_hba_setup_error;
3135 }
3136
dea31012005-04-17 16:05:31 -05003137 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3138
3139 rc = lpfc_config_port_post(phba);
3140 if (rc)
3141 goto lpfc_sli_hba_setup_error;
3142
James Smarted957682007-06-17 19:56:37 -05003143 return rc;
3144
James Smart92d7f7b2007-06-17 19:56:38 -05003145lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05003146 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05003147 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003148 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05003149 return rc;
3150}
3151
James Smarte59058c2008-08-24 21:49:00 -04003152
3153/**
3154 * lpfc_mbox_timeout: Timeout call back function for mbox timer.
3155 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05003156 *
James Smarte59058c2008-08-24 21:49:00 -04003157 * This is the callback function for mailbox timer. The mailbox
3158 * timer is armed when a new mailbox command is issued and the timer
3159 * is deleted when the mailbox complete. The function is called by
3160 * the kernel timer code when a mailbox does not complete within
3161 * expected time. This function wakes up the worker thread to
3162 * process the mailbox timeout and returns. All the processing is
3163 * done by the worker thread function lpfc_mbox_timeout_handler.
3164 **/
dea31012005-04-17 16:05:31 -05003165void
3166lpfc_mbox_timeout(unsigned long ptr)
3167{
James Smart92d7f7b2007-06-17 19:56:38 -05003168 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05003169 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05003170 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003171
James Smart2e0fef82007-06-17 19:56:36 -05003172 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05003173 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003174 if (!tmo_posted)
3175 phba->pport->work_port_events |= WORKER_MBOX_TMO;
3176 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
3177
James Smart5e9d9b82008-06-14 22:52:53 -04003178 if (!tmo_posted)
3179 lpfc_worker_wake_up(phba);
3180 return;
dea31012005-04-17 16:05:31 -05003181}
3182
James Smarte59058c2008-08-24 21:49:00 -04003183
3184/**
3185 * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
3186 * @phba: Pointer to HBA context object.
3187 *
3188 * This function is called from worker thread when a mailbox command times out.
3189 * The caller is not required to hold any locks. This function will reset the
3190 * HBA and recover all the pending commands.
3191 **/
dea31012005-04-17 16:05:31 -05003192void
3193lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
3194{
James Smart2e0fef82007-06-17 19:56:36 -05003195 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
3196 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04003197 struct lpfc_sli *psli = &phba->sli;
3198 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003199
James Smart2e0fef82007-06-17 19:56:36 -05003200 if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
dea31012005-04-17 16:05:31 -05003201 return;
3202 }
3203
dea31012005-04-17 16:05:31 -05003204 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05003205 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003206 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003207 mb->mbxCommand,
3208 phba->pport->port_state,
3209 phba->sli.sli_flag,
3210 phba->sli.mbox_active);
dea31012005-04-17 16:05:31 -05003211
James Smart1dcb58e2007-04-25 09:51:30 -04003212 /* Setting state unknown so lpfc_sli_abort_iocb_ring
3213 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
3214 * it to fail all oustanding SCSI IO.
3215 */
James Smart2e0fef82007-06-17 19:56:36 -05003216 spin_lock_irq(&phba->pport->work_port_lock);
3217 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3218 spin_unlock_irq(&phba->pport->work_port_lock);
3219 spin_lock_irq(&phba->hbalock);
3220 phba->link_state = LPFC_LINK_UNKNOWN;
James Smart1dcb58e2007-04-25 09:51:30 -04003221 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003222 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04003223
3224 pring = &psli->ring[psli->fcp_ring];
3225 lpfc_sli_abort_iocb_ring(phba, pring);
3226
3227 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003228 "0345 Resetting board due to mailbox timeout\n");
James Smart1dcb58e2007-04-25 09:51:30 -04003229 /*
3230 * lpfc_offline calls lpfc_sli_hba_down which will clean up
3231 * on oustanding mailbox commands.
3232 */
James Smart13815c82008-01-11 01:52:48 -05003233 /* If resets are disabled then set error state and return. */
3234 if (!phba->cfg_enable_hba_reset) {
3235 phba->link_state = LPFC_HBA_ERROR;
3236 return;
3237 }
James Smart1dcb58e2007-04-25 09:51:30 -04003238 lpfc_offline_prep(phba);
3239 lpfc_offline(phba);
3240 lpfc_sli_brdrestart(phba);
James Smart58da1ff2008-04-07 10:15:56 -04003241 lpfc_online(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04003242 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003243 return;
3244}
3245
James Smarte59058c2008-08-24 21:49:00 -04003246/**
3247 * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
3248 * @phba: Pointer to HBA context object.
3249 * @pmbox: Pointer to mailbox object.
3250 * @flag: Flag indicating how the mailbox need to be processed.
3251 *
3252 * This function is called by discovery code and HBA management code
3253 * to submit a mailbox command to firmware. This function gets the
3254 * hbalock to protect the data structures.
3255 * The mailbox command can be submitted in polling mode, in which case
3256 * this function will wait in a polling loop for the completion of the
3257 * mailbox.
3258 * If the mailbox is submitted in no_wait mode (not polling) the
3259 * function will submit the command and returns immediately without waiting
3260 * for the mailbox completion. The no_wait is supported only when HBA
3261 * is in SLI2/SLI3 mode - interrupts are enabled.
3262 * The SLI interface allows only one mailbox pending at a time. If the
3263 * mailbox is issued in polling mode and there is already a mailbox
3264 * pending, then the function will return an error. If the mailbox is issued
3265 * in NO_WAIT mode and there is a mailbox pending already, the function
3266 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
3267 * The sli layer owns the mailbox object until the completion of mailbox
3268 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
3269 * return codes the caller owns the mailbox command after the return of
3270 * the function.
3271 **/
dea31012005-04-17 16:05:31 -05003272int
James Smart2e0fef82007-06-17 19:56:36 -05003273lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
dea31012005-04-17 16:05:31 -05003274{
dea31012005-04-17 16:05:31 -05003275 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05003276 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003277 uint32_t status, evtctr;
3278 uint32_t ha_copy;
3279 int i;
James Smart09372822008-01-11 01:52:54 -05003280 unsigned long timeout;
dea31012005-04-17 16:05:31 -05003281 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04003282 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05003283 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04003284 int processing_queue = 0;
3285
3286 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3287 if (!pmbox) {
3288 /* processing mbox queue from intr_handler */
3289 processing_queue = 1;
3290 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3291 pmbox = lpfc_mbox_get(phba);
3292 if (!pmbox) {
3293 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3294 return MBX_SUCCESS;
3295 }
3296 }
dea31012005-04-17 16:05:31 -05003297
James Smarted957682007-06-17 19:56:37 -05003298 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05003299 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05003300 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04003301 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05003302 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003303 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003304 "1806 Mbox x%x failed. No vport\n",
James Smarted957682007-06-17 19:56:37 -05003305 pmbox->mb.mbxCommand);
3306 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04003307 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05003308 }
3309 }
3310
Linas Vepstas8d63f372007-02-14 14:28:36 -06003311 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04003312 if (unlikely(pci_channel_offline(phba->pcidev))) {
3313 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3314 goto out_not_finished;
3315 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06003316
dea31012005-04-17 16:05:31 -05003317 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05003318
dea31012005-04-17 16:05:31 -05003319 mb = &pmbox->mb;
3320 status = MBX_SUCCESS;
3321
James Smart2e0fef82007-06-17 19:56:36 -05003322 if (phba->link_state == LPFC_HBA_ERROR) {
3323 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003324
3325 /* Mbox command <mbxCommand> cannot issue */
James Smart7f5f3d02008-02-08 18:50:14 -05003326 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003327 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003328 }
3329
James Smart92908312006-03-07 15:04:13 -05003330 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
3331 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05003332 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart7f5f3d02008-02-08 18:50:14 -05003333 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003334 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05003335 }
3336
dea31012005-04-17 16:05:31 -05003337 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
3338 /* Polling for a mbox command when another one is already active
3339 * is not allowed in SLI. Also, the driver must have established
3340 * SLI2 mode to queue and process multiple mbox commands.
3341 */
3342
3343 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05003344 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003345
3346 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003347 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003348 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003349 }
3350
3351 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003352 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003353 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003354 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003355 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003356 }
3357
dea31012005-04-17 16:05:31 -05003358 /* Another mailbox command is still being processed, queue this
3359 * command to be processed later.
3360 */
3361 lpfc_mbox_put(phba, pmbox);
3362
3363 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05003364 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003365 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003366 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003367 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
3368 mb->mbxCommand, phba->pport->port_state,
3369 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003370
3371 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05003372 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003373
James Smart858c9f62007-06-17 19:56:39 -05003374 if (pmbox->vport) {
3375 lpfc_debugfs_disc_trc(pmbox->vport,
3376 LPFC_DISC_TRC_MBOX_VPORT,
3377 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
3378 (uint32_t)mb->mbxCommand,
3379 mb->un.varWords[0], mb->un.varWords[1]);
3380 }
3381 else {
3382 lpfc_debugfs_disc_trc(phba->pport,
3383 LPFC_DISC_TRC_MBOX,
3384 "MBOX Bsy: cmd:x%x mb:x%x x%x",
3385 (uint32_t)mb->mbxCommand,
3386 mb->un.varWords[0], mb->un.varWords[1]);
3387 }
3388
James Smart2e0fef82007-06-17 19:56:36 -05003389 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05003390 }
3391
dea31012005-04-17 16:05:31 -05003392 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3393
3394 /* If we are not polling, we MUST be in SLI2 mode */
3395 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05003396 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
3397 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05003398 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003399 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003400 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003401 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003402 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003403 }
3404 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04003405 mod_timer(&psli->mbox_tmo, (jiffies +
3406 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05003407 }
3408
3409 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05003410 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003411 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003412 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003413 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05003414 mb->mbxCommand, phba->pport->port_state,
3415 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003416
James Smart858c9f62007-06-17 19:56:39 -05003417 if (mb->mbxCommand != MBX_HEARTBEAT) {
3418 if (pmbox->vport) {
3419 lpfc_debugfs_disc_trc(pmbox->vport,
3420 LPFC_DISC_TRC_MBOX_VPORT,
3421 "MBOX Send vport: cmd:x%x mb:x%x x%x",
3422 (uint32_t)mb->mbxCommand,
3423 mb->un.varWords[0], mb->un.varWords[1]);
3424 }
3425 else {
3426 lpfc_debugfs_disc_trc(phba->pport,
3427 LPFC_DISC_TRC_MBOX,
3428 "MBOX Send: cmd:x%x mb:x%x x%x",
3429 (uint32_t)mb->mbxCommand,
3430 mb->un.varWords[0], mb->un.varWords[1]);
3431 }
3432 }
3433
dea31012005-04-17 16:05:31 -05003434 psli->slistat.mbox_cmd++;
3435 evtctr = psli->slistat.mbox_event;
3436
3437 /* next set own bit for the adapter and copy over command word */
3438 mb->mbxOwner = OWN_CHIP;
3439
3440 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003441 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04003442 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003443 } else {
James Smart92908312006-03-07 15:04:13 -05003444 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05003445 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04003446 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003447 }
3448
3449 /* First copy mbox command data to HBA SLIM, skip past first
3450 word */
3451 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3452 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
3453 MAILBOX_CMD_SIZE - sizeof (uint32_t));
3454
3455 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04003456 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05003457 to_slim = phba->MBslimaddr;
3458 writel(ldata, to_slim);
3459 readl(to_slim); /* flush */
3460
3461 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3462 /* switch over to host mailbox */
3463 psli->sli_flag |= LPFC_SLI2_ACTIVE;
3464 }
3465 }
3466
3467 wmb();
dea31012005-04-17 16:05:31 -05003468
3469 switch (flag) {
3470 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05003471 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05003472 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05003473 /* Interrupt board to do it */
3474 writel(CA_MBATT, phba->CAregaddr);
3475 readl(phba->CAregaddr); /* flush */
3476 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05003477 break;
3478
3479 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05003480 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05003481 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05003482 /* Interrupt board to do it */
3483 writel(CA_MBATT, phba->CAregaddr);
3484 readl(phba->CAregaddr); /* flush */
3485
dea31012005-04-17 16:05:31 -05003486 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3487 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04003488 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003489 word0 = le32_to_cpu(word0);
3490 } else {
3491 /* First read mbox status word */
3492 word0 = readl(phba->MBslimaddr);
3493 }
3494
3495 /* Read the HBA Host Attention Register */
3496 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05003497 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3498 mb->mbxCommand) *
3499 1000) + jiffies;
3500 i = 0;
dea31012005-04-17 16:05:31 -05003501 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003502 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
3503 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05003504 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05003505 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05003506 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003507 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05003508 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04003509 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003510 }
3511
3512 /* Check if we took a mbox interrupt while we were
3513 polling */
3514 if (((word0 & OWN_CHIP) != OWN_CHIP)
3515 && (evtctr != psli->slistat.mbox_event))
3516 break;
3517
James Smart09372822008-01-11 01:52:54 -05003518 if (i++ > 10) {
3519 spin_unlock_irqrestore(&phba->hbalock,
3520 drvr_flag);
3521 msleep(1);
3522 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3523 }
dea31012005-04-17 16:05:31 -05003524
3525 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3526 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04003527 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003528 word0 = le32_to_cpu(word0);
3529 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3530 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04003531 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05003532 /* Check real SLIM for any errors */
3533 slimword0 = readl(phba->MBslimaddr);
3534 slimmb = (MAILBOX_t *) & slimword0;
3535 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
3536 && slimmb->mbxStatus) {
3537 psli->sli_flag &=
3538 ~LPFC_SLI2_ACTIVE;
3539 word0 = slimword0;
3540 }
3541 }
3542 } else {
3543 /* First copy command data */
3544 word0 = readl(phba->MBslimaddr);
3545 }
3546 /* Read the HBA Host Attention Register */
3547 ha_copy = readl(phba->HAregaddr);
3548 }
3549
3550 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003551 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04003552 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003553 } else {
3554 /* First copy command data */
3555 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
3556 MAILBOX_CMD_SIZE);
3557 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
3558 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05003559 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05003560 phba->MBslimaddr + DMP_RSP_OFFSET,
3561 mb->un.varDmp.word_cnt);
3562 }
3563 }
3564
3565 writel(HA_MBATT, phba->HAregaddr);
3566 readl(phba->HAregaddr); /* flush */
3567
3568 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3569 status = mb->mbxStatus;
3570 }
3571
James Smart2e0fef82007-06-17 19:56:36 -05003572 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3573 return status;
James Smart58da1ff2008-04-07 10:15:56 -04003574
3575out_not_finished:
3576 if (processing_queue) {
3577 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
3578 lpfc_mbox_cmpl_put(phba, pmbox);
3579 }
3580 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003581}
3582
James Smarte59058c2008-08-24 21:49:00 -04003583/**
3584 * __lpfc_sli_ringtx_put: Add an iocb to the txq.
3585 * @phba: Pointer to HBA context object.
3586 * @pring: Pointer to driver SLI ring object.
3587 * @piocb: Pointer to address of newly added command iocb.
3588 *
3589 * This function is called with hbalock held to add a command
3590 * iocb to the txq when SLI layer cannot submit the command iocb
3591 * to the ring.
3592 **/
James Smart858c9f62007-06-17 19:56:39 -05003593static void
James Smart92d7f7b2007-06-17 19:56:38 -05003594__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003595 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05003596{
3597 /* Insert the caller's iocb in the txq tail for later processing. */
3598 list_add_tail(&piocb->list, &pring->txq);
3599 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05003600}
3601
James Smarte59058c2008-08-24 21:49:00 -04003602/**
3603 * lpfc_sli_next_iocb: Get the next iocb in the txq.
3604 * @phba: Pointer to HBA context object.
3605 * @pring: Pointer to driver SLI ring object.
3606 * @piocb: Pointer to address of newly added command iocb.
3607 *
3608 * This function is called with hbalock held before a new
3609 * iocb is submitted to the firmware. This function checks
3610 * txq to flush the iocbs in txq to Firmware before
3611 * submitting new iocbs to the Firmware.
3612 * If there are iocbs in the txq which need to be submitted
3613 * to firmware, lpfc_sli_next_iocb returns the first element
3614 * of the txq after dequeuing it from txq.
3615 * If there is no iocb in the txq then the function will return
3616 * *piocb and *piocb is set to NULL. Caller needs to check
3617 * *piocb to find if there are more commands in the txq.
3618 **/
dea31012005-04-17 16:05:31 -05003619static struct lpfc_iocbq *
3620lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003621 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05003622{
3623 struct lpfc_iocbq * nextiocb;
3624
3625 nextiocb = lpfc_sli_ringtx_get(phba, pring);
3626 if (!nextiocb) {
3627 nextiocb = *piocb;
3628 *piocb = NULL;
3629 }
3630
3631 return nextiocb;
3632}
3633
James Smarte59058c2008-08-24 21:49:00 -04003634/**
3635 * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
3636 * @phba: Pointer to HBA context object.
3637 * @pring: Pointer to driver SLI ring object.
3638 * @piocb: Pointer to command iocb.
3639 * @flag: Flag indicating if this command can be put into txq.
3640 *
3641 * __lpfc_sli_issue_iocb is used by other functions in the driver
3642 * to issue an iocb command to the HBA. If the PCI slot is recovering
3643 * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
3644 * flag is turned on, the function returns IOCB_ERROR.
3645 * When the link is down, this function allows only iocbs for
3646 * posting buffers.
3647 * This function finds next available slot in the command ring and
3648 * posts the command to the available slot and writes the port
3649 * attention register to request HBA start processing new iocb.
3650 * If there is no slot available in the ring and
3651 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
3652 * txq, otherwise the function returns IOCB_BUSY.
3653 *
3654 * This function is called with hbalock held.
3655 * The function will return success after it successfully submit the
3656 * iocb to firmware or after adding to the txq.
3657 **/
James Smart98c9ea52007-10-27 13:37:33 -04003658static int
James Smart92d7f7b2007-06-17 19:56:38 -05003659__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003660 struct lpfc_iocbq *piocb, uint32_t flag)
3661{
3662 struct lpfc_iocbq *nextiocb;
3663 IOCB_t *iocb;
3664
James Smart92d7f7b2007-06-17 19:56:38 -05003665 if (piocb->iocb_cmpl && (!piocb->vport) &&
3666 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
3667 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
3668 lpfc_printf_log(phba, KERN_ERR,
3669 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003670 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003671 piocb->iocb.ulpCommand);
3672 dump_stack();
3673 return IOCB_ERROR;
3674 }
3675
3676
Linas Vepstas8d63f372007-02-14 14:28:36 -06003677 /* If the PCI channel is in offline state, do not post iocbs. */
3678 if (unlikely(pci_channel_offline(phba->pcidev)))
3679 return IOCB_ERROR;
3680
dea31012005-04-17 16:05:31 -05003681 /*
3682 * We should never get an IOCB if we are in a < LINK_DOWN state
3683 */
James Smart2e0fef82007-06-17 19:56:36 -05003684 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003685 return IOCB_ERROR;
3686
3687 /*
3688 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04003689 * outstanding event.
dea31012005-04-17 16:05:31 -05003690 */
James Smart0b727fe2007-10-27 13:37:25 -04003691 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05003692 goto iocb_busy;
3693
James Smart2e0fef82007-06-17 19:56:36 -05003694 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05003695 /*
James Smart2680eea2007-04-25 09:52:55 -04003696 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05003697 * can be issued if the link is not up.
3698 */
3699 switch (piocb->iocb.ulpCommand) {
3700 case CMD_QUE_RING_BUF_CN:
3701 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05003702 /*
3703 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
3704 * completion, iocb_cmpl MUST be 0.
3705 */
3706 if (piocb->iocb_cmpl)
3707 piocb->iocb_cmpl = NULL;
3708 /*FALLTHROUGH*/
3709 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04003710 case CMD_CLOSE_XRI_CN:
3711 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05003712 break;
3713 default:
3714 goto iocb_busy;
3715 }
3716
3717 /*
3718 * For FCP commands, we must be in a state where we can process link
3719 * attention events.
3720 */
3721 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05003722 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05003723 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05003724 }
dea31012005-04-17 16:05:31 -05003725
dea31012005-04-17 16:05:31 -05003726 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
3727 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
3728 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
3729
3730 if (iocb)
3731 lpfc_sli_update_ring(phba, pring);
3732 else
3733 lpfc_sli_update_full_ring(phba, pring);
3734
3735 if (!piocb)
3736 return IOCB_SUCCESS;
3737
3738 goto out_busy;
3739
3740 iocb_busy:
3741 pring->stats.iocb_cmd_delay++;
3742
3743 out_busy:
3744
3745 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003746 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05003747 return IOCB_SUCCESS;
3748 }
3749
3750 return IOCB_BUSY;
3751}
3752
James Smart92d7f7b2007-06-17 19:56:38 -05003753
James Smarte59058c2008-08-24 21:49:00 -04003754/**
3755 * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
3756 * @phba: Pointer to HBA context object.
3757 * @pring: Pointer to driver SLI ring object.
3758 * @piocb: Pointer to command iocb.
3759 * @flag: Flag indicating if this command can be put into txq.
3760 *
3761 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
3762 * function. This function gets the hbalock and calls
3763 * __lpfc_sli_issue_iocb function and will return the error returned
3764 * by __lpfc_sli_issue_iocb function. This wrapper is used by
3765 * functions which do not hold hbalock.
3766 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003767int
3768lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3769 struct lpfc_iocbq *piocb, uint32_t flag)
3770{
3771 unsigned long iflags;
3772 int rc;
3773
3774 spin_lock_irqsave(&phba->hbalock, iflags);
3775 rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
3776 spin_unlock_irqrestore(&phba->hbalock, iflags);
3777
3778 return rc;
3779}
3780
James Smarte59058c2008-08-24 21:49:00 -04003781/**
3782 * lpfc_extra_ring_setup: Extra ring setup function.
3783 * @phba: Pointer to HBA context object.
3784 *
3785 * This function is called while driver attaches with the
3786 * HBA to setup the extra ring. The extra ring is used
3787 * only when driver needs to support target mode functionality
3788 * or IP over FC functionalities.
3789 *
3790 * This function is called with no lock held.
3791 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003792static int
3793lpfc_extra_ring_setup( struct lpfc_hba *phba)
3794{
3795 struct lpfc_sli *psli;
3796 struct lpfc_sli_ring *pring;
3797
3798 psli = &phba->sli;
3799
3800 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05003801
3802 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003803 pring = &psli->ring[psli->fcp_ring];
3804 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3805 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3806 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3807 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3808
James Smarta4bc3372006-12-02 13:34:16 -05003809 /* and give them to the extra ring */
3810 pring = &psli->ring[psli->extra_ring];
3811
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003812 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3813 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3814 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3815 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3816
3817 /* Setup default profile for this ring */
3818 pring->iotag_max = 4096;
3819 pring->num_mask = 1;
3820 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05003821 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
3822 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003823 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
3824 return 0;
3825}
3826
James Smarte59058c2008-08-24 21:49:00 -04003827/**
3828 * lpfc_sli_async_event_handler: ASYNC iocb handler function.
3829 * @phba: Pointer to HBA context object.
3830 * @pring: Pointer to driver SLI ring object.
3831 * @iocbq: Pointer to iocb object.
3832 *
3833 * This function is called by the slow ring event handler
3834 * function when there is an ASYNC event iocb in the ring.
3835 * This function is called with no lock held.
3836 * Currently this function handles only temperature related
3837 * ASYNC events. The function decodes the temperature sensor
3838 * event message and posts events for the management applications.
3839 **/
James Smart98c9ea52007-10-27 13:37:33 -04003840static void
James Smart57127f12007-10-27 13:37:05 -04003841lpfc_sli_async_event_handler(struct lpfc_hba * phba,
3842 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
3843{
3844 IOCB_t *icmd;
3845 uint16_t evt_code;
3846 uint16_t temp;
3847 struct temp_event temp_event_data;
3848 struct Scsi_Host *shost;
3849
3850 icmd = &iocbq->iocb;
3851 evt_code = icmd->un.asyncstat.evt_code;
3852 temp = icmd->ulpContext;
3853
3854 if ((evt_code != ASYNC_TEMP_WARN) &&
3855 (evt_code != ASYNC_TEMP_SAFE)) {
3856 lpfc_printf_log(phba,
3857 KERN_ERR,
3858 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003859 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smart57127f12007-10-27 13:37:05 -04003860 " evt_code 0x%x\n",
3861 pring->ringno,
3862 icmd->un.asyncstat.evt_code);
3863 return;
3864 }
3865 temp_event_data.data = (uint32_t)temp;
3866 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
3867 if (evt_code == ASYNC_TEMP_WARN) {
3868 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
3869 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003870 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003871 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04003872 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04003873 "corrective action. temperature : %d Celsius\n",
3874 temp);
3875 }
3876 if (evt_code == ASYNC_TEMP_SAFE) {
3877 temp_event_data.event_code = LPFC_NORMAL_TEMP;
3878 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003879 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003880 LOG_TEMP,
3881 "0340 Adapter temperature is OK now. "
3882 "temperature : %d Celsius\n",
3883 temp);
3884 }
3885
3886 /* Send temperature change event to applications */
3887 shost = lpfc_shost_from_vport(phba->pport);
3888 fc_host_post_vendor_event(shost, fc_get_event_number(),
3889 sizeof(temp_event_data), (char *) &temp_event_data,
3890 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
3891
3892}
3893
3894
James Smarte59058c2008-08-24 21:49:00 -04003895/**
3896 * lpfc_sli_setup: SLI ring setup function.
3897 * @phba: Pointer to HBA context object.
3898 *
3899 * lpfc_sli_setup sets up rings of the SLI interface with
3900 * number of iocbs per ring and iotags. This function is
3901 * called while driver attach to the HBA and before the
3902 * interrupts are enabled. So there is no need for locking.
3903 *
3904 * This function always returns 0.
3905 **/
dea31012005-04-17 16:05:31 -05003906int
3907lpfc_sli_setup(struct lpfc_hba *phba)
3908{
James Smarted957682007-06-17 19:56:37 -05003909 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05003910 struct lpfc_sli *psli = &phba->sli;
3911 struct lpfc_sli_ring *pring;
3912
3913 psli->num_rings = MAX_CONFIGURED_RINGS;
3914 psli->sli_flag = 0;
3915 psli->fcp_ring = LPFC_FCP_RING;
3916 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05003917 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05003918
James Bottomley604a3e32005-10-29 10:28:33 -05003919 psli->iocbq_lookup = NULL;
3920 psli->iocbq_lookup_len = 0;
3921 psli->last_iotag = 0;
3922
dea31012005-04-17 16:05:31 -05003923 for (i = 0; i < psli->num_rings; i++) {
3924 pring = &psli->ring[i];
3925 switch (i) {
3926 case LPFC_FCP_RING: /* ring 0 - FCP */
3927 /* numCiocb and numRiocb are used in config_port */
3928 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3929 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3930 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3931 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3932 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3933 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003934 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003935 SLI3_IOCB_CMD_SIZE :
3936 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003937 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003938 SLI3_IOCB_RSP_SIZE :
3939 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003940 pring->iotag_ctr = 0;
3941 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05003942 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05003943 pring->fast_iotag = pring->iotag_max;
3944 pring->num_mask = 0;
3945 break;
James Smarta4bc3372006-12-02 13:34:16 -05003946 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05003947 /* numCiocb and numRiocb are used in config_port */
3948 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
3949 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003950 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003951 SLI3_IOCB_CMD_SIZE :
3952 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003953 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003954 SLI3_IOCB_RSP_SIZE :
3955 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05003956 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05003957 pring->num_mask = 0;
3958 break;
3959 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
3960 /* numCiocb and numRiocb are used in config_port */
3961 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
3962 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05003963 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003964 SLI3_IOCB_CMD_SIZE :
3965 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05003966 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05003967 SLI3_IOCB_RSP_SIZE :
3968 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05003969 pring->fast_iotag = 0;
3970 pring->iotag_ctr = 0;
3971 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04003972 pring->lpfc_sli_rcv_async_status =
3973 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05003974 pring->num_mask = 4;
3975 pring->prt[0].profile = 0; /* Mask 0 */
3976 pring->prt[0].rctl = FC_ELS_REQ;
3977 pring->prt[0].type = FC_ELS_DATA;
3978 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003979 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003980 pring->prt[1].profile = 0; /* Mask 1 */
3981 pring->prt[1].rctl = FC_ELS_RSP;
3982 pring->prt[1].type = FC_ELS_DATA;
3983 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003984 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05003985 pring->prt[2].profile = 0; /* Mask 2 */
3986 /* NameServer Inquiry */
3987 pring->prt[2].rctl = FC_UNSOL_CTL;
3988 /* NameServer */
3989 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
3990 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003991 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003992 pring->prt[3].profile = 0; /* Mask 3 */
3993 /* NameServer response */
3994 pring->prt[3].rctl = FC_SOL_CTL;
3995 /* NameServer */
3996 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
3997 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05003998 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05003999 break;
4000 }
James Smarted957682007-06-17 19:56:37 -05004001 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05004002 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05004003 }
James Smarted957682007-06-17 19:56:37 -05004004 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05004005 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04004006 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
4007 "SLI2 SLIM Data: x%x x%lx\n",
4008 phba->brd_no, totiocbsize,
4009 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05004010 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004011 if (phba->cfg_multi_ring_support == 2)
4012 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05004013
4014 return 0;
4015}
4016
James Smarte59058c2008-08-24 21:49:00 -04004017/**
4018 * lpfc_sli_queue_setup: Queue initialization function.
4019 * @phba: Pointer to HBA context object.
4020 *
4021 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
4022 * ring. This function also initializes ring indices of each ring.
4023 * This function is called during the initialization of the SLI
4024 * interface of an HBA.
4025 * This function is called with no lock held and always returns
4026 * 1.
4027 **/
dea31012005-04-17 16:05:31 -05004028int
James Smart2e0fef82007-06-17 19:56:36 -05004029lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004030{
4031 struct lpfc_sli *psli;
4032 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05004033 int i;
dea31012005-04-17 16:05:31 -05004034
4035 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05004036 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004037 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05004038 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05004039 /* Initialize list headers for txq and txcmplq as double linked lists */
4040 for (i = 0; i < psli->num_rings; i++) {
4041 pring = &psli->ring[i];
4042 pring->ringno = i;
4043 pring->next_cmdidx = 0;
4044 pring->local_getidx = 0;
4045 pring->cmdidx = 0;
4046 INIT_LIST_HEAD(&pring->txq);
4047 INIT_LIST_HEAD(&pring->txcmplq);
4048 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05004049 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05004050 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05004051 }
James Smart2e0fef82007-06-17 19:56:36 -05004052 spin_unlock_irq(&phba->hbalock);
4053 return 1;
dea31012005-04-17 16:05:31 -05004054}
4055
James Smarte59058c2008-08-24 21:49:00 -04004056/**
4057 * lpfc_sli_host_down: Vport cleanup function.
4058 * @vport: Pointer to virtual port object.
4059 *
4060 * lpfc_sli_host_down is called to clean up the resources
4061 * associated with a vport before destroying virtual
4062 * port data structures.
4063 * This function does following operations:
4064 * - Free discovery resources associated with this virtual
4065 * port.
4066 * - Free iocbs associated with this virtual port in
4067 * the txq.
4068 * - Send abort for all iocb commands associated with this
4069 * vport in txcmplq.
4070 *
4071 * This function is called with no lock held and always returns 1.
4072 **/
dea31012005-04-17 16:05:31 -05004073int
James Smart92d7f7b2007-06-17 19:56:38 -05004074lpfc_sli_host_down(struct lpfc_vport *vport)
4075{
James Smart858c9f62007-06-17 19:56:39 -05004076 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004077 struct lpfc_hba *phba = vport->phba;
4078 struct lpfc_sli *psli = &phba->sli;
4079 struct lpfc_sli_ring *pring;
4080 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004081 int i;
4082 unsigned long flags = 0;
4083 uint16_t prev_pring_flag;
4084
4085 lpfc_cleanup_discovery_resources(vport);
4086
4087 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004088 for (i = 0; i < psli->num_rings; i++) {
4089 pring = &psli->ring[i];
4090 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04004091 /* Only slow rings */
4092 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004093 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004094 /* Set the lpfc data pending flag */
4095 set_bit(LPFC_DATA_READY, &phba->data_flags);
4096 }
James Smart92d7f7b2007-06-17 19:56:38 -05004097 /*
4098 * Error everything on the txq since these iocbs have not been
4099 * given to the FW yet.
4100 */
James Smart92d7f7b2007-06-17 19:56:38 -05004101 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4102 if (iocb->vport != vport)
4103 continue;
James Smart858c9f62007-06-17 19:56:39 -05004104 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004105 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05004106 }
4107
4108 /* Next issue ABTS for everything on the txcmplq */
4109 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
4110 list) {
4111 if (iocb->vport != vport)
4112 continue;
4113 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4114 }
4115
4116 pring->flag = prev_pring_flag;
4117 }
4118
4119 spin_unlock_irqrestore(&phba->hbalock, flags);
4120
James Smart858c9f62007-06-17 19:56:39 -05004121 while (!list_empty(&completions)) {
4122 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
4123
4124 if (!iocb->iocb_cmpl)
4125 lpfc_sli_release_iocbq(phba, iocb);
4126 else {
4127 iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4128 iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
4129 (iocb->iocb_cmpl) (phba, iocb, iocb);
4130 }
4131 }
James Smart92d7f7b2007-06-17 19:56:38 -05004132 return 1;
4133}
4134
James Smarte59058c2008-08-24 21:49:00 -04004135/**
4136 * lpfc_sli_hba_down: Resource cleanup function for the HBA.
4137 * @phba: Pointer to HBA context object.
4138 *
4139 * This function cleans up all iocb, buffers, mailbox commands
4140 * while shutting down the HBA. This function is called with no
4141 * lock held and always returns 1.
4142 * This function does the following to cleanup driver resources:
4143 * - Free discovery resources for each virtual port
4144 * - Cleanup any pending fabric iocbs
4145 * - Iterate through the iocb txq and free each entry
4146 * in the list.
4147 * - Free up any buffer posted to the HBA
4148 * - Free mailbox commands in the mailbox queue.
4149 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004150int
James Smart2e0fef82007-06-17 19:56:36 -05004151lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004152{
James Smart2534ba72007-04-25 09:52:20 -04004153 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05004154 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004155 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05004156 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05004157 LPFC_MBOXQ_t *pmb;
James Smart2534ba72007-04-25 09:52:20 -04004158 struct lpfc_iocbq *iocb;
4159 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004160 int i;
4161 unsigned long flags = 0;
4162
dea31012005-04-17 16:05:31 -05004163 lpfc_hba_down_prep(phba);
4164
James Smart92d7f7b2007-06-17 19:56:38 -05004165 lpfc_fabric_abort_hba(phba);
4166
James Smart2e0fef82007-06-17 19:56:36 -05004167 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004168 for (i = 0; i < psli->num_rings; i++) {
4169 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04004170 /* Only slow rings */
4171 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004172 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004173 /* Set the lpfc data pending flag */
4174 set_bit(LPFC_DATA_READY, &phba->data_flags);
4175 }
dea31012005-04-17 16:05:31 -05004176
4177 /*
4178 * Error everything on the txq since these iocbs have not been
4179 * given to the FW yet.
4180 */
James Smart2534ba72007-04-25 09:52:20 -04004181 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05004182 pring->txq_cnt = 0;
4183
dea31012005-04-17 16:05:31 -05004184 }
James Smart2e0fef82007-06-17 19:56:36 -05004185 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004186
James Smart2534ba72007-04-25 09:52:20 -04004187 while (!list_empty(&completions)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004188 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
James Smart2534ba72007-04-25 09:52:20 -04004189 cmd = &iocb->iocb;
James Smart2534ba72007-04-25 09:52:20 -04004190
James Smart2e0fef82007-06-17 19:56:36 -05004191 if (!iocb->iocb_cmpl)
4192 lpfc_sli_release_iocbq(phba, iocb);
4193 else {
James Smart2534ba72007-04-25 09:52:20 -04004194 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4195 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
4196 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05004197 }
James Smart2534ba72007-04-25 09:52:20 -04004198 }
4199
James Smart0ff10d42008-01-11 01:52:36 -05004200 spin_lock_irqsave(&phba->hbalock, flags);
4201 list_splice_init(&phba->elsbuf, &completions);
4202 phba->elsbuf_cnt = 0;
4203 phba->elsbuf_prev_cnt = 0;
4204 spin_unlock_irqrestore(&phba->hbalock, flags);
4205
4206 while (!list_empty(&completions)) {
4207 list_remove_head(&completions, buf_ptr,
4208 struct lpfc_dmabuf, list);
4209 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4210 kfree(buf_ptr);
4211 }
4212
dea31012005-04-17 16:05:31 -05004213 /* Return any active mbox cmds */
4214 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05004215 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004216
4217 spin_lock(&phba->pport->work_port_lock);
4218 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4219 spin_unlock(&phba->pport->work_port_lock);
4220
James Smart97eab632008-04-07 10:16:05 -04004221 /* Return any pending or completed mbox cmds */
4222 list_splice_init(&phba->sli.mboxq, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004223 if (psli->mbox_active) {
4224 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05004225 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004226 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05004227 }
James Smart92d7f7b2007-06-17 19:56:38 -05004228 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004229 spin_unlock_irqrestore(&phba->hbalock, flags);
4230
4231 while (!list_empty(&completions)) {
4232 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05004233 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
James Smart97eab632008-04-07 10:16:05 -04004234 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05004235 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05004236 }
dea31012005-04-17 16:05:31 -05004237 return 1;
4238}
4239
James Smarte59058c2008-08-24 21:49:00 -04004240/**
4241 * lpfc_sli_pcimem_bcopy: SLI memory copy function.
4242 * @srcp: Source memory pointer.
4243 * @destp: Destination memory pointer.
4244 * @cnt: Number of words required to be copied.
4245 *
4246 * This function is used for copying data between driver memory
4247 * and the SLI memory. This function also changes the endianness
4248 * of each word if native endianness is different from SLI
4249 * endianness. This function can be called with or without
4250 * lock.
4251 **/
dea31012005-04-17 16:05:31 -05004252void
4253lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
4254{
4255 uint32_t *src = srcp;
4256 uint32_t *dest = destp;
4257 uint32_t ldata;
4258 int i;
4259
4260 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
4261 ldata = *src;
4262 ldata = le32_to_cpu(ldata);
4263 *dest = ldata;
4264 src++;
4265 dest++;
4266 }
4267}
4268
James Smarte59058c2008-08-24 21:49:00 -04004269
4270/**
4271 * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
4272 * @phba: Pointer to HBA context object.
4273 * @pring: Pointer to driver SLI ring object.
4274 * @mp: Pointer to driver buffer object.
4275 *
4276 * This function is called with no lock held.
4277 * It always return zero after adding the buffer to the postbufq
4278 * buffer list.
4279 **/
dea31012005-04-17 16:05:31 -05004280int
James Smart2e0fef82007-06-17 19:56:36 -05004281lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4282 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05004283{
4284 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
4285 later */
James Smart2e0fef82007-06-17 19:56:36 -05004286 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004287 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05004288 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05004289 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004290 return 0;
4291}
4292
James Smarte59058c2008-08-24 21:49:00 -04004293/**
4294 * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
4295 * using CMD_QUE_XRI64_CX iocb.
4296 * @phba: Pointer to HBA context object.
4297 *
4298 * When HBQ is enabled, buffers are searched based on tags. This function
4299 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
4300 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
4301 * does not conflict with tags of buffer posted for unsolicited events.
4302 * The function returns the allocated tag. The function is called with
4303 * no locks held.
4304 **/
James Smart76bb24e2007-10-27 13:38:00 -04004305uint32_t
4306lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
4307{
4308 spin_lock_irq(&phba->hbalock);
4309 phba->buffer_tag_count++;
4310 /*
4311 * Always set the QUE_BUFTAG_BIT to distiguish between
4312 * a tag assigned by HBQ.
4313 */
4314 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
4315 spin_unlock_irq(&phba->hbalock);
4316 return phba->buffer_tag_count;
4317}
4318
James Smarte59058c2008-08-24 21:49:00 -04004319/**
4320 * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
4321 * posted using CMD_QUE_XRI64_CX iocb.
4322 * @phba: Pointer to HBA context object.
4323 * @pring: Pointer to driver SLI ring object.
4324 * @tag: Buffer tag.
4325 *
4326 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
4327 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
4328 * iocb is posted to the response ring with the tag of the buffer.
4329 * This function searches the pring->postbufq list using the tag
4330 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
4331 * iocb. If the buffer is found then lpfc_dmabuf object of the
4332 * buffer is returned to the caller else NULL is returned.
4333 * This function is called with no lock held.
4334 **/
James Smart76bb24e2007-10-27 13:38:00 -04004335struct lpfc_dmabuf *
4336lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4337 uint32_t tag)
4338{
4339 struct lpfc_dmabuf *mp, *next_mp;
4340 struct list_head *slp = &pring->postbufq;
4341
4342 /* Search postbufq, from the begining, looking for a match on tag */
4343 spin_lock_irq(&phba->hbalock);
4344 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4345 if (mp->buffer_tag == tag) {
4346 list_del_init(&mp->list);
4347 pring->postbufq_cnt--;
4348 spin_unlock_irq(&phba->hbalock);
4349 return mp;
4350 }
4351 }
4352
4353 spin_unlock_irq(&phba->hbalock);
4354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04004355 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04004356 "ring %d Data x%lx x%p x%p x%x\n",
4357 pring->ringno, (unsigned long) tag,
4358 slp->next, slp->prev, pring->postbufq_cnt);
4359
4360 return NULL;
4361}
dea31012005-04-17 16:05:31 -05004362
James Smarte59058c2008-08-24 21:49:00 -04004363/**
4364 * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
4365 * unsolicited ct and els events.
4366 * @phba: Pointer to HBA context object.
4367 * @pring: Pointer to driver SLI ring object.
4368 * @phys: DMA address of the buffer.
4369 *
4370 * This function searches the buffer list using the dma_address
4371 * of unsolicited event to find the driver's lpfc_dmabuf object
4372 * corresponding to the dma_address. The function returns the
4373 * lpfc_dmabuf object if a buffer is found else it returns NULL.
4374 * This function is called by the ct and els unsolicited event
4375 * handlers to get the buffer associated with the unsolicited
4376 * event.
4377 *
4378 * This function is called with no lock held.
4379 **/
dea31012005-04-17 16:05:31 -05004380struct lpfc_dmabuf *
4381lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4382 dma_addr_t phys)
4383{
4384 struct lpfc_dmabuf *mp, *next_mp;
4385 struct list_head *slp = &pring->postbufq;
4386
4387 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05004388 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004389 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4390 if (mp->phys == phys) {
4391 list_del_init(&mp->list);
4392 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004393 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004394 return mp;
4395 }
4396 }
4397
James Smart2e0fef82007-06-17 19:56:36 -05004398 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004399 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004400 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05004401 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004402 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05004403 slp->next, slp->prev, pring->postbufq_cnt);
4404 return NULL;
4405}
4406
James Smarte59058c2008-08-24 21:49:00 -04004407/**
4408 * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
4409 * @phba: Pointer to HBA context object.
4410 * @cmdiocb: Pointer to driver command iocb object.
4411 * @rspiocb: Pointer to driver response iocb object.
4412 *
4413 * This function is the completion handler for the abort iocbs for
4414 * ELS commands. This function is called from the ELS ring event
4415 * handler with no lock held. This function frees memory resources
4416 * associated with the abort iocb.
4417 **/
dea31012005-04-17 16:05:31 -05004418static void
James Smart2e0fef82007-06-17 19:56:36 -05004419lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4420 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05004421{
James Smart2e0fef82007-06-17 19:56:36 -05004422 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04004423 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05004424 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04004425 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4426
4427 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04004428
4429 if (irsp->ulpStatus) {
4430 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
4431 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
4432
James Smart2e0fef82007-06-17 19:56:36 -05004433 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004434 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
4435 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
4436
James Smart92d7f7b2007-06-17 19:56:38 -05004437 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004438 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05004439 "with tag %x context %x, abort status %x, "
4440 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04004441 abort_iocb, abort_iotag, abort_context,
4442 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04004443
4444 /*
James Smart58da1ff2008-04-07 10:15:56 -04004445 * If the iocb is not found in Firmware queue the iocb
4446 * might have completed already. Do not free it again.
4447 */
James Smart9b379602008-04-07 10:16:00 -04004448 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04004449 spin_unlock_irq(&phba->hbalock);
4450 lpfc_sli_release_iocbq(phba, cmdiocb);
4451 return;
4452 }
4453 /*
James Smart2680eea2007-04-25 09:52:55 -04004454 * make sure we have the right iocbq before taking it
4455 * off the txcmplq and try to call completion routine.
4456 */
James Smart2e0fef82007-06-17 19:56:36 -05004457 if (!abort_iocb ||
4458 abort_iocb->iocb.ulpContext != abort_context ||
4459 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
4460 spin_unlock_irq(&phba->hbalock);
4461 else {
James Smart92d7f7b2007-06-17 19:56:38 -05004462 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04004463 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004464 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004465
James Smart0ff10d42008-01-11 01:52:36 -05004466 /* Firmware could still be in progress of DMAing
4467 * payload, so don't free data buffer till after
4468 * a hbeat.
4469 */
4470 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
4471
James Smart92d7f7b2007-06-17 19:56:38 -05004472 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4473 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4474 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
4475 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04004476 }
4477 }
4478
James Bottomley604a3e32005-10-29 10:28:33 -05004479 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05004480 return;
4481}
4482
James Smarte59058c2008-08-24 21:49:00 -04004483/**
4484 * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
4485 * @phba: Pointer to HBA context object.
4486 * @cmdiocb: Pointer to driver command iocb object.
4487 * @rspiocb: Pointer to driver response iocb object.
4488 *
4489 * The function is called from SLI ring event handler with no
4490 * lock held. This function is the completion handler for ELS commands
4491 * which are aborted. The function frees memory resources used for
4492 * the aborted ELS commands.
4493 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004494static void
4495lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4496 struct lpfc_iocbq *rspiocb)
4497{
4498 IOCB_t *irsp = &rspiocb->iocb;
4499
4500 /* ELS cmd tag <ulpIoTag> completes */
4501 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04004502 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004503 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004504 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05004505 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05004506 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
4507 lpfc_ct_free_iocb(phba, cmdiocb);
4508 else
4509 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05004510 return;
4511}
4512
James Smarte59058c2008-08-24 21:49:00 -04004513/**
4514 * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
4515 * @phba: Pointer to HBA context object.
4516 * @pring: Pointer to driver SLI ring object.
4517 * @cmdiocb: Pointer to driver command iocb object.
4518 *
4519 * This function issues an abort iocb for the provided command
4520 * iocb. This function is called with hbalock held.
4521 * The function returns 0 when it fails due to memory allocation
4522 * failure or when the command iocb is an abort request.
4523 **/
dea31012005-04-17 16:05:31 -05004524int
James Smart2e0fef82007-06-17 19:56:36 -05004525lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4526 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05004527{
James Smart2e0fef82007-06-17 19:56:36 -05004528 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004529 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05004530 IOCB_t *icmd = NULL;
4531 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04004532 int retval = IOCB_ERROR;
4533
James Smart92d7f7b2007-06-17 19:56:38 -05004534 /*
4535 * There are certain command types we don't want to abort. And we
4536 * don't want to abort commands that are already in the process of
4537 * being aborted.
James Smart07951072007-04-25 09:51:38 -04004538 */
4539 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004540 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05004541 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4542 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04004543 return 0;
4544
James Smart858c9f62007-06-17 19:56:39 -05004545 /* If we're unloading, don't abort iocb on the ELS ring, but change the
4546 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04004547 */
James Smart858c9f62007-06-17 19:56:39 -05004548 if ((vport->load_flag & FC_UNLOADING) &&
4549 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004550 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
4551 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
4552 else
4553 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04004554 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05004555 }
dea31012005-04-17 16:05:31 -05004556
4557 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05004558 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004559 if (abtsiocbp == NULL)
4560 return 0;
dea31012005-04-17 16:05:31 -05004561
James Smart07951072007-04-25 09:51:38 -04004562 /* This signals the response to set the correct status
4563 * before calling the completion handler.
4564 */
4565 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4566
dea31012005-04-17 16:05:31 -05004567 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04004568 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
4569 iabt->un.acxri.abortContextTag = icmd->ulpContext;
4570 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004571 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04004572 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05004573
James Smart2e0fef82007-06-17 19:56:36 -05004574 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04004575 iabt->ulpCommand = CMD_ABORT_XRI_CN;
4576 else
4577 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
4578
4579 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04004580
James Smarte8b62012007-08-02 11:10:09 -04004581 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4582 "0339 Abort xri x%x, original iotag x%x, "
4583 "abort cmd iotag x%x\n",
4584 iabt->un.acxri.abortContextTag,
4585 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05004586 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04004587
James Smartd7c255b2008-08-24 21:50:00 -04004588 if (retval)
4589 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04004590abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05004591 /*
4592 * Caller to this routine should check for IOCB_ERROR
4593 * and handle it properly. This routine no longer removes
4594 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04004595 */
James Smart2e0fef82007-06-17 19:56:36 -05004596 return retval;
dea31012005-04-17 16:05:31 -05004597}
4598
James Smarte59058c2008-08-24 21:49:00 -04004599/**
4600 * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
4601 * associated with a vport/SCSI target/lun.
4602 * @iocbq: Pointer to driver iocb object.
4603 * @vport: Pointer to driver virtual port object.
4604 * @tgt_id: SCSI ID of the target.
4605 * @lun_id: LUN ID of the scsi device.
4606 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
4607 *
4608 * This function acts as iocb filter for functions which abort or count
4609 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
4610 * 0 if the filtering criteria is met for the given iocb and will return
4611 * 1 if the filtering criteria is not met.
4612 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
4613 * given iocb is for the SCSI device specified by vport, tgt_id and
4614 * lun_id parameter.
4615 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
4616 * given iocb is for the SCSI target specified by vport and tgt_id
4617 * parameters.
4618 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
4619 * given iocb is for the SCSI host associated with the given vport.
4620 * This function is called with no locks held.
4621 **/
dea31012005-04-17 16:05:31 -05004622static int
James Smart51ef4c22007-08-02 11:10:31 -04004623lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
4624 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004625 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004626{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004627 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004628 int rc = 1;
4629
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004630 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
4631 return rc;
4632
James Smart51ef4c22007-08-02 11:10:31 -04004633 if (iocbq->vport != vport)
4634 return rc;
4635
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004636 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004637
James Smart495a7142008-06-14 22:52:59 -04004638 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05004639 return rc;
4640
4641 switch (ctx_cmd) {
4642 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04004643 if ((lpfc_cmd->rdata->pnode) &&
4644 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
4645 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05004646 rc = 0;
4647 break;
4648 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04004649 if ((lpfc_cmd->rdata->pnode) &&
4650 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05004651 rc = 0;
4652 break;
dea31012005-04-17 16:05:31 -05004653 case LPFC_CTX_HOST:
4654 rc = 0;
4655 break;
4656 default:
4657 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004658 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05004659 break;
4660 }
4661
4662 return rc;
4663}
4664
James Smarte59058c2008-08-24 21:49:00 -04004665/**
4666 * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
4667 * @vport: Pointer to virtual port.
4668 * @tgt_id: SCSI ID of the target.
4669 * @lun_id: LUN ID of the scsi device.
4670 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4671 *
4672 * This function returns number of FCP commands pending for the vport.
4673 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
4674 * commands pending on the vport associated with SCSI device specified
4675 * by tgt_id and lun_id parameters.
4676 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
4677 * commands pending on the vport associated with SCSI target specified
4678 * by tgt_id parameter.
4679 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
4680 * commands pending on the vport.
4681 * This function returns the number of iocbs which satisfy the filter.
4682 * This function is called without any lock held.
4683 **/
dea31012005-04-17 16:05:31 -05004684int
James Smart51ef4c22007-08-02 11:10:31 -04004685lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
4686 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004687{
James Smart51ef4c22007-08-02 11:10:31 -04004688 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004689 struct lpfc_iocbq *iocbq;
4690 int sum, i;
dea31012005-04-17 16:05:31 -05004691
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004692 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
4693 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004694
James Smart51ef4c22007-08-02 11:10:31 -04004695 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
4696 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004697 sum++;
dea31012005-04-17 16:05:31 -05004698 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004699
dea31012005-04-17 16:05:31 -05004700 return sum;
4701}
4702
James Smarte59058c2008-08-24 21:49:00 -04004703/**
4704 * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
4705 * FCP iocb.
4706 * @phba: Pointer to HBA context object
4707 * @cmdiocb: Pointer to command iocb object.
4708 * @rspiocb: Pointer to response iocb object.
4709 *
4710 * This function is called when an aborted FCP iocb completes. This
4711 * function is called by the ring event handler with no lock held.
4712 * This function frees the iocb.
4713 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004714void
James Smart2e0fef82007-06-17 19:56:36 -05004715lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4716 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004717{
James Bottomley604a3e32005-10-29 10:28:33 -05004718 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004719 return;
4720}
4721
James Smarte59058c2008-08-24 21:49:00 -04004722/**
4723 * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
4724 * pending on a SCSI host(vport)/target/lun.
4725 * @vport: Pointer to virtual port.
4726 * @pring: Pointer to driver SLI ring object.
4727 * @tgt_id: SCSI ID of the target.
4728 * @lun_id: LUN ID of the scsi device.
4729 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4730 *
4731 * This function sends an abort command for every SCSI command
4732 * associated with the given virtual port pending on the ring
4733 * filtered by lpfc_sli_validate_fcp_iocb function.
4734 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
4735 * FCP iocbs associated with lun specified by tgt_id and lun_id
4736 * parameters
4737 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
4738 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
4739 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
4740 * FCP iocbs associated with virtual port.
4741 * This function returns number of iocbs it failed to abort.
4742 * This function is called with no locks held.
4743 **/
dea31012005-04-17 16:05:31 -05004744int
James Smart51ef4c22007-08-02 11:10:31 -04004745lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
4746 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05004747{
James Smart51ef4c22007-08-02 11:10:31 -04004748 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004749 struct lpfc_iocbq *iocbq;
4750 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004751 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004752 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004753 int i;
dea31012005-04-17 16:05:31 -05004754
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004755 for (i = 1; i <= phba->sli.last_iotag; i++) {
4756 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004757
James Smart51ef4c22007-08-02 11:10:31 -04004758 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05004759 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05004760 continue;
4761
4762 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004763 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004764 if (abtsiocb == NULL) {
4765 errcnt++;
4766 continue;
4767 }
dea31012005-04-17 16:05:31 -05004768
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004769 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05004770 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
4771 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
4772 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
4773 abtsiocb->iocb.ulpLe = 1;
4774 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05004775 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05004776
James Smart2e0fef82007-06-17 19:56:36 -05004777 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004778 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
4779 else
4780 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
4781
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004782 /* Setup callback routine and issue the command. */
4783 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05004784 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
4785 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05004786 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05004787 errcnt++;
4788 continue;
4789 }
4790 }
4791
4792 return errcnt;
4793}
4794
James Smarte59058c2008-08-24 21:49:00 -04004795/**
4796 * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
4797 * lpfc_sli_issue_iocb_wait.
4798 * @phba: Pointer to HBA context object.
4799 * @cmdiocbq: Pointer to command iocb.
4800 * @rspiocbq: Pointer to response iocb.
4801 *
4802 * This function is the completion handler for iocbs issued using
4803 * lpfc_sli_issue_iocb_wait function. This function is called by the
4804 * ring event handler function without any lock held. This function
4805 * can be called from both worker thread context and interrupt
4806 * context. This function also can be called from other thread which
4807 * cleans up the SLI layer objects.
4808 * This function copy the contents of the response iocb to the
4809 * response iocb memory object provided by the caller of
4810 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4811 * sleeps for the iocb completion.
4812 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004813static void
4814lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
4815 struct lpfc_iocbq *cmdiocbq,
4816 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05004817{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004818 wait_queue_head_t *pdone_q;
4819 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004820
James Smart2e0fef82007-06-17 19:56:36 -05004821 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004822 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4823 if (cmdiocbq->context2 && rspiocbq)
4824 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4825 &rspiocbq->iocb, sizeof(IOCB_t));
4826
4827 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004828 if (pdone_q)
4829 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05004830 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05004831 return;
4832}
4833
James Smarte59058c2008-08-24 21:49:00 -04004834/**
4835 * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
4836 * @phba: Pointer to HBA context object..
4837 * @pring: Pointer to sli ring.
4838 * @piocb: Pointer to command iocb.
4839 * @prspiocbq: Pointer to response iocb.
4840 * @timeout: Timeout in number of seconds.
4841 *
4842 * This function issues the iocb to firmware and waits for the
4843 * iocb to complete. If the iocb command is not
4844 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
4845 * Caller should not free the iocb resources if this function
4846 * returns IOCB_TIMEDOUT.
4847 * The function waits for the iocb completion using an
4848 * non-interruptible wait.
4849 * This function will sleep while waiting for iocb completion.
4850 * So, this function should not be called from any context which
4851 * does not allow sleeping. Due to the same reason, this function
4852 * cannot be called with interrupt disabled.
4853 * This function assumes that the iocb completions occur while
4854 * this function sleep. So, this function cannot be called from
4855 * the thread which process iocb completion for this ring.
4856 * This function clears the iocb_flag of the iocb object before
4857 * issuing the iocb and the iocb completion handler sets this
4858 * flag and wakes this thread when the iocb completes.
4859 * The contents of the response iocb will be copied to prspiocbq
4860 * by the completion handler when the command completes.
4861 * This function returns IOCB_SUCCESS when success.
4862 * This function is called with no lock held.
4863 **/
dea31012005-04-17 16:05:31 -05004864int
James Smart2e0fef82007-06-17 19:56:36 -05004865lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
4866 struct lpfc_sli_ring *pring,
4867 struct lpfc_iocbq *piocb,
4868 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004869 uint32_t timeout)
dea31012005-04-17 16:05:31 -05004870{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08004871 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004872 long timeleft, timeout_req = 0;
4873 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004874 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05004875
4876 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004877 * If the caller has provided a response iocbq buffer, then context2
4878 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05004879 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004880 if (prspiocbq) {
4881 if (piocb->context2)
4882 return IOCB_ERROR;
4883 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05004884 }
4885
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004886 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
4887 piocb->context_un.wait_queue = &done_q;
4888 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05004889
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004890 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4891 creg_val = readl(phba->HCregaddr);
4892 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
4893 writel(creg_val, phba->HCregaddr);
4894 readl(phba->HCregaddr); /* flush */
4895 }
4896
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004897 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
4898 if (retval == IOCB_SUCCESS) {
4899 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004900 timeleft = wait_event_timeout(done_q,
4901 piocb->iocb_flag & LPFC_IO_WAKE,
4902 timeout_req);
dea31012005-04-17 16:05:31 -05004903
James Smart7054a602007-04-25 09:52:34 -04004904 if (piocb->iocb_flag & LPFC_IO_WAKE) {
4905 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004906 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04004907 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004908 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004909 "0338 IOCB wait timeout error - no "
4910 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004911 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04004912 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004913 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004914 "0330 IOCB wake NOT set, "
4915 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004916 timeout, (timeleft / jiffies));
4917 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05004918 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004919 } else {
4920 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04004921 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004922 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004923 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05004924 }
4925
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004926 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4927 creg_val = readl(phba->HCregaddr);
4928 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
4929 writel(creg_val, phba->HCregaddr);
4930 readl(phba->HCregaddr); /* flush */
4931 }
4932
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004933 if (prspiocbq)
4934 piocb->context2 = NULL;
4935
4936 piocb->context_un.wait_queue = NULL;
4937 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05004938 return retval;
4939}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004940
James Smarte59058c2008-08-24 21:49:00 -04004941/**
4942 * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
4943 * @phba: Pointer to HBA context object.
4944 * @pmboxq: Pointer to driver mailbox object.
4945 * @timeout: Timeout in number of seconds.
4946 *
4947 * This function issues the mailbox to firmware and waits for the
4948 * mailbox command to complete. If the mailbox command is not
4949 * completed within timeout seconds, it returns MBX_TIMEOUT.
4950 * The function waits for the mailbox completion using an
4951 * interruptible wait. If the thread is woken up due to a
4952 * signal, MBX_TIMEOUT error is returned to the caller. Caller
4953 * should not free the mailbox resources, if this function returns
4954 * MBX_TIMEOUT.
4955 * This function will sleep while waiting for mailbox completion.
4956 * So, this function should not be called from any context which
4957 * does not allow sleeping. Due to the same reason, this function
4958 * cannot be called with interrupt disabled.
4959 * This function assumes that the mailbox completion occurs while
4960 * this function sleep. So, this function cannot be called from
4961 * the worker thread which processes mailbox completion.
4962 * This function is called in the context of HBA management
4963 * applications.
4964 * This function returns MBX_SUCCESS when successful.
4965 * This function is called with no lock held.
4966 **/
dea31012005-04-17 16:05:31 -05004967int
James Smart2e0fef82007-06-17 19:56:36 -05004968lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05004969 uint32_t timeout)
4970{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08004971 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05004972 int retval;
James Smart858c9f62007-06-17 19:56:39 -05004973 unsigned long flag;
dea31012005-04-17 16:05:31 -05004974
4975 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04004976 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05004977 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05004978
James Smart495a7142008-06-14 22:52:59 -04004979 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05004980 /* setup wake call as IOCB callback */
4981 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
4982 /* setup context field to pass wait_queue pointer to wake function */
4983 pmboxq->context1 = &done_q;
4984
dea31012005-04-17 16:05:31 -05004985 /* now issue the command */
4986 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4987
4988 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04004989 wait_event_interruptible_timeout(done_q,
4990 pmboxq->mbox_flag & LPFC_MBX_WAKE,
4991 timeout * HZ);
4992
James Smart858c9f62007-06-17 19:56:39 -05004993 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05004994 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04004995 /*
4996 * if LPFC_MBX_WAKE flag is set the mailbox is completed
4997 * else do not free the resources.
4998 */
4999 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05005000 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05005001 else {
James Smart7054a602007-04-25 09:52:34 -04005002 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05005003 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5004 }
5005 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005006 }
5007
dea31012005-04-17 16:05:31 -05005008 return retval;
5009}
5010
James Smarte59058c2008-08-24 21:49:00 -04005011/**
5012 * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
5013 * @phba: Pointer to HBA context.
5014 *
5015 * This function is called to cleanup any pending mailbox
5016 * objects in the driver queue before bringing the HBA offline.
5017 * This function is called while resetting the HBA.
5018 * The function is called without any lock held. The function
5019 * takes hbalock to update SLI data structure.
5020 * This function returns 1 when there is an active mailbox
5021 * command pending else returns 0.
5022 **/
James Smartb4c02652006-07-06 15:50:43 -04005023int
5024lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
5025{
James Smart2e0fef82007-06-17 19:56:36 -05005026 struct lpfc_vport *vport = phba->pport;
James Smartb4c02652006-07-06 15:50:43 -04005027 int i = 0;
James Smarted957682007-06-17 19:56:37 -05005028 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04005029
James Smart2e0fef82007-06-17 19:56:36 -05005030 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
James Smartb4c02652006-07-06 15:50:43 -04005031 if (i++ > LPFC_MBOX_TMO * 1000)
5032 return 1;
5033
James Smarted957682007-06-17 19:56:37 -05005034 /*
5035 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
5036 * did finish. This way we won't get the misleading
5037 * "Stray Mailbox Interrupt" message.
5038 */
5039 spin_lock_irq(&phba->hbalock);
5040 ha_copy = phba->work_ha;
5041 phba->work_ha &= ~HA_MBATT;
5042 spin_unlock_irq(&phba->hbalock);
5043
5044 if (ha_copy & HA_MBATT)
5045 if (lpfc_sli_handle_mb_event(phba) == 0)
5046 i = 0;
James Smartb4c02652006-07-06 15:50:43 -04005047
5048 msleep(1);
5049 }
5050
5051 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
5052}
5053
James Smarte59058c2008-08-24 21:49:00 -04005054/**
5055 * lpfc_intr_handler: The interrupt handler of lpfc driver.
5056 * @irq: Interrupt number.
5057 * @dev_id: The device context pointer.
5058 *
5059 * This function is called from the PCI layer when there is
5060 * an event in the HBA which requires driver attention. When
5061 * the PCI slot is in error recovery or the HBA is undergoing
5062 * initialization the interrupt handler will not process the
5063 * interrupt.
5064 * The error attention, link attention and els ring attention
5065 * events are handled by the worker thread. The interrupt
5066 * handler signals the worker thread and returns for these
5067 * events.
5068 * The SCSI ring event and mailbox events are handled in the
5069 * interrupt context.
5070 * This function is called without any lock held. It gets the
5071 * hbalock to access and update SLI data structures.
5072 * This function returns IRQ_HANDLED when interrupt is handled
5073 * else it returns IRQ_NONE.
5074 **/
dea31012005-04-17 16:05:31 -05005075irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01005076lpfc_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05005077{
James Smart2e0fef82007-06-17 19:56:36 -05005078 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05005079 uint32_t ha_copy;
5080 uint32_t work_ha_copy;
5081 unsigned long status;
dea31012005-04-17 16:05:31 -05005082 uint32_t control;
5083
James Smart92d7f7b2007-06-17 19:56:38 -05005084 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05005085 struct lpfc_vport *vport;
5086 struct lpfc_nodelist *ndlp;
5087 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05005088 LPFC_MBOXQ_t *pmb;
5089 int rc;
5090
dea31012005-04-17 16:05:31 -05005091 /*
5092 * Get the driver's phba structure from the dev_id and
5093 * assume the HBA is not interrupting.
5094 */
5095 phba = (struct lpfc_hba *) dev_id;
5096
5097 if (unlikely(!phba))
5098 return IRQ_NONE;
5099
Linas Vepstas8d63f372007-02-14 14:28:36 -06005100 /* If the pci channel is offline, ignore all the interrupts. */
5101 if (unlikely(pci_channel_offline(phba->pcidev)))
5102 return IRQ_NONE;
5103
dea31012005-04-17 16:05:31 -05005104 phba->sli.slistat.sli_intr++;
5105
5106 /*
5107 * Call the HBA to see if it is interrupting. If not, don't claim
5108 * the interrupt
5109 */
5110
5111 /* Ignore all interrupts during initialization. */
James Smart2e0fef82007-06-17 19:56:36 -05005112 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005113 return IRQ_NONE;
5114
5115 /*
5116 * Read host attention register to determine interrupt source
5117 * Clear Attention Sources, except Error Attention (to
5118 * preserve status) and Link Attention
5119 */
James Smart2e0fef82007-06-17 19:56:36 -05005120 spin_lock(&phba->hbalock);
James Smart34b02dc2008-08-24 21:49:55 -04005121 if (phba->sli3_options & LPFC_SLI3_INB_ENABLED &&
5122 (phba->inb_last_counter != *phba->inb_counter)) {
5123 phba->inb_last_counter = *phba->inb_counter;
5124 ha_copy = le32_to_cpu(*phba->inb_ha_copy);
5125 } else
5126 ha_copy = readl(phba->HAregaddr);
5127 if (unlikely(!ha_copy)) {
5128 spin_unlock(&phba->hbalock);
5129 return IRQ_NONE;
5130 }
James Smartebdbe652007-04-25 09:53:15 -04005131 /* If somebody is waiting to handle an eratt don't process it
5132 * here. The brdkill function will do this.
5133 */
James Smart2e0fef82007-06-17 19:56:36 -05005134 if (phba->link_flag & LS_IGNORE_ERATT)
James Smartebdbe652007-04-25 09:53:15 -04005135 ha_copy &= ~HA_ERATT;
dea31012005-04-17 16:05:31 -05005136 writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
5137 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05005138 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005139
dea31012005-04-17 16:05:31 -05005140 work_ha_copy = ha_copy & phba->work_ha_mask;
5141
5142 if (unlikely(work_ha_copy)) {
5143 if (work_ha_copy & HA_LATT) {
5144 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
5145 /*
5146 * Turn off Link Attention interrupts
5147 * until CLEAR_LA done
5148 */
James Smart2e0fef82007-06-17 19:56:36 -05005149 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005150 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
5151 control = readl(phba->HCregaddr);
5152 control &= ~HC_LAINT_ENA;
5153 writel(control, phba->HCregaddr);
5154 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05005155 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005156 }
5157 else
5158 work_ha_copy &= ~HA_LATT;
5159 }
5160
5161 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05005162 /*
5163 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
5164 * the only slow ring.
5165 */
5166 status = (work_ha_copy &
5167 (HA_RXMASK << (4*LPFC_ELS_RING)));
5168 status >>= (4*LPFC_ELS_RING);
5169 if (status & HA_RXMASK) {
5170 spin_lock(&phba->hbalock);
5171 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04005172
5173 lpfc_debugfs_slow_ring_trc(phba,
5174 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
5175 control, status,
5176 (uint32_t)phba->sli.slistat.sli_intr);
5177
James Smart858c9f62007-06-17 19:56:39 -05005178 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04005179 lpfc_debugfs_slow_ring_trc(phba,
5180 "ISR Disable ring:"
5181 "pwork:x%x hawork:x%x wait:x%x",
5182 phba->work_ha, work_ha_copy,
5183 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005184 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005185
James Smart858c9f62007-06-17 19:56:39 -05005186 control &=
5187 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05005188 writel(control, phba->HCregaddr);
5189 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05005190 }
James Smarta58cbd52007-08-02 11:09:43 -04005191 else {
5192 lpfc_debugfs_slow_ring_trc(phba,
5193 "ISR slow ring: pwork:"
5194 "x%x hawork:x%x wait:x%x",
5195 phba->work_ha, work_ha_copy,
5196 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005197 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005198 }
James Smart858c9f62007-06-17 19:56:39 -05005199 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005200 }
5201 }
5202
5203 if (work_ha_copy & HA_ERATT) {
dea31012005-04-17 16:05:31 -05005204 /*
5205 * There was a link/board error. Read the
5206 * status register to retrieve the error event
5207 * and process it.
5208 */
5209 phba->sli.slistat.err_attn_event++;
5210 /* Save status info */
5211 phba->work_hs = readl(phba->HSregaddr);
5212 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
5213 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
5214
5215 /* Clear Chip error bit */
5216 writel(HA_ERATT, phba->HAregaddr);
5217 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05005218 phba->pport->stopped = 1;
dea31012005-04-17 16:05:31 -05005219 }
5220
James Smart97eab632008-04-07 10:16:05 -04005221 spin_lock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005222 if ((work_ha_copy & HA_MBATT) &&
5223 (phba->sli.mbox_active)) {
5224 pmb = phba->sli.mbox_active;
5225 pmbox = &pmb->mb;
James Smart34b02dc2008-08-24 21:49:55 -04005226 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05005227 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005228
5229 /* First check out the status word */
5230 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
5231 if (pmbox->mbxOwner != OWN_HOST) {
James Smart97eab632008-04-07 10:16:05 -04005232 spin_unlock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005233 /*
5234 * Stray Mailbox Interrupt, mbxCommand <cmd>
5235 * mbxStatus <status>
5236 */
James Smart09372822008-01-11 01:52:54 -05005237 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05005238 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005239 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05005240 "Interrupt mbxCommand x%x "
5241 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005242 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05005243 pmbox->mbxCommand,
5244 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05005245 /* clear mailbox attention bit */
5246 work_ha_copy &= ~HA_MBATT;
5247 } else {
James Smart97eab632008-04-07 10:16:05 -04005248 phba->sli.mbox_active = NULL;
5249 spin_unlock(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05005250 phba->last_completion_time = jiffies;
5251 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05005252 if (pmb->mbox_cmpl) {
5253 lpfc_sli_pcimem_bcopy(mbox, pmbox,
5254 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05005255 }
James Smart09372822008-01-11 01:52:54 -05005256 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
5257 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
5258
5259 lpfc_debugfs_disc_trc(vport,
5260 LPFC_DISC_TRC_MBOX_VPORT,
5261 "MBOX dflt rpi: : "
5262 "status:x%x rpi:x%x",
5263 (uint32_t)pmbox->mbxStatus,
5264 pmbox->un.varWords[0], 0);
5265
5266 if (!pmbox->mbxStatus) {
5267 mp = (struct lpfc_dmabuf *)
5268 (pmb->context1);
5269 ndlp = (struct lpfc_nodelist *)
5270 pmb->context2;
5271
5272 /* Reg_LOGIN of dflt RPI was
5273 * successful. new lets get
5274 * rid of the RPI using the
5275 * same mbox buffer.
5276 */
5277 lpfc_unreg_login(phba,
5278 vport->vpi,
5279 pmbox->un.varWords[0],
5280 pmb);
5281 pmb->mbox_cmpl =
5282 lpfc_mbx_cmpl_dflt_rpi;
5283 pmb->context1 = mp;
5284 pmb->context2 = ndlp;
5285 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04005286 rc = lpfc_sli_issue_mbox(phba,
5287 pmb,
5288 MBX_NOWAIT);
5289 if (rc != MBX_BUSY)
5290 lpfc_printf_log(phba,
5291 KERN_ERR,
5292 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04005293 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04005294 "been MBX_BUSY");
James Smart09372822008-01-11 01:52:54 -05005295 goto send_current_mbox;
5296 }
5297 }
5298 spin_lock(&phba->pport->work_port_lock);
5299 phba->pport->work_port_events &=
5300 ~WORKER_MBOX_TMO;
5301 spin_unlock(&phba->pport->work_port_lock);
5302 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05005303 }
James Smart97eab632008-04-07 10:16:05 -04005304 } else
5305 spin_unlock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005306 if ((work_ha_copy & HA_MBATT) &&
5307 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05005308send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05005309 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04005310 do {
5311 rc = lpfc_sli_issue_mbox(phba, NULL,
5312 MBX_NOWAIT);
5313 } while (rc == MBX_NOT_FINISHED);
5314 if (rc != MBX_SUCCESS)
5315 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
5316 LOG_SLI, "0349 rc should be "
5317 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05005318 }
5319
James Smart2e0fef82007-06-17 19:56:36 -05005320 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005321 phba->work_ha |= work_ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05005322 spin_unlock(&phba->hbalock);
James Smart5e9d9b82008-06-14 22:52:53 -04005323 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005324 }
5325
5326 ha_copy &= ~(phba->work_ha_mask);
5327
5328 /*
5329 * Process all events on FCP ring. Take the optimized path for
5330 * FCP IO. Any other IO is slow path and is handled by
5331 * the worker thread.
5332 */
5333 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
5334 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05005335 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05005336 lpfc_sli_handle_fast_ring_event(phba,
5337 &phba->sli.ring[LPFC_FCP_RING],
5338 status);
James Smarta4bc3372006-12-02 13:34:16 -05005339
5340 if (phba->cfg_multi_ring_support == 2) {
5341 /*
5342 * Process all events on extra ring. Take the optimized path
5343 * for extra ring IO. Any other IO is slow path and is handled
5344 * by the worker thread.
5345 */
5346 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
5347 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05005348 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05005349 lpfc_sli_handle_fast_ring_event(phba,
5350 &phba->sli.ring[LPFC_EXTRA_RING],
5351 status);
5352 }
5353 }
dea31012005-04-17 16:05:31 -05005354 return IRQ_HANDLED;
5355
5356} /* lpfc_intr_handler */