1 /* 2 * QLogic iSCSI HBA Driver 3 * Copyright (c) 2003-2010 QLogic Corporation 4 * 5 * See LICENSE.qla4xxx for copyright and licensing details. 6 */ 7 8 #include "ql4_def.h" 9 #include "ql4_glbl.h" 10 #include "ql4_dbg.h" 11 #include "ql4_inline.h" 12 13 #include <scsi/scsi_tcq.h> 14 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) ha->isp_ops->rd_shdw_req_q_out(ha); 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 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 **/ 59 static int qla4xxx_get_req_pkt(struct scsi_qla_host *ha, 60 struct queue_entry **queue_entry) 61 { 62 uint16_t req_cnt = 1; 63 64 if (qla4xxx_space_in_req_ring(ha, req_cnt)) { 65 *queue_entry = ha->request_ptr; 66 memset(*queue_entry, 0, sizeof(**queue_entry)); 67 68 qla4xxx_advance_req_ring_ptr(ha); 69 ha->req_q_count -= req_cnt; 70 return QLA_SUCCESS; 71 } 72 73 return QLA_ERROR; 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 **/ 85 int qla4xxx_send_marker_iocb(struct scsi_qla_host *ha, 86 struct ddb_entry *ddb_entry, int lun, uint16_t mrkr_mod) 87 { 88 struct qla4_marker_entry *marker_entry; 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); 106 marker_entry->modifier = cpu_to_le16(mrkr_mod); 107 int_to_scsilun(lun, &marker_entry->lun); 108 wmb(); 109 110 /* Tell ISP it's got a new I/O request */ 111 ha->isp_ops->queue_iocb(ha); 112 113 exit_send_marker: 114 spin_unlock_irqrestore(&ha->hardware_lock, flags); 115 return status; 116 } 117 118 static struct continuation_t1_entry * 119 qla4xxx_alloc_cont_entry(struct scsi_qla_host *ha) 120 { 121 struct continuation_t1_entry *cont_entry; 122 123 cont_entry = (struct continuation_t1_entry *)ha->request_ptr; 124 125 qla4xxx_advance_req_ring_ptr(ha); 126 127 /* Load packet defaults */ 128 cont_entry->hdr.entryType = ET_CONTINUE; 129 cont_entry->hdr.entryCount = 1; 130 cont_entry->hdr.systemDefined = (uint8_t) cpu_to_le16(ha->request_in); 131 132 return cont_entry; 133 } 134 135 static uint16_t qla4xxx_calc_request_entries(uint16_t dsds) 136 { 137 uint16_t iocbs; 138 139 iocbs = 1; 140 if (dsds > COMMAND_SEG) { 141 iocbs += (dsds - COMMAND_SEG) / CONTINUE_SEG; 142 if ((dsds - COMMAND_SEG) % CONTINUE_SEG) 143 iocbs++; 144 } 145 return iocbs; 146 } 147 148 static void qla4xxx_build_scsi_iocbs(struct srb *srb, 149 struct command_t3_entry *cmd_entry, 150 uint16_t tot_dsds) 151 { 152 struct scsi_qla_host *ha; 153 uint16_t avail_dsds; 154 struct data_seg_a64 *cur_dsd; 155 struct scsi_cmnd *cmd; 156 struct scatterlist *sg; 157 int i; 158 159 cmd = srb->cmd; 160 ha = srb->ha; 161 162 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { 163 /* No data being transferred */ 164 cmd_entry->ttlByteCnt = __constant_cpu_to_le32(0); 165 return; 166 } 167 168 avail_dsds = COMMAND_SEG; 169 cur_dsd = (struct data_seg_a64 *) & (cmd_entry->dataseg[0]); 170 171 scsi_for_each_sg(cmd, sg, tot_dsds, i) { 172 dma_addr_t sle_dma; 173 174 /* Allocate additional continuation packets? */ 175 if (avail_dsds == 0) { 176 struct continuation_t1_entry *cont_entry; 177 178 cont_entry = qla4xxx_alloc_cont_entry(ha); 179 cur_dsd = 180 (struct data_seg_a64 *) 181 &cont_entry->dataseg[0]; 182 avail_dsds = CONTINUE_SEG; 183 } 184 185 sle_dma = sg_dma_address(sg); 186 cur_dsd->base.addrLow = cpu_to_le32(LSDW(sle_dma)); 187 cur_dsd->base.addrHigh = cpu_to_le32(MSDW(sle_dma)); 188 cur_dsd->count = cpu_to_le32(sg_dma_len(sg)); 189 avail_dsds--; 190 191 cur_dsd++; 192 } 193 } 194 195 /** 196 * qla4_8xxx_queue_iocb - Tell ISP it's got new request(s) 197 * @ha: pointer to host adapter structure. 198 * 199 * This routine notifies the ISP that one or more new request 200 * queue entries have been placed on the request queue. 201 **/ 202 void qla4_8xxx_queue_iocb(struct scsi_qla_host *ha) 203 { 204 uint32_t dbval = 0; 205 206 dbval = 0x14 | (ha->func_num << 5); 207 dbval = dbval | (0 << 8) | (ha->request_in << 16); 208 209 qla4_8xxx_wr_32(ha, ha->nx_db_wr_ptr, ha->request_in); 210 } 211 212 /** 213 * qla4_8xxx_complete_iocb - Tell ISP we're done with response(s) 214 * @ha: pointer to host adapter structure. 215 * 216 * This routine notifies the ISP that one or more response/completion 217 * queue entries have been processed by the driver. 218 * This also clears the interrupt. 219 **/ 220 void qla4_8xxx_complete_iocb(struct scsi_qla_host *ha) 221 { 222 writel(ha->response_out, &ha->qla4_8xxx_reg->rsp_q_out); 223 readl(&ha->qla4_8xxx_reg->rsp_q_out); 224 } 225 226 /** 227 * qla4xxx_queue_iocb - Tell ISP it's got new request(s) 228 * @ha: pointer to host adapter structure. 229 * 230 * This routine is notifies the ISP that one or more new request 231 * queue entries have been placed on the request queue. 232 **/ 233 void qla4xxx_queue_iocb(struct scsi_qla_host *ha) 234 { 235 writel(ha->request_in, &ha->reg->req_q_in); 236 readl(&ha->reg->req_q_in); 237 } 238 239 /** 240 * qla4xxx_complete_iocb - Tell ISP we're done with response(s) 241 * @ha: pointer to host adapter structure. 242 * 243 * This routine is notifies the ISP that one or more response/completion 244 * queue entries have been processed by the driver. 245 * This also clears the interrupt. 246 **/ 247 void qla4xxx_complete_iocb(struct scsi_qla_host *ha) 248 { 249 writel(ha->response_out, &ha->reg->rsp_q_out); 250 readl(&ha->reg->rsp_q_out); 251 } 252 253 /** 254 * qla4xxx_send_command_to_isp - issues command to HBA 255 * @ha: pointer to host adapter structure. 256 * @srb: pointer to SCSI Request Block to be sent to ISP 257 * 258 * This routine is called by qla4xxx_queuecommand to build an ISP 259 * command and pass it to the ISP for execution. 260 **/ 261 int qla4xxx_send_command_to_isp(struct scsi_qla_host *ha, struct srb * srb) 262 { 263 struct scsi_cmnd *cmd = srb->cmd; 264 struct ddb_entry *ddb_entry; 265 struct command_t3_entry *cmd_entry; 266 int nseg; 267 uint16_t tot_dsds; 268 uint16_t req_cnt; 269 unsigned long flags; 270 uint32_t index; 271 char tag[2]; 272 273 /* Get real lun and adapter */ 274 ddb_entry = srb->ddb; 275 276 tot_dsds = 0; 277 278 /* Acquire hardware specific lock */ 279 spin_lock_irqsave(&ha->hardware_lock, flags); 280 281 index = (uint32_t)cmd->request->tag; 282 283 /* 284 * Check to see if adapter is online before placing request on 285 * request queue. If a reset occurs and a request is in the queue, 286 * the firmware will still attempt to process the request, retrieving 287 * garbage for pointers. 288 */ 289 if (!test_bit(AF_ONLINE, &ha->flags)) { 290 DEBUG2(printk("scsi%ld: %s: Adapter OFFLINE! " 291 "Do not issue command.\n", 292 ha->host_no, __func__)); 293 goto queuing_error; 294 } 295 296 /* Calculate the number of request entries needed. */ 297 nseg = scsi_dma_map(cmd); 298 if (nseg < 0) 299 goto queuing_error; 300 tot_dsds = nseg; 301 302 req_cnt = qla4xxx_calc_request_entries(tot_dsds); 303 if (!qla4xxx_space_in_req_ring(ha, req_cnt)) 304 goto queuing_error; 305 306 /* total iocbs active */ 307 if ((ha->iocb_cnt + req_cnt) >= REQUEST_QUEUE_DEPTH) 308 goto queuing_error; 309 310 /* Build command packet */ 311 cmd_entry = (struct command_t3_entry *) ha->request_ptr; 312 memset(cmd_entry, 0, sizeof(struct command_t3_entry)); 313 cmd_entry->hdr.entryType = ET_COMMAND; 314 cmd_entry->handle = cpu_to_le32(index); 315 cmd_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index); 316 317 int_to_scsilun(cmd->device->lun, &cmd_entry->lun); 318 cmd_entry->ttlByteCnt = cpu_to_le32(scsi_bufflen(cmd)); 319 memcpy(cmd_entry->cdb, cmd->cmnd, cmd->cmd_len); 320 cmd_entry->dataSegCnt = cpu_to_le16(tot_dsds); 321 cmd_entry->hdr.entryCount = req_cnt; 322 323 /* Set data transfer direction control flags 324 * NOTE: Look at data_direction bits iff there is data to be 325 * transferred, as the data direction bit is sometimed filled 326 * in when there is no data to be transferred */ 327 cmd_entry->control_flags = CF_NO_DATA; 328 if (scsi_bufflen(cmd)) { 329 if (cmd->sc_data_direction == DMA_TO_DEVICE) 330 cmd_entry->control_flags = CF_WRITE; 331 else if (cmd->sc_data_direction == DMA_FROM_DEVICE) 332 cmd_entry->control_flags = CF_READ; 333 334 ha->bytes_xfered += scsi_bufflen(cmd); 335 if (ha->bytes_xfered & ~0xFFFFF){ 336 ha->total_mbytes_xferred += ha->bytes_xfered >> 20; 337 ha->bytes_xfered &= 0xFFFFF; 338 } 339 } 340 341 /* Set tagged queueing control flags */ 342 cmd_entry->control_flags |= CF_SIMPLE_TAG; 343 if (scsi_populate_tag_msg(cmd, tag)) 344 switch (tag[0]) { 345 case MSG_HEAD_TAG: 346 cmd_entry->control_flags |= CF_HEAD_TAG; 347 break; 348 case MSG_ORDERED_TAG: 349 cmd_entry->control_flags |= CF_ORDERED_TAG; 350 break; 351 } 352 353 qla4xxx_advance_req_ring_ptr(ha); 354 qla4xxx_build_scsi_iocbs(srb, cmd_entry, tot_dsds); 355 wmb(); 356 357 srb->cmd->host_scribble = (unsigned char *)(unsigned long)index; 358 359 /* update counters */ 360 srb->state = SRB_ACTIVE_STATE; 361 srb->flags |= SRB_DMA_VALID; 362 363 /* Track IOCB used */ 364 ha->iocb_cnt += req_cnt; 365 srb->iocb_cnt = req_cnt; 366 ha->req_q_count -= req_cnt; 367 368 ha->isp_ops->queue_iocb(ha); 369 spin_unlock_irqrestore(&ha->hardware_lock, flags); 370 371 return QLA_SUCCESS; 372 373 queuing_error: 374 if (tot_dsds) 375 scsi_dma_unmap(cmd); 376 377 spin_unlock_irqrestore(&ha->hardware_lock, flags); 378 379 return QLA_ERROR; 380 } 381 382 int qla4xxx_send_passthru0(struct iscsi_task *task) 383 { 384 struct passthru0 *passthru_iocb; 385 struct iscsi_session *sess = task->conn->session; 386 struct ddb_entry *ddb_entry = sess->dd_data; 387 struct scsi_qla_host *ha = ddb_entry->ha; 388 struct ql4_task_data *task_data = task->dd_data; 389 uint16_t ctrl_flags = 0; 390 unsigned long flags; 391 int ret = QLA_ERROR; 392 393 spin_lock_irqsave(&ha->hardware_lock, flags); 394 task_data->iocb_req_cnt = 1; 395 /* Put the IOCB on the request queue */ 396 if (!qla4xxx_space_in_req_ring(ha, task_data->iocb_req_cnt)) 397 goto queuing_error; 398 399 passthru_iocb = (struct passthru0 *) ha->request_ptr; 400 401 memset(passthru_iocb, 0, sizeof(struct passthru0)); 402 passthru_iocb->hdr.entryType = ET_PASSTHRU0; 403 passthru_iocb->hdr.systemDefined = SD_ISCSI_PDU; 404 passthru_iocb->hdr.entryCount = task_data->iocb_req_cnt; 405 passthru_iocb->handle = task->itt; 406 passthru_iocb->target = cpu_to_le16(ddb_entry->fw_ddb_index); 407 passthru_iocb->timeout = cpu_to_le16(PT_DEFAULT_TIMEOUT); 408 409 /* Setup the out & in DSDs */ 410 if (task_data->req_len) { 411 memcpy((uint8_t *)task_data->req_buffer + 412 sizeof(struct iscsi_hdr), task->data, task->data_count); 413 ctrl_flags |= PT_FLAG_SEND_BUFFER; 414 passthru_iocb->out_dsd.base.addrLow = 415 cpu_to_le32(LSDW(task_data->req_dma)); 416 passthru_iocb->out_dsd.base.addrHigh = 417 cpu_to_le32(MSDW(task_data->req_dma)); 418 passthru_iocb->out_dsd.count = 419 cpu_to_le32(task->data_count + 420 sizeof(struct iscsi_hdr)); 421 } 422 if (task_data->resp_len) { 423 passthru_iocb->in_dsd.base.addrLow = 424 cpu_to_le32(LSDW(task_data->resp_dma)); 425 passthru_iocb->in_dsd.base.addrHigh = 426 cpu_to_le32(MSDW(task_data->resp_dma)); 427 passthru_iocb->in_dsd.count = 428 cpu_to_le32(task_data->resp_len); 429 } 430 431 ctrl_flags |= (PT_FLAG_ISCSI_PDU | PT_FLAG_WAIT_4_RESPONSE); 432 passthru_iocb->control_flags = cpu_to_le16(ctrl_flags); 433 434 /* Update the request pointer */ 435 qla4xxx_advance_req_ring_ptr(ha); 436 wmb(); 437 438 /* Track IOCB used */ 439 ha->iocb_cnt += task_data->iocb_req_cnt; 440 ha->req_q_count -= task_data->iocb_req_cnt; 441 ha->isp_ops->queue_iocb(ha); 442 ret = QLA_SUCCESS; 443 444 queuing_error: 445 spin_unlock_irqrestore(&ha->hardware_lock, flags); 446 return ret; 447 } 448 449 static struct mrb *qla4xxx_get_new_mrb(struct scsi_qla_host *ha) 450 { 451 struct mrb *mrb; 452 453 mrb = kzalloc(sizeof(*mrb), GFP_KERNEL); 454 if (!mrb) 455 return mrb; 456 457 mrb->ha = ha; 458 return mrb; 459 } 460 461 static int qla4xxx_send_mbox_iocb(struct scsi_qla_host *ha, struct mrb *mrb, 462 uint32_t *in_mbox) 463 { 464 int rval = QLA_SUCCESS; 465 uint32_t i; 466 unsigned long flags; 467 uint32_t index = 0; 468 469 /* Acquire hardware specific lock */ 470 spin_lock_irqsave(&ha->hardware_lock, flags); 471 472 /* Get pointer to the queue entry for the marker */ 473 rval = qla4xxx_get_req_pkt(ha, (struct queue_entry **) &(mrb->mbox)); 474 if (rval != QLA_SUCCESS) 475 goto exit_mbox_iocb; 476 477 index = ha->mrb_index; 478 /* get valid mrb index*/ 479 for (i = 0; i < MAX_MRB; i++) { 480 index++; 481 if (index == MAX_MRB) 482 index = 1; 483 if (ha->active_mrb_array[index] == NULL) { 484 ha->mrb_index = index; 485 break; 486 } 487 } 488 489 mrb->iocb_cnt = 1; 490 ha->active_mrb_array[index] = mrb; 491 mrb->mbox->handle = index; 492 mrb->mbox->hdr.entryType = ET_MBOX_CMD; 493 mrb->mbox->hdr.entryCount = mrb->iocb_cnt; 494 memcpy(mrb->mbox->in_mbox, in_mbox, 32); 495 mrb->mbox_cmd = in_mbox[0]; 496 wmb(); 497 498 ha->isp_ops->queue_iocb(ha); 499 exit_mbox_iocb: 500 spin_unlock_irqrestore(&ha->hardware_lock, flags); 501 return rval; 502 } 503 504 int qla4xxx_ping_iocb(struct scsi_qla_host *ha, uint32_t options, 505 uint32_t payload_size, uint32_t pid, uint8_t *ipaddr) 506 { 507 uint32_t in_mbox[8]; 508 struct mrb *mrb = NULL; 509 int rval = QLA_SUCCESS; 510 511 memset(in_mbox, 0, sizeof(in_mbox)); 512 513 mrb = qla4xxx_get_new_mrb(ha); 514 if (!mrb) { 515 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: fail to get new mrb\n", 516 __func__)); 517 rval = QLA_ERROR; 518 goto exit_ping; 519 } 520 521 in_mbox[0] = MBOX_CMD_PING; 522 in_mbox[1] = options; 523 memcpy(&in_mbox[2], &ipaddr[0], 4); 524 memcpy(&in_mbox[3], &ipaddr[4], 4); 525 memcpy(&in_mbox[4], &ipaddr[8], 4); 526 memcpy(&in_mbox[5], &ipaddr[12], 4); 527 in_mbox[6] = payload_size; 528 529 mrb->pid = pid; 530 rval = qla4xxx_send_mbox_iocb(ha, mrb, in_mbox); 531 532 if (rval != QLA_SUCCESS) 533 goto exit_ping; 534 535 return rval; 536 exit_ping: 537 kfree(mrb); 538 return rval; 539 } 540