blob: 64100247c6243d862d945af00e65fb9c52ac8607 [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 Gortmakeracf33682011-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);
51MODULE_AUTHOR("ServerEngines Corporation");
52MODULE_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;
150 int rc = FAILED;
151
152 /* invalidate iocbs */
153 cls_session = starget_to_session(scsi_target(sc->device));
154 session = cls_session->dd_data;
155 spin_lock_bh(&session->lock);
156 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
157 goto unlock;
158
159 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);
211unlock:
212 spin_unlock_bh(&session->lock);
213 return rc;
214}
215
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530216static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
217{
218 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500219 struct mgmt_session_info *boot_sess = &phba->boot_sess;
220 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530221 char *str = buf;
222 int rc;
223
224 switch (type) {
225 case ISCSI_BOOT_TGT_NAME:
226 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500227 (int)strlen(boot_sess->target_name),
228 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530229 break;
230 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500231 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530232 rc = sprintf(buf, "%pI4\n",
Mike Christief457a462011-06-24 15:11:53 -0500233 (char *)&boot_conn->dest_ipaddr.ip_address);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530234 else
235 rc = sprintf(str, "%pI6\n",
Mike Christief457a462011-06-24 15:11:53 -0500236 (char *)&boot_conn->dest_ipaddr.ip_address);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530237 break;
238 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500239 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530240 break;
241
242 case ISCSI_BOOT_TGT_CHAP_NAME:
243 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500244 boot_conn->negotiated_login_options.auth_data.chap.
245 target_chap_name_length,
246 (char *)&boot_conn->negotiated_login_options.
247 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530248 break;
249 case ISCSI_BOOT_TGT_CHAP_SECRET:
250 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500251 boot_conn->negotiated_login_options.auth_data.chap.
252 target_secret_length,
253 (char *)&boot_conn->negotiated_login_options.
254 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530255 break;
256 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
257 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500258 boot_conn->negotiated_login_options.auth_data.chap.
259 intr_chap_name_length,
260 (char *)&boot_conn->negotiated_login_options.
261 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530262 break;
263 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500264 rc = sprintf(str, "%.*s\n",
265 boot_conn->negotiated_login_options.auth_data.chap.
266 intr_secret_length,
267 (char *)&boot_conn->negotiated_login_options.
268 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530269 break;
270 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500271 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530272 break;
273 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500274 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530275 break;
276 default:
277 rc = -ENOSYS;
278 break;
279 }
280 return rc;
281}
282
283static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
284{
285 struct beiscsi_hba *phba = data;
286 char *str = buf;
287 int rc;
288
289 switch (type) {
290 case ISCSI_BOOT_INI_INITIATOR_NAME:
291 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
292 break;
293 default:
294 rc = -ENOSYS;
295 break;
296 }
297 return rc;
298}
299
300static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
301{
302 struct beiscsi_hba *phba = data;
303 char *str = buf;
304 int rc;
305
306 switch (type) {
307 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500308 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530309 break;
310 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500311 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530312 break;
313 case ISCSI_BOOT_ETH_MAC:
314 rc = beiscsi_get_macaddr(buf, phba);
315 if (rc < 0) {
316 SE_DEBUG(DBG_LVL_1, "beiscsi_get_macaddr Failed\n");
317 return rc;
318 }
319 break;
320 default:
321 rc = -ENOSYS;
322 break;
323 }
324 return rc;
325}
326
327
Al Viro587a1f12011-07-23 23:11:19 -0400328static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530329{
Al Viro587a1f12011-07-23 23:11:19 -0400330 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530331
332 switch (type) {
333 case ISCSI_BOOT_TGT_NAME:
334 case ISCSI_BOOT_TGT_IP_ADDR:
335 case ISCSI_BOOT_TGT_PORT:
336 case ISCSI_BOOT_TGT_CHAP_NAME:
337 case ISCSI_BOOT_TGT_CHAP_SECRET:
338 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
339 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
340 case ISCSI_BOOT_TGT_NIC_ASSOC:
341 case ISCSI_BOOT_TGT_FLAGS:
342 rc = S_IRUGO;
343 break;
344 default:
345 rc = 0;
346 break;
347 }
348 return rc;
349}
350
Al Viro587a1f12011-07-23 23:11:19 -0400351static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530352{
Al Viro587a1f12011-07-23 23:11:19 -0400353 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530354
355 switch (type) {
356 case ISCSI_BOOT_INI_INITIATOR_NAME:
357 rc = S_IRUGO;
358 break;
359 default:
360 rc = 0;
361 break;
362 }
363 return rc;
364}
365
366
Al Viro587a1f12011-07-23 23:11:19 -0400367static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530368{
Al Viro587a1f12011-07-23 23:11:19 -0400369 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530370
371 switch (type) {
372 case ISCSI_BOOT_ETH_FLAGS:
373 case ISCSI_BOOT_ETH_MAC:
374 case ISCSI_BOOT_ETH_INDEX:
375 rc = S_IRUGO;
376 break;
377 default:
378 rc = 0;
379 break;
380 }
381 return rc;
382}
383
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530384/*------------------- PCI Driver operations and data ----------------- */
385static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
386 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530387 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530388 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
389 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
390 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530391 { 0 }
392};
393MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
394
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530395static struct scsi_host_template beiscsi_sht = {
396 .module = THIS_MODULE,
397 .name = "ServerEngines 10Gbe open-iscsi Initiator Driver",
398 .proc_name = DRV_NAME,
399 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530400 .change_queue_depth = iscsi_change_queue_depth,
401 .slave_configure = beiscsi_slave_configure,
402 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530403 .eh_abort_handler = beiscsi_eh_abort,
404 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530405 .eh_target_reset_handler = iscsi_eh_session_reset,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530406 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
407 .can_queue = BE2_IO_DEPTH,
408 .this_id = -1,
409 .max_sectors = BEISCSI_MAX_SECTORS,
410 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
411 .use_clustering = ENABLE_CLUSTERING,
412};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530413
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530414static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530415
416static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
417{
418 struct beiscsi_hba *phba;
419 struct Scsi_Host *shost;
420
421 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
422 if (!shost) {
423 dev_err(&pcidev->dev, "beiscsi_hba_alloc -"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530424 "iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530425 return NULL;
426 }
427 shost->dma_boundary = pcidev->dma_mask;
428 shost->max_id = BE2_MAX_SESSIONS;
429 shost->max_channel = 0;
430 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
431 shost->max_lun = BEISCSI_NUM_MAX_LUN;
432 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530433 phba = iscsi_host_priv(shost);
434 memset(phba, 0, sizeof(*phba));
435 phba->shost = shost;
436 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530437 pci_set_drvdata(pcidev, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530438
439 if (iscsi_host_add(shost, &phba->pcidev->dev))
440 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530441
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530442 return phba;
443
444free_devices:
445 pci_dev_put(phba->pcidev);
446 iscsi_host_free(phba->shost);
447 return NULL;
448}
449
450static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
451{
452 if (phba->csr_va) {
453 iounmap(phba->csr_va);
454 phba->csr_va = NULL;
455 }
456 if (phba->db_va) {
457 iounmap(phba->db_va);
458 phba->db_va = NULL;
459 }
460 if (phba->pci_va) {
461 iounmap(phba->pci_va);
462 phba->pci_va = NULL;
463 }
464}
465
466static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
467 struct pci_dev *pcidev)
468{
469 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530470 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530471
472 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
473 pci_resource_len(pcidev, 2));
474 if (addr == NULL)
475 return -ENOMEM;
476 phba->ctrl.csr = addr;
477 phba->csr_va = addr;
478 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
479
480 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
481 if (addr == NULL)
482 goto pci_map_err;
483 phba->ctrl.db = addr;
484 phba->db_va = addr;
485 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
486
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530487 if (phba->generation == BE_GEN2)
488 pcicfg_reg = 1;
489 else
490 pcicfg_reg = 0;
491
492 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
493 pci_resource_len(pcidev, pcicfg_reg));
494
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530495 if (addr == NULL)
496 goto pci_map_err;
497 phba->ctrl.pcicfg = addr;
498 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530499 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530500 return 0;
501
502pci_map_err:
503 beiscsi_unmap_pci_function(phba);
504 return -ENOMEM;
505}
506
507static int beiscsi_enable_pci(struct pci_dev *pcidev)
508{
509 int ret;
510
511 ret = pci_enable_device(pcidev);
512 if (ret) {
513 dev_err(&pcidev->dev, "beiscsi_enable_pci - enable device "
514 "failed. Returning -ENODEV\n");
515 return ret;
516 }
517
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530518 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530519 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
520 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
521 if (ret) {
522 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
523 pci_disable_device(pcidev);
524 return ret;
525 }
526 }
527 return 0;
528}
529
530static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
531{
532 struct be_ctrl_info *ctrl = &phba->ctrl;
533 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
534 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
535 int status = 0;
536
537 ctrl->pdev = pdev;
538 status = beiscsi_map_pci_bars(phba, pdev);
539 if (status)
540 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530541 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
542 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
543 mbox_mem_alloc->size,
544 &mbox_mem_alloc->dma);
545 if (!mbox_mem_alloc->va) {
546 beiscsi_unmap_pci_function(phba);
547 status = -ENOMEM;
548 return status;
549 }
550
551 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
552 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
553 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
554 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
555 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530556 spin_lock_init(&phba->ctrl.mcc_lock);
557 spin_lock_init(&phba->ctrl.mcc_cq_lock);
558
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530559 return status;
560}
561
562static void beiscsi_get_params(struct beiscsi_hba *phba)
563{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530564 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
565 - (phba->fw_config.iscsi_cid_count
566 + BE2_TMFS
567 + BE2_NOPOUT_REQ));
568 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530569 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700570 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530571 phba->params.num_sge_per_io = BE2_SGE;
572 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
573 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
574 phba->params.eq_timer = 64;
575 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530576 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
577 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530578 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
579 ? 1024 : phba->params.num_eq_entries;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530580 SE_DEBUG(DBG_LVL_8, "phba->params.num_eq_entries=%d\n",
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530581 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530582 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530583 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
584 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530585 phba->params.wrbs_per_cxn = 256;
586}
587
588static void hwi_ring_eq_db(struct beiscsi_hba *phba,
589 unsigned int id, unsigned int clr_interrupt,
590 unsigned int num_processed,
591 unsigned char rearm, unsigned char event)
592{
593 u32 val = 0;
594 val |= id & DB_EQ_RING_ID_MASK;
595 if (rearm)
596 val |= 1 << DB_EQ_REARM_SHIFT;
597 if (clr_interrupt)
598 val |= 1 << DB_EQ_CLR_SHIFT;
599 if (event)
600 val |= 1 << DB_EQ_EVNT_SHIFT;
601 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
602 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
603}
604
605/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530606 * be_isr_mcc - The isr routine of the driver.
607 * @irq: Not used
608 * @dev_id: Pointer to host adapter structure
609 */
610static irqreturn_t be_isr_mcc(int irq, void *dev_id)
611{
612 struct beiscsi_hba *phba;
613 struct be_eq_entry *eqe = NULL;
614 struct be_queue_info *eq;
615 struct be_queue_info *mcc;
616 unsigned int num_eq_processed;
617 struct be_eq_obj *pbe_eq;
618 unsigned long flags;
619
620 pbe_eq = dev_id;
621 eq = &pbe_eq->q;
622 phba = pbe_eq->phba;
623 mcc = &phba->ctrl.mcc_obj.cq;
624 eqe = queue_tail_node(eq);
625 if (!eqe)
626 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
627
628 num_eq_processed = 0;
629
630 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
631 & EQE_VALID_MASK) {
632 if (((eqe->dw[offsetof(struct amap_eq_entry,
633 resource_id) / 32] &
634 EQE_RESID_MASK) >> 16) == mcc->id) {
635 spin_lock_irqsave(&phba->isr_lock, flags);
636 phba->todo_mcc_cq = 1;
637 spin_unlock_irqrestore(&phba->isr_lock, flags);
638 }
639 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
640 queue_tail_inc(eq);
641 eqe = queue_tail_node(eq);
642 num_eq_processed++;
643 }
644 if (phba->todo_mcc_cq)
645 queue_work(phba->wq, &phba->work_cqs);
646 if (num_eq_processed)
647 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
648
649 return IRQ_HANDLED;
650}
651
652/**
653 * be_isr_msix - The isr routine of the driver.
654 * @irq: Not used
655 * @dev_id: Pointer to host adapter structure
656 */
657static irqreturn_t be_isr_msix(int irq, void *dev_id)
658{
659 struct beiscsi_hba *phba;
660 struct be_eq_entry *eqe = NULL;
661 struct be_queue_info *eq;
662 struct be_queue_info *cq;
663 unsigned int num_eq_processed;
664 struct be_eq_obj *pbe_eq;
665 unsigned long flags;
666
667 pbe_eq = dev_id;
668 eq = &pbe_eq->q;
669 cq = pbe_eq->cq;
670 eqe = queue_tail_node(eq);
671 if (!eqe)
672 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
673
674 phba = pbe_eq->phba;
675 num_eq_processed = 0;
676 if (blk_iopoll_enabled) {
677 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
678 & EQE_VALID_MASK) {
679 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
680 blk_iopoll_sched(&pbe_eq->iopoll);
681
682 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
683 queue_tail_inc(eq);
684 eqe = queue_tail_node(eq);
685 num_eq_processed++;
686 }
687 if (num_eq_processed)
688 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
689
690 return IRQ_HANDLED;
691 } else {
692 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
693 & EQE_VALID_MASK) {
694 spin_lock_irqsave(&phba->isr_lock, flags);
695 phba->todo_cq = 1;
696 spin_unlock_irqrestore(&phba->isr_lock, flags);
697 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
698 queue_tail_inc(eq);
699 eqe = queue_tail_node(eq);
700 num_eq_processed++;
701 }
702 if (phba->todo_cq)
703 queue_work(phba->wq, &phba->work_cqs);
704
705 if (num_eq_processed)
706 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
707
708 return IRQ_HANDLED;
709 }
710}
711
712/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530713 * be_isr - The isr routine of the driver.
714 * @irq: Not used
715 * @dev_id: Pointer to host adapter structure
716 */
717static irqreturn_t be_isr(int irq, void *dev_id)
718{
719 struct beiscsi_hba *phba;
720 struct hwi_controller *phwi_ctrlr;
721 struct hwi_context_memory *phwi_context;
722 struct be_eq_entry *eqe = NULL;
723 struct be_queue_info *eq;
724 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530725 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530726 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530727 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530728 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530729 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530730 int isr;
731
732 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700733 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530734 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
735 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
736 if (!isr)
737 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530738
739 phwi_ctrlr = phba->phwi_ctrlr;
740 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530741 pbe_eq = &phwi_context->be_eq[0];
742
743 eq = &phwi_context->be_eq[0].q;
744 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530745 index = 0;
746 eqe = queue_tail_node(eq);
747 if (!eqe)
748 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
749
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530750 num_ioeq_processed = 0;
751 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530752 if (blk_iopoll_enabled) {
753 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
754 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530755 if (((eqe->dw[offsetof(struct amap_eq_entry,
756 resource_id) / 32] &
757 EQE_RESID_MASK) >> 16) == mcc->id) {
758 spin_lock_irqsave(&phba->isr_lock, flags);
759 phba->todo_mcc_cq = 1;
760 spin_unlock_irqrestore(&phba->isr_lock, flags);
761 num_mcceq_processed++;
762 } else {
763 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
764 blk_iopoll_sched(&pbe_eq->iopoll);
765 num_ioeq_processed++;
766 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530767 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
768 queue_tail_inc(eq);
769 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530770 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530771 if (num_ioeq_processed || num_mcceq_processed) {
772 if (phba->todo_mcc_cq)
773 queue_work(phba->wq, &phba->work_cqs);
774
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530775 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530776 hwi_ring_eq_db(phba, eq->id, 0,
777 (num_ioeq_processed +
778 num_mcceq_processed) , 1, 1);
779 else
780 hwi_ring_eq_db(phba, eq->id, 0,
781 (num_ioeq_processed +
782 num_mcceq_processed), 0, 1);
783
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530784 return IRQ_HANDLED;
785 } else
786 return IRQ_NONE;
787 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530788 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530789 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
790 & EQE_VALID_MASK) {
791
792 if (((eqe->dw[offsetof(struct amap_eq_entry,
793 resource_id) / 32] &
794 EQE_RESID_MASK) >> 16) != cq->id) {
795 spin_lock_irqsave(&phba->isr_lock, flags);
796 phba->todo_mcc_cq = 1;
797 spin_unlock_irqrestore(&phba->isr_lock, flags);
798 } else {
799 spin_lock_irqsave(&phba->isr_lock, flags);
800 phba->todo_cq = 1;
801 spin_unlock_irqrestore(&phba->isr_lock, flags);
802 }
803 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
804 queue_tail_inc(eq);
805 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530806 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530807 }
808 if (phba->todo_cq || phba->todo_mcc_cq)
809 queue_work(phba->wq, &phba->work_cqs);
810
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530811 if (num_ioeq_processed) {
812 hwi_ring_eq_db(phba, eq->id, 0,
813 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530814 return IRQ_HANDLED;
815 } else
816 return IRQ_NONE;
817 }
818}
819
820static int beiscsi_init_irqs(struct beiscsi_hba *phba)
821{
822 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530823 struct hwi_controller *phwi_ctrlr;
824 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530825 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530826
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530827 phwi_ctrlr = phba->phwi_ctrlr;
828 phwi_context = phwi_ctrlr->phwi_ctxt;
829
830 if (phba->msix_enabled) {
831 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700832 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
833 GFP_KERNEL);
834 if (!phba->msi_name[i]) {
835 ret = -ENOMEM;
836 goto free_msix_irqs;
837 }
838
839 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
840 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530841 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700842 ret = request_irq(msix_vec, be_isr_msix, 0,
843 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530844 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530845 if (ret) {
846 shost_printk(KERN_ERR, phba->shost,
847 "beiscsi_init_irqs-Failed to"
848 "register msix for i = %d\n", i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700849 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530850 goto free_msix_irqs;
851 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530852 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700853 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
854 if (!phba->msi_name[i]) {
855 ret = -ENOMEM;
856 goto free_msix_irqs;
857 }
858 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
859 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530860 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700861 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530862 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530863 if (ret) {
864 shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
865 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700866 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530867 goto free_msix_irqs;
868 }
869
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530870 } else {
871 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
872 "beiscsi", phba);
873 if (ret) {
874 shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
875 "Failed to register irq\\n");
876 return ret;
877 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530878 }
879 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530880free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700881 for (j = i - 1; j >= 0; j--) {
882 kfree(phba->msi_name[j]);
883 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530884 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700885 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530886 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530887}
888
889static void hwi_ring_cq_db(struct beiscsi_hba *phba,
890 unsigned int id, unsigned int num_processed,
891 unsigned char rearm, unsigned char event)
892{
893 u32 val = 0;
894 val |= id & DB_CQ_RING_ID_MASK;
895 if (rearm)
896 val |= 1 << DB_CQ_REARM_SHIFT;
897 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
898 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
899}
900
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530901static unsigned int
902beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
903 struct beiscsi_hba *phba,
904 unsigned short cid,
905 struct pdu_base *ppdu,
906 unsigned long pdu_len,
907 void *pbuffer, unsigned long buf_len)
908{
909 struct iscsi_conn *conn = beiscsi_conn->conn;
910 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530911 struct iscsi_task *task;
912 struct beiscsi_io_task *io_task;
913 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530914
915 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
916 PDUBASE_OPCODE_MASK) {
917 case ISCSI_OP_NOOP_IN:
918 pbuffer = NULL;
919 buf_len = 0;
920 break;
921 case ISCSI_OP_ASYNC_EVENT:
922 break;
923 case ISCSI_OP_REJECT:
924 WARN_ON(!pbuffer);
925 WARN_ON(!(buf_len == 48));
926 SE_DEBUG(DBG_LVL_1, "In ISCSI_OP_REJECT\n");
927 break;
928 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +0530929 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530930 task = conn->login_task;
931 io_task = task->dd_data;
932 login_hdr = (struct iscsi_hdr *)ppdu;
933 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530934 break;
935 default:
936 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530937 "Unrecognized opcode 0x%x in async msg\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530938 (ppdu->
939 dw[offsetof(struct amap_pdu_base, opcode) / 32]
940 & PDUBASE_OPCODE_MASK));
941 return 1;
942 }
943
944 spin_lock_bh(&session->lock);
945 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
946 spin_unlock_bh(&session->lock);
947 return 0;
948}
949
950static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
951{
952 struct sgl_handle *psgl_handle;
953
954 if (phba->io_sgl_hndl_avbl) {
955 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530956 "In alloc_io_sgl_handle,io_sgl_alloc_index=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530957 phba->io_sgl_alloc_index);
958 psgl_handle = phba->io_sgl_hndl_base[phba->
959 io_sgl_alloc_index];
960 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
961 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530962 if (phba->io_sgl_alloc_index == (phba->params.
963 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530964 phba->io_sgl_alloc_index = 0;
965 else
966 phba->io_sgl_alloc_index++;
967 } else
968 psgl_handle = NULL;
969 return psgl_handle;
970}
971
972static void
973free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
974{
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530975 SE_DEBUG(DBG_LVL_8, "In free_,io_sgl_free_index=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530976 phba->io_sgl_free_index);
977 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
978 /*
979 * this can happen if clean_task is called on a task that
980 * failed in xmit_task or alloc_pdu.
981 */
982 SE_DEBUG(DBG_LVL_8,
983 "Double Free in IO SGL io_sgl_free_index=%d,"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530984 "value there=%p\n", phba->io_sgl_free_index,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530985 phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
986 return;
987 }
988 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
989 phba->io_sgl_hndl_avbl++;
990 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
991 phba->io_sgl_free_index = 0;
992 else
993 phba->io_sgl_free_index++;
994}
995
996/**
997 * alloc_wrb_handle - To allocate a wrb handle
998 * @phba: The hba pointer
999 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301000 *
1001 * This happens under session_lock until submission to chip
1002 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301003struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301004{
1005 struct hwi_wrb_context *pwrb_context;
1006 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301007 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301008
1009 phwi_ctrlr = phba->phwi_ctrlr;
1010 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301011 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301012 pwrb_handle = pwrb_context->pwrb_handle_base[
1013 pwrb_context->alloc_index];
1014 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301015 if (pwrb_context->alloc_index ==
1016 (phba->params.wrbs_per_cxn - 1))
1017 pwrb_context->alloc_index = 0;
1018 else
1019 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301020 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
1021 pwrb_context->alloc_index];
1022 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301023 } else
1024 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301025 return pwrb_handle;
1026}
1027
1028/**
1029 * free_wrb_handle - To free the wrb handle back to pool
1030 * @phba: The hba pointer
1031 * @pwrb_context: The context to free from
1032 * @pwrb_handle: The wrb_handle to free
1033 *
1034 * This happens under session_lock until submission to chip
1035 */
1036static void
1037free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1038 struct wrb_handle *pwrb_handle)
1039{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301040 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301041 pwrb_context->wrb_handles_available++;
1042 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
1043 pwrb_context->free_index = 0;
1044 else
1045 pwrb_context->free_index++;
1046
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301047 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301048 "FREE WRB: pwrb_handle=%p free_index=0x%x"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301049 "wrb_handles_available=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301050 pwrb_handle, pwrb_context->free_index,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301051 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301052}
1053
1054static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1055{
1056 struct sgl_handle *psgl_handle;
1057
1058 if (phba->eh_sgl_hndl_avbl) {
1059 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1060 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301061 SE_DEBUG(DBG_LVL_8, "mgmt_sgl_alloc_index=%d=0x%x\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301062 phba->eh_sgl_alloc_index, phba->eh_sgl_alloc_index);
1063 phba->eh_sgl_hndl_avbl--;
1064 if (phba->eh_sgl_alloc_index ==
1065 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1066 1))
1067 phba->eh_sgl_alloc_index = 0;
1068 else
1069 phba->eh_sgl_alloc_index++;
1070 } else
1071 psgl_handle = NULL;
1072 return psgl_handle;
1073}
1074
1075void
1076free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1077{
1078
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301079 SE_DEBUG(DBG_LVL_8, "In free_mgmt_sgl_handle,eh_sgl_free_index=%d\n",
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301080 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301081 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1082 /*
1083 * this can happen if clean_task is called on a task that
1084 * failed in xmit_task or alloc_pdu.
1085 */
1086 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301087 "Double Free in eh SGL ,eh_sgl_free_index=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301088 phba->eh_sgl_free_index);
1089 return;
1090 }
1091 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1092 phba->eh_sgl_hndl_avbl++;
1093 if (phba->eh_sgl_free_index ==
1094 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1095 phba->eh_sgl_free_index = 0;
1096 else
1097 phba->eh_sgl_free_index++;
1098}
1099
1100static void
1101be_complete_io(struct beiscsi_conn *beiscsi_conn,
1102 struct iscsi_task *task, struct sol_cqe *psol)
1103{
1104 struct beiscsi_io_task *io_task = task->dd_data;
1105 struct be_status_bhs *sts_bhs =
1106 (struct be_status_bhs *)io_task->cmd_bhs;
1107 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301108 unsigned char *sense;
1109 u32 resid = 0, exp_cmdsn, max_cmdsn;
1110 u8 rsp, status, flags;
1111
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301112 exp_cmdsn = (psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301113 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1114 & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301115 max_cmdsn = ((psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301116 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1117 & SOL_EXP_CMD_SN_MASK) +
1118 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1119 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1120 rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
1121 & SOL_RESP_MASK) >> 16);
1122 status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
1123 & SOL_STS_MASK) >> 8);
1124 flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1125 & SOL_FLAGS_MASK) >> 24) | 0x80;
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001126 if (!task->sc) {
1127 if (io_task->scsi_cmnd)
1128 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301129
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001130 return;
1131 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301132 task->sc->result = (DID_OK << 16) | status;
1133 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1134 task->sc->result = DID_ERROR << 16;
1135 goto unmap;
1136 }
1137
1138 /* bidi not initially supported */
1139 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
1140 resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
1141 32] & SOL_RES_CNT_MASK);
1142
1143 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1144 task->sc->result = DID_ERROR << 16;
1145
1146 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1147 scsi_set_resid(task->sc, resid);
1148 if (!status && (scsi_bufflen(task->sc) - resid <
1149 task->sc->underflow))
1150 task->sc->result = DID_ERROR << 16;
1151 }
1152 }
1153
1154 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001155 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301156 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001157
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301158 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001159 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301160 memcpy(task->sc->sense_buffer, sense,
1161 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1162 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301163
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301164 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
1165 if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1166 & SOL_RES_CNT_MASK)
1167 conn->rxdata_octets += (psol->
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301168 dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1169 & SOL_RES_CNT_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301170 }
1171unmap:
1172 scsi_dma_unmap(io_task->scsi_cmnd);
1173 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1174}
1175
1176static void
1177be_complete_logout(struct beiscsi_conn *beiscsi_conn,
1178 struct iscsi_task *task, struct sol_cqe *psol)
1179{
1180 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301181 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301182 struct iscsi_conn *conn = beiscsi_conn->conn;
1183
1184 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301185 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301186 hdr->t2wait = 5;
1187 hdr->t2retain = 0;
1188 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1189 & SOL_FLAGS_MASK) >> 24) | 0x80;
1190 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1191 32] & SOL_RESP_MASK);
1192 hdr->exp_cmdsn = cpu_to_be32(psol->
1193 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1194 & SOL_EXP_CMD_SN_MASK);
1195 hdr->max_cmdsn = be32_to_cpu((psol->
1196 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1197 & SOL_EXP_CMD_SN_MASK) +
1198 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1199 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301200 hdr->dlength[0] = 0;
1201 hdr->dlength[1] = 0;
1202 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301203 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301204 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301205 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1206}
1207
1208static void
1209be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
1210 struct iscsi_task *task, struct sol_cqe *psol)
1211{
1212 struct iscsi_tm_rsp *hdr;
1213 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301214 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301215
1216 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301217 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301218 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1219 & SOL_FLAGS_MASK) >> 24) | 0x80;
1220 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1221 32] & SOL_RESP_MASK);
1222 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301223 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301224 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1225 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1226 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1227 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301228 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301229 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1230}
1231
1232static void
1233hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1234 struct beiscsi_hba *phba, struct sol_cqe *psol)
1235{
1236 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301237 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301238 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301239 struct iscsi_task *task;
1240 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301241 struct iscsi_conn *conn = beiscsi_conn->conn;
1242 struct iscsi_session *session = conn->session;
1243
1244 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301245 pwrb_context = &phwi_ctrlr->wrb_context[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301246 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301247 SOL_CID_MASK) >> 6) -
1248 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301249 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301250 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1251 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301252 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301253
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301254 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001255 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301256 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001257 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301258 spin_lock_bh(&session->lock);
1259 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1260 spin_unlock_bh(&session->lock);
1261}
1262
1263static void
1264be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
1265 struct iscsi_task *task, struct sol_cqe *psol)
1266{
1267 struct iscsi_nopin *hdr;
1268 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301269 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301270
1271 hdr = (struct iscsi_nopin *)task->hdr;
1272 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1273 & SOL_FLAGS_MASK) >> 24) | 0x80;
1274 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
1275 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
1276 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1277 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1278 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1279 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1280 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301281 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301282 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1283}
1284
1285static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1286 struct beiscsi_hba *phba, struct sol_cqe *psol)
1287{
1288 struct hwi_wrb_context *pwrb_context;
1289 struct wrb_handle *pwrb_handle;
1290 struct iscsi_wrb *pwrb = NULL;
1291 struct hwi_controller *phwi_ctrlr;
1292 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301293 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301294 struct iscsi_conn *conn = beiscsi_conn->conn;
1295 struct iscsi_session *session = conn->session;
1296
1297 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301298 pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301299 (struct amap_sol_cqe, cid) / 32]
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301300 & SOL_CID_MASK) >> 6) -
1301 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301302 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301303 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1304 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301305 task = pwrb_handle->pio_handle;
1306 pwrb = pwrb_handle->pwrb;
1307 type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
1308 WRB_TYPE_MASK) >> 28;
1309
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301310 spin_lock_bh(&session->lock);
1311 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301312 case HWH_TYPE_IO:
1313 case HWH_TYPE_IO_RD:
1314 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301315 ISCSI_OP_NOOP_OUT)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301316 be_complete_nopin_resp(beiscsi_conn, task, psol);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301317 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301318 be_complete_io(beiscsi_conn, task, psol);
1319 break;
1320
1321 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301322 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
1323 be_complete_logout(beiscsi_conn, task, psol);
1324 else
1325 be_complete_tmf(beiscsi_conn, task, psol);
1326
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301327 break;
1328
1329 case HWH_TYPE_LOGIN:
1330 SE_DEBUG(DBG_LVL_1,
1331 "\t\t No HWH_TYPE_LOGIN Expected in hwi_complete_cmd"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301332 "- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301333 break;
1334
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301335 case HWH_TYPE_NOP:
1336 be_complete_nopin_resp(beiscsi_conn, task, psol);
1337 break;
1338
1339 default:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301340 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301341 "In hwi_complete_cmd, unknown type = %d"
1342 "wrb_index 0x%x CID 0x%x\n", type,
1343 ((psol->dw[offsetof(struct amap_iscsi_wrb,
1344 type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
1345 ((psol->dw[offsetof(struct amap_sol_cqe,
1346 cid) / 32] & SOL_CID_MASK) >> 6));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301347 break;
1348 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301349
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301350 spin_unlock_bh(&session->lock);
1351}
1352
1353static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1354 *pasync_ctx, unsigned int is_header,
1355 unsigned int host_write_ptr)
1356{
1357 if (is_header)
1358 return &pasync_ctx->async_entry[host_write_ptr].
1359 header_busy_list;
1360 else
1361 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1362}
1363
1364static struct async_pdu_handle *
1365hwi_get_async_handle(struct beiscsi_hba *phba,
1366 struct beiscsi_conn *beiscsi_conn,
1367 struct hwi_async_pdu_context *pasync_ctx,
1368 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1369{
1370 struct be_bus_address phys_addr;
1371 struct list_head *pbusy_list;
1372 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301373 unsigned char is_header = 0;
1374
1375 phys_addr.u.a32.address_lo =
1376 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
1377 ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1378 & PDUCQE_DPL_MASK) >> 16);
1379 phys_addr.u.a32.address_hi =
1380 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
1381
1382 phys_addr.u.a64.address =
1383 *((unsigned long long *)(&phys_addr.u.a64.address));
1384
1385 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1386 & PDUCQE_CODE_MASK) {
1387 case UNSOL_HDR_NOTIFY:
1388 is_header = 1;
1389
1390 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
1391 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1392 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301393 break;
1394 case UNSOL_DATA_NOTIFY:
1395 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
1396 dw[offsetof(struct amap_i_t_dpdu_cqe,
1397 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301398 break;
1399 default:
1400 pbusy_list = NULL;
1401 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301402 "Unexpected code=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301403 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1404 code) / 32] & PDUCQE_CODE_MASK);
1405 return NULL;
1406 }
1407
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301408 WARN_ON(list_empty(pbusy_list));
1409 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001410 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301411 break;
1412 }
1413
1414 WARN_ON(!pasync_handle);
1415
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301416 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1417 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301418 pasync_handle->is_header = is_header;
1419 pasync_handle->buffer_len = ((pdpdu_cqe->
1420 dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1421 & PDUCQE_DPL_MASK) >> 16);
1422
1423 *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1424 index) / 32] & PDUCQE_INDEX_MASK);
1425 return pasync_handle;
1426}
1427
1428static unsigned int
1429hwi_update_async_writables(struct hwi_async_pdu_context *pasync_ctx,
1430 unsigned int is_header, unsigned int cq_index)
1431{
1432 struct list_head *pbusy_list;
1433 struct async_pdu_handle *pasync_handle;
1434 unsigned int num_entries, writables = 0;
1435 unsigned int *pep_read_ptr, *pwritables;
1436
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001437 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301438 if (is_header) {
1439 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1440 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301441 } else {
1442 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1443 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301444 }
1445
1446 while ((*pep_read_ptr) != cq_index) {
1447 (*pep_read_ptr)++;
1448 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1449
1450 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1451 *pep_read_ptr);
1452 if (writables == 0)
1453 WARN_ON(list_empty(pbusy_list));
1454
1455 if (!list_empty(pbusy_list)) {
1456 pasync_handle = list_entry(pbusy_list->next,
1457 struct async_pdu_handle,
1458 link);
1459 WARN_ON(!pasync_handle);
1460 pasync_handle->consumed = 1;
1461 }
1462
1463 writables++;
1464 }
1465
1466 if (!writables) {
1467 SE_DEBUG(DBG_LVL_1,
1468 "Duplicate notification received - index 0x%x!!\n",
1469 cq_index);
1470 WARN_ON(1);
1471 }
1472
1473 *pwritables = *pwritables + writables;
1474 return 0;
1475}
1476
1477static unsigned int hwi_free_async_msg(struct beiscsi_hba *phba,
1478 unsigned int cri)
1479{
1480 struct hwi_controller *phwi_ctrlr;
1481 struct hwi_async_pdu_context *pasync_ctx;
1482 struct async_pdu_handle *pasync_handle, *tmp_handle;
1483 struct list_head *plist;
1484 unsigned int i = 0;
1485
1486 phwi_ctrlr = phba->phwi_ctrlr;
1487 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1488
1489 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1490
1491 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1492 list_del(&pasync_handle->link);
1493
1494 if (i == 0) {
1495 list_add_tail(&pasync_handle->link,
1496 &pasync_ctx->async_header.free_list);
1497 pasync_ctx->async_header.free_entries++;
1498 i++;
1499 } else {
1500 list_add_tail(&pasync_handle->link,
1501 &pasync_ctx->async_data.free_list);
1502 pasync_ctx->async_data.free_entries++;
1503 i++;
1504 }
1505 }
1506
1507 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1508 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1509 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1510 return 0;
1511}
1512
1513static struct phys_addr *
1514hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1515 unsigned int is_header, unsigned int host_write_ptr)
1516{
1517 struct phys_addr *pasync_sge = NULL;
1518
1519 if (is_header)
1520 pasync_sge = pasync_ctx->async_header.ring_base;
1521 else
1522 pasync_sge = pasync_ctx->async_data.ring_base;
1523
1524 return pasync_sge + host_write_ptr;
1525}
1526
1527static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1528 unsigned int is_header)
1529{
1530 struct hwi_controller *phwi_ctrlr;
1531 struct hwi_async_pdu_context *pasync_ctx;
1532 struct async_pdu_handle *pasync_handle;
1533 struct list_head *pfree_link, *pbusy_list;
1534 struct phys_addr *pasync_sge;
1535 unsigned int ring_id, num_entries;
1536 unsigned int host_write_num;
1537 unsigned int writables;
1538 unsigned int i = 0;
1539 u32 doorbell = 0;
1540
1541 phwi_ctrlr = phba->phwi_ctrlr;
1542 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001543 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301544
1545 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301546 writables = min(pasync_ctx->async_header.writables,
1547 pasync_ctx->async_header.free_entries);
1548 pfree_link = pasync_ctx->async_header.free_list.next;
1549 host_write_num = pasync_ctx->async_header.host_write_ptr;
1550 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1551 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301552 writables = min(pasync_ctx->async_data.writables,
1553 pasync_ctx->async_data.free_entries);
1554 pfree_link = pasync_ctx->async_data.free_list.next;
1555 host_write_num = pasync_ctx->async_data.host_write_ptr;
1556 ring_id = phwi_ctrlr->default_pdu_data.id;
1557 }
1558
1559 writables = (writables / 8) * 8;
1560 if (writables) {
1561 for (i = 0; i < writables; i++) {
1562 pbusy_list =
1563 hwi_get_async_busy_list(pasync_ctx, is_header,
1564 host_write_num);
1565 pasync_handle =
1566 list_entry(pfree_link, struct async_pdu_handle,
1567 link);
1568 WARN_ON(!pasync_handle);
1569 pasync_handle->consumed = 0;
1570
1571 pfree_link = pfree_link->next;
1572
1573 pasync_sge = hwi_get_ring_address(pasync_ctx,
1574 is_header, host_write_num);
1575
1576 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1577 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1578
1579 list_move(&pasync_handle->link, pbusy_list);
1580
1581 host_write_num++;
1582 host_write_num = host_write_num % num_entries;
1583 }
1584
1585 if (is_header) {
1586 pasync_ctx->async_header.host_write_ptr =
1587 host_write_num;
1588 pasync_ctx->async_header.free_entries -= writables;
1589 pasync_ctx->async_header.writables -= writables;
1590 pasync_ctx->async_header.busy_entries += writables;
1591 } else {
1592 pasync_ctx->async_data.host_write_ptr = host_write_num;
1593 pasync_ctx->async_data.free_entries -= writables;
1594 pasync_ctx->async_data.writables -= writables;
1595 pasync_ctx->async_data.busy_entries += writables;
1596 }
1597
1598 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1599 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1600 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1601 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1602 << DB_DEF_PDU_CQPROC_SHIFT;
1603
1604 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1605 }
1606}
1607
1608static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1609 struct beiscsi_conn *beiscsi_conn,
1610 struct i_t_dpdu_cqe *pdpdu_cqe)
1611{
1612 struct hwi_controller *phwi_ctrlr;
1613 struct hwi_async_pdu_context *pasync_ctx;
1614 struct async_pdu_handle *pasync_handle = NULL;
1615 unsigned int cq_index = -1;
1616
1617 phwi_ctrlr = phba->phwi_ctrlr;
1618 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1619
1620 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1621 pdpdu_cqe, &cq_index);
1622 BUG_ON(pasync_handle->is_header != 0);
1623 if (pasync_handle->consumed == 0)
1624 hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
1625 cq_index);
1626
1627 hwi_free_async_msg(phba, pasync_handle->cri);
1628 hwi_post_async_buffers(phba, pasync_handle->is_header);
1629}
1630
1631static unsigned int
1632hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1633 struct beiscsi_hba *phba,
1634 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1635{
1636 struct list_head *plist;
1637 struct async_pdu_handle *pasync_handle;
1638 void *phdr = NULL;
1639 unsigned int hdr_len = 0, buf_len = 0;
1640 unsigned int status, index = 0, offset = 0;
1641 void *pfirst_buffer = NULL;
1642 unsigned int num_buf = 0;
1643
1644 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1645
1646 list_for_each_entry(pasync_handle, plist, link) {
1647 if (index == 0) {
1648 phdr = pasync_handle->pbuffer;
1649 hdr_len = pasync_handle->buffer_len;
1650 } else {
1651 buf_len = pasync_handle->buffer_len;
1652 if (!num_buf) {
1653 pfirst_buffer = pasync_handle->pbuffer;
1654 num_buf++;
1655 }
1656 memcpy(pfirst_buffer + offset,
1657 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001658 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301659 }
1660 index++;
1661 }
1662
1663 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301664 (beiscsi_conn->beiscsi_conn_cid -
1665 phba->fw_config.iscsi_cid_start),
1666 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001667 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301668
1669 if (status == 0)
1670 hwi_free_async_msg(phba, cri);
1671 return 0;
1672}
1673
1674static unsigned int
1675hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1676 struct beiscsi_hba *phba,
1677 struct async_pdu_handle *pasync_handle)
1678{
1679 struct hwi_async_pdu_context *pasync_ctx;
1680 struct hwi_controller *phwi_ctrlr;
1681 unsigned int bytes_needed = 0, status = 0;
1682 unsigned short cri = pasync_handle->cri;
1683 struct pdu_base *ppdu;
1684
1685 phwi_ctrlr = phba->phwi_ctrlr;
1686 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1687
1688 list_del(&pasync_handle->link);
1689 if (pasync_handle->is_header) {
1690 pasync_ctx->async_header.busy_entries--;
1691 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1692 hwi_free_async_msg(phba, cri);
1693 BUG();
1694 }
1695
1696 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1697 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1698 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1699 (unsigned short)pasync_handle->buffer_len;
1700 list_add_tail(&pasync_handle->link,
1701 &pasync_ctx->async_entry[cri].wait_queue.list);
1702
1703 ppdu = pasync_handle->pbuffer;
1704 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1705 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1706 0xFFFF0000) | ((be16_to_cpu((ppdu->
1707 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1708 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1709
1710 if (status == 0) {
1711 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1712 bytes_needed;
1713
1714 if (bytes_needed == 0)
1715 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1716 pasync_ctx, cri);
1717 }
1718 } else {
1719 pasync_ctx->async_data.busy_entries--;
1720 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1721 list_add_tail(&pasync_handle->link,
1722 &pasync_ctx->async_entry[cri].wait_queue.
1723 list);
1724 pasync_ctx->async_entry[cri].wait_queue.
1725 bytes_received +=
1726 (unsigned short)pasync_handle->buffer_len;
1727
1728 if (pasync_ctx->async_entry[cri].wait_queue.
1729 bytes_received >=
1730 pasync_ctx->async_entry[cri].wait_queue.
1731 bytes_needed)
1732 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1733 pasync_ctx, cri);
1734 }
1735 }
1736 return status;
1737}
1738
1739static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1740 struct beiscsi_hba *phba,
1741 struct i_t_dpdu_cqe *pdpdu_cqe)
1742{
1743 struct hwi_controller *phwi_ctrlr;
1744 struct hwi_async_pdu_context *pasync_ctx;
1745 struct async_pdu_handle *pasync_handle = NULL;
1746 unsigned int cq_index = -1;
1747
1748 phwi_ctrlr = phba->phwi_ctrlr;
1749 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1750 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1751 pdpdu_cqe, &cq_index);
1752
1753 if (pasync_handle->consumed == 0)
1754 hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
1755 cq_index);
1756 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1757 hwi_post_async_buffers(phba, pasync_handle->is_header);
1758}
1759
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301760static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1761{
1762 struct be_queue_info *mcc_cq;
1763 struct be_mcc_compl *mcc_compl;
1764 unsigned int num_processed = 0;
1765
1766 mcc_cq = &phba->ctrl.mcc_obj.cq;
1767 mcc_compl = queue_tail_node(mcc_cq);
1768 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1769 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1770
1771 if (num_processed >= 32) {
1772 hwi_ring_cq_db(phba, mcc_cq->id,
1773 num_processed, 0, 0);
1774 num_processed = 0;
1775 }
1776 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1777 /* Interpret flags as an async trailer */
1778 if (is_link_state_evt(mcc_compl->flags))
1779 /* Interpret compl as a async link evt */
1780 beiscsi_async_link_state_process(phba,
1781 (struct be_async_event_link_state *) mcc_compl);
1782 else
1783 SE_DEBUG(DBG_LVL_1,
1784 " Unsupported Async Event, flags"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301785 " = 0x%08x\n", mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301786 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1787 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1788 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1789 }
1790
1791 mcc_compl->flags = 0;
1792 queue_tail_inc(mcc_cq);
1793 mcc_compl = queue_tail_node(mcc_cq);
1794 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1795 num_processed++;
1796 }
1797
1798 if (num_processed > 0)
1799 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1800
1801}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301802
1803static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301804{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301805 struct be_queue_info *cq;
1806 struct sol_cqe *sol;
1807 struct dmsg_cqe *dmsg;
1808 unsigned int num_processed = 0;
1809 unsigned int tot_nump = 0;
1810 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301811 struct beiscsi_endpoint *beiscsi_ep;
1812 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301813 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301814
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301815 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301816 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301817 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301818
1819 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1820 CQE_VALID_MASK) {
1821 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1822
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301823 ep = phba->ep_array[(u32) ((sol->
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301824 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
1825 SOL_CID_MASK) >> 6) -
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301826 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301827
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301828 beiscsi_ep = ep->dd_data;
1829 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301830
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301831 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301832 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301833 num_processed, 0, 0);
1834 tot_nump += num_processed;
1835 num_processed = 0;
1836 }
1837
1838 switch ((u32) sol->dw[offsetof(struct amap_sol_cqe, code) /
1839 32] & CQE_CODE_MASK) {
1840 case SOL_CMD_COMPLETE:
1841 hwi_complete_cmd(beiscsi_conn, phba, sol);
1842 break;
1843 case DRIVERMSG_NOTIFY:
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301844 SE_DEBUG(DBG_LVL_8, "Received DRIVERMSG_NOTIFY\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301845 dmsg = (struct dmsg_cqe *)sol;
1846 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1847 break;
1848 case UNSOL_HDR_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301849 SE_DEBUG(DBG_LVL_8, "Received UNSOL_HDR_ NOTIFY\n");
1850 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1851 (struct i_t_dpdu_cqe *)sol);
1852 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301853 case UNSOL_DATA_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301854 SE_DEBUG(DBG_LVL_8, "Received UNSOL_DATA_NOTIFY\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301855 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1856 (struct i_t_dpdu_cqe *)sol);
1857 break;
1858 case CXN_INVALIDATE_INDEX_NOTIFY:
1859 case CMD_INVALIDATED_NOTIFY:
1860 case CXN_INVALIDATE_NOTIFY:
1861 SE_DEBUG(DBG_LVL_1,
1862 "Ignoring CQ Error notification for cmd/cxn"
1863 "invalidate\n");
1864 break;
1865 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
1866 case CMD_KILLED_INVALID_STATSN_RCVD:
1867 case CMD_KILLED_INVALID_R2T_RCVD:
1868 case CMD_CXN_KILLED_LUN_INVALID:
1869 case CMD_CXN_KILLED_ICD_INVALID:
1870 case CMD_CXN_KILLED_ITT_INVALID:
1871 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1872 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301873 SE_DEBUG(DBG_LVL_1,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301874 "CQ Error notification for cmd.. "
1875 "code %d cid 0x%x\n",
1876 sol->dw[offsetof(struct amap_sol_cqe, code) /
1877 32] & CQE_CODE_MASK,
1878 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1879 32] & SOL_CID_MASK));
1880 break;
1881 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1882 SE_DEBUG(DBG_LVL_1,
1883 "Digest error on def pdu ring, dropping..\n");
1884 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
1885 (struct i_t_dpdu_cqe *) sol);
1886 break;
1887 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
1888 case CXN_KILLED_BURST_LEN_MISMATCH:
1889 case CXN_KILLED_AHS_RCVD:
1890 case CXN_KILLED_HDR_DIGEST_ERR:
1891 case CXN_KILLED_UNKNOWN_HDR:
1892 case CXN_KILLED_STALE_ITT_TTT_RCVD:
1893 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
1894 case CXN_KILLED_TIMED_OUT:
1895 case CXN_KILLED_FIN_RCVD:
1896 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
1897 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
1898 case CXN_KILLED_OVER_RUN_RESIDUAL:
1899 case CXN_KILLED_UNDER_RUN_RESIDUAL:
1900 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301901 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset CID "
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301902 "0x%x...\n",
1903 sol->dw[offsetof(struct amap_sol_cqe, code) /
1904 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301905 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1906 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301907 iscsi_conn_failure(beiscsi_conn->conn,
1908 ISCSI_ERR_CONN_FAILED);
1909 break;
1910 case CXN_KILLED_RST_SENT:
1911 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301912 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset"
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301913 "received/sent on CID 0x%x...\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301914 sol->dw[offsetof(struct amap_sol_cqe, code) /
1915 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301916 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1917 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301918 iscsi_conn_failure(beiscsi_conn->conn,
1919 ISCSI_ERR_CONN_FAILED);
1920 break;
1921 default:
1922 SE_DEBUG(DBG_LVL_1, "CQ Error Invalid code= %d "
1923 "received on CID 0x%x...\n",
1924 sol->dw[offsetof(struct amap_sol_cqe, code) /
1925 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301926 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1927 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301928 break;
1929 }
1930
1931 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
1932 queue_tail_inc(cq);
1933 sol = queue_tail_node(cq);
1934 num_processed++;
1935 }
1936
1937 if (num_processed > 0) {
1938 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301939 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301940 }
1941 return tot_nump;
1942}
1943
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301944void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301945{
1946 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301947 struct hwi_controller *phwi_ctrlr;
1948 struct hwi_context_memory *phwi_context;
1949 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301950 struct beiscsi_hba *phba =
1951 container_of(work, struct beiscsi_hba, work_cqs);
1952
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301953 phwi_ctrlr = phba->phwi_ctrlr;
1954 phwi_context = phwi_ctrlr->phwi_ctxt;
1955 if (phba->msix_enabled)
1956 pbe_eq = &phwi_context->be_eq[phba->num_cpus];
1957 else
1958 pbe_eq = &phwi_context->be_eq[0];
1959
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301960 if (phba->todo_mcc_cq) {
1961 spin_lock_irqsave(&phba->isr_lock, flags);
1962 phba->todo_mcc_cq = 0;
1963 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301964 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301965 }
1966
1967 if (phba->todo_cq) {
1968 spin_lock_irqsave(&phba->isr_lock, flags);
1969 phba->todo_cq = 0;
1970 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301971 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301972 }
1973}
1974
1975static int be_iopoll(struct blk_iopoll *iop, int budget)
1976{
1977 static unsigned int ret;
1978 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301979 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301980
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301981 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
1982 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301983 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301984 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301985 blk_iopoll_complete(iop);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301986 SE_DEBUG(DBG_LVL_8, "rearm pbe_eq->q.id =%d\n", pbe_eq->q.id);
1987 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301988 }
1989 return ret;
1990}
1991
1992static void
1993hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
1994 unsigned int num_sg, struct beiscsi_io_task *io_task)
1995{
1996 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05301997 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301998 unsigned int sge_len = 0;
1999 unsigned long long addr;
2000 struct scatterlist *l_sg;
2001 unsigned int offset;
2002
2003 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2004 io_task->bhs_pa.u.a32.address_lo);
2005 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2006 io_task->bhs_pa.u.a32.address_hi);
2007
2008 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302009 for (index = 0; (index < num_sg) && (index < 2); index++,
2010 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302011 if (index == 0) {
2012 sg_len = sg_dma_len(sg);
2013 addr = (u64) sg_dma_address(sg);
2014 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302015 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302016 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302017 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302018 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2019 sg_len);
2020 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302021 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302022 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2023 pwrb, sge_len);
2024 sg_len = sg_dma_len(sg);
2025 addr = (u64) sg_dma_address(sg);
2026 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302027 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302028 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302029 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302030 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2031 sg_len);
2032 }
2033 }
2034 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2035 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2036
2037 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2038
2039 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2040 io_task->bhs_pa.u.a32.address_hi);
2041 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2042 io_task->bhs_pa.u.a32.address_lo);
2043
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302044 if (num_sg == 1) {
2045 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2046 1);
2047 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2048 0);
2049 } else if (num_sg == 2) {
2050 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2051 0);
2052 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2053 1);
2054 } else {
2055 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2056 0);
2057 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2058 0);
2059 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302060 sg = l_sg;
2061 psgl++;
2062 psgl++;
2063 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302064 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302065 sg_len = sg_dma_len(sg);
2066 addr = (u64) sg_dma_address(sg);
2067 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2068 (addr & 0xFFFFFFFF));
2069 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2070 (addr >> 32));
2071 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2072 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2073 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2074 offset += sg_len;
2075 }
2076 psgl--;
2077 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2078}
2079
2080static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2081{
2082 struct iscsi_sge *psgl;
2083 unsigned long long addr;
2084 struct beiscsi_io_task *io_task = task->dd_data;
2085 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2086 struct beiscsi_hba *phba = beiscsi_conn->phba;
2087
2088 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2089 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2090 io_task->bhs_pa.u.a32.address_lo);
2091 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2092 io_task->bhs_pa.u.a32.address_hi);
2093
2094 if (task->data) {
2095 if (task->data_count) {
2096 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
2097 addr = (u64) pci_map_single(phba->pcidev,
2098 task->data,
2099 task->data_count, 1);
2100 } else {
2101 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2102 addr = 0;
2103 }
2104 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302105 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302106 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302107 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302108 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2109 task->data_count);
2110
2111 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2112 } else {
2113 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2114 addr = 0;
2115 }
2116
2117 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2118
2119 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2120
2121 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2122 io_task->bhs_pa.u.a32.address_hi);
2123 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2124 io_task->bhs_pa.u.a32.address_lo);
2125 if (task->data) {
2126 psgl++;
2127 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2128 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2129 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2130 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2131 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2132 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2133
2134 psgl++;
2135 if (task->data) {
2136 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302137 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302138 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302139 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302140 }
2141 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2142 }
2143 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2144}
2145
2146static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2147{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302148 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302149 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2150 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2151
2152 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2153 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302154 num_async_pdu_buf_pages =
2155 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2156 phba->params.defpdu_hdr_sz);
2157 num_async_pdu_buf_sgl_pages =
2158 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2159 sizeof(struct phys_addr));
2160 num_async_pdu_data_pages =
2161 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2162 phba->params.defpdu_data_sz);
2163 num_async_pdu_data_sgl_pages =
2164 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2165 sizeof(struct phys_addr));
2166
2167 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2168
2169 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2170 BE_ISCSI_PDU_HEADER_SIZE;
2171 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2172 sizeof(struct hwi_context_memory);
2173
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302174
2175 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2176 * (phba->params.wrbs_per_cxn)
2177 * phba->params.cxns_per_ctrl;
2178 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2179 (phba->params.wrbs_per_cxn);
2180 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2181 phba->params.cxns_per_ctrl);
2182
2183 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2184 phba->params.icds_per_ctrl;
2185 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2186 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2187
2188 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2189 num_async_pdu_buf_pages * PAGE_SIZE;
2190 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2191 num_async_pdu_data_pages * PAGE_SIZE;
2192 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2193 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2194 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2195 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2196 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2197 phba->params.asyncpdus_per_ctrl *
2198 sizeof(struct async_pdu_handle);
2199 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2200 phba->params.asyncpdus_per_ctrl *
2201 sizeof(struct async_pdu_handle);
2202 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2203 sizeof(struct hwi_async_pdu_context) +
2204 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2205}
2206
2207static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2208{
2209 struct be_mem_descriptor *mem_descr;
2210 dma_addr_t bus_add;
2211 struct mem_array *mem_arr, *mem_arr_orig;
2212 unsigned int i, j, alloc_size, curr_alloc_size;
2213
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002214 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302215 if (!phba->phwi_ctrlr)
2216 return -ENOMEM;
2217
2218 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2219 GFP_KERNEL);
2220 if (!phba->init_mem) {
2221 kfree(phba->phwi_ctrlr);
2222 return -ENOMEM;
2223 }
2224
2225 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2226 GFP_KERNEL);
2227 if (!mem_arr_orig) {
2228 kfree(phba->init_mem);
2229 kfree(phba->phwi_ctrlr);
2230 return -ENOMEM;
2231 }
2232
2233 mem_descr = phba->init_mem;
2234 for (i = 0; i < SE_MEM_MAX; i++) {
2235 j = 0;
2236 mem_arr = mem_arr_orig;
2237 alloc_size = phba->mem_req[i];
2238 memset(mem_arr, 0, sizeof(struct mem_array) *
2239 BEISCSI_MAX_FRAGS_INIT);
2240 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2241 do {
2242 mem_arr->virtual_address = pci_alloc_consistent(
2243 phba->pcidev,
2244 curr_alloc_size,
2245 &bus_add);
2246 if (!mem_arr->virtual_address) {
2247 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2248 goto free_mem;
2249 if (curr_alloc_size -
2250 rounddown_pow_of_two(curr_alloc_size))
2251 curr_alloc_size = rounddown_pow_of_two
2252 (curr_alloc_size);
2253 else
2254 curr_alloc_size = curr_alloc_size / 2;
2255 } else {
2256 mem_arr->bus_address.u.
2257 a64.address = (__u64) bus_add;
2258 mem_arr->size = curr_alloc_size;
2259 alloc_size -= curr_alloc_size;
2260 curr_alloc_size = min(be_max_phys_size *
2261 1024, alloc_size);
2262 j++;
2263 mem_arr++;
2264 }
2265 } while (alloc_size);
2266 mem_descr->num_elements = j;
2267 mem_descr->size_in_bytes = phba->mem_req[i];
2268 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2269 GFP_KERNEL);
2270 if (!mem_descr->mem_array)
2271 goto free_mem;
2272
2273 memcpy(mem_descr->mem_array, mem_arr_orig,
2274 sizeof(struct mem_array) * j);
2275 mem_descr++;
2276 }
2277 kfree(mem_arr_orig);
2278 return 0;
2279free_mem:
2280 mem_descr->num_elements = j;
2281 while ((i) || (j)) {
2282 for (j = mem_descr->num_elements; j > 0; j--) {
2283 pci_free_consistent(phba->pcidev,
2284 mem_descr->mem_array[j - 1].size,
2285 mem_descr->mem_array[j - 1].
2286 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302287 (unsigned long)mem_descr->
2288 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302289 bus_address.u.a64.address);
2290 }
2291 if (i) {
2292 i--;
2293 kfree(mem_descr->mem_array);
2294 mem_descr--;
2295 }
2296 }
2297 kfree(mem_arr_orig);
2298 kfree(phba->init_mem);
2299 kfree(phba->phwi_ctrlr);
2300 return -ENOMEM;
2301}
2302
2303static int beiscsi_get_memory(struct beiscsi_hba *phba)
2304{
2305 beiscsi_find_mem_req(phba);
2306 return beiscsi_alloc_mem(phba);
2307}
2308
2309static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2310{
2311 struct pdu_data_out *pdata_out;
2312 struct pdu_nop_out *pnop_out;
2313 struct be_mem_descriptor *mem_descr;
2314
2315 mem_descr = phba->init_mem;
2316 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2317 pdata_out =
2318 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2319 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2320
2321 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2322 IIOC_SCSI_DATA);
2323
2324 pnop_out =
2325 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2326 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2327
2328 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2329 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2330 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2331 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2332}
2333
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002334static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302335{
2336 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002337 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302338 struct hwi_controller *phwi_ctrlr;
2339 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002340 struct iscsi_wrb *pwrb = NULL;
2341 unsigned int num_cxn_wrbh = 0;
2342 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302343
2344 mem_descr_wrbh = phba->init_mem;
2345 mem_descr_wrbh += HWI_MEM_WRBH;
2346
2347 mem_descr_wrb = phba->init_mem;
2348 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302349 phwi_ctrlr = phba->phwi_ctrlr;
2350
2351 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2352 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302353 pwrb_context->pwrb_handle_base =
2354 kzalloc(sizeof(struct wrb_handle *) *
2355 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002356 if (!pwrb_context->pwrb_handle_base) {
2357 shost_printk(KERN_ERR, phba->shost,
2358 "Mem Alloc Failed. Failing to load\n");
2359 goto init_wrb_hndl_failed;
2360 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302361 pwrb_context->pwrb_handle_basestd =
2362 kzalloc(sizeof(struct wrb_handle *) *
2363 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002364 if (!pwrb_context->pwrb_handle_basestd) {
2365 shost_printk(KERN_ERR, phba->shost,
2366 "Mem Alloc Failed. Failing to load\n");
2367 goto init_wrb_hndl_failed;
2368 }
2369 if (!num_cxn_wrbh) {
2370 pwrb_handle =
2371 mem_descr_wrbh->mem_array[idx].virtual_address;
2372 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2373 ((sizeof(struct wrb_handle)) *
2374 phba->params.wrbs_per_cxn));
2375 idx++;
2376 }
2377 pwrb_context->alloc_index = 0;
2378 pwrb_context->wrb_handles_available = 0;
2379 pwrb_context->free_index = 0;
2380
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302381 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302382 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2383 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2384 pwrb_context->pwrb_handle_basestd[j] =
2385 pwrb_handle;
2386 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302387 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302388 pwrb_handle++;
2389 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302390 num_cxn_wrbh--;
2391 }
2392 }
2393 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302394 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302395 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002396 if (!num_cxn_wrb) {
2397 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2398 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2399 ((sizeof(struct iscsi_wrb) *
2400 phba->params.wrbs_per_cxn));
2401 idx++;
2402 }
2403
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302404 if (num_cxn_wrb) {
2405 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2406 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2407 pwrb_handle->pwrb = pwrb;
2408 pwrb++;
2409 }
2410 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302411 }
2412 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002413 return 0;
2414init_wrb_hndl_failed:
2415 for (j = index; j > 0; j--) {
2416 pwrb_context = &phwi_ctrlr->wrb_context[j];
2417 kfree(pwrb_context->pwrb_handle_base);
2418 kfree(pwrb_context->pwrb_handle_basestd);
2419 }
2420 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302421}
2422
2423static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2424{
2425 struct hwi_controller *phwi_ctrlr;
2426 struct hba_parameters *p = &phba->params;
2427 struct hwi_async_pdu_context *pasync_ctx;
2428 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002429 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302430 struct be_mem_descriptor *mem_descr;
2431
2432 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2433 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2434
2435 phwi_ctrlr = phba->phwi_ctrlr;
2436 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2437 mem_descr->mem_array[0].virtual_address;
2438 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2439 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2440
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002441 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2442 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302443
2444 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2445 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2446 if (mem_descr->mem_array[0].virtual_address) {
2447 SE_DEBUG(DBG_LVL_8,
2448 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_BUF"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302449 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302450 } else
2451 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302452 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302453
2454 pasync_ctx->async_header.va_base =
2455 mem_descr->mem_array[0].virtual_address;
2456
2457 pasync_ctx->async_header.pa_base.u.a64.address =
2458 mem_descr->mem_array[0].bus_address.u.a64.address;
2459
2460 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2461 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2462 if (mem_descr->mem_array[0].virtual_address) {
2463 SE_DEBUG(DBG_LVL_8,
2464 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_RING"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302465 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302466 } else
2467 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302468 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302469 pasync_ctx->async_header.ring_base =
2470 mem_descr->mem_array[0].virtual_address;
2471
2472 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2473 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2474 if (mem_descr->mem_array[0].virtual_address) {
2475 SE_DEBUG(DBG_LVL_8,
2476 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_HANDLE"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302477 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302478 } else
2479 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302480 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302481
2482 pasync_ctx->async_header.handle_base =
2483 mem_descr->mem_array[0].virtual_address;
2484 pasync_ctx->async_header.writables = 0;
2485 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2486
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302487
2488 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2489 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2490 if (mem_descr->mem_array[0].virtual_address) {
2491 SE_DEBUG(DBG_LVL_8,
2492 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_RING"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302493 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302494 } else
2495 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302496 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302497
2498 pasync_ctx->async_data.ring_base =
2499 mem_descr->mem_array[0].virtual_address;
2500
2501 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2502 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2503 if (!mem_descr->mem_array[0].virtual_address)
2504 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302505 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302506
2507 pasync_ctx->async_data.handle_base =
2508 mem_descr->mem_array[0].virtual_address;
2509 pasync_ctx->async_data.writables = 0;
2510 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2511
2512 pasync_header_h =
2513 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2514 pasync_data_h =
2515 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2516
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002517 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2518 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2519 if (mem_descr->mem_array[0].virtual_address) {
2520 SE_DEBUG(DBG_LVL_8,
2521 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_BUF"
2522 "va=%p\n", mem_descr->mem_array[0].virtual_address);
2523 } else
2524 shost_printk(KERN_WARNING, phba->shost,
2525 "No Virtual address\n");
2526 idx = 0;
2527 pasync_ctx->async_data.va_base =
2528 mem_descr->mem_array[idx].virtual_address;
2529 pasync_ctx->async_data.pa_base.u.a64.address =
2530 mem_descr->mem_array[idx].bus_address.u.a64.address;
2531
2532 num_async_data = ((mem_descr->mem_array[idx].size) /
2533 phba->params.defpdu_data_sz);
2534 num_per_mem = 0;
2535
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302536 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2537 pasync_header_h->cri = -1;
2538 pasync_header_h->index = (char)index;
2539 INIT_LIST_HEAD(&pasync_header_h->link);
2540 pasync_header_h->pbuffer =
2541 (void *)((unsigned long)
2542 (pasync_ctx->async_header.va_base) +
2543 (p->defpdu_hdr_sz * index));
2544
2545 pasync_header_h->pa.u.a64.address =
2546 pasync_ctx->async_header.pa_base.u.a64.address +
2547 (p->defpdu_hdr_sz * index);
2548
2549 list_add_tail(&pasync_header_h->link,
2550 &pasync_ctx->async_header.free_list);
2551 pasync_header_h++;
2552 pasync_ctx->async_header.free_entries++;
2553 pasync_ctx->async_header.writables++;
2554
2555 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2556 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2557 header_busy_list);
2558 pasync_data_h->cri = -1;
2559 pasync_data_h->index = (char)index;
2560 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002561
2562 if (!num_async_data) {
2563 num_per_mem = 0;
2564 idx++;
2565 pasync_ctx->async_data.va_base =
2566 mem_descr->mem_array[idx].virtual_address;
2567 pasync_ctx->async_data.pa_base.u.a64.address =
2568 mem_descr->mem_array[idx].
2569 bus_address.u.a64.address;
2570
2571 num_async_data = ((mem_descr->mem_array[idx].size) /
2572 phba->params.defpdu_data_sz);
2573 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302574 pasync_data_h->pbuffer =
2575 (void *)((unsigned long)
2576 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002577 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302578
2579 pasync_data_h->pa.u.a64.address =
2580 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002581 (p->defpdu_data_sz * num_per_mem);
2582 num_per_mem++;
2583 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302584
2585 list_add_tail(&pasync_data_h->link,
2586 &pasync_ctx->async_data.free_list);
2587 pasync_data_h++;
2588 pasync_ctx->async_data.free_entries++;
2589 pasync_ctx->async_data.writables++;
2590
2591 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2592 }
2593
2594 pasync_ctx->async_header.host_write_ptr = 0;
2595 pasync_ctx->async_header.ep_read_ptr = -1;
2596 pasync_ctx->async_data.host_write_ptr = 0;
2597 pasync_ctx->async_data.ep_read_ptr = -1;
2598}
2599
2600static int
2601be_sgl_create_contiguous(void *virtual_address,
2602 u64 physical_address, u32 length,
2603 struct be_dma_mem *sgl)
2604{
2605 WARN_ON(!virtual_address);
2606 WARN_ON(!physical_address);
2607 WARN_ON(!length > 0);
2608 WARN_ON(!sgl);
2609
2610 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302611 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302612 sgl->size = length;
2613
2614 return 0;
2615}
2616
2617static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2618{
2619 memset(sgl, 0, sizeof(*sgl));
2620}
2621
2622static void
2623hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2624 struct mem_array *pmem, struct be_dma_mem *sgl)
2625{
2626 if (sgl->va)
2627 be_sgl_destroy_contiguous(sgl);
2628
2629 be_sgl_create_contiguous(pmem->virtual_address,
2630 pmem->bus_address.u.a64.address,
2631 pmem->size, sgl);
2632}
2633
2634static void
2635hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2636 struct mem_array *pmem, struct be_dma_mem *sgl)
2637{
2638 if (sgl->va)
2639 be_sgl_destroy_contiguous(sgl);
2640
2641 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2642 pmem->bus_address.u.a64.address,
2643 pmem->size, sgl);
2644}
2645
2646static int be_fill_queue(struct be_queue_info *q,
2647 u16 len, u16 entry_size, void *vaddress)
2648{
2649 struct be_dma_mem *mem = &q->dma_mem;
2650
2651 memset(q, 0, sizeof(*q));
2652 q->len = len;
2653 q->entry_size = entry_size;
2654 mem->size = len * entry_size;
2655 mem->va = vaddress;
2656 if (!mem->va)
2657 return -ENOMEM;
2658 memset(mem->va, 0, mem->size);
2659 return 0;
2660}
2661
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302662static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302663 struct hwi_context_memory *phwi_context)
2664{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302665 unsigned int i, num_eq_pages;
2666 int ret, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302667 struct be_queue_info *eq;
2668 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302669 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302670 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302671
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302672 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
2673 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302674
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302675 if (phba->msix_enabled)
2676 eq_for_mcc = 1;
2677 else
2678 eq_for_mcc = 0;
2679 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
2680 eq = &phwi_context->be_eq[i].q;
2681 mem = &eq->dma_mem;
2682 phwi_context->be_eq[i].phba = phba;
2683 eq_vaddress = pci_alloc_consistent(phba->pcidev,
2684 num_eq_pages * PAGE_SIZE,
2685 &paddr);
2686 if (!eq_vaddress)
2687 goto create_eq_error;
2688
2689 mem->va = eq_vaddress;
2690 ret = be_fill_queue(eq, phba->params.num_eq_entries,
2691 sizeof(struct be_eq_entry), eq_vaddress);
2692 if (ret) {
2693 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302694 "be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302695 goto create_eq_error;
2696 }
2697
2698 mem->dma = paddr;
2699 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
2700 phwi_context->cur_eqd);
2701 if (ret) {
2702 shost_printk(KERN_ERR, phba->shost,
2703 "beiscsi_cmd_eq_create"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302704 "Failedfor EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302705 goto create_eq_error;
2706 }
2707 SE_DEBUG(DBG_LVL_8, "eqid = %d\n", phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302708 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302709 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302710create_eq_error:
2711 for (i = 0; i < (phba->num_cpus + 1); i++) {
2712 eq = &phwi_context->be_eq[i].q;
2713 mem = &eq->dma_mem;
2714 if (mem->va)
2715 pci_free_consistent(phba->pcidev, num_eq_pages
2716 * PAGE_SIZE,
2717 mem->va, mem->dma);
2718 }
2719 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302720}
2721
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302722static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302723 struct hwi_context_memory *phwi_context)
2724{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302725 unsigned int i, num_cq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302726 int ret;
2727 struct be_queue_info *cq, *eq;
2728 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302729 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302730 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302731 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302732
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302733 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2734 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302735
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302736 for (i = 0; i < phba->num_cpus; i++) {
2737 cq = &phwi_context->be_cq[i];
2738 eq = &phwi_context->be_eq[i].q;
2739 pbe_eq = &phwi_context->be_eq[i];
2740 pbe_eq->cq = cq;
2741 pbe_eq->phba = phba;
2742 mem = &cq->dma_mem;
2743 cq_vaddress = pci_alloc_consistent(phba->pcidev,
2744 num_cq_pages * PAGE_SIZE,
2745 &paddr);
2746 if (!cq_vaddress)
2747 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302748 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302749 sizeof(struct sol_cqe), cq_vaddress);
2750 if (ret) {
2751 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302752 "be_fill_queue Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302753 goto create_cq_error;
2754 }
2755
2756 mem->dma = paddr;
2757 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
2758 false, 0);
2759 if (ret) {
2760 shost_printk(KERN_ERR, phba->shost,
2761 "beiscsi_cmd_eq_create"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302762 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302763 goto create_cq_error;
2764 }
2765 SE_DEBUG(DBG_LVL_8, "iscsi cq_id is %d for eq_id %d\n",
2766 cq->id, eq->id);
2767 SE_DEBUG(DBG_LVL_8, "ISCSI CQ CREATED\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302768 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302769 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302770
2771create_cq_error:
2772 for (i = 0; i < phba->num_cpus; i++) {
2773 cq = &phwi_context->be_cq[i];
2774 mem = &cq->dma_mem;
2775 if (mem->va)
2776 pci_free_consistent(phba->pcidev, num_cq_pages
2777 * PAGE_SIZE,
2778 mem->va, mem->dma);
2779 }
2780 return ret;
2781
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302782}
2783
2784static int
2785beiscsi_create_def_hdr(struct beiscsi_hba *phba,
2786 struct hwi_context_memory *phwi_context,
2787 struct hwi_controller *phwi_ctrlr,
2788 unsigned int def_pdu_ring_sz)
2789{
2790 unsigned int idx;
2791 int ret;
2792 struct be_queue_info *dq, *cq;
2793 struct be_dma_mem *mem;
2794 struct be_mem_descriptor *mem_descr;
2795 void *dq_vaddress;
2796
2797 idx = 0;
2798 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302799 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302800 mem = &dq->dma_mem;
2801 mem_descr = phba->init_mem;
2802 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2803 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2804 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
2805 sizeof(struct phys_addr),
2806 sizeof(struct phys_addr), dq_vaddress);
2807 if (ret) {
2808 shost_printk(KERN_ERR, phba->shost,
2809 "be_fill_queue Failed for DEF PDU HDR\n");
2810 return ret;
2811 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302812 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2813 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302814 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
2815 def_pdu_ring_sz,
2816 phba->params.defpdu_hdr_sz);
2817 if (ret) {
2818 shost_printk(KERN_ERR, phba->shost,
2819 "be_cmd_create_default_pdu_queue Failed DEFHDR\n");
2820 return ret;
2821 }
2822 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
2823 SE_DEBUG(DBG_LVL_8, "iscsi def pdu id is %d\n",
2824 phwi_context->be_def_hdrq.id);
2825 hwi_post_async_buffers(phba, 1);
2826 return 0;
2827}
2828
2829static int
2830beiscsi_create_def_data(struct beiscsi_hba *phba,
2831 struct hwi_context_memory *phwi_context,
2832 struct hwi_controller *phwi_ctrlr,
2833 unsigned int def_pdu_ring_sz)
2834{
2835 unsigned int idx;
2836 int ret;
2837 struct be_queue_info *dataq, *cq;
2838 struct be_dma_mem *mem;
2839 struct be_mem_descriptor *mem_descr;
2840 void *dq_vaddress;
2841
2842 idx = 0;
2843 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302844 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302845 mem = &dataq->dma_mem;
2846 mem_descr = phba->init_mem;
2847 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2848 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2849 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
2850 sizeof(struct phys_addr),
2851 sizeof(struct phys_addr), dq_vaddress);
2852 if (ret) {
2853 shost_printk(KERN_ERR, phba->shost,
2854 "be_fill_queue Failed for DEF PDU DATA\n");
2855 return ret;
2856 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302857 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2858 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302859 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
2860 def_pdu_ring_sz,
2861 phba->params.defpdu_data_sz);
2862 if (ret) {
2863 shost_printk(KERN_ERR, phba->shost,
2864 "be_cmd_create_default_pdu_queue Failed"
2865 " for DEF PDU DATA\n");
2866 return ret;
2867 }
2868 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
2869 SE_DEBUG(DBG_LVL_8, "iscsi def data id is %d\n",
2870 phwi_context->be_def_dataq.id);
2871 hwi_post_async_buffers(phba, 0);
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302872 SE_DEBUG(DBG_LVL_8, "DEFAULT PDU DATA RING CREATED\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302873 return 0;
2874}
2875
2876static int
2877beiscsi_post_pages(struct beiscsi_hba *phba)
2878{
2879 struct be_mem_descriptor *mem_descr;
2880 struct mem_array *pm_arr;
2881 unsigned int page_offset, i;
2882 struct be_dma_mem sgl;
2883 int status;
2884
2885 mem_descr = phba->init_mem;
2886 mem_descr += HWI_MEM_SGE;
2887 pm_arr = mem_descr->mem_array;
2888
2889 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
2890 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
2891 for (i = 0; i < mem_descr->num_elements; i++) {
2892 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
2893 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
2894 page_offset,
2895 (pm_arr->size / PAGE_SIZE));
2896 page_offset += pm_arr->size / PAGE_SIZE;
2897 if (status != 0) {
2898 shost_printk(KERN_ERR, phba->shost,
2899 "post sgl failed.\n");
2900 return status;
2901 }
2902 pm_arr++;
2903 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302904 SE_DEBUG(DBG_LVL_8, "POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302905 return 0;
2906}
2907
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302908static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
2909{
2910 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05002911 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302912 pci_free_consistent(phba->pcidev, mem->size,
2913 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05002914 mem->va = NULL;
2915 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302916}
2917
2918static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
2919 u16 len, u16 entry_size)
2920{
2921 struct be_dma_mem *mem = &q->dma_mem;
2922
2923 memset(q, 0, sizeof(*q));
2924 q->len = len;
2925 q->entry_size = entry_size;
2926 mem->size = len * entry_size;
2927 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
2928 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05302929 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302930 memset(mem->va, 0, mem->size);
2931 return 0;
2932}
2933
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302934static int
2935beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
2936 struct hwi_context_memory *phwi_context,
2937 struct hwi_controller *phwi_ctrlr)
2938{
2939 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
2940 u64 pa_addr_lo;
2941 unsigned int idx, num, i;
2942 struct mem_array *pwrb_arr;
2943 void *wrb_vaddr;
2944 struct be_dma_mem sgl;
2945 struct be_mem_descriptor *mem_descr;
2946 int status;
2947
2948 idx = 0;
2949 mem_descr = phba->init_mem;
2950 mem_descr += HWI_MEM_WRB;
2951 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
2952 GFP_KERNEL);
2953 if (!pwrb_arr) {
2954 shost_printk(KERN_ERR, phba->shost,
2955 "Memory alloc failed in create wrb ring.\n");
2956 return -ENOMEM;
2957 }
2958 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2959 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
2960 num_wrb_rings = mem_descr->mem_array[idx].size /
2961 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
2962
2963 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
2964 if (num_wrb_rings) {
2965 pwrb_arr[num].virtual_address = wrb_vaddr;
2966 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
2967 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2968 sizeof(struct iscsi_wrb);
2969 wrb_vaddr += pwrb_arr[num].size;
2970 pa_addr_lo += pwrb_arr[num].size;
2971 num_wrb_rings--;
2972 } else {
2973 idx++;
2974 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2975 pa_addr_lo = mem_descr->mem_array[idx].\
2976 bus_address.u.a64.address;
2977 num_wrb_rings = mem_descr->mem_array[idx].size /
2978 (phba->params.wrbs_per_cxn *
2979 sizeof(struct iscsi_wrb));
2980 pwrb_arr[num].virtual_address = wrb_vaddr;
2981 pwrb_arr[num].bus_address.u.a64.address\
2982 = pa_addr_lo;
2983 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2984 sizeof(struct iscsi_wrb);
2985 wrb_vaddr += pwrb_arr[num].size;
2986 pa_addr_lo += pwrb_arr[num].size;
2987 num_wrb_rings--;
2988 }
2989 }
2990 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
2991 wrb_mem_index = 0;
2992 offset = 0;
2993 size = 0;
2994
2995 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
2996 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
2997 &phwi_context->be_wrbq[i]);
2998 if (status != 0) {
2999 shost_printk(KERN_ERR, phba->shost,
3000 "wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003001 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303002 return status;
3003 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303004 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
3005 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303006 }
3007 kfree(pwrb_arr);
3008 return 0;
3009}
3010
3011static void free_wrb_handles(struct beiscsi_hba *phba)
3012{
3013 unsigned int index;
3014 struct hwi_controller *phwi_ctrlr;
3015 struct hwi_wrb_context *pwrb_context;
3016
3017 phwi_ctrlr = phba->phwi_ctrlr;
3018 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3019 pwrb_context = &phwi_ctrlr->wrb_context[index];
3020 kfree(pwrb_context->pwrb_handle_base);
3021 kfree(pwrb_context->pwrb_handle_basestd);
3022 }
3023}
3024
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303025static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3026{
3027 struct be_queue_info *q;
3028 struct be_ctrl_info *ctrl = &phba->ctrl;
3029
3030 q = &phba->ctrl.mcc_obj.q;
3031 if (q->created)
3032 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3033 be_queue_free(phba, q);
3034
3035 q = &phba->ctrl.mcc_obj.cq;
3036 if (q->created)
3037 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3038 be_queue_free(phba, q);
3039}
3040
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303041static void hwi_cleanup(struct beiscsi_hba *phba)
3042{
3043 struct be_queue_info *q;
3044 struct be_ctrl_info *ctrl = &phba->ctrl;
3045 struct hwi_controller *phwi_ctrlr;
3046 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303047 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303048
3049 phwi_ctrlr = phba->phwi_ctrlr;
3050 phwi_context = phwi_ctrlr->phwi_ctxt;
3051 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3052 q = &phwi_context->be_wrbq[i];
3053 if (q->created)
3054 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3055 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303056 free_wrb_handles(phba);
3057
3058 q = &phwi_context->be_def_hdrq;
3059 if (q->created)
3060 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3061
3062 q = &phwi_context->be_def_dataq;
3063 if (q->created)
3064 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3065
3066 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3067
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303068 for (i = 0; i < (phba->num_cpus); i++) {
3069 q = &phwi_context->be_cq[i];
3070 if (q->created)
3071 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3072 }
3073 if (phba->msix_enabled)
3074 eq_num = 1;
3075 else
3076 eq_num = 0;
3077 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3078 q = &phwi_context->be_eq[i].q;
3079 if (q->created)
3080 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3081 }
3082 be_mcc_queues_destroy(phba);
3083}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303084
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303085static int be_mcc_queues_create(struct beiscsi_hba *phba,
3086 struct hwi_context_memory *phwi_context)
3087{
3088 struct be_queue_info *q, *cq;
3089 struct be_ctrl_info *ctrl = &phba->ctrl;
3090
3091 /* Alloc MCC compl queue */
3092 cq = &phba->ctrl.mcc_obj.cq;
3093 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3094 sizeof(struct be_mcc_compl)))
3095 goto err;
3096 /* Ask BE to create MCC compl queue; */
3097 if (phba->msix_enabled) {
3098 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3099 [phba->num_cpus].q, false, true, 0))
3100 goto mcc_cq_free;
3101 } else {
3102 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3103 false, true, 0))
3104 goto mcc_cq_free;
3105 }
3106
3107 /* Alloc MCC queue */
3108 q = &phba->ctrl.mcc_obj.q;
3109 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3110 goto mcc_cq_destroy;
3111
3112 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303113 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303114 goto mcc_q_free;
3115
3116 return 0;
3117
3118mcc_q_free:
3119 be_queue_free(phba, q);
3120mcc_cq_destroy:
3121 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3122mcc_cq_free:
3123 be_queue_free(phba, cq);
3124err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303125 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303126}
3127
3128static int find_num_cpus(void)
3129{
3130 int num_cpus = 0;
3131
3132 num_cpus = num_online_cpus();
3133 if (num_cpus >= MAX_CPUS)
3134 num_cpus = MAX_CPUS - 1;
3135
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303136 SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303137 return num_cpus;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303138}
3139
3140static int hwi_init_port(struct beiscsi_hba *phba)
3141{
3142 struct hwi_controller *phwi_ctrlr;
3143 struct hwi_context_memory *phwi_context;
3144 unsigned int def_pdu_ring_sz;
3145 struct be_ctrl_info *ctrl = &phba->ctrl;
3146 int status;
3147
3148 def_pdu_ring_sz =
3149 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3150 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303151 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303152 phwi_context->max_eqd = 0;
3153 phwi_context->min_eqd = 0;
3154 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303155 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303156
3157 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303158 if (status != 0) {
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303159 shost_printk(KERN_ERR, phba->shost, "EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303160 goto error;
3161 }
3162
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303163 status = be_mcc_queues_create(phba, phwi_context);
3164 if (status != 0)
3165 goto error;
3166
3167 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303168 if (status != 0) {
3169 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303170 "Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303171 goto error;
3172 }
3173
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303174 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303175 if (status != 0) {
3176 shost_printk(KERN_ERR, phba->shost, "CQ not created\n");
3177 goto error;
3178 }
3179
3180 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3181 def_pdu_ring_sz);
3182 if (status != 0) {
3183 shost_printk(KERN_ERR, phba->shost,
3184 "Default Header not created\n");
3185 goto error;
3186 }
3187
3188 status = beiscsi_create_def_data(phba, phwi_context,
3189 phwi_ctrlr, def_pdu_ring_sz);
3190 if (status != 0) {
3191 shost_printk(KERN_ERR, phba->shost,
3192 "Default Data not created\n");
3193 goto error;
3194 }
3195
3196 status = beiscsi_post_pages(phba);
3197 if (status != 0) {
3198 shost_printk(KERN_ERR, phba->shost, "Post SGL Pages Failed\n");
3199 goto error;
3200 }
3201
3202 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3203 if (status != 0) {
3204 shost_printk(KERN_ERR, phba->shost,
3205 "WRB Rings not created\n");
3206 goto error;
3207 }
3208
3209 SE_DEBUG(DBG_LVL_8, "hwi_init_port success\n");
3210 return 0;
3211
3212error:
3213 shost_printk(KERN_ERR, phba->shost, "hwi_init_port failed");
3214 hwi_cleanup(phba);
3215 return -ENOMEM;
3216}
3217
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303218static int hwi_init_controller(struct beiscsi_hba *phba)
3219{
3220 struct hwi_controller *phwi_ctrlr;
3221
3222 phwi_ctrlr = phba->phwi_ctrlr;
3223 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3224 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3225 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303226 SE_DEBUG(DBG_LVL_8, " phwi_ctrlr->phwi_ctxt=%p\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303227 phwi_ctrlr->phwi_ctxt);
3228 } else {
3229 shost_printk(KERN_ERR, phba->shost,
3230 "HWI_MEM_ADDN_CONTEXT is more than one element."
3231 "Failing to load\n");
3232 return -ENOMEM;
3233 }
3234
3235 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003236 if (beiscsi_init_wrb_handle(phba))
3237 return -ENOMEM;
3238
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303239 hwi_init_async_pdu_ctx(phba);
3240 if (hwi_init_port(phba) != 0) {
3241 shost_printk(KERN_ERR, phba->shost,
3242 "hwi_init_controller failed\n");
3243 return -ENOMEM;
3244 }
3245 return 0;
3246}
3247
3248static void beiscsi_free_mem(struct beiscsi_hba *phba)
3249{
3250 struct be_mem_descriptor *mem_descr;
3251 int i, j;
3252
3253 mem_descr = phba->init_mem;
3254 i = 0;
3255 j = 0;
3256 for (i = 0; i < SE_MEM_MAX; i++) {
3257 for (j = mem_descr->num_elements; j > 0; j--) {
3258 pci_free_consistent(phba->pcidev,
3259 mem_descr->mem_array[j - 1].size,
3260 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303261 (unsigned long)mem_descr->mem_array[j - 1].
3262 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303263 }
3264 kfree(mem_descr->mem_array);
3265 mem_descr++;
3266 }
3267 kfree(phba->init_mem);
3268 kfree(phba->phwi_ctrlr);
3269}
3270
3271static int beiscsi_init_controller(struct beiscsi_hba *phba)
3272{
3273 int ret = -ENOMEM;
3274
3275 ret = beiscsi_get_memory(phba);
3276 if (ret < 0) {
3277 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe -"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303278 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303279 return ret;
3280 }
3281
3282 ret = hwi_init_controller(phba);
3283 if (ret)
3284 goto free_init;
3285 SE_DEBUG(DBG_LVL_8, "Return success from beiscsi_init_controller");
3286 return 0;
3287
3288free_init:
3289 beiscsi_free_mem(phba);
3290 return -ENOMEM;
3291}
3292
3293static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3294{
3295 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3296 struct sgl_handle *psgl_handle;
3297 struct iscsi_sge *pfrag;
3298 unsigned int arr_index, i, idx;
3299
3300 phba->io_sgl_hndl_avbl = 0;
3301 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303302
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303303 mem_descr_sglh = phba->init_mem;
3304 mem_descr_sglh += HWI_MEM_SGLH;
3305 if (1 == mem_descr_sglh->num_elements) {
3306 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3307 phba->params.ios_per_ctrl,
3308 GFP_KERNEL);
3309 if (!phba->io_sgl_hndl_base) {
3310 shost_printk(KERN_ERR, phba->shost,
3311 "Mem Alloc Failed. Failing to load\n");
3312 return -ENOMEM;
3313 }
3314 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3315 (phba->params.icds_per_ctrl -
3316 phba->params.ios_per_ctrl),
3317 GFP_KERNEL);
3318 if (!phba->eh_sgl_hndl_base) {
3319 kfree(phba->io_sgl_hndl_base);
3320 shost_printk(KERN_ERR, phba->shost,
3321 "Mem Alloc Failed. Failing to load\n");
3322 return -ENOMEM;
3323 }
3324 } else {
3325 shost_printk(KERN_ERR, phba->shost,
3326 "HWI_MEM_SGLH is more than one element."
3327 "Failing to load\n");
3328 return -ENOMEM;
3329 }
3330
3331 arr_index = 0;
3332 idx = 0;
3333 while (idx < mem_descr_sglh->num_elements) {
3334 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3335
3336 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3337 sizeof(struct sgl_handle)); i++) {
3338 if (arr_index < phba->params.ios_per_ctrl) {
3339 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3340 phba->io_sgl_hndl_avbl++;
3341 arr_index++;
3342 } else {
3343 phba->eh_sgl_hndl_base[arr_index -
3344 phba->params.ios_per_ctrl] =
3345 psgl_handle;
3346 arr_index++;
3347 phba->eh_sgl_hndl_avbl++;
3348 }
3349 psgl_handle++;
3350 }
3351 idx++;
3352 }
3353 SE_DEBUG(DBG_LVL_8,
3354 "phba->io_sgl_hndl_avbl=%d"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303355 "phba->eh_sgl_hndl_avbl=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303356 phba->io_sgl_hndl_avbl,
3357 phba->eh_sgl_hndl_avbl);
3358 mem_descr_sg = phba->init_mem;
3359 mem_descr_sg += HWI_MEM_SGE;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303360 SE_DEBUG(DBG_LVL_8, "\n mem_descr_sg->num_elements=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303361 mem_descr_sg->num_elements);
3362 arr_index = 0;
3363 idx = 0;
3364 while (idx < mem_descr_sg->num_elements) {
3365 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3366
3367 for (i = 0;
3368 i < (mem_descr_sg->mem_array[idx].size) /
3369 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3370 i++) {
3371 if (arr_index < phba->params.ios_per_ctrl)
3372 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3373 else
3374 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3375 phba->params.ios_per_ctrl];
3376 psgl_handle->pfrag = pfrag;
3377 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3378 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3379 pfrag += phba->params.num_sge_per_io;
3380 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303381 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303382 }
3383 idx++;
3384 }
3385 phba->io_sgl_free_index = 0;
3386 phba->io_sgl_alloc_index = 0;
3387 phba->eh_sgl_free_index = 0;
3388 phba->eh_sgl_alloc_index = 0;
3389 return 0;
3390}
3391
3392static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3393{
3394 int i, new_cid;
3395
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303396 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303397 GFP_KERNEL);
3398 if (!phba->cid_array) {
3399 shost_printk(KERN_ERR, phba->shost,
3400 "Failed to allocate memory in "
3401 "hba_setup_cid_tbls\n");
3402 return -ENOMEM;
3403 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303404 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303405 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3406 if (!phba->ep_array) {
3407 shost_printk(KERN_ERR, phba->shost,
3408 "Failed to allocate memory in "
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303409 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303410 kfree(phba->cid_array);
3411 return -ENOMEM;
3412 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303413 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303414 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3415 phba->cid_array[i] = new_cid;
3416 new_cid += 2;
3417 }
3418 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3419 return 0;
3420}
3421
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303422static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303423{
3424 struct be_ctrl_info *ctrl = &phba->ctrl;
3425 struct hwi_controller *phwi_ctrlr;
3426 struct hwi_context_memory *phwi_context;
3427 struct be_queue_info *eq;
3428 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303429 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303430 u32 enabled;
3431
3432 phwi_ctrlr = phba->phwi_ctrlr;
3433 phwi_context = phwi_ctrlr->phwi_ctxt;
3434
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303435 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3436 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3437 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303438
3439 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3440 if (!enabled) {
3441 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303442 SE_DEBUG(DBG_LVL_8, "reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303443 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003444 }
3445
3446 if (!phba->msix_enabled) {
3447 eq = &phwi_context->be_eq[0].q;
3448 SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
3449 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3450 } else {
3451 for (i = 0; i <= phba->num_cpus; i++) {
3452 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303453 SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303454 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3455 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303456 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303457}
3458
3459static void hwi_disable_intr(struct beiscsi_hba *phba)
3460{
3461 struct be_ctrl_info *ctrl = &phba->ctrl;
3462
3463 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3464 u32 reg = ioread32(addr);
3465
3466 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3467 if (enabled) {
3468 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3469 iowrite32(reg, addr);
3470 } else
3471 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303472 "In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303473}
3474
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303475static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3476{
3477 struct be_cmd_resp_get_boot_target *boot_resp;
3478 struct be_cmd_resp_get_session *session_resp;
3479 struct be_mcc_wrb *wrb;
3480 struct be_dma_mem nonemb_cmd;
3481 unsigned int tag, wrb_num;
3482 unsigned short status, extd_status;
3483 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
Mike Christief457a462011-06-24 15:11:53 -05003484 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303485
3486 tag = beiscsi_get_boot_target(phba);
3487 if (!tag) {
3488 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
3489 return -EAGAIN;
3490 } else
3491 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3492 phba->ctrl.mcc_numtag[tag]);
3493
3494 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3495 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3496 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3497 if (status || extd_status) {
3498 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed"
3499 " status = %d extd_status = %d\n",
3500 status, extd_status);
3501 free_mcc_tag(&phba->ctrl, tag);
3502 return -EBUSY;
3503 }
3504 wrb = queue_get_wrb(mccq, wrb_num);
3505 free_mcc_tag(&phba->ctrl, tag);
3506 boot_resp = embedded_payload(wrb);
3507
3508 if (boot_resp->boot_session_handle < 0) {
Mike Christief457a462011-06-24 15:11:53 -05003509 shost_printk(KERN_INFO, phba->shost, "No Boot Session.\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303510 return -ENXIO;
3511 }
3512
3513 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3514 sizeof(*session_resp),
3515 &nonemb_cmd.dma);
3516 if (nonemb_cmd.va == NULL) {
3517 SE_DEBUG(DBG_LVL_1,
3518 "Failed to allocate memory for"
3519 "beiscsi_get_session_info\n");
3520 return -ENOMEM;
3521 }
3522
3523 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
3524 tag = beiscsi_get_session_info(phba,
3525 boot_resp->boot_session_handle, &nonemb_cmd);
3526 if (!tag) {
3527 SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info"
3528 " Failed\n");
3529 goto boot_freemem;
3530 } else
3531 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3532 phba->ctrl.mcc_numtag[tag]);
3533
3534 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3535 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3536 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3537 if (status || extd_status) {
3538 SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info Failed"
3539 " status = %d extd_status = %d\n",
3540 status, extd_status);
3541 free_mcc_tag(&phba->ctrl, tag);
3542 goto boot_freemem;
3543 }
3544 wrb = queue_get_wrb(mccq, wrb_num);
3545 free_mcc_tag(&phba->ctrl, tag);
3546 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003547
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303548 memcpy(&phba->boot_sess, &session_resp->session_info,
3549 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003550 ret = 0;
3551
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303552boot_freemem:
3553 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3554 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003555 return ret;
3556}
3557
3558static void beiscsi_boot_release(void *data)
3559{
3560 struct beiscsi_hba *phba = data;
3561
3562 scsi_host_put(phba->shost);
3563}
3564
3565static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3566{
3567 struct iscsi_boot_kobj *boot_kobj;
3568
3569 /* get boot info using mgmt cmd */
3570 if (beiscsi_get_boot_info(phba))
3571 /* Try to see if we can carry on without this */
3572 return 0;
3573
3574 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3575 if (!phba->boot_kset)
3576 return -ENOMEM;
3577
3578 /* get a ref because the show function will ref the phba */
3579 if (!scsi_host_get(phba->shost))
3580 goto free_kset;
3581 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3582 beiscsi_show_boot_tgt_info,
3583 beiscsi_tgt_get_attr_visibility,
3584 beiscsi_boot_release);
3585 if (!boot_kobj)
3586 goto put_shost;
3587
3588 if (!scsi_host_get(phba->shost))
3589 goto free_kset;
3590 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3591 beiscsi_show_boot_ini_info,
3592 beiscsi_ini_get_attr_visibility,
3593 beiscsi_boot_release);
3594 if (!boot_kobj)
3595 goto put_shost;
3596
3597 if (!scsi_host_get(phba->shost))
3598 goto free_kset;
3599 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3600 beiscsi_show_boot_eth_info,
3601 beiscsi_eth_get_attr_visibility,
3602 beiscsi_boot_release);
3603 if (!boot_kobj)
3604 goto put_shost;
3605 return 0;
3606
3607put_shost:
3608 scsi_host_put(phba->shost);
3609free_kset:
3610 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303611 return -ENOMEM;
3612}
3613
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303614static int beiscsi_init_port(struct beiscsi_hba *phba)
3615{
3616 int ret;
3617
3618 ret = beiscsi_init_controller(phba);
3619 if (ret < 0) {
3620 shost_printk(KERN_ERR, phba->shost,
3621 "beiscsi_dev_probe - Failed in"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303622 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303623 return ret;
3624 }
3625 ret = beiscsi_init_sgl_handle(phba);
3626 if (ret < 0) {
3627 shost_printk(KERN_ERR, phba->shost,
3628 "beiscsi_dev_probe - Failed in"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303629 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303630 goto do_cleanup_ctrlr;
3631 }
3632
3633 if (hba_setup_cid_tbls(phba)) {
3634 shost_printk(KERN_ERR, phba->shost,
3635 "Failed in hba_setup_cid_tbls\n");
3636 kfree(phba->io_sgl_hndl_base);
3637 kfree(phba->eh_sgl_hndl_base);
3638 goto do_cleanup_ctrlr;
3639 }
3640
3641 return ret;
3642
3643do_cleanup_ctrlr:
3644 hwi_cleanup(phba);
3645 return ret;
3646}
3647
3648static void hwi_purge_eq(struct beiscsi_hba *phba)
3649{
3650 struct hwi_controller *phwi_ctrlr;
3651 struct hwi_context_memory *phwi_context;
3652 struct be_queue_info *eq;
3653 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303654 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303655 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303656
3657 phwi_ctrlr = phba->phwi_ctrlr;
3658 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303659 if (phba->msix_enabled)
3660 eq_msix = 1;
3661 else
3662 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303663
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303664 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3665 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303666 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303667 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303668 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3669 & EQE_VALID_MASK) {
3670 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3671 queue_tail_inc(eq);
3672 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303673 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303674 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303675
3676 if (num_processed)
3677 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303678 }
3679}
3680
3681static void beiscsi_clean_port(struct beiscsi_hba *phba)
3682{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05303683 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303684
3685 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
3686 if (mgmt_status)
3687 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303688 "mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303689
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303690 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303691 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303692 kfree(phba->io_sgl_hndl_base);
3693 kfree(phba->eh_sgl_hndl_base);
3694 kfree(phba->cid_array);
3695 kfree(phba->ep_array);
3696}
3697
Mike Christie1282ab72012-04-18 03:06:00 -05003698static void beiscsi_cleanup_task(struct iscsi_task *task)
3699{
3700 struct beiscsi_io_task *io_task = task->dd_data;
3701 struct iscsi_conn *conn = task->conn;
3702 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3703 struct beiscsi_hba *phba = beiscsi_conn->phba;
3704 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3705 struct hwi_wrb_context *pwrb_context;
3706 struct hwi_controller *phwi_ctrlr;
3707
3708 phwi_ctrlr = phba->phwi_ctrlr;
3709 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
3710 - phba->fw_config.iscsi_cid_start];
3711
3712 if (io_task->cmd_bhs) {
3713 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3714 io_task->bhs_pa.u.a64.address);
3715 io_task->cmd_bhs = NULL;
3716 }
3717
3718 if (task->sc) {
3719 if (io_task->pwrb_handle) {
3720 free_wrb_handle(phba, pwrb_context,
3721 io_task->pwrb_handle);
3722 io_task->pwrb_handle = NULL;
3723 }
3724
3725 if (io_task->psgl_handle) {
3726 spin_lock(&phba->io_sgl_lock);
3727 free_io_sgl_handle(phba, io_task->psgl_handle);
3728 spin_unlock(&phba->io_sgl_lock);
3729 io_task->psgl_handle = NULL;
3730 }
3731 } else {
3732 if (!beiscsi_conn->login_in_progress) {
3733 if (io_task->pwrb_handle) {
3734 free_wrb_handle(phba, pwrb_context,
3735 io_task->pwrb_handle);
3736 io_task->pwrb_handle = NULL;
3737 }
3738 if (io_task->psgl_handle) {
3739 spin_lock(&phba->mgmt_sgl_lock);
3740 free_mgmt_sgl_handle(phba,
3741 io_task->psgl_handle);
3742 spin_unlock(&phba->mgmt_sgl_lock);
3743 io_task->psgl_handle = NULL;
3744 }
3745 }
3746 }
3747}
3748
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303749void
3750beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
3751 struct beiscsi_offload_params *params)
3752{
3753 struct wrb_handle *pwrb_handle;
3754 struct iscsi_target_context_update_wrb *pwrb = NULL;
3755 struct be_mem_descriptor *mem_descr;
3756 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05003757 struct iscsi_task *task = beiscsi_conn->task;
3758 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303759 u32 doorbell = 0;
3760
3761 /*
3762 * We can always use 0 here because it is reserved by libiscsi for
3763 * login/startup related tasks.
3764 */
Mike Christie1282ab72012-04-18 03:06:00 -05003765 beiscsi_conn->login_in_progress = 0;
3766 spin_lock_bh(&session->lock);
3767 beiscsi_cleanup_task(task);
3768 spin_unlock_bh(&session->lock);
3769
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303770 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05303771 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303772 pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
3773 memset(pwrb, 0, sizeof(*pwrb));
3774 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3775 max_burst_length, pwrb, params->dw[offsetof
3776 (struct amap_beiscsi_offload_params,
3777 max_burst_length) / 32]);
3778 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3779 max_send_data_segment_length, pwrb,
3780 params->dw[offsetof(struct amap_beiscsi_offload_params,
3781 max_send_data_segment_length) / 32]);
3782 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3783 first_burst_length,
3784 pwrb,
3785 params->dw[offsetof(struct amap_beiscsi_offload_params,
3786 first_burst_length) / 32]);
3787
3788 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
3789 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3790 erl) / 32] & OFFLD_PARAMS_ERL));
3791 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
3792 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3793 dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
3794 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
3795 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3796 hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
3797 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
3798 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3799 ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
3800 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
3801 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3802 imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
3803 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
3804 pwrb,
3805 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3806 exp_statsn) / 32] + 1));
3807 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
3808 0x7);
3809 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
3810 pwrb, pwrb_handle->wrb_index);
3811 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
3812 pwrb, pwrb_handle->nxt_wrb_index);
3813 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3814 session_state, pwrb, 0);
3815 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
3816 pwrb, 1);
3817 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
3818 pwrb, 0);
3819 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
3820 0);
3821
3822 mem_descr = phba->init_mem;
3823 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
3824
3825 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3826 pad_buffer_addr_hi, pwrb,
3827 mem_descr->mem_array[0].bus_address.u.a32.address_hi);
3828 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3829 pad_buffer_addr_lo, pwrb,
3830 mem_descr->mem_array[0].bus_address.u.a32.address_lo);
3831
3832 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
3833
3834 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303835 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303836 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303837 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
3838
3839 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
3840}
3841
3842static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
3843 int *index, int *age)
3844{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303845 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303846 if (age)
3847 *age = conn->session->age;
3848}
3849
3850/**
3851 * beiscsi_alloc_pdu - allocates pdu and related resources
3852 * @task: libiscsi task
3853 * @opcode: opcode of pdu for task
3854 *
3855 * This is called with the session lock held. It will allocate
3856 * the wrb and sgl if needed for the command. And it will prep
3857 * the pdu's itt. beiscsi_parse_pdu will later translate
3858 * the pdu itt to the libiscsi task itt.
3859 */
3860static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
3861{
3862 struct beiscsi_io_task *io_task = task->dd_data;
3863 struct iscsi_conn *conn = task->conn;
3864 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3865 struct beiscsi_hba *phba = beiscsi_conn->phba;
3866 struct hwi_wrb_context *pwrb_context;
3867 struct hwi_controller *phwi_ctrlr;
3868 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303869 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3870 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303871
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303872 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06003873 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303874 if (!io_task->cmd_bhs)
3875 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303876 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303877 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303878 io_task->conn = beiscsi_conn;
3879
3880 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
3881 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303882 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003883 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303884
3885 if (task->sc) {
3886 spin_lock(&phba->io_sgl_lock);
3887 io_task->psgl_handle = alloc_io_sgl_handle(phba);
3888 spin_unlock(&phba->io_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303889 if (!io_task->psgl_handle)
3890 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303891 io_task->pwrb_handle = alloc_wrb_handle(phba,
3892 beiscsi_conn->beiscsi_conn_cid -
3893 phba->fw_config.iscsi_cid_start);
3894 if (!io_task->pwrb_handle)
3895 goto free_io_hndls;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303896 } else {
3897 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05303898 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303899 if (!beiscsi_conn->login_in_progress) {
3900 spin_lock(&phba->mgmt_sgl_lock);
3901 io_task->psgl_handle = (struct sgl_handle *)
3902 alloc_mgmt_sgl_handle(phba);
3903 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303904 if (!io_task->psgl_handle)
3905 goto free_hndls;
3906
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303907 beiscsi_conn->login_in_progress = 1;
3908 beiscsi_conn->plogin_sgl_handle =
3909 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303910 io_task->pwrb_handle =
3911 alloc_wrb_handle(phba,
3912 beiscsi_conn->beiscsi_conn_cid -
3913 phba->fw_config.iscsi_cid_start);
3914 if (!io_task->pwrb_handle)
3915 goto free_io_hndls;
3916 beiscsi_conn->plogin_wrb_handle =
3917 io_task->pwrb_handle;
3918
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303919 } else {
3920 io_task->psgl_handle =
3921 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303922 io_task->pwrb_handle =
3923 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303924 }
Mike Christie1282ab72012-04-18 03:06:00 -05003925 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303926 } else {
3927 spin_lock(&phba->mgmt_sgl_lock);
3928 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
3929 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303930 if (!io_task->psgl_handle)
3931 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303932 io_task->pwrb_handle =
3933 alloc_wrb_handle(phba,
3934 beiscsi_conn->beiscsi_conn_cid -
3935 phba->fw_config.iscsi_cid_start);
3936 if (!io_task->pwrb_handle)
3937 goto free_mgmt_hndls;
3938
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303939 }
3940 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303941 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
3942 wrb_index << 16) | (unsigned int)
3943 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303944 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303945
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303946 io_task->cmd_bhs->iscsi_hdr.itt = itt;
3947 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303948
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303949free_io_hndls:
3950 spin_lock(&phba->io_sgl_lock);
3951 free_io_sgl_handle(phba, io_task->psgl_handle);
3952 spin_unlock(&phba->io_sgl_lock);
3953 goto free_hndls;
3954free_mgmt_hndls:
3955 spin_lock(&phba->mgmt_sgl_lock);
3956 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
3957 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303958free_hndls:
3959 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303960 pwrb_context = &phwi_ctrlr->wrb_context[
3961 beiscsi_conn->beiscsi_conn_cid -
3962 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303963 if (io_task->pwrb_handle)
3964 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303965 io_task->pwrb_handle = NULL;
3966 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3967 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05003968 io_task->cmd_bhs = NULL;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303969 SE_DEBUG(DBG_LVL_1, "Alloc of SGL_ICD Failed\n");
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303970 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303971}
3972
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303973static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
3974 unsigned int num_sg, unsigned int xferlen,
3975 unsigned int writedir)
3976{
3977
3978 struct beiscsi_io_task *io_task = task->dd_data;
3979 struct iscsi_conn *conn = task->conn;
3980 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3981 struct beiscsi_hba *phba = beiscsi_conn->phba;
3982 struct iscsi_wrb *pwrb = NULL;
3983 unsigned int doorbell = 0;
3984
3985 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303986 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
3987 io_task->bhs_len = sizeof(struct be_cmd_bhs);
3988
3989 if (writedir) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303990 memset(&io_task->cmd_bhs->iscsi_data_pdu, 0, 48);
3991 AMAP_SET_BITS(struct amap_pdu_data_out, itt,
3992 &io_task->cmd_bhs->iscsi_data_pdu,
3993 (unsigned int)io_task->cmd_bhs->iscsi_hdr.itt);
3994 AMAP_SET_BITS(struct amap_pdu_data_out, opcode,
3995 &io_task->cmd_bhs->iscsi_data_pdu,
3996 ISCSI_OPCODE_SCSI_DATA_OUT);
3997 AMAP_SET_BITS(struct amap_pdu_data_out, final_bit,
3998 &io_task->cmd_bhs->iscsi_data_pdu, 1);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303999 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4000 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304001 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304002 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304003 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4004 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304005 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4006 }
4007 memcpy(&io_task->cmd_bhs->iscsi_data_pdu.
4008 dw[offsetof(struct amap_pdu_data_out, lun) / 32],
Andy Grover516f43a2011-06-16 15:57:09 -07004009 &io_task->cmd_bhs->iscsi_hdr.lun, sizeof(struct scsi_lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304010
4011 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004012 cpu_to_be16(*(unsigned short *)
4013 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304014 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4015 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4016 io_task->pwrb_handle->wrb_index);
4017 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4018 be32_to_cpu(task->cmdsn));
4019 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4020 io_task->psgl_handle->sgl_index);
4021
4022 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4023
4024 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4025 io_task->pwrb_handle->nxt_wrb_index);
4026 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4027
4028 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304029 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304030 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4031 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4032
4033 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4034 return 0;
4035}
4036
4037static int beiscsi_mtask(struct iscsi_task *task)
4038{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304039 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304040 struct iscsi_conn *conn = task->conn;
4041 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4042 struct beiscsi_hba *phba = beiscsi_conn->phba;
4043 struct iscsi_wrb *pwrb = NULL;
4044 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304045 unsigned int cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304046
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304047 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304048 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304049 memset(pwrb, 0, sizeof(*pwrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304050 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4051 be32_to_cpu(task->cmdsn));
4052 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4053 io_task->pwrb_handle->wrb_index);
4054 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4055 io_task->psgl_handle->sgl_index);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304056
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304057 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4058 case ISCSI_OP_LOGIN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304059 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4060 TGT_DM_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304061 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4062 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
4063 hwi_write_buffer(pwrb, task);
4064 break;
4065 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004066 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
4067 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4068 TGT_DM_CMD);
4069 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt,
4070 pwrb, 0);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004071 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004072 } else {
4073 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4074 INI_RD_CMD);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004075 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004076 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304077 hwi_write_buffer(pwrb, task);
4078 break;
4079 case ISCSI_OP_TEXT:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304080 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickalb30c6da2010-01-23 05:38:56 +05304081 TGT_DM_CMD);
Jayamohan Kallickal0ecb0b42010-01-05 05:09:19 +05304082 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304083 hwi_write_buffer(pwrb, task);
4084 break;
4085 case ISCSI_OP_SCSI_TMFUNC:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304086 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4087 INI_TMF_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304088 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4089 hwi_write_buffer(pwrb, task);
4090 break;
4091 case ISCSI_OP_LOGOUT:
4092 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4093 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304094 HWH_TYPE_LOGOUT);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304095 hwi_write_buffer(pwrb, task);
4096 break;
4097
4098 default:
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304099 SE_DEBUG(DBG_LVL_1, "opcode =%d Not supported\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304100 task->hdr->opcode & ISCSI_OPCODE_MASK);
4101 return -EINVAL;
4102 }
4103
4104 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
Jayamohan Kallickal51a46252010-01-05 05:10:01 +05304105 task->data_count);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304106 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4107 io_task->pwrb_handle->nxt_wrb_index);
4108 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4109
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304110 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304111 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304112 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4113 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4114 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4115 return 0;
4116}
4117
4118static int beiscsi_task_xmit(struct iscsi_task *task)
4119{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304120 struct beiscsi_io_task *io_task = task->dd_data;
4121 struct scsi_cmnd *sc = task->sc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304122 struct scatterlist *sg;
4123 int num_sg;
4124 unsigned int writedir = 0, xferlen = 0;
4125
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304126 if (!sc)
4127 return beiscsi_mtask(task);
4128
4129 io_task->scsi_cmnd = sc;
4130 num_sg = scsi_dma_map(sc);
4131 if (num_sg < 0) {
4132 SE_DEBUG(DBG_LVL_1, " scsi_dma_map Failed\n")
4133 return num_sg;
4134 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304135 xferlen = scsi_bufflen(sc);
4136 sg = scsi_sglist(sc);
4137 if (sc->sc_data_direction == DMA_TO_DEVICE) {
4138 writedir = 1;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304139 SE_DEBUG(DBG_LVL_4, "task->imm_count=0x%08x\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304140 task->imm_count);
4141 } else
4142 writedir = 0;
4143 return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
4144}
4145
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004146static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304147{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304148 struct hwi_controller *phwi_ctrlr;
4149 struct hwi_context_memory *phwi_context;
4150 struct be_eq_obj *pbe_eq;
4151 unsigned int i, msix_vec;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304152 u8 *real_offset = 0;
4153 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304154
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304155 phwi_ctrlr = phba->phwi_ctrlr;
4156 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304157 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304158 if (phba->msix_enabled) {
4159 for (i = 0; i <= phba->num_cpus; i++) {
4160 msix_vec = phba->msix_entries[i].vector;
4161 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004162 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304163 }
4164 } else
4165 if (phba->pcidev->irq)
4166 free_irq(phba->pcidev->irq, phba);
4167 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304168 destroy_workqueue(phba->wq);
4169 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304170 for (i = 0; i < phba->num_cpus; i++) {
4171 pbe_eq = &phwi_context->be_eq[i];
4172 blk_iopoll_disable(&pbe_eq->iopoll);
4173 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304174
4175 beiscsi_clean_port(phba);
4176 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304177 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4178
4179 value = readl((void *)real_offset);
4180
4181 if (value & 0x00010000) {
4182 value &= 0xfffeffff;
4183 writel(value, (void *)real_offset);
4184 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304185 beiscsi_unmap_pci_function(phba);
4186 pci_free_consistent(phba->pcidev,
4187 phba->ctrl.mbox_mem_alloced.size,
4188 phba->ctrl.mbox_mem_alloced.va,
4189 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004190}
4191
4192static void beiscsi_remove(struct pci_dev *pcidev)
4193{
4194
4195 struct beiscsi_hba *phba = NULL;
4196
4197 phba = pci_get_drvdata(pcidev);
4198 if (!phba) {
4199 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4200 return;
4201 }
4202
4203 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004204 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304205 iscsi_host_remove(phba->shost);
4206 pci_dev_put(phba->pcidev);
4207 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004208 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304209}
4210
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004211static void beiscsi_shutdown(struct pci_dev *pcidev)
4212{
4213
4214 struct beiscsi_hba *phba = NULL;
4215
4216 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4217 if (!phba) {
4218 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4219 return;
4220 }
4221
4222 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004223 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004224}
4225
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304226static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4227{
4228 int i, status;
4229
4230 for (i = 0; i <= phba->num_cpus; i++)
4231 phba->msix_entries[i].entry = i;
4232
4233 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4234 (phba->num_cpus + 1));
4235 if (!status)
4236 phba->msix_enabled = true;
4237
4238 return;
4239}
4240
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304241static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
4242 const struct pci_device_id *id)
4243{
4244 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304245 struct hwi_controller *phwi_ctrlr;
4246 struct hwi_context_memory *phwi_context;
4247 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304248 int ret, num_cpus, i;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304249 u8 *real_offset = 0;
4250 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304251
4252 ret = beiscsi_enable_pci(pcidev);
4253 if (ret < 0) {
Dan Carpenter82284c02010-06-10 09:53:05 +02004254 dev_err(&pcidev->dev, "beiscsi_dev_probe-"
4255 " Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304256 return ret;
4257 }
4258
4259 phba = beiscsi_hba_alloc(pcidev);
4260 if (!phba) {
4261 dev_err(&pcidev->dev, "beiscsi_dev_probe-"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304262 " Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304263 goto disable_pci;
4264 }
4265
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304266 switch (pcidev->device) {
4267 case BE_DEVICE_ID1:
4268 case OC_DEVICE_ID1:
4269 case OC_DEVICE_ID2:
4270 phba->generation = BE_GEN2;
4271 break;
4272 case BE_DEVICE_ID2:
4273 case OC_DEVICE_ID3:
4274 phba->generation = BE_GEN3;
4275 break;
4276 default:
4277 phba->generation = 0;
4278 }
4279
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304280 if (enable_msix)
4281 num_cpus = find_num_cpus();
4282 else
4283 num_cpus = 1;
4284 phba->num_cpus = num_cpus;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304285 SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304286
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004287 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304288 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004289 if (!phba->msix_enabled)
4290 phba->num_cpus = 1;
4291 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304292 ret = be_ctrl_init(phba, pcidev);
4293 if (ret) {
4294 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4295 "Failed in be_ctrl_init\n");
4296 goto hba_free;
4297 }
4298
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304299 if (!num_hba) {
4300 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4301 value = readl((void *)real_offset);
4302 if (value & 0x00010000) {
4303 gcrashmode++;
4304 shost_printk(KERN_ERR, phba->shost,
4305 "Loading Driver in crashdump mode\n");
Jayamohan Kallickale5285862011-10-07 19:31:08 -05004306 ret = beiscsi_cmd_reset_function(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304307 if (ret) {
4308 shost_printk(KERN_ERR, phba->shost,
4309 "Reset Failed. Aborting Crashdump\n");
4310 goto hba_free;
4311 }
4312 ret = be_chk_reset_complete(phba);
4313 if (ret) {
4314 shost_printk(KERN_ERR, phba->shost,
4315 "Failed to get out of reset."
4316 "Aborting Crashdump\n");
4317 goto hba_free;
4318 }
4319 } else {
4320 value |= 0x00010000;
4321 writel(value, (void *)real_offset);
4322 num_hba++;
4323 }
4324 }
4325
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304326 spin_lock_init(&phba->io_sgl_lock);
4327 spin_lock_init(&phba->mgmt_sgl_lock);
4328 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304329 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4330 if (ret != 0) {
4331 shost_printk(KERN_ERR, phba->shost,
4332 "Error getting fw config\n");
4333 goto free_port;
4334 }
4335 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304336 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304337 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304338 ret = beiscsi_init_port(phba);
4339 if (ret < 0) {
4340 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4341 "Failed in beiscsi_init_port\n");
4342 goto free_port;
4343 }
4344
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304345 for (i = 0; i < MAX_MCC_CMD ; i++) {
4346 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4347 phba->ctrl.mcc_tag[i] = i + 1;
4348 phba->ctrl.mcc_numtag[i + 1] = 0;
4349 phba->ctrl.mcc_tag_available++;
4350 }
4351
4352 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4353
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304354 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
4355 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004356 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304357 if (!phba->wq) {
4358 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4359 "Failed to allocate work queue\n");
4360 goto free_twq;
4361 }
4362
4363 INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
4364
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304365 phwi_ctrlr = phba->phwi_ctrlr;
4366 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304367 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304368 for (i = 0; i < phba->num_cpus; i++) {
4369 pbe_eq = &phwi_context->be_eq[i];
4370 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4371 be_iopoll);
4372 blk_iopoll_enable(&pbe_eq->iopoll);
4373 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304374 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304375 ret = beiscsi_init_irqs(phba);
4376 if (ret < 0) {
4377 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4378 "Failed to beiscsi_init_irqs\n");
4379 goto free_blkenbld;
4380 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304381 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004382
4383 if (beiscsi_setup_boot_info(phba))
4384 /*
4385 * log error but continue, because we may not be using
4386 * iscsi boot.
4387 */
4388 shost_printk(KERN_ERR, phba->shost, "Could not set up "
4389 "iSCSI boot info.");
4390
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304391 SE_DEBUG(DBG_LVL_8, "\n\n\n SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304392 return 0;
4393
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304394free_blkenbld:
4395 destroy_workqueue(phba->wq);
4396 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304397 for (i = 0; i < phba->num_cpus; i++) {
4398 pbe_eq = &phwi_context->be_eq[i];
4399 blk_iopoll_disable(&pbe_eq->iopoll);
4400 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304401free_twq:
4402 beiscsi_clean_port(phba);
4403 beiscsi_free_mem(phba);
4404free_port:
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304405 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4406
4407 value = readl((void *)real_offset);
4408
4409 if (value & 0x00010000) {
4410 value &= 0xfffeffff;
4411 writel(value, (void *)real_offset);
4412 }
4413
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304414 pci_free_consistent(phba->pcidev,
4415 phba->ctrl.mbox_mem_alloced.size,
4416 phba->ctrl.mbox_mem_alloced.va,
4417 phba->ctrl.mbox_mem_alloced.dma);
4418 beiscsi_unmap_pci_function(phba);
4419hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304420 if (phba->msix_enabled)
4421 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304422 iscsi_host_remove(phba->shost);
4423 pci_dev_put(phba->pcidev);
4424 iscsi_host_free(phba->shost);
4425disable_pci:
4426 pci_disable_device(pcidev);
4427 return ret;
4428}
4429
4430struct iscsi_transport beiscsi_iscsi_transport = {
4431 .owner = THIS_MODULE,
4432 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05304433 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304434 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304435 .create_session = beiscsi_session_create,
4436 .destroy_session = beiscsi_session_destroy,
4437 .create_conn = beiscsi_conn_create,
4438 .bind_conn = beiscsi_conn_bind,
4439 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05004440 .attr_is_visible = be2iscsi_attr_is_visible,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304441 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004442 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304443 .get_session_param = iscsi_session_get_param,
4444 .get_host_param = beiscsi_get_host_param,
4445 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05004446 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304447 .send_pdu = iscsi_conn_send_pdu,
4448 .xmit_task = beiscsi_task_xmit,
4449 .cleanup_task = beiscsi_cleanup_task,
4450 .alloc_pdu = beiscsi_alloc_pdu,
4451 .parse_pdu_itt = beiscsi_parse_pdu,
4452 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004453 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304454 .ep_connect = beiscsi_ep_connect,
4455 .ep_poll = beiscsi_ep_poll,
4456 .ep_disconnect = beiscsi_ep_disconnect,
4457 .session_recovery_timedout = iscsi_session_recovery_timedout,
4458};
4459
4460static struct pci_driver beiscsi_pci_driver = {
4461 .name = DRV_NAME,
4462 .probe = beiscsi_dev_probe,
4463 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004464 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304465 .id_table = beiscsi_pci_id_table
4466};
4467
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304468
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304469static int __init beiscsi_module_init(void)
4470{
4471 int ret;
4472
4473 beiscsi_scsi_transport =
4474 iscsi_register_transport(&beiscsi_iscsi_transport);
4475 if (!beiscsi_scsi_transport) {
4476 SE_DEBUG(DBG_LVL_1,
4477 "beiscsi_module_init - Unable to register beiscsi"
4478 "transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05304479 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304480 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304481 SE_DEBUG(DBG_LVL_8, "In beiscsi_module_init, tt=%p\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304482 &beiscsi_iscsi_transport);
4483
4484 ret = pci_register_driver(&beiscsi_pci_driver);
4485 if (ret) {
4486 SE_DEBUG(DBG_LVL_1,
4487 "beiscsi_module_init - Unable to register"
4488 "beiscsi pci driver.\n");
4489 goto unregister_iscsi_transport;
4490 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304491 return 0;
4492
4493unregister_iscsi_transport:
4494 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4495 return ret;
4496}
4497
4498static void __exit beiscsi_module_exit(void)
4499{
4500 pci_unregister_driver(&beiscsi_pci_driver);
4501 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4502}
4503
4504module_init(beiscsi_module_init);
4505module_exit(beiscsi_module_exit);