1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2010 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <scsi/scsi_tcq.h>
12 #include <scsi/scsi_bsg_fc.h>
13 #include <scsi/scsi_eh.h>
14 
15 static void qla2x00_mbx_completion(scsi_qla_host_t *, uint16_t);
16 static void qla2x00_process_completed_request(struct scsi_qla_host *,
17 	struct req_que *, uint32_t);
18 static void qla2x00_status_entry(scsi_qla_host_t *, struct rsp_que *, void *);
19 static void qla2x00_status_cont_entry(struct rsp_que *, sts_cont_entry_t *);
20 static void qla2x00_error_entry(scsi_qla_host_t *, struct rsp_que *,
21 	sts_entry_t *);
22 
23 /**
24  * qla2100_intr_handler() - Process interrupts for the ISP2100 and ISP2200.
25  * @irq:
26  * @dev_id: SCSI driver HA context
27  *
28  * Called by system whenever the host adapter generates an interrupt.
29  *
30  * Returns handled flag.
31  */
32 irqreturn_t
qla2100_intr_handler(int irq,void * dev_id)33 qla2100_intr_handler(int irq, void *dev_id)
34 {
35 	scsi_qla_host_t	*vha;
36 	struct qla_hw_data *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 	struct rsp_que *rsp;
43 	unsigned long	flags;
44 
45 	rsp = (struct rsp_que *) dev_id;
46 	if (!rsp) {
47 		printk(KERN_INFO
48 		    "%s(): NULL response queue pointer\n", __func__);
49 		return (IRQ_NONE);
50 	}
51 
52 	ha = rsp->hw;
53 	reg = &ha->iobase->isp;
54 	status = 0;
55 
56 	spin_lock_irqsave(&ha->hardware_lock, flags);
57 	vha = pci_get_drvdata(ha->pdev);
58 	for (iter = 50; iter--; ) {
59 		hccr = RD_REG_WORD(&reg->hccr);
60 		if (hccr & HCCR_RISC_PAUSE) {
61 			if (pci_channel_offline(ha->pdev))
62 				break;
63 
64 			/*
65 			 * Issue a "HARD" reset in order for the RISC interrupt
66 			 * bit to be cleared.  Schedule a big hammmer to get
67 			 * out of the RISC PAUSED state.
68 			 */
69 			WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
70 			RD_REG_WORD(&reg->hccr);
71 
72 			ha->isp_ops->fw_dump(vha, 1);
73 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
74 			break;
75 		} else if ((RD_REG_WORD(&reg->istatus) & ISR_RISC_INT) == 0)
76 			break;
77 
78 		if (RD_REG_WORD(&reg->semaphore) & BIT_0) {
79 			WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
80 			RD_REG_WORD(&reg->hccr);
81 
82 			/* Get mailbox data. */
83 			mb[0] = RD_MAILBOX_REG(ha, reg, 0);
84 			if (mb[0] > 0x3fff && mb[0] < 0x8000) {
85 				qla2x00_mbx_completion(vha, mb[0]);
86 				status |= MBX_INTERRUPT;
87 			} else if (mb[0] > 0x7fff && mb[0] < 0xc000) {
88 				mb[1] = RD_MAILBOX_REG(ha, reg, 1);
89 				mb[2] = RD_MAILBOX_REG(ha, reg, 2);
90 				mb[3] = RD_MAILBOX_REG(ha, reg, 3);
91 				qla2x00_async_event(vha, rsp, mb);
92 			} else {
93 				/*EMPTY*/
94 				DEBUG2(printk("scsi(%ld): Unrecognized "
95 				    "interrupt type (%d).\n",
96 				    vha->host_no, mb[0]));
97 			}
98 			/* Release mailbox registers. */
99 			WRT_REG_WORD(&reg->semaphore, 0);
100 			RD_REG_WORD(&reg->semaphore);
101 		} else {
102 			qla2x00_process_response_queue(rsp);
103 
104 			WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
105 			RD_REG_WORD(&reg->hccr);
106 		}
107 	}
108 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
109 
110 	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
111 	    (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
112 		set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
113 		complete(&ha->mbx_intr_comp);
114 	}
115 
116 	return (IRQ_HANDLED);
117 }
118 
119 /**
120  * qla2300_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
121  * @irq:
122  * @dev_id: SCSI driver HA context
123  *
124  * Called by system whenever the host adapter generates an interrupt.
125  *
126  * Returns handled flag.
127  */
128 irqreturn_t
qla2300_intr_handler(int irq,void * dev_id)129 qla2300_intr_handler(int irq, void *dev_id)
130 {
131 	scsi_qla_host_t	*vha;
132 	struct device_reg_2xxx __iomem *reg;
133 	int		status;
134 	unsigned long	iter;
135 	uint32_t	stat;
136 	uint16_t	hccr;
137 	uint16_t	mb[4];
138 	struct rsp_que *rsp;
139 	struct qla_hw_data *ha;
140 	unsigned long	flags;
141 
142 	rsp = (struct rsp_que *) dev_id;
143 	if (!rsp) {
144 		printk(KERN_INFO
145 		    "%s(): NULL response queue pointer\n", __func__);
146 		return (IRQ_NONE);
147 	}
148 
149 	ha = rsp->hw;
150 	reg = &ha->iobase->isp;
151 	status = 0;
152 
153 	spin_lock_irqsave(&ha->hardware_lock, flags);
154 	vha = pci_get_drvdata(ha->pdev);
155 	for (iter = 50; iter--; ) {
156 		stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
157 		if (stat & HSR_RISC_PAUSED) {
158 			if (unlikely(pci_channel_offline(ha->pdev)))
159 				break;
160 
161 			hccr = RD_REG_WORD(&reg->hccr);
162 			if (hccr & (BIT_15 | BIT_13 | BIT_11 | BIT_8))
163 				qla_printk(KERN_INFO, ha, "Parity error -- "
164 				    "HCCR=%x, Dumping firmware!\n", hccr);
165 			else
166 				qla_printk(KERN_INFO, ha, "RISC paused -- "
167 				    "HCCR=%x, Dumping firmware!\n", hccr);
168 
169 			/*
170 			 * Issue a "HARD" reset in order for the RISC
171 			 * interrupt bit to be cleared.  Schedule a big
172 			 * hammmer to get out of the RISC PAUSED state.
173 			 */
174 			WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
175 			RD_REG_WORD(&reg->hccr);
176 
177 			ha->isp_ops->fw_dump(vha, 1);
178 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
179 			break;
180 		} else if ((stat & HSR_RISC_INT) == 0)
181 			break;
182 
183 		switch (stat & 0xff) {
184 		case 0x1:
185 		case 0x2:
186 		case 0x10:
187 		case 0x11:
188 			qla2x00_mbx_completion(vha, MSW(stat));
189 			status |= MBX_INTERRUPT;
190 
191 			/* Release mailbox registers. */
192 			WRT_REG_WORD(&reg->semaphore, 0);
193 			break;
194 		case 0x12:
195 			mb[0] = MSW(stat);
196 			mb[1] = RD_MAILBOX_REG(ha, reg, 1);
197 			mb[2] = RD_MAILBOX_REG(ha, reg, 2);
198 			mb[3] = RD_MAILBOX_REG(ha, reg, 3);
199 			qla2x00_async_event(vha, rsp, mb);
200 			break;
201 		case 0x13:
202 			qla2x00_process_response_queue(rsp);
203 			break;
204 		case 0x15:
205 			mb[0] = MBA_CMPLT_1_16BIT;
206 			mb[1] = MSW(stat);
207 			qla2x00_async_event(vha, rsp, mb);
208 			break;
209 		case 0x16:
210 			mb[0] = MBA_SCSI_COMPLETION;
211 			mb[1] = MSW(stat);
212 			mb[2] = RD_MAILBOX_REG(ha, reg, 2);
213 			qla2x00_async_event(vha, rsp, mb);
214 			break;
215 		default:
216 			DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
217 			    "(%d).\n",
218 			    vha->host_no, stat & 0xff));
219 			break;
220 		}
221 		WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
222 		RD_REG_WORD_RELAXED(&reg->hccr);
223 	}
224 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
225 
226 	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
227 	    (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
228 		set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
229 		complete(&ha->mbx_intr_comp);
230 	}
231 
232 	return (IRQ_HANDLED);
233 }
234 
235 /**
236  * qla2x00_mbx_completion() - Process mailbox command completions.
237  * @ha: SCSI driver HA context
238  * @mb0: Mailbox0 register
239  */
240 static void
qla2x00_mbx_completion(scsi_qla_host_t * vha,uint16_t mb0)241 qla2x00_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
242 {
243 	uint16_t	cnt;
244 	uint16_t __iomem *wptr;
245 	struct qla_hw_data *ha = vha->hw;
246 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
247 
248 	/* Load return mailbox registers. */
249 	ha->flags.mbox_int = 1;
250 	ha->mailbox_out[0] = mb0;
251 	wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 1);
252 
253 	for (cnt = 1; cnt < ha->mbx_count; cnt++) {
254 		if (IS_QLA2200(ha) && cnt == 8)
255 			wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 8);
256 		if (cnt == 4 || cnt == 5)
257 			ha->mailbox_out[cnt] = qla2x00_debounce_register(wptr);
258 		else
259 			ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
260 
261 		wptr++;
262 	}
263 
264 	if (ha->mcp) {
265 		DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
266 		    __func__, vha->host_no, ha->mcp->mb[0]));
267 	} else {
268 		DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
269 		    __func__, vha->host_no));
270 	}
271 }
272 
273 static void
qla81xx_idc_event(scsi_qla_host_t * vha,uint16_t aen,uint16_t descr)274 qla81xx_idc_event(scsi_qla_host_t *vha, uint16_t aen, uint16_t descr)
275 {
276 	static char *event[] =
277 		{ "Complete", "Request Notification", "Time Extension" };
278 	int rval;
279 	struct device_reg_24xx __iomem *reg24 = &vha->hw->iobase->isp24;
280 	uint16_t __iomem *wptr;
281 	uint16_t cnt, timeout, mb[QLA_IDC_ACK_REGS];
282 
283 	/* Seed data -- mailbox1 -> mailbox7. */
284 	wptr = (uint16_t __iomem *)&reg24->mailbox1;
285 	for (cnt = 0; cnt < QLA_IDC_ACK_REGS; cnt++, wptr++)
286 		mb[cnt] = RD_REG_WORD(wptr);
287 
288 	DEBUG2(printk("scsi(%ld): Inter-Driver Commucation %s -- "
289 	    "%04x %04x %04x %04x %04x %04x %04x.\n", vha->host_no,
290 	    event[aen & 0xff],
291 	    mb[0], mb[1], mb[2], mb[3], mb[4], mb[5], mb[6]));
292 
293 	/* Acknowledgement needed? [Notify && non-zero timeout]. */
294 	timeout = (descr >> 8) & 0xf;
295 	if (aen != MBA_IDC_NOTIFY || !timeout)
296 		return;
297 
298 	DEBUG2(printk("scsi(%ld): Inter-Driver Commucation %s -- "
299 	    "ACK timeout=%d.\n", vha->host_no, event[aen & 0xff], timeout));
300 
301 	rval = qla2x00_post_idc_ack_work(vha, mb);
302 	if (rval != QLA_SUCCESS)
303 		qla_printk(KERN_WARNING, vha->hw,
304 		    "IDC failed to post ACK.\n");
305 }
306 
307 /**
308  * qla2x00_async_event() - Process aynchronous events.
309  * @ha: SCSI driver HA context
310  * @mb: Mailbox registers (0 - 3)
311  */
312 void
qla2x00_async_event(scsi_qla_host_t * vha,struct rsp_que * rsp,uint16_t * mb)313 qla2x00_async_event(scsi_qla_host_t *vha, struct rsp_que *rsp, uint16_t *mb)
314 {
315 #define LS_UNKNOWN	2
316 	static char	*link_speeds[] = { "1", "2", "?", "4", "8", "10" };
317 	char		*link_speed;
318 	uint16_t	handle_cnt;
319 	uint16_t	cnt, mbx;
320 	uint32_t	handles[5];
321 	struct qla_hw_data *ha = vha->hw;
322 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
323 	struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
324 	struct device_reg_82xx __iomem *reg82 = &ha->iobase->isp82;
325 	uint32_t	rscn_entry, host_pid;
326 	uint8_t		rscn_queue_index;
327 	unsigned long	flags;
328 
329 	/* Setup to process RIO completion. */
330 	handle_cnt = 0;
331 	if (IS_QLA8XXX_TYPE(ha))
332 		goto skip_rio;
333 	switch (mb[0]) {
334 	case MBA_SCSI_COMPLETION:
335 		handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
336 		handle_cnt = 1;
337 		break;
338 	case MBA_CMPLT_1_16BIT:
339 		handles[0] = mb[1];
340 		handle_cnt = 1;
341 		mb[0] = MBA_SCSI_COMPLETION;
342 		break;
343 	case MBA_CMPLT_2_16BIT:
344 		handles[0] = mb[1];
345 		handles[1] = mb[2];
346 		handle_cnt = 2;
347 		mb[0] = MBA_SCSI_COMPLETION;
348 		break;
349 	case MBA_CMPLT_3_16BIT:
350 		handles[0] = mb[1];
351 		handles[1] = mb[2];
352 		handles[2] = mb[3];
353 		handle_cnt = 3;
354 		mb[0] = MBA_SCSI_COMPLETION;
355 		break;
356 	case MBA_CMPLT_4_16BIT:
357 		handles[0] = mb[1];
358 		handles[1] = mb[2];
359 		handles[2] = mb[3];
360 		handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
361 		handle_cnt = 4;
362 		mb[0] = MBA_SCSI_COMPLETION;
363 		break;
364 	case MBA_CMPLT_5_16BIT:
365 		handles[0] = mb[1];
366 		handles[1] = mb[2];
367 		handles[2] = mb[3];
368 		handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
369 		handles[4] = (uint32_t)RD_MAILBOX_REG(ha, reg, 7);
370 		handle_cnt = 5;
371 		mb[0] = MBA_SCSI_COMPLETION;
372 		break;
373 	case MBA_CMPLT_2_32BIT:
374 		handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
375 		handles[1] = le32_to_cpu(
376 		    ((uint32_t)(RD_MAILBOX_REG(ha, reg, 7) << 16)) |
377 		    RD_MAILBOX_REG(ha, reg, 6));
378 		handle_cnt = 2;
379 		mb[0] = MBA_SCSI_COMPLETION;
380 		break;
381 	default:
382 		break;
383 	}
384 skip_rio:
385 	switch (mb[0]) {
386 	case MBA_SCSI_COMPLETION:	/* Fast Post */
387 		if (!vha->flags.online)
388 			break;
389 
390 		for (cnt = 0; cnt < handle_cnt; cnt++)
391 			qla2x00_process_completed_request(vha, rsp->req,
392 				handles[cnt]);
393 		break;
394 
395 	case MBA_RESET:			/* Reset */
396 		DEBUG2(printk("scsi(%ld): Asynchronous RESET.\n",
397 			vha->host_no));
398 
399 		set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
400 		break;
401 
402 	case MBA_SYSTEM_ERR:		/* System Error */
403 		mbx = IS_QLA81XX(ha) ? RD_REG_WORD(&reg24->mailbox7) : 0;
404 		qla_printk(KERN_INFO, ha,
405 		    "ISP System Error - mbx1=%xh mbx2=%xh mbx3=%xh "
406 		    "mbx7=%xh.\n", mb[1], mb[2], mb[3], mbx);
407 
408 		ha->isp_ops->fw_dump(vha, 1);
409 
410 		if (IS_FWI2_CAPABLE(ha)) {
411 			if (mb[1] == 0 && mb[2] == 0) {
412 				qla_printk(KERN_ERR, ha,
413 				    "Unrecoverable Hardware Error: adapter "
414 				    "marked OFFLINE!\n");
415 				vha->flags.online = 0;
416 			} else {
417 				/* Check to see if MPI timeout occurred */
418 				if ((mbx & MBX_3) && (ha->flags.port0))
419 					set_bit(MPI_RESET_NEEDED,
420 					    &vha->dpc_flags);
421 
422 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
423 			}
424 		} else if (mb[1] == 0) {
425 			qla_printk(KERN_INFO, ha,
426 			    "Unrecoverable Hardware Error: adapter marked "
427 			    "OFFLINE!\n");
428 			vha->flags.online = 0;
429 		} else
430 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
431 		break;
432 
433 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
434 		DEBUG2(printk("scsi(%ld): ISP Request Transfer Error (%x).\n",
435 		    vha->host_no, mb[1]));
436 		qla_printk(KERN_WARNING, ha,
437 		    "ISP Request Transfer Error (%x).\n", mb[1]);
438 
439 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
440 		break;
441 
442 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
443 		DEBUG2(printk("scsi(%ld): ISP Response Transfer Error.\n",
444 		    vha->host_no));
445 		qla_printk(KERN_WARNING, ha, "ISP Response Transfer Error.\n");
446 
447 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
448 		break;
449 
450 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up */
451 		DEBUG2(printk("scsi(%ld): Asynchronous WAKEUP_THRES.\n",
452 		    vha->host_no));
453 		break;
454 
455 	case MBA_LIP_OCCURRED:		/* Loop Initialization Procedure */
456 		DEBUG2(printk("scsi(%ld): LIP occurred (%x).\n", vha->host_no,
457 		    mb[1]));
458 		qla_printk(KERN_INFO, ha, "LIP occurred (%x).\n", mb[1]);
459 
460 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
461 			atomic_set(&vha->loop_state, LOOP_DOWN);
462 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
463 			qla2x00_mark_all_devices_lost(vha, 1);
464 		}
465 
466 		if (vha->vp_idx) {
467 			atomic_set(&vha->vp_state, VP_FAILED);
468 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
469 		}
470 
471 		set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
472 		set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
473 
474 		vha->flags.management_server_logged_in = 0;
475 		qla2x00_post_aen_work(vha, FCH_EVT_LIP, mb[1]);
476 		break;
477 
478 	case MBA_LOOP_UP:		/* Loop Up Event */
479 		if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
480 			link_speed = link_speeds[0];
481 			ha->link_data_rate = PORT_SPEED_1GB;
482 		} else {
483 			link_speed = link_speeds[LS_UNKNOWN];
484 			if (mb[1] < 5)
485 				link_speed = link_speeds[mb[1]];
486 			else if (mb[1] == 0x13)
487 				link_speed = link_speeds[5];
488 			ha->link_data_rate = mb[1];
489 		}
490 
491 		DEBUG2(printk("scsi(%ld): Asynchronous LOOP UP (%s Gbps).\n",
492 		    vha->host_no, link_speed));
493 		qla_printk(KERN_INFO, ha, "LOOP UP detected (%s Gbps).\n",
494 		    link_speed);
495 
496 		vha->flags.management_server_logged_in = 0;
497 		qla2x00_post_aen_work(vha, FCH_EVT_LINKUP, ha->link_data_rate);
498 		break;
499 
500 	case MBA_LOOP_DOWN:		/* Loop Down Event */
501 		mbx = IS_QLA81XX(ha) ? RD_REG_WORD(&reg24->mailbox4) : 0;
502 		mbx = IS_QLA82XX(ha) ? RD_REG_WORD(&reg82->mailbox_out[4]) : mbx;
503 		DEBUG2(printk("scsi(%ld): Asynchronous LOOP DOWN "
504 		    "(%x %x %x %x).\n", vha->host_no, mb[1], mb[2], mb[3],
505 		    mbx));
506 		qla_printk(KERN_INFO, ha,
507 		    "LOOP DOWN detected (%x %x %x %x).\n", mb[1], mb[2], mb[3],
508 		    mbx);
509 
510 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
511 			atomic_set(&vha->loop_state, LOOP_DOWN);
512 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
513 			vha->device_flags |= DFLG_NO_CABLE;
514 			qla2x00_mark_all_devices_lost(vha, 1);
515 		}
516 
517 		if (vha->vp_idx) {
518 			atomic_set(&vha->vp_state, VP_FAILED);
519 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
520 		}
521 
522 		vha->flags.management_server_logged_in = 0;
523 		ha->link_data_rate = PORT_SPEED_UNKNOWN;
524 		qla2x00_post_aen_work(vha, FCH_EVT_LINKDOWN, 0);
525 		break;
526 
527 	case MBA_LIP_RESET:		/* LIP reset occurred */
528 		DEBUG2(printk("scsi(%ld): Asynchronous LIP RESET (%x).\n",
529 		    vha->host_no, mb[1]));
530 		qla_printk(KERN_INFO, ha,
531 		    "LIP reset occurred (%x).\n", mb[1]);
532 
533 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
534 			atomic_set(&vha->loop_state, LOOP_DOWN);
535 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
536 			qla2x00_mark_all_devices_lost(vha, 1);
537 		}
538 
539 		if (vha->vp_idx) {
540 			atomic_set(&vha->vp_state, VP_FAILED);
541 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
542 		}
543 
544 		set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
545 
546 		ha->operating_mode = LOOP;
547 		vha->flags.management_server_logged_in = 0;
548 		qla2x00_post_aen_work(vha, FCH_EVT_LIPRESET, mb[1]);
549 		break;
550 
551 	/* case MBA_DCBX_COMPLETE: */
552 	case MBA_POINT_TO_POINT:	/* Point-to-Point */
553 		if (IS_QLA2100(ha))
554 			break;
555 
556 		if (IS_QLA8XXX_TYPE(ha)) {
557 			DEBUG2(printk("scsi(%ld): DCBX Completed -- %04x %04x "
558 			    "%04x\n", vha->host_no, mb[1], mb[2], mb[3]));
559 			if (ha->notify_dcbx_comp)
560 				complete(&ha->dcbx_comp);
561 
562 		} else
563 			DEBUG2(printk("scsi(%ld): Asynchronous P2P MODE "
564 			    "received.\n", vha->host_no));
565 
566 		/*
567 		 * Until there's a transition from loop down to loop up, treat
568 		 * this as loop down only.
569 		 */
570 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
571 			atomic_set(&vha->loop_state, LOOP_DOWN);
572 			if (!atomic_read(&vha->loop_down_timer))
573 				atomic_set(&vha->loop_down_timer,
574 				    LOOP_DOWN_TIME);
575 			qla2x00_mark_all_devices_lost(vha, 1);
576 		}
577 
578 		if (vha->vp_idx) {
579 			atomic_set(&vha->vp_state, VP_FAILED);
580 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
581 		}
582 
583 		if (!(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)))
584 			set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
585 
586 		set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
587 		set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
588 
589 		ha->flags.gpsc_supported = 1;
590 		vha->flags.management_server_logged_in = 0;
591 		break;
592 
593 	case MBA_CHG_IN_CONNECTION:	/* Change in connection mode */
594 		if (IS_QLA2100(ha))
595 			break;
596 
597 		DEBUG2(printk("scsi(%ld): Asynchronous Change In Connection "
598 		    "received.\n",
599 		    vha->host_no));
600 		qla_printk(KERN_INFO, ha,
601 		    "Configuration change detected: value=%x.\n", mb[1]);
602 
603 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
604 			atomic_set(&vha->loop_state, LOOP_DOWN);
605 			if (!atomic_read(&vha->loop_down_timer))
606 				atomic_set(&vha->loop_down_timer,
607 				    LOOP_DOWN_TIME);
608 			qla2x00_mark_all_devices_lost(vha, 1);
609 		}
610 
611 		if (vha->vp_idx) {
612 			atomic_set(&vha->vp_state, VP_FAILED);
613 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
614 		}
615 
616 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
617 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
618 		break;
619 
620 	case MBA_PORT_UPDATE:		/* Port database update */
621 		/*
622 		 * Handle only global and vn-port update events
623 		 *
624 		 * Relevant inputs:
625 		 * mb[1] = N_Port handle of changed port
626 		 * OR 0xffff for global event
627 		 * mb[2] = New login state
628 		 * 7 = Port logged out
629 		 * mb[3] = LSB is vp_idx, 0xff = all vps
630 		 *
631 		 * Skip processing if:
632 		 *       Event is global, vp_idx is NOT all vps,
633 		 *           vp_idx does not match
634 		 *       Event is not global, vp_idx does not match
635 		 */
636 		if (IS_QLA2XXX_MIDTYPE(ha) &&
637 		    ((mb[1] == 0xffff && (mb[3] & 0xff) != 0xff) ||
638 			(mb[1] != 0xffff)) && vha->vp_idx != (mb[3] & 0xff))
639 			break;
640 
641 		/* Global event -- port logout or port unavailable. */
642 		if (mb[1] == 0xffff && mb[2] == 0x7) {
643 			DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE.\n",
644 			    vha->host_no));
645 			DEBUG(printk(KERN_INFO
646 			    "scsi(%ld): Port unavailable %04x %04x %04x.\n",
647 			    vha->host_no, mb[1], mb[2], mb[3]));
648 
649 			if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
650 				atomic_set(&vha->loop_state, LOOP_DOWN);
651 				atomic_set(&vha->loop_down_timer,
652 				    LOOP_DOWN_TIME);
653 				vha->device_flags |= DFLG_NO_CABLE;
654 				qla2x00_mark_all_devices_lost(vha, 1);
655 			}
656 
657 			if (vha->vp_idx) {
658 				atomic_set(&vha->vp_state, VP_FAILED);
659 				fc_vport_set_state(vha->fc_vport,
660 				    FC_VPORT_FAILED);
661 				qla2x00_mark_all_devices_lost(vha, 1);
662 			}
663 
664 			vha->flags.management_server_logged_in = 0;
665 			ha->link_data_rate = PORT_SPEED_UNKNOWN;
666 			break;
667 		}
668 
669 		/*
670 		 * If PORT UPDATE is global (received LIP_OCCURRED/LIP_RESET
671 		 * event etc. earlier indicating loop is down) then process
672 		 * it.  Otherwise ignore it and Wait for RSCN to come in.
673 		 */
674 		atomic_set(&vha->loop_down_timer, 0);
675 		if (atomic_read(&vha->loop_state) != LOOP_DOWN &&
676 		    atomic_read(&vha->loop_state) != LOOP_DEAD) {
677 			DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE "
678 			    "ignored %04x/%04x/%04x.\n", vha->host_no, mb[1],
679 			    mb[2], mb[3]));
680 			break;
681 		}
682 
683 		DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE.\n",
684 		    vha->host_no));
685 		DEBUG(printk(KERN_INFO
686 		    "scsi(%ld): Port database changed %04x %04x %04x.\n",
687 		    vha->host_no, mb[1], mb[2], mb[3]));
688 
689 		/*
690 		 * Mark all devices as missing so we will login again.
691 		 */
692 		atomic_set(&vha->loop_state, LOOP_UP);
693 
694 		qla2x00_mark_all_devices_lost(vha, 1);
695 
696 		vha->flags.rscn_queue_overflow = 1;
697 
698 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
699 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
700 		break;
701 
702 	case MBA_RSCN_UPDATE:		/* State Change Registration */
703 		/* Check if the Vport has issued a SCR */
704 		if (vha->vp_idx && test_bit(VP_SCR_NEEDED, &vha->vp_flags))
705 			break;
706 		/* Only handle SCNs for our Vport index. */
707 		if (ha->flags.npiv_supported && vha->vp_idx != (mb[3] & 0xff))
708 			break;
709 
710 		DEBUG2(printk("scsi(%ld): Asynchronous RSCR UPDATE.\n",
711 		    vha->host_no));
712 		DEBUG(printk(KERN_INFO
713 		    "scsi(%ld): RSCN database changed -- %04x %04x %04x.\n",
714 		    vha->host_no, mb[1], mb[2], mb[3]));
715 
716 		rscn_entry = ((mb[1] & 0xff) << 16) | mb[2];
717 		host_pid = (vha->d_id.b.domain << 16) | (vha->d_id.b.area << 8)
718 				| vha->d_id.b.al_pa;
719 		if (rscn_entry == host_pid) {
720 			DEBUG(printk(KERN_INFO
721 			    "scsi(%ld): Ignoring RSCN update to local host "
722 			    "port ID (%06x)\n",
723 			    vha->host_no, host_pid));
724 			break;
725 		}
726 
727 		/* Ignore reserved bits from RSCN-payload. */
728 		rscn_entry = ((mb[1] & 0x3ff) << 16) | mb[2];
729 		rscn_queue_index = vha->rscn_in_ptr + 1;
730 		if (rscn_queue_index == MAX_RSCN_COUNT)
731 			rscn_queue_index = 0;
732 		if (rscn_queue_index != vha->rscn_out_ptr) {
733 			vha->rscn_queue[vha->rscn_in_ptr] = rscn_entry;
734 			vha->rscn_in_ptr = rscn_queue_index;
735 		} else {
736 			vha->flags.rscn_queue_overflow = 1;
737 		}
738 
739 		atomic_set(&vha->loop_state, LOOP_UPDATE);
740 		atomic_set(&vha->loop_down_timer, 0);
741 		vha->flags.management_server_logged_in = 0;
742 
743 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
744 		set_bit(RSCN_UPDATE, &vha->dpc_flags);
745 		qla2x00_post_aen_work(vha, FCH_EVT_RSCN, rscn_entry);
746 		break;
747 
748 	/* case MBA_RIO_RESPONSE: */
749 	case MBA_ZIO_RESPONSE:
750 		DEBUG3(printk("scsi(%ld): [R|Z]IO update completion.\n",
751 		    vha->host_no));
752 
753 		if (IS_FWI2_CAPABLE(ha))
754 			qla24xx_process_response_queue(vha, rsp);
755 		else
756 			qla2x00_process_response_queue(rsp);
757 		break;
758 
759 	case MBA_DISCARD_RND_FRAME:
760 		DEBUG2(printk("scsi(%ld): Discard RND Frame -- %04x %04x "
761 		    "%04x.\n", vha->host_no, mb[1], mb[2], mb[3]));
762 		break;
763 
764 	case MBA_TRACE_NOTIFICATION:
765 		DEBUG2(printk("scsi(%ld): Trace Notification -- %04x %04x.\n",
766 		vha->host_no, mb[1], mb[2]));
767 		break;
768 
769 	case MBA_ISP84XX_ALERT:
770 		DEBUG2(printk("scsi(%ld): ISP84XX Alert Notification -- "
771 		    "%04x %04x %04x\n", vha->host_no, mb[1], mb[2], mb[3]));
772 
773 		spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
774 		switch (mb[1]) {
775 		case A84_PANIC_RECOVERY:
776 			qla_printk(KERN_INFO, ha, "Alert 84XX: panic recovery "
777 			    "%04x %04x\n", mb[2], mb[3]);
778 			break;
779 		case A84_OP_LOGIN_COMPLETE:
780 			ha->cs84xx->op_fw_version = mb[3] << 16 | mb[2];
781 			DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
782 			    "firmware version %x\n", ha->cs84xx->op_fw_version));
783 			break;
784 		case A84_DIAG_LOGIN_COMPLETE:
785 			ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
786 			DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
787 			    "diagnostic firmware version %x\n",
788 			    ha->cs84xx->diag_fw_version));
789 			break;
790 		case A84_GOLD_LOGIN_COMPLETE:
791 			ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
792 			ha->cs84xx->fw_update = 1;
793 			DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX: gold "
794 			    "firmware version %x\n",
795 			    ha->cs84xx->gold_fw_version));
796 			break;
797 		default:
798 			qla_printk(KERN_ERR, ha,
799 			    "Alert 84xx: Invalid Alert %04x %04x %04x\n",
800 			    mb[1], mb[2], mb[3]);
801 		}
802 		spin_unlock_irqrestore(&ha->cs84xx->access_lock, flags);
803 		break;
804 	case MBA_DCBX_START:
805 		DEBUG2(printk("scsi(%ld): DCBX Started -- %04x %04x %04x\n",
806 		    vha->host_no, mb[1], mb[2], mb[3]));
807 		break;
808 	case MBA_DCBX_PARAM_UPDATE:
809 		DEBUG2(printk("scsi(%ld): DCBX Parameters Updated -- "
810 		    "%04x %04x %04x\n", vha->host_no, mb[1], mb[2], mb[3]));
811 		break;
812 	case MBA_FCF_CONF_ERR:
813 		DEBUG2(printk("scsi(%ld): FCF Configuration Error -- "
814 		    "%04x %04x %04x\n", vha->host_no, mb[1], mb[2], mb[3]));
815 		break;
816 	case MBA_IDC_COMPLETE:
817 	case MBA_IDC_NOTIFY:
818 	case MBA_IDC_TIME_EXT:
819 		qla81xx_idc_event(vha, mb[0], mb[1]);
820 		break;
821 	}
822 
823 	if (!vha->vp_idx && ha->num_vhosts)
824 		qla2x00_alert_all_vps(rsp, mb);
825 }
826 
827 /**
828  * qla2x00_process_completed_request() - Process a Fast Post response.
829  * @ha: SCSI driver HA context
830  * @index: SRB index
831  */
832 static void
qla2x00_process_completed_request(struct scsi_qla_host * vha,struct req_que * req,uint32_t index)833 qla2x00_process_completed_request(struct scsi_qla_host *vha,
834 				struct req_que *req, uint32_t index)
835 {
836 	srb_t *sp;
837 	struct qla_hw_data *ha = vha->hw;
838 
839 	/* Validate handle. */
840 	if (index >= MAX_OUTSTANDING_COMMANDS) {
841 		DEBUG2(printk("scsi(%ld): Invalid SCSI completion handle %d.\n",
842 		    vha->host_no, index));
843 		qla_printk(KERN_WARNING, ha,
844 		    "Invalid SCSI completion handle %d.\n", index);
845 
846 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
847 		return;
848 	}
849 
850 	sp = req->outstanding_cmds[index];
851 	if (sp) {
852 		/* Free outstanding command slot. */
853 		req->outstanding_cmds[index] = NULL;
854 
855 		/* Save ISP completion status */
856 		sp->cmd->result = DID_OK << 16;
857 		qla2x00_sp_compl(ha, sp);
858 	} else {
859 		DEBUG2(printk("scsi(%ld) Req:%d: Invalid ISP SCSI completion"
860 			" handle(0x%x)\n", vha->host_no, req->id, index));
861 		qla_printk(KERN_WARNING, ha,
862 		    "Invalid ISP SCSI completion handle\n");
863 
864 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
865 	}
866 }
867 
868 static srb_t *
qla2x00_get_sp_from_handle(scsi_qla_host_t * vha,const char * func,struct req_que * req,void * iocb)869 qla2x00_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
870     struct req_que *req, void *iocb)
871 {
872 	struct qla_hw_data *ha = vha->hw;
873 	sts_entry_t *pkt = iocb;
874 	srb_t *sp = NULL;
875 	uint16_t index;
876 
877 	index = LSW(pkt->handle);
878 	if (index >= MAX_OUTSTANDING_COMMANDS) {
879 		qla_printk(KERN_WARNING, ha,
880 		    "%s: Invalid completion handle (%x).\n", func, index);
881 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
882 		goto done;
883 	}
884 	sp = req->outstanding_cmds[index];
885 	if (!sp) {
886 		qla_printk(KERN_WARNING, ha,
887 		    "%s: Invalid completion handle (%x) -- timed-out.\n", func,
888 		    index);
889 		return sp;
890 	}
891 	if (sp->handle != index) {
892 		qla_printk(KERN_WARNING, ha,
893 		    "%s: SRB handle (%x) mismatch %x.\n", func, sp->handle,
894 		    index);
895 		return NULL;
896 	}
897 
898 	req->outstanding_cmds[index] = NULL;
899 
900 done:
901 	return sp;
902 }
903 
904 static void
qla2x00_mbx_iocb_entry(scsi_qla_host_t * vha,struct req_que * req,struct mbx_entry * mbx)905 qla2x00_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
906     struct mbx_entry *mbx)
907 {
908 	const char func[] = "MBX-IOCB";
909 	const char *type;
910 	fc_port_t *fcport;
911 	srb_t *sp;
912 	struct srb_iocb *lio;
913 	struct srb_ctx *ctx;
914 	uint16_t *data;
915 	uint16_t status;
916 
917 	sp = qla2x00_get_sp_from_handle(vha, func, req, mbx);
918 	if (!sp)
919 		return;
920 
921 	ctx = sp->ctx;
922 	lio = ctx->u.iocb_cmd;
923 	type = ctx->name;
924 	fcport = sp->fcport;
925 	data = lio->u.logio.data;
926 
927 	data[0] = MBS_COMMAND_ERROR;
928 	data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
929 	    QLA_LOGIO_LOGIN_RETRIED : 0;
930 	if (mbx->entry_status) {
931 		DEBUG2(printk(KERN_WARNING
932 		    "scsi(%ld:%x): Async-%s error entry - portid=%02x%02x%02x "
933 		    "entry-status=%x status=%x state-flag=%x "
934 		    "status-flags=%x.\n",
935 		    fcport->vha->host_no, sp->handle, type,
936 		    fcport->d_id.b.domain, fcport->d_id.b.area,
937 		    fcport->d_id.b.al_pa, mbx->entry_status,
938 		    le16_to_cpu(mbx->status), le16_to_cpu(mbx->state_flags),
939 		    le16_to_cpu(mbx->status_flags)));
940 
941 		DEBUG2(qla2x00_dump_buffer((uint8_t *)mbx, sizeof(*mbx)));
942 
943 		goto logio_done;
944 	}
945 
946 	status = le16_to_cpu(mbx->status);
947 	if (status == 0x30 && ctx->type == SRB_LOGIN_CMD &&
948 	    le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE)
949 		status = 0;
950 	if (!status && le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE) {
951 		DEBUG2(printk(KERN_DEBUG
952 		    "scsi(%ld:%x): Async-%s complete - portid=%02x%02x%02x "
953 		    "mbx1=%x.\n",
954 		    fcport->vha->host_no, sp->handle, type,
955 		    fcport->d_id.b.domain, fcport->d_id.b.area,
956 		    fcport->d_id.b.al_pa, le16_to_cpu(mbx->mb1)));
957 
958 		data[0] = MBS_COMMAND_COMPLETE;
959 		if (ctx->type == SRB_LOGIN_CMD) {
960 			fcport->port_type = FCT_TARGET;
961 			if (le16_to_cpu(mbx->mb1) & BIT_0)
962 				fcport->port_type = FCT_INITIATOR;
963 			else if (le16_to_cpu(mbx->mb1) & BIT_1)
964 				fcport->flags |= FCF_FCP2_DEVICE;
965 		}
966 		goto logio_done;
967 	}
968 
969 	data[0] = le16_to_cpu(mbx->mb0);
970 	switch (data[0]) {
971 	case MBS_PORT_ID_USED:
972 		data[1] = le16_to_cpu(mbx->mb1);
973 		break;
974 	case MBS_LOOP_ID_USED:
975 		break;
976 	default:
977 		data[0] = MBS_COMMAND_ERROR;
978 		break;
979 	}
980 
981 	DEBUG2(printk(KERN_WARNING
982 	    "scsi(%ld:%x): Async-%s failed - portid=%02x%02x%02x status=%x "
983 	    "mb0=%x mb1=%x mb2=%x mb6=%x mb7=%x.\n",
984 	    fcport->vha->host_no, sp->handle, type, fcport->d_id.b.domain,
985 	    fcport->d_id.b.area, fcport->d_id.b.al_pa, status,
986 	    le16_to_cpu(mbx->mb0), le16_to_cpu(mbx->mb1),
987 	    le16_to_cpu(mbx->mb2), le16_to_cpu(mbx->mb6),
988 	    le16_to_cpu(mbx->mb7)));
989 
990 logio_done:
991 	lio->done(sp);
992 }
993 
994 static void
qla2x00_ct_entry(scsi_qla_host_t * vha,struct req_que * req,sts_entry_t * pkt,int iocb_type)995 qla2x00_ct_entry(scsi_qla_host_t *vha, struct req_que *req,
996     sts_entry_t *pkt, int iocb_type)
997 {
998 	const char func[] = "CT_IOCB";
999 	const char *type;
1000 	struct qla_hw_data *ha = vha->hw;
1001 	srb_t *sp;
1002 	struct srb_ctx *sp_bsg;
1003 	struct fc_bsg_job *bsg_job;
1004 	uint16_t comp_status;
1005 
1006 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1007 	if (!sp)
1008 		return;
1009 
1010 	sp_bsg = sp->ctx;
1011 	bsg_job = sp_bsg->u.bsg_job;
1012 
1013 	type = NULL;
1014 	switch (sp_bsg->type) {
1015 	case SRB_CT_CMD:
1016 		type = "ct pass-through";
1017 		break;
1018 	default:
1019 		qla_printk(KERN_WARNING, ha,
1020 		    "%s: Unrecognized SRB: (%p) type=%d.\n", func, sp,
1021 		    sp_bsg->type);
1022 		return;
1023 	}
1024 
1025 	comp_status = le16_to_cpu(pkt->comp_status);
1026 
1027 	/* return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
1028 	 * fc payload  to the caller
1029 	 */
1030 	bsg_job->reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
1031 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1032 
1033 	if (comp_status != CS_COMPLETE) {
1034 		if (comp_status == CS_DATA_UNDERRUN) {
1035 			bsg_job->reply->result = DID_OK << 16;
1036 			bsg_job->reply->reply_payload_rcv_len =
1037 			    le16_to_cpu(((sts_entry_t *)pkt)->rsp_info_len);
1038 
1039 			DEBUG2(qla_printk(KERN_WARNING, ha,
1040 			    "scsi(%ld): CT pass-through-%s error "
1041 			    "comp_status-status=0x%x total_byte = 0x%x.\n",
1042 			    vha->host_no, type, comp_status,
1043 			    bsg_job->reply->reply_payload_rcv_len));
1044 		} else {
1045 			DEBUG2(qla_printk(KERN_WARNING, ha,
1046 			    "scsi(%ld): CT pass-through-%s error "
1047 			    "comp_status-status=0x%x.\n",
1048 			    vha->host_no, type, comp_status));
1049 			bsg_job->reply->result = DID_ERROR << 16;
1050 			bsg_job->reply->reply_payload_rcv_len = 0;
1051 		}
1052 		DEBUG2(qla2x00_dump_buffer((uint8_t *)pkt, sizeof(*pkt)));
1053 	} else {
1054 		bsg_job->reply->result =  DID_OK << 16;;
1055 		bsg_job->reply->reply_payload_rcv_len =
1056 		    bsg_job->reply_payload.payload_len;
1057 		bsg_job->reply_len = 0;
1058 	}
1059 
1060 	dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1061 	    bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1062 
1063 	dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1064 	    bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1065 
1066 	if (sp_bsg->type == SRB_ELS_CMD_HST || sp_bsg->type == SRB_CT_CMD)
1067 		kfree(sp->fcport);
1068 
1069 	kfree(sp->ctx);
1070 	mempool_free(sp, ha->srb_mempool);
1071 	bsg_job->job_done(bsg_job);
1072 }
1073 
1074 static void
qla24xx_els_ct_entry(scsi_qla_host_t * vha,struct req_que * req,struct sts_entry_24xx * pkt,int iocb_type)1075 qla24xx_els_ct_entry(scsi_qla_host_t *vha, struct req_que *req,
1076     struct sts_entry_24xx *pkt, int iocb_type)
1077 {
1078 	const char func[] = "ELS_CT_IOCB";
1079 	const char *type;
1080 	struct qla_hw_data *ha = vha->hw;
1081 	srb_t *sp;
1082 	struct srb_ctx *sp_bsg;
1083 	struct fc_bsg_job *bsg_job;
1084 	uint16_t comp_status;
1085 	uint32_t fw_status[3];
1086 	uint8_t* fw_sts_ptr;
1087 
1088 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1089 	if (!sp)
1090 		return;
1091 	sp_bsg = sp->ctx;
1092 	bsg_job = sp_bsg->u.bsg_job;
1093 
1094 	type = NULL;
1095 	switch (sp_bsg->type) {
1096 	case SRB_ELS_CMD_RPT:
1097 	case SRB_ELS_CMD_HST:
1098 		type = "els";
1099 		break;
1100 	case SRB_CT_CMD:
1101 		type = "ct pass-through";
1102 		break;
1103 	default:
1104 		qla_printk(KERN_WARNING, ha,
1105 		    "%s: Unrecognized SRB: (%p) type=%d.\n", func, sp,
1106 		    sp_bsg->type);
1107 		return;
1108 	}
1109 
1110 	comp_status = fw_status[0] = le16_to_cpu(pkt->comp_status);
1111 	fw_status[1] = le16_to_cpu(((struct els_sts_entry_24xx*)pkt)->error_subcode_1);
1112 	fw_status[2] = le16_to_cpu(((struct els_sts_entry_24xx*)pkt)->error_subcode_2);
1113 
1114 	/* return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
1115 	 * fc payload  to the caller
1116 	 */
1117 	bsg_job->reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
1118 	bsg_job->reply_len = sizeof(struct fc_bsg_reply) + sizeof(fw_status);
1119 
1120 	if (comp_status != CS_COMPLETE) {
1121 		if (comp_status == CS_DATA_UNDERRUN) {
1122 			bsg_job->reply->result = DID_OK << 16;
1123 			bsg_job->reply->reply_payload_rcv_len =
1124 				le16_to_cpu(((struct els_sts_entry_24xx*)pkt)->total_byte_count);
1125 
1126 			DEBUG2(qla_printk(KERN_WARNING, ha,
1127 			    "scsi(%ld:0x%x): ELS-CT pass-through-%s error comp_status-status=0x%x "
1128 			    "error subcode 1=0x%x error subcode 2=0x%x total_byte = 0x%x.\n",
1129 				vha->host_no, sp->handle, type, comp_status, fw_status[1], fw_status[2],
1130 				le16_to_cpu(((struct els_sts_entry_24xx*)pkt)->total_byte_count)));
1131 			fw_sts_ptr = ((uint8_t*)bsg_job->req->sense) + sizeof(struct fc_bsg_reply);
1132 			memcpy( fw_sts_ptr, fw_status, sizeof(fw_status));
1133 		}
1134 		else {
1135 			DEBUG2(qla_printk(KERN_WARNING, ha,
1136 			    "scsi(%ld:0x%x): ELS-CT pass-through-%s error comp_status-status=0x%x "
1137 			    "error subcode 1=0x%x error subcode 2=0x%x.\n",
1138 				vha->host_no, sp->handle, type, comp_status,
1139 				le16_to_cpu(((struct els_sts_entry_24xx*)pkt)->error_subcode_1),
1140 				le16_to_cpu(((struct els_sts_entry_24xx*)pkt)->error_subcode_2)));
1141 			bsg_job->reply->result = DID_ERROR << 16;
1142 			bsg_job->reply->reply_payload_rcv_len = 0;
1143 			fw_sts_ptr = ((uint8_t*)bsg_job->req->sense) + sizeof(struct fc_bsg_reply);
1144 			memcpy( fw_sts_ptr, fw_status, sizeof(fw_status));
1145 		}
1146 		DEBUG2(qla2x00_dump_buffer((uint8_t *)pkt, sizeof(*pkt)));
1147 	}
1148 	else {
1149 		bsg_job->reply->result =  DID_OK << 16;;
1150 		bsg_job->reply->reply_payload_rcv_len = bsg_job->reply_payload.payload_len;
1151 		bsg_job->reply_len = 0;
1152 	}
1153 
1154 	dma_unmap_sg(&ha->pdev->dev,
1155 	    bsg_job->request_payload.sg_list,
1156 	    bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1157 	dma_unmap_sg(&ha->pdev->dev,
1158 	    bsg_job->reply_payload.sg_list,
1159 	    bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1160 	if ((sp_bsg->type == SRB_ELS_CMD_HST) ||
1161 	    (sp_bsg->type == SRB_CT_CMD))
1162 		kfree(sp->fcport);
1163 	kfree(sp->ctx);
1164 	mempool_free(sp, ha->srb_mempool);
1165 	bsg_job->job_done(bsg_job);
1166 }
1167 
1168 static void
qla24xx_logio_entry(scsi_qla_host_t * vha,struct req_que * req,struct logio_entry_24xx * logio)1169 qla24xx_logio_entry(scsi_qla_host_t *vha, struct req_que *req,
1170     struct logio_entry_24xx *logio)
1171 {
1172 	const char func[] = "LOGIO-IOCB";
1173 	const char *type;
1174 	fc_port_t *fcport;
1175 	srb_t *sp;
1176 	struct srb_iocb *lio;
1177 	struct srb_ctx *ctx;
1178 	uint16_t *data;
1179 	uint32_t iop[2];
1180 
1181 	sp = qla2x00_get_sp_from_handle(vha, func, req, logio);
1182 	if (!sp)
1183 		return;
1184 
1185 	ctx = sp->ctx;
1186 	lio = ctx->u.iocb_cmd;
1187 	type = ctx->name;
1188 	fcport = sp->fcport;
1189 	data = lio->u.logio.data;
1190 
1191 	data[0] = MBS_COMMAND_ERROR;
1192 	data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
1193 		QLA_LOGIO_LOGIN_RETRIED : 0;
1194 	if (logio->entry_status) {
1195 		DEBUG2(printk(KERN_WARNING
1196 		    "scsi(%ld:%x): Async-%s error entry - "
1197 		    "portid=%02x%02x%02x entry-status=%x.\n",
1198 		    fcport->vha->host_no, sp->handle, type,
1199 		    fcport->d_id.b.domain, fcport->d_id.b.area,
1200 		    fcport->d_id.b.al_pa, logio->entry_status));
1201 		DEBUG2(qla2x00_dump_buffer((uint8_t *)logio, sizeof(*logio)));
1202 
1203 		goto logio_done;
1204 	}
1205 
1206 	if (le16_to_cpu(logio->comp_status) == CS_COMPLETE) {
1207 		DEBUG2(printk(KERN_DEBUG
1208 		    "scsi(%ld:%x): Async-%s complete - portid=%02x%02x%02x "
1209 		    "iop0=%x.\n",
1210 		    fcport->vha->host_no, sp->handle, type,
1211 		    fcport->d_id.b.domain, fcport->d_id.b.area,
1212 		    fcport->d_id.b.al_pa,
1213 		    le32_to_cpu(logio->io_parameter[0])));
1214 
1215 		data[0] = MBS_COMMAND_COMPLETE;
1216 		if (ctx->type != SRB_LOGIN_CMD)
1217 			goto logio_done;
1218 
1219 		iop[0] = le32_to_cpu(logio->io_parameter[0]);
1220 		if (iop[0] & BIT_4) {
1221 			fcport->port_type = FCT_TARGET;
1222 			if (iop[0] & BIT_8)
1223 				fcport->flags |= FCF_FCP2_DEVICE;
1224 		} else if (iop[0] & BIT_5)
1225 			fcport->port_type = FCT_INITIATOR;
1226 
1227 		if (logio->io_parameter[7] || logio->io_parameter[8])
1228 			fcport->supported_classes |= FC_COS_CLASS2;
1229 		if (logio->io_parameter[9] || logio->io_parameter[10])
1230 			fcport->supported_classes |= FC_COS_CLASS3;
1231 
1232 		goto logio_done;
1233 	}
1234 
1235 	iop[0] = le32_to_cpu(logio->io_parameter[0]);
1236 	iop[1] = le32_to_cpu(logio->io_parameter[1]);
1237 	switch (iop[0]) {
1238 	case LSC_SCODE_PORTID_USED:
1239 		data[0] = MBS_PORT_ID_USED;
1240 		data[1] = LSW(iop[1]);
1241 		break;
1242 	case LSC_SCODE_NPORT_USED:
1243 		data[0] = MBS_LOOP_ID_USED;
1244 		break;
1245 	default:
1246 		data[0] = MBS_COMMAND_ERROR;
1247 		break;
1248 	}
1249 
1250 	DEBUG2(printk(KERN_WARNING
1251 	    "scsi(%ld:%x): Async-%s failed - portid=%02x%02x%02x comp=%x "
1252 	    "iop0=%x iop1=%x.\n",
1253 	    fcport->vha->host_no, sp->handle, type, fcport->d_id.b.domain,
1254 	    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1255 	    le16_to_cpu(logio->comp_status),
1256 	    le32_to_cpu(logio->io_parameter[0]),
1257 	    le32_to_cpu(logio->io_parameter[1])));
1258 
1259 logio_done:
1260 	lio->done(sp);
1261 }
1262 
1263 static void
qla24xx_tm_iocb_entry(scsi_qla_host_t * vha,struct req_que * req,struct tsk_mgmt_entry * tsk)1264 qla24xx_tm_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1265     struct tsk_mgmt_entry *tsk)
1266 {
1267 	const char func[] = "TMF-IOCB";
1268 	const char *type;
1269 	fc_port_t *fcport;
1270 	srb_t *sp;
1271 	struct srb_iocb *iocb;
1272 	struct srb_ctx *ctx;
1273 	struct sts_entry_24xx *sts = (struct sts_entry_24xx *)tsk;
1274 	int error = 1;
1275 
1276 	sp = qla2x00_get_sp_from_handle(vha, func, req, tsk);
1277 	if (!sp)
1278 		return;
1279 
1280 	ctx = sp->ctx;
1281 	iocb = ctx->u.iocb_cmd;
1282 	type = ctx->name;
1283 	fcport = sp->fcport;
1284 
1285 	if (sts->entry_status) {
1286 		DEBUG2(printk(KERN_WARNING
1287 		    "scsi(%ld:%x): Async-%s error - entry-status(%x).\n",
1288 		    fcport->vha->host_no, sp->handle, type,
1289 		    sts->entry_status));
1290 	} else if (sts->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1291 		DEBUG2(printk(KERN_WARNING
1292 		    "scsi(%ld:%x): Async-%s error - completion status(%x).\n",
1293 		    fcport->vha->host_no, sp->handle, type,
1294 		    sts->comp_status));
1295 	} else if (!(le16_to_cpu(sts->scsi_status) &
1296 	    SS_RESPONSE_INFO_LEN_VALID)) {
1297 		DEBUG2(printk(KERN_WARNING
1298 		    "scsi(%ld:%x): Async-%s error - no response info(%x).\n",
1299 		    fcport->vha->host_no, sp->handle, type,
1300 		    sts->scsi_status));
1301 	} else if (le32_to_cpu(sts->rsp_data_len) < 4) {
1302 		DEBUG2(printk(KERN_WARNING
1303 		    "scsi(%ld:%x): Async-%s error - not enough response(%d).\n",
1304 		    fcport->vha->host_no, sp->handle, type,
1305 		    sts->rsp_data_len));
1306 	} else if (sts->data[3]) {
1307 		DEBUG2(printk(KERN_WARNING
1308 		    "scsi(%ld:%x): Async-%s error - response(%x).\n",
1309 		    fcport->vha->host_no, sp->handle, type,
1310 		    sts->data[3]));
1311 	} else {
1312 		error = 0;
1313 	}
1314 
1315 	if (error) {
1316 		iocb->u.tmf.data = error;
1317 		DEBUG2(qla2x00_dump_buffer((uint8_t *)sts, sizeof(*sts)));
1318 	}
1319 
1320 	iocb->done(sp);
1321 }
1322 
1323 /**
1324  * qla2x00_process_response_queue() - Process response queue entries.
1325  * @ha: SCSI driver HA context
1326  */
1327 void
qla2x00_process_response_queue(struct rsp_que * rsp)1328 qla2x00_process_response_queue(struct rsp_que *rsp)
1329 {
1330 	struct scsi_qla_host *vha;
1331 	struct qla_hw_data *ha = rsp->hw;
1332 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1333 	sts_entry_t	*pkt;
1334 	uint16_t        handle_cnt;
1335 	uint16_t        cnt;
1336 
1337 	vha = pci_get_drvdata(ha->pdev);
1338 
1339 	if (!vha->flags.online)
1340 		return;
1341 
1342 	while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
1343 		pkt = (sts_entry_t *)rsp->ring_ptr;
1344 
1345 		rsp->ring_index++;
1346 		if (rsp->ring_index == rsp->length) {
1347 			rsp->ring_index = 0;
1348 			rsp->ring_ptr = rsp->ring;
1349 		} else {
1350 			rsp->ring_ptr++;
1351 		}
1352 
1353 		if (pkt->entry_status != 0) {
1354 			DEBUG3(printk(KERN_INFO
1355 			    "scsi(%ld): Process error entry.\n", vha->host_no));
1356 
1357 			qla2x00_error_entry(vha, rsp, pkt);
1358 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1359 			wmb();
1360 			continue;
1361 		}
1362 
1363 		switch (pkt->entry_type) {
1364 		case STATUS_TYPE:
1365 			qla2x00_status_entry(vha, rsp, pkt);
1366 			break;
1367 		case STATUS_TYPE_21:
1368 			handle_cnt = ((sts21_entry_t *)pkt)->handle_count;
1369 			for (cnt = 0; cnt < handle_cnt; cnt++) {
1370 				qla2x00_process_completed_request(vha, rsp->req,
1371 				    ((sts21_entry_t *)pkt)->handle[cnt]);
1372 			}
1373 			break;
1374 		case STATUS_TYPE_22:
1375 			handle_cnt = ((sts22_entry_t *)pkt)->handle_count;
1376 			for (cnt = 0; cnt < handle_cnt; cnt++) {
1377 				qla2x00_process_completed_request(vha, rsp->req,
1378 				    ((sts22_entry_t *)pkt)->handle[cnt]);
1379 			}
1380 			break;
1381 		case STATUS_CONT_TYPE:
1382 			qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
1383 			break;
1384 		case MBX_IOCB_TYPE:
1385 			qla2x00_mbx_iocb_entry(vha, rsp->req,
1386 			    (struct mbx_entry *)pkt);
1387 			break;
1388 		case CT_IOCB_TYPE:
1389 			qla2x00_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
1390 			break;
1391 		default:
1392 			/* Type Not Supported. */
1393 			DEBUG4(printk(KERN_WARNING
1394 			    "scsi(%ld): Received unknown response pkt type %x "
1395 			    "entry status=%x.\n",
1396 			    vha->host_no, pkt->entry_type, pkt->entry_status));
1397 			break;
1398 		}
1399 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1400 		wmb();
1401 	}
1402 
1403 	/* Adjust ring index */
1404 	WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), rsp->ring_index);
1405 }
1406 
1407 static inline void
1408 
qla2x00_handle_sense(srb_t * sp,uint8_t * sense_data,uint32_t par_sense_len,uint32_t sense_len,struct rsp_que * rsp)1409 qla2x00_handle_sense(srb_t *sp, uint8_t *sense_data, uint32_t par_sense_len,
1410     uint32_t sense_len, struct rsp_que *rsp)
1411 {
1412 	struct scsi_cmnd *cp = sp->cmd;
1413 
1414 	if (sense_len >= SCSI_SENSE_BUFFERSIZE)
1415 		sense_len = SCSI_SENSE_BUFFERSIZE;
1416 
1417 	sp->request_sense_length = sense_len;
1418 	sp->request_sense_ptr = cp->sense_buffer;
1419 	if (sp->request_sense_length > par_sense_len)
1420 		sense_len = par_sense_len;
1421 
1422 	memcpy(cp->sense_buffer, sense_data, sense_len);
1423 
1424 	sp->request_sense_ptr += sense_len;
1425 	sp->request_sense_length -= sense_len;
1426 	if (sp->request_sense_length != 0)
1427 		rsp->status_srb = sp;
1428 
1429 	DEBUG5(printk("%s(): Check condition Sense data, scsi(%ld:%d:%d:%d) "
1430 	    "cmd=%p\n", __func__, sp->fcport->vha->host_no,
1431 	    cp->device->channel, cp->device->id, cp->device->lun, cp));
1432 	if (sense_len)
1433 		DEBUG5(qla2x00_dump_buffer(cp->sense_buffer, sense_len));
1434 }
1435 
1436 struct scsi_dif_tuple {
1437 	__be16 guard;       /* Checksum */
1438 	__be16 app_tag;         /* APPL identifer */
1439 	__be32 ref_tag;         /* Target LBA or indirect LBA */
1440 };
1441 
1442 /*
1443  * Checks the guard or meta-data for the type of error
1444  * detected by the HBA. In case of errors, we set the
1445  * ASC/ASCQ fields in the sense buffer with ILLEGAL_REQUEST
1446  * to indicate to the kernel that the HBA detected error.
1447  */
1448 static inline void
qla2x00_handle_dif_error(srb_t * sp,struct sts_entry_24xx * sts24)1449 qla2x00_handle_dif_error(srb_t *sp, struct sts_entry_24xx *sts24)
1450 {
1451 	struct scsi_cmnd *cmd = sp->cmd;
1452 	struct scsi_dif_tuple	*ep =
1453 			(struct scsi_dif_tuple *)&sts24->data[20];
1454 	struct scsi_dif_tuple	*ap =
1455 			(struct scsi_dif_tuple *)&sts24->data[12];
1456 	uint32_t	e_ref_tag, a_ref_tag;
1457 	uint16_t	e_app_tag, a_app_tag;
1458 	uint16_t	e_guard, a_guard;
1459 
1460 	e_ref_tag = be32_to_cpu(ep->ref_tag);
1461 	a_ref_tag = be32_to_cpu(ap->ref_tag);
1462 	e_app_tag = be16_to_cpu(ep->app_tag);
1463 	a_app_tag = be16_to_cpu(ap->app_tag);
1464 	e_guard = be16_to_cpu(ep->guard);
1465 	a_guard = be16_to_cpu(ap->guard);
1466 
1467 	DEBUG18(printk(KERN_DEBUG
1468 	    "%s(): iocb(s) %p Returned STATUS\n", __func__, sts24));
1469 
1470 	DEBUG18(printk(KERN_ERR "DIF ERROR in cmd 0x%x lba 0x%llx act ref"
1471 	    " tag=0x%x, exp ref_tag=0x%x, act app tag=0x%x, exp app"
1472 	    " tag=0x%x, act guard=0x%x, exp guard=0x%x\n",
1473 	    cmd->cmnd[0], (u64)scsi_get_lba(cmd), a_ref_tag, e_ref_tag,
1474 	    a_app_tag, e_app_tag, a_guard, e_guard));
1475 
1476 
1477 	/* check guard */
1478 	if (e_guard != a_guard) {
1479 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1480 		    0x10, 0x1);
1481 		set_driver_byte(cmd, DRIVER_SENSE);
1482 		set_host_byte(cmd, DID_ABORT);
1483 		cmd->result |= SAM_STAT_CHECK_CONDITION << 1;
1484 		return;
1485 	}
1486 
1487 	/* check appl tag */
1488 	if (e_app_tag != a_app_tag) {
1489 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1490 		    0x10, 0x2);
1491 		set_driver_byte(cmd, DRIVER_SENSE);
1492 		set_host_byte(cmd, DID_ABORT);
1493 		cmd->result |= SAM_STAT_CHECK_CONDITION << 1;
1494 		return;
1495 	}
1496 
1497 	/* check ref tag */
1498 	if (e_ref_tag != a_ref_tag) {
1499 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1500 		    0x10, 0x3);
1501 		set_driver_byte(cmd, DRIVER_SENSE);
1502 		set_host_byte(cmd, DID_ABORT);
1503 		cmd->result |= SAM_STAT_CHECK_CONDITION << 1;
1504 		return;
1505 	}
1506 }
1507 
1508 /**
1509  * qla2x00_status_entry() - Process a Status IOCB entry.
1510  * @ha: SCSI driver HA context
1511  * @pkt: Entry pointer
1512  */
1513 static void
qla2x00_status_entry(scsi_qla_host_t * vha,struct rsp_que * rsp,void * pkt)1514 qla2x00_status_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, void *pkt)
1515 {
1516 	srb_t		*sp;
1517 	fc_port_t	*fcport;
1518 	struct scsi_cmnd *cp;
1519 	sts_entry_t *sts;
1520 	struct sts_entry_24xx *sts24;
1521 	uint16_t	comp_status;
1522 	uint16_t	scsi_status;
1523 	uint16_t	ox_id;
1524 	uint8_t		lscsi_status;
1525 	int32_t		resid;
1526 	uint32_t sense_len, par_sense_len, rsp_info_len, resid_len,
1527 	    fw_resid_len;
1528 	uint8_t		*rsp_info, *sense_data;
1529 	struct qla_hw_data *ha = vha->hw;
1530 	uint32_t handle;
1531 	uint16_t que;
1532 	struct req_que *req;
1533 	int logit = 1;
1534 
1535 	sts = (sts_entry_t *) pkt;
1536 	sts24 = (struct sts_entry_24xx *) pkt;
1537 	if (IS_FWI2_CAPABLE(ha)) {
1538 		comp_status = le16_to_cpu(sts24->comp_status);
1539 		scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
1540 	} else {
1541 		comp_status = le16_to_cpu(sts->comp_status);
1542 		scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
1543 	}
1544 	handle = (uint32_t) LSW(sts->handle);
1545 	que = MSW(sts->handle);
1546 	req = ha->req_q_map[que];
1547 
1548 	/* Fast path completion. */
1549 	if (comp_status == CS_COMPLETE && scsi_status == 0) {
1550 		qla2x00_process_completed_request(vha, req, handle);
1551 
1552 		return;
1553 	}
1554 
1555 	/* Validate handle. */
1556 	if (handle < MAX_OUTSTANDING_COMMANDS) {
1557 		sp = req->outstanding_cmds[handle];
1558 		req->outstanding_cmds[handle] = NULL;
1559 	} else
1560 		sp = NULL;
1561 
1562 	if (sp == NULL) {
1563 		qla_printk(KERN_WARNING, ha,
1564 		    "scsi(%ld): Invalid status handle (0x%x).\n", vha->host_no,
1565 		    sts->handle);
1566 
1567 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1568 		qla2xxx_wake_dpc(vha);
1569 		return;
1570 	}
1571 	cp = sp->cmd;
1572 	if (cp == NULL) {
1573 		qla_printk(KERN_WARNING, ha,
1574 		    "scsi(%ld): Command already returned (0x%x/%p).\n",
1575 		    vha->host_no, sts->handle, sp);
1576 
1577 		return;
1578 	}
1579 
1580   	lscsi_status = scsi_status & STATUS_MASK;
1581 
1582 	fcport = sp->fcport;
1583 
1584 	ox_id = 0;
1585 	sense_len = par_sense_len = rsp_info_len = resid_len =
1586 	    fw_resid_len = 0;
1587 	if (IS_FWI2_CAPABLE(ha)) {
1588 		if (scsi_status & SS_SENSE_LEN_VALID)
1589 			sense_len = le32_to_cpu(sts24->sense_len);
1590 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
1591 			rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
1592 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER))
1593 			resid_len = le32_to_cpu(sts24->rsp_residual_count);
1594 		if (comp_status == CS_DATA_UNDERRUN)
1595 			fw_resid_len = le32_to_cpu(sts24->residual_len);
1596 		rsp_info = sts24->data;
1597 		sense_data = sts24->data;
1598 		host_to_fcp_swap(sts24->data, sizeof(sts24->data));
1599 		ox_id = le16_to_cpu(sts24->ox_id);
1600 		par_sense_len = sizeof(sts24->data);
1601 	} else {
1602 		if (scsi_status & SS_SENSE_LEN_VALID)
1603 			sense_len = le16_to_cpu(sts->req_sense_length);
1604 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
1605 			rsp_info_len = le16_to_cpu(sts->rsp_info_len);
1606 		resid_len = le32_to_cpu(sts->residual_length);
1607 		rsp_info = sts->rsp_info;
1608 		sense_data = sts->req_sense_data;
1609 		par_sense_len = sizeof(sts->req_sense_data);
1610 	}
1611 
1612 	/* Check for any FCP transport errors. */
1613 	if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
1614 		/* Sense data lies beyond any FCP RESPONSE data. */
1615 		if (IS_FWI2_CAPABLE(ha)) {
1616 			sense_data += rsp_info_len;
1617 			par_sense_len -= rsp_info_len;
1618 		}
1619 		if (rsp_info_len > 3 && rsp_info[3]) {
1620 			DEBUG2(qla_printk(KERN_INFO, ha,
1621 			    "scsi(%ld:%d:%d): FCP I/O protocol failure "
1622 			    "(0x%x/0x%x).\n", vha->host_no, cp->device->id,
1623 			    cp->device->lun, rsp_info_len, rsp_info[3]));
1624 
1625 			cp->result = DID_BUS_BUSY << 16;
1626 			goto out;
1627 		}
1628 	}
1629 
1630 	/* Check for overrun. */
1631 	if (IS_FWI2_CAPABLE(ha) && comp_status == CS_COMPLETE &&
1632 	    scsi_status & SS_RESIDUAL_OVER)
1633 		comp_status = CS_DATA_OVERRUN;
1634 
1635 	/*
1636 	 * Based on Host and scsi status generate status code for Linux
1637 	 */
1638 	switch (comp_status) {
1639 	case CS_COMPLETE:
1640 	case CS_QUEUE_FULL:
1641 		if (scsi_status == 0) {
1642 			cp->result = DID_OK << 16;
1643 			break;
1644 		}
1645 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
1646 			resid = resid_len;
1647 			scsi_set_resid(cp, resid);
1648 
1649 			if (!lscsi_status &&
1650 			    ((unsigned)(scsi_bufflen(cp) - resid) <
1651 			     cp->underflow)) {
1652 				qla_printk(KERN_INFO, ha,
1653 				    "scsi(%ld:%d:%d): Mid-layer underflow "
1654 				    "detected (0x%x of 0x%x bytes).\n",
1655 				    vha->host_no, cp->device->id,
1656 				    cp->device->lun, resid, scsi_bufflen(cp));
1657 
1658 				cp->result = DID_ERROR << 16;
1659 				break;
1660 			}
1661 		}
1662 		cp->result = DID_OK << 16 | lscsi_status;
1663 
1664 		if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1665 			DEBUG2(qla_printk(KERN_INFO, ha,
1666 			    "scsi(%ld:%d:%d) QUEUE FULL detected.\n",
1667 			    vha->host_no, cp->device->id, cp->device->lun));
1668 			break;
1669 		}
1670 		logit = 0;
1671 		if (lscsi_status != SS_CHECK_CONDITION)
1672 			break;
1673 
1674 		memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1675 		if (!(scsi_status & SS_SENSE_LEN_VALID))
1676 			break;
1677 
1678 		qla2x00_handle_sense(sp, sense_data, par_sense_len, sense_len,
1679 		    rsp);
1680 		break;
1681 
1682 	case CS_DATA_UNDERRUN:
1683 		/* Use F/W calculated residual length. */
1684 		resid = IS_FWI2_CAPABLE(ha) ? fw_resid_len : resid_len;
1685 		scsi_set_resid(cp, resid);
1686 		if (scsi_status & SS_RESIDUAL_UNDER) {
1687 			if (IS_FWI2_CAPABLE(ha) && fw_resid_len != resid_len) {
1688 				DEBUG2(qla_printk(KERN_INFO, ha,
1689 				    "scsi(%ld:%d:%d) Dropped frame(s) detected "
1690 				    "(0x%x of 0x%x bytes).\n", vha->host_no,
1691 				    cp->device->id, cp->device->lun, resid,
1692 				    scsi_bufflen(cp)));
1693 
1694 				cp->result = DID_ERROR << 16 | lscsi_status;
1695 				break;
1696 			}
1697 
1698 			if (!lscsi_status &&
1699 			    ((unsigned)(scsi_bufflen(cp) - resid) <
1700 			    cp->underflow)) {
1701 				qla_printk(KERN_INFO, ha,
1702 				    "scsi(%ld:%d:%d): Mid-layer underflow "
1703 				    "detected (0x%x of 0x%x bytes).\n",
1704 				    vha->host_no, cp->device->id,
1705 				    cp->device->lun, resid, scsi_bufflen(cp));
1706 
1707 				cp->result = DID_ERROR << 16;
1708 				break;
1709 			}
1710 		} else {
1711 			DEBUG2(qla_printk(KERN_INFO, ha,
1712 			    "scsi(%ld:%d:%d) Dropped frame(s) detected (0x%x "
1713 			    "of 0x%x bytes).\n", vha->host_no, cp->device->id,
1714 			    cp->device->lun, resid, scsi_bufflen(cp)));
1715 
1716 			cp->result = DID_ERROR << 16 | lscsi_status;
1717 			goto check_scsi_status;
1718 		}
1719 
1720 		cp->result = DID_OK << 16 | lscsi_status;
1721 		logit = 0;
1722 
1723 check_scsi_status:
1724 		/*
1725 		 * Check to see if SCSI Status is non zero. If so report SCSI
1726 		 * Status.
1727 		 */
1728 		if (lscsi_status != 0) {
1729 			if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1730 				DEBUG2(qla_printk(KERN_INFO, ha,
1731 				    "scsi(%ld:%d:%d) QUEUE FULL detected.\n",
1732 				    vha->host_no, cp->device->id,
1733 				    cp->device->lun));
1734 				logit = 1;
1735 				break;
1736 			}
1737 			if (lscsi_status != SS_CHECK_CONDITION)
1738 				break;
1739 
1740 			memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1741 			if (!(scsi_status & SS_SENSE_LEN_VALID))
1742 				break;
1743 
1744 			qla2x00_handle_sense(sp, sense_data, par_sense_len,
1745 			    sense_len, rsp);
1746 		}
1747 		break;
1748 
1749 	case CS_PORT_LOGGED_OUT:
1750 	case CS_PORT_CONFIG_CHG:
1751 	case CS_PORT_BUSY:
1752 	case CS_INCOMPLETE:
1753 	case CS_PORT_UNAVAILABLE:
1754 	case CS_TIMEOUT:
1755 	case CS_RESET:
1756 
1757 		/*
1758 		 * We are going to have the fc class block the rport
1759 		 * while we try to recover so instruct the mid layer
1760 		 * to requeue until the class decides how to handle this.
1761 		 */
1762 		cp->result = DID_TRANSPORT_DISRUPTED << 16;
1763 
1764 		if (comp_status == CS_TIMEOUT) {
1765 			if (IS_FWI2_CAPABLE(ha))
1766 				break;
1767 			else if ((le16_to_cpu(sts->status_flags) &
1768 			    SF_LOGOUT_SENT) == 0)
1769 				break;
1770 		}
1771 
1772 		DEBUG2(qla_printk(KERN_INFO, ha,
1773 			"scsi(%ld:%d:%d) Port down status: port-state=0x%x\n",
1774 			vha->host_no, cp->device->id, cp->device->lun,
1775 			atomic_read(&fcport->state)));
1776 
1777 		if (atomic_read(&fcport->state) == FCS_ONLINE)
1778 			qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
1779 		break;
1780 
1781 	case CS_ABORTED:
1782 		cp->result = DID_RESET << 16;
1783 		break;
1784 
1785 	case CS_DIF_ERROR:
1786 		qla2x00_handle_dif_error(sp, sts24);
1787 		break;
1788 	default:
1789 		cp->result = DID_ERROR << 16;
1790 		break;
1791 	}
1792 
1793 out:
1794 	if (logit)
1795 		DEBUG2(qla_printk(KERN_INFO, ha,
1796 		    "scsi(%ld:%d:%d) FCP command status: 0x%x-0x%x (0x%x) "
1797 		    "oxid=0x%x cdb=%02x%02x%02x len=0x%x "
1798 		    "rsp_info=0x%x resid=0x%x fw_resid=0x%x\n", vha->host_no,
1799 		    cp->device->id, cp->device->lun, comp_status, scsi_status,
1800 		    cp->result, ox_id, cp->cmnd[0],
1801 		    cp->cmnd[1], cp->cmnd[2], scsi_bufflen(cp), rsp_info_len,
1802 		    resid_len, fw_resid_len));
1803 
1804 	if (rsp->status_srb == NULL)
1805 		qla2x00_sp_compl(ha, sp);
1806 }
1807 
1808 /**
1809  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
1810  * @ha: SCSI driver HA context
1811  * @pkt: Entry pointer
1812  *
1813  * Extended sense data.
1814  */
1815 static void
qla2x00_status_cont_entry(struct rsp_que * rsp,sts_cont_entry_t * pkt)1816 qla2x00_status_cont_entry(struct rsp_que *rsp, sts_cont_entry_t *pkt)
1817 {
1818 	uint8_t		sense_sz = 0;
1819 	struct qla_hw_data *ha = rsp->hw;
1820 	srb_t		*sp = rsp->status_srb;
1821 	struct scsi_cmnd *cp;
1822 
1823 	if (sp != NULL && sp->request_sense_length != 0) {
1824 		cp = sp->cmd;
1825 		if (cp == NULL) {
1826 			DEBUG2(printk("%s(): Cmd already returned back to OS "
1827 			    "sp=%p.\n", __func__, sp));
1828 			qla_printk(KERN_INFO, ha,
1829 			    "cmd is NULL: already returned to OS (sp=%p)\n",
1830 			    sp);
1831 
1832 			rsp->status_srb = NULL;
1833 			return;
1834 		}
1835 
1836 		if (sp->request_sense_length > sizeof(pkt->data)) {
1837 			sense_sz = sizeof(pkt->data);
1838 		} else {
1839 			sense_sz = sp->request_sense_length;
1840 		}
1841 
1842 		/* Move sense data. */
1843 		if (IS_FWI2_CAPABLE(ha))
1844 			host_to_fcp_swap(pkt->data, sizeof(pkt->data));
1845 		memcpy(sp->request_sense_ptr, pkt->data, sense_sz);
1846 		DEBUG5(qla2x00_dump_buffer(sp->request_sense_ptr, sense_sz));
1847 
1848 		sp->request_sense_ptr += sense_sz;
1849 		sp->request_sense_length -= sense_sz;
1850 
1851 		/* Place command on done queue. */
1852 		if (sp->request_sense_length == 0) {
1853 			rsp->status_srb = NULL;
1854 			qla2x00_sp_compl(ha, sp);
1855 		}
1856 	}
1857 }
1858 
1859 /**
1860  * qla2x00_error_entry() - Process an error entry.
1861  * @ha: SCSI driver HA context
1862  * @pkt: Entry pointer
1863  */
1864 static void
qla2x00_error_entry(scsi_qla_host_t * vha,struct rsp_que * rsp,sts_entry_t * pkt)1865 qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
1866 {
1867 	srb_t *sp;
1868 	struct qla_hw_data *ha = vha->hw;
1869 	uint32_t handle = LSW(pkt->handle);
1870 	uint16_t que = MSW(pkt->handle);
1871 	struct req_que *req = ha->req_q_map[que];
1872 #if defined(QL_DEBUG_LEVEL_2)
1873 	if (pkt->entry_status & RF_INV_E_ORDER)
1874 		qla_printk(KERN_ERR, ha, "%s: Invalid Entry Order\n", __func__);
1875 	else if (pkt->entry_status & RF_INV_E_COUNT)
1876 		qla_printk(KERN_ERR, ha, "%s: Invalid Entry Count\n", __func__);
1877 	else if (pkt->entry_status & RF_INV_E_PARAM)
1878 		qla_printk(KERN_ERR, ha,
1879 		    "%s: Invalid Entry Parameter\n", __func__);
1880 	else if (pkt->entry_status & RF_INV_E_TYPE)
1881 		qla_printk(KERN_ERR, ha, "%s: Invalid Entry Type\n", __func__);
1882 	else if (pkt->entry_status & RF_BUSY)
1883 		qla_printk(KERN_ERR, ha, "%s: Busy\n", __func__);
1884 	else
1885 		qla_printk(KERN_ERR, ha, "%s: UNKNOWN flag error\n", __func__);
1886 #endif
1887 
1888 	/* Validate handle. */
1889 	if (handle < MAX_OUTSTANDING_COMMANDS)
1890 		sp = req->outstanding_cmds[handle];
1891 	else
1892 		sp = NULL;
1893 
1894 	if (sp) {
1895 		/* Free outstanding command slot. */
1896 		req->outstanding_cmds[handle] = NULL;
1897 
1898 		/* Bad payload or header */
1899 		if (pkt->entry_status &
1900 		    (RF_INV_E_ORDER | RF_INV_E_COUNT |
1901 		     RF_INV_E_PARAM | RF_INV_E_TYPE)) {
1902 			sp->cmd->result = DID_ERROR << 16;
1903 		} else if (pkt->entry_status & RF_BUSY) {
1904 			sp->cmd->result = DID_BUS_BUSY << 16;
1905 		} else {
1906 			sp->cmd->result = DID_ERROR << 16;
1907 		}
1908 		qla2x00_sp_compl(ha, sp);
1909 
1910 	} else if (pkt->entry_type == COMMAND_A64_TYPE || pkt->entry_type ==
1911 	    COMMAND_TYPE || pkt->entry_type == COMMAND_TYPE_7) {
1912 		DEBUG2(printk("scsi(%ld): Error entry - invalid handle\n",
1913 		    vha->host_no));
1914 		qla_printk(KERN_WARNING, ha,
1915 		    "Error entry - invalid handle\n");
1916 
1917 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1918 		qla2xxx_wake_dpc(vha);
1919 	}
1920 }
1921 
1922 /**
1923  * qla24xx_mbx_completion() - Process mailbox command completions.
1924  * @ha: SCSI driver HA context
1925  * @mb0: Mailbox0 register
1926  */
1927 static void
qla24xx_mbx_completion(scsi_qla_host_t * vha,uint16_t mb0)1928 qla24xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
1929 {
1930 	uint16_t	cnt;
1931 	uint16_t __iomem *wptr;
1932 	struct qla_hw_data *ha = vha->hw;
1933 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1934 
1935 	/* Load return mailbox registers. */
1936 	ha->flags.mbox_int = 1;
1937 	ha->mailbox_out[0] = mb0;
1938 	wptr = (uint16_t __iomem *)&reg->mailbox1;
1939 
1940 	for (cnt = 1; cnt < ha->mbx_count; cnt++) {
1941 		ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
1942 		wptr++;
1943 	}
1944 
1945 	if (ha->mcp) {
1946 		DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
1947 		    __func__, vha->host_no, ha->mcp->mb[0]));
1948 	} else {
1949 		DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
1950 		    __func__, vha->host_no));
1951 	}
1952 }
1953 
1954 /**
1955  * qla24xx_process_response_queue() - Process response queue entries.
1956  * @ha: SCSI driver HA context
1957  */
qla24xx_process_response_queue(struct scsi_qla_host * vha,struct rsp_que * rsp)1958 void qla24xx_process_response_queue(struct scsi_qla_host *vha,
1959 	struct rsp_que *rsp)
1960 {
1961 	struct sts_entry_24xx *pkt;
1962 	struct qla_hw_data *ha = vha->hw;
1963 
1964 	if (!vha->flags.online)
1965 		return;
1966 
1967 	while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
1968 		pkt = (struct sts_entry_24xx *)rsp->ring_ptr;
1969 
1970 		rsp->ring_index++;
1971 		if (rsp->ring_index == rsp->length) {
1972 			rsp->ring_index = 0;
1973 			rsp->ring_ptr = rsp->ring;
1974 		} else {
1975 			rsp->ring_ptr++;
1976 		}
1977 
1978 		if (pkt->entry_status != 0) {
1979 			DEBUG3(printk(KERN_INFO
1980 			    "scsi(%ld): Process error entry.\n", vha->host_no));
1981 
1982 			qla2x00_error_entry(vha, rsp, (sts_entry_t *) pkt);
1983 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1984 			wmb();
1985 			continue;
1986 		}
1987 
1988 		switch (pkt->entry_type) {
1989 		case STATUS_TYPE:
1990 			qla2x00_status_entry(vha, rsp, pkt);
1991 			break;
1992 		case STATUS_CONT_TYPE:
1993 			qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
1994 			break;
1995 		case VP_RPT_ID_IOCB_TYPE:
1996 			qla24xx_report_id_acquisition(vha,
1997 			    (struct vp_rpt_id_entry_24xx *)pkt);
1998 			break;
1999 		case LOGINOUT_PORT_IOCB_TYPE:
2000 			qla24xx_logio_entry(vha, rsp->req,
2001 			    (struct logio_entry_24xx *)pkt);
2002 			break;
2003 		case TSK_MGMT_IOCB_TYPE:
2004 			qla24xx_tm_iocb_entry(vha, rsp->req,
2005 			    (struct tsk_mgmt_entry *)pkt);
2006 			break;
2007                 case CT_IOCB_TYPE:
2008 			qla24xx_els_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
2009 			clear_bit(MBX_INTERRUPT, &vha->hw->mbx_cmd_flags);
2010 			break;
2011                 case ELS_IOCB_TYPE:
2012 			qla24xx_els_ct_entry(vha, rsp->req, pkt, ELS_IOCB_TYPE);
2013 			break;
2014 		default:
2015 			/* Type Not Supported. */
2016 			DEBUG4(printk(KERN_WARNING
2017 			    "scsi(%ld): Received unknown response pkt type %x "
2018 			    "entry status=%x.\n",
2019 			    vha->host_no, pkt->entry_type, pkt->entry_status));
2020 			break;
2021 		}
2022 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
2023 		wmb();
2024 	}
2025 
2026 	/* Adjust ring index */
2027 	if (IS_QLA82XX(ha)) {
2028 		struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
2029 		WRT_REG_DWORD(&reg->rsp_q_out[0], rsp->ring_index);
2030 	} else
2031 		WRT_REG_DWORD(rsp->rsp_q_out, rsp->ring_index);
2032 }
2033 
2034 static void
qla2xxx_check_risc_status(scsi_qla_host_t * vha)2035 qla2xxx_check_risc_status(scsi_qla_host_t *vha)
2036 {
2037 	int rval;
2038 	uint32_t cnt;
2039 	struct qla_hw_data *ha = vha->hw;
2040 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2041 
2042 	if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha))
2043 		return;
2044 
2045 	rval = QLA_SUCCESS;
2046 	WRT_REG_DWORD(&reg->iobase_addr, 0x7C00);
2047 	RD_REG_DWORD(&reg->iobase_addr);
2048 	WRT_REG_DWORD(&reg->iobase_window, 0x0001);
2049 	for (cnt = 10000; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
2050 	    rval == QLA_SUCCESS; cnt--) {
2051 		if (cnt) {
2052 			WRT_REG_DWORD(&reg->iobase_window, 0x0001);
2053 			udelay(10);
2054 		} else
2055 			rval = QLA_FUNCTION_TIMEOUT;
2056 	}
2057 	if (rval == QLA_SUCCESS)
2058 		goto next_test;
2059 
2060 	WRT_REG_DWORD(&reg->iobase_window, 0x0003);
2061 	for (cnt = 100; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
2062 	    rval == QLA_SUCCESS; cnt--) {
2063 		if (cnt) {
2064 			WRT_REG_DWORD(&reg->iobase_window, 0x0003);
2065 			udelay(10);
2066 		} else
2067 			rval = QLA_FUNCTION_TIMEOUT;
2068 	}
2069 	if (rval != QLA_SUCCESS)
2070 		goto done;
2071 
2072 next_test:
2073 	if (RD_REG_DWORD(&reg->iobase_c8) & BIT_3)
2074 		qla_printk(KERN_INFO, ha, "Additional code -- 0x55AA.\n");
2075 
2076 done:
2077 	WRT_REG_DWORD(&reg->iobase_window, 0x0000);
2078 	RD_REG_DWORD(&reg->iobase_window);
2079 }
2080 
2081 /**
2082  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
2083  * @irq:
2084  * @dev_id: SCSI driver HA context
2085  *
2086  * Called by system whenever the host adapter generates an interrupt.
2087  *
2088  * Returns handled flag.
2089  */
2090 irqreturn_t
qla24xx_intr_handler(int irq,void * dev_id)2091 qla24xx_intr_handler(int irq, void *dev_id)
2092 {
2093 	scsi_qla_host_t	*vha;
2094 	struct qla_hw_data *ha;
2095 	struct device_reg_24xx __iomem *reg;
2096 	int		status;
2097 	unsigned long	iter;
2098 	uint32_t	stat;
2099 	uint32_t	hccr;
2100 	uint16_t	mb[4];
2101 	struct rsp_que *rsp;
2102 	unsigned long	flags;
2103 
2104 	rsp = (struct rsp_que *) dev_id;
2105 	if (!rsp) {
2106 		printk(KERN_INFO
2107 		    "%s(): NULL response queue pointer\n", __func__);
2108 		return IRQ_NONE;
2109 	}
2110 
2111 	ha = rsp->hw;
2112 	reg = &ha->iobase->isp24;
2113 	status = 0;
2114 
2115 	if (unlikely(pci_channel_offline(ha->pdev)))
2116 		return IRQ_HANDLED;
2117 
2118 	spin_lock_irqsave(&ha->hardware_lock, flags);
2119 	vha = pci_get_drvdata(ha->pdev);
2120 	for (iter = 50; iter--; ) {
2121 		stat = RD_REG_DWORD(&reg->host_status);
2122 		if (stat & HSRX_RISC_PAUSED) {
2123 			if (unlikely(pci_channel_offline(ha->pdev)))
2124 				break;
2125 
2126 			hccr = RD_REG_DWORD(&reg->hccr);
2127 
2128 			qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
2129 			    "Dumping firmware!\n", hccr);
2130 
2131 			qla2xxx_check_risc_status(vha);
2132 
2133 			ha->isp_ops->fw_dump(vha, 1);
2134 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2135 			break;
2136 		} else if ((stat & HSRX_RISC_INT) == 0)
2137 			break;
2138 
2139 		switch (stat & 0xff) {
2140 		case 0x1:
2141 		case 0x2:
2142 		case 0x10:
2143 		case 0x11:
2144 			qla24xx_mbx_completion(vha, MSW(stat));
2145 			status |= MBX_INTERRUPT;
2146 
2147 			break;
2148 		case 0x12:
2149 			mb[0] = MSW(stat);
2150 			mb[1] = RD_REG_WORD(&reg->mailbox1);
2151 			mb[2] = RD_REG_WORD(&reg->mailbox2);
2152 			mb[3] = RD_REG_WORD(&reg->mailbox3);
2153 			qla2x00_async_event(vha, rsp, mb);
2154 			break;
2155 		case 0x13:
2156 		case 0x14:
2157 			qla24xx_process_response_queue(vha, rsp);
2158 			break;
2159 		default:
2160 			DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
2161 			    "(%d).\n",
2162 			    vha->host_no, stat & 0xff));
2163 			break;
2164 		}
2165 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
2166 		RD_REG_DWORD_RELAXED(&reg->hccr);
2167 	}
2168 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2169 
2170 	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
2171 	    (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
2172 		set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
2173 		complete(&ha->mbx_intr_comp);
2174 	}
2175 
2176 	return IRQ_HANDLED;
2177 }
2178 
2179 static irqreturn_t
qla24xx_msix_rsp_q(int irq,void * dev_id)2180 qla24xx_msix_rsp_q(int irq, void *dev_id)
2181 {
2182 	struct qla_hw_data *ha;
2183 	struct rsp_que *rsp;
2184 	struct device_reg_24xx __iomem *reg;
2185 	struct scsi_qla_host *vha;
2186 	unsigned long flags;
2187 
2188 	rsp = (struct rsp_que *) dev_id;
2189 	if (!rsp) {
2190 		printk(KERN_INFO
2191 		"%s(): NULL response queue pointer\n", __func__);
2192 		return IRQ_NONE;
2193 	}
2194 	ha = rsp->hw;
2195 	reg = &ha->iobase->isp24;
2196 
2197 	spin_lock_irqsave(&ha->hardware_lock, flags);
2198 
2199 	vha = pci_get_drvdata(ha->pdev);
2200 	qla24xx_process_response_queue(vha, rsp);
2201 	if (!ha->flags.disable_msix_handshake) {
2202 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
2203 		RD_REG_DWORD_RELAXED(&reg->hccr);
2204 	}
2205 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2206 
2207 	return IRQ_HANDLED;
2208 }
2209 
2210 static irqreturn_t
qla25xx_msix_rsp_q(int irq,void * dev_id)2211 qla25xx_msix_rsp_q(int irq, void *dev_id)
2212 {
2213 	struct qla_hw_data *ha;
2214 	struct rsp_que *rsp;
2215 	struct device_reg_24xx __iomem *reg;
2216 	unsigned long flags;
2217 
2218 	rsp = (struct rsp_que *) dev_id;
2219 	if (!rsp) {
2220 		printk(KERN_INFO
2221 			"%s(): NULL response queue pointer\n", __func__);
2222 		return IRQ_NONE;
2223 	}
2224 	ha = rsp->hw;
2225 
2226 	/* Clear the interrupt, if enabled, for this response queue */
2227 	if (rsp->options & ~BIT_6) {
2228 		reg = &ha->iobase->isp24;
2229 		spin_lock_irqsave(&ha->hardware_lock, flags);
2230 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
2231 		RD_REG_DWORD_RELAXED(&reg->hccr);
2232 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
2233 	}
2234 	queue_work_on((int) (rsp->id - 1), ha->wq, &rsp->q_work);
2235 
2236 	return IRQ_HANDLED;
2237 }
2238 
2239 static irqreturn_t
qla24xx_msix_default(int irq,void * dev_id)2240 qla24xx_msix_default(int irq, void *dev_id)
2241 {
2242 	scsi_qla_host_t	*vha;
2243 	struct qla_hw_data *ha;
2244 	struct rsp_que *rsp;
2245 	struct device_reg_24xx __iomem *reg;
2246 	int		status;
2247 	uint32_t	stat;
2248 	uint32_t	hccr;
2249 	uint16_t	mb[4];
2250 	unsigned long flags;
2251 
2252 	rsp = (struct rsp_que *) dev_id;
2253 	if (!rsp) {
2254 		DEBUG(printk(
2255 		"%s(): NULL response queue pointer\n", __func__));
2256 		return IRQ_NONE;
2257 	}
2258 	ha = rsp->hw;
2259 	reg = &ha->iobase->isp24;
2260 	status = 0;
2261 
2262 	spin_lock_irqsave(&ha->hardware_lock, flags);
2263 	vha = pci_get_drvdata(ha->pdev);
2264 	do {
2265 		stat = RD_REG_DWORD(&reg->host_status);
2266 		if (stat & HSRX_RISC_PAUSED) {
2267 			if (unlikely(pci_channel_offline(ha->pdev)))
2268 				break;
2269 
2270 			hccr = RD_REG_DWORD(&reg->hccr);
2271 
2272 			qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
2273 			    "Dumping firmware!\n", hccr);
2274 
2275 			qla2xxx_check_risc_status(vha);
2276 
2277 			ha->isp_ops->fw_dump(vha, 1);
2278 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2279 			break;
2280 		} else if ((stat & HSRX_RISC_INT) == 0)
2281 			break;
2282 
2283 		switch (stat & 0xff) {
2284 		case 0x1:
2285 		case 0x2:
2286 		case 0x10:
2287 		case 0x11:
2288 			qla24xx_mbx_completion(vha, MSW(stat));
2289 			status |= MBX_INTERRUPT;
2290 
2291 			break;
2292 		case 0x12:
2293 			mb[0] = MSW(stat);
2294 			mb[1] = RD_REG_WORD(&reg->mailbox1);
2295 			mb[2] = RD_REG_WORD(&reg->mailbox2);
2296 			mb[3] = RD_REG_WORD(&reg->mailbox3);
2297 			qla2x00_async_event(vha, rsp, mb);
2298 			break;
2299 		case 0x13:
2300 		case 0x14:
2301 			qla24xx_process_response_queue(vha, rsp);
2302 			break;
2303 		default:
2304 			DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
2305 			    "(%d).\n",
2306 			    vha->host_no, stat & 0xff));
2307 			break;
2308 		}
2309 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
2310 	} while (0);
2311 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2312 
2313 	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
2314 	    (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
2315 		set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
2316 		complete(&ha->mbx_intr_comp);
2317 	}
2318 	return IRQ_HANDLED;
2319 }
2320 
2321 /* Interrupt handling helpers. */
2322 
2323 struct qla_init_msix_entry {
2324 	const char *name;
2325 	irq_handler_t handler;
2326 };
2327 
2328 static struct qla_init_msix_entry msix_entries[3] = {
2329 	{ "qla2xxx (default)", qla24xx_msix_default },
2330 	{ "qla2xxx (rsp_q)", qla24xx_msix_rsp_q },
2331 	{ "qla2xxx (multiq)", qla25xx_msix_rsp_q },
2332 };
2333 
2334 static struct qla_init_msix_entry qla82xx_msix_entries[2] = {
2335 	{ "qla2xxx (default)", qla82xx_msix_default },
2336 	{ "qla2xxx (rsp_q)", qla82xx_msix_rsp_q },
2337 };
2338 
2339 static void
qla24xx_disable_msix(struct qla_hw_data * ha)2340 qla24xx_disable_msix(struct qla_hw_data *ha)
2341 {
2342 	int i;
2343 	struct qla_msix_entry *qentry;
2344 
2345 	for (i = 0; i < ha->msix_count; i++) {
2346 		qentry = &ha->msix_entries[i];
2347 		if (qentry->have_irq)
2348 			free_irq(qentry->vector, qentry->rsp);
2349 	}
2350 	pci_disable_msix(ha->pdev);
2351 	kfree(ha->msix_entries);
2352 	ha->msix_entries = NULL;
2353 	ha->flags.msix_enabled = 0;
2354 }
2355 
2356 static int
qla24xx_enable_msix(struct qla_hw_data * ha,struct rsp_que * rsp)2357 qla24xx_enable_msix(struct qla_hw_data *ha, struct rsp_que *rsp)
2358 {
2359 #define MIN_MSIX_COUNT	2
2360 	int i, ret;
2361 	struct msix_entry *entries;
2362 	struct qla_msix_entry *qentry;
2363 
2364 	entries = kzalloc(sizeof(struct msix_entry) * ha->msix_count,
2365 			GFP_KERNEL);
2366 	if (!entries)
2367 		return -ENOMEM;
2368 
2369 	for (i = 0; i < ha->msix_count; i++)
2370 		entries[i].entry = i;
2371 
2372 	ret = pci_enable_msix(ha->pdev, entries, ha->msix_count);
2373 	if (ret) {
2374 		if (ret < MIN_MSIX_COUNT)
2375 			goto msix_failed;
2376 
2377 		qla_printk(KERN_WARNING, ha,
2378 			"MSI-X: Failed to enable support -- %d/%d\n"
2379 			" Retry with %d vectors\n", ha->msix_count, ret, ret);
2380 		ha->msix_count = ret;
2381 		ret = pci_enable_msix(ha->pdev, entries, ha->msix_count);
2382 		if (ret) {
2383 msix_failed:
2384 			qla_printk(KERN_WARNING, ha, "MSI-X: Failed to enable"
2385 				" support, giving up -- %d/%d\n",
2386 				ha->msix_count, ret);
2387 			goto msix_out;
2388 		}
2389 		ha->max_rsp_queues = ha->msix_count - 1;
2390 	}
2391 	ha->msix_entries = kzalloc(sizeof(struct qla_msix_entry) *
2392 				ha->msix_count, GFP_KERNEL);
2393 	if (!ha->msix_entries) {
2394 		ret = -ENOMEM;
2395 		goto msix_out;
2396 	}
2397 	ha->flags.msix_enabled = 1;
2398 
2399 	for (i = 0; i < ha->msix_count; i++) {
2400 		qentry = &ha->msix_entries[i];
2401 		qentry->vector = entries[i].vector;
2402 		qentry->entry = entries[i].entry;
2403 		qentry->have_irq = 0;
2404 		qentry->rsp = NULL;
2405 	}
2406 
2407 	/* Enable MSI-X vectors for the base queue */
2408 	for (i = 0; i < 2; i++) {
2409 		qentry = &ha->msix_entries[i];
2410 		if (IS_QLA82XX(ha)) {
2411 			ret = request_irq(qentry->vector,
2412 				qla82xx_msix_entries[i].handler,
2413 				0, qla82xx_msix_entries[i].name, rsp);
2414 		} else {
2415 			ret = request_irq(qentry->vector,
2416 				msix_entries[i].handler,
2417 				0, msix_entries[i].name, rsp);
2418 		}
2419 		if (ret) {
2420 			qla_printk(KERN_WARNING, ha,
2421 			"MSI-X: Unable to register handler -- %x/%d.\n",
2422 			qentry->vector, ret);
2423 			qla24xx_disable_msix(ha);
2424 			ha->mqenable = 0;
2425 			goto msix_out;
2426 		}
2427 		qentry->have_irq = 1;
2428 		qentry->rsp = rsp;
2429 		rsp->msix = qentry;
2430 	}
2431 
2432 	/* Enable MSI-X vector for response queue update for queue 0 */
2433 	if (ha->mqiobase &&  (ha->max_rsp_queues > 1 || ha->max_req_queues > 1))
2434 		ha->mqenable = 1;
2435 
2436 msix_out:
2437 	kfree(entries);
2438 	return ret;
2439 }
2440 
2441 int
qla2x00_request_irqs(struct qla_hw_data * ha,struct rsp_que * rsp)2442 qla2x00_request_irqs(struct qla_hw_data *ha, struct rsp_que *rsp)
2443 {
2444 	int ret;
2445 	device_reg_t __iomem *reg = ha->iobase;
2446 
2447 	/* If possible, enable MSI-X. */
2448 	if (!IS_QLA2432(ha) && !IS_QLA2532(ha) &&
2449 		!IS_QLA8432(ha) && !IS_QLA8XXX_TYPE(ha))
2450 		goto skip_msi;
2451 
2452 	if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_HP &&
2453 		(ha->pdev->subsystem_device == 0x7040 ||
2454 		ha->pdev->subsystem_device == 0x7041 ||
2455 		ha->pdev->subsystem_device == 0x1705)) {
2456 		DEBUG2(qla_printk(KERN_WARNING, ha,
2457 			"MSI-X: Unsupported ISP2432 SSVID/SSDID (0x%X,0x%X).\n",
2458 			ha->pdev->subsystem_vendor,
2459 			ha->pdev->subsystem_device));
2460 		goto skip_msi;
2461 	}
2462 
2463 	if (IS_QLA2432(ha) && (ha->pdev->revision < QLA_MSIX_CHIP_REV_24XX ||
2464 		!QLA_MSIX_FW_MODE_1(ha->fw_attributes))) {
2465 		DEBUG2(qla_printk(KERN_WARNING, ha,
2466 		"MSI-X: Unsupported ISP2432 (0x%X, 0x%X).\n",
2467 			ha->pdev->revision, ha->fw_attributes));
2468 		goto skip_msix;
2469 	}
2470 
2471 	ret = qla24xx_enable_msix(ha, rsp);
2472 	if (!ret) {
2473 		DEBUG2(qla_printk(KERN_INFO, ha,
2474 		    "MSI-X: Enabled (0x%X, 0x%X).\n", ha->chip_revision,
2475 		    ha->fw_attributes));
2476 		goto clear_risc_ints;
2477 	}
2478 	qla_printk(KERN_WARNING, ha,
2479 	    "MSI-X: Falling back-to MSI mode -- %d.\n", ret);
2480 skip_msix:
2481 
2482 	if (!IS_QLA24XX(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha) &&
2483 	    !IS_QLA8001(ha))
2484 		goto skip_msi;
2485 
2486 	ret = pci_enable_msi(ha->pdev);
2487 	if (!ret) {
2488 		DEBUG2(qla_printk(KERN_INFO, ha, "MSI: Enabled.\n"));
2489 		ha->flags.msi_enabled = 1;
2490 	} else
2491 		qla_printk(KERN_WARNING, ha,
2492 		    "MSI-X: Falling back-to INTa mode -- %d.\n", ret);
2493 skip_msi:
2494 
2495 	ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
2496 	    ha->flags.msi_enabled ? 0 : IRQF_SHARED,
2497 	    QLA2XXX_DRIVER_NAME, rsp);
2498 	if (ret) {
2499 		qla_printk(KERN_WARNING, ha,
2500 		    "Failed to reserve interrupt %d already in use.\n",
2501 		    ha->pdev->irq);
2502 		goto fail;
2503 	}
2504 
2505 clear_risc_ints:
2506 
2507 	/*
2508 	 * FIXME: Noted that 8014s were being dropped during NK testing.
2509 	 * Timing deltas during MSI-X/INTa transitions?
2510 	 */
2511 	if (IS_QLA81XX(ha) || IS_QLA82XX(ha))
2512 		goto fail;
2513 	spin_lock_irq(&ha->hardware_lock);
2514 	if (IS_FWI2_CAPABLE(ha)) {
2515 		WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
2516 		WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
2517 	} else {
2518 		WRT_REG_WORD(&reg->isp.semaphore, 0);
2519 		WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
2520 		WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
2521 	}
2522 	spin_unlock_irq(&ha->hardware_lock);
2523 
2524 fail:
2525 	return ret;
2526 }
2527 
2528 void
qla2x00_free_irqs(scsi_qla_host_t * vha)2529 qla2x00_free_irqs(scsi_qla_host_t *vha)
2530 {
2531 	struct qla_hw_data *ha = vha->hw;
2532 	struct rsp_que *rsp = ha->rsp_q_map[0];
2533 
2534 	if (ha->flags.msix_enabled)
2535 		qla24xx_disable_msix(ha);
2536 	else if (ha->flags.msi_enabled) {
2537 		free_irq(ha->pdev->irq, rsp);
2538 		pci_disable_msi(ha->pdev);
2539 	} else
2540 		free_irq(ha->pdev->irq, rsp);
2541 }
2542 
2543 
qla25xx_request_irq(struct rsp_que * rsp)2544 int qla25xx_request_irq(struct rsp_que *rsp)
2545 {
2546 	struct qla_hw_data *ha = rsp->hw;
2547 	struct qla_init_msix_entry *intr = &msix_entries[2];
2548 	struct qla_msix_entry *msix = rsp->msix;
2549 	int ret;
2550 
2551 	ret = request_irq(msix->vector, intr->handler, 0, intr->name, rsp);
2552 	if (ret) {
2553 		qla_printk(KERN_WARNING, ha,
2554 			"MSI-X: Unable to register handler -- %x/%d.\n",
2555 			msix->vector, ret);
2556 		return ret;
2557 	}
2558 	msix->have_irq = 1;
2559 	msix->rsp = rsp;
2560 	return ret;
2561 }
2562