Merge tag 'disintegrate-raid-20121009' of git://git.infradead.org/users/dhowells...
[linux-2.6-block.git] / drivers / scsi / qla2xxx / qla_mid.c
CommitLineData
2c3dfe3f 1/*
01e58d8e 2 * QLogic Fibre Channel HBA Driver
46152ceb 3 * Copyright (c) 2003-2012 QLogic Corporation
2c3dfe3f 4 *
01e58d8e 5 * See LICENSE.qla2xxx for copyright and licensing details.
2c3dfe3f
SJ
6 */
7#include "qla_def.h"
7b867cf7 8#include "qla_gbl.h"
2d70c103 9#include "qla_target.h"
2c3dfe3f 10
2c3dfe3f
SJ
11#include <linux/moduleparam.h>
12#include <linux/vmalloc.h>
5a0e3ad6 13#include <linux/slab.h>
2c3dfe3f
SJ
14#include <linux/list.h>
15
16#include <scsi/scsi_tcq.h>
17#include <scsi/scsicam.h>
18#include <linux/delay.h>
19
2c3dfe3f
SJ
20void
21qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
22{
7b867cf7 23 if (vha->vp_idx && vha->timer_active) {
2c3dfe3f
SJ
24 del_timer_sync(&vha->timer);
25 vha->timer_active = 0;
26 }
27}
28
a824ebb3 29static uint32_t
2c3dfe3f
SJ
30qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
31{
32 uint32_t vp_id;
7b867cf7 33 struct qla_hw_data *ha = vha->hw;
feafb7b1 34 unsigned long flags;
2c3dfe3f
SJ
35
36 /* Find an empty slot and assign an vp_id */
6c2f527c 37 mutex_lock(&ha->vport_lock);
eb66dc60
AV
38 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
39 if (vp_id > ha->max_npiv_vports) {
7c3df132
SK
40 ql_dbg(ql_dbg_vport, vha, 0xa000,
41 "vp_id %d is bigger than max-supported %d.\n",
42 vp_id, ha->max_npiv_vports);
6c2f527c 43 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
44 return vp_id;
45 }
46
eb66dc60 47 set_bit(vp_id, ha->vp_idx_map);
2c3dfe3f
SJ
48 ha->num_vhosts++;
49 vha->vp_idx = vp_id;
feafb7b1
AE
50
51 spin_lock_irqsave(&ha->vport_slock, flags);
7b867cf7 52 list_add_tail(&vha->list, &ha->vp_list);
2d70c103
NB
53
54 qlt_update_vp_map(vha, SET_VP_IDX);
55
feafb7b1
AE
56 spin_unlock_irqrestore(&ha->vport_slock, flags);
57
6c2f527c 58 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
59 return vp_id;
60}
61
62void
63qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
64{
65 uint16_t vp_id;
7b867cf7 66 struct qla_hw_data *ha = vha->hw;
feafb7b1 67 unsigned long flags = 0;
2c3dfe3f 68
6c2f527c 69 mutex_lock(&ha->vport_lock);
feafb7b1
AE
70 /*
71 * Wait for all pending activities to finish before removing vport from
72 * the list.
73 * Lock needs to be held for safe removal from the list (it
74 * ensures no active vp_list traversal while the vport is removed
75 * from the queue)
76 */
77 spin_lock_irqsave(&ha->vport_slock, flags);
78 while (atomic_read(&vha->vref_count)) {
79 spin_unlock_irqrestore(&ha->vport_slock, flags);
80
81 msleep(500);
82
83 spin_lock_irqsave(&ha->vport_slock, flags);
84 }
85 list_del(&vha->list);
2d70c103 86 qlt_update_vp_map(vha, RESET_VP_IDX);
feafb7b1
AE
87 spin_unlock_irqrestore(&ha->vport_slock, flags);
88
2c3dfe3f
SJ
89 vp_id = vha->vp_idx;
90 ha->num_vhosts--;
eb66dc60 91 clear_bit(vp_id, ha->vp_idx_map);
feafb7b1 92
6c2f527c 93 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
94}
95
a824ebb3 96static scsi_qla_host_t *
7b867cf7 97qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
2c3dfe3f
SJ
98{
99 scsi_qla_host_t *vha;
ee546b6e 100 struct scsi_qla_host *tvha;
feafb7b1 101 unsigned long flags;
2c3dfe3f 102
feafb7b1 103 spin_lock_irqsave(&ha->vport_slock, flags);
2c3dfe3f 104 /* Locate matching device in database. */
ee546b6e 105 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
feafb7b1
AE
106 if (!memcmp(port_name, vha->port_name, WWN_SIZE)) {
107 spin_unlock_irqrestore(&ha->vport_slock, flags);
2c3dfe3f 108 return vha;
feafb7b1 109 }
2c3dfe3f 110 }
feafb7b1 111 spin_unlock_irqrestore(&ha->vport_slock, flags);
2c3dfe3f
SJ
112 return NULL;
113}
114
115/*
116 * qla2x00_mark_vp_devices_dead
117 * Updates fcport state when device goes offline.
118 *
119 * Input:
120 * ha = adapter block pointer.
121 * fcport = port structure pointer.
122 *
123 * Return:
124 * None.
125 *
126 * Context:
127 */
26ff776d 128static void
2c3dfe3f
SJ
129qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
130{
feafb7b1
AE
131 /*
132 * !!! NOTE !!!
133 * This function, if called in contexts other than vp create, disable
134 * or delete, please make sure this is synchronized with the
135 * delete thread.
136 */
2c3dfe3f 137 fc_port_t *fcport;
2c3dfe3f 138
7b867cf7 139 list_for_each_entry(fcport, &vha->vp_fcports, list) {
7c3df132
SK
140 ql_dbg(ql_dbg_vport, vha, 0xa001,
141 "Marking port dead, loop_id=0x%04x : %x.\n",
c6d39e23 142 fcport->loop_id, fcport->vha->vp_idx);
2c3dfe3f 143
2c3dfe3f 144 qla2x00_mark_device_lost(vha, fcport, 0, 0);
ec426e10 145 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
2c3dfe3f
SJ
146 }
147}
148
149int
150qla24xx_disable_vp(scsi_qla_host_t *vha)
151{
152 int ret;
153
154 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
155 atomic_set(&vha->loop_state, LOOP_DOWN);
156 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
157
2d70c103
NB
158 /* Remove port id from vp target map */
159 qlt_update_vp_map(vha, RESET_AL_PA);
160
2c3dfe3f
SJ
161 qla2x00_mark_vp_devices_dead(vha);
162 atomic_set(&vha->vp_state, VP_FAILED);
163 vha->flags.management_server_logged_in = 0;
164 if (ret == QLA_SUCCESS) {
165 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
166 } else {
167 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
168 return -1;
169 }
170 return 0;
171}
172
173int
174qla24xx_enable_vp(scsi_qla_host_t *vha)
175{
176 int ret;
7b867cf7
AC
177 struct qla_hw_data *ha = vha->hw;
178 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
2c3dfe3f
SJ
179
180 /* Check if physical ha port is Up */
7b867cf7 181 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
3f3b6f98
LC
182 atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
183 !(ha->current_topology & ISP_CFG_F)) {
2c3dfe3f
SJ
184 vha->vp_err_state = VP_ERR_PORTDWN;
185 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
186 goto enable_failed;
187 }
188
189 /* Initialize the new vport unless it is a persistent port */
6c2f527c 190 mutex_lock(&ha->vport_lock);
2c3dfe3f 191 ret = qla24xx_modify_vp_config(vha);
6c2f527c 192 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
193
194 if (ret != QLA_SUCCESS) {
195 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
196 goto enable_failed;
197 }
198
7c3df132
SK
199 ql_dbg(ql_dbg_taskm, vha, 0x801a,
200 "Virtual port with id: %d - Enabled.\n", vha->vp_idx);
2c3dfe3f
SJ
201 return 0;
202
203enable_failed:
7c3df132
SK
204 ql_dbg(ql_dbg_taskm, vha, 0x801b,
205 "Virtual port with id: %d - Disabled.\n", vha->vp_idx);
2c3dfe3f
SJ
206 return 1;
207}
208
26ff776d 209static void
2c3dfe3f
SJ
210qla24xx_configure_vp(scsi_qla_host_t *vha)
211{
212 struct fc_vport *fc_vport;
213 int ret;
214
215 fc_vport = vha->fc_vport;
216
7c3df132
SK
217 ql_dbg(ql_dbg_vport, vha, 0xa002,
218 "%s: change request #3.\n", __func__);
2c3dfe3f
SJ
219 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
220 if (ret != QLA_SUCCESS) {
7c3df132
SK
221 ql_dbg(ql_dbg_vport, vha, 0xa003, "Failed to enable "
222 "receiving of RSCN requests: 0x%x.\n", ret);
2c3dfe3f
SJ
223 return;
224 } else {
225 /* Corresponds to SCR enabled */
226 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
227 }
228
229 vha->flags.online = 1;
230 if (qla24xx_configure_vhba(vha))
231 return;
232
233 atomic_set(&vha->vp_state, VP_ACTIVE);
234 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
235}
236
237void
73208dfd 238qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
2c3dfe3f 239{
feafb7b1 240 scsi_qla_host_t *vha;
73208dfd 241 struct qla_hw_data *ha = rsp->hw;
7b867cf7 242 int i = 0;
feafb7b1 243 unsigned long flags;
2c3dfe3f 244
feafb7b1
AE
245 spin_lock_irqsave(&ha->vport_slock, flags);
246 list_for_each_entry(vha, &ha->vp_list, list) {
7b867cf7 247 if (vha->vp_idx) {
feafb7b1
AE
248 atomic_inc(&vha->vref_count);
249 spin_unlock_irqrestore(&ha->vport_slock, flags);
250
2c3dfe3f
SJ
251 switch (mb[0]) {
252 case MBA_LIP_OCCURRED:
253 case MBA_LOOP_UP:
254 case MBA_LOOP_DOWN:
255 case MBA_LIP_RESET:
256 case MBA_POINT_TO_POINT:
257 case MBA_CHG_IN_CONNECTION:
258 case MBA_PORT_UPDATE:
259 case MBA_RSCN_UPDATE:
7c3df132
SK
260 ql_dbg(ql_dbg_async, vha, 0x5024,
261 "Async_event for VP[%d], mb=0x%x vha=%p.\n",
262 i, *mb, vha);
73208dfd 263 qla2x00_async_event(vha, rsp, mb);
2c3dfe3f
SJ
264 break;
265 }
feafb7b1
AE
266
267 spin_lock_irqsave(&ha->vport_slock, flags);
268 atomic_dec(&vha->vref_count);
2c3dfe3f 269 }
7b867cf7 270 i++;
2c3dfe3f 271 }
feafb7b1 272 spin_unlock_irqrestore(&ha->vport_slock, flags);
2c3dfe3f
SJ
273}
274
7b867cf7 275int
2c3dfe3f
SJ
276qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
277{
278 /*
279 * Physical port will do most of the abort and recovery work. We can
280 * just treat it as a loop down
281 */
282 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
283 atomic_set(&vha->loop_state, LOOP_DOWN);
284 qla2x00_mark_all_devices_lost(vha, 0);
285 } else {
286 if (!atomic_read(&vha->loop_down_timer))
287 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
288 }
289
0d6e61bc
AV
290 /*
291 * To exclusively reset vport, we need to log it out first. Note: this
292 * control_vp can fail if ISP reset is already issued, this is
293 * expected, as the vp would be already logged out due to ISP reset.
294 */
7b867cf7
AC
295 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
296 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
297
7c3df132
SK
298 ql_dbg(ql_dbg_taskm, vha, 0x801d,
299 "Scheduling enable of Vport %d.\n", vha->vp_idx);
7b867cf7 300 return qla24xx_enable_vp(vha);
2c3dfe3f
SJ
301}
302
a824ebb3 303static int
2c3dfe3f
SJ
304qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
305{
5f28d2d7
SK
306 ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x4012,
307 "Entering %s vp_flags: 0x%lx.\n", __func__, vha->vp_flags);
7c3df132 308
ac280b67
AV
309 qla2x00_do_work(vha);
310
2c3dfe3f
SJ
311 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
312 /* VP acquired. complete port configuration */
7c3df132
SK
313 ql_dbg(ql_dbg_dpc, vha, 0x4014,
314 "Configure VP scheduled.\n");
0d6e61bc 315 qla24xx_configure_vp(vha);
7c3df132
SK
316 ql_dbg(ql_dbg_dpc, vha, 0x4015,
317 "Configure VP end.\n");
2c3dfe3f
SJ
318 return 0;
319 }
320
7b867cf7 321 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
7c3df132
SK
322 ql_dbg(ql_dbg_dpc, vha, 0x4016,
323 "FCPort update scheduled.\n");
7b867cf7
AC
324 qla2x00_update_fcports(vha);
325 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
7c3df132
SK
326 ql_dbg(ql_dbg_dpc, vha, 0x4017,
327 "FCPort update end.\n");
7b867cf7
AC
328 }
329
330 if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
331 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
332 atomic_read(&vha->loop_state) != LOOP_DOWN) {
333
7c3df132
SK
334 ql_dbg(ql_dbg_dpc, vha, 0x4018,
335 "Relogin needed scheduled.\n");
7b867cf7 336 qla2x00_relogin(vha);
7c3df132
SK
337 ql_dbg(ql_dbg_dpc, vha, 0x4019,
338 "Relogin needed end.\n");
7b867cf7 339 }
2c3dfe3f
SJ
340
341 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
342 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
343 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
344 }
345
73208dfd 346 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
2c3dfe3f 347 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
7c3df132
SK
348 ql_dbg(ql_dbg_dpc, vha, 0x401a,
349 "Loop resync scheduled.\n");
2c3dfe3f
SJ
350 qla2x00_loop_resync(vha);
351 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
7c3df132
SK
352 ql_dbg(ql_dbg_dpc, vha, 0x401b,
353 "Loop resync end.\n");
2c3dfe3f
SJ
354 }
355 }
356
5f28d2d7 357 ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x401c,
7c3df132 358 "Exiting %s.\n", __func__);
2c3dfe3f
SJ
359 return 0;
360}
361
362void
7b867cf7 363qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
2c3dfe3f
SJ
364{
365 int ret;
7b867cf7
AC
366 struct qla_hw_data *ha = vha->hw;
367 scsi_qla_host_t *vp;
feafb7b1 368 unsigned long flags = 0;
2c3dfe3f 369
7b867cf7 370 if (vha->vp_idx)
2c3dfe3f
SJ
371 return;
372 if (list_empty(&ha->vp_list))
373 return;
374
7b867cf7 375 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
2c3dfe3f 376
0d6e61bc
AV
377 if (!(ha->current_topology & ISP_CFG_F))
378 return;
379
feafb7b1
AE
380 spin_lock_irqsave(&ha->vport_slock, flags);
381 list_for_each_entry(vp, &ha->vp_list, list) {
382 if (vp->vp_idx) {
383 atomic_inc(&vp->vref_count);
384 spin_unlock_irqrestore(&ha->vport_slock, flags);
385
7b867cf7 386 ret = qla2x00_do_dpc_vp(vp);
feafb7b1
AE
387
388 spin_lock_irqsave(&ha->vport_slock, flags);
389 atomic_dec(&vp->vref_count);
390 }
2c3dfe3f 391 }
feafb7b1 392 spin_unlock_irqrestore(&ha->vport_slock, flags);
2c3dfe3f
SJ
393}
394
395int
396qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
397{
7b867cf7
AC
398 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
399 struct qla_hw_data *ha = base_vha->hw;
2c3dfe3f
SJ
400 scsi_qla_host_t *vha;
401 uint8_t port_name[WWN_SIZE];
402
403 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
404 return VPCERR_UNSUPPORTED;
405
406 /* Check up the F/W and H/W support NPIV */
407 if (!ha->flags.npiv_supported)
408 return VPCERR_UNSUPPORTED;
409
410 /* Check up whether npiv supported switch presented */
411 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
412 return VPCERR_NO_FABRIC_SUPP;
413
414 /* Check up unique WWPN */
415 u64_to_wwn(fc_vport->port_name, port_name);
7b867cf7 416 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
50db6b13 417 return VPCERR_BAD_WWN;
2c3dfe3f
SJ
418 vha = qla24xx_find_vhost_by_name(ha, port_name);
419 if (vha)
420 return VPCERR_BAD_WWN;
421
422 /* Check up max-npiv-supports */
423 if (ha->num_vhosts > ha->max_npiv_vports) {
7c3df132
SK
424 ql_dbg(ql_dbg_vport, vha, 0xa004,
425 "num_vhosts %ud is bigger "
426 "than max_npiv_vports %ud.\n",
427 ha->num_vhosts, ha->max_npiv_vports);
2c3dfe3f
SJ
428 return VPCERR_UNSUPPORTED;
429 }
430 return 0;
431}
432
433scsi_qla_host_t *
434qla24xx_create_vhost(struct fc_vport *fc_vport)
435{
7b867cf7
AC
436 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
437 struct qla_hw_data *ha = base_vha->hw;
2c3dfe3f 438 scsi_qla_host_t *vha;
a5326f86 439 struct scsi_host_template *sht = &qla2xxx_driver_template;
2c3dfe3f
SJ
440 struct Scsi_Host *host;
441
7b867cf7
AC
442 vha = qla2x00_create_host(sht, ha);
443 if (!vha) {
7c3df132
SK
444 ql_log(ql_log_warn, vha, 0xa005,
445 "scsi_host_alloc() failed for vport.\n");
2c3dfe3f
SJ
446 return(NULL);
447 }
448
7b867cf7 449 host = vha->host;
2c3dfe3f 450 fc_vport->dd_data = vha;
2c3dfe3f
SJ
451 /* New host info */
452 u64_to_wwn(fc_vport->node_name, vha->node_name);
453 u64_to_wwn(fc_vport->port_name, vha->port_name);
454
2c3dfe3f
SJ
455 vha->fc_vport = fc_vport;
456 vha->device_flags = 0;
2c3dfe3f
SJ
457 vha->vp_idx = qla24xx_allocate_vp_id(vha);
458 if (vha->vp_idx > ha->max_npiv_vports) {
7c3df132
SK
459 ql_dbg(ql_dbg_vport, vha, 0xa006,
460 "Couldn't allocate vp_id.\n");
7b867cf7 461 goto create_vhost_failed;
2c3dfe3f
SJ
462 }
463 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
464
2c3dfe3f 465 vha->dpc_flags = 0L;
2c3dfe3f
SJ
466
467 /*
468 * To fix the issue of processing a parent's RSCN for the vport before
469 * its SCR is complete.
470 */
471 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
472 atomic_set(&vha->loop_state, LOOP_DOWN);
473 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
474
475 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
476
2afa19a9
AC
477 vha->req = base_vha->req;
478 host->can_queue = base_vha->req->length + 128;
2c3dfe3f 479 host->cmd_per_lun = 3;
e02587d7 480 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
0c470874
AE
481 host->max_cmd_len = 32;
482 else
483 host->max_cmd_len = MAX_CMDSZ;
2c3dfe3f 484 host->max_channel = MAX_BUSES - 1;
82515920 485 host->max_lun = ql2xmaxlun;
711c1d91 486 host->unique_id = host->host_no;
642ef983 487 host->max_id = ha->max_fibre_devices;
2c3dfe3f
SJ
488 host->transportt = qla2xxx_transport_vport_template;
489
7c3df132
SK
490 ql_dbg(ql_dbg_vport, vha, 0xa007,
491 "Detect vport hba %ld at address = %p.\n",
492 vha->host_no, vha);
2c3dfe3f
SJ
493
494 vha->flags.init_done = 1;
2c3dfe3f 495
0d6e61bc
AV
496 mutex_lock(&ha->vport_lock);
497 set_bit(vha->vp_idx, ha->vp_idx_map);
498 ha->cur_vport_count++;
499 mutex_unlock(&ha->vport_lock);
500
2c3dfe3f
SJ
501 return vha;
502
7b867cf7 503create_vhost_failed:
2c3dfe3f
SJ
504 return NULL;
505}
73208dfd
AC
506
507static void
508qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
509{
510 struct qla_hw_data *ha = vha->hw;
511 uint16_t que_id = req->id;
512
513 dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
514 sizeof(request_t), req->ring, req->dma);
515 req->ring = NULL;
516 req->dma = 0;
517 if (que_id) {
518 ha->req_q_map[que_id] = NULL;
519 mutex_lock(&ha->vport_lock);
520 clear_bit(que_id, ha->req_qid_map);
521 mutex_unlock(&ha->vport_lock);
522 }
523 kfree(req);
524 req = NULL;
525}
526
527static void
528qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
529{
530 struct qla_hw_data *ha = vha->hw;
531 uint16_t que_id = rsp->id;
532
533 if (rsp->msix && rsp->msix->have_irq) {
534 free_irq(rsp->msix->vector, rsp);
535 rsp->msix->have_irq = 0;
536 rsp->msix->rsp = NULL;
537 }
538 dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
539 sizeof(response_t), rsp->ring, rsp->dma);
540 rsp->ring = NULL;
541 rsp->dma = 0;
542 if (que_id) {
543 ha->rsp_q_map[que_id] = NULL;
544 mutex_lock(&ha->vport_lock);
545 clear_bit(que_id, ha->rsp_qid_map);
546 mutex_unlock(&ha->vport_lock);
547 }
548 kfree(rsp);
549 rsp = NULL;
550}
551
552int
553qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
554{
555 int ret = -1;
556
557 if (req) {
558 req->options |= BIT_0;
618a7523 559 ret = qla25xx_init_req_que(vha, req);
73208dfd
AC
560 }
561 if (ret == QLA_SUCCESS)
562 qla25xx_free_req_que(vha, req);
563
564 return ret;
565}
566
3dbe756a 567static int
73208dfd
AC
568qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
569{
570 int ret = -1;
571
572 if (rsp) {
573 rsp->options |= BIT_0;
618a7523 574 ret = qla25xx_init_rsp_que(vha, rsp);
73208dfd
AC
575 }
576 if (ret == QLA_SUCCESS)
577 qla25xx_free_rsp_que(vha, rsp);
578
579 return ret;
580}
581
73208dfd
AC
582/* Delete all queues for a given vhost */
583int
2afa19a9 584qla25xx_delete_queues(struct scsi_qla_host *vha)
73208dfd
AC
585{
586 int cnt, ret = 0;
587 struct req_que *req = NULL;
588 struct rsp_que *rsp = NULL;
589 struct qla_hw_data *ha = vha->hw;
590
2afa19a9
AC
591 /* Delete request queues */
592 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
593 req = ha->req_q_map[cnt];
73208dfd 594 if (req) {
73208dfd
AC
595 ret = qla25xx_delete_req_que(vha, req);
596 if (ret != QLA_SUCCESS) {
7c3df132
SK
597 ql_log(ql_log_warn, vha, 0x00ea,
598 "Couldn't delete req que %d.\n",
599 req->id);
73208dfd
AC
600 return ret;
601 }
73208dfd 602 }
2afa19a9
AC
603 }
604
605 /* Delete response queues */
606 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
607 rsp = ha->rsp_q_map[cnt];
608 if (rsp) {
609 ret = qla25xx_delete_rsp_que(vha, rsp);
610 if (ret != QLA_SUCCESS) {
7c3df132
SK
611 ql_log(ql_log_warn, vha, 0x00eb,
612 "Couldn't delete rsp que %d.\n",
613 rsp->id);
2afa19a9 614 return ret;
73208dfd
AC
615 }
616 }
617 }
73208dfd
AC
618 return ret;
619}
620
621int
622qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
2afa19a9 623 uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
73208dfd
AC
624{
625 int ret = 0;
626 struct req_que *req = NULL;
627 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
628 uint16_t que_id = 0;
08029990 629 device_reg_t __iomem *reg;
2afa19a9 630 uint32_t cnt;
73208dfd
AC
631
632 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
633 if (req == NULL) {
7c3df132
SK
634 ql_log(ql_log_fatal, base_vha, 0x00d9,
635 "Failed to allocate memory for request queue.\n");
c7922a91 636 goto failed;
73208dfd
AC
637 }
638
639 req->length = REQUEST_ENTRY_CNT_24XX;
640 req->ring = dma_alloc_coherent(&ha->pdev->dev,
641 (req->length + 1) * sizeof(request_t),
642 &req->dma, GFP_KERNEL);
643 if (req->ring == NULL) {
7c3df132 644 ql_log(ql_log_fatal, base_vha, 0x00da,
d6a03581 645 "Failed to allocate memory for request_ring.\n");
73208dfd
AC
646 goto que_failed;
647 }
648
649 mutex_lock(&ha->vport_lock);
2afa19a9
AC
650 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
651 if (que_id >= ha->max_req_queues) {
73208dfd 652 mutex_unlock(&ha->vport_lock);
7c3df132
SK
653 ql_log(ql_log_warn, base_vha, 0x00db,
654 "No resources to create additional request queue.\n");
73208dfd
AC
655 goto que_failed;
656 }
657 set_bit(que_id, ha->req_qid_map);
658 ha->req_q_map[que_id] = req;
659 req->rid = rid;
660 req->vp_idx = vp_idx;
661 req->qos = qos;
662
7c3df132
SK
663 ql_dbg(ql_dbg_multiq, base_vha, 0xc002,
664 "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
665 que_id, req->rid, req->vp_idx, req->qos);
666 ql_dbg(ql_dbg_init, base_vha, 0x00dc,
667 "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
668 que_id, req->rid, req->vp_idx, req->qos);
2afa19a9
AC
669 if (rsp_que < 0)
670 req->rsp = NULL;
671 else
73208dfd
AC
672 req->rsp = ha->rsp_q_map[rsp_que];
673 /* Use alternate PCI bus number */
674 if (MSB(req->rid))
675 options |= BIT_4;
676 /* Use alternate PCI devfn */
677 if (LSB(req->rid))
678 options |= BIT_5;
679 req->options = options;
2afa19a9 680
7c3df132
SK
681 ql_dbg(ql_dbg_multiq, base_vha, 0xc003,
682 "options=0x%x.\n", req->options);
683 ql_dbg(ql_dbg_init, base_vha, 0x00dd,
684 "options=0x%x.\n", req->options);
2afa19a9
AC
685 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
686 req->outstanding_cmds[cnt] = NULL;
687 req->current_outstanding_cmd = 1;
688
73208dfd
AC
689 req->ring_ptr = req->ring;
690 req->ring_index = 0;
691 req->cnt = req->length;
692 req->id = que_id;
08029990 693 reg = ISP_QUE_REG(ha, que_id);
29bdccbe 694 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
73208dfd 695 mutex_unlock(&ha->vport_lock);
7c3df132
SK
696 ql_dbg(ql_dbg_multiq, base_vha, 0xc004,
697 "ring_ptr=%p ring_index=%d, "
698 "cnt=%d id=%d max_q_depth=%d.\n",
699 req->ring_ptr, req->ring_index,
700 req->cnt, req->id, req->max_q_depth);
701 ql_dbg(ql_dbg_init, base_vha, 0x00de,
702 "ring_ptr=%p ring_index=%d, "
703 "cnt=%d id=%d max_q_depth=%d.\n",
704 req->ring_ptr, req->ring_index, req->cnt,
705 req->id, req->max_q_depth);
73208dfd 706
618a7523 707 ret = qla25xx_init_req_que(base_vha, req);
73208dfd 708 if (ret != QLA_SUCCESS) {
7c3df132
SK
709 ql_log(ql_log_fatal, base_vha, 0x00df,
710 "%s failed.\n", __func__);
73208dfd
AC
711 mutex_lock(&ha->vport_lock);
712 clear_bit(que_id, ha->req_qid_map);
713 mutex_unlock(&ha->vport_lock);
714 goto que_failed;
715 }
716
717 return req->id;
718
719que_failed:
720 qla25xx_free_req_que(base_vha, req);
c7922a91 721failed:
73208dfd
AC
722 return 0;
723}
724
68ca949c
AC
725static void qla_do_work(struct work_struct *work)
726{
a67093d4 727 unsigned long flags;
68ca949c
AC
728 struct rsp_que *rsp = container_of(work, struct rsp_que, q_work);
729 struct scsi_qla_host *vha;
a67093d4 730 struct qla_hw_data *ha = rsp->hw;
68ca949c 731
a67093d4
AC
732 spin_lock_irqsave(&rsp->hw->hardware_lock, flags);
733 vha = pci_get_drvdata(ha->pdev);
68ca949c 734 qla24xx_process_response_queue(vha, rsp);
a67093d4 735 spin_unlock_irqrestore(&rsp->hw->hardware_lock, flags);
68ca949c
AC
736}
737
73208dfd
AC
738/* create response queue */
739int
740qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
2afa19a9 741 uint8_t vp_idx, uint16_t rid, int req)
73208dfd
AC
742{
743 int ret = 0;
744 struct rsp_que *rsp = NULL;
745 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
08029990
AV
746 uint16_t que_id = 0;
747 device_reg_t __iomem *reg;
73208dfd
AC
748
749 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
750 if (rsp == NULL) {
7c3df132
SK
751 ql_log(ql_log_warn, base_vha, 0x0066,
752 "Failed to allocate memory for response queue.\n");
c7922a91 753 goto failed;
73208dfd
AC
754 }
755
2afa19a9 756 rsp->length = RESPONSE_ENTRY_CNT_MQ;
73208dfd
AC
757 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
758 (rsp->length + 1) * sizeof(response_t),
759 &rsp->dma, GFP_KERNEL);
760 if (rsp->ring == NULL) {
7c3df132
SK
761 ql_log(ql_log_warn, base_vha, 0x00e1,
762 "Failed to allocate memory for response ring.\n");
73208dfd
AC
763 goto que_failed;
764 }
765
766 mutex_lock(&ha->vport_lock);
2afa19a9
AC
767 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
768 if (que_id >= ha->max_rsp_queues) {
73208dfd 769 mutex_unlock(&ha->vport_lock);
7c3df132
SK
770 ql_log(ql_log_warn, base_vha, 0x00e2,
771 "No resources to create additional request queue.\n");
73208dfd
AC
772 goto que_failed;
773 }
774 set_bit(que_id, ha->rsp_qid_map);
775
776 if (ha->flags.msix_enabled)
777 rsp->msix = &ha->msix_entries[que_id + 1];
778 else
7c3df132
SK
779 ql_log(ql_log_warn, base_vha, 0x00e3,
780 "MSIX not enalbled.\n");
73208dfd
AC
781
782 ha->rsp_q_map[que_id] = rsp;
783 rsp->rid = rid;
784 rsp->vp_idx = vp_idx;
785 rsp->hw = ha;
7c3df132
SK
786 ql_dbg(ql_dbg_init, base_vha, 0x00e4,
787 "queue_id=%d rid=%d vp_idx=%d hw=%p.\n",
788 que_id, rsp->rid, rsp->vp_idx, rsp->hw);
73208dfd
AC
789 /* Use alternate PCI bus number */
790 if (MSB(rsp->rid))
791 options |= BIT_4;
792 /* Use alternate PCI devfn */
793 if (LSB(rsp->rid))
794 options |= BIT_5;
3155754a
AC
795 /* Enable MSIX handshake mode on for uncapable adapters */
796 if (!IS_MSIX_NACK_CAPABLE(ha))
797 options |= BIT_6;
798
73208dfd 799 rsp->options = options;
73208dfd 800 rsp->id = que_id;
08029990
AV
801 reg = ISP_QUE_REG(ha, que_id);
802 rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
803 rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
73208dfd 804 mutex_unlock(&ha->vport_lock);
7c3df132
SK
805 ql_dbg(ql_dbg_multiq, base_vha, 0xc00b,
806 "options=%x id=%d rsp_q_in=%p rsp_q_out=%p",
807 rsp->options, rsp->id, rsp->rsp_q_in,
808 rsp->rsp_q_out);
809 ql_dbg(ql_dbg_init, base_vha, 0x00e5,
810 "options=%x id=%d rsp_q_in=%p rsp_q_out=%p",
811 rsp->options, rsp->id, rsp->rsp_q_in,
812 rsp->rsp_q_out);
73208dfd
AC
813
814 ret = qla25xx_request_irq(rsp);
815 if (ret)
816 goto que_failed;
817
618a7523 818 ret = qla25xx_init_rsp_que(base_vha, rsp);
73208dfd 819 if (ret != QLA_SUCCESS) {
7c3df132
SK
820 ql_log(ql_log_fatal, base_vha, 0x00e7,
821 "%s failed.\n", __func__);
73208dfd
AC
822 mutex_lock(&ha->vport_lock);
823 clear_bit(que_id, ha->rsp_qid_map);
824 mutex_unlock(&ha->vport_lock);
825 goto que_failed;
826 }
2afa19a9
AC
827 if (req >= 0)
828 rsp->req = ha->req_q_map[req];
829 else
830 rsp->req = NULL;
73208dfd
AC
831
832 qla2x00_init_response_q_entries(rsp);
68ca949c
AC
833 if (rsp->hw->wq)
834 INIT_WORK(&rsp->q_work, qla_do_work);
73208dfd
AC
835 return rsp->id;
836
837que_failed:
838 qla25xx_free_rsp_que(base_vha, rsp);
c7922a91 839failed:
73208dfd
AC
840 return 0;
841}