blob: f453fccd67c8183059921f61ff59f071455fd2dc [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. *
Jamie Wellnitz41415862006-02-28 19:25:27 -05004 * Copyright (C) 2004-2006 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040031#include <scsi/scsi_transport_fc.h>
dea31012005-04-17 16:05:31 -050032
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
35#include "lpfc_disc.h"
36#include "lpfc_scsi.h"
37#include "lpfc.h"
38#include "lpfc_crtn.h"
39#include "lpfc_logmsg.h"
40#include "lpfc_compat.h"
41
42/*
43 * Define macro to log: Mailbox command x%x cannot issue Data
44 * This allows multiple uses of lpfc_msgBlk0311
45 * w/o perturbing log msg utility.
46 */
47#define LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag) \
48 lpfc_printf_log(phba, \
49 KERN_INFO, \
50 LOG_MBOX | LOG_SLI, \
51 "%d:0311 Mailbox command x%x cannot issue " \
52 "Data: x%x x%x x%x\n", \
53 phba->brd_no, \
54 mb->mbxCommand, \
55 phba->hba_state, \
56 psli->sli_flag, \
57 flag);
58
59
60/* There are only four IOCB completion types. */
61typedef enum _lpfc_iocb_type {
62 LPFC_UNKNOWN_IOCB,
63 LPFC_UNSOL_IOCB,
64 LPFC_SOL_IOCB,
65 LPFC_ABORT_IOCB
66} lpfc_iocb_type;
67
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040068struct lpfc_iocbq *
69lpfc_sli_get_iocbq(struct lpfc_hba * phba)
70{
71 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
72 struct lpfc_iocbq * iocbq = NULL;
73
74 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
75 return iocbq;
76}
77
James Bottomley604a3e32005-10-29 10:28:33 -050078void
79lpfc_sli_release_iocbq(struct lpfc_hba * phba, struct lpfc_iocbq * iocbq)
80{
81 size_t start_clean = (size_t)(&((struct lpfc_iocbq *)NULL)->iocb);
82
83 /*
84 * Clean all volatile data fields, preserve iotag and node struct.
85 */
86 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
87 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
88}
89
dea31012005-04-17 16:05:31 -050090/*
91 * Translate the iocb command to an iocb command type used to decide the final
92 * disposition of each completed IOCB.
93 */
94static lpfc_iocb_type
95lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
96{
97 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
98
99 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
100 return 0;
101
102 switch (iocb_cmnd) {
103 case CMD_XMIT_SEQUENCE_CR:
104 case CMD_XMIT_SEQUENCE_CX:
105 case CMD_XMIT_BCAST_CN:
106 case CMD_XMIT_BCAST_CX:
107 case CMD_ELS_REQUEST_CR:
108 case CMD_ELS_REQUEST_CX:
109 case CMD_CREATE_XRI_CR:
110 case CMD_CREATE_XRI_CX:
111 case CMD_GET_RPI_CN:
112 case CMD_XMIT_ELS_RSP_CX:
113 case CMD_GET_RPI_CR:
114 case CMD_FCP_IWRITE_CR:
115 case CMD_FCP_IWRITE_CX:
116 case CMD_FCP_IREAD_CR:
117 case CMD_FCP_IREAD_CX:
118 case CMD_FCP_ICMND_CR:
119 case CMD_FCP_ICMND_CX:
120 case CMD_ADAPTER_MSG:
121 case CMD_ADAPTER_DUMP:
122 case CMD_XMIT_SEQUENCE64_CR:
123 case CMD_XMIT_SEQUENCE64_CX:
124 case CMD_XMIT_BCAST64_CN:
125 case CMD_XMIT_BCAST64_CX:
126 case CMD_ELS_REQUEST64_CR:
127 case CMD_ELS_REQUEST64_CX:
128 case CMD_FCP_IWRITE64_CR:
129 case CMD_FCP_IWRITE64_CX:
130 case CMD_FCP_IREAD64_CR:
131 case CMD_FCP_IREAD64_CX:
132 case CMD_FCP_ICMND64_CR:
133 case CMD_FCP_ICMND64_CX:
134 case CMD_GEN_REQUEST64_CR:
135 case CMD_GEN_REQUEST64_CX:
136 case CMD_XMIT_ELS_RSP64_CX:
137 type = LPFC_SOL_IOCB;
138 break;
139 case CMD_ABORT_XRI_CN:
140 case CMD_ABORT_XRI_CX:
141 case CMD_CLOSE_XRI_CN:
142 case CMD_CLOSE_XRI_CX:
143 case CMD_XRI_ABORTED_CX:
144 case CMD_ABORT_MXRI64_CN:
145 type = LPFC_ABORT_IOCB;
146 break;
147 case CMD_RCV_SEQUENCE_CX:
148 case CMD_RCV_ELS_REQ_CX:
149 case CMD_RCV_SEQUENCE64_CX:
150 case CMD_RCV_ELS_REQ64_CX:
151 type = LPFC_UNSOL_IOCB;
152 break;
153 default:
154 type = LPFC_UNKNOWN_IOCB;
155 break;
156 }
157
158 return type;
159}
160
161static int
162lpfc_sli_ring_map(struct lpfc_hba * phba, LPFC_MBOXQ_t *pmb)
163{
164 struct lpfc_sli *psli = &phba->sli;
165 MAILBOX_t *pmbox = &pmb->mb;
166 int i, rc;
167
168 for (i = 0; i < psli->num_rings; i++) {
169 phba->hba_state = LPFC_INIT_MBX_CMDS;
170 lpfc_config_ring(phba, i, pmb);
171 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
172 if (rc != MBX_SUCCESS) {
173 lpfc_printf_log(phba,
174 KERN_ERR,
175 LOG_INIT,
176 "%d:0446 Adapter failed to init, "
177 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
178 "ring %d\n",
179 phba->brd_no,
180 pmbox->mbxCommand,
181 pmbox->mbxStatus,
182 i);
183 phba->hba_state = LPFC_HBA_ERROR;
184 return -ENXIO;
185 }
186 }
187 return 0;
188}
189
190static int
191lpfc_sli_ringtxcmpl_put(struct lpfc_hba * phba,
192 struct lpfc_sli_ring * pring, struct lpfc_iocbq * piocb)
193{
dea31012005-04-17 16:05:31 -0500194 list_add_tail(&piocb->list, &pring->txcmplq);
195 pring->txcmplq_cnt++;
196 if (unlikely(pring->ringno == LPFC_ELS_RING))
197 mod_timer(&phba->els_tmofunc,
198 jiffies + HZ * (phba->fc_ratov << 1));
199
dea31012005-04-17 16:05:31 -0500200 return (0);
201}
202
203static struct lpfc_iocbq *
204lpfc_sli_ringtx_get(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
205{
206 struct list_head *dlp;
207 struct lpfc_iocbq *cmd_iocb;
208
209 dlp = &pring->txq;
210 cmd_iocb = NULL;
211 list_remove_head((&pring->txq), cmd_iocb,
212 struct lpfc_iocbq,
213 list);
214 if (cmd_iocb) {
215 /* If the first ptr is not equal to the list header,
216 * deque the IOCBQ_t and return it.
217 */
218 pring->txq_cnt--;
219 }
220 return (cmd_iocb);
221}
222
223static IOCB_t *
224lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
225{
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -0400226 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -0500227 uint32_t max_cmd_idx = pring->numCiocb;
228 IOCB_t *iocb = NULL;
229
230 if ((pring->next_cmdidx == pring->cmdidx) &&
231 (++pring->next_cmdidx >= max_cmd_idx))
232 pring->next_cmdidx = 0;
233
234 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
235
236 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
237
238 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
239 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
240 "%d:0315 Ring %d issue: portCmdGet %d "
241 "is bigger then cmd ring %d\n",
242 phba->brd_no, pring->ringno,
243 pring->local_getidx, max_cmd_idx);
244
245 phba->hba_state = LPFC_HBA_ERROR;
246 /*
247 * All error attention handlers are posted to
248 * worker thread
249 */
250 phba->work_ha |= HA_ERATT;
251 phba->work_hs = HS_FFER3;
252 if (phba->work_wait)
253 wake_up(phba->work_wait);
254
255 return NULL;
256 }
257
258 if (pring->local_getidx == pring->next_cmdidx)
259 return NULL;
260 }
261
262 iocb = IOCB_ENTRY(pring->cmdringaddr, pring->cmdidx);
263
264 return iocb;
265}
266
James Bottomley604a3e32005-10-29 10:28:33 -0500267uint16_t
268lpfc_sli_next_iotag(struct lpfc_hba * phba, struct lpfc_iocbq * iocbq)
dea31012005-04-17 16:05:31 -0500269{
James Bottomley604a3e32005-10-29 10:28:33 -0500270 struct lpfc_iocbq ** new_arr;
271 struct lpfc_iocbq ** old_arr;
272 size_t new_len;
273 struct lpfc_sli *psli = &phba->sli;
274 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500275
James Bottomley604a3e32005-10-29 10:28:33 -0500276 spin_lock_irq(phba->host->host_lock);
277 iotag = psli->last_iotag;
278 if(++iotag < psli->iocbq_lookup_len) {
279 psli->last_iotag = iotag;
280 psli->iocbq_lookup[iotag] = iocbq;
281 spin_unlock_irq(phba->host->host_lock);
282 iocbq->iotag = iotag;
283 return iotag;
284 }
285 else if (psli->iocbq_lookup_len < (0xffff
286 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
287 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
288 spin_unlock_irq(phba->host->host_lock);
289 new_arr = kmalloc(new_len * sizeof (struct lpfc_iocbq *),
290 GFP_KERNEL);
291 if (new_arr) {
292 memset((char *)new_arr, 0,
293 new_len * sizeof (struct lpfc_iocbq *));
294 spin_lock_irq(phba->host->host_lock);
295 old_arr = psli->iocbq_lookup;
296 if (new_len <= psli->iocbq_lookup_len) {
297 /* highly unprobable case */
298 kfree(new_arr);
299 iotag = psli->last_iotag;
300 if(++iotag < psli->iocbq_lookup_len) {
301 psli->last_iotag = iotag;
302 psli->iocbq_lookup[iotag] = iocbq;
303 spin_unlock_irq(phba->host->host_lock);
304 iocbq->iotag = iotag;
305 return iotag;
306 }
307 spin_unlock_irq(phba->host->host_lock);
308 return 0;
309 }
310 if (psli->iocbq_lookup)
311 memcpy(new_arr, old_arr,
312 ((psli->last_iotag + 1) *
313 sizeof (struct lpfc_iocbq *)));
314 psli->iocbq_lookup = new_arr;
315 psli->iocbq_lookup_len = new_len;
316 psli->last_iotag = iotag;
317 psli->iocbq_lookup[iotag] = iocbq;
318 spin_unlock_irq(phba->host->host_lock);
319 iocbq->iotag = iotag;
320 kfree(old_arr);
321 return iotag;
322 }
dea31012005-04-17 16:05:31 -0500323 }
324
James Bottomley604a3e32005-10-29 10:28:33 -0500325 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
326 "%d:0318 Failed to allocate IOTAG.last IOTAG is %d\n",
327 phba->brd_no, psli->last_iotag);
dea31012005-04-17 16:05:31 -0500328
James Bottomley604a3e32005-10-29 10:28:33 -0500329 return 0;
dea31012005-04-17 16:05:31 -0500330}
331
332static void
333lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
334 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
335{
336 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500337 * Set up an iotag
dea31012005-04-17 16:05:31 -0500338 */
James Bottomley604a3e32005-10-29 10:28:33 -0500339 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500340
341 /*
342 * Issue iocb command to adapter
343 */
344 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, sizeof (IOCB_t));
345 wmb();
346 pring->stats.iocb_cmd++;
347
348 /*
349 * If there is no completion routine to call, we can release the
350 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
351 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
352 */
353 if (nextiocb->iocb_cmpl)
354 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500355 else
356 lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500357
358 /*
359 * Let the HBA know what IOCB slot will be the next one the
360 * driver will put a command into.
361 */
362 pring->cmdidx = pring->next_cmdidx;
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -0400363 writel(pring->cmdidx, phba->MBslimaddr
dea31012005-04-17 16:05:31 -0500364 + (SLIMOFF + (pring->ringno * 2)) * 4);
365}
366
367static void
368lpfc_sli_update_full_ring(struct lpfc_hba * phba,
369 struct lpfc_sli_ring *pring)
370{
371 int ringno = pring->ringno;
372
373 pring->flag |= LPFC_CALL_RING_AVAILABLE;
374
375 wmb();
376
377 /*
378 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
379 * The HBA will tell us when an IOCB entry is available.
380 */
381 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
382 readl(phba->CAregaddr); /* flush */
383
384 pring->stats.iocb_cmd_full++;
385}
386
387static void
388lpfc_sli_update_ring(struct lpfc_hba * phba,
389 struct lpfc_sli_ring *pring)
390{
391 int ringno = pring->ringno;
392
393 /*
394 * Tell the HBA that there is work to do in this ring.
395 */
396 wmb();
397 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
398 readl(phba->CAregaddr); /* flush */
399}
400
401static void
402lpfc_sli_resume_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
403{
404 IOCB_t *iocb;
405 struct lpfc_iocbq *nextiocb;
406
407 /*
408 * Check to see if:
409 * (a) there is anything on the txq to send
410 * (b) link is up
411 * (c) link attention events can be processed (fcp ring only)
412 * (d) IOCB processing is not blocked by the outstanding mbox command.
413 */
414 if (pring->txq_cnt &&
415 (phba->hba_state > LPFC_LINK_DOWN) &&
416 (pring->ringno != phba->sli.fcp_ring ||
417 phba->sli.sli_flag & LPFC_PROCESS_LA) &&
418 !(pring->flag & LPFC_STOP_IOCB_MBX)) {
419
420 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
421 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
422 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
423
424 if (iocb)
425 lpfc_sli_update_ring(phba, pring);
426 else
427 lpfc_sli_update_full_ring(phba, pring);
428 }
429
430 return;
431}
432
433/* lpfc_sli_turn_on_ring is only called by lpfc_sli_handle_mb_event below */
434static void
435lpfc_sli_turn_on_ring(struct lpfc_hba * phba, int ringno)
436{
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -0400437 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[ringno];
dea31012005-04-17 16:05:31 -0500438
439 /* If the ring is active, flag it */
440 if (phba->sli.ring[ringno].cmdringaddr) {
441 if (phba->sli.ring[ringno].flag & LPFC_STOP_IOCB_MBX) {
442 phba->sli.ring[ringno].flag &= ~LPFC_STOP_IOCB_MBX;
443 /*
444 * Force update of the local copy of cmdGetInx
445 */
446 phba->sli.ring[ringno].local_getidx
447 = le32_to_cpu(pgp->cmdGetInx);
448 spin_lock_irq(phba->host->host_lock);
449 lpfc_sli_resume_iocb(phba, &phba->sli.ring[ringno]);
450 spin_unlock_irq(phba->host->host_lock);
451 }
452 }
453}
454
455static int
456lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
457{
458 uint8_t ret;
459
460 switch (mbxCommand) {
461 case MBX_LOAD_SM:
462 case MBX_READ_NV:
463 case MBX_WRITE_NV:
464 case MBX_RUN_BIU_DIAG:
465 case MBX_INIT_LINK:
466 case MBX_DOWN_LINK:
467 case MBX_CONFIG_LINK:
468 case MBX_CONFIG_RING:
469 case MBX_RESET_RING:
470 case MBX_READ_CONFIG:
471 case MBX_READ_RCONFIG:
472 case MBX_READ_SPARM:
473 case MBX_READ_STATUS:
474 case MBX_READ_RPI:
475 case MBX_READ_XRI:
476 case MBX_READ_REV:
477 case MBX_READ_LNK_STAT:
478 case MBX_REG_LOGIN:
479 case MBX_UNREG_LOGIN:
480 case MBX_READ_LA:
481 case MBX_CLEAR_LA:
482 case MBX_DUMP_MEMORY:
483 case MBX_DUMP_CONTEXT:
484 case MBX_RUN_DIAGS:
485 case MBX_RESTART:
486 case MBX_UPDATE_CFG:
487 case MBX_DOWN_LOAD:
488 case MBX_DEL_LD_ENTRY:
489 case MBX_RUN_PROGRAM:
490 case MBX_SET_MASK:
491 case MBX_SET_SLIM:
492 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500493 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -0500494 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -0500495 case MBX_BEACON:
dea31012005-04-17 16:05:31 -0500496 case MBX_LOAD_AREA:
497 case MBX_RUN_BIU_DIAG64:
498 case MBX_CONFIG_PORT:
499 case MBX_READ_SPARM64:
500 case MBX_READ_RPI64:
501 case MBX_REG_LOGIN64:
502 case MBX_READ_LA64:
503 case MBX_FLASH_WR_ULA:
504 case MBX_SET_DEBUG:
505 case MBX_LOAD_EXP_ROM:
506 ret = mbxCommand;
507 break;
508 default:
509 ret = MBX_SHUTDOWN;
510 break;
511 }
512 return (ret);
513}
514static void
515lpfc_sli_wake_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
516{
517 wait_queue_head_t *pdone_q;
518
519 /*
520 * If pdone_q is empty, the driver thread gave up waiting and
521 * continued running.
522 */
523 pdone_q = (wait_queue_head_t *) pmboxq->context1;
524 if (pdone_q)
525 wake_up_interruptible(pdone_q);
526 return;
527}
528
529void
530lpfc_sli_def_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
531{
532 struct lpfc_dmabuf *mp;
533 mp = (struct lpfc_dmabuf *) (pmb->context1);
534 if (mp) {
535 lpfc_mbuf_free(phba, mp->virt, mp->phys);
536 kfree(mp);
537 }
538 mempool_free( pmb, phba->mbox_mem_pool);
539 return;
540}
541
542int
543lpfc_sli_handle_mb_event(struct lpfc_hba * phba)
544{
545 MAILBOX_t *mbox;
546 MAILBOX_t *pmbox;
547 LPFC_MBOXQ_t *pmb;
548 struct lpfc_sli *psli;
549 int i, rc;
550 uint32_t process_next;
551
552 psli = &phba->sli;
553 /* We should only get here if we are in SLI2 mode */
554 if (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE)) {
555 return (1);
556 }
557
558 phba->sli.slistat.mbox_event++;
559
560 /* Get a Mailbox buffer to setup mailbox commands for callback */
561 if ((pmb = phba->sli.mbox_active)) {
562 pmbox = &pmb->mb;
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -0400563 mbox = &phba->slim2p->mbx;
dea31012005-04-17 16:05:31 -0500564
565 /* First check out the status word */
566 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof (uint32_t));
567
568 /* Sanity check to ensure the host owns the mailbox */
569 if (pmbox->mbxOwner != OWN_HOST) {
570 /* Lets try for a while */
571 for (i = 0; i < 10240; i++) {
572 /* First copy command data */
573 lpfc_sli_pcimem_bcopy(mbox, pmbox,
574 sizeof (uint32_t));
575 if (pmbox->mbxOwner == OWN_HOST)
576 goto mbout;
577 }
578 /* Stray Mailbox Interrupt, mbxCommand <cmd> mbxStatus
579 <status> */
580 lpfc_printf_log(phba,
James Smartb4c02652006-07-06 15:50:43 -0400581 KERN_WARNING,
dea31012005-04-17 16:05:31 -0500582 LOG_MBOX | LOG_SLI,
583 "%d:0304 Stray Mailbox Interrupt "
584 "mbxCommand x%x mbxStatus x%x\n",
585 phba->brd_no,
586 pmbox->mbxCommand,
587 pmbox->mbxStatus);
588
589 spin_lock_irq(phba->host->host_lock);
590 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
591 spin_unlock_irq(phba->host->host_lock);
592 return (1);
593 }
594
595 mbout:
596 del_timer_sync(&phba->sli.mbox_tmo);
597 phba->work_hba_events &= ~WORKER_MBOX_TMO;
598
599 /*
600 * It is a fatal error if unknown mbox command completion.
601 */
602 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
603 MBX_SHUTDOWN) {
604
605 /* Unknow mailbox command compl */
606 lpfc_printf_log(phba,
607 KERN_ERR,
608 LOG_MBOX | LOG_SLI,
609 "%d:0323 Unknown Mailbox command %x Cmpl\n",
610 phba->brd_no,
611 pmbox->mbxCommand);
612 phba->hba_state = LPFC_HBA_ERROR;
613 phba->work_hs = HS_FFER3;
614 lpfc_handle_eratt(phba);
615 return (0);
616 }
617
618 phba->sli.mbox_active = NULL;
619 if (pmbox->mbxStatus) {
620 phba->sli.slistat.mbox_stat_err++;
621 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
622 /* Mbox cmd cmpl error - RETRYing */
623 lpfc_printf_log(phba,
624 KERN_INFO,
625 LOG_MBOX | LOG_SLI,
626 "%d:0305 Mbox cmd cmpl error - "
627 "RETRYing Data: x%x x%x x%x x%x\n",
628 phba->brd_no,
629 pmbox->mbxCommand,
630 pmbox->mbxStatus,
631 pmbox->un.varWords[0],
632 phba->hba_state);
633 pmbox->mbxStatus = 0;
634 pmbox->mbxOwner = OWN_HOST;
635 spin_lock_irq(phba->host->host_lock);
636 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
637 spin_unlock_irq(phba->host->host_lock);
638 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
639 if (rc == MBX_SUCCESS)
640 return (0);
641 }
642 }
643
644 /* Mailbox cmd <cmd> Cmpl <cmpl> */
645 lpfc_printf_log(phba,
646 KERN_INFO,
647 LOG_MBOX | LOG_SLI,
648 "%d:0307 Mailbox cmd x%x Cmpl x%p "
649 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
650 phba->brd_no,
651 pmbox->mbxCommand,
652 pmb->mbox_cmpl,
653 *((uint32_t *) pmbox),
654 pmbox->un.varWords[0],
655 pmbox->un.varWords[1],
656 pmbox->un.varWords[2],
657 pmbox->un.varWords[3],
658 pmbox->un.varWords[4],
659 pmbox->un.varWords[5],
660 pmbox->un.varWords[6],
661 pmbox->un.varWords[7]);
662
663 if (pmb->mbox_cmpl) {
664 lpfc_sli_pcimem_bcopy(mbox, pmbox, MAILBOX_CMD_SIZE);
665 pmb->mbox_cmpl(phba,pmb);
666 }
667 }
668
669
670 do {
671 process_next = 0; /* by default don't loop */
672 spin_lock_irq(phba->host->host_lock);
673 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
674
675 /* Process next mailbox command if there is one */
676 if ((pmb = lpfc_mbox_get(phba))) {
677 spin_unlock_irq(phba->host->host_lock);
678 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
679 if (rc == MBX_NOT_FINISHED) {
680 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
681 pmb->mbox_cmpl(phba,pmb);
682 process_next = 1;
683 continue; /* loop back */
684 }
685 } else {
686 spin_unlock_irq(phba->host->host_lock);
687 /* Turn on IOCB processing */
688 for (i = 0; i < phba->sli.num_rings; i++) {
689 lpfc_sli_turn_on_ring(phba, i);
690 }
691
692 /* Free any lpfc_dmabuf's waiting for mbox cmd cmpls */
693 while (!list_empty(&phba->freebufList)) {
694 struct lpfc_dmabuf *mp;
695
696 mp = NULL;
697 list_remove_head((&phba->freebufList),
698 mp,
699 struct lpfc_dmabuf,
700 list);
701 if (mp) {
702 lpfc_mbuf_free(phba, mp->virt,
703 mp->phys);
704 kfree(mp);
705 }
706 }
707 }
708
709 } while (process_next);
710
711 return (0);
712}
713static int
714lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
715 struct lpfc_iocbq *saveq)
716{
717 IOCB_t * irsp;
718 WORD5 * w5p;
719 uint32_t Rctl, Type;
720 uint32_t match, i;
721
722 match = 0;
723 irsp = &(saveq->iocb);
724 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
725 || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)) {
726 Rctl = FC_ELS_REQ;
727 Type = FC_ELS_DATA;
728 } else {
729 w5p =
730 (WORD5 *) & (saveq->iocb.un.
731 ulpWord[5]);
732 Rctl = w5p->hcsw.Rctl;
733 Type = w5p->hcsw.Type;
734
735 /* Firmware Workaround */
736 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
737 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX)) {
738 Rctl = FC_ELS_REQ;
739 Type = FC_ELS_DATA;
740 w5p->hcsw.Rctl = Rctl;
741 w5p->hcsw.Type = Type;
742 }
743 }
744 /* unSolicited Responses */
745 if (pring->prt[0].profile) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -0500746 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
747 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
748 saveq);
dea31012005-04-17 16:05:31 -0500749 match = 1;
750 } else {
751 /* We must search, based on rctl / type
752 for the right routine */
753 for (i = 0; i < pring->num_mask;
754 i++) {
755 if ((pring->prt[i].rctl ==
756 Rctl)
757 && (pring->prt[i].
758 type == Type)) {
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -0500759 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
760 (pring->prt[i].lpfc_sli_rcv_unsol_event)
761 (phba, pring, saveq);
dea31012005-04-17 16:05:31 -0500762 match = 1;
763 break;
764 }
765 }
766 }
767 if (match == 0) {
768 /* Unexpected Rctl / Type received */
769 /* Ring <ringno> handler: unexpected
770 Rctl <Rctl> Type <Type> received */
771 lpfc_printf_log(phba,
772 KERN_WARNING,
773 LOG_SLI,
774 "%d:0313 Ring %d handler: unexpected Rctl x%x "
775 "Type x%x received \n",
776 phba->brd_no,
777 pring->ringno,
778 Rctl,
779 Type);
780 }
781 return(1);
782}
783
784static struct lpfc_iocbq *
James Bottomley604a3e32005-10-29 10:28:33 -0500785lpfc_sli_iocbq_lookup(struct lpfc_hba * phba,
786 struct lpfc_sli_ring * pring,
787 struct lpfc_iocbq * prspiocb)
dea31012005-04-17 16:05:31 -0500788{
dea31012005-04-17 16:05:31 -0500789 struct lpfc_iocbq *cmd_iocb = NULL;
790 uint16_t iotag;
791
James Bottomley604a3e32005-10-29 10:28:33 -0500792 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -0500793
James Bottomley604a3e32005-10-29 10:28:33 -0500794 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
795 cmd_iocb = phba->sli.iocbq_lookup[iotag];
796 list_del(&cmd_iocb->list);
797 pring->txcmplq_cnt--;
798 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500799 }
800
dea31012005-04-17 16:05:31 -0500801 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Bottomley604a3e32005-10-29 10:28:33 -0500802 "%d:0317 iotag x%x is out off "
803 "range: max iotag x%x wd0 x%x\n",
804 phba->brd_no, iotag,
805 phba->sli.last_iotag,
806 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -0500807 return NULL;
808}
809
810static int
811lpfc_sli_process_sol_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
812 struct lpfc_iocbq *saveq)
813{
814 struct lpfc_iocbq * cmdiocbp;
815 int rc = 1;
816 unsigned long iflag;
817
818 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
819 spin_lock_irqsave(phba->host->host_lock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -0500820 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
dea31012005-04-17 16:05:31 -0500821 if (cmdiocbp) {
822 if (cmdiocbp->iocb_cmpl) {
823 /*
824 * Post all ELS completions to the worker thread.
825 * All other are passed to the completion callback.
826 */
827 if (pring->ringno == LPFC_ELS_RING) {
828 spin_unlock_irqrestore(phba->host->host_lock,
829 iflag);
830 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
831 spin_lock_irqsave(phba->host->host_lock, iflag);
832 }
833 else {
dea31012005-04-17 16:05:31 -0500834 spin_unlock_irqrestore(phba->host->host_lock,
835 iflag);
836 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
837 spin_lock_irqsave(phba->host->host_lock, iflag);
838 }
James Bottomley604a3e32005-10-29 10:28:33 -0500839 } else
840 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -0500841 } else {
842 /*
843 * Unknown initiating command based on the response iotag.
844 * This could be the case on the ELS ring because of
845 * lpfc_els_abort().
846 */
847 if (pring->ringno != LPFC_ELS_RING) {
848 /*
849 * Ring <ringno> handler: unexpected completion IoTag
850 * <IoTag>
851 */
852 lpfc_printf_log(phba,
853 KERN_WARNING,
854 LOG_SLI,
855 "%d:0322 Ring %d handler: unexpected "
856 "completion IoTag x%x Data: x%x x%x x%x x%x\n",
857 phba->brd_no,
858 pring->ringno,
859 saveq->iocb.ulpIoTag,
860 saveq->iocb.ulpStatus,
861 saveq->iocb.un.ulpWord[4],
862 saveq->iocb.ulpCommand,
863 saveq->iocb.ulpContext);
864 }
865 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400866
dea31012005-04-17 16:05:31 -0500867 spin_unlock_irqrestore(phba->host->host_lock, iflag);
868 return rc;
869}
870
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500871static void lpfc_sli_rsp_pointers_error(struct lpfc_hba * phba,
872 struct lpfc_sli_ring * pring)
873{
874 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
875 /*
876 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
877 * rsp ring <portRspMax>
878 */
879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
880 "%d:0312 Ring %d handler: portRspPut %d "
881 "is bigger then rsp ring %d\n",
882 phba->brd_no, pring->ringno,
883 le32_to_cpu(pgp->rspPutInx),
884 pring->numRiocb);
885
886 phba->hba_state = LPFC_HBA_ERROR;
887
888 /*
889 * All error attention handlers are posted to
890 * worker thread
891 */
892 phba->work_ha |= HA_ERATT;
893 phba->work_hs = HS_FFER3;
894 if (phba->work_wait)
895 wake_up(phba->work_wait);
896
897 return;
898}
899
900void lpfc_sli_poll_fcp_ring(struct lpfc_hba * phba)
901{
902 struct lpfc_sli * psli = &phba->sli;
903 struct lpfc_sli_ring * pring = &psli->ring[LPFC_FCP_RING];
904 IOCB_t *irsp = NULL;
905 IOCB_t *entry = NULL;
906 struct lpfc_iocbq *cmdiocbq = NULL;
907 struct lpfc_iocbq rspiocbq;
908 struct lpfc_pgp *pgp;
909 uint32_t status;
910 uint32_t portRspPut, portRspMax;
911 int type;
912 uint32_t rsp_cmpl = 0;
913 void __iomem *to_slim;
914 uint32_t ha_copy;
915
916 pring->stats.iocb_event++;
917
918 /* The driver assumes SLI-2 mode */
919 pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
920
921 /*
922 * The next available response entry should never exceed the maximum
923 * entries. If it does, treat it as an adapter hardware error.
924 */
925 portRspMax = pring->numRiocb;
926 portRspPut = le32_to_cpu(pgp->rspPutInx);
927 if (unlikely(portRspPut >= portRspMax)) {
928 lpfc_sli_rsp_pointers_error(phba, pring);
929 return;
930 }
931
932 rmb();
933 while (pring->rspidx != portRspPut) {
934
935 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
936
937 if (++pring->rspidx >= portRspMax)
938 pring->rspidx = 0;
939
940 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
941 (uint32_t *) &rspiocbq.iocb,
942 sizeof (IOCB_t));
943 irsp = &rspiocbq.iocb;
944 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
945 pring->stats.iocb_rsp++;
946 rsp_cmpl++;
947
948 if (unlikely(irsp->ulpStatus)) {
949 /* Rsp ring <ringno> error: IOCB */
950 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
951 "%d:0326 Rsp Ring %d error: IOCB Data: "
952 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
953 phba->brd_no, pring->ringno,
954 irsp->un.ulpWord[0],
955 irsp->un.ulpWord[1],
956 irsp->un.ulpWord[2],
957 irsp->un.ulpWord[3],
958 irsp->un.ulpWord[4],
959 irsp->un.ulpWord[5],
960 *(((uint32_t *) irsp) + 6),
961 *(((uint32_t *) irsp) + 7));
962 }
963
964 switch (type) {
965 case LPFC_ABORT_IOCB:
966 case LPFC_SOL_IOCB:
967 /*
968 * Idle exchange closed via ABTS from port. No iocb
969 * resources need to be recovered.
970 */
971 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
972 printk(KERN_INFO "%s: IOCB cmd 0x%x processed."
973 " Skipping completion\n", __FUNCTION__,
974 irsp->ulpCommand);
975 break;
976 }
977
978 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
979 &rspiocbq);
980 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
981 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
982 &rspiocbq);
983 }
984 break;
985 default:
986 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
987 char adaptermsg[LPFC_MAX_ADPTMSG];
988 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
989 memcpy(&adaptermsg[0], (uint8_t *) irsp,
990 MAX_MSG_DATA);
991 dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
992 phba->brd_no, adaptermsg);
993 } else {
994 /* Unknown IOCB command */
995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
996 "%d:0321 Unknown IOCB command "
997 "Data: x%x, x%x x%x x%x x%x\n",
998 phba->brd_no, type,
999 irsp->ulpCommand,
1000 irsp->ulpStatus,
1001 irsp->ulpIoTag,
1002 irsp->ulpContext);
1003 }
1004 break;
1005 }
1006
1007 /*
1008 * The response IOCB has been processed. Update the ring
1009 * pointer in SLIM. If the port response put pointer has not
1010 * been updated, sync the pgp->rspPutInx and fetch the new port
1011 * response put pointer.
1012 */
1013 to_slim = phba->MBslimaddr +
1014 (SLIMOFF + (pring->ringno * 2) + 1) * 4;
1015 writeb(pring->rspidx, to_slim);
1016
1017 if (pring->rspidx == portRspPut)
1018 portRspPut = le32_to_cpu(pgp->rspPutInx);
1019 }
1020
1021 ha_copy = readl(phba->HAregaddr);
1022 ha_copy >>= (LPFC_FCP_RING * 4);
1023
1024 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
1025 pring->stats.iocb_rsp_full++;
1026 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1027 writel(status, phba->CAregaddr);
1028 readl(phba->CAregaddr);
1029 }
1030 if ((ha_copy & HA_R0CE_RSP) &&
1031 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1032 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1033 pring->stats.iocb_cmd_empty++;
1034
1035 /* Force update of the local copy of cmdGetInx */
1036 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1037 lpfc_sli_resume_iocb(phba, pring);
1038
1039 if ((pring->lpfc_sli_cmd_available))
1040 (pring->lpfc_sli_cmd_available) (phba, pring);
1041
1042 }
1043
1044 return;
1045}
1046
dea31012005-04-17 16:05:31 -05001047/*
1048 * This routine presumes LPFC_FCP_RING handling and doesn't bother
1049 * to check it explicitly.
1050 */
1051static int
1052lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,
1053 struct lpfc_sli_ring * pring, uint32_t mask)
1054{
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04001055 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001056 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001057 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001058 struct lpfc_iocbq *cmdiocbq = NULL;
1059 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001060 uint32_t status;
1061 uint32_t portRspPut, portRspMax;
1062 int rc = 1;
1063 lpfc_iocb_type type;
1064 unsigned long iflag;
1065 uint32_t rsp_cmpl = 0;
1066 void __iomem *to_slim;
1067
1068 spin_lock_irqsave(phba->host->host_lock, iflag);
1069 pring->stats.iocb_event++;
1070
dea31012005-04-17 16:05:31 -05001071 /*
1072 * The next available response entry should never exceed the maximum
1073 * entries. If it does, treat it as an adapter hardware error.
1074 */
1075 portRspMax = pring->numRiocb;
1076 portRspPut = le32_to_cpu(pgp->rspPutInx);
1077 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001078 lpfc_sli_rsp_pointers_error(phba, pring);
dea31012005-04-17 16:05:31 -05001079 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1080 return 1;
1081 }
1082
1083 rmb();
1084 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001085 /*
1086 * Fetch an entry off the ring and copy it into a local data
1087 * structure. The copy involves a byte-swap since the
1088 * network byte order and pci byte orders are different.
1089 */
James.Smart@Emulex.Com4a0dfcd2005-10-28 20:29:56 -04001090 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001091
1092 if (++pring->rspidx >= portRspMax)
1093 pring->rspidx = 0;
1094
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001095 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1096 (uint32_t *) &rspiocbq.iocb,
1097 sizeof (IOCB_t));
1098 irsp = &rspiocbq.iocb;
1099
dea31012005-04-17 16:05:31 -05001100 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1101 pring->stats.iocb_rsp++;
1102 rsp_cmpl++;
1103
1104 if (unlikely(irsp->ulpStatus)) {
1105 /* Rsp ring <ringno> error: IOCB */
1106 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1107 "%d:0326 Rsp Ring %d error: IOCB Data: "
1108 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1109 phba->brd_no, pring->ringno,
1110 irsp->un.ulpWord[0], irsp->un.ulpWord[1],
1111 irsp->un.ulpWord[2], irsp->un.ulpWord[3],
1112 irsp->un.ulpWord[4], irsp->un.ulpWord[5],
1113 *(((uint32_t *) irsp) + 6),
1114 *(((uint32_t *) irsp) + 7));
1115 }
1116
1117 switch (type) {
1118 case LPFC_ABORT_IOCB:
1119 case LPFC_SOL_IOCB:
1120 /*
1121 * Idle exchange closed via ABTS from port. No iocb
1122 * resources need to be recovered.
1123 */
1124 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1125 printk(KERN_INFO "%s: IOCB cmd 0x%x processed. "
1126 "Skipping completion\n", __FUNCTION__,
1127 irsp->ulpCommand);
1128 break;
1129 }
1130
James Bottomley604a3e32005-10-29 10:28:33 -05001131 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1132 &rspiocbq);
dea31012005-04-17 16:05:31 -05001133 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001134 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1135 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1136 &rspiocbq);
1137 } else {
1138 spin_unlock_irqrestore(
1139 phba->host->host_lock, iflag);
1140 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1141 &rspiocbq);
1142 spin_lock_irqsave(phba->host->host_lock,
1143 iflag);
1144 }
dea31012005-04-17 16:05:31 -05001145 }
1146 break;
1147 default:
1148 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1149 char adaptermsg[LPFC_MAX_ADPTMSG];
1150 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1151 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1152 MAX_MSG_DATA);
1153 dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
1154 phba->brd_no, adaptermsg);
1155 } else {
1156 /* Unknown IOCB command */
1157 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1158 "%d:0321 Unknown IOCB command "
1159 "Data: x%x, x%x x%x x%x x%x\n",
1160 phba->brd_no, type, irsp->ulpCommand,
1161 irsp->ulpStatus, irsp->ulpIoTag,
1162 irsp->ulpContext);
1163 }
1164 break;
1165 }
1166
1167 /*
1168 * The response IOCB has been processed. Update the ring
1169 * pointer in SLIM. If the port response put pointer has not
1170 * been updated, sync the pgp->rspPutInx and fetch the new port
1171 * response put pointer.
1172 */
dea31012005-04-17 16:05:31 -05001173 to_slim = phba->MBslimaddr +
1174 (SLIMOFF + (pring->ringno * 2) + 1) * 4;
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -04001175 writel(pring->rspidx, to_slim);
dea31012005-04-17 16:05:31 -05001176
1177 if (pring->rspidx == portRspPut)
1178 portRspPut = le32_to_cpu(pgp->rspPutInx);
1179 }
1180
1181 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
1182 pring->stats.iocb_rsp_full++;
1183 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1184 writel(status, phba->CAregaddr);
1185 readl(phba->CAregaddr);
1186 }
1187 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1188 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1189 pring->stats.iocb_cmd_empty++;
1190
1191 /* Force update of the local copy of cmdGetInx */
1192 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1193 lpfc_sli_resume_iocb(phba, pring);
1194
1195 if ((pring->lpfc_sli_cmd_available))
1196 (pring->lpfc_sli_cmd_available) (phba, pring);
1197
1198 }
1199
1200 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1201 return rc;
1202}
1203
1204
1205int
1206lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,
1207 struct lpfc_sli_ring * pring, uint32_t mask)
1208{
1209 IOCB_t *entry;
1210 IOCB_t *irsp = NULL;
1211 struct lpfc_iocbq *rspiocbp = NULL;
1212 struct lpfc_iocbq *next_iocb;
1213 struct lpfc_iocbq *cmdiocbp;
1214 struct lpfc_iocbq *saveq;
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04001215 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001216 uint8_t iocb_cmd_type;
1217 lpfc_iocb_type type;
1218 uint32_t status, free_saveq;
1219 uint32_t portRspPut, portRspMax;
1220 int rc = 1;
1221 unsigned long iflag;
1222 void __iomem *to_slim;
1223
1224 spin_lock_irqsave(phba->host->host_lock, iflag);
1225 pring->stats.iocb_event++;
1226
dea31012005-04-17 16:05:31 -05001227 /*
1228 * The next available response entry should never exceed the maximum
1229 * entries. If it does, treat it as an adapter hardware error.
1230 */
1231 portRspMax = pring->numRiocb;
1232 portRspPut = le32_to_cpu(pgp->rspPutInx);
1233 if (portRspPut >= portRspMax) {
1234 /*
1235 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1236 * rsp ring <portRspMax>
1237 */
1238 lpfc_printf_log(phba,
1239 KERN_ERR,
1240 LOG_SLI,
1241 "%d:0312 Ring %d handler: portRspPut %d "
1242 "is bigger then rsp ring %d\n",
1243 phba->brd_no,
1244 pring->ringno, portRspPut, portRspMax);
1245
1246 phba->hba_state = LPFC_HBA_ERROR;
1247 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1248
1249 phba->work_hs = HS_FFER3;
1250 lpfc_handle_eratt(phba);
1251
1252 return 1;
1253 }
1254
1255 rmb();
dea31012005-04-17 16:05:31 -05001256 while (pring->rspidx != portRspPut) {
1257 /*
1258 * Build a completion list and call the appropriate handler.
1259 * The process is to get the next available response iocb, get
1260 * a free iocb from the list, copy the response data into the
1261 * free iocb, insert to the continuation list, and update the
1262 * next response index to slim. This process makes response
1263 * iocb's in the ring available to DMA as fast as possible but
1264 * pays a penalty for a copy operation. Since the iocb is
1265 * only 32 bytes, this penalty is considered small relative to
1266 * the PCI reads for register values and a slim write. When
1267 * the ulpLe field is set, the entire Command has been
1268 * received.
1269 */
1270 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001271 rspiocbp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001272 if (rspiocbp == NULL) {
1273 printk(KERN_ERR "%s: out of buffers! Failing "
1274 "completion.\n", __FUNCTION__);
1275 break;
1276 }
1277
1278 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb, sizeof (IOCB_t));
1279 irsp = &rspiocbp->iocb;
1280
1281 if (++pring->rspidx >= portRspMax)
1282 pring->rspidx = 0;
1283
1284 to_slim = phba->MBslimaddr + (SLIMOFF + (pring->ringno * 2)
1285 + 1) * 4;
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -04001286 writel(pring->rspidx, to_slim);
dea31012005-04-17 16:05:31 -05001287
1288 if (list_empty(&(pring->iocb_continueq))) {
1289 list_add(&rspiocbp->list, &(pring->iocb_continueq));
1290 } else {
1291 list_add_tail(&rspiocbp->list,
1292 &(pring->iocb_continueq));
1293 }
1294
1295 pring->iocb_continueq_cnt++;
1296 if (irsp->ulpLe) {
1297 /*
1298 * By default, the driver expects to free all resources
1299 * associated with this iocb completion.
1300 */
1301 free_saveq = 1;
1302 saveq = list_get_first(&pring->iocb_continueq,
1303 struct lpfc_iocbq, list);
1304 irsp = &(saveq->iocb);
1305 list_del_init(&pring->iocb_continueq);
1306 pring->iocb_continueq_cnt = 0;
1307
1308 pring->stats.iocb_rsp++;
1309
1310 if (irsp->ulpStatus) {
1311 /* Rsp ring <ringno> error: IOCB */
1312 lpfc_printf_log(phba,
1313 KERN_WARNING,
1314 LOG_SLI,
1315 "%d:0328 Rsp Ring %d error: IOCB Data: "
1316 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1317 phba->brd_no,
1318 pring->ringno,
1319 irsp->un.ulpWord[0],
1320 irsp->un.ulpWord[1],
1321 irsp->un.ulpWord[2],
1322 irsp->un.ulpWord[3],
1323 irsp->un.ulpWord[4],
1324 irsp->un.ulpWord[5],
1325 *(((uint32_t *) irsp) + 6),
1326 *(((uint32_t *) irsp) + 7));
1327 }
1328
1329 /*
1330 * Fetch the IOCB command type and call the correct
1331 * completion routine. Solicited and Unsolicited
1332 * IOCBs on the ELS ring get freed back to the
1333 * lpfc_iocb_list by the discovery kernel thread.
1334 */
1335 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
1336 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
1337 if (type == LPFC_SOL_IOCB) {
1338 spin_unlock_irqrestore(phba->host->host_lock,
1339 iflag);
1340 rc = lpfc_sli_process_sol_iocb(phba, pring,
1341 saveq);
1342 spin_lock_irqsave(phba->host->host_lock, iflag);
1343 } else if (type == LPFC_UNSOL_IOCB) {
1344 spin_unlock_irqrestore(phba->host->host_lock,
1345 iflag);
1346 rc = lpfc_sli_process_unsol_iocb(phba, pring,
1347 saveq);
1348 spin_lock_irqsave(phba->host->host_lock, iflag);
1349 } else if (type == LPFC_ABORT_IOCB) {
1350 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
1351 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05001352 lpfc_sli_iocbq_lookup(phba, pring,
1353 saveq)))) {
dea31012005-04-17 16:05:31 -05001354 /* Call the specified completion
1355 routine */
1356 if (cmdiocbp->iocb_cmpl) {
1357 spin_unlock_irqrestore(
1358 phba->host->host_lock,
1359 iflag);
1360 (cmdiocbp->iocb_cmpl) (phba,
1361 cmdiocbp, saveq);
1362 spin_lock_irqsave(
1363 phba->host->host_lock,
1364 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001365 } else
1366 lpfc_sli_release_iocbq(phba,
1367 cmdiocbp);
dea31012005-04-17 16:05:31 -05001368 }
1369 } else if (type == LPFC_UNKNOWN_IOCB) {
1370 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1371
1372 char adaptermsg[LPFC_MAX_ADPTMSG];
1373
1374 memset(adaptermsg, 0,
1375 LPFC_MAX_ADPTMSG);
1376 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1377 MAX_MSG_DATA);
1378 dev_warn(&((phba->pcidev)->dev),
1379 "lpfc%d: %s",
1380 phba->brd_no, adaptermsg);
1381 } else {
1382 /* Unknown IOCB command */
1383 lpfc_printf_log(phba,
1384 KERN_ERR,
1385 LOG_SLI,
1386 "%d:0321 Unknown IOCB command "
1387 "Data: x%x x%x x%x x%x\n",
1388 phba->brd_no,
1389 irsp->ulpCommand,
1390 irsp->ulpStatus,
1391 irsp->ulpIoTag,
1392 irsp->ulpContext);
1393 }
1394 }
1395
1396 if (free_saveq) {
1397 if (!list_empty(&saveq->list)) {
1398 list_for_each_entry_safe(rspiocbp,
1399 next_iocb,
1400 &saveq->list,
1401 list) {
James Bottomley604a3e32005-10-29 10:28:33 -05001402 lpfc_sli_release_iocbq(phba,
1403 rspiocbp);
dea31012005-04-17 16:05:31 -05001404 }
1405 }
1406
James Bottomley604a3e32005-10-29 10:28:33 -05001407 lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05001408 }
1409 }
1410
1411 /*
1412 * If the port response put pointer has not been updated, sync
1413 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
1414 * response put pointer.
1415 */
1416 if (pring->rspidx == portRspPut) {
1417 portRspPut = le32_to_cpu(pgp->rspPutInx);
1418 }
1419 } /* while (pring->rspidx != portRspPut) */
1420
1421 if ((rspiocbp != 0) && (mask & HA_R0RE_REQ)) {
1422 /* At least one response entry has been freed */
1423 pring->stats.iocb_rsp_full++;
1424 /* SET RxRE_RSP in Chip Att register */
1425 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1426 writel(status, phba->CAregaddr);
1427 readl(phba->CAregaddr); /* flush */
1428 }
1429 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1430 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1431 pring->stats.iocb_cmd_empty++;
1432
1433 /* Force update of the local copy of cmdGetInx */
1434 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1435 lpfc_sli_resume_iocb(phba, pring);
1436
1437 if ((pring->lpfc_sli_cmd_available))
1438 (pring->lpfc_sli_cmd_available) (phba, pring);
1439
1440 }
1441
1442 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1443 return rc;
1444}
1445
1446int
1447lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1448{
1449 struct lpfc_iocbq *iocb, *next_iocb;
1450 IOCB_t *icmd = NULL, *cmd = NULL;
1451 int errcnt;
dea31012005-04-17 16:05:31 -05001452
1453 errcnt = 0;
1454
1455 /* Error everything on txq and txcmplq
1456 * First do the txq.
1457 */
1458 spin_lock_irq(phba->host->host_lock);
1459 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
1460 list_del_init(&iocb->list);
1461 if (iocb->iocb_cmpl) {
1462 icmd = &iocb->iocb;
1463 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1464 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1465 spin_unlock_irq(phba->host->host_lock);
1466 (iocb->iocb_cmpl) (phba, iocb, iocb);
1467 spin_lock_irq(phba->host->host_lock);
James Bottomley604a3e32005-10-29 10:28:33 -05001468 } else
1469 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001470 }
1471 pring->txq_cnt = 0;
1472 INIT_LIST_HEAD(&(pring->txq));
1473
1474 /* Next issue ABTS for everything on the txcmplq */
1475 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1476 cmd = &iocb->iocb;
1477
1478 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001479 * Imediate abort of IOCB, deque and call compl
dea31012005-04-17 16:05:31 -05001480 */
dea31012005-04-17 16:05:31 -05001481
1482 list_del_init(&iocb->list);
1483 pring->txcmplq_cnt--;
1484
1485 if (iocb->iocb_cmpl) {
1486 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1487 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1488 spin_unlock_irq(phba->host->host_lock);
1489 (iocb->iocb_cmpl) (phba, iocb, iocb);
1490 spin_lock_irq(phba->host->host_lock);
James Bottomley604a3e32005-10-29 10:28:33 -05001491 } else
1492 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001493 }
1494
1495 INIT_LIST_HEAD(&pring->txcmplq);
1496 pring->txcmplq_cnt = 0;
1497 spin_unlock_irq(phba->host->host_lock);
1498
1499 return errcnt;
1500}
1501
Jamie Wellnitz41415862006-02-28 19:25:27 -05001502int
1503lpfc_sli_brdready(struct lpfc_hba * phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05001504{
Jamie Wellnitz41415862006-02-28 19:25:27 -05001505 uint32_t status;
1506 int i = 0;
1507 int retval = 0;
dea31012005-04-17 16:05:31 -05001508
Jamie Wellnitz41415862006-02-28 19:25:27 -05001509 /* Read the HBA Host Status Register */
1510 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05001511
Jamie Wellnitz41415862006-02-28 19:25:27 -05001512 /*
1513 * Check status register every 100ms for 5 retries, then every
1514 * 500ms for 5, then every 2.5 sec for 5, then reset board and
1515 * every 2.5 sec for 4.
1516 * Break our of the loop if errors occurred during init.
1517 */
1518 while (((status & mask) != mask) &&
1519 !(status & HS_FFERM) &&
1520 i++ < 20) {
dea31012005-04-17 16:05:31 -05001521
Jamie Wellnitz41415862006-02-28 19:25:27 -05001522 if (i <= 5)
1523 msleep(10);
1524 else if (i <= 10)
1525 msleep(500);
1526 else
1527 msleep(2500);
dea31012005-04-17 16:05:31 -05001528
Jamie Wellnitz41415862006-02-28 19:25:27 -05001529 if (i == 15) {
1530 phba->hba_state = LPFC_STATE_UNKNOWN; /* Do post */
1531 lpfc_sli_brdrestart(phba);
1532 }
1533 /* Read the HBA Host Status Register */
1534 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05001535 }
dea31012005-04-17 16:05:31 -05001536
Jamie Wellnitz41415862006-02-28 19:25:27 -05001537 /* Check to see if any errors occurred during init */
1538 if ((status & HS_FFERM) || (i >= 20)) {
1539 phba->hba_state = LPFC_HBA_ERROR;
1540 retval = 1;
1541 }
dea31012005-04-17 16:05:31 -05001542
Jamie Wellnitz41415862006-02-28 19:25:27 -05001543 return retval;
dea31012005-04-17 16:05:31 -05001544}
1545
James Smart92908312006-03-07 15:04:13 -05001546#define BARRIER_TEST_PATTERN (0xdeadbeef)
1547
1548void lpfc_reset_barrier(struct lpfc_hba * phba)
1549{
James Smart65a29c12006-07-06 15:50:50 -04001550 uint32_t __iomem *resp_buf;
1551 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05001552 volatile uint32_t mbox;
1553 uint32_t hc_copy;
1554 int i;
1555 uint8_t hdrtype;
1556
1557 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
1558 if (hdrtype != 0x80 ||
1559 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
1560 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
1561 return;
1562
1563 /*
1564 * Tell the other part of the chip to suspend temporarily all
1565 * its DMA activity.
1566 */
James Smart65a29c12006-07-06 15:50:50 -04001567 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05001568
1569 /* Disable the error attention */
1570 hc_copy = readl(phba->HCregaddr);
1571 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
1572 readl(phba->HCregaddr); /* flush */
1573
1574 if (readl(phba->HAregaddr) & HA_ERATT) {
1575 /* Clear Chip error bit */
1576 writel(HA_ERATT, phba->HAregaddr);
1577 phba->stopped = 1;
1578 }
1579
1580 mbox = 0;
1581 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
1582 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
1583
1584 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04001585 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05001586 writel(mbox, mbox_buf);
1587
1588 for (i = 0;
1589 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
1590 mdelay(1);
1591
1592 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
1593 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
1594 phba->stopped)
1595 goto restore_hc;
1596 else
1597 goto clear_errat;
1598 }
1599
1600 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
1601 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
1602 mdelay(1);
1603
1604clear_errat:
1605
1606 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
1607 mdelay(1);
1608
1609 if (readl(phba->HAregaddr) & HA_ERATT) {
1610 writel(HA_ERATT, phba->HAregaddr);
1611 phba->stopped = 1;
1612 }
1613
1614restore_hc:
1615 writel(hc_copy, phba->HCregaddr);
1616 readl(phba->HCregaddr); /* flush */
1617}
1618
Jamie Wellnitz41415862006-02-28 19:25:27 -05001619int
1620lpfc_sli_brdkill(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -05001621{
Jamie Wellnitz41415862006-02-28 19:25:27 -05001622 struct lpfc_sli *psli;
1623 LPFC_MBOXQ_t *pmb;
1624 uint32_t status;
1625 uint32_t ha_copy;
1626 int retval;
1627 int i = 0;
1628
1629 psli = &phba->sli;
1630
1631 /* Kill HBA */
1632 lpfc_printf_log(phba,
1633 KERN_INFO,
1634 LOG_SLI,
1635 "%d:0329 Kill HBA Data: x%x x%x\n",
1636 phba->brd_no,
1637 phba->hba_state,
1638 psli->sli_flag);
1639
1640 if ((pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
James Smart92908312006-03-07 15:04:13 -05001641 GFP_KERNEL)) == 0)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001642 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001643
1644 /* Disable the error attention */
1645 spin_lock_irq(phba->host->host_lock);
1646 status = readl(phba->HCregaddr);
1647 status &= ~HC_ERINT_ENA;
1648 writel(status, phba->HCregaddr);
1649 readl(phba->HCregaddr); /* flush */
1650 spin_unlock_irq(phba->host->host_lock);
1651
1652 lpfc_kill_board(phba, pmb);
1653 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1654 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1655
1656 if (retval != MBX_SUCCESS) {
1657 if (retval != MBX_BUSY)
1658 mempool_free(pmb, phba->mbox_mem_pool);
1659 return 1;
1660 }
1661
James Smart92908312006-03-07 15:04:13 -05001662 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
1663
Jamie Wellnitz41415862006-02-28 19:25:27 -05001664 mempool_free(pmb, phba->mbox_mem_pool);
1665
1666 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
1667 * attention every 100ms for 3 seconds. If we don't get ERATT after
1668 * 3 seconds we still set HBA_ERROR state because the status of the
1669 * board is now undefined.
1670 */
1671 ha_copy = readl(phba->HAregaddr);
1672
1673 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
1674 mdelay(100);
1675 ha_copy = readl(phba->HAregaddr);
1676 }
1677
1678 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05001679 if (ha_copy & HA_ERATT) {
1680 writel(HA_ERATT, phba->HAregaddr);
1681 phba->stopped = 1;
1682 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05001683 spin_lock_irq(phba->host->host_lock);
1684 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1685 spin_unlock_irq(phba->host->host_lock);
1686
1687 psli->mbox_active = NULL;
1688 lpfc_hba_down_post(phba);
1689 phba->hba_state = LPFC_HBA_ERROR;
1690
1691 return (ha_copy & HA_ERATT ? 0 : 1);
1692}
1693
1694int
1695lpfc_sli_brdreset(struct lpfc_hba * phba)
1696{
1697 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05001698 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001699 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05001700 int i;
dea31012005-04-17 16:05:31 -05001701
Jamie Wellnitz41415862006-02-28 19:25:27 -05001702 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001703
Jamie Wellnitz41415862006-02-28 19:25:27 -05001704 /* Reset HBA */
1705 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1706 "%d:0325 Reset HBA Data: x%x x%x\n", phba->brd_no,
1707 phba->hba_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05001708
1709 /* perform board reset */
1710 phba->fc_eventTag = 0;
1711 phba->fc_myDID = 0;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001712 phba->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05001713
Jamie Wellnitz41415862006-02-28 19:25:27 -05001714 psli->sli_flag = 0;
1715
1716 /* Turn off parity checking and serr during the physical reset */
1717 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
1718 pci_write_config_word(phba->pcidev, PCI_COMMAND,
1719 (cfg_value &
1720 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
1721
James Smart92908312006-03-07 15:04:13 -05001722 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001723 /* Now toggle INITFF bit in the Host Control Register */
1724 writel(HC_INITFF, phba->HCregaddr);
1725 mdelay(1);
1726 readl(phba->HCregaddr); /* flush */
1727 writel(0, phba->HCregaddr);
1728 readl(phba->HCregaddr); /* flush */
1729
1730 /* Restore PCI cmd register */
1731 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05001732
1733 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05001734 for (i = 0; i < psli->num_rings; i++) {
1735 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05001736 pring->flag = 0;
1737 pring->rspidx = 0;
1738 pring->next_cmdidx = 0;
1739 pring->local_getidx = 0;
1740 pring->cmdidx = 0;
1741 pring->missbufcnt = 0;
1742 }
dea31012005-04-17 16:05:31 -05001743
Jamie Wellnitz41415862006-02-28 19:25:27 -05001744 phba->hba_state = LPFC_WARM_START;
1745 return 0;
1746}
1747
1748int
1749lpfc_sli_brdrestart(struct lpfc_hba * phba)
1750{
1751 MAILBOX_t *mb;
1752 struct lpfc_sli *psli;
1753 uint16_t skip_post;
1754 volatile uint32_t word0;
1755 void __iomem *to_slim;
1756
1757 spin_lock_irq(phba->host->host_lock);
1758
1759 psli = &phba->sli;
1760
1761 /* Restart HBA */
1762 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1763 "%d:0328 Restart HBA Data: x%x x%x\n", phba->brd_no,
1764 phba->hba_state, psli->sli_flag);
1765
1766 word0 = 0;
1767 mb = (MAILBOX_t *) &word0;
1768 mb->mbxCommand = MBX_RESTART;
1769 mb->mbxHc = 1;
1770
James Smart92908312006-03-07 15:04:13 -05001771 lpfc_reset_barrier(phba);
1772
Jamie Wellnitz41415862006-02-28 19:25:27 -05001773 to_slim = phba->MBslimaddr;
1774 writel(*(uint32_t *) mb, to_slim);
1775 readl(to_slim); /* flush */
1776
1777 /* Only skip post after fc_ffinit is completed */
1778 if (phba->hba_state) {
1779 skip_post = 1;
1780 word0 = 1; /* This is really setting up word1 */
dea31012005-04-17 16:05:31 -05001781 } else {
Jamie Wellnitz41415862006-02-28 19:25:27 -05001782 skip_post = 0;
1783 word0 = 0; /* This is really setting up word1 */
1784 }
James Smart65a29c12006-07-06 15:50:50 -04001785 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001786 writel(*(uint32_t *) mb, to_slim);
1787 readl(to_slim); /* flush */
1788
1789 lpfc_sli_brdreset(phba);
James Smart92908312006-03-07 15:04:13 -05001790 phba->stopped = 0;
Jamie Wellnitz41415862006-02-28 19:25:27 -05001791 phba->hba_state = LPFC_INIT_START;
1792
1793 spin_unlock_irq(phba->host->host_lock);
1794
James Smart64ba8812006-08-02 15:24:34 -04001795 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
1796 psli->stats_start = get_seconds();
1797
Jamie Wellnitz41415862006-02-28 19:25:27 -05001798 if (skip_post)
1799 mdelay(100);
1800 else
dea31012005-04-17 16:05:31 -05001801 mdelay(2000);
dea31012005-04-17 16:05:31 -05001802
Jamie Wellnitz41415862006-02-28 19:25:27 -05001803 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05001804
1805 return 0;
1806}
1807
1808static int
1809lpfc_sli_chipset_init(struct lpfc_hba *phba)
1810{
1811 uint32_t status, i = 0;
1812
1813 /* Read the HBA Host Status Register */
1814 status = readl(phba->HSregaddr);
1815
1816 /* Check status register to see what current state is */
1817 i = 0;
1818 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
1819
1820 /* Check every 100ms for 5 retries, then every 500ms for 5, then
1821 * every 2.5 sec for 5, then reset board and every 2.5 sec for
1822 * 4.
1823 */
1824 if (i++ >= 20) {
1825 /* Adapter failed to init, timeout, status reg
1826 <status> */
1827 lpfc_printf_log(phba,
1828 KERN_ERR,
1829 LOG_INIT,
1830 "%d:0436 Adapter failed to init, "
1831 "timeout, status reg x%x\n",
1832 phba->brd_no,
1833 status);
1834 phba->hba_state = LPFC_HBA_ERROR;
1835 return -ETIMEDOUT;
1836 }
1837
1838 /* Check to see if any errors occurred during init */
1839 if (status & HS_FFERM) {
1840 /* ERROR: During chipset initialization */
1841 /* Adapter failed to init, chipset, status reg
1842 <status> */
1843 lpfc_printf_log(phba,
1844 KERN_ERR,
1845 LOG_INIT,
1846 "%d:0437 Adapter failed to init, "
1847 "chipset, status reg x%x\n",
1848 phba->brd_no,
1849 status);
1850 phba->hba_state = LPFC_HBA_ERROR;
1851 return -EIO;
1852 }
1853
1854 if (i <= 5) {
1855 msleep(10);
1856 } else if (i <= 10) {
1857 msleep(500);
1858 } else {
1859 msleep(2500);
1860 }
1861
1862 if (i == 15) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05001863 phba->hba_state = LPFC_STATE_UNKNOWN; /* Do post */
1864 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001865 }
1866 /* Read the HBA Host Status Register */
1867 status = readl(phba->HSregaddr);
1868 }
1869
1870 /* Check to see if any errors occurred during init */
1871 if (status & HS_FFERM) {
1872 /* ERROR: During chipset initialization */
1873 /* Adapter failed to init, chipset, status reg <status> */
1874 lpfc_printf_log(phba,
1875 KERN_ERR,
1876 LOG_INIT,
1877 "%d:0438 Adapter failed to init, chipset, "
1878 "status reg x%x\n",
1879 phba->brd_no,
1880 status);
1881 phba->hba_state = LPFC_HBA_ERROR;
1882 return -EIO;
1883 }
1884
1885 /* Clear all interrupt enable conditions */
1886 writel(0, phba->HCregaddr);
1887 readl(phba->HCregaddr); /* flush */
1888
1889 /* setup host attn register */
1890 writel(0xffffffff, phba->HAregaddr);
1891 readl(phba->HAregaddr); /* flush */
1892 return 0;
1893}
1894
1895int
1896lpfc_sli_hba_setup(struct lpfc_hba * phba)
1897{
1898 LPFC_MBOXQ_t *pmb;
1899 uint32_t resetcount = 0, rc = 0, done = 0;
1900
1901 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1902 if (!pmb) {
1903 phba->hba_state = LPFC_HBA_ERROR;
1904 return -ENOMEM;
1905 }
1906
1907 while (resetcount < 2 && !done) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05001908 phba->hba_state = LPFC_STATE_UNKNOWN;
1909 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001910 msleep(2500);
1911 rc = lpfc_sli_chipset_init(phba);
1912 if (rc)
1913 break;
1914
1915 resetcount++;
1916
1917 /* Call pre CONFIG_PORT mailbox command initialization. A value of 0
1918 * means the call was successful. Any other nonzero value is a failure,
1919 * but if ERESTART is returned, the driver may reset the HBA and try
1920 * again.
1921 */
1922 rc = lpfc_config_port_prep(phba);
1923 if (rc == -ERESTART) {
1924 phba->hba_state = 0;
1925 continue;
1926 } else if (rc) {
1927 break;
1928 }
1929
1930 phba->hba_state = LPFC_INIT_MBX_CMDS;
1931 lpfc_config_port(phba, pmb);
1932 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1933 if (rc == MBX_SUCCESS)
1934 done = 1;
1935 else {
1936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1937 "%d:0442 Adapter failed to init, mbxCmd x%x "
1938 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
1939 phba->brd_no, pmb->mb.mbxCommand,
1940 pmb->mb.mbxStatus, 0);
1941 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
1942 }
1943 }
1944 if (!done)
1945 goto lpfc_sli_hba_setup_error;
1946
1947 rc = lpfc_sli_ring_map(phba, pmb);
1948
1949 if (rc)
1950 goto lpfc_sli_hba_setup_error;
1951
1952 phba->sli.sli_flag |= LPFC_PROCESS_LA;
1953
1954 rc = lpfc_config_port_post(phba);
1955 if (rc)
1956 goto lpfc_sli_hba_setup_error;
1957
1958 goto lpfc_sli_hba_setup_exit;
1959lpfc_sli_hba_setup_error:
1960 phba->hba_state = LPFC_HBA_ERROR;
1961lpfc_sli_hba_setup_exit:
1962 mempool_free(pmb, phba->mbox_mem_pool);
1963 return rc;
1964}
1965
1966static void
1967lpfc_mbox_abort(struct lpfc_hba * phba)
1968{
1969 LPFC_MBOXQ_t *pmbox;
1970 MAILBOX_t *mb;
1971
1972 if (phba->sli.mbox_active) {
1973 del_timer_sync(&phba->sli.mbox_tmo);
1974 phba->work_hba_events &= ~WORKER_MBOX_TMO;
1975 pmbox = phba->sli.mbox_active;
1976 mb = &pmbox->mb;
1977 phba->sli.mbox_active = NULL;
1978 if (pmbox->mbox_cmpl) {
1979 mb->mbxStatus = MBX_NOT_FINISHED;
1980 (pmbox->mbox_cmpl) (phba, pmbox);
1981 }
1982 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1983 }
1984
1985 /* Abort all the non active mailbox commands. */
1986 spin_lock_irq(phba->host->host_lock);
1987 pmbox = lpfc_mbox_get(phba);
1988 while (pmbox) {
1989 mb = &pmbox->mb;
1990 if (pmbox->mbox_cmpl) {
1991 mb->mbxStatus = MBX_NOT_FINISHED;
1992 spin_unlock_irq(phba->host->host_lock);
1993 (pmbox->mbox_cmpl) (phba, pmbox);
1994 spin_lock_irq(phba->host->host_lock);
1995 }
1996 pmbox = lpfc_mbox_get(phba);
1997 }
1998 spin_unlock_irq(phba->host->host_lock);
1999 return;
2000}
2001
2002/*! lpfc_mbox_timeout
2003 *
2004 * \pre
2005 * \post
2006 * \param hba Pointer to per struct lpfc_hba structure
2007 * \param l1 Pointer to the driver's mailbox queue.
2008 * \return
2009 * void
2010 *
2011 * \b Description:
2012 *
2013 * This routine handles mailbox timeout events at timer interrupt context.
2014 */
2015void
2016lpfc_mbox_timeout(unsigned long ptr)
2017{
2018 struct lpfc_hba *phba;
2019 unsigned long iflag;
2020
2021 phba = (struct lpfc_hba *)ptr;
2022 spin_lock_irqsave(phba->host->host_lock, iflag);
2023 if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
2024 phba->work_hba_events |= WORKER_MBOX_TMO;
2025 if (phba->work_wait)
2026 wake_up(phba->work_wait);
2027 }
2028 spin_unlock_irqrestore(phba->host->host_lock, iflag);
2029}
2030
2031void
2032lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
2033{
2034 LPFC_MBOXQ_t *pmbox;
2035 MAILBOX_t *mb;
2036
2037 spin_lock_irq(phba->host->host_lock);
2038 if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
2039 spin_unlock_irq(phba->host->host_lock);
2040 return;
2041 }
2042
James.Smart@Emulex.Comdb468d12005-06-25 10:34:08 -04002043 phba->work_hba_events &= ~WORKER_MBOX_TMO;
2044
dea31012005-04-17 16:05:31 -05002045 pmbox = phba->sli.mbox_active;
2046 mb = &pmbox->mb;
2047
2048 /* Mbox cmd <mbxCommand> timeout */
2049 lpfc_printf_log(phba,
2050 KERN_ERR,
2051 LOG_MBOX | LOG_SLI,
2052 "%d:0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
2053 phba->brd_no,
2054 mb->mbxCommand,
2055 phba->hba_state,
2056 phba->sli.sli_flag,
2057 phba->sli.mbox_active);
2058
James.Smart@Emulex.Comdb468d12005-06-25 10:34:08 -04002059 phba->sli.mbox_active = NULL;
2060 if (pmbox->mbox_cmpl) {
2061 mb->mbxStatus = MBX_NOT_FINISHED;
2062 spin_unlock_irq(phba->host->host_lock);
2063 (pmbox->mbox_cmpl) (phba, pmbox);
2064 spin_lock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -05002065 }
James.Smart@Emulex.Comdb468d12005-06-25 10:34:08 -04002066 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05002067
2068 spin_unlock_irq(phba->host->host_lock);
2069 lpfc_mbox_abort(phba);
2070 return;
2071}
2072
2073int
2074lpfc_sli_issue_mbox(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmbox, uint32_t flag)
2075{
dea31012005-04-17 16:05:31 -05002076 MAILBOX_t *mb;
2077 struct lpfc_sli *psli;
2078 uint32_t status, evtctr;
2079 uint32_t ha_copy;
2080 int i;
2081 unsigned long drvr_flag = 0;
2082 volatile uint32_t word0, ldata;
2083 void __iomem *to_slim;
2084
2085 psli = &phba->sli;
2086
2087 spin_lock_irqsave(phba->host->host_lock, drvr_flag);
2088
2089
2090 mb = &pmbox->mb;
2091 status = MBX_SUCCESS;
2092
Jamie Wellnitz41415862006-02-28 19:25:27 -05002093 if (phba->hba_state == LPFC_HBA_ERROR) {
2094 spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2095
2096 /* Mbox command <mbxCommand> cannot issue */
2097 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2098 return (MBX_NOT_FINISHED);
2099 }
2100
James Smart92908312006-03-07 15:04:13 -05002101 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
2102 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2103 spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2104 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2105 return (MBX_NOT_FINISHED);
2106 }
2107
dea31012005-04-17 16:05:31 -05002108 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
2109 /* Polling for a mbox command when another one is already active
2110 * is not allowed in SLI. Also, the driver must have established
2111 * SLI2 mode to queue and process multiple mbox commands.
2112 */
2113
2114 if (flag & MBX_POLL) {
2115 spin_unlock_irqrestore(phba->host->host_lock,
2116 drvr_flag);
2117
2118 /* Mbox command <mbxCommand> cannot issue */
2119 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2120 return (MBX_NOT_FINISHED);
2121 }
2122
2123 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
2124 spin_unlock_irqrestore(phba->host->host_lock,
2125 drvr_flag);
2126 /* Mbox command <mbxCommand> cannot issue */
2127 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2128 return (MBX_NOT_FINISHED);
2129 }
2130
2131 /* Handle STOP IOCB processing flag. This is only meaningful
2132 * if we are not polling for mbox completion.
2133 */
2134 if (flag & MBX_STOP_IOCB) {
2135 flag &= ~MBX_STOP_IOCB;
2136 /* Now flag each ring */
2137 for (i = 0; i < psli->num_rings; i++) {
2138 /* If the ring is active, flag it */
2139 if (psli->ring[i].cmdringaddr) {
2140 psli->ring[i].flag |=
2141 LPFC_STOP_IOCB_MBX;
2142 }
2143 }
2144 }
2145
2146 /* Another mailbox command is still being processed, queue this
2147 * command to be processed later.
2148 */
2149 lpfc_mbox_put(phba, pmbox);
2150
2151 /* Mbox cmd issue - BUSY */
2152 lpfc_printf_log(phba,
2153 KERN_INFO,
2154 LOG_MBOX | LOG_SLI,
2155 "%d:0308 Mbox cmd issue - BUSY Data: x%x x%x x%x x%x\n",
2156 phba->brd_no,
2157 mb->mbxCommand,
2158 phba->hba_state,
2159 psli->sli_flag,
2160 flag);
2161
2162 psli->slistat.mbox_busy++;
2163 spin_unlock_irqrestore(phba->host->host_lock,
2164 drvr_flag);
2165
2166 return (MBX_BUSY);
2167 }
2168
2169 /* Handle STOP IOCB processing flag. This is only meaningful
2170 * if we are not polling for mbox completion.
2171 */
2172 if (flag & MBX_STOP_IOCB) {
2173 flag &= ~MBX_STOP_IOCB;
2174 if (flag == MBX_NOWAIT) {
2175 /* Now flag each ring */
2176 for (i = 0; i < psli->num_rings; i++) {
2177 /* If the ring is active, flag it */
2178 if (psli->ring[i].cmdringaddr) {
2179 psli->ring[i].flag |=
2180 LPFC_STOP_IOCB_MBX;
2181 }
2182 }
2183 }
2184 }
2185
2186 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2187
2188 /* If we are not polling, we MUST be in SLI2 mode */
2189 if (flag != MBX_POLL) {
Jamie Wellnitz41415862006-02-28 19:25:27 -05002190 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
2191 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05002192 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2193 spin_unlock_irqrestore(phba->host->host_lock,
2194 drvr_flag);
2195 /* Mbox command <mbxCommand> cannot issue */
2196 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag);
2197 return (MBX_NOT_FINISHED);
2198 }
2199 /* timeout active mbox command */
2200 mod_timer(&psli->mbox_tmo, jiffies + HZ * LPFC_MBOX_TMO);
2201 }
2202
2203 /* Mailbox cmd <cmd> issue */
2204 lpfc_printf_log(phba,
2205 KERN_INFO,
2206 LOG_MBOX | LOG_SLI,
2207 "%d:0309 Mailbox cmd x%x issue Data: x%x x%x x%x\n",
2208 phba->brd_no,
2209 mb->mbxCommand,
2210 phba->hba_state,
2211 psli->sli_flag,
2212 flag);
2213
2214 psli->slistat.mbox_cmd++;
2215 evtctr = psli->slistat.mbox_event;
2216
2217 /* next set own bit for the adapter and copy over command word */
2218 mb->mbxOwner = OWN_CHIP;
2219
2220 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002221 /* First copy command data to host SLIM area */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002222 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002223 } else {
James Smart92908312006-03-07 15:04:13 -05002224 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05002225 /* copy command data into host mbox for cmpl */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002226 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
2227 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002228 }
2229
2230 /* First copy mbox command data to HBA SLIM, skip past first
2231 word */
2232 to_slim = phba->MBslimaddr + sizeof (uint32_t);
2233 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
2234 MAILBOX_CMD_SIZE - sizeof (uint32_t));
2235
2236 /* Next copy over first word, with mbxOwner set */
2237 ldata = *((volatile uint32_t *)mb);
2238 to_slim = phba->MBslimaddr;
2239 writel(ldata, to_slim);
2240 readl(to_slim); /* flush */
2241
2242 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2243 /* switch over to host mailbox */
2244 psli->sli_flag |= LPFC_SLI2_ACTIVE;
2245 }
2246 }
2247
2248 wmb();
2249 /* interrupt board to doit right away */
2250 writel(CA_MBATT, phba->CAregaddr);
2251 readl(phba->CAregaddr); /* flush */
2252
2253 switch (flag) {
2254 case MBX_NOWAIT:
2255 /* Don't wait for it to finish, just return */
2256 psli->mbox_active = pmbox;
2257 break;
2258
2259 case MBX_POLL:
2260 i = 0;
2261 psli->mbox_active = NULL;
2262 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2263 /* First read mbox status word */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002264 word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002265 word0 = le32_to_cpu(word0);
2266 } else {
2267 /* First read mbox status word */
2268 word0 = readl(phba->MBslimaddr);
2269 }
2270
2271 /* Read the HBA Host Attention Register */
2272 ha_copy = readl(phba->HAregaddr);
2273
2274 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05002275 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
2276 (!(ha_copy & HA_MBATT) &&
2277 (phba->hba_state > LPFC_WARM_START))) {
dea31012005-04-17 16:05:31 -05002278 if (i++ >= 100) {
2279 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2280 spin_unlock_irqrestore(phba->host->host_lock,
2281 drvr_flag);
2282 return (MBX_NOT_FINISHED);
2283 }
2284
2285 /* Check if we took a mbox interrupt while we were
2286 polling */
2287 if (((word0 & OWN_CHIP) != OWN_CHIP)
2288 && (evtctr != psli->slistat.mbox_event))
2289 break;
2290
2291 spin_unlock_irqrestore(phba->host->host_lock,
2292 drvr_flag);
2293
2294 /* Can be in interrupt context, do not sleep */
2295 /* (or might be called with interrupts disabled) */
2296 mdelay(i);
2297
2298 spin_lock_irqsave(phba->host->host_lock, drvr_flag);
2299
2300 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2301 /* First copy command data */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002302 word0 = *((volatile uint32_t *)
2303 &phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002304 word0 = le32_to_cpu(word0);
2305 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2306 MAILBOX_t *slimmb;
2307 volatile uint32_t slimword0;
2308 /* Check real SLIM for any errors */
2309 slimword0 = readl(phba->MBslimaddr);
2310 slimmb = (MAILBOX_t *) & slimword0;
2311 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
2312 && slimmb->mbxStatus) {
2313 psli->sli_flag &=
2314 ~LPFC_SLI2_ACTIVE;
2315 word0 = slimword0;
2316 }
2317 }
2318 } else {
2319 /* First copy command data */
2320 word0 = readl(phba->MBslimaddr);
2321 }
2322 /* Read the HBA Host Attention Register */
2323 ha_copy = readl(phba->HAregaddr);
2324 }
2325
2326 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002327 /* copy results back to user */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002328 lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
2329 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002330 } else {
2331 /* First copy command data */
2332 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2333 MAILBOX_CMD_SIZE);
2334 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2335 pmbox->context2) {
2336 lpfc_memcpy_from_slim((void *)pmbox->context2,
2337 phba->MBslimaddr + DMP_RSP_OFFSET,
2338 mb->un.varDmp.word_cnt);
2339 }
2340 }
2341
2342 writel(HA_MBATT, phba->HAregaddr);
2343 readl(phba->HAregaddr); /* flush */
2344
2345 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2346 status = mb->mbxStatus;
2347 }
2348
2349 spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2350 return (status);
2351}
2352
2353static int
2354lpfc_sli_ringtx_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2355 struct lpfc_iocbq * piocb)
2356{
2357 /* Insert the caller's iocb in the txq tail for later processing. */
2358 list_add_tail(&piocb->list, &pring->txq);
2359 pring->txq_cnt++;
2360 return (0);
2361}
2362
2363static struct lpfc_iocbq *
2364lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2365 struct lpfc_iocbq ** piocb)
2366{
2367 struct lpfc_iocbq * nextiocb;
2368
2369 nextiocb = lpfc_sli_ringtx_get(phba, pring);
2370 if (!nextiocb) {
2371 nextiocb = *piocb;
2372 *piocb = NULL;
2373 }
2374
2375 return nextiocb;
2376}
2377
2378int
2379lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2380 struct lpfc_iocbq *piocb, uint32_t flag)
2381{
2382 struct lpfc_iocbq *nextiocb;
2383 IOCB_t *iocb;
2384
2385 /*
2386 * We should never get an IOCB if we are in a < LINK_DOWN state
2387 */
2388 if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
2389 return IOCB_ERROR;
2390
2391 /*
2392 * Check to see if we are blocking IOCB processing because of a
2393 * outstanding mbox command.
2394 */
2395 if (unlikely(pring->flag & LPFC_STOP_IOCB_MBX))
2396 goto iocb_busy;
2397
2398 if (unlikely(phba->hba_state == LPFC_LINK_DOWN)) {
2399 /*
2400 * Only CREATE_XRI, CLOSE_XRI, ABORT_XRI, and QUE_RING_BUF
2401 * can be issued if the link is not up.
2402 */
2403 switch (piocb->iocb.ulpCommand) {
2404 case CMD_QUE_RING_BUF_CN:
2405 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05002406 /*
2407 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
2408 * completion, iocb_cmpl MUST be 0.
2409 */
2410 if (piocb->iocb_cmpl)
2411 piocb->iocb_cmpl = NULL;
2412 /*FALLTHROUGH*/
2413 case CMD_CREATE_XRI_CR:
2414 break;
2415 default:
2416 goto iocb_busy;
2417 }
2418
2419 /*
2420 * For FCP commands, we must be in a state where we can process link
2421 * attention events.
2422 */
2423 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
2424 !(phba->sli.sli_flag & LPFC_PROCESS_LA)))
2425 goto iocb_busy;
2426
dea31012005-04-17 16:05:31 -05002427 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2428 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
2429 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2430
2431 if (iocb)
2432 lpfc_sli_update_ring(phba, pring);
2433 else
2434 lpfc_sli_update_full_ring(phba, pring);
2435
2436 if (!piocb)
2437 return IOCB_SUCCESS;
2438
2439 goto out_busy;
2440
2441 iocb_busy:
2442 pring->stats.iocb_cmd_delay++;
2443
2444 out_busy:
2445
2446 if (!(flag & SLI_IOCB_RET_IOCB)) {
2447 lpfc_sli_ringtx_put(phba, pring, piocb);
2448 return IOCB_SUCCESS;
2449 }
2450
2451 return IOCB_BUSY;
2452}
2453
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002454static int
2455lpfc_extra_ring_setup( struct lpfc_hba *phba)
2456{
2457 struct lpfc_sli *psli;
2458 struct lpfc_sli_ring *pring;
2459
2460 psli = &phba->sli;
2461
2462 /* Adjust cmd/rsp ring iocb entries more evenly */
2463 pring = &psli->ring[psli->fcp_ring];
2464 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2465 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2466 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2467 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2468
2469 pring = &psli->ring[1];
2470 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2471 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2472 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2473 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2474
2475 /* Setup default profile for this ring */
2476 pring->iotag_max = 4096;
2477 pring->num_mask = 1;
2478 pring->prt[0].profile = 0; /* Mask 0 */
2479 pring->prt[0].rctl = FC_UNSOL_DATA;
2480 pring->prt[0].type = 5;
2481 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
2482 return 0;
2483}
2484
dea31012005-04-17 16:05:31 -05002485int
2486lpfc_sli_setup(struct lpfc_hba *phba)
2487{
2488 int i, totiocb = 0;
2489 struct lpfc_sli *psli = &phba->sli;
2490 struct lpfc_sli_ring *pring;
2491
2492 psli->num_rings = MAX_CONFIGURED_RINGS;
2493 psli->sli_flag = 0;
2494 psli->fcp_ring = LPFC_FCP_RING;
2495 psli->next_ring = LPFC_FCP_NEXT_RING;
2496 psli->ip_ring = LPFC_IP_RING;
2497
James Bottomley604a3e32005-10-29 10:28:33 -05002498 psli->iocbq_lookup = NULL;
2499 psli->iocbq_lookup_len = 0;
2500 psli->last_iotag = 0;
2501
dea31012005-04-17 16:05:31 -05002502 for (i = 0; i < psli->num_rings; i++) {
2503 pring = &psli->ring[i];
2504 switch (i) {
2505 case LPFC_FCP_RING: /* ring 0 - FCP */
2506 /* numCiocb and numRiocb are used in config_port */
2507 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
2508 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
2509 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2510 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2511 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2512 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2513 pring->iotag_ctr = 0;
2514 pring->iotag_max =
2515 (phba->cfg_hba_queue_depth * 2);
2516 pring->fast_iotag = pring->iotag_max;
2517 pring->num_mask = 0;
2518 break;
2519 case LPFC_IP_RING: /* ring 1 - IP */
2520 /* numCiocb and numRiocb are used in config_port */
2521 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
2522 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
2523 pring->num_mask = 0;
2524 break;
2525 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
2526 /* numCiocb and numRiocb are used in config_port */
2527 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
2528 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
2529 pring->fast_iotag = 0;
2530 pring->iotag_ctr = 0;
2531 pring->iotag_max = 4096;
2532 pring->num_mask = 4;
2533 pring->prt[0].profile = 0; /* Mask 0 */
2534 pring->prt[0].rctl = FC_ELS_REQ;
2535 pring->prt[0].type = FC_ELS_DATA;
2536 pring->prt[0].lpfc_sli_rcv_unsol_event =
2537 lpfc_els_unsol_event;
2538 pring->prt[1].profile = 0; /* Mask 1 */
2539 pring->prt[1].rctl = FC_ELS_RSP;
2540 pring->prt[1].type = FC_ELS_DATA;
2541 pring->prt[1].lpfc_sli_rcv_unsol_event =
2542 lpfc_els_unsol_event;
2543 pring->prt[2].profile = 0; /* Mask 2 */
2544 /* NameServer Inquiry */
2545 pring->prt[2].rctl = FC_UNSOL_CTL;
2546 /* NameServer */
2547 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
2548 pring->prt[2].lpfc_sli_rcv_unsol_event =
2549 lpfc_ct_unsol_event;
2550 pring->prt[3].profile = 0; /* Mask 3 */
2551 /* NameServer response */
2552 pring->prt[3].rctl = FC_SOL_CTL;
2553 /* NameServer */
2554 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
2555 pring->prt[3].lpfc_sli_rcv_unsol_event =
2556 lpfc_ct_unsol_event;
2557 break;
2558 }
2559 totiocb += (pring->numCiocb + pring->numRiocb);
2560 }
2561 if (totiocb > MAX_SLI2_IOCB) {
2562 /* Too many cmd / rsp ring entries in SLI2 SLIM */
2563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2564 "%d:0462 Too many cmd / rsp ring entries in "
2565 "SLI2 SLIM Data: x%x x%x\n",
2566 phba->brd_no, totiocb, MAX_SLI2_IOCB);
2567 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05002568 if (phba->cfg_multi_ring_support == 2)
2569 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05002570
2571 return 0;
2572}
2573
2574int
2575lpfc_sli_queue_setup(struct lpfc_hba * phba)
2576{
2577 struct lpfc_sli *psli;
2578 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05002579 int i;
dea31012005-04-17 16:05:31 -05002580
2581 psli = &phba->sli;
2582 spin_lock_irq(phba->host->host_lock);
2583 INIT_LIST_HEAD(&psli->mboxq);
2584 /* Initialize list headers for txq and txcmplq as double linked lists */
2585 for (i = 0; i < psli->num_rings; i++) {
2586 pring = &psli->ring[i];
2587 pring->ringno = i;
2588 pring->next_cmdidx = 0;
2589 pring->local_getidx = 0;
2590 pring->cmdidx = 0;
2591 INIT_LIST_HEAD(&pring->txq);
2592 INIT_LIST_HEAD(&pring->txcmplq);
2593 INIT_LIST_HEAD(&pring->iocb_continueq);
2594 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05002595 }
2596 spin_unlock_irq(phba->host->host_lock);
2597 return (1);
2598}
2599
2600int
2601lpfc_sli_hba_down(struct lpfc_hba * phba)
2602{
2603 struct lpfc_sli *psli;
2604 struct lpfc_sli_ring *pring;
2605 LPFC_MBOXQ_t *pmb;
2606 struct lpfc_iocbq *iocb, *next_iocb;
2607 IOCB_t *icmd = NULL;
2608 int i;
2609 unsigned long flags = 0;
2610
2611 psli = &phba->sli;
2612 lpfc_hba_down_prep(phba);
2613
2614 spin_lock_irqsave(phba->host->host_lock, flags);
2615
2616 for (i = 0; i < psli->num_rings; i++) {
2617 pring = &psli->ring[i];
2618 pring->flag |= LPFC_DEFERRED_RING_EVENT;
2619
2620 /*
2621 * Error everything on the txq since these iocbs have not been
2622 * given to the FW yet.
2623 */
2624 pring->txq_cnt = 0;
2625
2626 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2627 list_del_init(&iocb->list);
2628 if (iocb->iocb_cmpl) {
2629 icmd = &iocb->iocb;
2630 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2631 icmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2632 spin_unlock_irqrestore(phba->host->host_lock,
2633 flags);
2634 (iocb->iocb_cmpl) (phba, iocb, iocb);
2635 spin_lock_irqsave(phba->host->host_lock, flags);
James Bottomley604a3e32005-10-29 10:28:33 -05002636 } else
2637 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002638 }
2639
2640 INIT_LIST_HEAD(&(pring->txq));
2641
dea31012005-04-17 16:05:31 -05002642 }
2643
2644 spin_unlock_irqrestore(phba->host->host_lock, flags);
2645
2646 /* Return any active mbox cmds */
2647 del_timer_sync(&psli->mbox_tmo);
2648 spin_lock_irqsave(phba->host->host_lock, flags);
2649 phba->work_hba_events &= ~WORKER_MBOX_TMO;
2650 if (psli->mbox_active) {
2651 pmb = psli->mbox_active;
2652 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2653 if (pmb->mbox_cmpl) {
2654 spin_unlock_irqrestore(phba->host->host_lock, flags);
2655 pmb->mbox_cmpl(phba,pmb);
2656 spin_lock_irqsave(phba->host->host_lock, flags);
2657 }
2658 }
2659 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2660 psli->mbox_active = NULL;
2661
2662 /* Return any pending mbox cmds */
2663 while ((pmb = lpfc_mbox_get(phba)) != NULL) {
2664 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2665 if (pmb->mbox_cmpl) {
2666 spin_unlock_irqrestore(phba->host->host_lock, flags);
2667 pmb->mbox_cmpl(phba,pmb);
2668 spin_lock_irqsave(phba->host->host_lock, flags);
2669 }
2670 }
2671
2672 INIT_LIST_HEAD(&psli->mboxq);
2673
2674 spin_unlock_irqrestore(phba->host->host_lock, flags);
2675
dea31012005-04-17 16:05:31 -05002676 return 1;
2677}
2678
2679void
2680lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
2681{
2682 uint32_t *src = srcp;
2683 uint32_t *dest = destp;
2684 uint32_t ldata;
2685 int i;
2686
2687 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
2688 ldata = *src;
2689 ldata = le32_to_cpu(ldata);
2690 *dest = ldata;
2691 src++;
2692 dest++;
2693 }
2694}
2695
2696int
2697lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2698 struct lpfc_dmabuf * mp)
2699{
2700 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
2701 later */
2702 list_add_tail(&mp->list, &pring->postbufq);
2703
2704 pring->postbufq_cnt++;
2705 return 0;
2706}
2707
2708
2709struct lpfc_dmabuf *
2710lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2711 dma_addr_t phys)
2712{
2713 struct lpfc_dmabuf *mp, *next_mp;
2714 struct list_head *slp = &pring->postbufq;
2715
2716 /* Search postbufq, from the begining, looking for a match on phys */
2717 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
2718 if (mp->phys == phys) {
2719 list_del_init(&mp->list);
2720 pring->postbufq_cnt--;
2721 return mp;
2722 }
2723 }
2724
2725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2726 "%d:0410 Cannot find virtual addr for mapped buf on "
2727 "ring %d Data x%llx x%p x%p x%x\n",
2728 phba->brd_no, pring->ringno, (unsigned long long)phys,
2729 slp->next, slp->prev, pring->postbufq_cnt);
2730 return NULL;
2731}
2732
2733static void
2734lpfc_sli_abort_elsreq_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2735 struct lpfc_iocbq * rspiocb)
2736{
2737 struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2738 /* Free the resources associated with the ELS_REQUEST64 IOCB the driver
2739 * just aborted.
2740 * In this case, context2 = cmd, context2->next = rsp, context3 = bpl
2741 */
2742 if (cmdiocb->context2) {
2743 buf_ptr1 = (struct lpfc_dmabuf *) cmdiocb->context2;
2744
2745 /* Free the response IOCB before completing the abort
2746 command. */
2747 buf_ptr = NULL;
2748 list_remove_head((&buf_ptr1->list), buf_ptr,
2749 struct lpfc_dmabuf, list);
2750 if (buf_ptr) {
2751 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2752 kfree(buf_ptr);
2753 }
2754 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2755 kfree(buf_ptr1);
2756 }
2757
2758 if (cmdiocb->context3) {
2759 buf_ptr = (struct lpfc_dmabuf *) cmdiocb->context3;
2760 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2761 kfree(buf_ptr);
2762 }
2763
James Bottomley604a3e32005-10-29 10:28:33 -05002764 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05002765 return;
2766}
2767
2768int
2769lpfc_sli_issue_abort_iotag32(struct lpfc_hba * phba,
2770 struct lpfc_sli_ring * pring,
2771 struct lpfc_iocbq * cmdiocb)
2772{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002773 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05002774 IOCB_t *icmd = NULL;
2775 IOCB_t *iabt = NULL;
2776
2777 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002778 abtsiocbp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002779 if (abtsiocbp == NULL)
2780 return 0;
dea31012005-04-17 16:05:31 -05002781
2782 iabt = &abtsiocbp->iocb;
2783 icmd = &cmdiocb->iocb;
2784 switch (icmd->ulpCommand) {
2785 case CMD_ELS_REQUEST64_CR:
2786 /* Even though we abort the ELS command, the firmware may access
2787 * the BPL or other resources before it processes our
2788 * ABORT_MXRI64. Thus we must delay reusing the cmdiocb
2789 * resources till the actual abort request completes.
2790 */
2791 abtsiocbp->context1 = (void *)((unsigned long)icmd->ulpCommand);
2792 abtsiocbp->context2 = cmdiocb->context2;
2793 abtsiocbp->context3 = cmdiocb->context3;
2794 cmdiocb->context2 = NULL;
2795 cmdiocb->context3 = NULL;
2796 abtsiocbp->iocb_cmpl = lpfc_sli_abort_elsreq_cmpl;
2797 break;
2798 default:
James Bottomley604a3e32005-10-29 10:28:33 -05002799 lpfc_sli_release_iocbq(phba, abtsiocbp);
dea31012005-04-17 16:05:31 -05002800 return 0;
2801 }
2802
2803 iabt->un.amxri.abortType = ABORT_TYPE_ABTS;
2804 iabt->un.amxri.iotag32 = icmd->un.elsreq64.bdl.ulpIoTag32;
2805
2806 iabt->ulpLe = 1;
2807 iabt->ulpClass = CLASS3;
2808 iabt->ulpCommand = CMD_ABORT_MXRI64_CN;
2809
2810 if (lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0) == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05002811 lpfc_sli_release_iocbq(phba, abtsiocbp);
dea31012005-04-17 16:05:31 -05002812 return 0;
2813 }
2814
2815 return 1;
2816}
2817
2818static int
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002819lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, uint16_t tgt_id,
2820 uint64_t lun_id, uint32_t ctx,
2821 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05002822{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002823 struct lpfc_scsi_buf *lpfc_cmd;
2824 struct scsi_cmnd *cmnd;
dea31012005-04-17 16:05:31 -05002825 int rc = 1;
2826
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002827 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
2828 return rc;
2829
2830 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
2831 cmnd = lpfc_cmd->pCmd;
2832
2833 if (cmnd == NULL)
dea31012005-04-17 16:05:31 -05002834 return rc;
2835
2836 switch (ctx_cmd) {
2837 case LPFC_CTX_LUN:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002838 if ((cmnd->device->id == tgt_id) &&
2839 (cmnd->device->lun == lun_id))
dea31012005-04-17 16:05:31 -05002840 rc = 0;
2841 break;
2842 case LPFC_CTX_TGT:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002843 if (cmnd->device->id == tgt_id)
dea31012005-04-17 16:05:31 -05002844 rc = 0;
2845 break;
2846 case LPFC_CTX_CTX:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002847 if (iocbq->iocb.ulpContext == ctx)
dea31012005-04-17 16:05:31 -05002848 rc = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002849 break;
dea31012005-04-17 16:05:31 -05002850 case LPFC_CTX_HOST:
2851 rc = 0;
2852 break;
2853 default:
2854 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
2855 __FUNCTION__, ctx_cmd);
2856 break;
2857 }
2858
2859 return rc;
2860}
2861
2862int
2863lpfc_sli_sum_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2864 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd ctx_cmd)
2865{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002866 struct lpfc_iocbq *iocbq;
2867 int sum, i;
dea31012005-04-17 16:05:31 -05002868
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002869 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
2870 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05002871
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002872 if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
2873 0, ctx_cmd) == 0)
2874 sum++;
dea31012005-04-17 16:05:31 -05002875 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002876
dea31012005-04-17 16:05:31 -05002877 return sum;
2878}
2879
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04002880void
2881lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2882 struct lpfc_iocbq * rspiocb)
2883{
2884 spin_lock_irq(phba->host->host_lock);
James Bottomley604a3e32005-10-29 10:28:33 -05002885 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04002886 spin_unlock_irq(phba->host->host_lock);
2887 return;
2888}
2889
dea31012005-04-17 16:05:31 -05002890int
2891lpfc_sli_abort_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2892 uint16_t tgt_id, uint64_t lun_id, uint32_t ctx,
2893 lpfc_ctx_cmd abort_cmd)
2894{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002895 struct lpfc_iocbq *iocbq;
2896 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05002897 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05002898 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002899 int i;
dea31012005-04-17 16:05:31 -05002900
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002901 for (i = 1; i <= phba->sli.last_iotag; i++) {
2902 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05002903
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002904 if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
2905 0, abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05002906 continue;
2907
2908 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002909 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002910 if (abtsiocb == NULL) {
2911 errcnt++;
2912 continue;
2913 }
dea31012005-04-17 16:05:31 -05002914
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002915 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05002916 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
2917 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
2918 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
2919 abtsiocb->iocb.ulpLe = 1;
2920 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2921
2922 if (phba->hba_state >= LPFC_LINK_UP)
2923 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
2924 else
2925 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
2926
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04002927 /* Setup callback routine and issue the command. */
2928 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05002929 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
2930 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05002931 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05002932 errcnt++;
2933 continue;
2934 }
2935 }
2936
2937 return errcnt;
2938}
2939
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002940static void
2941lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
2942 struct lpfc_iocbq *cmdiocbq,
2943 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05002944{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002945 wait_queue_head_t *pdone_q;
2946 unsigned long iflags;
dea31012005-04-17 16:05:31 -05002947
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002948 spin_lock_irqsave(phba->host->host_lock, iflags);
2949 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
2950 if (cmdiocbq->context2 && rspiocbq)
2951 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
2952 &rspiocbq->iocb, sizeof(IOCB_t));
2953
2954 pdone_q = cmdiocbq->context_un.wait_queue;
2955 spin_unlock_irqrestore(phba->host->host_lock, iflags);
2956 if (pdone_q)
2957 wake_up(pdone_q);
dea31012005-04-17 16:05:31 -05002958 return;
2959}
2960
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002961/*
2962 * Issue the caller's iocb and wait for its completion, but no longer than the
2963 * caller's timeout. Note that iocb_flags is cleared before the
2964 * lpfc_sli_issue_call since the wake routine sets a unique value and by
2965 * definition this is a wait function.
2966 */
dea31012005-04-17 16:05:31 -05002967int
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002968lpfc_sli_issue_iocb_wait(struct lpfc_hba * phba,
2969 struct lpfc_sli_ring * pring,
2970 struct lpfc_iocbq * piocb,
2971 struct lpfc_iocbq * prspiocbq,
2972 uint32_t timeout)
dea31012005-04-17 16:05:31 -05002973{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002974 DECLARE_WAIT_QUEUE_HEAD(done_q);
2975 long timeleft, timeout_req = 0;
2976 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002977 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05002978
2979 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002980 * If the caller has provided a response iocbq buffer, then context2
2981 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05002982 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002983 if (prspiocbq) {
2984 if (piocb->context2)
2985 return IOCB_ERROR;
2986 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05002987 }
2988
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002989 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
2990 piocb->context_un.wait_queue = &done_q;
2991 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05002992
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002993 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2994 creg_val = readl(phba->HCregaddr);
2995 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
2996 writel(creg_val, phba->HCregaddr);
2997 readl(phba->HCregaddr); /* flush */
2998 }
2999
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003000 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
3001 if (retval == IOCB_SUCCESS) {
3002 timeout_req = timeout * HZ;
3003 spin_unlock_irq(phba->host->host_lock);
3004 timeleft = wait_event_timeout(done_q,
3005 piocb->iocb_flag & LPFC_IO_WAKE,
3006 timeout_req);
3007 spin_lock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -05003008
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003009 if (timeleft == 0) {
3010 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3011 "%d:0329 IOCB wait timeout error - no "
3012 "wake response Data x%x\n",
3013 phba->brd_no, timeout);
3014 retval = IOCB_TIMEDOUT;
3015 } else if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
3016 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3017 "%d:0330 IOCB wake NOT set, "
3018 "Data x%x x%lx\n", phba->brd_no,
3019 timeout, (timeleft / jiffies));
3020 retval = IOCB_TIMEDOUT;
3021 } else {
3022 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3023 "%d:0331 IOCB wake signaled\n",
3024 phba->brd_no);
dea31012005-04-17 16:05:31 -05003025 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003026 } else {
3027 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3028 "%d:0332 IOCB wait issue failed, Data x%x\n",
3029 phba->brd_no, retval);
3030 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05003031 }
3032
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003033 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3034 creg_val = readl(phba->HCregaddr);
3035 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
3036 writel(creg_val, phba->HCregaddr);
3037 readl(phba->HCregaddr); /* flush */
3038 }
3039
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003040 if (prspiocbq)
3041 piocb->context2 = NULL;
3042
3043 piocb->context_un.wait_queue = NULL;
3044 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05003045 return retval;
3046}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003047
dea31012005-04-17 16:05:31 -05003048int
3049lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq,
3050 uint32_t timeout)
3051{
3052 DECLARE_WAIT_QUEUE_HEAD(done_q);
3053 DECLARE_WAITQUEUE(wq_entry, current);
3054 uint32_t timeleft = 0;
3055 int retval;
3056
3057 /* The caller must leave context1 empty. */
3058 if (pmboxq->context1 != 0) {
3059 return (MBX_NOT_FINISHED);
3060 }
3061
3062 /* setup wake call as IOCB callback */
3063 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
3064 /* setup context field to pass wait_queue pointer to wake function */
3065 pmboxq->context1 = &done_q;
3066
3067 /* start to sleep before we wait, to avoid races */
3068 set_current_state(TASK_INTERRUPTIBLE);
3069 add_wait_queue(&done_q, &wq_entry);
3070
3071 /* now issue the command */
3072 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3073
3074 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
3075 timeleft = schedule_timeout(timeout * HZ);
3076 pmboxq->context1 = NULL;
3077 /* if schedule_timeout returns 0, we timed out and were not
3078 woken up */
Jamie Wellnitz406d6042006-02-28 19:25:20 -05003079 if ((timeleft == 0) || signal_pending(current))
dea31012005-04-17 16:05:31 -05003080 retval = MBX_TIMEOUT;
Jamie Wellnitz406d6042006-02-28 19:25:20 -05003081 else
dea31012005-04-17 16:05:31 -05003082 retval = MBX_SUCCESS;
dea31012005-04-17 16:05:31 -05003083 }
3084
3085
3086 set_current_state(TASK_RUNNING);
3087 remove_wait_queue(&done_q, &wq_entry);
3088 return retval;
3089}
3090
James Smartb4c02652006-07-06 15:50:43 -04003091int
3092lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
3093{
3094 int i = 0;
3095
3096 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !phba->stopped) {
3097 if (i++ > LPFC_MBOX_TMO * 1000)
3098 return 1;
3099
3100 if (lpfc_sli_handle_mb_event(phba) == 0)
3101 i = 0;
3102
3103 msleep(1);
3104 }
3105
3106 return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
3107}
3108
dea31012005-04-17 16:05:31 -05003109irqreturn_t
3110lpfc_intr_handler(int irq, void *dev_id, struct pt_regs * regs)
3111{
3112 struct lpfc_hba *phba;
3113 uint32_t ha_copy;
3114 uint32_t work_ha_copy;
3115 unsigned long status;
3116 int i;
3117 uint32_t control;
3118
3119 /*
3120 * Get the driver's phba structure from the dev_id and
3121 * assume the HBA is not interrupting.
3122 */
3123 phba = (struct lpfc_hba *) dev_id;
3124
3125 if (unlikely(!phba))
3126 return IRQ_NONE;
3127
3128 phba->sli.slistat.sli_intr++;
3129
3130 /*
3131 * Call the HBA to see if it is interrupting. If not, don't claim
3132 * the interrupt
3133 */
3134
3135 /* Ignore all interrupts during initialization. */
3136 if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
3137 return IRQ_NONE;
3138
3139 /*
3140 * Read host attention register to determine interrupt source
3141 * Clear Attention Sources, except Error Attention (to
3142 * preserve status) and Link Attention
3143 */
3144 spin_lock(phba->host->host_lock);
3145 ha_copy = readl(phba->HAregaddr);
3146 writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
3147 readl(phba->HAregaddr); /* flush */
3148 spin_unlock(phba->host->host_lock);
3149
3150 if (unlikely(!ha_copy))
3151 return IRQ_NONE;
3152
3153 work_ha_copy = ha_copy & phba->work_ha_mask;
3154
3155 if (unlikely(work_ha_copy)) {
3156 if (work_ha_copy & HA_LATT) {
3157 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
3158 /*
3159 * Turn off Link Attention interrupts
3160 * until CLEAR_LA done
3161 */
3162 spin_lock(phba->host->host_lock);
3163 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
3164 control = readl(phba->HCregaddr);
3165 control &= ~HC_LAINT_ENA;
3166 writel(control, phba->HCregaddr);
3167 readl(phba->HCregaddr); /* flush */
3168 spin_unlock(phba->host->host_lock);
3169 }
3170 else
3171 work_ha_copy &= ~HA_LATT;
3172 }
3173
3174 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
3175 for (i = 0; i < phba->sli.num_rings; i++) {
3176 if (work_ha_copy & (HA_RXATT << (4*i))) {
3177 /*
3178 * Turn off Slow Rings interrupts
3179 */
3180 spin_lock(phba->host->host_lock);
3181 control = readl(phba->HCregaddr);
3182 control &= ~(HC_R0INT_ENA << i);
3183 writel(control, phba->HCregaddr);
3184 readl(phba->HCregaddr); /* flush */
3185 spin_unlock(phba->host->host_lock);
3186 }
3187 }
3188 }
3189
3190 if (work_ha_copy & HA_ERATT) {
3191 phba->hba_state = LPFC_HBA_ERROR;
3192 /*
3193 * There was a link/board error. Read the
3194 * status register to retrieve the error event
3195 * and process it.
3196 */
3197 phba->sli.slistat.err_attn_event++;
3198 /* Save status info */
3199 phba->work_hs = readl(phba->HSregaddr);
3200 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
3201 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
3202
3203 /* Clear Chip error bit */
3204 writel(HA_ERATT, phba->HAregaddr);
3205 readl(phba->HAregaddr); /* flush */
James Smart92908312006-03-07 15:04:13 -05003206 phba->stopped = 1;
dea31012005-04-17 16:05:31 -05003207 }
3208
3209 spin_lock(phba->host->host_lock);
3210 phba->work_ha |= work_ha_copy;
3211 if (phba->work_wait)
3212 wake_up(phba->work_wait);
3213 spin_unlock(phba->host->host_lock);
3214 }
3215
3216 ha_copy &= ~(phba->work_ha_mask);
3217
3218 /*
3219 * Process all events on FCP ring. Take the optimized path for
3220 * FCP IO. Any other IO is slow path and is handled by
3221 * the worker thread.
3222 */
3223 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
3224 status >>= (4*LPFC_FCP_RING);
3225 if (status & HA_RXATT)
3226 lpfc_sli_handle_fast_ring_event(phba,
3227 &phba->sli.ring[LPFC_FCP_RING],
3228 status);
3229 return IRQ_HANDLED;
3230
3231} /* lpfc_intr_handler */