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