Merge tag 'iommu-fixes-v3.3-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / scsi / qla4xxx / ql4_isr.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2010 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  * qla4xxx_copy_sense - copy sense data into cmd sense buffer
15  * @ha: Pointer to host adapter structure.
16  * @sts_entry: Pointer to status entry structure.
17  * @srb: Pointer to srb structure.
18  **/
19 static void qla4xxx_copy_sense(struct scsi_qla_host *ha,
20                                struct status_entry *sts_entry,
21                                struct srb *srb)
22 {
23         struct scsi_cmnd *cmd = srb->cmd;
24         uint16_t sense_len;
25
26         memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
27         sense_len = le16_to_cpu(sts_entry->senseDataByteCnt);
28         if (sense_len == 0) {
29                 DEBUG2(ql4_printk(KERN_INFO, ha, "scsi%ld:%d:%d:%d: %s:"
30                                   " sense len 0\n", ha->host_no,
31                                   cmd->device->channel, cmd->device->id,
32                                   cmd->device->lun, __func__));
33                 ha->status_srb = NULL;
34                 return;
35         }
36         /* Save total available sense length,
37          * not to exceed cmd's sense buffer size */
38         sense_len = min_t(uint16_t, sense_len, SCSI_SENSE_BUFFERSIZE);
39         srb->req_sense_ptr = cmd->sense_buffer;
40         srb->req_sense_len = sense_len;
41
42         /* Copy sense from sts_entry pkt */
43         sense_len = min_t(uint16_t, sense_len, IOCB_MAX_SENSEDATA_LEN);
44         memcpy(cmd->sense_buffer, sts_entry->senseData, sense_len);
45
46         DEBUG2(printk(KERN_INFO "scsi%ld:%d:%d:%d: %s: sense key = %x, "
47                 "ASL= %02x, ASC/ASCQ = %02x/%02x\n", ha->host_no,
48                 cmd->device->channel, cmd->device->id,
49                 cmd->device->lun, __func__,
50                 sts_entry->senseData[2] & 0x0f,
51                 sts_entry->senseData[7],
52                 sts_entry->senseData[12],
53                 sts_entry->senseData[13]));
54
55         DEBUG5(qla4xxx_dump_buffer(cmd->sense_buffer, sense_len));
56         srb->flags |= SRB_GOT_SENSE;
57
58         /* Update srb, in case a sts_cont pkt follows */
59         srb->req_sense_ptr += sense_len;
60         srb->req_sense_len -= sense_len;
61         if (srb->req_sense_len != 0)
62                 ha->status_srb = srb;
63         else
64                 ha->status_srb = NULL;
65 }
66
67 /**
68  * qla4xxx_status_cont_entry - Process a Status Continuations entry.
69  * @ha: SCSI driver HA context
70  * @sts_cont: Entry pointer
71  *
72  * Extended sense data.
73  */
74 static void
75 qla4xxx_status_cont_entry(struct scsi_qla_host *ha,
76                           struct status_cont_entry *sts_cont)
77 {
78         struct srb *srb = ha->status_srb;
79         struct scsi_cmnd *cmd;
80         uint16_t sense_len;
81
82         if (srb == NULL)
83                 return;
84
85         cmd = srb->cmd;
86         if (cmd == NULL) {
87                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Cmd already returned "
88                         "back to OS srb=%p srb->state:%d\n", ha->host_no,
89                         __func__, srb, srb->state));
90                 ha->status_srb = NULL;
91                 return;
92         }
93
94         /* Copy sense data. */
95         sense_len = min_t(uint16_t, srb->req_sense_len,
96                           IOCB_MAX_EXT_SENSEDATA_LEN);
97         memcpy(srb->req_sense_ptr, sts_cont->ext_sense_data, sense_len);
98         DEBUG5(qla4xxx_dump_buffer(srb->req_sense_ptr, sense_len));
99
100         srb->req_sense_ptr += sense_len;
101         srb->req_sense_len -= sense_len;
102
103         /* Place command on done queue. */
104         if (srb->req_sense_len == 0) {
105                 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
106                 ha->status_srb = NULL;
107         }
108 }
109
110 /**
111  * qla4xxx_status_entry - processes status IOCBs
112  * @ha: Pointer to host adapter structure.
113  * @sts_entry: Pointer to status entry structure.
114  **/
115 static void qla4xxx_status_entry(struct scsi_qla_host *ha,
116                                  struct status_entry *sts_entry)
117 {
118         uint8_t scsi_status;
119         struct scsi_cmnd *cmd;
120         struct srb *srb;
121         struct ddb_entry *ddb_entry;
122         uint32_t residual;
123
124         srb = qla4xxx_del_from_active_array(ha, le32_to_cpu(sts_entry->handle));
125         if (!srb) {
126                 ql4_printk(KERN_WARNING, ha, "%s invalid status entry: "
127                            "handle=0x%0x, srb=%p\n", __func__,
128                            sts_entry->handle, srb);
129                 if (is_qla8022(ha))
130                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
131                 else
132                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
133                 return;
134         }
135
136         cmd = srb->cmd;
137         if (cmd == NULL) {
138                 DEBUG2(printk("scsi%ld: %s: Command already returned back to "
139                               "OS pkt->handle=%d srb=%p srb->state:%d\n",
140                               ha->host_no, __func__, sts_entry->handle,
141                               srb, srb->state));
142                 ql4_printk(KERN_WARNING, ha, "Command is NULL:"
143                     " already returned to OS (srb=%p)\n", srb);
144                 return;
145         }
146
147         ddb_entry = srb->ddb;
148         if (ddb_entry == NULL) {
149                 cmd->result = DID_NO_CONNECT << 16;
150                 goto status_entry_exit;
151         }
152
153         residual = le32_to_cpu(sts_entry->residualByteCnt);
154
155         /* Translate ISP error to a Linux SCSI error. */
156         scsi_status = sts_entry->scsiStatus;
157         switch (sts_entry->completionStatus) {
158         case SCS_COMPLETE:
159
160                 if (sts_entry->iscsiFlags & ISCSI_FLAG_RESIDUAL_OVER) {
161                         cmd->result = DID_ERROR << 16;
162                         break;
163                 }
164
165                 if (sts_entry->iscsiFlags &ISCSI_FLAG_RESIDUAL_UNDER) {
166                         scsi_set_resid(cmd, residual);
167                         if (!scsi_status && ((scsi_bufflen(cmd) - residual) <
168                                 cmd->underflow)) {
169
170                                 cmd->result = DID_ERROR << 16;
171
172                                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: "
173                                         "Mid-layer Data underrun0, "
174                                         "xferlen = 0x%x, "
175                                         "residual = 0x%x\n", ha->host_no,
176                                         cmd->device->channel,
177                                         cmd->device->id,
178                                         cmd->device->lun, __func__,
179                                         scsi_bufflen(cmd), residual));
180                                 break;
181                         }
182                 }
183
184                 cmd->result = DID_OK << 16 | scsi_status;
185
186                 if (scsi_status != SCSI_CHECK_CONDITION)
187                         break;
188
189                 /* Copy Sense Data into sense buffer. */
190                 qla4xxx_copy_sense(ha, sts_entry, srb);
191                 break;
192
193         case SCS_INCOMPLETE:
194                 /* Always set the status to DID_ERROR, since
195                  * all conditions result in that status anyway */
196                 cmd->result = DID_ERROR << 16;
197                 break;
198
199         case SCS_RESET_OCCURRED:
200                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: Device RESET occurred\n",
201                               ha->host_no, cmd->device->channel,
202                               cmd->device->id, cmd->device->lun, __func__));
203
204                 cmd->result = DID_RESET << 16;
205                 break;
206
207         case SCS_ABORTED:
208                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: Abort occurred\n",
209                               ha->host_no, cmd->device->channel,
210                               cmd->device->id, cmd->device->lun, __func__));
211
212                 cmd->result = DID_RESET << 16;
213                 break;
214
215         case SCS_TIMEOUT:
216                 DEBUG2(printk(KERN_INFO "scsi%ld:%d:%d:%d: Timeout\n",
217                               ha->host_no, cmd->device->channel,
218                               cmd->device->id, cmd->device->lun));
219
220                 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
221
222                 /*
223                  * Mark device missing so that we won't continue to send
224                  * I/O to this device.  We should get a ddb state change
225                  * AEN soon.
226                  */
227                 if (iscsi_is_session_online(ddb_entry->sess))
228                         qla4xxx_mark_device_missing(ddb_entry->sess);
229                 break;
230
231         case SCS_DATA_UNDERRUN:
232         case SCS_DATA_OVERRUN:
233                 if ((sts_entry->iscsiFlags & ISCSI_FLAG_RESIDUAL_OVER) ||
234                      (sts_entry->completionStatus == SCS_DATA_OVERRUN)) {
235                         DEBUG2(printk("scsi%ld:%d:%d:%d: %s: " "Data overrun\n",
236                                       ha->host_no,
237                                       cmd->device->channel, cmd->device->id,
238                                       cmd->device->lun, __func__));
239
240                         cmd->result = DID_ERROR << 16;
241                         break;
242                 }
243
244                 scsi_set_resid(cmd, residual);
245
246                 /*
247                  * If there is scsi_status, it takes precedense over
248                  * underflow condition.
249                  */
250                 if (scsi_status != 0) {
251                         cmd->result = DID_OK << 16 | scsi_status;
252
253                         if (scsi_status != SCSI_CHECK_CONDITION)
254                                 break;
255
256                         /* Copy Sense Data into sense buffer. */
257                         qla4xxx_copy_sense(ha, sts_entry, srb);
258                 } else {
259                         /*
260                          * If RISC reports underrun and target does not
261                          * report it then we must have a lost frame, so
262                          * tell upper layer to retry it by reporting a
263                          * bus busy.
264                          */
265                         if ((sts_entry->iscsiFlags &
266                              ISCSI_FLAG_RESIDUAL_UNDER) == 0) {
267                                 cmd->result = DID_BUS_BUSY << 16;
268                         } else if ((scsi_bufflen(cmd) - residual) <
269                                    cmd->underflow) {
270                                 /*
271                                  * Handle mid-layer underflow???
272                                  *
273                                  * For kernels less than 2.4, the driver must
274                                  * return an error if an underflow is detected.
275                                  * For kernels equal-to and above 2.4, the
276                                  * mid-layer will appearantly handle the
277                                  * underflow by detecting the residual count --
278                                  * unfortunately, we do not see where this is
279                                  * actually being done.  In the interim, we
280                                  * will return DID_ERROR.
281                                  */
282                                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: "
283                                         "Mid-layer Data underrun1, "
284                                         "xferlen = 0x%x, "
285                                         "residual = 0x%x\n", ha->host_no,
286                                         cmd->device->channel,
287                                         cmd->device->id,
288                                         cmd->device->lun, __func__,
289                                         scsi_bufflen(cmd), residual));
290
291                                 cmd->result = DID_ERROR << 16;
292                         } else {
293                                 cmd->result = DID_OK << 16;
294                         }
295                 }
296                 break;
297
298         case SCS_DEVICE_LOGGED_OUT:
299         case SCS_DEVICE_UNAVAILABLE:
300                 DEBUG2(printk(KERN_INFO "scsi%ld:%d:%d:%d: SCS_DEVICE "
301                     "state: 0x%x\n", ha->host_no,
302                     cmd->device->channel, cmd->device->id,
303                     cmd->device->lun, sts_entry->completionStatus));
304                 /*
305                  * Mark device missing so that we won't continue to
306                  * send I/O to this device.  We should get a ddb
307                  * state change AEN soon.
308                  */
309                 if (iscsi_is_session_online(ddb_entry->sess))
310                         qla4xxx_mark_device_missing(ddb_entry->sess);
311
312                 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
313                 break;
314
315         case SCS_QUEUE_FULL:
316                 /*
317                  * SCSI Mid-Layer handles device queue full
318                  */
319                 cmd->result = DID_OK << 16 | sts_entry->scsiStatus;
320                 DEBUG2(printk("scsi%ld:%d:%d: %s: QUEUE FULL detected "
321                               "compl=%02x, scsi=%02x, state=%02x, iFlags=%02x,"
322                               " iResp=%02x\n", ha->host_no, cmd->device->id,
323                               cmd->device->lun, __func__,
324                               sts_entry->completionStatus,
325                               sts_entry->scsiStatus, sts_entry->state_flags,
326                               sts_entry->iscsiFlags,
327                               sts_entry->iscsiResponse));
328                 break;
329
330         default:
331                 cmd->result = DID_ERROR << 16;
332                 break;
333         }
334
335 status_entry_exit:
336
337         /* complete the request, if not waiting for status_continuation pkt */
338         srb->cc_stat = sts_entry->completionStatus;
339         if (ha->status_srb == NULL)
340                 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
341 }
342
343 /**
344  * qla4xxx_passthru_status_entry - processes passthru status IOCBs (0x3C)
345  * @ha: Pointer to host adapter structure.
346  * @sts_entry: Pointer to status entry structure.
347  **/
348 static void qla4xxx_passthru_status_entry(struct scsi_qla_host *ha,
349                                           struct passthru_status *sts_entry)
350 {
351         struct iscsi_task *task;
352         struct ddb_entry *ddb_entry;
353         struct ql4_task_data *task_data;
354         struct iscsi_cls_conn *cls_conn;
355         struct iscsi_conn *conn;
356         itt_t itt;
357         uint32_t fw_ddb_index;
358
359         itt = sts_entry->handle;
360         fw_ddb_index = le32_to_cpu(sts_entry->target);
361
362         ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
363
364         if (ddb_entry == NULL) {
365                 ql4_printk(KERN_ERR, ha, "%s: Invalid target index = 0x%x\n",
366                            __func__, sts_entry->target);
367                 return;
368         }
369
370         cls_conn = ddb_entry->conn;
371         conn = cls_conn->dd_data;
372         spin_lock(&conn->session->lock);
373         task = iscsi_itt_to_task(conn, itt);
374         spin_unlock(&conn->session->lock);
375
376         if (task == NULL) {
377                 ql4_printk(KERN_ERR, ha, "%s: Task is NULL\n", __func__);
378                 return;
379         }
380
381         task_data = task->dd_data;
382         memcpy(&task_data->sts, sts_entry, sizeof(struct passthru_status));
383         ha->req_q_count += task_data->iocb_req_cnt;
384         ha->iocb_cnt -= task_data->iocb_req_cnt;
385         queue_work(ha->task_wq, &task_data->task_work);
386 }
387
388 /**
389  * qla4xxx_process_response_queue - process response queue completions
390  * @ha: Pointer to host adapter structure.
391  *
392  * This routine process response queue completions in interrupt context.
393  * Hardware_lock locked upon entry
394  **/
395 void qla4xxx_process_response_queue(struct scsi_qla_host *ha)
396 {
397         uint32_t count = 0;
398         struct srb *srb = NULL;
399         struct status_entry *sts_entry;
400
401         /* Process all responses from response queue */
402         while ((ha->response_ptr->signature != RESPONSE_PROCESSED)) {
403                 sts_entry = (struct status_entry *) ha->response_ptr;
404                 count++;
405
406                 /* Advance pointers for next entry */
407                 if (ha->response_out == (RESPONSE_QUEUE_DEPTH - 1)) {
408                         ha->response_out = 0;
409                         ha->response_ptr = ha->response_ring;
410                 } else {
411                         ha->response_out++;
412                         ha->response_ptr++;
413                 }
414
415                 /* process entry */
416                 switch (sts_entry->hdr.entryType) {
417                 case ET_STATUS:
418                         /* Common status */
419                         qla4xxx_status_entry(ha, sts_entry);
420                         break;
421
422                 case ET_PASSTHRU_STATUS:
423                         if (sts_entry->hdr.systemDefined == SD_ISCSI_PDU)
424                                 qla4xxx_passthru_status_entry(ha,
425                                         (struct passthru_status *)sts_entry);
426                         else
427                                 ql4_printk(KERN_ERR, ha,
428                                            "%s: Invalid status received\n",
429                                            __func__);
430
431                         break;
432
433                 case ET_STATUS_CONTINUATION:
434                         qla4xxx_status_cont_entry(ha,
435                                 (struct status_cont_entry *) sts_entry);
436                         break;
437
438                 case ET_COMMAND:
439                         /* ISP device queue is full. Command not
440                          * accepted by ISP.  Queue command for
441                          * later */
442
443                         srb = qla4xxx_del_from_active_array(ha,
444                                                     le32_to_cpu(sts_entry->
445                                                                 handle));
446                         if (srb == NULL)
447                                 goto exit_prq_invalid_handle;
448
449                         DEBUG2(printk("scsi%ld: %s: FW device queue full, "
450                                       "srb %p\n", ha->host_no, __func__, srb));
451
452                         /* ETRY normally by sending it back with
453                          * DID_BUS_BUSY */
454                         srb->cmd->result = DID_BUS_BUSY << 16;
455                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
456                         break;
457
458                 case ET_CONTINUE:
459                         /* Just throw away the continuation entries */
460                         DEBUG2(printk("scsi%ld: %s: Continuation entry - "
461                                       "ignoring\n", ha->host_no, __func__));
462                         break;
463
464                 default:
465                         /*
466                          * Invalid entry in response queue, reset RISC
467                          * firmware.
468                          */
469                         DEBUG2(printk("scsi%ld: %s: Invalid entry %x in "
470                                       "response queue \n", ha->host_no,
471                                       __func__,
472                                       sts_entry->hdr.entryType));
473                         goto exit_prq_error;
474                 }
475                 ((struct response *)sts_entry)->signature = RESPONSE_PROCESSED;
476                 wmb();
477         }
478
479         /*
480          * Tell ISP we're done with response(s). This also clears the interrupt.
481          */
482         ha->isp_ops->complete_iocb(ha);
483
484         return;
485
486 exit_prq_invalid_handle:
487         DEBUG2(printk("scsi%ld: %s: Invalid handle(srb)=%p type=%x IOCS=%x\n",
488                       ha->host_no, __func__, srb, sts_entry->hdr.entryType,
489                       sts_entry->completionStatus));
490
491 exit_prq_error:
492         ha->isp_ops->complete_iocb(ha);
493         set_bit(DPC_RESET_HA, &ha->dpc_flags);
494 }
495
496 /**
497  * qla4xxx_isr_decode_mailbox - decodes mailbox status
498  * @ha: Pointer to host adapter structure.
499  * @mailbox_status: Mailbox status.
500  *
501  * This routine decodes the mailbox status during the ISR.
502  * Hardware_lock locked upon entry. runs in interrupt context.
503  **/
504 static void qla4xxx_isr_decode_mailbox(struct scsi_qla_host * ha,
505                                        uint32_t mbox_status)
506 {
507         int i;
508         uint32_t mbox_sts[MBOX_AEN_REG_COUNT];
509
510         if ((mbox_status == MBOX_STS_BUSY) ||
511             (mbox_status == MBOX_STS_INTERMEDIATE_COMPLETION) ||
512             (mbox_status >> 12 == MBOX_COMPLETION_STATUS)) {
513                 ha->mbox_status[0] = mbox_status;
514
515                 if (test_bit(AF_MBOX_COMMAND, &ha->flags)) {
516                         /*
517                          * Copy all mailbox registers to a temporary
518                          * location and set mailbox command done flag
519                          */
520                         for (i = 0; i < ha->mbox_status_count; i++)
521                                 ha->mbox_status[i] = is_qla8022(ha)
522                                     ? readl(&ha->qla4_8xxx_reg->mailbox_out[i])
523                                     : readl(&ha->reg->mailbox[i]);
524
525                         set_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
526
527                         if (test_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags))
528                                 complete(&ha->mbx_intr_comp);
529                 }
530         } else if (mbox_status >> 12 == MBOX_ASYNC_EVENT_STATUS) {
531                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
532                         mbox_sts[i] = is_qla8022(ha)
533                             ? readl(&ha->qla4_8xxx_reg->mailbox_out[i])
534                             : readl(&ha->reg->mailbox[i]);
535
536                 /* Immediately process the AENs that don't require much work.
537                  * Only queue the database_changed AENs */
538                 if (ha->aen_log.count < MAX_AEN_ENTRIES) {
539                         for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
540                                 ha->aen_log.entry[ha->aen_log.count].mbox_sts[i] =
541                                     mbox_sts[i];
542                         ha->aen_log.count++;
543                 }
544                 switch (mbox_status) {
545                 case MBOX_ASTS_SYSTEM_ERROR:
546                         /* Log Mailbox registers */
547                         ql4_printk(KERN_INFO, ha, "%s: System Err\n", __func__);
548                         qla4xxx_dump_registers(ha);
549
550                         if (ql4xdontresethba) {
551                                 DEBUG2(printk("scsi%ld: %s:Don't Reset HBA\n",
552                                     ha->host_no, __func__));
553                         } else {
554                                 set_bit(AF_GET_CRASH_RECORD, &ha->flags);
555                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
556                         }
557                         break;
558
559                 case MBOX_ASTS_REQUEST_TRANSFER_ERROR:
560                 case MBOX_ASTS_RESPONSE_TRANSFER_ERROR:
561                 case MBOX_ASTS_NVRAM_INVALID:
562                 case MBOX_ASTS_IP_ADDRESS_CHANGED:
563                 case MBOX_ASTS_DHCP_LEASE_EXPIRED:
564                         DEBUG2(printk("scsi%ld: AEN %04x, ERROR Status, "
565                                       "Reset HA\n", ha->host_no, mbox_status));
566                         if (is_qla8022(ha))
567                                 set_bit(DPC_RESET_HA_FW_CONTEXT,
568                                         &ha->dpc_flags);
569                         else
570                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
571                         break;
572
573                 case MBOX_ASTS_LINK_UP:
574                         set_bit(AF_LINK_UP, &ha->flags);
575                         if (test_bit(AF_INIT_DONE, &ha->flags))
576                                 set_bit(DPC_LINK_CHANGED, &ha->dpc_flags);
577
578                         ql4_printk(KERN_INFO, ha, "%s: LINK UP\n", __func__);
579                         break;
580
581                 case MBOX_ASTS_LINK_DOWN:
582                         clear_bit(AF_LINK_UP, &ha->flags);
583                         if (test_bit(AF_INIT_DONE, &ha->flags))
584                                 set_bit(DPC_LINK_CHANGED, &ha->dpc_flags);
585
586                         ql4_printk(KERN_INFO, ha, "%s: LINK DOWN\n", __func__);
587                         break;
588
589                 case MBOX_ASTS_HEARTBEAT:
590                         ha->seconds_since_last_heartbeat = 0;
591                         break;
592
593                 case MBOX_ASTS_DHCP_LEASE_ACQUIRED:
594                         DEBUG2(printk("scsi%ld: AEN %04x DHCP LEASE "
595                                       "ACQUIRED\n", ha->host_no, mbox_status));
596                         set_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags);
597                         break;
598
599                 case MBOX_ASTS_PROTOCOL_STATISTIC_ALARM:
600                 case MBOX_ASTS_SCSI_COMMAND_PDU_REJECTED: /* Target
601                                                            * mode
602                                                            * only */
603                 case MBOX_ASTS_UNSOLICITED_PDU_RECEIVED:  /* Connection mode */
604                 case MBOX_ASTS_IPSEC_SYSTEM_FATAL_ERROR:
605                 case MBOX_ASTS_SUBNET_STATE_CHANGE:
606                 case MBOX_ASTS_DUPLICATE_IP:
607                         /* No action */
608                         DEBUG2(printk("scsi%ld: AEN %04x\n", ha->host_no,
609                                       mbox_status));
610                         break;
611
612                 case MBOX_ASTS_IP_ADDR_STATE_CHANGED:
613                         printk("scsi%ld: AEN %04x, mbox_sts[2]=%04x, "
614                             "mbox_sts[3]=%04x\n", ha->host_no, mbox_sts[0],
615                             mbox_sts[2], mbox_sts[3]);
616
617                         /* mbox_sts[2] = Old ACB state
618                          * mbox_sts[3] = new ACB state */
619                         if ((mbox_sts[3] == ACB_STATE_VALID) &&
620                             ((mbox_sts[2] == ACB_STATE_TENTATIVE) ||
621                             (mbox_sts[2] == ACB_STATE_ACQUIRING)))
622                                 set_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags);
623                         else if ((mbox_sts[3] == ACB_STATE_ACQUIRING) &&
624                                  (mbox_sts[2] == ACB_STATE_VALID)) {
625                                 if (is_qla8022(ha))
626                                         set_bit(DPC_RESET_HA_FW_CONTEXT,
627                                                 &ha->dpc_flags);
628                                 else
629                                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
630                         } else if ((mbox_sts[3] == ACB_STATE_UNCONFIGURED))
631                                 complete(&ha->disable_acb_comp);
632                         break;
633
634                 case MBOX_ASTS_MAC_ADDRESS_CHANGED:
635                 case MBOX_ASTS_DNS:
636                         /* No action */
637                         DEBUG2(printk(KERN_INFO "scsi%ld: AEN %04x, "
638                                       "mbox_sts[1]=%04x, mbox_sts[2]=%04x\n",
639                                       ha->host_no, mbox_sts[0],
640                                       mbox_sts[1], mbox_sts[2]));
641                         break;
642
643                 case MBOX_ASTS_SELF_TEST_FAILED:
644                 case MBOX_ASTS_LOGIN_FAILED:
645                         /* No action */
646                         DEBUG2(printk("scsi%ld: AEN %04x, mbox_sts[1]=%04x, "
647                                       "mbox_sts[2]=%04x, mbox_sts[3]=%04x\n",
648                                       ha->host_no, mbox_sts[0], mbox_sts[1],
649                                       mbox_sts[2], mbox_sts[3]));
650                         break;
651
652                 case MBOX_ASTS_DATABASE_CHANGED:
653                         /* Queue AEN information and process it in the DPC
654                          * routine */
655                         if (ha->aen_q_count > 0) {
656
657                                 /* decrement available counter */
658                                 ha->aen_q_count--;
659
660                                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
661                                         ha->aen_q[ha->aen_in].mbox_sts[i] =
662                                             mbox_sts[i];
663
664                                 /* print debug message */
665                                 DEBUG2(printk("scsi%ld: AEN[%d] %04x queued "
666                                               "mb1:0x%x mb2:0x%x mb3:0x%x "
667                                               "mb4:0x%x mb5:0x%x\n",
668                                               ha->host_no, ha->aen_in,
669                                               mbox_sts[0], mbox_sts[1],
670                                               mbox_sts[2], mbox_sts[3],
671                                               mbox_sts[4], mbox_sts[5]));
672
673                                 /* advance pointer */
674                                 ha->aen_in++;
675                                 if (ha->aen_in == MAX_AEN_ENTRIES)
676                                         ha->aen_in = 0;
677
678                                 /* The DPC routine will process the aen */
679                                 set_bit(DPC_AEN, &ha->dpc_flags);
680                         } else {
681                                 DEBUG2(printk("scsi%ld: %s: aen %04x, queue "
682                                               "overflowed!  AEN LOST!!\n",
683                                               ha->host_no, __func__,
684                                               mbox_sts[0]));
685
686                                 DEBUG2(printk("scsi%ld: DUMP AEN QUEUE\n",
687                                               ha->host_no));
688
689                                 for (i = 0; i < MAX_AEN_ENTRIES; i++) {
690                                         DEBUG2(printk("AEN[%d] %04x %04x %04x "
691                                                       "%04x\n", i, mbox_sts[0],
692                                                       mbox_sts[1], mbox_sts[2],
693                                                       mbox_sts[3]));
694                                 }
695                         }
696                         break;
697
698                 case MBOX_ASTS_TXSCVR_INSERTED:
699                         DEBUG2(printk(KERN_WARNING
700                             "scsi%ld: AEN %04x Transceiver"
701                             " inserted\n",  ha->host_no, mbox_sts[0]));
702                         break;
703
704                 case MBOX_ASTS_TXSCVR_REMOVED:
705                         DEBUG2(printk(KERN_WARNING
706                             "scsi%ld: AEN %04x Transceiver"
707                             " removed\n",  ha->host_no, mbox_sts[0]));
708                         break;
709
710                 default:
711                         DEBUG2(printk(KERN_WARNING
712                                       "scsi%ld: AEN %04x UNKNOWN\n",
713                                       ha->host_no, mbox_sts[0]));
714                         break;
715                 }
716         } else {
717                 DEBUG2(printk("scsi%ld: Unknown mailbox status %08X\n",
718                               ha->host_no, mbox_status));
719
720                 ha->mbox_status[0] = mbox_status;
721         }
722 }
723
724 /**
725  * qla4_8xxx_interrupt_service_routine - isr
726  * @ha: pointer to host adapter structure.
727  *
728  * This is the main interrupt service routine.
729  * hardware_lock locked upon entry. runs in interrupt context.
730  **/
731 void qla4_8xxx_interrupt_service_routine(struct scsi_qla_host *ha,
732     uint32_t intr_status)
733 {
734         /* Process response queue interrupt. */
735         if (intr_status & HSRX_RISC_IOCB_INT)
736                 qla4xxx_process_response_queue(ha);
737
738         /* Process mailbox/asynch event interrupt.*/
739         if (intr_status & HSRX_RISC_MB_INT)
740                 qla4xxx_isr_decode_mailbox(ha,
741                     readl(&ha->qla4_8xxx_reg->mailbox_out[0]));
742
743         /* clear the interrupt */
744         writel(0, &ha->qla4_8xxx_reg->host_int);
745         readl(&ha->qla4_8xxx_reg->host_int);
746 }
747
748 /**
749  * qla4xxx_interrupt_service_routine - isr
750  * @ha: pointer to host adapter structure.
751  *
752  * This is the main interrupt service routine.
753  * hardware_lock locked upon entry. runs in interrupt context.
754  **/
755 void qla4xxx_interrupt_service_routine(struct scsi_qla_host * ha,
756                                        uint32_t intr_status)
757 {
758         /* Process response queue interrupt. */
759         if (intr_status & CSR_SCSI_COMPLETION_INTR)
760                 qla4xxx_process_response_queue(ha);
761
762         /* Process mailbox/asynch event  interrupt.*/
763         if (intr_status & CSR_SCSI_PROCESSOR_INTR) {
764                 qla4xxx_isr_decode_mailbox(ha,
765                                            readl(&ha->reg->mailbox[0]));
766
767                 /* Clear Mailbox Interrupt */
768                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
769                        &ha->reg->ctrl_status);
770                 readl(&ha->reg->ctrl_status);
771         }
772 }
773
774 /**
775  * qla4_8xxx_spurious_interrupt - processes spurious interrupt
776  * @ha: pointer to host adapter structure.
777  * @reqs_count: .
778  *
779  **/
780 static void qla4_8xxx_spurious_interrupt(struct scsi_qla_host *ha,
781     uint8_t reqs_count)
782 {
783         if (reqs_count)
784                 return;
785
786         DEBUG2(ql4_printk(KERN_INFO, ha, "Spurious Interrupt\n"));
787         if (is_qla8022(ha)) {
788                 writel(0, &ha->qla4_8xxx_reg->host_int);
789                 if (test_bit(AF_INTx_ENABLED, &ha->flags))
790                         qla4_8xxx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg,
791                             0xfbff);
792         }
793         ha->spurious_int_count++;
794 }
795
796 /**
797  * qla4xxx_intr_handler - hardware interrupt handler.
798  * @irq: Unused
799  * @dev_id: Pointer to host adapter structure
800  **/
801 irqreturn_t qla4xxx_intr_handler(int irq, void *dev_id)
802 {
803         struct scsi_qla_host *ha;
804         uint32_t intr_status;
805         unsigned long flags = 0;
806         uint8_t reqs_count = 0;
807
808         ha = (struct scsi_qla_host *) dev_id;
809         if (!ha) {
810                 DEBUG2(printk(KERN_INFO
811                               "qla4xxx: Interrupt with NULL host ptr\n"));
812                 return IRQ_NONE;
813         }
814
815         spin_lock_irqsave(&ha->hardware_lock, flags);
816
817         ha->isr_count++;
818         /*
819          * Repeatedly service interrupts up to a maximum of
820          * MAX_REQS_SERVICED_PER_INTR
821          */
822         while (1) {
823                 /*
824                  * Read interrupt status
825                  */
826                 if (ha->isp_ops->rd_shdw_rsp_q_in(ha) !=
827                     ha->response_out)
828                         intr_status = CSR_SCSI_COMPLETION_INTR;
829                 else
830                         intr_status = readl(&ha->reg->ctrl_status);
831
832                 if ((intr_status &
833                     (CSR_SCSI_RESET_INTR|CSR_FATAL_ERROR|INTR_PENDING)) == 0) {
834                         if (reqs_count == 0)
835                                 ha->spurious_int_count++;
836                         break;
837                 }
838
839                 if (intr_status & CSR_FATAL_ERROR) {
840                         DEBUG2(printk(KERN_INFO "scsi%ld: Fatal Error, "
841                                       "Status 0x%04x\n", ha->host_no,
842                                       readl(isp_port_error_status (ha))));
843
844                         /* Issue Soft Reset to clear this error condition.
845                          * This will prevent the RISC from repeatedly
846                          * interrupting the driver; thus, allowing the DPC to
847                          * get scheduled to continue error recovery.
848                          * NOTE: Disabling RISC interrupts does not work in
849                          * this case, as CSR_FATAL_ERROR overrides
850                          * CSR_SCSI_INTR_ENABLE */
851                         if ((readl(&ha->reg->ctrl_status) &
852                              CSR_SCSI_RESET_INTR) == 0) {
853                                 writel(set_rmask(CSR_SOFT_RESET),
854                                        &ha->reg->ctrl_status);
855                                 readl(&ha->reg->ctrl_status);
856                         }
857
858                         writel(set_rmask(CSR_FATAL_ERROR),
859                                &ha->reg->ctrl_status);
860                         readl(&ha->reg->ctrl_status);
861
862                         __qla4xxx_disable_intrs(ha);
863
864                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
865
866                         break;
867                 } else if (intr_status & CSR_SCSI_RESET_INTR) {
868                         clear_bit(AF_ONLINE, &ha->flags);
869                         __qla4xxx_disable_intrs(ha);
870
871                         writel(set_rmask(CSR_SCSI_RESET_INTR),
872                                &ha->reg->ctrl_status);
873                         readl(&ha->reg->ctrl_status);
874
875                         if (!test_bit(AF_HA_REMOVAL, &ha->flags))
876                                 set_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
877
878                         break;
879                 } else if (intr_status & INTR_PENDING) {
880                         ha->isp_ops->interrupt_service_routine(ha, intr_status);
881                         ha->total_io_count++;
882                         if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
883                                 break;
884                 }
885         }
886
887         spin_unlock_irqrestore(&ha->hardware_lock, flags);
888
889         return IRQ_HANDLED;
890 }
891
892 /**
893  * qla4_8xxx_intr_handler - hardware interrupt handler.
894  * @irq: Unused
895  * @dev_id: Pointer to host adapter structure
896  **/
897 irqreturn_t qla4_8xxx_intr_handler(int irq, void *dev_id)
898 {
899         struct scsi_qla_host *ha = dev_id;
900         uint32_t intr_status;
901         uint32_t status;
902         unsigned long flags = 0;
903         uint8_t reqs_count = 0;
904
905         if (unlikely(pci_channel_offline(ha->pdev)))
906                 return IRQ_HANDLED;
907
908         ha->isr_count++;
909         status = qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
910         if (!(status & ha->nx_legacy_intr.int_vec_bit))
911                 return IRQ_NONE;
912
913         status = qla4_8xxx_rd_32(ha, ISR_INT_STATE_REG);
914         if (!ISR_IS_LEGACY_INTR_TRIGGERED(status)) {
915                 DEBUG2(ql4_printk(KERN_INFO, ha,
916                     "%s legacy Int not triggered\n", __func__));
917                 return IRQ_NONE;
918         }
919
920         /* clear the interrupt */
921         qla4_8xxx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
922
923         /* read twice to ensure write is flushed */
924         qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
925         qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
926
927         spin_lock_irqsave(&ha->hardware_lock, flags);
928         while (1) {
929                 if (!(readl(&ha->qla4_8xxx_reg->host_int) &
930                     ISRX_82XX_RISC_INT)) {
931                         qla4_8xxx_spurious_interrupt(ha, reqs_count);
932                         break;
933                 }
934                 intr_status =  readl(&ha->qla4_8xxx_reg->host_status);
935                 if ((intr_status &
936                     (HSRX_RISC_MB_INT | HSRX_RISC_IOCB_INT)) == 0)  {
937                         qla4_8xxx_spurious_interrupt(ha, reqs_count);
938                         break;
939                 }
940
941                 ha->isp_ops->interrupt_service_routine(ha, intr_status);
942
943                 /* Enable Interrupt */
944                 qla4_8xxx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
945
946                 if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
947                         break;
948         }
949
950         spin_unlock_irqrestore(&ha->hardware_lock, flags);
951         return IRQ_HANDLED;
952 }
953
954 irqreturn_t
955 qla4_8xxx_msi_handler(int irq, void *dev_id)
956 {
957         struct scsi_qla_host *ha;
958
959         ha = (struct scsi_qla_host *) dev_id;
960         if (!ha) {
961                 DEBUG2(printk(KERN_INFO
962                     "qla4xxx: MSIX: Interrupt with NULL host ptr\n"));
963                 return IRQ_NONE;
964         }
965
966         ha->isr_count++;
967         /* clear the interrupt */
968         qla4_8xxx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
969
970         /* read twice to ensure write is flushed */
971         qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
972         qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
973
974         return qla4_8xxx_default_intr_handler(irq, dev_id);
975 }
976
977 /**
978  * qla4_8xxx_default_intr_handler - hardware interrupt handler.
979  * @irq: Unused
980  * @dev_id: Pointer to host adapter structure
981  *
982  * This interrupt handler is called directly for MSI-X, and
983  * called indirectly for MSI.
984  **/
985 irqreturn_t
986 qla4_8xxx_default_intr_handler(int irq, void *dev_id)
987 {
988         struct scsi_qla_host *ha = dev_id;
989         unsigned long   flags;
990         uint32_t intr_status;
991         uint8_t reqs_count = 0;
992
993         spin_lock_irqsave(&ha->hardware_lock, flags);
994         while (1) {
995                 if (!(readl(&ha->qla4_8xxx_reg->host_int) &
996                     ISRX_82XX_RISC_INT)) {
997                         qla4_8xxx_spurious_interrupt(ha, reqs_count);
998                         break;
999                 }
1000
1001                 intr_status =  readl(&ha->qla4_8xxx_reg->host_status);
1002                 if ((intr_status &
1003                     (HSRX_RISC_MB_INT | HSRX_RISC_IOCB_INT)) == 0) {
1004                         qla4_8xxx_spurious_interrupt(ha, reqs_count);
1005                         break;
1006                 }
1007
1008                 ha->isp_ops->interrupt_service_routine(ha, intr_status);
1009
1010                 if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
1011                         break;
1012         }
1013
1014         ha->isr_count++;
1015         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1016         return IRQ_HANDLED;
1017 }
1018
1019 irqreturn_t
1020 qla4_8xxx_msix_rsp_q(int irq, void *dev_id)
1021 {
1022         struct scsi_qla_host *ha = dev_id;
1023         unsigned long flags;
1024
1025         spin_lock_irqsave(&ha->hardware_lock, flags);
1026         qla4xxx_process_response_queue(ha);
1027         writel(0, &ha->qla4_8xxx_reg->host_int);
1028         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1029
1030         ha->isr_count++;
1031         return IRQ_HANDLED;
1032 }
1033
1034 /**
1035  * qla4xxx_process_aen - processes AENs generated by firmware
1036  * @ha: pointer to host adapter structure.
1037  * @process_aen: type of AENs to process
1038  *
1039  * Processes specific types of Asynchronous Events generated by firmware.
1040  * The type of AENs to process is specified by process_aen and can be
1041  *      PROCESS_ALL_AENS         0
1042  *      FLUSH_DDB_CHANGED_AENS   1
1043  *      RELOGIN_DDB_CHANGED_AENS 2
1044  **/
1045 void qla4xxx_process_aen(struct scsi_qla_host * ha, uint8_t process_aen)
1046 {
1047         uint32_t mbox_sts[MBOX_AEN_REG_COUNT];
1048         struct aen *aen;
1049         int i;
1050         unsigned long flags;
1051
1052         spin_lock_irqsave(&ha->hardware_lock, flags);
1053         while (ha->aen_out != ha->aen_in) {
1054                 aen = &ha->aen_q[ha->aen_out];
1055                 /* copy aen information to local structure */
1056                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
1057                         mbox_sts[i] = aen->mbox_sts[i];
1058
1059                 ha->aen_q_count++;
1060                 ha->aen_out++;
1061
1062                 if (ha->aen_out == MAX_AEN_ENTRIES)
1063                         ha->aen_out = 0;
1064
1065                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1066
1067                 DEBUG2(printk("qla4xxx(%ld): AEN[%d]=0x%08x, mbx1=0x%08x mbx2=0x%08x"
1068                         " mbx3=0x%08x mbx4=0x%08x\n", ha->host_no,
1069                         (ha->aen_out ? (ha->aen_out-1): (MAX_AEN_ENTRIES-1)),
1070                         mbox_sts[0], mbox_sts[1], mbox_sts[2],
1071                         mbox_sts[3], mbox_sts[4]));
1072
1073                 switch (mbox_sts[0]) {
1074                 case MBOX_ASTS_DATABASE_CHANGED:
1075                         switch (process_aen) {
1076                         case FLUSH_DDB_CHANGED_AENS:
1077                                 DEBUG2(printk("scsi%ld: AEN[%d] %04x, index "
1078                                               "[%d] state=%04x FLUSHED!\n",
1079                                               ha->host_no, ha->aen_out,
1080                                               mbox_sts[0], mbox_sts[2],
1081                                               mbox_sts[3]));
1082                                 break;
1083                         case PROCESS_ALL_AENS:
1084                         default:
1085                                 /* Specific device. */
1086                                 if (mbox_sts[1] == 1)
1087                                         qla4xxx_process_ddb_changed(ha,
1088                                                 mbox_sts[2], mbox_sts[3],
1089                                                 mbox_sts[4]);
1090                                 break;
1091                         }
1092                 }
1093                 spin_lock_irqsave(&ha->hardware_lock, flags);
1094         }
1095         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1096 }
1097
1098 int qla4xxx_request_irqs(struct scsi_qla_host *ha)
1099 {
1100         int ret;
1101
1102         if (!is_qla8022(ha))
1103                 goto try_intx;
1104
1105         if (ql4xenablemsix == 2)
1106                 goto try_msi;
1107
1108         if (ql4xenablemsix == 0 || ql4xenablemsix != 1)
1109                 goto try_intx;
1110
1111         /* Trying MSI-X */
1112         ret = qla4_8xxx_enable_msix(ha);
1113         if (!ret) {
1114                 DEBUG2(ql4_printk(KERN_INFO, ha,
1115                     "MSI-X: Enabled (0x%X).\n", ha->revision_id));
1116                 goto irq_attached;
1117         }
1118
1119         ql4_printk(KERN_WARNING, ha,
1120             "MSI-X: Falling back-to MSI mode -- %d.\n", ret);
1121
1122 try_msi:
1123         /* Trying MSI */
1124         ret = pci_enable_msi(ha->pdev);
1125         if (!ret) {
1126                 ret = request_irq(ha->pdev->irq, qla4_8xxx_msi_handler,
1127                         0, DRIVER_NAME, ha);
1128                 if (!ret) {
1129                         DEBUG2(ql4_printk(KERN_INFO, ha, "MSI: Enabled.\n"));
1130                         set_bit(AF_MSI_ENABLED, &ha->flags);
1131                         goto irq_attached;
1132                 } else {
1133                         ql4_printk(KERN_WARNING, ha,
1134                             "MSI: Failed to reserve interrupt %d "
1135                             "already in use.\n", ha->pdev->irq);
1136                         pci_disable_msi(ha->pdev);
1137                 }
1138         }
1139         ql4_printk(KERN_WARNING, ha,
1140             "MSI: Falling back-to INTx mode -- %d.\n", ret);
1141
1142 try_intx:
1143         /* Trying INTx */
1144         ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
1145             IRQF_SHARED, DRIVER_NAME, ha);
1146         if (!ret) {
1147                 DEBUG2(ql4_printk(KERN_INFO, ha, "INTx: Enabled.\n"));
1148                 set_bit(AF_INTx_ENABLED, &ha->flags);
1149                 goto irq_attached;
1150
1151         } else {
1152                 ql4_printk(KERN_WARNING, ha,
1153                     "INTx: Failed to reserve interrupt %d already in"
1154                     " use.\n", ha->pdev->irq);
1155                 return ret;
1156         }
1157
1158 irq_attached:
1159         set_bit(AF_IRQ_ATTACHED, &ha->flags);
1160         ha->host->irq = ha->pdev->irq;
1161         ql4_printk(KERN_INFO, ha, "%s: irq %d attached\n",
1162             __func__, ha->pdev->irq);
1163         return ret;
1164 }
1165
1166 void qla4xxx_free_irqs(struct scsi_qla_host *ha)
1167 {
1168         if (test_bit(AF_MSIX_ENABLED, &ha->flags))
1169                 qla4_8xxx_disable_msix(ha);
1170         else if (test_and_clear_bit(AF_MSI_ENABLED, &ha->flags)) {
1171                 free_irq(ha->pdev->irq, ha);
1172                 pci_disable_msi(ha->pdev);
1173         } else if (test_and_clear_bit(AF_INTx_ENABLED, &ha->flags))
1174                 free_irq(ha->pdev->irq, ha);
1175 }