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