]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/scsi/qla2xxx/qla_isr.c
c8d0a176fea4a1287853716a7dd1d6f20c910060
[net-next-2.6.git] / drivers / scsi / qla2xxx / qla_isr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/delay.h>
10 #include <scsi/scsi_tcq.h>
11
12 static void qla2x00_mbx_completion(scsi_qla_host_t *, uint16_t);
13 static void qla2x00_process_completed_request(struct scsi_qla_host *,
14         struct req_que *, uint32_t);
15 static void qla2x00_status_entry(scsi_qla_host_t *, struct rsp_que *, void *);
16 static void qla2x00_status_cont_entry(struct rsp_que *, sts_cont_entry_t *);
17 static void qla2x00_error_entry(scsi_qla_host_t *, struct rsp_que *,
18         sts_entry_t *);
19
20 /**
21  * qla2100_intr_handler() - Process interrupts for the ISP2100 and ISP2200.
22  * @irq:
23  * @dev_id: SCSI driver HA context
24  *
25  * Called by system whenever the host adapter generates an interrupt.
26  *
27  * Returns handled flag.
28  */
29 irqreturn_t
30 qla2100_intr_handler(int irq, void *dev_id)
31 {
32         scsi_qla_host_t *vha;
33         struct qla_hw_data *ha;
34         struct device_reg_2xxx __iomem *reg;
35         int             status;
36         unsigned long   iter;
37         uint16_t        hccr;
38         uint16_t        mb[4];
39         struct rsp_que *rsp;
40
41         rsp = (struct rsp_que *) dev_id;
42         if (!rsp) {
43                 printk(KERN_INFO
44                     "%s(): NULL response queue pointer\n", __func__);
45                 return (IRQ_NONE);
46         }
47
48         ha = rsp->hw;
49         reg = &ha->iobase->isp;
50         status = 0;
51
52         spin_lock(&ha->hardware_lock);
53         vha = pci_get_drvdata(ha->pdev);
54         for (iter = 50; iter--; ) {
55                 hccr = RD_REG_WORD(&reg->hccr);
56                 if (hccr & HCCR_RISC_PAUSE) {
57                         if (pci_channel_offline(ha->pdev))
58                                 break;
59
60                         /*
61                          * Issue a "HARD" reset in order for the RISC interrupt
62                          * bit to be cleared.  Schedule a big hammmer to get
63                          * out of the RISC PAUSED state.
64                          */
65                         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
66                         RD_REG_WORD(&reg->hccr);
67
68                         ha->isp_ops->fw_dump(vha, 1);
69                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
70                         break;
71                 } else if ((RD_REG_WORD(&reg->istatus) & ISR_RISC_INT) == 0)
72                         break;
73
74                 if (RD_REG_WORD(&reg->semaphore) & BIT_0) {
75                         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
76                         RD_REG_WORD(&reg->hccr);
77
78                         /* Get mailbox data. */
79                         mb[0] = RD_MAILBOX_REG(ha, reg, 0);
80                         if (mb[0] > 0x3fff && mb[0] < 0x8000) {
81                                 qla2x00_mbx_completion(vha, mb[0]);
82                                 status |= MBX_INTERRUPT;
83                         } else if (mb[0] > 0x7fff && mb[0] < 0xc000) {
84                                 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
85                                 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
86                                 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
87                                 qla2x00_async_event(vha, rsp, mb);
88                         } else {
89                                 /*EMPTY*/
90                                 DEBUG2(printk("scsi(%ld): Unrecognized "
91                                     "interrupt type (%d).\n",
92                                     vha->host_no, mb[0]));
93                         }
94                         /* Release mailbox registers. */
95                         WRT_REG_WORD(&reg->semaphore, 0);
96                         RD_REG_WORD(&reg->semaphore);
97                 } else {
98                         qla2x00_process_response_queue(rsp);
99
100                         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
101                         RD_REG_WORD(&reg->hccr);
102                 }
103         }
104         spin_unlock(&ha->hardware_lock);
105
106         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
107             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
108                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
109                 complete(&ha->mbx_intr_comp);
110         }
111
112         return (IRQ_HANDLED);
113 }
114
115 /**
116  * qla2300_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
117  * @irq:
118  * @dev_id: SCSI driver HA context
119  *
120  * Called by system whenever the host adapter generates an interrupt.
121  *
122  * Returns handled flag.
123  */
124 irqreturn_t
125 qla2300_intr_handler(int irq, void *dev_id)
126 {
127         scsi_qla_host_t *vha;
128         struct device_reg_2xxx __iomem *reg;
129         int             status;
130         unsigned long   iter;
131         uint32_t        stat;
132         uint16_t        hccr;
133         uint16_t        mb[4];
134         struct rsp_que *rsp;
135         struct qla_hw_data *ha;
136
137         rsp = (struct rsp_que *) dev_id;
138         if (!rsp) {
139                 printk(KERN_INFO
140                     "%s(): NULL response queue pointer\n", __func__);
141                 return (IRQ_NONE);
142         }
143
144         ha = rsp->hw;
145         reg = &ha->iobase->isp;
146         status = 0;
147
148         spin_lock(&ha->hardware_lock);
149         vha = pci_get_drvdata(ha->pdev);
150         for (iter = 50; iter--; ) {
151                 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
152                 if (stat & HSR_RISC_PAUSED) {
153                         if (pci_channel_offline(ha->pdev))
154                                 break;
155
156                         hccr = RD_REG_WORD(&reg->hccr);
157                         if (hccr & (BIT_15 | BIT_13 | BIT_11 | BIT_8))
158                                 qla_printk(KERN_INFO, ha, "Parity error -- "
159                                     "HCCR=%x, Dumping firmware!\n", hccr);
160                         else
161                                 qla_printk(KERN_INFO, ha, "RISC paused -- "
162                                     "HCCR=%x, Dumping firmware!\n", hccr);
163
164                         /*
165                          * Issue a "HARD" reset in order for the RISC
166                          * interrupt bit to be cleared.  Schedule a big
167                          * hammmer to get out of the RISC PAUSED state.
168                          */
169                         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
170                         RD_REG_WORD(&reg->hccr);
171
172                         ha->isp_ops->fw_dump(vha, 1);
173                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
174                         break;
175                 } else if ((stat & HSR_RISC_INT) == 0)
176                         break;
177
178                 switch (stat & 0xff) {
179                 case 0x1:
180                 case 0x2:
181                 case 0x10:
182                 case 0x11:
183                         qla2x00_mbx_completion(vha, MSW(stat));
184                         status |= MBX_INTERRUPT;
185
186                         /* Release mailbox registers. */
187                         WRT_REG_WORD(&reg->semaphore, 0);
188                         break;
189                 case 0x12:
190                         mb[0] = MSW(stat);
191                         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
192                         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
193                         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
194                         qla2x00_async_event(vha, rsp, mb);
195                         break;
196                 case 0x13:
197                         qla2x00_process_response_queue(rsp);
198                         break;
199                 case 0x15:
200                         mb[0] = MBA_CMPLT_1_16BIT;
201                         mb[1] = MSW(stat);
202                         qla2x00_async_event(vha, rsp, mb);
203                         break;
204                 case 0x16:
205                         mb[0] = MBA_SCSI_COMPLETION;
206                         mb[1] = MSW(stat);
207                         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
208                         qla2x00_async_event(vha, rsp, mb);
209                         break;
210                 default:
211                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
212                             "(%d).\n",
213                             vha->host_no, stat & 0xff));
214                         break;
215                 }
216                 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
217                 RD_REG_WORD_RELAXED(&reg->hccr);
218         }
219         spin_unlock(&ha->hardware_lock);
220
221         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
222             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
223                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
224                 complete(&ha->mbx_intr_comp);
225         }
226
227         return (IRQ_HANDLED);
228 }
229
230 /**
231  * qla2x00_mbx_completion() - Process mailbox command completions.
232  * @ha: SCSI driver HA context
233  * @mb0: Mailbox0 register
234  */
235 static void
236 qla2x00_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
237 {
238         uint16_t        cnt;
239         uint16_t __iomem *wptr;
240         struct qla_hw_data *ha = vha->hw;
241         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
242
243         /* Load return mailbox registers. */
244         ha->flags.mbox_int = 1;
245         ha->mailbox_out[0] = mb0;
246         wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 1);
247
248         for (cnt = 1; cnt < ha->mbx_count; cnt++) {
249                 if (IS_QLA2200(ha) && cnt == 8)
250                         wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 8);
251                 if (cnt == 4 || cnt == 5)
252                         ha->mailbox_out[cnt] = qla2x00_debounce_register(wptr);
253                 else
254                         ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
255
256                 wptr++;
257         }
258
259         if (ha->mcp) {
260                 DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
261                     __func__, vha->host_no, ha->mcp->mb[0]));
262         } else {
263                 DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
264                     __func__, vha->host_no));
265         }
266 }
267
268 static void
269 qla81xx_idc_event(scsi_qla_host_t *vha, uint16_t aen, uint16_t descr)
270 {
271         static char *event[] =
272                 { "Complete", "Request Notification", "Time Extension" };
273         int rval;
274         struct device_reg_24xx __iomem *reg24 = &vha->hw->iobase->isp24;
275         uint16_t __iomem *wptr;
276         uint16_t cnt, timeout, mb[QLA_IDC_ACK_REGS];
277
278         /* Seed data -- mailbox1 -> mailbox7. */
279         wptr = (uint16_t __iomem *)&reg24->mailbox1;
280         for (cnt = 0; cnt < QLA_IDC_ACK_REGS; cnt++, wptr++)
281                 mb[cnt] = RD_REG_WORD(wptr);
282
283         DEBUG2(printk("scsi(%ld): Inter-Driver Commucation %s -- "
284             "%04x %04x %04x %04x %04x %04x %04x.\n", vha->host_no,
285             event[aen & 0xff],
286             mb[0], mb[1], mb[2], mb[3], mb[4], mb[5], mb[6]));
287
288         /* Acknowledgement needed? [Notify && non-zero timeout]. */
289         timeout = (descr >> 8) & 0xf;
290         if (aen != MBA_IDC_NOTIFY || !timeout)
291                 return;
292
293         DEBUG2(printk("scsi(%ld): Inter-Driver Commucation %s -- "
294             "ACK timeout=%d.\n", vha->host_no, event[aen & 0xff], timeout));
295
296         rval = qla2x00_post_idc_ack_work(vha, mb);
297         if (rval != QLA_SUCCESS)
298                 qla_printk(KERN_WARNING, vha->hw,
299                     "IDC failed to post ACK.\n");
300 }
301
302 /**
303  * qla2x00_async_event() - Process aynchronous events.
304  * @ha: SCSI driver HA context
305  * @mb: Mailbox registers (0 - 3)
306  */
307 void
308 qla2x00_async_event(scsi_qla_host_t *vha, struct rsp_que *rsp, uint16_t *mb)
309 {
310 #define LS_UNKNOWN      2
311         static char     *link_speeds[] = { "1", "2", "?", "4", "8", "10" };
312         char            *link_speed;
313         uint16_t        handle_cnt;
314         uint16_t        cnt;
315         uint32_t        handles[5];
316         struct qla_hw_data *ha = vha->hw;
317         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
318         uint32_t        rscn_entry, host_pid;
319         uint8_t         rscn_queue_index;
320         unsigned long   flags;
321
322         /* Setup to process RIO completion. */
323         handle_cnt = 0;
324         if (IS_QLA81XX(ha))
325                 goto skip_rio;
326         switch (mb[0]) {
327         case MBA_SCSI_COMPLETION:
328                 handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
329                 handle_cnt = 1;
330                 break;
331         case MBA_CMPLT_1_16BIT:
332                 handles[0] = mb[1];
333                 handle_cnt = 1;
334                 mb[0] = MBA_SCSI_COMPLETION;
335                 break;
336         case MBA_CMPLT_2_16BIT:
337                 handles[0] = mb[1];
338                 handles[1] = mb[2];
339                 handle_cnt = 2;
340                 mb[0] = MBA_SCSI_COMPLETION;
341                 break;
342         case MBA_CMPLT_3_16BIT:
343                 handles[0] = mb[1];
344                 handles[1] = mb[2];
345                 handles[2] = mb[3];
346                 handle_cnt = 3;
347                 mb[0] = MBA_SCSI_COMPLETION;
348                 break;
349         case MBA_CMPLT_4_16BIT:
350                 handles[0] = mb[1];
351                 handles[1] = mb[2];
352                 handles[2] = mb[3];
353                 handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
354                 handle_cnt = 4;
355                 mb[0] = MBA_SCSI_COMPLETION;
356                 break;
357         case MBA_CMPLT_5_16BIT:
358                 handles[0] = mb[1];
359                 handles[1] = mb[2];
360                 handles[2] = mb[3];
361                 handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
362                 handles[4] = (uint32_t)RD_MAILBOX_REG(ha, reg, 7);
363                 handle_cnt = 5;
364                 mb[0] = MBA_SCSI_COMPLETION;
365                 break;
366         case MBA_CMPLT_2_32BIT:
367                 handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
368                 handles[1] = le32_to_cpu(
369                     ((uint32_t)(RD_MAILBOX_REG(ha, reg, 7) << 16)) |
370                     RD_MAILBOX_REG(ha, reg, 6));
371                 handle_cnt = 2;
372                 mb[0] = MBA_SCSI_COMPLETION;
373                 break;
374         default:
375                 break;
376         }
377 skip_rio:
378         switch (mb[0]) {
379         case MBA_SCSI_COMPLETION:       /* Fast Post */
380                 if (!vha->flags.online)
381                         break;
382
383                 for (cnt = 0; cnt < handle_cnt; cnt++)
384                         qla2x00_process_completed_request(vha, rsp->req,
385                                 handles[cnt]);
386                 break;
387
388         case MBA_RESET:                 /* Reset */
389                 DEBUG2(printk("scsi(%ld): Asynchronous RESET.\n",
390                         vha->host_no));
391
392                 set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
393                 break;
394
395         case MBA_SYSTEM_ERR:            /* System Error */
396                 qla_printk(KERN_INFO, ha,
397                     "ISP System Error - mbx1=%xh mbx2=%xh mbx3=%xh.\n",
398                     mb[1], mb[2], mb[3]);
399
400                 ha->isp_ops->fw_dump(vha, 1);
401
402                 if (IS_FWI2_CAPABLE(ha)) {
403                         if (mb[1] == 0 && mb[2] == 0) {
404                                 qla_printk(KERN_ERR, ha,
405                                     "Unrecoverable Hardware Error: adapter "
406                                     "marked OFFLINE!\n");
407                                 vha->flags.online = 0;
408                         } else
409                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
410                 } else if (mb[1] == 0) {
411                         qla_printk(KERN_INFO, ha,
412                             "Unrecoverable Hardware Error: adapter marked "
413                             "OFFLINE!\n");
414                         vha->flags.online = 0;
415                 } else
416                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
417                 break;
418
419         case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
420                 DEBUG2(printk("scsi(%ld): ISP Request Transfer Error.\n",
421                     vha->host_no));
422                 qla_printk(KERN_WARNING, ha, "ISP Request Transfer Error.\n");
423
424                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
425                 break;
426
427         case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
428                 DEBUG2(printk("scsi(%ld): ISP Response Transfer Error.\n",
429                     vha->host_no));
430                 qla_printk(KERN_WARNING, ha, "ISP Response Transfer Error.\n");
431
432                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
433                 break;
434
435         case MBA_WAKEUP_THRES:          /* Request Queue Wake-up */
436                 DEBUG2(printk("scsi(%ld): Asynchronous WAKEUP_THRES.\n",
437                     vha->host_no));
438                 break;
439
440         case MBA_LIP_OCCURRED:          /* Loop Initialization Procedure */
441                 DEBUG2(printk("scsi(%ld): LIP occurred (%x).\n", vha->host_no,
442                     mb[1]));
443                 qla_printk(KERN_INFO, ha, "LIP occurred (%x).\n", mb[1]);
444
445                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
446                         atomic_set(&vha->loop_state, LOOP_DOWN);
447                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
448                         qla2x00_mark_all_devices_lost(vha, 1);
449                 }
450
451                 if (vha->vp_idx) {
452                         atomic_set(&vha->vp_state, VP_FAILED);
453                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
454                 }
455
456                 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
457                 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
458
459                 vha->flags.management_server_logged_in = 0;
460                 qla2x00_post_aen_work(vha, FCH_EVT_LIP, mb[1]);
461                 break;
462
463         case MBA_LOOP_UP:               /* Loop Up Event */
464                 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
465                         link_speed = link_speeds[0];
466                         ha->link_data_rate = PORT_SPEED_1GB;
467                 } else {
468                         link_speed = link_speeds[LS_UNKNOWN];
469                         if (mb[1] < 5)
470                                 link_speed = link_speeds[mb[1]];
471                         else if (mb[1] == 0x13)
472                                 link_speed = link_speeds[5];
473                         ha->link_data_rate = mb[1];
474                 }
475
476                 DEBUG2(printk("scsi(%ld): Asynchronous LOOP UP (%s Gbps).\n",
477                     vha->host_no, link_speed));
478                 qla_printk(KERN_INFO, ha, "LOOP UP detected (%s Gbps).\n",
479                     link_speed);
480
481                 vha->flags.management_server_logged_in = 0;
482                 qla2x00_post_aen_work(vha, FCH_EVT_LINKUP, ha->link_data_rate);
483                 break;
484
485         case MBA_LOOP_DOWN:             /* Loop Down Event */
486                 DEBUG2(printk("scsi(%ld): Asynchronous LOOP DOWN "
487                     "(%x %x %x).\n", vha->host_no, mb[1], mb[2], mb[3]));
488                 qla_printk(KERN_INFO, ha, "LOOP DOWN detected (%x %x %x).\n",
489                     mb[1], mb[2], mb[3]);
490
491                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
492                         atomic_set(&vha->loop_state, LOOP_DOWN);
493                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
494                         vha->device_flags |= DFLG_NO_CABLE;
495                         qla2x00_mark_all_devices_lost(vha, 1);
496                 }
497
498                 if (vha->vp_idx) {
499                         atomic_set(&vha->vp_state, VP_FAILED);
500                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
501                 }
502
503                 vha->flags.management_server_logged_in = 0;
504                 ha->link_data_rate = PORT_SPEED_UNKNOWN;
505                 qla2x00_post_aen_work(vha, FCH_EVT_LINKDOWN, 0);
506                 break;
507
508         case MBA_LIP_RESET:             /* LIP reset occurred */
509                 DEBUG2(printk("scsi(%ld): Asynchronous LIP RESET (%x).\n",
510                     vha->host_no, mb[1]));
511                 qla_printk(KERN_INFO, ha,
512                     "LIP reset occurred (%x).\n", mb[1]);
513
514                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
515                         atomic_set(&vha->loop_state, LOOP_DOWN);
516                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
517                         qla2x00_mark_all_devices_lost(vha, 1);
518                 }
519
520                 if (vha->vp_idx) {
521                         atomic_set(&vha->vp_state, VP_FAILED);
522                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
523                 }
524
525                 set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
526
527                 ha->operating_mode = LOOP;
528                 vha->flags.management_server_logged_in = 0;
529                 qla2x00_post_aen_work(vha, FCH_EVT_LIPRESET, mb[1]);
530                 break;
531
532         /* case MBA_DCBX_COMPLETE: */
533         case MBA_POINT_TO_POINT:        /* Point-to-Point */
534                 if (IS_QLA2100(ha))
535                         break;
536
537                 if (IS_QLA81XX(ha))
538                         DEBUG2(printk("scsi(%ld): DCBX Completed -- %04x %04x "
539                             "%04x\n", vha->host_no, mb[1], mb[2], mb[3]));
540                 else
541                         DEBUG2(printk("scsi(%ld): Asynchronous P2P MODE "
542                             "received.\n", vha->host_no));
543
544                 /*
545                  * Until there's a transition from loop down to loop up, treat
546                  * this as loop down only.
547                  */
548                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
549                         atomic_set(&vha->loop_state, LOOP_DOWN);
550                         if (!atomic_read(&vha->loop_down_timer))
551                                 atomic_set(&vha->loop_down_timer,
552                                     LOOP_DOWN_TIME);
553                         qla2x00_mark_all_devices_lost(vha, 1);
554                 }
555
556                 if (vha->vp_idx) {
557                         atomic_set(&vha->vp_state, VP_FAILED);
558                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
559                 }
560
561                 if (!(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)))
562                         set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
563
564                 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
565                 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
566
567                 ha->flags.gpsc_supported = 1;
568                 vha->flags.management_server_logged_in = 0;
569                 break;
570
571         case MBA_CHG_IN_CONNECTION:     /* Change in connection mode */
572                 if (IS_QLA2100(ha))
573                         break;
574
575                 DEBUG2(printk("scsi(%ld): Asynchronous Change In Connection "
576                     "received.\n",
577                     vha->host_no));
578                 qla_printk(KERN_INFO, ha,
579                     "Configuration change detected: value=%x.\n", mb[1]);
580
581                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
582                         atomic_set(&vha->loop_state, LOOP_DOWN);
583                         if (!atomic_read(&vha->loop_down_timer))
584                                 atomic_set(&vha->loop_down_timer,
585                                     LOOP_DOWN_TIME);
586                         qla2x00_mark_all_devices_lost(vha, 1);
587                 }
588
589                 if (vha->vp_idx) {
590                         atomic_set(&vha->vp_state, VP_FAILED);
591                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
592                 }
593
594                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
595                 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
596                 break;
597
598         case MBA_PORT_UPDATE:           /* Port database update */
599                 /* Only handle SCNs for our Vport index. */
600                 if (vha->vp_idx && vha->vp_idx != (mb[3] & 0xff))
601                         break;
602
603                 /*
604                  * If PORT UPDATE is global (received LIP_OCCURRED/LIP_RESET
605                  * event etc. earlier indicating loop is down) then process
606                  * it.  Otherwise ignore it and Wait for RSCN to come in.
607                  */
608                 atomic_set(&vha->loop_down_timer, 0);
609                 if (atomic_read(&vha->loop_state) != LOOP_DOWN &&
610                     atomic_read(&vha->loop_state) != LOOP_DEAD) {
611                         DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE "
612                             "ignored %04x/%04x/%04x.\n", vha->host_no, mb[1],
613                             mb[2], mb[3]));
614                         break;
615                 }
616
617                 DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE.\n",
618                     vha->host_no));
619                 DEBUG(printk(KERN_INFO
620                     "scsi(%ld): Port database changed %04x %04x %04x.\n",
621                     vha->host_no, mb[1], mb[2], mb[3]));
622
623                 /*
624                  * Mark all devices as missing so we will login again.
625                  */
626                 atomic_set(&vha->loop_state, LOOP_UP);
627
628                 qla2x00_mark_all_devices_lost(vha, 1);
629
630                 vha->flags.rscn_queue_overflow = 1;
631
632                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
633                 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
634                 break;
635
636         case MBA_RSCN_UPDATE:           /* State Change Registration */
637                 /* Check if the Vport has issued a SCR */
638                 if (vha->vp_idx && test_bit(VP_SCR_NEEDED, &vha->vp_flags))
639                         break;
640                 /* Only handle SCNs for our Vport index. */
641                 if (vha->vp_idx && vha->vp_idx != (mb[3] & 0xff))
642                         break;
643                 DEBUG2(printk("scsi(%ld): Asynchronous RSCR UPDATE.\n",
644                     vha->host_no));
645                 DEBUG(printk(KERN_INFO
646                     "scsi(%ld): RSCN database changed -- %04x %04x %04x.\n",
647                     vha->host_no, mb[1], mb[2], mb[3]));
648
649                 rscn_entry = ((mb[1] & 0xff) << 16) | mb[2];
650                 host_pid = (vha->d_id.b.domain << 16) | (vha->d_id.b.area << 8)
651                                 | vha->d_id.b.al_pa;
652                 if (rscn_entry == host_pid) {
653                         DEBUG(printk(KERN_INFO
654                             "scsi(%ld): Ignoring RSCN update to local host "
655                             "port ID (%06x)\n",
656                             vha->host_no, host_pid));
657                         break;
658                 }
659
660                 /* Ignore reserved bits from RSCN-payload. */
661                 rscn_entry = ((mb[1] & 0x3ff) << 16) | mb[2];
662                 rscn_queue_index = vha->rscn_in_ptr + 1;
663                 if (rscn_queue_index == MAX_RSCN_COUNT)
664                         rscn_queue_index = 0;
665                 if (rscn_queue_index != vha->rscn_out_ptr) {
666                         vha->rscn_queue[vha->rscn_in_ptr] = rscn_entry;
667                         vha->rscn_in_ptr = rscn_queue_index;
668                 } else {
669                         vha->flags.rscn_queue_overflow = 1;
670                 }
671
672                 atomic_set(&vha->loop_state, LOOP_UPDATE);
673                 atomic_set(&vha->loop_down_timer, 0);
674                 vha->flags.management_server_logged_in = 0;
675
676                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
677                 set_bit(RSCN_UPDATE, &vha->dpc_flags);
678                 qla2x00_post_aen_work(vha, FCH_EVT_RSCN, rscn_entry);
679                 break;
680
681         /* case MBA_RIO_RESPONSE: */
682         case MBA_ZIO_RESPONSE:
683                 DEBUG3(printk("scsi(%ld): [R|Z]IO update completion.\n",
684                     vha->host_no));
685
686                 if (IS_FWI2_CAPABLE(ha))
687                         qla24xx_process_response_queue(vha, rsp);
688                 else
689                         qla2x00_process_response_queue(rsp);
690                 break;
691
692         case MBA_DISCARD_RND_FRAME:
693                 DEBUG2(printk("scsi(%ld): Discard RND Frame -- %04x %04x "
694                     "%04x.\n", vha->host_no, mb[1], mb[2], mb[3]));
695                 break;
696
697         case MBA_TRACE_NOTIFICATION:
698                 DEBUG2(printk("scsi(%ld): Trace Notification -- %04x %04x.\n",
699                 vha->host_no, mb[1], mb[2]));
700                 break;
701
702         case MBA_ISP84XX_ALERT:
703                 DEBUG2(printk("scsi(%ld): ISP84XX Alert Notification -- "
704                     "%04x %04x %04x\n", vha->host_no, mb[1], mb[2], mb[3]));
705
706                 spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
707                 switch (mb[1]) {
708                 case A84_PANIC_RECOVERY:
709                         qla_printk(KERN_INFO, ha, "Alert 84XX: panic recovery "
710                             "%04x %04x\n", mb[2], mb[3]);
711                         break;
712                 case A84_OP_LOGIN_COMPLETE:
713                         ha->cs84xx->op_fw_version = mb[3] << 16 | mb[2];
714                         DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
715                             "firmware version %x\n", ha->cs84xx->op_fw_version));
716                         break;
717                 case A84_DIAG_LOGIN_COMPLETE:
718                         ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
719                         DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
720                             "diagnostic firmware version %x\n",
721                             ha->cs84xx->diag_fw_version));
722                         break;
723                 case A84_GOLD_LOGIN_COMPLETE:
724                         ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
725                         ha->cs84xx->fw_update = 1;
726                         DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX: gold "
727                             "firmware version %x\n",
728                             ha->cs84xx->gold_fw_version));
729                         break;
730                 default:
731                         qla_printk(KERN_ERR, ha,
732                             "Alert 84xx: Invalid Alert %04x %04x %04x\n",
733                             mb[1], mb[2], mb[3]);
734                 }
735                 spin_unlock_irqrestore(&ha->cs84xx->access_lock, flags);
736                 break;
737         case MBA_DCBX_START:
738                 DEBUG2(printk("scsi(%ld): DCBX Started -- %04x %04x %04x\n",
739                     vha->host_no, mb[1], mb[2], mb[3]));
740                 break;
741         case MBA_DCBX_PARAM_UPDATE:
742                 DEBUG2(printk("scsi(%ld): DCBX Parameters Updated -- "
743                     "%04x %04x %04x\n", vha->host_no, mb[1], mb[2], mb[3]));
744                 break;
745         case MBA_FCF_CONF_ERR:
746                 DEBUG2(printk("scsi(%ld): FCF Configuration Error -- "
747                     "%04x %04x %04x\n", vha->host_no, mb[1], mb[2], mb[3]));
748                 break;
749         case MBA_IDC_COMPLETE:
750         case MBA_IDC_NOTIFY:
751         case MBA_IDC_TIME_EXT:
752                 qla81xx_idc_event(vha, mb[0], mb[1]);
753                 break;
754         }
755
756         if (!vha->vp_idx && ha->num_vhosts)
757                 qla2x00_alert_all_vps(rsp, mb);
758 }
759
760 static void
761 qla2x00_adjust_sdev_qdepth_up(struct scsi_device *sdev, void *data)
762 {
763         fc_port_t *fcport = data;
764         struct scsi_qla_host *vha = fcport->vha;
765         struct qla_hw_data *ha = vha->hw;
766         struct req_que *req = NULL;
767
768         if (!ql2xqfulltracking)
769                 return;
770
771         req = vha->req;
772         if (!req)
773                 return;
774         if (req->max_q_depth <= sdev->queue_depth)
775                 return;
776
777         if (sdev->ordered_tags)
778                 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG,
779                     sdev->queue_depth + 1);
780         else
781                 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG,
782                     sdev->queue_depth + 1);
783
784         fcport->last_ramp_up = jiffies;
785
786         DEBUG2(qla_printk(KERN_INFO, ha,
787             "scsi(%ld:%d:%d:%d): Queue depth adjusted-up to %d.\n",
788             fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
789             sdev->queue_depth));
790 }
791
792 static void
793 qla2x00_adjust_sdev_qdepth_down(struct scsi_device *sdev, void *data)
794 {
795         fc_port_t *fcport = data;
796
797         if (!scsi_track_queue_full(sdev, sdev->queue_depth - 1))
798                 return;
799
800         DEBUG2(qla_printk(KERN_INFO, fcport->vha->hw,
801             "scsi(%ld:%d:%d:%d): Queue depth adjusted-down to %d.\n",
802             fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
803             sdev->queue_depth));
804 }
805
806 static inline void
807 qla2x00_ramp_up_queue_depth(scsi_qla_host_t *vha, struct req_que *req,
808                                                                 srb_t *sp)
809 {
810         fc_port_t *fcport;
811         struct scsi_device *sdev;
812
813         if (!ql2xqfulltracking)
814                 return;
815
816         sdev = sp->cmd->device;
817         if (sdev->queue_depth >= req->max_q_depth)
818                 return;
819
820         fcport = sp->fcport;
821         if (time_before(jiffies,
822             fcport->last_ramp_up + ql2xqfullrampup * HZ))
823                 return;
824         if (time_before(jiffies,
825             fcport->last_queue_full + ql2xqfullrampup * HZ))
826                 return;
827
828         starget_for_each_device(sdev->sdev_target, fcport,
829             qla2x00_adjust_sdev_qdepth_up);
830 }
831
832 /**
833  * qla2x00_process_completed_request() - Process a Fast Post response.
834  * @ha: SCSI driver HA context
835  * @index: SRB index
836  */
837 static void
838 qla2x00_process_completed_request(struct scsi_qla_host *vha,
839                                 struct req_que *req, uint32_t index)
840 {
841         srb_t *sp;
842         struct qla_hw_data *ha = vha->hw;
843
844         /* Validate handle. */
845         if (index >= MAX_OUTSTANDING_COMMANDS) {
846                 DEBUG2(printk("scsi(%ld): Invalid SCSI completion handle %d.\n",
847                     vha->host_no, index));
848                 qla_printk(KERN_WARNING, ha,
849                     "Invalid SCSI completion handle %d.\n", index);
850
851                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
852                 return;
853         }
854
855         sp = req->outstanding_cmds[index];
856         if (sp) {
857                 /* Free outstanding command slot. */
858                 req->outstanding_cmds[index] = NULL;
859
860                 /* Save ISP completion status */
861                 sp->cmd->result = DID_OK << 16;
862
863                 qla2x00_ramp_up_queue_depth(vha, req, sp);
864                 qla2x00_sp_compl(ha, sp);
865         } else {
866                 DEBUG2(printk("scsi(%ld) Req:%d: Invalid ISP SCSI completion"
867                         " handle(%d)\n", vha->host_no, req->id, index));
868                 qla_printk(KERN_WARNING, ha,
869                     "Invalid ISP SCSI completion handle\n");
870
871                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
872         }
873 }
874
875 /**
876  * qla2x00_process_response_queue() - Process response queue entries.
877  * @ha: SCSI driver HA context
878  */
879 void
880 qla2x00_process_response_queue(struct rsp_que *rsp)
881 {
882         struct scsi_qla_host *vha;
883         struct qla_hw_data *ha = rsp->hw;
884         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
885         sts_entry_t     *pkt;
886         uint16_t        handle_cnt;
887         uint16_t        cnt;
888
889         vha = pci_get_drvdata(ha->pdev);
890
891         if (!vha->flags.online)
892                 return;
893
894         while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
895                 pkt = (sts_entry_t *)rsp->ring_ptr;
896
897                 rsp->ring_index++;
898                 if (rsp->ring_index == rsp->length) {
899                         rsp->ring_index = 0;
900                         rsp->ring_ptr = rsp->ring;
901                 } else {
902                         rsp->ring_ptr++;
903                 }
904
905                 if (pkt->entry_status != 0) {
906                         DEBUG3(printk(KERN_INFO
907                             "scsi(%ld): Process error entry.\n", vha->host_no));
908
909                         qla2x00_error_entry(vha, rsp, pkt);
910                         ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
911                         wmb();
912                         continue;
913                 }
914
915                 switch (pkt->entry_type) {
916                 case STATUS_TYPE:
917                         qla2x00_status_entry(vha, rsp, pkt);
918                         break;
919                 case STATUS_TYPE_21:
920                         handle_cnt = ((sts21_entry_t *)pkt)->handle_count;
921                         for (cnt = 0; cnt < handle_cnt; cnt++) {
922                                 qla2x00_process_completed_request(vha, rsp->req,
923                                     ((sts21_entry_t *)pkt)->handle[cnt]);
924                         }
925                         break;
926                 case STATUS_TYPE_22:
927                         handle_cnt = ((sts22_entry_t *)pkt)->handle_count;
928                         for (cnt = 0; cnt < handle_cnt; cnt++) {
929                                 qla2x00_process_completed_request(vha, rsp->req,
930                                     ((sts22_entry_t *)pkt)->handle[cnt]);
931                         }
932                         break;
933                 case STATUS_CONT_TYPE:
934                         qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
935                         break;
936                 default:
937                         /* Type Not Supported. */
938                         DEBUG4(printk(KERN_WARNING
939                             "scsi(%ld): Received unknown response pkt type %x "
940                             "entry status=%x.\n",
941                             vha->host_no, pkt->entry_type, pkt->entry_status));
942                         break;
943                 }
944                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
945                 wmb();
946         }
947
948         /* Adjust ring index */
949         WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), rsp->ring_index);
950 }
951
952 static inline void
953 qla2x00_handle_sense(srb_t *sp, uint8_t *sense_data, uint32_t sense_len,
954         struct rsp_que *rsp)
955 {
956         struct scsi_cmnd *cp = sp->cmd;
957
958         if (sense_len >= SCSI_SENSE_BUFFERSIZE)
959                 sense_len = SCSI_SENSE_BUFFERSIZE;
960
961         sp->request_sense_length = sense_len;
962         sp->request_sense_ptr = cp->sense_buffer;
963         if (sp->request_sense_length > 32)
964                 sense_len = 32;
965
966         memcpy(cp->sense_buffer, sense_data, sense_len);
967
968         sp->request_sense_ptr += sense_len;
969         sp->request_sense_length -= sense_len;
970         if (sp->request_sense_length != 0)
971                 rsp->status_srb = sp;
972
973         DEBUG5(printk("%s(): Check condition Sense data, scsi(%ld:%d:%d:%d) "
974             "cmd=%p pid=%ld\n", __func__, sp->fcport->vha->host_no,
975             cp->device->channel, cp->device->id, cp->device->lun, cp,
976             cp->serial_number));
977         if (sense_len)
978                 DEBUG5(qla2x00_dump_buffer(cp->sense_buffer, sense_len));
979 }
980
981 /**
982  * qla2x00_status_entry() - Process a Status IOCB entry.
983  * @ha: SCSI driver HA context
984  * @pkt: Entry pointer
985  */
986 static void
987 qla2x00_status_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, void *pkt)
988 {
989         srb_t           *sp;
990         fc_port_t       *fcport;
991         struct scsi_cmnd *cp;
992         sts_entry_t *sts;
993         struct sts_entry_24xx *sts24;
994         uint16_t        comp_status;
995         uint16_t        scsi_status;
996         uint8_t         lscsi_status;
997         int32_t         resid;
998         uint32_t        sense_len, rsp_info_len, resid_len, fw_resid_len;
999         uint8_t         *rsp_info, *sense_data;
1000         struct qla_hw_data *ha = vha->hw;
1001         uint32_t handle;
1002         uint16_t que;
1003         struct req_que *req;
1004
1005         sts = (sts_entry_t *) pkt;
1006         sts24 = (struct sts_entry_24xx *) pkt;
1007         if (IS_FWI2_CAPABLE(ha)) {
1008                 comp_status = le16_to_cpu(sts24->comp_status);
1009                 scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
1010         } else {
1011                 comp_status = le16_to_cpu(sts->comp_status);
1012                 scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
1013         }
1014         handle = (uint32_t) LSW(sts->handle);
1015         que = MSW(sts->handle);
1016         req = ha->req_q_map[que];
1017         /* Fast path completion. */
1018         if (comp_status == CS_COMPLETE && scsi_status == 0) {
1019                 qla2x00_process_completed_request(vha, req, handle);
1020
1021                 return;
1022         }
1023
1024         /* Validate handle. */
1025         if (handle < MAX_OUTSTANDING_COMMANDS) {
1026                 sp = req->outstanding_cmds[handle];
1027                 req->outstanding_cmds[handle] = NULL;
1028         } else
1029                 sp = NULL;
1030
1031         if (sp == NULL) {
1032                 DEBUG2(printk("scsi(%ld): Status Entry invalid handle.\n",
1033                     vha->host_no));
1034                 qla_printk(KERN_WARNING, ha, "Status Entry invalid handle.\n");
1035
1036                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1037                 qla2xxx_wake_dpc(vha);
1038                 return;
1039         }
1040         cp = sp->cmd;
1041         if (cp == NULL) {
1042                 DEBUG2(printk("scsi(%ld): Command already returned back to OS "
1043                     "pkt->handle=%d sp=%p.\n", vha->host_no, handle, sp));
1044                 qla_printk(KERN_WARNING, ha,
1045                     "Command is NULL: already returned to OS (sp=%p)\n", sp);
1046
1047                 return;
1048         }
1049
1050         lscsi_status = scsi_status & STATUS_MASK;
1051
1052         fcport = sp->fcport;
1053
1054         sense_len = rsp_info_len = resid_len = fw_resid_len = 0;
1055         if (IS_FWI2_CAPABLE(ha)) {
1056                 sense_len = le32_to_cpu(sts24->sense_len);
1057                 rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
1058                 resid_len = le32_to_cpu(sts24->rsp_residual_count);
1059                 fw_resid_len = le32_to_cpu(sts24->residual_len);
1060                 rsp_info = sts24->data;
1061                 sense_data = sts24->data;
1062                 host_to_fcp_swap(sts24->data, sizeof(sts24->data));
1063         } else {
1064                 sense_len = le16_to_cpu(sts->req_sense_length);
1065                 rsp_info_len = le16_to_cpu(sts->rsp_info_len);
1066                 resid_len = le32_to_cpu(sts->residual_length);
1067                 rsp_info = sts->rsp_info;
1068                 sense_data = sts->req_sense_data;
1069         }
1070
1071         /* Check for any FCP transport errors. */
1072         if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
1073                 /* Sense data lies beyond any FCP RESPONSE data. */
1074                 if (IS_FWI2_CAPABLE(ha))
1075                         sense_data += rsp_info_len;
1076                 if (rsp_info_len > 3 && rsp_info[3]) {
1077                         DEBUG2(printk("scsi(%ld:%d:%d:%d) FCP I/O protocol "
1078                             "failure (%x/%02x%02x%02x%02x%02x%02x%02x%02x)..."
1079                             "retrying command\n", vha->host_no,
1080                             cp->device->channel, cp->device->id,
1081                             cp->device->lun, rsp_info_len, rsp_info[0],
1082                             rsp_info[1], rsp_info[2], rsp_info[3], rsp_info[4],
1083                             rsp_info[5], rsp_info[6], rsp_info[7]));
1084
1085                         cp->result = DID_BUS_BUSY << 16;
1086                         qla2x00_sp_compl(ha, sp);
1087                         return;
1088                 }
1089         }
1090
1091         /* Check for overrun. */
1092         if (IS_FWI2_CAPABLE(ha) && comp_status == CS_COMPLETE &&
1093             scsi_status & SS_RESIDUAL_OVER)
1094                 comp_status = CS_DATA_OVERRUN;
1095
1096         /*
1097          * Based on Host and scsi status generate status code for Linux
1098          */
1099         switch (comp_status) {
1100         case CS_COMPLETE:
1101         case CS_QUEUE_FULL:
1102                 if (scsi_status == 0) {
1103                         cp->result = DID_OK << 16;
1104                         break;
1105                 }
1106                 if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
1107                         resid = resid_len;
1108                         scsi_set_resid(cp, resid);
1109
1110                         if (!lscsi_status &&
1111                             ((unsigned)(scsi_bufflen(cp) - resid) <
1112                              cp->underflow)) {
1113                                 qla_printk(KERN_INFO, ha,
1114                                            "scsi(%ld:%d:%d:%d): Mid-layer underflow "
1115                                            "detected (%x of %x bytes)...returning "
1116                                            "error status.\n", vha->host_no,
1117                                            cp->device->channel, cp->device->id,
1118                                            cp->device->lun, resid,
1119                                            scsi_bufflen(cp));
1120
1121                                 cp->result = DID_ERROR << 16;
1122                                 break;
1123                         }
1124                 }
1125                 cp->result = DID_OK << 16 | lscsi_status;
1126
1127                 if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1128                         DEBUG2(printk(KERN_INFO
1129                             "scsi(%ld): QUEUE FULL status detected "
1130                             "0x%x-0x%x.\n", vha->host_no, comp_status,
1131                             scsi_status));
1132
1133                         /* Adjust queue depth for all luns on the port. */
1134                         if (!ql2xqfulltracking)
1135                                 break;
1136                         fcport->last_queue_full = jiffies;
1137                         starget_for_each_device(cp->device->sdev_target,
1138                             fcport, qla2x00_adjust_sdev_qdepth_down);
1139                         break;
1140                 }
1141                 if (lscsi_status != SS_CHECK_CONDITION)
1142                         break;
1143
1144                 memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1145                 if (!(scsi_status & SS_SENSE_LEN_VALID))
1146                         break;
1147
1148                 qla2x00_handle_sense(sp, sense_data, sense_len, rsp);
1149                 break;
1150
1151         case CS_DATA_UNDERRUN:
1152                 resid = resid_len;
1153                 /* Use F/W calculated residual length. */
1154                 if (IS_FWI2_CAPABLE(ha)) {
1155                         if (!(scsi_status & SS_RESIDUAL_UNDER)) {
1156                                 lscsi_status = 0;
1157                         } else if (resid != fw_resid_len) {
1158                                 scsi_status &= ~SS_RESIDUAL_UNDER;
1159                                 lscsi_status = 0;
1160                         }
1161                         resid = fw_resid_len;
1162                 }
1163
1164                 if (scsi_status & SS_RESIDUAL_UNDER) {
1165                         scsi_set_resid(cp, resid);
1166                 } else {
1167                         DEBUG2(printk(KERN_INFO
1168                             "scsi(%ld:%d:%d) UNDERRUN status detected "
1169                             "0x%x-0x%x. resid=0x%x fw_resid=0x%x cdb=0x%x "
1170                             "os_underflow=0x%x\n", vha->host_no,
1171                             cp->device->id, cp->device->lun, comp_status,
1172                             scsi_status, resid_len, resid, cp->cmnd[0],
1173                             cp->underflow));
1174
1175                 }
1176
1177                 /*
1178                  * Check to see if SCSI Status is non zero. If so report SCSI
1179                  * Status.
1180                  */
1181                 if (lscsi_status != 0) {
1182                         cp->result = DID_OK << 16 | lscsi_status;
1183
1184                         if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1185                                 DEBUG2(printk(KERN_INFO
1186                                     "scsi(%ld): QUEUE FULL status detected "
1187                                     "0x%x-0x%x.\n", vha->host_no, comp_status,
1188                                     scsi_status));
1189
1190                                 /*
1191                                  * Adjust queue depth for all luns on the
1192                                  * port.
1193                                  */
1194                                 if (!ql2xqfulltracking)
1195                                         break;
1196                                 fcport->last_queue_full = jiffies;
1197                                 starget_for_each_device(
1198                                     cp->device->sdev_target, fcport,
1199                                     qla2x00_adjust_sdev_qdepth_down);
1200                                 break;
1201                         }
1202                         if (lscsi_status != SS_CHECK_CONDITION)
1203                                 break;
1204
1205                         memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1206                         if (!(scsi_status & SS_SENSE_LEN_VALID))
1207                                 break;
1208
1209                         qla2x00_handle_sense(sp, sense_data, sense_len, rsp);
1210                 } else {
1211                         /*
1212                          * If RISC reports underrun and target does not report
1213                          * it then we must have a lost frame, so tell upper
1214                          * layer to retry it by reporting an error.
1215                          */
1216                         if (!(scsi_status & SS_RESIDUAL_UNDER)) {
1217                                 DEBUG2(printk("scsi(%ld:%d:%d:%d) Dropped "
1218                                               "frame(s) detected (%x of %x bytes)..."
1219                                               "retrying command.\n",
1220                                         vha->host_no, cp->device->channel,
1221                                         cp->device->id, cp->device->lun, resid,
1222                                         scsi_bufflen(cp)));
1223
1224                                 cp->result = DID_ERROR << 16;
1225                                 break;
1226                         }
1227
1228                         /* Handle mid-layer underflow */
1229                         if ((unsigned)(scsi_bufflen(cp) - resid) <
1230                             cp->underflow) {
1231                                 qla_printk(KERN_INFO, ha,
1232                                            "scsi(%ld:%d:%d:%d): Mid-layer underflow "
1233                                            "detected (%x of %x bytes)...returning "
1234                                            "error status.\n", vha->host_no,
1235                                            cp->device->channel, cp->device->id,
1236                                            cp->device->lun, resid,
1237                                            scsi_bufflen(cp));
1238
1239                                 cp->result = DID_ERROR << 16;
1240                                 break;
1241                         }
1242
1243                         /* Everybody online, looking good... */
1244                         cp->result = DID_OK << 16;
1245                 }
1246                 break;
1247
1248         case CS_DATA_OVERRUN:
1249                 DEBUG2(printk(KERN_INFO
1250                     "scsi(%ld:%d:%d): OVERRUN status detected 0x%x-0x%x\n",
1251                     vha->host_no, cp->device->id, cp->device->lun, comp_status,
1252                     scsi_status));
1253                 DEBUG2(printk(KERN_INFO
1254                     "CDB: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1255                     cp->cmnd[0], cp->cmnd[1], cp->cmnd[2], cp->cmnd[3],
1256                     cp->cmnd[4], cp->cmnd[5]));
1257                 DEBUG2(printk(KERN_INFO
1258                     "PID=0x%lx req=0x%x xtra=0x%x -- returning DID_ERROR "
1259                     "status!\n",
1260                     cp->serial_number, scsi_bufflen(cp), resid_len));
1261
1262                 cp->result = DID_ERROR << 16;
1263                 break;
1264
1265         case CS_PORT_LOGGED_OUT:
1266         case CS_PORT_CONFIG_CHG:
1267         case CS_PORT_BUSY:
1268         case CS_INCOMPLETE:
1269         case CS_PORT_UNAVAILABLE:
1270                 /*
1271                  * If the port is in Target Down state, return all IOs for this
1272                  * Target with DID_NO_CONNECT ELSE Queue the IOs in the
1273                  * retry_queue.
1274                  */
1275                 DEBUG2(printk("scsi(%ld:%d:%d): status_entry: Port Down "
1276                     "pid=%ld, compl status=0x%x, port state=0x%x\n",
1277                     vha->host_no, cp->device->id, cp->device->lun,
1278                     cp->serial_number, comp_status,
1279                     atomic_read(&fcport->state)));
1280
1281                 /*
1282                  * We are going to have the fc class block the rport
1283                  * while we try to recover so instruct the mid layer
1284                  * to requeue until the class decides how to handle this.
1285                  */
1286                 cp->result = DID_TRANSPORT_DISRUPTED << 16;
1287                 if (atomic_read(&fcport->state) == FCS_ONLINE)
1288                         qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
1289                 break;
1290
1291         case CS_RESET:
1292                 DEBUG2(printk(KERN_INFO
1293                     "scsi(%ld): RESET status detected 0x%x-0x%x.\n",
1294                     vha->host_no, comp_status, scsi_status));
1295
1296                 cp->result = DID_RESET << 16;
1297                 break;
1298
1299         case CS_ABORTED:
1300                 /*
1301                  * hv2.19.12 - DID_ABORT does not retry the request if we
1302                  * aborted this request then abort otherwise it must be a
1303                  * reset.
1304                  */
1305                 DEBUG2(printk(KERN_INFO
1306                     "scsi(%ld): ABORT status detected 0x%x-0x%x.\n",
1307                     vha->host_no, comp_status, scsi_status));
1308
1309                 cp->result = DID_RESET << 16;
1310                 break;
1311
1312         case CS_TIMEOUT:
1313                 /*
1314                  * We are going to have the fc class block the rport
1315                  * while we try to recover so instruct the mid layer
1316                  * to requeue until the class decides how to handle this.
1317                  */
1318                 cp->result = DID_TRANSPORT_DISRUPTED << 16;
1319
1320                 if (IS_FWI2_CAPABLE(ha)) {
1321                         DEBUG2(printk(KERN_INFO
1322                             "scsi(%ld:%d:%d:%d): TIMEOUT status detected "
1323                             "0x%x-0x%x\n", vha->host_no, cp->device->channel,
1324                             cp->device->id, cp->device->lun, comp_status,
1325                             scsi_status));
1326                         break;
1327                 }
1328                 DEBUG2(printk(KERN_INFO
1329                     "scsi(%ld:%d:%d:%d): TIMEOUT status detected 0x%x-0x%x "
1330                     "sflags=%x.\n", vha->host_no, cp->device->channel,
1331                     cp->device->id, cp->device->lun, comp_status, scsi_status,
1332                     le16_to_cpu(sts->status_flags)));
1333
1334                 /* Check to see if logout occurred. */
1335                 if ((le16_to_cpu(sts->status_flags) & SF_LOGOUT_SENT))
1336                         qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
1337                 break;
1338
1339         default:
1340                 DEBUG3(printk("scsi(%ld): Error detected (unknown status) "
1341                     "0x%x-0x%x.\n", vha->host_no, comp_status, scsi_status));
1342                 qla_printk(KERN_INFO, ha,
1343                     "Unknown status detected 0x%x-0x%x.\n",
1344                     comp_status, scsi_status);
1345
1346                 cp->result = DID_ERROR << 16;
1347                 break;
1348         }
1349
1350         /* Place command on done queue. */
1351         if (rsp->status_srb == NULL)
1352                 qla2x00_sp_compl(ha, sp);
1353 }
1354
1355 /**
1356  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
1357  * @ha: SCSI driver HA context
1358  * @pkt: Entry pointer
1359  *
1360  * Extended sense data.
1361  */
1362 static void
1363 qla2x00_status_cont_entry(struct rsp_que *rsp, sts_cont_entry_t *pkt)
1364 {
1365         uint8_t         sense_sz = 0;
1366         struct qla_hw_data *ha = rsp->hw;
1367         srb_t           *sp = rsp->status_srb;
1368         struct scsi_cmnd *cp;
1369
1370         if (sp != NULL && sp->request_sense_length != 0) {
1371                 cp = sp->cmd;
1372                 if (cp == NULL) {
1373                         DEBUG2(printk("%s(): Cmd already returned back to OS "
1374                             "sp=%p.\n", __func__, sp));
1375                         qla_printk(KERN_INFO, ha,
1376                             "cmd is NULL: already returned to OS (sp=%p)\n",
1377                             sp);
1378
1379                         rsp->status_srb = NULL;
1380                         return;
1381                 }
1382
1383                 if (sp->request_sense_length > sizeof(pkt->data)) {
1384                         sense_sz = sizeof(pkt->data);
1385                 } else {
1386                         sense_sz = sp->request_sense_length;
1387                 }
1388
1389                 /* Move sense data. */
1390                 if (IS_FWI2_CAPABLE(ha))
1391                         host_to_fcp_swap(pkt->data, sizeof(pkt->data));
1392                 memcpy(sp->request_sense_ptr, pkt->data, sense_sz);
1393                 DEBUG5(qla2x00_dump_buffer(sp->request_sense_ptr, sense_sz));
1394
1395                 sp->request_sense_ptr += sense_sz;
1396                 sp->request_sense_length -= sense_sz;
1397
1398                 /* Place command on done queue. */
1399                 if (sp->request_sense_length == 0) {
1400                         rsp->status_srb = NULL;
1401                         qla2x00_sp_compl(ha, sp);
1402                 }
1403         }
1404 }
1405
1406 /**
1407  * qla2x00_error_entry() - Process an error entry.
1408  * @ha: SCSI driver HA context
1409  * @pkt: Entry pointer
1410  */
1411 static void
1412 qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
1413 {
1414         srb_t *sp;
1415         struct qla_hw_data *ha = vha->hw;
1416         uint32_t handle = LSW(pkt->handle);
1417         uint16_t que = MSW(pkt->handle);
1418         struct req_que *req = ha->req_q_map[que];
1419 #if defined(QL_DEBUG_LEVEL_2)
1420         if (pkt->entry_status & RF_INV_E_ORDER)
1421                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Order\n", __func__);
1422         else if (pkt->entry_status & RF_INV_E_COUNT)
1423                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Count\n", __func__);
1424         else if (pkt->entry_status & RF_INV_E_PARAM)
1425                 qla_printk(KERN_ERR, ha,
1426                     "%s: Invalid Entry Parameter\n", __func__);
1427         else if (pkt->entry_status & RF_INV_E_TYPE)
1428                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Type\n", __func__);
1429         else if (pkt->entry_status & RF_BUSY)
1430                 qla_printk(KERN_ERR, ha, "%s: Busy\n", __func__);
1431         else
1432                 qla_printk(KERN_ERR, ha, "%s: UNKNOWN flag error\n", __func__);
1433 #endif
1434
1435         /* Validate handle. */
1436         if (handle < MAX_OUTSTANDING_COMMANDS)
1437                 sp = req->outstanding_cmds[handle];
1438         else
1439                 sp = NULL;
1440
1441         if (sp) {
1442                 /* Free outstanding command slot. */
1443                 req->outstanding_cmds[handle] = NULL;
1444
1445                 /* Bad payload or header */
1446                 if (pkt->entry_status &
1447                     (RF_INV_E_ORDER | RF_INV_E_COUNT |
1448                      RF_INV_E_PARAM | RF_INV_E_TYPE)) {
1449                         sp->cmd->result = DID_ERROR << 16;
1450                 } else if (pkt->entry_status & RF_BUSY) {
1451                         sp->cmd->result = DID_BUS_BUSY << 16;
1452                 } else {
1453                         sp->cmd->result = DID_ERROR << 16;
1454                 }
1455                 qla2x00_sp_compl(ha, sp);
1456
1457         } else if (pkt->entry_type == COMMAND_A64_TYPE || pkt->entry_type ==
1458             COMMAND_TYPE || pkt->entry_type == COMMAND_TYPE_7) {
1459                 DEBUG2(printk("scsi(%ld): Error entry - invalid handle\n",
1460                     vha->host_no));
1461                 qla_printk(KERN_WARNING, ha,
1462                     "Error entry - invalid handle\n");
1463
1464                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1465                 qla2xxx_wake_dpc(vha);
1466         }
1467 }
1468
1469 /**
1470  * qla24xx_mbx_completion() - Process mailbox command completions.
1471  * @ha: SCSI driver HA context
1472  * @mb0: Mailbox0 register
1473  */
1474 static void
1475 qla24xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
1476 {
1477         uint16_t        cnt;
1478         uint16_t __iomem *wptr;
1479         struct qla_hw_data *ha = vha->hw;
1480         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1481
1482         /* Load return mailbox registers. */
1483         ha->flags.mbox_int = 1;
1484         ha->mailbox_out[0] = mb0;
1485         wptr = (uint16_t __iomem *)&reg->mailbox1;
1486
1487         for (cnt = 1; cnt < ha->mbx_count; cnt++) {
1488                 ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
1489                 wptr++;
1490         }
1491
1492         if (ha->mcp) {
1493                 DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
1494                     __func__, vha->host_no, ha->mcp->mb[0]));
1495         } else {
1496                 DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
1497                     __func__, vha->host_no));
1498         }
1499 }
1500
1501 /**
1502  * qla24xx_process_response_queue() - Process response queue entries.
1503  * @ha: SCSI driver HA context
1504  */
1505 void qla24xx_process_response_queue(struct scsi_qla_host *vha,
1506         struct rsp_que *rsp)
1507 {
1508         struct sts_entry_24xx *pkt;
1509
1510         if (!vha->flags.online)
1511                 return;
1512
1513         while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
1514                 pkt = (struct sts_entry_24xx *)rsp->ring_ptr;
1515
1516                 rsp->ring_index++;
1517                 if (rsp->ring_index == rsp->length) {
1518                         rsp->ring_index = 0;
1519                         rsp->ring_ptr = rsp->ring;
1520                 } else {
1521                         rsp->ring_ptr++;
1522                 }
1523
1524                 if (pkt->entry_status != 0) {
1525                         DEBUG3(printk(KERN_INFO
1526                             "scsi(%ld): Process error entry.\n", vha->host_no));
1527
1528                         qla2x00_error_entry(vha, rsp, (sts_entry_t *) pkt);
1529                         ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1530                         wmb();
1531                         continue;
1532                 }
1533
1534                 switch (pkt->entry_type) {
1535                 case STATUS_TYPE:
1536                         qla2x00_status_entry(vha, rsp, pkt);
1537                         break;
1538                 case STATUS_CONT_TYPE:
1539                         qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
1540                         break;
1541                 case VP_RPT_ID_IOCB_TYPE:
1542                         qla24xx_report_id_acquisition(vha,
1543                             (struct vp_rpt_id_entry_24xx *)pkt);
1544                         break;
1545                 default:
1546                         /* Type Not Supported. */
1547                         DEBUG4(printk(KERN_WARNING
1548                             "scsi(%ld): Received unknown response pkt type %x "
1549                             "entry status=%x.\n",
1550                             vha->host_no, pkt->entry_type, pkt->entry_status));
1551                         break;
1552                 }
1553                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1554                 wmb();
1555         }
1556
1557         /* Adjust ring index */
1558         WRT_REG_DWORD(rsp->rsp_q_out, rsp->ring_index);
1559 }
1560
1561 static void
1562 qla2xxx_check_risc_status(scsi_qla_host_t *vha)
1563 {
1564         int rval;
1565         uint32_t cnt;
1566         struct qla_hw_data *ha = vha->hw;
1567         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1568
1569         if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha))
1570                 return;
1571
1572         rval = QLA_SUCCESS;
1573         WRT_REG_DWORD(&reg->iobase_addr, 0x7C00);
1574         RD_REG_DWORD(&reg->iobase_addr);
1575         WRT_REG_DWORD(&reg->iobase_window, 0x0001);
1576         for (cnt = 10000; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
1577             rval == QLA_SUCCESS; cnt--) {
1578                 if (cnt) {
1579                         WRT_REG_DWORD(&reg->iobase_window, 0x0001);
1580                         udelay(10);
1581                 } else
1582                         rval = QLA_FUNCTION_TIMEOUT;
1583         }
1584         if (rval == QLA_SUCCESS)
1585                 goto next_test;
1586
1587         WRT_REG_DWORD(&reg->iobase_window, 0x0003);
1588         for (cnt = 100; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
1589             rval == QLA_SUCCESS; cnt--) {
1590                 if (cnt) {
1591                         WRT_REG_DWORD(&reg->iobase_window, 0x0003);
1592                         udelay(10);
1593                 } else
1594                         rval = QLA_FUNCTION_TIMEOUT;
1595         }
1596         if (rval != QLA_SUCCESS)
1597                 goto done;
1598
1599 next_test:
1600         if (RD_REG_DWORD(&reg->iobase_c8) & BIT_3)
1601                 qla_printk(KERN_INFO, ha, "Additional code -- 0x55AA.\n");
1602
1603 done:
1604         WRT_REG_DWORD(&reg->iobase_window, 0x0000);
1605         RD_REG_DWORD(&reg->iobase_window);
1606 }
1607
1608 /**
1609  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
1610  * @irq:
1611  * @dev_id: SCSI driver HA context
1612  *
1613  * Called by system whenever the host adapter generates an interrupt.
1614  *
1615  * Returns handled flag.
1616  */
1617 irqreturn_t
1618 qla24xx_intr_handler(int irq, void *dev_id)
1619 {
1620         scsi_qla_host_t *vha;
1621         struct qla_hw_data *ha;
1622         struct device_reg_24xx __iomem *reg;
1623         int             status;
1624         unsigned long   iter;
1625         uint32_t        stat;
1626         uint32_t        hccr;
1627         uint16_t        mb[4];
1628         struct rsp_que *rsp;
1629
1630         rsp = (struct rsp_que *) dev_id;
1631         if (!rsp) {
1632                 printk(KERN_INFO
1633                     "%s(): NULL response queue pointer\n", __func__);
1634                 return IRQ_NONE;
1635         }
1636
1637         ha = rsp->hw;
1638         reg = &ha->iobase->isp24;
1639         status = 0;
1640
1641         spin_lock(&ha->hardware_lock);
1642         vha = pci_get_drvdata(ha->pdev);
1643         for (iter = 50; iter--; ) {
1644                 stat = RD_REG_DWORD(&reg->host_status);
1645                 if (stat & HSRX_RISC_PAUSED) {
1646                         if (pci_channel_offline(ha->pdev))
1647                                 break;
1648
1649                         hccr = RD_REG_DWORD(&reg->hccr);
1650
1651                         qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
1652                             "Dumping firmware!\n", hccr);
1653
1654                         qla2xxx_check_risc_status(vha);
1655
1656                         ha->isp_ops->fw_dump(vha, 1);
1657                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1658                         break;
1659                 } else if ((stat & HSRX_RISC_INT) == 0)
1660                         break;
1661
1662                 switch (stat & 0xff) {
1663                 case 0x1:
1664                 case 0x2:
1665                 case 0x10:
1666                 case 0x11:
1667                         qla24xx_mbx_completion(vha, MSW(stat));
1668                         status |= MBX_INTERRUPT;
1669
1670                         break;
1671                 case 0x12:
1672                         mb[0] = MSW(stat);
1673                         mb[1] = RD_REG_WORD(&reg->mailbox1);
1674                         mb[2] = RD_REG_WORD(&reg->mailbox2);
1675                         mb[3] = RD_REG_WORD(&reg->mailbox3);
1676                         qla2x00_async_event(vha, rsp, mb);
1677                         break;
1678                 case 0x13:
1679                 case 0x14:
1680                         qla24xx_process_response_queue(vha, rsp);
1681                         break;
1682                 default:
1683                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
1684                             "(%d).\n",
1685                             vha->host_no, stat & 0xff));
1686                         break;
1687                 }
1688                 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1689                 RD_REG_DWORD_RELAXED(&reg->hccr);
1690         }
1691         spin_unlock(&ha->hardware_lock);
1692
1693         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
1694             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
1695                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
1696                 complete(&ha->mbx_intr_comp);
1697         }
1698
1699         return IRQ_HANDLED;
1700 }
1701
1702 static irqreturn_t
1703 qla24xx_msix_rsp_q(int irq, void *dev_id)
1704 {
1705         struct qla_hw_data *ha;
1706         struct rsp_que *rsp;
1707         struct device_reg_24xx __iomem *reg;
1708         struct scsi_qla_host *vha;
1709
1710         rsp = (struct rsp_que *) dev_id;
1711         if (!rsp) {
1712                 printk(KERN_INFO
1713                 "%s(): NULL response queue pointer\n", __func__);
1714                 return IRQ_NONE;
1715         }
1716         ha = rsp->hw;
1717         reg = &ha->iobase->isp24;
1718
1719         spin_lock_irq(&ha->hardware_lock);
1720
1721         vha = qla25xx_get_host(rsp);
1722         qla24xx_process_response_queue(vha, rsp);
1723         WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1724
1725         spin_unlock_irq(&ha->hardware_lock);
1726
1727         return IRQ_HANDLED;
1728 }
1729
1730 static irqreturn_t
1731 qla25xx_msix_rsp_q(int irq, void *dev_id)
1732 {
1733         struct qla_hw_data *ha;
1734         struct rsp_que *rsp;
1735
1736         rsp = (struct rsp_que *) dev_id;
1737         if (!rsp) {
1738                 printk(KERN_INFO
1739                         "%s(): NULL response queue pointer\n", __func__);
1740                 return IRQ_NONE;
1741         }
1742         ha = rsp->hw;
1743
1744         queue_work_on((int) (rsp->id - 1), ha->wq, &rsp->q_work);
1745
1746         return IRQ_HANDLED;
1747 }
1748
1749 static irqreturn_t
1750 qla24xx_msix_default(int irq, void *dev_id)
1751 {
1752         scsi_qla_host_t *vha;
1753         struct qla_hw_data *ha;
1754         struct rsp_que *rsp;
1755         struct device_reg_24xx __iomem *reg;
1756         int             status;
1757         uint32_t        stat;
1758         uint32_t        hccr;
1759         uint16_t        mb[4];
1760
1761         rsp = (struct rsp_que *) dev_id;
1762         if (!rsp) {
1763                 DEBUG(printk(
1764                 "%s(): NULL response queue pointer\n", __func__));
1765                 return IRQ_NONE;
1766         }
1767         ha = rsp->hw;
1768         reg = &ha->iobase->isp24;
1769         status = 0;
1770
1771         spin_lock_irq(&ha->hardware_lock);
1772         vha = pci_get_drvdata(ha->pdev);
1773         do {
1774                 stat = RD_REG_DWORD(&reg->host_status);
1775                 if (stat & HSRX_RISC_PAUSED) {
1776                         if (pci_channel_offline(ha->pdev))
1777                                 break;
1778
1779                         hccr = RD_REG_DWORD(&reg->hccr);
1780
1781                         qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
1782                             "Dumping firmware!\n", hccr);
1783
1784                         qla2xxx_check_risc_status(vha);
1785
1786                         ha->isp_ops->fw_dump(vha, 1);
1787                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1788                         break;
1789                 } else if ((stat & HSRX_RISC_INT) == 0)
1790                         break;
1791
1792                 switch (stat & 0xff) {
1793                 case 0x1:
1794                 case 0x2:
1795                 case 0x10:
1796                 case 0x11:
1797                         qla24xx_mbx_completion(vha, MSW(stat));
1798                         status |= MBX_INTERRUPT;
1799
1800                         break;
1801                 case 0x12:
1802                         mb[0] = MSW(stat);
1803                         mb[1] = RD_REG_WORD(&reg->mailbox1);
1804                         mb[2] = RD_REG_WORD(&reg->mailbox2);
1805                         mb[3] = RD_REG_WORD(&reg->mailbox3);
1806                         qla2x00_async_event(vha, rsp, mb);
1807                         break;
1808                 case 0x13:
1809                 case 0x14:
1810                         qla24xx_process_response_queue(vha, rsp);
1811                         break;
1812                 default:
1813                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
1814                             "(%d).\n",
1815                             vha->host_no, stat & 0xff));
1816                         break;
1817                 }
1818                 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1819         } while (0);
1820         spin_unlock_irq(&ha->hardware_lock);
1821
1822         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
1823             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
1824                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
1825                 complete(&ha->mbx_intr_comp);
1826         }
1827
1828         return IRQ_HANDLED;
1829 }
1830
1831 /* Interrupt handling helpers. */
1832
1833 struct qla_init_msix_entry {
1834         const char *name;
1835         irq_handler_t handler;
1836 };
1837
1838 static struct qla_init_msix_entry msix_entries[3] = {
1839         { "qla2xxx (default)", qla24xx_msix_default },
1840         { "qla2xxx (rsp_q)", qla24xx_msix_rsp_q },
1841         { "qla2xxx (multiq)", qla25xx_msix_rsp_q },
1842 };
1843
1844 static void
1845 qla24xx_disable_msix(struct qla_hw_data *ha)
1846 {
1847         int i;
1848         struct qla_msix_entry *qentry;
1849
1850         for (i = 0; i < ha->msix_count; i++) {
1851                 qentry = &ha->msix_entries[i];
1852                 if (qentry->have_irq)
1853                         free_irq(qentry->vector, qentry->rsp);
1854         }
1855         pci_disable_msix(ha->pdev);
1856         kfree(ha->msix_entries);
1857         ha->msix_entries = NULL;
1858         ha->flags.msix_enabled = 0;
1859 }
1860
1861 static int
1862 qla24xx_enable_msix(struct qla_hw_data *ha, struct rsp_que *rsp)
1863 {
1864 #define MIN_MSIX_COUNT  2
1865         int i, ret;
1866         struct msix_entry *entries;
1867         struct qla_msix_entry *qentry;
1868
1869         entries = kzalloc(sizeof(struct msix_entry) * ha->msix_count,
1870                                         GFP_KERNEL);
1871         if (!entries)
1872                 return -ENOMEM;
1873
1874         for (i = 0; i < ha->msix_count; i++)
1875                 entries[i].entry = i;
1876
1877         ret = pci_enable_msix(ha->pdev, entries, ha->msix_count);
1878         if (ret) {
1879                 if (ret < MIN_MSIX_COUNT)
1880                         goto msix_failed;
1881
1882                 qla_printk(KERN_WARNING, ha,
1883                         "MSI-X: Failed to enable support -- %d/%d\n"
1884                         " Retry with %d vectors\n", ha->msix_count, ret, ret);
1885                 ha->msix_count = ret;
1886                 ret = pci_enable_msix(ha->pdev, entries, ha->msix_count);
1887                 if (ret) {
1888 msix_failed:
1889                         qla_printk(KERN_WARNING, ha, "MSI-X: Failed to enable"
1890                                 " support, giving up -- %d/%d\n",
1891                                 ha->msix_count, ret);
1892                         goto msix_out;
1893                 }
1894                 ha->max_rsp_queues = ha->msix_count - 1;
1895         }
1896         ha->msix_entries = kzalloc(sizeof(struct qla_msix_entry) *
1897                                 ha->msix_count, GFP_KERNEL);
1898         if (!ha->msix_entries) {
1899                 ret = -ENOMEM;
1900                 goto msix_out;
1901         }
1902         ha->flags.msix_enabled = 1;
1903
1904         for (i = 0; i < ha->msix_count; i++) {
1905                 qentry = &ha->msix_entries[i];
1906                 qentry->vector = entries[i].vector;
1907                 qentry->entry = entries[i].entry;
1908                 qentry->have_irq = 0;
1909                 qentry->rsp = NULL;
1910         }
1911
1912         /* Enable MSI-X vectors for the base queue */
1913         for (i = 0; i < 2; i++) {
1914                 qentry = &ha->msix_entries[i];
1915                 ret = request_irq(qentry->vector, msix_entries[i].handler,
1916                                         0, msix_entries[i].name, rsp);
1917                 if (ret) {
1918                         qla_printk(KERN_WARNING, ha,
1919                         "MSI-X: Unable to register handler -- %x/%d.\n",
1920                         qentry->vector, ret);
1921                         qla24xx_disable_msix(ha);
1922                         ha->mqenable = 0;
1923                         goto msix_out;
1924                 }
1925                 qentry->have_irq = 1;
1926                 qentry->rsp = rsp;
1927                 rsp->msix = qentry;
1928         }
1929
1930         /* Enable MSI-X vector for response queue update for queue 0 */
1931         if (ha->mqiobase &&  (ha->max_rsp_queues > 1 || ha->max_req_queues > 1))
1932                 ha->mqenable = 1;
1933
1934 msix_out:
1935         kfree(entries);
1936         return ret;
1937 }
1938
1939 int
1940 qla2x00_request_irqs(struct qla_hw_data *ha, struct rsp_que *rsp)
1941 {
1942         int ret;
1943         device_reg_t __iomem *reg = ha->iobase;
1944
1945         /* If possible, enable MSI-X. */
1946         if (!IS_QLA2432(ha) && !IS_QLA2532(ha) &&
1947             !IS_QLA8432(ha) && !IS_QLA8001(ha))
1948                 goto skip_msix;
1949
1950         if (IS_QLA2432(ha) && (ha->pdev->revision < QLA_MSIX_CHIP_REV_24XX ||
1951                 !QLA_MSIX_FW_MODE_1(ha->fw_attributes))) {
1952                 DEBUG2(qla_printk(KERN_WARNING, ha,
1953                 "MSI-X: Unsupported ISP2432 (0x%X, 0x%X).\n",
1954                         ha->pdev->revision, ha->fw_attributes));
1955
1956                 goto skip_msix;
1957         }
1958
1959         if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_HP &&
1960             (ha->pdev->subsystem_device == 0x7040 ||
1961                 ha->pdev->subsystem_device == 0x7041 ||
1962                 ha->pdev->subsystem_device == 0x1705)) {
1963                 DEBUG2(qla_printk(KERN_WARNING, ha,
1964                     "MSI-X: Unsupported ISP2432 SSVID/SSDID (0x%X, 0x%X).\n",
1965                     ha->pdev->subsystem_vendor,
1966                     ha->pdev->subsystem_device));
1967
1968                 goto skip_msi;
1969         }
1970
1971         ret = qla24xx_enable_msix(ha, rsp);
1972         if (!ret) {
1973                 DEBUG2(qla_printk(KERN_INFO, ha,
1974                     "MSI-X: Enabled (0x%X, 0x%X).\n", ha->chip_revision,
1975                     ha->fw_attributes));
1976                 goto clear_risc_ints;
1977         }
1978         qla_printk(KERN_WARNING, ha,
1979             "MSI-X: Falling back-to INTa mode -- %d.\n", ret);
1980 skip_msix:
1981
1982         if (!IS_QLA24XX(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha) &&
1983             !IS_QLA8001(ha))
1984                 goto skip_msi;
1985
1986         ret = pci_enable_msi(ha->pdev);
1987         if (!ret) {
1988                 DEBUG2(qla_printk(KERN_INFO, ha, "MSI: Enabled.\n"));
1989                 ha->flags.msi_enabled = 1;
1990         }
1991 skip_msi:
1992
1993         ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
1994             IRQF_SHARED, QLA2XXX_DRIVER_NAME, rsp);
1995         if (ret) {
1996                 qla_printk(KERN_WARNING, ha,
1997                     "Failed to reserve interrupt %d already in use.\n",
1998                     ha->pdev->irq);
1999                 goto fail;
2000         }
2001         ha->flags.inta_enabled = 1;
2002 clear_risc_ints:
2003
2004         /*
2005          * FIXME: Noted that 8014s were being dropped during NK testing.
2006          * Timing deltas during MSI-X/INTa transitions?
2007          */
2008         if (IS_QLA81XX(ha))
2009                 goto fail;
2010         spin_lock_irq(&ha->hardware_lock);
2011         if (IS_FWI2_CAPABLE(ha)) {
2012                 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
2013                 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
2014         } else {
2015                 WRT_REG_WORD(&reg->isp.semaphore, 0);
2016                 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
2017                 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
2018         }
2019         spin_unlock_irq(&ha->hardware_lock);
2020
2021 fail:
2022         return ret;
2023 }
2024
2025 void
2026 qla2x00_free_irqs(scsi_qla_host_t *vha)
2027 {
2028         struct qla_hw_data *ha = vha->hw;
2029         struct rsp_que *rsp = ha->rsp_q_map[0];
2030
2031         if (ha->flags.msix_enabled)
2032                 qla24xx_disable_msix(ha);
2033         else if (ha->flags.inta_enabled) {
2034                 free_irq(ha->pdev->irq, rsp);
2035                 pci_disable_msi(ha->pdev);
2036         }
2037 }
2038
2039
2040 int qla25xx_request_irq(struct rsp_que *rsp)
2041 {
2042         struct qla_hw_data *ha = rsp->hw;
2043         struct qla_init_msix_entry *intr = &msix_entries[2];
2044         struct qla_msix_entry *msix = rsp->msix;
2045         int ret;
2046
2047         ret = request_irq(msix->vector, intr->handler, 0, intr->name, rsp);
2048         if (ret) {
2049                 qla_printk(KERN_WARNING, ha,
2050                         "MSI-X: Unable to register handler -- %x/%d.\n",
2051                         msix->vector, ret);
2052                 return ret;
2053         }
2054         msix->have_irq = 1;
2055         msix->rsp = rsp;
2056         return ret;
2057 }
2058
2059 struct scsi_qla_host *
2060 qla25xx_get_host(struct rsp_que *rsp)
2061 {
2062         srb_t *sp;
2063         struct qla_hw_data *ha = rsp->hw;
2064         struct scsi_qla_host *vha = NULL;
2065         struct sts_entry_24xx *pkt;
2066         struct req_que *req;
2067         uint16_t que;
2068         uint32_t handle;
2069
2070         pkt = (struct sts_entry_24xx *) rsp->ring_ptr;
2071         que = MSW(pkt->handle);
2072         handle = (uint32_t) LSW(pkt->handle);
2073         req = ha->req_q_map[que];
2074         if (handle < MAX_OUTSTANDING_COMMANDS) {
2075                 sp = req->outstanding_cmds[handle];
2076                 if (sp)
2077                         return  sp->fcport->vha;
2078                 else
2079                         goto base_que;
2080         }
2081 base_que:
2082         vha = pci_get_drvdata(ha->pdev);
2083         return vha;
2084 }