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/smp_lock.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);
46 ha->cur_vport_count++;
48 list_add_tail(&vha->list, &ha->vp_list);
49 mutex_unlock(&ha->vport_lock);
54 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
57 struct qla_hw_data *ha = vha->hw;
59 mutex_lock(&ha->vport_lock);
62 ha->cur_vport_count--;
63 clear_bit(vp_id, ha->vp_idx_map);
65 mutex_unlock(&ha->vport_lock);
68 static scsi_qla_host_t *
69 qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
72 struct scsi_qla_host *tvha;
74 /* Locate matching device in database. */
75 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
76 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
83 * qla2x00_mark_vp_devices_dead
84 * Updates fcport state when device goes offline.
87 * ha = adapter block pointer.
88 * fcport = port structure pointer.
96 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
100 list_for_each_entry(fcport, &vha->vp_fcports, list) {
101 DEBUG15(printk("scsi(%ld): Marking port dead, "
102 "loop_id=0x%04x :%x\n",
103 vha->host_no, fcport->loop_id, fcport->vp_idx));
105 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
106 qla2x00_mark_device_lost(vha, fcport, 0, 0);
107 atomic_set(&fcport->state, FCS_UNCONFIGURED);
112 qla24xx_disable_vp(scsi_qla_host_t *vha)
116 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
117 atomic_set(&vha->loop_state, LOOP_DOWN);
118 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
120 qla2x00_mark_vp_devices_dead(vha);
121 atomic_set(&vha->vp_state, VP_FAILED);
122 vha->flags.management_server_logged_in = 0;
123 if (ret == QLA_SUCCESS) {
124 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
126 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
133 qla24xx_enable_vp(scsi_qla_host_t *vha)
136 struct qla_hw_data *ha = vha->hw;
137 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
139 /* Check if physical ha port is Up */
140 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
141 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
142 vha->vp_err_state = VP_ERR_PORTDWN;
143 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
147 /* Initialize the new vport unless it is a persistent port */
148 mutex_lock(&ha->vport_lock);
149 ret = qla24xx_modify_vp_config(vha);
150 mutex_unlock(&ha->vport_lock);
152 if (ret != QLA_SUCCESS) {
153 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
157 DEBUG15(qla_printk(KERN_INFO, ha,
158 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
162 DEBUG15(qla_printk(KERN_INFO, ha,
163 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
168 qla24xx_configure_vp(scsi_qla_host_t *vha)
170 struct fc_vport *fc_vport;
173 fc_vport = vha->fc_vport;
175 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
176 vha->host_no, __func__));
177 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
178 if (ret != QLA_SUCCESS) {
179 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
180 "receiving of RSCN requests: 0x%x\n", ret));
183 /* Corresponds to SCR enabled */
184 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
187 vha->flags.online = 1;
188 if (qla24xx_configure_vhba(vha))
191 atomic_set(&vha->vp_state, VP_ACTIVE);
192 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
196 qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
198 scsi_qla_host_t *vha, *tvha;
199 struct qla_hw_data *ha = rsp->hw;
202 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
205 case MBA_LIP_OCCURRED:
209 case MBA_POINT_TO_POINT:
210 case MBA_CHG_IN_CONNECTION:
211 case MBA_PORT_UPDATE:
212 case MBA_RSCN_UPDATE:
213 DEBUG15(printk("scsi(%ld)%s: Async_event for"
214 " VP[%d], mb = 0x%x, vha=%p\n",
215 vha->host_no, __func__, i, *mb, vha));
216 qla2x00_async_event(vha, rsp, mb);
225 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
228 * Physical port will do most of the abort and recovery work. We can
229 * just treat it as a loop down
231 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
232 atomic_set(&vha->loop_state, LOOP_DOWN);
233 qla2x00_mark_all_devices_lost(vha, 0);
235 if (!atomic_read(&vha->loop_down_timer))
236 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
239 /* To exclusively reset vport, we need to log it out first.*/
240 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
241 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
243 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
244 vha->host_no, vha->vp_idx));
245 return qla24xx_enable_vp(vha);
249 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
251 struct qla_hw_data *ha = vha->hw;
252 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
254 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
255 /* VP acquired. complete port configuration */
256 if (atomic_read(&base_vha->loop_state) == LOOP_READY) {
257 qla24xx_configure_vp(vha);
259 set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
260 set_bit(VP_DPC_NEEDED, &base_vha->dpc_flags);
266 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
267 qla2x00_update_fcports(vha);
268 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
271 if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
272 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
273 atomic_read(&vha->loop_state) != LOOP_DOWN) {
275 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
277 qla2x00_relogin(vha);
279 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
283 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
284 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
285 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
288 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
289 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
290 qla2x00_loop_resync(vha);
291 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
299 qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
302 struct qla_hw_data *ha = vha->hw;
304 struct scsi_qla_host *tvp;
308 if (list_empty(&ha->vp_list))
311 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
313 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
315 ret = qla2x00_do_dpc_vp(vp);
320 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
322 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
323 struct qla_hw_data *ha = base_vha->hw;
324 scsi_qla_host_t *vha;
325 uint8_t port_name[WWN_SIZE];
327 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
328 return VPCERR_UNSUPPORTED;
330 /* Check up the F/W and H/W support NPIV */
331 if (!ha->flags.npiv_supported)
332 return VPCERR_UNSUPPORTED;
334 /* Check up whether npiv supported switch presented */
335 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
336 return VPCERR_NO_FABRIC_SUPP;
338 /* Check up unique WWPN */
339 u64_to_wwn(fc_vport->port_name, port_name);
340 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
341 return VPCERR_BAD_WWN;
342 vha = qla24xx_find_vhost_by_name(ha, port_name);
344 return VPCERR_BAD_WWN;
346 /* Check up max-npiv-supports */
347 if (ha->num_vhosts > ha->max_npiv_vports) {
348 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
349 "max_npv_vports %ud.\n", base_vha->host_no,
350 ha->num_vhosts, ha->max_npiv_vports));
351 return VPCERR_UNSUPPORTED;
357 qla24xx_create_vhost(struct fc_vport *fc_vport)
359 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
360 struct qla_hw_data *ha = base_vha->hw;
361 scsi_qla_host_t *vha;
362 struct scsi_host_template *sht = &qla2xxx_driver_template;
363 struct Scsi_Host *host;
365 vha = qla2x00_create_host(sht, ha);
367 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
372 fc_vport->dd_data = vha;
374 u64_to_wwn(fc_vport->node_name, vha->node_name);
375 u64_to_wwn(fc_vport->port_name, vha->port_name);
377 vha->fc_vport = fc_vport;
378 vha->device_flags = 0;
379 vha->vp_idx = qla24xx_allocate_vp_id(vha);
380 if (vha->vp_idx > ha->max_npiv_vports) {
381 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
383 goto create_vhost_failed;
385 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
388 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
389 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
392 * To fix the issue of processing a parent's RSCN for the vport before
393 * its SCR is complete.
395 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
396 atomic_set(&vha->loop_state, LOOP_DOWN);
397 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
399 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
401 memset(vha->req_ques, 0, sizeof(vha->req_ques));
402 vha->req_ques[0] = ha->req_q_map[0]->id;
403 host->can_queue = ha->req_q_map[0]->length + 128;
405 host->cmd_per_lun = 3;
406 host->max_cmd_len = MAX_CMDSZ;
407 host->max_channel = MAX_BUSES - 1;
408 host->max_lun = MAX_LUNS;
409 host->unique_id = host->host_no;
410 host->max_id = MAX_TARGETS_2200;
411 host->transportt = qla2xxx_transport_vport_template;
413 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
416 vha->flags.init_done = 1;
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, uint8_t que_no)
521 struct req_que *req = NULL;
522 struct rsp_que *rsp = NULL;
523 struct qla_hw_data *ha = vha->hw;
526 /* Delete request queue */
527 req = ha->req_q_map[que_no];
530 ret = qla25xx_delete_req_que(vha, req);
531 if (ret != QLA_SUCCESS) {
532 qla_printk(KERN_WARNING, ha,
533 "Couldn't delete req que %d\n", req->id);
536 /* Delete associated response queue */
538 ret = qla25xx_delete_rsp_que(vha, rsp);
539 if (ret != QLA_SUCCESS) {
540 qla_printk(KERN_WARNING, ha,
541 "Couldn't delete rsp que %d\n",
547 } else { /* delete all queues of this host */
548 for (cnt = 0; cnt < QLA_MAX_HOST_QUES; cnt++) {
549 /* Delete request queues */
550 req = ha->req_q_map[vha->req_ques[cnt]];
551 if (req && req->id) {
553 ret = qla25xx_delete_req_que(vha, req);
554 if (ret != QLA_SUCCESS) {
555 qla_printk(KERN_WARNING, ha,
556 "Couldn't delete req que %d\n",
560 vha->req_ques[cnt] = ha->req_q_map[0]->id;
561 /* Delete associated response queue */
562 if (rsp && rsp->id) {
563 ret = qla25xx_delete_rsp_que(vha, rsp);
564 if (ret != QLA_SUCCESS) {
565 qla_printk(KERN_WARNING, ha,
566 "Couldn't delete rsp que %d\n",
574 qla_printk(KERN_INFO, ha, "Queues deleted for vport:%d\n",
580 qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
581 uint8_t vp_idx, uint16_t rid, uint8_t rsp_que, uint8_t qos)
584 struct req_que *req = NULL;
585 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
587 device_reg_t __iomem *reg;
589 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
591 qla_printk(KERN_WARNING, ha, "could not allocate memory"
592 "for request que\n");
596 req->length = REQUEST_ENTRY_CNT_24XX;
597 req->ring = dma_alloc_coherent(&ha->pdev->dev,
598 (req->length + 1) * sizeof(request_t),
599 &req->dma, GFP_KERNEL);
600 if (req->ring == NULL) {
601 qla_printk(KERN_WARNING, ha,
602 "Memory Allocation failed - request_ring\n");
606 mutex_lock(&ha->vport_lock);
607 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_queues);
608 if (que_id >= ha->max_queues) {
609 mutex_unlock(&ha->vport_lock);
610 qla_printk(KERN_INFO, ha, "No resources to create "
611 "additional request queue\n");
614 set_bit(que_id, ha->req_qid_map);
615 ha->req_q_map[que_id] = req;
617 req->vp_idx = vp_idx;
620 if (ha->rsp_q_map[rsp_que]) {
621 req->rsp = ha->rsp_q_map[rsp_que];
624 /* Use alternate PCI bus number */
627 /* Use alternate PCI devfn */
630 req->options = options;
631 req->ring_ptr = req->ring;
633 req->cnt = req->length;
635 reg = ISP_QUE_REG(ha, que_id);
636 req->req_q_in = ®->isp25mq.req_q_in;
637 req->req_q_out = ®->isp25mq.req_q_out;
638 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
639 mutex_unlock(&ha->vport_lock);
641 ret = qla25xx_init_req_que(base_vha, req);
642 if (ret != QLA_SUCCESS) {
643 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
644 mutex_lock(&ha->vport_lock);
645 clear_bit(que_id, ha->req_qid_map);
646 mutex_unlock(&ha->vport_lock);
653 qla25xx_free_req_que(base_vha, req);
657 /* create response queue */
659 qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
660 uint8_t vp_idx, uint16_t rid)
663 struct rsp_que *rsp = NULL;
664 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
666 device_reg_t __iomem *reg;
668 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
670 qla_printk(KERN_WARNING, ha, "could not allocate memory for"
675 rsp->length = RESPONSE_ENTRY_CNT_2300;
676 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
677 (rsp->length + 1) * sizeof(response_t),
678 &rsp->dma, GFP_KERNEL);
679 if (rsp->ring == NULL) {
680 qla_printk(KERN_WARNING, ha,
681 "Memory Allocation failed - response_ring\n");
685 mutex_lock(&ha->vport_lock);
686 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_queues);
687 if (que_id >= ha->max_queues) {
688 mutex_unlock(&ha->vport_lock);
689 qla_printk(KERN_INFO, ha, "No resources to create "
690 "additional response queue\n");
693 set_bit(que_id, ha->rsp_qid_map);
695 if (ha->flags.msix_enabled)
696 rsp->msix = &ha->msix_entries[que_id + 1];
698 qla_printk(KERN_WARNING, ha, "msix not enabled\n");
700 ha->rsp_q_map[que_id] = rsp;
702 rsp->vp_idx = vp_idx;
704 /* Use alternate PCI bus number */
707 /* Use alternate PCI devfn */
710 rsp->options = options;
711 rsp->ring_ptr = rsp->ring;
714 reg = ISP_QUE_REG(ha, que_id);
715 rsp->rsp_q_in = ®->isp25mq.rsp_q_in;
716 rsp->rsp_q_out = ®->isp25mq.rsp_q_out;
717 mutex_unlock(&ha->vport_lock);
719 ret = qla25xx_request_irq(rsp);
723 ret = qla25xx_init_rsp_que(base_vha, rsp);
724 if (ret != QLA_SUCCESS) {
725 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
726 mutex_lock(&ha->vport_lock);
727 clear_bit(que_id, ha->rsp_qid_map);
728 mutex_unlock(&ha->vport_lock);
732 qla2x00_init_response_q_entries(rsp);
737 qla25xx_free_rsp_que(base_vha, rsp);
742 qla25xx_create_queues(struct scsi_qla_host *vha, uint8_t qos)
744 uint16_t options = 0;
746 struct qla_hw_data *ha = vha->hw;
749 ret = qla25xx_create_rsp_que(ha, options, vha->vp_idx, 0);
751 qla_printk(KERN_WARNING, ha, "Response Que create failed\n");
754 qla_printk(KERN_INFO, ha, "Response Que:%d created.\n", ret);
759 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, ret,
762 vha->req_ques[0] = ret;
763 qla_printk(KERN_INFO, ha, "Request Que:%d created.\n", ret);
765 qla_printk(KERN_WARNING, ha, "Request Que create failed\n");