blob: 642cd79bd030ded50be75fad6430bf1063a9b968 [file] [log] [blame]
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001/*
Andrew Vasquez01e58d82008-04-03 13:13:13 -07002 * QLogic Fibre Channel HBA Driver
Giridhar Malavalide7c5d02010-07-23 15:28:36 +05003 * Copyright (c) 2003-2010 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070013#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++;
46 vha->vp_idx = vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080047 list_add_tail(&vha->list, &ha->vp_list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070048 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070049 return vp_id;
50}
51
52void
53qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
54{
55 uint16_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080056 struct qla_hw_data *ha = vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070057
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070058 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070059 vp_id = vha->vp_idx;
60 ha->num_vhosts--;
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080061 clear_bit(vp_id, ha->vp_idx_map);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080062 list_del(&vha->list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070063 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070064}
65
Adrian Bunka824ebb2008-01-17 09:02:15 -080066static scsi_qla_host_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080067qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070068{
69 scsi_qla_host_t *vha;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080070 struct scsi_qla_host *tvha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070071
72 /* Locate matching device in database. */
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080073 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070074 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
75 return vha;
76 }
77 return NULL;
78}
79
80/*
81 * qla2x00_mark_vp_devices_dead
82 * Updates fcport state when device goes offline.
83 *
84 * Input:
85 * ha = adapter block pointer.
86 * fcport = port structure pointer.
87 *
88 * Return:
89 * None.
90 *
91 * Context:
92 */
Andrew Vasquez26ff7762007-09-20 14:07:47 -070093static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070094qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
95{
96 fc_port_t *fcport;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070097
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080098 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070099 DEBUG15(printk("scsi(%ld): Marking port dead, "
100 "loop_id=0x%04x :%x\n",
101 vha->host_no, fcport->loop_id, fcport->vp_idx));
102
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800103 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700104 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700105 atomic_set(&fcport->state, FCS_UNCONFIGURED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700106 }
107}
108
109int
110qla24xx_disable_vp(scsi_qla_host_t *vha)
111{
112 int ret;
113
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);
117
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700118 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);
123 } else {
124 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
125 return -1;
126 }
127 return 0;
128}
129
130int
131qla24xx_enable_vp(scsi_qla_host_t *vha)
132{
133 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800134 struct qla_hw_data *ha = vha->hw;
135 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700136
137 /* Check if physical ha port is Up */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800138 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
Lalit Chandivade3f3b6f92010-05-28 15:08:24 -0700139 atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
140 !(ha->current_topology & ISP_CFG_F)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700141 vha->vp_err_state = VP_ERR_PORTDWN;
142 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
143 goto enable_failed;
144 }
145
146 /* Initialize the new vport unless it is a persistent port */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700147 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700148 ret = qla24xx_modify_vp_config(vha);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700149 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700150
151 if (ret != QLA_SUCCESS) {
152 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
153 goto enable_failed;
154 }
155
156 DEBUG15(qla_printk(KERN_INFO, ha,
157 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
158 return 0;
159
160enable_failed:
161 DEBUG15(qla_printk(KERN_INFO, ha,
162 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
163 return 1;
164}
165
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700166static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700167qla24xx_configure_vp(scsi_qla_host_t *vha)
168{
169 struct fc_vport *fc_vport;
170 int ret;
171
172 fc_vport = vha->fc_vport;
173
174 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
175 vha->host_no, __func__));
176 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
177 if (ret != QLA_SUCCESS) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800178 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
179 "receiving of RSCN requests: 0x%x\n", ret));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700180 return;
181 } else {
182 /* Corresponds to SCR enabled */
183 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
184 }
185
186 vha->flags.online = 1;
187 if (qla24xx_configure_vhba(vha))
188 return;
189
190 atomic_set(&vha->vp_state, VP_ACTIVE);
191 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
192}
193
194void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800195qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700196{
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800197 scsi_qla_host_t *vha, *tvha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800198 struct qla_hw_data *ha = rsp->hw;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800199 int i = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700200
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800201 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800202 if (vha->vp_idx) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700203 switch (mb[0]) {
204 case MBA_LIP_OCCURRED:
205 case MBA_LOOP_UP:
206 case MBA_LOOP_DOWN:
207 case MBA_LIP_RESET:
208 case MBA_POINT_TO_POINT:
209 case MBA_CHG_IN_CONNECTION:
210 case MBA_PORT_UPDATE:
211 case MBA_RSCN_UPDATE:
212 DEBUG15(printk("scsi(%ld)%s: Async_event for"
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800213 " VP[%d], mb = 0x%x, vha=%p\n",
214 vha->host_no, __func__, i, *mb, vha));
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800215 qla2x00_async_event(vha, rsp, mb);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700216 break;
217 }
218 }
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800219 i++;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700220 }
221}
222
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800223int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700224qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
225{
226 /*
227 * Physical port will do most of the abort and recovery work. We can
228 * just treat it as a loop down
229 */
230 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
231 atomic_set(&vha->loop_state, LOOP_DOWN);
232 qla2x00_mark_all_devices_lost(vha, 0);
233 } else {
234 if (!atomic_read(&vha->loop_down_timer))
235 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
236 }
237
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700238 /*
239 * To exclusively reset vport, we need to log it out first. Note: this
240 * control_vp can fail if ISP reset is already issued, this is
241 * expected, as the vp would be already logged out due to ISP reset.
242 */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800243 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
244 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
245
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700246 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
247 vha->host_no, vha->vp_idx));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800248 return qla24xx_enable_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700249}
250
Adrian Bunka824ebb2008-01-17 09:02:15 -0800251static int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700252qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
253{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700254 qla2x00_do_work(vha);
255
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700256 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
257 /* VP acquired. complete port configuration */
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700258 qla24xx_configure_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700259 return 0;
260 }
261
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800262 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
263 qla2x00_update_fcports(vha);
264 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
265 }
266
267 if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
268 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
269 atomic_read(&vha->loop_state) != LOOP_DOWN) {
270
271 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
272 vha->host_no));
273 qla2x00_relogin(vha);
274
275 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
276 vha->host_no));
277 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700278
279 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
280 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
281 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
282 }
283
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800284 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700285 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
286 qla2x00_loop_resync(vha);
287 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
288 }
289 }
290
291 return 0;
292}
293
294void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800295qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700296{
297 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800298 struct qla_hw_data *ha = vha->hw;
299 scsi_qla_host_t *vp;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800300 struct scsi_qla_host *tvp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700301
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800302 if (vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700303 return;
304 if (list_empty(&ha->vp_list))
305 return;
306
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800307 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700308
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700309 if (!(ha->current_topology & ISP_CFG_F))
310 return;
311
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800312 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800313 if (vp->vp_idx)
314 ret = qla2x00_do_dpc_vp(vp);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700315 }
316}
317
318int
319qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
320{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800321 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
322 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700323 scsi_qla_host_t *vha;
324 uint8_t port_name[WWN_SIZE];
325
326 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
327 return VPCERR_UNSUPPORTED;
328
329 /* Check up the F/W and H/W support NPIV */
330 if (!ha->flags.npiv_supported)
331 return VPCERR_UNSUPPORTED;
332
333 /* Check up whether npiv supported switch presented */
334 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
335 return VPCERR_NO_FABRIC_SUPP;
336
337 /* Check up unique WWPN */
338 u64_to_wwn(fc_vport->port_name, port_name);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800339 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
Seokmann Ju50db6b12008-01-17 09:02:14 -0800340 return VPCERR_BAD_WWN;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700341 vha = qla24xx_find_vhost_by_name(ha, port_name);
342 if (vha)
343 return VPCERR_BAD_WWN;
344
345 /* Check up max-npiv-supports */
346 if (ha->num_vhosts > ha->max_npiv_vports) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800347 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800348 "max_npv_vports %ud.\n", base_vha->host_no,
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800349 ha->num_vhosts, ha->max_npiv_vports));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700350 return VPCERR_UNSUPPORTED;
351 }
352 return 0;
353}
354
355scsi_qla_host_t *
356qla24xx_create_vhost(struct fc_vport *fc_vport)
357{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800358 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
359 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700360 scsi_qla_host_t *vha;
Giridhar Malavalia5326f82009-03-24 09:07:56 -0700361 struct scsi_host_template *sht = &qla2xxx_driver_template;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700362 struct Scsi_Host *host;
363
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800364 vha = qla2x00_create_host(sht, ha);
365 if (!vha) {
366 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700367 return(NULL);
368 }
369
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800370 host = vha->host;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700371 fc_vport->dd_data = vha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700372 /* New host info */
373 u64_to_wwn(fc_vport->node_name, vha->node_name);
374 u64_to_wwn(fc_vport->port_name, vha->port_name);
375
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700376 vha->fc_vport = fc_vport;
377 vha->device_flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700378 vha->vp_idx = qla24xx_allocate_vp_id(vha);
379 if (vha->vp_idx > ha->max_npiv_vports) {
380 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
381 vha->host_no));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800382 goto create_vhost_failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700383 }
384 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
385
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700386 vha->dpc_flags = 0L;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700387
388 /*
389 * To fix the issue of processing a parent's RSCN for the vport before
390 * its SCR is complete.
391 */
392 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
393 atomic_set(&vha->loop_state, LOOP_DOWN);
394 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
395
396 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
397
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700398 vha->req = base_vha->req;
399 host->can_queue = base_vha->req->length + 128;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700400 host->this_id = 255;
401 host->cmd_per_lun = 3;
402 host->max_cmd_len = MAX_CMDSZ;
403 host->max_channel = MAX_BUSES - 1;
404 host->max_lun = MAX_LUNS;
Seokmann Ju711c1d92008-07-10 16:55:51 -0700405 host->unique_id = host->host_no;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700406 host->max_id = MAX_TARGETS_2200;
407 host->transportt = qla2xxx_transport_vport_template;
408
409 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
410 vha->host_no, vha));
411
412 vha->flags.init_done = 1;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700413
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700414 mutex_lock(&ha->vport_lock);
415 set_bit(vha->vp_idx, ha->vp_idx_map);
416 ha->cur_vport_count++;
417 mutex_unlock(&ha->vport_lock);
418
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700419 return vha;
420
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800421create_vhost_failed:
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700422 return NULL;
423}
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800424
425static void
426qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
427{
428 struct qla_hw_data *ha = vha->hw;
429 uint16_t que_id = req->id;
430
431 dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
432 sizeof(request_t), req->ring, req->dma);
433 req->ring = NULL;
434 req->dma = 0;
435 if (que_id) {
436 ha->req_q_map[que_id] = NULL;
437 mutex_lock(&ha->vport_lock);
438 clear_bit(que_id, ha->req_qid_map);
439 mutex_unlock(&ha->vport_lock);
440 }
441 kfree(req);
442 req = NULL;
443}
444
445static void
446qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
447{
448 struct qla_hw_data *ha = vha->hw;
449 uint16_t que_id = rsp->id;
450
451 if (rsp->msix && rsp->msix->have_irq) {
452 free_irq(rsp->msix->vector, rsp);
453 rsp->msix->have_irq = 0;
454 rsp->msix->rsp = NULL;
455 }
456 dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
457 sizeof(response_t), rsp->ring, rsp->dma);
458 rsp->ring = NULL;
459 rsp->dma = 0;
460 if (que_id) {
461 ha->rsp_q_map[que_id] = NULL;
462 mutex_lock(&ha->vport_lock);
463 clear_bit(que_id, ha->rsp_qid_map);
464 mutex_unlock(&ha->vport_lock);
465 }
466 kfree(rsp);
467 rsp = NULL;
468}
469
470int
471qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
472{
473 int ret = -1;
474
475 if (req) {
476 req->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800477 ret = qla25xx_init_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800478 }
479 if (ret == QLA_SUCCESS)
480 qla25xx_free_req_que(vha, req);
481
482 return ret;
483}
484
485int
486qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
487{
488 int ret = -1;
489
490 if (rsp) {
491 rsp->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800492 ret = qla25xx_init_rsp_que(vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800493 }
494 if (ret == QLA_SUCCESS)
495 qla25xx_free_rsp_que(vha, rsp);
496
497 return ret;
498}
499
500int qla25xx_update_req_que(struct scsi_qla_host *vha, uint8_t que, uint8_t qos)
501{
502 int ret = 0;
503 struct qla_hw_data *ha = vha->hw;
504 struct req_que *req = ha->req_q_map[que];
505
506 req->options |= BIT_3;
507 req->qos = qos;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800508 ret = qla25xx_init_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800509 if (ret != QLA_SUCCESS)
510 DEBUG2_17(printk(KERN_WARNING "%s failed\n", __func__));
511 /* restore options bit */
512 req->options &= ~BIT_3;
513 return ret;
514}
515
516
517/* Delete all queues for a given vhost */
518int
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700519qla25xx_delete_queues(struct scsi_qla_host *vha)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800520{
521 int cnt, ret = 0;
522 struct req_que *req = NULL;
523 struct rsp_que *rsp = NULL;
524 struct qla_hw_data *ha = vha->hw;
525
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700526 /* Delete request queues */
527 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
528 req = ha->req_q_map[cnt];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800529 if (req) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800530 ret = qla25xx_delete_req_que(vha, req);
531 if (ret != QLA_SUCCESS) {
532 qla_printk(KERN_WARNING, ha,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700533 "Couldn't delete req que %d\n",
534 req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800535 return ret;
536 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800537 }
538 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700539
540 /* Delete response queues */
541 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
542 rsp = ha->rsp_q_map[cnt];
543 if (rsp) {
544 ret = qla25xx_delete_rsp_que(vha, rsp);
545 if (ret != QLA_SUCCESS) {
546 qla_printk(KERN_WARNING, ha,
547 "Couldn't delete rsp que %d\n",
548 rsp->id);
549 return ret;
550 }
551 }
552 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800553 return ret;
554}
555
556int
557qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700558 uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800559{
560 int ret = 0;
561 struct req_que *req = NULL;
562 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
563 uint16_t que_id = 0;
Andrew Vasquez08029992009-03-24 09:07:55 -0700564 device_reg_t __iomem *reg;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700565 uint32_t cnt;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800566
567 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
568 if (req == NULL) {
569 qla_printk(KERN_WARNING, ha, "could not allocate memory"
570 "for request que\n");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700571 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800572 }
573
574 req->length = REQUEST_ENTRY_CNT_24XX;
575 req->ring = dma_alloc_coherent(&ha->pdev->dev,
576 (req->length + 1) * sizeof(request_t),
577 &req->dma, GFP_KERNEL);
578 if (req->ring == NULL) {
579 qla_printk(KERN_WARNING, ha,
580 "Memory Allocation failed - request_ring\n");
581 goto que_failed;
582 }
583
584 mutex_lock(&ha->vport_lock);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700585 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
586 if (que_id >= ha->max_req_queues) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800587 mutex_unlock(&ha->vport_lock);
588 qla_printk(KERN_INFO, ha, "No resources to create "
589 "additional request queue\n");
590 goto que_failed;
591 }
592 set_bit(que_id, ha->req_qid_map);
593 ha->req_q_map[que_id] = req;
594 req->rid = rid;
595 req->vp_idx = vp_idx;
596 req->qos = qos;
597
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700598 if (rsp_que < 0)
599 req->rsp = NULL;
600 else
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800601 req->rsp = ha->rsp_q_map[rsp_que];
602 /* Use alternate PCI bus number */
603 if (MSB(req->rid))
604 options |= BIT_4;
605 /* Use alternate PCI devfn */
606 if (LSB(req->rid))
607 options |= BIT_5;
608 req->options = options;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700609
610 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
611 req->outstanding_cmds[cnt] = NULL;
612 req->current_outstanding_cmd = 1;
613
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800614 req->ring_ptr = req->ring;
615 req->ring_index = 0;
616 req->cnt = req->length;
617 req->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700618 reg = ISP_QUE_REG(ha, que_id);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -0800619 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800620 mutex_unlock(&ha->vport_lock);
621
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800622 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800623 if (ret != QLA_SUCCESS) {
624 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
625 mutex_lock(&ha->vport_lock);
626 clear_bit(que_id, ha->req_qid_map);
627 mutex_unlock(&ha->vport_lock);
628 goto que_failed;
629 }
630
631 return req->id;
632
633que_failed:
634 qla25xx_free_req_que(base_vha, req);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700635failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800636 return 0;
637}
638
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700639static void qla_do_work(struct work_struct *work)
640{
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800641 unsigned long flags;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700642 struct rsp_que *rsp = container_of(work, struct rsp_que, q_work);
643 struct scsi_qla_host *vha;
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800644 struct qla_hw_data *ha = rsp->hw;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700645
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800646 spin_lock_irqsave(&rsp->hw->hardware_lock, flags);
647 vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700648 qla24xx_process_response_queue(vha, rsp);
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800649 spin_unlock_irqrestore(&rsp->hw->hardware_lock, flags);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700650}
651
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800652/* create response queue */
653int
654qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700655 uint8_t vp_idx, uint16_t rid, int req)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800656{
657 int ret = 0;
658 struct rsp_que *rsp = NULL;
659 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Andrew Vasquez08029992009-03-24 09:07:55 -0700660 uint16_t que_id = 0;
661 device_reg_t __iomem *reg;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800662
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");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700667 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800668 }
669
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700670 rsp->length = RESPONSE_ENTRY_CNT_MQ;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800671 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);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700681 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
682 if (que_id >= ha->max_rsp_queues) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800683 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;
Anirban Chakraborty31557542009-12-02 10:36:55 -0800705 /* Enable MSIX handshake mode on for uncapable adapters */
706 if (!IS_MSIX_NACK_CAPABLE(ha))
707 options |= BIT_6;
708
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800709 rsp->options = options;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800710 rsp->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700711 reg = ISP_QUE_REG(ha, que_id);
712 rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
713 rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800714 mutex_unlock(&ha->vport_lock);
715
716 ret = qla25xx_request_irq(rsp);
717 if (ret)
718 goto que_failed;
719
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800720 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800721 if (ret != QLA_SUCCESS) {
722 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
723 mutex_lock(&ha->vport_lock);
724 clear_bit(que_id, ha->rsp_qid_map);
725 mutex_unlock(&ha->vport_lock);
726 goto que_failed;
727 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700728 if (req >= 0)
729 rsp->req = ha->req_q_map[req];
730 else
731 rsp->req = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800732
733 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700734 if (rsp->hw->wq)
735 INIT_WORK(&rsp->q_work, qla_do_work);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800736 return rsp->id;
737
738que_failed:
739 qla25xx_free_rsp_que(base_vha, rsp);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700740failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800741 return 0;
742}
743
744int
745qla25xx_create_queues(struct scsi_qla_host *vha, uint8_t qos)
746{
747 uint16_t options = 0;
748 uint8_t ret = 0;
749 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700750 struct rsp_que *rsp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800751
752 options |= BIT_1;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700753 ret = qla25xx_create_rsp_que(ha, options, vha->vp_idx, 0, -1);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800754 if (!ret) {
755 qla_printk(KERN_WARNING, ha, "Response Que create failed\n");
756 return ret;
757 } else
758 qla_printk(KERN_INFO, ha, "Response Que:%d created.\n", ret);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700759 rsp = ha->rsp_q_map[ret];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800760
761 options = 0;
762 if (qos & BIT_7)
763 options |= BIT_8;
764 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, ret,
765 qos & ~BIT_7);
766 if (ret) {
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700767 vha->req = ha->req_q_map[ret];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800768 qla_printk(KERN_INFO, ha, "Request Que:%d created.\n", ret);
769 } else
770 qla_printk(KERN_WARNING, ha, "Request Que create failed\n");
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700771 rsp->req = ha->req_q_map[ret];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800772
773 return ret;
774}