blob: 706bb22a6e8ef6e02ea461c1ff297ec31bbfd6ee [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>
James Smartda0436e2009-05-22 14:51:39 -040032#include <scsi/fc/fc_fs.h>
dea31012005-04-17 16:05:31 -050033
James Smartda0436e2009-05-22 14:51:39 -040034#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_hw.h"
36#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040038#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_crtn.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050045#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050046
47/*
48 * Define macro to log: Mailbox command x%x cannot issue Data
49 * This allows multiple uses of lpfc_msgBlk0311
50 * w/o perturbing log msg utility.
51 */
James Smart92d7f7b2007-06-17 19:56:38 -050052#define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
dea31012005-04-17 16:05:31 -050053 lpfc_printf_log(phba, \
54 KERN_INFO, \
55 LOG_MBOX | LOG_SLI, \
James Smarte8b62012007-08-02 11:10:09 -040056 "(%d):0311 Mailbox command x%x cannot " \
James Smart92d7f7b2007-06-17 19:56:38 -050057 "issue Data: x%x x%x x%x\n", \
James Smart92d7f7b2007-06-17 19:56:38 -050058 pmbox->vport ? pmbox->vport->vpi : 0, \
59 pmbox->mb.mbxCommand, \
James Smart2e0fef82007-06-17 19:56:36 -050060 phba->pport->port_state, \
dea31012005-04-17 16:05:31 -050061 psli->sli_flag, \
James Smart2e0fef82007-06-17 19:56:36 -050062 flag)
dea31012005-04-17 16:05:31 -050063
64
65/* There are only four IOCB completion types. */
66typedef enum _lpfc_iocb_type {
67 LPFC_UNKNOWN_IOCB,
68 LPFC_UNSOL_IOCB,
69 LPFC_SOL_IOCB,
70 LPFC_ABORT_IOCB
71} lpfc_iocb_type;
72
James Smarte59058c2008-08-24 21:49:00 -040073/**
James Smart3621a712009-04-06 18:47:14 -040074 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -040075 * @phba: Pointer to HBA context object.
76 * @pring: Pointer to driver SLI ring object.
77 *
78 * This function returns pointer to next command iocb entry
79 * in the command ring. The caller must hold hbalock to prevent
80 * other threads consume the next command iocb.
81 * SLI-2/SLI-3 provide different sized iocbs.
82 **/
James Smarted957682007-06-17 19:56:37 -050083static inline IOCB_t *
84lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
85{
86 return (IOCB_t *) (((char *) pring->cmdringaddr) +
87 pring->cmdidx * phba->iocb_cmd_size);
88}
89
James Smarte59058c2008-08-24 21:49:00 -040090/**
James Smart3621a712009-04-06 18:47:14 -040091 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -040092 * @phba: Pointer to HBA context object.
93 * @pring: Pointer to driver SLI ring object.
94 *
95 * This function returns pointer to next response iocb entry
96 * in the response ring. The caller must hold hbalock to make sure
97 * that no other thread consume the next response iocb.
98 * SLI-2/SLI-3 provide different sized iocbs.
99 **/
James Smarted957682007-06-17 19:56:37 -0500100static inline IOCB_t *
101lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
102{
103 return (IOCB_t *) (((char *) pring->rspringaddr) +
104 pring->rspidx * phba->iocb_rsp_size);
105}
106
James Smarte59058c2008-08-24 21:49:00 -0400107/**
James Smart3621a712009-04-06 18:47:14 -0400108 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400109 * @phba: Pointer to HBA context object.
110 *
111 * This function is called with hbalock held. This function
112 * allocates a new driver iocb object from the iocb pool. If the
113 * allocation is successful, it returns pointer to the newly
114 * allocated iocb object else it returns NULL.
115 **/
James Smart2e0fef82007-06-17 19:56:36 -0500116static struct lpfc_iocbq *
117__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400118{
119 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
120 struct lpfc_iocbq * iocbq = NULL;
121
122 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
123 return iocbq;
124}
125
James Smarte59058c2008-08-24 21:49:00 -0400126/**
James Smartda0436e2009-05-22 14:51:39 -0400127 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
128 * @phba: Pointer to HBA context object.
129 * @xritag: XRI value.
130 *
131 * This function clears the sglq pointer from the array of acive
132 * sglq's. The xritag that is passed in is used to index into the
133 * array. Before the xritag can be used it needs to be adjusted
134 * by subtracting the xribase.
135 *
136 * Returns sglq ponter = success, NULL = Failure.
137 **/
138static struct lpfc_sglq *
139__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
140{
141 uint16_t adj_xri;
142 struct lpfc_sglq *sglq;
143 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
144 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
145 return NULL;
146 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
147 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
148 return sglq;
149}
150
151/**
152 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
153 * @phba: Pointer to HBA context object.
154 * @xritag: XRI value.
155 *
156 * This function returns the sglq pointer from the array of acive
157 * sglq's. The xritag that is passed in is used to index into the
158 * array. Before the xritag can be used it needs to be adjusted
159 * by subtracting the xribase.
160 *
161 * Returns sglq ponter = success, NULL = Failure.
162 **/
163static struct lpfc_sglq *
164__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
165{
166 uint16_t adj_xri;
167 struct lpfc_sglq *sglq;
168 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
169 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
170 return NULL;
171 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
172 return sglq;
173}
174
175/**
176 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
177 * @phba: Pointer to HBA context object.
178 *
179 * This function is called with hbalock held. This function
180 * Gets a new driver sglq object from the sglq list. If the
181 * list is not empty then it is successful, it returns pointer to the newly
182 * allocated sglq object else it returns NULL.
183 **/
184static struct lpfc_sglq *
185__lpfc_sli_get_sglq(struct lpfc_hba *phba)
186{
187 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
188 struct lpfc_sglq *sglq = NULL;
189 uint16_t adj_xri;
190 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
191 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
192 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
193 return sglq;
194}
195
196/**
James Smart3621a712009-04-06 18:47:14 -0400197 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400198 * @phba: Pointer to HBA context object.
199 *
200 * This function is called with no lock held. This function
201 * allocates a new driver iocb object from the iocb pool. If the
202 * allocation is successful, it returns pointer to the newly
203 * allocated iocb object else it returns NULL.
204 **/
James Smart2e0fef82007-06-17 19:56:36 -0500205struct lpfc_iocbq *
206lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500207{
James Smart2e0fef82007-06-17 19:56:36 -0500208 struct lpfc_iocbq * iocbq = NULL;
209 unsigned long iflags;
210
211 spin_lock_irqsave(&phba->hbalock, iflags);
212 iocbq = __lpfc_sli_get_iocbq(phba);
213 spin_unlock_irqrestore(&phba->hbalock, iflags);
214 return iocbq;
215}
216
James Smarte59058c2008-08-24 21:49:00 -0400217/**
James Smart3772a992009-05-22 14:50:54 -0400218 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
219 * @phba: Pointer to HBA context object.
220 * @iocbq: Pointer to driver iocb object.
221 *
222 * This function is called with hbalock held to release driver
223 * iocb object to the iocb pool. The iotag in the iocb object
224 * does not change for each use of the iocb object. This function
225 * clears all other fields of the iocb object when it is freed.
226 **/
227static void
228__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
229{
230 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
231
232 /*
233 * Clean all volatile data fields, preserve iotag and node struct.
234 */
235 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
236 iocbq->sli4_xritag = NO_XRI;
237 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
238}
239
240/**
James Smart3621a712009-04-06 18:47:14 -0400241 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400242 * @phba: Pointer to HBA context object.
243 * @iocbq: Pointer to driver iocb object.
244 *
245 * This function is called with hbalock held to release driver
246 * iocb object to the iocb pool. The iotag in the iocb object
247 * does not change for each use of the iocb object. This function
248 * clears all other fields of the iocb object when it is freed.
249 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100250static void
James Smart2e0fef82007-06-17 19:56:36 -0500251__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
252{
James Smart3772a992009-05-22 14:50:54 -0400253 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Bottomley604a3e32005-10-29 10:28:33 -0500254}
255
James Smarte59058c2008-08-24 21:49:00 -0400256/**
James Smart3621a712009-04-06 18:47:14 -0400257 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400258 * @phba: Pointer to HBA context object.
259 * @iocbq: Pointer to driver iocb object.
260 *
261 * This function is called with no lock held to release the iocb to
262 * iocb pool.
263 **/
James Smart2e0fef82007-06-17 19:56:36 -0500264void
265lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
266{
267 unsigned long iflags;
268
269 /*
270 * Clean all volatile data fields, preserve iotag and node struct.
271 */
272 spin_lock_irqsave(&phba->hbalock, iflags);
273 __lpfc_sli_release_iocbq(phba, iocbq);
274 spin_unlock_irqrestore(&phba->hbalock, iflags);
275}
276
James Smarte59058c2008-08-24 21:49:00 -0400277/**
James Smarta257bf92009-04-06 18:48:10 -0400278 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
279 * @phba: Pointer to HBA context object.
280 * @iocblist: List of IOCBs.
281 * @ulpstatus: ULP status in IOCB command field.
282 * @ulpWord4: ULP word-4 in IOCB command field.
283 *
284 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
285 * on the list by invoking the complete callback function associated with the
286 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
287 * fields.
288 **/
289void
290lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
291 uint32_t ulpstatus, uint32_t ulpWord4)
292{
293 struct lpfc_iocbq *piocb;
294
295 while (!list_empty(iocblist)) {
296 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
297
298 if (!piocb->iocb_cmpl)
299 lpfc_sli_release_iocbq(phba, piocb);
300 else {
301 piocb->iocb.ulpStatus = ulpstatus;
302 piocb->iocb.un.ulpWord[4] = ulpWord4;
303 (piocb->iocb_cmpl) (phba, piocb, piocb);
304 }
305 }
306 return;
307}
308
309/**
James Smart3621a712009-04-06 18:47:14 -0400310 * lpfc_sli_iocb_cmd_type - Get the iocb type
311 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400312 *
313 * This function is called by ring event handler function to get the iocb type.
314 * This function translates the iocb command to an iocb command type used to
315 * decide the final disposition of each completed IOCB.
316 * The function returns
317 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
318 * LPFC_SOL_IOCB if it is a solicited iocb completion
319 * LPFC_ABORT_IOCB if it is an abort iocb
320 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
321 *
322 * The caller is not required to hold any lock.
323 **/
dea31012005-04-17 16:05:31 -0500324static lpfc_iocb_type
325lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
326{
327 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
328
329 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
330 return 0;
331
332 switch (iocb_cmnd) {
333 case CMD_XMIT_SEQUENCE_CR:
334 case CMD_XMIT_SEQUENCE_CX:
335 case CMD_XMIT_BCAST_CN:
336 case CMD_XMIT_BCAST_CX:
337 case CMD_ELS_REQUEST_CR:
338 case CMD_ELS_REQUEST_CX:
339 case CMD_CREATE_XRI_CR:
340 case CMD_CREATE_XRI_CX:
341 case CMD_GET_RPI_CN:
342 case CMD_XMIT_ELS_RSP_CX:
343 case CMD_GET_RPI_CR:
344 case CMD_FCP_IWRITE_CR:
345 case CMD_FCP_IWRITE_CX:
346 case CMD_FCP_IREAD_CR:
347 case CMD_FCP_IREAD_CX:
348 case CMD_FCP_ICMND_CR:
349 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500350 case CMD_FCP_TSEND_CX:
351 case CMD_FCP_TRSP_CX:
352 case CMD_FCP_TRECEIVE_CX:
353 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500354 case CMD_ADAPTER_MSG:
355 case CMD_ADAPTER_DUMP:
356 case CMD_XMIT_SEQUENCE64_CR:
357 case CMD_XMIT_SEQUENCE64_CX:
358 case CMD_XMIT_BCAST64_CN:
359 case CMD_XMIT_BCAST64_CX:
360 case CMD_ELS_REQUEST64_CR:
361 case CMD_ELS_REQUEST64_CX:
362 case CMD_FCP_IWRITE64_CR:
363 case CMD_FCP_IWRITE64_CX:
364 case CMD_FCP_IREAD64_CR:
365 case CMD_FCP_IREAD64_CX:
366 case CMD_FCP_ICMND64_CR:
367 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500368 case CMD_FCP_TSEND64_CX:
369 case CMD_FCP_TRSP64_CX:
370 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500371 case CMD_GEN_REQUEST64_CR:
372 case CMD_GEN_REQUEST64_CX:
373 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400374 case DSSCMD_IWRITE64_CR:
375 case DSSCMD_IWRITE64_CX:
376 case DSSCMD_IREAD64_CR:
377 case DSSCMD_IREAD64_CX:
378 case DSSCMD_INVALIDATE_DEK:
379 case DSSCMD_SET_KEK:
380 case DSSCMD_GET_KEK_ID:
381 case DSSCMD_GEN_XFER:
dea31012005-04-17 16:05:31 -0500382 type = LPFC_SOL_IOCB;
383 break;
384 case CMD_ABORT_XRI_CN:
385 case CMD_ABORT_XRI_CX:
386 case CMD_CLOSE_XRI_CN:
387 case CMD_CLOSE_XRI_CX:
388 case CMD_XRI_ABORTED_CX:
389 case CMD_ABORT_MXRI64_CN:
390 type = LPFC_ABORT_IOCB;
391 break;
392 case CMD_RCV_SEQUENCE_CX:
393 case CMD_RCV_ELS_REQ_CX:
394 case CMD_RCV_SEQUENCE64_CX:
395 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400396 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500397 case CMD_IOCB_RCV_SEQ64_CX:
398 case CMD_IOCB_RCV_ELS64_CX:
399 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500400 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500401 type = LPFC_UNSOL_IOCB;
402 break;
James Smart3163f722008-02-08 18:50:25 -0500403 case CMD_IOCB_XMIT_MSEQ64_CR:
404 case CMD_IOCB_XMIT_MSEQ64_CX:
405 case CMD_IOCB_RCV_SEQ_LIST64_CX:
406 case CMD_IOCB_RCV_ELS_LIST64_CX:
407 case CMD_IOCB_CLOSE_EXTENDED_CN:
408 case CMD_IOCB_ABORT_EXTENDED_CN:
409 case CMD_IOCB_RET_HBQE64_CN:
410 case CMD_IOCB_FCP_IBIDIR64_CR:
411 case CMD_IOCB_FCP_IBIDIR64_CX:
412 case CMD_IOCB_FCP_ITASKMGT64_CX:
413 case CMD_IOCB_LOGENTRY_CN:
414 case CMD_IOCB_LOGENTRY_ASYNC_CN:
415 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700416 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500417 type = LPFC_UNKNOWN_IOCB;
418 break;
dea31012005-04-17 16:05:31 -0500419 default:
420 type = LPFC_UNKNOWN_IOCB;
421 break;
422 }
423
424 return type;
425}
426
James Smarte59058c2008-08-24 21:49:00 -0400427/**
James Smart3621a712009-04-06 18:47:14 -0400428 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400429 * @phba: Pointer to HBA context object.
430 *
431 * This function is called from SLI initialization code
432 * to configure every ring of the HBA's SLI interface. The
433 * caller is not required to hold any lock. This function issues
434 * a config_ring mailbox command for each ring.
435 * This function returns zero if successful else returns a negative
436 * error code.
437 **/
dea31012005-04-17 16:05:31 -0500438static int
James Smarted957682007-06-17 19:56:37 -0500439lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500440{
441 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500442 LPFC_MBOXQ_t *pmb;
443 MAILBOX_t *pmbox;
444 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500445
James Smarted957682007-06-17 19:56:37 -0500446 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
447 if (!pmb)
448 return -ENOMEM;
449 pmbox = &pmb->mb;
450 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500451 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500452 lpfc_config_ring(phba, i, pmb);
453 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
454 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400456 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500457 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
458 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400459 rc, pmbox->mbxCommand,
460 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500461 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500462 ret = -ENXIO;
463 break;
dea31012005-04-17 16:05:31 -0500464 }
465 }
James Smarted957682007-06-17 19:56:37 -0500466 mempool_free(pmb, phba->mbox_mem_pool);
467 return ret;
dea31012005-04-17 16:05:31 -0500468}
469
James Smarte59058c2008-08-24 21:49:00 -0400470/**
James Smart3621a712009-04-06 18:47:14 -0400471 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400472 * @phba: Pointer to HBA context object.
473 * @pring: Pointer to driver SLI ring object.
474 * @piocb: Pointer to the driver iocb object.
475 *
476 * This function is called with hbalock held. The function adds the
477 * new iocb to txcmplq of the given ring. This function always returns
478 * 0. If this function is called for ELS ring, this function checks if
479 * there is a vport associated with the ELS command. This function also
480 * starts els_tmofunc timer if this is an ELS command.
481 **/
dea31012005-04-17 16:05:31 -0500482static int
James Smart2e0fef82007-06-17 19:56:36 -0500483lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
484 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500485{
dea31012005-04-17 16:05:31 -0500486 list_add_tail(&piocb->list, &pring->txcmplq);
487 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500488 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
489 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
490 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
491 if (!piocb->vport)
492 BUG();
493 else
494 mod_timer(&piocb->vport->els_tmofunc,
495 jiffies + HZ * (phba->fc_ratov << 1));
496 }
497
dea31012005-04-17 16:05:31 -0500498
James Smart2e0fef82007-06-17 19:56:36 -0500499 return 0;
dea31012005-04-17 16:05:31 -0500500}
501
James Smarte59058c2008-08-24 21:49:00 -0400502/**
James Smart3621a712009-04-06 18:47:14 -0400503 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400504 * @phba: Pointer to HBA context object.
505 * @pring: Pointer to driver SLI ring object.
506 *
507 * This function is called with hbalock held to get next
508 * iocb in txq of the given ring. If there is any iocb in
509 * the txq, the function returns first iocb in the list after
510 * removing the iocb from the list, else it returns NULL.
511 **/
dea31012005-04-17 16:05:31 -0500512static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500513lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500514{
dea31012005-04-17 16:05:31 -0500515 struct lpfc_iocbq *cmd_iocb;
516
James Smart858c9f62007-06-17 19:56:39 -0500517 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
518 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500519 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500520 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500521}
522
James Smarte59058c2008-08-24 21:49:00 -0400523/**
James Smart3621a712009-04-06 18:47:14 -0400524 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400525 * @phba: Pointer to HBA context object.
526 * @pring: Pointer to driver SLI ring object.
527 *
528 * This function is called with hbalock held and the caller must post the
529 * iocb without releasing the lock. If the caller releases the lock,
530 * iocb slot returned by the function is not guaranteed to be available.
531 * The function returns pointer to the next available iocb slot if there
532 * is available slot in the ring, else it returns NULL.
533 * If the get index of the ring is ahead of the put index, the function
534 * will post an error attention event to the worker thread to take the
535 * HBA to offline state.
536 **/
dea31012005-04-17 16:05:31 -0500537static IOCB_t *
538lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
539{
James Smart34b02dc2008-08-24 21:49:55 -0400540 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500541 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500542 if ((pring->next_cmdidx == pring->cmdidx) &&
543 (++pring->next_cmdidx >= max_cmd_idx))
544 pring->next_cmdidx = 0;
545
546 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
547
548 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
549
550 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
551 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400552 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200553 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400554 pring->ringno,
dea31012005-04-17 16:05:31 -0500555 pring->local_getidx, max_cmd_idx);
556
James Smart2e0fef82007-06-17 19:56:36 -0500557 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500558 /*
559 * All error attention handlers are posted to
560 * worker thread
561 */
562 phba->work_ha |= HA_ERATT;
563 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500564
James Smart5e9d9b82008-06-14 22:52:53 -0400565 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500566
567 return NULL;
568 }
569
570 if (pring->local_getidx == pring->next_cmdidx)
571 return NULL;
572 }
573
James Smarted957682007-06-17 19:56:37 -0500574 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500575}
576
James Smarte59058c2008-08-24 21:49:00 -0400577/**
James Smart3621a712009-04-06 18:47:14 -0400578 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400579 * @phba: Pointer to HBA context object.
580 * @iocbq: Pointer to driver iocb object.
581 *
582 * This function gets an iotag for the iocb. If there is no unused iotag and
583 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
584 * array and assigns a new iotag.
585 * The function returns the allocated iotag if successful, else returns zero.
586 * Zero is not a valid iotag.
587 * The caller is not required to hold any lock.
588 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500589uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500590lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500591{
James Smart2e0fef82007-06-17 19:56:36 -0500592 struct lpfc_iocbq **new_arr;
593 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500594 size_t new_len;
595 struct lpfc_sli *psli = &phba->sli;
596 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500597
James Smart2e0fef82007-06-17 19:56:36 -0500598 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500599 iotag = psli->last_iotag;
600 if(++iotag < psli->iocbq_lookup_len) {
601 psli->last_iotag = iotag;
602 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500603 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500604 iocbq->iotag = iotag;
605 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500606 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500607 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
608 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500609 spin_unlock_irq(&phba->hbalock);
610 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500611 GFP_KERNEL);
612 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500613 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500614 old_arr = psli->iocbq_lookup;
615 if (new_len <= psli->iocbq_lookup_len) {
616 /* highly unprobable case */
617 kfree(new_arr);
618 iotag = psli->last_iotag;
619 if(++iotag < psli->iocbq_lookup_len) {
620 psli->last_iotag = iotag;
621 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500622 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500623 iocbq->iotag = iotag;
624 return iotag;
625 }
James Smart2e0fef82007-06-17 19:56:36 -0500626 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500627 return 0;
628 }
629 if (psli->iocbq_lookup)
630 memcpy(new_arr, old_arr,
631 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -0400632 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -0500633 psli->iocbq_lookup = new_arr;
634 psli->iocbq_lookup_len = new_len;
635 psli->last_iotag = iotag;
636 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500637 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500638 iocbq->iotag = iotag;
639 kfree(old_arr);
640 return iotag;
641 }
James Smart8f6d98d2006-08-01 07:34:00 -0400642 } else
James Smart2e0fef82007-06-17 19:56:36 -0500643 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500644
James Bottomley604a3e32005-10-29 10:28:33 -0500645 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400646 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
647 psli->last_iotag);
dea31012005-04-17 16:05:31 -0500648
James Bottomley604a3e32005-10-29 10:28:33 -0500649 return 0;
dea31012005-04-17 16:05:31 -0500650}
651
James Smarte59058c2008-08-24 21:49:00 -0400652/**
James Smart3621a712009-04-06 18:47:14 -0400653 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -0400654 * @phba: Pointer to HBA context object.
655 * @pring: Pointer to driver SLI ring object.
656 * @iocb: Pointer to iocb slot in the ring.
657 * @nextiocb: Pointer to driver iocb object which need to be
658 * posted to firmware.
659 *
660 * This function is called with hbalock held to post a new iocb to
661 * the firmware. This function copies the new iocb to ring iocb slot and
662 * updates the ring pointers. It adds the new iocb to txcmplq if there is
663 * a completion call back for this iocb else the function will free the
664 * iocb object.
665 **/
dea31012005-04-17 16:05:31 -0500666static void
667lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
668 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
669{
670 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500671 * Set up an iotag
dea31012005-04-17 16:05:31 -0500672 */
James Bottomley604a3e32005-10-29 10:28:33 -0500673 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500674
James Smarte2a0a9d2008-12-04 22:40:02 -0500675
James Smarta58cbd52007-08-02 11:09:43 -0400676 if (pring->ringno == LPFC_ELS_RING) {
677 lpfc_debugfs_slow_ring_trc(phba,
678 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
679 *(((uint32_t *) &nextiocb->iocb) + 4),
680 *(((uint32_t *) &nextiocb->iocb) + 6),
681 *(((uint32_t *) &nextiocb->iocb) + 7));
682 }
683
dea31012005-04-17 16:05:31 -0500684 /*
685 * Issue iocb command to adapter
686 */
James Smart92d7f7b2007-06-17 19:56:38 -0500687 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -0500688 wmb();
689 pring->stats.iocb_cmd++;
690
691 /*
692 * If there is no completion routine to call, we can release the
693 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
694 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
695 */
696 if (nextiocb->iocb_cmpl)
697 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500698 else
James Smart2e0fef82007-06-17 19:56:36 -0500699 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500700
701 /*
702 * Let the HBA know what IOCB slot will be the next one the
703 * driver will put a command into.
704 */
705 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -0500706 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -0500707}
708
James Smarte59058c2008-08-24 21:49:00 -0400709/**
James Smart3621a712009-04-06 18:47:14 -0400710 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -0400711 * @phba: Pointer to HBA context object.
712 * @pring: Pointer to driver SLI ring object.
713 *
714 * The caller is not required to hold any lock for calling this function.
715 * This function updates the chip attention bits for the ring to inform firmware
716 * that there are pending work to be done for this ring and requests an
717 * interrupt when there is space available in the ring. This function is
718 * called when the driver is unable to post more iocbs to the ring due
719 * to unavailability of space in the ring.
720 **/
dea31012005-04-17 16:05:31 -0500721static void
James Smart2e0fef82007-06-17 19:56:36 -0500722lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500723{
724 int ringno = pring->ringno;
725
726 pring->flag |= LPFC_CALL_RING_AVAILABLE;
727
728 wmb();
729
730 /*
731 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
732 * The HBA will tell us when an IOCB entry is available.
733 */
734 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
735 readl(phba->CAregaddr); /* flush */
736
737 pring->stats.iocb_cmd_full++;
738}
739
James Smarte59058c2008-08-24 21:49:00 -0400740/**
James Smart3621a712009-04-06 18:47:14 -0400741 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -0400742 * @phba: Pointer to HBA context object.
743 * @pring: Pointer to driver SLI ring object.
744 *
745 * This function updates the chip attention register bit for the
746 * given ring to inform HBA that there is more work to be done
747 * in this ring. The caller is not required to hold any lock.
748 **/
dea31012005-04-17 16:05:31 -0500749static void
James Smart2e0fef82007-06-17 19:56:36 -0500750lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500751{
752 int ringno = pring->ringno;
753
754 /*
755 * Tell the HBA that there is work to do in this ring.
756 */
James Smart34b02dc2008-08-24 21:49:55 -0400757 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
758 wmb();
759 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
760 readl(phba->CAregaddr); /* flush */
761 }
dea31012005-04-17 16:05:31 -0500762}
763
James Smarte59058c2008-08-24 21:49:00 -0400764/**
James Smart3621a712009-04-06 18:47:14 -0400765 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -0400766 * @phba: Pointer to HBA context object.
767 * @pring: Pointer to driver SLI ring object.
768 *
769 * This function is called with hbalock held to post pending iocbs
770 * in the txq to the firmware. This function is called when driver
771 * detects space available in the ring.
772 **/
dea31012005-04-17 16:05:31 -0500773static void
James Smart2e0fef82007-06-17 19:56:36 -0500774lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500775{
776 IOCB_t *iocb;
777 struct lpfc_iocbq *nextiocb;
778
779 /*
780 * Check to see if:
781 * (a) there is anything on the txq to send
782 * (b) link is up
783 * (c) link attention events can be processed (fcp ring only)
784 * (d) IOCB processing is not blocked by the outstanding mbox command.
785 */
786 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -0500787 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -0500788 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -0400789 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -0500790
791 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
792 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
793 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
794
795 if (iocb)
796 lpfc_sli_update_ring(phba, pring);
797 else
798 lpfc_sli_update_full_ring(phba, pring);
799 }
800
801 return;
802}
803
James Smarte59058c2008-08-24 21:49:00 -0400804/**
James Smart3621a712009-04-06 18:47:14 -0400805 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -0400806 * @phba: Pointer to HBA context object.
807 * @hbqno: HBQ number.
808 *
809 * This function is called with hbalock held to get the next
810 * available slot for the given HBQ. If there is free slot
811 * available for the HBQ it will return pointer to the next available
812 * HBQ entry else it will return NULL.
813 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100814static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -0500815lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
816{
817 struct hbq_s *hbqp = &phba->hbqs[hbqno];
818
819 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
820 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
821 hbqp->next_hbqPutIdx = 0;
822
823 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500824 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -0500825 uint32_t getidx = le32_to_cpu(raw_index);
826
827 hbqp->local_hbqGetIdx = getidx;
828
829 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
830 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -0500831 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400832 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -0500833 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -0400834 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -0500835 hbqp->entry_count);
836
837 phba->link_state = LPFC_HBA_ERROR;
838 return NULL;
839 }
840
841 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
842 return NULL;
843 }
844
James Smart51ef4c22007-08-02 11:10:31 -0400845 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
846 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -0500847}
848
James Smarte59058c2008-08-24 21:49:00 -0400849/**
James Smart3621a712009-04-06 18:47:14 -0400850 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -0400851 * @phba: Pointer to HBA context object.
852 *
853 * This function is called with no lock held to free all the
854 * hbq buffers while uninitializing the SLI interface. It also
855 * frees the HBQ buffers returned by the firmware but not yet
856 * processed by the upper layers.
857 **/
James Smarted957682007-06-17 19:56:37 -0500858void
859lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
860{
James Smart92d7f7b2007-06-17 19:56:38 -0500861 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
862 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -0500863 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -0400864 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -0500865 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -0500866
James Smart51ef4c22007-08-02 11:10:31 -0400867 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -0500868 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -0500869 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -0400870 for (i = 0; i < hbq_count; ++i) {
871 list_for_each_entry_safe(dmabuf, next_dmabuf,
872 &phba->hbqs[i].hbq_buffer_list, list) {
873 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
874 list_del(&hbq_buf->dbuf.list);
875 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
876 }
James Smarta8adb832007-10-27 13:37:53 -0400877 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -0500878 }
James Smart3163f722008-02-08 18:50:25 -0500879 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -0400880 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
881 list) {
James Smart3163f722008-02-08 18:50:25 -0500882 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
883 list_del(&hbq_buf->dbuf.list);
884 if (hbq_buf->tag == -1) {
885 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
886 (phba, hbq_buf);
887 } else {
888 hbqno = hbq_buf->tag >> 16;
889 if (hbqno >= LPFC_MAX_HBQS)
890 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
891 (phba, hbq_buf);
892 else
893 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
894 hbq_buf);
895 }
896 }
897
898 /* Mark the HBQs not in use */
899 phba->hbq_in_use = 0;
900 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -0500901}
902
James Smarte59058c2008-08-24 21:49:00 -0400903/**
James Smart3621a712009-04-06 18:47:14 -0400904 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -0400905 * @phba: Pointer to HBA context object.
906 * @hbqno: HBQ number.
907 * @hbq_buf: Pointer to HBQ buffer.
908 *
909 * This function is called with the hbalock held to post a
910 * hbq buffer to the firmware. If the function finds an empty
911 * slot in the HBQ, it will post the buffer. The function will return
912 * pointer to the hbq entry if it successfully post the buffer
913 * else it will return NULL.
914 **/
James Smart3772a992009-05-22 14:50:54 -0400915static int
James Smarted957682007-06-17 19:56:37 -0500916lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -0500917 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -0500918{
James Smart3772a992009-05-22 14:50:54 -0400919 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
920}
921
922/**
923 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
924 * @phba: Pointer to HBA context object.
925 * @hbqno: HBQ number.
926 * @hbq_buf: Pointer to HBQ buffer.
927 *
928 * This function is called with the hbalock held to post a hbq buffer to the
929 * firmware. If the function finds an empty slot in the HBQ, it will post the
930 * buffer and place it on the hbq_buffer_list. The function will return zero if
931 * it successfully post the buffer else it will return an error.
932 **/
933static int
934lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
935 struct hbq_dmabuf *hbq_buf)
936{
James Smarted957682007-06-17 19:56:37 -0500937 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -0500938 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -0500939
940 /* Get next HBQ entry slot to use */
941 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
942 if (hbqe) {
943 struct hbq_s *hbqp = &phba->hbqs[hbqno];
944
James Smart92d7f7b2007-06-17 19:56:38 -0500945 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
946 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -0400947 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -0500948 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -0500949 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
950 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
951 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -0500952 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
953 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -0500954 /* flush */
James Smarted957682007-06-17 19:56:37 -0500955 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -0400956 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -0400957 return 0;
958 } else
959 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -0500960}
961
James Smarte59058c2008-08-24 21:49:00 -0400962/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -0500963static struct lpfc_hbq_init lpfc_els_hbq = {
964 .rn = 1,
965 .entry_count = 200,
966 .mask_count = 0,
967 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -0400968 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -0500969 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -0400970 .init_count = 40,
971 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -0500972};
James Smarted957682007-06-17 19:56:37 -0500973
James Smarte59058c2008-08-24 21:49:00 -0400974/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -0400975static struct lpfc_hbq_init lpfc_extra_hbq = {
976 .rn = 1,
977 .entry_count = 200,
978 .mask_count = 0,
979 .profile = 0,
980 .ring_mask = (1 << LPFC_EXTRA_RING),
981 .buffer_count = 0,
982 .init_count = 0,
983 .add_count = 5,
984};
985
James Smarte59058c2008-08-24 21:49:00 -0400986/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -0400987struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -0500988 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -0400989 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -0500990};
991
James Smarte59058c2008-08-24 21:49:00 -0400992/**
James Smart3621a712009-04-06 18:47:14 -0400993 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -0400994 * @phba: Pointer to HBA context object.
995 * @hbqno: HBQ number.
996 * @count: Number of HBQ buffers to be posted.
997 *
James Smartd7c255b2008-08-24 21:50:00 -0400998 * This function is called with no lock held to post more hbq buffers to the
999 * given HBQ. The function returns the number of HBQ buffers successfully
1000 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001001 **/
James Smart311464e2007-08-02 11:10:37 -04001002static int
James Smart92d7f7b2007-06-17 19:56:38 -05001003lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1004{
James Smartd7c255b2008-08-24 21:50:00 -04001005 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001006 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001007 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001008 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001009 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001010 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001011
James Smartd7c255b2008-08-24 21:50:00 -04001012 if ((phba->hbqs[hbqno].buffer_count + count) >
1013 lpfc_hbq_defs[hbqno]->entry_count)
1014 count = lpfc_hbq_defs[hbqno]->entry_count -
1015 phba->hbqs[hbqno].buffer_count;
1016 if (!count)
1017 return 0;
1018 /* Allocate HBQ entries */
1019 for (i = 0; i < count; i++) {
1020 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1021 if (!hbq_buffer)
1022 break;
1023 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1024 }
James Smart3163f722008-02-08 18:50:25 -05001025 /* Check whether HBQ is still in use */
1026 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001027 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001028 goto err;
1029 while (!list_empty(&hbq_buf_list)) {
1030 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1031 dbuf.list);
1032 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1033 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001034 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001035 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001036 posted++;
1037 } else
James Smart51ef4c22007-08-02 11:10:31 -04001038 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001039 }
James Smart3163f722008-02-08 18:50:25 -05001040 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001041 return posted;
1042err:
1043 spin_unlock_irqrestore(&phba->hbalock, flags);
1044 while (!list_empty(&hbq_buf_list)) {
1045 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1046 dbuf.list);
1047 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1048 }
James Smart92d7f7b2007-06-17 19:56:38 -05001049 return 0;
James Smarted957682007-06-17 19:56:37 -05001050}
1051
James Smarte59058c2008-08-24 21:49:00 -04001052/**
James Smart3621a712009-04-06 18:47:14 -04001053 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001054 * @phba: Pointer to HBA context object.
1055 * @qno: HBQ number.
1056 *
1057 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001058 * is called with no lock held. The function returns the number of HBQ entries
1059 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001060 **/
James Smarted957682007-06-17 19:56:37 -05001061int
James Smart92d7f7b2007-06-17 19:56:38 -05001062lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001063{
James Smart92d7f7b2007-06-17 19:56:38 -05001064 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1065 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -05001066}
1067
James Smarte59058c2008-08-24 21:49:00 -04001068/**
James Smart3621a712009-04-06 18:47:14 -04001069 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001070 * @phba: Pointer to HBA context object.
1071 * @qno: HBQ queue number.
1072 *
1073 * This function is called from SLI initialization code path with
1074 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001075 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001076 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001077static int
James Smart92d7f7b2007-06-17 19:56:38 -05001078lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001079{
James Smart92d7f7b2007-06-17 19:56:38 -05001080 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1081 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -05001082}
1083
James Smarte59058c2008-08-24 21:49:00 -04001084/**
James Smart3772a992009-05-22 14:50:54 -04001085 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1086 * @phba: Pointer to HBA context object.
1087 * @hbqno: HBQ number.
1088 *
1089 * This function removes the first hbq buffer on an hbq list and returns a
1090 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1091 **/
1092static struct hbq_dmabuf *
1093lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1094{
1095 struct lpfc_dmabuf *d_buf;
1096
1097 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1098 if (!d_buf)
1099 return NULL;
1100 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1101}
1102
1103/**
James Smart3621a712009-04-06 18:47:14 -04001104 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001105 * @phba: Pointer to HBA context object.
1106 * @tag: Tag of the hbq buffer.
1107 *
1108 * This function is called with hbalock held. This function searches
1109 * for the hbq buffer associated with the given tag in the hbq buffer
1110 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1111 * it returns NULL.
1112 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001113static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001114lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1115{
James Smart92d7f7b2007-06-17 19:56:38 -05001116 struct lpfc_dmabuf *d_buf;
1117 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001118 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001119
James Smart51ef4c22007-08-02 11:10:31 -04001120 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001121 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001122 return NULL;
1123
James Smart3772a992009-05-22 14:50:54 -04001124 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001125 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001126 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001127 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001128 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001129 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001130 }
1131 }
James Smart3772a992009-05-22 14:50:54 -04001132 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001134 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001135 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001136 return NULL;
James Smarted957682007-06-17 19:56:37 -05001137}
1138
James Smarte59058c2008-08-24 21:49:00 -04001139/**
James Smart3621a712009-04-06 18:47:14 -04001140 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001141 * @phba: Pointer to HBA context object.
1142 * @hbq_buffer: Pointer to HBQ buffer.
1143 *
1144 * This function is called with hbalock. This function gives back
1145 * the hbq buffer to firmware. If the HBQ does not have space to
1146 * post the buffer, it will free the buffer.
1147 **/
James Smarted957682007-06-17 19:56:37 -05001148void
James Smart51ef4c22007-08-02 11:10:31 -04001149lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001150{
1151 uint32_t hbqno;
1152
James Smart51ef4c22007-08-02 11:10:31 -04001153 if (hbq_buffer) {
1154 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001155 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001156 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001157 }
1158}
1159
James Smarte59058c2008-08-24 21:49:00 -04001160/**
James Smart3621a712009-04-06 18:47:14 -04001161 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001162 * @mbxCommand: mailbox command code.
1163 *
1164 * This function is called by the mailbox event handler function to verify
1165 * that the completed mailbox command is a legitimate mailbox command. If the
1166 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1167 * and the mailbox event handler will take the HBA offline.
1168 **/
dea31012005-04-17 16:05:31 -05001169static int
1170lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1171{
1172 uint8_t ret;
1173
1174 switch (mbxCommand) {
1175 case MBX_LOAD_SM:
1176 case MBX_READ_NV:
1177 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001178 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001179 case MBX_RUN_BIU_DIAG:
1180 case MBX_INIT_LINK:
1181 case MBX_DOWN_LINK:
1182 case MBX_CONFIG_LINK:
1183 case MBX_CONFIG_RING:
1184 case MBX_RESET_RING:
1185 case MBX_READ_CONFIG:
1186 case MBX_READ_RCONFIG:
1187 case MBX_READ_SPARM:
1188 case MBX_READ_STATUS:
1189 case MBX_READ_RPI:
1190 case MBX_READ_XRI:
1191 case MBX_READ_REV:
1192 case MBX_READ_LNK_STAT:
1193 case MBX_REG_LOGIN:
1194 case MBX_UNREG_LOGIN:
1195 case MBX_READ_LA:
1196 case MBX_CLEAR_LA:
1197 case MBX_DUMP_MEMORY:
1198 case MBX_DUMP_CONTEXT:
1199 case MBX_RUN_DIAGS:
1200 case MBX_RESTART:
1201 case MBX_UPDATE_CFG:
1202 case MBX_DOWN_LOAD:
1203 case MBX_DEL_LD_ENTRY:
1204 case MBX_RUN_PROGRAM:
1205 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001206 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001207 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001208 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001209 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001210 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001211 case MBX_LOAD_AREA:
1212 case MBX_RUN_BIU_DIAG64:
1213 case MBX_CONFIG_PORT:
1214 case MBX_READ_SPARM64:
1215 case MBX_READ_RPI64:
1216 case MBX_REG_LOGIN64:
1217 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001218 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001219 case MBX_SET_DEBUG:
1220 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001221 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001222 case MBX_REG_VPI:
1223 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001224 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001225 case MBX_PORT_CAPABILITIES:
1226 case MBX_PORT_IOV_CONTROL:
dea31012005-04-17 16:05:31 -05001227 ret = mbxCommand;
1228 break;
1229 default:
1230 ret = MBX_SHUTDOWN;
1231 break;
1232 }
James Smart2e0fef82007-06-17 19:56:36 -05001233 return ret;
dea31012005-04-17 16:05:31 -05001234}
James Smarte59058c2008-08-24 21:49:00 -04001235
1236/**
James Smart3621a712009-04-06 18:47:14 -04001237 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001238 * @phba: Pointer to HBA context object.
1239 * @pmboxq: Pointer to mailbox command.
1240 *
1241 * This is completion handler function for mailbox commands issued from
1242 * lpfc_sli_issue_mbox_wait function. This function is called by the
1243 * mailbox event handler function with no lock held. This function
1244 * will wake up thread waiting on the wait queue pointed by context1
1245 * of the mailbox.
1246 **/
dea31012005-04-17 16:05:31 -05001247static void
James Smart2e0fef82007-06-17 19:56:36 -05001248lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001249{
1250 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001251 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001252
1253 /*
1254 * If pdone_q is empty, the driver thread gave up waiting and
1255 * continued running.
1256 */
James Smart7054a602007-04-25 09:52:34 -04001257 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001258 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001259 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1260 if (pdone_q)
1261 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001262 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001263 return;
1264}
1265
James Smarte59058c2008-08-24 21:49:00 -04001266
1267/**
James Smart3621a712009-04-06 18:47:14 -04001268 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001269 * @phba: Pointer to HBA context object.
1270 * @pmb: Pointer to mailbox object.
1271 *
1272 * This function is the default mailbox completion handler. It
1273 * frees the memory resources associated with the completed mailbox
1274 * command. If the completed command is a REG_LOGIN mailbox command,
1275 * this function will issue a UREG_LOGIN to re-claim the RPI.
1276 **/
dea31012005-04-17 16:05:31 -05001277void
James Smart2e0fef82007-06-17 19:56:36 -05001278lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001279{
1280 struct lpfc_dmabuf *mp;
James Smart7054a602007-04-25 09:52:34 -04001281 uint16_t rpi;
1282 int rc;
1283
dea31012005-04-17 16:05:31 -05001284 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001285
dea31012005-04-17 16:05:31 -05001286 if (mp) {
1287 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1288 kfree(mp);
1289 }
James Smart7054a602007-04-25 09:52:34 -04001290
1291 /*
1292 * If a REG_LOGIN succeeded after node is destroyed or node
1293 * is in re-discovery driver need to cleanup the RPI.
1294 */
James Smart2e0fef82007-06-17 19:56:36 -05001295 if (!(phba->pport->load_flag & FC_UNLOADING) &&
1296 pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1297 !pmb->mb.mbxStatus) {
James Smart7054a602007-04-25 09:52:34 -04001298
1299 rpi = pmb->mb.un.varWords[0];
James Smart92d7f7b2007-06-17 19:56:38 -05001300 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1301 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001302 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1303 if (rc != MBX_NOT_FINISHED)
1304 return;
1305 }
1306
James Smart2e0fef82007-06-17 19:56:36 -05001307 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001308 return;
1309}
1310
James Smarte59058c2008-08-24 21:49:00 -04001311/**
James Smart3621a712009-04-06 18:47:14 -04001312 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001313 * @phba: Pointer to HBA context object.
1314 *
1315 * This function is called with no lock held. This function processes all
1316 * the completed mailbox commands and gives it to upper layers. The interrupt
1317 * service routine processes mailbox completion interrupt and adds completed
1318 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1319 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1320 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1321 * function returns the mailbox commands to the upper layer by calling the
1322 * completion handler function of each mailbox.
1323 **/
dea31012005-04-17 16:05:31 -05001324int
James Smart2e0fef82007-06-17 19:56:36 -05001325lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001326{
James Smart92d7f7b2007-06-17 19:56:38 -05001327 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001328 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001329 int rc;
1330 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001331
1332 phba->sli.slistat.mbox_event++;
1333
James Smart92d7f7b2007-06-17 19:56:38 -05001334 /* Get all completed mailboxe buffers into the cmplq */
1335 spin_lock_irq(&phba->hbalock);
1336 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1337 spin_unlock_irq(&phba->hbalock);
1338
dea31012005-04-17 16:05:31 -05001339 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001340 do {
1341 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1342 if (pmb == NULL)
1343 break;
1344
dea31012005-04-17 16:05:31 -05001345 pmbox = &pmb->mb;
dea31012005-04-17 16:05:31 -05001346
James Smart858c9f62007-06-17 19:56:39 -05001347 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1348 if (pmb->vport) {
1349 lpfc_debugfs_disc_trc(pmb->vport,
1350 LPFC_DISC_TRC_MBOX_VPORT,
1351 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1352 (uint32_t)pmbox->mbxCommand,
1353 pmbox->un.varWords[0],
1354 pmbox->un.varWords[1]);
1355 }
1356 else {
1357 lpfc_debugfs_disc_trc(phba->pport,
1358 LPFC_DISC_TRC_MBOX,
1359 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1360 (uint32_t)pmbox->mbxCommand,
1361 pmbox->un.varWords[0],
1362 pmbox->un.varWords[1]);
1363 }
1364 }
1365
dea31012005-04-17 16:05:31 -05001366 /*
1367 * It is a fatal error if unknown mbox command completion.
1368 */
1369 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1370 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001371 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001372 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001373 "(%d):0323 Unknown Mailbox command "
James Smart92d7f7b2007-06-17 19:56:38 -05001374 "%x Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001375 pmb->vport ? pmb->vport->vpi : 0,
1376 pmbox->mbxCommand);
James Smart2e0fef82007-06-17 19:56:36 -05001377 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001378 phba->work_hs = HS_FFER3;
1379 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001380 continue;
dea31012005-04-17 16:05:31 -05001381 }
1382
dea31012005-04-17 16:05:31 -05001383 if (pmbox->mbxStatus) {
1384 phba->sli.slistat.mbox_stat_err++;
1385 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1386 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001387 lpfc_printf_log(phba, KERN_INFO,
1388 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001389 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001390 "error - RETRYing Data: x%x "
1391 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001392 pmb->vport ? pmb->vport->vpi :0,
1393 pmbox->mbxCommand,
1394 pmbox->mbxStatus,
1395 pmbox->un.varWords[0],
1396 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001397 pmbox->mbxStatus = 0;
1398 pmbox->mbxOwner = OWN_HOST;
James Smart2e0fef82007-06-17 19:56:36 -05001399 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001400 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001401 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001402 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1403 if (rc == MBX_SUCCESS)
James Smart92d7f7b2007-06-17 19:56:38 -05001404 continue;
dea31012005-04-17 16:05:31 -05001405 }
1406 }
1407
1408 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001409 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001410 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
dea31012005-04-17 16:05:31 -05001411 "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 -05001412 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001413 pmbox->mbxCommand,
1414 pmb->mbox_cmpl,
1415 *((uint32_t *) pmbox),
1416 pmbox->un.varWords[0],
1417 pmbox->un.varWords[1],
1418 pmbox->un.varWords[2],
1419 pmbox->un.varWords[3],
1420 pmbox->un.varWords[4],
1421 pmbox->un.varWords[5],
1422 pmbox->un.varWords[6],
1423 pmbox->un.varWords[7]);
1424
James Smart92d7f7b2007-06-17 19:56:38 -05001425 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001426 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001427 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001428 return 0;
dea31012005-04-17 16:05:31 -05001429}
James Smart92d7f7b2007-06-17 19:56:38 -05001430
James Smarte59058c2008-08-24 21:49:00 -04001431/**
James Smart3621a712009-04-06 18:47:14 -04001432 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001433 * @phba: Pointer to HBA context object.
1434 * @pring: Pointer to driver SLI ring object.
1435 * @tag: buffer tag.
1436 *
1437 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1438 * is set in the tag the buffer is posted for a particular exchange,
1439 * the function will return the buffer without replacing the buffer.
1440 * If the buffer is for unsolicited ELS or CT traffic, this function
1441 * returns the buffer and also posts another buffer to the firmware.
1442 **/
James Smart76bb24e2007-10-27 13:38:00 -04001443static struct lpfc_dmabuf *
1444lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001445 struct lpfc_sli_ring *pring,
1446 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001447{
James Smart9f1e1b52008-12-04 22:39:40 -05001448 struct hbq_dmabuf *hbq_entry;
1449
James Smart76bb24e2007-10-27 13:38:00 -04001450 if (tag & QUE_BUFTAG_BIT)
1451 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001452 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1453 if (!hbq_entry)
1454 return NULL;
1455 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001456}
James Smart57127f12007-10-27 13:37:05 -04001457
James Smart3772a992009-05-22 14:50:54 -04001458/**
1459 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1460 * @phba: Pointer to HBA context object.
1461 * @pring: Pointer to driver SLI ring object.
1462 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1463 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1464 * @fch_type: the type for the first frame of the sequence.
1465 *
1466 * This function is called with no lock held. This function uses the r_ctl and
1467 * type of the received sequence to find the correct callback function to call
1468 * to process the sequence.
1469 **/
1470static int
1471lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1472 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1473 uint32_t fch_type)
1474{
1475 int i;
1476
1477 /* unSolicited Responses */
1478 if (pring->prt[0].profile) {
1479 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1480 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1481 saveq);
1482 return 1;
1483 }
1484 /* We must search, based on rctl / type
1485 for the right routine */
1486 for (i = 0; i < pring->num_mask; i++) {
1487 if ((pring->prt[i].rctl == fch_r_ctl) &&
1488 (pring->prt[i].type == fch_type)) {
1489 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1490 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1491 (phba, pring, saveq);
1492 return 1;
1493 }
1494 }
1495 return 0;
1496}
James Smarte59058c2008-08-24 21:49:00 -04001497
1498/**
James Smart3621a712009-04-06 18:47:14 -04001499 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001500 * @phba: Pointer to HBA context object.
1501 * @pring: Pointer to driver SLI ring object.
1502 * @saveq: Pointer to the unsolicited iocb.
1503 *
1504 * This function is called with no lock held by the ring event handler
1505 * when there is an unsolicited iocb posted to the response ring by the
1506 * firmware. This function gets the buffer associated with the iocbs
1507 * and calls the event handler for the ring. This function handles both
1508 * qring buffers and hbq buffers.
1509 * When the function returns 1 the caller can free the iocb object otherwise
1510 * upper layer functions will free the iocb objects.
1511 **/
dea31012005-04-17 16:05:31 -05001512static int
1513lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1514 struct lpfc_iocbq *saveq)
1515{
1516 IOCB_t * irsp;
1517 WORD5 * w5p;
1518 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001519 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04001520 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001521 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001522
1523 match = 0;
1524 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001525
1526 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1527 if (pring->lpfc_sli_rcv_async_status)
1528 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1529 else
1530 lpfc_printf_log(phba,
1531 KERN_WARNING,
1532 LOG_SLI,
1533 "0316 Ring %d handler: unexpected "
1534 "ASYNC_STATUS iocb received evt_code "
1535 "0x%x\n",
1536 pring->ringno,
1537 irsp->un.asyncstat.evt_code);
1538 return 1;
1539 }
1540
James Smart3163f722008-02-08 18:50:25 -05001541 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1542 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1543 if (irsp->ulpBdeCount > 0) {
1544 dmzbuf = lpfc_sli_get_buff(phba, pring,
1545 irsp->un.ulpWord[3]);
1546 lpfc_in_buf_free(phba, dmzbuf);
1547 }
1548
1549 if (irsp->ulpBdeCount > 1) {
1550 dmzbuf = lpfc_sli_get_buff(phba, pring,
1551 irsp->unsli3.sli3Words[3]);
1552 lpfc_in_buf_free(phba, dmzbuf);
1553 }
1554
1555 if (irsp->ulpBdeCount > 2) {
1556 dmzbuf = lpfc_sli_get_buff(phba, pring,
1557 irsp->unsli3.sli3Words[7]);
1558 lpfc_in_buf_free(phba, dmzbuf);
1559 }
1560
1561 return 1;
1562 }
1563
James Smart92d7f7b2007-06-17 19:56:38 -05001564 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001565 if (irsp->ulpBdeCount != 0) {
1566 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001567 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001568 if (!saveq->context2)
1569 lpfc_printf_log(phba,
1570 KERN_ERR,
1571 LOG_SLI,
1572 "0341 Ring %d Cannot find buffer for "
1573 "an unsolicited iocb. tag 0x%x\n",
1574 pring->ringno,
1575 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001576 }
1577 if (irsp->ulpBdeCount == 2) {
1578 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04001579 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04001580 if (!saveq->context3)
1581 lpfc_printf_log(phba,
1582 KERN_ERR,
1583 LOG_SLI,
1584 "0342 Ring %d Cannot find buffer for an"
1585 " unsolicited iocb. tag 0x%x\n",
1586 pring->ringno,
1587 irsp->unsli3.sli3Words[7]);
1588 }
1589 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04001590 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04001591 if (irsp->ulpBdeCount != 0) {
1592 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1593 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05001594 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04001595 lpfc_printf_log(phba,
1596 KERN_ERR,
1597 LOG_SLI,
1598 "0343 Ring %d Cannot find "
1599 "buffer for an unsolicited iocb"
1600 ". tag 0x%x\n", pring->ringno,
1601 irsp->un.ulpWord[3]);
1602 }
1603 if (irsp->ulpBdeCount == 2) {
1604 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1605 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05001606 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04001607 lpfc_printf_log(phba,
1608 KERN_ERR,
1609 LOG_SLI,
1610 "0344 Ring %d Cannot find "
1611 "buffer for an unsolicited "
1612 "iocb. tag 0x%x\n",
1613 pring->ringno,
1614 irsp->unsli3.sli3Words[7]);
1615 }
1616 }
James Smart92d7f7b2007-06-17 19:56:38 -05001617 }
James Smart9c2face2008-01-11 01:53:18 -05001618 if (irsp->ulpBdeCount != 0 &&
1619 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1620 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1621 int found = 0;
1622
1623 /* search continue save q for same XRI */
1624 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1625 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1626 list_add_tail(&saveq->list, &iocbq->list);
1627 found = 1;
1628 break;
1629 }
1630 }
1631 if (!found)
1632 list_add_tail(&saveq->clist,
1633 &pring->iocb_continue_saveq);
1634 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1635 list_del_init(&iocbq->clist);
1636 saveq = iocbq;
1637 irsp = &(saveq->iocb);
1638 } else
1639 return 0;
1640 }
1641 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1642 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1643 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1644 Rctl = FC_ELS_REQ;
1645 Type = FC_ELS_DATA;
1646 } else {
1647 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1648 Rctl = w5p->hcsw.Rctl;
1649 Type = w5p->hcsw.Type;
1650
1651 /* Firmware Workaround */
1652 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1653 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1654 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1655 Rctl = FC_ELS_REQ;
1656 Type = FC_ELS_DATA;
1657 w5p->hcsw.Rctl = Rctl;
1658 w5p->hcsw.Type = Type;
1659 }
1660 }
James Smart92d7f7b2007-06-17 19:56:38 -05001661
James Smart3772a992009-05-22 14:50:54 -04001662 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05001663 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001664 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05001665 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04001666 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04001667
James Smart92d7f7b2007-06-17 19:56:38 -05001668 return 1;
dea31012005-04-17 16:05:31 -05001669}
1670
James Smarte59058c2008-08-24 21:49:00 -04001671/**
James Smart3621a712009-04-06 18:47:14 -04001672 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04001673 * @phba: Pointer to HBA context object.
1674 * @pring: Pointer to driver SLI ring object.
1675 * @prspiocb: Pointer to response iocb object.
1676 *
1677 * This function looks up the iocb_lookup table to get the command iocb
1678 * corresponding to the given response iocb using the iotag of the
1679 * response iocb. This function is called with the hbalock held.
1680 * This function returns the command iocb object if it finds the command
1681 * iocb else returns NULL.
1682 **/
dea31012005-04-17 16:05:31 -05001683static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001684lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1685 struct lpfc_sli_ring *pring,
1686 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05001687{
dea31012005-04-17 16:05:31 -05001688 struct lpfc_iocbq *cmd_iocb = NULL;
1689 uint16_t iotag;
1690
James Bottomley604a3e32005-10-29 10:28:33 -05001691 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05001692
James Bottomley604a3e32005-10-29 10:28:33 -05001693 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1694 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05001695 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05001696 pring->txcmplq_cnt--;
1697 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001698 }
1699
dea31012005-04-17 16:05:31 -05001700 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001701 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05001702 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001703 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05001704 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05001705 return NULL;
1706}
1707
James Smarte59058c2008-08-24 21:49:00 -04001708/**
James Smart3772a992009-05-22 14:50:54 -04001709 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
1710 * @phba: Pointer to HBA context object.
1711 * @pring: Pointer to driver SLI ring object.
1712 * @iotag: IOCB tag.
1713 *
1714 * This function looks up the iocb_lookup table to get the command iocb
1715 * corresponding to the given iotag. This function is called with the
1716 * hbalock held.
1717 * This function returns the command iocb object if it finds the command
1718 * iocb else returns NULL.
1719 **/
1720static struct lpfc_iocbq *
1721lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
1722 struct lpfc_sli_ring *pring, uint16_t iotag)
1723{
1724 struct lpfc_iocbq *cmd_iocb;
1725
1726 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1727 cmd_iocb = phba->sli.iocbq_lookup[iotag];
1728 list_del_init(&cmd_iocb->list);
1729 pring->txcmplq_cnt--;
1730 return cmd_iocb;
1731 }
1732
1733 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1734 "0372 iotag x%x is out off range: max iotag (x%x)\n",
1735 iotag, phba->sli.last_iotag);
1736 return NULL;
1737}
1738
1739/**
James Smart3621a712009-04-06 18:47:14 -04001740 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04001741 * @phba: Pointer to HBA context object.
1742 * @pring: Pointer to driver SLI ring object.
1743 * @saveq: Pointer to the response iocb to be processed.
1744 *
1745 * This function is called by the ring event handler for non-fcp
1746 * rings when there is a new response iocb in the response ring.
1747 * The caller is not required to hold any locks. This function
1748 * gets the command iocb associated with the response iocb and
1749 * calls the completion handler for the command iocb. If there
1750 * is no completion handler, the function will free the resources
1751 * associated with command iocb. If the response iocb is for
1752 * an already aborted command iocb, the status of the completion
1753 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1754 * This function always returns 1.
1755 **/
dea31012005-04-17 16:05:31 -05001756static int
James Smart2e0fef82007-06-17 19:56:36 -05001757lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05001758 struct lpfc_iocbq *saveq)
1759{
James Smart2e0fef82007-06-17 19:56:36 -05001760 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05001761 int rc = 1;
1762 unsigned long iflag;
1763
1764 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05001765 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001766 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05001767 spin_unlock_irqrestore(&phba->hbalock, iflag);
1768
dea31012005-04-17 16:05:31 -05001769 if (cmdiocbp) {
1770 if (cmdiocbp->iocb_cmpl) {
1771 /*
James Smartea2151b2008-09-07 11:52:10 -04001772 * If an ELS command failed send an event to mgmt
1773 * application.
1774 */
1775 if (saveq->iocb.ulpStatus &&
1776 (pring->ringno == LPFC_ELS_RING) &&
1777 (cmdiocbp->iocb.ulpCommand ==
1778 CMD_ELS_REQUEST64_CR))
1779 lpfc_send_els_failure_event(phba,
1780 cmdiocbp, saveq);
1781
1782 /*
dea31012005-04-17 16:05:31 -05001783 * Post all ELS completions to the worker thread.
1784 * All other are passed to the completion callback.
1785 */
1786 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04001787 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1788 cmdiocbp->iocb_flag &=
1789 ~LPFC_DRIVER_ABORTED;
1790 saveq->iocb.ulpStatus =
1791 IOSTAT_LOCAL_REJECT;
1792 saveq->iocb.un.ulpWord[4] =
1793 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05001794
1795 /* Firmware could still be in progress
1796 * of DMAing payload, so don't free data
1797 * buffer till after a hbeat.
1798 */
1799 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04001800 }
dea31012005-04-17 16:05:31 -05001801 }
James Smart2e0fef82007-06-17 19:56:36 -05001802 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05001803 } else
1804 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05001805 } else {
1806 /*
1807 * Unknown initiating command based on the response iotag.
1808 * This could be the case on the ELS ring because of
1809 * lpfc_els_abort().
1810 */
1811 if (pring->ringno != LPFC_ELS_RING) {
1812 /*
1813 * Ring <ringno> handler: unexpected completion IoTag
1814 * <IoTag>
1815 */
James Smarta257bf92009-04-06 18:48:10 -04001816 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001817 "0322 Ring %d handler: "
1818 "unexpected completion IoTag x%x "
1819 "Data: x%x x%x x%x x%x\n",
1820 pring->ringno,
1821 saveq->iocb.ulpIoTag,
1822 saveq->iocb.ulpStatus,
1823 saveq->iocb.un.ulpWord[4],
1824 saveq->iocb.ulpCommand,
1825 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05001826 }
1827 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001828
dea31012005-04-17 16:05:31 -05001829 return rc;
1830}
1831
James Smarte59058c2008-08-24 21:49:00 -04001832/**
James Smart3621a712009-04-06 18:47:14 -04001833 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04001834 * @phba: Pointer to HBA context object.
1835 * @pring: Pointer to driver SLI ring object.
1836 *
1837 * This function is called from the iocb ring event handlers when
1838 * put pointer is ahead of the get pointer for a ring. This function signal
1839 * an error attention condition to the worker thread and the worker
1840 * thread will transition the HBA to offline state.
1841 **/
James Smart2e0fef82007-06-17 19:56:36 -05001842static void
1843lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001844{
James Smart34b02dc2008-08-24 21:49:55 -04001845 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001846 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001847 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001848 * rsp ring <portRspMax>
1849 */
1850 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001851 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001852 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001853 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001854 pring->numRiocb);
1855
James Smart2e0fef82007-06-17 19:56:36 -05001856 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001857
1858 /*
1859 * All error attention handlers are posted to
1860 * worker thread
1861 */
1862 phba->work_ha |= HA_ERATT;
1863 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001864
James Smart5e9d9b82008-06-14 22:52:53 -04001865 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001866
1867 return;
1868}
1869
James Smarte59058c2008-08-24 21:49:00 -04001870/**
James Smart3621a712009-04-06 18:47:14 -04001871 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04001872 * @ptr: Pointer to address of HBA context object.
1873 *
1874 * This function is invoked by the Error Attention polling timer when the
1875 * timer times out. It will check the SLI Error Attention register for
1876 * possible attention events. If so, it will post an Error Attention event
1877 * and wake up worker thread to process it. Otherwise, it will set up the
1878 * Error Attention polling timer for the next poll.
1879 **/
1880void lpfc_poll_eratt(unsigned long ptr)
1881{
1882 struct lpfc_hba *phba;
1883 uint32_t eratt = 0;
1884
1885 phba = (struct lpfc_hba *)ptr;
1886
1887 /* Check chip HA register for error event */
1888 eratt = lpfc_sli_check_eratt(phba);
1889
1890 if (eratt)
1891 /* Tell the worker thread there is work to do */
1892 lpfc_worker_wake_up(phba);
1893 else
1894 /* Restart the timer for next eratt poll */
1895 mod_timer(&phba->eratt_poll, jiffies +
1896 HZ * LPFC_ERATT_POLL_INTERVAL);
1897 return;
1898}
1899
1900/**
James Smart3621a712009-04-06 18:47:14 -04001901 * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
James Smarte59058c2008-08-24 21:49:00 -04001902 * @phba: Pointer to HBA context object.
1903 *
1904 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1905 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1906 * is enabled.
1907 *
1908 * The caller does not hold any lock.
1909 * The function processes each response iocb in the response ring until it
1910 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1911 * LE bit set. The function will call the completion handler of the command iocb
1912 * if the response iocb indicates a completion for a command iocb or it is
1913 * an abort completion.
1914 **/
James Smart2e0fef82007-06-17 19:56:36 -05001915void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001916{
James Smart2e0fef82007-06-17 19:56:36 -05001917 struct lpfc_sli *psli = &phba->sli;
1918 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001919 IOCB_t *irsp = NULL;
1920 IOCB_t *entry = NULL;
1921 struct lpfc_iocbq *cmdiocbq = NULL;
1922 struct lpfc_iocbq rspiocbq;
James Smart34b02dc2008-08-24 21:49:55 -04001923 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001924 uint32_t status;
1925 uint32_t portRspPut, portRspMax;
1926 int type;
1927 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001928 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05001929 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001930
1931 pring->stats.iocb_event++;
1932
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001933 /*
1934 * The next available response entry should never exceed the maximum
1935 * entries. If it does, treat it as an adapter hardware error.
1936 */
1937 portRspMax = pring->numRiocb;
1938 portRspPut = le32_to_cpu(pgp->rspPutInx);
1939 if (unlikely(portRspPut >= portRspMax)) {
1940 lpfc_sli_rsp_pointers_error(phba, pring);
1941 return;
1942 }
1943
1944 rmb();
1945 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05001946 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001947 if (++pring->rspidx >= portRspMax)
1948 pring->rspidx = 0;
1949
1950 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1951 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05001952 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001953 irsp = &rspiocbq.iocb;
1954 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1955 pring->stats.iocb_rsp++;
1956 rsp_cmpl++;
1957
1958 if (unlikely(irsp->ulpStatus)) {
1959 /* Rsp ring <ringno> error: IOCB */
1960 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001961 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001962 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001963 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001964 irsp->un.ulpWord[0],
1965 irsp->un.ulpWord[1],
1966 irsp->un.ulpWord[2],
1967 irsp->un.ulpWord[3],
1968 irsp->un.ulpWord[4],
1969 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04001970 *(uint32_t *)&irsp->un1,
1971 *((uint32_t *)&irsp->un1 + 1));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001972 }
1973
1974 switch (type) {
1975 case LPFC_ABORT_IOCB:
1976 case LPFC_SOL_IOCB:
1977 /*
1978 * Idle exchange closed via ABTS from port. No iocb
1979 * resources need to be recovered.
1980 */
1981 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04001982 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001983 "0314 IOCB cmd 0x%x "
1984 "processed. Skipping "
1985 "completion",
James Smartdca94792006-08-01 07:34:08 -04001986 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001987 break;
1988 }
1989
James Smart2e0fef82007-06-17 19:56:36 -05001990 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001991 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1992 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001993 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001994 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1995 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1996 &rspiocbq);
1997 }
1998 break;
1999 default:
2000 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2001 char adaptermsg[LPFC_MAX_ADPTMSG];
2002 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2003 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2004 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002005 dev_warn(&((phba->pcidev)->dev),
2006 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002007 phba->brd_no, adaptermsg);
2008 } else {
2009 /* Unknown IOCB command */
2010 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002011 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002012 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002013 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002014 irsp->ulpStatus,
2015 irsp->ulpIoTag,
2016 irsp->ulpContext);
2017 }
2018 break;
2019 }
2020
2021 /*
2022 * The response IOCB has been processed. Update the ring
2023 * pointer in SLIM. If the port response put pointer has not
2024 * been updated, sync the pgp->rspPutInx and fetch the new port
2025 * response put pointer.
2026 */
James Smarted957682007-06-17 19:56:37 -05002027 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002028
2029 if (pring->rspidx == portRspPut)
2030 portRspPut = le32_to_cpu(pgp->rspPutInx);
2031 }
2032
2033 ha_copy = readl(phba->HAregaddr);
2034 ha_copy >>= (LPFC_FCP_RING * 4);
2035
2036 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05002037 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002038 pring->stats.iocb_rsp_full++;
2039 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
2040 writel(status, phba->CAregaddr);
2041 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002042 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002043 }
2044 if ((ha_copy & HA_R0CE_RSP) &&
2045 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002046 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002047 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2048 pring->stats.iocb_cmd_empty++;
2049
2050 /* Force update of the local copy of cmdGetInx */
2051 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2052 lpfc_sli_resume_iocb(phba, pring);
2053
2054 if ((pring->lpfc_sli_cmd_available))
2055 (pring->lpfc_sli_cmd_available) (phba, pring);
2056
James Smart2e0fef82007-06-17 19:56:36 -05002057 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002058 }
2059
2060 return;
2061}
2062
James Smarte59058c2008-08-24 21:49:00 -04002063/**
James Smart3621a712009-04-06 18:47:14 -04002064 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002065 * @phba: Pointer to HBA context object.
2066 * @pring: Pointer to driver SLI ring object.
2067 * @mask: Host attention register mask for this ring.
2068 *
2069 * This function is called from the interrupt context when there is a ring
2070 * event for the fcp ring. The caller does not hold any lock.
2071 * The function processes each response iocb in the response ring until it
2072 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2073 * LE bit set. The function will call the completion handler of the command iocb
2074 * if the response iocb indicates a completion for a command iocb or it is
2075 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2076 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002077 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04002078 * to check it explicitly. This function always returns 1.
2079 **/
dea31012005-04-17 16:05:31 -05002080static int
James Smart2e0fef82007-06-17 19:56:36 -05002081lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2082 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002083{
James Smart34b02dc2008-08-24 21:49:55 -04002084 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002085 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002086 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002087 struct lpfc_iocbq *cmdiocbq = NULL;
2088 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002089 uint32_t status;
2090 uint32_t portRspPut, portRspMax;
2091 int rc = 1;
2092 lpfc_iocb_type type;
2093 unsigned long iflag;
2094 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002095
James Smart2e0fef82007-06-17 19:56:36 -05002096 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002097 pring->stats.iocb_event++;
2098
dea31012005-04-17 16:05:31 -05002099 /*
2100 * The next available response entry should never exceed the maximum
2101 * entries. If it does, treat it as an adapter hardware error.
2102 */
2103 portRspMax = pring->numRiocb;
2104 portRspPut = le32_to_cpu(pgp->rspPutInx);
2105 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002106 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002107 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002108 return 1;
2109 }
2110
2111 rmb();
2112 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002113 /*
2114 * Fetch an entry off the ring and copy it into a local data
2115 * structure. The copy involves a byte-swap since the
2116 * network byte order and pci byte orders are different.
2117 */
James Smarted957682007-06-17 19:56:37 -05002118 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002119 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002120
2121 if (++pring->rspidx >= portRspMax)
2122 pring->rspidx = 0;
2123
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002124 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2125 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002126 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002127 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002128 irsp = &rspiocbq.iocb;
2129
dea31012005-04-17 16:05:31 -05002130 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2131 pring->stats.iocb_rsp++;
2132 rsp_cmpl++;
2133
2134 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002135 /*
2136 * If resource errors reported from HBA, reduce
2137 * queuedepths of the SCSI device.
2138 */
2139 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2140 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2141 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002142 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002143 spin_lock_irqsave(&phba->hbalock, iflag);
2144 }
2145
dea31012005-04-17 16:05:31 -05002146 /* Rsp ring <ringno> error: IOCB */
2147 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002148 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002149 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002150 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002151 irsp->un.ulpWord[0],
2152 irsp->un.ulpWord[1],
2153 irsp->un.ulpWord[2],
2154 irsp->un.ulpWord[3],
2155 irsp->un.ulpWord[4],
2156 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002157 *(uint32_t *)&irsp->un1,
2158 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002159 }
2160
2161 switch (type) {
2162 case LPFC_ABORT_IOCB:
2163 case LPFC_SOL_IOCB:
2164 /*
2165 * Idle exchange closed via ABTS from port. No iocb
2166 * resources need to be recovered.
2167 */
2168 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002169 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002170 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002171 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002172 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002173 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002174 break;
2175 }
2176
James Bottomley604a3e32005-10-29 10:28:33 -05002177 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2178 &rspiocbq);
dea31012005-04-17 16:05:31 -05002179 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002180 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2181 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2182 &rspiocbq);
2183 } else {
James Smart2e0fef82007-06-17 19:56:36 -05002184 spin_unlock_irqrestore(&phba->hbalock,
2185 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002186 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2187 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002188 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002189 iflag);
2190 }
dea31012005-04-17 16:05:31 -05002191 }
2192 break;
James Smarta4bc3372006-12-02 13:34:16 -05002193 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002194 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002195 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002196 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002197 break;
dea31012005-04-17 16:05:31 -05002198 default:
2199 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2200 char adaptermsg[LPFC_MAX_ADPTMSG];
2201 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2202 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2203 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002204 dev_warn(&((phba->pcidev)->dev),
2205 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002206 phba->brd_no, adaptermsg);
2207 } else {
2208 /* Unknown IOCB command */
2209 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002210 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002211 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002212 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002213 irsp->ulpStatus,
2214 irsp->ulpIoTag,
2215 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002216 }
2217 break;
2218 }
2219
2220 /*
2221 * The response IOCB has been processed. Update the ring
2222 * pointer in SLIM. If the port response put pointer has not
2223 * been updated, sync the pgp->rspPutInx and fetch the new port
2224 * response put pointer.
2225 */
James Smarted957682007-06-17 19:56:37 -05002226 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002227
2228 if (pring->rspidx == portRspPut)
2229 portRspPut = le32_to_cpu(pgp->rspPutInx);
2230 }
2231
2232 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2233 pring->stats.iocb_rsp_full++;
2234 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2235 writel(status, phba->CAregaddr);
2236 readl(phba->CAregaddr);
2237 }
2238 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2239 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2240 pring->stats.iocb_cmd_empty++;
2241
2242 /* Force update of the local copy of cmdGetInx */
2243 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2244 lpfc_sli_resume_iocb(phba, pring);
2245
2246 if ((pring->lpfc_sli_cmd_available))
2247 (pring->lpfc_sli_cmd_available) (phba, pring);
2248
2249 }
2250
James Smart2e0fef82007-06-17 19:56:36 -05002251 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002252 return rc;
2253}
2254
James Smarte59058c2008-08-24 21:49:00 -04002255/**
James Smart3772a992009-05-22 14:50:54 -04002256 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2257 * @phba: Pointer to HBA context object.
2258 * @pring: Pointer to driver SLI ring object.
2259 * @rspiocbp: Pointer to driver response IOCB object.
2260 *
2261 * This function is called from the worker thread when there is a slow-path
2262 * response IOCB to process. This function chains all the response iocbs until
2263 * seeing the iocb with the LE bit set. The function will call
2264 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2265 * completion of a command iocb. The function will call the
2266 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2267 * The function frees the resources or calls the completion handler if this
2268 * iocb is an abort completion. The function returns NULL when the response
2269 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2270 * this function shall chain the iocb on to the iocb_continueq and return the
2271 * response iocb passed in.
2272 **/
2273static struct lpfc_iocbq *
2274lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2275 struct lpfc_iocbq *rspiocbp)
2276{
2277 struct lpfc_iocbq *saveq;
2278 struct lpfc_iocbq *cmdiocbp;
2279 struct lpfc_iocbq *next_iocb;
2280 IOCB_t *irsp = NULL;
2281 uint32_t free_saveq;
2282 uint8_t iocb_cmd_type;
2283 lpfc_iocb_type type;
2284 unsigned long iflag;
2285 int rc;
2286
2287 spin_lock_irqsave(&phba->hbalock, iflag);
2288 /* First add the response iocb to the countinueq list */
2289 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2290 pring->iocb_continueq_cnt++;
2291
2292 /* Now, determine whetehr the list is completed for processing */
2293 irsp = &rspiocbp->iocb;
2294 if (irsp->ulpLe) {
2295 /*
2296 * By default, the driver expects to free all resources
2297 * associated with this iocb completion.
2298 */
2299 free_saveq = 1;
2300 saveq = list_get_first(&pring->iocb_continueq,
2301 struct lpfc_iocbq, list);
2302 irsp = &(saveq->iocb);
2303 list_del_init(&pring->iocb_continueq);
2304 pring->iocb_continueq_cnt = 0;
2305
2306 pring->stats.iocb_rsp++;
2307
2308 /*
2309 * If resource errors reported from HBA, reduce
2310 * queuedepths of the SCSI device.
2311 */
2312 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2313 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2314 spin_unlock_irqrestore(&phba->hbalock, iflag);
2315 phba->lpfc_rampdown_queue_depth(phba);
2316 spin_lock_irqsave(&phba->hbalock, iflag);
2317 }
2318
2319 if (irsp->ulpStatus) {
2320 /* Rsp ring <ringno> error: IOCB */
2321 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2322 "0328 Rsp Ring %d error: "
2323 "IOCB Data: "
2324 "x%x x%x x%x x%x "
2325 "x%x x%x x%x x%x "
2326 "x%x x%x x%x x%x "
2327 "x%x x%x x%x x%x\n",
2328 pring->ringno,
2329 irsp->un.ulpWord[0],
2330 irsp->un.ulpWord[1],
2331 irsp->un.ulpWord[2],
2332 irsp->un.ulpWord[3],
2333 irsp->un.ulpWord[4],
2334 irsp->un.ulpWord[5],
2335 *(((uint32_t *) irsp) + 6),
2336 *(((uint32_t *) irsp) + 7),
2337 *(((uint32_t *) irsp) + 8),
2338 *(((uint32_t *) irsp) + 9),
2339 *(((uint32_t *) irsp) + 10),
2340 *(((uint32_t *) irsp) + 11),
2341 *(((uint32_t *) irsp) + 12),
2342 *(((uint32_t *) irsp) + 13),
2343 *(((uint32_t *) irsp) + 14),
2344 *(((uint32_t *) irsp) + 15));
2345 }
2346
2347 /*
2348 * Fetch the IOCB command type and call the correct completion
2349 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2350 * get freed back to the lpfc_iocb_list by the discovery
2351 * kernel thread.
2352 */
2353 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2354 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2355 switch (type) {
2356 case LPFC_SOL_IOCB:
2357 spin_unlock_irqrestore(&phba->hbalock, iflag);
2358 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2359 spin_lock_irqsave(&phba->hbalock, iflag);
2360 break;
2361
2362 case LPFC_UNSOL_IOCB:
2363 spin_unlock_irqrestore(&phba->hbalock, iflag);
2364 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2365 spin_lock_irqsave(&phba->hbalock, iflag);
2366 if (!rc)
2367 free_saveq = 0;
2368 break;
2369
2370 case LPFC_ABORT_IOCB:
2371 cmdiocbp = NULL;
2372 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2373 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2374 saveq);
2375 if (cmdiocbp) {
2376 /* Call the specified completion routine */
2377 if (cmdiocbp->iocb_cmpl) {
2378 spin_unlock_irqrestore(&phba->hbalock,
2379 iflag);
2380 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2381 saveq);
2382 spin_lock_irqsave(&phba->hbalock,
2383 iflag);
2384 } else
2385 __lpfc_sli_release_iocbq(phba,
2386 cmdiocbp);
2387 }
2388 break;
2389
2390 case LPFC_UNKNOWN_IOCB:
2391 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2392 char adaptermsg[LPFC_MAX_ADPTMSG];
2393 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2394 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2395 MAX_MSG_DATA);
2396 dev_warn(&((phba->pcidev)->dev),
2397 "lpfc%d: %s\n",
2398 phba->brd_no, adaptermsg);
2399 } else {
2400 /* Unknown IOCB command */
2401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2402 "0335 Unknown IOCB "
2403 "command Data: x%x "
2404 "x%x x%x x%x\n",
2405 irsp->ulpCommand,
2406 irsp->ulpStatus,
2407 irsp->ulpIoTag,
2408 irsp->ulpContext);
2409 }
2410 break;
2411 }
2412
2413 if (free_saveq) {
2414 list_for_each_entry_safe(rspiocbp, next_iocb,
2415 &saveq->list, list) {
2416 list_del(&rspiocbp->list);
2417 __lpfc_sli_release_iocbq(phba, rspiocbp);
2418 }
2419 __lpfc_sli_release_iocbq(phba, saveq);
2420 }
2421 rspiocbp = NULL;
2422 }
2423 spin_unlock_irqrestore(&phba->hbalock, iflag);
2424 return rspiocbp;
2425}
2426
2427/**
2428 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002429 * @phba: Pointer to HBA context object.
2430 * @pring: Pointer to driver SLI ring object.
2431 * @mask: Host attention register mask for this ring.
2432 *
James Smart3772a992009-05-22 14:50:54 -04002433 * This routine wraps the actual slow_ring event process routine from the
2434 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002435 **/
James Smart3772a992009-05-22 14:50:54 -04002436void
James Smart2e0fef82007-06-17 19:56:36 -05002437lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2438 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002439{
James Smart3772a992009-05-22 14:50:54 -04002440 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2441}
2442
2443/**
2444 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2445 * @phba: Pointer to HBA context object.
2446 * @pring: Pointer to driver SLI ring object.
2447 * @mask: Host attention register mask for this ring.
2448 *
2449 * This function is called from the worker thread when there is a ring event
2450 * for non-fcp rings. The caller does not hold any lock. The function will
2451 * remove each response iocb in the response ring and calls the handle
2452 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2453 **/
2454static void
2455lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2456 struct lpfc_sli_ring *pring, uint32_t mask)
2457{
James Smart34b02dc2008-08-24 21:49:55 -04002458 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002459 IOCB_t *entry;
2460 IOCB_t *irsp = NULL;
2461 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002462 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002463 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002464 uint32_t status;
dea31012005-04-17 16:05:31 -05002465
James Smart34b02dc2008-08-24 21:49:55 -04002466 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002467 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002468 pring->stats.iocb_event++;
2469
dea31012005-04-17 16:05:31 -05002470 /*
2471 * The next available response entry should never exceed the maximum
2472 * entries. If it does, treat it as an adapter hardware error.
2473 */
2474 portRspMax = pring->numRiocb;
2475 portRspPut = le32_to_cpu(pgp->rspPutInx);
2476 if (portRspPut >= portRspMax) {
2477 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002478 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002479 * rsp ring <portRspMax>
2480 */
James Smarted957682007-06-17 19:56:37 -05002481 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002482 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002483 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002484 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002485
James Smart2e0fef82007-06-17 19:56:36 -05002486 phba->link_state = LPFC_HBA_ERROR;
2487 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002488
2489 phba->work_hs = HS_FFER3;
2490 lpfc_handle_eratt(phba);
2491
James Smart3772a992009-05-22 14:50:54 -04002492 return;
dea31012005-04-17 16:05:31 -05002493 }
2494
2495 rmb();
dea31012005-04-17 16:05:31 -05002496 while (pring->rspidx != portRspPut) {
2497 /*
2498 * Build a completion list and call the appropriate handler.
2499 * The process is to get the next available response iocb, get
2500 * a free iocb from the list, copy the response data into the
2501 * free iocb, insert to the continuation list, and update the
2502 * next response index to slim. This process makes response
2503 * iocb's in the ring available to DMA as fast as possible but
2504 * pays a penalty for a copy operation. Since the iocb is
2505 * only 32 bytes, this penalty is considered small relative to
2506 * the PCI reads for register values and a slim write. When
2507 * the ulpLe field is set, the entire Command has been
2508 * received.
2509 */
James Smarted957682007-06-17 19:56:37 -05002510 entry = lpfc_resp_iocb(phba, pring);
2511
James Smart858c9f62007-06-17 19:56:39 -05002512 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002513 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002514 if (rspiocbp == NULL) {
2515 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002516 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002517 break;
2518 }
2519
James Smarted957682007-06-17 19:56:37 -05002520 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2521 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002522 irsp = &rspiocbp->iocb;
2523
2524 if (++pring->rspidx >= portRspMax)
2525 pring->rspidx = 0;
2526
James Smarta58cbd52007-08-02 11:09:43 -04002527 if (pring->ringno == LPFC_ELS_RING) {
2528 lpfc_debugfs_slow_ring_trc(phba,
2529 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2530 *(((uint32_t *) irsp) + 4),
2531 *(((uint32_t *) irsp) + 6),
2532 *(((uint32_t *) irsp) + 7));
2533 }
2534
James Smarted957682007-06-17 19:56:37 -05002535 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002536
James Smart3772a992009-05-22 14:50:54 -04002537 spin_unlock_irqrestore(&phba->hbalock, iflag);
2538 /* Handle the response IOCB */
2539 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2540 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002541
2542 /*
2543 * If the port response put pointer has not been updated, sync
2544 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2545 * response put pointer.
2546 */
2547 if (pring->rspidx == portRspPut) {
2548 portRspPut = le32_to_cpu(pgp->rspPutInx);
2549 }
2550 } /* while (pring->rspidx != portRspPut) */
2551
James Smart92d7f7b2007-06-17 19:56:38 -05002552 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002553 /* At least one response entry has been freed */
2554 pring->stats.iocb_rsp_full++;
2555 /* SET RxRE_RSP in Chip Att register */
2556 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2557 writel(status, phba->CAregaddr);
2558 readl(phba->CAregaddr); /* flush */
2559 }
2560 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2561 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2562 pring->stats.iocb_cmd_empty++;
2563
2564 /* Force update of the local copy of cmdGetInx */
2565 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2566 lpfc_sli_resume_iocb(phba, pring);
2567
2568 if ((pring->lpfc_sli_cmd_available))
2569 (pring->lpfc_sli_cmd_available) (phba, pring);
2570
2571 }
2572
James Smart2e0fef82007-06-17 19:56:36 -05002573 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002574 return;
dea31012005-04-17 16:05:31 -05002575}
2576
James Smarte59058c2008-08-24 21:49:00 -04002577/**
James Smart3621a712009-04-06 18:47:14 -04002578 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04002579 * @phba: Pointer to HBA context object.
2580 * @pring: Pointer to driver SLI ring object.
2581 *
2582 * This function aborts all iocbs in the given ring and frees all the iocb
2583 * objects in txq. This function issues an abort iocb for all the iocb commands
2584 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2585 * the return of this function. The caller is not required to hold any locks.
2586 **/
James Smart2e0fef82007-06-17 19:56:36 -05002587void
dea31012005-04-17 16:05:31 -05002588lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2589{
James Smart2534ba72007-04-25 09:52:20 -04002590 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002591 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05002592
James Smart92d7f7b2007-06-17 19:56:38 -05002593 if (pring->ringno == LPFC_ELS_RING) {
2594 lpfc_fabric_abort_hba(phba);
2595 }
2596
dea31012005-04-17 16:05:31 -05002597 /* Error everything on txq and txcmplq
2598 * First do the txq.
2599 */
James Smart2e0fef82007-06-17 19:56:36 -05002600 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002601 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002602 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002603
2604 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002605 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2606 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2607
James Smart2e0fef82007-06-17 19:56:36 -05002608 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002609
James Smarta257bf92009-04-06 18:48:10 -04002610 /* Cancel all the IOCBs from the completions list */
2611 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2612 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05002613}
2614
James Smarte59058c2008-08-24 21:49:00 -04002615/**
James Smart3621a712009-04-06 18:47:14 -04002616 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04002617 * @phba: Pointer to HBA context object.
2618 *
2619 * This function flushes all iocbs in the fcp ring and frees all the iocb
2620 * objects in txq and txcmplq. This function will not issue abort iocbs
2621 * for all the iocb commands in txcmplq, they will just be returned with
2622 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2623 * slot has been permanently disabled.
2624 **/
2625void
2626lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2627{
2628 LIST_HEAD(txq);
2629 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04002630 struct lpfc_sli *psli = &phba->sli;
2631 struct lpfc_sli_ring *pring;
2632
2633 /* Currently, only one fcp ring */
2634 pring = &psli->ring[psli->fcp_ring];
2635
2636 spin_lock_irq(&phba->hbalock);
2637 /* Retrieve everything on txq */
2638 list_splice_init(&pring->txq, &txq);
2639 pring->txq_cnt = 0;
2640
2641 /* Retrieve everything on the txcmplq */
2642 list_splice_init(&pring->txcmplq, &txcmplq);
2643 pring->txcmplq_cnt = 0;
2644 spin_unlock_irq(&phba->hbalock);
2645
2646 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04002647 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
2648 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04002649
2650 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04002651 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
2652 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04002653}
2654
2655/**
James Smart3772a992009-05-22 14:50:54 -04002656 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04002657 * @phba: Pointer to HBA context object.
2658 * @mask: Bit mask to be checked.
2659 *
2660 * This function reads the host status register and compares
2661 * with the provided bit mask to check if HBA completed
2662 * the restart. This function will wait in a loop for the
2663 * HBA to complete restart. If the HBA does not restart within
2664 * 15 iterations, the function will reset the HBA again. The
2665 * function returns 1 when HBA fail to restart otherwise returns
2666 * zero.
2667 **/
James Smart3772a992009-05-22 14:50:54 -04002668static int
2669lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05002670{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002671 uint32_t status;
2672 int i = 0;
2673 int retval = 0;
dea31012005-04-17 16:05:31 -05002674
Jamie Wellnitz41415862006-02-28 19:25:27 -05002675 /* Read the HBA Host Status Register */
2676 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002677
Jamie Wellnitz41415862006-02-28 19:25:27 -05002678 /*
2679 * Check status register every 100ms for 5 retries, then every
2680 * 500ms for 5, then every 2.5 sec for 5, then reset board and
2681 * every 2.5 sec for 4.
2682 * Break our of the loop if errors occurred during init.
2683 */
2684 while (((status & mask) != mask) &&
2685 !(status & HS_FFERM) &&
2686 i++ < 20) {
dea31012005-04-17 16:05:31 -05002687
Jamie Wellnitz41415862006-02-28 19:25:27 -05002688 if (i <= 5)
2689 msleep(10);
2690 else if (i <= 10)
2691 msleep(500);
2692 else
2693 msleep(2500);
dea31012005-04-17 16:05:31 -05002694
Jamie Wellnitz41415862006-02-28 19:25:27 -05002695 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05002696 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05002697 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002698 lpfc_sli_brdrestart(phba);
2699 }
2700 /* Read the HBA Host Status Register */
2701 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05002702 }
dea31012005-04-17 16:05:31 -05002703
Jamie Wellnitz41415862006-02-28 19:25:27 -05002704 /* Check to see if any errors occurred during init */
2705 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05002706 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002707 retval = 1;
2708 }
dea31012005-04-17 16:05:31 -05002709
Jamie Wellnitz41415862006-02-28 19:25:27 -05002710 return retval;
dea31012005-04-17 16:05:31 -05002711}
2712
James Smartda0436e2009-05-22 14:51:39 -04002713/**
2714 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
2715 * @phba: Pointer to HBA context object.
2716 * @mask: Bit mask to be checked.
2717 *
2718 * This function checks the host status register to check if HBA is
2719 * ready. This function will wait in a loop for the HBA to be ready
2720 * If the HBA is not ready , the function will will reset the HBA PCI
2721 * function again. The function returns 1 when HBA fail to be ready
2722 * otherwise returns zero.
2723 **/
2724static int
2725lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
2726{
2727 uint32_t status;
2728 int retval = 0;
2729
2730 /* Read the HBA Host Status Register */
2731 status = lpfc_sli4_post_status_check(phba);
2732
2733 if (status) {
2734 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2735 lpfc_sli_brdrestart(phba);
2736 status = lpfc_sli4_post_status_check(phba);
2737 }
2738
2739 /* Check to see if any errors occurred during init */
2740 if (status) {
2741 phba->link_state = LPFC_HBA_ERROR;
2742 retval = 1;
2743 } else
2744 phba->sli4_hba.intr_enable = 0;
2745
2746 return retval;
2747}
2748
2749/**
2750 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
2751 * @phba: Pointer to HBA context object.
2752 * @mask: Bit mask to be checked.
2753 *
2754 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
2755 * from the API jump table function pointer from the lpfc_hba struct.
2756 **/
2757int
2758lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
2759{
2760 return phba->lpfc_sli_brdready(phba, mask);
2761}
2762
James Smart92908312006-03-07 15:04:13 -05002763#define BARRIER_TEST_PATTERN (0xdeadbeef)
2764
James Smarte59058c2008-08-24 21:49:00 -04002765/**
James Smart3621a712009-04-06 18:47:14 -04002766 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04002767 * @phba: Pointer to HBA context object.
2768 *
2769 * This function is called before resetting an HBA. This
2770 * function requests HBA to quiesce DMAs before a reset.
2771 **/
James Smart2e0fef82007-06-17 19:56:36 -05002772void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05002773{
James Smart65a29c12006-07-06 15:50:50 -04002774 uint32_t __iomem *resp_buf;
2775 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05002776 volatile uint32_t mbox;
2777 uint32_t hc_copy;
2778 int i;
2779 uint8_t hdrtype;
2780
2781 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2782 if (hdrtype != 0x80 ||
2783 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2784 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2785 return;
2786
2787 /*
2788 * Tell the other part of the chip to suspend temporarily all
2789 * its DMA activity.
2790 */
James Smart65a29c12006-07-06 15:50:50 -04002791 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002792
2793 /* Disable the error attention */
2794 hc_copy = readl(phba->HCregaddr);
2795 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2796 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002797 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002798
2799 if (readl(phba->HAregaddr) & HA_ERATT) {
2800 /* Clear Chip error bit */
2801 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002802 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002803 }
2804
2805 mbox = 0;
2806 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2807 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2808
2809 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04002810 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05002811 writel(mbox, mbox_buf);
2812
2813 for (i = 0;
2814 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2815 mdelay(1);
2816
2817 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2818 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05002819 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05002820 goto restore_hc;
2821 else
2822 goto clear_errat;
2823 }
2824
2825 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2826 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
2827 mdelay(1);
2828
2829clear_errat:
2830
2831 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2832 mdelay(1);
2833
2834 if (readl(phba->HAregaddr) & HA_ERATT) {
2835 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002836 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002837 }
2838
2839restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05002840 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05002841 writel(hc_copy, phba->HCregaddr);
2842 readl(phba->HCregaddr); /* flush */
2843}
2844
James Smarte59058c2008-08-24 21:49:00 -04002845/**
James Smart3621a712009-04-06 18:47:14 -04002846 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04002847 * @phba: Pointer to HBA context object.
2848 *
2849 * This function issues a kill_board mailbox command and waits for
2850 * the error attention interrupt. This function is called for stopping
2851 * the firmware processing. The caller is not required to hold any
2852 * locks. This function calls lpfc_hba_down_post function to free
2853 * any pending commands after the kill. The function will return 1 when it
2854 * fails to kill the board else will return 0.
2855 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002856int
James Smart2e0fef82007-06-17 19:56:36 -05002857lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002858{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002859 struct lpfc_sli *psli;
2860 LPFC_MBOXQ_t *pmb;
2861 uint32_t status;
2862 uint32_t ha_copy;
2863 int retval;
2864 int i = 0;
2865
2866 psli = &phba->sli;
2867
2868 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05002869 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002870 "0329 Kill HBA Data: x%x x%x\n",
2871 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002872
James Smart98c9ea52007-10-27 13:37:33 -04002873 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2874 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002875 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002876
2877 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05002878 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002879 status = readl(phba->HCregaddr);
2880 status &= ~HC_ERINT_ENA;
2881 writel(status, phba->HCregaddr);
2882 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002883 phba->link_flag |= LS_IGNORE_ERATT;
2884 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002885
2886 lpfc_kill_board(phba, pmb);
2887 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2888 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2889
2890 if (retval != MBX_SUCCESS) {
2891 if (retval != MBX_BUSY)
2892 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05002893 spin_lock_irq(&phba->hbalock);
2894 phba->link_flag &= ~LS_IGNORE_ERATT;
2895 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002896 return 1;
2897 }
2898
James Smart92908312006-03-07 15:04:13 -05002899 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2900
Jamie Wellnitz41415862006-02-28 19:25:27 -05002901 mempool_free(pmb, phba->mbox_mem_pool);
2902
2903 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2904 * attention every 100ms for 3 seconds. If we don't get ERATT after
2905 * 3 seconds we still set HBA_ERROR state because the status of the
2906 * board is now undefined.
2907 */
2908 ha_copy = readl(phba->HAregaddr);
2909
2910 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2911 mdelay(100);
2912 ha_copy = readl(phba->HAregaddr);
2913 }
2914
2915 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05002916 if (ha_copy & HA_ERATT) {
2917 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002918 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05002919 }
James Smart2e0fef82007-06-17 19:56:36 -05002920 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002921 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05002922 phba->link_flag &= ~LS_IGNORE_ERATT;
2923 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05002924
2925 psli->mbox_active = NULL;
2926 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002927 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002928
James Smart2e0fef82007-06-17 19:56:36 -05002929 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002930}
2931
James Smarte59058c2008-08-24 21:49:00 -04002932/**
James Smart3772a992009-05-22 14:50:54 -04002933 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04002934 * @phba: Pointer to HBA context object.
2935 *
2936 * This function resets the HBA by writing HC_INITFF to the control
2937 * register. After the HBA resets, this function resets all the iocb ring
2938 * indices. This function disables PCI layer parity checking during
2939 * the reset.
2940 * This function returns 0 always.
2941 * The caller is not required to hold any locks.
2942 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05002943int
James Smart2e0fef82007-06-17 19:56:36 -05002944lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05002945{
2946 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002947 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002948 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05002949 int i;
dea31012005-04-17 16:05:31 -05002950
Jamie Wellnitz41415862006-02-28 19:25:27 -05002951 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002952
Jamie Wellnitz41415862006-02-28 19:25:27 -05002953 /* Reset HBA */
2954 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002955 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05002956 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05002957
2958 /* perform board reset */
2959 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002960 phba->pport->fc_myDID = 0;
2961 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05002962
Jamie Wellnitz41415862006-02-28 19:25:27 -05002963 /* Turn off parity checking and serr during the physical reset */
2964 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2965 pci_write_config_word(phba->pcidev, PCI_COMMAND,
2966 (cfg_value &
2967 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2968
James Smart3772a992009-05-22 14:50:54 -04002969 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
2970
Jamie Wellnitz41415862006-02-28 19:25:27 -05002971 /* Now toggle INITFF bit in the Host Control Register */
2972 writel(HC_INITFF, phba->HCregaddr);
2973 mdelay(1);
2974 readl(phba->HCregaddr); /* flush */
2975 writel(0, phba->HCregaddr);
2976 readl(phba->HCregaddr); /* flush */
2977
2978 /* Restore PCI cmd register */
2979 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05002980
2981 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002982 for (i = 0; i < psli->num_rings; i++) {
2983 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05002984 pring->flag = 0;
2985 pring->rspidx = 0;
2986 pring->next_cmdidx = 0;
2987 pring->local_getidx = 0;
2988 pring->cmdidx = 0;
2989 pring->missbufcnt = 0;
2990 }
dea31012005-04-17 16:05:31 -05002991
James Smart2e0fef82007-06-17 19:56:36 -05002992 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05002993 return 0;
2994}
2995
James Smarte59058c2008-08-24 21:49:00 -04002996/**
James Smartda0436e2009-05-22 14:51:39 -04002997 * lpfc_sli4_brdreset - Reset a sli-4 HBA
2998 * @phba: Pointer to HBA context object.
2999 *
3000 * This function resets a SLI4 HBA. This function disables PCI layer parity
3001 * checking during resets the device. The caller is not required to hold
3002 * any locks.
3003 *
3004 * This function returns 0 always.
3005 **/
3006int
3007lpfc_sli4_brdreset(struct lpfc_hba *phba)
3008{
3009 struct lpfc_sli *psli = &phba->sli;
3010 uint16_t cfg_value;
3011 uint8_t qindx;
3012
3013 /* Reset HBA */
3014 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3015 "0295 Reset HBA Data: x%x x%x\n",
3016 phba->pport->port_state, psli->sli_flag);
3017
3018 /* perform board reset */
3019 phba->fc_eventTag = 0;
3020 phba->pport->fc_myDID = 0;
3021 phba->pport->fc_prevDID = 0;
3022
3023 /* Turn off parity checking and serr during the physical reset */
3024 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3025 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3026 (cfg_value &
3027 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3028
3029 spin_lock_irq(&phba->hbalock);
3030 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3031 phba->fcf.fcf_flag = 0;
3032 /* Clean up the child queue list for the CQs */
3033 list_del_init(&phba->sli4_hba.mbx_wq->list);
3034 list_del_init(&phba->sli4_hba.els_wq->list);
3035 list_del_init(&phba->sli4_hba.hdr_rq->list);
3036 list_del_init(&phba->sli4_hba.dat_rq->list);
3037 list_del_init(&phba->sli4_hba.mbx_cq->list);
3038 list_del_init(&phba->sli4_hba.els_cq->list);
3039 list_del_init(&phba->sli4_hba.rxq_cq->list);
3040 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3041 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3042 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3043 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3044 spin_unlock_irq(&phba->hbalock);
3045
3046 /* Now physically reset the device */
3047 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3048 "0389 Performing PCI function reset!\n");
3049 /* Perform FCoE PCI function reset */
3050 lpfc_pci_function_reset(phba);
3051
3052 return 0;
3053}
3054
3055/**
3056 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003057 * @phba: Pointer to HBA context object.
3058 *
3059 * This function is called in the SLI initialization code path to
3060 * restart the HBA. The caller is not required to hold any lock.
3061 * This function writes MBX_RESTART mailbox command to the SLIM and
3062 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3063 * function to free any pending commands. The function enables
3064 * POST only during the first initialization. The function returns zero.
3065 * The function does not guarantee completion of MBX_RESTART mailbox
3066 * command before the return of this function.
3067 **/
James Smartda0436e2009-05-22 14:51:39 -04003068static int
3069lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003070{
3071 MAILBOX_t *mb;
3072 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003073 volatile uint32_t word0;
3074 void __iomem *to_slim;
3075
James Smart2e0fef82007-06-17 19:56:36 -05003076 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003077
3078 psli = &phba->sli;
3079
3080 /* Restart HBA */
3081 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003082 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003083 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003084
3085 word0 = 0;
3086 mb = (MAILBOX_t *) &word0;
3087 mb->mbxCommand = MBX_RESTART;
3088 mb->mbxHc = 1;
3089
James Smart92908312006-03-07 15:04:13 -05003090 lpfc_reset_barrier(phba);
3091
Jamie Wellnitz41415862006-02-28 19:25:27 -05003092 to_slim = phba->MBslimaddr;
3093 writel(*(uint32_t *) mb, to_slim);
3094 readl(to_slim); /* flush */
3095
3096 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003097 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003098 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003099 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003100 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003101 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003102 writel(*(uint32_t *) mb, to_slim);
3103 readl(to_slim); /* flush */
3104
3105 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003106 phba->pport->stopped = 0;
3107 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003108 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003109 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003110
James Smart64ba8812006-08-02 15:24:34 -04003111 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3112 psli->stats_start = get_seconds();
3113
James Smarteaf15d52008-12-04 22:39:29 -05003114 /* Give the INITFF and Post time to settle. */
3115 mdelay(100);
dea31012005-04-17 16:05:31 -05003116
Jamie Wellnitz41415862006-02-28 19:25:27 -05003117 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003118
3119 return 0;
3120}
3121
James Smarte59058c2008-08-24 21:49:00 -04003122/**
James Smartda0436e2009-05-22 14:51:39 -04003123 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3124 * @phba: Pointer to HBA context object.
3125 *
3126 * This function is called in the SLI initialization code path to restart
3127 * a SLI4 HBA. The caller is not required to hold any lock.
3128 * At the end of the function, it calls lpfc_hba_down_post function to
3129 * free any pending commands.
3130 **/
3131static int
3132lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3133{
3134 struct lpfc_sli *psli = &phba->sli;
3135
3136
3137 /* Restart HBA */
3138 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3139 "0296 Restart HBA Data: x%x x%x\n",
3140 phba->pport->port_state, psli->sli_flag);
3141
3142 lpfc_sli4_brdreset(phba);
3143
3144 spin_lock_irq(&phba->hbalock);
3145 phba->pport->stopped = 0;
3146 phba->link_state = LPFC_INIT_START;
3147 phba->hba_flag = 0;
3148 spin_unlock_irq(&phba->hbalock);
3149
3150 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3151 psli->stats_start = get_seconds();
3152
3153 lpfc_hba_down_post(phba);
3154
3155 return 0;
3156}
3157
3158/**
3159 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3160 * @phba: Pointer to HBA context object.
3161 *
3162 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3163 * API jump table function pointer from the lpfc_hba struct.
3164**/
3165int
3166lpfc_sli_brdrestart(struct lpfc_hba *phba)
3167{
3168 return phba->lpfc_sli_brdrestart(phba);
3169}
3170
3171/**
James Smart3621a712009-04-06 18:47:14 -04003172 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003173 * @phba: Pointer to HBA context object.
3174 *
3175 * This function is called after a HBA restart to wait for successful
3176 * restart of the HBA. Successful restart of the HBA is indicated by
3177 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3178 * iteration, the function will restart the HBA again. The function returns
3179 * zero if HBA successfully restarted else returns negative error code.
3180 **/
dea31012005-04-17 16:05:31 -05003181static int
3182lpfc_sli_chipset_init(struct lpfc_hba *phba)
3183{
3184 uint32_t status, i = 0;
3185
3186 /* Read the HBA Host Status Register */
3187 status = readl(phba->HSregaddr);
3188
3189 /* Check status register to see what current state is */
3190 i = 0;
3191 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3192
3193 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3194 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3195 * 4.
3196 */
3197 if (i++ >= 20) {
3198 /* Adapter failed to init, timeout, status reg
3199 <status> */
James Smarted957682007-06-17 19:56:37 -05003200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003201 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003202 "timeout, status reg x%x, "
3203 "FW Data: A8 x%x AC x%x\n", status,
3204 readl(phba->MBslimaddr + 0xa8),
3205 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003206 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003207 return -ETIMEDOUT;
3208 }
3209
3210 /* Check to see if any errors occurred during init */
3211 if (status & HS_FFERM) {
3212 /* ERROR: During chipset initialization */
3213 /* Adapter failed to init, chipset, status reg
3214 <status> */
James Smarted957682007-06-17 19:56:37 -05003215 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003216 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003217 "chipset, status reg x%x, "
3218 "FW Data: A8 x%x AC x%x\n", status,
3219 readl(phba->MBslimaddr + 0xa8),
3220 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003221 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003222 return -EIO;
3223 }
3224
3225 if (i <= 5) {
3226 msleep(10);
3227 } else if (i <= 10) {
3228 msleep(500);
3229 } else {
3230 msleep(2500);
3231 }
3232
3233 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003234 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003235 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003236 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003237 }
3238 /* Read the HBA Host Status Register */
3239 status = readl(phba->HSregaddr);
3240 }
3241
3242 /* Check to see if any errors occurred during init */
3243 if (status & HS_FFERM) {
3244 /* ERROR: During chipset initialization */
3245 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003247 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003248 "status reg x%x, "
3249 "FW Data: A8 x%x AC x%x\n", status,
3250 readl(phba->MBslimaddr + 0xa8),
3251 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003252 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003253 return -EIO;
3254 }
3255
3256 /* Clear all interrupt enable conditions */
3257 writel(0, phba->HCregaddr);
3258 readl(phba->HCregaddr); /* flush */
3259
3260 /* setup host attn register */
3261 writel(0xffffffff, phba->HAregaddr);
3262 readl(phba->HAregaddr); /* flush */
3263 return 0;
3264}
3265
James Smarte59058c2008-08-24 21:49:00 -04003266/**
James Smart3621a712009-04-06 18:47:14 -04003267 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003268 *
3269 * This function calculates and returns the number of HBQs required to be
3270 * configured.
3271 **/
James Smart78b2d852007-08-02 11:10:21 -04003272int
James Smarted957682007-06-17 19:56:37 -05003273lpfc_sli_hbq_count(void)
3274{
James Smart92d7f7b2007-06-17 19:56:38 -05003275 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003276}
3277
James Smarte59058c2008-08-24 21:49:00 -04003278/**
James Smart3621a712009-04-06 18:47:14 -04003279 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003280 *
3281 * This function adds the number of hbq entries in every HBQ to get
3282 * the total number of hbq entries required for the HBA and returns
3283 * the total count.
3284 **/
James Smarted957682007-06-17 19:56:37 -05003285static int
3286lpfc_sli_hbq_entry_count(void)
3287{
3288 int hbq_count = lpfc_sli_hbq_count();
3289 int count = 0;
3290 int i;
3291
3292 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003293 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003294 return count;
3295}
3296
James Smarte59058c2008-08-24 21:49:00 -04003297/**
James Smart3621a712009-04-06 18:47:14 -04003298 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003299 *
3300 * This function calculates amount of memory required for all hbq entries
3301 * to be configured and returns the total memory required.
3302 **/
dea31012005-04-17 16:05:31 -05003303int
James Smarted957682007-06-17 19:56:37 -05003304lpfc_sli_hbq_size(void)
3305{
3306 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3307}
3308
James Smarte59058c2008-08-24 21:49:00 -04003309/**
James Smart3621a712009-04-06 18:47:14 -04003310 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003311 * @phba: Pointer to HBA context object.
3312 *
3313 * This function is called during the SLI initialization to configure
3314 * all the HBQs and post buffers to the HBQ. The caller is not
3315 * required to hold any locks. This function will return zero if successful
3316 * else it will return negative error code.
3317 **/
James Smarted957682007-06-17 19:56:37 -05003318static int
3319lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3320{
3321 int hbq_count = lpfc_sli_hbq_count();
3322 LPFC_MBOXQ_t *pmb;
3323 MAILBOX_t *pmbox;
3324 uint32_t hbqno;
3325 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003326
James Smart92d7f7b2007-06-17 19:56:38 -05003327 /* Get a Mailbox buffer to setup mailbox
3328 * commands for HBA initialization
3329 */
James Smarted957682007-06-17 19:56:37 -05003330 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3331
3332 if (!pmb)
3333 return -ENOMEM;
3334
3335 pmbox = &pmb->mb;
3336
3337 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3338 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003339 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003340
3341 hbq_entry_index = 0;
3342 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3343 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3344 phba->hbqs[hbqno].hbqPutIdx = 0;
3345 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3346 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003347 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003348 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3349 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003350 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3351
3352 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3353 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3354 mbxStatus <status>, ring <num> */
3355
3356 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003357 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003358 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003359 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003360 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003361 pmbox->mbxStatus, hbqno);
3362
3363 phba->link_state = LPFC_HBA_ERROR;
3364 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003365 return ENXIO;
3366 }
3367 }
3368 phba->hbq_count = hbq_count;
3369
James Smarted957682007-06-17 19:56:37 -05003370 mempool_free(pmb, phba->mbox_mem_pool);
3371
James Smart92d7f7b2007-06-17 19:56:38 -05003372 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003373 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3374 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003375 return 0;
3376}
3377
James Smarte59058c2008-08-24 21:49:00 -04003378/**
James Smart3621a712009-04-06 18:47:14 -04003379 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003380 * @phba: Pointer to HBA context object.
3381 * @sli_mode: sli mode - 2/3
3382 *
3383 * This function is called by the sli intialization code path
3384 * to issue config_port mailbox command. This function restarts the
3385 * HBA firmware and issues a config_port mailbox command to configure
3386 * the SLI interface in the sli mode specified by sli_mode
3387 * variable. The caller is not required to hold any locks.
3388 * The function returns 0 if successful, else returns negative error
3389 * code.
3390 **/
James Smart93996272008-08-24 21:50:30 -04003391int
3392lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003393{
3394 LPFC_MBOXQ_t *pmb;
3395 uint32_t resetcount = 0, rc = 0, done = 0;
3396
3397 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3398 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003399 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003400 return -ENOMEM;
3401 }
3402
James Smarted957682007-06-17 19:56:37 -05003403 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003404 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003405 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003406 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003407 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003408 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003409 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003410 rc = lpfc_sli_chipset_init(phba);
3411 if (rc)
3412 break;
3413
James Smart2e0fef82007-06-17 19:56:36 -05003414 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003415 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003416 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003417 resetcount++;
3418
James Smarted957682007-06-17 19:56:37 -05003419 /* Call pre CONFIG_PORT mailbox command initialization. A
3420 * value of 0 means the call was successful. Any other
3421 * nonzero value is a failure, but if ERESTART is returned,
3422 * the driver may reset the HBA and try again.
3423 */
dea31012005-04-17 16:05:31 -05003424 rc = lpfc_config_port_prep(phba);
3425 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003426 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003427 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003428 } else if (rc)
dea31012005-04-17 16:05:31 -05003429 break;
James Smart2e0fef82007-06-17 19:56:36 -05003430 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003431 lpfc_config_port(phba, pmb);
3432 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003433 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3434 LPFC_SLI3_HBQ_ENABLED |
3435 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003436 LPFC_SLI3_INB_ENABLED |
3437 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003438 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003440 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003441 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003442 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003443 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003444 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003445 spin_unlock_irq(&phba->hbalock);
3446 rc = -ENXIO;
James Smart34b02dc2008-08-24 21:49:55 -04003447 } else
James Smarted957682007-06-17 19:56:37 -05003448 done = 1;
dea31012005-04-17 16:05:31 -05003449 }
James Smarted957682007-06-17 19:56:37 -05003450 if (!done) {
3451 rc = -EINVAL;
3452 goto do_prep_failed;
3453 }
James Smart34b02dc2008-08-24 21:49:55 -04003454 if (pmb->mb.un.varCfgPort.sli_mode == 3) {
3455 if (!pmb->mb.un.varCfgPort.cMA) {
3456 rc = -ENXIO;
3457 goto do_prep_failed;
3458 }
3459 if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
3460 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3461 phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
3462 } else
3463 phba->max_vpi = 0;
3464 if (pmb->mb.un.varCfgPort.gerbm)
3465 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3466 if (pmb->mb.un.varCfgPort.gcrp)
3467 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3468 if (pmb->mb.un.varCfgPort.ginb) {
3469 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003470 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003471 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3472 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3473 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3474 phba->inb_last_counter =
3475 phba->mbox->us.s3_inb_pgp.counter;
3476 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003477 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003478 phba->port_gp = phba->mbox->us.s3_pgp.port;
3479 phba->inb_ha_copy = NULL;
3480 phba->inb_counter = NULL;
3481 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003482
3483 if (phba->cfg_enable_bg) {
3484 if (pmb->mb.un.varCfgPort.gbg)
3485 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3486 else
3487 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3488 "0443 Adapter did not grant "
3489 "BlockGuard\n");
3490 }
James Smart34b02dc2008-08-24 21:49:55 -04003491 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003492 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003493 phba->port_gp = phba->mbox->us.s2.port;
3494 phba->inb_ha_copy = NULL;
3495 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003496 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003497 }
James Smart92d7f7b2007-06-17 19:56:38 -05003498do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003499 mempool_free(pmb, phba->mbox_mem_pool);
3500 return rc;
3501}
3502
James Smarte59058c2008-08-24 21:49:00 -04003503
3504/**
James Smart3621a712009-04-06 18:47:14 -04003505 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003506 * @phba: Pointer to HBA context object.
3507 *
3508 * This function is the main SLI intialization function. This function
3509 * is called by the HBA intialization code, HBA reset code and HBA
3510 * error attention handler code. Caller is not required to hold any
3511 * locks. This function issues config_port mailbox command to configure
3512 * the SLI, setup iocb rings and HBQ rings. In the end the function
3513 * calls the config_port_post function to issue init_link mailbox
3514 * command and to start the discovery. The function will return zero
3515 * if successful, else it will return negative error code.
3516 **/
James Smarted957682007-06-17 19:56:37 -05003517int
3518lpfc_sli_hba_setup(struct lpfc_hba *phba)
3519{
3520 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003521 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003522
3523 switch (lpfc_sli_mode) {
3524 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003525 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003527 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003528 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003529 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003530 break;
3531 }
James Smarted957682007-06-17 19:56:37 -05003532 mode = 2;
3533 break;
3534 case 0:
3535 case 3:
3536 break;
3537 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003539 "1819 Unrecognized lpfc_sli_mode "
3540 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003541
3542 break;
3543 }
3544
James Smart93996272008-08-24 21:50:30 -04003545 rc = lpfc_sli_config_port(phba, mode);
3546
James Smarted957682007-06-17 19:56:37 -05003547 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003549 "1820 Unable to select SLI-3. "
3550 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003551 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04003552 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05003553 if (rc)
dea31012005-04-17 16:05:31 -05003554 goto lpfc_sli_hba_setup_error;
3555
James Smarted957682007-06-17 19:56:37 -05003556 if (phba->sli_rev == 3) {
3557 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3558 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05003559 } else {
3560 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3561 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003562 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003563 }
3564
3565 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003566 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3567 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05003568 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05003569
3570 if (rc)
3571 goto lpfc_sli_hba_setup_error;
3572
James Smart93996272008-08-24 21:50:30 -04003573 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05003574 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3575 rc = lpfc_sli_hbq_setup(phba);
3576 if (rc)
3577 goto lpfc_sli_hba_setup_error;
3578 }
3579
dea31012005-04-17 16:05:31 -05003580 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3581
3582 rc = lpfc_config_port_post(phba);
3583 if (rc)
3584 goto lpfc_sli_hba_setup_error;
3585
James Smarted957682007-06-17 19:56:37 -05003586 return rc;
3587
James Smart92d7f7b2007-06-17 19:56:38 -05003588lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05003589 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05003590 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003591 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05003592 return rc;
3593}
3594
James Smartda0436e2009-05-22 14:51:39 -04003595/**
3596 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
3597 * @phba: Pointer to HBA context object.
3598 * @mboxq: mailbox pointer.
3599 * This function issue a dump mailbox command to read config region
3600 * 23 and parse the records in the region and populate driver
3601 * data structure.
3602 **/
3603static int
3604lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
3605 LPFC_MBOXQ_t *mboxq)
3606{
3607 struct lpfc_dmabuf *mp;
3608 struct lpfc_mqe *mqe;
3609 uint32_t data_length;
3610 int rc;
3611
3612 /* Program the default value of vlan_id and fc_map */
3613 phba->valid_vlan = 0;
3614 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3615 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3616 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3617
3618 mqe = &mboxq->u.mqe;
3619 if (lpfc_dump_fcoe_param(phba, mboxq))
3620 return -ENOMEM;
3621
3622 mp = (struct lpfc_dmabuf *) mboxq->context1;
3623 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3624
3625 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
3626 "(%d):2571 Mailbox cmd x%x Status x%x "
3627 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3628 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3629 "CQ: x%x x%x x%x x%x\n",
3630 mboxq->vport ? mboxq->vport->vpi : 0,
3631 bf_get(lpfc_mqe_command, mqe),
3632 bf_get(lpfc_mqe_status, mqe),
3633 mqe->un.mb_words[0], mqe->un.mb_words[1],
3634 mqe->un.mb_words[2], mqe->un.mb_words[3],
3635 mqe->un.mb_words[4], mqe->un.mb_words[5],
3636 mqe->un.mb_words[6], mqe->un.mb_words[7],
3637 mqe->un.mb_words[8], mqe->un.mb_words[9],
3638 mqe->un.mb_words[10], mqe->un.mb_words[11],
3639 mqe->un.mb_words[12], mqe->un.mb_words[13],
3640 mqe->un.mb_words[14], mqe->un.mb_words[15],
3641 mqe->un.mb_words[16], mqe->un.mb_words[50],
3642 mboxq->mcqe.word0,
3643 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
3644 mboxq->mcqe.trailer);
3645
3646 if (rc) {
3647 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3648 kfree(mp);
3649 return -EIO;
3650 }
3651 data_length = mqe->un.mb_words[5];
3652 if (data_length > DMP_FCOEPARAM_RGN_SIZE)
3653 return -EIO;
3654
3655 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
3656 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3657 kfree(mp);
3658 return 0;
3659}
3660
3661/**
3662 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
3663 * @phba: pointer to lpfc hba data structure.
3664 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
3665 * @vpd: pointer to the memory to hold resulting port vpd data.
3666 * @vpd_size: On input, the number of bytes allocated to @vpd.
3667 * On output, the number of data bytes in @vpd.
3668 *
3669 * This routine executes a READ_REV SLI4 mailbox command. In
3670 * addition, this routine gets the port vpd data.
3671 *
3672 * Return codes
3673 * 0 - sucessful
3674 * ENOMEM - could not allocated memory.
3675 **/
3676static int
3677lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
3678 uint8_t *vpd, uint32_t *vpd_size)
3679{
3680 int rc = 0;
3681 uint32_t dma_size;
3682 struct lpfc_dmabuf *dmabuf;
3683 struct lpfc_mqe *mqe;
3684
3685 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3686 if (!dmabuf)
3687 return -ENOMEM;
3688
3689 /*
3690 * Get a DMA buffer for the vpd data resulting from the READ_REV
3691 * mailbox command.
3692 */
3693 dma_size = *vpd_size;
3694 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
3695 dma_size,
3696 &dmabuf->phys,
3697 GFP_KERNEL);
3698 if (!dmabuf->virt) {
3699 kfree(dmabuf);
3700 return -ENOMEM;
3701 }
3702 memset(dmabuf->virt, 0, dma_size);
3703
3704 /*
3705 * The SLI4 implementation of READ_REV conflicts at word1,
3706 * bits 31:16 and SLI4 adds vpd functionality not present
3707 * in SLI3. This code corrects the conflicts.
3708 */
3709 lpfc_read_rev(phba, mboxq);
3710 mqe = &mboxq->u.mqe;
3711 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
3712 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
3713 mqe->un.read_rev.word1 &= 0x0000FFFF;
3714 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
3715 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
3716
3717 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3718 if (rc) {
3719 dma_free_coherent(&phba->pcidev->dev, dma_size,
3720 dmabuf->virt, dmabuf->phys);
3721 return -EIO;
3722 }
3723
3724 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
3725 "(%d):0380 Mailbox cmd x%x Status x%x "
3726 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3727 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3728 "CQ: x%x x%x x%x x%x\n",
3729 mboxq->vport ? mboxq->vport->vpi : 0,
3730 bf_get(lpfc_mqe_command, mqe),
3731 bf_get(lpfc_mqe_status, mqe),
3732 mqe->un.mb_words[0], mqe->un.mb_words[1],
3733 mqe->un.mb_words[2], mqe->un.mb_words[3],
3734 mqe->un.mb_words[4], mqe->un.mb_words[5],
3735 mqe->un.mb_words[6], mqe->un.mb_words[7],
3736 mqe->un.mb_words[8], mqe->un.mb_words[9],
3737 mqe->un.mb_words[10], mqe->un.mb_words[11],
3738 mqe->un.mb_words[12], mqe->un.mb_words[13],
3739 mqe->un.mb_words[14], mqe->un.mb_words[15],
3740 mqe->un.mb_words[16], mqe->un.mb_words[50],
3741 mboxq->mcqe.word0,
3742 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
3743 mboxq->mcqe.trailer);
3744
3745 /*
3746 * The available vpd length cannot be bigger than the
3747 * DMA buffer passed to the port. Catch the less than
3748 * case and update the caller's size.
3749 */
3750 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
3751 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3752
3753 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
3754 dma_free_coherent(&phba->pcidev->dev, dma_size,
3755 dmabuf->virt, dmabuf->phys);
3756 kfree(dmabuf);
3757 return 0;
3758}
3759
3760/**
3761 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
3762 * @phba: pointer to lpfc hba data structure.
3763 *
3764 * This routine is called to explicitly arm the SLI4 device's completion and
3765 * event queues
3766 **/
3767static void
3768lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
3769{
3770 uint8_t fcp_eqidx;
3771
3772 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
3773 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
3774 lpfc_sli4_cq_release(phba->sli4_hba.rxq_cq, LPFC_QUEUE_REARM);
3775 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
3776 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
3777 LPFC_QUEUE_REARM);
3778 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
3779 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
3780 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
3781 LPFC_QUEUE_REARM);
3782}
3783
3784/**
3785 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
3786 * @phba: Pointer to HBA context object.
3787 *
3788 * This function is the main SLI4 device intialization PCI function. This
3789 * function is called by the HBA intialization code, HBA reset code and
3790 * HBA error attention handler code. Caller is not required to hold any
3791 * locks.
3792 **/
3793int
3794lpfc_sli4_hba_setup(struct lpfc_hba *phba)
3795{
3796 int rc;
3797 LPFC_MBOXQ_t *mboxq;
3798 struct lpfc_mqe *mqe;
3799 uint8_t *vpd;
3800 uint32_t vpd_size;
3801 uint32_t ftr_rsp = 0;
3802 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
3803 struct lpfc_vport *vport = phba->pport;
3804 struct lpfc_dmabuf *mp;
3805
3806 /* Perform a PCI function reset to start from clean */
3807 rc = lpfc_pci_function_reset(phba);
3808 if (unlikely(rc))
3809 return -ENODEV;
3810
3811 /* Check the HBA Host Status Register for readyness */
3812 rc = lpfc_sli4_post_status_check(phba);
3813 if (unlikely(rc))
3814 return -ENODEV;
3815 else {
3816 spin_lock_irq(&phba->hbalock);
3817 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
3818 spin_unlock_irq(&phba->hbalock);
3819 }
3820
3821 /*
3822 * Allocate a single mailbox container for initializing the
3823 * port.
3824 */
3825 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3826 if (!mboxq)
3827 return -ENOMEM;
3828
3829 /*
3830 * Continue initialization with default values even if driver failed
3831 * to read FCoE param config regions
3832 */
3833 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
3834 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
3835 "2570 Failed to read FCoE parameters \n");
3836
3837 /* Issue READ_REV to collect vpd and FW information. */
3838 vpd_size = PAGE_SIZE;
3839 vpd = kzalloc(vpd_size, GFP_KERNEL);
3840 if (!vpd) {
3841 rc = -ENOMEM;
3842 goto out_free_mbox;
3843 }
3844
3845 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
3846 if (unlikely(rc))
3847 goto out_free_vpd;
3848
3849 mqe = &mboxq->u.mqe;
3850 if ((bf_get(lpfc_mbx_rd_rev_sli_lvl,
3851 &mqe->un.read_rev) != LPFC_SLI_REV4) ||
3852 (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev) == 0)) {
3853 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3854 "0376 READ_REV Error. SLI Level %d "
3855 "FCoE enabled %d\n",
3856 bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev),
3857 bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev));
3858 rc = -EIO;
3859 goto out_free_vpd;
3860 }
3861 /* Single threaded at this point, no need for lock */
3862 spin_lock_irq(&phba->hbalock);
3863 phba->hba_flag |= HBA_FCOE_SUPPORT;
3864 spin_unlock_irq(&phba->hbalock);
3865 /*
3866 * Evaluate the read rev and vpd data. Populate the driver
3867 * state with the results. If this routine fails, the failure
3868 * is not fatal as the driver will use generic values.
3869 */
3870 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
3871 if (unlikely(!rc)) {
3872 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3873 "0377 Error %d parsing vpd. "
3874 "Using defaults.\n", rc);
3875 rc = 0;
3876 }
3877
3878 /* By now, we should determine the SLI revision, hard code for now */
3879 phba->sli_rev = LPFC_SLI_REV4;
3880
3881 /*
3882 * Discover the port's supported feature set and match it against the
3883 * hosts requests.
3884 */
3885 lpfc_request_features(phba, mboxq);
3886 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3887 if (unlikely(rc)) {
3888 rc = -EIO;
3889 goto out_free_vpd;
3890 }
3891
3892 /*
3893 * The port must support FCP initiator mode as this is the
3894 * only mode running in the host.
3895 */
3896 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
3897 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
3898 "0378 No support for fcpi mode.\n");
3899 ftr_rsp++;
3900 }
3901
3902 /*
3903 * If the port cannot support the host's requested features
3904 * then turn off the global config parameters to disable the
3905 * feature in the driver. This is not a fatal error.
3906 */
3907 if ((phba->cfg_enable_bg) &&
3908 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
3909 ftr_rsp++;
3910
3911 if (phba->max_vpi && phba->cfg_enable_npiv &&
3912 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
3913 ftr_rsp++;
3914
3915 if (ftr_rsp) {
3916 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
3917 "0379 Feature Mismatch Data: x%08x %08x "
3918 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
3919 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
3920 phba->cfg_enable_npiv, phba->max_vpi);
3921 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
3922 phba->cfg_enable_bg = 0;
3923 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
3924 phba->cfg_enable_npiv = 0;
3925 }
3926
3927 /* These SLI3 features are assumed in SLI4 */
3928 spin_lock_irq(&phba->hbalock);
3929 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
3930 spin_unlock_irq(&phba->hbalock);
3931
3932 /* Read the port's service parameters. */
3933 lpfc_read_sparam(phba, mboxq, vport->vpi);
3934 mboxq->vport = vport;
3935 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3936 mp = (struct lpfc_dmabuf *) mboxq->context1;
3937 if (rc == MBX_SUCCESS) {
3938 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
3939 rc = 0;
3940 }
3941
3942 /*
3943 * This memory was allocated by the lpfc_read_sparam routine. Release
3944 * it to the mbuf pool.
3945 */
3946 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3947 kfree(mp);
3948 mboxq->context1 = NULL;
3949 if (unlikely(rc)) {
3950 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3951 "0382 READ_SPARAM command failed "
3952 "status %d, mbxStatus x%x\n",
3953 rc, bf_get(lpfc_mqe_status, mqe));
3954 phba->link_state = LPFC_HBA_ERROR;
3955 rc = -EIO;
3956 goto out_free_vpd;
3957 }
3958
3959 if (phba->cfg_soft_wwnn)
3960 u64_to_wwn(phba->cfg_soft_wwnn,
3961 vport->fc_sparam.nodeName.u.wwn);
3962 if (phba->cfg_soft_wwpn)
3963 u64_to_wwn(phba->cfg_soft_wwpn,
3964 vport->fc_sparam.portName.u.wwn);
3965 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
3966 sizeof(struct lpfc_name));
3967 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
3968 sizeof(struct lpfc_name));
3969
3970 /* Update the fc_host data structures with new wwn. */
3971 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3972 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
3973
3974 /* Register SGL pool to the device using non-embedded mailbox command */
3975 rc = lpfc_sli4_post_sgl_list(phba);
3976 if (unlikely(rc)) {
3977 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3978 "0582 Error %d during sgl post operation", rc);
3979 rc = -ENODEV;
3980 goto out_free_vpd;
3981 }
3982
3983 /* Register SCSI SGL pool to the device */
3984 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
3985 if (unlikely(rc)) {
3986 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
3987 "0383 Error %d during scsi sgl post opeation",
3988 rc);
3989 /* Some Scsi buffers were moved to the abort scsi list */
3990 /* A pci function reset will repost them */
3991 rc = -ENODEV;
3992 goto out_free_vpd;
3993 }
3994
3995 /* Post the rpi header region to the device. */
3996 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
3997 if (unlikely(rc)) {
3998 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3999 "0393 Error %d during rpi post operation\n",
4000 rc);
4001 rc = -ENODEV;
4002 goto out_free_vpd;
4003 }
4004 /* Temporary initialization of lpfc_fip_flag to non-fip */
4005 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);
4006
4007 /* Set up all the queues to the device */
4008 rc = lpfc_sli4_queue_setup(phba);
4009 if (unlikely(rc)) {
4010 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4011 "0381 Error %d during queue setup.\n ", rc);
4012 goto out_stop_timers;
4013 }
4014
4015 /* Arm the CQs and then EQs on device */
4016 lpfc_sli4_arm_cqeq_intr(phba);
4017
4018 /* Indicate device interrupt mode */
4019 phba->sli4_hba.intr_enable = 1;
4020
4021 /* Allow asynchronous mailbox command to go through */
4022 spin_lock_irq(&phba->hbalock);
4023 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4024 spin_unlock_irq(&phba->hbalock);
4025
4026 /* Post receive buffers to the device */
4027 lpfc_sli4_rb_setup(phba);
4028
4029 /* Start the ELS watchdog timer */
4030 /*
4031 * The driver for SLI4 is not yet ready to process timeouts
4032 * or interrupts. Once it is, the comment bars can be removed.
4033 */
4034 /* mod_timer(&vport->els_tmofunc,
4035 * jiffies + HZ * (phba->fc_ratov*2)); */
4036
4037 /* Start heart beat timer */
4038 mod_timer(&phba->hb_tmofunc,
4039 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4040 phba->hb_outstanding = 0;
4041 phba->last_completion_time = jiffies;
4042
4043 /* Start error attention (ERATT) polling timer */
4044 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4045
4046 /*
4047 * The port is ready, set the host's link state to LINK_DOWN
4048 * in preparation for link interrupts.
4049 */
4050 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4051 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4052 lpfc_set_loopback_flag(phba);
4053 /* Change driver state to LPFC_LINK_DOWN right before init link */
4054 spin_lock_irq(&phba->hbalock);
4055 phba->link_state = LPFC_LINK_DOWN;
4056 spin_unlock_irq(&phba->hbalock);
4057 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4058 if (unlikely(rc != MBX_NOT_FINISHED)) {
4059 kfree(vpd);
4060 return 0;
4061 } else
4062 rc = -EIO;
4063
4064 /* Unset all the queues set up in this routine when error out */
4065 if (rc)
4066 lpfc_sli4_queue_unset(phba);
4067
4068out_stop_timers:
4069 if (rc)
4070 lpfc_stop_hba_timers(phba);
4071out_free_vpd:
4072 kfree(vpd);
4073out_free_mbox:
4074 mempool_free(mboxq, phba->mbox_mem_pool);
4075 return rc;
4076}
James Smarte59058c2008-08-24 21:49:00 -04004077
4078/**
James Smart3621a712009-04-06 18:47:14 -04004079 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004080 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004081 *
James Smarte59058c2008-08-24 21:49:00 -04004082 * This is the callback function for mailbox timer. The mailbox
4083 * timer is armed when a new mailbox command is issued and the timer
4084 * is deleted when the mailbox complete. The function is called by
4085 * the kernel timer code when a mailbox does not complete within
4086 * expected time. This function wakes up the worker thread to
4087 * process the mailbox timeout and returns. All the processing is
4088 * done by the worker thread function lpfc_mbox_timeout_handler.
4089 **/
dea31012005-04-17 16:05:31 -05004090void
4091lpfc_mbox_timeout(unsigned long ptr)
4092{
James Smart92d7f7b2007-06-17 19:56:38 -05004093 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004094 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004095 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004096
James Smart2e0fef82007-06-17 19:56:36 -05004097 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004098 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004099 if (!tmo_posted)
4100 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4101 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4102
James Smart5e9d9b82008-06-14 22:52:53 -04004103 if (!tmo_posted)
4104 lpfc_worker_wake_up(phba);
4105 return;
dea31012005-04-17 16:05:31 -05004106}
4107
James Smarte59058c2008-08-24 21:49:00 -04004108
4109/**
James Smart3621a712009-04-06 18:47:14 -04004110 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004111 * @phba: Pointer to HBA context object.
4112 *
4113 * This function is called from worker thread when a mailbox command times out.
4114 * The caller is not required to hold any locks. This function will reset the
4115 * HBA and recover all the pending commands.
4116 **/
dea31012005-04-17 16:05:31 -05004117void
4118lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4119{
James Smart2e0fef82007-06-17 19:56:36 -05004120 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
4121 MAILBOX_t *mb = &pmbox->mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004122 struct lpfc_sli *psli = &phba->sli;
4123 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004124
James Smarta257bf92009-04-06 18:48:10 -04004125 /* Check the pmbox pointer first. There is a race condition
4126 * between the mbox timeout handler getting executed in the
4127 * worklist and the mailbox actually completing. When this
4128 * race condition occurs, the mbox_active will be NULL.
4129 */
4130 spin_lock_irq(&phba->hbalock);
4131 if (pmbox == NULL) {
4132 lpfc_printf_log(phba, KERN_WARNING,
4133 LOG_MBOX | LOG_SLI,
4134 "0353 Active Mailbox cleared - mailbox timeout "
4135 "exiting\n");
4136 spin_unlock_irq(&phba->hbalock);
4137 return;
4138 }
4139
dea31012005-04-17 16:05:31 -05004140 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004141 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004142 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004143 mb->mbxCommand,
4144 phba->pport->port_state,
4145 phba->sli.sli_flag,
4146 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004147 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004148
James Smart1dcb58e2007-04-25 09:51:30 -04004149 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4150 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4151 * it to fail all oustanding SCSI IO.
4152 */
James Smart2e0fef82007-06-17 19:56:36 -05004153 spin_lock_irq(&phba->pport->work_port_lock);
4154 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4155 spin_unlock_irq(&phba->pport->work_port_lock);
4156 spin_lock_irq(&phba->hbalock);
4157 phba->link_state = LPFC_LINK_UNKNOWN;
James Smart1dcb58e2007-04-25 09:51:30 -04004158 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004159 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004160
4161 pring = &psli->ring[psli->fcp_ring];
4162 lpfc_sli_abort_iocb_ring(phba, pring);
4163
4164 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004165 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004166
4167 /* Reset the HBA device */
4168 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004169}
4170
James Smarte59058c2008-08-24 21:49:00 -04004171/**
James Smart3772a992009-05-22 14:50:54 -04004172 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004173 * @phba: Pointer to HBA context object.
4174 * @pmbox: Pointer to mailbox object.
4175 * @flag: Flag indicating how the mailbox need to be processed.
4176 *
4177 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004178 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4179 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004180 * The mailbox command can be submitted in polling mode, in which case
4181 * this function will wait in a polling loop for the completion of the
4182 * mailbox.
4183 * If the mailbox is submitted in no_wait mode (not polling) the
4184 * function will submit the command and returns immediately without waiting
4185 * for the mailbox completion. The no_wait is supported only when HBA
4186 * is in SLI2/SLI3 mode - interrupts are enabled.
4187 * The SLI interface allows only one mailbox pending at a time. If the
4188 * mailbox is issued in polling mode and there is already a mailbox
4189 * pending, then the function will return an error. If the mailbox is issued
4190 * in NO_WAIT mode and there is a mailbox pending already, the function
4191 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4192 * The sli layer owns the mailbox object until the completion of mailbox
4193 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4194 * return codes the caller owns the mailbox command after the return of
4195 * the function.
4196 **/
James Smart3772a992009-05-22 14:50:54 -04004197static int
4198lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4199 uint32_t flag)
dea31012005-04-17 16:05:31 -05004200{
dea31012005-04-17 16:05:31 -05004201 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004202 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004203 uint32_t status, evtctr;
4204 uint32_t ha_copy;
4205 int i;
James Smart09372822008-01-11 01:52:54 -05004206 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004207 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004208 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004209 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004210 int processing_queue = 0;
4211
4212 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4213 if (!pmbox) {
4214 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004215 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4216 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4217 return MBX_SUCCESS;
4218 }
James Smart58da1ff2008-04-07 10:15:56 -04004219 processing_queue = 1;
4220 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4221 pmbox = lpfc_mbox_get(phba);
4222 if (!pmbox) {
4223 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4224 return MBX_SUCCESS;
4225 }
4226 }
dea31012005-04-17 16:05:31 -05004227
James Smarted957682007-06-17 19:56:37 -05004228 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004229 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004230 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004231 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004232 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004233 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004234 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004235 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004236 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004237 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004238 }
4239 }
4240
Linas Vepstas8d63f372007-02-14 14:28:36 -06004241 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004242 if (unlikely(pci_channel_offline(phba->pcidev))) {
4243 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4244 goto out_not_finished;
4245 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004246
James Smarta257bf92009-04-06 18:48:10 -04004247 /* If HBA has a deferred error attention, fail the iocb. */
4248 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4249 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4250 goto out_not_finished;
4251 }
4252
dea31012005-04-17 16:05:31 -05004253 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004254
James Smart3772a992009-05-22 14:50:54 -04004255 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004256 status = MBX_SUCCESS;
4257
James Smart2e0fef82007-06-17 19:56:36 -05004258 if (phba->link_state == LPFC_HBA_ERROR) {
4259 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004260
4261 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004262 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4263 "(%d):0311 Mailbox command x%x cannot "
4264 "issue Data: x%x x%x\n",
4265 pmbox->vport ? pmbox->vport->vpi : 0,
4266 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004267 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004268 }
4269
James Smart92908312006-03-07 15:04:13 -05004270 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4271 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004272 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004273 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4274 "(%d):2528 Mailbox command x%x cannot "
4275 "issue Data: x%x x%x\n",
4276 pmbox->vport ? pmbox->vport->vpi : 0,
4277 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004278 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004279 }
4280
dea31012005-04-17 16:05:31 -05004281 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4282 /* Polling for a mbox command when another one is already active
4283 * is not allowed in SLI. Also, the driver must have established
4284 * SLI2 mode to queue and process multiple mbox commands.
4285 */
4286
4287 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004288 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004289
4290 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004291 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4292 "(%d):2529 Mailbox command x%x "
4293 "cannot issue Data: x%x x%x\n",
4294 pmbox->vport ? pmbox->vport->vpi : 0,
4295 pmbox->u.mb.mbxCommand,
4296 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004297 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004298 }
4299
James Smart3772a992009-05-22 14:50:54 -04004300 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004301 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004302 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004303 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4304 "(%d):2530 Mailbox command x%x "
4305 "cannot issue Data: x%x x%x\n",
4306 pmbox->vport ? pmbox->vport->vpi : 0,
4307 pmbox->u.mb.mbxCommand,
4308 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004309 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004310 }
4311
dea31012005-04-17 16:05:31 -05004312 /* Another mailbox command is still being processed, queue this
4313 * command to be processed later.
4314 */
4315 lpfc_mbox_put(phba, pmbox);
4316
4317 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004318 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004319 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004320 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004321 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4322 mb->mbxCommand, phba->pport->port_state,
4323 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004324
4325 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004326 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004327
James Smart858c9f62007-06-17 19:56:39 -05004328 if (pmbox->vport) {
4329 lpfc_debugfs_disc_trc(pmbox->vport,
4330 LPFC_DISC_TRC_MBOX_VPORT,
4331 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4332 (uint32_t)mb->mbxCommand,
4333 mb->un.varWords[0], mb->un.varWords[1]);
4334 }
4335 else {
4336 lpfc_debugfs_disc_trc(phba->pport,
4337 LPFC_DISC_TRC_MBOX,
4338 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4339 (uint32_t)mb->mbxCommand,
4340 mb->un.varWords[0], mb->un.varWords[1]);
4341 }
4342
James Smart2e0fef82007-06-17 19:56:36 -05004343 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004344 }
4345
dea31012005-04-17 16:05:31 -05004346 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4347
4348 /* If we are not polling, we MUST be in SLI2 mode */
4349 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004350 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004351 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004352 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004353 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004354 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004355 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4356 "(%d):2531 Mailbox command x%x "
4357 "cannot issue Data: x%x x%x\n",
4358 pmbox->vport ? pmbox->vport->vpi : 0,
4359 pmbox->u.mb.mbxCommand,
4360 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004361 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004362 }
4363 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004364 mod_timer(&psli->mbox_tmo, (jiffies +
4365 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004366 }
4367
4368 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004369 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004370 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004371 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004372 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004373 mb->mbxCommand, phba->pport->port_state,
4374 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004375
James Smart858c9f62007-06-17 19:56:39 -05004376 if (mb->mbxCommand != MBX_HEARTBEAT) {
4377 if (pmbox->vport) {
4378 lpfc_debugfs_disc_trc(pmbox->vport,
4379 LPFC_DISC_TRC_MBOX_VPORT,
4380 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4381 (uint32_t)mb->mbxCommand,
4382 mb->un.varWords[0], mb->un.varWords[1]);
4383 }
4384 else {
4385 lpfc_debugfs_disc_trc(phba->pport,
4386 LPFC_DISC_TRC_MBOX,
4387 "MBOX Send: cmd:x%x mb:x%x x%x",
4388 (uint32_t)mb->mbxCommand,
4389 mb->un.varWords[0], mb->un.varWords[1]);
4390 }
4391 }
4392
dea31012005-04-17 16:05:31 -05004393 psli->slistat.mbox_cmd++;
4394 evtctr = psli->slistat.mbox_event;
4395
4396 /* next set own bit for the adapter and copy over command word */
4397 mb->mbxOwner = OWN_CHIP;
4398
James Smart3772a992009-05-22 14:50:54 -04004399 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004400 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004401 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004402 } else {
James Smart92908312006-03-07 15:04:13 -05004403 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004404 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004405 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004406 }
4407
4408 /* First copy mbox command data to HBA SLIM, skip past first
4409 word */
4410 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4411 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4412 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4413
4414 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004415 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004416 to_slim = phba->MBslimaddr;
4417 writel(ldata, to_slim);
4418 readl(to_slim); /* flush */
4419
4420 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4421 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004422 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004423 }
4424 }
4425
4426 wmb();
dea31012005-04-17 16:05:31 -05004427
4428 switch (flag) {
4429 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004430 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004431 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004432 /* Interrupt board to do it */
4433 writel(CA_MBATT, phba->CAregaddr);
4434 readl(phba->CAregaddr); /* flush */
4435 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004436 break;
4437
4438 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004439 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004440 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004441 /* Interrupt board to do it */
4442 writel(CA_MBATT, phba->CAregaddr);
4443 readl(phba->CAregaddr); /* flush */
4444
James Smart3772a992009-05-22 14:50:54 -04004445 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004446 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004447 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004448 word0 = le32_to_cpu(word0);
4449 } else {
4450 /* First read mbox status word */
4451 word0 = readl(phba->MBslimaddr);
4452 }
4453
4454 /* Read the HBA Host Attention Register */
4455 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004456 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4457 mb->mbxCommand) *
4458 1000) + jiffies;
4459 i = 0;
dea31012005-04-17 16:05:31 -05004460 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004461 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4462 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004463 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004464 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004465 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004466 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004467 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004468 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004469 }
4470
4471 /* Check if we took a mbox interrupt while we were
4472 polling */
4473 if (((word0 & OWN_CHIP) != OWN_CHIP)
4474 && (evtctr != psli->slistat.mbox_event))
4475 break;
4476
James Smart09372822008-01-11 01:52:54 -05004477 if (i++ > 10) {
4478 spin_unlock_irqrestore(&phba->hbalock,
4479 drvr_flag);
4480 msleep(1);
4481 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4482 }
dea31012005-04-17 16:05:31 -05004483
James Smart3772a992009-05-22 14:50:54 -04004484 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004485 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04004486 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004487 word0 = le32_to_cpu(word0);
4488 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4489 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04004490 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05004491 /* Check real SLIM for any errors */
4492 slimword0 = readl(phba->MBslimaddr);
4493 slimmb = (MAILBOX_t *) & slimword0;
4494 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4495 && slimmb->mbxStatus) {
4496 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04004497 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004498 word0 = slimword0;
4499 }
4500 }
4501 } else {
4502 /* First copy command data */
4503 word0 = readl(phba->MBslimaddr);
4504 }
4505 /* Read the HBA Host Attention Register */
4506 ha_copy = readl(phba->HAregaddr);
4507 }
4508
James Smart3772a992009-05-22 14:50:54 -04004509 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004510 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04004511 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004512 } else {
4513 /* First copy command data */
4514 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
4515 MAILBOX_CMD_SIZE);
4516 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
4517 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05004518 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05004519 phba->MBslimaddr + DMP_RSP_OFFSET,
4520 mb->un.varDmp.word_cnt);
4521 }
4522 }
4523
4524 writel(HA_MBATT, phba->HAregaddr);
4525 readl(phba->HAregaddr); /* flush */
4526
4527 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4528 status = mb->mbxStatus;
4529 }
4530
James Smart2e0fef82007-06-17 19:56:36 -05004531 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4532 return status;
James Smart58da1ff2008-04-07 10:15:56 -04004533
4534out_not_finished:
4535 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04004536 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04004537 lpfc_mbox_cmpl_put(phba, pmbox);
4538 }
4539 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05004540}
4541
James Smarte59058c2008-08-24 21:49:00 -04004542/**
James Smartda0436e2009-05-22 14:51:39 -04004543 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
4544 * @phba: Pointer to HBA context object.
4545 * @mboxq: Pointer to mailbox object.
4546 *
4547 * The function posts a mailbox to the port. The mailbox is expected
4548 * to be comletely filled in and ready for the port to operate on it.
4549 * This routine executes a synchronous completion operation on the
4550 * mailbox by polling for its completion.
4551 *
4552 * The caller must not be holding any locks when calling this routine.
4553 *
4554 * Returns:
4555 * MBX_SUCCESS - mailbox posted successfully
4556 * Any of the MBX error values.
4557 **/
4558static int
4559lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4560{
4561 int rc = MBX_SUCCESS;
4562 unsigned long iflag;
4563 uint32_t db_ready;
4564 uint32_t mcqe_status;
4565 uint32_t mbx_cmnd;
4566 unsigned long timeout;
4567 struct lpfc_sli *psli = &phba->sli;
4568 struct lpfc_mqe *mb = &mboxq->u.mqe;
4569 struct lpfc_bmbx_create *mbox_rgn;
4570 struct dma_address *dma_address;
4571 struct lpfc_register bmbx_reg;
4572
4573 /*
4574 * Only one mailbox can be active to the bootstrap mailbox region
4575 * at a time and there is no queueing provided.
4576 */
4577 spin_lock_irqsave(&phba->hbalock, iflag);
4578 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4579 spin_unlock_irqrestore(&phba->hbalock, iflag);
4580 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4581 "(%d):2532 Mailbox command x%x (x%x) "
4582 "cannot issue Data: x%x x%x\n",
4583 mboxq->vport ? mboxq->vport->vpi : 0,
4584 mboxq->u.mb.mbxCommand,
4585 lpfc_sli4_mbox_opcode_get(phba, mboxq),
4586 psli->sli_flag, MBX_POLL);
4587 return MBXERR_ERROR;
4588 }
4589 /* The server grabs the token and owns it until release */
4590 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4591 phba->sli.mbox_active = mboxq;
4592 spin_unlock_irqrestore(&phba->hbalock, iflag);
4593
4594 /*
4595 * Initialize the bootstrap memory region to avoid stale data areas
4596 * in the mailbox post. Then copy the caller's mailbox contents to
4597 * the bmbx mailbox region.
4598 */
4599 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
4600 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
4601 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
4602 sizeof(struct lpfc_mqe));
4603
4604 /* Post the high mailbox dma address to the port and wait for ready. */
4605 dma_address = &phba->sli4_hba.bmbx.dma_address;
4606 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
4607
4608 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
4609 * 1000) + jiffies;
4610 do {
4611 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
4612 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
4613 if (!db_ready)
4614 msleep(2);
4615
4616 if (time_after(jiffies, timeout)) {
4617 rc = MBXERR_ERROR;
4618 goto exit;
4619 }
4620 } while (!db_ready);
4621
4622 /* Post the low mailbox dma address to the port. */
4623 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
4624 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
4625 * 1000) + jiffies;
4626 do {
4627 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
4628 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
4629 if (!db_ready)
4630 msleep(2);
4631
4632 if (time_after(jiffies, timeout)) {
4633 rc = MBXERR_ERROR;
4634 goto exit;
4635 }
4636 } while (!db_ready);
4637
4638 /*
4639 * Read the CQ to ensure the mailbox has completed.
4640 * If so, update the mailbox status so that the upper layers
4641 * can complete the request normally.
4642 */
4643 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
4644 sizeof(struct lpfc_mqe));
4645 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
4646 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
4647 sizeof(struct lpfc_mcqe));
4648 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
4649
4650 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
4651 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
4652 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
4653 rc = MBXERR_ERROR;
4654 }
4655
4656 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4657 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
4658 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
4659 " x%x x%x CQ: x%x x%x x%x x%x\n",
4660 mboxq->vport ? mboxq->vport->vpi : 0,
4661 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
4662 bf_get(lpfc_mqe_status, mb),
4663 mb->un.mb_words[0], mb->un.mb_words[1],
4664 mb->un.mb_words[2], mb->un.mb_words[3],
4665 mb->un.mb_words[4], mb->un.mb_words[5],
4666 mb->un.mb_words[6], mb->un.mb_words[7],
4667 mb->un.mb_words[8], mb->un.mb_words[9],
4668 mb->un.mb_words[10], mb->un.mb_words[11],
4669 mb->un.mb_words[12], mboxq->mcqe.word0,
4670 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4671 mboxq->mcqe.trailer);
4672exit:
4673 /* We are holding the token, no needed for lock when release */
4674 spin_lock_irqsave(&phba->hbalock, iflag);
4675 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4676 phba->sli.mbox_active = NULL;
4677 spin_unlock_irqrestore(&phba->hbalock, iflag);
4678 return rc;
4679}
4680
4681/**
4682 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
4683 * @phba: Pointer to HBA context object.
4684 * @pmbox: Pointer to mailbox object.
4685 * @flag: Flag indicating how the mailbox need to be processed.
4686 *
4687 * This function is called by discovery code and HBA management code to submit
4688 * a mailbox command to firmware with SLI-4 interface spec.
4689 *
4690 * Return codes the caller owns the mailbox command after the return of the
4691 * function.
4692 **/
4693static int
4694lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4695 uint32_t flag)
4696{
4697 struct lpfc_sli *psli = &phba->sli;
4698 unsigned long iflags;
4699 int rc;
4700
4701 /* Detect polling mode and jump to a handler */
4702 if (!phba->sli4_hba.intr_enable) {
4703 if (flag == MBX_POLL)
4704 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
4705 else
4706 rc = -EIO;
4707 if (rc != MBX_SUCCESS)
4708 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4709 "(%d):2541 Mailbox command x%x "
4710 "(x%x) cannot issue Data: x%x x%x\n",
4711 mboxq->vport ? mboxq->vport->vpi : 0,
4712 mboxq->u.mb.mbxCommand,
4713 lpfc_sli4_mbox_opcode_get(phba, mboxq),
4714 psli->sli_flag, flag);
4715 return rc;
4716 } else if (flag == MBX_POLL) {
4717 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4718 "(%d):2542 Mailbox command x%x (x%x) "
4719 "cannot issue Data: x%x x%x\n",
4720 mboxq->vport ? mboxq->vport->vpi : 0,
4721 mboxq->u.mb.mbxCommand,
4722 lpfc_sli4_mbox_opcode_get(phba, mboxq),
4723 psli->sli_flag, flag);
4724 return -EIO;
4725 }
4726
4727 /* Now, interrupt mode asynchrous mailbox command */
4728 rc = lpfc_mbox_cmd_check(phba, mboxq);
4729 if (rc) {
4730 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4731 "(%d):2543 Mailbox command x%x (x%x) "
4732 "cannot issue Data: x%x x%x\n",
4733 mboxq->vport ? mboxq->vport->vpi : 0,
4734 mboxq->u.mb.mbxCommand,
4735 lpfc_sli4_mbox_opcode_get(phba, mboxq),
4736 psli->sli_flag, flag);
4737 goto out_not_finished;
4738 }
4739 rc = lpfc_mbox_dev_check(phba);
4740 if (unlikely(rc)) {
4741 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4742 "(%d):2544 Mailbox command x%x (x%x) "
4743 "cannot issue Data: x%x x%x\n",
4744 mboxq->vport ? mboxq->vport->vpi : 0,
4745 mboxq->u.mb.mbxCommand,
4746 lpfc_sli4_mbox_opcode_get(phba, mboxq),
4747 psli->sli_flag, flag);
4748 goto out_not_finished;
4749 }
4750
4751 /* Put the mailbox command to the driver internal FIFO */
4752 psli->slistat.mbox_busy++;
4753 spin_lock_irqsave(&phba->hbalock, iflags);
4754 lpfc_mbox_put(phba, mboxq);
4755 spin_unlock_irqrestore(&phba->hbalock, iflags);
4756 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4757 "(%d):0354 Mbox cmd issue - Enqueue Data: "
4758 "x%x (x%x) x%x x%x x%x\n",
4759 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
4760 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4761 lpfc_sli4_mbox_opcode_get(phba, mboxq),
4762 phba->pport->port_state,
4763 psli->sli_flag, MBX_NOWAIT);
4764 /* Wake up worker thread to transport mailbox command from head */
4765 lpfc_worker_wake_up(phba);
4766
4767 return MBX_BUSY;
4768
4769out_not_finished:
4770 return MBX_NOT_FINISHED;
4771}
4772
4773/**
4774 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
4775 * @phba: Pointer to HBA context object.
4776 *
4777 * This function is called by worker thread to send a mailbox command to
4778 * SLI4 HBA firmware.
4779 *
4780 **/
4781int
4782lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
4783{
4784 struct lpfc_sli *psli = &phba->sli;
4785 LPFC_MBOXQ_t *mboxq;
4786 int rc = MBX_SUCCESS;
4787 unsigned long iflags;
4788 struct lpfc_mqe *mqe;
4789 uint32_t mbx_cmnd;
4790
4791 /* Check interrupt mode before post async mailbox command */
4792 if (unlikely(!phba->sli4_hba.intr_enable))
4793 return MBX_NOT_FINISHED;
4794
4795 /* Check for mailbox command service token */
4796 spin_lock_irqsave(&phba->hbalock, iflags);
4797 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4798 spin_unlock_irqrestore(&phba->hbalock, iflags);
4799 return MBX_NOT_FINISHED;
4800 }
4801 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4802 spin_unlock_irqrestore(&phba->hbalock, iflags);
4803 return MBX_NOT_FINISHED;
4804 }
4805 if (unlikely(phba->sli.mbox_active)) {
4806 spin_unlock_irqrestore(&phba->hbalock, iflags);
4807 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4808 "0384 There is pending active mailbox cmd\n");
4809 return MBX_NOT_FINISHED;
4810 }
4811 /* Take the mailbox command service token */
4812 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4813
4814 /* Get the next mailbox command from head of queue */
4815 mboxq = lpfc_mbox_get(phba);
4816
4817 /* If no more mailbox command waiting for post, we're done */
4818 if (!mboxq) {
4819 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4820 spin_unlock_irqrestore(&phba->hbalock, iflags);
4821 return MBX_SUCCESS;
4822 }
4823 phba->sli.mbox_active = mboxq;
4824 spin_unlock_irqrestore(&phba->hbalock, iflags);
4825
4826 /* Check device readiness for posting mailbox command */
4827 rc = lpfc_mbox_dev_check(phba);
4828 if (unlikely(rc))
4829 /* Driver clean routine will clean up pending mailbox */
4830 goto out_not_finished;
4831
4832 /* Prepare the mbox command to be posted */
4833 mqe = &mboxq->u.mqe;
4834 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
4835
4836 /* Start timer for the mbox_tmo and log some mailbox post messages */
4837 mod_timer(&psli->mbox_tmo, (jiffies +
4838 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
4839
4840 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4841 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
4842 "x%x x%x\n",
4843 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
4844 lpfc_sli4_mbox_opcode_get(phba, mboxq),
4845 phba->pport->port_state, psli->sli_flag);
4846
4847 if (mbx_cmnd != MBX_HEARTBEAT) {
4848 if (mboxq->vport) {
4849 lpfc_debugfs_disc_trc(mboxq->vport,
4850 LPFC_DISC_TRC_MBOX_VPORT,
4851 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4852 mbx_cmnd, mqe->un.mb_words[0],
4853 mqe->un.mb_words[1]);
4854 } else {
4855 lpfc_debugfs_disc_trc(phba->pport,
4856 LPFC_DISC_TRC_MBOX,
4857 "MBOX Send: cmd:x%x mb:x%x x%x",
4858 mbx_cmnd, mqe->un.mb_words[0],
4859 mqe->un.mb_words[1]);
4860 }
4861 }
4862 psli->slistat.mbox_cmd++;
4863
4864 /* Post the mailbox command to the port */
4865 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
4866 if (rc != MBX_SUCCESS) {
4867 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4868 "(%d):2533 Mailbox command x%x (x%x) "
4869 "cannot issue Data: x%x x%x\n",
4870 mboxq->vport ? mboxq->vport->vpi : 0,
4871 mboxq->u.mb.mbxCommand,
4872 lpfc_sli4_mbox_opcode_get(phba, mboxq),
4873 psli->sli_flag, MBX_NOWAIT);
4874 goto out_not_finished;
4875 }
4876
4877 return rc;
4878
4879out_not_finished:
4880 spin_lock_irqsave(&phba->hbalock, iflags);
4881 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
4882 __lpfc_mbox_cmpl_put(phba, mboxq);
4883 /* Release the token */
4884 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4885 phba->sli.mbox_active = NULL;
4886 spin_unlock_irqrestore(&phba->hbalock, iflags);
4887
4888 return MBX_NOT_FINISHED;
4889}
4890
4891/**
4892 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
4893 * @phba: Pointer to HBA context object.
4894 * @pmbox: Pointer to mailbox object.
4895 * @flag: Flag indicating how the mailbox need to be processed.
4896 *
4897 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
4898 * the API jump table function pointer from the lpfc_hba struct.
4899 *
4900 * Return codes the caller owns the mailbox command after the return of the
4901 * function.
4902 **/
4903int
4904lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
4905{
4906 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
4907}
4908
4909/**
4910 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
4911 * @phba: The hba struct for which this call is being executed.
4912 * @dev_grp: The HBA PCI-Device group number.
4913 *
4914 * This routine sets up the mbox interface API function jump table in @phba
4915 * struct.
4916 * Returns: 0 - success, -ENODEV - failure.
4917 **/
4918int
4919lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4920{
4921
4922 switch (dev_grp) {
4923 case LPFC_PCI_DEV_LP:
4924 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
4925 phba->lpfc_sli_handle_slow_ring_event =
4926 lpfc_sli_handle_slow_ring_event_s3;
4927 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
4928 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
4929 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
4930 break;
4931 case LPFC_PCI_DEV_OC:
4932 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
4933 phba->lpfc_sli_handle_slow_ring_event =
4934 lpfc_sli_handle_slow_ring_event_s4;
4935 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
4936 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
4937 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
4938 break;
4939 default:
4940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4941 "1420 Invalid HBA PCI-device group: 0x%x\n",
4942 dev_grp);
4943 return -ENODEV;
4944 break;
4945 }
4946 return 0;
4947}
4948
4949/**
James Smart3621a712009-04-06 18:47:14 -04004950 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04004951 * @phba: Pointer to HBA context object.
4952 * @pring: Pointer to driver SLI ring object.
4953 * @piocb: Pointer to address of newly added command iocb.
4954 *
4955 * This function is called with hbalock held to add a command
4956 * iocb to the txq when SLI layer cannot submit the command iocb
4957 * to the ring.
4958 **/
James Smart858c9f62007-06-17 19:56:39 -05004959static void
James Smart92d7f7b2007-06-17 19:56:38 -05004960__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05004961 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05004962{
4963 /* Insert the caller's iocb in the txq tail for later processing. */
4964 list_add_tail(&piocb->list, &pring->txq);
4965 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05004966}
4967
James Smarte59058c2008-08-24 21:49:00 -04004968/**
James Smart3621a712009-04-06 18:47:14 -04004969 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04004970 * @phba: Pointer to HBA context object.
4971 * @pring: Pointer to driver SLI ring object.
4972 * @piocb: Pointer to address of newly added command iocb.
4973 *
4974 * This function is called with hbalock held before a new
4975 * iocb is submitted to the firmware. This function checks
4976 * txq to flush the iocbs in txq to Firmware before
4977 * submitting new iocbs to the Firmware.
4978 * If there are iocbs in the txq which need to be submitted
4979 * to firmware, lpfc_sli_next_iocb returns the first element
4980 * of the txq after dequeuing it from txq.
4981 * If there is no iocb in the txq then the function will return
4982 * *piocb and *piocb is set to NULL. Caller needs to check
4983 * *piocb to find if there are more commands in the txq.
4984 **/
dea31012005-04-17 16:05:31 -05004985static struct lpfc_iocbq *
4986lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05004987 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05004988{
4989 struct lpfc_iocbq * nextiocb;
4990
4991 nextiocb = lpfc_sli_ringtx_get(phba, pring);
4992 if (!nextiocb) {
4993 nextiocb = *piocb;
4994 *piocb = NULL;
4995 }
4996
4997 return nextiocb;
4998}
4999
James Smarte59058c2008-08-24 21:49:00 -04005000/**
James Smart3772a992009-05-22 14:50:54 -04005001 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005002 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005003 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005004 * @piocb: Pointer to command iocb.
5005 * @flag: Flag indicating if this command can be put into txq.
5006 *
James Smart3772a992009-05-22 14:50:54 -04005007 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5008 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5009 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5010 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5011 * this function allows only iocbs for posting buffers. This function finds
5012 * next available slot in the command ring and posts the command to the
5013 * available slot and writes the port attention register to request HBA start
5014 * processing new iocb. If there is no slot available in the ring and
5015 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5016 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005017 *
James Smart3772a992009-05-22 14:50:54 -04005018 * This function is called with hbalock held. The function will return success
5019 * after it successfully submit the iocb to firmware or after adding to the
5020 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005021 **/
James Smart98c9ea52007-10-27 13:37:33 -04005022static int
James Smart3772a992009-05-22 14:50:54 -04005023__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005024 struct lpfc_iocbq *piocb, uint32_t flag)
5025{
5026 struct lpfc_iocbq *nextiocb;
5027 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005028 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005029
James Smart92d7f7b2007-06-17 19:56:38 -05005030 if (piocb->iocb_cmpl && (!piocb->vport) &&
5031 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5032 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5033 lpfc_printf_log(phba, KERN_ERR,
5034 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005035 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005036 piocb->iocb.ulpCommand);
5037 dump_stack();
5038 return IOCB_ERROR;
5039 }
5040
5041
Linas Vepstas8d63f372007-02-14 14:28:36 -06005042 /* If the PCI channel is in offline state, do not post iocbs. */
5043 if (unlikely(pci_channel_offline(phba->pcidev)))
5044 return IOCB_ERROR;
5045
James Smarta257bf92009-04-06 18:48:10 -04005046 /* If HBA has a deferred error attention, fail the iocb. */
5047 if (unlikely(phba->hba_flag & DEFER_ERATT))
5048 return IOCB_ERROR;
5049
dea31012005-04-17 16:05:31 -05005050 /*
5051 * We should never get an IOCB if we are in a < LINK_DOWN state
5052 */
James Smart2e0fef82007-06-17 19:56:36 -05005053 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005054 return IOCB_ERROR;
5055
5056 /*
5057 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005058 * outstanding event.
dea31012005-04-17 16:05:31 -05005059 */
James Smart0b727fe2007-10-27 13:37:25 -04005060 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005061 goto iocb_busy;
5062
James Smart2e0fef82007-06-17 19:56:36 -05005063 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005064 /*
James Smart2680eea2007-04-25 09:52:55 -04005065 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005066 * can be issued if the link is not up.
5067 */
5068 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005069 case CMD_GEN_REQUEST64_CR:
5070 case CMD_GEN_REQUEST64_CX:
5071 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5072 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5073 FC_FCP_CMND) ||
5074 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5075 MENLO_TRANSPORT_TYPE))
5076
5077 goto iocb_busy;
5078 break;
dea31012005-04-17 16:05:31 -05005079 case CMD_QUE_RING_BUF_CN:
5080 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005081 /*
5082 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5083 * completion, iocb_cmpl MUST be 0.
5084 */
5085 if (piocb->iocb_cmpl)
5086 piocb->iocb_cmpl = NULL;
5087 /*FALLTHROUGH*/
5088 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005089 case CMD_CLOSE_XRI_CN:
5090 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005091 break;
5092 default:
5093 goto iocb_busy;
5094 }
5095
5096 /*
5097 * For FCP commands, we must be in a state where we can process link
5098 * attention events.
5099 */
5100 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005101 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005102 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005103 }
dea31012005-04-17 16:05:31 -05005104
dea31012005-04-17 16:05:31 -05005105 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5106 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5107 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5108
5109 if (iocb)
5110 lpfc_sli_update_ring(phba, pring);
5111 else
5112 lpfc_sli_update_full_ring(phba, pring);
5113
5114 if (!piocb)
5115 return IOCB_SUCCESS;
5116
5117 goto out_busy;
5118
5119 iocb_busy:
5120 pring->stats.iocb_cmd_delay++;
5121
5122 out_busy:
5123
5124 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005125 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005126 return IOCB_SUCCESS;
5127 }
5128
5129 return IOCB_BUSY;
5130}
5131
James Smart3772a992009-05-22 14:50:54 -04005132/**
5133 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
5134 *
5135 * This routine wraps the actual lockless version for issusing IOCB function
5136 * pointer from the lpfc_hba struct.
5137 *
5138 * Return codes:
5139 * IOCB_ERROR - Error
5140 * IOCB_SUCCESS - Success
5141 * IOCB_BUSY - Busy
5142 **/
5143static inline int
5144__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
5145 struct lpfc_iocbq *piocb, uint32_t flag)
5146{
5147 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
5148}
5149
5150/**
5151 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
5152 * @phba: The hba struct for which this call is being executed.
5153 * @dev_grp: The HBA PCI-Device group number.
5154 *
5155 * This routine sets up the SLI interface API function jump table in @phba
5156 * struct.
5157 * Returns: 0 - success, -ENODEV - failure.
5158 **/
5159int
5160lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5161{
5162
5163 switch (dev_grp) {
5164 case LPFC_PCI_DEV_LP:
5165 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
5166 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
5167 break;
5168 default:
5169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5170 "1419 Invalid HBA PCI-device group: 0x%x\n",
5171 dev_grp);
5172 return -ENODEV;
5173 break;
5174 }
5175 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
5176 return 0;
5177}
James Smart92d7f7b2007-06-17 19:56:38 -05005178
James Smarte59058c2008-08-24 21:49:00 -04005179/**
James Smart3621a712009-04-06 18:47:14 -04005180 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005181 * @phba: Pointer to HBA context object.
5182 * @pring: Pointer to driver SLI ring object.
5183 * @piocb: Pointer to command iocb.
5184 * @flag: Flag indicating if this command can be put into txq.
5185 *
5186 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
5187 * function. This function gets the hbalock and calls
5188 * __lpfc_sli_issue_iocb function and will return the error returned
5189 * by __lpfc_sli_issue_iocb function. This wrapper is used by
5190 * functions which do not hold hbalock.
5191 **/
James Smart92d7f7b2007-06-17 19:56:38 -05005192int
James Smart3772a992009-05-22 14:50:54 -04005193lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05005194 struct lpfc_iocbq *piocb, uint32_t flag)
5195{
5196 unsigned long iflags;
5197 int rc;
5198
5199 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04005200 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05005201 spin_unlock_irqrestore(&phba->hbalock, iflags);
5202
5203 return rc;
5204}
5205
James Smarte59058c2008-08-24 21:49:00 -04005206/**
James Smart3621a712009-04-06 18:47:14 -04005207 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04005208 * @phba: Pointer to HBA context object.
5209 *
5210 * This function is called while driver attaches with the
5211 * HBA to setup the extra ring. The extra ring is used
5212 * only when driver needs to support target mode functionality
5213 * or IP over FC functionalities.
5214 *
5215 * This function is called with no lock held.
5216 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05005217static int
5218lpfc_extra_ring_setup( struct lpfc_hba *phba)
5219{
5220 struct lpfc_sli *psli;
5221 struct lpfc_sli_ring *pring;
5222
5223 psli = &phba->sli;
5224
5225 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05005226
5227 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05005228 pring = &psli->ring[psli->fcp_ring];
5229 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
5230 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
5231 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
5232 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
5233
James Smarta4bc3372006-12-02 13:34:16 -05005234 /* and give them to the extra ring */
5235 pring = &psli->ring[psli->extra_ring];
5236
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05005237 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
5238 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
5239 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
5240 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
5241
5242 /* Setup default profile for this ring */
5243 pring->iotag_max = 4096;
5244 pring->num_mask = 1;
5245 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05005246 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
5247 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05005248 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
5249 return 0;
5250}
5251
James Smarte59058c2008-08-24 21:49:00 -04005252/**
James Smart3621a712009-04-06 18:47:14 -04005253 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04005254 * @phba: Pointer to HBA context object.
5255 * @pring: Pointer to driver SLI ring object.
5256 * @iocbq: Pointer to iocb object.
5257 *
5258 * This function is called by the slow ring event handler
5259 * function when there is an ASYNC event iocb in the ring.
5260 * This function is called with no lock held.
5261 * Currently this function handles only temperature related
5262 * ASYNC events. The function decodes the temperature sensor
5263 * event message and posts events for the management applications.
5264 **/
James Smart98c9ea52007-10-27 13:37:33 -04005265static void
James Smart57127f12007-10-27 13:37:05 -04005266lpfc_sli_async_event_handler(struct lpfc_hba * phba,
5267 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
5268{
5269 IOCB_t *icmd;
5270 uint16_t evt_code;
5271 uint16_t temp;
5272 struct temp_event temp_event_data;
5273 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04005274 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04005275
5276 icmd = &iocbq->iocb;
5277 evt_code = icmd->un.asyncstat.evt_code;
5278 temp = icmd->ulpContext;
5279
5280 if ((evt_code != ASYNC_TEMP_WARN) &&
5281 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04005282 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04005283 lpfc_printf_log(phba,
5284 KERN_ERR,
5285 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04005286 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarta257bf92009-04-06 18:48:10 -04005287 " evt_code 0x%x \n"
5288 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
5289 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
5290 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
5291 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04005292 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04005293 icmd->un.asyncstat.evt_code,
5294 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
5295 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
5296 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
5297 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
5298
James Smart57127f12007-10-27 13:37:05 -04005299 return;
5300 }
5301 temp_event_data.data = (uint32_t)temp;
5302 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5303 if (evt_code == ASYNC_TEMP_WARN) {
5304 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
5305 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05005306 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04005307 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04005308 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04005309 "corrective action. temperature : %d Celsius\n",
5310 temp);
5311 }
5312 if (evt_code == ASYNC_TEMP_SAFE) {
5313 temp_event_data.event_code = LPFC_NORMAL_TEMP;
5314 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05005315 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04005316 LOG_TEMP,
5317 "0340 Adapter temperature is OK now. "
5318 "temperature : %d Celsius\n",
5319 temp);
5320 }
5321
5322 /* Send temperature change event to applications */
5323 shost = lpfc_shost_from_vport(phba->pport);
5324 fc_host_post_vendor_event(shost, fc_get_event_number(),
5325 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05005326 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04005327
5328}
5329
5330
James Smarte59058c2008-08-24 21:49:00 -04005331/**
James Smart3621a712009-04-06 18:47:14 -04005332 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04005333 * @phba: Pointer to HBA context object.
5334 *
5335 * lpfc_sli_setup sets up rings of the SLI interface with
5336 * number of iocbs per ring and iotags. This function is
5337 * called while driver attach to the HBA and before the
5338 * interrupts are enabled. So there is no need for locking.
5339 *
5340 * This function always returns 0.
5341 **/
dea31012005-04-17 16:05:31 -05005342int
5343lpfc_sli_setup(struct lpfc_hba *phba)
5344{
James Smarted957682007-06-17 19:56:37 -05005345 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05005346 struct lpfc_sli *psli = &phba->sli;
5347 struct lpfc_sli_ring *pring;
5348
5349 psli->num_rings = MAX_CONFIGURED_RINGS;
5350 psli->sli_flag = 0;
5351 psli->fcp_ring = LPFC_FCP_RING;
5352 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05005353 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05005354
James Bottomley604a3e32005-10-29 10:28:33 -05005355 psli->iocbq_lookup = NULL;
5356 psli->iocbq_lookup_len = 0;
5357 psli->last_iotag = 0;
5358
dea31012005-04-17 16:05:31 -05005359 for (i = 0; i < psli->num_rings; i++) {
5360 pring = &psli->ring[i];
5361 switch (i) {
5362 case LPFC_FCP_RING: /* ring 0 - FCP */
5363 /* numCiocb and numRiocb are used in config_port */
5364 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
5365 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
5366 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
5367 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
5368 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
5369 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05005370 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05005371 SLI3_IOCB_CMD_SIZE :
5372 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05005373 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05005374 SLI3_IOCB_RSP_SIZE :
5375 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05005376 pring->iotag_ctr = 0;
5377 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05005378 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05005379 pring->fast_iotag = pring->iotag_max;
5380 pring->num_mask = 0;
5381 break;
James Smarta4bc3372006-12-02 13:34:16 -05005382 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05005383 /* numCiocb and numRiocb are used in config_port */
5384 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
5385 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05005386 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05005387 SLI3_IOCB_CMD_SIZE :
5388 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05005389 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05005390 SLI3_IOCB_RSP_SIZE :
5391 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05005392 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05005393 pring->num_mask = 0;
5394 break;
5395 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
5396 /* numCiocb and numRiocb are used in config_port */
5397 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
5398 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05005399 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05005400 SLI3_IOCB_CMD_SIZE :
5401 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05005402 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05005403 SLI3_IOCB_RSP_SIZE :
5404 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05005405 pring->fast_iotag = 0;
5406 pring->iotag_ctr = 0;
5407 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04005408 pring->lpfc_sli_rcv_async_status =
5409 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05005410 pring->num_mask = 4;
5411 pring->prt[0].profile = 0; /* Mask 0 */
5412 pring->prt[0].rctl = FC_ELS_REQ;
5413 pring->prt[0].type = FC_ELS_DATA;
5414 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05005415 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05005416 pring->prt[1].profile = 0; /* Mask 1 */
5417 pring->prt[1].rctl = FC_ELS_RSP;
5418 pring->prt[1].type = FC_ELS_DATA;
5419 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05005420 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05005421 pring->prt[2].profile = 0; /* Mask 2 */
5422 /* NameServer Inquiry */
5423 pring->prt[2].rctl = FC_UNSOL_CTL;
5424 /* NameServer */
5425 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
5426 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05005427 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05005428 pring->prt[3].profile = 0; /* Mask 3 */
5429 /* NameServer response */
5430 pring->prt[3].rctl = FC_SOL_CTL;
5431 /* NameServer */
5432 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
5433 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05005434 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05005435 break;
5436 }
James Smarted957682007-06-17 19:56:37 -05005437 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05005438 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05005439 }
James Smarted957682007-06-17 19:56:37 -05005440 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05005441 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04005442 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
5443 "SLI2 SLIM Data: x%x x%lx\n",
5444 phba->brd_no, totiocbsize,
5445 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05005446 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05005447 if (phba->cfg_multi_ring_support == 2)
5448 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05005449
5450 return 0;
5451}
5452
James Smarte59058c2008-08-24 21:49:00 -04005453/**
James Smart3621a712009-04-06 18:47:14 -04005454 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04005455 * @phba: Pointer to HBA context object.
5456 *
5457 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
5458 * ring. This function also initializes ring indices of each ring.
5459 * This function is called during the initialization of the SLI
5460 * interface of an HBA.
5461 * This function is called with no lock held and always returns
5462 * 1.
5463 **/
dea31012005-04-17 16:05:31 -05005464int
James Smart2e0fef82007-06-17 19:56:36 -05005465lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05005466{
5467 struct lpfc_sli *psli;
5468 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05005469 int i;
dea31012005-04-17 16:05:31 -05005470
5471 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05005472 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005473 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05005474 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05005475 /* Initialize list headers for txq and txcmplq as double linked lists */
5476 for (i = 0; i < psli->num_rings; i++) {
5477 pring = &psli->ring[i];
5478 pring->ringno = i;
5479 pring->next_cmdidx = 0;
5480 pring->local_getidx = 0;
5481 pring->cmdidx = 0;
5482 INIT_LIST_HEAD(&pring->txq);
5483 INIT_LIST_HEAD(&pring->txcmplq);
5484 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05005485 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05005486 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05005487 }
James Smart2e0fef82007-06-17 19:56:36 -05005488 spin_unlock_irq(&phba->hbalock);
5489 return 1;
dea31012005-04-17 16:05:31 -05005490}
5491
James Smarte59058c2008-08-24 21:49:00 -04005492/**
James Smart3621a712009-04-06 18:47:14 -04005493 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04005494 * @vport: Pointer to virtual port object.
5495 *
5496 * lpfc_sli_host_down is called to clean up the resources
5497 * associated with a vport before destroying virtual
5498 * port data structures.
5499 * This function does following operations:
5500 * - Free discovery resources associated with this virtual
5501 * port.
5502 * - Free iocbs associated with this virtual port in
5503 * the txq.
5504 * - Send abort for all iocb commands associated with this
5505 * vport in txcmplq.
5506 *
5507 * This function is called with no lock held and always returns 1.
5508 **/
dea31012005-04-17 16:05:31 -05005509int
James Smart92d7f7b2007-06-17 19:56:38 -05005510lpfc_sli_host_down(struct lpfc_vport *vport)
5511{
James Smart858c9f62007-06-17 19:56:39 -05005512 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05005513 struct lpfc_hba *phba = vport->phba;
5514 struct lpfc_sli *psli = &phba->sli;
5515 struct lpfc_sli_ring *pring;
5516 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05005517 int i;
5518 unsigned long flags = 0;
5519 uint16_t prev_pring_flag;
5520
5521 lpfc_cleanup_discovery_resources(vport);
5522
5523 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05005524 for (i = 0; i < psli->num_rings; i++) {
5525 pring = &psli->ring[i];
5526 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04005527 /* Only slow rings */
5528 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05005529 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04005530 /* Set the lpfc data pending flag */
5531 set_bit(LPFC_DATA_READY, &phba->data_flags);
5532 }
James Smart92d7f7b2007-06-17 19:56:38 -05005533 /*
5534 * Error everything on the txq since these iocbs have not been
5535 * given to the FW yet.
5536 */
James Smart92d7f7b2007-06-17 19:56:38 -05005537 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5538 if (iocb->vport != vport)
5539 continue;
James Smart858c9f62007-06-17 19:56:39 -05005540 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05005541 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05005542 }
5543
5544 /* Next issue ABTS for everything on the txcmplq */
5545 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
5546 list) {
5547 if (iocb->vport != vport)
5548 continue;
5549 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
5550 }
5551
5552 pring->flag = prev_pring_flag;
5553 }
5554
5555 spin_unlock_irqrestore(&phba->hbalock, flags);
5556
James Smarta257bf92009-04-06 18:48:10 -04005557 /* Cancel all the IOCBs from the completions list */
5558 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5559 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05005560 return 1;
5561}
5562
James Smarte59058c2008-08-24 21:49:00 -04005563/**
James Smart3621a712009-04-06 18:47:14 -04005564 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04005565 * @phba: Pointer to HBA context object.
5566 *
5567 * This function cleans up all iocb, buffers, mailbox commands
5568 * while shutting down the HBA. This function is called with no
5569 * lock held and always returns 1.
5570 * This function does the following to cleanup driver resources:
5571 * - Free discovery resources for each virtual port
5572 * - Cleanup any pending fabric iocbs
5573 * - Iterate through the iocb txq and free each entry
5574 * in the list.
5575 * - Free up any buffer posted to the HBA
5576 * - Free mailbox commands in the mailbox queue.
5577 **/
James Smart92d7f7b2007-06-17 19:56:38 -05005578int
James Smart2e0fef82007-06-17 19:56:36 -05005579lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05005580{
James Smart2534ba72007-04-25 09:52:20 -04005581 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05005582 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005583 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05005584 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05005585 LPFC_MBOXQ_t *pmb;
dea31012005-04-17 16:05:31 -05005586 int i;
5587 unsigned long flags = 0;
5588
dea31012005-04-17 16:05:31 -05005589 lpfc_hba_down_prep(phba);
5590
James Smart92d7f7b2007-06-17 19:56:38 -05005591 lpfc_fabric_abort_hba(phba);
5592
James Smart2e0fef82007-06-17 19:56:36 -05005593 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05005594 for (i = 0; i < psli->num_rings; i++) {
5595 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04005596 /* Only slow rings */
5597 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05005598 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04005599 /* Set the lpfc data pending flag */
5600 set_bit(LPFC_DATA_READY, &phba->data_flags);
5601 }
dea31012005-04-17 16:05:31 -05005602
5603 /*
5604 * Error everything on the txq since these iocbs have not been
5605 * given to the FW yet.
5606 */
James Smart2534ba72007-04-25 09:52:20 -04005607 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05005608 pring->txq_cnt = 0;
5609
dea31012005-04-17 16:05:31 -05005610 }
James Smart2e0fef82007-06-17 19:56:36 -05005611 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05005612
James Smarta257bf92009-04-06 18:48:10 -04005613 /* Cancel all the IOCBs from the completions list */
5614 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5615 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04005616
James Smart0ff10d42008-01-11 01:52:36 -05005617 spin_lock_irqsave(&phba->hbalock, flags);
5618 list_splice_init(&phba->elsbuf, &completions);
5619 phba->elsbuf_cnt = 0;
5620 phba->elsbuf_prev_cnt = 0;
5621 spin_unlock_irqrestore(&phba->hbalock, flags);
5622
5623 while (!list_empty(&completions)) {
5624 list_remove_head(&completions, buf_ptr,
5625 struct lpfc_dmabuf, list);
5626 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
5627 kfree(buf_ptr);
5628 }
5629
dea31012005-04-17 16:05:31 -05005630 /* Return any active mbox cmds */
5631 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05005632
James Smartda0436e2009-05-22 14:51:39 -04005633 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05005634 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04005635 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05005636
James Smartda0436e2009-05-22 14:51:39 -04005637 return 1;
5638}
James Smart92d7f7b2007-06-17 19:56:38 -05005639
James Smartda0436e2009-05-22 14:51:39 -04005640/**
5641 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
5642 * @phba: Pointer to HBA context object.
5643 *
5644 * This function cleans up all queues, iocb, buffers, mailbox commands while
5645 * shutting down the SLI4 HBA FCoE function. This function is called with no
5646 * lock held and always returns 1.
5647 *
5648 * This function does the following to cleanup driver FCoE function resources:
5649 * - Free discovery resources for each virtual port
5650 * - Cleanup any pending fabric iocbs
5651 * - Iterate through the iocb txq and free each entry in the list.
5652 * - Free up any buffer posted to the HBA.
5653 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
5654 * - Free mailbox commands in the mailbox queue.
5655 **/
5656int
5657lpfc_sli4_hba_down(struct lpfc_hba *phba)
5658{
5659 /* Stop the SLI4 device port */
5660 lpfc_stop_port(phba);
5661
5662 /* Tear down the queues in the HBA */
5663 lpfc_sli4_queue_unset(phba);
5664
5665 /* unregister default FCFI from the HBA */
5666 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
5667
dea31012005-04-17 16:05:31 -05005668 return 1;
5669}
5670
James Smarte59058c2008-08-24 21:49:00 -04005671/**
James Smart3621a712009-04-06 18:47:14 -04005672 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04005673 * @srcp: Source memory pointer.
5674 * @destp: Destination memory pointer.
5675 * @cnt: Number of words required to be copied.
5676 *
5677 * This function is used for copying data between driver memory
5678 * and the SLI memory. This function also changes the endianness
5679 * of each word if native endianness is different from SLI
5680 * endianness. This function can be called with or without
5681 * lock.
5682 **/
dea31012005-04-17 16:05:31 -05005683void
5684lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
5685{
5686 uint32_t *src = srcp;
5687 uint32_t *dest = destp;
5688 uint32_t ldata;
5689 int i;
5690
5691 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
5692 ldata = *src;
5693 ldata = le32_to_cpu(ldata);
5694 *dest = ldata;
5695 src++;
5696 dest++;
5697 }
5698}
5699
James Smarte59058c2008-08-24 21:49:00 -04005700
5701/**
James Smart3621a712009-04-06 18:47:14 -04005702 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04005703 * @phba: Pointer to HBA context object.
5704 * @pring: Pointer to driver SLI ring object.
5705 * @mp: Pointer to driver buffer object.
5706 *
5707 * This function is called with no lock held.
5708 * It always return zero after adding the buffer to the postbufq
5709 * buffer list.
5710 **/
dea31012005-04-17 16:05:31 -05005711int
James Smart2e0fef82007-06-17 19:56:36 -05005712lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5713 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05005714{
5715 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
5716 later */
James Smart2e0fef82007-06-17 19:56:36 -05005717 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005718 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05005719 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05005720 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005721 return 0;
5722}
5723
James Smarte59058c2008-08-24 21:49:00 -04005724/**
James Smart3621a712009-04-06 18:47:14 -04005725 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04005726 * @phba: Pointer to HBA context object.
5727 *
5728 * When HBQ is enabled, buffers are searched based on tags. This function
5729 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
5730 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
5731 * does not conflict with tags of buffer posted for unsolicited events.
5732 * The function returns the allocated tag. The function is called with
5733 * no locks held.
5734 **/
James Smart76bb24e2007-10-27 13:38:00 -04005735uint32_t
5736lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
5737{
5738 spin_lock_irq(&phba->hbalock);
5739 phba->buffer_tag_count++;
5740 /*
5741 * Always set the QUE_BUFTAG_BIT to distiguish between
5742 * a tag assigned by HBQ.
5743 */
5744 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
5745 spin_unlock_irq(&phba->hbalock);
5746 return phba->buffer_tag_count;
5747}
5748
James Smarte59058c2008-08-24 21:49:00 -04005749/**
James Smart3621a712009-04-06 18:47:14 -04005750 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04005751 * @phba: Pointer to HBA context object.
5752 * @pring: Pointer to driver SLI ring object.
5753 * @tag: Buffer tag.
5754 *
5755 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
5756 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
5757 * iocb is posted to the response ring with the tag of the buffer.
5758 * This function searches the pring->postbufq list using the tag
5759 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
5760 * iocb. If the buffer is found then lpfc_dmabuf object of the
5761 * buffer is returned to the caller else NULL is returned.
5762 * This function is called with no lock held.
5763 **/
James Smart76bb24e2007-10-27 13:38:00 -04005764struct lpfc_dmabuf *
5765lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5766 uint32_t tag)
5767{
5768 struct lpfc_dmabuf *mp, *next_mp;
5769 struct list_head *slp = &pring->postbufq;
5770
5771 /* Search postbufq, from the begining, looking for a match on tag */
5772 spin_lock_irq(&phba->hbalock);
5773 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
5774 if (mp->buffer_tag == tag) {
5775 list_del_init(&mp->list);
5776 pring->postbufq_cnt--;
5777 spin_unlock_irq(&phba->hbalock);
5778 return mp;
5779 }
5780 }
5781
5782 spin_unlock_irq(&phba->hbalock);
5783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04005784 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04005785 "ring %d Data x%lx x%p x%p x%x\n",
5786 pring->ringno, (unsigned long) tag,
5787 slp->next, slp->prev, pring->postbufq_cnt);
5788
5789 return NULL;
5790}
dea31012005-04-17 16:05:31 -05005791
James Smarte59058c2008-08-24 21:49:00 -04005792/**
James Smart3621a712009-04-06 18:47:14 -04005793 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04005794 * @phba: Pointer to HBA context object.
5795 * @pring: Pointer to driver SLI ring object.
5796 * @phys: DMA address of the buffer.
5797 *
5798 * This function searches the buffer list using the dma_address
5799 * of unsolicited event to find the driver's lpfc_dmabuf object
5800 * corresponding to the dma_address. The function returns the
5801 * lpfc_dmabuf object if a buffer is found else it returns NULL.
5802 * This function is called by the ct and els unsolicited event
5803 * handlers to get the buffer associated with the unsolicited
5804 * event.
5805 *
5806 * This function is called with no lock held.
5807 **/
dea31012005-04-17 16:05:31 -05005808struct lpfc_dmabuf *
5809lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5810 dma_addr_t phys)
5811{
5812 struct lpfc_dmabuf *mp, *next_mp;
5813 struct list_head *slp = &pring->postbufq;
5814
5815 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05005816 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005817 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
5818 if (mp->phys == phys) {
5819 list_del_init(&mp->list);
5820 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05005821 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005822 return mp;
5823 }
5824 }
5825
James Smart2e0fef82007-06-17 19:56:36 -05005826 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005828 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05005829 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005830 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05005831 slp->next, slp->prev, pring->postbufq_cnt);
5832 return NULL;
5833}
5834
James Smarte59058c2008-08-24 21:49:00 -04005835/**
James Smart3621a712009-04-06 18:47:14 -04005836 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04005837 * @phba: Pointer to HBA context object.
5838 * @cmdiocb: Pointer to driver command iocb object.
5839 * @rspiocb: Pointer to driver response iocb object.
5840 *
5841 * This function is the completion handler for the abort iocbs for
5842 * ELS commands. This function is called from the ELS ring event
5843 * handler with no lock held. This function frees memory resources
5844 * associated with the abort iocb.
5845 **/
dea31012005-04-17 16:05:31 -05005846static void
James Smart2e0fef82007-06-17 19:56:36 -05005847lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5848 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05005849{
James Smart2e0fef82007-06-17 19:56:36 -05005850 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04005851 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05005852 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04005853 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5854
5855 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04005856
5857 if (irsp->ulpStatus) {
5858 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
5859 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
5860
James Smart2e0fef82007-06-17 19:56:36 -05005861 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04005862 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
5863 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
5864
James Smart92d7f7b2007-06-17 19:56:38 -05005865 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005866 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05005867 "with tag %x context %x, abort status %x, "
5868 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04005869 abort_iocb, abort_iotag, abort_context,
5870 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04005871
5872 /*
James Smart58da1ff2008-04-07 10:15:56 -04005873 * If the iocb is not found in Firmware queue the iocb
5874 * might have completed already. Do not free it again.
5875 */
James Smart9b379602008-04-07 10:16:00 -04005876 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04005877 spin_unlock_irq(&phba->hbalock);
5878 lpfc_sli_release_iocbq(phba, cmdiocb);
5879 return;
5880 }
5881 /*
James Smart2680eea2007-04-25 09:52:55 -04005882 * make sure we have the right iocbq before taking it
5883 * off the txcmplq and try to call completion routine.
5884 */
James Smart2e0fef82007-06-17 19:56:36 -05005885 if (!abort_iocb ||
5886 abort_iocb->iocb.ulpContext != abort_context ||
5887 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
5888 spin_unlock_irq(&phba->hbalock);
5889 else {
James Smart92d7f7b2007-06-17 19:56:38 -05005890 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04005891 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05005892 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04005893
James Smart0ff10d42008-01-11 01:52:36 -05005894 /* Firmware could still be in progress of DMAing
5895 * payload, so don't free data buffer till after
5896 * a hbeat.
5897 */
5898 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
5899
James Smart92d7f7b2007-06-17 19:56:38 -05005900 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
5901 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
5902 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
5903 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04005904 }
5905 }
5906
James Bottomley604a3e32005-10-29 10:28:33 -05005907 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05005908 return;
5909}
5910
James Smarte59058c2008-08-24 21:49:00 -04005911/**
James Smart3621a712009-04-06 18:47:14 -04005912 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04005913 * @phba: Pointer to HBA context object.
5914 * @cmdiocb: Pointer to driver command iocb object.
5915 * @rspiocb: Pointer to driver response iocb object.
5916 *
5917 * The function is called from SLI ring event handler with no
5918 * lock held. This function is the completion handler for ELS commands
5919 * which are aborted. The function frees memory resources used for
5920 * the aborted ELS commands.
5921 **/
James Smart92d7f7b2007-06-17 19:56:38 -05005922static void
5923lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5924 struct lpfc_iocbq *rspiocb)
5925{
5926 IOCB_t *irsp = &rspiocb->iocb;
5927
5928 /* ELS cmd tag <ulpIoTag> completes */
5929 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04005930 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05005931 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005932 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05005933 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05005934 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
5935 lpfc_ct_free_iocb(phba, cmdiocb);
5936 else
5937 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05005938 return;
5939}
5940
James Smarte59058c2008-08-24 21:49:00 -04005941/**
James Smart3621a712009-04-06 18:47:14 -04005942 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04005943 * @phba: Pointer to HBA context object.
5944 * @pring: Pointer to driver SLI ring object.
5945 * @cmdiocb: Pointer to driver command iocb object.
5946 *
5947 * This function issues an abort iocb for the provided command
5948 * iocb. This function is called with hbalock held.
5949 * The function returns 0 when it fails due to memory allocation
5950 * failure or when the command iocb is an abort request.
5951 **/
dea31012005-04-17 16:05:31 -05005952int
James Smart2e0fef82007-06-17 19:56:36 -05005953lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5954 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05005955{
James Smart2e0fef82007-06-17 19:56:36 -05005956 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005957 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05005958 IOCB_t *icmd = NULL;
5959 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04005960 int retval = IOCB_ERROR;
5961
James Smart92d7f7b2007-06-17 19:56:38 -05005962 /*
5963 * There are certain command types we don't want to abort. And we
5964 * don't want to abort commands that are already in the process of
5965 * being aborted.
James Smart07951072007-04-25 09:51:38 -04005966 */
5967 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005968 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05005969 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5970 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04005971 return 0;
5972
James Smart858c9f62007-06-17 19:56:39 -05005973 /* If we're unloading, don't abort iocb on the ELS ring, but change the
5974 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04005975 */
James Smart858c9f62007-06-17 19:56:39 -05005976 if ((vport->load_flag & FC_UNLOADING) &&
5977 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005978 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
5979 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
5980 else
5981 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04005982 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05005983 }
dea31012005-04-17 16:05:31 -05005984
5985 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05005986 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05005987 if (abtsiocbp == NULL)
5988 return 0;
dea31012005-04-17 16:05:31 -05005989
James Smart07951072007-04-25 09:51:38 -04005990 /* This signals the response to set the correct status
5991 * before calling the completion handler.
5992 */
5993 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
5994
dea31012005-04-17 16:05:31 -05005995 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04005996 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
5997 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04005998 if (phba->sli_rev == LPFC_SLI_REV4)
5999 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
6000 else
6001 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05006002 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04006003 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05006004
James Smart2e0fef82007-06-17 19:56:36 -05006005 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04006006 iabt->ulpCommand = CMD_ABORT_XRI_CN;
6007 else
6008 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
6009
6010 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04006011
James Smarte8b62012007-08-02 11:10:09 -04006012 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
6013 "0339 Abort xri x%x, original iotag x%x, "
6014 "abort cmd iotag x%x\n",
6015 iabt->un.acxri.abortContextTag,
6016 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04006017 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04006018
James Smartd7c255b2008-08-24 21:50:00 -04006019 if (retval)
6020 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04006021abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05006022 /*
6023 * Caller to this routine should check for IOCB_ERROR
6024 * and handle it properly. This routine no longer removes
6025 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04006026 */
James Smart2e0fef82007-06-17 19:56:36 -05006027 return retval;
dea31012005-04-17 16:05:31 -05006028}
6029
James Smarte59058c2008-08-24 21:49:00 -04006030/**
James Smart3621a712009-04-06 18:47:14 -04006031 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04006032 * @iocbq: Pointer to driver iocb object.
6033 * @vport: Pointer to driver virtual port object.
6034 * @tgt_id: SCSI ID of the target.
6035 * @lun_id: LUN ID of the scsi device.
6036 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
6037 *
James Smart3621a712009-04-06 18:47:14 -04006038 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04006039 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
6040 * 0 if the filtering criteria is met for the given iocb and will return
6041 * 1 if the filtering criteria is not met.
6042 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
6043 * given iocb is for the SCSI device specified by vport, tgt_id and
6044 * lun_id parameter.
6045 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
6046 * given iocb is for the SCSI target specified by vport and tgt_id
6047 * parameters.
6048 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
6049 * given iocb is for the SCSI host associated with the given vport.
6050 * This function is called with no locks held.
6051 **/
dea31012005-04-17 16:05:31 -05006052static int
James Smart51ef4c22007-08-02 11:10:31 -04006053lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
6054 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006055 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05006056{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006057 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05006058 int rc = 1;
6059
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006060 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
6061 return rc;
6062
James Smart51ef4c22007-08-02 11:10:31 -04006063 if (iocbq->vport != vport)
6064 return rc;
6065
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006066 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006067
James Smart495a7142008-06-14 22:52:59 -04006068 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05006069 return rc;
6070
6071 switch (ctx_cmd) {
6072 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04006073 if ((lpfc_cmd->rdata->pnode) &&
6074 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
6075 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05006076 rc = 0;
6077 break;
6078 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04006079 if ((lpfc_cmd->rdata->pnode) &&
6080 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05006081 rc = 0;
6082 break;
dea31012005-04-17 16:05:31 -05006083 case LPFC_CTX_HOST:
6084 rc = 0;
6085 break;
6086 default:
6087 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07006088 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05006089 break;
6090 }
6091
6092 return rc;
6093}
6094
James Smarte59058c2008-08-24 21:49:00 -04006095/**
James Smart3621a712009-04-06 18:47:14 -04006096 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04006097 * @vport: Pointer to virtual port.
6098 * @tgt_id: SCSI ID of the target.
6099 * @lun_id: LUN ID of the scsi device.
6100 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
6101 *
6102 * This function returns number of FCP commands pending for the vport.
6103 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
6104 * commands pending on the vport associated with SCSI device specified
6105 * by tgt_id and lun_id parameters.
6106 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
6107 * commands pending on the vport associated with SCSI target specified
6108 * by tgt_id parameter.
6109 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
6110 * commands pending on the vport.
6111 * This function returns the number of iocbs which satisfy the filter.
6112 * This function is called without any lock held.
6113 **/
dea31012005-04-17 16:05:31 -05006114int
James Smart51ef4c22007-08-02 11:10:31 -04006115lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
6116 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05006117{
James Smart51ef4c22007-08-02 11:10:31 -04006118 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006119 struct lpfc_iocbq *iocbq;
6120 int sum, i;
dea31012005-04-17 16:05:31 -05006121
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006122 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
6123 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05006124
James Smart51ef4c22007-08-02 11:10:31 -04006125 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
6126 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006127 sum++;
dea31012005-04-17 16:05:31 -05006128 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006129
dea31012005-04-17 16:05:31 -05006130 return sum;
6131}
6132
James Smarte59058c2008-08-24 21:49:00 -04006133/**
James Smart3621a712009-04-06 18:47:14 -04006134 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04006135 * @phba: Pointer to HBA context object
6136 * @cmdiocb: Pointer to command iocb object.
6137 * @rspiocb: Pointer to response iocb object.
6138 *
6139 * This function is called when an aborted FCP iocb completes. This
6140 * function is called by the ring event handler with no lock held.
6141 * This function frees the iocb.
6142 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04006143void
James Smart2e0fef82007-06-17 19:56:36 -05006144lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6145 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04006146{
James Bottomley604a3e32005-10-29 10:28:33 -05006147 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04006148 return;
6149}
6150
James Smarte59058c2008-08-24 21:49:00 -04006151/**
James Smart3621a712009-04-06 18:47:14 -04006152 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04006153 * @vport: Pointer to virtual port.
6154 * @pring: Pointer to driver SLI ring object.
6155 * @tgt_id: SCSI ID of the target.
6156 * @lun_id: LUN ID of the scsi device.
6157 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
6158 *
6159 * This function sends an abort command for every SCSI command
6160 * associated with the given virtual port pending on the ring
6161 * filtered by lpfc_sli_validate_fcp_iocb function.
6162 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
6163 * FCP iocbs associated with lun specified by tgt_id and lun_id
6164 * parameters
6165 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
6166 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
6167 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
6168 * FCP iocbs associated with virtual port.
6169 * This function returns number of iocbs it failed to abort.
6170 * This function is called with no locks held.
6171 **/
dea31012005-04-17 16:05:31 -05006172int
James Smart51ef4c22007-08-02 11:10:31 -04006173lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
6174 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05006175{
James Smart51ef4c22007-08-02 11:10:31 -04006176 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006177 struct lpfc_iocbq *iocbq;
6178 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05006179 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05006180 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006181 int i;
dea31012005-04-17 16:05:31 -05006182
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006183 for (i = 1; i <= phba->sli.last_iotag; i++) {
6184 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05006185
James Smart51ef4c22007-08-02 11:10:31 -04006186 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05006187 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05006188 continue;
6189
6190 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006191 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05006192 if (abtsiocb == NULL) {
6193 errcnt++;
6194 continue;
6195 }
dea31012005-04-17 16:05:31 -05006196
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006197 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05006198 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
6199 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04006200 if (phba->sli_rev == LPFC_SLI_REV4)
6201 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
6202 else
6203 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05006204 abtsiocb->iocb.ulpLe = 1;
6205 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05006206 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05006207
James Smart2e0fef82007-06-17 19:56:36 -05006208 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05006209 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
6210 else
6211 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
6212
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04006213 /* Setup callback routine and issue the command. */
6214 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04006215 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
6216 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05006217 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05006218 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05006219 errcnt++;
6220 continue;
6221 }
6222 }
6223
6224 return errcnt;
6225}
6226
James Smarte59058c2008-08-24 21:49:00 -04006227/**
James Smart3621a712009-04-06 18:47:14 -04006228 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04006229 * @phba: Pointer to HBA context object.
6230 * @cmdiocbq: Pointer to command iocb.
6231 * @rspiocbq: Pointer to response iocb.
6232 *
6233 * This function is the completion handler for iocbs issued using
6234 * lpfc_sli_issue_iocb_wait function. This function is called by the
6235 * ring event handler function without any lock held. This function
6236 * can be called from both worker thread context and interrupt
6237 * context. This function also can be called from other thread which
6238 * cleans up the SLI layer objects.
6239 * This function copy the contents of the response iocb to the
6240 * response iocb memory object provided by the caller of
6241 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
6242 * sleeps for the iocb completion.
6243 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006244static void
6245lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
6246 struct lpfc_iocbq *cmdiocbq,
6247 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05006248{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006249 wait_queue_head_t *pdone_q;
6250 unsigned long iflags;
dea31012005-04-17 16:05:31 -05006251
James Smart2e0fef82007-06-17 19:56:36 -05006252 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006253 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
6254 if (cmdiocbq->context2 && rspiocbq)
6255 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
6256 &rspiocbq->iocb, sizeof(IOCB_t));
6257
6258 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006259 if (pdone_q)
6260 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05006261 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05006262 return;
6263}
6264
James Smarte59058c2008-08-24 21:49:00 -04006265/**
James Smart3621a712009-04-06 18:47:14 -04006266 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04006267 * @phba: Pointer to HBA context object..
6268 * @pring: Pointer to sli ring.
6269 * @piocb: Pointer to command iocb.
6270 * @prspiocbq: Pointer to response iocb.
6271 * @timeout: Timeout in number of seconds.
6272 *
6273 * This function issues the iocb to firmware and waits for the
6274 * iocb to complete. If the iocb command is not
6275 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
6276 * Caller should not free the iocb resources if this function
6277 * returns IOCB_TIMEDOUT.
6278 * The function waits for the iocb completion using an
6279 * non-interruptible wait.
6280 * This function will sleep while waiting for iocb completion.
6281 * So, this function should not be called from any context which
6282 * does not allow sleeping. Due to the same reason, this function
6283 * cannot be called with interrupt disabled.
6284 * This function assumes that the iocb completions occur while
6285 * this function sleep. So, this function cannot be called from
6286 * the thread which process iocb completion for this ring.
6287 * This function clears the iocb_flag of the iocb object before
6288 * issuing the iocb and the iocb completion handler sets this
6289 * flag and wakes this thread when the iocb completes.
6290 * The contents of the response iocb will be copied to prspiocbq
6291 * by the completion handler when the command completes.
6292 * This function returns IOCB_SUCCESS when success.
6293 * This function is called with no lock held.
6294 **/
dea31012005-04-17 16:05:31 -05006295int
James Smart2e0fef82007-06-17 19:56:36 -05006296lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04006297 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05006298 struct lpfc_iocbq *piocb,
6299 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006300 uint32_t timeout)
dea31012005-04-17 16:05:31 -05006301{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08006302 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006303 long timeleft, timeout_req = 0;
6304 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05006305 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05006306
6307 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006308 * If the caller has provided a response iocbq buffer, then context2
6309 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05006310 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006311 if (prspiocbq) {
6312 if (piocb->context2)
6313 return IOCB_ERROR;
6314 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05006315 }
6316
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006317 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
6318 piocb->context_un.wait_queue = &done_q;
6319 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05006320
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05006321 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6322 creg_val = readl(phba->HCregaddr);
6323 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
6324 writel(creg_val, phba->HCregaddr);
6325 readl(phba->HCregaddr); /* flush */
6326 }
6327
James Smartda0436e2009-05-22 14:51:39 -04006328 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006329 if (retval == IOCB_SUCCESS) {
6330 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006331 timeleft = wait_event_timeout(done_q,
6332 piocb->iocb_flag & LPFC_IO_WAKE,
6333 timeout_req);
dea31012005-04-17 16:05:31 -05006334
James Smart7054a602007-04-25 09:52:34 -04006335 if (piocb->iocb_flag & LPFC_IO_WAKE) {
6336 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006337 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04006338 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006339 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006340 "0338 IOCB wait timeout error - no "
6341 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006342 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04006343 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006344 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006345 "0330 IOCB wake NOT set, "
6346 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006347 timeout, (timeleft / jiffies));
6348 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05006349 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006350 } else {
6351 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04006352 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006353 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006354 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05006355 }
6356
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05006357 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6358 creg_val = readl(phba->HCregaddr);
6359 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
6360 writel(creg_val, phba->HCregaddr);
6361 readl(phba->HCregaddr); /* flush */
6362 }
6363
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006364 if (prspiocbq)
6365 piocb->context2 = NULL;
6366
6367 piocb->context_un.wait_queue = NULL;
6368 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05006369 return retval;
6370}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04006371
James Smarte59058c2008-08-24 21:49:00 -04006372/**
James Smart3621a712009-04-06 18:47:14 -04006373 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04006374 * @phba: Pointer to HBA context object.
6375 * @pmboxq: Pointer to driver mailbox object.
6376 * @timeout: Timeout in number of seconds.
6377 *
6378 * This function issues the mailbox to firmware and waits for the
6379 * mailbox command to complete. If the mailbox command is not
6380 * completed within timeout seconds, it returns MBX_TIMEOUT.
6381 * The function waits for the mailbox completion using an
6382 * interruptible wait. If the thread is woken up due to a
6383 * signal, MBX_TIMEOUT error is returned to the caller. Caller
6384 * should not free the mailbox resources, if this function returns
6385 * MBX_TIMEOUT.
6386 * This function will sleep while waiting for mailbox completion.
6387 * So, this function should not be called from any context which
6388 * does not allow sleeping. Due to the same reason, this function
6389 * cannot be called with interrupt disabled.
6390 * This function assumes that the mailbox completion occurs while
6391 * this function sleep. So, this function cannot be called from
6392 * the worker thread which processes mailbox completion.
6393 * This function is called in the context of HBA management
6394 * applications.
6395 * This function returns MBX_SUCCESS when successful.
6396 * This function is called with no lock held.
6397 **/
dea31012005-04-17 16:05:31 -05006398int
James Smart2e0fef82007-06-17 19:56:36 -05006399lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05006400 uint32_t timeout)
6401{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08006402 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05006403 int retval;
James Smart858c9f62007-06-17 19:56:39 -05006404 unsigned long flag;
dea31012005-04-17 16:05:31 -05006405
6406 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04006407 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05006408 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006409
James Smart495a7142008-06-14 22:52:59 -04006410 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05006411 /* setup wake call as IOCB callback */
6412 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
6413 /* setup context field to pass wait_queue pointer to wake function */
6414 pmboxq->context1 = &done_q;
6415
dea31012005-04-17 16:05:31 -05006416 /* now issue the command */
6417 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
6418
6419 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04006420 wait_event_interruptible_timeout(done_q,
6421 pmboxq->mbox_flag & LPFC_MBX_WAKE,
6422 timeout * HZ);
6423
James Smart858c9f62007-06-17 19:56:39 -05006424 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05006425 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04006426 /*
6427 * if LPFC_MBX_WAKE flag is set the mailbox is completed
6428 * else do not free the resources.
6429 */
6430 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05006431 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05006432 else {
James Smart7054a602007-04-25 09:52:34 -04006433 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05006434 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6435 }
6436 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05006437 }
6438
dea31012005-04-17 16:05:31 -05006439 return retval;
6440}
6441
James Smarte59058c2008-08-24 21:49:00 -04006442/**
James Smart3772a992009-05-22 14:50:54 -04006443 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04006444 * @phba: Pointer to HBA context.
6445 *
James Smart3772a992009-05-22 14:50:54 -04006446 * This function is called to shutdown the driver's mailbox sub-system.
6447 * It first marks the mailbox sub-system is in a block state to prevent
6448 * the asynchronous mailbox command from issued off the pending mailbox
6449 * command queue. If the mailbox command sub-system shutdown is due to
6450 * HBA error conditions such as EEH or ERATT, this routine shall invoke
6451 * the mailbox sub-system flush routine to forcefully bring down the
6452 * mailbox sub-system. Otherwise, if it is due to normal condition (such
6453 * as with offline or HBA function reset), this routine will wait for the
6454 * outstanding mailbox command to complete before invoking the mailbox
6455 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04006456 **/
James Smart3772a992009-05-22 14:50:54 -04006457void
6458lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04006459{
James Smart3772a992009-05-22 14:50:54 -04006460 struct lpfc_sli *psli = &phba->sli;
6461 uint8_t actcmd = MBX_HEARTBEAT;
6462 unsigned long timeout;
6463
6464 spin_lock_irq(&phba->hbalock);
6465 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6466 spin_unlock_irq(&phba->hbalock);
6467
6468 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6469 spin_lock_irq(&phba->hbalock);
6470 if (phba->sli.mbox_active)
6471 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
6472 spin_unlock_irq(&phba->hbalock);
6473 /* Determine how long we might wait for the active mailbox
6474 * command to be gracefully completed by firmware.
6475 */
6476 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
6477 1000) + jiffies;
6478 while (phba->sli.mbox_active) {
6479 /* Check active mailbox complete status every 2ms */
6480 msleep(2);
6481 if (time_after(jiffies, timeout))
6482 /* Timeout, let the mailbox flush routine to
6483 * forcefully release active mailbox command
6484 */
6485 break;
6486 }
6487 }
6488 lpfc_sli_mbox_sys_flush(phba);
6489}
6490
6491/**
6492 * lpfc_sli_eratt_read - read sli-3 error attention events
6493 * @phba: Pointer to HBA context.
6494 *
6495 * This function is called to read the SLI3 device error attention registers
6496 * for possible error attention events. The caller must hold the hostlock
6497 * with spin_lock_irq().
6498 *
6499 * This fucntion returns 1 when there is Error Attention in the Host Attention
6500 * Register and returns 0 otherwise.
6501 **/
6502static int
6503lpfc_sli_eratt_read(struct lpfc_hba *phba)
6504{
James Smarted957682007-06-17 19:56:37 -05006505 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04006506
James Smart3772a992009-05-22 14:50:54 -04006507 /* Read chip Host Attention (HA) register */
6508 ha_copy = readl(phba->HAregaddr);
6509 if (ha_copy & HA_ERATT) {
6510 /* Read host status register to retrieve error event */
6511 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04006512
James Smart3772a992009-05-22 14:50:54 -04006513 /* Check if there is a deferred error condition is active */
6514 if ((HS_FFER1 & phba->work_hs) &&
6515 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
6516 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
6517 spin_lock_irq(&phba->hbalock);
6518 phba->hba_flag |= DEFER_ERATT;
6519 spin_unlock_irq(&phba->hbalock);
6520 /* Clear all interrupt enable conditions */
6521 writel(0, phba->HCregaddr);
6522 readl(phba->HCregaddr);
6523 }
6524
6525 /* Set the driver HA work bitmap */
James Smarted957682007-06-17 19:56:37 -05006526 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04006527 phba->work_ha |= HA_ERATT;
6528 /* Indicate polling handles this ERATT */
6529 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smarted957682007-06-17 19:56:37 -05006530 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04006531 return 1;
James Smartb4c02652006-07-06 15:50:43 -04006532 }
James Smart3772a992009-05-22 14:50:54 -04006533 return 0;
James Smartb4c02652006-07-06 15:50:43 -04006534}
6535
James Smarte59058c2008-08-24 21:49:00 -04006536/**
James Smartda0436e2009-05-22 14:51:39 -04006537 * lpfc_sli4_eratt_read - read sli-4 error attention events
6538 * @phba: Pointer to HBA context.
6539 *
6540 * This function is called to read the SLI4 device error attention registers
6541 * for possible error attention events. The caller must hold the hostlock
6542 * with spin_lock_irq().
6543 *
6544 * This fucntion returns 1 when there is Error Attention in the Host Attention
6545 * Register and returns 0 otherwise.
6546 **/
6547static int
6548lpfc_sli4_eratt_read(struct lpfc_hba *phba)
6549{
6550 uint32_t uerr_sta_hi, uerr_sta_lo;
6551 uint32_t onlnreg0, onlnreg1;
6552
6553 /* For now, use the SLI4 device internal unrecoverable error
6554 * registers for error attention. This can be changed later.
6555 */
6556 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
6557 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
6558 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
6559 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
6560 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
6561 if (uerr_sta_lo || uerr_sta_hi) {
6562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6563 "1423 HBA Unrecoverable error: "
6564 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
6565 "online0_reg=0x%x, online1_reg=0x%x\n",
6566 uerr_sta_lo, uerr_sta_hi,
6567 onlnreg0, onlnreg1);
6568 /* TEMP: as the driver error recover logic is not
6569 * fully developed, we just log the error message
6570 * and the device error attention action is now
6571 * temporarily disabled.
6572 */
6573 return 0;
6574 phba->work_status[0] = uerr_sta_lo;
6575 phba->work_status[1] = uerr_sta_hi;
6576 spin_lock_irq(&phba->hbalock);
6577 /* Set the driver HA work bitmap */
6578 phba->work_ha |= HA_ERATT;
6579 /* Indicate polling handles this ERATT */
6580 phba->hba_flag |= HBA_ERATT_HANDLED;
6581 spin_unlock_irq(&phba->hbalock);
6582 return 1;
6583 }
6584 }
6585 return 0;
6586}
6587
6588/**
James Smart3621a712009-04-06 18:47:14 -04006589 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04006590 * @phba: Pointer to HBA context.
6591 *
James Smart3772a992009-05-22 14:50:54 -04006592 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04006593 * error attention register bit for error attention events.
6594 *
6595 * This fucntion returns 1 when there is Error Attention in the Host Attention
6596 * Register and returns 0 otherwise.
6597 **/
6598int
6599lpfc_sli_check_eratt(struct lpfc_hba *phba)
6600{
6601 uint32_t ha_copy;
6602
6603 /* If somebody is waiting to handle an eratt, don't process it
6604 * here. The brdkill function will do this.
6605 */
6606 if (phba->link_flag & LS_IGNORE_ERATT)
6607 return 0;
6608
6609 /* Check if interrupt handler handles this ERATT */
6610 spin_lock_irq(&phba->hbalock);
6611 if (phba->hba_flag & HBA_ERATT_HANDLED) {
6612 /* Interrupt handler has handled ERATT */
6613 spin_unlock_irq(&phba->hbalock);
6614 return 0;
6615 }
6616
James Smarta257bf92009-04-06 18:48:10 -04006617 /*
6618 * If there is deferred error attention, do not check for error
6619 * attention
6620 */
6621 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6622 spin_unlock_irq(&phba->hbalock);
6623 return 0;
6624 }
6625
James Smart3772a992009-05-22 14:50:54 -04006626 /* If PCI channel is offline, don't process it */
6627 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04006628 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04006629 return 0;
6630 }
6631
6632 switch (phba->sli_rev) {
6633 case LPFC_SLI_REV2:
6634 case LPFC_SLI_REV3:
6635 /* Read chip Host Attention (HA) register */
6636 ha_copy = lpfc_sli_eratt_read(phba);
6637 break;
James Smartda0436e2009-05-22 14:51:39 -04006638 case LPFC_SLI_REV4:
6639 /* Read devcie Uncoverable Error (UERR) registers */
6640 ha_copy = lpfc_sli4_eratt_read(phba);
6641 break;
James Smart3772a992009-05-22 14:50:54 -04006642 default:
6643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6644 "0299 Invalid SLI revision (%d)\n",
6645 phba->sli_rev);
6646 ha_copy = 0;
6647 break;
James Smart93996272008-08-24 21:50:30 -04006648 }
6649 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04006650
6651 return ha_copy;
6652}
6653
6654/**
6655 * lpfc_intr_state_check - Check device state for interrupt handling
6656 * @phba: Pointer to HBA context.
6657 *
6658 * This inline routine checks whether a device or its PCI slot is in a state
6659 * that the interrupt should be handled.
6660 *
6661 * This function returns 0 if the device or the PCI slot is in a state that
6662 * interrupt should be handled, otherwise -EIO.
6663 */
6664static inline int
6665lpfc_intr_state_check(struct lpfc_hba *phba)
6666{
6667 /* If the pci channel is offline, ignore all the interrupts */
6668 if (unlikely(pci_channel_offline(phba->pcidev)))
6669 return -EIO;
6670
6671 /* Update device level interrupt statistics */
6672 phba->sli.slistat.sli_intr++;
6673
6674 /* Ignore all interrupts during initialization. */
6675 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
6676 return -EIO;
6677
James Smart93996272008-08-24 21:50:30 -04006678 return 0;
6679}
6680
6681/**
James Smart3772a992009-05-22 14:50:54 -04006682 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04006683 * @irq: Interrupt number.
6684 * @dev_id: The device context pointer.
6685 *
James Smart93996272008-08-24 21:50:30 -04006686 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04006687 * service routine when device with SLI-3 interface spec is enabled with
6688 * MSI-X multi-message interrupt mode and there are slow-path events in
6689 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
6690 * interrupt mode, this function is called as part of the device-level
6691 * interrupt handler. When the PCI slot is in error recovery or the HBA
6692 * is undergoing initialization, the interrupt handler will not process
6693 * the interrupt. The link attention and ELS ring attention events are
6694 * handled by the worker thread. The interrupt handler signals the worker
6695 * thread and returns for these events. This function is called without
6696 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04006697 * structures.
6698 *
6699 * This function returns IRQ_HANDLED when interrupt is handled else it
6700 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04006701 **/
dea31012005-04-17 16:05:31 -05006702irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04006703lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05006704{
James Smart2e0fef82007-06-17 19:56:36 -05006705 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05006706 uint32_t ha_copy;
6707 uint32_t work_ha_copy;
6708 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05006709 unsigned long iflag;
dea31012005-04-17 16:05:31 -05006710 uint32_t control;
6711
James Smart92d7f7b2007-06-17 19:56:38 -05006712 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05006713 struct lpfc_vport *vport;
6714 struct lpfc_nodelist *ndlp;
6715 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05006716 LPFC_MBOXQ_t *pmb;
6717 int rc;
6718
dea31012005-04-17 16:05:31 -05006719 /*
6720 * Get the driver's phba structure from the dev_id and
6721 * assume the HBA is not interrupting.
6722 */
James Smart93996272008-08-24 21:50:30 -04006723 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05006724
6725 if (unlikely(!phba))
6726 return IRQ_NONE;
6727
dea31012005-04-17 16:05:31 -05006728 /*
James Smart93996272008-08-24 21:50:30 -04006729 * Stuff needs to be attented to when this function is invoked as an
6730 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05006731 */
James Smart93996272008-08-24 21:50:30 -04006732 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04006733 /* Check device state for handling interrupt */
6734 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04006735 return IRQ_NONE;
6736 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05006737 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04006738 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04006739 /* If somebody is waiting to handle an eratt don't process it
6740 * here. The brdkill function will do this.
6741 */
6742 if (phba->link_flag & LS_IGNORE_ERATT)
6743 ha_copy &= ~HA_ERATT;
6744 /* Check the need for handling ERATT in interrupt handler */
6745 if (ha_copy & HA_ERATT) {
6746 if (phba->hba_flag & HBA_ERATT_HANDLED)
6747 /* ERATT polling has handled ERATT */
6748 ha_copy &= ~HA_ERATT;
6749 else
6750 /* Indicate interrupt handler handles ERATT */
6751 phba->hba_flag |= HBA_ERATT_HANDLED;
6752 }
James Smarta257bf92009-04-06 18:48:10 -04006753
6754 /*
6755 * If there is deferred error attention, do not check for any
6756 * interrupt.
6757 */
6758 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04006759 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04006760 return IRQ_NONE;
6761 }
6762
James Smart93996272008-08-24 21:50:30 -04006763 /* Clear up only attention source related to slow-path */
6764 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
6765 phba->HAregaddr);
6766 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05006767 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04006768 } else
6769 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05006770
dea31012005-04-17 16:05:31 -05006771 work_ha_copy = ha_copy & phba->work_ha_mask;
6772
James Smart93996272008-08-24 21:50:30 -04006773 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05006774 if (work_ha_copy & HA_LATT) {
6775 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
6776 /*
6777 * Turn off Link Attention interrupts
6778 * until CLEAR_LA done
6779 */
James Smart5b75da22008-12-04 22:39:35 -05006780 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05006781 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
6782 control = readl(phba->HCregaddr);
6783 control &= ~HC_LAINT_ENA;
6784 writel(control, phba->HCregaddr);
6785 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05006786 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05006787 }
6788 else
6789 work_ha_copy &= ~HA_LATT;
6790 }
6791
James Smart93996272008-08-24 21:50:30 -04006792 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05006793 /*
6794 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
6795 * the only slow ring.
6796 */
6797 status = (work_ha_copy &
6798 (HA_RXMASK << (4*LPFC_ELS_RING)));
6799 status >>= (4*LPFC_ELS_RING);
6800 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05006801 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05006802 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04006803
6804 lpfc_debugfs_slow_ring_trc(phba,
6805 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
6806 control, status,
6807 (uint32_t)phba->sli.slistat.sli_intr);
6808
James Smart858c9f62007-06-17 19:56:39 -05006809 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04006810 lpfc_debugfs_slow_ring_trc(phba,
6811 "ISR Disable ring:"
6812 "pwork:x%x hawork:x%x wait:x%x",
6813 phba->work_ha, work_ha_copy,
6814 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04006815 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04006816
James Smart858c9f62007-06-17 19:56:39 -05006817 control &=
6818 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05006819 writel(control, phba->HCregaddr);
6820 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05006821 }
James Smarta58cbd52007-08-02 11:09:43 -04006822 else {
6823 lpfc_debugfs_slow_ring_trc(phba,
6824 "ISR slow ring: pwork:"
6825 "x%x hawork:x%x wait:x%x",
6826 phba->work_ha, work_ha_copy,
6827 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04006828 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04006829 }
James Smart5b75da22008-12-04 22:39:35 -05006830 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05006831 }
6832 }
James Smart5b75da22008-12-04 22:39:35 -05006833 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04006834 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04006835 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04006836 /*
6837 * Check if there is a deferred error condition
6838 * is active
6839 */
6840 if ((HS_FFER1 & phba->work_hs) &&
6841 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
6842 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
6843 phba->hba_flag |= DEFER_ERATT;
6844 /* Clear all interrupt enable conditions */
6845 writel(0, phba->HCregaddr);
6846 readl(phba->HCregaddr);
6847 }
6848 }
6849
James Smart93996272008-08-24 21:50:30 -04006850 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05006851 pmb = phba->sli.mbox_active;
6852 pmbox = &pmb->mb;
James Smart34b02dc2008-08-24 21:49:55 -04006853 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05006854 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05006855
6856 /* First check out the status word */
6857 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
6858 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05006859 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006860 /*
6861 * Stray Mailbox Interrupt, mbxCommand <cmd>
6862 * mbxStatus <status>
6863 */
James Smart09372822008-01-11 01:52:54 -05006864 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05006865 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006866 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05006867 "Interrupt mbxCommand x%x "
6868 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006869 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05006870 pmbox->mbxCommand,
6871 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05006872 /* clear mailbox attention bit */
6873 work_ha_copy &= ~HA_MBATT;
6874 } else {
James Smart97eab632008-04-07 10:16:05 -04006875 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05006876 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05006877 phba->last_completion_time = jiffies;
6878 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05006879 if (pmb->mbox_cmpl) {
6880 lpfc_sli_pcimem_bcopy(mbox, pmbox,
6881 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05006882 }
James Smart09372822008-01-11 01:52:54 -05006883 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
6884 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
6885
6886 lpfc_debugfs_disc_trc(vport,
6887 LPFC_DISC_TRC_MBOX_VPORT,
6888 "MBOX dflt rpi: : "
6889 "status:x%x rpi:x%x",
6890 (uint32_t)pmbox->mbxStatus,
6891 pmbox->un.varWords[0], 0);
6892
6893 if (!pmbox->mbxStatus) {
6894 mp = (struct lpfc_dmabuf *)
6895 (pmb->context1);
6896 ndlp = (struct lpfc_nodelist *)
6897 pmb->context2;
6898
6899 /* Reg_LOGIN of dflt RPI was
6900 * successful. new lets get
6901 * rid of the RPI using the
6902 * same mbox buffer.
6903 */
6904 lpfc_unreg_login(phba,
6905 vport->vpi,
6906 pmbox->un.varWords[0],
6907 pmb);
6908 pmb->mbox_cmpl =
6909 lpfc_mbx_cmpl_dflt_rpi;
6910 pmb->context1 = mp;
6911 pmb->context2 = ndlp;
6912 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04006913 rc = lpfc_sli_issue_mbox(phba,
6914 pmb,
6915 MBX_NOWAIT);
6916 if (rc != MBX_BUSY)
6917 lpfc_printf_log(phba,
6918 KERN_ERR,
6919 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04006920 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04006921 "been MBX_BUSY");
James Smart3772a992009-05-22 14:50:54 -04006922 if (rc != MBX_NOT_FINISHED)
6923 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05006924 }
6925 }
James Smart5b75da22008-12-04 22:39:35 -05006926 spin_lock_irqsave(
6927 &phba->pport->work_port_lock,
6928 iflag);
James Smart09372822008-01-11 01:52:54 -05006929 phba->pport->work_port_events &=
6930 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05006931 spin_unlock_irqrestore(
6932 &phba->pport->work_port_lock,
6933 iflag);
James Smart09372822008-01-11 01:52:54 -05006934 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05006935 }
James Smart97eab632008-04-07 10:16:05 -04006936 } else
James Smart5b75da22008-12-04 22:39:35 -05006937 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04006938
James Smart92d7f7b2007-06-17 19:56:38 -05006939 if ((work_ha_copy & HA_MBATT) &&
6940 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05006941send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05006942 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04006943 do {
6944 rc = lpfc_sli_issue_mbox(phba, NULL,
6945 MBX_NOWAIT);
6946 } while (rc == MBX_NOT_FINISHED);
6947 if (rc != MBX_SUCCESS)
6948 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
6949 LOG_SLI, "0349 rc should be "
6950 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05006951 }
6952
James Smart5b75da22008-12-04 22:39:35 -05006953 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05006954 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05006955 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04006956 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05006957 }
James Smart93996272008-08-24 21:50:30 -04006958 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05006959
James Smart3772a992009-05-22 14:50:54 -04006960} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04006961
6962/**
James Smart3772a992009-05-22 14:50:54 -04006963 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04006964 * @irq: Interrupt number.
6965 * @dev_id: The device context pointer.
6966 *
6967 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04006968 * service routine when device with SLI-3 interface spec is enabled with
6969 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
6970 * ring event in the HBA. However, when the device is enabled with either
6971 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
6972 * device-level interrupt handler. When the PCI slot is in error recovery
6973 * or the HBA is undergoing initialization, the interrupt handler will not
6974 * process the interrupt. The SCSI FCP fast-path ring event are handled in
6975 * the intrrupt context. This function is called without any lock held.
6976 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04006977 *
6978 * This function returns IRQ_HANDLED when interrupt is handled else it
6979 * returns IRQ_NONE.
6980 **/
6981irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04006982lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04006983{
6984 struct lpfc_hba *phba;
6985 uint32_t ha_copy;
6986 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05006987 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04006988
6989 /* Get the driver's phba structure from the dev_id and
6990 * assume the HBA is not interrupting.
6991 */
6992 phba = (struct lpfc_hba *) dev_id;
6993
6994 if (unlikely(!phba))
6995 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05006996
6997 /*
James Smart93996272008-08-24 21:50:30 -04006998 * Stuff needs to be attented to when this function is invoked as an
6999 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05007000 */
James Smart93996272008-08-24 21:50:30 -04007001 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04007002 /* Check device state for handling interrupt */
7003 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007004 return IRQ_NONE;
7005 /* Need to read HA REG for FCP ring and other ring events */
7006 ha_copy = readl(phba->HAregaddr);
7007 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05007008 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007009 /*
7010 * If there is deferred error attention, do not check for
7011 * any interrupt.
7012 */
7013 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04007014 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007015 return IRQ_NONE;
7016 }
James Smart93996272008-08-24 21:50:30 -04007017 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
7018 phba->HAregaddr);
7019 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007020 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04007021 } else
7022 ha_copy = phba->ha_copy;
7023
7024 /*
7025 * Process all events on FCP ring. Take the optimized path for FCP IO.
7026 */
7027 ha_copy &= ~(phba->work_ha_mask);
7028
7029 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05007030 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05007031 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05007032 lpfc_sli_handle_fast_ring_event(phba,
7033 &phba->sli.ring[LPFC_FCP_RING],
7034 status);
James Smarta4bc3372006-12-02 13:34:16 -05007035
7036 if (phba->cfg_multi_ring_support == 2) {
7037 /*
James Smart93996272008-08-24 21:50:30 -04007038 * Process all events on extra ring. Take the optimized path
7039 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05007040 */
James Smart93996272008-08-24 21:50:30 -04007041 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05007042 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05007043 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05007044 lpfc_sli_handle_fast_ring_event(phba,
7045 &phba->sli.ring[LPFC_EXTRA_RING],
7046 status);
7047 }
7048 }
dea31012005-04-17 16:05:31 -05007049 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007050} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05007051
James Smart93996272008-08-24 21:50:30 -04007052/**
James Smart3772a992009-05-22 14:50:54 -04007053 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04007054 * @irq: Interrupt number.
7055 * @dev_id: The device context pointer.
7056 *
James Smart3772a992009-05-22 14:50:54 -04007057 * This function is the HBA device-level interrupt handler to device with
7058 * SLI-3 interface spec, called from the PCI layer when either MSI or
7059 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
7060 * requires driver attention. This function invokes the slow-path interrupt
7061 * attention handling function and fast-path interrupt attention handling
7062 * function in turn to process the relevant HBA attention events. This
7063 * function is called without any lock held. It gets the hbalock to access
7064 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04007065 *
7066 * This function returns IRQ_HANDLED when interrupt is handled, else it
7067 * returns IRQ_NONE.
7068 **/
7069irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007070lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04007071{
7072 struct lpfc_hba *phba;
7073 irqreturn_t sp_irq_rc, fp_irq_rc;
7074 unsigned long status1, status2;
7075
7076 /*
7077 * Get the driver's phba structure from the dev_id and
7078 * assume the HBA is not interrupting.
7079 */
7080 phba = (struct lpfc_hba *) dev_id;
7081
7082 if (unlikely(!phba))
7083 return IRQ_NONE;
7084
James Smart3772a992009-05-22 14:50:54 -04007085 /* Check device state for handling interrupt */
7086 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007087 return IRQ_NONE;
7088
7089 spin_lock(&phba->hbalock);
7090 phba->ha_copy = readl(phba->HAregaddr);
7091 if (unlikely(!phba->ha_copy)) {
7092 spin_unlock(&phba->hbalock);
7093 return IRQ_NONE;
7094 } else if (phba->ha_copy & HA_ERATT) {
7095 if (phba->hba_flag & HBA_ERATT_HANDLED)
7096 /* ERATT polling has handled ERATT */
7097 phba->ha_copy &= ~HA_ERATT;
7098 else
7099 /* Indicate interrupt handler handles ERATT */
7100 phba->hba_flag |= HBA_ERATT_HANDLED;
7101 }
7102
James Smarta257bf92009-04-06 18:48:10 -04007103 /*
7104 * If there is deferred error attention, do not check for any interrupt.
7105 */
7106 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7107 spin_unlock_irq(&phba->hbalock);
7108 return IRQ_NONE;
7109 }
7110
James Smart93996272008-08-24 21:50:30 -04007111 /* Clear attention sources except link and error attentions */
7112 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
7113 readl(phba->HAregaddr); /* flush */
7114 spin_unlock(&phba->hbalock);
7115
7116 /*
7117 * Invokes slow-path host attention interrupt handling as appropriate.
7118 */
7119
7120 /* status of events with mailbox and link attention */
7121 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
7122
7123 /* status of events with ELS ring */
7124 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
7125 status2 >>= (4*LPFC_ELS_RING);
7126
7127 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04007128 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04007129 else
7130 sp_irq_rc = IRQ_NONE;
7131
7132 /*
7133 * Invoke fast-path host attention interrupt handling as appropriate.
7134 */
7135
7136 /* status of events with FCP ring */
7137 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
7138 status1 >>= (4*LPFC_FCP_RING);
7139
7140 /* status of events with extra ring */
7141 if (phba->cfg_multi_ring_support == 2) {
7142 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
7143 status2 >>= (4*LPFC_EXTRA_RING);
7144 } else
7145 status2 = 0;
7146
7147 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04007148 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04007149 else
7150 fp_irq_rc = IRQ_NONE;
7151
7152 /* Return device-level interrupt handling status */
7153 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04007154} /* lpfc_sli_intr_handler */