blob: b901aa267e7d35e5fb1530056ceedab8d2c75b25 [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>
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070012#include <linux/list.h>
13
14#include <scsi/scsi_tcq.h>
15#include <scsi/scsicam.h>
16#include <linux/delay.h>
17
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070018void
19qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
20{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080021 if (vha->vp_idx && vha->timer_active) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070022 del_timer_sync(&vha->timer);
23 vha->timer_active = 0;
24 }
25}
26
Adrian Bunka824ebb2008-01-17 09:02:15 -080027static uint32_t
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070028qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
29{
30 uint32_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080031 struct qla_hw_data *ha = vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070032
33 /* Find an empty slot and assign an vp_id */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070034 mutex_lock(&ha->vport_lock);
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080035 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
36 if (vp_id > ha->max_npiv_vports) {
37 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
38 vp_id, ha->max_npiv_vports));
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070039 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070040 return vp_id;
41 }
42
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080043 set_bit(vp_id, ha->vp_idx_map);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070044 ha->num_vhosts++;
45 vha->vp_idx = vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080046 list_add_tail(&vha->list, &ha->vp_list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070047 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070048 return vp_id;
49}
50
51void
52qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
53{
54 uint16_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080055 struct qla_hw_data *ha = vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070056
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070057 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070058 vp_id = vha->vp_idx;
59 ha->num_vhosts--;
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080060 clear_bit(vp_id, ha->vp_idx_map);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080061 list_del(&vha->list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070062 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070063}
64
Adrian Bunka824ebb2008-01-17 09:02:15 -080065static scsi_qla_host_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080066qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070067{
68 scsi_qla_host_t *vha;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080069 struct scsi_qla_host *tvha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070070
71 /* Locate matching device in database. */
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080072 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070073 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
74 return vha;
75 }
76 return NULL;
77}
78
79/*
80 * qla2x00_mark_vp_devices_dead
81 * Updates fcport state when device goes offline.
82 *
83 * Input:
84 * ha = adapter block pointer.
85 * fcport = port structure pointer.
86 *
87 * Return:
88 * None.
89 *
90 * Context:
91 */
Andrew Vasquez26ff7762007-09-20 14:07:47 -070092static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070093qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
94{
95 fc_port_t *fcport;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070096
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080097 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070098 DEBUG15(printk("scsi(%ld): Marking port dead, "
99 "loop_id=0x%04x :%x\n",
100 vha->host_no, fcport->loop_id, fcport->vp_idx));
101
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800102 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700103 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700104 atomic_set(&fcport->state, FCS_UNCONFIGURED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700105 }
106}
107
108int
109qla24xx_disable_vp(scsi_qla_host_t *vha)
110{
111 int ret;
112
113 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
114 atomic_set(&vha->loop_state, LOOP_DOWN);
115 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
116
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700117 qla2x00_mark_vp_devices_dead(vha);
118 atomic_set(&vha->vp_state, VP_FAILED);
119 vha->flags.management_server_logged_in = 0;
120 if (ret == QLA_SUCCESS) {
121 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
122 } else {
123 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
124 return -1;
125 }
126 return 0;
127}
128
129int
130qla24xx_enable_vp(scsi_qla_host_t *vha)
131{
132 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800133 struct qla_hw_data *ha = vha->hw;
134 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700135
136 /* Check if physical ha port is Up */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800137 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
138 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700139 vha->vp_err_state = VP_ERR_PORTDWN;
140 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
141 goto enable_failed;
142 }
143
144 /* Initialize the new vport unless it is a persistent port */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700145 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700146 ret = qla24xx_modify_vp_config(vha);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700147 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700148
149 if (ret != QLA_SUCCESS) {
150 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
151 goto enable_failed;
152 }
153
154 DEBUG15(qla_printk(KERN_INFO, ha,
155 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
156 return 0;
157
158enable_failed:
159 DEBUG15(qla_printk(KERN_INFO, ha,
160 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
161 return 1;
162}
163
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700164static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700165qla24xx_configure_vp(scsi_qla_host_t *vha)
166{
167 struct fc_vport *fc_vport;
168 int ret;
169
170 fc_vport = vha->fc_vport;
171
172 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
173 vha->host_no, __func__));
174 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
175 if (ret != QLA_SUCCESS) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800176 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
177 "receiving of RSCN requests: 0x%x\n", ret));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700178 return;
179 } else {
180 /* Corresponds to SCR enabled */
181 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
182 }
183
184 vha->flags.online = 1;
185 if (qla24xx_configure_vhba(vha))
186 return;
187
188 atomic_set(&vha->vp_state, VP_ACTIVE);
189 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
190}
191
192void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800193qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700194{
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800195 scsi_qla_host_t *vha, *tvha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800196 struct qla_hw_data *ha = rsp->hw;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800197 int i = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700198
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800199 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800200 if (vha->vp_idx) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700201 switch (mb[0]) {
202 case MBA_LIP_OCCURRED:
203 case MBA_LOOP_UP:
204 case MBA_LOOP_DOWN:
205 case MBA_LIP_RESET:
206 case MBA_POINT_TO_POINT:
207 case MBA_CHG_IN_CONNECTION:
208 case MBA_PORT_UPDATE:
209 case MBA_RSCN_UPDATE:
210 DEBUG15(printk("scsi(%ld)%s: Async_event for"
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800211 " VP[%d], mb = 0x%x, vha=%p\n",
212 vha->host_no, __func__, i, *mb, vha));
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800213 qla2x00_async_event(vha, rsp, mb);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700214 break;
215 }
216 }
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800217 i++;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700218 }
219}
220
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800221int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700222qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
223{
224 /*
225 * Physical port will do most of the abort and recovery work. We can
226 * just treat it as a loop down
227 */
228 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
229 atomic_set(&vha->loop_state, LOOP_DOWN);
230 qla2x00_mark_all_devices_lost(vha, 0);
231 } else {
232 if (!atomic_read(&vha->loop_down_timer))
233 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
234 }
235
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700236 /*
237 * To exclusively reset vport, we need to log it out first. Note: this
238 * control_vp can fail if ISP reset is already issued, this is
239 * expected, as the vp would be already logged out due to ISP reset.
240 */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800241 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
242 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
243
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700244 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
245 vha->host_no, vha->vp_idx));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800246 return qla24xx_enable_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700247}
248
Adrian Bunka824ebb2008-01-17 09:02:15 -0800249static int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700250qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
251{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700252 qla2x00_do_work(vha);
253
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 */
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700256 qla24xx_configure_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700257 return 0;
258 }
259
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800260 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
261 qla2x00_update_fcports(vha);
262 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
263 }
264
265 if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
266 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
267 atomic_read(&vha->loop_state) != LOOP_DOWN) {
268
269 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
270 vha->host_no));
271 qla2x00_relogin(vha);
272
273 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
274 vha->host_no));
275 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700276
277 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
278 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
279 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
280 }
281
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800282 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700283 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
284 qla2x00_loop_resync(vha);
285 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
286 }
287 }
288
289 return 0;
290}
291
292void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800293qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700294{
295 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800296 struct qla_hw_data *ha = vha->hw;
297 scsi_qla_host_t *vp;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800298 struct scsi_qla_host *tvp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700299
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800300 if (vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700301 return;
302 if (list_empty(&ha->vp_list))
303 return;
304
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800305 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700306
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700307 if (!(ha->current_topology & ISP_CFG_F))
308 return;
309
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800310 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800311 if (vp->vp_idx)
312 ret = qla2x00_do_dpc_vp(vp);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700313 }
314}
315
316int
317qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
318{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800319 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
320 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700321 scsi_qla_host_t *vha;
322 uint8_t port_name[WWN_SIZE];
323
324 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
325 return VPCERR_UNSUPPORTED;
326
327 /* Check up the F/W and H/W support NPIV */
328 if (!ha->flags.npiv_supported)
329 return VPCERR_UNSUPPORTED;
330
331 /* Check up whether npiv supported switch presented */
332 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
333 return VPCERR_NO_FABRIC_SUPP;
334
335 /* Check up unique WWPN */
336 u64_to_wwn(fc_vport->port_name, port_name);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800337 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
Seokmann Ju50db6b12008-01-17 09:02:14 -0800338 return VPCERR_BAD_WWN;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700339 vha = qla24xx_find_vhost_by_name(ha, port_name);
340 if (vha)
341 return VPCERR_BAD_WWN;
342
343 /* Check up max-npiv-supports */
344 if (ha->num_vhosts > ha->max_npiv_vports) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800345 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800346 "max_npv_vports %ud.\n", base_vha->host_no,
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800347 ha->num_vhosts, ha->max_npiv_vports));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700348 return VPCERR_UNSUPPORTED;
349 }
350 return 0;
351}
352
353scsi_qla_host_t *
354qla24xx_create_vhost(struct fc_vport *fc_vport)
355{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800356 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
357 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700358 scsi_qla_host_t *vha;
Giridhar Malavalia5326f82009-03-24 09:07:56 -0700359 struct scsi_host_template *sht = &qla2xxx_driver_template;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700360 struct Scsi_Host *host;
361
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800362 vha = qla2x00_create_host(sht, ha);
363 if (!vha) {
364 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700365 return(NULL);
366 }
367
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800368 host = vha->host;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700369 fc_vport->dd_data = vha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700370 /* New host info */
371 u64_to_wwn(fc_vport->node_name, vha->node_name);
372 u64_to_wwn(fc_vport->port_name, vha->port_name);
373
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700374 vha->fc_vport = fc_vport;
375 vha->device_flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700376 vha->vp_idx = qla24xx_allocate_vp_id(vha);
377 if (vha->vp_idx > ha->max_npiv_vports) {
378 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
379 vha->host_no));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800380 goto create_vhost_failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700381 }
382 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
383
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700384 vha->dpc_flags = 0L;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700385
386 /*
387 * To fix the issue of processing a parent's RSCN for the vport before
388 * its SCR is complete.
389 */
390 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
391 atomic_set(&vha->loop_state, LOOP_DOWN);
392 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
393
394 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
395
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700396 vha->req = base_vha->req;
397 host->can_queue = base_vha->req->length + 128;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700398 host->this_id = 255;
399 host->cmd_per_lun = 3;
400 host->max_cmd_len = MAX_CMDSZ;
401 host->max_channel = MAX_BUSES - 1;
402 host->max_lun = MAX_LUNS;
Seokmann Ju711c1d92008-07-10 16:55:51 -0700403 host->unique_id = host->host_no;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700404 host->max_id = MAX_TARGETS_2200;
405 host->transportt = qla2xxx_transport_vport_template;
406
407 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
408 vha->host_no, vha));
409
410 vha->flags.init_done = 1;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700411
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700412 mutex_lock(&ha->vport_lock);
413 set_bit(vha->vp_idx, ha->vp_idx_map);
414 ha->cur_vport_count++;
415 mutex_unlock(&ha->vport_lock);
416
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700417 return vha;
418
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800419create_vhost_failed:
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700420 return NULL;
421}
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800422
423static void
424qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
425{
426 struct qla_hw_data *ha = vha->hw;
427 uint16_t que_id = req->id;
428
429 dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
430 sizeof(request_t), req->ring, req->dma);
431 req->ring = NULL;
432 req->dma = 0;
433 if (que_id) {
434 ha->req_q_map[que_id] = NULL;
435 mutex_lock(&ha->vport_lock);
436 clear_bit(que_id, ha->req_qid_map);
437 mutex_unlock(&ha->vport_lock);
438 }
439 kfree(req);
440 req = NULL;
441}
442
443static void
444qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
445{
446 struct qla_hw_data *ha = vha->hw;
447 uint16_t que_id = rsp->id;
448
449 if (rsp->msix && rsp->msix->have_irq) {
450 free_irq(rsp->msix->vector, rsp);
451 rsp->msix->have_irq = 0;
452 rsp->msix->rsp = NULL;
453 }
454 dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
455 sizeof(response_t), rsp->ring, rsp->dma);
456 rsp->ring = NULL;
457 rsp->dma = 0;
458 if (que_id) {
459 ha->rsp_q_map[que_id] = NULL;
460 mutex_lock(&ha->vport_lock);
461 clear_bit(que_id, ha->rsp_qid_map);
462 mutex_unlock(&ha->vport_lock);
463 }
464 kfree(rsp);
465 rsp = NULL;
466}
467
468int
469qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
470{
471 int ret = -1;
472
473 if (req) {
474 req->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800475 ret = qla25xx_init_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800476 }
477 if (ret == QLA_SUCCESS)
478 qla25xx_free_req_que(vha, req);
479
480 return ret;
481}
482
483int
484qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
485{
486 int ret = -1;
487
488 if (rsp) {
489 rsp->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800490 ret = qla25xx_init_rsp_que(vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800491 }
492 if (ret == QLA_SUCCESS)
493 qla25xx_free_rsp_que(vha, rsp);
494
495 return ret;
496}
497
498int qla25xx_update_req_que(struct scsi_qla_host *vha, uint8_t que, uint8_t qos)
499{
500 int ret = 0;
501 struct qla_hw_data *ha = vha->hw;
502 struct req_que *req = ha->req_q_map[que];
503
504 req->options |= BIT_3;
505 req->qos = qos;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800506 ret = qla25xx_init_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800507 if (ret != QLA_SUCCESS)
508 DEBUG2_17(printk(KERN_WARNING "%s failed\n", __func__));
509 /* restore options bit */
510 req->options &= ~BIT_3;
511 return ret;
512}
513
514
515/* Delete all queues for a given vhost */
516int
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700517qla25xx_delete_queues(struct scsi_qla_host *vha)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800518{
519 int cnt, ret = 0;
520 struct req_que *req = NULL;
521 struct rsp_que *rsp = NULL;
522 struct qla_hw_data *ha = vha->hw;
523
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700524 /* Delete request queues */
525 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
526 req = ha->req_q_map[cnt];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800527 if (req) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800528 ret = qla25xx_delete_req_que(vha, req);
529 if (ret != QLA_SUCCESS) {
530 qla_printk(KERN_WARNING, ha,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700531 "Couldn't delete req que %d\n",
532 req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800533 return ret;
534 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800535 }
536 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700537
538 /* Delete response queues */
539 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
540 rsp = ha->rsp_q_map[cnt];
541 if (rsp) {
542 ret = qla25xx_delete_rsp_que(vha, rsp);
543 if (ret != QLA_SUCCESS) {
544 qla_printk(KERN_WARNING, ha,
545 "Couldn't delete rsp que %d\n",
546 rsp->id);
547 return ret;
548 }
549 }
550 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800551 return ret;
552}
553
554int
555qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700556 uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800557{
558 int ret = 0;
559 struct req_que *req = NULL;
560 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
561 uint16_t que_id = 0;
Andrew Vasquez08029992009-03-24 09:07:55 -0700562 device_reg_t __iomem *reg;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700563 uint32_t cnt;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800564
565 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
566 if (req == NULL) {
567 qla_printk(KERN_WARNING, ha, "could not allocate memory"
568 "for request que\n");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700569 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800570 }
571
572 req->length = REQUEST_ENTRY_CNT_24XX;
573 req->ring = dma_alloc_coherent(&ha->pdev->dev,
574 (req->length + 1) * sizeof(request_t),
575 &req->dma, GFP_KERNEL);
576 if (req->ring == NULL) {
577 qla_printk(KERN_WARNING, ha,
578 "Memory Allocation failed - request_ring\n");
579 goto que_failed;
580 }
581
582 mutex_lock(&ha->vport_lock);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700583 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
584 if (que_id >= ha->max_req_queues) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800585 mutex_unlock(&ha->vport_lock);
586 qla_printk(KERN_INFO, ha, "No resources to create "
587 "additional request queue\n");
588 goto que_failed;
589 }
590 set_bit(que_id, ha->req_qid_map);
591 ha->req_q_map[que_id] = req;
592 req->rid = rid;
593 req->vp_idx = vp_idx;
594 req->qos = qos;
595
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700596 if (rsp_que < 0)
597 req->rsp = NULL;
598 else
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800599 req->rsp = ha->rsp_q_map[rsp_que];
600 /* Use alternate PCI bus number */
601 if (MSB(req->rid))
602 options |= BIT_4;
603 /* Use alternate PCI devfn */
604 if (LSB(req->rid))
605 options |= BIT_5;
606 req->options = options;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700607
608 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
609 req->outstanding_cmds[cnt] = NULL;
610 req->current_outstanding_cmd = 1;
611
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800612 req->ring_ptr = req->ring;
613 req->ring_index = 0;
614 req->cnt = req->length;
615 req->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700616 reg = ISP_QUE_REG(ha, que_id);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -0800617 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800618 mutex_unlock(&ha->vport_lock);
619
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800620 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800621 if (ret != QLA_SUCCESS) {
622 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
623 mutex_lock(&ha->vport_lock);
624 clear_bit(que_id, ha->req_qid_map);
625 mutex_unlock(&ha->vport_lock);
626 goto que_failed;
627 }
628
629 return req->id;
630
631que_failed:
632 qla25xx_free_req_que(base_vha, req);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700633failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800634 return 0;
635}
636
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700637static void qla_do_work(struct work_struct *work)
638{
639 struct rsp_que *rsp = container_of(work, struct rsp_que, q_work);
640 struct scsi_qla_host *vha;
641
Anirban Chakraborty5c66f5d2009-12-15 21:29:45 -0800642 spin_lock_irq(&rsp->hw->hardware_lock);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700643 vha = qla25xx_get_host(rsp);
644 qla24xx_process_response_queue(vha, rsp);
Anirban Chakraborty5c66f5d2009-12-15 21:29:45 -0800645 spin_unlock_irq(&rsp->hw->hardware_lock);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700646}
647
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800648/* create response queue */
649int
650qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700651 uint8_t vp_idx, uint16_t rid, int req)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800652{
653 int ret = 0;
654 struct rsp_que *rsp = NULL;
655 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Andrew Vasquez08029992009-03-24 09:07:55 -0700656 uint16_t que_id = 0;
657 device_reg_t __iomem *reg;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800658
659 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
660 if (rsp == NULL) {
661 qla_printk(KERN_WARNING, ha, "could not allocate memory for"
662 " response que\n");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700663 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800664 }
665
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700666 rsp->length = RESPONSE_ENTRY_CNT_MQ;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800667 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
668 (rsp->length + 1) * sizeof(response_t),
669 &rsp->dma, GFP_KERNEL);
670 if (rsp->ring == NULL) {
671 qla_printk(KERN_WARNING, ha,
672 "Memory Allocation failed - response_ring\n");
673 goto que_failed;
674 }
675
676 mutex_lock(&ha->vport_lock);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700677 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
678 if (que_id >= ha->max_rsp_queues) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800679 mutex_unlock(&ha->vport_lock);
680 qla_printk(KERN_INFO, ha, "No resources to create "
681 "additional response queue\n");
682 goto que_failed;
683 }
684 set_bit(que_id, ha->rsp_qid_map);
685
686 if (ha->flags.msix_enabled)
687 rsp->msix = &ha->msix_entries[que_id + 1];
688 else
689 qla_printk(KERN_WARNING, ha, "msix not enabled\n");
690
691 ha->rsp_q_map[que_id] = rsp;
692 rsp->rid = rid;
693 rsp->vp_idx = vp_idx;
694 rsp->hw = ha;
695 /* Use alternate PCI bus number */
696 if (MSB(rsp->rid))
697 options |= BIT_4;
698 /* Use alternate PCI devfn */
699 if (LSB(rsp->rid))
700 options |= BIT_5;
Anirban Chakraborty31557542009-12-02 10:36:55 -0800701 /* Enable MSIX handshake mode on for uncapable adapters */
702 if (!IS_MSIX_NACK_CAPABLE(ha))
703 options |= BIT_6;
704
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800705 rsp->options = options;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800706 rsp->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700707 reg = ISP_QUE_REG(ha, que_id);
708 rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
709 rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800710 mutex_unlock(&ha->vport_lock);
711
712 ret = qla25xx_request_irq(rsp);
713 if (ret)
714 goto que_failed;
715
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800716 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800717 if (ret != QLA_SUCCESS) {
718 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
719 mutex_lock(&ha->vport_lock);
720 clear_bit(que_id, ha->rsp_qid_map);
721 mutex_unlock(&ha->vport_lock);
722 goto que_failed;
723 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700724 if (req >= 0)
725 rsp->req = ha->req_q_map[req];
726 else
727 rsp->req = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800728
729 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700730 if (rsp->hw->wq)
731 INIT_WORK(&rsp->q_work, qla_do_work);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800732 return rsp->id;
733
734que_failed:
735 qla25xx_free_rsp_que(base_vha, rsp);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700736failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800737 return 0;
738}
739
740int
741qla25xx_create_queues(struct scsi_qla_host *vha, uint8_t qos)
742{
743 uint16_t options = 0;
744 uint8_t ret = 0;
745 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700746 struct rsp_que *rsp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800747
748 options |= BIT_1;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700749 ret = qla25xx_create_rsp_que(ha, options, vha->vp_idx, 0, -1);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800750 if (!ret) {
751 qla_printk(KERN_WARNING, ha, "Response Que create failed\n");
752 return ret;
753 } else
754 qla_printk(KERN_INFO, ha, "Response Que:%d created.\n", ret);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700755 rsp = ha->rsp_q_map[ret];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800756
757 options = 0;
758 if (qos & BIT_7)
759 options |= BIT_8;
760 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, ret,
761 qos & ~BIT_7);
762 if (ret) {
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700763 vha->req = ha->req_q_map[ret];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800764 qla_printk(KERN_INFO, ha, "Request Que:%d created.\n", ret);
765 } else
766 qla_printk(KERN_WARNING, ha, "Request Que create failed\n");
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700767 rsp->req = ha->req_q_map[ret];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800768
769 return ret;
770}