David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * QLogic iSCSI HBA Driver |
| 3 | * Copyright (c) 2003-2006 QLogic Corporation |
| 4 | * |
| 5 | * See LICENSE.qla4xxx for copyright and licensing details. |
| 6 | */ |
| 7 | |
| 8 | #include "ql4_def.h" |
David C Somayajulu | e08c182 | 2007-05-23 18:14:34 -0700 | [diff] [blame] | 9 | #include "ql4_glbl.h" |
| 10 | #include "ql4_dbg.h" |
| 11 | #include "ql4_inline.h" |
| 12 | |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 13 | #include <scsi/scsi_tcq.h> |
| 14 | |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 15 | static int |
| 16 | qla4xxx_space_in_req_ring(struct scsi_qla_host *ha, uint16_t req_cnt) |
| 17 | { |
| 18 | uint16_t cnt; |
| 19 | |
| 20 | /* Calculate number of free request entries. */ |
| 21 | if ((req_cnt + 2) >= ha->req_q_count) { |
| 22 | cnt = (uint16_t) le32_to_cpu(ha->shadow_regs->req_q_out); |
| 23 | if (ha->request_in < cnt) |
| 24 | ha->req_q_count = cnt - ha->request_in; |
| 25 | else |
| 26 | ha->req_q_count = REQUEST_QUEUE_DEPTH - |
| 27 | (ha->request_in - cnt); |
| 28 | } |
| 29 | |
| 30 | /* Check if room for request in request ring. */ |
| 31 | if ((req_cnt + 2) < ha->req_q_count) |
| 32 | return 1; |
| 33 | else |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static void qla4xxx_advance_req_ring_ptr(struct scsi_qla_host *ha) |
| 38 | { |
| 39 | /* Advance request queue pointer */ |
| 40 | if (ha->request_in == (REQUEST_QUEUE_DEPTH - 1)) { |
| 41 | ha->request_in = 0; |
| 42 | ha->request_ptr = ha->request_ring; |
| 43 | } else { |
| 44 | ha->request_in++; |
| 45 | ha->request_ptr++; |
| 46 | } |
| 47 | } |
| 48 | |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 49 | /** |
| 50 | * qla4xxx_get_req_pkt - returns a valid entry in request queue. |
| 51 | * @ha: Pointer to host adapter structure. |
| 52 | * @queue_entry: Pointer to pointer to queue entry structure |
| 53 | * |
| 54 | * This routine performs the following tasks: |
| 55 | * - returns the current request_in pointer (if queue not full) |
| 56 | * - advances the request_in pointer |
| 57 | * - checks for queue full |
| 58 | **/ |
Adrian Bunk | 4797547 | 2007-04-26 00:35:16 -0700 | [diff] [blame] | 59 | static int qla4xxx_get_req_pkt(struct scsi_qla_host *ha, |
| 60 | struct queue_entry **queue_entry) |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 61 | { |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 62 | uint16_t req_cnt = 1; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 63 | |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 64 | if (qla4xxx_space_in_req_ring(ha, req_cnt)) { |
| 65 | *queue_entry = ha->request_ptr; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 66 | memset(*queue_entry, 0, sizeof(**queue_entry)); |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 67 | |
| 68 | qla4xxx_advance_req_ring_ptr(ha); |
| 69 | ha->req_q_count -= req_cnt; |
| 70 | return QLA_SUCCESS; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 73 | return QLA_ERROR; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * qla4xxx_send_marker_iocb - issues marker iocb to HBA |
| 78 | * @ha: Pointer to host adapter structure. |
| 79 | * @ddb_entry: Pointer to device database entry |
| 80 | * @lun: SCSI LUN |
| 81 | * @marker_type: marker identifier |
| 82 | * |
| 83 | * This routine issues a marker IOCB. |
| 84 | **/ |
David C Somayajulu | 9d56291 | 2008-03-19 11:23:03 -0700 | [diff] [blame] | 85 | int qla4xxx_send_marker_iocb(struct scsi_qla_host *ha, |
| 86 | struct ddb_entry *ddb_entry, int lun, uint16_t mrkr_mod) |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 87 | { |
Mathieu Desnoyers | 1c3f0b8 | 2007-10-18 23:41:04 -0700 | [diff] [blame] | 88 | struct qla4_marker_entry *marker_entry; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 89 | unsigned long flags = 0; |
| 90 | uint8_t status = QLA_SUCCESS; |
| 91 | |
| 92 | /* Acquire hardware specific lock */ |
| 93 | spin_lock_irqsave(&ha->hardware_lock, flags); |
| 94 | |
| 95 | /* Get pointer to the queue entry for the marker */ |
| 96 | if (qla4xxx_get_req_pkt(ha, (struct queue_entry **) &marker_entry) != |
| 97 | QLA_SUCCESS) { |
| 98 | status = QLA_ERROR; |
| 99 | goto exit_send_marker; |
| 100 | } |
| 101 | |
| 102 | /* Put the marker in the request queue */ |
| 103 | marker_entry->hdr.entryType = ET_MARKER; |
| 104 | marker_entry->hdr.entryCount = 1; |
| 105 | marker_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index); |
David C Somayajulu | 9d56291 | 2008-03-19 11:23:03 -0700 | [diff] [blame] | 106 | marker_entry->modifier = cpu_to_le16(mrkr_mod); |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 107 | int_to_scsilun(lun, &marker_entry->lun); |
| 108 | wmb(); |
| 109 | |
| 110 | /* Tell ISP it's got a new I/O request */ |
| 111 | writel(ha->request_in, &ha->reg->req_q_in); |
| 112 | readl(&ha->reg->req_q_in); |
| 113 | |
| 114 | exit_send_marker: |
| 115 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
| 116 | return status; |
| 117 | } |
| 118 | |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 119 | static struct continuation_t1_entry * |
| 120 | qla4xxx_alloc_cont_entry(struct scsi_qla_host *ha) |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 121 | { |
| 122 | struct continuation_t1_entry *cont_entry; |
| 123 | |
| 124 | cont_entry = (struct continuation_t1_entry *)ha->request_ptr; |
| 125 | |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 126 | qla4xxx_advance_req_ring_ptr(ha); |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 127 | |
| 128 | /* Load packet defaults */ |
| 129 | cont_entry->hdr.entryType = ET_CONTINUE; |
| 130 | cont_entry->hdr.entryCount = 1; |
| 131 | cont_entry->hdr.systemDefined = (uint8_t) cpu_to_le16(ha->request_in); |
| 132 | |
| 133 | return cont_entry; |
| 134 | } |
| 135 | |
Adrian Bunk | 4797547 | 2007-04-26 00:35:16 -0700 | [diff] [blame] | 136 | static uint16_t qla4xxx_calc_request_entries(uint16_t dsds) |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 137 | { |
| 138 | uint16_t iocbs; |
| 139 | |
| 140 | iocbs = 1; |
| 141 | if (dsds > COMMAND_SEG) { |
| 142 | iocbs += (dsds - COMMAND_SEG) / CONTINUE_SEG; |
| 143 | if ((dsds - COMMAND_SEG) % CONTINUE_SEG) |
| 144 | iocbs++; |
| 145 | } |
| 146 | return iocbs; |
| 147 | } |
| 148 | |
Adrian Bunk | 4797547 | 2007-04-26 00:35:16 -0700 | [diff] [blame] | 149 | static void qla4xxx_build_scsi_iocbs(struct srb *srb, |
| 150 | struct command_t3_entry *cmd_entry, |
| 151 | uint16_t tot_dsds) |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 152 | { |
| 153 | struct scsi_qla_host *ha; |
| 154 | uint16_t avail_dsds; |
| 155 | struct data_seg_a64 *cur_dsd; |
| 156 | struct scsi_cmnd *cmd; |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 157 | struct scatterlist *sg; |
| 158 | int i; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 159 | |
| 160 | cmd = srb->cmd; |
| 161 | ha = srb->ha; |
| 162 | |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 163 | if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 164 | /* No data being transferred */ |
| 165 | cmd_entry->ttlByteCnt = __constant_cpu_to_le32(0); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | avail_dsds = COMMAND_SEG; |
| 170 | cur_dsd = (struct data_seg_a64 *) & (cmd_entry->dataseg[0]); |
| 171 | |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 172 | scsi_for_each_sg(cmd, sg, tot_dsds, i) { |
| 173 | dma_addr_t sle_dma; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 174 | |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 175 | /* Allocate additional continuation packets? */ |
| 176 | if (avail_dsds == 0) { |
| 177 | struct continuation_t1_entry *cont_entry; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 178 | |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 179 | cont_entry = qla4xxx_alloc_cont_entry(ha); |
| 180 | cur_dsd = |
| 181 | (struct data_seg_a64 *) |
| 182 | &cont_entry->dataseg[0]; |
| 183 | avail_dsds = CONTINUE_SEG; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 184 | } |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 185 | |
| 186 | sle_dma = sg_dma_address(sg); |
| 187 | cur_dsd->base.addrLow = cpu_to_le32(LSDW(sle_dma)); |
| 188 | cur_dsd->base.addrHigh = cpu_to_le32(MSDW(sle_dma)); |
| 189 | cur_dsd->count = cpu_to_le32(sg_dma_len(sg)); |
| 190 | avail_dsds--; |
| 191 | |
| 192 | cur_dsd++; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * qla4xxx_send_command_to_isp - issues command to HBA |
| 198 | * @ha: pointer to host adapter structure. |
| 199 | * @srb: pointer to SCSI Request Block to be sent to ISP |
| 200 | * |
| 201 | * This routine is called by qla4xxx_queuecommand to build an ISP |
| 202 | * command and pass it to the ISP for execution. |
| 203 | **/ |
| 204 | int qla4xxx_send_command_to_isp(struct scsi_qla_host *ha, struct srb * srb) |
| 205 | { |
| 206 | struct scsi_cmnd *cmd = srb->cmd; |
| 207 | struct ddb_entry *ddb_entry; |
| 208 | struct command_t3_entry *cmd_entry; |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 209 | int nseg; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 210 | uint16_t tot_dsds; |
| 211 | uint16_t req_cnt; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 212 | unsigned long flags; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 213 | uint32_t index; |
| 214 | char tag[2]; |
| 215 | |
| 216 | /* Get real lun and adapter */ |
| 217 | ddb_entry = srb->ddb; |
| 218 | |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 219 | tot_dsds = 0; |
| 220 | |
| 221 | /* Acquire hardware specific lock */ |
| 222 | spin_lock_irqsave(&ha->hardware_lock, flags); |
| 223 | |
| 224 | index = (uint32_t)cmd->request->tag; |
| 225 | |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 226 | /* |
| 227 | * Check to see if adapter is online before placing request on |
| 228 | * request queue. If a reset occurs and a request is in the queue, |
| 229 | * the firmware will still attempt to process the request, retrieving |
| 230 | * garbage for pointers. |
| 231 | */ |
| 232 | if (!test_bit(AF_ONLINE, &ha->flags)) { |
| 233 | DEBUG2(printk("scsi%ld: %s: Adapter OFFLINE! " |
| 234 | "Do not issue command.\n", |
| 235 | ha->host_no, __func__)); |
| 236 | goto queuing_error; |
| 237 | } |
| 238 | |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 239 | /* Calculate the number of request entries needed. */ |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 240 | nseg = scsi_dma_map(cmd); |
| 241 | if (nseg < 0) |
| 242 | goto queuing_error; |
| 243 | tot_dsds = nseg; |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 244 | |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 245 | req_cnt = qla4xxx_calc_request_entries(tot_dsds); |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 246 | if (!qla4xxx_space_in_req_ring(ha, req_cnt)) |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 247 | goto queuing_error; |
| 248 | |
| 249 | /* total iocbs active */ |
| 250 | if ((ha->iocb_cnt + req_cnt) >= REQUEST_QUEUE_DEPTH) |
| 251 | goto queuing_error; |
| 252 | |
| 253 | /* Build command packet */ |
| 254 | cmd_entry = (struct command_t3_entry *) ha->request_ptr; |
| 255 | memset(cmd_entry, 0, sizeof(struct command_t3_entry)); |
| 256 | cmd_entry->hdr.entryType = ET_COMMAND; |
| 257 | cmd_entry->handle = cpu_to_le32(index); |
| 258 | cmd_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index); |
| 259 | cmd_entry->connection_id = cpu_to_le16(ddb_entry->connection_id); |
| 260 | |
| 261 | int_to_scsilun(cmd->device->lun, &cmd_entry->lun); |
| 262 | cmd_entry->cmdSeqNum = cpu_to_le32(ddb_entry->CmdSn); |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 263 | cmd_entry->ttlByteCnt = cpu_to_le32(scsi_bufflen(cmd)); |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 264 | memcpy(cmd_entry->cdb, cmd->cmnd, cmd->cmd_len); |
| 265 | cmd_entry->dataSegCnt = cpu_to_le16(tot_dsds); |
| 266 | cmd_entry->hdr.entryCount = req_cnt; |
| 267 | |
| 268 | /* Set data transfer direction control flags |
| 269 | * NOTE: Look at data_direction bits iff there is data to be |
| 270 | * transferred, as the data direction bit is sometimed filled |
| 271 | * in when there is no data to be transferred */ |
| 272 | cmd_entry->control_flags = CF_NO_DATA; |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 273 | if (scsi_bufflen(cmd)) { |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 274 | if (cmd->sc_data_direction == DMA_TO_DEVICE) |
| 275 | cmd_entry->control_flags = CF_WRITE; |
| 276 | else if (cmd->sc_data_direction == DMA_FROM_DEVICE) |
| 277 | cmd_entry->control_flags = CF_READ; |
David C Somayajulu | d915058 | 2006-11-15 17:38:40 -0800 | [diff] [blame] | 278 | |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 279 | ha->bytes_xfered += scsi_bufflen(cmd); |
David C Somayajulu | d915058 | 2006-11-15 17:38:40 -0800 | [diff] [blame] | 280 | if (ha->bytes_xfered & ~0xFFFFF){ |
| 281 | ha->total_mbytes_xferred += ha->bytes_xfered >> 20; |
| 282 | ha->bytes_xfered &= 0xFFFFF; |
| 283 | } |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Set tagged queueing control flags */ |
| 287 | cmd_entry->control_flags |= CF_SIMPLE_TAG; |
| 288 | if (scsi_populate_tag_msg(cmd, tag)) |
| 289 | switch (tag[0]) { |
| 290 | case MSG_HEAD_TAG: |
| 291 | cmd_entry->control_flags |= CF_HEAD_TAG; |
| 292 | break; |
| 293 | case MSG_ORDERED_TAG: |
| 294 | cmd_entry->control_flags |= CF_ORDERED_TAG; |
| 295 | break; |
| 296 | } |
| 297 | |
Karen Higgins | 16ed55f | 2009-07-15 15:03:02 -0500 | [diff] [blame] | 298 | qla4xxx_advance_req_ring_ptr(ha); |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 299 | qla4xxx_build_scsi_iocbs(srb, cmd_entry, tot_dsds); |
| 300 | wmb(); |
| 301 | |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 302 | srb->cmd->host_scribble = (unsigned char *)srb; |
| 303 | |
| 304 | /* update counters */ |
| 305 | srb->state = SRB_ACTIVE_STATE; |
| 306 | srb->flags |= SRB_DMA_VALID; |
| 307 | |
| 308 | /* Track IOCB used */ |
| 309 | ha->iocb_cnt += req_cnt; |
| 310 | srb->iocb_cnt = req_cnt; |
| 311 | ha->req_q_count -= req_cnt; |
| 312 | |
| 313 | /* Debug print statements */ |
| 314 | writel(ha->request_in, &ha->reg->req_q_in); |
| 315 | readl(&ha->reg->req_q_in); |
| 316 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
| 317 | |
| 318 | return QLA_SUCCESS; |
| 319 | |
| 320 | queuing_error: |
FUJITA Tomonori | 5f7186c | 2007-05-26 14:08:20 +0900 | [diff] [blame] | 321 | if (tot_dsds) |
| 322 | scsi_dma_unmap(cmd); |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 323 | |
David Somayajulu | afaf5a2 | 2006-09-19 10:28:00 -0700 | [diff] [blame] | 324 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
| 325 | |
| 326 | return QLA_ERROR; |
| 327 | } |
| 328 | |