blob: 2cca39e9b93d621b9241ecdf10c8875cfd55f742 [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:
James Smart84774a42008-08-24 21:50:06 -04001053 case MBX_PORT_CAPABILITIES:
1054 case MBX_PORT_IOV_CONTROL:
dea31012005-04-17 16:05:31 -05001055 ret = mbxCommand;
1056 break;
1057 default:
1058 ret = MBX_SHUTDOWN;
1059 break;
1060 }
James Smart2e0fef82007-06-17 19:56:36 -05001061 return ret;
dea31012005-04-17 16:05:31 -05001062}
James Smarte59058c2008-08-24 21:49:00 -04001063
1064/**
1065 * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
1066 * lpfc_sli_issue_mbox_wait.
1067 * @phba: Pointer to HBA context object.
1068 * @pmboxq: Pointer to mailbox command.
1069 *
1070 * This is completion handler function for mailbox commands issued from
1071 * lpfc_sli_issue_mbox_wait function. This function is called by the
1072 * mailbox event handler function with no lock held. This function
1073 * will wake up thread waiting on the wait queue pointed by context1
1074 * of the mailbox.
1075 **/
dea31012005-04-17 16:05:31 -05001076static void
James Smart2e0fef82007-06-17 19:56:36 -05001077lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001078{
1079 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001080 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001081
1082 /*
1083 * If pdone_q is empty, the driver thread gave up waiting and
1084 * continued running.
1085 */
James Smart7054a602007-04-25 09:52:34 -04001086 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001087 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001088 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1089 if (pdone_q)
1090 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001091 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001092 return;
1093}
1094
James Smarte59058c2008-08-24 21:49:00 -04001095
1096/**
1097 * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
1098 * @phba: Pointer to HBA context object.
1099 * @pmb: Pointer to mailbox object.
1100 *
1101 * This function is the default mailbox completion handler. It
1102 * frees the memory resources associated with the completed mailbox
1103 * command. If the completed command is a REG_LOGIN mailbox command,
1104 * this function will issue a UREG_LOGIN to re-claim the RPI.
1105 **/
dea31012005-04-17 16:05:31 -05001106void
James Smart2e0fef82007-06-17 19:56:36 -05001107lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001108{
1109 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -04001110 uint16_t rpi;
1111 int rc;
1112
dea31012005-04-17 16:05:31 -05001113 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001114
dea31012005-04-17 16:05:31 -05001115 if (mp) {
1116 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1117 kfree(mp);
1118 }
James Smart7054a602007-04-25 09:52:34 -04001119
1120 /*
1121 * If a REG_LOGIN succeeded after node is destroyed or node
1122 * is in re-discovery driver need to cleanup the RPI.
1123 */
James Smart2e0fef82007-06-17 19:56:36 -05001124 if (!(phba->pport->load_flag & FC_UNLOADING) &&
1125 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1126 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -04001127
1128 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -05001129 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1130 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001131 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1132 if (rc != MBX_NOT_FINISHED)
1133 return;
1134 }
1135
James Smart2e0fef82007-06-17 19:56:36 -05001136 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001137 return;
1138}
1139
James Smarte59058c2008-08-24 21:49:00 -04001140/**
1141 * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
1142 * @phba: Pointer to HBA context object.
1143 *
1144 * This function is called with no lock held. This function processes all
1145 * the completed mailbox commands and gives it to upper layers. The interrupt
1146 * service routine processes mailbox completion interrupt and adds completed
1147 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1148 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1149 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1150 * function returns the mailbox commands to the upper layer by calling the
1151 * completion handler function of each mailbox.
1152 **/
dea31012005-04-17 16:05:31 -05001153int
James Smart2e0fef82007-06-17 19:56:36 -05001154lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001155{
James Smart92d7f7b2007-06-17 19:56:38 -05001156 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001157 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001158 int rc;
1159 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001160
1161 phba->sli.slistat.mbox_event++;
1162
James Smart92d7f7b2007-06-17 19:56:38 -05001163 /* Get all completed mailboxe buffers into the cmplq */
1164 spin_lock_irq(&phba->hbalock);
1165 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1166 spin_unlock_irq(&phba->hbalock);
1167
dea31012005-04-17 16:05:31 -05001168 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001169 do {
1170 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1171 if (pmb == NULL)
1172 break;
1173
dea31012005-04-17 16:05:31 -05001174 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -05001175
James Smart858c9f62007-06-17 19:56:39 -05001176 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1177 if (pmb->vport) {
1178 lpfc_debugfs_disc_trc(pmb->vport,
1179 LPFC_DISC_TRC_MBOX_VPORT,
1180 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1181 (uint32_t)pmbox->mbxCommand,
1182 pmbox->un.varWords[0],
1183 pmbox->un.varWords[1]);
1184 }
1185 else {
1186 lpfc_debugfs_disc_trc(phba->pport,
1187 LPFC_DISC_TRC_MBOX,
1188 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1189 (uint32_t)pmbox->mbxCommand,
1190 pmbox->un.varWords[0],
1191 pmbox->un.varWords[1]);
1192 }
1193 }
1194
dea31012005-04-17 16:05:31 -05001195 /*
1196 * It is a fatal error if unknown mbox command completion.
1197 */
1198 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1199 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001200 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001202 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -05001203 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001204 pmb->vport ? pmb->vport->vpi : 0,
1205 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -05001206 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001207 phba->work_hs = HS_FFER3;
1208 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001209 continue;
dea31012005-04-17 16:05:31 -05001210 }
1211
dea31012005-04-17 16:05:31 -05001212 if (pmbox->mbxStatus) {
1213 phba->sli.slistat.mbox_stat_err++;
1214 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1215 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001216 lpfc_printf_log(phba, KERN_INFO,
1217 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001218 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001219 "error - RETRYing Data: x%x "
1220 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001221 pmb->vport ? pmb->vport->vpi :0,
1222 pmbox->mbxCommand,
1223 pmbox->mbxStatus,
1224 pmbox->un.varWords[0],
1225 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001226 pmbox->mbxStatus = 0;
1227 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -05001228 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001229 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001230 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001231 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1232 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -05001233 continue;
dea31012005-04-17 16:05:31 -05001234 }
1235 }
1236
1237 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001238 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001239 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -05001240 "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 -05001241 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001242 pmbox->mbxCommand,
1243 pmb->mbox_cmpl,
1244 *((uint32_t *) pmbox),
1245 pmbox->un.varWords[0],
1246 pmbox->un.varWords[1],
1247 pmbox->un.varWords[2],
1248 pmbox->un.varWords[3],
1249 pmbox->un.varWords[4],
1250 pmbox->un.varWords[5],
1251 pmbox->un.varWords[6],
1252 pmbox->un.varWords[7]);
1253
James Smart92d7f7b2007-06-17 19:56:38 -05001254 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001255 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001256 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001257 return 0;
dea31012005-04-17 16:05:31 -05001258}
James Smart92d7f7b2007-06-17 19:56:38 -05001259
James Smarte59058c2008-08-24 21:49:00 -04001260/**
1261 * lpfc_sli_replace_hbqbuff: Replace the HBQ buffer with a new buffer.
1262 * @phba: Pointer to HBA context object.
1263 * @tag: Tag for the HBQ buffer.
1264 *
1265 * This function is called from unsolicited event handler code path to get the
1266 * HBQ buffer associated with an unsolicited iocb. This function is called with
1267 * no lock held. It returns the buffer associated with the given tag and posts
James Smartd7c255b2008-08-24 21:50:00 -04001268 * another buffer to the firmware. Note that the new buffer must be allocated
1269 * before taking the hbalock and that the hba lock must be held until it is
1270 * finished with the hbq entry swap.
James Smarte59058c2008-08-24 21:49:00 -04001271 **/
James Smart92d7f7b2007-06-17 19:56:38 -05001272static struct lpfc_dmabuf *
1273lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
1274{
1275 struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
James Smart51ef4c22007-08-02 11:10:31 -04001276 uint32_t hbqno;
1277 void *virt; /* virtual address ptr */
1278 dma_addr_t phys; /* mapped address */
James Smart3163f722008-02-08 18:50:25 -05001279 unsigned long flags;
1280
James Smartd7c255b2008-08-24 21:50:00 -04001281 hbqno = tag >> 16;
1282 new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
James Smart3163f722008-02-08 18:50:25 -05001283 /* Check whether HBQ is still in use */
1284 spin_lock_irqsave(&phba->hbalock, flags);
1285 if (!phba->hbq_in_use) {
James Smartd7c255b2008-08-24 21:50:00 -04001286 if (new_hbq_entry)
1287 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1288 new_hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001289 spin_unlock_irqrestore(&phba->hbalock, flags);
1290 return NULL;
1291 }
James Smart92d7f7b2007-06-17 19:56:38 -05001292
1293 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
James Smart3163f722008-02-08 18:50:25 -05001294 if (hbq_entry == NULL) {
James Smartd7c255b2008-08-24 21:50:00 -04001295 if (new_hbq_entry)
1296 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1297 new_hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001298 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05001299 return NULL;
James Smart3163f722008-02-08 18:50:25 -05001300 }
James Smart92d7f7b2007-06-17 19:56:38 -05001301 list_del(&hbq_entry->dbuf.list);
James Smart51ef4c22007-08-02 11:10:31 -04001302
James Smart3163f722008-02-08 18:50:25 -05001303 if (new_hbq_entry == NULL) {
1304 list_add_tail(&hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1305 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05001306 return &hbq_entry->dbuf;
James Smart3163f722008-02-08 18:50:25 -05001307 }
James Smart92d7f7b2007-06-17 19:56:38 -05001308 new_hbq_entry->tag = -1;
James Smart51ef4c22007-08-02 11:10:31 -04001309 phys = new_hbq_entry->dbuf.phys;
1310 virt = new_hbq_entry->dbuf.virt;
1311 new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
1312 new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
1313 hbq_entry->dbuf.phys = phys;
1314 hbq_entry->dbuf.virt = virt;
James Smart92d7f7b2007-06-17 19:56:38 -05001315 lpfc_sli_free_hbq(phba, hbq_entry);
James Smart3163f722008-02-08 18:50:25 -05001316 list_add_tail(&new_hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1317 spin_unlock_irqrestore(&phba->hbalock, flags);
1318
James Smart92d7f7b2007-06-17 19:56:38 -05001319 return &new_hbq_entry->dbuf;
1320}
1321
James Smarte59058c2008-08-24 21:49:00 -04001322/**
1323 * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
1324 * @phba: Pointer to HBA context object.
1325 * @pring: Pointer to driver SLI ring object.
1326 * @tag: buffer tag.
1327 *
1328 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1329 * is set in the tag the buffer is posted for a particular exchange,
1330 * the function will return the buffer without replacing the buffer.
1331 * If the buffer is for unsolicited ELS or CT traffic, this function
1332 * returns the buffer and also posts another buffer to the firmware.
1333 **/
James Smart76bb24e2007-10-27 13:38:00 -04001334static struct lpfc_dmabuf *
1335lpfc_sli_get_buff(struct lpfc_hba *phba,
1336 struct lpfc_sli_ring *pring,
1337 uint32_t tag)
1338{
1339 if (tag & QUE_BUFTAG_BIT)
1340 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1341 else
1342 return lpfc_sli_replace_hbqbuff(phba, tag);
1343}
James Smart57127f12007-10-27 13:37:05 -04001344
James Smarte59058c2008-08-24 21:49:00 -04001345
1346/**
1347 * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
1348 * @phba: Pointer to HBA context object.
1349 * @pring: Pointer to driver SLI ring object.
1350 * @saveq: Pointer to the unsolicited iocb.
1351 *
1352 * This function is called with no lock held by the ring event handler
1353 * when there is an unsolicited iocb posted to the response ring by the
1354 * firmware. This function gets the buffer associated with the iocbs
1355 * and calls the event handler for the ring. This function handles both
1356 * qring buffers and hbq buffers.
1357 * When the function returns 1 the caller can free the iocb object otherwise
1358 * upper layer functions will free the iocb objects.
1359 **/
dea31012005-04-17 16:05:31 -05001360static int
1361lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1362 struct lpfc_iocbq *saveq)
1363{
1364 IOCB_t * irsp;
1365 WORD5 * w5p;
1366 uint32_t Rctl, Type;
1367 uint32_t match, i;
James Smart76bb24e2007-10-27 13:38:00 -04001368 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001369 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001370
1371 match = 0;
1372 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001373
James Smart9c2face2008-01-11 01:53:18 -05001374 if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
1375 return 1;
James Smart57127f12007-10-27 13:37:05 -04001376 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1377 if (pring->lpfc_sli_rcv_async_status)
1378 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1379 else
1380 lpfc_printf_log(phba,
1381 KERN_WARNING,
1382 LOG_SLI,
1383 "0316 Ring %d handler: unexpected "
1384 "ASYNC_STATUS iocb received evt_code "
1385 "0x%x\n",
1386 pring->ringno,
1387 irsp->un.asyncstat.evt_code);
1388 return 1;
1389 }
1390
James Smart3163f722008-02-08 18:50:25 -05001391 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1392 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1393 if (irsp->ulpBdeCount > 0) {
1394 dmzbuf = lpfc_sli_get_buff(phba, pring,
1395 irsp->un.ulpWord[3]);
1396 lpfc_in_buf_free(phba, dmzbuf);
1397 }
1398
1399 if (irsp->ulpBdeCount > 1) {
1400 dmzbuf = lpfc_sli_get_buff(phba, pring,
1401 irsp->unsli3.sli3Words[3]);
1402 lpfc_in_buf_free(phba, dmzbuf);
1403 }
1404
1405 if (irsp->ulpBdeCount > 2) {
1406 dmzbuf = lpfc_sli_get_buff(phba, pring,
1407 irsp->unsli3.sli3Words[7]);
1408 lpfc_in_buf_free(phba, dmzbuf);
1409 }
1410
1411 return 1;
1412 }
1413
James Smart92d7f7b2007-06-17 19:56:38 -05001414 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001415 if (irsp->ulpBdeCount != 0) {
1416 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001417 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001418 if (!saveq->context2)
1419 lpfc_printf_log(phba,
1420 KERN_ERR,
1421 LOG_SLI,
1422 "0341 Ring %d Cannot find buffer for "
1423 "an unsolicited iocb. tag 0x%x\n",
1424 pring->ringno,
1425 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001426 }
1427 if (irsp->ulpBdeCount == 2) {
1428 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04001429 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04001430 if (!saveq->context3)
1431 lpfc_printf_log(phba,
1432 KERN_ERR,
1433 LOG_SLI,
1434 "0342 Ring %d Cannot find buffer for an"
1435 " unsolicited iocb. tag 0x%x\n",
1436 pring->ringno,
1437 irsp->unsli3.sli3Words[7]);
1438 }
1439 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04001440 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04001441 if (irsp->ulpBdeCount != 0) {
1442 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1443 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05001444 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04001445 lpfc_printf_log(phba,
1446 KERN_ERR,
1447 LOG_SLI,
1448 "0343 Ring %d Cannot find "
1449 "buffer for an unsolicited iocb"
1450 ". tag 0x%x\n", pring->ringno,
1451 irsp->un.ulpWord[3]);
1452 }
1453 if (irsp->ulpBdeCount == 2) {
1454 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1455 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05001456 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04001457 lpfc_printf_log(phba,
1458 KERN_ERR,
1459 LOG_SLI,
1460 "0344 Ring %d Cannot find "
1461 "buffer for an unsolicited "
1462 "iocb. tag 0x%x\n",
1463 pring->ringno,
1464 irsp->unsli3.sli3Words[7]);
1465 }
1466 }
James Smart92d7f7b2007-06-17 19:56:38 -05001467 }
James Smart9c2face2008-01-11 01:53:18 -05001468 if (irsp->ulpBdeCount != 0 &&
1469 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1470 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1471 int found = 0;
1472
1473 /* search continue save q for same XRI */
1474 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1475 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1476 list_add_tail(&saveq->list, &iocbq->list);
1477 found = 1;
1478 break;
1479 }
1480 }
1481 if (!found)
1482 list_add_tail(&saveq->clist,
1483 &pring->iocb_continue_saveq);
1484 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1485 list_del_init(&iocbq->clist);
1486 saveq = iocbq;
1487 irsp = &(saveq->iocb);
1488 } else
1489 return 0;
1490 }
1491 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1492 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1493 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1494 Rctl = FC_ELS_REQ;
1495 Type = FC_ELS_DATA;
1496 } else {
1497 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1498 Rctl = w5p->hcsw.Rctl;
1499 Type = w5p->hcsw.Type;
1500
1501 /* Firmware Workaround */
1502 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1503 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1504 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1505 Rctl = FC_ELS_REQ;
1506 Type = FC_ELS_DATA;
1507 w5p->hcsw.Rctl = Rctl;
1508 w5p->hcsw.Type = Type;
1509 }
1510 }
James Smart92d7f7b2007-06-17 19:56:38 -05001511
dea31012005-04-17 16:05:31 -05001512 /* unSolicited Responses */
1513 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001514 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1515 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1516 saveq);
dea31012005-04-17 16:05:31 -05001517 match = 1;
1518 } else {
1519 /* We must search, based on rctl / type
1520 for the right routine */
James Smart9c2face2008-01-11 01:53:18 -05001521 for (i = 0; i < pring->num_mask; i++) {
1522 if ((pring->prt[i].rctl == Rctl)
1523 && (pring->prt[i].type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05001524 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1525 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1526 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -05001527 match = 1;
1528 break;
1529 }
1530 }
1531 }
1532 if (match == 0) {
1533 /* Unexpected Rctl / Type received */
1534 /* Ring <ringno> handler: unexpected
1535 Rctl <Rctl> Type <Type> received */
James Smart92d7f7b2007-06-17 19:56:38 -05001536 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001537 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001538 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001539 pring->ringno, Rctl, Type);
dea31012005-04-17 16:05:31 -05001540 }
James Smart92d7f7b2007-06-17 19:56:38 -05001541 return 1;
dea31012005-04-17 16:05:31 -05001542}
1543
James Smarte59058c2008-08-24 21:49:00 -04001544/**
1545 * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
1546 * @phba: Pointer to HBA context object.
1547 * @pring: Pointer to driver SLI ring object.
1548 * @prspiocb: Pointer to response iocb object.
1549 *
1550 * This function looks up the iocb_lookup table to get the command iocb
1551 * corresponding to the given response iocb using the iotag of the
1552 * response iocb. This function is called with the hbalock held.
1553 * This function returns the command iocb object if it finds the command
1554 * iocb else returns NULL.
1555 **/
dea31012005-04-17 16:05:31 -05001556static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001557lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1558 struct lpfc_sli_ring *pring,
1559 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001560{
dea31012005-04-17 16:05:31 -05001561 struct lpfc_iocbq *cmd_iocb = NULL;
1562 uint16_t iotag;
1563
James Bottomley604a3e32005-10-29 10:28:33 -05001564 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001565
James Bottomley604a3e32005-10-29 10:28:33 -05001566 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1567 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001568 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001569 pring->txcmplq_cnt--;
1570 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001571 }
1572
dea31012005-04-17 16:05:31 -05001573 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001574 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001575 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001576 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001577 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001578 return NULL;
1579}
1580
James Smarte59058c2008-08-24 21:49:00 -04001581/**
1582 * lpfc_sli_process_sol_iocb: process solicited iocb completion.
1583 * @phba: Pointer to HBA context object.
1584 * @pring: Pointer to driver SLI ring object.
1585 * @saveq: Pointer to the response iocb to be processed.
1586 *
1587 * This function is called by the ring event handler for non-fcp
1588 * rings when there is a new response iocb in the response ring.
1589 * The caller is not required to hold any locks. This function
1590 * gets the command iocb associated with the response iocb and
1591 * calls the completion handler for the command iocb. If there
1592 * is no completion handler, the function will free the resources
1593 * associated with command iocb. If the response iocb is for
1594 * an already aborted command iocb, the status of the completion
1595 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1596 * This function always returns 1.
1597 **/
dea31012005-04-17 16:05:31 -05001598static int
James Smart2e0fef82007-06-17 19:56:36 -05001599lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001600 struct lpfc_iocbq *saveq)
1601{
James Smart2e0fef82007-06-17 19:56:36 -05001602 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001603 int rc = 1;
1604 unsigned long iflag;
1605
1606 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001607 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001608 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001609 spin_unlock_irqrestore(&phba->hbalock, iflag);
1610
dea31012005-04-17 16:05:31 -05001611 if (cmdiocbp) {
1612 if (cmdiocbp->iocb_cmpl) {
1613 /*
1614 * Post all ELS completions to the worker thread.
1615 * All other are passed to the completion callback.
1616 */
1617 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001618 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1619 cmdiocbp->iocb_flag &=
1620 ~LPFC_DRIVER_ABORTED;
1621 saveq->iocb.ulpStatus =
1622 IOSTAT_LOCAL_REJECT;
1623 saveq->iocb.un.ulpWord[4] =
1624 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05001625
1626 /* Firmware could still be in progress
1627 * of DMAing payload, so don't free data
1628 * buffer till after a hbeat.
1629 */
1630 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04001631 }
dea31012005-04-17 16:05:31 -05001632 }
James Smart2e0fef82007-06-17 19:56:36 -05001633 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001634 } else
1635 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001636 } else {
1637 /*
1638 * Unknown initiating command based on the response iotag.
1639 * This could be the case on the ELS ring because of
1640 * lpfc_els_abort().
1641 */
1642 if (pring->ringno != LPFC_ELS_RING) {
1643 /*
1644 * Ring <ringno> handler: unexpected completion IoTag
1645 * <IoTag>
1646 */
James Smarte8b62012007-08-02 11:10:09 -04001647 lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1648 "0322 Ring %d handler: "
1649 "unexpected completion IoTag x%x "
1650 "Data: x%x x%x x%x x%x\n",
1651 pring->ringno,
1652 saveq->iocb.ulpIoTag,
1653 saveq->iocb.ulpStatus,
1654 saveq->iocb.un.ulpWord[4],
1655 saveq->iocb.ulpCommand,
1656 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001657 }
1658 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001659
dea31012005-04-17 16:05:31 -05001660 return rc;
1661}
1662
James Smarte59058c2008-08-24 21:49:00 -04001663/**
1664 * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
1665 * @phba: Pointer to HBA context object.
1666 * @pring: Pointer to driver SLI ring object.
1667 *
1668 * This function is called from the iocb ring event handlers when
1669 * put pointer is ahead of the get pointer for a ring. This function signal
1670 * an error attention condition to the worker thread and the worker
1671 * thread will transition the HBA to offline state.
1672 **/
James Smart2e0fef82007-06-17 19:56:36 -05001673static void
1674lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001675{
James Smart34b02dc2008-08-24 21:49:55 -04001676 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001677 /*
1678 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1679 * rsp ring <portRspMax>
1680 */
1681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001682 "0312 Ring %d handler: portRspPut %d "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001683 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001684 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001685 pring->numRiocb);
1686
James Smart2e0fef82007-06-17 19:56:36 -05001687 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001688
1689 /*
1690 * All error attention handlers are posted to
1691 * worker thread
1692 */
1693 phba->work_ha |= HA_ERATT;
1694 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001695
James Smart5e9d9b82008-06-14 22:52:53 -04001696 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001697
1698 return;
1699}
1700
James Smarte59058c2008-08-24 21:49:00 -04001701/**
James Smart93996272008-08-24 21:50:30 -04001702 * lpfc_poll_eratt: Error attention polling timer timeout handler.
1703 * @ptr: Pointer to address of HBA context object.
1704 *
1705 * This function is invoked by the Error Attention polling timer when the
1706 * timer times out. It will check the SLI Error Attention register for
1707 * possible attention events. If so, it will post an Error Attention event
1708 * and wake up worker thread to process it. Otherwise, it will set up the
1709 * Error Attention polling timer for the next poll.
1710 **/
1711void lpfc_poll_eratt(unsigned long ptr)
1712{
1713 struct lpfc_hba *phba;
1714 uint32_t eratt = 0;
1715
1716 phba = (struct lpfc_hba *)ptr;
1717
1718 /* Check chip HA register for error event */
1719 eratt = lpfc_sli_check_eratt(phba);
1720
1721 if (eratt)
1722 /* Tell the worker thread there is work to do */
1723 lpfc_worker_wake_up(phba);
1724 else
1725 /* Restart the timer for next eratt poll */
1726 mod_timer(&phba->eratt_poll, jiffies +
1727 HZ * LPFC_ERATT_POLL_INTERVAL);
1728 return;
1729}
1730
1731/**
James Smarte59058c2008-08-24 21:49:00 -04001732 * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
1733 * @phba: Pointer to HBA context object.
1734 *
1735 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1736 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1737 * is enabled.
1738 *
1739 * The caller does not hold any lock.
1740 * The function processes each response iocb in the response ring until it
1741 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1742 * LE bit set. The function will call the completion handler of the command iocb
1743 * if the response iocb indicates a completion for a command iocb or it is
1744 * an abort completion.
1745 **/
James Smart2e0fef82007-06-17 19:56:36 -05001746void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001747{
James Smart2e0fef82007-06-17 19:56:36 -05001748 struct lpfc_sli *psli = &phba->sli;
1749 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001750 IOCB_t *irsp = NULL;
1751 IOCB_t *entry = NULL;
1752 struct lpfc_iocbq *cmdiocbq = NULL;
1753 struct lpfc_iocbq rspiocbq;
James Smart34b02dc2008-08-24 21:49:55 -04001754 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001755 uint32_t status;
1756 uint32_t portRspPut, portRspMax;
1757 int type;
1758 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001759 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001760 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001761
1762 pring->stats.iocb_event++;
1763
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001764 /*
1765 * The next available response entry should never exceed the maximum
1766 * entries. If it does, treat it as an adapter hardware error.
1767 */
1768 portRspMax = pring->numRiocb;
1769 portRspPut = le32_to_cpu(pgp->rspPutInx);
1770 if (unlikely(portRspPut >= portRspMax)) {
1771 lpfc_sli_rsp_pointers_error(phba, pring);
1772 return;
1773 }
1774
1775 rmb();
1776 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001777 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001778 if (++pring->rspidx >= portRspMax)
1779 pring->rspidx = 0;
1780
1781 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1782 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001783 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001784 irsp = &rspiocbq.iocb;
1785 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1786 pring->stats.iocb_rsp++;
1787 rsp_cmpl++;
1788
1789 if (unlikely(irsp->ulpStatus)) {
1790 /* Rsp ring <ringno> error: IOCB */
1791 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001792 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001793 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001794 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001795 irsp->un.ulpWord[0],
1796 irsp->un.ulpWord[1],
1797 irsp->un.ulpWord[2],
1798 irsp->un.ulpWord[3],
1799 irsp->un.ulpWord[4],
1800 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001801 *(uint32_t *)&irsp->un1,
1802 *((uint32_t *)&irsp->un1 + 1));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001803 }
1804
1805 switch (type) {
1806 case LPFC_ABORT_IOCB:
1807 case LPFC_SOL_IOCB:
1808 /*
1809 * Idle exchange closed via ABTS from port. No iocb
1810 * resources need to be recovered.
1811 */
1812 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001813 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001814 "0314 IOCB cmd 0x%x "
1815 "processed. Skipping "
1816 "completion",
James Smartdca94792006-08-01 07:34:08 -04001817 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001818 break;
1819 }
1820
James Smart2e0fef82007-06-17 19:56:36 -05001821 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001822 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1823 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001824 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001825 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1826 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1827 &rspiocbq);
1828 }
1829 break;
1830 default:
1831 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1832 char adaptermsg[LPFC_MAX_ADPTMSG];
1833 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1834 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1835 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07001836 dev_warn(&((phba->pcidev)->dev),
1837 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001838 phba->brd_no, adaptermsg);
1839 } else {
1840 /* Unknown IOCB command */
1841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001842 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001843 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001844 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001845 irsp->ulpStatus,
1846 irsp->ulpIoTag,
1847 irsp->ulpContext);
1848 }
1849 break;
1850 }
1851
1852 /*
1853 * The response IOCB has been processed. Update the ring
1854 * pointer in SLIM. If the port response put pointer has not
1855 * been updated, sync the pgp->rspPutInx and fetch the new port
1856 * response put pointer.
1857 */
James Smarted957682007-06-17 19:56:37 -05001858 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001859
1860 if (pring->rspidx == portRspPut)
1861 portRspPut = le32_to_cpu(pgp->rspPutInx);
1862 }
1863
1864 ha_copy = readl(phba->HAregaddr);
1865 ha_copy >>= (LPFC_FCP_RING * 4);
1866
1867 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05001868 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001869 pring->stats.iocb_rsp_full++;
1870 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1871 writel(status, phba->CAregaddr);
1872 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05001873 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001874 }
1875 if ((ha_copy & HA_R0CE_RSP) &&
1876 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05001877 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001878 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1879 pring->stats.iocb_cmd_empty++;
1880
1881 /* Force update of the local copy of cmdGetInx */
1882 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1883 lpfc_sli_resume_iocb(phba, pring);
1884
1885 if ((pring->lpfc_sli_cmd_available))
1886 (pring->lpfc_sli_cmd_available) (phba, pring);
1887
James Smart2e0fef82007-06-17 19:56:36 -05001888 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001889 }
1890
1891 return;
1892}
1893
James Smarte59058c2008-08-24 21:49:00 -04001894/**
1895 * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
1896 * @phba: Pointer to HBA context object.
1897 * @pring: Pointer to driver SLI ring object.
1898 * @mask: Host attention register mask for this ring.
1899 *
1900 * This function is called from the interrupt context when there is a ring
1901 * event for the fcp ring. The caller does not hold any lock.
1902 * The function processes each response iocb in the response ring until it
1903 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1904 * LE bit set. The function will call the completion handler of the command iocb
1905 * if the response iocb indicates a completion for a command iocb or it is
1906 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
1907 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05001908 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04001909 * to check it explicitly. This function always returns 1.
1910 **/
dea31012005-04-17 16:05:31 -05001911static int
James Smart2e0fef82007-06-17 19:56:36 -05001912lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1913 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05001914{
James Smart34b02dc2008-08-24 21:49:55 -04001915 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001916 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001917 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001918 struct lpfc_iocbq *cmdiocbq = NULL;
1919 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001920 uint32_t status;
1921 uint32_t portRspPut, portRspMax;
1922 int rc = 1;
1923 lpfc_iocb_type type;
1924 unsigned long iflag;
1925 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05001926
James Smart2e0fef82007-06-17 19:56:36 -05001927 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001928 pring->stats.iocb_event++;
1929
dea31012005-04-17 16:05:31 -05001930 /*
1931 * The next available response entry should never exceed the maximum
1932 * entries. If it does, treat it as an adapter hardware error.
1933 */
1934 portRspMax = pring->numRiocb;
1935 portRspPut = le32_to_cpu(pgp->rspPutInx);
1936 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001937 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05001938 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05001939 return 1;
1940 }
1941
1942 rmb();
1943 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001944 /*
1945 * Fetch an entry off the ring and copy it into a local data
1946 * structure. The copy involves a byte-swap since the
1947 * network byte order and pci byte orders are different.
1948 */
James Smarted957682007-06-17 19:56:37 -05001949 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05001950 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001951
1952 if (++pring->rspidx >= portRspMax)
1953 pring->rspidx = 0;
1954
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001955 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1956 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05001957 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05001958 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001959 irsp = &rspiocbq.iocb;
1960
dea31012005-04-17 16:05:31 -05001961 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1962 pring->stats.iocb_rsp++;
1963 rsp_cmpl++;
1964
1965 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001966 /*
1967 * If resource errors reported from HBA, reduce
1968 * queuedepths of the SCSI device.
1969 */
1970 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1971 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1972 spin_unlock_irqrestore(&phba->hbalock, iflag);
1973 lpfc_adjust_queue_depth(phba);
1974 spin_lock_irqsave(&phba->hbalock, iflag);
1975 }
1976
dea31012005-04-17 16:05:31 -05001977 /* Rsp ring <ringno> error: IOCB */
1978 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001979 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05001980 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001981 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05001982 irsp->un.ulpWord[0],
1983 irsp->un.ulpWord[1],
1984 irsp->un.ulpWord[2],
1985 irsp->un.ulpWord[3],
1986 irsp->un.ulpWord[4],
1987 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001988 *(uint32_t *)&irsp->un1,
1989 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05001990 }
1991
1992 switch (type) {
1993 case LPFC_ABORT_IOCB:
1994 case LPFC_SOL_IOCB:
1995 /*
1996 * Idle exchange closed via ABTS from port. No iocb
1997 * resources need to be recovered.
1998 */
1999 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002000 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002001 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002002 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002003 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002004 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002005 break;
2006 }
2007
James Bottomley604a3e32005-10-29 10:28:33 -05002008 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2009 &rspiocbq);
dea31012005-04-17 16:05:31 -05002010 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002011 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2012 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2013 &rspiocbq);
2014 } else {
James Smart2e0fef82007-06-17 19:56:36 -05002015 spin_unlock_irqrestore(&phba->hbalock,
2016 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002017 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2018 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002019 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002020 iflag);
2021 }
dea31012005-04-17 16:05:31 -05002022 }
2023 break;
James Smarta4bc3372006-12-02 13:34:16 -05002024 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002025 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002026 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002027 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002028 break;
dea31012005-04-17 16:05:31 -05002029 default:
2030 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2031 char adaptermsg[LPFC_MAX_ADPTMSG];
2032 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2033 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2034 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002035 dev_warn(&((phba->pcidev)->dev),
2036 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002037 phba->brd_no, adaptermsg);
2038 } else {
2039 /* Unknown IOCB command */
2040 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002041 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002042 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002043 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002044 irsp->ulpStatus,
2045 irsp->ulpIoTag,
2046 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002047 }
2048 break;
2049 }
2050
2051 /*
2052 * The response IOCB has been processed. Update the ring
2053 * pointer in SLIM. If the port response put pointer has not
2054 * been updated, sync the pgp->rspPutInx and fetch the new port
2055 * response put pointer.
2056 */
James Smarted957682007-06-17 19:56:37 -05002057 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002058
2059 if (pring->rspidx == portRspPut)
2060 portRspPut = le32_to_cpu(pgp->rspPutInx);
2061 }
2062
2063 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2064 pring->stats.iocb_rsp_full++;
2065 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2066 writel(status, phba->CAregaddr);
2067 readl(phba->CAregaddr);
2068 }
2069 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2070 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2071 pring->stats.iocb_cmd_empty++;
2072
2073 /* Force update of the local copy of cmdGetInx */
2074 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2075 lpfc_sli_resume_iocb(phba, pring);
2076
2077 if ((pring->lpfc_sli_cmd_available))
2078 (pring->lpfc_sli_cmd_available) (phba, pring);
2079
2080 }
2081
James Smart2e0fef82007-06-17 19:56:36 -05002082 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002083 return rc;
2084}
2085
James Smarte59058c2008-08-24 21:49:00 -04002086/**
2087 * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
2088 * @phba: Pointer to HBA context object.
2089 * @pring: Pointer to driver SLI ring object.
2090 * @mask: Host attention register mask for this ring.
2091 *
2092 * This function is called from the worker thread when there is a ring
2093 * event for non-fcp rings. The caller does not hold any lock .
2094 * The function processes each response iocb in the response ring until it
2095 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2096 * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
2097 * response iocb indicates a completion of a command iocb. The function
2098 * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
2099 * iocb. The function frees the resources or calls the completion handler if
2100 * this iocb is an abort completion. The function returns 0 when the allocated
2101 * iocbs are not freed, otherwise returns 1.
2102 **/
dea31012005-04-17 16:05:31 -05002103int
James Smart2e0fef82007-06-17 19:56:36 -05002104lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2105 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002106{
James Smart34b02dc2008-08-24 21:49:55 -04002107 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002108 IOCB_t *entry;
2109 IOCB_t *irsp = NULL;
2110 struct lpfc_iocbq *rspiocbp = NULL;
2111 struct lpfc_iocbq *next_iocb;
2112 struct lpfc_iocbq *cmdiocbp;
2113 struct lpfc_iocbq *saveq;
dea31012005-04-17 16:05:31 -05002114 uint8_t iocb_cmd_type;
2115 lpfc_iocb_type type;
2116 uint32_t status, free_saveq;
2117 uint32_t portRspPut, portRspMax;
2118 int rc = 1;
2119 unsigned long iflag;
dea31012005-04-17 16:05:31 -05002120
James Smart34b02dc2008-08-24 21:49:55 -04002121 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002122 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002123 pring->stats.iocb_event++;
2124
dea31012005-04-17 16:05:31 -05002125 /*
2126 * The next available response entry should never exceed the maximum
2127 * entries. If it does, treat it as an adapter hardware error.
2128 */
2129 portRspMax = pring->numRiocb;
2130 portRspPut = le32_to_cpu(pgp->rspPutInx);
2131 if (portRspPut >= portRspMax) {
2132 /*
2133 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
2134 * rsp ring <portRspMax>
2135 */
James Smarted957682007-06-17 19:56:37 -05002136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002137 "0303 Ring %d handler: portRspPut %d "
dea31012005-04-17 16:05:31 -05002138 "is bigger then rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002139 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002140
James Smart2e0fef82007-06-17 19:56:36 -05002141 phba->link_state = LPFC_HBA_ERROR;
2142 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002143
2144 phba->work_hs = HS_FFER3;
2145 lpfc_handle_eratt(phba);
2146
2147 return 1;
2148 }
2149
2150 rmb();
dea31012005-04-17 16:05:31 -05002151 while (pring->rspidx != portRspPut) {
2152 /*
2153 * Build a completion list and call the appropriate handler.
2154 * The process is to get the next available response iocb, get
2155 * a free iocb from the list, copy the response data into the
2156 * free iocb, insert to the continuation list, and update the
2157 * next response index to slim. This process makes response
2158 * iocb's in the ring available to DMA as fast as possible but
2159 * pays a penalty for a copy operation. Since the iocb is
2160 * only 32 bytes, this penalty is considered small relative to
2161 * the PCI reads for register values and a slim write. When
2162 * the ulpLe field is set, the entire Command has been
2163 * received.
2164 */
James Smarted957682007-06-17 19:56:37 -05002165 entry = lpfc_resp_iocb(phba, pring);
2166
James Smart858c9f62007-06-17 19:56:39 -05002167 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002168 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002169 if (rspiocbp == NULL) {
2170 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002171 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002172 break;
2173 }
2174
James Smarted957682007-06-17 19:56:37 -05002175 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2176 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002177 irsp = &rspiocbp->iocb;
2178
2179 if (++pring->rspidx >= portRspMax)
2180 pring->rspidx = 0;
2181
James Smarta58cbd52007-08-02 11:09:43 -04002182 if (pring->ringno == LPFC_ELS_RING) {
2183 lpfc_debugfs_slow_ring_trc(phba,
2184 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2185 *(((uint32_t *) irsp) + 4),
2186 *(((uint32_t *) irsp) + 6),
2187 *(((uint32_t *) irsp) + 7));
2188 }
2189
James Smarted957682007-06-17 19:56:37 -05002190 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002191
James Smart9c2face2008-01-11 01:53:18 -05002192 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
dea31012005-04-17 16:05:31 -05002193
2194 pring->iocb_continueq_cnt++;
2195 if (irsp->ulpLe) {
2196 /*
2197 * By default, the driver expects to free all resources
2198 * associated with this iocb completion.
2199 */
2200 free_saveq = 1;
2201 saveq = list_get_first(&pring->iocb_continueq,
2202 struct lpfc_iocbq, list);
2203 irsp = &(saveq->iocb);
2204 list_del_init(&pring->iocb_continueq);
2205 pring->iocb_continueq_cnt = 0;
2206
2207 pring->stats.iocb_rsp++;
2208
James Smart92d7f7b2007-06-17 19:56:38 -05002209 /*
2210 * If resource errors reported from HBA, reduce
2211 * queuedepths of the SCSI device.
2212 */
2213 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2214 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2215 spin_unlock_irqrestore(&phba->hbalock, iflag);
2216 lpfc_adjust_queue_depth(phba);
2217 spin_lock_irqsave(&phba->hbalock, iflag);
2218 }
2219
dea31012005-04-17 16:05:31 -05002220 if (irsp->ulpStatus) {
2221 /* Rsp ring <ringno> error: IOCB */
James Smarted957682007-06-17 19:56:37 -05002222 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002223 "0328 Rsp Ring %d error: "
James Smarted957682007-06-17 19:56:37 -05002224 "IOCB Data: "
2225 "x%x x%x x%x x%x "
2226 "x%x x%x x%x x%x "
2227 "x%x x%x x%x x%x "
2228 "x%x x%x x%x x%x\n",
James Smarted957682007-06-17 19:56:37 -05002229 pring->ringno,
2230 irsp->un.ulpWord[0],
2231 irsp->un.ulpWord[1],
2232 irsp->un.ulpWord[2],
2233 irsp->un.ulpWord[3],
2234 irsp->un.ulpWord[4],
2235 irsp->un.ulpWord[5],
2236 *(((uint32_t *) irsp) + 6),
2237 *(((uint32_t *) irsp) + 7),
2238 *(((uint32_t *) irsp) + 8),
2239 *(((uint32_t *) irsp) + 9),
2240 *(((uint32_t *) irsp) + 10),
2241 *(((uint32_t *) irsp) + 11),
2242 *(((uint32_t *) irsp) + 12),
2243 *(((uint32_t *) irsp) + 13),
2244 *(((uint32_t *) irsp) + 14),
2245 *(((uint32_t *) irsp) + 15));
dea31012005-04-17 16:05:31 -05002246 }
2247
2248 /*
2249 * Fetch the IOCB command type and call the correct
2250 * completion routine. Solicited and Unsolicited
2251 * IOCBs on the ELS ring get freed back to the
2252 * lpfc_iocb_list by the discovery kernel thread.
2253 */
2254 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2255 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2256 if (type == LPFC_SOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002257 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002258 rc = lpfc_sli_process_sol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002259 saveq);
2260 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002261 } else if (type == LPFC_UNSOL_IOCB) {
James Smart9c2face2008-01-11 01:53:18 -05002262 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002263 rc = lpfc_sli_process_unsol_iocb(phba, pring,
James Smart2e0fef82007-06-17 19:56:36 -05002264 saveq);
2265 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9c2face2008-01-11 01:53:18 -05002266 if (!rc)
2267 free_saveq = 0;
dea31012005-04-17 16:05:31 -05002268 } else if (type == LPFC_ABORT_IOCB) {
2269 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
2270 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05002271 lpfc_sli_iocbq_lookup(phba, pring,
2272 saveq)))) {
dea31012005-04-17 16:05:31 -05002273 /* Call the specified completion
2274 routine */
2275 if (cmdiocbp->iocb_cmpl) {
2276 spin_unlock_irqrestore(
James Smart2e0fef82007-06-17 19:56:36 -05002277 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002278 iflag);
2279 (cmdiocbp->iocb_cmpl) (phba,
2280 cmdiocbp, saveq);
2281 spin_lock_irqsave(
James Smart2e0fef82007-06-17 19:56:36 -05002282 &phba->hbalock,
dea31012005-04-17 16:05:31 -05002283 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002284 } else
James Smart2e0fef82007-06-17 19:56:36 -05002285 __lpfc_sli_release_iocbq(phba,
James Bottomley604a3e32005-10-29 10:28:33 -05002286 cmdiocbp);
dea31012005-04-17 16:05:31 -05002287 }
2288 } else if (type == LPFC_UNKNOWN_IOCB) {
2289 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2290
2291 char adaptermsg[LPFC_MAX_ADPTMSG];
2292
2293 memset(adaptermsg, 0,
2294 LPFC_MAX_ADPTMSG);
2295 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2296 MAX_MSG_DATA);
2297 dev_warn(&((phba->pcidev)->dev),
Joe Perches898eb712007-10-18 03:06:30 -07002298 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002299 phba->brd_no, adaptermsg);
2300 } else {
2301 /* Unknown IOCB command */
James Smart92d7f7b2007-06-17 19:56:38 -05002302 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002303 "0335 Unknown IOCB "
James Smart92d7f7b2007-06-17 19:56:38 -05002304 "command Data: x%x "
2305 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002306 irsp->ulpCommand,
2307 irsp->ulpStatus,
2308 irsp->ulpIoTag,
2309 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002310 }
2311 }
2312
2313 if (free_saveq) {
James Smart2e0fef82007-06-17 19:56:36 -05002314 list_for_each_entry_safe(rspiocbp, next_iocb,
2315 &saveq->list, list) {
2316 list_del(&rspiocbp->list);
2317 __lpfc_sli_release_iocbq(phba,
2318 rspiocbp);
dea31012005-04-17 16:05:31 -05002319 }
James Smart2e0fef82007-06-17 19:56:36 -05002320 __lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05002321 }
James Smart92d7f7b2007-06-17 19:56:38 -05002322 rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002323 }
2324
2325 /*
2326 * If the port response put pointer has not been updated, sync
2327 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2328 * response put pointer.
2329 */
2330 if (pring->rspidx == portRspPut) {
2331 portRspPut = le32_to_cpu(pgp->rspPutInx);
2332 }
2333 } /* while (pring->rspidx != portRspPut) */
2334
James Smart92d7f7b2007-06-17 19:56:38 -05002335 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002336 /* At least one response entry has been freed */
2337 pring->stats.iocb_rsp_full++;
2338 /* SET RxRE_RSP in Chip Att register */
2339 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2340 writel(status, phba->CAregaddr);
2341 readl(phba->CAregaddr); /* flush */
2342 }
2343 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2344 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2345 pring->stats.iocb_cmd_empty++;
2346
2347 /* Force update of the local copy of cmdGetInx */
2348 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2349 lpfc_sli_resume_iocb(phba, pring);
2350
2351 if ((pring->lpfc_sli_cmd_available))
2352 (pring->lpfc_sli_cmd_available) (phba, pring);
2353
2354 }
2355
James Smart2e0fef82007-06-17 19:56:36 -05002356 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002357 return rc;
2358}
2359
James Smarte59058c2008-08-24 21:49:00 -04002360/**
2361 * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
2362 * @phba: Pointer to HBA context object.
2363 * @pring: Pointer to driver SLI ring object.
2364 *
2365 * This function aborts all iocbs in the given ring and frees all the iocb
2366 * objects in txq. This function issues an abort iocb for all the iocb commands
2367 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2368 * the return of this function. The caller is not required to hold any locks.
2369 **/
James Smart2e0fef82007-06-17 19:56:36 -05002370void
dea31012005-04-17 16:05:31 -05002371lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2372{
James Smart2534ba72007-04-25 09:52:20 -04002373 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002374 struct lpfc_iocbq *iocb, *next_iocb;
James Smart2534ba72007-04-25 09:52:20 -04002375 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05002376
James Smart92d7f7b2007-06-17 19:56:38 -05002377 if (pring->ringno == LPFC_ELS_RING) {
2378 lpfc_fabric_abort_hba(phba);
2379 }
2380
dea31012005-04-17 16:05:31 -05002381 /* Error everything on txq and txcmplq
2382 * First do the txq.
2383 */
James Smart2e0fef82007-06-17 19:56:36 -05002384 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002385 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002386 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002387
2388 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002389 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2390 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2391
James Smart2e0fef82007-06-17 19:56:36 -05002392 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002393
2394 while (!list_empty(&completions)) {
2395 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
dea31012005-04-17 16:05:31 -05002396 cmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05002397 list_del_init(&iocb->list);
dea31012005-04-17 16:05:31 -05002398
James Smart2e0fef82007-06-17 19:56:36 -05002399 if (!iocb->iocb_cmpl)
2400 lpfc_sli_release_iocbq(phba, iocb);
2401 else {
dea31012005-04-17 16:05:31 -05002402 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2403 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
dea31012005-04-17 16:05:31 -05002404 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05002405 }
dea31012005-04-17 16:05:31 -05002406 }
dea31012005-04-17 16:05:31 -05002407}
2408
James Smarte59058c2008-08-24 21:49:00 -04002409/**
James Smarta8e497d2008-08-24 21:50:11 -04002410 * lpfc_sli_flush_fcp_rings: flush all iocbs in the fcp ring.
2411 * @phba: Pointer to HBA context object.
2412 *
2413 * This function flushes all iocbs in the fcp ring and frees all the iocb
2414 * objects in txq and txcmplq. This function will not issue abort iocbs
2415 * for all the iocb commands in txcmplq, they will just be returned with
2416 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2417 * slot has been permanently disabled.
2418 **/
2419void
2420lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2421{
2422 LIST_HEAD(txq);
2423 LIST_HEAD(txcmplq);
2424 struct lpfc_iocbq *iocb;
2425 IOCB_t *cmd = NULL;
2426 struct lpfc_sli *psli = &phba->sli;
2427 struct lpfc_sli_ring *pring;
2428
2429 /* Currently, only one fcp ring */
2430 pring = &psli->ring[psli->fcp_ring];
2431
2432 spin_lock_irq(&phba->hbalock);
2433 /* Retrieve everything on txq */
2434 list_splice_init(&pring->txq, &txq);
2435 pring->txq_cnt = 0;
2436
2437 /* Retrieve everything on the txcmplq */
2438 list_splice_init(&pring->txcmplq, &txcmplq);
2439 pring->txcmplq_cnt = 0;
2440 spin_unlock_irq(&phba->hbalock);
2441
2442 /* Flush the txq */
2443 while (!list_empty(&txq)) {
2444 iocb = list_get_first(&txq, struct lpfc_iocbq, list);
2445 cmd = &iocb->iocb;
2446 list_del_init(&iocb->list);
2447
2448 if (!iocb->iocb_cmpl)
2449 lpfc_sli_release_iocbq(phba, iocb);
2450 else {
2451 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2452 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2453 (iocb->iocb_cmpl) (phba, iocb, iocb);
2454 }
2455 }
2456
2457 /* Flush the txcmpq */
2458 while (!list_empty(&txcmplq)) {
2459 iocb = list_get_first(&txcmplq, struct lpfc_iocbq, list);
2460 cmd = &iocb->iocb;
2461 list_del_init(&iocb->list);
2462
2463 if (!iocb->iocb_cmpl)
2464 lpfc_sli_release_iocbq(phba, iocb);
2465 else {
2466 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2467 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2468 (iocb->iocb_cmpl) (phba, iocb, iocb);
2469 }
2470 }
2471}
2472
2473/**
James Smarte59058c2008-08-24 21:49:00 -04002474 * lpfc_sli_brdready: Check for host status bits.
2475 * @phba: Pointer to HBA context object.
2476 * @mask: Bit mask to be checked.
2477 *
2478 * This function reads the host status register and compares
2479 * with the provided bit mask to check if HBA completed
2480 * the restart. This function will wait in a loop for the
2481 * HBA to complete restart. If the HBA does not restart within
2482 * 15 iterations, the function will reset the HBA again. The
2483 * function returns 1 when HBA fail to restart otherwise returns
2484 * zero.
2485 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002486int
James Smart2e0fef82007-06-17 19:56:36 -05002487lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05002488{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002489 uint32_t status;
2490 int i = 0;
2491 int retval = 0;
dea31012005-04-17 16:05:31 -05002492
Jamie Wellnitz41415862006-02-28 19:25:27 -05002493 /* Read the HBA Host Status Register */
2494 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002495
Jamie Wellnitz41415862006-02-28 19:25:27 -05002496 /*
2497 * Check status register every 100ms for 5 retries, then every
2498 * 500ms for 5, then every 2.5 sec for 5, then reset board and
2499 * every 2.5 sec for 4.
2500 * Break our of the loop if errors occurred during init.
2501 */
2502 while (((status & mask) != mask) &&
2503 !(status & HS_FFERM) &&
2504 i++ < 20) {
dea31012005-04-17 16:05:31 -05002505
Jamie Wellnitz41415862006-02-28 19:25:27 -05002506 if (i <= 5)
2507 msleep(10);
2508 else if (i <= 10)
2509 msleep(500);
2510 else
2511 msleep(2500);
dea31012005-04-17 16:05:31 -05002512
Jamie Wellnitz41415862006-02-28 19:25:27 -05002513 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002514 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002515 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002516 lpfc_sli_brdrestart(phba);
2517 }
2518 /* Read the HBA Host Status Register */
2519 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002520 }
dea31012005-04-17 16:05:31 -05002521
Jamie Wellnitz41415862006-02-28 19:25:27 -05002522 /* Check to see if any errors occurred during init */
2523 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05002524 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002525 retval = 1;
2526 }
dea31012005-04-17 16:05:31 -05002527
Jamie Wellnitz41415862006-02-28 19:25:27 -05002528 return retval;
dea31012005-04-17 16:05:31 -05002529}
2530
James Smart92908312006-03-07 15:04:13 -05002531#define BARRIER_TEST_PATTERN (0xdeadbeef)
2532
James Smarte59058c2008-08-24 21:49:00 -04002533/**
2534 * lpfc_reset_barrier: Make HBA ready for HBA reset.
2535 * @phba: Pointer to HBA context object.
2536 *
2537 * This function is called before resetting an HBA. This
2538 * function requests HBA to quiesce DMAs before a reset.
2539 **/
James Smart2e0fef82007-06-17 19:56:36 -05002540void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05002541{
James Smart65a29c12006-07-06 15:50:50 -04002542 uint32_t __iomem *resp_buf;
2543 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05002544 volatile uint32_t mbox;
2545 uint32_t hc_copy;
2546 int i;
2547 uint8_t hdrtype;
2548
2549 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2550 if (hdrtype != 0x80 ||
2551 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2552 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2553 return;
2554
2555 /*
2556 * Tell the other part of the chip to suspend temporarily all
2557 * its DMA activity.
2558 */
James Smart65a29c12006-07-06 15:50:50 -04002559 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002560
2561 /* Disable the error attention */
2562 hc_copy = readl(phba->HCregaddr);
2563 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2564 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002565 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002566
2567 if (readl(phba->HAregaddr) & HA_ERATT) {
2568 /* Clear Chip error bit */
2569 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002570 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002571 }
2572
2573 mbox = 0;
2574 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2575 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2576
2577 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04002578 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002579 writel(mbox, mbox_buf);
2580
2581 for (i = 0;
2582 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2583 mdelay(1);
2584
2585 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2586 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05002587 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05002588 goto restore_hc;
2589 else
2590 goto clear_errat;
2591 }
2592
2593 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2594 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
2595 mdelay(1);
2596
2597clear_errat:
2598
2599 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2600 mdelay(1);
2601
2602 if (readl(phba->HAregaddr) & HA_ERATT) {
2603 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002604 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002605 }
2606
2607restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05002608 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002609 writel(hc_copy, phba->HCregaddr);
2610 readl(phba->HCregaddr); /* flush */
2611}
2612
James Smarte59058c2008-08-24 21:49:00 -04002613/**
2614 * lpfc_sli_brdkill: Issue a kill_board mailbox command.
2615 * @phba: Pointer to HBA context object.
2616 *
2617 * This function issues a kill_board mailbox command and waits for
2618 * the error attention interrupt. This function is called for stopping
2619 * the firmware processing. The caller is not required to hold any
2620 * locks. This function calls lpfc_hba_down_post function to free
2621 * any pending commands after the kill. The function will return 1 when it
2622 * fails to kill the board else will return 0.
2623 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002624int
James Smart2e0fef82007-06-17 19:56:36 -05002625lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002626{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002627 struct lpfc_sli *psli;
2628 LPFC_MBOXQ_t *pmb;
2629 uint32_t status;
2630 uint32_t ha_copy;
2631 int retval;
2632 int i = 0;
2633
2634 psli = &phba->sli;
2635
2636 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05002637 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002638 "0329 Kill HBA Data: x%x x%x\n",
2639 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002640
James Smart98c9ea52007-10-27 13:37:33 -04002641 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2642 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002643 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002644
2645 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05002646 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002647 status = readl(phba->HCregaddr);
2648 status &= ~HC_ERINT_ENA;
2649 writel(status, phba->HCregaddr);
2650 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002651 phba->link_flag |= LS_IGNORE_ERATT;
2652 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002653
2654 lpfc_kill_board(phba, pmb);
2655 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2656 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2657
2658 if (retval != MBX_SUCCESS) {
2659 if (retval != MBX_BUSY)
2660 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002661 spin_lock_irq(&phba->hbalock);
2662 phba->link_flag &= ~LS_IGNORE_ERATT;
2663 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002664 return 1;
2665 }
2666
James Smart92908312006-03-07 15:04:13 -05002667 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2668
Jamie Wellnitz41415862006-02-28 19:25:27 -05002669 mempool_free(pmb, phba->mbox_mem_pool);
2670
2671 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2672 * attention every 100ms for 3 seconds. If we don't get ERATT after
2673 * 3 seconds we still set HBA_ERROR state because the status of the
2674 * board is now undefined.
2675 */
2676 ha_copy = readl(phba->HAregaddr);
2677
2678 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2679 mdelay(100);
2680 ha_copy = readl(phba->HAregaddr);
2681 }
2682
2683 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05002684 if (ha_copy & HA_ERATT) {
2685 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002686 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002687 }
James Smart2e0fef82007-06-17 19:56:36 -05002688 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002689 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002690 phba->link_flag &= ~LS_IGNORE_ERATT;
2691 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002692
2693 psli->mbox_active = NULL;
2694 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002695 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002696
James Smart2e0fef82007-06-17 19:56:36 -05002697 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002698}
2699
James Smarte59058c2008-08-24 21:49:00 -04002700/**
2701 * lpfc_sli_brdreset: Reset the HBA.
2702 * @phba: Pointer to HBA context object.
2703 *
2704 * This function resets the HBA by writing HC_INITFF to the control
2705 * register. After the HBA resets, this function resets all the iocb ring
2706 * indices. This function disables PCI layer parity checking during
2707 * the reset.
2708 * This function returns 0 always.
2709 * The caller is not required to hold any locks.
2710 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002711int
James Smart2e0fef82007-06-17 19:56:36 -05002712lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002713{
2714 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002715 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002716 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05002717 int i;
dea31012005-04-17 16:05:31 -05002718
Jamie Wellnitz41415862006-02-28 19:25:27 -05002719 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002720
Jamie Wellnitz41415862006-02-28 19:25:27 -05002721 /* Reset HBA */
2722 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002723 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002724 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05002725
2726 /* perform board reset */
2727 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002728 phba->pport->fc_myDID = 0;
2729 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002730
Jamie Wellnitz41415862006-02-28 19:25:27 -05002731 /* Turn off parity checking and serr during the physical reset */
2732 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2733 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2734 (cfg_value &
2735 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2736
James Smart1c067a42006-08-01 07:33:52 -04002737 psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002738 /* Now toggle INITFF bit in the Host Control Register */
2739 writel(HC_INITFF, phba->HCregaddr);
2740 mdelay(1);
2741 readl(phba->HCregaddr); /* flush */
2742 writel(0, phba->HCregaddr);
2743 readl(phba->HCregaddr); /* flush */
2744
2745 /* Restore PCI cmd register */
2746 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002747
2748 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002749 for (i = 0; i < psli->num_rings; i++) {
2750 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002751 pring->flag = 0;
2752 pring->rspidx = 0;
2753 pring->next_cmdidx = 0;
2754 pring->local_getidx = 0;
2755 pring->cmdidx = 0;
2756 pring->missbufcnt = 0;
2757 }
dea31012005-04-17 16:05:31 -05002758
James Smart2e0fef82007-06-17 19:56:36 -05002759 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002760 return 0;
2761}
2762
James Smarte59058c2008-08-24 21:49:00 -04002763/**
2764 * lpfc_sli_brdrestart: Restart the HBA.
2765 * @phba: Pointer to HBA context object.
2766 *
2767 * This function is called in the SLI initialization code path to
2768 * restart the HBA. The caller is not required to hold any lock.
2769 * This function writes MBX_RESTART mailbox command to the SLIM and
2770 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
2771 * function to free any pending commands. The function enables
2772 * POST only during the first initialization. The function returns zero.
2773 * The function does not guarantee completion of MBX_RESTART mailbox
2774 * command before the return of this function.
2775 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002776int
James Smart2e0fef82007-06-17 19:56:36 -05002777lpfc_sli_brdrestart(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002778{
2779 MAILBOX_t *mb;
2780 struct lpfc_sli *psli;
2781 uint16_t skip_post;
2782 volatile uint32_t word0;
2783 void __iomem *to_slim;
2784
James Smart2e0fef82007-06-17 19:56:36 -05002785 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002786
2787 psli = &phba->sli;
2788
2789 /* Restart HBA */
2790 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002791 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002792 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002793
2794 word0 = 0;
2795 mb = (MAILBOX_t *) &word0;
2796 mb->mbxCommand = MBX_RESTART;
2797 mb->mbxHc = 1;
2798
James Smart92908312006-03-07 15:04:13 -05002799 lpfc_reset_barrier(phba);
2800
Jamie Wellnitz41415862006-02-28 19:25:27 -05002801 to_slim = phba->MBslimaddr;
2802 writel(*(uint32_t *) mb, to_slim);
2803 readl(to_slim); /* flush */
2804
2805 /* Only skip post after fc_ffinit is completed */
James Smart2e0fef82007-06-17 19:56:36 -05002806 if (phba->pport->port_state) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002807 skip_post = 1;
2808 word0 = 1; /* This is really setting up word1 */
dea31012005-04-17 16:05:31 -05002809 } else {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002810 skip_post = 0;
2811 word0 = 0; /* This is really setting up word1 */
2812 }
James Smart65a29c12006-07-06 15:50:50 -04002813 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002814 writel(*(uint32_t *) mb, to_slim);
2815 readl(to_slim); /* flush */
2816
2817 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002818 phba->pport->stopped = 0;
2819 phba->link_state = LPFC_INIT_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002820
James Smart2e0fef82007-06-17 19:56:36 -05002821 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002822
James Smart64ba8812006-08-02 15:24:34 -04002823 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2824 psli->stats_start = get_seconds();
2825
Jamie Wellnitz41415862006-02-28 19:25:27 -05002826 if (skip_post)
2827 mdelay(100);
2828 else
dea31012005-04-17 16:05:31 -05002829 mdelay(2000);
dea31012005-04-17 16:05:31 -05002830
Jamie Wellnitz41415862006-02-28 19:25:27 -05002831 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05002832
2833 return 0;
2834}
2835
James Smarte59058c2008-08-24 21:49:00 -04002836/**
2837 * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
2838 * @phba: Pointer to HBA context object.
2839 *
2840 * This function is called after a HBA restart to wait for successful
2841 * restart of the HBA. Successful restart of the HBA is indicated by
2842 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
2843 * iteration, the function will restart the HBA again. The function returns
2844 * zero if HBA successfully restarted else returns negative error code.
2845 **/
dea31012005-04-17 16:05:31 -05002846static int
2847lpfc_sli_chipset_init(struct lpfc_hba *phba)
2848{
2849 uint32_t status, i = 0;
2850
2851 /* Read the HBA Host Status Register */
2852 status = readl(phba->HSregaddr);
2853
2854 /* Check status register to see what current state is */
2855 i = 0;
2856 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2857
2858 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2859 * every 2.5 sec for 5, then reset board and every 2.5 sec for
2860 * 4.
2861 */
2862 if (i++ >= 20) {
2863 /* Adapter failed to init, timeout, status reg
2864 <status> */
James Smarted957682007-06-17 19:56:37 -05002865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002866 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002867 "timeout, status reg x%x, "
2868 "FW Data: A8 x%x AC x%x\n", status,
2869 readl(phba->MBslimaddr + 0xa8),
2870 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002871 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002872 return -ETIMEDOUT;
2873 }
2874
2875 /* Check to see if any errors occurred during init */
2876 if (status & HS_FFERM) {
2877 /* ERROR: During chipset initialization */
2878 /* Adapter failed to init, chipset, status reg
2879 <status> */
James Smarted957682007-06-17 19:56:37 -05002880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002881 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05002882 "chipset, status reg x%x, "
2883 "FW Data: A8 x%x AC x%x\n", status,
2884 readl(phba->MBslimaddr + 0xa8),
2885 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002886 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002887 return -EIO;
2888 }
2889
2890 if (i <= 5) {
2891 msleep(10);
2892 } else if (i <= 10) {
2893 msleep(500);
2894 } else {
2895 msleep(2500);
2896 }
2897
2898 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002899 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002900 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002901 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05002902 }
2903 /* Read the HBA Host Status Register */
2904 status = readl(phba->HSregaddr);
2905 }
2906
2907 /* Check to see if any errors occurred during init */
2908 if (status & HS_FFERM) {
2909 /* ERROR: During chipset initialization */
2910 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05002911 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002912 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05002913 "status reg x%x, "
2914 "FW Data: A8 x%x AC x%x\n", status,
2915 readl(phba->MBslimaddr + 0xa8),
2916 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05002917 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002918 return -EIO;
2919 }
2920
2921 /* Clear all interrupt enable conditions */
2922 writel(0, phba->HCregaddr);
2923 readl(phba->HCregaddr); /* flush */
2924
2925 /* setup host attn register */
2926 writel(0xffffffff, phba->HAregaddr);
2927 readl(phba->HAregaddr); /* flush */
2928 return 0;
2929}
2930
James Smarte59058c2008-08-24 21:49:00 -04002931/**
2932 * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
2933 *
2934 * This function calculates and returns the number of HBQs required to be
2935 * configured.
2936 **/
James Smart78b2d852007-08-02 11:10:21 -04002937int
James Smarted957682007-06-17 19:56:37 -05002938lpfc_sli_hbq_count(void)
2939{
James Smart92d7f7b2007-06-17 19:56:38 -05002940 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05002941}
2942
James Smarte59058c2008-08-24 21:49:00 -04002943/**
2944 * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
2945 *
2946 * This function adds the number of hbq entries in every HBQ to get
2947 * the total number of hbq entries required for the HBA and returns
2948 * the total count.
2949 **/
James Smarted957682007-06-17 19:56:37 -05002950static int
2951lpfc_sli_hbq_entry_count(void)
2952{
2953 int hbq_count = lpfc_sli_hbq_count();
2954 int count = 0;
2955 int i;
2956
2957 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05002958 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05002959 return count;
2960}
2961
James Smarte59058c2008-08-24 21:49:00 -04002962/**
2963 * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
2964 *
2965 * This function calculates amount of memory required for all hbq entries
2966 * to be configured and returns the total memory required.
2967 **/
dea31012005-04-17 16:05:31 -05002968int
James Smarted957682007-06-17 19:56:37 -05002969lpfc_sli_hbq_size(void)
2970{
2971 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2972}
2973
James Smarte59058c2008-08-24 21:49:00 -04002974/**
2975 * lpfc_sli_hbq_setup: configure and initialize HBQs.
2976 * @phba: Pointer to HBA context object.
2977 *
2978 * This function is called during the SLI initialization to configure
2979 * all the HBQs and post buffers to the HBQ. The caller is not
2980 * required to hold any locks. This function will return zero if successful
2981 * else it will return negative error code.
2982 **/
James Smarted957682007-06-17 19:56:37 -05002983static int
2984lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2985{
2986 int hbq_count = lpfc_sli_hbq_count();
2987 LPFC_MBOXQ_t *pmb;
2988 MAILBOX_t *pmbox;
2989 uint32_t hbqno;
2990 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05002991
James Smart92d7f7b2007-06-17 19:56:38 -05002992 /* Get a Mailbox buffer to setup mailbox
2993 * commands for HBA initialization
2994 */
James Smarted957682007-06-17 19:56:37 -05002995 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2996
2997 if (!pmb)
2998 return -ENOMEM;
2999
3000 pmbox = &pmb->mb;
3001
3002 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3003 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003004 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003005
3006 hbq_entry_index = 0;
3007 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3008 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3009 phba->hbqs[hbqno].hbqPutIdx = 0;
3010 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3011 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003012 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003013 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3014 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003015 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3016
3017 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3018 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3019 mbxStatus <status>, ring <num> */
3020
3021 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003022 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003023 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003024 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003025 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003026 pmbox->mbxStatus, hbqno);
3027
3028 phba->link_state = LPFC_HBA_ERROR;
3029 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003030 return ENXIO;
3031 }
3032 }
3033 phba->hbq_count = hbq_count;
3034
James Smarted957682007-06-17 19:56:37 -05003035 mempool_free(pmb, phba->mbox_mem_pool);
3036
James Smart92d7f7b2007-06-17 19:56:38 -05003037 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003038 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3039 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003040 return 0;
3041}
3042
James Smarte59058c2008-08-24 21:49:00 -04003043/**
James Smart93996272008-08-24 21:50:30 -04003044 * lpfc_sli_config_port: Issue config port mailbox command.
James Smarte59058c2008-08-24 21:49:00 -04003045 * @phba: Pointer to HBA context object.
3046 * @sli_mode: sli mode - 2/3
3047 *
3048 * This function is called by the sli intialization code path
3049 * to issue config_port mailbox command. This function restarts the
3050 * HBA firmware and issues a config_port mailbox command to configure
3051 * the SLI interface in the sli mode specified by sli_mode
3052 * variable. The caller is not required to hold any locks.
3053 * The function returns 0 if successful, else returns negative error
3054 * code.
3055 **/
James Smart93996272008-08-24 21:50:30 -04003056int
3057lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003058{
3059 LPFC_MBOXQ_t *pmb;
3060 uint32_t resetcount = 0, rc = 0, done = 0;
3061
3062 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3063 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003064 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003065 return -ENOMEM;
3066 }
3067
James Smarted957682007-06-17 19:56:37 -05003068 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003069 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003070 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003071 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003072 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003073 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003074 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003075 msleep(2500);
3076 rc = lpfc_sli_chipset_init(phba);
3077 if (rc)
3078 break;
3079
James Smart2e0fef82007-06-17 19:56:36 -05003080 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003081 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003082 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003083 resetcount++;
3084
James Smarted957682007-06-17 19:56:37 -05003085 /* Call pre CONFIG_PORT mailbox command initialization. A
3086 * value of 0 means the call was successful. Any other
3087 * nonzero value is a failure, but if ERESTART is returned,
3088 * the driver may reset the HBA and try again.
3089 */
dea31012005-04-17 16:05:31 -05003090 rc = lpfc_config_port_prep(phba);
3091 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003092 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003093 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003094 } else if (rc)
dea31012005-04-17 16:05:31 -05003095 break;
James Smart2e0fef82007-06-17 19:56:36 -05003096 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003097 lpfc_config_port(phba, pmb);
3098 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003099 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3100 LPFC_SLI3_HBQ_ENABLED |
3101 LPFC_SLI3_CRP_ENABLED |
3102 LPFC_SLI3_INB_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003103 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003105 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003106 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003107 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003108 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003109 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003110 spin_unlock_irq(&phba->hbalock);
3111 rc = -ENXIO;
James Smart34b02dc2008-08-24 21:49:55 -04003112 } else
James Smarted957682007-06-17 19:56:37 -05003113 done = 1;
dea31012005-04-17 16:05:31 -05003114 }
James Smarted957682007-06-17 19:56:37 -05003115 if (!done) {
3116 rc = -EINVAL;
3117 goto do_prep_failed;
3118 }
James Smart34b02dc2008-08-24 21:49:55 -04003119 if (pmb->mb.un.varCfgPort.sli_mode == 3) {
3120 if (!pmb->mb.un.varCfgPort.cMA) {
3121 rc = -ENXIO;
3122 goto do_prep_failed;
3123 }
3124 if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
3125 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3126 phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
3127 } else
3128 phba->max_vpi = 0;
3129 if (pmb->mb.un.varCfgPort.gerbm)
3130 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3131 if (pmb->mb.un.varCfgPort.gcrp)
3132 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3133 if (pmb->mb.un.varCfgPort.ginb) {
3134 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
3135 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3136 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3137 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3138 phba->inb_last_counter =
3139 phba->mbox->us.s3_inb_pgp.counter;
3140 } else {
3141 phba->port_gp = phba->mbox->us.s3_pgp.port;
3142 phba->inb_ha_copy = NULL;
3143 phba->inb_counter = NULL;
3144 }
3145 } else {
3146 phba->port_gp = phba->mbox->us.s2.port;
3147 phba->inb_ha_copy = NULL;
3148 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003149 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003150 }
James Smart92d7f7b2007-06-17 19:56:38 -05003151do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003152 mempool_free(pmb, phba->mbox_mem_pool);
3153 return rc;
3154}
3155
James Smarte59058c2008-08-24 21:49:00 -04003156
3157/**
3158 * lpfc_sli_hba_setup: SLI intialization function.
3159 * @phba: Pointer to HBA context object.
3160 *
3161 * This function is the main SLI intialization function. This function
3162 * is called by the HBA intialization code, HBA reset code and HBA
3163 * error attention handler code. Caller is not required to hold any
3164 * locks. This function issues config_port mailbox command to configure
3165 * the SLI, setup iocb rings and HBQ rings. In the end the function
3166 * calls the config_port_post function to issue init_link mailbox
3167 * command and to start the discovery. The function will return zero
3168 * if successful, else it will return negative error code.
3169 **/
James Smarted957682007-06-17 19:56:37 -05003170int
3171lpfc_sli_hba_setup(struct lpfc_hba *phba)
3172{
3173 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003174 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003175
3176 switch (lpfc_sli_mode) {
3177 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003178 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003180 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003181 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003182 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003183 break;
3184 }
James Smarted957682007-06-17 19:56:37 -05003185 mode = 2;
3186 break;
3187 case 0:
3188 case 3:
3189 break;
3190 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003192 "1819 Unrecognized lpfc_sli_mode "
3193 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003194
3195 break;
3196 }
3197
James Smart93996272008-08-24 21:50:30 -04003198 rc = lpfc_sli_config_port(phba, mode);
3199
James Smarted957682007-06-17 19:56:37 -05003200 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003202 "1820 Unable to select SLI-3. "
3203 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003204 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04003205 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05003206 if (rc)
dea31012005-04-17 16:05:31 -05003207 goto lpfc_sli_hba_setup_error;
3208
James Smarted957682007-06-17 19:56:37 -05003209 if (phba->sli_rev == 3) {
3210 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3211 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05003212 } else {
3213 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3214 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003215 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003216 }
3217
3218 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003219 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3220 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05003221 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05003222
3223 if (rc)
3224 goto lpfc_sli_hba_setup_error;
3225
James Smart93996272008-08-24 21:50:30 -04003226 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05003227 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3228 rc = lpfc_sli_hbq_setup(phba);
3229 if (rc)
3230 goto lpfc_sli_hba_setup_error;
3231 }
3232
dea31012005-04-17 16:05:31 -05003233 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3234
3235 rc = lpfc_config_port_post(phba);
3236 if (rc)
3237 goto lpfc_sli_hba_setup_error;
3238
James Smarted957682007-06-17 19:56:37 -05003239 return rc;
3240
James Smart92d7f7b2007-06-17 19:56:38 -05003241lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05003242 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05003243 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003244 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05003245 return rc;
3246}
3247
James Smarte59058c2008-08-24 21:49:00 -04003248
3249/**
3250 * lpfc_mbox_timeout: Timeout call back function for mbox timer.
3251 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05003252 *
James Smarte59058c2008-08-24 21:49:00 -04003253 * This is the callback function for mailbox timer. The mailbox
3254 * timer is armed when a new mailbox command is issued and the timer
3255 * is deleted when the mailbox complete. The function is called by
3256 * the kernel timer code when a mailbox does not complete within
3257 * expected time. This function wakes up the worker thread to
3258 * process the mailbox timeout and returns. All the processing is
3259 * done by the worker thread function lpfc_mbox_timeout_handler.
3260 **/
dea31012005-04-17 16:05:31 -05003261void
3262lpfc_mbox_timeout(unsigned long ptr)
3263{
James Smart92d7f7b2007-06-17 19:56:38 -05003264 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05003265 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05003266 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05003267
James Smart2e0fef82007-06-17 19:56:36 -05003268 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05003269 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05003270 if (!tmo_posted)
3271 phba->pport->work_port_events |= WORKER_MBOX_TMO;
3272 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
3273
James Smart5e9d9b82008-06-14 22:52:53 -04003274 if (!tmo_posted)
3275 lpfc_worker_wake_up(phba);
3276 return;
dea31012005-04-17 16:05:31 -05003277}
3278
James Smarte59058c2008-08-24 21:49:00 -04003279
3280/**
3281 * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
3282 * @phba: Pointer to HBA context object.
3283 *
3284 * This function is called from worker thread when a mailbox command times out.
3285 * The caller is not required to hold any locks. This function will reset the
3286 * HBA and recover all the pending commands.
3287 **/
dea31012005-04-17 16:05:31 -05003288void
3289lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
3290{
James Smart2e0fef82007-06-17 19:56:36 -05003291 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
3292 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04003293 struct lpfc_sli *psli = &phba->sli;
3294 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05003295
James Smart2e0fef82007-06-17 19:56:36 -05003296 if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
dea31012005-04-17 16:05:31 -05003297 return;
3298 }
3299
dea31012005-04-17 16:05:31 -05003300 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05003301 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003302 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003303 mb->mbxCommand,
3304 phba->pport->port_state,
3305 phba->sli.sli_flag,
3306 phba->sli.mbox_active);
dea31012005-04-17 16:05:31 -05003307
James Smart1dcb58e2007-04-25 09:51:30 -04003308 /* Setting state unknown so lpfc_sli_abort_iocb_ring
3309 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
3310 * it to fail all oustanding SCSI IO.
3311 */
James Smart2e0fef82007-06-17 19:56:36 -05003312 spin_lock_irq(&phba->pport->work_port_lock);
3313 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3314 spin_unlock_irq(&phba->pport->work_port_lock);
3315 spin_lock_irq(&phba->hbalock);
3316 phba->link_state = LPFC_LINK_UNKNOWN;
James Smart1dcb58e2007-04-25 09:51:30 -04003317 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003318 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04003319
3320 pring = &psli->ring[psli->fcp_ring];
3321 lpfc_sli_abort_iocb_ring(phba, pring);
3322
3323 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003324 "0345 Resetting board due to mailbox timeout\n");
James Smart1dcb58e2007-04-25 09:51:30 -04003325 /*
3326 * lpfc_offline calls lpfc_sli_hba_down which will clean up
3327 * on oustanding mailbox commands.
3328 */
James Smart13815c82008-01-11 01:52:48 -05003329 /* If resets are disabled then set error state and return. */
3330 if (!phba->cfg_enable_hba_reset) {
3331 phba->link_state = LPFC_HBA_ERROR;
3332 return;
3333 }
James Smart1dcb58e2007-04-25 09:51:30 -04003334 lpfc_offline_prep(phba);
3335 lpfc_offline(phba);
3336 lpfc_sli_brdrestart(phba);
James Smart58da1ff2008-04-07 10:15:56 -04003337 lpfc_online(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04003338 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003339 return;
3340}
3341
James Smarte59058c2008-08-24 21:49:00 -04003342/**
3343 * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
3344 * @phba: Pointer to HBA context object.
3345 * @pmbox: Pointer to mailbox object.
3346 * @flag: Flag indicating how the mailbox need to be processed.
3347 *
3348 * This function is called by discovery code and HBA management code
3349 * to submit a mailbox command to firmware. This function gets the
3350 * hbalock to protect the data structures.
3351 * The mailbox command can be submitted in polling mode, in which case
3352 * this function will wait in a polling loop for the completion of the
3353 * mailbox.
3354 * If the mailbox is submitted in no_wait mode (not polling) the
3355 * function will submit the command and returns immediately without waiting
3356 * for the mailbox completion. The no_wait is supported only when HBA
3357 * is in SLI2/SLI3 mode - interrupts are enabled.
3358 * The SLI interface allows only one mailbox pending at a time. If the
3359 * mailbox is issued in polling mode and there is already a mailbox
3360 * pending, then the function will return an error. If the mailbox is issued
3361 * in NO_WAIT mode and there is a mailbox pending already, the function
3362 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
3363 * The sli layer owns the mailbox object until the completion of mailbox
3364 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
3365 * return codes the caller owns the mailbox command after the return of
3366 * the function.
3367 **/
dea31012005-04-17 16:05:31 -05003368int
James Smart2e0fef82007-06-17 19:56:36 -05003369lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
dea31012005-04-17 16:05:31 -05003370{
dea31012005-04-17 16:05:31 -05003371 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05003372 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003373 uint32_t status, evtctr;
3374 uint32_t ha_copy;
3375 int i;
James Smart09372822008-01-11 01:52:54 -05003376 unsigned long timeout;
dea31012005-04-17 16:05:31 -05003377 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04003378 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05003379 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04003380 int processing_queue = 0;
3381
3382 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3383 if (!pmbox) {
3384 /* processing mbox queue from intr_handler */
3385 processing_queue = 1;
3386 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3387 pmbox = lpfc_mbox_get(phba);
3388 if (!pmbox) {
3389 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3390 return MBX_SUCCESS;
3391 }
3392 }
dea31012005-04-17 16:05:31 -05003393
James Smarted957682007-06-17 19:56:37 -05003394 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05003395 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05003396 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04003397 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05003398 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003399 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003400 "1806 Mbox x%x failed. No vport\n",
James Smarted957682007-06-17 19:56:37 -05003401 pmbox->mb.mbxCommand);
3402 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04003403 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05003404 }
3405 }
3406
Linas Vepstas8d63f372007-02-14 14:28:36 -06003407 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04003408 if (unlikely(pci_channel_offline(phba->pcidev))) {
3409 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3410 goto out_not_finished;
3411 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06003412
dea31012005-04-17 16:05:31 -05003413 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05003414
dea31012005-04-17 16:05:31 -05003415 mb = &pmbox->mb;
3416 status = MBX_SUCCESS;
3417
James Smart2e0fef82007-06-17 19:56:36 -05003418 if (phba->link_state == LPFC_HBA_ERROR) {
3419 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003420
3421 /* Mbox command <mbxCommand> cannot issue */
James Smart7f5f3d02008-02-08 18:50:14 -05003422 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003423 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003424 }
3425
James Smart92908312006-03-07 15:04:13 -05003426 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
3427 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05003428 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart7f5f3d02008-02-08 18:50:14 -05003429 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003430 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05003431 }
3432
dea31012005-04-17 16:05:31 -05003433 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
3434 /* Polling for a mbox command when another one is already active
3435 * is not allowed in SLI. Also, the driver must have established
3436 * SLI2 mode to queue and process multiple mbox commands.
3437 */
3438
3439 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05003440 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003441
3442 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003443 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003444 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003445 }
3446
3447 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003448 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003449 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003450 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003451 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003452 }
3453
dea31012005-04-17 16:05:31 -05003454 /* Another mailbox command is still being processed, queue this
3455 * command to be processed later.
3456 */
3457 lpfc_mbox_put(phba, pmbox);
3458
3459 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05003460 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003461 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003462 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003463 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
3464 mb->mbxCommand, phba->pport->port_state,
3465 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003466
3467 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05003468 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003469
James Smart858c9f62007-06-17 19:56:39 -05003470 if (pmbox->vport) {
3471 lpfc_debugfs_disc_trc(pmbox->vport,
3472 LPFC_DISC_TRC_MBOX_VPORT,
3473 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
3474 (uint32_t)mb->mbxCommand,
3475 mb->un.varWords[0], mb->un.varWords[1]);
3476 }
3477 else {
3478 lpfc_debugfs_disc_trc(phba->pport,
3479 LPFC_DISC_TRC_MBOX,
3480 "MBOX Bsy: cmd:x%x mb:x%x x%x",
3481 (uint32_t)mb->mbxCommand,
3482 mb->un.varWords[0], mb->un.varWords[1]);
3483 }
3484
James Smart2e0fef82007-06-17 19:56:36 -05003485 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05003486 }
3487
dea31012005-04-17 16:05:31 -05003488 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3489
3490 /* If we are not polling, we MUST be in SLI2 mode */
3491 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05003492 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
3493 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05003494 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003495 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05003496 /* Mbox command <mbxCommand> cannot issue */
James Smart92d7f7b2007-06-17 19:56:38 -05003497 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
James Smart58da1ff2008-04-07 10:15:56 -04003498 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003499 }
3500 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04003501 mod_timer(&psli->mbox_tmo, (jiffies +
3502 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05003503 }
3504
3505 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05003506 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003507 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003508 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003509 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05003510 mb->mbxCommand, phba->pport->port_state,
3511 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05003512
James Smart858c9f62007-06-17 19:56:39 -05003513 if (mb->mbxCommand != MBX_HEARTBEAT) {
3514 if (pmbox->vport) {
3515 lpfc_debugfs_disc_trc(pmbox->vport,
3516 LPFC_DISC_TRC_MBOX_VPORT,
3517 "MBOX Send vport: cmd:x%x mb:x%x x%x",
3518 (uint32_t)mb->mbxCommand,
3519 mb->un.varWords[0], mb->un.varWords[1]);
3520 }
3521 else {
3522 lpfc_debugfs_disc_trc(phba->pport,
3523 LPFC_DISC_TRC_MBOX,
3524 "MBOX Send: cmd:x%x mb:x%x x%x",
3525 (uint32_t)mb->mbxCommand,
3526 mb->un.varWords[0], mb->un.varWords[1]);
3527 }
3528 }
3529
dea31012005-04-17 16:05:31 -05003530 psli->slistat.mbox_cmd++;
3531 evtctr = psli->slistat.mbox_event;
3532
3533 /* next set own bit for the adapter and copy over command word */
3534 mb->mbxOwner = OWN_CHIP;
3535
3536 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003537 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04003538 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003539 } else {
James Smart92908312006-03-07 15:04:13 -05003540 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05003541 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04003542 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003543 }
3544
3545 /* First copy mbox command data to HBA SLIM, skip past first
3546 word */
3547 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3548 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
3549 MAILBOX_CMD_SIZE - sizeof (uint32_t));
3550
3551 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04003552 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05003553 to_slim = phba->MBslimaddr;
3554 writel(ldata, to_slim);
3555 readl(to_slim); /* flush */
3556
3557 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3558 /* switch over to host mailbox */
3559 psli->sli_flag |= LPFC_SLI2_ACTIVE;
3560 }
3561 }
3562
3563 wmb();
dea31012005-04-17 16:05:31 -05003564
3565 switch (flag) {
3566 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05003567 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05003568 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05003569 /* Interrupt board to do it */
3570 writel(CA_MBATT, phba->CAregaddr);
3571 readl(phba->CAregaddr); /* flush */
3572 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05003573 break;
3574
3575 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05003576 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05003577 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05003578 /* Interrupt board to do it */
3579 writel(CA_MBATT, phba->CAregaddr);
3580 readl(phba->CAregaddr); /* flush */
3581
dea31012005-04-17 16:05:31 -05003582 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3583 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04003584 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003585 word0 = le32_to_cpu(word0);
3586 } else {
3587 /* First read mbox status word */
3588 word0 = readl(phba->MBslimaddr);
3589 }
3590
3591 /* Read the HBA Host Attention Register */
3592 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05003593 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3594 mb->mbxCommand) *
3595 1000) + jiffies;
3596 i = 0;
dea31012005-04-17 16:05:31 -05003597 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003598 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
3599 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05003600 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05003601 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05003602 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003603 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05003604 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04003605 goto out_not_finished;
dea31012005-04-17 16:05:31 -05003606 }
3607
3608 /* Check if we took a mbox interrupt while we were
3609 polling */
3610 if (((word0 & OWN_CHIP) != OWN_CHIP)
3611 && (evtctr != psli->slistat.mbox_event))
3612 break;
3613
James Smart09372822008-01-11 01:52:54 -05003614 if (i++ > 10) {
3615 spin_unlock_irqrestore(&phba->hbalock,
3616 drvr_flag);
3617 msleep(1);
3618 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3619 }
dea31012005-04-17 16:05:31 -05003620
3621 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3622 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04003623 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05003624 word0 = le32_to_cpu(word0);
3625 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3626 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04003627 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05003628 /* Check real SLIM for any errors */
3629 slimword0 = readl(phba->MBslimaddr);
3630 slimmb = (MAILBOX_t *) & slimword0;
3631 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
3632 && slimmb->mbxStatus) {
3633 psli->sli_flag &=
3634 ~LPFC_SLI2_ACTIVE;
3635 word0 = slimword0;
3636 }
3637 }
3638 } else {
3639 /* First copy command data */
3640 word0 = readl(phba->MBslimaddr);
3641 }
3642 /* Read the HBA Host Attention Register */
3643 ha_copy = readl(phba->HAregaddr);
3644 }
3645
3646 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05003647 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04003648 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05003649 } else {
3650 /* First copy command data */
3651 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
3652 MAILBOX_CMD_SIZE);
3653 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
3654 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05003655 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05003656 phba->MBslimaddr + DMP_RSP_OFFSET,
3657 mb->un.varDmp.word_cnt);
3658 }
3659 }
3660
3661 writel(HA_MBATT, phba->HAregaddr);
3662 readl(phba->HAregaddr); /* flush */
3663
3664 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3665 status = mb->mbxStatus;
3666 }
3667
James Smart2e0fef82007-06-17 19:56:36 -05003668 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3669 return status;
James Smart58da1ff2008-04-07 10:15:56 -04003670
3671out_not_finished:
3672 if (processing_queue) {
3673 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
3674 lpfc_mbox_cmpl_put(phba, pmbox);
3675 }
3676 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05003677}
3678
James Smarte59058c2008-08-24 21:49:00 -04003679/**
3680 * __lpfc_sli_ringtx_put: Add an iocb to the txq.
3681 * @phba: Pointer to HBA context object.
3682 * @pring: Pointer to driver SLI ring object.
3683 * @piocb: Pointer to address of newly added command iocb.
3684 *
3685 * This function is called with hbalock held to add a command
3686 * iocb to the txq when SLI layer cannot submit the command iocb
3687 * to the ring.
3688 **/
James Smart858c9f62007-06-17 19:56:39 -05003689static void
James Smart92d7f7b2007-06-17 19:56:38 -05003690__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003691 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05003692{
3693 /* Insert the caller's iocb in the txq tail for later processing. */
3694 list_add_tail(&piocb->list, &pring->txq);
3695 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05003696}
3697
James Smarte59058c2008-08-24 21:49:00 -04003698/**
3699 * lpfc_sli_next_iocb: Get the next iocb in the txq.
3700 * @phba: Pointer to HBA context object.
3701 * @pring: Pointer to driver SLI ring object.
3702 * @piocb: Pointer to address of newly added command iocb.
3703 *
3704 * This function is called with hbalock held before a new
3705 * iocb is submitted to the firmware. This function checks
3706 * txq to flush the iocbs in txq to Firmware before
3707 * submitting new iocbs to the Firmware.
3708 * If there are iocbs in the txq which need to be submitted
3709 * to firmware, lpfc_sli_next_iocb returns the first element
3710 * of the txq after dequeuing it from txq.
3711 * If there is no iocb in the txq then the function will return
3712 * *piocb and *piocb is set to NULL. Caller needs to check
3713 * *piocb to find if there are more commands in the txq.
3714 **/
dea31012005-04-17 16:05:31 -05003715static struct lpfc_iocbq *
3716lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05003717 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05003718{
3719 struct lpfc_iocbq * nextiocb;
3720
3721 nextiocb = lpfc_sli_ringtx_get(phba, pring);
3722 if (!nextiocb) {
3723 nextiocb = *piocb;
3724 *piocb = NULL;
3725 }
3726
3727 return nextiocb;
3728}
3729
James Smarte59058c2008-08-24 21:49:00 -04003730/**
3731 * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
3732 * @phba: Pointer to HBA context object.
3733 * @pring: Pointer to driver SLI ring object.
3734 * @piocb: Pointer to command iocb.
3735 * @flag: Flag indicating if this command can be put into txq.
3736 *
3737 * __lpfc_sli_issue_iocb is used by other functions in the driver
3738 * to issue an iocb command to the HBA. If the PCI slot is recovering
3739 * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
3740 * flag is turned on, the function returns IOCB_ERROR.
3741 * When the link is down, this function allows only iocbs for
3742 * posting buffers.
3743 * This function finds next available slot in the command ring and
3744 * posts the command to the available slot and writes the port
3745 * attention register to request HBA start processing new iocb.
3746 * If there is no slot available in the ring and
3747 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
3748 * txq, otherwise the function returns IOCB_BUSY.
3749 *
3750 * This function is called with hbalock held.
3751 * The function will return success after it successfully submit the
3752 * iocb to firmware or after adding to the txq.
3753 **/
James Smart98c9ea52007-10-27 13:37:33 -04003754static int
James Smart92d7f7b2007-06-17 19:56:38 -05003755__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003756 struct lpfc_iocbq *piocb, uint32_t flag)
3757{
3758 struct lpfc_iocbq *nextiocb;
3759 IOCB_t *iocb;
3760
James Smart92d7f7b2007-06-17 19:56:38 -05003761 if (piocb->iocb_cmpl && (!piocb->vport) &&
3762 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
3763 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
3764 lpfc_printf_log(phba, KERN_ERR,
3765 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003766 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003767 piocb->iocb.ulpCommand);
3768 dump_stack();
3769 return IOCB_ERROR;
3770 }
3771
3772
Linas Vepstas8d63f372007-02-14 14:28:36 -06003773 /* If the PCI channel is in offline state, do not post iocbs. */
3774 if (unlikely(pci_channel_offline(phba->pcidev)))
3775 return IOCB_ERROR;
3776
dea31012005-04-17 16:05:31 -05003777 /*
3778 * We should never get an IOCB if we are in a < LINK_DOWN state
3779 */
James Smart2e0fef82007-06-17 19:56:36 -05003780 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05003781 return IOCB_ERROR;
3782
3783 /*
3784 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04003785 * outstanding event.
dea31012005-04-17 16:05:31 -05003786 */
James Smart0b727fe2007-10-27 13:37:25 -04003787 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05003788 goto iocb_busy;
3789
James Smart2e0fef82007-06-17 19:56:36 -05003790 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05003791 /*
James Smart2680eea2007-04-25 09:52:55 -04003792 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05003793 * can be issued if the link is not up.
3794 */
3795 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04003796 case CMD_GEN_REQUEST64_CR:
3797 case CMD_GEN_REQUEST64_CX:
3798 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
3799 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
3800 FC_FCP_CMND) ||
3801 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
3802 MENLO_TRANSPORT_TYPE))
3803
3804 goto iocb_busy;
3805 break;
dea31012005-04-17 16:05:31 -05003806 case CMD_QUE_RING_BUF_CN:
3807 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05003808 /*
3809 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
3810 * completion, iocb_cmpl MUST be 0.
3811 */
3812 if (piocb->iocb_cmpl)
3813 piocb->iocb_cmpl = NULL;
3814 /*FALLTHROUGH*/
3815 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04003816 case CMD_CLOSE_XRI_CN:
3817 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05003818 break;
3819 default:
3820 goto iocb_busy;
3821 }
3822
3823 /*
3824 * For FCP commands, we must be in a state where we can process link
3825 * attention events.
3826 */
3827 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05003828 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05003829 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05003830 }
dea31012005-04-17 16:05:31 -05003831
dea31012005-04-17 16:05:31 -05003832 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
3833 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
3834 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
3835
3836 if (iocb)
3837 lpfc_sli_update_ring(phba, pring);
3838 else
3839 lpfc_sli_update_full_ring(phba, pring);
3840
3841 if (!piocb)
3842 return IOCB_SUCCESS;
3843
3844 goto out_busy;
3845
3846 iocb_busy:
3847 pring->stats.iocb_cmd_delay++;
3848
3849 out_busy:
3850
3851 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003852 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05003853 return IOCB_SUCCESS;
3854 }
3855
3856 return IOCB_BUSY;
3857}
3858
James Smart92d7f7b2007-06-17 19:56:38 -05003859
James Smarte59058c2008-08-24 21:49:00 -04003860/**
3861 * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
3862 * @phba: Pointer to HBA context object.
3863 * @pring: Pointer to driver SLI ring object.
3864 * @piocb: Pointer to command iocb.
3865 * @flag: Flag indicating if this command can be put into txq.
3866 *
3867 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
3868 * function. This function gets the hbalock and calls
3869 * __lpfc_sli_issue_iocb function and will return the error returned
3870 * by __lpfc_sli_issue_iocb function. This wrapper is used by
3871 * functions which do not hold hbalock.
3872 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003873int
3874lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3875 struct lpfc_iocbq *piocb, uint32_t flag)
3876{
3877 unsigned long iflags;
3878 int rc;
3879
3880 spin_lock_irqsave(&phba->hbalock, iflags);
3881 rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
3882 spin_unlock_irqrestore(&phba->hbalock, iflags);
3883
3884 return rc;
3885}
3886
James Smarte59058c2008-08-24 21:49:00 -04003887/**
3888 * lpfc_extra_ring_setup: Extra ring setup function.
3889 * @phba: Pointer to HBA context object.
3890 *
3891 * This function is called while driver attaches with the
3892 * HBA to setup the extra ring. The extra ring is used
3893 * only when driver needs to support target mode functionality
3894 * or IP over FC functionalities.
3895 *
3896 * This function is called with no lock held.
3897 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003898static int
3899lpfc_extra_ring_setup( struct lpfc_hba *phba)
3900{
3901 struct lpfc_sli *psli;
3902 struct lpfc_sli_ring *pring;
3903
3904 psli = &phba->sli;
3905
3906 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05003907
3908 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003909 pring = &psli->ring[psli->fcp_ring];
3910 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3911 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3912 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3913 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3914
James Smarta4bc3372006-12-02 13:34:16 -05003915 /* and give them to the extra ring */
3916 pring = &psli->ring[psli->extra_ring];
3917
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003918 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3919 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3920 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3921 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3922
3923 /* Setup default profile for this ring */
3924 pring->iotag_max = 4096;
3925 pring->num_mask = 1;
3926 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05003927 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
3928 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05003929 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
3930 return 0;
3931}
3932
James Smarte59058c2008-08-24 21:49:00 -04003933/**
3934 * lpfc_sli_async_event_handler: ASYNC iocb handler function.
3935 * @phba: Pointer to HBA context object.
3936 * @pring: Pointer to driver SLI ring object.
3937 * @iocbq: Pointer to iocb object.
3938 *
3939 * This function is called by the slow ring event handler
3940 * function when there is an ASYNC event iocb in the ring.
3941 * This function is called with no lock held.
3942 * Currently this function handles only temperature related
3943 * ASYNC events. The function decodes the temperature sensor
3944 * event message and posts events for the management applications.
3945 **/
James Smart98c9ea52007-10-27 13:37:33 -04003946static void
James Smart57127f12007-10-27 13:37:05 -04003947lpfc_sli_async_event_handler(struct lpfc_hba * phba,
3948 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
3949{
3950 IOCB_t *icmd;
3951 uint16_t evt_code;
3952 uint16_t temp;
3953 struct temp_event temp_event_data;
3954 struct Scsi_Host *shost;
3955
3956 icmd = &iocbq->iocb;
3957 evt_code = icmd->un.asyncstat.evt_code;
3958 temp = icmd->ulpContext;
3959
3960 if ((evt_code != ASYNC_TEMP_WARN) &&
3961 (evt_code != ASYNC_TEMP_SAFE)) {
3962 lpfc_printf_log(phba,
3963 KERN_ERR,
3964 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04003965 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smart57127f12007-10-27 13:37:05 -04003966 " evt_code 0x%x\n",
3967 pring->ringno,
3968 icmd->un.asyncstat.evt_code);
3969 return;
3970 }
3971 temp_event_data.data = (uint32_t)temp;
3972 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
3973 if (evt_code == ASYNC_TEMP_WARN) {
3974 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
3975 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003976 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003977 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04003978 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04003979 "corrective action. temperature : %d Celsius\n",
3980 temp);
3981 }
3982 if (evt_code == ASYNC_TEMP_SAFE) {
3983 temp_event_data.event_code = LPFC_NORMAL_TEMP;
3984 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05003985 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04003986 LOG_TEMP,
3987 "0340 Adapter temperature is OK now. "
3988 "temperature : %d Celsius\n",
3989 temp);
3990 }
3991
3992 /* Send temperature change event to applications */
3993 shost = lpfc_shost_from_vport(phba->pport);
3994 fc_host_post_vendor_event(shost, fc_get_event_number(),
3995 sizeof(temp_event_data), (char *) &temp_event_data,
3996 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
3997
3998}
3999
4000
James Smarte59058c2008-08-24 21:49:00 -04004001/**
4002 * lpfc_sli_setup: SLI ring setup function.
4003 * @phba: Pointer to HBA context object.
4004 *
4005 * lpfc_sli_setup sets up rings of the SLI interface with
4006 * number of iocbs per ring and iotags. This function is
4007 * called while driver attach to the HBA and before the
4008 * interrupts are enabled. So there is no need for locking.
4009 *
4010 * This function always returns 0.
4011 **/
dea31012005-04-17 16:05:31 -05004012int
4013lpfc_sli_setup(struct lpfc_hba *phba)
4014{
James Smarted957682007-06-17 19:56:37 -05004015 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05004016 struct lpfc_sli *psli = &phba->sli;
4017 struct lpfc_sli_ring *pring;
4018
4019 psli->num_rings = MAX_CONFIGURED_RINGS;
4020 psli->sli_flag = 0;
4021 psli->fcp_ring = LPFC_FCP_RING;
4022 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05004023 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05004024
James Bottomley604a3e32005-10-29 10:28:33 -05004025 psli->iocbq_lookup = NULL;
4026 psli->iocbq_lookup_len = 0;
4027 psli->last_iotag = 0;
4028
dea31012005-04-17 16:05:31 -05004029 for (i = 0; i < psli->num_rings; i++) {
4030 pring = &psli->ring[i];
4031 switch (i) {
4032 case LPFC_FCP_RING: /* ring 0 - FCP */
4033 /* numCiocb and numRiocb are used in config_port */
4034 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
4035 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
4036 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
4037 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
4038 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
4039 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004040 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004041 SLI3_IOCB_CMD_SIZE :
4042 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004043 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004044 SLI3_IOCB_RSP_SIZE :
4045 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05004046 pring->iotag_ctr = 0;
4047 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05004048 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05004049 pring->fast_iotag = pring->iotag_max;
4050 pring->num_mask = 0;
4051 break;
James Smarta4bc3372006-12-02 13:34:16 -05004052 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05004053 /* numCiocb and numRiocb are used in config_port */
4054 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
4055 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004056 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004057 SLI3_IOCB_CMD_SIZE :
4058 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004059 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004060 SLI3_IOCB_RSP_SIZE :
4061 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05004062 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05004063 pring->num_mask = 0;
4064 break;
4065 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
4066 /* numCiocb and numRiocb are used in config_port */
4067 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
4068 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05004069 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004070 SLI3_IOCB_CMD_SIZE :
4071 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05004072 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05004073 SLI3_IOCB_RSP_SIZE :
4074 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05004075 pring->fast_iotag = 0;
4076 pring->iotag_ctr = 0;
4077 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04004078 pring->lpfc_sli_rcv_async_status =
4079 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05004080 pring->num_mask = 4;
4081 pring->prt[0].profile = 0; /* Mask 0 */
4082 pring->prt[0].rctl = FC_ELS_REQ;
4083 pring->prt[0].type = FC_ELS_DATA;
4084 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004085 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004086 pring->prt[1].profile = 0; /* Mask 1 */
4087 pring->prt[1].rctl = FC_ELS_RSP;
4088 pring->prt[1].type = FC_ELS_DATA;
4089 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004090 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05004091 pring->prt[2].profile = 0; /* Mask 2 */
4092 /* NameServer Inquiry */
4093 pring->prt[2].rctl = FC_UNSOL_CTL;
4094 /* NameServer */
4095 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
4096 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004097 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004098 pring->prt[3].profile = 0; /* Mask 3 */
4099 /* NameServer response */
4100 pring->prt[3].rctl = FC_SOL_CTL;
4101 /* NameServer */
4102 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
4103 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05004104 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05004105 break;
4106 }
James Smarted957682007-06-17 19:56:37 -05004107 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05004108 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05004109 }
James Smarted957682007-06-17 19:56:37 -05004110 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05004111 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04004112 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
4113 "SLI2 SLIM Data: x%x x%lx\n",
4114 phba->brd_no, totiocbsize,
4115 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05004116 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05004117 if (phba->cfg_multi_ring_support == 2)
4118 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05004119
4120 return 0;
4121}
4122
James Smarte59058c2008-08-24 21:49:00 -04004123/**
4124 * lpfc_sli_queue_setup: Queue initialization function.
4125 * @phba: Pointer to HBA context object.
4126 *
4127 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
4128 * ring. This function also initializes ring indices of each ring.
4129 * This function is called during the initialization of the SLI
4130 * interface of an HBA.
4131 * This function is called with no lock held and always returns
4132 * 1.
4133 **/
dea31012005-04-17 16:05:31 -05004134int
James Smart2e0fef82007-06-17 19:56:36 -05004135lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004136{
4137 struct lpfc_sli *psli;
4138 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05004139 int i;
dea31012005-04-17 16:05:31 -05004140
4141 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05004142 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004143 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05004144 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05004145 /* Initialize list headers for txq and txcmplq as double linked lists */
4146 for (i = 0; i < psli->num_rings; i++) {
4147 pring = &psli->ring[i];
4148 pring->ringno = i;
4149 pring->next_cmdidx = 0;
4150 pring->local_getidx = 0;
4151 pring->cmdidx = 0;
4152 INIT_LIST_HEAD(&pring->txq);
4153 INIT_LIST_HEAD(&pring->txcmplq);
4154 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05004155 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05004156 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05004157 }
James Smart2e0fef82007-06-17 19:56:36 -05004158 spin_unlock_irq(&phba->hbalock);
4159 return 1;
dea31012005-04-17 16:05:31 -05004160}
4161
James Smarte59058c2008-08-24 21:49:00 -04004162/**
4163 * lpfc_sli_host_down: Vport cleanup function.
4164 * @vport: Pointer to virtual port object.
4165 *
4166 * lpfc_sli_host_down is called to clean up the resources
4167 * associated with a vport before destroying virtual
4168 * port data structures.
4169 * This function does following operations:
4170 * - Free discovery resources associated with this virtual
4171 * port.
4172 * - Free iocbs associated with this virtual port in
4173 * the txq.
4174 * - Send abort for all iocb commands associated with this
4175 * vport in txcmplq.
4176 *
4177 * This function is called with no lock held and always returns 1.
4178 **/
dea31012005-04-17 16:05:31 -05004179int
James Smart92d7f7b2007-06-17 19:56:38 -05004180lpfc_sli_host_down(struct lpfc_vport *vport)
4181{
James Smart858c9f62007-06-17 19:56:39 -05004182 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004183 struct lpfc_hba *phba = vport->phba;
4184 struct lpfc_sli *psli = &phba->sli;
4185 struct lpfc_sli_ring *pring;
4186 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004187 int i;
4188 unsigned long flags = 0;
4189 uint16_t prev_pring_flag;
4190
4191 lpfc_cleanup_discovery_resources(vport);
4192
4193 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004194 for (i = 0; i < psli->num_rings; i++) {
4195 pring = &psli->ring[i];
4196 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04004197 /* Only slow rings */
4198 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004199 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004200 /* Set the lpfc data pending flag */
4201 set_bit(LPFC_DATA_READY, &phba->data_flags);
4202 }
James Smart92d7f7b2007-06-17 19:56:38 -05004203 /*
4204 * Error everything on the txq since these iocbs have not been
4205 * given to the FW yet.
4206 */
James Smart92d7f7b2007-06-17 19:56:38 -05004207 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4208 if (iocb->vport != vport)
4209 continue;
James Smart858c9f62007-06-17 19:56:39 -05004210 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004211 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05004212 }
4213
4214 /* Next issue ABTS for everything on the txcmplq */
4215 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
4216 list) {
4217 if (iocb->vport != vport)
4218 continue;
4219 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4220 }
4221
4222 pring->flag = prev_pring_flag;
4223 }
4224
4225 spin_unlock_irqrestore(&phba->hbalock, flags);
4226
James Smart858c9f62007-06-17 19:56:39 -05004227 while (!list_empty(&completions)) {
4228 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
4229
4230 if (!iocb->iocb_cmpl)
4231 lpfc_sli_release_iocbq(phba, iocb);
4232 else {
4233 iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4234 iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
4235 (iocb->iocb_cmpl) (phba, iocb, iocb);
4236 }
4237 }
James Smart92d7f7b2007-06-17 19:56:38 -05004238 return 1;
4239}
4240
James Smarte59058c2008-08-24 21:49:00 -04004241/**
4242 * lpfc_sli_hba_down: Resource cleanup function for the HBA.
4243 * @phba: Pointer to HBA context object.
4244 *
4245 * This function cleans up all iocb, buffers, mailbox commands
4246 * while shutting down the HBA. This function is called with no
4247 * lock held and always returns 1.
4248 * This function does the following to cleanup driver resources:
4249 * - Free discovery resources for each virtual port
4250 * - Cleanup any pending fabric iocbs
4251 * - Iterate through the iocb txq and free each entry
4252 * in the list.
4253 * - Free up any buffer posted to the HBA
4254 * - Free mailbox commands in the mailbox queue.
4255 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004256int
James Smart2e0fef82007-06-17 19:56:36 -05004257lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004258{
James Smart2534ba72007-04-25 09:52:20 -04004259 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05004260 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004261 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05004262 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05004263 LPFC_MBOXQ_t *pmb;
James Smart2534ba72007-04-25 09:52:20 -04004264 struct lpfc_iocbq *iocb;
4265 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004266 int i;
4267 unsigned long flags = 0;
4268
dea31012005-04-17 16:05:31 -05004269 lpfc_hba_down_prep(phba);
4270
James Smart92d7f7b2007-06-17 19:56:38 -05004271 lpfc_fabric_abort_hba(phba);
4272
James Smart2e0fef82007-06-17 19:56:36 -05004273 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004274 for (i = 0; i < psli->num_rings; i++) {
4275 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04004276 /* Only slow rings */
4277 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05004278 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04004279 /* Set the lpfc data pending flag */
4280 set_bit(LPFC_DATA_READY, &phba->data_flags);
4281 }
dea31012005-04-17 16:05:31 -05004282
4283 /*
4284 * Error everything on the txq since these iocbs have not been
4285 * given to the FW yet.
4286 */
James Smart2534ba72007-04-25 09:52:20 -04004287 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05004288 pring->txq_cnt = 0;
4289
dea31012005-04-17 16:05:31 -05004290 }
James Smart2e0fef82007-06-17 19:56:36 -05004291 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05004292
James Smart2534ba72007-04-25 09:52:20 -04004293 while (!list_empty(&completions)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004294 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
James Smart2534ba72007-04-25 09:52:20 -04004295 cmd = &iocb->iocb;
James Smart2534ba72007-04-25 09:52:20 -04004296
James Smart2e0fef82007-06-17 19:56:36 -05004297 if (!iocb->iocb_cmpl)
4298 lpfc_sli_release_iocbq(phba, iocb);
4299 else {
James Smart2534ba72007-04-25 09:52:20 -04004300 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4301 cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
4302 (iocb->iocb_cmpl) (phba, iocb, iocb);
James Smart2e0fef82007-06-17 19:56:36 -05004303 }
James Smart2534ba72007-04-25 09:52:20 -04004304 }
4305
James Smart0ff10d42008-01-11 01:52:36 -05004306 spin_lock_irqsave(&phba->hbalock, flags);
4307 list_splice_init(&phba->elsbuf, &completions);
4308 phba->elsbuf_cnt = 0;
4309 phba->elsbuf_prev_cnt = 0;
4310 spin_unlock_irqrestore(&phba->hbalock, flags);
4311
4312 while (!list_empty(&completions)) {
4313 list_remove_head(&completions, buf_ptr,
4314 struct lpfc_dmabuf, list);
4315 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4316 kfree(buf_ptr);
4317 }
4318
dea31012005-04-17 16:05:31 -05004319 /* Return any active mbox cmds */
4320 del_timer_sync(&psli->mbox_tmo);
James Smarted957682007-06-17 19:56:37 -05004321 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05004322
4323 spin_lock(&phba->pport->work_port_lock);
4324 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4325 spin_unlock(&phba->pport->work_port_lock);
4326
James Smart97eab632008-04-07 10:16:05 -04004327 /* Return any pending or completed mbox cmds */
4328 list_splice_init(&phba->sli.mboxq, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004329 if (psli->mbox_active) {
4330 list_add_tail(&psli->mbox_active->list, &completions);
James Smart2e0fef82007-06-17 19:56:36 -05004331 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004332 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05004333 }
James Smart92d7f7b2007-06-17 19:56:38 -05004334 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05004335 spin_unlock_irqrestore(&phba->hbalock, flags);
4336
4337 while (!list_empty(&completions)) {
4338 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
dea31012005-04-17 16:05:31 -05004339 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
James Smart97eab632008-04-07 10:16:05 -04004340 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05004341 pmb->mbox_cmpl(phba,pmb);
dea31012005-04-17 16:05:31 -05004342 }
dea31012005-04-17 16:05:31 -05004343 return 1;
4344}
4345
James Smarte59058c2008-08-24 21:49:00 -04004346/**
4347 * lpfc_sli_pcimem_bcopy: SLI memory copy function.
4348 * @srcp: Source memory pointer.
4349 * @destp: Destination memory pointer.
4350 * @cnt: Number of words required to be copied.
4351 *
4352 * This function is used for copying data between driver memory
4353 * and the SLI memory. This function also changes the endianness
4354 * of each word if native endianness is different from SLI
4355 * endianness. This function can be called with or without
4356 * lock.
4357 **/
dea31012005-04-17 16:05:31 -05004358void
4359lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
4360{
4361 uint32_t *src = srcp;
4362 uint32_t *dest = destp;
4363 uint32_t ldata;
4364 int i;
4365
4366 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
4367 ldata = *src;
4368 ldata = le32_to_cpu(ldata);
4369 *dest = ldata;
4370 src++;
4371 dest++;
4372 }
4373}
4374
James Smarte59058c2008-08-24 21:49:00 -04004375
4376/**
4377 * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
4378 * @phba: Pointer to HBA context object.
4379 * @pring: Pointer to driver SLI ring object.
4380 * @mp: Pointer to driver buffer object.
4381 *
4382 * This function is called with no lock held.
4383 * It always return zero after adding the buffer to the postbufq
4384 * buffer list.
4385 **/
dea31012005-04-17 16:05:31 -05004386int
James Smart2e0fef82007-06-17 19:56:36 -05004387lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4388 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05004389{
4390 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
4391 later */
James Smart2e0fef82007-06-17 19:56:36 -05004392 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004393 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05004394 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05004395 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004396 return 0;
4397}
4398
James Smarte59058c2008-08-24 21:49:00 -04004399/**
4400 * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
4401 * using CMD_QUE_XRI64_CX iocb.
4402 * @phba: Pointer to HBA context object.
4403 *
4404 * When HBQ is enabled, buffers are searched based on tags. This function
4405 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
4406 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
4407 * does not conflict with tags of buffer posted for unsolicited events.
4408 * The function returns the allocated tag. The function is called with
4409 * no locks held.
4410 **/
James Smart76bb24e2007-10-27 13:38:00 -04004411uint32_t
4412lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
4413{
4414 spin_lock_irq(&phba->hbalock);
4415 phba->buffer_tag_count++;
4416 /*
4417 * Always set the QUE_BUFTAG_BIT to distiguish between
4418 * a tag assigned by HBQ.
4419 */
4420 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
4421 spin_unlock_irq(&phba->hbalock);
4422 return phba->buffer_tag_count;
4423}
4424
James Smarte59058c2008-08-24 21:49:00 -04004425/**
4426 * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
4427 * posted using CMD_QUE_XRI64_CX iocb.
4428 * @phba: Pointer to HBA context object.
4429 * @pring: Pointer to driver SLI ring object.
4430 * @tag: Buffer tag.
4431 *
4432 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
4433 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
4434 * iocb is posted to the response ring with the tag of the buffer.
4435 * This function searches the pring->postbufq list using the tag
4436 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
4437 * iocb. If the buffer is found then lpfc_dmabuf object of the
4438 * buffer is returned to the caller else NULL is returned.
4439 * This function is called with no lock held.
4440 **/
James Smart76bb24e2007-10-27 13:38:00 -04004441struct lpfc_dmabuf *
4442lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4443 uint32_t tag)
4444{
4445 struct lpfc_dmabuf *mp, *next_mp;
4446 struct list_head *slp = &pring->postbufq;
4447
4448 /* Search postbufq, from the begining, looking for a match on tag */
4449 spin_lock_irq(&phba->hbalock);
4450 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4451 if (mp->buffer_tag == tag) {
4452 list_del_init(&mp->list);
4453 pring->postbufq_cnt--;
4454 spin_unlock_irq(&phba->hbalock);
4455 return mp;
4456 }
4457 }
4458
4459 spin_unlock_irq(&phba->hbalock);
4460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04004461 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04004462 "ring %d Data x%lx x%p x%p x%x\n",
4463 pring->ringno, (unsigned long) tag,
4464 slp->next, slp->prev, pring->postbufq_cnt);
4465
4466 return NULL;
4467}
dea31012005-04-17 16:05:31 -05004468
James Smarte59058c2008-08-24 21:49:00 -04004469/**
4470 * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
4471 * unsolicited ct and els events.
4472 * @phba: Pointer to HBA context object.
4473 * @pring: Pointer to driver SLI ring object.
4474 * @phys: DMA address of the buffer.
4475 *
4476 * This function searches the buffer list using the dma_address
4477 * of unsolicited event to find the driver's lpfc_dmabuf object
4478 * corresponding to the dma_address. The function returns the
4479 * lpfc_dmabuf object if a buffer is found else it returns NULL.
4480 * This function is called by the ct and els unsolicited event
4481 * handlers to get the buffer associated with the unsolicited
4482 * event.
4483 *
4484 * This function is called with no lock held.
4485 **/
dea31012005-04-17 16:05:31 -05004486struct lpfc_dmabuf *
4487lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4488 dma_addr_t phys)
4489{
4490 struct lpfc_dmabuf *mp, *next_mp;
4491 struct list_head *slp = &pring->postbufq;
4492
4493 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05004494 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004495 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4496 if (mp->phys == phys) {
4497 list_del_init(&mp->list);
4498 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004499 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004500 return mp;
4501 }
4502 }
4503
James Smart2e0fef82007-06-17 19:56:36 -05004504 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004506 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05004507 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004508 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05004509 slp->next, slp->prev, pring->postbufq_cnt);
4510 return NULL;
4511}
4512
James Smarte59058c2008-08-24 21:49:00 -04004513/**
4514 * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
4515 * @phba: Pointer to HBA context object.
4516 * @cmdiocb: Pointer to driver command iocb object.
4517 * @rspiocb: Pointer to driver response iocb object.
4518 *
4519 * This function is the completion handler for the abort iocbs for
4520 * ELS commands. This function is called from the ELS ring event
4521 * handler with no lock held. This function frees memory resources
4522 * associated with the abort iocb.
4523 **/
dea31012005-04-17 16:05:31 -05004524static void
James Smart2e0fef82007-06-17 19:56:36 -05004525lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4526 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05004527{
James Smart2e0fef82007-06-17 19:56:36 -05004528 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04004529 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05004530 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04004531 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4532
4533 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04004534
4535 if (irsp->ulpStatus) {
4536 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
4537 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
4538
James Smart2e0fef82007-06-17 19:56:36 -05004539 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004540 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
4541 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
4542
James Smart92d7f7b2007-06-17 19:56:38 -05004543 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004544 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05004545 "with tag %x context %x, abort status %x, "
4546 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04004547 abort_iocb, abort_iotag, abort_context,
4548 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04004549
4550 /*
James Smart58da1ff2008-04-07 10:15:56 -04004551 * If the iocb is not found in Firmware queue the iocb
4552 * might have completed already. Do not free it again.
4553 */
James Smart9b379602008-04-07 10:16:00 -04004554 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04004555 spin_unlock_irq(&phba->hbalock);
4556 lpfc_sli_release_iocbq(phba, cmdiocb);
4557 return;
4558 }
4559 /*
James Smart2680eea2007-04-25 09:52:55 -04004560 * make sure we have the right iocbq before taking it
4561 * off the txcmplq and try to call completion routine.
4562 */
James Smart2e0fef82007-06-17 19:56:36 -05004563 if (!abort_iocb ||
4564 abort_iocb->iocb.ulpContext != abort_context ||
4565 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
4566 spin_unlock_irq(&phba->hbalock);
4567 else {
James Smart92d7f7b2007-06-17 19:56:38 -05004568 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04004569 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05004570 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04004571
James Smart0ff10d42008-01-11 01:52:36 -05004572 /* Firmware could still be in progress of DMAing
4573 * payload, so don't free data buffer till after
4574 * a hbeat.
4575 */
4576 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
4577
James Smart92d7f7b2007-06-17 19:56:38 -05004578 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4579 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4580 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
4581 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04004582 }
4583 }
4584
James Bottomley604a3e32005-10-29 10:28:33 -05004585 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05004586 return;
4587}
4588
James Smarte59058c2008-08-24 21:49:00 -04004589/**
4590 * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
4591 * @phba: Pointer to HBA context object.
4592 * @cmdiocb: Pointer to driver command iocb object.
4593 * @rspiocb: Pointer to driver response iocb object.
4594 *
4595 * The function is called from SLI ring event handler with no
4596 * lock held. This function is the completion handler for ELS commands
4597 * which are aborted. The function frees memory resources used for
4598 * the aborted ELS commands.
4599 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004600static void
4601lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4602 struct lpfc_iocbq *rspiocb)
4603{
4604 IOCB_t *irsp = &rspiocb->iocb;
4605
4606 /* ELS cmd tag <ulpIoTag> completes */
4607 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04004608 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004609 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004610 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05004611 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05004612 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
4613 lpfc_ct_free_iocb(phba, cmdiocb);
4614 else
4615 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05004616 return;
4617}
4618
James Smarte59058c2008-08-24 21:49:00 -04004619/**
4620 * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
4621 * @phba: Pointer to HBA context object.
4622 * @pring: Pointer to driver SLI ring object.
4623 * @cmdiocb: Pointer to driver command iocb object.
4624 *
4625 * This function issues an abort iocb for the provided command
4626 * iocb. This function is called with hbalock held.
4627 * The function returns 0 when it fails due to memory allocation
4628 * failure or when the command iocb is an abort request.
4629 **/
dea31012005-04-17 16:05:31 -05004630int
James Smart2e0fef82007-06-17 19:56:36 -05004631lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4632 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05004633{
James Smart2e0fef82007-06-17 19:56:36 -05004634 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004635 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05004636 IOCB_t *icmd = NULL;
4637 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04004638 int retval = IOCB_ERROR;
4639
James Smart92d7f7b2007-06-17 19:56:38 -05004640 /*
4641 * There are certain command types we don't want to abort. And we
4642 * don't want to abort commands that are already in the process of
4643 * being aborted.
James Smart07951072007-04-25 09:51:38 -04004644 */
4645 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004646 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05004647 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4648 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04004649 return 0;
4650
James Smart858c9f62007-06-17 19:56:39 -05004651 /* If we're unloading, don't abort iocb on the ELS ring, but change the
4652 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04004653 */
James Smart858c9f62007-06-17 19:56:39 -05004654 if ((vport->load_flag & FC_UNLOADING) &&
4655 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004656 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
4657 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
4658 else
4659 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04004660 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05004661 }
dea31012005-04-17 16:05:31 -05004662
4663 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05004664 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004665 if (abtsiocbp == NULL)
4666 return 0;
dea31012005-04-17 16:05:31 -05004667
James Smart07951072007-04-25 09:51:38 -04004668 /* This signals the response to set the correct status
4669 * before calling the completion handler.
4670 */
4671 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4672
dea31012005-04-17 16:05:31 -05004673 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04004674 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
4675 iabt->un.acxri.abortContextTag = icmd->ulpContext;
4676 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004677 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04004678 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05004679
James Smart2e0fef82007-06-17 19:56:36 -05004680 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04004681 iabt->ulpCommand = CMD_ABORT_XRI_CN;
4682 else
4683 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
4684
4685 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04004686
James Smarte8b62012007-08-02 11:10:09 -04004687 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4688 "0339 Abort xri x%x, original iotag x%x, "
4689 "abort cmd iotag x%x\n",
4690 iabt->un.acxri.abortContextTag,
4691 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smart92d7f7b2007-06-17 19:56:38 -05004692 retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04004693
James Smartd7c255b2008-08-24 21:50:00 -04004694 if (retval)
4695 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04004696abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05004697 /*
4698 * Caller to this routine should check for IOCB_ERROR
4699 * and handle it properly. This routine no longer removes
4700 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04004701 */
James Smart2e0fef82007-06-17 19:56:36 -05004702 return retval;
dea31012005-04-17 16:05:31 -05004703}
4704
James Smarte59058c2008-08-24 21:49:00 -04004705/**
4706 * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
4707 * associated with a vport/SCSI target/lun.
4708 * @iocbq: Pointer to driver iocb object.
4709 * @vport: Pointer to driver virtual port object.
4710 * @tgt_id: SCSI ID of the target.
4711 * @lun_id: LUN ID of the scsi device.
4712 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
4713 *
4714 * This function acts as iocb filter for functions which abort or count
4715 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
4716 * 0 if the filtering criteria is met for the given iocb and will return
4717 * 1 if the filtering criteria is not met.
4718 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
4719 * given iocb is for the SCSI device specified by vport, tgt_id and
4720 * lun_id parameter.
4721 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
4722 * given iocb is for the SCSI target specified by vport and tgt_id
4723 * parameters.
4724 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
4725 * given iocb is for the SCSI host associated with the given vport.
4726 * This function is called with no locks held.
4727 **/
dea31012005-04-17 16:05:31 -05004728static int
James Smart51ef4c22007-08-02 11:10:31 -04004729lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
4730 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004731 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004732{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004733 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004734 int rc = 1;
4735
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004736 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
4737 return rc;
4738
James Smart51ef4c22007-08-02 11:10:31 -04004739 if (iocbq->vport != vport)
4740 return rc;
4741
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004742 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004743
James Smart495a7142008-06-14 22:52:59 -04004744 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05004745 return rc;
4746
4747 switch (ctx_cmd) {
4748 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04004749 if ((lpfc_cmd->rdata->pnode) &&
4750 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
4751 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05004752 rc = 0;
4753 break;
4754 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04004755 if ((lpfc_cmd->rdata->pnode) &&
4756 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05004757 rc = 0;
4758 break;
dea31012005-04-17 16:05:31 -05004759 case LPFC_CTX_HOST:
4760 rc = 0;
4761 break;
4762 default:
4763 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004764 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05004765 break;
4766 }
4767
4768 return rc;
4769}
4770
James Smarte59058c2008-08-24 21:49:00 -04004771/**
4772 * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
4773 * @vport: Pointer to virtual port.
4774 * @tgt_id: SCSI ID of the target.
4775 * @lun_id: LUN ID of the scsi device.
4776 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4777 *
4778 * This function returns number of FCP commands pending for the vport.
4779 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
4780 * commands pending on the vport associated with SCSI device specified
4781 * by tgt_id and lun_id parameters.
4782 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
4783 * commands pending on the vport associated with SCSI target specified
4784 * by tgt_id parameter.
4785 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
4786 * commands pending on the vport.
4787 * This function returns the number of iocbs which satisfy the filter.
4788 * This function is called without any lock held.
4789 **/
dea31012005-04-17 16:05:31 -05004790int
James Smart51ef4c22007-08-02 11:10:31 -04004791lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
4792 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05004793{
James Smart51ef4c22007-08-02 11:10:31 -04004794 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004795 struct lpfc_iocbq *iocbq;
4796 int sum, i;
dea31012005-04-17 16:05:31 -05004797
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004798 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
4799 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004800
James Smart51ef4c22007-08-02 11:10:31 -04004801 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
4802 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004803 sum++;
dea31012005-04-17 16:05:31 -05004804 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004805
dea31012005-04-17 16:05:31 -05004806 return sum;
4807}
4808
James Smarte59058c2008-08-24 21:49:00 -04004809/**
4810 * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
4811 * FCP iocb.
4812 * @phba: Pointer to HBA context object
4813 * @cmdiocb: Pointer to command iocb object.
4814 * @rspiocb: Pointer to response iocb object.
4815 *
4816 * This function is called when an aborted FCP iocb completes. This
4817 * function is called by the ring event handler with no lock held.
4818 * This function frees the iocb.
4819 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004820void
James Smart2e0fef82007-06-17 19:56:36 -05004821lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4822 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004823{
James Bottomley604a3e32005-10-29 10:28:33 -05004824 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004825 return;
4826}
4827
James Smarte59058c2008-08-24 21:49:00 -04004828/**
4829 * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
4830 * pending on a SCSI host(vport)/target/lun.
4831 * @vport: Pointer to virtual port.
4832 * @pring: Pointer to driver SLI ring object.
4833 * @tgt_id: SCSI ID of the target.
4834 * @lun_id: LUN ID of the scsi device.
4835 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4836 *
4837 * This function sends an abort command for every SCSI command
4838 * associated with the given virtual port pending on the ring
4839 * filtered by lpfc_sli_validate_fcp_iocb function.
4840 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
4841 * FCP iocbs associated with lun specified by tgt_id and lun_id
4842 * parameters
4843 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
4844 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
4845 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
4846 * FCP iocbs associated with virtual port.
4847 * This function returns number of iocbs it failed to abort.
4848 * This function is called with no locks held.
4849 **/
dea31012005-04-17 16:05:31 -05004850int
James Smart51ef4c22007-08-02 11:10:31 -04004851lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
4852 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05004853{
James Smart51ef4c22007-08-02 11:10:31 -04004854 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004855 struct lpfc_iocbq *iocbq;
4856 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004857 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05004858 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004859 int i;
dea31012005-04-17 16:05:31 -05004860
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004861 for (i = 1; i <= phba->sli.last_iotag; i++) {
4862 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05004863
James Smart51ef4c22007-08-02 11:10:31 -04004864 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05004865 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05004866 continue;
4867
4868 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004869 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004870 if (abtsiocb == NULL) {
4871 errcnt++;
4872 continue;
4873 }
dea31012005-04-17 16:05:31 -05004874
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004875 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05004876 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
4877 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
4878 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
4879 abtsiocb->iocb.ulpLe = 1;
4880 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05004881 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05004882
James Smart2e0fef82007-06-17 19:56:36 -05004883 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004884 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
4885 else
4886 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
4887
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04004888 /* Setup callback routine and issue the command. */
4889 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05004890 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
4891 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05004892 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05004893 errcnt++;
4894 continue;
4895 }
4896 }
4897
4898 return errcnt;
4899}
4900
James Smarte59058c2008-08-24 21:49:00 -04004901/**
4902 * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
4903 * lpfc_sli_issue_iocb_wait.
4904 * @phba: Pointer to HBA context object.
4905 * @cmdiocbq: Pointer to command iocb.
4906 * @rspiocbq: Pointer to response iocb.
4907 *
4908 * This function is the completion handler for iocbs issued using
4909 * lpfc_sli_issue_iocb_wait function. This function is called by the
4910 * ring event handler function without any lock held. This function
4911 * can be called from both worker thread context and interrupt
4912 * context. This function also can be called from other thread which
4913 * cleans up the SLI layer objects.
4914 * This function copy the contents of the response iocb to the
4915 * response iocb memory object provided by the caller of
4916 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4917 * sleeps for the iocb completion.
4918 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004919static void
4920lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
4921 struct lpfc_iocbq *cmdiocbq,
4922 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05004923{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004924 wait_queue_head_t *pdone_q;
4925 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004926
James Smart2e0fef82007-06-17 19:56:36 -05004927 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004928 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4929 if (cmdiocbq->context2 && rspiocbq)
4930 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4931 &rspiocbq->iocb, sizeof(IOCB_t));
4932
4933 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004934 if (pdone_q)
4935 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05004936 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05004937 return;
4938}
4939
James Smarte59058c2008-08-24 21:49:00 -04004940/**
4941 * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
4942 * @phba: Pointer to HBA context object..
4943 * @pring: Pointer to sli ring.
4944 * @piocb: Pointer to command iocb.
4945 * @prspiocbq: Pointer to response iocb.
4946 * @timeout: Timeout in number of seconds.
4947 *
4948 * This function issues the iocb to firmware and waits for the
4949 * iocb to complete. If the iocb command is not
4950 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
4951 * Caller should not free the iocb resources if this function
4952 * returns IOCB_TIMEDOUT.
4953 * The function waits for the iocb completion using an
4954 * non-interruptible wait.
4955 * This function will sleep while waiting for iocb 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 iocb completions occur while
4960 * this function sleep. So, this function cannot be called from
4961 * the thread which process iocb completion for this ring.
4962 * This function clears the iocb_flag of the iocb object before
4963 * issuing the iocb and the iocb completion handler sets this
4964 * flag and wakes this thread when the iocb completes.
4965 * The contents of the response iocb will be copied to prspiocbq
4966 * by the completion handler when the command completes.
4967 * This function returns IOCB_SUCCESS when success.
4968 * This function is called with no lock held.
4969 **/
dea31012005-04-17 16:05:31 -05004970int
James Smart2e0fef82007-06-17 19:56:36 -05004971lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
4972 struct lpfc_sli_ring *pring,
4973 struct lpfc_iocbq *piocb,
4974 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004975 uint32_t timeout)
dea31012005-04-17 16:05:31 -05004976{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08004977 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004978 long timeleft, timeout_req = 0;
4979 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004980 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05004981
4982 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004983 * If the caller has provided a response iocbq buffer, then context2
4984 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05004985 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004986 if (prspiocbq) {
4987 if (piocb->context2)
4988 return IOCB_ERROR;
4989 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05004990 }
4991
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04004992 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
4993 piocb->context_un.wait_queue = &done_q;
4994 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05004995
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004996 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4997 creg_val = readl(phba->HCregaddr);
4998 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
4999 writel(creg_val, phba->HCregaddr);
5000 readl(phba->HCregaddr); /* flush */
5001 }
5002
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005003 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
5004 if (retval == IOCB_SUCCESS) {
5005 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005006 timeleft = wait_event_timeout(done_q,
5007 piocb->iocb_flag & LPFC_IO_WAKE,
5008 timeout_req);
dea31012005-04-17 16:05:31 -05005009
James Smart7054a602007-04-25 09:52:34 -04005010 if (piocb->iocb_flag & LPFC_IO_WAKE) {
5011 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005012 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04005013 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005014 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005015 "0338 IOCB wait timeout error - no "
5016 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005017 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04005018 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005019 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005020 "0330 IOCB wake NOT set, "
5021 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005022 timeout, (timeleft / jiffies));
5023 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05005024 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005025 } else {
5026 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04005027 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005028 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005029 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05005030 }
5031
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005032 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5033 creg_val = readl(phba->HCregaddr);
5034 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
5035 writel(creg_val, phba->HCregaddr);
5036 readl(phba->HCregaddr); /* flush */
5037 }
5038
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005039 if (prspiocbq)
5040 piocb->context2 = NULL;
5041
5042 piocb->context_un.wait_queue = NULL;
5043 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05005044 return retval;
5045}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04005046
James Smarte59058c2008-08-24 21:49:00 -04005047/**
5048 * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
5049 * @phba: Pointer to HBA context object.
5050 * @pmboxq: Pointer to driver mailbox object.
5051 * @timeout: Timeout in number of seconds.
5052 *
5053 * This function issues the mailbox to firmware and waits for the
5054 * mailbox command to complete. If the mailbox command is not
5055 * completed within timeout seconds, it returns MBX_TIMEOUT.
5056 * The function waits for the mailbox completion using an
5057 * interruptible wait. If the thread is woken up due to a
5058 * signal, MBX_TIMEOUT error is returned to the caller. Caller
5059 * should not free the mailbox resources, if this function returns
5060 * MBX_TIMEOUT.
5061 * This function will sleep while waiting for mailbox completion.
5062 * So, this function should not be called from any context which
5063 * does not allow sleeping. Due to the same reason, this function
5064 * cannot be called with interrupt disabled.
5065 * This function assumes that the mailbox completion occurs while
5066 * this function sleep. So, this function cannot be called from
5067 * the worker thread which processes mailbox completion.
5068 * This function is called in the context of HBA management
5069 * applications.
5070 * This function returns MBX_SUCCESS when successful.
5071 * This function is called with no lock held.
5072 **/
dea31012005-04-17 16:05:31 -05005073int
James Smart2e0fef82007-06-17 19:56:36 -05005074lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05005075 uint32_t timeout)
5076{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08005077 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05005078 int retval;
James Smart858c9f62007-06-17 19:56:39 -05005079 unsigned long flag;
dea31012005-04-17 16:05:31 -05005080
5081 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04005082 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05005083 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005084
James Smart495a7142008-06-14 22:52:59 -04005085 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05005086 /* setup wake call as IOCB callback */
5087 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
5088 /* setup context field to pass wait_queue pointer to wake function */
5089 pmboxq->context1 = &done_q;
5090
dea31012005-04-17 16:05:31 -05005091 /* now issue the command */
5092 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
5093
5094 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04005095 wait_event_interruptible_timeout(done_q,
5096 pmboxq->mbox_flag & LPFC_MBX_WAKE,
5097 timeout * HZ);
5098
James Smart858c9f62007-06-17 19:56:39 -05005099 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005100 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04005101 /*
5102 * if LPFC_MBX_WAKE flag is set the mailbox is completed
5103 * else do not free the resources.
5104 */
5105 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05005106 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05005107 else {
James Smart7054a602007-04-25 09:52:34 -04005108 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05005109 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5110 }
5111 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05005112 }
5113
dea31012005-04-17 16:05:31 -05005114 return retval;
5115}
5116
James Smarte59058c2008-08-24 21:49:00 -04005117/**
5118 * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
5119 * @phba: Pointer to HBA context.
5120 *
5121 * This function is called to cleanup any pending mailbox
5122 * objects in the driver queue before bringing the HBA offline.
5123 * This function is called while resetting the HBA.
5124 * The function is called without any lock held. The function
5125 * takes hbalock to update SLI data structure.
5126 * This function returns 1 when there is an active mailbox
5127 * command pending else returns 0.
5128 **/
James Smartb4c02652006-07-06 15:50:43 -04005129int
5130lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
5131{
James Smart2e0fef82007-06-17 19:56:36 -05005132 struct lpfc_vport *vport = phba->pport;
James Smartb4c02652006-07-06 15:50:43 -04005133 int i = 0;
James Smarted957682007-06-17 19:56:37 -05005134 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04005135
James Smart2e0fef82007-06-17 19:56:36 -05005136 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
James Smartb4c02652006-07-06 15:50:43 -04005137 if (i++ > LPFC_MBOX_TMO * 1000)
5138 return 1;
5139
James Smarted957682007-06-17 19:56:37 -05005140 /*
5141 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
5142 * did finish. This way we won't get the misleading
5143 * "Stray Mailbox Interrupt" message.
5144 */
5145 spin_lock_irq(&phba->hbalock);
5146 ha_copy = phba->work_ha;
5147 phba->work_ha &= ~HA_MBATT;
5148 spin_unlock_irq(&phba->hbalock);
5149
5150 if (ha_copy & HA_MBATT)
5151 if (lpfc_sli_handle_mb_event(phba) == 0)
5152 i = 0;
James Smartb4c02652006-07-06 15:50:43 -04005153
5154 msleep(1);
5155 }
5156
5157 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
5158}
5159
James Smarte59058c2008-08-24 21:49:00 -04005160/**
James Smart93996272008-08-24 21:50:30 -04005161 * lpfc_sli_check_eratt: check error attention events
5162 * @phba: Pointer to HBA context.
5163 *
5164 * This function is called form timer soft interrupt context to check HBA's
5165 * error attention register bit for error attention events.
5166 *
5167 * This fucntion returns 1 when there is Error Attention in the Host Attention
5168 * Register and returns 0 otherwise.
5169 **/
5170int
5171lpfc_sli_check_eratt(struct lpfc_hba *phba)
5172{
5173 uint32_t ha_copy;
5174
5175 /* If somebody is waiting to handle an eratt, don't process it
5176 * here. The brdkill function will do this.
5177 */
5178 if (phba->link_flag & LS_IGNORE_ERATT)
5179 return 0;
5180
5181 /* Check if interrupt handler handles this ERATT */
5182 spin_lock_irq(&phba->hbalock);
5183 if (phba->hba_flag & HBA_ERATT_HANDLED) {
5184 /* Interrupt handler has handled ERATT */
5185 spin_unlock_irq(&phba->hbalock);
5186 return 0;
5187 }
5188
5189 /* Read chip Host Attention (HA) register */
5190 ha_copy = readl(phba->HAregaddr);
5191 if (ha_copy & HA_ERATT) {
5192 /* Read host status register to retrieve error event */
5193 lpfc_sli_read_hs(phba);
5194 /* Set the driver HA work bitmap */
5195 phba->work_ha |= HA_ERATT;
5196 /* Indicate polling handles this ERATT */
5197 phba->hba_flag |= HBA_ERATT_HANDLED;
5198 spin_unlock_irq(&phba->hbalock);
5199 return 1;
5200 }
5201 spin_unlock_irq(&phba->hbalock);
5202 return 0;
5203}
5204
5205/**
5206 * lpfc_sp_intr_handler: The slow-path interrupt handler of lpfc driver.
James Smarte59058c2008-08-24 21:49:00 -04005207 * @irq: Interrupt number.
5208 * @dev_id: The device context pointer.
5209 *
James Smart93996272008-08-24 21:50:30 -04005210 * This function is directly called from the PCI layer as an interrupt
5211 * service routine when the device is enabled with MSI-X multi-message
5212 * interrupt mode and there are slow-path events in the HBA. However,
5213 * when the device is enabled with either MSI or Pin-IRQ interrupt mode,
5214 * this function is called as part of the device-level interrupt handler.
5215 * When the PCI slot is in error recovery or the HBA is undergoing
5216 * initialization, the interrupt handler will not process the interrupt.
5217 * The link attention and ELS ring attention events are handled by the
5218 * worker thread. The interrupt handler signals the worker thread and
5219 * and returns for these events. This function is called without any
5220 * lock held. It gets the hbalock to access and update SLI data
5221 * structures.
5222 *
5223 * This function returns IRQ_HANDLED when interrupt is handled else it
5224 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04005225 **/
dea31012005-04-17 16:05:31 -05005226irqreturn_t
James Smart93996272008-08-24 21:50:30 -04005227lpfc_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05005228{
James Smart2e0fef82007-06-17 19:56:36 -05005229 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05005230 uint32_t ha_copy;
5231 uint32_t work_ha_copy;
5232 unsigned long status;
dea31012005-04-17 16:05:31 -05005233 uint32_t control;
5234
James Smart92d7f7b2007-06-17 19:56:38 -05005235 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05005236 struct lpfc_vport *vport;
5237 struct lpfc_nodelist *ndlp;
5238 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05005239 LPFC_MBOXQ_t *pmb;
5240 int rc;
5241
dea31012005-04-17 16:05:31 -05005242 /*
5243 * Get the driver's phba structure from the dev_id and
5244 * assume the HBA is not interrupting.
5245 */
James Smart93996272008-08-24 21:50:30 -04005246 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05005247
5248 if (unlikely(!phba))
5249 return IRQ_NONE;
5250
dea31012005-04-17 16:05:31 -05005251 /*
James Smart93996272008-08-24 21:50:30 -04005252 * Stuff needs to be attented to when this function is invoked as an
5253 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005254 */
James Smart93996272008-08-24 21:50:30 -04005255 if (phba->intr_type == MSIX) {
5256 /* If the pci channel is offline, ignore all the interrupts */
5257 if (unlikely(pci_channel_offline(phba->pcidev)))
5258 return IRQ_NONE;
5259 /* Update device-level interrupt statistics */
5260 phba->sli.slistat.sli_intr++;
5261 /* Ignore all interrupts during initialization. */
5262 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5263 return IRQ_NONE;
5264 /* Need to read HA REG for slow-path events */
5265 spin_lock(&phba->hbalock);
James Smart34b02dc2008-08-24 21:49:55 -04005266 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04005267 /* If somebody is waiting to handle an eratt don't process it
5268 * here. The brdkill function will do this.
5269 */
5270 if (phba->link_flag & LS_IGNORE_ERATT)
5271 ha_copy &= ~HA_ERATT;
5272 /* Check the need for handling ERATT in interrupt handler */
5273 if (ha_copy & HA_ERATT) {
5274 if (phba->hba_flag & HBA_ERATT_HANDLED)
5275 /* ERATT polling has handled ERATT */
5276 ha_copy &= ~HA_ERATT;
5277 else
5278 /* Indicate interrupt handler handles ERATT */
5279 phba->hba_flag |= HBA_ERATT_HANDLED;
5280 }
5281 /* Clear up only attention source related to slow-path */
5282 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
5283 phba->HAregaddr);
5284 readl(phba->HAregaddr); /* flush */
James Smart34b02dc2008-08-24 21:49:55 -04005285 spin_unlock(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -04005286 } else
5287 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05005288
dea31012005-04-17 16:05:31 -05005289 work_ha_copy = ha_copy & phba->work_ha_mask;
5290
James Smart93996272008-08-24 21:50:30 -04005291 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05005292 if (work_ha_copy & HA_LATT) {
5293 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
5294 /*
5295 * Turn off Link Attention interrupts
5296 * until CLEAR_LA done
5297 */
James Smart2e0fef82007-06-17 19:56:36 -05005298 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005299 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
5300 control = readl(phba->HCregaddr);
5301 control &= ~HC_LAINT_ENA;
5302 writel(control, phba->HCregaddr);
5303 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05005304 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005305 }
5306 else
5307 work_ha_copy &= ~HA_LATT;
5308 }
5309
James Smart93996272008-08-24 21:50:30 -04005310 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05005311 /*
5312 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
5313 * the only slow ring.
5314 */
5315 status = (work_ha_copy &
5316 (HA_RXMASK << (4*LPFC_ELS_RING)));
5317 status >>= (4*LPFC_ELS_RING);
5318 if (status & HA_RXMASK) {
5319 spin_lock(&phba->hbalock);
5320 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04005321
5322 lpfc_debugfs_slow_ring_trc(phba,
5323 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
5324 control, status,
5325 (uint32_t)phba->sli.slistat.sli_intr);
5326
James Smart858c9f62007-06-17 19:56:39 -05005327 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04005328 lpfc_debugfs_slow_ring_trc(phba,
5329 "ISR Disable ring:"
5330 "pwork:x%x hawork:x%x wait:x%x",
5331 phba->work_ha, work_ha_copy,
5332 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005333 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005334
James Smart858c9f62007-06-17 19:56:39 -05005335 control &=
5336 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05005337 writel(control, phba->HCregaddr);
5338 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05005339 }
James Smarta58cbd52007-08-02 11:09:43 -04005340 else {
5341 lpfc_debugfs_slow_ring_trc(phba,
5342 "ISR slow ring: pwork:"
5343 "x%x hawork:x%x wait:x%x",
5344 phba->work_ha, work_ha_copy,
5345 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04005346 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04005347 }
James Smart858c9f62007-06-17 19:56:39 -05005348 spin_unlock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005349 }
5350 }
James Smart97eab632008-04-07 10:16:05 -04005351 spin_lock(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -04005352 if (work_ha_copy & HA_ERATT)
5353 lpfc_sli_read_hs(phba);
5354 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005355 pmb = phba->sli.mbox_active;
5356 pmbox = &pmb->mb;
James Smart34b02dc2008-08-24 21:49:55 -04005357 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05005358 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005359
5360 /* First check out the status word */
5361 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
5362 if (pmbox->mbxOwner != OWN_HOST) {
James Smart97eab632008-04-07 10:16:05 -04005363 spin_unlock(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005364 /*
5365 * Stray Mailbox Interrupt, mbxCommand <cmd>
5366 * mbxStatus <status>
5367 */
James Smart09372822008-01-11 01:52:54 -05005368 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05005369 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005370 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05005371 "Interrupt mbxCommand x%x "
5372 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005373 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05005374 pmbox->mbxCommand,
5375 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05005376 /* clear mailbox attention bit */
5377 work_ha_copy &= ~HA_MBATT;
5378 } else {
James Smart97eab632008-04-07 10:16:05 -04005379 phba->sli.mbox_active = NULL;
5380 spin_unlock(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05005381 phba->last_completion_time = jiffies;
5382 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05005383 if (pmb->mbox_cmpl) {
5384 lpfc_sli_pcimem_bcopy(mbox, pmbox,
5385 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05005386 }
James Smart09372822008-01-11 01:52:54 -05005387 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
5388 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
5389
5390 lpfc_debugfs_disc_trc(vport,
5391 LPFC_DISC_TRC_MBOX_VPORT,
5392 "MBOX dflt rpi: : "
5393 "status:x%x rpi:x%x",
5394 (uint32_t)pmbox->mbxStatus,
5395 pmbox->un.varWords[0], 0);
5396
5397 if (!pmbox->mbxStatus) {
5398 mp = (struct lpfc_dmabuf *)
5399 (pmb->context1);
5400 ndlp = (struct lpfc_nodelist *)
5401 pmb->context2;
5402
5403 /* Reg_LOGIN of dflt RPI was
5404 * successful. new lets get
5405 * rid of the RPI using the
5406 * same mbox buffer.
5407 */
5408 lpfc_unreg_login(phba,
5409 vport->vpi,
5410 pmbox->un.varWords[0],
5411 pmb);
5412 pmb->mbox_cmpl =
5413 lpfc_mbx_cmpl_dflt_rpi;
5414 pmb->context1 = mp;
5415 pmb->context2 = ndlp;
5416 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04005417 rc = lpfc_sli_issue_mbox(phba,
5418 pmb,
5419 MBX_NOWAIT);
5420 if (rc != MBX_BUSY)
5421 lpfc_printf_log(phba,
5422 KERN_ERR,
5423 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04005424 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04005425 "been MBX_BUSY");
James Smart09372822008-01-11 01:52:54 -05005426 goto send_current_mbox;
5427 }
5428 }
5429 spin_lock(&phba->pport->work_port_lock);
5430 phba->pport->work_port_events &=
5431 ~WORKER_MBOX_TMO;
5432 spin_unlock(&phba->pport->work_port_lock);
5433 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05005434 }
James Smart97eab632008-04-07 10:16:05 -04005435 } else
5436 spin_unlock(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -04005437
James Smart92d7f7b2007-06-17 19:56:38 -05005438 if ((work_ha_copy & HA_MBATT) &&
5439 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05005440send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05005441 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04005442 do {
5443 rc = lpfc_sli_issue_mbox(phba, NULL,
5444 MBX_NOWAIT);
5445 } while (rc == MBX_NOT_FINISHED);
5446 if (rc != MBX_SUCCESS)
5447 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
5448 LOG_SLI, "0349 rc should be "
5449 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05005450 }
5451
James Smart2e0fef82007-06-17 19:56:36 -05005452 spin_lock(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005453 phba->work_ha |= work_ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05005454 spin_unlock(&phba->hbalock);
James Smart5e9d9b82008-06-14 22:52:53 -04005455 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005456 }
James Smart93996272008-08-24 21:50:30 -04005457 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05005458
James Smart93996272008-08-24 21:50:30 -04005459} /* lpfc_sp_intr_handler */
5460
5461/**
5462 * lpfc_fp_intr_handler: The fast-path interrupt handler of lpfc driver.
5463 * @irq: Interrupt number.
5464 * @dev_id: The device context pointer.
5465 *
5466 * This function is directly called from the PCI layer as an interrupt
5467 * service routine when the device is enabled with MSI-X multi-message
5468 * interrupt mode and there is a fast-path FCP IOCB ring event in the
5469 * HBA. However, when the device is enabled with either MSI or Pin-IRQ
5470 * interrupt mode, this function is called as part of the device-level
5471 * interrupt handler. When the PCI slot is in error recovery or the HBA
5472 * is undergoing initialization, the interrupt handler will not process
5473 * the interrupt. The SCSI FCP fast-path ring event are handled in the
5474 * intrrupt context. This function is called without any lock held. It
5475 * gets the hbalock to access and update SLI data structures.
5476 *
5477 * This function returns IRQ_HANDLED when interrupt is handled else it
5478 * returns IRQ_NONE.
5479 **/
5480irqreturn_t
5481lpfc_fp_intr_handler(int irq, void *dev_id)
5482{
5483 struct lpfc_hba *phba;
5484 uint32_t ha_copy;
5485 unsigned long status;
5486
5487 /* Get the driver's phba structure from the dev_id and
5488 * assume the HBA is not interrupting.
5489 */
5490 phba = (struct lpfc_hba *) dev_id;
5491
5492 if (unlikely(!phba))
5493 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05005494
5495 /*
James Smart93996272008-08-24 21:50:30 -04005496 * Stuff needs to be attented to when this function is invoked as an
5497 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05005498 */
James Smart93996272008-08-24 21:50:30 -04005499 if (phba->intr_type == MSIX) {
5500 /* If pci channel is offline, ignore all the interrupts */
5501 if (unlikely(pci_channel_offline(phba->pcidev)))
5502 return IRQ_NONE;
5503 /* Update device-level interrupt statistics */
5504 phba->sli.slistat.sli_intr++;
5505 /* Ignore all interrupts during initialization. */
5506 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5507 return IRQ_NONE;
5508 /* Need to read HA REG for FCP ring and other ring events */
5509 ha_copy = readl(phba->HAregaddr);
5510 /* Clear up only attention source related to fast-path */
5511 spin_lock(&phba->hbalock);
5512 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
5513 phba->HAregaddr);
5514 readl(phba->HAregaddr); /* flush */
5515 spin_unlock(&phba->hbalock);
5516 } else
5517 ha_copy = phba->ha_copy;
5518
5519 /*
5520 * Process all events on FCP ring. Take the optimized path for FCP IO.
5521 */
5522 ha_copy &= ~(phba->work_ha_mask);
5523
5524 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05005525 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05005526 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05005527 lpfc_sli_handle_fast_ring_event(phba,
5528 &phba->sli.ring[LPFC_FCP_RING],
5529 status);
James Smarta4bc3372006-12-02 13:34:16 -05005530
5531 if (phba->cfg_multi_ring_support == 2) {
5532 /*
James Smart93996272008-08-24 21:50:30 -04005533 * Process all events on extra ring. Take the optimized path
5534 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05005535 */
James Smart93996272008-08-24 21:50:30 -04005536 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05005537 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05005538 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05005539 lpfc_sli_handle_fast_ring_event(phba,
5540 &phba->sli.ring[LPFC_EXTRA_RING],
5541 status);
5542 }
5543 }
dea31012005-04-17 16:05:31 -05005544 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -04005545} /* lpfc_fp_intr_handler */
dea31012005-04-17 16:05:31 -05005546
James Smart93996272008-08-24 21:50:30 -04005547/**
5548 * lpfc_intr_handler: The device-level interrupt handler of lpfc driver.
5549 * @irq: Interrupt number.
5550 * @dev_id: The device context pointer.
5551 *
5552 * This function is the device-level interrupt handler called from the PCI
5553 * layer when either MSI or Pin-IRQ interrupt mode is enabled and there is
5554 * an event in the HBA which requires driver attention. This function
5555 * invokes the slow-path interrupt attention handling function and fast-path
5556 * interrupt attention handling function in turn to process the relevant
5557 * HBA attention events. This function is called without any lock held. It
5558 * gets the hbalock to access and update SLI data structures.
5559 *
5560 * This function returns IRQ_HANDLED when interrupt is handled, else it
5561 * returns IRQ_NONE.
5562 **/
5563irqreturn_t
5564lpfc_intr_handler(int irq, void *dev_id)
5565{
5566 struct lpfc_hba *phba;
5567 irqreturn_t sp_irq_rc, fp_irq_rc;
5568 unsigned long status1, status2;
5569
5570 /*
5571 * Get the driver's phba structure from the dev_id and
5572 * assume the HBA is not interrupting.
5573 */
5574 phba = (struct lpfc_hba *) dev_id;
5575
5576 if (unlikely(!phba))
5577 return IRQ_NONE;
5578
5579 /* If the pci channel is offline, ignore all the interrupts. */
5580 if (unlikely(pci_channel_offline(phba->pcidev)))
5581 return IRQ_NONE;
5582
5583 /* Update device level interrupt statistics */
5584 phba->sli.slistat.sli_intr++;
5585
5586 /* Ignore all interrupts during initialization. */
5587 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5588 return IRQ_NONE;
5589
5590 spin_lock(&phba->hbalock);
5591 phba->ha_copy = readl(phba->HAregaddr);
5592 if (unlikely(!phba->ha_copy)) {
5593 spin_unlock(&phba->hbalock);
5594 return IRQ_NONE;
5595 } else if (phba->ha_copy & HA_ERATT) {
5596 if (phba->hba_flag & HBA_ERATT_HANDLED)
5597 /* ERATT polling has handled ERATT */
5598 phba->ha_copy &= ~HA_ERATT;
5599 else
5600 /* Indicate interrupt handler handles ERATT */
5601 phba->hba_flag |= HBA_ERATT_HANDLED;
5602 }
5603
5604 /* Clear attention sources except link and error attentions */
5605 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
5606 readl(phba->HAregaddr); /* flush */
5607 spin_unlock(&phba->hbalock);
5608
5609 /*
5610 * Invokes slow-path host attention interrupt handling as appropriate.
5611 */
5612
5613 /* status of events with mailbox and link attention */
5614 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
5615
5616 /* status of events with ELS ring */
5617 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
5618 status2 >>= (4*LPFC_ELS_RING);
5619
5620 if (status1 || (status2 & HA_RXMASK))
5621 sp_irq_rc = lpfc_sp_intr_handler(irq, dev_id);
5622 else
5623 sp_irq_rc = IRQ_NONE;
5624
5625 /*
5626 * Invoke fast-path host attention interrupt handling as appropriate.
5627 */
5628
5629 /* status of events with FCP ring */
5630 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
5631 status1 >>= (4*LPFC_FCP_RING);
5632
5633 /* status of events with extra ring */
5634 if (phba->cfg_multi_ring_support == 2) {
5635 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
5636 status2 >>= (4*LPFC_EXTRA_RING);
5637 } else
5638 status2 = 0;
5639
5640 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
5641 fp_irq_rc = lpfc_fp_intr_handler(irq, dev_id);
5642 else
5643 fp_irq_rc = IRQ_NONE;
5644
5645 /* Return device-level interrupt handling status */
5646 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
5647} /* lpfc_intr_handler */