blob: 286fb4ce4b7e4054f03447d6c761b8182a1bbb45 [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -07002 * Copyright (C) 2005 - 2011 Emulex
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation. The full GNU General
8 * Public License is included in this distribution in the file called COPYING.
9 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070010 * Written by: Jayamohan Kallickal (jayamohan.kallickal@emulex.com)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053011 *
12 * Contact Information:
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070013 * linux-drivers@emulex.com
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053014 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070015 * Emulex
16 * 3333 Susan Street
17 * Costa Mesa, CA 92626
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053018 */
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070019
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053020#include <linux/reboot.h>
21#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053023#include <linux/interrupt.h>
24#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/string.h>
27#include <linux/kernel.h>
28#include <linux/semaphore.h>
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +053029#include <linux/iscsi_boot_sysfs.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040030#include <linux/module.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053031
32#include <scsi/libiscsi.h>
33#include <scsi/scsi_transport_iscsi.h>
34#include <scsi/scsi_transport.h>
35#include <scsi/scsi_cmnd.h>
36#include <scsi/scsi_device.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi.h>
39#include "be_main.h"
40#include "be_iscsi.h"
41#include "be_mgmt.h"
42
43static unsigned int be_iopoll_budget = 10;
44static unsigned int be_max_phys_size = 64;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053045static unsigned int enable_msix = 1;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +053046static unsigned int gcrashmode = 0;
47static unsigned int num_hba = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053048
49MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
50MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
Jayamohan Kallickal2f635882012-04-03 23:41:45 -050051MODULE_AUTHOR("Emulex Corporation");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053052MODULE_LICENSE("GPL");
53module_param(be_iopoll_budget, int, 0);
54module_param(enable_msix, int, 0);
55module_param(be_max_phys_size, uint, S_IRUGO);
56MODULE_PARM_DESC(be_max_phys_size, "Maximum Size (In Kilobytes) of physically"
57 "contiguous memory that can be allocated."
58 "Range is 16 - 128");
59
60static int beiscsi_slave_configure(struct scsi_device *sdev)
61{
62 blk_queue_max_segment_size(sdev->request_queue, 65536);
63 return 0;
64}
65
Jayamohan Kallickal41831222010-02-20 08:02:39 +053066static int beiscsi_eh_abort(struct scsi_cmnd *sc)
67{
68 struct iscsi_cls_session *cls_session;
69 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
70 struct beiscsi_io_task *aborted_io_task;
71 struct iscsi_conn *conn;
72 struct beiscsi_conn *beiscsi_conn;
73 struct beiscsi_hba *phba;
74 struct iscsi_session *session;
75 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +053076 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +053077 unsigned int cid, tag, num_invalidate;
78
79 cls_session = starget_to_session(scsi_target(sc->device));
80 session = cls_session->dd_data;
81
82 spin_lock_bh(&session->lock);
83 if (!aborted_task || !aborted_task->sc) {
84 /* we raced */
85 spin_unlock_bh(&session->lock);
86 return SUCCESS;
87 }
88
89 aborted_io_task = aborted_task->dd_data;
90 if (!aborted_io_task->scsi_cmnd) {
91 /* raced or invalid command */
92 spin_unlock_bh(&session->lock);
93 return SUCCESS;
94 }
95 spin_unlock_bh(&session->lock);
96 conn = aborted_task->conn;
97 beiscsi_conn = conn->dd_data;
98 phba = beiscsi_conn->phba;
99
100 /* invalidate iocb */
101 cid = beiscsi_conn->beiscsi_conn_cid;
102 inv_tbl = phba->inv_tbl;
103 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
104 inv_tbl->cid = cid;
105 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
106 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530107 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
108 sizeof(struct invalidate_commands_params_in),
109 &nonemb_cmd.dma);
110 if (nonemb_cmd.va == NULL) {
111 SE_DEBUG(DBG_LVL_1,
112 "Failed to allocate memory for"
113 "mgmt_invalidate_icds\n");
114 return FAILED;
115 }
116 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
117
118 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
119 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530120 if (!tag) {
121 shost_printk(KERN_WARNING, phba->shost,
122 "mgmt_invalidate_icds could not be"
123 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530124 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
125 nonemb_cmd.va, nonemb_cmd.dma);
126
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530127 return FAILED;
128 } else {
129 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
130 phba->ctrl.mcc_numtag[tag]);
131 free_mcc_tag(&phba->ctrl, tag);
132 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530133 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
134 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530135 return iscsi_eh_abort(sc);
136}
137
138static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
139{
140 struct iscsi_task *abrt_task;
141 struct beiscsi_io_task *abrt_io_task;
142 struct iscsi_conn *conn;
143 struct beiscsi_conn *beiscsi_conn;
144 struct beiscsi_hba *phba;
145 struct iscsi_session *session;
146 struct iscsi_cls_session *cls_session;
147 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530148 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530149 unsigned int cid, tag, i, num_invalidate;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530150
151 /* invalidate iocbs */
152 cls_session = starget_to_session(scsi_target(sc->device));
153 session = cls_session->dd_data;
154 spin_lock_bh(&session->lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500155 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
156 spin_unlock_bh(&session->lock);
157 return FAILED;
158 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530159 conn = session->leadconn;
160 beiscsi_conn = conn->dd_data;
161 phba = beiscsi_conn->phba;
162 cid = beiscsi_conn->beiscsi_conn_cid;
163 inv_tbl = phba->inv_tbl;
164 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
165 num_invalidate = 0;
166 for (i = 0; i < conn->session->cmds_max; i++) {
167 abrt_task = conn->session->cmds[i];
168 abrt_io_task = abrt_task->dd_data;
169 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
170 continue;
171
172 if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
173 continue;
174
175 inv_tbl->cid = cid;
176 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
177 num_invalidate++;
178 inv_tbl++;
179 }
180 spin_unlock_bh(&session->lock);
181 inv_tbl = phba->inv_tbl;
182
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530183 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
184 sizeof(struct invalidate_commands_params_in),
185 &nonemb_cmd.dma);
186 if (nonemb_cmd.va == NULL) {
187 SE_DEBUG(DBG_LVL_1,
188 "Failed to allocate memory for"
189 "mgmt_invalidate_icds\n");
190 return FAILED;
191 }
192 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
193 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
194 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
195 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530196 if (!tag) {
197 shost_printk(KERN_WARNING, phba->shost,
198 "mgmt_invalidate_icds could not be"
199 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530200 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
201 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530202 return FAILED;
203 } else {
204 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
205 phba->ctrl.mcc_numtag[tag]);
206 free_mcc_tag(&phba->ctrl, tag);
207 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530208 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
209 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530210 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530211}
212
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530213static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
214{
215 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500216 struct mgmt_session_info *boot_sess = &phba->boot_sess;
217 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530218 char *str = buf;
219 int rc;
220
221 switch (type) {
222 case ISCSI_BOOT_TGT_NAME:
223 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500224 (int)strlen(boot_sess->target_name),
225 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530226 break;
227 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500228 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530229 rc = sprintf(buf, "%pI4\n",
Mike Christief457a462011-06-24 15:11:53 -0500230 (char *)&boot_conn->dest_ipaddr.ip_address);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530231 else
232 rc = sprintf(str, "%pI6\n",
Mike Christief457a462011-06-24 15:11:53 -0500233 (char *)&boot_conn->dest_ipaddr.ip_address);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530234 break;
235 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500236 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530237 break;
238
239 case ISCSI_BOOT_TGT_CHAP_NAME:
240 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500241 boot_conn->negotiated_login_options.auth_data.chap.
242 target_chap_name_length,
243 (char *)&boot_conn->negotiated_login_options.
244 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530245 break;
246 case ISCSI_BOOT_TGT_CHAP_SECRET:
247 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500248 boot_conn->negotiated_login_options.auth_data.chap.
249 target_secret_length,
250 (char *)&boot_conn->negotiated_login_options.
251 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530252 break;
253 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
254 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500255 boot_conn->negotiated_login_options.auth_data.chap.
256 intr_chap_name_length,
257 (char *)&boot_conn->negotiated_login_options.
258 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530259 break;
260 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500261 rc = sprintf(str, "%.*s\n",
262 boot_conn->negotiated_login_options.auth_data.chap.
263 intr_secret_length,
264 (char *)&boot_conn->negotiated_login_options.
265 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530266 break;
267 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500268 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530269 break;
270 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500271 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530272 break;
273 default:
274 rc = -ENOSYS;
275 break;
276 }
277 return rc;
278}
279
280static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
281{
282 struct beiscsi_hba *phba = data;
283 char *str = buf;
284 int rc;
285
286 switch (type) {
287 case ISCSI_BOOT_INI_INITIATOR_NAME:
288 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
289 break;
290 default:
291 rc = -ENOSYS;
292 break;
293 }
294 return rc;
295}
296
297static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
298{
299 struct beiscsi_hba *phba = data;
300 char *str = buf;
301 int rc;
302
303 switch (type) {
304 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500305 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530306 break;
307 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500308 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530309 break;
310 case ISCSI_BOOT_ETH_MAC:
311 rc = beiscsi_get_macaddr(buf, phba);
312 if (rc < 0) {
313 SE_DEBUG(DBG_LVL_1, "beiscsi_get_macaddr Failed\n");
314 return rc;
315 }
316 break;
317 default:
318 rc = -ENOSYS;
319 break;
320 }
321 return rc;
322}
323
324
Al Viro587a1f12011-07-23 23:11:19 -0400325static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530326{
Al Viro587a1f12011-07-23 23:11:19 -0400327 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530328
329 switch (type) {
330 case ISCSI_BOOT_TGT_NAME:
331 case ISCSI_BOOT_TGT_IP_ADDR:
332 case ISCSI_BOOT_TGT_PORT:
333 case ISCSI_BOOT_TGT_CHAP_NAME:
334 case ISCSI_BOOT_TGT_CHAP_SECRET:
335 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
336 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
337 case ISCSI_BOOT_TGT_NIC_ASSOC:
338 case ISCSI_BOOT_TGT_FLAGS:
339 rc = S_IRUGO;
340 break;
341 default:
342 rc = 0;
343 break;
344 }
345 return rc;
346}
347
Al Viro587a1f12011-07-23 23:11:19 -0400348static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530349{
Al Viro587a1f12011-07-23 23:11:19 -0400350 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530351
352 switch (type) {
353 case ISCSI_BOOT_INI_INITIATOR_NAME:
354 rc = S_IRUGO;
355 break;
356 default:
357 rc = 0;
358 break;
359 }
360 return rc;
361}
362
363
Al Viro587a1f12011-07-23 23:11:19 -0400364static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530365{
Al Viro587a1f12011-07-23 23:11:19 -0400366 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530367
368 switch (type) {
369 case ISCSI_BOOT_ETH_FLAGS:
370 case ISCSI_BOOT_ETH_MAC:
371 case ISCSI_BOOT_ETH_INDEX:
372 rc = S_IRUGO;
373 break;
374 default:
375 rc = 0;
376 break;
377 }
378 return rc;
379}
380
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530381/*------------------- PCI Driver operations and data ----------------- */
382static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
383 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530384 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530385 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
386 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
387 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530388 { 0 }
389};
390MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
391
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530392static struct scsi_host_template beiscsi_sht = {
393 .module = THIS_MODULE,
Jayamohan Kallickal2f635882012-04-03 23:41:45 -0500394 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530395 .proc_name = DRV_NAME,
396 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530397 .change_queue_depth = iscsi_change_queue_depth,
398 .slave_configure = beiscsi_slave_configure,
399 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530400 .eh_abort_handler = beiscsi_eh_abort,
401 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530402 .eh_target_reset_handler = iscsi_eh_session_reset,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530403 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
404 .can_queue = BE2_IO_DEPTH,
405 .this_id = -1,
406 .max_sectors = BEISCSI_MAX_SECTORS,
407 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
408 .use_clustering = ENABLE_CLUSTERING,
409};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530410
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530411static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530412
413static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
414{
415 struct beiscsi_hba *phba;
416 struct Scsi_Host *shost;
417
418 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
419 if (!shost) {
420 dev_err(&pcidev->dev, "beiscsi_hba_alloc -"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530421 "iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530422 return NULL;
423 }
424 shost->dma_boundary = pcidev->dma_mask;
425 shost->max_id = BE2_MAX_SESSIONS;
426 shost->max_channel = 0;
427 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
428 shost->max_lun = BEISCSI_NUM_MAX_LUN;
429 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530430 phba = iscsi_host_priv(shost);
431 memset(phba, 0, sizeof(*phba));
432 phba->shost = shost;
433 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530434 pci_set_drvdata(pcidev, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530435
436 if (iscsi_host_add(shost, &phba->pcidev->dev))
437 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530438
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530439 return phba;
440
441free_devices:
442 pci_dev_put(phba->pcidev);
443 iscsi_host_free(phba->shost);
444 return NULL;
445}
446
447static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
448{
449 if (phba->csr_va) {
450 iounmap(phba->csr_va);
451 phba->csr_va = NULL;
452 }
453 if (phba->db_va) {
454 iounmap(phba->db_va);
455 phba->db_va = NULL;
456 }
457 if (phba->pci_va) {
458 iounmap(phba->pci_va);
459 phba->pci_va = NULL;
460 }
461}
462
463static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
464 struct pci_dev *pcidev)
465{
466 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530467 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530468
469 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
470 pci_resource_len(pcidev, 2));
471 if (addr == NULL)
472 return -ENOMEM;
473 phba->ctrl.csr = addr;
474 phba->csr_va = addr;
475 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
476
477 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
478 if (addr == NULL)
479 goto pci_map_err;
480 phba->ctrl.db = addr;
481 phba->db_va = addr;
482 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
483
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530484 if (phba->generation == BE_GEN2)
485 pcicfg_reg = 1;
486 else
487 pcicfg_reg = 0;
488
489 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
490 pci_resource_len(pcidev, pcicfg_reg));
491
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530492 if (addr == NULL)
493 goto pci_map_err;
494 phba->ctrl.pcicfg = addr;
495 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530496 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530497 return 0;
498
499pci_map_err:
500 beiscsi_unmap_pci_function(phba);
501 return -ENOMEM;
502}
503
504static int beiscsi_enable_pci(struct pci_dev *pcidev)
505{
506 int ret;
507
508 ret = pci_enable_device(pcidev);
509 if (ret) {
510 dev_err(&pcidev->dev, "beiscsi_enable_pci - enable device "
511 "failed. Returning -ENODEV\n");
512 return ret;
513 }
514
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530515 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530516 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
517 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
518 if (ret) {
519 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
520 pci_disable_device(pcidev);
521 return ret;
522 }
523 }
524 return 0;
525}
526
527static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
528{
529 struct be_ctrl_info *ctrl = &phba->ctrl;
530 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
531 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
532 int status = 0;
533
534 ctrl->pdev = pdev;
535 status = beiscsi_map_pci_bars(phba, pdev);
536 if (status)
537 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530538 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
539 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
540 mbox_mem_alloc->size,
541 &mbox_mem_alloc->dma);
542 if (!mbox_mem_alloc->va) {
543 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500544 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530545 }
546
547 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
548 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
549 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
550 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
551 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530552 spin_lock_init(&phba->ctrl.mcc_lock);
553 spin_lock_init(&phba->ctrl.mcc_cq_lock);
554
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530555 return status;
556}
557
558static void beiscsi_get_params(struct beiscsi_hba *phba)
559{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530560 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
561 - (phba->fw_config.iscsi_cid_count
562 + BE2_TMFS
563 + BE2_NOPOUT_REQ));
564 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530565 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700566 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530567 phba->params.num_sge_per_io = BE2_SGE;
568 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
569 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
570 phba->params.eq_timer = 64;
571 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530572 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
573 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530574 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
575 ? 1024 : phba->params.num_eq_entries;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530576 SE_DEBUG(DBG_LVL_8, "phba->params.num_eq_entries=%d\n",
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530577 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530578 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530579 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
580 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530581 phba->params.wrbs_per_cxn = 256;
582}
583
584static void hwi_ring_eq_db(struct beiscsi_hba *phba,
585 unsigned int id, unsigned int clr_interrupt,
586 unsigned int num_processed,
587 unsigned char rearm, unsigned char event)
588{
589 u32 val = 0;
590 val |= id & DB_EQ_RING_ID_MASK;
591 if (rearm)
592 val |= 1 << DB_EQ_REARM_SHIFT;
593 if (clr_interrupt)
594 val |= 1 << DB_EQ_CLR_SHIFT;
595 if (event)
596 val |= 1 << DB_EQ_EVNT_SHIFT;
597 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
598 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
599}
600
601/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530602 * be_isr_mcc - The isr routine of the driver.
603 * @irq: Not used
604 * @dev_id: Pointer to host adapter structure
605 */
606static irqreturn_t be_isr_mcc(int irq, void *dev_id)
607{
608 struct beiscsi_hba *phba;
609 struct be_eq_entry *eqe = NULL;
610 struct be_queue_info *eq;
611 struct be_queue_info *mcc;
612 unsigned int num_eq_processed;
613 struct be_eq_obj *pbe_eq;
614 unsigned long flags;
615
616 pbe_eq = dev_id;
617 eq = &pbe_eq->q;
618 phba = pbe_eq->phba;
619 mcc = &phba->ctrl.mcc_obj.cq;
620 eqe = queue_tail_node(eq);
621 if (!eqe)
622 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
623
624 num_eq_processed = 0;
625
626 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
627 & EQE_VALID_MASK) {
628 if (((eqe->dw[offsetof(struct amap_eq_entry,
629 resource_id) / 32] &
630 EQE_RESID_MASK) >> 16) == mcc->id) {
631 spin_lock_irqsave(&phba->isr_lock, flags);
632 phba->todo_mcc_cq = 1;
633 spin_unlock_irqrestore(&phba->isr_lock, flags);
634 }
635 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
636 queue_tail_inc(eq);
637 eqe = queue_tail_node(eq);
638 num_eq_processed++;
639 }
640 if (phba->todo_mcc_cq)
641 queue_work(phba->wq, &phba->work_cqs);
642 if (num_eq_processed)
643 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
644
645 return IRQ_HANDLED;
646}
647
648/**
649 * be_isr_msix - The isr routine of the driver.
650 * @irq: Not used
651 * @dev_id: Pointer to host adapter structure
652 */
653static irqreturn_t be_isr_msix(int irq, void *dev_id)
654{
655 struct beiscsi_hba *phba;
656 struct be_eq_entry *eqe = NULL;
657 struct be_queue_info *eq;
658 struct be_queue_info *cq;
659 unsigned int num_eq_processed;
660 struct be_eq_obj *pbe_eq;
661 unsigned long flags;
662
663 pbe_eq = dev_id;
664 eq = &pbe_eq->q;
665 cq = pbe_eq->cq;
666 eqe = queue_tail_node(eq);
667 if (!eqe)
668 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
669
670 phba = pbe_eq->phba;
671 num_eq_processed = 0;
672 if (blk_iopoll_enabled) {
673 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
674 & EQE_VALID_MASK) {
675 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
676 blk_iopoll_sched(&pbe_eq->iopoll);
677
678 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
679 queue_tail_inc(eq);
680 eqe = queue_tail_node(eq);
681 num_eq_processed++;
682 }
683 if (num_eq_processed)
684 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
685
686 return IRQ_HANDLED;
687 } else {
688 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
689 & EQE_VALID_MASK) {
690 spin_lock_irqsave(&phba->isr_lock, flags);
691 phba->todo_cq = 1;
692 spin_unlock_irqrestore(&phba->isr_lock, flags);
693 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
694 queue_tail_inc(eq);
695 eqe = queue_tail_node(eq);
696 num_eq_processed++;
697 }
698 if (phba->todo_cq)
699 queue_work(phba->wq, &phba->work_cqs);
700
701 if (num_eq_processed)
702 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
703
704 return IRQ_HANDLED;
705 }
706}
707
708/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530709 * be_isr - The isr routine of the driver.
710 * @irq: Not used
711 * @dev_id: Pointer to host adapter structure
712 */
713static irqreturn_t be_isr(int irq, void *dev_id)
714{
715 struct beiscsi_hba *phba;
716 struct hwi_controller *phwi_ctrlr;
717 struct hwi_context_memory *phwi_context;
718 struct be_eq_entry *eqe = NULL;
719 struct be_queue_info *eq;
720 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530721 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530722 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530723 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530724 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530725 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530726 int isr;
727
728 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700729 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530730 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
731 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
732 if (!isr)
733 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530734
735 phwi_ctrlr = phba->phwi_ctrlr;
736 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530737 pbe_eq = &phwi_context->be_eq[0];
738
739 eq = &phwi_context->be_eq[0].q;
740 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530741 index = 0;
742 eqe = queue_tail_node(eq);
743 if (!eqe)
744 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
745
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530746 num_ioeq_processed = 0;
747 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530748 if (blk_iopoll_enabled) {
749 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
750 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530751 if (((eqe->dw[offsetof(struct amap_eq_entry,
752 resource_id) / 32] &
753 EQE_RESID_MASK) >> 16) == mcc->id) {
754 spin_lock_irqsave(&phba->isr_lock, flags);
755 phba->todo_mcc_cq = 1;
756 spin_unlock_irqrestore(&phba->isr_lock, flags);
757 num_mcceq_processed++;
758 } else {
759 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
760 blk_iopoll_sched(&pbe_eq->iopoll);
761 num_ioeq_processed++;
762 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530763 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
764 queue_tail_inc(eq);
765 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530766 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530767 if (num_ioeq_processed || num_mcceq_processed) {
768 if (phba->todo_mcc_cq)
769 queue_work(phba->wq, &phba->work_cqs);
770
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530771 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530772 hwi_ring_eq_db(phba, eq->id, 0,
773 (num_ioeq_processed +
774 num_mcceq_processed) , 1, 1);
775 else
776 hwi_ring_eq_db(phba, eq->id, 0,
777 (num_ioeq_processed +
778 num_mcceq_processed), 0, 1);
779
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530780 return IRQ_HANDLED;
781 } else
782 return IRQ_NONE;
783 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530784 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530785 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
786 & EQE_VALID_MASK) {
787
788 if (((eqe->dw[offsetof(struct amap_eq_entry,
789 resource_id) / 32] &
790 EQE_RESID_MASK) >> 16) != cq->id) {
791 spin_lock_irqsave(&phba->isr_lock, flags);
792 phba->todo_mcc_cq = 1;
793 spin_unlock_irqrestore(&phba->isr_lock, flags);
794 } else {
795 spin_lock_irqsave(&phba->isr_lock, flags);
796 phba->todo_cq = 1;
797 spin_unlock_irqrestore(&phba->isr_lock, flags);
798 }
799 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
800 queue_tail_inc(eq);
801 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530802 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530803 }
804 if (phba->todo_cq || phba->todo_mcc_cq)
805 queue_work(phba->wq, &phba->work_cqs);
806
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530807 if (num_ioeq_processed) {
808 hwi_ring_eq_db(phba, eq->id, 0,
809 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530810 return IRQ_HANDLED;
811 } else
812 return IRQ_NONE;
813 }
814}
815
816static int beiscsi_init_irqs(struct beiscsi_hba *phba)
817{
818 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530819 struct hwi_controller *phwi_ctrlr;
820 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530821 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530822
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530823 phwi_ctrlr = phba->phwi_ctrlr;
824 phwi_context = phwi_ctrlr->phwi_ctxt;
825
826 if (phba->msix_enabled) {
827 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700828 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
829 GFP_KERNEL);
830 if (!phba->msi_name[i]) {
831 ret = -ENOMEM;
832 goto free_msix_irqs;
833 }
834
835 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
836 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530837 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700838 ret = request_irq(msix_vec, be_isr_msix, 0,
839 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530840 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530841 if (ret) {
842 shost_printk(KERN_ERR, phba->shost,
843 "beiscsi_init_irqs-Failed to"
844 "register msix for i = %d\n", i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700845 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530846 goto free_msix_irqs;
847 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530848 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700849 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
850 if (!phba->msi_name[i]) {
851 ret = -ENOMEM;
852 goto free_msix_irqs;
853 }
854 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
855 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530856 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700857 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530858 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530859 if (ret) {
860 shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
861 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700862 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530863 goto free_msix_irqs;
864 }
865
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530866 } else {
867 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
868 "beiscsi", phba);
869 if (ret) {
870 shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
871 "Failed to register irq\\n");
872 return ret;
873 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530874 }
875 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530876free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700877 for (j = i - 1; j >= 0; j--) {
878 kfree(phba->msi_name[j]);
879 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530880 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700881 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530882 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530883}
884
885static void hwi_ring_cq_db(struct beiscsi_hba *phba,
886 unsigned int id, unsigned int num_processed,
887 unsigned char rearm, unsigned char event)
888{
889 u32 val = 0;
890 val |= id & DB_CQ_RING_ID_MASK;
891 if (rearm)
892 val |= 1 << DB_CQ_REARM_SHIFT;
893 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
894 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
895}
896
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530897static unsigned int
898beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
899 struct beiscsi_hba *phba,
900 unsigned short cid,
901 struct pdu_base *ppdu,
902 unsigned long pdu_len,
903 void *pbuffer, unsigned long buf_len)
904{
905 struct iscsi_conn *conn = beiscsi_conn->conn;
906 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530907 struct iscsi_task *task;
908 struct beiscsi_io_task *io_task;
909 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530910
911 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
912 PDUBASE_OPCODE_MASK) {
913 case ISCSI_OP_NOOP_IN:
914 pbuffer = NULL;
915 buf_len = 0;
916 break;
917 case ISCSI_OP_ASYNC_EVENT:
918 break;
919 case ISCSI_OP_REJECT:
920 WARN_ON(!pbuffer);
921 WARN_ON(!(buf_len == 48));
922 SE_DEBUG(DBG_LVL_1, "In ISCSI_OP_REJECT\n");
923 break;
924 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +0530925 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530926 task = conn->login_task;
927 io_task = task->dd_data;
928 login_hdr = (struct iscsi_hdr *)ppdu;
929 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530930 break;
931 default:
932 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530933 "Unrecognized opcode 0x%x in async msg\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530934 (ppdu->
935 dw[offsetof(struct amap_pdu_base, opcode) / 32]
936 & PDUBASE_OPCODE_MASK));
937 return 1;
938 }
939
940 spin_lock_bh(&session->lock);
941 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
942 spin_unlock_bh(&session->lock);
943 return 0;
944}
945
946static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
947{
948 struct sgl_handle *psgl_handle;
949
950 if (phba->io_sgl_hndl_avbl) {
951 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530952 "In alloc_io_sgl_handle,io_sgl_alloc_index=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530953 phba->io_sgl_alloc_index);
954 psgl_handle = phba->io_sgl_hndl_base[phba->
955 io_sgl_alloc_index];
956 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
957 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530958 if (phba->io_sgl_alloc_index == (phba->params.
959 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530960 phba->io_sgl_alloc_index = 0;
961 else
962 phba->io_sgl_alloc_index++;
963 } else
964 psgl_handle = NULL;
965 return psgl_handle;
966}
967
968static void
969free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
970{
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530971 SE_DEBUG(DBG_LVL_8, "In free_,io_sgl_free_index=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530972 phba->io_sgl_free_index);
973 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
974 /*
975 * this can happen if clean_task is called on a task that
976 * failed in xmit_task or alloc_pdu.
977 */
978 SE_DEBUG(DBG_LVL_8,
979 "Double Free in IO SGL io_sgl_free_index=%d,"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530980 "value there=%p\n", phba->io_sgl_free_index,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530981 phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
982 return;
983 }
984 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
985 phba->io_sgl_hndl_avbl++;
986 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
987 phba->io_sgl_free_index = 0;
988 else
989 phba->io_sgl_free_index++;
990}
991
992/**
993 * alloc_wrb_handle - To allocate a wrb handle
994 * @phba: The hba pointer
995 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530996 *
997 * This happens under session_lock until submission to chip
998 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +0530999struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301000{
1001 struct hwi_wrb_context *pwrb_context;
1002 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301003 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301004
1005 phwi_ctrlr = phba->phwi_ctrlr;
1006 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301007 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301008 pwrb_handle = pwrb_context->pwrb_handle_base[
1009 pwrb_context->alloc_index];
1010 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301011 if (pwrb_context->alloc_index ==
1012 (phba->params.wrbs_per_cxn - 1))
1013 pwrb_context->alloc_index = 0;
1014 else
1015 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301016 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
1017 pwrb_context->alloc_index];
1018 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301019 } else
1020 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301021 return pwrb_handle;
1022}
1023
1024/**
1025 * free_wrb_handle - To free the wrb handle back to pool
1026 * @phba: The hba pointer
1027 * @pwrb_context: The context to free from
1028 * @pwrb_handle: The wrb_handle to free
1029 *
1030 * This happens under session_lock until submission to chip
1031 */
1032static void
1033free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1034 struct wrb_handle *pwrb_handle)
1035{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301036 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301037 pwrb_context->wrb_handles_available++;
1038 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
1039 pwrb_context->free_index = 0;
1040 else
1041 pwrb_context->free_index++;
1042
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301043 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301044 "FREE WRB: pwrb_handle=%p free_index=0x%x"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301045 "wrb_handles_available=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301046 pwrb_handle, pwrb_context->free_index,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301047 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301048}
1049
1050static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1051{
1052 struct sgl_handle *psgl_handle;
1053
1054 if (phba->eh_sgl_hndl_avbl) {
1055 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1056 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301057 SE_DEBUG(DBG_LVL_8, "mgmt_sgl_alloc_index=%d=0x%x\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301058 phba->eh_sgl_alloc_index, phba->eh_sgl_alloc_index);
1059 phba->eh_sgl_hndl_avbl--;
1060 if (phba->eh_sgl_alloc_index ==
1061 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1062 1))
1063 phba->eh_sgl_alloc_index = 0;
1064 else
1065 phba->eh_sgl_alloc_index++;
1066 } else
1067 psgl_handle = NULL;
1068 return psgl_handle;
1069}
1070
1071void
1072free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1073{
1074
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301075 SE_DEBUG(DBG_LVL_8, "In free_mgmt_sgl_handle,eh_sgl_free_index=%d\n",
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301076 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301077 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1078 /*
1079 * this can happen if clean_task is called on a task that
1080 * failed in xmit_task or alloc_pdu.
1081 */
1082 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301083 "Double Free in eh SGL ,eh_sgl_free_index=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301084 phba->eh_sgl_free_index);
1085 return;
1086 }
1087 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1088 phba->eh_sgl_hndl_avbl++;
1089 if (phba->eh_sgl_free_index ==
1090 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1091 phba->eh_sgl_free_index = 0;
1092 else
1093 phba->eh_sgl_free_index++;
1094}
1095
1096static void
1097be_complete_io(struct beiscsi_conn *beiscsi_conn,
1098 struct iscsi_task *task, struct sol_cqe *psol)
1099{
1100 struct beiscsi_io_task *io_task = task->dd_data;
1101 struct be_status_bhs *sts_bhs =
1102 (struct be_status_bhs *)io_task->cmd_bhs;
1103 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301104 unsigned char *sense;
1105 u32 resid = 0, exp_cmdsn, max_cmdsn;
1106 u8 rsp, status, flags;
1107
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301108 exp_cmdsn = (psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301109 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1110 & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301111 max_cmdsn = ((psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301112 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1113 & SOL_EXP_CMD_SN_MASK) +
1114 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1115 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1116 rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
1117 & SOL_RESP_MASK) >> 16);
1118 status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
1119 & SOL_STS_MASK) >> 8);
1120 flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1121 & SOL_FLAGS_MASK) >> 24) | 0x80;
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001122 if (!task->sc) {
1123 if (io_task->scsi_cmnd)
1124 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301125
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001126 return;
1127 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301128 task->sc->result = (DID_OK << 16) | status;
1129 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1130 task->sc->result = DID_ERROR << 16;
1131 goto unmap;
1132 }
1133
1134 /* bidi not initially supported */
1135 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
1136 resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
1137 32] & SOL_RES_CNT_MASK);
1138
1139 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1140 task->sc->result = DID_ERROR << 16;
1141
1142 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1143 scsi_set_resid(task->sc, resid);
1144 if (!status && (scsi_bufflen(task->sc) - resid <
1145 task->sc->underflow))
1146 task->sc->result = DID_ERROR << 16;
1147 }
1148 }
1149
1150 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001151 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301152 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001153
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301154 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001155 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301156 memcpy(task->sc->sense_buffer, sense,
1157 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1158 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301159
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301160 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
1161 if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1162 & SOL_RES_CNT_MASK)
1163 conn->rxdata_octets += (psol->
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301164 dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1165 & SOL_RES_CNT_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301166 }
1167unmap:
1168 scsi_dma_unmap(io_task->scsi_cmnd);
1169 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1170}
1171
1172static void
1173be_complete_logout(struct beiscsi_conn *beiscsi_conn,
1174 struct iscsi_task *task, struct sol_cqe *psol)
1175{
1176 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301177 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301178 struct iscsi_conn *conn = beiscsi_conn->conn;
1179
1180 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301181 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301182 hdr->t2wait = 5;
1183 hdr->t2retain = 0;
1184 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1185 & SOL_FLAGS_MASK) >> 24) | 0x80;
1186 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1187 32] & SOL_RESP_MASK);
1188 hdr->exp_cmdsn = cpu_to_be32(psol->
1189 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1190 & SOL_EXP_CMD_SN_MASK);
1191 hdr->max_cmdsn = be32_to_cpu((psol->
1192 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1193 & SOL_EXP_CMD_SN_MASK) +
1194 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1195 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301196 hdr->dlength[0] = 0;
1197 hdr->dlength[1] = 0;
1198 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301199 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301200 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301201 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1202}
1203
1204static void
1205be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
1206 struct iscsi_task *task, struct sol_cqe *psol)
1207{
1208 struct iscsi_tm_rsp *hdr;
1209 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301210 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301211
1212 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301213 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301214 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1215 & SOL_FLAGS_MASK) >> 24) | 0x80;
1216 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1217 32] & SOL_RESP_MASK);
1218 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301219 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301220 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1221 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1222 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1223 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301224 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301225 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1226}
1227
1228static void
1229hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1230 struct beiscsi_hba *phba, struct sol_cqe *psol)
1231{
1232 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301233 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301234 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301235 struct iscsi_task *task;
1236 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301237 struct iscsi_conn *conn = beiscsi_conn->conn;
1238 struct iscsi_session *session = conn->session;
1239
1240 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301241 pwrb_context = &phwi_ctrlr->wrb_context[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301242 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301243 SOL_CID_MASK) >> 6) -
1244 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301245 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301246 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1247 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301248 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301249
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301250 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001251 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301252 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001253 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301254 spin_lock_bh(&session->lock);
1255 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1256 spin_unlock_bh(&session->lock);
1257}
1258
1259static void
1260be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
1261 struct iscsi_task *task, struct sol_cqe *psol)
1262{
1263 struct iscsi_nopin *hdr;
1264 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301265 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301266
1267 hdr = (struct iscsi_nopin *)task->hdr;
1268 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1269 & SOL_FLAGS_MASK) >> 24) | 0x80;
1270 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
1271 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
1272 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1273 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1274 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1275 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1276 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301277 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301278 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1279}
1280
1281static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1282 struct beiscsi_hba *phba, struct sol_cqe *psol)
1283{
1284 struct hwi_wrb_context *pwrb_context;
1285 struct wrb_handle *pwrb_handle;
1286 struct iscsi_wrb *pwrb = NULL;
1287 struct hwi_controller *phwi_ctrlr;
1288 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301289 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301290 struct iscsi_conn *conn = beiscsi_conn->conn;
1291 struct iscsi_session *session = conn->session;
1292
1293 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301294 pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301295 (struct amap_sol_cqe, cid) / 32]
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301296 & SOL_CID_MASK) >> 6) -
1297 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301298 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301299 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1300 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301301 task = pwrb_handle->pio_handle;
1302 pwrb = pwrb_handle->pwrb;
1303 type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
1304 WRB_TYPE_MASK) >> 28;
1305
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301306 spin_lock_bh(&session->lock);
1307 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301308 case HWH_TYPE_IO:
1309 case HWH_TYPE_IO_RD:
1310 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301311 ISCSI_OP_NOOP_OUT)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301312 be_complete_nopin_resp(beiscsi_conn, task, psol);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301313 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301314 be_complete_io(beiscsi_conn, task, psol);
1315 break;
1316
1317 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301318 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
1319 be_complete_logout(beiscsi_conn, task, psol);
1320 else
1321 be_complete_tmf(beiscsi_conn, task, psol);
1322
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301323 break;
1324
1325 case HWH_TYPE_LOGIN:
1326 SE_DEBUG(DBG_LVL_1,
1327 "\t\t No HWH_TYPE_LOGIN Expected in hwi_complete_cmd"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301328 "- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301329 break;
1330
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301331 case HWH_TYPE_NOP:
1332 be_complete_nopin_resp(beiscsi_conn, task, psol);
1333 break;
1334
1335 default:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301336 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301337 "In hwi_complete_cmd, unknown type = %d"
1338 "wrb_index 0x%x CID 0x%x\n", type,
1339 ((psol->dw[offsetof(struct amap_iscsi_wrb,
1340 type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
1341 ((psol->dw[offsetof(struct amap_sol_cqe,
1342 cid) / 32] & SOL_CID_MASK) >> 6));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301343 break;
1344 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301345
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301346 spin_unlock_bh(&session->lock);
1347}
1348
1349static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1350 *pasync_ctx, unsigned int is_header,
1351 unsigned int host_write_ptr)
1352{
1353 if (is_header)
1354 return &pasync_ctx->async_entry[host_write_ptr].
1355 header_busy_list;
1356 else
1357 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1358}
1359
1360static struct async_pdu_handle *
1361hwi_get_async_handle(struct beiscsi_hba *phba,
1362 struct beiscsi_conn *beiscsi_conn,
1363 struct hwi_async_pdu_context *pasync_ctx,
1364 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1365{
1366 struct be_bus_address phys_addr;
1367 struct list_head *pbusy_list;
1368 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301369 unsigned char is_header = 0;
1370
1371 phys_addr.u.a32.address_lo =
1372 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
1373 ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1374 & PDUCQE_DPL_MASK) >> 16);
1375 phys_addr.u.a32.address_hi =
1376 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
1377
1378 phys_addr.u.a64.address =
1379 *((unsigned long long *)(&phys_addr.u.a64.address));
1380
1381 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1382 & PDUCQE_CODE_MASK) {
1383 case UNSOL_HDR_NOTIFY:
1384 is_header = 1;
1385
1386 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
1387 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1388 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301389 break;
1390 case UNSOL_DATA_NOTIFY:
1391 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
1392 dw[offsetof(struct amap_i_t_dpdu_cqe,
1393 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301394 break;
1395 default:
1396 pbusy_list = NULL;
1397 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301398 "Unexpected code=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301399 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1400 code) / 32] & PDUCQE_CODE_MASK);
1401 return NULL;
1402 }
1403
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301404 WARN_ON(list_empty(pbusy_list));
1405 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001406 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301407 break;
1408 }
1409
1410 WARN_ON(!pasync_handle);
1411
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301412 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1413 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301414 pasync_handle->is_header = is_header;
1415 pasync_handle->buffer_len = ((pdpdu_cqe->
1416 dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1417 & PDUCQE_DPL_MASK) >> 16);
1418
1419 *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1420 index) / 32] & PDUCQE_INDEX_MASK);
1421 return pasync_handle;
1422}
1423
1424static unsigned int
1425hwi_update_async_writables(struct hwi_async_pdu_context *pasync_ctx,
1426 unsigned int is_header, unsigned int cq_index)
1427{
1428 struct list_head *pbusy_list;
1429 struct async_pdu_handle *pasync_handle;
1430 unsigned int num_entries, writables = 0;
1431 unsigned int *pep_read_ptr, *pwritables;
1432
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001433 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301434 if (is_header) {
1435 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1436 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301437 } else {
1438 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1439 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301440 }
1441
1442 while ((*pep_read_ptr) != cq_index) {
1443 (*pep_read_ptr)++;
1444 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1445
1446 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1447 *pep_read_ptr);
1448 if (writables == 0)
1449 WARN_ON(list_empty(pbusy_list));
1450
1451 if (!list_empty(pbusy_list)) {
1452 pasync_handle = list_entry(pbusy_list->next,
1453 struct async_pdu_handle,
1454 link);
1455 WARN_ON(!pasync_handle);
1456 pasync_handle->consumed = 1;
1457 }
1458
1459 writables++;
1460 }
1461
1462 if (!writables) {
1463 SE_DEBUG(DBG_LVL_1,
1464 "Duplicate notification received - index 0x%x!!\n",
1465 cq_index);
1466 WARN_ON(1);
1467 }
1468
1469 *pwritables = *pwritables + writables;
1470 return 0;
1471}
1472
1473static unsigned int hwi_free_async_msg(struct beiscsi_hba *phba,
1474 unsigned int cri)
1475{
1476 struct hwi_controller *phwi_ctrlr;
1477 struct hwi_async_pdu_context *pasync_ctx;
1478 struct async_pdu_handle *pasync_handle, *tmp_handle;
1479 struct list_head *plist;
1480 unsigned int i = 0;
1481
1482 phwi_ctrlr = phba->phwi_ctrlr;
1483 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1484
1485 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1486
1487 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1488 list_del(&pasync_handle->link);
1489
1490 if (i == 0) {
1491 list_add_tail(&pasync_handle->link,
1492 &pasync_ctx->async_header.free_list);
1493 pasync_ctx->async_header.free_entries++;
1494 i++;
1495 } else {
1496 list_add_tail(&pasync_handle->link,
1497 &pasync_ctx->async_data.free_list);
1498 pasync_ctx->async_data.free_entries++;
1499 i++;
1500 }
1501 }
1502
1503 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1504 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1505 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1506 return 0;
1507}
1508
1509static struct phys_addr *
1510hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1511 unsigned int is_header, unsigned int host_write_ptr)
1512{
1513 struct phys_addr *pasync_sge = NULL;
1514
1515 if (is_header)
1516 pasync_sge = pasync_ctx->async_header.ring_base;
1517 else
1518 pasync_sge = pasync_ctx->async_data.ring_base;
1519
1520 return pasync_sge + host_write_ptr;
1521}
1522
1523static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1524 unsigned int is_header)
1525{
1526 struct hwi_controller *phwi_ctrlr;
1527 struct hwi_async_pdu_context *pasync_ctx;
1528 struct async_pdu_handle *pasync_handle;
1529 struct list_head *pfree_link, *pbusy_list;
1530 struct phys_addr *pasync_sge;
1531 unsigned int ring_id, num_entries;
1532 unsigned int host_write_num;
1533 unsigned int writables;
1534 unsigned int i = 0;
1535 u32 doorbell = 0;
1536
1537 phwi_ctrlr = phba->phwi_ctrlr;
1538 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001539 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301540
1541 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301542 writables = min(pasync_ctx->async_header.writables,
1543 pasync_ctx->async_header.free_entries);
1544 pfree_link = pasync_ctx->async_header.free_list.next;
1545 host_write_num = pasync_ctx->async_header.host_write_ptr;
1546 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1547 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301548 writables = min(pasync_ctx->async_data.writables,
1549 pasync_ctx->async_data.free_entries);
1550 pfree_link = pasync_ctx->async_data.free_list.next;
1551 host_write_num = pasync_ctx->async_data.host_write_ptr;
1552 ring_id = phwi_ctrlr->default_pdu_data.id;
1553 }
1554
1555 writables = (writables / 8) * 8;
1556 if (writables) {
1557 for (i = 0; i < writables; i++) {
1558 pbusy_list =
1559 hwi_get_async_busy_list(pasync_ctx, is_header,
1560 host_write_num);
1561 pasync_handle =
1562 list_entry(pfree_link, struct async_pdu_handle,
1563 link);
1564 WARN_ON(!pasync_handle);
1565 pasync_handle->consumed = 0;
1566
1567 pfree_link = pfree_link->next;
1568
1569 pasync_sge = hwi_get_ring_address(pasync_ctx,
1570 is_header, host_write_num);
1571
1572 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1573 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1574
1575 list_move(&pasync_handle->link, pbusy_list);
1576
1577 host_write_num++;
1578 host_write_num = host_write_num % num_entries;
1579 }
1580
1581 if (is_header) {
1582 pasync_ctx->async_header.host_write_ptr =
1583 host_write_num;
1584 pasync_ctx->async_header.free_entries -= writables;
1585 pasync_ctx->async_header.writables -= writables;
1586 pasync_ctx->async_header.busy_entries += writables;
1587 } else {
1588 pasync_ctx->async_data.host_write_ptr = host_write_num;
1589 pasync_ctx->async_data.free_entries -= writables;
1590 pasync_ctx->async_data.writables -= writables;
1591 pasync_ctx->async_data.busy_entries += writables;
1592 }
1593
1594 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1595 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1596 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1597 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1598 << DB_DEF_PDU_CQPROC_SHIFT;
1599
1600 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1601 }
1602}
1603
1604static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1605 struct beiscsi_conn *beiscsi_conn,
1606 struct i_t_dpdu_cqe *pdpdu_cqe)
1607{
1608 struct hwi_controller *phwi_ctrlr;
1609 struct hwi_async_pdu_context *pasync_ctx;
1610 struct async_pdu_handle *pasync_handle = NULL;
1611 unsigned int cq_index = -1;
1612
1613 phwi_ctrlr = phba->phwi_ctrlr;
1614 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1615
1616 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1617 pdpdu_cqe, &cq_index);
1618 BUG_ON(pasync_handle->is_header != 0);
1619 if (pasync_handle->consumed == 0)
1620 hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
1621 cq_index);
1622
1623 hwi_free_async_msg(phba, pasync_handle->cri);
1624 hwi_post_async_buffers(phba, pasync_handle->is_header);
1625}
1626
1627static unsigned int
1628hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1629 struct beiscsi_hba *phba,
1630 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1631{
1632 struct list_head *plist;
1633 struct async_pdu_handle *pasync_handle;
1634 void *phdr = NULL;
1635 unsigned int hdr_len = 0, buf_len = 0;
1636 unsigned int status, index = 0, offset = 0;
1637 void *pfirst_buffer = NULL;
1638 unsigned int num_buf = 0;
1639
1640 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1641
1642 list_for_each_entry(pasync_handle, plist, link) {
1643 if (index == 0) {
1644 phdr = pasync_handle->pbuffer;
1645 hdr_len = pasync_handle->buffer_len;
1646 } else {
1647 buf_len = pasync_handle->buffer_len;
1648 if (!num_buf) {
1649 pfirst_buffer = pasync_handle->pbuffer;
1650 num_buf++;
1651 }
1652 memcpy(pfirst_buffer + offset,
1653 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001654 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301655 }
1656 index++;
1657 }
1658
1659 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301660 (beiscsi_conn->beiscsi_conn_cid -
1661 phba->fw_config.iscsi_cid_start),
1662 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001663 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301664
1665 if (status == 0)
1666 hwi_free_async_msg(phba, cri);
1667 return 0;
1668}
1669
1670static unsigned int
1671hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1672 struct beiscsi_hba *phba,
1673 struct async_pdu_handle *pasync_handle)
1674{
1675 struct hwi_async_pdu_context *pasync_ctx;
1676 struct hwi_controller *phwi_ctrlr;
1677 unsigned int bytes_needed = 0, status = 0;
1678 unsigned short cri = pasync_handle->cri;
1679 struct pdu_base *ppdu;
1680
1681 phwi_ctrlr = phba->phwi_ctrlr;
1682 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1683
1684 list_del(&pasync_handle->link);
1685 if (pasync_handle->is_header) {
1686 pasync_ctx->async_header.busy_entries--;
1687 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1688 hwi_free_async_msg(phba, cri);
1689 BUG();
1690 }
1691
1692 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1693 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1694 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1695 (unsigned short)pasync_handle->buffer_len;
1696 list_add_tail(&pasync_handle->link,
1697 &pasync_ctx->async_entry[cri].wait_queue.list);
1698
1699 ppdu = pasync_handle->pbuffer;
1700 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1701 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1702 0xFFFF0000) | ((be16_to_cpu((ppdu->
1703 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1704 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1705
1706 if (status == 0) {
1707 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1708 bytes_needed;
1709
1710 if (bytes_needed == 0)
1711 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1712 pasync_ctx, cri);
1713 }
1714 } else {
1715 pasync_ctx->async_data.busy_entries--;
1716 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1717 list_add_tail(&pasync_handle->link,
1718 &pasync_ctx->async_entry[cri].wait_queue.
1719 list);
1720 pasync_ctx->async_entry[cri].wait_queue.
1721 bytes_received +=
1722 (unsigned short)pasync_handle->buffer_len;
1723
1724 if (pasync_ctx->async_entry[cri].wait_queue.
1725 bytes_received >=
1726 pasync_ctx->async_entry[cri].wait_queue.
1727 bytes_needed)
1728 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1729 pasync_ctx, cri);
1730 }
1731 }
1732 return status;
1733}
1734
1735static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1736 struct beiscsi_hba *phba,
1737 struct i_t_dpdu_cqe *pdpdu_cqe)
1738{
1739 struct hwi_controller *phwi_ctrlr;
1740 struct hwi_async_pdu_context *pasync_ctx;
1741 struct async_pdu_handle *pasync_handle = NULL;
1742 unsigned int cq_index = -1;
1743
1744 phwi_ctrlr = phba->phwi_ctrlr;
1745 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1746 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1747 pdpdu_cqe, &cq_index);
1748
1749 if (pasync_handle->consumed == 0)
1750 hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
1751 cq_index);
1752 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1753 hwi_post_async_buffers(phba, pasync_handle->is_header);
1754}
1755
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301756static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1757{
1758 struct be_queue_info *mcc_cq;
1759 struct be_mcc_compl *mcc_compl;
1760 unsigned int num_processed = 0;
1761
1762 mcc_cq = &phba->ctrl.mcc_obj.cq;
1763 mcc_compl = queue_tail_node(mcc_cq);
1764 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1765 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1766
1767 if (num_processed >= 32) {
1768 hwi_ring_cq_db(phba, mcc_cq->id,
1769 num_processed, 0, 0);
1770 num_processed = 0;
1771 }
1772 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1773 /* Interpret flags as an async trailer */
1774 if (is_link_state_evt(mcc_compl->flags))
1775 /* Interpret compl as a async link evt */
1776 beiscsi_async_link_state_process(phba,
1777 (struct be_async_event_link_state *) mcc_compl);
1778 else
1779 SE_DEBUG(DBG_LVL_1,
1780 " Unsupported Async Event, flags"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301781 " = 0x%08x\n", mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301782 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1783 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1784 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1785 }
1786
1787 mcc_compl->flags = 0;
1788 queue_tail_inc(mcc_cq);
1789 mcc_compl = queue_tail_node(mcc_cq);
1790 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1791 num_processed++;
1792 }
1793
1794 if (num_processed > 0)
1795 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1796
1797}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301798
1799static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301800{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301801 struct be_queue_info *cq;
1802 struct sol_cqe *sol;
1803 struct dmsg_cqe *dmsg;
1804 unsigned int num_processed = 0;
1805 unsigned int tot_nump = 0;
1806 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301807 struct beiscsi_endpoint *beiscsi_ep;
1808 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301809 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301810
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301811 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301812 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301813 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301814
1815 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1816 CQE_VALID_MASK) {
1817 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1818
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301819 ep = phba->ep_array[(u32) ((sol->
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301820 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
1821 SOL_CID_MASK) >> 6) -
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301822 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301823
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301824 beiscsi_ep = ep->dd_data;
1825 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301826
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301827 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301828 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301829 num_processed, 0, 0);
1830 tot_nump += num_processed;
1831 num_processed = 0;
1832 }
1833
1834 switch ((u32) sol->dw[offsetof(struct amap_sol_cqe, code) /
1835 32] & CQE_CODE_MASK) {
1836 case SOL_CMD_COMPLETE:
1837 hwi_complete_cmd(beiscsi_conn, phba, sol);
1838 break;
1839 case DRIVERMSG_NOTIFY:
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301840 SE_DEBUG(DBG_LVL_8, "Received DRIVERMSG_NOTIFY\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301841 dmsg = (struct dmsg_cqe *)sol;
1842 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1843 break;
1844 case UNSOL_HDR_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301845 SE_DEBUG(DBG_LVL_8, "Received UNSOL_HDR_ NOTIFY\n");
1846 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1847 (struct i_t_dpdu_cqe *)sol);
1848 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301849 case UNSOL_DATA_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301850 SE_DEBUG(DBG_LVL_8, "Received UNSOL_DATA_NOTIFY\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301851 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1852 (struct i_t_dpdu_cqe *)sol);
1853 break;
1854 case CXN_INVALIDATE_INDEX_NOTIFY:
1855 case CMD_INVALIDATED_NOTIFY:
1856 case CXN_INVALIDATE_NOTIFY:
1857 SE_DEBUG(DBG_LVL_1,
1858 "Ignoring CQ Error notification for cmd/cxn"
1859 "invalidate\n");
1860 break;
1861 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
1862 case CMD_KILLED_INVALID_STATSN_RCVD:
1863 case CMD_KILLED_INVALID_R2T_RCVD:
1864 case CMD_CXN_KILLED_LUN_INVALID:
1865 case CMD_CXN_KILLED_ICD_INVALID:
1866 case CMD_CXN_KILLED_ITT_INVALID:
1867 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1868 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301869 SE_DEBUG(DBG_LVL_1,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301870 "CQ Error notification for cmd.. "
1871 "code %d cid 0x%x\n",
1872 sol->dw[offsetof(struct amap_sol_cqe, code) /
1873 32] & CQE_CODE_MASK,
1874 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1875 32] & SOL_CID_MASK));
1876 break;
1877 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1878 SE_DEBUG(DBG_LVL_1,
1879 "Digest error on def pdu ring, dropping..\n");
1880 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
1881 (struct i_t_dpdu_cqe *) sol);
1882 break;
1883 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
1884 case CXN_KILLED_BURST_LEN_MISMATCH:
1885 case CXN_KILLED_AHS_RCVD:
1886 case CXN_KILLED_HDR_DIGEST_ERR:
1887 case CXN_KILLED_UNKNOWN_HDR:
1888 case CXN_KILLED_STALE_ITT_TTT_RCVD:
1889 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
1890 case CXN_KILLED_TIMED_OUT:
1891 case CXN_KILLED_FIN_RCVD:
1892 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
1893 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
1894 case CXN_KILLED_OVER_RUN_RESIDUAL:
1895 case CXN_KILLED_UNDER_RUN_RESIDUAL:
1896 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301897 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset CID "
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301898 "0x%x...\n",
1899 sol->dw[offsetof(struct amap_sol_cqe, code) /
1900 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301901 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1902 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301903 iscsi_conn_failure(beiscsi_conn->conn,
1904 ISCSI_ERR_CONN_FAILED);
1905 break;
1906 case CXN_KILLED_RST_SENT:
1907 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301908 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset"
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301909 "received/sent on CID 0x%x...\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301910 sol->dw[offsetof(struct amap_sol_cqe, code) /
1911 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301912 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1913 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301914 iscsi_conn_failure(beiscsi_conn->conn,
1915 ISCSI_ERR_CONN_FAILED);
1916 break;
1917 default:
1918 SE_DEBUG(DBG_LVL_1, "CQ Error Invalid code= %d "
1919 "received on CID 0x%x...\n",
1920 sol->dw[offsetof(struct amap_sol_cqe, code) /
1921 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301922 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1923 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301924 break;
1925 }
1926
1927 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
1928 queue_tail_inc(cq);
1929 sol = queue_tail_node(cq);
1930 num_processed++;
1931 }
1932
1933 if (num_processed > 0) {
1934 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301935 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301936 }
1937 return tot_nump;
1938}
1939
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301940void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301941{
1942 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301943 struct hwi_controller *phwi_ctrlr;
1944 struct hwi_context_memory *phwi_context;
1945 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301946 struct beiscsi_hba *phba =
1947 container_of(work, struct beiscsi_hba, work_cqs);
1948
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301949 phwi_ctrlr = phba->phwi_ctrlr;
1950 phwi_context = phwi_ctrlr->phwi_ctxt;
1951 if (phba->msix_enabled)
1952 pbe_eq = &phwi_context->be_eq[phba->num_cpus];
1953 else
1954 pbe_eq = &phwi_context->be_eq[0];
1955
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301956 if (phba->todo_mcc_cq) {
1957 spin_lock_irqsave(&phba->isr_lock, flags);
1958 phba->todo_mcc_cq = 0;
1959 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301960 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301961 }
1962
1963 if (phba->todo_cq) {
1964 spin_lock_irqsave(&phba->isr_lock, flags);
1965 phba->todo_cq = 0;
1966 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301967 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301968 }
1969}
1970
1971static int be_iopoll(struct blk_iopoll *iop, int budget)
1972{
1973 static unsigned int ret;
1974 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301975 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301976
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301977 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
1978 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301979 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301980 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301981 blk_iopoll_complete(iop);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301982 SE_DEBUG(DBG_LVL_8, "rearm pbe_eq->q.id =%d\n", pbe_eq->q.id);
1983 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301984 }
1985 return ret;
1986}
1987
1988static void
1989hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
1990 unsigned int num_sg, struct beiscsi_io_task *io_task)
1991{
1992 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05301993 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301994 unsigned int sge_len = 0;
1995 unsigned long long addr;
1996 struct scatterlist *l_sg;
1997 unsigned int offset;
1998
1999 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2000 io_task->bhs_pa.u.a32.address_lo);
2001 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2002 io_task->bhs_pa.u.a32.address_hi);
2003
2004 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302005 for (index = 0; (index < num_sg) && (index < 2); index++,
2006 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302007 if (index == 0) {
2008 sg_len = sg_dma_len(sg);
2009 addr = (u64) sg_dma_address(sg);
2010 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302011 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302012 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302013 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302014 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2015 sg_len);
2016 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302017 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302018 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2019 pwrb, sge_len);
2020 sg_len = sg_dma_len(sg);
2021 addr = (u64) sg_dma_address(sg);
2022 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302023 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302024 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302025 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302026 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2027 sg_len);
2028 }
2029 }
2030 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2031 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2032
2033 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2034
2035 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2036 io_task->bhs_pa.u.a32.address_hi);
2037 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2038 io_task->bhs_pa.u.a32.address_lo);
2039
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302040 if (num_sg == 1) {
2041 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2042 1);
2043 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2044 0);
2045 } else if (num_sg == 2) {
2046 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2047 0);
2048 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2049 1);
2050 } else {
2051 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2052 0);
2053 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2054 0);
2055 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302056 sg = l_sg;
2057 psgl++;
2058 psgl++;
2059 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302060 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302061 sg_len = sg_dma_len(sg);
2062 addr = (u64) sg_dma_address(sg);
2063 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2064 (addr & 0xFFFFFFFF));
2065 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2066 (addr >> 32));
2067 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2068 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2069 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2070 offset += sg_len;
2071 }
2072 psgl--;
2073 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2074}
2075
2076static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2077{
2078 struct iscsi_sge *psgl;
2079 unsigned long long addr;
2080 struct beiscsi_io_task *io_task = task->dd_data;
2081 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2082 struct beiscsi_hba *phba = beiscsi_conn->phba;
2083
2084 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2085 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2086 io_task->bhs_pa.u.a32.address_lo);
2087 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2088 io_task->bhs_pa.u.a32.address_hi);
2089
2090 if (task->data) {
2091 if (task->data_count) {
2092 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
2093 addr = (u64) pci_map_single(phba->pcidev,
2094 task->data,
2095 task->data_count, 1);
2096 } else {
2097 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2098 addr = 0;
2099 }
2100 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302101 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302102 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302103 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302104 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2105 task->data_count);
2106
2107 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2108 } else {
2109 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2110 addr = 0;
2111 }
2112
2113 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2114
2115 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2116
2117 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2118 io_task->bhs_pa.u.a32.address_hi);
2119 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2120 io_task->bhs_pa.u.a32.address_lo);
2121 if (task->data) {
2122 psgl++;
2123 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2124 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2125 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2126 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2127 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2128 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2129
2130 psgl++;
2131 if (task->data) {
2132 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302133 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302134 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302135 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302136 }
2137 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2138 }
2139 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2140}
2141
2142static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2143{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302144 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302145 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2146 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2147
2148 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2149 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302150 num_async_pdu_buf_pages =
2151 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2152 phba->params.defpdu_hdr_sz);
2153 num_async_pdu_buf_sgl_pages =
2154 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2155 sizeof(struct phys_addr));
2156 num_async_pdu_data_pages =
2157 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2158 phba->params.defpdu_data_sz);
2159 num_async_pdu_data_sgl_pages =
2160 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2161 sizeof(struct phys_addr));
2162
2163 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2164
2165 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2166 BE_ISCSI_PDU_HEADER_SIZE;
2167 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2168 sizeof(struct hwi_context_memory);
2169
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302170
2171 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2172 * (phba->params.wrbs_per_cxn)
2173 * phba->params.cxns_per_ctrl;
2174 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2175 (phba->params.wrbs_per_cxn);
2176 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2177 phba->params.cxns_per_ctrl);
2178
2179 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2180 phba->params.icds_per_ctrl;
2181 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2182 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2183
2184 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2185 num_async_pdu_buf_pages * PAGE_SIZE;
2186 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2187 num_async_pdu_data_pages * PAGE_SIZE;
2188 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2189 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2190 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2191 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2192 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2193 phba->params.asyncpdus_per_ctrl *
2194 sizeof(struct async_pdu_handle);
2195 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2196 phba->params.asyncpdus_per_ctrl *
2197 sizeof(struct async_pdu_handle);
2198 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2199 sizeof(struct hwi_async_pdu_context) +
2200 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2201}
2202
2203static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2204{
2205 struct be_mem_descriptor *mem_descr;
2206 dma_addr_t bus_add;
2207 struct mem_array *mem_arr, *mem_arr_orig;
2208 unsigned int i, j, alloc_size, curr_alloc_size;
2209
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002210 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302211 if (!phba->phwi_ctrlr)
2212 return -ENOMEM;
2213
2214 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2215 GFP_KERNEL);
2216 if (!phba->init_mem) {
2217 kfree(phba->phwi_ctrlr);
2218 return -ENOMEM;
2219 }
2220
2221 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2222 GFP_KERNEL);
2223 if (!mem_arr_orig) {
2224 kfree(phba->init_mem);
2225 kfree(phba->phwi_ctrlr);
2226 return -ENOMEM;
2227 }
2228
2229 mem_descr = phba->init_mem;
2230 for (i = 0; i < SE_MEM_MAX; i++) {
2231 j = 0;
2232 mem_arr = mem_arr_orig;
2233 alloc_size = phba->mem_req[i];
2234 memset(mem_arr, 0, sizeof(struct mem_array) *
2235 BEISCSI_MAX_FRAGS_INIT);
2236 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2237 do {
2238 mem_arr->virtual_address = pci_alloc_consistent(
2239 phba->pcidev,
2240 curr_alloc_size,
2241 &bus_add);
2242 if (!mem_arr->virtual_address) {
2243 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2244 goto free_mem;
2245 if (curr_alloc_size -
2246 rounddown_pow_of_two(curr_alloc_size))
2247 curr_alloc_size = rounddown_pow_of_two
2248 (curr_alloc_size);
2249 else
2250 curr_alloc_size = curr_alloc_size / 2;
2251 } else {
2252 mem_arr->bus_address.u.
2253 a64.address = (__u64) bus_add;
2254 mem_arr->size = curr_alloc_size;
2255 alloc_size -= curr_alloc_size;
2256 curr_alloc_size = min(be_max_phys_size *
2257 1024, alloc_size);
2258 j++;
2259 mem_arr++;
2260 }
2261 } while (alloc_size);
2262 mem_descr->num_elements = j;
2263 mem_descr->size_in_bytes = phba->mem_req[i];
2264 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2265 GFP_KERNEL);
2266 if (!mem_descr->mem_array)
2267 goto free_mem;
2268
2269 memcpy(mem_descr->mem_array, mem_arr_orig,
2270 sizeof(struct mem_array) * j);
2271 mem_descr++;
2272 }
2273 kfree(mem_arr_orig);
2274 return 0;
2275free_mem:
2276 mem_descr->num_elements = j;
2277 while ((i) || (j)) {
2278 for (j = mem_descr->num_elements; j > 0; j--) {
2279 pci_free_consistent(phba->pcidev,
2280 mem_descr->mem_array[j - 1].size,
2281 mem_descr->mem_array[j - 1].
2282 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302283 (unsigned long)mem_descr->
2284 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302285 bus_address.u.a64.address);
2286 }
2287 if (i) {
2288 i--;
2289 kfree(mem_descr->mem_array);
2290 mem_descr--;
2291 }
2292 }
2293 kfree(mem_arr_orig);
2294 kfree(phba->init_mem);
2295 kfree(phba->phwi_ctrlr);
2296 return -ENOMEM;
2297}
2298
2299static int beiscsi_get_memory(struct beiscsi_hba *phba)
2300{
2301 beiscsi_find_mem_req(phba);
2302 return beiscsi_alloc_mem(phba);
2303}
2304
2305static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2306{
2307 struct pdu_data_out *pdata_out;
2308 struct pdu_nop_out *pnop_out;
2309 struct be_mem_descriptor *mem_descr;
2310
2311 mem_descr = phba->init_mem;
2312 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2313 pdata_out =
2314 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2315 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2316
2317 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2318 IIOC_SCSI_DATA);
2319
2320 pnop_out =
2321 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2322 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2323
2324 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2325 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2326 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2327 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2328}
2329
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002330static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302331{
2332 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002333 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302334 struct hwi_controller *phwi_ctrlr;
2335 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002336 struct iscsi_wrb *pwrb = NULL;
2337 unsigned int num_cxn_wrbh = 0;
2338 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302339
2340 mem_descr_wrbh = phba->init_mem;
2341 mem_descr_wrbh += HWI_MEM_WRBH;
2342
2343 mem_descr_wrb = phba->init_mem;
2344 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302345 phwi_ctrlr = phba->phwi_ctrlr;
2346
2347 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2348 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302349 pwrb_context->pwrb_handle_base =
2350 kzalloc(sizeof(struct wrb_handle *) *
2351 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002352 if (!pwrb_context->pwrb_handle_base) {
2353 shost_printk(KERN_ERR, phba->shost,
2354 "Mem Alloc Failed. Failing to load\n");
2355 goto init_wrb_hndl_failed;
2356 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302357 pwrb_context->pwrb_handle_basestd =
2358 kzalloc(sizeof(struct wrb_handle *) *
2359 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002360 if (!pwrb_context->pwrb_handle_basestd) {
2361 shost_printk(KERN_ERR, phba->shost,
2362 "Mem Alloc Failed. Failing to load\n");
2363 goto init_wrb_hndl_failed;
2364 }
2365 if (!num_cxn_wrbh) {
2366 pwrb_handle =
2367 mem_descr_wrbh->mem_array[idx].virtual_address;
2368 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2369 ((sizeof(struct wrb_handle)) *
2370 phba->params.wrbs_per_cxn));
2371 idx++;
2372 }
2373 pwrb_context->alloc_index = 0;
2374 pwrb_context->wrb_handles_available = 0;
2375 pwrb_context->free_index = 0;
2376
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302377 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302378 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2379 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2380 pwrb_context->pwrb_handle_basestd[j] =
2381 pwrb_handle;
2382 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302383 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302384 pwrb_handle++;
2385 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302386 num_cxn_wrbh--;
2387 }
2388 }
2389 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302390 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302391 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002392 if (!num_cxn_wrb) {
2393 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2394 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2395 ((sizeof(struct iscsi_wrb) *
2396 phba->params.wrbs_per_cxn));
2397 idx++;
2398 }
2399
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302400 if (num_cxn_wrb) {
2401 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2402 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2403 pwrb_handle->pwrb = pwrb;
2404 pwrb++;
2405 }
2406 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302407 }
2408 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002409 return 0;
2410init_wrb_hndl_failed:
2411 for (j = index; j > 0; j--) {
2412 pwrb_context = &phwi_ctrlr->wrb_context[j];
2413 kfree(pwrb_context->pwrb_handle_base);
2414 kfree(pwrb_context->pwrb_handle_basestd);
2415 }
2416 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302417}
2418
2419static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2420{
2421 struct hwi_controller *phwi_ctrlr;
2422 struct hba_parameters *p = &phba->params;
2423 struct hwi_async_pdu_context *pasync_ctx;
2424 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002425 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302426 struct be_mem_descriptor *mem_descr;
2427
2428 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2429 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2430
2431 phwi_ctrlr = phba->phwi_ctrlr;
2432 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2433 mem_descr->mem_array[0].virtual_address;
2434 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2435 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2436
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002437 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2438 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302439
2440 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2441 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2442 if (mem_descr->mem_array[0].virtual_address) {
2443 SE_DEBUG(DBG_LVL_8,
2444 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_BUF"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302445 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302446 } else
2447 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302448 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302449
2450 pasync_ctx->async_header.va_base =
2451 mem_descr->mem_array[0].virtual_address;
2452
2453 pasync_ctx->async_header.pa_base.u.a64.address =
2454 mem_descr->mem_array[0].bus_address.u.a64.address;
2455
2456 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2457 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2458 if (mem_descr->mem_array[0].virtual_address) {
2459 SE_DEBUG(DBG_LVL_8,
2460 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_RING"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302461 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302462 } else
2463 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302464 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302465 pasync_ctx->async_header.ring_base =
2466 mem_descr->mem_array[0].virtual_address;
2467
2468 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2469 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2470 if (mem_descr->mem_array[0].virtual_address) {
2471 SE_DEBUG(DBG_LVL_8,
2472 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_HANDLE"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302473 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302474 } else
2475 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302476 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302477
2478 pasync_ctx->async_header.handle_base =
2479 mem_descr->mem_array[0].virtual_address;
2480 pasync_ctx->async_header.writables = 0;
2481 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2482
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302483
2484 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2485 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2486 if (mem_descr->mem_array[0].virtual_address) {
2487 SE_DEBUG(DBG_LVL_8,
2488 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_RING"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302489 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302490 } else
2491 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302492 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302493
2494 pasync_ctx->async_data.ring_base =
2495 mem_descr->mem_array[0].virtual_address;
2496
2497 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2498 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2499 if (!mem_descr->mem_array[0].virtual_address)
2500 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302501 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302502
2503 pasync_ctx->async_data.handle_base =
2504 mem_descr->mem_array[0].virtual_address;
2505 pasync_ctx->async_data.writables = 0;
2506 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2507
2508 pasync_header_h =
2509 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2510 pasync_data_h =
2511 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2512
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002513 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2514 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2515 if (mem_descr->mem_array[0].virtual_address) {
2516 SE_DEBUG(DBG_LVL_8,
2517 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_BUF"
2518 "va=%p\n", mem_descr->mem_array[0].virtual_address);
2519 } else
2520 shost_printk(KERN_WARNING, phba->shost,
2521 "No Virtual address\n");
2522 idx = 0;
2523 pasync_ctx->async_data.va_base =
2524 mem_descr->mem_array[idx].virtual_address;
2525 pasync_ctx->async_data.pa_base.u.a64.address =
2526 mem_descr->mem_array[idx].bus_address.u.a64.address;
2527
2528 num_async_data = ((mem_descr->mem_array[idx].size) /
2529 phba->params.defpdu_data_sz);
2530 num_per_mem = 0;
2531
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302532 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2533 pasync_header_h->cri = -1;
2534 pasync_header_h->index = (char)index;
2535 INIT_LIST_HEAD(&pasync_header_h->link);
2536 pasync_header_h->pbuffer =
2537 (void *)((unsigned long)
2538 (pasync_ctx->async_header.va_base) +
2539 (p->defpdu_hdr_sz * index));
2540
2541 pasync_header_h->pa.u.a64.address =
2542 pasync_ctx->async_header.pa_base.u.a64.address +
2543 (p->defpdu_hdr_sz * index);
2544
2545 list_add_tail(&pasync_header_h->link,
2546 &pasync_ctx->async_header.free_list);
2547 pasync_header_h++;
2548 pasync_ctx->async_header.free_entries++;
2549 pasync_ctx->async_header.writables++;
2550
2551 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2552 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2553 header_busy_list);
2554 pasync_data_h->cri = -1;
2555 pasync_data_h->index = (char)index;
2556 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002557
2558 if (!num_async_data) {
2559 num_per_mem = 0;
2560 idx++;
2561 pasync_ctx->async_data.va_base =
2562 mem_descr->mem_array[idx].virtual_address;
2563 pasync_ctx->async_data.pa_base.u.a64.address =
2564 mem_descr->mem_array[idx].
2565 bus_address.u.a64.address;
2566
2567 num_async_data = ((mem_descr->mem_array[idx].size) /
2568 phba->params.defpdu_data_sz);
2569 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302570 pasync_data_h->pbuffer =
2571 (void *)((unsigned long)
2572 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002573 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302574
2575 pasync_data_h->pa.u.a64.address =
2576 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002577 (p->defpdu_data_sz * num_per_mem);
2578 num_per_mem++;
2579 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302580
2581 list_add_tail(&pasync_data_h->link,
2582 &pasync_ctx->async_data.free_list);
2583 pasync_data_h++;
2584 pasync_ctx->async_data.free_entries++;
2585 pasync_ctx->async_data.writables++;
2586
2587 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2588 }
2589
2590 pasync_ctx->async_header.host_write_ptr = 0;
2591 pasync_ctx->async_header.ep_read_ptr = -1;
2592 pasync_ctx->async_data.host_write_ptr = 0;
2593 pasync_ctx->async_data.ep_read_ptr = -1;
2594}
2595
2596static int
2597be_sgl_create_contiguous(void *virtual_address,
2598 u64 physical_address, u32 length,
2599 struct be_dma_mem *sgl)
2600{
2601 WARN_ON(!virtual_address);
2602 WARN_ON(!physical_address);
2603 WARN_ON(!length > 0);
2604 WARN_ON(!sgl);
2605
2606 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302607 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302608 sgl->size = length;
2609
2610 return 0;
2611}
2612
2613static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2614{
2615 memset(sgl, 0, sizeof(*sgl));
2616}
2617
2618static void
2619hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2620 struct mem_array *pmem, struct be_dma_mem *sgl)
2621{
2622 if (sgl->va)
2623 be_sgl_destroy_contiguous(sgl);
2624
2625 be_sgl_create_contiguous(pmem->virtual_address,
2626 pmem->bus_address.u.a64.address,
2627 pmem->size, sgl);
2628}
2629
2630static void
2631hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2632 struct mem_array *pmem, struct be_dma_mem *sgl)
2633{
2634 if (sgl->va)
2635 be_sgl_destroy_contiguous(sgl);
2636
2637 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2638 pmem->bus_address.u.a64.address,
2639 pmem->size, sgl);
2640}
2641
2642static int be_fill_queue(struct be_queue_info *q,
2643 u16 len, u16 entry_size, void *vaddress)
2644{
2645 struct be_dma_mem *mem = &q->dma_mem;
2646
2647 memset(q, 0, sizeof(*q));
2648 q->len = len;
2649 q->entry_size = entry_size;
2650 mem->size = len * entry_size;
2651 mem->va = vaddress;
2652 if (!mem->va)
2653 return -ENOMEM;
2654 memset(mem->va, 0, mem->size);
2655 return 0;
2656}
2657
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302658static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302659 struct hwi_context_memory *phwi_context)
2660{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302661 unsigned int i, num_eq_pages;
2662 int ret, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302663 struct be_queue_info *eq;
2664 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302665 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302666 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302667
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302668 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
2669 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302670
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302671 if (phba->msix_enabled)
2672 eq_for_mcc = 1;
2673 else
2674 eq_for_mcc = 0;
2675 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
2676 eq = &phwi_context->be_eq[i].q;
2677 mem = &eq->dma_mem;
2678 phwi_context->be_eq[i].phba = phba;
2679 eq_vaddress = pci_alloc_consistent(phba->pcidev,
2680 num_eq_pages * PAGE_SIZE,
2681 &paddr);
2682 if (!eq_vaddress)
2683 goto create_eq_error;
2684
2685 mem->va = eq_vaddress;
2686 ret = be_fill_queue(eq, phba->params.num_eq_entries,
2687 sizeof(struct be_eq_entry), eq_vaddress);
2688 if (ret) {
2689 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302690 "be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302691 goto create_eq_error;
2692 }
2693
2694 mem->dma = paddr;
2695 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
2696 phwi_context->cur_eqd);
2697 if (ret) {
2698 shost_printk(KERN_ERR, phba->shost,
2699 "beiscsi_cmd_eq_create"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302700 "Failedfor EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302701 goto create_eq_error;
2702 }
2703 SE_DEBUG(DBG_LVL_8, "eqid = %d\n", phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302704 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302705 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302706create_eq_error:
2707 for (i = 0; i < (phba->num_cpus + 1); i++) {
2708 eq = &phwi_context->be_eq[i].q;
2709 mem = &eq->dma_mem;
2710 if (mem->va)
2711 pci_free_consistent(phba->pcidev, num_eq_pages
2712 * PAGE_SIZE,
2713 mem->va, mem->dma);
2714 }
2715 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302716}
2717
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302718static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302719 struct hwi_context_memory *phwi_context)
2720{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302721 unsigned int i, num_cq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302722 int ret;
2723 struct be_queue_info *cq, *eq;
2724 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302725 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302726 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302727 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302728
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302729 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2730 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302731
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302732 for (i = 0; i < phba->num_cpus; i++) {
2733 cq = &phwi_context->be_cq[i];
2734 eq = &phwi_context->be_eq[i].q;
2735 pbe_eq = &phwi_context->be_eq[i];
2736 pbe_eq->cq = cq;
2737 pbe_eq->phba = phba;
2738 mem = &cq->dma_mem;
2739 cq_vaddress = pci_alloc_consistent(phba->pcidev,
2740 num_cq_pages * PAGE_SIZE,
2741 &paddr);
2742 if (!cq_vaddress)
2743 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302744 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302745 sizeof(struct sol_cqe), cq_vaddress);
2746 if (ret) {
2747 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302748 "be_fill_queue Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302749 goto create_cq_error;
2750 }
2751
2752 mem->dma = paddr;
2753 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
2754 false, 0);
2755 if (ret) {
2756 shost_printk(KERN_ERR, phba->shost,
2757 "beiscsi_cmd_eq_create"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302758 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302759 goto create_cq_error;
2760 }
2761 SE_DEBUG(DBG_LVL_8, "iscsi cq_id is %d for eq_id %d\n",
2762 cq->id, eq->id);
2763 SE_DEBUG(DBG_LVL_8, "ISCSI CQ CREATED\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302764 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302765 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302766
2767create_cq_error:
2768 for (i = 0; i < phba->num_cpus; i++) {
2769 cq = &phwi_context->be_cq[i];
2770 mem = &cq->dma_mem;
2771 if (mem->va)
2772 pci_free_consistent(phba->pcidev, num_cq_pages
2773 * PAGE_SIZE,
2774 mem->va, mem->dma);
2775 }
2776 return ret;
2777
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302778}
2779
2780static int
2781beiscsi_create_def_hdr(struct beiscsi_hba *phba,
2782 struct hwi_context_memory *phwi_context,
2783 struct hwi_controller *phwi_ctrlr,
2784 unsigned int def_pdu_ring_sz)
2785{
2786 unsigned int idx;
2787 int ret;
2788 struct be_queue_info *dq, *cq;
2789 struct be_dma_mem *mem;
2790 struct be_mem_descriptor *mem_descr;
2791 void *dq_vaddress;
2792
2793 idx = 0;
2794 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302795 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302796 mem = &dq->dma_mem;
2797 mem_descr = phba->init_mem;
2798 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2799 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2800 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
2801 sizeof(struct phys_addr),
2802 sizeof(struct phys_addr), dq_vaddress);
2803 if (ret) {
2804 shost_printk(KERN_ERR, phba->shost,
2805 "be_fill_queue Failed for DEF PDU HDR\n");
2806 return ret;
2807 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302808 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2809 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302810 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
2811 def_pdu_ring_sz,
2812 phba->params.defpdu_hdr_sz);
2813 if (ret) {
2814 shost_printk(KERN_ERR, phba->shost,
2815 "be_cmd_create_default_pdu_queue Failed DEFHDR\n");
2816 return ret;
2817 }
2818 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
2819 SE_DEBUG(DBG_LVL_8, "iscsi def pdu id is %d\n",
2820 phwi_context->be_def_hdrq.id);
2821 hwi_post_async_buffers(phba, 1);
2822 return 0;
2823}
2824
2825static int
2826beiscsi_create_def_data(struct beiscsi_hba *phba,
2827 struct hwi_context_memory *phwi_context,
2828 struct hwi_controller *phwi_ctrlr,
2829 unsigned int def_pdu_ring_sz)
2830{
2831 unsigned int idx;
2832 int ret;
2833 struct be_queue_info *dataq, *cq;
2834 struct be_dma_mem *mem;
2835 struct be_mem_descriptor *mem_descr;
2836 void *dq_vaddress;
2837
2838 idx = 0;
2839 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302840 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302841 mem = &dataq->dma_mem;
2842 mem_descr = phba->init_mem;
2843 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2844 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2845 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
2846 sizeof(struct phys_addr),
2847 sizeof(struct phys_addr), dq_vaddress);
2848 if (ret) {
2849 shost_printk(KERN_ERR, phba->shost,
2850 "be_fill_queue Failed for DEF PDU DATA\n");
2851 return ret;
2852 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302853 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2854 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302855 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
2856 def_pdu_ring_sz,
2857 phba->params.defpdu_data_sz);
2858 if (ret) {
2859 shost_printk(KERN_ERR, phba->shost,
2860 "be_cmd_create_default_pdu_queue Failed"
2861 " for DEF PDU DATA\n");
2862 return ret;
2863 }
2864 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
2865 SE_DEBUG(DBG_LVL_8, "iscsi def data id is %d\n",
2866 phwi_context->be_def_dataq.id);
2867 hwi_post_async_buffers(phba, 0);
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302868 SE_DEBUG(DBG_LVL_8, "DEFAULT PDU DATA RING CREATED\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302869 return 0;
2870}
2871
2872static int
2873beiscsi_post_pages(struct beiscsi_hba *phba)
2874{
2875 struct be_mem_descriptor *mem_descr;
2876 struct mem_array *pm_arr;
2877 unsigned int page_offset, i;
2878 struct be_dma_mem sgl;
2879 int status;
2880
2881 mem_descr = phba->init_mem;
2882 mem_descr += HWI_MEM_SGE;
2883 pm_arr = mem_descr->mem_array;
2884
2885 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
2886 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
2887 for (i = 0; i < mem_descr->num_elements; i++) {
2888 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
2889 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
2890 page_offset,
2891 (pm_arr->size / PAGE_SIZE));
2892 page_offset += pm_arr->size / PAGE_SIZE;
2893 if (status != 0) {
2894 shost_printk(KERN_ERR, phba->shost,
2895 "post sgl failed.\n");
2896 return status;
2897 }
2898 pm_arr++;
2899 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302900 SE_DEBUG(DBG_LVL_8, "POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302901 return 0;
2902}
2903
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302904static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
2905{
2906 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05002907 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302908 pci_free_consistent(phba->pcidev, mem->size,
2909 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05002910 mem->va = NULL;
2911 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302912}
2913
2914static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
2915 u16 len, u16 entry_size)
2916{
2917 struct be_dma_mem *mem = &q->dma_mem;
2918
2919 memset(q, 0, sizeof(*q));
2920 q->len = len;
2921 q->entry_size = entry_size;
2922 mem->size = len * entry_size;
2923 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
2924 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05302925 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302926 memset(mem->va, 0, mem->size);
2927 return 0;
2928}
2929
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302930static int
2931beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
2932 struct hwi_context_memory *phwi_context,
2933 struct hwi_controller *phwi_ctrlr)
2934{
2935 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
2936 u64 pa_addr_lo;
2937 unsigned int idx, num, i;
2938 struct mem_array *pwrb_arr;
2939 void *wrb_vaddr;
2940 struct be_dma_mem sgl;
2941 struct be_mem_descriptor *mem_descr;
2942 int status;
2943
2944 idx = 0;
2945 mem_descr = phba->init_mem;
2946 mem_descr += HWI_MEM_WRB;
2947 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
2948 GFP_KERNEL);
2949 if (!pwrb_arr) {
2950 shost_printk(KERN_ERR, phba->shost,
2951 "Memory alloc failed in create wrb ring.\n");
2952 return -ENOMEM;
2953 }
2954 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2955 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
2956 num_wrb_rings = mem_descr->mem_array[idx].size /
2957 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
2958
2959 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
2960 if (num_wrb_rings) {
2961 pwrb_arr[num].virtual_address = wrb_vaddr;
2962 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
2963 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2964 sizeof(struct iscsi_wrb);
2965 wrb_vaddr += pwrb_arr[num].size;
2966 pa_addr_lo += pwrb_arr[num].size;
2967 num_wrb_rings--;
2968 } else {
2969 idx++;
2970 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2971 pa_addr_lo = mem_descr->mem_array[idx].\
2972 bus_address.u.a64.address;
2973 num_wrb_rings = mem_descr->mem_array[idx].size /
2974 (phba->params.wrbs_per_cxn *
2975 sizeof(struct iscsi_wrb));
2976 pwrb_arr[num].virtual_address = wrb_vaddr;
2977 pwrb_arr[num].bus_address.u.a64.address\
2978 = pa_addr_lo;
2979 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2980 sizeof(struct iscsi_wrb);
2981 wrb_vaddr += pwrb_arr[num].size;
2982 pa_addr_lo += pwrb_arr[num].size;
2983 num_wrb_rings--;
2984 }
2985 }
2986 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
2987 wrb_mem_index = 0;
2988 offset = 0;
2989 size = 0;
2990
2991 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
2992 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
2993 &phwi_context->be_wrbq[i]);
2994 if (status != 0) {
2995 shost_printk(KERN_ERR, phba->shost,
2996 "wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02002997 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302998 return status;
2999 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303000 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
3001 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303002 }
3003 kfree(pwrb_arr);
3004 return 0;
3005}
3006
3007static void free_wrb_handles(struct beiscsi_hba *phba)
3008{
3009 unsigned int index;
3010 struct hwi_controller *phwi_ctrlr;
3011 struct hwi_wrb_context *pwrb_context;
3012
3013 phwi_ctrlr = phba->phwi_ctrlr;
3014 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3015 pwrb_context = &phwi_ctrlr->wrb_context[index];
3016 kfree(pwrb_context->pwrb_handle_base);
3017 kfree(pwrb_context->pwrb_handle_basestd);
3018 }
3019}
3020
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303021static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3022{
3023 struct be_queue_info *q;
3024 struct be_ctrl_info *ctrl = &phba->ctrl;
3025
3026 q = &phba->ctrl.mcc_obj.q;
3027 if (q->created)
3028 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3029 be_queue_free(phba, q);
3030
3031 q = &phba->ctrl.mcc_obj.cq;
3032 if (q->created)
3033 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3034 be_queue_free(phba, q);
3035}
3036
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303037static void hwi_cleanup(struct beiscsi_hba *phba)
3038{
3039 struct be_queue_info *q;
3040 struct be_ctrl_info *ctrl = &phba->ctrl;
3041 struct hwi_controller *phwi_ctrlr;
3042 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303043 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303044
3045 phwi_ctrlr = phba->phwi_ctrlr;
3046 phwi_context = phwi_ctrlr->phwi_ctxt;
3047 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3048 q = &phwi_context->be_wrbq[i];
3049 if (q->created)
3050 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3051 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303052 free_wrb_handles(phba);
3053
3054 q = &phwi_context->be_def_hdrq;
3055 if (q->created)
3056 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3057
3058 q = &phwi_context->be_def_dataq;
3059 if (q->created)
3060 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3061
3062 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3063
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303064 for (i = 0; i < (phba->num_cpus); i++) {
3065 q = &phwi_context->be_cq[i];
3066 if (q->created)
3067 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3068 }
3069 if (phba->msix_enabled)
3070 eq_num = 1;
3071 else
3072 eq_num = 0;
3073 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3074 q = &phwi_context->be_eq[i].q;
3075 if (q->created)
3076 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3077 }
3078 be_mcc_queues_destroy(phba);
3079}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303080
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303081static int be_mcc_queues_create(struct beiscsi_hba *phba,
3082 struct hwi_context_memory *phwi_context)
3083{
3084 struct be_queue_info *q, *cq;
3085 struct be_ctrl_info *ctrl = &phba->ctrl;
3086
3087 /* Alloc MCC compl queue */
3088 cq = &phba->ctrl.mcc_obj.cq;
3089 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3090 sizeof(struct be_mcc_compl)))
3091 goto err;
3092 /* Ask BE to create MCC compl queue; */
3093 if (phba->msix_enabled) {
3094 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3095 [phba->num_cpus].q, false, true, 0))
3096 goto mcc_cq_free;
3097 } else {
3098 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3099 false, true, 0))
3100 goto mcc_cq_free;
3101 }
3102
3103 /* Alloc MCC queue */
3104 q = &phba->ctrl.mcc_obj.q;
3105 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3106 goto mcc_cq_destroy;
3107
3108 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303109 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303110 goto mcc_q_free;
3111
3112 return 0;
3113
3114mcc_q_free:
3115 be_queue_free(phba, q);
3116mcc_cq_destroy:
3117 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3118mcc_cq_free:
3119 be_queue_free(phba, cq);
3120err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303121 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303122}
3123
3124static int find_num_cpus(void)
3125{
3126 int num_cpus = 0;
3127
3128 num_cpus = num_online_cpus();
3129 if (num_cpus >= MAX_CPUS)
3130 num_cpus = MAX_CPUS - 1;
3131
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303132 SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303133 return num_cpus;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303134}
3135
3136static int hwi_init_port(struct beiscsi_hba *phba)
3137{
3138 struct hwi_controller *phwi_ctrlr;
3139 struct hwi_context_memory *phwi_context;
3140 unsigned int def_pdu_ring_sz;
3141 struct be_ctrl_info *ctrl = &phba->ctrl;
3142 int status;
3143
3144 def_pdu_ring_sz =
3145 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3146 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303147 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303148 phwi_context->max_eqd = 0;
3149 phwi_context->min_eqd = 0;
3150 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303151 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303152
3153 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303154 if (status != 0) {
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303155 shost_printk(KERN_ERR, phba->shost, "EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303156 goto error;
3157 }
3158
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303159 status = be_mcc_queues_create(phba, phwi_context);
3160 if (status != 0)
3161 goto error;
3162
3163 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303164 if (status != 0) {
3165 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303166 "Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303167 goto error;
3168 }
3169
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303170 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303171 if (status != 0) {
3172 shost_printk(KERN_ERR, phba->shost, "CQ not created\n");
3173 goto error;
3174 }
3175
3176 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3177 def_pdu_ring_sz);
3178 if (status != 0) {
3179 shost_printk(KERN_ERR, phba->shost,
3180 "Default Header not created\n");
3181 goto error;
3182 }
3183
3184 status = beiscsi_create_def_data(phba, phwi_context,
3185 phwi_ctrlr, def_pdu_ring_sz);
3186 if (status != 0) {
3187 shost_printk(KERN_ERR, phba->shost,
3188 "Default Data not created\n");
3189 goto error;
3190 }
3191
3192 status = beiscsi_post_pages(phba);
3193 if (status != 0) {
3194 shost_printk(KERN_ERR, phba->shost, "Post SGL Pages Failed\n");
3195 goto error;
3196 }
3197
3198 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3199 if (status != 0) {
3200 shost_printk(KERN_ERR, phba->shost,
3201 "WRB Rings not created\n");
3202 goto error;
3203 }
3204
3205 SE_DEBUG(DBG_LVL_8, "hwi_init_port success\n");
3206 return 0;
3207
3208error:
3209 shost_printk(KERN_ERR, phba->shost, "hwi_init_port failed");
3210 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003211 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303212}
3213
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303214static int hwi_init_controller(struct beiscsi_hba *phba)
3215{
3216 struct hwi_controller *phwi_ctrlr;
3217
3218 phwi_ctrlr = phba->phwi_ctrlr;
3219 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3220 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3221 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303222 SE_DEBUG(DBG_LVL_8, " phwi_ctrlr->phwi_ctxt=%p\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303223 phwi_ctrlr->phwi_ctxt);
3224 } else {
3225 shost_printk(KERN_ERR, phba->shost,
3226 "HWI_MEM_ADDN_CONTEXT is more than one element."
3227 "Failing to load\n");
3228 return -ENOMEM;
3229 }
3230
3231 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003232 if (beiscsi_init_wrb_handle(phba))
3233 return -ENOMEM;
3234
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303235 hwi_init_async_pdu_ctx(phba);
3236 if (hwi_init_port(phba) != 0) {
3237 shost_printk(KERN_ERR, phba->shost,
3238 "hwi_init_controller failed\n");
3239 return -ENOMEM;
3240 }
3241 return 0;
3242}
3243
3244static void beiscsi_free_mem(struct beiscsi_hba *phba)
3245{
3246 struct be_mem_descriptor *mem_descr;
3247 int i, j;
3248
3249 mem_descr = phba->init_mem;
3250 i = 0;
3251 j = 0;
3252 for (i = 0; i < SE_MEM_MAX; i++) {
3253 for (j = mem_descr->num_elements; j > 0; j--) {
3254 pci_free_consistent(phba->pcidev,
3255 mem_descr->mem_array[j - 1].size,
3256 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303257 (unsigned long)mem_descr->mem_array[j - 1].
3258 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303259 }
3260 kfree(mem_descr->mem_array);
3261 mem_descr++;
3262 }
3263 kfree(phba->init_mem);
3264 kfree(phba->phwi_ctrlr);
3265}
3266
3267static int beiscsi_init_controller(struct beiscsi_hba *phba)
3268{
3269 int ret = -ENOMEM;
3270
3271 ret = beiscsi_get_memory(phba);
3272 if (ret < 0) {
3273 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe -"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303274 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303275 return ret;
3276 }
3277
3278 ret = hwi_init_controller(phba);
3279 if (ret)
3280 goto free_init;
3281 SE_DEBUG(DBG_LVL_8, "Return success from beiscsi_init_controller");
3282 return 0;
3283
3284free_init:
3285 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003286 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303287}
3288
3289static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3290{
3291 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3292 struct sgl_handle *psgl_handle;
3293 struct iscsi_sge *pfrag;
3294 unsigned int arr_index, i, idx;
3295
3296 phba->io_sgl_hndl_avbl = 0;
3297 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303298
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303299 mem_descr_sglh = phba->init_mem;
3300 mem_descr_sglh += HWI_MEM_SGLH;
3301 if (1 == mem_descr_sglh->num_elements) {
3302 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3303 phba->params.ios_per_ctrl,
3304 GFP_KERNEL);
3305 if (!phba->io_sgl_hndl_base) {
3306 shost_printk(KERN_ERR, phba->shost,
3307 "Mem Alloc Failed. Failing to load\n");
3308 return -ENOMEM;
3309 }
3310 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3311 (phba->params.icds_per_ctrl -
3312 phba->params.ios_per_ctrl),
3313 GFP_KERNEL);
3314 if (!phba->eh_sgl_hndl_base) {
3315 kfree(phba->io_sgl_hndl_base);
3316 shost_printk(KERN_ERR, phba->shost,
3317 "Mem Alloc Failed. Failing to load\n");
3318 return -ENOMEM;
3319 }
3320 } else {
3321 shost_printk(KERN_ERR, phba->shost,
3322 "HWI_MEM_SGLH is more than one element."
3323 "Failing to load\n");
3324 return -ENOMEM;
3325 }
3326
3327 arr_index = 0;
3328 idx = 0;
3329 while (idx < mem_descr_sglh->num_elements) {
3330 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3331
3332 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3333 sizeof(struct sgl_handle)); i++) {
3334 if (arr_index < phba->params.ios_per_ctrl) {
3335 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3336 phba->io_sgl_hndl_avbl++;
3337 arr_index++;
3338 } else {
3339 phba->eh_sgl_hndl_base[arr_index -
3340 phba->params.ios_per_ctrl] =
3341 psgl_handle;
3342 arr_index++;
3343 phba->eh_sgl_hndl_avbl++;
3344 }
3345 psgl_handle++;
3346 }
3347 idx++;
3348 }
3349 SE_DEBUG(DBG_LVL_8,
3350 "phba->io_sgl_hndl_avbl=%d"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303351 "phba->eh_sgl_hndl_avbl=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303352 phba->io_sgl_hndl_avbl,
3353 phba->eh_sgl_hndl_avbl);
3354 mem_descr_sg = phba->init_mem;
3355 mem_descr_sg += HWI_MEM_SGE;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303356 SE_DEBUG(DBG_LVL_8, "\n mem_descr_sg->num_elements=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303357 mem_descr_sg->num_elements);
3358 arr_index = 0;
3359 idx = 0;
3360 while (idx < mem_descr_sg->num_elements) {
3361 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3362
3363 for (i = 0;
3364 i < (mem_descr_sg->mem_array[idx].size) /
3365 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3366 i++) {
3367 if (arr_index < phba->params.ios_per_ctrl)
3368 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3369 else
3370 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3371 phba->params.ios_per_ctrl];
3372 psgl_handle->pfrag = pfrag;
3373 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3374 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3375 pfrag += phba->params.num_sge_per_io;
3376 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303377 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303378 }
3379 idx++;
3380 }
3381 phba->io_sgl_free_index = 0;
3382 phba->io_sgl_alloc_index = 0;
3383 phba->eh_sgl_free_index = 0;
3384 phba->eh_sgl_alloc_index = 0;
3385 return 0;
3386}
3387
3388static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3389{
3390 int i, new_cid;
3391
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303392 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303393 GFP_KERNEL);
3394 if (!phba->cid_array) {
3395 shost_printk(KERN_ERR, phba->shost,
3396 "Failed to allocate memory in "
3397 "hba_setup_cid_tbls\n");
3398 return -ENOMEM;
3399 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303400 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303401 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3402 if (!phba->ep_array) {
3403 shost_printk(KERN_ERR, phba->shost,
3404 "Failed to allocate memory in "
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303405 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303406 kfree(phba->cid_array);
3407 return -ENOMEM;
3408 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303409 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303410 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3411 phba->cid_array[i] = new_cid;
3412 new_cid += 2;
3413 }
3414 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3415 return 0;
3416}
3417
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303418static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303419{
3420 struct be_ctrl_info *ctrl = &phba->ctrl;
3421 struct hwi_controller *phwi_ctrlr;
3422 struct hwi_context_memory *phwi_context;
3423 struct be_queue_info *eq;
3424 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303425 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303426 u32 enabled;
3427
3428 phwi_ctrlr = phba->phwi_ctrlr;
3429 phwi_context = phwi_ctrlr->phwi_ctxt;
3430
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303431 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3432 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3433 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303434
3435 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3436 if (!enabled) {
3437 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303438 SE_DEBUG(DBG_LVL_8, "reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303439 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003440 }
3441
3442 if (!phba->msix_enabled) {
3443 eq = &phwi_context->be_eq[0].q;
3444 SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
3445 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3446 } else {
3447 for (i = 0; i <= phba->num_cpus; i++) {
3448 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303449 SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303450 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3451 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303452 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303453}
3454
3455static void hwi_disable_intr(struct beiscsi_hba *phba)
3456{
3457 struct be_ctrl_info *ctrl = &phba->ctrl;
3458
3459 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3460 u32 reg = ioread32(addr);
3461
3462 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3463 if (enabled) {
3464 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3465 iowrite32(reg, addr);
3466 } else
3467 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303468 "In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303469}
3470
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303471static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3472{
3473 struct be_cmd_resp_get_boot_target *boot_resp;
3474 struct be_cmd_resp_get_session *session_resp;
3475 struct be_mcc_wrb *wrb;
3476 struct be_dma_mem nonemb_cmd;
3477 unsigned int tag, wrb_num;
3478 unsigned short status, extd_status;
3479 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
Mike Christief457a462011-06-24 15:11:53 -05003480 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303481
3482 tag = beiscsi_get_boot_target(phba);
3483 if (!tag) {
3484 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
3485 return -EAGAIN;
3486 } else
3487 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3488 phba->ctrl.mcc_numtag[tag]);
3489
3490 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3491 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3492 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3493 if (status || extd_status) {
3494 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed"
3495 " status = %d extd_status = %d\n",
3496 status, extd_status);
3497 free_mcc_tag(&phba->ctrl, tag);
3498 return -EBUSY;
3499 }
3500 wrb = queue_get_wrb(mccq, wrb_num);
3501 free_mcc_tag(&phba->ctrl, tag);
3502 boot_resp = embedded_payload(wrb);
3503
3504 if (boot_resp->boot_session_handle < 0) {
Mike Christief457a462011-06-24 15:11:53 -05003505 shost_printk(KERN_INFO, phba->shost, "No Boot Session.\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303506 return -ENXIO;
3507 }
3508
3509 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3510 sizeof(*session_resp),
3511 &nonemb_cmd.dma);
3512 if (nonemb_cmd.va == NULL) {
3513 SE_DEBUG(DBG_LVL_1,
3514 "Failed to allocate memory for"
3515 "beiscsi_get_session_info\n");
3516 return -ENOMEM;
3517 }
3518
3519 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
3520 tag = beiscsi_get_session_info(phba,
3521 boot_resp->boot_session_handle, &nonemb_cmd);
3522 if (!tag) {
3523 SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info"
3524 " Failed\n");
3525 goto boot_freemem;
3526 } else
3527 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3528 phba->ctrl.mcc_numtag[tag]);
3529
3530 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3531 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3532 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3533 if (status || extd_status) {
3534 SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info Failed"
3535 " status = %d extd_status = %d\n",
3536 status, extd_status);
3537 free_mcc_tag(&phba->ctrl, tag);
3538 goto boot_freemem;
3539 }
3540 wrb = queue_get_wrb(mccq, wrb_num);
3541 free_mcc_tag(&phba->ctrl, tag);
3542 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003543
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303544 memcpy(&phba->boot_sess, &session_resp->session_info,
3545 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003546 ret = 0;
3547
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303548boot_freemem:
3549 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3550 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003551 return ret;
3552}
3553
3554static void beiscsi_boot_release(void *data)
3555{
3556 struct beiscsi_hba *phba = data;
3557
3558 scsi_host_put(phba->shost);
3559}
3560
3561static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3562{
3563 struct iscsi_boot_kobj *boot_kobj;
3564
3565 /* get boot info using mgmt cmd */
3566 if (beiscsi_get_boot_info(phba))
3567 /* Try to see if we can carry on without this */
3568 return 0;
3569
3570 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3571 if (!phba->boot_kset)
3572 return -ENOMEM;
3573
3574 /* get a ref because the show function will ref the phba */
3575 if (!scsi_host_get(phba->shost))
3576 goto free_kset;
3577 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3578 beiscsi_show_boot_tgt_info,
3579 beiscsi_tgt_get_attr_visibility,
3580 beiscsi_boot_release);
3581 if (!boot_kobj)
3582 goto put_shost;
3583
3584 if (!scsi_host_get(phba->shost))
3585 goto free_kset;
3586 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3587 beiscsi_show_boot_ini_info,
3588 beiscsi_ini_get_attr_visibility,
3589 beiscsi_boot_release);
3590 if (!boot_kobj)
3591 goto put_shost;
3592
3593 if (!scsi_host_get(phba->shost))
3594 goto free_kset;
3595 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3596 beiscsi_show_boot_eth_info,
3597 beiscsi_eth_get_attr_visibility,
3598 beiscsi_boot_release);
3599 if (!boot_kobj)
3600 goto put_shost;
3601 return 0;
3602
3603put_shost:
3604 scsi_host_put(phba->shost);
3605free_kset:
3606 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303607 return -ENOMEM;
3608}
3609
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303610static int beiscsi_init_port(struct beiscsi_hba *phba)
3611{
3612 int ret;
3613
3614 ret = beiscsi_init_controller(phba);
3615 if (ret < 0) {
3616 shost_printk(KERN_ERR, phba->shost,
3617 "beiscsi_dev_probe - Failed in"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303618 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303619 return ret;
3620 }
3621 ret = beiscsi_init_sgl_handle(phba);
3622 if (ret < 0) {
3623 shost_printk(KERN_ERR, phba->shost,
3624 "beiscsi_dev_probe - Failed in"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303625 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303626 goto do_cleanup_ctrlr;
3627 }
3628
3629 if (hba_setup_cid_tbls(phba)) {
3630 shost_printk(KERN_ERR, phba->shost,
3631 "Failed in hba_setup_cid_tbls\n");
3632 kfree(phba->io_sgl_hndl_base);
3633 kfree(phba->eh_sgl_hndl_base);
3634 goto do_cleanup_ctrlr;
3635 }
3636
3637 return ret;
3638
3639do_cleanup_ctrlr:
3640 hwi_cleanup(phba);
3641 return ret;
3642}
3643
3644static void hwi_purge_eq(struct beiscsi_hba *phba)
3645{
3646 struct hwi_controller *phwi_ctrlr;
3647 struct hwi_context_memory *phwi_context;
3648 struct be_queue_info *eq;
3649 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303650 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303651 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303652
3653 phwi_ctrlr = phba->phwi_ctrlr;
3654 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303655 if (phba->msix_enabled)
3656 eq_msix = 1;
3657 else
3658 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303659
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303660 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3661 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303662 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303663 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303664 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3665 & EQE_VALID_MASK) {
3666 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3667 queue_tail_inc(eq);
3668 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303669 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303670 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303671
3672 if (num_processed)
3673 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303674 }
3675}
3676
3677static void beiscsi_clean_port(struct beiscsi_hba *phba)
3678{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05303679 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303680
3681 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
3682 if (mgmt_status)
3683 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303684 "mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303685
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303686 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303687 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303688 kfree(phba->io_sgl_hndl_base);
3689 kfree(phba->eh_sgl_hndl_base);
3690 kfree(phba->cid_array);
3691 kfree(phba->ep_array);
3692}
3693
Mike Christie1282ab72012-04-18 03:06:00 -05003694static void beiscsi_cleanup_task(struct iscsi_task *task)
3695{
3696 struct beiscsi_io_task *io_task = task->dd_data;
3697 struct iscsi_conn *conn = task->conn;
3698 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3699 struct beiscsi_hba *phba = beiscsi_conn->phba;
3700 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3701 struct hwi_wrb_context *pwrb_context;
3702 struct hwi_controller *phwi_ctrlr;
3703
3704 phwi_ctrlr = phba->phwi_ctrlr;
3705 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
3706 - phba->fw_config.iscsi_cid_start];
3707
3708 if (io_task->cmd_bhs) {
3709 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3710 io_task->bhs_pa.u.a64.address);
3711 io_task->cmd_bhs = NULL;
3712 }
3713
3714 if (task->sc) {
3715 if (io_task->pwrb_handle) {
3716 free_wrb_handle(phba, pwrb_context,
3717 io_task->pwrb_handle);
3718 io_task->pwrb_handle = NULL;
3719 }
3720
3721 if (io_task->psgl_handle) {
3722 spin_lock(&phba->io_sgl_lock);
3723 free_io_sgl_handle(phba, io_task->psgl_handle);
3724 spin_unlock(&phba->io_sgl_lock);
3725 io_task->psgl_handle = NULL;
3726 }
3727 } else {
3728 if (!beiscsi_conn->login_in_progress) {
3729 if (io_task->pwrb_handle) {
3730 free_wrb_handle(phba, pwrb_context,
3731 io_task->pwrb_handle);
3732 io_task->pwrb_handle = NULL;
3733 }
3734 if (io_task->psgl_handle) {
3735 spin_lock(&phba->mgmt_sgl_lock);
3736 free_mgmt_sgl_handle(phba,
3737 io_task->psgl_handle);
3738 spin_unlock(&phba->mgmt_sgl_lock);
3739 io_task->psgl_handle = NULL;
3740 }
3741 }
3742 }
3743}
3744
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303745void
3746beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
3747 struct beiscsi_offload_params *params)
3748{
3749 struct wrb_handle *pwrb_handle;
3750 struct iscsi_target_context_update_wrb *pwrb = NULL;
3751 struct be_mem_descriptor *mem_descr;
3752 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05003753 struct iscsi_task *task = beiscsi_conn->task;
3754 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303755 u32 doorbell = 0;
3756
3757 /*
3758 * We can always use 0 here because it is reserved by libiscsi for
3759 * login/startup related tasks.
3760 */
Mike Christie1282ab72012-04-18 03:06:00 -05003761 beiscsi_conn->login_in_progress = 0;
3762 spin_lock_bh(&session->lock);
3763 beiscsi_cleanup_task(task);
3764 spin_unlock_bh(&session->lock);
3765
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303766 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05303767 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303768 pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
3769 memset(pwrb, 0, sizeof(*pwrb));
3770 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3771 max_burst_length, pwrb, params->dw[offsetof
3772 (struct amap_beiscsi_offload_params,
3773 max_burst_length) / 32]);
3774 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3775 max_send_data_segment_length, pwrb,
3776 params->dw[offsetof(struct amap_beiscsi_offload_params,
3777 max_send_data_segment_length) / 32]);
3778 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3779 first_burst_length,
3780 pwrb,
3781 params->dw[offsetof(struct amap_beiscsi_offload_params,
3782 first_burst_length) / 32]);
3783
3784 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
3785 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3786 erl) / 32] & OFFLD_PARAMS_ERL));
3787 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
3788 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3789 dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
3790 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
3791 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3792 hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
3793 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
3794 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3795 ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
3796 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
3797 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3798 imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
3799 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
3800 pwrb,
3801 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3802 exp_statsn) / 32] + 1));
3803 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
3804 0x7);
3805 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
3806 pwrb, pwrb_handle->wrb_index);
3807 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
3808 pwrb, pwrb_handle->nxt_wrb_index);
3809 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3810 session_state, pwrb, 0);
3811 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
3812 pwrb, 1);
3813 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
3814 pwrb, 0);
3815 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
3816 0);
3817
3818 mem_descr = phba->init_mem;
3819 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
3820
3821 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3822 pad_buffer_addr_hi, pwrb,
3823 mem_descr->mem_array[0].bus_address.u.a32.address_hi);
3824 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3825 pad_buffer_addr_lo, pwrb,
3826 mem_descr->mem_array[0].bus_address.u.a32.address_lo);
3827
3828 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
3829
3830 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303831 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303832 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303833 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
3834
3835 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
3836}
3837
3838static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
3839 int *index, int *age)
3840{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303841 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303842 if (age)
3843 *age = conn->session->age;
3844}
3845
3846/**
3847 * beiscsi_alloc_pdu - allocates pdu and related resources
3848 * @task: libiscsi task
3849 * @opcode: opcode of pdu for task
3850 *
3851 * This is called with the session lock held. It will allocate
3852 * the wrb and sgl if needed for the command. And it will prep
3853 * the pdu's itt. beiscsi_parse_pdu will later translate
3854 * the pdu itt to the libiscsi task itt.
3855 */
3856static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
3857{
3858 struct beiscsi_io_task *io_task = task->dd_data;
3859 struct iscsi_conn *conn = task->conn;
3860 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3861 struct beiscsi_hba *phba = beiscsi_conn->phba;
3862 struct hwi_wrb_context *pwrb_context;
3863 struct hwi_controller *phwi_ctrlr;
3864 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303865 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3866 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303867
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303868 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06003869 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303870 if (!io_task->cmd_bhs)
3871 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303872 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303873 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303874 io_task->conn = beiscsi_conn;
3875
3876 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
3877 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303878 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003879 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303880
3881 if (task->sc) {
3882 spin_lock(&phba->io_sgl_lock);
3883 io_task->psgl_handle = alloc_io_sgl_handle(phba);
3884 spin_unlock(&phba->io_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303885 if (!io_task->psgl_handle)
3886 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303887 io_task->pwrb_handle = alloc_wrb_handle(phba,
3888 beiscsi_conn->beiscsi_conn_cid -
3889 phba->fw_config.iscsi_cid_start);
3890 if (!io_task->pwrb_handle)
3891 goto free_io_hndls;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303892 } else {
3893 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05303894 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303895 if (!beiscsi_conn->login_in_progress) {
3896 spin_lock(&phba->mgmt_sgl_lock);
3897 io_task->psgl_handle = (struct sgl_handle *)
3898 alloc_mgmt_sgl_handle(phba);
3899 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303900 if (!io_task->psgl_handle)
3901 goto free_hndls;
3902
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303903 beiscsi_conn->login_in_progress = 1;
3904 beiscsi_conn->plogin_sgl_handle =
3905 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303906 io_task->pwrb_handle =
3907 alloc_wrb_handle(phba,
3908 beiscsi_conn->beiscsi_conn_cid -
3909 phba->fw_config.iscsi_cid_start);
3910 if (!io_task->pwrb_handle)
3911 goto free_io_hndls;
3912 beiscsi_conn->plogin_wrb_handle =
3913 io_task->pwrb_handle;
3914
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303915 } else {
3916 io_task->psgl_handle =
3917 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303918 io_task->pwrb_handle =
3919 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303920 }
Mike Christie1282ab72012-04-18 03:06:00 -05003921 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303922 } else {
3923 spin_lock(&phba->mgmt_sgl_lock);
3924 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
3925 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303926 if (!io_task->psgl_handle)
3927 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303928 io_task->pwrb_handle =
3929 alloc_wrb_handle(phba,
3930 beiscsi_conn->beiscsi_conn_cid -
3931 phba->fw_config.iscsi_cid_start);
3932 if (!io_task->pwrb_handle)
3933 goto free_mgmt_hndls;
3934
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303935 }
3936 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303937 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
3938 wrb_index << 16) | (unsigned int)
3939 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303940 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303941
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303942 io_task->cmd_bhs->iscsi_hdr.itt = itt;
3943 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303944
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303945free_io_hndls:
3946 spin_lock(&phba->io_sgl_lock);
3947 free_io_sgl_handle(phba, io_task->psgl_handle);
3948 spin_unlock(&phba->io_sgl_lock);
3949 goto free_hndls;
3950free_mgmt_hndls:
3951 spin_lock(&phba->mgmt_sgl_lock);
3952 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
3953 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303954free_hndls:
3955 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303956 pwrb_context = &phwi_ctrlr->wrb_context[
3957 beiscsi_conn->beiscsi_conn_cid -
3958 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303959 if (io_task->pwrb_handle)
3960 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303961 io_task->pwrb_handle = NULL;
3962 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3963 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05003964 io_task->cmd_bhs = NULL;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303965 SE_DEBUG(DBG_LVL_1, "Alloc of SGL_ICD Failed\n");
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303966 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303967}
3968
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303969static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
3970 unsigned int num_sg, unsigned int xferlen,
3971 unsigned int writedir)
3972{
3973
3974 struct beiscsi_io_task *io_task = task->dd_data;
3975 struct iscsi_conn *conn = task->conn;
3976 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3977 struct beiscsi_hba *phba = beiscsi_conn->phba;
3978 struct iscsi_wrb *pwrb = NULL;
3979 unsigned int doorbell = 0;
3980
3981 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303982 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
3983 io_task->bhs_len = sizeof(struct be_cmd_bhs);
3984
3985 if (writedir) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303986 memset(&io_task->cmd_bhs->iscsi_data_pdu, 0, 48);
3987 AMAP_SET_BITS(struct amap_pdu_data_out, itt,
3988 &io_task->cmd_bhs->iscsi_data_pdu,
3989 (unsigned int)io_task->cmd_bhs->iscsi_hdr.itt);
3990 AMAP_SET_BITS(struct amap_pdu_data_out, opcode,
3991 &io_task->cmd_bhs->iscsi_data_pdu,
3992 ISCSI_OPCODE_SCSI_DATA_OUT);
3993 AMAP_SET_BITS(struct amap_pdu_data_out, final_bit,
3994 &io_task->cmd_bhs->iscsi_data_pdu, 1);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303995 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3996 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303997 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303998 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303999 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4000 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304001 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4002 }
4003 memcpy(&io_task->cmd_bhs->iscsi_data_pdu.
4004 dw[offsetof(struct amap_pdu_data_out, lun) / 32],
Andy Grover516f43a2011-06-16 15:57:09 -07004005 &io_task->cmd_bhs->iscsi_hdr.lun, sizeof(struct scsi_lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304006
4007 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004008 cpu_to_be16(*(unsigned short *)
4009 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304010 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4011 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4012 io_task->pwrb_handle->wrb_index);
4013 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4014 be32_to_cpu(task->cmdsn));
4015 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4016 io_task->psgl_handle->sgl_index);
4017
4018 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4019
4020 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4021 io_task->pwrb_handle->nxt_wrb_index);
4022 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4023
4024 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304025 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304026 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4027 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4028
4029 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4030 return 0;
4031}
4032
4033static int beiscsi_mtask(struct iscsi_task *task)
4034{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304035 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304036 struct iscsi_conn *conn = task->conn;
4037 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4038 struct beiscsi_hba *phba = beiscsi_conn->phba;
4039 struct iscsi_wrb *pwrb = NULL;
4040 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304041 unsigned int cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304042
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304043 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304044 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304045 memset(pwrb, 0, sizeof(*pwrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304046 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4047 be32_to_cpu(task->cmdsn));
4048 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4049 io_task->pwrb_handle->wrb_index);
4050 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4051 io_task->psgl_handle->sgl_index);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304052
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304053 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4054 case ISCSI_OP_LOGIN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304055 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4056 TGT_DM_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304057 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4058 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
4059 hwi_write_buffer(pwrb, task);
4060 break;
4061 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004062 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
4063 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4064 TGT_DM_CMD);
4065 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt,
4066 pwrb, 0);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004067 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004068 } else {
4069 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4070 INI_RD_CMD);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004071 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004072 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304073 hwi_write_buffer(pwrb, task);
4074 break;
4075 case ISCSI_OP_TEXT:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304076 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickalb30c6da2010-01-23 05:38:56 +05304077 TGT_DM_CMD);
Jayamohan Kallickal0ecb0b42010-01-05 05:09:19 +05304078 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304079 hwi_write_buffer(pwrb, task);
4080 break;
4081 case ISCSI_OP_SCSI_TMFUNC:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304082 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4083 INI_TMF_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304084 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4085 hwi_write_buffer(pwrb, task);
4086 break;
4087 case ISCSI_OP_LOGOUT:
4088 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4089 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304090 HWH_TYPE_LOGOUT);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304091 hwi_write_buffer(pwrb, task);
4092 break;
4093
4094 default:
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304095 SE_DEBUG(DBG_LVL_1, "opcode =%d Not supported\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304096 task->hdr->opcode & ISCSI_OPCODE_MASK);
4097 return -EINVAL;
4098 }
4099
4100 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
Jayamohan Kallickal51a46252010-01-05 05:10:01 +05304101 task->data_count);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304102 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4103 io_task->pwrb_handle->nxt_wrb_index);
4104 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4105
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304106 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304107 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304108 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4109 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4110 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4111 return 0;
4112}
4113
4114static int beiscsi_task_xmit(struct iscsi_task *task)
4115{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304116 struct beiscsi_io_task *io_task = task->dd_data;
4117 struct scsi_cmnd *sc = task->sc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304118 struct scatterlist *sg;
4119 int num_sg;
4120 unsigned int writedir = 0, xferlen = 0;
4121
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304122 if (!sc)
4123 return beiscsi_mtask(task);
4124
4125 io_task->scsi_cmnd = sc;
4126 num_sg = scsi_dma_map(sc);
4127 if (num_sg < 0) {
4128 SE_DEBUG(DBG_LVL_1, " scsi_dma_map Failed\n")
4129 return num_sg;
4130 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304131 xferlen = scsi_bufflen(sc);
4132 sg = scsi_sglist(sc);
4133 if (sc->sc_data_direction == DMA_TO_DEVICE) {
4134 writedir = 1;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304135 SE_DEBUG(DBG_LVL_4, "task->imm_count=0x%08x\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304136 task->imm_count);
4137 } else
4138 writedir = 0;
4139 return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
4140}
4141
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004142static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304143{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304144 struct hwi_controller *phwi_ctrlr;
4145 struct hwi_context_memory *phwi_context;
4146 struct be_eq_obj *pbe_eq;
4147 unsigned int i, msix_vec;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304148 u8 *real_offset = 0;
4149 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304150
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304151 phwi_ctrlr = phba->phwi_ctrlr;
4152 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304153 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304154 if (phba->msix_enabled) {
4155 for (i = 0; i <= phba->num_cpus; i++) {
4156 msix_vec = phba->msix_entries[i].vector;
4157 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004158 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304159 }
4160 } else
4161 if (phba->pcidev->irq)
4162 free_irq(phba->pcidev->irq, phba);
4163 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304164 destroy_workqueue(phba->wq);
4165 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304166 for (i = 0; i < phba->num_cpus; i++) {
4167 pbe_eq = &phwi_context->be_eq[i];
4168 blk_iopoll_disable(&pbe_eq->iopoll);
4169 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304170
4171 beiscsi_clean_port(phba);
4172 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304173 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4174
4175 value = readl((void *)real_offset);
4176
4177 if (value & 0x00010000) {
4178 value &= 0xfffeffff;
4179 writel(value, (void *)real_offset);
4180 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304181 beiscsi_unmap_pci_function(phba);
4182 pci_free_consistent(phba->pcidev,
4183 phba->ctrl.mbox_mem_alloced.size,
4184 phba->ctrl.mbox_mem_alloced.va,
4185 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004186}
4187
4188static void beiscsi_remove(struct pci_dev *pcidev)
4189{
4190
4191 struct beiscsi_hba *phba = NULL;
4192
4193 phba = pci_get_drvdata(pcidev);
4194 if (!phba) {
4195 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4196 return;
4197 }
4198
4199 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004200 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304201 iscsi_host_remove(phba->shost);
4202 pci_dev_put(phba->pcidev);
4203 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004204 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304205}
4206
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004207static void beiscsi_shutdown(struct pci_dev *pcidev)
4208{
4209
4210 struct beiscsi_hba *phba = NULL;
4211
4212 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4213 if (!phba) {
4214 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4215 return;
4216 }
4217
4218 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004219 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004220}
4221
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304222static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4223{
4224 int i, status;
4225
4226 for (i = 0; i <= phba->num_cpus; i++)
4227 phba->msix_entries[i].entry = i;
4228
4229 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4230 (phba->num_cpus + 1));
4231 if (!status)
4232 phba->msix_enabled = true;
4233
4234 return;
4235}
4236
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304237static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
4238 const struct pci_device_id *id)
4239{
4240 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304241 struct hwi_controller *phwi_ctrlr;
4242 struct hwi_context_memory *phwi_context;
4243 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304244 int ret, num_cpus, i;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304245 u8 *real_offset = 0;
4246 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304247
4248 ret = beiscsi_enable_pci(pcidev);
4249 if (ret < 0) {
Dan Carpenter82284c02010-06-10 09:53:05 +02004250 dev_err(&pcidev->dev, "beiscsi_dev_probe-"
4251 " Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304252 return ret;
4253 }
4254
4255 phba = beiscsi_hba_alloc(pcidev);
4256 if (!phba) {
4257 dev_err(&pcidev->dev, "beiscsi_dev_probe-"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304258 " Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304259 goto disable_pci;
4260 }
4261
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304262 switch (pcidev->device) {
4263 case BE_DEVICE_ID1:
4264 case OC_DEVICE_ID1:
4265 case OC_DEVICE_ID2:
4266 phba->generation = BE_GEN2;
4267 break;
4268 case BE_DEVICE_ID2:
4269 case OC_DEVICE_ID3:
4270 phba->generation = BE_GEN3;
4271 break;
4272 default:
4273 phba->generation = 0;
4274 }
4275
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304276 if (enable_msix)
4277 num_cpus = find_num_cpus();
4278 else
4279 num_cpus = 1;
4280 phba->num_cpus = num_cpus;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304281 SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304282
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004283 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304284 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004285 if (!phba->msix_enabled)
4286 phba->num_cpus = 1;
4287 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304288 ret = be_ctrl_init(phba, pcidev);
4289 if (ret) {
4290 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4291 "Failed in be_ctrl_init\n");
4292 goto hba_free;
4293 }
4294
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304295 if (!num_hba) {
4296 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4297 value = readl((void *)real_offset);
4298 if (value & 0x00010000) {
4299 gcrashmode++;
4300 shost_printk(KERN_ERR, phba->shost,
4301 "Loading Driver in crashdump mode\n");
Jayamohan Kallickale5285862011-10-07 19:31:08 -05004302 ret = beiscsi_cmd_reset_function(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304303 if (ret) {
4304 shost_printk(KERN_ERR, phba->shost,
4305 "Reset Failed. Aborting Crashdump\n");
4306 goto hba_free;
4307 }
4308 ret = be_chk_reset_complete(phba);
4309 if (ret) {
4310 shost_printk(KERN_ERR, phba->shost,
4311 "Failed to get out of reset."
4312 "Aborting Crashdump\n");
4313 goto hba_free;
4314 }
4315 } else {
4316 value |= 0x00010000;
4317 writel(value, (void *)real_offset);
4318 num_hba++;
4319 }
4320 }
4321
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304322 spin_lock_init(&phba->io_sgl_lock);
4323 spin_lock_init(&phba->mgmt_sgl_lock);
4324 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304325 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4326 if (ret != 0) {
4327 shost_printk(KERN_ERR, phba->shost,
4328 "Error getting fw config\n");
4329 goto free_port;
4330 }
4331 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304332 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304333 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304334 ret = beiscsi_init_port(phba);
4335 if (ret < 0) {
4336 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4337 "Failed in beiscsi_init_port\n");
4338 goto free_port;
4339 }
4340
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304341 for (i = 0; i < MAX_MCC_CMD ; i++) {
4342 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4343 phba->ctrl.mcc_tag[i] = i + 1;
4344 phba->ctrl.mcc_numtag[i + 1] = 0;
4345 phba->ctrl.mcc_tag_available++;
4346 }
4347
4348 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4349
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304350 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
4351 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004352 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304353 if (!phba->wq) {
4354 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4355 "Failed to allocate work queue\n");
4356 goto free_twq;
4357 }
4358
4359 INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
4360
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304361 phwi_ctrlr = phba->phwi_ctrlr;
4362 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304363 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304364 for (i = 0; i < phba->num_cpus; i++) {
4365 pbe_eq = &phwi_context->be_eq[i];
4366 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4367 be_iopoll);
4368 blk_iopoll_enable(&pbe_eq->iopoll);
4369 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304370 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304371 ret = beiscsi_init_irqs(phba);
4372 if (ret < 0) {
4373 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4374 "Failed to beiscsi_init_irqs\n");
4375 goto free_blkenbld;
4376 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304377 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004378
4379 if (beiscsi_setup_boot_info(phba))
4380 /*
4381 * log error but continue, because we may not be using
4382 * iscsi boot.
4383 */
4384 shost_printk(KERN_ERR, phba->shost, "Could not set up "
4385 "iSCSI boot info.");
4386
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304387 SE_DEBUG(DBG_LVL_8, "\n\n\n SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304388 return 0;
4389
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304390free_blkenbld:
4391 destroy_workqueue(phba->wq);
4392 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304393 for (i = 0; i < phba->num_cpus; i++) {
4394 pbe_eq = &phwi_context->be_eq[i];
4395 blk_iopoll_disable(&pbe_eq->iopoll);
4396 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304397free_twq:
4398 beiscsi_clean_port(phba);
4399 beiscsi_free_mem(phba);
4400free_port:
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304401 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4402
4403 value = readl((void *)real_offset);
4404
4405 if (value & 0x00010000) {
4406 value &= 0xfffeffff;
4407 writel(value, (void *)real_offset);
4408 }
4409
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304410 pci_free_consistent(phba->pcidev,
4411 phba->ctrl.mbox_mem_alloced.size,
4412 phba->ctrl.mbox_mem_alloced.va,
4413 phba->ctrl.mbox_mem_alloced.dma);
4414 beiscsi_unmap_pci_function(phba);
4415hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304416 if (phba->msix_enabled)
4417 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304418 iscsi_host_remove(phba->shost);
4419 pci_dev_put(phba->pcidev);
4420 iscsi_host_free(phba->shost);
4421disable_pci:
4422 pci_disable_device(pcidev);
4423 return ret;
4424}
4425
4426struct iscsi_transport beiscsi_iscsi_transport = {
4427 .owner = THIS_MODULE,
4428 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05304429 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304430 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304431 .create_session = beiscsi_session_create,
4432 .destroy_session = beiscsi_session_destroy,
4433 .create_conn = beiscsi_conn_create,
4434 .bind_conn = beiscsi_conn_bind,
4435 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05004436 .attr_is_visible = be2iscsi_attr_is_visible,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304437 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004438 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304439 .get_session_param = iscsi_session_get_param,
4440 .get_host_param = beiscsi_get_host_param,
4441 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05004442 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304443 .send_pdu = iscsi_conn_send_pdu,
4444 .xmit_task = beiscsi_task_xmit,
4445 .cleanup_task = beiscsi_cleanup_task,
4446 .alloc_pdu = beiscsi_alloc_pdu,
4447 .parse_pdu_itt = beiscsi_parse_pdu,
4448 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004449 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304450 .ep_connect = beiscsi_ep_connect,
4451 .ep_poll = beiscsi_ep_poll,
4452 .ep_disconnect = beiscsi_ep_disconnect,
4453 .session_recovery_timedout = iscsi_session_recovery_timedout,
4454};
4455
4456static struct pci_driver beiscsi_pci_driver = {
4457 .name = DRV_NAME,
4458 .probe = beiscsi_dev_probe,
4459 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004460 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304461 .id_table = beiscsi_pci_id_table
4462};
4463
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304464
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304465static int __init beiscsi_module_init(void)
4466{
4467 int ret;
4468
4469 beiscsi_scsi_transport =
4470 iscsi_register_transport(&beiscsi_iscsi_transport);
4471 if (!beiscsi_scsi_transport) {
4472 SE_DEBUG(DBG_LVL_1,
4473 "beiscsi_module_init - Unable to register beiscsi"
4474 "transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05304475 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304476 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304477 SE_DEBUG(DBG_LVL_8, "In beiscsi_module_init, tt=%p\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304478 &beiscsi_iscsi_transport);
4479
4480 ret = pci_register_driver(&beiscsi_pci_driver);
4481 if (ret) {
4482 SE_DEBUG(DBG_LVL_1,
4483 "beiscsi_module_init - Unable to register"
4484 "beiscsi pci driver.\n");
4485 goto unregister_iscsi_transport;
4486 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304487 return 0;
4488
4489unregister_iscsi_transport:
4490 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4491 return ret;
4492}
4493
4494static void __exit beiscsi_module_exit(void)
4495{
4496 pci_unregister_driver(&beiscsi_pci_driver);
4497 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4498}
4499
4500module_init(beiscsi_module_init);
4501module_exit(beiscsi_module_exit);