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