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