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