blob: 1f876328b44b5de7cb17fce78819c9c2b484207f [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. *
4 * Copyright (C) 2004-2005 Emulex. All rights reserved. *
5 * 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{
194 uint16_t iotag;
195
196 list_add_tail(&piocb->list, &pring->txcmplq);
197 pring->txcmplq_cnt++;
198 if (unlikely(pring->ringno == LPFC_ELS_RING))
199 mod_timer(&phba->els_tmofunc,
200 jiffies + HZ * (phba->fc_ratov << 1));
201
202 if (pring->fast_lookup) {
203 /* Setup fast lookup based on iotag for completion */
204 iotag = piocb->iocb.ulpIoTag;
205 if (iotag && (iotag < pring->fast_iotag))
206 *(pring->fast_lookup + iotag) = piocb;
207 else {
208
209 /* Cmd ring <ringno> put: iotag <iotag> greater then
210 configured max <fast_iotag> wd0 <icmd> */
211 lpfc_printf_log(phba,
212 KERN_ERR,
213 LOG_SLI,
214 "%d:0316 Cmd ring %d put: iotag x%x "
215 "greater then configured max x%x "
216 "wd0 x%x\n",
217 phba->brd_no,
218 pring->ringno, iotag,
219 pring->fast_iotag,
220 *(((uint32_t *)(&piocb->iocb)) + 7));
221 }
222 }
223 return (0);
224}
225
226static struct lpfc_iocbq *
227lpfc_sli_ringtx_get(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
228{
229 struct list_head *dlp;
230 struct lpfc_iocbq *cmd_iocb;
231
232 dlp = &pring->txq;
233 cmd_iocb = NULL;
234 list_remove_head((&pring->txq), cmd_iocb,
235 struct lpfc_iocbq,
236 list);
237 if (cmd_iocb) {
238 /* If the first ptr is not equal to the list header,
239 * deque the IOCBQ_t and return it.
240 */
241 pring->txq_cnt--;
242 }
243 return (cmd_iocb);
244}
245
246static IOCB_t *
247lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
248{
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -0400249 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -0500250 uint32_t max_cmd_idx = pring->numCiocb;
251 IOCB_t *iocb = NULL;
252
253 if ((pring->next_cmdidx == pring->cmdidx) &&
254 (++pring->next_cmdidx >= max_cmd_idx))
255 pring->next_cmdidx = 0;
256
257 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
258
259 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
260
261 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
262 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
263 "%d:0315 Ring %d issue: portCmdGet %d "
264 "is bigger then cmd ring %d\n",
265 phba->brd_no, pring->ringno,
266 pring->local_getidx, max_cmd_idx);
267
268 phba->hba_state = LPFC_HBA_ERROR;
269 /*
270 * All error attention handlers are posted to
271 * worker thread
272 */
273 phba->work_ha |= HA_ERATT;
274 phba->work_hs = HS_FFER3;
275 if (phba->work_wait)
276 wake_up(phba->work_wait);
277
278 return NULL;
279 }
280
281 if (pring->local_getidx == pring->next_cmdidx)
282 return NULL;
283 }
284
285 iocb = IOCB_ENTRY(pring->cmdringaddr, pring->cmdidx);
286
287 return iocb;
288}
289
James Bottomley604a3e32005-10-29 10:28:33 -0500290uint16_t
291lpfc_sli_next_iotag(struct lpfc_hba * phba, struct lpfc_iocbq * iocbq)
dea31012005-04-17 16:05:31 -0500292{
James Bottomley604a3e32005-10-29 10:28:33 -0500293 struct lpfc_iocbq ** new_arr;
294 struct lpfc_iocbq ** old_arr;
295 size_t new_len;
296 struct lpfc_sli *psli = &phba->sli;
297 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500298
James Bottomley604a3e32005-10-29 10:28:33 -0500299 spin_lock_irq(phba->host->host_lock);
300 iotag = psli->last_iotag;
301 if(++iotag < psli->iocbq_lookup_len) {
302 psli->last_iotag = iotag;
303 psli->iocbq_lookup[iotag] = iocbq;
304 spin_unlock_irq(phba->host->host_lock);
305 iocbq->iotag = iotag;
306 return iotag;
307 }
308 else if (psli->iocbq_lookup_len < (0xffff
309 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
310 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
311 spin_unlock_irq(phba->host->host_lock);
312 new_arr = kmalloc(new_len * sizeof (struct lpfc_iocbq *),
313 GFP_KERNEL);
314 if (new_arr) {
315 memset((char *)new_arr, 0,
316 new_len * sizeof (struct lpfc_iocbq *));
317 spin_lock_irq(phba->host->host_lock);
318 old_arr = psli->iocbq_lookup;
319 if (new_len <= psli->iocbq_lookup_len) {
320 /* highly unprobable case */
321 kfree(new_arr);
322 iotag = psli->last_iotag;
323 if(++iotag < psli->iocbq_lookup_len) {
324 psli->last_iotag = iotag;
325 psli->iocbq_lookup[iotag] = iocbq;
326 spin_unlock_irq(phba->host->host_lock);
327 iocbq->iotag = iotag;
328 return iotag;
329 }
330 spin_unlock_irq(phba->host->host_lock);
331 return 0;
332 }
333 if (psli->iocbq_lookup)
334 memcpy(new_arr, old_arr,
335 ((psli->last_iotag + 1) *
336 sizeof (struct lpfc_iocbq *)));
337 psli->iocbq_lookup = new_arr;
338 psli->iocbq_lookup_len = new_len;
339 psli->last_iotag = iotag;
340 psli->iocbq_lookup[iotag] = iocbq;
341 spin_unlock_irq(phba->host->host_lock);
342 iocbq->iotag = iotag;
343 kfree(old_arr);
344 return iotag;
345 }
dea31012005-04-17 16:05:31 -0500346 }
347
James Bottomley604a3e32005-10-29 10:28:33 -0500348 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
349 "%d:0318 Failed to allocate IOTAG.last IOTAG is %d\n",
350 phba->brd_no, psli->last_iotag);
dea31012005-04-17 16:05:31 -0500351
James Bottomley604a3e32005-10-29 10:28:33 -0500352 return 0;
dea31012005-04-17 16:05:31 -0500353}
354
355static void
356lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
357 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
358{
359 /*
James Bottomley604a3e32005-10-29 10:28:33 -0500360 * Set up an iotag
dea31012005-04-17 16:05:31 -0500361 */
James Bottomley604a3e32005-10-29 10:28:33 -0500362 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -0500363
364 /*
365 * Issue iocb command to adapter
366 */
367 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, sizeof (IOCB_t));
368 wmb();
369 pring->stats.iocb_cmd++;
370
371 /*
372 * If there is no completion routine to call, we can release the
373 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
374 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
375 */
376 if (nextiocb->iocb_cmpl)
377 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -0500378 else
379 lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -0500380
381 /*
382 * Let the HBA know what IOCB slot will be the next one the
383 * driver will put a command into.
384 */
385 pring->cmdidx = pring->next_cmdidx;
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -0400386 writel(pring->cmdidx, phba->MBslimaddr
dea31012005-04-17 16:05:31 -0500387 + (SLIMOFF + (pring->ringno * 2)) * 4);
388}
389
390static void
391lpfc_sli_update_full_ring(struct lpfc_hba * phba,
392 struct lpfc_sli_ring *pring)
393{
394 int ringno = pring->ringno;
395
396 pring->flag |= LPFC_CALL_RING_AVAILABLE;
397
398 wmb();
399
400 /*
401 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
402 * The HBA will tell us when an IOCB entry is available.
403 */
404 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
405 readl(phba->CAregaddr); /* flush */
406
407 pring->stats.iocb_cmd_full++;
408}
409
410static void
411lpfc_sli_update_ring(struct lpfc_hba * phba,
412 struct lpfc_sli_ring *pring)
413{
414 int ringno = pring->ringno;
415
416 /*
417 * Tell the HBA that there is work to do in this ring.
418 */
419 wmb();
420 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
421 readl(phba->CAregaddr); /* flush */
422}
423
424static void
425lpfc_sli_resume_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
426{
427 IOCB_t *iocb;
428 struct lpfc_iocbq *nextiocb;
429
430 /*
431 * Check to see if:
432 * (a) there is anything on the txq to send
433 * (b) link is up
434 * (c) link attention events can be processed (fcp ring only)
435 * (d) IOCB processing is not blocked by the outstanding mbox command.
436 */
437 if (pring->txq_cnt &&
438 (phba->hba_state > LPFC_LINK_DOWN) &&
439 (pring->ringno != phba->sli.fcp_ring ||
440 phba->sli.sli_flag & LPFC_PROCESS_LA) &&
441 !(pring->flag & LPFC_STOP_IOCB_MBX)) {
442
443 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
444 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
445 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
446
447 if (iocb)
448 lpfc_sli_update_ring(phba, pring);
449 else
450 lpfc_sli_update_full_ring(phba, pring);
451 }
452
453 return;
454}
455
456/* lpfc_sli_turn_on_ring is only called by lpfc_sli_handle_mb_event below */
457static void
458lpfc_sli_turn_on_ring(struct lpfc_hba * phba, int ringno)
459{
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -0400460 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[ringno];
dea31012005-04-17 16:05:31 -0500461
462 /* If the ring is active, flag it */
463 if (phba->sli.ring[ringno].cmdringaddr) {
464 if (phba->sli.ring[ringno].flag & LPFC_STOP_IOCB_MBX) {
465 phba->sli.ring[ringno].flag &= ~LPFC_STOP_IOCB_MBX;
466 /*
467 * Force update of the local copy of cmdGetInx
468 */
469 phba->sli.ring[ringno].local_getidx
470 = le32_to_cpu(pgp->cmdGetInx);
471 spin_lock_irq(phba->host->host_lock);
472 lpfc_sli_resume_iocb(phba, &phba->sli.ring[ringno]);
473 spin_unlock_irq(phba->host->host_lock);
474 }
475 }
476}
477
478static int
479lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
480{
481 uint8_t ret;
482
483 switch (mbxCommand) {
484 case MBX_LOAD_SM:
485 case MBX_READ_NV:
486 case MBX_WRITE_NV:
487 case MBX_RUN_BIU_DIAG:
488 case MBX_INIT_LINK:
489 case MBX_DOWN_LINK:
490 case MBX_CONFIG_LINK:
491 case MBX_CONFIG_RING:
492 case MBX_RESET_RING:
493 case MBX_READ_CONFIG:
494 case MBX_READ_RCONFIG:
495 case MBX_READ_SPARM:
496 case MBX_READ_STATUS:
497 case MBX_READ_RPI:
498 case MBX_READ_XRI:
499 case MBX_READ_REV:
500 case MBX_READ_LNK_STAT:
501 case MBX_REG_LOGIN:
502 case MBX_UNREG_LOGIN:
503 case MBX_READ_LA:
504 case MBX_CLEAR_LA:
505 case MBX_DUMP_MEMORY:
506 case MBX_DUMP_CONTEXT:
507 case MBX_RUN_DIAGS:
508 case MBX_RESTART:
509 case MBX_UPDATE_CFG:
510 case MBX_DOWN_LOAD:
511 case MBX_DEL_LD_ENTRY:
512 case MBX_RUN_PROGRAM:
513 case MBX_SET_MASK:
514 case MBX_SET_SLIM:
515 case MBX_UNREG_D_ID:
516 case MBX_CONFIG_FARP:
517 case MBX_LOAD_AREA:
518 case MBX_RUN_BIU_DIAG64:
519 case MBX_CONFIG_PORT:
520 case MBX_READ_SPARM64:
521 case MBX_READ_RPI64:
522 case MBX_REG_LOGIN64:
523 case MBX_READ_LA64:
524 case MBX_FLASH_WR_ULA:
525 case MBX_SET_DEBUG:
526 case MBX_LOAD_EXP_ROM:
527 ret = mbxCommand;
528 break;
529 default:
530 ret = MBX_SHUTDOWN;
531 break;
532 }
533 return (ret);
534}
535static void
536lpfc_sli_wake_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
537{
538 wait_queue_head_t *pdone_q;
539
540 /*
541 * If pdone_q is empty, the driver thread gave up waiting and
542 * continued running.
543 */
544 pdone_q = (wait_queue_head_t *) pmboxq->context1;
545 if (pdone_q)
546 wake_up_interruptible(pdone_q);
547 return;
548}
549
550void
551lpfc_sli_def_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
552{
553 struct lpfc_dmabuf *mp;
554 mp = (struct lpfc_dmabuf *) (pmb->context1);
555 if (mp) {
556 lpfc_mbuf_free(phba, mp->virt, mp->phys);
557 kfree(mp);
558 }
559 mempool_free( pmb, phba->mbox_mem_pool);
560 return;
561}
562
563int
564lpfc_sli_handle_mb_event(struct lpfc_hba * phba)
565{
566 MAILBOX_t *mbox;
567 MAILBOX_t *pmbox;
568 LPFC_MBOXQ_t *pmb;
569 struct lpfc_sli *psli;
570 int i, rc;
571 uint32_t process_next;
572
573 psli = &phba->sli;
574 /* We should only get here if we are in SLI2 mode */
575 if (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE)) {
576 return (1);
577 }
578
579 phba->sli.slistat.mbox_event++;
580
581 /* Get a Mailbox buffer to setup mailbox commands for callback */
582 if ((pmb = phba->sli.mbox_active)) {
583 pmbox = &pmb->mb;
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -0400584 mbox = &phba->slim2p->mbx;
dea31012005-04-17 16:05:31 -0500585
586 /* First check out the status word */
587 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof (uint32_t));
588
589 /* Sanity check to ensure the host owns the mailbox */
590 if (pmbox->mbxOwner != OWN_HOST) {
591 /* Lets try for a while */
592 for (i = 0; i < 10240; i++) {
593 /* First copy command data */
594 lpfc_sli_pcimem_bcopy(mbox, pmbox,
595 sizeof (uint32_t));
596 if (pmbox->mbxOwner == OWN_HOST)
597 goto mbout;
598 }
599 /* Stray Mailbox Interrupt, mbxCommand <cmd> mbxStatus
600 <status> */
601 lpfc_printf_log(phba,
602 KERN_ERR,
603 LOG_MBOX | LOG_SLI,
604 "%d:0304 Stray Mailbox Interrupt "
605 "mbxCommand x%x mbxStatus x%x\n",
606 phba->brd_no,
607 pmbox->mbxCommand,
608 pmbox->mbxStatus);
609
610 spin_lock_irq(phba->host->host_lock);
611 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
612 spin_unlock_irq(phba->host->host_lock);
613 return (1);
614 }
615
616 mbout:
617 del_timer_sync(&phba->sli.mbox_tmo);
618 phba->work_hba_events &= ~WORKER_MBOX_TMO;
619
620 /*
621 * It is a fatal error if unknown mbox command completion.
622 */
623 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
624 MBX_SHUTDOWN) {
625
626 /* Unknow mailbox command compl */
627 lpfc_printf_log(phba,
628 KERN_ERR,
629 LOG_MBOX | LOG_SLI,
630 "%d:0323 Unknown Mailbox command %x Cmpl\n",
631 phba->brd_no,
632 pmbox->mbxCommand);
633 phba->hba_state = LPFC_HBA_ERROR;
634 phba->work_hs = HS_FFER3;
635 lpfc_handle_eratt(phba);
636 return (0);
637 }
638
639 phba->sli.mbox_active = NULL;
640 if (pmbox->mbxStatus) {
641 phba->sli.slistat.mbox_stat_err++;
642 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
643 /* Mbox cmd cmpl error - RETRYing */
644 lpfc_printf_log(phba,
645 KERN_INFO,
646 LOG_MBOX | LOG_SLI,
647 "%d:0305 Mbox cmd cmpl error - "
648 "RETRYing Data: x%x x%x x%x x%x\n",
649 phba->brd_no,
650 pmbox->mbxCommand,
651 pmbox->mbxStatus,
652 pmbox->un.varWords[0],
653 phba->hba_state);
654 pmbox->mbxStatus = 0;
655 pmbox->mbxOwner = OWN_HOST;
656 spin_lock_irq(phba->host->host_lock);
657 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
658 spin_unlock_irq(phba->host->host_lock);
659 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
660 if (rc == MBX_SUCCESS)
661 return (0);
662 }
663 }
664
665 /* Mailbox cmd <cmd> Cmpl <cmpl> */
666 lpfc_printf_log(phba,
667 KERN_INFO,
668 LOG_MBOX | LOG_SLI,
669 "%d:0307 Mailbox cmd x%x Cmpl x%p "
670 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
671 phba->brd_no,
672 pmbox->mbxCommand,
673 pmb->mbox_cmpl,
674 *((uint32_t *) pmbox),
675 pmbox->un.varWords[0],
676 pmbox->un.varWords[1],
677 pmbox->un.varWords[2],
678 pmbox->un.varWords[3],
679 pmbox->un.varWords[4],
680 pmbox->un.varWords[5],
681 pmbox->un.varWords[6],
682 pmbox->un.varWords[7]);
683
684 if (pmb->mbox_cmpl) {
685 lpfc_sli_pcimem_bcopy(mbox, pmbox, MAILBOX_CMD_SIZE);
686 pmb->mbox_cmpl(phba,pmb);
687 }
688 }
689
690
691 do {
692 process_next = 0; /* by default don't loop */
693 spin_lock_irq(phba->host->host_lock);
694 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
695
696 /* Process next mailbox command if there is one */
697 if ((pmb = lpfc_mbox_get(phba))) {
698 spin_unlock_irq(phba->host->host_lock);
699 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
700 if (rc == MBX_NOT_FINISHED) {
701 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
702 pmb->mbox_cmpl(phba,pmb);
703 process_next = 1;
704 continue; /* loop back */
705 }
706 } else {
707 spin_unlock_irq(phba->host->host_lock);
708 /* Turn on IOCB processing */
709 for (i = 0; i < phba->sli.num_rings; i++) {
710 lpfc_sli_turn_on_ring(phba, i);
711 }
712
713 /* Free any lpfc_dmabuf's waiting for mbox cmd cmpls */
714 while (!list_empty(&phba->freebufList)) {
715 struct lpfc_dmabuf *mp;
716
717 mp = NULL;
718 list_remove_head((&phba->freebufList),
719 mp,
720 struct lpfc_dmabuf,
721 list);
722 if (mp) {
723 lpfc_mbuf_free(phba, mp->virt,
724 mp->phys);
725 kfree(mp);
726 }
727 }
728 }
729
730 } while (process_next);
731
732 return (0);
733}
734static int
735lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
736 struct lpfc_iocbq *saveq)
737{
738 IOCB_t * irsp;
739 WORD5 * w5p;
740 uint32_t Rctl, Type;
741 uint32_t match, i;
742
743 match = 0;
744 irsp = &(saveq->iocb);
745 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
746 || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)) {
747 Rctl = FC_ELS_REQ;
748 Type = FC_ELS_DATA;
749 } else {
750 w5p =
751 (WORD5 *) & (saveq->iocb.un.
752 ulpWord[5]);
753 Rctl = w5p->hcsw.Rctl;
754 Type = w5p->hcsw.Type;
755
756 /* Firmware Workaround */
757 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
758 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX)) {
759 Rctl = FC_ELS_REQ;
760 Type = FC_ELS_DATA;
761 w5p->hcsw.Rctl = Rctl;
762 w5p->hcsw.Type = Type;
763 }
764 }
765 /* unSolicited Responses */
766 if (pring->prt[0].profile) {
767 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring, saveq);
768 match = 1;
769 } else {
770 /* We must search, based on rctl / type
771 for the right routine */
772 for (i = 0; i < pring->num_mask;
773 i++) {
774 if ((pring->prt[i].rctl ==
775 Rctl)
776 && (pring->prt[i].
777 type == Type)) {
778 (pring->prt[i].lpfc_sli_rcv_unsol_event)
779 (phba, pring, saveq);
780 match = 1;
781 break;
782 }
783 }
784 }
785 if (match == 0) {
786 /* Unexpected Rctl / Type received */
787 /* Ring <ringno> handler: unexpected
788 Rctl <Rctl> Type <Type> received */
789 lpfc_printf_log(phba,
790 KERN_WARNING,
791 LOG_SLI,
792 "%d:0313 Ring %d handler: unexpected Rctl x%x "
793 "Type x%x received \n",
794 phba->brd_no,
795 pring->ringno,
796 Rctl,
797 Type);
798 }
799 return(1);
800}
801
802static struct lpfc_iocbq *
James Bottomley604a3e32005-10-29 10:28:33 -0500803lpfc_sli_iocbq_lookup(struct lpfc_hba * phba,
804 struct lpfc_sli_ring * pring,
805 struct lpfc_iocbq * prspiocb)
dea31012005-04-17 16:05:31 -0500806{
dea31012005-04-17 16:05:31 -0500807 struct lpfc_iocbq *cmd_iocb = NULL;
808 uint16_t iotag;
809
James Bottomley604a3e32005-10-29 10:28:33 -0500810 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -0500811
James Bottomley604a3e32005-10-29 10:28:33 -0500812 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
813 cmd_iocb = phba->sli.iocbq_lookup[iotag];
814 list_del(&cmd_iocb->list);
815 pring->txcmplq_cnt--;
816 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500817 }
818
dea31012005-04-17 16:05:31 -0500819 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Bottomley604a3e32005-10-29 10:28:33 -0500820 "%d:0317 iotag x%x is out off "
821 "range: max iotag x%x wd0 x%x\n",
822 phba->brd_no, iotag,
823 phba->sli.last_iotag,
824 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -0500825 return NULL;
826}
827
828static int
829lpfc_sli_process_sol_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
830 struct lpfc_iocbq *saveq)
831{
832 struct lpfc_iocbq * cmdiocbp;
833 int rc = 1;
834 unsigned long iflag;
835
836 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
837 spin_lock_irqsave(phba->host->host_lock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -0500838 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
dea31012005-04-17 16:05:31 -0500839 if (cmdiocbp) {
840 if (cmdiocbp->iocb_cmpl) {
841 /*
842 * Post all ELS completions to the worker thread.
843 * All other are passed to the completion callback.
844 */
845 if (pring->ringno == LPFC_ELS_RING) {
846 spin_unlock_irqrestore(phba->host->host_lock,
847 iflag);
848 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
849 spin_lock_irqsave(phba->host->host_lock, iflag);
850 }
851 else {
dea31012005-04-17 16:05:31 -0500852 spin_unlock_irqrestore(phba->host->host_lock,
853 iflag);
854 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
855 spin_lock_irqsave(phba->host->host_lock, iflag);
856 }
James Bottomley604a3e32005-10-29 10:28:33 -0500857 } else
858 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -0500859 } else {
860 /*
861 * Unknown initiating command based on the response iotag.
862 * This could be the case on the ELS ring because of
863 * lpfc_els_abort().
864 */
865 if (pring->ringno != LPFC_ELS_RING) {
866 /*
867 * Ring <ringno> handler: unexpected completion IoTag
868 * <IoTag>
869 */
870 lpfc_printf_log(phba,
871 KERN_WARNING,
872 LOG_SLI,
873 "%d:0322 Ring %d handler: unexpected "
874 "completion IoTag x%x Data: x%x x%x x%x x%x\n",
875 phba->brd_no,
876 pring->ringno,
877 saveq->iocb.ulpIoTag,
878 saveq->iocb.ulpStatus,
879 saveq->iocb.un.ulpWord[4],
880 saveq->iocb.ulpCommand,
881 saveq->iocb.ulpContext);
882 }
883 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400884
dea31012005-04-17 16:05:31 -0500885 spin_unlock_irqrestore(phba->host->host_lock, iflag);
886 return rc;
887}
888
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500889static void lpfc_sli_rsp_pointers_error(struct lpfc_hba * phba,
890 struct lpfc_sli_ring * pring)
891{
892 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
893 /*
894 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
895 * rsp ring <portRspMax>
896 */
897 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
898 "%d:0312 Ring %d handler: portRspPut %d "
899 "is bigger then rsp ring %d\n",
900 phba->brd_no, pring->ringno,
901 le32_to_cpu(pgp->rspPutInx),
902 pring->numRiocb);
903
904 phba->hba_state = LPFC_HBA_ERROR;
905
906 /*
907 * All error attention handlers are posted to
908 * worker thread
909 */
910 phba->work_ha |= HA_ERATT;
911 phba->work_hs = HS_FFER3;
912 if (phba->work_wait)
913 wake_up(phba->work_wait);
914
915 return;
916}
917
918void lpfc_sli_poll_fcp_ring(struct lpfc_hba * phba)
919{
920 struct lpfc_sli * psli = &phba->sli;
921 struct lpfc_sli_ring * pring = &psli->ring[LPFC_FCP_RING];
922 IOCB_t *irsp = NULL;
923 IOCB_t *entry = NULL;
924 struct lpfc_iocbq *cmdiocbq = NULL;
925 struct lpfc_iocbq rspiocbq;
926 struct lpfc_pgp *pgp;
927 uint32_t status;
928 uint32_t portRspPut, portRspMax;
929 int type;
930 uint32_t rsp_cmpl = 0;
931 void __iomem *to_slim;
932 uint32_t ha_copy;
933
934 pring->stats.iocb_event++;
935
936 /* The driver assumes SLI-2 mode */
937 pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
938
939 /*
940 * The next available response entry should never exceed the maximum
941 * entries. If it does, treat it as an adapter hardware error.
942 */
943 portRspMax = pring->numRiocb;
944 portRspPut = le32_to_cpu(pgp->rspPutInx);
945 if (unlikely(portRspPut >= portRspMax)) {
946 lpfc_sli_rsp_pointers_error(phba, pring);
947 return;
948 }
949
950 rmb();
951 while (pring->rspidx != portRspPut) {
952
953 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
954
955 if (++pring->rspidx >= portRspMax)
956 pring->rspidx = 0;
957
958 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
959 (uint32_t *) &rspiocbq.iocb,
960 sizeof (IOCB_t));
961 irsp = &rspiocbq.iocb;
962 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
963 pring->stats.iocb_rsp++;
964 rsp_cmpl++;
965
966 if (unlikely(irsp->ulpStatus)) {
967 /* Rsp ring <ringno> error: IOCB */
968 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
969 "%d:0326 Rsp Ring %d error: IOCB Data: "
970 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
971 phba->brd_no, pring->ringno,
972 irsp->un.ulpWord[0],
973 irsp->un.ulpWord[1],
974 irsp->un.ulpWord[2],
975 irsp->un.ulpWord[3],
976 irsp->un.ulpWord[4],
977 irsp->un.ulpWord[5],
978 *(((uint32_t *) irsp) + 6),
979 *(((uint32_t *) irsp) + 7));
980 }
981
982 switch (type) {
983 case LPFC_ABORT_IOCB:
984 case LPFC_SOL_IOCB:
985 /*
986 * Idle exchange closed via ABTS from port. No iocb
987 * resources need to be recovered.
988 */
989 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
990 printk(KERN_INFO "%s: IOCB cmd 0x%x processed."
991 " Skipping completion\n", __FUNCTION__,
992 irsp->ulpCommand);
993 break;
994 }
995
996 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
997 &rspiocbq);
998 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
999 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1000 &rspiocbq);
1001 }
1002 break;
1003 default:
1004 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1005 char adaptermsg[LPFC_MAX_ADPTMSG];
1006 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1007 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1008 MAX_MSG_DATA);
1009 dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
1010 phba->brd_no, adaptermsg);
1011 } else {
1012 /* Unknown IOCB command */
1013 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1014 "%d:0321 Unknown IOCB command "
1015 "Data: x%x, x%x x%x x%x x%x\n",
1016 phba->brd_no, type,
1017 irsp->ulpCommand,
1018 irsp->ulpStatus,
1019 irsp->ulpIoTag,
1020 irsp->ulpContext);
1021 }
1022 break;
1023 }
1024
1025 /*
1026 * The response IOCB has been processed. Update the ring
1027 * pointer in SLIM. If the port response put pointer has not
1028 * been updated, sync the pgp->rspPutInx and fetch the new port
1029 * response put pointer.
1030 */
1031 to_slim = phba->MBslimaddr +
1032 (SLIMOFF + (pring->ringno * 2) + 1) * 4;
1033 writeb(pring->rspidx, to_slim);
1034
1035 if (pring->rspidx == portRspPut)
1036 portRspPut = le32_to_cpu(pgp->rspPutInx);
1037 }
1038
1039 ha_copy = readl(phba->HAregaddr);
1040 ha_copy >>= (LPFC_FCP_RING * 4);
1041
1042 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
1043 pring->stats.iocb_rsp_full++;
1044 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1045 writel(status, phba->CAregaddr);
1046 readl(phba->CAregaddr);
1047 }
1048 if ((ha_copy & HA_R0CE_RSP) &&
1049 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1050 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1051 pring->stats.iocb_cmd_empty++;
1052
1053 /* Force update of the local copy of cmdGetInx */
1054 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1055 lpfc_sli_resume_iocb(phba, pring);
1056
1057 if ((pring->lpfc_sli_cmd_available))
1058 (pring->lpfc_sli_cmd_available) (phba, pring);
1059
1060 }
1061
1062 return;
1063}
1064
dea31012005-04-17 16:05:31 -05001065/*
1066 * This routine presumes LPFC_FCP_RING handling and doesn't bother
1067 * to check it explicitly.
1068 */
1069static int
1070lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,
1071 struct lpfc_sli_ring * pring, uint32_t mask)
1072{
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04001073 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001074 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001075 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05001076 struct lpfc_iocbq *cmdiocbq = NULL;
1077 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05001078 uint32_t status;
1079 uint32_t portRspPut, portRspMax;
1080 int rc = 1;
1081 lpfc_iocb_type type;
1082 unsigned long iflag;
1083 uint32_t rsp_cmpl = 0;
1084 void __iomem *to_slim;
1085
1086 spin_lock_irqsave(phba->host->host_lock, iflag);
1087 pring->stats.iocb_event++;
1088
dea31012005-04-17 16:05:31 -05001089 /*
1090 * The next available response entry should never exceed the maximum
1091 * entries. If it does, treat it as an adapter hardware error.
1092 */
1093 portRspMax = pring->numRiocb;
1094 portRspPut = le32_to_cpu(pgp->rspPutInx);
1095 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001096 lpfc_sli_rsp_pointers_error(phba, pring);
dea31012005-04-17 16:05:31 -05001097 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1098 return 1;
1099 }
1100
1101 rmb();
1102 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001103 /*
1104 * Fetch an entry off the ring and copy it into a local data
1105 * structure. The copy involves a byte-swap since the
1106 * network byte order and pci byte orders are different.
1107 */
James.Smart@Emulex.Com4a0dfcd2005-10-28 20:29:56 -04001108 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001109
1110 if (++pring->rspidx >= portRspMax)
1111 pring->rspidx = 0;
1112
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04001113 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1114 (uint32_t *) &rspiocbq.iocb,
1115 sizeof (IOCB_t));
1116 irsp = &rspiocbq.iocb;
1117
dea31012005-04-17 16:05:31 -05001118 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1119 pring->stats.iocb_rsp++;
1120 rsp_cmpl++;
1121
1122 if (unlikely(irsp->ulpStatus)) {
1123 /* Rsp ring <ringno> error: IOCB */
1124 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1125 "%d:0326 Rsp Ring %d error: IOCB Data: "
1126 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1127 phba->brd_no, pring->ringno,
1128 irsp->un.ulpWord[0], irsp->un.ulpWord[1],
1129 irsp->un.ulpWord[2], irsp->un.ulpWord[3],
1130 irsp->un.ulpWord[4], irsp->un.ulpWord[5],
1131 *(((uint32_t *) irsp) + 6),
1132 *(((uint32_t *) irsp) + 7));
1133 }
1134
1135 switch (type) {
1136 case LPFC_ABORT_IOCB:
1137 case LPFC_SOL_IOCB:
1138 /*
1139 * Idle exchange closed via ABTS from port. No iocb
1140 * resources need to be recovered.
1141 */
1142 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1143 printk(KERN_INFO "%s: IOCB cmd 0x%x processed. "
1144 "Skipping completion\n", __FUNCTION__,
1145 irsp->ulpCommand);
1146 break;
1147 }
1148
James Bottomley604a3e32005-10-29 10:28:33 -05001149 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1150 &rspiocbq);
dea31012005-04-17 16:05:31 -05001151 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1152 spin_unlock_irqrestore(
1153 phba->host->host_lock, iflag);
1154 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1155 &rspiocbq);
1156 spin_lock_irqsave(phba->host->host_lock,
1157 iflag);
1158 }
1159 break;
1160 default:
1161 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1162 char adaptermsg[LPFC_MAX_ADPTMSG];
1163 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1164 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1165 MAX_MSG_DATA);
1166 dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
1167 phba->brd_no, adaptermsg);
1168 } else {
1169 /* Unknown IOCB command */
1170 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1171 "%d:0321 Unknown IOCB command "
1172 "Data: x%x, x%x x%x x%x x%x\n",
1173 phba->brd_no, type, irsp->ulpCommand,
1174 irsp->ulpStatus, irsp->ulpIoTag,
1175 irsp->ulpContext);
1176 }
1177 break;
1178 }
1179
1180 /*
1181 * The response IOCB has been processed. Update the ring
1182 * pointer in SLIM. If the port response put pointer has not
1183 * been updated, sync the pgp->rspPutInx and fetch the new port
1184 * response put pointer.
1185 */
dea31012005-04-17 16:05:31 -05001186 to_slim = phba->MBslimaddr +
1187 (SLIMOFF + (pring->ringno * 2) + 1) * 4;
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -04001188 writel(pring->rspidx, to_slim);
dea31012005-04-17 16:05:31 -05001189
1190 if (pring->rspidx == portRspPut)
1191 portRspPut = le32_to_cpu(pgp->rspPutInx);
1192 }
1193
1194 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
1195 pring->stats.iocb_rsp_full++;
1196 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1197 writel(status, phba->CAregaddr);
1198 readl(phba->CAregaddr);
1199 }
1200 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1201 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1202 pring->stats.iocb_cmd_empty++;
1203
1204 /* Force update of the local copy of cmdGetInx */
1205 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1206 lpfc_sli_resume_iocb(phba, pring);
1207
1208 if ((pring->lpfc_sli_cmd_available))
1209 (pring->lpfc_sli_cmd_available) (phba, pring);
1210
1211 }
1212
1213 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1214 return rc;
1215}
1216
1217
1218int
1219lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,
1220 struct lpfc_sli_ring * pring, uint32_t mask)
1221{
1222 IOCB_t *entry;
1223 IOCB_t *irsp = NULL;
1224 struct lpfc_iocbq *rspiocbp = NULL;
1225 struct lpfc_iocbq *next_iocb;
1226 struct lpfc_iocbq *cmdiocbp;
1227 struct lpfc_iocbq *saveq;
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04001228 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
dea31012005-04-17 16:05:31 -05001229 uint8_t iocb_cmd_type;
1230 lpfc_iocb_type type;
1231 uint32_t status, free_saveq;
1232 uint32_t portRspPut, portRspMax;
1233 int rc = 1;
1234 unsigned long iflag;
1235 void __iomem *to_slim;
1236
1237 spin_lock_irqsave(phba->host->host_lock, iflag);
1238 pring->stats.iocb_event++;
1239
dea31012005-04-17 16:05:31 -05001240 /*
1241 * The next available response entry should never exceed the maximum
1242 * entries. If it does, treat it as an adapter hardware error.
1243 */
1244 portRspMax = pring->numRiocb;
1245 portRspPut = le32_to_cpu(pgp->rspPutInx);
1246 if (portRspPut >= portRspMax) {
1247 /*
1248 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1249 * rsp ring <portRspMax>
1250 */
1251 lpfc_printf_log(phba,
1252 KERN_ERR,
1253 LOG_SLI,
1254 "%d:0312 Ring %d handler: portRspPut %d "
1255 "is bigger then rsp ring %d\n",
1256 phba->brd_no,
1257 pring->ringno, portRspPut, portRspMax);
1258
1259 phba->hba_state = LPFC_HBA_ERROR;
1260 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1261
1262 phba->work_hs = HS_FFER3;
1263 lpfc_handle_eratt(phba);
1264
1265 return 1;
1266 }
1267
1268 rmb();
dea31012005-04-17 16:05:31 -05001269 while (pring->rspidx != portRspPut) {
1270 /*
1271 * Build a completion list and call the appropriate handler.
1272 * The process is to get the next available response iocb, get
1273 * a free iocb from the list, copy the response data into the
1274 * free iocb, insert to the continuation list, and update the
1275 * next response index to slim. This process makes response
1276 * iocb's in the ring available to DMA as fast as possible but
1277 * pays a penalty for a copy operation. Since the iocb is
1278 * only 32 bytes, this penalty is considered small relative to
1279 * the PCI reads for register values and a slim write. When
1280 * the ulpLe field is set, the entire Command has been
1281 * received.
1282 */
1283 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001284 rspiocbp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001285 if (rspiocbp == NULL) {
1286 printk(KERN_ERR "%s: out of buffers! Failing "
1287 "completion.\n", __FUNCTION__);
1288 break;
1289 }
1290
1291 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb, sizeof (IOCB_t));
1292 irsp = &rspiocbp->iocb;
1293
1294 if (++pring->rspidx >= portRspMax)
1295 pring->rspidx = 0;
1296
1297 to_slim = phba->MBslimaddr + (SLIMOFF + (pring->ringno * 2)
1298 + 1) * 4;
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -04001299 writel(pring->rspidx, to_slim);
dea31012005-04-17 16:05:31 -05001300
1301 if (list_empty(&(pring->iocb_continueq))) {
1302 list_add(&rspiocbp->list, &(pring->iocb_continueq));
1303 } else {
1304 list_add_tail(&rspiocbp->list,
1305 &(pring->iocb_continueq));
1306 }
1307
1308 pring->iocb_continueq_cnt++;
1309 if (irsp->ulpLe) {
1310 /*
1311 * By default, the driver expects to free all resources
1312 * associated with this iocb completion.
1313 */
1314 free_saveq = 1;
1315 saveq = list_get_first(&pring->iocb_continueq,
1316 struct lpfc_iocbq, list);
1317 irsp = &(saveq->iocb);
1318 list_del_init(&pring->iocb_continueq);
1319 pring->iocb_continueq_cnt = 0;
1320
1321 pring->stats.iocb_rsp++;
1322
1323 if (irsp->ulpStatus) {
1324 /* Rsp ring <ringno> error: IOCB */
1325 lpfc_printf_log(phba,
1326 KERN_WARNING,
1327 LOG_SLI,
1328 "%d:0328 Rsp Ring %d error: IOCB Data: "
1329 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1330 phba->brd_no,
1331 pring->ringno,
1332 irsp->un.ulpWord[0],
1333 irsp->un.ulpWord[1],
1334 irsp->un.ulpWord[2],
1335 irsp->un.ulpWord[3],
1336 irsp->un.ulpWord[4],
1337 irsp->un.ulpWord[5],
1338 *(((uint32_t *) irsp) + 6),
1339 *(((uint32_t *) irsp) + 7));
1340 }
1341
1342 /*
1343 * Fetch the IOCB command type and call the correct
1344 * completion routine. Solicited and Unsolicited
1345 * IOCBs on the ELS ring get freed back to the
1346 * lpfc_iocb_list by the discovery kernel thread.
1347 */
1348 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
1349 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
1350 if (type == LPFC_SOL_IOCB) {
1351 spin_unlock_irqrestore(phba->host->host_lock,
1352 iflag);
1353 rc = lpfc_sli_process_sol_iocb(phba, pring,
1354 saveq);
1355 spin_lock_irqsave(phba->host->host_lock, iflag);
1356 } else if (type == LPFC_UNSOL_IOCB) {
1357 spin_unlock_irqrestore(phba->host->host_lock,
1358 iflag);
1359 rc = lpfc_sli_process_unsol_iocb(phba, pring,
1360 saveq);
1361 spin_lock_irqsave(phba->host->host_lock, iflag);
1362 } else if (type == LPFC_ABORT_IOCB) {
1363 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
1364 ((cmdiocbp =
James Bottomley604a3e32005-10-29 10:28:33 -05001365 lpfc_sli_iocbq_lookup(phba, pring,
1366 saveq)))) {
dea31012005-04-17 16:05:31 -05001367 /* Call the specified completion
1368 routine */
1369 if (cmdiocbp->iocb_cmpl) {
1370 spin_unlock_irqrestore(
1371 phba->host->host_lock,
1372 iflag);
1373 (cmdiocbp->iocb_cmpl) (phba,
1374 cmdiocbp, saveq);
1375 spin_lock_irqsave(
1376 phba->host->host_lock,
1377 iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05001378 } else
1379 lpfc_sli_release_iocbq(phba,
1380 cmdiocbp);
dea31012005-04-17 16:05:31 -05001381 }
1382 } else if (type == LPFC_UNKNOWN_IOCB) {
1383 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1384
1385 char adaptermsg[LPFC_MAX_ADPTMSG];
1386
1387 memset(adaptermsg, 0,
1388 LPFC_MAX_ADPTMSG);
1389 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1390 MAX_MSG_DATA);
1391 dev_warn(&((phba->pcidev)->dev),
1392 "lpfc%d: %s",
1393 phba->brd_no, adaptermsg);
1394 } else {
1395 /* Unknown IOCB command */
1396 lpfc_printf_log(phba,
1397 KERN_ERR,
1398 LOG_SLI,
1399 "%d:0321 Unknown IOCB command "
1400 "Data: x%x x%x x%x x%x\n",
1401 phba->brd_no,
1402 irsp->ulpCommand,
1403 irsp->ulpStatus,
1404 irsp->ulpIoTag,
1405 irsp->ulpContext);
1406 }
1407 }
1408
1409 if (free_saveq) {
1410 if (!list_empty(&saveq->list)) {
1411 list_for_each_entry_safe(rspiocbp,
1412 next_iocb,
1413 &saveq->list,
1414 list) {
James Bottomley604a3e32005-10-29 10:28:33 -05001415 lpfc_sli_release_iocbq(phba,
1416 rspiocbp);
dea31012005-04-17 16:05:31 -05001417 }
1418 }
1419
James Bottomley604a3e32005-10-29 10:28:33 -05001420 lpfc_sli_release_iocbq(phba, saveq);
dea31012005-04-17 16:05:31 -05001421 }
1422 }
1423
1424 /*
1425 * If the port response put pointer has not been updated, sync
1426 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
1427 * response put pointer.
1428 */
1429 if (pring->rspidx == portRspPut) {
1430 portRspPut = le32_to_cpu(pgp->rspPutInx);
1431 }
1432 } /* while (pring->rspidx != portRspPut) */
1433
1434 if ((rspiocbp != 0) && (mask & HA_R0RE_REQ)) {
1435 /* At least one response entry has been freed */
1436 pring->stats.iocb_rsp_full++;
1437 /* SET RxRE_RSP in Chip Att register */
1438 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1439 writel(status, phba->CAregaddr);
1440 readl(phba->CAregaddr); /* flush */
1441 }
1442 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1443 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1444 pring->stats.iocb_cmd_empty++;
1445
1446 /* Force update of the local copy of cmdGetInx */
1447 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1448 lpfc_sli_resume_iocb(phba, pring);
1449
1450 if ((pring->lpfc_sli_cmd_available))
1451 (pring->lpfc_sli_cmd_available) (phba, pring);
1452
1453 }
1454
1455 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1456 return rc;
1457}
1458
1459int
1460lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1461{
1462 struct lpfc_iocbq *iocb, *next_iocb;
1463 IOCB_t *icmd = NULL, *cmd = NULL;
1464 int errcnt;
dea31012005-04-17 16:05:31 -05001465
1466 errcnt = 0;
1467
1468 /* Error everything on txq and txcmplq
1469 * First do the txq.
1470 */
1471 spin_lock_irq(phba->host->host_lock);
1472 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
1473 list_del_init(&iocb->list);
1474 if (iocb->iocb_cmpl) {
1475 icmd = &iocb->iocb;
1476 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1477 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1478 spin_unlock_irq(phba->host->host_lock);
1479 (iocb->iocb_cmpl) (phba, iocb, iocb);
1480 spin_lock_irq(phba->host->host_lock);
James Bottomley604a3e32005-10-29 10:28:33 -05001481 } else
1482 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001483 }
1484 pring->txq_cnt = 0;
1485 INIT_LIST_HEAD(&(pring->txq));
1486
1487 /* Next issue ABTS for everything on the txcmplq */
1488 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1489 cmd = &iocb->iocb;
1490
1491 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001492 * Imediate abort of IOCB, deque and call compl
dea31012005-04-17 16:05:31 -05001493 */
dea31012005-04-17 16:05:31 -05001494
1495 list_del_init(&iocb->list);
1496 pring->txcmplq_cnt--;
1497
1498 if (iocb->iocb_cmpl) {
1499 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1500 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1501 spin_unlock_irq(phba->host->host_lock);
1502 (iocb->iocb_cmpl) (phba, iocb, iocb);
1503 spin_lock_irq(phba->host->host_lock);
James Bottomley604a3e32005-10-29 10:28:33 -05001504 } else
1505 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001506 }
1507
1508 INIT_LIST_HEAD(&pring->txcmplq);
1509 pring->txcmplq_cnt = 0;
1510 spin_unlock_irq(phba->host->host_lock);
1511
1512 return errcnt;
1513}
1514
1515/******************************************************************************
1516* lpfc_sli_send_reset
1517*
1518* Note: After returning from this function, the HBA cannot be accessed for
1519* 1 ms. Since we do not wish to delay in interrupt context, it is the
1520* responsibility of the caller to perform the mdelay(1) and flush via readl().
1521******************************************************************************/
1522static int
1523lpfc_sli_send_reset(struct lpfc_hba * phba, uint16_t skip_post)
1524{
1525 MAILBOX_t *swpmb;
1526 volatile uint32_t word0;
1527 void __iomem *to_slim;
1528 unsigned long flags = 0;
1529
1530 spin_lock_irqsave(phba->host->host_lock, flags);
1531
1532 /* A board reset must use REAL SLIM. */
1533 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
1534
1535 word0 = 0;
1536 swpmb = (MAILBOX_t *) & word0;
1537 swpmb->mbxCommand = MBX_RESTART;
1538 swpmb->mbxHc = 1;
1539
1540 to_slim = phba->MBslimaddr;
1541 writel(*(uint32_t *) swpmb, to_slim);
1542 readl(to_slim); /* flush */
1543
1544 /* Only skip post after fc_ffinit is completed */
1545 if (skip_post) {
1546 word0 = 1; /* This is really setting up word1 */
1547 } else {
1548 word0 = 0; /* This is really setting up word1 */
1549 }
1550 to_slim = phba->MBslimaddr + sizeof (uint32_t);
1551 writel(*(uint32_t *) swpmb, to_slim);
1552 readl(to_slim); /* flush */
1553
1554 /* Turn off parity checking and serr during the physical reset */
1555 pci_read_config_word(phba->pcidev, PCI_COMMAND, &phba->pci_cfg_value);
1556 pci_write_config_word(phba->pcidev, PCI_COMMAND,
1557 (phba->pci_cfg_value &
1558 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
1559
1560 writel(HC_INITFF, phba->HCregaddr);
1561
1562 phba->hba_state = LPFC_INIT_START;
1563 spin_unlock_irqrestore(phba->host->host_lock, flags);
1564
1565 return 0;
1566}
1567
1568static int
1569lpfc_sli_brdreset(struct lpfc_hba * phba, uint16_t skip_post)
1570{
1571 struct lpfc_sli_ring *pring;
1572 int i;
1573 struct lpfc_dmabuf *mp, *next_mp;
1574 unsigned long flags = 0;
1575
1576 lpfc_sli_send_reset(phba, skip_post);
1577 mdelay(1);
1578
1579 spin_lock_irqsave(phba->host->host_lock, flags);
1580 /* Risk the write on flush case ie no delay after the readl */
1581 readl(phba->HCregaddr); /* flush */
1582 /* Now toggle INITFF bit set by lpfc_sli_send_reset */
1583 writel(0, phba->HCregaddr);
1584 readl(phba->HCregaddr); /* flush */
1585
1586 /* Restore PCI cmd register */
1587 pci_write_config_word(phba->pcidev, PCI_COMMAND, phba->pci_cfg_value);
1588
1589 /* perform board reset */
1590 phba->fc_eventTag = 0;
1591 phba->fc_myDID = 0;
1592 phba->fc_prevDID = Mask_DID;
1593
1594 /* Reset HBA */
1595 lpfc_printf_log(phba,
1596 KERN_INFO,
1597 LOG_SLI,
1598 "%d:0325 Reset HBA Data: x%x x%x x%x\n",
1599 phba->brd_no,
1600 phba->hba_state,
1601 phba->sli.sli_flag,
1602 skip_post);
1603
1604 /* Initialize relevant SLI info */
1605 for (i = 0; i < phba->sli.num_rings; i++) {
1606 pring = &phba->sli.ring[i];
1607 pring->flag = 0;
1608 pring->rspidx = 0;
1609 pring->next_cmdidx = 0;
1610 pring->local_getidx = 0;
1611 pring->cmdidx = 0;
1612 pring->missbufcnt = 0;
1613 }
1614 spin_unlock_irqrestore(phba->host->host_lock, flags);
1615
1616 if (skip_post) {
1617 mdelay(100);
1618 } else {
1619 mdelay(2000);
1620 }
1621
1622 spin_lock_irqsave(phba->host->host_lock, flags);
1623 /* Cleanup preposted buffers on the ELS ring */
1624 pring = &phba->sli.ring[LPFC_ELS_RING];
1625 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
1626 list_del(&mp->list);
1627 pring->postbufq_cnt--;
1628 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1629 kfree(mp);
1630 }
1631 spin_unlock_irqrestore(phba->host->host_lock, flags);
1632
1633 for (i = 0; i < phba->sli.num_rings; i++)
1634 lpfc_sli_abort_iocb_ring(phba, &phba->sli.ring[i]);
1635
1636 return 0;
1637}
1638
1639static int
1640lpfc_sli_chipset_init(struct lpfc_hba *phba)
1641{
1642 uint32_t status, i = 0;
1643
1644 /* Read the HBA Host Status Register */
1645 status = readl(phba->HSregaddr);
1646
1647 /* Check status register to see what current state is */
1648 i = 0;
1649 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
1650
1651 /* Check every 100ms for 5 retries, then every 500ms for 5, then
1652 * every 2.5 sec for 5, then reset board and every 2.5 sec for
1653 * 4.
1654 */
1655 if (i++ >= 20) {
1656 /* Adapter failed to init, timeout, status reg
1657 <status> */
1658 lpfc_printf_log(phba,
1659 KERN_ERR,
1660 LOG_INIT,
1661 "%d:0436 Adapter failed to init, "
1662 "timeout, status reg x%x\n",
1663 phba->brd_no,
1664 status);
1665 phba->hba_state = LPFC_HBA_ERROR;
1666 return -ETIMEDOUT;
1667 }
1668
1669 /* Check to see if any errors occurred during init */
1670 if (status & HS_FFERM) {
1671 /* ERROR: During chipset initialization */
1672 /* Adapter failed to init, chipset, status reg
1673 <status> */
1674 lpfc_printf_log(phba,
1675 KERN_ERR,
1676 LOG_INIT,
1677 "%d:0437 Adapter failed to init, "
1678 "chipset, status reg x%x\n",
1679 phba->brd_no,
1680 status);
1681 phba->hba_state = LPFC_HBA_ERROR;
1682 return -EIO;
1683 }
1684
1685 if (i <= 5) {
1686 msleep(10);
1687 } else if (i <= 10) {
1688 msleep(500);
1689 } else {
1690 msleep(2500);
1691 }
1692
1693 if (i == 15) {
1694 lpfc_sli_brdreset(phba, 0);
1695 }
1696 /* Read the HBA Host Status Register */
1697 status = readl(phba->HSregaddr);
1698 }
1699
1700 /* Check to see if any errors occurred during init */
1701 if (status & HS_FFERM) {
1702 /* ERROR: During chipset initialization */
1703 /* Adapter failed to init, chipset, status reg <status> */
1704 lpfc_printf_log(phba,
1705 KERN_ERR,
1706 LOG_INIT,
1707 "%d:0438 Adapter failed to init, chipset, "
1708 "status reg x%x\n",
1709 phba->brd_no,
1710 status);
1711 phba->hba_state = LPFC_HBA_ERROR;
1712 return -EIO;
1713 }
1714
1715 /* Clear all interrupt enable conditions */
1716 writel(0, phba->HCregaddr);
1717 readl(phba->HCregaddr); /* flush */
1718
1719 /* setup host attn register */
1720 writel(0xffffffff, phba->HAregaddr);
1721 readl(phba->HAregaddr); /* flush */
1722 return 0;
1723}
1724
1725int
1726lpfc_sli_hba_setup(struct lpfc_hba * phba)
1727{
1728 LPFC_MBOXQ_t *pmb;
1729 uint32_t resetcount = 0, rc = 0, done = 0;
1730
1731 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1732 if (!pmb) {
1733 phba->hba_state = LPFC_HBA_ERROR;
1734 return -ENOMEM;
1735 }
1736
1737 while (resetcount < 2 && !done) {
1738 phba->hba_state = 0;
1739 lpfc_sli_brdreset(phba, 0);
1740 msleep(2500);
1741 rc = lpfc_sli_chipset_init(phba);
1742 if (rc)
1743 break;
1744
1745 resetcount++;
1746
1747 /* Call pre CONFIG_PORT mailbox command initialization. A value of 0
1748 * means the call was successful. Any other nonzero value is a failure,
1749 * but if ERESTART is returned, the driver may reset the HBA and try
1750 * again.
1751 */
1752 rc = lpfc_config_port_prep(phba);
1753 if (rc == -ERESTART) {
1754 phba->hba_state = 0;
1755 continue;
1756 } else if (rc) {
1757 break;
1758 }
1759
1760 phba->hba_state = LPFC_INIT_MBX_CMDS;
1761 lpfc_config_port(phba, pmb);
1762 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1763 if (rc == MBX_SUCCESS)
1764 done = 1;
1765 else {
1766 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1767 "%d:0442 Adapter failed to init, mbxCmd x%x "
1768 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
1769 phba->brd_no, pmb->mb.mbxCommand,
1770 pmb->mb.mbxStatus, 0);
1771 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
1772 }
1773 }
1774 if (!done)
1775 goto lpfc_sli_hba_setup_error;
1776
1777 rc = lpfc_sli_ring_map(phba, pmb);
1778
1779 if (rc)
1780 goto lpfc_sli_hba_setup_error;
1781
1782 phba->sli.sli_flag |= LPFC_PROCESS_LA;
1783
1784 rc = lpfc_config_port_post(phba);
1785 if (rc)
1786 goto lpfc_sli_hba_setup_error;
1787
1788 goto lpfc_sli_hba_setup_exit;
1789lpfc_sli_hba_setup_error:
1790 phba->hba_state = LPFC_HBA_ERROR;
1791lpfc_sli_hba_setup_exit:
1792 mempool_free(pmb, phba->mbox_mem_pool);
1793 return rc;
1794}
1795
1796static void
1797lpfc_mbox_abort(struct lpfc_hba * phba)
1798{
1799 LPFC_MBOXQ_t *pmbox;
1800 MAILBOX_t *mb;
1801
1802 if (phba->sli.mbox_active) {
1803 del_timer_sync(&phba->sli.mbox_tmo);
1804 phba->work_hba_events &= ~WORKER_MBOX_TMO;
1805 pmbox = phba->sli.mbox_active;
1806 mb = &pmbox->mb;
1807 phba->sli.mbox_active = NULL;
1808 if (pmbox->mbox_cmpl) {
1809 mb->mbxStatus = MBX_NOT_FINISHED;
1810 (pmbox->mbox_cmpl) (phba, pmbox);
1811 }
1812 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1813 }
1814
1815 /* Abort all the non active mailbox commands. */
1816 spin_lock_irq(phba->host->host_lock);
1817 pmbox = lpfc_mbox_get(phba);
1818 while (pmbox) {
1819 mb = &pmbox->mb;
1820 if (pmbox->mbox_cmpl) {
1821 mb->mbxStatus = MBX_NOT_FINISHED;
1822 spin_unlock_irq(phba->host->host_lock);
1823 (pmbox->mbox_cmpl) (phba, pmbox);
1824 spin_lock_irq(phba->host->host_lock);
1825 }
1826 pmbox = lpfc_mbox_get(phba);
1827 }
1828 spin_unlock_irq(phba->host->host_lock);
1829 return;
1830}
1831
1832/*! lpfc_mbox_timeout
1833 *
1834 * \pre
1835 * \post
1836 * \param hba Pointer to per struct lpfc_hba structure
1837 * \param l1 Pointer to the driver's mailbox queue.
1838 * \return
1839 * void
1840 *
1841 * \b Description:
1842 *
1843 * This routine handles mailbox timeout events at timer interrupt context.
1844 */
1845void
1846lpfc_mbox_timeout(unsigned long ptr)
1847{
1848 struct lpfc_hba *phba;
1849 unsigned long iflag;
1850
1851 phba = (struct lpfc_hba *)ptr;
1852 spin_lock_irqsave(phba->host->host_lock, iflag);
1853 if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
1854 phba->work_hba_events |= WORKER_MBOX_TMO;
1855 if (phba->work_wait)
1856 wake_up(phba->work_wait);
1857 }
1858 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1859}
1860
1861void
1862lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
1863{
1864 LPFC_MBOXQ_t *pmbox;
1865 MAILBOX_t *mb;
1866
1867 spin_lock_irq(phba->host->host_lock);
1868 if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
1869 spin_unlock_irq(phba->host->host_lock);
1870 return;
1871 }
1872
James.Smart@Emulex.Comdb468d12005-06-25 10:34:08 -04001873 phba->work_hba_events &= ~WORKER_MBOX_TMO;
1874
dea31012005-04-17 16:05:31 -05001875 pmbox = phba->sli.mbox_active;
1876 mb = &pmbox->mb;
1877
1878 /* Mbox cmd <mbxCommand> timeout */
1879 lpfc_printf_log(phba,
1880 KERN_ERR,
1881 LOG_MBOX | LOG_SLI,
1882 "%d:0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
1883 phba->brd_no,
1884 mb->mbxCommand,
1885 phba->hba_state,
1886 phba->sli.sli_flag,
1887 phba->sli.mbox_active);
1888
James.Smart@Emulex.Comdb468d12005-06-25 10:34:08 -04001889 phba->sli.mbox_active = NULL;
1890 if (pmbox->mbox_cmpl) {
1891 mb->mbxStatus = MBX_NOT_FINISHED;
1892 spin_unlock_irq(phba->host->host_lock);
1893 (pmbox->mbox_cmpl) (phba, pmbox);
1894 spin_lock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -05001895 }
James.Smart@Emulex.Comdb468d12005-06-25 10:34:08 -04001896 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
dea31012005-04-17 16:05:31 -05001897
1898 spin_unlock_irq(phba->host->host_lock);
1899 lpfc_mbox_abort(phba);
1900 return;
1901}
1902
1903int
1904lpfc_sli_issue_mbox(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmbox, uint32_t flag)
1905{
dea31012005-04-17 16:05:31 -05001906 MAILBOX_t *mb;
1907 struct lpfc_sli *psli;
1908 uint32_t status, evtctr;
1909 uint32_t ha_copy;
1910 int i;
1911 unsigned long drvr_flag = 0;
1912 volatile uint32_t word0, ldata;
1913 void __iomem *to_slim;
1914
1915 psli = &phba->sli;
1916
1917 spin_lock_irqsave(phba->host->host_lock, drvr_flag);
1918
1919
1920 mb = &pmbox->mb;
1921 status = MBX_SUCCESS;
1922
1923 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
1924 /* Polling for a mbox command when another one is already active
1925 * is not allowed in SLI. Also, the driver must have established
1926 * SLI2 mode to queue and process multiple mbox commands.
1927 */
1928
1929 if (flag & MBX_POLL) {
1930 spin_unlock_irqrestore(phba->host->host_lock,
1931 drvr_flag);
1932
1933 /* Mbox command <mbxCommand> cannot issue */
1934 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
1935 return (MBX_NOT_FINISHED);
1936 }
1937
1938 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
1939 spin_unlock_irqrestore(phba->host->host_lock,
1940 drvr_flag);
1941 /* Mbox command <mbxCommand> cannot issue */
1942 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
1943 return (MBX_NOT_FINISHED);
1944 }
1945
1946 /* Handle STOP IOCB processing flag. This is only meaningful
1947 * if we are not polling for mbox completion.
1948 */
1949 if (flag & MBX_STOP_IOCB) {
1950 flag &= ~MBX_STOP_IOCB;
1951 /* Now flag each ring */
1952 for (i = 0; i < psli->num_rings; i++) {
1953 /* If the ring is active, flag it */
1954 if (psli->ring[i].cmdringaddr) {
1955 psli->ring[i].flag |=
1956 LPFC_STOP_IOCB_MBX;
1957 }
1958 }
1959 }
1960
1961 /* Another mailbox command is still being processed, queue this
1962 * command to be processed later.
1963 */
1964 lpfc_mbox_put(phba, pmbox);
1965
1966 /* Mbox cmd issue - BUSY */
1967 lpfc_printf_log(phba,
1968 KERN_INFO,
1969 LOG_MBOX | LOG_SLI,
1970 "%d:0308 Mbox cmd issue - BUSY Data: x%x x%x x%x x%x\n",
1971 phba->brd_no,
1972 mb->mbxCommand,
1973 phba->hba_state,
1974 psli->sli_flag,
1975 flag);
1976
1977 psli->slistat.mbox_busy++;
1978 spin_unlock_irqrestore(phba->host->host_lock,
1979 drvr_flag);
1980
1981 return (MBX_BUSY);
1982 }
1983
1984 /* Handle STOP IOCB processing flag. This is only meaningful
1985 * if we are not polling for mbox completion.
1986 */
1987 if (flag & MBX_STOP_IOCB) {
1988 flag &= ~MBX_STOP_IOCB;
1989 if (flag == MBX_NOWAIT) {
1990 /* Now flag each ring */
1991 for (i = 0; i < psli->num_rings; i++) {
1992 /* If the ring is active, flag it */
1993 if (psli->ring[i].cmdringaddr) {
1994 psli->ring[i].flag |=
1995 LPFC_STOP_IOCB_MBX;
1996 }
1997 }
1998 }
1999 }
2000
2001 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2002
2003 /* If we are not polling, we MUST be in SLI2 mode */
2004 if (flag != MBX_POLL) {
2005 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
2006 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2007 spin_unlock_irqrestore(phba->host->host_lock,
2008 drvr_flag);
2009 /* Mbox command <mbxCommand> cannot issue */
2010 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag);
2011 return (MBX_NOT_FINISHED);
2012 }
2013 /* timeout active mbox command */
2014 mod_timer(&psli->mbox_tmo, jiffies + HZ * LPFC_MBOX_TMO);
2015 }
2016
2017 /* Mailbox cmd <cmd> issue */
2018 lpfc_printf_log(phba,
2019 KERN_INFO,
2020 LOG_MBOX | LOG_SLI,
2021 "%d:0309 Mailbox cmd x%x issue Data: x%x x%x x%x\n",
2022 phba->brd_no,
2023 mb->mbxCommand,
2024 phba->hba_state,
2025 psli->sli_flag,
2026 flag);
2027
2028 psli->slistat.mbox_cmd++;
2029 evtctr = psli->slistat.mbox_event;
2030
2031 /* next set own bit for the adapter and copy over command word */
2032 mb->mbxOwner = OWN_CHIP;
2033
2034 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002035 /* First copy command data to host SLIM area */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002036 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002037 } else {
2038 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2039 /* copy command data into host mbox for cmpl */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002040 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
2041 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002042 }
2043
2044 /* First copy mbox command data to HBA SLIM, skip past first
2045 word */
2046 to_slim = phba->MBslimaddr + sizeof (uint32_t);
2047 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
2048 MAILBOX_CMD_SIZE - sizeof (uint32_t));
2049
2050 /* Next copy over first word, with mbxOwner set */
2051 ldata = *((volatile uint32_t *)mb);
2052 to_slim = phba->MBslimaddr;
2053 writel(ldata, to_slim);
2054 readl(to_slim); /* flush */
2055
2056 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2057 /* switch over to host mailbox */
2058 psli->sli_flag |= LPFC_SLI2_ACTIVE;
2059 }
2060 }
2061
2062 wmb();
2063 /* interrupt board to doit right away */
2064 writel(CA_MBATT, phba->CAregaddr);
2065 readl(phba->CAregaddr); /* flush */
2066
2067 switch (flag) {
2068 case MBX_NOWAIT:
2069 /* Don't wait for it to finish, just return */
2070 psli->mbox_active = pmbox;
2071 break;
2072
2073 case MBX_POLL:
2074 i = 0;
2075 psli->mbox_active = NULL;
2076 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2077 /* First read mbox status word */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002078 word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002079 word0 = le32_to_cpu(word0);
2080 } else {
2081 /* First read mbox status word */
2082 word0 = readl(phba->MBslimaddr);
2083 }
2084
2085 /* Read the HBA Host Attention Register */
2086 ha_copy = readl(phba->HAregaddr);
2087
2088 /* Wait for command to complete */
2089 while (((word0 & OWN_CHIP) == OWN_CHIP)
2090 || !(ha_copy & HA_MBATT)) {
2091 if (i++ >= 100) {
2092 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2093 spin_unlock_irqrestore(phba->host->host_lock,
2094 drvr_flag);
2095 return (MBX_NOT_FINISHED);
2096 }
2097
2098 /* Check if we took a mbox interrupt while we were
2099 polling */
2100 if (((word0 & OWN_CHIP) != OWN_CHIP)
2101 && (evtctr != psli->slistat.mbox_event))
2102 break;
2103
2104 spin_unlock_irqrestore(phba->host->host_lock,
2105 drvr_flag);
2106
2107 /* Can be in interrupt context, do not sleep */
2108 /* (or might be called with interrupts disabled) */
2109 mdelay(i);
2110
2111 spin_lock_irqsave(phba->host->host_lock, drvr_flag);
2112
2113 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2114 /* First copy command data */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002115 word0 = *((volatile uint32_t *)
2116 &phba->slim2p->mbx);
dea31012005-04-17 16:05:31 -05002117 word0 = le32_to_cpu(word0);
2118 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2119 MAILBOX_t *slimmb;
2120 volatile uint32_t slimword0;
2121 /* Check real SLIM for any errors */
2122 slimword0 = readl(phba->MBslimaddr);
2123 slimmb = (MAILBOX_t *) & slimword0;
2124 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
2125 && slimmb->mbxStatus) {
2126 psli->sli_flag &=
2127 ~LPFC_SLI2_ACTIVE;
2128 word0 = slimword0;
2129 }
2130 }
2131 } else {
2132 /* First copy command data */
2133 word0 = readl(phba->MBslimaddr);
2134 }
2135 /* Read the HBA Host Attention Register */
2136 ha_copy = readl(phba->HAregaddr);
2137 }
2138
2139 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
dea31012005-04-17 16:05:31 -05002140 /* copy results back to user */
James.Smart@Emulex.Com4cc2da12005-06-25 10:34:00 -04002141 lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
2142 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05002143 } else {
2144 /* First copy command data */
2145 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2146 MAILBOX_CMD_SIZE);
2147 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2148 pmbox->context2) {
2149 lpfc_memcpy_from_slim((void *)pmbox->context2,
2150 phba->MBslimaddr + DMP_RSP_OFFSET,
2151 mb->un.varDmp.word_cnt);
2152 }
2153 }
2154
2155 writel(HA_MBATT, phba->HAregaddr);
2156 readl(phba->HAregaddr); /* flush */
2157
2158 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2159 status = mb->mbxStatus;
2160 }
2161
2162 spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2163 return (status);
2164}
2165
2166static int
2167lpfc_sli_ringtx_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2168 struct lpfc_iocbq * piocb)
2169{
2170 /* Insert the caller's iocb in the txq tail for later processing. */
2171 list_add_tail(&piocb->list, &pring->txq);
2172 pring->txq_cnt++;
2173 return (0);
2174}
2175
2176static struct lpfc_iocbq *
2177lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2178 struct lpfc_iocbq ** piocb)
2179{
2180 struct lpfc_iocbq * nextiocb;
2181
2182 nextiocb = lpfc_sli_ringtx_get(phba, pring);
2183 if (!nextiocb) {
2184 nextiocb = *piocb;
2185 *piocb = NULL;
2186 }
2187
2188 return nextiocb;
2189}
2190
2191int
2192lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2193 struct lpfc_iocbq *piocb, uint32_t flag)
2194{
2195 struct lpfc_iocbq *nextiocb;
2196 IOCB_t *iocb;
2197
2198 /*
2199 * We should never get an IOCB if we are in a < LINK_DOWN state
2200 */
2201 if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
2202 return IOCB_ERROR;
2203
2204 /*
2205 * Check to see if we are blocking IOCB processing because of a
2206 * outstanding mbox command.
2207 */
2208 if (unlikely(pring->flag & LPFC_STOP_IOCB_MBX))
2209 goto iocb_busy;
2210
2211 if (unlikely(phba->hba_state == LPFC_LINK_DOWN)) {
2212 /*
2213 * Only CREATE_XRI, CLOSE_XRI, ABORT_XRI, and QUE_RING_BUF
2214 * can be issued if the link is not up.
2215 */
2216 switch (piocb->iocb.ulpCommand) {
2217 case CMD_QUE_RING_BUF_CN:
2218 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05002219 /*
2220 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
2221 * completion, iocb_cmpl MUST be 0.
2222 */
2223 if (piocb->iocb_cmpl)
2224 piocb->iocb_cmpl = NULL;
2225 /*FALLTHROUGH*/
2226 case CMD_CREATE_XRI_CR:
2227 break;
2228 default:
2229 goto iocb_busy;
2230 }
2231
2232 /*
2233 * For FCP commands, we must be in a state where we can process link
2234 * attention events.
2235 */
2236 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
2237 !(phba->sli.sli_flag & LPFC_PROCESS_LA)))
2238 goto iocb_busy;
2239
dea31012005-04-17 16:05:31 -05002240 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2241 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
2242 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2243
2244 if (iocb)
2245 lpfc_sli_update_ring(phba, pring);
2246 else
2247 lpfc_sli_update_full_ring(phba, pring);
2248
2249 if (!piocb)
2250 return IOCB_SUCCESS;
2251
2252 goto out_busy;
2253
2254 iocb_busy:
2255 pring->stats.iocb_cmd_delay++;
2256
2257 out_busy:
2258
2259 if (!(flag & SLI_IOCB_RET_IOCB)) {
2260 lpfc_sli_ringtx_put(phba, pring, piocb);
2261 return IOCB_SUCCESS;
2262 }
2263
2264 return IOCB_BUSY;
2265}
2266
2267int
2268lpfc_sli_setup(struct lpfc_hba *phba)
2269{
2270 int i, totiocb = 0;
2271 struct lpfc_sli *psli = &phba->sli;
2272 struct lpfc_sli_ring *pring;
2273
2274 psli->num_rings = MAX_CONFIGURED_RINGS;
2275 psli->sli_flag = 0;
2276 psli->fcp_ring = LPFC_FCP_RING;
2277 psli->next_ring = LPFC_FCP_NEXT_RING;
2278 psli->ip_ring = LPFC_IP_RING;
2279
James Bottomley604a3e32005-10-29 10:28:33 -05002280 psli->iocbq_lookup = NULL;
2281 psli->iocbq_lookup_len = 0;
2282 psli->last_iotag = 0;
2283
dea31012005-04-17 16:05:31 -05002284 for (i = 0; i < psli->num_rings; i++) {
2285 pring = &psli->ring[i];
2286 switch (i) {
2287 case LPFC_FCP_RING: /* ring 0 - FCP */
2288 /* numCiocb and numRiocb are used in config_port */
2289 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
2290 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
2291 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2292 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2293 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2294 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2295 pring->iotag_ctr = 0;
2296 pring->iotag_max =
2297 (phba->cfg_hba_queue_depth * 2);
2298 pring->fast_iotag = pring->iotag_max;
2299 pring->num_mask = 0;
2300 break;
2301 case LPFC_IP_RING: /* ring 1 - IP */
2302 /* numCiocb and numRiocb are used in config_port */
2303 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
2304 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
2305 pring->num_mask = 0;
2306 break;
2307 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
2308 /* numCiocb and numRiocb are used in config_port */
2309 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
2310 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
2311 pring->fast_iotag = 0;
2312 pring->iotag_ctr = 0;
2313 pring->iotag_max = 4096;
2314 pring->num_mask = 4;
2315 pring->prt[0].profile = 0; /* Mask 0 */
2316 pring->prt[0].rctl = FC_ELS_REQ;
2317 pring->prt[0].type = FC_ELS_DATA;
2318 pring->prt[0].lpfc_sli_rcv_unsol_event =
2319 lpfc_els_unsol_event;
2320 pring->prt[1].profile = 0; /* Mask 1 */
2321 pring->prt[1].rctl = FC_ELS_RSP;
2322 pring->prt[1].type = FC_ELS_DATA;
2323 pring->prt[1].lpfc_sli_rcv_unsol_event =
2324 lpfc_els_unsol_event;
2325 pring->prt[2].profile = 0; /* Mask 2 */
2326 /* NameServer Inquiry */
2327 pring->prt[2].rctl = FC_UNSOL_CTL;
2328 /* NameServer */
2329 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
2330 pring->prt[2].lpfc_sli_rcv_unsol_event =
2331 lpfc_ct_unsol_event;
2332 pring->prt[3].profile = 0; /* Mask 3 */
2333 /* NameServer response */
2334 pring->prt[3].rctl = FC_SOL_CTL;
2335 /* NameServer */
2336 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
2337 pring->prt[3].lpfc_sli_rcv_unsol_event =
2338 lpfc_ct_unsol_event;
2339 break;
2340 }
2341 totiocb += (pring->numCiocb + pring->numRiocb);
2342 }
2343 if (totiocb > MAX_SLI2_IOCB) {
2344 /* Too many cmd / rsp ring entries in SLI2 SLIM */
2345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2346 "%d:0462 Too many cmd / rsp ring entries in "
2347 "SLI2 SLIM Data: x%x x%x\n",
2348 phba->brd_no, totiocb, MAX_SLI2_IOCB);
2349 }
2350
2351 return 0;
2352}
2353
2354int
2355lpfc_sli_queue_setup(struct lpfc_hba * phba)
2356{
2357 struct lpfc_sli *psli;
2358 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05002359 int i;
dea31012005-04-17 16:05:31 -05002360
2361 psli = &phba->sli;
2362 spin_lock_irq(phba->host->host_lock);
2363 INIT_LIST_HEAD(&psli->mboxq);
2364 /* Initialize list headers for txq and txcmplq as double linked lists */
2365 for (i = 0; i < psli->num_rings; i++) {
2366 pring = &psli->ring[i];
2367 pring->ringno = i;
2368 pring->next_cmdidx = 0;
2369 pring->local_getidx = 0;
2370 pring->cmdidx = 0;
2371 INIT_LIST_HEAD(&pring->txq);
2372 INIT_LIST_HEAD(&pring->txcmplq);
2373 INIT_LIST_HEAD(&pring->iocb_continueq);
2374 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05002375 }
2376 spin_unlock_irq(phba->host->host_lock);
2377 return (1);
2378}
2379
2380int
2381lpfc_sli_hba_down(struct lpfc_hba * phba)
2382{
2383 struct lpfc_sli *psli;
2384 struct lpfc_sli_ring *pring;
2385 LPFC_MBOXQ_t *pmb;
2386 struct lpfc_iocbq *iocb, *next_iocb;
2387 IOCB_t *icmd = NULL;
2388 int i;
2389 unsigned long flags = 0;
2390
2391 psli = &phba->sli;
2392 lpfc_hba_down_prep(phba);
2393
2394 spin_lock_irqsave(phba->host->host_lock, flags);
2395
2396 for (i = 0; i < psli->num_rings; i++) {
2397 pring = &psli->ring[i];
2398 pring->flag |= LPFC_DEFERRED_RING_EVENT;
2399
2400 /*
2401 * Error everything on the txq since these iocbs have not been
2402 * given to the FW yet.
2403 */
2404 pring->txq_cnt = 0;
2405
2406 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2407 list_del_init(&iocb->list);
2408 if (iocb->iocb_cmpl) {
2409 icmd = &iocb->iocb;
2410 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2411 icmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2412 spin_unlock_irqrestore(phba->host->host_lock,
2413 flags);
2414 (iocb->iocb_cmpl) (phba, iocb, iocb);
2415 spin_lock_irqsave(phba->host->host_lock, flags);
James Bottomley604a3e32005-10-29 10:28:33 -05002416 } else
2417 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002418 }
2419
2420 INIT_LIST_HEAD(&(pring->txq));
2421
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002422 kfree(pring->fast_lookup);
2423 pring->fast_lookup = NULL;
dea31012005-04-17 16:05:31 -05002424 }
2425
2426 spin_unlock_irqrestore(phba->host->host_lock, flags);
2427
2428 /* Return any active mbox cmds */
2429 del_timer_sync(&psli->mbox_tmo);
2430 spin_lock_irqsave(phba->host->host_lock, flags);
2431 phba->work_hba_events &= ~WORKER_MBOX_TMO;
2432 if (psli->mbox_active) {
2433 pmb = psli->mbox_active;
2434 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2435 if (pmb->mbox_cmpl) {
2436 spin_unlock_irqrestore(phba->host->host_lock, flags);
2437 pmb->mbox_cmpl(phba,pmb);
2438 spin_lock_irqsave(phba->host->host_lock, flags);
2439 }
2440 }
2441 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2442 psli->mbox_active = NULL;
2443
2444 /* Return any pending mbox cmds */
2445 while ((pmb = lpfc_mbox_get(phba)) != NULL) {
2446 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2447 if (pmb->mbox_cmpl) {
2448 spin_unlock_irqrestore(phba->host->host_lock, flags);
2449 pmb->mbox_cmpl(phba,pmb);
2450 spin_lock_irqsave(phba->host->host_lock, flags);
2451 }
2452 }
2453
2454 INIT_LIST_HEAD(&psli->mboxq);
2455
2456 spin_unlock_irqrestore(phba->host->host_lock, flags);
2457
2458 /*
2459 * Provided the hba is not in an error state, reset it. It is not
2460 * capable of IO anymore.
2461 */
2462 if (phba->hba_state != LPFC_HBA_ERROR) {
2463 phba->hba_state = LPFC_INIT_START;
2464 lpfc_sli_brdreset(phba, 1);
2465 }
2466
2467 return 1;
2468}
2469
2470void
2471lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
2472{
2473 uint32_t *src = srcp;
2474 uint32_t *dest = destp;
2475 uint32_t ldata;
2476 int i;
2477
2478 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
2479 ldata = *src;
2480 ldata = le32_to_cpu(ldata);
2481 *dest = ldata;
2482 src++;
2483 dest++;
2484 }
2485}
2486
2487int
2488lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2489 struct lpfc_dmabuf * mp)
2490{
2491 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
2492 later */
2493 list_add_tail(&mp->list, &pring->postbufq);
2494
2495 pring->postbufq_cnt++;
2496 return 0;
2497}
2498
2499
2500struct lpfc_dmabuf *
2501lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2502 dma_addr_t phys)
2503{
2504 struct lpfc_dmabuf *mp, *next_mp;
2505 struct list_head *slp = &pring->postbufq;
2506
2507 /* Search postbufq, from the begining, looking for a match on phys */
2508 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
2509 if (mp->phys == phys) {
2510 list_del_init(&mp->list);
2511 pring->postbufq_cnt--;
2512 return mp;
2513 }
2514 }
2515
2516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2517 "%d:0410 Cannot find virtual addr for mapped buf on "
2518 "ring %d Data x%llx x%p x%p x%x\n",
2519 phba->brd_no, pring->ringno, (unsigned long long)phys,
2520 slp->next, slp->prev, pring->postbufq_cnt);
2521 return NULL;
2522}
2523
2524static void
2525lpfc_sli_abort_elsreq_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2526 struct lpfc_iocbq * rspiocb)
2527{
2528 struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2529 /* Free the resources associated with the ELS_REQUEST64 IOCB the driver
2530 * just aborted.
2531 * In this case, context2 = cmd, context2->next = rsp, context3 = bpl
2532 */
2533 if (cmdiocb->context2) {
2534 buf_ptr1 = (struct lpfc_dmabuf *) cmdiocb->context2;
2535
2536 /* Free the response IOCB before completing the abort
2537 command. */
2538 buf_ptr = NULL;
2539 list_remove_head((&buf_ptr1->list), buf_ptr,
2540 struct lpfc_dmabuf, list);
2541 if (buf_ptr) {
2542 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2543 kfree(buf_ptr);
2544 }
2545 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2546 kfree(buf_ptr1);
2547 }
2548
2549 if (cmdiocb->context3) {
2550 buf_ptr = (struct lpfc_dmabuf *) cmdiocb->context3;
2551 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2552 kfree(buf_ptr);
2553 }
2554
James Bottomley604a3e32005-10-29 10:28:33 -05002555 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05002556 return;
2557}
2558
2559int
2560lpfc_sli_issue_abort_iotag32(struct lpfc_hba * phba,
2561 struct lpfc_sli_ring * pring,
2562 struct lpfc_iocbq * cmdiocb)
2563{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002564 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05002565 IOCB_t *icmd = NULL;
2566 IOCB_t *iabt = NULL;
2567
2568 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002569 abtsiocbp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002570 if (abtsiocbp == NULL)
2571 return 0;
dea31012005-04-17 16:05:31 -05002572
2573 iabt = &abtsiocbp->iocb;
2574 icmd = &cmdiocb->iocb;
2575 switch (icmd->ulpCommand) {
2576 case CMD_ELS_REQUEST64_CR:
2577 /* Even though we abort the ELS command, the firmware may access
2578 * the BPL or other resources before it processes our
2579 * ABORT_MXRI64. Thus we must delay reusing the cmdiocb
2580 * resources till the actual abort request completes.
2581 */
2582 abtsiocbp->context1 = (void *)((unsigned long)icmd->ulpCommand);
2583 abtsiocbp->context2 = cmdiocb->context2;
2584 abtsiocbp->context3 = cmdiocb->context3;
2585 cmdiocb->context2 = NULL;
2586 cmdiocb->context3 = NULL;
2587 abtsiocbp->iocb_cmpl = lpfc_sli_abort_elsreq_cmpl;
2588 break;
2589 default:
James Bottomley604a3e32005-10-29 10:28:33 -05002590 lpfc_sli_release_iocbq(phba, abtsiocbp);
dea31012005-04-17 16:05:31 -05002591 return 0;
2592 }
2593
2594 iabt->un.amxri.abortType = ABORT_TYPE_ABTS;
2595 iabt->un.amxri.iotag32 = icmd->un.elsreq64.bdl.ulpIoTag32;
2596
2597 iabt->ulpLe = 1;
2598 iabt->ulpClass = CLASS3;
2599 iabt->ulpCommand = CMD_ABORT_MXRI64_CN;
2600
2601 if (lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0) == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05002602 lpfc_sli_release_iocbq(phba, abtsiocbp);
dea31012005-04-17 16:05:31 -05002603 return 0;
2604 }
2605
2606 return 1;
2607}
2608
2609static int
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002610lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, uint16_t tgt_id,
2611 uint64_t lun_id, uint32_t ctx,
2612 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05002613{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002614 struct lpfc_scsi_buf *lpfc_cmd;
2615 struct scsi_cmnd *cmnd;
dea31012005-04-17 16:05:31 -05002616 int rc = 1;
2617
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002618 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
2619 return rc;
2620
2621 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
2622 cmnd = lpfc_cmd->pCmd;
2623
2624 if (cmnd == NULL)
dea31012005-04-17 16:05:31 -05002625 return rc;
2626
2627 switch (ctx_cmd) {
2628 case LPFC_CTX_LUN:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002629 if ((cmnd->device->id == tgt_id) &&
2630 (cmnd->device->lun == lun_id))
dea31012005-04-17 16:05:31 -05002631 rc = 0;
2632 break;
2633 case LPFC_CTX_TGT:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002634 if (cmnd->device->id == tgt_id)
dea31012005-04-17 16:05:31 -05002635 rc = 0;
2636 break;
2637 case LPFC_CTX_CTX:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002638 if (iocbq->iocb.ulpContext == ctx)
dea31012005-04-17 16:05:31 -05002639 rc = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002640 break;
dea31012005-04-17 16:05:31 -05002641 case LPFC_CTX_HOST:
2642 rc = 0;
2643 break;
2644 default:
2645 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
2646 __FUNCTION__, ctx_cmd);
2647 break;
2648 }
2649
2650 return rc;
2651}
2652
2653int
2654lpfc_sli_sum_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2655 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd ctx_cmd)
2656{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002657 struct lpfc_iocbq *iocbq;
2658 int sum, i;
dea31012005-04-17 16:05:31 -05002659
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002660 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
2661 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05002662
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002663 if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
2664 0, ctx_cmd) == 0)
2665 sum++;
dea31012005-04-17 16:05:31 -05002666 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002667
dea31012005-04-17 16:05:31 -05002668 return sum;
2669}
2670
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04002671void
2672lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2673 struct lpfc_iocbq * rspiocb)
2674{
2675 spin_lock_irq(phba->host->host_lock);
James Bottomley604a3e32005-10-29 10:28:33 -05002676 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04002677 spin_unlock_irq(phba->host->host_lock);
2678 return;
2679}
2680
dea31012005-04-17 16:05:31 -05002681int
2682lpfc_sli_abort_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2683 uint16_t tgt_id, uint64_t lun_id, uint32_t ctx,
2684 lpfc_ctx_cmd abort_cmd)
2685{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002686 struct lpfc_iocbq *iocbq;
2687 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05002688 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05002689 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002690 int i;
dea31012005-04-17 16:05:31 -05002691
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002692 for (i = 1; i <= phba->sli.last_iotag; i++) {
2693 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05002694
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002695 if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
2696 0, abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05002697 continue;
2698
2699 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002700 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002701 if (abtsiocb == NULL) {
2702 errcnt++;
2703 continue;
2704 }
dea31012005-04-17 16:05:31 -05002705
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002706 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05002707 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
2708 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
2709 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
2710 abtsiocb->iocb.ulpLe = 1;
2711 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2712
2713 if (phba->hba_state >= LPFC_LINK_UP)
2714 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
2715 else
2716 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
2717
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04002718 /* Setup callback routine and issue the command. */
2719 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
dea31012005-04-17 16:05:31 -05002720 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
2721 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05002722 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05002723 errcnt++;
2724 continue;
2725 }
2726 }
2727
2728 return errcnt;
2729}
2730
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002731static void
2732lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
2733 struct lpfc_iocbq *cmdiocbq,
2734 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05002735{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002736 wait_queue_head_t *pdone_q;
2737 unsigned long iflags;
dea31012005-04-17 16:05:31 -05002738
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002739 spin_lock_irqsave(phba->host->host_lock, iflags);
2740 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
2741 if (cmdiocbq->context2 && rspiocbq)
2742 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
2743 &rspiocbq->iocb, sizeof(IOCB_t));
2744
2745 pdone_q = cmdiocbq->context_un.wait_queue;
2746 spin_unlock_irqrestore(phba->host->host_lock, iflags);
2747 if (pdone_q)
2748 wake_up(pdone_q);
dea31012005-04-17 16:05:31 -05002749 return;
2750}
2751
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002752/*
2753 * Issue the caller's iocb and wait for its completion, but no longer than the
2754 * caller's timeout. Note that iocb_flags is cleared before the
2755 * lpfc_sli_issue_call since the wake routine sets a unique value and by
2756 * definition this is a wait function.
2757 */
dea31012005-04-17 16:05:31 -05002758int
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002759lpfc_sli_issue_iocb_wait(struct lpfc_hba * phba,
2760 struct lpfc_sli_ring * pring,
2761 struct lpfc_iocbq * piocb,
2762 struct lpfc_iocbq * prspiocbq,
2763 uint32_t timeout)
dea31012005-04-17 16:05:31 -05002764{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002765 DECLARE_WAIT_QUEUE_HEAD(done_q);
2766 long timeleft, timeout_req = 0;
2767 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002768 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05002769
2770 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002771 * If the caller has provided a response iocbq buffer, then context2
2772 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05002773 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002774 if (prspiocbq) {
2775 if (piocb->context2)
2776 return IOCB_ERROR;
2777 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05002778 }
2779
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002780 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
2781 piocb->context_un.wait_queue = &done_q;
2782 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05002783
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002784 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2785 creg_val = readl(phba->HCregaddr);
2786 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
2787 writel(creg_val, phba->HCregaddr);
2788 readl(phba->HCregaddr); /* flush */
2789 }
2790
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002791 retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
2792 if (retval == IOCB_SUCCESS) {
2793 timeout_req = timeout * HZ;
2794 spin_unlock_irq(phba->host->host_lock);
2795 timeleft = wait_event_timeout(done_q,
2796 piocb->iocb_flag & LPFC_IO_WAKE,
2797 timeout_req);
2798 spin_lock_irq(phba->host->host_lock);
dea31012005-04-17 16:05:31 -05002799
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002800 if (timeleft == 0) {
2801 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2802 "%d:0329 IOCB wait timeout error - no "
2803 "wake response Data x%x\n",
2804 phba->brd_no, timeout);
2805 retval = IOCB_TIMEDOUT;
2806 } else if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
2807 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2808 "%d:0330 IOCB wake NOT set, "
2809 "Data x%x x%lx\n", phba->brd_no,
2810 timeout, (timeleft / jiffies));
2811 retval = IOCB_TIMEDOUT;
2812 } else {
2813 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2814 "%d:0331 IOCB wake signaled\n",
2815 phba->brd_no);
dea31012005-04-17 16:05:31 -05002816 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002817 } else {
2818 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2819 "%d:0332 IOCB wait issue failed, Data x%x\n",
2820 phba->brd_no, retval);
2821 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05002822 }
2823
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002824 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2825 creg_val = readl(phba->HCregaddr);
2826 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
2827 writel(creg_val, phba->HCregaddr);
2828 readl(phba->HCregaddr); /* flush */
2829 }
2830
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002831 if (prspiocbq)
2832 piocb->context2 = NULL;
2833
2834 piocb->context_un.wait_queue = NULL;
2835 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05002836 return retval;
2837}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002838
dea31012005-04-17 16:05:31 -05002839int
2840lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq,
2841 uint32_t timeout)
2842{
2843 DECLARE_WAIT_QUEUE_HEAD(done_q);
2844 DECLARE_WAITQUEUE(wq_entry, current);
2845 uint32_t timeleft = 0;
2846 int retval;
2847
2848 /* The caller must leave context1 empty. */
2849 if (pmboxq->context1 != 0) {
2850 return (MBX_NOT_FINISHED);
2851 }
2852
2853 /* setup wake call as IOCB callback */
2854 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
2855 /* setup context field to pass wait_queue pointer to wake function */
2856 pmboxq->context1 = &done_q;
2857
2858 /* start to sleep before we wait, to avoid races */
2859 set_current_state(TASK_INTERRUPTIBLE);
2860 add_wait_queue(&done_q, &wq_entry);
2861
2862 /* now issue the command */
2863 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
2864
2865 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
2866 timeleft = schedule_timeout(timeout * HZ);
2867 pmboxq->context1 = NULL;
2868 /* if schedule_timeout returns 0, we timed out and were not
2869 woken up */
Jamie Wellnitz406d6042006-02-28 19:25:20 -05002870 if ((timeleft == 0) || signal_pending(current))
dea31012005-04-17 16:05:31 -05002871 retval = MBX_TIMEOUT;
Jamie Wellnitz406d6042006-02-28 19:25:20 -05002872 else
dea31012005-04-17 16:05:31 -05002873 retval = MBX_SUCCESS;
dea31012005-04-17 16:05:31 -05002874 }
2875
2876
2877 set_current_state(TASK_RUNNING);
2878 remove_wait_queue(&done_q, &wq_entry);
2879 return retval;
2880}
2881
2882irqreturn_t
2883lpfc_intr_handler(int irq, void *dev_id, struct pt_regs * regs)
2884{
2885 struct lpfc_hba *phba;
2886 uint32_t ha_copy;
2887 uint32_t work_ha_copy;
2888 unsigned long status;
2889 int i;
2890 uint32_t control;
2891
2892 /*
2893 * Get the driver's phba structure from the dev_id and
2894 * assume the HBA is not interrupting.
2895 */
2896 phba = (struct lpfc_hba *) dev_id;
2897
2898 if (unlikely(!phba))
2899 return IRQ_NONE;
2900
2901 phba->sli.slistat.sli_intr++;
2902
2903 /*
2904 * Call the HBA to see if it is interrupting. If not, don't claim
2905 * the interrupt
2906 */
2907
2908 /* Ignore all interrupts during initialization. */
2909 if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
2910 return IRQ_NONE;
2911
2912 /*
2913 * Read host attention register to determine interrupt source
2914 * Clear Attention Sources, except Error Attention (to
2915 * preserve status) and Link Attention
2916 */
2917 spin_lock(phba->host->host_lock);
2918 ha_copy = readl(phba->HAregaddr);
2919 writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
2920 readl(phba->HAregaddr); /* flush */
2921 spin_unlock(phba->host->host_lock);
2922
2923 if (unlikely(!ha_copy))
2924 return IRQ_NONE;
2925
2926 work_ha_copy = ha_copy & phba->work_ha_mask;
2927
2928 if (unlikely(work_ha_copy)) {
2929 if (work_ha_copy & HA_LATT) {
2930 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
2931 /*
2932 * Turn off Link Attention interrupts
2933 * until CLEAR_LA done
2934 */
2935 spin_lock(phba->host->host_lock);
2936 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
2937 control = readl(phba->HCregaddr);
2938 control &= ~HC_LAINT_ENA;
2939 writel(control, phba->HCregaddr);
2940 readl(phba->HCregaddr); /* flush */
2941 spin_unlock(phba->host->host_lock);
2942 }
2943 else
2944 work_ha_copy &= ~HA_LATT;
2945 }
2946
2947 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
2948 for (i = 0; i < phba->sli.num_rings; i++) {
2949 if (work_ha_copy & (HA_RXATT << (4*i))) {
2950 /*
2951 * Turn off Slow Rings interrupts
2952 */
2953 spin_lock(phba->host->host_lock);
2954 control = readl(phba->HCregaddr);
2955 control &= ~(HC_R0INT_ENA << i);
2956 writel(control, phba->HCregaddr);
2957 readl(phba->HCregaddr); /* flush */
2958 spin_unlock(phba->host->host_lock);
2959 }
2960 }
2961 }
2962
2963 if (work_ha_copy & HA_ERATT) {
2964 phba->hba_state = LPFC_HBA_ERROR;
2965 /*
2966 * There was a link/board error. Read the
2967 * status register to retrieve the error event
2968 * and process it.
2969 */
2970 phba->sli.slistat.err_attn_event++;
2971 /* Save status info */
2972 phba->work_hs = readl(phba->HSregaddr);
2973 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
2974 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
2975
2976 /* Clear Chip error bit */
2977 writel(HA_ERATT, phba->HAregaddr);
2978 readl(phba->HAregaddr); /* flush */
2979
2980 /*
2981 * Reseting the HBA is the only reliable way
2982 * to shutdown interrupt when there is a
2983 * ERROR.
2984 */
Jamie Wellnitz7062c522006-02-28 19:25:17 -05002985 lpfc_sli_send_reset(phba, 1);
dea31012005-04-17 16:05:31 -05002986 }
2987
2988 spin_lock(phba->host->host_lock);
2989 phba->work_ha |= work_ha_copy;
2990 if (phba->work_wait)
2991 wake_up(phba->work_wait);
2992 spin_unlock(phba->host->host_lock);
2993 }
2994
2995 ha_copy &= ~(phba->work_ha_mask);
2996
2997 /*
2998 * Process all events on FCP ring. Take the optimized path for
2999 * FCP IO. Any other IO is slow path and is handled by
3000 * the worker thread.
3001 */
3002 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
3003 status >>= (4*LPFC_FCP_RING);
3004 if (status & HA_RXATT)
3005 lpfc_sli_handle_fast_ring_event(phba,
3006 &phba->sli.ring[LPFC_FCP_RING],
3007 status);
3008 return IRQ_HANDLED;
3009
3010} /* lpfc_intr_handler */