Printk cleanups and minor fixes
[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(KERN_INFO "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(KERN_INFO "scsi(%ld): ha state %d "
585                                 "init_done %d oper_mode %d topo %d\n",
586                                 ha->host_no, atomic_read(&ha->loop_state),
587                                 ha->flags.init_done, ha->operating_mode,
588                                 ha->current_topology));
589                         if (qla_target.tgt_async_event)
590                                 qla_target.tgt_async_event(mb[0], ha, mb);
591 #endif
592                         break;
593                 }
594
595                 DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE.\n",
596                     ha->host_no));
597                 DEBUG(printk(KERN_INFO
598                     "scsi(%ld): Port database changed %04x %04x %04x.\n",
599                     ha->host_no, mb[1], mb[2], mb[3]));
600
601                 /*
602                  * Mark all devices as missing so we will login again.
603                  */
604                 atomic_set(&ha->loop_state, LOOP_UP);
605
606                 qla2x00_mark_all_devices_lost(ha, 1);
607
608                 ha->flags.rscn_queue_overflow = 1;
609
610                 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
611                 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
612 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
613                 if (qla_target.tgt_async_event)
614                         qla_target.tgt_async_event(mb[0], ha, mb);
615 #endif
616                 break;
617
618         case MBA_RSCN_UPDATE:           /* State Change Registration */
619                 /* Check if the Vport has issued a SCR */
620                 if (ha->parent && test_bit(VP_SCR_NEEDED, &ha->vp_flags))
621                         break;
622                 /* Only handle SCNs for our Vport index. */
623                 if (ha->parent && ha->vp_idx != (mb[3] & 0xff))
624                         break;
625
626                 DEBUG2(printk("scsi(%ld): Asynchronous RSCR UPDATE.\n",
627                     ha->host_no));
628                 DEBUG(printk(KERN_INFO
629                     "scsi(%ld): RSCN database changed -- %04x %04x %04x.\n",
630                     ha->host_no, mb[1], mb[2], mb[3]));
631
632                 rscn_entry = (mb[1] << 16) | mb[2];
633                 host_pid = (ha->d_id.b.domain << 16) | (ha->d_id.b.area << 8) |
634                     ha->d_id.b.al_pa;
635                 if (rscn_entry == host_pid) {
636                         DEBUG(printk(KERN_INFO
637                             "scsi(%ld): Ignoring RSCN update to local host "
638                             "port ID (%06x)\n",
639                             ha->host_no, host_pid));
640                         break;
641                 }
642
643                 rscn_queue_index = ha->rscn_in_ptr + 1;
644                 if (rscn_queue_index == MAX_RSCN_COUNT)
645                         rscn_queue_index = 0;
646                 if (rscn_queue_index != ha->rscn_out_ptr) {
647                         ha->rscn_queue[ha->rscn_in_ptr] = rscn_entry;
648                         ha->rscn_in_ptr = rscn_queue_index;
649                 } else {
650                         ha->flags.rscn_queue_overflow = 1;
651                 }
652
653                 atomic_set(&ha->loop_state, LOOP_UPDATE);
654                 atomic_set(&ha->loop_down_timer, 0);
655                 ha->flags.management_server_logged_in = 0;
656
657                 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
658                 set_bit(RSCN_UPDATE, &ha->dpc_flags);
659                 qla2x00_post_aen_work(ha, FCH_EVT_RSCN, rscn_entry);
660                 break;
661
662         /* case MBA_RIO_RESPONSE: */
663         case MBA_ZIO_RESPONSE:
664                 DEBUG2(printk("scsi(%ld): [R|Z]IO update completion.\n",
665                     ha->host_no));
666                 DEBUG(printk(KERN_INFO
667                     "scsi(%ld): [R|Z]IO update completion.\n",
668                     ha->host_no));
669
670                 if (IS_FWI2_CAPABLE(ha))
671                         qla24xx_process_response_queue(ha);
672                 else
673                         qla2x00_process_response_queue(ha);
674                 break;
675
676         case MBA_DISCARD_RND_FRAME:
677                 DEBUG2(printk("scsi(%ld): Discard RND Frame -- %04x %04x "
678                     "%04x.\n", ha->host_no, mb[1], mb[2], mb[3]));
679                 break;
680
681         case MBA_TRACE_NOTIFICATION:
682                 DEBUG2(printk("scsi(%ld): Trace Notification -- %04x %04x.\n",
683                 ha->host_no, mb[1], mb[2]));
684                 break;
685
686         case MBA_ISP84XX_ALERT:
687                 DEBUG2(printk("scsi(%ld): ISP84XX Alert Notification -- "
688                     "%04x %04x %04x\n", ha->host_no, mb[1], mb[2], mb[3]));
689
690                 spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
691                 switch (mb[1]) {
692                 case A84_PANIC_RECOVERY:
693                         qla_printk(KERN_INFO, ha, "Alert 84XX: panic recovery "
694                             "%04x %04x\n", mb[2], mb[3]);
695                         break;
696                 case A84_OP_LOGIN_COMPLETE:
697                         ha->cs84xx->op_fw_version = mb[3] << 16 | mb[2];
698                         DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
699                             "firmware version %x\n", ha->cs84xx->op_fw_version));
700                         break;
701                 case A84_DIAG_LOGIN_COMPLETE:
702                         ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
703                         DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
704                             "diagnostic firmware version %x\n",
705                             ha->cs84xx->diag_fw_version));
706                         break;
707                 case A84_GOLD_LOGIN_COMPLETE:
708                         ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
709                         ha->cs84xx->fw_update = 1;
710                         DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX: gold "
711                             "firmware version %x\n",
712                             ha->cs84xx->gold_fw_version));
713                         break;
714                 default:
715                         qla_printk(KERN_ERR, ha,
716                             "Alert 84xx: Invalid Alert %04x %04x %04x\n",
717                             mb[1], mb[2], mb[3]);
718                 }
719                 spin_unlock_irqrestore(&ha->cs84xx->access_lock, flags);
720                 break;
721
722         case MBA_LOOP_INIT_ERR:
723                 printk(KERN_INFO "scsi(%ld): Loop init error received -- "
724                     "%04x %04x %04x.\n", ha->host_no, mb[1], mb[2],
725                     mb[3]);
726                 break;
727
728         default:
729                 printk(KERN_INFO "scsi(%ld): Unhandled async event %d "
730                     "received -- %04x %04x %04x.\n", ha->host_no,
731                     mb[0], mb[1], mb[2], mb[3]);
732                 break;
733         }
734
735         switch (mb[0]) {
736         case MBA_POINT_TO_POINT:        /* Point-to-Point */
737         case MBA_CHG_IN_CONNECTION:     /* Change in connection mode */
738                 if (IS_QLA2100(ha))
739                         break;
740                 /* else go through */
741         case MBA_RESET:                 /* Reset */
742         case MBA_SYSTEM_ERR:            /* System Error */
743         case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
744         case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
745         case MBA_ATIO_TRANSFER_ERR:     /* ATIO Queue Transfer Error */
746         case MBA_LIP_OCCURRED:          /* Loop Initialization Procedure */
747         case MBA_LOOP_UP:               /* Loop Up Event */
748         case MBA_LOOP_DOWN:             /* Loop Down Event */
749         case MBA_LIP_RESET:             /* LIP reset occurred */
750 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
751                 if (qla_target.tgt_async_event)
752                         qla_target.tgt_async_event(mb[0], ha, mb);
753 #endif
754                 break;
755         }
756
757         if (!ha->parent && ha->num_vhosts)
758                 qla2x00_alert_all_vps(ha, mb);
759 }
760
761 static void
762 qla2x00_adjust_sdev_qdepth_up(struct scsi_device *sdev, void *data)
763 {
764         fc_port_t *fcport = data;
765
766         if (fcport->ha->max_q_depth <= sdev->queue_depth)
767                 return;
768
769         if (sdev->ordered_tags)
770                 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG,
771                     sdev->queue_depth + 1);
772         else
773                 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG,
774                     sdev->queue_depth + 1);
775
776         fcport->last_ramp_up = jiffies;
777
778         DEBUG2(qla_printk(KERN_INFO, fcport->ha,
779             "scsi(%ld:%d:%d:%d): Queue depth adjusted-up to %d.\n",
780             fcport->ha->host_no, sdev->channel, sdev->id, sdev->lun,
781             sdev->queue_depth));
782 }
783
784 static void
785 qla2x00_adjust_sdev_qdepth_down(struct scsi_device *sdev, void *data)
786 {
787         fc_port_t *fcport = data;
788
789         if (!scsi_track_queue_full(sdev, sdev->queue_depth - 1))
790                 return;
791
792         DEBUG2(qla_printk(KERN_INFO, fcport->ha,
793             "scsi(%ld:%d:%d:%d): Queue depth adjusted-down to %d.\n",
794             fcport->ha->host_no, sdev->channel, sdev->id, sdev->lun,
795             sdev->queue_depth));
796 }
797
798 static inline void
799 qla2x00_ramp_up_queue_depth(scsi_qla_host_t *ha, srb_t *sp)
800 {
801         fc_port_t *fcport;
802         struct scsi_device *sdev;
803
804         sdev = sp->cmd->device;
805         if (sdev->queue_depth >= ha->max_q_depth)
806                 return;
807
808         fcport = sp->fcport;
809         if (time_before(jiffies,
810             fcport->last_ramp_up + ql2xqfullrampup * HZ))
811                 return;
812         if (time_before(jiffies,
813             fcport->last_queue_full + ql2xqfullrampup * HZ))
814                 return;
815
816         starget_for_each_device(sdev->sdev_target, fcport,
817             qla2x00_adjust_sdev_qdepth_up);
818 }
819
820 /**
821  * qla2x00_process_completed_request() - Process a Fast Post response.
822  * @ha: SCSI driver HA context
823  * @index: SRB index
824  */
825 static void
826 qla2x00_process_completed_request(struct scsi_qla_host *ha, uint32_t index)
827 {
828         srb_t *sp;
829
830 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
831         if (HANDLE_IS_CTIO_COMP(index)) {
832                 if (qla_target.tgt2x_ctio_completion)
833                         qla_target.tgt2x_ctio_completion(ha, index);
834                 return;
835         }
836 #endif
837
838         /* Validate handle. */
839         if (index >= MAX_OUTSTANDING_COMMANDS) {
840                 DEBUG2(printk("scsi(%ld): Invalid SCSI completion handle %d.\n",
841                     ha->host_no, index));
842                 qla_printk(KERN_WARNING, ha,
843                     "Invalid SCSI completion handle %d.\n", index);
844                 DEBUG2(dump_stack());
845                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
846                 return;
847         }
848
849         sp = ha->outstanding_cmds[index];
850         if (sp) {
851                 /* Free outstanding command slot. */
852                 ha->outstanding_cmds[index] = NULL;
853
854                 CMD_COMPL_STATUS(sp->cmd) = 0L;
855                 CMD_SCSI_STATUS(sp->cmd) = 0L;
856
857                 /* Save ISP completion status */
858                 sp->cmd->result = DID_OK << 16;
859
860                 qla2x00_ramp_up_queue_depth(ha, sp);
861                 qla2x00_sp_compl(ha, sp);
862         } else {
863                 DEBUG2(printk("scsi(%ld): Invalid ISP SCSI completion handle\n",
864                     ha->host_no));
865                 qla_printk(KERN_WARNING, ha,
866                     "Invalid ISP SCSI completion handle\n");
867                 DEBUG2(dump_stack());
868
869                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
870         }
871 }
872
873 /**
874  * qla2x00_process_response_queue() - Process response queue entries.
875  * @ha: SCSI driver HA context
876  */
877 void
878 qla2x00_process_response_queue(struct scsi_qla_host *ha)
879 {
880         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
881         sts_entry_t     *pkt;
882         uint16_t        handle_cnt;
883         uint16_t        cnt;
884
885         if (!ha->flags.online)
886                 return;
887
888         while (ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
889                 pkt = (sts_entry_t *)ha->response_ring_ptr;
890
891                 DEBUG5(printk(KERN_INFO "%s(): IOCB data:\n", __func__));
892                 DEBUG5(qla2x00_dump_buffer((uint8_t *)pkt, RESPONSE_ENTRY_SIZE));
893
894                 ha->rsp_ring_index++;
895                 if (ha->rsp_ring_index == ha->response_q_length) {
896                         ha->rsp_ring_index = 0;
897                         ha->response_ring_ptr = ha->response_ring;
898                 } else {
899                         ha->response_ring_ptr++;
900                 }
901
902                 if (pkt->entry_status != 0) {
903                         DEBUG3(printk(KERN_INFO
904                             "scsi(%ld): Process error entry.\n", ha->host_no));
905
906                         qla2x00_error_entry(ha, pkt);
907 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
908                         switch (pkt->entry_type) {
909                         case ACCEPT_TGT_IO_TYPE:
910                         case CONTINUE_TGT_IO_TYPE:
911                         case CTIO_A64_TYPE:
912                         case IMMED_NOTIFY_TYPE:
913                         case NOTIFY_ACK_TYPE:
914                         case ENABLE_LUN_TYPE:
915                         case MODIFY_LUN_TYPE:
916                                 break;
917                         default:
918                                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
919                                 wmb();
920                                 continue;
921                         }
922 #endif
923                 }
924
925                 switch (pkt->entry_type) {
926 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
927                 case ACCEPT_TGT_IO_TYPE:
928                 case CONTINUE_TGT_IO_TYPE:
929                 case CTIO_A64_TYPE:
930                 case IMMED_NOTIFY_TYPE:
931                 case NOTIFY_ACK_TYPE:
932                 case ENABLE_LUN_TYPE:
933                 case MODIFY_LUN_TYPE:
934                         DEBUG5(printk(KERN_WARNING
935                                       "qla2x00_response_pkt: "
936                                       "calling tgt_response_pkt %p "
937                                       "(type %02X)\n",
938                                       qla_target.tgt_response_pkt,
939                                       pkt->entry_type););
940                         if (qla_target.tgt_response_pkt)
941                                 qla_target.tgt_response_pkt(ha,
942                                         (response_t *)pkt);
943                         break;
944 #endif /* CONFIG_SCSI_QLA2XXX_TARGET */
945                 case STATUS_TYPE:
946                         qla2x00_status_entry(ha, pkt);
947                         break;
948                 case STATUS_TYPE_21:
949                         handle_cnt = ((sts21_entry_t *)pkt)->handle_count;
950                         for (cnt = 0; cnt < handle_cnt; cnt++) {
951                                 qla2x00_process_completed_request(ha,
952                                     ((sts21_entry_t *)pkt)->handle[cnt]);
953                         }
954                         break;
955                 case STATUS_TYPE_22:
956                         handle_cnt = ((sts22_entry_t *)pkt)->handle_count;
957                         for (cnt = 0; cnt < handle_cnt; cnt++) {
958                                 qla2x00_process_completed_request(ha,
959                                     ((sts22_entry_t *)pkt)->handle[cnt]);
960                         }
961                         break;
962                 case STATUS_CONT_TYPE:
963                         qla2x00_status_cont_entry(ha, (sts_cont_entry_t *)pkt);
964                         break;
965                 case MARKER_TYPE:
966                         break;
967                 default:
968                         /* Type Not Supported. */
969                         DEBUG4(printk(KERN_WARNING
970                             "scsi(%ld): Received unknown response pkt type %x "
971                             "entry status=%x.\n",
972                             ha->host_no, pkt->entry_type, pkt->entry_status));
973                         break;
974                 }
975                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
976                 wmb();
977         }
978
979         /* Adjust ring index */
980         WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), ha->rsp_ring_index);
981 }
982
983 static inline void
984 qla2x00_handle_sense(srb_t *sp, uint8_t *sense_data, uint32_t sense_len)
985 {
986         struct scsi_cmnd *cp = sp->cmd;
987
988         if (sense_len >= SCSI_SENSE_BUFFERSIZE)
989                 sense_len = SCSI_SENSE_BUFFERSIZE;
990
991         CMD_ACTUAL_SNSLEN(cp) = sense_len;
992         sp->request_sense_length = sense_len;
993         sp->request_sense_ptr = cp->sense_buffer;
994         if (sp->request_sense_length > 32)
995                 sense_len = 32;
996
997         memcpy(cp->sense_buffer, sense_data, sense_len);
998
999         sp->request_sense_ptr += sense_len;
1000         sp->request_sense_length -= sense_len;
1001         if (sp->request_sense_length != 0)
1002                 sp->ha->status_srb = sp;
1003
1004         DEBUG5(printk("%s(): Check condition Sense data, scsi(%ld:%d:%d:%d) "
1005             "cmd=%p pid=%ld\n", __func__, sp->ha->host_no, cp->device->channel,
1006             cp->device->id, cp->device->lun, cp, cp->serial_number));
1007         if (sense_len)
1008                 DEBUG5(qla2x00_dump_buffer(cp->sense_buffer,
1009                     CMD_ACTUAL_SNSLEN(cp)));
1010 }
1011
1012 /**
1013  * qla2x00_status_entry() - Process a Status IOCB entry.
1014  * @ha: SCSI driver HA context
1015  * @pkt: Entry pointer
1016  */
1017 static void
1018 qla2x00_status_entry(scsi_qla_host_t *ha, void *pkt)
1019 {
1020         srb_t           *sp;
1021         fc_port_t       *fcport;
1022         struct scsi_cmnd *cp;
1023         sts_entry_t *sts;
1024         struct sts_entry_24xx *sts24;
1025         uint16_t        comp_status;
1026         uint16_t        scsi_status;
1027         uint8_t         lscsi_status;
1028         int32_t         resid;
1029         uint32_t        sense_len, rsp_info_len, resid_len, fw_resid_len;
1030         uint8_t         *rsp_info, *sense_data;
1031
1032         sts = (sts_entry_t *) pkt;
1033         sts24 = (struct sts_entry_24xx *) pkt;
1034         if (IS_FWI2_CAPABLE(ha)) {
1035                 comp_status = le16_to_cpu(sts24->comp_status);
1036                 scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
1037         } else {
1038                 comp_status = le16_to_cpu(sts->comp_status);
1039                 scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
1040         }
1041
1042         /* Fast path completion. */
1043         if (comp_status == CS_COMPLETE && scsi_status == 0) {
1044                 DEBUG3(printk(KERN_WARNING "scsi(%ld): "
1045                               "Status Type %#x:\n"
1046                               "  entry_count\t%d\n"
1047                               "  entry_status\t%#x\n"
1048                               "  handle:\t%#x\n"
1049                               "  scsi_status:\t%#x\n"
1050                               "  comp_status:\t%#x\n"
1051                               "  state_flags:\t%#x\n"
1052                               "  status_flags:\t%#x\n",
1053                               ha->host_no,
1054                               sts->entry_type,
1055                               sts->entry_count,
1056                               sts->entry_status,
1057                               sts->handle,
1058                               sts->scsi_status,
1059                               sts->comp_status,
1060                               sts->state_flags,
1061                               sts->status_flags));
1062                 qla2x00_process_completed_request(ha, sts->handle);
1063
1064                 return;
1065         }
1066
1067         /* Validate handle. */
1068         if (sts->handle < MAX_OUTSTANDING_COMMANDS) {
1069                 sp = ha->outstanding_cmds[sts->handle];
1070                 ha->outstanding_cmds[sts->handle] = NULL;
1071         } else
1072                 sp = NULL;
1073
1074         if (sp == NULL) {
1075                 DEBUG2(printk("scsi(%ld): Status Entry invalid handle.\n",
1076                     ha->host_no));
1077                 qla_printk(KERN_WARNING, ha, "Status Entry invalid handle.\n");
1078
1079                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1080                 qla2xxx_wake_dpc(ha);
1081                 return;
1082         }
1083         cp = sp->cmd;
1084         if (cp == NULL) {
1085                 DEBUG2(printk("scsi(%ld): Command already returned back to OS "
1086                     "pkt->handle=%d sp=%p.\n", ha->host_no, sts->handle, sp));
1087                 qla_printk(KERN_WARNING, ha,
1088                     "Command is NULL: already returned to OS (sp=%p)\n", sp);
1089
1090                 return;
1091         }
1092
1093         lscsi_status = scsi_status & STATUS_MASK;
1094         CMD_ENTRY_STATUS(cp) = sts->entry_status;
1095         CMD_COMPL_STATUS(cp) = comp_status;
1096         CMD_SCSI_STATUS(cp) = scsi_status;
1097
1098         fcport = sp->fcport;
1099
1100         sense_len = rsp_info_len = resid_len = fw_resid_len = 0;
1101         if (IS_FWI2_CAPABLE(ha)) {
1102                 sense_len = le32_to_cpu(sts24->sense_len);
1103                 rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
1104                 resid_len = le32_to_cpu(sts24->rsp_residual_count);
1105                 fw_resid_len = le32_to_cpu(sts24->residual_len);
1106                 rsp_info = sts24->data;
1107                 sense_data = sts24->data;
1108                 host_to_fcp_swap(sts24->data, sizeof(sts24->data));
1109         } else {
1110                 sense_len = le16_to_cpu(sts->req_sense_length);
1111                 rsp_info_len = le16_to_cpu(sts->rsp_info_len);
1112                 resid_len = le32_to_cpu(sts->residual_length);
1113                 rsp_info = sts->rsp_info;
1114                 sense_data = sts->req_sense_data;
1115         }
1116
1117         /* Check for any FCP transport errors. */
1118         if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
1119                 /* Sense data lies beyond any FCP RESPONSE data. */
1120                 if (IS_FWI2_CAPABLE(ha))
1121                         sense_data += rsp_info_len;
1122                 if (rsp_info_len > 3 && rsp_info[3]) {
1123                         DEBUG2(printk("scsi(%ld:%d:%d:%d) FCP I/O protocol "
1124                             "failure (%x/%02x%02x%02x%02x%02x%02x%02x%02x)..."
1125                             "retrying command\n", ha->host_no,
1126                             cp->device->channel, cp->device->id,
1127                             cp->device->lun, rsp_info_len, rsp_info[0],
1128                             rsp_info[1], rsp_info[2], rsp_info[3], rsp_info[4],
1129                             rsp_info[5], rsp_info[6], rsp_info[7]));
1130
1131                         cp->result = DID_BUS_BUSY << 16;
1132                         qla2x00_sp_compl(ha, sp);
1133                         return;
1134                 }
1135         }
1136
1137         /* Check for overrun. */
1138         if (IS_FWI2_CAPABLE(ha) && comp_status == CS_COMPLETE &&
1139             scsi_status & SS_RESIDUAL_OVER)
1140                 comp_status = CS_DATA_OVERRUN;
1141
1142         /*
1143          * Based on Host and scsi status generate status code for Linux
1144          */
1145         switch (comp_status) {
1146         case CS_COMPLETE:
1147         case CS_QUEUE_FULL:
1148                 if (scsi_status == 0) {
1149                         cp->result = DID_OK << 16;
1150                         break;
1151                 }
1152                 if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
1153                         resid = resid_len;
1154                         scsi_set_resid(cp, resid);
1155                         CMD_RESID_LEN(cp) = resid;
1156
1157                         if (!lscsi_status &&
1158                             ((unsigned)(scsi_bufflen(cp) - resid) <
1159                              cp->underflow)) {
1160                                 qla_printk(KERN_INFO, ha,
1161                                            "scsi(%ld:%d:%d:%d): Mid-layer underflow "
1162                                            "detected (%x of %x bytes)...returning "
1163                                            "error status.\n", ha->host_no,
1164                                            cp->device->channel, cp->device->id,
1165                                            cp->device->lun, resid,
1166                                            scsi_bufflen(cp));
1167
1168                                 cp->result = DID_ERROR << 16;
1169                                 break;
1170                         }
1171                 }
1172                 cp->result = DID_OK << 16 | lscsi_status;
1173
1174                 if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1175                         DEBUG2(printk(KERN_INFO
1176                             "scsi(%ld): QUEUE FULL status detected "
1177                             "0x%x-0x%x.\n", ha->host_no, comp_status,
1178                             scsi_status));
1179
1180                         /* Adjust queue depth for all luns on the port. */
1181                         fcport->last_queue_full = jiffies;
1182                         starget_for_each_device(cp->device->sdev_target,
1183                             fcport, qla2x00_adjust_sdev_qdepth_down);
1184                         break;
1185                 }
1186                 if (lscsi_status != SS_CHECK_CONDITION)
1187                         break;
1188
1189                 memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1190                 if (!(scsi_status & SS_SENSE_LEN_VALID))
1191                         break;
1192
1193                 qla2x00_handle_sense(sp, sense_data, sense_len);
1194                 break;
1195
1196         case CS_DATA_UNDERRUN:
1197                 resid = resid_len;
1198                 /* Use F/W calculated residual length. */
1199                 if (IS_FWI2_CAPABLE(ha)) {
1200                         if (scsi_status & SS_RESIDUAL_UNDER &&
1201                             resid != fw_resid_len) {
1202                                 scsi_status &= ~SS_RESIDUAL_UNDER;
1203                                 lscsi_status = 0;
1204                         }
1205                         resid = fw_resid_len;
1206                 }
1207
1208                 if (scsi_status & SS_RESIDUAL_UNDER) {
1209                         scsi_set_resid(cp, resid);
1210                         CMD_RESID_LEN(cp) = resid;
1211                 } else {
1212                         DEBUG2(printk(KERN_INFO
1213                             "scsi(%ld:%d:%d) UNDERRUN status detected "
1214                             "0x%x-0x%x. resid=0x%x fw_resid=0x%x cdb=0x%x "
1215                             "os_underflow=0x%x\n", ha->host_no,
1216                             cp->device->id, cp->device->lun, comp_status,
1217                             scsi_status, resid_len, resid, cp->cmnd[0],
1218                             cp->underflow));
1219
1220                 }
1221
1222                 /*
1223                  * Check to see if SCSI Status is non zero. If so report SCSI
1224                  * Status.
1225                  */
1226                 if (lscsi_status != 0) {
1227                         cp->result = DID_OK << 16 | lscsi_status;
1228
1229                         if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1230                                 DEBUG2(printk(KERN_INFO
1231                                     "scsi(%ld): QUEUE FULL status detected "
1232                                     "0x%x-0x%x.\n", ha->host_no, comp_status,
1233                                     scsi_status));
1234
1235                                 /*
1236                                  * Adjust queue depth for all luns on the
1237                                  * port.
1238                                  */
1239                                 fcport->last_queue_full = jiffies;
1240                                 starget_for_each_device(
1241                                     cp->device->sdev_target, fcport,
1242                                     qla2x00_adjust_sdev_qdepth_down);
1243                                 break;
1244                         }
1245                         if (lscsi_status != SS_CHECK_CONDITION)
1246                                 break;
1247
1248                         memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1249                         if (!(scsi_status & SS_SENSE_LEN_VALID))
1250                                 break;
1251
1252                         qla2x00_handle_sense(sp, sense_data, sense_len);
1253                 } else {
1254                         /*
1255                          * If RISC reports underrun and target does not report
1256                          * it then we must have a lost frame, so tell upper
1257                          * layer to retry it by reporting a bus busy.
1258                          */
1259                         if (!(scsi_status & SS_RESIDUAL_UNDER)) {
1260                                 DEBUG2(printk("scsi(%ld:%d:%d:%d) Dropped "
1261                                               "frame(s) detected (%x of %x bytes)..."
1262                                               "retrying command.\n", ha->host_no,
1263                                               cp->device->channel, cp->device->id,
1264                                               cp->device->lun, resid,
1265                                               scsi_bufflen(cp)));
1266
1267                                 cp->result = DID_BUS_BUSY << 16;
1268                                 break;
1269                         }
1270
1271                         /* Handle mid-layer underflow */
1272                         if ((unsigned)(scsi_bufflen(cp) - resid) <
1273                             cp->underflow) {
1274                                 qla_printk(KERN_INFO, ha,
1275                                            "scsi(%ld:%d:%d:%d): Mid-layer underflow "
1276                                            "detected (%x of %x bytes)...returning "
1277                                            "error status.\n", ha->host_no,
1278                                            cp->device->channel, cp->device->id,
1279                                            cp->device->lun, resid,
1280                                            scsi_bufflen(cp));
1281
1282                                 cp->result = DID_ERROR << 16;
1283                                 break;
1284                         }
1285
1286                         /* Everybody online, looking good... */
1287                         cp->result = DID_OK << 16;
1288                 }
1289                 break;
1290
1291         case CS_DATA_OVERRUN:
1292                 DEBUG2(printk(KERN_INFO
1293                     "scsi(%ld:%d:%d): OVERRUN status detected 0x%x-0x%x\n",
1294                     ha->host_no, cp->device->id, cp->device->lun, comp_status,
1295                     scsi_status));
1296                 DEBUG2(printk(KERN_INFO
1297                     "CDB: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1298                     cp->cmnd[0], cp->cmnd[1], cp->cmnd[2], cp->cmnd[3],
1299                     cp->cmnd[4], cp->cmnd[5]));
1300                 DEBUG2(printk(KERN_INFO
1301                     "PID=0x%lx req=0x%x xtra=0x%x -- returning DID_ERROR "
1302                     "status!\n",
1303                     cp->serial_number, scsi_bufflen(cp), resid_len));
1304
1305                 cp->result = DID_ERROR << 16;
1306                 break;
1307
1308         case CS_PORT_LOGGED_OUT:
1309         case CS_PORT_CONFIG_CHG:
1310         case CS_PORT_BUSY:
1311         case CS_INCOMPLETE:
1312         case CS_PORT_UNAVAILABLE:
1313                 /*
1314                  * If the port is in Target Down state, return all IOs for this
1315                  * Target with DID_NO_CONNECT ELSE Queue the IOs in the
1316                  * retry_queue.
1317                  */
1318                 DEBUG2(printk("scsi(%ld:%d:%d): status_entry: Port Down "
1319                     "pid=%ld, compl status=0x%x, port state=0x%x\n",
1320                     ha->host_no, cp->device->id, cp->device->lun,
1321                     cp->serial_number, comp_status,
1322                     atomic_read(&fcport->state)));
1323
1324                 cp->result = DID_BUS_BUSY << 16;
1325                 if (atomic_read(&fcport->state) == FCS_ONLINE) {
1326                         qla2x00_mark_device_lost(ha, fcport, 1, 1);
1327                 }
1328                 break;
1329
1330         case CS_RESET:
1331                 DEBUG2(printk(KERN_INFO
1332                     "scsi(%ld): RESET status detected 0x%x-0x%x.\n",
1333                     ha->host_no, comp_status, scsi_status));
1334
1335                 cp->result = DID_RESET << 16;
1336                 break;
1337
1338         case CS_ABORTED:
1339                 /*
1340                  * hv2.19.12 - DID_ABORT does not retry the request if we
1341                  * aborted this request then abort otherwise it must be a
1342                  * reset.
1343                  */
1344                 DEBUG2(printk(KERN_INFO
1345                     "scsi(%ld): ABORT status detected 0x%x-0x%x.\n",
1346                     ha->host_no, comp_status, scsi_status));
1347
1348                 cp->result = DID_RESET << 16;
1349                 break;
1350
1351         case CS_TIMEOUT:
1352                 cp->result = DID_BUS_BUSY << 16;
1353
1354                 if (IS_FWI2_CAPABLE(ha)) {
1355                         DEBUG2(printk(KERN_INFO
1356                             "scsi(%ld:%d:%d:%d): TIMEOUT status detected "
1357                             "0x%x-0x%x\n", ha->host_no, cp->device->channel,
1358                             cp->device->id, cp->device->lun, comp_status,
1359                             scsi_status));
1360                         break;
1361                 }
1362                 DEBUG2(printk(KERN_INFO
1363                     "scsi(%ld:%d:%d:%d): TIMEOUT status detected 0x%x-0x%x "
1364                     "sflags=%x.\n", ha->host_no, cp->device->channel,
1365                     cp->device->id, cp->device->lun, comp_status, scsi_status,
1366                     le16_to_cpu(sts->status_flags)));
1367
1368                 /* Check to see if logout occurred. */
1369                 if ((le16_to_cpu(sts->status_flags) & SF_LOGOUT_SENT))
1370                         qla2x00_mark_device_lost(ha, fcport, 1, 1);
1371                 break;
1372
1373         default:
1374                 DEBUG3(printk("scsi(%ld): Error detected (unknown status) "
1375                     "0x%x-0x%x.\n", ha->host_no, comp_status, scsi_status));
1376                 qla_printk(KERN_INFO, ha,
1377                     "Unknown status detected 0x%x-0x%x.\n",
1378                     comp_status, scsi_status);
1379
1380                 cp->result = DID_ERROR << 16;
1381                 break;
1382         }
1383
1384         /* Place command on done queue. */
1385         if (ha->status_srb == NULL)
1386                 qla2x00_sp_compl(ha, sp);
1387 }
1388
1389 /**
1390  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
1391  * @ha: SCSI driver HA context
1392  * @pkt: Entry pointer
1393  *
1394  * Extended sense data.
1395  */
1396 static void
1397 qla2x00_status_cont_entry(scsi_qla_host_t *ha, sts_cont_entry_t *pkt)
1398 {
1399         uint8_t         sense_sz = 0;
1400         srb_t           *sp = ha->status_srb;
1401         struct scsi_cmnd *cp;
1402
1403         if (sp != NULL && sp->request_sense_length != 0) {
1404                 cp = sp->cmd;
1405                 if (cp == NULL) {
1406                         DEBUG2(printk("%s(): Cmd already returned back to OS "
1407                             "sp=%p.\n", __func__, sp));
1408                         qla_printk(KERN_INFO, ha,
1409                             "cmd is NULL: already returned to OS (sp=%p)\n",
1410                             sp);
1411
1412                         ha->status_srb = NULL;
1413                         return;
1414                 }
1415
1416                 if (sp->request_sense_length > sizeof(pkt->data)) {
1417                         sense_sz = sizeof(pkt->data);
1418                 } else {
1419                         sense_sz = sp->request_sense_length;
1420                 }
1421
1422                 /* Move sense data. */
1423                 if (IS_FWI2_CAPABLE(ha))
1424                         host_to_fcp_swap(pkt->data, sizeof(pkt->data));
1425                 memcpy(sp->request_sense_ptr, pkt->data, sense_sz);
1426                 DEBUG5(qla2x00_dump_buffer(sp->request_sense_ptr, sense_sz));
1427
1428                 sp->request_sense_ptr += sense_sz;
1429                 sp->request_sense_length -= sense_sz;
1430
1431                 /* Place command on done queue. */
1432                 if (sp->request_sense_length == 0) {
1433                         ha->status_srb = NULL;
1434                         qla2x00_sp_compl(ha, sp);
1435                 }
1436         }
1437 }
1438
1439 /**
1440  * qla2x00_error_entry() - Process an error entry.
1441  * @ha: SCSI driver HA context
1442  * @pkt: Entry pointer
1443  */
1444 static void
1445 qla2x00_error_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
1446 {
1447         srb_t *sp;
1448
1449 #if defined(QL_DEBUG_LEVEL_2)
1450         qla_printk(KERN_ERR, ha, "%s: Error entry with type %x:\n", __func__,
1451                 pkt->entry_type);
1452         if (pkt->entry_status & RF_INV_E_ORDER) {
1453                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Order:\n", __func__);
1454                 qla2x00_dump_buffer((void *)pkt, sizeof(*pkt));
1455         } else if (pkt->entry_status & RF_INV_E_COUNT)
1456                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Count\n", __func__);
1457         else if (pkt->entry_status & RF_INV_E_PARAM)
1458                 qla_printk(KERN_ERR, ha,
1459                     "%s: Invalid Entry Parameter\n", __func__);
1460         else if (pkt->entry_status & RF_INV_E_TYPE)
1461                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Type\n", __func__);
1462         else if (pkt->entry_status & RF_BUSY)
1463                 qla_printk(KERN_ERR, ha, "%s: Busy\n", __func__);
1464         else
1465                 qla_printk(KERN_ERR, ha, "%s: UNKNOWN flag error\n", __func__);
1466 #endif
1467
1468         /* Validate handle. */
1469         if (pkt->handle < MAX_OUTSTANDING_COMMANDS)
1470                 sp = ha->outstanding_cmds[pkt->handle];
1471         else
1472                 sp = NULL;
1473
1474         if (sp) {
1475                 /* Free outstanding command slot. */
1476                 ha->outstanding_cmds[pkt->handle] = NULL;
1477
1478                 /* Bad payload or header */
1479                 if (pkt->entry_status &
1480                     (RF_INV_E_ORDER | RF_INV_E_COUNT |
1481                      RF_INV_E_PARAM | RF_INV_E_TYPE)) {
1482                         sp->cmd->result = DID_ERROR << 16;
1483                 } else if (pkt->entry_status & RF_BUSY) {
1484                         sp->cmd->result = DID_BUS_BUSY << 16;
1485                 } else {
1486                         sp->cmd->result = DID_ERROR << 16;
1487                 }
1488                 qla2x00_sp_compl(ha, sp);
1489
1490         } else if (pkt->entry_type == COMMAND_A64_TYPE || pkt->entry_type ==
1491             COMMAND_TYPE || pkt->entry_type == COMMAND_TYPE_7) {
1492                 DEBUG2(printk("scsi(%ld): Error entry - invalid handle\n",
1493                     ha->host_no));
1494                 qla_printk(KERN_WARNING, ha,
1495                     "Error entry - invalid handle\n");
1496
1497                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1498                 qla2xxx_wake_dpc(ha);
1499         }
1500 }
1501
1502 /**
1503  * qla24xx_mbx_completion() - Process mailbox command completions.
1504  * @ha: SCSI driver HA context
1505  * @mb0: Mailbox0 register
1506  */
1507 static void
1508 qla24xx_mbx_completion(scsi_qla_host_t *ha, uint16_t mb0)
1509 {
1510         uint16_t        cnt;
1511         uint16_t __iomem *wptr;
1512         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1513
1514         /* Load return mailbox registers. */
1515         ha->flags.mbox_int = 1;
1516         ha->mailbox_out[0] = mb0;
1517         wptr = (uint16_t __iomem *)&reg->mailbox1;
1518
1519         for (cnt = 1; cnt < ha->mbx_count; cnt++) {
1520                 ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
1521                 wptr++;
1522         }
1523
1524         if (ha->mcp) {
1525                 DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
1526                     __func__, ha->host_no, ha->mcp->mb[0]));
1527         } else {
1528                 DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
1529                     __func__, ha->host_no));
1530         }
1531
1532 #if defined(QL_DEBUG_LEVEL_1)
1533         printk(KERN_INFO "scsi(%ld): Mailbox registers:", ha->host_no);
1534         for (cnt = 0; cnt < ha->mbx_count; cnt++) {
1535                 if ((cnt % 4) == 0)
1536                         printk(KERN_CONT "\n");
1537                 printk("mbox %02d: 0x%04x   ", cnt, ha->mailbox_out[cnt]);
1538         }
1539         printk(KERN_CONT "\n");
1540 #endif
1541 }
1542
1543 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
1544 /**
1545  * qla24xx_process_atio_queue() - Process ATIO queue entries.
1546  * @ha: SCSI driver HA context
1547  */
1548 static void
1549 qla24xx_process_atio_queue(struct scsi_qla_host *ha)
1550 {
1551         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1552         atio_t *pkt;
1553         int cnt, i;
1554
1555         if (!ha->flags.online)
1556                 return;
1557
1558         while (ha->atio_ring_ptr->signature != ATIO_PROCESSED) {
1559                 pkt = ha->atio_ring_ptr;
1560                 cnt = pkt->entry_count;
1561
1562                 DEBUG5(printk(KERN_INFO "%s(): IOCB data:\n", __func__));
1563                 DEBUG5(qla2x00_dump_buffer((uint8_t *)pkt, RESPONSE_ENTRY_SIZE));
1564
1565                 DEBUG5(printk(KERN_WARNING
1566                               "%s: calling tgt24_atio_pkt %p (type %02X, "
1567                               "count %d)\n", __func__, qla_target.tgt24_atio_pkt,
1568                               pkt->entry_type, cnt););
1569
1570                 if (likely(qla_target.tgt24_atio_pkt))
1571                         qla_target.tgt24_atio_pkt(ha, (atio7_entry_t *)pkt);
1572
1573                 for (i = 0; i < cnt; i++) {
1574                         ha->atio_ring_index++;
1575                         if (ha->atio_ring_index == ha->atio_q_length) {
1576                                 ha->atio_ring_index = 0;
1577                                 ha->atio_ring_ptr = ha->atio_ring;
1578                         } else
1579                                 ha->atio_ring_ptr++;
1580
1581                         pkt->signature = ATIO_PROCESSED;
1582                         pkt = ha->atio_ring_ptr;
1583                 }
1584                 wmb();
1585         }
1586
1587         /* Adjust ring index */
1588         WRT_REG_DWORD(&reg->atio_q_out, ha->atio_ring_index);
1589 }
1590 #endif /* CONFIG_SCSI_QLA2XXX_TARGET */
1591
1592 /**
1593  * qla24xx_process_response_queue() - Process response queue entries.
1594  * @ha: SCSI driver HA context
1595  */
1596 void
1597 qla24xx_process_response_queue(struct scsi_qla_host *ha)
1598 {
1599         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1600         struct sts_entry_24xx *pkt;
1601
1602         if (!ha->flags.online)
1603                 return;
1604
1605         while (ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
1606                 pkt = (struct sts_entry_24xx *)ha->response_ring_ptr;
1607
1608                 DEBUG5(printk(KERN_INFO "%s(): IOCB data:\n", __func__));
1609                 DEBUG5(qla2x00_dump_buffer((uint8_t *)pkt, RESPONSE_ENTRY_SIZE));
1610
1611                 ha->rsp_ring_index++;
1612                 if (ha->rsp_ring_index == ha->response_q_length) {
1613                         ha->rsp_ring_index = 0;
1614                         ha->response_ring_ptr = ha->response_ring;
1615                 } else {
1616                         ha->response_ring_ptr++;
1617                 }
1618
1619                 if (pkt->entry_status != 0) {
1620                         DEBUG3(printk(KERN_INFO
1621                             "scsi(%ld): Process error entry.\n", ha->host_no));
1622
1623                         qla2x00_error_entry(ha, (sts_entry_t *) pkt);
1624 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
1625                         switch (pkt->entry_type) {
1626                         case ABTS_RECV_24XX:
1627                         case ABTS_RESP_24XX:
1628                         case CTIO_TYPE7:
1629                         case NOTIFY_ACK_TYPE:
1630                                 break;
1631                         default:
1632                                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1633                                 wmb();
1634                                 continue;
1635                         }
1636 #endif
1637                 }
1638
1639                 switch (pkt->entry_type) {
1640                 case STATUS_TYPE:
1641                         qla2x00_status_entry(ha, pkt);
1642                         break;
1643                 case STATUS_CONT_TYPE:
1644                         qla2x00_status_cont_entry(ha, (sts_cont_entry_t *)pkt);
1645                         break;
1646                 case VP_RPT_ID_IOCB_TYPE:
1647                         qla24xx_report_id_acquisition(ha,
1648                             (struct vp_rpt_id_entry_24xx *)pkt);
1649                         break;
1650 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
1651                 case ABTS_RECV_24XX:
1652                         /* ensure that the ATIO queue is empty */
1653                         qla24xx_process_atio_queue(ha);
1654                         /* go through */
1655                 case ABTS_RESP_24XX:
1656                 case CTIO_TYPE7:
1657                 case NOTIFY_ACK_TYPE:
1658                         DEBUG5(printk(KERN_WARNING
1659                                       "qla2x00_response_pkt: "
1660                                       "calling tgt_response_pkt %p "
1661                                       "(type %02X)\n",
1662                                       qla_target.tgt_response_pkt,
1663                                       pkt->entry_type););
1664                         if (qla_target.tgt_response_pkt)
1665                                 qla_target.tgt_response_pkt(ha,
1666                                         (response_t *)pkt);
1667                         break;
1668 #endif /* CONFIG_SCSI_QLA2XXX_TARGET */
1669                 case MARKER_TYPE:
1670                         break;
1671                 default:
1672                         /* Type Not Supported. */
1673                         DEBUG4(printk(KERN_WARNING
1674                             "scsi(%ld): Received unknown response pkt type %x "
1675                             "entry status=%x.\n",
1676                             ha->host_no, pkt->entry_type, pkt->entry_status));
1677                         break;
1678                 }
1679                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1680                 wmb();
1681         }
1682
1683         /* Adjust ring index */
1684         WRT_REG_DWORD(&reg->rsp_q_out, ha->rsp_ring_index);
1685 }
1686
1687 static void
1688 qla2xxx_check_risc_status(scsi_qla_host_t *ha)
1689 {
1690         int rval;
1691         uint32_t cnt;
1692         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1693
1694         if (!IS_QLA25XX(ha))
1695                 return;
1696
1697         rval = QLA_SUCCESS;
1698         WRT_REG_DWORD(&reg->iobase_addr, 0x7C00);
1699         RD_REG_DWORD(&reg->iobase_addr);
1700         WRT_REG_DWORD(&reg->iobase_window, 0x0001);
1701         for (cnt = 10000; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
1702             rval == QLA_SUCCESS; cnt--) {
1703                 if (cnt) {
1704                         WRT_REG_DWORD(&reg->iobase_window, 0x0001);
1705                         udelay(10);
1706                 } else
1707                         rval = QLA_FUNCTION_TIMEOUT;
1708         }
1709         if (rval == QLA_SUCCESS)
1710                 goto next_test;
1711
1712         WRT_REG_DWORD(&reg->iobase_window, 0x0003);
1713         for (cnt = 100; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
1714             rval == QLA_SUCCESS; cnt--) {
1715                 if (cnt) {
1716                         WRT_REG_DWORD(&reg->iobase_window, 0x0003);
1717                         udelay(10);
1718                 } else
1719                         rval = QLA_FUNCTION_TIMEOUT;
1720         }
1721         if (rval != QLA_SUCCESS)
1722                 goto done;
1723
1724 next_test:
1725         if (RD_REG_DWORD(&reg->iobase_c8) & BIT_3)
1726                 qla_printk(KERN_INFO, ha, "Additional code -- 0x55AA.\n");
1727
1728 done:
1729         WRT_REG_DWORD(&reg->iobase_window, 0x0000);
1730         RD_REG_DWORD(&reg->iobase_window);
1731 }
1732
1733 /**
1734  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
1735  * @irq:
1736  * @dev_id: SCSI driver HA context
1737  *
1738  * Called by system whenever the host adapter generates an interrupt.
1739  *
1740  * Returns handled flag.
1741  */
1742 irqreturn_t
1743 qla24xx_intr_handler(int irq, void *dev_id)
1744 {
1745         scsi_qla_host_t *ha;
1746         struct device_reg_24xx __iomem *reg;
1747         int             status;
1748         unsigned long   iter;
1749         uint32_t        stat;
1750         uint32_t        hccr;
1751         uint16_t        mb[4];
1752
1753         ha = (scsi_qla_host_t *) dev_id;
1754         if (!ha) {
1755                 printk(KERN_INFO
1756                     "%s(): NULL host pointer\n", __func__);
1757                 return IRQ_NONE;
1758         }
1759
1760         reg = &ha->iobase->isp24;
1761         status = 0;
1762
1763         spin_lock(&ha->hardware_lock);
1764         for (iter = 50; iter--; ) {
1765                 stat = RD_REG_DWORD(&reg->host_status);
1766                 if (stat & HSRX_RISC_PAUSED) {
1767                         if (pci_channel_offline(ha->pdev))
1768                                 break;
1769
1770                         if (ha->hw_event_pause_errors == 0)
1771                                 qla2x00_post_hwe_work(ha, HW_EVENT_PARITY_ERR,
1772                                     0, MSW(stat), LSW(stat));
1773                         else if (ha->hw_event_pause_errors < 0xffffffff)
1774                                 ha->hw_event_pause_errors++;
1775
1776                         hccr = RD_REG_DWORD(&reg->hccr);
1777
1778                         qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
1779                             "Dumping firmware!\n", hccr);
1780
1781                         qla2xxx_check_risc_status(ha);
1782
1783                         ha->isp_ops->fw_dump(ha, 1);
1784                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1785                         break;
1786                 } else if ((stat & HSRX_RISC_INT) == 0)
1787                         break;
1788
1789                 switch (stat & 0xff) {
1790                 case 0x1:
1791                 case 0x2:
1792                 case 0x10:
1793                 case 0x11:
1794                         qla24xx_mbx_completion(ha, MSW(stat));
1795                         status |= MBX_INTERRUPT;
1796
1797                         break;
1798                 case 0x12:
1799                         mb[0] = MSW(stat);
1800                         mb[1] = RD_REG_WORD(&reg->mailbox1);
1801                         mb[2] = RD_REG_WORD(&reg->mailbox2);
1802                         mb[3] = RD_REG_WORD(&reg->mailbox3);
1803                         qla2x00_async_event(ha, mb);
1804                         break;
1805                 case 0x13:
1806                         qla24xx_process_response_queue(ha);
1807                         break;
1808 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
1809                 case 0x1C: /* ATIO queue updated */
1810                         qla24xx_process_atio_queue(ha);
1811                         break;
1812                 case 0x1D: /* ATIO and response queues updated */
1813                         qla24xx_process_atio_queue(ha);
1814                         qla24xx_process_response_queue(ha);
1815                         break;
1816 #endif
1817                 default:
1818                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
1819                             "(%d).\n",
1820                             ha->host_no, stat & 0xff));
1821                         break;
1822                 }
1823                 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1824                 RD_REG_DWORD_RELAXED(&reg->hccr);
1825         }
1826         spin_unlock(&ha->hardware_lock);
1827
1828         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
1829             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
1830                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
1831                 complete(&ha->mbx_intr_comp);
1832         }
1833
1834         return IRQ_HANDLED;
1835 }
1836
1837 static irqreturn_t
1838 qla24xx_msix_rsp_q(int irq, void *dev_id)
1839 {
1840         scsi_qla_host_t *ha;
1841         struct device_reg_24xx __iomem *reg;
1842
1843         ha = dev_id;
1844         reg = &ha->iobase->isp24;
1845
1846         spin_lock_irq(&ha->hardware_lock);
1847
1848         qla24xx_process_response_queue(ha);
1849         WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1850
1851         spin_unlock_irq(&ha->hardware_lock);
1852
1853         return IRQ_HANDLED;
1854 }
1855
1856 static irqreturn_t
1857 qla24xx_msix_default(int irq, void *dev_id)
1858 {
1859         scsi_qla_host_t *ha;
1860         struct device_reg_24xx __iomem *reg;
1861         int             status;
1862         uint32_t        stat;
1863         uint32_t        hccr;
1864         uint16_t        mb[4];
1865
1866         ha = dev_id;
1867         reg = &ha->iobase->isp24;
1868         status = 0;
1869
1870         spin_lock_irq(&ha->hardware_lock);
1871         do {
1872                 stat = RD_REG_DWORD(&reg->host_status);
1873                 if (stat & HSRX_RISC_PAUSED) {
1874                         if (pci_channel_offline(ha->pdev))
1875                                 break;
1876
1877                         if (ha->hw_event_pause_errors == 0)
1878                                 qla2x00_post_hwe_work(ha, HW_EVENT_PARITY_ERR,
1879                                     0, MSW(stat), LSW(stat));
1880                         else if (ha->hw_event_pause_errors < 0xffffffff)
1881                                 ha->hw_event_pause_errors++;
1882
1883                         hccr = RD_REG_DWORD(&reg->hccr);
1884
1885                         qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
1886                             "Dumping firmware!\n", hccr);
1887
1888                         qla2xxx_check_risc_status(ha);
1889
1890                         ha->isp_ops->fw_dump(ha, 1);
1891                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1892                         break;
1893                 } else if ((stat & HSRX_RISC_INT) == 0)
1894                         break;
1895
1896                 switch (stat & 0xff) {
1897                 case 0x1:
1898                 case 0x2:
1899                 case 0x10:
1900                 case 0x11:
1901                         qla24xx_mbx_completion(ha, MSW(stat));
1902                         status |= MBX_INTERRUPT;
1903
1904                         break;
1905                 case 0x12:
1906                         mb[0] = MSW(stat);
1907                         mb[1] = RD_REG_WORD(&reg->mailbox1);
1908                         mb[2] = RD_REG_WORD(&reg->mailbox2);
1909                         mb[3] = RD_REG_WORD(&reg->mailbox3);
1910                         qla2x00_async_event(ha, mb);
1911                         break;
1912                 case 0x13:
1913                         qla24xx_process_response_queue(ha);
1914                         break;
1915 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
1916                 case 0x1C: /* ATIO queue updated */
1917                         qla24xx_process_atio_queue(ha);
1918                         break;
1919                 case 0x1D: /* ATIO and response queues updated */
1920                         qla24xx_process_atio_queue(ha);
1921                         qla24xx_process_response_queue(ha);
1922                         break;
1923 #endif
1924                 default:
1925                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
1926                             "(%d).\n",
1927                             ha->host_no, stat & 0xff));
1928                         break;
1929                 }
1930                 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1931         } while (0);
1932         spin_unlock_irq(&ha->hardware_lock);
1933
1934         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
1935             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
1936                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
1937                 complete(&ha->mbx_intr_comp);
1938         }
1939
1940         return IRQ_HANDLED;
1941 }
1942
1943 /* Interrupt handling helpers. */
1944
1945 struct qla_init_msix_entry {
1946         uint16_t entry;
1947         uint16_t index;
1948         const char *name;
1949         irq_handler_t handler;
1950 };
1951
1952 static struct qla_init_msix_entry imsix_entries[QLA_MSIX_ENTRIES] = {
1953         { QLA_MSIX_DEFAULT, QLA_MIDX_DEFAULT,
1954                 "qla2xxx (default)", qla24xx_msix_default },
1955
1956         { QLA_MSIX_RSP_Q, QLA_MIDX_RSP_Q,
1957                 "qla2xxx (rsp_q)", qla24xx_msix_rsp_q },
1958 };
1959
1960 static void
1961 qla24xx_disable_msix(scsi_qla_host_t *ha)
1962 {
1963         int i;
1964         struct qla_msix_entry *qentry;
1965
1966         for (i = 0; i < QLA_MSIX_ENTRIES; i++) {
1967                 qentry = &ha->msix_entries[imsix_entries[i].index];
1968                 if (qentry->have_irq)
1969                         free_irq(qentry->msix_vector, ha);
1970         }
1971         pci_disable_msix(ha->pdev);
1972 }
1973
1974 static int
1975 qla24xx_enable_msix(scsi_qla_host_t *ha)
1976 {
1977         int i, ret;
1978         struct msix_entry entries[QLA_MSIX_ENTRIES];
1979         struct qla_msix_entry *qentry;
1980
1981         for (i = 0; i < QLA_MSIX_ENTRIES; i++)
1982                 entries[i].entry = imsix_entries[i].entry;
1983
1984         ret = pci_enable_msix(ha->pdev, entries, ARRAY_SIZE(entries));
1985         if (ret) {
1986                 qla_printk(KERN_WARNING, ha,
1987                     "MSI-X: Failed to enable support -- %d/%d\n",
1988                     QLA_MSIX_ENTRIES, ret);
1989                 goto msix_out;
1990         }
1991         ha->flags.msix_enabled = 1;
1992
1993         for (i = 0; i < QLA_MSIX_ENTRIES; i++) {
1994                 qentry = &ha->msix_entries[imsix_entries[i].index];
1995                 qentry->msix_vector = entries[i].vector;
1996                 qentry->msix_entry = entries[i].entry;
1997                 qentry->have_irq = 0;
1998                 ret = request_irq(qentry->msix_vector,
1999                     imsix_entries[i].handler, 0, imsix_entries[i].name, ha);
2000                 if (ret) {
2001                         qla_printk(KERN_WARNING, ha,
2002                             "MSI-X: Unable to register handler -- %x/%d.\n",
2003                             imsix_entries[i].index, ret);
2004                         qla24xx_disable_msix(ha);
2005                         goto msix_out;
2006                 }
2007                 qentry->have_irq = 1;
2008         }
2009
2010 msix_out:
2011         return ret;
2012 }
2013
2014 int
2015 qla2x00_request_irqs(scsi_qla_host_t *ha)
2016 {
2017         int ret;
2018         device_reg_t __iomem *reg = ha->iobase;
2019
2020         /* If possible, enable MSI-X. */
2021         if (!IS_QLA2432(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha))
2022                 goto skip_msix;
2023
2024         if (IS_QLA2432(ha) && (ha->chip_revision < QLA_MSIX_CHIP_REV_24XX ||
2025             !QLA_MSIX_FW_MODE_1(ha->fw_attributes))) {
2026                 DEBUG2(qla_printk(KERN_WARNING, ha,
2027                     "MSI-X: Unsupported ISP2432 (0x%X, 0x%X).\n",
2028                     ha->chip_revision, ha->fw_attributes));
2029
2030                 goto skip_msix;
2031         }
2032
2033         if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_HP &&
2034             (ha->pdev->subsystem_device == 0x7040 ||
2035                 ha->pdev->subsystem_device == 0x7041 ||
2036                 ha->pdev->subsystem_device == 0x1705)) {
2037                 DEBUG2(qla_printk(KERN_WARNING, ha,
2038                     "MSI-X: Unsupported ISP2432 SSVID/SSDID (0x%X, 0x%X).\n",
2039                     ha->pdev->subsystem_vendor,
2040                     ha->pdev->subsystem_device));
2041
2042                 goto skip_msi;
2043         }
2044
2045         ret = qla24xx_enable_msix(ha);
2046         if (!ret) {
2047                 DEBUG2(qla_printk(KERN_INFO, ha,
2048                     "MSI-X: Enabled (0x%X, 0x%X).\n", ha->chip_revision,
2049                     ha->fw_attributes));
2050                 goto clear_risc_ints;
2051         }
2052         qla_printk(KERN_WARNING, ha,
2053             "MSI-X: Falling back-to INTa mode -- %d.\n", ret);
2054 skip_msix:
2055
2056         if (!IS_QLA24XX(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha))
2057                 goto skip_msi;
2058
2059         ret = pci_enable_msi(ha->pdev);
2060         if (!ret) {
2061                 DEBUG2(qla_printk(KERN_INFO, ha, "MSI: Enabled.\n"));
2062                 ha->flags.msi_enabled = 1;
2063         }
2064 skip_msi:
2065
2066         ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
2067             IRQF_DISABLED|IRQF_SHARED, QLA2XXX_DRIVER_NAME, ha);
2068         if (ret) {
2069                 qla_printk(KERN_WARNING, ha,
2070                     "Failed to reserve interrupt %d already in use.\n",
2071                     ha->pdev->irq);
2072                 goto fail;
2073         }
2074         ha->flags.inta_enabled = 1;
2075         ha->host->irq = ha->pdev->irq;
2076 clear_risc_ints:
2077
2078         ha->isp_ops->disable_intrs(ha);
2079         spin_lock_irq(&ha->hardware_lock);
2080         if (IS_FWI2_CAPABLE(ha)) {
2081                 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
2082                 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
2083         } else {
2084                 WRT_REG_WORD(&reg->isp.semaphore, 0);
2085                 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
2086                 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
2087         }
2088         spin_unlock_irq(&ha->hardware_lock);
2089
2090 fail:
2091         return ret;
2092 }
2093
2094 void
2095 qla2x00_free_irqs(scsi_qla_host_t *ha)
2096 {
2097
2098         if (ha->flags.msix_enabled)
2099                 qla24xx_disable_msix(ha);
2100         else if (ha->flags.inta_enabled) {
2101                 free_irq(ha->host->irq, ha);
2102                 pci_disable_msi(ha->pdev);
2103         }
2104 }