2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
10 #include <linux/moduleparam.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
13 #include <linux/list.h>
15 #include <scsi/scsi_tcq.h>
16 #include <scsi/scsicam.h>
17 #include <linux/delay.h>
20 qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
22 if (vha->vp_idx && vha->timer_active) {
23 del_timer_sync(&vha->timer);
24 vha->timer_active = 0;
29 qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
32 struct qla_hw_data *ha = vha->hw;
34 /* Find an empty slot and assign an vp_id */
35 mutex_lock(&ha->vport_lock);
36 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
37 if (vp_id > ha->max_npiv_vports) {
38 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
39 vp_id, ha->max_npiv_vports));
40 mutex_unlock(&ha->vport_lock);
44 set_bit(vp_id, ha->vp_idx_map);
47 list_add_tail(&vha->list, &ha->vp_list);
48 mutex_unlock(&ha->vport_lock);
53 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
56 struct qla_hw_data *ha = vha->hw;
58 mutex_lock(&ha->vport_lock);
61 clear_bit(vp_id, ha->vp_idx_map);
63 mutex_unlock(&ha->vport_lock);
66 static scsi_qla_host_t *
67 qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
70 struct scsi_qla_host *tvha;
72 /* Locate matching device in database. */
73 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
74 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
81 * qla2x00_mark_vp_devices_dead
82 * Updates fcport state when device goes offline.
85 * ha = adapter block pointer.
86 * fcport = port structure pointer.
94 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
98 list_for_each_entry(fcport, &vha->vp_fcports, list) {
99 DEBUG15(printk("scsi(%ld): Marking port dead, "
100 "loop_id=0x%04x :%x\n",
101 vha->host_no, fcport->loop_id, fcport->vp_idx));
103 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
104 qla2x00_mark_device_lost(vha, fcport, 0, 0);
105 atomic_set(&fcport->state, FCS_UNCONFIGURED);
110 qla24xx_disable_vp(scsi_qla_host_t *vha)
114 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
115 atomic_set(&vha->loop_state, LOOP_DOWN);
116 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
118 qla2x00_mark_vp_devices_dead(vha);
119 atomic_set(&vha->vp_state, VP_FAILED);
120 vha->flags.management_server_logged_in = 0;
121 if (ret == QLA_SUCCESS) {
122 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
124 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
131 qla24xx_enable_vp(scsi_qla_host_t *vha)
134 struct qla_hw_data *ha = vha->hw;
135 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
137 /* Check if physical ha port is Up */
138 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
139 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
140 vha->vp_err_state = VP_ERR_PORTDWN;
141 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
145 /* Initialize the new vport unless it is a persistent port */
146 mutex_lock(&ha->vport_lock);
147 ret = qla24xx_modify_vp_config(vha);
148 mutex_unlock(&ha->vport_lock);
150 if (ret != QLA_SUCCESS) {
151 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
155 DEBUG15(qla_printk(KERN_INFO, ha,
156 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
160 DEBUG15(qla_printk(KERN_INFO, ha,
161 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
166 qla24xx_configure_vp(scsi_qla_host_t *vha)
168 struct fc_vport *fc_vport;
171 fc_vport = vha->fc_vport;
173 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
174 vha->host_no, __func__));
175 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
176 if (ret != QLA_SUCCESS) {
177 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
178 "receiving of RSCN requests: 0x%x\n", ret));
181 /* Corresponds to SCR enabled */
182 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
185 vha->flags.online = 1;
186 if (qla24xx_configure_vhba(vha))
189 atomic_set(&vha->vp_state, VP_ACTIVE);
190 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
194 qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
196 scsi_qla_host_t *vha, *tvha;
197 struct qla_hw_data *ha = rsp->hw;
200 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
203 case MBA_LIP_OCCURRED:
207 case MBA_POINT_TO_POINT:
208 case MBA_CHG_IN_CONNECTION:
209 case MBA_PORT_UPDATE:
210 case MBA_RSCN_UPDATE:
211 DEBUG15(printk("scsi(%ld)%s: Async_event for"
212 " VP[%d], mb = 0x%x, vha=%p\n",
213 vha->host_no, __func__, i, *mb, vha));
214 qla2x00_async_event(vha, rsp, mb);
223 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
226 * Physical port will do most of the abort and recovery work. We can
227 * just treat it as a loop down
229 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
230 atomic_set(&vha->loop_state, LOOP_DOWN);
231 qla2x00_mark_all_devices_lost(vha, 0);
233 if (!atomic_read(&vha->loop_down_timer))
234 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
238 * To exclusively reset vport, we need to log it out first. Note: this
239 * control_vp can fail if ISP reset is already issued, this is
240 * expected, as the vp would be already logged out due to ISP reset.
242 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
243 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
245 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
246 vha->host_no, vha->vp_idx));
247 return qla24xx_enable_vp(vha);
251 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
253 qla2x00_do_work(vha);
255 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
256 /* VP acquired. complete port configuration */
257 qla24xx_configure_vp(vha);
261 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
262 qla2x00_update_fcports(vha);
263 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
266 if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
267 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
268 atomic_read(&vha->loop_state) != LOOP_DOWN) {
270 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
272 qla2x00_relogin(vha);
274 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
278 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
279 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
280 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
283 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
284 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
285 qla2x00_loop_resync(vha);
286 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
294 qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
297 struct qla_hw_data *ha = vha->hw;
299 struct scsi_qla_host *tvp;
303 if (list_empty(&ha->vp_list))
306 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
308 if (!(ha->current_topology & ISP_CFG_F))
311 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
313 ret = qla2x00_do_dpc_vp(vp);
318 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
320 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
321 struct qla_hw_data *ha = base_vha->hw;
322 scsi_qla_host_t *vha;
323 uint8_t port_name[WWN_SIZE];
325 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
326 return VPCERR_UNSUPPORTED;
328 /* Check up the F/W and H/W support NPIV */
329 if (!ha->flags.npiv_supported)
330 return VPCERR_UNSUPPORTED;
332 /* Check up whether npiv supported switch presented */
333 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
334 return VPCERR_NO_FABRIC_SUPP;
336 /* Check up unique WWPN */
337 u64_to_wwn(fc_vport->port_name, port_name);
338 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
339 return VPCERR_BAD_WWN;
340 vha = qla24xx_find_vhost_by_name(ha, port_name);
342 return VPCERR_BAD_WWN;
344 /* Check up max-npiv-supports */
345 if (ha->num_vhosts > ha->max_npiv_vports) {
346 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
347 "max_npv_vports %ud.\n", base_vha->host_no,
348 ha->num_vhosts, ha->max_npiv_vports));
349 return VPCERR_UNSUPPORTED;
355 qla24xx_create_vhost(struct fc_vport *fc_vport)
357 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
358 struct qla_hw_data *ha = base_vha->hw;
359 scsi_qla_host_t *vha;
360 struct scsi_host_template *sht = &qla2xxx_driver_template;
361 struct Scsi_Host *host;
363 vha = qla2x00_create_host(sht, ha);
365 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
370 fc_vport->dd_data = vha;
372 u64_to_wwn(fc_vport->node_name, vha->node_name);
373 u64_to_wwn(fc_vport->port_name, vha->port_name);
375 vha->fc_vport = fc_vport;
376 vha->device_flags = 0;
377 vha->vp_idx = qla24xx_allocate_vp_id(vha);
378 if (vha->vp_idx > ha->max_npiv_vports) {
379 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
381 goto create_vhost_failed;
383 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
388 * To fix the issue of processing a parent's RSCN for the vport before
389 * its SCR is complete.
391 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
392 atomic_set(&vha->loop_state, LOOP_DOWN);
393 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
395 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
397 vha->req = base_vha->req;
398 host->can_queue = base_vha->req->length + 128;
400 host->cmd_per_lun = 3;
401 host->max_cmd_len = MAX_CMDSZ;
402 host->max_channel = MAX_BUSES - 1;
403 host->max_lun = MAX_LUNS;
404 host->unique_id = host->host_no;
405 host->max_id = MAX_TARGETS_2200;
406 host->transportt = qla2xxx_transport_vport_template;
408 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
411 vha->flags.init_done = 1;
413 mutex_lock(&ha->vport_lock);
414 set_bit(vha->vp_idx, ha->vp_idx_map);
415 ha->cur_vport_count++;
416 mutex_unlock(&ha->vport_lock);
425 qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
427 struct qla_hw_data *ha = vha->hw;
428 uint16_t que_id = req->id;
430 dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
431 sizeof(request_t), req->ring, req->dma);
435 ha->req_q_map[que_id] = NULL;
436 mutex_lock(&ha->vport_lock);
437 clear_bit(que_id, ha->req_qid_map);
438 mutex_unlock(&ha->vport_lock);
445 qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
447 struct qla_hw_data *ha = vha->hw;
448 uint16_t que_id = rsp->id;
450 if (rsp->msix && rsp->msix->have_irq) {
451 free_irq(rsp->msix->vector, rsp);
452 rsp->msix->have_irq = 0;
453 rsp->msix->rsp = NULL;
455 dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
456 sizeof(response_t), rsp->ring, rsp->dma);
460 ha->rsp_q_map[que_id] = NULL;
461 mutex_lock(&ha->vport_lock);
462 clear_bit(que_id, ha->rsp_qid_map);
463 mutex_unlock(&ha->vport_lock);
470 qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
475 req->options |= BIT_0;
476 ret = qla25xx_init_req_que(vha, req);
478 if (ret == QLA_SUCCESS)
479 qla25xx_free_req_que(vha, req);
485 qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
490 rsp->options |= BIT_0;
491 ret = qla25xx_init_rsp_que(vha, rsp);
493 if (ret == QLA_SUCCESS)
494 qla25xx_free_rsp_que(vha, rsp);
499 int qla25xx_update_req_que(struct scsi_qla_host *vha, uint8_t que, uint8_t qos)
502 struct qla_hw_data *ha = vha->hw;
503 struct req_que *req = ha->req_q_map[que];
505 req->options |= BIT_3;
507 ret = qla25xx_init_req_que(vha, req);
508 if (ret != QLA_SUCCESS)
509 DEBUG2_17(printk(KERN_WARNING "%s failed\n", __func__));
510 /* restore options bit */
511 req->options &= ~BIT_3;
516 /* Delete all queues for a given vhost */
518 qla25xx_delete_queues(struct scsi_qla_host *vha)
521 struct req_que *req = NULL;
522 struct rsp_que *rsp = NULL;
523 struct qla_hw_data *ha = vha->hw;
525 /* Delete request queues */
526 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
527 req = ha->req_q_map[cnt];
529 ret = qla25xx_delete_req_que(vha, req);
530 if (ret != QLA_SUCCESS) {
531 qla_printk(KERN_WARNING, ha,
532 "Couldn't delete req que %d\n",
539 /* Delete response queues */
540 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
541 rsp = ha->rsp_q_map[cnt];
543 ret = qla25xx_delete_rsp_que(vha, rsp);
544 if (ret != QLA_SUCCESS) {
545 qla_printk(KERN_WARNING, ha,
546 "Couldn't delete rsp que %d\n",
556 qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
557 uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
560 struct req_que *req = NULL;
561 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
563 device_reg_t __iomem *reg;
566 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
568 qla_printk(KERN_WARNING, ha, "could not allocate memory"
569 "for request que\n");
573 req->length = REQUEST_ENTRY_CNT_24XX;
574 req->ring = dma_alloc_coherent(&ha->pdev->dev,
575 (req->length + 1) * sizeof(request_t),
576 &req->dma, GFP_KERNEL);
577 if (req->ring == NULL) {
578 qla_printk(KERN_WARNING, ha,
579 "Memory Allocation failed - request_ring\n");
583 mutex_lock(&ha->vport_lock);
584 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
585 if (que_id >= ha->max_req_queues) {
586 mutex_unlock(&ha->vport_lock);
587 qla_printk(KERN_INFO, ha, "No resources to create "
588 "additional request queue\n");
591 set_bit(que_id, ha->req_qid_map);
592 ha->req_q_map[que_id] = req;
594 req->vp_idx = vp_idx;
600 req->rsp = ha->rsp_q_map[rsp_que];
601 /* Use alternate PCI bus number */
604 /* Use alternate PCI devfn */
607 req->options = options;
609 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
610 req->outstanding_cmds[cnt] = NULL;
611 req->current_outstanding_cmd = 1;
613 req->ring_ptr = req->ring;
615 req->cnt = req->length;
617 reg = ISP_QUE_REG(ha, que_id);
618 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
619 mutex_unlock(&ha->vport_lock);
621 ret = qla25xx_init_req_que(base_vha, req);
622 if (ret != QLA_SUCCESS) {
623 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
624 mutex_lock(&ha->vport_lock);
625 clear_bit(que_id, ha->req_qid_map);
626 mutex_unlock(&ha->vport_lock);
633 qla25xx_free_req_que(base_vha, req);
638 static void qla_do_work(struct work_struct *work)
641 struct rsp_que *rsp = container_of(work, struct rsp_que, q_work);
642 struct scsi_qla_host *vha;
643 struct qla_hw_data *ha = rsp->hw;
645 spin_lock_irqsave(&rsp->hw->hardware_lock, flags);
646 vha = pci_get_drvdata(ha->pdev);
647 qla24xx_process_response_queue(vha, rsp);
648 spin_unlock_irqrestore(&rsp->hw->hardware_lock, flags);
651 /* create response queue */
653 qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
654 uint8_t vp_idx, uint16_t rid, int req)
657 struct rsp_que *rsp = NULL;
658 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
660 device_reg_t __iomem *reg;
662 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
664 qla_printk(KERN_WARNING, ha, "could not allocate memory for"
669 rsp->length = RESPONSE_ENTRY_CNT_MQ;
670 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
671 (rsp->length + 1) * sizeof(response_t),
672 &rsp->dma, GFP_KERNEL);
673 if (rsp->ring == NULL) {
674 qla_printk(KERN_WARNING, ha,
675 "Memory Allocation failed - response_ring\n");
679 mutex_lock(&ha->vport_lock);
680 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
681 if (que_id >= ha->max_rsp_queues) {
682 mutex_unlock(&ha->vport_lock);
683 qla_printk(KERN_INFO, ha, "No resources to create "
684 "additional response queue\n");
687 set_bit(que_id, ha->rsp_qid_map);
689 if (ha->flags.msix_enabled)
690 rsp->msix = &ha->msix_entries[que_id + 1];
692 qla_printk(KERN_WARNING, ha, "msix not enabled\n");
694 ha->rsp_q_map[que_id] = rsp;
696 rsp->vp_idx = vp_idx;
698 /* Use alternate PCI bus number */
701 /* Use alternate PCI devfn */
704 /* Enable MSIX handshake mode on for uncapable adapters */
705 if (!IS_MSIX_NACK_CAPABLE(ha))
708 rsp->options = options;
710 reg = ISP_QUE_REG(ha, que_id);
711 rsp->rsp_q_in = ®->isp25mq.rsp_q_in;
712 rsp->rsp_q_out = ®->isp25mq.rsp_q_out;
713 mutex_unlock(&ha->vport_lock);
715 ret = qla25xx_request_irq(rsp);
719 ret = qla25xx_init_rsp_que(base_vha, rsp);
720 if (ret != QLA_SUCCESS) {
721 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
722 mutex_lock(&ha->vport_lock);
723 clear_bit(que_id, ha->rsp_qid_map);
724 mutex_unlock(&ha->vport_lock);
728 rsp->req = ha->req_q_map[req];
732 qla2x00_init_response_q_entries(rsp);
734 INIT_WORK(&rsp->q_work, qla_do_work);
738 qla25xx_free_rsp_que(base_vha, rsp);
744 qla25xx_create_queues(struct scsi_qla_host *vha, uint8_t qos)
746 uint16_t options = 0;
748 struct qla_hw_data *ha = vha->hw;
752 ret = qla25xx_create_rsp_que(ha, options, vha->vp_idx, 0, -1);
754 qla_printk(KERN_WARNING, ha, "Response Que create failed\n");
757 qla_printk(KERN_INFO, ha, "Response Que:%d created.\n", ret);
758 rsp = ha->rsp_q_map[ret];
763 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, ret,
766 vha->req = ha->req_q_map[ret];
767 qla_printk(KERN_INFO, ha, "Request Que:%d created.\n", ret);
769 qla_printk(KERN_WARNING, ha, "Request Que create failed\n");
770 rsp->req = ha->req_q_map[ret];