]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/qla4xxx/ql4_isr.c
[SCSI] Merge up to linux-2.6 head
[linux-2.6-omap-h63xx.git] / drivers / scsi / qla4xxx / ql4_isr.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2006 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7
8 #include "ql4_def.h"
9 #include "ql4_glbl.h"
10 #include "ql4_dbg.h"
11 #include "ql4_inline.h"
12
13 /**
14  * qla2x00_process_completed_request() - Process a Fast Post response.
15  * @ha: SCSI driver HA context
16  * @index: SRB index
17  **/
18 static void qla4xxx_process_completed_request(struct scsi_qla_host *ha,
19                                               uint32_t index)
20 {
21         struct srb *srb;
22
23         srb = qla4xxx_del_from_active_array(ha, index);
24         if (srb) {
25                 /* Save ISP completion status */
26                 srb->cmd->result = DID_OK << 16;
27                 qla4xxx_srb_compl(ha, srb);
28         } else {
29                 DEBUG2(printk("scsi%ld: Invalid ISP SCSI completion handle = "
30                               "%d\n", ha->host_no, index));
31                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
32         }
33 }
34
35 /**
36  * qla4xxx_status_entry - processes status IOCBs
37  * @ha: Pointer to host adapter structure.
38  * @sts_entry: Pointer to status entry structure.
39  **/
40 static void qla4xxx_status_entry(struct scsi_qla_host *ha,
41                                  struct status_entry *sts_entry)
42 {
43         uint8_t scsi_status;
44         struct scsi_cmnd *cmd;
45         struct srb *srb;
46         struct ddb_entry *ddb_entry;
47         uint32_t residual;
48         uint16_t sensebytecnt;
49
50         if (sts_entry->completionStatus == SCS_COMPLETE &&
51             sts_entry->scsiStatus == 0) {
52                 qla4xxx_process_completed_request(ha,
53                                                   le32_to_cpu(sts_entry->
54                                                               handle));
55                 return;
56         }
57
58         srb = qla4xxx_del_from_active_array(ha, le32_to_cpu(sts_entry->handle));
59         if (!srb) {
60                 /* FIXMEdg: Don't we need to reset ISP in this case??? */
61                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Status Entry invalid "
62                               "handle 0x%x, sp=%p. This cmd may have already "
63                               "been completed.\n", ha->host_no, __func__,
64                               le32_to_cpu(sts_entry->handle), srb));
65                 return;
66         }
67
68         cmd = srb->cmd;
69         if (cmd == NULL) {
70                 DEBUG2(printk("scsi%ld: %s: Command already returned back to "
71                               "OS pkt->handle=%d srb=%p srb->state:%d\n",
72                               ha->host_no, __func__, sts_entry->handle,
73                               srb, srb->state));
74                 dev_warn(&ha->pdev->dev, "Command is NULL:"
75                         " already returned to OS (srb=%p)\n", srb);
76                 return;
77         }
78
79         ddb_entry = srb->ddb;
80         if (ddb_entry == NULL) {
81                 cmd->result = DID_NO_CONNECT << 16;
82                 goto status_entry_exit;
83         }
84
85         residual = le32_to_cpu(sts_entry->residualByteCnt);
86
87         /* Translate ISP error to a Linux SCSI error. */
88         scsi_status = sts_entry->scsiStatus;
89         switch (sts_entry->completionStatus) {
90         case SCS_COMPLETE:
91                 if (scsi_status == 0) {
92                         cmd->result = DID_OK << 16;
93                         break;
94                 }
95
96                 if (sts_entry->iscsiFlags &
97                     (ISCSI_FLAG_RESIDUAL_OVER|ISCSI_FLAG_RESIDUAL_UNDER))
98                         scsi_set_resid(cmd, residual);
99
100                 cmd->result = DID_OK << 16 | scsi_status;
101
102                 if (scsi_status != SCSI_CHECK_CONDITION)
103                         break;
104
105                 /* Copy Sense Data into sense buffer. */
106                 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
107
108                 sensebytecnt = le16_to_cpu(sts_entry->senseDataByteCnt);
109                 if (sensebytecnt == 0)
110                         break;
111
112                 memcpy(cmd->sense_buffer, sts_entry->senseData,
113                        min(sensebytecnt,
114                            (uint16_t) sizeof(cmd->sense_buffer)));
115
116                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: sense key = %x, "
117                               "ASC/ASCQ = %02x/%02x\n", ha->host_no,
118                               cmd->device->channel, cmd->device->id,
119                               cmd->device->lun, __func__,
120                               sts_entry->senseData[2] & 0x0f,
121                               sts_entry->senseData[12],
122                               sts_entry->senseData[13]));
123
124                 srb->flags |= SRB_GOT_SENSE;
125                 break;
126
127         case SCS_INCOMPLETE:
128                 /* Always set the status to DID_ERROR, since
129                  * all conditions result in that status anyway */
130                 cmd->result = DID_ERROR << 16;
131                 break;
132
133         case SCS_RESET_OCCURRED:
134                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: Device RESET occurred\n",
135                               ha->host_no, cmd->device->channel,
136                               cmd->device->id, cmd->device->lun, __func__));
137
138                 cmd->result = DID_RESET << 16;
139                 break;
140
141         case SCS_ABORTED:
142                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: Abort occurred\n",
143                               ha->host_no, cmd->device->channel,
144                               cmd->device->id, cmd->device->lun, __func__));
145
146                 cmd->result = DID_RESET << 16;
147                 break;
148
149         case SCS_TIMEOUT:
150                 DEBUG2(printk(KERN_INFO "scsi%ld:%d:%d:%d: Timeout\n",
151                               ha->host_no, cmd->device->channel,
152                               cmd->device->id, cmd->device->lun));
153
154                 cmd->result = DID_BUS_BUSY << 16;
155
156                 /*
157                  * Mark device missing so that we won't continue to send
158                  * I/O to this device.  We should get a ddb state change
159                  * AEN soon.
160                  */
161                 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
162                         qla4xxx_mark_device_missing(ha, ddb_entry);
163                 break;
164
165         case SCS_DATA_UNDERRUN:
166         case SCS_DATA_OVERRUN:
167                 if (sts_entry->iscsiFlags & ISCSI_FLAG_RESIDUAL_OVER) {
168                         DEBUG2(printk("scsi%ld:%d:%d:%d: %s: " "Data overrun, "
169                                       "residual = 0x%x\n", ha->host_no,
170                                       cmd->device->channel, cmd->device->id,
171                                       cmd->device->lun, __func__, residual));
172
173                         cmd->result = DID_ERROR << 16;
174                         break;
175                 }
176
177                 if ((sts_entry->iscsiFlags & ISCSI_FLAG_RESIDUAL_UNDER) == 0) {
178                         /*
179                          * Firmware detected a SCSI transport underrun
180                          * condition
181                          */
182                         scsi_set_resid(cmd, residual);
183                         DEBUG2(printk("scsi%ld:%d:%d:%d: %s: UNDERRUN status "
184                                       "detected, xferlen = 0x%x, residual = "
185                                       "0x%x\n",
186                                       ha->host_no, cmd->device->channel,
187                                       cmd->device->id,
188                                       cmd->device->lun, __func__,
189                                       scsi_bufflen(cmd),
190                                       residual));
191                 }
192
193                 /*
194                  * If there is scsi_status, it takes precedense over
195                  * underflow condition.
196                  */
197                 if (scsi_status != 0) {
198                         cmd->result = DID_OK << 16 | scsi_status;
199
200                         if (scsi_status != SCSI_CHECK_CONDITION)
201                                 break;
202
203                         /* Copy Sense Data into sense buffer. */
204                         memset(cmd->sense_buffer, 0,
205                                sizeof(cmd->sense_buffer));
206
207                         sensebytecnt =
208                                 le16_to_cpu(sts_entry->senseDataByteCnt);
209                         if (sensebytecnt == 0)
210                                 break;
211
212                         memcpy(cmd->sense_buffer, sts_entry->senseData,
213                                min(sensebytecnt,
214                                    (uint16_t) sizeof(cmd->sense_buffer)));
215
216                         DEBUG2(printk("scsi%ld:%d:%d:%d: %s: sense key = %x, "
217                                       "ASC/ASCQ = %02x/%02x\n", ha->host_no,
218                                       cmd->device->channel, cmd->device->id,
219                                       cmd->device->lun, __func__,
220                                       sts_entry->senseData[2] & 0x0f,
221                                       sts_entry->senseData[12],
222                                       sts_entry->senseData[13]));
223                 } else {
224                         /*
225                          * If RISC reports underrun and target does not
226                          * report it then we must have a lost frame, so
227                          * tell upper layer to retry it by reporting a
228                          * bus busy.
229                          */
230                         if ((sts_entry->iscsiFlags &
231                              ISCSI_FLAG_RESIDUAL_UNDER) == 0) {
232                                 cmd->result = DID_BUS_BUSY << 16;
233                         } else if ((scsi_bufflen(cmd) - residual) <
234                                    cmd->underflow) {
235                                 /*
236                                  * Handle mid-layer underflow???
237                                  *
238                                  * For kernels less than 2.4, the driver must
239                                  * return an error if an underflow is detected.
240                                  * For kernels equal-to and above 2.4, the
241                                  * mid-layer will appearantly handle the
242                                  * underflow by detecting the residual count --
243                                  * unfortunately, we do not see where this is
244                                  * actually being done.  In the interim, we
245                                  * will return DID_ERROR.
246                                  */
247                                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: "
248                                               "Mid-layer Data underrun, "
249                                               "xferlen = 0x%x, "
250                                               "residual = 0x%x\n", ha->host_no,
251                                               cmd->device->channel,
252                                               cmd->device->id,
253                                               cmd->device->lun, __func__,
254                                               scsi_bufflen(cmd), residual));
255
256                                 cmd->result = DID_ERROR << 16;
257                         } else {
258                                 cmd->result = DID_OK << 16;
259                         }
260                 }
261                 break;
262
263         case SCS_DEVICE_LOGGED_OUT:
264         case SCS_DEVICE_UNAVAILABLE:
265                 /*
266                  * Mark device missing so that we won't continue to
267                  * send I/O to this device.  We should get a ddb
268                  * state change AEN soon.
269                  */
270                 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
271                         qla4xxx_mark_device_missing(ha, ddb_entry);
272
273                 cmd->result = DID_BUS_BUSY << 16;
274                 break;
275
276         case SCS_QUEUE_FULL:
277                 /*
278                  * SCSI Mid-Layer handles device queue full
279                  */
280                 cmd->result = DID_OK << 16 | sts_entry->scsiStatus;
281                 DEBUG2(printk("scsi%ld:%d:%d: %s: QUEUE FULL detected "
282                               "compl=%02x, scsi=%02x, state=%02x, iFlags=%02x,"
283                               " iResp=%02x\n", ha->host_no, cmd->device->id,
284                               cmd->device->lun, __func__,
285                               sts_entry->completionStatus,
286                               sts_entry->scsiStatus, sts_entry->state_flags,
287                               sts_entry->iscsiFlags,
288                               sts_entry->iscsiResponse));
289                 break;
290
291         default:
292                 cmd->result = DID_ERROR << 16;
293                 break;
294         }
295
296 status_entry_exit:
297
298         /* complete the request */
299         srb->cc_stat = sts_entry->completionStatus;
300         qla4xxx_srb_compl(ha, srb);
301 }
302
303 /**
304  * qla4xxx_process_response_queue - process response queue completions
305  * @ha: Pointer to host adapter structure.
306  *
307  * This routine process response queue completions in interrupt context.
308  * Hardware_lock locked upon entry
309  **/
310 static void qla4xxx_process_response_queue(struct scsi_qla_host * ha)
311 {
312         uint32_t count = 0;
313         struct srb *srb = NULL;
314         struct status_entry *sts_entry;
315
316         /* Process all responses from response queue */
317         while ((ha->response_in =
318                 (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in)) !=
319                ha->response_out) {
320                 sts_entry = (struct status_entry *) ha->response_ptr;
321                 count++;
322
323                 /* Advance pointers for next entry */
324                 if (ha->response_out == (RESPONSE_QUEUE_DEPTH - 1)) {
325                         ha->response_out = 0;
326                         ha->response_ptr = ha->response_ring;
327                 } else {
328                         ha->response_out++;
329                         ha->response_ptr++;
330                 }
331
332                 /* process entry */
333                 switch (sts_entry->hdr.entryType) {
334                 case ET_STATUS:
335                         /*
336                          * Common status - Single completion posted in single
337                          * IOSB.
338                          */
339                         qla4xxx_status_entry(ha, sts_entry);
340                         break;
341
342                 case ET_PASSTHRU_STATUS:
343                         break;
344
345                 case ET_STATUS_CONTINUATION:
346                         /* Just throw away the status continuation entries */
347                         DEBUG2(printk("scsi%ld: %s: Status Continuation entry "
348                                       "- ignoring\n", ha->host_no, __func__));
349                         break;
350
351                 case ET_COMMAND:
352                         /* ISP device queue is full. Command not
353                          * accepted by ISP.  Queue command for
354                          * later */
355
356                         srb = qla4xxx_del_from_active_array(ha,
357                                                     le32_to_cpu(sts_entry->
358                                                                 handle));
359                         if (srb == NULL)
360                                 goto exit_prq_invalid_handle;
361
362                         DEBUG2(printk("scsi%ld: %s: FW device queue full, "
363                                       "srb %p\n", ha->host_no, __func__, srb));
364
365                         /* ETRY normally by sending it back with
366                          * DID_BUS_BUSY */
367                         srb->cmd->result = DID_BUS_BUSY << 16;
368                         qla4xxx_srb_compl(ha, srb);
369                         break;
370
371                 case ET_CONTINUE:
372                         /* Just throw away the continuation entries */
373                         DEBUG2(printk("scsi%ld: %s: Continuation entry - "
374                                       "ignoring\n", ha->host_no, __func__));
375                         break;
376
377                 default:
378                         /*
379                          * Invalid entry in response queue, reset RISC
380                          * firmware.
381                          */
382                         DEBUG2(printk("scsi%ld: %s: Invalid entry %x in "
383                                       "response queue \n", ha->host_no,
384                                       __func__,
385                                       sts_entry->hdr.entryType));
386                         goto exit_prq_error;
387                 }
388         }
389
390         /*
391          * Done with responses, update the ISP For QLA4010, this also clears
392          * the interrupt.
393          */
394         writel(ha->response_out, &ha->reg->rsp_q_out);
395         readl(&ha->reg->rsp_q_out);
396
397         return;
398
399 exit_prq_invalid_handle:
400         DEBUG2(printk("scsi%ld: %s: Invalid handle(srb)=%p type=%x IOCS=%x\n",
401                       ha->host_no, __func__, srb, sts_entry->hdr.entryType,
402                       sts_entry->completionStatus));
403
404 exit_prq_error:
405         writel(ha->response_out, &ha->reg->rsp_q_out);
406         readl(&ha->reg->rsp_q_out);
407
408         set_bit(DPC_RESET_HA, &ha->dpc_flags);
409 }
410
411 /**
412  * qla4xxx_isr_decode_mailbox - decodes mailbox status
413  * @ha: Pointer to host adapter structure.
414  * @mailbox_status: Mailbox status.
415  *
416  * This routine decodes the mailbox status during the ISR.
417  * Hardware_lock locked upon entry. runs in interrupt context.
418  **/
419 static void qla4xxx_isr_decode_mailbox(struct scsi_qla_host * ha,
420                                        uint32_t mbox_status)
421 {
422         int i;
423         uint32_t mbox_stat2, mbox_stat3;
424
425         if ((mbox_status == MBOX_STS_BUSY) ||
426             (mbox_status == MBOX_STS_INTERMEDIATE_COMPLETION) ||
427             (mbox_status >> 12 == MBOX_COMPLETION_STATUS)) {
428                 ha->mbox_status[0] = mbox_status;
429
430                 if (test_bit(AF_MBOX_COMMAND, &ha->flags)) {
431                         /*
432                          * Copy all mailbox registers to a temporary
433                          * location and set mailbox command done flag
434                          */
435                         for (i = 1; i < ha->mbox_status_count; i++)
436                                 ha->mbox_status[i] =
437                                         readl(&ha->reg->mailbox[i]);
438
439                         set_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
440                 }
441         } else if (mbox_status >> 12 == MBOX_ASYNC_EVENT_STATUS) {
442                 /* Immediately process the AENs that don't require much work.
443                  * Only queue the database_changed AENs */
444                 if (ha->aen_log.count < MAX_AEN_ENTRIES) {
445                         for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
446                                 ha->aen_log.entry[ha->aen_log.count].mbox_sts[i] =
447                                         readl(&ha->reg->mailbox[i]);
448                         ha->aen_log.count++;
449                 }
450                 switch (mbox_status) {
451                 case MBOX_ASTS_SYSTEM_ERROR:
452                         /* Log Mailbox registers */
453                         if (ql4xdontresethba) {
454                                 DEBUG2(printk("%s:Dont Reset HBA\n",
455                                               __func__));
456                         } else {
457                                 set_bit(AF_GET_CRASH_RECORD, &ha->flags);
458                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
459                         }
460                         break;
461
462                 case MBOX_ASTS_REQUEST_TRANSFER_ERROR:
463                 case MBOX_ASTS_RESPONSE_TRANSFER_ERROR:
464                 case MBOX_ASTS_NVRAM_INVALID:
465                 case MBOX_ASTS_IP_ADDRESS_CHANGED:
466                 case MBOX_ASTS_DHCP_LEASE_EXPIRED:
467                         DEBUG2(printk("scsi%ld: AEN %04x, ERROR Status, "
468                                       "Reset HA\n", ha->host_no, mbox_status));
469                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
470                         break;
471
472                 case MBOX_ASTS_LINK_UP:
473                         DEBUG2(printk("scsi%ld: AEN %04x Adapter LINK UP\n",
474                                       ha->host_no, mbox_status));
475                         set_bit(AF_LINK_UP, &ha->flags);
476                         break;
477
478                 case MBOX_ASTS_LINK_DOWN:
479                         DEBUG2(printk("scsi%ld: AEN %04x Adapter LINK DOWN\n",
480                                       ha->host_no, mbox_status));
481                         clear_bit(AF_LINK_UP, &ha->flags);
482                         break;
483
484                 case MBOX_ASTS_HEARTBEAT:
485                         ha->seconds_since_last_heartbeat = 0;
486                         break;
487
488                 case MBOX_ASTS_DHCP_LEASE_ACQUIRED:
489                         DEBUG2(printk("scsi%ld: AEN %04x DHCP LEASE "
490                                       "ACQUIRED\n", ha->host_no, mbox_status));
491                         set_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags);
492                         break;
493
494                 case MBOX_ASTS_PROTOCOL_STATISTIC_ALARM:
495                 case MBOX_ASTS_SCSI_COMMAND_PDU_REJECTED: /* Target
496                                                            * mode
497                                                            * only */
498                 case MBOX_ASTS_UNSOLICITED_PDU_RECEIVED:  /* Connection mode */
499                 case MBOX_ASTS_IPSEC_SYSTEM_FATAL_ERROR:
500                 case MBOX_ASTS_SUBNET_STATE_CHANGE:
501                         /* No action */
502                         DEBUG2(printk("scsi%ld: AEN %04x\n", ha->host_no,
503                                       mbox_status));
504                         break;
505
506                 case MBOX_ASTS_IP_ADDR_STATE_CHANGED:
507                         mbox_stat2 = readl(&ha->reg->mailbox[2]);
508                         mbox_stat3 = readl(&ha->reg->mailbox[3]);
509
510                         if ((mbox_stat3 == 5) && (mbox_stat2 == 3))
511                                 set_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags);
512                         else if ((mbox_stat3 == 2) && (mbox_stat2 == 5))
513                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
514                         break;
515
516                 case MBOX_ASTS_MAC_ADDRESS_CHANGED:
517                 case MBOX_ASTS_DNS:
518                         /* No action */
519                         DEBUG2(printk(KERN_INFO "scsi%ld: AEN %04x, "
520                                       "mbox_sts[1]=%04x, mbox_sts[2]=%04x\n",
521                                       ha->host_no, mbox_status,
522                                       readl(&ha->reg->mailbox[1]),
523                                       readl(&ha->reg->mailbox[2])));
524                         break;
525
526                 case MBOX_ASTS_SELF_TEST_FAILED:
527                 case MBOX_ASTS_LOGIN_FAILED:
528                         /* No action */
529                         DEBUG2(printk("scsi%ld: AEN %04x, mbox_sts[1]=%04x, "
530                                       "mbox_sts[2]=%04x, mbox_sts[3]=%04x\n",
531                                       ha->host_no, mbox_status,
532                                       readl(&ha->reg->mailbox[1]),
533                                       readl(&ha->reg->mailbox[2]),
534                                       readl(&ha->reg->mailbox[3])));
535                         break;
536
537                 case MBOX_ASTS_DATABASE_CHANGED:
538                         /* Queue AEN information and process it in the DPC
539                          * routine */
540                         if (ha->aen_q_count > 0) {
541
542                                 /* decrement available counter */
543                                 ha->aen_q_count--;
544
545                                 for (i = 1; i < MBOX_AEN_REG_COUNT; i++)
546                                         ha->aen_q[ha->aen_in].mbox_sts[i] =
547                                                 readl(&ha->reg->mailbox[i]);
548
549                                 ha->aen_q[ha->aen_in].mbox_sts[0] = mbox_status;
550
551                                 /* print debug message */
552                                 DEBUG2(printk("scsi%ld: AEN[%d] %04x queued"
553                                               " mb1:0x%x mb2:0x%x mb3:0x%x mb4:0x%x\n",
554                                               ha->host_no, ha->aen_in,
555                                               mbox_status,
556                                               ha->aen_q[ha->aen_in].mbox_sts[1],
557                                               ha->aen_q[ha->aen_in].mbox_sts[2],
558                                               ha->aen_q[ha->aen_in].mbox_sts[3],
559                                               ha->aen_q[ha->aen_in].  mbox_sts[4]));
560                                 /* advance pointer */
561                                 ha->aen_in++;
562                                 if (ha->aen_in == MAX_AEN_ENTRIES)
563                                         ha->aen_in = 0;
564
565                                 /* The DPC routine will process the aen */
566                                 set_bit(DPC_AEN, &ha->dpc_flags);
567                         } else {
568                                 DEBUG2(printk("scsi%ld: %s: aen %04x, queue "
569                                               "overflowed!  AEN LOST!!\n",
570                                               ha->host_no, __func__,
571                                               mbox_status));
572
573                                 DEBUG2(printk("scsi%ld: DUMP AEN QUEUE\n",
574                                               ha->host_no));
575
576                                 for (i = 0; i < MAX_AEN_ENTRIES; i++) {
577                                         DEBUG2(printk("AEN[%d] %04x %04x %04x "
578                                                       "%04x\n", i,
579                                                       ha->aen_q[i].mbox_sts[0],
580                                                       ha->aen_q[i].mbox_sts[1],
581                                                       ha->aen_q[i].mbox_sts[2],
582                                                       ha->aen_q[i].mbox_sts[3]));
583                                 }
584                         }
585                         break;
586
587                 default:
588                         DEBUG2(printk(KERN_WARNING
589                                       "scsi%ld: AEN %04x UNKNOWN\n",
590                                       ha->host_no, mbox_status));
591                         break;
592                 }
593         } else {
594                 DEBUG2(printk("scsi%ld: Unknown mailbox status %08X\n",
595                               ha->host_no, mbox_status));
596
597                 ha->mbox_status[0] = mbox_status;
598         }
599 }
600
601 /**
602  * qla4xxx_interrupt_service_routine - isr
603  * @ha: pointer to host adapter structure.
604  *
605  * This is the main interrupt service routine.
606  * hardware_lock locked upon entry. runs in interrupt context.
607  **/
608 void qla4xxx_interrupt_service_routine(struct scsi_qla_host * ha,
609                                        uint32_t intr_status)
610 {
611         /* Process response queue interrupt. */
612         if (intr_status & CSR_SCSI_COMPLETION_INTR)
613                 qla4xxx_process_response_queue(ha);
614
615         /* Process mailbox/asynch event  interrupt.*/
616         if (intr_status & CSR_SCSI_PROCESSOR_INTR) {
617                 qla4xxx_isr_decode_mailbox(ha,
618                                            readl(&ha->reg->mailbox[0]));
619
620                 /* Clear Mailbox Interrupt */
621                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
622                        &ha->reg->ctrl_status);
623                 readl(&ha->reg->ctrl_status);
624         }
625 }
626
627 /**
628  * qla4xxx_intr_handler - hardware interrupt handler.
629  * @irq: Unused
630  * @dev_id: Pointer to host adapter structure
631  **/
632 irqreturn_t qla4xxx_intr_handler(int irq, void *dev_id)
633 {
634         struct scsi_qla_host *ha;
635         uint32_t intr_status;
636         unsigned long flags = 0;
637         uint8_t reqs_count = 0;
638
639         ha = (struct scsi_qla_host *) dev_id;
640         if (!ha) {
641                 DEBUG2(printk(KERN_INFO
642                               "qla4xxx: Interrupt with NULL host ptr\n"));
643                 return IRQ_NONE;
644         }
645
646         spin_lock_irqsave(&ha->hardware_lock, flags);
647
648         ha->isr_count++;
649         /*
650          * Repeatedly service interrupts up to a maximum of
651          * MAX_REQS_SERVICED_PER_INTR
652          */
653         while (1) {
654                 /*
655                  * Read interrupt status
656                  */
657                 if (le32_to_cpu(ha->shadow_regs->rsp_q_in) !=
658                     ha->response_out)
659                         intr_status = CSR_SCSI_COMPLETION_INTR;
660                 else
661                         intr_status = readl(&ha->reg->ctrl_status);
662
663                 if ((intr_status &
664                      (CSR_SCSI_RESET_INTR|CSR_FATAL_ERROR|INTR_PENDING)) ==
665                     0) {
666                         if (reqs_count == 0)
667                                 ha->spurious_int_count++;
668                         break;
669                 }
670
671                 if (intr_status & CSR_FATAL_ERROR) {
672                         DEBUG2(printk(KERN_INFO "scsi%ld: Fatal Error, "
673                                       "Status 0x%04x\n", ha->host_no,
674                                       readl(isp_port_error_status (ha))));
675
676                         /* Issue Soft Reset to clear this error condition.
677                          * This will prevent the RISC from repeatedly
678                          * interrupting the driver; thus, allowing the DPC to
679                          * get scheduled to continue error recovery.
680                          * NOTE: Disabling RISC interrupts does not work in
681                          * this case, as CSR_FATAL_ERROR overrides
682                          * CSR_SCSI_INTR_ENABLE */
683                         if ((readl(&ha->reg->ctrl_status) &
684                              CSR_SCSI_RESET_INTR) == 0) {
685                                 writel(set_rmask(CSR_SOFT_RESET),
686                                        &ha->reg->ctrl_status);
687                                 readl(&ha->reg->ctrl_status);
688                         }
689
690                         writel(set_rmask(CSR_FATAL_ERROR),
691                                &ha->reg->ctrl_status);
692                         readl(&ha->reg->ctrl_status);
693
694                         __qla4xxx_disable_intrs(ha);
695
696                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
697
698                         break;
699                 } else if (intr_status & CSR_SCSI_RESET_INTR) {
700                         clear_bit(AF_ONLINE, &ha->flags);
701                         __qla4xxx_disable_intrs(ha);
702
703                         writel(set_rmask(CSR_SCSI_RESET_INTR),
704                                &ha->reg->ctrl_status);
705                         readl(&ha->reg->ctrl_status);
706
707                         if (!ql4_mod_unload)
708                                 set_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
709
710                         break;
711                 } else if (intr_status & INTR_PENDING) {
712                         qla4xxx_interrupt_service_routine(ha, intr_status);
713                         ha->total_io_count++;
714                         if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
715                                 break;
716
717                         intr_status = 0;
718                 }
719         }
720
721         spin_unlock_irqrestore(&ha->hardware_lock, flags);
722
723         return IRQ_HANDLED;
724 }
725
726 /**
727  * qla4xxx_process_aen - processes AENs generated by firmware
728  * @ha: pointer to host adapter structure.
729  * @process_aen: type of AENs to process
730  *
731  * Processes specific types of Asynchronous Events generated by firmware.
732  * The type of AENs to process is specified by process_aen and can be
733  *      PROCESS_ALL_AENS         0
734  *      FLUSH_DDB_CHANGED_AENS   1
735  *      RELOGIN_DDB_CHANGED_AENS 2
736  **/
737 void qla4xxx_process_aen(struct scsi_qla_host * ha, uint8_t process_aen)
738 {
739         uint32_t mbox_sts[MBOX_AEN_REG_COUNT];
740         struct aen *aen;
741         int i;
742         unsigned long flags;
743
744         spin_lock_irqsave(&ha->hardware_lock, flags);
745         while (ha->aen_out != ha->aen_in) {
746                 aen = &ha->aen_q[ha->aen_out];
747                 /* copy aen information to local structure */
748                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
749                         mbox_sts[i] = aen->mbox_sts[i];
750
751                 ha->aen_q_count++;
752                 ha->aen_out++;
753
754                 if (ha->aen_out == MAX_AEN_ENTRIES)
755                         ha->aen_out = 0;
756
757                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
758
759                 DEBUG2(printk("qla4xxx(%ld): AEN[%d]=0x%08x, mbx1=0x%08x mbx2=0x%08x"
760                         " mbx3=0x%08x mbx4=0x%08x\n", ha->host_no,
761                         (ha->aen_out ? (ha->aen_out-1): (MAX_AEN_ENTRIES-1)),
762                         mbox_sts[0], mbox_sts[1], mbox_sts[2],
763                         mbox_sts[3], mbox_sts[4]));
764
765                 switch (mbox_sts[0]) {
766                 case MBOX_ASTS_DATABASE_CHANGED:
767                         if (process_aen == FLUSH_DDB_CHANGED_AENS) {
768                                 DEBUG2(printk("scsi%ld: AEN[%d] %04x, index "
769                                               "[%d] state=%04x FLUSHED!\n",
770                                               ha->host_no, ha->aen_out,
771                                               mbox_sts[0], mbox_sts[2],
772                                               mbox_sts[3]));
773                                 break;
774                         } else if (process_aen == RELOGIN_DDB_CHANGED_AENS) {
775                                 /* for use during init time, we only want to
776                                  * relogin non-active ddbs */
777                                 struct ddb_entry *ddb_entry;
778
779                                 ddb_entry =
780                                         /* FIXME: name length? */
781                                         qla4xxx_lookup_ddb_by_fw_index(ha,
782                                                                        mbox_sts[2]);
783                                 if (!ddb_entry)
784                                         break;
785
786                                 ddb_entry->dev_scan_wait_to_complete_relogin =
787                                         0;
788                                 ddb_entry->dev_scan_wait_to_start_relogin =
789                                         jiffies +
790                                         ((ddb_entry->default_time2wait +
791                                           4) * HZ);
792
793                                 DEBUG2(printk("scsi%ld: ddb index [%d] initate"
794                                               " RELOGIN after %d seconds\n",
795                                               ha->host_no,
796                                               ddb_entry->fw_ddb_index,
797                                               ddb_entry->default_time2wait +
798                                               4));
799                                 break;
800                         }
801
802                         if (mbox_sts[1] == 0) { /* Global DB change. */
803                                 qla4xxx_reinitialize_ddb_list(ha);
804                         } else if (mbox_sts[1] == 1) {  /* Specific device. */
805                                 qla4xxx_process_ddb_changed(ha, mbox_sts[2],
806                                                             mbox_sts[3]);
807                         }
808                         break;
809                 }
810                 spin_lock_irqsave(&ha->hardware_lock, flags);
811         }
812         spin_unlock_irqrestore(&ha->hardware_lock, flags);
813 }
814