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