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