| Andrew Vasquez | fa90c54 | 2005-10-27 11:10:08 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * QLogic Fibre Channel HBA Driver | 
 | 3 |  * Copyright (c)  2003-2005 QLogic Corporation | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  * | 
| Andrew Vasquez | fa90c54 | 2005-10-27 11:10:08 -0700 | [diff] [blame] | 5 |  * See LICENSE.qla2xxx for copyright and licensing details. | 
 | 6 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "qla_def.h" | 
 | 8 |  | 
 | 9 | #include <linux/blkdev.h> | 
 | 10 | #include <linux/delay.h> | 
 | 11 |  | 
 | 12 | #include <scsi/scsi_tcq.h> | 
 | 13 |  | 
 | 14 | static inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd *cmd); | 
 | 15 | static inline cont_entry_t *qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *); | 
 | 16 | static inline cont_a64_entry_t *qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *); | 
 | 17 | static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha); | 
| Adrian Bunk | 413975a | 2006-06-30 02:33:06 -0700 | [diff] [blame] | 18 | static void qla2x00_isp_cmd(scsi_qla_host_t *ha); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 |  | 
 | 20 | /** | 
 | 21 |  * qla2x00_get_cmd_direction() - Determine control_flag data direction. | 
 | 22 |  * @cmd: SCSI command | 
 | 23 |  * | 
 | 24 |  * Returns the proper CF_* direction based on CDB. | 
 | 25 |  */ | 
 | 26 | static inline uint16_t | 
 | 27 | qla2x00_get_cmd_direction(struct scsi_cmnd *cmd) | 
 | 28 | { | 
 | 29 | 	uint16_t cflags; | 
 | 30 |  | 
 | 31 | 	cflags = 0; | 
 | 32 |  | 
 | 33 | 	/* Set transfer direction */ | 
 | 34 | 	if (cmd->sc_data_direction == DMA_TO_DEVICE) | 
 | 35 | 		cflags = CF_WRITE; | 
 | 36 | 	else if (cmd->sc_data_direction == DMA_FROM_DEVICE) | 
 | 37 | 		cflags = CF_READ; | 
 | 38 | 	return (cflags); | 
 | 39 | } | 
 | 40 |  | 
 | 41 | /** | 
 | 42 |  * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and | 
 | 43 |  * Continuation Type 0 IOCBs to allocate. | 
 | 44 |  * | 
 | 45 |  * @dsds: number of data segment decriptors needed | 
 | 46 |  * | 
 | 47 |  * Returns the number of IOCB entries needed to store @dsds. | 
 | 48 |  */ | 
 | 49 | uint16_t | 
 | 50 | qla2x00_calc_iocbs_32(uint16_t dsds) | 
 | 51 | { | 
 | 52 | 	uint16_t iocbs; | 
 | 53 |  | 
 | 54 | 	iocbs = 1; | 
 | 55 | 	if (dsds > 3) { | 
 | 56 | 		iocbs += (dsds - 3) / 7; | 
 | 57 | 		if ((dsds - 3) % 7) | 
 | 58 | 			iocbs++; | 
 | 59 | 	} | 
 | 60 | 	return (iocbs); | 
 | 61 | } | 
 | 62 |  | 
 | 63 | /** | 
 | 64 |  * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and | 
 | 65 |  * Continuation Type 1 IOCBs to allocate. | 
 | 66 |  * | 
 | 67 |  * @dsds: number of data segment decriptors needed | 
 | 68 |  * | 
 | 69 |  * Returns the number of IOCB entries needed to store @dsds. | 
 | 70 |  */ | 
 | 71 | uint16_t | 
 | 72 | qla2x00_calc_iocbs_64(uint16_t dsds) | 
 | 73 | { | 
 | 74 | 	uint16_t iocbs; | 
 | 75 |  | 
 | 76 | 	iocbs = 1; | 
 | 77 | 	if (dsds > 2) { | 
 | 78 | 		iocbs += (dsds - 2) / 5; | 
 | 79 | 		if ((dsds - 2) % 5) | 
 | 80 | 			iocbs++; | 
 | 81 | 	} | 
 | 82 | 	return (iocbs); | 
 | 83 | } | 
 | 84 |  | 
 | 85 | /** | 
 | 86 |  * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB. | 
 | 87 |  * @ha: HA context | 
 | 88 |  * | 
 | 89 |  * Returns a pointer to the Continuation Type 0 IOCB packet. | 
 | 90 |  */ | 
 | 91 | static inline cont_entry_t * | 
 | 92 | qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha) | 
 | 93 | { | 
 | 94 | 	cont_entry_t *cont_pkt; | 
 | 95 |  | 
 | 96 | 	/* Adjust ring index. */ | 
 | 97 | 	ha->req_ring_index++; | 
 | 98 | 	if (ha->req_ring_index == ha->request_q_length) { | 
 | 99 | 		ha->req_ring_index = 0; | 
 | 100 | 		ha->request_ring_ptr = ha->request_ring; | 
 | 101 | 	} else { | 
 | 102 | 		ha->request_ring_ptr++; | 
 | 103 | 	} | 
 | 104 |  | 
 | 105 | 	cont_pkt = (cont_entry_t *)ha->request_ring_ptr; | 
 | 106 |  | 
 | 107 | 	/* Load packet defaults. */ | 
 | 108 | 	*((uint32_t *)(&cont_pkt->entry_type)) = | 
 | 109 | 	    __constant_cpu_to_le32(CONTINUE_TYPE); | 
 | 110 |  | 
 | 111 | 	return (cont_pkt); | 
 | 112 | } | 
 | 113 |  | 
 | 114 | /** | 
 | 115 |  * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB. | 
 | 116 |  * @ha: HA context | 
 | 117 |  * | 
 | 118 |  * Returns a pointer to the continuation type 1 IOCB packet. | 
 | 119 |  */ | 
 | 120 | static inline cont_a64_entry_t * | 
 | 121 | qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha) | 
 | 122 | { | 
 | 123 | 	cont_a64_entry_t *cont_pkt; | 
 | 124 |  | 
 | 125 | 	/* Adjust ring index. */ | 
 | 126 | 	ha->req_ring_index++; | 
 | 127 | 	if (ha->req_ring_index == ha->request_q_length) { | 
 | 128 | 		ha->req_ring_index = 0; | 
 | 129 | 		ha->request_ring_ptr = ha->request_ring; | 
 | 130 | 	} else { | 
 | 131 | 		ha->request_ring_ptr++; | 
 | 132 | 	} | 
 | 133 |  | 
 | 134 | 	cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr; | 
 | 135 |  | 
 | 136 | 	/* Load packet defaults. */ | 
 | 137 | 	*((uint32_t *)(&cont_pkt->entry_type)) = | 
 | 138 | 	    __constant_cpu_to_le32(CONTINUE_A64_TYPE); | 
 | 139 |  | 
 | 140 | 	return (cont_pkt); | 
 | 141 | } | 
 | 142 |  | 
 | 143 | /** | 
 | 144 |  * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit | 
 | 145 |  * capable IOCB types. | 
 | 146 |  * | 
 | 147 |  * @sp: SRB command to process | 
 | 148 |  * @cmd_pkt: Command type 2 IOCB | 
 | 149 |  * @tot_dsds: Total number of segments to transfer | 
 | 150 |  */ | 
 | 151 | void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt, | 
 | 152 |     uint16_t tot_dsds) | 
 | 153 | { | 
 | 154 | 	uint16_t	avail_dsds; | 
 | 155 | 	uint32_t	*cur_dsd; | 
 | 156 | 	scsi_qla_host_t	*ha; | 
 | 157 | 	struct scsi_cmnd *cmd; | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 158 | 	struct scatterlist *sg; | 
 | 159 | 	int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 |  | 
 | 161 | 	cmd = sp->cmd; | 
 | 162 |  | 
 | 163 | 	/* Update entry type to indicate Command Type 2 IOCB */ | 
 | 164 | 	*((uint32_t *)(&cmd_pkt->entry_type)) = | 
 | 165 | 	    __constant_cpu_to_le32(COMMAND_TYPE); | 
 | 166 |  | 
 | 167 | 	/* No data transfer */ | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 168 | 	if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | 		cmd_pkt->byte_count = __constant_cpu_to_le32(0); | 
 | 170 | 		return; | 
 | 171 | 	} | 
 | 172 |  | 
 | 173 | 	ha = sp->ha; | 
 | 174 |  | 
 | 175 | 	cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd)); | 
 | 176 |  | 
 | 177 | 	/* Three DSDs are available in the Command Type 2 IOCB */ | 
 | 178 | 	avail_dsds = 3; | 
 | 179 | 	cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; | 
 | 180 |  | 
 | 181 | 	/* Load data segments */ | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 182 | 	scsi_for_each_sg(cmd, sg, tot_dsds, i) { | 
 | 183 | 		cont_entry_t *cont_pkt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 |  | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 185 | 		/* Allocate additional continuation packets? */ | 
 | 186 | 		if (avail_dsds == 0) { | 
 | 187 | 			/* | 
 | 188 | 			 * Seven DSDs are available in the Continuation | 
 | 189 | 			 * Type 0 IOCB. | 
 | 190 | 			 */ | 
 | 191 | 			cont_pkt = qla2x00_prep_cont_type0_iocb(ha); | 
 | 192 | 			cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address; | 
 | 193 | 			avail_dsds = 7; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | 		} | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 195 |  | 
 | 196 | 		*cur_dsd++ = cpu_to_le32(sg_dma_address(sg)); | 
 | 197 | 		*cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); | 
 | 198 | 		avail_dsds--; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | 	} | 
 | 200 | } | 
 | 201 |  | 
 | 202 | /** | 
 | 203 |  * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit | 
 | 204 |  * capable IOCB types. | 
 | 205 |  * | 
 | 206 |  * @sp: SRB command to process | 
 | 207 |  * @cmd_pkt: Command type 3 IOCB | 
 | 208 |  * @tot_dsds: Total number of segments to transfer | 
 | 209 |  */ | 
 | 210 | void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt, | 
 | 211 |     uint16_t tot_dsds) | 
 | 212 | { | 
 | 213 | 	uint16_t	avail_dsds; | 
 | 214 | 	uint32_t	*cur_dsd; | 
 | 215 | 	scsi_qla_host_t	*ha; | 
 | 216 | 	struct scsi_cmnd *cmd; | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 217 | 	struct scatterlist *sg; | 
 | 218 | 	int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 |  | 
 | 220 | 	cmd = sp->cmd; | 
 | 221 |  | 
 | 222 | 	/* Update entry type to indicate Command Type 3 IOCB */ | 
 | 223 | 	*((uint32_t *)(&cmd_pkt->entry_type)) = | 
 | 224 | 	    __constant_cpu_to_le32(COMMAND_A64_TYPE); | 
 | 225 |  | 
 | 226 | 	/* No data transfer */ | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 227 | 	if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | 		cmd_pkt->byte_count = __constant_cpu_to_le32(0); | 
 | 229 | 		return; | 
 | 230 | 	} | 
 | 231 |  | 
 | 232 | 	ha = sp->ha; | 
 | 233 |  | 
 | 234 | 	cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd)); | 
 | 235 |  | 
 | 236 | 	/* Two DSDs are available in the Command Type 3 IOCB */ | 
 | 237 | 	avail_dsds = 2; | 
 | 238 | 	cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; | 
 | 239 |  | 
 | 240 | 	/* Load data segments */ | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 241 | 	scsi_for_each_sg(cmd, sg, tot_dsds, i) { | 
 | 242 | 		dma_addr_t	sle_dma; | 
 | 243 | 		cont_a64_entry_t *cont_pkt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 |  | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 245 | 		/* Allocate additional continuation packets? */ | 
 | 246 | 		if (avail_dsds == 0) { | 
 | 247 | 			/* | 
 | 248 | 			 * Five DSDs are available in the Continuation | 
 | 249 | 			 * Type 1 IOCB. | 
 | 250 | 			 */ | 
 | 251 | 			cont_pkt = qla2x00_prep_cont_type1_iocb(ha); | 
 | 252 | 			cur_dsd = (uint32_t *)cont_pkt->dseg_0_address; | 
 | 253 | 			avail_dsds = 5; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | 		} | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 255 |  | 
 | 256 | 		sle_dma = sg_dma_address(sg); | 
 | 257 | 		*cur_dsd++ = cpu_to_le32(LSD(sle_dma)); | 
 | 258 | 		*cur_dsd++ = cpu_to_le32(MSD(sle_dma)); | 
 | 259 | 		*cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); | 
 | 260 | 		avail_dsds--; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | 	} | 
 | 262 | } | 
 | 263 |  | 
 | 264 | /** | 
 | 265 |  * qla2x00_start_scsi() - Send a SCSI command to the ISP | 
 | 266 |  * @sp: command to send to the ISP | 
 | 267 |  * | 
 | 268 |  * Returns non-zero if a failure occured, else zero. | 
 | 269 |  */ | 
 | 270 | int | 
 | 271 | qla2x00_start_scsi(srb_t *sp) | 
 | 272 | { | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 273 | 	int		ret, nseg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | 	unsigned long   flags; | 
 | 275 | 	scsi_qla_host_t	*ha; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | 	struct scsi_cmnd *cmd; | 
 | 277 | 	uint32_t	*clr_ptr; | 
 | 278 | 	uint32_t        index; | 
 | 279 | 	uint32_t	handle; | 
 | 280 | 	cmd_entry_t	*cmd_pkt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | 	uint16_t	cnt; | 
 | 282 | 	uint16_t	req_cnt; | 
 | 283 | 	uint16_t	tot_dsds; | 
| Andrew Vasquez | 3d71644 | 2005-07-06 10:30:26 -0700 | [diff] [blame] | 284 | 	struct device_reg_2xxx __iomem *reg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 |  | 
 | 286 | 	/* Setup device pointers. */ | 
 | 287 | 	ret = 0; | 
 | bdf7962 | 2005-04-17 15:06:53 -0500 | [diff] [blame] | 288 | 	ha = sp->ha; | 
| Andrew Vasquez | 3d71644 | 2005-07-06 10:30:26 -0700 | [diff] [blame] | 289 | 	reg = &ha->iobase->isp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 	cmd = sp->cmd; | 
 | 8302192 | 2005-04-17 15:10:41 -0500 | [diff] [blame] | 291 | 	/* So we know we haven't pci_map'ed anything yet */ | 
 | 292 | 	tot_dsds = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 |  | 
 | 294 | 	/* Send marker if required */ | 
 | 295 | 	if (ha->marker_needed != 0) { | 
 | 296 | 		if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) { | 
 | 297 | 			return (QLA_FUNCTION_FAILED); | 
 | 298 | 		} | 
 | 299 | 		ha->marker_needed = 0; | 
 | 300 | 	} | 
 | 301 |  | 
 | 302 | 	/* Acquire ring specific lock */ | 
 | 303 | 	spin_lock_irqsave(&ha->hardware_lock, flags); | 
 | 304 |  | 
 | 305 | 	/* Check for room in outstanding command list. */ | 
 | 306 | 	handle = ha->current_outstanding_cmd; | 
 | 307 | 	for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) { | 
 | 308 | 		handle++; | 
 | 309 | 		if (handle == MAX_OUTSTANDING_COMMANDS) | 
 | 310 | 			handle = 1; | 
| Andrew Vasquez | 700ca0e | 2007-09-20 14:07:46 -0700 | [diff] [blame] | 311 | 		if (!ha->outstanding_cmds[handle]) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | 			break; | 
 | 313 | 	} | 
 | 314 | 	if (index == MAX_OUTSTANDING_COMMANDS) | 
 | 315 | 		goto queuing_error; | 
 | 316 |  | 
 | 8302192 | 2005-04-17 15:10:41 -0500 | [diff] [blame] | 317 | 	/* Map the sg table so we have an accurate count of sg entries needed */ | 
| Seokmann Ju | 2c3dfe3 | 2007-07-05 13:16:51 -0700 | [diff] [blame] | 318 | 	if (scsi_sg_count(cmd)) { | 
 | 319 | 		nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), | 
 | 320 | 		    scsi_sg_count(cmd), cmd->sc_data_direction); | 
 | 321 | 		if (unlikely(!nseg)) | 
 | 322 | 			goto queuing_error; | 
 | 323 | 	} else | 
 | 324 | 		nseg = 0; | 
 | 325 |  | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 326 | 	tot_dsds = nseg; | 
 | 8302192 | 2005-04-17 15:10:41 -0500 | [diff] [blame] | 327 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | 	/* Calculate the number of request entries needed. */ | 
| Andrew Vasquez | fd34f55 | 2007-07-19 15:06:00 -0700 | [diff] [blame] | 329 | 	req_cnt = ha->isp_ops->calc_req_entries(tot_dsds); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 	if (ha->req_q_cnt < (req_cnt + 2)) { | 
 | 331 | 		cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg)); | 
 | 332 | 		if (ha->req_ring_index < cnt) | 
 | 333 | 			ha->req_q_cnt = cnt - ha->req_ring_index; | 
 | 334 | 		else | 
 | 335 | 			ha->req_q_cnt = ha->request_q_length - | 
 | 336 | 			    (ha->req_ring_index - cnt); | 
 | 337 | 	} | 
 | 338 | 	if (ha->req_q_cnt < (req_cnt + 2)) | 
 | 339 | 		goto queuing_error; | 
 | 340 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | 	/* Build command packet */ | 
 | 342 | 	ha->current_outstanding_cmd = handle; | 
 | 343 | 	ha->outstanding_cmds[handle] = sp; | 
 | 344 | 	sp->ha = ha; | 
 | 345 | 	sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle; | 
 | 346 | 	ha->req_q_cnt -= req_cnt; | 
 | 347 |  | 
 | 348 | 	cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr; | 
 | 349 | 	cmd_pkt->handle = handle; | 
 | 350 | 	/* Zero out remaining portion of packet. */ | 
 | 351 | 	clr_ptr = (uint32_t *)cmd_pkt + 2; | 
 | 352 | 	memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); | 
 | 353 | 	cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); | 
 | 354 |  | 
 | bdf7962 | 2005-04-17 15:06:53 -0500 | [diff] [blame] | 355 | 	/* Set target ID and LUN number*/ | 
 | 356 | 	SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id); | 
 | 357 | 	cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 |  | 
 | 359 | 	/* Update tagged queuing modifier */ | 
 | 360 | 	cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | 	/* Load SCSI command packet. */ | 
 | 363 | 	memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len); | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 364 | 	cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 |  | 
 | 366 | 	/* Build IOCB segments */ | 
| Andrew Vasquez | fd34f55 | 2007-07-19 15:06:00 -0700 | [diff] [blame] | 367 | 	ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 |  | 
 | 369 | 	/* Set total data segment count. */ | 
 | 370 | 	cmd_pkt->entry_count = (uint8_t)req_cnt; | 
 | 371 | 	wmb(); | 
 | 372 |  | 
 | 373 | 	/* Adjust ring index. */ | 
 | 374 | 	ha->req_ring_index++; | 
 | 375 | 	if (ha->req_ring_index == ha->request_q_length) { | 
 | 376 | 		ha->req_ring_index = 0; | 
 | 377 | 		ha->request_ring_ptr = ha->request_ring; | 
 | 378 | 	} else | 
 | 379 | 		ha->request_ring_ptr++; | 
 | 380 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | 	sp->flags |= SRB_DMA_VALID; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 |  | 
 | 383 | 	/* Set chip new ring index. */ | 
 | 384 | 	WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index); | 
 | 385 | 	RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg));	/* PCI Posting. */ | 
 | 386 |  | 
| Andrew Vasquez | 4fdfefe | 2005-10-27 11:09:48 -0700 | [diff] [blame] | 387 | 	/* Manage unprocessed RIO/ZIO commands in response queue. */ | 
 | 388 | 	if (ha->flags.process_response_queue && | 
 | 389 | 	    ha->response_ring_ptr->signature != RESPONSE_PROCESSED) | 
 | 390 | 		qla2x00_process_response_queue(ha); | 
 | 391 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | 	spin_unlock_irqrestore(&ha->hardware_lock, flags); | 
 | 393 | 	return (QLA_SUCCESS); | 
 | 394 |  | 
 | 395 | queuing_error: | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 396 | 	if (tot_dsds) | 
 | 397 | 		scsi_dma_unmap(cmd); | 
 | 398 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | 	spin_unlock_irqrestore(&ha->hardware_lock, flags); | 
 | 400 |  | 
 | 401 | 	return (QLA_FUNCTION_FAILED); | 
 | 402 | } | 
 | 403 |  | 
 | 404 | /** | 
 | 405 |  * qla2x00_marker() - Send a marker IOCB to the firmware. | 
 | 406 |  * @ha: HA context | 
 | 407 |  * @loop_id: loop ID | 
 | 408 |  * @lun: LUN | 
 | 409 |  * @type: marker modifier | 
 | 410 |  * | 
 | 411 |  * Can be called from both normal and interrupt context. | 
 | 412 |  * | 
 | 413 |  * Returns non-zero if a failure occured, else zero. | 
 | 414 |  */ | 
| Andrew Vasquez | fa2a1ce | 2005-07-06 10:32:07 -0700 | [diff] [blame] | 415 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun, | 
 | 417 |     uint8_t type) | 
 | 418 | { | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 419 | 	mrk_entry_t *mrk; | 
 | 420 | 	struct mrk_entry_24xx *mrk24; | 
| Seokmann Ju | 2c3dfe3 | 2007-07-05 13:16:51 -0700 | [diff] [blame] | 421 | 	scsi_qla_host_t *pha = to_qla_parent(ha); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 |  | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 423 | 	mrk24 = NULL; | 
| Seokmann Ju | 2c3dfe3 | 2007-07-05 13:16:51 -0700 | [diff] [blame] | 424 | 	mrk = (mrk_entry_t *)qla2x00_req_pkt(pha); | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 425 | 	if (mrk == NULL) { | 
 | 426 | 		DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n", | 
 | 427 | 		    __func__, ha->host_no)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 |  | 
 | 429 | 		return (QLA_FUNCTION_FAILED); | 
 | 430 | 	} | 
 | 431 |  | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 432 | 	mrk->entry_type = MARKER_TYPE; | 
 | 433 | 	mrk->modifier = type; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | 	if (type != MK_SYNC_ALL) { | 
| Andrew Vasquez | e428924 | 2007-07-19 15:05:56 -0700 | [diff] [blame] | 435 | 		if (IS_FWI2_CAPABLE(ha)) { | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 436 | 			mrk24 = (struct mrk_entry_24xx *) mrk; | 
 | 437 | 			mrk24->nport_handle = cpu_to_le16(loop_id); | 
 | 438 | 			mrk24->lun[1] = LSB(lun); | 
 | 439 | 			mrk24->lun[2] = MSB(lun); | 
| Shyam Sundar | b797b6d | 2006-08-01 13:48:13 -0700 | [diff] [blame] | 440 | 			host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun)); | 
| Seokmann Ju | 2c3dfe3 | 2007-07-05 13:16:51 -0700 | [diff] [blame] | 441 | 			mrk24->vp_index = ha->vp_idx; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 442 | 		} else { | 
 | 443 | 			SET_TARGET_ID(ha, mrk->target, loop_id); | 
 | 444 | 			mrk->lun = cpu_to_le16(lun); | 
 | 445 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | 	} | 
 | 447 | 	wmb(); | 
 | 448 |  | 
| Seokmann Ju | 2c3dfe3 | 2007-07-05 13:16:51 -0700 | [diff] [blame] | 449 | 	qla2x00_isp_cmd(pha); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 |  | 
 | 451 | 	return (QLA_SUCCESS); | 
 | 452 | } | 
 | 453 |  | 
| Andrew Vasquez | fa2a1ce | 2005-07-06 10:32:07 -0700 | [diff] [blame] | 454 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun, | 
 | 456 |     uint8_t type) | 
 | 457 | { | 
 | 458 | 	int ret; | 
 | 459 | 	unsigned long flags = 0; | 
 | 460 |  | 
 | 461 | 	spin_lock_irqsave(&ha->hardware_lock, flags); | 
 | 462 | 	ret = __qla2x00_marker(ha, loop_id, lun, type); | 
 | 463 | 	spin_unlock_irqrestore(&ha->hardware_lock, flags); | 
 | 464 |  | 
 | 465 | 	return (ret); | 
 | 466 | } | 
 | 467 |  | 
 | 468 | /** | 
 | 469 |  * qla2x00_req_pkt() - Retrieve a request packet from the request ring. | 
 | 470 |  * @ha: HA context | 
 | 471 |  * | 
 | 472 |  * Note: The caller must hold the hardware lock before calling this routine. | 
 | 473 |  * | 
 | 474 |  * Returns NULL if function failed, else, a pointer to the request packet. | 
 | 475 |  */ | 
 | 476 | static request_t * | 
 | 477 | qla2x00_req_pkt(scsi_qla_host_t *ha) | 
 | 478 | { | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 479 | 	device_reg_t __iomem *reg = ha->iobase; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | 	request_t	*pkt = NULL; | 
 | 481 | 	uint16_t	cnt; | 
 | 482 | 	uint32_t	*dword_ptr; | 
 | 483 | 	uint32_t	timer; | 
 | 484 | 	uint16_t	req_cnt = 1; | 
 | 485 |  | 
 | 486 | 	/* Wait 1 second for slot. */ | 
 | 487 | 	for (timer = HZ; timer; timer--) { | 
 | 488 | 		if ((req_cnt + 2) >= ha->req_q_cnt) { | 
 | 489 | 			/* Calculate number of free request entries. */ | 
| Andrew Vasquez | e428924 | 2007-07-19 15:05:56 -0700 | [diff] [blame] | 490 | 			if (IS_FWI2_CAPABLE(ha)) | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 491 | 				cnt = (uint16_t)RD_REG_DWORD( | 
 | 492 | 				    ®->isp24.req_q_out); | 
 | 493 | 			else | 
 | 494 | 				cnt = qla2x00_debounce_register( | 
 | 495 | 				    ISP_REQ_Q_OUT(ha, ®->isp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | 			if  (ha->req_ring_index < cnt) | 
 | 497 | 				ha->req_q_cnt = cnt - ha->req_ring_index; | 
 | 498 | 			else | 
 | 499 | 				ha->req_q_cnt = ha->request_q_length - | 
 | 500 | 				    (ha->req_ring_index - cnt); | 
 | 501 | 		} | 
 | 502 | 		/* If room for request in request ring. */ | 
 | 503 | 		if ((req_cnt + 2) < ha->req_q_cnt) { | 
 | 504 | 			ha->req_q_cnt--; | 
 | 505 | 			pkt = ha->request_ring_ptr; | 
 | 506 |  | 
 | 507 | 			/* Zero out packet. */ | 
 | 508 | 			dword_ptr = (uint32_t *)pkt; | 
 | 509 | 			for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++) | 
 | 510 | 				*dword_ptr++ = 0; | 
 | 511 |  | 
 | 512 | 			/* Set system defined field. */ | 
 | 513 | 			pkt->sys_define = (uint8_t)ha->req_ring_index; | 
 | 514 |  | 
 | 515 | 			/* Set entry count. */ | 
 | 516 | 			pkt->entry_count = 1; | 
 | 517 |  | 
 | 518 | 			break; | 
 | 519 | 		} | 
 | 520 |  | 
 | 521 | 		/* Release ring specific lock */ | 
 | 522 | 		spin_unlock(&ha->hardware_lock); | 
 | 523 |  | 
 | 524 | 		udelay(2);   /* 2 us */ | 
 | 525 |  | 
 | 526 | 		/* Check for pending interrupts. */ | 
 | 527 | 		/* During init we issue marker directly */ | 
| Andrew Vasquez | a3a63d5 | 2007-10-19 15:59:14 -0700 | [diff] [blame] | 528 | 		if (!ha->marker_needed && !ha->flags.init_done) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | 			qla2x00_poll(ha); | 
 | 530 |  | 
 | 531 | 		spin_lock_irq(&ha->hardware_lock); | 
 | 532 | 	} | 
 | 533 | 	if (!pkt) { | 
 | 534 | 		DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__)); | 
 | 535 | 	} | 
 | 536 |  | 
 | 537 | 	return (pkt); | 
 | 538 | } | 
 | 539 |  | 
 | 540 | /** | 
 | 541 |  * qla2x00_isp_cmd() - Modify the request ring pointer. | 
 | 542 |  * @ha: HA context | 
 | 543 |  * | 
 | 544 |  * Note: The caller must hold the hardware lock before calling this routine. | 
 | 545 |  */ | 
| Adrian Bunk | 413975a | 2006-06-30 02:33:06 -0700 | [diff] [blame] | 546 | static void | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | qla2x00_isp_cmd(scsi_qla_host_t *ha) | 
 | 548 | { | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 549 | 	device_reg_t __iomem *reg = ha->iobase; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 |  | 
 | 551 | 	DEBUG5(printk("%s(): IOCB data:\n", __func__)); | 
 | 552 | 	DEBUG5(qla2x00_dump_buffer( | 
 | 553 | 	    (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE)); | 
 | 554 |  | 
 | 555 | 	/* Adjust ring index. */ | 
 | 556 | 	ha->req_ring_index++; | 
 | 557 | 	if (ha->req_ring_index == ha->request_q_length) { | 
 | 558 | 		ha->req_ring_index = 0; | 
 | 559 | 		ha->request_ring_ptr = ha->request_ring; | 
 | 560 | 	} else | 
 | 561 | 		ha->request_ring_ptr++; | 
 | 562 |  | 
 | 563 | 	/* Set chip new ring index. */ | 
| Andrew Vasquez | e428924 | 2007-07-19 15:05:56 -0700 | [diff] [blame] | 564 | 	if (IS_FWI2_CAPABLE(ha)) { | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 565 | 		WRT_REG_DWORD(®->isp24.req_q_in, ha->req_ring_index); | 
 | 566 | 		RD_REG_DWORD_RELAXED(®->isp24.req_q_in); | 
 | 567 | 	} else { | 
 | 568 | 		WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp), ha->req_ring_index); | 
 | 569 | 		RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp)); | 
 | 570 | 	} | 
 | 571 |  | 
 | 572 | } | 
 | 573 |  | 
 | 574 | /** | 
 | 575 |  * qla24xx_calc_iocbs() - Determine number of Command Type 3 and | 
 | 576 |  * Continuation Type 1 IOCBs to allocate. | 
 | 577 |  * | 
 | 578 |  * @dsds: number of data segment decriptors needed | 
 | 579 |  * | 
 | 580 |  * Returns the number of IOCB entries needed to store @dsds. | 
 | 581 |  */ | 
 | 582 | static inline uint16_t | 
 | 583 | qla24xx_calc_iocbs(uint16_t dsds) | 
 | 584 | { | 
 | 585 | 	uint16_t iocbs; | 
 | 586 |  | 
 | 587 | 	iocbs = 1; | 
 | 588 | 	if (dsds > 1) { | 
 | 589 | 		iocbs += (dsds - 1) / 5; | 
 | 590 | 		if ((dsds - 1) % 5) | 
 | 591 | 			iocbs++; | 
 | 592 | 	} | 
 | 593 | 	return iocbs; | 
 | 594 | } | 
 | 595 |  | 
 | 596 | /** | 
 | 597 |  * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7 | 
 | 598 |  * IOCB types. | 
 | 599 |  * | 
 | 600 |  * @sp: SRB command to process | 
 | 601 |  * @cmd_pkt: Command type 3 IOCB | 
 | 602 |  * @tot_dsds: Total number of segments to transfer | 
 | 603 |  */ | 
 | 604 | static inline void | 
 | 605 | qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt, | 
 | 606 |     uint16_t tot_dsds) | 
 | 607 | { | 
 | 608 | 	uint16_t	avail_dsds; | 
 | 609 | 	uint32_t	*cur_dsd; | 
 | 610 | 	scsi_qla_host_t	*ha; | 
 | 611 | 	struct scsi_cmnd *cmd; | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 612 | 	struct scatterlist *sg; | 
 | 613 | 	int i; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 614 |  | 
 | 615 | 	cmd = sp->cmd; | 
 | 616 |  | 
 | 617 | 	/* Update entry type to indicate Command Type 3 IOCB */ | 
 | 618 | 	*((uint32_t *)(&cmd_pkt->entry_type)) = | 
 | 619 | 	    __constant_cpu_to_le32(COMMAND_TYPE_7); | 
 | 620 |  | 
 | 621 | 	/* No data transfer */ | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 622 | 	if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 623 | 		cmd_pkt->byte_count = __constant_cpu_to_le32(0); | 
 | 624 | 		return; | 
 | 625 | 	} | 
 | 626 |  | 
 | 627 | 	ha = sp->ha; | 
 | 628 |  | 
 | 629 | 	/* Set transfer direction */ | 
 | 630 | 	if (cmd->sc_data_direction == DMA_TO_DEVICE) | 
 | 631 | 		cmd_pkt->task_mgmt_flags = | 
 | 632 | 		    __constant_cpu_to_le16(TMF_WRITE_DATA); | 
 | 633 | 	else if (cmd->sc_data_direction == DMA_FROM_DEVICE) | 
 | 634 | 		cmd_pkt->task_mgmt_flags = | 
 | 635 | 		    __constant_cpu_to_le16(TMF_READ_DATA); | 
 | 636 |  | 
 | 637 | 	/* One DSD is available in the Command Type 3 IOCB */ | 
 | 638 | 	avail_dsds = 1; | 
 | 639 | 	cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; | 
 | 640 |  | 
 | 641 | 	/* Load data segments */ | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 642 |  | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 643 | 	scsi_for_each_sg(cmd, sg, tot_dsds, i) { | 
 | 644 | 		dma_addr_t	sle_dma; | 
 | 645 | 		cont_a64_entry_t *cont_pkt; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 646 |  | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 647 | 		/* Allocate additional continuation packets? */ | 
 | 648 | 		if (avail_dsds == 0) { | 
 | 649 | 			/* | 
 | 650 | 			 * Five DSDs are available in the Continuation | 
 | 651 | 			 * Type 1 IOCB. | 
 | 652 | 			 */ | 
 | 653 | 			cont_pkt = qla2x00_prep_cont_type1_iocb(ha); | 
 | 654 | 			cur_dsd = (uint32_t *)cont_pkt->dseg_0_address; | 
 | 655 | 			avail_dsds = 5; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 656 | 		} | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 657 |  | 
 | 658 | 		sle_dma = sg_dma_address(sg); | 
 | 659 | 		*cur_dsd++ = cpu_to_le32(LSD(sle_dma)); | 
 | 660 | 		*cur_dsd++ = cpu_to_le32(MSD(sle_dma)); | 
 | 661 | 		*cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); | 
 | 662 | 		avail_dsds--; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 663 | 	} | 
 | 664 | } | 
 | 665 |  | 
 | 666 |  | 
 | 667 | /** | 
 | 668 |  * qla24xx_start_scsi() - Send a SCSI command to the ISP | 
 | 669 |  * @sp: command to send to the ISP | 
 | 670 |  * | 
 | 671 |  * Returns non-zero if a failure occured, else zero. | 
 | 672 |  */ | 
 | 673 | int | 
 | 674 | qla24xx_start_scsi(srb_t *sp) | 
 | 675 | { | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 676 | 	int		ret, nseg; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 677 | 	unsigned long   flags; | 
 | 678 | 	scsi_qla_host_t	*ha; | 
 | 679 | 	struct scsi_cmnd *cmd; | 
 | 680 | 	uint32_t	*clr_ptr; | 
 | 681 | 	uint32_t        index; | 
 | 682 | 	uint32_t	handle; | 
 | 683 | 	struct cmd_type_7 *cmd_pkt; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 684 | 	uint16_t	cnt; | 
 | 685 | 	uint16_t	req_cnt; | 
 | 686 | 	uint16_t	tot_dsds; | 
| Linus Torvalds | db776a1 | 2005-07-26 14:50:02 -0700 | [diff] [blame] | 687 | 	struct device_reg_24xx __iomem *reg; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 688 |  | 
 | 689 | 	/* Setup device pointers. */ | 
 | 690 | 	ret = 0; | 
 | 691 | 	ha = sp->ha; | 
 | 692 | 	reg = &ha->iobase->isp24; | 
 | 693 | 	cmd = sp->cmd; | 
 | 694 | 	/* So we know we haven't pci_map'ed anything yet */ | 
 | 695 | 	tot_dsds = 0; | 
 | 696 |  | 
 | 697 | 	/* Send marker if required */ | 
 | 698 | 	if (ha->marker_needed != 0) { | 
 | 699 | 		if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) { | 
 | 700 | 			return QLA_FUNCTION_FAILED; | 
 | 701 | 		} | 
 | 702 | 		ha->marker_needed = 0; | 
 | 703 | 	} | 
 | 704 |  | 
 | 705 | 	/* Acquire ring specific lock */ | 
 | 706 | 	spin_lock_irqsave(&ha->hardware_lock, flags); | 
 | 707 |  | 
 | 708 | 	/* Check for room in outstanding command list. */ | 
 | 709 | 	handle = ha->current_outstanding_cmd; | 
 | 710 | 	for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) { | 
 | 711 | 		handle++; | 
 | 712 | 		if (handle == MAX_OUTSTANDING_COMMANDS) | 
 | 713 | 			handle = 1; | 
| Andrew Vasquez | 700ca0e | 2007-09-20 14:07:46 -0700 | [diff] [blame] | 714 | 		if (!ha->outstanding_cmds[handle]) | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 715 | 			break; | 
 | 716 | 	} | 
 | 717 | 	if (index == MAX_OUTSTANDING_COMMANDS) | 
 | 718 | 		goto queuing_error; | 
 | 719 |  | 
 | 720 | 	/* Map the sg table so we have an accurate count of sg entries needed */ | 
| Seokmann Ju | 2c3dfe3 | 2007-07-05 13:16:51 -0700 | [diff] [blame] | 721 | 	if (scsi_sg_count(cmd)) { | 
 | 722 | 		nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), | 
 | 723 | 		    scsi_sg_count(cmd), cmd->sc_data_direction); | 
 | 724 | 		if (unlikely(!nseg)) | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 725 | 			goto queuing_error; | 
| Seokmann Ju | 2c3dfe3 | 2007-07-05 13:16:51 -0700 | [diff] [blame] | 726 | 	} else | 
 | 727 | 		nseg = 0; | 
 | 728 |  | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 729 | 	tot_dsds = nseg; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 730 |  | 
 | 731 | 	req_cnt = qla24xx_calc_iocbs(tot_dsds); | 
 | 732 | 	if (ha->req_q_cnt < (req_cnt + 2)) { | 
 | 733 | 		cnt = (uint16_t)RD_REG_DWORD_RELAXED(®->req_q_out); | 
 | 734 | 		if (ha->req_ring_index < cnt) | 
 | 735 | 			ha->req_q_cnt = cnt - ha->req_ring_index; | 
 | 736 | 		else | 
 | 737 | 			ha->req_q_cnt = ha->request_q_length - | 
 | 738 | 				(ha->req_ring_index - cnt); | 
 | 739 | 	} | 
| Andrew Vasquez | 131736d | 2005-08-26 19:09:20 -0700 | [diff] [blame] | 740 | 	if (ha->req_q_cnt < (req_cnt + 2)) | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 741 | 		goto queuing_error; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 742 |  | 
 | 743 | 	/* Build command packet. */ | 
 | 744 | 	ha->current_outstanding_cmd = handle; | 
 | 745 | 	ha->outstanding_cmds[handle] = sp; | 
 | 746 | 	sp->ha = ha; | 
 | 747 | 	sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle; | 
 | 748 | 	ha->req_q_cnt -= req_cnt; | 
 | 749 |  | 
 | 750 | 	cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr; | 
 | 751 | 	cmd_pkt->handle = handle; | 
 | 752 |  | 
 | 753 | 	/* Zero out remaining portion of packet. */ | 
| James Bottomley | 72df832 | 2005-10-28 14:41:19 -0500 | [diff] [blame] | 754 | 	/*    tagged queuing modifier -- default is TSK_SIMPLE (0). */ | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 755 | 	clr_ptr = (uint32_t *)cmd_pkt + 2; | 
 | 756 | 	memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); | 
 | 757 | 	cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); | 
 | 758 |  | 
 | 759 | 	/* Set NPORT-ID and LUN number*/ | 
 | 760 | 	cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); | 
 | 761 | 	cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa; | 
 | 762 | 	cmd_pkt->port_id[1] = sp->fcport->d_id.b.area; | 
 | 763 | 	cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain; | 
| Seokmann Ju | 2c3dfe3 | 2007-07-05 13:16:51 -0700 | [diff] [blame] | 764 | 	cmd_pkt->vp_index = sp->fcport->vp_idx; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 765 |  | 
| Andrew Vasquez | 661c3f6 | 2005-10-27 11:09:58 -0700 | [diff] [blame] | 766 | 	int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun); | 
| andrew.vasquez@qlogic.com | 0d4be12 | 2006-02-07 08:45:35 -0800 | [diff] [blame] | 767 | 	host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun)); | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 768 |  | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 769 | 	/* Load SCSI command packet. */ | 
 | 770 | 	memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len); | 
 | 771 | 	host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb)); | 
 | 772 |  | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 773 | 	cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd)); | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 774 |  | 
 | 775 | 	/* Build IOCB segments */ | 
 | 776 | 	qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds); | 
 | 777 |  | 
 | 778 | 	/* Set total data segment count. */ | 
 | 779 | 	cmd_pkt->entry_count = (uint8_t)req_cnt; | 
 | 780 | 	wmb(); | 
 | 781 |  | 
 | 782 | 	/* Adjust ring index. */ | 
 | 783 | 	ha->req_ring_index++; | 
 | 784 | 	if (ha->req_ring_index == ha->request_q_length) { | 
 | 785 | 		ha->req_ring_index = 0; | 
 | 786 | 		ha->request_ring_ptr = ha->request_ring; | 
 | 787 | 	} else | 
 | 788 | 		ha->request_ring_ptr++; | 
 | 789 |  | 
 | 790 | 	sp->flags |= SRB_DMA_VALID; | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 791 |  | 
 | 792 | 	/* Set chip new ring index. */ | 
 | 793 | 	WRT_REG_DWORD(®->req_q_in, ha->req_ring_index); | 
 | 794 | 	RD_REG_DWORD_RELAXED(®->req_q_in);		/* PCI Posting. */ | 
 | 795 |  | 
| Andrew Vasquez | 4fdfefe | 2005-10-27 11:09:48 -0700 | [diff] [blame] | 796 | 	/* Manage unprocessed RIO/ZIO commands in response queue. */ | 
 | 797 | 	if (ha->flags.process_response_queue && | 
 | 798 | 	    ha->response_ring_ptr->signature != RESPONSE_PROCESSED) | 
 | 799 | 		qla24xx_process_response_queue(ha); | 
 | 800 |  | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 801 | 	spin_unlock_irqrestore(&ha->hardware_lock, flags); | 
 | 802 | 	return QLA_SUCCESS; | 
 | 803 |  | 
 | 804 | queuing_error: | 
| FUJITA Tomonori | 385d70b | 2007-05-26 01:55:38 +0900 | [diff] [blame] | 805 | 	if (tot_dsds) | 
 | 806 | 		scsi_dma_unmap(cmd); | 
 | 807 |  | 
| Andrew Vasquez | 2b6c0ce | 2005-07-06 10:31:17 -0700 | [diff] [blame] | 808 | 	spin_unlock_irqrestore(&ha->hardware_lock, flags); | 
 | 809 |  | 
 | 810 | 	return QLA_FUNCTION_FAILED; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |