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