blob: 785c61279e6e47c582329f08e6ea47ca9a159c44 [file] [log] [blame]
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001/*
Andrew Vasquez01e58d82008-04-03 13:13:13 -07002 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004 *
Andrew Vasquez01e58d82008-04-03 13:13:13 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006 */
7#include "qla_def.h"
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08008#include "qla_gbl.h"
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07009
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070010#include <linux/moduleparam.h>
11#include <linux/vmalloc.h>
12#include <linux/smp_lock.h>
13#include <linux/list.h>
14
15#include <scsi/scsi_tcq.h>
16#include <scsi/scsicam.h>
17#include <linux/delay.h>
18
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070019void
20qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
21{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080022 if (vha->vp_idx && vha->timer_active) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070023 del_timer_sync(&vha->timer);
24 vha->timer_active = 0;
25 }
26}
27
Adrian Bunka824ebb2008-01-17 09:02:15 -080028static uint32_t
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070029qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
30{
31 uint32_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080032 struct qla_hw_data *ha = vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070033
34 /* Find an empty slot and assign an vp_id */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070035 mutex_lock(&ha->vport_lock);
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080036 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));
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070040 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070041 return vp_id;
42 }
43
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080044 set_bit(vp_id, ha->vp_idx_map);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070045 ha->num_vhosts++;
Seokmann Ju711c1d92008-07-10 16:55:51 -070046 ha->cur_vport_count++;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070047 vha->vp_idx = vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080048 list_add_tail(&vha->list, &ha->vp_list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070049 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070050 return vp_id;
51}
52
53void
54qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
55{
56 uint16_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080057 struct qla_hw_data *ha = vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070058
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070059 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070060 vp_id = vha->vp_idx;
61 ha->num_vhosts--;
Seokmann Ju711c1d92008-07-10 16:55:51 -070062 ha->cur_vport_count--;
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080063 clear_bit(vp_id, ha->vp_idx_map);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080064 list_del(&vha->list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070065 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070066}
67
Adrian Bunka824ebb2008-01-17 09:02:15 -080068static scsi_qla_host_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080069qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070070{
71 scsi_qla_host_t *vha;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080072 struct scsi_qla_host *tvha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070073
74 /* Locate matching device in database. */
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080075 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070076 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
77 return vha;
78 }
79 return NULL;
80}
81
82/*
83 * qla2x00_mark_vp_devices_dead
84 * Updates fcport state when device goes offline.
85 *
86 * Input:
87 * ha = adapter block pointer.
88 * fcport = port structure pointer.
89 *
90 * Return:
91 * None.
92 *
93 * Context:
94 */
Andrew Vasquez26ff7762007-09-20 14:07:47 -070095static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070096qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
97{
98 fc_port_t *fcport;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070099
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800100 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700101 DEBUG15(printk("scsi(%ld): Marking port dead, "
102 "loop_id=0x%04x :%x\n",
103 vha->host_no, fcport->loop_id, fcport->vp_idx));
104
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800105 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700106 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700107 atomic_set(&fcport->state, FCS_UNCONFIGURED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700108 }
109}
110
111int
112qla24xx_disable_vp(scsi_qla_host_t *vha)
113{
114 int ret;
115
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);
119
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700120 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);
125 } else {
126 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
127 return -1;
128 }
129 return 0;
130}
131
132int
133qla24xx_enable_vp(scsi_qla_host_t *vha)
134{
135 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800136 struct qla_hw_data *ha = vha->hw;
137 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700138
139 /* Check if physical ha port is Up */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800140 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
141 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700142 vha->vp_err_state = VP_ERR_PORTDWN;
143 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
144 goto enable_failed;
145 }
146
147 /* Initialize the new vport unless it is a persistent port */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700148 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700149 ret = qla24xx_modify_vp_config(vha);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700150 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700151
152 if (ret != QLA_SUCCESS) {
153 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
154 goto enable_failed;
155 }
156
157 DEBUG15(qla_printk(KERN_INFO, ha,
158 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
159 return 0;
160
161enable_failed:
162 DEBUG15(qla_printk(KERN_INFO, ha,
163 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
164 return 1;
165}
166
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700167static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700168qla24xx_configure_vp(scsi_qla_host_t *vha)
169{
170 struct fc_vport *fc_vport;
171 int ret;
172
173 fc_vport = vha->fc_vport;
174
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) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800179 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
180 "receiving of RSCN requests: 0x%x\n", ret));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700181 return;
182 } else {
183 /* Corresponds to SCR enabled */
184 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
185 }
186
187 vha->flags.online = 1;
188 if (qla24xx_configure_vhba(vha))
189 return;
190
191 atomic_set(&vha->vp_state, VP_ACTIVE);
192 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
193}
194
195void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800196qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700197{
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800198 scsi_qla_host_t *vha, *tvha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800199 struct qla_hw_data *ha = rsp->hw;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800200 int i = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700201
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800202 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800203 if (vha->vp_idx) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700204 switch (mb[0]) {
205 case MBA_LIP_OCCURRED:
206 case MBA_LOOP_UP:
207 case MBA_LOOP_DOWN:
208 case MBA_LIP_RESET:
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"
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800214 " VP[%d], mb = 0x%x, vha=%p\n",
215 vha->host_no, __func__, i, *mb, vha));
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800216 qla2x00_async_event(vha, rsp, mb);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700217 break;
218 }
219 }
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800220 i++;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700221 }
222}
223
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800224int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700225qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
226{
227 /*
228 * Physical port will do most of the abort and recovery work. We can
229 * just treat it as a loop down
230 */
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);
234 } else {
235 if (!atomic_read(&vha->loop_down_timer))
236 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
237 }
238
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800239 /* 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);
242
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700243 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
244 vha->host_no, vha->vp_idx));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800245 return qla24xx_enable_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700246}
247
Adrian Bunka824ebb2008-01-17 09:02:15 -0800248static int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700249qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
250{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800251 struct qla_hw_data *ha = vha->hw;
252 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Seokmann Ju221726d2008-04-03 13:13:31 -0700253
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700254 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
255 /* VP acquired. complete port configuration */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800256 if (atomic_read(&base_vha->loop_state) == LOOP_READY) {
Seokmann Ju221726d2008-04-03 13:13:31 -0700257 qla24xx_configure_vp(vha);
258 } else {
259 set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800260 set_bit(VP_DPC_NEEDED, &base_vha->dpc_flags);
Seokmann Ju221726d2008-04-03 13:13:31 -0700261 }
262
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700263 return 0;
264 }
265
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800266 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
267 qla2x00_update_fcports(vha);
268 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
269 }
270
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) {
274
275 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
276 vha->host_no));
277 qla2x00_relogin(vha);
278
279 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
280 vha->host_no));
281 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700282
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);
286 }
287
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800288 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700289 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);
292 }
293 }
294
295 return 0;
296}
297
298void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800299qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700300{
301 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800302 struct qla_hw_data *ha = vha->hw;
303 scsi_qla_host_t *vp;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800304 struct scsi_qla_host *tvp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700305
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800306 if (vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700307 return;
308 if (list_empty(&ha->vp_list))
309 return;
310
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800311 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700312
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800313 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800314 if (vp->vp_idx)
315 ret = qla2x00_do_dpc_vp(vp);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700316 }
317}
318
319int
320qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
321{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800322 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
323 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700324 scsi_qla_host_t *vha;
325 uint8_t port_name[WWN_SIZE];
326
327 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
328 return VPCERR_UNSUPPORTED;
329
330 /* Check up the F/W and H/W support NPIV */
331 if (!ha->flags.npiv_supported)
332 return VPCERR_UNSUPPORTED;
333
334 /* Check up whether npiv supported switch presented */
335 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
336 return VPCERR_NO_FABRIC_SUPP;
337
338 /* Check up unique WWPN */
339 u64_to_wwn(fc_vport->port_name, port_name);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800340 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
Seokmann Ju50db6b12008-01-17 09:02:14 -0800341 return VPCERR_BAD_WWN;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700342 vha = qla24xx_find_vhost_by_name(ha, port_name);
343 if (vha)
344 return VPCERR_BAD_WWN;
345
346 /* Check up max-npiv-supports */
347 if (ha->num_vhosts > ha->max_npiv_vports) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800348 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800349 "max_npv_vports %ud.\n", base_vha->host_no,
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800350 ha->num_vhosts, ha->max_npiv_vports));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700351 return VPCERR_UNSUPPORTED;
352 }
353 return 0;
354}
355
356scsi_qla_host_t *
357qla24xx_create_vhost(struct fc_vport *fc_vport)
358{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800359 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
360 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700361 scsi_qla_host_t *vha;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800362 struct scsi_host_template *sht = &qla24xx_driver_template;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700363 struct Scsi_Host *host;
364
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800365 vha = qla2x00_create_host(sht, ha);
366 if (!vha) {
367 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700368 return(NULL);
369 }
370
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800371 host = vha->host;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700372 fc_vport->dd_data = vha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700373 /* New host info */
374 u64_to_wwn(fc_vport->node_name, vha->node_name);
375 u64_to_wwn(fc_vport->port_name, vha->port_name);
376
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700377 vha->fc_vport = fc_vport;
378 vha->device_flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700379 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",
382 vha->host_no));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800383 goto create_vhost_failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700384 }
385 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
386
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700387 vha->dpc_flags = 0L;
388 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
389 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
390
391 /*
392 * To fix the issue of processing a parent's RSCN for the vport before
393 * its SCR is complete.
394 */
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);
398
399 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
400
Anirban Chakrabortycf5a1632009-02-08 20:50:13 -0800401 memset(vha->req_ques, 0, sizeof(vha->req_ques));
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800402 vha->req_ques[0] = ha->req_q_map[0]->id;
403 host->can_queue = ha->req_q_map[0]->length + 128;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700404 host->this_id = 255;
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;
Seokmann Ju711c1d92008-07-10 16:55:51 -0700409 host->unique_id = host->host_no;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700410 host->max_id = MAX_TARGETS_2200;
411 host->transportt = qla2xxx_transport_vport_template;
412
413 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
414 vha->host_no, vha));
415
416 vha->flags.init_done = 1;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700417
418 return vha;
419
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800420create_vhost_failed:
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700421 return NULL;
422}
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800423
424static void
425qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
426{
427 struct qla_hw_data *ha = vha->hw;
428 uint16_t que_id = req->id;
429
430 dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
431 sizeof(request_t), req->ring, req->dma);
432 req->ring = NULL;
433 req->dma = 0;
434 if (que_id) {
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);
439 }
440 kfree(req);
441 req = NULL;
442}
443
444static void
445qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
446{
447 struct qla_hw_data *ha = vha->hw;
448 uint16_t que_id = rsp->id;
449
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;
454 }
455 dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
456 sizeof(response_t), rsp->ring, rsp->dma);
457 rsp->ring = NULL;
458 rsp->dma = 0;
459 if (que_id) {
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);
464 }
465 kfree(rsp);
466 rsp = NULL;
467}
468
469int
470qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
471{
472 int ret = -1;
473
474 if (req) {
475 req->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800476 ret = qla25xx_init_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800477 }
478 if (ret == QLA_SUCCESS)
479 qla25xx_free_req_que(vha, req);
480
481 return ret;
482}
483
484int
485qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
486{
487 int ret = -1;
488
489 if (rsp) {
490 rsp->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800491 ret = qla25xx_init_rsp_que(vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800492 }
493 if (ret == QLA_SUCCESS)
494 qla25xx_free_rsp_que(vha, rsp);
495
496 return ret;
497}
498
499int qla25xx_update_req_que(struct scsi_qla_host *vha, uint8_t que, uint8_t qos)
500{
501 int ret = 0;
502 struct qla_hw_data *ha = vha->hw;
503 struct req_que *req = ha->req_q_map[que];
504
505 req->options |= BIT_3;
506 req->qos = qos;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800507 ret = qla25xx_init_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800508 if (ret != QLA_SUCCESS)
509 DEBUG2_17(printk(KERN_WARNING "%s failed\n", __func__));
510 /* restore options bit */
511 req->options &= ~BIT_3;
512 return ret;
513}
514
515
516/* Delete all queues for a given vhost */
517int
518qla25xx_delete_queues(struct scsi_qla_host *vha, uint8_t que_no)
519{
520 int cnt, ret = 0;
521 struct req_que *req = NULL;
522 struct rsp_que *rsp = NULL;
523 struct qla_hw_data *ha = vha->hw;
524
525 if (que_no) {
526 /* Delete request queue */
527 req = ha->req_q_map[que_no];
528 if (req) {
529 rsp = req->rsp;
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);
534 return ret;
535 }
536 /* Delete associated response queue */
537 if (rsp) {
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",
542 rsp->id);
543 return ret;
544 }
545 }
546 }
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) {
552 rsp = req->rsp;
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",
557 vha->req_ques[cnt]);
558 return ret;
559 }
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",
567 rsp->id);
568 return ret;
569 }
570 }
571 }
572 }
573 }
574 qla_printk(KERN_INFO, ha, "Queues deleted for vport:%d\n",
575 vha->vp_idx);
576 return ret;
577}
578
579int
580qla25xx_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)
582{
583 int ret = 0;
584 struct req_que *req = NULL;
585 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
586 uint16_t que_id = 0;
587
588 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
589 if (req == NULL) {
590 qla_printk(KERN_WARNING, ha, "could not allocate memory"
591 "for request que\n");
592 goto que_failed;
593 }
594
595 req->length = REQUEST_ENTRY_CNT_24XX;
596 req->ring = dma_alloc_coherent(&ha->pdev->dev,
597 (req->length + 1) * sizeof(request_t),
598 &req->dma, GFP_KERNEL);
599 if (req->ring == NULL) {
600 qla_printk(KERN_WARNING, ha,
601 "Memory Allocation failed - request_ring\n");
602 goto que_failed;
603 }
604
605 mutex_lock(&ha->vport_lock);
606 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_queues);
607 if (que_id >= ha->max_queues) {
608 mutex_unlock(&ha->vport_lock);
609 qla_printk(KERN_INFO, ha, "No resources to create "
610 "additional request queue\n");
611 goto que_failed;
612 }
613 set_bit(que_id, ha->req_qid_map);
614 ha->req_q_map[que_id] = req;
615 req->rid = rid;
616 req->vp_idx = vp_idx;
617 req->qos = qos;
618
Andrew Vasquez444786d2009-01-05 11:18:10 -0800619 if (ha->rsp_q_map[rsp_que]) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800620 req->rsp = ha->rsp_q_map[rsp_que];
Andrew Vasquez444786d2009-01-05 11:18:10 -0800621 req->rsp->req = req;
622 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800623 /* Use alternate PCI bus number */
624 if (MSB(req->rid))
625 options |= BIT_4;
626 /* Use alternate PCI devfn */
627 if (LSB(req->rid))
628 options |= BIT_5;
629 req->options = options;
630 req->ring_ptr = req->ring;
631 req->ring_index = 0;
632 req->cnt = req->length;
633 req->id = que_id;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -0800634 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800635 mutex_unlock(&ha->vport_lock);
636
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800637 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800638 if (ret != QLA_SUCCESS) {
639 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
640 mutex_lock(&ha->vport_lock);
641 clear_bit(que_id, ha->req_qid_map);
642 mutex_unlock(&ha->vport_lock);
643 goto que_failed;
644 }
645
646 return req->id;
647
648que_failed:
649 qla25xx_free_req_que(base_vha, req);
650 return 0;
651}
652
653/* create response queue */
654int
655qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
656 uint8_t vp_idx, uint16_t rid)
657{
658 int ret = 0;
659 struct rsp_que *rsp = NULL;
660 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
661 uint16_t que_id = 0;;
662
663 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
664 if (rsp == NULL) {
665 qla_printk(KERN_WARNING, ha, "could not allocate memory for"
666 " response que\n");
667 goto que_failed;
668 }
669
670 rsp->length = RESPONSE_ENTRY_CNT_2300;
671 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
672 (rsp->length + 1) * sizeof(response_t),
673 &rsp->dma, GFP_KERNEL);
674 if (rsp->ring == NULL) {
675 qla_printk(KERN_WARNING, ha,
676 "Memory Allocation failed - response_ring\n");
677 goto que_failed;
678 }
679
680 mutex_lock(&ha->vport_lock);
681 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_queues);
682 if (que_id >= ha->max_queues) {
683 mutex_unlock(&ha->vport_lock);
684 qla_printk(KERN_INFO, ha, "No resources to create "
685 "additional response queue\n");
686 goto que_failed;
687 }
688 set_bit(que_id, ha->rsp_qid_map);
689
690 if (ha->flags.msix_enabled)
691 rsp->msix = &ha->msix_entries[que_id + 1];
692 else
693 qla_printk(KERN_WARNING, ha, "msix not enabled\n");
694
695 ha->rsp_q_map[que_id] = rsp;
696 rsp->rid = rid;
697 rsp->vp_idx = vp_idx;
698 rsp->hw = ha;
699 /* Use alternate PCI bus number */
700 if (MSB(rsp->rid))
701 options |= BIT_4;
702 /* Use alternate PCI devfn */
703 if (LSB(rsp->rid))
704 options |= BIT_5;
705 rsp->options = options;
706 rsp->ring_ptr = rsp->ring;
707 rsp->ring_index = 0;
708 rsp->id = que_id;
709 mutex_unlock(&ha->vport_lock);
710
711 ret = qla25xx_request_irq(rsp);
712 if (ret)
713 goto que_failed;
714
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800715 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800716 if (ret != QLA_SUCCESS) {
717 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
718 mutex_lock(&ha->vport_lock);
719 clear_bit(que_id, ha->rsp_qid_map);
720 mutex_unlock(&ha->vport_lock);
721 goto que_failed;
722 }
723
724 qla2x00_init_response_q_entries(rsp);
725
726 return rsp->id;
727
728que_failed:
729 qla25xx_free_rsp_que(base_vha, rsp);
730 return 0;
731}
732
733int
734qla25xx_create_queues(struct scsi_qla_host *vha, uint8_t qos)
735{
736 uint16_t options = 0;
737 uint8_t ret = 0;
738 struct qla_hw_data *ha = vha->hw;
739
740 options |= BIT_1;
741 ret = qla25xx_create_rsp_que(ha, options, vha->vp_idx, 0);
742 if (!ret) {
743 qla_printk(KERN_WARNING, ha, "Response Que create failed\n");
744 return ret;
745 } else
746 qla_printk(KERN_INFO, ha, "Response Que:%d created.\n", ret);
747
748 options = 0;
749 if (qos & BIT_7)
750 options |= BIT_8;
751 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, ret,
752 qos & ~BIT_7);
753 if (ret) {
754 vha->req_ques[0] = ret;
755 qla_printk(KERN_INFO, ha, "Request Que:%d created.\n", ret);
756 } else
757 qla_printk(KERN_WARNING, ha, "Request Que create failed\n");
758
759 return ret;
760}